* sysdeps/unix/sysv/linux/m68k/register-dump.h: Use
[glibc.git] / elf / dl-lookup.c
blob88ac67ae1ec117e5cbb5501e4023b0fedf7bbc11
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2002, 2003, 2004 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 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.
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.
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. */
20 #include <alloca.h>
21 #include <libintl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <ldsodefs.h>
26 #include "dl-hash.h"
27 #include <dl-machine.h>
28 #include <bits/libc-lock.h>
29 #include <tls.h>
31 #include <assert.h>
33 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
35 /* We need this string more than once. */
36 static const char undefined_msg[] = "undefined symbol: ";
39 struct sym_val
41 const ElfW(Sym) *s;
42 struct link_map *m;
46 #define make_string(string, rest...) \
47 ({ \
48 const char *all[] = { string, ## rest }; \
49 size_t len, cnt; \
50 char *result, *cp; \
52 len = 1; \
53 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
54 len += strlen (all[cnt]); \
56 cp = result = alloca (len); \
57 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
58 cp = __stpcpy (cp, all[cnt]); \
60 result; \
63 /* Statistics function. */
64 #ifdef SHARED
65 # define bump_num_relocations() ++GL(dl_num_relocations)
66 #else
67 # define bump_num_relocations() ((void) 0)
68 #endif
71 /* The actual lookup code. */
72 #include "do-lookup.h"
75 /* Add extra dependency on MAP to UNDEF_MAP. */
76 static int
77 internal_function
78 add_dependency (struct link_map *undef_map, struct link_map *map)
80 struct link_map **list;
81 struct link_map *runp;
82 unsigned int act;
83 unsigned int i;
84 int result = 0;
86 /* Avoid self-references and references to objects which cannot be
87 unloaded anyway. */
88 if (undef_map == map)
89 return 0;
91 /* Make sure nobody can unload the object while we are at it. */
92 __rtld_lock_lock_recursive (GL(dl_load_lock));
94 /* Don't create cross-reference between modules which are
95 dynamically loaded by the same dlopen() call. */
96 if (undef_map->l_opencount == 0 && map->l_opencount == 0)
97 goto out;
99 /* Avoid references to objects which cannot be unloaded anyway. */
100 if (map->l_type != lt_loaded
101 || (map->l_flags_1 & DF_1_NODELETE) != 0)
102 goto out;
104 /* If the object with the undefined reference cannot be removed ever
105 just make sure the same is true for the object which contains the
106 definition. */
107 if (undef_map->l_type != lt_loaded
108 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
110 ++map->l_opencount;
111 map->l_flags |= DF_1_NODELETE;
112 goto out;
115 /* Determine whether UNDEF_MAP already has a reference to MAP. First
116 look in the normal dependencies. */
117 if (undef_map->l_searchlist.r_list != NULL)
119 list = undef_map->l_initfini;
121 for (i = 0; list[i] != NULL; ++i)
122 if (list[i] == map)
123 goto out;
126 /* No normal dependency. See whether we already had to add it
127 to the special list of dynamic dependencies. */
128 list = undef_map->l_reldeps;
129 act = undef_map->l_reldepsact;
131 for (i = 0; i < act; ++i)
132 if (list[i] == map)
133 goto out;
135 /* The object is not yet in the dependency list. Before we add
136 it make sure just one more time the object we are about to
137 reference is still available. There is a brief period in
138 which the object could have been removed since we found the
139 definition. */
140 runp = GL(dl_loaded);
141 while (runp != NULL && runp != map)
142 runp = runp->l_next;
144 if (runp != NULL)
146 /* The object is still available. Add the reference now. */
147 if (__builtin_expect (act >= undef_map->l_reldepsmax, 0))
149 /* Allocate more memory for the dependency list. Since this
150 can never happen during the startup phase we can use
151 `realloc'. */
152 void *newp;
154 undef_map->l_reldepsmax += 5;
155 newp = realloc (undef_map->l_reldeps,
156 undef_map->l_reldepsmax
157 * sizeof (struct link_map *));
159 if (__builtin_expect (newp != NULL, 1))
160 undef_map->l_reldeps = (struct link_map **) newp;
161 else
162 /* Correct the addition. */
163 undef_map->l_reldepsmax -= 5;
166 /* If we didn't manage to allocate memory for the list this is
167 no fatal mistake. We simply increment the use counter of the
168 referenced object and don't record the dependencies. This
169 means this increment can never be reverted and the object
170 will never be unloaded. This is semantically the correct
171 behavior. */
172 if (__builtin_expect (act < undef_map->l_reldepsmax, 1))
173 undef_map->l_reldeps[undef_map->l_reldepsact++] = map;
175 if (map->l_searchlist.r_list != NULL)
176 /* And increment the counter in the referenced object. */
177 ++map->l_opencount;
178 else
179 /* We have to bump the counts for all dependencies since so far
180 this object was only a normal or transitive dependency.
181 Now it might be closed with _dl_close() directly. */
182 for (list = map->l_initfini; *list != NULL; ++list)
183 ++(*list)->l_opencount;
185 /* Display information if we are debugging. */
186 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
187 _dl_debug_printf ("\
188 \nfile=%s; needed by %s (relocation dependency)\n\n",
189 map->l_name[0] ? map->l_name : rtld_progname,
190 undef_map->l_name[0]
191 ? undef_map->l_name : rtld_progname);
193 else
194 /* Whoa, that was bad luck. We have to search again. */
195 result = -1;
197 out:
198 /* Release the lock. */
199 __rtld_lock_unlock_recursive (GL(dl_load_lock));
201 return result;
204 static void
205 internal_function
206 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
207 const ElfW(Sym) **ref, struct r_scope_elem *symbol_scope[],
208 struct sym_val *value,
209 const struct r_found_version *version, int type_class,
210 int protected);
213 /* Search loaded objects' symbol tables for a definition of the symbol
214 UNDEF_NAME, perhaps with a requested version for the symbol. */
215 lookup_t
216 internal_function
217 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
218 const ElfW(Sym) **ref,
219 struct r_scope_elem *symbol_scope[],
220 const struct r_found_version *version,
221 int type_class, int flags, struct link_map *skip_map)
223 const unsigned long int hash = _dl_elf_hash (undef_name);
224 struct sym_val current_value = { NULL, NULL };
225 struct r_scope_elem **scope = symbol_scope;
227 bump_num_relocations ();
229 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY is allowed if we look
230 up a versioned symbol. */
231 assert (version == NULL || flags == 0 || flags == DL_LOOKUP_ADD_DEPENDENCY);
233 size_t i = 0;
234 if (__builtin_expect (skip_map != NULL, 0))
236 /* Search the relevant loaded objects for a definition. */
237 while ((*scope)->r_list[i] != skip_map)
238 ++i;
240 assert (i < (*scope)->r_nlist);
243 /* Search the relevant loaded objects for a definition. */
244 for (size_t start = i; *scope != NULL; start = 0, ++scope)
246 int res = do_lookup_x (undef_name, hash, *ref, &current_value, *scope,
247 start, version, flags, skip_map, type_class);
248 if (res > 0)
249 break;
251 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
253 /* Oh, oh. The file named in the relocation entry does not
254 contain the needed symbol. This code is never reached
255 for unversioned lookups. */
256 assert (version != NULL);
257 const char *reference_name = undef_map ? undef_map->l_name : NULL;
259 /* XXX We cannot translate the message. */
260 _dl_signal_cerror (0, (reference_name[0]
261 ? reference_name
262 : (rtld_progname ?: "<main program>")),
263 N_("relocation error"),
264 make_string ("symbol ", undef_name, ", version ",
265 version->name,
266 " not defined in file ",
267 version->filename,
268 " with link time reference",
269 res == -2
270 ? " (no version symbols)" : ""));
271 *ref = NULL;
272 return 0;
276 if (__builtin_expect (current_value.s == NULL, 0))
278 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
279 && skip_map == NULL)
281 /* We could find no value for a strong reference. */
282 const char *reference_name = undef_map ? undef_map->l_name : "";
283 const char *versionstr = version ? ", version " : "";
284 const char *versionname = (version && version->name
285 ? version->name : "");
287 /* XXX We cannot translate the message. */
288 _dl_signal_cerror (0, (reference_name[0]
289 ? reference_name
290 : (rtld_progname ?: "<main program>")), NULL,
291 make_string (undefined_msg, undef_name,
292 versionstr, versionname));
294 *ref = NULL;
295 return 0;
298 int protected = (*ref
299 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
300 if (__builtin_expect (protected != 0, 0))
302 /* It is very tricky. We need to figure out what value to
303 return for the protected symbol. */
304 if (type_class == ELF_RTYPE_CLASS_PLT)
306 if (current_value.s != NULL && current_value.m != undef_map)
308 current_value.s = *ref;
309 current_value.m = undef_map;
312 else
314 struct sym_val protected_value = { NULL, NULL };
316 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
317 if (do_lookup_x (undef_name, hash, *ref, &protected_value,
318 *scope, i, version, flags, skip_map,
319 ELF_RTYPE_CLASS_PLT) != 0)
320 break;
322 if (protected_value.s != NULL && protected_value.m != undef_map)
324 current_value.s = *ref;
325 current_value.m = undef_map;
330 /* We have to check whether this would bind UNDEF_MAP to an object
331 in the global scope which was dynamically loaded. In this case
332 we have to prevent the latter from being unloaded unless the
333 UNDEF_MAP object is also unloaded. */
334 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
335 /* Don't do this for explicit lookups as opposed to implicit
336 runtime lookups. */
337 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
338 /* Add UNDEF_MAP to the dependencies. */
339 && add_dependency (undef_map, current_value.m) < 0)
340 /* Something went wrong. Perhaps the object we tried to reference
341 was just removed. Try finding another definition. */
342 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
343 symbol_scope, version, type_class,
344 flags, skip_map);
346 if (__builtin_expect (GLRO(dl_debug_mask)
347 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
348 _dl_debug_bindings (undef_name, undef_map, ref, symbol_scope,
349 &current_value, version, type_class, protected);
351 *ref = current_value.s;
352 return LOOKUP_VALUE (current_value.m);
356 /* Cache the location of MAP's hash table. */
358 void
359 internal_function
360 _dl_setup_hash (struct link_map *map)
362 Elf_Symndx *hash;
363 Elf_Symndx nchain;
365 if (!map->l_info[DT_HASH])
366 return;
367 hash = (void *) D_PTR (map, l_info[DT_HASH]);
369 map->l_nbuckets = *hash++;
370 nchain = *hash++;
371 map->l_buckets = hash;
372 hash += map->l_nbuckets;
373 map->l_chain = hash;
377 static void
378 internal_function
379 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
380 const ElfW(Sym) **ref, struct r_scope_elem *symbol_scope[],
381 struct sym_val *value,
382 const struct r_found_version *version, int type_class,
383 int protected)
385 const char *reference_name = undef_map->l_name;
387 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
389 _dl_debug_printf ("binding file %s to %s: %s symbol `%s'",
390 (reference_name[0]
391 ? reference_name
392 : (rtld_progname ?: "<main program>")),
393 value->m->l_name[0] ? value->m->l_name : rtld_progname,
394 protected ? "protected" : "normal", undef_name);
395 if (version)
396 _dl_debug_printf_c (" [%s]\n", version->name);
397 else
398 _dl_debug_printf_c ("\n");
400 #ifdef SHARED
401 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
403 int conflict = 0;
404 struct sym_val val = { NULL, NULL };
406 if ((GLRO(dl_trace_prelink_map) == NULL
407 || GLRO(dl_trace_prelink_map) == GL(dl_loaded))
408 && undef_map != GL(dl_loaded))
410 const unsigned long int hash = _dl_elf_hash (undef_name);
412 do_lookup_x (undef_name, hash, *ref, &val,
413 undef_map->l_local_scope[0], 0, version, 0, NULL,
414 type_class);
416 if (val.s != value->s || val.m != value->m)
417 conflict = 1;
420 #ifdef USE_TLS
421 if (value->s
422 && (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
423 == STT_TLS, 0)))
424 type_class = 4;
425 #endif
427 if (conflict
428 || GLRO(dl_trace_prelink_map) == undef_map
429 || GLRO(dl_trace_prelink_map) == NULL
430 || type_class == 4)
432 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
433 conflict ? "conflict" : "lookup",
434 (int) sizeof (ElfW(Addr)) * 2,
435 (size_t) undef_map->l_map_start,
436 (int) sizeof (ElfW(Addr)) * 2,
437 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
438 (int) sizeof (ElfW(Addr)) * 2,
439 (size_t) (value->s ? value->m->l_map_start : 0),
440 (int) sizeof (ElfW(Addr)) * 2,
441 (size_t) (value->s ? value->s->st_value : 0));
443 if (conflict)
444 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
445 (int) sizeof (ElfW(Addr)) * 2,
446 (size_t) (val.s ? val.m->l_map_start : 0),
447 (int) sizeof (ElfW(Addr)) * 2,
448 (size_t) (val.s ? val.s->st_value : 0));
450 _dl_printf ("/%x %s\n", type_class, undef_name);
453 #endif