xref: /minix3/libexec/ld.elf_so/symbol.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: symbol.c,v 1.65 2014/08/10 23:35:26 matt Exp $	 */
2e83f7ba2SBen Gras 
3e83f7ba2SBen Gras /*
4e83f7ba2SBen Gras  * Copyright 1996 John D. Polstra.
5e83f7ba2SBen Gras  * Copyright 1996 Matt Thomas <matt@3am-software.com>
6e83f7ba2SBen Gras  * Copyright 2002 Charles M. Hannum <root@ihack.net>
7e83f7ba2SBen Gras  * All rights reserved.
8e83f7ba2SBen Gras  *
9e83f7ba2SBen Gras  * Redistribution and use in source and binary forms, with or without
10e83f7ba2SBen Gras  * modification, are permitted provided that the following conditions
11e83f7ba2SBen Gras  * are met:
12e83f7ba2SBen Gras  * 1. Redistributions of source code must retain the above copyright
13e83f7ba2SBen Gras  *    notice, this list of conditions and the following disclaimer.
14e83f7ba2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
15e83f7ba2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
16e83f7ba2SBen Gras  *    documentation and/or other materials provided with the distribution.
17e83f7ba2SBen Gras  * 3. All advertising materials mentioning features or use of this software
18e83f7ba2SBen Gras  *    must display the following acknowledgement:
19e83f7ba2SBen Gras  *      This product includes software developed by John Polstra.
20e83f7ba2SBen Gras  * 4. The name of the author may not be used to endorse or promote products
21e83f7ba2SBen Gras  *    derived from this software without specific prior written permission.
22e83f7ba2SBen Gras  *
23e83f7ba2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24e83f7ba2SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25e83f7ba2SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26e83f7ba2SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27e83f7ba2SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28e83f7ba2SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29e83f7ba2SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30e83f7ba2SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31e83f7ba2SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32e83f7ba2SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33e83f7ba2SBen Gras  */
34e83f7ba2SBen Gras 
35e83f7ba2SBen Gras /*
36e83f7ba2SBen Gras  * Dynamic linker for ELF.
37e83f7ba2SBen Gras  *
38e83f7ba2SBen Gras  * John Polstra <jdp@polstra.com>.
39e83f7ba2SBen Gras  */
40e83f7ba2SBen Gras 
41e83f7ba2SBen Gras #include <sys/cdefs.h>
42e83f7ba2SBen Gras #ifndef lint
43*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: symbol.c,v 1.65 2014/08/10 23:35:26 matt Exp $");
44e83f7ba2SBen Gras #endif /* not lint */
45e83f7ba2SBen Gras 
46e83f7ba2SBen Gras #include <err.h>
47e83f7ba2SBen Gras #include <errno.h>
48e83f7ba2SBen Gras #include <fcntl.h>
49e83f7ba2SBen Gras #include <stdarg.h>
50e83f7ba2SBen Gras #include <stdio.h>
51e83f7ba2SBen Gras #include <stdlib.h>
52e83f7ba2SBen Gras #include <string.h>
53e83f7ba2SBen Gras #include <unistd.h>
54e83f7ba2SBen Gras #include <sys/types.h>
55e83f7ba2SBen Gras #include <sys/mman.h>
56e83f7ba2SBen Gras #include <sys/bitops.h>
57e83f7ba2SBen Gras #include <dirent.h>
58e83f7ba2SBen Gras 
59e83f7ba2SBen Gras #include "debug.h"
60e83f7ba2SBen Gras #include "rtld.h"
61e83f7ba2SBen Gras 
62e83f7ba2SBen Gras /*
63e83f7ba2SBen Gras  * If the given object is already in the donelist, return true.  Otherwise
64e83f7ba2SBen Gras  * add the object to the list and return false.
65e83f7ba2SBen Gras  */
66e83f7ba2SBen Gras static bool
_rtld_donelist_check(DoneList * dlp,const Obj_Entry * obj)67e83f7ba2SBen Gras _rtld_donelist_check(DoneList *dlp, const Obj_Entry *obj)
68e83f7ba2SBen Gras {
69e83f7ba2SBen Gras 	unsigned int i;
70e83f7ba2SBen Gras 
71e83f7ba2SBen Gras 	for (i = 0;  i < dlp->num_used;  i++)
72e83f7ba2SBen Gras 		if (dlp->objs[i] == obj)
73e83f7ba2SBen Gras 			return true;
74e83f7ba2SBen Gras 	/*
75e83f7ba2SBen Gras 	 * Our donelist allocation may not always be sufficient as we're not
76e83f7ba2SBen Gras 	 * thread safe. We'll handle it properly anyway.
77e83f7ba2SBen Gras 	 */
78e83f7ba2SBen Gras 	if (dlp->num_used < dlp->num_alloc)
79e83f7ba2SBen Gras 		dlp->objs[dlp->num_used++] = obj;
80e83f7ba2SBen Gras 	return false;
81e83f7ba2SBen Gras }
82e83f7ba2SBen Gras 
83e83f7ba2SBen Gras static bool
_rtld_is_exported(const Elf_Sym * def)84e83f7ba2SBen Gras _rtld_is_exported(const Elf_Sym *def)
85e83f7ba2SBen Gras {
86e83f7ba2SBen Gras 	static const fptr_t _rtld_exports[] = {
87e83f7ba2SBen Gras 		(fptr_t)dlopen,
88e83f7ba2SBen Gras 		(fptr_t)dlclose,
89e83f7ba2SBen Gras 		(fptr_t)dlsym,
90f14fb602SLionel Sambuc 		(fptr_t)dlvsym,
91e83f7ba2SBen Gras 		(fptr_t)dlerror,
92e83f7ba2SBen Gras 		(fptr_t)dladdr,
93e83f7ba2SBen Gras 		(fptr_t)dlinfo,
94e83f7ba2SBen Gras 		(fptr_t)dl_iterate_phdr,
95f14fb602SLionel Sambuc 		(fptr_t)_dlauxinfo,
96f14fb602SLionel Sambuc #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
97f14fb602SLionel Sambuc 		(fptr_t)_rtld_tls_allocate,
98f14fb602SLionel Sambuc 		(fptr_t)_rtld_tls_free,
99f14fb602SLionel Sambuc 		(fptr_t)__tls_get_addr,
100f14fb602SLionel Sambuc #ifdef __i386__
101f14fb602SLionel Sambuc 		(fptr_t)___tls_get_addr,
102f14fb602SLionel Sambuc #endif
103f14fb602SLionel Sambuc #endif
104*0a6a1f1dSLionel Sambuc #if defined(__ARM_EABI__) && !defined(__ARM_DWARF_EH__)
10584d9c625SLionel Sambuc 		(fptr_t)__gnu_Unwind_Find_exidx,	/* for gcc EHABI */
10684d9c625SLionel Sambuc #endif
107e83f7ba2SBen Gras 		NULL
108e83f7ba2SBen Gras 	};
109e83f7ba2SBen Gras 	int i;
110e83f7ba2SBen Gras 	fptr_t value;
111e83f7ba2SBen Gras 
112e83f7ba2SBen Gras 	value = (fptr_t)(_rtld_objself.relocbase + def->st_value);
113e83f7ba2SBen Gras 	for (i = 0; _rtld_exports[i] != NULL; i++) {
114e83f7ba2SBen Gras 		if (value == _rtld_exports[i])
115e83f7ba2SBen Gras 			return true;
116e83f7ba2SBen Gras 	}
117e83f7ba2SBen Gras 	return false;
118e83f7ba2SBen Gras }
119e83f7ba2SBen Gras 
120e83f7ba2SBen Gras /*
121e83f7ba2SBen Gras  * Hash function for symbol table lookup.  Don't even think about changing
122e83f7ba2SBen Gras  * this.  It is specified by the System V ABI.
123e83f7ba2SBen Gras  */
124e83f7ba2SBen Gras unsigned long
_rtld_elf_hash(const char * name)125e83f7ba2SBen Gras _rtld_elf_hash(const char *name)
126e83f7ba2SBen Gras {
127e83f7ba2SBen Gras 	const unsigned char *p = (const unsigned char *) name;
128e83f7ba2SBen Gras 	unsigned long   h = 0;
129e83f7ba2SBen Gras 	unsigned long   g;
130e83f7ba2SBen Gras 	unsigned long   c;
131e83f7ba2SBen Gras 
132e83f7ba2SBen Gras 	for (; __predict_true((c = *p) != '\0'); p++) {
133e83f7ba2SBen Gras 		h <<= 4;
134e83f7ba2SBen Gras 		h += c;
135e83f7ba2SBen Gras 		if ((g = h & 0xf0000000) != 0) {
136e83f7ba2SBen Gras 			h ^= g;
137e83f7ba2SBen Gras 			h ^= g >> 24;
138e83f7ba2SBen Gras 		}
139e83f7ba2SBen Gras 	}
140e83f7ba2SBen Gras 	return (h);
141e83f7ba2SBen Gras }
142e83f7ba2SBen Gras 
143e83f7ba2SBen Gras const Elf_Sym *
_rtld_symlook_list(const char * name,unsigned long hash,const Objlist * objlist,const Obj_Entry ** defobj_out,u_int flags,const Ver_Entry * ventry,DoneList * dlp)144e83f7ba2SBen Gras _rtld_symlook_list(const char *name, unsigned long hash, const Objlist *objlist,
145f14fb602SLionel Sambuc     const Obj_Entry **defobj_out, u_int flags, const Ver_Entry *ventry,
146f14fb602SLionel Sambuc     DoneList *dlp)
147e83f7ba2SBen Gras {
148e83f7ba2SBen Gras 	const Elf_Sym *symp;
149e83f7ba2SBen Gras 	const Elf_Sym *def;
150e83f7ba2SBen Gras 	const Obj_Entry *defobj;
151e83f7ba2SBen Gras 	const Objlist_Entry *elm;
152e83f7ba2SBen Gras 
153e83f7ba2SBen Gras 	def = NULL;
154e83f7ba2SBen Gras 	defobj = NULL;
155e83f7ba2SBen Gras 	SIMPLEQ_FOREACH(elm, objlist, link) {
156e83f7ba2SBen Gras 		if (_rtld_donelist_check(dlp, elm->obj))
157e83f7ba2SBen Gras 			continue;
158e83f7ba2SBen Gras 		rdbg(("search object %p (%s) for %s", elm->obj, elm->obj->path,
159e83f7ba2SBen Gras 		    name));
160f14fb602SLionel Sambuc 		symp = _rtld_symlook_obj(name, hash, elm->obj, flags, ventry);
161f14fb602SLionel Sambuc 		if (symp != NULL) {
162e83f7ba2SBen Gras 			if ((def == NULL) ||
163e83f7ba2SBen Gras 			    (ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
164e83f7ba2SBen Gras 				def = symp;
165e83f7ba2SBen Gras 				defobj = elm->obj;
166e83f7ba2SBen Gras 				if (ELF_ST_BIND(def->st_info) != STB_WEAK)
167e83f7ba2SBen Gras 					break;
168e83f7ba2SBen Gras 			}
169e83f7ba2SBen Gras 		}
170e83f7ba2SBen Gras 	}
171e83f7ba2SBen Gras 	if (def != NULL)
172e83f7ba2SBen Gras 		*defobj_out = defobj;
173e83f7ba2SBen Gras 	return def;
174e83f7ba2SBen Gras }
175e83f7ba2SBen Gras 
176e83f7ba2SBen Gras /*
177e83f7ba2SBen Gras  * Search the symbol table of a shared object and all objects needed by it for
178e83f7ba2SBen Gras  * a symbol of the given name. Search order is breadth-first. Returns a pointer
179e83f7ba2SBen Gras  * to the symbol, or NULL if no definition was found.
180e83f7ba2SBen Gras  */
181e83f7ba2SBen Gras const Elf_Sym *
_rtld_symlook_needed(const char * name,unsigned long hash,const Needed_Entry * needed,const Obj_Entry ** defobj_out,u_int flags,const Ver_Entry * ventry,DoneList * breadth,DoneList * depth)182e83f7ba2SBen Gras _rtld_symlook_needed(const char *name, unsigned long hash,
183f14fb602SLionel Sambuc     const Needed_Entry *needed, const Obj_Entry **defobj_out, u_int flags,
184f14fb602SLionel Sambuc     const Ver_Entry *ventry, DoneList *breadth, DoneList *depth)
185e83f7ba2SBen Gras {
186e83f7ba2SBen Gras 	const Elf_Sym *def, *def_w;
187e83f7ba2SBen Gras 	const Needed_Entry *n;
188e83f7ba2SBen Gras 	const Obj_Entry *obj, *defobj, *defobj1;
189e83f7ba2SBen Gras 
190e83f7ba2SBen Gras 	def = def_w = NULL;
191e83f7ba2SBen Gras 	defobj = NULL;
192e83f7ba2SBen Gras 	for (n = needed; n != NULL; n = n->next) {
193e83f7ba2SBen Gras 		if ((obj = n->obj) == NULL)
194e83f7ba2SBen Gras 			continue;
195e83f7ba2SBen Gras 		if (_rtld_donelist_check(breadth, obj))
196e83f7ba2SBen Gras 			continue;
197f14fb602SLionel Sambuc 		def = _rtld_symlook_obj(name, hash, obj, flags, ventry);
198f14fb602SLionel Sambuc 		if (def == NULL)
199e83f7ba2SBen Gras 			continue;
200e83f7ba2SBen Gras 		defobj = obj;
201e83f7ba2SBen Gras 		if (ELF_ST_BIND(def->st_info) != STB_WEAK) {
202e83f7ba2SBen Gras 			*defobj_out = defobj;
203e83f7ba2SBen Gras 
204e83f7ba2SBen Gras 			return (def);
205e83f7ba2SBen Gras 		}
206e83f7ba2SBen Gras 	}
207e83f7ba2SBen Gras 	/*
208e83f7ba2SBen Gras 	 * Either the symbol definition has not been found in directly needed
209e83f7ba2SBen Gras 	 * objects, or the found symbol is weak.
210e83f7ba2SBen Gras 	 */
211e83f7ba2SBen Gras 	for (n = needed; n != NULL; n = n->next) {
212e83f7ba2SBen Gras 		if ((obj = n->obj) == NULL)
213e83f7ba2SBen Gras 			continue;
214e83f7ba2SBen Gras 		if (_rtld_donelist_check(depth, obj))
215e83f7ba2SBen Gras 			continue;
216e83f7ba2SBen Gras 		def_w = _rtld_symlook_needed(name, hash, obj->needed, &defobj1,
217f14fb602SLionel Sambuc 		    flags, ventry, breadth, depth);
218e83f7ba2SBen Gras 		if (def_w == NULL)
219e83f7ba2SBen Gras 			continue;
220e83f7ba2SBen Gras 		if (def == NULL || ELF_ST_BIND(def_w->st_info) != STB_WEAK) {
221e83f7ba2SBen Gras 			def = def_w;
222e83f7ba2SBen Gras 			defobj = defobj1;
223e83f7ba2SBen Gras 			if (ELF_ST_BIND(def_w->st_info) != STB_WEAK)
224e83f7ba2SBen Gras 				break;
225e83f7ba2SBen Gras 		}
226e83f7ba2SBen Gras 	}
227e83f7ba2SBen Gras 	if (def != NULL)
228e83f7ba2SBen Gras 		*defobj_out = defobj;
229e83f7ba2SBen Gras 
230e83f7ba2SBen Gras 	return def;
231e83f7ba2SBen Gras }
232e83f7ba2SBen Gras 
233e83f7ba2SBen Gras /*
234e83f7ba2SBen Gras  * Search the symbol table of a single shared object for a symbol of
235e83f7ba2SBen Gras  * the given name.  Returns a pointer to the symbol, or NULL if no
236e83f7ba2SBen Gras  * definition was found.
237e83f7ba2SBen Gras  *
238e83f7ba2SBen Gras  * The symbol's hash value is passed in for efficiency reasons; that
239e83f7ba2SBen Gras  * eliminates many recomputations of the hash value.
240e83f7ba2SBen Gras  */
241e83f7ba2SBen Gras const Elf_Sym *
_rtld_symlook_obj(const char * name,unsigned long hash,const Obj_Entry * obj,u_int flags,const Ver_Entry * ventry)242e83f7ba2SBen Gras _rtld_symlook_obj(const char *name, unsigned long hash,
243f14fb602SLionel Sambuc     const Obj_Entry *obj, u_int flags, const Ver_Entry *ventry)
244e83f7ba2SBen Gras {
245e83f7ba2SBen Gras 	unsigned long symnum;
246f14fb602SLionel Sambuc 	const Elf_Sym *vsymp = NULL;
247f14fb602SLionel Sambuc 	Elf_Half verndx;
248f14fb602SLionel Sambuc 	int vcount = 0;
249e83f7ba2SBen Gras 
250e83f7ba2SBen Gras 	for (symnum = obj->buckets[fast_remainder32(hash, obj->nbuckets,
251e83f7ba2SBen Gras 	     obj->nbuckets_m, obj->nbuckets_s1, obj->nbuckets_s2)];
252e83f7ba2SBen Gras 	     symnum != ELF_SYM_UNDEFINED;
253e83f7ba2SBen Gras 	     symnum = obj->chains[symnum]) {
254e83f7ba2SBen Gras 		const Elf_Sym  *symp;
255e83f7ba2SBen Gras 		const char     *strp;
256e83f7ba2SBen Gras 
257e83f7ba2SBen Gras 		assert(symnum < obj->nchains);
258e83f7ba2SBen Gras 		symp = obj->symtab + symnum;
259e83f7ba2SBen Gras 		strp = obj->strtab + symp->st_name;
260f14fb602SLionel Sambuc 		rdbg(("check \"%s\" vs \"%s\" in %s", name, strp, obj->path));
261f14fb602SLionel Sambuc 		if (name[1] != strp[1] || strcmp(name, strp))
262f14fb602SLionel Sambuc 			continue;
263*0a6a1f1dSLionel Sambuc #if defined(__mips__) || defined(__vax__)
264f14fb602SLionel Sambuc 		if (symp->st_shndx == SHN_UNDEF)
265f14fb602SLionel Sambuc 			continue;
266f14fb602SLionel Sambuc #else
267e83f7ba2SBen Gras 		/*
268e83f7ba2SBen Gras 		 * XXX DANGER WILL ROBINSON!
269e83f7ba2SBen Gras 		 * If we have a function pointer in the executable's
270e83f7ba2SBen Gras 		 * data section, it points to the executable's PLT
271e83f7ba2SBen Gras 		 * slot, and there is NO relocation emitted.  To make
272e83f7ba2SBen Gras 		 * the function pointer comparable to function pointers
273e83f7ba2SBen Gras 		 * in shared libraries, we must resolve data references
274e83f7ba2SBen Gras 		 * in the libraries to point to PLT slots in the
275e83f7ba2SBen Gras 		 * executable, if they exist.
276e83f7ba2SBen Gras 		 */
277f14fb602SLionel Sambuc 		if (symp->st_shndx == SHN_UNDEF &&
278f14fb602SLionel Sambuc 		    ((flags & SYMLOOK_IN_PLT) ||
279f14fb602SLionel Sambuc 		    symp->st_value == 0 ||
280f14fb602SLionel Sambuc 		    ELF_ST_TYPE(symp->st_info) != STT_FUNC))
281f14fb602SLionel Sambuc 			continue;
282e83f7ba2SBen Gras #endif
283f14fb602SLionel Sambuc 
284f14fb602SLionel Sambuc 		if (ventry == NULL) {
285f14fb602SLionel Sambuc 			if (obj->versyms != NULL) {
286f14fb602SLionel Sambuc 				verndx = VER_NDX(obj->versyms[symnum].vs_vers);
287f14fb602SLionel Sambuc 				if (verndx > obj->vertabnum) {
288f14fb602SLionel Sambuc 					_rtld_error("%s: symbol %s references "
289f14fb602SLionel Sambuc 					    "wrong version %d", obj->path,
290f14fb602SLionel Sambuc 					    &obj->strtab[symnum], verndx);
291f14fb602SLionel Sambuc 					continue;
292e83f7ba2SBen Gras 				}
293e83f7ba2SBen Gras 
294f14fb602SLionel Sambuc 				/*
295f14fb602SLionel Sambuc 				 * If we are not called from dlsym (i.e. this
296f14fb602SLionel Sambuc 				 * is a normal relocation from unversioned
297f14fb602SLionel Sambuc 				 * binary), accept the symbol immediately
298f14fb602SLionel Sambuc 				 * if it happens to have first version after
299f14fb602SLionel Sambuc 				 * this shared object became versioned.
300f14fb602SLionel Sambuc 				 * Otherwise, if symbol is versioned and not
301f14fb602SLionel Sambuc 				 * hidden, remember it. If it is the only
302f14fb602SLionel Sambuc 				 * symbol with this name exported by the shared
303f14fb602SLionel Sambuc 				 * object, it will be returned as a match at the
304f14fb602SLionel Sambuc 				 * end of the function. If symbol is global
305f14fb602SLionel Sambuc 				 * (verndx < 2) accept it unconditionally.
306f14fb602SLionel Sambuc 				 */
307f14fb602SLionel Sambuc 				if (!(flags & SYMLOOK_DLSYM) &&
308f14fb602SLionel Sambuc 				    verndx == VER_NDX_GIVEN) {
309f14fb602SLionel Sambuc 					return symp;
310f14fb602SLionel Sambuc 				} else if (verndx >= VER_NDX_GIVEN) {
311f14fb602SLionel Sambuc 					if (!(obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN)) {
312f14fb602SLionel Sambuc 						if (vsymp == NULL)
313f14fb602SLionel Sambuc 							vsymp = symp;
314f14fb602SLionel Sambuc 						vcount++;
315f14fb602SLionel Sambuc 					}
316f14fb602SLionel Sambuc 					continue;
317f14fb602SLionel Sambuc 				}
318f14fb602SLionel Sambuc 			}
319f14fb602SLionel Sambuc 			return symp;
320f14fb602SLionel Sambuc 		} else {
321f14fb602SLionel Sambuc 			if (obj->versyms == NULL) {
322f14fb602SLionel Sambuc 				if (_rtld_object_match_name(obj, ventry->name)){
323f14fb602SLionel Sambuc 					_rtld_error("%s: object %s should "
324f14fb602SLionel Sambuc 					    "provide version %s for symbol %s",
325f14fb602SLionel Sambuc 					    _rtld_objself.path, obj->path,
326f14fb602SLionel Sambuc 					    ventry->name, &obj->strtab[symnum]);
327f14fb602SLionel Sambuc 					continue;
328f14fb602SLionel Sambuc 				}
329f14fb602SLionel Sambuc 			} else {
330f14fb602SLionel Sambuc 				verndx = VER_NDX(obj->versyms[symnum].vs_vers);
331f14fb602SLionel Sambuc 				if (verndx > obj->vertabnum) {
332f14fb602SLionel Sambuc 					_rtld_error("%s: symbol %s references "
333f14fb602SLionel Sambuc 					    "wrong version %d", obj->path,
334f14fb602SLionel Sambuc 					    &obj->strtab[symnum], verndx);
335f14fb602SLionel Sambuc 					continue;
336f14fb602SLionel Sambuc 				}
337f14fb602SLionel Sambuc 				if (obj->vertab[verndx].hash != ventry->hash ||
338f14fb602SLionel Sambuc 				    strcmp(obj->vertab[verndx].name, ventry->name)) {
339f14fb602SLionel Sambuc 					/*
340f14fb602SLionel Sambuc 					* Version does not match. Look if this
341f14fb602SLionel Sambuc 					* is a global symbol and if it is not
342f14fb602SLionel Sambuc 					* hidden. If global symbol (verndx < 2)
343f14fb602SLionel Sambuc 					* is available, use it. Do not return
344f14fb602SLionel Sambuc 					* symbol if we are called by dlvsym,
345f14fb602SLionel Sambuc 					* because dlvsym looks for a specific
346f14fb602SLionel Sambuc 					* version and default one is not what
347f14fb602SLionel Sambuc 					* dlvsym wants.
348f14fb602SLionel Sambuc 					*/
349f14fb602SLionel Sambuc 					if ((flags & SYMLOOK_DLSYM) ||
350f14fb602SLionel Sambuc 					    (obj->versyms[symnum].vs_vers & VER_NDX_HIDDEN) ||
351f14fb602SLionel Sambuc 					    (verndx >= VER_NDX_GIVEN))
352f14fb602SLionel Sambuc 						continue;
353f14fb602SLionel Sambuc 				}
354f14fb602SLionel Sambuc 			}
355f14fb602SLionel Sambuc 			return symp;
356f14fb602SLionel Sambuc 		}
357f14fb602SLionel Sambuc 	}
358f14fb602SLionel Sambuc 	if (vcount == 1)
359f14fb602SLionel Sambuc 		return vsymp;
360e83f7ba2SBen Gras 	return NULL;
361e83f7ba2SBen Gras }
362e83f7ba2SBen Gras 
363e83f7ba2SBen Gras #ifdef COMBRELOC
364e83f7ba2SBen Gras static const Obj_Entry *_rtld_last_refobj;
365e83f7ba2SBen Gras 
366e83f7ba2SBen Gras /*
367e83f7ba2SBen Gras  * Called when an object is freed. Reset the cached symbol look up if
368e83f7ba2SBen Gras  * our last referencing or definition object just got unloaded.
369e83f7ba2SBen Gras  */
370e83f7ba2SBen Gras void
_rtld_combreloc_reset(const Obj_Entry * obj)371e83f7ba2SBen Gras _rtld_combreloc_reset(const Obj_Entry *obj)
372e83f7ba2SBen Gras {
373e83f7ba2SBen Gras 	if (_rtld_last_refobj == obj)
374e83f7ba2SBen Gras 		_rtld_last_refobj = NULL;
375e83f7ba2SBen Gras }
376e83f7ba2SBen Gras #endif
377e83f7ba2SBen Gras 
378e83f7ba2SBen Gras /*
379e83f7ba2SBen Gras  * Given a symbol number in a referencing object, find the corresponding
380e83f7ba2SBen Gras  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
381e83f7ba2SBen Gras  * no definition was found.  Returns a pointer to the Obj_Entry of the
382e83f7ba2SBen Gras  * defining object via the reference parameter DEFOBJ_OUT.
383e83f7ba2SBen Gras  */
384e83f7ba2SBen Gras const Elf_Sym *
_rtld_find_symdef(unsigned long symnum,const Obj_Entry * refobj,const Obj_Entry ** defobj_out,u_int flags)385e83f7ba2SBen Gras _rtld_find_symdef(unsigned long symnum, const Obj_Entry *refobj,
386f14fb602SLionel Sambuc     const Obj_Entry **defobj_out, u_int flags)
387e83f7ba2SBen Gras {
388e83f7ba2SBen Gras 	const Elf_Sym  *ref;
389e83f7ba2SBen Gras 	const Elf_Sym  *def;
390e83f7ba2SBen Gras 	const Obj_Entry *defobj;
391e83f7ba2SBen Gras 	const char     *name;
392e83f7ba2SBen Gras 	unsigned long   hash;
393e83f7ba2SBen Gras 
394e83f7ba2SBen Gras #ifdef COMBRELOC
395e83f7ba2SBen Gras 	/*
396e83f7ba2SBen Gras 	 * COMBRELOC combines multiple reloc sections and sorts them to make
397e83f7ba2SBen Gras 	 * dynamic symbol lookup caching possible.
398e83f7ba2SBen Gras 	 *
399e83f7ba2SBen Gras 	 * So if the lookup we are doing is the same as the previous lookup
400e83f7ba2SBen Gras 	 * return the cached results.
401e83f7ba2SBen Gras 	 */
402e83f7ba2SBen Gras 	static unsigned long last_symnum;
403e83f7ba2SBen Gras 	static const Obj_Entry *last_defobj;
404e83f7ba2SBen Gras 	static const Elf_Sym *last_def;
405e83f7ba2SBen Gras 
406e83f7ba2SBen Gras 	if (symnum == last_symnum && refobj == _rtld_last_refobj
407f14fb602SLionel Sambuc 	    && !(flags & SYMLOOK_IN_PLT)) {
408e83f7ba2SBen Gras 		*defobj_out = last_defobj;
409e83f7ba2SBen Gras 		return last_def;
410e83f7ba2SBen Gras 	}
411e83f7ba2SBen Gras #endif
412e83f7ba2SBen Gras 
413e83f7ba2SBen Gras 	ref = refobj->symtab + symnum;
414e83f7ba2SBen Gras 	name = refobj->strtab + ref->st_name;
415e83f7ba2SBen Gras 
416e83f7ba2SBen Gras 	/*
417e83f7ba2SBen Gras 	 * We don't have to do a full scale lookup if the symbol is local.
418e83f7ba2SBen Gras 	 * We know it will bind to the instance in this load module; to
419e83f7ba2SBen Gras 	 * which we already have a pointer (ie ref).
420e83f7ba2SBen Gras 	 */
421e83f7ba2SBen Gras 	if (ELF_ST_BIND(ref->st_info) != STB_LOCAL) {
422e83f7ba2SBen Gras 		if (ELF_ST_TYPE(ref->st_info) == STT_SECTION) {
423e83f7ba2SBen Gras 			_rtld_error("%s: Bogus symbol table entry %lu",
424e83f7ba2SBen Gras 			    refobj->path, symnum);
425e83f7ba2SBen Gras         	}
426e83f7ba2SBen Gras 
427e83f7ba2SBen Gras 		hash = _rtld_elf_hash(name);
428e83f7ba2SBen Gras 		defobj = NULL;
429f14fb602SLionel Sambuc 		def = _rtld_symlook_default(name, hash, refobj, &defobj, flags,
430f14fb602SLionel Sambuc 		    _rtld_fetch_ventry(refobj, symnum));
431e83f7ba2SBen Gras 	} else {
432e83f7ba2SBen Gras 		rdbg(("STB_LOCAL symbol %s in %s", name, refobj->path));
433e83f7ba2SBen Gras 		def = ref;
434e83f7ba2SBen Gras 		defobj = refobj;
435e83f7ba2SBen Gras 	}
436e83f7ba2SBen Gras 
437e83f7ba2SBen Gras 	/*
438e83f7ba2SBen Gras 	 * If we found no definition and the reference is weak, treat the
439e83f7ba2SBen Gras 	 * symbol as having the value zero.
440e83f7ba2SBen Gras 	 */
441e83f7ba2SBen Gras 	if (def == NULL && ELF_ST_BIND(ref->st_info) == STB_WEAK) {
442e83f7ba2SBen Gras 		rdbg(("  returning _rtld_sym_zero@_rtld_objself"));
443e83f7ba2SBen Gras 		def = &_rtld_sym_zero;
444e83f7ba2SBen Gras 		defobj = &_rtld_objself;
445e83f7ba2SBen Gras 	}
446e83f7ba2SBen Gras 
447e83f7ba2SBen Gras 	if (def != NULL) {
448e83f7ba2SBen Gras 		*defobj_out = defobj;
449e83f7ba2SBen Gras #ifdef COMBRELOC
450f14fb602SLionel Sambuc 		if (!(flags & SYMLOOK_IN_PLT)) {
451e83f7ba2SBen Gras 			/*
452e83f7ba2SBen Gras 			 * Cache the lookup arguments and results if this was
453e83f7ba2SBen Gras 			 * non-PLT lookup.
454e83f7ba2SBen Gras 			 */
455e83f7ba2SBen Gras 			last_symnum = symnum;
456e83f7ba2SBen Gras 			_rtld_last_refobj = refobj;
457e83f7ba2SBen Gras 			last_def = def;
458e83f7ba2SBen Gras 			last_defobj = defobj;
459e83f7ba2SBen Gras 		}
460e83f7ba2SBen Gras #endif
461e83f7ba2SBen Gras 	} else {
462e83f7ba2SBen Gras 		rdbg(("lookup failed"));
463e83f7ba2SBen Gras 		_rtld_error("%s: Undefined %ssymbol \"%s\" (symnum = %ld)",
464f14fb602SLionel Sambuc 		    refobj->path, (flags & SYMLOOK_IN_PLT) ? "PLT " : "",
465f14fb602SLionel Sambuc 		    name, symnum);
466e83f7ba2SBen Gras 	}
467e83f7ba2SBen Gras 	return def;
468e83f7ba2SBen Gras }
469e83f7ba2SBen Gras 
470e83f7ba2SBen Gras const Elf_Sym *
_rtld_find_plt_symdef(unsigned long symnum,const Obj_Entry * obj,const Obj_Entry ** defobj,bool imm)471e83f7ba2SBen Gras _rtld_find_plt_symdef(unsigned long symnum, const Obj_Entry *obj,
472e83f7ba2SBen Gras     const Obj_Entry **defobj, bool imm)
473e83f7ba2SBen Gras {
474f14fb602SLionel Sambuc  	const Elf_Sym  *def = _rtld_find_symdef(symnum, obj, defobj,
475f14fb602SLionel Sambuc 	    SYMLOOK_IN_PLT);
476e83f7ba2SBen Gras 	if (__predict_false(def == NULL))
477e83f7ba2SBen Gras  		return NULL;
478e83f7ba2SBen Gras 
479e83f7ba2SBen Gras 	if (__predict_false(def == &_rtld_sym_zero)) {
480e83f7ba2SBen Gras 		/* tp is set during lazy binding. */
481e83f7ba2SBen Gras 		if (imm) {
482e83f7ba2SBen Gras 			const Elf_Sym	*ref = obj->symtab + symnum;
483e83f7ba2SBen Gras 			const char	*name = obj->strtab + ref->st_name;
484e83f7ba2SBen Gras 
485e83f7ba2SBen Gras 			_rtld_error(
486e83f7ba2SBen Gras 			    "%s: Trying to call undefined weak symbol `%s'",
487e83f7ba2SBen Gras 			    obj->path, name);
488e83f7ba2SBen Gras 			return NULL;
489e83f7ba2SBen Gras 		}
490e83f7ba2SBen Gras 	}
491e83f7ba2SBen Gras 	return def;
492e83f7ba2SBen Gras }
493e83f7ba2SBen Gras 
494e83f7ba2SBen Gras /*
495e83f7ba2SBen Gras  * Given a symbol name in a referencing object, find the corresponding
496e83f7ba2SBen Gras  * definition of the symbol.  Returns a pointer to the symbol, or NULL if
497e83f7ba2SBen Gras  * no definition was found.  Returns a pointer to the Obj_Entry of the
498e83f7ba2SBen Gras  * defining object via the reference parameter DEFOBJ_OUT.
499e83f7ba2SBen Gras  */
500e83f7ba2SBen Gras const Elf_Sym *
_rtld_symlook_default(const char * name,unsigned long hash,const Obj_Entry * refobj,const Obj_Entry ** defobj_out,u_int flags,const Ver_Entry * ventry)501e83f7ba2SBen Gras _rtld_symlook_default(const char *name, unsigned long hash,
502f14fb602SLionel Sambuc     const Obj_Entry *refobj, const Obj_Entry **defobj_out, u_int flags,
503f14fb602SLionel Sambuc     const Ver_Entry *ventry)
504e83f7ba2SBen Gras {
505e83f7ba2SBen Gras 	const Elf_Sym *def;
506e83f7ba2SBen Gras 	const Elf_Sym *symp;
507e83f7ba2SBen Gras 	const Obj_Entry *obj;
508e83f7ba2SBen Gras 	const Obj_Entry *defobj;
509e83f7ba2SBen Gras 	const Objlist_Entry *elm;
510e83f7ba2SBen Gras 	def = NULL;
511e83f7ba2SBen Gras 	defobj = NULL;
512e83f7ba2SBen Gras 	DoneList donelist;
513e83f7ba2SBen Gras 
514e83f7ba2SBen Gras 	_rtld_donelist_init(&donelist);
515e83f7ba2SBen Gras 
516e83f7ba2SBen Gras 	/* Look first in the referencing object if linked symbolically. */
517e83f7ba2SBen Gras 	if (refobj->symbolic && !_rtld_donelist_check(&donelist, refobj)) {
518e83f7ba2SBen Gras 		rdbg(("search referencing object for %s", name));
519f14fb602SLionel Sambuc 		symp = _rtld_symlook_obj(name, hash, refobj, flags, ventry);
520e83f7ba2SBen Gras 		if (symp != NULL) {
521e83f7ba2SBen Gras 			def = symp;
522e83f7ba2SBen Gras 			defobj = refobj;
523e83f7ba2SBen Gras 		}
524e83f7ba2SBen Gras 	}
525e83f7ba2SBen Gras 
526e83f7ba2SBen Gras 	/* Search all objects loaded at program start up. */
527e83f7ba2SBen Gras 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
528e83f7ba2SBen Gras 		rdbg(("search _rtld_list_main for %s", name));
529e83f7ba2SBen Gras 		symp = _rtld_symlook_list(name, hash, &_rtld_list_main, &obj,
530f14fb602SLionel Sambuc 		    flags, ventry, &donelist);
531e83f7ba2SBen Gras 		if (symp != NULL &&
532e83f7ba2SBen Gras 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
533e83f7ba2SBen Gras 			def = symp;
534e83f7ba2SBen Gras 			defobj = obj;
535e83f7ba2SBen Gras 		}
536e83f7ba2SBen Gras 	}
537e83f7ba2SBen Gras 
538e83f7ba2SBen Gras 	/* Search all RTLD_GLOBAL objects. */
539e83f7ba2SBen Gras 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
540e83f7ba2SBen Gras 		rdbg(("search _rtld_list_global for %s", name));
541e83f7ba2SBen Gras 		symp = _rtld_symlook_list(name, hash, &_rtld_list_global,
542f14fb602SLionel Sambuc 		    &obj, flags, ventry, &donelist);
543e83f7ba2SBen Gras 		if (symp != NULL &&
544e83f7ba2SBen Gras 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
545e83f7ba2SBen Gras 			def = symp;
546e83f7ba2SBen Gras 			defobj = obj;
547e83f7ba2SBen Gras 		}
548e83f7ba2SBen Gras 	}
549e83f7ba2SBen Gras 
550e83f7ba2SBen Gras 	/* Search all dlopened DAGs containing the referencing object. */
551e83f7ba2SBen Gras 	SIMPLEQ_FOREACH(elm, &refobj->dldags, link) {
552e83f7ba2SBen Gras 		if (def != NULL && ELF_ST_BIND(def->st_info) != STB_WEAK)
553e83f7ba2SBen Gras 			break;
554e83f7ba2SBen Gras 		rdbg(("search DAG with root %p (%s) for %s", elm->obj,
555e83f7ba2SBen Gras 		    elm->obj->path, name));
556e83f7ba2SBen Gras 		symp = _rtld_symlook_list(name, hash, &elm->obj->dagmembers,
557f14fb602SLionel Sambuc 		    &obj, flags, ventry, &donelist);
558e83f7ba2SBen Gras 		if (symp != NULL &&
559e83f7ba2SBen Gras 		    (def == NULL || ELF_ST_BIND(symp->st_info) != STB_WEAK)) {
560e83f7ba2SBen Gras 			def = symp;
561e83f7ba2SBen Gras 			defobj = obj;
562e83f7ba2SBen Gras 		}
563e83f7ba2SBen Gras 	}
564e83f7ba2SBen Gras 
565e83f7ba2SBen Gras 	/*
566e83f7ba2SBen Gras 	 * Search the dynamic linker itself, and possibly resolve the
567e83f7ba2SBen Gras 	 * symbol from there.  This is how the application links to
568e83f7ba2SBen Gras 	 * dynamic linker services such as dlopen.  Only the values listed
569e83f7ba2SBen Gras 	 * in the "_rtld_exports" array can be resolved from the dynamic
570e83f7ba2SBen Gras 	 * linker.
571e83f7ba2SBen Gras 	 */
572e83f7ba2SBen Gras 	if (def == NULL || ELF_ST_BIND(def->st_info) == STB_WEAK) {
573e83f7ba2SBen Gras 		rdbg(("Search the dynamic linker itself."));
574f14fb602SLionel Sambuc 		symp = _rtld_symlook_obj(name, hash, &_rtld_objself, flags,
575f14fb602SLionel Sambuc 		    ventry);
576e83f7ba2SBen Gras 		if (symp != NULL && _rtld_is_exported(symp)) {
577e83f7ba2SBen Gras 			def = symp;
578e83f7ba2SBen Gras 			defobj = &_rtld_objself;
579e83f7ba2SBen Gras 		}
580e83f7ba2SBen Gras 	}
581e83f7ba2SBen Gras 
582e83f7ba2SBen Gras 	if (def != NULL)
583e83f7ba2SBen Gras 		*defobj_out = defobj;
584e83f7ba2SBen Gras 	return def;
585e83f7ba2SBen Gras }
586