title: Given a mach_header how do I find the binary image name?
body: <p>I'm investigating the Mach dynamic linker <code>dyld</code>.  This question applies to all Apple platforms but happy for an answer to be platform specific; I'm using ObjC but also happy to translate Swift if that works for you.  <a href="https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html" rel="nofollow noreferrer">The relevant Apple docs can be found here</a>.    </p>
  
  <p>I'm trying to address a deadlocking issue in a library I maintain and so need to capture information about loaded binary images. (for the uninitiated these are not the JPEG kind of images).</p>
  
  <p>I've registered a callback to watch for binary image addition via <code>_dyld_register_func_for_add_image()</code> and this is being called as images are loaded.  I can see the <code>mach_header</code> structure being passed in as well as the <code>vmaddr_slide</code> value.  </p>
  
  <p>For various deadlock-avoidance reasons I don't want to iterate over images using <code>_dyld_image_count()</code>/<code>_dyld_get_image_header()</code> post-hoc, but instead want to keep track of images as they're loaded/unloaded solely using the <code>add()</code>/<code>remove()</code> callbacks.  Ordinarily I'd be able to call <code>_dyld_get_image_name(index)</code> to retrieve an image name as I iterated over images but, like I say, I can't use the iterative model.</p>
  
  <p>My question is: based on the values passed in to the <code>func_for_add_image()</code> callback (using any additional system calls necessary) how could I determine the image name?</p>