ld: Source Code Reference
1
1 3.5.5 Source Code Reference
1 ---------------------------
1
1 Accessing a linker script defined variable from source code is not
1 intuitive. In particular a linker script symbol is not equivalent to a
1 variable declaration in a high level language, it is instead a symbol
1 that does not have a value.
1
1 Before going further, it is important to note that compilers often
1 transform names in the source code into different names when they are
1 stored in the symbol table. For example, Fortran compilers commonly
1 prepend or append an underscore, and C++ performs extensive 'name
1 mangling'. Therefore there might be a discrepancy between the name of a
1 variable as it is used in source code and the name of the same variable
1 as it is defined in a linker script. For example in C a linker script
1 variable might be referred to as:
1
1 extern int foo;
1
1 But in the linker script it might be defined as:
1
1 _foo = 1000;
1
1 In the remaining examples however it is assumed that no name
1 transformation has taken place.
1
1 When a symbol is declared in a high level language such as C, two
1 things happen. The first is that the compiler reserves enough space in
1 the program's memory to hold the _value_ of the symbol. The second is
1 that the compiler creates an entry in the program's symbol table which
1 holds the symbol's _address_. ie the symbol table contains the address
1 of the block of memory holding the symbol's value. So for example the
1 following C declaration, at file scope:
1
1 int foo = 1000;
1
1 creates an entry called 'foo' in the symbol table. This entry holds
1 the address of an 'int' sized block of memory where the number 1000 is
1 initially stored.
1
1 When a program references a symbol the compiler generates code that
1 first accesses the symbol table to find the address of the symbol's
1 memory block and then code to read the value from that memory block.
1 So:
1
1 foo = 1;
1
1 looks up the symbol 'foo' in the symbol table, gets the address
1 associated with this symbol and then writes the value 1 into that
1 address. Whereas:
1
1 int * a = & foo;
1
1 looks up the symbol 'foo' in the symbol table, gets its address and
1 then copies this address into the block of memory associated with the
1 variable 'a'.
1
1 Linker scripts symbol declarations, by contrast, create an entry in
1 the symbol table but do not assign any memory to them. Thus they are an
1 address without a value. So for example the linker script definition:
1
1 foo = 1000;
1
1 creates an entry in the symbol table called 'foo' which holds the
1 address of memory location 1000, but nothing special is stored at
1 address 1000. This means that you cannot access the _value_ of a linker
1 script defined symbol - it has no value - all you can do is access the
1 _address_ of a linker script defined symbol.
1
1 Hence when you are using a linker script defined symbol in source
1 code you should always take the address of the symbol, and never attempt
1 to use its value. For example suppose you want to copy the contents of
1 a section of memory called .ROM into a section called .FLASH and the
1 linker script contains these declarations:
1
1 start_of_ROM = .ROM;
1 end_of_ROM = .ROM + sizeof (.ROM);
1 start_of_FLASH = .FLASH;
1
1 Then the C source code to perform the copy would be:
1
1 extern char start_of_ROM, end_of_ROM, start_of_FLASH;
1
1 memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
1
1 Note the use of the '&' operators. These are correct. Alternatively
1 the symbols can be treated as the names of vectors or arrays and then
1 the code will again work as expected:
1
1 extern char start_of_ROM[], end_of_ROM[], start_of_FLASH[];
1
1 memcpy (start_of_FLASH, start_of_ROM, end_of_ROM - start_of_ROM);
1
1 Note how using this method does not require the use of '&' operators.
1