]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-lookup.c
* elf/dl-lookup.c (_dl_lookup_symbol_x): Remove use of r_nlist.
[thirdparty/glibc.git] / elf / dl-lookup.c
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.
4
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.
9
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.
14
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
18 02111-1307 USA. */
19
20 #include <alloca.h>
21 #include <libintl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <ldsodefs.h>
26 #include <dl-hash.h>
27 #include <dl-machine.h>
28 #include <sysdep-cancel.h>
29 #include <bits/libc-lock.h>
30 #include <tls.h>
31
32 #include <assert.h>
33
34 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
35
36 /* We need this string more than once. */
37 static const char undefined_msg[] = "undefined symbol: ";
38
39
40 struct sym_val
41 {
42 const ElfW(Sym) *s;
43 struct link_map *m;
44 };
45
46
47 #define make_string(string, rest...) \
48 ({ \
49 const char *all[] = { string, ## rest }; \
50 size_t len, cnt; \
51 char *result, *cp; \
52 \
53 len = 1; \
54 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
55 len += strlen (all[cnt]); \
56 \
57 cp = result = alloca (len); \
58 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
59 cp = __stpcpy (cp, all[cnt]); \
60 \
61 result; \
62 })
63
64 /* Statistics function. */
65 #ifdef SHARED
66 # define bump_num_relocations() ++GL(dl_num_relocations)
67 #else
68 # define bump_num_relocations() ((void) 0)
69 #endif
70
71
72 /* The actual lookup code. */
73 #include "do-lookup.h"
74
75
76 static uint_fast32_t
77 dl_new_hash (const char *s)
78 {
79 uint_fast32_t h = 5381;
80 for (unsigned char c = *s; c != '\0'; c = *++s)
81 h = h * 33 + c;
82 return h & 0xffffffff;
83 }
84
85
86 /* Add extra dependency on MAP to UNDEF_MAP. */
87 static int
88 internal_function
89 add_dependency (struct link_map *undef_map, struct link_map *map, int flags)
90 {
91 struct link_map **list;
92 struct link_map *runp;
93 unsigned int act;
94 unsigned int i;
95 int result = 0;
96
97 /* Avoid self-references and references to objects which cannot be
98 unloaded anyway. */
99 if (undef_map == map)
100 return 0;
101
102 /* Make sure nobody can unload the object while we are at it.
103 If we hold a scope lock drop it now to avoid ABBA locking problems. */
104 if ((flags & DL_LOOKUP_SCOPE_LOCK) != 0 && !RTLD_SINGLE_THREAD_P)
105 {
106 __rtld_mrlock_unlock (undef_map->l_scope_lock);
107
108 __rtld_lock_lock_recursive (GL(dl_load_lock));
109
110 __rtld_mrlock_lock (undef_map->l_scope_lock);
111 }
112 else
113 __rtld_lock_lock_recursive (GL(dl_load_lock));
114
115 /* Avoid references to objects which cannot be unloaded anyway. */
116 if (map->l_type != lt_loaded
117 || (map->l_flags_1 & DF_1_NODELETE) != 0)
118 goto out;
119
120 /* If the object with the undefined reference cannot be removed ever
121 just make sure the same is true for the object which contains the
122 definition. */
123 if (undef_map->l_type != lt_loaded
124 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
125 {
126 map->l_flags_1 |= DF_1_NODELETE;
127 goto out;
128 }
129
130 /* Determine whether UNDEF_MAP already has a reference to MAP. First
131 look in the normal dependencies. */
132 if (undef_map->l_initfini != NULL)
133 {
134 list = undef_map->l_initfini;
135
136 for (i = 0; list[i] != NULL; ++i)
137 if (list[i] == map)
138 goto out;
139 }
140
141 /* No normal dependency. See whether we already had to add it
142 to the special list of dynamic dependencies. */
143 list = undef_map->l_reldeps;
144 act = undef_map->l_reldepsact;
145
146 for (i = 0; i < act; ++i)
147 if (list[i] == map)
148 goto out;
149
150 /* The object is not yet in the dependency list. Before we add
151 it make sure just one more time the object we are about to
152 reference is still available. There is a brief period in
153 which the object could have been removed since we found the
154 definition. */
155 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
156 while (runp != NULL && runp != map)
157 runp = runp->l_next;
158
159 if (runp != NULL)
160 {
161 /* The object is still available. Add the reference now. */
162 if (__builtin_expect (act >= undef_map->l_reldepsmax, 0))
163 {
164 /* Allocate more memory for the dependency list. Since this
165 can never happen during the startup phase we can use
166 `realloc'. */
167 void *newp;
168
169 undef_map->l_reldepsmax += 5;
170 newp = realloc (undef_map->l_reldeps,
171 undef_map->l_reldepsmax
172 * sizeof (struct link_map *));
173
174 if (__builtin_expect (newp != NULL, 1))
175 undef_map->l_reldeps = (struct link_map **) newp;
176 else
177 /* Correct the addition. */
178 undef_map->l_reldepsmax -= 5;
179 }
180
181 /* If we didn't manage to allocate memory for the list this is
182 no fatal mistake. We simply increment the use counter of the
183 referenced object and don't record the dependencies. This
184 means this increment can never be reverted and the object
185 will never be unloaded. This is semantically the correct
186 behavior. */
187 if (__builtin_expect (act < undef_map->l_reldepsmax, 1))
188 undef_map->l_reldeps[undef_map->l_reldepsact++] = map;
189
190 /* Display information if we are debugging. */
191 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
192 _dl_debug_printf ("\
193 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
194 map->l_name[0] ? map->l_name : rtld_progname,
195 map->l_ns,
196 undef_map->l_name[0]
197 ? undef_map->l_name : rtld_progname,
198 undef_map->l_ns);
199 }
200 else
201 /* Whoa, that was bad luck. We have to search again. */
202 result = -1;
203
204 out:
205 /* Release the lock. */
206 __rtld_lock_unlock_recursive (GL(dl_load_lock));
207
208 return result;
209 }
210
211 static void
212 internal_function
213 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
214 const ElfW(Sym) **ref, struct sym_val *value,
215 const struct r_found_version *version, int type_class,
216 int protected);
217
218
219 /* Search loaded objects' symbol tables for a definition of the symbol
220 UNDEF_NAME, perhaps with a requested version for the symbol.
221
222 We must never have calls to the audit functions inside this function
223 or in any function which gets called. If this would happen the audit
224 code might create a thread which can throw off all the scope locking. */
225 lookup_t
226 internal_function
227 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
228 const ElfW(Sym) **ref,
229 struct r_scope_elem *symbol_scope[],
230 const struct r_found_version *version,
231 int type_class, int flags, struct link_map *skip_map)
232 {
233 const uint_fast32_t new_hash = dl_new_hash (undef_name);
234 unsigned long int old_hash = 0xffffffff;
235 struct sym_val current_value = { NULL, NULL };
236 struct r_scope_elem **scope = symbol_scope;
237
238 bump_num_relocations ();
239
240 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY and DL_LOOKUP_SCOPE_LOCK
241 is allowed if we look up a versioned symbol. */
242 assert (version == NULL || (flags & ~(DL_LOOKUP_ADD_DEPENDENCY
243 | DL_LOOKUP_SCOPE_LOCK)) == 0);
244
245 size_t i = 0;
246 if (__builtin_expect (skip_map != NULL, 0))
247 /* Search the relevant loaded objects for a definition. */
248 while ((*scope)->r_list[i] != skip_map)
249 ++i;
250
251 /* Search the relevant loaded objects for a definition. */
252 for (size_t start = i; *scope != NULL; start = 0, ++scope)
253 {
254 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
255 &current_value, *scope, start, version, flags,
256 skip_map, type_class);
257 if (res > 0)
258 break;
259
260 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
261 {
262 /* Oh, oh. The file named in the relocation entry does not
263 contain the needed symbol. This code is never reached
264 for unversioned lookups. */
265 assert (version != NULL);
266 const char *reference_name = undef_map ? undef_map->l_name : NULL;
267
268 /* XXX We cannot translate the message. */
269 _dl_signal_cerror (0, (reference_name[0]
270 ? reference_name
271 : (rtld_progname ?: "<main program>")),
272 N_("relocation error"),
273 make_string ("symbol ", undef_name, ", version ",
274 version->name,
275 " not defined in file ",
276 version->filename,
277 " with link time reference",
278 res == -2
279 ? " (no version symbols)" : ""));
280 *ref = NULL;
281 return 0;
282 }
283 }
284
285 if (__builtin_expect (current_value.s == NULL, 0))
286 {
287 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
288 && skip_map == NULL)
289 {
290 /* We could find no value for a strong reference. */
291 const char *reference_name = undef_map ? undef_map->l_name : "";
292 const char *versionstr = version ? ", version " : "";
293 const char *versionname = (version && version->name
294 ? version->name : "");
295
296 /* XXX We cannot translate the message. */
297 _dl_signal_cerror (0, (reference_name[0]
298 ? reference_name
299 : (rtld_progname ?: "<main program>")),
300 N_("symbol lookup error"),
301 make_string (undefined_msg, undef_name,
302 versionstr, versionname));
303 }
304 *ref = NULL;
305 return 0;
306 }
307
308 int protected = (*ref
309 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
310 if (__builtin_expect (protected != 0, 0))
311 {
312 /* It is very tricky. We need to figure out what value to
313 return for the protected symbol. */
314 if (type_class == ELF_RTYPE_CLASS_PLT)
315 {
316 if (current_value.s != NULL && current_value.m != undef_map)
317 {
318 current_value.s = *ref;
319 current_value.m = undef_map;
320 }
321 }
322 else
323 {
324 struct sym_val protected_value = { NULL, NULL };
325
326 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
327 if (do_lookup_x (undef_name, new_hash, &old_hash, *ref,
328 &protected_value, *scope, i, version, flags,
329 skip_map, ELF_RTYPE_CLASS_PLT) != 0)
330 break;
331
332 if (protected_value.s != NULL && protected_value.m != undef_map)
333 {
334 current_value.s = *ref;
335 current_value.m = undef_map;
336 }
337 }
338 }
339
340 /* We have to check whether this would bind UNDEF_MAP to an object
341 in the global scope which was dynamically loaded. In this case
342 we have to prevent the latter from being unloaded unless the
343 UNDEF_MAP object is also unloaded. */
344 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
345 /* Don't do this for explicit lookups as opposed to implicit
346 runtime lookups. */
347 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
348 /* Add UNDEF_MAP to the dependencies. */
349 && add_dependency (undef_map, current_value.m, flags) < 0)
350 /* Something went wrong. Perhaps the object we tried to reference
351 was just removed. Try finding another definition. */
352 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
353 (flags & DL_LOOKUP_SCOPE_LOCK) == 0
354 ? symbol_scope : undef_map->l_scope, version,
355 type_class, flags, skip_map);
356
357 /* The object is used. */
358 current_value.m->l_used = 1;
359
360 if (__builtin_expect (GLRO(dl_debug_mask)
361 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
362 _dl_debug_bindings (undef_name, undef_map, ref,
363 &current_value, version, type_class, protected);
364
365 *ref = current_value.s;
366 return LOOKUP_VALUE (current_value.m);
367 }
368
369
370 /* Cache the location of MAP's hash table. */
371
372 void
373 internal_function
374 _dl_setup_hash (struct link_map *map)
375 {
376 Elf_Symndx *hash;
377 Elf_Symndx nchain;
378
379 if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
380 + DT_THISPROCNUM + DT_VERSIONTAGNUM
381 + DT_EXTRANUM + DT_VALNUM] != NULL, 1))
382 {
383 Elf32_Word *hash32
384 = (void *) D_PTR (map, l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
385 + DT_THISPROCNUM + DT_VERSIONTAGNUM
386 + DT_EXTRANUM + DT_VALNUM]);
387 map->l_nbuckets = *hash32++;
388 Elf32_Word symbias = *hash32++;
389 Elf32_Word bitmask_nwords = *hash32++;
390 /* Must be a power of two. */
391 assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
392 map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
393 map->l_gnu_shift = *hash32++;
394
395 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
396 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
397
398 map->l_gnu_buckets = hash32;
399 hash32 += map->l_nbuckets;
400 map->l_gnu_chain_zero = hash32 - symbias;
401 return;
402 }
403
404 if (!map->l_info[DT_HASH])
405 return;
406 hash = (void *) D_PTR (map, l_info[DT_HASH]);
407
408 map->l_nbuckets = *hash++;
409 nchain = *hash++;
410 map->l_buckets = hash;
411 hash += map->l_nbuckets;
412 map->l_chain = hash;
413 }
414
415
416 static void
417 internal_function
418 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
419 const ElfW(Sym) **ref, struct sym_val *value,
420 const struct r_found_version *version, int type_class,
421 int protected)
422 {
423 const char *reference_name = undef_map->l_name;
424
425 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
426 {
427 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
428 (reference_name[0]
429 ? reference_name
430 : (rtld_progname ?: "<main program>")),
431 undef_map->l_ns,
432 value->m->l_name[0] ? value->m->l_name : rtld_progname,
433 value->m->l_ns,
434 protected ? "protected" : "normal", undef_name);
435 if (version)
436 _dl_debug_printf_c (" [%s]\n", version->name);
437 else
438 _dl_debug_printf_c ("\n");
439 }
440 #ifdef SHARED
441 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
442 {
443 int conflict = 0;
444 struct sym_val val = { NULL, NULL };
445
446 if ((GLRO(dl_trace_prelink_map) == NULL
447 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
448 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
449 {
450 const uint_fast32_t new_hash = dl_new_hash (undef_name);
451 unsigned long int old_hash = 0xffffffff;
452
453 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
454 undef_map->l_local_scope[0], 0, version, 0, NULL,
455 type_class);
456
457 if (val.s != value->s || val.m != value->m)
458 conflict = 1;
459 }
460
461 if (value->s
462 && (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
463 == STT_TLS, 0)))
464 type_class = 4;
465
466 if (conflict
467 || GLRO(dl_trace_prelink_map) == undef_map
468 || GLRO(dl_trace_prelink_map) == NULL
469 || type_class == 4)
470 {
471 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
472 conflict ? "conflict" : "lookup",
473 (int) sizeof (ElfW(Addr)) * 2,
474 (size_t) undef_map->l_map_start,
475 (int) sizeof (ElfW(Addr)) * 2,
476 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
477 (int) sizeof (ElfW(Addr)) * 2,
478 (size_t) (value->s ? value->m->l_map_start : 0),
479 (int) sizeof (ElfW(Addr)) * 2,
480 (size_t) (value->s ? value->s->st_value : 0));
481
482 if (conflict)
483 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
484 (int) sizeof (ElfW(Addr)) * 2,
485 (size_t) (val.s ? val.m->l_map_start : 0),
486 (int) sizeof (ElfW(Addr)) * 2,
487 (size_t) (val.s ? val.s->st_value : 0));
488
489 _dl_printf ("/%x %s\n", type_class, undef_name);
490 }
491 }
492 #endif
493 }