Fix whitespace issue.
[glibc.git] / elf / dl-lookup.c
blob874a4bba4e0b1af607263d859e03ea04a1d8b92a
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2007, 2009, 2010, 2011 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <libintl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <ldsodefs.h>
26 #include <dl-hash.h>
27 #include <dl-machine.h>
28 #include <sysdep-cancel.h>
29 #include <bits/libc-lock.h>
30 #include <tls.h>
32 #include <assert.h>
34 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
36 /* We need this string more than once. */
37 static const char undefined_msg[] = "undefined symbol: ";
40 struct sym_val
42 const ElfW(Sym) *s;
43 struct link_map *m;
47 #define make_string(string, rest...) \
48 ({ \
49 const char *all[] = { string, ## rest }; \
50 size_t len, cnt; \
51 char *result, *cp; \
53 len = 1; \
54 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
55 len += strlen (all[cnt]); \
57 cp = result = alloca (len); \
58 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
59 cp = __stpcpy (cp, all[cnt]); \
61 result; \
64 /* Statistics function. */
65 #ifdef SHARED
66 # define bump_num_relocations() ++GL(dl_num_relocations)
67 #else
68 # define bump_num_relocations() ((void) 0)
69 #endif
72 /* Inner part of the lookup functions. We return a value > 0 if we
73 found the symbol, the value 0 if nothing is found and < 0 if
74 something bad happened. */
75 static int
76 __attribute_noinline__
77 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
78 unsigned long int *old_hash, const ElfW(Sym) *ref,
79 struct sym_val *result, struct r_scope_elem *scope, size_t i,
80 const struct r_found_version *const version, int flags,
81 struct link_map *skip, int type_class, struct link_map *undef_map)
83 size_t n = scope->r_nlist;
84 /* Make sure we read the value before proceeding. Otherwise we
85 might use r_list pointing to the initial scope and r_nlist being
86 the value after a resize. That is the only path in dl-open.c not
87 protected by GSCOPE. A read barrier here might be to expensive. */
88 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
89 struct link_map **list = scope->r_list;
93 /* These variables are used in the nested function. */
94 Elf_Symndx symidx;
95 int num_versions = 0;
96 const ElfW(Sym) *versioned_sym = NULL;
98 const struct link_map *map = list[i]->l_real;
100 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
101 if (map == skip)
102 continue;
104 /* Don't search the executable when resolving a copy reloc. */
105 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
106 continue;
108 /* Do not look into objects which are going to be removed. */
109 if (map->l_removed)
110 continue;
112 /* Print some debugging info if wanted. */
113 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
114 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
115 undef_name,
116 map->l_name[0] ? map->l_name : rtld_progname,
117 map->l_ns);
119 /* If the hash table is empty there is nothing to do here. */
120 if (map->l_nbuckets == 0)
121 continue;
123 /* The tables for this map. */
124 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
125 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
128 /* Nested routine to check whether the symbol matches. */
129 const ElfW(Sym) *
130 __attribute_noinline__
131 check_match (const ElfW(Sym) *sym)
133 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
134 assert (ELF_RTYPE_CLASS_PLT == 1);
135 if (__builtin_expect ((sym->st_value == 0 /* No value. */
136 && stt != STT_TLS)
137 || (type_class & (sym->st_shndx == SHN_UNDEF)),
139 return NULL;
141 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
142 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
143 code/data definitions. */
144 #define ALLOWED_STT \
145 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
146 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
147 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
148 return NULL;
150 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
151 /* Not the symbol we are looking for. */
152 return NULL;
154 const ElfW(Half) *verstab = map->l_versyms;
155 if (version != NULL)
157 if (__builtin_expect (verstab == NULL, 0))
159 /* We need a versioned symbol but haven't found any. If
160 this is the object which is referenced in the verneed
161 entry it is a bug in the library since a symbol must
162 not simply disappear.
164 It would also be a bug in the object since it means that
165 the list of required versions is incomplete and so the
166 tests in dl-version.c haven't found a problem.*/
167 assert (version->filename == NULL
168 || ! _dl_name_match_p (version->filename, map));
170 /* Otherwise we accept the symbol. */
172 else
174 /* We can match the version information or use the
175 default one if it is not hidden. */
176 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
177 if ((map->l_versions[ndx].hash != version->hash
178 || strcmp (map->l_versions[ndx].name, version->name))
179 && (version->hidden || map->l_versions[ndx].hash
180 || (verstab[symidx] & 0x8000)))
181 /* It's not the version we want. */
182 return NULL;
185 else
187 /* No specific version is selected. There are two ways we
188 can got here:
190 - a binary which does not include versioning information
191 is loaded
193 - dlsym() instead of dlvsym() is used to get a symbol which
194 might exist in more than one form
196 If the library does not provide symbol version information
197 there is no problem at at: we simply use the symbol if it
198 is defined.
200 These two lookups need to be handled differently if the
201 library defines versions. In the case of the old
202 unversioned application the oldest (default) version
203 should be used. In case of a dlsym() call the latest and
204 public interface should be returned. */
205 if (verstab != NULL)
207 if ((verstab[symidx] & 0x7fff)
208 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
210 /* Don't accept hidden symbols. */
211 if ((verstab[symidx] & 0x8000) == 0
212 && num_versions++ == 0)
213 /* No version so far. */
214 versioned_sym = sym;
216 return NULL;
221 /* There cannot be another entry for this symbol so stop here. */
222 return sym;
225 const ElfW(Sym) *sym;
226 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
227 if (__builtin_expect (bitmask != NULL, 1))
229 ElfW(Addr) bitmask_word
230 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
231 & map->l_gnu_bitmask_idxbits];
233 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
234 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
235 & (__ELF_NATIVE_CLASS - 1));
237 if (__builtin_expect ((bitmask_word >> hashbit1)
238 & (bitmask_word >> hashbit2) & 1, 0))
240 Elf32_Word bucket = map->l_gnu_buckets[new_hash
241 % map->l_nbuckets];
242 if (bucket != 0)
244 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
247 if (((*hasharr ^ new_hash) >> 1) == 0)
249 symidx = hasharr - map->l_gnu_chain_zero;
250 sym = check_match (&symtab[symidx]);
251 if (sym != NULL)
252 goto found_it;
254 while ((*hasharr++ & 1u) == 0);
257 /* No symbol found. */
258 symidx = SHN_UNDEF;
260 else
262 if (*old_hash == 0xffffffff)
263 *old_hash = _dl_elf_hash (undef_name);
265 /* Use the old SysV-style hash table. Search the appropriate
266 hash bucket in this object's symbol table for a definition
267 for the same symbol name. */
268 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
269 symidx != STN_UNDEF;
270 symidx = map->l_chain[symidx])
272 sym = check_match (&symtab[symidx]);
273 if (sym != NULL)
274 goto found_it;
278 /* If we have seen exactly one versioned symbol while we are
279 looking for an unversioned symbol and the version is not the
280 default version we still accept this symbol since there are
281 no possible ambiguities. */
282 sym = num_versions == 1 ? versioned_sym : NULL;
284 if (sym != NULL)
286 found_it:
287 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
289 case STB_WEAK:
290 /* Weak definition. Use this value if we don't find another. */
291 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
293 if (! result->s)
295 result->s = sym;
296 result->m = (struct link_map *) map;
298 break;
300 /* FALLTHROUGH */
301 case STB_GLOBAL:
302 success:
303 /* Global definition. Just what we need. */
304 result->s = sym;
305 result->m = (struct link_map *) map;
306 return 1;
308 case STB_GNU_UNIQUE:;
309 /* We have to determine whether we already found a
310 symbol with this name before. If not then we have to
311 add it to the search table. If we already found a
312 definition we have to use it. */
313 void enter (struct unique_sym *table, size_t size,
314 unsigned int hash, const char *name,
315 const ElfW(Sym) *sym, struct link_map *map)
317 size_t idx = hash % size;
318 size_t hash2 = 1 + hash % (size - 2);
319 while (1)
321 if (table[idx].name == NULL)
323 table[idx].hashval = hash;
324 table[idx].name = name;
325 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
327 table[idx].sym = ref;
328 table[idx].map = undef_map;
330 else
332 table[idx].sym = sym;
333 table[idx].map = map;
335 if (map->l_type == lt_loaded)
336 /* Make sure we don't unload this object by
337 setting the appropriate flag. */
338 map->l_flags_1 |= DF_1_NODELETE;
341 return;
344 idx += hash2;
345 if (idx >= size)
346 idx -= size;
350 struct unique_sym_table *tab
351 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
353 __rtld_lock_lock_recursive (tab->lock);
355 struct unique_sym *entries = tab->entries;
356 size_t size = tab->size;
357 if (entries != NULL)
359 size_t idx = new_hash % size;
360 size_t hash2 = 1 + new_hash % (size - 2);
361 while (1)
363 if (entries[idx].hashval == new_hash
364 && strcmp (entries[idx].name, undef_name) == 0)
366 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
368 /* We possibly have to initialize the central
369 copy from the copy addressed through the
370 relocation. */
371 result->s = sym;
372 result->m = (struct link_map *) map;
374 else
376 result->s = entries[idx].sym;
377 result->m = (struct link_map *) entries[idx].map;
379 __rtld_lock_unlock_recursive (tab->lock);
380 return 1;
383 if (entries[idx].name == NULL)
384 break;
386 idx += hash2;
387 if (idx >= size)
388 idx -= size;
391 if (size * 3 <= tab->n_elements * 4)
393 /* Expand the table. */
394 #ifdef RTLD_CHECK_FOREIGN_CALL
395 /* This must not happen during runtime relocations. */
396 assert (!RTLD_CHECK_FOREIGN_CALL);
397 #endif
398 size_t newsize = _dl_higher_prime_number (size + 1);
399 struct unique_sym *newentries
400 = calloc (sizeof (struct unique_sym), newsize);
401 if (newentries == NULL)
403 nomem:
404 __rtld_lock_unlock_recursive (tab->lock);
405 _dl_fatal_printf ("out of memory\n");
408 for (idx = 0; idx < size; ++idx)
409 if (entries[idx].name != NULL)
410 enter (newentries, newsize, entries[idx].hashval,
411 entries[idx].name, entries[idx].sym,
412 entries[idx].map);
414 tab->free (entries);
415 tab->size = newsize;
416 size = newsize;
417 entries = tab->entries = newentries;
418 tab->free = free;
421 else
423 #ifdef RTLD_CHECK_FOREIGN_CALL
424 /* This must not happen during runtime relocations. */
425 assert (!RTLD_CHECK_FOREIGN_CALL);
426 #endif
428 #ifdef SHARED
429 /* If tab->entries is NULL, but tab->size is not, it means
430 this is the second, conflict finding, lookup for
431 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
432 allocate anything and don't enter anything into the
433 hash table. */
434 if (__builtin_expect (tab->size, 0))
436 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
437 __rtld_lock_unlock_recursive (tab->lock);
438 goto success;
440 #endif
442 #define INITIAL_NUNIQUE_SYM_TABLE 31
443 size = INITIAL_NUNIQUE_SYM_TABLE;
444 entries = calloc (sizeof (struct unique_sym), size);
445 if (entries == NULL)
446 goto nomem;
448 tab->entries = entries;
449 tab->size = size;
450 tab->free = free;
453 enter (entries, size, new_hash, strtab + sym->st_name, sym,
454 (struct link_map *) map);
455 ++tab->n_elements;
457 __rtld_lock_unlock_recursive (tab->lock);
459 goto success;
461 default:
462 /* Local symbols are ignored. */
463 break;
467 /* If this current map is the one mentioned in the verneed entry
468 and we have not found a weak entry, it is a bug. */
469 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
470 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
471 return -1;
473 while (++i < n);
475 /* We have not found anything until now. */
476 return 0;
480 static uint_fast32_t
481 dl_new_hash (const char *s)
483 uint_fast32_t h = 5381;
484 for (unsigned char c = *s; c != '\0'; c = *++s)
485 h = h * 33 + c;
486 return h & 0xffffffff;
490 /* Add extra dependency on MAP to UNDEF_MAP. */
491 static int
492 internal_function
493 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
495 struct link_map *runp;
496 unsigned int i;
497 int result = 0;
499 /* Avoid self-references and references to objects which cannot be
500 unloaded anyway. */
501 if (undef_map == map)
502 return 0;
504 /* Avoid references to objects which cannot be unloaded anyway. */
505 assert (map->l_type == lt_loaded);
506 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
507 return 0;
509 struct link_map_reldeps *l_reldeps
510 = atomic_forced_read (undef_map->l_reldeps);
512 /* Make sure l_reldeps is read before l_initfini. */
513 atomic_read_barrier ();
515 /* Determine whether UNDEF_MAP already has a reference to MAP. First
516 look in the normal dependencies. */
517 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
518 if (l_initfini != NULL)
520 for (i = 0; l_initfini[i] != NULL; ++i)
521 if (l_initfini[i] == map)
522 return 0;
525 /* No normal dependency. See whether we already had to add it
526 to the special list of dynamic dependencies. */
527 unsigned int l_reldepsact = 0;
528 if (l_reldeps != NULL)
530 struct link_map **list = &l_reldeps->list[0];
531 l_reldepsact = l_reldeps->act;
532 for (i = 0; i < l_reldepsact; ++i)
533 if (list[i] == map)
534 return 0;
537 /* Save serial number of the target MAP. */
538 unsigned long long serial = map->l_serial;
540 /* Make sure nobody can unload the object while we are at it. */
541 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
543 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
544 here, that can result in ABBA deadlock. */
545 THREAD_GSCOPE_RESET_FLAG ();
546 __rtld_lock_lock_recursive (GL(dl_load_lock));
547 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
548 it can e.g. point to unallocated memory. So avoid the optimizer
549 treating the above read from MAP->l_serial as ensurance it
550 can safely dereference it. */
551 map = atomic_forced_read (map);
553 /* From this point on it is unsafe to dereference MAP, until it
554 has been found in one of the lists. */
556 /* Redo the l_initfini check in case undef_map's l_initfini
557 changed in the mean time. */
558 if (undef_map->l_initfini != l_initfini
559 && undef_map->l_initfini != NULL)
561 l_initfini = undef_map->l_initfini;
562 for (i = 0; l_initfini[i] != NULL; ++i)
563 if (l_initfini[i] == map)
564 goto out_check;
567 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
568 the mean time. */
569 if (undef_map->l_reldeps != NULL)
571 if (undef_map->l_reldeps != l_reldeps)
573 struct link_map **list = &undef_map->l_reldeps->list[0];
574 l_reldepsact = undef_map->l_reldeps->act;
575 for (i = 0; i < l_reldepsact; ++i)
576 if (list[i] == map)
577 goto out_check;
579 else if (undef_map->l_reldeps->act > l_reldepsact)
581 struct link_map **list
582 = &undef_map->l_reldeps->list[0];
583 i = l_reldepsact;
584 l_reldepsact = undef_map->l_reldeps->act;
585 for (; i < l_reldepsact; ++i)
586 if (list[i] == map)
587 goto out_check;
591 else
592 __rtld_lock_lock_recursive (GL(dl_load_lock));
594 /* The object is not yet in the dependency list. Before we add
595 it make sure just one more time the object we are about to
596 reference is still available. There is a brief period in
597 which the object could have been removed since we found the
598 definition. */
599 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
600 while (runp != NULL && runp != map)
601 runp = runp->l_next;
603 if (runp != NULL)
605 /* The object is still available. */
607 /* MAP could have been dlclosed, freed and then some other dlopened
608 library could have the same link_map pointer. */
609 if (map->l_serial != serial)
610 goto out_check;
612 /* Redo the NODELETE check, as when dl_load_lock wasn't held
613 yet this could have changed. */
614 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
615 goto out;
617 /* If the object with the undefined reference cannot be removed ever
618 just make sure the same is true for the object which contains the
619 definition. */
620 if (undef_map->l_type != lt_loaded
621 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
623 map->l_flags_1 |= DF_1_NODELETE;
624 goto out;
627 /* Add the reference now. */
628 if (__builtin_expect (l_reldepsact >= undef_map->l_reldepsmax, 0))
630 /* Allocate more memory for the dependency list. Since this
631 can never happen during the startup phase we can use
632 `realloc'. */
633 struct link_map_reldeps *newp;
634 unsigned int max
635 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
637 #ifdef RTLD_PREPARE_FOREIGN_CALL
638 RTLD_PREPARE_FOREIGN_CALL;
639 #endif
641 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
642 if (newp == NULL)
644 /* If we didn't manage to allocate memory for the list this is
645 no fatal problem. We simply make sure the referenced object
646 cannot be unloaded. This is semantically the correct
647 behavior. */
648 map->l_flags_1 |= DF_1_NODELETE;
649 goto out;
651 else
653 if (l_reldepsact)
654 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
655 l_reldepsact * sizeof (struct link_map *));
656 newp->list[l_reldepsact] = map;
657 newp->act = l_reldepsact + 1;
658 atomic_write_barrier ();
659 void *old = undef_map->l_reldeps;
660 undef_map->l_reldeps = newp;
661 undef_map->l_reldepsmax = max;
662 if (old)
663 _dl_scope_free (old);
666 else
668 undef_map->l_reldeps->list[l_reldepsact] = map;
669 atomic_write_barrier ();
670 undef_map->l_reldeps->act = l_reldepsact + 1;
673 /* Display information if we are debugging. */
674 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
675 _dl_debug_printf ("\
676 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
677 map->l_name[0] ? map->l_name : rtld_progname,
678 map->l_ns,
679 undef_map->l_name[0]
680 ? undef_map->l_name : rtld_progname,
681 undef_map->l_ns);
683 else
684 /* Whoa, that was bad luck. We have to search again. */
685 result = -1;
687 out:
688 /* Release the lock. */
689 __rtld_lock_unlock_recursive (GL(dl_load_lock));
691 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
692 THREAD_GSCOPE_SET_FLAG ();
694 return result;
696 out_check:
697 if (map->l_serial != serial)
698 result = -1;
699 goto out;
702 static void
703 internal_function
704 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
705 const ElfW(Sym) **ref, struct sym_val *value,
706 const struct r_found_version *version, int type_class,
707 int protected);
710 /* Search loaded objects' symbol tables for a definition of the symbol
711 UNDEF_NAME, perhaps with a requested version for the symbol.
713 We must never have calls to the audit functions inside this function
714 or in any function which gets called. If this would happen the audit
715 code might create a thread which can throw off all the scope locking. */
716 lookup_t
717 internal_function
718 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
719 const ElfW(Sym) **ref,
720 struct r_scope_elem *symbol_scope[],
721 const struct r_found_version *version,
722 int type_class, int flags, struct link_map *skip_map)
724 const uint_fast32_t new_hash = dl_new_hash (undef_name);
725 unsigned long int old_hash = 0xffffffff;
726 struct sym_val current_value = { NULL, NULL };
727 struct r_scope_elem **scope = symbol_scope;
729 bump_num_relocations ();
731 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
732 is allowed if we look up a versioned symbol. */
733 assert (version == NULL
734 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
735 == 0);
737 size_t i = 0;
738 if (__builtin_expect (skip_map != NULL, 0))
739 /* Search the relevant loaded objects for a definition. */
740 while ((*scope)->r_list[i] != skip_map)
741 ++i;
743 /* Search the relevant loaded objects for a definition. */
744 for (size_t start = i; *scope != NULL; start = 0, ++scope)
746 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
747 &current_value, *scope, start, version, flags,
748 skip_map, type_class, undef_map);
749 if (res > 0)
750 break;
752 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
754 /* Oh, oh. The file named in the relocation entry does not
755 contain the needed symbol. This code is never reached
756 for unversioned lookups. */
757 assert (version != NULL);
758 const char *reference_name = undef_map ? undef_map->l_name : NULL;
760 /* XXX We cannot translate the message. */
761 _dl_signal_cerror (0, (reference_name[0]
762 ? reference_name
763 : (rtld_progname ?: "<main program>")),
764 N_("relocation error"),
765 make_string ("symbol ", undef_name, ", version ",
766 version->name,
767 " not defined in file ",
768 version->filename,
769 " with link time reference",
770 res == -2
771 ? " (no version symbols)" : ""));
772 *ref = NULL;
773 return 0;
777 if (__builtin_expect (current_value.s == NULL, 0))
779 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
780 && skip_map == NULL)
782 /* We could find no value for a strong reference. */
783 const char *reference_name = undef_map ? undef_map->l_name : "";
784 const char *versionstr = version ? ", version " : "";
785 const char *versionname = (version && version->name
786 ? version->name : "");
788 /* XXX We cannot translate the message. */
789 _dl_signal_cerror (0, (reference_name[0]
790 ? reference_name
791 : (rtld_progname ?: "<main program>")),
792 N_("symbol lookup error"),
793 make_string (undefined_msg, undef_name,
794 versionstr, versionname));
796 *ref = NULL;
797 return 0;
800 int protected = (*ref
801 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
802 if (__builtin_expect (protected != 0, 0))
804 /* It is very tricky. We need to figure out what value to
805 return for the protected symbol. */
806 if (type_class == ELF_RTYPE_CLASS_PLT)
808 if (current_value.s != NULL && current_value.m != undef_map)
810 current_value.s = *ref;
811 current_value.m = undef_map;
814 else
816 struct sym_val protected_value = { NULL, NULL };
818 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
819 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
820 &protected_value, *scope, i, version, flags,
821 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
822 break;
824 if (protected_value.s != NULL && protected_value.m != undef_map)
826 current_value.s = *ref;
827 current_value.m = undef_map;
832 /* We have to check whether this would bind UNDEF_MAP to an object
833 in the global scope which was dynamically loaded. In this case
834 we have to prevent the latter from being unloaded unless the
835 UNDEF_MAP object is also unloaded. */
836 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
837 /* Don't do this for explicit lookups as opposed to implicit
838 runtime lookups. */
839 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
840 /* Add UNDEF_MAP to the dependencies. */
841 && add_dependency (undef_map, current_value.m, flags) < 0)
842 /* Something went wrong. Perhaps the object we tried to reference
843 was just removed. Try finding another definition. */
844 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
845 (flags & DL_LOOKUP_GSCOPE_LOCK)
846 ? undef_map->l_scope : symbol_scope,
847 version, type_class, flags, skip_map);
849 /* The object is used. */
850 if (__builtin_expect (current_value.m->l_used == 0, 0))
851 current_value.m->l_used = 1;
853 if (__builtin_expect (GLRO(dl_debug_mask)
854 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
855 _dl_debug_bindings (undef_name, undef_map, ref,
856 &current_value, version, type_class, protected);
858 *ref = current_value.s;
859 return LOOKUP_VALUE (current_value.m);
863 /* Cache the location of MAP's hash table. */
865 void
866 internal_function
867 _dl_setup_hash (struct link_map *map)
869 Elf_Symndx *hash;
870 Elf_Symndx nchain;
872 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
873 + DT_THISPROCNUM + DT_VERSIONTAGNUM
874 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
876 Elf32_Word *hash32
877 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
878 + DT_THISPROCNUM + DT_VERSIONTAGNUM
879 + DT_EXTRANUM + DT_VALNUM]);
880 map->l_nbuckets = *hash32++;
881 Elf32_Word symbias = *hash32++;
882 Elf32_Word bitmask_nwords = *hash32++;
883 /* Must be a power of two. */
884 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
885 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
886 map->l_gnu_shift = *hash32++;
888 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
889 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
891 map->l_gnu_buckets = hash32;
892 hash32 += map->l_nbuckets;
893 map->l_gnu_chain_zero = hash32 - symbias;
894 return;
897 if (!map->l_info[DT_HASH])
898 return;
899 hash = (void *) D_PTR (map, l_info[DT_HASH]);
901 map->l_nbuckets = *hash++;
902 nchain = *hash++;
903 map->l_buckets = hash;
904 hash += map->l_nbuckets;
905 map->l_chain = hash;
909 static void
910 internal_function
911 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
912 const ElfW(Sym) **ref, struct sym_val *value,
913 const struct r_found_version *version, int type_class,
914 int protected)
916 const char *reference_name = undef_map->l_name;
918 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
920 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
921 (reference_name[0]
922 ? reference_name
923 : (rtld_progname ?: "<main program>")),
924 undef_map->l_ns,
925 value->m->l_name[0] ? value->m->l_name : rtld_progname,
926 value->m->l_ns,
927 protected ? "protected" : "normal", undef_name);
928 if (version)
929 _dl_debug_printf_c (" [%s]\n", version->name);
930 else
931 _dl_debug_printf_c ("\n");
933 #ifdef SHARED
934 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
936 int conflict = 0;
937 struct sym_val val = { NULL, NULL };
939 if ((GLRO(dl_trace_prelink_map) == NULL
940 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
941 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
943 const uint_fast32_t new_hash = dl_new_hash (undef_name);
944 unsigned long int old_hash = 0xffffffff;
945 struct unique_sym *saved_entries
946 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
948 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
949 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
950 undef_map->l_local_scope[0], 0, version, 0, NULL,
951 type_class, undef_map);
952 if (val.s != value->s || val.m != value->m)
953 conflict = 1;
954 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
955 && val.s
956 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
957 STB_GLOBAL) == STB_GNU_UNIQUE)
959 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
960 contains any DT_SYMBOLIC libraries, unfortunately there
961 can be conflicts even if the above is equal. As symbol
962 resolution goes from the last library to the first and
963 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
964 library, it would be the one that is looked up. */
965 struct sym_val val2 = { NULL, NULL };
966 size_t n;
967 struct r_scope_elem *scope = undef_map->l_local_scope[0];
969 for (n = 0; n < scope->r_nlist; n++)
970 if (scope->r_list[n] == val.m)
971 break;
973 for (n++; n < scope->r_nlist; n++)
974 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
975 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
976 &val2,
977 &scope->r_list[n]->l_symbolic_searchlist,
978 0, version, 0, NULL, type_class,
979 undef_map) > 0)
981 conflict = 1;
982 val = val2;
983 break;
986 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
989 if (value->s)
991 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
992 == STT_TLS, 0))
993 type_class = 4;
994 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
995 == STT_GNU_IFUNC, 0))
996 type_class |= 8;
999 if (conflict
1000 || GLRO(dl_trace_prelink_map) == undef_map
1001 || GLRO(dl_trace_prelink_map) == NULL
1002 || type_class >= 4)
1004 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1005 conflict ? "conflict" : "lookup",
1006 (int) sizeof (ElfW(Addr)) * 2,
1007 (size_t) undef_map->l_map_start,
1008 (int) sizeof (ElfW(Addr)) * 2,
1009 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1010 (int) sizeof (ElfW(Addr)) * 2,
1011 (size_t) (value->s ? value->m->l_map_start : 0),
1012 (int) sizeof (ElfW(Addr)) * 2,
1013 (size_t) (value->s ? value->s->st_value : 0));
1015 if (conflict)
1016 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1017 (int) sizeof (ElfW(Addr)) * 2,
1018 (size_t) (val.s ? val.m->l_map_start : 0),
1019 (int) sizeof (ElfW(Addr)) * 2,
1020 (size_t) (val.s ? val.s->st_value : 0));
1022 _dl_printf ("/%x %s\n", type_class, undef_name);
1025 #endif