update from main archive 961116
[platform/upstream/glibc.git] / elf / dl-error.c
1 /* Error handling for runtime dynamic linker.
2    Copyright (C) 1995, 1996 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 <stddef.h>
21 #include <link.h>
22 #include <setjmp.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 /* This structure communicates state between _dl_catch_error and
27    _dl_signal_error.  */
28 struct catch
29   {
30     char *errstring;            /* Error detail filled in here.  */
31     const char *objname;
32     jmp_buf env;                /* longjmp here on error.  */
33   };
34
35 /* This points to such a structure during a call to _dl_catch_error.
36    During implicit startup and run-time work for needed shared libraries,
37    this is null.  */
38 static struct catch *catch;
39
40
41 void
42 _dl_signal_error (int errcode,
43                   const char *objname,
44                   const char *errstring)
45 {
46   if (! errstring)
47     errstring = "DYNAMIC LINKER BUG!!!";
48
49   if (catch)
50     {
51       /* We are inside _dl_catch_error.  Return to it.  We have to
52          duplicate the error string since it might be allocated on the
53          stack.  */
54       size_t len = strlen (errstring) + 1;
55       catch->errstring = malloc (len);
56       if (catch->errstring != NULL)
57         memcpy (catch->errstring, errstring, len);
58       catch->objname = objname;
59       longjmp (catch->env, errcode ?: -1);
60     }
61   else
62     {
63       /* Lossage while resolving the program's own symbols is always fatal.  */
64       extern char **_dl_argv;   /* Set in rtld.c at startup.  */
65       _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
66                         ": error in loading shared libraries\n",
67                         objname ?: "", objname ? ": " : "",
68                         errstring, errcode ? ": " : "",
69                         errcode ? strerror (errcode) : "", "\n", NULL);
70     }
71 }
72
73 int
74 _dl_catch_error (char **errstring,
75                  const char **objname,
76                  void (*operate) (void))
77 {
78   int errcode;
79   struct catch c = { errstring: NULL, objname: NULL };
80
81   errcode = setjmp (c.env);
82   if (errcode == 0)
83     {
84       catch = &c;
85       (*operate) ();
86       catch = NULL;
87       *errstring = NULL;
88       *objname = NULL;
89       return 0;
90     }
91
92   /* We get here only if we longjmp'd out of OPERATE.  */
93   *errstring = c.errstring;
94   *objname = c.objname;
95   return errcode == -1 ? 0 : errcode;
96 }