dlopen, dlsym, dlclose, dlerror - interface to dynamic library loader
#include <stdio.h> #include <dlfcn.h>
void *dlopen(pathname, mode) char *pathname; int mode;
void *dlsym(handle, name) void *handle; char *name;
void dlclose(handle) void *handle;
The dlopen function provides an interface to the dynamic library loader to allow shared libraries to be loaded and called at run time. The dlopen function attempts to load pathname in the address space of the process, resolving symbols as appropriate. Any libraries that pathname depends upon are also loaded.
If pathname includes a /, dlopen will attempt to open it as specified. Otherwise, dlopen will attempt to locate pathname using shared library search directories in the order specified below (see loader(5) for more details on shared library search directories): The current directory The program's rpath directories LD_LIBRARY_PATH directories Default shared library directories
If mode is RTLD_LAZY, then the run-time loader does symbol resolution only as needed. Typically, this means that the first call to a function in the newly loaded library will cause the resolution of the address of that function to occur. If mode is RTLD_NOW, then the run-time loader must do all symbol binding during the dlopen call. The dlopen function returns a handle that is used by dlsym or dlclose call. If an error occurs, a NULL pointer is returned.
If a NULL pathname is specified, dlopen returns a handle for the main executable, which allows access to dynamic symbols in the running program.
The dlsym function returns the address of the symbol name found in the shared library corresponding to handle. If the symbol is not found, a NULL pointer is returned.
The dlclose function deallocates the address space for the library corresponding to handle. The results are undefined if any user function continues to call a symbol resolved in the address space of a library that has since been deallocated by dlclose.
The dlerror function returns a string describing the last error that occurred from a call to dlopen, dlclose, or dlsym.
The dlopen and dlclose routines might dynamically change the resolution of certain symbols referenced by a program or its shared library dependencies. The dlopen routine might resolve symbols that were previously unresolved, and dlclose might cause resolved symbols to become unresolved or to be reresolved to a different symbol definition.
Use of the dlsym routine is the preferred mechanism for retrieving symbol addresses. This routine reliably returns the current address of a symbol at any point in the program, while the dynamic symbol resolution described previously might not function as expected due to compiler optimizations. For example, the address of a symbol might be saved in a register prior to a dlopen call. The saved address might then be used after the dlopen call, even if the dlopen call changed the resolution of the symbol.
Dynamic symbol resolution functions reliably for programs compiled with the -O0 flag. Also, routines that do not call dlopen or dlclose, either directly or indirectly, can safely depend on dynamic symbol resolution.
The maximum number of shared libraries that can be loaded simultaneously by a single process is approximately 60. This limit can be raised by reconfiguring the kernel's vm-mapentries parameter. This parameter should be set to at least three times the desired maximum number of shared libraries that can be loaded by a process. See the manual System Administration for instructions on reconfiguring the vm-mapentries parameter.
ld(1), loader(5). delim off