]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-lookup.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / elf / dl-lookup.c
CommitLineData
d66e34cd 1/* Look up a symbol in the loaded objects.
04277e02 2 Copyright (C) 1995-2019 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
8352b484 50/* Statistics function. */
d6b5d570
UD
51#ifdef SHARED
52# define bump_num_relocations() ++GL(dl_num_relocations)
be4b5a95 53#else
d6b5d570 54# define bump_num_relocations() ((void) 0)
be4b5a95
UD
55#endif
56
8e25d1e7
CD
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. */
63static const ElfW(Sym) *
64check_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)
75{
76 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
77 assert (ELF_RTYPE_CLASS_PLT == 1);
cc7d0447 78 if (__glibc_unlikely ((sym->st_value == 0 /* No value. */
bac15a72 79 && sym->st_shndx != SHN_ABS
8e25d1e7
CD
80 && stt != STT_TLS)
81 || ELF_MACHINE_SYM_NO_MATCH (sym)
cc7d0447 82 || (type_class & (sym->st_shndx == SHN_UNDEF))))
8e25d1e7
CD
83 return NULL;
84
85 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
86 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
87 code/data definitions. */
88#define ALLOWED_STT \
89 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
90 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
91 if (__glibc_unlikely (((1 << stt) & ALLOWED_STT) == 0))
92 return NULL;
93
94 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
95 /* Not the symbol we are looking for. */
96 return NULL;
97
98 const ElfW(Half) *verstab = map->l_versyms;
99 if (version != NULL)
100 {
101 if (__glibc_unlikely (verstab == NULL))
102 {
103 /* We need a versioned symbol but haven't found any. If
104 this is the object which is referenced in the verneed
105 entry it is a bug in the library since a symbol must
106 not simply disappear.
107
108 It would also be a bug in the object since it means that
109 the list of required versions is incomplete and so the
110 tests in dl-version.c haven't found a problem.*/
111 assert (version->filename == NULL
112 || ! _dl_name_match_p (version->filename, map));
113
114 /* Otherwise we accept the symbol. */
115 }
116 else
117 {
118 /* We can match the version information or use the
119 default one if it is not hidden. */
120 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
121 if ((map->l_versions[ndx].hash != version->hash
122 || strcmp (map->l_versions[ndx].name, version->name))
123 && (version->hidden || map->l_versions[ndx].hash
124 || (verstab[symidx] & 0x8000)))
125 /* It's not the version we want. */
126 return NULL;
127 }
128 }
129 else
130 {
131 /* No specific version is selected. There are two ways we
132 can got here:
133
134 - a binary which does not include versioning information
135 is loaded
136
137 - dlsym() instead of dlvsym() is used to get a symbol which
138 might exist in more than one form
139
140 If the library does not provide symbol version information
141 there is no problem at all: we simply use the symbol if it
142 is defined.
143
144 These two lookups need to be handled differently if the
145 library defines versions. In the case of the old
146 unversioned application the oldest (default) version
147 should be used. In case of a dlsym() call the latest and
148 public interface should be returned. */
149 if (verstab != NULL)
150 {
151 if ((verstab[symidx] & 0x7fff)
152 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
153 {
154 /* Don't accept hidden symbols. */
155 if ((verstab[symidx] & 0x8000) == 0
156 && (*num_versions)++ == 0)
157 /* No version so far. */
158 *versioned_sym = sym;
159
160 return NULL;
161 }
162 }
163 }
164
165 /* There cannot be another entry for this symbol so stop here. */
166 return sym;
167}
168
7b8fb2b8
RM
169/* Utility function for do_lookup_unique. Add a symbol to TABLE. */
170static void
171enter_unique_sym (struct unique_sym *table, size_t size,
172 unsigned int hash, const char *name,
173 const ElfW(Sym) *sym, const struct link_map *map)
174{
175 size_t idx = hash % size;
176 size_t hash2 = 1 + hash % (size - 2);
177 while (table[idx].name != NULL)
178 {
179 idx += hash2;
180 if (idx >= size)
181 idx -= size;
182 }
183
184 table[idx].hashval = hash;
185 table[idx].name = name;
186 table[idx].sym = sym;
187 table[idx].map = map;
188}
189
f393b4aa
WN
190/* Utility function for do_lookup_x. Lookup an STB_GNU_UNIQUE symbol
191 in the unique symbol table, creating a new entry if necessary.
192 Return the matching symbol in RESULT. */
193static void
194do_lookup_unique (const char *undef_name, uint_fast32_t new_hash,
195 const struct link_map *map, struct sym_val *result,
196 int type_class, const ElfW(Sym) *sym, const char *strtab,
197 const ElfW(Sym) *ref, const struct link_map *undef_map)
198{
7b8fb2b8
RM
199 /* We have to determine whether we already found a symbol with this
200 name before. If not then we have to add it to the search table.
201 If we already found a definition we have to use it. */
f393b4aa
WN
202
203 struct unique_sym_table *tab
204 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
205
206 __rtld_lock_lock_recursive (tab->lock);
207
208 struct unique_sym *entries = tab->entries;
209 size_t size = tab->size;
210 if (entries != NULL)
211 {
212 size_t idx = new_hash % size;
213 size_t hash2 = 1 + new_hash % (size - 2);
214 while (1)
215 {
216 if (entries[idx].hashval == new_hash
217 && strcmp (entries[idx].name, undef_name) == 0)
218 {
219 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
220 {
221 /* We possibly have to initialize the central
222 copy from the copy addressed through the
223 relocation. */
224 result->s = sym;
225 result->m = (struct link_map *) map;
226 }
227 else
228 {
229 result->s = entries[idx].sym;
230 result->m = (struct link_map *) entries[idx].map;
231 }
232 __rtld_lock_unlock_recursive (tab->lock);
233 return;
234 }
235
236 if (entries[idx].name == NULL)
237 break;
238
239 idx += hash2;
240 if (idx >= size)
241 idx -= size;
242 }
243
244 if (size * 3 <= tab->n_elements * 4)
245 {
246 /* Expand the table. */
247#ifdef RTLD_CHECK_FOREIGN_CALL
248 /* This must not happen during runtime relocations. */
249 assert (!RTLD_CHECK_FOREIGN_CALL);
250#endif
251 size_t newsize = _dl_higher_prime_number (size + 1);
252 struct unique_sym *newentries
253 = calloc (sizeof (struct unique_sym), newsize);
254 if (newentries == NULL)
255 {
256 nomem:
257 __rtld_lock_unlock_recursive (tab->lock);
258 _dl_fatal_printf ("out of memory\n");
259 }
260
261 for (idx = 0; idx < size; ++idx)
262 if (entries[idx].name != NULL)
7b8fb2b8
RM
263 enter_unique_sym (newentries, newsize, entries[idx].hashval,
264 entries[idx].name, entries[idx].sym,
265 entries[idx].map);
f393b4aa
WN
266
267 tab->free (entries);
268 tab->size = newsize;
269 size = newsize;
270 entries = tab->entries = newentries;
271 tab->free = free;
272 }
273 }
274 else
275 {
276#ifdef RTLD_CHECK_FOREIGN_CALL
277 /* This must not happen during runtime relocations. */
278 assert (!RTLD_CHECK_FOREIGN_CALL);
279#endif
280
281#ifdef SHARED
282 /* If tab->entries is NULL, but tab->size is not, it means
283 this is the second, conflict finding, lookup for
284 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
285 allocate anything and don't enter anything into the
286 hash table. */
287 if (__glibc_unlikely (tab->size))
288 {
289 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
290 goto success;
291 }
292#endif
293
294#define INITIAL_NUNIQUE_SYM_TABLE 31
295 size = INITIAL_NUNIQUE_SYM_TABLE;
296 entries = calloc (sizeof (struct unique_sym), size);
297 if (entries == NULL)
298 goto nomem;
299
300 tab->entries = entries;
301 tab->size = size;
302 tab->free = free;
303 }
304
305 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
7b8fb2b8 306 enter_unique_sym (entries, size, new_hash, strtab + sym->st_name, ref,
f393b4aa
WN
307 undef_map);
308 else
309 {
7b8fb2b8
RM
310 enter_unique_sym (entries, size,
311 new_hash, strtab + sym->st_name, sym, map);
f393b4aa
WN
312
313 if (map->l_type == lt_loaded)
314 /* Make sure we don't unload this object by
315 setting the appropriate flag. */
316 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
317 }
318 ++tab->n_elements;
319
320#ifdef SHARED
321 success:
322#endif
323 __rtld_lock_unlock_recursive (tab->lock);
324
325 result->s = sym;
326 result->m = (struct link_map *) map;
327}
8352b484 328
786b74f4
UD
329/* Inner part of the lookup functions. We return a value > 0 if we
330 found the symbol, the value 0 if nothing is found and < 0 if
331 something bad happened. */
332static int
333__attribute_noinline__
334do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
335 unsigned long int *old_hash, const ElfW(Sym) *ref,
336 struct sym_val *result, struct r_scope_elem *scope, size_t i,
337 const struct r_found_version *const version, int flags,
338 struct link_map *skip, int type_class, struct link_map *undef_map)
339{
340 size_t n = scope->r_nlist;
341 /* Make sure we read the value before proceeding. Otherwise we
342 might use r_list pointing to the initial scope and r_nlist being
343 the value after a resize. That is the only path in dl-open.c not
344 protected by GSCOPE. A read barrier here might be to expensive. */
345 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
346 struct link_map **list = scope->r_list;
347
348 do
349 {
786b74f4
UD
350 const struct link_map *map = list[i]->l_real;
351
352 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
353 if (map == skip)
354 continue;
355
356 /* Don't search the executable when resolving a copy reloc. */
357 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
358 continue;
359
360 /* Do not look into objects which are going to be removed. */
361 if (map->l_removed)
362 continue;
363
364 /* Print some debugging info if wanted. */
a1ffb40e 365 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS))
786b74f4 366 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
b9375348 367 undef_name, DSO_FILENAME (map->l_name),
786b74f4
UD
368 map->l_ns);
369
370 /* If the hash table is empty there is nothing to do here. */
371 if (map->l_nbuckets == 0)
372 continue;
373
f6488e2b
WN
374 Elf_Symndx symidx;
375 int num_versions = 0;
376 const ElfW(Sym) *versioned_sym = NULL;
377
786b74f4
UD
378 /* The tables for this map. */
379 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
380 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
381
786b74f4
UD
382 const ElfW(Sym) *sym;
383 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
a1ffb40e 384 if (__glibc_likely (bitmask != NULL))
786b74f4
UD
385 {
386 ElfW(Addr) bitmask_word
387 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
388 & map->l_gnu_bitmask_idxbits];
389
390 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
391 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
392 & (__ELF_NATIVE_CLASS - 1));
393
cc7d0447
WN
394 if (__glibc_unlikely ((bitmask_word >> hashbit1)
395 & (bitmask_word >> hashbit2) & 1))
786b74f4
UD
396 {
397 Elf32_Word bucket = map->l_gnu_buckets[new_hash
398 % map->l_nbuckets];
399 if (bucket != 0)
400 {
401 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
402
403 do
404 if (((*hasharr ^ new_hash) >> 1) == 0)
405 {
406 symidx = hasharr - map->l_gnu_chain_zero;
8e25d1e7
CD
407 sym = check_match (undef_name, ref, version, flags,
408 type_class, &symtab[symidx], symidx,
409 strtab, map, &versioned_sym,
410 &num_versions);
786b74f4
UD
411 if (sym != NULL)
412 goto found_it;
413 }
414 while ((*hasharr++ & 1u) == 0);
415 }
416 }
417 /* No symbol found. */
418 symidx = SHN_UNDEF;
419 }
420 else
421 {
422 if (*old_hash == 0xffffffff)
423 *old_hash = _dl_elf_hash (undef_name);
424
425 /* Use the old SysV-style hash table. Search the appropriate
426 hash bucket in this object's symbol table for a definition
427 for the same symbol name. */
428 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
429 symidx != STN_UNDEF;
430 symidx = map->l_chain[symidx])
431 {
8e25d1e7
CD
432 sym = check_match (undef_name, ref, version, flags,
433 type_class, &symtab[symidx], symidx,
434 strtab, map, &versioned_sym,
435 &num_versions);
786b74f4
UD
436 if (sym != NULL)
437 goto found_it;
438 }
439 }
440
441 /* If we have seen exactly one versioned symbol while we are
442 looking for an unversioned symbol and the version is not the
443 default version we still accept this symbol since there are
444 no possible ambiguities. */
445 sym = num_versions == 1 ? versioned_sym : NULL;
446
447 if (sym != NULL)
448 {
449 found_it:
62da1e3b
L
450 /* When UNDEF_MAP is NULL, which indicates we are called from
451 do_lookup_x on relocation against protected data, we skip
452 the data definion in the executable from copy reloc. */
453 if (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
454 && undef_map == NULL
455 && map->l_type == lt_executable
456 && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)
457 {
458 const ElfW(Sym) *s;
459 unsigned int i;
460
461#if ! ELF_MACHINE_NO_RELA
462 if (map->l_info[DT_RELA] != NULL
463 && map->l_info[DT_RELASZ] != NULL
464 && map->l_info[DT_RELASZ]->d_un.d_val != 0)
465 {
466 const ElfW(Rela) *rela
467 = (const ElfW(Rela) *) D_PTR (map, l_info[DT_RELA]);
468 unsigned int rela_count
469 = map->l_info[DT_RELASZ]->d_un.d_val / sizeof (*rela);
470
471 for (i = 0; i < rela_count; i++, rela++)
472 if (elf_machine_type_class (ELFW(R_TYPE) (rela->r_info))
473 == ELF_RTYPE_CLASS_COPY)
474 {
475 s = &symtab[ELFW(R_SYM) (rela->r_info)];
476 if (!strcmp (strtab + s->st_name, undef_name))
477 goto skip;
478 }
479 }
480#endif
481#if ! ELF_MACHINE_NO_REL
482 if (map->l_info[DT_REL] != NULL
483 && map->l_info[DT_RELSZ] != NULL
484 && map->l_info[DT_RELSZ]->d_un.d_val != 0)
485 {
486 const ElfW(Rel) *rel
487 = (const ElfW(Rel) *) D_PTR (map, l_info[DT_REL]);
488 unsigned int rel_count
489 = map->l_info[DT_RELSZ]->d_un.d_val / sizeof (*rel);
490
491 for (i = 0; i < rel_count; i++, rel++)
492 if (elf_machine_type_class (ELFW(R_TYPE) (rel->r_info))
493 == ELF_RTYPE_CLASS_COPY)
494 {
495 s = &symtab[ELFW(R_SYM) (rel->r_info)];
496 if (!strcmp (strtab + s->st_name, undef_name))
497 goto skip;
498 }
499 }
500#endif
501 }
502
b6084a95
MR
503 /* Hidden and internal symbols are local, ignore them. */
504 if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym)))
505 goto skip;
506
cc7d0447 507 switch (ELFW(ST_BIND) (sym->st_info))
786b74f4
UD
508 {
509 case STB_WEAK:
510 /* Weak definition. Use this value if we don't find another. */
a1ffb40e 511 if (__glibc_unlikely (GLRO(dl_dynamic_weak)))
786b74f4
UD
512 {
513 if (! result->s)
514 {
515 result->s = sym;
516 result->m = (struct link_map *) map;
517 }
518 break;
519 }
520 /* FALLTHROUGH */
521 case STB_GLOBAL:
786b74f4
UD
522 /* Global definition. Just what we need. */
523 result->s = sym;
524 result->m = (struct link_map *) map;
525 return 1;
526
527 case STB_GNU_UNIQUE:;
f393b4aa
WN
528 do_lookup_unique (undef_name, new_hash, map, result, type_class,
529 sym, strtab, ref, undef_map);
530 return 1;
786b74f4
UD
531
532 default:
533 /* Local symbols are ignored. */
534 break;
535 }
536 }
537
62da1e3b 538skip:
786b74f4
UD
539 /* If this current map is the one mentioned in the verneed entry
540 and we have not found a weak entry, it is a bug. */
541 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
cc7d0447 542 && __glibc_unlikely (_dl_name_match_p (version->filename, map)))
786b74f4
UD
543 return -1;
544 }
545 while (++i < n);
546
547 /* We have not found anything until now. */
548 return 0;
549}
84384f5b 550
84384f5b 551
871b9158
UD
552static uint_fast32_t
553dl_new_hash (const char *s)
554{
555 uint_fast32_t h = 5381;
556 for (unsigned char c = *s; c != '\0'; c = *++s)
557 h = h * 33 + c;
558 return h & 0xffffffff;
559}
560
561
cf197e41
UD
562/* Add extra dependency on MAP to UNDEF_MAP. */
563static int
b90395e6 564add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
cf197e41 565{
c4bb124a 566 struct link_map *runp;
cf197e41
UD
567 unsigned int i;
568 int result = 0;
569
aff4519d
UD
570 /* Avoid self-references and references to objects which cannot be
571 unloaded anyway. */
c4bb124a
UD
572 if (undef_map == map)
573 return 0;
574
385b4cf4
UD
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;
579
580 struct link_map_reldeps *l_reldeps
581 = atomic_forced_read (undef_map->l_reldeps);
582
583 /* Make sure l_reldeps is read before l_initfini. */
584 atomic_read_barrier ();
585
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)
590 {
591 for (i = 0; l_initfini[i] != NULL; ++i)
592 if (l_initfini[i] == map)
593 return 0;
594 }
595
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)
600 {
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;
606 }
607
b90395e6 608 /* Save serial number of the target MAP. */
385b4cf4 609 unsigned long long serial = map->l_serial;
aff4519d 610
b90395e6 611 /* Make sure nobody can unload the object while we are at it. */
a1ffb40e 612 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
aff4519d 613 {
b90395e6
UD
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));
b90395e6
UD
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);
b90395e6 623
385b4cf4
UD
624 /* From this point on it is unsafe to dereference MAP, until it
625 has been found in one of the lists. */
cf197e41 626
385b4cf4
UD
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)
631 {
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;
636 }
cf197e41 637
385b4cf4
UD
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)
641 {
642 if (undef_map->l_reldeps != l_reldeps)
643 {
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;
649 }
650 else if (undef_map->l_reldeps->act > l_reldepsact)
651 {
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;
659 }
660 }
c4bb124a 661 }
385b4cf4
UD
662 else
663 __rtld_lock_lock_recursive (GL(dl_load_lock));
c4bb124a
UD
664
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. */
c0f62c56 670 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
c4bb124a
UD
671 while (runp != NULL && runp != map)
672 runp = runp->l_next;
673
674 if (runp != NULL)
675 {
b90395e6
UD
676 /* The object is still available. */
677
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;
682
385b4cf4
UD
683 /* Redo the NODELETE check, as when dl_load_lock wasn't held
684 yet this could have changed. */
715899d1 685 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
b90395e6
UD
686 goto out;
687
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)
693 {
694 map->l_flags_1 |= DF_1_NODELETE;
695 goto out;
696 }
697
698 /* Add the reference now. */
a1ffb40e 699 if (__glibc_unlikely (l_reldepsact >= undef_map->l_reldepsmax))
cf197e41 700 {
c4bb124a
UD
701 /* Allocate more memory for the dependency list. Since this
702 can never happen during the startup phase we can use
703 `realloc'. */
385b4cf4
UD
704 struct link_map_reldeps *newp;
705 unsigned int max
706 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
707
b48a267b
UD
708#ifdef RTLD_PREPARE_FOREIGN_CALL
709 RTLD_PREPARE_FOREIGN_CALL;
710#endif
711
385b4cf4
UD
712 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
713 if (newp == NULL)
714 {
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;
721 }
cf197e41 722 else
385b4cf4
UD
723 {
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);
735 }
cf197e41 736 }
715899d1 737 else
385b4cf4
UD
738 {
739 undef_map->l_reldeps->list[l_reldepsact] = map;
740 atomic_write_barrier ();
741 undef_map->l_reldeps->act = l_reldepsact + 1;
742 }
c4bb124a 743
c4bb124a 744 /* Display information if we are debugging. */
a1ffb40e 745 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_FILES))
154d10bd 746 _dl_debug_printf ("\
c0f62c56 747\nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
b9375348 748 DSO_FILENAME (map->l_name),
c0f62c56 749 map->l_ns,
b9375348 750 DSO_FILENAME (undef_map->l_name),
c0f62c56 751 undef_map->l_ns);
cf197e41 752 }
c4bb124a
UD
753 else
754 /* Whoa, that was bad luck. We have to search again. */
755 result = -1;
cf197e41 756
c4bb124a 757 out:
cf197e41 758 /* Release the lock. */
d3c9f895 759 __rtld_lock_unlock_recursive (GL(dl_load_lock));
cf197e41 760
a1ffb40e 761 if (__glibc_unlikely (flags & DL_LOOKUP_GSCOPE_LOCK))
385b4cf4
UD
762 THREAD_GSCOPE_SET_FLAG ();
763
cf197e41 764 return result;
b90395e6
UD
765
766 out_check:
767 if (map->l_serial != serial)
768 result = -1;
769 goto out;
cf197e41
UD
770}
771
32e6df36 772static void
32e6df36 773_dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
c0a777e8 774 const ElfW(Sym) **ref, struct sym_val *value,
7969407a
UD
775 const struct r_found_version *version, int type_class,
776 int protected);
647eb037 777
84384f5b 778
bdf4a4f1 779/* Search loaded objects' symbol tables for a definition of the symbol
609cf614
UD
780 UNDEF_NAME, perhaps with a requested version for the symbol.
781
782 We must never have calls to the audit functions inside this function
783 or in any function which gets called. If this would happen the audit
784 code might create a thread which can throw off all the scope locking. */
c0282c06 785lookup_t
bdf4a4f1
UD
786_dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
787 const ElfW(Sym) **ref,
788 struct r_scope_elem *symbol_scope[],
789 const struct r_found_version *version,
790 int type_class, int flags, struct link_map *skip_map)
84384f5b 791{
871b9158
UD
792 const uint_fast32_t new_hash = dl_new_hash (undef_name);
793 unsigned long int old_hash = 0xffffffff;
0c367d92 794 struct sym_val current_value = { NULL, NULL };
bdf4a4f1 795 struct r_scope_elem **scope = symbol_scope;
84384f5b 796
be4b5a95 797 bump_num_relocations ();
48f6496e 798
b90395e6
UD
799 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
800 is allowed if we look up a versioned symbol. */
801 assert (version == NULL
802 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
803 == 0);
c84142e8 804
bdf4a4f1 805 size_t i = 0;
a1ffb40e 806 if (__glibc_unlikely (skip_map != NULL))
3c457089
UD
807 /* Search the relevant loaded objects for a definition. */
808 while ((*scope)->r_list[i] != skip_map)
809 ++i;
32e6df36 810
c84142e8 811 /* Search the relevant loaded objects for a definition. */
bdf4a4f1 812 for (size_t start = i; *scope != NULL; start = 0, ++scope)
1fb05e3d 813 {
871b9158
UD
814 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
815 &current_value, *scope, start, version, flags,
415ac3df 816 skip_map, type_class, undef_map);
1fb05e3d 817 if (res > 0)
78575a84 818 break;
1fb05e3d 819
cc7d0447 820 if (__glibc_unlikely (res < 0) && skip_map == NULL)
3f933dc2
UD
821 {
822 /* Oh, oh. The file named in the relocation entry does not
bdf4a4f1
UD
823 contain the needed symbol. This code is never reached
824 for unversioned lookups. */
825 assert (version != NULL);
50727aa7 826 const char *reference_name = undef_map ? undef_map->l_name : "";
2449ae7b 827 struct dl_exception exception;
8e17ea58 828 /* XXX We cannot translate the message. */
2449ae7b
FW
829 _dl_exception_create_format
830 (&exception, DSO_FILENAME (reference_name),
831 "symbol %s version %s not defined in file %s"
832 " with link time reference%s",
833 undef_name, version->name, version->filename,
834 res == -2 ? " (no version symbols)" : "");
835 _dl_signal_cexception (0, &exception, N_("relocation error"));
836 _dl_exception_free (&exception);
3f933dc2
UD
837 *ref = NULL;
838 return 0;
839 }
1fb05e3d 840 }
c84142e8 841
a1ffb40e 842 if (__glibc_unlikely (current_value.s == NULL))
0c367d92 843 {
bdf4a4f1 844 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
ff9f1c5f 845 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
c90b5d28
UD
846 {
847 /* We could find no value for a strong reference. */
9363dbb8 848 const char *reference_name = undef_map ? undef_map->l_name : "";
bdf4a4f1
UD
849 const char *versionstr = version ? ", version " : "";
850 const char *versionname = (version && version->name
851 ? version->name : "");
2449ae7b 852 struct dl_exception exception;
c90b5d28 853 /* XXX We cannot translate the message. */
2449ae7b
FW
854 _dl_exception_create_format
855 (&exception, DSO_FILENAME (reference_name),
856 "undefined symbol: %s%s%s",
857 undef_name, versionstr, versionname);
858 _dl_signal_cexception (0, &exception, N_("symbol lookup error"));
859 _dl_exception_free (&exception);
c90b5d28 860 }
0c367d92
UD
861 *ref = NULL;
862 return 0;
863 }
864
bdf4a4f1
UD
865 int protected = (*ref
866 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
a1ffb40e 867 if (__glibc_unlikely (protected != 0))
6aa29abe 868 {
78575a84 869 /* It is very tricky. We need to figure out what value to
2af63968 870 return for the protected symbol. */
697119d6 871 if (type_class == ELF_RTYPE_CLASS_PLT)
6aa29abe 872 {
697119d6
UD
873 if (current_value.s != NULL && current_value.m != undef_map)
874 {
875 current_value.s = *ref;
876 current_value.m = undef_map;
877 }
878 }
879 else
880 {
881 struct sym_val protected_value = { NULL, NULL };
882
9363dbb8 883 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
871b9158
UD
884 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
885 &protected_value, *scope, i, version, flags,
62da1e3b
L
886 skip_map,
887 (ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
888 && ELFW(ST_TYPE) ((*ref)->st_info) == STT_OBJECT
889 && type_class == ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA)
890 ? ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA
891 : ELF_RTYPE_CLASS_PLT, NULL) != 0)
697119d6
UD
892 break;
893
9363dbb8 894 if (protected_value.s != NULL && protected_value.m != undef_map)
697119d6
UD
895 {
896 current_value.s = *ref;
897 current_value.m = undef_map;
898 }
6aa29abe 899 }
6aa29abe 900 }
32e6df36 901
78575a84
UD
902 /* We have to check whether this would bind UNDEF_MAP to an object
903 in the global scope which was dynamically loaded. In this case
904 we have to prevent the latter from being unloaded unless the
905 UNDEF_MAP object is also unloaded. */
cc7d0447 906 if (__glibc_unlikely (current_value.m->l_type == lt_loaded)
78575a84
UD
907 /* Don't do this for explicit lookups as opposed to implicit
908 runtime lookups. */
bdf4a4f1 909 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
78575a84 910 /* Add UNDEF_MAP to the dependencies. */
b90395e6 911 && add_dependency (undef_map, current_value.m, flags) < 0)
78575a84
UD
912 /* Something went wrong. Perhaps the object we tried to reference
913 was just removed. Try finding another definition. */
b90395e6
UD
914 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
915 (flags & DL_LOOKUP_GSCOPE_LOCK)
916 ? undef_map->l_scope : symbol_scope,
e4eb675d 917 version, type_class, flags, skip_map);
78575a84 918
7a11603d 919 /* The object is used. */
a1ffb40e 920 if (__glibc_unlikely (current_value.m->l_used == 0))
2af63968 921 current_value.m->l_used = 1;
7a11603d 922
cc7d0447
WN
923 if (__glibc_unlikely (GLRO(dl_debug_mask)
924 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK)))
c0a777e8 925 _dl_debug_bindings (undef_name, undef_map, ref,
32e6df36
UD
926 &current_value, version, type_class, protected);
927
928 *ref = current_value.s;
929 return LOOKUP_VALUE (current_value.m);
c84142e8
UD
930}
931
932
d66e34cd
RM
933/* Cache the location of MAP's hash table. */
934
935void
936_dl_setup_hash (struct link_map *map)
937{
a1eca9f3 938 Elf_Symndx *hash;
f41c8091 939
6a5cac49 940 if (__glibc_likely (map->l_info[ADDRIDX (DT_GNU_HASH)] != NULL))
871b9158
UD
941 {
942 Elf32_Word *hash32
6a5cac49 943 = (void *) D_PTR (map, l_info[ADDRIDX (DT_GNU_HASH)]);
871b9158
UD
944 map->l_nbuckets = *hash32++;
945 Elf32_Word symbias = *hash32++;
946 Elf32_Word bitmask_nwords = *hash32++;
947 /* Must be a power of two. */
948 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
949 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
950 map->l_gnu_shift = *hash32++;
951
952 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
953 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
954
955 map->l_gnu_buckets = hash32;
956 hash32 += map->l_nbuckets;
957 map->l_gnu_chain_zero = hash32 - symbias;
958 return;
959 }
960
f41c8091
UD
961 if (!map->l_info[DT_HASH])
962 return;
9a88a2d7 963 hash = (void *) D_PTR (map, l_info[DT_HASH]);
f41c8091 964
d66e34cd 965 map->l_nbuckets = *hash++;
1bc33071
UD
966 /* Skip nchain. */
967 hash++;
d66e34cd
RM
968 map->l_buckets = hash;
969 hash += map->l_nbuckets;
970 map->l_chain = hash;
971}
80d9c5f0 972
f9f2a150 973
32e6df36 974static void
32e6df36 975_dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
c0a777e8 976 const ElfW(Sym) **ref, struct sym_val *value,
f9f2a150
UD
977 const struct r_found_version *version, int type_class,
978 int protected)
32e6df36
UD
979{
980 const char *reference_name = undef_map->l_name;
981
afdca0f2 982 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
32e6df36 983 {
21e2d3a4 984 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
b9375348 985 DSO_FILENAME (reference_name),
21e2d3a4 986 undef_map->l_ns,
b9375348 987 DSO_FILENAME (value->m->l_name),
21e2d3a4 988 value->m->l_ns,
154d10bd 989 protected ? "protected" : "normal", undef_name);
32e6df36
UD
990 if (version)
991 _dl_debug_printf_c (" [%s]\n", version->name);
992 else
993 _dl_debug_printf_c ("\n");
994 }
995#ifdef SHARED
afdca0f2 996 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
32e6df36 997 {
f3d18efb
L
998/* ELF_RTYPE_CLASS_XXX must match RTYPE_CLASS_XXX used by prelink with
999 LD_TRACE_PRELINKING. */
1000#define RTYPE_CLASS_VALID 8
1001#define RTYPE_CLASS_PLT (8|1)
1002#define RTYPE_CLASS_COPY (8|2)
1003#define RTYPE_CLASS_TLS (8|4)
1004#if ELF_RTYPE_CLASS_PLT != 0 && ELF_RTYPE_CLASS_PLT != 1
1005# error ELF_RTYPE_CLASS_PLT must be 0 or 1!
1006#endif
1007#if ELF_RTYPE_CLASS_COPY != 0 && ELF_RTYPE_CLASS_COPY != 2
1008# error ELF_RTYPE_CLASS_COPY must be 0 or 2!
1009#endif
32e6df36
UD
1010 int conflict = 0;
1011 struct sym_val val = { NULL, NULL };
1012
afdca0f2 1013 if ((GLRO(dl_trace_prelink_map) == NULL
c0f62c56
UD
1014 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
1015 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
32e6df36 1016 {
871b9158
UD
1017 const uint_fast32_t new_hash = dl_new_hash (undef_name);
1018 unsigned long int old_hash = 0xffffffff;
4ad43b62
UD
1019 struct unique_sym *saved_entries
1020 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
32e6df36 1021
4ad43b62 1022 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
871b9158 1023 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
bdf4a4f1 1024 undef_map->l_local_scope[0], 0, version, 0, NULL,
415ac3df 1025 type_class, undef_map);
32e6df36
UD
1026 if (val.s != value->s || val.m != value->m)
1027 conflict = 1;
cc7d0447 1028 else if (__glibc_unlikely (undef_map->l_symbolic_in_local_scope)
4ad43b62 1029 && val.s
cc7d0447
WN
1030 && __glibc_unlikely (ELFW(ST_BIND) (val.s->st_info)
1031 == STB_GNU_UNIQUE))
4ad43b62
UD
1032 {
1033 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
1034 contains any DT_SYMBOLIC libraries, unfortunately there
1035 can be conflicts even if the above is equal. As symbol
1036 resolution goes from the last library to the first and
1037 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
1038 library, it would be the one that is looked up. */
1039 struct sym_val val2 = { NULL, NULL };
1040 size_t n;
1041 struct r_scope_elem *scope = undef_map->l_local_scope[0];
1042
1043 for (n = 0; n < scope->r_nlist; n++)
1044 if (scope->r_list[n] == val.m)
1045 break;
1046
1047 for (n++; n < scope->r_nlist; n++)
1048 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
1049 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
1050 &val2,
1051 &scope->r_list[n]->l_symbolic_searchlist,
1052 0, version, 0, NULL, type_class,
1053 undef_map) > 0)
1054 {
1055 conflict = 1;
1056 val = val2;
1057 break;
1058 }
1059 }
1060 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
32e6df36
UD
1061 }
1062
02125962
JJ
1063 if (value->s)
1064 {
f3d18efb
L
1065 /* Keep only ELF_RTYPE_CLASS_PLT and ELF_RTYPE_CLASS_COPY
1066 bits since since prelink only uses them. */
1067 type_class &= ELF_RTYPE_CLASS_PLT | ELF_RTYPE_CLASS_COPY;
cc7d0447
WN
1068 if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1069 == STT_TLS))
f3d18efb
L
1070 /* Clear the RTYPE_CLASS_VALID bit in RTYPE_CLASS_TLS. */
1071 type_class = RTYPE_CLASS_TLS & ~RTYPE_CLASS_VALID;
cc7d0447
WN
1072 else if (__glibc_unlikely (ELFW(ST_TYPE) (value->s->st_info)
1073 == STT_GNU_IFUNC))
f3d18efb
L
1074 /* Set the RTYPE_CLASS_VALID bit. */
1075 type_class |= RTYPE_CLASS_VALID;
02125962 1076 }
1d0ad773 1077
32e6df36 1078 if (conflict
afdca0f2
UD
1079 || GLRO(dl_trace_prelink_map) == undef_map
1080 || GLRO(dl_trace_prelink_map) == NULL
02125962 1081 || type_class >= 4)
32e6df36
UD
1082 {
1083 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1084 conflict ? "conflict" : "lookup",
32e6df36 1085 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1086 (size_t) undef_map->l_map_start,
32e6df36 1087 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1088 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
32e6df36 1089 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab
UD
1090 (size_t) (value->s ? value->m->l_map_start : 0),
1091 (int) sizeof (ElfW(Addr)) * 2,
1092 (size_t) (value->s ? value->s->st_value : 0));
32e6df36
UD
1093
1094 if (conflict)
1095 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1096 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1097 (size_t) (val.s ? val.m->l_map_start : 0),
32e6df36 1098 (int) sizeof (ElfW(Addr)) * 2,
d347a4ab 1099 (size_t) (val.s ? val.s->st_value : 0));
32e6df36
UD
1100
1101 _dl_printf ("/%x %s\n", type_class, undef_name);
1102 }
1103 }
1104#endif
1105}