Home | History | Annotate | Line # | Download | only in ld.elf_so
symbol.c revision 1.60
      1 /*	$NetBSD: symbol.c,v 1.60 2012/02/16 23:00:39 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.60 2012/02/16 23:00:39 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)dlvsym,
     93 		(fptr_t)dlerror,
     94 		(fptr_t)dladdr,
     95 		(fptr_t)dlinfo,
     96 		(fptr_t)dl_iterate_phdr,
     97 		(fptr_t)_dlauxinfo,
     98 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
     99 		(fptr_t)_rtld_tls_allocate,
    100 		(fptr_t)_rtld_tls_free,
    101 		(fptr_t)__tls_get_addr,
    102 #ifdef __i386__
    103 		(fptr_t)___tls_get_addr,
    104 #endif
    105 #endif
    106 		NULL
    107 	};
    108 	int i;
    109 	fptr_t value;
    110 
    111 	value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
    112 	for (i = 0; _rtld_exports[i] != NULL; i++) {
    113 		if (value == _rtld_exports[i])
    114 			return true;
    115 	}
    116 	return false;
    117 }
    118 
    119 /*
    120  * Hash function for symbol table lookup.  Don't even think about changing
    121  * this.  It is specified by the System V ABI.
    122  */
    123 unsigned long
    124 _rtld_elf_hash(const char *name)
    125 {
    126 	const unsigned char *p = (const unsigned char *) name;
    127 	unsigned long   h = 0;
    128 	unsigned long   g;
    129 	unsigned long   c;
    130 
    131 	for (; __predict_true((c = *p) != '\0'); p++) {
    132 		h <<= 4;
    133 		h += c;
    134 		if ((g = h & 0xf0000000) != 0) {
    135 			h ^= g;
    136 			h ^= g >> 24;
    137 		}
    138 	}
    139 	return (h);
    140 }
    141 
    142 const Elf_Sym *
    143 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
    144     const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry,
    145     DoneList *dlp)
    146 {
    147 	const Elf_Sym *symp;
    148 	const Elf_Sym *def;
    149 	const Obj_Entry *defobj;
    150 	const Objlist_Entry *elm;
    151 
    152 	def = NULL;
    153 	defobj = NULL;
    154 	SIMPLEQ_FOREACH(elm, objlist, link) {
    155 		if (_rtld_donelist_check(dlp, elm->obj))
    156 			continue;
    157 		rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
    158 		    name));
    159 		symp = _rtld_symlook_obj(name, hash, elm->obj, flags, ventry);
    160 		if (symp != NULL) {
    161 			if ((def == NULL) ||
    162 			    (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    163 				def = symp;
    164 				defobj = elm->obj;
    165 				if (ELF_ST_BIND(def->st_info) != STB_WEAK)
    166 					break;
    167 			}
    168 		}
    169 	}
    170 	if (def != NULL)
    171 		*defobj_out = defobj;
    172 	return def;
    173 }
    174 
    175 /*
    176  * Search the symbol table of a shared object and all objects needed by it for
    177  * a symbol of the given name. Search order is breadth-first. Returns a pointer
    178  * to the symbol, or NULL if no definition was found.
    179  */
    180 const Elf_Sym *
    181 _rtld_symlook_needed(const char *name, unsigned long hash,
    182     const Needed_Entry *needed, const Obj_Entry **defobj_out, u_int flags,
    183     const Ver_Entry *ventry, DoneList *breadth, DoneList *depth)
    184 {
    185 	const Elf_Sym *def, *def_w;
    186 	const Needed_Entry *n;
    187 	const Obj_Entry *obj, *defobj, *defobj1;
    188 
    189 	def = def_w = NULL;
    190 	defobj = NULL;
    191 	for (n = needed; n != NULL; n = n->next) {
    192 		if ((obj = n->obj) == NULL)
    193 			continue;
    194 		if (_rtld_donelist_check(breadth, obj))
    195 			continue;
    196 		def = _rtld_symlook_obj(name, hash, obj, flags, ventry);
    197 		if (def == NULL)
    198 			continue;
    199 		defobj = obj;
    200 		if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
    201 			*defobj_out = defobj;
    202 
    203 			return (def);
    204 		}
    205 	}
    206 	/*
    207 	 * Either the symbol definition has not been found in directly needed
    208 	 * objects, or the found symbol is weak.
    209 	 */
    210 	for (n = needed; n != NULL; n = n->next) {
    211 		if ((obj = n->obj) == NULL)
    212 			continue;
    213 		if (_rtld_donelist_check(depth, obj))
    214 			continue;
    215 		def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
    216 		    flags, ventry, breadth, depth);
    217 		if (def_w == NULL)
    218 			continue;
    219 		if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
    220 			def = def_w;
    221 			defobj = defobj1;
    222 			if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
    223 				break;
    224 		}
    225 	}
    226 	if (def != NULL)
    227 		*defobj_out = defobj;
    228 
    229 	return def;
    230 }
    231 
    232 /*
    233  * Search the symbol table of a single shared object for a symbol of
    234  * the given name.  Returns a pointer to the symbol, or NULL if no
    235  * definition was found.
    236  *
    237  * The symbol's hash value is passed in for efficiency reasons; that
    238  * eliminates many recomputations of the hash value.
    239  */
    240 const Elf_Sym *
    241 _rtld_symlook_obj(const char *name, unsigned long hash,
    242     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
    243 {
    244 	unsigned long symnum;
    245 	const Elf_Sym *vsymp = NULL;
    246 	Elf_Half verndx;
    247 	int vcount = 0;
    248 
    249 	for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
    250 	     obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
    251 	     symnum != ELF_SYM_UNDEFINED;
    252 	     symnum = obj->chains[symnum]) {
    253 		const Elf_Sym  *symp;
    254 		const char     *strp;
    255 
    256 		assert(symnum < obj->nchains);
    257 		symp = obj->symtab + symnum;
    258 		strp = obj->strtab + symp->st_name;
    259 		rdbg(("check \"%s\" vs \"%s\" in %s", name, strp, obj->path));
    260 		if (name[1] != strp[1] || strcmp(name, strp))
    261 			continue;
    262 #ifdef __mips__
    263 		if (symp->st_shndx == SHN_UNDEF)
    264 			continue;
    265 #else
    266 		/*
    267 		 * XXX DANGER WILL ROBINSON!
    268 		 * If we have a function pointer in the executable's
    269 		 * data section, it points to the executable's PLT
    270 		 * slot, and there is NO relocation emitted.  To make
    271 		 * the function pointer comparable to function pointers
    272 		 * in shared libraries, we must resolve data references
    273 		 * in the libraries to point to PLT slots in the
    274 		 * executable, if they exist.
    275 		 */
    276 		if (symp->st_shndx == SHN_UNDEF &&
    277 		    ((flags & SYMLOOK_IN_PLT) ||
    278 		    symp->st_value == 0 ||
    279 		    ELF_ST_TYPE(symp->st_info) != STT_FUNC))
    280 			continue;
    281 #endif
    282 
    283 		if (ventry == NULL) {
    284 			if (obj->versyms != NULL) {
    285 				verndx = VER_NDX(obj->versyms[symnum].vs_vers);
    286 				if (verndx > obj->vertabnum) {
    287 					_rtld_error("%s: symbol %s references "
    288 					    "wrong version %d", obj->path,
    289 					    &obj->strtab[symnum], verndx);
    290 					continue;
    291 				}
    292 
    293 				/*
    294 				 * If we are not called from dlsym (i.e. this
    295 				 * is a normal relocation from unversioned
    296 				 * binary), accept the symbol immediately
    297 				 * if it happens to have first version after
    298 				 * this shared object became versioned.
    299 				 * Otherwise, if symbol is versioned and not
    300 				 * hidden, remember it. If it is the only
    301 				 * symbol with this name exported by the shared
    302 				 * object, it will be returned as a match at the
    303 				 * end of the function. If symbol is global
    304 				 * (verndx < 2) accept it unconditionally.
    305 				 */
    306 				if (!(flags & SYMLOOK_DLSYM) &&
    307 				    verndx == VER_NDX_GIVEN) {
    308 					return symp;
    309 				} else if (verndx >= VER_NDX_GIVEN) {
    310 					if (!(obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN)) {
    311 						if (vsymp == NULL)
    312 							vsymp = symp;
    313 						vcount++;
    314 					}
    315 					continue;
    316 				}
    317 			}
    318 			return symp;
    319 		} else {
    320 			if (obj->versyms == NULL) {
    321 				if (_rtld_object_match_name(obj, ventry->name)){
    322 					_rtld_error("%s: object %s should "
    323 					    "provide version %s for symbol %s",
    324 					    _rtld_objself.path, obj->path,
    325 					    ventry->name, &obj->strtab[symnum]);
    326 					continue;
    327 				}
    328 			} else {
    329 				verndx = VER_NDX(obj->versyms[symnum].vs_vers);
    330 				if (verndx > obj->vertabnum) {
    331 					_rtld_error("%s: symbol %s references "
    332 					    "wrong version %d", obj->path,
    333 					    &obj->strtab[symnum], verndx);
    334 					continue;
    335 				}
    336 				if (obj->vertab[verndx].hash != ventry->hash ||
    337 				    strcmp(obj->vertab[verndx].name, ventry->name)) {
    338 					/*
    339 					* Version does not match. Look if this
    340 					* is a global symbol and if it is not
    341 					* hidden. If global symbol (verndx < 2)
    342 					* is available, use it. Do not return
    343 					* symbol if we are called by dlvsym,
    344 					* because dlvsym looks for a specific
    345 					* version and default one is not what
    346 					* dlvsym wants.
    347 					*/
    348 					if ((flags & SYMLOOK_DLSYM) ||
    349 					    (obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN) ||
    350 					    (verndx >= VER_NDX_GIVEN))
    351 						continue;
    352 				}
    353 			}
    354 			return symp;
    355 		}
    356 	}
    357 	if (vcount == 1)
    358 		return vsymp;
    359 	return NULL;
    360 }
    361 
    362 #ifdef COMBRELOC
    363 static const Obj_Entry *_rtld_last_refobj;
    364 
    365 /*
    366  * Called when an object is freed. Reset the cached symbol look up if
    367  * our last referencing or definition object just got unloaded.
    368  */
    369 void
    370 _rtld_combreloc_reset(const Obj_Entry *obj)
    371 {
    372 	if (_rtld_last_refobj == obj)
    373 		_rtld_last_refobj = NULL;
    374 }
    375 #endif
    376 
    377 /*
    378  * Given a symbol number in a referencing object, find the corresponding
    379  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    380  * no definition was found.  Returns a pointer to the Obj_Entry of the
    381  * defining object via the reference parameter DEFOBJ_OUT.
    382  */
    383 const Elf_Sym *
    384 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
    385     const Obj_Entry **defobj_out, u_int flags)
    386 {
    387 	const Elf_Sym  *ref;
    388 	const Elf_Sym  *def;
    389 	const Obj_Entry *defobj;
    390 	const char     *name;
    391 	unsigned long   hash;
    392 
    393 #ifdef COMBRELOC
    394 	/*
    395 	 * COMBRELOC combines multiple reloc sections and sorts them to make
    396 	 * dynamic symbol lookup caching possible.
    397 	 *
    398 	 * So if the lookup we are doing is the same as the previous lookup
    399 	 * return the cached results.
    400 	 */
    401 	static unsigned long last_symnum;
    402 	static const Obj_Entry *last_defobj;
    403 	static const Elf_Sym *last_def;
    404 
    405 	if (symnum == last_symnum && refobj == _rtld_last_refobj
    406 	    && !(flags & SYMLOOK_IN_PLT)) {
    407 		*defobj_out = last_defobj;
    408 		return last_def;
    409 	}
    410 #endif
    411 
    412 	ref = refobj->symtab + symnum;
    413 	name = refobj->strtab + ref->st_name;
    414 
    415 	/*
    416 	 * We don't have to do a full scale lookup if the symbol is local.
    417 	 * We know it will bind to the instance in this load module; to
    418 	 * which we already have a pointer (ie ref).
    419 	 */
    420 	if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
    421 		if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
    422 			_rtld_error("%s: Bogus symbol table entry %lu",
    423 			    refobj->path, symnum);
    424         	}
    425 
    426 		hash = _rtld_elf_hash(name);
    427 		defobj = NULL;
    428 		def = _rtld_symlook_default(name, hash, refobj, &defobj, flags,
    429 		    _rtld_fetch_ventry(refobj, symnum));
    430 	} else {
    431 		rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
    432 		def = ref;
    433 		defobj = refobj;
    434 	}
    435 
    436 	/*
    437 	 * If we found no definition and the reference is weak, treat the
    438 	 * symbol as having the value zero.
    439 	 */
    440 	if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
    441 		rdbg(("  returning _rtld_sym_zero@_rtld_objself"));
    442 		def = &_rtld_sym_zero;
    443 		defobj = &_rtld_objself;
    444 	}
    445 
    446 	if (def != NULL) {
    447 		*defobj_out = defobj;
    448 #ifdef COMBRELOC
    449 		if (!(flags & SYMLOOK_IN_PLT)) {
    450 			/*
    451 			 * Cache the lookup arguments and results if this was
    452 			 * non-PLT lookup.
    453 			 */
    454 			last_symnum = symnum;
    455 			_rtld_last_refobj = refobj;
    456 			last_def = def;
    457 			last_defobj = defobj;
    458 		}
    459 #endif
    460 	} else {
    461 		rdbg(("lookup failed"));
    462 		_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
    463 		    refobj->path, (flags & SYMLOOK_IN_PLT) ? "PLT " : "",
    464 		    name, symnum);
    465 	}
    466 	return def;
    467 }
    468 
    469 const Elf_Sym *
    470 _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
    471     const Obj_Entry **defobj, bool imm)
    472 {
    473  	const Elf_Sym  *def = _rtld_find_symdef(symnum, obj, defobj,
    474 	    SYMLOOK_IN_PLT);
    475 	if (__predict_false(def == NULL))
    476  		return NULL;
    477 
    478 	if (__predict_false(def == &_rtld_sym_zero)) {
    479 		/* tp is set during lazy binding. */
    480 		if (imm) {
    481 			const Elf_Sym	*ref = obj->symtab + symnum;
    482 			const char	*name = obj->strtab + ref->st_name;
    483 
    484 			_rtld_error(
    485 			    "%s: Trying to call undefined weak symbol `%s'",
    486 			    obj->path, name);
    487 			return NULL;
    488 		}
    489 	}
    490 	return def;
    491 }
    492 
    493 /*
    494  * Given a symbol name in a referencing object, find the corresponding
    495  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    496  * no definition was found.  Returns a pointer to the Obj_Entry of the
    497  * defining object via the reference parameter DEFOBJ_OUT.
    498  */
    499 const Elf_Sym *
    500 _rtld_symlook_default(const char *name, unsigned long hash,
    501     const Obj_Entry *refobj, const Obj_Entry **defobj_out, u_int flags,
    502     const Ver_Entry *ventry)
    503 {
    504 	const Elf_Sym *def;
    505 	const Elf_Sym *symp;
    506 	const Obj_Entry *obj;
    507 	const Obj_Entry *defobj;
    508 	const Objlist_Entry *elm;
    509 	def = NULL;
    510 	defobj = NULL;
    511 	DoneList donelist;
    512 
    513 	_rtld_donelist_init(&donelist);
    514 
    515 	/* Look first in the referencing object if linked symbolically. */
    516 	if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
    517 		rdbg(("search referencing object for %s", name));
    518 		symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
    519 		if (symp != NULL) {
    520 			def = symp;
    521 			defobj = refobj;
    522 		}
    523 	}
    524 
    525 	/* Search all objects loaded at program start up. */
    526 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    527 		rdbg(("search _rtld_list_main for %s", name));
    528 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
    529 		    flags, ventry, &donelist);
    530 		if (symp != NULL &&
    531 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    532 			def = symp;
    533 			defobj = obj;
    534 		}
    535 	}
    536 
    537 	/* Search all RTLD_GLOBAL objects. */
    538 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    539 		rdbg(("search _rtld_list_global for %s", name));
    540 		symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
    541 		    &obj, flags, ventry, &donelist);
    542 		if (symp != NULL &&
    543 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    544 			def = symp;
    545 			defobj = obj;
    546 		}
    547 	}
    548 
    549 	/* Search all dlopened DAGs containing the referencing object. */
    550 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
    551 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    552 			break;
    553 		rdbg(("search DAG with root %p (%s) for %s", elm->obj,
    554 		    elm->obj->path, name));
    555 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
    556 		    &obj, flags, ventry, &donelist);
    557 		if (symp != NULL &&
    558 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    559 			def = symp;
    560 			defobj = obj;
    561 		}
    562 	}
    563 
    564 	/*
    565 	 * Search the dynamic linker itself, and possibly resolve the
    566 	 * symbol from there.  This is how the application links to
    567 	 * dynamic linker services such as dlopen.  Only the values listed
    568 	 * in the "_rtld_exports" array can be resolved from the dynamic
    569 	 * linker.
    570 	 */
    571 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    572 		rdbg(("Search the dynamic linker itself."));
    573 		symp = _rtld_symlook_obj(name, hash, &_rtld_objself, flags,
    574 		    ventry);
    575 		if (symp != NULL && _rtld_is_exported(symp)) {
    576 			def = symp;
    577 			defobj = &_rtld_objself;
    578 		}
    579 	}
    580 
    581 	if (def != NULL)
    582 		*defobj_out = defobj;
    583 	return def;
    584 }
    585