Skip to content

Instantly share code, notes, and snippets.

@atotto
Created October 8, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atotto/b9e1afe9f7079b896ba7 to your computer and use it in GitHub Desktop.
Save atotto/b9e1afe9f7079b896ba7 to your computer and use it in GitHub Desktop.
print go version from built binary
#!/bin/bash -e
# usage
# $0 path/to/go/built/binary
usage() {
echo "Usage"
echo " $0 path/to/go/built/binary"
exit 2
}
obj=$1
if [ -z $obj ] || [ ! -e $obj ];then
echo "No such file"
usage
fi
v=(`go tool nm -size $obj | grep 'runtime.buildVersion' | awk '{print $1,$2}'`)
addr=${v[0]}
size=${v[1]}
start=`objdump -s $obj | grep $addr | awk '{hex=$2; printf "0x%s%s%s%s\n",substr(hex,7,2),substr(hex,5,2),substr(hex,3,2),substr(hex,1,2);}'`
end=`printf "%d+%d\n" $start $size | bc`
objdump -s --start-address=$start --stop-address=$end $obj | grep go | sed -e 's/.*\(go.*\).*/\1/' -e 's/\.\.\+//g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment