📄 builtin-functions.html
字号:
<html lang="en">
<head>
<title>Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.3">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home">
</head>
<body>
<div class="node">
<p>
Node:<a name="Builtin%20Functions">Builtin Functions</a>,
Previous:<a rel="previous" accesskey="p" href="Expression-Section.html#Expression%20Section">Expression Section</a>,
Up:<a rel="up" accesskey="u" href="Expressions.html#Expressions">Expressions</a>
<hr><br>
</div>
<h4 class="subsection">Builtin Functions</h4>
<p>The linker script language includes a number of builtin functions for
use in linker script expressions.
<dl>
<dt><code>ABSOLUTE(</code><var>exp</var><code>)</code>
<dd>Return the absolute (non-relocatable, as opposed to non-negative) value
of the expression <var>exp</var>. Primarily useful to assign an absolute
value to a symbol within a section definition, where symbol values are
normally section relative. See <a href="Expression-Section.html#Expression%20Section">Expression Section</a>.
<br><dt><code>ADDR(</code><var>section</var><code>)</code>
<dd>Return the absolute address (the VMA) of the named <var>section</var>. Your
script must previously have defined the location of that section. In
the following example, <code>symbol_1</code> and <code>symbol_2</code> are assigned
identical values:
<pre class="smallexample"> SECTIONS { ...
.output1 :
{
start_of_output_1 = ABSOLUTE(.);
...
}
.output :
{
symbol_1 = ADDR(.output1);
symbol_2 = start_of_output_1;
}
... }
</pre>
<br><dt><code>ALIGN(</code><var>exp</var><code>)</code>
<dd>Return the location counter (<code>.</code>) aligned to the next <var>exp</var>
boundary.
<code>ALIGN</code> doesn't change the value of the location counter--it just
does arithmetic on it. Here is an example which aligns the output
<code>.data</code> section to the next <code>0x2000</code> byte boundary after the
preceding section and sets a variable within the section to the next
<code>0x8000</code> boundary after the input sections:
<pre class="smallexample"> SECTIONS { ...
.data ALIGN(0x2000): {
*(.data)
variable = ALIGN(0x8000);
}
... }
</pre>
<p>The first use of <code>ALIGN</code> in this example specifies the location of
a section because it is used as the optional <var>address</var> attribute of
a section definition (see <a href="Output-Section-Address.html#Output%20Section%20Address">Output Section Address</a>). The second use
of <code>ALIGN</code> is used to defines the value of a symbol.
<p>The builtin function <code>NEXT</code> is closely related to <code>ALIGN</code>.
<br><dt><code>BLOCK(</code><var>exp</var><code>)</code>
<dd>This is a synonym for <code>ALIGN</code>, for compatibility with older linker
scripts. It is most often seen when setting the address of an output
section.
<br><dt><code>DATA_SEGMENT_ALIGN(</code><var>maxpagesize</var><code>, </code><var>commonpagesize</var><code>)</code>
<dd>This is equivalent to either
<pre class="smallexample"> (ALIGN(<var>maxpagesize</var>) + (. & (<var>maxpagesize</var> - 1)))
</pre>
or
<pre class="smallexample"> (ALIGN(<var>maxpagesize</var>) + (. & (<var>maxpagesize</var> - <var>commonpagesize</var>)))
</pre>
<p>depending on whether the latter uses fewer <var>commonpagesize</var> sized pages
for the data segment (area between the result of this expression and
<code>DATA_SEGMENT_END</code>) than the former or not.
If the latter form is used, it means <var>commonpagesize</var> bytes of runtime
memory will be saved at the expense of up to <var>commonpagesize</var> wasted
bytes in the on-disk file.
<p>This expression can only be used directly in <code>SECTIONS</code> commands, not in
any output section descriptions and only once in the linker script.
<var>commonpagesize</var> should be less or equal to <var>maxpagesize</var> and should
be the system page size the object wants to be optimized for (while still
working on system page sizes up to <var>maxpagesize</var>).
<p>Example:
<pre class="smallexample"> . = DATA_SEGMENT_ALIGN(0x10000, 0x2000);
</pre>
<br><dt><code>DATA_SEGMENT_END(</code><var>exp</var><code>)</code>
<dd>This defines the end of data segment for <code>DATA_SEGMENT_ALIGN</code>
evaluation purposes.
<pre class="smallexample"> . = DATA_SEGMENT_END(.);
</pre>
<br><dt><code>DEFINED(</code><var>symbol</var><code>)</code>
<dd>Return 1 if <var>symbol</var> is in the linker global symbol table and is
defined, otherwise return 0. You can use this function to provide
default values for symbols. For example, the following script fragment
shows how to set a global symbol <code>begin</code> to the first location in
the <code>.text</code> section--but if a symbol called <code>begin</code> already
existed, its value is preserved:
<pre class="smallexample"> SECTIONS { ...
.text : {
begin = DEFINED(begin) ? begin : . ;
...
}
...
}
</pre>
<br><dt><code>LOADADDR(</code><var>section</var><code>)</code>
<dd>Return the absolute LMA of the named <var>section</var>. This is normally
the same as <code>ADDR</code>, but it may be different if the <code>AT</code>
attribute is used in the output section definition (see <a href="Output-Section-LMA.html#Output%20Section%20LMA">Output Section LMA</a>).
<br><dt><code>MAX(</code><var>exp1</var><code>, </code><var>exp2</var><code>)</code>
<dd>Returns the maximum of <var>exp1</var> and <var>exp2</var>.
<br><dt><code>MIN(</code><var>exp1</var><code>, </code><var>exp2</var><code>)</code>
<dd>Returns the minimum of <var>exp1</var> and <var>exp2</var>.
<br><dt><code>NEXT(</code><var>exp</var><code>)</code>
<dd>Return the next unallocated address that is a multiple of <var>exp</var>.
This function is closely related to <code>ALIGN(</code><var>exp</var><code>)</code>; unless you
use the <code>MEMORY</code> command to define discontinuous memory for the
output file, the two functions are equivalent.
<br><dt><code>SIZEOF(</code><var>section</var><code>)</code>
<dd>Return the size in bytes of the named <var>section</var>, if that section has
been allocated. If the section has not been allocated when this is
evaluated, the linker will report an error. In the following example,
<code>symbol_1</code> and <code>symbol_2</code> are assigned identical values:
<pre class="smallexample"> SECTIONS{ ...
.output {
.start = . ;
...
.end = . ;
}
symbol_1 = .end - .start ;
symbol_2 = SIZEOF(.output);
... }
</pre>
<br><dt><code>SIZEOF_HEADERS</code>
<dd><dt><code>sizeof_headers</code>
<dd>Return the size in bytes of the output file's headers. This is
information which appears at the start of the output file. You can use
this number when setting the start address of the first section, if you
choose, to facilitate paging.
<p>When producing an ELF output file, if the linker script uses the
<code>SIZEOF_HEADERS</code> builtin function, the linker must compute the
number of program headers before it has determined all the section
addresses and sizes. If the linker later discovers that it needs
additional program headers, it will report an error <code>not enough
room for program headers</code>. To avoid this error, you must avoid using
the <code>SIZEOF_HEADERS</code> function, or you must rework your linker
script to avoid forcing the linker to use additional program headers, or
you must define the program headers yourself using the <code>PHDRS</code>
command (see <a href="PHDRS.html#PHDRS">PHDRS</a>).
</dl>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -