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