| 1 | /* Template for error handling for runtime dynamic linker. |
| 2 | Copyright (C) 1995-2020 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 | <https://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | /* The following macro needs to be defined before including this |
| 20 | skeleton file: |
| 21 | |
| 22 | DL_ERROR_BOOTSTRAP |
| 23 | |
| 24 | If 1, do not use TLS and implement _dl_signal_cerror and |
| 25 | _dl_receive_error. If 0, TLS is used, and the variants with |
| 26 | error callbacks are not provided. */ |
| 27 | |
| 28 | |
| 29 | #include <libintl.h> |
| 30 | #include <setjmp.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
| 35 | #include <ldsodefs.h> |
| 36 | #include <stdio.h> |
| 37 | |
| 38 | /* This structure communicates state between _dl_catch_error and |
| 39 | _dl_signal_error. */ |
| 40 | struct catch |
| 41 | { |
| 42 | struct dl_exception *exception; /* The exception data is stored there. */ |
| 43 | volatile int *errcode; /* Return value of _dl_signal_error. */ |
| 44 | jmp_buf env; /* longjmp here on error. */ |
| 45 | }; |
| 46 | |
| 47 | /* Multiple threads at once can use the `_dl_catch_error' function. The |
| 48 | calls can come from `_dl_map_object_deps', `_dlerror_run', or from |
| 49 | any of the libc functionality which loads dynamic objects (NSS, iconv). |
| 50 | Therefore we have to be prepared to save the state in thread-local |
| 51 | memory. */ |
| 52 | #if !DL_ERROR_BOOTSTRAP |
| 53 | static __thread struct catch *catch_hook attribute_tls_model_ie; |
| 54 | #else |
| 55 | /* The version of this code in ld.so cannot use thread-local variables |
| 56 | and is used during bootstrap only. */ |
| 57 | static struct catch *catch_hook; |
| 58 | #endif |
| 59 | |
| 60 | #if DL_ERROR_BOOTSTRAP |
| 61 | /* This points to a function which is called when an continuable error is |
| 62 | received. Unlike the handling of `catch' this function may return. |
| 63 | The arguments will be the `errstring' and `objname'. |
| 64 | |
| 65 | Since this functionality is not used in normal programs (only in ld.so) |
| 66 | we do not care about multi-threaded programs here. We keep this as a |
| 67 | global variable. */ |
| 68 | static receiver_fct receiver; |
| 69 | #endif /* DL_ERROR_BOOTSTRAP */ |
| 70 | |
| 71 | /* Lossage while resolving the program's own symbols is always fatal. */ |
| 72 | static void |
| 73 | __attribute__ ((noreturn)) |
| 74 | fatal_error (int errcode, const char *objname, const char *occasion, |
| 75 | const char *errstring) |
| 76 | { |
| 77 | char buffer[1024]; |
| 78 | _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n" , |
| 79 | RTLD_PROGNAME, |
| 80 | occasion ?: N_("error while loading shared libraries" ), |
| 81 | objname, *objname ? ": " : "" , |
| 82 | errstring, errcode ? ": " : "" , |
| 83 | (errcode |
| 84 | ? __strerror_r (errcode, buffer, sizeof buffer) |
| 85 | : "" )); |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | _dl_signal_exception (int errcode, struct dl_exception *exception, |
| 90 | const char *occasion) |
| 91 | { |
| 92 | struct catch *lcatch = catch_hook; |
| 93 | if (lcatch != NULL) |
| 94 | { |
| 95 | *lcatch->exception = *exception; |
| 96 | *lcatch->errcode = errcode; |
| 97 | |
| 98 | /* We do not restore the signal mask because none was saved. */ |
| 99 | __longjmp (lcatch->env[0].__jmpbuf, 1); |
| 100 | } |
| 101 | else |
| 102 | fatal_error (errcode, exception->objname, occasion, exception->errstring); |
| 103 | } |
| 104 | libc_hidden_def (_dl_signal_exception) |
| 105 | |
| 106 | void |
| 107 | _dl_signal_error (int errcode, const char *objname, const char *occation, |
| 108 | const char *errstring) |
| 109 | { |
| 110 | struct catch *lcatch = catch_hook; |
| 111 | |
| 112 | if (! errstring) |
| 113 | errstring = N_("DYNAMIC LINKER BUG!!!" ); |
| 114 | |
| 115 | if (lcatch != NULL) |
| 116 | { |
| 117 | _dl_exception_create (lcatch->exception, objname, errstring); |
| 118 | *lcatch->errcode = errcode; |
| 119 | |
| 120 | /* We do not restore the signal mask because none was saved. */ |
| 121 | __longjmp (lcatch->env[0].__jmpbuf, 1); |
| 122 | } |
| 123 | else |
| 124 | fatal_error (errcode, objname, occation, errstring); |
| 125 | } |
| 126 | libc_hidden_def (_dl_signal_error) |
| 127 | |
| 128 | |
| 129 | #if DL_ERROR_BOOTSTRAP |
| 130 | void |
| 131 | _dl_signal_cexception (int errcode, struct dl_exception *exception, |
| 132 | const char *occasion) |
| 133 | { |
| 134 | if (__builtin_expect (GLRO(dl_debug_mask) |
| 135 | & ~(DL_DEBUG_STATISTICS|DL_DEBUG_PRELINK), 0)) |
| 136 | _dl_debug_printf ("%s: error: %s: %s (%s)\n" , |
| 137 | exception->objname, occasion, |
| 138 | exception->errstring, receiver ? "continued" : "fatal" ); |
| 139 | |
| 140 | if (receiver) |
| 141 | { |
| 142 | /* We are inside _dl_receive_error. Call the user supplied |
| 143 | handler and resume the work. The receiver will still be |
| 144 | installed. */ |
| 145 | (*receiver) (errcode, exception->objname, exception->errstring); |
| 146 | } |
| 147 | else |
| 148 | _dl_signal_exception (errcode, exception, occasion); |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | _dl_signal_cerror (int errcode, const char *objname, const char *occation, |
| 153 | const char *errstring) |
| 154 | { |
| 155 | if (__builtin_expect (GLRO(dl_debug_mask) |
| 156 | & ~(DL_DEBUG_STATISTICS|DL_DEBUG_PRELINK), 0)) |
| 157 | _dl_debug_printf ("%s: error: %s: %s (%s)\n" , objname, occation, |
| 158 | errstring, receiver ? "continued" : "fatal" ); |
| 159 | |
| 160 | if (receiver) |
| 161 | { |
| 162 | /* We are inside _dl_receive_error. Call the user supplied |
| 163 | handler and resume the work. The receiver will still be |
| 164 | installed. */ |
| 165 | (*receiver) (errcode, objname, errstring); |
| 166 | } |
| 167 | else |
| 168 | _dl_signal_error (errcode, objname, occation, errstring); |
| 169 | } |
| 170 | #endif /* DL_ERROR_BOOTSTRAP */ |
| 171 | |
| 172 | int |
| 173 | _dl_catch_exception (struct dl_exception *exception, |
| 174 | void (*operate) (void *), void *args) |
| 175 | { |
| 176 | /* If exception is NULL, temporarily disable exception handling. |
| 177 | Exceptions during operate (args) are fatal. */ |
| 178 | if (exception == NULL) |
| 179 | { |
| 180 | struct catch *const old = catch_hook; |
| 181 | catch_hook = NULL; |
| 182 | operate (args); |
| 183 | /* If we get here, the operation was successful. */ |
| 184 | catch_hook = old; |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /* We need not handle `receiver' since setting a `catch' is handled |
| 189 | before it. */ |
| 190 | |
| 191 | /* Only this needs to be marked volatile, because it is the only local |
| 192 | variable that gets changed between the setjmp invocation and the |
| 193 | longjmp call. All others are just set here (before setjmp) and read |
| 194 | in _dl_signal_error (before longjmp). */ |
| 195 | volatile int errcode; |
| 196 | |
| 197 | struct catch c; |
| 198 | /* Don't use an initializer since we don't need to clear C.env. */ |
| 199 | c.exception = exception; |
| 200 | c.errcode = &errcode; |
| 201 | |
| 202 | struct catch *const old = catch_hook; |
| 203 | catch_hook = &c; |
| 204 | |
| 205 | /* Do not save the signal mask. */ |
| 206 | if (__builtin_expect (__sigsetjmp (c.env, 0), 0) == 0) |
| 207 | { |
| 208 | (*operate) (args); |
| 209 | catch_hook = old; |
| 210 | *exception = (struct dl_exception) { NULL }; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | /* We get here only if we longjmp'd out of OPERATE. |
| 215 | _dl_signal_exception has already stored values into |
| 216 | *EXCEPTION. */ |
| 217 | catch_hook = old; |
| 218 | return errcode; |
| 219 | } |
| 220 | libc_hidden_def (_dl_catch_exception) |
| 221 | |
| 222 | int |
| 223 | _dl_catch_error (const char **objname, const char **errstring, |
| 224 | bool *mallocedp, void (*operate) (void *), void *args) |
| 225 | { |
| 226 | struct dl_exception exception; |
| 227 | int errorcode = _dl_catch_exception (&exception, operate, args); |
| 228 | *objname = exception.objname; |
| 229 | *errstring = exception.errstring; |
| 230 | *mallocedp = exception.message_buffer == exception.errstring; |
| 231 | return errorcode; |
| 232 | } |
| 233 | libc_hidden_def (_dl_catch_error) |
| 234 | |
| 235 | #if DL_ERROR_BOOTSTRAP |
| 236 | void |
| 237 | _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args) |
| 238 | { |
| 239 | struct catch *old_catch = catch_hook; |
| 240 | receiver_fct old_receiver = receiver; |
| 241 | |
| 242 | /* Set the new values. */ |
| 243 | catch_hook = NULL; |
| 244 | receiver = fct; |
| 245 | |
| 246 | (*operate) (args); |
| 247 | |
| 248 | catch_hook = old_catch; |
| 249 | receiver = old_receiver; |
| 250 | } |
| 251 | #endif /* DL_ERROR_BOOTSTRAP */ |
| 252 | |