Fixed and unified pthread_once.
[glibc.git] / elf / dl-lookup.c
blobbe6f76faa8b8bcd0ffa9664dc439827847ec510e
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;
190 /* Inner part of the lookup functions. We return a value > 0 if we
191 found the symbol, the value 0 if nothing is found and < 0 if
192 something bad happened. */
193 static int
194 __attribute_noinline__
195 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
196 unsigned long int *old_hash, const ElfW(Sym) *ref,
197 struct sym_val *result, struct r_scope_elem *scope, size_t i,
198 const struct r_found_version *const version, int flags,
199 struct link_map *skip, int type_class, struct link_map *undef_map)
201 size_t n = scope->r_nlist;
202 /* Make sure we read the value before proceeding. Otherwise we
203 might use r_list pointing to the initial scope and r_nlist being
204 the value after a resize. That is the only path in dl-open.c not
205 protected by GSCOPE. A read barrier here might be to expensive. */
206 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
207 struct link_map **list = scope->r_list;
211 const struct link_map *map = list[i]->l_real;
213 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
214 if (map == skip)
215 continue;
217 /* Don't search the executable when resolving a copy reloc. */
218 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
219 continue;
221 /* Do not look into objects which are going to be removed. */
222 if (map->l_removed)
223 continue;
225 /* Print some debugging info if wanted. */
226 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
227 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
228 undef_name, DSO_FILENAME (map->l_name),
229 map->l_ns);
231 /* If the hash table is empty there is nothing to do here. */
232 if (map->l_nbuckets == 0)
233 continue;
235 Elf_Symndx symidx;
236 int num_versions = 0;
237 const ElfW(Sym) *versioned_sym = NULL;
239 /* The tables for this map. */
240 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
241 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
243 const ElfW(Sym) *sym;
244 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
245 if (__glibc_likely (bitmask != NULL))
247 ElfW(Addr) bitmask_word
248 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
249 & map->l_gnu_bitmask_idxbits];
251 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
252 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
253 & (__ELF_NATIVE_CLASS - 1));
255 if (__builtin_expect ((bitmask_word >> hashbit1)
256 & (bitmask_word >> hashbit2) & 1, 0))
258 Elf32_Word bucket = map->l_gnu_buckets[new_hash
259 % map->l_nbuckets];
260 if (bucket != 0)
262 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
265 if (((*hasharr ^ new_hash) >> 1) == 0)
267 symidx = hasharr - map->l_gnu_chain_zero;
268 sym = check_match (undef_name, ref, version, flags,
269 type_class, &symtab[symidx], symidx,
270 strtab, map, &versioned_sym,
271 &num_versions);
272 if (sym != NULL)
273 goto found_it;
275 while ((*hasharr++ & 1u) == 0);
278 /* No symbol found. */
279 symidx = SHN_UNDEF;
281 else
283 if (*old_hash == 0xffffffff)
284 *old_hash = _dl_elf_hash (undef_name);
286 /* Use the old SysV-style hash table. Search the appropriate
287 hash bucket in this object's symbol table for a definition
288 for the same symbol name. */
289 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
290 symidx != STN_UNDEF;
291 symidx = map->l_chain[symidx])
293 sym = check_match (undef_name, ref, version, flags,
294 type_class, &symtab[symidx], symidx,
295 strtab, map, &versioned_sym,
296 &num_versions);
297 if (sym != NULL)
298 goto found_it;
302 /* If we have seen exactly one versioned symbol while we are
303 looking for an unversioned symbol and the version is not the
304 default version we still accept this symbol since there are
305 no possible ambiguities. */
306 sym = num_versions == 1 ? versioned_sym : NULL;
308 if (sym != NULL)
310 found_it:
311 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
313 case STB_WEAK:
314 /* Weak definition. Use this value if we don't find another. */
315 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
317 if (! result->s)
319 result->s = sym;
320 result->m = (struct link_map *) map;
322 break;
324 /* FALLTHROUGH */
325 case STB_GLOBAL:
326 success:
327 /* Global definition. Just what we need. */
328 result->s = sym;
329 result->m = (struct link_map *) map;
330 return 1;
332 case STB_GNU_UNIQUE:;
333 /* We have to determine whether we already found a
334 symbol with this name before. If not then we have to
335 add it to the search table. If we already found a
336 definition we have to use it. */
337 void enter (struct unique_sym *table, size_t size,
338 unsigned int hash, const char *name,
339 const ElfW(Sym) *sym, const struct link_map *map)
341 size_t idx = hash % size;
342 size_t hash2 = 1 + hash % (size - 2);
343 while (table[idx].name != NULL)
345 idx += hash2;
346 if (idx >= size)
347 idx -= size;
350 table[idx].hashval = hash;
351 table[idx].name = name;
352 table[idx].sym = sym;
353 table[idx].map = map;
356 struct unique_sym_table *tab
357 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
359 __rtld_lock_lock_recursive (tab->lock);
361 struct unique_sym *entries = tab->entries;
362 size_t size = tab->size;
363 if (entries != NULL)
365 size_t idx = new_hash % size;
366 size_t hash2 = 1 + new_hash % (size - 2);
367 while (1)
369 if (entries[idx].hashval == new_hash
370 && strcmp (entries[idx].name, undef_name) == 0)
372 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
374 /* We possibly have to initialize the central
375 copy from the copy addressed through the
376 relocation. */
377 result->s = sym;
378 result->m = (struct link_map *) map;
380 else
382 result->s = entries[idx].sym;
383 result->m = (struct link_map *) entries[idx].map;
385 __rtld_lock_unlock_recursive (tab->lock);
386 return 1;
389 if (entries[idx].name == NULL)
390 break;
392 idx += hash2;
393 if (idx >= size)
394 idx -= size;
397 if (size * 3 <= tab->n_elements * 4)
399 /* Expand the table. */
400 #ifdef RTLD_CHECK_FOREIGN_CALL
401 /* This must not happen during runtime relocations. */
402 assert (!RTLD_CHECK_FOREIGN_CALL);
403 #endif
404 size_t newsize = _dl_higher_prime_number (size + 1);
405 struct unique_sym *newentries
406 = calloc (sizeof (struct unique_sym), newsize);
407 if (newentries == NULL)
409 nomem:
410 __rtld_lock_unlock_recursive (tab->lock);
411 _dl_fatal_printf ("out of memory\n");
414 for (idx = 0; idx < size; ++idx)
415 if (entries[idx].name != NULL)
416 enter (newentries, newsize, entries[idx].hashval,
417 entries[idx].name, entries[idx].sym,
418 entries[idx].map);
420 tab->free (entries);
421 tab->size = newsize;
422 size = newsize;
423 entries = tab->entries = newentries;
424 tab->free = free;
427 else
429 #ifdef RTLD_CHECK_FOREIGN_CALL
430 /* This must not happen during runtime relocations. */
431 assert (!RTLD_CHECK_FOREIGN_CALL);
432 #endif
434 #ifdef SHARED
435 /* If tab->entries is NULL, but tab->size is not, it means
436 this is the second, conflict finding, lookup for
437 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
438 allocate anything and don't enter anything into the
439 hash table. */
440 if (__glibc_unlikely (tab->size))
442 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
443 __rtld_lock_unlock_recursive (tab->lock);
444 goto success;
446 #endif
448 #define INITIAL_NUNIQUE_SYM_TABLE 31
449 size = INITIAL_NUNIQUE_SYM_TABLE;
450 entries = calloc (sizeof (struct unique_sym), size);
451 if (entries == NULL)
452 goto nomem;
454 tab->entries = entries;
455 tab->size = size;
456 tab->free = free;
459 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
460 enter (entries, size, new_hash, strtab + sym->st_name, ref,
461 undef_map);
462 else
464 enter (entries, size, new_hash, strtab + sym->st_name, sym,
465 map);
467 if (map->l_type == lt_loaded)
468 /* Make sure we don't unload this object by
469 setting the appropriate flag. */
470 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
472 ++tab->n_elements;
474 __rtld_lock_unlock_recursive (tab->lock);
476 goto success;
478 default:
479 /* Local symbols are ignored. */
480 break;
484 /* If this current map is the one mentioned in the verneed entry
485 and we have not found a weak entry, it is a bug. */
486 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
487 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
488 return -1;
490 while (++i < n);
492 /* We have not found anything until now. */
493 return 0;
497 static uint_fast32_t
498 dl_new_hash (const char *s)
500 uint_fast32_t h = 5381;
501 for (unsigned char c = *s; c != '\0'; c = *++s)
502 h = h * 33 + c;
503 return h & 0xffffffff;
507 /* Add extra dependency on MAP to UNDEF_MAP. */
508 static int
509 internal_function
510 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
512 struct link_map *runp;
513 unsigned int i;
514 int result = 0;
516 /* Avoid self-references and references to objects which cannot be
517 unloaded anyway. */
518 if (undef_map == map)
519 return 0;
521 /* Avoid references to objects which cannot be unloaded anyway. */
522 assert (map->l_type == lt_loaded);
523 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
524 return 0;
526 struct link_map_reldeps *l_reldeps
527 = atomic_forced_read (undef_map->l_reldeps);
529 /* Make sure l_reldeps is read before l_initfini. */
530 atomic_read_barrier ();
532 /* Determine whether UNDEF_MAP already has a reference to MAP. First
533 look in the normal dependencies. */
534 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
535 if (l_initfini != NULL)
537 for (i = 0; l_initfini[i] != NULL; ++i)
538 if (l_initfini[i] == map)
539 return 0;
542 /* No normal dependency. See whether we already had to add it
543 to the special list of dynamic dependencies. */
544 unsigned int l_reldepsact = 0;
545 if (l_reldeps != NULL)
547 struct link_map **list = &l_reldeps->list[0];
548 l_reldepsact = l_reldeps->act;
549 for (i = 0; i < l_reldepsact; ++i)
550 if (list[i] == map)
551 return 0;
554 /* Save serial number of the target MAP. */
555 unsigned long long serial = map->l_serial;
557 /* Make sure nobody can unload the object while we are at it. */
558 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
560 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
561 here, that can result in ABBA deadlock. */
562 THREAD_GSCOPE_RESET_FLAG ();
563 __rtld_lock_lock_recursive (GL(dl_load_lock));
564 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
565 it can e.g. point to unallocated memory. So avoid the optimizer
566 treating the above read from MAP->l_serial as ensurance it
567 can safely dereference it. */
568 map = atomic_forced_read (map);
570 /* From this point on it is unsafe to dereference MAP, until it
571 has been found in one of the lists. */
573 /* Redo the l_initfini check in case undef_map's l_initfini
574 changed in the mean time. */
575 if (undef_map->l_initfini != l_initfini
576 && undef_map->l_initfini != NULL)
578 l_initfini = undef_map->l_initfini;
579 for (i = 0; l_initfini[i] != NULL; ++i)
580 if (l_initfini[i] == map)
581 goto out_check;
584 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
585 the mean time. */
586 if (undef_map->l_reldeps != NULL)
588 if (undef_map->l_reldeps != l_reldeps)
590 struct link_map **list = &undef_map->l_reldeps->list[0];
591 l_reldepsact = undef_map->l_reldeps->act;
592 for (i = 0; i < l_reldepsact; ++i)
593 if (list[i] == map)
594 goto out_check;
596 else if (undef_map->l_reldeps->act > l_reldepsact)
598 struct link_map **list
599 = &undef_map->l_reldeps->list[0];
600 i = l_reldepsact;
601 l_reldepsact = undef_map->l_reldeps->act;
602 for (; i < l_reldepsact; ++i)
603 if (list[i] == map)
604 goto out_check;
608 else
609 __rtld_lock_lock_recursive (GL(dl_load_lock));
611 /* The object is not yet in the dependency list. Before we add
612 it make sure just one more time the object we are about to
613 reference is still available. There is a brief period in
614 which the object could have been removed since we found the
615 definition. */
616 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
617 while (runp != NULL && runp != map)
618 runp = runp->l_next;
620 if (runp != NULL)
622 /* The object is still available. */
624 /* MAP could have been dlclosed, freed and then some other dlopened
625 library could have the same link_map pointer. */
626 if (map->l_serial != serial)
627 goto out_check;
629 /* Redo the NODELETE check, as when dl_load_lock wasn't held
630 yet this could have changed. */
631 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
632 goto out;
634 /* If the object with the undefined reference cannot be removed ever
635 just make sure the same is true for the object which contains the
636 definition. */
637 if (undef_map->l_type != lt_loaded
638 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
640 map->l_flags_1 |= DF_1_NODELETE;
641 goto out;
644 /* Add the reference now. */
645 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
647 /* Allocate more memory for the dependency list. Since this
648 can never happen during the startup phase we can use
649 `realloc'. */
650 struct link_map_reldeps *newp;
651 unsigned int max
652 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
654 #ifdef RTLD_PREPARE_FOREIGN_CALL
655 RTLD_PREPARE_FOREIGN_CALL;
656 #endif
658 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
659 if (newp == NULL)
661 /* If we didn't manage to allocate memory for the list this is
662 no fatal problem. We simply make sure the referenced object
663 cannot be unloaded. This is semantically the correct
664 behavior. */
665 map->l_flags_1 |= DF_1_NODELETE;
666 goto out;
668 else
670 if (l_reldepsact)
671 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
672 l_reldepsact * sizeof (struct link_map *));
673 newp->list[l_reldepsact] = map;
674 newp->act = l_reldepsact + 1;
675 atomic_write_barrier ();
676 void *old = undef_map->l_reldeps;
677 undef_map->l_reldeps = newp;
678 undef_map->l_reldepsmax = max;
679 if (old)
680 _dl_scope_free (old);
683 else
685 undef_map->l_reldeps->list[l_reldepsact] = map;
686 atomic_write_barrier ();
687 undef_map->l_reldeps->act = l_reldepsact + 1;
690 /* Display information if we are debugging. */
691 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
692 _dl_debug_printf ("\
693 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
694 DSO_FILENAME (map->l_name),
695 map->l_ns,
696 DSO_FILENAME (undef_map->l_name),
697 undef_map->l_ns);
699 else
700 /* Whoa, that was bad luck. We have to search again. */
701 result = -1;
703 out:
704 /* Release the lock. */
705 __rtld_lock_unlock_recursive (GL(dl_load_lock));
707 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
708 THREAD_GSCOPE_SET_FLAG ();
710 return result;
712 out_check:
713 if (map->l_serial != serial)
714 result = -1;
715 goto out;
718 static void
719 internal_function
720 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
721 const ElfW(Sym) **ref, struct sym_val *value,
722 const struct r_found_version *version, int type_class,
723 int protected);
726 /* Search loaded objects' symbol tables for a definition of the symbol
727 UNDEF_NAME, perhaps with a requested version for the symbol.
729 We must never have calls to the audit functions inside this function
730 or in any function which gets called. If this would happen the audit
731 code might create a thread which can throw off all the scope locking. */
732 lookup_t
733 internal_function
734 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
735 const ElfW(Sym) **ref,
736 struct r_scope_elem *symbol_scope[],
737 const struct r_found_version *version,
738 int type_class, int flags, struct link_map *skip_map)
740 const uint_fast32_t new_hash = dl_new_hash (undef_name);
741 unsigned long int old_hash = 0xffffffff;
742 struct sym_val current_value = { NULL, NULL };
743 struct r_scope_elem **scope = symbol_scope;
745 bump_num_relocations ();
747 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
748 is allowed if we look up a versioned symbol. */
749 assert (version == NULL
750 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
751 == 0);
753 size_t i = 0;
754 if (__glibc_unlikely (skip_map != NULL))
755 /* Search the relevant loaded objects for a definition. */
756 while ((*scope)->r_list[i] != skip_map)
757 ++i;
759 /* Search the relevant loaded objects for a definition. */
760 for (size_t start = i; *scope != NULL; start = 0, ++scope)
762 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
763 &current_value, *scope, start, version, flags,
764 skip_map, type_class, undef_map);
765 if (res > 0)
766 break;
768 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
770 /* Oh, oh. The file named in the relocation entry does not
771 contain the needed symbol. This code is never reached
772 for unversioned lookups. */
773 assert (version != NULL);
774 const char *reference_name = undef_map ? undef_map->l_name : "";
776 /* XXX We cannot translate the message. */
777 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
778 N_("relocation error"),
779 make_string ("symbol ", undef_name, ", version ",
780 version->name,
781 " not defined in file ",
782 version->filename,
783 " with link time reference",
784 res == -2
785 ? " (no version symbols)" : ""));
786 *ref = NULL;
787 return 0;
791 if (__glibc_unlikely (current_value.s == NULL))
793 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
794 && skip_map == NULL
795 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
797 /* We could find no value for a strong reference. */
798 const char *reference_name = undef_map ? undef_map->l_name : "";
799 const char *versionstr = version ? ", version " : "";
800 const char *versionname = (version && version->name
801 ? version->name : "");
803 /* XXX We cannot translate the message. */
804 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
805 N_("symbol lookup error"),
806 make_string (undefined_msg, undef_name,
807 versionstr, versionname));
809 *ref = NULL;
810 return 0;
813 int protected = (*ref
814 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
815 if (__glibc_unlikely (protected != 0))
817 /* It is very tricky. We need to figure out what value to
818 return for the protected symbol. */
819 if (type_class == ELF_RTYPE_CLASS_PLT)
821 if (current_value.s != NULL && current_value.m != undef_map)
823 current_value.s = *ref;
824 current_value.m = undef_map;
827 else
829 struct sym_val protected_value = { NULL, NULL };
831 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
832 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
833 &protected_value, *scope, i, version, flags,
834 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
835 break;
837 if (protected_value.s != NULL && protected_value.m != undef_map)
839 current_value.s = *ref;
840 current_value.m = undef_map;
845 /* We have to check whether this would bind UNDEF_MAP to an object
846 in the global scope which was dynamically loaded. In this case
847 we have to prevent the latter from being unloaded unless the
848 UNDEF_MAP object is also unloaded. */
849 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
850 /* Don't do this for explicit lookups as opposed to implicit
851 runtime lookups. */
852 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
853 /* Add UNDEF_MAP to the dependencies. */
854 && add_dependency (undef_map, current_value.m, flags) < 0)
855 /* Something went wrong. Perhaps the object we tried to reference
856 was just removed. Try finding another definition. */
857 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
858 (flags & DL_LOOKUP_GSCOPE_LOCK)
859 ? undef_map->l_scope : symbol_scope,
860 version, type_class, flags, skip_map);
862 /* The object is used. */
863 if (__glibc_unlikely (current_value.m->l_used == 0))
864 current_value.m->l_used = 1;
866 if (__builtin_expect (GLRO(dl_debug_mask)
867 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
868 _dl_debug_bindings (undef_name, undef_map, ref,
869 &current_value, version, type_class, protected);
871 *ref = current_value.s;
872 return LOOKUP_VALUE (current_value.m);
876 /* Cache the location of MAP's hash table. */
878 void
879 internal_function
880 _dl_setup_hash (struct link_map *map)
882 Elf_Symndx *hash;
884 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
885 + DT_THISPROCNUM + DT_VERSIONTAGNUM
886 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
888 Elf32_Word *hash32
889 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
890 + DT_THISPROCNUM + DT_VERSIONTAGNUM
891 + DT_EXTRANUM + DT_VALNUM]);
892 map->l_nbuckets = *hash32++;
893 Elf32_Word symbias = *hash32++;
894 Elf32_Word bitmask_nwords = *hash32++;
895 /* Must be a power of two. */
896 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
897 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
898 map->l_gnu_shift = *hash32++;
900 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
901 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
903 map->l_gnu_buckets = hash32;
904 hash32 += map->l_nbuckets;
905 map->l_gnu_chain_zero = hash32 - symbias;
906 return;
909 if (!map->l_info[DT_HASH])
910 return;
911 hash = (void *) D_PTR (map, l_info[DT_HASH]);
913 map->l_nbuckets = *hash++;
914 /* Skip nchain. */
915 hash++;
916 map->l_buckets = hash;
917 hash += map->l_nbuckets;
918 map->l_chain = hash;
922 static void
923 internal_function
924 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
925 const ElfW(Sym) **ref, struct sym_val *value,
926 const struct r_found_version *version, int type_class,
927 int protected)
929 const char *reference_name = undef_map->l_name;
931 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
933 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
934 DSO_FILENAME (reference_name),
935 undef_map->l_ns,
936 DSO_FILENAME (value->m->l_name),
937 value->m->l_ns,
938 protected ? "protected" : "normal", undef_name);
939 if (version)
940 _dl_debug_printf_c (" [%s]\n", version->name);
941 else
942 _dl_debug_printf_c ("\n");
944 #ifdef SHARED
945 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
947 int conflict = 0;
948 struct sym_val val = { NULL, NULL };
950 if ((GLRO(dl_trace_prelink_map) == NULL
951 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
952 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
954 const uint_fast32_t new_hash = dl_new_hash (undef_name);
955 unsigned long int old_hash = 0xffffffff;
956 struct unique_sym *saved_entries
957 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
959 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
960 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
961 undef_map->l_local_scope[0], 0, version, 0, NULL,
962 type_class, undef_map);
963 if (val.s != value->s || val.m != value->m)
964 conflict = 1;
965 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
966 && val.s
967 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
968 STB_GLOBAL) == STB_GNU_UNIQUE)
970 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
971 contains any DT_SYMBOLIC libraries, unfortunately there
972 can be conflicts even if the above is equal. As symbol
973 resolution goes from the last library to the first and
974 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
975 library, it would be the one that is looked up. */
976 struct sym_val val2 = { NULL, NULL };
977 size_t n;
978 struct r_scope_elem *scope = undef_map->l_local_scope[0];
980 for (n = 0; n < scope->r_nlist; n++)
981 if (scope->r_list[n] == val.m)
982 break;
984 for (n++; n < scope->r_nlist; n++)
985 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
986 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
987 &val2,
988 &scope->r_list[n]->l_symbolic_searchlist,
989 0, version, 0, NULL, type_class,
990 undef_map) > 0)
992 conflict = 1;
993 val = val2;
994 break;
997 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
1000 if (value->s)
1002 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1003 == STT_TLS, 0))
1004 type_class = 4;
1005 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1006 == STT_GNU_IFUNC, 0))
1007 type_class |= 8;
1010 if (conflict
1011 || GLRO(dl_trace_prelink_map) == undef_map
1012 || GLRO(dl_trace_prelink_map) == NULL
1013 || type_class >= 4)
1015 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1016 conflict ? "conflict" : "lookup",
1017 (int) sizeof (ElfW(Addr)) * 2,
1018 (size_t) undef_map->l_map_start,
1019 (int) sizeof (ElfW(Addr)) * 2,
1020 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1021 (int) sizeof (ElfW(Addr)) * 2,
1022 (size_t) (value->s ? value->m->l_map_start : 0),
1023 (int) sizeof (ElfW(Addr)) * 2,
1024 (size_t) (value->s ? value->s->st_value : 0));
1026 if (conflict)
1027 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1028 (int) sizeof (ElfW(Addr)) * 2,
1029 (size_t) (val.s ? val.m->l_map_start : 0),
1030 (int) sizeof (ElfW(Addr)) * 2,
1031 (size_t) (val.s ? val.s->st_value : 0));
1033 _dl_printf ("/%x %s\n", type_class, undef_name);
1036 #endif