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.
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 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. */
48 };
49
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
54 memory. */
55#if !DL_ERROR_BOOTSTRAP
56static __thread struct catch *catch_hook attribute_tls_model_ie;
57#else
58/* The version of this code in ld.so cannot use thread-local variables
59 and is used during bootstrap only. */
60static struct catch *catch_hook;
61#endif
62
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. */
66static const char _dl_out_of_memory[] = "out of memory";
67
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'.
72
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
75 global variable. */
76static receiver_fct receiver;
77#endif /* DL_ERROR_BOOTSTRAP */
78
79void
80internal_function
81_dl_signal_error (int errcode, const char *objname, const char *occation,
82 const char *errstring)
83{
84 struct catch *lcatch = catch_hook;
85
86 if (! errstring)
87 errstring = N_("DYNAMIC LINKER BUG!!!");
88
89 if (objname == NULL)
90 objname = "";
91 if (lcatch != NULL)
92 {
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;
98
99 char *errstring_copy = malloc (len_objname + len_errstring);
100 if (errstring_copy != NULL)
101 {
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;
107
108 /* If the main executable is relocated it means the libc's malloc
109 is used. */
110 bool malloced = true;
111#ifdef SHARED
112 malloced = (GL(dl_ns)[LM_ID_BASE]._ns_loaded != NULL
113 && (GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_relocated != 0));
114#endif
115 *lcatch->malloced = malloced;
116 }
117 else
118 {
119 /* This is better than nothing. */
120 *lcatch->objname = "";
121 *lcatch->errstring = _dl_out_of_memory;
122 *lcatch->malloced = false;
123 }
124
125 *lcatch->errcode = errcode;
126
127 /* We do not restore the signal mask because none was saved. */
128 __longjmp (lcatch->env[0].__jmpbuf, 1);
129 }
130 else
131 {
132 /* Lossage while resolving the program's own symbols is always fatal. */
133 char buffer[1024];
134 _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
135 RTLD_PROGNAME,
136 occation ?: N_("error while loading shared libraries"),
137 objname, *objname ? ": " : "",
138 errstring, errcode ? ": " : "",
139 (errcode
140 ? __strerror_r (errcode, buffer, sizeof buffer)
141 : ""));
142 }
143}
144libc_hidden_def (_dl_signal_error)
145
146
147#if DL_ERROR_BOOTSTRAP
148void
149internal_function
150_dl_signal_cerror (int errcode, const char *objname, const char *occation,
151 const char *errstring)
152{
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");
157
158 if (receiver)
159 {
160 /* We are inside _dl_receive_error. Call the user supplied
161 handler and resume the work. The receiver will still be
162 installed. */
163 (*receiver) (errcode, objname, errstring);
164 }
165 else
166 _dl_signal_error (errcode, objname, occation, errstring);
167}
168#endif /* DL_ERROR_BOOTSTRAP */
169
170
171int
172internal_function
173_dl_catch_error (const char **objname, const char **errstring,
174 bool *mallocedp, 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.objname = objname;
188 c.errstring = errstring;
189 c.malloced = mallocedp;
190 c.errcode = &errcode;
191
192 struct catch *const old = catch_hook;
193 catch_hook = &c;
194
195 /* Do not save the signal mask. */
196 if (__builtin_expect (__sigsetjmp (c.env, 0), 0) == 0)
197 {
198 (*operate) (args);
199 catch_hook = old;
200 *objname = NULL;
201 *errstring = NULL;
202 *mallocedp = false;
203 return 0;
204 }
205
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. */
208 catch_hook = old;
209 return errcode;
210}
211libc_hidden_def (_dl_catch_error)
212
213#if DL_ERROR_BOOTSTRAP
214void
215internal_function
216_dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
217{
218 struct catch *old_catch = catch_hook;
219 receiver_fct old_receiver = receiver;
220
221 /* Set the new values. */
222 catch_hook = NULL;
223 receiver = fct;
224
225 (*operate) (args);
226
227 catch_hook = old_catch;
228 receiver = old_receiver;
229}
230#endif /* DL_ERROR_BOOTSTRAP */
231