]> git.ipfire.org Git - thirdparty/man-pages.git/blame_incremental - man3/dlopen.3
Clarify how strcmp() should be used as the 'compar'
[thirdparty/man-pages.git] / man3 / dlopen.3
... / ...
CommitLineData
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).
5.\" Additional material copyright 2003, Michael Kerrisk
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
31.\" Modified by Michael Kerrisk <mtk-manpages@gmx.net> 2003-05-16.
32.\" Modified by Walter Harms: dladdr, dlvsym
33.\"
34.TH DLOPEN 3 2003-11-17 "Linux" "Linux Programmer's Manual"
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
43.BI "char *dlerror(void);"
44.sp
45.BI "void *dlsym(void *" handle ", const char *" symbol );
46.sp
47.BI "int dlclose(void *" handle );
48.SH DESCRIPTION
49The four functions
50.BR dlopen (),
51.BR dlsym (),
52.BR dlclose (),
53.BR dlerror ()
54implement the interface to the dynamic linking loader.
55.SS "dlerror"
56The function
57.BR dlerror ()
58returns a human readable string describing the most recent error
59that occurred from
60.BR dlopen (),
61.BR dlsym ()
62or
63.BR dlclose ()
64since the last call to
65.BR dlerror () .
66It returns NULL if no errors have occurred since initialization or since
67it was last called.
68.SS "dlopen"
69The function
70.BR dlopen ()
71loads the dynamic library file named by the null-terminated
72string
73.I filename
74and returns an opaque "handle" for the dynamic library.
75If
76.I filename
77is NULL, then the returned handle is for the main program.
78If
79.I filename
80contains a slash ("/"), then it is interpreted as a (relative
81or absolute) pathname.
82Otherwise, the dynamic linker searches for the library as follows
83(see
84.BR ld.so (8)
85for further details):
86.IP o
87(ELF only) If the executable file for the calling program
88contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
89then the directories listed in the DT_RPATH tag are searched.
90.IP o
91If the environment variable
92.BR LD_LIBRARY_PATH
93is defined to contain a colon-separated list of directories,
94then these are searched.
95(As a security measure this variable is ignored for set-user-ID and
96set-group-ID programs.)
97.IP o
98(ELF only) If the executable file for the calling program
99contains a DT_RUNPATH tag, then the directories listed in that tag
100are searched.
101.IP o
102The cache file
103.IR /etc/ld.so.cache
104(maintained by
105.BR ldconfig (8))
106is checked to see whether it contains an entry for
107.IR filename .
108.IP o
109The directories
110.I /lib
111and
112.I /usr/lib
113are searched (in that order).
114.PP
115If the library has dependencies on other shared libraries,
116then these are also automatically loaded by the dynamic linker
117using the same rules. (This process may occur recursively,
118if those libraries in turn have dependencies, and so on.)
119.PP
120One of the following two values must be included in
121.IR flag :
122.TP
123.B RTLD_LAZY
124Perform lazy binding.
125Only resolve undefined symbols as the code that referenced
126them is executed.
127If the symbol is never referenced, then it is never resolved.
128(Lazy binding is only performed for function references;
129references to variables are always immediately bound when
130the library is loaded.)
131.TP
132.B RTLD_NOW
133If this value is specified, or the environment variable
134.B LD_BIND_NOW
135is set to a non-empty string,
136all undefined symbols in the library are resolved before
137.BR dlopen ()
138returns. If this cannot be done, an error is returned.
139.PP
140Zero of more of the following values may also be ORed in
141.IR flag :
142.TP
143.B RTLD_GLOBAL
144The external symbols defined in the library will be
145made available for symbol resolution of subsequently loaded libraries.
146.TP
147.B RTLD_LOCAL
148This is the converse of
149.BR RTLD_GLOBAL ,
150and the default if neither flag is specified.
151Symbols defined in this library are not made available to resolve
152references in subsequently loaded libraries.
153.TP
154.BR RTLD_NODELETE " (since glibc 2.2)
155Do not unload the library during
156.BR dlclose ().
157Consequently, the library's static variables are not reinitialised
158if the library is reloaded with
159.BR dlopen ()
160at a later time.
161This flag is not specified in POSIX.1-2001.
162.\" (But it is present on Solaris.)
163.TP
164.BR RTLD_NOLOAD " (since glibc 2.2)
165Don't load the library.
166This can be used to test if the library is already resident
167.RB ( dlopen ()
168returns NULL if it is not, or the library's handle if it is resident).
169This flag can also be used to promote the flags on a library
170that is already loaded.
171For example, a library that was previously loaded with
172.B RTLD_LOCAL
173can be re-opened with
174.BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
175This flag is not specified in POSIX.1-2001.
176.\" (But it is present on Solaris.)
177.\"
178.TP
179.BR RTLD_DEEPBIND " (since glibc 2.3.4)"
180Place the lookup scope of the symbols in this
181library ahead of the global scope.
182This means that a self-contained library will use
183its own symbols in preference to global symbols with the same name
184contained in libraries that have already been loaded.
185This flag is not specified in POSIX.1-2001.
186.\" Inimitably described by UD in
187.\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
188.PP
189If
190.I filename
191is a NULL pointer, then the returned handle is for the main program.
192When given to
193.BR dlsym (),
194this handle causes a search for a symbol in the main program,
195followed by all shared libraries loaded at program startup,
196and then all shared libraries loaded by
197.BR dlopen ()
198with the flag
199.BR RTLD_GLOBAL .
200.PP
201External references in the library are resolved using the libraries
202in that library's dependency list and any other libraries previously
203opened with the
204.B RTLD_GLOBAL
205flag.
206If the executable was linked with the flag "\-rdynamic"
207(or, synonymously, "\-\-export\-dynamic"),
208then the global symbols in the executable will also be used
209to resolve references in a dynamically loaded library.
210.PP
211If the same library is loaded again with
212.BR dlopen (),
213the same file handle is returned. The dl library maintains reference
214counts for library handles, so a dynamic library is not
215deallocated until
216.BR dlclose ()
217has been called on it as many times as
218.BR dlopen ()
219has succeeded on it. The
220.B _init
221routine, if present, is only called once. But a subsequent call with
222.B RTLD_NOW
223may force symbol resolution for a library earlier loaded with
224.BR RTLD_LAZY .
225.PP
226If
227.BR dlopen ()
228fails for any reason, it returns NULL.
229.SS "dlsym"
230The function
231.BR dlsym ()
232takes a "handle" of a dynamic library returned by
233.BR dlopen ()
234and the
235null-terminated symbol name, returning the address where that symbol is
236loaded into memory. If the symbol is not found, in the specified
237library or any of the libraries that were automatically loaded by
238.BR dlopen ()
239when that library was loaded,
240.BR dlsym ()
241returns NULL.
242(The search performed by
243.BR dlsym ()
244is breadth first through the dependency tree of these libraries.)
245Since the value of the symbol could actually be NULL (so that a
246NULL return from
247.BR dlsym ()
248need not indicate an error), the correct way to test for an error
249is to call
250.BR dlerror ()
251to clear any old error conditions, then call
252.BR dlsym (),
253and then call
254.BR dlerror ()
255again, saving its return value into a variable, and check whether
256this saved value is not NULL.
257.PP
258There are two special pseudo-handles,
259.B RTLD_DEFAULT
260and
261.BR RTLD_NEXT .
262The former will find the first occurrence of the desired symbol
263using the default library search order. The latter
264will find the next occurrence of a function in the search order
265after the current library. This allows one to provide a wrapper
266around a function in another shared library.
267.SS "dlclose"
268The function
269.BR dlclose ()
270decrements the reference count on the dynamic library handle
271.IR handle .
272If the reference count drops to zero and no other loaded libraries use
273symbols in it, then the dynamic library is unloaded.
274.LP
275The function
276.BR dlclose ()
277returns 0 on success, and non-zero on error.
278.SS "The obsolete symbols _init and _fini"
279The linker recognizes special symbols
280.B _init
281and
282.BR _fini .
283If a dynamic library exports a routine named
284.BR _init ,
285then that code is executed after the loading, before
286.BR dlopen ()
287returns. If the dynamic library exports a routine named
288.BR _fini ,
289then that routine is called just before the library is unloaded.
290In case you need to avoid linking against the system startup files,
291this can be done by giving gcc the "\-nostartfiles" parameter on
292the command line.
293.LP
294Using these routines, or the gcc
295.B \-nostartfiles
296or
297.B \-nostdlib
298options, is not recommended. Their use may result in undesired behavior,
299since the constructor/destructor routines will not be executed
300(unless special measures are taken).
301.\" void _init(void) __attribute__((constructor));
302.\" void _fini(void) __attribute__((destructor));
303.LP
304Instead, libraries should export routines using the
305.BR __attribute__((constructor))
306and
307.BR __attribute__((destructor))
308function attributes. See the gcc info pages for information on these.
309Constructor routines are executed before
310.BR dlopen ()
311returns, and destructor routines are executed before
312.BR dlclose ()
313returns.
314.SH "GNU EXTENSIONS"
315Glibc adds two functions not described by POSIX, with prototypes
316.sp
317.nf
318.B #define _GNU_SOURCE
319.B #include <dlfcn.h>
320.sp
321.BI "int dladdr(void *" addr ", Dl_info *" info );
322.sp
323.BI "void *dlvsym(void *" handle ", char *" symbol ", char *" version );
324.fi
325.PP
326The function
327.BR dladdr ()
328takes a function pointer and tries to resolve name
329and file where it is located. Information is stored in the
330Dl_info structure:
331.sp
332.nf
333typedef struct {
334 const char *dli_fname;/* File name of defining object */
335 void *dli_fbase; /* Load address of that object */
336 const char *dli_sname;/* Name of nearest lower symbol */
337 void *dli_saddr; /* Exact value of nearest symbol */
338} Dl_info;
339.fi
340.sp
341.BR dladdr ()
342returns 0 on error, and non-zero on success.
343.PP
344The function
345.BR dlvsym ()
346does the same as
347.BR dlsym ()
348but takes a version string as an additional argument.
349
350.SH EXAMPLE
351.B Load the math library, and print the cosine of 2.0:
352.RS
353.nf
354.if t .ft CW
355#include <stdio.h>
356#include <dlfcn.h>
357
358int main(int argc, char **argv) {
359 void *handle;
360 double (*cosine)(double);
361 char *error;
362
363 handle = dlopen ("libm.so", RTLD_LAZY);
364 if (!handle) {
365 fprintf (stderr, "%s\en", dlerror());
366 exit(1);
367 }
368
369 dlerror(); /* Clear any existing error */
370.\" This is the (somewhat ugly) SUSv3 TC1 fix for
371.\" the dlsym() casting problem
372 *(void **) (&cosine) = dlsym(handle, "cos");
373 if ((error = dlerror()) != NULL) {
374 fprintf (stderr, "%s\en", error);
375 exit(1);
376 }
377
378 printf ("%f\en", (*cosine)(2.0));
379 dlclose(handle);
380 return 0;
381}
382.if t .ft P
383.fi
384.RE
385.PP
386If this program were in a file named "foo.c", you would build the program
387with the following command:
388.RS
389.LP
390gcc \-rdynamic \-o foo foo.c \-ldl
391.RE
392.PP
393Libraries exporting _init() and _fini() will want to be compiled as
394follows, using bar.c as the example name:
395.RS
396.LP
397gcc \-shared \-nostartfiles \-o bar bar.c
398.RE
399.SH NOTES
400The symbols RTLD_DEFAULT and RTLD_NEXT are defined by
401.I <dlfcn.h>
402only when _GNU_SOURCE was defined before including it.
403.\" .LP
404.\" The string returned by
405.\" .BR dlerror ()
406.\" should not be modified. Some systems give the prototype as
407.\" .sp
408.\" .in +5
409.\" .B "const char *dlerror(void);"
410.\" .in
411
412Since glibc 2.2.3,
413.BR atexit (3)
414can be used to register an exit handler that is automatically
415called when a library is unloaded.
416.SH HISTORY
417The dlopen interface standard comes from SunOS. That system also has
418.BR dladdr (),
419but not
420.BR dlvsym ().
421.SH "CONFORMING TO"
422POSIX 1003.1-2003 describes
423.BR dlclose (),
424.BR dlerror (),
425.BR dlopen (),
426and
427.BR dlsym ().
428.SH "SEE ALSO"
429.BR ld (1),
430.BR ldd (1),
431.BR dl_iterate_phdr (3),
432.BR ld.so (8),
433.BR ldconfig (8),
434ld.so info pages, gcc info pages, ld info pages