Home | History | Annotate | Line # | Download | only in ld.elf_so
symbol.c revision 1.35
      1 /*	$NetBSD: symbol.c,v 1.35 2003/12/07 09:36:06 mrauch 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 <err.h>
     42 #include <errno.h>
     43 #include <fcntl.h>
     44 #include <stdarg.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <unistd.h>
     49 #include <sys/types.h>
     50 #include <sys/mman.h>
     51 #include <dirent.h>
     52 
     53 #include "debug.h"
     54 #include "rtld.h"
     55 
     56 static bool
     57 _rtld_is_exported(const Elf_Sym *def)
     58 {
     59 	static Elf_Addr _rtld_exports[] = {
     60 		(Elf_Addr)dlopen,
     61 		(Elf_Addr)dlclose,
     62 		(Elf_Addr)dlsym,
     63 		(Elf_Addr)dlerror,
     64 		(Elf_Addr)dladdr,
     65 		0
     66 	};
     67 	int i;
     68 
     69 	Elf_Addr value;
     70 	value = (Elf_Addr)(_rtld_objself.relocbase + def->st_value);
     71 
     72 	for (i = 0; _rtld_exports[i] != 0; i++) {
     73 		if (value == _rtld_exports[i])
     74 			return true;
     75 	}
     76 	return false;
     77 }
     78 
     79 /*
     80  * Hash function for symbol table lookup.  Don't even think about changing
     81  * this.  It is specified by the System V ABI.
     82  */
     83 unsigned long
     84 _rtld_elf_hash(const char *name)
     85 {
     86 	const unsigned char *p = (const unsigned char *) name;
     87 	unsigned long   h = 0;
     88 	unsigned long   g;
     89 	unsigned long   c;
     90 
     91 	for (; __predict_true((c = *p) != '\0'); p++) {
     92 		h <<= 4;
     93 		h += c;
     94 		if ((g = h & 0xf0000000) != 0) {
     95 			h ^= g;
     96 			h ^= g >> 24;
     97 		}
     98 	}
     99 	return (h);
    100 }
    101 
    102 const Elf_Sym *
    103 _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
    104     const Obj_Entry **defobj_out, bool in_plt)
    105 {
    106 	const Elf_Sym *symp;
    107 	const Elf_Sym *def;
    108 	const Obj_Entry *defobj;
    109 	const Objlist_Entry *elm;
    110 
    111 	def = NULL;
    112 	defobj = NULL;
    113 	SIMPLEQ_FOREACH(elm, objlist, link) {
    114 		rdbg(("search object %p (%s)", elm->obj, elm->obj->path));
    115 		if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
    116 		    != NULL) {
    117 			if ((def == NULL) ||
    118 			    (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    119 				def = symp;
    120 				defobj = elm->obj;
    121 				if (ELF_ST_BIND(def->st_info) != STB_WEAK)
    122 					break;
    123 			}
    124 		}
    125 	}
    126 	if (def != NULL)
    127 		*defobj_out = defobj;
    128 	return def;
    129 }
    130 
    131 /*
    132  * Search the symbol table of a single shared object for a symbol of
    133  * the given name.  Returns a pointer to the symbol, or NULL if no
    134  * definition was found.
    135  *
    136  * The symbol's hash value is passed in for efficiency reasons; that
    137  * eliminates many recomputations of the hash value.
    138  */
    139 const Elf_Sym *
    140 _rtld_symlook_obj(const char *name, unsigned long hash,
    141     const Obj_Entry *obj, bool in_plt)
    142 {
    143 	unsigned long symnum;
    144 
    145 	for (symnum = obj->buckets[hash % obj->nbuckets];
    146 	     symnum != ELF_SYM_UNDEFINED;
    147 	     symnum = obj->chains[symnum]) {
    148 		const Elf_Sym  *symp;
    149 		const char     *strp;
    150 
    151 		assert(symnum < obj->nchains);
    152 		symp = obj->symtab + symnum;
    153 		strp = obj->strtab + symp->st_name;
    154 		rdbg(("check %s vs %s in %p", name, strp, obj));
    155 		if (name[1] == strp[1] && !strcmp(name, strp)) {
    156 			if (symp->st_shndx != SHN_UNDEF)
    157 				return symp;
    158 #ifndef __mips__
    159 			/*
    160 			 * XXX DANGER WILL ROBINSON!
    161 			 * If we have a function pointer in the executable's
    162 			 * data section, it points to the executable's PLT
    163 			 * slot, and there is NO relocation emitted.  To make
    164 			 * the function pointer comparable to function pointers
    165 			 * in shared libraries, we must resolve data references
    166 			 * in the libraries to point to PLT slots in the
    167 			 * executable, if they exist.
    168 			 */
    169 			else if (!in_plt && symp->st_value != 0 &&
    170 			     ELF_ST_TYPE(symp->st_info) == STT_FUNC)
    171 				return symp;
    172 #endif
    173 			else
    174 				return NULL;
    175 		}
    176 	}
    177 
    178 	return NULL;
    179 }
    180 
    181 /*
    182  * Given a symbol number in a referencing object, find the corresponding
    183  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    184  * no definition was found.  Returns a pointer to the Obj_Entry of the
    185  * defining object via the reference parameter DEFOBJ_OUT.
    186  */
    187 const Elf_Sym *
    188 _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
    189     const Obj_Entry **defobj_out, bool in_plt)
    190 {
    191 	const Elf_Sym  *ref;
    192 	const Elf_Sym  *def;
    193 	const Elf_Sym  *symp;
    194 	const Obj_Entry *obj;
    195 	const Obj_Entry *defobj;
    196 	const Objlist_Entry *elm;
    197 	const char     *name;
    198 	unsigned long   hash;
    199 
    200 	ref = refobj->symtab + symnum;
    201 	name = refobj->strtab + ref->st_name;
    202 
    203 	hash = _rtld_elf_hash(name);
    204 	def = NULL;
    205 	defobj = NULL;
    206 
    207 	/* Look first in the referencing object if linked symbolically */
    208 	if (refobj->symbolic) {
    209 		symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
    210 		if (symp != NULL) {
    211 			def = symp;
    212 			defobj = refobj;
    213 		}
    214 	}
    215 
    216 	/* Search all objects loaded at program start up. */
    217 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    218 		rdbg(("search _rtld_list_main"));
    219 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, in_plt);
    220 		if (symp != NULL &&
    221 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    222 			def = symp;
    223 			defobj = obj;
    224 		}
    225 	}
    226 
    227 	/* Search all RTLD_GLOBAL objects. */
    228 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    229 		rdbg(("search _rtld_list_global"));
    230 		symp = _rtld_symlook_list(name, hash, &_rtld_list_global, &obj, in_plt);
    231 		if (symp != NULL &&
    232 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    233 			def = symp;
    234 			defobj = obj;
    235 		}
    236 	}
    237 
    238 	/* Search all dlopened DAGs containing the referencing object. */
    239 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
    240 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    241 			break;
    242 		rdbg(("search DAG with root %p (%s)", elm->obj, elm->obj->path));
    243 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj, in_plt);
    244 		if (symp != NULL &&
    245 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    246 			def = symp;
    247 			defobj = obj;
    248 		}
    249 	}
    250 
    251 	/*
    252 	 * Search the dynamic linker itself, and possibly resolve the
    253 	 * symbol from there.  This is how the application links to
    254 	 * dynamic linker services such as dlopen.  Only the values listed
    255 	 * in the "_rtld_exports" array can be resolved from the dynamic linker.
    256 	 */
    257 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    258 		symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
    259 		if (symp != NULL && _rtld_is_exported(symp)) {
    260 			def = symp;
    261 			defobj = &_rtld_objself;
    262 		}
    263 	}
    264 
    265 	/*
    266 	 * If we found no definition and the reference is weak, treat the
    267 	 * symbol as having the value zero.
    268 	 */
    269 	if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
    270 		rdbg(("  returning _rtld_sym_zero@_rtld_objmain"));
    271 		def = &_rtld_sym_zero;
    272 		defobj = _rtld_objmain;
    273 	}
    274 
    275 	if (def != NULL)
    276 		*defobj_out = defobj;
    277 	else {
    278 		rdbg(("lookup failed"));
    279 		_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
    280 		    refobj->path, in_plt ? "PLT " : "", name, symnum);
    281 	}
    282 	return def;
    283 }
    284 
    285 /*
    286  * Given a symbol name in a referencing object, find the corresponding
    287  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    288  * no definition was found.  Returns a pointer to the Obj_Entry of the
    289  * defining object via the reference parameter DEFOBJ_OUT.
    290  */
    291 const Elf_Sym *
    292 _rtld_symlook_default(const char *name, unsigned long hash,
    293     const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
    294 {
    295 	const Elf_Sym *def;
    296 	const Elf_Sym *symp;
    297 	const Obj_Entry *obj;
    298 	const Obj_Entry *defobj;
    299 	const Objlist_Entry *elm;
    300 	def = NULL;
    301 	defobj = NULL;
    302 
    303 	/* Look first in the referencing object if linked symbolically. */
    304 	if (refobj->symbolic) {
    305 		symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
    306 		if (symp != NULL) {
    307 			def = symp;
    308 			defobj = refobj;
    309 		}
    310 	}
    311 
    312 	/* Search all objects loaded at program start up. */
    313 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    314 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, in_plt);
    315 		if (symp != NULL &&
    316 		  (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    317 			def = symp;
    318 			defobj = obj;
    319 		}
    320 	}
    321 
    322 	/* Search all dlopened DAGs containing the referencing object. */
    323 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
    324 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    325 			break;
    326 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj,
    327 		    in_plt);
    328 		if (symp != NULL &&
    329 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    330 			def = symp;
    331 			defobj = obj;
    332 		}
    333 	}
    334 
    335 	/* Search all DAGs whose roots are RTLD_GLOBAL objects. */
    336 	SIMPLEQ_FOREACH(elm, &_rtld_list_global, link) {
    337 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    338 			break;
    339 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj,
    340 		    in_plt);
    341 		if (symp != NULL &&
    342 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    343 			def = symp;
    344 			defobj = obj;
    345 		}
    346 	}
    347 
    348 #ifdef notyet
    349 	/*
    350 	 * Search the dynamic linker itself, and possibly resolve the
    351 	 * symbol from there.  This is how the application links to
    352 	 * dynamic linker services such as dlopen.  Only the values listed
    353 	 * in the "exports" array can be resolved from the dynamic linker.
    354 	 */
    355 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    356 		symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
    357 		if (symp != NULL && is_exported(symp)) {
    358 			def = symp;
    359 			defobj = &_rtld_objself;
    360 		}
    361 	}
    362 #endif
    363 
    364 	if (def != NULL)
    365 		*defobj_out = defobj;
    366 	return def;
    367 }
    368