-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiffvmlinux.sh
More file actions
executable file
·229 lines (197 loc) · 4.77 KB
/
diffvmlinux.sh
File metadata and controls
executable file
·229 lines (197 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/sh
#
# Copyright (c) 2012, Intel Corporation
# Author: Lv Zheng <lv.zheng@intel.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; version 2 of the License.
#
# NAME
# diffvmlinux.sh - Generating vmlinux assembly differences before/after
# applying a quilt patch
# SYNOPSIS
# diffvmlinux.sh [-f assembly-diff] [-p patch-file] [-o object-file]
# <linux-dir>
# FIXME: More Useless Sections
#
# Please add other sections that would appear as executable but should get
# removed for the comparision.
USELESS_SECTIONS=".notes"
fulldir() {
lpath=$1
(
cd $lpath; pwd
)
}
getdir() {
lpath=`dirname $1`
fulldir $lpath
}
fatal() {
echo $1
exit -1
}
usage() {
echo "Usage: `basename $0` [-f diff] [-o object] [-p patch] <linux>"
echo "Where:"
echo " linux: Specify the linux source tree."
echo "object: Specify the object file to compare, default is vmlinuz."
echo " diff: Specify the generated diff file, default is bin.diff."
echo " patch: Specify the patch file name, detection will be performed without"
echo " specification."
}
fatal_usage() {
usage
exit -1
}
quilt_applied() {
c=$1
applied_patches=`quilt applied 2>/dev/null`
for a in $applied_patches; do
if [ "x$a" = "x$c" ]; then
return 0
fi
done
return 1
}
remove_sections() {
binary2=$1
binary3=$2
for section in $USELESS_SECTIONS; do
echo "Removing $section section..."
objcopy -R $section $binary2 $binary3
mv $binary3 $binary2
done
}
quilt_push() {
quilt push > /dev/null || fatal "failed to push quilt stack"
}
quilt_pop() {
quilt pop > /dev/null || fatal "failed to pop quilt stack"
}
detect_patch() {
quilt_push
detected_patches=`quilt applied 2>/dev/null`
quilt_pop
for d in $detected_patches; do
quilt_applied $d || echo $d
done
}
quilt_forward() {
l=$1
patches=`quilt unapplied 2>/dev/null`
for i in $patches; do
quilt_push
quilt_applied $l
if [ $? -eq 0 ]; then
quilt_pop
return 0;
fi
done
fatal "Cannot find $locate in the unapplied patches."
}
quilt_backward() {
l=$1
patches=`quilt applied 2>/dev/null`
for i in $patches; do
quilt_pop
quilt_applied $l || return 0
done
fatal "Cannot find $locate in the applied patches."
}
locate_patch() {
t=$1
quilt_applied $t
if [ $? -eq 0 ]; then
echo "Locating $t in the applied patches..."
quilt_backward $t
else
echo "Locating $t in the unapplied patches..."
quilt_forward $t
fi
}
while getopts "f:p:o:" opt
do
case $opt in
f) ASMDIFF=$OPTARG;;
o) BINARY=$OPTARG;;
p) PATCH=$OPTARG;;
?) echo "Invalid argument $opt"
fatal_usage;;
esac
done
shift $(($OPTIND - 1))
if [ "x$1" = "x" ]; then
echo "Missing <linux> paraemter."
fatal_usage
fi
CURDIR=`pwd`
LINUX=`getdir $1`
LINUX_BASE=`basename $LINUX`/vmlinux
AFTER=$CURDIR/after.dump
BEFORE=$CURDIR/before.dump
if [ "x$BINARY" = "x" ]; then
BINARY=vmlinux
fi
echo "Diffing $BINARY..."
BINARY0=$LINUX/$BINARY
BINARY1=$LINUX/diffvmlinux
BINARY2=$LINUX/diffvmlinux-nosections
BINARY3=$LINUX/diffvmlinux-nosection
# Prepare quilt stack
if [ "x$PATCH" != "x" ]; then
echo "Locating patch $PATCH..."
locate_patch $PATCH
else
PATCH=`detect_patch`
echo "Detected patch $PATCH."
fi
if [ "x$PATCH" = "x" ]; then
echo "Missing patch file"
fatal_usage
fi
# Allow overloading of ASMDIFF
if [ "x$ASMDIFF" = "x" ]; then
ASMDIFF=$CURDIR/bin.diff
fi
(
cd $LINUX
# Assertion in case the above locating/detecting failed.
quilt_applied $PATCH && fatal "patch $PATCH should not be applied"
quilt_push
quilt_applied $PATCH || fatal "patch $PATCH should be the next patch"
quilt_pop
echo "Building $BINARY before applying $PATCH..."
rm -f $BINARY
make -j64 $BINARY | tee $CURDIR/before.log || fatal "failed to build $LINUX"
echo "Disassembling $BINARY before applying $PATCH..."
cp -f $BINARY0 $BINARY1
cp -f $BINARY1 $BINARY2 && remove_sections $BINARY2 $BINARY3
objdump -d $BINARY2 > $BEFORE
echo "Pushing $PATCH to $LINUX_BASE..."
quilt_push
echo "Building $BINARY after applying $PATCH..."
rm -f $BINARY
make -j64 $BINARY | tee $CURDIR/after.log || fatal "failed to build $LINUX"
echo "Disassembling $BINARY after applying $PATCH..."
cp -f $BINARY0 $BINARY1
cp -f $BINARY1 $BINARY2 && remove_sections $BINARY2 $BINARY3
objdump -d $BINARY2 > $AFTER
echo "Popping $PATCH from $LINUX_BASE..."
quilt_pop
)
# XXX: Fatal Error for the Sub-shell
#
# We'll reach here even in the case of fatal due to the sub-shell.
# Thus we do cleanup here and the relative path of ASMDIFF is correct.
if [ -f $BEFORE ] && [ -f $AFTER ]; then
echo "Generating assembly differences..."
diff -u $BEFORE $AFTER > $ASMDIFF
echo "$ASMDIFF is ready."
fi
rm -f $BINARY1
rm -f $BINARY2
rm -f $BINARY3
#rm -f $BEFORE
#rm -f $AFTER