Expression-Section.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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: Expression Section</title>
  16. <meta name="description" content="LD: Expression Section">
  17. <meta name="keywords" content="LD: Expression Section">
  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="Builtin-Functions.html#Builtin-Functions" rel="next" title="Builtin Functions">
  27. <link href="Evaluation.html#Evaluation" rel="prev" title="Evaluation">
  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="Expression-Section"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Builtin-Functions.html#Builtin-Functions" accesskey="n" rel="next">Builtin Functions</a>, Previous: <a href="Evaluation.html#Evaluation" accesskey="p" rel="prev">Evaluation</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-Section-of-an-Expression"></a>
  65. <h4 class="subsection">3.10.8 The Section of an Expression</h4>
  66. <a name="index-expression-sections"></a>
  67. <a name="index-absolute-expressions"></a>
  68. <a name="index-relative-expressions"></a>
  69. <a name="index-absolute-and-relocatable-symbols"></a>
  70. <a name="index-relocatable-and-absolute-symbols"></a>
  71. <a name="index-symbols_002c-relocatable-and-absolute"></a>
  72. <p>Addresses and symbols may be section relative, or absolute. A section
  73. relative symbol is relocatable. If you request relocatable output
  74. using the &lsquo;<samp>-r</samp>&rsquo; option, a further link operation may change the
  75. value of a section relative symbol. On the other hand, an absolute
  76. symbol will retain the same value throughout any further link
  77. operations.
  78. </p>
  79. <p>Some terms in linker expressions are addresses. This is true of
  80. section relative symbols and for builtin functions that return an
  81. address, such as <code>ADDR</code>, <code>LOADADDR</code>, <code>ORIGIN</code> and
  82. <code>SEGMENT_START</code>. Other terms are simply numbers, or are builtin
  83. functions that return a non-address value, such as <code>LENGTH</code>.
  84. One complication is that unless you set <code>LD_FEATURE (&quot;SANE_EXPR&quot;)</code>
  85. (see <a href="Miscellaneous-Commands.html#Miscellaneous-Commands">Miscellaneous Commands</a>), numbers and absolute symbols are treated
  86. differently depending on their location, for compatibility with older
  87. versions of <code>ld</code>. Expressions appearing outside an output
  88. section definition treat all numbers as absolute addresses.
  89. Expressions appearing inside an output section definition treat
  90. absolute symbols as numbers. If <code>LD_FEATURE (&quot;SANE_EXPR&quot;)</code> is
  91. given, then absolute symbols and numbers are simply treated as numbers
  92. everywhere.
  93. </p>
  94. <p>In the following simple example,
  95. </p>
  96. <div class="smallexample">
  97. <pre class="smallexample">SECTIONS
  98. {
  99. . = 0x100;
  100. __executable_start = 0x100;
  101. .data :
  102. {
  103. . = 0x10;
  104. __data_start = 0x10;
  105. *(.data)
  106. }
  107. &hellip;
  108. }
  109. </pre></div>
  110. <p>both <code>.</code> and <code>__executable_start</code> are set to the absolute
  111. address 0x100 in the first two assignments, then both <code>.</code> and
  112. <code>__data_start</code> are set to 0x10 relative to the <code>.data</code>
  113. section in the second two assignments.
  114. </p>
  115. <p>For expressions involving numbers, relative addresses and absolute
  116. addresses, ld follows these rules to evaluate terms:
  117. </p>
  118. <ul>
  119. <li> Unary operations on an absolute address or number, and binary
  120. operations on two absolute addresses or two numbers, or between one
  121. absolute address and a number, apply the operator to the value(s).
  122. </li><li> Unary operations on a relative address, and binary operations on two
  123. relative addresses in the same section or between one relative address
  124. and a number, apply the operator to the offset part of the address(es).
  125. </li><li> Other binary operations, that is, between two relative addresses not
  126. in the same section, or between a relative address and an absolute
  127. address, first convert any non-absolute term to an absolute address
  128. before applying the operator.
  129. </li></ul>
  130. <p>The result section of each sub-expression is as follows:
  131. </p>
  132. <ul>
  133. <li> An operation involving only numbers results in a number.
  134. </li><li> The result of comparisons, &lsquo;<samp>&amp;&amp;</samp>&rsquo; and &lsquo;<samp>||</samp>&rsquo; is also a number.
  135. </li><li> The result of other binary arithmetic and logical operations on two
  136. relative addresses in the same section or two absolute addresses
  137. (after above conversions) is also a number when
  138. <code>LD_FEATURE (&quot;SANE_EXPR&quot;)</code> or inside an output section definition
  139. but an absolute address otherwise.
  140. </li><li> The result of other operations on relative addresses or one
  141. relative address and a number, is a relative address in the same
  142. section as the relative operand(s).
  143. </li><li> The result of other operations on absolute addresses (after above
  144. conversions) is an absolute address.
  145. </li></ul>
  146. <p>You can use the builtin function <code>ABSOLUTE</code> to force an expression
  147. to be absolute when it would otherwise be relative. For example, to
  148. create an absolute symbol set to the address of the end of the output
  149. section &lsquo;<samp>.data</samp>&rsquo;:
  150. </p><div class="smallexample">
  151. <pre class="smallexample">SECTIONS
  152. {
  153. .data : { *(.data) _edata = ABSOLUTE(.); }
  154. }
  155. </pre></div>
  156. <p>If &lsquo;<samp>ABSOLUTE</samp>&rsquo; were not used, &lsquo;<samp>_edata</samp>&rsquo; would be relative to the
  157. &lsquo;<samp>.data</samp>&rsquo; section.
  158. </p>
  159. <p>Using <code>LOADADDR</code> also forces an expression absolute, since this
  160. particular builtin function returns an absolute address.
  161. </p>
  162. <hr>
  163. <div class="header">
  164. <p>
  165. Next: <a href="Builtin-Functions.html#Builtin-Functions" accesskey="n" rel="next">Builtin Functions</a>, Previous: <a href="Evaluation.html#Evaluation" accesskey="p" rel="prev">Evaluation</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>
  166. </div>
  167. </body>
  168. </html>