Home | History | Annotate | Line # | Download | only in ld.elf_so
symbol.c revision 1.51
      1  1.51       roy /*	$NetBSD: symbol.c,v 1.51 2010/02/27 11:16:38 roy 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.51       roy __RCSID("$NetBSD: symbol.c,v 1.51 2010/02/27 11:16:38 roy 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.1       cgd #include <dirent.h>
     57   1.1       cgd 
     58   1.1       cgd #include "debug.h"
     59   1.1       cgd #include "rtld.h"
     60   1.1       cgd 
     61  1.39       chs typedef void (*fptr_t)(void);
     62  1.39       chs 
     63  1.51       roy /*
     64  1.51       roy  * If the given object is already in the donelist, return true.  Otherwise
     65  1.51       roy  * add the object to the list and return false.
     66  1.51       roy  */
     67  1.51       roy static bool
     68  1.51       roy _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
     69  1.51       roy {
     70  1.51       roy 	unsigned int i;
     71  1.51       roy 
     72  1.51       roy 	for (i = 0;  i < dlp->num_used;  i++)
     73  1.51       roy 		if (dlp->objs[i] == obj)
     74  1.51       roy 			return true;
     75  1.51       roy 	/*
     76  1.51       roy 	 * Our donelist allocation may not always be sufficient as we're not
     77  1.51       roy 	 * thread safe. We'll handle it properly anyway.
     78  1.51       roy 	 */
     79  1.51       roy 	if (dlp->num_used < dlp->num_alloc)
     80  1.51       roy 		dlp->objs[dlp->num_used++] = obj;
     81  1.51       roy 	return false;
     82  1.51       roy }
     83  1.51       roy 
     84  1.33     skrll static bool
     85  1.33     skrll _rtld_is_exported(const Elf_Sym *def)
     86  1.33     skrll {
     87  1.44      yamt 	static const fptr_t _rtld_exports[] = {
     88  1.39       chs 		(fptr_t)dlopen,
     89  1.39       chs 		(fptr_t)dlclose,
     90  1.39       chs 		(fptr_t)dlsym,
     91  1.39       chs 		(fptr_t)dlerror,
     92  1.39       chs 		(fptr_t)dladdr,
     93  1.48     pooka 		(fptr_t)dlinfo,
     94  1.39       chs 		NULL
     95  1.33     skrll 	};
     96  1.33     skrll 	int i;
     97  1.39       chs 	fptr_t value;
     98  1.33     skrll 
     99  1.39       chs 	value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
    100  1.39       chs 	for (i = 0; _rtld_exports[i] != NULL; i++) {
    101  1.33     skrll 		if (value == _rtld_exports[i])
    102  1.33     skrll 			return true;
    103  1.33     skrll 	}
    104  1.33     skrll 	return false;
    105  1.33     skrll }
    106  1.33     skrll 
    107   1.1       cgd /*
    108   1.1       cgd  * Hash function for symbol table lookup.  Don't even think about changing
    109   1.1       cgd  * this.  It is specified by the System V ABI.
    110   1.1       cgd  */
    111   1.1       cgd unsigned long
    112  1.31     skrll _rtld_elf_hash(const char *name)
    113   1.1       cgd {
    114   1.3  christos 	const unsigned char *p = (const unsigned char *) name;
    115   1.3  christos 	unsigned long   h = 0;
    116   1.3  christos 	unsigned long   g;
    117  1.24   mycroft 	unsigned long   c;
    118   1.3  christos 
    119  1.24   mycroft 	for (; __predict_true((c = *p) != '\0'); p++) {
    120  1.24   mycroft 		h <<= 4;
    121  1.24   mycroft 		h += c;
    122  1.24   mycroft 		if ((g = h & 0xf0000000) != 0) {
    123  1.24   mycroft 			h ^= g;
    124   1.3  christos 			h ^= g >> 24;
    125  1.24   mycroft 		}
    126   1.3  christos 	}
    127  1.24   mycroft 	return (h);
    128   1.1       cgd }
    129   1.1       cgd 
    130   1.5   mycroft const Elf_Sym *
    131  1.23   mycroft _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
    132  1.51       roy     const Obj_Entry **defobj_out, bool in_plt, DoneList *dlp)
    133   1.5   mycroft {
    134   1.5   mycroft 	const Elf_Sym *symp;
    135   1.5   mycroft 	const Elf_Sym *def;
    136  1.27   mycroft 	const Obj_Entry *defobj;
    137   1.5   mycroft 	const Objlist_Entry *elm;
    138   1.5   mycroft 
    139   1.5   mycroft 	def = NULL;
    140  1.27   mycroft 	defobj = NULL;
    141  1.12     lukem 	SIMPLEQ_FOREACH(elm, objlist, link) {
    142  1.51       roy 		if (_rtld_donelist_check(dlp, elm->obj))
    143  1.51       roy 			continue;
    144  1.43  christos 		rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
    145  1.43  christos 		    name));
    146  1.21   mycroft 		if ((symp = _rtld_symlook_obj(name, hash, elm->obj, in_plt))
    147   1.5   mycroft 		    != NULL) {
    148   1.5   mycroft 			if ((def == NULL) ||
    149   1.6   thorpej 			    (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    150   1.5   mycroft 				def = symp;
    151  1.27   mycroft 				defobj = elm->obj;
    152   1.6   thorpej 				if (ELF_ST_BIND(def->st_info) != STB_WEAK)
    153   1.5   mycroft 					break;
    154   1.5   mycroft 			}
    155   1.5   mycroft 		}
    156   1.5   mycroft 	}
    157  1.27   mycroft 	if (def != NULL)
    158  1.27   mycroft 		*defobj_out = defobj;
    159   1.5   mycroft 	return def;
    160   1.5   mycroft }
    161   1.5   mycroft 
    162   1.1       cgd /*
    163  1.47     skrll  * Search the symbol table of a shared object and all objects needed by it for
    164  1.47     skrll  * a symbol of the given name. Search order is breadth-first. Returns a pointer
    165  1.47     skrll  * to the symbol, or NULL if no definition was found.
    166  1.47     skrll  */
    167  1.47     skrll const Elf_Sym *
    168  1.47     skrll _rtld_symlook_needed(const char *name, unsigned long hash,
    169  1.51       roy     const Needed_Entry *needed, const Obj_Entry **defobj_out, bool inplt,
    170  1.51       roy     DoneList *breadth, DoneList *depth)
    171  1.47     skrll {
    172  1.47     skrll 	const Elf_Sym *def, *def_w;
    173  1.47     skrll 	const Needed_Entry *n;
    174  1.47     skrll 	const Obj_Entry *obj, *defobj, *defobj1;
    175  1.47     skrll 
    176  1.47     skrll 	def = def_w = NULL;
    177  1.47     skrll 	defobj = NULL;
    178  1.47     skrll 	for (n = needed; n != NULL; n = n->next) {
    179  1.51       roy 		if ((obj = n->obj) == NULL)
    180  1.51       roy 			continue;
    181  1.51       roy 		if (_rtld_donelist_check(breadth, obj))
    182  1.51       roy 			continue;
    183  1.51       roy 		if ((def = _rtld_symlook_obj(name, hash, obj, inplt)) == NULL)
    184  1.47     skrll 			continue;
    185  1.47     skrll 		defobj = obj;
    186  1.47     skrll 		if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
    187  1.47     skrll 			*defobj_out = defobj;
    188  1.47     skrll 
    189  1.47     skrll 			return (def);
    190  1.47     skrll 		}
    191  1.47     skrll 	}
    192  1.47     skrll 	/*
    193  1.47     skrll 	 * Either the symbol definition has not been found in directly needed
    194  1.47     skrll 	 * objects, or the found symbol is weak.
    195  1.47     skrll 	 */
    196  1.47     skrll 	for (n = needed; n != NULL; n = n->next) {
    197  1.47     skrll 		if ((obj = n->obj) == NULL)
    198  1.47     skrll 			continue;
    199  1.51       roy 		if (_rtld_donelist_check(depth, obj))
    200  1.51       roy 			continue;
    201  1.47     skrll 		def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
    202  1.51       roy 		    inplt, breadth, depth);
    203  1.47     skrll 		if (def_w == NULL)
    204  1.47     skrll 			continue;
    205  1.47     skrll 		if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
    206  1.47     skrll 			def = def_w;
    207  1.47     skrll 			defobj = defobj1;
    208  1.47     skrll 			if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
    209  1.47     skrll 				break;
    210  1.47     skrll 		}
    211  1.47     skrll 	}
    212  1.47     skrll 	if (def != NULL)
    213  1.47     skrll 		*defobj_out = defobj;
    214  1.47     skrll 
    215  1.47     skrll 	return def;
    216  1.47     skrll }
    217  1.47     skrll 
    218  1.47     skrll /*
    219   1.1       cgd  * Search the symbol table of a single shared object for a symbol of
    220   1.1       cgd  * the given name.  Returns a pointer to the symbol, or NULL if no
    221   1.1       cgd  * definition was found.
    222   1.1       cgd  *
    223   1.1       cgd  * The symbol's hash value is passed in for efficiency reasons; that
    224   1.1       cgd  * eliminates many recomputations of the hash value.
    225   1.1       cgd  */
    226   1.1       cgd const Elf_Sym *
    227  1.31     skrll _rtld_symlook_obj(const char *name, unsigned long hash,
    228  1.31     skrll     const Obj_Entry *obj, bool in_plt)
    229   1.1       cgd {
    230  1.20   mycroft 	unsigned long symnum;
    231   1.1       cgd 
    232  1.20   mycroft 	for (symnum = obj->buckets[hash % obj->nbuckets];
    233  1.20   mycroft 	     symnum != ELF_SYM_UNDEFINED;
    234  1.20   mycroft 	     symnum = obj->chains[symnum]) {
    235   1.3  christos 		const Elf_Sym  *symp;
    236   1.3  christos 		const char     *strp;
    237   1.3  christos 
    238   1.3  christos 		assert(symnum < obj->nchains);
    239   1.3  christos 		symp = obj->symtab + symnum;
    240   1.3  christos 		strp = obj->strtab + symp->st_name;
    241  1.38    martin 		rdbg(("check \"%s\" vs \"%s\" in %p", name, strp, obj));
    242  1.20   mycroft 		if (name[1] == strp[1] && !strcmp(name, strp)) {
    243  1.20   mycroft 			if (symp->st_shndx != SHN_UNDEF)
    244   1.3  christos 				return symp;
    245  1.21   mycroft #ifndef __mips__
    246  1.22   mycroft 			/*
    247  1.22   mycroft 			 * XXX DANGER WILL ROBINSON!
    248  1.22   mycroft 			 * If we have a function pointer in the executable's
    249  1.22   mycroft 			 * data section, it points to the executable's PLT
    250  1.22   mycroft 			 * slot, and there is NO relocation emitted.  To make
    251  1.22   mycroft 			 * the function pointer comparable to function pointers
    252  1.22   mycroft 			 * in shared libraries, we must resolve data references
    253  1.22   mycroft 			 * in the libraries to point to PLT slots in the
    254  1.22   mycroft 			 * executable, if they exist.
    255  1.22   mycroft 			 */
    256  1.21   mycroft 			else if (!in_plt && symp->st_value != 0 &&
    257  1.21   mycroft 			     ELF_ST_TYPE(symp->st_info) == STT_FUNC)
    258  1.21   mycroft 				return symp;
    259  1.21   mycroft #endif
    260  1.19   mycroft 			else
    261  1.19   mycroft 				return NULL;
    262   1.3  christos 		}
    263   1.1       cgd 	}
    264   1.1       cgd 
    265   1.3  christos 	return NULL;
    266   1.1       cgd }
    267   1.1       cgd 
    268  1.49     skrll #ifdef COMBRELOC
    269  1.49     skrll static const Obj_Entry *_rtld_last_refobj;
    270  1.49     skrll 
    271  1.49     skrll /*
    272  1.49     skrll  * Called when an object is freed. Reset the cached symbol look up if
    273  1.49     skrll  * our last referencing or definition object just got unloaded.
    274  1.49     skrll  */
    275  1.49     skrll void
    276  1.49     skrll _rtld_combreloc_reset(const Obj_Entry *obj)
    277  1.49     skrll {
    278  1.49     skrll 	if (_rtld_last_refobj == obj)
    279  1.49     skrll 		_rtld_last_refobj = NULL;
    280  1.49     skrll }
    281  1.49     skrll #endif
    282  1.49     skrll 
    283   1.1       cgd /*
    284   1.1       cgd  * Given a symbol number in a referencing object, find the corresponding
    285   1.1       cgd  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    286   1.1       cgd  * no definition was found.  Returns a pointer to the Obj_Entry of the
    287   1.1       cgd  * defining object via the reference parameter DEFOBJ_OUT.
    288   1.1       cgd  */
    289   1.1       cgd const Elf_Sym *
    290  1.31     skrll _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
    291  1.31     skrll     const Obj_Entry **defobj_out, bool in_plt)
    292   1.1       cgd {
    293  1.27   mycroft 	const Elf_Sym  *ref;
    294   1.5   mycroft 	const Elf_Sym  *def;
    295  1.27   mycroft 	const Obj_Entry *defobj;
    296  1.27   mycroft 	const char     *name;
    297   1.3  christos 	unsigned long   hash;
    298   1.3  christos 
    299  1.41      matt #ifdef COMBRELOC
    300  1.41      matt 	/*
    301  1.41      matt 	 * COMBRELOC combines multiple reloc sections and sorts them to make
    302  1.41      matt 	 * dynamic symbol lookup caching possible.
    303  1.41      matt 	 *
    304  1.41      matt 	 * So if the lookup we are doing is the same as the previous lookup
    305  1.41      matt 	 * return the cached results.
    306  1.41      matt 	 */
    307  1.41      matt 	static unsigned long last_symnum;
    308  1.49     skrll 	static const Obj_Entry *last_defobj;
    309  1.41      matt 	static const Elf_Sym *last_def;
    310  1.41      matt 
    311  1.49     skrll 	if (symnum == last_symnum && refobj == _rtld_last_refobj
    312  1.42      matt 	    && in_plt == false) {
    313  1.41      matt 		*defobj_out = last_defobj;
    314  1.41      matt 		return last_def;
    315  1.41      matt 	}
    316  1.41      matt #endif
    317  1.41      matt 
    318  1.27   mycroft 	ref = refobj->symtab + symnum;
    319  1.27   mycroft 	name = refobj->strtab + ref->st_name;
    320  1.27   mycroft 
    321  1.27   mycroft 	/*
    322  1.40     skrll 	 * We don't have to do a full scale lookup if the symbol is local.
    323  1.40     skrll 	 * We know it will bind to the instance in this load module; to
    324  1.40     skrll 	 * which we already have a pointer (ie ref).
    325  1.33     skrll 	 */
    326  1.40     skrll 	if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
    327  1.40     skrll 		if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
    328  1.40     skrll 			_rtld_error("%s: Bogus symbol table entry %lu",
    329  1.40     skrll 			    refobj->path, symnum);
    330  1.40     skrll         	}
    331  1.40     skrll 
    332  1.40     skrll 		hash = _rtld_elf_hash(name);
    333  1.40     skrll 		defobj = NULL;
    334  1.40     skrll 		def = _rtld_symlook_default(name, hash, refobj, &defobj, in_plt);
    335  1.40     skrll 	} else {
    336  1.40     skrll 		rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
    337  1.40     skrll 		def = ref;
    338  1.40     skrll 		defobj = refobj;
    339  1.33     skrll 	}
    340  1.40     skrll 
    341  1.33     skrll 	/*
    342  1.27   mycroft 	 * If we found no definition and the reference is weak, treat the
    343  1.27   mycroft 	 * symbol as having the value zero.
    344  1.27   mycroft 	 */
    345  1.27   mycroft 	if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
    346  1.45  christos 		rdbg(("  returning _rtld_sym_zero@_rtld_objself"));
    347  1.27   mycroft 		def = &_rtld_sym_zero;
    348  1.45  christos 		defobj = &_rtld_objself;
    349  1.27   mycroft 	}
    350  1.33     skrll 
    351  1.41      matt 	if (def != NULL) {
    352  1.27   mycroft 		*defobj_out = defobj;
    353  1.41      matt #ifdef COMBRELOC
    354  1.42      matt 		if (in_plt == false) {
    355  1.42      matt 			/*
    356  1.42      matt 			 * Cache the lookup arguments and results if this was
    357  1.42      matt 			 * non-PLT lookup.
    358  1.42      matt 			 */
    359  1.42      matt 			last_symnum = symnum;
    360  1.49     skrll 			_rtld_last_refobj = refobj;
    361  1.42      matt 			last_def = def;
    362  1.42      matt 			last_defobj = defobj;
    363  1.42      matt 		}
    364  1.41      matt #endif
    365  1.41      matt 	} else {
    366  1.27   mycroft 		rdbg(("lookup failed"));
    367  1.27   mycroft 		_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
    368  1.27   mycroft 		    refobj->path, in_plt ? "PLT " : "", name, symnum);
    369  1.27   mycroft 	}
    370  1.26   mycroft 	return def;
    371  1.28  christos }
    372  1.28  christos 
    373  1.50  christos const Elf_Sym *
    374  1.50  christos _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
    375  1.50  christos     const Obj_Entry **defobj, bool imm)
    376  1.50  christos {
    377  1.50  christos  	const Elf_Sym  *def = _rtld_find_symdef(symnum, obj, defobj, true);
    378  1.50  christos 	if (__predict_false(def == NULL))
    379  1.50  christos  		return NULL;
    380  1.50  christos 
    381  1.50  christos 	if (__predict_false(def == &_rtld_sym_zero)) {
    382  1.50  christos 		/* tp is set during lazy binding. */
    383  1.50  christos 		if (imm) {
    384  1.50  christos 			const Elf_Sym	*ref = obj->symtab + symnum;
    385  1.50  christos 			const char	*name = obj->strtab + ref->st_name;
    386  1.50  christos 
    387  1.50  christos 			_rtld_error(
    388  1.50  christos 			    "%s: Trying to call undefined weak symbol `%s'",
    389  1.50  christos 			    obj->path, name);
    390  1.50  christos 			return NULL;
    391  1.50  christos 		}
    392  1.50  christos 	}
    393  1.50  christos 	return def;
    394  1.50  christos }
    395  1.50  christos 
    396  1.28  christos /*
    397  1.28  christos  * Given a symbol name in a referencing object, find the corresponding
    398  1.28  christos  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
    399  1.28  christos  * no definition was found.  Returns a pointer to the Obj_Entry of the
    400  1.28  christos  * defining object via the reference parameter DEFOBJ_OUT.
    401  1.28  christos  */
    402  1.28  christos const Elf_Sym *
    403  1.28  christos _rtld_symlook_default(const char *name, unsigned long hash,
    404  1.28  christos     const Obj_Entry *refobj, const Obj_Entry **defobj_out, bool in_plt)
    405  1.28  christos {
    406  1.29     skrll 	const Elf_Sym *def;
    407  1.29     skrll 	const Elf_Sym *symp;
    408  1.29     skrll 	const Obj_Entry *obj;
    409  1.29     skrll 	const Obj_Entry *defobj;
    410  1.29     skrll 	const Objlist_Entry *elm;
    411  1.29     skrll 	def = NULL;
    412  1.29     skrll 	defobj = NULL;
    413  1.51       roy 	DoneList donelist;
    414  1.51       roy 
    415  1.51       roy 	_rtld_donelist_init(&donelist);
    416  1.29     skrll 
    417  1.29     skrll 	/* Look first in the referencing object if linked symbolically. */
    418  1.51       roy 	if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
    419  1.43  christos 		rdbg(("search referencing object for %s", name));
    420  1.29     skrll 		symp = _rtld_symlook_obj(name, hash, refobj, in_plt);
    421  1.29     skrll 		if (symp != NULL) {
    422  1.29     skrll 			def = symp;
    423  1.29     skrll 			defobj = refobj;
    424  1.29     skrll 		}
    425  1.29     skrll 	}
    426  1.29     skrll 
    427  1.29     skrll 	/* Search all objects loaded at program start up. */
    428  1.29     skrll 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    429  1.43  christos 		rdbg(("search _rtld_list_main for %s", name));
    430  1.40     skrll 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
    431  1.51       roy 		    in_plt, &donelist);
    432  1.29     skrll 		if (symp != NULL &&
    433  1.40     skrll 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    434  1.29     skrll 			def = symp;
    435  1.29     skrll 			defobj = obj;
    436  1.29     skrll 		}
    437  1.29     skrll 	}
    438  1.29     skrll 
    439  1.40     skrll 	/* Search all RTLD_GLOBAL objects. */
    440  1.40     skrll 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    441  1.43  christos 		rdbg(("search _rtld_list_global for %s", name));
    442  1.40     skrll 		symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
    443  1.51       roy 		    &obj, in_plt, &donelist);
    444  1.29     skrll 		if (symp != NULL &&
    445  1.29     skrll 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    446  1.29     skrll 			def = symp;
    447  1.29     skrll 			defobj = obj;
    448  1.29     skrll 		}
    449  1.29     skrll 	}
    450  1.40     skrll 
    451  1.40     skrll 	/* Search all dlopened DAGs containing the referencing object. */
    452  1.40     skrll 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
    453  1.29     skrll 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
    454  1.29     skrll 			break;
    455  1.43  christos 		rdbg(("search DAG with root %p (%s) for %s", elm->obj,
    456  1.43  christos 		    elm->obj->path, name));
    457  1.40     skrll 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
    458  1.51       roy 		    &obj, in_plt, &donelist);
    459  1.29     skrll 		if (symp != NULL &&
    460  1.29     skrll 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
    461  1.29     skrll 			def = symp;
    462  1.29     skrll 			defobj = obj;
    463  1.29     skrll 		}
    464  1.28  christos 	}
    465  1.28  christos 
    466  1.29     skrll 	/*
    467  1.29     skrll 	 * Search the dynamic linker itself, and possibly resolve the
    468  1.29     skrll 	 * symbol from there.  This is how the application links to
    469  1.29     skrll 	 * dynamic linker services such as dlopen.  Only the values listed
    470  1.40     skrll 	 * in the "_rtld_exports" array can be resolved from the dynamic
    471  1.40     skrll 	 * linker.
    472  1.29     skrll 	 */
    473  1.29     skrll 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
    474  1.40     skrll 		rdbg(("Search the dynamic linker itself."));
    475  1.30     skrll 		symp = _rtld_symlook_obj(name, hash, &_rtld_objself, in_plt);
    476  1.40     skrll 		if (symp != NULL && _rtld_is_exported(symp)) {
    477  1.29     skrll 			def = symp;
    478  1.30     skrll 			defobj = &_rtld_objself;
    479  1.29     skrll 		}
    480  1.28  christos 	}
    481  1.28  christos 
    482  1.51       roy 	_rtld_donelist_clear(&donelist);
    483  1.51       roy 
    484  1.29     skrll 	if (def != NULL)
    485  1.29     skrll 		*defobj_out = defobj;
    486  1.29     skrll 	return def;
    487   1.1       cgd }
    488