Skip to content

Instantly share code, notes, and snippets.

@corytodd
Created November 2, 2023 19:41
Show Gist options
  • Save corytodd/7cb0dce44457d7586debf2b39f681264 to your computer and use it in GitHub Desktop.
Save corytodd/7cb0dce44457d7586debf2b39f681264 to your computer and use it in GitHub Desktop.
GDB disassmbly helper
#!/bin/bash -eu
TOOLCHAIN=${TOOLCHAIN-arm-none-eabi-}
OBJDUMP=${TOOLCHAIN}objdump
GDB=${TOOLCHAIN}gdb
function usage()
{
cat <<EOF
Usage: dism.sh -f|function -b|--binary --no-source
Disassmble a possibly mangled function and output to stdout. For C functions, just provide
the function name. For C++, either locate the mangled name yourself or provide a substring
and you'll get given a list of possible matches to chose from.
Optional arguments:
-h, --help Show this help message and exit.
-f, --function Name of function to disassemble.
-b, --binary Target binary.
--no-source Do not emit source reference
ENV:
TOOLCHAIN: toolchain prefix, default arm-none-eabi
Example:
Disassemble a function named foo.
dism.sh --funcion foo --binary my_binary
Disassemble a function named foo using a different gdb
TOOLCHAIN= dism.sh --funcion foo --binary my_binary
EOF
}
target_function=
binary=
dism_args="/s"
while [ ${#} -gt 0 ] ; do
case "${1}" in
-h|--help)
usage
exit 0
;;
-f|--function)
target_function="${2}"
shift
;;
-b|--binary)
binary="${2}"
shift
;;
--no-source)
dism_args=""
;;
*)
usage
exit 2
;;
esac
shift
done
if [ -z "${target_function}" ]; then
echo "--function required"
usage
exit 1
fi
if [ -z "${binary}" ]; then
echo "--binary required"
usage
exit 1
fi
matched_functions=()
while IFS='' read -r func; do
matched_functions+=("$func");
done < <(${OBJDUMP} -j .text -t "${binary}" \
| grep "${target_function}" \
| awk -F ' ' '{print $6}')
if [ "${#matched_functions[@]}" -eq 0 ]; then
echo "Function not found"
exit 1
elif [ "${#matched_functions[@]}" -gt 1 ]; then
prompt="Multiple (mangled?) functions found. Pick one:"
PS3="${prompt} "
select func in "${matched_functions[@]}"; do
matched_functions=("${func}")
break
done
fi
target_function=${matched_functions[0]}
$GDB -batch -ex "disassemble$dism_args $target_function" "${binary}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment