Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does gcc have any options to add version info in ELF binary file?

Tags:

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.

like image 564
Lane Avatar asked May 02 '13 23:05

Lane


People also ask

How do I know what version of GCC is binary?

comment section from the binary to find the version string. Use objdump and add --section to specify section name. For example, if your compiled a program named foo in the source dir, you can run the following commands to get GCC's version info: $ objdump -s --section .

What does an elf file contains?

An elf file contains the bin information but it is surrounded by lots of other information, possible debug info, symbols, can distinguish code from data within the binary.

How is an elf file loaded?

ELF files are used by two tools: the linker and the loader. A linker combines multiple ELF files into an executable or a library and a loader loads the executable ELF file in the memory of the process.

How do I open ELF files on Linux?

you can use readelf and objdump to read parts of an elf file. You can also use 'hexdump filename' to get a hexdump of the contents of a binary file (this is likely only useful if you like reading machine code or you are writing an assembler).


1 Answers

If you don't mind changing your source file just once, add something like this:

const volatile static char version[] = VERSION; 

and compile with:

gcc -c -DVERSION='"1.2.3"' 

The volatile keeps gcc from removing the string at higher optimization levels.

As written, this won't compile if you forget the -D option, which may be either good or bad depending on your requirements.

like image 118
Keith Thompson Avatar answered Sep 18 '22 11:09

Keith Thompson