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