Builtin-Functions.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 Toolchain for the A-profile Architecture 10.2-2020.11 (arm-10.16))
  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 5.1, 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="previous" 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="previous">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. running on system page sizes up to <var>maxpagesize</var>. Note however
  186. that &lsquo;<samp>-z relro</samp>&rsquo; protection will not be effective if the system
  187. page size is larger than <var>commonpagesize</var>.
  188. </p>
  189. <p>Example:
  190. </p><div class="smallexample">
  191. <pre class="smallexample"> . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
  192. </pre></div>
  193. </dd>
  194. <dt><code>DATA_SEGMENT_END(<var>exp</var>)</code></dt>
  195. <dd><a name="index-DATA_005fSEGMENT_005fEND_0028exp_0029"></a>
  196. <p>This defines the end of data segment for <code>DATA_SEGMENT_ALIGN</code>
  197. evaluation purposes.
  198. </p>
  199. <div class="smallexample">
  200. <pre class="smallexample"> . = DATA_SEGMENT_END(.);
  201. </pre></div>
  202. </dd>
  203. <dt><code>DATA_SEGMENT_RELRO_END(<var>offset</var>, <var>exp</var>)</code></dt>
  204. <dd><a name="index-DATA_005fSEGMENT_005fRELRO_005fEND_0028offset_002c-exp_0029"></a>
  205. <p>This defines the end of the <code>PT_GNU_RELRO</code> segment when
  206. &lsquo;<samp>-z relro</samp>&rsquo; option is used.
  207. When &lsquo;<samp>-z relro</samp>&rsquo; option is not present, <code>DATA_SEGMENT_RELRO_END</code>
  208. does nothing, otherwise <code>DATA_SEGMENT_ALIGN</code> is padded so that
  209. <var>exp</var> + <var>offset</var> is aligned to the <var>commonpagesize</var>
  210. argument given to <code>DATA_SEGMENT_ALIGN</code>. If present in the linker
  211. script, it must be placed between <code>DATA_SEGMENT_ALIGN</code> and
  212. <code>DATA_SEGMENT_END</code>. Evaluates to the second argument plus any
  213. padding needed at the end of the <code>PT_GNU_RELRO</code> segment due to
  214. section alignment.
  215. </p>
  216. <div class="smallexample">
  217. <pre class="smallexample"> . = DATA_SEGMENT_RELRO_END(24, .);
  218. </pre></div>
  219. </dd>
  220. <dt><code>DEFINED(<var>symbol</var>)</code></dt>
  221. <dd><a name="index-DEFINED_0028symbol_0029"></a>
  222. <a name="index-symbol-defaults"></a>
  223. <p>Return 1 if <var>symbol</var> is in the linker global symbol table and is
  224. defined before the statement using DEFINED in the script, otherwise
  225. return 0. You can use this function to provide
  226. default values for symbols. For example, the following script fragment
  227. shows how to set a global symbol &lsquo;<samp>begin</samp>&rsquo; to the first location in
  228. the &lsquo;<samp>.text</samp>&rsquo; section&mdash;but if a symbol called &lsquo;<samp>begin</samp>&rsquo; already
  229. existed, its value is preserved:
  230. </p>
  231. <div class="smallexample">
  232. <pre class="smallexample">SECTIONS { &hellip;
  233. .text : {
  234. begin = DEFINED(begin) ? begin : . ;
  235. &hellip;
  236. }
  237. &hellip;
  238. }
  239. </pre></div>
  240. </dd>
  241. <dt><code>LENGTH(<var>memory</var>)</code></dt>
  242. <dd><a name="index-LENGTH_0028memory_0029"></a>
  243. <p>Return the length of the memory region named <var>memory</var>.
  244. </p>
  245. </dd>
  246. <dt><code>LOADADDR(<var>section</var>)</code></dt>
  247. <dd><a name="index-LOADADDR_0028section_0029"></a>
  248. <a name="index-section-load-address-in-expression"></a>
  249. <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>).
  250. </p>
  251. </dd>
  252. <dt><code>LOG2CEIL(<var>exp</var>)</code></dt>
  253. <dd><a name="index-LOG2CEIL_0028exp_0029"></a>
  254. <p>Return the binary logarithm of <var>exp</var> rounded towards infinity.
  255. <code>LOG2CEIL(0)</code> returns 0.
  256. </p>
  257. <a name="index-MAX"></a>
  258. </dd>
  259. <dt><code>MAX(<var>exp1</var>, <var>exp2</var>)</code></dt>
  260. <dd><p>Returns the maximum of <var>exp1</var> and <var>exp2</var>.
  261. </p>
  262. <a name="index-MIN"></a>
  263. </dd>
  264. <dt><code>MIN(<var>exp1</var>, <var>exp2</var>)</code></dt>
  265. <dd><p>Returns the minimum of <var>exp1</var> and <var>exp2</var>.
  266. </p>
  267. </dd>
  268. <dt><code>NEXT(<var>exp</var>)</code></dt>
  269. <dd><a name="index-NEXT_0028exp_0029"></a>
  270. <a name="index-unallocated-address_002c-next"></a>
  271. <p>Return the next unallocated address that is a multiple of <var>exp</var>.
  272. This function is closely related to <code>ALIGN(<var>exp</var>)</code>; unless you
  273. use the <code>MEMORY</code> command to define discontinuous memory for the
  274. output file, the two functions are equivalent.
  275. </p>
  276. </dd>
  277. <dt><code>ORIGIN(<var>memory</var>)</code></dt>
  278. <dd><a name="index-ORIGIN_0028memory_0029"></a>
  279. <p>Return the origin of the memory region named <var>memory</var>.
  280. </p>
  281. </dd>
  282. <dt><code>SEGMENT_START(<var>segment</var>, <var>default</var>)</code></dt>
  283. <dd><a name="index-SEGMENT_005fSTART_0028segment_002c-default_0029"></a>
  284. <p>Return the base address of the named <var>segment</var>. If an explicit
  285. value has already been given for this segment (with a command-line
  286. &lsquo;<samp>-T</samp>&rsquo; option) then that value will be returned otherwise the value
  287. will be <var>default</var>. At present, the &lsquo;<samp>-T</samp>&rsquo; command-line option
  288. can only be used to set the base address for the &ldquo;text&rdquo;, &ldquo;data&rdquo;, and
  289. &ldquo;bss&rdquo; sections, but you can use <code>SEGMENT_START</code> with any segment
  290. name.
  291. </p>
  292. </dd>
  293. <dt><code>SIZEOF(<var>section</var>)</code></dt>
  294. <dd><a name="index-SIZEOF_0028section_0029"></a>
  295. <a name="index-section-size"></a>
  296. <p>Return the size in bytes of the named <var>section</var>, if that section has
  297. been allocated. If the section has not been allocated when this is
  298. evaluated, the linker will report an error. In the following example,
  299. <code>symbol_1</code> and <code>symbol_2</code> are assigned identical values:
  300. </p><div class="smallexample">
  301. <pre class="smallexample">SECTIONS{ &hellip;
  302. .output {
  303. .start = . ;
  304. &hellip;
  305. .end = . ;
  306. }
  307. symbol_1 = .end - .start ;
  308. symbol_2 = SIZEOF(.output);
  309. &hellip; }
  310. </pre></div>
  311. </dd>
  312. <dt><code>SIZEOF_HEADERS</code></dt>
  313. <dt><code>sizeof_headers</code></dt>
  314. <dd><a name="index-SIZEOF_005fHEADERS"></a>
  315. <a name="index-header-size"></a>
  316. <p>Return the size in bytes of the output file&rsquo;s headers. This is
  317. information which appears at the start of the output file. You can use
  318. this number when setting the start address of the first section, if you
  319. choose, to facilitate paging.
  320. </p>
  321. <a name="index-not-enough-room-for-program-headers"></a>
  322. <a name="index-program-headers_002c-not-enough-room"></a>
  323. <p>When producing an ELF output file, if the linker script uses the
  324. <code>SIZEOF_HEADERS</code> builtin function, the linker must compute the
  325. number of program headers before it has determined all the section
  326. addresses and sizes. If the linker later discovers that it needs
  327. additional program headers, it will report an error &lsquo;<samp>not enough
  328. room for program headers</samp>&rsquo;. To avoid this error, you must avoid using
  329. the <code>SIZEOF_HEADERS</code> function, or you must rework your linker
  330. script to avoid forcing the linker to use additional program headers, or
  331. you must define the program headers yourself using the <code>PHDRS</code>
  332. command (see <a href="PHDRS.html#PHDRS">PHDRS</a>).
  333. </p></dd>
  334. </dl>
  335. <hr>
  336. <div class="header">
  337. <p>
  338. Previous: <a href="Expression-Section.html#Expression-Section" accesskey="p" rel="previous">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>
  339. </div>
  340. </body>
  341. </html>