Mention IFUNC enhancements to testsuite in NEWS.
[platform/upstream/glibc.git] / elf / dl-error.c
1 /* Error handling for runtime dynamic linker.
2    Copyright (C) 1995-2002,2004,2005 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 #include <libintl.h>
20 #include <setjmp.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <ldsodefs.h>
26
27 /* This structure communicates state between _dl_catch_error and
28    _dl_signal_error.  */
29 struct catch
30   {
31     const char *objname;        /* Object/File name.  */
32     const char *errstring;      /* Error detail filled in here.  */
33     bool malloced;              /* Nonzero if the string is malloced
34                                    by the libc malloc.  */
35     jmp_buf env;                /* longjmp here on error.  */
36   };
37
38 /* Multiple threads at once can use the `_dl_catch_error' function.  The
39    calls can come from `_dl_map_object_deps', `_dlerror_run', or from
40    any of the libc functionality which loads dynamic objects (NSS, iconv).
41    Therefore we have to be prepared to save the state in thread-local
42    memory.  The _dl_error_catch_tsd function pointer is reset by the thread
43    library so that it returns the address of a thread-local variable.  */
44
45
46 /* This message we return as a last resort.  We define the string in a
47    variable since we have to avoid freeing it and so have to enable
48    a pointer comparison.  See below and in dlfcn/dlerror.c.  */
49 static const char _dl_out_of_memory[] = "out of memory";
50
51
52 /* This points to a function which is called when an continuable error is
53    received.  Unlike the handling of `catch' this function may return.
54    The arguments will be the `errstring' and `objname'.
55
56    Since this functionality is not used in normal programs (only in ld.so)
57    we do not care about multi-threaded programs here.  We keep this as a
58    global variable.  */
59 static receiver_fct receiver;
60
61 #ifdef _LIBC_REENTRANT
62 # define CATCH_HOOK     (*(struct catch **) (*GL(dl_error_catch_tsd)) ())
63 #else
64 static struct catch *catch_hook;
65 # define CATCH_HOOK     catch_hook
66 #endif
67
68 void
69 internal_function
70 _dl_signal_error (int errcode, const char *objname, const char *occation,
71                   const char *errstring)
72 {
73   struct catch *lcatch;
74
75   if (! errstring)
76     errstring = N_("DYNAMIC LINKER BUG!!!");
77
78   lcatch = CATCH_HOOK;
79   if (objname == NULL)
80     objname = "";
81   if (lcatch != NULL)
82     {
83       /* We are inside _dl_catch_error.  Return to it.  We have to
84          duplicate the error string since it might be allocated on the
85          stack.  The object name is always a string constant.  */
86       size_t len_objname = strlen (objname) + 1;
87       size_t len_errstring = strlen (errstring) + 1;
88
89       lcatch->errstring = (char *) malloc (len_objname + len_errstring);
90       if (lcatch->errstring != NULL)
91         {
92           /* Make a copy of the object file name and the error string.  */
93           lcatch->objname = memcpy (__mempcpy ((char *) lcatch->errstring,
94                                                errstring, len_errstring),
95                                     objname, len_objname);
96
97           /* If the main executable is relocated it means the libc's malloc
98              is used.  */
99 #ifdef SHARED
100           lcatch->malloced = (GL(dl_ns)[LM_ID_BASE]._ns_loaded != NULL
101                               && (GL(dl_ns)[LM_ID_BASE]._ns_loaded->l_relocated
102                                   != 0));
103 #else
104           lcatch->malloced = true;
105 #endif
106         }
107       else
108         {
109           /* This is better than nothing.  */
110           lcatch->objname = "";
111           lcatch->errstring = _dl_out_of_memory;
112           lcatch->malloced = false;
113         }
114       /* We do not restore the signal mask because none was saved.  */
115       __longjmp (lcatch->env[0].__jmpbuf, errcode ?: -1);
116     }
117   else
118     {
119       /* Lossage while resolving the program's own symbols is always fatal.  */
120       char buffer[1024];
121       _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
122                         rtld_progname ?: "<program name unknown>",
123                         occation ?: N_("error while loading shared libraries"),
124                         objname, *objname ? ": " : "",
125                         errstring, errcode ? ": " : "",
126                         (errcode
127                          ? __strerror_r (errcode, buffer, sizeof buffer)
128                          : ""));
129     }
130 }
131
132
133 void
134 internal_function
135 _dl_signal_cerror (int errcode, const char *objname, const char *occation,
136                    const char *errstring)
137 {
138   if (__builtin_expect (GLRO(dl_debug_mask)
139                         & ~(DL_DEBUG_STATISTICS|DL_DEBUG_PRELINK), 0))
140     _dl_debug_printf ("%s: error: %s: %s (%s)\n", objname, occation,
141                       errstring, receiver ? "continued" : "fatal");
142
143   if (receiver)
144     {
145       /* We are inside _dl_receive_error.  Call the user supplied
146          handler and resume the work.  The receiver will still be
147          installed.  */
148       (*receiver) (errcode, objname, errstring);
149     }
150   else
151     _dl_signal_error (errcode, objname, occation, errstring);
152 }
153
154
155 int
156 internal_function
157 _dl_catch_error (const char **objname, const char **errstring,
158                  bool *mallocedp, void (*operate) (void *), void *args)
159 {
160   int errcode;
161   struct catch *volatile old;
162   struct catch c;
163   /* We need not handle `receiver' since setting a `catch' is handled
164      before it.  */
165
166   /* Some systems (e.g., SPARC) handle constructors to local variables
167      inefficient.  So we initialize `c' by hand.  */
168   c.errstring = NULL;
169
170   struct catch **const catchp = &CATCH_HOOK;
171   old = *catchp;
172   /* Do not save the signal mask.  */
173   errcode = __sigsetjmp (c.env, 0);
174   if (__builtin_expect (errcode, 0) == 0)
175     {
176       *catchp = &c;
177       (*operate) (args);
178       *catchp = old;
179       *objname = NULL;
180       *errstring = NULL;
181       *mallocedp = false;
182       return 0;
183     }
184
185   /* We get here only if we longjmp'd out of OPERATE.  */
186   *catchp = old;
187   *objname = c.objname;
188   *errstring = c.errstring;
189   *mallocedp = c.malloced;
190   return errcode == -1 ? 0 : errcode;
191 }
192
193
194 void
195 internal_function
196 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
197 {
198   struct catch **const catchp = &CATCH_HOOK;
199   struct catch *old_catch;
200   receiver_fct old_receiver;
201
202   old_catch = *catchp;
203   old_receiver = receiver;
204
205   /* Set the new values.  */
206   *catchp = NULL;
207   receiver = fct;
208
209   (*operate) (args);
210
211   *catchp = old_catch;
212   receiver = old_receiver;
213 }