将文件打包到ELF可执行文件中 [英] Packing a file into an ELF executable
问题描述
我目前正在寻找一种将数据添加到已编译的ELF可执行文件中的方法,即将文件嵌入到可执行文件中,而无需重新编译它.
我可以使用cat myexe mydata > myexe_with_mydata
轻松地做到这一点,但是由于我不知道原始可执行文件的大小,所以无法从可执行文件访问数据.
有人知道我该如何实现吗?我曾想在可执行文件中添加一个部分,或使用特殊标记(例如0xBADBEEFC0FFEE
)来检测可执行文件中数据的开始,但是我不知道是否有更漂亮的方法来实现.>
谢谢.
您可以使用 objcopy(1)将文件作为特殊部分添加到elf文件中:
objcopy --addsection sname=file oldelf newelf
会将文件添加到oldelf并将结果写入newelf(不会修改oldelf)
然后,您可以使用 libbfd 来读取elf文件并按名称提取节,或者只是滚动您自己的代码即可读取节表并找到您的节.确保使用的部分名称不会与系统期望的任何名称发生冲突-只要您的名称不是以.
开头,就可以了.
I'm currently looking for a way to add data to an already compiled ELF executable, i.e. embedding a file into the executable without recompiling it.
I could easily do that by using cat myexe mydata > myexe_with_mydata
, but I couldn't access the data from the executable because I don't know the size of the original executable.
Does anyone have an idea of how I could implement this ? I thought of adding a section to the executable or using a special marker (0xBADBEEFC0FFEE
for example) to detect the beginning of the data in the executable, but I do not know if there is a more beautiful way to do it.
Thanks in advance.
You could add the file to the elf file as a special section with objcopy(1):
objcopy --addsection sname=file oldelf newelf
will add the file to oldelf and write the results to newelf (oldelf won't be modified)
You can then use libbfd to read the elf file and extract the section by name, or just roll your own code that reads the section table and finds you section. Make sure to use a section name that doesn't collide with anything the system is expecting -- as long as your name doesn't start with a .
, you should be fine.
这篇关于将文件打包到ELF可执行文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!