Skip to content

Commit

Permalink
Merge pull request #162 from p-x9/feature/current-instance
Browse files Browse the repository at this point in the history
Add property to get current MachOImage/DyldCacheLoaded instance
  • Loading branch information
p-x9 authored Jan 9, 2025
2 parents a0f42bc + 013ce9d commit ec0e1a7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sources/MachOKit/DyldCacheLoaded+static.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// DyldCacheLoaded+static.swift
// MachOKit
//
// Created by p-x9 on 2025/01/09
//
//

#if canImport(Darwin)
extension DyldCacheLoaded {
public static var current: DyldCacheLoaded? {
var size = 0
guard let ptr = _dyld_get_shared_cache_range(&size),
let cache = try? DyldCacheLoaded(ptr: ptr) else {
return nil
}
return cache
}
}
#endif
18 changes: 18 additions & 0 deletions Sources/MachOKit/MachOImage+static.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ extension MachOImage {
}
}

extension MachOImage {
/// The `MachOImage` instance of the image for which this function was called
public static func current(
_ dso: UnsafeRawPointer = #dsohandle
) -> MachOImage {
.init(
ptr: dso.assumingMemoryBound(to: mach_header.self)
)
}

/// The `MachOImage` instance representing the current executable image.
public static var currentExecutable: MachOImage {
images.first(where: {
$0.header.fileType == .execute
})!
}
}

extension MachOImage {
/// Obtains the symbol closest to the specified address.
///
Expand Down

0 comments on commit ec0e1a7

Please sign in to comment.