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
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. */
38 /* This structure communicates state between _dl_catch_error and
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
52 #if !DL_ERROR_BOOTSTRAP
53 static __thread
struct catch *catch_hook attribute_tls_model_ie
;
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
;
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
68 static receiver_fct receiver
;
69 #endif /* DL_ERROR_BOOTSTRAP */
71 /* Lossage while resolving the program's own symbols is always fatal. */
73 __attribute__ ((noreturn
))
74 fatal_error (int errcode
, const char *objname
, const char *occasion
,
75 const char *errstring
)
78 _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
80 occasion
?: N_("error while loading shared libraries"),
81 objname
, *objname
? ": " : "",
82 errstring
, errcode
? ": " : "",
84 ? __strerror_r (errcode
, buffer
, sizeof buffer
)
89 _dl_signal_exception (int errcode
, struct dl_exception
*exception
,
92 struct catch *lcatch
= catch_hook
;
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);
102 fatal_error (errcode
, exception
->objname
, occasion
, exception
->errstring
);
104 libc_hidden_def (_dl_signal_exception
)
107 _dl_signal_error (int errcode
, const char *objname
, const char *occation
,
108 const char *errstring
)
110 struct catch *lcatch
= catch_hook
;
113 errstring
= N_("DYNAMIC LINKER BUG!!!");
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);
124 fatal_error (errcode
, objname
, occation
, errstring
);
126 libc_hidden_def (_dl_signal_error
)
129 #if DL_ERROR_BOOTSTRAP
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");
142 /* We are inside _dl_receive_error. Call the user supplied
143 handler and resume the work. The receiver will still be
145 (*receiver
) (errcode
, exception
->objname
, exception
->errstring
);
148 _dl_signal_exception (errcode
, exception
, occasion
);
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");
162 /* We are inside _dl_receive_error. Call the user supplied
163 handler and resume the work. The receiver will still be
165 (*receiver
) (errcode
, objname
, errstring
);
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
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
;
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
;
193 /* Do not save the signal mask. */
194 if (__builtin_expect (__sigsetjmp (c
.env
, 0), 0) == 0)
198 *exception
= (struct dl_exception
) { NULL
};
202 /* We get here only if we longjmp'd out of OPERATE.
203 _dl_signal_exception has already stored values into
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
;
221 libc_hidden_def (_dl_catch_error
)
223 #if DL_ERROR_BOOTSTRAP
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. */
236 catch_hook
= old_catch
;
237 receiver
= old_receiver
;
239 #endif /* DL_ERROR_BOOTSTRAP */