Skip to content

Multi-format loader PR-2: object features + format dispatch (--experimental-formats) - #38

Merged
mahaloz merged 1 commit into
mainfrom
multiformat-loader-dispatch
Jun 25, 2026
Merged

mahaloz merged 1 commit into
mainfrom
multiformat-loader-dispatch

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

PR-2 of the multi-format-loader expansion (docs/multiformat-loader-design.md §1.2/§1.4/§8/§2). Builds on PR-1's ObjectFormat seam: turn on object's PE/Mach-O/COFF readers, add the three format skeletons, and wire the engine dispatch — all behind --experimental-formats, default-off ⇒ byte-identical.

BEFORE → AFTER

  • BEFORE: a PE/COFF or Mach-O binary fed to load file is rejected — it never reaches the object loader (the ELF_MAGIC dispatch routes it to the XML branch, which fails to parse it; the object loader's hard reject would say "not an ELF object" anyway, and object couldn't even parse it — pe/macho/coff features off).
  • AFTER (with --experimental-formats): PE/Mach-O/COFF objects load, map their sections with the right exec/readonly bits, and select the right SLEIGH spec — x86:LE:64:default:windows for PE/COFF, x86:LE:64:default:gcc for Mach-O (macOS x86-64 is System V AMD64). disassemble yields real x86-64 instructions out of .text/__text. Imports are empty (IAT/__stubs naming is PR-4/PR-7).

How --experimental-formats is exposed

One mechanism, two surfaces: the KUNA_EXPERIMENTAL_FORMATS env var (read live in bootstrap_from_file, so decomp_dbg/decomp_test_dbg and the in-process e2e all honor it), and a kuna decompile --experimental-formats CLI flag that exports it onto the spawned decomp_dbg subprocess. By default is_object_binary admits only ELF — the non-ELF magics (Mach-O 0xfeedfac*/fat, PE MZ, bare-COFF machine prefix) are gated on the flag.

Per-format spec selection (proven)

The extended verify_elf_language_ids.rs gate asserts every produced id resolves in scan_language_database, with the headline ids resolving directly:

Format x86-64 id arm64 id
PE / COFF x86:LE:64:default:windows AARCH64:LE:64:v8A:windows
Mach-O x86:LE:64:default:gcc AARCH64:LE:64:v8A:default

The §2.2 compiler-model fallback (fallback_arch_id() → the default-model id; bootstrap_from_object retries with it when the primary doesn't resolve and no --target was given) is the safety net — for every headline arch the primary resolves directly, so the fallback isn't hit in practice.

Default-off byte-identical proof

kuna decompile fauxware main (a real x86-64 ELF) produces identical output with and without --experimental-formats — the flag is a pure no-op on ELF (ELF is always admitted). The XML/datatest dispatch branch and bootstrap_program are untouched (Invariant 1).

Gate results

  • make test — 675/675 PARITY OK
  • make test-stages — 159/159 PARITY OK
  • make rust-test — green (incl. 8 format unit tests + 2 language-id gate tests + a §2.2 fallback unit + the 2-test verify_object_formats e2e; the e2e RAN, not skipped — the x86 .sla is built)

Cargo.lock note

decompiler/Cargo.toml: object features ["read_core","elf","std"] → +["pe","macho","coff"]. Cargo.lock is unchanged — enabling these object features pulls no new crate versions or transitive deps (the PE/Mach-O/COFF readers share object's existing deps). Confirmed offline-safe via cargo build --offline (zero downloads). An unchanged lock is the correct offline-gate outcome.

Fixtures

Built in-container with the existing toolchain (no new packages): pe_min.obj (clang -target x86_64-pc-windows-gnu -c → Intel amd64 COFF object) and macho_min.o (clang -target x86_64-apple-macos11 -c → relocatable Mach-O). VMAs/section bits pinned from the object-crate view.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

…patch (--experimental-formats)

Enable object's pe/macho/coff readers, add the three ObjectFormat skeletons,
and wire is_object_binary to route their magics to the object loader — all
behind --experimental-formats so the default path stays ELF-only and
byte-identical. Builds on the PR-1 ObjectFormat seam (Increment 35).

- decompiler/Cargo.toml: object features +["pe","macho","coff"]. Cargo.lock
  unchanged (no new transitive crates — the offline-safe outcome); confirmed
  via `cargo build --offline` (zero downloads).
- s1_loader/format/{pe,macho,coff}.rs: skeletons. compiler_model per §2
  (PE→windows, Mach-O→gcc[x86-64]/default[arm64], COFF→windows); section_bits
  (PE/COFF share a Characteristics-keyed helper, Mach-O keys on SectionKind +
  S_ATTR_PURE_INSTRUCTIONS); resolve_imports empty (IAT/__stubs naming is
  PR-4/PR-7). detect() now constructs all four.
- engine.rs: bootstrap_from_elf → bootstrap_from_object (format-neutral; all
  callers + verify_* imports updated). ELF_MAGIC test → is_object_binary per
  §1.4 — admits ONLY ELF by default; PE/Mach-O/COFF/COFF magics are gated on
  KUNA_EXPERIMENTAL_FORMATS. The XML/bootstrap_program branch (Invariant 1) is
  untouched.
- --experimental-formats exposed via the KUNA_EXPERIMENTAL_FORMATS env var
  (read live in bootstrap_from_file) + a `kuna decompile --experimental-formats`
  CLI flag that exports it onto the decomp_dbg subprocess.
- §2.2 compiler-model fallback: ObjectLoadImage::fallback_arch_id() (the
  default-model id); bootstrap_from_object retries with it when the primary
  doesn't resolve and no --target was given. ELF carries no fallback.

Fixtures (built in-container, no new packages): pe_min.obj (clang
x86_64-pc-windows-gnu → Intel amd64 COFF), macho_min.o (clang
x86_64-apple-macos11 → relocatable Mach-O).

Tests (all ran, not skipped — x86 .sla built): 8 format unit tests; the
language-id resolves gate extended (PE/Mach-O/COFF ids resolve directly:
PE→x86:LE:64:default:windows, Mach-O→x86:LE:64:default:gcc); a §2.2 fallback
unit test; e2e verify_object_formats.rs (2 tests) — each fixture parses, maps
CODE|READONLY sections, selects the right spec, disassembles real x86-64, and
is rejected with the flag off (the default-off proof).

Gates: make test 675/675 PARITY OK; make test-stages 159/159 PARITY OK;
make rust-test green. Default `kuna decompile` on an ELF is byte-identical with
and without --experimental-formats (the flag is a no-op on ELF).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit 5629254 into main Jun 25, 2026
@mahaloz
mahaloz deleted the multiformat-loader-dispatch branch July 29, 2026 08:01
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ad, before a call

Adds e_bcall (call on a branch), e_lcall (after a loop of loads) and e_arg
(the call takes the global's old value) to storealias_x86_64.c, rebuilds the
three fixtures (SHAs in the README), round-trips them in store_alias.rs and
adds stage assertions #33-#38.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
mahaloz added a commit that referenced this pull request Oct 2, 2026
…ad, before a call

Adds e_bcall (call on a branch), e_lcall (after a loop of loads) and e_arg
(the call takes the global's old value) to storealias_x86_64.c, rebuilds the
three fixtures (SHAs in the README), round-trips them in store_alias.rs and
adds stage assertions #33-#38.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_012UUBmGqZTcD5N8LxpVsiQt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant