{ |
|
void* handle; | handle = dlopen("./lib1.so", RTLD_GLOBAL RTLD_LAZY); |
if (handle == NULL) { printf("Cannot load library\n");
}
}
The following example illustrates the use of dlopene to load a shared library with an explicit data segment address. For simplicity, error checking has been omitted:
#include <dlfcn.h> #include <sys/mman.h>
int main() {
struct dlfileinfo info; void *handle;
struct dlopen_opts opts; int status;
memset(&info, 0, sizeof(info)); memset(&opts, 0, sizeof(opts));
/* Get file info */
status = dlgetfileinfo("libfoo.so", sizeof(info), &info); opts.flags = RTLD_EXT_DATA_ADDR;
/* allocate memory for the data segment */ opts.data_addr = (char*) mmap(0, info.data_size,
PROT_READPROT_WRITE, MAP_SHAREDMAP_ANONYMOUS,
/* call dlopene */
handle = dlopene("libfoo.so", RTLD_NOWRTLD_GLOBAL, &opts); /* Insert user code to use library */
/* close library */ status = dlclose(handle); /* free memory */ munmap(opts.data_addr, info.data_size);
}
The dlsetlibpath Routine
The dlsetlibpath routine sets the dynamic search path to locate shared libraries.
Synopsis
cc[flag ... ] file ...
#include <dlfcn.h>
int dlsetlibpath(const char *libpath, int flags)
Description
The dlsetlibpath routine is one of a family of routines that give the user direct access to the dynamic linking facilities (using the
The libpath is the dynamic search path. It is a list of one or more path names separated by colons (:).
When searching for a library, the dynamic loader uses search paths in the following default order:
1.The dynamic search path specified in a call to dlsetlibpath.
2.The LD_LIBRARY_PATH environment variable.
3.The SHLIB_PATH environment variable.
The dlopen Shared Library Management Routines 153