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