]>
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 |
374b34d3 MK |
37 | dlclose, dlopen, dlmopen \- |
38 | open and close a shared object | |
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 |
fea681da | 44 | .BI "int dlclose(void *" handle ); |
3f37321b | 45 | .sp |
3f4c09d0 MK |
46 | .B #define _GNU_SOURCE |
47 | .br | |
48 | .B #include <dlfcn.h> | |
49 | .sp | |
efdd68a5 MK |
50 | .BI "void *dlmopen (Lmid_t " lmid ", const char *" filename ", int " flags ); |
51 | .sp | |
3f37321b | 52 | Link with \fI\-ldl\fP. |
fea681da | 53 | .SH DESCRIPTION |
73d8cece | 54 | .SS dlopen() |
fea681da | 55 | The function |
d355f1ed | 56 | .BR dlopen () |
43151de3 MK |
57 | loads the dynamic shared object (shared library) |
58 | file named by the null-terminated | |
fea681da MK |
59 | string |
60 | .I filename | |
43151de3 | 61 | and returns an opaque "handle" for the loaded object. |
6ceba646 MK |
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 | ||
fea681da MK |
69 | If |
70 | .I filename | |
6962fdf9 MK |
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 | .\" | |
fea681da MK |
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. | |
43151de3 | 83 | Otherwise, the dynamic linker searches for the object as follows |
fea681da MK |
84 | (see |
85 | .BR ld.so (8) | |
86 | for further details): | |
86100362 | 87 | .IP o 4 |
fea681da MK |
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 | |
65f6a3ee | 92 | If, at the time that the program was started, the environment variable |
0daa9e92 | 93 | .B LD_LIBRARY_PATH |
65f6a3ee | 94 | was defined to contain a colon-separated list of directories, |
fea681da | 95 | then these are searched. |
8ac45f42 | 96 | (As a security measure, this variable is ignored for set-user-ID and |
880f5b4b | 97 | set-group-ID programs.) |
fea681da MK |
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 | |
0daa9e92 | 104 | .I /etc/ld.so.cache |
fea681da MK |
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 | |
c13182ef MK |
111 | .I /lib |
112 | and | |
113 | .I /usr/lib | |
fea681da MK |
114 | are searched (in that order). |
115 | .PP | |
43151de3 MK |
116 | If the object specified by |
117 | .I filename | |
118 | has dependencies on other shared objects, | |
fea681da | 119 | then these are also automatically loaded by the dynamic linker |
c13182ef MK |
120 | using the same rules. |
121 | (This process may occur recursively, | |
43151de3 | 122 | if those objects in turn have dependencies, and so on.) |
fea681da | 123 | .PP |
336e88f0 | 124 | One of the following two values must be included in |
cf2789f1 | 125 | .IR flags : |
336e88f0 | 126 | .TP |
fea681da | 127 | .B RTLD_LAZY |
c13182ef | 128 | Perform lazy binding. |
a3a11667 | 129 | Only resolve symbols as the code that references them is executed. |
336e88f0 | 130 | If the symbol is never referenced, then it is never resolved. |
33a0ccb2 | 131 | (Lazy binding is performed only for function references; |
336e88f0 | 132 | references to variables are always immediately bound when |
43151de3 | 133 | the shared object is loaded.) |
336e88f0 | 134 | .TP |
fea681da | 135 | .B RTLD_NOW |
336e88f0 | 136 | If this value is specified, or the environment variable |
fea681da | 137 | .B LD_BIND_NOW |
aa796481 | 138 | is set to a nonempty string, |
43151de3 | 139 | all undefined symbols in the shared object are resolved before |
d355f1ed | 140 | .BR dlopen () |
c13182ef MK |
141 | returns. |
142 | If this cannot be done, an error is returned. | |
fea681da | 143 | .PP |
dfacdd0a | 144 | Zero or more of the following values may also be ORed in |
cf2789f1 | 145 | .IR flags : |
336e88f0 | 146 | .TP |
fea681da | 147 | .B RTLD_GLOBAL |
43151de3 | 148 | The symbols defined by this shared object will be |
fe854f15 | 149 | made available for symbol resolution of subsequently loaded shared objects. |
c13182ef | 150 | .TP |
336e88f0 | 151 | .B RTLD_LOCAL |
c13182ef | 152 | This is the converse of |
336e88f0 MK |
153 | .BR RTLD_GLOBAL , |
154 | and the default if neither flag is specified. | |
43151de3 MK |
155 | Symbols defined in this shared object are not made available to resolve |
156 | references in subsequently loaded shared objects. | |
336e88f0 | 157 | .TP |
c10859eb | 158 | .BR RTLD_NODELETE " (since glibc 2.2)" |
43151de3 | 159 | Do not unload the shared object during |
336e88f0 | 160 | .BR dlclose (). |
43151de3 MK |
161 | Consequently, the object's static variables are not reinitialized |
162 | if the object is reloaded with | |
336e88f0 MK |
163 | .BR dlopen () |
164 | at a later time. | |
336e88f0 | 165 | .TP |
c10859eb | 166 | .BR RTLD_NOLOAD " (since glibc 2.2)" |
43151de3 MK |
167 | Don't load the shared object. |
168 | This can be used to test if the object is already resident | |
336e88f0 | 169 | .RB ( dlopen () |
43151de3 MK |
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 | |
336e88f0 | 172 | that is already loaded. |
43151de3 | 173 | For example, a shared object that was previously loaded with |
336e88f0 | 174 | .B RTLD_LOCAL |
3b777aff | 175 | can be reopened with |
336e88f0 | 176 | .BR RTLD_NOLOAD\ |\ RTLD_GLOBAL . |
336e88f0 | 177 | .\" |
bba06189 MK |
178 | .TP |
179 | .BR RTLD_DEEPBIND " (since glibc 2.3.4)" | |
c13182ef | 180 | .\" Inimitably described by UD in |
a3a11667 | 181 | .\" http://sources.redhat.com/ml/libc-hacker/2004-09/msg00083.html. |
c13182ef | 182 | Place the lookup scope of the symbols in this |
43151de3 MK |
183 | shared object ahead of the global scope. |
184 | This means that a self-contained object will use | |
c13182ef | 185 | its own symbols in preference to global symbols with the same name |
43151de3 | 186 | contained in objects that have already been loaded. |
fea681da MK |
187 | .PP |
188 | If | |
189 | .I filename | |
b437fdd9 | 190 | is NULL, then the returned handle is for the main program. |
fea681da | 191 | When given to |
d355f1ed | 192 | .BR dlsym (), |
fea681da | 193 | this handle causes a search for a symbol in the main program, |
43151de3 MK |
194 | followed by all shared objects loaded at program startup, |
195 | and then all shared objects loaded by | |
d355f1ed | 196 | .BR dlopen () |
fea681da | 197 | with the flag |
a5e0a0e4 | 198 | .BR RTLD_GLOBAL . |
fea681da | 199 | .PP |
43151de3 MK |
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 | |
fea681da MK |
203 | .B RTLD_GLOBAL |
204 | flag. | |
2bc2f479 MK |
205 | If the executable was linked with the flag "\-rdynamic" |
206 | (or, synonymously, "\-\-export\-dynamic"), | |
fea681da | 207 | then the global symbols in the executable will also be used |
43151de3 | 208 | to resolve references in a dynamically loaded shared object. |
fea681da | 209 | .PP |
43151de3 | 210 | If the same shared object is loaded again with |
d355f1ed | 211 | .BR dlopen (), |
43151de3 | 212 | the same object handle is returned. |
e83ed17d | 213 | The dynamic linker maintains reference |
43151de3 | 214 | counts for object handles, so a dynamically loaded shared object is not |
fea681da | 215 | deallocated until |
d355f1ed | 216 | .BR dlclose () |
fea681da | 217 | has been called on it as many times as |
d355f1ed | 218 | .BR dlopen () |
c13182ef | 219 | has succeeded on it. |
c7c01315 | 220 | Any initialization returns (see below) are called just once. |
b6779d1d MK |
221 | However, a subsequent |
222 | .BR dlopen () | |
223 | call that loads the same shared object with | |
fea681da | 224 | .B RTLD_NOW |
9c169754 | 225 | may force symbol resolution for a shared object earlier loaded with |
fea681da MK |
226 | .BR RTLD_LAZY . |
227 | .PP | |
228 | If | |
d355f1ed | 229 | .BR dlopen () |
fea681da | 230 | fails for any reason, it returns NULL. |
9c169754 | 231 | .\" |
efdd68a5 MK |
232 | .SS dlmopen() |
233 | This function performs the same task as | |
234 | .BR dlopen ()\(emthe | |
235 | .I filename | |
236 | and | |
237 | .I flags | |
65175b1d MK |
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, | |
efdd68a5 | 246 | .IR lmid , |
9c169754 MK |
247 | that specifies the link-map list (also referred to as a |
248 | .IR namespace ) | |
249 | in which the shared object should be loaded. | |
efdd68a5 MK |
250 | (By comparison, |
251 | .BR dlopen () | |
9c169754 | 252 | adds the dynamically loaded shared object to the same namespace as |
efdd68a5 MK |
253 | the shared object from which the |
254 | .BR dlopen () | |
255 | call is made.) | |
9c169754 MK |
256 | The |
257 | .I Lmid_t | |
258 | type is an opaque handle that refers to a namespace. | |
efdd68a5 MK |
259 | |
260 | The | |
261 | .I lmid | |
9c169754 | 262 | argument is either the ID of an existing namespace |
efdd68a5 MK |
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 | |
9c169754 MK |
270 | Load the shared object in the initial namespace |
271 | (i.e., the application's namespace). | |
efdd68a5 MK |
272 | .TP |
273 | .B LM_ID_NEWLM | |
9c169754 | 274 | Create a new namespace and load the shared object in that namespace. |
982eac0d | 275 | The object must have been correctly linked |
9c169754 MK |
276 | to reference all of the other shared objects that it requires, |
277 | since the new namespace is initially empty. | |
65175b1d | 278 | .PP |
65175b1d | 279 | If |
438168a2 | 280 | .I filename |
65175b1d MK |
281 | is NULL, then the only permitted value for |
282 | .I lmid | |
283 | is | |
284 | .BR LM_ID_BASE . | |
73d8cece | 285 | .SS dlclose() |
fea681da | 286 | The function |
d355f1ed | 287 | .BR dlclose () |
43151de3 MK |
288 | decrements the reference count on the |
289 | dynamically loaded shared object referred to by | |
fea681da | 290 | .IR handle . |
4a1af09b | 291 | If the reference count drops to zero, |
43151de3 | 292 | then the object is unloaded. |
cc2ddf2f MK |
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. | |
747b2b0a MK |
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. | |
982eac0d MK |
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, | |
d355f1ed | 322 | .BR dlclose () |
9ebcbe13 | 323 | returns 0; on error, it returns a nonzero value. |
982eac0d MK |
324 | |
325 | Errors from these functions can be diagnosed using | |
326 | .BR dlerror (3). | |
6445992d | 327 | .SH VERSIONS |
374b34d3 | 328 | .BR dlopen () |
6445992d | 329 | and |
374b34d3 | 330 | .BR dlclose () |
6445992d | 331 | are present in glibc 2.0 and later. |
efdd68a5 MK |
332 | .BR dlmopen () |
333 | first appeared in glibc 2.3.4. | |
b23510e5 MK |
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 | |
47297adb | 348 | .SH CONFORMING TO |
2b2581ee | 349 | POSIX.1-2001 describes |
374b34d3 | 350 | .BR dlclose () |
2b2581ee | 351 | and |
896c71d0 | 352 | .BR dlopen (). |
3f4c09d0 | 353 | The |
efdd68a5 | 354 | .BR dlmopen () |
896c71d0 | 355 | function is a GNU extension. |
e8a1758c MK |
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. | |
2b2581ee | 364 | .SH NOTES |
9c169754 MK |
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 | |
43151de3 | 388 | loaded shared object available to (a subset of) other shared objects |
9c169754 | 389 | without exposing those symbols to the entire application. |
982eac0d MK |
390 | This can be achieved by using a separate namespace and the |
391 | .B RTLD_GLOBAL | |
392 | flag. | |
9c169754 MK |
393 | |
394 | Possible uses of | |
395 | .BR dlmopen () | |
396 | are plugins where the author of the plugin-loading framework | |
397 | can't trust the plugin authors and does not wish | |
398 | any undefined symbols from the plugin framework to be resolved to plugin | |
399 | symbols. | |
400 | Another use is to load the same object more than once. | |
401 | Without the use of | |
982eac0d | 402 | .BR dlmopen (), |
9c169754 MK |
403 | this would require the creation of distinct copies of the shared object file. |
404 | Using | |
982eac0d | 405 | .BR dlmopen (), |
9c169754 MK |
406 | this can be achieved by loading the same shared object file into |
407 | different namespaces. | |
982eac0d MK |
408 | |
409 | The glibc implementation supports a maximum of | |
410 | .\" DL_NNS | |
411 | 16 namespaces. | |
9c169754 | 412 | .\" |
c7c01315 MK |
413 | .SS Initialization and finalization functions |
414 | Shared objects may export functions using the | |
415 | .B __attribute__((constructor)) | |
416 | and | |
417 | .B __attribute__((destructor)) | |
418 | function attributes. | |
419 | Constructor functions are executed before | |
420 | .BR dlopen () | |
421 | returns, and destructor functions are executed before | |
422 | .BR dlclose () | |
423 | returns. | |
424 | A shared object may export multiple constructors and destructors, | |
425 | and priorities can be associated with each function | |
426 | to determine the order in which they are executed. | |
427 | See the | |
428 | .BR gcc | |
429 | info pages (under "Function attributes") | |
430 | .\" info gcc "C Extensions" "Function attributes" | |
431 | for further information. | |
432 | ||
433 | An older method of (partially) achieving the same result is via the use of | |
434 | two special symbols recognized by the linker: | |
e8290357 MK |
435 | .B _init |
436 | and | |
437 | .BR _fini . | |
438 | If a dynamically loaded shared object exports a routine named | |
439 | .BR _init (), | |
c7c01315 | 440 | then that code is executed after loading a shared object, before |
e8290357 MK |
441 | .BR dlopen () |
442 | returns. | |
443 | If the shared object exports a routine named | |
444 | .BR _fini (), | |
445 | then that routine is called just before the object is unloaded. | |
c7c01315 MK |
446 | In this case, one must avoid linking against the system startup files, |
447 | which contain default versions of these files; | |
e8290357 MK |
448 | this can be done by using the |
449 | .BR gcc (1) | |
450 | .I \-nostartfiles | |
451 | command-line option. | |
452 | .LP | |
c7c01315 MK |
453 | Use of |
454 | .B _init | |
e8290357 | 455 | and |
c7c01315 MK |
456 | .BR _fini |
457 | is now deprecated in favor of the aforementioned | |
458 | constructors and destructors, | |
459 | which among other advantages, | |
460 | permit multiple initialization and finalization functions to be defined. | |
461 | .\" | |
462 | .\" Using these routines, or the gcc | |
463 | .\" .B \-nostartfiles | |
464 | .\" or | |
465 | .\" .B \-nostdlib | |
466 | .\" options, is not recommended. | |
467 | .\" Their use may result in undesired behavior, | |
468 | .\" since the constructor/destructor routines will not be executed | |
469 | .\" (unless special measures are taken). | |
470 | .\" .\" void _init(void) __attribute__((constructor)); | |
471 | .\" .\" void _fini(void) __attribute__((destructor)); | |
e8290357 | 472 | .\" |
b198e0ae MK |
473 | |
474 | Since glibc 2.2.3, | |
475 | .BR atexit (3) | |
476 | can be used to register an exit handler that is automatically | |
477 | called when a shared object is unloaded. | |
2b2581ee | 478 | .SS History |
896c71d0 | 479 | These functions are part of the dlopen API, derived from SunOS. |
fea681da | 480 | .SH EXAMPLE |
9ff08aad | 481 | Load the math library, and print the cosine of 2.0: |
fea681da | 482 | .nf |
9ff08aad | 483 | |
fea681da | 484 | #include <stdio.h> |
b28f7a67 | 485 | #include <stdlib.h> |
fea681da MK |
486 | #include <dlfcn.h> |
487 | ||
c13182ef MK |
488 | int |
489 | main(int argc, char **argv) | |
cf0a9ace | 490 | { |
fea681da MK |
491 | void *handle; |
492 | double (*cosine)(double); | |
493 | char *error; | |
494 | ||
cf0a9ace | 495 | handle = dlopen("libm.so", RTLD_LAZY); |
fea681da | 496 | if (!handle) { |
31a6818e | 497 | fprintf(stderr, "%s\en", dlerror()); |
4c44ffe5 | 498 | exit(EXIT_FAILURE); |
fea681da MK |
499 | } |
500 | ||
501 | dlerror(); /* Clear any existing error */ | |
c73d7281 | 502 | |
29965345 | 503 | cosine = (double (*)(double)) dlsym(handle, "cos"); |
c73d7281 | 504 | |
daf28a2b | 505 | /* According to the ISO C standard, casting between function |
4318a7d0 MK |
506 | pointers and 'void *', as done above, produces undefined results. |
507 | POSIX.1-2003 and POSIX.1-2008 accepted this state of affairs and | |
508 | proposed the following workaround: | |
29965345 MK |
509 | |
510 | *(void **) (&cosine) = dlsym(handle, "cos"); | |
511 | ||
4318a7d0 MK |
512 | This (clumsy) cast conforms with the ISO C standard and will |
513 | avoid any compiler warnings. | |
29965345 | 514 | |
daf28a2b MK |
515 | The 2013 Technical Corrigendum to POSIX.1-2008 (a.k.a. |
516 | POSIX.1-2013) improved matters by requiring that conforming | |
517 | implementations support casting 'void *' to a function pointer. | |
518 | Nevertheless, some compilers (e.g., gcc with the '-pedantic' | |
519 | option) may complain about the cast used in this program. */ | |
520 | .\" http://pubs.opengroup.org/onlinepubs/009695399/functions/dlsym.html#tag_03_112_08 | |
521 | .\" http://pubs.opengroup.org/onlinepubs/9699919799/functions/dlsym.html#tag_16_96_07 | |
5e0622ba | 522 | .\" http://austingroupbugs.net/view.php?id=74 |
c73d7281 | 523 | |
aefd6f89 | 524 | error = dlerror(); |
3487d63b | 525 | if (error != NULL) { |
31a6818e | 526 | fprintf(stderr, "%s\en", error); |
4c44ffe5 | 527 | exit(EXIT_FAILURE); |
fea681da MK |
528 | } |
529 | ||
31a6818e | 530 | printf("%f\en", (*cosine)(2.0)); |
fea681da | 531 | dlclose(handle); |
5bc8c34c | 532 | exit(EXIT_SUCCESS); |
fea681da | 533 | } |
fea681da | 534 | .fi |
982eac0d MK |
535 | .SH BUGS |
536 | As at glibc 2.21, specifying the | |
537 | .BR RTLD_GLOBAL | |
538 | flag when calling | |
539 | .BR dlmopen () | |
540 | .\" dlerror(): "invalid mode" | |
541 | generates an error. | |
542 | Furthermore, specifying | |
543 | .BR RTLD_GLOBAL | |
544 | when calling | |
545 | .BR dlopen () | |
546 | results in a program crash | |
547 | .RB ( SIGSEGV ) | |
548 | if the call is made from any object loaded in a | |
549 | namespace other than the initial namespace. | |
47297adb | 550 | .SH SEE ALSO |
fea681da MK |
551 | .BR ld (1), |
552 | .BR ldd (1), | |
5d95b7d8 | 553 | .BR pldd (1), |
fea681da | 554 | .BR dl_iterate_phdr (3), |
6c46d3bc | 555 | .BR dladdr (3), |
374b34d3 | 556 | .BR dlerror (3), |
46db2df1 | 557 | .BR dlinfo (3), |
896c71d0 | 558 | .BR dlsym (3), |
c18ecec9 | 559 | .BR rtld-audit (7), |
fea681da | 560 | .BR ld.so (8), |
173fe7e7 DP |
561 | .BR ldconfig (8) |
562 | ||
7e936c29 | 563 | gcc info pages, ld info pages |