Skip to content

Instantly share code, notes, and snippets.

@aperezdc
Created October 25, 2012 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aperezdc/3954731 to your computer and use it in GitHub Desktop.
Save aperezdc/3954731 to your computer and use it in GitHub Desktop.
Disassemble given symbols in an object file (requires Zsh)
#! /bin/zsh
#
# dis-symbol
# Copyright (C) 2012 Adrian Perez <aperez@igalia.com>
#
# 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
@aperezdc
Copy link
Author

Uses nm and objdump 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. using local -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_ or gtk_button_

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment