Home | History | Annotate | Line # | Download | only in ld.elf_so
symbol.c revision 1.70
      1 /*	$NetBSD: symbol.c,v 1.70 2020/02/29 04:21:42 kamil 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.70 2020/02/29 04:21:42 kamil 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 /*
     63  * If the given object is already in the donelist, return true.  Otherwise
     64  * add the object to the list and return false.
     65  */
     66 static bool
     67 _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
     68 {
     69 	unsigned int i;
     70 
     71 	for (i = 0;  i < dlp->num_used;  i++)
     72 		if (dlp->objs[i] == obj)
     73 			return true;
     74 	/*
     75 	 * Our donelist allocation may not always be sufficient as we're not
     76 	 * thread safe. We'll handle it properly anyway.
     77 	 */
     78 	if (dlp->num_used < dlp->num_alloc)
     79 		dlp->objs[dlp->num_used++] = obj;
     80 	return false;
     81 }
     82 
     83 /*
     84  * Hash function for symbol table lookup.  Don't even think about changing
     85  * this.  It is specified by the System V ABI.
     86  */
     87 unsigned long
     88 _rtld_elf_hash(const char *name)
     89 {
     90 	const unsigned char *p = (const unsigned char *) name;
     91 	unsigned long   h = 0;
     92 	unsigned long   g;
     93 	unsigned long   c;
     94 
     95 	for (; __predict_true((c = *p) != '\0'); p++) {
     96 		h <<= 4;
     97 		h += c;
     98 		if ((g = h & 0xf0000000) != 0) {
     99 			h ^= g;
    100 			h ^= g >> 24;
    101 		}
    102 	}
    103 	return (h);
    104 }
    105 
    106 const Elf_Sym *
    107 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
    108     const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry,
    109     DoneList *dlp)
    110 {
    111 	const Elf_Sym *symp;
    112 	const Elf_Sym *def;
    113 	const Obj_Entry *defobj;
    114 	const Objlist_Entry *elm;
    115 
    116 	def = NULL;
    117 	defobj = NULL;
    118 	SIMPLEQ_FOREACH(elm, objlist, link) {
    119 		if (_rtld_donelist_check(dlp, elm->obj))
    120 			continue;
    121 		rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
    122 		    name));
    123 		symp = _rtld_symlook_obj(name, hash, elm->obj, flags, ventry);
    124 		if (symp != 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, u_int flags,
    147     const Ver_Entry *ventry, DoneList *breadth, DoneList *depth)
    148 {
    149 	const Elf_Sym *def, *def_w;
    150 	const Needed_Entry *n;
    151 	const Obj_Entry *obj, *defobj, *defobj1;
    152 
    153 	def = def_w = NULL;
    154 	defobj = NULL;
    155 	for (n = needed; n != NULL; n = n->next) {
    156 		if ((obj = n->obj) == NULL)
    157 			continue;
    158 		if (_rtld_donelist_check(breadth, obj))
    159 			continue;
    160 		def = _rtld_symlook_obj(name, hash, obj, flags, ventry);
    161 		if (def == NULL)
    162 			continue;
    163 		defobj = obj;
    164 		if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
    165 			*defobj_out = defobj;
    166 
    167 			return (def);
    168 		}
    169 	}
    170 	/*
    171 	 * Either the symbol definition has not been found in directly needed
    172 	 * objects, or the found symbol is weak.
    173 	 */
    174 	for (n = needed; n != NULL; n = n->next) {
    175 		if ((obj = n->obj) == NULL)
    176 			continue;
    177 		if (_rtld_donelist_check(depth, obj))
    178 			continue;
    179 		def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
    180 		    flags, ventry, breadth, depth);
    181 		if (def_w == NULL)
    182 			continue;
    183 		if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
    184 			def = def_w;
    185 			defobj = defobj1;
    186 			if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
    187 				break;
    188 		}
    189 	}
    190 	if (def != NULL)
    191 		*defobj_out = defobj;
    192 
    193 	return def;
    194 }
    195 
    196 static bool
    197 _rtld_symlook_obj_matched_symbol(const char *name,
    198     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry,
    199     unsigned long symnum, const Elf_Sym **vsymp, int *vcount)
    200 {
    201 	const Elf_Sym  *symp;
    202 	const char     *strp;
    203 	Elf_Half verndx;
    204 
    205 	symp = obj->symtab + symnum;
    206 	strp = obj->strtab + symp->st_name;
    207 	rdbg(("check \"%s\" vs \"%s\" in %s", name, strp, obj->path));
    208 	if (name[1] != strp[1] || strcmp(name, strp))
    209 		return false;
    210 #if defined(__mips__) || defined(__vax__)
    211 	if (symp->st_shndx == SHN_UNDEF)
    212 		continue;
    213 #else
    214 	/*
    215 	 * XXX DANGER WILL ROBINSON!
    216 	 * If we have a function pointer in the executable's
    217 	 * data section, it points to the executable's PLT
    218 	 * slot, and there is NO relocation emitted.  To make
    219 	 * the function pointer comparable to function pointers
    220 	 * in shared libraries, we must resolve data references
    221 	 * in the libraries to point to PLT slots in the
    222 	 * executable, if they exist.
    223 	 */
    224 	if (symp->st_shndx == SHN_UNDEF &&
    225 	    ((flags & SYMLOOK_IN_PLT) ||
    226 	    symp->st_value == 0 ||
    227 	    ELF_ST_TYPE(symp->st_info) != STT_FUNC))
    228 		return false;
    229 #endif
    230 
    231 	if (ventry == NULL) {
    232 		if (obj->versyms != NULL) {
    233 			verndx = VER_NDX(obj->versyms[symnum].vs_vers);
    234 			if (verndx > obj->vertabnum) {
    235 				_rtld_error("%s: symbol %s references "
    236 				    "wrong version %d", obj->path,
    237 				    &obj->strtab[symnum], verndx);
    238 				return false;
    239 			}
    240 
    241 			/*
    242 			 * If we are not called from dlsym (i.e. this
    243 			 * is a normal relocation from unversioned
    244 			 * binary), accept the symbol immediately
    245 			 * if it happens to have first version after
    246 			 * this shared object became versioned.
    247 			 * Otherwise, if symbol is versioned and not
    248 			 * hidden, remember it. If it is the only
    249 			 * symbol with this name exported by the shared
    250 			 * object, it will be returned as a match at the
    251 			 * end of the function. If symbol is global
    252 			 * (verndx < 2) accept it unconditionally.
    253 			 */
    254 			if (!(flags & SYMLOOK_DLSYM) &&
    255 			    verndx == VER_NDX_GIVEN) {
    256 				*vsymp = symp;
    257 				return true;
    258 			} else if (verndx >= VER_NDX_GIVEN) {
    259 				if (!(obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN)) {
    260 					if (*vsymp == NULL)
    261 						*vsymp = symp;
    262 					(*vcount)++;
    263 				}
    264 				return false;
    265 			}
    266 		}
    267 		*vsymp = symp;
    268 		return true;
    269 	} else {
    270 		if (obj->versyms == NULL) {
    271 			if (_rtld_object_match_name(obj, ventry->name)){
    272 				_rtld_error("%s: object %s should "
    273 				    "provide version %s for symbol %s",
    274 				    _rtld_objself.path, obj->path,
    275 				    ventry->name, &obj->strtab[symnum]);
    276 				return false;
    277 			}
    278 		} else {
    279 			verndx = VER_NDX(obj->versyms[symnum].vs_vers);
    280 			if (verndx > obj->vertabnum) {
    281 				_rtld_error("%s: symbol %s references "
    282 				    "wrong version %d", obj->path,
    283 				    &obj->strtab[symnum], verndx);
    284 				return false;
    285 			}
    286 			if (obj->vertab[verndx].hash != ventry->hash ||
    287 			    strcmp(obj->vertab[verndx].name, ventry->name)) {
    288 				/*
    289 				* Version does not match. Look if this
    290 				* is a global symbol and if it is not
    291 				* hidden. If global symbol (verndx < 2)
    292 				* is available, use it. Do not return
    293 				* symbol if we are called by dlvsym,
    294 				* because dlvsym looks for a specific
    295 				* version and default one is not what
    296 				* dlvsym wants.
    297 				*/
    298 				if ((flags & SYMLOOK_DLSYM) ||
    299 				    (obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN) ||
    300 				    (verndx >= VER_NDX_GIVEN))
    301 					return false;
    302 			}
    303 		}
    304 		*vsymp = symp;
    305 		return true;
    306 	}
    307 }
    308 
    309 /*
    310  * Search the symbol table of a single shared object for a symbol of
    311  * the given name.  Returns a pointer to the symbol, or NULL if no
    312  * definition was found.
    313  *
    314  * The symbol's hash value is passed in for efficiency reasons; that
    315  * eliminates many recomputations of the hash value.
    316  */
    317 const Elf_Sym *
    318 _rtld_symlook_obj(const char *name, unsigned long hash,
    319     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
    320 {
    321 	unsigned long symnum;
    322 	const Elf_Sym *vsymp = NULL;
    323 	int vcount = 0;
    324 
    325 	for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
    326 	     obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
    327 	     symnum != ELF_SYM_UNDEFINED;
    328 	     symnum = obj->chains[symnum]) {
    329 		assert(symnum < obj->nchains);
    330 
    331 		if (_rtld_symlook_obj_matched_symbol(name, obj, flags,
    332 		    ventry, symnum, &vsymp, &vcount)) {
    333 			return vsymp;
    334 		}
    335 	}
    336 	if (vcount == 1)
    337 		return vsymp;
    338 	return NULL;
    339 }
    340 
    341 /*
    342  * Given a symbol number in a referencing object, find the corresponding
    343  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    344  * no definition was found.  Returns a pointer to the Obj_Entry of the
    345  * defining object via the reference parameter DEFOBJ_OUT.
    346  */
    347 const Elf_Sym *
    348 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
    349     const Obj_Entry **defobj_out, u_int flags)
    350 {
    351 	const Elf_Sym  *ref;
    352 	const Elf_Sym  *def;
    353 	const Obj_Entry *defobj;
    354 	const char     *name;
    355 	unsigned long   hash;
    356 
    357 	ref = refobj->symtab + symnum;
    358 	name = refobj->strtab + ref->st_name;
    359 
    360 	/*
    361 	 * We don't have to do a full scale lookup if the symbol is local.
    362 	 * We know it will bind to the instance in this load module; to
    363 	 * which we already have a pointer (ie ref).
    364 	 */
    365 	if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
    366 		if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
    367 			_rtld_error("%s: Bogus symbol table entry %lu",
    368 			    refobj->path, symnum);
    369         	}
    370 
    371 		hash = _rtld_elf_hash(name);
    372 		defobj = NULL;
    373 		def = _rtld_symlook_default(name, hash, refobj, &defobj, flags,
    374 		    _rtld_fetch_ventry(refobj, symnum));
    375 	} else {
    376 		rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
    377 		def = ref;
    378 		defobj = refobj;
    379 	}
    380 
    381 	/*
    382 	 * If we found no definition and the reference is weak, treat the
    383 	 * symbol as having the value zero.
    384 	 */
    385 	if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
    386 		rdbg(("  returning _rtld_sym_zero@_rtld_objself"));
    387 		def = &_rtld_sym_zero;
    388 		defobj = &_rtld_objself;
    389 	}
    390 
    391 	if (def != NULL) {
    392 		*defobj_out = defobj;
    393 	} else {
    394 		rdbg(("lookup failed"));
    395 		_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
    396 		    refobj->path, (flags & SYMLOOK_IN_PLT) ? "PLT " : "",
    397 		    name, symnum);
    398 	}
    399 	return def;
    400 }
    401 
    402 const Elf_Sym *
    403 _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
    404     const Obj_Entry **defobj, bool imm)
    405 {
    406  	const Elf_Sym  *def = _rtld_find_symdef(symnum, obj, defobj,
    407 	    SYMLOOK_IN_PLT);
    408 	if (__predict_false(def == NULL))
    409  		return NULL;
    410 
    411 	if (__predict_false(def == &_rtld_sym_zero)) {
    412 		/* tp is set during lazy binding. */
    413 		if (imm) {
    414 			const Elf_Sym	*ref = obj->symtab + symnum;
    415 			const char	*name = obj->strtab + ref->st_name;
    416 
    417 			_rtld_error(
    418 			    "%s: Trying to call undefined weak symbol `%s'",
    419 			    obj->path, name);
    420 			return NULL;
    421 		}
    422 	}
    423 	return def;
    424 }
    425 
    426 /*
    427  * Given a symbol name in a referencing object, find the corresponding
    428  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    429  * no definition was found.  Returns a pointer to the Obj_Entry of the
    430  * defining object via the reference parameter DEFOBJ_OUT.
    431  */
    432 const Elf_Sym *
    433 _rtld_symlook_default(const char *name, unsigned long hash,
    434     const Obj_Entry *refobj, const Obj_Entry **defobj_out, u_int flags,
    435     const Ver_Entry *ventry)
    436 {
    437 	const Elf_Sym *def;
    438 	const Elf_Sym *symp;
    439 	const Obj_Entry *obj;
    440 	const Obj_Entry *defobj;
    441 	const Objlist_Entry *elm;
    442 	def = NULL;
    443 	defobj = NULL;
    444 	DoneList donelist;
    445 
    446 	_rtld_donelist_init(&donelist);
    447 
    448 	/* Look first in the referencing object if linked symbolically. */
    449 	if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
    450 		rdbg(("search referencing object for %s", name));
    451 		symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
    452 		if (symp != NULL) {
    453 			def = symp;
    454 			defobj = refobj;
    455 		}
    456 	}
    457 
    458 	/* Search all objects loaded at program start up. */
    459 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    460 		rdbg(("search _rtld_list_main for %s", name));
    461 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
    462 		    flags, ventry, &donelist);
    463 		if (symp != NULL &&
    464 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    465 			def = symp;
    466 			defobj = obj;
    467 		}
    468 	}
    469 
    470 	/* Search all RTLD_GLOBAL objects. */
    471 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    472 		rdbg(("search _rtld_list_global for %s", name));
    473 		symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
    474 		    &obj, flags, ventry, &donelist);
    475 		if (symp != NULL &&
    476 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    477 			def = symp;
    478 			defobj = obj;
    479 		}
    480 	}
    481 
    482 	/* Search all dlopened DAGs containing the referencing object. */
    483 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
    484 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    485 			break;
    486 		rdbg(("search DAG with root %p (%s) for %s", elm->obj,
    487 		    elm->obj->path, name));
    488 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
    489 		    &obj, flags, ventry, &donelist);
    490 		if (symp != NULL &&
    491 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    492 			def = symp;
    493 			defobj = obj;
    494 		}
    495 	}
    496 
    497 	/*
    498 	 * Finally, look in the referencing object if not linked symbolically.
    499 	 * This is necessary for DF_1_NODELETE objects where the containing DAG
    500 	 * has been unlinked, so local references are resolved properly.
    501 	 */
    502 	if ((def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) &&
    503 	    !refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
    504 		rdbg(("search referencing object for %s", name));
    505 		symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
    506 		if (symp != NULL) {
    507 			def = symp;
    508 			defobj = refobj;
    509 		}
    510 	}
    511 
    512 	/*
    513 	 * Search the dynamic linker itself, and possibly resolve the
    514 	 * symbol from there.  This is how the application links to
    515 	 * dynamic linker services such as dlopen.
    516 	 */
    517 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    518 		rdbg(("Search the dynamic linker itself."));
    519 		symp = _rtld_symlook_obj(name, hash, &_rtld_objself, flags,
    520 		    ventry);
    521 		if (symp != NULL) {
    522 			def = symp;
    523 			defobj = &_rtld_objself;
    524 		}
    525 	}
    526 
    527 	if (def != NULL)
    528 		*defobj_out = defobj;
    529 	return def;
    530 }
    531