DLSYM(3C) DLSYM(3C)
dlsym - get the address of a symbol in shared object
cc [flag ...] file ... -lc [library ...]
#include <dlfcn.h>
void *dlsym(void *handle, const char *name);
dlsym allows a process to obtain the address of a symbol defined within a
shared object previously opened by dlopen, sgidlopen_version, or
sgidladd. handle is a value returned by a call to dlopen; the
corresponding shared object must not have been closed using dlclose.
name is the symbol's name as a character string. dlsym searchs for the
named symbol in all shared objects loaded automatically as a result of
loading the object referenced by handle [see dlopen(3)].
The name search is done in the following order:
If the handle is NULL, the base object is searched and then the rldlist
of objects is searched, looking only at globally-visible DSOs.
If the handle is not NULL, the named object is searched and then the
library list of the DSO opened on handle is searched (in a pre-order
breadth-first search of all listed DSOs).
The first visible symbol with the requested name (weak or strong) is
returned.
For information on how names are generally resolved (as distinct from
dlsym resolution) and on symbol visibility see the dlopen "NAMESPACE
ISSUES" section.
Note that name resolution for names on the NULL handle is in rld-list
order whereas for other handles the resolution is in library-list order.
The following example shows how one can use dlopen and dlsym to access
either function or data objects. For simplicity, error checking has been
omitted.
void *handle;
int i, *iptr;
int (*fptr)(int);
/* open the needed object */
handle = dlopen("/usr/mydir/libx.so", RTLD_LAZY);
/* find address of function and data objects */
fptr = (int (*)(int))dlsym(handle, "some_function");
Page 1
DLSYM(3C) DLSYM(3C)
iptr = (int *)dlsym(handle, "int_object");
/* invoke function, passing value of integer as a parameter */
i = (*fptr)(*iptr);
dlerror(3), dlopen(3), sgidlopen_version(3), dlclose(3),
sgigetdsoversion(3), sgidladd(3).
The C or C++ compiler may emit a warning similar to "warning(1048): cast
between pointer-to-object and pointer-to-function" when casting the
return type of dlsym to be a function pointer. While a cast from
pointer-to-data to pointer-to-function is not necessarily portable to all
systems (which is what the warning means) the cast and the resulting call
will work fine on any system actually supporting dlsym. One could define
a function
int (*dlsymfunc(void *handle,char *name))();
int (*dlsymfunc(void *handle,char *name))()
{
return (int (*)())dlsym(handle,name);
}
and call it instead of dlsym when getting function addresses: that has
the effect of moving the warning message to a single spot in the program
(the dlsymfunc definition) so the rest of the code does not get this
warning.
If handle does not refer to a valid object opened by dlopen, or if the
named symbol cannot be found within any of the objects associated with
handle, dlsym returns NULL. More detailed diagnostic information is
available through dlerror.
PPPPaaaaggggeeee 2222 [ Back ]
|