-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsym_info.py
More file actions
executable file
·160 lines (134 loc) · 4.79 KB
/
sym_info.py
File metadata and controls
executable file
·160 lines (134 loc) · 4.79 KB
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
#!/usr/bin/env python3
import argparse
import os.path
# Not actually macros, just look like them
_GAME_DIR_TEMPLATE = "{game}"
_VERSION_DIR_TEMPLATE = os.path.join(_GAME_DIR_TEMPLATE, "{version}")
_ROM_NAME_TEMPLATE = "{game}_{version}_uncompressed.z64"
_BASEROM_NAME_TEMPLATE = "baserom_" + _ROM_NAME_TEMPLATE
_BUILT_MAP_RELPATH_TEMPLATE = os.path.join("build", "{game}_{version}.map")
_EXPECTED_MAP_RELPATH_TEMPLATE = os.path.join("expected", _BUILT_MAP_RELPATH_TEMPLATE)
# The ones you should actually use
ROM_PATH_TEMPLATE = _ROM_NAME_TEMPLATE
BASEROM_PATH_TEMPLATE = os.path.join(_GAME_DIR_TEMPLATE, _BASEROM_NAME_TEMPLATE)
BUILT_MAP_PATH_TEMPLATE = os.path.join(_VERSION_DIR_TEMPLATE, _BUILT_MAP_RELPATH_TEMPLATE)
EXPECTED_MAP_PATH_TEMPLATE = os.path.join(_VERSION_DIR_TEMPLATE, _EXPECTED_MAP_RELPATH_TEMPLATE)
parser = argparse.ArgumentParser(
description="Display various information about a symbol or address."
)
parser.add_argument(
"name",
type=str,
default="",
help="symbol name or VROM/VRAM address to lookup"
)
parser.add_argument(
"-e",
"--expected",
dest="use_expected",
action="store_true",
help="use the map file in expected/build/ instead of build/"
)
parser.add_argument("--game", help="game folder to use", default="oot")
parser.add_argument("--version", help="version to use", default="ne0")
args = parser.parse_args()
game_version = {
"game": args.game,
"version": args.version,
}
mymap = BUILT_MAP_PATH_TEMPLATE.format(**game_version)
if args.use_expected:
mymap = EXPECTED_MAP_PATH_TEMPLATE.format(**game_version)
if not os.path.isfile(mymap):
print(f"{mymap} must exist.")
exit(1)
def search_address(target_addr):
is_ram = target_addr & 0x80000000
ram_offset = None
prev_ram = 0
prev_rom = 0
prev_sym = "<start of rom>"
cur_file = "<no file>"
prev_file = cur_file
prev_line = ""
with open(mymap) as f:
for line in f:
if "load address" in line:
# Ignore .bss sections if we're looking for a ROM address
if not is_ram and (".bss" in line or ".bss" in prev_line):
ram_offset = None
continue
ram = int(line[16 : 16 + 18], 0)
rom = int(line[59 : 59 + 18], 0)
ram_offset = ram - rom
continue
prev_line = line
if (
ram_offset is None
or "=" in line
or "*fill*" in line
or " 0x" not in line
):
continue
ram = int(line[16 : 16 + 18], 0)
rom = ram - ram_offset
sym = line.split()[-1]
if "0x" in sym and "/" not in sym:
ram_offset = None
continue
if "/" in sym:
cur_file = sym
continue
if rom == target_addr or (is_ram and ram == target_addr):
return f"{sym} (VRAM 0x{ram:X}, VROM 0x{rom:X}, {cur_file})"
if rom > target_addr or (is_ram and ram > target_addr):
offset = target_addr - prev_ram if is_ram else target_addr - prev_rom
return f"at 0x{offset:X} bytes inside {prev_sym} (VRAM 0x{prev_ram:X}, VROM 0x{prev_rom:X}, {prev_file})"
prev_ram = ram
prev_rom = rom
prev_sym = sym
prev_file = cur_file
return "at end of rom?"
def search_symbol(target_sym):
ram_offset = None
cur_file = "<no file>"
prev_line = ""
with open(mymap) as f:
for line in f:
if "load address" in line:
ram = int(line[16 : 16 + 18], 0)
rom = int(line[59 : 59 + 18], 0)
ram_offset = ram - rom
continue
prev_line = line
if (
ram_offset is None
or "=" in line
or "*fill*" in line
or " 0x" not in line
):
continue
ram = int(line[16 : 16 + 18], 0)
rom = ram - ram_offset
sym = line.split()[-1]
if "0x" in sym and "/" not in sym:
ram_offset = None
continue
elif "/" in sym:
cur_file = sym
continue
if sym == target_sym:
return (rom, cur_file, ram)
return None
try:
target_addr = int(args.name, 0)
print(args.name, "is", search_address(target_addr))
except ValueError:
sym_info = search_symbol(args.name)
if sym_info is not None:
sym_rom = sym_info[0]
sym_file = sym_info[1]
sym_ram = sym_info[2]
print(f"Symbol {args.name} (VRAM: 0x{sym_ram:08X}, VROM: 0x{sym_rom:06X}, {sym_file})")
else:
print(f"Symbol {args.name} not found in map file {mymap}")