#! /bin/bash # print_function_binary functionName binaryName # Author: Samuel Ellicott 2022-11-16 # Outputs the disassembled and hex form of a C function to the command line function_name=$1 binary_name=$2 # Get the function memory offset and length function_entry=`objdump -t $binary_name | grep -P "$function_name([^@]|$)" | grep '.text'` output=$(echo $function_entry | sed -En -e 's/^(\w*)(\s*[.[:alnum:]]*){3}\s*(\w*).*$/0x\1 0x\3/p') read begin length <<<$output end=$(( begin + length )) # get the function file offset code_offset=$(readelf -l $binary_name | grep LOAD | head -n 1 | sed -En -e 's/^\s*LOAD\s*\w*\s*(\w*).*$/\1/p') file_begin=$(( begin - code_offset )) echo "Dissembled Output" objdump -d --start-address=$begin --stop-address=$end $binary_name | tail -n +7 echo "" echo "C Output" xxd -i -s $file_begin -l $length $binary_name