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