Skip to content

Multi-format loader PR-12+13: PE/Mach-O entry discovery (stripped binaries find their functions) - #63

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

mahaloz merged 2 commits into
mainfrom
multiformat-loader-entry-disc

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

What

Ports the PE and Mach-O analogs of the ELF entry oracles so a stripped
PE/Mach-O finds its functions automatically (no --addr), exactly as a stripped
ELF does today. EntryDiscoveryPass (s1_entry) now dispatches its oracle
selection on the object format
via the ObjectFormat seam; the ELF oracles are
unchanged and the new PE/Mach-O oracles are no-ops on ELF.

Design docs/multiformat-loader-design.md §4.1 / §5.3 / §8 PR-12 + PR-13.

Entry sources implemented per format

  • PE (s1_entry/pe_entry.rs): the entry point (AddressOfEntryPoint+ImageBase,
    via file.entry()); .pdata RUNTIME_FUNCTION BeginAddresses (the
    .eh_frame analog — the richest source, 97 records in the fixture); TLS
    callbacks (AddressOfCallBacks); and exports. The glibc _start→main idiom
    is not reused (.pdata recovers main directly).
  • Mach-O (s1_entry/macho_entry.rs): the entry (LC_MAIN/LC_UNIXTHREAD —
    note LC_MAIN's entryoff is __TEXT-relative, so the VMA is
    __TEXT.vmaddr + entryoff; object's file.entry() returns the un-rebased
    offset for Mach-O); LC_FUNCTION_STARTS (the ULEB128 delta table — the
    richest, stripped-surviving source); __mod_init_func; and exports.

executable_sections learned the PE COFF (IMAGE_SCN_MEM_EXECUTE) and Mach-O
(S_ATTR_*_INSTRUCTIONS) executable tests. Discovery stays purely additive
(union → exec-section filter → funcsym-dedup); only the oracle selection
branches on format.

Before → after (the payoff)

  • Stripped PE (pe_imports_stripped.exe, 0 symbols / 0 exports):
    • before (--option entry_disc off): no function "sub_140001592" — finds
      nothing by name.
    • after: the entry + .pdata recover dozens incl. main@0x140001592 →
      kuna decompile … sub_140001592 (no --addr) yields
      sub_140001592(uint a0){ … puts(…); … } — discovered by name and its
      IAT-named puts import renders (PR-12 + PR-4 together).
  • Stripped Mach-O (macho_func_starts_stripped, helper is file-local so
    ld64.lld -x removed its symbol): LC_FUNCTION_STARTS =
    [0x100000550 (_main, the entry), 0x100000590 (helper, stripped)]. Before,
    helper is invisible; after, collect_entries skips the symboled _main
    and discovers 0x100000590 → kuna decompile … sub_100000590 (no --addr)
    yields int4 sub_100000590(int4 a0){ return a0 * 7 + 3; } — proving
    LC_FUNCTION_STARTS, not the symtab, is the source.

e2e

kuna-console/tests/verify_multiformat_entry.rs (3 tests) asserts the discovery

  • decompile-by-discovered-name (no --addr) on stripped PE + stripped Mach-O,
    plus the default-off rejection. It RUNS, not skipped (1.64s, .sla built).
    Plus 4 new kuna-analysis unit tests + 2 new collect_entries core tests.

Speed

Full kuna decompile of a discovered function, no --addr
(--experimental-formats, release): stripped-PE main
(sub_140001592, .pdata-discovered) 0.11s (51 MB RSS); stripped-Mach-O
helper (sub_100000590, FUNCTION_STARTS-discovered) 0.16s.

Gates

  • make test — 675/675 PARITY OK
  • make test-stages — 159/159 PARITY OK
  • make rust-test — green (incl. the new e2e)

Default-off ⇒ ELF byte-identical: the new oracles are no-ops on ELF and PE/Mach-O
only load under --experimental-formats. verify_s1_entry +
verify_crossarch_entry_main pass unchanged; discovery is gateable
(--option entry_disc off).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 25, 2026 14:18
…aries find their functions)

Port the PE and Mach-O analogs of the ELF entry oracles so a STRIPPED PE/Mach-O
recovers its function starts automatically (no `--addr`), exactly as a stripped
ELF does. `EntryDiscoveryPass` now dispatches its oracle selection on the object
format via the ObjectFormat seam; the ELF oracles stay unchanged and the new
PE/Mach-O oracles are no-ops on ELF.

PE (s1_entry/pe_entry.rs): the entry point, the `.pdata` RUNTIME_FUNCTION table
(the `.eh_frame` analog — 97 records recover `main` in the stripped fixture),
TLS callbacks, and exports.

Mach-O (s1_entry/macho_entry.rs): the `LC_MAIN`/`LC_UNIXTHREAD` entry
(`__TEXT.vmaddr + entryoff` — `object`'s `file.entry()` returns the un-rebased
entryoff for Mach-O), `LC_FUNCTION_STARTS` (the ULEB128 delta table — the
richest, stripped-surviving source), `__mod_init_func`, and exports.

`executable_sections` learned the PE COFF / Mach-O instruction-attribute exec
tests. The discovery stays purely additive (union -> exec-section filter ->
funcsym-dedup); only the oracle selection branches on the format.

Before/after: a stripped PE finds `main`@0x140001592 via `.pdata` ->
`sub_140001592` decompiles by name (no --addr, 0.11s) with its IAT-named `puts`;
a stripped Mach-O finds the symbol-removed `helper`@0x100000590 via
LC_FUNCTION_STARTS -> `sub_100000590` decompiles (0.16s). New e2e gate
`verify_multiformat_entry` (3 tests) RUNS (not skipped).

Gates: make test 675/675 PARITY OK, make test-stages 159/159 PARITY OK,
make rust-test green (incl. the new e2e + 4 new kuna-analysis unit tests). The
ELF gates verify_s1_entry / verify_crossarch_entry_main pass unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
…try-disc

# Conflicts:
#	docs/analysis-port-log.md
@mahaloz
mahaloz merged commit 0f6cd9c into main Jun 25, 2026
@mahaloz
mahaloz deleted the multiformat-loader-entry-disc branch July 29, 2026 08:01
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