查找进程的集合点(struct r_debug)结构?

nit*_*ram 5 c linux debugging elf debug-symbols

我正在尝试访问“集合结构”(struct r_debug *)以便找到进程的链接图。但我不断遇到无效地址,我真的不知道发生了什么。

以下是我继续尝试找到它的方法:

1. Get the AT_PHDR value from the auxiliary vector
2. Go through the program headers until I find the PT_DYNAMIC segment
3. Try to access the vaddr of that segment (PT_DYNAMIC) to get the dynamic tags
4. Iterate through the dynamic tags until I find DT_DEBUG. If I get here I should be done
Run Code Online (Sandbox Code Playgroud)

问题是我无法通过步骤 3,因为 PT_DYNAMIC 段的 vaddr 始终指向无效地址。

我究竟做错了什么 ?我需要找到 vaddr 的重定位吗?我查看了 LLDB 来源,但我不知道他们是如何获得地址的。

更新:@EmployedRussian 是对的,我正在寻找一个与位置无关的可执行文件。他的计算搬迁的解决方案效果非常好。

Emp*_*ian 4

我究竟做错了什么 ?

您很可能正在寻找与位置无关的可执行文件。如果你readelf -Wl a.out看起来像这样:

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  PHDR           0x000040 0x0000000000000040 0x0000000000000040 0x0001f8 0x0001f8 R   0x8
  INTERP         0x000238 0x0000000000000238 0x0000000000000238 0x00001c 0x00001c R   0x1
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x016d28 0x016d28 R E 0x200000
  LOAD           0x017250 0x0000000000217250 0x0000000000217250 0x0010d0 0x001290 RW  0x200000
  DYNAMIC        0x017df8 0x0000000000217df8 0x0000000000217df8 0x0001e0 0x0001e0 RW  0x8
Run Code Online (Sandbox Code Playgroud)

那么就需要Phdr_pt_dynamic.p_vaddr通过可执行文件的重定位地址来调整(关键是第一个Phdr_pt_load.p_vaddr == 0)。

AT_PHDR您可以找到此重定位地址作为aux 向量中的值与 之间的增量Phdr_pt_phdr.p_vaddr

(上面我用它作为withPhdr_xxx的简写)。Phdr[j].p_type == xxx

您还以比必须要复杂得多的方式执行此操作:动态数组的地址通常可以作为_DYNAMIC[]. 看到这个答案