Home | History | Annotate | Line # | Download | only in ld.elf_so
symbol.c revision 1.73
      1  1.73     kamil /*	$NetBSD: symbol.c,v 1.73 2020/02/29 18:45:20 kamil 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.37     skrll #include <sys/cdefs.h>
     42  1.37     skrll #ifndef lint
     43  1.73     kamil __RCSID("$NetBSD: symbol.c,v 1.73 2020/02/29 18:45:20 kamil Exp $");
     44  1.37     skrll #endif /* not lint */
     45  1.37     skrll 
     46   1.1       cgd #include <err.h>
     47   1.1       cgd #include <errno.h>
     48   1.1       cgd #include <fcntl.h>
     49   1.1       cgd #include <stdarg.h>
     50   1.1       cgd #include <stdio.h>
     51   1.1       cgd #include <stdlib.h>
     52   1.1       cgd #include <string.h>
     53   1.1       cgd #include <unistd.h>
     54   1.1       cgd #include <sys/types.h>
     55   1.1       cgd #include <sys/mman.h>
     56  1.53     joerg #include <sys/bitops.h>
     57   1.1       cgd #include <dirent.h>
     58   1.1       cgd 
     59   1.1       cgd #include "debug.h"
     60   1.1       cgd #include "rtld.h"
     61   1.1       cgd 
     62  1.51       roy /*
     63  1.51       roy  * If the given object is already in the donelist, return true.  Otherwise
     64  1.51       roy  * add the object to the list and return false.
     65  1.51       roy  */
     66  1.51       roy static bool
     67  1.51       roy _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
     68  1.51       roy {
     69  1.51       roy 	unsigned int i;
     70  1.51       roy 
     71  1.51       roy 	for (i = 0;  i < dlp->num_used;  i++)
     72  1.51       roy 		if (dlp->objs[i] == obj)
     73  1.51       roy 			return true;
     74  1.51       roy 	/*
     75  1.51       roy 	 * Our donelist allocation may not always be sufficient as we're not
     76  1.51       roy 	 * thread safe. We'll handle it properly anyway.
     77  1.51       roy 	 */
     78  1.51       roy 	if (dlp->num_used < dlp->num_alloc)
     79  1.51       roy 		dlp->objs[dlp->num_used++] = obj;
     80  1.51       roy 	return false;
     81  1.51       roy }
     82  1.51       roy 
     83   1.1       cgd /*
     84   1.1       cgd  * Hash function for symbol table lookup.  Don't even think about changing
     85   1.1       cgd  * this.  It is specified by the System V ABI.
     86   1.1       cgd  */
     87   1.1       cgd unsigned long
     88  1.71     kamil _rtld_sysv_hash(const char *name)
     89   1.1       cgd {
     90   1.3  christos 	const unsigned char *p = (const unsigned char *) name;
     91   1.3  christos 	unsigned long   h = 0;
     92   1.3  christos 	unsigned long   g;
     93  1.24   mycroft 	unsigned long   c;
     94   1.3  christos 
     95  1.24   mycroft 	for (; __predict_true((c = *p) != '\0'); p++) {
     96  1.24   mycroft 		h <<= 4;
     97  1.24   mycroft 		h += c;
     98  1.24   mycroft 		if ((g = h & 0xf0000000) != 0) {
     99  1.24   mycroft 			h ^= g;
    100   1.3  christos 			h ^= g >> 24;
    101  1.24   mycroft 		}
    102   1.3  christos 	}
    103  1.24   mycroft 	return (h);
    104   1.1       cgd }
    105   1.1       cgd 
    106  1.71     kamil /*
    107  1.71     kamil  * Hash function for symbol table lookup.  Don't even think about changing
    108  1.71     kamil  * this.  It is specified by the GNU toolchain ABI.
    109  1.71     kamil  */
    110  1.71     kamil unsigned long
    111  1.71     kamil _rtld_gnu_hash(const char *name)
    112  1.71     kamil {
    113  1.71     kamil 	const unsigned char *p = (const unsigned char *) name;
    114  1.71     kamil 	uint_fast32_t h = 5381;
    115  1.71     kamil 	unsigned char c;
    116  1.71     kamil 
    117  1.71     kamil 	for (c = *p; c != '\0'; c = *++p)
    118  1.71     kamil 		h = h * 33 + c;
    119  1.71     kamil 	return (unsigned long)h;
    120  1.71     kamil }
    121  1.71     kamil 
    122   1.5   mycroft const Elf_Sym *
    123  1.71     kamil _rtld_symlook_list(const char *name, Elf_Hash *hash, const Objlist *objlist,
    124  1.57    nonaka     const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry,
    125  1.57    nonaka     DoneList *dlp)
    126   1.5   mycroft {
    127   1.5   mycroft 	const Elf_Sym *symp;
    128   1.5   mycroft 	const Elf_Sym *def;
    129  1.27   mycroft 	const Obj_Entry *defobj;
    130   1.5   mycroft 	const Objlist_Entry *elm;
    131  1.63     skrll 
    132   1.5   mycroft 	def = NULL;
    133  1.27   mycroft 	defobj = NULL;
    134  1.12     lukem 	SIMPLEQ_FOREACH(elm, objlist, link) {
    135  1.51       roy 		if (_rtld_donelist_check(dlp, elm->obj))
    136  1.51       roy 			continue;
    137  1.43  christos 		rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
    138  1.43  christos 		    name));
    139  1.57    nonaka 		symp = _rtld_symlook_obj(name, hash, elm->obj, flags, ventry);
    140  1.57    nonaka 		if (symp != NULL) {
    141   1.5   mycroft 			if ((def == NULL) ||
    142   1.6   thorpej 			    (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    143   1.5   mycroft 				def = symp;
    144  1.27   mycroft 				defobj = elm->obj;
    145   1.6   thorpej 				if (ELF_ST_BIND(def->st_info) != STB_WEAK)
    146   1.5   mycroft 					break;
    147   1.5   mycroft 			}
    148   1.5   mycroft 		}
    149   1.5   mycroft 	}
    150  1.27   mycroft 	if (def != NULL)
    151  1.27   mycroft 		*defobj_out = defobj;
    152   1.5   mycroft 	return def;
    153   1.5   mycroft }
    154   1.5   mycroft 
    155   1.1       cgd /*
    156  1.47     skrll  * Search the symbol table of a shared object and all objects needed by it for
    157  1.47     skrll  * a symbol of the given name. Search order is breadth-first. Returns a pointer
    158  1.47     skrll  * to the symbol, or NULL if no definition was found.
    159  1.47     skrll  */
    160  1.47     skrll const Elf_Sym *
    161  1.71     kamil _rtld_symlook_needed(const char *name, Elf_Hash *hash,
    162  1.57    nonaka     const Needed_Entry *needed, const Obj_Entry **defobj_out, u_int flags,
    163  1.57    nonaka     const Ver_Entry *ventry, DoneList *breadth, DoneList *depth)
    164  1.47     skrll {
    165  1.47     skrll 	const Elf_Sym *def, *def_w;
    166  1.47     skrll 	const Needed_Entry *n;
    167  1.47     skrll 	const Obj_Entry *obj, *defobj, *defobj1;
    168  1.47     skrll 
    169  1.47     skrll 	def = def_w = NULL;
    170  1.47     skrll 	defobj = NULL;
    171  1.47     skrll 	for (n = needed; n != NULL; n = n->next) {
    172  1.51       roy 		if ((obj = n->obj) == NULL)
    173  1.51       roy 			continue;
    174  1.51       roy 		if (_rtld_donelist_check(breadth, obj))
    175  1.51       roy 			continue;
    176  1.57    nonaka 		def = _rtld_symlook_obj(name, hash, obj, flags, ventry);
    177  1.57    nonaka 		if (def == NULL)
    178  1.47     skrll 			continue;
    179  1.47     skrll 		defobj = obj;
    180  1.47     skrll 		if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
    181  1.47     skrll 			*defobj_out = defobj;
    182  1.47     skrll 
    183  1.47     skrll 			return (def);
    184  1.47     skrll 		}
    185  1.47     skrll 	}
    186  1.47     skrll 	/*
    187  1.47     skrll 	 * Either the symbol definition has not been found in directly needed
    188  1.47     skrll 	 * objects, or the found symbol is weak.
    189  1.47     skrll 	 */
    190  1.47     skrll 	for (n = needed; n != NULL; n = n->next) {
    191  1.47     skrll 		if ((obj = n->obj) == NULL)
    192  1.47     skrll 			continue;
    193  1.51       roy 		if (_rtld_donelist_check(depth, obj))
    194  1.51       roy 			continue;
    195  1.47     skrll 		def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
    196  1.57    nonaka 		    flags, ventry, breadth, depth);
    197  1.47     skrll 		if (def_w == NULL)
    198  1.47     skrll 			continue;
    199  1.47     skrll 		if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
    200  1.47     skrll 			def = def_w;
    201  1.47     skrll 			defobj = defobj1;
    202  1.47     skrll 			if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
    203  1.47     skrll 				break;
    204  1.47     skrll 		}
    205  1.47     skrll 	}
    206  1.47     skrll 	if (def != NULL)
    207  1.47     skrll 		*defobj_out = defobj;
    208  1.47     skrll 
    209  1.47     skrll 	return def;
    210  1.47     skrll }
    211  1.47     skrll 
    212  1.70     kamil static bool
    213  1.70     kamil _rtld_symlook_obj_matched_symbol(const char *name,
    214  1.70     kamil     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry,
    215  1.70     kamil     unsigned long symnum, const Elf_Sym **vsymp, int *vcount)
    216  1.70     kamil {
    217  1.70     kamil 	const Elf_Sym  *symp;
    218  1.70     kamil 	const char     *strp;
    219  1.70     kamil 	Elf_Half verndx;
    220  1.70     kamil 
    221  1.70     kamil 	symp = obj->symtab + symnum;
    222  1.70     kamil 	strp = obj->strtab + symp->st_name;
    223  1.70     kamil 	rdbg(("check \"%s\" vs \"%s\" in %s", name, strp, obj->path));
    224  1.70     kamil 	if (name[1] != strp[1] || strcmp(name, strp))
    225  1.70     kamil 		return false;
    226  1.70     kamil #if defined(__mips__) || defined(__vax__)
    227  1.70     kamil 	if (symp->st_shndx == SHN_UNDEF)
    228  1.73     kamil 		return false;
    229  1.70     kamil #else
    230  1.70     kamil 	/*
    231  1.70     kamil 	 * XXX DANGER WILL ROBINSON!
    232  1.70     kamil 	 * If we have a function pointer in the executable's
    233  1.70     kamil 	 * data section, it points to the executable's PLT
    234  1.70     kamil 	 * slot, and there is NO relocation emitted.  To make
    235  1.70     kamil 	 * the function pointer comparable to function pointers
    236  1.70     kamil 	 * in shared libraries, we must resolve data references
    237  1.70     kamil 	 * in the libraries to point to PLT slots in the
    238  1.70     kamil 	 * executable, if they exist.
    239  1.70     kamil 	 */
    240  1.70     kamil 	if (symp->st_shndx == SHN_UNDEF &&
    241  1.70     kamil 	    ((flags & SYMLOOK_IN_PLT) ||
    242  1.70     kamil 	    symp->st_value == 0 ||
    243  1.70     kamil 	    ELF_ST_TYPE(symp->st_info) != STT_FUNC))
    244  1.70     kamil 		return false;
    245  1.70     kamil #endif
    246  1.70     kamil 
    247  1.70     kamil 	if (ventry == NULL) {
    248  1.70     kamil 		if (obj->versyms != NULL) {
    249  1.70     kamil 			verndx = VER_NDX(obj->versyms[symnum].vs_vers);
    250  1.70     kamil 			if (verndx > obj->vertabnum) {
    251  1.70     kamil 				_rtld_error("%s: symbol %s references "
    252  1.70     kamil 				    "wrong version %d", obj->path,
    253  1.70     kamil 				    &obj->strtab[symnum], verndx);
    254  1.70     kamil 				return false;
    255  1.70     kamil 			}
    256  1.70     kamil 
    257  1.70     kamil 			/*
    258  1.70     kamil 			 * If we are not called from dlsym (i.e. this
    259  1.70     kamil 			 * is a normal relocation from unversioned
    260  1.70     kamil 			 * binary), accept the symbol immediately
    261  1.70     kamil 			 * if it happens to have first version after
    262  1.70     kamil 			 * this shared object became versioned.
    263  1.70     kamil 			 * Otherwise, if symbol is versioned and not
    264  1.70     kamil 			 * hidden, remember it. If it is the only
    265  1.70     kamil 			 * symbol with this name exported by the shared
    266  1.70     kamil 			 * object, it will be returned as a match at the
    267  1.70     kamil 			 * end of the function. If symbol is global
    268  1.70     kamil 			 * (verndx < 2) accept it unconditionally.
    269  1.70     kamil 			 */
    270  1.70     kamil 			if (!(flags & SYMLOOK_DLSYM) &&
    271  1.70     kamil 			    verndx == VER_NDX_GIVEN) {
    272  1.70     kamil 				*vsymp = symp;
    273  1.70     kamil 				return true;
    274  1.70     kamil 			} else if (verndx >= VER_NDX_GIVEN) {
    275  1.70     kamil 				if (!(obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN)) {
    276  1.70     kamil 					if (*vsymp == NULL)
    277  1.70     kamil 						*vsymp = symp;
    278  1.70     kamil 					(*vcount)++;
    279  1.70     kamil 				}
    280  1.70     kamil 				return false;
    281  1.70     kamil 			}
    282  1.70     kamil 		}
    283  1.70     kamil 		*vsymp = symp;
    284  1.70     kamil 		return true;
    285  1.70     kamil 	} else {
    286  1.70     kamil 		if (obj->versyms == NULL) {
    287  1.70     kamil 			if (_rtld_object_match_name(obj, ventry->name)){
    288  1.70     kamil 				_rtld_error("%s: object %s should "
    289  1.70     kamil 				    "provide version %s for symbol %s",
    290  1.70     kamil 				    _rtld_objself.path, obj->path,
    291  1.70     kamil 				    ventry->name, &obj->strtab[symnum]);
    292  1.70     kamil 				return false;
    293  1.70     kamil 			}
    294  1.70     kamil 		} else {
    295  1.70     kamil 			verndx = VER_NDX(obj->versyms[symnum].vs_vers);
    296  1.70     kamil 			if (verndx > obj->vertabnum) {
    297  1.70     kamil 				_rtld_error("%s: symbol %s references "
    298  1.70     kamil 				    "wrong version %d", obj->path,
    299  1.70     kamil 				    &obj->strtab[symnum], verndx);
    300  1.70     kamil 				return false;
    301  1.70     kamil 			}
    302  1.70     kamil 			if (obj->vertab[verndx].hash != ventry->hash ||
    303  1.70     kamil 			    strcmp(obj->vertab[verndx].name, ventry->name)) {
    304  1.70     kamil 				/*
    305  1.70     kamil 				* Version does not match. Look if this
    306  1.70     kamil 				* is a global symbol and if it is not
    307  1.70     kamil 				* hidden. If global symbol (verndx < 2)
    308  1.70     kamil 				* is available, use it. Do not return
    309  1.70     kamil 				* symbol if we are called by dlvsym,
    310  1.70     kamil 				* because dlvsym looks for a specific
    311  1.70     kamil 				* version and default one is not what
    312  1.70     kamil 				* dlvsym wants.
    313  1.70     kamil 				*/
    314  1.70     kamil 				if ((flags & SYMLOOK_DLSYM) ||
    315  1.70     kamil 				    (obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN) ||
    316  1.70     kamil 				    (verndx >= VER_NDX_GIVEN))
    317  1.70     kamil 					return false;
    318  1.70     kamil 			}
    319  1.70     kamil 		}
    320  1.70     kamil 		*vsymp = symp;
    321  1.70     kamil 		return true;
    322  1.70     kamil 	}
    323  1.70     kamil }
    324  1.70     kamil 
    325  1.47     skrll /*
    326   1.1       cgd  * Search the symbol table of a single shared object for a symbol of
    327   1.1       cgd  * the given name.  Returns a pointer to the symbol, or NULL if no
    328   1.1       cgd  * definition was found.
    329   1.1       cgd  *
    330  1.72     kamil  * SysV Hash version.
    331   1.1       cgd  */
    332  1.72     kamil static const Elf_Sym *
    333  1.72     kamil _rtld_symlook_obj_sysv(const char *name, unsigned long hash,
    334  1.57    nonaka     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
    335   1.1       cgd {
    336  1.20   mycroft 	unsigned long symnum;
    337  1.57    nonaka 	const Elf_Sym *vsymp = NULL;
    338  1.57    nonaka 	int vcount = 0;
    339   1.1       cgd 
    340  1.72     kamil 	for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
    341  1.53     joerg 	     obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
    342  1.20   mycroft 	     symnum != ELF_SYM_UNDEFINED;
    343  1.20   mycroft 	     symnum = obj->chains[symnum]) {
    344   1.3  christos 		assert(symnum < obj->nchains);
    345  1.57    nonaka 
    346  1.70     kamil 		if (_rtld_symlook_obj_matched_symbol(name, obj, flags,
    347  1.70     kamil 		    ventry, symnum, &vsymp, &vcount)) {
    348  1.70     kamil 			return vsymp;
    349   1.3  christos 		}
    350   1.1       cgd 	}
    351  1.57    nonaka 	if (vcount == 1)
    352  1.57    nonaka 		return vsymp;
    353   1.3  christos 	return NULL;
    354   1.1       cgd }
    355   1.1       cgd 
    356   1.1       cgd /*
    357  1.72     kamil  * Search the symbol table of a single shared object for a symbol of
    358  1.72     kamil  * the given name.  Returns a pointer to the symbol, or NULL if no
    359  1.72     kamil  * definition was found.
    360  1.72     kamil  *
    361  1.72     kamil  * GNU Hash version.
    362  1.72     kamil  */
    363  1.72     kamil static const Elf_Sym *
    364  1.72     kamil _rtld_symlook_obj_gnu(const char *name, unsigned long hash,
    365  1.72     kamil     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
    366  1.72     kamil {
    367  1.72     kamil 	unsigned long symnum;
    368  1.72     kamil 	const Elf_Sym *vsymp = NULL;
    369  1.72     kamil 	const Elf32_Word *hashval;
    370  1.72     kamil 	Elf_Addr bloom_word;
    371  1.72     kamil 	Elf32_Word bucket;
    372  1.72     kamil 	int vcount = 0;
    373  1.72     kamil 	unsigned int h1, h2;
    374  1.72     kamil 
    375  1.72     kamil 	/* Pick right bitmask word from Bloom filter array */
    376  1.72     kamil 	bloom_word = obj->bloom_gnu[(hash / ELFSIZE) & obj->mask_bm_gnu];
    377  1.72     kamil 
    378  1.72     kamil 	/* Calculate modulus word size of gnu hash and its derivative */
    379  1.72     kamil 	h1 = hash & (ELFSIZE - 1);
    380  1.72     kamil 	h2 = ((hash >> obj->shift2_gnu) & (ELFSIZE - 1));
    381  1.72     kamil 
    382  1.72     kamil 	/* Filter out the "definitely not in set" queries */
    383  1.72     kamil 	if (((bloom_word >> h1) & (bloom_word >> h2) & 1) == 0)
    384  1.72     kamil 		return NULL;
    385  1.72     kamil 
    386  1.72     kamil 	/* Locate hash chain and corresponding value element*/
    387  1.72     kamil 	bucket = obj->buckets_gnu[fast_remainder32(hash, obj->nbuckets_gnu,
    388  1.72     kamil 	     obj->nbuckets_m_gnu, obj->nbuckets_s1_gnu, obj->nbuckets_s2_gnu)];
    389  1.72     kamil 	if (bucket == 0)
    390  1.72     kamil 		return NULL;
    391  1.72     kamil 
    392  1.72     kamil 	hashval = &obj->chains_gnu[bucket];
    393  1.72     kamil 	do {
    394  1.72     kamil 		if (((*hashval ^ hash) >> 1) == 0) {
    395  1.72     kamil 			symnum = hashval - obj->chains_gnu;
    396  1.72     kamil 
    397  1.72     kamil 			if (_rtld_symlook_obj_matched_symbol(name, obj, flags,
    398  1.72     kamil 			    ventry, symnum, &vsymp, &vcount)) {
    399  1.72     kamil 				return vsymp;
    400  1.72     kamil 			}
    401  1.72     kamil 		}
    402  1.72     kamil 	} while ((*hashval++ & 1) == 0);
    403  1.72     kamil 	if (vcount == 1)
    404  1.72     kamil 		return vsymp;
    405  1.72     kamil 	return NULL;
    406  1.72     kamil }
    407  1.72     kamil 
    408  1.72     kamil /*
    409  1.72     kamil  * Search the symbol table of a single shared object for a symbol of
    410  1.72     kamil  * the given name.  Returns a pointer to the symbol, or NULL if no
    411  1.72     kamil  * definition was found.
    412  1.72     kamil  *
    413  1.72     kamil  * The symbol's hash value is passed in for efficiency reasons; that
    414  1.72     kamil  * eliminates many recomputations of the hash value.
    415  1.72     kamil  *
    416  1.72     kamil  * Redirect to either GNU Hash (whenever available) or ELF Hash.
    417  1.72     kamil  */
    418  1.72     kamil const Elf_Sym *
    419  1.72     kamil _rtld_symlook_obj(const char *name, Elf_Hash *hash,
    420  1.72     kamil     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
    421  1.72     kamil {
    422  1.72     kamil 
    423  1.72     kamil 	assert(obj->sysv_hash || obj->gnu_hash);
    424  1.72     kamil 
    425  1.72     kamil 	/* Always prefer the GNU Hash as it is faster. */
    426  1.72     kamil 	if (obj->gnu_hash)
    427  1.72     kamil 		return _rtld_symlook_obj_gnu(name, hash->gnu, obj, flags, ventry);
    428  1.72     kamil 	else
    429  1.72     kamil 		return _rtld_symlook_obj_sysv(name, hash->sysv, obj, flags, ventry);
    430  1.72     kamil }
    431  1.72     kamil 
    432  1.72     kamil /*
    433   1.1       cgd  * Given a symbol number in a referencing object, find the corresponding
    434   1.1       cgd  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    435   1.1       cgd  * no definition was found.  Returns a pointer to the Obj_Entry of the
    436   1.1       cgd  * defining object via the reference parameter DEFOBJ_OUT.
    437   1.1       cgd  */
    438   1.1       cgd const Elf_Sym *
    439  1.31     skrll _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
    440  1.57    nonaka     const Obj_Entry **defobj_out, u_int flags)
    441   1.1       cgd {
    442  1.27   mycroft 	const Elf_Sym  *ref;
    443   1.5   mycroft 	const Elf_Sym  *def;
    444  1.27   mycroft 	const Obj_Entry *defobj;
    445  1.27   mycroft 	const char     *name;
    446  1.71     kamil 	Elf_Hash        hash;
    447   1.3  christos 
    448  1.27   mycroft 	ref = refobj->symtab + symnum;
    449  1.27   mycroft 	name = refobj->strtab + ref->st_name;
    450  1.27   mycroft 
    451  1.27   mycroft 	/*
    452  1.40     skrll 	 * We don't have to do a full scale lookup if the symbol is local.
    453  1.40     skrll 	 * We know it will bind to the instance in this load module; to
    454  1.40     skrll 	 * which we already have a pointer (ie ref).
    455  1.33     skrll 	 */
    456  1.40     skrll 	if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
    457  1.40     skrll 		if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
    458  1.40     skrll 			_rtld_error("%s: Bogus symbol table entry %lu",
    459  1.40     skrll 			    refobj->path, symnum);
    460  1.40     skrll         	}
    461  1.40     skrll 
    462  1.71     kamil 		hash.sysv = _rtld_sysv_hash(name);
    463  1.71     kamil 		hash.gnu = _rtld_gnu_hash(name);
    464  1.40     skrll 		defobj = NULL;
    465  1.71     kamil 		def = _rtld_symlook_default(name, &hash, refobj, &defobj, flags,
    466  1.57    nonaka 		    _rtld_fetch_ventry(refobj, symnum));
    467  1.40     skrll 	} else {
    468  1.40     skrll 		rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
    469  1.40     skrll 		def = ref;
    470  1.40     skrll 		defobj = refobj;
    471  1.33     skrll 	}
    472  1.63     skrll 
    473  1.33     skrll 	/*
    474  1.27   mycroft 	 * If we found no definition and the reference is weak, treat the
    475  1.27   mycroft 	 * symbol as having the value zero.
    476  1.27   mycroft 	 */
    477  1.27   mycroft 	if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
    478  1.45  christos 		rdbg(("  returning _rtld_sym_zero@_rtld_objself"));
    479  1.27   mycroft 		def = &_rtld_sym_zero;
    480  1.45  christos 		defobj = &_rtld_objself;
    481  1.27   mycroft 	}
    482  1.33     skrll 
    483  1.41      matt 	if (def != NULL) {
    484  1.27   mycroft 		*defobj_out = defobj;
    485  1.41      matt 	} else {
    486  1.27   mycroft 		rdbg(("lookup failed"));
    487  1.27   mycroft 		_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
    488  1.57    nonaka 		    refobj->path, (flags & SYMLOOK_IN_PLT) ? "PLT " : "",
    489  1.57    nonaka 		    name, symnum);
    490  1.27   mycroft 	}
    491  1.26   mycroft 	return def;
    492  1.28  christos }
    493  1.28  christos 
    494  1.50  christos const Elf_Sym *
    495  1.50  christos _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
    496  1.50  christos     const Obj_Entry **defobj, bool imm)
    497  1.50  christos {
    498  1.57    nonaka  	const Elf_Sym  *def = _rtld_find_symdef(symnum, obj, defobj,
    499  1.57    nonaka 	    SYMLOOK_IN_PLT);
    500  1.50  christos 	if (__predict_false(def == NULL))
    501  1.50  christos  		return NULL;
    502  1.50  christos 
    503  1.50  christos 	if (__predict_false(def == &_rtld_sym_zero)) {
    504  1.50  christos 		/* tp is set during lazy binding. */
    505  1.50  christos 		if (imm) {
    506  1.50  christos 			const Elf_Sym	*ref = obj->symtab + symnum;
    507  1.50  christos 			const char	*name = obj->strtab + ref->st_name;
    508  1.50  christos 
    509  1.50  christos 			_rtld_error(
    510  1.50  christos 			    "%s: Trying to call undefined weak symbol `%s'",
    511  1.50  christos 			    obj->path, name);
    512  1.50  christos 			return NULL;
    513  1.50  christos 		}
    514  1.50  christos 	}
    515  1.50  christos 	return def;
    516  1.50  christos }
    517  1.50  christos 
    518  1.28  christos /*
    519  1.28  christos  * Given a symbol name in a referencing object, find the corresponding
    520  1.28  christos  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    521  1.28  christos  * no definition was found.  Returns a pointer to the Obj_Entry of the
    522  1.28  christos  * defining object via the reference parameter DEFOBJ_OUT.
    523  1.28  christos  */
    524  1.28  christos const Elf_Sym *
    525  1.71     kamil _rtld_symlook_default(const char *name, Elf_Hash *hash,
    526  1.57    nonaka     const Obj_Entry *refobj, const Obj_Entry **defobj_out, u_int flags,
    527  1.57    nonaka     const Ver_Entry *ventry)
    528  1.28  christos {
    529  1.29     skrll 	const Elf_Sym *def;
    530  1.29     skrll 	const Elf_Sym *symp;
    531  1.29     skrll 	const Obj_Entry *obj;
    532  1.29     skrll 	const Obj_Entry *defobj;
    533  1.29     skrll 	const Objlist_Entry *elm;
    534  1.29     skrll 	def = NULL;
    535  1.29     skrll 	defobj = NULL;
    536  1.51       roy 	DoneList donelist;
    537  1.51       roy 
    538  1.51       roy 	_rtld_donelist_init(&donelist);
    539  1.29     skrll 
    540  1.29     skrll 	/* Look first in the referencing object if linked symbolically. */
    541  1.51       roy 	if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
    542  1.43  christos 		rdbg(("search referencing object for %s", name));
    543  1.57    nonaka 		symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
    544  1.29     skrll 		if (symp != NULL) {
    545  1.29     skrll 			def = symp;
    546  1.29     skrll 			defobj = refobj;
    547  1.29     skrll 		}
    548  1.29     skrll 	}
    549  1.29     skrll 
    550  1.29     skrll 	/* Search all objects loaded at program start up. */
    551  1.29     skrll 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    552  1.43  christos 		rdbg(("search _rtld_list_main for %s", name));
    553  1.40     skrll 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
    554  1.57    nonaka 		    flags, ventry, &donelist);
    555  1.29     skrll 		if (symp != NULL &&
    556  1.40     skrll 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    557  1.29     skrll 			def = symp;
    558  1.29     skrll 			defobj = obj;
    559  1.29     skrll 		}
    560  1.29     skrll 	}
    561  1.29     skrll 
    562  1.40     skrll 	/* Search all RTLD_GLOBAL objects. */
    563  1.40     skrll 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    564  1.43  christos 		rdbg(("search _rtld_list_global for %s", name));
    565  1.40     skrll 		symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
    566  1.57    nonaka 		    &obj, flags, ventry, &donelist);
    567  1.29     skrll 		if (symp != NULL &&
    568  1.29     skrll 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    569  1.29     skrll 			def = symp;
    570  1.29     skrll 			defobj = obj;
    571  1.29     skrll 		}
    572  1.29     skrll 	}
    573  1.63     skrll 
    574  1.40     skrll 	/* Search all dlopened DAGs containing the referencing object. */
    575  1.40     skrll 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
    576  1.29     skrll 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    577  1.29     skrll 			break;
    578  1.43  christos 		rdbg(("search DAG with root %p (%s) for %s", elm->obj,
    579  1.43  christos 		    elm->obj->path, name));
    580  1.40     skrll 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
    581  1.57    nonaka 		    &obj, flags, ventry, &donelist);
    582  1.29     skrll 		if (symp != NULL &&
    583  1.29     skrll 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    584  1.29     skrll 			def = symp;
    585  1.29     skrll 			defobj = obj;
    586  1.29     skrll 		}
    587  1.28  christos 	}
    588  1.28  christos 
    589  1.29     skrll 	/*
    590  1.69     joerg 	 * Finally, look in the referencing object if not linked symbolically.
    591  1.69     joerg 	 * This is necessary for DF_1_NODELETE objects where the containing DAG
    592  1.69     joerg 	 * has been unlinked, so local references are resolved properly.
    593  1.69     joerg 	 */
    594  1.69     joerg 	if ((def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) &&
    595  1.69     joerg 	    !refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
    596  1.69     joerg 		rdbg(("search referencing object for %s", name));
    597  1.69     joerg 		symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
    598  1.69     joerg 		if (symp != NULL) {
    599  1.69     joerg 			def = symp;
    600  1.69     joerg 			defobj = refobj;
    601  1.69     joerg 		}
    602  1.69     joerg 	}
    603  1.69     joerg 
    604  1.69     joerg 	/*
    605  1.29     skrll 	 * Search the dynamic linker itself, and possibly resolve the
    606  1.29     skrll 	 * symbol from there.  This is how the application links to
    607  1.67  christos 	 * dynamic linker services such as dlopen.
    608  1.29     skrll 	 */
    609  1.29     skrll 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    610  1.40     skrll 		rdbg(("Search the dynamic linker itself."));
    611  1.57    nonaka 		symp = _rtld_symlook_obj(name, hash, &_rtld_objself, flags,
    612  1.57    nonaka 		    ventry);
    613  1.67  christos 		if (symp != NULL) {
    614  1.29     skrll 			def = symp;
    615  1.30     skrll 			defobj = &_rtld_objself;
    616  1.29     skrll 		}
    617  1.28  christos 	}
    618  1.28  christos 
    619  1.29     skrll 	if (def != NULL)
    620  1.29     skrll 		*defobj_out = defobj;
    621  1.29     skrll 	return def;
    622   1.1       cgd }
    623