From 43a3c1c42abbc4163167467bec6d6821a1dbaf7a Mon Sep 17 00:00:00 2001 From: Aetias Date: Sun, 17 Sep 2023 12:25:20 +0200 Subject: [PATCH] Fix BLX Thumb-to-ARM bug in the assembler --- INSTALL.md | 4 ++-- tools/patch_mwcc.py | 51 +++++++++++++++++++++++++++++++++++++++++++++ tools/setup.py | 5 +++++ 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 tools/patch_mwcc.py diff --git a/INSTALL.md b/INSTALL.md index de15e803..0a8f74a2 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -6,10 +6,10 @@ Contents: ## Prerequisites -1. Install Python 3.7 or higher +1. Install Python 3.11 or higher 2. Install the Python dependencies: ```shell -python3 -m pip install -r tools/requirements.txt +python -m pip install -r tools/requirements.txt ``` 3. Run the setup script: ```shell diff --git a/tools/patch_mwcc.py b/tools/patch_mwcc.py new file mode 100644 index 00000000..199030d7 --- /dev/null +++ b/tools/patch_mwcc.py @@ -0,0 +1,51 @@ +import hashlib +import sys + +class Patch: + def __init__(self, name: str, version: str, sha1: str, desc: str, offset: int, write: list[int]) -> None: + self.name = name + self.version = version + self.sha1 = sha1 + self.desc = desc + self.offset = offset + self.write = write + +VERSION = '1.0' +PATCHES = [ + Patch( + 'mwasmarm', '2.0/sp1p5', '448cb0c7f1ace4393e9a9562f819f7a9f049be83', + 'Fix 2-aligned Thumb BLX where ARM call target was off by 1', + offset=0x1b45b, write=[0xc2] + ), +] + +if len(sys.argv) != 2: + print(f'patch_mwcc {VERSION}') + print() + print(f'Usage: {sys.argv[0]} ') + print(' \tPath to mwasmarm.exe') + exit() + +filename = sys.argv[1] + +with open(filename, 'rb', buffering=0) as file: + sha1 = hashlib.file_digest(file, 'sha1').hexdigest() + file.seek(0, 2) + length = file.tell() + file.seek(0, 0) + file_bytes = bytearray(file.read(length)) + +num_patches = 0 +for patch in PATCHES: + if sha1 == patch.sha1: + num_patches += 1 + print(f'{patch.name} version {patch.version}') + print(patch.desc) + for i in range(len(patch.write)): + file_bytes[patch.offset + i] = patch.write[i] + print() + +with open(filename, 'wb') as file: + file.write(file_bytes) + +print(f'Applied {num_patches} patch(es)') diff --git a/tools/setup.py b/tools/setup.py index 34f11787..e11daa23 100644 --- a/tools/setup.py +++ b/tools/setup.py @@ -2,10 +2,15 @@ import requests import zipfile import io from pathlib import Path +import subprocess +import sys tools_path = Path(__file__).parent +print('\nInstalling toolchain...') response = requests.get('https://cdn.discordapp.com/attachments/698589325620936736/845499146982129684/mwccarm.zip') zip_file = zipfile.ZipFile(io.BytesIO(response.content)) zip_file.extractall(tools_path) +print('\nPatching...') +subprocess.run([sys.executable, 'patch_mwcc.py', 'mwccarm/2.0/sp1p5/mwasmarm.exe'], cwd=tools_path)