I know of one specific case where the character value returned by dlsym () can be NULL, which is used when using the GNU indirect functions (IFUNC). However, there are apparently other cases, since the text in the dlsym (3) manual page precedes the invention of IFUNC.
Here is an example of using IFUNC. First, the file that will be used to create the shared library:
$ cat foo.c
Now the main program that will look for the foo character in the shared library:
$ cat main.c #include <dlfcn.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { void *handle; void (*funcp)(void); handle = dlopen("./foo.so", RTLD_LAZY); if (handle == NULL) { fprintf(stderr, "dlopen: %s\n", dlerror()); exit(EXIT_FAILURE); } dlerror(); /* Clear any outstanding error */ funcp = dlsym(handle, "foo"); printf("Results after dlsym(): funcp = %p; dlerror = %s\n", (void *) funcp, dlerror()); exit(EXIT_SUCCESS); }
Now create and run to see the case where dlsym() returns NULL and dlerror() also returns NULL :
$ cc -Wall -fPIC -shared -o libfoo.so foo.c $ cc -Wall -o main main.c libfoo.so -ldl $ LD_LIBRARY_PATH=. ./main foo called Results after dlsym(): funcp = (nil); dlerror = (null)
source share