Skip to content

Commit 7785ca2

Browse files
authored
Merge pull request #190 from p-x9/feature/dyld-in-dyld-cache
2 parents b424ba6 + 689e5c8 commit 7785ca2

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

Sources/MachOKit/DyldCache.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ extension DyldCache {
203203

204204
return AnySequence(machOFiles)
205205
}
206+
207+
public var dyld: MachOFile? {
208+
guard let offset = fileOffset(of: mainCacheHeader.dyldInCacheMH) else {
209+
return nil
210+
}
211+
return try? MachOFile(
212+
url: url,
213+
imagePath: "/usr/lib/dyld",
214+
headerStartOffsetInCache: numericCast(offset)
215+
)
216+
}
206217
}
207218

208219
extension DyldCache {

Sources/MachOKit/DyldCacheLoaded.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,15 @@ extension DyldCacheLoaded {
204204

205205
return AnySequence(machOFiles)
206206
}
207+
208+
public var dyld: MachOImage? {
209+
guard let slide,
210+
let ptr = UnsafeRawPointer(bitPattern: Int(header.dyldInCacheMH) + slide) else {
211+
return nil
212+
}
213+
214+
return .init(ptr: ptr.assumingMemoryBound(to: mach_header.self))
215+
}
207216
}
208217

209218
extension DyldCacheLoaded {

Tests/MachOKitTests/DyldCacheLoadedPrintTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ final class DyldCacheLoadedPrintTests: XCTestCase {
156156
}
157157
}
158158

159+
func testDyld() throws {
160+
guard let dyld = cache.dyld else { return }
161+
let sourceVersion = dyld.loadCommands.info(of: LoadCommand.sourceVersion)!
162+
print("Dyld:", dyld.path ?? "unknown", "dyld-\(sourceVersion.version)")
163+
for cmd in dyld.loadCommands {
164+
print(" -", cmd.type)
165+
}
166+
}
167+
159168
func testDylibIndices() {
160169
let cache = cache!
161170
let indices = cache.dylibIndices

Tests/MachOKitTests/DyldCachePrintTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@ final class DyldCachePrintTests: XCTestCase {
163163
}
164164
}
165165

166+
func testDyld() throws {
167+
guard let dyld = cache.dyld else { return }
168+
let sourceVersion = dyld.loadCommands.info(of: LoadCommand.sourceVersion)!
169+
print("Dyld:", dyld.imagePath, "dyld-\(sourceVersion.version)")
170+
for cmd in dyld.loadCommands {
171+
print(" -", cmd.type)
172+
}
173+
}
174+
166175
func testDylibIndices() {
167176
let cache = self.cache1!
168177
let indices = cache.dylibIndices

0 commit comments

Comments
 (0)