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: Output Section LMA</title>
18
19<meta name="description" content="LD: Output Section LMA">
20<meta name="keywords" content="LD: Output Section LMA">
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="Output-Section-Attributes.html#Output-Section-Attributes" rel="up" title="Output Section Attributes">
29<link href="Forced-Output-Alignment.html#Forced-Output-Alignment" rel="next" title="Forced Output Alignment">
30<link href="Output-Section-Type.html#Output-Section-Type" rel="previous" title="Output Section Type">
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="Output-Section-LMA"></a>
65<div class="header">
66<p>
67Next: <a href="Forced-Output-Alignment.html#Forced-Output-Alignment" accesskey="n" rel="next">Forced Output Alignment</a>, Previous: <a href="Output-Section-Type.html#Output-Section-Type" accesskey="p" rel="previous">Output Section Type</a>, Up: <a href="Output-Section-Attributes.html#Output-Section-Attributes" accesskey="u" rel="up">Output Section Attributes</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="Output-Section-LMA-1"></a>
71<h4 class="subsubsection">3.6.8.2 Output Section LMA</h4>
72<a name="index-AT_003elma_005fregion"></a>
73<a name="index-AT_0028lma_0029"></a>
74<a name="index-load-address"></a>
75<a name="index-section-load-address"></a>
76<p>Every section has a virtual address (VMA) and a load address (LMA); see
77<a href="Basic-Script-Concepts.html#Basic-Script-Concepts">Basic Script Concepts</a>.  The virtual address is specified by the
78see <a href="Output-Section-Address.html#Output-Section-Address">Output Section Address</a> described earlier.  The load address is
79specified by the <code>AT</code> or <code>AT&gt;</code> keywords.  Specifying a load
80address is optional.
81</p>
82<p>The <code>AT</code> keyword takes an expression as an argument.  This
83specifies the exact load address of the section.  The <code>AT&gt;</code> keyword
84takes the name of a memory region as an argument.  See <a href="MEMORY.html#MEMORY">MEMORY</a>.  The
85load address of the section is set to the next free address in the
86region, aligned to the section&rsquo;s alignment requirements.
87</p>
88<p>If neither <code>AT</code> nor <code>AT&gt;</code> is specified for an allocatable
89section, the linker will use the following heuristic to determine the
90load address:
91</p>
92<ul>
93<li> If the section has a specific VMA address, then this is used as
94the LMA address as well.
95
96</li><li> If the section is not allocatable then its LMA is set to its VMA.
97
98</li><li> Otherwise if a memory region can be found that is compatible
99with the current section, and this region contains at least one
100section, then the LMA is set so the difference between the
101VMA and LMA is the same as the difference between the VMA and LMA of
102the last section in the located region.
103
104</li><li> If no memory regions have been declared then a default region
105that covers the entire address space is used in the previous step.
106
107</li><li> If no suitable region could be found, or there was no previous
108section then the LMA is set equal to the VMA.
109</li></ul>
110
111<a name="index-ROM-initialized-data"></a>
112<a name="index-initialized-data-in-ROM"></a>
113<p>This feature is designed to make it easy to build a ROM image.  For
114example, the following linker script creates three output sections: one
115called &lsquo;<samp>.text</samp>&rsquo;, which starts at <code>0x1000</code>, one called
116&lsquo;<samp>.mdata</samp>&rsquo;, which is loaded at the end of the &lsquo;<samp>.text</samp>&rsquo; section
117even though its VMA is <code>0x2000</code>, and one called &lsquo;<samp>.bss</samp>&rsquo; to hold
118uninitialized data at address <code>0x3000</code>.  The symbol <code>_data</code> is
119defined with the value <code>0x2000</code>, which shows that the location
120counter holds the VMA value, not the LMA value.
121</p>
122<div class="smallexample">
123<pre class="smallexample">SECTIONS
124  {
125  .text 0x1000 : { *(.text) _etext = . ; }
126  .mdata 0x2000 :
127    AT ( ADDR (.text) + SIZEOF (.text) )
128    { _data = . ; *(.data); _edata = . ;  }
129  .bss 0x3000 :
130    { _bstart = . ;  *(.bss) *(COMMON) ; _bend = . ;}
131}
132</pre></div>
133
134<p>The run-time initialization code for use with a program generated with
135this linker script would include something like the following, to copy
136the initialized data from the ROM image to its runtime address.  Notice
137how this code takes advantage of the symbols defined by the linker
138script.
139</p>
140<div class="smallexample">
141<pre class="smallexample">extern char _etext, _data, _edata, _bstart, _bend;
142char *src = &amp;_etext;
143char *dst = &amp;_data;
144
145/* ROM has data at end of text; copy it.  */
146while (dst &lt; &amp;_edata)
147  *dst++ = *src++;
148
149/* Zero bss.  */
150for (dst = &amp;_bstart; dst&lt; &amp;_bend; dst++)
151  *dst = 0;
152</pre></div>
153
154<hr>
155<div class="header">
156<p>
157Next: <a href="Forced-Output-Alignment.html#Forced-Output-Alignment" accesskey="n" rel="next">Forced Output Alignment</a>, Previous: <a href="Output-Section-Type.html#Output-Section-Type" accesskey="p" rel="previous">Output Section Type</a>, Up: <a href="Output-Section-Attributes.html#Output-Section-Attributes" accesskey="u" rel="up">Output Section Attributes</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>
158</div>
159
160
161
162</body>
163</html>
164