|
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <!-- This file documents the GNU linker LD
- (GNU Arm Embedded Toolchain 10-2020-q4-major)
- version 2.35.1.
-
- Copyright (C) 1991-2020 Free Software Foundation, Inc.
-
- Permission is granted to copy, distribute and/or modify this document
- under the terms of the GNU Free Documentation License, Version 1.3
- or any later version published by the Free Software Foundation;
- with no Invariant Sections, with no Front-Cover Texts, and with no
- Back-Cover Texts. A copy of the license is included in the
- section entitled "GNU Free Documentation License". -->
- <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <title>Source Code Reference (LD)</title>
-
- <meta name="description" content="Source Code Reference (LD)">
- <meta name="keywords" content="Source Code Reference (LD)">
- <meta name="resource-type" content="document">
- <meta name="distribution" content="global">
- <meta name="Generator" content="makeinfo">
- <link href="index.html#Top" rel="start" title="Top">
- <link href="LD-Index.html#LD-Index" rel="index" title="LD Index">
- <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
- <link href="Assignments.html#Assignments" rel="up" title="Assignments">
- <link href="SECTIONS.html#SECTIONS" rel="next" title="SECTIONS">
- <link href="PROVIDE_005fHIDDEN.html#PROVIDE_005fHIDDEN" rel="prev" title="PROVIDE_HIDDEN">
- <style type="text/css">
- <!--
- a.summary-letter {text-decoration: none}
- blockquote.indentedblock {margin-right: 0em}
- blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
- blockquote.smallquotation {font-size: smaller}
- div.display {margin-left: 3.2em}
- div.example {margin-left: 3.2em}
- div.lisp {margin-left: 3.2em}
- div.smalldisplay {margin-left: 3.2em}
- div.smallexample {margin-left: 3.2em}
- div.smalllisp {margin-left: 3.2em}
- kbd {font-style: oblique}
- pre.display {font-family: inherit}
- pre.format {font-family: inherit}
- pre.menu-comment {font-family: serif}
- pre.menu-preformatted {font-family: serif}
- pre.smalldisplay {font-family: inherit; font-size: smaller}
- pre.smallexample {font-size: smaller}
- pre.smallformat {font-family: inherit; font-size: smaller}
- pre.smalllisp {font-size: smaller}
- span.nolinebreak {white-space: nowrap}
- span.roman {font-family: initial; font-weight: normal}
- span.sansserif {font-family: sans-serif; font-weight: normal}
- ul.no-bullet {list-style: none}
- -->
- </style>
-
-
- </head>
-
- <body lang="en">
- <a name="Source-Code-Reference"></a>
- <div class="header">
- <p>
- 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> [<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>
- </div>
- <hr>
- <a name="Source-Code-Reference-1"></a>
- <h4 class="subsection">3.5.5 Source Code Reference</h4>
-
- <p>Accessing a linker script defined variable from source code is not
- intuitive. In particular a linker script symbol is not equivalent to
- a variable declaration in a high level language, it is instead a
- symbol that does not have a value.
- </p>
- <p>Before going further, it is important to note that compilers often
- transform names in the source code into different names when they are
- stored in the symbol table. For example, Fortran compilers commonly
- prepend or append an underscore, and C++ performs extensive ‘<samp>name
- mangling</samp>’. Therefore there might be a discrepancy between the name
- of a variable as it is used in source code and the name of the same
- variable as it is defined in a linker script. For example in C a
- linker script variable might be referred to as:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> extern int foo;
- </pre></div>
-
- <p>But in the linker script it might be defined as:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> _foo = 1000;
- </pre></div>
-
- <p>In the remaining examples however it is assumed that no name
- transformation has taken place.
- </p>
- <p>When a symbol is declared in a high level language such as C, two
- things happen. The first is that the compiler reserves enough space
- in the program’s memory to hold the <em>value</em> of the symbol. The
- second is that the compiler creates an entry in the program’s symbol
- table which holds the symbol’s <em>address</em>. ie the symbol table
- contains the address of the block of memory holding the symbol’s
- value. So for example the following C declaration, at file scope:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> int foo = 1000;
- </pre></div>
-
- <p>creates an entry called ‘<samp>foo</samp>’ in the symbol table. This entry
- holds the address of an ‘<samp>int</samp>’ sized block of memory where the
- number 1000 is initially stored.
- </p>
- <p>When a program references a symbol the compiler generates code that
- first accesses the symbol table to find the address of the symbol’s
- memory block and then code to read the value from that memory block.
- So:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> foo = 1;
- </pre></div>
-
- <p>looks up the symbol ‘<samp>foo</samp>’ in the symbol table, gets the address
- associated with this symbol and then writes the value 1 into that
- address. Whereas:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> int * a = & foo;
- </pre></div>
-
- <p>looks up the symbol ‘<samp>foo</samp>’ in the symbol table, gets its address
- and then copies this address into the block of memory associated with
- the variable ‘<samp>a</samp>’.
- </p>
- <p>Linker scripts symbol declarations, by contrast, create an entry in
- the symbol table but do not assign any memory to them. Thus they are
- an address without a value. So for example the linker script definition:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> foo = 1000;
- </pre></div>
-
- <p>creates an entry in the symbol table called ‘<samp>foo</samp>’ which holds
- the address of memory location 1000, but nothing special is stored at
- address 1000. This means that you cannot access the <em>value</em> of a
- linker script defined symbol - it has no value - all you can do is
- access the <em>address</em> of a linker script defined symbol.
- </p>
- <p>Hence when you are using a linker script defined symbol in source code
- you should always take the address of the symbol, and never attempt to
- use its value. For example suppose you want to copy the contents of a
- section of memory called .ROM into a section called .FLASH and the
- linker script contains these declarations:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> start_of_ROM = .ROM;
- end_of_ROM = .ROM + sizeof (.ROM);
- start_of_FLASH = .FLASH;
- </pre></div>
-
- <p>Then the C source code to perform the copy would be:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> extern char start_of_ROM, end_of_ROM, start_of_FLASH;
-
- memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
- </pre></div>
-
- <p>Note the use of the ‘<samp>&</samp>’ operators. These are correct.
- Alternatively the symbols can be treated as the names of vectors or
- arrays and then the code will again work as expected:
- </p>
- <div class="smallexample">
- <pre class="smallexample"> extern char start_of_ROM[], end_of_ROM[], start_of_FLASH[];
-
- memcpy (start_of_FLASH, start_of_ROM, end_of_ROM - start_of_ROM);
- </pre></div>
-
- <p>Note how using this method does not require the use of ‘<samp>&</samp>’
- operators.
- </p>
- <hr>
- <div class="header">
- <p>
- 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> [<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>
- </div>
-
-
-
- </body>
- </html>
|