Files

81 lines
2.0 KiB
Python
Raw Permalink Normal View History

2024-03-19 00:56:32 +01:00
import argparse
import os
from pathlib import Path
import platform
import re
import subprocess
parser = argparse.ArgumentParser(description='Mangles function declarations')
parser.add_argument('file', help='C/C++ source file with defined function to mangle')
tools_dir = Path(os.path.dirname(os.path.realpath(__file__)))
cc_path = tools_dir / 'mwccarm' / '2.0' / 'sp1p5' / 'mwccarm.exe'
root_dir = tools_dir.parent
include_dir = root_dir / 'include'
2024-04-30 15:09:04 +02:00
libs_dir = root_dir / 'libs'
libc_include_dir = libs_dir / 'c' / 'include'
libcpp_include_dir = libs_dir / 'cpp' / 'include'
2025-01-18 15:50:03 +01:00
libnds_include_dir = libs_dir / 'nds' / 'include'
2024-03-19 00:56:32 +01:00
if platform.system() == 'Windows': cc = [str(cc_path)]
else: cc = ['wine', str(cc_path)]
args = parser.parse_args()
cc.extend([
2024-04-30 15:09:04 +02:00
'-enum', 'int',
2024-03-19 17:54:21 +01:00
'-char', 'signed',
2024-04-30 15:09:04 +02:00
'-proc', 'arm946e',
'-gccext,on',
'-fp', 'soft',
2025-01-19 11:58:47 +01:00
'-inline', 'noauto',
2024-04-30 15:09:04 +02:00
'-Cpp_exceptions', 'off',
'-RTTI', 'off',
'-interworking',
'-nolink',
2024-03-19 17:54:21 +01:00
'-msgstyle', 'gcc',
2024-03-19 00:56:32 +01:00
'-dis',
2024-04-30 15:09:04 +02:00
'-gccinc',
'-i', include_dir,
'-i', libc_include_dir,
'-i', libcpp_include_dir,
2025-01-18 15:50:03 +01:00
'-i', libnds_include_dir,
2024-03-19 00:56:32 +01:00
args.file
])
2024-03-19 17:54:21 +01:00
try:
output = subprocess.check_output(cc)
except subprocess.CalledProcessError as e:
print(e.stdout.decode())
exit(1)
2025-01-01 21:19:30 -05:00
2024-03-19 00:56:32 +01:00
output = output.decode()
# print(output)
2024-04-21 12:12:14 +02:00
mangled_funcs: list[str] = re.findall(r'.text +([^\$ ]\S+)', output)
2025-01-19 12:22:57 +01:00
init_funcs: list[str] = re.findall(r'.init +([^\$ ]\S+)', output)
2025-01-31 21:39:48 +01:00
mangled_data: list[str] = re.findall(r'.data +([^\. ]\S+)', output)
mangled_bss: list[str] = re.findall(r'.bss +([^\. ]\S+)', output)
2024-04-07 15:04:51 +02:00
if len(mangled_funcs) > 0:
2025-01-31 21:39:48 +01:00
print('.text:\n')
2024-04-07 15:04:51 +02:00
for func in mangled_funcs:
print(func)
2025-01-31 21:39:48 +01:00
print('\n')
2025-01-19 12:22:57 +01:00
if len(init_funcs) > 0:
2025-01-31 21:39:48 +01:00
print('.init:\n')
2025-01-19 12:22:57 +01:00
for func in init_funcs:
print(func)
2025-01-31 21:39:48 +01:00
print('\n')
2024-04-07 15:04:51 +02:00
if len(mangled_data) > 0:
2025-01-31 21:39:48 +01:00
print('.data:\n')
2024-04-07 15:04:51 +02:00
for data in mangled_data:
print(data)
2025-01-31 21:39:48 +01:00
print('\n')
if len(mangled_bss) > 0:
print('.bss:\n')
for bss in mangled_bss:
print(bss)
print('\n')