REGION_005fALIAS.html 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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: REGION_ALIAS</title>
  16. <meta name="description" content="LD: REGION_ALIAS">
  17. <meta name="keywords" content="LD: REGION_ALIAS">
  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="Simple-Commands.html#Simple-Commands" rel="up" title="Simple Commands">
  26. <link href="Miscellaneous-Commands.html#Miscellaneous-Commands" rel="next" title="Miscellaneous Commands">
  27. <link href="Format-Commands.html#Format-Commands" rel="prev" title="Format Commands">
  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="REGION_005fALIAS"></a>
  59. <div class="header">
  60. <p>
  61. Next: <a href="Miscellaneous-Commands.html#Miscellaneous-Commands" accesskey="n" rel="next">Miscellaneous Commands</a>, Previous: <a href="Format-Commands.html#Format-Commands" accesskey="p" rel="prev">Format Commands</a>, Up: <a href="Simple-Commands.html#Simple-Commands" accesskey="u" rel="up">Simple Commands</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="Assign-alias-names-to-memory-regions"></a>
  65. <h4 class="subsection">3.4.4 Assign alias names to memory regions</h4>
  66. <a name="index-REGION_005fALIAS_0028alias_002c-region_0029"></a>
  67. <a name="index-region-alias"></a>
  68. <a name="index-region-names"></a>
  69. <p>Alias names can be added to existing memory regions created with the
  70. <a href="MEMORY.html#MEMORY">MEMORY</a> command. Each name corresponds to at most one memory region.
  71. </p>
  72. <div class="smallexample">
  73. <pre class="smallexample">REGION_ALIAS(<var>alias</var>, <var>region</var>)
  74. </pre></div>
  75. <p>The <code>REGION_ALIAS</code> function creates an alias name <var>alias</var> for the
  76. memory region <var>region</var>. This allows a flexible mapping of output sections
  77. to memory regions. An example follows.
  78. </p>
  79. <p>Suppose we have an application for embedded systems which come with various
  80. memory storage devices. All have a general purpose, volatile memory <code>RAM</code>
  81. that allows code execution or data storage. Some may have a read-only,
  82. non-volatile memory <code>ROM</code> that allows code execution and read-only data
  83. access. The last variant is a read-only, non-volatile memory <code>ROM2</code> with
  84. read-only data access and no code execution capability. We have four output
  85. sections:
  86. </p>
  87. <ul>
  88. <li> <code>.text</code> program code;
  89. </li><li> <code>.rodata</code> read-only data;
  90. </li><li> <code>.data</code> read-write initialized data;
  91. </li><li> <code>.bss</code> read-write zero initialized data.
  92. </li></ul>
  93. <p>The goal is to provide a linker command file that contains a system independent
  94. part defining the output sections and a system dependent part mapping the
  95. output sections to the memory regions available on the system. Our embedded
  96. systems come with three different memory setups <code>A</code>, <code>B</code> and
  97. <code>C</code>:
  98. </p><table>
  99. <tr><td width="25%">Section</td><td width="25%">Variant A</td><td width="25%">Variant B</td><td width="25%">Variant C</td></tr>
  100. <tr><td width="25%">.text</td><td width="25%">RAM</td><td width="25%">ROM</td><td width="25%">ROM</td></tr>
  101. <tr><td width="25%">.rodata</td><td width="25%">RAM</td><td width="25%">ROM</td><td width="25%">ROM2</td></tr>
  102. <tr><td width="25%">.data</td><td width="25%">RAM</td><td width="25%">RAM/ROM</td><td width="25%">RAM/ROM2</td></tr>
  103. <tr><td width="25%">.bss</td><td width="25%">RAM</td><td width="25%">RAM</td><td width="25%">RAM</td></tr>
  104. </table>
  105. <p>The notation <code>RAM/ROM</code> or <code>RAM/ROM2</code> means that this section is
  106. loaded into region <code>ROM</code> or <code>ROM2</code> respectively. Please note that
  107. the load address of the <code>.data</code> section starts in all three variants at
  108. the end of the <code>.rodata</code> section.
  109. </p>
  110. <p>The base linker script that deals with the output sections follows. It
  111. includes the system dependent <code>linkcmds.memory</code> file that describes the
  112. memory layout:
  113. </p><div class="smallexample">
  114. <pre class="smallexample">INCLUDE linkcmds.memory
  115. SECTIONS
  116. {
  117. .text :
  118. {
  119. *(.text)
  120. } &gt; REGION_TEXT
  121. .rodata :
  122. {
  123. *(.rodata)
  124. rodata_end = .;
  125. } &gt; REGION_RODATA
  126. .data : AT (rodata_end)
  127. {
  128. data_start = .;
  129. *(.data)
  130. } &gt; REGION_DATA
  131. data_size = SIZEOF(.data);
  132. data_load_start = LOADADDR(.data);
  133. .bss :
  134. {
  135. *(.bss)
  136. } &gt; REGION_BSS
  137. }
  138. </pre></div>
  139. <p>Now we need three different <code>linkcmds.memory</code> files to define memory
  140. regions and alias names. The content of <code>linkcmds.memory</code> for the three
  141. variants <code>A</code>, <code>B</code> and <code>C</code>:
  142. </p><dl compact="compact">
  143. <dt><code>A</code></dt>
  144. <dd><p>Here everything goes into the <code>RAM</code>.
  145. </p><div class="smallexample">
  146. <pre class="smallexample">MEMORY
  147. {
  148. RAM : ORIGIN = 0, LENGTH = 4M
  149. }
  150. REGION_ALIAS(&quot;REGION_TEXT&quot;, RAM);
  151. REGION_ALIAS(&quot;REGION_RODATA&quot;, RAM);
  152. REGION_ALIAS(&quot;REGION_DATA&quot;, RAM);
  153. REGION_ALIAS(&quot;REGION_BSS&quot;, RAM);
  154. </pre></div>
  155. </dd>
  156. <dt><code>B</code></dt>
  157. <dd><p>Program code and read-only data go into the <code>ROM</code>. Read-write data goes
  158. into the <code>RAM</code>. An image of the initialized data is loaded into the
  159. <code>ROM</code> and will be copied during system start into the <code>RAM</code>.
  160. </p><div class="smallexample">
  161. <pre class="smallexample">MEMORY
  162. {
  163. ROM : ORIGIN = 0, LENGTH = 3M
  164. RAM : ORIGIN = 0x10000000, LENGTH = 1M
  165. }
  166. REGION_ALIAS(&quot;REGION_TEXT&quot;, ROM);
  167. REGION_ALIAS(&quot;REGION_RODATA&quot;, ROM);
  168. REGION_ALIAS(&quot;REGION_DATA&quot;, RAM);
  169. REGION_ALIAS(&quot;REGION_BSS&quot;, RAM);
  170. </pre></div>
  171. </dd>
  172. <dt><code>C</code></dt>
  173. <dd><p>Program code goes into the <code>ROM</code>. Read-only data goes into the
  174. <code>ROM2</code>. Read-write data goes into the <code>RAM</code>. An image of the
  175. initialized data is loaded into the <code>ROM2</code> and will be copied during
  176. system start into the <code>RAM</code>.
  177. </p><div class="smallexample">
  178. <pre class="smallexample">MEMORY
  179. {
  180. ROM : ORIGIN = 0, LENGTH = 2M
  181. ROM2 : ORIGIN = 0x10000000, LENGTH = 1M
  182. RAM : ORIGIN = 0x20000000, LENGTH = 1M
  183. }
  184. REGION_ALIAS(&quot;REGION_TEXT&quot;, ROM);
  185. REGION_ALIAS(&quot;REGION_RODATA&quot;, ROM2);
  186. REGION_ALIAS(&quot;REGION_DATA&quot;, RAM);
  187. REGION_ALIAS(&quot;REGION_BSS&quot;, RAM);
  188. </pre></div>
  189. </dd>
  190. </dl>
  191. <p>It is possible to write a common system initialization routine to copy the
  192. <code>.data</code> section from <code>ROM</code> or <code>ROM2</code> into the <code>RAM</code> if
  193. necessary:
  194. </p><div class="smallexample">
  195. <pre class="smallexample">#include &lt;string.h&gt;
  196. extern char data_start [];
  197. extern char data_size [];
  198. extern char data_load_start [];
  199. void copy_data(void)
  200. {
  201. if (data_start != data_load_start)
  202. {
  203. memcpy(data_start, data_load_start, (size_t) data_size);
  204. }
  205. }
  206. </pre></div>
  207. <hr>
  208. <div class="header">
  209. <p>
  210. Next: <a href="Miscellaneous-Commands.html#Miscellaneous-Commands" accesskey="n" rel="next">Miscellaneous Commands</a>, Previous: <a href="Format-Commands.html#Format-Commands" accesskey="p" rel="prev">Format Commands</a>, Up: <a href="Simple-Commands.html#Simple-Commands" accesskey="u" rel="up">Simple Commands</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>
  211. </div>
  212. </body>
  213. </html>