Make __ffs hidden
[glibc.git] / elf / dl-lookup.c
blob39f463eae1babdc04713bfca344ae83c9bb963cb
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2013 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <alloca.h>
20 #include <libintl.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
25 #include <dl-hash.h>
26 #include <dl-machine.h>
27 #include <sysdep-cancel.h>
28 #include <bits/libc-lock.h>
29 #include <tls.h>
30 #include <atomic.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, DSO_FILENAME (map->l_name),
116 map->l_ns);
118 /* If the hash table is empty there is nothing to do here. */
119 if (map->l_nbuckets == 0)
120 continue;
122 /* The tables for this map. */
123 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
124 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
127 /* Nested routine to check whether the symbol matches. */
128 const ElfW(Sym) *
129 __attribute_noinline__
130 check_match (const ElfW(Sym) *sym)
132 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
133 assert (ELF_RTYPE_CLASS_PLT == 1);
134 if (__builtin_expect ((sym->st_value == 0 /* No value. */
135 && stt != STT_TLS)
136 || (type_class & (sym->st_shndx == SHN_UNDEF)),
138 return NULL;
140 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
141 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
142 code/data definitions. */
143 #define ALLOWED_STT \
144 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
145 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
146 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
147 return NULL;
149 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
150 /* Not the symbol we are looking for. */
151 return NULL;
153 const ElfW(Half) *verstab = map->l_versyms;
154 if (version != NULL)
156 if (__builtin_expect (verstab == NULL, 0))
158 /* We need a versioned symbol but haven't found any. If
159 this is the object which is referenced in the verneed
160 entry it is a bug in the library since a symbol must
161 not simply disappear.
163 It would also be a bug in the object since it means that
164 the list of required versions is incomplete and so the
165 tests in dl-version.c haven't found a problem.*/
166 assert (version->filename == NULL
167 || ! _dl_name_match_p (version->filename, map));
169 /* Otherwise we accept the symbol. */
171 else
173 /* We can match the version information or use the
174 default one if it is not hidden. */
175 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
176 if ((map->l_versions[ndx].hash != version->hash
177 || strcmp (map->l_versions[ndx].name, version->name))
178 && (version->hidden || map->l_versions[ndx].hash
179 || (verstab[symidx] & 0x8000)))
180 /* It's not the version we want. */
181 return NULL;
184 else
186 /* No specific version is selected. There are two ways we
187 can got here:
189 - a binary which does not include versioning information
190 is loaded
192 - dlsym() instead of dlvsym() is used to get a symbol which
193 might exist in more than one form
195 If the library does not provide symbol version information
196 there is no problem at all: we simply use the symbol if it
197 is defined.
199 These two lookups need to be handled differently if the
200 library defines versions. In the case of the old
201 unversioned application the oldest (default) version
202 should be used. In case of a dlsym() call the latest and
203 public interface should be returned. */
204 if (verstab != NULL)
206 if ((verstab[symidx] & 0x7fff)
207 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
209 /* Don't accept hidden symbols. */
210 if ((verstab[symidx] & 0x8000) == 0
211 && num_versions++ == 0)
212 /* No version so far. */
213 versioned_sym = sym;
215 return NULL;
220 /* There cannot be another entry for this symbol so stop here. */
221 return sym;
224 const ElfW(Sym) *sym;
225 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
226 if (__builtin_expect (bitmask != NULL, 1))
228 ElfW(Addr) bitmask_word
229 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
230 & map->l_gnu_bitmask_idxbits];
232 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
233 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
234 & (__ELF_NATIVE_CLASS - 1));
236 if (__builtin_expect ((bitmask_word >> hashbit1)
237 & (bitmask_word >> hashbit2) & 1, 0))
239 Elf32_Word bucket = map->l_gnu_buckets[new_hash
240 % map->l_nbuckets];
241 if (bucket != 0)
243 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
246 if (((*hasharr ^ new_hash) >> 1) == 0)
248 symidx = hasharr - map->l_gnu_chain_zero;
249 sym = check_match (&symtab[symidx]);
250 if (sym != NULL)
251 goto found_it;
253 while ((*hasharr++ & 1u) == 0);
256 /* No symbol found. */
257 symidx = SHN_UNDEF;
259 else
261 if (*old_hash == 0xffffffff)
262 *old_hash = _dl_elf_hash (undef_name);
264 /* Use the old SysV-style hash table. Search the appropriate
265 hash bucket in this object's symbol table for a definition
266 for the same symbol name. */
267 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
268 symidx != STN_UNDEF;
269 symidx = map->l_chain[symidx])
271 sym = check_match (&symtab[symidx]);
272 if (sym != NULL)
273 goto found_it;
277 /* If we have seen exactly one versioned symbol while we are
278 looking for an unversioned symbol and the version is not the
279 default version we still accept this symbol since there are
280 no possible ambiguities. */
281 sym = num_versions == 1 ? versioned_sym : NULL;
283 if (sym != NULL)
285 found_it:
286 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
288 case STB_WEAK:
289 /* Weak definition. Use this value if we don't find another. */
290 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
292 if (! result->s)
294 result->s = sym;
295 result->m = (struct link_map *) map;
297 break;
299 /* FALLTHROUGH */
300 case STB_GLOBAL:
301 success:
302 /* Global definition. Just what we need. */
303 result->s = sym;
304 result->m = (struct link_map *) map;
305 return 1;
307 case STB_GNU_UNIQUE:;
308 /* We have to determine whether we already found a
309 symbol with this name before. If not then we have to
310 add it to the search table. If we already found a
311 definition we have to use it. */
312 void enter (struct unique_sym *table, size_t size,
313 unsigned int hash, const char *name,
314 const ElfW(Sym) *sym, const struct link_map *map)
316 size_t idx = hash % size;
317 size_t hash2 = 1 + hash % (size - 2);
318 while (table[idx].name != NULL)
320 idx += hash2;
321 if (idx >= size)
322 idx -= size;
325 table[idx].hashval = hash;
326 table[idx].name = name;
327 table[idx].sym = sym;
328 table[idx].map = map;
331 struct unique_sym_table *tab
332 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
334 __rtld_lock_lock_recursive (tab->lock);
336 struct unique_sym *entries = tab->entries;
337 size_t size = tab->size;
338 if (entries != NULL)
340 size_t idx = new_hash % size;
341 size_t hash2 = 1 + new_hash % (size - 2);
342 while (1)
344 if (entries[idx].hashval == new_hash
345 && strcmp (entries[idx].name, undef_name) == 0)
347 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
349 /* We possibly have to initialize the central
350 copy from the copy addressed through the
351 relocation. */
352 result->s = sym;
353 result->m = (struct link_map *) map;
355 else
357 result->s = entries[idx].sym;
358 result->m = (struct link_map *) entries[idx].map;
360 __rtld_lock_unlock_recursive (tab->lock);
361 return 1;
364 if (entries[idx].name == NULL)
365 break;
367 idx += hash2;
368 if (idx >= size)
369 idx -= size;
372 if (size * 3 <= tab->n_elements * 4)
374 /* Expand the table. */
375 #ifdef RTLD_CHECK_FOREIGN_CALL
376 /* This must not happen during runtime relocations. */
377 assert (!RTLD_CHECK_FOREIGN_CALL);
378 #endif
379 size_t newsize = _dl_higher_prime_number (size + 1);
380 struct unique_sym *newentries
381 = calloc (sizeof (struct unique_sym), newsize);
382 if (newentries == NULL)
384 nomem:
385 __rtld_lock_unlock_recursive (tab->lock);
386 _dl_fatal_printf ("out of memory\n");
389 for (idx = 0; idx < size; ++idx)
390 if (entries[idx].name != NULL)
391 enter (newentries, newsize, entries[idx].hashval,
392 entries[idx].name, entries[idx].sym,
393 entries[idx].map);
395 tab->free (entries);
396 tab->size = newsize;
397 size = newsize;
398 entries = tab->entries = newentries;
399 tab->free = free;
402 else
404 #ifdef RTLD_CHECK_FOREIGN_CALL
405 /* This must not happen during runtime relocations. */
406 assert (!RTLD_CHECK_FOREIGN_CALL);
407 #endif
409 #ifdef SHARED
410 /* If tab->entries is NULL, but tab->size is not, it means
411 this is the second, conflict finding, lookup for
412 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
413 allocate anything and don't enter anything into the
414 hash table. */
415 if (__builtin_expect (tab->size, 0))
417 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
418 __rtld_lock_unlock_recursive (tab->lock);
419 goto success;
421 #endif
423 #define INITIAL_NUNIQUE_SYM_TABLE 31
424 size = INITIAL_NUNIQUE_SYM_TABLE;
425 entries = calloc (sizeof (struct unique_sym), size);
426 if (entries == NULL)
427 goto nomem;
429 tab->entries = entries;
430 tab->size = size;
431 tab->free = free;
434 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
435 enter (entries, size, new_hash, strtab + sym->st_name, ref,
436 undef_map);
437 else
439 enter (entries, size, new_hash, strtab + sym->st_name, sym,
440 map);
442 if (map->l_type == lt_loaded)
443 /* Make sure we don't unload this object by
444 setting the appropriate flag. */
445 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
447 ++tab->n_elements;
449 __rtld_lock_unlock_recursive (tab->lock);
451 goto success;
453 default:
454 /* Local symbols are ignored. */
455 break;
459 /* If this current map is the one mentioned in the verneed entry
460 and we have not found a weak entry, it is a bug. */
461 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
462 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
463 return -1;
465 while (++i < n);
467 /* We have not found anything until now. */
468 return 0;
472 static uint_fast32_t
473 dl_new_hash (const char *s)
475 uint_fast32_t h = 5381;
476 for (unsigned char c = *s; c != '\0'; c = *++s)
477 h = h * 33 + c;
478 return h & 0xffffffff;
482 /* Add extra dependency on MAP to UNDEF_MAP. */
483 static int
484 internal_function
485 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
487 struct link_map *runp;
488 unsigned int i;
489 int result = 0;
491 /* Avoid self-references and references to objects which cannot be
492 unloaded anyway. */
493 if (undef_map == map)
494 return 0;
496 /* Avoid references to objects which cannot be unloaded anyway. */
497 assert (map->l_type == lt_loaded);
498 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
499 return 0;
501 struct link_map_reldeps *l_reldeps
502 = atomic_forced_read (undef_map->l_reldeps);
504 /* Make sure l_reldeps is read before l_initfini. */
505 atomic_read_barrier ();
507 /* Determine whether UNDEF_MAP already has a reference to MAP. First
508 look in the normal dependencies. */
509 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
510 if (l_initfini != NULL)
512 for (i = 0; l_initfini[i] != NULL; ++i)
513 if (l_initfini[i] == map)
514 return 0;
517 /* No normal dependency. See whether we already had to add it
518 to the special list of dynamic dependencies. */
519 unsigned int l_reldepsact = 0;
520 if (l_reldeps != NULL)
522 struct link_map **list = &l_reldeps->list[0];
523 l_reldepsact = l_reldeps->act;
524 for (i = 0; i < l_reldepsact; ++i)
525 if (list[i] == map)
526 return 0;
529 /* Save serial number of the target MAP. */
530 unsigned long long serial = map->l_serial;
532 /* Make sure nobody can unload the object while we are at it. */
533 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
535 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
536 here, that can result in ABBA deadlock. */
537 THREAD_GSCOPE_RESET_FLAG ();
538 __rtld_lock_lock_recursive (GL(dl_load_lock));
539 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
540 it can e.g. point to unallocated memory. So avoid the optimizer
541 treating the above read from MAP->l_serial as ensurance it
542 can safely dereference it. */
543 map = atomic_forced_read (map);
545 /* From this point on it is unsafe to dereference MAP, until it
546 has been found in one of the lists. */
548 /* Redo the l_initfini check in case undef_map's l_initfini
549 changed in the mean time. */
550 if (undef_map->l_initfini != l_initfini
551 && undef_map->l_initfini != NULL)
553 l_initfini = undef_map->l_initfini;
554 for (i = 0; l_initfini[i] != NULL; ++i)
555 if (l_initfini[i] == map)
556 goto out_check;
559 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
560 the mean time. */
561 if (undef_map->l_reldeps != NULL)
563 if (undef_map->l_reldeps != l_reldeps)
565 struct link_map **list = &undef_map->l_reldeps->list[0];
566 l_reldepsact = undef_map->l_reldeps->act;
567 for (i = 0; i < l_reldepsact; ++i)
568 if (list[i] == map)
569 goto out_check;
571 else if (undef_map->l_reldeps->act > l_reldepsact)
573 struct link_map **list
574 = &undef_map->l_reldeps->list[0];
575 i = l_reldepsact;
576 l_reldepsact = undef_map->l_reldeps->act;
577 for (; i < l_reldepsact; ++i)
578 if (list[i] == map)
579 goto out_check;
583 else
584 __rtld_lock_lock_recursive (GL(dl_load_lock));
586 /* The object is not yet in the dependency list. Before we add
587 it make sure just one more time the object we are about to
588 reference is still available. There is a brief period in
589 which the object could have been removed since we found the
590 definition. */
591 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
592 while (runp != NULL && runp != map)
593 runp = runp->l_next;
595 if (runp != NULL)
597 /* The object is still available. */
599 /* MAP could have been dlclosed, freed and then some other dlopened
600 library could have the same link_map pointer. */
601 if (map->l_serial != serial)
602 goto out_check;
604 /* Redo the NODELETE check, as when dl_load_lock wasn't held
605 yet this could have changed. */
606 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
607 goto out;
609 /* If the object with the undefined reference cannot be removed ever
610 just make sure the same is true for the object which contains the
611 definition. */
612 if (undef_map->l_type != lt_loaded
613 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
615 map->l_flags_1 |= DF_1_NODELETE;
616 goto out;
619 /* Add the reference now. */
620 if (__builtin_expect (l_reldepsact >= undef_map->l_reldepsmax, 0))
622 /* Allocate more memory for the dependency list. Since this
623 can never happen during the startup phase we can use
624 `realloc'. */
625 struct link_map_reldeps *newp;
626 unsigned int max
627 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
629 #ifdef RTLD_PREPARE_FOREIGN_CALL
630 RTLD_PREPARE_FOREIGN_CALL;
631 #endif
633 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
634 if (newp == NULL)
636 /* If we didn't manage to allocate memory for the list this is
637 no fatal problem. We simply make sure the referenced object
638 cannot be unloaded. This is semantically the correct
639 behavior. */
640 map->l_flags_1 |= DF_1_NODELETE;
641 goto out;
643 else
645 if (l_reldepsact)
646 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
647 l_reldepsact * sizeof (struct link_map *));
648 newp->list[l_reldepsact] = map;
649 newp->act = l_reldepsact + 1;
650 atomic_write_barrier ();
651 void *old = undef_map->l_reldeps;
652 undef_map->l_reldeps = newp;
653 undef_map->l_reldepsmax = max;
654 if (old)
655 _dl_scope_free (old);
658 else
660 undef_map->l_reldeps->list[l_reldepsact] = map;
661 atomic_write_barrier ();
662 undef_map->l_reldeps->act = l_reldepsact + 1;
665 /* Display information if we are debugging. */
666 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
667 _dl_debug_printf ("\
668 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
669 DSO_FILENAME (map->l_name),
670 map->l_ns,
671 DSO_FILENAME (undef_map->l_name),
672 undef_map->l_ns);
674 else
675 /* Whoa, that was bad luck. We have to search again. */
676 result = -1;
678 out:
679 /* Release the lock. */
680 __rtld_lock_unlock_recursive (GL(dl_load_lock));
682 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
683 THREAD_GSCOPE_SET_FLAG ();
685 return result;
687 out_check:
688 if (map->l_serial != serial)
689 result = -1;
690 goto out;
693 static void
694 internal_function
695 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
696 const ElfW(Sym) **ref, struct sym_val *value,
697 const struct r_found_version *version, int type_class,
698 int protected);
701 /* Search loaded objects' symbol tables for a definition of the symbol
702 UNDEF_NAME, perhaps with a requested version for the symbol.
704 We must never have calls to the audit functions inside this function
705 or in any function which gets called. If this would happen the audit
706 code might create a thread which can throw off all the scope locking. */
707 lookup_t
708 internal_function
709 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
710 const ElfW(Sym) **ref,
711 struct r_scope_elem *symbol_scope[],
712 const struct r_found_version *version,
713 int type_class, int flags, struct link_map *skip_map)
715 const uint_fast32_t new_hash = dl_new_hash (undef_name);
716 unsigned long int old_hash = 0xffffffff;
717 struct sym_val current_value = { NULL, NULL };
718 struct r_scope_elem **scope = symbol_scope;
720 bump_num_relocations ();
722 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
723 is allowed if we look up a versioned symbol. */
724 assert (version == NULL
725 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
726 == 0);
728 size_t i = 0;
729 if (__builtin_expect (skip_map != NULL, 0))
730 /* Search the relevant loaded objects for a definition. */
731 while ((*scope)->r_list[i] != skip_map)
732 ++i;
734 /* Search the relevant loaded objects for a definition. */
735 for (size_t start = i; *scope != NULL; start = 0, ++scope)
737 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
738 &current_value, *scope, start, version, flags,
739 skip_map, type_class, undef_map);
740 if (res > 0)
741 break;
743 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
745 /* Oh, oh. The file named in the relocation entry does not
746 contain the needed symbol. This code is never reached
747 for unversioned lookups. */
748 assert (version != NULL);
749 const char *reference_name = undef_map ? undef_map->l_name : NULL;
751 /* XXX We cannot translate the message. */
752 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
753 N_("relocation error"),
754 make_string ("symbol ", undef_name, ", version ",
755 version->name,
756 " not defined in file ",
757 version->filename,
758 " with link time reference",
759 res == -2
760 ? " (no version symbols)" : ""));
761 *ref = NULL;
762 return 0;
766 if (__builtin_expect (current_value.s == NULL, 0))
768 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
769 && skip_map == NULL
770 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
772 /* We could find no value for a strong reference. */
773 const char *reference_name = undef_map ? undef_map->l_name : "";
774 const char *versionstr = version ? ", version " : "";
775 const char *versionname = (version && version->name
776 ? version->name : "");
778 /* XXX We cannot translate the message. */
779 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
780 N_("symbol lookup error"),
781 make_string (undefined_msg, undef_name,
782 versionstr, versionname));
784 *ref = NULL;
785 return 0;
788 int protected = (*ref
789 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
790 if (__builtin_expect (protected != 0, 0))
792 /* It is very tricky. We need to figure out what value to
793 return for the protected symbol. */
794 if (type_class == ELF_RTYPE_CLASS_PLT)
796 if (current_value.s != NULL && current_value.m != undef_map)
798 current_value.s = *ref;
799 current_value.m = undef_map;
802 else
804 struct sym_val protected_value = { NULL, NULL };
806 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
807 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
808 &protected_value, *scope, i, version, flags,
809 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
810 break;
812 if (protected_value.s != NULL && protected_value.m != undef_map)
814 current_value.s = *ref;
815 current_value.m = undef_map;
820 /* We have to check whether this would bind UNDEF_MAP to an object
821 in the global scope which was dynamically loaded. In this case
822 we have to prevent the latter from being unloaded unless the
823 UNDEF_MAP object is also unloaded. */
824 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
825 /* Don't do this for explicit lookups as opposed to implicit
826 runtime lookups. */
827 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
828 /* Add UNDEF_MAP to the dependencies. */
829 && add_dependency (undef_map, current_value.m, flags) < 0)
830 /* Something went wrong. Perhaps the object we tried to reference
831 was just removed. Try finding another definition. */
832 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
833 (flags & DL_LOOKUP_GSCOPE_LOCK)
834 ? undef_map->l_scope : symbol_scope,
835 version, type_class, flags, skip_map);
837 /* The object is used. */
838 if (__builtin_expect (current_value.m->l_used == 0, 0))
839 current_value.m->l_used = 1;
841 if (__builtin_expect (GLRO(dl_debug_mask)
842 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
843 _dl_debug_bindings (undef_name, undef_map, ref,
844 &current_value, version, type_class, protected);
846 *ref = current_value.s;
847 return LOOKUP_VALUE (current_value.m);
851 /* Cache the location of MAP's hash table. */
853 void
854 internal_function
855 _dl_setup_hash (struct link_map *map)
857 Elf_Symndx *hash;
859 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
860 + DT_THISPROCNUM + DT_VERSIONTAGNUM
861 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
863 Elf32_Word *hash32
864 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
865 + DT_THISPROCNUM + DT_VERSIONTAGNUM
866 + DT_EXTRANUM + DT_VALNUM]);
867 map->l_nbuckets = *hash32++;
868 Elf32_Word symbias = *hash32++;
869 Elf32_Word bitmask_nwords = *hash32++;
870 /* Must be a power of two. */
871 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
872 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
873 map->l_gnu_shift = *hash32++;
875 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
876 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
878 map->l_gnu_buckets = hash32;
879 hash32 += map->l_nbuckets;
880 map->l_gnu_chain_zero = hash32 - symbias;
881 return;
884 if (!map->l_info[DT_HASH])
885 return;
886 hash = (void *) D_PTR (map, l_info[DT_HASH]);
888 map->l_nbuckets = *hash++;
889 /* Skip nchain. */
890 hash++;
891 map->l_buckets = hash;
892 hash += map->l_nbuckets;
893 map->l_chain = hash;
897 static void
898 internal_function
899 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
900 const ElfW(Sym) **ref, struct sym_val *value,
901 const struct r_found_version *version, int type_class,
902 int protected)
904 const char *reference_name = undef_map->l_name;
906 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
908 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
909 DSO_FILENAME (reference_name),
910 undef_map->l_ns,
911 DSO_FILENAME (value->m->l_name),
912 value->m->l_ns,
913 protected ? "protected" : "normal", undef_name);
914 if (version)
915 _dl_debug_printf_c (" [%s]\n", version->name);
916 else
917 _dl_debug_printf_c ("\n");
919 #ifdef SHARED
920 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
922 int conflict = 0;
923 struct sym_val val = { NULL, NULL };
925 if ((GLRO(dl_trace_prelink_map) == NULL
926 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
927 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
929 const uint_fast32_t new_hash = dl_new_hash (undef_name);
930 unsigned long int old_hash = 0xffffffff;
931 struct unique_sym *saved_entries
932 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
934 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
935 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
936 undef_map->l_local_scope[0], 0, version, 0, NULL,
937 type_class, undef_map);
938 if (val.s != value->s || val.m != value->m)
939 conflict = 1;
940 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
941 && val.s
942 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
943 STB_GLOBAL) == STB_GNU_UNIQUE)
945 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
946 contains any DT_SYMBOLIC libraries, unfortunately there
947 can be conflicts even if the above is equal. As symbol
948 resolution goes from the last library to the first and
949 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
950 library, it would be the one that is looked up. */
951 struct sym_val val2 = { NULL, NULL };
952 size_t n;
953 struct r_scope_elem *scope = undef_map->l_local_scope[0];
955 for (n = 0; n < scope->r_nlist; n++)
956 if (scope->r_list[n] == val.m)
957 break;
959 for (n++; n < scope->r_nlist; n++)
960 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
961 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
962 &val2,
963 &scope->r_list[n]->l_symbolic_searchlist,
964 0, version, 0, NULL, type_class,
965 undef_map) > 0)
967 conflict = 1;
968 val = val2;
969 break;
972 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
975 if (value->s)
977 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
978 == STT_TLS, 0))
979 type_class = 4;
980 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
981 == STT_GNU_IFUNC, 0))
982 type_class |= 8;
985 if (conflict
986 || GLRO(dl_trace_prelink_map) == undef_map
987 || GLRO(dl_trace_prelink_map) == NULL
988 || type_class >= 4)
990 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
991 conflict ? "conflict" : "lookup",
992 (int) sizeof (ElfW(Addr)) * 2,
993 (size_t) undef_map->l_map_start,
994 (int) sizeof (ElfW(Addr)) * 2,
995 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
996 (int) sizeof (ElfW(Addr)) * 2,
997 (size_t) (value->s ? value->m->l_map_start : 0),
998 (int) sizeof (ElfW(Addr)) * 2,
999 (size_t) (value->s ? value->s->st_value : 0));
1001 if (conflict)
1002 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1003 (int) sizeof (ElfW(Addr)) * 2,
1004 (size_t) (val.s ? val.m->l_map_start : 0),
1005 (int) sizeof (ElfW(Addr)) * 2,
1006 (size_t) (val.s ? val.s->st_value : 0));
1008 _dl_printf ("/%x %s\n", type_class, undef_name);
1011 #endif