Skip to content

Multi-format loader PR-3+4: PE linked-exe + IAT import naming (puts/printf on a Windows PE) - #40

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

mahaloz merged 1 commit into
mainfrom
multiformat-loader-pe-imports

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

The PE headline

A linked Windows PE32+ executable now loads and decompiles a function with its libc imports NAMED (puts/printf, not sub_<addr>) — the first non-ELF binary kuna names imports on. PR-2 proved a PE object parses; this proves a full linked exe loads (ImageBase + AddressOfEntryPoint), decompiles, and names the imports.

  • PR-3 (loader proof) needed no new loadimage fix — object surfaces the PE ImageBase+RVA transparently, so the linked exe rides the same bootstrap_from_object path the PE object did.
  • PR-4 (IAT naming) is the real new logic, in a new s1_loader/pe_iat.rs.

BEFORE → AFTER (the headline)

The clean PR-4 delta is on the stripped PE (pe_imports_stripped.exe), where the symbols are gone so the puts call is named only by the IAT walk + thunk decode. main (no symbol) decompiled by address (0x140001592 --addr):

// BEFORE  (PeFormat::resolve_imports empty)        // AFTER  (PR-4 IAT/thunk naming)
unsigned long long sub_140001592(unsigned int a0)   unsigned long long sub_140001592(unsigned int a0)
{                                                   {
  sub_140001680();                                    sub_140001680();
  sub_140007240(0x140009000);          // <--          puts(0x140009000);                // named!
  sub_140001550(0x140009006,a0);                       sub_140001550(0x140009006,a0);    // local printf wrapper, not an import
  return 0;                                            return 0;
}                                                   }

On the non-stripped pe_imports.exe (kuna decompile … main), the headline renders:

unsigned long long main(unsigned int a0)
{
  __main();
  puts(0x140009000);
  printf(0x140009006,a0);
  return 0;
}

How main calls the imports (and how each is named)

  • puts — main does a direct call 0x140007240 to a MinGW thunk veneer (jmp [rip+0x60f6] = FF 25 → the __imp_puts IAT slot at 0x14000d33c). PR-4 names it by walking the Import Directory (slot 0x14000d33c → puts) and decoding the FF 25 veneer over .text so the thunk entry 0x140007240 (the actual call target) is named puts. This is the load-bearing MinGW case.
  • printf — a local MinGW wrapper function at 0x140001550 (it internally calls vfprintf), not an import. In the non-stripped exe it's named from the COFF symtab (printf); in the stripped exe it correctly stays sub_140001550 (we name only genuine imports).

What PR-4 implements (s1_loader/pe_iat.rs, design §3.2)

  • Branch on FileKind::Pe32/Pe64, re-parse with the typed PeFile32/PeFile64 (the neutral object::File::imports() gives no slot address). Walk the Import Directory pairing the INT (names) and IAT (slots) in lockstep → ImportSym{addr: image_base + first_thunk_rva + i*ptr, name} — the ELF GOT-slot analog, arch-independent (the engine constant-folds call [slot]).
  • Decode the FF 25 thunk veneers over the executable sections and name the thunk entry with its slot's import (same recovery as elf_plt::decode_x86_64; x86 only).
  • Import::Ordinal(n) → <DLL>_Ordinal_<n>; file.exports() registered additively. Pure/total/never-error: unknown → empty Vec.

Decompile speed

pe_imports.exe main: ~0.12–0.25 s wall (end-to-end incl. the decomp_dbg subprocess spawn); the stripped main by addr is the same range. On par with the ELF baseline (fauxware main, ~0.15 s on the same box) — both dominated by process startup + the tiny function. PE loading is not materially slower than ELF: the IAT walk is one Import-Directory scan + one FF 25 sweep over .text, once at load.

Tests (the e2e RAN — not skipped; specs built)

  • e2e kuna-console/tests/verify_pe_imports.rs (2): the non-stripped linked exe loads (Windows x86-64 spec), resolves main/puts, decompiles main to puts(/printf(; the stripped exe names the puts thunk via the IAT walk (puts(, not sub_140007240) while leaving the local printf wrapper sub_<addr>. Test 1 also proves the default-off path (PE rejected without the flag).
  • pe_iat unit tests (4, synthetic, mirroring elf_plt's decoder tests): FF 25 decode both widths (x64 RIP-relative + x86 absolute), an unknown-slot veneer left alone, ordinal-name synthesis.

Gates

  • make test — 675/675 PARITY OK
  • make test-stages — 159/159 PARITY OK
  • make rust-test — green (exit 0; incl. the 2 e2e + 4 pe_iat unit tests, 0 failures)

Default-off (--experimental-formats unset) ⇒ the ELF path is byte-identical (the PE path is unreachable without the flag; the XML/datatest oracles never reach the object loader — Invariant 1).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

…rintf on a Windows PE)

The PE headline: a linked Windows PE32+ executable loads and decompiles a
function with its libc imports NAMED (puts/printf, not sub_<addr>) — the first
non-ELF binary kuna names imports on.

PR-3 (loader proof) needed no new loadimage fix: `object` surfaces the PE
ImageBase+RVA transparently, so the linked exe loads through the same
`bootstrap_from_object` path PR-2's PE object did. PR-4 (IAT naming) is the real
new logic, in a new `s1_loader/pe_iat.rs`:

- Walk the PE Import Directory pairing the INT (names) and IAT (slot addresses)
  in lockstep — the i-th name belongs to the slot at
  image_base + first_thunk_rva + i*ptr (re-parsed with the typed
  PeFile32/PeFile64, since the neutral object::File view gives no slot address).
  Each slot -> ImportSym{addr: slot_va, name}: the ELF GOT-slot analog, so the
  engine constant-folds a `call [slot]` to the name (arch-independent).
- MinGW thunk wrinkle: MinGW routes a direct `call thunk` to a one-instruction
  `jmp [rip+disp]` (FF 25) veneer through the IAT slot. So pe_iat ALSO decodes
  the FF 25 veneers over .text and names the thunk entry with its slot's import
  name (the same FF 25 recovery as elf_plt::decode_x86_64). x86 only.
- Import::Ordinal(n) -> <DLL>_Ordinal_<n>; file.exports() registered additively.
  Pure/total/never-error: non-PE / no-import-dir / unparsable -> empty Vec.

Before -> after on the stripped fixture (the clean PR-4 delta):
  sub_140007240(0x140009000);  ->  puts(0x140009000);

Fixtures (MinGW, x86_64-w64-mingw32-gcc in kuna-dev): pe_imports.exe
(non-stripped, the headline) + pe_imports_stripped.exe (the PR-4 proof — symbols
gone, so the puts thunk is named only by the IAT walk). e2e
verify_pe_imports.rs (2) + pe_iat synthetic unit tests (4, mirroring elf_plt).

Default-off (--experimental-formats unset) => ELF byte-identical. Gates:
make test 675/675 PARITY OK, make test-stages 159/159 PARITY OK, make rust-test
green. Decompile speed ~0.12-0.25s, on par with ELF.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit bd6426f into main Jun 25, 2026
@mahaloz
mahaloz deleted the multiformat-loader-pe-imports branch July 29, 2026 08:01
mahaloz added a commit to borancar/kuna that referenced this pull request Nov 16, 2026
…s cleanups

Two cases the cleanup scans read differently from before this PR:

- A C table call through ECX whose cleanup MSVC defers past the next
  __cdecl call (`push 5; call [eax+0x40]; push 6; call cfn; add esp,8`)
  got ECX as a stray receiver once the virtual-call check stopped at the
  next call. Past a later call the check now counts a run of pops that
  starts below where the virtual call returns (it removes both calls'
  pushes); a run starting at or above it is still frame teardown or follows
  a later callee popping earlier pushes, so vepilog/vouter keep their
  receiver. The helper is renamed own_cleanup. Fixture gains vdefer
  (0x4012c0), stage assertion CALLEEREGINPUTS Noelo-Lab#40.
- The FS stop now applies only to a store that leaves ESP alone
  (`mov fs:[0],ecx`). Delphi's constructor epilogue
  (`call AfterConstruction; pop dword ptr fs:[0]; add esp,0xc`) reads as
  before; stopping there moved a constructor's pushed arguments onto the
  call before it (`sub_4054ec(0,a2); sub_41ce0c();`) in three Delphi images.

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 16, 2026
Resolves docs/baseline-stages.json with a key-level 3-way merge
(main's SPARC64 spill assertions plus CALLEEREGINPUTS Noelo-Lab#31-Noelo-Lab#40).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
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