(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / elf / dl-lookup.c
blobfdb076951d7ee75951127b6ed66cb08f8150a7db
1 /* Look up a symbol in the loaded objects.
2 Copyright (C) 1995-2002, 2003, 2004 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
18 02111-1307 USA. */
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 <bits/libc-lock.h>
29 #include <tls.h>
31 #include <assert.h>
33 #define VERSTAG(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (tag))
35 /* We need this string more than once. */
36 static const char undefined_msg[] = "undefined symbol: ";
39 struct sym_val
41 const ElfW(Sym) *s;
42 struct link_map *m;
46 #define make_string(string, rest...) \
47 ({ \
48 const char *all[] = { string, ## rest }; \
49 size_t len, cnt; \
50 char *result, *cp; \
52 len = 1; \
53 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
54 len += strlen (all[cnt]); \
56 cp = result = alloca (len); \
57 for (cnt = 0; cnt < sizeof (all) / sizeof (all[0]); ++cnt) \
58 cp = __stpcpy (cp, all[cnt]); \
60 result; \
63 /* Statistics function. */
64 #ifdef SHARED
65 # define bump_num_relocations() ++GL(dl_num_relocations)
66 #else
67 # define bump_num_relocations() ((void) 0)
68 #endif
71 /* The actual lookup code. */
72 #include "do-lookup.h"
75 /* Add extra dependency on MAP to UNDEF_MAP. */
76 static int
77 internal_function
78 add_dependency (struct link_map *undef_map, struct link_map *map)
80 struct link_map **list;
81 struct link_map *runp;
82 unsigned int act;
83 unsigned int i;
84 int result = 0;
86 /* Avoid self-references and references to objects which cannot be
87 unloaded anyway. */
88 if (undef_map == map)
89 return 0;
91 /* Make sure nobody can unload the object while we are at it. */
92 __rtld_lock_lock_recursive (GL(dl_load_lock));
94 /* Don't create cross-reference between modules which are
95 dynamically loaded by the same dlopen() call. */
96 if (undef_map->l_opencount == 0 && map->l_opencount == 0)
97 goto out;
99 /* Avoid references to objects which cannot be unloaded anyway. */
100 if (map->l_type != lt_loaded
101 || (map->l_flags_1 & DF_1_NODELETE) != 0)
102 goto out;
104 /* If the object with the undefined reference cannot be removed ever
105 just make sure the same is true for the object which contains the
106 definition. */
107 if (undef_map->l_type != lt_loaded
108 || (undef_map->l_flags_1 & DF_1_NODELETE) != 0)
110 ++map->l_opencount;
111 map->l_flags_1 |= DF_1_NODELETE;
112 goto out;
115 /* Determine whether UNDEF_MAP already has a reference to MAP. First
116 look in the normal dependencies. */
117 if (undef_map->l_searchlist.r_list != NULL)
119 list = undef_map->l_initfini;
121 for (i = 0; list[i] != NULL; ++i)
122 if (list[i] == map)
123 goto out;
126 /* No normal dependency. See whether we already had to add it
127 to the special list of dynamic dependencies. */
128 list = undef_map->l_reldeps;
129 act = undef_map->l_reldepsact;
131 for (i = 0; i < act; ++i)
132 if (list[i] == map)
133 goto out;
135 /* The object is not yet in the dependency list. Before we add
136 it make sure just one more time the object we are about to
137 reference is still available. There is a brief period in
138 which the object could have been removed since we found the
139 definition. */
140 runp = GL(dl_ns)[undef_map->l_ns]._ns_loaded;
141 while (runp != NULL && runp != map)
142 runp = runp->l_next;
144 if (runp != NULL)
146 /* The object is still available. Add the reference now. */
147 if (__builtin_expect (act >= undef_map->l_reldepsmax, 0))
149 /* Allocate more memory for the dependency list. Since this
150 can never happen during the startup phase we can use
151 `realloc'. */
152 void *newp;
154 undef_map->l_reldepsmax += 5;
155 newp = realloc (undef_map->l_reldeps,
156 undef_map->l_reldepsmax
157 * sizeof (struct link_map *));
159 if (__builtin_expect (newp != NULL, 1))
160 undef_map->l_reldeps = (struct link_map **) newp;
161 else
162 /* Correct the addition. */
163 undef_map->l_reldepsmax -= 5;
166 /* If we didn't manage to allocate memory for the list this is
167 no fatal mistake. We simply increment the use counter of the
168 referenced object and don't record the dependencies. This
169 means this increment can never be reverted and the object
170 will never be unloaded. This is semantically the correct
171 behavior. */
172 if (__builtin_expect (act < undef_map->l_reldepsmax, 1))
173 undef_map->l_reldeps[undef_map->l_reldepsact++] = map;
175 if (map->l_searchlist.r_list != NULL)
176 /* And increment the counter in the referenced object. */
177 ++map->l_opencount;
178 else
179 /* We have to bump the counts for all dependencies since so far
180 this object was only a normal or transitive dependency.
181 Now it might be closed with _dl_close() directly. */
182 for (list = map->l_initfini; *list != NULL; ++list)
183 ++(*list)->l_opencount;
185 /* As if it is opened through _dl_open. */
186 ++map->l_direct_opencount;
188 /* Display information if we are debugging. */
189 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0))
190 _dl_debug_printf ("\
191 \nfile=%s [%lu]; needed by %s [%lu] (relocation dependency)\n\n",
192 map->l_name[0] ? map->l_name : rtld_progname,
193 map->l_ns,
194 undef_map->l_name[0]
195 ? undef_map->l_name : rtld_progname,
196 undef_map->l_ns);
198 else
199 /* Whoa, that was bad luck. We have to search again. */
200 result = -1;
202 out:
203 /* Release the lock. */
204 __rtld_lock_unlock_recursive (GL(dl_load_lock));
206 return result;
209 static void
210 internal_function
211 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
212 const ElfW(Sym) **ref, struct r_scope_elem *symbol_scope[],
213 struct sym_val *value,
214 const struct r_found_version *version, int type_class,
215 int protected);
218 /* Search loaded objects' symbol tables for a definition of the symbol
219 UNDEF_NAME, perhaps with a requested version for the symbol. */
220 lookup_t
221 internal_function
222 _dl_lookup_symbol_x (const char *undef_name, struct link_map *undef_map,
223 const ElfW(Sym) **ref,
224 struct r_scope_elem *symbol_scope[],
225 const struct r_found_version *version,
226 int type_class, int flags, struct link_map *skip_map)
228 const unsigned long int hash = _dl_elf_hash (undef_name);
229 struct sym_val current_value = { NULL, NULL };
230 struct r_scope_elem **scope = symbol_scope;
232 bump_num_relocations ();
234 /* No other flag than DL_LOOKUP_ADD_DEPENDENCY is allowed if we look
235 up a versioned symbol. */
236 assert (version == NULL || flags == 0 || flags == DL_LOOKUP_ADD_DEPENDENCY);
238 size_t i = 0;
239 if (__builtin_expect (skip_map != NULL, 0))
241 /* Search the relevant loaded objects for a definition. */
242 while ((*scope)->r_list[i] != skip_map)
243 ++i;
245 assert (i < (*scope)->r_nlist);
248 /* Search the relevant loaded objects for a definition. */
249 for (size_t start = i; *scope != NULL; start = 0, ++scope)
251 int res = do_lookup_x (undef_name, hash, *ref, &current_value, *scope,
252 start, version, flags, skip_map, type_class);
253 if (res > 0)
254 break;
256 if (__builtin_expect (res, 0) < 0 && skip_map == NULL)
258 /* Oh, oh. The file named in the relocation entry does not
259 contain the needed symbol. This code is never reached
260 for unversioned lookups. */
261 assert (version != NULL);
262 const char *reference_name = undef_map ? undef_map->l_name : NULL;
264 /* XXX We cannot translate the message. */
265 _dl_signal_cerror (0, (reference_name[0]
266 ? reference_name
267 : (rtld_progname ?: "<main program>")),
268 N_("relocation error"),
269 make_string ("symbol ", undef_name, ", version ",
270 version->name,
271 " not defined in file ",
272 version->filename,
273 " with link time reference",
274 res == -2
275 ? " (no version symbols)" : ""));
276 *ref = NULL;
277 return 0;
281 if (__builtin_expect (current_value.s == NULL, 0))
283 if ((*ref == NULL || ELFW(ST_BIND) ((*ref)->st_info) != STB_WEAK)
284 && skip_map == NULL)
286 /* We could find no value for a strong reference. */
287 const char *reference_name = undef_map ? undef_map->l_name : "";
288 const char *versionstr = version ? ", version " : "";
289 const char *versionname = (version && version->name
290 ? version->name : "");
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_("symbol lookup error"),
297 make_string (undefined_msg, undef_name,
298 versionstr, versionname));
300 *ref = NULL;
301 return 0;
304 int protected = (*ref
305 && ELFW(ST_VISIBILITY) ((*ref)->st_other) == STV_PROTECTED);
306 if (__builtin_expect (protected != 0, 0))
308 /* It is very tricky. We need to figure out what value to
309 return for the protected symbol. */
310 if (type_class == ELF_RTYPE_CLASS_PLT)
312 if (current_value.s != NULL && current_value.m != undef_map)
314 current_value.s = *ref;
315 current_value.m = undef_map;
318 else
320 struct sym_val protected_value = { NULL, NULL };
322 for (scope = symbol_scope; *scope != NULL; i = 0, ++scope)
323 if (do_lookup_x (undef_name, hash, *ref, &protected_value,
324 *scope, i, version, flags, skip_map,
325 ELF_RTYPE_CLASS_PLT) != 0)
326 break;
328 if (protected_value.s != NULL && protected_value.m != undef_map)
330 current_value.s = *ref;
331 current_value.m = undef_map;
336 /* We have to check whether this would bind UNDEF_MAP to an object
337 in the global scope which was dynamically loaded. In this case
338 we have to prevent the latter from being unloaded unless the
339 UNDEF_MAP object is also unloaded. */
340 if (__builtin_expect (current_value.m->l_type == lt_loaded, 0)
341 /* Don't do this for explicit lookups as opposed to implicit
342 runtime lookups. */
343 && (flags & DL_LOOKUP_ADD_DEPENDENCY) != 0
344 /* Add UNDEF_MAP to the dependencies. */
345 && add_dependency (undef_map, current_value.m) < 0)
346 /* Something went wrong. Perhaps the object we tried to reference
347 was just removed. Try finding another definition. */
348 return _dl_lookup_symbol_x (undef_name, undef_map, ref,
349 symbol_scope, version, type_class,
350 flags, skip_map);
352 /* The object is used. */
353 current_value.m->l_used = 1;
355 if (__builtin_expect (GLRO(dl_debug_mask)
356 & (DL_DEBUG_BINDINGS|DL_DEBUG_PRELINK), 0))
357 _dl_debug_bindings (undef_name, undef_map, ref, symbol_scope,
358 &current_value, version, type_class, protected);
360 *ref = current_value.s;
361 return LOOKUP_VALUE (current_value.m);
365 /* Cache the location of MAP's hash table. */
367 void
368 internal_function
369 _dl_setup_hash (struct link_map *map)
371 Elf_Symndx *hash;
372 Elf_Symndx nchain;
374 if (!map->l_info[DT_HASH])
375 return;
376 hash = (void *) D_PTR (map, l_info[DT_HASH]);
378 map->l_nbuckets = *hash++;
379 nchain = *hash++;
380 map->l_buckets = hash;
381 hash += map->l_nbuckets;
382 map->l_chain = hash;
386 static void
387 internal_function
388 _dl_debug_bindings (const char *undef_name, struct link_map *undef_map,
389 const ElfW(Sym) **ref, struct r_scope_elem *symbol_scope[],
390 struct sym_val *value,
391 const struct r_found_version *version, int type_class,
392 int protected)
394 const char *reference_name = undef_map->l_name;
396 if (GLRO(dl_debug_mask) & DL_DEBUG_BINDINGS)
398 _dl_debug_printf ("binding file %s to %s: %s symbol `%s'",
399 (reference_name[0]
400 ? reference_name
401 : (rtld_progname ?: "<main program>")),
402 value->m->l_name[0] ? value->m->l_name : rtld_progname,
403 protected ? "protected" : "normal", undef_name);
404 if (version)
405 _dl_debug_printf_c (" [%s]\n", version->name);
406 else
407 _dl_debug_printf_c ("\n");
409 #ifdef SHARED
410 if (GLRO(dl_debug_mask) & DL_DEBUG_PRELINK)
412 int conflict = 0;
413 struct sym_val val = { NULL, NULL };
415 if ((GLRO(dl_trace_prelink_map) == NULL
416 || GLRO(dl_trace_prelink_map) == GL(dl_ns)[LM_ID_BASE]._ns_loaded)
417 && undef_map != GL(dl_ns)[LM_ID_BASE]._ns_loaded)
419 const unsigned long int hash = _dl_elf_hash (undef_name);
421 do_lookup_x (undef_name, hash, *ref, &val,
422 undef_map->l_local_scope[0], 0, version, 0, NULL,
423 type_class);
425 if (val.s != value->s || val.m != value->m)
426 conflict = 1;
429 # ifdef USE_TLS
430 if (value->s
431 && (__builtin_expect (ELFW(ST_TYPE) (value->s->st_info)
432 == STT_TLS, 0)))
433 type_class = 4;
434 # endif
436 if (conflict
437 || GLRO(dl_trace_prelink_map) == undef_map
438 || GLRO(dl_trace_prelink_map) == NULL
439 || type_class == 4)
441 _dl_printf ("%s 0x%0*Zx 0x%0*Zx -> 0x%0*Zx 0x%0*Zx ",
442 conflict ? "conflict" : "lookup",
443 (int) sizeof (ElfW(Addr)) * 2,
444 (size_t) undef_map->l_map_start,
445 (int) sizeof (ElfW(Addr)) * 2,
446 (size_t) (((ElfW(Addr)) *ref) - undef_map->l_map_start),
447 (int) sizeof (ElfW(Addr)) * 2,
448 (size_t) (value->s ? value->m->l_map_start : 0),
449 (int) sizeof (ElfW(Addr)) * 2,
450 (size_t) (value->s ? value->s->st_value : 0));
452 if (conflict)
453 _dl_printf ("x 0x%0*Zx 0x%0*Zx ",
454 (int) sizeof (ElfW(Addr)) * 2,
455 (size_t) (val.s ? val.m->l_map_start : 0),
456 (int) sizeof (ElfW(Addr)) * 2,
457 (size_t) (val.s ? val.s->st_value : 0));
459 _dl_printf ("/%x %s\n", type_class, undef_name);
462 #endif