Shared low-level extensions on top of MachOKit, used by both MachOSwiftSection and MachOObjCSection.
This code started life as the MachOKitExtensions target inside MachOSwiftSection. Because
MachOSwiftSection already depends on MachOObjCSection, MachOObjCSection could not depend back on
it without forming a package-level cycle — so the ObjC side had to go without the address
arithmetic, cache resolution and load-command helpers that the Swift side took for granted.
Extracting the target into its own package breaks that cycle: both packages now depend on
MachOKitExtensions, and neither depends on the other.
| Area | Highlights |
|---|---|
| Address arithmetic | address(forOffset:), addressString(forOffset:), resolveOffset(at:) |
| Pointer tagging | stripPointerTags(of:) and the architecture-specific vmaddrMask |
| Dyld shared cache | cache(for:), cacheAndFileOffset(for:), cacheAndFileOffset(fromStart:), machOFile(by:) |
| Bind / rebase | resolveRebase(fileOffset:), resolveBind(fileOffset:), isBind(fileOffset:), and resolveSelfBind(fileOffset:) for a bind the image satisfies from its own exports (what -interposable links a pointer to an exported symbol as, the frameworks of a simulator runtime among them) |
| Cached views | machOFile.cached memoizes chained fixups behind the same resolveRebase(at:) / resolveOptionalRebase(at:) / resolveBind(at:) API, and the export trie as exportTrie; DyldCacheLoaded.cachedCurrent; FullDyldCache.cachedHost; cache.cached.headerInfo(at:in:) |
| Load commands | Typed accessors for __TEXT, __DATA, __DATA_CONST, __AUTH, __AUTH_CONST, build version |
| Protocols | MachORepresentableWithCache, LocatableLayoutWrapper, MachOTargetIdentifier |
| Reader wrappers | MachOReaderWrapper for a type of your own that holds a reader and forwards every read to it; asMachOImage, asMachOFile and underlyingReader answer for the reader beneath any layer of wrapping |
.package(url: "https://github.com/MxIris-Reverse-Engineering/MachOKitExtensions", from: "1.0.0")import MachOKit
import MachOKitExtensions
let machOFile: MachOFile = // …
// Turn a file offset into the address it will have once loaded.
let address = machOFile.addressString(forOffset: 0x1000)A type of your own that holds a reader and forwards every read to it declares what it wraps, so that whatever is worked out per reader kind — absolute pointers for an image mapped into this process, a file's own offset accounting — is worked out for the reader beneath it:
struct ImageDocument: MachORepresentableWithCache {
let machOImage: MachOImage
// …every requirement forwarded to `machOImage`, `identifier` included…
}
extension ImageDocument: MachOReaderWrapper {
var base: MachOImage { machOImage }
}
let document = ImageDocument(machOImage: machOImage)
document.asMachOImage // the image beneath, not nilmacOS 10.15+, iOS 13+, watchOS 6+, tvOS 13+, visionOS 1+, and Linux.
Linux support matters here because MachOObjCSection supports Linux, and it would have regressed had this package pulled in Apple-only dependencies. The package therefore depends only on MachOKit and AssociatedObject — the latter routes through swift-object-association on non-Objective-C platforms.
MachOKitExtensions is released under the MIT License. See LICENSE.