Reading through man elf
, I came across the following description of the r_offset
field of an ElfN_Rela
:
This member gives the location at which to apply the relocation action. For a relocatable file, the value is the byte offset from the beginning of the section to the storage unit affected by the relocation. For an executable file or shared object, the value is the virtual address of the storage unit affected by the relocation.
(emphasis mine)
My interpretation of that last sentence is that, to get the virtual address of the relocation (e.g., of a GOT entry), I would take r_offset
and add it to the base address where the ELF was loaded iff the ELF header's e_type
was something other than ET_EXEC
("executable file") or ET_DYN
("shared object").
However, in practice, I see that the base address does need to be added to r_offset
when e_type
is ET_DYN
which makes sense since a shared object doesn't know in advance where it will be loaded.
Obviously, I'm misunderstanding what man elf
is saying.