将部分添加到 ELF 文件

发布于 2024-07-25 19:31:33 字数 145 浏览 5 评论 0原文

我需要能够向 ELF 文件添加任意部分。 我不能在这个程序中使用 GPL 代码,所以 BFD 是不可能的。 我可以使用 libelf/gelf 来阅读部分,但这些部分的文档相当稀疏,而且我不知道如何添加部分。 有人知道怎么做这个吗? 我宁愿不编写自己的 ELF 代码。

I need to be able to add an arbitrary section to an ELF file. I cannot use GPL code in this program, so BFD is out of the question. I can use libelf/gelf to read sections, but the documentation is fairly sparse for these, and I cannot figure out how to add a section. Does anybody know how to do this? I would rather not write my own ELF code.

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

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

发布评论

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

评论(5

冷︶言冷语的世界 2024-08-01 19:31:33

我知道这是一个老问题,但我找到了一个有效的示例,可以帮助我将其应用到我的项目中。 (以防万一有人偶然发现这个问题)

取自Sourceware Mail Archive

$ echo 'int main() { puts ("Hello world"); }' | gcc -x c - -c -o hello.o

$ echo "this is my special data" >mydata

$ objcopy --add-section .mydata=mydata \
          --set-section-flags .mydata=noload,readonly hello.o hello2.o

$ gcc hello2.o -o hello

$ ./hello
Hello world

$ objdump -sj .mydata hello

I know this is an old question but i found a working example that helped me to apply it to my project. (In case anyone stumbles upon this question)

taken from Sourceware Mail Archiv

$ echo 'int main() { puts ("Hello world"); }' | gcc -x c - -c -o hello.o

$ echo "this is my special data" >mydata

$ objcopy --add-section .mydata=mydata \
          --set-section-flags .mydata=noload,readonly hello.o hello2.o

$ gcc hello2.o -o hello

$ ./hello
Hello world

$ objdump -sj .mydata hello
少跟Wǒ拽 2024-08-01 19:31:33

这个关于 ELF 文件头的问题中有一些(可能)相关的答案。 接受的答案提到使用 objcopy 将部分添加到 ELF 文件,以及 BSD bintools 声称拥有 BSD 许可的 objcopy 实现,可能适合您的需求。

There's a few (possibly) related answers in this question about ELF file headers. The accepted answer mentioned using objcopy to add sections to an ELF file, and the BSD bintools claims to have a BSD-licensed implementation of objcopy that might suit your needs.

遗心遗梦遗幸福 2024-08-01 19:31:33

以下链接可能有用:

The following links could be useful:

谜兔 2024-08-01 19:31:33

看看 ELFsh,它现在是 ERESI 项目的一部分
http://www.eresi-project.org/
他们有大量的文档和工具的酷炫用法。

have a look at ELFsh, which is now part of the ERESI project
http://www.eresi-project.org/
They have a lot of documentation and cool uses of their tools.

宫墨修音 2024-08-01 19:31:33

我自己还没有尝试过,但你可以看看 libelf 中的 elf_newscn 函数

Haven't tried it (yet) myself but you might look at the function elf_newscn in libelf

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