ldivmod, uldivmod: fix qdivrem calls
[minix.git] / libexec / ld.elf_so / symbol.c
blobf4eea1197c48bd60b8d660f1fc9a5ee622414f54
1 /* $NetBSD: symbol.c,v 1.61 2012/08/15 03:46:07 matt 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.61 2012/08/15 03:46:07 matt 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"
63 * If the given object is already in the donelist, return true. Otherwise
64 * add the object to the list and return false.
66 static bool
67 _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
69 unsigned int i;
71 for (i = 0; i < dlp->num_used; i++)
72 if (dlp->objs[i] == obj)
73 return true;
75 * Our donelist allocation may not always be sufficient as we're not
76 * thread safe. We'll handle it properly anyway.
78 if (dlp->num_used < dlp->num_alloc)
79 dlp->objs[dlp->num_used++] = obj;
80 return false;
83 static bool
84 _rtld_is_exported(const Elf_Sym *def)
86 static const fptr_t _rtld_exports[] = {
87 (fptr_t)dlopen,
88 (fptr_t)dlclose,
89 (fptr_t)dlsym,
90 (fptr_t)dlvsym,
91 (fptr_t)dlerror,
92 (fptr_t)dladdr,
93 (fptr_t)dlinfo,
94 (fptr_t)dl_iterate_phdr,
95 (fptr_t)_dlauxinfo,
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 #ifdef __i386__
101 (fptr_t)___tls_get_addr,
102 #endif
103 #endif
104 NULL
106 int i;
107 fptr_t value;
109 value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
110 for (i = 0; _rtld_exports[i] != NULL; i++) {
111 if (value == _rtld_exports[i])
112 return true;
114 return false;
118 * Hash function for symbol table lookup. Don't even think about changing
119 * this. It is specified by the System V ABI.
121 unsigned long
122 _rtld_elf_hash(const char *name)
124 const unsigned char *p = (const unsigned char *) name;
125 unsigned long h = 0;
126 unsigned long g;
127 unsigned long c;
129 for (; __predict_true((c = *p) != '\0'); p++) {
130 h <<= 4;
131 h += c;
132 if ((g = h & 0xf0000000) != 0) {
133 h ^= g;
134 h ^= g >> 24;
137 return (h);
140 const Elf_Sym *
141 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
142 const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry,
143 DoneList *dlp)
145 const Elf_Sym *symp;
146 const Elf_Sym *def;
147 const Obj_Entry *defobj;
148 const Objlist_Entry *elm;
150 def = NULL;
151 defobj = NULL;
152 SIMPLEQ_FOREACH(elm, objlist, link) {
153 if (_rtld_donelist_check(dlp, elm->obj))
154 continue;
155 rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
156 name));
157 symp = _rtld_symlook_obj(name, hash, elm->obj, flags, ventry);
158 if (symp != NULL) {
159 if ((def == NULL) ||
160 (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
161 def = symp;
162 defobj = elm->obj;
163 if (ELF_ST_BIND(def->st_info) != STB_WEAK)
164 break;
168 if (def != NULL)
169 *defobj_out = defobj;
170 return def;
174 * Search the symbol table of a shared object and all objects needed by it for
175 * a symbol of the given name. Search order is breadth-first. Returns a pointer
176 * to the symbol, or NULL if no definition was found.
178 const Elf_Sym *
179 _rtld_symlook_needed(const char *name, unsigned long hash,
180 const Needed_Entry *needed, const Obj_Entry **defobj_out, u_int flags,
181 const Ver_Entry *ventry, DoneList *breadth, DoneList *depth)
183 const Elf_Sym *def, *def_w;
184 const Needed_Entry *n;
185 const Obj_Entry *obj, *defobj, *defobj1;
187 def = def_w = NULL;
188 defobj = NULL;
189 for (n = needed; n != NULL; n = n->next) {
190 if ((obj = n->obj) == NULL)
191 continue;
192 if (_rtld_donelist_check(breadth, obj))
193 continue;
194 def = _rtld_symlook_obj(name, hash, obj, flags, ventry);
195 if (def == NULL)
196 continue;
197 defobj = obj;
198 if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
199 *defobj_out = defobj;
201 return (def);
205 * Either the symbol definition has not been found in directly needed
206 * objects, or the found symbol is weak.
208 for (n = needed; n != NULL; n = n->next) {
209 if ((obj = n->obj) == NULL)
210 continue;
211 if (_rtld_donelist_check(depth, obj))
212 continue;
213 def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
214 flags, ventry, breadth, depth);
215 if (def_w == NULL)
216 continue;
217 if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
218 def = def_w;
219 defobj = defobj1;
220 if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
221 break;
224 if (def != NULL)
225 *defobj_out = defobj;
227 return def;
231 * Search the symbol table of a single shared object for a symbol of
232 * the given name. Returns a pointer to the symbol, or NULL if no
233 * definition was found.
235 * The symbol's hash value is passed in for efficiency reasons; that
236 * eliminates many recomputations of the hash value.
238 const Elf_Sym *
239 _rtld_symlook_obj(const char *name, unsigned long hash,
240 const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
242 unsigned long symnum;
243 const Elf_Sym *vsymp = NULL;
244 Elf_Half verndx;
245 int vcount = 0;
247 for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
248 obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
249 symnum != ELF_SYM_UNDEFINED;
250 symnum = obj->chains[symnum]) {
251 const Elf_Sym *symp;
252 const char *strp;
254 assert(symnum < obj->nchains);
255 symp = obj->symtab + symnum;
256 strp = obj->strtab + symp->st_name;
257 rdbg(("check \"%s\" vs \"%s\" in %s", name, strp, obj->path));
258 if (name[1] != strp[1] || strcmp(name, strp))
259 continue;
260 #ifdef __mips__
261 if (symp->st_shndx == SHN_UNDEF)
262 continue;
263 #else
265 * XXX DANGER WILL ROBINSON!
266 * If we have a function pointer in the executable's
267 * data section, it points to the executable's PLT
268 * slot, and there is NO relocation emitted. To make
269 * the function pointer comparable to function pointers
270 * in shared libraries, we must resolve data references
271 * in the libraries to point to PLT slots in the
272 * executable, if they exist.
274 if (symp->st_shndx == SHN_UNDEF &&
275 ((flags & SYMLOOK_IN_PLT) ||
276 symp->st_value == 0 ||
277 ELF_ST_TYPE(symp->st_info) != STT_FUNC))
278 continue;
279 #endif
281 if (ventry == NULL) {
282 if (obj->versyms != NULL) {
283 verndx = VER_NDX(obj->versyms[symnum].vs_vers);
284 if (verndx > obj->vertabnum) {
285 _rtld_error("%s: symbol %s references "
286 "wrong version %d", obj->path,
287 &obj->strtab[symnum], verndx);
288 continue;
292 * If we are not called from dlsym (i.e. this
293 * is a normal relocation from unversioned
294 * binary), accept the symbol immediately
295 * if it happens to have first version after
296 * this shared object became versioned.
297 * Otherwise, if symbol is versioned and not
298 * hidden, remember it. If it is the only
299 * symbol with this name exported by the shared
300 * object, it will be returned as a match at the
301 * end of the function. If symbol is global
302 * (verndx < 2) accept it unconditionally.
304 if (!(flags & SYMLOOK_DLSYM) &&
305 verndx == VER_NDX_GIVEN) {
306 return symp;
307 } else if (verndx >= VER_NDX_GIVEN) {
308 if (!(obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN)) {
309 if (vsymp == NULL)
310 vsymp = symp;
311 vcount++;
313 continue;
316 return symp;
317 } else {
318 if (obj->versyms == NULL) {
319 if (_rtld_object_match_name(obj, ventry->name)){
320 _rtld_error("%s: object %s should "
321 "provide version %s for symbol %s",
322 _rtld_objself.path, obj->path,
323 ventry->name, &obj->strtab[symnum]);
324 continue;
326 } else {
327 verndx = VER_NDX(obj->versyms[symnum].vs_vers);
328 if (verndx > obj->vertabnum) {
329 _rtld_error("%s: symbol %s references "
330 "wrong version %d", obj->path,
331 &obj->strtab[symnum], verndx);
332 continue;
334 if (obj->vertab[verndx].hash != ventry->hash ||
335 strcmp(obj->vertab[verndx].name, ventry->name)) {
337 * Version does not match. Look if this
338 * is a global symbol and if it is not
339 * hidden. If global symbol (verndx < 2)
340 * is available, use it. Do not return
341 * symbol if we are called by dlvsym,
342 * because dlvsym looks for a specific
343 * version and default one is not what
344 * dlvsym wants.
346 if ((flags & SYMLOOK_DLSYM) ||
347 (obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN) ||
348 (verndx >= VER_NDX_GIVEN))
349 continue;
352 return symp;
355 if (vcount == 1)
356 return vsymp;
357 return NULL;
360 #ifdef COMBRELOC
361 static const Obj_Entry *_rtld_last_refobj;
364 * Called when an object is freed. Reset the cached symbol look up if
365 * our last referencing or definition object just got unloaded.
367 void
368 _rtld_combreloc_reset(const Obj_Entry *obj)
370 if (_rtld_last_refobj == obj)
371 _rtld_last_refobj = NULL;
373 #endif
376 * Given a symbol number in a referencing object, find the corresponding
377 * definition of the symbol. Returns a pointer to the symbol, or NULL if
378 * no definition was found. Returns a pointer to the Obj_Entry of the
379 * defining object via the reference parameter DEFOBJ_OUT.
381 const Elf_Sym *
382 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
383 const Obj_Entry **defobj_out, u_int flags)
385 const Elf_Sym *ref;
386 const Elf_Sym *def;
387 const Obj_Entry *defobj;
388 const char *name;
389 unsigned long hash;
391 #ifdef COMBRELOC
393 * COMBRELOC combines multiple reloc sections and sorts them to make
394 * dynamic symbol lookup caching possible.
396 * So if the lookup we are doing is the same as the previous lookup
397 * return the cached results.
399 static unsigned long last_symnum;
400 static const Obj_Entry *last_defobj;
401 static const Elf_Sym *last_def;
403 if (symnum == last_symnum && refobj == _rtld_last_refobj
404 && !(flags & SYMLOOK_IN_PLT)) {
405 *defobj_out = last_defobj;
406 return last_def;
408 #endif
410 ref = refobj->symtab + symnum;
411 name = refobj->strtab + ref->st_name;
414 * We don't have to do a full scale lookup if the symbol is local.
415 * We know it will bind to the instance in this load module; to
416 * which we already have a pointer (ie ref).
418 if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
419 if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
420 _rtld_error("%s: Bogus symbol table entry %lu",
421 refobj->path, symnum);
424 hash = _rtld_elf_hash(name);
425 defobj = NULL;
426 def = _rtld_symlook_default(name, hash, refobj, &defobj, flags,
427 _rtld_fetch_ventry(refobj, symnum));
428 } else {
429 rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
430 def = ref;
431 defobj = refobj;
435 * If we found no definition and the reference is weak, treat the
436 * symbol as having the value zero.
438 if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
439 rdbg((" returning _rtld_sym_zero@_rtld_objself"));
440 def = &_rtld_sym_zero;
441 defobj = &_rtld_objself;
444 if (def != NULL) {
445 *defobj_out = defobj;
446 #ifdef COMBRELOC
447 if (!(flags & SYMLOOK_IN_PLT)) {
449 * Cache the lookup arguments and results if this was
450 * non-PLT lookup.
452 last_symnum = symnum;
453 _rtld_last_refobj = refobj;
454 last_def = def;
455 last_defobj = defobj;
457 #endif
458 } else {
459 rdbg(("lookup failed"));
460 _rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
461 refobj->path, (flags & SYMLOOK_IN_PLT) ? "PLT " : "",
462 name, symnum);
464 return def;
467 const Elf_Sym *
468 _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
469 const Obj_Entry **defobj, bool imm)
471 const Elf_Sym *def = _rtld_find_symdef(symnum, obj, defobj,
472 SYMLOOK_IN_PLT);
473 if (__predict_false(def == NULL))
474 return NULL;
476 if (__predict_false(def == &_rtld_sym_zero)) {
477 /* tp is set during lazy binding. */
478 if (imm) {
479 const Elf_Sym *ref = obj->symtab + symnum;
480 const char *name = obj->strtab + ref->st_name;
482 _rtld_error(
483 "%s: Trying to call undefined weak symbol `%s'",
484 obj->path, name);
485 return NULL;
488 return def;
492 * Given a symbol name in a referencing object, find the corresponding
493 * definition of the symbol. Returns a pointer to the symbol, or NULL if
494 * no definition was found. Returns a pointer to the Obj_Entry of the
495 * defining object via the reference parameter DEFOBJ_OUT.
497 const Elf_Sym *
498 _rtld_symlook_default(const char *name, unsigned long hash,
499 const Obj_Entry *refobj, const Obj_Entry **defobj_out, u_int flags,
500 const Ver_Entry *ventry)
502 const Elf_Sym *def;
503 const Elf_Sym *symp;
504 const Obj_Entry *obj;
505 const Obj_Entry *defobj;
506 const Objlist_Entry *elm;
507 def = NULL;
508 defobj = NULL;
509 DoneList donelist;
511 _rtld_donelist_init(&donelist);
513 /* Look first in the referencing object if linked symbolically. */
514 if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
515 rdbg(("search referencing object for %s", name));
516 symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
517 if (symp != NULL) {
518 def = symp;
519 defobj = refobj;
523 /* Search all objects loaded at program start up. */
524 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
525 rdbg(("search _rtld_list_main for %s", name));
526 symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
527 flags, ventry, &donelist);
528 if (symp != NULL &&
529 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
530 def = symp;
531 defobj = obj;
535 /* Search all RTLD_GLOBAL objects. */
536 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
537 rdbg(("search _rtld_list_global for %s", name));
538 symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
539 &obj, flags, ventry, &donelist);
540 if (symp != NULL &&
541 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
542 def = symp;
543 defobj = obj;
547 /* Search all dlopened DAGs containing the referencing object. */
548 SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
549 if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
550 break;
551 rdbg(("search DAG with root %p (%s) for %s", elm->obj,
552 elm->obj->path, name));
553 symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
554 &obj, flags, ventry, &donelist);
555 if (symp != NULL &&
556 (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
557 def = symp;
558 defobj = obj;
563 * Search the dynamic linker itself, and possibly resolve the
564 * symbol from there. This is how the application links to
565 * dynamic linker services such as dlopen. Only the values listed
566 * in the "_rtld_exports" array can be resolved from the dynamic
567 * linker.
569 if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
570 rdbg(("Search the dynamic linker itself."));
571 symp = _rtld_symlook_obj(name, hash, &_rtld_objself, flags,
572 ventry);
573 if (symp != NULL && _rtld_is_exported(symp)) {
574 def = symp;
575 defobj = &_rtld_objself;
579 if (def != NULL)
580 *defobj_out = defobj;
581 return def;