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: MEMORY</title>
18
19<meta name="description" content="LD: MEMORY">
20<meta name="keywords" content="LD: MEMORY">
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="Scripts.html#Scripts" rel="up" title="Scripts">
29<link href="PHDRS.html#PHDRS" rel="next" title="PHDRS">
30<link href="Overlay-Description.html#Overlay-Description" rel="previous" title="Overlay Description">
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="MEMORY"></a>
65<div class="header">
66<p>
67Next: <a href="PHDRS.html#PHDRS" accesskey="n" rel="next">PHDRS</a>, Previous: <a href="SECTIONS.html#SECTIONS" accesskey="p" rel="previous">SECTIONS</a>, Up: <a href="Scripts.html#Scripts" accesskey="u" rel="up">Scripts</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="MEMORY-Command"></a>
71<h3 class="section">3.7 MEMORY Command</h3>
72<a name="index-MEMORY"></a>
73<a name="index-memory-regions"></a>
74<a name="index-regions-of-memory"></a>
75<a name="index-allocating-memory"></a>
76<a name="index-discontinuous-memory"></a>
77<p>The linker&rsquo;s default configuration permits allocation of all available
78memory.  You can override this by using the <code>MEMORY</code> command.
79</p>
80<p>The <code>MEMORY</code> command describes the location and size of blocks of
81memory in the target.  You can use it to describe which memory regions
82may be used by the linker, and which memory regions it must avoid.  You
83can then assign sections to particular memory regions.  The linker will
84set section addresses based on the memory regions, and will warn about
85regions that become too full.  The linker will not shuffle sections
86around to fit into the available regions.
87</p>
88<p>A linker script may contain many uses of the <code>MEMORY</code> command,
89however, all memory blocks defined are treated as if they were
90specified inside a single <code>MEMORY</code> command.  The syntax for
91<code>MEMORY</code> is:
92</p><div class="smallexample">
93<pre class="smallexample">MEMORY
94  {
95    <var>name</var> [(<var>attr</var>)] : ORIGIN = <var>origin</var>, LENGTH = <var>len</var>
96    &hellip;
97  }
98</pre></div>
99
100<p>The <var>name</var> is a name used in the linker script to refer to the
101region.  The region name has no meaning outside of the linker script.
102Region names are stored in a separate name space, and will not conflict
103with symbol names, file names, or section names.  Each memory region
104must have a distinct name within the <code>MEMORY</code> command.  However you can
105add later alias names to existing memory regions with the <a href="REGION_005fALIAS.html#REGION_005fALIAS">REGION_ALIAS</a>
106command.
107</p>
108<a name="index-memory-region-attributes"></a>
109<p>The <var>attr</var> string is an optional list of attributes that specify
110whether to use a particular memory region for an input section which is
111not explicitly mapped in the linker script.  As described in
112<a href="SECTIONS.html#SECTIONS">SECTIONS</a>, if you do not specify an output section for some input
113section, the linker will create an output section with the same name as
114the input section.  If you define region attributes, the linker will use
115them to select the memory region for the output section that it creates.
116</p>
117<p>The <var>attr</var> string must consist only of the following characters:
118</p><dl compact="compact">
119<dt>&lsquo;<samp>R</samp>&rsquo;</dt>
120<dd><p>Read-only section
121</p></dd>
122<dt>&lsquo;<samp>W</samp>&rsquo;</dt>
123<dd><p>Read/write section
124</p></dd>
125<dt>&lsquo;<samp>X</samp>&rsquo;</dt>
126<dd><p>Executable section
127</p></dd>
128<dt>&lsquo;<samp>A</samp>&rsquo;</dt>
129<dd><p>Allocatable section
130</p></dd>
131<dt>&lsquo;<samp>I</samp>&rsquo;</dt>
132<dd><p>Initialized section
133</p></dd>
134<dt>&lsquo;<samp>L</samp>&rsquo;</dt>
135<dd><p>Same as &lsquo;<samp>I</samp>&rsquo;
136</p></dd>
137<dt>&lsquo;<samp>!</samp>&rsquo;</dt>
138<dd><p>Invert the sense of any of the attributes that follow
139</p></dd>
140</dl>
141
142<p>If an unmapped section matches any of the listed attributes other than
143&lsquo;<samp>!</samp>&rsquo;, it will be placed in the memory region.  The &lsquo;<samp>!</samp>&rsquo;
144attribute reverses the test for the characters that follow, so that an
145unmapped section will be placed in the memory region only if it does
146not match any of the attributes listed afterwards.  Thus an attribute
147string of &lsquo;<samp>RW!X</samp>&rsquo; will match any unmapped section that has either
148or both of the &lsquo;<samp>R</samp>&rsquo; and &lsquo;<samp>W</samp>&rsquo; attributes, but only as long as
149the section does not also have the &lsquo;<samp>X</samp>&rsquo; attribute.
150</p>
151<a name="index-ORIGIN-_003d"></a>
152<a name="index-o-_003d"></a>
153<a name="index-org-_003d"></a>
154<p>The <var>origin</var> is an numerical expression for the start address of
155the memory region.  The expression must evaluate to a constant and it
156cannot involve any symbols.  The keyword <code>ORIGIN</code> may be
157abbreviated to <code>org</code> or <code>o</code> (but not, for example,
158<code>ORG</code>).
159</p>
160<a name="index-LENGTH-_003d"></a>
161<a name="index-len-_003d"></a>
162<a name="index-l-_003d"></a>
163<p>The <var>len</var> is an expression for the size in bytes of the memory
164region.  As with the <var>origin</var> expression, the expression must
165be numerical only and must evaluate to a constant.  The keyword
166<code>LENGTH</code> may be abbreviated to <code>len</code> or <code>l</code>.
167</p>
168<p>In the following example, we specify that there are two memory regions
169available for allocation: one starting at &lsquo;<samp>0</samp>&rsquo; for 256 kilobytes,
170and the other starting at &lsquo;<samp>0x40000000</samp>&rsquo; for four megabytes.  The
171linker will place into the &lsquo;<samp>rom</samp>&rsquo; memory region every section which
172is not explicitly mapped into a memory region, and is either read-only
173or executable.  The linker will place other sections which are not
174explicitly mapped into a memory region into the &lsquo;<samp>ram</samp>&rsquo; memory
175region.
176</p>
177<div class="smallexample">
178<pre class="smallexample">MEMORY
179  {
180    rom (rx)  : ORIGIN = 0, LENGTH = 256K
181    ram (!rx) : org = 0x40000000, l = 4M
182  }
183</pre></div>
184
185<p>Once you define a memory region, you can direct the linker to place
186specific output sections into that memory region by using the
187&lsquo;<samp>&gt;<var>region</var></samp>&rsquo; output section attribute.  For example, if you have
188a memory region named &lsquo;<samp>mem</samp>&rsquo;, you would use &lsquo;<samp>&gt;mem</samp>&rsquo; in the
189output section definition.  See <a href="Output-Section-Region.html#Output-Section-Region">Output Section Region</a>.  If no address
190was specified for the output section, the linker will set the address to
191the next available address within the memory region.  If the combined
192output sections directed to a memory region are too large for the
193region, the linker will issue an error message.
194</p>
195<p>It is possible to access the origin and length of a memory in an
196expression via the <code>ORIGIN(<var>memory</var>)</code> and
197<code>LENGTH(<var>memory</var>)</code> functions:
198</p>
199<div class="smallexample">
200<pre class="smallexample">  _fstack = ORIGIN(ram) + LENGTH(ram) - 4;
201</pre></div>
202
203<hr>
204<div class="header">
205<p>
206Next: <a href="PHDRS.html#PHDRS" accesskey="n" rel="next">PHDRS</a>, Previous: <a href="SECTIONS.html#SECTIONS" accesskey="p" rel="previous">SECTIONS</a>, Up: <a href="Scripts.html#Scripts" accesskey="u" rel="up">Scripts</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>
207</div>
208
209
210
211</body>
212</html>
213