1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
26 #include <dl-machine.h>
27 #include <sysdep-cancel.h>
28 #include <bits/libc-lock.h>
34 /* Return nonzero if check_match should consider SYM to fail to match a
35 symbol reference for some machine-specific reason. */
36 #ifndef ELF_MACHINE_SYM_NO_MATCH
37 # define ELF_MACHINE_SYM_NO_MATCH(sym) 0
40 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
42 /* We need this string more than once. */
43 static const char undefined_msg
[] = "undefined symbol: ";
53 #define make_string(string, rest...) \
55 const char *all[] = { string, ## rest }; \
60 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
61 len += strlen (all[cnt]); \
63 cp = result = alloca (len); \
64 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
65 cp = __stpcpy (cp, all[cnt]); \
70 /* Statistics function. */
72 # define bump_num_relocations() ++GL(dl_num_relocations)
74 # define bump_num_relocations() ((void) 0)
77 /* Utility function for do_lookup_x. The caller is called with undef_name,
78 ref, version, flags and type_class, and those are passed as the first
79 five arguments. The caller then computes sym, symidx, strtab, and map
80 and passes them as the next four arguments. Lastly the caller passes in
81 versioned_sym and num_versions which are modified by check_match during
82 the checking process. */
83 static const ElfW(Sym
) *
84 check_match (const char *const undef_name
,
85 const ElfW(Sym
) *const ref
,
86 const struct r_found_version
*const version
,
89 const ElfW(Sym
) *const sym
,
90 const Elf_Symndx symidx
,
91 const char *const strtab
,
92 const struct link_map
*const map
,
93 const ElfW(Sym
) **const versioned_sym
,
94 int *const num_versions
)
96 unsigned int stt
= ELFW(ST_TYPE
) (sym
->st_info
);
97 assert (ELF_RTYPE_CLASS_PLT
== 1);
98 if (__builtin_expect ((sym
->st_value
== 0 /* No value. */
100 || ELF_MACHINE_SYM_NO_MATCH (sym
)
101 || (type_class
& (sym
->st_shndx
== SHN_UNDEF
)),
105 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
106 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
107 code/data definitions. */
108 #define ALLOWED_STT \
109 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
110 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
111 if (__glibc_unlikely (((1 << stt
) & ALLOWED_STT
) == 0))
114 if (sym
!= ref
&& strcmp (strtab
+ sym
->st_name
, undef_name
))
115 /* Not the symbol we are looking for. */
118 const ElfW(Half
) *verstab
= map
->l_versyms
;
121 if (__glibc_unlikely (verstab
== NULL
))
123 /* We need a versioned symbol but haven't found any. If
124 this is the object which is referenced in the verneed
125 entry it is a bug in the library since a symbol must
126 not simply disappear.
128 It would also be a bug in the object since it means that
129 the list of required versions is incomplete and so the
130 tests in dl-version.c haven't found a problem.*/
131 assert (version
->filename
== NULL
132 || ! _dl_name_match_p (version
->filename
, map
));
134 /* Otherwise we accept the symbol. */
138 /* We can match the version information or use the
139 default one if it is not hidden. */
140 ElfW(Half
) ndx
= verstab
[symidx
] & 0x7fff;
141 if ((map
->l_versions
[ndx
].hash
!= version
->hash
142 || strcmp (map
->l_versions
[ndx
].name
, version
->name
))
143 && (version
->hidden
|| map
->l_versions
[ndx
].hash
144 || (verstab
[symidx
] & 0x8000)))
145 /* It's not the version we want. */
151 /* No specific version is selected. There are two ways we
154 - a binary which does not include versioning information
157 - dlsym() instead of dlvsym() is used to get a symbol which
158 might exist in more than one form
160 If the library does not provide symbol version information
161 there is no problem at all: we simply use the symbol if it
164 These two lookups need to be handled differently if the
165 library defines versions. In the case of the old
166 unversioned application the oldest (default) version
167 should be used. In case of a dlsym() call the latest and
168 public interface should be returned. */
171 if ((verstab
[symidx
] & 0x7fff)
172 >= ((flags
& DL_LOOKUP_RETURN_NEWEST
) ? 2 : 3))
174 /* Don't accept hidden symbols. */
175 if ((verstab
[symidx
] & 0x8000) == 0
176 && (*num_versions
)++ == 0)
177 /* No version so far. */
178 *versioned_sym
= sym
;
185 /* There cannot be another entry for this symbol so stop here. */
190 /* Inner part of the lookup functions. We return a value > 0 if we
191 found the symbol, the value 0 if nothing is found and < 0 if
192 something bad happened. */
194 __attribute_noinline__
195 do_lookup_x (const char *undef_name
, uint_fast32_t new_hash
,
196 unsigned long int *old_hash
, const ElfW(Sym
) *ref
,
197 struct sym_val
*result
, struct r_scope_elem
*scope
, size_t i
,
198 const struct r_found_version
*const version
, int flags
,
199 struct link_map
*skip
, int type_class
, struct link_map
*undef_map
)
201 size_t n
= scope
->r_nlist
;
202 /* Make sure we read the value before proceeding. Otherwise we
203 might use r_list pointing to the initial scope and r_nlist being
204 the value after a resize. That is the only path in dl-open.c not
205 protected by GSCOPE. A read barrier here might be to expensive. */
206 __asm
volatile ("" : "+r" (n
), "+m" (scope
->r_list
));
207 struct link_map
**list
= scope
->r_list
;
211 /* These variables are used in the nested function. */
213 int num_versions
= 0;
214 const ElfW(Sym
) *versioned_sym
= NULL
;
216 const struct link_map
*map
= list
[i
]->l_real
;
218 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
222 /* Don't search the executable when resolving a copy reloc. */
223 if ((type_class
& ELF_RTYPE_CLASS_COPY
) && map
->l_type
== lt_executable
)
226 /* Do not look into objects which are going to be removed. */
230 /* Print some debugging info if wanted. */
231 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_SYMBOLS
))
232 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
233 undef_name
, DSO_FILENAME (map
->l_name
),
236 /* If the hash table is empty there is nothing to do here. */
237 if (map
->l_nbuckets
== 0)
240 /* The tables for this map. */
241 const ElfW(Sym
) *symtab
= (const void *) D_PTR (map
, l_info
[DT_SYMTAB
]);
242 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
244 const ElfW(Sym
) *sym
;
245 const ElfW(Addr
) *bitmask
= map
->l_gnu_bitmask
;
246 if (__glibc_likely (bitmask
!= NULL
))
248 ElfW(Addr
) bitmask_word
249 = bitmask
[(new_hash
/ __ELF_NATIVE_CLASS
)
250 & map
->l_gnu_bitmask_idxbits
];
252 unsigned int hashbit1
= new_hash
& (__ELF_NATIVE_CLASS
- 1);
253 unsigned int hashbit2
= ((new_hash
>> map
->l_gnu_shift
)
254 & (__ELF_NATIVE_CLASS
- 1));
256 if (__builtin_expect ((bitmask_word
>> hashbit1
)
257 & (bitmask_word
>> hashbit2
) & 1, 0))
259 Elf32_Word bucket
= map
->l_gnu_buckets
[new_hash
263 const Elf32_Word
*hasharr
= &map
->l_gnu_chain_zero
[bucket
];
266 if (((*hasharr
^ new_hash
) >> 1) == 0)
268 symidx
= hasharr
- map
->l_gnu_chain_zero
;
269 sym
= check_match (undef_name
, ref
, version
, flags
,
270 type_class
, &symtab
[symidx
], symidx
,
271 strtab
, map
, &versioned_sym
,
276 while ((*hasharr
++ & 1u) == 0);
279 /* No symbol found. */
284 if (*old_hash
== 0xffffffff)
285 *old_hash
= _dl_elf_hash (undef_name
);
287 /* Use the old SysV-style hash table. Search the appropriate
288 hash bucket in this object's symbol table for a definition
289 for the same symbol name. */
290 for (symidx
= map
->l_buckets
[*old_hash
% map
->l_nbuckets
];
292 symidx
= map
->l_chain
[symidx
])
294 sym
= check_match (undef_name
, ref
, version
, flags
,
295 type_class
, &symtab
[symidx
], symidx
,
296 strtab
, map
, &versioned_sym
,
303 /* If we have seen exactly one versioned symbol while we are
304 looking for an unversioned symbol and the version is not the
305 default version we still accept this symbol since there are
306 no possible ambiguities. */
307 sym
= num_versions
== 1 ? versioned_sym
: NULL
;
312 switch (__builtin_expect (ELFW(ST_BIND
) (sym
->st_info
), STB_GLOBAL
))
315 /* Weak definition. Use this value if we don't find another. */
316 if (__glibc_unlikely (GLRO(dl_dynamic_weak
)))
321 result
->m
= (struct link_map
*) map
;
328 /* Global definition. Just what we need. */
330 result
->m
= (struct link_map
*) map
;
333 case STB_GNU_UNIQUE
:;
334 /* We have to determine whether we already found a
335 symbol with this name before. If not then we have to
336 add it to the search table. If we already found a
337 definition we have to use it. */
338 void enter (struct unique_sym
*table
, size_t size
,
339 unsigned int hash
, const char *name
,
340 const ElfW(Sym
) *sym
, const struct link_map
*map
)
342 size_t idx
= hash
% size
;
343 size_t hash2
= 1 + hash
% (size
- 2);
344 while (table
[idx
].name
!= NULL
)
351 table
[idx
].hashval
= hash
;
352 table
[idx
].name
= name
;
353 table
[idx
].sym
= sym
;
354 table
[idx
].map
= map
;
357 struct unique_sym_table
*tab
358 = &GL(dl_ns
)[map
->l_ns
]._ns_unique_sym_table
;
360 __rtld_lock_lock_recursive (tab
->lock
);
362 struct unique_sym
*entries
= tab
->entries
;
363 size_t size
= tab
->size
;
366 size_t idx
= new_hash
% size
;
367 size_t hash2
= 1 + new_hash
% (size
- 2);
370 if (entries
[idx
].hashval
== new_hash
371 && strcmp (entries
[idx
].name
, undef_name
) == 0)
373 if ((type_class
& ELF_RTYPE_CLASS_COPY
) != 0)
375 /* We possibly have to initialize the central
376 copy from the copy addressed through the
379 result
->m
= (struct link_map
*) map
;
383 result
->s
= entries
[idx
].sym
;
384 result
->m
= (struct link_map
*) entries
[idx
].map
;
386 __rtld_lock_unlock_recursive (tab
->lock
);
390 if (entries
[idx
].name
== NULL
)
398 if (size
* 3 <= tab
->n_elements
* 4)
400 /* Expand the table. */
401 #ifdef RTLD_CHECK_FOREIGN_CALL
402 /* This must not happen during runtime relocations. */
403 assert (!RTLD_CHECK_FOREIGN_CALL
);
405 size_t newsize
= _dl_higher_prime_number (size
+ 1);
406 struct unique_sym
*newentries
407 = calloc (sizeof (struct unique_sym
), newsize
);
408 if (newentries
== NULL
)
411 __rtld_lock_unlock_recursive (tab
->lock
);
412 _dl_fatal_printf ("out of memory\n");
415 for (idx
= 0; idx
< size
; ++idx
)
416 if (entries
[idx
].name
!= NULL
)
417 enter (newentries
, newsize
, entries
[idx
].hashval
,
418 entries
[idx
].name
, entries
[idx
].sym
,
424 entries
= tab
->entries
= newentries
;
430 #ifdef RTLD_CHECK_FOREIGN_CALL
431 /* This must not happen during runtime relocations. */
432 assert (!RTLD_CHECK_FOREIGN_CALL
);
436 /* If tab->entries is NULL, but tab->size is not, it means
437 this is the second, conflict finding, lookup for
438 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
439 allocate anything and don't enter anything into the
441 if (__glibc_unlikely (tab
->size
))
443 assert (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
);
444 __rtld_lock_unlock_recursive (tab
->lock
);
449 #define INITIAL_NUNIQUE_SYM_TABLE 31
450 size
= INITIAL_NUNIQUE_SYM_TABLE
;
451 entries
= calloc (sizeof (struct unique_sym
), size
);
455 tab
->entries
= entries
;
460 if ((type_class
& ELF_RTYPE_CLASS_COPY
) != 0)
461 enter (entries
, size
, new_hash
, strtab
+ sym
->st_name
, ref
,
465 enter (entries
, size
, new_hash
, strtab
+ sym
->st_name
, sym
,
468 if (map
->l_type
== lt_loaded
)
469 /* Make sure we don't unload this object by
470 setting the appropriate flag. */
471 ((struct link_map
*) map
)->l_flags_1
|= DF_1_NODELETE
;
475 __rtld_lock_unlock_recursive (tab
->lock
);
480 /* Local symbols are ignored. */
485 /* If this current map is the one mentioned in the verneed entry
486 and we have not found a weak entry, it is a bug. */
487 if (symidx
== STN_UNDEF
&& version
!= NULL
&& version
->filename
!= NULL
488 && __builtin_expect (_dl_name_match_p (version
->filename
, map
), 0))
493 /* We have not found anything until now. */
499 dl_new_hash (const char *s
)
501 uint_fast32_t h
= 5381;
502 for (unsigned char c
= *s
; c
!= '\0'; c
= *++s
)
504 return h
& 0xffffffff;
508 /* Add extra dependency on MAP to UNDEF_MAP. */
511 add_dependency (struct link_map
*undef_map
, struct link_map
*map
, int flags
)
513 struct link_map
*runp
;
517 /* Avoid self-references and references to objects which cannot be
519 if (undef_map
== map
)
522 /* Avoid references to objects which cannot be unloaded anyway. */
523 assert (map
->l_type
== lt_loaded
);
524 if ((map
->l_flags_1
& DF_1_NODELETE
) != 0)
527 struct link_map_reldeps
*l_reldeps
528 = atomic_forced_read (undef_map
->l_reldeps
);
530 /* Make sure l_reldeps is read before l_initfini. */
531 atomic_read_barrier ();
533 /* Determine whether UNDEF_MAP already has a reference to MAP. First
534 look in the normal dependencies. */
535 struct link_map
**l_initfini
= atomic_forced_read (undef_map
->l_initfini
);
536 if (l_initfini
!= NULL
)
538 for (i
= 0; l_initfini
[i
] != NULL
; ++i
)
539 if (l_initfini
[i
] == map
)
543 /* No normal dependency. See whether we already had to add it
544 to the special list of dynamic dependencies. */
545 unsigned int l_reldepsact
= 0;
546 if (l_reldeps
!= NULL
)
548 struct link_map
**list
= &l_reldeps
->list
[0];
549 l_reldepsact
= l_reldeps
->act
;
550 for (i
= 0; i
< l_reldepsact
; ++i
)
555 /* Save serial number of the target MAP. */
556 unsigned long long serial
= map
->l_serial
;
558 /* Make sure nobody can unload the object while we are at it. */
559 if (__glibc_unlikely (flags
& DL_LOOKUP_GSCOPE_LOCK
))
561 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
562 here, that can result in ABBA deadlock. */
563 THREAD_GSCOPE_RESET_FLAG ();
564 __rtld_lock_lock_recursive (GL(dl_load_lock
));
565 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
566 it can e.g. point to unallocated memory. So avoid the optimizer
567 treating the above read from MAP->l_serial as ensurance it
568 can safely dereference it. */
569 map
= atomic_forced_read (map
);
571 /* From this point on it is unsafe to dereference MAP, until it
572 has been found in one of the lists. */
574 /* Redo the l_initfini check in case undef_map's l_initfini
575 changed in the mean time. */
576 if (undef_map
->l_initfini
!= l_initfini
577 && undef_map
->l_initfini
!= NULL
)
579 l_initfini
= undef_map
->l_initfini
;
580 for (i
= 0; l_initfini
[i
] != NULL
; ++i
)
581 if (l_initfini
[i
] == map
)
585 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
587 if (undef_map
->l_reldeps
!= NULL
)
589 if (undef_map
->l_reldeps
!= l_reldeps
)
591 struct link_map
**list
= &undef_map
->l_reldeps
->list
[0];
592 l_reldepsact
= undef_map
->l_reldeps
->act
;
593 for (i
= 0; i
< l_reldepsact
; ++i
)
597 else if (undef_map
->l_reldeps
->act
> l_reldepsact
)
599 struct link_map
**list
600 = &undef_map
->l_reldeps
->list
[0];
602 l_reldepsact
= undef_map
->l_reldeps
->act
;
603 for (; i
< l_reldepsact
; ++i
)
610 __rtld_lock_lock_recursive (GL(dl_load_lock
));
612 /* The object is not yet in the dependency list. Before we add
613 it make sure just one more time the object we are about to
614 reference is still available. There is a brief period in
615 which the object could have been removed since we found the
617 runp
= GL(dl_ns
)[undef_map
->l_ns
]._ns_loaded
;
618 while (runp
!= NULL
&& runp
!= map
)
623 /* The object is still available. */
625 /* MAP could have been dlclosed, freed and then some other dlopened
626 library could have the same link_map pointer. */
627 if (map
->l_serial
!= serial
)
630 /* Redo the NODELETE check, as when dl_load_lock wasn't held
631 yet this could have changed. */
632 if ((map
->l_flags_1
& DF_1_NODELETE
) != 0)
635 /* If the object with the undefined reference cannot be removed ever
636 just make sure the same is true for the object which contains the
638 if (undef_map
->l_type
!= lt_loaded
639 || (undef_map
->l_flags_1
& DF_1_NODELETE
) != 0)
641 map
->l_flags_1
|= DF_1_NODELETE
;
645 /* Add the reference now. */
646 if (__glibc_unlikely (l_reldepsact
>= undef_map
->l_reldepsmax
))
648 /* Allocate more memory for the dependency list. Since this
649 can never happen during the startup phase we can use
651 struct link_map_reldeps
*newp
;
653 = undef_map
->l_reldepsmax
? undef_map
->l_reldepsmax
* 2 : 10;
655 #ifdef RTLD_PREPARE_FOREIGN_CALL
656 RTLD_PREPARE_FOREIGN_CALL
;
659 newp
= malloc (sizeof (*newp
) + max
* sizeof (struct link_map
*));
662 /* If we didn't manage to allocate memory for the list this is
663 no fatal problem. We simply make sure the referenced object
664 cannot be unloaded. This is semantically the correct
666 map
->l_flags_1
|= DF_1_NODELETE
;
672 memcpy (&newp
->list
[0], &undef_map
->l_reldeps
->list
[0],
673 l_reldepsact
* sizeof (struct link_map
*));
674 newp
->list
[l_reldepsact
] = map
;
675 newp
->act
= l_reldepsact
+ 1;
676 atomic_write_barrier ();
677 void *old
= undef_map
->l_reldeps
;
678 undef_map
->l_reldeps
= newp
;
679 undef_map
->l_reldepsmax
= max
;
681 _dl_scope_free (old
);
686 undef_map
->l_reldeps
->list
[l_reldepsact
] = map
;
687 atomic_write_barrier ();
688 undef_map
->l_reldeps
->act
= l_reldepsact
+ 1;
691 /* Display information if we are debugging. */
692 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
))
694 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
695 DSO_FILENAME (map
->l_name
),
697 DSO_FILENAME (undef_map
->l_name
),
701 /* Whoa, that was bad luck. We have to search again. */
705 /* Release the lock. */
706 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
708 if (__glibc_unlikely (flags
& DL_LOOKUP_GSCOPE_LOCK
))
709 THREAD_GSCOPE_SET_FLAG ();
714 if (map
->l_serial
!= serial
)
721 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
722 const ElfW(Sym
) **ref
, struct sym_val
*value
,
723 const struct r_found_version
*version
, int type_class
,
727 /* Search loaded objects' symbol tables for a definition of the symbol
728 UNDEF_NAME, perhaps with a requested version for the symbol.
730 We must never have calls to the audit functions inside this function
731 or in any function which gets called. If this would happen the audit
732 code might create a thread which can throw off all the scope locking. */
735 _dl_lookup_symbol_x (const char *undef_name
, struct link_map
*undef_map
,
736 const ElfW(Sym
) **ref
,
737 struct r_scope_elem
*symbol_scope
[],
738 const struct r_found_version
*version
,
739 int type_class
, int flags
, struct link_map
*skip_map
)
741 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
742 unsigned long int old_hash
= 0xffffffff;
743 struct sym_val current_value
= { NULL
, NULL
};
744 struct r_scope_elem
**scope
= symbol_scope
;
746 bump_num_relocations ();
748 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
749 is allowed if we look up a versioned symbol. */
750 assert (version
== NULL
751 || (flags
& ~(DL_LOOKUP_ADD_DEPENDENCY
| DL_LOOKUP_GSCOPE_LOCK
))
755 if (__glibc_unlikely (skip_map
!= NULL
))
756 /* Search the relevant loaded objects for a definition. */
757 while ((*scope
)->r_list
[i
] != skip_map
)
760 /* Search the relevant loaded objects for a definition. */
761 for (size_t start
= i
; *scope
!= NULL
; start
= 0, ++scope
)
763 int res
= do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
764 ¤t_value
, *scope
, start
, version
, flags
,
765 skip_map
, type_class
, undef_map
);
769 if (__builtin_expect (res
, 0) < 0 && skip_map
== NULL
)
771 /* Oh, oh. The file named in the relocation entry does not
772 contain the needed symbol. This code is never reached
773 for unversioned lookups. */
774 assert (version
!= NULL
);
775 const char *reference_name
= undef_map
? undef_map
->l_name
: "";
777 /* XXX We cannot translate the message. */
778 _dl_signal_cerror (0, DSO_FILENAME (reference_name
),
779 N_("relocation error"),
780 make_string ("symbol ", undef_name
, ", version ",
782 " not defined in file ",
784 " with link time reference",
786 ? " (no version symbols)" : ""));
792 if (__glibc_unlikely (current_value
.s
== NULL
))
794 if ((*ref
== NULL
|| ELFW(ST_BIND
) ((*ref
)->st_info
) != STB_WEAK
)
796 && !(GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
))
798 /* We could find no value for a strong reference. */
799 const char *reference_name
= undef_map
? undef_map
->l_name
: "";
800 const char *versionstr
= version
? ", version " : "";
801 const char *versionname
= (version
&& version
->name
802 ? version
->name
: "");
804 /* XXX We cannot translate the message. */
805 _dl_signal_cerror (0, DSO_FILENAME (reference_name
),
806 N_("symbol lookup error"),
807 make_string (undefined_msg
, undef_name
,
808 versionstr
, versionname
));
814 int protected = (*ref
815 && ELFW(ST_VISIBILITY
) ((*ref
)->st_other
) == STV_PROTECTED
);
816 if (__glibc_unlikely (protected != 0))
818 /* It is very tricky. We need to figure out what value to
819 return for the protected symbol. */
820 if (type_class
== ELF_RTYPE_CLASS_PLT
)
822 if (current_value
.s
!= NULL
&& current_value
.m
!= undef_map
)
824 current_value
.s
= *ref
;
825 current_value
.m
= undef_map
;
830 struct sym_val protected_value
= { NULL
, NULL
};
832 for (scope
= symbol_scope
; *scope
!= NULL
; i
= 0, ++scope
)
833 if (do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
834 &protected_value
, *scope
, i
, version
, flags
,
835 skip_map
, ELF_RTYPE_CLASS_PLT
, NULL
) != 0)
838 if (protected_value
.s
!= NULL
&& protected_value
.m
!= undef_map
)
840 current_value
.s
= *ref
;
841 current_value
.m
= undef_map
;
846 /* We have to check whether this would bind UNDEF_MAP to an object
847 in the global scope which was dynamically loaded. In this case
848 we have to prevent the latter from being unloaded unless the
849 UNDEF_MAP object is also unloaded. */
850 if (__builtin_expect (current_value
.m
->l_type
== lt_loaded
, 0)
851 /* Don't do this for explicit lookups as opposed to implicit
853 && (flags
& DL_LOOKUP_ADD_DEPENDENCY
) != 0
854 /* Add UNDEF_MAP to the dependencies. */
855 && add_dependency (undef_map
, current_value
.m
, flags
) < 0)
856 /* Something went wrong. Perhaps the object we tried to reference
857 was just removed. Try finding another definition. */
858 return _dl_lookup_symbol_x (undef_name
, undef_map
, ref
,
859 (flags
& DL_LOOKUP_GSCOPE_LOCK
)
860 ? undef_map
->l_scope
: symbol_scope
,
861 version
, type_class
, flags
, skip_map
);
863 /* The object is used. */
864 if (__glibc_unlikely (current_value
.m
->l_used
== 0))
865 current_value
.m
->l_used
= 1;
867 if (__builtin_expect (GLRO(dl_debug_mask
)
868 & (DL_DEBUG_BINDINGS
|DL_DEBUG_PRELINK
), 0))
869 _dl_debug_bindings (undef_name
, undef_map
, ref
,
870 ¤t_value
, version
, type_class
, protected);
872 *ref
= current_value
.s
;
873 return LOOKUP_VALUE (current_value
.m
);
877 /* Cache the location of MAP's hash table. */
881 _dl_setup_hash (struct link_map
*map
)
885 if (__builtin_expect (map
->l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
886 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
887 + DT_EXTRANUM
+ DT_VALNUM
] != NULL
, 1))
890 = (void *) D_PTR (map
, l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
891 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
892 + DT_EXTRANUM
+ DT_VALNUM
]);
893 map
->l_nbuckets
= *hash32
++;
894 Elf32_Word symbias
= *hash32
++;
895 Elf32_Word bitmask_nwords
= *hash32
++;
896 /* Must be a power of two. */
897 assert ((bitmask_nwords
& (bitmask_nwords
- 1)) == 0);
898 map
->l_gnu_bitmask_idxbits
= bitmask_nwords
- 1;
899 map
->l_gnu_shift
= *hash32
++;
901 map
->l_gnu_bitmask
= (ElfW(Addr
) *) hash32
;
902 hash32
+= __ELF_NATIVE_CLASS
/ 32 * bitmask_nwords
;
904 map
->l_gnu_buckets
= hash32
;
905 hash32
+= map
->l_nbuckets
;
906 map
->l_gnu_chain_zero
= hash32
- symbias
;
910 if (!map
->l_info
[DT_HASH
])
912 hash
= (void *) D_PTR (map
, l_info
[DT_HASH
]);
914 map
->l_nbuckets
= *hash
++;
917 map
->l_buckets
= hash
;
918 hash
+= map
->l_nbuckets
;
925 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
926 const ElfW(Sym
) **ref
, struct sym_val
*value
,
927 const struct r_found_version
*version
, int type_class
,
930 const char *reference_name
= undef_map
->l_name
;
932 if (GLRO(dl_debug_mask
) & DL_DEBUG_BINDINGS
)
934 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
935 DSO_FILENAME (reference_name
),
937 DSO_FILENAME (value
->m
->l_name
),
939 protected ? "protected" : "normal", undef_name
);
941 _dl_debug_printf_c (" [%s]\n", version
->name
);
943 _dl_debug_printf_c ("\n");
946 if (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
949 struct sym_val val
= { NULL
, NULL
};
951 if ((GLRO(dl_trace_prelink_map
) == NULL
952 || GLRO(dl_trace_prelink_map
) == GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
953 && undef_map
!= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
955 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
956 unsigned long int old_hash
= 0xffffffff;
957 struct unique_sym
*saved_entries
958 = GL(dl_ns
)[LM_ID_BASE
]._ns_unique_sym_table
.entries
;
960 GL(dl_ns
)[LM_ID_BASE
]._ns_unique_sym_table
.entries
= NULL
;
961 do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
, &val
,
962 undef_map
->l_local_scope
[0], 0, version
, 0, NULL
,
963 type_class
, undef_map
);
964 if (val
.s
!= value
->s
|| val
.m
!= value
->m
)
966 else if (__builtin_expect (undef_map
->l_symbolic_in_local_scope
, 0)
968 && __builtin_expect (ELFW(ST_BIND
) (val
.s
->st_info
),
969 STB_GLOBAL
) == STB_GNU_UNIQUE
)
971 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
972 contains any DT_SYMBOLIC libraries, unfortunately there
973 can be conflicts even if the above is equal. As symbol
974 resolution goes from the last library to the first and
975 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
976 library, it would be the one that is looked up. */
977 struct sym_val val2
= { NULL
, NULL
};
979 struct r_scope_elem
*scope
= undef_map
->l_local_scope
[0];
981 for (n
= 0; n
< scope
->r_nlist
; n
++)
982 if (scope
->r_list
[n
] == val
.m
)
985 for (n
++; n
< scope
->r_nlist
; n
++)
986 if (scope
->r_list
[n
]->l_info
[DT_SYMBOLIC
] != NULL
987 && do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
989 &scope
->r_list
[n
]->l_symbolic_searchlist
,
990 0, version
, 0, NULL
, type_class
,
998 GL(dl_ns
)[LM_ID_BASE
]._ns_unique_sym_table
.entries
= saved_entries
;
1003 if (__builtin_expect (ELFW(ST_TYPE
) (value
->s
->st_info
)
1006 else if (__builtin_expect (ELFW(ST_TYPE
) (value
->s
->st_info
)
1007 == STT_GNU_IFUNC
, 0))
1012 || GLRO(dl_trace_prelink_map
) == undef_map
1013 || GLRO(dl_trace_prelink_map
) == NULL
1016 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1017 conflict
? "conflict" : "lookup",
1018 (int) sizeof (ElfW(Addr
)) * 2,
1019 (size_t) undef_map
->l_map_start
,
1020 (int) sizeof (ElfW(Addr
)) * 2,
1021 (size_t) (((ElfW(Addr
)) *ref
) - undef_map
->l_map_start
),
1022 (int) sizeof (ElfW(Addr
)) * 2,
1023 (size_t) (value
->s
? value
->m
->l_map_start
: 0),
1024 (int) sizeof (ElfW(Addr
)) * 2,
1025 (size_t) (value
->s
? value
->s
->st_value
: 0));
1028 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1029 (int) sizeof (ElfW(Addr
)) * 2,
1030 (size_t) (val
.s
? val
.m
->l_map_start
: 0),
1031 (int) sizeof (ElfW(Addr
)) * 2,
1032 (size_t) (val
.s
? val
.s
->st_value
: 0));
1034 _dl_printf ("/%x %s\n", type_class
, undef_name
);