gcc是否有可以在ELF二进制文件中添加版本信息的选项? [英] Does gcc have any options to add version info in ELF binary file?

查看:322
本文介绍了gcc是否有可以在ELF二进制文件中添加版本信息的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是gcc是否可以将部分源代码版本信息插入到ELF二进制文件中作为章节或类似内容。我不想改变我的源文件,但添加在Makefile中GCC选项的一些信息。

I mean whether gcc can insert some source code version infor into ELF binary as section or something similar. I do not want to change my source file, but add some info with gcc option in Makefile.

推荐答案

您可以发出你的版本信息转换为文本文件,然后将该文本文件转换为目标文件,然后将其静态链接到您的可执行文件中。

You can emit your version info into a text file, then turn that text file into an object file which you then statically link into your executable.

第一步很简单,但您必须编写一些代码:一个脚本或者以你喜欢的任何格式编写你的版本信息的纯文本文件。然后编写一个makefile规则,使用 objcopy 从version.txt生成版本version.o。现在您将拥有一个包含两个有用符号的对象文件:文本版本信息的开始和结尾。将生成的对象添加到可执行文件中,并且可以通过以下两种方式访问​​版本:在二进制文件上运行 strings ,或者在应用程序中编写代码打印版本字符串(您需要在某个头文件中将开始和结束符号声明为变量)。

The first step is simple but you have to write some code: a script or something to write your version info in any format you like as a plain text file. Then write a makefile rule to produce say version.o from version.txt, using objcopy. Now you'll have an object file with two useful symbols defined in it: the beginning and end of the textual version info. Add that generated object to your executable, and you'll be able to access the version two ways: by running strings on the binary, or by writing code in the application to print the version string (you'll need to declare the start and end symbols as variables in some header file).

这篇关于gcc是否有可以在ELF二进制文件中添加版本信息的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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