123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- #!/bin/bash
- set -o errexit
- set -o nounset
- command -v awk >/dev/null 2>&1 || die "awk isn't installed"
- command -v readelf >/dev/null 2>&1 || die "readelf isn't installed"
- command -v addr2line >/dev/null 2>&1 || die "addr2line isn't installed"
- usage() {
- echo "usage: faddr2line <object file> <func+offset> <func+offset>..." >&2
- exit 1
- }
- warn() {
- echo "$1" >&2
- }
- die() {
- echo "ERROR: $1" >&2
- exit 1
- }
- find_dir_prefix() {
- local objfile=$1
- local start_kernel_addr=$(readelf -sW $objfile | awk '$8 == "start_kernel" {printf "0x%s", $2}')
- [[ -z $start_kernel_addr ]] && return
- local file_line=$(addr2line -e $objfile $start_kernel_addr)
- [[ -z $file_line ]] && return
- local prefix=${file_line%init/main.c:*}
- if [[ -z $prefix ]] || [[ $prefix = $file_line ]]; then
- return
- fi
- DIR_PREFIX=$prefix
- return 0
- }
- __faddr2line() {
- local objfile=$1
- local func_addr=$2
- local dir_prefix=$3
- local print_warnings=$4
- local func=${func_addr%+*}
- local offset=${func_addr#*+}
- offset=${offset%/*}
- local size=
- [[ $func_addr =~ "/" ]] && size=${func_addr#*/}
- if [[ -z $func ]] || [[ -z $offset ]] || [[ $func = $func_addr ]]; then
- warn "bad func+offset $func_addr"
- DONE=1
- return
- fi
-
-
- while read symbol; do
- local fields=($symbol)
- local sym_base=0x${fields[1]}
- local sym_size=${fields[2]}
- local sym_type=${fields[3]}
-
- local addr=$(($sym_base + $offset))
- if [[ -z $addr ]] || [[ $addr = 0 ]]; then
- warn "bad address: $sym_base + $offset"
- DONE=1
- return
- fi
- local hexaddr=0x$(printf %x $addr)
-
- if [[ $sym_type != "FUNC" ]]; then
- [[ $print_warnings = 1 ]] &&
- echo "skipping $func address at $hexaddr due to non-function symbol"
- continue
- fi
-
- if [[ -n $size ]] && [[ $size -ne $sym_size ]]; then
- [[ $print_warnings = 1 ]] &&
- echo "skipping $func address at $hexaddr due to size mismatch ($size != $sym_size)"
- continue;
- fi
-
- if [[ $offset -gt $sym_size ]]; then
- [[ $print_warnings = 1 ]] &&
- echo "skipping $func address at $hexaddr due to size mismatch ($offset > $sym_size)"
- continue
- fi
-
- [[ $FIRST = 0 ]] && echo
- FIRST=0
- local hexsize=0x$(printf %x $sym_size)
- echo "$func+$offset/$hexsize:"
- addr2line -fpie $objfile $hexaddr | sed "s; $dir_prefix\(\./\)*; ;"
- DONE=1
- done < <(readelf -sW $objfile | awk -v f=$func '$8 == f {print}')
- }
- [[ $# -lt 2 ]] && usage
- objfile=$1
- [[ ! -f $objfile ]] && die "can't find objfile $objfile"
- shift
- DIR_PREFIX=supercalifragilisticexpialidocious
- find_dir_prefix $objfile
- FIRST=1
- while [[ $# -gt 0 ]]; do
- func_addr=$1
- shift
-
- DONE=0
- __faddr2line $objfile $func_addr $DIR_PREFIX 0
-
- if [[ $DONE = 0 ]]; then
- __faddr2line $objfile $func_addr $DIR_PREFIX 1
- warn "no match for $func_addr"
- fi
- done
|