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

标签 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

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

我做错了什么?我需要找到 vaddr 的搬迁吗? 我查看了 LLDB 源代码,但我无法弄清楚他们是如何获得地址的。

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

最佳答案

What am I doing wrong ?

您很可能正在查看与位置无关的可执行文件。如果您的 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

然后需要通过可执行重定位地址调整Phdr_pt_dynamic.p_vaddr(关键是第一个Phdr_pt_load.p_vaddr == 0)。

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

(以上我使用 Phdr_xxx 作为 Phdr[j].p_type == xxx 的简写)。

您还以比必须的方式复杂得多的方式进行操作:动态数组的地址可以作为 _DYNAMIC[] 轻松获得。参见 this answer .

关于c - 查找进程的集合点(struct r_debug)结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55643058/

相关文章:

c - c 中 printf 函数的意外行为

linux - 如何将 Jenkins 作业从 Windows 本地计算机迁移到 Linux 服务器?

java -/usr/bin/time 是否包括 java 程序的程序后垃圾收集?

c# - 在每次调用时调用一个方法

arrays - 结构体数组成员地址问题

c - 如果我增加条目数,由 memmove 引起的段错误

c - Linux内核模式字符串复制

c# - 还有另一种方法可以将 ffmpeg 中的帧导出到 texture2d 吗?我的代码在 Windows 中工作但在 Linux 中不工作

c# - 当前在 C# 中跟踪程序流的可能性?

visual-studio - Visual Studio 的 "Start Without Debugging"在 JetBrains Rider 中是否等效?