global constructors keyed to _blah_
global constructors keyed to _blah_
- Subject: global constructors keyed to _blah_
- From: James Milne <email@hidden>
- Date: Thu, 5 Jan 2006 21:42:15 +0000
I'm trying to track down some weirdness relating to the
initialisation of global variables and constructor functions.
I have the following brief piece of code:
#include <iostream>
bool g_initialised = false;
bool g_wasNotInitialised = false;
extern "C" void __AAA_premain_init( void ) __attribute__((constructor));
void __AAA_premain_init( void )
{
g_initialised = true;
}
class Woggle {
public:
Woggle() { if( !g_initialised ) g_wasNotInitialised = true; }
};
Woggle g_woggle;
int main (int argc, char * const argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
if( g_wasNotInitialised )
{
std::cout << "g_initialised was 'false' when g_woggle's constructor
executed.";
}
return 0;
}
I set breakpoints in the body of __AAA_premain_init and the
constructor of Woggle.
When I debug the application, it hits the breakpoint in Woggle's
constructor first, then it hits the breakpoint in __AAA_premain_init.
Fair enough, I know all about the non-deterministic behaviour of C++
global constructors.
What is puzzling me, however, is that Xcode reports a callstack like
so when I'm debugging the application:
#0 0x00002a08 in __AAA_premain_init at main.cpp:11
#1 0x00002bc0 in global constructors keyed to g_initialised at
main.cpp:40
#2 0x8fe15984 in
__dyld__ZN16ImageLoaderMachO16doInitializationERKN11ImageLoader11LinkCon
textE
#3 0x8fe0bca4 in
__dyld__ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextE
#4 0x8fe0d900 in
__dyld__ZN11ImageLoader15runInitializersERKNS_11LinkContextE
#5 0x8fe030a0 in __dyld__ZN4dyld24initializeMainExecutableEv
#6 0x00002388 in _call_mod_init_funcs at crt.c:304
#7 0x00002178 in _start at crt.c:222
#8 0x000020d8 in start
The curious part is entry #1. What does 'global constructors keyed to
g_initialised' mean?
When I disassemble the __DATA,__mod_init_func section of my
executable, there is only one function pointer in the section. I
would have expected to see two function pointers, one for
__AAA_premain_init, and one for the global constructor of g_woggle.
I must presume that the compiler is generating one glue function that
ultimately calls both __AAA_premain_init and the constructor for
g_woggle.
Also, does anyone know why defining a symbol like so fails?
extern "C" __AAA_premain_init( void ) __attribute__((section
("__DATA,__mod_init_func")));
This fails similarly:
void premain_init( void )
{
g_initialised = true;
}
extern "C" void (*__AA_premain_init)(void) __attribute__((section
("__DATA,__mod_init_func")));
void (*__AA_premain_init)(void) = premain_init;
Doing either of the above yields the following error:
{standard input}:30:section type does not match previous section type
--
Kind regards,
James Milne
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden