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 const char **objname
; /* Object/File name. */
43 const char **errstring
; /* Error detail filled in here. */
44 bool *malloced
; /* Nonzero if the string is malloced
45 by the libc malloc. */
46 volatile int *errcode
; /* Return value of _dl_signal_error. */
47 jmp_buf env
; /* longjmp here on error. */
50 /* Multiple threads at once can use the `_dl_catch_error' function. The
51 calls can come from `_dl_map_object_deps', `_dlerror_run', or from
52 any of the libc functionality which loads dynamic objects (NSS, iconv).
53 Therefore we have to be prepared to save the state in thread-local
55 #if !DL_ERROR_BOOTSTRAP
56 static __thread
struct catch *catch_hook attribute_tls_model_ie
;
58 /* The version of this code in ld.so cannot use thread-local variables
59 and is used during bootstrap only. */
60 static struct catch *catch_hook
;
63 /* This message we return as a last resort. We define the string in a
64 variable since we have to avoid freeing it and so have to enable
65 a pointer comparison. See below and in dlfcn/dlerror.c. */
66 static const char _dl_out_of_memory
[] = "out of memory";
68 #if DL_ERROR_BOOTSTRAP
69 /* This points to a function which is called when an continuable error is
70 received. Unlike the handling of `catch' this function may return.
71 The arguments will be the `errstring' and `objname'.
73 Since this functionality is not used in normal programs (only in ld.so)
74 we do not care about multi-threaded programs here. We keep this as a
76 static receiver_fct receiver
;
77 #endif /* DL_ERROR_BOOTSTRAP */
81 _dl_signal_error (int errcode
, const char *objname
, const char *occation
,
82 const char *errstring
)
84 struct catch *lcatch
= catch_hook
;
87 errstring
= N_("DYNAMIC LINKER BUG!!!");
93 /* We are inside _dl_catch_error. Return to it. We have to
94 duplicate the error string since it might be allocated on the
95 stack. The object name is always a string constant. */
96 size_t len_objname
= strlen (objname
) + 1;
97 size_t len_errstring
= strlen (errstring
) + 1;
99 char *errstring_copy
= malloc (len_objname
+ len_errstring
);
100 if (errstring_copy
!= NULL
)
102 /* Make a copy of the object file name and the error string. */
103 *lcatch
->objname
= memcpy (__mempcpy (errstring_copy
,
104 errstring
, len_errstring
),
105 objname
, len_objname
);
106 *lcatch
->errstring
= errstring_copy
;
108 /* If the main executable is relocated it means the libc's malloc
110 bool malloced
= true;
112 malloced
= (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
!= NULL
113 && (GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
->l_relocated
!= 0));
115 *lcatch
->malloced
= malloced
;
119 /* This is better than nothing. */
120 *lcatch
->objname
= "";
121 *lcatch
->errstring
= _dl_out_of_memory
;
122 *lcatch
->malloced
= false;
125 *lcatch
->errcode
= errcode
;
127 /* We do not restore the signal mask because none was saved. */
128 __longjmp (lcatch
->env
[0].__jmpbuf
, 1);
132 /* Lossage while resolving the program's own symbols is always fatal. */
134 _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
136 occation
?: N_("error while loading shared libraries"),
137 objname
, *objname
? ": " : "",
138 errstring
, errcode
? ": " : "",
140 ? __strerror_r (errcode
, buffer
, sizeof buffer
)
144 libc_hidden_def (_dl_signal_error
)
147 #if DL_ERROR_BOOTSTRAP
150 _dl_signal_cerror (int errcode
, const char *objname
, const char *occation
,
151 const char *errstring
)
153 if (__builtin_expect (GLRO(dl_debug_mask
)
154 & ~(DL_DEBUG_STATISTICS
|DL_DEBUG_PRELINK
), 0))
155 _dl_debug_printf ("%s: error: %s: %s (%s)\n", objname
, occation
,
156 errstring
, receiver
? "continued" : "fatal");
160 /* We are inside _dl_receive_error. Call the user supplied
161 handler and resume the work. The receiver will still be
163 (*receiver
) (errcode
, objname
, errstring
);
166 _dl_signal_error (errcode
, objname
, occation
, errstring
);
168 #endif /* DL_ERROR_BOOTSTRAP */
173 _dl_catch_error (const char **objname
, const char **errstring
,
174 bool *mallocedp
, 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. */
188 c
.errstring
= errstring
;
189 c
.malloced
= mallocedp
;
190 c
.errcode
= &errcode
;
192 struct catch *const old
= catch_hook
;
195 /* Do not save the signal mask. */
196 if (__builtin_expect (__sigsetjmp (c
.env
, 0), 0) == 0)
206 /* We get here only if we longjmp'd out of OPERATE. _dl_signal_error has
207 already stored values into *OBJNAME, *ERRSTRING, and *MALLOCEDP. */
211 libc_hidden_def (_dl_catch_error
)
213 #if DL_ERROR_BOOTSTRAP
216 _dl_receive_error (receiver_fct fct
, void (*operate
) (void *), void *args
)
218 struct catch *old_catch
= catch_hook
;
219 receiver_fct old_receiver
= receiver
;
221 /* Set the new values. */
227 catch_hook
= old_catch
;
228 receiver
= old_receiver
;
230 #endif /* DL_ERROR_BOOTSTRAP */