mbrtowc.3: SYNOPSIS: Use 'restrict' in prototypes
[man-pages.git] / man3 / dlsym.3
blobd6a73fb5b573dfe8de56b4d89c7fc5567d635df2
1 .\" Copyright 1995 Yggdrasil Computing, Incorporated.
2 .\" and Copyright 2003, 2015 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
9 .\"
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
14 .\"
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 .\" GNU General Public License for more details.
19 .\"
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
23 .\" %%%LICENSE_END
24 .\"
25 .TH DLSYM 3 2020-06-09 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 dlsym, dlvsym \- obtain address of a symbol in a shared object or executable
28 .SH SYNOPSIS
29 .nf
30 .B #include <dlfcn.h>
31 .PP
32 .BI "void *dlsym(void *restrict " handle ", const char *restrict " symbol );
33 .PP
34 .B #define _GNU_SOURCE
35 .B #include <dlfcn.h>
36 .PP
37 .BI "void *dlvsym(void *restrict " handle ", const char *restrict " symbol ,
38 .BI "             const char *restrict " version );
39 .PP
40 Link with \fI\-ldl\fP.
41 .fi
42 .SH DESCRIPTION
43 The function
44 .BR dlsym ()
45 takes a "handle" of a dynamic loaded shared object returned by
46 .BR dlopen (3)
47 along with a null-terminated symbol name,
48 and returns the address where that symbol is
49 loaded into memory.
50 If the symbol is not found, in the specified
51 object or any of the shared objects that were automatically loaded by
52 .BR dlopen (3)
53 when that object was loaded,
54 .BR dlsym ()
55 returns NULL.
56 (The search performed by
57 .BR dlsym ()
58 is breadth first through the dependency tree of these shared objects.)
59 .PP
60 In unusual cases (see NOTES) the value of the symbol could actually be NULL.
61 Therefore, a NULL return from
62 .BR dlsym ()
63 need not indicate an error.
64 The correct way to distinguish an error from a symbol whose value is NULL
65 is to call
66 .BR dlerror (3)
67 to clear any old error conditions, then call
68 .BR dlsym (),
69 and then call
70 .BR dlerror (3)
71 again, saving its return value into a variable, and check whether
72 this saved value is not NULL.
73 .PP
74 There are two special pseudo-handles that may be specified in
75 .IR handle :
76 .TP
77 .B RTLD_DEFAULT
78 Find the first occurrence of the desired symbol
79 using the default shared object search order.
80 The search will include global symbols in the executable
81 and its dependencies,
82 as well as symbols in shared objects that were dynamically loaded with the
83 .BR RTLD_GLOBAL
84 flag.
85 .TP
86 .BR RTLD_NEXT
87 Find the next occurrence of the desired symbol in the search order
88 after the current object.
89 This allows one to provide a wrapper
90 around a function in another shared object, so that, for example,
91 the definition of a function in a preloaded shared object
92 (see
93 .B LD_PRELOAD
95 .BR ld.so (8))
96 can find and invoke the "real" function provided in another shared object
97 (or for that matter, the "next" definition of the function in cases
98 where there are multiple layers of preloading).
99 .PP
101 .B _GNU_SOURCE
102 feature test macro must be defined in order to obtain the
103 definitions of
104 .B RTLD_DEFAULT
106 .B RTLD_NEXT
107 from
108 .IR <dlfcn.h> .
110 The function
111 .BR dlvsym ()
112 does the same as
113 .BR dlsym ()
114 but takes a version string as an additional argument.
115 .SH RETURN VALUE
116 On success,
117 these functions return the address associated with
118 .IR symbol .
119 On failure, they return NULL;
120 the cause of the error can be diagnosed using
121 .BR dlerror (3).
122 .SH VERSIONS
123 .BR dlsym ()
124 is present in glibc 2.0 and later.
125 .BR dlvsym ()
126 first appeared in glibc 2.1.
127 .SH ATTRIBUTES
128 For an explanation of the terms used in this section, see
129 .BR attributes (7).
130 .ad l
133 allbox;
134 lbx lb lb
135 l l l.
136 Interface       Attribute       Value
138 .BR dlsym (),
139 .BR dlvsym ()
140 T}      Thread safety   MT-Safe
144 .sp 1
145 .SH CONFORMING TO
146 POSIX.1-2001 describes
147 .BR dlsym ().
149 .BR dlvsym ()
150 function is a GNU extension.
151 .SH NOTES
152 There are several scenarios when the address of a global symbol is NULL.
153 For example, a symbol can be placed at zero address by the linker, via
154 a linker script or with
155 .I \-\-defsym
156 command-line option. Undefined weak symbols also have NULL value.
157 Finally, the symbol value may be the result of
158 a GNU indirect function (IFUNC) resolver function that returns
159 NULL as the resolved value. In the latter case,
160 .BR dlsym ()
161 also returns NULL without error. However, in the former two cases, the
162 behavior of GNU dynamic linker is inconsistent: relocation processing
163 succeeds and the symbol can be observed to have NULL value, but
164 .BR dlsym ()
165 fails and
166 .BR dlerror ()
167 indicates a lookup error.
169 .SS History
171 .BR dlsym ()
172 function is part of the dlopen API, derived from SunOS.
173 That system does not have
174 .BR dlvsym ().
175 .SH EXAMPLES
177 .BR dlopen (3).
178 .SH SEE ALSO
179 .BR dl_iterate_phdr (3),
180 .BR dladdr (3),
181 .BR dlerror (3),
182 .BR dlinfo (3),
183 .BR dlopen (3),
184 .BR ld.so (8)