Fix ChangeLogs.
[glibc.git] / elf / dl-lookup.c
blob78c8669e30d403420a0910be14bdeb9faf24c807
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2005, 2006, 2007, 2009, 2010
3 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 if (__builtin_expect ((sym->st_value == 0 /* No value. */
137 && stt != STT_TLS)
138 || (type_class & (sym->st_shndx == SHN_UNDEF)),
140 return NULL;
142 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
143 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
144 code/data definitions. */
145 #define ALLOWED_STT \
146 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
147 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
148 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
149 return NULL;
151 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
152 /* Not the symbol we are looking for. */
153 return NULL;
155 const ElfW(Half) *verstab = map->l_versyms;
156 if (version != NULL)
158 if (__builtin_expect (verstab == NULL, 0))
160 /* We need a versioned symbol but haven't found any. If
161 this is the object which is referenced in the verneed
162 entry it is a bug in the library since a symbol must
163 not simply disappear.
165 It would also be a bug in the object since it means that
166 the list of required versions is incomplete and so the
167 tests in dl-version.c haven't found a problem.*/
168 assert (version->filename == NULL
169 || ! _dl_name_match_p (version->filename, map));
171 /* Otherwise we accept the symbol. */
173 else
175 /* We can match the version information or use the
176 default one if it is not hidden. */
177 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
178 if ((map->l_versions[ndx].hash != version->hash
179 || strcmp (map->l_versions[ndx].name, version->name))
180 && (version->hidden || map->l_versions[ndx].hash
181 || (verstab[symidx] & 0x8000)))
182 /* It's not the version we want. */
183 return NULL;
186 else
188 /* No specific version is selected. There are two ways we
189 can got here:
191 - a binary which does not include versioning information
192 is loaded
194 - dlsym() instead of dlvsym() is used to get a symbol which
195 might exist in more than one form
197 If the library does not provide symbol version information
198 there is no problem at at: we simply use the symbol if it
199 is defined.
201 These two lookups need to be handled differently if the
202 library defines versions. In the case of the old
203 unversioned application the oldest (default) version
204 should be used. In case of a dlsym() call the latest and
205 public interface should be returned. */
206 if (verstab != NULL)
208 if ((verstab[symidx] & 0x7fff)
209 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
211 /* Don't accept hidden symbols. */
212 if ((verstab[symidx] & 0x8000) == 0
213 && num_versions++ == 0)
214 /* No version so far. */
215 versioned_sym = sym;
217 return NULL;
222 /* There cannot be another entry for this symbol so stop here. */
223 return sym;
226 const ElfW(Sym) *sym;
227 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
228 if (__builtin_expect (bitmask != NULL, 1))
230 ElfW(Addr) bitmask_word
231 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
232 & map->l_gnu_bitmask_idxbits];
234 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
235 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
236 & (__ELF_NATIVE_CLASS - 1));
238 if (__builtin_expect ((bitmask_word >> hashbit1)
239 & (bitmask_word >> hashbit2) & 1, 0))
241 Elf32_Word bucket = map->l_gnu_buckets[new_hash
242 % map->l_nbuckets];
243 if (bucket != 0)
245 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
248 if (((*hasharr ^ new_hash) >> 1) == 0)
250 symidx = hasharr - map->l_gnu_chain_zero;
251 sym = check_match (&symtab[symidx]);
252 if (sym != NULL)
253 goto found_it;
255 while ((*hasharr++ & 1u) == 0);
258 /* No symbol found. */
259 symidx = SHN_UNDEF;
261 else
263 if (*old_hash == 0xffffffff)
264 *old_hash = _dl_elf_hash (undef_name);
266 /* Use the old SysV-style hash table. Search the appropriate
267 hash bucket in this object's symbol table for a definition
268 for the same symbol name. */
269 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
270 symidx != STN_UNDEF;
271 symidx = map->l_chain[symidx])
273 sym = check_match (&symtab[symidx]);
274 if (sym != NULL)
275 goto found_it;
279 /* If we have seen exactly one versioned symbol while we are
280 looking for an unversioned symbol and the version is not the
281 default version we still accept this symbol since there are
282 no possible ambiguities. */
283 sym = num_versions == 1 ? versioned_sym : NULL;
285 if (sym != NULL)
287 found_it:
288 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
290 case STB_WEAK:
291 /* Weak definition. Use this value if we don't find another. */
292 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
294 if (! result->s)
296 result->s = sym;
297 result->m = (struct link_map *) map;
299 break;
301 /* FALLTHROUGH */
302 case STB_GLOBAL:
303 success:
304 /* Global definition. Just what we need. */
305 result->s = sym;
306 result->m = (struct link_map *) map;
307 return 1;
309 case STB_GNU_UNIQUE:;
310 /* We have to determine whether we already found a
311 symbol with this name before. If not then we have to
312 add it to the search table. If we already found a
313 definition we have to use it. */
314 void enter (struct unique_sym *table, size_t size,
315 unsigned int hash, const char *name,
316 const ElfW(Sym) *sym, struct link_map *map)
318 size_t idx = hash % size;
319 size_t hash2 = 1 + hash % (size - 2);
320 while (1)
322 if (table[idx].name == NULL)
324 table[idx].hashval = hash;
325 table[idx].name = name;
326 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
328 table[idx].sym = ref;
329 table[idx].map = undef_map;
331 else
333 table[idx].sym = sym;
334 table[idx].map = map;
336 if (map->l_type == lt_loaded)
337 /* Make sure we don't unload this object by
338 setting the appropriate flag. */
339 map->l_flags_1 |= DF_1_NODELETE;
342 return;
345 idx += hash2;
346 if (idx >= size)
347 idx -= size;
351 struct unique_sym_table *tab
352 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
354 __rtld_lock_lock_recursive (tab->lock);
356 struct unique_sym *entries = tab->entries;
357 size_t size = tab->size;
358 if (entries != NULL)
360 size_t idx = new_hash % size;
361 size_t hash2 = 1 + new_hash % (size - 2);
362 while (1)
364 if (entries[idx].hashval == new_hash
365 && strcmp (entries[idx].name, undef_name) == 0)
367 result->s = entries[idx].sym;
368 result->m = (struct link_map *) entries[idx].map;
369 __rtld_lock_unlock_recursive (tab->lock);
370 return 1;
373 if (entries[idx].name == NULL)
374 break;
376 idx += hash2;
377 if (idx >= size)
378 idx -= size;
381 if (size * 3 <= tab->n_elements * 4)
383 /* Expand the table. */
384 #ifdef RTLD_CHECK_FOREIGN_CALL
385 /* This must not happen during runtime relocations. */
386 assert (!RTLD_CHECK_FOREIGN_CALL);
387 #endif
388 size_t newsize = _dl_higher_prime_number (size + 1);
389 struct unique_sym *newentries
390 = calloc (sizeof (struct unique_sym), newsize);
391 if (newentries == NULL)
393 nomem:
394 __rtld_lock_unlock_recursive (tab->lock);
395 _dl_fatal_printf ("out of memory\n");
398 for (idx = 0; idx < size; ++idx)
399 if (entries[idx].name != NULL)
400 enter (newentries, newsize, entries[idx].hashval,
401 entries[idx].name, entries[idx].sym,
402 entries[idx].map);
404 tab->free (entries);
405 tab->size = newsize;
406 size = newsize;
407 entries = tab->entries = newentries;
408 tab->free = free;
411 else
413 #ifdef RTLD_CHECK_FOREIGN_CALL
414 /* This must not happen during runtime relocations. */
415 assert (!RTLD_CHECK_FOREIGN_CALL);
416 #endif
418 #ifdef SHARED
419 /* If tab->entries is NULL, but tab->size is not, it means
420 this is the second, conflict finding, lookup for
421 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
422 allocate anything and don't enter anything into the
423 hash table. */
424 if (__builtin_expect (tab->size, 0))
426 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
427 __rtld_lock_unlock_recursive (tab->lock);
428 goto success;
430 #endif
432 #define INITIAL_NUNIQUE_SYM_TABLE 31
433 size = INITIAL_NUNIQUE_SYM_TABLE;
434 entries = calloc (sizeof (struct unique_sym), size);
435 if (entries == NULL)
436 goto nomem;
438 tab->entries = entries;
439 tab->size = size;
440 tab->free = free;
443 enter (entries, size, new_hash, strtab + sym->st_name, sym,
444 (struct link_map *) map);
445 ++tab->n_elements;
447 __rtld_lock_unlock_recursive (tab->lock);
449 goto success;
451 default:
452 /* Local symbols are ignored. */
453 break;
457 /* If this current map is the one mentioned in the verneed entry
458 and we have not found a weak entry, it is a bug. */
459 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
460 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
461 return -1;
463 while (++i < n);
465 /* We have not found anything until now. */
466 return 0;
470 static uint_fast32_t
471 dl_new_hash (const char *s)
473 uint_fast32_t h = 5381;
474 for (unsigned char c = *s; c != '\0'; c = *++s)
475 h = h * 33 + c;
476 return h & 0xffffffff;
480 /* Add extra dependency on MAP to UNDEF_MAP. */
481 static int
482 internal_function
483 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
485 struct link_map *runp;
486 unsigned int i;
487 int result = 0;
489 /* Avoid self-references and references to objects which cannot be
490 unloaded anyway. */
491 if (undef_map == map)
492 return 0;
494 /* Avoid references to objects which cannot be unloaded anyway. */
495 assert (map->l_type == lt_loaded);
496 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
497 return 0;
499 struct link_map_reldeps *l_reldeps
500 = atomic_forced_read (undef_map->l_reldeps);
502 /* Make sure l_reldeps is read before l_initfini. */
503 atomic_read_barrier ();
505 /* Determine whether UNDEF_MAP already has a reference to MAP. First
506 look in the normal dependencies. */
507 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
508 if (l_initfini != NULL)
510 for (i = 0; l_initfini[i] != NULL; ++i)
511 if (l_initfini[i] == map)
512 return 0;
515 /* No normal dependency. See whether we already had to add it
516 to the special list of dynamic dependencies. */
517 unsigned int l_reldepsact = 0;
518 if (l_reldeps != NULL)
520 struct link_map **list = &l_reldeps->list[0];
521 l_reldepsact = l_reldeps->act;
522 for (i = 0; i < l_reldepsact; ++i)
523 if (list[i] == map)
524 return 0;
527 /* Save serial number of the target MAP. */
528 unsigned long long serial = map->l_serial;
530 /* Make sure nobody can unload the object while we are at it. */
531 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
533 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
534 here, that can result in ABBA deadlock. */
535 THREAD_GSCOPE_RESET_FLAG ();
536 __rtld_lock_lock_recursive (GL(dl_load_lock));
537 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
538 it can e.g. point to unallocated memory. So avoid the optimizer
539 treating the above read from MAP->l_serial as ensurance it
540 can safely dereference it. */
541 map = atomic_forced_read (map);
543 /* From this point on it is unsafe to dereference MAP, until it
544 has been found in one of the lists. */
546 /* Redo the l_initfini check in case undef_map's l_initfini
547 changed in the mean time. */
548 if (undef_map->l_initfini != l_initfini
549 && undef_map->l_initfini != NULL)
551 l_initfini = undef_map->l_initfini;
552 for (i = 0; l_initfini[i] != NULL; ++i)
553 if (l_initfini[i] == map)
554 goto out_check;
557 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
558 the mean time. */
559 if (undef_map->l_reldeps != NULL)
561 if (undef_map->l_reldeps != l_reldeps)
563 struct link_map **list = &undef_map->l_reldeps->list[0];
564 l_reldepsact = undef_map->l_reldeps->act;
565 for (i = 0; i < l_reldepsact; ++i)
566 if (list[i] == map)
567 goto out_check;
569 else if (undef_map->l_reldeps->act > l_reldepsact)
571 struct link_map **list
572 = &undef_map->l_reldeps->list[0];
573 i = l_reldepsact;
574 l_reldepsact = undef_map->l_reldeps->act;
575 for (; i < l_reldepsact; ++i)
576 if (list[i] == map)
577 goto out_check;
581 else
582 __rtld_lock_lock_recursive (GL(dl_load_lock));
584 /* The object is not yet in the dependency list. Before we add
585 it make sure just one more time the object we are about to
586 reference is still available. There is a brief period in
587 which the object could have been removed since we found the
588 definition. */
589 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
590 while (runp != NULL && runp != map)
591 runp = runp->l_next;
593 if (runp != NULL)
595 /* The object is still available. */
597 /* MAP could have been dlclosed, freed and then some other dlopened
598 library could have the same link_map pointer. */
599 if (map->l_serial != serial)
600 goto out_check;
602 /* Redo the NODELETE check, as when dl_load_lock wasn't held
603 yet this could have changed. */
604 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
605 goto out;
607 /* If the object with the undefined reference cannot be removed ever
608 just make sure the same is true for the object which contains the
609 definition. */
610 if (undef_map->l_type != lt_loaded
611 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
613 map->l_flags_1 |= DF_1_NODELETE;
614 goto out;
617 /* Add the reference now. */
618 if (__builtin_expect (l_reldepsact >= undef_map->l_reldepsmax, 0))
620 /* Allocate more memory for the dependency list. Since this
621 can never happen during the startup phase we can use
622 `realloc'. */
623 struct link_map_reldeps *newp;
624 unsigned int max
625 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
627 #ifdef RTLD_PREPARE_FOREIGN_CALL
628 RTLD_PREPARE_FOREIGN_CALL;
629 #endif
631 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
632 if (newp == NULL)
634 /* If we didn't manage to allocate memory for the list this is
635 no fatal problem. We simply make sure the referenced object
636 cannot be unloaded. This is semantically the correct
637 behavior. */
638 map->l_flags_1 |= DF_1_NODELETE;
639 goto out;
641 else
643 if (l_reldepsact)
644 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
645 l_reldepsact * sizeof (struct link_map *));
646 newp->list[l_reldepsact] = map;
647 newp->act = l_reldepsact + 1;
648 atomic_write_barrier ();
649 void *old = undef_map->l_reldeps;
650 undef_map->l_reldeps = newp;
651 undef_map->l_reldepsmax = max;
652 if (old)
653 _dl_scope_free (old);
656 else
658 undef_map->l_reldeps->list[l_reldepsact] = map;
659 atomic_write_barrier ();
660 undef_map->l_reldeps->act = l_reldepsact + 1;
663 /* Display information if we are debugging. */
664 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
665 _dl_debug_printf ("\
666 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
667 map->l_name[0] ? map->l_name : rtld_progname,
668 map->l_ns,
669 undef_map->l_name[0]
670 ? undef_map->l_name : rtld_progname,
671 undef_map->l_ns);
673 else
674 /* Whoa, that was bad luck. We have to search again. */
675 result = -1;
677 out:
678 /* Release the lock. */
679 __rtld_lock_unlock_recursive (GL(dl_load_lock));
681 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
682 THREAD_GSCOPE_SET_FLAG ();
684 return result;
686 out_check:
687 if (map->l_serial != serial)
688 result = -1;
689 goto out;
692 static void
693 internal_function
694 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
695 const ElfW(Sym) **ref, struct sym_val *value,
696 const struct r_found_version *version, int type_class,
697 int protected);
700 /* Search loaded objects' symbol tables for a definition of the symbol
701 UNDEF_NAME, perhaps with a requested version for the symbol.
703 We must never have calls to the audit functions inside this function
704 or in any function which gets called. If this would happen the audit
705 code might create a thread which can throw off all the scope locking. */
706 lookup_t
707 internal_function
708 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
709 const ElfW(Sym) **ref,
710 struct r_scope_elem *symbol_scope[],
711 const struct r_found_version *version,
712 int type_class, int flags, struct link_map *skip_map)
714 const uint_fast32_t new_hash = dl_new_hash (undef_name);
715 unsigned long int old_hash = 0xffffffff;
716 struct sym_val current_value = { NULL, NULL };
717 struct r_scope_elem **scope = symbol_scope;
719 bump_num_relocations ();
721 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
722 is allowed if we look up a versioned symbol. */
723 assert (version == NULL
724 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
725 == 0);
727 size_t i = 0;
728 if (__builtin_expect (skip_map != NULL, 0))
729 /* Search the relevant loaded objects for a definition. */
730 while ((*scope)->r_list[i] != skip_map)
731 ++i;
733 /* Search the relevant loaded objects for a definition. */
734 for (size_t start = i; *scope != NULL; start = 0, ++scope)
736 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
737 &current_value, *scope, start, version, flags,
738 skip_map, type_class, undef_map);
739 if (res > 0)
740 break;
742 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
744 /* Oh, oh. The file named in the relocation entry does not
745 contain the needed symbol. This code is never reached
746 for unversioned lookups. */
747 assert (version != NULL);
748 const char *reference_name = undef_map ? undef_map->l_name : NULL;
750 /* XXX We cannot translate the message. */
751 _dl_signal_cerror (0, (reference_name[0]
752 ? reference_name
753 : (rtld_progname ?: "<main program>")),
754 N_("relocation error"),
755 make_string ("symbol ", undef_name, ", version ",
756 version->name,
757 " not defined in file ",
758 version->filename,
759 " with link time reference",
760 res == -2
761 ? " (no version symbols)" : ""));
762 *ref = NULL;
763 return 0;
767 if (__builtin_expect (current_value.s == NULL, 0))
769 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
770 && skip_map == NULL)
772 /* We could find no value for a strong reference. */
773 const char *reference_name = undef_map ? undef_map->l_name : "";
774 const char *versionstr = version ? ", version " : "";
775 const char *versionname = (version && version->name
776 ? version->name : "");
778 /* XXX We cannot translate the message. */
779 _dl_signal_cerror (0, (reference_name[0]
780 ? reference_name
781 : (rtld_progname ?: "<main program>")),
782 N_("symbol lookup error"),
783 make_string (undefined_msg, undef_name,
784 versionstr, versionname));
786 *ref = NULL;
787 return 0;
790 int protected = (*ref
791 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
792 if (__builtin_expect (protected != 0, 0))
794 /* It is very tricky. We need to figure out what value to
795 return for the protected symbol. */
796 if (type_class == ELF_RTYPE_CLASS_PLT)
798 if (current_value.s != NULL && current_value.m != undef_map)
800 current_value.s = *ref;
801 current_value.m = undef_map;
804 else
806 struct sym_val protected_value = { NULL, NULL };
808 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
809 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
810 &protected_value, *scope, i, version, flags,
811 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
812 break;
814 if (protected_value.s != NULL && protected_value.m != undef_map)
816 current_value.s = *ref;
817 current_value.m = undef_map;
822 /* We have to check whether this would bind UNDEF_MAP to an object
823 in the global scope which was dynamically loaded. In this case
824 we have to prevent the latter from being unloaded unless the
825 UNDEF_MAP object is also unloaded. */
826 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
827 /* Don't do this for explicit lookups as opposed to implicit
828 runtime lookups. */
829 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
830 /* Add UNDEF_MAP to the dependencies. */
831 && add_dependency (undef_map, current_value.m, flags) < 0)
832 /* Something went wrong. Perhaps the object we tried to reference
833 was just removed. Try finding another definition. */
834 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
835 (flags & DL_LOOKUP_GSCOPE_LOCK)
836 ? undef_map->l_scope : symbol_scope,
837 version, type_class, flags, skip_map);
839 /* The object is used. */
840 if (__builtin_expect (current_value.m->l_used == 0, 0))
841 current_value.m->l_used = 1;
843 if (__builtin_expect (GLRO(dl_debug_mask)
844 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
845 _dl_debug_bindings (undef_name, undef_map, ref,
846 &current_value, version, type_class, protected);
848 *ref = current_value.s;
849 return LOOKUP_VALUE (current_value.m);
853 /* Cache the location of MAP's hash table. */
855 void
856 internal_function
857 _dl_setup_hash (struct link_map *map)
859 Elf_Symndx *hash;
860 Elf_Symndx nchain;
862 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
863 + DT_THISPROCNUM + DT_VERSIONTAGNUM
864 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
866 Elf32_Word *hash32
867 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
868 + DT_THISPROCNUM + DT_VERSIONTAGNUM
869 + DT_EXTRANUM + DT_VALNUM]);
870 map->l_nbuckets = *hash32++;
871 Elf32_Word symbias = *hash32++;
872 Elf32_Word bitmask_nwords = *hash32++;
873 /* Must be a power of two. */
874 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
875 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
876 map->l_gnu_shift = *hash32++;
878 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
879 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
881 map->l_gnu_buckets = hash32;
882 hash32 += map->l_nbuckets;
883 map->l_gnu_chain_zero = hash32 - symbias;
884 return;
887 if (!map->l_info[DT_HASH])
888 return;
889 hash = (void *) D_PTR (map, l_info[DT_HASH]);
891 map->l_nbuckets = *hash++;
892 nchain = *hash++;
893 map->l_buckets = hash;
894 hash += map->l_nbuckets;
895 map->l_chain = hash;
899 static void
900 internal_function
901 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
902 const ElfW(Sym) **ref, struct sym_val *value,
903 const struct r_found_version *version, int type_class,
904 int protected)
906 const char *reference_name = undef_map->l_name;
908 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
910 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
911 (reference_name[0]
912 ? reference_name
913 : (rtld_progname ?: "<main program>")),
914 undef_map->l_ns,
915 value->m->l_name[0] ? value->m->l_name : rtld_progname,
916 value->m->l_ns,
917 protected ? "protected" : "normal", undef_name);
918 if (version)
919 _dl_debug_printf_c (" [%s]\n", version->name);
920 else
921 _dl_debug_printf_c ("\n");
923 #ifdef SHARED
924 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
926 int conflict = 0;
927 struct sym_val val = { NULL, NULL };
929 if ((GLRO(dl_trace_prelink_map) == NULL
930 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
931 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
933 const uint_fast32_t new_hash = dl_new_hash (undef_name);
934 unsigned long int old_hash = 0xffffffff;
935 struct unique_sym *saved_entries
936 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
938 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
939 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
940 undef_map->l_local_scope[0], 0, version, 0, NULL,
941 type_class, undef_map);
942 if (val.s != value->s || val.m != value->m)
943 conflict = 1;
944 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
945 && val.s
946 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
947 STB_GLOBAL) == STB_GNU_UNIQUE)
949 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
950 contains any DT_SYMBOLIC libraries, unfortunately there
951 can be conflicts even if the above is equal. As symbol
952 resolution goes from the last library to the first and
953 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
954 library, it would be the one that is looked up. */
955 struct sym_val val2 = { NULL, NULL };
956 size_t n;
957 struct r_scope_elem *scope = undef_map->l_local_scope[0];
959 for (n = 0; n < scope->r_nlist; n++)
960 if (scope->r_list[n] == val.m)
961 break;
963 for (n++; n < scope->r_nlist; n++)
964 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
965 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
966 &val2,
967 &scope->r_list[n]->l_symbolic_searchlist,
968 0, version, 0, NULL, type_class,
969 undef_map) > 0)
971 conflict = 1;
972 val = val2;
973 break;
976 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
979 if (value->s)
981 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
982 == STT_TLS, 0))
983 type_class = 4;
984 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
985 == STT_GNU_IFUNC, 0))
986 type_class |= 8;
989 if (conflict
990 || GLRO(dl_trace_prelink_map) == undef_map
991 || GLRO(dl_trace_prelink_map) == NULL
992 || type_class >= 4)
994 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
995 conflict ? "conflict" : "lookup",
996 (int) sizeof (ElfW(Addr)) * 2,
997 (size_t) undef_map->l_map_start,
998 (int) sizeof (ElfW(Addr)) * 2,
999 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1000 (int) sizeof (ElfW(Addr)) * 2,
1001 (size_t) (value->s ? value->m->l_map_start : 0),
1002 (int) sizeof (ElfW(Addr)) * 2,
1003 (size_t) (value->s ? value->s->st_value : 0));
1005 if (conflict)
1006 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1007 (int) sizeof (ElfW(Addr)) * 2,
1008 (size_t) (val.s ? val.m->l_map_start : 0),
1009 (int) sizeof (ElfW(Addr)) * 2,
1010 (size_t) (val.s ? val.s->st_value : 0));
1012 _dl_printf ("/%x %s\n", type_class, undef_name);
1015 #endif