mirror of
https://github.com/sp00nznet/xboxrecomp.git
synced 2026-12-05 10:03:01 +00:00
e54233e752
Asked for by someone using the recomp as a function splitter for a decomp,
which the toolkit was already most of the way to without saying so anywhere.
The analysis half -- boundaries, call graph, calling conventions, frame shapes,
recovered names -- is the same whether you go on to recompile or not, and
tools/recomp was simply the only consumer of it.
Each function gets a file: a header with its address range, size, section,
detection method and confidence, its signature from tools.abi_analysis, and its
callers; then the body, with branch targets inside the function labelled and
callees named.
The bytes are `db` and the disassembly is a comment beside them, which is the
opposite of what an N64 decomp does and deliberately so. MIPS round-trips
through an assembler because the encoding is fixed-width and unambiguous. x86
does not -- `mov eax, ecx` has two encodings, immediates have short forms, and
which one MSVC picked cannot be recovered from the mnemonic. Reassembling a
listing gives code that runs identically and does not match, and matching is
the only oracle a decomp has. Emitting the original bytes as data makes the
file byte-exact by construction and still leaves the disassembly to read.
Verified rather than asserted, and the verification found two real bugs:
- reconstructing bytes from the instruction stream produced files longer than
the function, because a decoded instruction can start inside a function and
run past its end wherever data was decoded as code. 102 of 2,254 functions,
silently. Bytes now come from the image and coverage is exact by
construction.
- filling only the trailing gap left three functions short, because MSVC
parks switch tables in the middle of a function body and the disassembler
correctly marks those as data. memcpy has two. Gaps are filled wherever
they occur and come out as labelled data runs in place.
Xbox Dashboard .text: 2,254 of 2,254 functions byte-identical to the shipped
binary.
docs/DECOMP.md is the guide, separate from GETTING_STARTED because it is a
different job with a different endpoint -- and it is honest about what is not
solved: no object-file grouping, code only, inferred boundaries that are wrong
in data-heavy regions, and byte-identical is not build-identical.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013pS5pcx2cx9JXyKurjok17
90 lines
3.5 KiB
Python
90 lines
3.5 KiB
Python
"""Split an Xbox binary into one .s per function.
|
|
|
|
py -3 -m tools.split game_files/default.xbe --out build/split
|
|
|
|
Reuses the same analysis the recompiler does -- tools.disasm for boundaries and
|
|
instructions, tools.abi_analysis for signatures -- and emits it in the shape a
|
|
decompilation project wants instead of the shape a recompiler wants.
|
|
|
|
See docs/DECOMP.md for the workflow this is the first step of.
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
|
|
from ..disasm.disasm import Disassembler
|
|
from .splitter import load_abi, split
|
|
|
|
|
|
DEFAULT_ABI = os.path.join("tools", "abi_analysis", "output",
|
|
"abi_functions.json")
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(
|
|
prog="tools.split",
|
|
description="One assembly file per function, for decompilation.")
|
|
p.add_argument("xbe_path", help="Path to the .xbe")
|
|
p.add_argument("-o", "--out", default=os.path.join("tools", "split",
|
|
"output"),
|
|
help="Output directory (default: tools/split/output)")
|
|
p.add_argument("--analysis-json",
|
|
help="Path to <stem>_analysis.json (auto-detected if "
|
|
"omitted; written by tools.xbe_parser --json)")
|
|
p.add_argument("--abi", default=DEFAULT_ABI,
|
|
help="abi_functions.json from tools.abi_analysis. Optional: "
|
|
"without it the .s files carry no signature line "
|
|
"(default: %s)" % DEFAULT_ABI)
|
|
p.add_argument("--text-only", action="store_true",
|
|
help="Only the .text section")
|
|
p.add_argument("--extra-sections",
|
|
help="Comma-separated sections to treat as code, for code "
|
|
"the XBE does not mark executable (e.g. XIPS,DOLBY)")
|
|
p.add_argument("--section", action="append", dest="sections",
|
|
help="Only emit functions from this section. Repeatable")
|
|
p.add_argument("-n", "--limit", type=int, default=0,
|
|
help="Stop after N functions, for a quick look")
|
|
p.add_argument("-v", "--verbose", action="store_true")
|
|
|
|
args = p.parse_args()
|
|
|
|
extra = ([s.strip() for s in args.extra_sections.split(",")]
|
|
if args.extra_sections else [])
|
|
|
|
# stats_only: analyse without writing tools/disasm/output. This tool has
|
|
# its own output directory, and a title being split is often not the one
|
|
# whose disassembly is sitting in the shared one.
|
|
d = Disassembler(
|
|
xbe_path=args.xbe_path,
|
|
analysis_json=args.analysis_json,
|
|
text_only=args.text_only,
|
|
stats_only=True,
|
|
verbose=args.verbose,
|
|
force=True,
|
|
extra_sections=extra,
|
|
)
|
|
if not d.run():
|
|
return 1
|
|
|
|
abi = load_abi(args.abi)
|
|
if not abi and args.abi:
|
|
print(" no ABI data at %s; .s files will have no signature line\n"
|
|
" (run: py -3 -m tools.abi_analysis %s)"
|
|
% (args.abi, args.xbe_path), file=sys.stderr)
|
|
|
|
manifest = split(d.engine, d.func_detector, args.out,
|
|
abi_by_addr=abi, sections=args.sections,
|
|
limit=args.limit, verbose=args.verbose)
|
|
|
|
print("\nSplit %d functions into %s"
|
|
% (manifest["functions"], os.path.join(args.out, "asm")))
|
|
print(" sections : %s" % ", ".join(manifest["sections"]))
|
|
print(" with ABI : %d" % manifest["with_abi"])
|
|
print(" manifest : %s" % os.path.join(args.out, "manifest.json"))
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|