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?
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).
By default, gcc assumes that you want to create an executable program called a.exe.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With