elf - ELF重定位偏移什么时候需要基地址

标签 elf dynamic-linking virtual-address-space

通过阅读 man elf,我发现了以下关于 ElfN_Relar_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_typeET_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_RELET_DYN(ET_EXEC 不能在除它所链接的地址,因此不能有(非零)基地址)。

关于elf - ELF重定位偏移什么时候需要基地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69452510/

相关文章:

linux - 强制 ELF 二进制文件使用另一个 libc.so

c++ - 比较 pimpl idiom 与 Microsoft COM

virtual-address-space - 为什么内核模式下的驱动程序必须非常小心地直接读取或写入用户空间中的地址?

windows - 为什么 Windows 为其系统地址空间保留 1Gb(或 2 Gb)?

android - 什么是 ELF 二进制文件中的 .init_array 部分?

linux - 错误/lib/x86_64-linux-gnu/libc.so.6 : version `GLIBC_2.34' not found

linux - 符号、ELF 以及如何在内存中链接

c++ - 使用 CMake 在 macOS 中链接 .so 库时出错

c++ - cmake在ubuntu上链接共享库