Update
[glibc.git] / elf / dl-error.c
blob88ab8e9519c0514e07079b224b2cac682e623cf0
1 /* Error handling for runtime dynamic linker.
2 Copyright (C) 1995,96,97,98,99,2000 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 <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 points to a function which is called when an continuable error is
49 received. Unlike the handling of `catch' this function may return.
50 The arguments will be the `errstring' and `objname'.
52 Since this functionality is not used in normal programs (only in ld.so)
53 we do not care about multi-threaded programs here. We keep this as a
54 global variable. */
55 static receiver_fct receiver;
58 void
59 internal_function
60 _dl_signal_error (int errcode, const char *objname, const char *errstring)
62 struct catch *lcatch;
64 if (! errstring)
65 errstring = N_("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. The object name is always a string constant. */
73 lcatch->objname = objname;
74 lcatch->errstring = strdup (errstring);
75 longjmp (lcatch->env, errcode ?: -1);
77 else
79 /* Lossage while resolving the program's own symbols is always fatal. */
80 char buffer[1024];
81 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
82 ": error while loading shared libraries: ",
83 objname ?: "", objname && *objname ? ": " : "",
84 errstring, errcode ? ": " : "",
85 (errcode
86 ? __strerror_r (errcode, buffer, sizeof buffer)
87 : ""), "\n", NULL);
92 void
93 internal_function
94 _dl_signal_cerror (int errcode,
95 const char *objname,
96 const char *errstring)
98 if (receiver)
100 /* We are inside _dl_receive_error. Call the user supplied
101 handler and resume the work. The receiver will still be
102 installed. */
103 (*receiver) (errcode, objname, errstring);
105 else
106 _dl_signal_error (errcode, objname, errstring);
111 internal_function
112 _dl_catch_error (const char **objname, const char **errstring,
113 void (*operate) (void *), void *args)
115 int errcode;
116 struct catch *volatile old;
117 struct catch c;
118 /* We need not handle `receiver' since setting a `catch' is handled
119 before it. */
121 /* Some systems (e.g., SPARC) handle constructors to local variables
122 inefficient. So we initialize `c' by hand. */
123 c.errstring = NULL;
125 old = tsd_getspecific ();
126 errcode = setjmp (c.env);
127 if (errcode == 0)
129 tsd_setspecific (&c);
130 (*operate) (args);
131 tsd_setspecific (old);
132 *objname = NULL;
133 *errstring = NULL;
134 return 0;
137 /* We get here only if we longjmp'd out of OPERATE. */
138 tsd_setspecific (old);
139 *objname = c.objname;
140 *errstring = c.errstring;
141 return errcode == -1 ? 0 : errcode;
144 void
145 internal_function
146 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
148 struct catch *old_catch;
149 receiver_fct old_receiver;
151 old_catch = tsd_getspecific ();
152 old_receiver = receiver;
154 /* Set the new values. */
155 tsd_setspecific (NULL);
156 receiver = fct;
158 (*operate) (args);
160 tsd_setspecific (old_catch);
161 receiver = old_receiver;