Skip to content

Load a Mach-O relocatable object - #787

Merged
ltfish merged 1 commit into
masterfrom
feature/macho-relocatable-object
Sep 3, 2026
Merged

ltfish merged 1 commit into
masterfrom
feature/macho-relocatable-object

Conversation

@zardus

@zardus zardus commented Aug 25, 2026 •

Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

A Mach-O MH_OBJECT is refused outright. On the aarch64 and x86_64
relocatable-object fixtures added by angr/binaries#193 the load ends here:

tests/aarch64/relocatable_object.macho:
  CLECompatibilityError: Unsupported Mach-O file type: 1. Please open an issue if you need support for this
tests/x86_64/relocatable_object.macho:
  CLECompatibilityError: Unsupported Mach-O file type: 1. Please open an issue if you need support for this

Nothing downstream sees the file at all — no sections, no symbols, no arch.

Root cause

The gate that raises only chooses a base-address policy; every load command,
segment and section path after it is already shared with the filetypes that do
load. A relocatable object is linked against 0 and carries one unnamed segment
holding every section, so its situation is the same as a dylib loaded as the
main object, which that same gate already admits.

Fix

Admit MH_OBJECT on the dylib branch of that gate. Relocations are
deliberately not applied, so an intra-object call still reads as a branch to
itself; that matches how ELF ET_REL and COFF objects are already handled
here, rather than being new behaviour.

Testing

tests/test_macho.py::test_relocatable_object loads both fixtures and asserts
the sections and symbols; on master it fails with the
CLECompatibilityError above. After the change the aarch64 object maps
[0x0:0x1e7] and the x86_64 one [0x0:0x1c7], each with __text followed by
__bss and the four symbols _keccak_init, _keccak_absorb,
_keccak_squeeze and _state.

Needs those fixtures.

These three Mach-O changes complement rather than replace each other; the
intended order is cle 728, then cle 674, then cle 787, and each closes a case
the others leave standing.

Validation: #787 (comment)

session: sharpen

@angr-bot

Copy link
Copy Markdown
Member

Corpus decompilation diffs can be found at angr/dec-snapshots@master...angr/cle_787

@zardus

zardus commented Aug 27, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Validation record for head 2c0d6690220bb7c3bb7dc1797a914e7e572082f3 against baseline 46a37333f4f59b0facf8774ee743ebc4cc074e9b, with the fixtures of angr/binaries#193 at af665c9f4fcf7247694053e74ca178450343a46b checked out beside the cle worktree.

  • Regression: python -m pytest tests/test_macho.py::test_relocatable_object -p no:randomly -q — on the baseline it fails with cle.errors.CLECompatibilityError: Unsupported Mach-O file type: 1, raised at cle/backends/macho/macho.py:213; on head, 1 passed
  • Focused: python -m pytest tests/test_macho.py -p no:randomly -q — 13 passed
  • Full cle suite on head: python -m pytest tests/ -p no:randomly -q — 240 passed, 9 skipped
  • Fixtures exercised: tests/x86_64/relocatable_object.macho (792 bytes) and tests/aarch64/relocatable_object.macho (920 bytes), both from Add Mach-O relocatable object fixtures binaries#193
  • Where it was found: a block-level sweep over a mixed-architecture body of material that is not public, in which CLECompatibilityError: Unsupported Mach-O file type: 1 over 9,018 aarch64 and x86_64 objects was the largest remaining load failure
  • Behaviour deliberately not changed: relocations are still not applied to an MH_OBJECT, so an intra-object call reads as a branch to itself, matching the existing ELF ET_REL and COFF handling

Caveats: these are focused and cle-only runs; the full workspace gate was not rerun against this head. Runs were inside the workspace nix develop shell — under a bare virtualenv tests/test_clemory.py::test_cclemory fails on head and baseline alike with a cffi CompileError for a missing gcc, which is a missing-toolchain artifact rather than anything to do with this change.


What this closes of the refused Mach-O population (measured 2026-08-28)

Private corpus; objects are cited by architecture, container format and sha256 only.

CLECompatibilityError: Unsupported Mach-O file type at cle/backends/macho/macho.py:__init__ accounts for 17,273 objects, and 0% of them is fixed on master: a stratified sample of 1,191 of them (every cell of filetype × container × architecture at min(population, 120), so MH_DSYM, MH_KEXT_BUNDLE, the armv7 objects and all four CaRT units are complete censuses) loads 0/1,191 at cle master d2ecea06 and 0/1,191 at this branch's merge base 46a3733.

The filetype histogram was derived twice and reconciled: once from the recorded refusal message, parsed with an anchored regex and compared as a whole field, and once from the object bytes, reading mach_header.filetype and following the fat header to each slice. 17,269 of 17,273 agree; the four disagreements are the four CaRT objects, whose payload is encrypted so the byte reader sees no Mach-O header at all.

filetype value objects
MH_OBJECT 1 9,018
MH_BUNDLE 8 7,901
MH_KEXT_BUNDLE 11 297
MH_DSYM 10 57

This change covers MH_OBJECT and only MH_OBJECT: 9,005 of the 9,018. In the sample, 473 of 477 load — 120/120 aarch64, 120/120 x86_64, 120/120 x86, 113/114 arm, 0/3 CaRT — and every MH_BUNDLE, MH_KEXT_BUNDLE and MH_DSYM is still refused at the same line. The 13 residual objects are counted over the whole population rather than extrapolated, and are two distinct defects, neither of them this change's subject:

  • 10 objects carry no LC_SYMTAB. A load-command census over all 9,015 byte-readable MH_OBJECTs finds exactly 10 without one, so symtab_nsyms stays None and _parse_symbols raises TypeError: 'NoneType' object cannot be interpreted as an integer. One is the 168-byte armv7 object 6fff4239bb24b21aef551c0637cfc40e713048811c090cfeb8e57ee6d3612ff9, which carries only LC_SEGMENT and LC_VERSION_MIN_IPHONEOS. A symtab_nsyms is None guard is the fix, and it is a pre-existing backend defect that only becomes reachable once MH_OBJECT gets past the gate.
  • 3 objects are CaRT-wrapped and fail at cle/loader.py:_map_object with CLEError: Position-DEPENDENT object None cannot be loaded at 0x0: self.pic = bool(self.flags & MH_flags.MH_PIE) or bool(self.filetype & MachoFiletype.MH_DYLIB) masks a field that holds an ordinal, and 1 & 6 == 0, so a relocatable object is not treated as position-independent and _custom_base_addr is ignored. Adding or self.filetype == MachoFiletype.MH_OBJECT to that expression loads all three, rebased above the container at mapped_base=0x400000 with 5 to 33 sections and 90 to 702 symbols. Objects: aarch64 CaRT 82c13e1c714b7d42516c9079a74b987a766a8c9f28130182c1fe1f886a09edd8 and f05f046fbe2061651f9769cb15512a36ee4db809d4101eeb2d17bc4094a8972b, x86_64 CaRT d9416131b9019681f3ea9dff7d7e735251979b8872d7a76924414abe52232278. This is the same family as Load container-owned Mach-O MH_EXECUTE files #780: right for a thin file, incomplete for one a container owns.

Every fat container in this population is homogeneous, so a per-slice refusal never kills a mixed file here: all 2,339 universal2 objects are 32-bit fat with exactly two slices, 2,293 of them (MH_BUNDLE, MH_BUNDLE) and 46 (MH_DSYM, MH_DSYM). That matters because Universal2.__init__ loads slices in a bare loop with no per-slice guard, so the first refusing slice would abort the whole file — measured blast radius in this population, zero.

Loading is not vacuous downstream: 45 of the loaded objects run through CFGFast(normalize=True, data_references=False, resolve_indirect_jumps=True, force_complete_scan=False), and the 20 MH_OBJECTs among them recover a median of 46.5 functions and 124.5 blocks with no zero-function object. The unapplied-relocation caveat in the description is real and bounded: the section headers of the 9,015 declare 6,881,777 relocation entries in total (median 84 per object, maximum 258,216, 311 objects declaring none), and MachO.relocs is 0 for every MH_OBJECT that loads, exactly as for ELF ET_REL and COFF today.

Merge order, recorded in the same words on #728, #674 and #787. These three Mach-O changes complement rather than replace each other; the intended order is cle 728, then cle 674, then cle 787, and each closes a case the others leave standing. Measured on one stratified sample of 1,191 objects, one variable at a time, each build differing only in which cle worktree the editable install resolves to:

Build MH_OBJECT (477) MH_BUNDLE (360) MH_KEXT_BUNDLE (297) MH_DSYM (57)
cle master d2ecea06 0 0 0 0
#787 2c0d669 473 0 0 0
#728 d34063f 0 357 297 0
#674 1732676 0 332 297 0
#728 and #787 merged by hand 473 357 297 0

The two bundle residuals are each exactly what the other change repairs: #728 leaves 3 fat bundles at ValueError: Address 0x400000 is already backed! in cle/memory.py:add_backer, which #674's single-slice selection in universal2.py fixes, and #674 leaves 28 thin bundles at IndexError: list index out of range in cle/backends/macho/symbol.py:library_name, which #728's LIBRARY_ORDINAL_EXECUTABLE handling fixes. Neither alone reaches 360.

#787 is disjoint from both. It accepts MH_OBJECT, which #728 deliberately keeps refused, so #728 neither subsumes it nor drops it, and their conflict is textual only: git cherry-pick of #787 onto #728 conflicts in cle/backends/macho/macho.py and tests/test_macho.py, and resolving it by hand — #787's MH_OBJECT arm inserted ahead of #728's dSYM arm — measures as the exact union above, 1,127 of 1,191. MH_DSYM stays refused under every build, which is correct: rewriting only the 4-byte filetype field of all 57 to MH_DYLIB in scratch copies and loading them gives 16 loads, 36 × KeyError at cle/memory.py:load reading module init pointers out of memory a dSYM does not back, and 5 × Address 0x400000 is already backed!, so there is nothing there to analyse.

#727 and #780 edit the same twenty lines of that chain, so whichever of these lands first, those two want the same rebase treatment.

Caveat on #674: its base is four months behind #728's, so its residual is not strictly comparable to a run at #728's base; the two failure classes above are nonetheless distinct and each was reproduced against its own head.

Note on the cle baseline named above. d2ecea06 was cle master when these runs happened; master has since moved one commit, to a4fb800, which rewrites the Mach-O magic, function-starts and entry-point error messages and changes nothing on the paths measured here.

@zardus

zardus commented Aug 28, 2026

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Both MH_OBJECT fixtures from angr/binaries#193 loaded before and after this
change. The command is the same on both sides:

import cle
ld = cle.Loader(path, auto_load_libs=False)
m = ld.main_object
print(type(m).__name__, m.arch, hex(m.min_addr), hex(m.max_addr))
print([(s.name, hex(s.vaddr), hex(s.memsize)) for s in m.sections])
print([(s.name, hex(s.rebased_addr)) for s in m.symbols])

Before — the filetype is refused, so nothing is loaded:

angr/cle master
tests/aarch64/relocatable_object.macho:
  CLECompatibilityError: Unsupported Mach-O file type: 1. Please open an issue if you need support for this
tests/x86_64/relocatable_object.macho:
  CLECompatibilityError: Unsupported Mach-O file type: 1. Please open an issue if you need support for this

After — both objects load, with their sections and symbols:

with this change
tests/aarch64/relocatable_object.macho:
  920 bytes
  backend MachO  arch <Arch AARCH64 (LE)>  mapped [0x0:0x1e7]
  section __text   vaddr 0x000000 memsize 0x0120
  section __bss    vaddr 0x000120 memsize 0x00c8
  symbol  ltmp0            0x000000
  symbol  _keccak_init     0x000000
  symbol  _keccak_absorb   0x00002c
  symbol  _keccak_squeeze  0x0000a8
  symbol  _state           0x000120
  symbol  ltmp1            0x000120
tests/x86_64/relocatable_object.macho:
  792 bytes
  backend MachO  arch <Arch AMD64 (LE)>  mapped [0x0:0x1c7]
  section __text   vaddr 0x000000 memsize 0x00f9
  section __bss    vaddr 0x000100 memsize 0x00c8
  symbol  _keccak_init     0x000000
  symbol  _keccak_absorb   0x000030
  symbol  _keccak_squeeze  0x000090
  symbol  _state           0x000100

@zardus
zardus force-pushed the feature/macho-relocatable-object branch 2 times, most recently from ce23051 to 56adbd2 Compare August 29, 2026 19:17
MH_OBJECT was refused as an unsupported file type. The gate it fell through only
chooses a base-address policy; every load command, segment and section path after
it is already shared. A relocatable object is linked against 0 and carries one
unnamed segment holding every section, so it needs the same treatment as a dylib
loaded as the main object.

Relocations are not applied, which matches how ELF ET_REL and COFF objects are
already handled.
@zardus
zardus force-pushed the feature/macho-relocatable-object branch from 56adbd2 to 20ab5b3 Compare August 30, 2026 04:46
@ltfish ltfish self-assigned this Sep 3, 2026
@ltfish
ltfish merged commit 3812052 into master Sep 3, 2026
18 checks passed
@ltfish
ltfish deleted the feature/macho-relocatable-object branch September 3, 2026 17:48
zardus added a commit that referenced this pull request Sep 5, 2026
The mach-o backend chose a base address with a chain of filetype tests whose
else arm refused everything it had no rule for, so MH_BUNDLE, MH_KEXT_BUNDLE
and MH_DSYM were all rejected as unsupported even though nothing after that
block reads the filetype at all. Bundles and kexts want the base address dylibs
already get, because their segment vaddrs are relative to 0 as well, so give the
three of them one branch. MH_DSYM stays refused, but now says it carries debug
information rather than inviting a support request that should not be granted.
MH_OBJECT is not in that set: #787 gave it an arm of its own, because a
relocatable object is never loaded as a dependency and so zeroes its base
address unconditionally.

The refusal named the filetype by number because IntEnum.__str__ has been
int.__str__ since Python 3.11, and a filetype outside the enum escaped as a
bare ValueError raised while formatting that same message. Parse the field into
the enum once, where an unknown value becomes a CLECompatibilityError like
every other malformed header this backend reports.

A bundle binds its undefined symbols against the executable that dlopen-ed it,
which both the symbol table and the chained fixup imports encode as library
ordinal 255. That ordinal does not index imported_libraries, so resolving such
a symbol raised IndexError out of Loader.__init__. Handle it beside the
dynamic-lookup ordinal, which has the same shape.

pic was computed with a bitwise and against MH_DYLIB on a field that holds an
ordinal rather than a bitmask, so it was true for every filetype but
MH_OBJECT, MH_BUNDLE and MH_DYLIB_STUB, including non-PIE executables that the
loader then treated as rebasable. Test membership of the same zero-based
filetypes instead, and let the executable branch stand on the filetype alone,
which is what kept the wrong pic harmless there.

tests/aarch64/IPwnKit.macho.kext has sat in the binaries repository unused
since commit 2705d97 because it could not be loaded; it covers MH_KEXT_BUNDLE
now. The other cases rewrite one header field of an existing fixture in a
temporary copy.
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.

3 participants