Created
October 25, 2012 19:04
-
-
Save aperezdc/3954731 to your computer and use it in GitHub Desktop.
Disassemble given symbols in an object file (requires Zsh)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/zsh | |
# | |
# dis-symbol | |
# Copyright (C) 2012 Adrian Perez <[email protected]> | |
# | |
# Distributed under terms of the MIT license. | |
# | |
if [[ $# -lt 2 ]] ; then | |
cat <<EOF | |
Usage: $(basename "$0") object-file symbol... | |
EOF | |
exit 1 | |
fi | |
: ${OBJDUMP:=objdump} | |
: ${NM:=nm} | |
ofile=$1 | |
shift | |
find_symbol () { | |
local big_d='' | |
if [[ ${1} = *.so ]] ; then | |
big_d=-D | |
fi | |
local addr size stype symbol | |
while read -r addr size stype symbol ; do | |
if [[ ${symbol} =~ $2 ]] ; then | |
local -i ia=${addr} | |
local -i is=${size} | |
printf '0x%x 0x%x %s\n' $ia $(( ia + is )) "${symbol}" | |
fi | |
done < <( "${NM}" --radix=d ${big_d} --defined-only -S -C "$1" ) | |
} | |
disassemble_one () { | |
local start end symbol | |
while read -r start end symbol ; do | |
if [[ -n ${symbol} ]] ; then | |
"${OBJDUMP}" --start-address=${start} --stop-address=${end} \ | |
--demangle=auto --section=.text --disassemble "$1" | |
else | |
echo "No symbol found matching '$2'" | |
return | |
fi | |
done < <( find_symbol "$1" "$2" ) | |
} | |
while [[ $# -gt 0 ]] ; do | |
disassemble_one "${ofile}" "$1" | |
shift | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses
nm
andobjdump
from the GNU Binutils to find out the start and end addresses of symbols matching any of the given regular expressions, and disassembles them. Initially I wanted the script to work with Bash; but it would choke when coercing strings with trailing zeroes to integers e.g. usinglocal -i foo=0042
. Zsh works fine, but if someone knows how to make it work in Bash without requiring external tools, please drop a comment.Usage:
dis-symbol.sh /usr/lib/libgtk-3.so '^gtk_window_' '^gtk_button_'
The example above will disassemble all the symbols whose name starts with
gtk_window_
orgtk_button_