source: trunk/libs/newlib/src/newlib/libc/sys/linux/dl/dl-error.c

Last change on this file was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 5.6 KB
Line 
1/* Error handling for runtime dynamic linker.
2   Copyright (C) 1995,96,97,98,99,2000,2001 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 Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the 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   Lesser General Public License for more details.
14
15   You should have received a copy of the GNU Lesser General Public
16   License along with the GNU C Library; if not, write to the Free
17   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18   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 <sys/libc-tsd.h>
27
28/* This structure communicates state between _dl_catch_error and
29   _dl_signal_error.  */
30struct 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.  */
51const 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.  */
61static receiver_fct receiver;
62
63
64void
65internal_function
66_dl_signal_error (int errcode, const char *objname, const char *occation,
67                  const char *errstring)
68{
69  struct catch *lcatch;
70
71  if (! errstring)
72    errstring = N_("DYNAMIC LINKER BUG!!!");
73
74  lcatch = tsd_getspecific ();
75  if (objname == NULL)
76    objname = "";
77  if (lcatch != NULL)
78    {
79      /* We are inside _dl_catch_error.  Return to it.  We have to
80         duplicate the error string since it might be allocated on the
81         stack.  The object name is always a string constant.  */
82      size_t len_objname = strlen (objname) + 1;
83      size_t len_errstring = strlen (errstring) + 1;
84
85      lcatch->errstring = (char *) malloc (len_objname + len_errstring);
86      if (lcatch->errstring != NULL)
87        {
88          char *tmp;
89          /* Make a copy of the object file name and the error string.  */
90          tmp = memcpy ((char *) lcatch->errstring,
91                           errstring, len_errstring);
92          tmp += len_errstring;
93          lcatch->objname = memcpy (tmp,
94                                  objname, len_objname);
95        }
96      else
97        {
98          /* This is better than nothing.  */
99          lcatch->objname = "";
100          lcatch->errstring = _dl_out_of_memory;
101        }
102      longjmp (lcatch->env, errcode ?: -1);
103    }
104  else
105    {
106      /* Lossage while resolving the program's own symbols is always fatal.  */
107      char buffer[1024];
108      _dl_fatal_printf ("%s: %s: %s%s%s%s%s\n",
109                        _dl_argv[0] ?: "<program name unknown>",
110                        occation ?: N_("error while loading shared libraries"),
111                        objname, *objname ? ": " : "",
112                        errstring, errcode ? ": " : "",
113                        (errcode
114                         ? __strerror_r (errcode, buffer, sizeof buffer)
115                         : ""));
116    }
117}
118
119
120void
121internal_function
122_dl_signal_cerror (int errcode, const char *objname, const char *occation,
123                   const char *errstring)
124{
125  if (receiver)
126    {
127      /* We are inside _dl_receive_error.  Call the user supplied
128         handler and resume the work.  The receiver will still be
129         installed.  */
130      (*receiver) (errcode, objname, errstring);
131    }
132  else
133    _dl_signal_error (errcode, objname, occation, errstring);
134}
135
136
137int
138internal_function
139_dl_catch_error (const char **objname, const char **errstring,
140                 void (*operate) (void *), void *args)
141{
142  int errcode;
143  struct catch *volatile old;
144  struct catch c;
145  /* We need not handle `receiver' since setting a `catch' is handled
146     before it.  */
147
148  /* Some systems (e.g., SPARC) handle constructors to local variables
149     inefficient.  So we initialize `c' by hand.  */
150  c.errstring = NULL;
151
152  old = tsd_getspecific ();
153  errcode = setjmp (c.env);
154  if (__builtin_expect (errcode, 0) == 0)
155    {
156      tsd_setspecific (&c);
157      (*operate) (args);
158      tsd_setspecific (old);
159      *objname = NULL;
160      *errstring = NULL;
161      return 0;
162    }
163
164  /* We get here only if we longjmp'd out of OPERATE.  */
165  tsd_setspecific (old);
166  *objname = c.objname;
167  *errstring = c.errstring;
168  return errcode == -1 ? 0 : errcode;
169}
170
171void
172internal_function
173_dl_receive_error (receiver_fct fct, void (*operate) (void *), void *args)
174{
175  struct catch *old_catch;
176  receiver_fct old_receiver;
177
178  old_catch = tsd_getspecific ();
179  old_receiver = receiver;
180
181  /* Set the new values.  */
182  tsd_setspecific (NULL);
183  receiver = fct;
184
185  (*operate) (args);
186
187  tsd_setspecific (old_catch);
188  receiver = old_receiver;
189}
Note: See TracBrowser for help on using the repository browser.