1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2024 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 <https://www.gnu.org/licenses/>. */
25 #include <dl-machine.h>
26 #include <dl-new-hash.h>
27 #include <dl-protected.h>
28 #include <sysdep-cancel.h>
29 #include <libc-lock.h>
32 #include <elf_machine_sym_no_match.h>
36 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
45 /* Statistics function. */
47 # define bump_num_relocations() ++GL(dl_num_relocations)
49 # define bump_num_relocations() ((void) 0)
52 /* Utility function for do_lookup_x. The caller is called with undef_name,
53 ref, version, flags and type_class, and those are passed as the first
54 five arguments. The caller then computes sym, symidx, strtab, and map
55 and passes them as the next four arguments. Lastly the caller passes in
56 versioned_sym and num_versions which are modified by check_match during
57 the checking process. */
58 static const ElfW(Sym
) *
59 check_match (const char *const undef_name
,
60 const ElfW(Sym
) *const ref
,
61 const struct r_found_version
*const version
,
64 const ElfW(Sym
) *const sym
,
65 const Elf_Symndx symidx
,
66 const char *const strtab
,
67 const struct link_map
*const map
,
68 const ElfW(Sym
) **const versioned_sym
,
69 int *const num_versions
)
71 unsigned int stt
= ELFW(ST_TYPE
) (sym
->st_info
);
72 assert (ELF_RTYPE_CLASS_PLT
== 1);
73 if (__glibc_unlikely ((sym
->st_value
== 0 /* No value. */
74 && sym
->st_shndx
!= SHN_ABS
76 || elf_machine_sym_no_match (sym
)
77 || (type_class
& (sym
->st_shndx
== SHN_UNDEF
))))
80 /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
81 STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
82 code/data definitions. */
84 ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
85 | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
86 if (__glibc_unlikely (((1 << stt
) & ALLOWED_STT
) == 0))
89 if (sym
!= ref
&& strcmp (strtab
+ sym
->st_name
, undef_name
))
90 /* Not the symbol we are looking for. */
93 const ElfW(Half
) *verstab
= map
->l_versyms
;
96 if (__glibc_unlikely (verstab
== NULL
))
98 /* We need a versioned symbol but haven't found any. If
99 this is the object which is referenced in the verneed
100 entry it is a bug in the library since a symbol must
101 not simply disappear.
103 It would also be a bug in the object since it means that
104 the list of required versions is incomplete and so the
105 tests in dl-version.c haven't found a problem.*/
106 assert (version
->filename
== NULL
107 || ! _dl_name_match_p (version
->filename
, map
));
109 /* Otherwise we accept the symbol. */
113 /* We can match the version information or use the
114 default one if it is not hidden. */
115 ElfW(Half
) ndx
= verstab
[symidx
] & 0x7fff;
116 if ((map
->l_versions
[ndx
].hash
!= version
->hash
117 || strcmp (map
->l_versions
[ndx
].name
, version
->name
))
118 && (version
->hidden
|| map
->l_versions
[ndx
].hash
119 || (verstab
[symidx
] & 0x8000)))
120 /* It's not the version we want. */
126 /* No specific version is selected. There are two ways we
129 - a binary which does not include versioning information
132 - dlsym() instead of dlvsym() is used to get a symbol which
133 might exist in more than one form
135 If the library does not provide symbol version information
136 there is no problem at all: we simply use the symbol if it
139 These two lookups need to be handled differently if the
140 library defines versions. In the case of the old
141 unversioned application the oldest (default) version
142 should be used. In case of a dlsym() call the latest and
143 public interface should be returned. */
146 if ((verstab
[symidx
] & 0x7fff)
147 >= ((flags
& DL_LOOKUP_RETURN_NEWEST
) ? 2 : 3))
149 /* Don't accept hidden symbols. */
150 if ((verstab
[symidx
] & 0x8000) == 0
151 && (*num_versions
)++ == 0)
152 /* No version so far. */
153 *versioned_sym
= sym
;
160 /* There cannot be another entry for this symbol so stop here. */
164 /* Utility function for do_lookup_unique. Add a symbol to TABLE. */
166 enter_unique_sym (struct unique_sym
*table
, size_t size
,
167 unsigned int hash
, const char *name
,
168 const ElfW(Sym
) *sym
, const struct link_map
*map
)
170 size_t idx
= hash
% size
;
171 size_t hash2
= 1 + hash
% (size
- 2);
172 while (table
[idx
].name
!= NULL
)
179 table
[idx
].hashval
= hash
;
180 table
[idx
].name
= name
;
181 table
[idx
].sym
= sym
;
182 table
[idx
].map
= map
;
185 /* Mark MAP as NODELETE according to the lookup mode in FLAGS. During
186 initial relocation, NODELETE state is pending only. */
188 mark_nodelete (struct link_map
*map
, int flags
)
190 if (flags
& DL_LOOKUP_FOR_RELOCATE
)
191 map
->l_nodelete_pending
= true;
193 map
->l_nodelete_active
= true;
196 /* Return true if MAP is marked as NODELETE according to the lookup
199 is_nodelete (struct link_map
*map
, int flags
)
201 /* Non-pending NODELETE always counts. Pending NODELETE only counts
202 during initial relocation processing. */
203 return map
->l_nodelete_active
204 || ((flags
& DL_LOOKUP_FOR_RELOCATE
) && map
->l_nodelete_pending
);
207 /* Utility function for do_lookup_x. Lookup an STB_GNU_UNIQUE symbol
208 in the unique symbol table, creating a new entry if necessary.
209 Return the matching symbol in RESULT. */
211 do_lookup_unique (const char *undef_name
, unsigned int new_hash
,
212 struct link_map
*map
, struct sym_val
*result
,
213 int type_class
, const ElfW(Sym
) *sym
, const char *strtab
,
214 const ElfW(Sym
) *ref
, const struct link_map
*undef_map
,
217 /* We have to determine whether we already found a symbol with this
218 name before. If not then we have to add it to the search table.
219 If we already found a definition we have to use it. */
221 struct unique_sym_table
*tab
222 = &GL(dl_ns
)[map
->l_ns
]._ns_unique_sym_table
;
224 __rtld_lock_lock_recursive (tab
->lock
);
226 struct unique_sym
*entries
= tab
->entries
;
227 size_t size
= tab
->size
;
230 size_t idx
= new_hash
% size
;
231 size_t hash2
= 1 + new_hash
% (size
- 2);
234 if (entries
[idx
].hashval
== new_hash
235 && strcmp (entries
[idx
].name
, undef_name
) == 0)
237 if ((type_class
& ELF_RTYPE_CLASS_COPY
) != 0)
239 /* We possibly have to initialize the central
240 copy from the copy addressed through the
247 result
->s
= entries
[idx
].sym
;
248 result
->m
= (struct link_map
*) entries
[idx
].map
;
250 __rtld_lock_unlock_recursive (tab
->lock
);
254 if (entries
[idx
].name
== NULL
)
262 if (size
* 3 <= tab
->n_elements
* 4)
264 /* Expand the table. */
265 #ifdef RTLD_CHECK_FOREIGN_CALL
266 /* This must not happen during runtime relocations. */
267 assert (!RTLD_CHECK_FOREIGN_CALL
);
269 size_t newsize
= _dl_higher_prime_number (size
+ 1);
270 struct unique_sym
*newentries
271 = calloc (sizeof (struct unique_sym
), newsize
);
272 if (newentries
== NULL
)
275 __rtld_lock_unlock_recursive (tab
->lock
);
276 _dl_fatal_printf ("out of memory\n");
279 for (idx
= 0; idx
< size
; ++idx
)
280 if (entries
[idx
].name
!= NULL
)
281 enter_unique_sym (newentries
, newsize
, entries
[idx
].hashval
,
282 entries
[idx
].name
, entries
[idx
].sym
,
288 entries
= tab
->entries
= newentries
;
289 tab
->free
= __rtld_free
;
294 #ifdef RTLD_CHECK_FOREIGN_CALL
295 /* This must not happen during runtime relocations. */
296 assert (!RTLD_CHECK_FOREIGN_CALL
);
299 #define INITIAL_NUNIQUE_SYM_TABLE 31
300 size
= INITIAL_NUNIQUE_SYM_TABLE
;
301 entries
= calloc (sizeof (struct unique_sym
), size
);
305 tab
->entries
= entries
;
307 tab
->free
= __rtld_free
;
310 if ((type_class
& ELF_RTYPE_CLASS_COPY
) != 0)
311 enter_unique_sym (entries
, size
, new_hash
, strtab
+ sym
->st_name
, ref
,
315 enter_unique_sym (entries
, size
,
316 new_hash
, strtab
+ sym
->st_name
, sym
, map
);
318 if (map
->l_type
== lt_loaded
&& !is_nodelete (map
, flags
))
320 /* Make sure we don't unload this object by
321 setting the appropriate flag. */
322 if (__glibc_unlikely (GLRO (dl_debug_mask
) & DL_DEBUG_BINDINGS
))
324 marking %s [%lu] as NODELETE due to unique symbol\n",
325 map
->l_name
, map
->l_ns
);
326 mark_nodelete (map
, flags
);
331 __rtld_lock_unlock_recursive (tab
->lock
);
334 result
->m
= (struct link_map
*) map
;
337 /* Inner part of the lookup functions. We return a value > 0 if we
338 found the symbol, the value 0 if nothing is found and < 0 if
339 something bad happened. */
341 __attribute_noinline__
342 do_lookup_x (const char *undef_name
, unsigned int new_hash
,
343 unsigned long int *old_hash
, const ElfW(Sym
) *ref
,
344 struct sym_val
*result
, struct r_scope_elem
*scope
, size_t i
,
345 const struct r_found_version
*const version
, int flags
,
346 struct link_map
*skip
, int type_class
, struct link_map
*undef_map
)
348 size_t n
= scope
->r_nlist
;
349 /* Make sure we read the value before proceeding. Otherwise we
350 might use r_list pointing to the initial scope and r_nlist being
351 the value after a resize. That is the only path in dl-open.c not
352 protected by GSCOPE. A read barrier here might be to expensive. */
353 __asm
volatile ("" : "+r" (n
), "+m" (scope
->r_list
));
354 struct link_map
**list
= scope
->r_list
;
358 const struct link_map
*map
= list
[i
]->l_real
;
360 /* Here come the extra test needed for `_dl_lookup_symbol_skip'. */
364 /* Don't search the executable when resolving a copy reloc. */
365 if ((type_class
& ELF_RTYPE_CLASS_COPY
) && map
->l_type
== lt_executable
)
368 /* Do not look into objects which are going to be removed,
369 except when the referencing object itself is being removed.
371 The second part covers the situation when an object lazily
372 binds to another object while running its destructor, but the
373 destructor of the other object has already run, so that
374 dlclose has set l_removed. It may not always be obvious how
375 to avoid such a scenario to programmers creating DSOs,
376 particularly if C++ vague linkage is involved and triggers
377 symbol interposition.
379 Accepting these to-be-removed objects makes the lazy and
380 BIND_NOW cases more similar. (With BIND_NOW, the symbol is
381 resolved early, before the destructor call, so the issue does
382 not arise.). Behavior matches the constructor scenario: the
383 implementation allows binding to symbols of objects whose
384 constructors have not run. In fact, not doing this would be
385 mostly incompatible with symbol interposition. */
386 if (map
->l_removed
&& !(undef_map
!= NULL
&& undef_map
->l_removed
))
389 /* Print some debugging info if wanted. */
390 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_SYMBOLS
))
391 _dl_debug_printf ("symbol=%s; lookup in file=%s [%lu]\n",
392 undef_name
, DSO_FILENAME (map
->l_name
),
395 /* If the hash table is empty there is nothing to do here. */
396 if (map
->l_nbuckets
== 0)
400 int num_versions
= 0;
401 const ElfW(Sym
) *versioned_sym
= NULL
;
403 /* The tables for this map. */
404 const ElfW(Sym
) *symtab
= (const void *) D_PTR (map
, l_info
[DT_SYMTAB
]);
405 const char *strtab
= (const void *) D_PTR (map
, l_info
[DT_STRTAB
]);
407 const ElfW(Sym
) *sym
;
408 const ElfW(Addr
) *bitmask
= map
->l_gnu_bitmask
;
409 if (__glibc_likely (bitmask
!= NULL
))
411 ElfW(Addr
) bitmask_word
412 = bitmask
[(new_hash
/ __ELF_NATIVE_CLASS
)
413 & map
->l_gnu_bitmask_idxbits
];
415 unsigned int hashbit1
= new_hash
& (__ELF_NATIVE_CLASS
- 1);
416 unsigned int hashbit2
= ((new_hash
>> map
->l_gnu_shift
)
417 & (__ELF_NATIVE_CLASS
- 1));
419 if (__glibc_unlikely ((bitmask_word
>> hashbit1
)
420 & (bitmask_word
>> hashbit2
) & 1))
422 Elf32_Word bucket
= map
->l_gnu_buckets
[new_hash
426 const Elf32_Word
*hasharr
= &map
->l_gnu_chain_zero
[bucket
];
429 if (((*hasharr
^ new_hash
) >> 1) == 0)
431 symidx
= ELF_MACHINE_HASH_SYMIDX (map
, hasharr
);
432 sym
= check_match (undef_name
, ref
, version
, flags
,
433 type_class
, &symtab
[symidx
], symidx
,
434 strtab
, map
, &versioned_sym
,
439 while ((*hasharr
++ & 1u) == 0);
442 /* No symbol found. */
447 if (*old_hash
== 0xffffffff)
448 *old_hash
= _dl_elf_hash (undef_name
);
450 /* Use the old SysV-style hash table. Search the appropriate
451 hash bucket in this object's symbol table for a definition
452 for the same symbol name. */
453 for (symidx
= map
->l_buckets
[*old_hash
% map
->l_nbuckets
];
455 symidx
= map
->l_chain
[symidx
])
457 sym
= check_match (undef_name
, ref
, version
, flags
,
458 type_class
, &symtab
[symidx
], symidx
,
459 strtab
, map
, &versioned_sym
,
466 /* If we have seen exactly one versioned symbol while we are
467 looking for an unversioned symbol and the version is not the
468 default version we still accept this symbol since there are
469 no possible ambiguities. */
470 sym
= num_versions
== 1 ? versioned_sym
: NULL
;
475 /* Hidden and internal symbols are local, ignore them. */
476 if (__glibc_unlikely (dl_symbol_visibility_binds_local_p (sym
)))
479 if (ELFW(ST_VISIBILITY
) (sym
->st_other
) == STV_PROTECTED
)
480 _dl_check_protected_symbol (undef_name
, undef_map
, ref
, map
,
483 switch (ELFW(ST_BIND
) (sym
->st_info
))
486 /* Weak definition. Use this value if we don't find another. */
487 if (__glibc_unlikely (GLRO(dl_dynamic_weak
)))
492 result
->m
= (struct link_map
*) map
;
498 /* Global definition. Just what we need. */
500 result
->m
= (struct link_map
*) map
;
503 case STB_GNU_UNIQUE
:;
504 do_lookup_unique (undef_name
, new_hash
, (struct link_map
*) map
,
505 result
, type_class
, sym
, strtab
, ref
,
510 /* Local symbols are ignored. */
520 /* We have not found anything until now. */
525 /* Add extra dependency on MAP to UNDEF_MAP. */
527 add_dependency (struct link_map
*undef_map
, struct link_map
*map
, int flags
)
529 struct link_map
*runp
;
533 /* Avoid self-references and references to objects which cannot be
535 if (undef_map
== map
)
538 /* Avoid references to objects which cannot be unloaded anyway. We
539 do not need to record dependencies if this object goes away
540 during dlopen failure, either. IFUNC resolvers with relocation
541 dependencies may pick an dependency which can be dlclose'd, but
542 such IFUNC resolvers are undefined anyway. */
543 assert (map
->l_type
== lt_loaded
);
544 if (is_nodelete (map
, flags
))
547 struct link_map_reldeps
*l_reldeps
548 = atomic_forced_read (undef_map
->l_reldeps
);
550 /* Make sure l_reldeps is read before l_initfini. */
551 atomic_read_barrier ();
553 /* Determine whether UNDEF_MAP already has a reference to MAP. First
554 look in the normal dependencies. */
555 struct link_map
**l_initfini
= atomic_forced_read (undef_map
->l_initfini
);
556 if (l_initfini
!= NULL
)
558 for (i
= 0; l_initfini
[i
] != NULL
; ++i
)
559 if (l_initfini
[i
] == map
)
563 /* No normal dependency. See whether we already had to add it
564 to the special list of dynamic dependencies. */
565 unsigned int l_reldepsact
= 0;
566 if (l_reldeps
!= NULL
)
568 struct link_map
**list
= &l_reldeps
->list
[0];
569 l_reldepsact
= l_reldeps
->act
;
570 for (i
= 0; i
< l_reldepsact
; ++i
)
575 /* Save serial number of the target MAP. */
576 unsigned long long serial
= map
->l_serial
;
578 /* Make sure nobody can unload the object while we are at it. */
579 if (__glibc_unlikely (flags
& DL_LOOKUP_GSCOPE_LOCK
))
581 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
582 here, that can result in ABBA deadlock. */
583 THREAD_GSCOPE_RESET_FLAG ();
584 __rtld_lock_lock_recursive (GL(dl_load_lock
));
585 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
586 it can e.g. point to unallocated memory. So avoid the optimizer
587 treating the above read from MAP->l_serial as ensurance it
588 can safely dereference it. */
589 map
= atomic_forced_read (map
);
591 /* From this point on it is unsafe to dereference MAP, until it
592 has been found in one of the lists. */
594 /* Redo the l_initfini check in case undef_map's l_initfini
595 changed in the mean time. */
596 if (undef_map
->l_initfini
!= l_initfini
597 && undef_map
->l_initfini
!= NULL
)
599 l_initfini
= undef_map
->l_initfini
;
600 for (i
= 0; l_initfini
[i
] != NULL
; ++i
)
601 if (l_initfini
[i
] == map
)
605 /* Redo the l_reldeps check if undef_map's l_reldeps changed in
607 if (undef_map
->l_reldeps
!= NULL
)
609 if (undef_map
->l_reldeps
!= l_reldeps
)
611 struct link_map
**list
= &undef_map
->l_reldeps
->list
[0];
612 l_reldepsact
= undef_map
->l_reldeps
->act
;
613 for (i
= 0; i
< l_reldepsact
; ++i
)
617 else if (undef_map
->l_reldeps
->act
> l_reldepsact
)
619 struct link_map
**list
620 = &undef_map
->l_reldeps
->list
[0];
622 l_reldepsact
= undef_map
->l_reldeps
->act
;
623 for (; i
< l_reldepsact
; ++i
)
630 __rtld_lock_lock_recursive (GL(dl_load_lock
));
632 /* The object is not yet in the dependency list. Before we add
633 it make sure just one more time the object we are about to
634 reference is still available. There is a brief period in
635 which the object could have been removed since we found the
637 runp
= GL(dl_ns
)[undef_map
->l_ns
]._ns_loaded
;
638 while (runp
!= NULL
&& runp
!= map
)
643 /* The object is still available. */
645 /* MAP could have been dlclosed, freed and then some other dlopened
646 library could have the same link_map pointer. */
647 if (map
->l_serial
!= serial
)
650 /* Redo the NODELETE check, as when dl_load_lock wasn't held
651 yet this could have changed. */
652 if (is_nodelete (map
, flags
))
655 /* If the object with the undefined reference cannot be removed ever
656 just make sure the same is true for the object which contains the
658 if (undef_map
->l_type
!= lt_loaded
|| is_nodelete (map
, flags
))
660 if (__glibc_unlikely (GLRO (dl_debug_mask
) & DL_DEBUG_BINDINGS
)
661 && !is_nodelete (map
, flags
))
663 if (undef_map
->l_name
[0] == '\0')
665 marking %s [%lu] as NODELETE due to reference from main program\n",
666 map
->l_name
, map
->l_ns
);
669 marking %s [%lu] as NODELETE due to reference from %s [%lu]\n",
670 map
->l_name
, map
->l_ns
,
671 undef_map
->l_name
, undef_map
->l_ns
);
673 mark_nodelete (map
, flags
);
677 /* Add the reference now. */
678 if (__glibc_unlikely (l_reldepsact
>= undef_map
->l_reldepsmax
))
680 /* Allocate more memory for the dependency list. Since this
681 can never happen during the startup phase we can use
683 struct link_map_reldeps
*newp
;
685 = undef_map
->l_reldepsmax
? undef_map
->l_reldepsmax
* 2 : 10;
687 #ifdef RTLD_PREPARE_FOREIGN_CALL
688 RTLD_PREPARE_FOREIGN_CALL
;
691 newp
= malloc (sizeof (*newp
) + max
* sizeof (struct link_map
*));
694 /* If we didn't manage to allocate memory for the list this is
695 no fatal problem. We simply make sure the referenced object
696 cannot be unloaded. This is semantically the correct
698 if (__glibc_unlikely (GLRO (dl_debug_mask
) & DL_DEBUG_BINDINGS
)
699 && !is_nodelete (map
, flags
))
701 marking %s [%lu] as NODELETE due to memory allocation failure\n",
702 map
->l_name
, map
->l_ns
);
703 /* In case of non-lazy binding, we could actually report
704 the memory allocation error, but for now, we use the
705 conservative approximation as well. */
706 mark_nodelete (map
, flags
);
712 memcpy (&newp
->list
[0], &undef_map
->l_reldeps
->list
[0],
713 l_reldepsact
* sizeof (struct link_map
*));
714 newp
->list
[l_reldepsact
] = map
;
715 newp
->act
= l_reldepsact
+ 1;
716 atomic_write_barrier ();
717 void *old
= undef_map
->l_reldeps
;
718 undef_map
->l_reldeps
= newp
;
719 undef_map
->l_reldepsmax
= max
;
721 _dl_scope_free (old
);
726 undef_map
->l_reldeps
->list
[l_reldepsact
] = map
;
727 atomic_write_barrier ();
728 undef_map
->l_reldeps
->act
= l_reldepsact
+ 1;
731 /* Display information if we are debugging. */
732 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
))
734 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
735 DSO_FILENAME (map
->l_name
),
737 DSO_FILENAME (undef_map
->l_name
),
741 /* Whoa, that was bad luck. We have to search again. */
745 /* Release the lock. */
746 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
748 if (__glibc_unlikely (flags
& DL_LOOKUP_GSCOPE_LOCK
))
749 THREAD_GSCOPE_SET_FLAG ();
754 if (map
->l_serial
!= serial
)
760 /* Search loaded objects' symbol tables for a definition of the symbol
761 UNDEF_NAME, perhaps with a requested version for the symbol.
763 We must never have calls to the audit functions inside this function
764 or in any function which gets called. If this would happen the audit
765 code might create a thread which can throw off all the scope locking. */
767 _dl_lookup_symbol_x (const char *undef_name
, struct link_map
*undef_map
,
768 const ElfW(Sym
) **ref
,
769 struct r_scope_elem
*symbol_scope
[],
770 const struct r_found_version
*version
,
771 int type_class
, int flags
, struct link_map
*skip_map
)
773 const unsigned int new_hash
= _dl_new_hash (undef_name
);
774 unsigned long int old_hash
= 0xffffffff;
775 struct sym_val current_value
= { NULL
, NULL
};
776 struct r_scope_elem
**scope
= symbol_scope
;
778 bump_num_relocations ();
780 /* DL_LOOKUP_RETURN_NEWEST does not make sense for versioned
782 assert (version
== NULL
|| !(flags
& DL_LOOKUP_RETURN_NEWEST
));
785 if (__glibc_unlikely (skip_map
!= NULL
))
786 /* Search the relevant loaded objects for a definition. */
787 while ((*scope
)->r_list
[i
] != skip_map
)
790 /* Search the relevant loaded objects for a definition. */
791 for (size_t start
= i
; *scope
!= NULL
; start
= 0, ++scope
)
792 if (do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
793 ¤t_value
, *scope
, start
, version
, flags
,
794 skip_map
, type_class
, undef_map
) != 0)
797 if (__glibc_unlikely (current_value
.s
== NULL
))
799 if ((*ref
== NULL
|| ELFW(ST_BIND
) ((*ref
)->st_info
) != STB_WEAK
)
800 && !(GLRO(dl_debug_mask
) & DL_DEBUG_UNUSED
))
802 /* We could find no value for a strong reference. */
803 const char *reference_name
= undef_map
? undef_map
->l_name
: "";
804 const char *versionstr
= version
? ", version " : "";
805 const char *versionname
= (version
&& version
->name
806 ? version
->name
: "");
807 struct dl_exception exception
;
808 /* XXX We cannot translate the message. */
809 _dl_exception_create_format
810 (&exception
, DSO_FILENAME (reference_name
),
811 "undefined symbol: %s%s%s",
812 undef_name
, versionstr
, versionname
);
813 _dl_signal_cexception (0, &exception
, N_("symbol lookup error"));
814 _dl_exception_free (&exception
);
820 int protected = (*ref
821 && ELFW(ST_VISIBILITY
) ((*ref
)->st_other
) == STV_PROTECTED
);
822 if (__glibc_unlikely (protected != 0))
824 /* It is very tricky. We need to figure out what value to
825 return for the protected symbol. */
826 if (type_class
== ELF_RTYPE_CLASS_PLT
)
828 if (current_value
.s
!= NULL
&& current_value
.m
!= undef_map
)
830 current_value
.s
= *ref
;
831 current_value
.m
= undef_map
;
836 struct sym_val protected_value
= { NULL
, NULL
};
838 for (scope
= symbol_scope
; *scope
!= NULL
; i
= 0, ++scope
)
839 if (do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
840 &protected_value
, *scope
, i
, version
, flags
,
841 skip_map
, ELF_RTYPE_CLASS_PLT
, NULL
) != 0)
844 if (protected_value
.s
!= NULL
&& protected_value
.m
!= undef_map
)
846 current_value
.s
= *ref
;
847 current_value
.m
= undef_map
;
852 /* We have to check whether this would bind UNDEF_MAP to an object
853 in the global scope which was dynamically loaded. In this case
854 we have to prevent the latter from being unloaded unless the
855 UNDEF_MAP object is also unloaded. */
856 if (__glibc_unlikely (current_value
.m
->l_type
== lt_loaded
)
857 /* Don't do this for explicit lookups as opposed to implicit
859 && (flags
& DL_LOOKUP_ADD_DEPENDENCY
) != 0
860 /* Add UNDEF_MAP to the dependencies. */
861 && add_dependency (undef_map
, current_value
.m
, flags
) < 0)
862 /* Something went wrong. Perhaps the object we tried to reference
863 was just removed. Try finding another definition. */
864 return _dl_lookup_symbol_x (undef_name
, undef_map
, ref
,
865 (flags
& DL_LOOKUP_GSCOPE_LOCK
)
866 ? undef_map
->l_scope
: symbol_scope
,
867 version
, type_class
, flags
, skip_map
);
869 /* The object is used. */
870 if (__glibc_unlikely (current_value
.m
->l_used
== 0))
871 current_value
.m
->l_used
= 1;
873 if (__glibc_unlikely (GLRO(dl_debug_mask
) & DL_DEBUG_BINDINGS
))
875 const char *reference_name
= undef_map
->l_name
;
877 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
878 DSO_FILENAME (reference_name
),
880 DSO_FILENAME (current_value
.m
->l_name
),
881 current_value
.m
->l_ns
,
882 protected ? "protected" : "normal", undef_name
);
884 _dl_debug_printf_c (" [%s]\n", version
->name
);
886 _dl_debug_printf_c ("\n");
890 *ref
= current_value
.s
;
891 return LOOKUP_VALUE (current_value
.m
);