A small doubt

Hi LLVMDev,

The GCC frontend for LLVM inserts a call to a __main() in the
main() function. Whats the purpose of this? And which LLVM
library do I need to link to satisfy the reference?

Thanks,
Rahul

Hi LLVMDev,

The GCC frontend for LLVM inserts a call to a __main() in the
main() function. Whats the purpose of this? And which LLVM
library do I need to link to satisfy the reference?

__main is used primarily to call static constructors. The way you
satisfy this dependency is to link the bytecode file against crtend.o
(aka libcrtend.bc). This is normally accomplished by using gccld
(or by llvm-gcc <bytecode files> -o program, which calls gccld).

If you're in a pinch, and you absolutely know for sure that you
won't ever have any static constructors called in your program, you
can replace __main with an empty function. But I heartily recommend
the solution above, because it's much more complete and only minimally
more painful.

Hope this helps,
-Brian

Brian is absolutely right. Note that static constructors really only
happen in C++ programs (or C programs which use the GCC
constructor/destructor extensions, which are really really rare).
However, gccld should elimination all of the overhead of the __main
function if the functionality is not used, so C programs don't suffer any
overhead, and you can just delete the call to __main if it is causing you
grief.

If you're interested, the source code for __main lives in:
llvm/runtime/GCCLibraries/crtend/crtend.c & listend.ll

-Chris