0

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'

1 Answers1

2

It's similar to c, where you would use the -> operator (libHeader->ncmds), requivalent to (*libHeader).ncmds. That is, first your reference the pointer to get to the mach_header_64 value, then you access its ncmds field.

In Swift, it's the exact same idea, with different syntax: libHeaver.pointee.ncmds

Alexander
  • 59,041
  • 12
  • 98
  • 151