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