Update include paths following the move of alpha to ports
[glibc.git] / sysdeps / mips / dl-lookup.c
blobb7516dab1a38ecc2e15875de874630b9d81d67ee
1 /* Look up a symbol in the loaded objects.
2 MIPS/Linux version - special handling of non-PIC undefined symbol rules.
3 Copyright (C) 1995-2005, 2006, 2007, 2009 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <alloca.h>
22 #include <libintl.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <ldsodefs.h>
27 #include <dl-hash.h>
28 #include <dl-machine.h>
29 #include <sysdep-cancel.h>
30 #include <bits/libc-lock.h>
31 #include <tls.h>
33 #include <assert.h>
35 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
37 /* We need this string more than once. */
38 static const char undefined_msg[] = "undefined symbol: ";
41 struct sym_val
43 const ElfW(Sym) *s;
44 struct link_map *m;
48 #define make_string(string, rest...) \
49 ({ \
50 const char *all[] = { string, ## rest }; \
51 size_t len, cnt; \
52 char *result, *cp; \
54 len = 1; \
55 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
56 len += strlen (all[cnt]); \
58 cp = result = alloca (len); \
59 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
60 cp = __stpcpy (cp, all[cnt]); \
62 result; \
65 /* Statistics function. */
66 #ifdef SHARED
67 # define bump_num_relocations() ++GL(dl_num_relocations)
68 #else
69 # define bump_num_relocations() ((void) 0)
70 #endif
73 /* Inner part of the lookup functions. We return a value > 0 if we
74 found the symbol, the value 0 if nothing is found and < 0 if
75 something bad happened. */
76 static int
77 __attribute_noinline__
78 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
79 unsigned long int *old_hash, const ElfW(Sym) *ref,
80 struct sym_val *result, struct r_scope_elem *scope, size_t i,
81 const struct r_found_version *const version, int flags,
82 struct link_map *skip, int type_class, struct link_map *undef_map)
84 size_t n = scope->r_nlist;
85 /* Make sure we read the value before proceeding. Otherwise we
86 might use r_list pointing to the initial scope and r_nlist being
87 the value after a resize. That is the only path in dl-open.c not
88 protected by GSCOPE. A read barrier here might be to expensive. */
89 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
90 struct link_map **list = scope->r_list;
94 /* These variables are used in the nested function. */
95 Elf_Symndx symidx;
96 int num_versions = 0;
97 const ElfW(Sym) *versioned_sym = NULL;
99 const struct link_map *map = list[i]->l_real;
101 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
102 if (map == skip)
103 continue;
105 /* Don't search the executable when resolving a copy reloc. */
106 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
107 continue;
109 /* Do not look into objects which are going to be removed. */
110 if (map->l_removed)
111 continue;
113 /* Print some debugging info if wanted. */
114 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
115 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
116 undef_name,
117 map->l_name[0] ? map->l_name : rtld_progname,
118 map->l_ns);
120 /* If the hash table is empty there is nothing to do here. */
121 if (map->l_nbuckets == 0)
122 continue;
124 /* The tables for this map. */
125 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
126 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
129 /* Nested routine to check whether the symbol matches. */
130 const ElfW(Sym) *
131 __attribute_noinline__
132 check_match (const ElfW(Sym) *sym)
134 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
135 assert (ELF_RTYPE_CLASS_PLT == 1);
136 /* The semantics of zero/non-zero values of undefined symbols
137 differs depending on whether the non-PIC ABI is in use.
138 Under the non-PIC ABI, a non-zero value indicates that
139 there is an address reference to the symbol and thus it
140 must always be resolved (except when resolving a jump slot
141 relocation) to the PLT entry whose address is provided as
142 the symbol's value; a zero value indicates that this
143 canonical-address behaviour is not required. Yet under the
144 classic MIPS psABI, a zero value indicates that there is an
145 address reference to the function and the dynamic linker
146 must resolve the symbol immediately upon loading. To avoid
147 conflict, symbols for which the dynamic linker must assume
148 the non-PIC ABI semantics are marked with the STO_MIPS_PLT
149 flag. */
150 if (__builtin_expect ((sym->st_value == 0 /* No value. */
151 && stt != STT_TLS)
152 || (sym->st_shndx == SHN_UNDEF
153 && !(sym->st_other & STO_MIPS_PLT))
154 || (type_class & (sym->st_shndx == SHN_UNDEF)),
156 return NULL;
158 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
159 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
160 code/data definitions. */
161 #define ALLOWED_STT \
162 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
163 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
164 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
165 return NULL;
167 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
168 /* Not the symbol we are looking for. */
169 return NULL;
171 const ElfW(Half) *verstab = map->l_versyms;
172 if (version != NULL)
174 if (__builtin_expect (verstab == NULL, 0))
176 /* We need a versioned symbol but haven't found any. If
177 this is the object which is referenced in the verneed
178 entry it is a bug in the library since a symbol must
179 not simply disappear.
181 It would also be a bug in the object since it means that
182 the list of required versions is incomplete and so the
183 tests in dl-version.c haven't found a problem.*/
184 assert (version->filename == NULL
185 || ! _dl_name_match_p (version->filename, map));
187 /* Otherwise we accept the symbol. */
189 else
191 /* We can match the version information or use the
192 default one if it is not hidden. */
193 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
194 if ((map->l_versions[ndx].hash != version->hash
195 || strcmp (map->l_versions[ndx].name, version->name))
196 && (version->hidden || map->l_versions[ndx].hash
197 || (verstab[symidx] & 0x8000)))
198 /* It's not the version we want. */
199 return NULL;
202 else
204 /* No specific version is selected. There are two ways we
205 can got here:
207 - a binary which does not include versioning information
208 is loaded
210 - dlsym() instead of dlvsym() is used to get a symbol which
211 might exist in more than one form
213 If the library does not provide symbol version information
214 there is no problem at at: we simply use the symbol if it
215 is defined.
217 These two lookups need to be handled differently if the
218 library defines versions. In the case of the old
219 unversioned application the oldest (default) version
220 should be used. In case of a dlsym() call the latest and
221 public interface should be returned. */
222 if (verstab != NULL)
224 if ((verstab[symidx] & 0x7fff)
225 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
227 /* Don't accept hidden symbols. */
228 if ((verstab[symidx] & 0x8000) == 0
229 && num_versions++ == 0)
230 /* No version so far. */
231 versioned_sym = sym;
233 return NULL;
238 /* There cannot be another entry for this symbol so stop here. */
239 return sym;
242 const ElfW(Sym) *sym;
243 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
244 if (__builtin_expect (bitmask != NULL, 1))
246 ElfW(Addr) bitmask_word
247 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
248 & map->l_gnu_bitmask_idxbits];
250 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
251 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
252 & (__ELF_NATIVE_CLASS - 1));
254 if (__builtin_expect ((bitmask_word >> hashbit1)
255 & (bitmask_word >> hashbit2) & 1, 0))
257 Elf32_Word bucket = map->l_gnu_buckets[new_hash
258 % map->l_nbuckets];
259 if (bucket != 0)
261 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
264 if (((*hasharr ^ new_hash) >> 1) == 0)
266 symidx = hasharr - map->l_gnu_chain_zero;
267 sym = check_match (&symtab[symidx]);
268 if (sym != NULL)
269 goto found_it;
271 while ((*hasharr++ & 1u) == 0);
274 /* No symbol found. */
275 symidx = SHN_UNDEF;
277 else
279 if (*old_hash == 0xffffffff)
280 *old_hash = _dl_elf_hash (undef_name);
282 /* Use the old SysV-style hash table. Search the appropriate
283 hash bucket in this object's symbol table for a definition
284 for the same symbol name. */
285 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
286 symidx != STN_UNDEF;
287 symidx = map->l_chain[symidx])
289 sym = check_match (&symtab[symidx]);
290 if (sym != NULL)
291 goto found_it;
295 /* If we have seen exactly one versioned symbol while we are
296 looking for an unversioned symbol and the version is not the
297 default version we still accept this symbol since there are
298 no possible ambiguities. */
299 sym = num_versions == 1 ? versioned_sym : NULL;
301 if (sym != NULL)
303 found_it:
304 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
306 case STB_WEAK:
307 /* Weak definition. Use this value if we don't find another. */
308 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
310 if (! result->s)
312 result->s = sym;
313 result->m = (struct link_map *) map;
315 break;
317 /* FALLTHROUGH */
318 case STB_GLOBAL:
319 success:
320 /* Global definition. Just what we need. */
321 result->s = sym;
322 result->m = (struct link_map *) map;
323 return 1;
325 case STB_GNU_UNIQUE:;
326 /* We have to determine whether we already found a
327 symbol with this name before. If not then we have to
328 add it to the search table. If we already found a
329 definition we have to use it. */
330 void enter (struct unique_sym *table, size_t size,
331 unsigned int hash, const char *name,
332 const ElfW(Sym) *sym, const struct link_map *map)
334 size_t idx = hash % size;
335 size_t hash2 = 1 + hash % (size - 2);
336 while (1)
338 if (table[idx].hashval == 0)
340 table[idx].hashval = hash;
341 table[idx].name = strtab + sym->st_name;
342 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
344 table[idx].sym = ref;
345 table[idx].map = undef_map;
347 else
349 table[idx].sym = sym;
350 table[idx].map = map;
352 return;
355 idx += hash2;
356 if (idx >= size)
357 idx -= size;
361 struct unique_sym_table *tab
362 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
364 __rtld_lock_lock_recursive (tab->lock);
366 struct unique_sym *entries = tab->entries;
367 size_t size = tab->size;
368 if (entries != NULL)
370 size_t idx = new_hash % size;
371 size_t hash2 = 1 + new_hash % (size - 2);
372 while (1)
374 if (entries[idx].hashval == new_hash
375 && strcmp (entries[idx].name, undef_name) == 0)
377 result->s = entries[idx].sym;
378 result->m = (struct link_map *) entries[idx].map;
379 __rtld_lock_unlock_recursive (tab->lock);
380 return 1;
383 if (entries[idx].hashval == 0
384 && entries[idx].name == NULL)
385 break;
387 idx += hash2;
388 if (idx >= size)
389 idx -= size;
392 if (size * 3 <= tab->n_elements)
394 /* Expand the table. */
395 size_t newsize = _dl_higher_prime_number (size);
396 struct unique_sym *newentries
397 = calloc (sizeof (struct unique_sym), newsize);
398 if (newentries == NULL)
400 nomem:
401 __rtld_lock_unlock_recursive (tab->lock);
402 _dl_fatal_printf ("out of memory\n");
405 for (idx = 0; idx < size; ++idx)
406 if (entries[idx].hashval != 0)
407 enter (newentries, newsize, entries[idx].hashval,
408 entries[idx].name, entries[idx].sym,
409 entries[idx].map);
411 tab->free (entries);
412 tab->size = newsize;
413 entries = tab->entries = newentries;
414 tab->free = free;
417 else
419 #define INITIAL_NUNIQUE_SYM_TABLE 31
420 size = INITIAL_NUNIQUE_SYM_TABLE;
421 entries = calloc (sizeof (struct unique_sym), size);
422 if (entries == NULL)
423 goto nomem;
425 tab->entries = entries;
426 tab->size = size;
427 tab->free = free;
430 enter (entries, size, new_hash, strtab + sym->st_name, sym, map);
431 ++tab->n_elements;
433 __rtld_lock_unlock_recursive (tab->lock);
435 goto success;
437 default:
438 /* Local symbols are ignored. */
439 break;
443 /* If this current map is the one mentioned in the verneed entry
444 and we have not found a weak entry, it is a bug. */
445 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
446 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
447 return -1;
449 while (++i < n);
451 /* We have not found anything until now. */
452 return 0;
456 static uint_fast32_t
457 dl_new_hash (const char *s)
459 uint_fast32_t h = 5381;
460 for (unsigned char c = *s; c != '\0'; c = *++s)
461 h = h * 33 + c;
462 return h & 0xffffffff;
466 /* Add extra dependency on MAP to UNDEF_MAP. */
467 static int
468 internal_function
469 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
471 struct link_map *runp;
472 unsigned int i;
473 int result = 0;
475 /* Avoid self-references and references to objects which cannot be
476 unloaded anyway. */
477 if (undef_map == map)
478 return 0;
480 /* Avoid references to objects which cannot be unloaded anyway. */
481 assert (map->l_type == lt_loaded);
482 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
483 return 0;
485 struct link_map_reldeps *l_reldeps
486 = atomic_forced_read (undef_map->l_reldeps);
488 /* Make sure l_reldeps is read before l_initfini. */
489 atomic_read_barrier ();
491 /* Determine whether UNDEF_MAP already has a reference to MAP. First
492 look in the normal dependencies. */
493 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
494 if (l_initfini != NULL)
496 for (i = 0; l_initfini[i] != NULL; ++i)
497 if (l_initfini[i] == map)
498 return 0;
501 /* No normal dependency. See whether we already had to add it
502 to the special list of dynamic dependencies. */
503 unsigned int l_reldepsact = 0;
504 if (l_reldeps != NULL)
506 struct link_map **list = &l_reldeps->list[0];
507 l_reldepsact = l_reldeps->act;
508 for (i = 0; i < l_reldepsact; ++i)
509 if (list[i] == map)
510 return 0;
513 /* Save serial number of the target MAP. */
514 unsigned long long serial = map->l_serial;
516 /* Make sure nobody can unload the object while we are at it. */
517 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
519 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
520 here, that can result in ABBA deadlock. */
521 THREAD_GSCOPE_RESET_FLAG ();
522 __rtld_lock_lock_recursive (GL(dl_load_lock));
523 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
524 it can e.g. point to unallocated memory. So avoid the optimizer
525 treating the above read from MAP->l_serial as ensurance it
526 can safely dereference it. */
527 map = atomic_forced_read (map);
529 /* From this point on it is unsafe to dereference MAP, until it
530 has been found in one of the lists. */
532 /* Redo the l_initfini check in case undef_map's l_initfini
533 changed in the mean time. */
534 if (undef_map->l_initfini != l_initfini
535 && undef_map->l_initfini != NULL)
537 l_initfini = undef_map->l_initfini;
538 for (i = 0; l_initfini[i] != NULL; ++i)
539 if (l_initfini[i] == map)
540 goto out_check;
543 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
544 the mean time. */
545 if (undef_map->l_reldeps != NULL)
547 if (undef_map->l_reldeps != l_reldeps)
549 struct link_map **list = &undef_map->l_reldeps->list[0];
550 l_reldepsact = undef_map->l_reldeps->act;
551 for (i = 0; i < l_reldepsact; ++i)
552 if (list[i] == map)
553 goto out_check;
555 else if (undef_map->l_reldeps->act > l_reldepsact)
557 struct link_map **list
558 = &undef_map->l_reldeps->list[0];
559 i = l_reldepsact;
560 l_reldepsact = undef_map->l_reldeps->act;
561 for (; i < l_reldepsact; ++i)
562 if (list[i] == map)
563 goto out_check;
567 else
568 __rtld_lock_lock_recursive (GL(dl_load_lock));
570 /* The object is not yet in the dependency list. Before we add
571 it make sure just one more time the object we are about to
572 reference is still available. There is a brief period in
573 which the object could have been removed since we found the
574 definition. */
575 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
576 while (runp != NULL && runp != map)
577 runp = runp->l_next;
579 if (runp != NULL)
581 /* The object is still available. */
583 /* MAP could have been dlclosed, freed and then some other dlopened
584 library could have the same link_map pointer. */
585 if (map->l_serial != serial)
586 goto out_check;
588 /* Redo the NODELETE check, as when dl_load_lock wasn't held
589 yet this could have changed. */
590 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
591 goto out;
593 /* If the object with the undefined reference cannot be removed ever
594 just make sure the same is true for the object which contains the
595 definition. */
596 if (undef_map->l_type != lt_loaded
597 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
599 map->l_flags_1 |= DF_1_NODELETE;
600 goto out;
603 /* Add the reference now. */
604 if (__builtin_expect (l_reldepsact >= undef_map->l_reldepsmax, 0))
606 /* Allocate more memory for the dependency list. Since this
607 can never happen during the startup phase we can use
608 `realloc'. */
609 struct link_map_reldeps *newp;
610 unsigned int max
611 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
613 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
614 if (newp == NULL)
616 /* If we didn't manage to allocate memory for the list this is
617 no fatal problem. We simply make sure the referenced object
618 cannot be unloaded. This is semantically the correct
619 behavior. */
620 map->l_flags_1 |= DF_1_NODELETE;
621 goto out;
623 else
625 if (l_reldepsact)
626 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
627 l_reldepsact * sizeof (struct link_map *));
628 newp->list[l_reldepsact] = map;
629 newp->act = l_reldepsact + 1;
630 atomic_write_barrier ();
631 void *old = undef_map->l_reldeps;
632 undef_map->l_reldeps = newp;
633 undef_map->l_reldepsmax = max;
634 if (old)
635 _dl_scope_free (old);
638 else
640 undef_map->l_reldeps->list[l_reldepsact] = map;
641 atomic_write_barrier ();
642 undef_map->l_reldeps->act = l_reldepsact + 1;
645 /* Display information if we are debugging. */
646 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
647 _dl_debug_printf ("\
648 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
649 map->l_name[0] ? map->l_name : rtld_progname,
650 map->l_ns,
651 undef_map->l_name[0]
652 ? undef_map->l_name : rtld_progname,
653 undef_map->l_ns);
655 else
656 /* Whoa, that was bad luck. We have to search again. */
657 result = -1;
659 out:
660 /* Release the lock. */
661 __rtld_lock_unlock_recursive (GL(dl_load_lock));
663 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
664 THREAD_GSCOPE_SET_FLAG ();
666 return result;
668 out_check:
669 if (map->l_serial != serial)
670 result = -1;
671 goto out;
674 static void
675 internal_function
676 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
677 const ElfW(Sym) **ref, struct sym_val *value,
678 const struct r_found_version *version, int type_class,
679 int protected);
682 /* Search loaded objects' symbol tables for a definition of the symbol
683 UNDEF_NAME, perhaps with a requested version for the symbol.
685 We must never have calls to the audit functions inside this function
686 or in any function which gets called. If this would happen the audit
687 code might create a thread which can throw off all the scope locking. */
688 lookup_t
689 internal_function
690 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
691 const ElfW(Sym) **ref,
692 struct r_scope_elem *symbol_scope[],
693 const struct r_found_version *version,
694 int type_class, int flags, struct link_map *skip_map)
696 const uint_fast32_t new_hash = dl_new_hash (undef_name);
697 unsigned long int old_hash = 0xffffffff;
698 struct sym_val current_value = { NULL, NULL };
699 struct r_scope_elem **scope = symbol_scope;
701 bump_num_relocations ();
703 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
704 is allowed if we look up a versioned symbol. */
705 assert (version == NULL
706 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
707 == 0);
709 size_t i = 0;
710 if (__builtin_expect (skip_map != NULL, 0))
711 /* Search the relevant loaded objects for a definition. */
712 while ((*scope)->r_list[i] != skip_map)
713 ++i;
715 /* Search the relevant loaded objects for a definition. */
716 for (size_t start = i; *scope != NULL; start = 0, ++scope)
718 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
719 &current_value, *scope, start, version, flags,
720 skip_map, type_class, undef_map);
721 if (res > 0)
722 break;
724 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
726 /* Oh, oh. The file named in the relocation entry does not
727 contain the needed symbol. This code is never reached
728 for unversioned lookups. */
729 assert (version != NULL);
730 const char *reference_name = undef_map ? undef_map->l_name : NULL;
732 /* XXX We cannot translate the message. */
733 _dl_signal_cerror (0, (reference_name[0]
734 ? reference_name
735 : (rtld_progname ?: "<main program>")),
736 N_("relocation error"),
737 make_string ("symbol ", undef_name, ", version ",
738 version->name,
739 " not defined in file ",
740 version->filename,
741 " with link time reference",
742 res == -2
743 ? " (no version symbols)" : ""));
744 *ref = NULL;
745 return 0;
749 if (__builtin_expect (current_value.s == NULL, 0))
751 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
752 && skip_map == NULL)
754 /* We could find no value for a strong reference. */
755 const char *reference_name = undef_map ? undef_map->l_name : "";
756 const char *versionstr = version ? ", version " : "";
757 const char *versionname = (version && version->name
758 ? version->name : "");
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_("symbol lookup error"),
765 make_string (undefined_msg, undef_name,
766 versionstr, versionname));
768 *ref = NULL;
769 return 0;
772 int protected = (*ref
773 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
774 if (__builtin_expect (protected != 0, 0))
776 /* It is very tricky. We need to figure out what value to
777 return for the protected symbol. */
778 if (type_class == ELF_RTYPE_CLASS_PLT)
780 if (current_value.s != NULL && current_value.m != undef_map)
782 current_value.s = *ref;
783 current_value.m = undef_map;
786 else
788 struct sym_val protected_value = { NULL, NULL };
790 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
791 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
792 &protected_value, *scope, i, version, flags,
793 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
794 break;
796 if (protected_value.s != NULL && protected_value.m != undef_map)
798 current_value.s = *ref;
799 current_value.m = undef_map;
804 /* We have to check whether this would bind UNDEF_MAP to an object
805 in the global scope which was dynamically loaded. In this case
806 we have to prevent the latter from being unloaded unless the
807 UNDEF_MAP object is also unloaded. */
808 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
809 /* Don't do this for explicit lookups as opposed to implicit
810 runtime lookups. */
811 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
812 /* Add UNDEF_MAP to the dependencies. */
813 && add_dependency (undef_map, current_value.m, flags) < 0)
814 /* Something went wrong. Perhaps the object we tried to reference
815 was just removed. Try finding another definition. */
816 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
817 (flags & DL_LOOKUP_GSCOPE_LOCK)
818 ? undef_map->l_scope : symbol_scope,
819 version, type_class, flags, skip_map);
821 /* The object is used. */
822 current_value.m->l_used = 1;
824 if (__builtin_expect (GLRO(dl_debug_mask)
825 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
826 _dl_debug_bindings (undef_name, undef_map, ref,
827 &current_value, version, type_class, protected);
829 *ref = current_value.s;
830 return LOOKUP_VALUE (current_value.m);
834 /* Cache the location of MAP's hash table. */
836 void
837 internal_function
838 _dl_setup_hash (struct link_map *map)
840 Elf_Symndx *hash;
841 Elf_Symndx nchain;
843 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
844 + DT_THISPROCNUM + DT_VERSIONTAGNUM
845 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
847 Elf32_Word *hash32
848 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
849 + DT_THISPROCNUM + DT_VERSIONTAGNUM
850 + DT_EXTRANUM + DT_VALNUM]);
851 map->l_nbuckets = *hash32++;
852 Elf32_Word symbias = *hash32++;
853 Elf32_Word bitmask_nwords = *hash32++;
854 /* Must be a power of two. */
855 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
856 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
857 map->l_gnu_shift = *hash32++;
859 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
860 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
862 map->l_gnu_buckets = hash32;
863 hash32 += map->l_nbuckets;
864 map->l_gnu_chain_zero = hash32 - symbias;
865 return;
868 if (!map->l_info[DT_HASH])
869 return;
870 hash = (void *) D_PTR (map, l_info[DT_HASH]);
872 map->l_nbuckets = *hash++;
873 nchain = *hash++;
874 map->l_buckets = hash;
875 hash += map->l_nbuckets;
876 map->l_chain = hash;
880 static void
881 internal_function
882 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
883 const ElfW(Sym) **ref, struct sym_val *value,
884 const struct r_found_version *version, int type_class,
885 int protected)
887 const char *reference_name = undef_map->l_name;
889 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
891 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
892 (reference_name[0]
893 ? reference_name
894 : (rtld_progname ?: "<main program>")),
895 undef_map->l_ns,
896 value->m->l_name[0] ? value->m->l_name : rtld_progname,
897 value->m->l_ns,
898 protected ? "protected" : "normal", undef_name);
899 if (version)
900 _dl_debug_printf_c (" [%s]\n", version->name);
901 else
902 _dl_debug_printf_c ("\n");
904 #ifdef SHARED
905 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
907 int conflict = 0;
908 struct sym_val val = { NULL, NULL };
910 if ((GLRO(dl_trace_prelink_map) == NULL
911 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
912 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
914 const uint_fast32_t new_hash = dl_new_hash (undef_name);
915 unsigned long int old_hash = 0xffffffff;
917 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
918 undef_map->l_local_scope[0], 0, version, 0, NULL,
919 type_class, undef_map);
921 if (val.s != value->s || val.m != value->m)
922 conflict = 1;
925 if (value->s)
927 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
928 == STT_TLS, 0))
929 type_class = 4;
930 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
931 == STT_GNU_IFUNC, 0))
932 type_class |= 8;
935 if (conflict
936 || GLRO(dl_trace_prelink_map) == undef_map
937 || GLRO(dl_trace_prelink_map) == NULL
938 || type_class >= 4)
940 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
941 conflict ? "conflict" : "lookup",
942 (int) sizeof (ElfW(Addr)) * 2,
943 (size_t) undef_map->l_map_start,
944 (int) sizeof (ElfW(Addr)) * 2,
945 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
946 (int) sizeof (ElfW(Addr)) * 2,
947 (size_t) (value->s ? value->m->l_map_start : 0),
948 (int) sizeof (ElfW(Addr)) * 2,
949 (size_t) (value->s ? value->s->st_value : 0));
951 if (conflict)
952 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
953 (int) sizeof (ElfW(Addr)) * 2,
954 (size_t) (val.s ? val.m->l_map_start : 0),
955 (int) sizeof (ElfW(Addr)) * 2,
956 (size_t) (val.s ? val.s->st_value : 0));
958 _dl_printf ("/%x %s\n", type_class, undef_name);
961 #endif