Update.
[platform/upstream/glibc.git] / elf / dl-error.c
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.
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 <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>
27
28 /* This structure communicates state between _dl_catch_error and
29    _dl_signal_error.  */
30 struct catch
31   {
32     const char *objname;        /* Object/File name.  */
33     const char *errstring;      /* Error detail filled in here.  */
34     jmp_buf env;                /* longjmp here on error.  */
35   };
36
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.  */
42
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))
46
47
48 /* This message we return as a last resort.  We define the string in a
49    variable since we have to avoid freeing it and so have to enable
50    a pointer comparison.  See below and in dlfcn/dlerror.c.  */
51 const char _dl_out_of_memory[] = "out of memory";
52
53
54 /* This points to a function which is called when an continuable error is
55    received.  Unlike the handling of `catch' this function may return.
56    The arguments will be the `errstring' and `objname'.
57
58    Since this functionality is not used in normal programs (only in ld.so)
59    we do not care about multi-threaded programs here.  We keep this as a
60    global variable.  */
61 static receiver_fct receiver;
62
63
64 void
65 internal_function
66 _dl_signal_error (int errcode, const char *objname, const char *errstring)
67 {
68   struct catch *lcatch;
69
70   if (! errstring)
71     errstring = N_("DYNAMIC LINKER BUG!!!");
72
73   lcatch = tsd_getspecific ();
74   if (lcatch != NULL)
75     {
76       /* We are inside _dl_catch_error.  Return to it.  We have to
77          duplicate the error string since it might be allocated on the
78          stack.  The object name is always a string constant.  */
79       size_t len_objname = strlen (objname) + 1;
80       size_t len_errstring = strlen (errstring) + 1;
81
82       lcatch->errstring = (char *) malloc (len_objname + len_errstring);
83       if (lcatch->errstring != NULL)
84         /* Make a copy of the object file name and the error string.  */
85         lcatch->objname = memcpy (__mempcpy ((char *) lcatch->errstring,
86                                              errstring, len_errstring),
87                                   objname, len_objname);
88       else
89         {
90           /* This is better than nothing.  */
91           lcatch->objname = objname;
92           lcatch->errstring = _dl_out_of_memory;
93         }
94       longjmp (lcatch->env, errcode ?: -1);
95     }
96   else
97     {
98       /* Lossage while resolving the program's own symbols is always fatal.  */
99       char buffer[1024];
100       _dl_sysdep_fatal (_dl_argv[0] ?: "<program name unknown>",
101                         ": error while loading shared libraries: ",
102                         objname ?: "", objname && *objname ? ": " : "",
103                         errstring, errcode ? ": " : "",
104                         (errcode
105                          ? __strerror_r (errcode, buffer, sizeof buffer)
106                          : ""), "\n", NULL);
107     }
108 }
109
110
111 void
112 internal_function
113 _dl_signal_cerror (int errcode,
114                    const char *objname,
115                    const char *errstring)
116 {
117   if (receiver)
118     {
119       /* We are inside _dl_receive_error.  Call the user supplied
120          handler and resume the work.  The receiver will still be
121          installed.  */
122       (*receiver) (errcode, objname, errstring);
123     }
124   else
125     _dl_signal_error (errcode, objname, errstring);
126 }
127
128
129 int
130 internal_function
131 _dl_catch_error (const char **objname, const char **errstring,
132                  void (*operate) (void *), void *args)
133 {
134   int errcode;
135   struct catch *volatile old;
136   struct catch c;
137   /* We need not handle `receiver' since setting a `catch' is handled
138      before it.  */
139
140   /* Some systems (e.g., SPARC) handle constructors to local variables
141      inefficient.  So we initialize `c' by hand.  */
142   c.errstring = NULL;
143
144   old = tsd_getspecific ();
145   errcode = setjmp (c.env);
146   if (errcode == 0)
147     {
148       tsd_setspecific (&c);
149       (*operate) (args);
150       tsd_setspecific (old);
151       *objname = NULL;
152       *errstring = NULL;
153       return 0;
154     }
155
156   /* We get here only if we longjmp'd out of OPERATE.  */
157   tsd_setspecific (old);
158   *objname = c.objname;
159   *errstring = c.errstring;
160   return errcode == -1 ? 0 : errcode;
161 }
162
163 void
164 internal_function
165 _dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
166 {
167   struct catch *old_catch;
168   receiver_fct old_receiver;
169
170   old_catch = tsd_getspecific ();
171   old_receiver = receiver;
172
173   /* Set the new values.  */
174   tsd_setspecific (NULL);
175   receiver = fct;
176
177   (*operate) (args);
178
179   tsd_setspecific (old_catch);
180   receiver = old_receiver;
181 }