]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/dlopen.3
dlopen.3: srcfix
[thirdparty/man-pages.git] / man3 / dlopen.3
CommitLineData
fea681da
MK
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).
9c169754 4.\" and Copyright 2003, 2015 Michael Kerrisk (mtk.manpages@gmail.com).
fea681da 5.\"
1dd72f9c 6.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
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.
11.\"
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.
16.\"
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.
21.\"
22.\" You should have received a copy of the GNU General Public
c715f741
MK
23.\" License along with this manual; if not, see
24.\" <http://www.gnu.org/licenses/>.
6a8d8745 25.\" %%%LICENSE_END
fea681da
MK
26.\"
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
c11b1abf 31.\" Modified by Michael Kerrisk <mtk.manpages@gmail.com> 2003-05-16.
fea681da 32.\" Modified by Walter Harms: dladdr, dlvsym
27a61e86 33.\" Modified by Petr Baudis <pasky@suse.cz>, 2008-12-04: dladdr caveat
fea681da 34.\"
1e64c86b 35.TH DLOPEN 3 2015-03-29 "Linux" "Linux Programmer's Manual"
fea681da 36.SH NAME
374b34d3
MK
37dlclose, dlopen, dlmopen \-
38open and close a shared object
fea681da
MK
39.SH SYNOPSIS
40.B #include <dlfcn.h>
41.sp
cf2789f1 42.BI "void *dlopen(const char *" filename ", int " flags );
fea681da 43.sp
fea681da 44.BI "int dlclose(void *" handle );
3f37321b 45.sp
3f4c09d0
MK
46.B #define _GNU_SOURCE
47.br
48.B #include <dlfcn.h>
49.sp
efdd68a5
MK
50.BI "void *dlmopen (Lmid_t " lmid ", const char *" filename ", int " flags );
51.sp
3f37321b 52Link with \fI\-ldl\fP.
fea681da 53.SH DESCRIPTION
73d8cece 54.SS dlopen()
fea681da 55The function
d355f1ed 56.BR dlopen ()
43151de3
MK
57loads the dynamic shared object (shared library)
58file named by the null-terminated
fea681da
MK
59string
60.I filename
43151de3 61and returns an opaque "handle" for the loaded object.
6ceba646
MK
62This handle is employed with other functions in the dlopen API, such as
63.BR dlsym (3),
64.BR dladdr (3),
65.BR dlinfo (3),
66and
67.BR dlclose ().
68
fea681da
MK
69If
70.I filename
6962fdf9
MK
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.
77.\"
fea681da
MK
78is NULL, then the returned handle is for the main program.
79If
80.I filename
81contains a slash ("/"), then it is interpreted as a (relative
82or absolute) pathname.
43151de3 83Otherwise, the dynamic linker searches for the object as follows
fea681da
MK
84(see
85.BR ld.so (8)
86for further details):
86100362 87.IP o 4
fea681da
MK
88(ELF only) If the executable file for the calling program
89contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
90then the directories listed in the DT_RPATH tag are searched.
91.IP o
65f6a3ee 92If, at the time that the program was started, the environment variable
0daa9e92 93.B LD_LIBRARY_PATH
65f6a3ee 94was defined to contain a colon-separated list of directories,
fea681da 95then these are searched.
8ac45f42 96(As a security measure, this variable is ignored for set-user-ID and
880f5b4b 97set-group-ID programs.)
fea681da
MK
98.IP o
99(ELF only) If the executable file for the calling program
100contains a DT_RUNPATH tag, then the directories listed in that tag
101are searched.
102.IP o
103The cache file
0daa9e92 104.I /etc/ld.so.cache
fea681da
MK
105(maintained by
106.BR ldconfig (8))
107is checked to see whether it contains an entry for
108.IR filename .
109.IP o
110The directories
c13182ef
MK
111.I /lib
112and
113.I /usr/lib
fea681da
MK
114are searched (in that order).
115.PP
43151de3
MK
116If the object specified by
117.I filename
118has dependencies on other shared objects,
fea681da 119then these are also automatically loaded by the dynamic linker
c13182ef
MK
120using the same rules.
121(This process may occur recursively,
43151de3 122if those objects in turn have dependencies, and so on.)
fea681da 123.PP
336e88f0 124One of the following two values must be included in
cf2789f1 125.IR flags :
336e88f0 126.TP
fea681da 127.B RTLD_LAZY
c13182ef 128Perform lazy binding.
a3a11667 129Only resolve symbols as the code that references them is executed.
336e88f0 130If the symbol is never referenced, then it is never resolved.
33a0ccb2 131(Lazy binding is performed only for function references;
336e88f0 132references to variables are always immediately bound when
43151de3 133the shared object is loaded.)
336e88f0 134.TP
fea681da 135.B RTLD_NOW
336e88f0 136If this value is specified, or the environment variable
fea681da 137.B LD_BIND_NOW
aa796481 138is set to a nonempty string,
43151de3 139all undefined symbols in the shared object are resolved before
d355f1ed 140.BR dlopen ()
c13182ef
MK
141returns.
142If this cannot be done, an error is returned.
fea681da 143.PP
dfacdd0a 144Zero or more of the following values may also be ORed in
cf2789f1 145.IR flags :
336e88f0 146.TP
fea681da 147.B RTLD_GLOBAL
43151de3 148The symbols defined by this shared object will be
fe854f15 149made available for symbol resolution of subsequently loaded shared objects.
c13182ef 150.TP
336e88f0 151.B RTLD_LOCAL
c13182ef 152This is the converse of
336e88f0
MK
153.BR RTLD_GLOBAL ,
154and the default if neither flag is specified.
43151de3
MK
155Symbols defined in this shared object are not made available to resolve
156references in subsequently loaded shared objects.
336e88f0 157.TP
c10859eb 158.BR RTLD_NODELETE " (since glibc 2.2)"
43151de3 159Do not unload the shared object during
336e88f0 160.BR dlclose ().
43151de3
MK
161Consequently, the object's static variables are not reinitialized
162if the object is reloaded with
336e88f0
MK
163.BR dlopen ()
164at a later time.
336e88f0 165.TP
c10859eb 166.BR RTLD_NOLOAD " (since glibc 2.2)"
43151de3
MK
167Don't load the shared object.
168This can be used to test if the object is already resident
336e88f0 169.RB ( dlopen ()
43151de3
MK
170returns NULL if it is not, or the object's handle if it is resident).
171This flag can also be used to promote the flags on a shared object
336e88f0 172that is already loaded.
43151de3 173For example, a shared object that was previously loaded with
336e88f0 174.B RTLD_LOCAL
3b777aff 175can be reopened with
336e88f0 176.BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
336e88f0 177.\"
bba06189
MK
178.TP
179.BR RTLD_DEEPBIND " (since glibc 2.3.4)"
c13182ef 180.\" Inimitably described by UD in
a3a11667 181.\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
c13182ef 182Place the lookup scope of the symbols in this
43151de3
MK
183shared object ahead of the global scope.
184This means that a self-contained object will use
c13182ef 185its own symbols in preference to global symbols with the same name
43151de3 186contained in objects that have already been loaded.
fea681da
MK
187.PP
188If
189.I filename
b437fdd9 190is NULL, then the returned handle is for the main program.
fea681da 191When given to
d355f1ed 192.BR dlsym (),
fea681da 193this handle causes a search for a symbol in the main program,
43151de3
MK
194followed by all shared objects loaded at program startup,
195and then all shared objects loaded by
d355f1ed 196.BR dlopen ()
fea681da 197with the flag
a5e0a0e4 198.BR RTLD_GLOBAL .
fea681da 199.PP
43151de3
MK
200External references in the shared object are resolved using the
201shared objects in that object's dependency list and any other
202objects previously opened with the
fea681da
MK
203.B RTLD_GLOBAL
204flag.
2bc2f479
MK
205If the executable was linked with the flag "\-rdynamic"
206(or, synonymously, "\-\-export\-dynamic"),
fea681da 207then the global symbols in the executable will also be used
43151de3 208to resolve references in a dynamically loaded shared object.
fea681da 209.PP
43151de3 210If the same shared object is loaded again with
d355f1ed 211.BR dlopen (),
43151de3 212the same object handle is returned.
e83ed17d 213The dynamic linker maintains reference
43151de3 214counts for object handles, so a dynamically loaded shared object is not
fea681da 215deallocated until
d355f1ed 216.BR dlclose ()
fea681da 217has been called on it as many times as
d355f1ed 218.BR dlopen ()
c13182ef 219has succeeded on it.
c7c01315 220Any initialization returns (see below) are called just once.
b6779d1d
MK
221However, a subsequent
222.BR dlopen ()
223call that loads the same shared object with
fea681da 224.B RTLD_NOW
9c169754 225may force symbol resolution for a shared object earlier loaded with
fea681da
MK
226.BR RTLD_LAZY .
227.PP
228If
d355f1ed 229.BR dlopen ()
fea681da 230fails for any reason, it returns NULL.
9c169754 231.\"
efdd68a5
MK
232.SS dlmopen()
233This function performs the same task as
234.BR dlopen ()\(emthe
235.I filename
236and
237.I flags
65175b1d
MK
238arguments, as well as the return value, are the same,
239except for the differences noted below.
240
241The
242.BR dlmopen ()
243function differs from
244.BR dlopen ()
245primarily in that it accepts an additional argument,
efdd68a5 246.IR lmid ,
9c169754
MK
247that specifies the link-map list (also referred to as a
248.IR namespace )
249in which the shared object should be loaded.
efdd68a5
MK
250(By comparison,
251.BR dlopen ()
9c169754 252adds the dynamically loaded shared object to the same namespace as
efdd68a5
MK
253the shared object from which the
254.BR dlopen ()
255call is made.)
9c169754
MK
256The
257.I Lmid_t
258type is an opaque handle that refers to a namespace.
efdd68a5
MK
259
260The
261.I lmid
9c169754 262argument is either the ID of an existing namespace
efdd68a5
MK
263.\" FIXME: Is using dlinfo() RTLD_DI_LMID the right technique?
264(which can be obtained using the
265.BR dlinfo (3)
266.B RTLD_DI_LMID
267request) or one of the following special values:
268.TP
269.B LM_ID_BASE
9c169754
MK
270Load the shared object in the initial namespace
271(i.e., the application's namespace).
efdd68a5
MK
272.TP
273.B LM_ID_NEWLM
9c169754 274Create a new namespace and load the shared object in that namespace.
982eac0d 275The object must have been correctly linked
9c169754
MK
276to reference all of the other shared objects that it requires,
277since the new namespace is initially empty.
65175b1d 278.PP
65175b1d 279If
438168a2 280.I filename
65175b1d
MK
281is NULL, then the only permitted value for
282.I lmid
283is
284.BR LM_ID_BASE .
73d8cece 285.SS dlclose()
fea681da 286The function
d355f1ed 287.BR dlclose ()
43151de3
MK
288decrements the reference count on the
289dynamically loaded shared object referred to by
fea681da 290.IR handle .
4a1af09b 291If the reference count drops to zero,
43151de3 292then the object is unloaded.
cc2ddf2f
MK
293All shared objects that were automatically loaded when
294.BR dlopen ()
295was invoked on the object referred to by
296.I handle
297are recursively closed in the same manner.
747b2b0a
MK
298
299A successful return from
300.BR dlclose ()
301does not guarantee that the symbols associated with
302.I handle
303are removed from the caller's address space.
304In addition to references resulting from explicit
305.BR dlopen ()
306calls, a shared object may have been implicitly loaded
307(and reference counted) because of dependencies in other shared objects.
308Only when all references have been released can the shared object
309be removed from the address space.
982eac0d
MK
310.SH RETURN VALUE
311On success,
312.BR dlopen ()
313and
314.BR dlmopen ()
315return a non-NULL handle for the loaded library.
316On error
317(file could not be found, was not readable, had the wrong format,
318or caused errors during loading),
319these functions return NULL.
320
321On success,
d355f1ed 322.BR dlclose ()
9ebcbe13 323returns 0; on error, it returns a nonzero value.
982eac0d
MK
324
325Errors from these functions can be diagnosed using
326.BR dlerror (3).
6445992d 327.SH VERSIONS
374b34d3 328.BR dlopen ()
6445992d 329and
374b34d3 330.BR dlclose ()
6445992d 331are present in glibc 2.0 and later.
efdd68a5
MK
332.BR dlmopen ()
333first appeared in glibc 2.3.4.
b23510e5
MK
334.SH ATTRIBUTES
335For an explanation of the terms used in this section, see
336.BR attributes (7).
337.TS
338allbox;
339lbw30 lb lb
340l l l.
341Interface Attribute Value
342T{
343.BR dlopen (),
344.BR dlmopen (),
345.BR dlclose ()
346T} Thread safety MT-Safe
347.TE
47297adb 348.SH CONFORMING TO
2b2581ee 349POSIX.1-2001 describes
374b34d3 350.BR dlclose ()
2b2581ee 351and
896c71d0 352.BR dlopen ().
3f4c09d0 353The
efdd68a5 354.BR dlmopen ()
896c71d0 355function is a GNU extension.
e8a1758c
MK
356
357The
358.BR RTLD_NOLOAD ,
359.BR RTLD_NODELETE ,
360and
361.BR RTLD_DEEP_BIND
362flags are GNU extensions;
363the first two of these flags are also present on Solaris.
2b2581ee 364.SH NOTES
9c169754
MK
365.SS dlmopen() and namespaces
366A link-map list defines an isolated namespace for the
367resolution of symbols by the dynamic linker.
368Within a namespace,
369dependent shared objects are implicitly loaded according to the usual rules,
370and symbol references are likewise resolved according to the usual rules,
371but such resolution is confined to the definitions provided by the
372objects that have been (explicitly and implicitly) loaded into the namespace.
373
374The
375.BR dlmopen ()
376function permits object-load isolation\(emthe ability
377to load a shared object in a new namespace without
378exposing the rest of the application to the symbols
379made available by the new object.
380Note that the use of the
381.B RTLD_LOCAL
382flag is not sufficient for this purpose,
383since it prevents a shared object's symbols from being available to
384.I any
385other shared object.
386In some cases,
387we may want to make the symbols provided by a dynamically
43151de3 388loaded shared object available to (a subset of) other shared objects
9c169754 389without exposing those symbols to the entire application.
982eac0d
MK
390This can be achieved by using a separate namespace and the
391.B RTLD_GLOBAL
392flag.
9c169754
MK
393
394Possible uses of
395.BR dlmopen ()
396are plugins where the author of the plugin-loading framework
397can't trust the plugin authors and does not wish
398any undefined symbols from the plugin framework to be resolved to plugin
399symbols.
400Another use is to load the same object more than once.
401Without the use of
982eac0d 402.BR dlmopen (),
9c169754
MK
403this would require the creation of distinct copies of the shared object file.
404Using
982eac0d 405.BR dlmopen (),
9c169754
MK
406this can be achieved by loading the same shared object file into
407different namespaces.
982eac0d
MK
408
409The glibc implementation supports a maximum of
410.\" DL_NNS
41116 namespaces.
9c169754 412.\"
c7c01315
MK
413.SS Initialization and finalization functions
414Shared objects may export functions using the
415.B __attribute__((constructor))
416and
417.B __attribute__((destructor))
418function attributes.
419Constructor functions are executed before
420.BR dlopen ()
421returns, and destructor functions are executed before
422.BR dlclose ()
423returns.
424A shared object may export multiple constructors and destructors,
425and priorities can be associated with each function
426to determine the order in which they are executed.
427See the
428.BR gcc
429info pages (under "Function attributes")
430.\" info gcc "C Extensions" "Function attributes"
431for further information.
432
433An older method of (partially) achieving the same result is via the use of
434two special symbols recognized by the linker:
e8290357
MK
435.B _init
436and
437.BR _fini .
438If a dynamically loaded shared object exports a routine named
439.BR _init (),
c7c01315 440then that code is executed after loading a shared object, before
e8290357
MK
441.BR dlopen ()
442returns.
443If the shared object exports a routine named
444.BR _fini (),
445then that routine is called just before the object is unloaded.
c7c01315
MK
446In this case, one must avoid linking against the system startup files,
447which contain default versions of these files;
e8290357
MK
448this can be done by using the
449.BR gcc (1)
450.I \-nostartfiles
451command-line option.
452.LP
c7c01315
MK
453Use of
454.B _init
e8290357 455and
c7c01315
MK
456.BR _fini
457is now deprecated in favor of the aforementioned
458constructors and destructors,
459which among other advantages,
460permit multiple initialization and finalization functions to be defined.
461.\"
462.\" Using these routines, or the gcc
463.\" .B \-nostartfiles
464.\" or
465.\" .B \-nostdlib
466.\" options, is not recommended.
467.\" Their use may result in undesired behavior,
468.\" since the constructor/destructor routines will not be executed
469.\" (unless special measures are taken).
470.\" .\" void _init(void) __attribute__((constructor));
471.\" .\" void _fini(void) __attribute__((destructor));
e8290357 472.\"
b198e0ae
MK
473
474Since glibc 2.2.3,
475.BR atexit (3)
476can be used to register an exit handler that is automatically
477called when a shared object is unloaded.
2b2581ee 478.SS History
896c71d0 479These functions are part of the dlopen API, derived from SunOS.
fea681da 480.SH EXAMPLE
9ff08aad 481Load the math library, and print the cosine of 2.0:
fea681da 482.nf
9ff08aad 483
fea681da 484#include <stdio.h>
b28f7a67 485#include <stdlib.h>
fea681da
MK
486#include <dlfcn.h>
487
c13182ef
MK
488int
489main(int argc, char **argv)
cf0a9ace 490{
fea681da
MK
491 void *handle;
492 double (*cosine)(double);
493 char *error;
494
cf0a9ace 495 handle = dlopen("libm.so", RTLD_LAZY);
fea681da 496 if (!handle) {
31a6818e 497 fprintf(stderr, "%s\en", dlerror());
4c44ffe5 498 exit(EXIT_FAILURE);
fea681da
MK
499 }
500
501 dlerror(); /* Clear any existing error */
c73d7281 502
29965345 503 cosine = (double (*)(double)) dlsym(handle, "cos");
c73d7281 504
daf28a2b 505 /* According to the ISO C standard, casting between function
4318a7d0
MK
506 pointers and 'void *', as done above, produces undefined results.
507 POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and
508 proposed the following workaround:
29965345
MK
509
510 *(void **) (&cosine) = dlsym(handle, "cos");
511
4318a7d0
MK
512 This (clumsy) cast conforms with the ISO C standard and will
513 avoid any compiler warnings.
29965345 514
daf28a2b
MK
515 The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a.
516 POSIX.1-2013) improved matters by requiring that conforming
517 implementations support casting 'void *' to a function pointer.
518 Nevertheless, some compilers (e.g., gcc with the '-pedantic'
519 option) may complain about the cast used in this program. */
520.\" http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08
521.\" http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html#tag_16_96_07
5e0622ba 522.\" http://austingroupbugs.net/view.php?id=74
c73d7281 523
aefd6f89 524 error = dlerror();
3487d63b 525 if (error != NULL) {
31a6818e 526 fprintf(stderr, "%s\en", error);
4c44ffe5 527 exit(EXIT_FAILURE);
fea681da
MK
528 }
529
31a6818e 530 printf("%f\en", (*cosine)(2.0));
fea681da 531 dlclose(handle);
5bc8c34c 532 exit(EXIT_SUCCESS);
fea681da 533}
fea681da 534.fi
982eac0d
MK
535.SH BUGS
536As at glibc 2.21, specifying the
537.BR RTLD_GLOBAL
538flag when calling
539.BR dlmopen ()
540.\" dlerror(): "invalid mode"
541generates an error.
542Furthermore, specifying
543.BR RTLD_GLOBAL
544when calling
545.BR dlopen ()
546results in a program crash
547.RB ( SIGSEGV )
548if the call is made from any object loaded in a
549namespace other than the initial namespace.
47297adb 550.SH SEE ALSO
fea681da
MK
551.BR ld (1),
552.BR ldd (1),
5d95b7d8 553.BR pldd (1),
fea681da 554.BR dl_iterate_phdr (3),
6c46d3bc 555.BR dladdr (3),
374b34d3 556.BR dlerror (3),
46db2df1 557.BR dlinfo (3),
896c71d0 558.BR dlsym (3),
c18ecec9 559.BR rtld-audit (7),
fea681da 560.BR ld.so (8),
173fe7e7
DP
561.BR ldconfig (8)
562
7e936c29 563gcc info pages, ld info pages