relinker: opt-in, byte-checked game patches at conversion time #6206
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by eliyarson on 2026-10-15T02:54:59Z.
Proposal. An opt-in relinker feature: a per-title patch file that the relinker applies to the title's executable (and bundled modules) at conversion time. Each entry gives a virtual address, the bytes expected there and the bytes to write. The relinker refuses the conversion on any mismatch and writes a record of what it changed. With no patch file the output is byte-identical to today's. Nothing in code or docs names a title.
PR 1 (core patcher) is open: #6314. PRs 2–4 follow only if you want the feature.
Why. Titles hard-code budgets tuned for one machine, and PCs differ in core count, page size and per-access cost. The relinker already makes up for host differences at conversion time (
--to-intel,--to-rosetta,--linux-fixed-base); a checked, recorded change of a game constant is the same kind of step, and is shareable and reproducible.Why not a hex editor. The relinker checks the expected bytes against the original image, so a patch for another content version fails instead of corrupting the file. It refuses a patch that its own rewrites would replace: the ELF header and program-header table, Intel rewrites and their trampolines, syscall traps, relinker patches, and relocation targets, which the loader overwrites.
Design (PR 1).
relinker-patches.tomlnext to the input is loaded automatically;--patches <file>overrides it,--no-patchesdisables it.target,va,expected,value, an optionaltype, an optionalname, and optionalmin/max/alignchecks on the value.<out>.patches.toml.--patcheswith it reproduces the conversion.--verify-patchesre-checks a patch file against an input, or against an output, and writes nothing.Follow-ups. A
sha256version gate (PR 2); the web converter (PR 3);--set name=value(KiB/MiB/GiB suffixes) and--settings <profile>for shareable per-machine settings (PR 4). Settings apply at conversion, not at launch.What it does not do. It buys headroom for the title's own pipeline; it does not make the emulator cheaper. I know of no emulator-side fix. Two experiments (huge pages on direct memory; lazy write-watch re-arm) were not separable from base at 10 runs per arm (unpinned; base 1/10, huge pages 2/10, lazy re-arm 0/10).
Motivating case: Hades II (PPSA36082)
#1965 lists the 512 MiB upload-pool overflow, and its notes say the author played "with the pool patched bigger in the executable". The pool is a constant in the game's heap descriptor. I changed one byte of
app.elf(the block size,0x20to0x30at file offset0x290414): 768 MiB instead of 512 MiB. The arms differ only in that byte.Pinned to 2 cores (
taskset -c 0-1), 10 interleaved runs per arm:One-sided Fisher p = 0.0015 (two-sided 0.0031). It also holds on rounds 1–8 alone (5/8 against 0/8, p = 0.0128). Unpinned, neither arm crashed in 10 runs: no regression, no improvement.
Crash. The pool runs out and a
memcpywrites to NULL fromapp.elf+0x28fbae(the type-2 heap returning NULL is an inference, consistent with under 16 MiB free at every crash). gdb shows this in 6 of the 6 cores I could still read; the seventh has only a saved stack. #1965's blocker table lists "memcpy to null at eboot0x28fba9" when the first flip replaces the device, fixed by #1367 (merged 2026-10-09).0x28fba9is thecalland0x28fbaeits return address, so I believe it is the same site. My base build includes #1367, so that path is gone; the pool still runs out at the same site during loading, rarely unpinned and in 7 of 10 runs pinned to 2 cores.Caveat. The game ran at nice 16–19, not 0 (inferred, not recorded per run); base crashed and the patched arm did not at both.
Provenance.
main73efd9407with #6000; Linux (CachyOS, 7.2.2), Ryzen 9 5950X, RTX 3080 Ti, about 79 GiB available; 2026-10-14.Patch file and verify mode in detail
Grammar. A strict subset of TOML, line-oriented UTF-8. Any other syntax is an error that quotes the line.
#comments are allowed.[[patch]]starts an entry; no other tables.key = value, keys[A-Za-z0-9_]+. Integers are decimal or0xhex (no_, no size suffixes). Strings are double-quoted, no escapes.format = 1is required. The record keysrelinker_version,relinker_commit,config,config_sha256andmain_targetare accepted and ignored on input.target--executabletargets and modules by path relative to the module root. An ambiguous or unmatched name is an errorvatypeu8 u16 u32 u64 i8 i16 i32 i64. Typed entries take integers, range-checked, little-endian; otherwiseexpectedandvalueare hex-byte stringsname[a-z0-9_]+, unique in the file; shown in progress lines, the record and the verify reportexpected,valuemin,max,aligndescriptionx86
mov r/m64, imm32sign-extends its immediate; patch a 64-bit field through an imm32 withtype = "i32"and amax.Fatal before any output is written. An expected-byte mismatch; a range outside one
PT_LOAD's file bytes; overlapping entries; an unmatched or ambiguous target; an overlap with another in-place writer (Intel reports and trampoline sites, window and island sites, relinker patches, module syscall traps, the ELF header and program-header table), or with a relocation target.Record.
<out>.patches.tomlstarts withformat = 1andmain_target(the main executable's input name), then lists every applied entry with the effectivevalue, the oldexpectedbytes,nameanddescription.Verify.
--verify-patches <file> <path> [--verify-output]. Input mode checks the input executable and its modules. Output mode needs anET_DYNLinux ELF;main_targetpicks the entry checked against that file,--executableentries are checked against their derived outputs, other entries reportnot-checked, and a file withoutmain_targetis refused. Each entry isapplicable,applied,not-checkedormismatch; exit code 0 means nomismatch.Related. #1965 (Hades II bring-up); #5485 (Hades II hub performance). Questions for maintainers: #6207.
AI assistance: the analysis, measurements and this text were produced with LLM assistance; I reviewed them and take responsibility for them.
Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-15T11:28:43Z.
A second case from #6172 (007 First Light, PPSA11386), with the same shape. The title waits for the GPU to write a feedback buffer for at most 2 s of TSC time and skips the wait once the job is two frames old; on our stack the GPU side takes longer than that at a level load, the title reads the buffer stale and crashes. The two limits are instruction immediates in the executable: an imm32 in the compare behind the timeout and an imm8 in the compare behind the frame delta. With both widened, 8 runs of 8 were clean against 10 crashes in 24 unpatched runs the same day. Locally we patch the converted output with a script that checks the old bytes first.
With this proposal it would be two typed entries (
u32, andi8since the imm8 is sign-extended;expectedchecked) applied at conversion and recorded, so a run's log can carry the record and--verify-patchestells another content version apart.For us the patch is a diagnosis, not a fix: the emulator-side direction (a GPU-paced guest clock, or holding the title within two frames of the GPU) is in #6172.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by eliyarson on 2026-10-18T18:06:40Z.
Your 007 First Light case (PPSA11386, from #6172): an imm32 behind the timeout compare, an imm8 behind the frame-delta compare. With #6314 both are expressible as entries (
u32, ori32for a 64-bit compare, andi8) withexpected, applied at conversion and recorded in the manifest.The record/verify part is in #6314:
<output-stem>.patches.tomlbeside the output,--verify-patches <file> <path>checking an input executable, and--verify-patches <file> --verify-output <path>checking a converted output; either mode reports another content version asmismatch.x86
mov/add/sub/cmp r/m64, imm32sign-extend the immediate, so a 64-bit field reached through an imm32 takestype = "i32"plus aminto keep negative values out. For the imm8,type = "i8"withmin/maxkeeps the value in range.PR #6314 (core patcher) is rebased onto current main (
862503857) at head62d881cfa, mergeable; CI has not passed yet — ubuntu, macOS and ASan are green, the Windows relinker job is still running. I have not run it on 007 First Light.Agreed: a patch is a diagnosis, not the fix; the emulator-side direction belongs in #6172.
LLM assistance used.