LD 2.20 absolute / relative symbol assignment issue
Nick Clifton
nickc@redhat.com
Tue Feb 9 16:55:00 GMT 2010
Hi Mark,
> I have two issues:
>
> 1) In most cases the values assigned to symbols such as __EXAMPLE_START
> are absolute ones whether I use ABSOLUTE() or not; I cannot get
> section-relative symbol values.
I think that this is a documentation problem. Essentially I think that
your problems lie in the difference between a relative link and a final
link. For example consider this script:
SECTIONS
{
. = 0x1000 ;
sym1 = . ;
sym2 = ABSOLUTE (.);
.foo :
{
sym3 = .;
sym4 = ABSOLUTE (.);
LONG (1);
sym5 = .;
sym6 = ABSOLUTE (.);
. += 0x100;
sym7 = .;
sym8 = ABSOLUTE (.);
}
sym9 = .;
sym10 = ABSOLUTE (.);
/DISCARD/ : { *(*) }
}
If you run the following commands:
ld -T script.t foo.o
(You have to specify at least one input file, even
though it is going to be discarded).
readelf --syms a.out
Then you will get output like this:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00001004 0 NOTYPE GLOBAL DEFAULT ABS sym6
3: 00001000 0 NOTYPE GLOBAL DEFAULT ABS sym1
4: 00001000 0 NOTYPE GLOBAL DEFAULT ABS sym4
5: 00001104 0 NOTYPE GLOBAL DEFAULT ABS sym10
6: 00001104 0 NOTYPE GLOBAL DEFAULT ABS sym9
7: 00000000 0 NOTYPE GLOBAL DEFAULT 1 sym3
8: 00000104 0 NOTYPE GLOBAL DEFAULT 1 sym7
9: 00001104 0 NOTYPE GLOBAL DEFAULT ABS sym8
10: 00001000 0 NOTYPE GLOBAL DEFAULT ABS sym2
11: 00000004 0 NOTYPE GLOBAL DEFAULT 1 sym5
Note how symbols 3, 5 and 7 are section based symbols whereas all of the
rest are absolute symbols. The absolute symbols have the values that
you would expect, but the section-relative ones do not. But try these
commands next:
ld -T script.t -r foo.o
readelf --syms a.out
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
1: 00000000 0 SECTION LOCAL DEFAULT 1
2: 00001004 0 NOTYPE GLOBAL DEFAULT ABS sym6
3: 00001000 0 NOTYPE GLOBAL DEFAULT ABS sym1
4: 00001000 0 NOTYPE GLOBAL DEFAULT ABS sym4
5: 00001104 0 NOTYPE GLOBAL DEFAULT ABS sym10
6: 00001104 0 NOTYPE GLOBAL DEFAULT ABS sym9
7: 00000000 0 NOTYPE GLOBAL DEFAULT 1 sym3
8: 00000104 0 NOTYPE GLOBAL DEFAULT 1 sym7
9: 00001104 0 NOTYPE GLOBAL DEFAULT ABS sym8
10: 00001000 0 NOTYPE GLOBAL DEFAULT ABS sym2
11: 00000004 0 NOTYPE GLOBAL DEFAULT 1 sym5
Now the section-relative symbols have the values that you might expect.
The reason is that the "-r" command line switch instructs the linker
to perform a relocatable link. This is one where the final location of
output sections is not fixed, and so section relative symbols cannot be
fully evaluated.
Once a final link is performed (one without the -r command line switch)
the location of all output sections is fixed and the values of all
symbols, section based or absolute, can be computed.
Now I agree that reading the linker documentation gives the impression
that section-relative symbols always contain values that are relative to
the start of their containing section. But this is not true. It only
applies before a final link has been invoked.
Does this make sense ? If so then I will try to come up with a
documentation patch to explain the situation more clearly.
> 2) In some cases the use of the ABSOLUTE() keyword actually results in
> the section-relative value being assigned!
This worries me - can you give an example of when this happens ?
Cheers
Nick
More information about the Binutils
mailing list