Builtin-Functions.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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: Builtin Functions</title>
  16. <meta name="description" content="LD: Builtin Functions">
  17. <meta name="keywords" content="LD: Builtin Functions">
  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="Implicit-Linker-Scripts.html#Implicit-Linker-Scripts" rel="next" title="Implicit Linker Scripts">
  27. <link href="Expression-Section.html#Expression-Section" rel="prev" title="Expression Section">
  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="Builtin-Functions"></a>
  59. <div class="header">
  60. <p>
  61. Previous: <a href="Expression-Section.html#Expression-Section" accesskey="p" rel="prev">Expression Section</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="Builtin-Functions-1"></a>
  65. <h4 class="subsection">3.10.9 Builtin Functions</h4>
  66. <a name="index-functions-in-expressions"></a>
  67. <p>The linker script language includes a number of builtin functions for
  68. use in linker script expressions.
  69. </p>
  70. <dl compact="compact">
  71. <dt><code>ABSOLUTE(<var>exp</var>)</code></dt>
  72. <dd><a name="index-ABSOLUTE_0028exp_0029"></a>
  73. <a name="index-expression_002c-absolute"></a>
  74. <p>Return the absolute (non-relocatable, as opposed to non-negative) value
  75. of the expression <var>exp</var>. Primarily useful to assign an absolute
  76. value to a symbol within a section definition, where symbol values are
  77. normally section relative. See <a href="Expression-Section.html#Expression-Section">Expression Section</a>.
  78. </p>
  79. </dd>
  80. <dt><code>ADDR(<var>section</var>)</code></dt>
  81. <dd><a name="index-ADDR_0028section_0029"></a>
  82. <a name="index-section-address-in-expression"></a>
  83. <p>Return the address (VMA) of the named <var>section</var>. Your
  84. script must previously have defined the location of that section. In
  85. the following example, <code>start_of_output_1</code>, <code>symbol_1</code> and
  86. <code>symbol_2</code> are assigned equivalent values, except that
  87. <code>symbol_1</code> will be relative to the <code>.output1</code> section while
  88. the other two will be absolute:
  89. </p><div class="smallexample">
  90. <pre class="smallexample">SECTIONS { &hellip;
  91. .output1 :
  92. {
  93. start_of_output_1 = ABSOLUTE(.);
  94. &hellip;
  95. }
  96. .output :
  97. {
  98. symbol_1 = ADDR(.output1);
  99. symbol_2 = start_of_output_1;
  100. }
  101. &hellip; }
  102. </pre></div>
  103. </dd>
  104. <dt><code>ALIGN(<var>align</var>)</code></dt>
  105. <dt><code>ALIGN(<var>exp</var>,<var>align</var>)</code></dt>
  106. <dd><a name="index-ALIGN_0028align_0029"></a>
  107. <a name="index-ALIGN_0028exp_002calign_0029"></a>
  108. <a name="index-round-up-location-counter"></a>
  109. <a name="index-align-location-counter"></a>
  110. <a name="index-round-up-expression"></a>
  111. <a name="index-align-expression"></a>
  112. <p>Return the location counter (<code>.</code>) or arbitrary expression aligned
  113. to the next <var>align</var> boundary. The single operand <code>ALIGN</code>
  114. doesn&rsquo;t change the value of the location counter&mdash;it just does
  115. arithmetic on it. The two operand <code>ALIGN</code> allows an arbitrary
  116. expression to be aligned upwards (<code>ALIGN(<var>align</var>)</code> is
  117. equivalent to <code>ALIGN(ABSOLUTE(.), <var>align</var>)</code>).
  118. </p>
  119. <p>Here is an example which aligns the output <code>.data</code> section to the
  120. next <code>0x2000</code> byte boundary after the preceding section and sets a
  121. variable within the section to the next <code>0x8000</code> boundary after the
  122. input sections:
  123. </p><div class="smallexample">
  124. <pre class="smallexample">SECTIONS { &hellip;
  125. .data ALIGN(0x2000): {
  126. *(.data)
  127. variable = ALIGN(0x8000);
  128. }
  129. &hellip; }
  130. </pre></div>
  131. <p>The first use of <code>ALIGN</code> in this example specifies the location of
  132. a section because it is used as the optional <var>address</var> attribute of
  133. a section definition (see <a href="Output-Section-Address.html#Output-Section-Address">Output Section Address</a>). The second use
  134. of <code>ALIGN</code> is used to defines the value of a symbol.
  135. </p>
  136. <p>The builtin function <code>NEXT</code> is closely related to <code>ALIGN</code>.
  137. </p>
  138. </dd>
  139. <dt><code>ALIGNOF(<var>section</var>)</code></dt>
  140. <dd><a name="index-ALIGNOF_0028section_0029"></a>
  141. <a name="index-section-alignment"></a>
  142. <p>Return the alignment in bytes of the named <var>section</var>, if that section has
  143. been allocated. If the section has not been allocated when this is
  144. evaluated, the linker will report an error. In the following example,
  145. the alignment of the <code>.output</code> section is stored as the first
  146. value in that section.
  147. </p><div class="smallexample">
  148. <pre class="smallexample">SECTIONS{ &hellip;
  149. .output {
  150. LONG (ALIGNOF (.output))
  151. &hellip;
  152. }
  153. &hellip; }
  154. </pre></div>
  155. </dd>
  156. <dt><code>BLOCK(<var>exp</var>)</code></dt>
  157. <dd><a name="index-BLOCK_0028exp_0029"></a>
  158. <p>This is a synonym for <code>ALIGN</code>, for compatibility with older linker
  159. scripts. It is most often seen when setting the address of an output
  160. section.
  161. </p>
  162. </dd>
  163. <dt><code>DATA_SEGMENT_ALIGN(<var>maxpagesize</var>, <var>commonpagesize</var>)</code></dt>
  164. <dd><a name="index-DATA_005fSEGMENT_005fALIGN_0028maxpagesize_002c-commonpagesize_0029"></a>
  165. <p>This is equivalent to either
  166. </p><div class="smallexample">
  167. <pre class="smallexample">(ALIGN(<var>maxpagesize</var>) + (. &amp; (<var>maxpagesize</var> - 1)))
  168. </pre></div>
  169. <p>or
  170. </p><div class="smallexample">
  171. <pre class="smallexample">(ALIGN(<var>maxpagesize</var>)
  172. + ((. + <var>commonpagesize</var> - 1) &amp; (<var>maxpagesize</var> - <var>commonpagesize</var>)))
  173. </pre></div>
  174. <p>depending on whether the latter uses fewer <var>commonpagesize</var> sized pages
  175. for the data segment (area between the result of this expression and
  176. <code>DATA_SEGMENT_END</code>) than the former or not.
  177. If the latter form is used, it means <var>commonpagesize</var> bytes of runtime
  178. memory will be saved at the expense of up to <var>commonpagesize</var> wasted
  179. bytes in the on-disk file.
  180. </p>
  181. <p>This expression can only be used directly in <code>SECTIONS</code> commands, not in
  182. any output section descriptions and only once in the linker script.
  183. <var>commonpagesize</var> should be less or equal to <var>maxpagesize</var> and should
  184. be the system page size the object wants to be optimized for (while still
  185. working on system page sizes up to <var>maxpagesize</var>).
  186. </p>
  187. <p>Example:
  188. </p><div class="smallexample">
  189. <pre class="smallexample"> . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
  190. </pre></div>
  191. </dd>
  192. <dt><code>DATA_SEGMENT_END(<var>exp</var>)</code></dt>
  193. <dd><a name="index-DATA_005fSEGMENT_005fEND_0028exp_0029"></a>
  194. <p>This defines the end of data segment for <code>DATA_SEGMENT_ALIGN</code>
  195. evaluation purposes.
  196. </p>
  197. <div class="smallexample">
  198. <pre class="smallexample"> . = DATA_SEGMENT_END(.);
  199. </pre></div>
  200. </dd>
  201. <dt><code>DATA_SEGMENT_RELRO_END(<var>offset</var>, <var>exp</var>)</code></dt>
  202. <dd><a name="index-DATA_005fSEGMENT_005fRELRO_005fEND_0028offset_002c-exp_0029"></a>
  203. <p>This defines the end of the <code>PT_GNU_RELRO</code> segment when
  204. &lsquo;<samp>-z relro</samp>&rsquo; option is used.
  205. When &lsquo;<samp>-z relro</samp>&rsquo; option is not present, <code>DATA_SEGMENT_RELRO_END</code>
  206. does nothing, otherwise <code>DATA_SEGMENT_ALIGN</code> is padded so that
  207. <var>exp</var> + <var>offset</var> is aligned to the most commonly used page
  208. boundary for particular target. If present in the linker script,
  209. it must always come in between <code>DATA_SEGMENT_ALIGN</code> and
  210. <code>DATA_SEGMENT_END</code>. Evaluates to the second argument plus any
  211. padding needed at the end of the <code>PT_GNU_RELRO</code> segment due to
  212. section alignment.
  213. </p>
  214. <div class="smallexample">
  215. <pre class="smallexample"> . = DATA_SEGMENT_RELRO_END(24, .);
  216. </pre></div>
  217. </dd>
  218. <dt><code>DEFINED(<var>symbol</var>)</code></dt>
  219. <dd><a name="index-DEFINED_0028symbol_0029"></a>
  220. <a name="index-symbol-defaults"></a>
  221. <p>Return 1 if <var>symbol</var> is in the linker global symbol table and is
  222. defined before the statement using DEFINED in the script, otherwise
  223. return 0. You can use this function to provide
  224. default values for symbols. For example, the following script fragment
  225. shows how to set a global symbol &lsquo;<samp>begin</samp>&rsquo; to the first location in
  226. the &lsquo;<samp>.text</samp>&rsquo; section&mdash;but if a symbol called &lsquo;<samp>begin</samp>&rsquo; already
  227. existed, its value is preserved:
  228. </p>
  229. <div class="smallexample">
  230. <pre class="smallexample">SECTIONS { &hellip;
  231. .text : {
  232. begin = DEFINED(begin) ? begin : . ;
  233. &hellip;
  234. }
  235. &hellip;
  236. }
  237. </pre></div>
  238. </dd>
  239. <dt><code>LENGTH(<var>memory</var>)</code></dt>
  240. <dd><a name="index-LENGTH_0028memory_0029"></a>
  241. <p>Return the length of the memory region named <var>memory</var>.
  242. </p>
  243. </dd>
  244. <dt><code>LOADADDR(<var>section</var>)</code></dt>
  245. <dd><a name="index-LOADADDR_0028section_0029"></a>
  246. <a name="index-section-load-address-in-expression"></a>
  247. <p>Return the absolute LMA of the named <var>section</var>. (see <a href="Output-Section-LMA.html#Output-Section-LMA">Output Section LMA</a>).
  248. </p>
  249. </dd>
  250. <dt><code>LOG2CEIL(<var>exp</var>)</code></dt>
  251. <dd><a name="index-LOG2CEIL_0028exp_0029"></a>
  252. <p>Return the binary logarithm of <var>exp</var> rounded towards infinity.
  253. <code>LOG2CEIL(0)</code> returns 0.
  254. </p>
  255. <a name="index-MAX"></a>
  256. </dd>
  257. <dt><code>MAX(<var>exp1</var>, <var>exp2</var>)</code></dt>
  258. <dd><p>Returns the maximum of <var>exp1</var> and <var>exp2</var>.
  259. </p>
  260. <a name="index-MIN"></a>
  261. </dd>
  262. <dt><code>MIN(<var>exp1</var>, <var>exp2</var>)</code></dt>
  263. <dd><p>Returns the minimum of <var>exp1</var> and <var>exp2</var>.
  264. </p>
  265. </dd>
  266. <dt><code>NEXT(<var>exp</var>)</code></dt>
  267. <dd><a name="index-NEXT_0028exp_0029"></a>
  268. <a name="index-unallocated-address_002c-next"></a>
  269. <p>Return the next unallocated address that is a multiple of <var>exp</var>.
  270. This function is closely related to <code>ALIGN(<var>exp</var>)</code>; unless you
  271. use the <code>MEMORY</code> command to define discontinuous memory for the
  272. output file, the two functions are equivalent.
  273. </p>
  274. </dd>
  275. <dt><code>ORIGIN(<var>memory</var>)</code></dt>
  276. <dd><a name="index-ORIGIN_0028memory_0029"></a>
  277. <p>Return the origin of the memory region named <var>memory</var>.
  278. </p>
  279. </dd>
  280. <dt><code>SEGMENT_START(<var>segment</var>, <var>default</var>)</code></dt>
  281. <dd><a name="index-SEGMENT_005fSTART_0028segment_002c-default_0029"></a>
  282. <p>Return the base address of the named <var>segment</var>. If an explicit
  283. value has already been given for this segment (with a command-line
  284. &lsquo;<samp>-T</samp>&rsquo; option) then that value will be returned otherwise the value
  285. will be <var>default</var>. At present, the &lsquo;<samp>-T</samp>&rsquo; command-line option
  286. can only be used to set the base address for the &ldquo;text&rdquo;, &ldquo;data&rdquo;, and
  287. &ldquo;bss&rdquo; sections, but you can use <code>SEGMENT_START</code> with any segment
  288. name.
  289. </p>
  290. </dd>
  291. <dt><code>SIZEOF(<var>section</var>)</code></dt>
  292. <dd><a name="index-SIZEOF_0028section_0029"></a>
  293. <a name="index-section-size"></a>
  294. <p>Return the size in bytes of the named <var>section</var>, if that section has
  295. been allocated. If the section has not been allocated when this is
  296. evaluated, the linker will report an error. In the following example,
  297. <code>symbol_1</code> and <code>symbol_2</code> are assigned identical values:
  298. </p><div class="smallexample">
  299. <pre class="smallexample">SECTIONS{ &hellip;
  300. .output {
  301. .start = . ;
  302. &hellip;
  303. .end = . ;
  304. }
  305. symbol_1 = .end - .start ;
  306. symbol_2 = SIZEOF(.output);
  307. &hellip; }
  308. </pre></div>
  309. </dd>
  310. <dt><code>SIZEOF_HEADERS</code></dt>
  311. <dt><code>sizeof_headers</code></dt>
  312. <dd><a name="index-SIZEOF_005fHEADERS"></a>
  313. <a name="index-header-size"></a>
  314. <p>Return the size in bytes of the output file&rsquo;s headers. This is
  315. information which appears at the start of the output file. You can use
  316. this number when setting the start address of the first section, if you
  317. choose, to facilitate paging.
  318. </p>
  319. <a name="index-not-enough-room-for-program-headers"></a>
  320. <a name="index-program-headers_002c-not-enough-room"></a>
  321. <p>When producing an ELF output file, if the linker script uses the
  322. <code>SIZEOF_HEADERS</code> builtin function, the linker must compute the
  323. number of program headers before it has determined all the section
  324. addresses and sizes. If the linker later discovers that it needs
  325. additional program headers, it will report an error &lsquo;<samp>not enough
  326. room for program headers</samp>&rsquo;. To avoid this error, you must avoid using
  327. the <code>SIZEOF_HEADERS</code> function, or you must rework your linker
  328. script to avoid forcing the linker to use additional program headers, or
  329. you must define the program headers yourself using the <code>PHDRS</code>
  330. command (see <a href="PHDRS.html#PHDRS">PHDRS</a>).
  331. </p></dd>
  332. </dl>
  333. <hr>
  334. <div class="header">
  335. <p>
  336. Previous: <a href="Expression-Section.html#Expression-Section" accesskey="p" rel="prev">Expression Section</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>
  337. </div>
  338. </body>
  339. </html>