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