Some more fixes for the unique symbol handling.
[glibc.git] / elf / dl-lookup.c
blob1333123b0f6973cc8bbf4a76301a61c7ebba6fd9
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2005, 2006, 2007, 2009 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 <sysdep-cancel.h>
29 #include <bits/libc-lock.h>
30 #include <tls.h>
32 #include <assert.h>
34 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
36 /* We need this string more than once. */
37 static const char undefined_msg[] = "undefined symbol: ";
40 struct sym_val
42 const ElfW(Sym) *s;
43 struct link_map *m;
47 #define make_string(string, rest...) \
48 ({ \
49 const char *all[] = { string, ## rest }; \
50 size_t len, cnt; \
51 char *result, *cp; \
53 len = 1; \
54 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
55 len += strlen (all[cnt]); \
57 cp = result = alloca (len); \
58 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
59 cp = __stpcpy (cp, all[cnt]); \
61 result; \
64 /* Statistics function. */
65 #ifdef SHARED
66 # define bump_num_relocations() ++GL(dl_num_relocations)
67 #else
68 # define bump_num_relocations() ((void) 0)
69 #endif
72 /* Inner part of the lookup functions. We return a value > 0 if we
73 found the symbol, the value 0 if nothing is found and < 0 if
74 something bad happened. */
75 static int
76 __attribute_noinline__
77 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
78 unsigned long int *old_hash, const ElfW(Sym) *ref,
79 struct sym_val *result, struct r_scope_elem *scope, size_t i,
80 const struct r_found_version *const version, int flags,
81 struct link_map *skip, int type_class, struct link_map *undef_map)
83 size_t n = scope->r_nlist;
84 /* Make sure we read the value before proceeding. Otherwise we
85 might use r_list pointing to the initial scope and r_nlist being
86 the value after a resize. That is the only path in dl-open.c not
87 protected by GSCOPE. A read barrier here might be to expensive. */
88 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
89 struct link_map **list = scope->r_list;
93 /* These variables are used in the nested function. */
94 Elf_Symndx symidx;
95 int num_versions = 0;
96 const ElfW(Sym) *versioned_sym = NULL;
98 const struct link_map *map = list[i]->l_real;
100 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
101 if (map == skip)
102 continue;
104 /* Don't search the executable when resolving a copy reloc. */
105 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
106 continue;
108 /* Do not look into objects which are going to be removed. */
109 if (map->l_removed)
110 continue;
112 /* Print some debugging info if wanted. */
113 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
114 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
115 undef_name,
116 map->l_name[0] ? map->l_name : rtld_progname,
117 map->l_ns);
119 /* If the hash table is empty there is nothing to do here. */
120 if (map->l_nbuckets == 0)
121 continue;
123 /* The tables for this map. */
124 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
125 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
128 /* Nested routine to check whether the symbol matches. */
129 const ElfW(Sym) *
130 __attribute_noinline__
131 check_match (const ElfW(Sym) *sym)
133 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
134 assert (ELF_RTYPE_CLASS_PLT == 1);
135 if (__builtin_expect ((sym->st_value == 0 /* No value. */
136 && stt != STT_TLS)
137 || (type_class & (sym->st_shndx == SHN_UNDEF)),
139 return NULL;
141 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
142 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
143 code/data definitions. */
144 #define ALLOWED_STT \
145 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
146 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
147 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
148 return NULL;
150 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
151 /* Not the symbol we are looking for. */
152 return NULL;
154 const ElfW(Half) *verstab = map->l_versyms;
155 if (version != NULL)
157 if (__builtin_expect (verstab == NULL, 0))
159 /* We need a versioned symbol but haven't found any. If
160 this is the object which is referenced in the verneed
161 entry it is a bug in the library since a symbol must
162 not simply disappear.
164 It would also be a bug in the object since it means that
165 the list of required versions is incomplete and so the
166 tests in dl-version.c haven't found a problem.*/
167 assert (version->filename == NULL
168 || ! _dl_name_match_p (version->filename, map));
170 /* Otherwise we accept the symbol. */
172 else
174 /* We can match the version information or use the
175 default one if it is not hidden. */
176 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
177 if ((map->l_versions[ndx].hash != version->hash
178 || strcmp (map->l_versions[ndx].name, version->name))
179 && (version->hidden || map->l_versions[ndx].hash
180 || (verstab[symidx] & 0x8000)))
181 /* It's not the version we want. */
182 return NULL;
185 else
187 /* No specific version is selected. There are two ways we
188 can got here:
190 - a binary which does not include versioning information
191 is loaded
193 - dlsym() instead of dlvsym() is used to get a symbol which
194 might exist in more than one form
196 If the library does not provide symbol version information
197 there is no problem at at: we simply use the symbol if it
198 is defined.
200 These two lookups need to be handled differently if the
201 library defines versions. In the case of the old
202 unversioned application the oldest (default) version
203 should be used. In case of a dlsym() call the latest and
204 public interface should be returned. */
205 if (verstab != NULL)
207 if ((verstab[symidx] & 0x7fff)
208 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
210 /* Don't accept hidden symbols. */
211 if ((verstab[symidx] & 0x8000) == 0
212 && num_versions++ == 0)
213 /* No version so far. */
214 versioned_sym = sym;
216 return NULL;
221 /* There cannot be another entry for this symbol so stop here. */
222 return sym;
225 const ElfW(Sym) *sym;
226 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
227 if (__builtin_expect (bitmask != NULL, 1))
229 ElfW(Addr) bitmask_word
230 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
231 & map->l_gnu_bitmask_idxbits];
233 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
234 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
235 & (__ELF_NATIVE_CLASS - 1));
237 if (__builtin_expect ((bitmask_word >> hashbit1)
238 & (bitmask_word >> hashbit2) & 1, 0))
240 Elf32_Word bucket = map->l_gnu_buckets[new_hash
241 % map->l_nbuckets];
242 if (bucket != 0)
244 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
247 if (((*hasharr ^ new_hash) >> 1) == 0)
249 symidx = hasharr - map->l_gnu_chain_zero;
250 sym = check_match (&symtab[symidx]);
251 if (sym != NULL)
252 goto found_it;
254 while ((*hasharr++ & 1u) == 0);
257 /* No symbol found. */
258 symidx = SHN_UNDEF;
260 else
262 if (*old_hash == 0xffffffff)
263 *old_hash = _dl_elf_hash (undef_name);
265 /* Use the old SysV-style hash table. Search the appropriate
266 hash bucket in this object's symbol table for a definition
267 for the same symbol name. */
268 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
269 symidx != STN_UNDEF;
270 symidx = map->l_chain[symidx])
272 sym = check_match (&symtab[symidx]);
273 if (sym != NULL)
274 goto found_it;
278 /* If we have seen exactly one versioned symbol while we are
279 looking for an unversioned symbol and the version is not the
280 default version we still accept this symbol since there are
281 no possible ambiguities. */
282 sym = num_versions == 1 ? versioned_sym : NULL;
284 if (sym != NULL)
286 found_it:
287 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
289 case STB_WEAK:
290 /* Weak definition. Use this value if we don't find another. */
291 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
293 if (! result->s)
295 result->s = sym;
296 result->m = (struct link_map *) map;
298 break;
300 /* FALLTHROUGH */
301 case STB_GLOBAL:
302 success:
303 /* Global definition. Just what we need. */
304 result->s = sym;
305 result->m = (struct link_map *) map;
306 return 1;
308 case STB_GNU_UNIQUE:;
309 /* We have to determine whether we already found a
310 symbol with this name before. If not then we have to
311 add it to the search table. If we already found a
312 definition we have to use it. */
313 void enter (struct unique_sym *table, size_t size,
314 unsigned int hash, const char *name,
315 const ElfW(Sym) *sym, struct link_map *map)
317 size_t idx = hash % size;
318 size_t hash2 = 1 + hash % (size - 2);
319 while (1)
321 if (table[idx].name == NULL)
323 table[idx].hashval = hash;
324 table[idx].name = strtab + sym->st_name;
325 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
327 table[idx].sym = ref;
328 table[idx].map = undef_map;
330 else
332 table[idx].sym = sym;
333 table[idx].map = map;
336 if (table[idx].map->l_type == lt_loaded)
337 /* Make sure we don't unload this object by
338 artificially increason the open count. */
339 table[idx].map->l_flags_1 |= DF_1_NODELETE;
341 return;
344 idx += hash2;
345 if (idx >= size)
346 idx -= size;
350 struct unique_sym_table *tab
351 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
353 __rtld_lock_lock_recursive (tab->lock);
355 struct unique_sym *entries = tab->entries;
356 size_t size = tab->size;
357 if (entries != NULL)
359 size_t idx = new_hash % size;
360 size_t hash2 = 1 + new_hash % (size - 2);
361 while (1)
363 if (entries[idx].hashval == new_hash
364 && strcmp (entries[idx].name, undef_name) == 0)
366 result->s = entries[idx].sym;
367 result->m = (struct link_map *) entries[idx].map;
368 __rtld_lock_unlock_recursive (tab->lock);
369 return 1;
372 if (entries[idx].name == NULL)
373 break;
375 idx += hash2;
376 if (idx >= size)
377 idx -= size;
380 if (size * 3 <= tab->n_elements)
382 /* Expand the table. */
383 size_t newsize = _dl_higher_prime_number (size);
384 struct unique_sym *newentries
385 = calloc (sizeof (struct unique_sym), newsize);
386 if (newentries == NULL)
388 nomem:
389 __rtld_lock_unlock_recursive (tab->lock);
390 _dl_fatal_printf ("out of memory\n");
393 for (idx = 0; idx < size; ++idx)
394 if (entries[idx].name != NULL)
395 enter (newentries, newsize, entries[idx].hashval,
396 entries[idx].name, entries[idx].sym,
397 entries[idx].map);
399 tab->free (entries);
400 tab->size = newsize;
401 entries = tab->entries = newentries;
402 tab->free = free;
405 else
407 #define INITIAL_NUNIQUE_SYM_TABLE 31
408 size = INITIAL_NUNIQUE_SYM_TABLE;
409 entries = calloc (sizeof (struct unique_sym), size);
410 if (entries == NULL)
411 goto nomem;
413 tab->entries = entries;
414 tab->size = size;
415 tab->free = free;
418 enter (entries, size, new_hash, strtab + sym->st_name, sym,
419 (struct link_map *) map);
420 ++tab->n_elements;
422 __rtld_lock_unlock_recursive (tab->lock);
424 goto success;
426 default:
427 /* Local symbols are ignored. */
428 break;
432 /* If this current map is the one mentioned in the verneed entry
433 and we have not found a weak entry, it is a bug. */
434 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
435 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
436 return -1;
438 while (++i < n);
440 /* We have not found anything until now. */
441 return 0;
445 static uint_fast32_t
446 dl_new_hash (const char *s)
448 uint_fast32_t h = 5381;
449 for (unsigned char c = *s; c != '\0'; c = *++s)
450 h = h * 33 + c;
451 return h & 0xffffffff;
455 /* Add extra dependency on MAP to UNDEF_MAP. */
456 static int
457 internal_function
458 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
460 struct link_map *runp;
461 unsigned int i;
462 int result = 0;
464 /* Avoid self-references and references to objects which cannot be
465 unloaded anyway. */
466 if (undef_map == map)
467 return 0;
469 /* Avoid references to objects which cannot be unloaded anyway. */
470 assert (map->l_type == lt_loaded);
471 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
472 return 0;
474 struct link_map_reldeps *l_reldeps
475 = atomic_forced_read (undef_map->l_reldeps);
477 /* Make sure l_reldeps is read before l_initfini. */
478 atomic_read_barrier ();
480 /* Determine whether UNDEF_MAP already has a reference to MAP. First
481 look in the normal dependencies. */
482 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
483 if (l_initfini != NULL)
485 for (i = 0; l_initfini[i] != NULL; ++i)
486 if (l_initfini[i] == map)
487 return 0;
490 /* No normal dependency. See whether we already had to add it
491 to the special list of dynamic dependencies. */
492 unsigned int l_reldepsact = 0;
493 if (l_reldeps != NULL)
495 struct link_map **list = &l_reldeps->list[0];
496 l_reldepsact = l_reldeps->act;
497 for (i = 0; i < l_reldepsact; ++i)
498 if (list[i] == map)
499 return 0;
502 /* Save serial number of the target MAP. */
503 unsigned long long serial = map->l_serial;
505 /* Make sure nobody can unload the object while we are at it. */
506 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
508 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
509 here, that can result in ABBA deadlock. */
510 THREAD_GSCOPE_RESET_FLAG ();
511 __rtld_lock_lock_recursive (GL(dl_load_lock));
512 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
513 it can e.g. point to unallocated memory. So avoid the optimizer
514 treating the above read from MAP->l_serial as ensurance it
515 can safely dereference it. */
516 map = atomic_forced_read (map);
518 /* From this point on it is unsafe to dereference MAP, until it
519 has been found in one of the lists. */
521 /* Redo the l_initfini check in case undef_map's l_initfini
522 changed in the mean time. */
523 if (undef_map->l_initfini != l_initfini
524 && undef_map->l_initfini != NULL)
526 l_initfini = undef_map->l_initfini;
527 for (i = 0; l_initfini[i] != NULL; ++i)
528 if (l_initfini[i] == map)
529 goto out_check;
532 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
533 the mean time. */
534 if (undef_map->l_reldeps != NULL)
536 if (undef_map->l_reldeps != l_reldeps)
538 struct link_map **list = &undef_map->l_reldeps->list[0];
539 l_reldepsact = undef_map->l_reldeps->act;
540 for (i = 0; i < l_reldepsact; ++i)
541 if (list[i] == map)
542 goto out_check;
544 else if (undef_map->l_reldeps->act > l_reldepsact)
546 struct link_map **list
547 = &undef_map->l_reldeps->list[0];
548 i = l_reldepsact;
549 l_reldepsact = undef_map->l_reldeps->act;
550 for (; i < l_reldepsact; ++i)
551 if (list[i] == map)
552 goto out_check;
556 else
557 __rtld_lock_lock_recursive (GL(dl_load_lock));
559 /* The object is not yet in the dependency list. Before we add
560 it make sure just one more time the object we are about to
561 reference is still available. There is a brief period in
562 which the object could have been removed since we found the
563 definition. */
564 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
565 while (runp != NULL && runp != map)
566 runp = runp->l_next;
568 if (runp != NULL)
570 /* The object is still available. */
572 /* MAP could have been dlclosed, freed and then some other dlopened
573 library could have the same link_map pointer. */
574 if (map->l_serial != serial)
575 goto out_check;
577 /* Redo the NODELETE check, as when dl_load_lock wasn't held
578 yet this could have changed. */
579 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
580 goto out;
582 /* If the object with the undefined reference cannot be removed ever
583 just make sure the same is true for the object which contains the
584 definition. */
585 if (undef_map->l_type != lt_loaded
586 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
588 map->l_flags_1 |= DF_1_NODELETE;
589 goto out;
592 /* Add the reference now. */
593 if (__builtin_expect (l_reldepsact >= undef_map->l_reldepsmax, 0))
595 /* Allocate more memory for the dependency list. Since this
596 can never happen during the startup phase we can use
597 `realloc'. */
598 struct link_map_reldeps *newp;
599 unsigned int max
600 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
602 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
603 if (newp == NULL)
605 /* If we didn't manage to allocate memory for the list this is
606 no fatal problem. We simply make sure the referenced object
607 cannot be unloaded. This is semantically the correct
608 behavior. */
609 map->l_flags_1 |= DF_1_NODELETE;
610 goto out;
612 else
614 if (l_reldepsact)
615 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
616 l_reldepsact * sizeof (struct link_map *));
617 newp->list[l_reldepsact] = map;
618 newp->act = l_reldepsact + 1;
619 atomic_write_barrier ();
620 void *old = undef_map->l_reldeps;
621 undef_map->l_reldeps = newp;
622 undef_map->l_reldepsmax = max;
623 if (old)
624 _dl_scope_free (old);
627 else
629 undef_map->l_reldeps->list[l_reldepsact] = map;
630 atomic_write_barrier ();
631 undef_map->l_reldeps->act = l_reldepsact + 1;
634 /* Display information if we are debugging. */
635 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
636 _dl_debug_printf ("\
637 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
638 map->l_name[0] ? map->l_name : rtld_progname,
639 map->l_ns,
640 undef_map->l_name[0]
641 ? undef_map->l_name : rtld_progname,
642 undef_map->l_ns);
644 else
645 /* Whoa, that was bad luck. We have to search again. */
646 result = -1;
648 out:
649 /* Release the lock. */
650 __rtld_lock_unlock_recursive (GL(dl_load_lock));
652 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
653 THREAD_GSCOPE_SET_FLAG ();
655 return result;
657 out_check:
658 if (map->l_serial != serial)
659 result = -1;
660 goto out;
663 static void
664 internal_function
665 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
666 const ElfW(Sym) **ref, struct sym_val *value,
667 const struct r_found_version *version, int type_class,
668 int protected);
671 /* Search loaded objects' symbol tables for a definition of the symbol
672 UNDEF_NAME, perhaps with a requested version for the symbol.
674 We must never have calls to the audit functions inside this function
675 or in any function which gets called. If this would happen the audit
676 code might create a thread which can throw off all the scope locking. */
677 lookup_t
678 internal_function
679 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
680 const ElfW(Sym) **ref,
681 struct r_scope_elem *symbol_scope[],
682 const struct r_found_version *version,
683 int type_class, int flags, struct link_map *skip_map)
685 const uint_fast32_t new_hash = dl_new_hash (undef_name);
686 unsigned long int old_hash = 0xffffffff;
687 struct sym_val current_value = { NULL, NULL };
688 struct r_scope_elem **scope = symbol_scope;
690 bump_num_relocations ();
692 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
693 is allowed if we look up a versioned symbol. */
694 assert (version == NULL
695 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
696 == 0);
698 size_t i = 0;
699 if (__builtin_expect (skip_map != NULL, 0))
700 /* Search the relevant loaded objects for a definition. */
701 while ((*scope)->r_list[i] != skip_map)
702 ++i;
704 /* Search the relevant loaded objects for a definition. */
705 for (size_t start = i; *scope != NULL; start = 0, ++scope)
707 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
708 &current_value, *scope, start, version, flags,
709 skip_map, type_class, undef_map);
710 if (res > 0)
711 break;
713 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
715 /* Oh, oh. The file named in the relocation entry does not
716 contain the needed symbol. This code is never reached
717 for unversioned lookups. */
718 assert (version != NULL);
719 const char *reference_name = undef_map ? undef_map->l_name : NULL;
721 /* XXX We cannot translate the message. */
722 _dl_signal_cerror (0, (reference_name[0]
723 ? reference_name
724 : (rtld_progname ?: "<main program>")),
725 N_("relocation error"),
726 make_string ("symbol ", undef_name, ", version ",
727 version->name,
728 " not defined in file ",
729 version->filename,
730 " with link time reference",
731 res == -2
732 ? " (no version symbols)" : ""));
733 *ref = NULL;
734 return 0;
738 if (__builtin_expect (current_value.s == NULL, 0))
740 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
741 && skip_map == NULL)
743 /* We could find no value for a strong reference. */
744 const char *reference_name = undef_map ? undef_map->l_name : "";
745 const char *versionstr = version ? ", version " : "";
746 const char *versionname = (version && version->name
747 ? version->name : "");
749 /* XXX We cannot translate the message. */
750 _dl_signal_cerror (0, (reference_name[0]
751 ? reference_name
752 : (rtld_progname ?: "<main program>")),
753 N_("symbol lookup error"),
754 make_string (undefined_msg, undef_name,
755 versionstr, versionname));
757 *ref = NULL;
758 return 0;
761 int protected = (*ref
762 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
763 if (__builtin_expect (protected != 0, 0))
765 /* It is very tricky. We need to figure out what value to
766 return for the protected symbol. */
767 if (type_class == ELF_RTYPE_CLASS_PLT)
769 if (current_value.s != NULL && current_value.m != undef_map)
771 current_value.s = *ref;
772 current_value.m = undef_map;
775 else
777 struct sym_val protected_value = { NULL, NULL };
779 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
780 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
781 &protected_value, *scope, i, version, flags,
782 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
783 break;
785 if (protected_value.s != NULL && protected_value.m != undef_map)
787 current_value.s = *ref;
788 current_value.m = undef_map;
793 /* We have to check whether this would bind UNDEF_MAP to an object
794 in the global scope which was dynamically loaded. In this case
795 we have to prevent the latter from being unloaded unless the
796 UNDEF_MAP object is also unloaded. */
797 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
798 /* Don't do this for explicit lookups as opposed to implicit
799 runtime lookups. */
800 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
801 /* Add UNDEF_MAP to the dependencies. */
802 && add_dependency (undef_map, current_value.m, flags) < 0)
803 /* Something went wrong. Perhaps the object we tried to reference
804 was just removed. Try finding another definition. */
805 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
806 (flags & DL_LOOKUP_GSCOPE_LOCK)
807 ? undef_map->l_scope : symbol_scope,
808 version, type_class, flags, skip_map);
810 /* The object is used. */
811 current_value.m->l_used = 1;
813 if (__builtin_expect (GLRO(dl_debug_mask)
814 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
815 _dl_debug_bindings (undef_name, undef_map, ref,
816 &current_value, version, type_class, protected);
818 *ref = current_value.s;
819 return LOOKUP_VALUE (current_value.m);
823 /* Cache the location of MAP's hash table. */
825 void
826 internal_function
827 _dl_setup_hash (struct link_map *map)
829 Elf_Symndx *hash;
830 Elf_Symndx nchain;
832 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
833 + DT_THISPROCNUM + DT_VERSIONTAGNUM
834 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
836 Elf32_Word *hash32
837 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
838 + DT_THISPROCNUM + DT_VERSIONTAGNUM
839 + DT_EXTRANUM + DT_VALNUM]);
840 map->l_nbuckets = *hash32++;
841 Elf32_Word symbias = *hash32++;
842 Elf32_Word bitmask_nwords = *hash32++;
843 /* Must be a power of two. */
844 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
845 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
846 map->l_gnu_shift = *hash32++;
848 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
849 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
851 map->l_gnu_buckets = hash32;
852 hash32 += map->l_nbuckets;
853 map->l_gnu_chain_zero = hash32 - symbias;
854 return;
857 if (!map->l_info[DT_HASH])
858 return;
859 hash = (void *) D_PTR (map, l_info[DT_HASH]);
861 map->l_nbuckets = *hash++;
862 nchain = *hash++;
863 map->l_buckets = hash;
864 hash += map->l_nbuckets;
865 map->l_chain = hash;
869 static void
870 internal_function
871 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
872 const ElfW(Sym) **ref, struct sym_val *value,
873 const struct r_found_version *version, int type_class,
874 int protected)
876 const char *reference_name = undef_map->l_name;
878 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
880 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
881 (reference_name[0]
882 ? reference_name
883 : (rtld_progname ?: "<main program>")),
884 undef_map->l_ns,
885 value->m->l_name[0] ? value->m->l_name : rtld_progname,
886 value->m->l_ns,
887 protected ? "protected" : "normal", undef_name);
888 if (version)
889 _dl_debug_printf_c (" [%s]\n", version->name);
890 else
891 _dl_debug_printf_c ("\n");
893 #ifdef SHARED
894 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
896 int conflict = 0;
897 struct sym_val val = { NULL, NULL };
899 if ((GLRO(dl_trace_prelink_map) == NULL
900 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
901 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
903 const uint_fast32_t new_hash = dl_new_hash (undef_name);
904 unsigned long int old_hash = 0xffffffff;
906 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
907 undef_map->l_local_scope[0], 0, version, 0, NULL,
908 type_class, undef_map);
910 if (val.s != value->s || val.m != value->m)
911 conflict = 1;
914 if (value->s)
916 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
917 == STT_TLS, 0))
918 type_class = 4;
919 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
920 == STT_GNU_IFUNC, 0))
921 type_class |= 8;
924 if (conflict
925 || GLRO(dl_trace_prelink_map) == undef_map
926 || GLRO(dl_trace_prelink_map) == NULL
927 || type_class >= 4)
929 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
930 conflict ? "conflict" : "lookup",
931 (int) sizeof (ElfW(Addr)) * 2,
932 (size_t) undef_map->l_map_start,
933 (int) sizeof (ElfW(Addr)) * 2,
934 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
935 (int) sizeof (ElfW(Addr)) * 2,
936 (size_t) (value->s ? value->m->l_map_start : 0),
937 (int) sizeof (ElfW(Addr)) * 2,
938 (size_t) (value->s ? value->s->st_value : 0));
940 if (conflict)
941 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
942 (int) sizeof (ElfW(Addr)) * 2,
943 (size_t) (val.s ? val.m->l_map_start : 0),
944 (int) sizeof (ElfW(Addr)) * 2,
945 (size_t) (val.s ? val.s->st_value : 0));
947 _dl_printf ("/%x %s\n", type_class, undef_name);
950 #endif