1<html lang="en"> 2<head> 3<title>Source Code Reference - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Assignments.html#Assignments" title="Assignments"> 9<link rel="prev" href="PROVIDE_005fHIDDEN.html#PROVIDE_005fHIDDEN" title="PROVIDE_HIDDEN"> 10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 11<!-- 12This file documents the GNU linker LD 13(Sourcery CodeBench Lite 2011.09-69) 14version 2.21.53. 15 16Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 172001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. 18 19Permission is granted to copy, distribute and/or modify this document 20under the terms of the GNU Free Documentation License, Version 1.3 21or any later version published by the Free Software Foundation; 22with no Invariant Sections, with no Front-Cover Texts, and with no 23Back-Cover Texts. A copy of the license is included in the 24section entitled ``GNU Free Documentation License''.--> 25<meta http-equiv="Content-Style-Type" content="text/css"> 26<style type="text/css"><!-- 27 pre.display { font-family:inherit } 28 pre.format { font-family:inherit } 29 pre.smalldisplay { font-family:inherit; font-size:smaller } 30 pre.smallformat { font-family:inherit; font-size:smaller } 31 pre.smallexample { font-size:smaller } 32 pre.smalllisp { font-size:smaller } 33 span.sc { font-variant:small-caps } 34 span.roman { font-family:serif; font-weight:normal; } 35 span.sansserif { font-family:sans-serif; font-weight:normal; } 36--></style> 37<link rel="stylesheet" type="text/css" href="../cs.css"> 38</head> 39<body> 40<div class="node"> 41<a name="Source-Code-Reference"></a> 42<p> 43Previous: <a rel="previous" accesskey="p" href="PROVIDE_005fHIDDEN.html#PROVIDE_005fHIDDEN">PROVIDE_HIDDEN</a>, 44Up: <a rel="up" accesskey="u" href="Assignments.html#Assignments">Assignments</a> 45<hr> 46</div> 47 48<h4 class="subsection">3.5.4 Source Code Reference</h4> 49 50<p>Accessing a linker script defined variable from source code is not 51intuitive. In particular a linker script symbol is not equivalent to 52a variable declaration in a high level language, it is instead a 53symbol that does not have a value. 54 55 <p>Before going further, it is important to note that compilers often 56transform names in the source code into different names when they are 57stored in the symbol table. For example, Fortran compilers commonly 58prepend or append an underscore, and C++ performs extensive ‘<samp><span class="samp">name 59mangling</span></samp>’. Therefore there might be a discrepancy between the name 60of a variable as it is used in source code and the name of the same 61variable as it is defined in a linker script. For example in C a 62linker script variable might be referred to as: 63 64<pre class="smallexample"> extern int foo; 65</pre> 66 <p>But in the linker script it might be defined as: 67 68<pre class="smallexample"> _foo = 1000; 69</pre> 70 <p>In the remaining examples however it is assumed that no name 71transformation has taken place. 72 73 <p>When a symbol is declared in a high level language such as C, two 74things happen. The first is that the compiler reserves enough space 75in the program's memory to hold the <em>value</em> of the symbol. The 76second is that the compiler creates an entry in the program's symbol 77table which holds the symbol's <em>address</em>. ie the symbol table 78contains the address of the block of memory holding the symbol's 79value. So for example the following C declaration, at file scope: 80 81<pre class="smallexample"> int foo = 1000; 82</pre> 83 <p>creates a entry called ‘<samp><span class="samp">foo</span></samp>’ in the symbol table. This entry 84holds the address of an ‘<samp><span class="samp">int</span></samp>’ sized block of memory where the 85number 1000 is initially stored. 86 87 <p>When a program references a symbol the compiler generates code that 88first accesses the symbol table to find the address of the symbol's 89memory block and then code to read the value from that memory block. 90So: 91 92<pre class="smallexample"> foo = 1; 93</pre> 94 <p>looks up the symbol ‘<samp><span class="samp">foo</span></samp>’ in the symbol table, gets the address 95associated with this symbol and then writes the value 1 into that 96address. Whereas: 97 98<pre class="smallexample"> int * a = & foo; 99</pre> 100 <p>looks up the symbol ‘<samp><span class="samp">foo</span></samp>’ in the symbol table, gets it address 101and then copies this address into the block of memory associated with 102the variable ‘<samp><span class="samp">a</span></samp>’. 103 104 <p>Linker scripts symbol declarations, by contrast, create an entry in 105the symbol table but do not assign any memory to them. Thus they are 106an address without a value. So for example the linker script definition: 107 108<pre class="smallexample"> foo = 1000; 109</pre> 110 <p>creates an entry in the symbol table called ‘<samp><span class="samp">foo</span></samp>’ which holds 111the address of memory location 1000, but nothing special is stored at 112address 1000. This means that you cannot access the <em>value</em> of a 113linker script defined symbol - it has no value - all you can do is 114access the <em>address</em> of a linker script defined symbol. 115 116 <p>Hence when you are using a linker script defined symbol in source code 117you should always take the address of the symbol, and never attempt to 118use its value. For example suppose you want to copy the contents of a 119section of memory called .ROM into a section called .FLASH and the 120linker script contains these declarations: 121 122<pre class="smallexample"> start_of_ROM = .ROM; 123 end_of_ROM = .ROM + sizeof (.ROM) - 1; 124 start_of_FLASH = .FLASH; 125</pre> 126 <p>Then the C source code to perform the copy would be: 127 128<pre class="smallexample"> extern char start_of_ROM, end_of_ROM, start_of_FLASH; 129 130 memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM); 131</pre> 132 <p>Note the use of the ‘<samp><span class="samp">&</span></samp>’ operators. These are correct. 133 134 </body></html> 135 136