]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-error.c
Update.
[thirdparty/glibc.git] / elf / dl-error.c
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.
4
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.
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 Library General Public License for more details.
14
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. */
19
20 #include <link.h>
21 #include <setjmp.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 /* This is the internal function we use to generate the error string. */
27 extern char *_strerror_internal __P ((int, char *, size_t));
28
29 /* This structure communicates state between _dl_catch_error and
30 _dl_signal_error. */
31 struct catch
32 {
33 char *errstring; /* Error detail filled in here. */
34 jmp_buf env; /* longjmp here on error. */
35 };
36
37 /* This points to such a structure during a call to _dl_catch_error.
38 During implicit startup and run-time work for needed shared libraries,
39 this is null. */
40 static struct catch *catch;
41
42 /* This points to a function which is called when an error is
43 received. Unlike the handling of `catch' this function may return.
44 The arguments will be the `errstring' and `objname'. */
45 static receiver_fct receiver;
46
47
48 void
49 _dl_signal_error (int errcode,
50 const char *objname,
51 const char *errstring)
52 {
53 if (! errstring)
54 errstring = "DYNAMIC LINKER BUG!!!";
55
56 if (catch)
57 {
58 /* We are inside _dl_catch_error. Return to it. We have to
59 duplicate the error string since it might be allocated on the
60 stack. */
61 size_t objname_len = objname ? strlen (objname) + 2 : 0;
62 size_t errstring_len = strlen (errstring) + 1;
63 catch->errstring = malloc (objname_len + errstring_len);
64 if (catch->errstring != NULL)
65 {
66 if (objname_len > 0)
67 {
68 memcpy (catch->errstring, objname, objname_len - 2);
69 memcpy (catch->errstring + objname_len - 2, ": ", 2);
70 }
71 memcpy (catch->errstring + objname_len, errstring, errstring_len);
72 }
73 longjmp (catch->env, errcode ?: -1);
74 }
75 else if (receiver)
76 {
77 /* We are inside _dl_receive_error. Call the user supplied
78 handler and resume the work. The receiver will still be
79 installed. */
80 (*receiver) (errcode, objname, errstring);
81 }
82 else
83 {
84 /* Lossage while resolving the program's own symbols is always fatal. */
85 char buffer[1024];
86 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
87 ": error in loading shared libraries: ",
88 objname ?: "", objname && *objname ? ": " : "",
89 errstring, errcode ? ": " : "",
90 (errcode
91 ? _strerror_internal (errcode, buffer, sizeof buffer)
92 : ""), "\n", NULL);
93 }
94 }
95
96 int
97 _dl_catch_error (char **errstring,
98 void (*operate) (void *),
99 void *args)
100 {
101 int errcode;
102 struct catch *old, c;
103 /* We need not handle `receiver' since setting a `catch' is handled
104 before it. */
105
106 /* Some systems (e.g., SPARC) handle constructors to local variables
107 inefficient. So we initialize `c' by hand. */
108 c.errstring = NULL;
109
110 old = catch;
111 errcode = setjmp (c.env);
112 if (errcode == 0)
113 {
114 catch = &c;
115 (*operate) (args);
116 catch = old;
117 *errstring = NULL;
118 return 0;
119 }
120
121 /* We get here only if we longjmp'd out of OPERATE. */
122 catch = old;
123 *errstring = c.errstring;
124 return errcode == -1 ? 0 : errcode;
125 }
126
127 void
128 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
129 {
130 struct catch *old_catch;
131 receiver_fct old_receiver;
132
133 old_catch = catch;
134 old_receiver = receiver;
135
136 /* Set the new values. */
137 catch = NULL;
138 receiver = fct;
139
140 (*operate) (args);
141
142 catch = old_catch;
143 receiver = old_receiver;
144 }