Update.
[glibc.git] / elf / dl-error.c
blobe802c27763e3c5d456c277d29dc57636d0472d4b
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-tsd.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. */
41 __libc_tsd_define (static, DL_ERROR)
42 #define tsd_getspecific() __libc_tsd_get (DL_ERROR)
43 #define tsd_setspecific(data) __libc_tsd_set (DL_ERROR, (data))
46 /* This points to a function which is called when an continuable error is
47 received. Unlike the handling of `catch' this function may return.
48 The arguments will be the `errstring' and `objname'.
50 Since this functionality is not used in normal programs (only in ld.so)
51 we do not care about multi-threaded programs here. We keep this as a
52 global variable. */
53 static receiver_fct receiver;
56 void
57 internal_function
58 _dl_signal_error (int errcode,
59 const char *objname,
60 const char *errstring)
62 struct catch *lcatch;
64 if (! errstring)
65 errstring = "DYNAMIC LINKER BUG!!!";
67 lcatch = tsd_getspecific ();
68 if (lcatch != NULL)
70 /* We are inside _dl_catch_error. Return to it. We have to
71 duplicate the error string since it might be allocated on the
72 stack. */
73 size_t objname_len = objname ? strlen (objname) + 2 : 0;
74 size_t errstring_len = strlen (errstring) + 1;
75 lcatch->errstring = malloc (objname_len + errstring_len);
76 if (lcatch->errstring != NULL)
78 if (objname_len > 0)
80 memcpy (lcatch->errstring, objname, objname_len - 2);
81 memcpy (lcatch->errstring + objname_len - 2, ": ", 2);
83 memcpy (lcatch->errstring + objname_len, errstring, errstring_len);
85 longjmp (lcatch->env, errcode ?: -1);
87 else
89 /* Lossage while resolving the program's own symbols is always fatal. */
90 char buffer[1024];
91 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
92 ": error in loading shared libraries: ",
93 objname ?: "", objname && *objname ? ": " : "",
94 errstring, errcode ? ": " : "",
95 (errcode
96 ? __strerror_r (errcode, buffer, sizeof buffer)
97 : ""), "\n", NULL);
102 void
103 internal_function
104 _dl_signal_cerror (int errcode,
105 const char *objname,
106 const char *errstring)
108 if (receiver)
110 /* We are inside _dl_receive_error. Call the user supplied
111 handler and resume the work. The receiver will still be
112 installed. */
113 (*receiver) (errcode, objname, errstring);
115 else
116 _dl_signal_error (errcode, objname, errstring);
121 internal_function
122 _dl_catch_error (char **errstring,
123 void (*operate) (void *),
124 void *args)
126 int errcode;
127 struct catch *volatile old;
128 struct catch c;
129 /* We need not handle `receiver' since setting a `catch' is handled
130 before it. */
132 /* Some systems (e.g., SPARC) handle constructors to local variables
133 inefficient. So we initialize `c' by hand. */
134 c.errstring = NULL;
136 old = tsd_getspecific ();
137 errcode = setjmp (c.env);
138 if (errcode == 0)
140 tsd_setspecific (&c);
141 (*operate) (args);
142 tsd_setspecific (old);
143 *errstring = NULL;
144 return 0;
147 /* We get here only if we longjmp'd out of OPERATE. */
148 tsd_setspecific (old);
149 *errstring = c.errstring;
150 return errcode == -1 ? 0 : errcode;
153 void
154 internal_function
155 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
157 struct catch *old_catch;
158 receiver_fct old_receiver;
160 old_catch = tsd_getspecific ();
161 old_receiver = receiver;
163 /* Set the new values. */
164 tsd_setspecific (NULL);
165 receiver = fct;
167 (*operate) (args);
169 tsd_setspecific (old_catch);
170 receiver = old_receiver;