1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2005, 2006, 2007 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <dl-machine.h>
28 #include <sysdep-cancel.h>
29 #include <bits/libc-lock.h>
34 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
36 /* We need this string more than once. */
37 static const char undefined_msg
[] = "undefined symbol: ";
47 #define make_string(string, rest...) \
49 const char *all[] = { string, ## rest }; \
54 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
55 len += strlen (all[cnt]); \
57 cp = result = alloca (len); \
58 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
59 cp = __stpcpy (cp, all[cnt]); \
64 /* Statistics function. */
66 # define bump_num_relocations() ++GL(dl_num_relocations)
68 # define bump_num_relocations() ((void) 0)
72 /* The actual lookup code. */
73 #include "do-lookup.h"
77 dl_new_hash (const char *s
)
79 uint_fast32_t h
= 5381;
80 for (unsigned char c
= *s
; c
!= '\0'; c
= *++s
)
82 return h
& 0xffffffff;
86 /* Add extra dependency on MAP to UNDEF_MAP. */
89 add_dependency (struct link_map
*undef_map
, struct link_map
*map
, int flags
)
91 struct link_map
**list
;
92 struct link_map
*runp
;
96 unsigned long long int serial
;
98 /* Avoid self-references and references to objects which cannot be
100 if (undef_map
== map
)
103 /* Save serial number of the target MAP. */
104 serial
= map
->l_serial
;
106 /* Make sure nobody can unload the object while we are at it. */
107 if (__builtin_expect (flags
& DL_LOOKUP_GSCOPE_LOCK
, 0))
109 /* We can't just call __rtld_lock_lock_recursive (GL(dl_load_lock))
110 here, that can result in ABBA deadlock. */
111 THREAD_GSCOPE_RESET_FLAG ();
112 __rtld_lock_lock_recursive (GL(dl_load_lock
));
113 THREAD_GSCOPE_SET_FLAG ();
114 /* While MAP value won't change, after THREAD_GSCOPE_RESET_FLAG ()
115 it can e.g. point to unallocated memory. So avoid the optimizer
116 treating the above read from MAP->l_serial as ensurance it
117 can safely dereference it. */
118 map
= atomic_forced_read (map
);
121 __rtld_lock_lock_recursive (GL(dl_load_lock
));
123 /* From this point on it is unsafe to dereference MAP, until it
124 has been found in one of the lists. */
126 /* Determine whether UNDEF_MAP already has a reference to MAP. First
127 look in the normal dependencies. */
128 if (undef_map
->l_initfini
!= NULL
)
130 list
= undef_map
->l_initfini
;
132 for (i
= 0; list
[i
] != NULL
; ++i
)
137 /* No normal dependency. See whether we already had to add it
138 to the special list of dynamic dependencies. */
139 list
= undef_map
->l_reldeps
;
140 act
= undef_map
->l_reldepsact
;
142 for (i
= 0; i
< act
; ++i
)
146 /* The object is not yet in the dependency list. Before we add
147 it make sure just one more time the object we are about to
148 reference is still available. There is a brief period in
149 which the object could have been removed since we found the
151 runp
= GL(dl_ns
)[undef_map
->l_ns
]._ns_loaded
;
152 while (runp
!= NULL
&& runp
!= map
)
157 /* The object is still available. */
159 /* MAP could have been dlclosed, freed and then some other dlopened
160 library could have the same link_map pointer. */
161 if (map
->l_serial
!= serial
)
164 /* Avoid references to objects which cannot be unloaded anyway. */
165 if (map
->l_type
!= lt_loaded
166 || (map
->l_flags_1
& DF_1_NODELETE
) != 0)
169 /* If the object with the undefined reference cannot be removed ever
170 just make sure the same is true for the object which contains the
172 if (undef_map
->l_type
!= lt_loaded
173 || (undef_map
->l_flags_1
& DF_1_NODELETE
) != 0)
175 map
->l_flags_1
|= DF_1_NODELETE
;
179 /* Add the reference now. */
180 if (__builtin_expect (act
>= undef_map
->l_reldepsmax
, 0))
182 /* Allocate more memory for the dependency list. Since this
183 can never happen during the startup phase we can use
187 undef_map
->l_reldepsmax
+= 5;
188 newp
= realloc (undef_map
->l_reldeps
,
189 undef_map
->l_reldepsmax
190 * sizeof (struct link_map
*));
192 if (__builtin_expect (newp
!= NULL
, 1))
193 undef_map
->l_reldeps
= (struct link_map
**) newp
;
195 /* Correct the addition. */
196 undef_map
->l_reldepsmax
-= 5;
199 /* If we didn't manage to allocate memory for the list this is
200 no fatal mistake. We simply increment the use counter of the
201 referenced object and don't record the dependencies. This
202 means this increment can never be reverted and the object
203 will never be unloaded. This is semantically the correct
205 if (__builtin_expect (act
< undef_map
->l_reldepsmax
, 1))
206 undef_map
->l_reldeps
[undef_map
->l_reldepsact
++] = map
;
208 /* Display information if we are debugging. */
209 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
211 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
212 map
->l_name
[0] ? map
->l_name
: rtld_progname
,
215 ? undef_map
->l_name
: rtld_progname
,
219 /* Whoa, that was bad luck. We have to search again. */
223 /* Release the lock. */
224 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
229 if (map
->l_serial
!= serial
)
236 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
237 const ElfW(Sym
) **ref
, struct sym_val
*value
,
238 const struct r_found_version
*version
, int type_class
,
242 /* Search loaded objects' symbol tables for a definition of the symbol
243 UNDEF_NAME, perhaps with a requested version for the symbol.
245 We must never have calls to the audit functions inside this function
246 or in any function which gets called. If this would happen the audit
247 code might create a thread which can throw off all the scope locking. */
250 _dl_lookup_symbol_x (const char *undef_name
, struct link_map
*undef_map
,
251 const ElfW(Sym
) **ref
,
252 struct r_scope_elem
*symbol_scope
[],
253 const struct r_found_version
*version
,
254 int type_class
, int flags
, struct link_map
*skip_map
)
256 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
257 unsigned long int old_hash
= 0xffffffff;
258 struct sym_val current_value
= { NULL
, NULL
};
259 struct r_scope_elem
**scope
= symbol_scope
;
261 bump_num_relocations ();
263 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY or DL_LOOKUP_GSCOPE_LOCK
264 is allowed if we look up a versioned symbol. */
265 assert (version
== NULL
266 || (flags
& ~(DL_LOOKUP_ADD_DEPENDENCY
| DL_LOOKUP_GSCOPE_LOCK
))
270 if (__builtin_expect (skip_map
!= NULL
, 0))
271 /* Search the relevant loaded objects for a definition. */
272 while ((*scope
)->r_list
[i
] != skip_map
)
275 /* Search the relevant loaded objects for a definition. */
276 for (size_t start
= i
; *scope
!= NULL
; start
= 0, ++scope
)
278 int res
= do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
279 ¤t_value
, *scope
, start
, version
, flags
,
280 skip_map
, type_class
);
284 if (__builtin_expect (res
, 0) < 0 && skip_map
== NULL
)
286 /* Oh, oh. The file named in the relocation entry does not
287 contain the needed symbol. This code is never reached
288 for unversioned lookups. */
289 assert (version
!= NULL
);
290 const char *reference_name
= undef_map
? undef_map
->l_name
: NULL
;
292 /* XXX We cannot translate the message. */
293 _dl_signal_cerror (0, (reference_name
[0]
295 : (rtld_progname
?: "<main program>")),
296 N_("relocation error"),
297 make_string ("symbol ", undef_name
, ", version ",
299 " not defined in file ",
301 " with link time reference",
303 ? " (no version symbols)" : ""));
309 if (__builtin_expect (current_value
.s
== NULL
, 0))
311 if ((*ref
== NULL
|| ELFW(ST_BIND
) ((*ref
)->st_info
) != STB_WEAK
)
314 /* We could find no value for a strong reference. */
315 const char *reference_name
= undef_map
? undef_map
->l_name
: "";
316 const char *versionstr
= version
? ", version " : "";
317 const char *versionname
= (version
&& version
->name
318 ? version
->name
: "");
320 /* XXX We cannot translate the message. */
321 _dl_signal_cerror (0, (reference_name
[0]
323 : (rtld_progname
?: "<main program>")),
324 N_("symbol lookup error"),
325 make_string (undefined_msg
, undef_name
,
326 versionstr
, versionname
));
332 int protected = (*ref
333 && ELFW(ST_VISIBILITY
) ((*ref
)->st_other
) == STV_PROTECTED
);
334 if (__builtin_expect (protected != 0, 0))
336 /* It is very tricky. We need to figure out what value to
337 return for the protected symbol. */
338 if (type_class
== ELF_RTYPE_CLASS_PLT
)
340 if (current_value
.s
!= NULL
&& current_value
.m
!= undef_map
)
342 current_value
.s
= *ref
;
343 current_value
.m
= undef_map
;
348 struct sym_val protected_value
= { NULL
, NULL
};
350 for (scope
= symbol_scope
; *scope
!= NULL
; i
= 0, ++scope
)
351 if (do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
352 &protected_value
, *scope
, i
, version
, flags
,
353 skip_map
, ELF_RTYPE_CLASS_PLT
) != 0)
356 if (protected_value
.s
!= NULL
&& protected_value
.m
!= undef_map
)
358 current_value
.s
= *ref
;
359 current_value
.m
= undef_map
;
364 /* We have to check whether this would bind UNDEF_MAP to an object
365 in the global scope which was dynamically loaded. In this case
366 we have to prevent the latter from being unloaded unless the
367 UNDEF_MAP object is also unloaded. */
368 if (__builtin_expect (current_value
.m
->l_type
== lt_loaded
, 0)
369 /* Don't do this for explicit lookups as opposed to implicit
371 && (flags
& DL_LOOKUP_ADD_DEPENDENCY
) != 0
372 /* Add UNDEF_MAP to the dependencies. */
373 && add_dependency (undef_map
, current_value
.m
, flags
) < 0)
374 /* Something went wrong. Perhaps the object we tried to reference
375 was just removed. Try finding another definition. */
376 return _dl_lookup_symbol_x (undef_name
, undef_map
, ref
,
377 (flags
& DL_LOOKUP_GSCOPE_LOCK
)
378 ? undef_map
->l_scope
: symbol_scope
,
379 version
, type_class
, flags
, skip_map
);
381 /* The object is used. */
382 current_value
.m
->l_used
= 1;
384 if (__builtin_expect (GLRO(dl_debug_mask
)
385 & (DL_DEBUG_BINDINGS
|DL_DEBUG_PRELINK
), 0))
386 _dl_debug_bindings (undef_name
, undef_map
, ref
,
387 ¤t_value
, version
, type_class
, protected);
389 *ref
= current_value
.s
;
390 return LOOKUP_VALUE (current_value
.m
);
394 /* Cache the location of MAP's hash table. */
398 _dl_setup_hash (struct link_map
*map
)
403 if (__builtin_expect (map
->l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
404 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
405 + DT_EXTRANUM
+ DT_VALNUM
] != NULL
, 1))
408 = (void *) D_PTR (map
, l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
409 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
410 + DT_EXTRANUM
+ DT_VALNUM
]);
411 map
->l_nbuckets
= *hash32
++;
412 Elf32_Word symbias
= *hash32
++;
413 Elf32_Word bitmask_nwords
= *hash32
++;
414 /* Must be a power of two. */
415 assert ((bitmask_nwords
& (bitmask_nwords
- 1)) == 0);
416 map
->l_gnu_bitmask_idxbits
= bitmask_nwords
- 1;
417 map
->l_gnu_shift
= *hash32
++;
419 map
->l_gnu_bitmask
= (ElfW(Addr
) *) hash32
;
420 hash32
+= __ELF_NATIVE_CLASS
/ 32 * bitmask_nwords
;
422 map
->l_gnu_buckets
= hash32
;
423 hash32
+= map
->l_nbuckets
;
424 map
->l_gnu_chain_zero
= hash32
- symbias
;
428 if (!map
->l_info
[DT_HASH
])
430 hash
= (void *) D_PTR (map
, l_info
[DT_HASH
]);
432 map
->l_nbuckets
= *hash
++;
434 map
->l_buckets
= hash
;
435 hash
+= map
->l_nbuckets
;
442 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
443 const ElfW(Sym
) **ref
, struct sym_val
*value
,
444 const struct r_found_version
*version
, int type_class
,
447 const char *reference_name
= undef_map
->l_name
;
449 if (GLRO(dl_debug_mask
) & DL_DEBUG_BINDINGS
)
451 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
454 : (rtld_progname
?: "<main program>")),
456 value
->m
->l_name
[0] ? value
->m
->l_name
: rtld_progname
,
458 protected ? "protected" : "normal", undef_name
);
460 _dl_debug_printf_c (" [%s]\n", version
->name
);
462 _dl_debug_printf_c ("\n");
465 if (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
468 struct sym_val val
= { NULL
, NULL
};
470 if ((GLRO(dl_trace_prelink_map
) == NULL
471 || GLRO(dl_trace_prelink_map
) == GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
472 && undef_map
!= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
474 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
475 unsigned long int old_hash
= 0xffffffff;
477 do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
, &val
,
478 undef_map
->l_local_scope
[0], 0, version
, 0, NULL
,
481 if (val
.s
!= value
->s
|| val
.m
!= value
->m
)
486 && (__builtin_expect (ELFW(ST_TYPE
) (value
->s
->st_info
)
491 || GLRO(dl_trace_prelink_map
) == undef_map
492 || GLRO(dl_trace_prelink_map
) == NULL
495 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
496 conflict
? "conflict" : "lookup",
497 (int) sizeof (ElfW(Addr
)) * 2,
498 (size_t) undef_map
->l_map_start
,
499 (int) sizeof (ElfW(Addr
)) * 2,
500 (size_t) (((ElfW(Addr
)) *ref
) - undef_map
->l_map_start
),
501 (int) sizeof (ElfW(Addr
)) * 2,
502 (size_t) (value
->s
? value
->m
->l_map_start
: 0),
503 (int) sizeof (ElfW(Addr
)) * 2,
504 (size_t) (value
->s
? value
->s
->st_value
: 0));
507 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
508 (int) sizeof (ElfW(Addr
)) * 2,
509 (size_t) (val
.s
? val
.m
->l_map_start
: 0),
510 (int) sizeof (ElfW(Addr
)) * 2,
511 (size_t) (val
.s
? val
.s
->st_value
: 0));
513 _dl_printf ("/%x %s\n", type_class
, undef_name
);