MicroBlaze: Update kernel-features.h for syscalls added in 3.15
[glibc.git] / elf / dl-lookup.c
blob7c32830a301b2b7cfc01d02e5aa5cd6528b0f715
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))
43 struct sym_val
45 const ElfW(Sym) *s;
46 struct link_map *m;
50 #define make_string(string, rest...) \
51 ({ \
52 const char *all[] = { string, ## rest }; \
53 size_t len, cnt; \
54 char *result, *cp; \
56 len = 1; \
57 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
58 len += strlen (all[cnt]); \
60 cp = result = alloca (len); \
61 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
62 cp = __stpcpy (cp, all[cnt]); \
64 result; \
67 /* Statistics function. */
68 #ifdef SHARED
69 # define bump_num_relocations() ++GL(dl_num_relocations)
70 #else
71 # define bump_num_relocations() ((void) 0)
72 #endif
74 /* Utility function for do_lookup_x. The caller is called with undef_name,
75 ref, version, flags and type_class, and those are passed as the first
76 five arguments. The caller then computes sym, symidx, strtab, and map
77 and passes them as the next four arguments. Lastly the caller passes in
78 versioned_sym and num_versions which are modified by check_match during
79 the checking process. */
80 static const ElfW(Sym) *
81 check_match (const char *const undef_name,
82 const ElfW(Sym) *const ref,
83 const struct r_found_version *const version,
84 const int flags,
85 const int type_class,
86 const ElfW(Sym) *const sym,
87 const Elf_Symndx symidx,
88 const char *const strtab,
89 const struct link_map *const map,
90 const ElfW(Sym) **const versioned_sym,
91 int *const num_versions)
93 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
94 assert (ELF_RTYPE_CLASS_PLT == 1);
95 if (__glibc_unlikely ((sym->st_value == 0 /* No value. */
96 && stt != STT_TLS)
97 || ELF_MACHINE_SYM_NO_MATCH (sym)
98 || (type_class & (sym->st_shndx == SHN_UNDEF))))
99 return NULL;
101 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
102 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
103 code/data definitions. */
104 #define ALLOWED_STT \
105 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
106 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
107 if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
108 return NULL;
110 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
111 /* Not the symbol we are looking for. */
112 return NULL;
114 const ElfW(Half) *verstab = map->l_versyms;
115 if (version != NULL)
117 if (__glibc_unlikely (verstab == NULL))
119 /* We need a versioned symbol but haven't found any. If
120 this is the object which is referenced in the verneed
121 entry it is a bug in the library since a symbol must
122 not simply disappear.
124 It would also be a bug in the object since it means that
125 the list of required versions is incomplete and so the
126 tests in dl-version.c haven't found a problem.*/
127 assert (version->filename == NULL
128 || ! _dl_name_match_p (version->filename, map));
130 /* Otherwise we accept the symbol. */
132 else
134 /* We can match the version information or use the
135 default one if it is not hidden. */
136 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
137 if ((map->l_versions[ndx].hash != version->hash
138 || strcmp (map->l_versions[ndx].name, version->name))
139 && (version->hidden || map->l_versions[ndx].hash
140 || (verstab[symidx] & 0x8000)))
141 /* It's not the version we want. */
142 return NULL;
145 else
147 /* No specific version is selected. There are two ways we
148 can got here:
150 - a binary which does not include versioning information
151 is loaded
153 - dlsym() instead of dlvsym() is used to get a symbol which
154 might exist in more than one form
156 If the library does not provide symbol version information
157 there is no problem at all: we simply use the symbol if it
158 is defined.
160 These two lookups need to be handled differently if the
161 library defines versions. In the case of the old
162 unversioned application the oldest (default) version
163 should be used. In case of a dlsym() call the latest and
164 public interface should be returned. */
165 if (verstab != NULL)
167 if ((verstab[symidx] & 0x7fff)
168 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
170 /* Don't accept hidden symbols. */
171 if ((verstab[symidx] & 0x8000) == 0
172 && (*num_versions)++ == 0)
173 /* No version so far. */
174 *versioned_sym = sym;
176 return NULL;
181 /* There cannot be another entry for this symbol so stop here. */
182 return sym;
185 /* Utility function for do_lookup_x. Lookup an STB_GNU_UNIQUE symbol
186 in the unique symbol table, creating a new entry if necessary.
187 Return the matching symbol in RESULT. */
188 static void
189 do_lookup_unique (const char *undef_name, uint_fast32_t new_hash,
190 const struct link_map *map, struct sym_val *result,
191 int type_class, const ElfW(Sym) *sym, const char *strtab,
192 const ElfW(Sym) *ref, const struct link_map *undef_map)
194 /* We have to determine whether we already found a
195 symbol with this name before. If not then we have to
196 add it to the search table. If we already found a
197 definition we have to use it. */
198 void enter (struct unique_sym *table, size_t size,
199 unsigned int hash, const char *name,
200 const ElfW(Sym) *sym, const struct link_map *map)
202 size_t idx = hash % size;
203 size_t hash2 = 1 + hash % (size - 2);
204 while (table[idx].name != NULL)
206 idx += hash2;
207 if (idx >= size)
208 idx -= size;
211 table[idx].hashval = hash;
212 table[idx].name = name;
213 table[idx].sym = sym;
214 table[idx].map = map;
217 struct unique_sym_table *tab
218 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
220 __rtld_lock_lock_recursive (tab->lock);
222 struct unique_sym *entries = tab->entries;
223 size_t size = tab->size;
224 if (entries != NULL)
226 size_t idx = new_hash % size;
227 size_t hash2 = 1 + new_hash % (size - 2);
228 while (1)
230 if (entries[idx].hashval == new_hash
231 && strcmp (entries[idx].name, undef_name) == 0)
233 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
235 /* We possibly have to initialize the central
236 copy from the copy addressed through the
237 relocation. */
238 result->s = sym;
239 result->m = (struct link_map *) map;
241 else
243 result->s = entries[idx].sym;
244 result->m = (struct link_map *) entries[idx].map;
246 __rtld_lock_unlock_recursive (tab->lock);
247 return;
250 if (entries[idx].name == NULL)
251 break;
253 idx += hash2;
254 if (idx >= size)
255 idx -= size;
258 if (size * 3 <= tab->n_elements * 4)
260 /* Expand the table. */
261 #ifdef RTLD_CHECK_FOREIGN_CALL
262 /* This must not happen during runtime relocations. */
263 assert (!RTLD_CHECK_FOREIGN_CALL);
264 #endif
265 size_t newsize = _dl_higher_prime_number (size + 1);
266 struct unique_sym *newentries
267 = calloc (sizeof (struct unique_sym), newsize);
268 if (newentries == NULL)
270 nomem:
271 __rtld_lock_unlock_recursive (tab->lock);
272 _dl_fatal_printf ("out of memory\n");
275 for (idx = 0; idx < size; ++idx)
276 if (entries[idx].name != NULL)
277 enter (newentries, newsize, entries[idx].hashval,
278 entries[idx].name, entries[idx].sym,
279 entries[idx].map);
281 tab->free (entries);
282 tab->size = newsize;
283 size = newsize;
284 entries = tab->entries = newentries;
285 tab->free = free;
288 else
290 #ifdef RTLD_CHECK_FOREIGN_CALL
291 /* This must not happen during runtime relocations. */
292 assert (!RTLD_CHECK_FOREIGN_CALL);
293 #endif
295 #ifdef SHARED
296 /* If tab->entries is NULL, but tab->size is not, it means
297 this is the second, conflict finding, lookup for
298 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
299 allocate anything and don't enter anything into the
300 hash table. */
301 if (__glibc_unlikely (tab->size))
303 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
304 goto success;
306 #endif
308 #define INITIAL_NUNIQUE_SYM_TABLE 31
309 size = INITIAL_NUNIQUE_SYM_TABLE;
310 entries = calloc (sizeof (struct unique_sym), size);
311 if (entries == NULL)
312 goto nomem;
314 tab->entries = entries;
315 tab->size = size;
316 tab->free = free;
319 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
320 enter (entries, size, new_hash, strtab + sym->st_name, ref,
321 undef_map);
322 else
324 enter (entries, size, new_hash, strtab + sym->st_name, sym, map);
326 if (map->l_type == lt_loaded)
327 /* Make sure we don't unload this object by
328 setting the appropriate flag. */
329 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
331 ++tab->n_elements;
333 #ifdef SHARED
334 success:
335 #endif
336 __rtld_lock_unlock_recursive (tab->lock);
338 result->s = sym;
339 result->m = (struct link_map *) map;
342 /* Inner part of the lookup functions. We return a value > 0 if we
343 found the symbol, the value 0 if nothing is found and < 0 if
344 something bad happened. */
345 static int
346 __attribute_noinline__
347 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
348 unsigned long int *old_hash, const ElfW(Sym) *ref,
349 struct sym_val *result, struct r_scope_elem *scope, size_t i,
350 const struct r_found_version *const version, int flags,
351 struct link_map *skip, int type_class, struct link_map *undef_map)
353 size_t n = scope->r_nlist;
354 /* Make sure we read the value before proceeding. Otherwise we
355 might use r_list pointing to the initial scope and r_nlist being
356 the value after a resize. That is the only path in dl-open.c not
357 protected by GSCOPE. A read barrier here might be to expensive. */
358 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
359 struct link_map **list = scope->r_list;
363 const struct link_map *map = list[i]->l_real;
365 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
366 if (map == skip)
367 continue;
369 /* Don't search the executable when resolving a copy reloc. */
370 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
371 continue;
373 /* Do not look into objects which are going to be removed. */
374 if (map->l_removed)
375 continue;
377 /* Print some debugging info if wanted. */
378 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
379 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
380 undef_name, DSO_FILENAME (map->l_name),
381 map->l_ns);
383 /* If the hash table is empty there is nothing to do here. */
384 if (map->l_nbuckets == 0)
385 continue;
387 Elf_Symndx symidx;
388 int num_versions = 0;
389 const ElfW(Sym) *versioned_sym = NULL;
391 /* The tables for this map. */
392 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
393 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
395 const ElfW(Sym) *sym;
396 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
397 if (__glibc_likely (bitmask != NULL))
399 ElfW(Addr) bitmask_word
400 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
401 & map->l_gnu_bitmask_idxbits];
403 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
404 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
405 & (__ELF_NATIVE_CLASS - 1));
407 if (__glibc_unlikely ((bitmask_word >> hashbit1)
408 & (bitmask_word >> hashbit2) & 1))
410 Elf32_Word bucket = map->l_gnu_buckets[new_hash
411 % map->l_nbuckets];
412 if (bucket != 0)
414 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
417 if (((*hasharr ^ new_hash) >> 1) == 0)
419 symidx = hasharr - map->l_gnu_chain_zero;
420 sym = check_match (undef_name, ref, version, flags,
421 type_class, &symtab[symidx], symidx,
422 strtab, map, &versioned_sym,
423 &num_versions);
424 if (sym != NULL)
425 goto found_it;
427 while ((*hasharr++ & 1u) == 0);
430 /* No symbol found. */
431 symidx = SHN_UNDEF;
433 else
435 if (*old_hash == 0xffffffff)
436 *old_hash = _dl_elf_hash (undef_name);
438 /* Use the old SysV-style hash table. Search the appropriate
439 hash bucket in this object's symbol table for a definition
440 for the same symbol name. */
441 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
442 symidx != STN_UNDEF;
443 symidx = map->l_chain[symidx])
445 sym = check_match (undef_name, ref, version, flags,
446 type_class, &symtab[symidx], symidx,
447 strtab, map, &versioned_sym,
448 &num_versions);
449 if (sym != NULL)
450 goto found_it;
454 /* If we have seen exactly one versioned symbol while we are
455 looking for an unversioned symbol and the version is not the
456 default version we still accept this symbol since there are
457 no possible ambiguities. */
458 sym = num_versions == 1 ? versioned_sym : NULL;
460 if (sym != NULL)
462 found_it:
463 switch (ELFW(ST_BIND) (sym->st_info))
465 case STB_WEAK:
466 /* Weak definition. Use this value if we don't find another. */
467 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
469 if (! result->s)
471 result->s = sym;
472 result->m = (struct link_map *) map;
474 break;
476 /* FALLTHROUGH */
477 case STB_GLOBAL:
478 /* Global definition. Just what we need. */
479 result->s = sym;
480 result->m = (struct link_map *) map;
481 return 1;
483 case STB_GNU_UNIQUE:;
484 do_lookup_unique (undef_name, new_hash, map, result, type_class,
485 sym, strtab, ref, undef_map);
486 return 1;
488 default:
489 /* Local symbols are ignored. */
490 break;
494 /* If this current map is the one mentioned in the verneed entry
495 and we have not found a weak entry, it is a bug. */
496 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
497 && __glibc_unlikely (_dl_name_match_p (version->filename, map)))
498 return -1;
500 while (++i < n);
502 /* We have not found anything until now. */
503 return 0;
507 static uint_fast32_t
508 dl_new_hash (const char *s)
510 uint_fast32_t h = 5381;
511 for (unsigned char c = *s; c != '\0'; c = *++s)
512 h = h * 33 + c;
513 return h & 0xffffffff;
517 /* Add extra dependency on MAP to UNDEF_MAP. */
518 static int
519 internal_function
520 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
522 struct link_map *runp;
523 unsigned int i;
524 int result = 0;
526 /* Avoid self-references and references to objects which cannot be
527 unloaded anyway. */
528 if (undef_map == map)
529 return 0;
531 /* Avoid references to objects which cannot be unloaded anyway. */
532 assert (map->l_type == lt_loaded);
533 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
534 return 0;
536 struct link_map_reldeps *l_reldeps
537 = atomic_forced_read (undef_map->l_reldeps);
539 /* Make sure l_reldeps is read before l_initfini. */
540 atomic_read_barrier ();
542 /* Determine whether UNDEF_MAP already has a reference to MAP. First
543 look in the normal dependencies. */
544 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
545 if (l_initfini != NULL)
547 for (i = 0; l_initfini[i] != NULL; ++i)
548 if (l_initfini[i] == map)
549 return 0;
552 /* No normal dependency. See whether we already had to add it
553 to the special list of dynamic dependencies. */
554 unsigned int l_reldepsact = 0;
555 if (l_reldeps != NULL)
557 struct link_map **list = &l_reldeps->list[0];
558 l_reldepsact = l_reldeps->act;
559 for (i = 0; i < l_reldepsact; ++i)
560 if (list[i] == map)
561 return 0;
564 /* Save serial number of the target MAP. */
565 unsigned long long serial = map->l_serial;
567 /* Make sure nobody can unload the object while we are at it. */
568 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
570 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
571 here, that can result in ABBA deadlock. */
572 THREAD_GSCOPE_RESET_FLAG ();
573 __rtld_lock_lock_recursive (GL(dl_load_lock));
574 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
575 it can e.g. point to unallocated memory. So avoid the optimizer
576 treating the above read from MAP->l_serial as ensurance it
577 can safely dereference it. */
578 map = atomic_forced_read (map);
580 /* From this point on it is unsafe to dereference MAP, until it
581 has been found in one of the lists. */
583 /* Redo the l_initfini check in case undef_map's l_initfini
584 changed in the mean time. */
585 if (undef_map->l_initfini != l_initfini
586 && undef_map->l_initfini != NULL)
588 l_initfini = undef_map->l_initfini;
589 for (i = 0; l_initfini[i] != NULL; ++i)
590 if (l_initfini[i] == map)
591 goto out_check;
594 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
595 the mean time. */
596 if (undef_map->l_reldeps != NULL)
598 if (undef_map->l_reldeps != l_reldeps)
600 struct link_map **list = &undef_map->l_reldeps->list[0];
601 l_reldepsact = undef_map->l_reldeps->act;
602 for (i = 0; i < l_reldepsact; ++i)
603 if (list[i] == map)
604 goto out_check;
606 else if (undef_map->l_reldeps->act > l_reldepsact)
608 struct link_map **list
609 = &undef_map->l_reldeps->list[0];
610 i = l_reldepsact;
611 l_reldepsact = undef_map->l_reldeps->act;
612 for (; i < l_reldepsact; ++i)
613 if (list[i] == map)
614 goto out_check;
618 else
619 __rtld_lock_lock_recursive (GL(dl_load_lock));
621 /* The object is not yet in the dependency list. Before we add
622 it make sure just one more time the object we are about to
623 reference is still available. There is a brief period in
624 which the object could have been removed since we found the
625 definition. */
626 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
627 while (runp != NULL && runp != map)
628 runp = runp->l_next;
630 if (runp != NULL)
632 /* The object is still available. */
634 /* MAP could have been dlclosed, freed and then some other dlopened
635 library could have the same link_map pointer. */
636 if (map->l_serial != serial)
637 goto out_check;
639 /* Redo the NODELETE check, as when dl_load_lock wasn't held
640 yet this could have changed. */
641 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
642 goto out;
644 /* If the object with the undefined reference cannot be removed ever
645 just make sure the same is true for the object which contains the
646 definition. */
647 if (undef_map->l_type != lt_loaded
648 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
650 map->l_flags_1 |= DF_1_NODELETE;
651 goto out;
654 /* Add the reference now. */
655 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
657 /* Allocate more memory for the dependency list. Since this
658 can never happen during the startup phase we can use
659 `realloc'. */
660 struct link_map_reldeps *newp;
661 unsigned int max
662 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
664 #ifdef RTLD_PREPARE_FOREIGN_CALL
665 RTLD_PREPARE_FOREIGN_CALL;
666 #endif
668 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
669 if (newp == NULL)
671 /* If we didn't manage to allocate memory for the list this is
672 no fatal problem. We simply make sure the referenced object
673 cannot be unloaded. This is semantically the correct
674 behavior. */
675 map->l_flags_1 |= DF_1_NODELETE;
676 goto out;
678 else
680 if (l_reldepsact)
681 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
682 l_reldepsact * sizeof (struct link_map *));
683 newp->list[l_reldepsact] = map;
684 newp->act = l_reldepsact + 1;
685 atomic_write_barrier ();
686 void *old = undef_map->l_reldeps;
687 undef_map->l_reldeps = newp;
688 undef_map->l_reldepsmax = max;
689 if (old)
690 _dl_scope_free (old);
693 else
695 undef_map->l_reldeps->list[l_reldepsact] = map;
696 atomic_write_barrier ();
697 undef_map->l_reldeps->act = l_reldepsact + 1;
700 /* Display information if we are debugging. */
701 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
702 _dl_debug_printf ("\
703 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
704 DSO_FILENAME (map->l_name),
705 map->l_ns,
706 DSO_FILENAME (undef_map->l_name),
707 undef_map->l_ns);
709 else
710 /* Whoa, that was bad luck. We have to search again. */
711 result = -1;
713 out:
714 /* Release the lock. */
715 __rtld_lock_unlock_recursive (GL(dl_load_lock));
717 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
718 THREAD_GSCOPE_SET_FLAG ();
720 return result;
722 out_check:
723 if (map->l_serial != serial)
724 result = -1;
725 goto out;
728 static void
729 internal_function
730 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
731 const ElfW(Sym) **ref, struct sym_val *value,
732 const struct r_found_version *version, int type_class,
733 int protected);
736 /* Search loaded objects' symbol tables for a definition of the symbol
737 UNDEF_NAME, perhaps with a requested version for the symbol.
739 We must never have calls to the audit functions inside this function
740 or in any function which gets called. If this would happen the audit
741 code might create a thread which can throw off all the scope locking. */
742 lookup_t
743 internal_function
744 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
745 const ElfW(Sym) **ref,
746 struct r_scope_elem *symbol_scope[],
747 const struct r_found_version *version,
748 int type_class, int flags, struct link_map *skip_map)
750 const uint_fast32_t new_hash = dl_new_hash (undef_name);
751 unsigned long int old_hash = 0xffffffff;
752 struct sym_val current_value = { NULL, NULL };
753 struct r_scope_elem **scope = symbol_scope;
755 bump_num_relocations ();
757 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
758 is allowed if we look up a versioned symbol. */
759 assert (version == NULL
760 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
761 == 0);
763 size_t i = 0;
764 if (__glibc_unlikely (skip_map != NULL))
765 /* Search the relevant loaded objects for a definition. */
766 while ((*scope)->r_list[i] != skip_map)
767 ++i;
769 /* Search the relevant loaded objects for a definition. */
770 for (size_t start = i; *scope != NULL; start = 0, ++scope)
772 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
773 &current_value, *scope, start, version, flags,
774 skip_map, type_class, undef_map);
775 if (res > 0)
776 break;
778 if (__glibc_unlikely (res < 0) && skip_map == NULL)
780 /* Oh, oh. The file named in the relocation entry does not
781 contain the needed symbol. This code is never reached
782 for unversioned lookups. */
783 assert (version != NULL);
784 const char *reference_name = undef_map ? undef_map->l_name : "";
786 /* XXX We cannot translate the message. */
787 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
788 N_("relocation error"),
789 make_string ("symbol ", undef_name, ", version ",
790 version->name,
791 " not defined in file ",
792 version->filename,
793 " with link time reference",
794 res == -2
795 ? " (no version symbols)" : ""));
796 *ref = NULL;
797 return 0;
801 if (__glibc_unlikely (current_value.s == NULL))
803 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
804 && skip_map == NULL
805 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
807 /* We could find no value for a strong reference. */
808 const char *reference_name = undef_map ? undef_map->l_name : "";
809 const char *versionstr = version ? ", version " : "";
810 const char *versionname = (version && version->name
811 ? version->name : "");
813 /* XXX We cannot translate the message. */
814 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
815 N_("symbol lookup error"),
816 make_string ("undefined symbol: ", undef_name,
817 versionstr, versionname));
819 *ref = NULL;
820 return 0;
823 int protected = (*ref
824 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
825 if (__glibc_unlikely (protected != 0))
827 /* It is very tricky. We need to figure out what value to
828 return for the protected symbol. */
829 if (type_class == ELF_RTYPE_CLASS_PLT)
831 if (current_value.s != NULL && current_value.m != undef_map)
833 current_value.s = *ref;
834 current_value.m = undef_map;
837 else
839 struct sym_val protected_value = { NULL, NULL };
841 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
842 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
843 &protected_value, *scope, i, version, flags,
844 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
845 break;
847 if (protected_value.s != NULL && protected_value.m != undef_map)
849 current_value.s = *ref;
850 current_value.m = undef_map;
855 /* We have to check whether this would bind UNDEF_MAP to an object
856 in the global scope which was dynamically loaded. In this case
857 we have to prevent the latter from being unloaded unless the
858 UNDEF_MAP object is also unloaded. */
859 if (__glibc_unlikely (current_value.m->l_type == lt_loaded)
860 /* Don't do this for explicit lookups as opposed to implicit
861 runtime lookups. */
862 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
863 /* Add UNDEF_MAP to the dependencies. */
864 && add_dependency (undef_map, current_value.m, flags) < 0)
865 /* Something went wrong. Perhaps the object we tried to reference
866 was just removed. Try finding another definition. */
867 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
868 (flags & DL_LOOKUP_GSCOPE_LOCK)
869 ? undef_map->l_scope : symbol_scope,
870 version, type_class, flags, skip_map);
872 /* The object is used. */
873 if (__glibc_unlikely (current_value.m->l_used == 0))
874 current_value.m->l_used = 1;
876 if (__glibc_unlikely (GLRO(dl_debug_mask)
877 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK)))
878 _dl_debug_bindings (undef_name, undef_map, ref,
879 &current_value, version, type_class, protected);
881 *ref = current_value.s;
882 return LOOKUP_VALUE (current_value.m);
886 /* Cache the location of MAP's hash table. */
888 void
889 internal_function
890 _dl_setup_hash (struct link_map *map)
892 Elf_Symndx *hash;
894 if (__glibc_likely (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
895 + DT_THISPROCNUM + DT_VERSIONTAGNUM
896 + DT_EXTRANUM + DT_VALNUM] != NULL))
898 Elf32_Word *hash32
899 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
900 + DT_THISPROCNUM + DT_VERSIONTAGNUM
901 + DT_EXTRANUM + DT_VALNUM]);
902 map->l_nbuckets = *hash32++;
903 Elf32_Word symbias = *hash32++;
904 Elf32_Word bitmask_nwords = *hash32++;
905 /* Must be a power of two. */
906 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
907 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
908 map->l_gnu_shift = *hash32++;
910 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
911 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
913 map->l_gnu_buckets = hash32;
914 hash32 += map->l_nbuckets;
915 map->l_gnu_chain_zero = hash32 - symbias;
916 return;
919 if (!map->l_info[DT_HASH])
920 return;
921 hash = (void *) D_PTR (map, l_info[DT_HASH]);
923 map->l_nbuckets = *hash++;
924 /* Skip nchain. */
925 hash++;
926 map->l_buckets = hash;
927 hash += map->l_nbuckets;
928 map->l_chain = hash;
932 static void
933 internal_function
934 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
935 const ElfW(Sym) **ref, struct sym_val *value,
936 const struct r_found_version *version, int type_class,
937 int protected)
939 const char *reference_name = undef_map->l_name;
941 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
943 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
944 DSO_FILENAME (reference_name),
945 undef_map->l_ns,
946 DSO_FILENAME (value->m->l_name),
947 value->m->l_ns,
948 protected ? "protected" : "normal", undef_name);
949 if (version)
950 _dl_debug_printf_c (" [%s]\n", version->name);
951 else
952 _dl_debug_printf_c ("\n");
954 #ifdef SHARED
955 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
957 int conflict = 0;
958 struct sym_val val = { NULL, NULL };
960 if ((GLRO(dl_trace_prelink_map) == NULL
961 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
962 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
964 const uint_fast32_t new_hash = dl_new_hash (undef_name);
965 unsigned long int old_hash = 0xffffffff;
966 struct unique_sym *saved_entries
967 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
969 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
970 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
971 undef_map->l_local_scope[0], 0, version, 0, NULL,
972 type_class, undef_map);
973 if (val.s != value->s || val.m != value->m)
974 conflict = 1;
975 else if (__glibc_unlikely (undef_map->l_symbolic_in_local_scope)
976 && val.s
977 && __glibc_unlikely (ELFW(ST_BIND) (val.s->st_info)
978 == STB_GNU_UNIQUE))
980 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
981 contains any DT_SYMBOLIC libraries, unfortunately there
982 can be conflicts even if the above is equal. As symbol
983 resolution goes from the last library to the first and
984 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
985 library, it would be the one that is looked up. */
986 struct sym_val val2 = { NULL, NULL };
987 size_t n;
988 struct r_scope_elem *scope = undef_map->l_local_scope[0];
990 for (n = 0; n < scope->r_nlist; n++)
991 if (scope->r_list[n] == val.m)
992 break;
994 for (n++; n < scope->r_nlist; n++)
995 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
996 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
997 &val2,
998 &scope->r_list[n]->l_symbolic_searchlist,
999 0, version, 0, NULL, type_class,
1000 undef_map) > 0)
1002 conflict = 1;
1003 val = val2;
1004 break;
1007 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
1010 if (value->s)
1012 if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1013 == STT_TLS))
1014 type_class = 4;
1015 else if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1016 == STT_GNU_IFUNC))
1017 type_class |= 8;
1020 if (conflict
1021 || GLRO(dl_trace_prelink_map) == undef_map
1022 || GLRO(dl_trace_prelink_map) == NULL
1023 || type_class >= 4)
1025 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1026 conflict ? "conflict" : "lookup",
1027 (int) sizeof (ElfW(Addr)) * 2,
1028 (size_t) undef_map->l_map_start,
1029 (int) sizeof (ElfW(Addr)) * 2,
1030 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1031 (int) sizeof (ElfW(Addr)) * 2,
1032 (size_t) (value->s ? value->m->l_map_start : 0),
1033 (int) sizeof (ElfW(Addr)) * 2,
1034 (size_t) (value->s ? value->s->st_value : 0));
1036 if (conflict)
1037 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1038 (int) sizeof (ElfW(Addr)) * 2,
1039 (size_t) (val.s ? val.m->l_map_start : 0),
1040 (int) sizeof (ElfW(Addr)) * 2,
1041 (size_t) (val.s ? val.s->st_value : 0));
1043 _dl_printf ("/%x %s\n", type_class, undef_name);
1046 #endif