Skip to content

Multi-format loader PR-1: ObjectFormat seam (pure refactor, ELF verbatim) + design doc - #32

Merged
mahaloz merged 2 commits into
mainfrom
multiformat-loader-seam
Jun 25, 2026
Merged

mahaloz merged 2 commits into
mainfrom
multiformat-loader-seam

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

What

PR-1 of kuna's multi-format-loader expansion (ELF-only → "a decompiler for most file formats": PE/Mach-O/COFF). This is a PURE REFACTOR that lifts today's ELF-only load logic behind an ObjectFormat trait with ZERO behavioral change — the foundation every later format-PR plugs into. No PE/Mach-O/COFF impls yet (PR-2).

The design doc

docs/multiformat-loader-design.md lands here too — the canonical spec for the whole expansion (§0–§9: the six chokepoints, the ObjectFormat trait, per-format spec/arch selection, per-format import naming, the per-pass compatibility verdict, fixtures/toolchains, parity safety, and the ordered PR-1…PR-14 breakdown). Every file:line in it was re-verified against the live tree.

The seam

New kuna-analysis/src/s1_loader/format/:

  • mod.rs — the ObjectFormat trait, FormatKind, ImportSym, detect(), and a free resolve_imports(file, bytes) dispatch fn.
  • elf.rs — ElfFormat, today's logic moved verbatim: section_bits = the old section_kind_flags body; resolve_imports calls the unchanged elf_plt::resolve_plt_imports; compiler_model returns the same gcc/default tokens; const_ranges = mips_got_const_ranges.

ObjectLoadImage::from_bytes — exactly the three substituted points (everything else verbatim):

  • (B) the non-ELF reject → format::detect(&file)?
  • (C) the section snapshot → fmt.section_bits(...)
  • (F) the PLT call → fmt.resolve_imports(&file, bytes) (plus MIPS const-ranges → fmt.const_ranges(...))
  • (D) language_id_for now threads the seam's compiler_model — but the ELF id strings are byte-identical (ELF→gcc/default), a structural change with no output change. The id shape is composed by a shared compose_language_id, reused by the new elf_language_ids() enumerator so the resolve-in-DB gate cannot drift.

The two other resolve_plt_imports call sites (s1_entry::existing_function_addrs, s1_loader::noreturn::scan_noreturn) call the free format::resolve_imports, with bytes threaded via a new AnalysisCtx.bytes field + a param on existing_function_addrs/collect_entries/listing_seeds. ELF ignores bytes; the seam carries it for the PE/Mach-O typed-reparse PR-2 needs.

ELF-only-reachable: detect() rejects everything but ELF and the engine dispatch still routes only \x7fELF to the object loader, so nothing PE/Mach-O/COFF is constructible from a real binary yet.

Faithfulness proof (the whole point)

This PR changes structure, not behavior. The proof:

  • All 675 datatests + every ELF fixture byte-identical. fauxware, cet_pie_x86_64, stripped_dynamic_x86_64, plt_mips32, cpp_mangled_x86_64, the cross-arch entry/plt fixtures — all now run through the trait path and produce identical output.
  • The XML datatest path never reaches the object loader (Invariant 1), and the ELF arm is the old code lifted verbatim (Invariant 2).

New tests (per §8 PR-1):

  • a kuna-analysis unit asserting ElfFormat::compiler_model/section_bits reproduce the old tokens/bits exactly;
  • a console gate verify_elf_language_ids.rs asserting every id language_id_for can produce — x86-64/x86-32/ARM/AArch64/MIPS/RISCV/SPARC/PPC, both endiannesses where the vendored .ldefs declare a stem — resolves in scan_language_database.

Gate results (all GREEN, all UNCHANGED)

Gate Result
make test 675/675 — PARITY OK
make test-stages 159/159 — PARITY OK
make rust-test green (workspace + golden + .sla parity)

One chokepoint that didn't lift as cleanly as the plan expected

§1.3 framed the free resolve_imports(file, bytes) as a drop-in at the two non-from_bytes call sites, but bytes was not in scope there (their callers carry file-only). Threading it required an AnalysisCtx.bytes field + a bytes param down existing_function_addrs/collect_entries/listing_seeds (and the cross-crate verify_listing_* callers). Mechanical and faithful (ELF ignores bytes) — just a wider ripple than the "three substituted lines" framing suggested.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 25, 2026 00:54
…tim) + design doc

kuna is ELF-only; the goal is a decompiler for most file formats (PE/Mach-O/COFF).
This is PR-1 of that expansion: a NO-BEHAVIOR-CHANGE refactor that lifts today's
ELF-only load logic behind an `ObjectFormat` trait, so ELF becomes an
implementation rather than the privileged default and a later format-PR plugs in
without touching any shared pass.

Design doc: `docs/multiformat-loader-design.md` — the canonical per-format PR plan
(§0–§9, every file:line re-verified against the live tree).

The seam (`kuna-analysis/src/s1_loader/format/`):
- `mod.rs`: the `ObjectFormat` trait, `FormatKind`, `ImportSym`, `detect()`, and a
  free `resolve_imports(file, bytes)` dispatch fn.
- `elf.rs`: `ElfFormat` — today's logic moved verbatim (`section_bits` = the old
  `section_kind_flags` body; `resolve_imports` calls the unchanged
  `elf_plt::resolve_plt_imports`; `compiler_model` returns the same `gcc`/`default`
  tokens; `const_ranges` = `mips_got_const_ranges`).

`ObjectLoadImage::from_bytes` — exactly the three substituted points (everything
else verbatim): (B) the non-ELF reject -> `format::detect(&file)?`; (C) the section
snapshot -> `fmt.section_bits(...)`; (F) the PLT call -> `fmt.resolve_imports(...)`
(plus the MIPS const-ranges -> `fmt.const_ranges(...)`). (D) `language_id_for` now
threads the seam's `compiler_model`, but the ELF id strings are byte-identical
(ELF->gcc/default) — a structural change with no output change. The id shape is
composed by a shared `compose_language_id`, reused by the new `elf_language_ids()`
enumerator so the resolve-in-DB gate cannot drift.

The two other call sites (`s1_entry::existing_function_addrs`,
`s1_loader::noreturn::scan_noreturn`) call the free `format::resolve_imports`, with
`bytes` threaded via a new `AnalysisCtx.bytes` field + a param on
`existing_function_addrs`/`collect_entries`/`listing_seeds` (ELF ignores it; the seam
carries it for the PE/Mach-O typed-reparse PR-2 needs).

ELF-only-reachable: `detect()` rejects everything but ELF and the engine dispatch
still routes only `\x7fELF` to the object loader, so no PE/Mach-O/COFF is
constructible from a real binary yet (those impls + `object` features are PR-2).

Faithfulness proof (the whole point): `make test` 675/675 PARITY OK,
`make test-stages` 159/159 PARITY OK, `make rust-test` green — all UNCHANGED. Every
existing ELF fixture now runs through the trait path and is byte-identical. New
tests: a kuna-analysis unit (`ElfFormat::compiler_model`/`section_bits` reproduce
the old tokens/bits) and a console gate `verify_elf_language_ids.rs` (every id
`language_id_for` can produce — x86-64/x86-32/ARM/AArch64/MIPS/RISCV/SPARC/PPC,
both endiannesses where the vendored `.ldefs` declare a stem — resolves in
`scan_language_database`).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit e1d3258 into main Jun 25, 2026
mahaloz added a commit that referenced this pull request Sep 19, 2026
…arguments when the version line is typed

fmtvalist_x86_64 (gcc -O2 -flto) passes 27-29: off keeps the version line's
three va_list phantoms, the default and full drop them, and no switch call
takes the va_list's slots. The round-6 binary fails #31 and #32.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01YFCA715QE6ZBSnTn7bwabG
mahaloz added a commit that referenced this pull request Sep 19, 2026
…arguments when the version line is typed

fmtvalist_x86_64 (gcc -O2 -flto) passes 27-29: off keeps the version line's
three va_list phantoms, the default and full drop them, and no switch call
takes the va_list's slots. The round-6 binary fails #31 and #32.

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

e_call and stage assertions #31/#32; globalvalue #6 accepts the widening
printed with its product (v4 = (int8)retsel * 0x20001;), which the kept
store COPY now leaves explicit; stage baseline re-recorded (+10 keys).

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

e_call and stage assertions #31/#32; globalvalue #6 accepts the widening
printed with its product (v4 = (int8)retsel * 0x20001;), which the kept
store COPY now leaves explicit; stage baseline re-recorded (+10 keys).

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

e_call and stage assertions #31/#32; globalvalue #6 accepts the widening
printed with its product (v4 = (int8)retsel * 0x20001;), which the kept
store COPY now leaves explicit; stage baseline re-recorded (+10 keys).

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 Nov 14, 2026
mahaloz added a commit that referenced this pull request Nov 14, 2026
… its call keeps its arguments (#1573)

On x86-32, a constant-count `rep movs` (at most 16 elements / 64 bytes) that copies a
by-value struct upward into the outgoing argument area is expanded right after the first
heritage pass into that many copies of its body, with the frame copy that built the struct,
when the block is the next call's arguments (its address goes nowhere else, and the callee
reads into it or its stated/proven pop or gcc's cdecl cleanup releases it). The call takes
the struct's slots, earlier calls keep their pushes, and no return-address push prints as a
value. Delphi-style bottom-of-frame locals, moves beside another frame-walking store, larger
moves and x86-64 stay loops. Stage test kuna-repframecopy; kuna-calleeargs #31/#32 added.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
mahaloz added a commit to borancar/kuna that referenced this pull request Nov 14, 2026
mahaloz pushed a commit that referenced this pull request Nov 23, 2026
…ns (#1909)

The Thumb-2 ands, bics, movs and mvns with a modified immediate, and ands and
mvns with a shifted register, now set C to the shifter's carry-out when S is
set: bit 31 of a rotated constant, the last bit shifted out of the register, or
C itself when nothing is rotated or shifted. They set only N and Z before, so a
bcs, adc or it cc after them read a stale carry (gcc's `ands.w ..., asr #32;
it cc` signed-division idiom in armhf glibc decompiled wrongly). orrs, eors,
tst and the other logical forms already did this; movs.w of an unshifted
register still leaves C alone. The ARM8_le compiler-oracle digest is
regenerated with slacomp.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWTdjVzTz11WxPZmJ9b
mahaloz added a commit that referenced this pull request Nov 24, 2026
…erently

[AUTOMATED] State::diff_constants records each differing location as an
aligned word, narrowed to one half only when the other half is unchanged.
Themida packs its context cells at unaligned addresses: in zlib_themida
(gz_read) the interpreter's copy of the frame pointer (rsp - 0x58) sits in
the cell at 0x18003fb19, so its top byte is the first byte of the aligned
word 0x18003fb20, whose other bytes belong to a cell the joining paths set
differently. Both halves of that word differ, so the merge symbolized all
of it, and the frame pointer copy read back as
PIECE(SUB1(merge symbol), SUB7(rsp) - 0x58). Every access through it (49:
the n, buf, state and len home slots) could no longer be placed; the
stores among them were lost, the loads returned unknowns, and the
decompiler, which resolved some of those reads to the frame but not the
loop's writes, printed strm.next_out = the entry buf.

A location left whole by the halving is now narrowed to the run from the
first to the last byte the two sides hold as different nodes (both sides
symbolic, as for the halving), as angr's page merge (ListPage.merge) starts
a merged object at the first changed byte. zlib_themida: 0 unplaced
accesses (was 49), same 11341 nodes / 12 rounds, next_out = the advanced
buf, memcpy and gz_load get it, n/got/len updates as the source.

Fixture f_cell (stage #32): v written through a frame pointer copy kept in
an unaligned context cell beside one two arms set to a0 and a1; returned 1
before, 2 now. Unit test for the narrowing. Stage baseline 5048.
Spec 02 2.11 (symbolization fixpoint, fixture list).

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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