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: Location Counter</title> 18 19<meta name="description" content="LD: Location Counter"> 20<meta name="keywords" content="LD: Location Counter"> 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="Operators.html#Operators" rel="next" title="Operators"> 30<link href="Orphan-Sections.html#Orphan-Sections" rel="previous" title="Orphan Sections"> 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="Location-Counter"></a> 65<div class="header"> 66<p> 67Next: <a href="Operators.html#Operators" accesskey="n" rel="next">Operators</a>, Previous: <a href="Orphan-Sections.html#Orphan-Sections" accesskey="p" rel="previous">Orphan Sections</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">Expressions</a> [<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-Location-Counter"></a> 71<h4 class="subsection">3.10.5 The Location Counter</h4> 72<a name="index-_002e"></a> 73<a name="index-dot"></a> 74<a name="index-location-counter"></a> 75<a name="index-current-output-location"></a> 76<p>The special linker variable <em>dot</em> ‘<samp>.</samp>’ always contains the 77current output location counter. Since the <code>.</code> always refers to a 78location in an output section, it may only appear in an expression 79within a <code>SECTIONS</code> command. The <code>.</code> symbol may appear 80anywhere that an ordinary symbol is allowed in an expression. 81</p> 82<a name="index-holes"></a> 83<p>Assigning a value to <code>.</code> will cause the location counter to be 84moved. This may be used to create holes in the output section. The 85location counter may not be moved backwards inside an output section, 86and may not be moved backwards outside of an output section if so 87doing creates areas with overlapping LMAs. 88</p> 89<div class="smallexample"> 90<pre class="smallexample">SECTIONS 91{ 92 output : 93 { 94 file1(.text) 95 . = . + 1000; 96 file2(.text) 97 . += 1000; 98 file3(.text) 99 } = 0x12345678; 100} 101</pre></div> 102<p>In the previous example, the ‘<samp>.text</samp>’ section from <samp>file1</samp> is 103located at the beginning of the output section ‘<samp>output</samp>’. It is 104followed by a 1000 byte gap. Then the ‘<samp>.text</samp>’ section from 105<samp>file2</samp> appears, also with a 1000 byte gap following before the 106‘<samp>.text</samp>’ section from <samp>file3</samp>. The notation ‘<samp>= 0x12345678</samp>’ 107specifies what data to write in the gaps (see <a href="Output-Section-Fill.html#Output-Section-Fill">Output Section Fill</a>). 108</p> 109<a name="index-dot-inside-sections"></a> 110<p>Note: <code>.</code> actually refers to the byte offset from the start of the 111current containing object. Normally this is the <code>SECTIONS</code> 112statement, whose start address is 0, hence <code>.</code> can be used as an 113absolute address. If <code>.</code> is used inside a section description 114however, it refers to the byte offset from the start of that section, 115not an absolute address. Thus in a script like this: 116</p> 117<div class="smallexample"> 118<pre class="smallexample">SECTIONS 119{ 120 . = 0x100 121 .text: { 122 *(.text) 123 . = 0x200 124 } 125 . = 0x500 126 .data: { 127 *(.data) 128 . += 0x600 129 } 130} 131</pre></div> 132 133<p>The ‘<samp>.text</samp>’ section will be assigned a starting address of 0x100 134and a size of exactly 0x200 bytes, even if there is not enough data in 135the ‘<samp>.text</samp>’ input sections to fill this area. (If there is too 136much data, an error will be produced because this would be an attempt to 137move <code>.</code> backwards). The ‘<samp>.data</samp>’ section will start at 0x500 138and it will have an extra 0x600 bytes worth of space after the end of 139the values from the ‘<samp>.data</samp>’ input sections and before the end of 140the ‘<samp>.data</samp>’ output section itself. 141</p> 142<a name="index-dot-outside-sections"></a> 143<p>Setting symbols to the value of the location counter outside of an 144output section statement can result in unexpected values if the linker 145needs to place orphan sections. For example, given the following: 146</p> 147<div class="smallexample"> 148<pre class="smallexample">SECTIONS 149{ 150 start_of_text = . ; 151 .text: { *(.text) } 152 end_of_text = . ; 153 154 start_of_data = . ; 155 .data: { *(.data) } 156 end_of_data = . ; 157} 158</pre></div> 159 160<p>If the linker needs to place some input section, e.g. <code>.rodata</code>, 161not mentioned in the script, it might choose to place that section 162between <code>.text</code> and <code>.data</code>. You might think the linker 163should place <code>.rodata</code> on the blank line in the above script, but 164blank lines are of no particular significance to the linker. As well, 165the linker doesn’t associate the above symbol names with their 166sections. Instead, it assumes that all assignments or other 167statements belong to the previous output section, except for the 168special case of an assignment to <code>.</code>. I.e., the linker will 169place the orphan <code>.rodata</code> section as if the script was written 170as follows: 171</p> 172<div class="smallexample"> 173<pre class="smallexample">SECTIONS 174{ 175 start_of_text = . ; 176 .text: { *(.text) } 177 end_of_text = . ; 178 179 start_of_data = . ; 180 .rodata: { *(.rodata) } 181 .data: { *(.data) } 182 end_of_data = . ; 183} 184</pre></div> 185 186<p>This may or may not be the script author’s intention for the value of 187<code>start_of_data</code>. One way to influence the orphan section 188placement is to assign the location counter to itself, as the linker 189assumes that an assignment to <code>.</code> is setting the start address of 190a following output section and thus should be grouped with that 191section. So you could write: 192</p> 193<div class="smallexample"> 194<pre class="smallexample">SECTIONS 195{ 196 start_of_text = . ; 197 .text: { *(.text) } 198 end_of_text = . ; 199 200 . = . ; 201 start_of_data = . ; 202 .data: { *(.data) } 203 end_of_data = . ; 204} 205</pre></div> 206 207<p>Now, the orphan <code>.rodata</code> section will be placed between 208<code>end_of_text</code> and <code>start_of_data</code>. 209</p> 210<hr> 211<div class="header"> 212<p> 213Next: <a href="Operators.html#Operators" accesskey="n" rel="next">Operators</a>, Previous: <a href="Orphan-Sections.html#Orphan-Sections" accesskey="p" rel="previous">Orphan Sections</a>, Up: <a href="Expressions.html#Expressions" accesskey="u" rel="up">Expressions</a> [<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> 214</div> 215 216 217 218</body> 219</html> 220