f78
Many target systems do not have support in the assembler and linker for “constructors”—initialization functions to be called before the official “start” of main. On such systems, GNU CC uses a utility called collect2 to arrange to call these functions at start time.
The program collect2 works by linking the program once and looking through the linker output file for symbols with particular names indicating they are constructor functions. If it finds any, it creates a new temporary ‘.c’ file containing a table of them, compiles it, and links the program a second time including that file.
The actual calls to the constructors are carried out by a subroutine called __main, which is called (automatically) at the beginning of the body of main (provided main was compiled with GNU CC).
Calling __main
is necessary, even when compiling C code, to allow linking C and C++ object
code together. (If you use ‘-nostdlib’,
you get an unresolved reference to __main,
since it’s defined in the standard GCC library. Include
‘-lgcc’
at the end of your compiler command line to resolve this reference.)
The program, collect2, is installed as ‘ld’ in the directory where the passes of the compiler are installed. When collect2 needs to find the real ‘ld’, it tries the following filenames.
Cross-compilers search a little differently, using the following filenames.
collect2 searches for the utilities, nm and strip, using the same algorithm as previous installation for ‘ld’. 0