Add sysdeps/ieee754/soft-fp.
[glibc.git] / elf / dl-error-skeleton.c
blob5b0fc8971b2aafe2141a7fec5a6ed5606cfa2d07
1 /* Template for error handling for runtime dynamic linker.
2 Copyright (C) 1995-2017 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/>. */
19 /* The following macro needs to be defined before including this
20 skeleton file:
22 DL_ERROR_BOOTSTRAP
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. */
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>
38 /* This structure communicates state between _dl_catch_error and
39 _dl_signal_error. */
40 struct catch
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. */
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
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'.
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 */
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)
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 : ""));
88 void
89 _dl_signal_exception (int errcode, struct dl_exception *exception,
90 const char *occasion)
92 struct catch *lcatch = catch_hook;
93 if (lcatch != NULL)
95 *lcatch->exception = *exception;
96 *lcatch->errcode = errcode;
98 /* We do not restore the signal mask because none was saved. */
99 __longjmp (lcatch->env[0].__jmpbuf, 1);
101 else
102 fatal_error (errcode, exception->objname, occasion, exception->errstring);
104 libc_hidden_def (_dl_signal_exception)
106 void
107 _dl_signal_error (int errcode, const char *objname, const char *occation,
108 const char *errstring)
110 struct catch *lcatch = catch_hook;
112 if (! errstring)
113 errstring = N_("DYNAMIC LINKER BUG!!!");
115 if (lcatch != NULL)
117 _dl_exception_create (lcatch->exception, objname, errstring);
118 *lcatch->errcode = errcode;
120 /* We do not restore the signal mask because none was saved. */
121 __longjmp (lcatch->env[0].__jmpbuf, 1);
123 else
124 fatal_error (errcode, objname, occation, errstring);
126 libc_hidden_def (_dl_signal_error)
129 #if DL_ERROR_BOOTSTRAP
130 void
131 _dl_signal_cexception (int errcode, struct dl_exception *exception,
132 const char *occasion)
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");
140 if (receiver)
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);
147 else
148 _dl_signal_exception (errcode, exception, occasion);
151 void
152 _dl_signal_cerror (int errcode, const char *objname, const char *occation,
153 const char *errstring)
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");
160 if (receiver)
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);
167 else
168 _dl_signal_error (errcode, objname, occation, errstring);
170 #endif /* DL_ERROR_BOOTSTRAP */
173 _dl_catch_exception (struct dl_exception *exception,
174 void (*operate) (void *), void *args)
176 /* We need not handle `receiver' since setting a `catch' is handled
177 before it. */
179 /* Only this needs to be marked volatile, because it is the only local
180 variable that gets changed between the setjmp invocation and the
181 longjmp call. All others are just set here (before setjmp) and read
182 in _dl_signal_error (before longjmp). */
183 volatile int errcode;
185 struct catch c;
186 /* Don't use an initializer since we don't need to clear C.env. */
187 c.exception = exception;
188 c.errcode = &errcode;
190 struct catch *const old = catch_hook;
191 catch_hook = &c;
193 /* Do not save the signal mask. */
194 if (__builtin_expect (__sigsetjmp (c.env, 0), 0) == 0)
196 (*operate) (args);
197 catch_hook = old;
198 *exception = (struct dl_exception) { NULL };
199 return 0;
202 /* We get here only if we longjmp'd out of OPERATE.
203 _dl_signal_exception has already stored values into
204 *EXCEPTION. */
205 catch_hook = old;
206 return errcode;
208 libc_hidden_def (_dl_catch_exception)
211 _dl_catch_error (const char **objname, const char **errstring,
212 bool *mallocedp, void (*operate) (void *), void *args)
214 struct dl_exception exception;
215 int errorcode = _dl_catch_exception (&exception, operate, args);
216 *objname = exception.objname;
217 *errstring = exception.errstring;
218 *mallocedp = exception.message_buffer == exception.errstring;
219 return errorcode;
221 libc_hidden_def (_dl_catch_error)
223 #if DL_ERROR_BOOTSTRAP
224 void
225 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
227 struct catch *old_catch = catch_hook;
228 receiver_fct old_receiver = receiver;
230 /* Set the new values. */
231 catch_hook = NULL;
232 receiver = fct;
234 (*operate) (args);
236 catch_hook = old_catch;
237 receiver = old_receiver;
239 #endif /* DL_ERROR_BOOTSTRAP */