Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the GCC version used to compile a given ELF executable?

Tags:

gcc

elf

I'd like to retrieve the GCC version used to compile a given executable. I tried readelf but didn't get the information. Any thoughts?

like image 822
Ilyes Gouta Avatar asked Mar 05 '10 13:03

Ilyes Gouta


People also ask

How do I decode an ELF file?

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).

What is the default executable name generated by gcc?

By default, gcc assumes that you want to create an executable program called a.exe.

How do I find gcc version so?

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 . comment foo sizeof: file format elf32-i386 Contents of section .

How do I know if gcc compiler is installed?

In the Command Prompt window type “gcc” and hit enter. If the output says something like “gcc: fatal error: no input files”, that is good, and you pass the test.


1 Answers

It is normally stored in the comment section

strings -a <binary/library> |grep "GCC: (" 

returns GCC: (GNU) X.X.X

strip -R .comment <binary> strings -a <binary/library> |grep "GCC: (" 

returns no output

It is not uncommon to strip the .comment (as well as .note) section out to reduce size via

strip --strip-all -R .note -R .comment <binary> strip --strip-unneeded -R .note -R .comment <library> 

Note: busybox strings specifies the -a option by default, which is needed for the .comment section

Edit: Contrary to Berendra Tusla's answer, it does not need to be compiled with any debugging flags for this method to work.

Binary example:

# echo "int main(void){}">a.c # gcc -o a a.c -s # strings -a a |grep GCC GCC: (GNU) 4.3.4 # strip -R .comment a # strings -a a |grep GCC # 

Object example:

# gcc -c a.c -s # strings -a a.o |grep GCC GCC: (GNU) 4.3.4 # strip -R .comment a.o # strings -a a |grep GCC # 

Note the absence of any -g (debugging) flags and the presence of the -s flag which strips unneeded symbols. The GCC info is still available unless the .comment section is removed. If you need to keep this info intact, you may need to check your makefile (or applicable build script) to verify that -fno-ident is not in your $CFLAGS and the $STRIP command lacks -R .comment. -fno-ident prevents gcc from generating these symbols in the comment section to begin with.

like image 68
technosaurus Avatar answered Sep 30 '22 04:09

technosaurus