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