mkfs: drop support for zone != block
[minix.git] / libexec / ld.elf_so / symbol.c
blobf4979bbd39c502192b1c0c7559a24050cc0bd4b1
1 /* $NetBSD: symbol.c,v 1.54 2010/10/16 10:27:07 skrll Exp $ */
3 /*
4 * Copyright 1996 John D. Polstra.
5 * Copyright 1996 Matt Thomas <matt@3am-software.com>
6 * Copyright 2002 Charles M. Hannum <root@ihack.net>
7 * All rights reserved.
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.
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.
36 * Dynamic linker for ELF.
38 * John Polstra <jdp@polstra.com>.
41 #include <sys/cdefs.h>
42 #ifndef lint
43 __RCSID("$NetBSD: symbol.c,v 1.54 2010/10/16 10:27:07 skrll Exp $");
44 #endif /* not lint */
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>
59 #include "debug.h"
60 #include "rtld.h"
62 typedef void (*fptr_t)(void);
65 * If the given object is already in the donelist, return true. Otherwise
66 * add the object to the list and return false.
68 static bool
69 _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
71 unsigned int i;
73 for (i = 0; i < dlp->num_used; i++)
74 if (dlp->objs[i] == obj)
75 return true;
77 * Our donelist allocation may not always be sufficient as we're not
78 * thread safe. We'll handle it properly anyway.
80 if (dlp->num_used < dlp->num_alloc)
81 dlp->objs[dlp->num_used++] = obj;
82 return false;
85 static bool
86 _rtld_is_exported(const Elf_Sym *def)
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 NULL
98 int i;
99 fptr_t value;
101 value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
102 for (i = 0; _rtld_exports[i] != NULL; i++) {
103 if (value == _rtld_exports[i])
104 return true;
106 return false;
110 * Hash function for symbol table lookup. Don't even think about changing
111 * this. It is specified by the System V ABI.
113 unsigned long
114 _rtld_elf_hash(const char *name)
116 const unsigned char *p = (const unsigned char *) name;
117 unsigned long h = 0;
118 unsigned long g;
119 unsigned long c;
121 for (; __predict_true((c = *p) != '\0'); p++) {
122 h <<= 4;
123 h += c;
124 if ((g = h & 0xf0000000) != 0) {
125 h ^= g;
126 h ^= g >> 24;
129 return (h);
132 const Elf_Sym *
133 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
134 const Obj_Entry **defobj_out, bool in_plt, DoneList *dlp)
136 const Elf_Sym *symp;
137 const Elf_Sym *def;
138 const Obj_Entry *defobj;
139 const Objlist_Entry *elm;
141 def = NULL;
142 defobj = NULL;
143 SIMPLEQ_FOREACH(elm, objlist, link) {
144 if (_rtld_donelist_check(dlp, elm->obj))
145 continue;
146 rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
147 name));
148 if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
149 != NULL) {
150 if ((def == NULL) ||
151 (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
152 def = symp;
153 defobj = elm->obj;
154 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
155 break;
159 if (def != NULL)
160 *defobj_out = defobj;
161 return def;
165 * Search the symbol table of a shared object and all objects needed by it for
166 * a symbol of the given name. Search order is breadth-first. Returns a pointer
167 * to the symbol, or NULL if no definition was found.
169 const Elf_Sym *
170 _rtld_symlook_needed(const char *name, unsigned long hash,
171 const Needed_Entry *needed, const Obj_Entry **defobj_out, bool inplt,
172 DoneList *breadth, DoneList *depth)
174 const Elf_Sym *def, *def_w;
175 const Needed_Entry *n;
176 const Obj_Entry *obj, *defobj, *defobj1;
178 def = def_w = NULL;
179 defobj = NULL;
180 for (n = needed; n != NULL; n = n->next) {
181 if ((obj = n->obj) == NULL)
182 continue;
183 if (_rtld_donelist_check(breadth, obj))
184 continue;
185 if ((def = _rtld_symlook_obj(name, hash, obj, inplt)) == NULL)
186 continue;
187 defobj = obj;
188 if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
189 *defobj_out = defobj;
191 return (def);
195 * Either the symbol definition has not been found in directly needed
196 * objects, or the found symbol is weak.
198 for (n = needed; n != NULL; n = n->next) {
199 if ((obj = n->obj) == NULL)
200 continue;
201 if (_rtld_donelist_check(depth, obj))
202 continue;
203 def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
204 inplt, breadth, depth);
205 if (def_w == NULL)
206 continue;
207 if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
208 def = def_w;
209 defobj = defobj1;
210 if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
211 break;
214 if (def != NULL)
215 *defobj_out = defobj;
217 return def;
221 * Search the symbol table of a single shared object for a symbol of
222 * the given name. Returns a pointer to the symbol, or NULL if no
223 * definition was found.
225 * The symbol's hash value is passed in for efficiency reasons; that
226 * eliminates many recomputations of the hash value.
228 const Elf_Sym *
229 _rtld_symlook_obj(const char *name, unsigned long hash,
230 const Obj_Entry *obj, bool in_plt)
232 unsigned long symnum;
234 for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
235 obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
236 symnum != ELF_SYM_UNDEFINED;
237 symnum = obj->chains[symnum]) {
238 const Elf_Sym *symp;
239 const char *strp;
241 assert(symnum < obj->nchains);
242 symp = obj->symtab + symnum;
243 strp = obj->strtab + symp->st_name;
244 rdbg(("check \"%s\" vs \"%s\" in %p", name, strp, obj));
245 if (name[1] == strp[1] && !strcmp(name, strp)) {
246 if (symp->st_shndx != SHN_UNDEF)
247 return symp;
248 #ifndef __mips__
250 * XXX DANGER WILL ROBINSON!
251 * If we have a function pointer in the executable's
252 * data section, it points to the executable's PLT
253 * slot, and there is NO relocation emitted. To make
254 * the function pointer comparable to function pointers
255 * in shared libraries, we must resolve data references
256 * in the libraries to point to PLT slots in the
257 * executable, if they exist.
259 else if (!in_plt && symp->st_value != 0 &&
260 ELF_ST_TYPE(symp->st_info) == STT_FUNC)
261 return symp;
262 #endif
263 else
264 return NULL;
268 return NULL;
271 #ifdef COMBRELOC
272 static const Obj_Entry *_rtld_last_refobj;
275 * Called when an object is freed. Reset the cached symbol look up if
276 * our last referencing or definition object just got unloaded.
278 void
279 _rtld_combreloc_reset(const Obj_Entry *obj)
281 if (_rtld_last_refobj == obj)
282 _rtld_last_refobj = NULL;
284 #endif
287 * Given a symbol number in a referencing object, find the corresponding
288 * definition of the symbol. Returns a pointer to the symbol, or NULL if
289 * no definition was found. Returns a pointer to the Obj_Entry of the
290 * defining object via the reference parameter DEFOBJ_OUT.
292 const Elf_Sym *
293 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
294 const Obj_Entry **defobj_out, bool in_plt)
296 const Elf_Sym *ref;
297 const Elf_Sym *def;
298 const Obj_Entry *defobj;
299 const char *name;
300 unsigned long hash;
302 #ifdef COMBRELOC
304 * COMBRELOC combines multiple reloc sections and sorts them to make
305 * dynamic symbol lookup caching possible.
307 * So if the lookup we are doing is the same as the previous lookup
308 * return the cached results.
310 static unsigned long last_symnum;
311 static const Obj_Entry *last_defobj;
312 static const Elf_Sym *last_def;
314 if (symnum == last_symnum && refobj == _rtld_last_refobj
315 && in_plt == false) {
316 *defobj_out = last_defobj;
317 return last_def;
319 #endif
321 ref = refobj->symtab + symnum;
322 name = refobj->strtab + ref->st_name;
325 * We don't have to do a full scale lookup if the symbol is local.
326 * We know it will bind to the instance in this load module; to
327 * which we already have a pointer (ie ref).
329 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
330 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
331 _rtld_error("%s: Bogus symbol table entry %lu",
332 refobj->path, symnum);
335 hash = _rtld_elf_hash(name);
336 defobj = NULL;
337 def = _rtld_symlook_default(name, hash, refobj, &defobj, in_plt);
338 } else {
339 rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
340 def = ref;
341 defobj = refobj;
345 * If we found no definition and the reference is weak, treat the
346 * symbol as having the value zero.
348 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
349 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
350 def = &_rtld_sym_zero;
351 defobj = &_rtld_objself;
354 if (def != NULL) {
355 *defobj_out = defobj;
356 #ifdef COMBRELOC
357 if (in_plt == false) {
359 * Cache the lookup arguments and results if this was
360 * non-PLT lookup.
362 last_symnum = symnum;
363 _rtld_last_refobj = refobj;
364 last_def = def;
365 last_defobj = defobj;
367 #endif
368 } else {
369 rdbg(("lookup failed"));
370 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
371 refobj->path, in_plt ? "PLT " : "", name, symnum);
373 return def;
376 const Elf_Sym *
377 _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
378 const Obj_Entry **defobj, bool imm)
380 const Elf_Sym *def = _rtld_find_symdef(symnum, obj, defobj, true);
381 if (__predict_false(def == NULL))
382 return NULL;
384 if (__predict_false(def == &_rtld_sym_zero)) {
385 /* tp is set during lazy binding. */
386 if (imm) {
387 const Elf_Sym *ref = obj->symtab + symnum;
388 const char *name = obj->strtab + ref->st_name;
390 _rtld_error(
391 "%s: Trying to call undefined weak symbol `%s'",
392 obj->path, name);
393 return NULL;
396 return def;
400 * Given a symbol name in a referencing object, find the corresponding
401 * definition of the symbol. Returns a pointer to the symbol, or NULL if
402 * no definition was found. Returns a pointer to the Obj_Entry of the
403 * defining object via the reference parameter DEFOBJ_OUT.
405 const Elf_Sym *
406 _rtld_symlook_default(const char *name, unsigned long hash,
407 const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
409 const Elf_Sym *def;
410 const Elf_Sym *symp;
411 const Obj_Entry *obj;
412 const Obj_Entry *defobj;
413 const Objlist_Entry *elm;
414 def = NULL;
415 defobj = NULL;
416 DoneList donelist;
418 _rtld_donelist_init(&donelist);
420 /* Look first in the referencing object if linked symbolically. */
421 if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
422 rdbg(("search referencing object for %s", name));
423 symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
424 if (symp != NULL) {
425 def = symp;
426 defobj = refobj;
430 /* Search all objects loaded at program start up. */
431 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
432 rdbg(("search _rtld_list_main for %s", name));
433 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
434 in_plt, &donelist);
435 if (symp != NULL &&
436 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
437 def = symp;
438 defobj = obj;
442 /* Search all RTLD_GLOBAL objects. */
443 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
444 rdbg(("search _rtld_list_global for %s", name));
445 symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
446 &obj, in_plt, &donelist);
447 if (symp != NULL &&
448 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
449 def = symp;
450 defobj = obj;
454 /* Search all dlopened DAGs containing the referencing object. */
455 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
456 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
457 break;
458 rdbg(("search DAG with root %p (%s) for %s", elm->obj,
459 elm->obj->path, name));
460 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
461 &obj, in_plt, &donelist);
462 if (symp != NULL &&
463 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
464 def = symp;
465 defobj = obj;
470 * Search the dynamic linker itself, and possibly resolve the
471 * symbol from there. This is how the application links to
472 * dynamic linker services such as dlopen. Only the values listed
473 * in the "_rtld_exports" array can be resolved from the dynamic
474 * linker.
476 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
477 rdbg(("Search the dynamic linker itself."));
478 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
479 if (symp != NULL && _rtld_is_exported(symp)) {
480 def = symp;
481 defobj = &_rtld_objself;
485 if (def != NULL)
486 *defobj_out = defobj;
487 return def;