papermario/tools/add_to_symbol_addrs.py

181 lines
5.4 KiB
Python
Raw Normal View History

2021-02-06 16:37:43 +01:00
#!/usr/bin/python3
import os
import re
2021-02-09 18:45:46 +01:00
import subprocess
import sys
2021-02-06 16:37:43 +01:00
script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = script_dir + "/../"
asm_dir = root_dir + "asm/nonmatchings/"
2021-02-09 18:45:46 +01:00
symbol_addrs_path = os.path.join(script_dir, "symbol_addrs.txt")
elf_path = os.path.join(root_dir, "build", "papermario.elf")
map_path = os.path.join(root_dir, "build", "papermario.map")
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
map_symbols = {}
symbol_addrs = []
elf_symbols = []
2021-02-06 16:37:43 +01:00
2021-02-10 16:04:58 +01:00
verbose = False
2021-02-09 18:45:46 +01:00
def scan_map():
ram_offset = None
cur_file = "<no file>"
prev_line = ""
with open(map_path) 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
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
if (
ram_offset is None
or "=" in line
or "*fill*" in line
or " 0x" not in line
):
continue
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
ram = int(line[16 : 16 + 18], 0)
rom = ram - ram_offset
sym = line.split()[-1]
if "0x" in sym:
ram_offset = None
continue
elif "/" in sym:
cur_file = sym
continue
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
map_symbols[sym] = (rom, cur_file, ram)
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
def read_symbol_addrs():
unique_lines = set()
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
with open(symbol_addrs_path, "r") as f:
for line in f.readlines():
unique_lines.add(line)
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
for line in unique_lines:
main, ext = line.rstrip().split(";")
2021-02-10 16:04:58 +01:00
opt = ext.split("//")[-1].strip().split(" ")
type = None
rom = -1
args = []
for thing in list(opt):
if thing.strip() == "":
opt.remove(thing)
if "type:" in thing:
type = thing.split(":")[1]
opt.remove(thing)
elif "rom:" in thing:
rom = int(thing.split(":")[1], 16)
opt.remove(thing)
2021-02-09 18:45:46 +01:00
name, addr = main.split(" = ")
2021-02-10 16:04:58 +01:00
symbol_addrs.append([name, int(addr, 0), type, rom, opt])
2021-02-06 16:37:43 +01:00
2021-02-09 18:45:46 +01:00
def read_elf():
try:
result = subprocess.run(['objdump', '-x', elf_path], stdout=subprocess.PIPE)
objdump_lines = result.stdout.decode().split("\n")
except:
print(f"Error: Could not run objdump on {elf_path} - make sure that the project is built")
sys.exit(1)
2021-02-09 11:39:22 +01:00
2021-02-09 18:45:46 +01:00
for line in objdump_lines:
if " F " in line or " O " in line or " *ABS*" in line:
components = line.split()
name = components[-1]
2021-02-09 11:39:22 +01:00
2021-02-09 18:45:46 +01:00
if "/" in name or \
name.startswith("_") or \
name.startswith("jtbl_") or \
re.match(r"L[0-9A-F]{8}", name):
2021-02-09 11:39:22 +01:00
continue
2021-02-09 18:45:46 +01:00
addr = int(components[0], 16)
if " F " in line:
type = "func"
else:
type = "data"
2021-02-10 16:04:58 +01:00
rom = None
2021-02-09 18:45:46 +01:00
if name in map_symbols:
2021-02-10 16:04:58 +01:00
rom = map_symbols[name][0]
2021-02-09 18:45:46 +01:00
elif re.match(".*_[0-9A-F]{8}_[0-9A-F]{6}", name):
2021-02-10 16:04:58 +01:00
rom = int(name.split('_')[-1], 16)
elf_symbols.append((name, addr, type, rom))
2021-02-09 18:45:46 +01:00
2021-02-10 16:04:58 +01:00
def log(s):
if verbose:
print(s)
2021-02-09 18:45:46 +01:00
def reconcile_symbols():
print(f"Processing {str(len(elf_symbols))} elf symbols...")
2021-02-10 16:04:58 +01:00
for i, elf_sym in enumerate(elf_symbols):
2021-02-09 18:45:46 +01:00
if i % 1000 == 0:
print(i)
name_match = None
rom_match = None
2021-02-10 16:04:58 +01:00
for known_sym in symbol_addrs:
2021-02-09 18:45:46 +01:00
# Name
if not name_match:
2021-02-10 16:04:58 +01:00
if elf_sym[0] == known_sym[0]:
name_match = known_sym
2021-02-09 18:45:46 +01:00
2021-02-10 16:04:58 +01:00
if elf_sym[1] != known_sym[1]:
log(f"Ram mismatch! {elf_sym[0]} is 0x{elf_sym[1]:X} in the elf and 0x{known_sym[1]} in symbol_addrs")
2021-02-09 18:45:46 +01:00
# Rom
if not rom_match:
# Todo account for either or both syms not containing a rom addr
2021-02-10 16:04:58 +01:00
if elf_sym[3]:
if elf_sym[3] == known_sym[3]:
rom_match = known_sym
2021-02-09 18:45:46 +01:00
if not name_match and not rom_match:
2021-02-10 16:04:58 +01:00
log(f"Creating new symbol {elf_sym[0]}")
symbol_addrs.append([elf_sym[0], elf_sym[1], elf_sym[2], elf_sym[3] if elf_sym[3] else -1, []])
2021-02-09 18:45:46 +01:00
elif not name_match:
2021-02-10 16:04:58 +01:00
log(f"Renaming identical rom address symbol {rom_match[0]} to {elf_sym[0]}")
rom_match[0] = elf_sym[0]
elif not rom_match and elf_sym[3]:
if name_match[3] >= 0:
log(f"Correcting rom address {name_match[3]} to {elf_sym[3]} for symbol {name_match[0]}")
else:
log(f"Adding rom address {elf_sym[3]} to symbol {name_match[0]}")
name_match[3] = elf_sym[3]
def write_new_symbol_addrs():
with open(symbol_addrs_path, "w", newline="\n") as f:
for symbol in sorted(symbol_addrs, key=lambda x: (x[3] == -1, x[3], x[1], x[0])):
line = f"{symbol[0]} = 0x{symbol[1]:X}; //"
if symbol[2]:
line += f" type:{symbol[2]}"
if symbol[3] >= 0:
line += f" rom:0x{symbol[3]:X}"
if len(symbol[4]) > 0:
for thing in symbol[4]:
line += f" {thing}"
f.write(line + "\n")
2021-02-09 18:45:46 +01:00
scan_map()
read_symbol_addrs()
read_elf()
reconcile_symbols()
2021-02-10 16:04:58 +01:00
write_new_symbol_addrs()