On Thu, Jul 5, 2012 at 12:47 PM, jigsaw <jigsaw@xxxxxxxxx> wrote: > > I'm writing a runtime linker for MIPS32 arch, and have problems with > GPREL16 relocation type. > For instance, the compiler/assembler outputs a relocatable object with > an instruction as below : > > ff838018 sd v1,-32744(gp) > > The symbol involved here belongs to .sbss section, and is set to > runtime address 0x12b119e8. > Given that _gp is 0x12b12220, and .sbss section starts at 0x12b11868. > ( The values are read from target ELF object.) > > After relocated by ld, the instruction becomes: > > ff83f7c8 sd v1,-2104(gp) > > My question is: How ld calculates the offset 0xf7c8? > My guess is that 0xf7c8 is calculated by _gp, .sbss address and > original offset 0x8018. > And I believe the related code is in routine > _bfd_mips_elf_gprel16_reloc of binutils/src/bfd/elf32-mips.c. > > But I'm not able to figure out how this is calculated. > Could someone give a hand? The object file has a GP value, stored in the SHT_MIPS_REGINFO section. The offset in the object file is an offset from that GP value. You have to subtract that offset to get the offset into the GP section in the object file. Then adjust by where the data winds up in the output file, and then adjust by the GP value in the output file. Then of course you have to do the MIPS16 value shuffling. Ian