1 .\" Copyright 1995 Yggdrasil Computing, Incorporated.
2 .\" written by Adam J. Richter (adam@yggdrasil.com),
3 .\" with typesetting help from Daniel Quinlan (quinlan@yggdrasil.com).
4 .\" and Copyright 2003, 2015 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 .\" GNU General Public License for more details.
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, see
24 .\" <http://www.gnu.org/licenses/>.
27 .\" Modified by David A. Wheeler <dwheeler@dwheeler.com> 2000-11-28.
28 .\" Applied patch by Terran Melconian, aeb, 2001-12-14.
29 .\" Modified by Hacksaw <hacksaw@hacksaw.org> 2003-03-13.
30 .\" Modified by Matt Domsch, 2003-04-09: _init and _fini obsolete
31 .\" Modified by Michael Kerrisk <mtk.manpages@gmail.com> 2003-05-16.
32 .\" Modified by Walter Harms: dladdr, dlvsym
33 .\" Modified by Petr Baudis <pasky@suse.cz>, 2008-12-04: dladdr caveat
35 .TH DLOPEN 3 2017-09-15 "Linux" "Linux Programmer's Manual"
37 dlclose, dlopen, dlmopen \-
38 open and close a shared object
42 .BI "void *dlopen(const char *" filename ", int " flags );
44 .BI "int dlclose(void *" handle );
46 .B #define _GNU_SOURCE
50 .BI "void *dlmopen (Lmid_t " lmid ", const char *" filename ", int " flags );
52 Link with \fI\-ldl\fP.
57 loads the dynamic shared object (shared library)
58 file named by the null-terminated
61 and returns an opaque "handle" for the loaded object.
62 This handle is employed with other functions in the dlopen API, such as
71 .\" FIXME On Solaris, when handle is NULL, we seem to get back
72 .\" a handle for (something like) the root of the namespace.
73 .\" The point here is that if we do a dlmopen(LM_ID_NEWLM), then
74 .\" the filename==NULL case returns a different handle than
75 .\" in the initial namespace. But, on glibc, the same handle is
76 .\" returned. This is probably a bug in glibc.
78 is NULL, then the returned handle is for the main program.
81 contains a slash ("/"), then it is interpreted as a (relative
82 or absolute) pathname.
83 Otherwise, the dynamic linker searches for the object as follows
88 (ELF only) If the executable file for the calling program
89 contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
90 then the directories listed in the DT_RPATH tag are searched.
92 If, at the time that the program was started, the environment variable
94 was defined to contain a colon-separated list of directories,
95 then these are searched.
96 (As a security measure, this variable is ignored for set-user-ID and
97 set-group-ID programs.)
99 (ELF only) If the executable file for the calling program
100 contains a DT_RUNPATH tag, then the directories listed in that tag
107 is checked to see whether it contains an entry for
114 are searched (in that order).
116 If the object specified by
118 has dependencies on other shared objects,
119 then these are also automatically loaded by the dynamic linker
120 using the same rules.
121 (This process may occur recursively,
122 if those objects in turn have dependencies, and so on.)
124 One of the following two values must be included in
128 Perform lazy binding.
129 Resolve symbols only as the code that references them is executed.
130 If the symbol is never referenced, then it is never resolved.
131 (Lazy binding is performed only for function references;
132 references to variables are always immediately bound when
133 the shared object is loaded.)
135 .\" commit 12b5b6b7f78ea111e89bbf638294a5413c791072
136 this flag is overridden by the effect of the
138 environment variable.
141 If this value is specified, or the environment variable
143 is set to a nonempty string,
144 all undefined symbols in the shared object are resolved before
147 If this cannot be done, an error is returned.
149 Zero or more of the following values may also be ORed in
153 The symbols defined by this shared object will be
154 made available for symbol resolution of subsequently loaded shared objects.
157 This is the converse of
159 and the default if neither flag is specified.
160 Symbols defined in this shared object are not made available to resolve
161 references in subsequently loaded shared objects.
163 .BR RTLD_NODELETE " (since glibc 2.2)"
164 Do not unload the shared object during
166 Consequently, the object's static variables are not reinitialized
167 if the object is reloaded with
171 .BR RTLD_NOLOAD " (since glibc 2.2)"
172 Don't load the shared object.
173 This can be used to test if the object is already resident
175 returns NULL if it is not, or the object's handle if it is resident).
176 This flag can also be used to promote the flags on a shared object
177 that is already loaded.
178 For example, a shared object that was previously loaded with
181 .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
184 .BR RTLD_DEEPBIND " (since glibc 2.3.4)"
185 .\" Inimitably described by UD in
186 .\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
187 Place the lookup scope of the symbols in this
188 shared object ahead of the global scope.
189 This means that a self-contained object will use
190 its own symbols in preference to global symbols with the same name
191 contained in objects that have already been loaded.
195 is NULL, then the returned handle is for the main program.
198 this handle causes a search for a symbol in the main program,
199 followed by all shared objects loaded at program startup,
200 and then all shared objects loaded by
205 Symbol references in the shared object are resolved using (in order):
206 symbols in the link map of objects loaded for the main program and its
208 symbols in shared objects (and their dependencies)
209 that were previously opened with
214 and definitions in the shared object itself
215 (and any dependencies that were loaded for that object).
217 If the executable was linked with the flag "\-rdynamic"
218 (or, synonymously, "\-\-export\-dynamic"),
219 then global symbols in the executable will also be used
220 to resolve references in a dynamically loaded shared object.
222 If the same shared object is opened again with
224 the same object handle is returned.
225 The dynamic linker maintains reference
226 counts for object handles, so a dynamically loaded shared object is not
229 has been called on it as many times as
232 Constructors (see below) are called only when the library is actually loaded
233 into memory (i.e., when the reference count increases to 1).
234 However, a subsequent
236 call that loads the same shared object with
238 may force symbol resolution for a shared object earlier loaded with
243 fails for any reason, it returns NULL.
246 This function performs the same task as
251 arguments, as well as the return value, are the same,
252 except for the differences noted below.
256 function differs from
258 primarily in that it accepts an additional argument,
260 that specifies the link-map list (also referred to as a
262 in which the shared object should be loaded.
265 adds the dynamically loaded shared object to the same namespace as
266 the shared object from which the
271 type is an opaque handle that refers to a namespace.
275 argument is either the ID of an existing namespace
276 .\" FIXME: Is using dlinfo() RTLD_DI_LMID the right technique?
277 (which can be obtained using the
280 request) or one of the following special values:
283 Load the shared object in the initial namespace
284 (i.e., the application's namespace).
287 Create a new namespace and load the shared object in that namespace.
288 The object must have been correctly linked
289 to reference all of the other shared objects that it requires,
290 since the new namespace is initially empty.
294 is NULL, then the only permitted value for
301 decrements the reference count on the
302 dynamically loaded shared object referred to by
305 If the object's reference count drops to zero
306 and no symbols in this object are required by other objects,
307 then the object is unloaded
308 after first calling any destructors defined for the object.
309 (Symbols in this object might be required in another object
310 because this object was opened with the
312 flag and one of its symbols satisfied a relocation in another object.)
314 All shared objects that were automatically loaded when
316 was invoked on the object referred to by
318 are recursively closed in the same manner.
320 A successful return from
322 does not guarantee that the symbols associated with
324 are removed from the caller's address space.
325 In addition to references resulting from explicit
327 calls, a shared object may have been implicitly loaded
328 (and reference counted) because of dependencies in other shared objects.
329 Only when all references have been released can the shared object
330 be removed from the address space.
336 return a non-NULL handle for the loaded library.
338 (file could not be found, was not readable, had the wrong format,
339 or caused errors during loading),
340 these functions return NULL.
344 returns 0; on error, it returns a nonzero value.
346 Errors from these functions can be diagnosed using
352 are present in glibc 2.0 and later.
354 first appeared in glibc 2.3.4.
356 For an explanation of the terms used in this section, see
362 Interface Attribute Value
367 T} Thread safety MT-Safe
370 POSIX.1-2001 describes
376 function is a GNU extension.
383 flags are GNU extensions;
384 the first two of these flags are also present on Solaris.
386 .SS dlmopen() and namespaces
387 A link-map list defines an isolated namespace for the
388 resolution of symbols by the dynamic linker.
390 dependent shared objects are implicitly loaded according to the usual rules,
391 and symbol references are likewise resolved according to the usual rules,
392 but such resolution is confined to the definitions provided by the
393 objects that have been (explicitly and implicitly) loaded into the namespace.
397 function permits object-load isolation\(emthe ability
398 to load a shared object in a new namespace without
399 exposing the rest of the application to the symbols
400 made available by the new object.
401 Note that the use of the
403 flag is not sufficient for this purpose,
404 since it prevents a shared object's symbols from being available to
408 we may want to make the symbols provided by a dynamically
409 loaded shared object available to (a subset of) other shared objects
410 without exposing those symbols to the entire application.
411 This can be achieved by using a separate namespace and the
417 function also can be used to provide better isolation than the
420 In particular, shared objects loaded with
424 if they are dependencies of another shared object loaded with
428 is insufficient to isolate a loaded shared object except in the (uncommon)
429 case where one has explicit control over all shared object dependencies.
433 are plugins where the author of the plugin-loading framework
434 can't trust the plugin authors and does not wish
435 any undefined symbols from the plugin framework to be resolved to plugin
437 Another use is to load the same object more than once.
440 this would require the creation of distinct copies of the shared object file.
443 this can be achieved by loading the same shared object file into
444 different namespaces.
446 The glibc implementation supports a maximum of
450 .SS Initialization and finalization functions
451 Shared objects may export functions using the
452 .B __attribute__((constructor))
454 .B __attribute__((destructor))
456 Constructor functions are executed before
458 returns, and destructor functions are executed before
461 A shared object may export multiple constructors and destructors,
462 and priorities can be associated with each function
463 to determine the order in which they are executed.
466 info pages (under "Function attributes")
467 .\" info gcc "C Extensions" "Function attributes"
468 for further information.
470 An older method of (partially) achieving the same result is via the use of
471 two special symbols recognized by the linker:
475 If a dynamically loaded shared object exports a routine named
477 then that code is executed after loading a shared object, before
480 If the shared object exports a routine named
482 then that routine is called just before the object is unloaded.
483 In this case, one must avoid linking against the system startup files,
484 which contain default versions of these files;
485 this can be done by using the
494 is now deprecated in favor of the aforementioned
495 constructors and destructors,
496 which among other advantages,
497 permit multiple initialization and finalization functions to be defined.
499 .\" Using these routines, or the gcc
500 .\" .B \-nostartfiles
503 .\" options, is not recommended.
504 .\" Their use may result in undesired behavior,
505 .\" since the constructor/destructor routines will not be executed
506 .\" (unless special measures are taken).
507 .\" .\" void _init(void) __attribute__((constructor));
508 .\" .\" void _fini(void) __attribute__((destructor));
513 can be used to register an exit handler that is automatically
514 called when a shared object is unloaded.
516 These functions are part of the dlopen API, derived from SunOS.
518 As at glibc 2.24, specifying the
522 .\" dlerror(): "invalid mode"
524 Furthermore, specifying
528 results in a program crash
530 if the call is made from any object loaded in a
531 namespace other than the initial namespace.
533 The program below loads the (glibc) math library,
534 looks up the address of the
536 function, and prints the cosine of 2.0.
537 The following is an example of building and running the program:
541 $ \fBcc dlopen_demo.c \-ldl\fP
552 #include <gnu/lib-names.h> /* Defines LIBM_SO (which will be a
553 string such as "libm.so.6") */
558 double (*cosine)(double);
561 handle = dlopen(LIBM_SO, RTLD_LAZY);
563 fprintf(stderr, "%s\en", dlerror());
567 dlerror(); /* Clear any existing error */
569 cosine = (double (*)(double)) dlsym(handle, "cos");
571 /* According to the ISO C standard, casting between function
572 pointers and 'void *', as done above, produces undefined results.
573 POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and
574 proposed the following workaround:
576 *(void **) (&cosine) = dlsym(handle, "cos");
578 This (clumsy) cast conforms with the ISO C standard and will
579 avoid any compiler warnings.
581 The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a.
582 POSIX.1-2013) improved matters by requiring that conforming
583 implementations support casting 'void *' to a function pointer.
584 Nevertheless, some compilers (e.g., gcc with the '-pedantic'
585 option) may complain about the cast used in this program. */
586 .\" http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08
587 .\" http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html#tag_16_96_07
588 .\" http://austingroupbugs.net/view.php?id=74
592 fprintf(stderr, "%s\en", error);
596 printf("%f\en", (*cosine)(2.0));
605 .BR dl_iterate_phdr (3),
614 gcc info pages, ld info pages