blob: 4fb5d67968932bbf83fbdc63ee84c374d552fa41 [file] [log] [blame]
Masahiro Yamada9cf2a452014-04-15 13:29:00 +09001#!/bin/bash
2
3# objdiff - a small script for validating that a commit or series of commits
4# didn't change object code.
5#
6# Copyright 2014, Jason Cooper <jason@lakedaemon.net>
7#
8# Licensed under the terms of the GNU GPL version 2
9
10# usage example:
11#
12# $ git checkout COMMIT_A
13# $ <your fancy build command here>
14# $ ./scripts/objdiff record path/to/*.o
15#
16# $ git checkout COMMIT_B
17# $ <your fancy build command here>
18# $ ./scripts/objdiff record path/to/*.o
19#
20# $ ./scripts/objdiff diff COMMIT_A COMMIT_B
21# $
22
23# And to clean up (everything is in .tmp_objdiff/*)
24# $ ./scripts/objdiff clean all
25#
26# Note: 'make mrproper' will also remove .tmp_objdiff
27
Masahiro Yamada537cda02014-08-05 15:56:45 +090028SRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090029
Masahiro Yamada537cda02014-08-05 15:56:45 +090030if [ -z "$SRCTREE" ]; then
31 echo >&2 "ERROR: Not a git repository."
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090032 exit 1
33fi
34
Masahiro Yamada537cda02014-08-05 15:56:45 +090035TMPD=$SRCTREE/.tmp_objdiff
36
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090037usage() {
Masahiro Yamada537cda02014-08-05 15:56:45 +090038 echo >&2 "Usage: $0 <command> <args>"
39 echo >&2 " record <list of object files or directories>"
40 echo >&2 " diff <commitA> <commitB>"
41 echo >&2 " clean all | <commit>"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090042 exit 1
43}
44
Masahiro Yamada537cda02014-08-05 15:56:45 +090045get_output_dir() {
46 dir=${1%/*}
47
48 if [ "$dir" = "$1" ]; then
49 dir=.
50 fi
51
52 dir=$(cd $dir; pwd)
53
54 echo $TMPD/$CMT${dir#$SRCTREE}
55}
56
57do_objdump() {
58 dir=$(get_output_dir $1)
59 base=${1##*/}
Stephen Boyd90913452017-08-23 10:59:31 +090060 stripped=$dir/${base%.o}.stripped
Masahiro Yamada537cda02014-08-05 15:56:45 +090061 dis=$dir/${base%.o}.dis
62
63 [ ! -d "$dir" ] && mkdir -p $dir
64
65 # remove addresses for a cleaner diff
66 # http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
Stephen Boyd90913452017-08-23 10:59:31 +090067 $STRIP -g $1 -R __bug_table -R .note -R .comment -o $stripped
68 $OBJDUMP -D $stripped | sed -e "s/^[[:space:]]\+[0-9a-f]\+//" -e "s:^$stripped:$1:" > $dis
Masahiro Yamada537cda02014-08-05 15:56:45 +090069}
70
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090071dorecord() {
72 [ $# -eq 0 ] && usage
73
74 FILES="$*"
75
76 CMT="`git rev-parse --short HEAD`"
77
Stephen Boyd90913452017-08-23 10:59:31 +090078 STRIP="${CROSS_COMPILE}strip"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090079 OBJDUMP="${CROSS_COMPILE}objdump"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090080
Masahiro Yamada537cda02014-08-05 15:56:45 +090081 for d in $FILES; do
82 if [ -d "$d" ]; then
83 for f in $(find $d -name '*.o')
84 do
85 do_objdump $f
86 done
87 else
88 do_objdump $d
89 fi
Masahiro Yamada9cf2a452014-04-15 13:29:00 +090090 done
91}
92
93dodiff() {
94 [ $# -ne 2 ] && [ $# -ne 0 ] && usage
95
96 if [ $# -eq 0 ]; then
97 SRC="`git rev-parse --short HEAD^`"
98 DST="`git rev-parse --short HEAD`"
99 else
100 SRC="`git rev-parse --short $1`"
101 DST="`git rev-parse --short $2`"
102 fi
103
104 DIFF="`which colordiff`"
105
106 if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
107 DIFF="`which diff`"
108 fi
109
110 SRCD="$TMPD/$SRC"
111 DSTD="$TMPD/$DST"
112
113 if [ ! -d "$SRCD" ]; then
Masahiro Yamada537cda02014-08-05 15:56:45 +0900114 echo >&2 "ERROR: $SRCD doesn't exist"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900115 exit 1
116 fi
117
118 if [ ! -d "$DSTD" ]; then
Masahiro Yamada537cda02014-08-05 15:56:45 +0900119 echo >&2 "ERROR: $DSTD doesn't exist"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900120 exit 1
121 fi
122
123 $DIFF -Nurd $SRCD $DSTD
124}
125
126doclean() {
127 [ $# -eq 0 ] && usage
128 [ $# -gt 1 ] && usage
129
130 if [ "x$1" = "xall" ]; then
131 rm -rf $TMPD/*
132 else
133 CMT="`git rev-parse --short $1`"
134
135 if [ -d "$TMPD/$CMT" ]; then
136 rm -rf $TMPD/$CMT
137 else
Masahiro Yamada537cda02014-08-05 15:56:45 +0900138 echo >&2 "$CMT not found"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900139 fi
140 fi
141}
142
143[ $# -eq 0 ] && usage
144
145case "$1" in
146 record)
147 shift
148 dorecord $*
149 ;;
150 diff)
151 shift
152 dodiff $*
153 ;;
154 clean)
155 shift
156 doclean $*
157 ;;
158 *)
Masahiro Yamada537cda02014-08-05 15:56:45 +0900159 echo >&2 "Unrecognized command '$1'"
Masahiro Yamada9cf2a452014-04-15 13:29:00 +0900160 exit 1
161 ;;
162esac