Information about symbols resolved at link time, including the symbol name and memory address, can be acquired by by executing
ld with the
-M option plus the name of the object file to be linked:
$ ld -M <OBJECT FILE>
This will result in a link map being printed to STDOUT. Of course, this output can also be redirected to a file:
$ ld -M <OBJECT FILE> > <OUTPUT FILE>
The following description of the
-M option is given in the manual page for ld(1) as well as in section 2.1 "Command Line Options" in the sourceware.org documentation of ld:
Print a link map to the standard output. A link map provides information about the link, including the following:
Where object files are mapped into memory.
How common symbols are allocated.
All archive members included in the link, with a mention of the symbol which caused the archive member to be brought in.
The values assigned to symbols.
Note - symbols whose values are computed by an expression which involves a reference to a previous value of the same symbol may not have correct result displayed in the link map. This is because the linker discards intermediate results and only retains the final value of an expression. Under such circumstances the linker will display the final value enclosed by square brackets. Thus for example a linker script containing:
foo = 1
foo = foo * 4
foo = foo + 8
will produce the following output in the link map if the -M option is used:
0x00000001 foo = 0x1
[0x0000000c] foo = (foo * 0x4)
[0x0000000c] foo = (foo + 0x8)
See Expressions for more information about expressions in linker scripts.
Here is an example link map snippet for an i386 ELF32 object file:
<<= snip =>>
.text 0x0000000008048074 0xa
*(.text.unlikely .text.*_unlikely .text.unlikely.*)
*(.text .stub .text.* .gnu.linkonce.t.*)
.text 0x0000000008048074 0xa test.o
[!provide] PROVIDE (__etext, .)
[!provide] PROVIDE (_etext, .)
[!provide] PROVIDE (etext, .)
*(.rodata .rodata.* .gnu.linkonce.r.*)
.eh_frame 0x0000000008048080 0x38
.eh_frame 0x0000000008048080 0x38 test.o
<<= snip =>>
If this is insufficient for your purposes and would like to manipulate the linker with a custom script, section "3: Linker Scripts" at sourceware.org may be helpful. Some documentation of the Link Editor Command Language can also be found on page 524 in the AT&T UNIX™ PC Model 7300 Unix System V Programmers Guide.
More information can also be found in the ld.texinfo file which is part of the source for
ld. There is also a document called The GNU Linker, which discusses linker scripts in 40 pages or so.