【发布时间】:2020-06-11 22:13:15
【问题描述】:
我在标准 elf 二进制文件中使用 gcc 编译了以下简单的 hello-world 程序:
#include <stdio.h>
void hello() {
printf("Preinit hello!\n");
}
int main() {
printf("Hello World\n");
return 0;
}
如何使用 libelf(或任何其他建议的用于此类操作的 C 库)创建新的 elf 文件,以添加调用 hello() 的 .preinit_array 部分?
我的最终目标是“模拟”编译器将如何处理以下内容:
__attribute__((section(".preinit_array"))) typeof(hello) *hello_p = hello;
我阅读了libelf by example,但我发现它完全神秘,并且仅使用 32 位示例已过时。
我还发现了this 相关的 SO 问题。 objcopy 正在做我想做的事情,但显然我不想从我的 C 程序中调用 objcopy。此外,我尝试从here 复制objcopy 的源代码,但收效甚微,因为代码非常复杂且相互关联。
【问题讨论】: