[LLVMdev] C++ Constructors

Chris Lattner clattner at apple.com
Thu Dec 11 20:36:56 PST 2008


On Dec 11, 2008, at 2:45 PM, John Criswell wrote:
>> It is explicit in the bitcode.  You should see an llvm.globalctors
>> global array that has function pointers for each initializer to run.
>>
> Right, I saw that.  I just didn't see anything from main() calling  
> those
> functions, which led me to believe that something else is calling  
> those
> functions before main().

Yep, the code generators handle them magically, usually by emitting  
something to a .init section.

>>> In older versions of LLVM, llvm-gcc added an explicit function
>>> call to __main() as the first instruction in main(); __main() would
>>> call
>>> all global static constructors.  It seems __main() is now gone.
>>>
>>
>> Yep, __main is now gone, llvm.globalctors is king. :)
>>
> I assume putting my constructor at the head of the llvm.globalctors  
> list
> will force it to be run before anything else?  Is that correct?

Yes, that will cause it to run before anything else in the module.  If  
you link to native code with a static ctor, the static ctors in that  
code may run first.

-Chris



More information about the llvm-dev mailing list