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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
"""
conflict.py - Finds conflicts between in main.dol that prevents it from matching.
"""
import sys
import logging
from pathlib import Path
from collections import defaultdict
try:
import click
from rich.logging import RichHandler
from rich.console import Console
except ImportError as e:
MISSING_PREREQUISITES = (
f"Missing prerequisite python module {e}.\n"
f"Run `python3 -m pip install --user -r tools/requirements.txt` to install prerequisites."
)
print(MISSING_PREREQUISITES, file=sys.stderr)
sys.exit(1)
class PathPath(click.Path):
def convert(self, value, param, ctx):
return Path(super().convert(value, param, ctx))
VERSION = "1.0"
CONSOLE = Console()
logging.basicConfig(
level="NOTSET",
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(console=CONSOLE, rich_tracebacks=True)],
)
LOG = logging.getLogger("rich")
LOG.setLevel(logging.INFO)
@click.group()
@click.version_option(VERSION)
def conflict():
"""Finds conflicts between in main.dol that prevents it from matching."""
pass
class ConflictException(Exception):
pass
def try_hex(value, padding):
if value == None:
return value
if not isinstance(value, int):
return value
return "0x{0:0{1}X}".format(value, padding)
def normalize_name(name):
if name == None:
return None
# literals will have different indices, thus we cannot rely on their name
if name.startswith("@") or name.startswith("lit_"):
return None
return name
def is_literal(name):
return name.startswith("@") or name.startswith("lit_")
def name_match(A, B, addr):
if A == B:
return True
elif is_literal(A) and is_literal(B):
return True
elif A == B.replace("_o_iconv_cpp", "_cpp"): # TODO: remove, not needed any more
return True
elif A == f"func_{addr:08X}":
return True
elif A == f"data_{addr:08X}":
return True
elif B == f"func_{addr:08X}":
return True
elif B == f"data_{addr:08X}":
return True
return False
#
# All
#
@conflict.command(name="all")
@click.option(
"--build_path",
"build_path",
required=False,
type=PathPath(file_okay=False, dir_okay=True),
default="build/dolzel2/",
)
@click.option(
"--expected_path",
"expected_path",
required=False,
type=PathPath(file_okay=False, dir_okay=True),
default="expected/build/dolzel2/",
)
def conflict_all(build_path, expected_path):
"""Run all conflict checks."""
try:
sections(build_path, expected_path)
except ConflictException as exception:
LOG.error(exception)
try:
symbols(build_path, expected_path)
except ConflictException as exception:
LOG.error(exception)
CONSOLE.print("no conflicts were found 😊")
#
# Sections
#
@conflict.command(name="sections")
@click.option(
"--build_path",
"build_path",
required=False,
type=PathPath(file_okay=False, dir_okay=True),
default="build/dolzel2/",
)
@click.option(
"--expected_path",
"expected_path",
required=False,
type=PathPath(file_okay=False, dir_okay=True),
default="expected/build/dolzel2/",
)
def conflict_sections(build_path, expected_path):
"""Check if there are problems with the sections in the build compared with the expected build."""
try:
sections(build_path, expected_path)
except ConflictException as exception:
LOG.error(exception)
def sections(build_path, expected_path):
import libelf
import libdol
belf_file = build_path.joinpath("main.elf")
eelf_file = expected_path.joinpath("main.elf")
# load elf
build = libelf.load_object_from_path(
belf_file, skip_symbols=True, skip_relocations=True
)
expected = libelf.load_object_from_path(
eelf_file, skip_symbols=True, skip_relocations=True
)
SECTION_NAMES = [y for x, y in libdol.NAMES_FOR_INDEX.items()]
bsection_names = [k for k in build.sections if k in SECTION_NAMES]
esection_names = [k for k in expected.sections if k in SECTION_NAMES]
if len(bsection_names) != len(esection_names):
raise ConflictException(
f"number of elf sections does not match (expected: {len(esection_names)}, got: {len(bsection_names)})"
)
for bsection_name, esection_name in zip(bsection_names, esection_names):
if bsection_name != esection_name:
raise ConflictException(
f"section names does not match (expected: '{esection_name}', got: '{bsection_name}')"
)
bsection = build.sections[bsection_name]
esection = expected.sections[esection_name]
if type(bsection) != type(esection):
raise ConflictException(
f"'{bsection_name}' section kinds does not match (expected: '{type(esection)}', got: '{type(bsection)}')"
)
if bsection.addr != esection.addr:
raise ConflictException(
f"'{bsection_name}' section addresses does not match (expected: {try_hex(esection.addr,8)}, got: {try_hex(bsection.addr,8)})"
)
if bsection.size != esection.size:
info = []
info.append(
f"'{bsection_name}' section sizes does not match (expected: {try_hex(esection.size,6)}, got: {try_hex(bsection.size,6)})"
)
if bsection.header.sh_addr != 0:
info.append(f"build section:")
info.append(f" begin: 0x{bsection.header.sh_addr:08X}")
info.append(
f" end: 0x{bsection.header.sh_addr + bsection.size:08X}"
)
if esection.header.sh_addr != 0:
info.append(f"expected section:")
info.append(f" begin: 0x{esection.header.sh_addr:08X}")
info.append(
f" end: 0x{esection.header.sh_addr + esection.size:08X}"
)
raise ConflictException("\n".join(info))
for bsection_name, esection_name in zip(bsection_names, esection_names):
bsection = build.sections[bsection_name]
esection = expected.sections[esection_name]
if bsection.data != esection.data:
position = -1
for index, tup in enumerate(zip(esection.data, bsection.data)):
if tup[0] != tup[1]:
position = index
break
info = []
if position >= 0:
info.append(f"'{bsection_name}' sections data does not match")
info.append(
f"first difference is at position {position} (0x{position:04X}) (expected: 0x{tup[0]:02X}, got: 0x{tup[1]:02X})"
)
if bsection.header.sh_addr != 0:
build_location = bsection.header.sh_addr + position
info.append(f"build location:")
info.append(f" addr: 0x{build_location:08X}")
if esection.header.sh_addr != 0:
expected_location = esection.header.sh_addr + position
info.append(f"expected location:")
info.append(f" addr: 0x{expected_location:08X}")
else:
info.append(f"could not determine the byte difference")
raise ConflictException("\n".join(info))
# TODO: more checks?
#
# symbols
#
@conflict.command(name="symbols")
@click.option(
"--build_path",
"build_path",
required=False,
type=PathPath(file_okay=False, dir_okay=True),
default="build/dolzel2/",
)
@click.option(
"--expected_path",
"expected_path",
required=False,
type=PathPath(file_okay=False, dir_okay=True),
default="expected/build/dolzel2/",
)
def conflict_symbols(build_path, expected_path):
"""Check if there are problems with the symbols in the build compared with the expected build."""
try:
symbols(build_path, expected_path)
except ConflictException as exception:
LOG.error(exception)
def symbols(build_path, expected_path):
import libelf
import libdol
belf_file = build_path.joinpath("main.elf")
eelf_file = expected_path.joinpath("main.elf")
# load elf
build = libelf.load_object_from_path(
belf_file, skip_symbols=False, skip_relocations=True
)
expected = libelf.load_object_from_path(
eelf_file, skip_symbols=False, skip_relocations=True
)
# assign section address
for _, section in build.sections.items():
if section.header.sh_addr == 0:
continue
section.addr = section.header.sh_addr
for _, section in expected.sections.items():
if section.header.sh_addr == 0:
continue
section.addr = section.header.sh_addr
# build dictionary of symbol
def strip_filter(symbol):
if isinstance(symbol, libelf.AbsoluteSymbol):
# we're not checking for conflict between absolute symbols,
# they are generated by the lcf.py script and are only temporary.
return False
if symbol.name == None:
# we only care about symbols with names
return False
return True
build_stripped_symbols = [x for x in build.symbols if strip_filter(x)]
expected_stripped_symbols = [x for x in expected.symbols if strip_filter(x)]
build_name2symbols = defaultdict(list)
for symbol in build_stripped_symbols:
build_name2symbols[symbol.name].append(symbol)
expected_name2symbols = defaultdict(list)
for symbol in expected_stripped_symbols:
expected_name2symbols[symbol.name].append(symbol)
build_addr2sym = {k.offset: k for k in build_stripped_symbols}
expected_addr2sym = {k.offset: k for k in expected_stripped_symbols}
build_symbol_address_list = list(build_addr2sym.keys())
build_symbol_address_list.sort()
check_address_set = set()
for i, symbol_addr in enumerate(build_symbol_address_list):
symbol = build_addr2sym[symbol_addr]
if not symbol.offset in expected_addr2sym:
info = []
info.append(f"symbol not found")
info.append(f" section: {symbol.getSection().name}")
info.append(f" addr: 0x{symbol.offset:08X}")
info.append(f" size: 0x{symbol.size:05X}")
info.append(f" name: {symbol.name}")
raise ConflictException("\n".join(info))
expected_symbol = expected_addr2sym[symbol.offset]
if symbol.size != expected_symbol.size:
# because of dol2asm all data elements, before they are decompiled, will include
# padding. when decompiling the padding may get removed, and thus this tool will
# report a false-positive size difference. to fix this, find the offset to the next
# symbol (in the same section) and make sure it is located at the expected location.
next_symbol = None
current_section = symbol.getSection()
i += 1 # skip current symbol
if i < len(build_symbol_address_list):
i_addr = build_symbol_address_list[i]
i_symbol = build_addr2sym[i_addr]
if i_symbol.getSection() == current_section:
next_symbol = i_symbol
false_positive = False
if next_symbol:
difference = next_symbol.offset - symbol.offset
if difference == expected_symbol.size:
false_positive = True
if not false_positive:
info = []
info.append(
f"size difference (expected: 0x{expected_symbol.size:05X}, got: 0x{symbol.size:05X})"
)
info.append(f"symbol:")
info.append(f" section: {symbol.getSection().name}")
info.append(f" addr: 0x{symbol.offset:08X}")
info.append(f" size: 0x{symbol.size:05X}")
info.append(f" name: {symbol.name}")
info.append(f"expected symbol:")
info.append(f" section: {expected_symbol.getSection().name}")
info.append(f" addr: 0x{expected_symbol.offset:08X}")
info.append(f" size: 0x{expected_symbol.size:05X}")
info.append(f" name: {expected_symbol.name}")
raise ConflictException("\n".join(info))
if not name_match(symbol.name, expected_symbol.name, symbol.offset):
info = []
info.append(
f"name difference (expected: '{expected_symbol.name}', got: '{symbol.name}')"
)
info.append(f"symbol:")
info.append(f" section: {symbol.getSection().name}")
info.append(f" addr: 0x{symbol.offset:08X}")
info.append(f" size: 0x{symbol.size:05X}")
info.append(f" name: {symbol.name}")
info.append(f"expected symbol:")
info.append(f" section: {expected_symbol.getSection().name}")
info.append(f" addr: 0x{expected_symbol.offset:08X}")
info.append(f" size: 0x{expected_symbol.size:05X}")
info.append(f" name: {expected_symbol.name}")
raise ConflictException("\n".join(info))
check_address_set.add(symbol.offset)
expected_symbol_address_list = list(expected_addr2sym.keys())
expected_symbol_address_list.sort()
for symbol_addr in expected_symbol_address_list:
if symbol_addr in check_address_set:
continue
expected_symbol = build_addr2sym[symbol_addr]
info = []
info.append(f"missing symbol")
info.append(f"expected symbol:")
info.append(f" section: {expected_symbol.getSection().name}")
info.append(f" addr: 0x{expected_symbol.offset:08X}")
info.append(f" size: 0x{expected_symbol.size:05X}")
info.append(f" name: {expected_symbol.name}")
raise ConflictException("\n".join(info))
#
#
#
if __name__ == "__main__":
conflict()
|