将源代码添加到elf文件中

发布于 2024-08-13 03:28:13 字数 314 浏览 1 评论 0 原文

我想将我的 C++ 源代码添加到相应的 elf 二进制文件中,并且我正在寻找执行此操作的最佳方法。 (我正在使用代码的多个版本,并不是每个版本都应该提交到 svn 中)。

我可以使用 bash 的 >> 运算符附加源代码而不破坏 elf 文件吗? 或者 objcopy --add-section 是一种方法吗?

顺便说一句,是否有更好的主意,只需从源代码文件中递归地 grep 查找所有 #include 行即可确定使用的所有源代码文件?

I want to add my C++ source code to the corresponding elf binary file and I'm looking for the best way to do this. (I'm using several versions of my code and not every version should be committed into svn).

Can I just append the source code without destroying the elf file using bash's >> operator?
Or is objcopy --add-section a way to do this?

By the way, is there a better idea that just grep'ing all #include lines recursively from the source code files to determine ALL source code files that are used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-08-20 03:28:13

您可以通过在命令行上使用“-E”选项从大多数 C/C++ 编译器获取预处理器的输出,例如,

g++ -E my_file.c -o my_file_preproc.c

objcopy 是一个简单的选择,但我遇到了 ELF 资源编译器 今天,它可能对您有用。它允许您在 ELF 文件中嵌入任何您想要的内容,甚至会生成 C/C++ 代码以允许您添加它。因此,您甚至可以创建一个您的代码可以链接的库,该库可以直接从可执行文件打印出可执行文件的源代码。

这提出了另一个想法......而不是仅仅包含所有预处理的源代码,您可以让可执行文件提供一个选项来打印所使用的方程。

You can get the output of the preprocessor from most C/C++ compilers by using the "-E" option on the command line, e.g.,

g++ -E my_file.c -o my_file_preproc.c

objcopy is an easy bet, but I ran across the ELF resource compiler today, and it might be useful to you. It allows you to embed anything you want in an ELF file, and will even generate C/C++ code to allow you to extra it. Thus, you could even create a library that your code could link with that would print out the source for the executable straight from the executable.

Which brings up another idea...rather than just include all the preprocessed source code, you could have the executable offer an option to print out the equation(s) that are used.

阳光下慵懒的猫 2024-08-20 03:28:13

如果您使用 gcc,则可以使用 -M 标志来获取包含所有包含文件的列表的文件。它被写入到 -o 指定的文件中,如下所示:

gcc -M -c my_file.c -o list_of_included_files.txt

If you're using gcc you could use the -M flag to get a file with a list of all included files. It's written to the file specified with -o, like this:

gcc -M -c my_file.c -o list_of_included_files.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文