Update.
[glibc.git] / elf / dl-error.c
blobccb4e4dc49164e6079de399d97c039b211ef54d5
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.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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 #include <bits/libc-tsd.h>
28 /* This structure communicates state between _dl_catch_error and
29 _dl_signal_error. */
30 struct catch
32 const char *objname; /* Object/File name. */
33 const char *errstring; /* Error detail filled in here. */
34 jmp_buf env; /* longjmp here on error. */
37 /* Multiple threads at once can use the `_dl_catch_error' function. The
38 calls can come from `_dl_map_object_deps', `_dlerror_run', or from
39 any of the libc functionality which loads dynamic objects (NSS, iconv).
40 Therefore we have to be prepared to save the state in thread-local
41 memory. */
43 __libc_tsd_define (static, DL_ERROR)
44 #define tsd_getspecific() __libc_tsd_get (DL_ERROR)
45 #define tsd_setspecific(data) __libc_tsd_set (DL_ERROR, (data))
48 /* This message we return as a last resort. We define the string in a
49 variable since we have to avoid freeing it and so have to enable
50 a pointer comparison. See below and in dlfcn/dlerror.c. */
51 const char _dl_out_of_memory[] = "out of memory";
52 INTVARDEF(_dl_out_of_memory)
55 /* This points to a function which is called when an continuable error is
56 received. Unlike the handling of `catch' this function may return.
57 The arguments will be the `errstring' and `objname'.
59 Since this functionality is not used in normal programs (only in ld.so)
60 we do not care about multi-threaded programs here. We keep this as a
61 global variable. */
62 static receiver_fct receiver;
65 void
66 internal_function
67 _dl_signal_error (int errcode, const char *objname, const char *occation,
68 const char *errstring)
70 struct catch *lcatch;
72 if (! errstring)
73 errstring = N_("DYNAMIC LINKER BUG!!!");
75 lcatch = tsd_getspecific ();
76 if (objname == NULL)
77 objname = "";
78 if (lcatch != NULL)
80 /* We are inside _dl_catch_error. Return to it. We have to
81 duplicate the error string since it might be allocated on the
82 stack. The object name is always a string constant. */
83 size_t len_objname = strlen (objname) + 1;
84 size_t len_errstring = strlen (errstring) + 1;
86 lcatch->errstring = (char *) malloc (len_objname + len_errstring);
87 if (lcatch->errstring != NULL)
88 /* Make a copy of the object file name and the error string. */
89 lcatch->objname = memcpy (__mempcpy ((char *) lcatch->errstring,
90 errstring, len_errstring),
91 objname, len_objname);
92 else
94 /* This is better than nothing. */
95 lcatch->objname = "";
96 lcatch->errstring = INTUSE(_dl_out_of_memory);
98 longjmp (lcatch->env, errcode ?: -1);
100 else
102 /* Lossage while resolving the program's own symbols is always fatal. */
103 char buffer[1024];
104 _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
105 rtld_progname ?: "<program name unknown>",
106 occation ?: N_("error while loading shared libraries"),
107 objname, *objname ? ": " : "",
108 errstring, errcode ? ": " : "",
109 (errcode
110 ? __strerror_r (errcode, buffer, sizeof buffer)
111 : ""));
114 INTDEF (_dl_signal_error)
117 void
118 internal_function
119 _dl_signal_cerror (int errcode, const char *objname, const char *occation,
120 const char *errstring)
122 if (__builtin_expect (GL(dl_debug_mask), 0))
123 INTUSE(_dl_debug_printf) ("%s: error: %s: %s (%s)\n", objname, occation,
124 errstring, receiver ? "continued" : "fatal");
126 if (receiver)
128 /* We are inside _dl_receive_error. Call the user supplied
129 handler and resume the work. The receiver will still be
130 installed. */
131 (*receiver) (errcode, objname, errstring);
133 else
134 INTUSE(_dl_signal_error) (errcode, objname, occation, errstring);
139 internal_function
140 _dl_catch_error (const char **objname, const char **errstring,
141 void (*operate) (void *), void *args)
143 int errcode;
144 struct catch *volatile old;
145 struct catch c;
146 /* We need not handle `receiver' since setting a `catch' is handled
147 before it. */
149 /* Some systems (e.g., SPARC) handle constructors to local variables
150 inefficient. So we initialize `c' by hand. */
151 c.errstring = NULL;
153 old = tsd_getspecific ();
154 errcode = setjmp (c.env);
155 if (__builtin_expect (errcode, 0) == 0)
157 tsd_setspecific (&c);
158 (*operate) (args);
159 tsd_setspecific (old);
160 *objname = NULL;
161 *errstring = NULL;
162 return 0;
165 /* We get here only if we longjmp'd out of OPERATE. */
166 tsd_setspecific (old);
167 *objname = c.objname;
168 *errstring = c.errstring;
169 return errcode == -1 ? 0 : errcode;
171 INTDEF (_dl_catch_error)
174 void
175 internal_function
176 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
178 struct catch *old_catch;
179 receiver_fct old_receiver;
181 old_catch = tsd_getspecific ();
182 old_receiver = receiver;
184 /* Set the new values. */
185 tsd_setspecific (NULL);
186 receiver = fct;
188 (*operate) (args);
190 tsd_setspecific (old_catch);
191 receiver = old_receiver;