Update.
[glibc.git] / elf / dl-error.c
blobae6344745327d00a2486c5675c68e3fe2026b40d
1 /* Error handling for runtime dynamic linker.
2 Copyright (C) 1995, 1996, 1997, 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <setjmp.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <elf/ldsodefs.h>
25 #include <bits/libc-lock.h>
27 /* This structure communicates state between _dl_catch_error and
28 _dl_signal_error. */
29 struct catch
31 char *errstring; /* Error detail filled in here. */
32 jmp_buf env; /* longjmp here on error. */
35 /* Multiple threads at once can use the `_dl_catch_error' function. The
36 calls can come from `_dl_map_object_deps', `_dlerror_run', or from
37 any of the libc functionality which loads dynamic objects (NSS, iconv).
38 Therefore we have to be prepared to save the state in thread-local
39 memory. `catch' will only be used for the non-threaded case.
41 Please note the horrible kludge we have to use to check for the
42 thread functions to be defined. The problem is that while running
43 ld.so standalone (i.e., before the relocation with the available
44 libc symbols) we do not have a real handling of undefined weak symbols.
45 All symbols are relocated, regardless of the availability. They are
46 relocated relative to the load address of the dynamic linker. Adding
47 this start address to zero (the value in the GOT for undefined symbols)
48 leads to an address which is the load address of ld.so. Once we have
49 relocated with the libc values the value is NULL if the function is
50 not available. Our "solution" is to regard NULL and the ld.so load
51 address as indicators for unavailable weak symbols. */
52 static struct catch *catch;
54 #ifdef PIC
55 # define tsd_setspecific(data) \
56 if (__libc_internal_tsd_set != (void *) _dl_rtld_map.l_addr \
57 && __libc_internal_tsd_set != NULL) \
58 __libc_internal_tsd_set (_LIBC_TSD_KEY_DL_ERROR, data); \
59 else \
60 catch = (data)
61 # define tsd_getspecific() \
62 (__libc_internal_tsd_set != (void *) _dl_rtld_map.l_addr \
63 && __libc_internal_tsd_set != NULL \
64 ? (struct catch *) __libc_internal_tsd_get (_LIBC_TSD_KEY_DL_ERROR) \
65 : catch)
66 #else
67 # define tsd_setspecific(data) \
68 if (__libc_internal_tsd_set != NULL) \
69 __libc_internal_tsd_set (_LIBC_TSD_KEY_DL_ERROR, data); \
70 else \
71 catch = (data)
72 # define tsd_getspecific() \
73 (__libc_internal_tsd_set != NULL \
74 ? (struct catch *) __libc_internal_tsd_get (_LIBC_TSD_KEY_DL_ERROR) \
75 : catch)
76 #endif
79 /* This points to a function which is called when an error is
80 received. Unlike the handling of `catch' this function may return.
81 The arguments will be the `errstring' and `objname'.
83 Since this functionality is not used in normal programs (only in ld.so)
84 we do not care about multi-threaded programs here. We keep this as a
85 global variable. */
86 static receiver_fct receiver;
89 void
90 internal_function
91 _dl_signal_error (int errcode,
92 const char *objname,
93 const char *errstring)
95 struct catch *lcatch;
97 if (! errstring)
98 errstring = "DYNAMIC LINKER BUG!!!";
100 lcatch = tsd_getspecific ();
101 if (lcatch != NULL)
103 /* We are inside _dl_catch_error. Return to it. We have to
104 duplicate the error string since it might be allocated on the
105 stack. */
106 size_t objname_len = objname ? strlen (objname) + 2 : 0;
107 size_t errstring_len = strlen (errstring) + 1;
108 lcatch->errstring = malloc (objname_len + errstring_len);
109 if (lcatch->errstring != NULL)
111 if (objname_len > 0)
113 memcpy (lcatch->errstring, objname, objname_len - 2);
114 memcpy (lcatch->errstring + objname_len - 2, ": ", 2);
116 memcpy (lcatch->errstring + objname_len, errstring, errstring_len);
118 longjmp (lcatch->env, errcode ?: -1);
120 else if (receiver)
122 /* We are inside _dl_receive_error. Call the user supplied
123 handler and resume the work. The receiver will still be
124 installed. */
125 (*receiver) (errcode, objname, errstring);
127 else
129 /* Lossage while resolving the program's own symbols is always fatal. */
130 char buffer[1024];
131 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
132 ": error in loading shared libraries: ",
133 objname ?: "", objname && *objname ? ": " : "",
134 errstring, errcode ? ": " : "",
135 (errcode
136 ? __strerror_r (errcode, buffer, sizeof buffer)
137 : ""), "\n", NULL);
142 internal_function
143 _dl_catch_error (char **errstring,
144 void (*operate) (void *),
145 void *args)
147 int errcode;
148 struct catch *volatile old;
149 struct catch c;
150 /* We need not handle `receiver' since setting a `catch' is handled
151 before it. */
153 /* Some systems (e.g., SPARC) handle constructors to local variables
154 inefficient. So we initialize `c' by hand. */
155 c.errstring = NULL;
157 old = tsd_getspecific ();
158 errcode = setjmp (c.env);
159 if (errcode == 0)
161 tsd_setspecific (&c);
162 (*operate) (args);
163 tsd_setspecific (old);
164 *errstring = NULL;
165 return 0;
168 /* We get here only if we longjmp'd out of OPERATE. */
169 tsd_setspecific (old);
170 *errstring = c.errstring;
171 return errcode == -1 ? 0 : errcode;
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;