diff --git a/Sources/MachOKit/MachOImage.swift b/Sources/MachOKit/MachOImage.swift index b01e049..df1e163 100644 --- a/Sources/MachOKit/MachOImage.swift +++ b/Sources/MachOKit/MachOImage.swift @@ -439,4 +439,12 @@ extension MachOImage { inSection: sectionNumber ) } + + /// Find symbols matching the specified address. + /// - Parameter address: Address to find matching symbol. + /// - Returns: Matched symbol + func symbol(for address: UnsafeRawPointer) -> Symbol? { + let offset = Int(bitPattern: address) - Int(bitPattern: ptr) + return symbol(for: offset) + } } diff --git a/Sources/MachOKit/Protocol/MachORepresentable.swift b/Sources/MachOKit/Protocol/MachORepresentable.swift index 356b6eb..b277eb8 100644 --- a/Sources/MachOKit/Protocol/MachORepresentable.swift +++ b/Sources/MachOKit/Protocol/MachORepresentable.swift @@ -98,6 +98,11 @@ public protocol MachORepresentable { /// - sectionNumber: Section number to be searched. /// - Returns: Closest symbol. func closestSymbol(at offset: Int, inSection sectionNumber: Int) -> Symbol? + + /// Find symbols matching the specified offset. + /// - Parameter offset: Offset from start of mach header. (``SymbolProtocol.offset``) + /// - Returns: Matched symbol + func symbol(for offset: Int) -> Symbol? } extension MachORepresentable { @@ -239,3 +244,10 @@ extension MachORepresentable { return bestSymbol } } + +extension MachORepresentable { + public func symbol(for offset: Int) -> Symbol? { + let best = closestSymbol(at: offset) + return best?.offset == offset ? best : nil + } +}