Hurd: Fix port deallocation on mknod error.
[glibc.git] / elf / dl-lookup.c
bloba2a699b48f5f188da2528ed163b7befffed586ee
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, 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>
31 #include <assert.h>
33 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
35 /* We need this string more than once. */
36 static const char undefined_msg[] = "undefined symbol: ";
39 struct sym_val
41 const ElfW(Sym) *s;
42 struct link_map *m;
46 #define make_string(string, rest...) \
47 ({ \
48 const char *all[] = { string, ## rest }; \
49 size_t len, cnt; \
50 char *result, *cp; \
52 len = 1; \
53 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
54 len += strlen (all[cnt]); \
56 cp = result = alloca (len); \
57 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
58 cp = __stpcpy (cp, all[cnt]); \
60 result; \
63 /* Statistics function. */
64 #ifdef SHARED
65 # define bump_num_relocations() ++GL(dl_num_relocations)
66 #else
67 # define bump_num_relocations() ((void) 0)
68 #endif
71 /* Inner part of the lookup functions. We return a value > 0 if we
72 found the symbol, the value 0 if nothing is found and < 0 if
73 something bad happened. */
74 static int
75 __attribute_noinline__
76 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
77 unsigned long int *old_hash, const ElfW(Sym) *ref,
78 struct sym_val *result, struct r_scope_elem *scope, size_t i,
79 const struct r_found_version *const version, int flags,
80 struct link_map *skip, int type_class, struct link_map *undef_map)
82 size_t n = scope->r_nlist;
83 /* Make sure we read the value before proceeding. Otherwise we
84 might use r_list pointing to the initial scope and r_nlist being
85 the value after a resize. That is the only path in dl-open.c not
86 protected by GSCOPE. A read barrier here might be to expensive. */
87 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
88 struct link_map **list = scope->r_list;
92 /* These variables are used in the nested function. */
93 Elf_Symndx symidx;
94 int num_versions = 0;
95 const ElfW(Sym) *versioned_sym = NULL;
97 const struct link_map *map = list[i]->l_real;
99 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
100 if (map == skip)
101 continue;
103 /* Don't search the executable when resolving a copy reloc. */
104 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
105 continue;
107 /* Do not look into objects which are going to be removed. */
108 if (map->l_removed)
109 continue;
111 /* Print some debugging info if wanted. */
112 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
113 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
114 undef_name,
115 map->l_name[0] ? map->l_name : rtld_progname,
116 map->l_ns);
118 /* If the hash table is empty there is nothing to do here. */
119 if (map->l_nbuckets == 0)
120 continue;
122 /* The tables for this map. */
123 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
124 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
127 /* Nested routine to check whether the symbol matches. */
128 const ElfW(Sym) *
129 __attribute_noinline__
130 check_match (const ElfW(Sym) *sym)
132 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
133 assert (ELF_RTYPE_CLASS_PLT == 1);
134 if (__builtin_expect ((sym->st_value == 0 /* No value. */
135 && stt != STT_TLS)
136 || (type_class & (sym->st_shndx == SHN_UNDEF)),
138 return NULL;
140 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
141 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
142 code/data definitions. */
143 #define ALLOWED_STT \
144 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
145 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
146 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
147 return NULL;
149 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
150 /* Not the symbol we are looking for. */
151 return NULL;
153 const ElfW(Half) *verstab = map->l_versyms;
154 if (version != NULL)
156 if (__builtin_expect (verstab == NULL, 0))
158 /* We need a versioned symbol but haven't found any. If
159 this is the object which is referenced in the verneed
160 entry it is a bug in the library since a symbol must
161 not simply disappear.
163 It would also be a bug in the object since it means that
164 the list of required versions is incomplete and so the
165 tests in dl-version.c haven't found a problem.*/
166 assert (version->filename == NULL
167 || ! _dl_name_match_p (version->filename, map));
169 /* Otherwise we accept the symbol. */
171 else
173 /* We can match the version information or use the
174 default one if it is not hidden. */
175 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
176 if ((map->l_versions[ndx].hash != version->hash
177 || strcmp (map->l_versions[ndx].name, version->name))
178 && (version->hidden || map->l_versions[ndx].hash
179 || (verstab[symidx] & 0x8000)))
180 /* It's not the version we want. */
181 return NULL;
184 else
186 /* No specific version is selected. There are two ways we
187 can got here:
189 - a binary which does not include versioning information
190 is loaded
192 - dlsym() instead of dlvsym() is used to get a symbol which
193 might exist in more than one form
195 If the library does not provide symbol version information
196 there is no problem at all: we simply use the symbol if it
197 is defined.
199 These two lookups need to be handled differently if the
200 library defines versions. In the case of the old
201 unversioned application the oldest (default) version
202 should be used. In case of a dlsym() call the latest and
203 public interface should be returned. */
204 if (verstab != NULL)
206 if ((verstab[symidx] & 0x7fff)
207 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
209 /* Don't accept hidden symbols. */
210 if ((verstab[symidx] & 0x8000) == 0
211 && num_versions++ == 0)
212 /* No version so far. */
213 versioned_sym = sym;
215 return NULL;
220 /* There cannot be another entry for this symbol so stop here. */
221 return sym;
224 const ElfW(Sym) *sym;
225 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
226 if (__builtin_expect (bitmask != NULL, 1))
228 ElfW(Addr) bitmask_word
229 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
230 & map->l_gnu_bitmask_idxbits];
232 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
233 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
234 & (__ELF_NATIVE_CLASS - 1));
236 if (__builtin_expect ((bitmask_word >> hashbit1)
237 & (bitmask_word >> hashbit2) & 1, 0))
239 Elf32_Word bucket = map->l_gnu_buckets[new_hash
240 % map->l_nbuckets];
241 if (bucket != 0)
243 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
246 if (((*hasharr ^ new_hash) >> 1) == 0)
248 symidx = hasharr - map->l_gnu_chain_zero;
249 sym = check_match (&symtab[symidx]);
250 if (sym != NULL)
251 goto found_it;
253 while ((*hasharr++ & 1u) == 0);
256 /* No symbol found. */
257 symidx = SHN_UNDEF;
259 else
261 if (*old_hash == 0xffffffff)
262 *old_hash = _dl_elf_hash (undef_name);
264 /* Use the old SysV-style hash table. Search the appropriate
265 hash bucket in this object's symbol table for a definition
266 for the same symbol name. */
267 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
268 symidx != STN_UNDEF;
269 symidx = map->l_chain[symidx])
271 sym = check_match (&symtab[symidx]);
272 if (sym != NULL)
273 goto found_it;
277 /* If we have seen exactly one versioned symbol while we are
278 looking for an unversioned symbol and the version is not the
279 default version we still accept this symbol since there are
280 no possible ambiguities. */
281 sym = num_versions == 1 ? versioned_sym : NULL;
283 if (sym != NULL)
285 found_it:
286 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
288 case STB_WEAK:
289 /* Weak definition. Use this value if we don't find another. */
290 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
292 if (! result->s)
294 result->s = sym;
295 result->m = (struct link_map *) map;
297 break;
299 /* FALLTHROUGH */
300 case STB_GLOBAL:
301 success:
302 /* Global definition. Just what we need. */
303 result->s = sym;
304 result->m = (struct link_map *) map;
305 return 1;
307 case STB_GNU_UNIQUE:;
308 /* We have to determine whether we already found a
309 symbol with this name before. If not then we have to
310 add it to the search table. If we already found a
311 definition we have to use it. */
312 void enter (struct unique_sym *table, size_t size,
313 unsigned int hash, const char *name,
314 const ElfW(Sym) *sym, const struct link_map *map)
316 size_t idx = hash % size;
317 size_t hash2 = 1 + hash % (size - 2);
318 while (table[idx].name != NULL)
320 idx += hash2;
321 if (idx >= size)
322 idx -= size;
325 table[idx].hashval = hash;
326 table[idx].name = name;
327 table[idx].sym = sym;
328 table[idx].map = map;
331 struct unique_sym_table *tab
332 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
334 __rtld_lock_lock_recursive (tab->lock);
336 struct unique_sym *entries = tab->entries;
337 size_t size = tab->size;
338 if (entries != NULL)
340 size_t idx = new_hash % size;
341 size_t hash2 = 1 + new_hash % (size - 2);
342 while (1)
344 if (entries[idx].hashval == new_hash
345 && strcmp (entries[idx].name, undef_name) == 0)
347 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
349 /* We possibly have to initialize the central
350 copy from the copy addressed through the
351 relocation. */
352 result->s = sym;
353 result->m = (struct link_map *) map;
355 else
357 result->s = entries[idx].sym;
358 result->m = (struct link_map *) entries[idx].map;
360 __rtld_lock_unlock_recursive (tab->lock);
361 return 1;
364 if (entries[idx].name == NULL)
365 break;
367 idx += hash2;
368 if (idx >= size)
369 idx -= size;
372 if (size * 3 <= tab->n_elements * 4)
374 /* Expand the table. */
375 #ifdef RTLD_CHECK_FOREIGN_CALL
376 /* This must not happen during runtime relocations. */
377 assert (!RTLD_CHECK_FOREIGN_CALL);
378 #endif
379 size_t newsize = _dl_higher_prime_number (size + 1);
380 struct unique_sym *newentries
381 = calloc (sizeof (struct unique_sym), newsize);
382 if (newentries == NULL)
384 nomem:
385 __rtld_lock_unlock_recursive (tab->lock);
386 _dl_fatal_printf ("out of memory\n");
389 for (idx = 0; idx < size; ++idx)
390 if (entries[idx].name != NULL)
391 enter (newentries, newsize, entries[idx].hashval,
392 entries[idx].name, entries[idx].sym,
393 entries[idx].map);
395 tab->free (entries);
396 tab->size = newsize;
397 size = newsize;
398 entries = tab->entries = newentries;
399 tab->free = free;
402 else
404 #ifdef RTLD_CHECK_FOREIGN_CALL
405 /* This must not happen during runtime relocations. */
406 assert (!RTLD_CHECK_FOREIGN_CALL);
407 #endif
409 #ifdef SHARED
410 /* If tab->entries is NULL, but tab->size is not, it means
411 this is the second, conflict finding, lookup for
412 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
413 allocate anything and don't enter anything into the
414 hash table. */
415 if (__builtin_expect (tab->size, 0))
417 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
418 __rtld_lock_unlock_recursive (tab->lock);
419 goto success;
421 #endif
423 #define INITIAL_NUNIQUE_SYM_TABLE 31
424 size = INITIAL_NUNIQUE_SYM_TABLE;
425 entries = calloc (sizeof (struct unique_sym), size);
426 if (entries == NULL)
427 goto nomem;
429 tab->entries = entries;
430 tab->size = size;
431 tab->free = free;
434 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
435 enter (entries, size, new_hash, strtab + sym->st_name, ref,
436 undef_map);
437 else
439 enter (entries, size, new_hash, strtab + sym->st_name, sym,
440 map);
442 if (map->l_type == lt_loaded)
443 /* Make sure we don't unload this object by
444 setting the appropriate flag. */
445 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
447 ++tab->n_elements;
449 __rtld_lock_unlock_recursive (tab->lock);
451 goto success;
453 default:
454 /* Local symbols are ignored. */
455 break;
459 /* If this current map is the one mentioned in the verneed entry
460 and we have not found a weak entry, it is a bug. */
461 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
462 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
463 return -1;
465 while (++i < n);
467 /* We have not found anything until now. */
468 return 0;
472 static uint_fast32_t
473 dl_new_hash (const char *s)
475 uint_fast32_t h = 5381;
476 for (unsigned char c = *s; c != '\0'; c = *++s)
477 h = h * 33 + c;
478 return h & 0xffffffff;
482 /* Add extra dependency on MAP to UNDEF_MAP. */
483 static int
484 internal_function
485 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
487 struct link_map *runp;
488 unsigned int i;
489 int result = 0;
491 /* Avoid self-references and references to objects which cannot be
492 unloaded anyway. */
493 if (undef_map == map)
494 return 0;
496 /* Avoid references to objects which cannot be unloaded anyway. */
497 assert (map->l_type == lt_loaded);
498 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
499 return 0;
501 struct link_map_reldeps *l_reldeps
502 = atomic_forced_read (undef_map->l_reldeps);
504 /* Make sure l_reldeps is read before l_initfini. */
505 atomic_read_barrier ();
507 /* Determine whether UNDEF_MAP already has a reference to MAP. First
508 look in the normal dependencies. */
509 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
510 if (l_initfini != NULL)
512 for (i = 0; l_initfini[i] != NULL; ++i)
513 if (l_initfini[i] == map)
514 return 0;
517 /* No normal dependency. See whether we already had to add it
518 to the special list of dynamic dependencies. */
519 unsigned int l_reldepsact = 0;
520 if (l_reldeps != NULL)
522 struct link_map **list = &l_reldeps->list[0];
523 l_reldepsact = l_reldeps->act;
524 for (i = 0; i < l_reldepsact; ++i)
525 if (list[i] == map)
526 return 0;
529 /* Save serial number of the target MAP. */
530 unsigned long long serial = map->l_serial;
532 /* Make sure nobody can unload the object while we are at it. */
533 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
535 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
536 here, that can result in ABBA deadlock. */
537 THREAD_GSCOPE_RESET_FLAG ();
538 __rtld_lock_lock_recursive (GL(dl_load_lock));
539 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
540 it can e.g. point to unallocated memory. So avoid the optimizer
541 treating the above read from MAP->l_serial as ensurance it
542 can safely dereference it. */
543 map = atomic_forced_read (map);
545 /* From this point on it is unsafe to dereference MAP, until it
546 has been found in one of the lists. */
548 /* Redo the l_initfini check in case undef_map's l_initfini
549 changed in the mean time. */
550 if (undef_map->l_initfini != l_initfini
551 && undef_map->l_initfini != NULL)
553 l_initfini = undef_map->l_initfini;
554 for (i = 0; l_initfini[i] != NULL; ++i)
555 if (l_initfini[i] == map)
556 goto out_check;
559 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
560 the mean time. */
561 if (undef_map->l_reldeps != NULL)
563 if (undef_map->l_reldeps != l_reldeps)
565 struct link_map **list = &undef_map->l_reldeps->list[0];
566 l_reldepsact = undef_map->l_reldeps->act;
567 for (i = 0; i < l_reldepsact; ++i)
568 if (list[i] == map)
569 goto out_check;
571 else if (undef_map->l_reldeps->act > l_reldepsact)
573 struct link_map **list
574 = &undef_map->l_reldeps->list[0];
575 i = l_reldepsact;
576 l_reldepsact = undef_map->l_reldeps->act;
577 for (; i < l_reldepsact; ++i)
578 if (list[i] == map)
579 goto out_check;
583 else
584 __rtld_lock_lock_recursive (GL(dl_load_lock));
586 /* The object is not yet in the dependency list. Before we add
587 it make sure just one more time the object we are about to
588 reference is still available. There is a brief period in
589 which the object could have been removed since we found the
590 definition. */
591 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
592 while (runp != NULL && runp != map)
593 runp = runp->l_next;
595 if (runp != NULL)
597 /* The object is still available. */
599 /* MAP could have been dlclosed, freed and then some other dlopened
600 library could have the same link_map pointer. */
601 if (map->l_serial != serial)
602 goto out_check;
604 /* Redo the NODELETE check, as when dl_load_lock wasn't held
605 yet this could have changed. */
606 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
607 goto out;
609 /* If the object with the undefined reference cannot be removed ever
610 just make sure the same is true for the object which contains the
611 definition. */
612 if (undef_map->l_type != lt_loaded
613 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
615 map->l_flags_1 |= DF_1_NODELETE;
616 goto out;
619 /* Add the reference now. */
620 if (__builtin_expect (l_reldepsact >= undef_map->l_reldepsmax, 0))
622 /* Allocate more memory for the dependency list. Since this
623 can never happen during the startup phase we can use
624 `realloc'. */
625 struct link_map_reldeps *newp;
626 unsigned int max
627 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
629 #ifdef RTLD_PREPARE_FOREIGN_CALL
630 RTLD_PREPARE_FOREIGN_CALL;
631 #endif
633 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
634 if (newp == NULL)
636 /* If we didn't manage to allocate memory for the list this is
637 no fatal problem. We simply make sure the referenced object
638 cannot be unloaded. This is semantically the correct
639 behavior. */
640 map->l_flags_1 |= DF_1_NODELETE;
641 goto out;
643 else
645 if (l_reldepsact)
646 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
647 l_reldepsact * sizeof (struct link_map *));
648 newp->list[l_reldepsact] = map;
649 newp->act = l_reldepsact + 1;
650 atomic_write_barrier ();
651 void *old = undef_map->l_reldeps;
652 undef_map->l_reldeps = newp;
653 undef_map->l_reldepsmax = max;
654 if (old)
655 _dl_scope_free (old);
658 else
660 undef_map->l_reldeps->list[l_reldepsact] = map;
661 atomic_write_barrier ();
662 undef_map->l_reldeps->act = l_reldepsact + 1;
665 /* Display information if we are debugging. */
666 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
667 _dl_debug_printf ("\
668 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
669 map->l_name[0] ? map->l_name : rtld_progname,
670 map->l_ns,
671 undef_map->l_name[0]
672 ? undef_map->l_name : rtld_progname,
673 undef_map->l_ns);
675 else
676 /* Whoa, that was bad luck. We have to search again. */
677 result = -1;
679 out:
680 /* Release the lock. */
681 __rtld_lock_unlock_recursive (GL(dl_load_lock));
683 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
684 THREAD_GSCOPE_SET_FLAG ();
686 return result;
688 out_check:
689 if (map->l_serial != serial)
690 result = -1;
691 goto out;
694 static void
695 internal_function
696 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
697 const ElfW(Sym) **ref, struct sym_val *value,
698 const struct r_found_version *version, int type_class,
699 int protected);
702 /* Search loaded objects' symbol tables for a definition of the symbol
703 UNDEF_NAME, perhaps with a requested version for the symbol.
705 We must never have calls to the audit functions inside this function
706 or in any function which gets called. If this would happen the audit
707 code might create a thread which can throw off all the scope locking. */
708 lookup_t
709 internal_function
710 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
711 const ElfW(Sym) **ref,
712 struct r_scope_elem *symbol_scope[],
713 const struct r_found_version *version,
714 int type_class, int flags, struct link_map *skip_map)
716 const uint_fast32_t new_hash = dl_new_hash (undef_name);
717 unsigned long int old_hash = 0xffffffff;
718 struct sym_val current_value = { NULL, NULL };
719 struct r_scope_elem **scope = symbol_scope;
721 bump_num_relocations ();
723 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
724 is allowed if we look up a versioned symbol. */
725 assert (version == NULL
726 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
727 == 0);
729 size_t i = 0;
730 if (__builtin_expect (skip_map != NULL, 0))
731 /* Search the relevant loaded objects for a definition. */
732 while ((*scope)->r_list[i] != skip_map)
733 ++i;
735 /* Search the relevant loaded objects for a definition. */
736 for (size_t start = i; *scope != NULL; start = 0, ++scope)
738 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
739 &current_value, *scope, start, version, flags,
740 skip_map, type_class, undef_map);
741 if (res > 0)
742 break;
744 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
746 /* Oh, oh. The file named in the relocation entry does not
747 contain the needed symbol. This code is never reached
748 for unversioned lookups. */
749 assert (version != NULL);
750 const char *reference_name = undef_map ? undef_map->l_name : NULL;
752 /* XXX We cannot translate the message. */
753 _dl_signal_cerror (0, (reference_name[0]
754 ? reference_name
755 : (rtld_progname ?: "<main program>")),
756 N_("relocation error"),
757 make_string ("symbol ", undef_name, ", version ",
758 version->name,
759 " not defined in file ",
760 version->filename,
761 " with link time reference",
762 res == -2
763 ? " (no version symbols)" : ""));
764 *ref = NULL;
765 return 0;
769 if (__builtin_expect (current_value.s == NULL, 0))
771 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
772 && skip_map == NULL
773 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
775 /* We could find no value for a strong reference. */
776 const char *reference_name = undef_map ? undef_map->l_name : "";
777 const char *versionstr = version ? ", version " : "";
778 const char *versionname = (version && version->name
779 ? version->name : "");
781 /* XXX We cannot translate the message. */
782 _dl_signal_cerror (0, (reference_name[0]
783 ? reference_name
784 : (rtld_progname ?: "<main program>")),
785 N_("symbol lookup error"),
786 make_string (undefined_msg, undef_name,
787 versionstr, versionname));
789 *ref = NULL;
790 return 0;
793 int protected = (*ref
794 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
795 if (__builtin_expect (protected != 0, 0))
797 /* It is very tricky. We need to figure out what value to
798 return for the protected symbol. */
799 if (type_class == ELF_RTYPE_CLASS_PLT)
801 if (current_value.s != NULL && current_value.m != undef_map)
803 current_value.s = *ref;
804 current_value.m = undef_map;
807 else
809 struct sym_val protected_value = { NULL, NULL };
811 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
812 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
813 &protected_value, *scope, i, version, flags,
814 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
815 break;
817 if (protected_value.s != NULL && protected_value.m != undef_map)
819 current_value.s = *ref;
820 current_value.m = undef_map;
825 /* We have to check whether this would bind UNDEF_MAP to an object
826 in the global scope which was dynamically loaded. In this case
827 we have to prevent the latter from being unloaded unless the
828 UNDEF_MAP object is also unloaded. */
829 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
830 /* Don't do this for explicit lookups as opposed to implicit
831 runtime lookups. */
832 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
833 /* Add UNDEF_MAP to the dependencies. */
834 && add_dependency (undef_map, current_value.m, flags) < 0)
835 /* Something went wrong. Perhaps the object we tried to reference
836 was just removed. Try finding another definition. */
837 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
838 (flags & DL_LOOKUP_GSCOPE_LOCK)
839 ? undef_map->l_scope : symbol_scope,
840 version, type_class, flags, skip_map);
842 /* The object is used. */
843 if (__builtin_expect (current_value.m->l_used == 0, 0))
844 current_value.m->l_used = 1;
846 if (__builtin_expect (GLRO(dl_debug_mask)
847 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
848 _dl_debug_bindings (undef_name, undef_map, ref,
849 &current_value, version, type_class, protected);
851 *ref = current_value.s;
852 return LOOKUP_VALUE (current_value.m);
856 /* Cache the location of MAP's hash table. */
858 void
859 internal_function
860 _dl_setup_hash (struct link_map *map)
862 Elf_Symndx *hash;
864 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
865 + DT_THISPROCNUM + DT_VERSIONTAGNUM
866 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
868 Elf32_Word *hash32
869 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
870 + DT_THISPROCNUM + DT_VERSIONTAGNUM
871 + DT_EXTRANUM + DT_VALNUM]);
872 map->l_nbuckets = *hash32++;
873 Elf32_Word symbias = *hash32++;
874 Elf32_Word bitmask_nwords = *hash32++;
875 /* Must be a power of two. */
876 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
877 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
878 map->l_gnu_shift = *hash32++;
880 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
881 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
883 map->l_gnu_buckets = hash32;
884 hash32 += map->l_nbuckets;
885 map->l_gnu_chain_zero = hash32 - symbias;
886 return;
889 if (!map->l_info[DT_HASH])
890 return;
891 hash = (void *) D_PTR (map, l_info[DT_HASH]);
893 map->l_nbuckets = *hash++;
894 /* Skip nchain. */
895 hash++;
896 map->l_buckets = hash;
897 hash += map->l_nbuckets;
898 map->l_chain = hash;
902 static void
903 internal_function
904 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
905 const ElfW(Sym) **ref, struct sym_val *value,
906 const struct r_found_version *version, int type_class,
907 int protected)
909 const char *reference_name = undef_map->l_name;
911 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
913 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
914 (reference_name[0]
915 ? reference_name
916 : (rtld_progname ?: "<main program>")),
917 undef_map->l_ns,
918 value->m->l_name[0] ? value->m->l_name : rtld_progname,
919 value->m->l_ns,
920 protected ? "protected" : "normal", undef_name);
921 if (version)
922 _dl_debug_printf_c (" [%s]\n", version->name);
923 else
924 _dl_debug_printf_c ("\n");
926 #ifdef SHARED
927 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
929 int conflict = 0;
930 struct sym_val val = { NULL, NULL };
932 if ((GLRO(dl_trace_prelink_map) == NULL
933 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
934 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
936 const uint_fast32_t new_hash = dl_new_hash (undef_name);
937 unsigned long int old_hash = 0xffffffff;
938 struct unique_sym *saved_entries
939 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
941 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
942 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
943 undef_map->l_local_scope[0], 0, version, 0, NULL,
944 type_class, undef_map);
945 if (val.s != value->s || val.m != value->m)
946 conflict = 1;
947 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
948 && val.s
949 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
950 STB_GLOBAL) == STB_GNU_UNIQUE)
952 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
953 contains any DT_SYMBOLIC libraries, unfortunately there
954 can be conflicts even if the above is equal. As symbol
955 resolution goes from the last library to the first and
956 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
957 library, it would be the one that is looked up. */
958 struct sym_val val2 = { NULL, NULL };
959 size_t n;
960 struct r_scope_elem *scope = undef_map->l_local_scope[0];
962 for (n = 0; n < scope->r_nlist; n++)
963 if (scope->r_list[n] == val.m)
964 break;
966 for (n++; n < scope->r_nlist; n++)
967 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
968 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
969 &val2,
970 &scope->r_list[n]->l_symbolic_searchlist,
971 0, version, 0, NULL, type_class,
972 undef_map) > 0)
974 conflict = 1;
975 val = val2;
976 break;
979 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
982 if (value->s)
984 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
985 == STT_TLS, 0))
986 type_class = 4;
987 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
988 == STT_GNU_IFUNC, 0))
989 type_class |= 8;
992 if (conflict
993 || GLRO(dl_trace_prelink_map) == undef_map
994 || GLRO(dl_trace_prelink_map) == NULL
995 || type_class >= 4)
997 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
998 conflict ? "conflict" : "lookup",
999 (int) sizeof (ElfW(Addr)) * 2,
1000 (size_t) undef_map->l_map_start,
1001 (int) sizeof (ElfW(Addr)) * 2,
1002 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1003 (int) sizeof (ElfW(Addr)) * 2,
1004 (size_t) (value->s ? value->m->l_map_start : 0),
1005 (int) sizeof (ElfW(Addr)) * 2,
1006 (size_t) (value->s ? value->s->st_value : 0));
1008 if (conflict)
1009 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1010 (int) sizeof (ElfW(Addr)) * 2,
1011 (size_t) (val.s ? val.m->l_map_start : 0),
1012 (int) sizeof (ElfW(Addr)) * 2,
1013 (size_t) (val.s ? val.s->st_value : 0));
1015 _dl_printf ("/%x %s\n", type_class, undef_name);
1018 #endif