Skip to content

Multi-format loader PR-6+7: Mach-O linked-exe + __stubs import naming (printf on a Mac binary) - #49

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

mahaloz merged 2 commits into
mainfrom
multiformat-loader-macho-imports

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

The Mach-O headline

A linked Mach-O executable loads, decompiles a function, and its libc import renders named — printf, not sub_<stub>. This is the Mach-O counterpart to the PE IAT headline (PR-3+4), and the simplest of the import-naming analogs: a bl/callq targets __TEXT,__stubs directly, so naming the stub entry is enough (no constant-fold, arch-independent).

BEFORE → AFTER (x86-64 _main, kuna decompile … --experimental-formats)

// BEFORE (resolve_imports empty)          // AFTER (PR-7 __stubs naming)
unsigned long _main(int4 a0)               unsigned long _main(int4 a0)
{                                          {
  sub_1000005cc(0x1000005ee,a0 * 3 + 7);     printf(0x1000005ee,a0 * 3 + 7);
  return 0;                                  return 0;
}                                          }

arm64 _main renders printf(0x1000005d0) identically — the same source linked for two arches, proving the naming is arch-independent.

How main reaches printf (and how it's named)

In both fixtures main reaches printf by a direct branch to the __stubs entry — x86-64 callq 0x1000005cc, arm64 bl 0x1000005a0. Unlike the PE IAT slot / ELF GOT slot (a pointer the engine constant-folds), there is no slot to fold and no per-arch stub decode. PR-7 names the stub entry at sec.addr + i*reserved2 (pure section metadata), resolving the name through the LC_DYSYMTAB indirect-symbol table → LC_SYMTAB (_printf, leading _ stripped). It also names the S_LAZY/NON_LAZY_SYMBOL_POINTERS slots (__la_symbol_ptr/__got, the GOT analog) defensively. INDIRECT_SYMBOL_LOCAL|ABS and n_strx==0 are skipped; fat 0xcafebabe selects one slice (prefer x86-64, then arm64) and re-dispatches. Pure/total/never-error — unknown → empty.

New module: s1_loader/macho_stubs.rs (design §3.3). MachOFormat::resolve_imports (was empty) now calls it; the downstream commit seam (funcsym dedup → FunctionSymbol) is unchanged.

How the Mach-O fixture was produced

Linked in-container with ld64.lld (the rustup-bundled LLD darwin flavor) — not vendored-prebuilt. Bare clang (no macOS SDK) compiles the object; ld64.lld emits the classic S_SYMBOL_STUBS indirect-symbol layout PR-7 walks, so the design's vendored-prebuilt fallback wasn't needed.

clang -target x86_64-apple-macos11 -O1 -c macho_imports.c -o m.o
ld64.lld -arch x86_64 -platform_version macos 11.0 11.0 \
         -undefined dynamic_lookup -e _main -o macho_imports m.o
# arm64: -target arm64-apple-macos11 + -arch arm64

macho_imports.c = _main calling printf("%d\n", compute(argc)). Verified with llvm-objdump --macho -d / llvm-otool -Iv: __stubs _printf@0x1000005cc (x64) / 0x1000005a0 (arm64), reached by a direct call. Fixtures: macho_imports (16 KB, x86-64), macho_imports_arm64 (49 KB).

Decompile speed

macho_imports _main: ~0.3–0.8 s wall (time kuna decompile, end-to-end incl. the decomp_dbg subprocess spawn + .sla load), ~0.10 s user CPU for the decompile itself; arm64 ~0.35 s. On par with the ELF/PE baselines (both dominated by process startup + the tiny function). The __stubs walk is a single load-command pass + per-section indirect-table slice — cheaper than the PE IAT walk (no FF 25 .text sweep; Mach-O calls the stub directly).

Tests (the e2e RAN — not skipped; the x86/AARCH64 .sla are built)

  • macho_stubs unit tests (3, synthetic — mirror elf_plt/pe_iat, so the merge gate needs no linker): underscore strip; stub-entry naming + INDIRECT_SYMBOL_LOCAL filtering; pointer-slot naming.
  • e2e kuna-console/tests/verify_macho_imports.rs (3): x86-64 by name + by VMA (the clean PR-7 proof — no symtab reliance), arm64 by name (arch-independence). Each asserts printf( and not sub_<stub>, plus the default-off rejection.

Gates

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

Default-off ⇒ ELF byte-identical: the Mach-O path is unreachable without --experimental-formats, and the XML/ELF oracles never reach the object loader (Invariant 1). No loadimage fix was needed — object surfaces vmaddr/LC_MAIN transparently.

Note: s1_loader/format/ + docs/analysis-port-log.md are also touched by the parallel COFF PR; a trivial conflict (distinct format files / log append) is expected.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 25, 2026 04:25
… (printf on a Mac binary)

The Mach-O headline: a linked Mach-O executable loads, decompiles a
function, and its libc import renders NAMED (printf, not sub_<stub>).

PR-6 (loader proof): the linked Mach-O loads through the same
bootstrap_from_object path the Mach-O object used — no loadimage fix
needed; object surfaces vmaddr/LC_MAIN transparently, and PR-2's
MachOFormat::{section_bits, compiler_model} (x86-64 -> gcc, the System V
AMD64 cspec; arm64 -> default) were already finalized.

PR-7 (the __stubs walk, new s1_loader/macho_stubs.rs, design 3.3):
MachOFormat::resolve_imports now walks the LC_DYSYMTAB indirect-symbol
table. For each S_SYMBOL_STUBS section it names the stub entry directly
(sec.addr + i*reserved2) — a bl/callq targets __stubs directly, so naming
the stub entry is enough (no constant-fold, arch-independent); it also
names the S_LAZY/NON_LAZY_SYMBOL_POINTERS slots (the GOT analog). Names
resolve via the indirect index into LC_SYMTAB; INDIRECT_SYMBOL_LOCAL|ABS
and n_strx==0 are skipped, the leading _ stripped. Fat 0xcafebabe selects
one slice (prefer x86-64, then arm64) and re-dispatches. Pure/total/
never-error: unknown -> empty. Exports appended additively.

Before -> after (x86-64 _main, --experimental-formats):
  sub_1000005cc(0x1000005ee,a0*3+7);  ->  printf(0x1000005ee,a0*3+7);

Fixtures: macho_imports (x86-64) + macho_imports_arm64 (arm64), the same
macho_imports.c linked in-container with the rustup-bundled ld64.lld
(no vendored-prebuilt needed) — proving the naming is arch-independent.

Tests: 3 macho_stubs synthetic unit tests (underscore strip, stub-entry
naming + LOCAL filtering, pointer-slot naming) mirroring elf_plt/pe_iat,
so the merge gate needs no linker; 3 e2e (kuna-console/tests/
verify_macho_imports.rs) that RAN (not skipped) — x86-64 by name + by VMA,
arm64 by name — each asserting printf( and not sub_<stub>, plus the
default-off rejection.

Gates: make test 675/675 PARITY OK; make test-stages 159/159 PARITY OK;
make rust-test green (incl. the 3 unit + 3 e2e). Default-off => ELF
byte-identical (the Mach-O path is unreachable without
--experimental-formats; the XML/ELF oracles never reach the object loader).

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

# Conflicts:
#	decompiler/crates/kuna-analysis/tests/fixtures/README.md
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