views:

116

answers:

1

I'm writing C++ code to run in a freestanding environment (basically an ARM board). It's been going well except I've run into a stumbling block - global static constructors.

To my understanding the .ctors section contains a list of addresses to each static constructor, and my code simply needs to iterate this list and make calls to each function as it goes. However, I've found that this section in my binary is in fact completely empty! Google pointed towards using ".init_array" instead of ".ctors" (an EABI thing), but that has not changed anything.

Any ideas as to why my static constructors don't exist? Relevant linker script and objdump output follows:

.ctors :
{
    . = ALIGN(4096);
    start_ctors = .;
    *(.init_array);
    *(.ctors);
    end_ctors = .;
}

.dtors :
{
    . = ALIGN(4096);
    start_dtors = .;
    *(.fini_array);
    *(.dtors);
    end_dtors = .;
}

--

2 .ctors        00001000  8014c000  8014c000  00054000  2**2
                CONTENTS, ALLOC, LOAD, DATA
<snip>
8014d000 g     O .ctors 00000004 start_ctors
<snip>
8014d000 g     O .ctors 00000004 end_ctors

I'm using an arm-elf targeted GCC compiler (4.4.1).

Update: The output binary is also full of __static_initialization_and_destruction_0 symbols, which I've never seen before.

Update 2: This is an excerpt from an objdump of a compiled object file (which is linked into the main binary) with the .ctors section intact:

21 .ctors        00000004  00000000  00000000  00000864  2**2
                 CONTENTS, ALLOC, LOAD, RELOC, DATA

RELOCATION RECORDS FOR [.ctors]:
OFFSET   TYPE              VALUE 
00000000 R_ARM_ABS32       _GLOBAL__I__ZN9SomeStaticClass10m_InstanceE
A: 

This ended up being a build system problem - the linker script was being specified multiple times on the linker command line, which somehow caused g++ to choke.

Matthew Iselin