You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
8.7 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents the GNU linker LD
(SiFive Binutils 2.32.0-2020.04.1)
version 2.32.
Copyright (C) 1991-2019 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3
or any later version published by the Free Software Foundation;
with no Invariant Sections, with no Front-Cover Texts, and with no
Back-Cover Texts. A copy of the license is included in the
section entitled "GNU Free Documentation License". -->
<!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ -->
<head>
<title>Location Counter (LD)</title>
<meta name="description" content="Location Counter (LD)">
<meta name="keywords" content="Location Counter (LD)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Expressions.html#Expressions" rel="up" title="Expressions">
<link href="Operators.html#Operators" rel="next" title="Operators">
<link href="Orphan-Sections.html#Orphan-Sections" rel="prev" title="Orphan Sections">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="Location-Counter"></a>
<div class="header">
<p>
Next: <a href="Operators.html#Operators" accesskey="n" rel="next">Operators</a>, Previous: <a href="Orphan-Sections.html#Orphan-Sections" accesskey="p" rel="prev">Orphan Sections</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>
</div>
<hr>
<a name="The-Location-Counter"></a>
<h4 class="subsection">3.10.5 The Location Counter</h4>
<a name="index-_002e"></a>
<a name="index-dot"></a>
<a name="index-location-counter"></a>
<a name="index-current-output-location"></a>
<p>The special linker variable <em>dot</em> &lsquo;<samp>.</samp>&rsquo; always contains the
current output location counter. Since the <code>.</code> always refers to a
location in an output section, it may only appear in an expression
within a <code>SECTIONS</code> command. The <code>.</code> symbol may appear
anywhere that an ordinary symbol is allowed in an expression.
</p>
<a name="index-holes"></a>
<p>Assigning a value to <code>.</code> will cause the location counter to be
moved. This may be used to create holes in the output section. The
location counter may not be moved backwards inside an output section,
and may not be moved backwards outside of an output section if so
doing creates areas with overlapping LMAs.
</p>
<div class="smallexample">
<pre class="smallexample">SECTIONS
{
output :
{
file1(.text)
. = . + 1000;
file2(.text)
. += 1000;
file3(.text)
} = 0x12345678;
}
</pre></div>
<p>In the previous example, the &lsquo;<samp>.text</samp>&rsquo; section from <samp>file1</samp> is
located at the beginning of the output section &lsquo;<samp>output</samp>&rsquo;. It is
followed by a 1000 byte gap. Then the &lsquo;<samp>.text</samp>&rsquo; section from
<samp>file2</samp> appears, also with a 1000 byte gap following before the
&lsquo;<samp>.text</samp>&rsquo; section from <samp>file3</samp>. The notation &lsquo;<samp>= 0x12345678</samp>&rsquo;
specifies what data to write in the gaps (see <a href="Output-Section-Fill.html#Output-Section-Fill">Output Section Fill</a>).
</p>
<a name="index-dot-inside-sections"></a>
<p>Note: <code>.</code> actually refers to the byte offset from the start of the
current containing object. Normally this is the <code>SECTIONS</code>
statement, whose start address is 0, hence <code>.</code> can be used as an
absolute address. If <code>.</code> is used inside a section description
however, it refers to the byte offset from the start of that section,
not an absolute address. Thus in a script like this:
</p>
<div class="smallexample">
<pre class="smallexample">SECTIONS
{
. = 0x100
.text: {
*(.text)
. = 0x200
}
. = 0x500
.data: {
*(.data)
. += 0x600
}
}
</pre></div>
<p>The &lsquo;<samp>.text</samp>&rsquo; section will be assigned a starting address of 0x100
and a size of exactly 0x200 bytes, even if there is not enough data in
the &lsquo;<samp>.text</samp>&rsquo; input sections to fill this area. (If there is too
much data, an error will be produced because this would be an attempt to
move <code>.</code> backwards). The &lsquo;<samp>.data</samp>&rsquo; section will start at 0x500
and it will have an extra 0x600 bytes worth of space after the end of
the values from the &lsquo;<samp>.data</samp>&rsquo; input sections and before the end of
the &lsquo;<samp>.data</samp>&rsquo; output section itself.
</p>
<a name="index-dot-outside-sections"></a>
<p>Setting symbols to the value of the location counter outside of an
output section statement can result in unexpected values if the linker
needs to place orphan sections. For example, given the following:
</p>
<div class="smallexample">
<pre class="smallexample">SECTIONS
{
start_of_text = . ;
.text: { *(.text) }
end_of_text = . ;
start_of_data = . ;
.data: { *(.data) }
end_of_data = . ;
}
</pre></div>
<p>If the linker needs to place some input section, e.g. <code>.rodata</code>,
not mentioned in the script, it might choose to place that section
between <code>.text</code> and <code>.data</code>. You might think the linker
should place <code>.rodata</code> on the blank line in the above script, but
blank lines are of no particular significance to the linker. As well,
the linker doesn&rsquo;t associate the above symbol names with their
sections. Instead, it assumes that all assignments or other
statements belong to the previous output section, except for the
special case of an assignment to <code>.</code>. I.e., the linker will
place the orphan <code>.rodata</code> section as if the script was written
as follows:
</p>
<div class="smallexample">
<pre class="smallexample">SECTIONS
{
start_of_text = . ;
.text: { *(.text) }
end_of_text = . ;
start_of_data = . ;
.rodata: { *(.rodata) }
.data: { *(.data) }
end_of_data = . ;
}
</pre></div>
<p>This may or may not be the script author&rsquo;s intention for the value of
<code>start_of_data</code>. One way to influence the orphan section
placement is to assign the location counter to itself, as the linker
assumes that an assignment to <code>.</code> is setting the start address of
a following output section and thus should be grouped with that
section. So you could write:
</p>
<div class="smallexample">
<pre class="smallexample">SECTIONS
{
start_of_text = . ;
.text: { *(.text) }
end_of_text = . ;
. = . ;
start_of_data = . ;
.data: { *(.data) }
end_of_data = . ;
}
</pre></div>
<p>Now, the orphan <code>.rodata</code> section will be placed between
<code>end_of_text</code> and <code>start_of_data</code>.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Operators.html#Operators" accesskey="n" rel="next">Operators</a>, Previous: <a href="Orphan-Sections.html#Orphan-Sections" accesskey="p" rel="prev">Orphan Sections</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>
</div>
</body>
</html>