I am messing around with Mach-O Headers and dyld and came across this function _dyld_get_image_header
that returns a pointer to the mach header of a dynamic library.
I thought I might be able to access the Mach-O segments of dynamic libraries through this pointer following this article. However, I am not able to access the properties of the Mach-O Headers and am getting errors . In the Swift code below, I attempted to access the ncmds or load command property of the struct and was met with an error:
var currentLibrary = 0
// Gets Pointer to Mach Header of First Dynamic Library, Index 0
let libHeader = unsafeBitCast(_dyld_get_image_header(UInt32(currentLibrary)), to: UnsafePointer<mach_header_64>.self)
print(libHeader.ncmds)
Error:
Value of type 'UnsafePointer<mach_header_64>' has no member 'ncmds'