有没有任何标准的方法嵌入资源到Linux可执行映像? [英] Is there any standard way of embedding resources into Linux executable image?

查看:264
本文介绍了有没有任何标准的方法嵌入资源到Linux可执行映像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Windows API将二进制资源嵌入到PE映像(EXE,DLL)中非常容易(请参阅 http://msdn.microsoft.com/en-us/library/ms648008(v=VS.85).aspx )。

It is quite easy to embed binary resources into PE images (EXE, DLL) via Windows API (refer to http://msdn.microsoft.com/en-us/library/ms648008(v=VS.85).aspx).

在Linux中是否有类似的标准API?

事实上的资源嵌入方法?

or maybe some kind of de-facto approach to resource embedding?

目标是将一些静态二进制和/或文本数据嵌入到可执行文件中,例如图片,HTML等等,所以程序二进制分配是简单的做一个文件副本? (假设所有库依赖都可以

The goal is to embed some static binary and/or textual data into executable, e.g. pictures, HTMLs, etc.. so that program binary distribution is as simple as making one file copy? (assuming all library dependencies are ok)

更新

遵循 bdk 的建议,我尝试了使用gcc mingw嵌入二进制blob,它为我工作。虽然,有一些值得一提的问题:我的项目(在Code :: Blocks)包含一些C ++文件,并添加二进制数据到任何相应的目标文件,使它们无用打破构建 - objdump -x 会显示大多数符号已经在嵌入后(我没有找到如何解决它)。为了克服这个问题,我添加了一个空的虚拟.cpp文件到项目中,目的仅仅是提供一个对象文件来玩,并为该文件编写了以下自定义构建步骤,这很好地工作(示例使用Code :: Blocks宏):

following bdk's suggestion, I've tried solution described in Embedding binary blobs using gcc mingw and it worked for me. Though, there were some issues that are worth mentioning: my project (in Code::Blocks) consists of a number of C++ files and adding binary data into any of corresponding object files rendered them useless breaking the build - objdump -x would show that most of the symbols have gone after embedding (and I didn't find how to fix it). To overcome this issue I added an empty dummy .cpp file into the project with the only purpose of providing an object file to play with and wrote the following custom build step for that file which did the job nicely (example uses Code::Blocks macros):

$compiler $options $includes -c $file -o $object
ld -Ur -b binary -o $object <binary payload path>


推荐答案

objcopy --add-section允许您添加任意文件作为ELF可执行文件中的一个节。 (objcopy man page)。但是这只是一半的解决方案,因为我还没有找到一种方法来访问这个数据从C程序,而不是通过使用ELF库加载和解析ELF二进制。

objcopy --add-section allows you to add an arbitrary file as a section in an ELF executable. (objcopy man page). However this is only half a solution, as I have not yet found a way to access this data from inside a C Program other than by loading and parsing the ELF Binary using an ELF Library.

编辑附加信息:

如果您有一个编译程序MyProgram一个要嵌入MyProgram的资源文件MyResource.dat,你可以像这样使用objcopy命令:

If you have a compiled program called MyProgram and a resource file MyResource.dat which you want embedded into MyProgram, you can use the objcopy command like this:

objcopy MyProgram --add-section MyResource=MyResource.dat

现在,如果你看到你的程序使用命令
objdump -x MyProgram

Now if you look at your program using the command objdump -x MyProgram

您将看到一个名为MyResource的部分,其中包含MyResource.dat的内容。该文件现在嵌入您的可执行文件中。

You will see a section called MyResource which contains the contents of MyResource.dat. The file is now embedded inside of your executable.

现在的窍门是,如何从程序内部访问数据。我的直觉告诉我,加载器应该将文件放在内存中,你应该能够得到一个指针,但我不知道如何做到这一点。理想情况下,我想要能够减少我的exeutable和dlsym的部分,但这不工作,因为它的一个部分,而不是一个符号。

The trick now, is how do you access the data from inside your program. My instinct tells me that the loader should place the file into memory somewhere and you should be able to get a pointer to it, however I'm not sure how to do that simply. Ideally I'd want to be able to dlopen my exeutable and dlsym the section, but that doesn't work because its a section and not a symbol.

我知道从程序内部访问部分是使用libelf库或类似的东西,这有点像使用大锤敲钉钉。您可以在应用程序中使用它将自身加载为ELF资源并检索节。文档很少,但这里是一个示例

The only alternative I know of to access the section from inside the program is to use the libelf library or something similar which is a little like using a sledgehammer to tap in a nail. You can use it in your application to load itself as an ELF Resource and retrieve the sections. Documentation is sparse, but here's an example

http://em386.blogspot.com/2007/03/quick-libelf-guide.html

我会喜欢,如果有人可以

I'd love if someone could chime in with an easier way to access the data from --add-section.

编辑2 在我的研究中,我遇到了这个问题:使用gcc mingw嵌入二进制blob

Edit 2 In My research I encoutered this question: Embedding binary blobs using gcc mingw

这应该适用于gcc以及mingw,并显示了使用ld而不是objcopy添加数据并能够作为符号访问它的方法。看起来很有前途

Which should work for gcc as well as mingw and shows a way to use ld instead of objcopy to add the data and be able to access it as a symbol. Looks promising

这篇关于有没有任何标准的方法嵌入资源到Linux可执行映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆