/* Return error detail for failing <dlfcn.h> functions.
- Copyright (C) 1995-2000,2002,2003,2004 Free Software Foundation, Inc.
+ Copyright (C) 1995-2021 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
- License along with the GNU C Library; if not, write to the Free
- Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
- 02111-1307 USA. */
+ License along with the GNU C Library; if not, see
+ <https://www.gnu.org/licenses/>. */
#include <dlfcn.h>
#include <libintl.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <bits/libc-lock.h>
+#include <libc-lock.h>
#include <ldsodefs.h>
+#include <libc-symbols.h>
-#if !defined SHARED && defined IS_IN_libdl
+#if !defined SHARED && IS_IN (libdl)
char *
dlerror (void)
{
int errcode;
int returned;
+ bool malloced;
const char *objname;
const char *errstring;
};
struct dl_action_result *result;
# ifdef SHARED
- if (__builtin_expect (_dlfcn_hook != NULL, 0))
+ if (!rtld_active ())
return _dlfcn_hook->dlerror ();
# endif
__libc_once (once, init);
/* Get error string. */
- result = (struct dl_action_result *) __libc_getspecific (key);
- if (result == NULL)
- result = &last_result;
+ if (static_buf != NULL)
+ result = static_buf;
+ else
+ {
+ /* init () has been run and we don't use the static buffer.
+ So we have a valid key. */
+ result = (struct dl_action_result *) __libc_getspecific (key);
+ if (result == NULL)
+ result = &last_result;
+ }
/* Test whether we already returned the string. */
if (result->returned != 0)
# endif
int
-internal_function
_dlerror_run (void (*operate) (void *), void *args)
{
struct dl_action_result *result;
{
/* Free the error string from the last failed command. This can
happen if `dlerror' was not run after an error was found. */
- if (strcmp (result->errstring, "out of memory") != 0)
+ if (result->malloced)
free ((char *) result->errstring);
result->errstring = NULL;
}
- result->errcode = GLRO(dl_catch_error) (&result->objname, &result->errstring,
- operate, args);
+ result->errcode = _dl_catch_error (&result->objname, &result->errstring,
+ &result->malloced, operate, args);
/* If no error we mark that no error string is available. */
result->returned = result->errstring == NULL;
static_buf = &last_result;
}
+
+static void
+check_free (struct dl_action_result *rec)
+{
+ if (rec->errstring != NULL
+ && strcmp (rec->errstring, "out of memory") != 0)
+ {
+ /* We can free the string only if the allocation happened in the
+ C library used by the dynamic linker. This means, it is
+ always the C library in the base namespace. When we're statically
+ linked, the dynamic linker is part of the program and so always
+ uses the same C library we use here. */
+#ifdef SHARED
+ struct link_map *map = NULL;
+ Dl_info info;
+ if (_dl_addr (check_free, &info, &map, NULL) != 0 && map->l_ns == 0)
+#endif
+ {
+ free ((char *) rec->errstring);
+ rec->errstring = NULL;
+ }
+ }
+}
+
+
static void
__attribute__ ((destructor))
fini (void)
{
- if (last_result.errstring != NULL
- && strcmp (last_result.errstring, "out of memory") != 0)
- free ((char *) last_result.errstring);
+ check_free (&last_result);
}
static void
free_key_mem (void *mem)
{
- struct dl_action_result *result = (struct dl_action_result *) mem;
-
- if (result->errstring != NULL
- && strcmp (result->errstring, "out of memory") != 0)
- free ((char *) result->errstring);
+ check_free ((struct dl_action_result *) mem);
free (mem);
__libc_setspecific (key, NULL);
# ifdef SHARED
+/* Free the dlerror-related resources. */
+void
+__dlerror_main_freeres (void)
+{
+ /* Free the global memory if used. */
+ check_free (&last_result);
+
+ if (__libc_once_get (once) && static_buf == NULL)
+ {
+ /* init () has been run and we don't use the static buffer.
+ So we have a valid key. */
+ void *mem;
+ /* Free the TSD memory if used. */
+ mem = __libc_getspecific (key);
+ if (mem != NULL)
+ free_key_mem (mem);
+ }
+}
+
struct dlfcn_hook *_dlfcn_hook __attribute__((nocommon));
libdl_hidden_data_def (_dlfcn_hook)