]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/dlopen.3
encrypt.3, lockf.3, updwtmp.3: srcfix: Note where to get copy of GPL online
[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).
c11b1abf 4.\" and Copyright 2003 Michael Kerrisk (mtk.manpages@gmail.com).
fea681da
MK
5.\"
6.\" This is free documentation; you can redistribute it and/or
7.\" modify it under the terms of the GNU General Public License as
8.\" published by the Free Software Foundation; either version 2 of
9.\" the License, or (at your option) any later version.
10.\"
11.\" The GNU General Public License's references to "object code"
12.\" and "executables" are to be interpreted as the output of any
13.\" document formatting or typesetting system, including
14.\" intermediate and printed output.
15.\"
16.\" This manual is distributed in the hope that it will be useful,
17.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19.\" GNU General Public License for more details.
20.\"
21.\" You should have received a copy of the GNU General Public
22.\" License along with this manual; if not, write to the Free
23.\" Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
24.\" USA.
25.\"
26.\" Modified by David A. Wheeler <dwheeler@dwheeler.com> 2000-11-28.
27.\" Applied patch by Terran Melconian, aeb, 2001-12-14.
28.\" Modified by Hacksaw <hacksaw@hacksaw.org> 2003-03-13.
29.\" Modified by Matt Domsch, 2003-04-09: _init and _fini obsolete
c11b1abf 30.\" Modified by Michael Kerrisk <mtk.manpages@gmail.com> 2003-05-16.
fea681da 31.\" Modified by Walter Harms: dladdr, dlvsym
27a61e86 32.\" Modified by Petr Baudis <pasky@suse.cz>, 2008-12-04: dladdr caveat
fea681da 33.\"
450c8386 34.TH DLOPEN 3 2008-12-06 "Linux" "Linux Programmer's Manual"
fea681da
MK
35.SH NAME
36dladdr, dlclose, dlerror, dlopen, dlsym, dlvsym \- programming interface to
37dynamic linking loader
38.SH SYNOPSIS
39.B #include <dlfcn.h>
40.sp
41.BI "void *dlopen(const char *" filename ", int " flag );
42.sp
0daa9e92 43.B "char *dlerror(void);"
fea681da
MK
44.sp
45.BI "void *dlsym(void *" handle ", const char *" symbol );
46.sp
47.BI "int dlclose(void *" handle );
3f37321b
MK
48.sp
49Link with \fI\-ldl\fP.
fea681da
MK
50.SH DESCRIPTION
51The four functions
d355f1ed
MK
52.BR dlopen (),
53.BR dlsym (),
54.BR dlclose (),
55.BR dlerror ()
fea681da 56implement the interface to the dynamic linking loader.
73d8cece 57.SS dlerror()
fea681da 58The function
d355f1ed 59.BR dlerror ()
fea681da 60returns a human readable string describing the most recent error
c13182ef
MK
61that occurred from
62.BR dlopen (),
63.BR dlsym ()
64or
1e321034 65.BR dlclose ()
fea681da 66since the last call to
1368e847 67.BR dlerror ().
fea681da
MK
68It returns NULL if no errors have occurred since initialization or since
69it was last called.
73d8cece 70.SS dlopen()
fea681da 71The function
d355f1ed 72.BR dlopen ()
fea681da
MK
73loads the dynamic library file named by the null-terminated
74string
75.I filename
76and returns an opaque "handle" for the dynamic library.
77If
78.I filename
79is NULL, then the returned handle is for the main program.
80If
81.I filename
82contains a slash ("/"), then it is interpreted as a (relative
83or absolute) pathname.
84Otherwise, the dynamic linker searches for the library as follows
85(see
86.BR ld.so (8)
87for further details):
86100362 88.IP o 4
fea681da
MK
89(ELF only) If the executable file for the calling program
90contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
91then the directories listed in the DT_RPATH tag are searched.
92.IP o
65f6a3ee 93If, at the time that the program was started, the environment variable
0daa9e92 94.B LD_LIBRARY_PATH
65f6a3ee 95was defined to contain a colon-separated list of directories,
fea681da 96then these are searched.
c13182ef 97(As a security measure this variable is ignored for set-user-ID and
880f5b4b 98set-group-ID programs.)
fea681da
MK
99.IP o
100(ELF only) If the executable file for the calling program
101contains a DT_RUNPATH tag, then the directories listed in that tag
102are searched.
103.IP o
104The cache file
0daa9e92 105.I /etc/ld.so.cache
fea681da
MK
106(maintained by
107.BR ldconfig (8))
108is checked to see whether it contains an entry for
109.IR filename .
110.IP o
111The directories
c13182ef
MK
112.I /lib
113and
114.I /usr/lib
fea681da
MK
115are searched (in that order).
116.PP
117If the library has dependencies on other shared libraries,
118then these are also automatically loaded by the dynamic linker
c13182ef
MK
119using the same rules.
120(This process may occur recursively,
fea681da
MK
121if those libraries in turn have dependencies, and so on.)
122.PP
336e88f0
MK
123One of the following two values must be included in
124.IR flag :
125.TP
fea681da 126.B RTLD_LAZY
c13182ef 127Perform lazy binding.
a3a11667 128Only resolve symbols as the code that references them is executed.
336e88f0
MK
129If the symbol is never referenced, then it is never resolved.
130(Lazy binding is only performed for function references;
131references to variables are always immediately bound when
132the library is loaded.)
133.TP
fea681da 134.B RTLD_NOW
336e88f0 135If this value is specified, or the environment variable
fea681da 136.B LD_BIND_NOW
aa796481 137is set to a nonempty string,
fea681da 138all undefined symbols in the library are resolved before
d355f1ed 139.BR dlopen ()
c13182ef
MK
140returns.
141If this cannot be done, an error is returned.
fea681da 142.PP
dfacdd0a 143Zero or more of the following values may also be ORed in
336e88f0
MK
144.IR flag :
145.TP
fea681da 146.B RTLD_GLOBAL
a3a11667 147The symbols defined by this library will be
fea681da 148made available for symbol resolution of subsequently loaded libraries.
c13182ef 149.TP
336e88f0 150.B RTLD_LOCAL
c13182ef 151This is the converse of
336e88f0
MK
152.BR RTLD_GLOBAL ,
153and the default if neither flag is specified.
154Symbols defined in this library are not made available to resolve
155references in subsequently loaded libraries.
156.TP
c10859eb 157.BR RTLD_NODELETE " (since glibc 2.2)"
336e88f0
MK
158Do not unload the library during
159.BR dlclose ().
d9bfdb9c 160Consequently, the library's static variables are not reinitialized
c13182ef 161if the library is reloaded with
336e88f0
MK
162.BR dlopen ()
163at a later time.
164This flag is not specified in POSIX.1-2001.
165.\" (But it is present on Solaris.)
166.TP
c10859eb 167.BR RTLD_NOLOAD " (since glibc 2.2)"
336e88f0
MK
168Don't load the library.
169This can be used to test if the library is already resident
170.RB ( dlopen ()
171returns NULL if it is not, or the library's handle if it is resident).
c13182ef 172This flag can also be used to promote the flags on a library
336e88f0
MK
173that is already loaded.
174For example, a library that was previously loaded with
175.B RTLD_LOCAL
3b777aff 176can be reopened with
336e88f0
MK
177.BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
178This flag is not specified in POSIX.1-2001.
179.\" (But it is present on Solaris.)
180.\"
bba06189
MK
181.TP
182.BR RTLD_DEEPBIND " (since glibc 2.3.4)"
c13182ef 183.\" Inimitably described by UD in
a3a11667 184.\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
c13182ef
MK
185Place the lookup scope of the symbols in this
186library ahead of the global scope.
187This means that a self-contained library will use
188its own symbols in preference to global symbols with the same name
bba06189
MK
189contained in libraries that have already been loaded.
190This flag is not specified in POSIX.1-2001.
fea681da
MK
191.PP
192If
193.I filename
194is a NULL pointer, then the returned handle is for the main program.
195When given to
d355f1ed 196.BR dlsym (),
fea681da
MK
197this handle causes a search for a symbol in the main program,
198followed by all shared libraries loaded at program startup,
c13182ef 199and then all shared libraries loaded by
d355f1ed 200.BR dlopen ()
fea681da 201with the flag
a5e0a0e4 202.BR RTLD_GLOBAL .
fea681da
MK
203.PP
204External references in the library are resolved using the libraries
205in that library's dependency list and any other libraries previously
c13182ef 206opened with the
fea681da
MK
207.B RTLD_GLOBAL
208flag.
2bc2f479
MK
209If the executable was linked with the flag "\-rdynamic"
210(or, synonymously, "\-\-export\-dynamic"),
fea681da
MK
211then the global symbols in the executable will also be used
212to resolve references in a dynamically loaded library.
213.PP
214If the same library is loaded again with
d355f1ed 215.BR dlopen (),
c13182ef
MK
216the same file handle is returned.
217The dl library maintains reference
fea681da
MK
218counts for library handles, so a dynamic library is not
219deallocated until
d355f1ed 220.BR dlclose ()
fea681da 221has been called on it as many times as
d355f1ed 222.BR dlopen ()
c13182ef
MK
223has succeeded on it.
224The
86100362 225.BR _init ()
c13182ef
MK
226routine, if present, is only called once.
227But a subsequent call with
fea681da
MK
228.B RTLD_NOW
229may force symbol resolution for a library earlier loaded with
230.BR RTLD_LAZY .
231.PP
232If
d355f1ed 233.BR dlopen ()
fea681da 234fails for any reason, it returns NULL.
73d8cece 235.SS dlsym()
fea681da 236The function
d355f1ed 237.BR dlsym ()
c13182ef 238takes a "handle" of a dynamic library returned by
28d88c17
MK
239.BR dlopen ()
240and the
241null-terminated symbol name, returning the address where that symbol is
c13182ef
MK
242loaded into memory.
243If the symbol is not found, in the specified
fea681da 244library or any of the libraries that were automatically loaded by
d355f1ed 245.BR dlopen ()
fea681da 246when that library was loaded,
d355f1ed 247.BR dlsym ()
fea681da
MK
248returns NULL.
249(The search performed by
d355f1ed 250.BR dlsym ()
fea681da
MK
251is breadth first through the dependency tree of these libraries.)
252Since the value of the symbol could actually be NULL (so that a
253NULL return from
d355f1ed 254.BR dlsym ()
fea681da
MK
255need not indicate an error), the correct way to test for an error
256is to call
d355f1ed 257.BR dlerror ()
fea681da 258to clear any old error conditions, then call
d355f1ed 259.BR dlsym (),
fea681da 260and then call
d355f1ed 261.BR dlerror ()
fea681da
MK
262again, saving its return value into a variable, and check whether
263this saved value is not NULL.
264.PP
265There are two special pseudo-handles,
266.B RTLD_DEFAULT
267and
268.BR RTLD_NEXT .
269The former will find the first occurrence of the desired symbol
c13182ef
MK
270using the default library search order.
271The latter
fea681da 272will find the next occurrence of a function in the search order
c13182ef
MK
273after the current library.
274This allows one to provide a wrapper
fea681da 275around a function in another shared library.
73d8cece 276.SS dlclose()
fea681da 277The function
d355f1ed 278.BR dlclose ()
fea681da
MK
279decrements the reference count on the dynamic library handle
280.IR handle .
281If the reference count drops to zero and no other loaded libraries use
282symbols in it, then the dynamic library is unloaded.
283.LP
284The function
d355f1ed 285.BR dlclose ()
c7094399 286returns 0 on success, and nonzero on error.
73d8cece 287.SS The obsolete symbols _init() and _fini()
fea681da
MK
288The linker recognizes special symbols
289.B _init
290and
291.BR _fini .
292If a dynamic library exports a routine named
86100362 293.BR _init (),
fea681da 294then that code is executed after the loading, before
d355f1ed 295.BR dlopen ()
c13182ef
MK
296returns.
297If the dynamic library exports a routine named
86100362 298.BR _fini (),
fea681da 299then that routine is called just before the library is unloaded.
0425de01 300In case you need to avoid linking against the system startup files,
86100362
MK
301this can be done by using the
302.BR gcc (1)
ea5a4ee4 303.I \-nostartfiles
86100362 304command-line option.
fea681da
MK
305.LP
306Using these routines, or the gcc
f242322f 307.B \-nostartfiles
fea681da
MK
308or
309.B \-nostdlib
c13182ef
MK
310options, is not recommended.
311Their use may result in undesired behavior,
fea681da
MK
312since the constructor/destructor routines will not be executed
313(unless special measures are taken).
314.\" void _init(void) __attribute__((constructor));
315.\" void _fini(void) __attribute__((destructor));
316.LP
317Instead, libraries should export routines using the
0daa9e92 318.B __attribute__((constructor))
fea681da 319and
0daa9e92 320.B __attribute__((destructor))
c13182ef
MK
321function attributes.
322See the gcc info pages for information on these.
fea681da 323Constructor routines are executed before
e511ffb6 324.BR dlopen ()
fea681da 325returns, and destructor routines are executed before
e511ffb6 326.BR dlclose ()
fea681da 327returns.
7b1efcab 328.SS Glibc extensions: dladdr() and dlvsym()
fea681da
MK
329Glibc adds two functions not described by POSIX, with prototypes
330.sp
331.nf
b80f966b 332.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
fea681da
MK
333.B #include <dlfcn.h>
334.sp
335.BI "int dladdr(void *" addr ", Dl_info *" info );
336.sp
337.BI "void *dlvsym(void *" handle ", char *" symbol ", char *" version );
338.fi
339.PP
340The function
d355f1ed 341.BR dladdr ()
fea681da 342takes a function pointer and tries to resolve name
c13182ef
MK
343and file where it is located.
344Information is stored in the
86100362
MK
345.I Dl_info
346structure:
fea681da 347.sp
088a639b 348.in +4n
fea681da
MK
349.nf
350typedef struct {
5744c9e1
MK
351 const char *dli_fname; /* Pathname of shared object that
352 contains address */
353 void *dli_fbase; /* Address at which shared object
354 is loaded */
355 const char *dli_sname; /* Name of nearest symbol with address
356 lower than \fIaddr\fP */
357 void *dli_saddr; /* Exact address of symbol named
358 in \fIdli_sname\fP */
fea681da
MK
359} Dl_info;
360.fi
86100362 361.in
5744c9e1
MK
362.PP
363If no symbol matching
364.I addr
365could be found, then
366.I dli_sname
367and
368.I dli_saddr
369are set to NULL.
370.PP
d355f1ed 371.BR dladdr ()
c7094399 372returns 0 on error, and nonzero on success.
fea681da
MK
373.PP
374The function
c343e74c
MK
375.BR dlvsym (),
376provided by glibc since version 2.1,
fea681da 377does the same as
d355f1ed
MK
378.BR dlsym ()
379but takes a version string as an additional argument.
47297adb 380.SH CONFORMING TO
2b2581ee
MK
381POSIX.1-2001 describes
382.BR dlclose (),
383.BR dlerror (),
384.BR dlopen (),
385and
386.BR dlsym ().
387.SH NOTES
097585ed
MK
388The symbols
389.B RTLD_DEFAULT
390and
391.B RTLD_NEXT
392are defined by
2b2581ee 393.I <dlfcn.h>
c3dfd2c8
MK
394only when
395.B _GNU_SOURCE
396was defined before including it.
2b2581ee
MK
397.\" .LP
398.\" The string returned by
399.\" .BR dlerror ()
400.\" should not be modified.
401.\" Some systems give the prototype as
402.\" .sp
403.\" .in +5
404.\" .B "const char *dlerror(void);"
405.\" .in
406
407Since glibc 2.2.3,
408.BR atexit (3)
409can be used to register an exit handler that is automatically
410called when a library is unloaded.
411.SS History
412The dlopen interface standard comes from SunOS.
413That system also has
414.BR dladdr (),
415but not
416.BR dlvsym ().
27a61e86
PB
417.SH BUGS
418Sometimes, the function pointers you pass to
419.BR dladdr ()
420may surprise you.
421On some architectures (notably i386 and x86_64),
422.I dli_fname
423and
424.I dli_fbase
425may end up pointing back at the object from which you called
426.BR dladdr (),
427even if the function used as an argument should come from
428a dynamically linked library.
429.PP
430The problem is that the function pointer will still be resolved
431at compile time, but merely point to the
432.I plt
450c8386 433(Procedure Linkage Table)
27a61e86
PB
434section of the original object (which dispatches the call after
435asking the dynamic linker to resolve the symbol).
450c8386
PB
436To work around this,
437you can try to compile the code to be position-independent:
1d2c48b3 438then, the compiler cannot prepare the pointer
27a61e86
PB
439at compile time anymore and today's
440.BR gcc (1)
450c8386
PB
441will generate code that just loads the final symbol address from the
442.I got
27a61e86
PB
443(Global Offset Table) at run time before passing it to
444.BR dladdr ().
fea681da 445.SH EXAMPLE
9ff08aad 446Load the math library, and print the cosine of 2.0:
fea681da 447.nf
9ff08aad 448
fea681da 449#include <stdio.h>
b28f7a67 450#include <stdlib.h>
fea681da
MK
451#include <dlfcn.h>
452
c13182ef
MK
453int
454main(int argc, char **argv)
cf0a9ace 455{
fea681da
MK
456 void *handle;
457 double (*cosine)(double);
458 char *error;
459
cf0a9ace 460 handle = dlopen("libm.so", RTLD_LAZY);
fea681da 461 if (!handle) {
31a6818e 462 fprintf(stderr, "%s\en", dlerror());
4c44ffe5 463 exit(EXIT_FAILURE);
fea681da
MK
464 }
465
466 dlerror(); /* Clear any existing error */
c73d7281
MK
467
468 /* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
680415af
MK
469 would seem more natural, but the C99 standard leaves
470 casting from "void *" to a function pointer undefined.
29059a65 471 The assignment used below is the POSIX.1\-2003 (Technical
c73d7281
MK
472 Corrigendum 1) workaround; see the Rationale for the
473 POSIX specification of dlsym(). */
474
fea681da 475 *(void **) (&cosine) = dlsym(handle, "cos");
680415af 476.\" But in fact "gcc -O2 -Wall" will complain about the preceding cast.
c73d7281 477
fea681da 478 if ((error = dlerror()) != NULL) {
31a6818e 479 fprintf(stderr, "%s\en", error);
4c44ffe5 480 exit(EXIT_FAILURE);
fea681da
MK
481 }
482
31a6818e 483 printf("%f\en", (*cosine)(2.0));
fea681da 484 dlclose(handle);
5bc8c34c 485 exit(EXIT_SUCCESS);
fea681da 486}
fea681da 487.fi
fea681da
MK
488.PP
489If this program were in a file named "foo.c", you would build the program
490with the following command:
a6e2f128 491.in +4n
fea681da 492.LP
f4398177 493 gcc \-rdynamic \-o foo foo.c \-ldl
a6e2f128 494.in
fea681da 495.PP
86100362
MK
496Libraries exporting
497.BR _init ()
988db661 498and
86100362
MK
499.BR _fini ()
500will want to be compiled as
501follows, using \fIbar.c\fP as the example name:
a6e2f128 502.in +4n
fea681da 503.LP
f4398177 504 gcc \-shared \-nostartfiles \-o bar bar.c
a6e2f128 505.in
47297adb 506.SH SEE ALSO
fea681da
MK
507.BR ld (1),
508.BR ldd (1),
509.BR dl_iterate_phdr (3),
c18ecec9 510.BR rtld-audit (7),
fea681da 511.BR ld.so (8),
173fe7e7
DP
512.BR ldconfig (8)
513
fea681da 514ld.so info pages, gcc info pages, ld info pages