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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 #include <bits/libc-lock.h>
29 #if !defined SHARED && defined IS_IN_libdl
39 /* Type for storing results of dynamic loading actions. */
40 struct dl_action_result
46 const char *errstring
;
48 static struct dl_action_result last_result
;
49 static struct dl_action_result
*static_buf
;
51 /* This is the key for the thread specific memory. */
52 static __libc_key_t key
;
53 __libc_once_define (static, once
);
55 /* Destructor for the thread-specific data. */
56 static void init (void);
57 static void free_key_mem (void *mem
);
64 struct dl_action_result
*result
;
67 if (__builtin_expect (_dlfcn_hook
!= NULL
, 0))
68 return _dlfcn_hook
->dlerror ();
71 /* If we have not yet initialized the buffer do it now. */
72 __libc_once (once
, init
);
74 /* Get error string. */
75 result
= (struct dl_action_result
*) __libc_getspecific (key
);
77 result
= &last_result
;
79 /* Test whether we already returned the string. */
80 if (result
->returned
!= 0)
82 /* We can now free the string. */
83 if (result
->errstring
!= NULL
)
85 if (strcmp (result
->errstring
, "out of memory") != 0)
86 free ((char *) result
->errstring
);
87 result
->errstring
= NULL
;
90 else if (result
->errstring
!= NULL
)
92 buf
= (char *) result
->errstring
;
94 if (result
->errcode
== 0)
95 n
= __asprintf (&buf
, "%s%s%s",
97 result
->objname
[0] == '\0' ? "" : ": ",
98 _(result
->errstring
));
100 n
= __asprintf (&buf
, "%s%s%s: %s",
102 result
->objname
[0] == '\0' ? "" : ": ",
103 _(result
->errstring
),
104 strerror (result
->errcode
));
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
;
113 /* Mark the error as returned. */
114 result
->returned
= 1;
120 strong_alias (__dlerror
, dlerror
)
125 _dlerror_run (void (*operate
) (void *), void *args
)
127 struct dl_action_result
*result
;
129 /* If we have not yet initialized the buffer do it now. */
130 __libc_once (once
, init
);
132 /* Get error string and number. */
133 if (static_buf
!= NULL
)
137 /* We don't use the static buffer and so we have a key. Use it
138 to get the thread-specific buffer. */
139 result
= __libc_getspecific (key
);
142 result
= (struct dl_action_result
*) calloc (1, sizeof (*result
));
144 /* We are out of memory. Since this is no really critical
145 situation we carry on by using the global variable.
146 This might lead to conflicts between the threads but
147 they soon all will have memory problems. */
148 result
= &last_result
;
151 __libc_setspecific (key
, result
);
155 if (result
->errstring
!= NULL
)
157 /* Free the error string from the last failed command. This can
158 happen if `dlerror' was not run after an error was found. */
159 if (result
->malloced
)
160 free ((char *) result
->errstring
);
161 result
->errstring
= NULL
;
164 result
->errcode
= GLRO(dl_catch_error
) (&result
->objname
, &result
->errstring
,
165 &result
->malloced
, operate
, args
);
167 /* If no error we mark that no error string is available. */
168 result
->returned
= result
->errstring
== NULL
;
170 return result
->errstring
!= NULL
;
174 /* Initialize buffers for results. */
178 if (__libc_key_create (&key
, free_key_mem
))
179 /* Creating the key failed. This means something really went
180 wrong. In any case use a static buffer which is better than
182 static_buf
= &last_result
;
187 check_free (struct dl_action_result
*rec
)
189 if (rec
->errstring
!= NULL
190 && strcmp (rec
->errstring
, "out of memory") != 0)
192 /* We can free the string only if the allocation happened in the
193 C library used by the dynamic linker. This means, it is
194 always the C library in the base namespave. */
195 struct link_map
*map
= NULL
;
197 if (_dl_addr (check_free
, &info
, &map
, NULL
) != 0
198 && map
!= NULL
&& map
->l_ns
== 0)
199 free ((char *) rec
->errstring
);
205 __attribute__ ((destructor
))
208 check_free (&last_result
);
212 /* Free the thread specific data, this is done if a thread terminates. */
214 free_key_mem (void *mem
)
216 check_free ((struct dl_action_result
*) mem
);
219 __libc_setspecific (key
, NULL
);
224 struct dlfcn_hook
*_dlfcn_hook
__attribute__((nocommon
));
225 libdl_hidden_data_def (_dlfcn_hook
)
229 static struct dlfcn_hook _dlfcn_hooks
=
232 .dlclose
= __dlclose
,
235 .dlerror
= __dlerror
,
237 .dladdr1
= __dladdr1
,
243 __libc_register_dlfcn_hook (struct link_map
*map
)
245 struct dlfcn_hook
**hook
;
247 hook
= (struct dlfcn_hook
**) __libc_dlsym_private (map
, "_dlfcn_hook");
249 *hook
= &_dlfcn_hooks
;