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 2021-03-22 "Linux" "Linux Programmer's Manual"
37 dlclose, dlopen, dlmopen \-
38 open and close a shared object
43 .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 );
53 Link with \fI\-ldl\fP.
58 loads the dynamic shared object (shared library)
59 file named by the null-terminated
62 and returns an opaque "handle" for the loaded object.
63 This handle is employed with other functions in the dlopen API, such as
72 .\" FIXME On Solaris, when handle is NULL, we seem to get back
73 .\" a handle for (something like) the root of the namespace.
74 .\" The point here is that if we do a dlmopen(LM_ID_NEWLM), then
75 .\" the filename==NULL case returns a different handle than
76 .\" in the initial namespace. But, on glibc, the same handle is
77 .\" returned. This is probably a bug in glibc.
79 is NULL, then the returned handle is for the main program.
82 contains a slash ("/"), then it is interpreted as a (relative
83 or absolute) pathname.
84 Otherwise, the dynamic linker searches for the object as follows
89 (ELF only) If the calling object
90 (i.e., the shared library or executable from which
93 contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
94 then the directories listed in the DT_RPATH tag are searched.
96 If, at the time that the program was started, the environment variable
98 was defined to contain a colon-separated list of directories,
99 then these are searched.
100 (As a security measure, this variable is ignored for set-user-ID and
101 set-group-ID programs.)
103 (ELF only) If the calling object
104 contains a DT_RUNPATH tag, then the directories listed in that tag
111 is checked to see whether it contains an entry for
118 are searched (in that order).
120 If the object specified by
122 has dependencies on other shared objects,
123 then these are also automatically loaded by the dynamic linker
124 using the same rules.
125 (This process may occur recursively,
126 if those objects in turn have dependencies, and so on.)
128 One of the following two values must be included in
132 Perform lazy binding.
133 Resolve symbols only as the code that references them is executed.
134 If the symbol is never referenced, then it is never resolved.
135 (Lazy binding is performed only for function references;
136 references to variables are always immediately bound when
137 the shared object is loaded.)
139 .\" commit 12b5b6b7f78ea111e89bbf638294a5413c791072
140 this flag is overridden by the effect of the
142 environment variable.
145 If this value is specified, or the environment variable
147 is set to a nonempty string,
148 all undefined symbols in the shared object are resolved before
151 If this cannot be done, an error is returned.
153 Zero or more of the following values may also be ORed in
157 The symbols defined by this shared object will be
158 made available for symbol resolution of subsequently loaded shared objects.
161 This is the converse of
163 and the default if neither flag is specified.
164 Symbols defined in this shared object are not made available to resolve
165 references in subsequently loaded shared objects.
167 .BR RTLD_NODELETE " (since glibc 2.2)"
168 Do not unload the shared object during
170 Consequently, the object's static and global variables are not reinitialized
171 if the object is reloaded with
175 .BR RTLD_NOLOAD " (since glibc 2.2)"
176 Don't load the shared object.
177 This can be used to test if the object is already resident
179 returns NULL if it is not, or the object's handle if it is resident).
180 This flag can also be used to promote the flags on a shared object
181 that is already loaded.
182 For example, a shared object that was previously loaded with
185 .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
188 .BR RTLD_DEEPBIND " (since glibc 2.3.4)"
189 .\" Inimitably described by UD in
190 .\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
191 Place the lookup scope of the symbols in this
192 shared object ahead of the global scope.
193 This means that a self-contained object will use
194 its own symbols in preference to global symbols with the same name
195 contained in objects that have already been loaded.
199 is NULL, then the returned handle is for the main program.
202 this handle causes a search for a symbol in the main program,
203 followed by all shared objects loaded at program startup,
204 and then all shared objects loaded by
209 Symbol references in the shared object are resolved using (in order):
210 symbols in the link map of objects loaded for the main program and its
212 symbols in shared objects (and their dependencies)
213 that were previously opened with
218 and definitions in the shared object itself
219 (and any dependencies that were loaded for that object).
221 Any global symbols in the executable that were placed into
222 its dynamic symbol table by
224 can also be used to resolve references in a dynamically loaded shared object.
225 Symbols may be placed in the dynamic symbol table
226 either because the executable was linked with the flag "\-rdynamic"
227 (or, synonymously, "\-\-export\-dynamic"), which causes all of
228 the executable's global symbols to be placed in the dynamic symbol table,
231 noted a dependency on a symbol in another object during static linking.
233 If the same shared object is opened again with
235 the same object handle is returned.
236 The dynamic linker maintains reference
237 counts for object handles, so a dynamically loaded shared object is not
240 has been called on it as many times as
243 Constructors (see below) are called only when the object is actually loaded
244 into memory (i.e., when the reference count increases to 1).
248 call that loads the same shared object with
250 may force symbol resolution for a shared object earlier loaded with
252 Similarly, an object that was previously opened with
261 fails for any reason, it returns NULL.
264 This function performs the same task as
269 arguments, as well as the return value, are the same,
270 except for the differences noted below.
274 function differs from
276 primarily in that it accepts an additional argument,
278 that specifies the link-map list (also referred to as a
280 in which the shared object should be loaded.
283 adds the dynamically loaded shared object to the same namespace as
284 the shared object from which the
289 type is an opaque handle that refers to a namespace.
293 argument is either the ID of an existing namespace
294 .\" FIXME: Is using dlinfo() RTLD_DI_LMID the right technique?
295 (which can be obtained using the
298 request) or one of the following special values:
301 Load the shared object in the initial namespace
302 (i.e., the application's namespace).
305 Create a new namespace and load the shared object in that namespace.
306 The object must have been correctly linked
307 to reference all of the other shared objects that it requires,
308 since the new namespace is initially empty.
312 is NULL, then the only permitted value for
319 decrements the reference count on the
320 dynamically loaded shared object referred to by
323 If the object's reference count drops to zero
324 and no symbols in this object are required by other objects,
325 then the object is unloaded
326 after first calling any destructors defined for the object.
327 (Symbols in this object might be required in another object
328 because this object was opened with the
330 flag and one of its symbols satisfied a relocation in another object.)
332 All shared objects that were automatically loaded when
334 was invoked on the object referred to by
336 are recursively closed in the same manner.
338 A successful return from
340 does not guarantee that the symbols associated with
342 are removed from the caller's address space.
343 In addition to references resulting from explicit
345 calls, a shared object may have been implicitly loaded
346 (and reference counted) because of dependencies in other shared objects.
347 Only when all references have been released can the shared object
348 be removed from the address space.
354 return a non-NULL handle for the loaded object.
356 (file could not be found, was not readable, had the wrong format,
357 or caused errors during loading),
358 these functions return NULL.
362 returns 0; on error, it returns a nonzero value.
364 Errors from these functions can be diagnosed using
370 are present in glibc 2.0 and later.
372 first appeared in glibc 2.3.4.
374 For an explanation of the terms used in this section, see
382 Interface Attribute Value
387 T} Thread safety MT-Safe
393 POSIX.1-2001 describes
399 function is a GNU extension.
406 flags are GNU extensions;
407 the first two of these flags are also present on Solaris.
409 .SS dlmopen() and namespaces
410 A link-map list defines an isolated namespace for the
411 resolution of symbols by the dynamic linker.
413 dependent shared objects are implicitly loaded according to the usual rules,
414 and symbol references are likewise resolved according to the usual rules,
415 but such resolution is confined to the definitions provided by the
416 objects that have been (explicitly and implicitly) loaded into the namespace.
420 function permits object-load isolation\(emthe ability
421 to load a shared object in a new namespace without
422 exposing the rest of the application to the symbols
423 made available by the new object.
424 Note that the use of the
426 flag is not sufficient for this purpose,
427 since it prevents a shared object's symbols from being available to
431 we may want to make the symbols provided by a dynamically
432 loaded shared object available to (a subset of) other shared objects
433 without exposing those symbols to the entire application.
434 This can be achieved by using a separate namespace and the
440 function also can be used to provide better isolation than the
443 In particular, shared objects loaded with
447 if they are dependencies of another shared object loaded with
451 is insufficient to isolate a loaded shared object except in the (uncommon)
452 case where one has explicit control over all shared object dependencies.
456 are plugins where the author of the plugin-loading framework
457 can't trust the plugin authors and does not wish
458 any undefined symbols from the plugin framework to be resolved to plugin
460 Another use is to load the same object more than once.
463 this would require the creation of distinct copies of the shared object file.
466 this can be achieved by loading the same shared object file into
467 different namespaces.
469 The glibc implementation supports a maximum of
473 .SS Initialization and finalization functions
474 Shared objects may export functions using the
475 .B __attribute__((constructor))
477 .B __attribute__((destructor))
479 Constructor functions are executed before
481 returns, and destructor functions are executed before
484 A shared object may export multiple constructors and destructors,
485 and priorities can be associated with each function
486 to determine the order in which they are executed.
489 info pages (under "Function attributes")
490 .\" info gcc "C Extensions" "Function attributes"
491 for further information.
493 An older method of (partially) achieving the same result is via the use of
494 two special symbols recognized by the linker:
498 If a dynamically loaded shared object exports a routine named
500 then that code is executed after loading a shared object, before
503 If the shared object exports a routine named
505 then that routine is called just before the object is unloaded.
506 In this case, one must avoid linking against the system startup files,
507 which contain default versions of these files;
508 this can be done by using the
517 is now deprecated in favor of the aforementioned
518 constructors and destructors,
519 which among other advantages,
520 permit multiple initialization and finalization functions to be defined.
522 .\" Using these routines, or the gcc
523 .\" .B \-nostartfiles
526 .\" options, is not recommended.
527 .\" Their use may result in undesired behavior,
528 .\" since the constructor/destructor routines will not be executed
529 .\" (unless special measures are taken).
530 .\" .\" void _init(void) __attribute__((constructor));
531 .\" .\" void _fini(void) __attribute__((destructor));
536 can be used to register an exit handler that is automatically
537 called when a shared object is unloaded.
539 These functions are part of the dlopen API, derived from SunOS.
541 As at glibc 2.24, specifying the
545 .\" dlerror(): "invalid mode"
547 Furthermore, specifying
551 results in a program crash
553 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=18684
554 if the call is made from any object loaded in a
555 namespace other than the initial namespace.
557 The program below loads the (glibc) math library,
558 looks up the address of the
560 function, and prints the cosine of 2.0.
561 The following is an example of building and running the program:
565 $ \fBcc dlopen_demo.c \-ldl\fP
576 #include <gnu/lib\-names.h> /* Defines LIBM_SO (which will be a
577 string such as "libm.so.6") */
582 double (*cosine)(double);
585 handle = dlopen(LIBM_SO, RTLD_LAZY);
587 fprintf(stderr, "%s\en", dlerror());
591 dlerror(); /* Clear any existing error */
593 cosine = (double (*)(double)) dlsym(handle, "cos");
595 /* According to the ISO C standard, casting between function
596 pointers and \(aqvoid *\(aq, as done above, produces undefined results.
597 POSIX.1\-2001 and POSIX.1\-2008 accepted this state of affairs and
598 proposed the following workaround:
600 *(void **) (&cosine) = dlsym(handle, "cos");
602 This (clumsy) cast conforms with the ISO C standard and will
603 avoid any compiler warnings.
605 The 2013 Technical Corrigendum 1 to POSIX.1\-2008 improved matters
606 by requiring that conforming implementations support casting
607 \(aqvoid *\(aq to a function pointer. Nevertheless, some compilers
608 (e.g., gcc with the \(aq\-pedantic\(aq option) may complain about the
609 cast used in this program. */
610 .\" http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08
611 .\" http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html#tag_16_96_07
612 .\" http://austingroupbugs.net/view.php?id=74
616 fprintf(stderr, "%s\en", error);
620 printf("%f\en", (*cosine)(2.0));
629 .BR dl_iterate_phdr (3),
638 gcc info pages, ld info pages