posix: Fix posix_spawn implict check style
[glibc.git] / elf / dl-lookup.c
blobf577759a9d377797b9ed7ce74328fd4ba1ca4c6f
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2016 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 #define make_string(string, rest...) \
51 ({ \
52 const char *all[] = { string, ## rest }; \
53 size_t len, cnt; \
54 char *result, *cp; \
56 len = 1; \
57 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
58 len += strlen (all[cnt]); \
60 cp = result = alloca (len); \
61 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
62 cp = __stpcpy (cp, all[cnt]); \
64 result; \
67 /* Statistics function. */
68 #ifdef SHARED
69 # define bump_num_relocations() ++GL(dl_num_relocations)
70 #else
71 # define bump_num_relocations() ((void) 0)
72 #endif
74 /* Utility function for do_lookup_x. The caller is called with undef_name,
75 ref, version, flags and type_class, and those are passed as the first
76 five arguments. The caller then computes sym, symidx, strtab, and map
77 and passes them as the next four arguments. Lastly the caller passes in
78 versioned_sym and num_versions which are modified by check_match during
79 the checking process. */
80 static const ElfW(Sym) *
81 check_match (const char *const undef_name,
82 const ElfW(Sym) *const ref,
83 const struct r_found_version *const version,
84 const int flags,
85 const int type_class,
86 const ElfW(Sym) *const sym,
87 const Elf_Symndx symidx,
88 const char *const strtab,
89 const struct link_map *const map,
90 const ElfW(Sym) **const versioned_sym,
91 int *const num_versions)
93 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
94 assert (ELF_RTYPE_CLASS_PLT == 1);
95 if (__glibc_unlikely ((sym->st_value == 0 /* No value. */
96 && stt != STT_TLS)
97 || ELF_MACHINE_SYM_NO_MATCH (sym)
98 || (type_class & (sym->st_shndx == SHN_UNDEF))))
99 return NULL;
101 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
102 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
103 code/data definitions. */
104 #define ALLOWED_STT \
105 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
106 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
107 if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
108 return NULL;
110 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
111 /* Not the symbol we are looking for. */
112 return NULL;
114 const ElfW(Half) *verstab = map->l_versyms;
115 if (version != NULL)
117 if (__glibc_unlikely (verstab == NULL))
119 /* We need a versioned symbol but haven't found any. If
120 this is the object which is referenced in the verneed
121 entry it is a bug in the library since a symbol must
122 not simply disappear.
124 It would also be a bug in the object since it means that
125 the list of required versions is incomplete and so the
126 tests in dl-version.c haven't found a problem.*/
127 assert (version->filename == NULL
128 || ! _dl_name_match_p (version->filename, map));
130 /* Otherwise we accept the symbol. */
132 else
134 /* We can match the version information or use the
135 default one if it is not hidden. */
136 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
137 if ((map->l_versions[ndx].hash != version->hash
138 || strcmp (map->l_versions[ndx].name, version->name))
139 && (version->hidden || map->l_versions[ndx].hash
140 || (verstab[symidx] & 0x8000)))
141 /* It's not the version we want. */
142 return NULL;
145 else
147 /* No specific version is selected. There are two ways we
148 can got here:
150 - a binary which does not include versioning information
151 is loaded
153 - dlsym() instead of dlvsym() is used to get a symbol which
154 might exist in more than one form
156 If the library does not provide symbol version information
157 there is no problem at all: we simply use the symbol if it
158 is defined.
160 These two lookups need to be handled differently if the
161 library defines versions. In the case of the old
162 unversioned application the oldest (default) version
163 should be used. In case of a dlsym() call the latest and
164 public interface should be returned. */
165 if (verstab != NULL)
167 if ((verstab[symidx] & 0x7fff)
168 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
170 /* Don't accept hidden symbols. */
171 if ((verstab[symidx] & 0x8000) == 0
172 && (*num_versions)++ == 0)
173 /* No version so far. */
174 *versioned_sym = sym;
176 return NULL;
181 /* There cannot be another entry for this symbol so stop here. */
182 return sym;
185 /* Utility function for do_lookup_unique. Add a symbol to TABLE. */
186 static void
187 enter_unique_sym (struct unique_sym *table, size_t size,
188 unsigned int hash, const char *name,
189 const ElfW(Sym) *sym, const struct link_map *map)
191 size_t idx = hash % size;
192 size_t hash2 = 1 + hash % (size - 2);
193 while (table[idx].name != NULL)
195 idx += hash2;
196 if (idx >= size)
197 idx -= size;
200 table[idx].hashval = hash;
201 table[idx].name = name;
202 table[idx].sym = sym;
203 table[idx].map = map;
206 /* Utility function for do_lookup_x. Lookup an STB_GNU_UNIQUE symbol
207 in the unique symbol table, creating a new entry if necessary.
208 Return the matching symbol in RESULT. */
209 static void
210 do_lookup_unique (const char *undef_name, uint_fast32_t new_hash,
211 const struct link_map *map, struct sym_val *result,
212 int type_class, const ElfW(Sym) *sym, const char *strtab,
213 const ElfW(Sym) *ref, const struct link_map *undef_map)
215 /* We have to determine whether we already found a symbol with this
216 name before. If not then we have to add it to the search table.
217 If we already found a definition we have to use it. */
219 struct unique_sym_table *tab
220 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
222 __rtld_lock_lock_recursive (tab->lock);
224 struct unique_sym *entries = tab->entries;
225 size_t size = tab->size;
226 if (entries != NULL)
228 size_t idx = new_hash % size;
229 size_t hash2 = 1 + new_hash % (size - 2);
230 while (1)
232 if (entries[idx].hashval == new_hash
233 && strcmp (entries[idx].name, undef_name) == 0)
235 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
237 /* We possibly have to initialize the central
238 copy from the copy addressed through the
239 relocation. */
240 result->s = sym;
241 result->m = (struct link_map *) map;
243 else
245 result->s = entries[idx].sym;
246 result->m = (struct link_map *) entries[idx].map;
248 __rtld_lock_unlock_recursive (tab->lock);
249 return;
252 if (entries[idx].name == NULL)
253 break;
255 idx += hash2;
256 if (idx >= size)
257 idx -= size;
260 if (size * 3 <= tab->n_elements * 4)
262 /* Expand the table. */
263 #ifdef RTLD_CHECK_FOREIGN_CALL
264 /* This must not happen during runtime relocations. */
265 assert (!RTLD_CHECK_FOREIGN_CALL);
266 #endif
267 size_t newsize = _dl_higher_prime_number (size + 1);
268 struct unique_sym *newentries
269 = calloc (sizeof (struct unique_sym), newsize);
270 if (newentries == NULL)
272 nomem:
273 __rtld_lock_unlock_recursive (tab->lock);
274 _dl_fatal_printf ("out of memory\n");
277 for (idx = 0; idx < size; ++idx)
278 if (entries[idx].name != NULL)
279 enter_unique_sym (newentries, newsize, entries[idx].hashval,
280 entries[idx].name, entries[idx].sym,
281 entries[idx].map);
283 tab->free (entries);
284 tab->size = newsize;
285 size = newsize;
286 entries = tab->entries = newentries;
287 tab->free = free;
290 else
292 #ifdef RTLD_CHECK_FOREIGN_CALL
293 /* This must not happen during runtime relocations. */
294 assert (!RTLD_CHECK_FOREIGN_CALL);
295 #endif
297 #ifdef SHARED
298 /* If tab->entries is NULL, but tab->size is not, it means
299 this is the second, conflict finding, lookup for
300 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
301 allocate anything and don't enter anything into the
302 hash table. */
303 if (__glibc_unlikely (tab->size))
305 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
306 goto success;
308 #endif
310 #define INITIAL_NUNIQUE_SYM_TABLE 31
311 size = INITIAL_NUNIQUE_SYM_TABLE;
312 entries = calloc (sizeof (struct unique_sym), size);
313 if (entries == NULL)
314 goto nomem;
316 tab->entries = entries;
317 tab->size = size;
318 tab->free = free;
321 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
322 enter_unique_sym (entries, size, new_hash, strtab + sym->st_name, ref,
323 undef_map);
324 else
326 enter_unique_sym (entries, size,
327 new_hash, strtab + sym->st_name, sym, map);
329 if (map->l_type == lt_loaded)
330 /* Make sure we don't unload this object by
331 setting the appropriate flag. */
332 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
334 ++tab->n_elements;
336 #ifdef SHARED
337 success:
338 #endif
339 __rtld_lock_unlock_recursive (tab->lock);
341 result->s = sym;
342 result->m = (struct link_map *) map;
345 /* Inner part of the lookup functions. We return a value > 0 if we
346 found the symbol, the value 0 if nothing is found and < 0 if
347 something bad happened. */
348 static int
349 __attribute_noinline__
350 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
351 unsigned long int *old_hash, const ElfW(Sym) *ref,
352 struct sym_val *result, struct r_scope_elem *scope, size_t i,
353 const struct r_found_version *const version, int flags,
354 struct link_map *skip, int type_class, struct link_map *undef_map)
356 size_t n = scope->r_nlist;
357 /* Make sure we read the value before proceeding. Otherwise we
358 might use r_list pointing to the initial scope and r_nlist being
359 the value after a resize. That is the only path in dl-open.c not
360 protected by GSCOPE. A read barrier here might be to expensive. */
361 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
362 struct link_map **list = scope->r_list;
366 const struct link_map *map = list[i]->l_real;
368 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
369 if (map == skip)
370 continue;
372 /* Don't search the executable when resolving a copy reloc. */
373 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
374 continue;
376 /* Do not look into objects which are going to be removed. */
377 if (map->l_removed)
378 continue;
380 /* Print some debugging info if wanted. */
381 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
382 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
383 undef_name, DSO_FILENAME (map->l_name),
384 map->l_ns);
386 /* If the hash table is empty there is nothing to do here. */
387 if (map->l_nbuckets == 0)
388 continue;
390 Elf_Symndx symidx;
391 int num_versions = 0;
392 const ElfW(Sym) *versioned_sym = NULL;
394 /* The tables for this map. */
395 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
396 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
398 const ElfW(Sym) *sym;
399 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
400 if (__glibc_likely (bitmask != NULL))
402 ElfW(Addr) bitmask_word
403 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
404 & map->l_gnu_bitmask_idxbits];
406 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
407 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
408 & (__ELF_NATIVE_CLASS - 1));
410 if (__glibc_unlikely ((bitmask_word >> hashbit1)
411 & (bitmask_word >> hashbit2) & 1))
413 Elf32_Word bucket = map->l_gnu_buckets[new_hash
414 % map->l_nbuckets];
415 if (bucket != 0)
417 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
420 if (((*hasharr ^ new_hash) >> 1) == 0)
422 symidx = hasharr - map->l_gnu_chain_zero;
423 sym = check_match (undef_name, ref, version, flags,
424 type_class, &symtab[symidx], symidx,
425 strtab, map, &versioned_sym,
426 &num_versions);
427 if (sym != NULL)
428 goto found_it;
430 while ((*hasharr++ & 1u) == 0);
433 /* No symbol found. */
434 symidx = SHN_UNDEF;
436 else
438 if (*old_hash == 0xffffffff)
439 *old_hash = _dl_elf_hash (undef_name);
441 /* Use the old SysV-style hash table. Search the appropriate
442 hash bucket in this object's symbol table for a definition
443 for the same symbol name. */
444 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
445 symidx != STN_UNDEF;
446 symidx = map->l_chain[symidx])
448 sym = check_match (undef_name, ref, version, flags,
449 type_class, &symtab[symidx], symidx,
450 strtab, map, &versioned_sym,
451 &num_versions);
452 if (sym != NULL)
453 goto found_it;
457 /* If we have seen exactly one versioned symbol while we are
458 looking for an unversioned symbol and the version is not the
459 default version we still accept this symbol since there are
460 no possible ambiguities. */
461 sym = num_versions == 1 ? versioned_sym : NULL;
463 if (sym != NULL)
465 found_it:
466 /* When UNDEF_MAP is NULL, which indicates we are called from
467 do_lookup_x on relocation against protected data, we skip
468 the data definion in the executable from copy reloc. */
469 if (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
470 && undef_map == NULL
471 && map->l_type == lt_executable
472 && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)
474 const ElfW(Sym) *s;
475 unsigned int i;
477 #if ! ELF_MACHINE_NO_RELA
478 if (map->l_info[DT_RELA] != NULL
479 && map->l_info[DT_RELASZ] != NULL
480 && map->l_info[DT_RELASZ]->d_un.d_val != 0)
482 const ElfW(Rela) *rela
483 = (const ElfW(Rela) *) D_PTR (map, l_info[DT_RELA]);
484 unsigned int rela_count
485 = map->l_info[DT_RELASZ]->d_un.d_val / sizeof (*rela);
487 for (i = 0; i < rela_count; i++, rela++)
488 if (elf_machine_type_class (ELFW(R_TYPE) (rela->r_info))
489 == ELF_RTYPE_CLASS_COPY)
491 s = &symtab[ELFW(R_SYM) (rela->r_info)];
492 if (!strcmp (strtab + s->st_name, undef_name))
493 goto skip;
496 #endif
497 #if ! ELF_MACHINE_NO_REL
498 if (map->l_info[DT_REL] != NULL
499 && map->l_info[DT_RELSZ] != NULL
500 && map->l_info[DT_RELSZ]->d_un.d_val != 0)
502 const ElfW(Rel) *rel
503 = (const ElfW(Rel) *) D_PTR (map, l_info[DT_REL]);
504 unsigned int rel_count
505 = map->l_info[DT_RELSZ]->d_un.d_val / sizeof (*rel);
507 for (i = 0; i < rel_count; i++, rel++)
508 if (elf_machine_type_class (ELFW(R_TYPE) (rel->r_info))
509 == ELF_RTYPE_CLASS_COPY)
511 s = &symtab[ELFW(R_SYM) (rel->r_info)];
512 if (!strcmp (strtab + s->st_name, undef_name))
513 goto skip;
516 #endif
519 switch (ELFW(ST_BIND) (sym->st_info))
521 case STB_WEAK:
522 /* Weak definition. Use this value if we don't find another. */
523 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
525 if (! result->s)
527 result->s = sym;
528 result->m = (struct link_map *) map;
530 break;
532 /* FALLTHROUGH */
533 case STB_GLOBAL:
534 /* Global definition. Just what we need. */
535 result->s = sym;
536 result->m = (struct link_map *) map;
537 return 1;
539 case STB_GNU_UNIQUE:;
540 do_lookup_unique (undef_name, new_hash, map, result, type_class,
541 sym, strtab, ref, undef_map);
542 return 1;
544 default:
545 /* Local symbols are ignored. */
546 break;
550 skip:
551 /* If this current map is the one mentioned in the verneed entry
552 and we have not found a weak entry, it is a bug. */
553 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
554 && __glibc_unlikely (_dl_name_match_p (version->filename, map)))
555 return -1;
557 while (++i < n);
559 /* We have not found anything until now. */
560 return 0;
564 static uint_fast32_t
565 dl_new_hash (const char *s)
567 uint_fast32_t h = 5381;
568 for (unsigned char c = *s; c != '\0'; c = *++s)
569 h = h * 33 + c;
570 return h & 0xffffffff;
574 /* Add extra dependency on MAP to UNDEF_MAP. */
575 static int
576 internal_function
577 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
579 struct link_map *runp;
580 unsigned int i;
581 int result = 0;
583 /* Avoid self-references and references to objects which cannot be
584 unloaded anyway. */
585 if (undef_map == map)
586 return 0;
588 /* Avoid references to objects which cannot be unloaded anyway. */
589 assert (map->l_type == lt_loaded);
590 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
591 return 0;
593 struct link_map_reldeps *l_reldeps
594 = atomic_forced_read (undef_map->l_reldeps);
596 /* Make sure l_reldeps is read before l_initfini. */
597 atomic_read_barrier ();
599 /* Determine whether UNDEF_MAP already has a reference to MAP. First
600 look in the normal dependencies. */
601 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
602 if (l_initfini != NULL)
604 for (i = 0; l_initfini[i] != NULL; ++i)
605 if (l_initfini[i] == map)
606 return 0;
609 /* No normal dependency. See whether we already had to add it
610 to the special list of dynamic dependencies. */
611 unsigned int l_reldepsact = 0;
612 if (l_reldeps != NULL)
614 struct link_map **list = &l_reldeps->list[0];
615 l_reldepsact = l_reldeps->act;
616 for (i = 0; i < l_reldepsact; ++i)
617 if (list[i] == map)
618 return 0;
621 /* Save serial number of the target MAP. */
622 unsigned long long serial = map->l_serial;
624 /* Make sure nobody can unload the object while we are at it. */
625 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
627 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
628 here, that can result in ABBA deadlock. */
629 THREAD_GSCOPE_RESET_FLAG ();
630 __rtld_lock_lock_recursive (GL(dl_load_lock));
631 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
632 it can e.g. point to unallocated memory. So avoid the optimizer
633 treating the above read from MAP->l_serial as ensurance it
634 can safely dereference it. */
635 map = atomic_forced_read (map);
637 /* From this point on it is unsafe to dereference MAP, until it
638 has been found in one of the lists. */
640 /* Redo the l_initfini check in case undef_map's l_initfini
641 changed in the mean time. */
642 if (undef_map->l_initfini != l_initfini
643 && undef_map->l_initfini != NULL)
645 l_initfini = undef_map->l_initfini;
646 for (i = 0; l_initfini[i] != NULL; ++i)
647 if (l_initfini[i] == map)
648 goto out_check;
651 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
652 the mean time. */
653 if (undef_map->l_reldeps != NULL)
655 if (undef_map->l_reldeps != l_reldeps)
657 struct link_map **list = &undef_map->l_reldeps->list[0];
658 l_reldepsact = undef_map->l_reldeps->act;
659 for (i = 0; i < l_reldepsact; ++i)
660 if (list[i] == map)
661 goto out_check;
663 else if (undef_map->l_reldeps->act > l_reldepsact)
665 struct link_map **list
666 = &undef_map->l_reldeps->list[0];
667 i = l_reldepsact;
668 l_reldepsact = undef_map->l_reldeps->act;
669 for (; i < l_reldepsact; ++i)
670 if (list[i] == map)
671 goto out_check;
675 else
676 __rtld_lock_lock_recursive (GL(dl_load_lock));
678 /* The object is not yet in the dependency list. Before we add
679 it make sure just one more time the object we are about to
680 reference is still available. There is a brief period in
681 which the object could have been removed since we found the
682 definition. */
683 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
684 while (runp != NULL && runp != map)
685 runp = runp->l_next;
687 if (runp != NULL)
689 /* The object is still available. */
691 /* MAP could have been dlclosed, freed and then some other dlopened
692 library could have the same link_map pointer. */
693 if (map->l_serial != serial)
694 goto out_check;
696 /* Redo the NODELETE check, as when dl_load_lock wasn't held
697 yet this could have changed. */
698 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
699 goto out;
701 /* If the object with the undefined reference cannot be removed ever
702 just make sure the same is true for the object which contains the
703 definition. */
704 if (undef_map->l_type != lt_loaded
705 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
707 map->l_flags_1 |= DF_1_NODELETE;
708 goto out;
711 /* Add the reference now. */
712 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
714 /* Allocate more memory for the dependency list. Since this
715 can never happen during the startup phase we can use
716 `realloc'. */
717 struct link_map_reldeps *newp;
718 unsigned int max
719 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
721 #ifdef RTLD_PREPARE_FOREIGN_CALL
722 RTLD_PREPARE_FOREIGN_CALL;
723 #endif
725 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
726 if (newp == NULL)
728 /* If we didn't manage to allocate memory for the list this is
729 no fatal problem. We simply make sure the referenced object
730 cannot be unloaded. This is semantically the correct
731 behavior. */
732 map->l_flags_1 |= DF_1_NODELETE;
733 goto out;
735 else
737 if (l_reldepsact)
738 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
739 l_reldepsact * sizeof (struct link_map *));
740 newp->list[l_reldepsact] = map;
741 newp->act = l_reldepsact + 1;
742 atomic_write_barrier ();
743 void *old = undef_map->l_reldeps;
744 undef_map->l_reldeps = newp;
745 undef_map->l_reldepsmax = max;
746 if (old)
747 _dl_scope_free (old);
750 else
752 undef_map->l_reldeps->list[l_reldepsact] = map;
753 atomic_write_barrier ();
754 undef_map->l_reldeps->act = l_reldepsact + 1;
757 /* Display information if we are debugging. */
758 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
759 _dl_debug_printf ("\
760 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
761 DSO_FILENAME (map->l_name),
762 map->l_ns,
763 DSO_FILENAME (undef_map->l_name),
764 undef_map->l_ns);
766 else
767 /* Whoa, that was bad luck. We have to search again. */
768 result = -1;
770 out:
771 /* Release the lock. */
772 __rtld_lock_unlock_recursive (GL(dl_load_lock));
774 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
775 THREAD_GSCOPE_SET_FLAG ();
777 return result;
779 out_check:
780 if (map->l_serial != serial)
781 result = -1;
782 goto out;
785 static void
786 internal_function
787 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
788 const ElfW(Sym) **ref, struct sym_val *value,
789 const struct r_found_version *version, int type_class,
790 int protected);
793 /* Search loaded objects' symbol tables for a definition of the symbol
794 UNDEF_NAME, perhaps with a requested version for the symbol.
796 We must never have calls to the audit functions inside this function
797 or in any function which gets called. If this would happen the audit
798 code might create a thread which can throw off all the scope locking. */
799 lookup_t
800 internal_function
801 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
802 const ElfW(Sym) **ref,
803 struct r_scope_elem *symbol_scope[],
804 const struct r_found_version *version,
805 int type_class, int flags, struct link_map *skip_map)
807 const uint_fast32_t new_hash = dl_new_hash (undef_name);
808 unsigned long int old_hash = 0xffffffff;
809 struct sym_val current_value = { NULL, NULL };
810 struct r_scope_elem **scope = symbol_scope;
812 bump_num_relocations ();
814 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
815 is allowed if we look up a versioned symbol. */
816 assert (version == NULL
817 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
818 == 0);
820 size_t i = 0;
821 if (__glibc_unlikely (skip_map != NULL))
822 /* Search the relevant loaded objects for a definition. */
823 while ((*scope)->r_list[i] != skip_map)
824 ++i;
826 /* Search the relevant loaded objects for a definition. */
827 for (size_t start = i; *scope != NULL; start = 0, ++scope)
829 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
830 &current_value, *scope, start, version, flags,
831 skip_map, type_class, undef_map);
832 if (res > 0)
833 break;
835 if (__glibc_unlikely (res < 0) && skip_map == NULL)
837 /* Oh, oh. The file named in the relocation entry does not
838 contain the needed symbol. This code is never reached
839 for unversioned lookups. */
840 assert (version != NULL);
841 const char *reference_name = undef_map ? undef_map->l_name : "";
843 /* XXX We cannot translate the message. */
844 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
845 N_("relocation error"),
846 make_string ("symbol ", undef_name, ", version ",
847 version->name,
848 " not defined in file ",
849 version->filename,
850 " with link time reference",
851 res == -2
852 ? " (no version symbols)" : ""));
853 *ref = NULL;
854 return 0;
858 if (__glibc_unlikely (current_value.s == NULL))
860 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
861 && skip_map == NULL
862 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
864 /* We could find no value for a strong reference. */
865 const char *reference_name = undef_map ? undef_map->l_name : "";
866 const char *versionstr = version ? ", version " : "";
867 const char *versionname = (version && version->name
868 ? version->name : "");
870 /* XXX We cannot translate the message. */
871 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
872 N_("symbol lookup error"),
873 make_string ("undefined symbol: ", undef_name,
874 versionstr, versionname));
876 *ref = NULL;
877 return 0;
880 int protected = (*ref
881 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
882 if (__glibc_unlikely (protected != 0))
884 /* It is very tricky. We need to figure out what value to
885 return for the protected symbol. */
886 if (type_class == ELF_RTYPE_CLASS_PLT)
888 if (current_value.s != NULL && current_value.m != undef_map)
890 current_value.s = *ref;
891 current_value.m = undef_map;
894 else
896 struct sym_val protected_value = { NULL, NULL };
898 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
899 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
900 &protected_value, *scope, i, version, flags,
901 skip_map,
902 (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
903 && ELFW(ST_TYPE) ((*ref)->st_info) == STT_OBJECT
904 && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)
905 ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
906 : ELF_RTYPE_CLASS_PLT, NULL) != 0)
907 break;
909 if (protected_value.s != NULL && protected_value.m != undef_map)
911 current_value.s = *ref;
912 current_value.m = undef_map;
917 /* We have to check whether this would bind UNDEF_MAP to an object
918 in the global scope which was dynamically loaded. In this case
919 we have to prevent the latter from being unloaded unless the
920 UNDEF_MAP object is also unloaded. */
921 if (__glibc_unlikely (current_value.m->l_type == lt_loaded)
922 /* Don't do this for explicit lookups as opposed to implicit
923 runtime lookups. */
924 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
925 /* Add UNDEF_MAP to the dependencies. */
926 && add_dependency (undef_map, current_value.m, flags) < 0)
927 /* Something went wrong. Perhaps the object we tried to reference
928 was just removed. Try finding another definition. */
929 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
930 (flags & DL_LOOKUP_GSCOPE_LOCK)
931 ? undef_map->l_scope : symbol_scope,
932 version, type_class, flags, skip_map);
934 /* The object is used. */
935 if (__glibc_unlikely (current_value.m->l_used == 0))
936 current_value.m->l_used = 1;
938 if (__glibc_unlikely (GLRO(dl_debug_mask)
939 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK)))
940 _dl_debug_bindings (undef_name, undef_map, ref,
941 &current_value, version, type_class, protected);
943 *ref = current_value.s;
944 return LOOKUP_VALUE (current_value.m);
948 /* Cache the location of MAP's hash table. */
950 void
951 internal_function
952 _dl_setup_hash (struct link_map *map)
954 Elf_Symndx *hash;
956 if (__glibc_likely (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
957 + DT_THISPROCNUM + DT_VERSIONTAGNUM
958 + DT_EXTRANUM + DT_VALNUM] != NULL))
960 Elf32_Word *hash32
961 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
962 + DT_THISPROCNUM + DT_VERSIONTAGNUM
963 + DT_EXTRANUM + DT_VALNUM]);
964 map->l_nbuckets = *hash32++;
965 Elf32_Word symbias = *hash32++;
966 Elf32_Word bitmask_nwords = *hash32++;
967 /* Must be a power of two. */
968 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
969 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
970 map->l_gnu_shift = *hash32++;
972 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
973 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
975 map->l_gnu_buckets = hash32;
976 hash32 += map->l_nbuckets;
977 map->l_gnu_chain_zero = hash32 - symbias;
978 return;
981 if (!map->l_info[DT_HASH])
982 return;
983 hash = (void *) D_PTR (map, l_info[DT_HASH]);
985 map->l_nbuckets = *hash++;
986 /* Skip nchain. */
987 hash++;
988 map->l_buckets = hash;
989 hash += map->l_nbuckets;
990 map->l_chain = hash;
994 static void
995 internal_function
996 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
997 const ElfW(Sym) **ref, struct sym_val *value,
998 const struct r_found_version *version, int type_class,
999 int protected)
1001 const char *reference_name = undef_map->l_name;
1003 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
1005 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
1006 DSO_FILENAME (reference_name),
1007 undef_map->l_ns,
1008 DSO_FILENAME (value->m->l_name),
1009 value->m->l_ns,
1010 protected ? "protected" : "normal", undef_name);
1011 if (version)
1012 _dl_debug_printf_c (" [%s]\n", version->name);
1013 else
1014 _dl_debug_printf_c ("\n");
1016 #ifdef SHARED
1017 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
1019 /* ELF_RTYPE_CLASS_XXX must match RTYPE_CLASS_XXX used by prelink with
1020 LD_TRACE_PRELINKING. */
1021 #define RTYPE_CLASS_VALID 8
1022 #define RTYPE_CLASS_PLT (8|1)
1023 #define RTYPE_CLASS_COPY (8|2)
1024 #define RTYPE_CLASS_TLS (8|4)
1025 #if ELF_RTYPE_CLASS_PLT != 0 && ELF_RTYPE_CLASS_PLT != 1
1026 # error ELF_RTYPE_CLASS_PLT must be 0 or 1!
1027 #endif
1028 #if ELF_RTYPE_CLASS_COPY != 0 && ELF_RTYPE_CLASS_COPY != 2
1029 # error ELF_RTYPE_CLASS_COPY must be 0 or 2!
1030 #endif
1031 int conflict = 0;
1032 struct sym_val val = { NULL, NULL };
1034 if ((GLRO(dl_trace_prelink_map) == NULL
1035 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
1036 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
1038 const uint_fast32_t new_hash = dl_new_hash (undef_name);
1039 unsigned long int old_hash = 0xffffffff;
1040 struct unique_sym *saved_entries
1041 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
1043 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
1044 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
1045 undef_map->l_local_scope[0], 0, version, 0, NULL,
1046 type_class, undef_map);
1047 if (val.s != value->s || val.m != value->m)
1048 conflict = 1;
1049 else if (__glibc_unlikely (undef_map->l_symbolic_in_local_scope)
1050 && val.s
1051 && __glibc_unlikely (ELFW(ST_BIND) (val.s->st_info)
1052 == STB_GNU_UNIQUE))
1054 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
1055 contains any DT_SYMBOLIC libraries, unfortunately there
1056 can be conflicts even if the above is equal. As symbol
1057 resolution goes from the last library to the first and
1058 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
1059 library, it would be the one that is looked up. */
1060 struct sym_val val2 = { NULL, NULL };
1061 size_t n;
1062 struct r_scope_elem *scope = undef_map->l_local_scope[0];
1064 for (n = 0; n < scope->r_nlist; n++)
1065 if (scope->r_list[n] == val.m)
1066 break;
1068 for (n++; n < scope->r_nlist; n++)
1069 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
1070 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
1071 &val2,
1072 &scope->r_list[n]->l_symbolic_searchlist,
1073 0, version, 0, NULL, type_class,
1074 undef_map) > 0)
1076 conflict = 1;
1077 val = val2;
1078 break;
1081 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
1084 if (value->s)
1086 /* Keep only ELF_RTYPE_CLASS_PLT and ELF_RTYPE_CLASS_COPY
1087 bits since since prelink only uses them. */
1088 type_class &= ELF_RTYPE_CLASS_PLT | ELF_RTYPE_CLASS_COPY;
1089 if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1090 == STT_TLS))
1091 /* Clear the RTYPE_CLASS_VALID bit in RTYPE_CLASS_TLS. */
1092 type_class = RTYPE_CLASS_TLS & ~RTYPE_CLASS_VALID;
1093 else if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1094 == STT_GNU_IFUNC))
1095 /* Set the RTYPE_CLASS_VALID bit. */
1096 type_class |= RTYPE_CLASS_VALID;
1099 if (conflict
1100 || GLRO(dl_trace_prelink_map) == undef_map
1101 || GLRO(dl_trace_prelink_map) == NULL
1102 || type_class >= 4)
1104 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1105 conflict ? "conflict" : "lookup",
1106 (int) sizeof (ElfW(Addr)) * 2,
1107 (size_t) undef_map->l_map_start,
1108 (int) sizeof (ElfW(Addr)) * 2,
1109 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1110 (int) sizeof (ElfW(Addr)) * 2,
1111 (size_t) (value->s ? value->m->l_map_start : 0),
1112 (int) sizeof (ElfW(Addr)) * 2,
1113 (size_t) (value->s ? value->s->st_value : 0));
1115 if (conflict)
1116 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1117 (int) sizeof (ElfW(Addr)) * 2,
1118 (size_t) (val.s ? val.m->l_map_start : 0),
1119 (int) sizeof (ElfW(Addr)) * 2,
1120 (size_t) (val.s ? val.s->st_value : 0));
1122 _dl_printf ("/%x %s\n", type_class, undef_name);
1125 #endif