Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

358 linhas
15KB

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