forked from justinwash/sm64-decomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diff.sh
414 lines (362 loc) · 12.3 KB
/
diff.sh
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/env bash
set -e
# ==== CONFIG ====
DIFF_OBJ=0
IGNORE_REGS=0
MAKE=0
DIFF_ARGS="-l"
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case "$1" in
-o)
# Diff .o files rather than a whole binary. This makes it possible to
# see relocations, which is helpful for navigating in the diff.
DIFF_OBJ=1
shift
;;
-m)
# Run "make" on the .o file or binary before diffing.
MAKE=1
shift
;;
-r)
IGNORE_REGS=1
shift
;;
-s)
DIFF_ARGS+=" --stop-jr-ra"
shift
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
# Set $BASEIMG, $BASEDUMP, $MYIMG, $MYDUMP, $MAPFILE, $MAKEFLAGS in a project-specific manner.
. diff-settings.sh
# ==== LOGIC ====
if [[ $# -lt 1 ]]; then
echo "Usage: ./diff.sh [flags] (function|rom addr) [end rom addr]" >&2
exit 1
fi
PYTHON_VERSION=$(python3 --version | cut -d'.' -f2)
if [[ $PYTHON_VERSION -lt 6 ]]; then
echo "$0 requires at least Python 3.6" >&2
exit 1
fi
START="$1"
BASE=0
set +e
if [ -n "$MAPFILE" ]; then
LINE=$(grep "$1$" $MAPFILE)
if [[ -n "$LINE" && "${1:0:2}" != "0x" ]]; then
START=$(echo $LINE | cut -d' ' -f1)
if [[ $DIFF_OBJ = 1 ]]; then
LINE2=$(grep "$1$\|^ .text" $MAPFILE | grep "$1$" -B1 | head -n1)
OBJFILE=$(echo $LINE2 | cut -d' ' -f4)
else
LINE2=$(grep "$1$\|load address" $MAPFILE | grep "$1$" -B1 | head -n1)
RAM=$(echo $LINE2 | cut -d' ' -f2)
ROM=$(echo $LINE2 | cut -d' ' -f6)
BASE="$RAM - $ROM"
fi
fi
fi
set -e
if [[ $DIFF_OBJ = 1 ]]; then
if [[ $MAKE = 1 ]]; then
make $MAKEFLAGS "$OBJFILE"
fi
if [[ ! -f "$OBJFILE" ]]; then
echo Not able to find .o file for function.
exit 1
fi
REFOBJFILE="expected/$OBJFILE"
if [[ ! -f "$REFOBJFILE" ]]; then
echo Please ensure an OK .o file exists at "$REFOBJFILE".
exit 1
fi
OBJDUMP="mips-linux-gnu-objdump -drz"
$OBJDUMP $REFOBJFILE | grep "<$1>:" -A1000 > $BASEDUMP
$OBJDUMP $OBJFILE | grep "<$1>:" -A1000 > $MYDUMP
DIFF_ARGS+=" -o"
else
if [[ $MAKE = 1 ]]; then
make $MAKEFLAGS "$MYIMG"
fi
END="$START + 0x1000"
if [[ $# -ge 2 ]]; then
END="$2"
fi
OBJDUMP="mips-linux-gnu-objdump -D -z -bbinary -mmips -EB"
OPTIONS="--start-address=$(($START - ($BASE))) --stop-address=$(($END - ($BASE)))"
$OBJDUMP $OPTIONS $BASEIMG > $BASEDUMP
$OBJDUMP $OPTIONS $MYIMG > $MYDUMP
fi
set +e
# sed -i "1s;^;$(sha1sum $MYDUMP)\n;" $MYDUMP
read -r -d '' DIFF_SCRIPT << EOM
try:
import argparse
import attr
from difflib import SequenceMatcher
from pathlib import Path
import itertools
from colorama import Fore, Style, Back
import ansiwrap
import re
import string
from signal import signal, SIGPIPE, SIG_DFL
except ModuleNotFoundError as e:
# Exit nicely and print to stdout, because less will be sad otherwise.
print("Missing prerequisite python module " + e.name +
". Run \`python3 -m pip install --user colorama ansiwrap attrs\` to install prerequisites.")
exit(0)
# Fixes pipe error
signal(SIGPIPE,SIG_DFL)
# Alignment with ANSI colors is just broken, let's fix it.
def ansi_ljust(s, width):
needed = width - ansiwrap.ansilen(s)
if needed > 0:
return s + ' ' * needed
else:
return s
@attr.s
class Options:
file1: str = attr.ib()
file2: str = attr.ib()
diff_obj: bool = attr.ib()
line_nums: bool = attr.ib()
reg_diff: bool = attr.ib()
column_width: int = attr.ib()
stop_jrra: bool = attr.ib()
skip_bl_delay: bool = attr.ib()
r = re.compile(r'[0-9]+')
comments = re.compile(r'<.*?>')
regs = re.compile(r'\b(a[0-3]|t[0-9]|s[0-7]|at|v[01])\b')
sprel = re.compile(r',([1-9][0-9]*|0x[1-9a-f][0-9a-f]*)\(sp\)')
forbidden = set(string.ascii_letters + '_')
branch_likely_instructions = [
'beql', 'bnel', 'beqzl', 'bnezl', 'bgezl', 'bgtzl', 'blezl', 'bltzl',
'bc1tl', 'bc1fl'
]
branch_instructions = [
'b', 'beq', 'bne', 'beqz', 'bnez', 'bgez', 'bgtz', 'blez', 'bltz',
'bc1t', 'bc1f'
] + branch_likely_instructions
def fn(row, pat):
full = pat.group(0)
if len(full) <= 1:
return full
start, end = pat.span()
if start and row[start - 1] in forbidden:
return full
if end < len(row) and row[end] in forbidden:
return full
return hex(int(full))
def parse_relocated_line(line):
try:
ind2 = line.rindex(',')
except ValueError:
ind2 = line.rindex('\t')
before = line[:ind2+1]
after = line[ind2+1:]
ind2 = after.find('(')
if ind2 == -1:
imm, after = after, ''
else:
imm, after = after[:ind2], after[ind2:]
if imm == '0x0':
imm = '0'
return before, imm, after
def process_reloc(row, prev):
before, imm, after = parse_relocated_line(prev)
repl = row.split()[-1]
if imm != '0':
if before.strip() == 'jal' and not imm.startswith('0x'):
imm = '0x' + imm
repl += '+' + imm if int(imm,0) > 0 else imm
if 'R_MIPS_LO16' in row:
repl = f'%lo({repl})'
elif 'R_MIPS_HI16' in row:
# Ideally we'd pair up R_MIPS_LO16 and R_MIPS_HI16 to generate a
# correct addend for each, but objdump doesn't give us the order of
# the relocations, so we can't find the right LO16. :(
repl = f'%hi({repl})'
else:
assert 'R_MIPS_26' in row, f"unknown relocation type '{row}'"
return before + repl + after
def process(lines, options):
diff_rows = []
skip_next = False
originals = []
line_nums = []
skip_lines = 1 if options.diff_obj else 7
for index, row in enumerate(lines):
if index < skip_lines:
continue
if options.diff_obj and ('>:' in row or not row):
continue
if 'R_MIPS_' in row:
if diff_rows[-1] == '<skipped>':
continue
diff_rows[-1] = process_reloc(row, diff_rows[-1])
originals[-1] = process_reloc(row, originals[-1])
continue
row = re.sub(comments, '', row)
row = row.rstrip()
tabs = row.split('\t')
row = '\t'.join(tabs[2:]) # [20:]
line_num = tabs[0].strip()
original = row
mnemonic = row.split('\t')[0].strip()
if mnemonic not in branch_instructions:
row = re.sub(r, lambda s: fn(row, s), row)
if skip_next:
skip_next = False
row = '<skipped>'
if mnemonic in branch_likely_instructions and options.skip_bl_delay:
skip_next = True
if options.reg_diff:
row = re.sub(regs, '<reg>', row)
row = re.sub(sprel, ',addr(sp)', row)
# Replace tabs with spaces
diff_rows.append(row)
originals.append(original)
line_nums.append(line_num)
if options.stop_jrra and mnemonic == 'jr' and row.split('\t')[1].strip() == 'ra':
break
# Cleanup whitespace
originals = [original.strip() for original in originals]
originals = [''.join(f'{o:<8s}' for o in original.split('\t')) for original in originals]
return diff_rows, originals, line_nums
regs_after = re.compile(r'<reg>')
def print_single_line_diff(line1, line2, column_width):
print(f"{ansi_ljust(line1,column_width)}{ansi_ljust(line2,column_width)}")
color_rotation = [
Fore.MAGENTA,
Fore.CYAN,
Fore.GREEN,
Fore.RED,
Fore.LIGHTYELLOW_EX,
Fore.LIGHTMAGENTA_EX,
Fore.LIGHTCYAN_EX,
Fore.LIGHTGREEN_EX,
#Fore.LIGHTRED_EX, (This is hard to distinguish (I'm not even colorblind))
Fore.LIGHTBLACK_EX,
]
color_index = [0, 0]
symbol_colors = [{}, {}]
def color_symbol(s, i):
global color_rotation
global color_index
global symbol_colors
s = s.group()
try:
color = symbol_colors[i][s]
except:
color = color_rotation[color_index[i]]
color_index[i] = (color_index[i] + 1) % len(color_rotation)
symbol_colors[i][s] = color
return f'{color}{s}{Fore.RESET}'
def main(options):
asm1: str = Path(options.file1).read_text()
asm2: str = Path(options.file2).read_text()
asm1_lines = asm1.split('\n')
asm2_lines = asm2.split('\n')
asm1_lines, originals1, line_nums1 = process(asm1_lines, options)
asm2_lines, originals2, line_nums2 = process(asm2_lines, options)
differ: SequenceMatcher = SequenceMatcher(a=asm1_lines, b=asm2_lines, autojunk=True)
for (tag, i1, i2, j1, j2) in differ.get_opcodes():
lines1 = asm1_lines[i1:i2]
lines2 = asm2_lines[j1:j2]
for k, (line1, line2) in enumerate(itertools.zip_longest(lines1, lines2)):
if tag == 'replace':
if line1 == None:
tag = 'insert'
elif line2 == None:
tag = 'delete'
try:
original1 = originals1[i1+k]
line_num1 = line_nums1[i1+k]
except:
original1 = ''
line_num1 = ''
try:
original2 = originals2[j1+k]
line_num2 = line_nums2[j1+k]
except:
original2 = ''
line_num2 = ''
line_color = Fore.RESET
line_prefix = ' '
if tag == 'equal' or line1 == line2:
if (line1 == '<skipped>'):
pass
elif original1 != original2 and options.reg_diff:
line_color = Fore.YELLOW
line_prefix = 'r'
line1 = f'{Fore.YELLOW}{original1}{Style.RESET_ALL}'
line2 = f'{Fore.YELLOW}{original2}{Style.RESET_ALL}'
line1 = re.sub(regs, lambda s: color_symbol(s, 0), line1)
line2 = re.sub(regs, lambda s: color_symbol(s, 1), line2)
line1 = re.sub(sprel, lambda s: color_symbol(s, 0), line1)
line2 = re.sub(sprel, lambda s: color_symbol(s, 1), line2)
else:
line1 = f'{original1}'
line2 = f'{original2}'
elif tag == 'replace':
line_prefix = '|'
line_color = Fore.BLUE
line1 = f"{Fore.BLUE}{original1}{Style.RESET_ALL}"
line2 = f"{Fore.BLUE}{original2}{Style.RESET_ALL}"
elif tag == 'delete':
line_prefix = '<'
line_color = Fore.RED
line1 = f"{Fore.RED}{original1}{Style.RESET_ALL}"
elif tag == 'insert':
line_prefix = '>'
line_color = Fore.GREEN
line2 = f"{Fore.GREEN}{original2}{Style.RESET_ALL}"
line1 = line1 or ''
line2 = line2 or ''
line_num1 = line_num1 if line1 else ''
line_num2 = line_num2 if line2 else ''
if not options.line_nums:
line_num1 = ''
line_num2 = ''
line1 = f"{line_color}{line_num1} {line1}{Style.RESET_ALL}"
line2 = f"{line_color}{line_prefix} {line_num2} {line2}{Style.RESET_ALL}"
print_single_line_diff(line1, line2, options.column_width)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Diff mips data")
parser.add_argument('file1',
help="The base file to compare")
parser.add_argument('file2',
help="The modified version to compare to")
parser.add_argument('-o', dest='diff_obj', action='store_true',
help="Perform an object file diff")
parser.add_argument('-l', dest='line_nums', action='store_true',
help="Show line numbers")
parser.add_argument('--stop-jr-ra', dest='stop_jrra', action='store_true',
help="Stop at the first 'jr ra'")
parser.add_argument('--column', dest='column_width', type=int, default=50,
help="Sets the width of the left and right view column")
args = parser.parse_args()
options = Options(
file1 = args.file1,
file2 = args.file2,
reg_diff = True,
line_nums = args.line_nums,
diff_obj = args.diff_obj,
column_width = args.column_width,
stop_jrra = args.stop_jrra,
skip_bl_delay = True
)
main(options)
EOM
set -e
python3 -c "$DIFF_SCRIPT" "$BASEDUMP" "$MYDUMP" $DIFF_ARGS | less -Ric