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. */
24 #include <elf/ldsodefs.h>
25 #include <bits/libc-lock.h>
27 /* This structure communicates state between _dl_catch_error and
31 char *errstring
; /* Error detail filled in here. */
32 jmp_buf env
; /* longjmp here on error. */
35 /* Multiple threads at once can use the `_dl_catch_error' function. The
36 calls can come from `_dl_map_object_deps', `_dlerror_run', or from
37 any of the libc functionality which loads dynamic objects (NSS, iconv).
38 Therefore we have to be prepared to save the state in thread-local
39 memory. `catch' will only be used for the non-threaded case.
41 Please note the horrible kludge we have to use to check for the
42 thread functions to be defined. The problem is that while running
43 ld.so standalone (i.e., before the relocation with the available
44 libc symbols) we do not have a real handling of undefined weak symbols.
45 All symbols are relocated, regardless of the availability. They are
46 relocated relative to the load address of the dynamic linker. Adding
47 this start address to zero (the value in the GOT for undefined symbols)
48 leads to an address which is the load address of ld.so. Once we have
49 relocated with the libc values the value is NULL if the function is
50 not available. Our "solution" is to regard NULL and the ld.so load
51 address as indicators for unavailable weak symbols. */
52 static struct catch *catch;
55 # define tsd_setspecific(data) \
56 if (__libc_internal_tsd_set != (void *) _dl_rtld_map.l_addr \
57 && __libc_internal_tsd_set != NULL) \
58 __libc_internal_tsd_set (_LIBC_TSD_KEY_DL_ERROR, data); \
61 # define tsd_getspecific() \
62 (__libc_internal_tsd_set != (void *) _dl_rtld_map.l_addr \
63 && __libc_internal_tsd_set != NULL \
64 ? (struct catch *) __libc_internal_tsd_get (_LIBC_TSD_KEY_DL_ERROR) \
67 # define tsd_setspecific(data) \
68 if (__libc_internal_tsd_set != NULL) \
69 __libc_internal_tsd_set (_LIBC_TSD_KEY_DL_ERROR, data); \
72 # define tsd_getspecific() \
73 (__libc_internal_tsd_set != NULL \
74 ? (struct catch *) __libc_internal_tsd_get (_LIBC_TSD_KEY_DL_ERROR) \
79 /* This points to a function which is called when an error is
80 received. Unlike the handling of `catch' this function may return.
81 The arguments will be the `errstring' and `objname'.
83 Since this functionality is not used in normal programs (only in ld.so)
84 we do not care about multi-threaded programs here. We keep this as a
86 static receiver_fct receiver
;
91 _dl_signal_error (int errcode
,
93 const char *errstring
)
98 errstring
= "DYNAMIC LINKER BUG!!!";
100 lcatch
= tsd_getspecific ();
103 /* We are inside _dl_catch_error. Return to it. We have to
104 duplicate the error string since it might be allocated on the
106 size_t objname_len
= objname
? strlen (objname
) + 2 : 0;
107 size_t errstring_len
= strlen (errstring
) + 1;
108 lcatch
->errstring
= malloc (objname_len
+ errstring_len
);
109 if (lcatch
->errstring
!= NULL
)
113 memcpy (lcatch
->errstring
, objname
, objname_len
- 2);
114 memcpy (lcatch
->errstring
+ objname_len
- 2, ": ", 2);
116 memcpy (lcatch
->errstring
+ objname_len
, errstring
, errstring_len
);
118 longjmp (lcatch
->env
, errcode
?: -1);
122 /* We are inside _dl_receive_error. Call the user supplied
123 handler and resume the work. The receiver will still be
125 (*receiver
) (errcode
, objname
, errstring
);
129 /* Lossage while resolving the program's own symbols is always fatal. */
131 _dl_sysdep_fatal (_dl_argv
[0] ?: "<program name unknown>",
132 ": error in loading shared libraries: ",
133 objname
?: "", objname
&& *objname
? ": " : "",
134 errstring
, errcode
? ": " : "",
136 ? __strerror_r (errcode
, buffer
, sizeof buffer
)
143 _dl_catch_error (char **errstring
,
144 void (*operate
) (void *),
148 struct catch *volatile old
;
150 /* We need not handle `receiver' since setting a `catch' is handled
153 /* Some systems (e.g., SPARC) handle constructors to local variables
154 inefficient. So we initialize `c' by hand. */
157 old
= tsd_getspecific ();
158 errcode
= setjmp (c
.env
);
161 tsd_setspecific (&c
);
163 tsd_setspecific (old
);
168 /* We get here only if we longjmp'd out of OPERATE. */
169 tsd_setspecific (old
);
170 *errstring
= c
.errstring
;
171 return errcode
== -1 ? 0 : errcode
;
176 _dl_receive_error (receiver_fct fct
, void (*operate
) (void *), void *args
)
178 struct catch *old_catch
;
179 receiver_fct old_receiver
;
181 old_catch
= tsd_getspecific ();
182 old_receiver
= receiver
;
184 /* Set the new values. */
185 tsd_setspecific (NULL
);
190 tsd_setspecific (old_catch
);
191 receiver
= old_receiver
;