]> sourceware.org Git - glibc.git/blob - dlfcn/dlerror.c
947b7c10c65c85cb06f0f86eb0ffca3d7e8247f2
[glibc.git] / dlfcn / dlerror.c
1 /* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <dlfcn.h>
20 #include <libintl.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <libc-lock.h>
26 #include <ldsodefs.h>
27 #include <libc-symbols.h>
28
29 #if !defined SHARED && IS_IN (libdl)
30
31 char *
32 dlerror (void)
33 {
34 return __dlerror ();
35 }
36
37 #else
38
39 /* Type for storing results of dynamic loading actions. */
40 struct dl_action_result
41 {
42 int errcode;
43 int returned;
44 bool malloced;
45 const char *objname;
46 const char *errstring;
47 };
48 static struct dl_action_result last_result;
49 static struct dl_action_result *static_buf;
50
51 /* This is the key for the thread specific memory. */
52 static __libc_key_t key;
53 __libc_once_define (static, once);
54
55 /* Destructor for the thread-specific data. */
56 static void init (void);
57 static void free_key_mem (void *mem);
58
59
60 char *
61 __dlerror (void)
62 {
63 char *buf = NULL;
64 struct dl_action_result *result;
65
66 # ifdef SHARED
67 if (!rtld_active ())
68 return _dlfcn_hook->dlerror ();
69 # endif
70
71 /* If we have not yet initialized the buffer do it now. */
72 __libc_once (once, init);
73
74 /* Get error string. */
75 if (static_buf != NULL)
76 result = static_buf;
77 else
78 {
79 /* init () has been run and we don't use the static buffer.
80 So we have a valid key. */
81 result = (struct dl_action_result *) __libc_getspecific (key);
82 if (result == NULL)
83 result = &last_result;
84 }
85
86 /* Test whether we already returned the string. */
87 if (result->returned != 0)
88 {
89 /* We can now free the string. */
90 if (result->errstring != NULL)
91 {
92 if (strcmp (result->errstring, "out of memory") != 0)
93 free ((char *) result->errstring);
94 result->errstring = NULL;
95 }
96 }
97 else if (result->errstring != NULL)
98 {
99 buf = (char *) result->errstring;
100 int n;
101 if (result->errcode == 0)
102 n = __asprintf (&buf, "%s%s%s",
103 result->objname,
104 result->objname[0] == '\0' ? "" : ": ",
105 _(result->errstring));
106 else
107 n = __asprintf (&buf, "%s%s%s: %s",
108 result->objname,
109 result->objname[0] == '\0' ? "" : ": ",
110 _(result->errstring),
111 strerror (result->errcode));
112 if (n != -1)
113 {
114 /* We don't need the error string anymore. */
115 if (strcmp (result->errstring, "out of memory") != 0)
116 free ((char *) result->errstring);
117 result->errstring = buf;
118 }
119
120 /* Mark the error as returned. */
121 result->returned = 1;
122 }
123
124 return buf;
125 }
126 # ifdef SHARED
127 strong_alias (__dlerror, dlerror)
128 # endif
129
130 int
131 _dlerror_run (void (*operate) (void *), void *args)
132 {
133 struct dl_action_result *result;
134
135 /* If we have not yet initialized the buffer do it now. */
136 __libc_once (once, init);
137
138 /* Get error string and number. */
139 if (static_buf != NULL)
140 result = static_buf;
141 else
142 {
143 /* We don't use the static buffer and so we have a key. Use it
144 to get the thread-specific buffer. */
145 result = __libc_getspecific (key);
146 if (result == NULL)
147 {
148 result = (struct dl_action_result *) calloc (1, sizeof (*result));
149 if (result == NULL)
150 /* We are out of memory. Since this is no really critical
151 situation we carry on by using the global variable.
152 This might lead to conflicts between the threads but
153 they soon all will have memory problems. */
154 result = &last_result;
155 else
156 /* Set the tsd. */
157 __libc_setspecific (key, result);
158 }
159 }
160
161 if (result->errstring != NULL)
162 {
163 /* Free the error string from the last failed command. This can
164 happen if `dlerror' was not run after an error was found. */
165 if (result->malloced)
166 free ((char *) result->errstring);
167 result->errstring = NULL;
168 }
169
170 result->errcode = GLRO (dl_catch_error) (&result->objname,
171 &result->errstring,
172 &result->malloced,
173 operate, args);
174
175 /* If no error we mark that no error string is available. */
176 result->returned = result->errstring == NULL;
177
178 return result->errstring != NULL;
179 }
180
181
182 /* Initialize buffers for results. */
183 static void
184 init (void)
185 {
186 if (__libc_key_create (&key, free_key_mem))
187 /* Creating the key failed. This means something really went
188 wrong. In any case use a static buffer which is better than
189 nothing. */
190 static_buf = &last_result;
191 }
192
193
194 static void
195 check_free (struct dl_action_result *rec)
196 {
197 if (rec->errstring != NULL
198 && strcmp (rec->errstring, "out of memory") != 0)
199 {
200 /* We can free the string only if the allocation happened in the
201 C library used by the dynamic linker. This means, it is
202 always the C library in the base namespace. When we're statically
203 linked, the dynamic linker is part of the program and so always
204 uses the same C library we use here. */
205 #ifdef SHARED
206 struct link_map *map = NULL;
207 Dl_info info;
208 if (_dl_addr (check_free, &info, &map, NULL) != 0 && map->l_ns == 0)
209 #endif
210 {
211 free ((char *) rec->errstring);
212 rec->errstring = NULL;
213 }
214 }
215 }
216
217
218 static void
219 __attribute__ ((destructor))
220 fini (void)
221 {
222 check_free (&last_result);
223 }
224
225
226 /* Free the thread specific data, this is done if a thread terminates. */
227 static void
228 free_key_mem (void *mem)
229 {
230 check_free ((struct dl_action_result *) mem);
231
232 free (mem);
233 __libc_setspecific (key, NULL);
234 }
235
236 # ifdef SHARED
237
238 /* Free the dlerror-related resources. */
239 void
240 __dlerror_main_freeres (void)
241 {
242 /* Free the global memory if used. */
243 check_free (&last_result);
244
245 if (__libc_once_get (once) && static_buf == NULL)
246 {
247 /* init () has been run and we don't use the static buffer.
248 So we have a valid key. */
249 void *mem;
250 /* Free the TSD memory if used. */
251 mem = __libc_getspecific (key);
252 if (mem != NULL)
253 free_key_mem (mem);
254 }
255 }
256
257 struct dlfcn_hook *_dlfcn_hook __attribute__((nocommon));
258 libdl_hidden_data_def (_dlfcn_hook)
259
260 # else
261
262 static struct dlfcn_hook _dlfcn_hooks =
263 {
264 .dlopen = __dlopen,
265 .dlclose = __dlclose,
266 .dlsym = __dlsym,
267 .dlvsym = __dlvsym,
268 .dlerror = __dlerror,
269 .dladdr = __dladdr,
270 .dladdr1 = __dladdr1,
271 .dlinfo = __dlinfo,
272 .dlmopen = __dlmopen
273 };
274
275 void
276 __libc_register_dlfcn_hook (struct link_map *map)
277 {
278 struct dlfcn_hook **hook;
279
280 hook = (struct dlfcn_hook **) __libc_dlsym_private (map, "_dlfcn_hook");
281 if (hook != NULL)
282 *hook = &_dlfcn_hooks;
283 }
284 # endif
285 #endif
This page took 0.0466 seconds and 4 git commands to generate.