diff --git a/Sources/MachOKit/DyldCacheLoaded+static.swift b/Sources/MachOKit/DyldCacheLoaded+static.swift new file mode 100644 index 0000000..d4b75e5 --- /dev/null +++ b/Sources/MachOKit/DyldCacheLoaded+static.swift @@ -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 diff --git a/Sources/MachOKit/MachOImage+static.swift b/Sources/MachOKit/MachOImage+static.swift index eed2f0b..d0fe809 100644 --- a/Sources/MachOKit/MachOImage+static.swift +++ b/Sources/MachOKit/MachOImage+static.swift @@ -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. ///