MacOSX: which dynamic libraries linked by binary?
Solution 1
Try setting these environment variables before running matlab:
export DYLD_PRINT_LIBRARIES=1
export DYLD_PRINT_LIBRARIES_POST_LAUNCH=1
export DYLD_PRINT_RPATHS=1
Run man dyld
for more possibilities.
You can also set the variables for just the matlab command like this:
DYLD_PRINT_LIBRARIES=1 DYLD_PRINT_LIBRARIES_POST_LAUNCH=1 DYLD_PRINT_RPATHS=1 matlab
Solution 2
Rob Mayoff's answer is a great solution when working with executables. If you find you need to check the runtime dependencies of a dylib, the following script my-ldd
may be useful.
#!/usr/bin/env bash
while getopts "r" OPTION; do
case $OPTION in
r) export DYLD_PRINT_RPATHS=1;;
esac
done
shift $((OPTIND-1))
cp `which true` .
DYLD_PRINT_LIBRARIES=1 \
DYLD_PRINT_LIBRARIES_POST_LAUNCH=1 \
DYLD_INSERT_LIBRARIES=$1 \
./true
rm ./true
where a the script may be invoked as
my-ldd ./foo.dylib
or (with rpath attempts echo'd)
my-ldd -r ./foo.dylib
Related videos on Youtube
Cris Luengo
Advancing quantitative image analysis since 1997. I occasionally write interesting stuff over at Cris' Image Analysis Blog. Please check out DIPlib. It’s the very best Image Analysis library you could dream of. DIPimage is a fantastic MATLAB toolbox built on top of DIPlib. DIPlib also has Python bindings (pip install diplib). Enjoy! And do contribute if you can!
Updated on May 22, 2020Comments
-
Cris Luengo almost 4 years
I have not been able to figure out why my binary is not loading. It is a dylib loaded by MATLAB (MEX-file), and links to quite a few dylibs in different locations. MATLAB tells me it cannot load the MEX-file, but I cannot figure out which of its dependencies it cannot find.
Does anybody have any suggestions for how to debug something like this?
On Linux,
ldd
is the perfect tool to debug this problem. People keep saying thatotool -L
is the MacOS equivalent to the Linuxldd
, but this is not true.ldd
actually looks for the libraries, and tells you which ones can be found, and where they were found.otool -L
only tells you what libraries are needed to link against. It does not effort to check to see if they are there. It doesn't even tell you where libraries are searched for when they use@rpath
.otool -l
(lowercase L) gives you a dump of the "load commands", there you can see theLC_RPATH
commands, which establish where@rpath
libraries are searched for. But these have not been able to explain to me which dependency is not found.-
0 _ almost 3 yearsAlso, for inspecting Linux binaries on a macOS,
greadelf
from MacPorts can be used, as well asnm
: unix.stackexchange.com/a/418357/43390, also relevant to: stackoverflow.com/a/23698812/1959808. -
0 _ almost 3 yearsWhen trying
otool -L
for a Linux executable, an error was raised "object is not a Mach-O file type.". -
Cris Luengo almost 3 years@Ioannis: Yes,
otool
is for macOS binaries, not Linux ones. The SO answer you link talks aboutotool -L
, which I already mention in the question is not useful in this particular case.readelf
andnm
are useful to see what symbols are in the binary, and serve a similar purpose tootool
with other flags. None of them attempt to replicate theldd
functionality. -
0 _ almost 3 yearsThank you for noting that none of
readelf
,nm
, andotool
operate similarly to howldd
does. This comment motivated me to read further, and the information I collected does not fit in a comment, so I posted it in an answer to a question that seems closer to what I was searching for: stackoverflow.com/a/67818921/1959808 -
0 _ almost 3 yearsI arrived to this thread while searching for analysis on macOS of ELF executables (compiled on Linux). My understanding is that this use case is related to the question here, which is phrased about
dylib
, so Mach-O analysis on macOS, and what for Mach-O on macOS is the closest thing toldd
for ELF on Linux. My purpose for commenting was informational, to provide pointers for others that might find this thread while searching for ELF analysis on macOS. -
0 _ almost 3 yearsI thought I was using macOS's
otool
, but was actually using theotool
that is part of MacPorts package thecctools
(installed as/opt/local/bin/otool
). Both variants raise an error when given an ELF file, with tiny differences in the error message:/opt/local/bin/otool -L file_name
says "llvm-objdump: error: file_name': object is not a Mach-O file type.", whereas/usr/bin/otool -L
says: "llvm-objdump: 'file_name': Object is not a Mach-O file type.". -
0 _ almost 3 yearsMy comment about
otool
was intended for the ELF case only. Ifotool
supported ELF files, it might have sufficed for what I was trying to confirm (I was looking for some specific versioned symbols). I did not intend to suggest thatotool
is equivalent toldd
. As you have noted, in general,otool
does not perform the kind of analysis thatldd
does. -
0 _ almost 3 yearsAbove, I linked to the SO answer: stackoverflow.com/a/23698812/1959808 with the purpose of linking the two threads.
-
-
Cris Luengo over 6 yearsThis is excellent, thank you so much! Matlab has a boot script that seems to not pass on environment variables to the executable, so I added these variables to the script. I also used
DYLD_PRINT_TO_FILE
. -
Cris Luengo almost 5 yearsNice! What is the reason this needs a local copy of the
true
binary? I tried running/usr/bin/true
in this way and it doesn't work. -
apmccartney almost 5 yearsI encountered the same issue. It's an enhanced security measure put in place by Apple. See this link for more discussion