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