Source-Code-Reference.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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: Source Code Reference</title>
  16. <meta name="description" content="LD: Source Code Reference">
  17. <meta name="keywords" content="LD: Source Code Reference">
  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="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.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="Source-Code-Reference"></a>
  59. <div class="header">
  60. <p>
  61. 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>
  62. </div>
  63. <hr>
  64. <a name="Source-Code-Reference-1"></a>
  65. <h4 class="subsection">3.5.5 Source Code Reference</h4>
  66. <p>Accessing a linker script defined variable from source code is not
  67. intuitive. In particular a linker script symbol is not equivalent to
  68. a variable declaration in a high level language, it is instead a
  69. symbol that does not have a value.
  70. </p>
  71. <p>Before going further, it is important to note that compilers often
  72. transform names in the source code into different names when they are
  73. stored in the symbol table. For example, Fortran compilers commonly
  74. prepend or append an underscore, and C++ performs extensive &lsquo;<samp>name
  75. mangling</samp>&rsquo;. Therefore there might be a discrepancy between the name
  76. of a variable as it is used in source code and the name of the same
  77. variable as it is defined in a linker script. For example in C a
  78. linker script variable might be referred to as:
  79. </p>
  80. <div class="smallexample">
  81. <pre class="smallexample"> extern int foo;
  82. </pre></div>
  83. <p>But in the linker script it might be defined as:
  84. </p>
  85. <div class="smallexample">
  86. <pre class="smallexample"> _foo = 1000;
  87. </pre></div>
  88. <p>In the remaining examples however it is assumed that no name
  89. transformation has taken place.
  90. </p>
  91. <p>When a symbol is declared in a high level language such as C, two
  92. things happen. The first is that the compiler reserves enough space
  93. in the program&rsquo;s memory to hold the <em>value</em> of the symbol. The
  94. second is that the compiler creates an entry in the program&rsquo;s symbol
  95. table which holds the symbol&rsquo;s <em>address</em>. ie the symbol table
  96. contains the address of the block of memory holding the symbol&rsquo;s
  97. value. So for example the following C declaration, at file scope:
  98. </p>
  99. <div class="smallexample">
  100. <pre class="smallexample"> int foo = 1000;
  101. </pre></div>
  102. <p>creates an entry called &lsquo;<samp>foo</samp>&rsquo; in the symbol table. This entry
  103. holds the address of an &lsquo;<samp>int</samp>&rsquo; sized block of memory where the
  104. number 1000 is initially stored.
  105. </p>
  106. <p>When a program references a symbol the compiler generates code that
  107. first accesses the symbol table to find the address of the symbol&rsquo;s
  108. memory block and then code to read the value from that memory block.
  109. So:
  110. </p>
  111. <div class="smallexample">
  112. <pre class="smallexample"> foo = 1;
  113. </pre></div>
  114. <p>looks up the symbol &lsquo;<samp>foo</samp>&rsquo; in the symbol table, gets the address
  115. associated with this symbol and then writes the value 1 into that
  116. address. Whereas:
  117. </p>
  118. <div class="smallexample">
  119. <pre class="smallexample"> int * a = &amp; foo;
  120. </pre></div>
  121. <p>looks up the symbol &lsquo;<samp>foo</samp>&rsquo; in the symbol table, gets its address
  122. and then copies this address into the block of memory associated with
  123. the variable &lsquo;<samp>a</samp>&rsquo;.
  124. </p>
  125. <p>Linker scripts symbol declarations, by contrast, create an entry in
  126. the symbol table but do not assign any memory to them. Thus they are
  127. an address without a value. So for example the linker script definition:
  128. </p>
  129. <div class="smallexample">
  130. <pre class="smallexample"> foo = 1000;
  131. </pre></div>
  132. <p>creates an entry in the symbol table called &lsquo;<samp>foo</samp>&rsquo; which holds
  133. the address of memory location 1000, but nothing special is stored at
  134. address 1000. This means that you cannot access the <em>value</em> of a
  135. linker script defined symbol - it has no value - all you can do is
  136. access the <em>address</em> of a linker script defined symbol.
  137. </p>
  138. <p>Hence when you are using a linker script defined symbol in source code
  139. you should always take the address of the symbol, and never attempt to
  140. use its value. For example suppose you want to copy the contents of a
  141. section of memory called .ROM into a section called .FLASH and the
  142. linker script contains these declarations:
  143. </p>
  144. <div class="smallexample">
  145. <pre class="smallexample"> start_of_ROM = .ROM;
  146. end_of_ROM = .ROM + sizeof (.ROM);
  147. start_of_FLASH = .FLASH;
  148. </pre></div>
  149. <p>Then the C source code to perform the copy would be:
  150. </p>
  151. <div class="smallexample">
  152. <pre class="smallexample"> extern char start_of_ROM, end_of_ROM, start_of_FLASH;
  153. memcpy (&amp; start_of_FLASH, &amp; start_of_ROM, &amp; end_of_ROM - &amp; start_of_ROM);
  154. </pre></div>
  155. <p>Note the use of the &lsquo;<samp>&amp;</samp>&rsquo; operators. These are correct.
  156. Alternatively the symbols can be treated as the names of vectors or
  157. arrays and then the code will again work as expected:
  158. </p>
  159. <div class="smallexample">
  160. <pre class="smallexample"> extern char start_of_ROM[], end_of_ROM[], start_of_FLASH[];
  161. memcpy (start_of_FLASH, start_of_ROM, end_of_ROM - start_of_ROM);
  162. </pre></div>
  163. <p>Note how using this method does not require the use of &lsquo;<samp>&amp;</samp>&rsquo;
  164. operators.
  165. </p>
  166. <hr>
  167. <div class="header">
  168. <p>
  169. 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>
  170. </div>
  171. </body>
  172. </html>