Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

219 lines
8.7KB

  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 Arm Embedded Toolchain 10-2020-q4-major)
  5. version 2.35.1.
  6. Copyright (C) 1991-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Location Counter (LD)</title>
  17. <meta name="description" content="Location Counter (LD)">
  18. <meta name="keywords" content="Location Counter (LD)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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="Expressions.html#Expressions" rel="up" title="Expressions">
  26. <link href="Operators.html#Operators" rel="next" title="Operators">
  27. <link href="Orphan-Sections.html#Orphan-Sections" rel="prev" title="Orphan Sections">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Location-Counter"></a>
  58. <div class="header">
  59. <p>
  60. Next: <a href="Operators.html#Operators" accesskey="n" rel="next">Operators</a>, Previous: <a href="Orphan-Sections.html#Orphan-Sections" accesskey="p" rel="prev">Orphan Sections</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">Expressions</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>
  61. </div>
  62. <hr>
  63. <a name="The-Location-Counter"></a>
  64. <h4 class="subsection">3.10.5 The Location Counter</h4>
  65. <a name="index-_002e"></a>
  66. <a name="index-dot"></a>
  67. <a name="index-location-counter"></a>
  68. <a name="index-current-output-location"></a>
  69. <p>The special linker variable <em>dot</em> &lsquo;<samp>.</samp>&rsquo; always contains the
  70. current output location counter. Since the <code>.</code> always refers to a
  71. location in an output section, it may only appear in an expression
  72. within a <code>SECTIONS</code> command. The <code>.</code> symbol may appear
  73. anywhere that an ordinary symbol is allowed in an expression.
  74. </p>
  75. <a name="index-holes"></a>
  76. <p>Assigning a value to <code>.</code> will cause the location counter to be
  77. moved. This may be used to create holes in the output section. The
  78. location counter may not be moved backwards inside an output section,
  79. and may not be moved backwards outside of an output section if so
  80. doing creates areas with overlapping LMAs.
  81. </p>
  82. <div class="smallexample">
  83. <pre class="smallexample">SECTIONS
  84. {
  85. output :
  86. {
  87. file1(.text)
  88. . = . + 1000;
  89. file2(.text)
  90. . += 1000;
  91. file3(.text)
  92. } = 0x12345678;
  93. }
  94. </pre></div>
  95. <p>In the previous example, the &lsquo;<samp>.text</samp>&rsquo; section from <samp>file1</samp> is
  96. located at the beginning of the output section &lsquo;<samp>output</samp>&rsquo;. It is
  97. followed by a 1000 byte gap. Then the &lsquo;<samp>.text</samp>&rsquo; section from
  98. <samp>file2</samp> appears, also with a 1000 byte gap following before the
  99. &lsquo;<samp>.text</samp>&rsquo; section from <samp>file3</samp>. The notation &lsquo;<samp>= 0x12345678</samp>&rsquo;
  100. specifies what data to write in the gaps (see <a href="Output-Section-Fill.html#Output-Section-Fill">Output Section Fill</a>).
  101. </p>
  102. <a name="index-dot-inside-sections"></a>
  103. <p>Note: <code>.</code> actually refers to the byte offset from the start of the
  104. current containing object. Normally this is the <code>SECTIONS</code>
  105. statement, whose start address is 0, hence <code>.</code> can be used as an
  106. absolute address. If <code>.</code> is used inside a section description
  107. however, it refers to the byte offset from the start of that section,
  108. not an absolute address. Thus in a script like this:
  109. </p>
  110. <div class="smallexample">
  111. <pre class="smallexample">SECTIONS
  112. {
  113. . = 0x100
  114. .text: {
  115. *(.text)
  116. . = 0x200
  117. }
  118. . = 0x500
  119. .data: {
  120. *(.data)
  121. . += 0x600
  122. }
  123. }
  124. </pre></div>
  125. <p>The &lsquo;<samp>.text</samp>&rsquo; section will be assigned a starting address of 0x100
  126. and a size of exactly 0x200 bytes, even if there is not enough data in
  127. the &lsquo;<samp>.text</samp>&rsquo; input sections to fill this area. (If there is too
  128. much data, an error will be produced because this would be an attempt to
  129. move <code>.</code> backwards). The &lsquo;<samp>.data</samp>&rsquo; section will start at 0x500
  130. and it will have an extra 0x600 bytes worth of space after the end of
  131. the values from the &lsquo;<samp>.data</samp>&rsquo; input sections and before the end of
  132. the &lsquo;<samp>.data</samp>&rsquo; output section itself.
  133. </p>
  134. <a name="index-dot-outside-sections"></a>
  135. <p>Setting symbols to the value of the location counter outside of an
  136. output section statement can result in unexpected values if the linker
  137. needs to place orphan sections. For example, given the following:
  138. </p>
  139. <div class="smallexample">
  140. <pre class="smallexample">SECTIONS
  141. {
  142. start_of_text = . ;
  143. .text: { *(.text) }
  144. end_of_text = . ;
  145. start_of_data = . ;
  146. .data: { *(.data) }
  147. end_of_data = . ;
  148. }
  149. </pre></div>
  150. <p>If the linker needs to place some input section, e.g. <code>.rodata</code>,
  151. not mentioned in the script, it might choose to place that section
  152. between <code>.text</code> and <code>.data</code>. You might think the linker
  153. should place <code>.rodata</code> on the blank line in the above script, but
  154. blank lines are of no particular significance to the linker. As well,
  155. the linker doesn&rsquo;t associate the above symbol names with their
  156. sections. Instead, it assumes that all assignments or other
  157. statements belong to the previous output section, except for the
  158. special case of an assignment to <code>.</code>. I.e., the linker will
  159. place the orphan <code>.rodata</code> section as if the script was written
  160. as follows:
  161. </p>
  162. <div class="smallexample">
  163. <pre class="smallexample">SECTIONS
  164. {
  165. start_of_text = . ;
  166. .text: { *(.text) }
  167. end_of_text = . ;
  168. start_of_data = . ;
  169. .rodata: { *(.rodata) }
  170. .data: { *(.data) }
  171. end_of_data = . ;
  172. }
  173. </pre></div>
  174. <p>This may or may not be the script author&rsquo;s intention for the value of
  175. <code>start_of_data</code>. One way to influence the orphan section
  176. placement is to assign the location counter to itself, as the linker
  177. assumes that an assignment to <code>.</code> is setting the start address of
  178. a following output section and thus should be grouped with that
  179. section. So you could write:
  180. </p>
  181. <div class="smallexample">
  182. <pre class="smallexample">SECTIONS
  183. {
  184. start_of_text = . ;
  185. .text: { *(.text) }
  186. end_of_text = . ;
  187. . = . ;
  188. start_of_data = . ;
  189. .data: { *(.data) }
  190. end_of_data = . ;
  191. }
  192. </pre></div>
  193. <p>Now, the orphan <code>.rodata</code> section will be placed between
  194. <code>end_of_text</code> and <code>start_of_data</code>.
  195. </p>
  196. <hr>
  197. <div class="header">
  198. <p>
  199. Next: <a href="Operators.html#Operators" accesskey="n" rel="next">Operators</a>, Previous: <a href="Orphan-Sections.html#Orphan-Sections" accesskey="p" rel="prev">Orphan Sections</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">Expressions</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>
  200. </div>
  201. </body>
  202. </html>