1 /* Return error detail for failing <dlfcn.h> functions.
2 Copyright (C) 1995-2000,2002,2003,2004,2005 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
25 #include <bits/libc-lock.h>
28 #if !defined SHARED && defined IS_IN_libdl
38 /* Type for storing results of dynamic loading actions. */
39 struct dl_action_result
45 const char *errstring;
47 static struct dl_action_result last_result;
48 static struct dl_action_result *static_buf;
50 /* This is the key for the thread specific memory. */
51 static __libc_key_t key;
52 __libc_once_define (static, once);
54 /* Destructor for the thread-specific data. */
55 static void init (void);
56 static void free_key_mem (void *mem);
63 struct dl_action_result *result;
66 if (__builtin_expect (_dlfcn_hook != NULL, 0))
67 return _dlfcn_hook->dlerror ();
70 /* If we have not yet initialized the buffer do it now. */
71 __libc_once (once, init);
73 /* Get error string. */
74 result = (struct dl_action_result *) __libc_getspecific (key);
76 result = &last_result;
78 /* Test whether we already returned the string. */
79 if (result->returned != 0)
81 /* We can now free the string. */
82 if (result->errstring != NULL)
84 if (strcmp (result->errstring, "out of memory") != 0)
85 free ((char *) result->errstring);
86 result->errstring = NULL;
89 else if (result->errstring != NULL)
91 buf = (char *) result->errstring;
93 if (result->errcode == 0)
94 n = __asprintf (&buf, "%s%s%s",
96 result->objname[0] == '\0' ? "" : ": ",
97 _(result->errstring));
99 n = __asprintf (&buf, "%s%s%s: %s",
101 result->objname[0] == '\0' ? "" : ": ",
102 _(result->errstring),
103 strerror (result->errcode));
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;
112 /* Mark the error as returned. */
113 result->returned = 1;
119 strong_alias (__dlerror, dlerror)
124 _dlerror_run (void (*operate) (void *), void *args)
126 struct dl_action_result *result;
128 /* If we have not yet initialized the buffer do it now. */
129 __libc_once (once, init);
131 /* Get error string and number. */
132 if (static_buf != NULL)
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);
141 result = (struct dl_action_result *) calloc (1, sizeof (*result));
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;
150 __libc_setspecific (key, result);
154 if (result->errstring != NULL)
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;
163 result->errcode = GLRO(dl_catch_error) (&result->objname, &result->errstring,
164 &result->malloced, operate, args);
166 /* If no error we mark that no error string is available. */
167 result->returned = result->errstring == NULL;
169 return result->errstring != NULL;
173 /* Initialize buffers for results. */
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
181 static_buf = &last_result;
186 check_free (struct dl_action_result *rec)
188 if (rec->errstring != NULL
189 && strcmp (rec->errstring, "out of memory") != 0)
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 namespave. */
194 struct link_map *map = NULL;
196 if (_dl_addr (check_free, &info, &map, NULL) != 0
197 && map != NULL && map->l_ns == 0)
198 free ((char *) rec->errstring);
204 __attribute__ ((destructor))
207 check_free (&last_result);
211 /* Free the thread specific data, this is done if a thread terminates. */
213 free_key_mem (void *mem)
215 check_free ((struct dl_action_result *) mem);
218 __libc_setspecific (key, NULL);
223 struct dlfcn_hook *_dlfcn_hook __attribute__((nocommon));
224 libdl_hidden_data_def (_dlfcn_hook)
228 static struct dlfcn_hook _dlfcn_hooks =
231 .dlclose = __dlclose,
234 .dlerror = __dlerror,
236 .dladdr1 = __dladdr1,
242 __libc_register_dlfcn_hook (struct link_map *map)
244 struct dlfcn_hook **hook;
246 hook = (struct dlfcn_hook **) __libc_dlsym_private (map, "_dlfcn_hook");
248 *hook = &_dlfcn_hooks;