79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 1) #!/bin/bash
958349ccb03ba (Thomas Gleixner 2019-05-28 09:57:26 -0700 2) # SPDX-License-Identifier: GPL-2.0-only
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 3)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 4) # objdiff - a small script for validating that a commit or series of commits
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 5) # didn't change object code.
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 6) #
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 7) # Copyright 2014, Jason Cooper <jason@lakedaemon.net>
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 8) #
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 9)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 10) # usage example:
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 11) #
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 12) # $ git checkout COMMIT_A
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 13) # $ <your fancy build command here>
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 14) # $ ./scripts/objdiff record path/to/*.o
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 15) #
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 16) # $ git checkout COMMIT_B
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 17) # $ <your fancy build command here>
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 18) # $ ./scripts/objdiff record path/to/*.o
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 19) #
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 20) # $ ./scripts/objdiff diff COMMIT_A COMMIT_B
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 21) # $
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 22)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 23) # And to clean up (everything is in .tmp_objdiff/*)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 24) # $ ./scripts/objdiff clean all
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 25) #
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 26) # Note: 'make mrproper' will also remove .tmp_objdiff
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 27)
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 28) SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 29)
fd6e124233116 (Masahiro Yamada 2014-06-09 11:16:34 +0900 30) if [ -z "$SRCTREE" ]; then
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 31) echo >&2 "ERROR: Not a git repository."
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 32) exit 1
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 33) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 34)
fd6e124233116 (Masahiro Yamada 2014-06-09 11:16:34 +0900 35) TMPD=$SRCTREE/.tmp_objdiff
fd6e124233116 (Masahiro Yamada 2014-06-09 11:16:34 +0900 36)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 37) usage() {
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 38) echo >&2 "Usage: $0 <command> <args>"
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 39) echo >&2 " record <list of object files or directories>"
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 40) echo >&2 " diff <commitA> <commitB>"
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 41) echo >&2 " clean all | <commit>"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 42) exit 1
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 43) }
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 44)
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 45) get_output_dir() {
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 46) dir=${1%/*}
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 47)
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 48) if [ "$dir" = "$1" ]; then
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 49) dir=.
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 50) fi
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 51)
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 52) dir=$(cd $dir; pwd)
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 53)
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 54) echo $TMPD/$CMT${dir#$SRCTREE}
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 55) }
18165efa8203a (Masahiro Yamada 2014-06-09 11:16:37 +0900 56)
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 57) do_objdump() {
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 58) dir=$(get_output_dir $1)
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 59) base=${1##*/}
65ba6fa439e7c (Stephen Boyd 2017-02-16 13:18:20 -0800 60) stripped=$dir/${base%.o}.stripped
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 61) dis=$dir/${base%.o}.dis
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 62)
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 63) [ ! -d "$dir" ] && mkdir -p $dir
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 64)
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 65) # remove addresses for a cleaner diff
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 66) # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
65ba6fa439e7c (Stephen Boyd 2017-02-16 13:18:20 -0800 67) $STRIP -g $1 -R __bug_table -R .note -R .comment -o $stripped
65ba6fa439e7c (Stephen Boyd 2017-02-16 13:18:20 -0800 68) $OBJDUMP -D $stripped | sed -e "s/^[[:space:]]\+[0-9a-f]\+//" -e "s:^$stripped:$1:" > $dis
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 69) }
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 70)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 71) dorecord() {
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 72) [ $# -eq 0 ] && usage
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 73)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 74) FILES="$*"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 75)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 76) CMT="`git rev-parse --short HEAD`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 77)
65ba6fa439e7c (Stephen Boyd 2017-02-16 13:18:20 -0800 78) STRIP="${CROSS_COMPILE}strip"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 79) OBJDUMP="${CROSS_COMPILE}objdump"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 80)
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 81) for d in $FILES; do
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 82) if [ -d "$d" ]; then
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 83) for f in $(find $d -name '*.o')
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 84) do
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 85) do_objdump $f
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 86) done
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 87) else
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 88) do_objdump $d
7fa0e6db3cedc (Masahiro Yamada 2014-06-09 11:16:40 +0900 89) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 90) done
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 91) }
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 92)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 93) dodiff() {
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 94) [ $# -ne 2 ] && [ $# -ne 0 ] && usage
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 95)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 96) if [ $# -eq 0 ]; then
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 97) SRC="`git rev-parse --short HEAD^`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 98) DST="`git rev-parse --short HEAD`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 99) else
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 100) SRC="`git rev-parse --short $1`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 101) DST="`git rev-parse --short $2`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 102) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 103)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 104) DIFF="`which colordiff`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 105)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 106) if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 107) DIFF="`which diff`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 108) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 109)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 110) SRCD="$TMPD/$SRC"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 111) DSTD="$TMPD/$DST"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 112)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 113) if [ ! -d "$SRCD" ]; then
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 114) echo >&2 "ERROR: $SRCD doesn't exist"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 115) exit 1
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 116) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 117)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 118) if [ ! -d "$DSTD" ]; then
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 119) echo >&2 "ERROR: $DSTD doesn't exist"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 120) exit 1
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 121) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 122)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 123) $DIFF -Nurd $SRCD $DSTD
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 124) }
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 125)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 126) doclean() {
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 127) [ $# -eq 0 ] && usage
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 128) [ $# -gt 1 ] && usage
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 129)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 130) if [ "x$1" = "xall" ]; then
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 131) rm -rf $TMPD/*
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 132) else
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 133) CMT="`git rev-parse --short $1`"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 134)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 135) if [ -d "$TMPD/$CMT" ]; then
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 136) rm -rf $TMPD/$CMT
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 137) else
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 138) echo >&2 "$CMT not found"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 139) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 140) fi
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 141) }
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 142)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 143) [ $# -eq 0 ] && usage
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 144)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 145) case "$1" in
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 146) record)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 147) shift
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 148) dorecord $*
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 149) ;;
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 150) diff)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 151) shift
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 152) dodiff $*
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 153) ;;
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 154) clean)
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 155) shift
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 156) doclean $*
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 157) ;;
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 158) *)
5ab370e91af70 (Masahiro Yamada 2014-06-09 11:16:35 +0900 159) echo >&2 "Unrecognized command '$1'"
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 160) exit 1
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 161) ;;
79192ca8ebd9a (Jason Cooper 2014-04-07 18:30:29 +0000 162) esac