]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-lookup.c
* sysdeps/generic/ldsodefs.h (DL_LOOKUP_GSCOPE_LOCK): New definition.
[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 unsigned long long int serial;
97
98 /* Avoid self-references and references to objects which cannot be
99 unloaded anyway. */
100 if (undef_map == map)
101 return 0;
102
103 /* Save serial number of the target MAP. */
104 serial = map->l_serial;
105
106 /* Make sure nobody can unload the object while we are at it. */
107 if (__builtin_expect (flags & DL_LOOKUP_GSCOPE_LOCK, 0))
108 {
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);
119 }
120 else
121 __rtld_lock_lock_recursive (GL(dl_load_lock));
122
123 /* From this point on it is unsafe to dereference MAP, until it
124 has been found in one of the lists. */
125
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)
129 {
130 list = undef_map->l_initfini;
131
132 for (i = 0; list[i] != NULL; ++i)
133 if (list[i] == map)
134 goto out_check;
135 }
136
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;
141
142 for (i = 0; i < act; ++i)
143 if (list[i] == map)
144 goto out_check;
145
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
150 definition. */
151 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
152 while (runp != NULL && runp != map)
153 runp = runp->l_next;
154
155 if (runp != NULL)
156 {
157 /* The object is still available. */
158
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)
162 goto out_check;
163
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)
167 goto out;
168
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
171 definition. */
172 if (undef_map->l_type != lt_loaded
173 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
174 {
175 map->l_flags_1 |= DF_1_NODELETE;
176 goto out;
177 }
178
179 /* Add the reference now. */
180 if (__builtin_expect (act >= undef_map->l_reldepsmax, 0))
181 {
182 /* Allocate more memory for the dependency list. Since this
183 can never happen during the startup phase we can use
184 `realloc'. */
185 void *newp;
186
187 undef_map->l_reldepsmax += 5;
188 newp = realloc (undef_map->l_reldeps,
189 undef_map->l_reldepsmax
190 * sizeof (struct link_map *));
191
192 if (__builtin_expect (newp != NULL, 1))
193 undef_map->l_reldeps = (struct link_map **) newp;
194 else
195 /* Correct the addition. */
196 undef_map->l_reldepsmax -= 5;
197 }
198
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
204 behavior. */
205 if (__builtin_expect (act < undef_map->l_reldepsmax, 1))
206 undef_map->l_reldeps[undef_map->l_reldepsact++] = map;
207
208 /* Display information if we are debugging. */
209 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
210 _dl_debug_printf ("\
211 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
212 map->l_name[0] ? map->l_name : rtld_progname,
213 map->l_ns,
214 undef_map->l_name[0]
215 ? undef_map->l_name : rtld_progname,
216 undef_map->l_ns);
217 }
218 else
219 /* Whoa, that was bad luck. We have to search again. */
220 result = -1;
221
222 out:
223 /* Release the lock. */
224 __rtld_lock_unlock_recursive (GL(dl_load_lock));
225
226 return result;
227
228 out_check:
229 if (map->l_serial != serial)
230 result = -1;
231 goto out;
232 }
233
234 static void
235 internal_function
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,
239 int protected);
240
241
242 /* Search loaded objects' symbol tables for a definition of the symbol
243 UNDEF_NAME, perhaps with a requested version for the symbol.
244
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. */
248 lookup_t
249 internal_function
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)
255 {
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;
260
261 bump_num_relocations ();
262
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))
267 == 0);
268
269 size_t i = 0;
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)
273 ++i;
274
275 /* Search the relevant loaded objects for a definition. */
276 for (size_t start = i; *scope != NULL; start = 0, ++scope)
277 {
278 int res = do_lookup_x (undef_name, new_hash, &old_hash, *ref,
279 &current_value, *scope, start, version, flags,
280 skip_map, type_class);
281 if (res > 0)
282 break;
283
284 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
285 {
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;
291
292 /* XXX We cannot translate the message. */
293 _dl_signal_cerror (0, (reference_name[0]
294 ? reference_name
295 : (rtld_progname ?: "<main program>")),
296 N_("relocation error"),
297 make_string ("symbol ", undef_name, ", version ",
298 version->name,
299 " not defined in file ",
300 version->filename,
301 " with link time reference",
302 res == -2
303 ? " (no version symbols)" : ""));
304 *ref = NULL;
305 return 0;
306 }
307 }
308
309 if (__builtin_expect (current_value.s == NULL, 0))
310 {
311 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
312 && skip_map == NULL)
313 {
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 : "");
319
320 /* XXX We cannot translate the message. */
321 _dl_signal_cerror (0, (reference_name[0]
322 ? reference_name
323 : (rtld_progname ?: "<main program>")),
324 N_("symbol lookup error"),
325 make_string (undefined_msg, undef_name,
326 versionstr, versionname));
327 }
328 *ref = NULL;
329 return 0;
330 }
331
332 int protected = (*ref
333 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
334 if (__builtin_expect (protected != 0, 0))
335 {
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)
339 {
340 if (current_value.s != NULL && current_value.m != undef_map)
341 {
342 current_value.s = *ref;
343 current_value.m = undef_map;
344 }
345 }
346 else
347 {
348 struct sym_val protected_value = { NULL, NULL };
349
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)
354 break;
355
356 if (protected_value.s != NULL && protected_value.m != undef_map)
357 {
358 current_value.s = *ref;
359 current_value.m = undef_map;
360 }
361 }
362 }
363
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
370 runtime lookups. */
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);
380
381 /* The object is used. */
382 current_value.m->l_used = 1;
383
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 &current_value, version, type_class, protected);
388
389 *ref = current_value.s;
390 return LOOKUP_VALUE (current_value.m);
391 }
392
393
394 /* Cache the location of MAP's hash table. */
395
396 void
397 internal_function
398 _dl_setup_hash (struct link_map *map)
399 {
400 Elf_Symndx *hash;
401 Elf_Symndx nchain;
402
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))
406 {
407 Elf32_Word *hash32
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++;
418
419 map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
420 hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
421
422 map->l_gnu_buckets = hash32;
423 hash32 += map->l_nbuckets;
424 map->l_gnu_chain_zero = hash32 - symbias;
425 return;
426 }
427
428 if (!map->l_info[DT_HASH])
429 return;
430 hash = (void *) D_PTR (map, l_info[DT_HASH]);
431
432 map->l_nbuckets = *hash++;
433 nchain = *hash++;
434 map->l_buckets = hash;
435 hash += map->l_nbuckets;
436 map->l_chain = hash;
437 }
438
439
440 static void
441 internal_function
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,
445 int protected)
446 {
447 const char *reference_name = undef_map->l_name;
448
449 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
450 {
451 _dl_debug_printf ("binding file %s [%lu] to %s [%lu]: %s symbol `%s'",
452 (reference_name[0]
453 ? reference_name
454 : (rtld_progname ?: "<main program>")),
455 undef_map->l_ns,
456 value->m->l_name[0] ? value->m->l_name : rtld_progname,
457 value->m->l_ns,
458 protected ? "protected" : "normal", undef_name);
459 if (version)
460 _dl_debug_printf_c (" [%s]\n", version->name);
461 else
462 _dl_debug_printf_c ("\n");
463 }
464 #ifdef SHARED
465 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
466 {
467 int conflict = 0;
468 struct sym_val val = { NULL, NULL };
469
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)
473 {
474 const uint_fast32_t new_hash = dl_new_hash (undef_name);
475 unsigned long int old_hash = 0xffffffff;
476
477 do_lookup_x (undef_name, new_hash, &old_hash, *ref, &val,
478 undef_map->l_local_scope[0], 0, version, 0, NULL,
479 type_class);
480
481 if (val.s != value->s || val.m != value->m)
482 conflict = 1;
483 }
484
485 if (value->s
486 && (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
487 == STT_TLS, 0)))
488 type_class = 4;
489
490 if (conflict
491 || GLRO(dl_trace_prelink_map) == undef_map
492 || GLRO(dl_trace_prelink_map) == NULL
493 || type_class == 4)
494 {
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));
505
506 if (conflict)
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));
512
513 _dl_printf ("/%x %s\n", type_class, undef_name);
514 }
515 }
516 #endif
517 }