简体   繁体   English

将部分添加到ELF文件

[英]Adding section to ELF file

I need to be able to add an arbitrary section to an ELF file. 我需要能够将任意部分添加到ELF文件中。 I cannot use GPL code in this program, so BFD is out of the question. 我不能在这个程序中使用GPL代码,所以BFD是不可能的。 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. 我可以使用libelf / gelf来阅读部分,但文档相当稀疏,我无法弄清楚如何添加一个部分。 Does anybody know how to do this? 有人知道怎么做这个吗? I would rather not write my own ELF code. 我宁愿不写我自己的ELF代码。

There's a few (possibly) related answers in this question about ELF file headers . 关于ELF文件头的这个问题中有一些(可能)相关的答案。 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. 接受的答案提到使用objcopy将部分添加到ELF文件, BSD bintools声称拥有可能适合您需要的BSD许可的objcopy实现。

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 取自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

The following links could be useful: 以下链接可能很有用:

看看ELFsh,它现在是ERESI项目的一部分http://www.eresi-project.org/他们有很多文档和很酷的用途。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM