選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

205 行
11KB

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