-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_bss.py
executable file
·208 lines (148 loc) · 6.86 KB
/
check_bss.py
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
#!/usr/bin/env python3
import colorama
colorama.init()
import argparse
import os
import re
import collections
regex_fileDataEntry = re.compile(r"^\s+(?P<section>[^\s]+)\s+(?P<vram>0x[^\s]+)\s+(?P<size>0x[^\s]+)\s+(?P<name>[^\s]+)$")
regex_bssEntry = re.compile(r"^\s+(?P<vram>0x[^\s]+)\s+(?P<name>[^\s]+)$")
regex_label = re.compile(r"^(?P<name>L[0-9A-F]{8})$")
File = collections.namedtuple("File", ["name", "vram", "bssVariables"])
FileEntry = collections.namedtuple("File", ["vram", "bssVariables"])
Variable = collections.namedtuple("Variable", ["name", "vram"])
Compared = collections.namedtuple("Compared", ["expected", "build", "diff"])
def parseMapFile(mapPath: str):
with open(mapPath) as f:
mapData = f.read()
startIndex = mapData.find("..makerom")
mapData = mapData[startIndex:]
# print(len(mapData))
filesList = list()
inFile = False
mapLines = mapData.split("\n")
for line in mapLines:
if inFile:
if line.startswith(" "):
entryMatch = regex_bssEntry.search(line)
# Find function
if entryMatch is not None:
varName = entryMatch["name"]
varVram = int(entryMatch["vram"], 16)
# Filter out jump table labels
labelMatch = regex_label.search(varName)
if labelMatch is None:
filesList[-1].bssVariables[varName] = varVram # append(Variable(varName, varVram))
# print(hex(funcVram), funcName)
else:
inFile = False
else:
if line.startswith(" .bss "):
inFile = False
entryMatch = regex_fileDataEntry.search(line)
# Find file
if entryMatch is not None:
name = "/".join(entryMatch["name"].split("/")[2:])
name = ".".join(name.split(".")[:-1])
size = int(entryMatch["size"], 16)
vram = int(entryMatch["vram"], 16)
if size > 0:
inFile = True
filesList.append(File(name, vram, collections.OrderedDict()))
resultFileDict = dict()
for file in filesList:
bssCount = len(file.bssVariables)
# Filter out files with no bss
if bssCount == 0:
continue
resultFileDict[file.name] = FileEntry(file.vram, file.bssVariables)
return resultFileDict
def compareMapFiles(mapFileBuild: str, mapFileExpected: str):
isOkay = dict()
print("Build mapfile: " + mapFileBuild, file=os.sys.stderr)
print("Expected mapfile: " + mapFileExpected, file=os.sys.stderr)
print("", file=os.sys.stderr)
buildMap = parseMapFile(mapFileBuild)
expectedMap = parseMapFile(mapFileExpected)
comparedDict = collections.OrderedDict()
for fileName in expectedMap:
if not fileName in buildMap:
print(f"File '{fileName}' not found in '{mapFileBuild}'. Has it been renamed?", file=os.sys.stderr)
continue
# print(fileName)
# print(buildMap[fileName])
# print(expectedMap[fileName])
# print("")
comparedDict[fileName] = collections.OrderedDict()
isOkay[fileName] = True
for symbol in expectedMap[fileName].bssVariables:
if not symbol in buildMap[fileName].bssVariables:
print(f"Variable {symbol} not found in {mapFileBuild}, file {fileName} . Has it been renamed?", file=os.sys.stderr)
continue
expectedAddress = expectedMap[fileName].bssVariables[symbol]
buildAddress = buildMap[fileName].bssVariables[symbol]
comparedDict[fileName][symbol] = Compared( expectedAddress , buildAddress , buildAddress - expectedAddress )
isOkay[fileName] &= (comparedDict[fileName][symbol].diff == 0)
# print(f"comparedDict: {comparedDict[fileName]}")
# print("")
# print(f"Is okay: {isOkay[fileName]}")
# print("")
return isOkay, comparedDict
def printCsv(isOkay, comparedDict, printAll = True):
print("File,Symbol Name,Expected,Build,Difference,GOOD/BAD")
allGood = True
for file in comparedDict:
allGood &= isOkay[file]
if isOkay[file]:
if not printAll:
continue
fileSymbols = comparedDict[file]
for symbol in fileSymbols:
addresses = fileSymbols[symbol]
symbolGood = colorama.Fore.RED + "BAD"
if addresses.diff == 0:
symbolGood = colorama.Fore.GREEN + "GOOD"
symbolGood += colorama.Style.RESET_ALL
print(f"{file},{symbol},{addresses.expected:X},{addresses.build:X},{addresses.diff:X},{symbolGood}")
# print("")
return allGood
def main():
description = "Check that bss has not been reordered"
# TODO
epilog = """\
"""
parser = argparse.ArgumentParser(description=description, epilog=epilog, formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("mapfile", help="Path to a map file.")
parser.add_argument("mapFileExpected", help="Path to the expected map file.")
parser.add_argument("-a", "--print-all", help="Print all bss, not just non-matching.", action="store_true")
parser.add_argument("-n", "--no-fun-allowed", help="Remove amusing messages.", action="store_true")
args = parser.parse_args()
isOkay, comparedDict = compareMapFiles(args.mapfile, args.mapFileExpected)
if printCsv(isOkay, comparedDict, args.print_all):
print(colorama.Fore.LIGHTGREEN_EX + " GOOD" + colorama.Style.RESET_ALL)
if args.no_fun_allowed:
return 0
print("\n" + colorama.Fore.LIGHTWHITE_EX +
colorama.Back.RED + " " + colorama.Back.BLACK + "\n" +
colorama.Back.RED + " CONGRATURATIONS! " + colorama.Back.BLACK + "\n" +
colorama.Back.RED + " All BSS is correct. " + colorama.Back.BLACK + "\n" +
colorama.Back.RED + " THANK YOU! " + colorama.Back.BLACK + "\n" +
colorama.Back.RED + " You are great decomper! " + colorama.Back.BLACK + "\n" +
colorama.Back.RED + " " + colorama.Style.RESET_ALL , file=os.sys.stderr)
return 0
else:
print("\n" + colorama.Fore.LIGHTRED_EX + "BAD" + colorama.Style.RESET_ALL, file=os.sys.stderr)
badFiles = []
for file in isOkay:
if not isOkay[file]:
badFiles.append(" ReOrdering in " + file)
print("\n ".join(badFiles), file=os.sys.stderr)
if args.no_fun_allowed:
return 1
print(colorama.Fore.LIGHTWHITE_EX +
" BSS is REORDERED!!\n"
" Oh! MY GOD!!"
+ colorama.Style.RESET_ALL, file=os.sys.stderr)
return 1
if __name__ == "__main__":
main()