1/* Template for error handling for runtime dynamic linker.
2 Copyright (C) 1995-2019 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 <http://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. */
40struct 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
53static __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. */
57static 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. */
68static receiver_fct receiver;
69#endif /* DL_ERROR_BOOTSTRAP */
70
71/* Lossage while resolving the program's own symbols is always fatal. */
72static void
73__attribute__ ((noreturn))
74fatal_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
88void
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}
104libc_hidden_def (_dl_signal_exception)
105
106void
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}
126libc_hidden_def (_dl_signal_error)
127
128
129#if DL_ERROR_BOOTSTRAP
130void
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
151void
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
172int
173_dl_catch_exception (struct dl_exception *exception,
174 void (*operate) (void *), void *args)
175{
176 /* We need not handle `receiver' since setting a `catch' is handled
177 before it. */
178
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;
184
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;
189
190 struct catch *const old = catch_hook;
191 catch_hook = &c;
192
193 /* Do not save the signal mask. */
194 if (__builtin_expect (__sigsetjmp (c.env, 0), 0) == 0)
195 {
196 (*operate) (args);
197 catch_hook = old;
198 *exception = (struct dl_exception) { NULL };
199 return 0;
200 }
201
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;
207}
208libc_hidden_def (_dl_catch_exception)
209
210int
211_dl_catch_error (const char **objname, const char **errstring,
212 bool *mallocedp, void (*operate) (void *), void *args)
213{
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;
220}
221libc_hidden_def (_dl_catch_error)
222
223#if DL_ERROR_BOOTSTRAP
224void
225_dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
226{
227 struct catch *old_catch = catch_hook;
228 receiver_fct old_receiver = receiver;
229
230 /* Set the new values. */
231 catch_hook = NULL;
232 receiver = fct;
233
234 (*operate) (args);
235
236 catch_hook = old_catch;
237 receiver = old_receiver;
238}
239#endif /* DL_ERROR_BOOTSTRAP */
240