Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

193 lines
8.0KB

  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 Arm Embedded Toolchain 10-2020-q4-major)
  5. version 2.35.1.
  6. Copyright (C) 1991-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  14. <head>
  15. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  16. <title>Source Code Reference (LD)</title>
  17. <meta name="description" content="Source Code Reference (LD)">
  18. <meta name="keywords" content="Source Code Reference (LD)">
  19. <meta name="resource-type" content="document">
  20. <meta name="distribution" content="global">
  21. <meta name="Generator" content="makeinfo">
  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="Assignments.html#Assignments" rel="up" title="Assignments">
  26. <link href="SECTIONS.html#SECTIONS" rel="next" title="SECTIONS">
  27. <link href="PROVIDE_005fHIDDEN.html#PROVIDE_005fHIDDEN" rel="prev" title="PROVIDE_HIDDEN">
  28. <style type="text/css">
  29. <!--
  30. a.summary-letter {text-decoration: none}
  31. blockquote.indentedblock {margin-right: 0em}
  32. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  33. blockquote.smallquotation {font-size: smaller}
  34. div.display {margin-left: 3.2em}
  35. div.example {margin-left: 3.2em}
  36. div.lisp {margin-left: 3.2em}
  37. div.smalldisplay {margin-left: 3.2em}
  38. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  50. span.roman {font-family: initial; font-weight: normal}
  51. span.sansserif {font-family: sans-serif; font-weight: normal}
  52. ul.no-bullet {list-style: none}
  53. -->
  54. </style>
  55. </head>
  56. <body lang="en">
  57. <a name="Source-Code-Reference"></a>
  58. <div class="header">
  59. <p>
  60. Previous: <a href="PROVIDE_005fHIDDEN.html#PROVIDE_005fHIDDEN" accesskey="p" rel="prev">PROVIDE_HIDDEN</a>, Up: <a href="Assignments.html#Assignments" accesskey="u" rel="up">Assignments</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>
  61. </div>
  62. <hr>
  63. <a name="Source-Code-Reference-1"></a>
  64. <h4 class="subsection">3.5.5 Source Code Reference</h4>
  65. <p>Accessing a linker script defined variable from source code is not
  66. intuitive. In particular a linker script symbol is not equivalent to
  67. a variable declaration in a high level language, it is instead a
  68. symbol that does not have a value.
  69. </p>
  70. <p>Before going further, it is important to note that compilers often
  71. transform names in the source code into different names when they are
  72. stored in the symbol table. For example, Fortran compilers commonly
  73. prepend or append an underscore, and C++ performs extensive &lsquo;<samp>name
  74. mangling</samp>&rsquo;. Therefore there might be a discrepancy between the name
  75. of a variable as it is used in source code and the name of the same
  76. variable as it is defined in a linker script. For example in C a
  77. linker script variable might be referred to as:
  78. </p>
  79. <div class="smallexample">
  80. <pre class="smallexample"> extern int foo;
  81. </pre></div>
  82. <p>But in the linker script it might be defined as:
  83. </p>
  84. <div class="smallexample">
  85. <pre class="smallexample"> _foo = 1000;
  86. </pre></div>
  87. <p>In the remaining examples however it is assumed that no name
  88. transformation has taken place.
  89. </p>
  90. <p>When a symbol is declared in a high level language such as C, two
  91. things happen. The first is that the compiler reserves enough space
  92. in the program&rsquo;s memory to hold the <em>value</em> of the symbol. The
  93. second is that the compiler creates an entry in the program&rsquo;s symbol
  94. table which holds the symbol&rsquo;s <em>address</em>. ie the symbol table
  95. contains the address of the block of memory holding the symbol&rsquo;s
  96. value. So for example the following C declaration, at file scope:
  97. </p>
  98. <div class="smallexample">
  99. <pre class="smallexample"> int foo = 1000;
  100. </pre></div>
  101. <p>creates an entry called &lsquo;<samp>foo</samp>&rsquo; in the symbol table. This entry
  102. holds the address of an &lsquo;<samp>int</samp>&rsquo; sized block of memory where the
  103. number 1000 is initially stored.
  104. </p>
  105. <p>When a program references a symbol the compiler generates code that
  106. first accesses the symbol table to find the address of the symbol&rsquo;s
  107. memory block and then code to read the value from that memory block.
  108. So:
  109. </p>
  110. <div class="smallexample">
  111. <pre class="smallexample"> foo = 1;
  112. </pre></div>
  113. <p>looks up the symbol &lsquo;<samp>foo</samp>&rsquo; in the symbol table, gets the address
  114. associated with this symbol and then writes the value 1 into that
  115. address. Whereas:
  116. </p>
  117. <div class="smallexample">
  118. <pre class="smallexample"> int * a = &amp; foo;
  119. </pre></div>
  120. <p>looks up the symbol &lsquo;<samp>foo</samp>&rsquo; in the symbol table, gets its address
  121. and then copies this address into the block of memory associated with
  122. the variable &lsquo;<samp>a</samp>&rsquo;.
  123. </p>
  124. <p>Linker scripts symbol declarations, by contrast, create an entry in
  125. the symbol table but do not assign any memory to them. Thus they are
  126. an address without a value. So for example the linker script definition:
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample"> foo = 1000;
  130. </pre></div>
  131. <p>creates an entry in the symbol table called &lsquo;<samp>foo</samp>&rsquo; which holds
  132. the address of memory location 1000, but nothing special is stored at
  133. address 1000. This means that you cannot access the <em>value</em> of a
  134. linker script defined symbol - it has no value - all you can do is
  135. access the <em>address</em> of a linker script defined symbol.
  136. </p>
  137. <p>Hence when you are using a linker script defined symbol in source code
  138. you should always take the address of the symbol, and never attempt to
  139. use its value. For example suppose you want to copy the contents of a
  140. section of memory called .ROM into a section called .FLASH and the
  141. linker script contains these declarations:
  142. </p>
  143. <div class="smallexample">
  144. <pre class="smallexample"> start_of_ROM = .ROM;
  145. end_of_ROM = .ROM + sizeof (.ROM);
  146. start_of_FLASH = .FLASH;
  147. </pre></div>
  148. <p>Then the C source code to perform the copy would be:
  149. </p>
  150. <div class="smallexample">
  151. <pre class="smallexample"> extern char start_of_ROM, end_of_ROM, start_of_FLASH;
  152. memcpy (&amp; start_of_FLASH, &amp; start_of_ROM, &amp; end_of_ROM - &amp; start_of_ROM);
  153. </pre></div>
  154. <p>Note the use of the &lsquo;<samp>&amp;</samp>&rsquo; operators. These are correct.
  155. Alternatively the symbols can be treated as the names of vectors or
  156. arrays and then the code will again work as expected:
  157. </p>
  158. <div class="smallexample">
  159. <pre class="smallexample"> extern char start_of_ROM[], end_of_ROM[], start_of_FLASH[];
  160. memcpy (start_of_FLASH, start_of_ROM, end_of_ROM - start_of_ROM);
  161. </pre></div>
  162. <p>Note how using this method does not require the use of &lsquo;<samp>&amp;</samp>&rsquo;
  163. operators.
  164. </p>
  165. <hr>
  166. <div class="header">
  167. <p>
  168. Previous: <a href="PROVIDE_005fHIDDEN.html#PROVIDE_005fHIDDEN" accesskey="p" rel="prev">PROVIDE_HIDDEN</a>, Up: <a href="Assignments.html#Assignments" accesskey="u" rel="up">Assignments</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>
  169. </div>
  170. </body>
  171. </html>