通过阅读 man elf
,我发现了以下关于 ElfN_Rela
的 r_offset
字段的描述:
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.
(强调我的)
我对最后一句的解释是,为了获取重定位的虚拟地址(例如,GOT 条目的虚拟地址),我将采用 r_offset 并将其添加到 ELF 所在的基地址当 ELF header 的 e_type
不是 ET_EXEC
(“可执行文件”)或 ET_DYN
(“共享对象”)时加载。 p>
但是,在实践中,我发现当e_type
为ET_DYN<时,基址确实需要添加到
这是有道理的,因为共享对象事先不知道它将被加载到哪里。r_offset
中
显然,我误解了 man elf
的意思。
最佳答案
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").
没有。您需要添加基地址IFFe_type == ET_DYN
(可以是(与位置无关的)可执行文件或 共享库)。
换句话说,手册页区分了 ET_REL
和 ET_DYN
(ET_EXEC
不能在除它所链接的地址,因此不能有(非零)基地址)。
关于elf - ELF重定位偏移什么时候需要基地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69452510/