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