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