将文件打包为 ELF 可执行文件
我目前正在寻找一种将数据添加到已编译的 ELF 可执行文件中的方法,即将文件嵌入到可执行文件中而不需要重新编译它。
我可以通过使用 cat myexe mydata > 轻松做到这一点myexe_with_mydata,但我无法访问可执行文件中的数据,因为我不知道原始可执行文件的大小。
有谁知道我如何实现这个?我想过在可执行文件中添加一个节或使用特殊标记(例如0xBADBEEFC0FFEE
)来检测可执行文件中数据的开头,但我不知道是否有更漂亮的方法做吧。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用objcopy(1)将文件作为特殊部分添加到elf文件中:
将文件添加到oldelf并将结果写入newelf(oldelf不会被修改)
然后,您可以使用 libbfd 读取 elf 文件并按名称提取该节,或者只需编写自己的代码来读取节表并找到您的节。确保使用的节名称不会与系统期望的任何内容发生冲突——只要您的名称不以
.
开头,就应该没问题。You could add the file to the elf file as a special section with objcopy(1):
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.我创建了一个名为 elfdataembed 的小型库,它提供了一个简单的界面,用于提取/引用使用
对象复制
。这允许您将偏移量/大小传递给另一个工具,或使用文件描述符直接从运行时引用它。希望这对将来的人有所帮助。值得一提的是,这种方法比编译为符号更有效,因为它允许外部工具引用数据而无需提取,并且它也不需要将整个二进制文件加载到内存中以提取/引用它。
I've created a small library called elfdataembed which provides a simple interface for extracting/referencing sections embedded using
objcopy
. This allows you to pass the offset/size to another tool, or reference it directly from the runtime using file descriptors. Hopefully this will help someone in the future.It's worth mentioning this approach is more efficient than compiling to a symbol, as it allows external tools to reference the data without needing to be extracted, and it also doesn't require the entire binary to be loaded into memory in order to extract/reference it.