Repository navigation
Multi-format loader PR-6+7: Mach-O linked-exe + __stubs import naming (printf on a Mac binary) - #49
Merged
Merged
Conversation
… (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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Mach-O headline
A linked Mach-O executable loads, decompiles a function, and its libc import renders named —
printf, notsub_<stub>. This is the Mach-O counterpart to the PE IAT headline (PR-3+4), and the simplest of the import-naming analogs: abl/callqtargets__TEXT,__stubsdirectly, so naming the stub entry is enough (no constant-fold, arch-independent).BEFORE → AFTER (x86-64
_main,kuna decompile … --experimental-formats)arm64
_mainrendersprintf(0x1000005d0)identically — the same source linked for two arches, proving the naming is arch-independent.How
mainreachesprintf(and how it's named)In both fixtures
mainreachesprintfby a direct branch to the__stubsentry — x86-64callq 0x1000005cc, arm64bl 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 atsec.addr + i*reserved2(pure section metadata), resolving the name through theLC_DYSYMTABindirect-symbol table →LC_SYMTAB(_printf, leading_stripped). It also names theS_LAZY/NON_LAZY_SYMBOL_POINTERSslots (__la_symbol_ptr/__got, the GOT analog) defensively.INDIRECT_SYMBOL_LOCAL|ABSandn_strx==0are skipped; fat0xcafebabeselects 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. Bareclang(no macOS SDK) compiles the object;ld64.lldemits the classicS_SYMBOL_STUBSindirect-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 arm64macho_imports.c=_maincallingprintf("%d\n", compute(argc)). Verified withllvm-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. thedecomp_dbgsubprocess spawn +.slaload), ~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__stubswalk is a single load-command pass + per-section indirect-table slice — cheaper than the PE IAT walk (noFF 25.textsweep; Mach-O calls the stub directly).Tests (the e2e RAN — not skipped; the
x86/AARCH64.slaare built)macho_stubsunit tests (3, synthetic — mirrorelf_plt/pe_iat, so the merge gate needs no linker): underscore strip; stub-entry naming +INDIRECT_SYMBOL_LOCALfiltering; pointer-slot naming.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 assertsprintf(and notsub_<stub>, plus the default-off rejection.Gates
make test— 675/675 PARITY OKmake test-stages— 159/159 PARITY OKmake 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 —objectsurfacesvmaddr/LC_MAINtransparently.🤖 Generated with Claude Code
https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb