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