Skip to content

Commit

Permalink
Merge pull request #154 from p-x9/feature/image-path-objc-ro-header
Browse files Browse the repository at this point in the history
Fix to get mach-o image name from `objc_header_info_ro_t`
  • Loading branch information
p-x9 authored Dec 13, 2024
2 parents 31c1916 + 91074f0 commit 9443b10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
Expand Down Expand Up @@ -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)
)
}
Expand Down Expand Up @@ -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)
}
}
6 changes: 1 addition & 5 deletions Tests/MachOKitTests/DyldCachePrintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}

Expand Down

0 comments on commit 9443b10

Please sign in to comment.