c - 动态符号的地址从何而来

标签 c elf

我正在编写一个用户态 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 无关

最佳答案

您必须手动解析 Sprite 。

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

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)

要查找符号表,请迭代各个部分,直到找到 sh_type = SHT_SYMTAB 的部分,然后对其进行适当的解析。

符号表是 Elf32_Sym 和/或 Elf64_Sym 结构的数组。该方法的地址将分配给结构体的 st_value 成员,该成员相对于索引 st_shndx 处的节头。

更多信息,您可以引用:

关于c - 动态符号的地址从何而来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46879401/

相关文章:

C 将共享变量传递给 pthread

c++ - 为什么动态链接的二进制文件显示硬编码的 SO 名称?

cygwin - 如何在cygwin上运行ELF二进制文件

c - 在 ELF 文件中存储和检索版本信息

c - AC_CHECK_LIB 检查什么类型的符号?

android - android art elf和linux elf文件有什么区别?

c - 打印链表

c - 如何使用curses.h下的move函数

c++ - 在 C 中写入 C++ 指针

c - 当我尝试在字符串末尾添加 NULL 终止符时,为什么会出现段错误?