DLOPEN(3)

NAME
dlopen, dlsym, dlerror, dlclose, dlcompat - 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. All these functions use the native dyld(3), NSModule(3), and NSObjectFileImage(3) functions to provide a compatibility library so that common unix source code may be easily compiled.

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. 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 contain one of the following values, possibly ORed with additional flags which will be described subsequently:

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().

One of the following flags may be ORed into the mode argument:

RTLD_GLOBAL
    Symbols from this shared object and its directed acyclic graph (DAG) of needed objects will be available for resolving undefined references from all other shared objects. This is the only opion available for objects linked with the -dylib flag to ld(1)

RTLD_LOCAL
    Symbols in this shared object and its DAG of needed objects will be available for resolving undefined references only from other objects in the same DAG. This is the default for objects linked with the -bundle flag to ld(1) but it may be set explicitly with this flag.

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. 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.

If dlsym() is called with the special handle NULL, it is interpreted as a reference to the executable or shared object from which the call is being made. Thus a shared object can reference its own symbols.

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 object's _fini() function is called, if such a function is defined by the object. 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.

NOTES
In previous implementations, it was necessary to prepend an underscore to all external symbols in order to gain symbol compatibility with object code compiled from the C language.

Depening on how the dlcompat package was built, the dlsym() function may, or may not, automatically prepend an underscore to the passed symbol name to compensate for the cc(1) behavior on darwin which automatically prepends an underscore to C symbol names. In this case the library libdl does indeed prepend underscores.

ERRORS
dlopen() and dlsym() return a 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().

AUTHORS
The dlcompat package was written by Jorge Acereda <jacereda@users.sourceforge.net> and Peter O'Gorman <ogorman@users.sourceforge.net>

This man page was borrowed from FreeBSD and modified, it originally came from Sun Microsystems, Inc.

BUGS
If you find any, please contact Peter O'Gorman <ogorman@users.sourceforge.net>

SEE ALSO
ld(1) cc(1) NSModule(3) NSObjectFileImage(3) dyld(3)