Skip to content

Instantly share code, notes, and snippets.

@landonf
Last active August 22, 2018 23:29
Show Gist options
  • Save landonf/e745c308dbbf19755c55 to your computer and use it in GitHub Desktop.
Save landonf/e745c308dbbf19755c55 to your computer and use it in GitHub Desktop.
Examples: How DYLD_* Environmental Variables Break Mach-O Library Resolution
# DYLD_LIBRARY_PATH library variables break linking of standard system libraries by forcing dyld to ignore
# everything but the library's basename when performing what would otherwise be successful library
# resolution.
mkdir rust-lib
echo 'void my_libjpeg_api (void) { }' | clang -dynamiclib -x objective-c -o rust-lib/libjpeg.dylib -
echo 'extern void my_libjpeg_api(void); int main (int argc, char *argv[]) { my_libjpeg_api(); return 0; }' | clang -x objective-c -o example - -framework ImageIO -Lrust-lib -ljpeg
env DYLD_LIBRARY_PATH=`pwd`/rust-lib ./example
#dyld: Library not loaded: /System/Library/Frameworks/ImageIO.framework/Versions/A/Resources/libJPEG.dylib
# Referenced from: /System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO
# Reason: Incompatible library version: ImageIO requires version 1.0.0 or later, but libjpeg.dylib provides version 0.0.0
# DYLD_FALLBACK_LIBRARY_PATH breaks library path resolution when you have two applications that
# have a library name collision and use DYLD_* varibles to force dyld to find their libraries -- a
# use-case that would work without error when using proper install name/@rpath Mach-O linking.
mkdir rust-lib
mkdir mathematica-lib
echo 'void my_libjpeg_api (void) { }' | clang -dynamiclib -x objective-c -o rust-lib/libjpeg.dylib -install_name libjpeg.dylib -
clang -dynamiclib -x objective-c -o mathematica-lib/libjpeg.dylib -install_name libjpeg.dylib - < /dev/null
echo 'extern void my_libjpeg_api(void); int main (int argc, char *argv[]) { my_libjpeg_api(); return 0; }' | clang -x objective-c -o example - -framework ImageIO -Lrust-lib -ljpeg
env DYLD_FALLBACK_LIBRARY_PATH=`pwd`/mathematica-lib:`pwd`/rust-lib ./example
#dyld: lazy symbol binding failed: Symbol not found: _my_libjpeg_api
# Referenced from: /private/tmp/./example
# Expected in: /tmp/mathematica-lib/libjpeg.dylib
#
#dyld: Symbol not found: _my_libjpeg_api
# Referenced from: /private/tmp/./example
# Expected in: /tmp/mathematica-lib/libjpeg.dylib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment