mirror of
https://github.com/sp00nznet/xboxrecomp.git
synced 2026-12-06 10:29:40 +00:00
373de05aff
All three from people following the instructions and reporting where they stopped. recomp_types.h is now written into --gen-dir by tools.recomp. It lived only in templates/runtime/, so a project copied from the template hit `error C1083: Cannot open include file: 'recomp_types.h'` at build time and had to find it by hand. It is the runtime's register model rather than anything a project writes, and recomp_funcs.h includes it -- a quoted include searches the including file's own directory first, so emitting it beside the generated code is all it takes. An existing copy is never overwritten, so a project that has edited it keeps its edits; delete it to get the current one back. The disassembler now names the file it wants when the analysis JSON is missing. It said only "run the XBE parser first", leaving both the filename and its location to guess, and someone did: `--json JSON`, then `--analysis-json JSON` to get past the same error one step later. It now prints the exact path and the exact command that writes it, and says that the name is what auto-detection keys on. The README's own quick start was the wrong instructions. Step 3 ran tools.xbe_parser with no --json, so step 4 could not find the analysis it needs -- and the README is what people follow, since it is the front page. It now passes --json with the name the next step looks for, notes what --text-only excludes, and points at GETTING_STARTED as the version that explains why. Also: the Ghidra naming pipeline was fully built and documented in tools/ghidra_naming/README.md and mentioned in neither guide, so "any way to port over Ghidra labels easily?" had no findable answer. GETTING_STARTED now has it as step 4.5, before recompiling, because the recompiler emits whatever name is on the functions.json entry -- run it after and the names reach the generated C, the crash traces and the ABI reports. And the CUSTOMIZE note on RECOMP_ICALL_IS_CODE is gone. It told readers to edit the VA range for their title; the range has come from g_xbox_code_lo/hi since the hardcoded 0x00400000 cutoff was removed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013pS5pcx2cx9JXyKurjok17
271 lines
9.5 KiB
Python
271 lines
9.5 KiB
Python
"""
|
|
Binary image loader for XBE files.
|
|
|
|
Loads the raw XBE binary and its analysis JSON, providing a unified
|
|
BinaryImage interface for the disassembly engine.
|
|
"""
|
|
|
|
import json
|
|
import struct
|
|
from pathlib import Path
|
|
from dataclasses import dataclass, field
|
|
from typing import Dict, List, Optional, Tuple
|
|
|
|
# Sections that hold data even when the XBE marks them executable. Excluded
|
|
# from disassembly by name; --extra-sections overrides this per title.
|
|
#
|
|
# Only the conventional PE data sections are listed. XDK library sections are
|
|
# NOT excluded even when their name suggests data (DSOUND_RD, D3D_RD, XON_RD):
|
|
# the linker maps class those as CODE, and they are exactly the sections we
|
|
# want disassembled.
|
|
DATA_SECTION_NAMES = frozenset({
|
|
".data", ".data1", ".rdata", ".idata", ".edata", ".reloc", ".tls",
|
|
})
|
|
|
|
|
|
@dataclass
|
|
class SectionInfo:
|
|
"""Parsed section metadata."""
|
|
name: str
|
|
virtual_addr: int
|
|
virtual_size: int
|
|
raw_addr: int
|
|
raw_size: int
|
|
writable: bool
|
|
executable: bool
|
|
flags: str
|
|
|
|
|
|
@dataclass
|
|
class KernelImport:
|
|
"""A kernel import thunk entry."""
|
|
ordinal: int
|
|
name: str
|
|
thunk_addr: int
|
|
|
|
|
|
@dataclass
|
|
class BinaryImage:
|
|
"""
|
|
Represents the loaded XBE binary with all metadata.
|
|
|
|
Provides methods for reading bytes at virtual addresses,
|
|
translating between VA and file offsets, and querying sections.
|
|
"""
|
|
filepath: str
|
|
raw_data: bytes
|
|
base_address: int
|
|
image_size: int
|
|
entry_point: int
|
|
kernel_thunk_addr: int
|
|
|
|
sections: List[SectionInfo] = field(default_factory=list)
|
|
kernel_imports: List[KernelImport] = field(default_factory=list)
|
|
|
|
# Lookup tables built after loading
|
|
_section_by_name: Dict[str, SectionInfo] = field(default_factory=dict, repr=False)
|
|
_thunk_to_import: Dict[int, KernelImport] = field(default_factory=dict, repr=False)
|
|
|
|
def __post_init__(self):
|
|
self._section_by_name = {s.name: s for s in self.sections}
|
|
self._thunk_to_import = {ki.thunk_addr: ki for ki in self.kernel_imports}
|
|
|
|
def get_section(self, name: str) -> Optional[SectionInfo]:
|
|
"""Get section by name."""
|
|
return self._section_by_name.get(name)
|
|
|
|
def get_section_at_va(self, va: int) -> Optional[SectionInfo]:
|
|
"""Find the section containing a virtual address."""
|
|
for sec in self.sections:
|
|
if sec.virtual_addr <= va < sec.virtual_addr + sec.virtual_size:
|
|
return sec
|
|
return None
|
|
|
|
def va_to_file_offset(self, va: int) -> Optional[int]:
|
|
"""Convert virtual address to file offset."""
|
|
# Header area
|
|
if va < self.base_address + 0x1000:
|
|
off = va - self.base_address
|
|
if 0 <= off < len(self.raw_data):
|
|
return off
|
|
return None
|
|
|
|
# Search sections
|
|
for sec in self.sections:
|
|
if sec.virtual_addr <= va < sec.virtual_addr + sec.virtual_size:
|
|
offset_in_sec = va - sec.virtual_addr
|
|
if offset_in_sec < sec.raw_size:
|
|
return sec.raw_addr + offset_in_sec
|
|
# In BSS (beyond raw data)
|
|
return None
|
|
return None
|
|
|
|
def read_bytes_at_va(self, va: int, size: int) -> Optional[bytes]:
|
|
"""Read bytes at a virtual address."""
|
|
off = self.va_to_file_offset(va)
|
|
if off is None:
|
|
return None
|
|
end = off + size
|
|
if end > len(self.raw_data):
|
|
return None
|
|
return self.raw_data[off:end]
|
|
|
|
def read_u32_at_va(self, va: int) -> Optional[int]:
|
|
"""Read a 32-bit unsigned integer at a virtual address."""
|
|
data = self.read_bytes_at_va(va, 4)
|
|
if data is None:
|
|
return None
|
|
return struct.unpack_from('<I', data)[0]
|
|
|
|
def get_section_data(self, section: SectionInfo) -> bytes:
|
|
"""Get the raw bytes for a section."""
|
|
return self.raw_data[section.raw_addr:section.raw_addr + section.raw_size]
|
|
|
|
def get_kernel_import_at_thunk(self, thunk_addr: int) -> Optional[KernelImport]:
|
|
"""Look up a kernel import by its thunk address."""
|
|
return self._thunk_to_import.get(thunk_addr)
|
|
|
|
def get_executable_sections(self) -> List[SectionInfo]:
|
|
"""Return all executable sections."""
|
|
return [s for s in self.sections if s.executable]
|
|
|
|
def get_code_sections(self) -> List[SectionInfo]:
|
|
"""Return sections suitable for disassembly.
|
|
|
|
The executable flag alone is not a usable signal: Xbox linkers mark
|
|
nearly every section executable, including .data. Disassembling those
|
|
yields phantom functions built out of zero-fill -- runs of 00 00 decode
|
|
as `add [eax], al`, and the lifter happily emits C for them (one such
|
|
phantom referenced a nonexistent `cr7` and broke the build).
|
|
|
|
So data sections are excluded by name regardless of their flags. A
|
|
title that genuinely hides code in one of them can put it back with
|
|
--extra-sections, which is exactly what that flag is for.
|
|
"""
|
|
return [s for s in self.sections
|
|
if s.executable and s.raw_size > 0
|
|
and s.name not in DATA_SECTION_NAMES]
|
|
|
|
|
|
def _parse_hex(s: str) -> int:
|
|
"""Parse a hex string like '0x00011000' to int."""
|
|
return int(s, 16)
|
|
|
|
|
|
def _find_analysis_json(xbe_path: Path) -> Optional[Path]:
|
|
"""Auto-detect the analysis JSON for this XBE.
|
|
|
|
Written by ``tools.xbe_parser --json``, so the name is per-game rather than
|
|
hardcoded to one title.
|
|
|
|
The exact ``<stem>_analysis.json`` always wins. A bare ``*_analysis.json``
|
|
glob is only accepted when the directory holds a single XBE: a disc that
|
|
ships one binary per region keeps them side by side -- Wreckless has seven
|
|
in one folder -- and taking the first sorted match there hands DSTEAL_JP.xbe
|
|
the analysis written for default.xbe. Different entry point, different
|
|
section layout, no warning. Data from the wrong binary is worse than none,
|
|
because everything downstream still runs.
|
|
"""
|
|
search_dirs = [
|
|
# Same directory as XBE
|
|
xbe_path.parent,
|
|
# In the xbe_parser tool directory
|
|
Path("tools/xbe_parser"),
|
|
# Relative to repo root
|
|
xbe_path.parent.parent / "tools" / "xbe_parser",
|
|
]
|
|
for d in search_dirs:
|
|
exact = d / (xbe_path.stem + "_analysis.json")
|
|
if exact.exists():
|
|
return exact
|
|
for d in search_dirs:
|
|
try:
|
|
ambiguous = len(list(d.glob("*.xbe"))) > 1
|
|
except OSError:
|
|
continue
|
|
if ambiguous:
|
|
continue
|
|
# sorted() so the pick is deterministic when a dir holds several
|
|
for p in sorted(d.glob("*_analysis.json")):
|
|
return p
|
|
return None
|
|
|
|
|
|
def load_image(xbe_path: str, analysis_json: Optional[str] = None) -> BinaryImage:
|
|
"""
|
|
Load an XBE binary and its analysis metadata.
|
|
|
|
Args:
|
|
xbe_path: Path to the .xbe file.
|
|
analysis_json: Optional path to burnout3_analysis.json.
|
|
If None, auto-detected from standard locations.
|
|
|
|
Returns:
|
|
A fully initialized BinaryImage instance.
|
|
"""
|
|
xbe_file = Path(xbe_path)
|
|
if not xbe_file.exists():
|
|
raise FileNotFoundError(f"XBE file not found: {xbe_path}")
|
|
|
|
# Read raw binary
|
|
raw_data = xbe_file.read_bytes()
|
|
|
|
# Validate XBE magic
|
|
if raw_data[:4] != b'XBEH':
|
|
raise ValueError(f"Invalid XBE magic: {raw_data[:4]!r}")
|
|
|
|
# Find and load analysis JSON
|
|
json_path = Path(analysis_json) if analysis_json else _find_analysis_json(xbe_file)
|
|
if json_path is None or not json_path.exists():
|
|
# Name the file it wants and the command that writes it. The old
|
|
# message said only "run the XBE parser first", which left the reader
|
|
# to guess both the filename and where it goes -- one did, passed
|
|
# `--json JSON`, and then had to pass `--analysis-json JSON` here to
|
|
# get past it. Auto-detection needs the exact name.
|
|
wanted = xbe_file.parent / (xbe_file.stem + "_analysis.json")
|
|
raise FileNotFoundError(
|
|
f"Analysis JSON not found: {wanted}\n"
|
|
f" Write it with: py -3 -m tools.xbe_parser {xbe_file} "
|
|
f"--json {wanted}\n"
|
|
f" The name matters -- this step looks for "
|
|
f"<xbe stem>_analysis.json beside the XBE. A file somewhere else, "
|
|
f"or under another name, needs --analysis-json <path>."
|
|
)
|
|
|
|
with open(json_path) as f:
|
|
analysis = json.load(f)
|
|
|
|
# Parse sections
|
|
sections = []
|
|
for sec_data in analysis["sections"]:
|
|
sections.append(SectionInfo(
|
|
name=sec_data["name"],
|
|
virtual_addr=_parse_hex(sec_data["virtual_addr"]),
|
|
virtual_size=sec_data["virtual_size"],
|
|
raw_addr=_parse_hex(sec_data["raw_addr"]),
|
|
raw_size=sec_data["raw_size"],
|
|
writable=sec_data["writable"],
|
|
executable=sec_data["executable"],
|
|
flags=sec_data["flags"],
|
|
))
|
|
|
|
# Parse kernel imports
|
|
kernel_imports = []
|
|
for imp_data in analysis["kernel_imports"]:
|
|
kernel_imports.append(KernelImport(
|
|
ordinal=imp_data["ordinal"],
|
|
name=imp_data["name"],
|
|
thunk_addr=_parse_hex(imp_data["thunk_addr"]),
|
|
))
|
|
|
|
return BinaryImage(
|
|
filepath=str(xbe_file),
|
|
raw_data=raw_data,
|
|
base_address=_parse_hex(analysis["base_address"]),
|
|
image_size=analysis["image_size"],
|
|
entry_point=_parse_hex(analysis["entry_point"]),
|
|
kernel_thunk_addr=_parse_hex(analysis["kernel_thunk_addr"]),
|
|
sections=sections,
|
|
kernel_imports=kernel_imports,
|
|
)
|