Skip to content

Commit 9443b10

Browse files
authored
Merge pull request #154 from p-x9/feature/image-path-objc-ro-header
Fix to get mach-o image name from `objc_header_info_ro_t`
2 parents 31c1916 + 91074f0 commit 9443b10

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Sources/MachOKit/Model/DyldCache/ObjCOptimization/ObjCHeaderInfoRO.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,10 @@ public struct ObjCHeaderInfoRO64: LayoutWrapper, ObjCHeaderInfoROProtocol {
9797
guard let offset = resolvedMachOHeaderOffset(in: cache) else {
9898
return nil
9999
}
100+
let imagePath = imagePath(in: cache)
100101
return try? .init(
101102
url: cache.url,
102-
imagePath: "", // FIXME: path
103+
imagePath: imagePath ?? "",
103104
headerStartOffsetInCache: numericCast(offset)
104105
)
105106
}
@@ -168,9 +169,10 @@ public struct ObjCHeaderInfoRO32: LayoutWrapper, ObjCHeaderInfoROProtocol {
168169
guard let offset = resolvedMachOHeaderOffset(in: cache) else {
169170
return nil
170171
}
172+
let imagePath = imagePath(in: cache)
171173
return try? .init(
172174
url: cache.url,
173-
imagePath: "", // FIXME: path
175+
imagePath: imagePath ?? "",
174176
headerStartOffsetInCache: numericCast(offset)
175177
)
176178
}
@@ -199,4 +201,18 @@ extension ObjCHeaderInfoROProtocol {
199201
}
200202
return fileOffset
201203
}
204+
205+
internal func imagePath(in cache: DyldCache) -> String? {
206+
let offset = offset + machOHeaderOffset
207+
let address = cache.mainCacheHeader.sharedRegionStart + numericCast(offset)
208+
guard let imageInfos = cache.imageInfos,
209+
let imageInfo = imageInfos.first(
210+
where: {
211+
$0.address == address
212+
}
213+
) else {
214+
return nil
215+
}
216+
return imageInfo.path(in: cache)
217+
}
202218
}

Tests/MachOKitTests/DyldCachePrintTests.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,7 @@ final class DyldCachePrintTests: XCTestCase {
285285
XCTAssertEqual(info.mhdr_offset, _info.mhdr_offset)
286286
XCTAssertEqual(info.info_offset, _info.info_offset)
287287

288-
let path = machO.loadCommands
289-
.info(of: LoadCommand.idDylib)?
290-
.dylib(in: machO)
291-
.name ?? "unknonw"
292-
print(" \(path), offset: \(machO.headerStartOffsetInCache)")
288+
print(" \(machO.imagePath), offset: \(machO.headerStartOffsetInCache)")
293289
}
294290
}
295291

0 commit comments

Comments
 (0)