Output-Section-Keywords.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- This file documents the GNU linker LD
  4. (GNU Binutils)
  5. version 2.28.
  6. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  7. Permission is granted to copy, distribute and/or modify this document
  8. under the terms of the GNU Free Documentation License, Version 1.3
  9. or any later version published by the Free Software Foundation;
  10. with no Invariant Sections, with no Front-Cover Texts, and with no
  11. Back-Cover Texts. A copy of the license is included in the
  12. section entitled "GNU Free Documentation License". -->
  13. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>LD: Output Section Keywords</title>
  16. <meta name="description" content="LD: Output Section Keywords">
  17. <meta name="keywords" content="LD: Output Section Keywords">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="SECTIONS.html#SECTIONS" rel="up" title="SECTIONS">
  26. <link href="Output-Section-Discarding.html#Output-Section-Discarding" rel="next" title="Output Section Discarding">
  27. <link href="Output-Section-Data.html#Output-Section-Data" rel="prev" title="Output Section Data">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="Output-Section-Keywords"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Output-Section-Discarding.html#Output-Section-Discarding" accesskey="n" rel="next">Output Section Discarding</a>, Previous: <a href="Output-Section-Data.html#Output-Section-Data" accesskey="p" rel="prev">Output Section Data</a>, Up: <a href="SECTIONS.html#SECTIONS" accesskey="u" rel="up">SECTIONS</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="LD-Index.html#LD-Index" title="Index" rel="index">Index</a>]</p>
  62. </div>
  63. <hr>
  64. <a name="Output-Section-Keywords-1"></a>
  65. <h4 class="subsection">3.6.6 Output Section Keywords</h4>
  66. <p>There are a couple of keywords which can appear as output section
  67. commands.
  68. </p>
  69. <dl compact="compact">
  70. <dd><a name="index-CREATE_005fOBJECT_005fSYMBOLS"></a>
  71. <a name="index-input-filename-symbols"></a>
  72. <a name="index-filename-symbols"></a>
  73. </dd>
  74. <dt><code>CREATE_OBJECT_SYMBOLS</code></dt>
  75. <dd><p>The command tells the linker to create a symbol for each input file.
  76. The name of each symbol will be the name of the corresponding input
  77. file. The section of each symbol will be the output section in which
  78. the <code>CREATE_OBJECT_SYMBOLS</code> command appears.
  79. </p>
  80. <p>This is conventional for the a.out object file format. It is not
  81. normally used for any other object file format.
  82. </p>
  83. <a name="index-CONSTRUCTORS"></a>
  84. <a name="index-C_002b_002b-constructors_002c-arranging-in-link"></a>
  85. <a name="index-constructors_002c-arranging-in-link"></a>
  86. </dd>
  87. <dt><code>CONSTRUCTORS</code></dt>
  88. <dd><p>When linking using the a.out object file format, the linker uses an
  89. unusual set construct to support C++ global constructors and
  90. destructors. When linking object file formats which do not support
  91. arbitrary sections, such as ECOFF and XCOFF, the linker will
  92. automatically recognize C++ global constructors and destructors by name.
  93. For these object file formats, the <code>CONSTRUCTORS</code> command tells the
  94. linker to place constructor information in the output section where the
  95. <code>CONSTRUCTORS</code> command appears. The <code>CONSTRUCTORS</code> command is
  96. ignored for other object file formats.
  97. </p>
  98. <p>The symbol <code><span class="nolinebreak">__CTOR_LIST__</span></code><!-- /@w --> marks the start of the global
  99. constructors, and the symbol <code><span class="nolinebreak">__CTOR_END__</span></code><!-- /@w --> marks the end.
  100. Similarly, <code><span class="nolinebreak">__DTOR_LIST__</span></code><!-- /@w --> and <code><span class="nolinebreak">__DTOR_END__</span></code><!-- /@w --> mark
  101. the start and end of the global destructors. The
  102. first word in the list is the number of entries, followed by the address
  103. of each constructor or destructor, followed by a zero word. The
  104. compiler must arrange to actually run the code. For these object file
  105. formats <small>GNU</small> C++ normally calls constructors from a subroutine
  106. <code>__main</code>; a call to <code>__main</code> is automatically inserted into
  107. the startup code for <code>main</code>. <small>GNU</small> C++ normally runs
  108. destructors either by using <code>atexit</code>, or directly from the function
  109. <code>exit</code>.
  110. </p>
  111. <p>For object file formats such as <code>COFF</code> or <code>ELF</code> which support
  112. arbitrary section names, <small>GNU</small> C++ will normally arrange to put the
  113. addresses of global constructors and destructors into the <code>.ctors</code>
  114. and <code>.dtors</code> sections. Placing the following sequence into your
  115. linker script will build the sort of table which the <small>GNU</small> C++
  116. runtime code expects to see.
  117. </p>
  118. <div class="smallexample">
  119. <pre class="smallexample"> __CTOR_LIST__ = .;
  120. LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2)
  121. *(.ctors)
  122. LONG(0)
  123. __CTOR_END__ = .;
  124. __DTOR_LIST__ = .;
  125. LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2)
  126. *(.dtors)
  127. LONG(0)
  128. __DTOR_END__ = .;
  129. </pre></div>
  130. <p>If you are using the <small>GNU</small> C++ support for initialization priority,
  131. which provides some control over the order in which global constructors
  132. are run, you must sort the constructors at link time to ensure that they
  133. are executed in the correct order. When using the <code>CONSTRUCTORS</code>
  134. command, use &lsquo;<samp>SORT_BY_NAME(CONSTRUCTORS)</samp>&rsquo; instead. When using the
  135. <code>.ctors</code> and <code>.dtors</code> sections, use &lsquo;<samp>*(SORT_BY_NAME(.ctors))</samp>&rsquo; and
  136. &lsquo;<samp>*(SORT_BY_NAME(.dtors))</samp>&rsquo; instead of just &lsquo;<samp>*(.ctors)</samp>&rsquo; and
  137. &lsquo;<samp>*(.dtors)</samp>&rsquo;.
  138. </p>
  139. <p>Normally the compiler and linker will handle these issues automatically,
  140. and you will not need to concern yourself with them. However, you may
  141. need to consider this if you are using C++ and writing your own linker
  142. scripts.
  143. </p>
  144. </dd>
  145. </dl>
  146. <hr>
  147. <div class="header">
  148. <p>
  149. Next: <a href="Output-Section-Discarding.html#Output-Section-Discarding" accesskey="n" rel="next">Output Section Discarding</a>, Previous: <a href="Output-Section-Data.html#Output-Section-Data" accesskey="p" rel="prev">Output Section Data</a>, Up: <a href="SECTIONS.html#SECTIONS" accesskey="u" rel="up">SECTIONS</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="LD-Index.html#LD-Index" title="Index" rel="index">Index</a>]</p>
  150. </div>
  151. </body>
  152. </html>