Back to home page

LXR

 
 

    


0001 #!/bin/bash
0002 
0003 # objdiff - a small script for validating that a commit or series of commits
0004 # didn't change object code.
0005 #
0006 # Copyright 2014, Jason Cooper <jason@lakedaemon.net>
0007 #
0008 # Licensed under the terms of the GNU GPL version 2
0009 
0010 # usage example:
0011 #
0012 # $ git checkout COMMIT_A
0013 # $ <your fancy build command here>
0014 # $ ./scripts/objdiff record path/to/*.o
0015 #
0016 # $ git checkout COMMIT_B
0017 # $ <your fancy build command here>
0018 # $ ./scripts/objdiff record path/to/*.o
0019 #
0020 # $ ./scripts/objdiff diff COMMIT_A COMMIT_B
0021 # $
0022 
0023 # And to clean up (everything is in .tmp_objdiff/*)
0024 # $ ./scripts/objdiff clean all
0025 #
0026 # Note: 'make mrproper' will also remove .tmp_objdiff
0027 
0028 SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
0029 
0030 if [ -z "$SRCTREE" ]; then
0031         echo >&2 "ERROR: Not a git repository."
0032         exit 1
0033 fi
0034 
0035 TMPD=$SRCTREE/.tmp_objdiff
0036 
0037 usage() {
0038         echo >&2 "Usage: $0 <command> <args>"
0039         echo >&2 "  record    <list of object files or directories>"
0040         echo >&2 "  diff      <commitA> <commitB>"
0041         echo >&2 "  clean     all | <commit>"
0042         exit 1
0043 }
0044 
0045 get_output_dir() {
0046         dir=${1%/*}
0047 
0048         if [ "$dir" = "$1" ]; then
0049                 dir=.
0050         fi
0051 
0052         dir=$(cd $dir; pwd)
0053 
0054         echo $TMPD/$CMT${dir#$SRCTREE}
0055 }
0056 
0057 do_objdump() {
0058         dir=$(get_output_dir $1)
0059         base=${1##*/}
0060         dis=$dir/${base%.o}.dis
0061 
0062         [ ! -d "$dir" ] && mkdir -p $dir
0063 
0064         # remove addresses for a cleaner diff
0065         # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
0066         $OBJDUMP -D $1 | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dis
0067 }
0068 
0069 dorecord() {
0070         [ $# -eq 0 ] && usage
0071 
0072         FILES="$*"
0073 
0074         CMT="`git rev-parse --short HEAD`"
0075 
0076         OBJDUMP="${CROSS_COMPILE}objdump"
0077 
0078         for d in $FILES; do
0079                 if [ -d "$d" ]; then
0080                         for f in $(find $d -name '*.o')
0081                         do
0082                                 do_objdump $f
0083                         done
0084                 else
0085                         do_objdump $d
0086                 fi
0087         done
0088 }
0089 
0090 dodiff() {
0091         [ $# -ne 2 ] && [ $# -ne 0 ] && usage
0092 
0093         if [ $# -eq 0 ]; then
0094                 SRC="`git rev-parse --short HEAD^`"
0095                 DST="`git rev-parse --short HEAD`"
0096         else
0097                 SRC="`git rev-parse --short $1`"
0098                 DST="`git rev-parse --short $2`"
0099         fi
0100 
0101         DIFF="`which colordiff`"
0102 
0103         if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
0104                 DIFF="`which diff`"
0105         fi
0106 
0107         SRCD="$TMPD/$SRC"
0108         DSTD="$TMPD/$DST"
0109 
0110         if [ ! -d "$SRCD" ]; then
0111                 echo >&2 "ERROR: $SRCD doesn't exist"
0112                 exit 1
0113         fi
0114 
0115         if [ ! -d "$DSTD" ]; then
0116                 echo >&2 "ERROR: $DSTD doesn't exist"
0117                 exit 1
0118         fi
0119 
0120         $DIFF -Nurd $SRCD $DSTD
0121 }
0122 
0123 doclean() {
0124         [ $# -eq 0 ] && usage
0125         [ $# -gt 1 ] && usage
0126 
0127         if [ "x$1" = "xall" ]; then
0128                 rm -rf $TMPD/*
0129         else
0130                 CMT="`git rev-parse --short $1`"
0131 
0132                 if [ -d "$TMPD/$CMT" ]; then
0133                         rm -rf $TMPD/$CMT
0134                 else
0135                         echo >&2 "$CMT not found"
0136                 fi
0137         fi
0138 }
0139 
0140 [ $# -eq 0 ] && usage
0141 
0142 case "$1" in
0143         record)
0144                 shift
0145                 dorecord $*
0146                 ;;
0147         diff)
0148                 shift
0149                 dodiff $*
0150                 ;;
0151         clean)
0152                 shift
0153                 doclean $*
0154                 ;;
0155         *)
0156                 echo >&2 "Unrecognized command '$1'"
0157                 exit 1
0158                 ;;
0159 esac