1 /* Look up a symbol in the loaded objects.
2 MIPS/Linux version - special handling of non-PIC undefined symbol rules.
3 Copyright (C) 1995-2013 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
27 #include <dl-machine.h>
28 #include <sysdep-cancel.h>
29 #include <bits/libc-lock.h>
35 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
37 /* We need this string more than once. */
38 static const char undefined_msg
[] = "undefined symbol: ";
48 #define make_string(string, rest...) \
50 const char *all[] = { string, ## rest }; \
55 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
56 len += strlen (all[cnt]); \
58 cp = result = alloca (len); \
59 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
60 cp = __stpcpy (cp, all[cnt]); \
65 /* Statistics function. */
67 # define bump_num_relocations() ++GL(dl_num_relocations)
69 # define bump_num_relocations() ((void) 0)
73 /* Inner part of the lookup functions. We return a value > 0 if we
74 found the symbol, the value 0 if nothing is found and < 0 if
75 something bad happened. */
77 __attribute_noinline__
78 do_lookup_x (const char *undef_name
, uint_fast32_t new_hash
,
79 unsigned long int *old_hash
, const ElfW(Sym
) *ref
,
80 struct sym_val
*result
, struct r_scope_elem
*scope
, size_t i
,
81 const struct r_found_version
*const version
, int flags
,
82 struct link_map
*skip
, int type_class
, struct link_map
*undef_map
)
84 size_t n
= scope
->r_nlist
;
85 /* Make sure we read the value before proceeding. Otherwise we
86 might use r_list pointing to the initial scope and r_nlist being
87 the value after a resize. That is the only path in dl-open.c not
88 protected by GSCOPE. A read barrier here might be to expensive. */
89 __asm
volatile ("" : "+r" (n
), "+m" (scope
->r_list
));
90 struct link_map
**list
= scope
->r_list
;
94 /* These variables are used in the nested function. */
97 const ElfW(Sym
) *versioned_sym
= NULL
;
99 const struct link_map
*map
= list
[i
]->l_real
;
101 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
105 /* Don't search the executable when resolving a copy reloc. */
106 if ((type_class
& ELF_RTYPE_CLASS_COPY
) && map
->l_type
== lt_executable
)
109 /* Do not look into objects which are going to be removed. */
113 /* Print some debugging info if wanted. */
114 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_SYMBOLS
, 0))
115 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
117 map
->l_name
[0] ? map
->l_name
: rtld_progname
,
120 /* If the hash table is empty there is nothing to do here. */
121 if (map
->l_nbuckets
== 0)
124 /* The tables for this map. */
125 const ElfW(Sym
) *symtab
= (const void *) D_PTR (map
, l_info
[DT_SYMTAB
]);
126 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
129 /* Nested routine to check whether the symbol matches. */
131 __attribute_noinline__
132 check_match (const ElfW(Sym
) *sym
)
134 unsigned int stt
= ELFW(ST_TYPE
) (sym
->st_info
);
135 assert (ELF_RTYPE_CLASS_PLT
== 1);
136 /* The semantics of zero/non-zero values of undefined symbols
137 differs depending on whether the non-PIC ABI is in use.
138 Under the non-PIC ABI, a non-zero value indicates that
139 there is an address reference to the symbol and thus it
140 must always be resolved (except when resolving a jump slot
141 relocation) to the PLT entry whose address is provided as
142 the symbol's value; a zero value indicates that this
143 canonical-address behaviour is not required. Yet under the
144 classic MIPS psABI, a zero value indicates that there is an
145 address reference to the function and the dynamic linker
146 must resolve the symbol immediately upon loading. To avoid
147 conflict, symbols for which the dynamic linker must assume
148 the non-PIC ABI semantics are marked with the STO_MIPS_PLT
150 if (__builtin_expect ((sym
->st_value
== 0 /* No value. */
152 || (sym
->st_shndx
== SHN_UNDEF
153 && !(sym
->st_other
& STO_MIPS_PLT
))
154 || (type_class
& (sym
->st_shndx
== SHN_UNDEF
)),
158 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
159 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
160 code/data definitions. */
161 #define ALLOWED_STT \
162 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
163 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
164 if (__builtin_expect (((1 << stt
) & ALLOWED_STT
) == 0, 0))
167 if (sym
!= ref
&& strcmp (strtab
+ sym
->st_name
, undef_name
))
168 /* Not the symbol we are looking for. */
171 const ElfW(Half
) *verstab
= map
->l_versyms
;
174 if (__builtin_expect (verstab
== NULL
, 0))
176 /* We need a versioned symbol but haven't found any. If
177 this is the object which is referenced in the verneed
178 entry it is a bug in the library since a symbol must
179 not simply disappear.
181 It would also be a bug in the object since it means that
182 the list of required versions is incomplete and so the
183 tests in dl-version.c haven't found a problem.*/
184 assert (version
->filename
== NULL
185 || ! _dl_name_match_p (version
->filename
, map
));
187 /* Otherwise we accept the symbol. */
191 /* We can match the version information or use the
192 default one if it is not hidden. */
193 ElfW(Half
) ndx
= verstab
[symidx
] & 0x7fff;
194 if ((map
->l_versions
[ndx
].hash
!= version
->hash
195 || strcmp (map
->l_versions
[ndx
].name
, version
->name
))
196 && (version
->hidden
|| map
->l_versions
[ndx
].hash
197 || (verstab
[symidx
] & 0x8000)))
198 /* It's not the version we want. */
204 /* No specific version is selected. There are two ways we
207 - a binary which does not include versioning information
210 - dlsym() instead of dlvsym() is used to get a symbol which
211 might exist in more than one form
213 If the library does not provide symbol version information
214 there is no problem at all: we simply use the symbol if it
217 These two lookups need to be handled differently if the
218 library defines versions. In the case of the old
219 unversioned application the oldest (default) version
220 should be used. In case of a dlsym() call the latest and
221 public interface should be returned. */
224 if ((verstab
[symidx
] & 0x7fff)
225 >= ((flags
& DL_LOOKUP_RETURN_NEWEST
) ? 2 : 3))
227 /* Don't accept hidden symbols. */
228 if ((verstab
[symidx
] & 0x8000) == 0
229 && num_versions
++ == 0)
230 /* No version so far. */
238 /* There cannot be another entry for this symbol so stop here. */
242 const ElfW(Sym
) *sym
;
243 const ElfW(Addr
) *bitmask
= map
->l_gnu_bitmask
;
244 if (__builtin_expect (bitmask
!= NULL
, 1))
246 ElfW(Addr
) bitmask_word
247 = bitmask
[(new_hash
/ __ELF_NATIVE_CLASS
)
248 & map
->l_gnu_bitmask_idxbits
];
250 unsigned int hashbit1
= new_hash
& (__ELF_NATIVE_CLASS
- 1);
251 unsigned int hashbit2
= ((new_hash
>> map
->l_gnu_shift
)
252 & (__ELF_NATIVE_CLASS
- 1));
254 if (__builtin_expect ((bitmask_word
>> hashbit1
)
255 & (bitmask_word
>> hashbit2
) & 1, 0))
257 Elf32_Word bucket
= map
->l_gnu_buckets
[new_hash
261 const Elf32_Word
*hasharr
= &map
->l_gnu_chain_zero
[bucket
];
264 if (((*hasharr
^ new_hash
) >> 1) == 0)
266 symidx
= hasharr
- map
->l_gnu_chain_zero
;
267 sym
= check_match (&symtab
[symidx
]);
271 while ((*hasharr
++ & 1u) == 0);
274 /* No symbol found. */
279 if (*old_hash
== 0xffffffff)
280 *old_hash
= _dl_elf_hash (undef_name
);
282 /* Use the old SysV-style hash table. Search the appropriate
283 hash bucket in this object's symbol table for a definition
284 for the same symbol name. */
285 for (symidx
= map
->l_buckets
[*old_hash
% map
->l_nbuckets
];
287 symidx
= map
->l_chain
[symidx
])
289 sym
= check_match (&symtab
[symidx
]);
295 /* If we have seen exactly one versioned symbol while we are
296 looking for an unversioned symbol and the version is not the
297 default version we still accept this symbol since there are
298 no possible ambiguities. */
299 sym
= num_versions
== 1 ? versioned_sym
: NULL
;
304 switch (__builtin_expect (ELFW(ST_BIND
) (sym
->st_info
), STB_GLOBAL
))
307 /* Weak definition. Use this value if we don't find another. */
308 if (__builtin_expect (GLRO(dl_dynamic_weak
), 0))
313 result
->m
= (struct link_map
*) map
;
320 /* Global definition. Just what we need. */
322 result
->m
= (struct link_map
*) map
;
325 case STB_GNU_UNIQUE
:;
326 /* We have to determine whether we already found a
327 symbol with this name before. If not then we have to
328 add it to the search table. If we already found a
329 definition we have to use it. */
330 void enter (struct unique_sym
*table
, size_t size
,
331 unsigned int hash
, const char *name
,
332 const ElfW(Sym
) *sym
, const struct link_map
*map
)
334 size_t idx
= hash
% size
;
335 size_t hash2
= 1 + hash
% (size
- 2);
336 while (table
[idx
].name
!= NULL
)
343 table
[idx
].hashval
= hash
;
344 table
[idx
].name
= name
;
345 table
[idx
].sym
= sym
;
346 table
[idx
].map
= map
;
349 struct unique_sym_table
*tab
350 = &GL(dl_ns
)[map
->l_ns
]._ns_unique_sym_table
;
352 __rtld_lock_lock_recursive (tab
->lock
);
354 struct unique_sym
*entries
= tab
->entries
;
355 size_t size
= tab
->size
;
358 size_t idx
= new_hash
% size
;
359 size_t hash2
= 1 + new_hash
% (size
- 2);
362 if (entries
[idx
].hashval
== new_hash
363 && strcmp (entries
[idx
].name
, undef_name
) == 0)
365 if ((type_class
& ELF_RTYPE_CLASS_COPY
) != 0)
367 /* We possibly have to initialize the central
368 copy from the copy addressed through the
371 result
->m
= (struct link_map
*) map
;
375 result
->s
= entries
[idx
].sym
;
376 result
->m
= (struct link_map
*) entries
[idx
].map
;
378 __rtld_lock_unlock_recursive (tab
->lock
);
382 if (entries
[idx
].name
== NULL
)
390 if (size
* 3 <= tab
->n_elements
* 4)
392 /* Expand the table. */
393 #ifdef RTLD_CHECK_FOREIGN_CALL
394 /* This must not happen during runtime relocations. */
395 assert (!RTLD_CHECK_FOREIGN_CALL
);
397 size_t newsize
= _dl_higher_prime_number (size
+ 1);
398 struct unique_sym
*newentries
399 = calloc (sizeof (struct unique_sym
), newsize
);
400 if (newentries
== NULL
)
403 __rtld_lock_unlock_recursive (tab
->lock
);
404 _dl_fatal_printf ("out of memory\n");
407 for (idx
= 0; idx
< size
; ++idx
)
408 if (entries
[idx
].name
!= NULL
)
409 enter (newentries
, newsize
, entries
[idx
].hashval
,
410 entries
[idx
].name
, entries
[idx
].sym
,
416 entries
= tab
->entries
= newentries
;
422 #ifdef RTLD_CHECK_FOREIGN_CALL
423 /* This must not happen during runtime relocations. */
424 assert (!RTLD_CHECK_FOREIGN_CALL
);
428 /* If tab->entries is NULL, but tab->size is not, it means
429 this is the second, conflict finding, lookup for
430 LD_TRACE_PRELINKING in _dl_debug_bindings. Don't
431 allocate anything and don't enter anything into the
433 if (__builtin_expect (tab
->size
, 0))
435 assert (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
);
436 __rtld_lock_unlock_recursive (tab
->lock
);
441 #define INITIAL_NUNIQUE_SYM_TABLE 31
442 size
= INITIAL_NUNIQUE_SYM_TABLE
;
443 entries
= calloc (sizeof (struct unique_sym
), size
);
447 tab
->entries
= entries
;
452 if ((type_class
& ELF_RTYPE_CLASS_COPY
) != 0)
453 enter (entries
, size
, new_hash
, strtab
+ sym
->st_name
, ref
,
457 enter (entries
, size
, new_hash
, strtab
+ sym
->st_name
, sym
,
460 if (map
->l_type
== lt_loaded
)
461 /* Make sure we don't unload this object by
462 setting the appropriate flag. */
463 ((struct link_map
*) map
)->l_flags_1
|= DF_1_NODELETE
;
467 __rtld_lock_unlock_recursive (tab
->lock
);
472 /* Local symbols are ignored. */
477 /* If this current map is the one mentioned in the verneed entry
478 and we have not found a weak entry, it is a bug. */
479 if (symidx
== STN_UNDEF
&& version
!= NULL
&& version
->filename
!= NULL
480 && __builtin_expect (_dl_name_match_p (version
->filename
, map
), 0))
485 /* We have not found anything until now. */
491 dl_new_hash (const char *s
)
493 uint_fast32_t h
= 5381;
494 for (unsigned char c
= *s
; c
!= '\0'; c
= *++s
)
496 return h
& 0xffffffff;
500 /* Add extra dependency on MAP to UNDEF_MAP. */
503 add_dependency (struct link_map
*undef_map
, struct link_map
*map
, int flags
)
505 struct link_map
*runp
;
509 /* Avoid self-references and references to objects which cannot be
511 if (undef_map
== map
)
514 /* Avoid references to objects which cannot be unloaded anyway. */
515 assert (map
->l_type
== lt_loaded
);
516 if ((map
->l_flags_1
& DF_1_NODELETE
) != 0)
519 struct link_map_reldeps
*l_reldeps
520 = atomic_forced_read (undef_map
->l_reldeps
);
522 /* Make sure l_reldeps is read before l_initfini. */
523 atomic_read_barrier ();
525 /* Determine whether UNDEF_MAP already has a reference to MAP. First
526 look in the normal dependencies. */
527 struct link_map
**l_initfini
= atomic_forced_read (undef_map
->l_initfini
);
528 if (l_initfini
!= NULL
)
530 for (i
= 0; l_initfini
[i
] != NULL
; ++i
)
531 if (l_initfini
[i
] == map
)
535 /* No normal dependency. See whether we already had to add it
536 to the special list of dynamic dependencies. */
537 unsigned int l_reldepsact
= 0;
538 if (l_reldeps
!= NULL
)
540 struct link_map
**list
= &l_reldeps
->list
[0];
541 l_reldepsact
= l_reldeps
->act
;
542 for (i
= 0; i
< l_reldepsact
; ++i
)
547 /* Save serial number of the target MAP. */
548 unsigned long long serial
= map
->l_serial
;
550 /* Make sure nobody can unload the object while we are at it. */
551 if (__builtin_expect (flags
& DL_LOOKUP_GSCOPE_LOCK
, 0))
553 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
554 here, that can result in ABBA deadlock. */
555 THREAD_GSCOPE_RESET_FLAG ();
556 __rtld_lock_lock_recursive (GL(dl_load_lock
));
557 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
558 it can e.g. point to unallocated memory. So avoid the optimizer
559 treating the above read from MAP->l_serial as ensurance it
560 can safely dereference it. */
561 map
= atomic_forced_read (map
);
563 /* From this point on it is unsafe to dereference MAP, until it
564 has been found in one of the lists. */
566 /* Redo the l_initfini check in case undef_map's l_initfini
567 changed in the mean time. */
568 if (undef_map
->l_initfini
!= l_initfini
569 && undef_map
->l_initfini
!= NULL
)
571 l_initfini
= undef_map
->l_initfini
;
572 for (i
= 0; l_initfini
[i
] != NULL
; ++i
)
573 if (l_initfini
[i
] == map
)
577 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
579 if (undef_map
->l_reldeps
!= NULL
)
581 if (undef_map
->l_reldeps
!= l_reldeps
)
583 struct link_map
**list
= &undef_map
->l_reldeps
->list
[0];
584 l_reldepsact
= undef_map
->l_reldeps
->act
;
585 for (i
= 0; i
< l_reldepsact
; ++i
)
589 else if (undef_map
->l_reldeps
->act
> l_reldepsact
)
591 struct link_map
**list
592 = &undef_map
->l_reldeps
->list
[0];
594 l_reldepsact
= undef_map
->l_reldeps
->act
;
595 for (; i
< l_reldepsact
; ++i
)
602 __rtld_lock_lock_recursive (GL(dl_load_lock
));
604 /* The object is not yet in the dependency list. Before we add
605 it make sure just one more time the object we are about to
606 reference is still available. There is a brief period in
607 which the object could have been removed since we found the
609 runp
= GL(dl_ns
)[undef_map
->l_ns
]._ns_loaded
;
610 while (runp
!= NULL
&& runp
!= map
)
615 /* The object is still available. */
617 /* MAP could have been dlclosed, freed and then some other dlopened
618 library could have the same link_map pointer. */
619 if (map
->l_serial
!= serial
)
622 /* Redo the NODELETE check, as when dl_load_lock wasn't held
623 yet this could have changed. */
624 if ((map
->l_flags_1
& DF_1_NODELETE
) != 0)
627 /* If the object with the undefined reference cannot be removed ever
628 just make sure the same is true for the object which contains the
630 if (undef_map
->l_type
!= lt_loaded
631 || (undef_map
->l_flags_1
& DF_1_NODELETE
) != 0)
633 map
->l_flags_1
|= DF_1_NODELETE
;
637 /* Add the reference now. */
638 if (__builtin_expect (l_reldepsact
>= undef_map
->l_reldepsmax
, 0))
640 /* Allocate more memory for the dependency list. Since this
641 can never happen during the startup phase we can use
643 struct link_map_reldeps
*newp
;
645 = undef_map
->l_reldepsmax
? undef_map
->l_reldepsmax
* 2 : 10;
647 #ifdef RTLD_PREPARE_FOREIGN_CALL
648 RTLD_PREPARE_FOREIGN_CALL
;
651 newp
= malloc (sizeof (*newp
) + max
* sizeof (struct link_map
*));
654 /* If we didn't manage to allocate memory for the list this is
655 no fatal problem. We simply make sure the referenced object
656 cannot be unloaded. This is semantically the correct
658 map
->l_flags_1
|= DF_1_NODELETE
;
664 memcpy (&newp
->list
[0], &undef_map
->l_reldeps
->list
[0],
665 l_reldepsact
* sizeof (struct link_map
*));
666 newp
->list
[l_reldepsact
] = map
;
667 newp
->act
= l_reldepsact
+ 1;
668 atomic_write_barrier ();
669 void *old
= undef_map
->l_reldeps
;
670 undef_map
->l_reldeps
= newp
;
671 undef_map
->l_reldepsmax
= max
;
673 _dl_scope_free (old
);
678 undef_map
->l_reldeps
->list
[l_reldepsact
] = map
;
679 atomic_write_barrier ();
680 undef_map
->l_reldeps
->act
= l_reldepsact
+ 1;
683 /* Display information if we are debugging. */
684 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
686 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
687 map
->l_name
[0] ? map
->l_name
: rtld_progname
,
690 ? undef_map
->l_name
: rtld_progname
,
694 /* Whoa, that was bad luck. We have to search again. */
698 /* Release the lock. */
699 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
701 if (__builtin_expect (flags
& DL_LOOKUP_GSCOPE_LOCK
, 0))
702 THREAD_GSCOPE_SET_FLAG ();
707 if (map
->l_serial
!= serial
)
714 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
715 const ElfW(Sym
) **ref
, struct sym_val
*value
,
716 const struct r_found_version
*version
, int type_class
,
720 /* Search loaded objects' symbol tables for a definition of the symbol
721 UNDEF_NAME, perhaps with a requested version for the symbol.
723 We must never have calls to the audit functions inside this function
724 or in any function which gets called. If this would happen the audit
725 code might create a thread which can throw off all the scope locking. */
728 _dl_lookup_symbol_x (const char *undef_name
, struct link_map
*undef_map
,
729 const ElfW(Sym
) **ref
,
730 struct r_scope_elem
*symbol_scope
[],
731 const struct r_found_version
*version
,
732 int type_class
, int flags
, struct link_map
*skip_map
)
734 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
735 unsigned long int old_hash
= 0xffffffff;
736 struct sym_val current_value
= { NULL
, NULL
};
737 struct r_scope_elem
**scope
= symbol_scope
;
739 bump_num_relocations ();
741 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
742 is allowed if we look up a versioned symbol. */
743 assert (version
== NULL
744 || (flags
& ~(DL_LOOKUP_ADD_DEPENDENCY
| DL_LOOKUP_GSCOPE_LOCK
))
748 if (__builtin_expect (skip_map
!= NULL
, 0))
749 /* Search the relevant loaded objects for a definition. */
750 while ((*scope
)->r_list
[i
] != skip_map
)
753 /* Search the relevant loaded objects for a definition. */
754 for (size_t start
= i
; *scope
!= NULL
; start
= 0, ++scope
)
756 int res
= do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
757 ¤t_value
, *scope
, start
, version
, flags
,
758 skip_map
, type_class
, undef_map
);
762 if (__builtin_expect (res
, 0) < 0 && skip_map
== NULL
)
764 /* Oh, oh. The file named in the relocation entry does not
765 contain the needed symbol. This code is never reached
766 for unversioned lookups. */
767 assert (version
!= NULL
);
768 const char *reference_name
= undef_map
? undef_map
->l_name
: NULL
;
770 /* XXX We cannot translate the message. */
771 _dl_signal_cerror (0, (reference_name
[0]
773 : (rtld_progname
?: "<main program>")),
774 N_("relocation error"),
775 make_string ("symbol ", undef_name
, ", version ",
777 " not defined in file ",
779 " with link time reference",
781 ? " (no version symbols)" : ""));
787 if (__builtin_expect (current_value
.s
== NULL
, 0))
789 if ((*ref
== NULL
|| ELFW(ST_BIND
) ((*ref
)->st_info
) != STB_WEAK
)
791 && !(GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
))
793 /* We could find no value for a strong reference. */
794 const char *reference_name
= undef_map
? undef_map
->l_name
: "";
795 const char *versionstr
= version
? ", version " : "";
796 const char *versionname
= (version
&& version
->name
797 ? version
->name
: "");
799 /* XXX We cannot translate the message. */
800 _dl_signal_cerror (0, (reference_name
[0]
802 : (rtld_progname
?: "<main program>")),
803 N_("symbol lookup error"),
804 make_string (undefined_msg
, undef_name
,
805 versionstr
, versionname
));
811 int protected = (*ref
812 && ELFW(ST_VISIBILITY
) ((*ref
)->st_other
) == STV_PROTECTED
);
813 if (__builtin_expect (protected != 0, 0))
815 /* It is very tricky. We need to figure out what value to
816 return for the protected symbol. */
817 if (type_class
== ELF_RTYPE_CLASS_PLT
)
819 if (current_value
.s
!= NULL
&& current_value
.m
!= undef_map
)
821 current_value
.s
= *ref
;
822 current_value
.m
= undef_map
;
827 struct sym_val protected_value
= { NULL
, NULL
};
829 for (scope
= symbol_scope
; *scope
!= NULL
; i
= 0, ++scope
)
830 if (do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
831 &protected_value
, *scope
, i
, version
, flags
,
832 skip_map
, ELF_RTYPE_CLASS_PLT
, NULL
) != 0)
835 if (protected_value
.s
!= NULL
&& protected_value
.m
!= undef_map
)
837 current_value
.s
= *ref
;
838 current_value
.m
= undef_map
;
843 /* We have to check whether this would bind UNDEF_MAP to an object
844 in the global scope which was dynamically loaded. In this case
845 we have to prevent the latter from being unloaded unless the
846 UNDEF_MAP object is also unloaded. */
847 if (__builtin_expect (current_value
.m
->l_type
== lt_loaded
, 0)
848 /* Don't do this for explicit lookups as opposed to implicit
850 && (flags
& DL_LOOKUP_ADD_DEPENDENCY
) != 0
851 /* Add UNDEF_MAP to the dependencies. */
852 && add_dependency (undef_map
, current_value
.m
, flags
) < 0)
853 /* Something went wrong. Perhaps the object we tried to reference
854 was just removed. Try finding another definition. */
855 return _dl_lookup_symbol_x (undef_name
, undef_map
, ref
,
856 (flags
& DL_LOOKUP_GSCOPE_LOCK
)
857 ? undef_map
->l_scope
: symbol_scope
,
858 version
, type_class
, flags
, skip_map
);
860 /* The object is used. */
861 if (__builtin_expect (current_value
.m
->l_used
== 0, 0))
862 current_value
.m
->l_used
= 1;
864 if (__builtin_expect (GLRO(dl_debug_mask
)
865 & (DL_DEBUG_BINDINGS
|DL_DEBUG_PRELINK
), 0))
866 _dl_debug_bindings (undef_name
, undef_map
, ref
,
867 ¤t_value
, version
, type_class
, protected);
869 *ref
= current_value
.s
;
870 return LOOKUP_VALUE (current_value
.m
);
874 /* Cache the location of MAP's hash table. */
878 _dl_setup_hash (struct link_map
*map
)
882 if (__builtin_expect (map
->l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
883 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
884 + DT_EXTRANUM
+ DT_VALNUM
] != NULL
, 1))
887 = (void *) D_PTR (map
, l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
888 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
889 + DT_EXTRANUM
+ DT_VALNUM
]);
890 map
->l_nbuckets
= *hash32
++;
891 Elf32_Word symbias
= *hash32
++;
892 Elf32_Word bitmask_nwords
= *hash32
++;
893 /* Must be a power of two. */
894 assert ((bitmask_nwords
& (bitmask_nwords
- 1)) == 0);
895 map
->l_gnu_bitmask_idxbits
= bitmask_nwords
- 1;
896 map
->l_gnu_shift
= *hash32
++;
898 map
->l_gnu_bitmask
= (ElfW(Addr
) *) hash32
;
899 hash32
+= __ELF_NATIVE_CLASS
/ 32 * bitmask_nwords
;
901 map
->l_gnu_buckets
= hash32
;
902 hash32
+= map
->l_nbuckets
;
903 map
->l_gnu_chain_zero
= hash32
- symbias
;
907 if (!map
->l_info
[DT_HASH
])
909 hash
= (void *) D_PTR (map
, l_info
[DT_HASH
]);
911 map
->l_nbuckets
= *hash
++;
914 map
->l_buckets
= hash
;
915 hash
+= map
->l_nbuckets
;
922 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
923 const ElfW(Sym
) **ref
, struct sym_val
*value
,
924 const struct r_found_version
*version
, int type_class
,
927 const char *reference_name
= undef_map
->l_name
;
929 if (GLRO(dl_debug_mask
) & DL_DEBUG_BINDINGS
)
931 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
934 : (rtld_progname
?: "<main program>")),
936 value
->m
->l_name
[0] ? value
->m
->l_name
: rtld_progname
,
938 protected ? "protected" : "normal", undef_name
);
940 _dl_debug_printf_c (" [%s]\n", version
->name
);
942 _dl_debug_printf_c ("\n");
945 if (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
948 struct sym_val val
= { NULL
, NULL
};
950 if ((GLRO(dl_trace_prelink_map
) == NULL
951 || GLRO(dl_trace_prelink_map
) == GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
952 && undef_map
!= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
954 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
955 unsigned long int old_hash
= 0xffffffff;
956 struct unique_sym
*saved_entries
957 = GL(dl_ns
)[LM_ID_BASE
]._ns_unique_sym_table
.entries
;
959 GL(dl_ns
)[LM_ID_BASE
]._ns_unique_sym_table
.entries
= NULL
;
960 do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
, &val
,
961 undef_map
->l_local_scope
[0], 0, version
, 0, NULL
,
962 type_class
, undef_map
);
963 if (val
.s
!= value
->s
|| val
.m
!= value
->m
)
965 else if (__builtin_expect (undef_map
->l_symbolic_in_local_scope
, 0)
967 && __builtin_expect (ELFW(ST_BIND
) (val
.s
->st_info
),
968 STB_GLOBAL
) == STB_GNU_UNIQUE
)
970 /* If it is STB_GNU_UNIQUE and undef_map's l_local_scope
971 contains any DT_SYMBOLIC libraries, unfortunately there
972 can be conflicts even if the above is equal. As symbol
973 resolution goes from the last library to the first and
974 if a STB_GNU_UNIQUE symbol is found in some late DT_SYMBOLIC
975 library, it would be the one that is looked up. */
976 struct sym_val val2
= { NULL
, NULL
};
978 struct r_scope_elem
*scope
= undef_map
->l_local_scope
[0];
980 for (n
= 0; n
< scope
->r_nlist
; n
++)
981 if (scope
->r_list
[n
] == val
.m
)
984 for (n
++; n
< scope
->r_nlist
; n
++)
985 if (scope
->r_list
[n
]->l_info
[DT_SYMBOLIC
] != NULL
986 && do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
988 &scope
->r_list
[n
]->l_symbolic_searchlist
,
989 0, version
, 0, NULL
, type_class
,
997 GL(dl_ns
)[LM_ID_BASE
]._ns_unique_sym_table
.entries
= saved_entries
;
1002 if (__builtin_expect (ELFW(ST_TYPE
) (value
->s
->st_info
)
1005 else if (__builtin_expect (ELFW(ST_TYPE
) (value
->s
->st_info
)
1006 == STT_GNU_IFUNC
, 0))
1011 || GLRO(dl_trace_prelink_map
) == undef_map
1012 || GLRO(dl_trace_prelink_map
) == NULL
1015 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
1016 conflict
? "conflict" : "lookup",
1017 (int) sizeof (ElfW(Addr
)) * 2,
1018 (size_t) undef_map
->l_map_start
,
1019 (int) sizeof (ElfW(Addr
)) * 2,
1020 (size_t) (((ElfW(Addr
)) *ref
) - undef_map
->l_map_start
),
1021 (int) sizeof (ElfW(Addr
)) * 2,
1022 (size_t) (value
->s
? value
->m
->l_map_start
: 0),
1023 (int) sizeof (ElfW(Addr
)) * 2,
1024 (size_t) (value
->s
? value
->s
->st_value
: 0));
1027 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
1028 (int) sizeof (ElfW(Addr
)) * 2,
1029 (size_t) (val
.s
? val
.m
->l_map_start
: 0),
1030 (int) sizeof (ElfW(Addr
)) * 2,
1031 (size_t) (val
.s
? val
.s
->st_value
: 0));
1033 _dl_printf ("/%x %s\n", type_class
, undef_name
);