diff --git a/Sources/MachOKit/Model/DyldCache/ObjCOptimization/ObjCHeaderInfoRO.swift b/Sources/MachOKit/Model/DyldCache/ObjCOptimization/ObjCHeaderInfoRO.swift index 4652eeb..adba6c0 100644 --- a/Sources/MachOKit/Model/DyldCache/ObjCOptimization/ObjCHeaderInfoRO.swift +++ b/Sources/MachOKit/Model/DyldCache/ObjCOptimization/ObjCHeaderInfoRO.swift @@ -97,9 +97,10 @@ public struct ObjCHeaderInfoRO64: LayoutWrapper, ObjCHeaderInfoROProtocol { guard let offset = resolvedMachOHeaderOffset(in: cache) else { return nil } + let imagePath = imagePath(in: cache) return try? .init( url: cache.url, - imagePath: "", // FIXME: path + imagePath: imagePath ?? "", headerStartOffsetInCache: numericCast(offset) ) } @@ -168,9 +169,10 @@ public struct ObjCHeaderInfoRO32: LayoutWrapper, ObjCHeaderInfoROProtocol { guard let offset = resolvedMachOHeaderOffset(in: cache) else { return nil } + let imagePath = imagePath(in: cache) return try? .init( url: cache.url, - imagePath: "", // FIXME: path + imagePath: imagePath ?? "", headerStartOffsetInCache: numericCast(offset) ) } @@ -199,4 +201,18 @@ extension ObjCHeaderInfoROProtocol { } return fileOffset } + + internal func imagePath(in cache: DyldCache) -> String? { + let offset = offset + machOHeaderOffset + let address = cache.mainCacheHeader.sharedRegionStart + numericCast(offset) + guard let imageInfos = cache.imageInfos, + let imageInfo = imageInfos.first( + where: { + $0.address == address + } + ) else { + return nil + } + return imageInfo.path(in: cache) + } } diff --git a/Tests/MachOKitTests/DyldCachePrintTests.swift b/Tests/MachOKitTests/DyldCachePrintTests.swift index 323154f..533c289 100644 --- a/Tests/MachOKitTests/DyldCachePrintTests.swift +++ b/Tests/MachOKitTests/DyldCachePrintTests.swift @@ -285,11 +285,7 @@ final class DyldCachePrintTests: XCTestCase { XCTAssertEqual(info.mhdr_offset, _info.mhdr_offset) XCTAssertEqual(info.info_offset, _info.info_offset) - let path = machO.loadCommands - .info(of: LoadCommand.idDylib)? - .dylib(in: machO) - .name ?? "unknonw" - print(" \(path), offset: \(machO.headerStartOffsetInCache)") + print(" \(machO.imagePath), offset: \(machO.headerStartOffsetInCache)") } }