Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5226690
  • 博文数量: 1696
  • 博客积分: 10870
  • 博客等级: 上将
  • 技术积分: 18357
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-30 15:16
文章分类
文章存档

2017年(1)

2016年(1)

2015年(1)

2013年(1)

2012年(43)

2011年(17)

2010年(828)

2009年(568)

2008年(185)

2007年(51)

分类: LINUX

2008-02-29 16:12:33

Next: , Previous: Fragments, Up: Top

16 collect2

GCC uses a utility called collect2 on nearly all systems to arrange to call various initialization 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 file names:

  • real-ld in the directories listed in the compiler's search directories.
  • real-ld in the directories listed in the environment variable PATH.
  • The file specified in the REAL_LD_FILE_NAME configuration macro, if specified.
  • ld in the compiler's search directories, except that collect2 will not execute itself recursively.
  • ld in PATH.

“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:

  • real-ld in the compiler's search directories.
  • target-real-ld in PATH.
  • The file specified in the REAL_LD_FILE_NAME configuration macro, if specified.
  • ld in the compiler's search directories.
  • target-ld in PATH.

collect2 explicitly avoids running ld using the file name under which collect2 itself was invoked. In fact, it remembers up a list of 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.

collect2 searches for the utilities nm and strip using the same algorithm as above for ld.
阅读(1036) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~