Initialization.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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-2017 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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>GNU Compiler Collection (GCC) Internals: Initialization</title>
  20. <meta name="description" content="GNU Compiler Collection (GCC) Internals: Initialization">
  21. <meta name="keywords" content="GNU Compiler Collection (GCC) Internals: Initialization">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  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="Assembler-Format.html#Assembler-Format" rel="up" title="Assembler Format">
  30. <link href="Macros-for-Initialization.html#Macros-for-Initialization" rel="next" title="Macros for Initialization">
  31. <link href="Label-Output.html#Label-Output" rel="prev" title="Label Output">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Initialization"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Macros-for-Initialization.html#Macros-for-Initialization" accesskey="n" rel="next">Macros for Initialization</a>, Previous: <a href="Label-Output.html#Label-Output" accesskey="p" rel="prev">Label Output</a>, Up: <a href="Assembler-Format.html#Assembler-Format" accesskey="u" rel="up">Assembler Format</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>
  66. </div>
  67. <hr>
  68. <a name="How-Initialization-Functions-Are-Handled"></a>
  69. <h4 class="subsection">17.20.5 How Initialization Functions Are Handled</h4>
  70. <a name="index-initialization-routines"></a>
  71. <a name="index-termination-routines"></a>
  72. <a name="index-constructors_002c-output-of"></a>
  73. <a name="index-destructors_002c-output-of"></a>
  74. <p>The compiled code for certain languages includes <em>constructors</em>
  75. (also called <em>initialization routines</em>)&mdash;functions to initialize
  76. data in the program when the program is started. These functions need
  77. to be called before the program is &ldquo;started&rdquo;&mdash;that is to say, before
  78. <code>main</code> is called.
  79. </p>
  80. <p>Compiling some languages generates <em>destructors</em> (also called
  81. <em>termination routines</em>) that should be called when the program
  82. terminates.
  83. </p>
  84. <p>To make the initialization and termination functions work, the compiler
  85. must output something in the assembler code to cause those functions to
  86. be called at the appropriate time. When you port the compiler to a new
  87. system, you need to specify how to do this.
  88. </p>
  89. <p>There are two major ways that GCC currently supports the execution of
  90. initialization and termination functions. Each way has two variants.
  91. Much of the structure is common to all four variations.
  92. </p>
  93. <a name="index-_005f_005fCTOR_005fLIST_005f_005f"></a>
  94. <a name="index-_005f_005fDTOR_005fLIST_005f_005f"></a>
  95. <p>The linker must build two lists of these functions&mdash;a list of
  96. initialization functions, called <code>__CTOR_LIST__</code>, and a list of
  97. termination functions, called <code>__DTOR_LIST__</code>.
  98. </p>
  99. <p>Each list always begins with an ignored function pointer (which may hold
  100. 0, -1, or a count of the function pointers after it, depending on
  101. the environment). This is followed by a series of zero or more function
  102. pointers to constructors (or destructors), followed by a function
  103. pointer containing zero.
  104. </p>
  105. <p>Depending on the operating system and its executable file format, either
  106. <samp>crtstuff.c</samp> or <samp>libgcc2.c</samp> traverses these lists at startup
  107. time and exit time. Constructors are called in reverse order of the
  108. list; destructors in forward order.
  109. </p>
  110. <p>The best way to handle static constructors works only for object file
  111. formats which provide arbitrarily-named sections. A section is set
  112. aside for a list of constructors, and another for a list of destructors.
  113. Traditionally these are called &lsquo;<samp>.ctors</samp>&rsquo; and &lsquo;<samp>.dtors</samp>&rsquo;. Each
  114. object file that defines an initialization function also puts a word in
  115. the constructor section to point to that function. The linker
  116. accumulates all these words into one contiguous &lsquo;<samp>.ctors</samp>&rsquo; section.
  117. Termination functions are handled similarly.
  118. </p>
  119. <p>This method will be chosen as the default by <samp>target-def.h</samp> if
  120. <code>TARGET_ASM_NAMED_SECTION</code> is defined. A target that does not
  121. support arbitrary sections, but does support special designated
  122. constructor and destructor sections may define <code>CTORS_SECTION_ASM_OP</code>
  123. and <code>DTORS_SECTION_ASM_OP</code> to achieve the same effect.
  124. </p>
  125. <p>When arbitrary sections are available, there are two variants, depending
  126. upon how the code in <samp>crtstuff.c</samp> is called. On systems that
  127. support a <em>.init</em> section which is executed at program startup,
  128. parts of <samp>crtstuff.c</samp> are compiled into that section. The
  129. program is linked by the <code>gcc</code> driver like this:
  130. </p>
  131. <div class="smallexample">
  132. <pre class="smallexample">ld -o <var>output_file</var> crti.o crtbegin.o &hellip; -lgcc crtend.o crtn.o
  133. </pre></div>
  134. <p>The prologue of a function (<code>__init</code>) appears in the <code>.init</code>
  135. section of <samp>crti.o</samp>; the epilogue appears in <samp>crtn.o</samp>. Likewise
  136. for the function <code>__fini</code> in the <em>.fini</em> section. Normally these
  137. files are provided by the operating system or by the GNU C library, but
  138. are provided by GCC for a few targets.
  139. </p>
  140. <p>The objects <samp>crtbegin.o</samp> and <samp>crtend.o</samp> are (for most targets)
  141. compiled from <samp>crtstuff.c</samp>. They contain, among other things, code
  142. fragments within the <code>.init</code> and <code>.fini</code> sections that branch
  143. to routines in the <code>.text</code> section. The linker will pull all parts
  144. of a section together, which results in a complete <code>__init</code> function
  145. that invokes the routines we need at startup.
  146. </p>
  147. <p>To use this variant, you must define the <code>INIT_SECTION_ASM_OP</code>
  148. macro properly.
  149. </p>
  150. <p>If no init section is available, when GCC compiles any function called
  151. <code>main</code> (or more accurately, any function designated as a program
  152. entry point by the language front end calling <code>expand_main_function</code>),
  153. it inserts a procedure call to <code>__main</code> as the first executable code
  154. after the function prologue. The <code>__main</code> function is defined
  155. in <samp>libgcc2.c</samp> and runs the global constructors.
  156. </p>
  157. <p>In file formats that don&rsquo;t support arbitrary sections, there are again
  158. two variants. In the simplest variant, the GNU linker (GNU <code>ld</code>)
  159. and an &lsquo;a.out&rsquo; format must be used. In this case,
  160. <code>TARGET_ASM_CONSTRUCTOR</code> is defined to produce a <code>.stabs</code>
  161. entry of type &lsquo;<samp>N_SETT</samp>&rsquo;, referencing the name <code>__CTOR_LIST__</code>,
  162. and with the address of the void function containing the initialization
  163. code as its value. The GNU linker recognizes this as a request to add
  164. the value to a <em>set</em>; the values are accumulated, and are eventually
  165. placed in the executable as a vector in the format described above, with
  166. a leading (ignored) count and a trailing zero element.
  167. <code>TARGET_ASM_DESTRUCTOR</code> is handled similarly. Since no init
  168. section is available, the absence of <code>INIT_SECTION_ASM_OP</code> causes
  169. the compilation of <code>main</code> to call <code>__main</code> as above, starting
  170. the initialization process.
  171. </p>
  172. <p>The last variant uses neither arbitrary sections nor the GNU linker.
  173. This is preferable when you want to do dynamic linking and when using
  174. file formats which the GNU linker does not support, such as &lsquo;ECOFF&rsquo;. In
  175. this case, <code>TARGET_HAVE_CTORS_DTORS</code> is false, initialization and
  176. termination functions are recognized simply by their names. This requires
  177. an extra program in the linkage step, called <code>collect2</code>. This program
  178. pretends to be the linker, for use with GCC; it does its job by running
  179. the ordinary linker, but also arranges to include the vectors of
  180. initialization and termination functions. These functions are called
  181. via <code>__main</code> as described above. In order to use this method,
  182. <code>use_collect2</code> must be defined in the target in <samp>config.gcc</samp>.
  183. </p>
  184. <hr>
  185. <div class="header">
  186. <p>
  187. Next: <a href="Macros-for-Initialization.html#Macros-for-Initialization" accesskey="n" rel="next">Macros for Initialization</a>, Previous: <a href="Label-Output.html#Label-Output" accesskey="p" rel="prev">Label Output</a>, Up: <a href="Assembler-Format.html#Assembler-Format" accesskey="u" rel="up">Assembler Format</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>
  188. </div>
  189. </body>
  190. </html>