You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
7.0KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2020 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Collect2 (GNU Compiler Collection (GCC) Internals)</title>
  21. <meta name="description" content="Collect2 (GNU Compiler Collection (GCC) Internals)">
  22. <meta name="keywords" content="Collect2 (GNU Compiler Collection (GCC) Internals)">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="index.html#Top" rel="up" title="Top">
  30. <link href="Header-Dirs.html#Header-Dirs" rel="next" title="Header Dirs">
  31. <link href="Host-Fragment.html#Host-Fragment" rel="prev" title="Host Fragment">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style: oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Collect2"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Header-Dirs.html#Header-Dirs" accesskey="n" rel="next">Header Dirs</a>, Previous: <a href="Fragments.html#Fragments" accesskey="p" rel="prev">Fragments</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  65. </div>
  66. <hr>
  67. <a name="collect2"></a>
  68. <h2 class="chapter">21 <code>collect2</code></h2>
  69. <p>GCC uses a utility called <code>collect2</code> on nearly all systems to arrange
  70. to call various initialization functions at start time.
  71. </p>
  72. <p>The program <code>collect2</code> works by linking the program once and
  73. looking through the linker output file for symbols with particular names
  74. indicating they are constructor functions. If it finds any, it
  75. creates a new temporary &lsquo;<samp>.c</samp>&rsquo; file containing a table of them,
  76. compiles it, and links the program a second time including that file.
  77. </p>
  78. <a name="index-_005f_005fmain"></a>
  79. <a name="index-constructors_002c-automatic-calls"></a>
  80. <p>The actual calls to the constructors are carried out by a subroutine
  81. called <code>__main</code>, which is called (automatically) at the beginning
  82. of the body of <code>main</code> (provided <code>main</code> was compiled with GNU
  83. CC). Calling <code>__main</code> is necessary, even when compiling C code, to
  84. allow linking C and C++ object code together. (If you use
  85. <samp>-nostdlib</samp>, you get an unresolved reference to <code>__main</code>,
  86. since it&rsquo;s defined in the standard GCC library. Include <samp>-lgcc</samp> at
  87. the end of your compiler command line to resolve this reference.)
  88. </p>
  89. <p>The program <code>collect2</code> is installed as <code>ld</code> in the directory
  90. where the passes of the compiler are installed. When <code>collect2</code>
  91. needs to find the <em>real</em> <code>ld</code>, it tries the following file
  92. names:
  93. </p>
  94. <ul>
  95. <li> a hard coded linker file name, if GCC was configured with the
  96. <samp>--with-ld</samp> option.
  97. </li><li> <samp>real-ld</samp> in the directories listed in the compiler&rsquo;s search
  98. directories.
  99. </li><li> <samp>real-ld</samp> in the directories listed in the environment variable
  100. <code>PATH</code>.
  101. </li><li> The file specified in the <code>REAL_LD_FILE_NAME</code> configuration macro,
  102. if specified.
  103. </li><li> <samp>ld</samp> in the compiler&rsquo;s search directories, except that
  104. <code>collect2</code> will not execute itself recursively.
  105. </li><li> <samp>ld</samp> in <code>PATH</code>.
  106. </li></ul>
  107. <p>&ldquo;The compiler&rsquo;s search directories&rdquo; means all the directories where
  108. <code>gcc</code> searches for passes of the compiler. This includes
  109. directories that you specify with <samp>-B</samp>.
  110. </p>
  111. <p>Cross-compilers search a little differently:
  112. </p>
  113. <ul>
  114. <li> <samp>real-ld</samp> in the compiler&rsquo;s search directories.
  115. </li><li> <samp><var>target</var>-real-ld</samp> in <code>PATH</code>.
  116. </li><li> The file specified in the <code>REAL_LD_FILE_NAME</code> configuration macro,
  117. if specified.
  118. </li><li> <samp>ld</samp> in the compiler&rsquo;s search directories.
  119. </li><li> <samp><var>target</var>-ld</samp> in <code>PATH</code>.
  120. </li></ul>
  121. <p><code>collect2</code> explicitly avoids running <code>ld</code> using the file name
  122. under which <code>collect2</code> itself was invoked. In fact, it remembers
  123. up a list of such names&mdash;in case one copy of <code>collect2</code> finds
  124. another copy (or version) of <code>collect2</code> installed as <code>ld</code> in a
  125. second place in the search path.
  126. </p>
  127. <p><code>collect2</code> searches for the utilities <code>nm</code> and <code>strip</code>
  128. using the same algorithm as above for <code>ld</code>.
  129. </p>
  130. <hr>
  131. <div class="header">
  132. <p>
  133. Next: <a href="Header-Dirs.html#Header-Dirs" accesskey="n" rel="next">Header Dirs</a>, Previous: <a href="Fragments.html#Fragments" accesskey="p" rel="prev">Fragments</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  134. </div>
  135. </body>
  136. </html>