xref: /openbmc/u-boot/scripts/objdiff (revision 9b586031db728929282b4702703f95a1cacbdb98)
1598e2d33SMasahiro Yamada#!/bin/bash
2598e2d33SMasahiro Yamada
3598e2d33SMasahiro Yamada# objdiff - a small script for validating that a commit or series of commits
4598e2d33SMasahiro Yamada# didn't change object code.
5598e2d33SMasahiro Yamada#
6598e2d33SMasahiro Yamada# Copyright 2014, Jason Cooper <jason@lakedaemon.net>
7598e2d33SMasahiro Yamada#
8598e2d33SMasahiro Yamada# Licensed under the terms of the GNU GPL version 2
9598e2d33SMasahiro Yamada
10598e2d33SMasahiro Yamada# usage example:
11598e2d33SMasahiro Yamada#
12598e2d33SMasahiro Yamada# $ git checkout COMMIT_A
13598e2d33SMasahiro Yamada# $ <your fancy build command here>
14598e2d33SMasahiro Yamada# $ ./scripts/objdiff record path/to/*.o
15598e2d33SMasahiro Yamada#
16598e2d33SMasahiro Yamada# $ git checkout COMMIT_B
17598e2d33SMasahiro Yamada# $ <your fancy build command here>
18598e2d33SMasahiro Yamada# $ ./scripts/objdiff record path/to/*.o
19598e2d33SMasahiro Yamada#
20598e2d33SMasahiro Yamada# $ ./scripts/objdiff diff COMMIT_A COMMIT_B
21598e2d33SMasahiro Yamada# $
22598e2d33SMasahiro Yamada
23598e2d33SMasahiro Yamada# And to clean up (everything is in .tmp_objdiff/*)
24598e2d33SMasahiro Yamada# $ ./scripts/objdiff clean all
25598e2d33SMasahiro Yamada#
26598e2d33SMasahiro Yamada# Note: 'make mrproper' will also remove .tmp_objdiff
27598e2d33SMasahiro Yamada
28*9b586031SMasahiro YamadaSRCTREE=$(cd $(git rev-parse --show-toplevel 2>/dev/null); pwd)
29598e2d33SMasahiro Yamada
30*9b586031SMasahiro Yamadaif [ -z "$SRCTREE" ]; then
31*9b586031SMasahiro Yamada	echo >&2 "ERROR: Not a git repository."
32598e2d33SMasahiro Yamada	exit 1
33598e2d33SMasahiro Yamadafi
34598e2d33SMasahiro Yamada
35*9b586031SMasahiro YamadaTMPD=$SRCTREE/.tmp_objdiff
36*9b586031SMasahiro Yamada
37598e2d33SMasahiro Yamadausage() {
38*9b586031SMasahiro Yamada	echo >&2 "Usage: $0 <command> <args>"
39*9b586031SMasahiro Yamada	echo >&2 "  record    <list of object files or directories>"
40*9b586031SMasahiro Yamada	echo >&2 "  diff      <commitA> <commitB>"
41*9b586031SMasahiro Yamada	echo >&2 "  clean     all | <commit>"
42598e2d33SMasahiro Yamada	exit 1
43598e2d33SMasahiro Yamada}
44598e2d33SMasahiro Yamada
45*9b586031SMasahiro Yamadaget_output_dir() {
46*9b586031SMasahiro Yamada	dir=${1%/*}
47*9b586031SMasahiro Yamada
48*9b586031SMasahiro Yamada	if [ "$dir" = "$1" ]; then
49*9b586031SMasahiro Yamada		dir=.
50*9b586031SMasahiro Yamada	fi
51*9b586031SMasahiro Yamada
52*9b586031SMasahiro Yamada	dir=$(cd $dir; pwd)
53*9b586031SMasahiro Yamada
54*9b586031SMasahiro Yamada	echo $TMPD/$CMT${dir#$SRCTREE}
55*9b586031SMasahiro Yamada}
56*9b586031SMasahiro Yamada
57*9b586031SMasahiro Yamadado_objdump() {
58*9b586031SMasahiro Yamada	dir=$(get_output_dir $1)
59*9b586031SMasahiro Yamada	base=${1##*/}
60*9b586031SMasahiro Yamada	dis=$dir/${base%.o}.dis
61*9b586031SMasahiro Yamada
62*9b586031SMasahiro Yamada	[ ! -d "$dir" ] && mkdir -p $dir
63*9b586031SMasahiro Yamada
64*9b586031SMasahiro Yamada	# remove addresses for a cleaner diff
65*9b586031SMasahiro Yamada	# http://dummdida.tumblr.com/post/60924060451/binary-diff-between-libc-from-scientificlinux-and
66*9b586031SMasahiro Yamada	$OBJDUMP -D $1 | sed "s/^[[:space:]]\+[0-9a-f]\+//" > $dis
67*9b586031SMasahiro Yamada}
68*9b586031SMasahiro Yamada
69598e2d33SMasahiro Yamadadorecord() {
70598e2d33SMasahiro Yamada	[ $# -eq 0 ] && usage
71598e2d33SMasahiro Yamada
72598e2d33SMasahiro Yamada	FILES="$*"
73598e2d33SMasahiro Yamada
74598e2d33SMasahiro Yamada	CMT="`git rev-parse --short HEAD`"
75598e2d33SMasahiro Yamada
76598e2d33SMasahiro Yamada	OBJDUMP="${CROSS_COMPILE}objdump"
77598e2d33SMasahiro Yamada
78*9b586031SMasahiro Yamada	for d in $FILES; do
79*9b586031SMasahiro Yamada		if [ -d "$d" ]; then
80*9b586031SMasahiro Yamada			for f in $(find $d -name '*.o')
81*9b586031SMasahiro Yamada			do
82*9b586031SMasahiro Yamada				do_objdump $f
83*9b586031SMasahiro Yamada			done
84*9b586031SMasahiro Yamada		else
85*9b586031SMasahiro Yamada			do_objdump $d
86*9b586031SMasahiro Yamada		fi
87598e2d33SMasahiro Yamada	done
88598e2d33SMasahiro Yamada}
89598e2d33SMasahiro Yamada
90598e2d33SMasahiro Yamadadodiff() {
91598e2d33SMasahiro Yamada	[ $# -ne 2 ] && [ $# -ne 0 ] && usage
92598e2d33SMasahiro Yamada
93598e2d33SMasahiro Yamada	if [ $# -eq 0 ]; then
94598e2d33SMasahiro Yamada		SRC="`git rev-parse --short HEAD^`"
95598e2d33SMasahiro Yamada		DST="`git rev-parse --short HEAD`"
96598e2d33SMasahiro Yamada	else
97598e2d33SMasahiro Yamada		SRC="`git rev-parse --short $1`"
98598e2d33SMasahiro Yamada		DST="`git rev-parse --short $2`"
99598e2d33SMasahiro Yamada	fi
100598e2d33SMasahiro Yamada
101598e2d33SMasahiro Yamada	DIFF="`which colordiff`"
102598e2d33SMasahiro Yamada
103598e2d33SMasahiro Yamada	if [ ${#DIFF} -eq 0 ] || [ ! -x "$DIFF" ]; then
104598e2d33SMasahiro Yamada		DIFF="`which diff`"
105598e2d33SMasahiro Yamada	fi
106598e2d33SMasahiro Yamada
107598e2d33SMasahiro Yamada	SRCD="$TMPD/$SRC"
108598e2d33SMasahiro Yamada	DSTD="$TMPD/$DST"
109598e2d33SMasahiro Yamada
110598e2d33SMasahiro Yamada	if [ ! -d "$SRCD" ]; then
111*9b586031SMasahiro Yamada		echo >&2 "ERROR: $SRCD doesn't exist"
112598e2d33SMasahiro Yamada		exit 1
113598e2d33SMasahiro Yamada	fi
114598e2d33SMasahiro Yamada
115598e2d33SMasahiro Yamada	if [ ! -d "$DSTD" ]; then
116*9b586031SMasahiro Yamada		echo >&2 "ERROR: $DSTD doesn't exist"
117598e2d33SMasahiro Yamada		exit 1
118598e2d33SMasahiro Yamada	fi
119598e2d33SMasahiro Yamada
120598e2d33SMasahiro Yamada	$DIFF -Nurd $SRCD $DSTD
121598e2d33SMasahiro Yamada}
122598e2d33SMasahiro Yamada
123598e2d33SMasahiro Yamadadoclean() {
124598e2d33SMasahiro Yamada	[ $# -eq 0 ] && usage
125598e2d33SMasahiro Yamada	[ $# -gt 1 ] && usage
126598e2d33SMasahiro Yamada
127598e2d33SMasahiro Yamada	if [ "x$1" = "xall" ]; then
128598e2d33SMasahiro Yamada		rm -rf $TMPD/*
129598e2d33SMasahiro Yamada	else
130598e2d33SMasahiro Yamada		CMT="`git rev-parse --short $1`"
131598e2d33SMasahiro Yamada
132598e2d33SMasahiro Yamada		if [ -d "$TMPD/$CMT" ]; then
133598e2d33SMasahiro Yamada			rm -rf $TMPD/$CMT
134598e2d33SMasahiro Yamada		else
135*9b586031SMasahiro Yamada			echo >&2 "$CMT not found"
136598e2d33SMasahiro Yamada		fi
137598e2d33SMasahiro Yamada	fi
138598e2d33SMasahiro Yamada}
139598e2d33SMasahiro Yamada
140598e2d33SMasahiro Yamada[ $# -eq 0 ] &&	usage
141598e2d33SMasahiro Yamada
142598e2d33SMasahiro Yamadacase "$1" in
143598e2d33SMasahiro Yamada	record)
144598e2d33SMasahiro Yamada		shift
145598e2d33SMasahiro Yamada		dorecord $*
146598e2d33SMasahiro Yamada		;;
147598e2d33SMasahiro Yamada	diff)
148598e2d33SMasahiro Yamada		shift
149598e2d33SMasahiro Yamada		dodiff $*
150598e2d33SMasahiro Yamada		;;
151598e2d33SMasahiro Yamada	clean)
152598e2d33SMasahiro Yamada		shift
153598e2d33SMasahiro Yamada		doclean $*
154598e2d33SMasahiro Yamada		;;
155598e2d33SMasahiro Yamada	*)
156*9b586031SMasahiro Yamada		echo >&2 "Unrecognized command '$1'"
157598e2d33SMasahiro Yamada		exit 1
158598e2d33SMasahiro Yamada		;;
159598e2d33SMasahiro Yamadaesac
160