symbol.c revision 1.55 1 /* $NetBSD: symbol.c,v 1.55 2011/03/09 23:10:07 joerg Exp $ */
2
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root (at) ihack.net>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John Polstra.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * Dynamic linker for ELF.
37 *
38 * John Polstra <jdp (at) polstra.com>.
39 */
40
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: symbol.c,v 1.55 2011/03/09 23:10:07 joerg Exp $");
44 #endif /* not lint */
45
46 #include <err.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 #include <sys/types.h>
55 #include <sys/mman.h>
56 #include <sys/bitops.h>
57 #include <dirent.h>
58
59 #include "debug.h"
60 #include "rtld.h"
61
62 typedef void (*fptr_t)(void);
63
64 /*
65 * If the given object is already in the donelist, return true. Otherwise
66 * add the object to the list and return false.
67 */
68 static bool
69 _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
70 {
71 unsigned int i;
72
73 for (i = 0; i < dlp->num_used; i++)
74 if (dlp->objs[i] == obj)
75 return true;
76 /*
77 * Our donelist allocation may not always be sufficient as we're not
78 * thread safe. We'll handle it properly anyway.
79 */
80 if (dlp->num_used < dlp->num_alloc)
81 dlp->objs[dlp->num_used++] = obj;
82 return false;
83 }
84
85 static bool
86 _rtld_is_exported(const Elf_Sym *def)
87 {
88 static const fptr_t _rtld_exports[] = {
89 (fptr_t)dlopen,
90 (fptr_t)dlclose,
91 (fptr_t)dlsym,
92 (fptr_t)dlerror,
93 (fptr_t)dladdr,
94 (fptr_t)dlinfo,
95 (fptr_t)dl_iterate_phdr,
96 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
97 (fptr_t)_rtld_tls_allocate,
98 (fptr_t)_rtld_tls_free,
99 (fptr_t)__tls_get_addr,
100 #endif
101 NULL
102 };
103 int i;
104 fptr_t value;
105
106 value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
107 for (i = 0; _rtld_exports[i] != NULL; i++) {
108 if (value == _rtld_exports[i])
109 return true;
110 }
111 return false;
112 }
113
114 /*
115 * Hash function for symbol table lookup. Don't even think about changing
116 * this. It is specified by the System V ABI.
117 */
118 unsigned long
119 _rtld_elf_hash(const char *name)
120 {
121 const unsigned char *p = (const unsigned char *) name;
122 unsigned long h = 0;
123 unsigned long g;
124 unsigned long c;
125
126 for (; __predict_true((c = *p) != '\0'); p++) {
127 h <<= 4;
128 h += c;
129 if ((g = h & 0xf0000000) != 0) {
130 h ^= g;
131 h ^= g >> 24;
132 }
133 }
134 return (h);
135 }
136
137 const Elf_Sym *
138 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
139 const Obj_Entry **defobj_out, bool in_plt, DoneList *dlp)
140 {
141 const Elf_Sym *symp;
142 const Elf_Sym *def;
143 const Obj_Entry *defobj;
144 const Objlist_Entry *elm;
145
146 def = NULL;
147 defobj = NULL;
148 SIMPLEQ_FOREACH(elm, objlist, link) {
149 if (_rtld_donelist_check(dlp, elm->obj))
150 continue;
151 rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
152 name));
153 if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
154 != NULL) {
155 if ((def == NULL) ||
156 (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
157 def = symp;
158 defobj = elm->obj;
159 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
160 break;
161 }
162 }
163 }
164 if (def != NULL)
165 *defobj_out = defobj;
166 return def;
167 }
168
169 /*
170 * Search the symbol table of a shared object and all objects needed by it for
171 * a symbol of the given name. Search order is breadth-first. Returns a pointer
172 * to the symbol, or NULL if no definition was found.
173 */
174 const Elf_Sym *
175 _rtld_symlook_needed(const char *name, unsigned long hash,
176 const Needed_Entry *needed, const Obj_Entry **defobj_out, bool inplt,
177 DoneList *breadth, DoneList *depth)
178 {
179 const Elf_Sym *def, *def_w;
180 const Needed_Entry *n;
181 const Obj_Entry *obj, *defobj, *defobj1;
182
183 def = def_w = NULL;
184 defobj = NULL;
185 for (n = needed; n != NULL; n = n->next) {
186 if ((obj = n->obj) == NULL)
187 continue;
188 if (_rtld_donelist_check(breadth, obj))
189 continue;
190 if ((def = _rtld_symlook_obj(name, hash, obj, inplt)) == NULL)
191 continue;
192 defobj = obj;
193 if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
194 *defobj_out = defobj;
195
196 return (def);
197 }
198 }
199 /*
200 * Either the symbol definition has not been found in directly needed
201 * objects, or the found symbol is weak.
202 */
203 for (n = needed; n != NULL; n = n->next) {
204 if ((obj = n->obj) == NULL)
205 continue;
206 if (_rtld_donelist_check(depth, obj))
207 continue;
208 def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
209 inplt, breadth, depth);
210 if (def_w == NULL)
211 continue;
212 if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
213 def = def_w;
214 defobj = defobj1;
215 if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
216 break;
217 }
218 }
219 if (def != NULL)
220 *defobj_out = defobj;
221
222 return def;
223 }
224
225 /*
226 * Search the symbol table of a single shared object for a symbol of
227 * the given name. Returns a pointer to the symbol, or NULL if no
228 * definition was found.
229 *
230 * The symbol's hash value is passed in for efficiency reasons; that
231 * eliminates many recomputations of the hash value.
232 */
233 const Elf_Sym *
234 _rtld_symlook_obj(const char *name, unsigned long hash,
235 const Obj_Entry *obj, bool in_plt)
236 {
237 unsigned long symnum;
238
239 for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
240 obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
241 symnum != ELF_SYM_UNDEFINED;
242 symnum = obj->chains[symnum]) {
243 const Elf_Sym *symp;
244 const char *strp;
245
246 assert(symnum < obj->nchains);
247 symp = obj->symtab + symnum;
248 strp = obj->strtab + symp->st_name;
249 rdbg(("check \"%s\" vs \"%s\" in %p", name, strp, obj));
250 if (name[1] == strp[1] && !strcmp(name, strp)) {
251 if (symp->st_shndx != SHN_UNDEF)
252 return symp;
253 #ifndef __mips__
254 /*
255 * XXX DANGER WILL ROBINSON!
256 * If we have a function pointer in the executable's
257 * data section, it points to the executable's PLT
258 * slot, and there is NO relocation emitted. To make
259 * the function pointer comparable to function pointers
260 * in shared libraries, we must resolve data references
261 * in the libraries to point to PLT slots in the
262 * executable, if they exist.
263 */
264 else if (!in_plt && symp->st_value != 0 &&
265 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
266 return symp;
267 #endif
268 else
269 return NULL;
270 }
271 }
272
273 return NULL;
274 }
275
276 #ifdef COMBRELOC
277 static const Obj_Entry *_rtld_last_refobj;
278
279 /*
280 * Called when an object is freed. Reset the cached symbol look up if
281 * our last referencing or definition object just got unloaded.
282 */
283 void
284 _rtld_combreloc_reset(const Obj_Entry *obj)
285 {
286 if (_rtld_last_refobj == obj)
287 _rtld_last_refobj = NULL;
288 }
289 #endif
290
291 /*
292 * Given a symbol number in a referencing object, find the corresponding
293 * definition of the symbol. Returns a pointer to the symbol, or NULL if
294 * no definition was found. Returns a pointer to the Obj_Entry of the
295 * defining object via the reference parameter DEFOBJ_OUT.
296 */
297 const Elf_Sym *
298 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
299 const Obj_Entry **defobj_out, bool in_plt)
300 {
301 const Elf_Sym *ref;
302 const Elf_Sym *def;
303 const Obj_Entry *defobj;
304 const char *name;
305 unsigned long hash;
306
307 #ifdef COMBRELOC
308 /*
309 * COMBRELOC combines multiple reloc sections and sorts them to make
310 * dynamic symbol lookup caching possible.
311 *
312 * So if the lookup we are doing is the same as the previous lookup
313 * return the cached results.
314 */
315 static unsigned long last_symnum;
316 static const Obj_Entry *last_defobj;
317 static const Elf_Sym *last_def;
318
319 if (symnum == last_symnum && refobj == _rtld_last_refobj
320 && in_plt == false) {
321 *defobj_out = last_defobj;
322 return last_def;
323 }
324 #endif
325
326 ref = refobj->symtab + symnum;
327 name = refobj->strtab + ref->st_name;
328
329 /*
330 * We don't have to do a full scale lookup if the symbol is local.
331 * We know it will bind to the instance in this load module; to
332 * which we already have a pointer (ie ref).
333 */
334 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
335 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
336 _rtld_error("%s: Bogus symbol table entry %lu",
337 refobj->path, symnum);
338 }
339
340 hash = _rtld_elf_hash(name);
341 defobj = NULL;
342 def = _rtld_symlook_default(name, hash, refobj, &defobj, in_plt);
343 } else {
344 rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
345 def = ref;
346 defobj = refobj;
347 }
348
349 /*
350 * If we found no definition and the reference is weak, treat the
351 * symbol as having the value zero.
352 */
353 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
354 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
355 def = &_rtld_sym_zero;
356 defobj = &_rtld_objself;
357 }
358
359 if (def != NULL) {
360 *defobj_out = defobj;
361 #ifdef COMBRELOC
362 if (in_plt == false) {
363 /*
364 * Cache the lookup arguments and results if this was
365 * non-PLT lookup.
366 */
367 last_symnum = symnum;
368 _rtld_last_refobj = refobj;
369 last_def = def;
370 last_defobj = defobj;
371 }
372 #endif
373 } else {
374 rdbg(("lookup failed"));
375 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
376 refobj->path, in_plt ? "PLT " : "", name, symnum);
377 }
378 return def;
379 }
380
381 const Elf_Sym *
382 _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
383 const Obj_Entry **defobj, bool imm)
384 {
385 const Elf_Sym *def = _rtld_find_symdef(symnum, obj, defobj, true);
386 if (__predict_false(def == NULL))
387 return NULL;
388
389 if (__predict_false(def == &_rtld_sym_zero)) {
390 /* tp is set during lazy binding. */
391 if (imm) {
392 const Elf_Sym *ref = obj->symtab + symnum;
393 const char *name = obj->strtab + ref->st_name;
394
395 _rtld_error(
396 "%s: Trying to call undefined weak symbol `%s'",
397 obj->path, name);
398 return NULL;
399 }
400 }
401 return def;
402 }
403
404 /*
405 * Given a symbol name in a referencing object, find the corresponding
406 * definition of the symbol. Returns a pointer to the symbol, or NULL if
407 * no definition was found. Returns a pointer to the Obj_Entry of the
408 * defining object via the reference parameter DEFOBJ_OUT.
409 */
410 const Elf_Sym *
411 _rtld_symlook_default(const char *name, unsigned long hash,
412 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
413 {
414 const Elf_Sym *def;
415 const Elf_Sym *symp;
416 const Obj_Entry *obj;
417 const Obj_Entry *defobj;
418 const Objlist_Entry *elm;
419 def = NULL;
420 defobj = NULL;
421 DoneList donelist;
422
423 _rtld_donelist_init(&donelist);
424
425 /* Look first in the referencing object if linked symbolically. */
426 if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
427 rdbg(("search referencing object for %s", name));
428 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
429 if (symp != NULL) {
430 def = symp;
431 defobj = refobj;
432 }
433 }
434
435 /* Search all objects loaded at program start up. */
436 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
437 rdbg(("search _rtld_list_main for %s", name));
438 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
439 in_plt, &donelist);
440 if (symp != NULL &&
441 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
442 def = symp;
443 defobj = obj;
444 }
445 }
446
447 /* Search all RTLD_GLOBAL objects. */
448 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
449 rdbg(("search _rtld_list_global for %s", name));
450 symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
451 &obj, in_plt, &donelist);
452 if (symp != NULL &&
453 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
454 def = symp;
455 defobj = obj;
456 }
457 }
458
459 /* Search all dlopened DAGs containing the referencing object. */
460 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
461 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
462 break;
463 rdbg(("search DAG with root %p (%s) for %s", elm->obj,
464 elm->obj->path, name));
465 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
466 &obj, in_plt, &donelist);
467 if (symp != NULL &&
468 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
469 def = symp;
470 defobj = obj;
471 }
472 }
473
474 /*
475 * Search the dynamic linker itself, and possibly resolve the
476 * symbol from there. This is how the application links to
477 * dynamic linker services such as dlopen. Only the values listed
478 * in the "_rtld_exports" array can be resolved from the dynamic
479 * linker.
480 */
481 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
482 rdbg(("Search the dynamic linker itself."));
483 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
484 if (symp != NULL && _rtld_is_exported(symp)) {
485 def = symp;
486 defobj = &_rtld_objself;
487 }
488 }
489
490 if (def != NULL)
491 *defobj_out = defobj;
492 return def;
493 }
494