MEMORY.html 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 Binutils)
  5. version 2.28.
  6. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  7. Permission is granted to copy, distribute and/or modify this document
  8. under the terms of the GNU Free Documentation License, Version 1.3
  9. or any later version published by the Free Software Foundation;
  10. with no Invariant Sections, with no Front-Cover Texts, and with no
  11. Back-Cover Texts. A copy of the license is included in the
  12. section entitled "GNU Free Documentation License". -->
  13. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <title>LD: MEMORY</title>
  16. <meta name="description" content="LD: MEMORY">
  17. <meta name="keywords" content="LD: MEMORY">
  18. <meta name="resource-type" content="document">
  19. <meta name="distribution" content="global">
  20. <meta name="Generator" content="makeinfo">
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  22. <link href="index.html#Top" rel="start" title="Top">
  23. <link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
  24. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  25. <link href="Scripts.html#Scripts" rel="up" title="Scripts">
  26. <link href="PHDRS.html#PHDRS" rel="next" title="PHDRS">
  27. <link href="Overlay-Description.html#Overlay-Description" rel="prev" title="Overlay Description">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.smallquotation {font-size: smaller}
  32. div.display {margin-left: 3.2em}
  33. div.example {margin-left: 3.2em}
  34. div.indentedblock {margin-left: 3.2em}
  35. div.lisp {margin-left: 3.2em}
  36. div.smalldisplay {margin-left: 3.2em}
  37. div.smallexample {margin-left: 3.2em}
  38. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  39. div.smalllisp {margin-left: 3.2em}
  40. kbd {font-style:oblique}
  41. pre.display {font-family: inherit}
  42. pre.format {font-family: inherit}
  43. pre.menu-comment {font-family: serif}
  44. pre.menu-preformatted {font-family: serif}
  45. pre.smalldisplay {font-family: inherit; font-size: smaller}
  46. pre.smallexample {font-size: smaller}
  47. pre.smallformat {font-family: inherit; font-size: smaller}
  48. pre.smalllisp {font-size: smaller}
  49. span.nocodebreak {white-space:nowrap}
  50. span.nolinebreak {white-space:nowrap}
  51. span.roman {font-family:serif; font-weight:normal}
  52. span.sansserif {font-family:sans-serif; font-weight:normal}
  53. ul.no-bullet {list-style: none}
  54. -->
  55. </style>
  56. </head>
  57. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  58. <a name="MEMORY"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="PHDRS.html#PHDRS" accesskey="n" rel="next">PHDRS</a>, Previous: <a href="SECTIONS.html#SECTIONS" accesskey="p" rel="prev">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>
  62. </div>
  63. <hr>
  64. <a name="MEMORY-Command"></a>
  65. <h3 class="section">3.7 MEMORY Command</h3>
  66. <a name="index-MEMORY"></a>
  67. <a name="index-memory-regions"></a>
  68. <a name="index-regions-of-memory"></a>
  69. <a name="index-allocating-memory"></a>
  70. <a name="index-discontinuous-memory"></a>
  71. <p>The linker&rsquo;s default configuration permits allocation of all available
  72. memory. You can override this by using the <code>MEMORY</code> command.
  73. </p>
  74. <p>The <code>MEMORY</code> command describes the location and size of blocks of
  75. memory in the target. You can use it to describe which memory regions
  76. may be used by the linker, and which memory regions it must avoid. You
  77. can then assign sections to particular memory regions. The linker will
  78. set section addresses based on the memory regions, and will warn about
  79. regions that become too full. The linker will not shuffle sections
  80. around to fit into the available regions.
  81. </p>
  82. <p>A linker script may contain many uses of the <code>MEMORY</code> command,
  83. however, all memory blocks defined are treated as if they were
  84. specified inside a single <code>MEMORY</code> command. The syntax for
  85. <code>MEMORY</code> is:
  86. </p><div class="smallexample">
  87. <pre class="smallexample">MEMORY
  88. {
  89. <var>name</var> [(<var>attr</var>)] : ORIGIN = <var>origin</var>, LENGTH = <var>len</var>
  90. &hellip;
  91. }
  92. </pre></div>
  93. <p>The <var>name</var> is a name used in the linker script to refer to the
  94. region. The region name has no meaning outside of the linker script.
  95. Region names are stored in a separate name space, and will not conflict
  96. with symbol names, file names, or section names. Each memory region
  97. must have a distinct name within the <code>MEMORY</code> command. However you can
  98. add later alias names to existing memory regions with the <a href="REGION_005fALIAS.html#REGION_005fALIAS">REGION_ALIAS</a>
  99. command.
  100. </p>
  101. <a name="index-memory-region-attributes"></a>
  102. <p>The <var>attr</var> string is an optional list of attributes that specify
  103. whether to use a particular memory region for an input section which is
  104. not explicitly mapped in the linker script. As described in
  105. <a href="SECTIONS.html#SECTIONS">SECTIONS</a>, if you do not specify an output section for some input
  106. section, the linker will create an output section with the same name as
  107. the input section. If you define region attributes, the linker will use
  108. them to select the memory region for the output section that it creates.
  109. </p>
  110. <p>The <var>attr</var> string must consist only of the following characters:
  111. </p><dl compact="compact">
  112. <dt>&lsquo;<samp>R</samp>&rsquo;</dt>
  113. <dd><p>Read-only section
  114. </p></dd>
  115. <dt>&lsquo;<samp>W</samp>&rsquo;</dt>
  116. <dd><p>Read/write section
  117. </p></dd>
  118. <dt>&lsquo;<samp>X</samp>&rsquo;</dt>
  119. <dd><p>Executable section
  120. </p></dd>
  121. <dt>&lsquo;<samp>A</samp>&rsquo;</dt>
  122. <dd><p>Allocatable section
  123. </p></dd>
  124. <dt>&lsquo;<samp>I</samp>&rsquo;</dt>
  125. <dd><p>Initialized section
  126. </p></dd>
  127. <dt>&lsquo;<samp>L</samp>&rsquo;</dt>
  128. <dd><p>Same as &lsquo;<samp>I</samp>&rsquo;
  129. </p></dd>
  130. <dt>&lsquo;<samp>!</samp>&rsquo;</dt>
  131. <dd><p>Invert the sense of any of the attributes that follow
  132. </p></dd>
  133. </dl>
  134. <p>If a unmapped section matches any of the listed attributes other than
  135. &lsquo;<samp>!</samp>&rsquo;, it will be placed in the memory region. The &lsquo;<samp>!</samp>&rsquo;
  136. attribute reverses this test, so that an unmapped section will be placed
  137. in the memory region only if it does not match any of the listed
  138. attributes.
  139. </p>
  140. <a name="index-ORIGIN-_003d"></a>
  141. <a name="index-o-_003d"></a>
  142. <a name="index-org-_003d"></a>
  143. <p>The <var>origin</var> is an numerical expression for the start address of
  144. the memory region. The expression must evaluate to a constant and it
  145. cannot involve any symbols. The keyword <code>ORIGIN</code> may be
  146. abbreviated to <code>org</code> or <code>o</code> (but not, for example,
  147. <code>ORG</code>).
  148. </p>
  149. <a name="index-LENGTH-_003d"></a>
  150. <a name="index-len-_003d"></a>
  151. <a name="index-l-_003d"></a>
  152. <p>The <var>len</var> is an expression for the size in bytes of the memory
  153. region. As with the <var>origin</var> expression, the expression must
  154. be numerical only and must evaluate to a constant. The keyword
  155. <code>LENGTH</code> may be abbreviated to <code>len</code> or <code>l</code>.
  156. </p>
  157. <p>In the following example, we specify that there are two memory regions
  158. available for allocation: one starting at &lsquo;<samp>0</samp>&rsquo; for 256 kilobytes,
  159. and the other starting at &lsquo;<samp>0x40000000</samp>&rsquo; for four megabytes. The
  160. linker will place into the &lsquo;<samp>rom</samp>&rsquo; memory region every section which
  161. is not explicitly mapped into a memory region, and is either read-only
  162. or executable. The linker will place other sections which are not
  163. explicitly mapped into a memory region into the &lsquo;<samp>ram</samp>&rsquo; memory
  164. region.
  165. </p>
  166. <div class="smallexample">
  167. <pre class="smallexample">MEMORY
  168. {
  169. rom (rx) : ORIGIN = 0, LENGTH = 256K
  170. ram (!rx) : org = 0x40000000, l = 4M
  171. }
  172. </pre></div>
  173. <p>Once you define a memory region, you can direct the linker to place
  174. specific output sections into that memory region by using the
  175. &lsquo;<samp>&gt;<var>region</var></samp>&rsquo; output section attribute. For example, if you have
  176. a memory region named &lsquo;<samp>mem</samp>&rsquo;, you would use &lsquo;<samp>&gt;mem</samp>&rsquo; in the
  177. output section definition. See <a href="Output-Section-Region.html#Output-Section-Region">Output Section Region</a>. If no address
  178. was specified for the output section, the linker will set the address to
  179. the next available address within the memory region. If the combined
  180. output sections directed to a memory region are too large for the
  181. region, the linker will issue an error message.
  182. </p>
  183. <p>It is possible to access the origin and length of a memory in an
  184. expression via the <code>ORIGIN(<var>memory</var>)</code> and
  185. <code>LENGTH(<var>memory</var>)</code> functions:
  186. </p>
  187. <div class="smallexample">
  188. <pre class="smallexample"> _fstack = ORIGIN(ram) + LENGTH(ram) - 4;
  189. </pre></div>
  190. <hr>
  191. <div class="header">
  192. <p>
  193. Next: <a href="PHDRS.html#PHDRS" accesskey="n" rel="next">PHDRS</a>, Previous: <a href="SECTIONS.html#SECTIONS" accesskey="p" rel="prev">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>
  194. </div>
  195. </body>
  196. </html>