dlopen(3) Library Functions Manual dlopen(3) dlclose, dlopen, dlmopen - (libdl -ldl) #include void *dlopen(const char *path, int flags); int dlclose(void *handle); #define _GNU_SOURCE #include void *dlmopen(Lmid_t lmid, const char *path, int flags); dlopen() dlopen() ( ) path "" . dlopen dlsym(3) dladdr(3) dlinfo(3) dlclose(). path NULL . path ("/") ( ). ( ld.so(8) ): o (ELF ) ( dlopen()) DT_RPATH DT_RUNPATH DT_RPATH . o LD_LIBRARY_PATH . ( .) o (ELF ) DT_RUNPATH . o /etc/ld.so.cache ( ldconfig(8)) path. o /lib /usr/lib ( ). path . ( .) flags: RTLD_LAZY . . . ( .) glibc 2.1.1 LD_BIND_NOW. RTLD_NOW LD_BIND_NOW dlopen(). . OR flags: RTLD_GLOBAL . RTLD_LOCAL RTLD_GLOBAL . . RTLD_NODELETE ( glibc 2.2) dlclose(). dlopen() . RTLD_NOLOAD ( glibc 2.2) . ( dlopen() NULL ). . RTLD_LOCAL RTLD_NOLOAD | RTLD_GLOBAL. RTLD_DEEPBIND ( glibc 2.3.4) . . path NULL . dlsym(3) dlopen() RTLD_GLOBAL. (): () dlopen() RTLD_GLOBAL ( ). ld(1) . "-rdynamic" ( "--export-dynamic") ld(1) . dlopen() . dlclose() dlopen() . ( ) ( 1). dlopen() RTLD_NOW RTLD_LAZY. RTLD_LOCAL RTLD_GLOBAL dlopen() . dlopen() NULL. dlmopen() dlopen()-- path flags . dlmopen() dlopen() lmid ( namespace) . ( dlopen() dlopen().) Lmid_t . lmid ( dlinfo(3) RTLD_DI_LMID) : LM_ID_BASE ( ). LM_ID_NEWLM . . (NULL) lmid LM_ID_BASE. dlclose() dlclose() . . ( RTLD_GLOBAL .) dlopen() . dlclose() . dlopen() ( ) . . dlopen() dlmopen() (non-NULL) . ( ) NULL. dlclose() 0 . dlerror(3). attributes(7). +------------+------------------------------------+--------------------+ || | | +------------+------------------------------------+--------------------+ |dlopen(), | | MT-Safe | |dlmopen(), | | | |dlclose() | | | +------------+------------------------------------+--------------------+ dlopen() dlclose() POSIX.1-2008. dlmopen() RTLD_NOLOAD RTLD_NODELETE GNU. RTLD_DEEPBIND . dlopen() dlclose() glibc 2.0. POSIX.1-2001. dlmopen() glibc 2.3.4. dlmopen() . ( ) . dlmopen() -- . RTLD_LOCAL . . RTLD_GLOBAL. dlmopen() RTLD_LOCAL. RTLD_LOCAL RTLD_GLOBAL RTLD_GLOBAL. RTLD_LOCAL () . dlmopen() . . dlmopen() . dlmopen() . glibc 16 . __attribute__((constructor)) __attribute__((destructor)). dlopen() dlclose(). . gcc ( " ") . () : _init _fini. _init() dlopen(). _fini() . gcc(1) -nostartfiles. _init _fini . glibc 2.2.3 atexit(3) . dlopen SunOS. glibc 2.24 RTLD_GLOBAL dlmopen() . RTLD_GLOBAL dlopen() (SIGSEGV) . (glibc) cos(3) 2.0. : $ cc dlopen_demo.c -ldl; $ ./a.out; -0.416147 #include #include #include #include /* Defines LIBM_SO (which will be a string such as "libm.so.6") */ int main(void) { void *handle; typeof(double (double)) *cosine; char *error; handle = dlopen(LIBM_SO, RTLD_LAZY); if (!handle) { fprintf(stderr, "%s\n", dlerror()); exit(EXIT_FAILURE); } dlerror(); /* Clear any existing error */ cosine = (typeof(double (double)) *) dlsym(handle, "cos"); /* According to the ISO C standard, casting between function pointers and 'void *', as done above, produces undefined results. POSIX.1-2001 and POSIX.1-2008 accepted this state of affairs and proposed the following workaround: *(void **) &cosine = dlsym(handle, "cos"); This (clumsy) cast conforms with the ISO C standard and will avoid any compiler warnings. The 2013 Technical Corrigendum 1 to POSIX.1-2008 improved matters by requiring that conforming implementations support casting 'void *' to a function pointer. Nevertheless, some compilers (e.g., gcc with the '-pedantic' option) may complain about the cast used in this program. */ error = dlerror(); if (error != NULL) { fprintf(stderr, "%s\n", error); exit(EXIT_FAILURE); } printf("%f\n", (*cosine)(2.0)); dlclose(handle); exit(EXIT_SUCCESS); } ld(1), ldd(1), pldd(1), dl_iterate_phdr(3), dladdr(3), dlerror(3), dlinfo(3), dlsym(3), rtld-audit(7), ld.so(8), ldconfig(8) gcc ld 3 . . : . 6.18 8 2026 dlopen(3)