From: Nick Clifton <nickc@redhat.com>
To: eddy@opera.com
Cc: ian@airs.com, amodra@bigpond.net.au, binutils@sources.redhat.com
Subject: Re: binutils-doc 2.15-5: glitches in ld.info
Date: Tue, 01 Feb 2005 12:34:00 -0000 [thread overview]
Message-ID: <41FF7985.4000807@redhat.com> (raw)
In-Reply-To: <E1CtCyj-0000TM-00@whorl.oslo.opera.com>
[-- Attachment #1: Type: text/plain, Size: 425 bytes --]
Hi Edward,
Thanks for the suggested documentation update. I have taken the liberty
of rewriting it slightly, err, well actually rather a lot, but I think
that I have managed to retain the feel and intent of your original version.
What do you think of this version then ? If you were coming to this
subject for the first time, do you think that this new section would
explain the linker's behaviour ?
Cheers
Nick
[-- Attachment #2: ld.texinfo.patch --]
[-- Type: text/plain, Size: 5308 bytes --]
Index: ld/ld.texinfo
===================================================================
RCS file: /cvs/src/src/ld/ld.texinfo,v
retrieving revision 1.139
diff -c -3 -p -r1.139 ld.texinfo
*** ld/ld.texinfo 1 Feb 2005 01:11:27 -0000 1.139
--- ld/ld.texinfo 1 Feb 2005 12:31:35 -0000
*************** the @samp{-f} option.
*** 2741,2751 ****
@cindex symbol definition, scripts
@cindex variables, defining
You may assign a value to a symbol in a linker script. This will define
! the symbol as a global symbol.
@menu
* Simple Assignments:: Simple Assignments
* PROVIDE:: PROVIDE
@end menu
@node Simple Assignments
--- 2741,2752 ----
@cindex symbol definition, scripts
@cindex variables, defining
You may assign a value to a symbol in a linker script. This will define
! the symbol and place it into the symbol table with a global scope.
@menu
* Simple Assignments:: Simple Assignments
* PROVIDE:: PROVIDE
+ * Source Code Reference:: How to use a linker script defined symbol in source code
@end menu
@node Simple Assignments
*************** underscore), the linker will silently us
*** 2838,2843 ****
--- 2839,2951 ----
If the program references @samp{etext} but does not define it, the
linker will use the definition in the linker script.
+ @node Source Code Reference
+ @subsection Source Code Reference
+
+ 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.
+
+ 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}. 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:
+
+ @smallexample
+ extern int foo;
+ @end smallexample
+
+ But in the linker script it might be defined as:
+
+ @smallexample
+ _foo = 1000;
+ @end smallexample
+
+ In the remaining examples however it is assumed that no name
+ transformation has taken place.
+
+ 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 @emph{value} of the symbol. The
+ second is that the compiler creates an entry in the program's symbol
+ table which holds the symbol's @emph{address}. ie the symbol table
+ contains the address of the block of memory holding the symbol's
+ value. So for example the C declaration:
+
+ @smallexample
+ int foo = 1000;
+ @end smallexample
+
+ Creates a entry called @samp{foo} in the symbol table. This entry
+ holds the address of an @samp{int} sized block of memory where the
+ number 1000 is currently stored.
+
+ 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:
+
+ @smallexample
+ foo = 1;
+ @end smallexample
+
+ Looks up the symbol @samp{foo} in the symbol table, gets the address
+ associated with this symbol and then writes the value 1 into that
+ address. Whereas:
+
+ @smallexample
+ int * a = & foo;
+ @end smallexample
+
+ Looks up the symbol @samp{foo} in the symbol table, gets it address
+ and then copies this address into the block of memory associated with
+ the variable @samp{a}.
+
+ 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:
+
+ @smallexample
+ foo = 1000;
+ @end smallexample
+
+ Creates an entry in the symbol table called @samp{foo} which contains
+ the address of memory location 1000, but nothing special is stored at
+ address 1000. This means that you cannot access the @emph{value} of a
+ linker script defined symbol - it has no value - all you can do is
+ access the @emph{address} of a linker script defined symbol,
+
+ 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:
+
+ @smallexample
+ @group
+ start_of_ROM = .ROM;
+ end_of_ROM = .ROM + sizeof (.ROM) - 1;
+ start_of_FLASH = .FLASH;
+ @end group
+ @end smallexample
+
+ Then the C source code to perform the copy would be:
+
+ @smallexample
+ @group
+ extern char start_of_ROM, end_of_ROM, start_of_FLASH;
+
+ memcpy (& start_of_FLASH, & start_of_ROM, & end_of_ROM - & start_of_ROM);
+ @end group
+ @end smallexample
+
+ Note the use of the @samp{&} operators. These are correct.
+
@node SECTIONS
@section SECTIONS Command
@kindex SECTIONS
next prev parent reply other threads:[~2005-02-01 12:34 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <E1Crf48-0008TJ-00@whorl.oslo.opera.com>
2005-01-23 7:43 ` Alan Modra
2005-01-24 10:53 ` Edward Welbourne
2005-01-24 19:03 ` Ian Lance Taylor
2005-01-24 22:47 ` Edward Welbourne
2005-02-01 12:34 ` Nick Clifton [this message]
2005-02-01 14:49 ` Edward Welbourne
2005-02-01 17:30 ` Nick Clifton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=41FF7985.4000807@redhat.com \
--to=nickc@redhat.com \
--cc=amodra@bigpond.net.au \
--cc=binutils@sources.redhat.com \
--cc=eddy@opera.com \
--cc=ian@airs.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).