2

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.

Daniel Walker
  • 6,380
  • 5
  • 22
  • 45

1 Answers1

2

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").

No. You need to add base address IFF e_type == ET_DYN (which can be either a (position-independent) executable or a shared library).

In other words, the man page makes a distinction between ET_REL and ET_DYN (ET_EXEC can't be loaded at any address other than the one it was linked at, and so can't have a (non-zero) base address).

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Then why does it say that, for a shared object, the value of `r_offset` **is** the virtual address? – Daniel Walker Oct 06 '21 at 01:06
  • @DanielWalker It doesn't say that. It says "the value by which `r_offset` needs to be adjusted is the virtual address of the storage unit (i.e. shared library). And that is the same thing as "base address". – Employed Russian Oct 06 '21 at 01:50
  • Maybe we're reading two different sections of the man page. I don't see what you've quoted anywhere. – Daniel Walker Oct 06 '21 at 02:50