elf/dl-lookup.c: Move STB_GNU_UNIQUE handling to a function
[glibc.git] / elf / dl-lookup.c
blob896e1d27711ff0fae51b4f678ca2b06c14cd00f7
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2014 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 /* Return nonzero if check_match should consider SYM to fail to match a
35 symbol reference for some machine-specific reason. */
36 #ifndef ELF_MACHINE_SYM_NO_MATCH
37 # define ELF_MACHINE_SYM_NO_MATCH(sym) 0
38 #endif
40 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
42 /* We need this string more than once. */
43 static const char undefined_msg[] = "undefined symbol: ";
46 struct sym_val
48 const ElfW(Sym) *s;
49 struct link_map *m;
53 #define make_string(string, rest...) \
54 ({ \
55 const char *all[] = { string, ## rest }; \
56 size_t len, cnt; \
57 char *result, *cp; \
59 len = 1; \
60 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
61 len += strlen (all[cnt]); \
63 cp = result = alloca (len); \
64 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
65 cp = __stpcpy (cp, all[cnt]); \
67 result; \
70 /* Statistics function. */
71 #ifdef SHARED
72 # define bump_num_relocations() ++GL(dl_num_relocations)
73 #else
74 # define bump_num_relocations() ((void) 0)
75 #endif
77 /* Utility function for do_lookup_x. The caller is called with undef_name,
78 ref, version, flags and type_class, and those are passed as the first
79 five arguments. The caller then computes sym, symidx, strtab, and map
80 and passes them as the next four arguments. Lastly the caller passes in
81 versioned_sym and num_versions which are modified by check_match during
82 the checking process. */
83 static const ElfW(Sym) *
84 check_match (const char *const undef_name,
85 const ElfW(Sym) *const ref,
86 const struct r_found_version *const version,
87 const int flags,
88 const int type_class,
89 const ElfW(Sym) *const sym,
90 const Elf_Symndx symidx,
91 const char *const strtab,
92 const struct link_map *const map,
93 const ElfW(Sym) **const versioned_sym,
94 int *const num_versions)
96 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
97 assert (ELF_RTYPE_CLASS_PLT == 1);
98 if (__builtin_expect ((sym->st_value == 0 /* No value. */
99 && stt != STT_TLS)
100 || ELF_MACHINE_SYM_NO_MATCH (sym)
101 || (type_class & (sym->st_shndx == SHN_UNDEF)),
103 return NULL;
105 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
106 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
107 code/data definitions. */
108 #define ALLOWED_STT \
109 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
110 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
111 if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
112 return NULL;
114 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
115 /* Not the symbol we are looking for. */
116 return NULL;
118 const ElfW(Half) *verstab = map->l_versyms;
119 if (version != NULL)
121 if (__glibc_unlikely (verstab == NULL))
123 /* We need a versioned symbol but haven't found any. If
124 this is the object which is referenced in the verneed
125 entry it is a bug in the library since a symbol must
126 not simply disappear.
128 It would also be a bug in the object since it means that
129 the list of required versions is incomplete and so the
130 tests in dl-version.c haven't found a problem.*/
131 assert (version->filename == NULL
132 || ! _dl_name_match_p (version->filename, map));
134 /* Otherwise we accept the symbol. */
136 else
138 /* We can match the version information or use the
139 default one if it is not hidden. */
140 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
141 if ((map->l_versions[ndx].hash != version->hash
142 || strcmp (map->l_versions[ndx].name, version->name))
143 && (version->hidden || map->l_versions[ndx].hash
144 || (verstab[symidx] & 0x8000)))
145 /* It's not the version we want. */
146 return NULL;
149 else
151 /* No specific version is selected. There are two ways we
152 can got here:
154 - a binary which does not include versioning information
155 is loaded
157 - dlsym() instead of dlvsym() is used to get a symbol which
158 might exist in more than one form
160 If the library does not provide symbol version information
161 there is no problem at all: we simply use the symbol if it
162 is defined.
164 These two lookups need to be handled differently if the
165 library defines versions. In the case of the old
166 unversioned application the oldest (default) version
167 should be used. In case of a dlsym() call the latest and
168 public interface should be returned. */
169 if (verstab != NULL)
171 if ((verstab[symidx] & 0x7fff)
172 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
174 /* Don't accept hidden symbols. */
175 if ((verstab[symidx] & 0x8000) == 0
176 && (*num_versions)++ == 0)
177 /* No version so far. */
178 *versioned_sym = sym;
180 return NULL;
185 /* There cannot be another entry for this symbol so stop here. */
186 return sym;
189 /* Utility function for do_lookup_x. Lookup an STB_GNU_UNIQUE symbol
190 in the unique symbol table, creating a new entry if necessary.
191 Return the matching symbol in RESULT. */
192 static void
193 do_lookup_unique (const char *undef_name, uint_fast32_t new_hash,
194 const struct link_map *map, struct sym_val *result,
195 int type_class, const ElfW(Sym) *sym, const char *strtab,
196 const ElfW(Sym) *ref, const struct link_map *undef_map)
198 /* We have to determine whether we already found a
199 symbol with this name before. If not then we have to
200 add it to the search table. If we already found a
201 definition we have to use it. */
202 void enter (struct unique_sym *table, size_t size,
203 unsigned int hash, const char *name,
204 const ElfW(Sym) *sym, const struct link_map *map)
206 size_t idx = hash % size;
207 size_t hash2 = 1 + hash % (size - 2);
208 while (table[idx].name != NULL)
210 idx += hash2;
211 if (idx >= size)
212 idx -= size;
215 table[idx].hashval = hash;
216 table[idx].name = name;
217 table[idx].sym = sym;
218 table[idx].map = map;
221 struct unique_sym_table *tab
222 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
224 __rtld_lock_lock_recursive (tab->lock);
226 struct unique_sym *entries = tab->entries;
227 size_t size = tab->size;
228 if (entries != NULL)
230 size_t idx = new_hash % size;
231 size_t hash2 = 1 + new_hash % (size - 2);
232 while (1)
234 if (entries[idx].hashval == new_hash
235 && strcmp (entries[idx].name, undef_name) == 0)
237 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
239 /* We possibly have to initialize the central
240 copy from the copy addressed through the
241 relocation. */
242 result->s = sym;
243 result->m = (struct link_map *) map;
245 else
247 result->s = entries[idx].sym;
248 result->m = (struct link_map *) entries[idx].map;
250 __rtld_lock_unlock_recursive (tab->lock);
251 return;
254 if (entries[idx].name == NULL)
255 break;
257 idx += hash2;
258 if (idx >= size)
259 idx -= size;
262 if (size * 3 <= tab->n_elements * 4)
264 /* Expand the table. */
265 #ifdef RTLD_CHECK_FOREIGN_CALL
266 /* This must not happen during runtime relocations. */
267 assert (!RTLD_CHECK_FOREIGN_CALL);
268 #endif
269 size_t newsize = _dl_higher_prime_number (size + 1);
270 struct unique_sym *newentries
271 = calloc (sizeof (struct unique_sym), newsize);
272 if (newentries == NULL)
274 nomem:
275 __rtld_lock_unlock_recursive (tab->lock);
276 _dl_fatal_printf ("out of memory\n");
279 for (idx = 0; idx < size; ++idx)
280 if (entries[idx].name != NULL)
281 enter (newentries, newsize, entries[idx].hashval,
282 entries[idx].name, entries[idx].sym,
283 entries[idx].map);
285 tab->free (entries);
286 tab->size = newsize;
287 size = newsize;
288 entries = tab->entries = newentries;
289 tab->free = free;
292 else
294 #ifdef RTLD_CHECK_FOREIGN_CALL
295 /* This must not happen during runtime relocations. */
296 assert (!RTLD_CHECK_FOREIGN_CALL);
297 #endif
299 #ifdef SHARED
300 /* If tab->entries is NULL, but tab->size is not, it means
301 this is the second, conflict finding, lookup for
302 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
303 allocate anything and don't enter anything into the
304 hash table. */
305 if (__glibc_unlikely (tab->size))
307 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
308 goto success;
310 #endif
312 #define INITIAL_NUNIQUE_SYM_TABLE 31
313 size = INITIAL_NUNIQUE_SYM_TABLE;
314 entries = calloc (sizeof (struct unique_sym), size);
315 if (entries == NULL)
316 goto nomem;
318 tab->entries = entries;
319 tab->size = size;
320 tab->free = free;
323 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
324 enter (entries, size, new_hash, strtab + sym->st_name, ref,
325 undef_map);
326 else
328 enter (entries, size, new_hash, strtab + sym->st_name, sym, map);
330 if (map->l_type == lt_loaded)
331 /* Make sure we don't unload this object by
332 setting the appropriate flag. */
333 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
335 ++tab->n_elements;
337 #ifdef SHARED
338 success:
339 #endif
340 __rtld_lock_unlock_recursive (tab->lock);
342 result->s = sym;
343 result->m = (struct link_map *) map;
346 /* Inner part of the lookup functions. We return a value > 0 if we
347 found the symbol, the value 0 if nothing is found and < 0 if
348 something bad happened. */
349 static int
350 __attribute_noinline__
351 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
352 unsigned long int *old_hash, const ElfW(Sym) *ref,
353 struct sym_val *result, struct r_scope_elem *scope, size_t i,
354 const struct r_found_version *const version, int flags,
355 struct link_map *skip, int type_class, struct link_map *undef_map)
357 size_t n = scope->r_nlist;
358 /* Make sure we read the value before proceeding. Otherwise we
359 might use r_list pointing to the initial scope and r_nlist being
360 the value after a resize. That is the only path in dl-open.c not
361 protected by GSCOPE. A read barrier here might be to expensive. */
362 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
363 struct link_map **list = scope->r_list;
367 const struct link_map *map = list[i]->l_real;
369 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
370 if (map == skip)
371 continue;
373 /* Don't search the executable when resolving a copy reloc. */
374 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
375 continue;
377 /* Do not look into objects which are going to be removed. */
378 if (map->l_removed)
379 continue;
381 /* Print some debugging info if wanted. */
382 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
383 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
384 undef_name, DSO_FILENAME (map->l_name),
385 map->l_ns);
387 /* If the hash table is empty there is nothing to do here. */
388 if (map->l_nbuckets == 0)
389 continue;
391 Elf_Symndx symidx;
392 int num_versions = 0;
393 const ElfW(Sym) *versioned_sym = NULL;
395 /* The tables for this map. */
396 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
397 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
399 const ElfW(Sym) *sym;
400 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
401 if (__glibc_likely (bitmask != NULL))
403 ElfW(Addr) bitmask_word
404 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
405 & map->l_gnu_bitmask_idxbits];
407 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
408 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
409 & (__ELF_NATIVE_CLASS - 1));
411 if (__builtin_expect ((bitmask_word >> hashbit1)
412 & (bitmask_word >> hashbit2) & 1, 0))
414 Elf32_Word bucket = map->l_gnu_buckets[new_hash
415 % map->l_nbuckets];
416 if (bucket != 0)
418 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
421 if (((*hasharr ^ new_hash) >> 1) == 0)
423 symidx = hasharr - map->l_gnu_chain_zero;
424 sym = check_match (undef_name, ref, version, flags,
425 type_class, &symtab[symidx], symidx,
426 strtab, map, &versioned_sym,
427 &num_versions);
428 if (sym != NULL)
429 goto found_it;
431 while ((*hasharr++ & 1u) == 0);
434 /* No symbol found. */
435 symidx = SHN_UNDEF;
437 else
439 if (*old_hash == 0xffffffff)
440 *old_hash = _dl_elf_hash (undef_name);
442 /* Use the old SysV-style hash table. Search the appropriate
443 hash bucket in this object's symbol table for a definition
444 for the same symbol name. */
445 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
446 symidx != STN_UNDEF;
447 symidx = map->l_chain[symidx])
449 sym = check_match (undef_name, ref, version, flags,
450 type_class, &symtab[symidx], symidx,
451 strtab, map, &versioned_sym,
452 &num_versions);
453 if (sym != NULL)
454 goto found_it;
458 /* If we have seen exactly one versioned symbol while we are
459 looking for an unversioned symbol and the version is not the
460 default version we still accept this symbol since there are
461 no possible ambiguities. */
462 sym = num_versions == 1 ? versioned_sym : NULL;
464 if (sym != NULL)
466 found_it:
467 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
469 case STB_WEAK:
470 /* Weak definition. Use this value if we don't find another. */
471 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
473 if (! result->s)
475 result->s = sym;
476 result->m = (struct link_map *) map;
478 break;
480 /* FALLTHROUGH */
481 case STB_GLOBAL:
482 /* Global definition. Just what we need. */
483 result->s = sym;
484 result->m = (struct link_map *) map;
485 return 1;
487 case STB_GNU_UNIQUE:;
488 do_lookup_unique (undef_name, new_hash, map, result, type_class,
489 sym, strtab, ref, undef_map);
490 return 1;
492 default:
493 /* Local symbols are ignored. */
494 break;
498 /* If this current map is the one mentioned in the verneed entry
499 and we have not found a weak entry, it is a bug. */
500 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
501 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
502 return -1;
504 while (++i < n);
506 /* We have not found anything until now. */
507 return 0;
511 static uint_fast32_t
512 dl_new_hash (const char *s)
514 uint_fast32_t h = 5381;
515 for (unsigned char c = *s; c != '\0'; c = *++s)
516 h = h * 33 + c;
517 return h & 0xffffffff;
521 /* Add extra dependency on MAP to UNDEF_MAP. */
522 static int
523 internal_function
524 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
526 struct link_map *runp;
527 unsigned int i;
528 int result = 0;
530 /* Avoid self-references and references to objects which cannot be
531 unloaded anyway. */
532 if (undef_map == map)
533 return 0;
535 /* Avoid references to objects which cannot be unloaded anyway. */
536 assert (map->l_type == lt_loaded);
537 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
538 return 0;
540 struct link_map_reldeps *l_reldeps
541 = atomic_forced_read (undef_map->l_reldeps);
543 /* Make sure l_reldeps is read before l_initfini. */
544 atomic_read_barrier ();
546 /* Determine whether UNDEF_MAP already has a reference to MAP. First
547 look in the normal dependencies. */
548 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
549 if (l_initfini != NULL)
551 for (i = 0; l_initfini[i] != NULL; ++i)
552 if (l_initfini[i] == map)
553 return 0;
556 /* No normal dependency. See whether we already had to add it
557 to the special list of dynamic dependencies. */
558 unsigned int l_reldepsact = 0;
559 if (l_reldeps != NULL)
561 struct link_map **list = &l_reldeps->list[0];
562 l_reldepsact = l_reldeps->act;
563 for (i = 0; i < l_reldepsact; ++i)
564 if (list[i] == map)
565 return 0;
568 /* Save serial number of the target MAP. */
569 unsigned long long serial = map->l_serial;
571 /* Make sure nobody can unload the object while we are at it. */
572 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
574 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
575 here, that can result in ABBA deadlock. */
576 THREAD_GSCOPE_RESET_FLAG ();
577 __rtld_lock_lock_recursive (GL(dl_load_lock));
578 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
579 it can e.g. point to unallocated memory. So avoid the optimizer
580 treating the above read from MAP->l_serial as ensurance it
581 can safely dereference it. */
582 map = atomic_forced_read (map);
584 /* From this point on it is unsafe to dereference MAP, until it
585 has been found in one of the lists. */
587 /* Redo the l_initfini check in case undef_map's l_initfini
588 changed in the mean time. */
589 if (undef_map->l_initfini != l_initfini
590 && undef_map->l_initfini != NULL)
592 l_initfini = undef_map->l_initfini;
593 for (i = 0; l_initfini[i] != NULL; ++i)
594 if (l_initfini[i] == map)
595 goto out_check;
598 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
599 the mean time. */
600 if (undef_map->l_reldeps != NULL)
602 if (undef_map->l_reldeps != l_reldeps)
604 struct link_map **list = &undef_map->l_reldeps->list[0];
605 l_reldepsact = undef_map->l_reldeps->act;
606 for (i = 0; i < l_reldepsact; ++i)
607 if (list[i] == map)
608 goto out_check;
610 else if (undef_map->l_reldeps->act > l_reldepsact)
612 struct link_map **list
613 = &undef_map->l_reldeps->list[0];
614 i = l_reldepsact;
615 l_reldepsact = undef_map->l_reldeps->act;
616 for (; i < l_reldepsact; ++i)
617 if (list[i] == map)
618 goto out_check;
622 else
623 __rtld_lock_lock_recursive (GL(dl_load_lock));
625 /* The object is not yet in the dependency list. Before we add
626 it make sure just one more time the object we are about to
627 reference is still available. There is a brief period in
628 which the object could have been removed since we found the
629 definition. */
630 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
631 while (runp != NULL && runp != map)
632 runp = runp->l_next;
634 if (runp != NULL)
636 /* The object is still available. */
638 /* MAP could have been dlclosed, freed and then some other dlopened
639 library could have the same link_map pointer. */
640 if (map->l_serial != serial)
641 goto out_check;
643 /* Redo the NODELETE check, as when dl_load_lock wasn't held
644 yet this could have changed. */
645 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
646 goto out;
648 /* If the object with the undefined reference cannot be removed ever
649 just make sure the same is true for the object which contains the
650 definition. */
651 if (undef_map->l_type != lt_loaded
652 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
654 map->l_flags_1 |= DF_1_NODELETE;
655 goto out;
658 /* Add the reference now. */
659 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
661 /* Allocate more memory for the dependency list. Since this
662 can never happen during the startup phase we can use
663 `realloc'. */
664 struct link_map_reldeps *newp;
665 unsigned int max
666 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
668 #ifdef RTLD_PREPARE_FOREIGN_CALL
669 RTLD_PREPARE_FOREIGN_CALL;
670 #endif
672 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
673 if (newp == NULL)
675 /* If we didn't manage to allocate memory for the list this is
676 no fatal problem. We simply make sure the referenced object
677 cannot be unloaded. This is semantically the correct
678 behavior. */
679 map->l_flags_1 |= DF_1_NODELETE;
680 goto out;
682 else
684 if (l_reldepsact)
685 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
686 l_reldepsact * sizeof (struct link_map *));
687 newp->list[l_reldepsact] = map;
688 newp->act = l_reldepsact + 1;
689 atomic_write_barrier ();
690 void *old = undef_map->l_reldeps;
691 undef_map->l_reldeps = newp;
692 undef_map->l_reldepsmax = max;
693 if (old)
694 _dl_scope_free (old);
697 else
699 undef_map->l_reldeps->list[l_reldepsact] = map;
700 atomic_write_barrier ();
701 undef_map->l_reldeps->act = l_reldepsact + 1;
704 /* Display information if we are debugging. */
705 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
706 _dl_debug_printf ("\
707 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
708 DSO_FILENAME (map->l_name),
709 map->l_ns,
710 DSO_FILENAME (undef_map->l_name),
711 undef_map->l_ns);
713 else
714 /* Whoa, that was bad luck. We have to search again. */
715 result = -1;
717 out:
718 /* Release the lock. */
719 __rtld_lock_unlock_recursive (GL(dl_load_lock));
721 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
722 THREAD_GSCOPE_SET_FLAG ();
724 return result;
726 out_check:
727 if (map->l_serial != serial)
728 result = -1;
729 goto out;
732 static void
733 internal_function
734 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
735 const ElfW(Sym) **ref, struct sym_val *value,
736 const struct r_found_version *version, int type_class,
737 int protected);
740 /* Search loaded objects' symbol tables for a definition of the symbol
741 UNDEF_NAME, perhaps with a requested version for the symbol.
743 We must never have calls to the audit functions inside this function
744 or in any function which gets called. If this would happen the audit
745 code might create a thread which can throw off all the scope locking. */
746 lookup_t
747 internal_function
748 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
749 const ElfW(Sym) **ref,
750 struct r_scope_elem *symbol_scope[],
751 const struct r_found_version *version,
752 int type_class, int flags, struct link_map *skip_map)
754 const uint_fast32_t new_hash = dl_new_hash (undef_name);
755 unsigned long int old_hash = 0xffffffff;
756 struct sym_val current_value = { NULL, NULL };
757 struct r_scope_elem **scope = symbol_scope;
759 bump_num_relocations ();
761 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
762 is allowed if we look up a versioned symbol. */
763 assert (version == NULL
764 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
765 == 0);
767 size_t i = 0;
768 if (__glibc_unlikely (skip_map != NULL))
769 /* Search the relevant loaded objects for a definition. */
770 while ((*scope)->r_list[i] != skip_map)
771 ++i;
773 /* Search the relevant loaded objects for a definition. */
774 for (size_t start = i; *scope != NULL; start = 0, ++scope)
776 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
777 &current_value, *scope, start, version, flags,
778 skip_map, type_class, undef_map);
779 if (res > 0)
780 break;
782 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
784 /* Oh, oh. The file named in the relocation entry does not
785 contain the needed symbol. This code is never reached
786 for unversioned lookups. */
787 assert (version != NULL);
788 const char *reference_name = undef_map ? undef_map->l_name : "";
790 /* XXX We cannot translate the message. */
791 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
792 N_("relocation error"),
793 make_string ("symbol ", undef_name, ", version ",
794 version->name,
795 " not defined in file ",
796 version->filename,
797 " with link time reference",
798 res == -2
799 ? " (no version symbols)" : ""));
800 *ref = NULL;
801 return 0;
805 if (__glibc_unlikely (current_value.s == NULL))
807 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
808 && skip_map == NULL
809 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
811 /* We could find no value for a strong reference. */
812 const char *reference_name = undef_map ? undef_map->l_name : "";
813 const char *versionstr = version ? ", version " : "";
814 const char *versionname = (version && version->name
815 ? version->name : "");
817 /* XXX We cannot translate the message. */
818 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
819 N_("symbol lookup error"),
820 make_string (undefined_msg, undef_name,
821 versionstr, versionname));
823 *ref = NULL;
824 return 0;
827 int protected = (*ref
828 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
829 if (__glibc_unlikely (protected != 0))
831 /* It is very tricky. We need to figure out what value to
832 return for the protected symbol. */
833 if (type_class == ELF_RTYPE_CLASS_PLT)
835 if (current_value.s != NULL && current_value.m != undef_map)
837 current_value.s = *ref;
838 current_value.m = undef_map;
841 else
843 struct sym_val protected_value = { NULL, NULL };
845 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
846 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
847 &protected_value, *scope, i, version, flags,
848 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
849 break;
851 if (protected_value.s != NULL && protected_value.m != undef_map)
853 current_value.s = *ref;
854 current_value.m = undef_map;
859 /* We have to check whether this would bind UNDEF_MAP to an object
860 in the global scope which was dynamically loaded. In this case
861 we have to prevent the latter from being unloaded unless the
862 UNDEF_MAP object is also unloaded. */
863 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
864 /* Don't do this for explicit lookups as opposed to implicit
865 runtime lookups. */
866 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
867 /* Add UNDEF_MAP to the dependencies. */
868 && add_dependency (undef_map, current_value.m, flags) < 0)
869 /* Something went wrong. Perhaps the object we tried to reference
870 was just removed. Try finding another definition. */
871 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
872 (flags & DL_LOOKUP_GSCOPE_LOCK)
873 ? undef_map->l_scope : symbol_scope,
874 version, type_class, flags, skip_map);
876 /* The object is used. */
877 if (__glibc_unlikely (current_value.m->l_used == 0))
878 current_value.m->l_used = 1;
880 if (__builtin_expect (GLRO(dl_debug_mask)
881 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
882 _dl_debug_bindings (undef_name, undef_map, ref,
883 &current_value, version, type_class, protected);
885 *ref = current_value.s;
886 return LOOKUP_VALUE (current_value.m);
890 /* Cache the location of MAP's hash table. */
892 void
893 internal_function
894 _dl_setup_hash (struct link_map *map)
896 Elf_Symndx *hash;
898 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
899 + DT_THISPROCNUM + DT_VERSIONTAGNUM
900 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
902 Elf32_Word *hash32
903 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
904 + DT_THISPROCNUM + DT_VERSIONTAGNUM
905 + DT_EXTRANUM + DT_VALNUM]);
906 map->l_nbuckets = *hash32++;
907 Elf32_Word symbias = *hash32++;
908 Elf32_Word bitmask_nwords = *hash32++;
909 /* Must be a power of two. */
910 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
911 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
912 map->l_gnu_shift = *hash32++;
914 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
915 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
917 map->l_gnu_buckets = hash32;
918 hash32 += map->l_nbuckets;
919 map->l_gnu_chain_zero = hash32 - symbias;
920 return;
923 if (!map->l_info[DT_HASH])
924 return;
925 hash = (void *) D_PTR (map, l_info[DT_HASH]);
927 map->l_nbuckets = *hash++;
928 /* Skip nchain. */
929 hash++;
930 map->l_buckets = hash;
931 hash += map->l_nbuckets;
932 map->l_chain = hash;
936 static void
937 internal_function
938 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
939 const ElfW(Sym) **ref, struct sym_val *value,
940 const struct r_found_version *version, int type_class,
941 int protected)
943 const char *reference_name = undef_map->l_name;
945 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
947 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
948 DSO_FILENAME (reference_name),
949 undef_map->l_ns,
950 DSO_FILENAME (value->m->l_name),
951 value->m->l_ns,
952 protected ? "protected" : "normal", undef_name);
953 if (version)
954 _dl_debug_printf_c (" [%s]\n", version->name);
955 else
956 _dl_debug_printf_c ("\n");
958 #ifdef SHARED
959 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
961 int conflict = 0;
962 struct sym_val val = { NULL, NULL };
964 if ((GLRO(dl_trace_prelink_map) == NULL
965 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
966 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
968 const uint_fast32_t new_hash = dl_new_hash (undef_name);
969 unsigned long int old_hash = 0xffffffff;
970 struct unique_sym *saved_entries
971 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
973 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
974 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
975 undef_map->l_local_scope[0], 0, version, 0, NULL,
976 type_class, undef_map);
977 if (val.s != value->s || val.m != value->m)
978 conflict = 1;
979 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
980 && val.s
981 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
982 STB_GLOBAL) == STB_GNU_UNIQUE)
984 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
985 contains any DT_SYMBOLIC libraries, unfortunately there
986 can be conflicts even if the above is equal. As symbol
987 resolution goes from the last library to the first and
988 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
989 library, it would be the one that is looked up. */
990 struct sym_val val2 = { NULL, NULL };
991 size_t n;
992 struct r_scope_elem *scope = undef_map->l_local_scope[0];
994 for (n = 0; n < scope->r_nlist; n++)
995 if (scope->r_list[n] == val.m)
996 break;
998 for (n++; n < scope->r_nlist; n++)
999 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
1000 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
1001 &val2,
1002 &scope->r_list[n]->l_symbolic_searchlist,
1003 0, version, 0, NULL, type_class,
1004 undef_map) > 0)
1006 conflict = 1;
1007 val = val2;
1008 break;
1011 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
1014 if (value->s)
1016 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1017 == STT_TLS, 0))
1018 type_class = 4;
1019 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1020 == STT_GNU_IFUNC, 0))
1021 type_class |= 8;
1024 if (conflict
1025 || GLRO(dl_trace_prelink_map) == undef_map
1026 || GLRO(dl_trace_prelink_map) == NULL
1027 || type_class >= 4)
1029 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1030 conflict ? "conflict" : "lookup",
1031 (int) sizeof (ElfW(Addr)) * 2,
1032 (size_t) undef_map->l_map_start,
1033 (int) sizeof (ElfW(Addr)) * 2,
1034 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1035 (int) sizeof (ElfW(Addr)) * 2,
1036 (size_t) (value->s ? value->m->l_map_start : 0),
1037 (int) sizeof (ElfW(Addr)) * 2,
1038 (size_t) (value->s ? value->s->st_value : 0));
1040 if (conflict)
1041 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1042 (int) sizeof (ElfW(Addr)) * 2,
1043 (size_t) (val.s ? val.m->l_map_start : 0),
1044 (int) sizeof (ElfW(Addr)) * 2,
1045 (size_t) (val.s ? val.s->st_value : 0));
1047 _dl_printf ("/%x %s\n", type_class, undef_name);
1050 #endif