Declare ifunc resolver to return a pointer to the same type as the target
[glibc.git] / elf / dl-lookup.c
blob645dc3ebb4260bfada41062ecaa877243350a7b2
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2017 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 <libc-lock.h>
29 #include <tls.h>
30 #include <atomic.h>
32 #include <assert.h>
34 /* Return nonzero if check_match should consider SYM to fail to match a
35 symbol reference for some machine-specific reason. */
36 #ifndef ELF_MACHINE_SYM_NO_MATCH
37 # define ELF_MACHINE_SYM_NO_MATCH(sym) 0
38 #endif
40 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
43 struct sym_val
45 const ElfW(Sym) *s;
46 struct link_map *m;
50 /* Statistics function. */
51 #ifdef SHARED
52 # define bump_num_relocations() ++GL(dl_num_relocations)
53 #else
54 # define bump_num_relocations() ((void) 0)
55 #endif
57 /* Utility function for do_lookup_x. The caller is called with undef_name,
58 ref, version, flags and type_class, and those are passed as the first
59 five arguments. The caller then computes sym, symidx, strtab, and map
60 and passes them as the next four arguments. Lastly the caller passes in
61 versioned_sym and num_versions which are modified by check_match during
62 the checking process. */
63 static const ElfW(Sym) *
64 check_match (const char *const undef_name,
65 const ElfW(Sym) *const ref,
66 const struct r_found_version *const version,
67 const int flags,
68 const int type_class,
69 const ElfW(Sym) *const sym,
70 const Elf_Symndx symidx,
71 const char *const strtab,
72 const struct link_map *const map,
73 const ElfW(Sym) **const versioned_sym,
74 int *const num_versions)
76 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
77 assert (ELF_RTYPE_CLASS_PLT == 1);
78 if (__glibc_unlikely ((sym->st_value == 0 /* No value. */
79 && stt != STT_TLS)
80 || ELF_MACHINE_SYM_NO_MATCH (sym)
81 || (type_class & (sym->st_shndx == SHN_UNDEF))))
82 return NULL;
84 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
85 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
86 code/data definitions. */
87 #define ALLOWED_STT \
88 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
89 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
90 if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
91 return NULL;
93 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
94 /* Not the symbol we are looking for. */
95 return NULL;
97 const ElfW(Half) *verstab = map->l_versyms;
98 if (version != NULL)
100 if (__glibc_unlikely (verstab == NULL))
102 /* We need a versioned symbol but haven't found any. If
103 this is the object which is referenced in the verneed
104 entry it is a bug in the library since a symbol must
105 not simply disappear.
107 It would also be a bug in the object since it means that
108 the list of required versions is incomplete and so the
109 tests in dl-version.c haven't found a problem.*/
110 assert (version->filename == NULL
111 || ! _dl_name_match_p (version->filename, map));
113 /* Otherwise we accept the symbol. */
115 else
117 /* We can match the version information or use the
118 default one if it is not hidden. */
119 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
120 if ((map->l_versions[ndx].hash != version->hash
121 || strcmp (map->l_versions[ndx].name, version->name))
122 && (version->hidden || map->l_versions[ndx].hash
123 || (verstab[symidx] & 0x8000)))
124 /* It's not the version we want. */
125 return NULL;
128 else
130 /* No specific version is selected. There are two ways we
131 can got here:
133 - a binary which does not include versioning information
134 is loaded
136 - dlsym() instead of dlvsym() is used to get a symbol which
137 might exist in more than one form
139 If the library does not provide symbol version information
140 there is no problem at all: we simply use the symbol if it
141 is defined.
143 These two lookups need to be handled differently if the
144 library defines versions. In the case of the old
145 unversioned application the oldest (default) version
146 should be used. In case of a dlsym() call the latest and
147 public interface should be returned. */
148 if (verstab != NULL)
150 if ((verstab[symidx] & 0x7fff)
151 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
153 /* Don't accept hidden symbols. */
154 if ((verstab[symidx] & 0x8000) == 0
155 && (*num_versions)++ == 0)
156 /* No version so far. */
157 *versioned_sym = sym;
159 return NULL;
164 /* There cannot be another entry for this symbol so stop here. */
165 return sym;
168 /* Utility function for do_lookup_unique. Add a symbol to TABLE. */
169 static void
170 enter_unique_sym (struct unique_sym *table, size_t size,
171 unsigned int hash, const char *name,
172 const ElfW(Sym) *sym, const struct link_map *map)
174 size_t idx = hash % size;
175 size_t hash2 = 1 + hash % (size - 2);
176 while (table[idx].name != NULL)
178 idx += hash2;
179 if (idx >= size)
180 idx -= size;
183 table[idx].hashval = hash;
184 table[idx].name = name;
185 table[idx].sym = sym;
186 table[idx].map = map;
189 /* Utility function for do_lookup_x. Lookup an STB_GNU_UNIQUE symbol
190 in the unique symbol table, creating a new entry if necessary.
191 Return the matching symbol in RESULT. */
192 static void
193 do_lookup_unique (const char *undef_name, uint_fast32_t new_hash,
194 const struct link_map *map, struct sym_val *result,
195 int type_class, const ElfW(Sym) *sym, const char *strtab,
196 const ElfW(Sym) *ref, const struct link_map *undef_map)
198 /* We have to determine whether we already found a symbol with this
199 name before. If not then we have to add it to the search table.
200 If we already found a definition we have to use it. */
202 struct unique_sym_table *tab
203 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
205 __rtld_lock_lock_recursive (tab->lock);
207 struct unique_sym *entries = tab->entries;
208 size_t size = tab->size;
209 if (entries != NULL)
211 size_t idx = new_hash % size;
212 size_t hash2 = 1 + new_hash % (size - 2);
213 while (1)
215 if (entries[idx].hashval == new_hash
216 && strcmp (entries[idx].name, undef_name) == 0)
218 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
220 /* We possibly have to initialize the central
221 copy from the copy addressed through the
222 relocation. */
223 result->s = sym;
224 result->m = (struct link_map *) map;
226 else
228 result->s = entries[idx].sym;
229 result->m = (struct link_map *) entries[idx].map;
231 __rtld_lock_unlock_recursive (tab->lock);
232 return;
235 if (entries[idx].name == NULL)
236 break;
238 idx += hash2;
239 if (idx >= size)
240 idx -= size;
243 if (size * 3 <= tab->n_elements * 4)
245 /* Expand the table. */
246 #ifdef RTLD_CHECK_FOREIGN_CALL
247 /* This must not happen during runtime relocations. */
248 assert (!RTLD_CHECK_FOREIGN_CALL);
249 #endif
250 size_t newsize = _dl_higher_prime_number (size + 1);
251 struct unique_sym *newentries
252 = calloc (sizeof (struct unique_sym), newsize);
253 if (newentries == NULL)
255 nomem:
256 __rtld_lock_unlock_recursive (tab->lock);
257 _dl_fatal_printf ("out of memory\n");
260 for (idx = 0; idx < size; ++idx)
261 if (entries[idx].name != NULL)
262 enter_unique_sym (newentries, newsize, entries[idx].hashval,
263 entries[idx].name, entries[idx].sym,
264 entries[idx].map);
266 tab->free (entries);
267 tab->size = newsize;
268 size = newsize;
269 entries = tab->entries = newentries;
270 tab->free = free;
273 else
275 #ifdef RTLD_CHECK_FOREIGN_CALL
276 /* This must not happen during runtime relocations. */
277 assert (!RTLD_CHECK_FOREIGN_CALL);
278 #endif
280 #ifdef SHARED
281 /* If tab->entries is NULL, but tab->size is not, it means
282 this is the second, conflict finding, lookup for
283 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
284 allocate anything and don't enter anything into the
285 hash table. */
286 if (__glibc_unlikely (tab->size))
288 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
289 goto success;
291 #endif
293 #define INITIAL_NUNIQUE_SYM_TABLE 31
294 size = INITIAL_NUNIQUE_SYM_TABLE;
295 entries = calloc (sizeof (struct unique_sym), size);
296 if (entries == NULL)
297 goto nomem;
299 tab->entries = entries;
300 tab->size = size;
301 tab->free = free;
304 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
305 enter_unique_sym (entries, size, new_hash, strtab + sym->st_name, ref,
306 undef_map);
307 else
309 enter_unique_sym (entries, size,
310 new_hash, strtab + sym->st_name, sym, map);
312 if (map->l_type == lt_loaded)
313 /* Make sure we don't unload this object by
314 setting the appropriate flag. */
315 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
317 ++tab->n_elements;
319 #ifdef SHARED
320 success:
321 #endif
322 __rtld_lock_unlock_recursive (tab->lock);
324 result->s = sym;
325 result->m = (struct link_map *) map;
328 /* Inner part of the lookup functions. We return a value > 0 if we
329 found the symbol, the value 0 if nothing is found and < 0 if
330 something bad happened. */
331 static int
332 __attribute_noinline__
333 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
334 unsigned long int *old_hash, const ElfW(Sym) *ref,
335 struct sym_val *result, struct r_scope_elem *scope, size_t i,
336 const struct r_found_version *const version, int flags,
337 struct link_map *skip, int type_class, struct link_map *undef_map)
339 size_t n = scope->r_nlist;
340 /* Make sure we read the value before proceeding. Otherwise we
341 might use r_list pointing to the initial scope and r_nlist being
342 the value after a resize. That is the only path in dl-open.c not
343 protected by GSCOPE. A read barrier here might be to expensive. */
344 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
345 struct link_map **list = scope->r_list;
349 const struct link_map *map = list[i]->l_real;
351 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
352 if (map == skip)
353 continue;
355 /* Don't search the executable when resolving a copy reloc. */
356 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
357 continue;
359 /* Do not look into objects which are going to be removed. */
360 if (map->l_removed)
361 continue;
363 /* Print some debugging info if wanted. */
364 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
365 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
366 undef_name, DSO_FILENAME (map->l_name),
367 map->l_ns);
369 /* If the hash table is empty there is nothing to do here. */
370 if (map->l_nbuckets == 0)
371 continue;
373 Elf_Symndx symidx;
374 int num_versions = 0;
375 const ElfW(Sym) *versioned_sym = NULL;
377 /* The tables for this map. */
378 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
379 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
381 const ElfW(Sym) *sym;
382 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
383 if (__glibc_likely (bitmask != NULL))
385 ElfW(Addr) bitmask_word
386 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
387 & map->l_gnu_bitmask_idxbits];
389 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
390 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
391 & (__ELF_NATIVE_CLASS - 1));
393 if (__glibc_unlikely ((bitmask_word >> hashbit1)
394 & (bitmask_word >> hashbit2) & 1))
396 Elf32_Word bucket = map->l_gnu_buckets[new_hash
397 % map->l_nbuckets];
398 if (bucket != 0)
400 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
403 if (((*hasharr ^ new_hash) >> 1) == 0)
405 symidx = hasharr - map->l_gnu_chain_zero;
406 sym = check_match (undef_name, ref, version, flags,
407 type_class, &symtab[symidx], symidx,
408 strtab, map, &versioned_sym,
409 &num_versions);
410 if (sym != NULL)
411 goto found_it;
413 while ((*hasharr++ & 1u) == 0);
416 /* No symbol found. */
417 symidx = SHN_UNDEF;
419 else
421 if (*old_hash == 0xffffffff)
422 *old_hash = _dl_elf_hash (undef_name);
424 /* Use the old SysV-style hash table. Search the appropriate
425 hash bucket in this object's symbol table for a definition
426 for the same symbol name. */
427 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
428 symidx != STN_UNDEF;
429 symidx = map->l_chain[symidx])
431 sym = check_match (undef_name, ref, version, flags,
432 type_class, &symtab[symidx], symidx,
433 strtab, map, &versioned_sym,
434 &num_versions);
435 if (sym != NULL)
436 goto found_it;
440 /* If we have seen exactly one versioned symbol while we are
441 looking for an unversioned symbol and the version is not the
442 default version we still accept this symbol since there are
443 no possible ambiguities. */
444 sym = num_versions == 1 ? versioned_sym : NULL;
446 if (sym != NULL)
448 found_it:
449 /* When UNDEF_MAP is NULL, which indicates we are called from
450 do_lookup_x on relocation against protected data, we skip
451 the data definion in the executable from copy reloc. */
452 if (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
453 && undef_map == NULL
454 && map->l_type == lt_executable
455 && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)
457 const ElfW(Sym) *s;
458 unsigned int i;
460 #if ! ELF_MACHINE_NO_RELA
461 if (map->l_info[DT_RELA] != NULL
462 && map->l_info[DT_RELASZ] != NULL
463 && map->l_info[DT_RELASZ]->d_un.d_val != 0)
465 const ElfW(Rela) *rela
466 = (const ElfW(Rela) *) D_PTR (map, l_info[DT_RELA]);
467 unsigned int rela_count
468 = map->l_info[DT_RELASZ]->d_un.d_val / sizeof (*rela);
470 for (i = 0; i < rela_count; i++, rela++)
471 if (elf_machine_type_class (ELFW(R_TYPE) (rela->r_info))
472 == ELF_RTYPE_CLASS_COPY)
474 s = &symtab[ELFW(R_SYM) (rela->r_info)];
475 if (!strcmp (strtab + s->st_name, undef_name))
476 goto skip;
479 #endif
480 #if ! ELF_MACHINE_NO_REL
481 if (map->l_info[DT_REL] != NULL
482 && map->l_info[DT_RELSZ] != NULL
483 && map->l_info[DT_RELSZ]->d_un.d_val != 0)
485 const ElfW(Rel) *rel
486 = (const ElfW(Rel) *) D_PTR (map, l_info[DT_REL]);
487 unsigned int rel_count
488 = map->l_info[DT_RELSZ]->d_un.d_val / sizeof (*rel);
490 for (i = 0; i < rel_count; i++, rel++)
491 if (elf_machine_type_class (ELFW(R_TYPE) (rel->r_info))
492 == ELF_RTYPE_CLASS_COPY)
494 s = &symtab[ELFW(R_SYM) (rel->r_info)];
495 if (!strcmp (strtab + s->st_name, undef_name))
496 goto skip;
499 #endif
502 /* Hidden and internal symbols are local, ignore them. */
503 if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym)))
504 goto skip;
506 switch (ELFW(ST_BIND) (sym->st_info))
508 case STB_WEAK:
509 /* Weak definition. Use this value if we don't find another. */
510 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
512 if (! result->s)
514 result->s = sym;
515 result->m = (struct link_map *) map;
517 break;
519 /* FALLTHROUGH */
520 case STB_GLOBAL:
521 /* Global definition. Just what we need. */
522 result->s = sym;
523 result->m = (struct link_map *) map;
524 return 1;
526 case STB_GNU_UNIQUE:;
527 do_lookup_unique (undef_name, new_hash, map, result, type_class,
528 sym, strtab, ref, undef_map);
529 return 1;
531 default:
532 /* Local symbols are ignored. */
533 break;
537 skip:
538 /* If this current map is the one mentioned in the verneed entry
539 and we have not found a weak entry, it is a bug. */
540 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
541 && __glibc_unlikely (_dl_name_match_p (version->filename, map)))
542 return -1;
544 while (++i < n);
546 /* We have not found anything until now. */
547 return 0;
551 static uint_fast32_t
552 dl_new_hash (const char *s)
554 uint_fast32_t h = 5381;
555 for (unsigned char c = *s; c != '\0'; c = *++s)
556 h = h * 33 + c;
557 return h & 0xffffffff;
561 /* Add extra dependency on MAP to UNDEF_MAP. */
562 static int
563 internal_function
564 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
566 struct link_map *runp;
567 unsigned int i;
568 int result = 0;
570 /* Avoid self-references and references to objects which cannot be
571 unloaded anyway. */
572 if (undef_map == map)
573 return 0;
575 /* Avoid references to objects which cannot be unloaded anyway. */
576 assert (map->l_type == lt_loaded);
577 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
578 return 0;
580 struct link_map_reldeps *l_reldeps
581 = atomic_forced_read (undef_map->l_reldeps);
583 /* Make sure l_reldeps is read before l_initfini. */
584 atomic_read_barrier ();
586 /* Determine whether UNDEF_MAP already has a reference to MAP. First
587 look in the normal dependencies. */
588 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
589 if (l_initfini != NULL)
591 for (i = 0; l_initfini[i] != NULL; ++i)
592 if (l_initfini[i] == map)
593 return 0;
596 /* No normal dependency. See whether we already had to add it
597 to the special list of dynamic dependencies. */
598 unsigned int l_reldepsact = 0;
599 if (l_reldeps != NULL)
601 struct link_map **list = &l_reldeps->list[0];
602 l_reldepsact = l_reldeps->act;
603 for (i = 0; i < l_reldepsact; ++i)
604 if (list[i] == map)
605 return 0;
608 /* Save serial number of the target MAP. */
609 unsigned long long serial = map->l_serial;
611 /* Make sure nobody can unload the object while we are at it. */
612 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
614 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
615 here, that can result in ABBA deadlock. */
616 THREAD_GSCOPE_RESET_FLAG ();
617 __rtld_lock_lock_recursive (GL(dl_load_lock));
618 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
619 it can e.g. point to unallocated memory. So avoid the optimizer
620 treating the above read from MAP->l_serial as ensurance it
621 can safely dereference it. */
622 map = atomic_forced_read (map);
624 /* From this point on it is unsafe to dereference MAP, until it
625 has been found in one of the lists. */
627 /* Redo the l_initfini check in case undef_map's l_initfini
628 changed in the mean time. */
629 if (undef_map->l_initfini != l_initfini
630 && undef_map->l_initfini != NULL)
632 l_initfini = undef_map->l_initfini;
633 for (i = 0; l_initfini[i] != NULL; ++i)
634 if (l_initfini[i] == map)
635 goto out_check;
638 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
639 the mean time. */
640 if (undef_map->l_reldeps != NULL)
642 if (undef_map->l_reldeps != l_reldeps)
644 struct link_map **list = &undef_map->l_reldeps->list[0];
645 l_reldepsact = undef_map->l_reldeps->act;
646 for (i = 0; i < l_reldepsact; ++i)
647 if (list[i] == map)
648 goto out_check;
650 else if (undef_map->l_reldeps->act > l_reldepsact)
652 struct link_map **list
653 = &undef_map->l_reldeps->list[0];
654 i = l_reldepsact;
655 l_reldepsact = undef_map->l_reldeps->act;
656 for (; i < l_reldepsact; ++i)
657 if (list[i] == map)
658 goto out_check;
662 else
663 __rtld_lock_lock_recursive (GL(dl_load_lock));
665 /* The object is not yet in the dependency list. Before we add
666 it make sure just one more time the object we are about to
667 reference is still available. There is a brief period in
668 which the object could have been removed since we found the
669 definition. */
670 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
671 while (runp != NULL && runp != map)
672 runp = runp->l_next;
674 if (runp != NULL)
676 /* The object is still available. */
678 /* MAP could have been dlclosed, freed and then some other dlopened
679 library could have the same link_map pointer. */
680 if (map->l_serial != serial)
681 goto out_check;
683 /* Redo the NODELETE check, as when dl_load_lock wasn't held
684 yet this could have changed. */
685 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
686 goto out;
688 /* If the object with the undefined reference cannot be removed ever
689 just make sure the same is true for the object which contains the
690 definition. */
691 if (undef_map->l_type != lt_loaded
692 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
694 map->l_flags_1 |= DF_1_NODELETE;
695 goto out;
698 /* Add the reference now. */
699 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
701 /* Allocate more memory for the dependency list. Since this
702 can never happen during the startup phase we can use
703 `realloc'. */
704 struct link_map_reldeps *newp;
705 unsigned int max
706 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
708 #ifdef RTLD_PREPARE_FOREIGN_CALL
709 RTLD_PREPARE_FOREIGN_CALL;
710 #endif
712 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
713 if (newp == NULL)
715 /* If we didn't manage to allocate memory for the list this is
716 no fatal problem. We simply make sure the referenced object
717 cannot be unloaded. This is semantically the correct
718 behavior. */
719 map->l_flags_1 |= DF_1_NODELETE;
720 goto out;
722 else
724 if (l_reldepsact)
725 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
726 l_reldepsact * sizeof (struct link_map *));
727 newp->list[l_reldepsact] = map;
728 newp->act = l_reldepsact + 1;
729 atomic_write_barrier ();
730 void *old = undef_map->l_reldeps;
731 undef_map->l_reldeps = newp;
732 undef_map->l_reldepsmax = max;
733 if (old)
734 _dl_scope_free (old);
737 else
739 undef_map->l_reldeps->list[l_reldepsact] = map;
740 atomic_write_barrier ();
741 undef_map->l_reldeps->act = l_reldepsact + 1;
744 /* Display information if we are debugging. */
745 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
746 _dl_debug_printf ("\
747 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
748 DSO_FILENAME (map->l_name),
749 map->l_ns,
750 DSO_FILENAME (undef_map->l_name),
751 undef_map->l_ns);
753 else
754 /* Whoa, that was bad luck. We have to search again. */
755 result = -1;
757 out:
758 /* Release the lock. */
759 __rtld_lock_unlock_recursive (GL(dl_load_lock));
761 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
762 THREAD_GSCOPE_SET_FLAG ();
764 return result;
766 out_check:
767 if (map->l_serial != serial)
768 result = -1;
769 goto out;
772 static void
773 internal_function
774 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
775 const ElfW(Sym) **ref, struct sym_val *value,
776 const struct r_found_version *version, int type_class,
777 int protected);
780 /* Search loaded objects' symbol tables for a definition of the symbol
781 UNDEF_NAME, perhaps with a requested version for the symbol.
783 We must never have calls to the audit functions inside this function
784 or in any function which gets called. If this would happen the audit
785 code might create a thread which can throw off all the scope locking. */
786 lookup_t
787 internal_function
788 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
789 const ElfW(Sym) **ref,
790 struct r_scope_elem *symbol_scope[],
791 const struct r_found_version *version,
792 int type_class, int flags, struct link_map *skip_map)
794 const uint_fast32_t new_hash = dl_new_hash (undef_name);
795 unsigned long int old_hash = 0xffffffff;
796 struct sym_val current_value = { NULL, NULL };
797 struct r_scope_elem **scope = symbol_scope;
799 bump_num_relocations ();
801 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
802 is allowed if we look up a versioned symbol. */
803 assert (version == NULL
804 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
805 == 0);
807 size_t i = 0;
808 if (__glibc_unlikely (skip_map != NULL))
809 /* Search the relevant loaded objects for a definition. */
810 while ((*scope)->r_list[i] != skip_map)
811 ++i;
813 /* Search the relevant loaded objects for a definition. */
814 for (size_t start = i; *scope != NULL; start = 0, ++scope)
816 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
817 &current_value, *scope, start, version, flags,
818 skip_map, type_class, undef_map);
819 if (res > 0)
820 break;
822 if (__glibc_unlikely (res < 0) && skip_map == NULL)
824 /* Oh, oh. The file named in the relocation entry does not
825 contain the needed symbol. This code is never reached
826 for unversioned lookups. */
827 assert (version != NULL);
828 const char *reference_name = undef_map ? undef_map->l_name : "";
829 struct dl_exception exception;
830 /* XXX We cannot translate the message. */
831 _dl_exception_create_format
832 (&exception, DSO_FILENAME (reference_name),
833 "symbol %s version %s not defined in file %s"
834 " with link time reference%s",
835 undef_name, version->name, version->filename,
836 res == -2 ? " (no version symbols)" : "");
837 _dl_signal_cexception (0, &exception, N_("relocation error"));
838 _dl_exception_free (&exception);
839 *ref = NULL;
840 return 0;
844 if (__glibc_unlikely (current_value.s == NULL))
846 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
847 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
849 /* We could find no value for a strong reference. */
850 const char *reference_name = undef_map ? undef_map->l_name : "";
851 const char *versionstr = version ? ", version " : "";
852 const char *versionname = (version && version->name
853 ? version->name : "");
854 struct dl_exception exception;
855 /* XXX We cannot translate the message. */
856 _dl_exception_create_format
857 (&exception, DSO_FILENAME (reference_name),
858 "undefined symbol: %s%s%s",
859 undef_name, versionstr, versionname);
860 _dl_signal_cexception (0, &exception, N_("symbol lookup error"));
861 _dl_exception_free (&exception);
863 *ref = NULL;
864 return 0;
867 int protected = (*ref
868 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
869 if (__glibc_unlikely (protected != 0))
871 /* It is very tricky. We need to figure out what value to
872 return for the protected symbol. */
873 if (type_class == ELF_RTYPE_CLASS_PLT)
875 if (current_value.s != NULL && current_value.m != undef_map)
877 current_value.s = *ref;
878 current_value.m = undef_map;
881 else
883 struct sym_val protected_value = { NULL, NULL };
885 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
886 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
887 &protected_value, *scope, i, version, flags,
888 skip_map,
889 (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
890 && ELFW(ST_TYPE) ((*ref)->st_info) == STT_OBJECT
891 && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)
892 ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
893 : ELF_RTYPE_CLASS_PLT, NULL) != 0)
894 break;
896 if (protected_value.s != NULL && protected_value.m != undef_map)
898 current_value.s = *ref;
899 current_value.m = undef_map;
904 /* We have to check whether this would bind UNDEF_MAP to an object
905 in the global scope which was dynamically loaded. In this case
906 we have to prevent the latter from being unloaded unless the
907 UNDEF_MAP object is also unloaded. */
908 if (__glibc_unlikely (current_value.m->l_type == lt_loaded)
909 /* Don't do this for explicit lookups as opposed to implicit
910 runtime lookups. */
911 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
912 /* Add UNDEF_MAP to the dependencies. */
913 && add_dependency (undef_map, current_value.m, flags) < 0)
914 /* Something went wrong. Perhaps the object we tried to reference
915 was just removed. Try finding another definition. */
916 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
917 (flags & DL_LOOKUP_GSCOPE_LOCK)
918 ? undef_map->l_scope : symbol_scope,
919 version, type_class, flags, skip_map);
921 /* The object is used. */
922 if (__glibc_unlikely (current_value.m->l_used == 0))
923 current_value.m->l_used = 1;
925 if (__glibc_unlikely (GLRO(dl_debug_mask)
926 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK)))
927 _dl_debug_bindings (undef_name, undef_map, ref,
928 &current_value, version, type_class, protected);
930 *ref = current_value.s;
931 return LOOKUP_VALUE (current_value.m);
935 /* Cache the location of MAP's hash table. */
937 void
938 internal_function
939 _dl_setup_hash (struct link_map *map)
941 Elf_Symndx *hash;
943 if (__glibc_likely (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
944 + DT_THISPROCNUM + DT_VERSIONTAGNUM
945 + DT_EXTRANUM + DT_VALNUM] != NULL))
947 Elf32_Word *hash32
948 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
949 + DT_THISPROCNUM + DT_VERSIONTAGNUM
950 + DT_EXTRANUM + DT_VALNUM]);
951 map->l_nbuckets = *hash32++;
952 Elf32_Word symbias = *hash32++;
953 Elf32_Word bitmask_nwords = *hash32++;
954 /* Must be a power of two. */
955 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
956 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
957 map->l_gnu_shift = *hash32++;
959 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
960 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
962 map->l_gnu_buckets = hash32;
963 hash32 += map->l_nbuckets;
964 map->l_gnu_chain_zero = hash32 - symbias;
965 return;
968 if (!map->l_info[DT_HASH])
969 return;
970 hash = (void *) D_PTR (map, l_info[DT_HASH]);
972 map->l_nbuckets = *hash++;
973 /* Skip nchain. */
974 hash++;
975 map->l_buckets = hash;
976 hash += map->l_nbuckets;
977 map->l_chain = hash;
981 static void
982 internal_function
983 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
984 const ElfW(Sym) **ref, struct sym_val *value,
985 const struct r_found_version *version, int type_class,
986 int protected)
988 const char *reference_name = undef_map->l_name;
990 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
992 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
993 DSO_FILENAME (reference_name),
994 undef_map->l_ns,
995 DSO_FILENAME (value->m->l_name),
996 value->m->l_ns,
997 protected ? "protected" : "normal", undef_name);
998 if (version)
999 _dl_debug_printf_c (" [%s]\n", version->name);
1000 else
1001 _dl_debug_printf_c ("\n");
1003 #ifdef SHARED
1004 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1006 /* ELF_RTYPE_CLASS_XXX must match RTYPE_CLASS_XXX used by prelink with
1007 LD_TRACE_PRELINKING. */
1008 #define RTYPE_CLASS_VALID 8
1009 #define RTYPE_CLASS_PLT (8|1)
1010 #define RTYPE_CLASS_COPY (8|2)
1011 #define RTYPE_CLASS_TLS (8|4)
1012 #if ELF_RTYPE_CLASS_PLT != 0 && ELF_RTYPE_CLASS_PLT != 1
1013 # error ELF_RTYPE_CLASS_PLT must be 0 or 1!
1014 #endif
1015 #if ELF_RTYPE_CLASS_COPY != 0 && ELF_RTYPE_CLASS_COPY != 2
1016 # error ELF_RTYPE_CLASS_COPY must be 0 or 2!
1017 #endif
1018 int conflict = 0;
1019 struct sym_val val = { NULL, NULL };
1021 if ((GLRO(dl_trace_prelink_map) == NULL
1022 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
1023 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
1025 const uint_fast32_t new_hash = dl_new_hash (undef_name);
1026 unsigned long int old_hash = 0xffffffff;
1027 struct unique_sym *saved_entries
1028 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
1030 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
1031 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
1032 undef_map->l_local_scope[0], 0, version, 0, NULL,
1033 type_class, undef_map);
1034 if (val.s != value->s || val.m != value->m)
1035 conflict = 1;
1036 else if (__glibc_unlikely (undef_map->l_symbolic_in_local_scope)
1037 && val.s
1038 && __glibc_unlikely (ELFW(ST_BIND) (val.s->st_info)
1039 == STB_GNU_UNIQUE))
1041 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
1042 contains any DT_SYMBOLIC libraries, unfortunately there
1043 can be conflicts even if the above is equal. As symbol
1044 resolution goes from the last library to the first and
1045 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
1046 library, it would be the one that is looked up. */
1047 struct sym_val val2 = { NULL, NULL };
1048 size_t n;
1049 struct r_scope_elem *scope = undef_map->l_local_scope[0];
1051 for (n = 0; n < scope->r_nlist; n++)
1052 if (scope->r_list[n] == val.m)
1053 break;
1055 for (n++; n < scope->r_nlist; n++)
1056 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
1057 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
1058 &val2,
1059 &scope->r_list[n]->l_symbolic_searchlist,
1060 0, version, 0, NULL, type_class,
1061 undef_map) > 0)
1063 conflict = 1;
1064 val = val2;
1065 break;
1068 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
1071 if (value->s)
1073 /* Keep only ELF_RTYPE_CLASS_PLT and ELF_RTYPE_CLASS_COPY
1074 bits since since prelink only uses them. */
1075 type_class &= ELF_RTYPE_CLASS_PLT | ELF_RTYPE_CLASS_COPY;
1076 if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1077 == STT_TLS))
1078 /* Clear the RTYPE_CLASS_VALID bit in RTYPE_CLASS_TLS. */
1079 type_class = RTYPE_CLASS_TLS & ~RTYPE_CLASS_VALID;
1080 else if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1081 == STT_GNU_IFUNC))
1082 /* Set the RTYPE_CLASS_VALID bit. */
1083 type_class |= RTYPE_CLASS_VALID;
1086 if (conflict
1087 || GLRO(dl_trace_prelink_map) == undef_map
1088 || GLRO(dl_trace_prelink_map) == NULL
1089 || type_class >= 4)
1091 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1092 conflict ? "conflict" : "lookup",
1093 (int) sizeof (ElfW(Addr)) * 2,
1094 (size_t) undef_map->l_map_start,
1095 (int) sizeof (ElfW(Addr)) * 2,
1096 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1097 (int) sizeof (ElfW(Addr)) * 2,
1098 (size_t) (value->s ? value->m->l_map_start : 0),
1099 (int) sizeof (ElfW(Addr)) * 2,
1100 (size_t) (value->s ? value->s->st_value : 0));
1102 if (conflict)
1103 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1104 (int) sizeof (ElfW(Addr)) * 2,
1105 (size_t) (val.s ? val.m->l_map_start : 0),
1106 (int) sizeof (ElfW(Addr)) * 2,
1107 (size_t) (val.s ? val.s->st_value : 0));
1109 _dl_printf ("/%x %s\n", type_class, undef_name);
1112 #endif