Skip to content

Instantly share code, notes, and snippets.

@lepelog
Created March 5, 2021 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepelog/9d560148e4caf57cf81ab4e1820dbc79 to your computer and use it in GitHub Desktop.
Save lepelog/9d560148e4caf57cf81ab4e1820dbc79 to your computer and use it in GitHub Desktop.
from pathlib import Path
from typing import List, Tuple
from itertools import chain
import re
basemap = Path('frameworkF.map').read_text()
buildmap = Path('build/dolzel2/dolzel2.map').read_text()
buildfuncdef_re = re.compile(r'.* (?P<addr>[0-9a-f]{8}) [0-9a-f]{8} . (?P<name>[^ ]+) .*')
basefuncdef_re = re.compile(r'.* (?P<addr>[0-9a-f]{8}) . (?P<name>[^ ]+) .*')
def get_funcname_addr(map_file: str, regex: re.Pattern):
start = False
addr_funcname = {}
for line in map_file.splitlines():
if line.startswith('.text'):
start = True
if not start:
continue
if line.startswith('.ctors'):
break
match = regex.match(line)
if match:
addr = match.group('addr')
name = match.group('name')
if not name.startswith('.') and not name.startswith('lbl'):
addr_funcname[addr] = name
# linesegs = line.strip().split()
# if len(linesegs) >= 5:
# addr_str = linesegs[2]
# func_name = linesegs[4]
# if func_name == '.text' or func_name.isnumeric():
# continue
# addr_funcname[addr_str] = func_name
return addr_funcname
basemap_map = get_funcname_addr(basemap, basefuncdef_re)
buildmap_map = get_funcname_addr(buildmap, buildfuncdef_re)
missmatch = {}
for addr, symbol in basemap_map.items():
if addr in buildmap_map:
bsymb = buildmap_map[addr]
if bsymb != symbol:
missmatch[addr] = (symbol, bsymb)
good_renames = []
for (orig_name, cur_name) in missmatch.values():
if not '@' in orig_name and not '<' in orig_name:
good_renames.append((orig_name, cur_name))
def do_renames(renames: List[Tuple[str, str]]):
"""
(orig_symbol, cur_symbol)
"""
for file in chain(Path('asm').glob('**/*.s'),
Path('include').glob('**/*.s')):
is_shiftjis = False
try:
filedata = file.read_text()
except UnicodeDecodeError:
is_shiftjis = True
filedata = file.read_text('shift-jis')
for (orig_symbol, cur_symbol) in renames:
filedata = filedata.replace('\n'+cur_symbol+':', '\n'+orig_symbol+':')
filedata = filedata.replace(' '+cur_symbol+'\n', ' '+orig_symbol+'\n')
filedata = filedata.replace(' '+cur_symbol+'@', ' '+orig_symbol+'@')
if is_shiftjis:
file.write_text(filedata, 'shift-jis')
else:
file.write_text(filedata)
for file in chain(Path('src').glob('**/*.cpp'),
Path('libs').glob('**/*.cpp'),
Path('include').glob('**/*.h')):
is_shiftjis = False
try:
filedata = file.read_text()
except UnicodeDecodeError:
is_shiftjis = True
filedata = file.read_text('shift-jis')
for (orig_symbol, cur_symbol) in renames:
filedata = filedata.replace(' '+cur_symbol+'(', ' '+orig_symbol+'(')
if is_shiftjis:
file.write_text(filedata, 'shift-jis')
else:
file.write_text(filedata)
ldscript = Path('ldscript.lcf')
filedata = ldscript.read_text()
for (orig_symbol, cur_symbol) in renames:
filedata = filedata.replace('\n'+cur_symbol+'\n', '\n'+orig_symbol+'\n')
ldscript.write_text(filedata)
# do_renames(good_renames)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment