1<html lang="en"> 2<head> 3<title>Location Counter - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Expressions.html#Expressions" title="Expressions"> 9<link rel="prev" href="Orphan-Sections.html#Orphan-Sections" title="Orphan Sections"> 10<link rel="next" href="Operators.html#Operators" title="Operators"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13This file documents the GNU linker LD 14(Sourcery CodeBench Lite 2013.11-24) 15version 2.23.52. 16 17Copyright (C) 1991-2013 Free Software Foundation, Inc. 18 19Permission is granted to copy, distribute and/or modify this document 20under the terms of the GNU Free Documentation License, Version 1.3 21or any later version published by the Free Software Foundation; 22with no Invariant Sections, with no Front-Cover Texts, and with no 23Back-Cover Texts. A copy of the license is included in the 24section entitled ``GNU Free Documentation License''.--> 25<meta http-equiv="Content-Style-Type" content="text/css"> 26<style type="text/css"><!-- 27 pre.display { font-family:inherit } 28 pre.format { font-family:inherit } 29 pre.smalldisplay { font-family:inherit; font-size:smaller } 30 pre.smallformat { font-family:inherit; font-size:smaller } 31 pre.smallexample { font-size:smaller } 32 pre.smalllisp { font-size:smaller } 33 span.sc { font-variant:small-caps } 34 span.roman { font-family:serif; font-weight:normal; } 35 span.sansserif { font-family:sans-serif; font-weight:normal; } 36--></style> 37<link rel="stylesheet" type="text/css" href="../cs.css"> 38</head> 39<body> 40<div class="node"> 41<a name="Location-Counter"></a> 42<p> 43Next: <a rel="next" accesskey="n" href="Operators.html#Operators">Operators</a>, 44Previous: <a rel="previous" accesskey="p" href="Orphan-Sections.html#Orphan-Sections">Orphan Sections</a>, 45Up: <a rel="up" accesskey="u" href="Expressions.html#Expressions">Expressions</a> 46<hr> 47</div> 48 49<h4 class="subsection">3.10.5 The Location Counter</h4> 50 51<p><a name="index-g_t_002e-524"></a><a name="index-dot-525"></a><a name="index-location-counter-526"></a><a name="index-current-output-location-527"></a>The special linker variable <dfn>dot</dfn> ‘<samp><span class="samp">.</span></samp>’ always contains the 52current output location counter. Since the <code>.</code> always refers to a 53location in an output section, it may only appear in an expression 54within a <code>SECTIONS</code> command. The <code>.</code> symbol may appear 55anywhere that an ordinary symbol is allowed in an expression. 56 57 <p><a name="index-holes-528"></a>Assigning a value to <code>.</code> will cause the location counter to be 58moved. This may be used to create holes in the output section. The 59location counter may not be moved backwards inside an output section, 60and may not be moved backwards outside of an output section if so 61doing creates areas with overlapping LMAs. 62 63<pre class="smallexample"> SECTIONS 64 { 65 output : 66 { 67 file1(.text) 68 . = . + 1000; 69 file2(.text) 70 . += 1000; 71 file3(.text) 72 } = 0x12345678; 73 } 74</pre> 75 <p class="noindent">In the previous example, the ‘<samp><span class="samp">.text</span></samp>’ section from <samp><span class="file">file1</span></samp> is 76located at the beginning of the output section ‘<samp><span class="samp">output</span></samp>’. It is 77followed by a 1000 byte gap. Then the ‘<samp><span class="samp">.text</span></samp>’ section from 78<samp><span class="file">file2</span></samp> appears, also with a 1000 byte gap following before the 79‘<samp><span class="samp">.text</span></samp>’ section from <samp><span class="file">file3</span></samp>. The notation ‘<samp><span class="samp">= 0x12345678</span></samp>’ 80specifies what data to write in the gaps (see <a href="Output-Section-Fill.html#Output-Section-Fill">Output Section Fill</a>). 81 82 <p><a name="index-dot-inside-sections-529"></a>Note: <code>.</code> actually refers to the byte offset from the start of the 83current containing object. Normally this is the <code>SECTIONS</code> 84statement, whose start address is 0, hence <code>.</code> can be used as an 85absolute address. If <code>.</code> is used inside a section description 86however, it refers to the byte offset from the start of that section, 87not an absolute address. Thus in a script like this: 88 89<pre class="smallexample"> SECTIONS 90 { 91 . = 0x100 92 .text: { 93 *(.text) 94 . = 0x200 95 } 96 . = 0x500 97 .data: { 98 *(.data) 99 . += 0x600 100 } 101 } 102</pre> 103 <p>The ‘<samp><span class="samp">.text</span></samp>’ section will be assigned a starting address of 0x100 104and a size of exactly 0x200 bytes, even if there is not enough data in 105the ‘<samp><span class="samp">.text</span></samp>’ input sections to fill this area. (If there is too 106much data, an error will be produced because this would be an attempt to 107move <code>.</code> backwards). The ‘<samp><span class="samp">.data</span></samp>’ section will start at 0x500 108and it will have an extra 0x600 bytes worth of space after the end of 109the values from the ‘<samp><span class="samp">.data</span></samp>’ input sections and before the end of 110the ‘<samp><span class="samp">.data</span></samp>’ output section itself. 111 112 <p><a name="index-dot-outside-sections-530"></a>Setting symbols to the value of the location counter outside of an 113output section statement can result in unexpected values if the linker 114needs to place orphan sections. For example, given the following: 115 116<pre class="smallexample"> SECTIONS 117 { 118 start_of_text = . ; 119 .text: { *(.text) } 120 end_of_text = . ; 121 122 start_of_data = . ; 123 .data: { *(.data) } 124 end_of_data = . ; 125 } 126</pre> 127 <p>If the linker needs to place some input section, e.g. <code>.rodata</code>, 128not mentioned in the script, it might choose to place that section 129between <code>.text</code> and <code>.data</code>. You might think the linker 130should place <code>.rodata</code> on the blank line in the above script, but 131blank lines are of no particular significance to the linker. As well, 132the linker doesn't associate the above symbol names with their 133sections. Instead, it assumes that all assignments or other 134statements belong to the previous output section, except for the 135special case of an assignment to <code>.</code>. I.e., the linker will 136place the orphan <code>.rodata</code> section as if the script was written 137as follows: 138 139<pre class="smallexample"> SECTIONS 140 { 141 start_of_text = . ; 142 .text: { *(.text) } 143 end_of_text = . ; 144 145 start_of_data = . ; 146 .rodata: { *(.rodata) } 147 .data: { *(.data) } 148 end_of_data = . ; 149 } 150</pre> 151 <p>This may or may not be the script author's intention for the value of 152<code>start_of_data</code>. One way to influence the orphan section 153placement is to assign the location counter to itself, as the linker 154assumes that an assignment to <code>.</code> is setting the start address of 155a following output section and thus should be grouped with that 156section. So you could write: 157 158<pre class="smallexample"> SECTIONS 159 { 160 start_of_text = . ; 161 .text: { *(.text) } 162 end_of_text = . ; 163 164 . = . ; 165 start_of_data = . ; 166 .data: { *(.data) } 167 end_of_data = . ; 168 } 169</pre> 170 <p>Now, the orphan <code>.rodata</code> section will be placed between 171<code>end_of_text</code> and <code>start_of_data</code>. 172 173 </body></html> 174 175