Home | History | Annotate | Line # | Download | only in ld.elf_so
symbol.c revision 1.17
      1 /*	$NetBSD: symbol.c,v 1.17 2002/09/06 03:12:05 mycroft Exp $	 */
      2 
      3 /*
      4  * Copyright 1996 John D. Polstra.
      5  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by John Polstra.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Dynamic linker for ELF.
     36  *
     37  * John Polstra <jdp (at) polstra.com>.
     38  */
     39 
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <fcntl.h>
     43 #include <stdarg.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 #include <sys/types.h>
     49 #include <sys/mman.h>
     50 #include <dirent.h>
     51 
     52 #include "debug.h"
     53 #include "rtld.h"
     54 
     55 /*
     56  * Hash function for symbol table lookup.  Don't even think about changing
     57  * this.  It is specified by the System V ABI.
     58  */
     59 unsigned long
     60 _rtld_elf_hash(name)
     61 	const char *name;
     62 {
     63 	const unsigned char *p = (const unsigned char *) name;
     64 	unsigned long   h = 0;
     65 	unsigned long   g;
     66 
     67 	while (*p != '\0') {
     68 		h = (h << 4) + *p++;
     69 		if ((g = h & 0xf0000000) != 0)
     70 			h ^= g >> 24;
     71 		h &= ~g;
     72 	}
     73 	return h;
     74 }
     75 
     76 const Elf_Sym *
     77 _rtld_symlook_list(const char *name, unsigned long hash, Objlist *objlist,
     78   const Obj_Entry **defobj_out, bool in_plt)
     79 {
     80 	const Elf_Sym *symp;
     81 	const Elf_Sym *def;
     82 	const Obj_Entry *defobj;
     83 	const Objlist_Entry *elm;
     84 
     85 	def = NULL;
     86 	defobj = NULL;
     87 	SIMPLEQ_FOREACH(elm, objlist, link) {
     88 		if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
     89 		    != NULL) {
     90 			if ((def == NULL) ||
     91 			    (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
     92 				def = symp;
     93 				defobj = elm->obj;
     94 				if (ELF_ST_BIND(def->st_info) != STB_WEAK)
     95 					break;
     96 			}
     97 		}
     98 	}
     99 	if (def != NULL)
    100 		*defobj_out = defobj;
    101 	return def;
    102 }
    103 
    104 /*
    105  * Search the symbol table of a single shared object for a symbol of
    106  * the given name.  Returns a pointer to the symbol, or NULL if no
    107  * definition was found.
    108  *
    109  * The symbol's hash value is passed in for efficiency reasons; that
    110  * eliminates many recomputations of the hash value.
    111  */
    112 const Elf_Sym *
    113 _rtld_symlook_obj(name, hash, obj, in_plt)
    114 	const char *name;
    115 	unsigned long hash;
    116 	const Obj_Entry *obj;
    117 	bool in_plt;
    118 {
    119 	unsigned long symnum = obj->buckets[hash % obj->nbuckets];
    120 
    121 	while (symnum != ELF_SYM_UNDEFINED) {
    122 		const Elf_Sym  *symp;
    123 		const char     *strp;
    124 
    125 		assert(symnum < obj->nchains);
    126 		symp = obj->symtab + symnum;
    127 		strp = obj->strtab + symp->st_name;
    128 #if 0
    129 		assert(symp->st_name != 0);
    130 #endif
    131 		if (strcmp(name, strp) == 0) {
    132 			if (symp->st_shndx != SHN_UNDEF
    133 #if !defined(__mips__)		/* Following doesn't work on MIPS? mhitch */
    134 			    || (!in_plt && symp->st_value != 0 &&
    135 			        ELF_ST_TYPE(symp->st_info) == STT_FUNC)
    136 #endif
    137 				) {
    138 				return symp;
    139 			}
    140 		}
    141 		symnum = obj->chains[symnum];
    142 	}
    143 
    144 	return NULL;
    145 }
    146 
    147 /*
    148  * Given a symbol number in a referencing object, find the corresponding
    149  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    150  * no definition was found.  Returns a pointer to the Obj_Entry of the
    151  * defining object via the reference parameter DEFOBJ_OUT.
    152  */
    153 const Elf_Sym *
    154 _rtld_find_symdef(symnum, refobj, defobj_out, in_plt)
    155 	unsigned long symnum;
    156 	const Obj_Entry *refobj;
    157 	const Obj_Entry **defobj_out;
    158 	bool in_plt;
    159 {
    160 	const Elf_Sym  *ref;
    161 	const Elf_Sym  *def;
    162 	const Elf_Sym  *symp;
    163 	const Obj_Entry *obj;
    164 	const Obj_Entry *defobj;
    165 	const Objlist_Entry *elm;
    166 	const char     *name;
    167 	unsigned long   hash;
    168 
    169 	ref = refobj->symtab + symnum;
    170 	name = refobj->strtab + ref->st_name;
    171 
    172 	hash = _rtld_elf_hash(name);
    173 	def = NULL;
    174 	defobj = NULL;
    175 
    176 	if (refobj->symbolic) {	/* Look first in the referencing object */
    177 		symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
    178 		if (symp != NULL) {
    179 			def = symp;
    180 			defobj = refobj;
    181 		}
    182 	}
    183 
    184 	/* Search all objects loaded at program start up. */
    185 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    186 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj, in_plt);
    187 		if (symp != NULL &&
    188 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    189 			def = symp;
    190 			defobj = obj;
    191 		}
    192 	}
    193 
    194 	/* Search all dlopened DAGs containing the referencing object. */
    195 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
    196 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    197 			break;
    198 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers, &obj,
    199 					  in_plt);
    200 		if (symp != NULL &&
    201 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    202 			def = symp;
    203 			defobj = obj;
    204 		}
    205 	}
    206 
    207 	/* Search all RTLD_GLOBAL objects. */
    208 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    209 		symp = _rtld_symlook_list(name, hash, &_rtld_list_global, &obj, in_plt);
    210 		if (symp != NULL &&
    211 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    212 			def = symp;
    213 			defobj = obj;
    214 		}
    215 	}
    216 
    217 	/*
    218 	 * If we found no definition and the reference is weak, treat the
    219 	 * symbol as having the value zero.
    220 	 */
    221 	if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
    222 		rdbg(1, ("  returning _rtld_sym_zero@_rtld_objmain"));
    223 		def = &_rtld_sym_zero;
    224 		defobj = _rtld_objmain;
    225 	}
    226 
    227 	if (def != NULL)
    228 		*defobj_out = defobj;
    229 	else
    230 		_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
    231 		    refobj->path, in_plt ? "PLT " : "", name, symnum);
    232 	return def;
    233 }
    234