]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/dlopen.3
pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2...
[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 2019-08-02 "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 .PP
42 .BI "void *dlopen(const char *" filename ", int " flags );
43 .PP
44 .BI "int dlclose(void *" handle );
45 .PP
46 .B #define _GNU_SOURCE
47 .br
48 .B #include <dlfcn.h>
49 .PP
50 .BI "void *dlmopen (Lmid_t " lmid ", const char *" filename ", int " flags );
51 .PP
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 .PP
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 Resolve symbols only 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 Since glibc 2.1.1,
135 .\" commit 12b5b6b7f78ea111e89bbf638294a5413c791072
136 this flag is overridden by the effect of the
137 .B LD_BIND_NOW
138 environment variable.
139 .TP
140 .B RTLD_NOW
141 If this value is specified, or the environment variable
142 .B LD_BIND_NOW
143 is set to a nonempty string,
144 all undefined symbols in the shared object are resolved before
145 .BR dlopen ()
146 returns.
147 If this cannot be done, an error is returned.
148 .PP
149 Zero or more of the following values may also be ORed in
150 .IR flags :
151 .TP
152 .B RTLD_GLOBAL
153 The symbols defined by this shared object will be
154 made available for symbol resolution of subsequently loaded shared objects.
155 .TP
156 .B RTLD_LOCAL
157 This is the converse of
158 .BR RTLD_GLOBAL ,
159 and the default if neither flag is specified.
160 Symbols defined in this shared object are not made available to resolve
161 references in subsequently loaded shared objects.
162 .TP
163 .BR RTLD_NODELETE " (since glibc 2.2)"
164 Do not unload the shared object during
165 .BR dlclose ().
166 Consequently, the object's static and global variables are not reinitialized
167 if the object is reloaded with
168 .BR dlopen ()
169 at a later time.
170 .TP
171 .BR RTLD_NOLOAD " (since glibc 2.2)"
172 Don't load the shared object.
173 This can be used to test if the object is already resident
174 .RB ( dlopen ()
175 returns NULL if it is not, or the object's handle if it is resident).
176 This flag can also be used to promote the flags on a shared object
177 that is already loaded.
178 For example, a shared object that was previously loaded with
179 .B RTLD_LOCAL
180 can be reopened with
181 .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
182 .\"
183 .TP
184 .BR RTLD_DEEPBIND " (since glibc 2.3.4)"
185 .\" Inimitably described by UD in
186 .\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
187 Place the lookup scope of the symbols in this
188 shared object ahead of the global scope.
189 This means that a self-contained object will use
190 its own symbols in preference to global symbols with the same name
191 contained in objects that have already been loaded.
192 .PP
193 If
194 .I filename
195 is NULL, then the returned handle is for the main program.
196 When given to
197 .BR dlsym (),
198 this handle causes a search for a symbol in the main program,
199 followed by all shared objects loaded at program startup,
200 and then all shared objects loaded by
201 .BR dlopen ()
202 with the flag
203 .BR RTLD_GLOBAL .
204 .PP
205 Symbol references in the shared object are resolved using (in order):
206 symbols in the link map of objects loaded for the main program and its
207 dependencies;
208 symbols in shared objects (and their dependencies)
209 that were previously opened with
210 .BR dlopen ()
211 using the
212 .BR RTLD_GLOBAL
213 flag;
214 and definitions in the shared object itself
215 (and any dependencies that were loaded for that object).
216 .PP
217 Any global symbols in the executable that were placed into
218 its dynamic symbol table by
219 .BR ld (1)
220 can also be used to resolve references in a dynamically loaded shared object.
221 Symbols may be placed in the dynamic symbol table
222 either because the executable was linked with the flag "\-rdynamic"
223 (or, synonymously, "\-\-export\-dynamic"), which causes all of
224 the executable's global symbols to be placed in the dynamic symbol table,
225 or because
226 .BR ld (1)
227 noted a dependency on a symbol in another object during static linking.
228 .PP
229 If the same shared object is opened again with
230 .BR dlopen (),
231 the same object handle is returned.
232 The dynamic linker maintains reference
233 counts for object handles, so a dynamically loaded shared object is not
234 deallocated until
235 .BR dlclose ()
236 has been called on it as many times as
237 .BR dlopen ()
238 has succeeded on it.
239 Constructors (see below) are called only when the object is actually loaded
240 into memory (i.e., when the reference count increases to 1).
241 .PP
242 A subsequent
243 .BR dlopen ()
244 call that loads the same shared object with
245 .B RTLD_NOW
246 may force symbol resolution for a shared object earlier loaded with
247 .BR RTLD_LAZY .
248 Similarly, an object that was previously opened with
249 .BR RTLD_LOCAL
250 can be promoted to
251 .BR RTLD_GLOBAL
252 in a subsequent
253 .BR dlopen ().
254 .PP
255 If
256 .BR dlopen ()
257 fails for any reason, it returns NULL.
258 .\"
259 .SS dlmopen()
260 This function performs the same task as
261 .BR dlopen ()\(emthe
262 .I filename
263 and
264 .I flags
265 arguments, as well as the return value, are the same,
266 except for the differences noted below.
267 .PP
268 The
269 .BR dlmopen ()
270 function differs from
271 .BR dlopen ()
272 primarily in that it accepts an additional argument,
273 .IR lmid ,
274 that specifies the link-map list (also referred to as a
275 .IR namespace )
276 in which the shared object should be loaded.
277 (By comparison,
278 .BR dlopen ()
279 adds the dynamically loaded shared object to the same namespace as
280 the shared object from which the
281 .BR dlopen ()
282 call is made.)
283 The
284 .I Lmid_t
285 type is an opaque handle that refers to a namespace.
286 .PP
287 The
288 .I lmid
289 argument is either the ID of an existing namespace
290 .\" FIXME: Is using dlinfo() RTLD_DI_LMID the right technique?
291 (which can be obtained using the
292 .BR dlinfo (3)
293 .B RTLD_DI_LMID
294 request) or one of the following special values:
295 .TP
296 .B LM_ID_BASE
297 Load the shared object in the initial namespace
298 (i.e., the application's namespace).
299 .TP
300 .B LM_ID_NEWLM
301 Create a new namespace and load the shared object in that namespace.
302 The object must have been correctly linked
303 to reference all of the other shared objects that it requires,
304 since the new namespace is initially empty.
305 .PP
306 If
307 .I filename
308 is NULL, then the only permitted value for
309 .I lmid
310 is
311 .BR LM_ID_BASE .
312 .SS dlclose()
313 The function
314 .BR dlclose ()
315 decrements the reference count on the
316 dynamically loaded shared object referred to by
317 .IR handle .
318 .PP
319 If the object's reference count drops to zero
320 and no symbols in this object are required by other objects,
321 then the object is unloaded
322 after first calling any destructors defined for the object.
323 (Symbols in this object might be required in another object
324 because this object was opened with the
325 .BR RTLD_GLOBAL
326 flag and one of its symbols satisfied a relocation in another object.)
327 .PP
328 All shared objects that were automatically loaded when
329 .BR dlopen ()
330 was invoked on the object referred to by
331 .I handle
332 are recursively closed in the same manner.
333 .PP
334 A successful return from
335 .BR dlclose ()
336 does not guarantee that the symbols associated with
337 .I handle
338 are removed from the caller's address space.
339 In addition to references resulting from explicit
340 .BR dlopen ()
341 calls, a shared object may have been implicitly loaded
342 (and reference counted) because of dependencies in other shared objects.
343 Only when all references have been released can the shared object
344 be removed from the address space.
345 .SH RETURN VALUE
346 On success,
347 .BR dlopen ()
348 and
349 .BR dlmopen ()
350 return a non-NULL handle for the loaded object.
351 On error
352 (file could not be found, was not readable, had the wrong format,
353 or caused errors during loading),
354 these functions return NULL.
355 .PP
356 On success,
357 .BR dlclose ()
358 returns 0; on error, it returns a nonzero value.
359 .PP
360 Errors from these functions can be diagnosed using
361 .BR dlerror (3).
362 .SH VERSIONS
363 .BR dlopen ()
364 and
365 .BR dlclose ()
366 are present in glibc 2.0 and later.
367 .BR dlmopen ()
368 first appeared in glibc 2.3.4.
369 .SH ATTRIBUTES
370 For an explanation of the terms used in this section, see
371 .BR attributes (7).
372 .TS
373 allbox;
374 lbw30 lb lb
375 l l l.
376 Interface Attribute Value
377 T{
378 .BR dlopen (),
379 .BR dlmopen (),
380 .BR dlclose ()
381 T} Thread safety MT-Safe
382 .TE
383 .SH CONFORMING TO
384 POSIX.1-2001 describes
385 .BR dlclose ()
386 and
387 .BR dlopen ().
388 The
389 .BR dlmopen ()
390 function is a GNU extension.
391 .PP
392 The
393 .BR RTLD_NOLOAD ,
394 .BR RTLD_NODELETE ,
395 and
396 .BR RTLD_DEEPBIND
397 flags are GNU extensions;
398 the first two of these flags are also present on Solaris.
399 .SH NOTES
400 .SS dlmopen() and namespaces
401 A link-map list defines an isolated namespace for the
402 resolution of symbols by the dynamic linker.
403 Within a namespace,
404 dependent shared objects are implicitly loaded according to the usual rules,
405 and symbol references are likewise resolved according to the usual rules,
406 but such resolution is confined to the definitions provided by the
407 objects that have been (explicitly and implicitly) loaded into the namespace.
408 .PP
409 The
410 .BR dlmopen ()
411 function permits object-load isolation\(emthe ability
412 to load a shared object in a new namespace without
413 exposing the rest of the application to the symbols
414 made available by the new object.
415 Note that the use of the
416 .B RTLD_LOCAL
417 flag is not sufficient for this purpose,
418 since it prevents a shared object's symbols from being available to
419 .I any
420 other shared object.
421 In some cases,
422 we may want to make the symbols provided by a dynamically
423 loaded shared object available to (a subset of) other shared objects
424 without exposing those symbols to the entire application.
425 This can be achieved by using a separate namespace and the
426 .B RTLD_GLOBAL
427 flag.
428 .PP
429 The
430 .BR dlmopen ()
431 function also can be used to provide better isolation than the
432 .BR RTLD_LOCAL
433 flag.
434 In particular, shared objects loaded with
435 .BR RTLD_LOCAL
436 may be promoted to
437 .BR RTLD_GLOBAL
438 if they are dependencies of another shared object loaded with
439 .BR RTLD_GLOBAL .
440 Thus,
441 .BR RTLD_LOCAL
442 is insufficient to isolate a loaded shared object except in the (uncommon)
443 case where one has explicit control over all shared object dependencies.
444 .PP
445 Possible uses of
446 .BR dlmopen ()
447 are plugins where the author of the plugin-loading framework
448 can't trust the plugin authors and does not wish
449 any undefined symbols from the plugin framework to be resolved to plugin
450 symbols.
451 Another use is to load the same object more than once.
452 Without the use of
453 .BR dlmopen (),
454 this would require the creation of distinct copies of the shared object file.
455 Using
456 .BR dlmopen (),
457 this can be achieved by loading the same shared object file into
458 different namespaces.
459 .PP
460 The glibc implementation supports a maximum of
461 .\" DL_NNS
462 16 namespaces.
463 .\"
464 .SS Initialization and finalization functions
465 Shared objects may export functions using the
466 .B __attribute__((constructor))
467 and
468 .B __attribute__((destructor))
469 function attributes.
470 Constructor functions are executed before
471 .BR dlopen ()
472 returns, and destructor functions are executed before
473 .BR dlclose ()
474 returns.
475 A shared object may export multiple constructors and destructors,
476 and priorities can be associated with each function
477 to determine the order in which they are executed.
478 See the
479 .BR gcc
480 info pages (under "Function attributes")
481 .\" info gcc "C Extensions" "Function attributes"
482 for further information.
483 .PP
484 An older method of (partially) achieving the same result is via the use of
485 two special symbols recognized by the linker:
486 .B _init
487 and
488 .BR _fini .
489 If a dynamically loaded shared object exports a routine named
490 .BR _init (),
491 then that code is executed after loading a shared object, before
492 .BR dlopen ()
493 returns.
494 If the shared object exports a routine named
495 .BR _fini (),
496 then that routine is called just before the object is unloaded.
497 In this case, one must avoid linking against the system startup files,
498 which contain default versions of these files;
499 this can be done by using the
500 .BR gcc (1)
501 .I \-nostartfiles
502 command-line option.
503 .PP
504 Use of
505 .B _init
506 and
507 .BR _fini
508 is now deprecated in favor of the aforementioned
509 constructors and destructors,
510 which among other advantages,
511 permit multiple initialization and finalization functions to be defined.
512 .\"
513 .\" Using these routines, or the gcc
514 .\" .B \-nostartfiles
515 .\" or
516 .\" .B \-nostdlib
517 .\" options, is not recommended.
518 .\" Their use may result in undesired behavior,
519 .\" since the constructor/destructor routines will not be executed
520 .\" (unless special measures are taken).
521 .\" .\" void _init(void) __attribute__((constructor));
522 .\" .\" void _fini(void) __attribute__((destructor));
523 .\"
524 .PP
525 Since glibc 2.2.3,
526 .BR atexit (3)
527 can be used to register an exit handler that is automatically
528 called when a shared object is unloaded.
529 .SS History
530 These functions are part of the dlopen API, derived from SunOS.
531 .SH BUGS
532 As at glibc 2.24, specifying the
533 .BR RTLD_GLOBAL
534 flag when calling
535 .BR dlmopen ()
536 .\" dlerror(): "invalid mode"
537 generates an error.
538 Furthermore, specifying
539 .BR RTLD_GLOBAL
540 when calling
541 .BR dlopen ()
542 results in a program crash
543 .RB ( SIGSEGV )
544 if the call is made from any object loaded in a
545 namespace other than the initial namespace.
546 .SH EXAMPLE
547 The program below loads the (glibc) math library,
548 looks up the address of the
549 .BR cos (3)
550 function, and prints the cosine of 2.0.
551 The following is an example of building and running the program:
552 .PP
553 .in +4n
554 .EX
555 $ \fBcc dlopen_demo.c \-ldl\fP
556 $ \fB./a.out\fP
557 \-0.416147
558 .EE
559 .in
560 .SS Program source
561 \&
562 .EX
563 #include <stdio.h>
564 #include <stdlib.h>
565 #include <dlfcn.h>
566 #include <gnu/lib-names.h> /* Defines LIBM_SO (which will be a
567 string such as "libm.so.6") */
568 int
569 main(void)
570 {
571 void *handle;
572 double (*cosine)(double);
573 char *error;
574
575 handle = dlopen(LIBM_SO, RTLD_LAZY);
576 if (!handle) {
577 fprintf(stderr, "%s\en", dlerror());
578 exit(EXIT_FAILURE);
579 }
580
581 dlerror(); /* Clear any existing error */
582
583 cosine = (double (*)(double)) dlsym(handle, "cos");
584
585 /* According to the ISO C standard, casting between function
586 pointers and 'void *', as done above, produces undefined results.
587 POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and
588 proposed the following workaround:
589
590 *(void **) (&cosine) = dlsym(handle, "cos");
591
592 This (clumsy) cast conforms with the ISO C standard and will
593 avoid any compiler warnings.
594
595 The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a.
596 POSIX.1-2013) improved matters by requiring that conforming
597 implementations support casting 'void *' to a function pointer.
598 Nevertheless, some compilers (e.g., gcc with the '-pedantic'
599 option) may complain about the cast used in this program. */
600 .\" http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08
601 .\" http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html#tag_16_96_07
602 .\" http://austingroupbugs.net/view.php?id=74
603
604 error = dlerror();
605 if (error != NULL) {
606 fprintf(stderr, "%s\en", error);
607 exit(EXIT_FAILURE);
608 }
609
610 printf("%f\en", (*cosine)(2.0));
611 dlclose(handle);
612 exit(EXIT_SUCCESS);
613 }
614 .EE
615 .SH SEE ALSO
616 .BR ld (1),
617 .BR ldd (1),
618 .BR pldd (1),
619 .BR dl_iterate_phdr (3),
620 .BR dladdr (3),
621 .BR dlerror (3),
622 .BR dlinfo (3),
623 .BR dlsym (3),
624 .BR rtld-audit (7),
625 .BR ld.so (8),
626 .BR ldconfig (8)
627 .PP
628 gcc info pages, ld info pages