Go to the previous, next section.

collect2

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).

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 file names:

"The compiler's search directories" means all the directories where gcc searches for passes of the compiler. This includes directories that you specify with `-B'.

Cross-compilers search a little differently:

collect2 does not search for `ld' using the compiler's search directories, because if it did, it would find itself--not the real ld---and this could lead to infinite recursion. However, the directory where collect2 is installed might happen to be in PATH. That could lead collect2 to invoke itself anyway. when looking for ld.

To prevent this, collect2 explicitly avoids running ld using the file name under which collect2 itself was invoked. In fact, it remembers up to two such names--in case one copy of collect2 finds another copy (or version) of collect2 installed as ld in a second place in the search path.

If two file names to avoid are not sufficient, you may still encounter an infinite recursion of collect2 processes. When this happens. check all the files installed as `ld' in any of the directories searched, and straighten out the situation.

(In a future version, we will probably change collect2 to avoid any reinvocation of a file from which any parent collect2 was run.)

Go to the previous, next section.