Thursday, March 22, 2012

Commands will let you determine which version(s) of GCC were used to compile and link a binary executable file

his trick will let you determine which version(s) of GCC were used to compile and link a binary executable file:
Code:
strings -a /path/to/binary-file | grep "GCC:" | sort -u

Here is a dirt-simple scripted version of the pipeline.
which-gcc:
#!/bin/bash

if [ -z "$1" ] ; then
echo "Usage: which-gcc file"
echo "Determines GCC version(s) used to build file"
exit
fi

strings -a $1 | grep "GCC:" | sort -u


Here are some examples from my system:
Code:
$ which-gcc /bin/bash
GCC: (GNU) 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)
I'm using GCC 3.4.6.

Code:
$ which-gcc `which vuescan`
GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
This commercial product was built on a Red Hat system, quite some time ago.

Code:
$ which-gcc /opt/google-earth/googleearth-bin
GCC: (GNU) 3.4.5 20050809 (prerelease) (Ubuntu 3.4.4-6ubuntu8)
GCC: (GNU) 4.0.2
The multiple version lines suggest Google Earth is at least in part statically linked. The final executable and at least one component library were built with different versions of GCC.

Code:
$ which-gcc /usr/bin/ooffice2

ooffice2 is not a GCC executable, thus no output. (It's a Bourne shell script. Reader exercise: produce a useful message when the file argument is not a GCC-created executable. :))