Update.
[glibc.git] / elf / dl-error.c
blob9f538e9318fb957f7a210cb812e62328fcb4100a
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 <stddef.h>
21 #include <link.h>
22 #include <setjmp.h>
23 #include <stdlib.h>
24 #include <string.h>
26 /* This structure communicates state between _dl_catch_error and
27 _dl_signal_error. */
28 struct catch
30 char *errstring; /* Error detail filled in here. */
31 jmp_buf env; /* longjmp here on error. */
34 /* This points to such a structure during a call to _dl_catch_error.
35 During implicit startup and run-time work for needed shared libraries,
36 this is null. */
37 static struct catch *catch;
39 /* This points to a function which is called when an error is
40 received. Unlike the handling of `catch' this function may return.
41 The arguments will be the `errstring' and `objname'. */
42 static receiver_fct receiver;
45 void
46 _dl_signal_error (int errcode,
47 const char *objname,
48 const char *errstring)
50 if (! errstring)
51 errstring = "DYNAMIC LINKER BUG!!!";
53 if (catch)
55 /* We are inside _dl_catch_error. Return to it. We have to
56 duplicate the error string since it might be allocated on the
57 stack. */
58 size_t objname_len = objname ? strlen (objname) + 2 : 0;
59 size_t errstring_len = strlen (errstring) + 1;
60 catch->errstring = malloc (objname_len + errstring_len);
61 if (catch->errstring != NULL)
63 if (objname_len > 0)
65 memcpy (catch->errstring, objname, objname_len - 2);
66 memcpy (catch->errstring + objname_len - 2, ": ", 2);
68 memcpy (catch->errstring + objname_len, errstring, errstring_len);
70 longjmp (catch->env, errcode ?: -1);
72 else if (receiver)
74 /* We are inside _dl_receive_error. Call the user supplied
75 handler and resume the work. The receiver will still
76 installed. */
77 (*receiver) (errstring, objname);
79 else
81 /* Lossage while resolving the program's own symbols is always fatal. */
82 extern char **_dl_argv; /* Set in rtld.c at startup. */
83 _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
84 ": error in loading shared libraries\n",
85 objname ?: "", objname ? ": " : "",
86 errstring, errcode ? ": " : "",
87 errcode ? strerror (errcode) : "", "\n", NULL);
91 int
92 _dl_catch_error (char **errstring,
93 void (*operate) (void))
95 int errcode;
96 struct catch *old, c = { errstring: NULL };
97 /* We need not handle `receiver' since setting a `catch' is handle
98 before it. */
100 old = catch;
101 errcode = setjmp (c.env);
102 if (errcode == 0)
104 catch = &c;
105 (*operate) ();
106 catch = old;
107 *errstring = NULL;
108 return 0;
111 /* We get here only if we longjmp'd out of OPERATE. */
112 catch = old;
113 *errstring = c.errstring;
114 return errcode == -1 ? 0 : errcode;
117 void
118 _dl_receive_error (receiver_fct fct, void (*operate) (void))
120 struct catch *old_catch;
121 receiver_fct old_receiver;
123 old_catch = catch;
124 old_receiver = receiver;
126 /* Set the new values. */
127 catch = NULL;
128 receiver = fct;
130 (*operate) ();
132 catch = old_catch;
133 receiver = old_receiver;