Adding section to ELF file

别来无恙 提交于 2019-11-27 11:41:49

问题


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.


回答1:


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.




回答2:


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



回答3:


The following links could be useful:

  • The tutorial "libelf by Example" has a chapter on creating new ELF objects.
  • The elftoolchain project has manual pages describing libelf's API in detail.



回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/1088128/adding-section-to-elf-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!