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 Michael Kerrisk (mtk.manpages@gmail.com).
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.
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.
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.
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/>.
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
35 .TH DLOPEN 3 2014-01-08 "Linux" "Linux Programmer's Manual"
37 dladdr, dlclose, dlerror, dlopen, dlsym, dlvsym \- programming interface to
38 dynamic linking loader
42 .BI "void *dlopen(const char *" filename ", int " flag );
44 .B "char *dlerror(void);"
46 .BI "void *dlsym(void *" handle ", const char *" symbol );
48 .BI "int dlclose(void *" handle );
50 Link with \fI\-ldl\fP.
57 implement the interface to the dynamic linking loader.
61 returns a human-readable string describing the most recent error
67 since the last call to
69 It returns NULL if no errors have occurred since initialization or since
74 loads the dynamic library file named by the null-terminated
77 and returns an opaque "handle" for the dynamic library.
80 is NULL, then the returned handle is for the main program.
83 contains a slash ("/"), then it is interpreted as a (relative
84 or absolute) pathname.
85 Otherwise, the dynamic linker searches for the library as follows
90 (ELF only) If the executable file for the calling program
91 contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag,
92 then the directories listed in the DT_RPATH tag are searched.
94 If, at the time that the program was started, the environment variable
96 was defined to contain a colon-separated list of directories,
97 then these are searched.
98 (As a security measure this variable is ignored for set-user-ID and
99 set-group-ID programs.)
101 (ELF only) If the executable file for the calling program
102 contains a DT_RUNPATH tag, then the directories listed in that tag
109 is checked to see whether it contains an entry for
116 are searched (in that order).
118 If the library has dependencies on other shared libraries,
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 libraries in turn have dependencies, and so on.)
124 One of the following two values must be included in
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 library is loaded.)
136 If this value is specified, or the environment variable
138 is set to a nonempty string,
139 all undefined symbols in the library are resolved before
142 If this cannot be done, an error is returned.
144 Zero or more of the following values may also be ORed in
148 The symbols defined by this library will be
149 made available for symbol resolution of subsequently loaded libraries.
152 This is the converse of
154 and the default if neither flag is specified.
155 Symbols defined in this library are not made available to resolve
156 references in subsequently loaded libraries.
158 .BR RTLD_NODELETE " (since glibc 2.2)"
159 Do not unload the library during
161 Consequently, the library's static variables are not reinitialized
162 if the library is reloaded with
165 This flag is not specified in POSIX.1-2001.
166 .\" (But it is present on Solaris.)
168 .BR RTLD_NOLOAD " (since glibc 2.2)"
169 Don't load the library.
170 This can be used to test if the library is already resident
172 returns NULL if it is not, or the library's handle if it is resident).
173 This flag can also be used to promote the flags on a library
174 that is already loaded.
175 For example, a library that was previously loaded with
178 .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL .
179 This flag is not specified in POSIX.1-2001.
180 .\" (But it is present on Solaris.)
183 .BR RTLD_DEEPBIND " (since glibc 2.3.4)"
184 .\" Inimitably described by UD in
185 .\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html.
186 Place the lookup scope of the symbols in this
187 library ahead of the global scope.
188 This means that a self-contained library will use
189 its own symbols in preference to global symbols with the same name
190 contained in libraries that have already been loaded.
191 This flag is not specified in POSIX.1-2001.
195 is NULL, then the returned handle is for the main program.
198 this handle causes a search for a symbol in the main program,
199 followed by all shared libraries loaded at program startup,
200 and then all shared libraries loaded by
205 External references in the library are resolved using the libraries
206 in that library's dependency list and any other libraries previously
210 If the executable was linked with the flag "\-rdynamic"
211 (or, synonymously, "\-\-export\-dynamic"),
212 then the global symbols in the executable will also be used
213 to resolve references in a dynamically loaded library.
215 If the same library is loaded again with
217 the same library handle is returned.
218 The dl library maintains reference
219 counts for library handles, so a dynamic library is not
222 has been called on it as many times as
227 routine, if present, is called only once.
228 But a subsequent call with
230 may force symbol resolution for a library earlier loaded with
235 fails for any reason, it returns NULL.
239 takes a "handle" of a dynamic library returned by
242 null-terminated symbol name, returning the address where that symbol is
244 If the symbol is not found, in the specified
245 library or any of the libraries that were automatically loaded by
247 when that library was loaded,
250 (The search performed by
252 is breadth first through the dependency tree of these libraries.)
253 Since the value of the symbol could actually be NULL (so that a
256 need not indicate an error), the correct way to test for an error
259 to clear any old error conditions, then call
263 again, saving its return value into a variable, and check whether
264 this saved value is not NULL.
266 There are two special pseudo-handles,
270 The former will find the first occurrence of the desired symbol
271 using the default library search order.
273 will find the next occurrence of a function in the search order
274 after the current library.
275 This allows one to provide a wrapper
276 around a function in another shared library.
280 decrements the reference count on the dynamic library handle
282 If the reference count drops to zero and no other loaded libraries use
283 symbols in it, then the dynamic library is unloaded.
287 returns 0 on success, and nonzero on error.
288 .SS The obsolete symbols _init() and _fini()
289 The linker recognizes special symbols
293 If a dynamic library exports a routine named
295 then that code is executed after the loading, before
298 If the dynamic library exports a routine named
300 then that routine is called just before the library is unloaded.
301 In case you need to avoid linking against the system startup files,
302 this can be done by using the
307 Using these routines, or the gcc
311 options, is not recommended.
312 Their use may result in undesired behavior,
313 since 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));
318 Instead, libraries should export routines using the
319 .B __attribute__((constructor))
321 .B __attribute__((destructor))
323 See the gcc info pages for information on these.
324 Constructor routines are executed before
326 returns, and destructor routines are executed before
329 .SS Glibc extensions: dladdr() and dlvsym()
330 Glibc adds two functions not described by POSIX, with prototypes
333 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
334 .B #include <dlfcn.h>
336 .BI "int dladdr(void *" addr ", Dl_info *" info );
338 .BI "void *dlvsym(void *" handle ", char *" symbol ", char *" version );
343 takes a function pointer and tries to resolve name
344 and file where it is located.
345 Information is stored in the
352 const char *dli_fname; /* Pathname of shared object that
354 void *dli_fbase; /* Address at which shared object
356 const char *dli_sname; /* Name of symbol whose definition
357 overlaps \fIaddr\fP */
358 void *dli_saddr; /* Exact address of symbol named
359 in \fIdli_sname\fP */
364 If no symbol matching
373 returns 0 on error, and nonzero on success.
377 provided by glibc since version 2.1,
380 but takes a version string as an additional argument.
382 POSIX.1-2001 describes
397 was defined before including it.
399 .\" The string returned by
401 .\" should not be modified.
402 .\" Some systems give the prototype as
405 .\" .B "const char *dlerror(void);"
410 can be used to register an exit handler that is automatically
411 called when a library is unloaded.
413 The dlopen interface standard comes from SunOS.
419 Sometimes, the function pointers you pass to
422 On some architectures (notably i386 and x86_64),
426 may end up pointing back at the object from which you called
428 even if the function used as an argument should come from
429 a dynamically linked library.
431 The problem is that the function pointer will still be resolved
432 at compile time, but merely point to the
434 (Procedure Linkage Table)
435 section of the original object (which dispatches the call after
436 asking the dynamic linker to resolve the symbol).
438 you can try to compile the code to be position-independent:
439 then, the compiler cannot prepare the pointer
440 at compile time anymore and today's
442 will generate code that just loads the final symbol address from the
444 (Global Offset Table) at run time before passing it to
447 Load the math library, and print the cosine of 2.0:
455 main(int argc, char **argv)
458 double (*cosine)(double);
461 handle = dlopen("libm.so", RTLD_LAZY);
463 fprintf(stderr, "%s\en", dlerror());
467 dlerror(); /* Clear any existing error */
469 cosine = (double (*)(double)) dlsym(handle, "cos");
471 /* According to the ISO C standard, casting between function
472 pointers and 'void *', as done above, produces undefined results.
473 POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and
474 proposed the following workaround:
476 *(void **) (&cosine) = dlsym(handle, "cos");
478 This (clumsy) cast conforms with the ISO C standard and will
479 avoid any compiler warnings.
481 The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a.
482 POSIX.1-2013) improved matters by requiring that conforming
483 implementations support casting 'void *' to a function pointer.
484 Nevertheless, some compilers (e.g., gcc with the '-pedantic'
485 option) may complain about the cast used in this program. */
486 .\" http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08
487 .\" http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html#tag_16_96_07
488 .\" http://austingroupbugs.net/view.php?id=74
492 fprintf(stderr, "%s\en", error);
496 printf("%f\en", (*cosine)(2.0));
502 If this program were in a file named "foo.c", you would build the program
503 with the following command:
506 gcc \-rdynamic \-o foo foo.c \-ldl
513 will want to be compiled as
519 gcc \-shared \-nostartfiles \-o bar bar.c
524 .BR dl_iterate_phdr (3),
529 ld.so info pages, gcc info pages, ld info pages