2

The test is on 32-bit Linux, x86. With gcc 4.6.3 and GNU ld 2.22.

So I am trying to get the information of "how many symbols are resolved by linker" during link time? And how can I list the information of all the resolved symbols? say, the symbol name, memory address.

I am thinking I should manipulate the linker to do so, but I have no idea how to do it. I have some experiences to fed linker with a link-script, but I didn't find anything related to resolved symbol information in the link-scripts..

Could anyone give me some help? I really appreciate that! Thank you!

1
  • 1
    you could start with the 'external symbols' table in each object file. You could have the linker(gcc) output a map file. This: '-Xlinker -Map=output.map' will cause a .map file to be output, named 'output.map' – user3629249 Feb 21 '15 at 3:56
2

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:

-M

--print-map

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 =>>

.plt.got
 *(.plt.got)

.text           0x0000000008048074        0xa
 *(.text.unlikely .text.*_unlikely .text.unlikely.*)
 *(.text.exit .text.exit.*)
 *(.text.startup .text.startup.*)
 *(.text.hot .text.hot.*)
 *(.text .stub .text.* .gnu.linkonce.t.*)
 .text          0x0000000008048074        0xa test.o
                0x0000000008048074                main
 *(.gnu.warning)

.fini
 *(SORT(.fini))
                [!provide]                        PROVIDE (__etext, .)
                [!provide]                        PROVIDE (_etext, .)
                [!provide]                        PROVIDE (etext, .)    
.rodata
 *(.rodata .rodata.* .gnu.linkonce.r.*)

.rodata1
 *(.rodata1)

.eh_frame_hdr
 *(.eh_frame_hdr)
 *(.eh_frame_entry .eh_frame_entry.*)

.eh_frame       0x0000000008048080       0x38
 *(.eh_frame)
 .eh_frame      0x0000000008048080       0x38 test.o
 *(.eh_frame.*)

<<= 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.