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