Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/MachOKit/DyldCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ extension DyldCache {
}
let suffix = ".symbols"
let path = url.path + suffix
return try .init(url: .init(fileURLWithPath: path))
return try .init(
subcacheUrl: .init(fileURLWithPath: path),
mainCacheHeader: mainCacheHeader
)
}
}

Expand Down
89 changes: 89 additions & 0 deletions Sources/MachOKit/Model/DyldCache/DyldCacheLocalSymbolsInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,92 @@ extension DyldCacheLocalSymbolsInfo {
}
}
}

extension DyldCacheLocalSymbolsInfo {
public func entry64(
for machO: MachOFile,
in cache: DyldCache
) -> DyldCacheLocalSymbolsEntry64? {
guard let offset = machO.textOffset(in: cache) else { return nil }
return entries64(in: cache)?.first(
where: {
$0.dylibOffset == offset
}
)
}

public func entry32(
for machO: MachOFile,
in cache: DyldCache
) -> DyldCacheLocalSymbolsEntry? {
guard let offset = machO.textOffset(in: cache) else { return nil }
return entries32(in: cache)?.first(
where: {
$0.dylibOffset == offset
}
)
}

public func entry(
for machO: MachOFile,
in cache: DyldCache
) -> (any DyldCacheLocalSymbolsEntryProtocol)? {
guard let offset = machO.textOffset(in: cache) else { return nil }
return entries(in: cache).first(
where: {
$0.dylibOffset == offset
}
)
}
}

extension DyldCacheLocalSymbolsInfo {
public func entry64(
for machO: MachOFile,
in cache: FullDyldCache
) -> DyldCacheLocalSymbolsEntry64? {
guard let offset = machO.textOffset(in: cache) else { return nil }
return entries64(in: cache)?.first(
where: {
$0.dylibOffset == offset
}
)
}

public func entry32(
for machO: MachOFile,
in cache: FullDyldCache
) -> DyldCacheLocalSymbolsEntry? {
guard let offset = machO.textOffset(in: cache) else { return nil }
return entries32(in: cache)?.first(
where: {
$0.dylibOffset == offset
}
)
}

public func entry(
for machO: MachOFile,
in cache: FullDyldCache
) -> (any DyldCacheLocalSymbolsEntryProtocol)? {
guard let offset = machO.textOffset(in: cache) else { return nil }
return entries(in: cache).first(
where: {
$0.dylibOffset == offset
}
)
}
}

fileprivate extension MachOFile {
func textOffset(in cache: DyldCache) -> UInt64? {
let loadCommands = loadCommands
let text: (any SegmentCommandProtocol)? = loadCommands.text64 ?? loadCommands.text
guard let text else { return nil }
return numericCast(text.virtualMemoryAddress) - cache.mainCacheHeader.sharedRegionStart
}

func textOffset(in cache: FullDyldCache) -> UInt64? {
textOffset(in: cache.mainCache)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ public protocol DyldCacheLocalSymbolsEntryProtocol {
/// Number of local symbols for this dylib
var nlistCount: Int { get }
}

extension DyldCacheLocalSymbolsEntryProtocol {
public var nlistRange: Range<Int> {
nlistStartIndex ..< nlistStartIndex + nlistCount
}
}
22 changes: 22 additions & 0 deletions Tests/MachOKitTests/DyldCachePrintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,28 @@ final class DyldCachePrintTests: XCTestCase {
}
}

func testLocalSymbolsInSymbolCache() throws {
guard let symbolCache = try cache.symbolCache else {
return
}
guard let info = symbolCache.localSymbolsInfo else {
return
}
let machO = cache.machOFiles().first(
where: {
$0.imagePath.contains("/SwiftUICore")
}
)!
guard let entry = info.entry(for: machO, in: symbolCache) else {
XCTFail("No entry found")
return
}
let symbols = Array(info.symbols(in: symbolCache))[entry.nlistRange]
for symbol in symbols.prefix(100) {
print(symbol.name)
}
}

func testMachOFiles() throws {
let machOs = cache.machOFiles()
for machO in machOs {
Expand Down
22 changes: 22 additions & 0 deletions Tests/MachOKitTests/FullDyldCachePrintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ final class FullDyldCachePrintTests: XCTestCase {
}
}

func testLocalSymbolsInSymbolCache() throws {
guard let symbolCache = try cache.symbolCache else {
return
}
guard let info = symbolCache.localSymbolsInfo else {
return
}
let machO = cache.machOFiles().first(
where: {
$0.imagePath.contains("/SwiftUICore")
}
)!
guard let entry = info.entry(for: machO, in: symbolCache) else {
XCTFail("No entry found")
return
}
let symbols = Array(info.symbols(in: symbolCache))[entry.nlistRange]
for symbol in symbols.prefix(100) {
print(symbol.name)
}
}

func testMachOFiles() throws {
let machOs = cache.machOFiles()
for machO in machOs {
Expand Down