I have dlopen()
'ed a library, and I want to invert back from the handle it passes to me to the full pathname of shared library. On Linux and friends, I know that I can use dlinfo()
to get the linkmap and iterate through those structures, but I can't seem to find an analogue on OSX. The closest thing I can do is to either:
Use
dyld_image_count()
anddyld_get_image_name()
, iterate over all the currently opened libraries and hope I can guess which one corresponds to my handleSomehow find a symbol that lives inside of the handle I have, and pass that to
dladdr()
.
If I have apriori knowledge as to a symbol name inside of the library I just opened, I can dlsym()
that and then use dladdr()
. That works fine. But in the general case where I have no idea what is inside this shared library, I would need to be able to enumerate symbols to do that, which I don't know how to do either.
So any tips on how to lookup the pathname of a library from its dlopen
handle would be very much appreciated. Thanks!
After about a year of using the solution provided by 0xced, we discovered an alternative method that is simpler and avoids one (rather rare) failure mode; specifically, because 0xced's code snippet iterates through each dylib currently loaded, finds the first exported symbol, attempts to resolve it in the dylib currently being sought, and returns positive if that symbol is found in that particular dylib, you can have false positives if the first exported symbol from an arbitrary library happens to be present inside of the dylib you're currently searching for.
My solution was to use
_dyld_get_image_name(i)
to get the absolute path of each image loaded,dlopen()
that image, and compare the handle (after masking out any mode bits set bydlopen()
due to usage of things likeRTLD_FIRST
) to ensure that this dylib is actually the same file as the handle passed into my function.The complete function can be seen here, as a part of the Julia Language, with the relevant portion copied below:
Note that functions such as
jl_load_dynamic_library()
are wrappers arounddlopen()
that returnlibuv
types, but the spirit of the code remains the same.