toolchain/share/doc/ld.html/Output-Section-LMA.html

124 lines
6.0 KiB
HTML
Raw Normal View History

2024-01-10 05:24:32 +00:00
<html lang="en">
<head>
<title>Output Section LMA - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Output-Section-Attributes.html#Output-Section-Attributes" title="Output Section Attributes">
<link rel="prev" href="Output-Section-Type.html#Output-Section-Type" title="Output Section Type">
<link rel="next" href="Forced-Output-Alignment.html#Forced-Output-Alignment" title="Forced Output Alignment">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU linker LD
(GNU Toolchain for the A-profile Architecture 9.2-2019.12 (arm-9.10))
version 2.33.1.
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''.-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<a name="Output-Section-LMA"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Forced-Output-Alignment.html#Forced-Output-Alignment">Forced Output Alignment</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Output-Section-Type.html#Output-Section-Type">Output Section Type</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Output-Section-Attributes.html#Output-Section-Attributes">Output Section Attributes</a>
<hr>
</div>
<h5 class="subsubsection">3.6.8.2 Output Section LMA</h5>
<p><a name="index-AT_003e_0040var_007blma_005fregion_007d-500"></a><a name="index-AT_0028_0040var_007blma_007d_0029-501"></a><a name="index-load-address-502"></a><a name="index-section-load-address-503"></a>Every section has a virtual address (VMA) and a load address (LMA); see
<a href="Basic-Script-Concepts.html#Basic-Script-Concepts">Basic Script Concepts</a>. The virtual address is specified by the
see <a href="Output-Section-Address.html#Output-Section-Address">Output Section Address</a> described earlier. The load address is
specified by the <code>AT</code> or <code>AT&gt;</code> keywords. Specifying a load
address is optional.
<p>The <code>AT</code> keyword takes an expression as an argument. This
specifies the exact load address of the section. The <code>AT&gt;</code> keyword
takes the name of a memory region as an argument. See <a href="MEMORY.html#MEMORY">MEMORY</a>. The
load address of the section is set to the next free address in the
region, aligned to the section's alignment requirements.
<p>If neither <code>AT</code> nor <code>AT&gt;</code> is specified for an allocatable
section, the linker will use the following heuristic to determine the
load address:
<ul>
<li>If the section has a specific VMA address, then this is used as
the LMA address as well.
<li>If the section is not allocatable then its LMA is set to its VMA.
<li>Otherwise if a memory region can be found that is compatible
with the current section, and this region contains at least one
section, then the LMA is set so the difference between the
VMA and LMA is the same as the difference between the VMA and LMA of
the last section in the located region.
<li>If no memory regions have been declared then a default region
that covers the entire address space is used in the previous step.
<li>If no suitable region could be found, or there was no previous
section then the LMA is set equal to the VMA.
</ul>
<p><a name="index-ROM-initialized-data-504"></a><a name="index-initialized-data-in-ROM-505"></a>This feature is designed to make it easy to build a ROM image. For
example, the following linker script creates three output sections: one
called &lsquo;<samp><span class="samp">.text</span></samp>&rsquo;, which starts at <code>0x1000</code>, one called
&lsquo;<samp><span class="samp">.mdata</span></samp>&rsquo;, which is loaded at the end of the &lsquo;<samp><span class="samp">.text</span></samp>&rsquo; section
even though its VMA is <code>0x2000</code>, and one called &lsquo;<samp><span class="samp">.bss</span></samp>&rsquo; to hold
uninitialized data at address <code>0x3000</code>. The symbol <code>_data</code> is
defined with the value <code>0x2000</code>, which shows that the location
counter holds the VMA value, not the LMA value.
<pre class="smallexample"> SECTIONS
{
.text 0x1000 : { *(.text) _etext = . ; }
.mdata 0x2000 :
AT ( ADDR (.text) + SIZEOF (.text) )
{ _data = . ; *(.data); _edata = . ; }
.bss 0x3000 :
{ _bstart = . ; *(.bss) *(COMMON) ; _bend = . ;}
}
</pre>
<p>The run-time initialization code for use with a program generated with
this linker script would include something like the following, to copy
the initialized data from the ROM image to its runtime address. Notice
how this code takes advantage of the symbols defined by the linker
script.
<pre class="smallexample"> extern char _etext, _data, _edata, _bstart, _bend;
char *src = &amp;_etext;
char *dst = &amp;_data;
/* ROM has data at end of text; copy it. */
while (dst &lt; &amp;_edata)
*dst++ = *src++;
/* Zero bss. */
for (dst = &amp;_bstart; dst&lt; &amp;_bend; dst++)
*dst = 0;
</pre>
</body></html>