Location-Counter.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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: Location Counter</title>
  16. <meta name="description" content="LD: Location Counter">
  17. <meta name="keywords" content="LD: Location Counter">
  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="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.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="Location-Counter"></a>
  59. <div class="header">
  60. <p>
  61. 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>
  62. </div>
  63. <hr>
  64. <a name="The-Location-Counter"></a>
  65. <h4 class="subsection">3.10.5 The Location Counter</h4>
  66. <a name="index-_002e"></a>
  67. <a name="index-dot"></a>
  68. <a name="index-location-counter"></a>
  69. <a name="index-current-output-location"></a>
  70. <p>The special linker variable <em>dot</em> &lsquo;<samp>.</samp>&rsquo; always contains the
  71. current output location counter. Since the <code>.</code> always refers to a
  72. location in an output section, it may only appear in an expression
  73. within a <code>SECTIONS</code> command. The <code>.</code> symbol may appear
  74. anywhere that an ordinary symbol is allowed in an expression.
  75. </p>
  76. <a name="index-holes"></a>
  77. <p>Assigning a value to <code>.</code> will cause the location counter to be
  78. moved. This may be used to create holes in the output section. The
  79. location counter may not be moved backwards inside an output section,
  80. and may not be moved backwards outside of an output section if so
  81. doing creates areas with overlapping LMAs.
  82. </p>
  83. <div class="smallexample">
  84. <pre class="smallexample">SECTIONS
  85. {
  86. output :
  87. {
  88. file1(.text)
  89. . = . + 1000;
  90. file2(.text)
  91. . += 1000;
  92. file3(.text)
  93. } = 0x12345678;
  94. }
  95. </pre></div>
  96. <p>In the previous example, the &lsquo;<samp>.text</samp>&rsquo; section from <samp>file1</samp> is
  97. located at the beginning of the output section &lsquo;<samp>output</samp>&rsquo;. It is
  98. followed by a 1000 byte gap. Then the &lsquo;<samp>.text</samp>&rsquo; section from
  99. <samp>file2</samp> appears, also with a 1000 byte gap following before the
  100. &lsquo;<samp>.text</samp>&rsquo; section from <samp>file3</samp>. The notation &lsquo;<samp>= 0x12345678</samp>&rsquo;
  101. specifies what data to write in the gaps (see <a href="Output-Section-Fill.html#Output-Section-Fill">Output Section Fill</a>).
  102. </p>
  103. <a name="index-dot-inside-sections"></a>
  104. <p>Note: <code>.</code> actually refers to the byte offset from the start of the
  105. current containing object. Normally this is the <code>SECTIONS</code>
  106. statement, whose start address is 0, hence <code>.</code> can be used as an
  107. absolute address. If <code>.</code> is used inside a section description
  108. however, it refers to the byte offset from the start of that section,
  109. not an absolute address. Thus in a script like this:
  110. </p>
  111. <div class="smallexample">
  112. <pre class="smallexample">SECTIONS
  113. {
  114. . = 0x100
  115. .text: {
  116. *(.text)
  117. . = 0x200
  118. }
  119. . = 0x500
  120. .data: {
  121. *(.data)
  122. . += 0x600
  123. }
  124. }
  125. </pre></div>
  126. <p>The &lsquo;<samp>.text</samp>&rsquo; section will be assigned a starting address of 0x100
  127. and a size of exactly 0x200 bytes, even if there is not enough data in
  128. the &lsquo;<samp>.text</samp>&rsquo; input sections to fill this area. (If there is too
  129. much data, an error will be produced because this would be an attempt to
  130. move <code>.</code> backwards). The &lsquo;<samp>.data</samp>&rsquo; section will start at 0x500
  131. and it will have an extra 0x600 bytes worth of space after the end of
  132. the values from the &lsquo;<samp>.data</samp>&rsquo; input sections and before the end of
  133. the &lsquo;<samp>.data</samp>&rsquo; output section itself.
  134. </p>
  135. <a name="index-dot-outside-sections"></a>
  136. <p>Setting symbols to the value of the location counter outside of an
  137. output section statement can result in unexpected values if the linker
  138. needs to place orphan sections. For example, given the following:
  139. </p>
  140. <div class="smallexample">
  141. <pre class="smallexample">SECTIONS
  142. {
  143. start_of_text = . ;
  144. .text: { *(.text) }
  145. end_of_text = . ;
  146. start_of_data = . ;
  147. .data: { *(.data) }
  148. end_of_data = . ;
  149. }
  150. </pre></div>
  151. <p>If the linker needs to place some input section, e.g. <code>.rodata</code>,
  152. not mentioned in the script, it might choose to place that section
  153. between <code>.text</code> and <code>.data</code>. You might think the linker
  154. should place <code>.rodata</code> on the blank line in the above script, but
  155. blank lines are of no particular significance to the linker. As well,
  156. the linker doesn&rsquo;t associate the above symbol names with their
  157. sections. Instead, it assumes that all assignments or other
  158. statements belong to the previous output section, except for the
  159. special case of an assignment to <code>.</code>. I.e., the linker will
  160. place the orphan <code>.rodata</code> section as if the script was written
  161. as follows:
  162. </p>
  163. <div class="smallexample">
  164. <pre class="smallexample">SECTIONS
  165. {
  166. start_of_text = . ;
  167. .text: { *(.text) }
  168. end_of_text = . ;
  169. start_of_data = . ;
  170. .rodata: { *(.rodata) }
  171. .data: { *(.data) }
  172. end_of_data = . ;
  173. }
  174. </pre></div>
  175. <p>This may or may not be the script author&rsquo;s intention for the value of
  176. <code>start_of_data</code>. One way to influence the orphan section
  177. placement is to assign the location counter to itself, as the linker
  178. assumes that an assignment to <code>.</code> is setting the start address of
  179. a following output section and thus should be grouped with that
  180. section. So you could write:
  181. </p>
  182. <div class="smallexample">
  183. <pre class="smallexample">SECTIONS
  184. {
  185. start_of_text = . ;
  186. .text: { *(.text) }
  187. end_of_text = . ;
  188. . = . ;
  189. start_of_data = . ;
  190. .data: { *(.data) }
  191. end_of_data = . ;
  192. }
  193. </pre></div>
  194. <p>Now, the orphan <code>.rodata</code> section will be placed between
  195. <code>end_of_text</code> and <code>start_of_data</code>.
  196. </p>
  197. <hr>
  198. <div class="header">
  199. <p>
  200. 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>
  201. </div>
  202. </body>
  203. </html>