This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Help about calculation of addend for ELF relocations for MIPS arch
- From: "Pan ruochen" <panruochen at gmail dot com>
- To: binutils at sourceware dot org
- Date: Tue, 12 Aug 2008 16:00:23 +0800
- Subject: Help about calculation of addend for ELF relocations for MIPS arch
Hi All,
I am studying relocation of ELF for MIPS ABI (SYSTEM V APPLICATION
BINARY INTERFACE,
MIPS RISC Processor, Supplement, 3rd Edition). I do not understand
how to calculate `A' specified in the specification.
The specification says (in chapter 4-17) :
A Represents the addend used to compute the value of the relocatable
field.
...
A relocation entryr_offset value designates the offset or virtual address of the
first byte of the affected storage unit. The relocation type specifies
which bits to
change and how to calculate their values. Because MIPS uses onlyElf32_Rel re-
location entries, the relocated field holds the addend.
Does this mean the addend is saved in the storage location where relocation
will be applied to ? For example, for a R_MIPS_26 relocation entry, the addend
is saved in the `targ26' field.
I have an experiment with a relocatable ELF file `foo.out'. And `foo.img'
is the runtime image of `foo.out' after being relocated to the base address.
$mips-elf-objdump -S foo.out
c0000000: 27bdffe0 addiu sp,sp,-32
c0000004: 24040003 li a0,3
c0000008: 00002821 move a1,zero
c000000c: 00003021 move a2,zero
c0000010: 00003821 move a3,zero
c0000014: afbf0018 sw ra,24(sp)
c0000018: 0c000000 jal c0000000
...
$mips-elf-readelf -r foo.out
Relocation section '.rel.text' at offset 0x3a74 contains 254 entries:
Offset Info Type Sym.Value Sym. Name
00000018 00009704 R_MIPS_26 000002e8 entry
00000020 00007705 R_MIPS_HI16 00000000 table
00000030 00007706 R_MIPS_LO16 00000000 table
...
$mips-elf-readelf -s foo.out
Symbol table '.symtab' contains 171 entries:
Num: Value Size Type Bind Vis Ndx Name
...
151: 000002e8 0 FUNC GLOBAL DEFAULT 1 entry
...
$mips-elf-ld -Ttext 0x188e1000 foo.out -o foo.img
$mips-elf-objdump -S foo.img
188e1000: 27bdffe0 addiu sp,sp,-32
188e1004: 24040003 li a0,3
188e1008: 00002821 move a1,zero
188e100c: 00003021 move a2,zero
188e1010: 00003821 move a3,zero
188e1014: afbf0018 sw ra,24(sp)
188e1018: 0e2384ba jal 188e12e8
...
Let's deduce back from the final results. The relocation entry is at
the offset 0x18,
and of the type `R_MIPS_26'. The symbol is not local, so the foluma
(sign-extend(A<<2) + S) >> 2
should be applied. And S will be the value of the symbol `entry',
which is 0x2e8.
(sign-extend(A<<2) + 0x2e8) >> 2 = targ26(0e2384ba) = 0x022384ba
signed-extend(A<<2) = 0x088e1000
This value seems to be the offset to the PC-region (0x10000000).
So, here the addend seems to be (segment_base - PC_region) >> 2. But
there is not
a word in the specification explicitly says that.
So, how `A' is calculated for R_MIPS_26, R_MIPS_32 and other types.
--------------
Best Regards
PRC
Aug 12, 2008