resolve.c revision 1.25
1/*	$OpenBSD: resolve.c,v 1.25 2005/04/05 19:29:09 drahn Exp $ */
2
3/*
4 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29#define _DYN_LOADER
30
31#include <sys/types.h>
32
33#include <nlist.h>
34#include <link.h>
35#include "syscall.h"
36#include "archdep.h"
37#include "resolve.h"
38
39elf_object_t *_dl_objects;
40elf_object_t *_dl_last_object;
41
42/*
43 * Add a new dynamic object to the object list.
44 */
45void
46_dl_add_object(elf_object_t *object)
47{
48
49	/*
50	 * if this is a new object, prev will be NULL
51	 * != NULL if an object already in the list
52	 * prev == NULL for the first item in the list, but that will
53	 * be the executable.
54	 */
55	if (object->prev != NULL)
56		return;
57
58	if (_dl_objects == NULL) {			/* First object ? */
59		_dl_last_object = _dl_objects = object;
60	} else {
61		_dl_last_object->next = object;
62		object->prev = _dl_last_object;
63		_dl_last_object = object;
64	}
65}
66
67/*
68 * Initialize a new dynamic object.
69 */
70elf_object_t *
71_dl_finalize_object(const char *objname, Elf_Dyn *dynp, const u_long *dl_data,
72    const int objtype, const long laddr, const long loff)
73{
74	elf_object_t *object;
75#if 0
76	_dl_printf("objname [%s], dynp %p, dl_data %p, objtype %x laddr %lx, loff %lx\n",
77	    objname, dynp, dl_data, objtype, laddr, loff);
78#endif
79	object = _dl_malloc(sizeof(elf_object_t));
80	object->prev = object->next = NULL;
81
82	object->load_dyn = dynp;
83	while (dynp->d_tag != DT_NULL) {
84		if (dynp->d_tag < DT_NUM)
85			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
86		else if (dynp->d_tag >= DT_LOPROC &&
87		    dynp->d_tag < DT_LOPROC + DT_NUM)
88			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] =
89			    dynp->d_un.d_val;
90		if (dynp->d_tag == DT_TEXTREL)
91			object->dyn.textrel = 1;
92		if (dynp->d_tag == DT_SYMBOLIC)
93			object->dyn.symbolic = 1;
94		if (dynp->d_tag == DT_BIND_NOW)
95			object->dyn.bind_now = 1;
96		dynp++;
97	}
98
99	/*
100	 *  Now relocate all pointer to dynamic info, but only
101	 *  the ones which have pointer values.
102	 */
103	if (object->Dyn.info[DT_PLTGOT])
104		object->Dyn.info[DT_PLTGOT] += loff;
105	if (object->Dyn.info[DT_HASH])
106		object->Dyn.info[DT_HASH] += loff;
107	if (object->Dyn.info[DT_STRTAB])
108		object->Dyn.info[DT_STRTAB] += loff;
109	if (object->Dyn.info[DT_SYMTAB])
110		object->Dyn.info[DT_SYMTAB] += loff;
111	if (object->Dyn.info[DT_RELA])
112		object->Dyn.info[DT_RELA] += loff;
113	if (object->Dyn.info[DT_SONAME])
114		object->Dyn.info[DT_SONAME] += loff;
115	if (object->Dyn.info[DT_RPATH])
116		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
117	if (object->Dyn.info[DT_REL])
118		object->Dyn.info[DT_REL] += loff;
119	if (object->Dyn.info[DT_INIT])
120		object->Dyn.info[DT_INIT] += loff;
121	if (object->Dyn.info[DT_FINI])
122		object->Dyn.info[DT_FINI] += loff;
123	if (object->Dyn.info[DT_JMPREL])
124		object->Dyn.info[DT_JMPREL] += loff;
125
126	if (object->Dyn.info[DT_HASH] != 0) {
127		Elf_Word *hashtab = (Elf_Word *)object->Dyn.info[DT_HASH];
128
129		object->nbuckets = hashtab[0];
130		object->nchains = hashtab[1];
131		object->buckets = hashtab + 2;
132		object->chains = object->buckets + object->nbuckets;
133	}
134
135	if (dl_data) {
136		object->phdrp = (Elf_Phdr *) dl_data[AUX_phdr];
137		object->phdrc = dl_data[AUX_phnum];
138	}
139	object->obj_type = objtype;
140	object->load_addr = laddr;
141	object->load_offs = loff;
142	object->load_name = _dl_strdup(objname);
143	object->refcount = 1;
144	object->first_child = NULL;
145	object->last_child = NULL;
146	/* default dev, inode for dlopen-able objects. */
147	object->dev = 0;
148	object->inode = 0;
149
150	return(object);
151}
152
153void
154_dl_remove_object(elf_object_t *object)
155{
156	elf_object_t *depobj;
157
158	object->prev->next = object->next;
159	if (object->next)
160		object->next->prev = object->prev;
161
162	if (_dl_last_object == object)
163		_dl_last_object = object->prev;
164
165	if (object->load_name)
166		_dl_free(object->load_name);
167
168	while ((depobj = object->dep_next)) {
169		object->dep_next = object->dep_next->dep_next;
170		_dl_free(depobj);
171	}
172	_dl_free(object);
173}
174
175
176elf_object_t *
177_dl_lookup_object(const char *name)
178{
179	elf_object_t *object;
180
181	object = _dl_objects;
182	while (object) {
183		if (_dl_strcmp(name, object->load_name) == 0)
184			return(object);
185		object = object->next;
186	}
187	return(0);
188}
189
190int _dl_find_symbol_obj(elf_object_t *object, const char *name,
191    unsigned long hash, int flags, const Elf_Sym **ref,
192    const Elf_Sym **weak_sym,
193    elf_object_t **weak_object);
194
195sym_cache *_dl_symcache;
196int _dl_symcachestat_hits;
197int _dl_symcachestat_lookups;
198
199Elf_Addr
200_dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
201    elf_object_t *startlook, const Elf_Sym **ref, const elf_object_t **pobj,
202    int flags, int req_size)
203{
204	Elf_Addr ret;
205	const Elf_Sym *sym;
206	const char *symn;
207	const elf_object_t *sobj;
208
209	_dl_symcachestat_lookups ++;
210	if ((_dl_symcache != NULL) &&
211	     (symidx < req_obj->nchains) &&
212	     (_dl_symcache[symidx].obj != NULL) &&
213	     (_dl_symcache[symidx].sym != NULL) &&
214	     (_dl_symcache[symidx].flags == flags)) {
215
216		_dl_symcachestat_hits++;
217		sobj = _dl_symcache[symidx].obj;
218		*ref = _dl_symcache[symidx].sym;
219		if (pobj)
220			*pobj = sobj;
221		return sobj->load_offs;
222	}
223
224	sym = req_obj->dyn.symtab;
225	sym += symidx;
226	symn = req_obj->dyn.strtab + sym->st_name;
227
228	ret = _dl_find_symbol(symn, startlook, ref, &sobj,
229	    flags, req_size, req_obj);
230
231	if (pobj)
232		*pobj = sobj;
233
234	if ((_dl_symcache != NULL) &&
235	     (symidx < req_obj->nchains)) {
236		_dl_symcache[symidx].sym = *ref;
237		_dl_symcache[symidx].obj = sobj;
238		_dl_symcache[symidx].flags = flags;
239	}
240
241	return ret;
242}
243
244Elf_Addr
245_dl_find_symbol(const char *name, elf_object_t *startlook,
246    const Elf_Sym **ref, const elf_object_t **pobj,
247    int flags, int req_size, elf_object_t *req_obj)
248{
249	const Elf_Sym *weak_sym = NULL;
250	unsigned long h = 0;
251	const char *p = name;
252	elf_object_t *object, *weak_object = NULL;
253	int found = 0;
254	int lastchance = 0;
255
256	while (*p) {
257		unsigned long g;
258		h = (h << 4) + *p++;
259		if ((g = h & 0xf0000000))
260			h ^= g >> 24;
261		h &= ~g;
262	}
263
264	if (req_obj->dyn.symbolic)
265		if (_dl_find_symbol_obj(req_obj, name, h, flags, ref, &weak_sym,
266		    &weak_object)) {
267			object = req_obj;
268			found = 1;
269			goto found;
270		}
271
272retry_nonglobal_dlo:
273	for (object = startlook; object;
274	    object = ((flags & SYM_SEARCH_SELF) ? 0 : object->next)) {
275
276		if ((lastchance == 0) &&
277		    ((object->obj_flags & RTLD_GLOBAL) == 0) &&
278		    (object->obj_type == OBJTYPE_DLO) &&
279		    (object != req_obj))
280			continue;
281
282		if (_dl_find_symbol_obj(object, name, h, flags, ref, &weak_sym,
283		    &weak_object)) {
284			found = 1;
285			break;
286		}
287	}
288
289found:
290	if (weak_object != NULL && found == 0) {
291		object=weak_object;
292		*ref = weak_sym;
293		found = 1;
294	}
295
296	if (found == 0) {
297		if (lastchance == 0) {
298			lastchance = 1;
299			goto retry_nonglobal_dlo;
300		}
301		if (flags & SYM_WARNNOTFOUND)
302			_dl_printf("%s:%s: undefined symbol '%s'\n",
303			    _dl_progname, req_obj->load_name, name);
304		return (0);
305	}
306
307	if (req_size != (*ref)->st_size && req_size != 0 &&
308	    (ELF_ST_TYPE((*ref)->st_info) != STT_FUNC)) {
309		_dl_printf("%s:%s: %s : WARNING: "
310		    "symbol(%s) size mismatch, relink your program\n",
311		    _dl_progname, req_obj->load_name,
312		    object->load_name, name);
313	}
314
315	if (pobj)
316		*pobj = object;
317
318	return (object->load_offs);
319}
320
321int
322_dl_find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash,
323    int flags, const Elf_Sym **ref, const Elf_Sym **weak_sym,
324    elf_object_t **weak_object)
325{
326	const Elf_Sym	*symt = object->dyn.symtab;
327	const char	*strt = object->dyn.strtab;
328	long	si;
329	const char *symn;
330
331	for (si = object->buckets[hash % object->nbuckets];
332	    si != STN_UNDEF; si = object->chains[si]) {
333		const Elf_Sym *sym = symt + si;
334
335		if (sym->st_value == 0)
336			continue;
337
338		if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
339		    ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
340		    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
341			continue;
342
343		symn = strt + sym->st_name;
344		if (sym != *ref && _dl_strcmp(symn, name))
345			continue;
346
347		/* allow this symbol if we are referring to a function
348		 * which has a value, even if section is UNDEF.
349		 * this allows &func to refer to PLT as per the
350		 * ELF spec. st_value is checked above.
351		 * if flags has SYM_PLT set, we must have actual
352		 * symbol, so this symbol is skipped.
353		 */
354		if (sym->st_shndx == SHN_UNDEF) {
355			if ((flags & SYM_PLT) || sym->st_value == 0 ||
356			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
357				continue;
358		}
359
360		if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
361			*ref = sym;
362			return 1;
363		} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
364			if (!*weak_sym) {
365				*weak_sym = sym;
366				*weak_object = object;
367			}
368		}
369	}
370	return 0;
371}
372