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
)
91 struct link_map
**list
;
92 struct link_map
*runp
;
97 /* Avoid self-references and references to objects which cannot be
102 /* Make sure nobody can unload the object while we are at it. */
103 __rtld_lock_lock_recursive (GL(dl_load_lock
));
105 /* Avoid references to objects which cannot be unloaded anyway. */
106 if (map
->l_type
!= lt_loaded
107 || (map
->l_flags_1
& DF_1_NODELETE
) != 0)
110 /* If the object with the undefined reference cannot be removed ever
111 just make sure the same is true for the object which contains the
113 if (undef_map
->l_type
!= lt_loaded
114 || (undef_map
->l_flags_1
& DF_1_NODELETE
) != 0)
116 map
->l_flags_1
|= DF_1_NODELETE
;
120 /* Determine whether UNDEF_MAP already has a reference to MAP. First
121 look in the normal dependencies. */
122 if (undef_map
->l_initfini
!= NULL
)
124 list
= undef_map
->l_initfini
;
126 for (i
= 0; list
[i
] != NULL
; ++i
)
131 /* No normal dependency. See whether we already had to add it
132 to the special list of dynamic dependencies. */
133 list
= undef_map
->l_reldeps
;
134 act
= undef_map
->l_reldepsact
;
136 for (i
= 0; i
< act
; ++i
)
140 /* The object is not yet in the dependency list. Before we add
141 it make sure just one more time the object we are about to
142 reference is still available. There is a brief period in
143 which the object could have been removed since we found the
145 runp
= GL(dl_ns
)[undef_map
->l_ns
]._ns_loaded
;
146 while (runp
!= NULL
&& runp
!= map
)
151 /* The object is still available. Add the reference now. */
152 if (__builtin_expect (act
>= undef_map
->l_reldepsmax
, 0))
154 /* Allocate more memory for the dependency list. Since this
155 can never happen during the startup phase we can use
159 undef_map
->l_reldepsmax
+= 5;
160 newp
= realloc (undef_map
->l_reldeps
,
161 undef_map
->l_reldepsmax
162 * sizeof (struct link_map
*));
164 if (__builtin_expect (newp
!= NULL
, 1))
165 undef_map
->l_reldeps
= (struct link_map
**) newp
;
167 /* Correct the addition. */
168 undef_map
->l_reldepsmax
-= 5;
171 /* If we didn't manage to allocate memory for the list this is
172 no fatal mistake. We simply increment the use counter of the
173 referenced object and don't record the dependencies. This
174 means this increment can never be reverted and the object
175 will never be unloaded. This is semantically the correct
177 if (__builtin_expect (act
< undef_map
->l_reldepsmax
, 1))
178 undef_map
->l_reldeps
[undef_map
->l_reldepsact
++] = map
;
180 /* Display information if we are debugging. */
181 if (__builtin_expect (GLRO(dl_debug_mask
) & DL_DEBUG_FILES
, 0))
183 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
184 map
->l_name
[0] ? map
->l_name
: rtld_progname
,
187 ? undef_map
->l_name
: rtld_progname
,
191 /* Whoa, that was bad luck. We have to search again. */
195 /* Release the lock. */
196 __rtld_lock_unlock_recursive (GL(dl_load_lock
));
203 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
204 const ElfW(Sym
) **ref
, struct sym_val
*value
,
205 const struct r_found_version
*version
, int type_class
,
209 /* Search loaded objects' symbol tables for a definition of the symbol
210 UNDEF_NAME, perhaps with a requested version for the symbol.
212 We must never have calls to the audit functions inside this function
213 or in any function which gets called. If this would happen the audit
214 code might create a thread which can throw off all the scope locking. */
217 _dl_lookup_symbol_x (const char *undef_name
, struct link_map
*undef_map
,
218 const ElfW(Sym
) **ref
,
219 struct r_scope_elem
*symbol_scope
[],
220 const struct r_found_version
*version
,
221 int type_class
, int flags
, struct link_map
*skip_map
)
223 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
224 unsigned long int old_hash
= 0xffffffff;
225 struct sym_val current_value
= { NULL
, NULL
};
226 struct r_scope_elem
**scope
= symbol_scope
;
228 bump_num_relocations ();
230 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY is allowed if we look
231 up a versioned symbol. */
232 assert (version
== NULL
|| (flags
& ~(DL_LOOKUP_ADD_DEPENDENCY
)) == 0);
235 if (__builtin_expect (skip_map
!= NULL
, 0))
236 /* Search the relevant loaded objects for a definition. */
237 while ((*scope
)->r_list
[i
] != skip_map
)
240 /* Search the relevant loaded objects for a definition. */
241 for (size_t start
= i
; *scope
!= NULL
; start
= 0, ++scope
)
243 int res
= do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
244 ¤t_value
, *scope
, start
, version
, flags
,
245 skip_map
, type_class
);
249 if (__builtin_expect (res
, 0) < 0 && skip_map
== NULL
)
251 /* Oh, oh. The file named in the relocation entry does not
252 contain the needed symbol. This code is never reached
253 for unversioned lookups. */
254 assert (version
!= NULL
);
255 const char *reference_name
= undef_map
? undef_map
->l_name
: NULL
;
257 /* XXX We cannot translate the message. */
258 _dl_signal_cerror (0, (reference_name
[0]
260 : (rtld_progname
?: "<main program>")),
261 N_("relocation error"),
262 make_string ("symbol ", undef_name
, ", version ",
264 " not defined in file ",
266 " with link time reference",
268 ? " (no version symbols)" : ""));
274 if (__builtin_expect (current_value
.s
== NULL
, 0))
276 if ((*ref
== NULL
|| ELFW(ST_BIND
) ((*ref
)->st_info
) != STB_WEAK
)
279 /* We could find no value for a strong reference. */
280 const char *reference_name
= undef_map
? undef_map
->l_name
: "";
281 const char *versionstr
= version
? ", version " : "";
282 const char *versionname
= (version
&& version
->name
283 ? version
->name
: "");
285 /* XXX We cannot translate the message. */
286 _dl_signal_cerror (0, (reference_name
[0]
288 : (rtld_progname
?: "<main program>")),
289 N_("symbol lookup error"),
290 make_string (undefined_msg
, undef_name
,
291 versionstr
, versionname
));
297 int protected = (*ref
298 && ELFW(ST_VISIBILITY
) ((*ref
)->st_other
) == STV_PROTECTED
);
299 if (__builtin_expect (protected != 0, 0))
301 /* It is very tricky. We need to figure out what value to
302 return for the protected symbol. */
303 if (type_class
== ELF_RTYPE_CLASS_PLT
)
305 if (current_value
.s
!= NULL
&& current_value
.m
!= undef_map
)
307 current_value
.s
= *ref
;
308 current_value
.m
= undef_map
;
313 struct sym_val protected_value
= { NULL
, NULL
};
315 for (scope
= symbol_scope
; *scope
!= NULL
; i
= 0, ++scope
)
316 if (do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
,
317 &protected_value
, *scope
, i
, version
, flags
,
318 skip_map
, ELF_RTYPE_CLASS_PLT
) != 0)
321 if (protected_value
.s
!= NULL
&& protected_value
.m
!= undef_map
)
323 current_value
.s
= *ref
;
324 current_value
.m
= undef_map
;
329 /* We have to check whether this would bind UNDEF_MAP to an object
330 in the global scope which was dynamically loaded. In this case
331 we have to prevent the latter from being unloaded unless the
332 UNDEF_MAP object is also unloaded. */
333 if (__builtin_expect (current_value
.m
->l_type
== lt_loaded
, 0)
334 /* Don't do this for explicit lookups as opposed to implicit
336 && (flags
& DL_LOOKUP_ADD_DEPENDENCY
) != 0
337 /* Add UNDEF_MAP to the dependencies. */
338 && add_dependency (undef_map
, current_value
.m
) < 0)
339 /* Something went wrong. Perhaps the object we tried to reference
340 was just removed. Try finding another definition. */
341 return _dl_lookup_symbol_x (undef_name
, undef_map
, ref
, symbol_scope
,
342 version
, type_class
, flags
, skip_map
);
344 /* The object is used. */
345 current_value
.m
->l_used
= 1;
347 if (__builtin_expect (GLRO(dl_debug_mask
)
348 & (DL_DEBUG_BINDINGS
|DL_DEBUG_PRELINK
), 0))
349 _dl_debug_bindings (undef_name
, undef_map
, ref
,
350 ¤t_value
, version
, type_class
, protected);
352 *ref
= current_value
.s
;
353 return LOOKUP_VALUE (current_value
.m
);
357 /* Cache the location of MAP's hash table. */
361 _dl_setup_hash (struct link_map
*map
)
366 if (__builtin_expect (map
->l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
367 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
368 + DT_EXTRANUM
+ DT_VALNUM
] != NULL
, 1))
371 = (void *) D_PTR (map
, l_info
[DT_ADDRTAGIDX (DT_GNU_HASH
) + DT_NUM
372 + DT_THISPROCNUM
+ DT_VERSIONTAGNUM
373 + DT_EXTRANUM
+ DT_VALNUM
]);
374 map
->l_nbuckets
= *hash32
++;
375 Elf32_Word symbias
= *hash32
++;
376 Elf32_Word bitmask_nwords
= *hash32
++;
377 /* Must be a power of two. */
378 assert ((bitmask_nwords
& (bitmask_nwords
- 1)) == 0);
379 map
->l_gnu_bitmask_idxbits
= bitmask_nwords
- 1;
380 map
->l_gnu_shift
= *hash32
++;
382 map
->l_gnu_bitmask
= (ElfW(Addr
) *) hash32
;
383 hash32
+= __ELF_NATIVE_CLASS
/ 32 * bitmask_nwords
;
385 map
->l_gnu_buckets
= hash32
;
386 hash32
+= map
->l_nbuckets
;
387 map
->l_gnu_chain_zero
= hash32
- symbias
;
391 if (!map
->l_info
[DT_HASH
])
393 hash
= (void *) D_PTR (map
, l_info
[DT_HASH
]);
395 map
->l_nbuckets
= *hash
++;
397 map
->l_buckets
= hash
;
398 hash
+= map
->l_nbuckets
;
405 _dl_debug_bindings (const char *undef_name
, struct link_map
*undef_map
,
406 const ElfW(Sym
) **ref
, struct sym_val
*value
,
407 const struct r_found_version
*version
, int type_class
,
410 const char *reference_name
= undef_map
->l_name
;
412 if (GLRO(dl_debug_mask
) & DL_DEBUG_BINDINGS
)
414 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
417 : (rtld_progname
?: "<main program>")),
419 value
->m
->l_name
[0] ? value
->m
->l_name
: rtld_progname
,
421 protected ? "protected" : "normal", undef_name
);
423 _dl_debug_printf_c (" [%s]\n", version
->name
);
425 _dl_debug_printf_c ("\n");
428 if (GLRO(dl_debug_mask
) & DL_DEBUG_PRELINK
)
431 struct sym_val val
= { NULL
, NULL
};
433 if ((GLRO(dl_trace_prelink_map
) == NULL
434 || GLRO(dl_trace_prelink_map
) == GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
435 && undef_map
!= GL(dl_ns
)[LM_ID_BASE
]._ns_loaded
)
437 const uint_fast32_t new_hash
= dl_new_hash (undef_name
);
438 unsigned long int old_hash
= 0xffffffff;
440 do_lookup_x (undef_name
, new_hash
, &old_hash
, *ref
, &val
,
441 undef_map
->l_local_scope
[0], 0, version
, 0, NULL
,
444 if (val
.s
!= value
->s
|| val
.m
!= value
->m
)
449 && (__builtin_expect (ELFW(ST_TYPE
) (value
->s
->st_info
)
454 || GLRO(dl_trace_prelink_map
) == undef_map
455 || GLRO(dl_trace_prelink_map
) == NULL
458 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
459 conflict
? "conflict" : "lookup",
460 (int) sizeof (ElfW(Addr
)) * 2,
461 (size_t) undef_map
->l_map_start
,
462 (int) sizeof (ElfW(Addr
)) * 2,
463 (size_t) (((ElfW(Addr
)) *ref
) - undef_map
->l_map_start
),
464 (int) sizeof (ElfW(Addr
)) * 2,
465 (size_t) (value
->s
? value
->m
->l_map_start
: 0),
466 (int) sizeof (ElfW(Addr
)) * 2,
467 (size_t) (value
->s
? value
->s
->st_value
: 0));
470 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
471 (int) sizeof (ElfW(Addr
)) * 2,
472 (size_t) (val
.s
? val
.m
->l_map_start
: 0),
473 (int) sizeof (ElfW(Addr
)) * 2,
474 (size_t) (val
.s
? val
.s
->st_value
: 0));
476 _dl_printf ("/%x %s\n", type_class
, undef_name
);