Create context with Makefile

This commit is contained in:
Luciano Ciccariello 2022-10-15 11:18:32 +01:00
parent 19dd340716
commit 52e75e6a2f
4 changed files with 86 additions and 3 deletions

1
.gitignore vendored
View File

@ -6,6 +6,7 @@ ST/
iso/
ctx.c
*.ld
*auto.*.txt
generated.symbols.*.txt

View File

@ -40,9 +40,11 @@ MAIN_TARGET := $(BUILD_DIR)/$(MAIN)
PYTHON := python3
SPLAT_DIR := $(TOOLS_DIR)/n64splat
SPLAT_APP := $(SPLAT_DIR)/split.py
SPLAT := $(PYTHON) $(SPLAT_DIR)/split.py
SPLAT := $(PYTHON) $(SPLAT_APP)
ASMDIFFER_DIR := $(TOOLS_DIR)/asm-differ
ASMDIFFER_APP := $(ASMDIFFER_DIR)/diff.py
M2CTX_APP := $(TOOLS_DIR)/m2ctx.py
M2CTX := $(PYTHON) $(M2CTX_APP)
define list_src_files
$(foreach dir,$(ASM_DIR)/$(1),$(wildcard $(dir)/**.s))
@ -194,7 +196,10 @@ extract_ric: require-tools
extract_st%: require-tools
cat $(CONFIG_DIR)/symbols.txt $(CONFIG_DIR)/symbols.st$*.txt > $(CONFIG_DIR)/generated.symbols.st$*.txt
$(SPLAT) --basedir . $(CONFIG_DIR)/splat.st$*.yaml
$(CONFIG_DIR)/generated.symbols.%.txt:
$(CONFIG_DIR)/generated.symbols.%.txt:
ctx.c: $(M2CTX_APP)
$(M2CTX) $(SOURCE)
require-tools: $(SPLAT_APP) $(ASMDIFFER_APP)
update-tools: require-tools
@ -206,6 +211,8 @@ $(SPLAT_APP):
$(ASMDIFFER_APP):
git submodule init $(ASMDIFFER_DIR)
git submodule update $(ASMDIFFER_DIR)
$(M2CTX_APP):
curl -o $@ https://raw.githubusercontent.com/ethteck/m2ctx/main/m2ctx.py
$(BUILD_DIR)/%.s.o: %.s
$(AS) $(AS_FLAGS) -o $@ $<

View File

@ -60,7 +60,9 @@ This guides you step-by-step to contribute to the decompilation project.
1. Look for one of those function which hasn't successfully decompiled yet (eg. `INCLUDE_ASM("asm/st/wrp/nonmatchings/6FD0", func_801873A0);`)
1. Follow [the guide to check the function matching](#check-function-matching)
1. Look for its assembly file (eg. `asm/st/wrp/nonmatchings/6FD0/func_801873A0.s`)
1. Copy&paste the entire file content into [mips_to_c](https://simonsoftware.se/other/mips_to_c.py) and decompile it
1. Copy&paste the entire assembly file content into [mips_to_c](https://simonsoftware.se/other/mips_to_c.py)
1. Generate a `ctx.c` context with `SOURCE=src/st/wrp/6FD0.c make ctx.c` and copy&paste it to the bottom part of the page
1. Click the decompile button!
1. The code from `mips_to_c` might not be compilable, so keep following `asm-differ` output until you get some assembly code on the console
1. You will probably have some differences from your compiled code to the original; keep refactoring the code and move variables around until you have a 100% match.

73
tools/m2ctx.py Normal file
View File

@ -0,0 +1,73 @@
#!/usr/bin/env python3
import argparse
import os
import sys
import subprocess
import tempfile
script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = os.path.abspath(os.path.join(script_dir, ".."))
src_dir = root_dir + "src/"
# Project-specific
CPP_FLAGS = [
"-Iinclude",
"-Isrc",
"-Iver/current/build/include",
"-D_LANGUAGE_C",
"-DF3DEX_GBI_2",
"-D_MIPS_SZLONG=32",
"-DSCRIPT(...)={}",
"-D__attribute__(...)=",
"-D__asm__(...)=",
"-ffreestanding",
"-DM2CTX",
]
def import_c_file(in_file) -> str:
in_file = os.path.relpath(in_file, root_dir)
cpp_command = ["gcc", "-E", "-P", "-dM", *CPP_FLAGS, in_file]
cpp_command2 = ["gcc", "-E", "-P", *CPP_FLAGS, in_file]
with tempfile.NamedTemporaryFile(suffix=".c") as tmp:
stock_macros = subprocess.check_output(["gcc", "-E", "-P", "-dM", tmp.name], cwd=root_dir, encoding="utf-8")
out_text = ""
try:
out_text += subprocess.check_output(cpp_command, cwd=root_dir, encoding="utf-8")
out_text += subprocess.check_output(cpp_command2, cwd=root_dir, encoding="utf-8")
except subprocess.CalledProcessError:
print(
"Failed to preprocess input file, when running command:\n"
+ ' '.join(cpp_command),
file=sys.stderr,
)
sys.exit(1)
if not out_text:
print("Output is empty - aborting")
sys.exit(1)
for line in stock_macros.strip().splitlines():
out_text = out_text.replace(line + "\n", "")
return out_text
def main():
parser = argparse.ArgumentParser(
description="""Create a context file which can be used for mips_to_c"""
)
parser.add_argument(
"c_file",
help="""File from which to create context""",
)
args = parser.parse_args()
output = import_c_file(args.c_file)
with open(os.path.join(root_dir, "ctx.c"), "w", encoding="UTF-8") as f:
f.write(output)
if __name__ == "__main__":
main()