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