加载时 ELF 重定位

发布于 2024-08-03 20:04:26 字数 452 浏览 3 评论 0原文

我正在 Linux 下编写一个简单的用户空间 ELF 加载器(为什么?为了“好玩”)。目前我的加载器非常简单,旨在仅加载包含位置无关代码的静态链接 ELF 文件。

通常,当一个程序被内核的ELF加载器加载时,它被加载到自己的地址空间中。这样,数据段和代码段可以加载到ELF段中指定的正确虚拟地址处。

然而,就我而言,我通过 mmap 从内核请求地址,并且可能会也可能不会获得 ELF 段中请求的地址。这对于代码段来说不是问题,因为它是位置无关的。但是,如果数据段未加载到预期地址,则代码将无法正确引用数据段中存储的任何内容。

事实上,我的加载程序似乎可以与不包含任何数据的简单程序集可执行文件一起正常工作。但是,一旦我添加数据段并引用它,可执行文件就无法正确运行或出现SEGFAULT。

如果可能的话,如何修复对数据段的任何引用以指向正确的位置?为此目的,(静态)ELF 文件中是否存储了重定位部分?

I am writing a simple user-space ELF loader under Linux (why? for 'fun'). My loader at the moment is quite simple and is designed to load only statically-linked ELF files containing position-independent code.

Normally, when a program is loaded by the kernel's ELF loader, it is loaded into its own address space. As such, the data segment and code segment can be loaded at the correct virtual address as specified in the ELF segments.

In my case, however, I am requesting addresses from the kernel via mmap, and may or may not get the addresses requested in the ELF segments. This is not a problem for the code segment since it is position independent. However, if the data segment is not loaded at the expected address, code will not be able to properly reference anything stored in the data segment.

Indeed, my loader appears to work fine with a simple assembly executable that does not contain any data. But as soon as I add a data segment and reference it, the executable fails to run correctly or SEGFAULTs.

How, if possible, can I fixup any references to the data segment to point to the correct place? Is there a relocation section stored in the (static) ELF file for this purpose?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花伊自在美 2024-08-10 20:04:26

如果您修改 .got 部分中可用的绝对地址(全局偏移表),您的程序应该可以工作。确保修改绝对地址计算以适应 .text 和 .data 之间的新距离,恐怕您需要为您的架构找出此信息的来源。

请参阅:全局偏移表(特定于处理器)祝

你好运。

If you modify the absolute addresses available in the .got section, (global offset table) your program should work. Make sure to modify the absolute address calculation to cater for the new distance between .text and .data, I'm afraid you need to figure out where this information comes from, for your architecture.

See this: Global Offset Table (Processor-Specific)

Good luck.

内心旳酸楚 2024-08-10 20:04:26

我看不出有什么方法可以做到这一点,除非您完全模拟内核提供的虚拟地址空间,并在该虚拟空间内运行代码。当您从文件中映射数据部分时,本质上是将其重新定位到 ELF 解释器虚拟地址空间的未知地址,并且您的代码将无法以任何方式引用它。

很高兴被证明是错误的。这里有一些非常酷的东西值得学习。

I don't see any way you can do that, unless you emulate the kernel-provided virtual address space completely, and run the code inside that virtual space. When you mmap the data section from the file, you are intrinsically relocating it to an unknown address of the virtual address space of your ELF interpreter, and your code will not be able to reference to it in any way.

Glad to be proven wrong. There's something very cool to learn here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文