forked from UsernameFodder/pmdsky-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol_check.py
executable file
·272 lines (238 loc) · 9.6 KB
/
symbol_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
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
#!/usr/bin/env python3
# Script to check that all function names present in the C headers are also
# present in the symbol tables.
from abc import ABC, abstractmethod
import argparse
import difflib
import os
import re
from typing import Dict, Generator, List, Optional
import yaml
ROOT_DIR = os.path.relpath(os.path.join(os.path.dirname(__file__), ".."))
SYMBOLS_DIR = os.path.join(ROOT_DIR, "symbols")
def get_headers(header_dir: str) -> Generator[str, None, None]:
for root, dirs, files in os.walk(header_dir):
dirs.sort() # Ensure subdirectories are visited in sorted order
for f in sorted(files):
if f.endswith(".h"):
yield os.path.join(root, f)
class HeaderSymbolList(ABC):
"""
A list of all symbols of some type defined for a specific binary file,
including the C headers and the symbol tables.
"""
HEADERS_DIR: str # Path to directory containing the C headers
SYMBOL_LIST_KEY: str # YAML key of the corresponding symbol list
header_file: str
symbol_file: str
# Caches to avoid doing unnecessary file I/O
cached_header_names: Optional[List[str]]
cached_symbol_names: Optional[List[str]]
def __init__(self, header_file: str):
assert hasattr(self, "HEADERS_DIR")
assert hasattr(self, "SYMBOL_LIST_KEY")
self.cached_header_names = None
self.cached_symbol_names = None
self.header_file = header_file
symbol_file = self.get_symbol_file(self.header_file)
if symbol_file is not None:
self.symbol_file = symbol_file
else:
raise ValueError(
f"Header file '{header_file}' has no corresponding symbol file"
)
def __str__(self) -> str:
return self.file_stem(self.header_file)
@classmethod
def file_stem(cls, header_file: str) -> str:
return os.path.splitext(os.path.relpath(header_file, start=cls.HEADERS_DIR))[0]
@classmethod
def headers(cls) -> Generator[str, None, None]:
for root, dirs, files in os.walk(cls.HEADERS_DIR):
dirs.sort() # Ensure subdirectories are visited in sorted order
for f in sorted(files):
if f.endswith(".h"):
yield os.path.join(root, f)
@classmethod
def get_symbol_file(cls, header_file: str) -> Optional[str]:
"""
Get the symbol file name that corresponds to the given header file.
"""
fname = os.path.join(SYMBOLS_DIR, cls.file_stem(header_file) + ".yml")
return fname if os.path.isfile(fname) else None
@staticmethod
def header_file_strip_comments(contents: str) -> str:
# Remove "//" comments
contents = re.sub(r"//[^\n]*\n", "\n", contents)
# Remove "/* */" comments
contents = re.sub(r"/\*.*?\*/", "", contents, flags=re.DOTALL)
return contents
@abstractmethod
def _names_from_header_file(self) -> List[str]:
return []
def names_from_header_file(self) -> List[str]:
if self.cached_header_names is None:
self.cached_header_names = self._names_from_header_file()
return list(self.cached_header_names)
def names_from_symbol_file(self) -> List[str]:
if self.cached_symbol_names is None:
with open(self.symbol_file, "r") as f:
self.cached_symbol_names = [
symbol["name"]
for block in yaml.safe_load(f).values()
for symbol in block[self.SYMBOL_LIST_KEY]
]
return list(self.cached_symbol_names)
def missing_symbols(self) -> List[str]:
"""
Find symbols that are in the C headers but not in the symbol tables.
"""
return list(
set(self.names_from_header_file()) - set(self.names_from_symbol_file())
)
def extra_symbols(self) -> List[str]:
"""
Find symbols that are in the symbol tables but not in the C headers.
"""
return list(
set(self.names_from_symbol_file()) - set(self.names_from_header_file())
)
def order_diff(self) -> List[str]:
"""
Return a diff between the symbol tables and the C headers, including only
the symbols that are in both.
"""
symbol_names = self.names_from_symbol_file()
header_names = self.names_from_header_file()
common_symbols = set(symbol_names) & set(header_names)
symbol_order = [name for name in symbol_names if name in common_symbols]
header_order = [name for name in header_names if name in common_symbols]
return list(
difflib.unified_diff(
header_order,
symbol_order,
fromfile=f"[old] {self.header_file}",
tofile=f"[new] {self.header_file} (matches {self.symbol_file})",
n=1,
lineterm="",
)
)
class FunctionList(HeaderSymbolList):
HEADERS_DIR = os.path.join(ROOT_DIR, "headers", "functions")
SYMBOL_LIST_KEY = "functions"
def _names_from_header_file(self) -> List[str]:
with open(self.header_file, "r") as f:
# Look for words preceding an open parenthesis. Not perfect, but
# good enough. This should work for everything except functions
# with function pointer parameters, but function pointer parameters
# should really be typedef'd for readability anyway.
return re.findall(
r"\b(\w+)\s*\(", self.header_file_strip_comments(f.read())
)
class DataList(HeaderSymbolList):
HEADERS_DIR = os.path.join(ROOT_DIR, "headers", "data")
SYMBOL_LIST_KEY = "data"
def _names_from_header_file(self) -> List[str]:
with open(self.header_file, "r") as f:
# Look for words preceding a semicolon, with optional closing
# parentheses and/or pairs of square brackets in between. This
# should work for most things, like:
# - normal variables `int x;`
# - arrays `int x[10];`
# - array pointers `int (*x)[10]`
# It won't work for function pointers `int (*f)(int, int)`, but
# these should really be typedef'd for readability anyway.
return re.findall(
r"\b(\w+)(?:\s*(?:\)|\[\s*\w+\s*\]))*\s*;",
self.header_file_strip_comments(f.read()),
)
def run_symbol_check(
symbol_list: HeaderSymbolList,
symbol_type_str: str,
find_extra_symbols: bool = False,
check_order: bool = False,
verbose: bool = False,
) -> bool:
passed = True
for header_file in symbol_list.headers():
try:
slist = symbol_list(header_file)
missing = slist.missing_symbols()
extra = slist.extra_symbols() if find_extra_symbols else []
order_diff = slist.order_diff() if check_order else []
if missing:
passed = False
print(
f"{slist}: found {len(missing)} discrepancies between"
+ " C headers and symbol tables."
)
print(
f"The following {symbol_type_str} are present in"
+ f" {slist.header_file} but missing from"
+ f" {slist.symbol_file}:"
)
for symbol_name in missing:
print(f" - {symbol_name}")
print()
if extra:
print(f"{slist}: found {len(extra)} untyped symbols in symbol tables")
print(
f"The following {symbol_type_str} are present in"
+ f" {slist.symbol_file} but missing from"
+ f" {slist.header_file}:"
)
for symbol_name in extra:
print(f" - {symbol_name}")
print()
if order_diff:
passed = False
print(
f"{slist}: found ordering discrepancies between"
+ f" {symbol_type_str} in the C headers and symbol tables."
)
print(
"To match the symbol tables, make the following changes"
+ " to the C headers:"
)
print("\n".join(order_diff))
print()
if not missing and not extra and not order_diff and verbose:
print(f"Checked {header_file}")
except ValueError:
# File doesn't correspond to a symbol file; skip
pass
return passed
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Cross-check the C headers with the symbol tables"
)
parser.add_argument(
"-e",
"--extra-symbols",
action="store_true",
help="print extra symbols present in the symbol tables but not in the C headers",
)
parser.add_argument(
"-s",
"--sort-order",
action="store_true",
help="check sort order in the C headers based on symbol table order",
)
parser.add_argument("-v", "--verbose", action="store_true", help="verbose output")
args = parser.parse_args()
functions_passed = run_symbol_check(
FunctionList,
"functions",
find_extra_symbols=args.extra_symbols,
check_order=args.sort_order,
verbose=args.verbose,
)
data_passed = run_symbol_check(
DataList,
"data symbols",
find_extra_symbols=args.extra_symbols,
check_order=args.sort_order,
verbose=args.verbose,
)
if not functions_passed or not data_passed:
raise SystemExit(1)