Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...