diff options
| author | Florian Weimer <fweimer@redhat.com> | 2017-08-10 13:40:22 +0200 |
|---|---|---|
| committer | Florian Weimer <fweimer@redhat.com> | 2017-08-10 16:54:57 +0200 |
| commit | 2449ae7b2da24c9940962304a3e44bc80e389265 (patch) | |
| tree | c2cfdcfc3a90731d2da26dda79984eda95e9079e | |
| parent | f87cc2bfba9b844da48a63441c6099342b1551c7 (diff) | |
| download | glibc-2449ae7b2da24c9940962304a3e44bc80e389265.tar.xz glibc-2449ae7b2da24c9940962304a3e44bc80e389265.zip | |
ld.so: Introduce struct dl_exception
This commit separates allocating and raising exceptions. This
simplifies catching and re-raising them because it is no longer
necessary to make a temporary, on-stack copy of the exception message.
29 files changed, 529 insertions, 239 deletions
@@ -1,5 +1,65 @@ 2017-08-10 Florian Weimer <fweimer@redhat.com> + Introduce ld.so exceptions. + * sysdeps/generic/ldsodefs.h (struct dl_exception): Define. + (_dl_exception_create, _dl_exception_create_format) + (_dl_exception_free, _dl_signal_exception, _dl_signal_cexception) + (_dl_catch_exception): Declare. + (_dl_catch_error): Update comment. + * elf/dl-error-skeleton.c (struct catch): Replace objname, + errstring, malloced members with exception member. + (_dl_out_of_memory): Remove. + (fatal_error): New function, extracted from _dl_signal_error. + (_dl_signal_exception, _dl_signal_cexception): New functions. + (_dl_signal_error): Call _dl_exception_create to allocate an + exception object. + (_dl_catch_exception): New function, based on _dl_catch_error. + (_dl_catch_error): Implement using _dl_catch_exception. + * elf/dl-exception.c: New file. + * elf/Makefile (dl-routines): Add dl-exception. + (elide-routines.os): Likewise. + * elf/Version (ld/GLIBC_PRIVATE): Add _dl_exception_create, + _dl_exception_create_format, _dl_exception_free. + * elf/dl-deps.c (_dl_map_object_deps): Use _dl_catch_exception and + _dl_signal_exception. + * elf/dl-lookup.c (make_string): Remove. + (_dl_lookup_symbol_x): Use _dl_exception_create_format, + _dl_signal_cexception, _dl_exception_free. + * elf/dl-open.c (_dl_open): Use _dl_catch_exception and + _dl_signal_exception. + * elf/dl-sym.c (do_sym): Likewise. + * elf/dl-version.c (make_string): Remove. + (match_symbol): Use _dl_exception_create_format, + _dl_signal_cexception, _dl_exception_free. + (_dl_check_map_versions): Likewise. + * sysdeps/generic/localplt.data (ld.so): Add _dl_signal_exception, + _dl_catch_exception. + * sysdeps/unix/sysv/linux/aarch64/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/alpha/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/arm/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/hppa/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/i386/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/ia64/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/m68k/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/microblaze/localplt.data (ld.so): + Likewise. + * sysdeps/unix/sysv/linux/nios2/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/localplt.data + (ld.so): Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/localplt.data + (ld.so): Likewise. + * sysdeps/unix/sysv/linux/powerpc/powerpc64/localplt.data (ld.so): + Likewise. + * sysdeps/unix/sysv/linux/s390/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/sh/localplt.data (ld.so): Likewise. + * sysdeps/unix/sysv/linux/sparc/sparc32/localplt.data (ld.so): + Likewise. + * sysdeps/unix/sysv/linux/sparc/sparc64/localplt.data (ld.so): + Likewise. + * sysdeps/x86_64/localplt.data (ld.so): Likewise. + +2017-08-10 Florian Weimer <fweimer@redhat.com> + * inet/net-internal.h (__inet6_scopeid_pton): Remove attribute_hidden, internal_function. * inet/inet6_scopeid_pton.c (__inet6_scopeid_pton): Remove diff --git a/elf/Makefile b/elf/Makefile index b54ebf8a98..d314a5fa7e 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -31,7 +31,8 @@ routines = $(all-dl-routines) dl-support dl-iteratephdr \ dl-routines = $(addprefix dl-,load lookup object reloc deps hwcaps \ runtime init fini debug misc \ version profile tls origin scope \ - execstack caller open close trampoline) + execstack caller open close trampoline \ + exception) ifeq (yes,$(use-ldconfig)) dl-routines += dl-cache endif @@ -51,7 +52,7 @@ endif all-dl-routines = $(dl-routines) $(sysdep-dl-routines) # But they are absent from the shared libc, because that code is in ld.so. elide-routines.os = $(all-dl-routines) dl-support enbl-secure dl-origin \ - dl-sysdep + dl-sysdep dl-exception shared-only-routines += dl-caller # ld.so uses those routines, plus some special stuff for being the program diff --git a/elf/Versions b/elf/Versions index e65f2fac20..79ffaf73d2 100644 --- a/elf/Versions +++ b/elf/Versions @@ -28,6 +28,7 @@ libc { __libc_dlclose; __libc_dlopen_mode; __libc_dlsym; # Internal error handling support. Interposes the functions in ld.so. + _dl_signal_exception; _dl_catch_exception; _dl_signal_error; _dl_catch_error; } } @@ -68,7 +69,11 @@ ld { # Pointer protection. __pointer_chk_guard; + # Internal error handling support. + _dl_exception_create; _dl_exception_create_format; _dl_exception_free; + # Internal error handling support. Interposed by libc.so. + _dl_signal_exception; _dl_catch_exception; _dl_signal_error; _dl_catch_error; # Set value of a tunable. diff --git a/elf/dl-deps.c b/elf/dl-deps.c index 1b8bac6593..7c82d42be9 100644 --- a/elf/dl-deps.c +++ b/elf/dl-deps.c @@ -165,8 +165,7 @@ _dl_map_object_deps (struct link_map *map, const char *name; int errno_saved; int errno_reason; - const char *errstring; - const char *objname; + struct dl_exception exception; /* No loaded object so far. */ nlist = 0; @@ -200,7 +199,6 @@ _dl_map_object_deps (struct link_map *map, alloca means we cannot use recursive function calls. */ errno_saved = errno; errno_reason = 0; - errstring = NULL; errno = 0; name = NULL; for (runp = known; runp; ) @@ -250,17 +248,9 @@ _dl_map_object_deps (struct link_map *map, /* Store the tag in the argument structure. */ args.name = name; - bool malloced; - int err = _dl_catch_error (&objname, &errstring, &malloced, - openaux, &args); - if (__glibc_unlikely (errstring != NULL)) + int err = _dl_catch_exception (&exception, openaux, &args); + if (__glibc_unlikely (exception.errstring != NULL)) { - char *new_errstring = strdupa (errstring); - objname = strdupa (objname); - if (malloced) - free ((char *) errstring); - errstring = new_errstring; - if (err) errno_reason = err; else @@ -313,31 +303,18 @@ _dl_map_object_deps (struct link_map *map, /* We must be prepared that the addressed shared object is not available. For filter objects the dependency must be available. */ - bool malloced; - int err = _dl_catch_error (&objname, &errstring, &malloced, - openaux, &args); - - if (__glibc_unlikely (errstring != NULL)) + int err = _dl_catch_exception (&exception, openaux, &args); + if (__glibc_unlikely (exception.errstring != NULL)) { if (d->d_tag == DT_AUXILIARY) { /* We are not interested in the error message. */ - assert (errstring != NULL); - if (malloced) - free ((char *) errstring); - + _dl_exception_free (&exception); /* Simply ignore this error and continue the work. */ continue; } else { - - char *new_errstring = strdupa (errstring); - objname = strdupa (objname); - if (malloced) - free ((char *) errstring); - errstring = new_errstring; - if (err) errno_reason = err; else @@ -683,6 +660,6 @@ Filters not supported with LD_TRACE_PRELINKING")); _dl_scope_free (old_l_initfini); if (errno_reason) - _dl_signal_error (errno_reason == -1 ? 0 : errno_reason, objname, - NULL, errstring); + _dl_signal_exception (errno_reason == -1 ? 0 : errno_reason, + &exception, NULL); } diff --git a/elf/dl-error-skeleton.c b/elf/dl-error-skeleton.c index 8e5888d4bd..8de6c87abf 100644 --- a/elf/dl-error-skeleton.c +++ b/elf/dl-error-skeleton.c @@ -39,10 +39,7 @@ _dl_signal_error. */ struct catch { - const char **objname; /* Object/File name. */ - const char **errstring; /* Error detail filled in here. */ - bool *malloced; /* Nonzero if the string is malloced - by the libc malloc. */ + struct dl_exception *exception; /* The exception data is stored there. */ volatile int *errcode; /* Return value of _dl_signal_error. */ jmp_buf env; /* longjmp here on error. */ }; @@ -60,11 +57,6 @@ static __thread struct catch *catch_hook attribute_tls_model_ie; static struct catch *catch_hook; #endif -/* This message we return as a last resort. We define the string in a - variable since we have to avoid freeing it and so have to enable - a pointer comparison. See below and in dlfcn/dlerror.c. */ -static const char _dl_out_of_memory[] = "out of memory"; - #if DL_ERROR_BOOTSTRAP /* This points to a function which is called when an continuable error is received. Unlike the handling of `catch' this function may return. @@ -76,6 +68,41 @@ static const char _dl_out_of_memory[] = "out of memory"; static receiver_fct receiver; #endif /* DL_ERROR_BOOTSTRAP */ +/* Lossage while resolving the program's own symbols is always fatal. */ +static void +__attribute__ ((noreturn)) +fatal_error (int errcode, const char *objname, const char *occasion, + const char *errstring) +{ + char buffer[1024]; + _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n", + RTLD_PROGNAME, + occasion ?: N_("error while loading shared libraries"), + objname, *objname ? ": " : "", + errstring, errcode ? ": " : "", + (errcode + ? __strerror_r (errcode, buffer, sizeof buffer) + : "")); +} + +void +_dl_signal_exception (int errcode, struct dl_exception *exception, + const char *occasion) +{ + struct catch *lcatch = catch_hook; + if (lcatch != NULL) + { + *lcatch->exception = *exception; + *lcatch->errcode = errcode; + + /* We do not restore the signal mask because none was saved. */ + __longjmp (lcatch->env[0].__jmpbuf, 1); + } + else + fatal_error (errcode, exception->objname, occasion, exception->errstring); +} +libc_hidden_def (_dl_signal_exception) + void internal_function _dl_signal_error (int errcode, const char *objname, const char *occation, @@ -86,66 +113,43 @@ _dl_signal_error (int errcode, const char *objname, const char *occation, if (! errstring) errstring = N_("DYNAMIC LINKER BUG!!!"); - if (objname == NULL) - objname = ""; if (lcatch != NULL) { - /* We are inside _dl_catch_error. Return to it. We have to - duplicate the error string since it might be allocated on the - stack. The object name is always a string constant. */ - size_t len_objname = strlen (objname) + 1; - size_t len_errstring = strlen (errstring) + 1; - - char *errstring_copy = malloc (len_objname + len_errstring); - if (errstring_copy != NULL) - { - /* Make a copy of the object file name and the error string. */ - *lcatch->objname = memcpy (__mempcpy (errstring_copy, - errstring, len_errstring), - objname, len_objname); - *lcatch->errstring = errstring_copy; - - /* If the main executable is relocated it means the libc's malloc - is used. */ - bool malloced = true; -#ifdef SHARED - malloced = (GL(dl_ns)[LM_ID_BASE]._ns_loaded != NULL - && (GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_relocated != 0)); -#endif - *lcatch->malloced = malloced; - } - else - { - /* This is better than nothing. */ - *lcatch->objname = ""; - *lcatch->errstring = _dl_out_of_memory; - *lcatch->malloced = false; - } - + _dl_exception_create (lcatch->exception, objname, errstring); *lcatch->errcode = errcode; /* We do not restore the signal mask because none was saved. */ __longjmp (lcatch->env[0].__jmpbuf, 1); } else - { - /* Lossage while resolving the program's own symbols is always fatal. */ - char buffer[1024]; - _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n", - RTLD_PROGNAME, - occation ?: N_("error while loading shared libraries"), - objname, *objname ? ": " : "", - errstring, errcode ? ": " : "", - (errcode - ? __strerror_r (errcode, buffer, sizeof buffer) - : "")); - } + fatal_error (errcode, objname, occation, errstring); } libc_hidden_def (_dl_signal_error) #if DL_ERROR_BOOTSTRAP void +_dl_signal_cexception (int errcode, struct dl_exception *exception, + const char *occasion) +{ + if (__builtin_expect (GLRO(dl_debug_mask) + & ~(DL_DEBUG_STATISTICS|DL_DEBUG_PRELINK), 0)) + _dl_debug_printf ("%s: error: %s: %s (%s)\n", + exception->objname, occasion, + exception->errstring, receiver ? "continued" : "fatal"); + + if (receiver) + { + /* We are inside _dl_receive_error. Call the user supplied + handler and resume the work. The receiver will still be + installed. */ + (*receiver) (errcode, exception->objname, exception->errstring); + } + else + _dl_signal_exception (errcode, exception, occasion); +} + +void internal_function _dl_signal_cerror (int errcode, const char *objname, const char *occation, const char *errstring) @@ -167,11 +171,9 @@ _dl_signal_cerror (int errcode, const char *objname, const char *occation, } #endif /* DL_ERROR_BOOTSTRAP */ - int -internal_function -_dl_catch_error (const char **objname, const char **errstring, - bool *mallocedp, void (*operate) (void *), void *args) +_dl_catch_exception (struct dl_exception *exception, + void (*operate) (void *), void *args) { /* We need not handle `receiver' since setting a `catch' is handled before it. */ @@ -184,9 +186,7 @@ _dl_catch_error (const char **objname, const char **errstring, struct catch c; /* Don't use an initializer since we don't need to clear C.env. */ - c.objname = objname; - c.errstring = errstring; - c.malloced = mallocedp; + c.exception = exception; c.errcode = &errcode; struct catch *const old = catch_hook; @@ -197,17 +197,30 @@ _dl_catch_error (const char **objname, const char **errstring, { (*operate) (args); catch_hook = old; - *objname = NULL; - *errstring = NULL; - *mallocedp = false; + *exception = (struct dl_exception) { NULL }; return 0; } - /* We get here only if we longjmp'd out of OPERATE. _dl_signal_error has - already stored values into *OBJNAME, *ERRSTRING, and *MALLOCEDP. */ + /* We get here only if we longjmp'd out of OPERATE. + _dl_signal_exception has already stored values into + *EXCEPTION. */ catch_hook = old; return errcode; } +libc_hidden_def (_dl_catch_exception) + +int +internal_function +_dl_catch_error (const char **objname, const char **errstring, + bool *mallocedp, void (*operate) (void *), void *args) +{ + struct dl_exception exception; + int errorcode = _dl_catch_exception (&exception, operate, args); + *objname = exception.objname; + *errstring = exception.errstring; + *mallocedp = exception.message_buffer == exception.errstring; + return errorcode; +} libc_hidden_def (_dl_catch_error) #if DL_ERROR_BOOTSTRAP diff --git a/elf/dl-exception.c b/elf/dl-exception.c new file mode 100644 index 0000000000..b4d0ca7578 --- /dev/null +++ b/elf/dl-exception.c @@ -0,0 +1,202 @@ +/* ld.so error exception allocation and deallocation. + Copyright (C) 1995-2017 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 + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more |
