]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-lookup.c
__inet6_scopeid_pton: Remove attribute_hidden, internal_function
[thirdparty/glibc.git] / elf / dl-lookup.c
CommitLineData
d66e34cd 1/* Look up a symbol in the loaded objects.
bfff8b1b 2 Copyright (C) 1995-2017 Free Software Foundation, Inc.
afd4eb37 3 This file is part of the GNU C Library.
d66e34cd 4
afd4eb37 5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
d66e34cd 9
afd4eb37
UD
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
41bdb6e2 13 Lesser General Public License for more details.
d66e34cd 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
d66e34cd 18
f671aeab 19#include <alloca.h>
407fe3bb 20#include <libintl.h>
cf197e41 21#include <stdlib.h>
8d6468d0 22#include <string.h>
3db52d94 23#include <unistd.h>
a42195db 24#include <ldsodefs.h>
8f480b4b 25#include <dl-hash.h>
bc9f6000 26#include <dl-machine.h>
4e35ef2c 27#include <sysdep-cancel.h>
ec999b8e 28#include <libc-lock.h>
f1cc669a 29#include <tls.h>
1ec79f26 30#include <atomic.h>
c84142e8 31
a853022c
UD
32#include <assert.h>
33
8e25d1e7
CD
34/* Return nonzero if check_match should consider SYM to fail to match a
35 symbol reference for some machine-specific reason. */
cb4a2928
JM
36#ifndef ELF_MACHINE_SYM_NO_MATCH
37# define ELF_MACHINE_SYM_NO_MATCH(sym) 0
38#endif
39
b0982c4a 40#define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
266180eb 41
714a562f 42
84384f5b
UD
43struct sym_val
44 {
84384f5b 45 const ElfW(Sym) *s;
0c367d92 46 struct link_map *m;
84384f5b
UD
47 };
48
49
1fb05e3d
UD
50#define make_string(string, rest...) \
51 ({ \
52 const char *all[] = { string, ## rest }; \
53 size_t len, cnt; \
54 char *result, *cp; \
55 \
56 len = 1; \
57 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
58 len += strlen (all[cnt]); \
59 \
60 cp = result = alloca (len); \
61 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
da832465 62 cp = __stpcpy (cp, all[cnt]); \
1fb05e3d
UD
63 \
64 result; \
65 })
66
8352b484 67/* Statistics function. */
d6b5d570
UD
68#ifdef SHARED
69# define bump_num_relocations() ++GL(dl_num_relocations)
be4b5a95 70#else
d6b5d570 71# define bump_num_relocations() ((void) 0)
be4b5a95
UD
72#endif
73
8e25d1e7
CD
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. */
80static const ElfW(Sym) *
81check_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)
92{
93 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
94 assert (ELF_RTYPE_CLASS_PLT == 1);
cc7d0447 95 if (__glibc_unlikely ((sym->st_value == 0 /* No value. */
8e25d1e7
CD
96 && stt != STT_TLS)
97 || ELF_MACHINE_SYM_NO_MATCH (sym)
cc7d0447 98 || (type_class & (sym->st_shndx == SHN_UNDEF))))
8e25d1e7
CD
99 return NULL;
100
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;
109
110 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
111 /* Not the symbol we are looking for. */
112 return NULL;
113
114 const ElfW(Half) *verstab = map->l_versyms;
115 if (version != NULL)
116 {
117 if (__glibc_unlikely (verstab == NULL))
118 {
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.
123
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));
129
130 /* Otherwise we accept the symbol. */
131 }
132 else
133 {
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;
143 }
144 }
145 else
146 {
147 /* No specific version is selected. There are two ways we
148 can got here:
149
150 - a binary which does not include versioning information
151 is loaded
152
153 - dlsym() instead of dlvsym() is used to get a symbol which
154 might exist in more than one form
155
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.
159
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)
166 {
167 if ((verstab[symidx] & 0x7fff)
168 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
169 {
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;
175
176 return NULL;
177 }
178 }
179 }
180
181 /* There cannot be another entry for this symbol so stop here. */
182 return sym;
183}
184
7b8fb2b8
RM
185/* Utility function for do_lookup_unique. Add a symbol to TABLE. */
186static void
187enter_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)
190{
191 size_t idx = hash % size;
192 size_t hash2 = 1 + hash % (size - 2);
193 while (table[idx].name != NULL)
194 {
195 idx += hash2;
196 if (idx >= size)
197 idx -= size;
198 }
199
200 table[idx].hashval = hash;
201 table[idx].name = name;
202 table[idx].sym = sym;
203 table[idx].map = map;
204}
205
f393b4aa
WN
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. */
209static void
210do_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)
214{
7b8fb2b8
RM
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. */
f393b4aa
WN
218
219 struct unique_sym_table *tab
220 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
221
222 __rtld_lock_lock_recursive (tab->lock);
223
224 struct unique_sym *entries = tab->entries;
225 size_t size = tab->size;
226 if (entries != NULL)
227 {
228 size_t idx = new_hash % size;
229 size_t hash2 = 1 + new_hash % (size - 2);
230 while (1)
231 {
232 if (entries[idx].hashval == new_hash
233 && strcmp (entries[idx].name, undef_name) == 0)
234 {
235 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
236 {
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;
242 }
243 else
244 {
245 result->s = entries[idx].sym;
246 result->m = (struct link_map *) entries[idx].map;
247 }
248 __rtld_lock_unlock_recursive (tab->lock);
249 return;
250 }
251
252 if (entries[idx].name == NULL)
253 break;
254
255 idx += hash2;
256 if (idx >= size)
257 idx -= size;
258 }
259
260 if (size * 3 <= tab->n_elements * 4)
261 {
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)
271 {
272 nomem:
273 __rtld_lock_unlock_recursive (tab->lock);
274 _dl_fatal_printf ("out of memory\n");
275 }
276
277 for (idx = 0; idx < size; ++idx)
278 if (entries[idx].name != NULL)
7b8fb2b8
RM
279 enter_unique_sym (newentries, newsize, entries[idx].hashval,
280 entries[idx].name, entries[idx].sym,
281 entries[idx].map);
f393b4aa
WN
282
283 tab->free (entries);
284 tab->size = newsize;
285 size = newsize;
286 entries = tab->entries = newentries;
287 tab->free = free;
288 }
289 }
290 else
291 {
292#ifdef RTLD_CHECK_FOREIGN_CALL
293 /* This must not happen during runtime relocations. */
294 assert (!RTLD_CHECK_FOREIGN_CALL);
295#endif
296
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))
304 {
305 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
306 goto success;
307 }
308#endif
309
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;
315
316 tab->entries = entries;
317 tab->size = size;
318 tab->free = free;
319 }
320
321 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
7b8fb2b8 322 enter_unique_sym (entries, size, new_hash, strtab + sym->st_name, ref,
f393b4aa
WN
323 undef_map);
324 else
325 {
7b8fb2b8
RM
326 enter_unique_sym (entries, size,
327 new_hash, strtab + sym->st_name, sym, map);
f393b4aa
WN
328
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;
333 }
334 ++tab->n_elements;
335
336#ifdef SHARED
337 success:
338#endif
339 __rtld_lock_unlock_recursive (tab->lock);
340
341 result->s = sym;
342 result->m = (struct link_map *) map;
343}
8352b484 344
786b74f4
UD
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. */
348static int
349__attribute_noinline__
350do_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)
355{
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;
363
364 do
365 {
786b74f4
UD
366 const struct link_map *map = list[i]->l_real;
367
368 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
369 if (map == skip)
370 continue;
371
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;
375
376 /* Do not look into objects which are going to be removed. */
377 if (map->l_removed)
378 continue;
379
380 /* Print some debugging info if wanted. */
a1ffb40e 381 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
786b74f4 382 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
b9375348 383 undef_name, DSO_FILENAME (map->l_name),
786b74f4
UD
384 map->l_ns);
385
386 /* If the hash table is empty there is nothing to do here. */
387 if (map->l_nbuckets == 0)
388 continue;
389
f6488e2b
WN
390 Elf_Symndx symidx;
391 int num_versions = 0;
392 const ElfW(Sym) *versioned_sym = NULL;
393
786b74f4
UD
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]);
397
786b74f4
UD
398 const ElfW(Sym) *sym;
399 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
a1ffb40e 400 if (__glibc_likely (bitmask != NULL))
786b74f4
UD
401 {
402 ElfW(Addr) bitmask_word
403 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
404 & map->l_gnu_bitmask_idxbits];
405
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));
409
cc7d0447
WN
410 if (__glibc_unlikely ((bitmask_word >> hashbit1)
411 & (bitmask_word >> hashbit2) & 1))
786b74f4
UD
412 {
413 Elf32_Word bucket = map->l_gnu_buckets[new_hash
414 % map->l_nbuckets];
415 if (bucket != 0)
416 {
417 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
418
419 do
420 if (((*hasharr ^ new_hash) >> 1) == 0)
421 {
422 symidx = hasharr - map->l_gnu_chain_zero;
8e25d1e7
CD
423 sym = check_match (undef_name, ref, version, flags,
424 type_class, &symtab[symidx], symidx,
425 strtab, map, &versioned_sym,
426 &num_versions);
786b74f4
UD
427 if (sym != NULL)
428 goto found_it;
429 }
430 while ((*hasharr++ & 1u) == 0);
431 }
432 }
433 /* No symbol found. */
434 symidx = SHN_UNDEF;
435 }
436 else
437 {
438 if (*old_hash == 0xffffffff)
439 *old_hash = _dl_elf_hash (undef_name);
440
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])
447 {
8e25d1e7
CD
448 sym = check_match (undef_name, ref, version, flags,
449 type_class, &symtab[symidx], symidx,
450 strtab, map, &versioned_sym,
451 &num_versions);
786b74f4
UD
452 if (sym != NULL)
453 goto found_it;
454 }
455 }
456
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;
462
463 if (sym != NULL)
464 {
465 found_it:
62da1e3b
L
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)
473 {
474 const ElfW(Sym) *s;
475 unsigned int i;
476
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)
481 {
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);
486
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)
490 {
491 s = &symtab[ELFW(R_SYM) (rela->r_info)];
492 if (!strcmp (strtab + s->st_name, undef_name))
493 goto skip;
494 }
495 }
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)
501 {
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);
506
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)
510 {
511 s = &symtab[ELFW(R_SYM) (rel->r_info)];
512 if (!strcmp (strtab + s->st_name, undef_name))
513 goto skip;
514 }
515 }
516#endif
517 }
518
b6084a95
MR
519 /* Hidden and internal symbols are local, ignore them. */
520 if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym)))
521 goto skip;
522
cc7d0447 523 switch (ELFW(ST_BIND) (sym->st_info))
786b74f4
UD
524 {
525 case STB_WEAK:
526 /* Weak definition. Use this value if we don't find another. */
a1ffb40e 527 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
786b74f4
UD
528 {
529 if (! result->s)
530 {
531 result->s = sym;
532 result->m = (struct link_map *) map;
533 }
534 break;
535 }
536 /* FALLTHROUGH */
537 case STB_GLOBAL:
786b74f4
UD
538 /* Global definition. Just what we need. */
539 result->s = sym;
540 result->m = (struct link_map *) map;
541 return 1;
542
543 case STB_GNU_UNIQUE:;
f393b4aa
WN
544 do_lookup_unique (undef_name, new_hash, map, result, type_class,
545 sym, strtab, ref, undef_map);
546 return 1;
786b74f4
UD
547
548 default:
549 /* Local symbols are ignored. */
550 break;
551 }
552 }
553
62da1e3b 554skip:
786b74f4
UD
555 /* If this current map is the one mentioned in the verneed entry
556 and we have not found a weak entry, it is a bug. */
557 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
cc7d0447 558 && __glibc_unlikely (_dl_name_match_p (version->filename, map)))
786b74f4
UD
559 return -1;
560 }
561 while (++i < n);
562
563 /* We have not found anything until now. */
564 return 0;
565}
84384f5b 566
84384f5b 567
871b9158
UD
568static uint_fast32_t
569dl_new_hash (const char *s)
570{
571 uint_fast32_t h = 5381;
572 for (unsigned char c = *s; c != '\0'; c = *++s)
573 h = h * 33 + c;
574 return h & 0xffffffff;
575}
576
577
cf197e41
UD
578/* Add extra dependency on MAP to UNDEF_MAP. */
579static int
80d9c5f0 580internal_function
b90395e6 581add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
cf197e41 582{
c4bb124a 583 struct link_map *runp;
cf197e41
UD
584 unsigned int i;
585 int result = 0;
586
aff4519d
UD
587 /* Avoid self-references and references to objects which cannot be
588 unloaded anyway. */
c4bb124a
UD
589 if (undef_map == map)
590 return 0;
591
385b4cf4
UD
592 /* Avoid references to objects which cannot be unloaded anyway. */
593 assert (map->l_type == lt_loaded);
594 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
595 return 0;
596
597 struct link_map_reldeps *l_reldeps
598 = atomic_forced_read (undef_map->l_reldeps);
599
600 /* Make sure l_reldeps is read before l_initfini. */
601 atomic_read_barrier ();
602
603 /* Determine whether UNDEF_MAP already has a reference to MAP. First
604 look in the normal dependencies. */
605 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
606 if (l_initfini != NULL)
607 {
608 for (i = 0; l_initfini[i] != NULL; ++i)
609 if (l_initfini[i] == map)
610 return 0;
611 }
612
613 /* No normal dependency. See whether we already had to add it
614 to the special list of dynamic dependencies. */
615 unsigned int l_reldepsact = 0;
616 if (l_reldeps != NULL)
617 {
618 struct link_map **list = &l_reldeps->list[0];
619 l_reldepsact = l_reldeps->act;
620 for (i = 0; i < l_reldepsact; ++i)
621 if (list[i] == map)
622 return 0;
623 }
624
b90395e6 625 /* Save serial number of the target MAP. */
385b4cf4 626 unsigned long long serial = map->l_serial;
aff4519d 627
b90395e6 628 /* Make sure nobody can unload the object while we are at it. */
a1ffb40e 629 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
aff4519d 630 {
b90395e6
UD
631 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
632 here, that can result in ABBA deadlock. */
633 THREAD_GSCOPE_RESET_FLAG ();
634 __rtld_lock_lock_recursive (GL(dl_load_lock));
b90395e6
UD
635 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
636 it can e.g. point to unallocated memory. So avoid the optimizer
637 treating the above read from MAP->l_serial as ensurance it
638 can safely dereference it. */
639 map = atomic_forced_read (map);
b90395e6 640
385b4cf4
UD
641 /* From this point on it is unsafe to dereference MAP, until it
642 has been found in one of the lists. */
cf197e41 643
385b4cf4
UD
644 /* Redo the l_initfini check in case undef_map's l_initfini
645 changed in the mean time. */
646 if (undef_map->l_initfini != l_initfini
647 && undef_map->l_initfini != NULL)
648 {
649 l_initfini = undef_map->l_initfini;
650 for (i = 0; l_initfini[i] != NULL; ++i)
651 if (l_initfini[i] == map)
652 goto out_check;
653 }
cf197e41 654
385b4cf4
UD
655 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
656 the mean time. */
657 if (undef_map->l_reldeps != NULL)
658 {
659 if (undef_map->l_reldeps != l_reldeps)
660 {
661 struct link_map **list = &undef_map->l_reldeps->list[0];
662 l_reldepsact = undef_map->l_reldeps->act;
663 for (i = 0; i < l_reldepsact; ++i)
664 if (list[i] == map)
665 goto out_check;
666 }
667 else if (undef_map->l_reldeps->act > l_reldepsact)
668 {
669 struct link_map **list
670 = &undef_map->l_reldeps->list[0];
671 i = l_reldepsact;
672 l_reldepsact = undef_map->l_reldeps->act;
673 for (; i < l_reldepsact; ++i)
674 if (list[i] == map)
675 goto out_check;
676 }
677 }
c4bb124a 678 }
385b4cf4
UD
679 else
680 __rtld_lock_lock_recursive (GL(dl_load_lock));
c4bb124a
UD
681
682 /* The object is not yet in the dependency list. Before we add
683 it make sure just one more time the object we are about to
684 reference is still available. There is a brief period in
685 which the object could have been removed since we found the
686 definition. */
c0f62c56 687 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
c4bb124a
UD
688 while (runp != NULL && runp != map)
689 runp = runp->l_next;
690
691 if (runp != NULL)
692 {
b90395e6
UD
693 /* The object is still available. */
694
695 /* MAP could have been dlclosed, freed and then some other dlopened
696 library could have the same link_map pointer. */
697 if (map->l_serial != serial)
698 goto out_check;
699
385b4cf4
UD
700 /* Redo the NODELETE check, as when dl_load_lock wasn't held
701 yet this could have changed. */
715899d1 702 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
b90395e6
UD
703 goto out;
704
705 /* If the object with the undefined reference cannot be removed ever
706 just make sure the same is true for the object which contains the
707 definition. */
708 if (undef_map->l_type != lt_loaded
709 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
710 {
711 map->l_flags_1 |= DF_1_NODELETE;
712 goto out;
713 }
714
715 /* Add the reference now. */
a1ffb40e 716 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
cf197e41 717 {
c4bb124a
UD
718 /* Allocate more memory for the dependency list. Since this
719 can never happen during the startup phase we can use
720 `realloc'. */
385b4cf4
UD
721 struct link_map_reldeps *newp;
722 unsigned int max
723 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
724
b48a267b
UD
725#ifdef RTLD_PREPARE_FOREIGN_CALL
726 RTLD_PREPARE_FOREIGN_CALL;
727#endif
728
385b4cf4
UD
729 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
730 if (newp == NULL)
731 {
732 /* If we didn't manage to allocate memory for the list this is
733 no fatal problem. We simply make sure the referenced object
734 cannot be unloaded. This is semantically the correct
735 behavior. */
736 map->l_flags_1 |= DF_1_NODELETE;
737 goto out;
738 }
cf197e41 739 else
385b4cf4
UD
740 {
741 if (l_reldepsact)
742 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
743 l_reldepsact * sizeof (struct link_map *));
744 newp->list[l_reldepsact] = map;
745 newp->act = l_reldepsact + 1;
746 atomic_write_barrier ();
747 void *old = undef_map->l_reldeps;
748 undef_map->l_reldeps = newp;
749 undef_map->l_reldepsmax = max;
750 if (old)
751 _dl_scope_free (old);
752 }
cf197e41 753 }
715899d1 754 else
385b4cf4
UD
755 {
756 undef_map->l_reldeps->list[l_reldepsact] = map;
757 atomic_write_barrier ();
758 undef_map->l_reldeps->act = l_reldepsact + 1;
759 }
c4bb124a 760
c4bb124a 761 /* Display information if we are debugging. */
a1ffb40e 762 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
154d10bd 763 _dl_debug_printf ("\
c0f62c56 764\nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
b9375348 765 DSO_FILENAME (map->l_name),
c0f62c56 766 map->l_ns,
b9375348 767 DSO_FILENAME (undef_map->l_name),
c0f62c56 768 undef_map->l_ns);
cf197e41 769 }
c4bb124a
UD
770 else
771 /* Whoa, that was bad luck. We have to search again. */
772 result = -1;
cf197e41 773
c4bb124a 774 out:
cf197e41 775 /* Release the lock. */
d3c9f895 776 __rtld_lock_unlock_recursive (GL(dl_load_lock));
cf197e41 777
a1ffb40e 778 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
385b4cf4
UD
779 THREAD_GSCOPE_SET_FLAG ();
780
cf197e41 781 return result;
b90395e6
UD
782
783 out_check:
784 if (map->l_serial != serial)
785 result = -1;
786 goto out;
cf197e41
UD
787}
788
32e6df36
UD
789static void
790internal_function
791_dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
c0a777e8 792 const ElfW(Sym) **ref, struct sym_val *value,
7969407a
UD
793 const struct r_found_version *version, int type_class,
794 int protected);
647eb037 795
84384f5b 796
bdf4a4f1 797/* Search loaded objects' symbol tables for a definition of the symbol
609cf614
UD
798 UNDEF_NAME, perhaps with a requested version for the symbol.
799
800 We must never have calls to the audit functions inside this function
801 or in any function which gets called. If this would happen the audit
802 code might create a thread which can throw off all the scope locking. */
c0282c06 803lookup_t
d0fc4041 804internal_function
bdf4a4f1
UD
805_dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
806 const ElfW(Sym) **ref,
807 struct r_scope_elem *symbol_scope[],
808 const struct r_found_version *version,
809 int type_class, int flags, struct link_map *skip_map)
84384f5b 810{
871b9158
UD
811 const uint_fast32_t new_hash = dl_new_hash (undef_name);
812 unsigned long int old_hash = 0xffffffff;
0c367d92 813 struct sym_val current_value = { NULL, NULL };
bdf4a4f1 814 struct r_scope_elem **scope = symbol_scope;
84384f5b 815
be4b5a95 816 bump_num_relocations ();
48f6496e 817
b90395e6
UD
818 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
819 is allowed if we look up a versioned symbol. */
820 assert (version == NULL
821 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
822 == 0);
c84142e8 823
bdf4a4f1 824 size_t i = 0;
a1ffb40e 825 if (__glibc_unlikely (skip_map != NULL))
3c457089
UD
826 /* Search the relevant loaded objects for a definition. */
827 while ((*scope)->r_list[i] != skip_map)
828 ++i;
32e6df36 829
c84142e8 830 /* Search the relevant loaded objects for a definition. */
bdf4a4f1 831 for (size_t start = i; *scope != NULL; start = 0, ++scope)
1fb05e3d 832 {
871b9158
UD
833 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
834 &current_value, *scope, start, version, flags,
415ac3df 835 skip_map, type_class, undef_map);
1fb05e3d 836 if (res > 0)
78575a84 837 break;
1fb05e3d 838
cc7d0447 839 if (__glibc_unlikely (res < 0) && skip_map == NULL)
3f933dc2
UD
840 {
841 /* Oh, oh. The file named in the relocation entry does not
bdf4a4f1
UD
842 contain the needed symbol. This code is never reached
843 for unversioned lookups. */
844 assert (version != NULL);
50727aa7 845 const char *reference_name = undef_map ? undef_map->l_name : "";
c90b5d28 846
8e17ea58 847 /* XXX We cannot translate the message. */
b9375348 848 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
407fe3bb 849 N_("relocation error"),
3f933dc2
UD
850 make_string ("symbol ", undef_name, ", version ",
851 version->name,
852 " not defined in file ",
853 version->filename,
854 " with link time reference",
855 res == -2
856 ? " (no version symbols)" : ""));
857 *ref = NULL;
858 return 0;
859 }
1fb05e3d 860 }
c84142e8 861
a1ffb40e 862 if (__glibc_unlikely (current_value.s == NULL))
0c367d92 863 {
bdf4a4f1 864 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
ff9f1c5f 865 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
c90b5d28
UD
866 {
867 /* We could find no value for a strong reference. */
9363dbb8 868 const char *reference_name = undef_map ? undef_map->l_name : "";
bdf4a4f1
UD
869 const char *versionstr = version ? ", version " : "";
870 const char *versionname = (version && version->name
871 ? version->name : "");
c90b5d28
UD
872
873 /* XXX We cannot translate the message. */
b9375348 874 _dl_signal_cerror (0, DSO_FILENAME (reference_name),
24a07b1f 875 N_("symbol lookup error"),
d6d06225 876 make_string ("undefined symbol: ", undef_name,
bdf4a4f1 877 versionstr, versionname));
c90b5d28 878 }
0c367d92
UD
879 *ref = NULL;
880 return 0;
881 }
882
bdf4a4f1
UD
883 int protected = (*ref
884 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
a1ffb40e 885 if (__glibc_unlikely (protected != 0))
6aa29abe 886 {
78575a84 887 /* It is very tricky. We need to figure out what value to
2af63968 888 return for the protected symbol. */
697119d6 889 if (type_class == ELF_RTYPE_CLASS_PLT)
6aa29abe 890 {
697119d6
UD
891 if (current_value.s != NULL && current_value.m != undef_map)
892 {
893 current_value.s = *ref;
894 current_value.m = undef_map;
895 }
896 }
897 else
898 {
899 struct sym_val protected_value = { NULL, NULL };
900
9363dbb8 901 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
871b9158
UD
902 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
903 &protected_value, *scope, i, version, flags,
62da1e3b
L
904 skip_map,
905 (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
906 && ELFW(ST_TYPE) ((*ref)->st_info) == STT_OBJECT
907 && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)
908 ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
909 : ELF_RTYPE_CLASS_PLT, NULL) != 0)
697119d6
UD
910 break;
911
9363dbb8 912 if (protected_value.s != NULL && protected_value.m != undef_map)
697119d6
UD
913 {
914 current_value.s = *ref;
915 current_value.m = undef_map;
916 }
6aa29abe 917 }
6aa29abe 918 }
32e6df36 919
78575a84
UD
920 /* We have to check whether this would bind UNDEF_MAP to an object
921 in the global scope which was dynamically loaded. In this case
922 we have to prevent the latter from being unloaded unless the
923 UNDEF_MAP object is also unloaded. */
cc7d0447 924 if (__glibc_unlikely (current_value.m->l_type == lt_loaded)
78575a84
UD
925 /* Don't do this for explicit lookups as opposed to implicit
926 runtime lookups. */
bdf4a4f1 927 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
78575a84 928 /* Add UNDEF_MAP to the dependencies. */
b90395e6 929 && add_dependency (undef_map, current_value.m, flags) < 0)
78575a84
UD
930 /* Something went wrong. Perhaps the object we tried to reference
931 was just removed. Try finding another definition. */
b90395e6
UD
932 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
933 (flags & DL_LOOKUP_GSCOPE_LOCK)
934 ? undef_map->l_scope : symbol_scope,
e4eb675d 935 version, type_class, flags, skip_map);
78575a84 936
7a11603d 937 /* The object is used. */
a1ffb40e 938 if (__glibc_unlikely (current_value.m->l_used == 0))
2af63968 939 current_value.m->l_used = 1;
7a11603d 940
cc7d0447
WN
941 if (__glibc_unlikely (GLRO(dl_debug_mask)
942 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK)))
c0a777e8 943 _dl_debug_bindings (undef_name, undef_map, ref,
32e6df36
UD
944 &current_value, version, type_class, protected);
945
946 *ref = current_value.s;
947 return LOOKUP_VALUE (current_value.m);
c84142e8
UD
948}
949
950
d66e34cd
RM
951/* Cache the location of MAP's hash table. */
952
953void
d0fc4041 954internal_function
d66e34cd
RM
955_dl_setup_hash (struct link_map *map)
956{
a1eca9f3 957 Elf_Symndx *hash;
f41c8091 958
cc7d0447 959 if (__glibc_likely (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
2af63968 960 + DT_THISPROCNUM + DT_VERSIONTAGNUM
cc7d0447 961 + DT_EXTRANUM + DT_VALNUM] != NULL))
871b9158
UD
962 {
963 Elf32_Word *hash32
964 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
965 + DT_THISPROCNUM + DT_VERSIONTAGNUM
966 + DT_EXTRANUM + DT_VALNUM]);
967 map->l_nbuckets = *hash32++;
968 Elf32_Word symbias = *hash32++;
969 Elf32_Word bitmask_nwords = *hash32++;
970 /* Must be a power of two. */
971 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
972 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
973 map->l_gnu_shift = *hash32++;
974
975 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
976 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
977
978 map->l_gnu_buckets = hash32;
979 hash32 += map->l_nbuckets;
980 map->l_gnu_chain_zero = hash32 - symbias;
981 return;
982 }
983
f41c8091
UD
984 if (!map->l_info[DT_HASH])
985 return;
9a88a2d7 986 hash = (void *) D_PTR (map, l_info[DT_HASH]);
f41c8091 987
d66e34cd 988 map->l_nbuckets = *hash++;
1bc33071
UD
989 /* Skip nchain. */
990 hash++;
d66e34cd
RM
991 map->l_buckets = hash;
992 hash += map->l_nbuckets;
993 map->l_chain = hash;
994}
80d9c5f0 995
f9f2a150 996
32e6df36
UD
997static void
998internal_function
999_dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
c0a777e8 1000 const ElfW(Sym) **ref, struct sym_val *value,
f9f2a150
UD
1001 const struct r_found_version *version, int type_class,
1002 int protected)
32e6df36
UD
1003{
1004 const char *reference_name = undef_map->l_name;
1005
afdca0f2 1006 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
32e6df36 1007 {
21e2d3a4 1008 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
b9375348 1009 DSO_FILENAME (reference_name),
21e2d3a4 1010 undef_map->l_ns,
b9375348 1011 DSO_FILENAME (value->m->l_name),
21e2d3a4 1012 value->m->l_ns,
154d10bd 1013 protected ? "protected" : "normal", undef_name);
32e6df36
UD
1014 if (version)
1015 _dl_debug_printf_c (" [%s]\n", version->name);
1016 else
1017 _dl_debug_printf_c ("\n");
1018 }
1019#ifdef SHARED
afdca0f2 1020 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
32e6df36 1021 {
f3d18efb
L
1022/* ELF_RTYPE_CLASS_XXX must match RTYPE_CLASS_XXX used by prelink with
1023 LD_TRACE_PRELINKING. */
1024#define RTYPE_CLASS_VALID 8
1025#define RTYPE_CLASS_PLT (8|1)
1026#define RTYPE_CLASS_COPY (8|2)
1027#define RTYPE_CLASS_TLS (8|4)
1028#if ELF_RTYPE_CLASS_PLT != 0 && ELF_RTYPE_CLASS_PLT != 1
1029# error ELF_RTYPE_CLASS_PLT must be 0 or 1!
1030#endif
1031#if ELF_RTYPE_CLASS_COPY != 0 && ELF_RTYPE_CLASS_COPY != 2
1032# error ELF_RTYPE_CLASS_COPY must be 0 or 2!
1033#endif
32e6df36
UD
1034 int conflict = 0;
1035 struct sym_val val = { NULL, NULL };
1036
afdca0f2 1037 if ((GLRO(dl_trace_prelink_map) == NULL
c0f62c56
UD
1038 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
1039 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
32e6df36 1040 {
871b9158
UD
1041 const uint_fast32_t new_hash = dl_new_hash (undef_name);
1042 unsigned long int old_hash = 0xffffffff;
4ad43b62
UD
1043 struct unique_sym *saved_entries
1044 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
32e6df36 1045
4ad43b62 1046 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
871b9158 1047 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
bdf4a4f1 1048 undef_map->l_local_scope[0], 0, version, 0, NULL,
415ac3df 1049 type_class, undef_map);
32e6df36
UD
1050 if (val.s != value->s || val.m != value->m)
1051 conflict = 1;
cc7d0447 1052 else if (__glibc_unlikely (undef_map->l_symbolic_in_local_scope)
4ad43b62 1053 && val.s
cc7d0447
WN
1054 && __glibc_unlikely (ELFW(ST_BIND) (val.s->st_info)
1055 == STB_GNU_UNIQUE))
4ad43b62
UD
1056 {
1057 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
1058 contains any DT_SYMBOLIC libraries, unfortunately there
1059 can be conflicts even if the above is equal. As symbol
1060 resolution goes from the last library to the first and
1061 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
1062 library, it would be the one that is looked up. */
1063 struct sym_val val2 = { NULL, NULL };
1064 size_t n;
1065 struct r_scope_elem *scope = undef_map->l_local_scope[0];
1066
1067 for (n = 0; n < scope->r_nlist; n++)
1068 if (scope->r_list[n] == val.m)
1069 break;
1070
1071 for (n++; n < scope->r_nlist; n++)
1072 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
1073 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
1074 &val2,
1075 &scope->r_list[n]->l_symbolic_searchlist,
1076 0, version, 0, NULL, type_class,
1077 undef_map) > 0)
1078 {
1079 conflict = 1;
1080 val = val2;
1081 break;
1082 }
1083 }
1084 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
32e6df36
UD
1085 }
1086
02125962
JJ
1087 if (value->s)
1088 {
f3d18efb
L
1089 /* Keep only ELF_RTYPE_CLASS_PLT and ELF_RTYPE_CLASS_COPY
1090 bits since since prelink only uses them. */
1091 type_class &= ELF_RTYPE_CLASS_PLT | ELF_RTYPE_CLASS_COPY;
cc7d0447
WN
1092 if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1093 == STT_TLS))
f3d18efb
L
1094 /* Clear the RTYPE_CLASS_VALID bit in RTYPE_CLASS_TLS. */
1095 type_class = RTYPE_CLASS_TLS & ~RTYPE_CLASS_VALID;
cc7d0447
WN
1096 else if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1097 == STT_GNU_IFUNC))
f3d18efb
L
1098 /* Set the RTYPE_CLASS_VALID bit. */
1099 type_class |= RTYPE_CLASS_VALID;
02125962 1100 }
1d0ad773 1101
32e6df36 1102 if (conflict
afdca0f2
UD
1103 || GLRO(dl_trace_prelink_map) == undef_map
1104 || GLRO(dl_trace_prelink_map) == NULL
02125962 1105 || type_class >= 4)
32e6df36
UD
1106 {
1107 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1108 conflict ? "conflict" : "lookup",
32e6df36 1109 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1110 (size_t) undef_map->l_map_start,
32e6df36 1111 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1112 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
32e6df36 1113 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab
UD
1114 (size_t) (value->s ? value->m->l_map_start : 0),
1115 (int) sizeof (ElfW(Addr)) * 2,
1116 (size_t) (value->s ? value->s->st_value : 0));
32e6df36
UD
1117
1118 if (conflict)
1119 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1120 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1121 (size_t) (val.s ? val.m->l_map_start : 0),
32e6df36 1122 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1123 (size_t) (val.s ? val.s->st_value : 0));
32e6df36
UD
1124
1125 _dl_printf ("/%x %s\n", type_class, undef_name);
1126 }
1127 }
1128#endif
1129}