How to use dlopen() to check for library libssl.dylib existence?

According to macOS Big Sur 11.0.1 Release Notes :


New in macOS Big Sur 11.0.1, the system ships with a built-in dynamic linker cache of all system-provided libraries. As part of this change, copies of dynamic libraries are no longer present on the filesystem. Code that attempts to check for dynamic library presence by looking for a file at a path or enumerating a directory will fail. Instead, check for library presence by attempting to dlopen() the path, which will correctly check for the library in the cache.

I am on M1 macOS Big Sur 11.1, and there is no file at /usr/lib/libssl.dylib :

Code Block
$ ls /usr/lib/libssl.dylib
ls: /usr/lib/libssl.dylib: No such file or directory

So I assume libssl.dylib is in the linker cache, however this simple C program aborts:

Code Block
#include <dlfcn.h>
#include <stdio.h>
int main() {
printf("Calling dlopen()..\n");
void* handle = dlopen("/usr/lib/libssl.dylib", RTLD_NOW );
if (handle == NULL) {
fprintf(stderr, "Could not open libssl.dylib: %s\n", dlerror());
return 1;
}
if (dlclose(handle) != 0) {
fprintf(stderr, "Could not close libssl.dylib: %s\n", dlerror());
return 1;
}
printf("Success!\n");
return 0;
}

compiled with

Code Block
cc -o test_load load.c -ldl

crashes (aborts):

Code Block
$ ./test_load
Calling dlopen()..
WARNING: /Users/hakonhaegland/test/test_load is loading libcrypto in an unsafe way
[1] 9364 abort ./test_load

  • How can I use dlopen() to check for existence of libssl.dylib?

  • What does the error message loading libcrypto in an unsafe way mean?

How to use dlopen() to check for library libssl.dylib existence?
 
 
Q