symbol.c revision 1.51 1 /* $NetBSD: symbol.c,v 1.51 2010/02/27 11:16:38 roy 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.51 2010/02/27 11:16:38 roy 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 <dirent.h>
57
58 #include "debug.h"
59 #include "rtld.h"
60
61 typedef void (*fptr_t)(void);
62
63 /*
64 * If the given object is already in the donelist, return true. Otherwise
65 * add the object to the list and return false.
66 */
67 static bool
68 _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
69 {
70 unsigned int i;
71
72 for (i = 0; i < dlp->num_used; i++)
73 if (dlp->objs[i] == obj)
74 return true;
75 /*
76 * Our donelist allocation may not always be sufficient as we're not
77 * thread safe. We'll handle it properly anyway.
78 */
79 if (dlp->num_used < dlp->num_alloc)
80 dlp->objs[dlp->num_used++] = obj;
81 return false;
82 }
83
84 static bool
85 _rtld_is_exported(const Elf_Sym *def)
86 {
87 static const fptr_t _rtld_exports[] = {
88 (fptr_t)dlopen,
89 (fptr_t)dlclose,
90 (fptr_t)dlsym,
91 (fptr_t)dlerror,
92 (fptr_t)dladdr,
93 (fptr_t)dlinfo,
94 NULL
95 };
96 int i;
97 fptr_t value;
98
99 value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
100 for (i = 0; _rtld_exports[i] != NULL; i++) {
101 if (value == _rtld_exports[i])
102 return true;
103 }
104 return false;
105 }
106
107 /*
108 * Hash function for symbol table lookup. Don't even think about changing
109 * this. It is specified by the System V ABI.
110 */
111 unsigned long
112 _rtld_elf_hash(const char *name)
113 {
114 const unsigned char *p = (const unsigned char *) name;
115 unsigned long h = 0;
116 unsigned long g;
117 unsigned long c;
118
119 for (; __predict_true((c = *p) != '\0'); p++) {
120 h <<= 4;
121 h += c;
122 if ((g = h & 0xf0000000) != 0) {
123 h ^= g;
124 h ^= g >> 24;
125 }
126 }
127 return (h);
128 }
129
130 const Elf_Sym *
131 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
132 const Obj_Entry **defobj_out, bool in_plt, DoneList *dlp)
133 {
134 const Elf_Sym *symp;
135 const Elf_Sym *def;
136 const Obj_Entry *defobj;
137 const Objlist_Entry *elm;
138
139 def = NULL;
140 defobj = NULL;
141 SIMPLEQ_FOREACH(elm, objlist, link) {
142 if (_rtld_donelist_check(dlp, elm->obj))
143 continue;
144 rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
145 name));
146 if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
147 != NULL) {
148 if ((def == NULL) ||
149 (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
150 def = symp;
151 defobj = elm->obj;
152 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
153 break;
154 }
155 }
156 }
157 if (def != NULL)
158 *defobj_out = defobj;
159 return def;
160 }
161
162 /*
163 * Search the symbol table of a shared object and all objects needed by it for
164 * a symbol of the given name. Search order is breadth-first. Returns a pointer
165 * to the symbol, or NULL if no definition was found.
166 */
167 const Elf_Sym *
168 _rtld_symlook_needed(const char *name, unsigned long hash,
169 const Needed_Entry *needed, const Obj_Entry **defobj_out, bool inplt,
170 DoneList *breadth, DoneList *depth)
171 {
172 const Elf_Sym *def, *def_w;
173 const Needed_Entry *n;
174 const Obj_Entry *obj, *defobj, *defobj1;
175
176 def = def_w = NULL;
177 defobj = NULL;
178 for (n = needed; n != NULL; n = n->next) {
179 if ((obj = n->obj) == NULL)
180 continue;
181 if (_rtld_donelist_check(breadth, obj))
182 continue;
183 if ((def = _rtld_symlook_obj(name, hash, obj, inplt)) == NULL)
184 continue;
185 defobj = obj;
186 if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
187 *defobj_out = defobj;
188
189 return (def);
190 }
191 }
192 /*
193 * Either the symbol definition has not been found in directly needed
194 * objects, or the found symbol is weak.
195 */
196 for (n = needed; n != NULL; n = n->next) {
197 if ((obj = n->obj) == NULL)
198 continue;
199 if (_rtld_donelist_check(depth, obj))
200 continue;
201 def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
202 inplt, breadth, depth);
203 if (def_w == NULL)
204 continue;
205 if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
206 def = def_w;
207 defobj = defobj1;
208 if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
209 break;
210 }
211 }
212 if (def != NULL)
213 *defobj_out = defobj;
214
215 return def;
216 }
217
218 /*
219 * Search the symbol table of a single shared object for a symbol of
220 * the given name. Returns a pointer to the symbol, or NULL if no
221 * definition was found.
222 *
223 * The symbol's hash value is passed in for efficiency reasons; that
224 * eliminates many recomputations of the hash value.
225 */
226 const Elf_Sym *
227 _rtld_symlook_obj(const char *name, unsigned long hash,
228 const Obj_Entry *obj, bool in_plt)
229 {
230 unsigned long symnum;
231
232 for (symnum = obj->buckets[hash % obj->nbuckets];
233 symnum != ELF_SYM_UNDEFINED;
234 symnum = obj->chains[symnum]) {
235 const Elf_Sym *symp;
236 const char *strp;
237
238 assert(symnum < obj->nchains);
239 symp = obj->symtab + symnum;
240 strp = obj->strtab + symp->st_name;
241 rdbg(("check \"%s\" vs \"%s\" in %p", name, strp, obj));
242 if (name[1] == strp[1] && !strcmp(name, strp)) {
243 if (symp->st_shndx != SHN_UNDEF)
244 return symp;
245 #ifndef __mips__
246 /*
247 * XXX DANGER WILL ROBINSON!
248 * If we have a function pointer in the executable's
249 * data section, it points to the executable's PLT
250 * slot, and there is NO relocation emitted. To make
251 * the function pointer comparable to function pointers
252 * in shared libraries, we must resolve data references
253 * in the libraries to point to PLT slots in the
254 * executable, if they exist.
255 */
256 else if (!in_plt && symp->st_value != 0 &&
257 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
258 return symp;
259 #endif
260 else
261 return NULL;
262 }
263 }
264
265 return NULL;
266 }
267
268 #ifdef COMBRELOC
269 static const Obj_Entry *_rtld_last_refobj;
270
271 /*
272 * Called when an object is freed. Reset the cached symbol look up if
273 * our last referencing or definition object just got unloaded.
274 */
275 void
276 _rtld_combreloc_reset(const Obj_Entry *obj)
277 {
278 if (_rtld_last_refobj == obj)
279 _rtld_last_refobj = NULL;
280 }
281 #endif
282
283 /*
284 * Given a symbol number in a referencing object, find the corresponding
285 * definition of the symbol. Returns a pointer to the symbol, or NULL if
286 * no definition was found. Returns a pointer to the Obj_Entry of the
287 * defining object via the reference parameter DEFOBJ_OUT.
288 */
289 const Elf_Sym *
290 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
291 const Obj_Entry **defobj_out, bool in_plt)
292 {
293 const Elf_Sym *ref;
294 const Elf_Sym *def;
295 const Obj_Entry *defobj;
296 const char *name;
297 unsigned long hash;
298
299 #ifdef COMBRELOC
300 /*
301 * COMBRELOC combines multiple reloc sections and sorts them to make
302 * dynamic symbol lookup caching possible.
303 *
304 * So if the lookup we are doing is the same as the previous lookup
305 * return the cached results.
306 */
307 static unsigned long last_symnum;
308 static const Obj_Entry *last_defobj;
309 static const Elf_Sym *last_def;
310
311 if (symnum == last_symnum && refobj == _rtld_last_refobj
312 && in_plt == false) {
313 *defobj_out = last_defobj;
314 return last_def;
315 }
316 #endif
317
318 ref = refobj->symtab + symnum;
319 name = refobj->strtab + ref->st_name;
320
321 /*
322 * We don't have to do a full scale lookup if the symbol is local.
323 * We know it will bind to the instance in this load module; to
324 * which we already have a pointer (ie ref).
325 */
326 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
327 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
328 _rtld_error("%s: Bogus symbol table entry %lu",
329 refobj->path, symnum);
330 }
331
332 hash = _rtld_elf_hash(name);
333 defobj = NULL;
334 def = _rtld_symlook_default(name, hash, refobj, &defobj, in_plt);
335 } else {
336 rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
337 def = ref;
338 defobj = refobj;
339 }
340
341 /*
342 * If we found no definition and the reference is weak, treat the
343 * symbol as having the value zero.
344 */
345 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
346 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
347 def = &_rtld_sym_zero;
348 defobj = &_rtld_objself;
349 }
350
351 if (def != NULL) {
352 *defobj_out = defobj;
353 #ifdef COMBRELOC
354 if (in_plt == false) {
355 /*
356 * Cache the lookup arguments and results if this was
357 * non-PLT lookup.
358 */
359 last_symnum = symnum;
360 _rtld_last_refobj = refobj;
361 last_def = def;
362 last_defobj = defobj;
363 }
364 #endif
365 } else {
366 rdbg(("lookup failed"));
367 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
368 refobj->path, in_plt ? "PLT " : "", name, symnum);
369 }
370 return def;
371 }
372
373 const Elf_Sym *
374 _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
375 const Obj_Entry **defobj, bool imm)
376 {
377 const Elf_Sym *def = _rtld_find_symdef(symnum, obj, defobj, true);
378 if (__predict_false(def == NULL))
379 return NULL;
380
381 if (__predict_false(def == &_rtld_sym_zero)) {
382 /* tp is set during lazy binding. */
383 if (imm) {
384 const Elf_Sym *ref = obj->symtab + symnum;
385 const char *name = obj->strtab + ref->st_name;
386
387 _rtld_error(
388 "%s: Trying to call undefined weak symbol `%s'",
389 obj->path, name);
390 return NULL;
391 }
392 }
393 return def;
394 }
395
396 /*
397 * Given a symbol name in a referencing object, find the corresponding
398 * definition of the symbol. Returns a pointer to the symbol, or NULL if
399 * no definition was found. Returns a pointer to the Obj_Entry of the
400 * defining object via the reference parameter DEFOBJ_OUT.
401 */
402 const Elf_Sym *
403 _rtld_symlook_default(const char *name, unsigned long hash,
404 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
405 {
406 const Elf_Sym *def;
407 const Elf_Sym *symp;
408 const Obj_Entry *obj;
409 const Obj_Entry *defobj;
410 const Objlist_Entry *elm;
411 def = NULL;
412 defobj = NULL;
413 DoneList donelist;
414
415 _rtld_donelist_init(&donelist);
416
417 /* Look first in the referencing object if linked symbolically. */
418 if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
419 rdbg(("search referencing object for %s", name));
420 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
421 if (symp != NULL) {
422 def = symp;
423 defobj = refobj;
424 }
425 }
426
427 /* Search all objects loaded at program start up. */
428 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
429 rdbg(("search _rtld_list_main for %s", name));
430 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
431 in_plt, &donelist);
432 if (symp != NULL &&
433 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
434 def = symp;
435 defobj = obj;
436 }
437 }
438
439 /* Search all RTLD_GLOBAL objects. */
440 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
441 rdbg(("search _rtld_list_global for %s", name));
442 symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
443 &obj, in_plt, &donelist);
444 if (symp != NULL &&
445 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
446 def = symp;
447 defobj = obj;
448 }
449 }
450
451 /* Search all dlopened DAGs containing the referencing object. */
452 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
453 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
454 break;
455 rdbg(("search DAG with root %p (%s) for %s", elm->obj,
456 elm->obj->path, name));
457 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
458 &obj, in_plt, &donelist);
459 if (symp != NULL &&
460 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
461 def = symp;
462 defobj = obj;
463 }
464 }
465
466 /*
467 * Search the dynamic linker itself, and possibly resolve the
468 * symbol from there. This is how the application links to
469 * dynamic linker services such as dlopen. Only the values listed
470 * in the "_rtld_exports" array can be resolved from the dynamic
471 * linker.
472 */
473 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
474 rdbg(("Search the dynamic linker itself."));
475 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
476 if (symp != NULL && _rtld_is_exported(symp)) {
477 def = symp;
478 defobj = &_rtld_objself;
479 }
480 }
481
482 _rtld_donelist_clear(&donelist);
483
484 if (def != NULL)
485 *defobj_out = defobj;
486 return def;
487 }
488