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.3-2021.07 (arm-10.29))
5version 2.36.1.
6
7Copyright (C) 1991-2021 Free Software Foundation, Inc.
8
9Permission is granted to copy, distribute and/or modify this document
10under the terms of the GNU Free Documentation License, Version 1.3
11or any later version published by the Free Software Foundation;
12with no Invariant Sections, with no Front-Cover Texts, and with no
13Back-Cover Texts.  A copy of the license is included in the
14section entitled "GNU Free Documentation License". -->
15<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
16<head>
17<title>LD: Expression Section</title>
18
19<meta name="description" content="LD: Expression Section">
20<meta name="keywords" content="LD: Expression Section">
21<meta name="resource-type" content="document">
22<meta name="distribution" content="global">
23<meta name="Generator" content="makeinfo">
24<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
25<link href="index.html#Top" rel="start" title="Top">
26<link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
27<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
28<link href="Expressions.html#Expressions" rel="up" title="Expressions">
29<link href="Builtin-Functions.html#Builtin-Functions" rel="next" title="Builtin Functions">
30<link href="Evaluation.html#Evaluation" rel="previous" title="Evaluation">
31<style type="text/css">
32<!--
33a.summary-letter {text-decoration: none}
34blockquote.smallquotation {font-size: smaller}
35div.display {margin-left: 3.2em}
36div.example {margin-left: 3.2em}
37div.indentedblock {margin-left: 3.2em}
38div.lisp {margin-left: 3.2em}
39div.smalldisplay {margin-left: 3.2em}
40div.smallexample {margin-left: 3.2em}
41div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
42div.smalllisp {margin-left: 3.2em}
43kbd {font-style:oblique}
44pre.display {font-family: inherit}
45pre.format {font-family: inherit}
46pre.menu-comment {font-family: serif}
47pre.menu-preformatted {font-family: serif}
48pre.smalldisplay {font-family: inherit; font-size: smaller}
49pre.smallexample {font-size: smaller}
50pre.smallformat {font-family: inherit; font-size: smaller}
51pre.smalllisp {font-size: smaller}
52span.nocodebreak {white-space:nowrap}
53span.nolinebreak {white-space:nowrap}
54span.roman {font-family:serif; font-weight:normal}
55span.sansserif {font-family:sans-serif; font-weight:normal}
56ul.no-bullet {list-style: none}
57-->
58</style>
59
60
61</head>
62
63<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
64<a name="Expression-Section"></a>
65<div class="header">
66<p>
67Next: <a href="Builtin-Functions.html#Builtin-Functions" accesskey="n" rel="next">Builtin Functions</a>, Previous: <a href="Evaluation.html#Evaluation" accesskey="p" rel="previous">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>
68</div>
69<hr>
70<a name="The-Section-of-an-Expression"></a>
71<h4 class="subsection">3.10.8 The Section of an Expression</h4>
72<a name="index-expression-sections"></a>
73<a name="index-absolute-expressions"></a>
74<a name="index-relative-expressions"></a>
75<a name="index-absolute-and-relocatable-symbols"></a>
76<a name="index-relocatable-and-absolute-symbols"></a>
77<a name="index-symbols_002c-relocatable-and-absolute"></a>
78<p>Addresses and symbols may be section relative, or absolute.  A section
79relative symbol is relocatable.  If you request relocatable output
80using the &lsquo;<samp>-r</samp>&rsquo; option, a further link operation may change the
81value of a section relative symbol.  On the other hand, an absolute
82symbol will retain the same value throughout any further link
83operations.
84</p>
85<p>Some terms in linker expressions are addresses.  This is true of
86section relative symbols and for builtin functions that return an
87address, such as <code>ADDR</code>, <code>LOADADDR</code>, <code>ORIGIN</code> and
88<code>SEGMENT_START</code>.  Other terms are simply numbers, or are builtin
89functions that return a non-address value, such as <code>LENGTH</code>.
90One complication is that unless you set <code>LD_FEATURE (&quot;SANE_EXPR&quot;)</code>
91(see <a href="Miscellaneous-Commands.html#Miscellaneous-Commands">Miscellaneous Commands</a>), numbers and absolute symbols are treated
92differently depending on their location, for compatibility with older
93versions of <code>ld</code>.  Expressions appearing outside an output
94section definition treat all numbers as absolute addresses.
95Expressions appearing inside an output section definition treat
96absolute symbols as numbers.  If <code>LD_FEATURE (&quot;SANE_EXPR&quot;)</code> is
97given, then absolute symbols and numbers are simply treated as numbers
98everywhere.
99</p>
100<p>In the following simple example,
101</p>
102<div class="smallexample">
103<pre class="smallexample">SECTIONS
104  {
105    . = 0x100;
106    __executable_start = 0x100;
107    .data :
108    {
109      . = 0x10;
110      __data_start = 0x10;
111      *(.data)
112    }
113    &hellip;
114  }
115</pre></div>
116
117<p>both <code>.</code> and <code>__executable_start</code> are set to the absolute
118address 0x100 in the first two assignments, then both <code>.</code> and
119<code>__data_start</code> are set to 0x10 relative to the <code>.data</code>
120section in the second two assignments.
121</p>
122<p>For expressions involving numbers, relative addresses and absolute
123addresses, ld follows these rules to evaluate terms:
124</p>
125<ul>
126<li> Unary operations on an absolute address or number, and binary
127operations on two absolute addresses or two numbers, or between one
128absolute address and a number, apply the operator to the value(s).
129</li><li> Unary operations on a relative address, and binary operations on two
130relative addresses in the same section or between one relative address
131and a number, apply the operator to the offset part of the address(es).
132</li><li> Other binary operations, that is, between two relative addresses not
133in the same section, or between a relative address and an absolute
134address, first convert any non-absolute term to an absolute address
135before applying the operator.
136</li></ul>
137
138<p>The result section of each sub-expression is as follows:
139</p>
140<ul>
141<li> An operation involving only numbers results in a number.
142</li><li> The result of comparisons, &lsquo;<samp>&amp;&amp;</samp>&rsquo; and &lsquo;<samp>||</samp>&rsquo; is also a number.
143</li><li> The result of other binary arithmetic and logical operations on two
144relative addresses in the same section or two absolute addresses
145(after above conversions) is also a number when
146<code>LD_FEATURE (&quot;SANE_EXPR&quot;)</code> or inside an output section definition
147but an absolute address otherwise.
148</li><li> The result of other operations on relative addresses or one
149relative address and a number, is a relative address in the same
150section as the relative operand(s).
151</li><li> The result of other operations on absolute addresses (after above
152conversions) is an absolute address.
153</li></ul>
154
155<p>You can use the builtin function <code>ABSOLUTE</code> to force an expression
156to be absolute when it would otherwise be relative.  For example, to
157create an absolute symbol set to the address of the end of the output
158section &lsquo;<samp>.data</samp>&rsquo;:
159</p><div class="smallexample">
160<pre class="smallexample">SECTIONS
161  {
162    .data : { *(.data) _edata = ABSOLUTE(.); }
163  }
164</pre></div>
165<p>If &lsquo;<samp>ABSOLUTE</samp>&rsquo; were not used, &lsquo;<samp>_edata</samp>&rsquo; would be relative to the
166&lsquo;<samp>.data</samp>&rsquo; section.
167</p>
168<p>Using <code>LOADADDR</code> also forces an expression absolute, since this
169particular builtin function returns an absolute address.
170</p>
171<hr>
172<div class="header">
173<p>
174Next: <a href="Builtin-Functions.html#Builtin-Functions" accesskey="n" rel="next">Builtin Functions</a>, Previous: <a href="Evaluation.html#Evaluation" accesskey="p" rel="previous">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>
175</div>
176
177
178
179</body>
180</html>
181