2023-05-04 11:03:02 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-02-06 16:37:43 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
2021-02-09 18:45:46 +01:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2022-05-05 16:08:16 +02:00
|
|
|
import tqdm
|
2021-02-06 16:37:43 +01:00
|
|
|
|
|
|
|
script_dir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
root_dir = script_dir + "/../"
|
2021-03-07 19:32:24 +01:00
|
|
|
|
|
|
|
current_ver_dir = script_dir + "/../ver/current/"
|
2021-02-06 16:37:43 +01:00
|
|
|
asm_dir = root_dir + "asm/nonmatchings/"
|
|
|
|
|
2021-03-07 19:32:24 +01:00
|
|
|
symbol_addrs_path = os.path.join(current_ver_dir, "symbol_addrs.txt")
|
|
|
|
elf_path = os.path.join(current_ver_dir, "build", "papermario.elf")
|
|
|
|
map_path = os.path.join(current_ver_dir, "build", "papermario.map")
|
2021-02-16 11:32:34 +01:00
|
|
|
ignores_path = os.path.join(root_dir, "tools", "ignored_funcs.txt")
|
2021-02-06 16:37:43 +01:00
|
|
|
|
2021-02-09 18:45:46 +01:00
|
|
|
map_symbols = {}
|
|
|
|
symbol_addrs = []
|
2021-04-01 20:00:29 +02:00
|
|
|
dead_symbols = []
|
2021-02-09 18:45:46 +01:00
|
|
|
elf_symbols = []
|
2021-02-06 16:37:43 +01:00
|
|
|
|
2021-02-16 11:32:34 +01:00
|
|
|
ignores = set()
|
|
|
|
|
2021-06-16 11:52:15 +02:00
|
|
|
verbose = False
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-02-16 11:32:34 +01:00
|
|
|
def read_ignores():
|
|
|
|
with open(ignores_path) as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
for line in lines:
|
|
|
|
name = line.split(" = ")[0].strip()
|
|
|
|
if name != "":
|
|
|
|
ignores.add(name)
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
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
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
if ram_offset is None or "=" in line or "*fill*" in line or " 0x" not in line:
|
2021-02-09 18:45:46 +01:00
|
|
|
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
|
|
|
|
2023-07-29 19:03:17 +02: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():
|
2023-07-29 19:03:17 +02:00
|
|
|
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:
|
2021-04-26 19:47:38 +02:00
|
|
|
if "_ROM_START" in line or "_ROM_END" in line:
|
|
|
|
continue
|
|
|
|
|
2023-04-08 16:04:45 +02:00
|
|
|
main_split = line.rstrip().split(";")
|
|
|
|
main = main_split[0]
|
2021-02-10 16:04:58 +01:00
|
|
|
|
2021-04-01 20:00:29 +02:00
|
|
|
dead = False
|
2023-04-08 16:04:45 +02:00
|
|
|
opts = []
|
2021-04-01 20:00:29 +02:00
|
|
|
type = ""
|
2021-02-10 16:04:58 +01:00
|
|
|
rom = -1
|
|
|
|
|
2023-04-08 16:04:45 +02:00
|
|
|
if len(main_split) > 1:
|
|
|
|
ext = main_split[1]
|
|
|
|
opts = ext.split("//")[-1].strip().split(" ")
|
|
|
|
|
|
|
|
for opt in list(opts):
|
|
|
|
if opt.strip() == "":
|
|
|
|
opts.remove(opt)
|
|
|
|
if "type:" in opt:
|
|
|
|
type = opt.split(":")[1]
|
|
|
|
opts.remove(opt)
|
|
|
|
elif "rom:" in opt:
|
|
|
|
rom = int(opt.split(":")[1], 16)
|
|
|
|
opts.remove(opt)
|
|
|
|
elif "dead:" in opt:
|
|
|
|
dead = True
|
2021-02-10 16:04:58 +01:00
|
|
|
|
2021-04-26 19:47:38 +02:00
|
|
|
eqsplit = main.split(" = ")
|
|
|
|
if len(eqsplit) != 2:
|
|
|
|
print(f"Line malformed: '{main}'")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-02-09 18:45:46 +01:00
|
|
|
name, addr = main.split(" = ")
|
2021-02-10 16:04:58 +01:00
|
|
|
|
2021-04-01 20:00:29 +02:00
|
|
|
if not dead:
|
2023-04-08 16:04:45 +02:00
|
|
|
symbol_addrs.append([name, int(addr, 0), type, rom, opts])
|
2021-04-01 20:00:29 +02:00
|
|
|
else:
|
2023-04-08 16:04:45 +02:00
|
|
|
dead_symbols.append([name, int(addr, 0), type, rom, opts])
|
2021-02-06 16:37:43 +01:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-02-09 18:45:46 +01:00
|
|
|
def read_elf():
|
|
|
|
try:
|
2023-07-29 19:03:17 +02:00
|
|
|
result = subprocess.run(["mips-linux-gnu-objdump", "-x", elf_path], stdout=subprocess.PIPE)
|
2021-02-09 18:45:46 +01:00
|
|
|
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-04-26 19:47:38 +02:00
|
|
|
if "_ROM_START" in name or "_ROM_END" in name:
|
|
|
|
continue
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
if (
|
|
|
|
"/" in name
|
|
|
|
or "." in name
|
|
|
|
or name in ignores
|
|
|
|
or name.startswith("_")
|
|
|
|
or name.startswith("jtbl_")
|
|
|
|
or name.endswith(".o")
|
|
|
|
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)
|
2021-02-10 17:52:26 +01:00
|
|
|
if " F " in line or name.startswith("func_"):
|
2021-02-09 18:45:46 +01:00
|
|
|
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):
|
2023-07-29 19:03:17 +02:00
|
|
|
rom = int(name.split("_")[-1], 16)
|
2021-02-10 16:04:58 +01:00
|
|
|
|
|
|
|
elf_symbols.append((name, addr, type, rom))
|
2021-02-09 18:45:46 +01:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-02-10 16:04:58 +01:00
|
|
|
def log(s):
|
|
|
|
if verbose:
|
|
|
|
print(s)
|
2021-02-09 18:45:46 +01:00
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-02-09 18:45:46 +01:00
|
|
|
def reconcile_symbols():
|
|
|
|
print(f"Processing {str(len(elf_symbols))} elf symbols...")
|
|
|
|
|
2022-05-05 16:08:16 +02:00
|
|
|
for i, elf_sym in tqdm.tqdm(enumerate(elf_symbols), total=len(elf_symbols)):
|
2021-02-09 18:45:46 +01:00
|
|
|
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]:
|
2023-07-29 19:03:17 +02:00
|
|
|
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]}")
|
2023-07-29 19:03:17 +02:00
|
|
|
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]
|
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-02-10 16:04:58 +01:00
|
|
|
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}; //"
|
2021-04-01 20:00:29 +02:00
|
|
|
if symbol[2] and len(symbol[2]) > 0:
|
|
|
|
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")
|
|
|
|
for symbol in sorted(dead_symbols, key=lambda x: (x[3] == -1, x[3], x[1], x[0])):
|
|
|
|
line = f"{symbol[0]} = 0x{symbol[1]:X}; //"
|
|
|
|
if symbol[2] and len(symbol[2]) > 0:
|
2021-02-10 16:04:58 +01:00
|
|
|
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
|
|
|
|
2023-07-29 19:03:17 +02:00
|
|
|
|
2021-02-16 11:32:34 +01:00
|
|
|
read_ignores()
|
2021-02-09 18:45:46 +01:00
|
|
|
scan_map()
|
|
|
|
read_symbol_addrs()
|
2021-03-26 15:46:45 +01:00
|
|
|
|
|
|
|
# chicken scratch cod to print out new / renamed symbols
|
|
|
|
# with open("tools/new_syms.txt") as f:
|
|
|
|
# new_syms = f.readlines()
|
|
|
|
|
|
|
|
# new_sym_dict = {}
|
|
|
|
# for sym_line in new_syms:
|
|
|
|
# sym_line = sym_line.strip()
|
|
|
|
# if sym_line:
|
|
|
|
# name, rest = sym_line.split(" = ")
|
|
|
|
# vram = int(rest.split(";")[0], 0)
|
|
|
|
# new_sym_dict[vram] = name
|
|
|
|
|
|
|
|
# renames = []
|
|
|
|
# adds = []
|
|
|
|
# for addr in new_sym_dict:
|
|
|
|
# found = False
|
|
|
|
# for thing in symbol_addrs:
|
|
|
|
# if thing[1] == addr and not thing[0].startswith("func_") and not thing[0].startswith("D_"):
|
|
|
|
# if new_sym_dict[addr] != thing[0]:
|
|
|
|
# renames.append(f"{thing[0]} -> {new_sym_dict[addr]}")
|
|
|
|
# found = True
|
|
|
|
# break
|
|
|
|
# if not found:
|
|
|
|
# adds.append(f"{new_sym_dict[addr]} = {addr:X}")
|
|
|
|
|
|
|
|
# for r in renames:
|
|
|
|
# print(r)
|
|
|
|
# for a in adds:
|
|
|
|
# print(a)
|
|
|
|
|
2021-02-09 18:45:46 +01:00
|
|
|
read_elf()
|
|
|
|
reconcile_symbols()
|
2021-02-10 16:04:58 +01:00
|
|
|
write_new_symbol_addrs()
|