HP-UX Reference (11i v1 05/09) - 3 Library Functions A-M (vol 6)
d
dlsym(3C) dlsym(3C)
[RTLD_ERR_SETCANCELSTATE_FAILED]
__thread_setcancelstate
failed on entry to or exit from
dlsym().
APPLICATION USAGE
RTLD_NEXT can be used to navigate an intentionally created hierarchy of multiply defined symbols
created through interposition. For example, if a program wished to create an implementation of
mal-
loc()
that embedded some statistics gathering about memory allocations, such an implementation could
define its own malloc() which would gather the necessary information, and use
dlsym() with
RTLD_NEXT to find the "real"
malloc(), which would perform the actual memory allocation. Of course,
this "real"
malloc()
could be another user-defined interface that added its own value and then used
RTLD_NEXT to find the system malloc().
EXAMPLES
The following example shows how you 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/mylib.sl", RTLD_LAZY);
/* find address of function and data objects */
fptr = (int (*)(int))dlsym(handle, "some_function");
iptr = (int *)dlsym(handle, "int_object");
/* invoke function, passing value of integer as a parameter */
i = (*fptr)(*iptr);
The next example shows how one can use
dlsym() with RTLD_NEXT to add functionality to an existing
interface. Again, error checking has been omitted.
extern void record_malloc(void *, size_t);
void *
malloc(size_t sz)
{
void *ptr;
void *(*real_malloc)(size_t);
real_malloc = (void * (*) (size_t))
dlsym(RTLD_NEXT, "malloc");
ptr = (*real_malloc)(sz);
record_malloc(ptr, sz);
return ptr;
}
SEE ALSO
dlclose(3C), dlerrno(3C), dlerror(3C), dlopen(3C).
Texts an d Tu tor ials
HP-UX Linker and Libraries Online User Guide
(See the +help option)
HP-UX Linker and Libraries User’s Guide
(See manuals(5) for ordering information)
HP-UX 11i Version 1: September 2005 − 2 − Hewlett-Packard Company Section 3−−181