Skip to content

[AUTOMATED] Name Mach-O exports from the export trie, so a stripped dylib keeps its names - #1365

Merged
mahaloz merged 10 commits into
mainfrom
macho-trie-names
Nov 1, 2026
Merged

mahaloz merged 10 commits into
mainfrom
macho-trie-names

Conversation

@mahaloz

@mahaloz mahaloz commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

A Mach-O dylib stripped of its symbol table (strip -N, or llvm-strip --strip-all) still exports its names through its export trie, but kuna lists sub_328 and cannot decompile by name. The repro builds the issue's source with clang and the ld64.lld rustup ships; no macOS needed.

cat > t.c <<'C'
__attribute__((visibility("default"), noinline))
int audit_exported_function(int x) { return x + 7; }
__attribute__((visibility("default")))
volatile int audit_exported_data = 3;
__attribute__((visibility("default"), noinline))
int audit_caller(int x) { return audit_exported_function(x) + audit_exported_data; }
C
LLD=$(rustc --print sysroot)/lib/rustlib/$(rustc -vV | sed -n 's/host: //p')/bin/gcc-ld/ld64.lld
clang -target arm64-apple-macos11 -O0 -c t.c -o t.o
"$LLD" -arch arm64 -dylib -platform_version macos 11.0 11.0 -fixup_chains -install_name @rpath/libt.dylib -o t.dylib t.o
llvm-strip --strip-all t.dylib -o s.dylib
kuna functions s.dylib
kuna decompile s.dylib audit_exported_function
0x328	sub_328
0x340	sub_340
error: no function "audit_exported_function" in s.dylib; for a stripped binary pass an address with --addr

The fix:

  • object's exports() reads the LC_DYSYMTAB external-defined nlist range, not the trie. The loader now walks LC_DYLD_EXPORTS_TRIE (or the LC_DYLD_INFO export range) itself and adds every trie name the nlist does not already hold at that address.
  • Trie names follow the [AUTOMATED] Keep Mach-O data symbols out of the function inventory and name them as data #1351 rule. A name in a code section is a function entry, spelled as the symbol table spells it (_audit_exported_function), so the stripped dylib lists and decompiles exactly what the unstripped one does: return _audit_exported_function(a0) + _audit_exported_data;. A trie name at an entry the nlist names differently becomes a lookup alias of it. Any other name is a data name and a data symbol. Re-exports are skipped, a stub-and-resolver names its stub, absolute exports are never entries. On 32-bit ARM the trie's Thumb bit is cleared only on a code address, so an exported byte at an odd data address keeps it.
  • The trie is untrusted input, so the walk is bounded by its size. Each node is entered once, so a cycle ends there. A ULEB128 over 64 bits or an offset past the end drops the node. Reads are capped at 4 bytes and kept names at 16 bytes per trie byte, so a quadratic chain stays linear in the trie's size, and the aliases one entry collects are merged in one pass, so tens of thousands of names at one entry list in about a second.
  • resolve_imports reaches the export list about twenty times per load, so the list is cached by the bytes it is read from. A 50,000-export dylib loads no slower than before (1.14 s vs 1.16 s).

Tests: macho_trie_exports_arm64 (the repro) and macho_trie_exports_info_arm64 (classic LC_DYLD_INFO_ONLY trie, no LC_FUNCTION_STARTS, where main finds no functions at all), prebuilt with a .provenance file, plus macho_trie_partial_arm64, whose nlist keeps one of three exports. A loader test and macho_trie_names_cli pin the names, both spellings, the caller's C and the data answer; they fail without the fix. Unit tests cover every export kind plus hostile tries: cycles, ULEB overflow, truncation, quadratic chains, overlapping nodes, and 20,000 mutated tries. Across 53 real Mach-O images, 202 functions in three Swift frameworks get their names, a stripped LC_UNIXTHREAD executable's crt start is found, and dyld_ios15's entry is named _main from its trie, as an unstripped image names it, instead of machomain's int main(int argc, char **argv). ELF and PE output is byte-identical.

Fixes #1349

🤖 Generated with Claude Code

https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE

…he nlist

A dylib stripped with `strip -N` (or a Swift framework that keeps a few external
nlist entries) still exports every name through its export trie, but kuna read
exports from `object`'s `exports()`, which walks the LC_DYSYMTAB external-defined
nlist range. So `kuna functions` listed `sub_328` and `kuna decompile x
audit_exported_function` failed while the address worked.

The loader now walks LC_DYLD_EXPORTS_TRIE / the LC_DYLD_INFO export range and adds
each trie name the nlist does not already hold. A name in a code section is a
function entry spelled as the symbol table spells it (`_audit_exported_function`),
so the stripped image lists what the unstripped one lists; any other is a data
name and a datasym, per the #1351 rule. Re-exports are skipped, a stub-and-resolver
names its stub, absolute exports are never entries. The walk is bounded by the
trie: each node entered once, ULEB128s capped at 64 bits, offsets checked, reads
capped at 4 and names at 16 bytes per trie byte. The export list is cached per
image bytes, so loading a 50k-export dylib is no slower.

On 53 real Mach-O images: 202 functions in three Swift frameworks gain their names,
a stripped LC_UNIXTHREAD executable's crt `start` is found, and dyld_ios15's
LC_MAIN entry is named `_main` from its trie (as unstripped images are). ELF/PE
output is byte-identical.

Fixes #1349.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
@mahaloz mahaloz added the full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default) label Oct 31, 2026
mahaloz and others added 3 commits October 31, 2026 20:09
A partly stripped image (a Swift framework that keeps a few external nlist
entries) can export a second name at an entry its nlist still names. That name
is now a lookup alias of the entry instead of being dropped. New fixture
macho_trie_partial_arm64 pins both shapes: the trie names the entry the nlist
lost, and its other name at the entry it kept selects that entry.

Part of #1349.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
The export trie marks a Thumb function with the low address bit, so the walk
cleared it on every regular export of a 32-bit ARM image. An exported byte at
an odd data address then moved onto its even neighbour: `odd_data` at 0xc9e5
was reported as data at 0xc9e4. Clear the bit only where the even address lies
in a code section.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
`push_loader_aliases` checked each loader alias against the entry's list with
`Vec::contains`, quadratic in the aliases one address holds. A Mach-O export
trie (or a symbol table) that puts tens of thousands of long names at one
entry took 47 s to list (`kuna functions`); a hash set makes it 1.5 s with the
same output.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
@mahaloz

mahaloz commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

[AUTOMATED] Pushed two commits.

  • c844db6: on 32-bit ARM the walk cleared the low bit of every regular export, so an exported byte at an odd data address moved onto its neighbour (a crafted armv7 trie's odd_data at 0xc9e5 was reported as data at 0xc9e4). The bit is now cleared only where the even address is code.
  • 4b179e2: push_loader_aliases deduplicated with Vec::contains, quadratic in the aliases one entry holds. A 2.9 MB hostile trie that puts 44,000 names of about 1 KiB at one entry took 47 s to list; it now takes 1.5 s with the same output. [AUTOMATED] Keep every Mach-O and PE symbol name at a function entry as a lookup alias #1368 carries the identical commit, so the two merge cleanly in either order.

mahaloz and others added 6 commits October 31, 2026 23:18
…e from its witness (CPython eval loop oppool1 758 s -> 137 s) (#1361)

RuleMultiCollapse walked the same failed phi web again on every pass: on CPython 3.11's
_PyEval_EvalFrameDefault, whose dispatch joins have about 190 inputs, one pass made 17.2M
applications. A failed walk now records a witness path, and the next application re-derives the
defining branch from the current IR and re-checks each link of that path before walking. The
walk reads inputs in place instead of copying them. Output is byte-identical (a side-by-side
build that ran the old walk before every application agreed on 32.7M applications); oppool1 on
the eval loop drops from 758 s to 137 s, and ordinary code runs 0.14% fewer instructions.

Part of #1034.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
…ers it at an input (CPython eval loop infertypes 722 s -> 44 s) (#1362)

Type propagation entered a MULTIEQUAL or INDIRECT at one input and then tried every other
input slot, each of which propagate_type refuses: 29.5 B slot attempts per run on CPython 3.11's
_PyEval_EvalFrameDefault. Once the walk enters a join at an input, it now skips the join's
other input slots when the entering type needs no union resolution. Output is byte-identical
(38.6M skips re-run against the old code took 0 edges and changed 0 types); infertypes on the
eval loop drops from 722 s to 44 s, and ordinary code runs 0.37% fewer instructions.

Part of #1034.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
…ady known (CPython eval loop mergerequired 1,884 s -> 953 s) (#1367)

merge_op re-ran cover tests whose answers were already known, on 187-input joins into a stack
slot with 19k instances in CPython 3.11's _PyEval_EvalFrameDefault. It now tests each distinct
earlier high once, lists each high once per test pass, indexes highs with 64 or more instances
by block during a pass, and skips a re-test pass when the recorded refusal reads neither the
trimmed high nor its piece partners. bank_symbol and stackvalueorder skip a member at the same
storage as the one before it. Output is byte-identical (an old-vs-new differential over 26,426
functions found 0 mismatches) and memory is unchanged; mergerequired on the eval loop drops by
about half (1,884 s to 953 s). Unit tests cover the block index and the trim-skip rule.

Part of #1034.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
…as a lookup alias (#1368)

Mach-O and PE now keep every extra symbol-table name at a function entry as a lookup alias, as
ELF already did; there is still one row and one decompile per address, and the reported name
is the first symbol-table entry. A Mach-O export's C spelling is not added as an alias at an
entry the symbol table already names. The `_` retry narrows by the decorated name, so
`audit_shared` picks the function actually named `_audit_shared` over another entry's alias,
and a name that is only an alias at two entries is still reported as ambiguous. MinGW PE pairs
(`fpreset`/`_fpreset`) had the same gap. The console's per-function alias merge is now
linear (a 16 MB image with 30,000 names at one address: 9.3 s to 1.2 s, same output).

Fixes #1357.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
Add `kuna raw-pcode`: each instruction's SLEIGH p-code with its bytes, before any decompiler
pass, using `disassemble`'s target resolution, JSON envelope (same `truncated` meaning) and
windowed load. It lifts linearly and stops at the first lift that fails (exit 1, prefix kept).
`--follow` walks direct flow by the decompiler's xrefControlFlow rule into blocks with
successors and issues, each failed lift reported once. An odd ARM32 address lifts as Thumb at
the even address, in a query-scoped context. No other command's output changes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
@mahaloz
mahaloz merged commit 77ededa into main Nov 1, 2026
8 checks passed
@mahaloz
mahaloz deleted the macho-trie-names branch November 1, 2026 00:25
mahaloz added a commit that referenced this pull request Nov 1, 2026
Brings in #1346 (truncptrflow) and the stage tests landed with #1303,
#1316, #1365, #1366, #1368 and #1370. clonephiorder now sits on top of
truncptrflow; every count is re-derived from phases.toml and the stage
directory: 354 settables, tiers 127 core / 135 transform / 92 analysis,
162 with a live field, 353 "},\n" separators, 354 fixture rows,
KUNA-CATALOG #15 = 360, corpus 623 files (83 datatests + 540 stages).
docs/options.md and phase_catalog.json regenerated with the merged build.
docs/baseline-stages.json re-recorded on the merged tree: main's set plus
RETCOPYCLONE #1-#9, with KUNA-CATALOG #15 renamed (3408 assertions).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013BBaWRVoTEYHNosYwYRoSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

full-ci Run the full cargo workspace suite on this PR before merge (internal PRs skip it by default)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mach-O export-trie names are missed without conventional symbol-table entries

2 participants