1DLSYM(3) Linux Programmer's Manual DLSYM(3)
2
3
4
6 dlsym, dlvsym - obtain address of a symbol in a shared object or exe‐
7 cutable
8
10 #include <dlfcn.h>
11
12 void *dlsym(void *restrict handle, const char *restrict symbol);
13
14 #define _GNU_SOURCE
15 #include <dlfcn.h>
16
17 void *dlvsym(void *restrict handle, const char *restrict symbol,
18 const char *restrict version);
19
20 Link with -ldl.
21
23 The function dlsym() takes a "handle" of a dynamic loaded shared object
24 returned by dlopen(3) along with a null-terminated symbol name, and re‐
25 turns the address where that symbol is loaded into memory. If the sym‐
26 bol is not found, in the specified object or any of the shared objects
27 that were automatically loaded by dlopen(3) when that object was
28 loaded, dlsym() returns NULL. (The search performed by dlsym() is
29 breadth first through the dependency tree of these shared objects.)
30
31 In unusual cases (see NOTES) the value of the symbol could actually be
32 NULL. Therefore, a NULL return from dlsym() need not indicate an er‐
33 ror. The correct way to distinguish an error from a symbol whose value
34 is NULL is to call dlerror(3) to clear any old error conditions, then
35 call dlsym(), and then call dlerror(3) again, saving its return value
36 into a variable, and check whether this saved value is not NULL.
37
38 There are two special pseudo-handles that may be specified in handle:
39
40 RTLD_DEFAULT
41 Find the first occurrence of the desired symbol using the de‐
42 fault shared object search order. The search will include
43 global symbols in the executable and its dependencies, as well
44 as symbols in shared objects that were dynamically loaded with
45 the RTLD_GLOBAL flag.
46
47 RTLD_NEXT
48 Find the next occurrence of the desired symbol in the search or‐
49 der after the current object. This allows one to provide a
50 wrapper around a function in another shared object, so that, for
51 example, the definition of a function in a preloaded shared ob‐
52 ject (see LD_PRELOAD in ld.so(8)) can find and invoke the "real"
53 function provided in another shared object (or for that matter,
54 the "next" definition of the function in cases where there are
55 multiple layers of preloading).
56
57 The _GNU_SOURCE feature test macro must be defined in order to obtain
58 the definitions of RTLD_DEFAULT and RTLD_NEXT from <dlfcn.h>.
59
60 The function dlvsym() does the same as dlsym() but takes a version
61 string as an additional argument.
62
64 On success, these functions return the address associated with symbol.
65 On failure, they return NULL; the cause of the error can be diagnosed
66 using dlerror(3).
67
69 dlsym() is present in glibc 2.0 and later. dlvsym() first appeared in
70 glibc 2.1.
71
73 For an explanation of the terms used in this section, see at‐
74 tributes(7).
75
76 ┌────────────────────────────────────────────┬───────────────┬─────────┐
77 │Interface │ Attribute │ Value │
78 ├────────────────────────────────────────────┼───────────────┼─────────┤
79 │dlsym(), dlvsym() │ Thread safety │ MT-Safe │
80 └────────────────────────────────────────────┴───────────────┴─────────┘
81
83 POSIX.1-2001 describes dlsym(). The dlvsym() function is a GNU exten‐
84 sion.
85
87 There are several scenarios when the address of a global symbol is
88 NULL. For example, a symbol can be placed at zero address by the link‐
89 er, via a linker script or with --defsym command-line option. Unde‐
90 fined weak symbols also have NULL value. Finally, the symbol value may
91 be the result of a GNU indirect function (IFUNC) resolver function that
92 returns NULL as the resolved value. In the latter case, dlsym() also
93 returns NULL without error. However, in the former two cases, the be‐
94 havior of GNU dynamic linker is inconsistent: relocation processing
95 succeeds and the symbol can be observed to have NULL value, but dlsym()
96 fails and dlerror() indicates a lookup error.
97
98 History
99 The dlsym() function is part of the dlopen API, derived from SunOS.
100 That system does not have dlvsym().
101
103 See dlopen(3).
104
106 dl_iterate_phdr(3), dladdr(3), dlerror(3), dlinfo(3), dlopen(3),
107 ld.so(8)
108
110 This page is part of release 5.13 of the Linux man-pages project. A
111 description of the project, information about reporting bugs, and the
112 latest version of this page, can be found at
113 https://www.kernel.org/doc/man-pages/.
114
115
116
117Linux 2021-03-22 DLSYM(3)