DLOPEN(3)

NAME
dlopen, dlsym, dlerror, dlclose - programmatic interface to the dynamic linker

SYNOPSIS
#include <dlfcn.h>
void *
dlopen(const char *path, int mode);
void *
dlsym(void *handle, const char *symbol);
const char *
dlerror(void);
int
dlclose(void *handle);

DESCRIPTION
These functions provide a simple programmatic interface to the services of the dynamic linker. Operations are provided to add new shared objects to a program's address space, to obtain the address bindings of symbols defined by such objects, and to remove such objects when their use is no longer required.

dlopen() provides access to the shared object in path, returning a descriptor that can be used for later references to the object in calls to dlsym() and dlclose(). If path was not in the address space prior to the call to dlopen(), it is placed in the address space. When an object is first loaded into the address space in this way, its function _init(), if any, is called by the dynamic linker. (Note that ‘_init’ is the name as expressed in the C language. From assembly language, the name would appear as ‘__init’ instead.) If path has already been placed in the address space in a previous call to dlopen(), it is not added a second time, although a reference count of dlopen() operations on path is maintained. A null pointer supplied for path is interpreted as a reference to the main executable of the process. mode controls the way in which external function references from the loaded object are bound to their referents. It must contains one of the following values:

RTLD_LAZY
    Each external function reference is resolved when the function is first called.

RTLD_NOW
    All external function references are bound immediately by dlopen().

RTLD_LAZY is normally preferred, for reasons of efficiency. However, RTLD_NOW is useful to ensure that any undefined symbols are discovered during the call to dlopen(). If dlopen() fails, it returns a null pointer, and sets an error condition which may be interrogated with dlerror().

dlsym() returns the address binding of the symbol described in the null-terminated character string symbol, as it occurs in the shared object identified by handle. Note that symbol is the assembly language representation of the symbol name. The assembly language representation of a C language symbol contains an extra underscore at the beginning. For example, the symbol ‘foo’ in C would appear as ‘_foo’ in assembly language, and in the symbol argument to dlsym(). The symbols exported by objects added to the address space by dlopen() can be accessed only through calls to dlsym(). Such symbols do not supersede any definition of those symbols already present in the address space when the object is loaded, nor are they available to satisfy normal dynamic linking references. A null pointer supplied as the value of handle is interpreted as a reference to the executable from which the call to dlsym() is being made. Thus a shared object can reference its own symbols. dlsym() returns a null pointer if the symbol cannot be found, and sets an error condition which may be queried with dlerror().

If dlsym() is called with the special handle RTLD_NEXT, then the search for the symbol is limited to the shared objects which were loaded after the one issuing the call to dlsym(). Thus, if the function is called from the main program, all the shared libraries are searched. If it is called from a shared library, all subsequent shared libraries are searched. RTLD_NEXT is useful for implementing wrappers around library functions. For example, a wrapper function getpid() could access the “real” getpid() with dlsym(RTLD_NEXT, "_getpid").

dlerror() returns a null-terminated character string describing the last error that occurred during a call to dlopen(), dlsym(), or dlclose(). If no such error has occurred, dlerror() returns a null pointer. At each call to dlerror(), the error indication is reset. Thus in the case of two calls to dlerror(), where the second call follows the first immediately, the second call will always return a null pointer.

dlclose() deletes a reference to the shared object referenced by handle. If the reference count drops to 0, the object is removed from the address space, and handle is rendered invalid. Just before removing a shared object in this way, the dynamic linker calls the object's _fini() function, if such a function is defined by the object. As with ‘_init’, ‘_fini’ is the C language name of the function. If dlclose() is successful, it returns a value of 0. Otherwise it returns -1, and sets an error condition that can be interrogated with dlerror().

The object-intrinsic functions _init() and _fini() are called with no arguments, and are not expected to return values.

ERRORS
dlopen() and dlsym() return the null pointer in the event of errors. dlclose() returns 0 on success, or -1 if an error occurred. Whenever an error has been detected, a message detailing it can be retrieved via a call to dlerror().

SEE ALSO
ld(1), link(5), rtld(1)