Update.
[glibc.git] / elf / dl-lookup.c
blobf8bb9e17cae98d3eecbe3843ec5d5636046fb8c7
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995,96,97,98,99,2000,2001 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <alloca.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
25 #include "dl-hash.h"
26 #include <dl-machine.h>
27 #include <bits/libc-lock.h>
29 #include <assert.h>
31 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
33 /* We need this string more than once. */
34 static const char undefined_msg[] = "undefined symbol: ";
37 struct sym_val
39 const ElfW(Sym) *s;
40 struct link_map *m;
44 #define make_string(string, rest...) \
45 ({ \
46 const char *all[] = { string, ## rest }; \
47 size_t len, cnt; \
48 char *result, *cp; \
50 len = 1; \
51 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
52 len += strlen (all[cnt]); \
54 cp = result = alloca (len); \
55 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
56 cp = __stpcpy (cp, all[cnt]); \
58 result; \
61 /* Statistics function. */
62 unsigned long int _dl_num_relocations;
64 /* During the program run we must not modify the global data of
65 loaded shared object simultanously in two threads. Therefore we
66 protect `_dl_open' and `_dl_close' in dl-close.c.
68 This must be a recursive lock since the initializer function of
69 the loaded object might as well require a call to this function.
70 At this time it is not anymore a problem to modify the tables. */
71 __libc_lock_define (extern, _dl_load_lock)
74 /* We have two different situations when looking up a simple: with or
75 without versioning. gcc is not able to optimize a single function
76 definition serving for both purposes so we define two functions. */
77 #define VERSIONED 0
78 #define PROTECTED 0
79 #include "do-lookup.h"
81 #define VERSIONED 1
82 #define PROTECTED 0
83 #include "do-lookup.h"
86 /* Add extra dependency on MAP to UNDEF_MAP. */
87 static int
88 add_dependency (struct link_map *undef_map, struct link_map *map)
90 struct link_map **list;
91 unsigned int act;
92 unsigned int i;
93 int result = 0;
95 /* Make sure nobody can unload the object while we are at it. */
96 __libc_lock_lock (_dl_load_lock);
98 /* Determine whether UNDEF_MAP already has a reference to MAP. First
99 look in the normal dependencies. */
100 list = undef_map->l_initfini;
102 for (i = 0; list[i] != NULL; ++i)
103 if (list[i] == map)
104 break;
106 if (__builtin_expect (list[i] == NULL, 1))
108 /* No normal dependency. See whether we already had to add it
109 to the special list of dynamic dependencies. */
110 list = undef_map->l_reldeps;
111 act = undef_map->l_reldepsact;
113 for (i = 0; i < act; ++i)
114 if (list[i] == map)
115 break;
117 if (i == act)
119 /* The object is not yet in the dependency list. Before we add
120 it make sure just one more time the object we are about to
121 reference is still available. There is a brief period in
122 which the object could have been removed since we found the
123 definition. */
124 struct link_map *runp = _dl_loaded;
126 while (runp != NULL && runp != map)
127 runp = runp->l_next;
129 if (runp != NULL)
131 /* The object is still available. Add the reference now. */
132 if (__builtin_expect (act >= undef_map->l_reldepsmax, 0))
134 /* Allocate more memory for the dependency list. Since
135 this can never happen during the startup phase we can
136 use `realloc'. */
137 void *newp;
139 undef_map->l_reldepsmax += 5;
140 newp = realloc (undef_map->l_reldeps,
141 undef_map->l_reldepsmax
142 * sizeof(struct link_map *));
144 if (__builtin_expect (newp != NULL, 1))
145 undef_map->l_reldeps = (struct link_map **) newp;
146 else
147 /* Correct the addition. */
148 undef_map->l_reldepsmax -= 5;
151 /* If we didn't manage to allocate memory for the list this
152 is no fatal mistake. We simply increment the use counter
153 of the referenced object and don't record the dependencies.
154 This means this increment can never be reverted and the
155 object will never be unloaded. This is semantically the
156 correct behaviour. */
157 if (__builtin_expect (act < undef_map->l_reldepsmax, 1))
158 undef_map->l_reldeps[undef_map->l_reldepsact++] = map;
160 /* And increment the counter in the referenced object. */
161 ++map->l_opencount;
163 /* Display information if we are debugging. */
164 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_FILES, 0))
165 _dl_debug_printf ("\
166 \nfile=%s; needed by %s (relocation dependency)\n\n",
167 map->l_name[0] ? map->l_name : _dl_argv[0],
168 undef_map->l_name[0]
169 ? undef_map->l_name : _dl_argv[0]);
171 else
172 /* Whoa, that was bad luck. We have to search again. */
173 result = -1;
177 /* Release the lock. */
178 __libc_lock_unlock (_dl_load_lock);
180 return result;
184 /* Search loaded objects' symbol tables for a definition of the symbol
185 UNDEF_NAME. */
187 lookup_t
188 internal_function
189 _dl_lookup_symbol (const char *undef_name, struct link_map *undef_map,
190 const ElfW(Sym) **ref, struct r_scope_elem *symbol_scope[],
191 int reloc_type, int explicit)
193 const char *reference_name = undef_map ? undef_map->l_name : NULL;
194 const unsigned long int hash = _dl_elf_hash (undef_name);
195 struct sym_val current_value = { NULL, NULL };
196 struct r_scope_elem **scope;
197 int protected;
198 int noexec = elf_machine_lookup_noexec_p (reloc_type);
199 int noplt = elf_machine_lookup_noplt_p (reloc_type);
201 ++_dl_num_relocations;
203 /* Search the relevant loaded objects for a definition. */
204 for (scope = symbol_scope; *scope; ++scope)
205 if (do_lookup (undef_name, hash, *ref, &current_value, *scope, 0, NULL,
206 noexec, noplt))
208 /* We have to check whether this would bind UNDEF_MAP to an object
209 in the global scope which was dynamically loaded. In this case
210 we have to prevent the latter from being unloaded unless the
211 UNDEF_MAP object is also unloaded. */
212 if (__builtin_expect (current_value.m->l_global, 0)
213 && (__builtin_expect (current_value.m->l_type, lt_library)
214 == lt_loaded)
215 && undef_map != current_value.m
216 /* Don't do this for explicit lookups as opposed to implicit
217 runtime lookups. */
218 && __builtin_expect (! explicit, 1)
219 /* Add UNDEF_MAP to the dependencies. */
220 && add_dependency (undef_map, current_value.m) < 0)
221 /* Something went wrong. Perhaps the object we tried to reference
222 was just removed. Try finding another definition. */
223 return _dl_lookup_symbol (undef_name, undef_map, ref, symbol_scope,
224 reloc_type, 0);
226 break;
229 if (__builtin_expect (current_value.s == NULL, 0))
231 if (*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
232 /* We could find no value for a strong reference. */
233 /* XXX We cannot translate the messages. */
234 _dl_signal_cerror (0, (reference_name && reference_name[0]
235 ? reference_name
236 : (_dl_argv[0] ?: "<main program>")),
237 make_string (undefined_msg, undef_name));
238 *ref = NULL;
239 return 0;
242 protected = *ref && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED;
244 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_BINDINGS, 0))
245 _dl_debug_printf ("binding file %s to %s: %s symbol `%s'\n",
246 (reference_name && reference_name[0]
247 ? reference_name : (_dl_argv[0] ?: "<main program>")),
248 current_value.m->l_name[0]
249 ? current_value.m->l_name : _dl_argv[0],
250 protected ? "protected" : "normal", undef_name);
252 if (__builtin_expect (protected == 0, 1))
254 *ref = current_value.s;
255 return LOOKUP_VALUE (current_value.m);
257 else
259 /* It is very tricky. We need to figure out what value to
260 return for the protected symbol */
261 struct sym_val protected_value = { NULL, NULL };
263 for (scope = symbol_scope; *scope; ++scope)
264 if (do_lookup (undef_name, hash, *ref, &protected_value, *scope, 0,
265 NULL, 0, 1))
266 break;
268 if (protected_value.s == NULL || protected_value.m == undef_map)
270 *ref = current_value.s;
271 return LOOKUP_VALUE (current_value.m);
274 return LOOKUP_VALUE (undef_map);
279 /* This function is nearly the same as `_dl_lookup_symbol' but it
280 skips in the first list all objects until SKIP_MAP is found. I.e.,
281 it only considers objects which were loaded after the described
282 object. If there are more search lists the object described by
283 SKIP_MAP is only skipped. */
284 lookup_t
285 internal_function
286 _dl_lookup_symbol_skip (const char *undef_name,
287 struct link_map *undef_map, const ElfW(Sym) **ref,
288 struct r_scope_elem *symbol_scope[],
289 struct link_map *skip_map)
291 const char *reference_name = undef_map ? undef_map->l_name : NULL;
292 const unsigned long int hash = _dl_elf_hash (undef_name);
293 struct sym_val current_value = { NULL, NULL };
294 struct r_scope_elem **scope;
295 size_t i;
296 int protected;
298 ++_dl_num_relocations;
300 /* Search the relevant loaded objects for a definition. */
301 scope = symbol_scope;
302 for (i = 0; (*scope)->r_duplist[i] != skip_map; ++i)
303 assert (i < (*scope)->r_nduplist);
305 if (i >= (*scope)->r_nlist
306 || ! do_lookup (undef_name, hash, *ref, &current_value, *scope, i,
307 skip_map, 0, 0))
308 while (*++scope)
309 if (do_lookup (undef_name, hash, *ref, &current_value, *scope, 0,
310 skip_map, 0, 0))
311 break;
313 if (__builtin_expect (current_value.s == NULL, 0))
315 *ref = NULL;
316 return 0;
319 protected = *ref && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED;
321 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_BINDINGS, 0))
322 _dl_debug_printf ("binding file %s to %s: %s symbol `%s'\n",
323 (reference_name && reference_name[0]
324 ? reference_name : (_dl_argv[0] ?: "<main program>")),
325 current_value.m->l_name[0]
326 ? current_value.m->l_name : _dl_argv[0],
327 protected ? "protected" : "normal", undef_name);
329 if (__builtin_expect (protected == 0, 1))
331 *ref = current_value.s;
332 return LOOKUP_VALUE (current_value.m);
334 else
336 /* It is very tricky. We need to figure out what value to
337 return for the protected symbol. */
338 struct sym_val protected_value = { NULL, NULL };
340 if (i >= (*scope)->r_nlist
341 || !do_lookup (undef_name, hash, *ref, &protected_value, *scope, i,
342 skip_map, 0, 1))
343 while (*++scope)
344 if (do_lookup (undef_name, hash, *ref, &protected_value, *scope, 0,
345 skip_map, 0, 1))
346 break;
348 if (protected_value.s == NULL || protected_value.m == undef_map)
350 *ref = current_value.s;
351 return LOOKUP_VALUE (current_value.m);
354 return LOOKUP_VALUE (undef_map);
359 /* This function works like _dl_lookup_symbol but it takes an
360 additional arguement with the version number of the requested
361 symbol.
363 XXX We'll see whether we need this separate function. */
364 lookup_t
365 internal_function
366 _dl_lookup_versioned_symbol (const char *undef_name,
367 struct link_map *undef_map, const ElfW(Sym) **ref,
368 struct r_scope_elem *symbol_scope[],
369 const struct r_found_version *version,
370 int reloc_type, int explicit)
372 const char *reference_name = undef_map ? undef_map->l_name : NULL;
373 const unsigned long int hash = _dl_elf_hash (undef_name);
374 struct sym_val current_value = { NULL, NULL };
375 struct r_scope_elem **scope;
376 int protected;
377 int noexec = elf_machine_lookup_noexec_p (reloc_type);
378 int noplt = elf_machine_lookup_noplt_p (reloc_type);
380 ++_dl_num_relocations;
382 /* Search the relevant loaded objects for a definition. */
383 for (scope = symbol_scope; *scope; ++scope)
385 int res = do_lookup_versioned (undef_name, hash, *ref, &current_value,
386 *scope, 0, version, NULL, noexec, noplt);
387 if (res > 0)
389 /* We have to check whether this would bind UNDEF_MAP to an object
390 in the global scope which was dynamically loaded. In this case
391 we have to prevent the latter from being unloaded unless the
392 UNDEF_MAP object is also unloaded. */
393 if (__builtin_expect (current_value.m->l_global, 0)
394 && (__builtin_expect (current_value.m->l_type, lt_library)
395 == lt_loaded)
396 && undef_map != current_value.m
397 /* Don't do this for explicit lookups as opposed to implicit
398 runtime lookups. */
399 && __builtin_expect (! explicit, 1)
400 /* Add UNDEF_MAP to the dependencies. */
401 && add_dependency (undef_map, current_value.m) < 0)
402 /* Something went wrong. Perhaps the object we tried to reference
403 was just removed. Try finding another definition. */
404 return _dl_lookup_versioned_symbol (undef_name, undef_map, ref,
405 symbol_scope, version,
406 reloc_type, 0);
408 break;
411 if (__builtin_expect (res, 0) < 0)
413 /* Oh, oh. The file named in the relocation entry does not
414 contain the needed symbol. */
415 /* XXX We cannot translate the message. */
416 _dl_signal_cerror (0, (reference_name && reference_name[0]
417 ? reference_name
418 : (_dl_argv[0] ?: "<main program>")),
419 make_string ("symbol ", undef_name, ", version ",
420 version->name,
421 " not defined in file ",
422 version->filename,
423 " with link time reference",
424 res == -2
425 ? " (no version symbols)" : ""));
426 *ref = NULL;
427 return 0;
431 if (__builtin_expect (current_value.s == NULL, 0))
433 if (*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
434 /* We could find no value for a strong reference. */
435 /* XXX We cannot translate the message. */
436 _dl_signal_cerror (0, (reference_name && reference_name[0]
437 ? reference_name
438 : (_dl_argv[0] ?: "<main program>")),
439 make_string (undefined_msg, undef_name,
440 ", version ", version->name ?: NULL));
441 *ref = NULL;
442 return 0;
445 protected = *ref && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED;
447 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_BINDINGS, 0))
448 _dl_debug_printf ("binding file %s to %s: %s symbol `%s' [%s]\n",
449 (reference_name && reference_name[0]
450 ? reference_name : (_dl_argv[0] ?: "<main program>")),
451 current_value.m->l_name[0]
452 ? current_value.m->l_name : _dl_argv[0],
453 protected ? "protected" : "normal",
454 undef_name, version->name);
456 if (__builtin_expect (protected == 0, 1))
458 *ref = current_value.s;
459 return LOOKUP_VALUE (current_value.m);
461 else
463 /* It is very tricky. We need to figure out what value to
464 return for the protected symbol */
465 struct sym_val protected_value = { NULL, NULL };
467 for (scope = symbol_scope; *scope; ++scope)
468 if (do_lookup_versioned (undef_name, hash, *ref, &protected_value,
469 *scope, 0, version, NULL, 0, 1))
470 break;
472 if (protected_value.s == NULL || protected_value.m == undef_map)
474 *ref = current_value.s;
475 return LOOKUP_VALUE (current_value.m);
478 return LOOKUP_VALUE (undef_map);
483 /* Similar to _dl_lookup_symbol_skip but takes an additional argument
484 with the version we are looking for. */
485 lookup_t
486 internal_function
487 _dl_lookup_versioned_symbol_skip (const char *undef_name,
488 struct link_map *undef_map,
489 const ElfW(Sym) **ref,
490 struct r_scope_elem *symbol_scope[],
491 const struct r_found_version *version,
492 struct link_map *skip_map)
494 const char *reference_name = undef_map ? undef_map->l_name : NULL;
495 const unsigned long int hash = _dl_elf_hash (undef_name);
496 struct sym_val current_value = { NULL, NULL };
497 struct r_scope_elem **scope;
498 size_t i;
499 int protected;
501 ++_dl_num_relocations;
503 /* Search the relevant loaded objects for a definition. */
504 scope = symbol_scope;
505 for (i = 0; (*scope)->r_duplist[i] != skip_map; ++i)
506 assert (i < (*scope)->r_nduplist);
508 if (i >= (*scope)->r_nlist
509 || ! do_lookup_versioned (undef_name, hash, *ref, &current_value,
510 *scope, i, version, skip_map, 0, 0))
511 while (*++scope)
512 if (do_lookup_versioned (undef_name, hash, *ref, &current_value, *scope,
513 0, version, skip_map, 0, 0))
514 break;
516 if (__builtin_expect (current_value.s == NULL, 0))
518 if (*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
520 /* We could find no value for a strong reference. */
521 const size_t len = strlen (undef_name);
522 char buf[sizeof undefined_msg + len];
523 __mempcpy (__mempcpy (buf, undefined_msg, sizeof undefined_msg - 1),
524 undef_name, len + 1);
525 /* XXX We cannot translate the messages. */
526 _dl_signal_cerror (0, (reference_name && reference_name[0]
527 ? reference_name
528 : (_dl_argv[0] ?: "<main program>")), buf);
530 *ref = NULL;
531 return 0;
534 protected = *ref && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED;
536 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_BINDINGS, 0))
537 _dl_debug_printf ("binding file %s to %s: %s symbol `%s' [%s]\n",
538 (reference_name && reference_name[0]
539 ? reference_name : (_dl_argv[0] ?: "<main program>")),
540 current_value.m->l_name[0]
541 ? current_value.m->l_name : _dl_argv[0],
542 protected ? "protected" : "normal",
543 undef_name, version->name);
545 if (__builtin_expect (protected == 0, 1))
547 *ref = current_value.s;
548 return LOOKUP_VALUE (current_value.m);
550 else
552 /* It is very tricky. We need to figure out what value to
553 return for the protected symbol */
554 struct sym_val protected_value = { NULL, NULL };
556 if (i >= (*scope)->r_nlist
557 || !do_lookup_versioned (undef_name, hash, *ref, &protected_value,
558 *scope, i, version, skip_map, 0, 1))
559 while (*++scope)
560 if (do_lookup_versioned (undef_name, hash, *ref, &protected_value,
561 *scope, 0, version, skip_map, 0, 1))
562 break;
564 if (protected_value.s == NULL || protected_value.m == undef_map)
566 *ref = current_value.s;
567 return LOOKUP_VALUE (current_value.m);
570 return LOOKUP_VALUE (undef_map);
575 /* Cache the location of MAP's hash table. */
577 void
578 internal_function
579 _dl_setup_hash (struct link_map *map)
581 Elf_Symndx *hash;
582 Elf_Symndx nchain;
584 if (!map->l_info[DT_HASH])
585 return;
586 hash = (void *)(map->l_addr + map->l_info[DT_HASH]->d_un.d_ptr);
588 map->l_nbuckets = *hash++;
589 nchain = *hash++;
590 map->l_buckets = hash;
591 hash += map->l_nbuckets;
592 map->l_chain = hash;