动态符号的地址从哪里来

Where does the address of a dynamic symbol come from

我正在编写一个 userland elf 加载程序作为副项目,以了解有关 ELF 文件及其工作方式的更多信息。我想知道从哪里获取地址以从动态库 (libc.so.6) 加载符号。 当我反汇编程序时,我得到:

$objdump -d test | grep puts
  400420:       ff 25 f2 0b 20 00       jmpq   *0x200bf2(%rip)        # 601018 <puts@GLIBC_2.2.5>

0x200bf2是函数所在的地址,但是从哪里获取呢?

编辑:我问的是加载位置而不是定义位置,因此 st_value 无关紧要

您必须手动解析精灵。

此类信息可以在符号Table部分找到:

This contains a list of all of the symbols (program entry points, addresses of variables, etc.) that are defined or referenced within the file, the address associated with the symbol, and some kind of tag indicating the type of the symbol.

(From The Linux Journal)

要查找符号 table,遍历各个部分,直到找到 sh_type = SHT_SYMTAB 对应的部分,然后对其进行适当的解析。

符号 table 是 Elf32_Sym and/or Elf64_Sym 结构的数组。该方法的地址将分配给结构的 st_value 成员,它与索引 st_shndx.

处的节头相关

更多信息可以参考: