Home | History | Annotate | Line # | Download | only in ld.elf_so
symbol.c revision 1.4
      1 /*	$NetBSD: symbol.c,v 1.4 1999/10/25 13:57:12 kleink 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 /*
     77  * Search the symbol table of a single shared object for a symbol of
     78  * the given name.  Returns a pointer to the symbol, or NULL if no
     79  * definition was found.
     80  *
     81  * The symbol's hash value is passed in for efficiency reasons; that
     82  * eliminates many recomputations of the hash value.
     83  */
     84 const Elf_Sym *
     85 _rtld_symlook_obj(name, hash, obj, in_plt)
     86 	const char *name;
     87 	unsigned long hash;
     88 	const Obj_Entry *obj;
     89 	bool in_plt;
     90 {
     91 	unsigned long symnum = obj->buckets[hash % obj->nbuckets];
     92 
     93 	while (symnum != ELF_SYM_UNDEFINED) {
     94 		const Elf_Sym  *symp;
     95 		const char     *strp;
     96 
     97 		assert(symnum < obj->nchains);
     98 		symp = obj->symtab + symnum;
     99 		strp = obj->strtab + symp->st_name;
    100 #if 0
    101 		assert(symp->st_name != 0);
    102 #endif
    103 		if (strcmp(name, strp) == 0) {
    104 			if (symp->st_shndx != SHN_UNDEF
    105 #if !defined(__mips__)		/* Following doesn't work on MIPS? mhitch */
    106 			    || (!in_plt && symp->st_value != 0 &&
    107 			    ELFDEFNNAME(ST_TYPE)(symp->st_info) == STT_FUNC)) {
    108 #else
    109 				) {
    110 #endif
    111 				return symp;
    112 			}
    113 		}
    114 		symnum = obj->chains[symnum];
    115 	}
    116 
    117 	return NULL;
    118 }
    119 
    120 /*
    121  * Given a symbol number in a referencing object, find the corresponding
    122  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    123  * no definition was found.  Returns a pointer to the Obj_Entry of the
    124  * defining object via the reference parameter DEFOBJ_OUT.
    125  */
    126 const Elf_Sym *
    127 _rtld_find_symdef(obj_list, r_info, name, refobj, defobj_out, in_plt)
    128 	const Obj_Entry *obj_list;
    129 	Elf_Word r_info;
    130 	const char *name;
    131 	const Obj_Entry *refobj;
    132 	const Obj_Entry **defobj_out;
    133 	bool in_plt;
    134 {
    135 	Elf_Word symnum = ELF_R_SYM(r_info);
    136 	const Elf_Sym  *ref;
    137 	const Obj_Entry *obj;
    138 	unsigned long   hash;
    139 
    140 	if (name == NULL) {
    141 		ref = refobj->symtab + symnum;
    142 		name = refobj->strtab + ref->st_name;
    143 	}
    144 	hash = _rtld_elf_hash(name);
    145 
    146 	if (refobj->symbolic) {	/* Look first in the referencing object */
    147 		const Elf_Sym *def;
    148 
    149 		def = _rtld_symlook_obj(name, hash, refobj, in_plt);
    150 		if (def != NULL) {
    151 			*defobj_out = refobj;
    152 			return def;
    153 		}
    154 	}
    155 	/*
    156          * Look in all loaded objects.  Skip the referencing object, if
    157          * we have already searched it.
    158          */
    159 	for (obj = obj_list; obj != NULL; obj = obj->next) {
    160 		if (obj != refobj || !refobj->symbolic) {
    161 			const Elf_Sym *def;
    162 
    163 			def = _rtld_symlook_obj(name, hash, obj, in_plt);
    164 			if (def != NULL) {
    165 				*defobj_out = obj;
    166 				return def;
    167 			}
    168 		}
    169 	}
    170 
    171 	if (ELF_R_TYPE(r_info) != R_TYPE(NONE)) {
    172 		_rtld_error(
    173 	    "%s: Undefined %ssymbol \"%s\" (reloc type = %d, symnum = %d)",
    174 		    refobj->path, in_plt ? "PLT " : "", name,
    175 		    ELF_R_TYPE(r_info), symnum);
    176 	}
    177 	return NULL;
    178 }
    179