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