1 /* Exception handling in the dynamic linker.
2 Copyright (C) 1995-2024 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 <https://www.gnu.org/licenses/>. */
29 /* This structure communicates state between _dl_catch_error and
33 struct dl_exception
*exception
; /* The exception data is stored there. */
34 volatile int *errcode
; /* Return value of _dl_signal_error. */
35 jmp_buf env
; /* longjmp here on error. */
38 /* Multiple threads at once can use the `_dl_catch_error' function.
39 The calls can come from `_dl_map_object_deps', `_dlerror_run', or
40 from any of the libc functionality which loads dynamic objects
41 (NSS, iconv). Therefore we have to be prepared to save the state
42 in thread-local memory. We use THREAD_GETMEM and THREAD_SETMEM
43 instead of ELF TLS because ELF TLS is not available in the dynamic
44 loader. Additionally, the exception handling mechanism must be
45 usable before the TCB has been set up, which is why
46 rtld_catch_notls is used if !__rtld_tls_init_tp_called. This is
47 not needed for static builds, where initialization completes before
48 static dlopen etc. can be called. */
51 static struct rtld_catch
*rtld_catch_notls
;
54 static struct rtld_catch
*
58 if (!__rtld_tls_init_tp_called
)
59 return rtld_catch_notls
;
62 return THREAD_GETMEM (THREAD_SELF
, rtld_catch
);
66 set_catch (struct rtld_catch
*catch)
69 if (!__rtld_tls_init_tp_called
)
70 rtld_catch_notls
= catch;
73 THREAD_SETMEM (THREAD_SELF
, rtld_catch
, catch);
76 /* Lossage while resolving the program's own symbols is always fatal. */
78 __attribute__ ((noreturn
))
79 fatal_error (int errcode
, const char *objname
, const char *occasion
,
80 const char *errstring
)
83 _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
85 occasion
?: N_("error while loading shared libraries"),
86 objname
, *objname
? ": " : "",
87 errstring
, errcode
? ": " : "",
89 ? __strerror_r (errcode
, buffer
, sizeof buffer
)
94 _dl_signal_exception (int errcode
, struct dl_exception
*exception
,
97 struct rtld_catch
*lcatch
= get_catch ();
100 *lcatch
->exception
= *exception
;
101 *lcatch
->errcode
= errcode
;
103 /* We do not restore the signal mask because none was saved. */
104 __longjmp (lcatch
->env
[0].__jmpbuf
, 1);
107 fatal_error (errcode
, exception
->objname
, occasion
, exception
->errstring
);
109 rtld_hidden_def (_dl_signal_exception
)
112 _dl_signal_error (int errcode
, const char *objname
, const char *occasion
,
113 const char *errstring
)
115 struct rtld_catch
*lcatch
= get_catch ();
118 errstring
= N_("DYNAMIC LINKER BUG!!!");
122 _dl_exception_create (lcatch
->exception
, objname
, errstring
);
123 *lcatch
->errcode
= errcode
;
125 /* We do not restore the signal mask because none was saved. */
126 __longjmp (lcatch
->env
[0].__jmpbuf
, 1);
132 fatal_error (errcode
, objname
, occasion
, errstring
);
135 rtld_hidden_def (_dl_signal_error
)
138 /* This points to a function which is called when a continuable error is
139 received. Unlike the handling of `catch' this function may return.
140 The arguments will be the `errstring' and `objname'.
142 Since this functionality is not used in normal programs (only in ld.so)
143 we do not care about multi-threaded programs here. We keep this as a
145 static receiver_fct receiver
;
148 _dl_signal_cexception (int errcode
, struct dl_exception
*exception
,
149 const char *occasion
)
151 if (__builtin_expect (GLRO(dl_debug_mask
)
152 & ~(DL_DEBUG_STATISTICS
), 0))
153 _dl_debug_printf ("%s: error: %s: %s (%s)\n",
154 exception
->objname
, occasion
,
155 exception
->errstring
, receiver
? "continued" : "fatal");
159 /* We are inside _dl_receive_error. Call the user supplied
160 handler and resume the work. The receiver will still be
162 (*receiver
) (errcode
, exception
->objname
, exception
->errstring
);
165 _dl_signal_exception (errcode
, exception
, occasion
);
169 _dl_signal_cerror (int errcode
, const char *objname
, const char *occasion
,
170 const char *errstring
)
172 if (__builtin_expect (GLRO(dl_debug_mask
)
173 & ~(DL_DEBUG_STATISTICS
), 0))
174 _dl_debug_printf ("%s: error: %s: %s (%s)\n", objname
, occasion
,
175 errstring
, receiver
? "continued" : "fatal");
179 /* We are inside _dl_receive_error. Call the user supplied
180 handler and resume the work. The receiver will still be
182 (*receiver
) (errcode
, objname
, errstring
);
185 _dl_signal_error (errcode
, objname
, occasion
, errstring
);
189 _dl_receive_error (receiver_fct fct
, void (*operate
) (void *), void *args
)
191 struct rtld_catch
*old_catch
= get_catch ();
192 receiver_fct old_receiver
= receiver
;
194 /* Set the new values. */
200 set_catch (old_catch
);
201 receiver
= old_receiver
;
206 _dl_catch_exception (struct dl_exception
*exception
,
207 void (*operate
) (void *), void *args
)
209 /* If exception is NULL, temporarily disable exception handling.
210 Exceptions during operate (args) are fatal. */
211 if (exception
== NULL
)
213 struct rtld_catch
*old_catch
= get_catch ();
216 /* If we get here, the operation was successful. */
217 set_catch (old_catch
);
221 /* We need not handle `receiver' since setting a `catch' is handled
224 /* Only this needs to be marked volatile, because it is the only local
225 variable that gets changed between the setjmp invocation and the
226 longjmp call. All others are just set here (before setjmp) and read
227 in _dl_signal_error (before longjmp). */
228 volatile int errcode
;
231 /* Don't use an initializer since we don't need to clear C.env. */
232 c
.exception
= exception
;
233 c
.errcode
= &errcode
;
235 struct rtld_catch
*old
= get_catch ();
238 /* Do not save the signal mask. */
239 if (__builtin_expect (__sigsetjmp (c
.env
, 0), 0) == 0)
243 *exception
= (struct dl_exception
) { NULL
};
247 /* We get here only if we longjmp'd out of OPERATE.
248 _dl_signal_exception has already stored values into
253 rtld_hidden_def (_dl_catch_exception
)
256 _dl_catch_error (const char **objname
, const char **errstring
,
257 bool *mallocedp
, void (*operate
) (void *), void *args
)
259 struct dl_exception exception
;
260 int errorcode
= _dl_catch_exception (&exception
, operate
, args
);
261 *objname
= exception
.objname
;
262 *errstring
= exception
.errstring
;
263 *mallocedp
= exception
.message_buffer
== exception
.errstring
;