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