]> git.ipfire.org Git - thirdparty/glibc.git/blob - ports/sysdeps/mips/dl-lookup.c
Update copyright notices with scripts/update-copyrights.
[thirdparty/glibc.git] / ports / sysdeps / mips / dl-lookup.c
1 /* Look up a symbol in the loaded objects.
2 MIPS/Linux version - special handling of non-PIC undefined symbol rules.
3 Copyright (C) 1995-2013 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <alloca.h>
21 #include <libintl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <ldsodefs.h>
26 #include <dl-hash.h>
27 #include <dl-machine.h>
28 #include <sysdep-cancel.h>
29 #include <bits/libc-lock.h>
30 #include <tls.h>
31 #include <atomic.h>
32
33 #include <assert.h>
34
35 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
36
37 /* We need this string more than once. */
38 static const char undefined_msg[] = "undefined symbol: ";
39
40
41 struct sym_val
42 {
43 const ElfW(Sym) *s;
44 struct link_map *m;
45 };
46
47
48 #define make_string(string, rest...) \
49 ({ \
50 const char *all[] = { string, ## rest }; \
51 size_t len, cnt; \
52 char *result, *cp; \
53 \
54 len = 1; \
55 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
56 len += strlen (all[cnt]); \
57 \
58 cp = result = alloca (len); \
59 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
60 cp = __stpcpy (cp, all[cnt]); \
61 \
62 result; \
63 })
64
65 /* Statistics function. */
66 #ifdef SHARED
67 # define bump_num_relocations() ++GL(dl_num_relocations)
68 #else
69 # define bump_num_relocations() ((void) 0)
70 #endif
71
72
73 /* Inner part of the lookup functions. We return a value > 0 if we
74 found the symbol, the value 0 if nothing is found and < 0 if
75 something bad happened. */
76 static int
77 __attribute_noinline__
78 do_lookup_x (const char *undef_name, uint_fast32_t new_hash,
79 unsigned long int *old_hash, const ElfW(Sym) *ref,
80 struct sym_val *result, struct r_scope_elem *scope, size_t i,
81 const struct r_found_version *const version, int flags,
82 struct link_map *skip, int type_class, struct link_map *undef_map)
83 {
84 size_t n = scope->r_nlist;
85 /* Make sure we read the value before proceeding. Otherwise we
86 might use r_list pointing to the initial scope and r_nlist being
87 the value after a resize. That is the only path in dl-open.c not
88 protected by GSCOPE. A read barrier here might be to expensive. */
89 __asm volatile ("" : "+r" (n), "+m" (scope->r_list));
90 struct link_map **list = scope->r_list;
91
92 do
93 {
94 /* These variables are used in the nested function. */
95 Elf_Symndx symidx;
96 int num_versions = 0;
97 const ElfW(Sym) *versioned_sym = NULL;
98
99 const struct link_map *map = list[i]->l_real;
100
101 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
102 if (map == skip)
103 continue;
104
105 /* Don't search the executable when resolving a copy reloc. */
106 if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable)
107 continue;
108
109 /* Do not look into objects which are going to be removed. */
110 if (map->l_removed)
111 continue;
112
113 /* Print some debugging info if wanted. */
114 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_SYMBOLS, 0))
115 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
116 undef_name,
117 map->l_name[0] ? map->l_name : rtld_progname,
118 map->l_ns);
119
120 /* If the hash table is empty there is nothing to do here. */
121 if (map->l_nbuckets == 0)
122 continue;
123
124 /* The tables for this map. */
125 const ElfW(Sym) *symtab = (const void *) D_PTR (map, l_info[DT_SYMTAB]);
126 const char *strtab = (const void *) D_PTR (map, l_info[DT_STRTAB]);
127
128
129 /* Nested routine to check whether the symbol matches. */
130 const ElfW(Sym) *
131 __attribute_noinline__
132 check_match (const ElfW(Sym) *sym)
133 {
134 unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
135 assert (ELF_RTYPE_CLASS_PLT == 1);
136 /* The semantics of zero/non-zero values of undefined symbols
137 differs depending on whether the non-PIC ABI is in use.
138 Under the non-PIC ABI, a non-zero value indicates that
139 there is an address reference to the symbol and thus it
140 must always be resolved (except when resolving a jump slot
141 relocation) to the PLT entry whose address is provided as
142 the symbol's value; a zero value indicates that this
143 canonical-address behaviour is not required. Yet under the
144 classic MIPS psABI, a zero value indicates that there is an
145 address reference to the function and the dynamic linker
146 must resolve the symbol immediately upon loading. To avoid
147 conflict, symbols for which the dynamic linker must assume
148 the non-PIC ABI semantics are marked with the STO_MIPS_PLT
149 flag. */
150 if (__builtin_expect ((sym->st_value == 0 /* No value. */
151 && stt != STT_TLS)
152 || (sym->st_shndx == SHN_UNDEF
153 && !(sym->st_other & STO_MIPS_PLT))
154 || (type_class & (sym->st_shndx == SHN_UNDEF)),
155 0))
156 return NULL;
157
158 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
159 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
160 code/data definitions. */
161 #define ALLOWED_STT \
162 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
163 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
164 if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
165 return NULL;
166
167 if (sym != ref && strcmp (strtab + sym->st_name, undef_name))
168 /* Not the symbol we are looking for. */
169 return NULL;
170
171 const ElfW(Half) *verstab = map->l_versyms;
172 if (version != NULL)
173 {
174 if (__builtin_expect (verstab == NULL, 0))
175 {
176 /* We need a versioned symbol but haven't found any. If
177 this is the object which is referenced in the verneed
178 entry it is a bug in the library since a symbol must
179 not simply disappear.
180
181 It would also be a bug in the object since it means that
182 the list of required versions is incomplete and so the
183 tests in dl-version.c haven't found a problem.*/
184 assert (version->filename == NULL
185 || ! _dl_name_match_p (version->filename, map));
186
187 /* Otherwise we accept the symbol. */
188 }
189 else
190 {
191 /* We can match the version information or use the
192 default one if it is not hidden. */
193 ElfW(Half) ndx = verstab[symidx] & 0x7fff;
194 if ((map->l_versions[ndx].hash != version->hash
195 || strcmp (map->l_versions[ndx].name, version->name))
196 && (version->hidden || map->l_versions[ndx].hash
197 || (verstab[symidx] & 0x8000)))
198 /* It's not the version we want. */
199 return NULL;
200 }
201 }
202 else
203 {
204 /* No specific version is selected. There are two ways we
205 can got here:
206
207 - a binary which does not include versioning information
208 is loaded
209
210 - dlsym() instead of dlvsym() is used to get a symbol which
211 might exist in more than one form
212
213 If the library does not provide symbol version information
214 there is no problem at all: we simply use the symbol if it
215 is defined.
216
217 These two lookups need to be handled differently if the
218 library defines versions. In the case of the old
219 unversioned application the oldest (default) version
220 should be used. In case of a dlsym() call the latest and
221 public interface should be returned. */
222 if (verstab != NULL)
223 {
224 if ((verstab[symidx] & 0x7fff)
225 >= ((flags & DL_LOOKUP_RETURN_NEWEST) ? 2 : 3))
226 {
227 /* Don't accept hidden symbols. */
228 if ((verstab[symidx] & 0x8000) == 0
229 && num_versions++ == 0)
230 /* No version so far. */
231 versioned_sym = sym;
232
233 return NULL;
234 }
235 }
236 }
237
238 /* There cannot be another entry for this symbol so stop here. */
239 return sym;
240 }
241
242 const ElfW(Sym) *sym;
243 const ElfW(Addr) *bitmask = map->l_gnu_bitmask;
244 if (__builtin_expect (bitmask != NULL, 1))
245 {
246 ElfW(Addr) bitmask_word
247 = bitmask[(new_hash / __ELF_NATIVE_CLASS)
248 & map->l_gnu_bitmask_idxbits];
249
250 unsigned int hashbit1 = new_hash & (__ELF_NATIVE_CLASS - 1);
251 unsigned int hashbit2 = ((new_hash >> map->l_gnu_shift)
252 & (__ELF_NATIVE_CLASS - 1));
253
254 if (__builtin_expect ((bitmask_word >> hashbit1)
255 & (bitmask_word >> hashbit2) & 1, 0))
256 {
257 Elf32_Word bucket = map->l_gnu_buckets[new_hash
258 % map->l_nbuckets];
259 if (bucket != 0)
260 {
261 const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
262
263 do
264 if (((*hasharr ^ new_hash) >> 1) == 0)
265 {
266 symidx = hasharr - map->l_gnu_chain_zero;
267 sym = check_match (&symtab[symidx]);
268 if (sym != NULL)
269 goto found_it;
270 }
271 while ((*hasharr++ & 1u) == 0);
272 }
273 }
274 /* No symbol found. */
275 symidx = SHN_UNDEF;
276 }
277 else
278 {
279 if (*old_hash == 0xffffffff)
280 *old_hash = _dl_elf_hash (undef_name);
281
282 /* Use the old SysV-style hash table. Search the appropriate
283 hash bucket in this object's symbol table for a definition
284 for the same symbol name. */
285 for (symidx = map->l_buckets[*old_hash % map->l_nbuckets];
286 symidx != STN_UNDEF;
287 symidx = map->l_chain[symidx])
288 {
289 sym = check_match (&symtab[symidx]);
290 if (sym != NULL)
291 goto found_it;
292 }
293 }
294
295 /* If we have seen exactly one versioned symbol while we are
296 looking for an unversioned symbol and the version is not the
297 default version we still accept this symbol since there are
298 no possible ambiguities. */
299 sym = num_versions == 1 ? versioned_sym : NULL;
300
301 if (sym != NULL)
302 {
303 found_it:
304 switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL))
305 {
306 case STB_WEAK:
307 /* Weak definition. Use this value if we don't find another. */
308 if (__builtin_expect (GLRO(dl_dynamic_weak), 0))
309 {
310 if (! result->s)
311 {
312 result->s = sym;
313 result->m = (struct link_map *) map;
314 }
315 break;
316 }
317 /* FALLTHROUGH */
318 case STB_GLOBAL:
319 success:
320 /* Global definition. Just what we need. */
321 result->s = sym;
322 result->m = (struct link_map *) map;
323 return 1;
324
325 case STB_GNU_UNIQUE:;
326 /* We have to determine whether we already found a
327 symbol with this name before. If not then we have to
328 add it to the search table. If we already found a
329 definition we have to use it. */
330 void enter (struct unique_sym *table, size_t size,
331 unsigned int hash, const char *name,
332 const ElfW(Sym) *sym, const struct link_map *map)
333 {
334 size_t idx = hash % size;
335 size_t hash2 = 1 + hash % (size - 2);
336 while (table[idx].name != NULL)
337 {
338 idx += hash2;
339 if (idx >= size)
340 idx -= size;
341 }
342
343 table[idx].hashval = hash;
344 table[idx].name = name;
345 table[idx].sym = sym;
346 table[idx].map = map;
347 }
348
349 struct unique_sym_table *tab
350 = &GL(dl_ns)[map->l_ns]._ns_unique_sym_table;
351
352 __rtld_lock_lock_recursive (tab->lock);
353
354 struct unique_sym *entries = tab->entries;
355 size_t size = tab->size;
356 if (entries != NULL)
357 {
358 size_t idx = new_hash % size;
359 size_t hash2 = 1 + new_hash % (size - 2);
360 while (1)
361 {
362 if (entries[idx].hashval == new_hash
363 && strcmp (entries[idx].name, undef_name) == 0)
364 {
365 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
366 {
367 /* We possibly have to initialize the central
368 copy from the copy addressed through the
369 relocation. */
370 result->s = sym;
371 result->m = (struct link_map *) map;
372 }
373 else
374 {
375 result->s = entries[idx].sym;
376 result->m = (struct link_map *) entries[idx].map;
377 }
378 __rtld_lock_unlock_recursive (tab->lock);
379 return 1;
380 }
381
382 if (entries[idx].name == NULL)
383 break;
384
385 idx += hash2;
386 if (idx >= size)
387 idx -= size;
388 }
389
390 if (size * 3 <= tab->n_elements * 4)
391 {
392 /* Expand the table. */
393 #ifdef RTLD_CHECK_FOREIGN_CALL
394 /* This must not happen during runtime relocations. */
395 assert (!RTLD_CHECK_FOREIGN_CALL);
396 #endif
397 size_t newsize = _dl_higher_prime_number (size + 1);
398 struct unique_sym *newentries
399 = calloc (sizeof (struct unique_sym), newsize);
400 if (newentries == NULL)
401 {
402 nomem:
403 __rtld_lock_unlock_recursive (tab->lock);
404 _dl_fatal_printf ("out of memory\n");
405 }
406
407 for (idx = 0; idx < size; ++idx)
408 if (entries[idx].name != NULL)
409 enter (newentries, newsize, entries[idx].hashval,
410 entries[idx].name, entries[idx].sym,
411 entries[idx].map);
412
413 tab->free (entries);
414 tab->size = newsize;
415 size = newsize;
416 entries = tab->entries = newentries;
417 tab->free = free;
418 }
419 }
420 else
421 {
422 #ifdef RTLD_CHECK_FOREIGN_CALL
423 /* This must not happen during runtime relocations. */
424 assert (!RTLD_CHECK_FOREIGN_CALL);
425 #endif
426
427 #ifdef SHARED
428 /* If tab->entries is NULL, but tab->size is not, it means
429 this is the second, conflict finding, lookup for
430 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
431 allocate anything and don't enter anything into the
432 hash table. */
433 if (__builtin_expect (tab->size, 0))
434 {
435 assert (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK);
436 __rtld_lock_unlock_recursive (tab->lock);
437 goto success;
438 }
439 #endif
440
441 #define INITIAL_NUNIQUE_SYM_TABLE 31
442 size = INITIAL_NUNIQUE_SYM_TABLE;
443 entries = calloc (sizeof (struct unique_sym), size);
444 if (entries == NULL)
445 goto nomem;
446
447 tab->entries = entries;
448 tab->size = size;
449 tab->free = free;
450 }
451
452 if ((type_class & ELF_RTYPE_CLASS_COPY) != 0)
453 enter (entries, size, new_hash, strtab + sym->st_name, ref,
454 undef_map);
455 else
456 {
457 enter (entries, size, new_hash, strtab + sym->st_name, sym,
458 map);
459
460 if (map->l_type == lt_loaded)
461 /* Make sure we don't unload this object by
462 setting the appropriate flag. */
463 ((struct link_map *) map)->l_flags_1 |= DF_1_NODELETE;
464 }
465 ++tab->n_elements;
466
467 __rtld_lock_unlock_recursive (tab->lock);
468
469 goto success;
470
471 default:
472 /* Local symbols are ignored. */
473 break;
474 }
475 }
476
477 /* If this current map is the one mentioned in the verneed entry
478 and we have not found a weak entry, it is a bug. */
479 if (symidx == STN_UNDEF && version != NULL && version->filename != NULL
480 && __builtin_expect (_dl_name_match_p (version->filename, map), 0))
481 return -1;
482 }
483 while (++i < n);
484
485 /* We have not found anything until now. */
486 return 0;
487 }
488
489
490 static uint_fast32_t
491 dl_new_hash (const char *s)
492 {
493 uint_fast32_t h = 5381;
494 for (unsigned char c = *s; c != '\0'; c = *++s)
495 h = h * 33 + c;
496 return h & 0xffffffff;
497 }
498
499
500 /* Add extra dependency on MAP to UNDEF_MAP. */
501 static int
502 internal_function
503 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
504 {
505 struct link_map *runp;
506 unsigned int i;
507 int result = 0;
508
509 /* Avoid self-references and references to objects which cannot be
510 unloaded anyway. */
511 if (undef_map == map)
512 return 0;
513
514 /* Avoid references to objects which cannot be unloaded anyway. */
515 assert (map->l_type == lt_loaded);
516 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
517 return 0;
518
519 struct link_map_reldeps *l_reldeps
520 = atomic_forced_read (undef_map->l_reldeps);
521
522 /* Make sure l_reldeps is read before l_initfini. */
523 atomic_read_barrier ();
524
525 /* Determine whether UNDEF_MAP already has a reference to MAP. First
526 look in the normal dependencies. */
527 struct link_map **l_initfini = atomic_forced_read (undef_map->l_initfini);
528 if (l_initfini != NULL)
529 {
530 for (i = 0; l_initfini[i] != NULL; ++i)
531 if (l_initfini[i] == map)
532 return 0;
533 }
534
535 /* No normal dependency. See whether we already had to add it
536 to the special list of dynamic dependencies. */
537 unsigned int l_reldepsact = 0;
538 if (l_reldeps != NULL)
539 {
540 struct link_map **list = &l_reldeps->list[0];
541 l_reldepsact = l_reldeps->act;
542 for (i = 0; i < l_reldepsact; ++i)
543 if (list[i] == map)
544 return 0;
545 }
546
547 /* Save serial number of the target MAP. */
548 unsigned long long serial = map->l_serial;
549
550 /* Make sure nobody can unload the object while we are at it. */
551 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
552 {
553 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
554 here, that can result in ABBA deadlock. */
555 THREAD_GSCOPE_RESET_FLAG ();
556 __rtld_lock_lock_recursive (GL(dl_load_lock));
557 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
558 it can e.g. point to unallocated memory. So avoid the optimizer
559 treating the above read from MAP->l_serial as ensurance it
560 can safely dereference it. */
561 map = atomic_forced_read (map);
562
563 /* From this point on it is unsafe to dereference MAP, until it
564 has been found in one of the lists. */
565
566 /* Redo the l_initfini check in case undef_map's l_initfini
567 changed in the mean time. */
568 if (undef_map->l_initfini != l_initfini
569 && undef_map->l_initfini != NULL)
570 {
571 l_initfini = undef_map->l_initfini;
572 for (i = 0; l_initfini[i] != NULL; ++i)
573 if (l_initfini[i] == map)
574 goto out_check;
575 }
576
577 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
578 the mean time. */
579 if (undef_map->l_reldeps != NULL)
580 {
581 if (undef_map->l_reldeps != l_reldeps)
582 {
583 struct link_map **list = &undef_map->l_reldeps->list[0];
584 l_reldepsact = undef_map->l_reldeps->act;
585 for (i = 0; i < l_reldepsact; ++i)
586 if (list[i] == map)
587 goto out_check;
588 }
589 else if (undef_map->l_reldeps->act > l_reldepsact)
590 {
591 struct link_map **list
592 = &undef_map->l_reldeps->list[0];
593 i = l_reldepsact;
594 l_reldepsact = undef_map->l_reldeps->act;
595 for (; i < l_reldepsact; ++i)
596 if (list[i] == map)
597 goto out_check;
598 }
599 }
600 }
601 else
602 __rtld_lock_lock_recursive (GL(dl_load_lock));
603
604 /* The object is not yet in the dependency list. Before we add
605 it make sure just one more time the object we are about to
606 reference is still available. There is a brief period in
607 which the object could have been removed since we found the
608 definition. */
609 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
610 while (runp != NULL && runp != map)
611 runp = runp->l_next;
612
613 if (runp != NULL)
614 {
615 /* The object is still available. */
616
617 /* MAP could have been dlclosed, freed and then some other dlopened
618 library could have the same link_map pointer. */
619 if (map->l_serial != serial)
620 goto out_check;
621
622 /* Redo the NODELETE check, as when dl_load_lock wasn't held
623 yet this could have changed. */
624 if ((map->l_flags_1 & DF_1_NODELETE) != 0)
625 goto out;
626
627 /* If the object with the undefined reference cannot be removed ever
628 just make sure the same is true for the object which contains the
629 definition. */
630 if (undef_map->l_type != lt_loaded
631 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
632 {
633 map->l_flags_1 |= DF_1_NODELETE;
634 goto out;
635 }
636
637 /* Add the reference now. */
638 if (__builtin_expect (l_reldepsact >= undef_map->l_reldepsmax, 0))
639 {
640 /* Allocate more memory for the dependency list. Since this
641 can never happen during the startup phase we can use
642 `realloc'. */
643 struct link_map_reldeps *newp;
644 unsigned int max
645 = undef_map->l_reldepsmax ? undef_map->l_reldepsmax * 2 : 10;
646
647 #ifdef RTLD_PREPARE_FOREIGN_CALL
648 RTLD_PREPARE_FOREIGN_CALL;
649 #endif
650
651 newp = malloc (sizeof (*newp) + max * sizeof (struct link_map *));
652 if (newp == NULL)
653 {
654 /* If we didn't manage to allocate memory for the list this is
655 no fatal problem. We simply make sure the referenced object
656 cannot be unloaded. This is semantically the correct
657 behavior. */
658 map->l_flags_1 |= DF_1_NODELETE;
659 goto out;
660 }
661 else
662 {
663 if (l_reldepsact)
664 memcpy (&newp->list[0], &undef_map->l_reldeps->list[0],
665 l_reldepsact * sizeof (struct link_map *));
666 newp->list[l_reldepsact] = map;
667 newp->act = l_reldepsact + 1;
668 atomic_write_barrier ();
669 void *old = undef_map->l_reldeps;
670 undef_map->l_reldeps = newp;
671 undef_map->l_reldepsmax = max;
672 if (old)
673 _dl_scope_free (old);
674 }
675 }
676 else
677 {
678 undef_map->l_reldeps->list[l_reldepsact] = map;
679 atomic_write_barrier ();
680 undef_map->l_reldeps->act = l_reldepsact + 1;
681 }
682
683 /* Display information if we are debugging. */
684 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
685 _dl_debug_printf ("\
686 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
687 map->l_name[0] ? map->l_name : rtld_progname,
688 map->l_ns,
689 undef_map->l_name[0]
690 ? undef_map->l_name : rtld_progname,
691 undef_map->l_ns);
692 }
693 else
694 /* Whoa, that was bad luck. We have to search again. */
695 result = -1;
696
697 out:
698 /* Release the lock. */
699 __rtld_lock_unlock_recursive (GL(dl_load_lock));
700
701 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
702 THREAD_GSCOPE_SET_FLAG ();
703
704 return result;
705
706 out_check:
707 if (map->l_serial != serial)
708 result = -1;
709 goto out;
710 }
711
712 static void
713 internal_function
714 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
715 const ElfW(Sym) **ref, struct sym_val *value,
716 const struct r_found_version *version, int type_class,
717 int protected);
718
719
720 /* Search loaded objects' symbol tables for a definition of the symbol
721 UNDEF_NAME, perhaps with a requested version for the symbol.
722
723 We must never have calls to the audit functions inside this function
724 or in any function which gets called. If this would happen the audit
725 code might create a thread which can throw off all the scope locking. */
726 lookup_t
727 internal_function
728 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
729 const ElfW(Sym) **ref,
730 struct r_scope_elem *symbol_scope[],
731 const struct r_found_version *version,
732 int type_class, int flags, struct link_map *skip_map)
733 {
734 const uint_fast32_t new_hash = dl_new_hash (undef_name);
735 unsigned long int old_hash = 0xffffffff;
736 struct sym_val current_value = { NULL, NULL };
737 struct r_scope_elem **scope = symbol_scope;
738
739 bump_num_relocations ();
740
741 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
742 is allowed if we look up a versioned symbol. */
743 assert (version == NULL
744 || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY | DL_LOOKUP_GSCOPE_LOCK))
745 == 0);
746
747 size_t i = 0;
748 if (__builtin_expect (skip_map != NULL, 0))
749 /* Search the relevant loaded objects for a definition. */
750 while ((*scope)->r_list[i] != skip_map)
751 ++i;
752
753 /* Search the relevant loaded objects for a definition. */
754 for (size_t start = i; *scope != NULL; start = 0, ++scope)
755 {
756 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
757 &current_value, *scope, start, version, flags,
758 skip_map, type_class, undef_map);
759 if (res > 0)
760 break;
761
762 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
763 {
764 /* Oh, oh. The file named in the relocation entry does not
765 contain the needed symbol. This code is never reached
766 for unversioned lookups. */
767 assert (version != NULL);
768 const char *reference_name = undef_map ? undef_map->l_name : NULL;
769
770 /* XXX We cannot translate the message. */
771 _dl_signal_cerror (0, (reference_name[0]
772 ? reference_name
773 : (rtld_progname ?: "<main program>")),
774 N_("relocation error"),
775 make_string ("symbol ", undef_name, ", version ",
776 version->name,
777 " not defined in file ",
778 version->filename,
779 " with link time reference",
780 res == -2
781 ? " (no version symbols)" : ""));
782 *ref = NULL;
783 return 0;
784 }
785 }
786
787 if (__builtin_expect (current_value.s == NULL, 0))
788 {
789 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
790 && skip_map == NULL
791 && !(GLRO(dl_debug_mask) & DL_DEBUG_UNUSED))
792 {
793 /* We could find no value for a strong reference. */
794 const char *reference_name = undef_map ? undef_map->l_name : "";
795 const char *versionstr = version ? ", version " : "";
796 const char *versionname = (version && version->name
797 ? version->name : "");
798
799 /* XXX We cannot translate the message. */
800 _dl_signal_cerror (0, (reference_name[0]
801 ? reference_name
802 : (rtld_progname ?: "<main program>")),
803 N_("symbol lookup error"),
804 make_string (undefined_msg, undef_name,
805 versionstr, versionname));
806 }
807 *ref = NULL;
808 return 0;
809 }
810
811 int protected = (*ref
812 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
813 if (__builtin_expect (protected != 0, 0))
814 {
815 /* It is very tricky. We need to figure out what value to
816 return for the protected symbol. */
817 if (type_class == ELF_RTYPE_CLASS_PLT)
818 {
819 if (current_value.s != NULL && current_value.m != undef_map)
820 {
821 current_value.s = *ref;
822 current_value.m = undef_map;
823 }
824 }
825 else
826 {
827 struct sym_val protected_value = { NULL, NULL };
828
829 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
830 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
831 &protected_value, *scope, i, version, flags,
832 skip_map, ELF_RTYPE_CLASS_PLT, NULL) != 0)
833 break;
834
835 if (protected_value.s != NULL && protected_value.m != undef_map)
836 {
837 current_value.s = *ref;
838 current_value.m = undef_map;
839 }
840 }
841 }
842
843 /* We have to check whether this would bind UNDEF_MAP to an object
844 in the global scope which was dynamically loaded. In this case
845 we have to prevent the latter from being unloaded unless the
846 UNDEF_MAP object is also unloaded. */
847 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
848 /* Don't do this for explicit lookups as opposed to implicit
849 runtime lookups. */
850 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
851 /* Add UNDEF_MAP to the dependencies. */
852 && add_dependency (undef_map, current_value.m, flags) < 0)
853 /* Something went wrong. Perhaps the object we tried to reference
854 was just removed. Try finding another definition. */
855 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
856 (flags & DL_LOOKUP_GSCOPE_LOCK)
857 ? undef_map->l_scope : symbol_scope,
858 version, type_class, flags, skip_map);
859
860 /* The object is used. */
861 if (__builtin_expect (current_value.m->l_used == 0, 0))
862 current_value.m->l_used = 1;
863
864 if (__builtin_expect (GLRO(dl_debug_mask)
865 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
866 _dl_debug_bindings (undef_name, undef_map, ref,
867 &current_value, version, type_class, protected);
868
869 *ref = current_value.s;
870 return LOOKUP_VALUE (current_value.m);
871 }
872
873
874 /* Cache the location of MAP's hash table. */
875
876 void
877 internal_function
878 _dl_setup_hash (struct link_map *map)
879 {
880 Elf_Symndx *hash;
881
882 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
883 + DT_THISPROCNUM + DT_VERSIONTAGNUM
884 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
885 {
886 Elf32_Word *hash32
887 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
888 + DT_THISPROCNUM + DT_VERSIONTAGNUM
889 + DT_EXTRANUM + DT_VALNUM]);
890 map->l_nbuckets = *hash32++;
891 Elf32_Word symbias = *hash32++;
892 Elf32_Word bitmask_nwords = *hash32++;
893 /* Must be a power of two. */
894 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
895 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
896 map->l_gnu_shift = *hash32++;
897
898 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
899 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
900
901 map->l_gnu_buckets = hash32;
902 hash32 += map->l_nbuckets;
903 map->l_gnu_chain_zero = hash32 - symbias;
904 return;
905 }
906
907 if (!map->l_info[DT_HASH])
908 return;
909 hash = (void *) D_PTR (map, l_info[DT_HASH]);
910
911 map->l_nbuckets = *hash++;
912 /* Skip nchain. */
913 hash++;
914 map->l_buckets = hash;
915 hash += map->l_nbuckets;
916 map->l_chain = hash;
917 }
918
919
920 static void
921 internal_function
922 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
923 const ElfW(Sym) **ref, struct sym_val *value,
924 const struct r_found_version *version, int type_class,
925 int protected)
926 {
927 const char *reference_name = undef_map->l_name;
928
929 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
930 {
931 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
932 (reference_name[0]
933 ? reference_name
934 : (rtld_progname ?: "<main program>")),
935 undef_map->l_ns,
936 value->m->l_name[0] ? value->m->l_name : rtld_progname,
937 value->m->l_ns,
938 protected ? "protected" : "normal", undef_name);
939 if (version)
940 _dl_debug_printf_c (" [%s]\n", version->name);
941 else
942 _dl_debug_printf_c ("\n");
943 }
944 #ifdef SHARED
945 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
946 {
947 int conflict = 0;
948 struct sym_val val = { NULL, NULL };
949
950 if ((GLRO(dl_trace_prelink_map) == NULL
951 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
952 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
953 {
954 const uint_fast32_t new_hash = dl_new_hash (undef_name);
955 unsigned long int old_hash = 0xffffffff;
956 struct unique_sym *saved_entries
957 = GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries;
958
959 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = NULL;
960 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
961 undef_map->l_local_scope[0], 0, version, 0, NULL,
962 type_class, undef_map);
963 if (val.s != value->s || val.m != value->m)
964 conflict = 1;
965 else if (__builtin_expect (undef_map->l_symbolic_in_local_scope, 0)
966 && val.s
967 && __builtin_expect (ELFW(ST_BIND) (val.s->st_info),
968 STB_GLOBAL) == STB_GNU_UNIQUE)
969 {
970 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
971 contains any DT_SYMBOLIC libraries, unfortunately there
972 can be conflicts even if the above is equal. As symbol
973 resolution goes from the last library to the first and
974 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
975 library, it would be the one that is looked up. */
976 struct sym_val val2 = { NULL, NULL };
977 size_t n;
978 struct r_scope_elem *scope = undef_map->l_local_scope[0];
979
980 for (n = 0; n < scope->r_nlist; n++)
981 if (scope->r_list[n] == val.m)
982 break;
983
984 for (n++; n < scope->r_nlist; n++)
985 if (scope->r_list[n]->l_info[DT_SYMBOLIC] != NULL
986 && do_lookup_x (undef_name, new_hash, &old_hash, *ref,
987 &val2,
988 &scope->r_list[n]->l_symbolic_searchlist,
989 0, version, 0, NULL, type_class,
990 undef_map) > 0)
991 {
992 conflict = 1;
993 val = val2;
994 break;
995 }
996 }
997 GL(dl_ns)[LM_ID_BASE]._ns_unique_sym_table.entries = saved_entries;
998 }
999
1000 if (value->s)
1001 {
1002 if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1003 == STT_TLS, 0))
1004 type_class = 4;
1005 else if (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
1006 == STT_GNU_IFUNC, 0))
1007 type_class |= 8;
1008 }
1009
1010 if (conflict
1011 || GLRO(dl_trace_prelink_map) == undef_map
1012 || GLRO(dl_trace_prelink_map) == NULL
1013 || type_class >= 4)
1014 {
1015 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1016 conflict ? "conflict" : "lookup",
1017 (int) sizeof (ElfW(Addr)) * 2,
1018 (size_t) undef_map->l_map_start,
1019 (int) sizeof (ElfW(Addr)) * 2,
1020 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
1021 (int) sizeof (ElfW(Addr)) * 2,
1022 (size_t) (value->s ? value->m->l_map_start : 0),
1023 (int) sizeof (ElfW(Addr)) * 2,
1024 (size_t) (value->s ? value->s->st_value : 0));
1025
1026 if (conflict)
1027 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1028 (int) sizeof (ElfW(Addr)) * 2,
1029 (size_t) (val.s ? val.m->l_map_start : 0),
1030 (int) sizeof (ElfW(Addr)) * 2,
1031 (size_t) (val.s ? val.s->st_value : 0));
1032
1033 _dl_printf ("/%x %s\n", type_class, undef_name);
1034 }
1035 }
1036 #endif
1037 }