gccint: Collect2

1 
1 21 'collect2'
1 *************
1 
1 GCC uses a utility called 'collect2' on nearly all systems to arrange to
1 call various initialization functions at start time.
1 
1  The program 'collect2' works by linking the program once and looking
1 through the linker output file for symbols with particular names
1 indicating they are constructor functions.  If it finds any, it creates
1 a new temporary '.c' file containing a table of them, compiles it, and
1 links the program a second time including that file.
1 
1  The actual calls to the constructors are carried out by a subroutine
1 called '__main', which is called (automatically) at the beginning of the
1 body of 'main' (provided 'main' was compiled with GNU CC).  Calling
1 '__main' is necessary, even when compiling C code, to allow linking C
1 and C++ object code together.  (If you use '-nostdlib', you get an
1 unresolved reference to '__main', since it's defined in the standard GCC
1 library.  Include '-lgcc' at the end of your compiler command line to
1 resolve this reference.)
1 
1  The program 'collect2' is installed as 'ld' in the directory where the
1 passes of the compiler are installed.  When 'collect2' needs to find the
1 _real_ 'ld', it tries the following file names:
1 
1    * a hard coded linker file name, if GCC was configured with the
1      '--with-ld' option.
1 
1    * 'real-ld' in the directories listed in the compiler's search
1      directories.
1 
1    * 'real-ld' in the directories listed in the environment variable
1      'PATH'.
1 
1    * The file specified in the 'REAL_LD_FILE_NAME' configuration macro,
1      if specified.
1 
1    * 'ld' in the compiler's search directories, except that 'collect2'
1      will not execute itself recursively.
1 
1    * 'ld' in 'PATH'.
1 
1  "The compiler's search directories" means all the directories where
1 'gcc' searches for passes of the compiler.  This includes directories
1 that you specify with '-B'.
1 
1  Cross-compilers search a little differently:
1 
1    * 'real-ld' in the compiler's search directories.
1 
1    * 'TARGET-real-ld' in 'PATH'.
1 
1    * The file specified in the 'REAL_LD_FILE_NAME' configuration macro,
1      if specified.
1 
1    * 'ld' in the compiler's search directories.
1 
1    * 'TARGET-ld' in 'PATH'.
1 
1  'collect2' explicitly avoids running 'ld' using the file name under
1 which 'collect2' itself was invoked.  In fact, it remembers up a list of
1 such names--in case one copy of 'collect2' finds another copy (or
1 version) of 'collect2' installed as 'ld' in a second place in the search
1 path.
1 
1  'collect2' searches for the utilities 'nm' and 'strip' using the same
1 algorithm as above for 'ld'.
1