]>
Commit | Line | Data |
---|---|---|
fea681da MK |
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). | |
9c169754 | 4 | .\" and Copyright 2003, 2015 Michael Kerrisk (mtk.manpages@gmail.com). |
fea681da | 5 | .\" |
1dd72f9c | 6 | .\" %%%LICENSE_START(GPLv2+_DOC_FULL) |
fea681da MK |
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 | |
c715f741 MK |
23 | .\" License along with this manual; if not, see |
24 | .\" <http://www.gnu.org/licenses/>. | |
6a8d8745 | 25 | .\" %%%LICENSE_END |
fea681da MK |
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 | .\" |
1e64c86b | 35 | .TH DLOPEN 3 2015-03-29 "Linux" "Linux Programmer's Manual" |
fea681da | 36 | .SH NAME |
efdd68a5 | 37 | dlclose, dlerror, dlopen, dlmopen, dlsym, dlvsym \- |
6445992d | 38 | programming interface to dynamic linking loader |
fea681da MK |
39 | .SH SYNOPSIS |
40 | .B #include <dlfcn.h> | |
41 | .sp | |
cf2789f1 | 42 | .BI "void *dlopen(const char *" filename ", int " flags ); |
fea681da | 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 | 49 | .sp |
3f4c09d0 MK |
50 | .B #define _GNU_SOURCE |
51 | .br | |
52 | .B #include <dlfcn.h> | |
53 | .sp | |
efdd68a5 MK |
54 | .BI "void *dlmopen (Lmid_t " lmid ", const char *" filename ", int " flags ); |
55 | .sp | |
3f4c09d0 MK |
56 | .BI "void *dlvsym(void *" handle ", char *" symbol ", char *" version ); |
57 | .sp | |
3f37321b | 58 | Link with \fI\-ldl\fP. |
fea681da | 59 | .SH DESCRIPTION |
3f4c09d0 | 60 | The functions described in this page |
0a78ab7b | 61 | provide an interface to the dynamic linking loader. |
73d8cece | 62 | .SS dlerror() |
fea681da | 63 | The function |
d355f1ed | 64 | .BR dlerror () |
23c9c564 | 65 | returns a human-readable string describing the most recent error |
c13182ef MK |
66 | that occurred from |
67 | .BR dlopen (), | |
68 | .BR dlsym () | |
3f4c09d0 | 69 | .BR dlvsym (), |
c13182ef | 70 | or |
1e321034 | 71 | .BR dlclose () |
fea681da | 72 | since the last call to |
1368e847 | 73 | .BR dlerror (). |
fea681da MK |
74 | It returns NULL if no errors have occurred since initialization or since |
75 | it was last called. | |
73d8cece | 76 | .SS dlopen() |
fea681da | 77 | The function |
d355f1ed | 78 | .BR dlopen () |
43151de3 MK |
79 | loads the dynamic shared object (shared library) |
80 | file named by the null-terminated | |
fea681da MK |
81 | string |
82 | .I filename | |
43151de3 | 83 | and returns an opaque "handle" for the loaded object. |
fea681da MK |
84 | If |
85 | .I filename | |
86 | is NULL, then the returned handle is for the main program. | |
87 | If | |
88 | .I filename | |
89 | contains a slash ("/"), then it is interpreted as a (relative | |
90 | or absolute) pathname. | |
43151de3 | 91 | Otherwise, the dynamic linker searches for the object as follows |
fea681da MK |
92 | (see |
93 | .BR ld.so (8) | |
94 | for further details): | |
86100362 | 95 | .IP o 4 |
fea681da MK |
96 | (ELF only) If the executable file for the calling program |
97 | contains a DT_RPATH tag, and does not contain a DT_RUNPATH tag, | |
98 | then the directories listed in the DT_RPATH tag are searched. | |
99 | .IP o | |
65f6a3ee | 100 | If, at the time that the program was started, the environment variable |
0daa9e92 | 101 | .B LD_LIBRARY_PATH |
65f6a3ee | 102 | was defined to contain a colon-separated list of directories, |
fea681da | 103 | then these are searched. |
8ac45f42 | 104 | (As a security measure, this variable is ignored for set-user-ID and |
880f5b4b | 105 | set-group-ID programs.) |
fea681da MK |
106 | .IP o |
107 | (ELF only) If the executable file for the calling program | |
108 | contains a DT_RUNPATH tag, then the directories listed in that tag | |
109 | are searched. | |
110 | .IP o | |
111 | The cache file | |
0daa9e92 | 112 | .I /etc/ld.so.cache |
fea681da MK |
113 | (maintained by |
114 | .BR ldconfig (8)) | |
115 | is checked to see whether it contains an entry for | |
116 | .IR filename . | |
117 | .IP o | |
118 | The directories | |
c13182ef MK |
119 | .I /lib |
120 | and | |
121 | .I /usr/lib | |
fea681da MK |
122 | are searched (in that order). |
123 | .PP | |
43151de3 MK |
124 | If the object specified by |
125 | .I filename | |
126 | has dependencies on other shared objects, | |
fea681da | 127 | then these are also automatically loaded by the dynamic linker |
c13182ef MK |
128 | using the same rules. |
129 | (This process may occur recursively, | |
43151de3 | 130 | if those objects in turn have dependencies, and so on.) |
fea681da | 131 | .PP |
336e88f0 | 132 | One of the following two values must be included in |
cf2789f1 | 133 | .IR flags : |
336e88f0 | 134 | .TP |
fea681da | 135 | .B RTLD_LAZY |
c13182ef | 136 | Perform lazy binding. |
a3a11667 | 137 | Only resolve symbols as the code that references them is executed. |
336e88f0 | 138 | If the symbol is never referenced, then it is never resolved. |
33a0ccb2 | 139 | (Lazy binding is performed only for function references; |
336e88f0 | 140 | references to variables are always immediately bound when |
43151de3 | 141 | the shared object is loaded.) |
336e88f0 | 142 | .TP |
fea681da | 143 | .B RTLD_NOW |
336e88f0 | 144 | If this value is specified, or the environment variable |
fea681da | 145 | .B LD_BIND_NOW |
aa796481 | 146 | is set to a nonempty string, |
43151de3 | 147 | all undefined symbols in the shared object are resolved before |
d355f1ed | 148 | .BR dlopen () |
c13182ef MK |
149 | returns. |
150 | If this cannot be done, an error is returned. | |
fea681da | 151 | .PP |
dfacdd0a | 152 | Zero or more of the following values may also be ORed in |
cf2789f1 | 153 | .IR flags : |
336e88f0 | 154 | .TP |
fea681da | 155 | .B RTLD_GLOBAL |
43151de3 | 156 | The symbols defined by this shared object will be |
fe854f15 | 157 | made available for symbol resolution of subsequently loaded shared objects. |
c13182ef | 158 | .TP |
336e88f0 | 159 | .B RTLD_LOCAL |
c13182ef | 160 | This is the converse of |
336e88f0 MK |
161 | .BR RTLD_GLOBAL , |
162 | and the default if neither flag is specified. | |
43151de3 MK |
163 | Symbols defined in this shared object are not made available to resolve |
164 | references in subsequently loaded shared objects. | |
336e88f0 | 165 | .TP |
c10859eb | 166 | .BR RTLD_NODELETE " (since glibc 2.2)" |
43151de3 | 167 | Do not unload the shared object during |
336e88f0 | 168 | .BR dlclose (). |
43151de3 MK |
169 | Consequently, the object's static variables are not reinitialized |
170 | if the object is reloaded with | |
336e88f0 MK |
171 | .BR dlopen () |
172 | at a later time. | |
173 | This flag is not specified in POSIX.1-2001. | |
174 | .\" (But it is present on Solaris.) | |
175 | .TP | |
c10859eb | 176 | .BR RTLD_NOLOAD " (since glibc 2.2)" |
43151de3 MK |
177 | Don't load the shared object. |
178 | This can be used to test if the object is already resident | |
336e88f0 | 179 | .RB ( dlopen () |
43151de3 MK |
180 | returns NULL if it is not, or the object's handle if it is resident). |
181 | This flag can also be used to promote the flags on a shared object | |
336e88f0 | 182 | that is already loaded. |
43151de3 | 183 | For example, a shared object that was previously loaded with |
336e88f0 | 184 | .B RTLD_LOCAL |
3b777aff | 185 | can be reopened with |
336e88f0 MK |
186 | .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL . |
187 | This flag is not specified in POSIX.1-2001. | |
188 | .\" (But it is present on Solaris.) | |
189 | .\" | |
bba06189 MK |
190 | .TP |
191 | .BR RTLD_DEEPBIND " (since glibc 2.3.4)" | |
c13182ef | 192 | .\" Inimitably described by UD in |
a3a11667 | 193 | .\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html. |
c13182ef | 194 | Place the lookup scope of the symbols in this |
43151de3 MK |
195 | shared object ahead of the global scope. |
196 | This means that a self-contained object will use | |
c13182ef | 197 | its own symbols in preference to global symbols with the same name |
43151de3 | 198 | contained in objects that have already been loaded. |
bba06189 | 199 | This flag is not specified in POSIX.1-2001. |
fea681da MK |
200 | .PP |
201 | If | |
202 | .I filename | |
b437fdd9 | 203 | is NULL, then the returned handle is for the main program. |
fea681da | 204 | When given to |
d355f1ed | 205 | .BR dlsym (), |
fea681da | 206 | this handle causes a search for a symbol in the main program, |
43151de3 MK |
207 | followed by all shared objects loaded at program startup, |
208 | and then all shared objects loaded by | |
d355f1ed | 209 | .BR dlopen () |
fea681da | 210 | with the flag |
a5e0a0e4 | 211 | .BR RTLD_GLOBAL . |
fea681da | 212 | .PP |
43151de3 MK |
213 | External references in the shared object are resolved using the |
214 | shared objects in that object's dependency list and any other | |
215 | objects previously opened with the | |
fea681da MK |
216 | .B RTLD_GLOBAL |
217 | flag. | |
2bc2f479 MK |
218 | If the executable was linked with the flag "\-rdynamic" |
219 | (or, synonymously, "\-\-export\-dynamic"), | |
fea681da | 220 | then the global symbols in the executable will also be used |
43151de3 | 221 | to resolve references in a dynamically loaded shared object. |
fea681da | 222 | .PP |
43151de3 | 223 | If the same shared object is loaded again with |
d355f1ed | 224 | .BR dlopen (), |
43151de3 | 225 | the same object handle is returned. |
c13182ef | 226 | The dl library maintains reference |
43151de3 | 227 | counts for object handles, so a dynamically loaded shared object is not |
fea681da | 228 | deallocated until |
d355f1ed | 229 | .BR dlclose () |
fea681da | 230 | has been called on it as many times as |
d355f1ed | 231 | .BR dlopen () |
c13182ef MK |
232 | has succeeded on it. |
233 | The | |
86100362 | 234 | .BR _init () |
33a0ccb2 | 235 | routine, if present, is called only once. |
c13182ef | 236 | But a subsequent call with |
fea681da | 237 | .B RTLD_NOW |
9c169754 | 238 | may force symbol resolution for a shared object earlier loaded with |
fea681da MK |
239 | .BR RTLD_LAZY . |
240 | .PP | |
241 | If | |
d355f1ed | 242 | .BR dlopen () |
fea681da | 243 | fails for any reason, it returns NULL. |
9c169754 | 244 | .\" |
efdd68a5 MK |
245 | .SS dlmopen() |
246 | This function performs the same task as | |
247 | .BR dlopen ()\(emthe | |
248 | .I filename | |
249 | and | |
250 | .I flags | |
65175b1d MK |
251 | arguments, as well as the return value, are the same, |
252 | except for the differences noted below. | |
253 | ||
254 | The | |
255 | .BR dlmopen () | |
256 | function differs from | |
257 | .BR dlopen () | |
258 | primarily in that it accepts an additional argument, | |
efdd68a5 | 259 | .IR lmid , |
9c169754 MK |
260 | that specifies the link-map list (also referred to as a |
261 | .IR namespace ) | |
262 | in which the shared object should be loaded. | |
efdd68a5 MK |
263 | (By comparison, |
264 | .BR dlopen () | |
9c169754 | 265 | adds the dynamically loaded shared object to the same namespace as |
efdd68a5 MK |
266 | the shared object from which the |
267 | .BR dlopen () | |
268 | call is made.) | |
9c169754 MK |
269 | The |
270 | .I Lmid_t | |
271 | type is an opaque handle that refers to a namespace. | |
efdd68a5 MK |
272 | |
273 | The | |
274 | .I lmid | |
9c169754 | 275 | argument is either the ID of an existing namespace |
efdd68a5 MK |
276 | .\" FIXME: Is using dlinfo() RTLD_DI_LMID the right technique? |
277 | (which can be obtained using the | |
278 | .BR dlinfo (3) | |
279 | .B RTLD_DI_LMID | |
280 | request) or one of the following special values: | |
281 | .TP | |
282 | .B LM_ID_BASE | |
9c169754 MK |
283 | Load the shared object in the initial namespace |
284 | (i.e., the application's namespace). | |
efdd68a5 MK |
285 | .TP |
286 | .B LM_ID_NEWLM | |
9c169754 MK |
287 | Create a new namespace and load the shared object in that namespace. |
288 | The object must have been correctly | |
289 | (statically) linked | |
290 | to reference all of the other shared objects that it requires, | |
291 | since the new namespace is initially empty. | |
65175b1d MK |
292 | .PP |
293 | Note the following restrictions for | |
294 | .BR dlmopen (): | |
295 | .IP * 3 | |
efdd68a5 MK |
296 | The glibc implementation supports a maximum of |
297 | .\" DL_NNS | |
9c169754 | 298 | 16 namespaces. |
65175b1d MK |
299 | .IP * |
300 | .\" dlerror(): "invalid mode" | |
301 | The value | |
302 | .BR RTLD_GLOBAL | |
303 | cannot be specified in | |
304 | .IR flags . | |
305 | More generally, the | |
306 | .BR RTLD_GLOBAL | |
307 | flag can be used with | |
308 | .BR dlopen () | |
309 | .\" Otherwise: one gets SIGSEGV. | |
310 | only when loading a library into the initial namespace. | |
311 | .IP * | |
312 | .\" dlerror(): "invalid namespace" | |
313 | If | |
314 | .I handle | |
315 | is NULL, then the only permitted value for | |
316 | .I lmid | |
317 | is | |
318 | .BR LM_ID_BASE . | |
9c169754 | 319 | .\" |
73d8cece | 320 | .SS dlsym() |
fea681da | 321 | The function |
d355f1ed | 322 | .BR dlsym () |
43151de3 | 323 | takes a "handle" of a dynamic loaded shared object returned by |
28d88c17 MK |
324 | .BR dlopen () |
325 | and the | |
326 | null-terminated symbol name, returning the address where that symbol is | |
c13182ef MK |
327 | loaded into memory. |
328 | If the symbol is not found, in the specified | |
43151de3 | 329 | object or any of the shared objects that were automatically loaded by |
d355f1ed | 330 | .BR dlopen () |
43151de3 | 331 | when that object was loaded, |
d355f1ed | 332 | .BR dlsym () |
fea681da MK |
333 | returns NULL. |
334 | (The search performed by | |
d355f1ed | 335 | .BR dlsym () |
43151de3 | 336 | is breadth first through the dependency tree of these shared objects.) |
fea681da MK |
337 | Since the value of the symbol could actually be NULL (so that a |
338 | NULL return from | |
d355f1ed | 339 | .BR dlsym () |
fea681da MK |
340 | need not indicate an error), the correct way to test for an error |
341 | is to call | |
d355f1ed | 342 | .BR dlerror () |
fea681da | 343 | to clear any old error conditions, then call |
d355f1ed | 344 | .BR dlsym (), |
fea681da | 345 | and then call |
d355f1ed | 346 | .BR dlerror () |
fea681da MK |
347 | again, saving its return value into a variable, and check whether |
348 | this saved value is not NULL. | |
349 | .PP | |
fad3d64e MK |
350 | There are two special pseudo-handles: |
351 | .TP | |
fea681da | 352 | .B RTLD_DEFAULT |
fad3d64e | 353 | Find the first occurrence of the desired symbol |
43151de3 | 354 | using the default shared object search order. |
99175a58 MK |
355 | The search will include global symbols in the executable |
356 | and its dependencies, | |
43151de3 | 357 | as well as symbols in shared objects that were dynamically loaded with the |
99175a58 MK |
358 | .BR RTLD_GLOBAL |
359 | flag. | |
fad3d64e MK |
360 | .TP |
361 | .BR RTLD_NEXT | |
b6fe25f7 | 362 | Find the next occurrence of the desired symbol in the search order |
43151de3 | 363 | after the current object. |
c13182ef | 364 | This allows one to provide a wrapper |
43151de3 MK |
365 | around a function in another shared object, so that, for example, |
366 | the definition of a function in a preloaded shared object | |
4251757a MK |
367 | (see |
368 | .B LD_PRELOAD | |
369 | in | |
370 | .BR ld.so (8)) | |
43151de3 | 371 | can find and invoke the "real" function provided in another shared object |
4251757a MK |
372 | (or for that matter, the "next" definition of the function in cases |
373 | where there are multiple layers of preloading). | |
3f4c09d0 MK |
374 | .SS dlvsym() |
375 | The function | |
376 | .BR dlvsym () | |
377 | does the same as | |
378 | .BR dlsym () | |
379 | but takes a version string as an additional argument. | |
73d8cece | 380 | .SS dlclose() |
fea681da | 381 | The function |
d355f1ed | 382 | .BR dlclose () |
43151de3 MK |
383 | decrements the reference count on the |
384 | dynamically loaded shared object referred to by | |
fea681da | 385 | .IR handle . |
4a1af09b | 386 | If the reference count drops to zero, |
43151de3 | 387 | then the object is unloaded. |
cc2ddf2f MK |
388 | All shared objects that were automatically loaded when |
389 | .BR dlopen () | |
390 | was invoked on the object referred to by | |
391 | .I handle | |
392 | are recursively closed in the same manner. | |
fea681da MK |
393 | .LP |
394 | The function | |
d355f1ed | 395 | .BR dlclose () |
c7094399 | 396 | returns 0 on success, and nonzero on error. |
6445992d MK |
397 | .SH VERSIONS |
398 | .BR dlopen (), | |
399 | .BR dlsym (), | |
400 | .BR dlclose (), | |
401 | and | |
402 | .BR dlerror () | |
403 | are present in glibc 2.0 and later. | |
efdd68a5 MK |
404 | .BR dlmopen () |
405 | first appeared in glibc 2.3.4. | |
6445992d MK |
406 | .BR dlvsym () |
407 | first appeared in glibc 2.1. | |
47297adb | 408 | .SH CONFORMING TO |
2b2581ee MK |
409 | POSIX.1-2001 describes |
410 | .BR dlclose (), | |
411 | .BR dlerror (), | |
412 | .BR dlopen (), | |
413 | and | |
414 | .BR dlsym (). | |
3f4c09d0 | 415 | The |
efdd68a5 MK |
416 | .BR dlmopen () |
417 | and | |
3f4c09d0 | 418 | .BR dlvsym () |
efdd68a5 | 419 | functions are GNU extensions. |
2b2581ee | 420 | .SH NOTES |
097585ed MK |
421 | The symbols |
422 | .B RTLD_DEFAULT | |
423 | and | |
424 | .B RTLD_NEXT | |
425 | are defined by | |
2b2581ee | 426 | .I <dlfcn.h> |
c3dfd2c8 MK |
427 | only when |
428 | .B _GNU_SOURCE | |
429 | was defined before including it. | |
2b2581ee MK |
430 | .\" .LP |
431 | .\" The string returned by | |
432 | .\" .BR dlerror () | |
433 | .\" should not be modified. | |
434 | .\" Some systems give the prototype as | |
435 | .\" .sp | |
436 | .\" .in +5 | |
437 | .\" .B "const char *dlerror(void);" | |
438 | .\" .in | |
439 | ||
440 | Since glibc 2.2.3, | |
441 | .BR atexit (3) | |
442 | can be used to register an exit handler that is automatically | |
43151de3 | 443 | called when a shared object is unloaded. |
9c169754 MK |
444 | .\" |
445 | .SS dlmopen() and namespaces | |
446 | A link-map list defines an isolated namespace for the | |
447 | resolution of symbols by the dynamic linker. | |
448 | Within a namespace, | |
449 | dependent shared objects are implicitly loaded according to the usual rules, | |
450 | and symbol references are likewise resolved according to the usual rules, | |
451 | but such resolution is confined to the definitions provided by the | |
452 | objects that have been (explicitly and implicitly) loaded into the namespace. | |
453 | ||
454 | The | |
455 | .BR dlmopen () | |
456 | function permits object-load isolation\(emthe ability | |
457 | to load a shared object in a new namespace without | |
458 | exposing the rest of the application to the symbols | |
459 | made available by the new object. | |
460 | Note that the use of the | |
461 | .B RTLD_LOCAL | |
462 | flag is not sufficient for this purpose, | |
463 | since it prevents a shared object's symbols from being available to | |
464 | .I any | |
465 | other shared object. | |
466 | In some cases, | |
467 | we may want to make the symbols provided by a dynamically | |
43151de3 | 468 | loaded shared object available to (a subset of) other shared objects |
9c169754 | 469 | without exposing those symbols to the entire application. |
65175b1d MK |
470 | .\" FIXME It's still not clear to me what dlmopen() gives us that |
471 | .\" RTLD_LOCAL did not... | |
9c169754 MK |
472 | |
473 | Possible uses of | |
474 | .BR dlmopen () | |
475 | are plugins where the author of the plugin-loading framework | |
476 | can't trust the plugin authors and does not wish | |
477 | any undefined symbols from the plugin framework to be resolved to plugin | |
478 | symbols. | |
479 | Another use is to load the same object more than once. | |
480 | Without the use of | |
481 | .BR dlopen (), | |
482 | this would require the creation of distinct copies of the shared object file. | |
483 | Using | |
484 | .BR dlopen (), | |
485 | this can be achieved by loading the same shared object file into | |
486 | different namespaces. | |
487 | .\" | |
e8290357 MK |
488 | .SS The obsolete symbols _init() and _fini() |
489 | The linker recognizes special symbols | |
490 | .B _init | |
491 | and | |
492 | .BR _fini . | |
493 | If a dynamically loaded shared object exports a routine named | |
494 | .BR _init (), | |
495 | then that code is executed after the loading, before | |
496 | .BR dlopen () | |
497 | returns. | |
498 | If the shared object exports a routine named | |
499 | .BR _fini (), | |
500 | then that routine is called just before the object is unloaded. | |
501 | In case you need to avoid linking against the system startup files, | |
502 | this can be done by using the | |
503 | .BR gcc (1) | |
504 | .I \-nostartfiles | |
505 | command-line option. | |
506 | .LP | |
507 | Using these routines, or the gcc | |
508 | .B \-nostartfiles | |
509 | or | |
510 | .B \-nostdlib | |
511 | options, is not recommended. | |
512 | Their use may result in undesired behavior, | |
513 | since the constructor/destructor routines will not be executed | |
514 | (unless special measures are taken). | |
515 | .\" void _init(void) __attribute__((constructor)); | |
516 | .\" void _fini(void) __attribute__((destructor)); | |
517 | .LP | |
518 | Instead, shared objects should export routines using the | |
519 | .B __attribute__((constructor)) | |
520 | and | |
521 | .B __attribute__((destructor)) | |
522 | function attributes. | |
523 | See the gcc info pages for information on these. | |
524 | Constructor routines are executed before | |
525 | .BR dlopen () | |
526 | returns, and destructor routines are executed before | |
527 | .BR dlclose () | |
528 | returns. | |
529 | .\" | |
2b2581ee MK |
530 | .SS History |
531 | The dlopen interface standard comes from SunOS. | |
6445992d | 532 | That system does not have |
efdd68a5 MK |
533 | .BR dlvsym (), |
534 | but does have | |
535 | .BR dlmopen (). | |
fea681da | 536 | .SH EXAMPLE |
9ff08aad | 537 | Load the math library, and print the cosine of 2.0: |
fea681da | 538 | .nf |
9ff08aad | 539 | |
fea681da | 540 | #include <stdio.h> |
b28f7a67 | 541 | #include <stdlib.h> |
fea681da MK |
542 | #include <dlfcn.h> |
543 | ||
c13182ef MK |
544 | int |
545 | main(int argc, char **argv) | |
cf0a9ace | 546 | { |
fea681da MK |
547 | void *handle; |
548 | double (*cosine)(double); | |
549 | char *error; | |
550 | ||
cf0a9ace | 551 | handle = dlopen("libm.so", RTLD_LAZY); |
fea681da | 552 | if (!handle) { |
31a6818e | 553 | fprintf(stderr, "%s\en", dlerror()); |
4c44ffe5 | 554 | exit(EXIT_FAILURE); |
fea681da MK |
555 | } |
556 | ||
557 | dlerror(); /* Clear any existing error */ | |
c73d7281 | 558 | |
29965345 | 559 | cosine = (double (*)(double)) dlsym(handle, "cos"); |
c73d7281 | 560 | |
daf28a2b | 561 | /* According to the ISO C standard, casting between function |
4318a7d0 MK |
562 | pointers and 'void *', as done above, produces undefined results. |
563 | POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and | |
564 | proposed the following workaround: | |
29965345 MK |
565 | |
566 | *(void **) (&cosine) = dlsym(handle, "cos"); | |
567 | ||
4318a7d0 MK |
568 | This (clumsy) cast conforms with the ISO C standard and will |
569 | avoid any compiler warnings. | |
29965345 | 570 | |
daf28a2b MK |
571 | The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a. |
572 | POSIX.1-2013) improved matters by requiring that conforming | |
573 | implementations support casting 'void *' to a function pointer. | |
574 | Nevertheless, some compilers (e.g., gcc with the '-pedantic' | |
575 | option) may complain about the cast used in this program. */ | |
576 | .\" http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08 | |
577 | .\" http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html#tag_16_96_07 | |
5e0622ba | 578 | .\" http://austingroupbugs.net/view.php?id=74 |
c73d7281 | 579 | |
aefd6f89 | 580 | error = dlerror(); |
3487d63b | 581 | if (error != NULL) { |
31a6818e | 582 | fprintf(stderr, "%s\en", error); |
4c44ffe5 | 583 | exit(EXIT_FAILURE); |
fea681da MK |
584 | } |
585 | ||
31a6818e | 586 | printf("%f\en", (*cosine)(2.0)); |
fea681da | 587 | dlclose(handle); |
5bc8c34c | 588 | exit(EXIT_SUCCESS); |
fea681da | 589 | } |
fea681da | 590 | .fi |
fea681da MK |
591 | .PP |
592 | If this program were in a file named "foo.c", you would build the program | |
593 | with the following command: | |
a6e2f128 | 594 | .in +4n |
fea681da | 595 | .LP |
f4398177 | 596 | gcc \-rdynamic \-o foo foo.c \-ldl |
a6e2f128 | 597 | .in |
fea681da | 598 | .PP |
43151de3 | 599 | Shared objects exporting (the obsolete) |
86100362 | 600 | .BR _init () |
988db661 | 601 | and |
86100362 MK |
602 | .BR _fini () |
603 | will want to be compiled as | |
c6fa0841 MK |
604 | follows, using |
605 | .I bar.c | |
606 | as the example name: | |
a6e2f128 | 607 | .in +4n |
fea681da | 608 | .LP |
f4398177 | 609 | gcc \-shared \-nostartfiles \-o bar bar.c |
a6e2f128 | 610 | .in |
47297adb | 611 | .SH SEE ALSO |
fea681da MK |
612 | .BR ld (1), |
613 | .BR ldd (1), | |
5d95b7d8 | 614 | .BR pldd (1), |
fea681da | 615 | .BR dl_iterate_phdr (3), |
6c46d3bc | 616 | .BR dladdr (3), |
46db2df1 | 617 | .BR dlinfo (3), |
c18ecec9 | 618 | .BR rtld-audit (7), |
fea681da | 619 | .BR ld.so (8), |
173fe7e7 DP |
620 | .BR ldconfig (8) |
621 | ||
7e936c29 | 622 | gcc info pages, ld info pages |