1*ec02198aSmrg@c Copyright (C) 1988-2020 Free Software Foundation, Inc.
210d565efSmrg@c This is part of the GCC manual.
310d565efSmrg@c For copying conditions, see the file gcc.texi.
410d565efSmrg
510d565efSmrg@node Collect2
610d565efSmrg@chapter @code{collect2}
710d565efSmrg
810d565efSmrgGCC uses a utility called @code{collect2} on nearly all systems to arrange
910d565efSmrgto call various initialization functions at start time.
1010d565efSmrg
1110d565efSmrgThe program @code{collect2} works by linking the program once and
1210d565efSmrglooking through the linker output file for symbols with particular names
1310d565efSmrgindicating they are constructor functions.  If it finds any, it
1410d565efSmrgcreates a new temporary @samp{.c} file containing a table of them,
1510d565efSmrgcompiles it, and links the program a second time including that file.
1610d565efSmrg
1710d565efSmrg@findex __main
1810d565efSmrg@cindex constructors, automatic calls
1910d565efSmrgThe actual calls to the constructors are carried out by a subroutine
2010d565efSmrgcalled @code{__main}, which is called (automatically) at the beginning
2110d565efSmrgof the body of @code{main} (provided @code{main} was compiled with GNU
2210d565efSmrgCC)@.  Calling @code{__main} is necessary, even when compiling C code, to
2310d565efSmrgallow linking C and C++ object code together.  (If you use
2410d565efSmrg@option{-nostdlib}, you get an unresolved reference to @code{__main},
2510d565efSmrgsince it's defined in the standard GCC library.  Include @option{-lgcc} at
2610d565efSmrgthe end of your compiler command line to resolve this reference.)
2710d565efSmrg
2810d565efSmrgThe program @code{collect2} is installed as @code{ld} in the directory
2910d565efSmrgwhere the passes of the compiler are installed.  When @code{collect2}
3010d565efSmrgneeds to find the @emph{real} @code{ld}, it tries the following file
3110d565efSmrgnames:
3210d565efSmrg
3310d565efSmrg@itemize @bullet
3410d565efSmrg@item
3510d565efSmrga hard coded linker file name, if GCC was configured with the
3610d565efSmrg@option{--with-ld} option.
3710d565efSmrg
3810d565efSmrg@item
3910d565efSmrg@file{real-ld} in the directories listed in the compiler's search
4010d565efSmrgdirectories.
4110d565efSmrg
4210d565efSmrg@item
4310d565efSmrg@file{real-ld} in the directories listed in the environment variable
4410d565efSmrg@code{PATH}.
4510d565efSmrg
4610d565efSmrg@item
4710d565efSmrgThe file specified in the @code{REAL_LD_FILE_NAME} configuration macro,
4810d565efSmrgif specified.
4910d565efSmrg
5010d565efSmrg@item
5110d565efSmrg@file{ld} in the compiler's search directories, except that
5210d565efSmrg@code{collect2} will not execute itself recursively.
5310d565efSmrg
5410d565efSmrg@item
5510d565efSmrg@file{ld} in @code{PATH}.
5610d565efSmrg@end itemize
5710d565efSmrg
5810d565efSmrg``The compiler's search directories'' means all the directories where
5910d565efSmrg@command{gcc} searches for passes of the compiler.  This includes
6010d565efSmrgdirectories that you specify with @option{-B}.
6110d565efSmrg
6210d565efSmrgCross-compilers search a little differently:
6310d565efSmrg
6410d565efSmrg@itemize @bullet
6510d565efSmrg@item
6610d565efSmrg@file{real-ld} in the compiler's search directories.
6710d565efSmrg
6810d565efSmrg@item
6910d565efSmrg@file{@var{target}-real-ld} in @code{PATH}.
7010d565efSmrg
7110d565efSmrg@item
7210d565efSmrgThe file specified in the @code{REAL_LD_FILE_NAME} configuration macro,
7310d565efSmrgif specified.
7410d565efSmrg
7510d565efSmrg@item
7610d565efSmrg@file{ld} in the compiler's search directories.
7710d565efSmrg
7810d565efSmrg@item
7910d565efSmrg@file{@var{target}-ld} in @code{PATH}.
8010d565efSmrg@end itemize
8110d565efSmrg
8210d565efSmrg@code{collect2} explicitly avoids running @code{ld} using the file name
8310d565efSmrgunder which @code{collect2} itself was invoked.  In fact, it remembers
8410d565efSmrgup a list of such names---in case one copy of @code{collect2} finds
8510d565efSmrganother copy (or version) of @code{collect2} installed as @code{ld} in a
8610d565efSmrgsecond place in the search path.
8710d565efSmrg
8810d565efSmrg@code{collect2} searches for the utilities @code{nm} and @code{strip}
8910d565efSmrgusing the same algorithm as above for @code{ld}.
90