Skip to content

Commit

Permalink
Merge pull request #15 from p-x9/feature/symbol-for-address
Browse files Browse the repository at this point in the history
Add function to find the matching symbol to a given address
  • Loading branch information
p-x9 authored Jan 4, 2024
2 parents aa3901f + 9668ec5 commit e14a9e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/MachOKit/MachOImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 12 additions & 0 deletions Sources/MachOKit/Protocol/MachORepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
}

0 comments on commit e14a9e9

Please sign in to comment.