Use += before-compile instead of a :=.
[glibc.git] / elf / dl-lookup.c
blobd1b8efc5d79eaf2271435b2bca8ecfdca836fe46
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 /* These variables are used in the nested function. */
212 Elf_Symndx symidx;
213 int num_versions = 0;
214 const ElfW(Sym) *versioned_sym = NULL;
216 const struct link_map *map = list[i]->l_real;
218 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
219 if (map == skip)
220 continue;
222 /* Don't search the executable when resolving a copy reloc. */
223 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
224 continue;
226 /* Do not look into objects which are going to be removed. */
227 if (map->l_removed)
228 continue;
230 /* Print some debugging info if wanted. */
231 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
232 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
233 undef_name, DSO_FILENAME (map->l_name),
234 map->l_ns);
236 /* If the hash table is empty there is nothing to do here. */
237 if (map->l_nbuckets == 0)
238 continue;
240 /* The tables for this map. */
241 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
242 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
244 const ElfW(Sym) *sym;
245 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
246 if (__glibc_likely (bitmask != NULL))
248 ElfW(Addr) bitmask_word
249 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
250 & map->l_gnu_bitmask_idxbits];
252 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
253 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
254 & (__ELF_NATIVE_CLASS - 1));
256 if (__builtin_expect ((bitmask_word >> hashbit1)
257 & (bitmask_word >> hashbit2) & 1, 0))
259 Elf32_Word bucket = map->l_gnu_buckets[new_hash
260 % map->l_nbuckets];
261 if (bucket != 0)
263 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
266 if (((*hasharr ^ new_hash) >> 1) == 0)
268 symidx = hasharr - map->l_gnu_chain_zero;
269 sym = check_match (undef_name, ref, version, flags,
270 type_class, &symtab[symidx], symidx,
271 strtab, map, &versioned_sym,
272 &num_versions);
273 if (sym != NULL)
274 goto found_it;
276 while ((*hasharr++ & 1u) == 0);
279 /* No symbol found. */
280 symidx = SHN_UNDEF;
282 else
284 if (*old_hash == 0xffffffff)
285 *old_hash = _dl_elf_hash (undef_name);
287 /* Use the old SysV-style hash table. Search the appropriate
288 hash bucket in this object's symbol table for a definition
289 for the same symbol name. */
290 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
291 symidx != STN_UNDEF;
292 symidx = map->l_chain[symidx])
294 sym = check_match (undef_name, ref, version, flags,
295 type_class, &symtab[symidx], symidx,
296 strtab, map, &versioned_sym,
297 &num_versions);
298 if (sym != NULL)
299 goto found_it;
303 /* If we have seen exactly one versioned symbol while we are
304 looking for an unversioned symbol and the version is not the
305 default version we still accept this symbol since there are
306 no possible ambiguities. */
307 sym = num_versions == 1 ? versioned_sym : NULL;
309 if (sym != NULL)
311 found_it:
312 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
314 case STB_WEAK:
315 /* Weak definition. Use this value if we don't find another. */
316 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
318 if (! result->s)
320 result->s = sym;
321 result->m = (struct link_map *) map;
323 break;
325 /* FALLTHROUGH */
326 case STB_GLOBAL:
327 success:
328 /* Global definition. Just what we need. */
329 result->s = sym;
330 result->m = (struct link_map *) map;
331 return 1;
333 case STB_GNU_UNIQUE:;
334 /* We have to determine whether we already found a
335 symbol with this name before. If not then we have to
336 add it to the search table. If we already found a
337 definition we have to use it. */
338 void enter (struct unique_sym *table, size_t size,
339 unsigned int hash, const char *name,
340 const ElfW(Sym) *sym, const struct link_map *map)
342 size_t idx = hash % size;
343 size_t hash2 = 1 + hash % (size - 2);
344 while (table[idx].name != NULL)
346 idx += hash2;
347 if (idx >= size)
348 idx -= size;
351 table[idx].hashval = hash;
352 table[idx].name = name;
353 table[idx].sym = sym;
354 table[idx].map = map;
357 struct unique_sym_table *tab
358 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
360 __rtld_lock_lock_recursive (tab->lock);
362 struct unique_sym *entries = tab->entries;
363 size_t size = tab->size;
364 if (entries != NULL)
366 size_t idx = new_hash % size;
367 size_t hash2 = 1 + new_hash % (size - 2);
368 while (1)
370 if (entries[idx].hashval == new_hash
371 && strcmp (entries[idx].name, undef_name) == 0)
373 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
375 /* We possibly have to initialize the central
376 copy from the copy addressed through the
377 relocation. */
378 result->s = sym;
379 result->m = (struct link_map *) map;
381 else
383 result->s = entries[idx].sym;
384 result->m = (struct link_map *) entries[idx].map;
386 __rtld_lock_unlock_recursive (tab->lock);
387 return 1;
390 if (entries[idx].name == NULL)
391 break;
393 idx += hash2;
394 if (idx >= size)
395 idx -= size;
398 if (size * 3 <= tab->n_elements * 4)
400 /* Expand the table. */
401 #ifdef RTLD_CHECK_FOREIGN_CALL
402 /* This must not happen during runtime relocations. */
403 assert (!RTLD_CHECK_FOREIGN_CALL);
404 #endif
405 size_t newsize = _dl_higher_prime_number (size + 1);
406 struct unique_sym *newentries
407 = calloc (sizeof (struct unique_sym), newsize);
408 if (newentries == NULL)
410 nomem:
411 __rtld_lock_unlock_recursive (tab->lock);
412 _dl_fatal_printf ("out of memory\n");
415 for (idx = 0; idx < size; ++idx)
416 if (entries[idx].name != NULL)
417 enter (newentries, newsize, entries[idx].hashval,
418 entries[idx].name, entries[idx].sym,
419 entries[idx].map);
421 tab->free (entries);
422 tab->size = newsize;
423 size = newsize;
424 entries = tab->entries = newentries;
425 tab->free = free;
428 else
430 #ifdef RTLD_CHECK_FOREIGN_CALL
431 /* This must not happen during runtime relocations. */
432 assert (!RTLD_CHECK_FOREIGN_CALL);
433 #endif
435 #ifdef SHARED
436 /* If tab->entries is NULL, but tab->size is not, it means
437 this is the second, conflict finding, lookup for
438 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
439 allocate anything and don't enter anything into the
440 hash table. */
441 if (__glibc_unlikely (tab->size))
443 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
444 __rtld_lock_unlock_recursive (tab->lock);
445 goto success;
447 #endif
449 #define INITIAL_NUNIQUE_SYM_TABLE 31
450 size = INITIAL_NUNIQUE_SYM_TABLE;
451 entries = calloc (sizeof (struct unique_sym), size);
452 if (entries == NULL)
453 goto nomem;
455 tab->entries = entries;
456 tab->size = size;
457 tab->free = free;
460 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
461 enter (entries, size, new_hash, strtab + sym->st_name, ref,
462 undef_map);
463 else
465 enter (entries, size, new_hash, strtab + sym->st_name, sym,
466 map);
468 if (map->l_type == lt_loaded)
469 /* Make sure we don't unload this object by
470 setting the appropriate flag. */
471 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
473 ++tab->n_elements;
475 __rtld_lock_unlock_recursive (tab->lock);
477 goto success;
479 default:
480 /* Local symbols are ignored. */
481 break;
485 /* If this current map is the one mentioned in the verneed entry
486 and we have not found a weak entry, it is a bug. */
487 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
488 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
489 return -1;
491 while (++i < n);
493 /* We have not found anything until now. */
494 return 0;
498 static uint_fast32_t
499 dl_new_hash (const char *s)
501 uint_fast32_t h = 5381;
502 for (unsigned char c = *s; c != '\0'; c = *++s)
503 h = h * 33 + c;
504 return h & 0xffffffff;
508 /* Add extra dependency on MAP to UNDEF_MAP. */
509 static int
510 internal_function
511 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
513 struct link_map *runp;
514 unsigned int i;
515 int result = 0;
517 /* Avoid self-references and references to objects which cannot be
518 unloaded anyway. */
519 if (undef_map == map)
520 return 0;
522 /* Avoid references to objects which cannot be unloaded anyway. */
523 assert (map->l_type == lt_loaded);
524 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
525 return 0;
527 struct link_map_reldeps *l_reldeps
528 = atomic_forced_read (undef_map->l_reldeps);
530 /* Make sure l_reldeps is read before l_initfini. */
531 atomic_read_barrier ();
533 /* Determine whether UNDEF_MAP already has a reference to MAP. First
534 look in the normal dependencies. */
535 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
536 if (l_initfini != NULL)
538 for (i = 0; l_initfini[i] != NULL; ++i)
539 if (l_initfini[i] == map)
540 return 0;
543 /* No normal dependency. See whether we already had to add it
544 to the special list of dynamic dependencies. */
545 unsigned int l_reldepsact = 0;
546 if (l_reldeps != NULL)
548 struct link_map **list = &l_reldeps->list[0];
549 l_reldepsact = l_reldeps->act;
550 for (i = 0; i < l_reldepsact; ++i)
551 if (list[i] == map)
552 return 0;
555 /* Save serial number of the target MAP. */
556 unsigned long long serial = map->l_serial;
558 /* Make sure nobody can unload the object while we are at it. */
559 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
561 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
562 here, that can result in ABBA deadlock. */
563 THREAD_GSCOPE_RESET_FLAG ();
564 __rtld_lock_lock_recursive (GL(dl_load_lock));
565 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
566 it can e.g. point to unallocated memory. So avoid the optimizer
567 treating the above read from MAP->l_serial as ensurance it
568 can safely dereference it. */
569 map = atomic_forced_read (map);
571 /* From this point on it is unsafe to dereference MAP, until it
572 has been found in one of the lists. */
574 /* Redo the l_initfini check in case undef_map's l_initfini
575 changed in the mean time. */
576 if (undef_map->l_initfini != l_initfini
577 && undef_map->l_initfini != NULL)
579 l_initfini = undef_map->l_initfini;
580 for (i = 0; l_initfini[i] != NULL; ++i)
581 if (l_initfini[i] == map)
582 goto out_check;
585 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
586 the mean time. */
587 if (undef_map->l_reldeps != NULL)
589 if (undef_map->l_reldeps != l_reldeps)
591 struct link_map **list = &undef_map->l_reldeps->list[0];
592 l_reldepsact = undef_map->l_reldeps->act;
593 for (i = 0; i < l_reldepsact; ++i)
594 if (list[i] == map)
595 goto out_check;
597 else if (undef_map->l_reldeps->act > l_reldepsact)
599 struct link_map **list
600 = &undef_map->l_reldeps->list[0];
601 i = l_reldepsact;
602 l_reldepsact = undef_map->l_reldeps->act;
603 for (; i < l_reldepsact; ++i)
604 if (list[i] == map)
605 goto out_check;
609 else
610 __rtld_lock_lock_recursive (GL(dl_load_lock));
612 /* The object is not yet in the dependency list. Before we add
613 it make sure just one more time the object we are about to
614 reference is still available. There is a brief period in
615 which the object could have been removed since we found the
616 definition. */
617 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
618 while (runp != NULL && runp != map)
619 runp = runp->l_next;
621 if (runp != NULL)
623 /* The object is still available. */
625 /* MAP could have been dlclosed, freed and then some other dlopened
626 library could have the same link_map pointer. */
627 if (map->l_serial != serial)
628 goto out_check;
630 /* Redo the NODELETE check, as when dl_load_lock wasn't held
631 yet this could have changed. */
632 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
633 goto out;
635 /* If the object with the undefined reference cannot be removed ever
636 just make sure the same is true for the object which contains the
637 definition. */
638 if (undef_map->l_type != lt_loaded
639 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
641 map->l_flags_1 |= DF_1_NODELETE;
642 goto out;
645 /* Add the reference now. */
646 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
648 /* Allocate more memory for the dependency list. Since this
649 can never happen during the startup phase we can use
650 `realloc'. */
651 struct link_map_reldeps *newp;
652 unsigned int max
653 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
655 #ifdef RTLD_PREPARE_FOREIGN_CALL
656 RTLD_PREPARE_FOREIGN_CALL;
657 #endif
659 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
660 if (newp == NULL)
662 /* If we didn't manage to allocate memory for the list this is
663 no fatal problem. We simply make sure the referenced object
664 cannot be unloaded. This is semantically the correct
665 behavior. */
666 map->l_flags_1 |= DF_1_NODELETE;
667 goto out;
669 else
671 if (l_reldepsact)
672 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
673 l_reldepsact * sizeof (struct link_map *));
674 newp->list[l_reldepsact] = map;
675 newp->act = l_reldepsact + 1;
676 atomic_write_barrier ();
677 void *old = undef_map->l_reldeps;
678 undef_map->l_reldeps = newp;
679 undef_map->l_reldepsmax = max;
680 if (old)
681 _dl_scope_free (old);
684 else
686 undef_map->l_reldeps->list[l_reldepsact] = map;
687 atomic_write_barrier ();
688 undef_map->l_reldeps->act = l_reldepsact + 1;
691 /* Display information if we are debugging. */
692 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
693 _dl_debug_printf ("\
694 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
695 DSO_FILENAME (map->l_name),
696 map->l_ns,
697 DSO_FILENAME (undef_map->l_name),
698 undef_map->l_ns);
700 else
701 /* Whoa, that was bad luck. We have to search again. */
702 result = -1;
704 out:
705 /* Release the lock. */
706 __rtld_lock_unlock_recursive (GL(dl_load_lock));
708 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
709 THREAD_GSCOPE_SET_FLAG ();
711 return result;
713 out_check:
714 if (map->l_serial != serial)
715 result = -1;
716 goto out;
719 static void
720 internal_function
721 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
722 const ElfW(Sym) **ref, struct sym_val *value,
723 const struct r_found_version *version, int type_class,
724 int protected);
727 /* Search loaded objects' symbol tables for a definition of the symbol
728 UNDEF_NAME, perhaps with a requested version for the symbol.
730 We must never have calls to the audit functions inside this function
731 or in any function which gets called. If this would happen the audit
732 code might create a thread which can throw off all the scope locking. */
733 lookup_t
734 internal_function
735 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
736 const ElfW(Sym) **ref,
737 struct r_scope_elem *symbol_scope[],
738 const struct r_found_version *version,
739 int type_class, int flags, struct link_map *skip_map)
741 const uint_fast32_t new_hash = dl_new_hash (undef_name);
742 unsigned long int old_hash = 0xffffffff;
743 struct sym_val current_value = { NULL, NULL };
744 struct r_scope_elem **scope = symbol_scope;
746 bump_num_relocations ();
748 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
749 is allowed if we look up a versioned symbol. */
750 assert (version == NULL
751 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
752 == 0);
754 size_t i = 0;
755 if (__glibc_unlikely (skip_map != NULL))
756 /* Search the relevant loaded objects for a definition. */
757 while ((*scope)->r_list[i] != skip_map)
758 ++i;
760 /* Search the relevant loaded objects for a definition. */
761 for (size_t start = i; *scope != NULL; start = 0, ++scope)
763 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
764 &current_value, *scope, start, version, flags,
765 skip_map, type_class, undef_map);
766 if (res > 0)
767 break;
769 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
771 /* Oh, oh. The file named in the relocation entry does not
772 contain the needed symbol. This code is never reached
773 for unversioned lookups. */
774 assert (version != NULL);
775 const char *reference_name = undef_map ? undef_map->l_name : "";
777 /* XXX We cannot translate the message. */
778 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
779 N_("relocation error"),
780 make_string ("symbol ", undef_name, ", version ",
781 version->name,
782 " not defined in file ",
783 version->filename,
784 " with link time reference",
785 res == -2
786 ? " (no version symbols)" : ""));
787 *ref = NULL;
788 return 0;
792 if (__glibc_unlikely (current_value.s == NULL))
794 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
795 && skip_map == NULL
796 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
798 /* We could find no value for a strong reference. */
799 const char *reference_name = undef_map ? undef_map->l_name : "";
800 const char *versionstr = version ? ", version " : "";
801 const char *versionname = (version && version->name
802 ? version->name : "");
804 /* XXX We cannot translate the message. */
805 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
806 N_("symbol lookup error"),
807 make_string (undefined_msg, undef_name,
808 versionstr, versionname));
810 *ref = NULL;
811 return 0;
814 int protected = (*ref
815 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
816 if (__glibc_unlikely (protected != 0))
818 /* It is very tricky. We need to figure out what value to
819 return for the protected symbol. */
820 if (type_class == ELF_RTYPE_CLASS_PLT)
822 if (current_value.s != NULL && current_value.m != undef_map)
824 current_value.s = *ref;
825 current_value.m = undef_map;
828 else
830 struct sym_val protected_value = { NULL, NULL };
832 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
833 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
834 &protected_value, *scope, i, version, flags,
835 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
836 break;
838 if (protected_value.s != NULL && protected_value.m != undef_map)
840 current_value.s = *ref;
841 current_value.m = undef_map;
846 /* We have to check whether this would bind UNDEF_MAP to an object
847 in the global scope which was dynamically loaded. In this case
848 we have to prevent the latter from being unloaded unless the
849 UNDEF_MAP object is also unloaded. */
850 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
851 /* Don't do this for explicit lookups as opposed to implicit
852 runtime lookups. */
853 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
854 /* Add UNDEF_MAP to the dependencies. */
855 && add_dependency (undef_map, current_value.m, flags) < 0)
856 /* Something went wrong. Perhaps the object we tried to reference
857 was just removed. Try finding another definition. */
858 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
859 (flags & DL_LOOKUP_GSCOPE_LOCK)
860 ? undef_map->l_scope : symbol_scope,
861 version, type_class, flags, skip_map);
863 /* The object is used. */
864 if (__glibc_unlikely (current_value.m->l_used == 0))
865 current_value.m->l_used = 1;
867 if (__builtin_expect (GLRO(dl_debug_mask)
868 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
869 _dl_debug_bindings (undef_name, undef_map, ref,
870 &current_value, version, type_class, protected);
872 *ref = current_value.s;
873 return LOOKUP_VALUE (current_value.m);
877 /* Cache the location of MAP's hash table. */
879 void
880 internal_function
881 _dl_setup_hash (struct link_map *map)
883 Elf_Symndx *hash;
885 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
886 + DT_THISPROCNUM + DT_VERSIONTAGNUM
887 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
889 Elf32_Word *hash32
890 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
891 + DT_THISPROCNUM + DT_VERSIONTAGNUM
892 + DT_EXTRANUM + DT_VALNUM]);
893 map->l_nbuckets = *hash32++;
894 Elf32_Word symbias = *hash32++;
895 Elf32_Word bitmask_nwords = *hash32++;
896 /* Must be a power of two. */
897 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
898 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
899 map->l_gnu_shift = *hash32++;
901 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
902 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
904 map->l_gnu_buckets = hash32;
905 hash32 += map->l_nbuckets;
906 map->l_gnu_chain_zero = hash32 - symbias;
907 return;
910 if (!map->l_info[DT_HASH])
911 return;
912 hash = (void *) D_PTR (map, l_info[DT_HASH]);
914 map->l_nbuckets = *hash++;
915 /* Skip nchain. */
916 hash++;
917 map->l_buckets = hash;
918 hash += map->l_nbuckets;
919 map->l_chain = hash;
923 static void
924 internal_function
925 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
926 const ElfW(Sym) **ref, struct sym_val *value,
927 const struct r_found_version *version, int type_class,
928 int protected)
930 const char *reference_name = undef_map->l_name;
932 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
934 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
935 DSO_FILENAME (reference_name),
936 undef_map->l_ns,
937 DSO_FILENAME (value->m->l_name),
938 value->m->l_ns,
939 protected ? "protected" : "normal", undef_name);
940 if (version)
941 _dl_debug_printf_c (" [%s]\n", version->name);
942 else
943 _dl_debug_printf_c ("\n");
945 #ifdef SHARED
946 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
948 int conflict = 0;
949 struct sym_val val = { NULL, NULL };
951 if ((GLRO(dl_trace_prelink_map) == NULL
952 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
953 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
955 const uint_fast32_t new_hash = dl_new_hash (undef_name);
956 unsigned long int old_hash = 0xffffffff;
957 struct unique_sym *saved_entries
958 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
960 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
961 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
962 undef_map->l_local_scope[0], 0, version, 0, NULL,
963 type_class, undef_map);
964 if (val.s != value->s || val.m != value->m)
965 conflict = 1;
966 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
967 && val.s
968 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
969 STB_GLOBAL) == STB_GNU_UNIQUE)
971 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
972 contains any DT_SYMBOLIC libraries, unfortunately there
973 can be conflicts even if the above is equal. As symbol
974 resolution goes from the last library to the first and
975 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
976 library, it would be the one that is looked up. */
977 struct sym_val val2 = { NULL, NULL };
978 size_t n;
979 struct r_scope_elem *scope = undef_map->l_local_scope[0];
981 for (n = 0; n < scope->r_nlist; n++)
982 if (scope->r_list[n] == val.m)
983 break;
985 for (n++; n < scope->r_nlist; n++)
986 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
987 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
988 &val2,
989 &scope->r_list[n]->l_symbolic_searchlist,
990 0, version, 0, NULL, type_class,
991 undef_map) > 0)
993 conflict = 1;
994 val = val2;
995 break;
998 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
1001 if (value->s)
1003 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1004 == STT_TLS, 0))
1005 type_class = 4;
1006 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1007 == STT_GNU_IFUNC, 0))
1008 type_class |= 8;
1011 if (conflict
1012 || GLRO(dl_trace_prelink_map) == undef_map
1013 || GLRO(dl_trace_prelink_map) == NULL
1014 || type_class >= 4)
1016 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1017 conflict ? "conflict" : "lookup",
1018 (int) sizeof (ElfW(Addr)) * 2,
1019 (size_t) undef_map->l_map_start,
1020 (int) sizeof (ElfW(Addr)) * 2,
1021 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1022 (int) sizeof (ElfW(Addr)) * 2,
1023 (size_t) (value->s ? value->m->l_map_start : 0),
1024 (int) sizeof (ElfW(Addr)) * 2,
1025 (size_t) (value->s ? value->s->st_value : 0));
1027 if (conflict)
1028 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1029 (int) sizeof (ElfW(Addr)) * 2,
1030 (size_t) (val.s ? val.m->l_map_start : 0),
1031 (int) sizeof (ElfW(Addr)) * 2,
1032 (size_t) (val.s ? val.s->st_value : 0));
1034 _dl_printf ("/%x %s\n", type_class, undef_name);
1037 #endif