-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobal_bss_check.py
executable file
·234 lines (172 loc) · 9.44 KB
/
global_bss_check.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
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
#!/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"])
VarInfo = collections.namedtuple("Variable", ["file", "vram"])
def parseMapFile(mapPath: str):
with open(mapPath) as f:
mapData = f.read()
startIndex = mapData.find("..makerom")
mapData = mapData[startIndex:]
# print(len(mapData))
filesList = list()
symbolsDict = collections.OrderedDict()
inFile = False
currentFile = ""
mapLines = mapData.split("\n")
for line in mapLines:
if inFile:
if line.startswith(" "):
entryMatch = regex_bssEntry.search(line)
# Find variable
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:
symbolsDict[varName] = VarInfo( currentFile, varVram )
# print( symbolsDict[varName] )
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("/")[1:])
# mapfile only contains .o files, so just strip the last character to replace it
# we assume all the .c files are in the src folder, and all others are .s (true for OoT/MM)
if name.split("/")[0] == "src":
name = name[:-1] + "c"
else:
name = name[:-1] + "s"
size = int(entryMatch["size"], 16)
vram = int(entryMatch["vram"], 16)
if size > 0:
inFile = True
currentFile = name
# print(symbolsDict)
# 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 symbolsDict
Compared = collections.namedtuple("Compared", [ "buildAddress", "buildFile", "expectedAddress", "expectedFile", "diff"])
def compareMapFiles(mapFileBuild: str, mapFileExpected: str):
badFiles = set()
missingFiles = set()
print("Build mapfile: " + mapFileBuild, file=os.sys.stderr)
print("Expected mapfile: " + mapFileExpected, file=os.sys.stderr)
print("", file=os.sys.stderr)
if not os.path.exists(mapFileBuild):
print(f"{colorama.Fore.LIGHTRED_EX}error{colorama.Fore.RESET}: mapfile not found at {mapFileBuild}. Did you enter the correct path?", file=os.sys.stderr)
exit(1)
if not os.path.exists(mapFileExpected):
print(f"{colorama.Fore.LIGHTRED_EX}error{colorama.Fore.RESET}: expected mapfile not found at {mapFileExpected}. Is 'expected' missing or in a different folder?", file=os.sys.stderr)
exit(1)
buildMap = parseMapFile(mapFileBuild)
expectedMap = parseMapFile(mapFileExpected)
comparedDict = collections.OrderedDict()
for symbol in buildMap:
if symbol in expectedMap:
comparedDict[symbol] = Compared( buildMap[symbol].vram, buildMap[symbol].file, expectedMap[symbol].vram, expectedMap[symbol].file, buildMap[symbol].vram - expectedMap[symbol].vram )
if comparedDict[symbol].diff != 0:
badFiles.add(buildMap[symbol].file)
else:
missingFiles.add(buildMap[symbol].file)
comparedDict[symbol] = Compared( buildMap[symbol].vram, buildMap[symbol].file, -1, "", "Unknown" )
for symbol in expectedMap:
if not symbol in buildMap:
missingFiles.add(expectedMap[symbol].file)
comparedDict[symbol] = Compared( -1, "", expectedMap[symbol].vram, expectedMap[symbol].file, "Unknown" )
return badFiles, missingFiles, comparedDict
def printCsv(badFiles, missingFiles, comparedDict, printAll = True):
print("Symbol Name,Build Address,Build File,Expected Address,Expected File,Difference,GOOD/BAD/MISSING")
allGood = True
allFound = True
problemFiles = set.union(badFiles, missingFiles)
# If it's bad or missing, don't need to do anything special.
# If it's good, check for if it's in a file with bad or missing stuff, and check if print all is on. If none of these, print it.
for symbol in comparedDict:
symbolInfo = comparedDict[symbol]
symbolGood = colorama.Fore.RED + "BAD" + colorama.Fore.RESET
if type(symbolInfo.diff) != int:
symbolGood = colorama.Fore.YELLOW + "MISSING" + colorama.Fore.RESET
print(f"{symbol},{symbolInfo.buildAddress:X},{symbolInfo.buildFile},{symbolInfo.expectedAddress:X},{symbolInfo.expectedFile},{symbolInfo.diff},{symbolGood}")
continue
if symbolInfo.diff == 0:
symbolGood = colorama.Fore.GREEN + "GOOD" + colorama.Fore.RESET
if (not symbolInfo.buildFile in badFiles and not symbolInfo.expectedFile in badFiles) and (not symbolInfo.buildFile in badFiles and not symbolInfo.expectedFile in badFiles) and not printAll:
continue
if symbolInfo.buildFile != symbolInfo.expectedFile:
symbolGood += colorama.Fore.CYAN + " MOVED" + colorama.Fore.RESET
print(f"{symbol},{symbolInfo.buildAddress:X},{symbolInfo.buildFile},{symbolInfo.expectedAddress:X},{symbolInfo.expectedFile},{symbolInfo.diff:X},{symbolGood}")
def main():
description = "Check that globally visible bss has not been reordered."
# TODO
epilog = """\
N.B. Since this script reads the map files, it can only see globally visible bss; in-function static bss must be examined with other tools.
"""
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. Optional, default is 'expected/mapFile'.", nargs="?", default="")
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()
if args.mapFileExpected == "":
args.mapFileExpected = os.path.join("expected", args.mapFile)
badFiles, missingFiles, comparedDict = compareMapFiles(args.mapFile, args.mapFileExpected)
printCsv(badFiles, missingFiles, comparedDict, args.print_all)
if len(badFiles) + len(missingFiles) != 0:
print("", file=os.sys.stderr)
if len(badFiles) != 0:
print(colorama.Fore.RED + " BAD" + colorama.Style.RESET_ALL)
for file in badFiles:
print(f"bss reordering in {file}", file=os.sys.stderr)
print("", file=os.sys.stderr)
if not args.no_fun_allowed:
print(colorama.Fore.LIGHTWHITE_EX +
" BSS is REORDERED!!\n"
" Oh! MY GOD!!"
+ colorama.Style.RESET_ALL, file=os.sys.stderr)
print("", file=os.sys.stderr)
if len(missingFiles) != 0:
print(colorama.Fore.YELLOW + " MISSING" + colorama.Style.RESET_ALL)
for file in missingFiles:
print(f"Symbols missing from {file}", file=os.sys.stderr)
print("", file=os.sys.stderr)
if not args.no_fun_allowed:
print(colorama.Fore.LIGHTWHITE_EX + " Error, should (not) be in here " + colorama.Style.RESET_ALL, file=os.sys.stderr)
print("", file=os.sys.stderr)
print("Some files appear to be missing symbols. Have they been renamed or declared as static? You may need to remake 'expected'", file=os.sys.stderr)
if len(badFiles) + len(missingFiles) != 0:
return 1
print("", file=os.sys.stderr)
print(colorama.Fore.GREEN + " GOOD" + colorama.Style.RESET_ALL, file=os.sys.stderr)
if args.no_fun_allowed:
return 0
print("\n" + colorama.Fore.LIGHTWHITE_EX +
colorama.Back.RED + " " + colorama.Back.RESET + "\n" +
colorama.Back.RED + " CONGRATURATIONS! " + colorama.Back.RESET + "\n" +
colorama.Back.RED + " All global BSS is correct. " + colorama.Back.RESET + "\n" +
colorama.Back.RED + " THANK YOU! " + colorama.Back.RESET + "\n" +
colorama.Back.RED + " You are great decomper! " + colorama.Back.RESET + "\n" +
colorama.Back.RED + " " + colorama.Style.RESET_ALL , file=os.sys.stderr)
return 0
if __name__ == "__main__":
main()