1/* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995-2019 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 <http://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
31char *
32dlerror (void)
33{
34 return __dlerror ();
35}
36
37#else
38
39/* Type for storing results of dynamic loading actions. */
40struct dl_action_result
41 {
42 int errcode;
43 int returned;
44 bool malloced;
45 const char *objname;
46 const char *errstring;
47 };
48static struct dl_action_result last_result;
49static struct dl_action_result *static_buf;
50
51/* This is the key for the thread specific memory. */
52static __libc_key_t key;
53__libc_once_define (static, once);
54
55/* Destructor for the thread-specific data. */
56static void init (void);
57static void free_key_mem (void *mem);
58
59
60char *
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 result = (struct dl_action_result *) __libc_getspecific (key);
76 if (result == NULL)
77 result = &last_result;
78
79 /* Test whether we already returned the string. */
80 if (result->returned != 0)
81 {
82 /* We can now free the string. */
83 if (result->errstring != NULL)
84 {
85 if (strcmp (result->errstring, "out of memory") != 0)
86 free ((char *) result->errstring);
87 result->errstring = NULL;
88 }
89 }
90 else if (result->errstring != NULL)
91 {
92 buf = (char *) result->errstring;
93 int n;
94 if (result->errcode == 0)
95 n = __asprintf (&buf, "%s%s%s",
96 result->objname,
97 result->objname[0] == '\0' ? "" : ": ",
98 _(result->errstring));
99 else
100 n = __asprintf (&buf, "%s%s%s: %s",
101 result->objname,
102 result->objname[0] == '\0' ? "" : ": ",
103 _(result->errstring),
104 strerror (result->errcode));
105 if (n != -1)
106 {
107 /* We don't need the error string anymore. */
108 if (strcmp (result->errstring, "out of memory") != 0)
109 free ((char *) result->errstring);
110 result->errstring = buf;
111 }
112
113 /* Mark the error as returned. */
114 result->returned = 1;
115 }
116
117 return buf;
118}
119# ifdef SHARED
120strong_alias (__dlerror, dlerror)
121# endif
122
123int
124_dlerror_run (void (*operate) (void *), void *args)
125{
126 struct dl_action_result *result;
127
128 /* If we have not yet initialized the buffer do it now. */
129 __libc_once (once, init);
130
131 /* Get error string and number. */
132 if (static_buf != NULL)
133 result = static_buf;
134 else
135 {
136 /* We don't use the static buffer and so we have a key. Use it
137 to get the thread-specific buffer. */
138 result = __libc_getspecific (key);
139 if (result == NULL)
140 {
141 result = (struct dl_action_result *) calloc (1, sizeof (*result));
142 if (result == NULL)
143 /* We are out of memory. Since this is no really critical
144 situation we carry on by using the global variable.
145 This might lead to conflicts between the threads but
146 they soon all will have memory problems. */
147 result = &last_result;
148 else
149 /* Set the tsd. */
150 __libc_setspecific (key, result);
151 }
152 }
153
154 if (result->errstring != NULL)
155 {
156 /* Free the error string from the last failed command. This can
157 happen if `dlerror' was not run after an error was found. */
158 if (result->malloced)
159 free ((char *) result->errstring);
160 result->errstring = NULL;
161 }
162
163 result->errcode = _dl_catch_error (&result->objname, &result->errstring,
164 &result->malloced, operate, args);
165
166 /* If no error we mark that no error string is available. */
167 result->returned = result->errstring == NULL;
168
169 return result->errstring != NULL;
170}
171
172
173/* Initialize buffers for results. */
174static void
175init (void)
176{
177 if (__libc_key_create (&key, free_key_mem))
178 /* Creating the key failed. This means something really went
179 wrong. In any case use a static buffer which is better than
180 nothing. */
181 static_buf = &last_result;
182}
183
184
185static void
186check_free (struct dl_action_result *rec)
187{
188 if (rec->errstring != NULL
189 && strcmp (rec->errstring, "out of memory") != 0)
190 {
191 /* We can free the string only if the allocation happened in the
192 C library used by the dynamic linker. This means, it is
193 always the C library in the base namespace. When we're statically
194 linked, the dynamic linker is part of the program and so always
195 uses the same C library we use here. */
196#ifdef SHARED
197 struct link_map *map = NULL;
198 Dl_info info;
199 if (_dl_addr (check_free, &info, &map, NULL) != 0 && map->l_ns == 0)
200#endif
201 {
202 free ((char *) rec->errstring);
203 rec->errstring = NULL;
204 }
205 }
206}
207
208
209static void
210__attribute__ ((destructor))
211fini (void)
212{
213 check_free (&last_result);
214}
215
216
217/* Free the thread specific data, this is done if a thread terminates. */
218static void
219free_key_mem (void *mem)
220{
221 check_free ((struct dl_action_result *) mem);
222
223 free (mem);
224 __libc_setspecific (key, NULL);
225}
226
227# ifdef SHARED
228
229/* Free the dlerror-related resources. */
230void
231__dlerror_main_freeres (void)
232{
233 void *mem;
234 /* Free the global memory if used. */
235 check_free (&last_result);
236 /* Free the TSD memory if used. */
237 mem = __libc_getspecific (key);
238 if (mem != NULL)
239 free_key_mem (mem);
240}
241
242struct dlfcn_hook *_dlfcn_hook __attribute__((nocommon));
243libdl_hidden_data_def (_dlfcn_hook)
244
245# else
246
247static struct dlfcn_hook _dlfcn_hooks =
248 {
249 .dlopen = __dlopen,
250 .dlclose = __dlclose,
251 .dlsym = __dlsym,
252 .dlvsym = __dlvsym,
253 .dlerror = __dlerror,
254 .dladdr = __dladdr,
255 .dladdr1 = __dladdr1,
256 .dlinfo = __dlinfo,
257 .dlmopen = __dlmopen
258 };
259
260void
261__libc_register_dlfcn_hook (struct link_map *map)
262{
263 struct dlfcn_hook **hook;
264
265 hook = (struct dlfcn_hook **) __libc_dlsym_private (map, "_dlfcn_hook");
266 if (hook != NULL)
267 *hook = &_dlfcn_hooks;
268}
269# endif
270#endif
271