Skip to content

Commit 9506bc5

Browse files
authored
Merge pull request #188 from p-x9/feature/dyld-cache-tpro-mapping
2 parents 7d65710 + 293bd06 commit 9506bc5

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

Sources/MachOKit/DyldCache.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,17 @@ extension DyldCache {
390390
}
391391
return fileHandle.read(offset: offset)
392392
}
393+
394+
public var tproMappings: DataSequence<DyldCacheTproMappingInfo>? {
395+
guard mainCacheHeader.tproMappingsOffset > 0,
396+
mainCacheHeader.hasProperty(\.tproMappingsCount) else {
397+
return nil
398+
}
399+
return fileHandle.readDataSequence(
400+
offset: numericCast(header.tproMappingsOffset),
401+
numberOfElements: numericCast(header.tproMappingsCount)
402+
)
403+
}
393404
}
394405

395406
extension DyldCache {

Sources/MachOKit/DyldCacheLoaded.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,17 @@ extension DyldCacheLoaded {
392392
.advanced(by: numericCast(header.swiftOptsOffset))
393393
.autoBoundPointee()
394394
}
395+
396+
public var tproMappings: MemorySequence<DyldCacheTproMappingInfo>? {
397+
guard mainCacheHeader.tproMappingsOffset > 0,
398+
mainCacheHeader.hasProperty(\.tproMappingsCount) else {
399+
return nil
400+
}
401+
return .init(
402+
basePointer: ptr
403+
.advanced(by: numericCast(header.tproMappingsOffset))
404+
.assumingMemoryBound(to: DyldCacheTproMappingInfo.self),
405+
numberOfElements: numericCast(header.tproMappingsCount)
406+
)
407+
}
395408
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// DyldCacheTproMappingInfo.swift
3+
// MachOKit
4+
//
5+
// Created by p-x9 on 2025/02/27
6+
//
7+
//
8+
9+
import Foundation
10+
11+
public struct DyldCacheTproMappingInfo: LayoutWrapper {
12+
public typealias Layout = dyld_cache_tpro_mapping_info
13+
14+
public var layout: Layout
15+
}

Sources/MachOKit/Protocol/DyldCacheRepresentable.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public protocol DyldCacheRepresentable {
1616
associatedtype SubCaches: RandomAccessCollection<DyldSubCacheEntry>
1717
associatedtype DylibsTrie: TrieTreeProtocol<DylibsTrieNodeContent>
1818
associatedtype ProgramsTrie: TrieTreeProtocol<ProgramsTrieNodeContent>
19+
associatedtype TproMappingInfos: RandomAccessCollection<DyldCacheTproMappingInfo>
1920

2021
/// Byte size of header
2122
var headerSize: Int { get }
@@ -97,6 +98,9 @@ public protocol DyldCacheRepresentable {
9798
/// [dyld implementation](https://github.com/apple-oss-distributions/dyld/blob/65bbeed63cec73f313b1d636e63f243964725a9d/common/DyldSharedCache.cpp#L2088-L2098)
9899
var swiftOptimization: SwiftOptimization? { get }
99100

101+
/// Sequence of tpro mapping infos
102+
var tproMappings: TproMappingInfos? { get }
103+
100104
/// Get the prebuiltLoaderSet indicated by programOffset.
101105
/// - Parameter programOffset: program name and offset pair
102106
/// - Returns: prebuiltLoaderSet

Tests/MachOKitTests/DyldCacheLoadedPrintTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,13 @@ final class DyldCacheLoadedPrintTests: XCTestCase {
318318
print("Metadata Conformance Hash Table Cache Offset:", swiftOptimization.metadataConformanceHashTableCacheOffset)
319319
print("Foreign Type Conformance Hash Table Cache Offset:", swiftOptimization.foreignTypeConformanceHashTableCacheOffset)
320320
}
321+
322+
func testTproMappings() throws {
323+
guard let mappings = cache.tproMappings else { return }
324+
for mapping in mappings {
325+
print("- 0x\(String(mapping.unslidAddress, radix: 16)), \(mapping.size)")
326+
}
327+
}
321328
}
322329

323330
#endif

Tests/MachOKitTests/DyldCachePrintTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ final class DyldCachePrintTests: XCTestCase {
298298
print("Metadata Conformance Hash Table Cache Offset:", swiftOptimization.metadataConformanceHashTableCacheOffset)
299299
print("Foreign Type Conformance Hash Table Cache Offset:", swiftOptimization.foreignTypeConformanceHashTableCacheOffset)
300300
}
301+
302+
func testTproMappings() throws {
303+
guard let mappings = cache.tproMappings else { return }
304+
for mapping in mappings {
305+
print("- 0x\(String(mapping.unslidAddress, radix: 16)), \(mapping.size)")
306+
}
307+
}
301308
}
302309

303310
extension DyldCachePrintTests {

0 commit comments

Comments
 (0)