resolve.c revision 1.28
1/*	$OpenBSD: resolve.c,v 1.28 2005/09/16 23:19:41 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;
41elf_object_t *_dl_loading_object;
42
43/*
44 * Add a new dynamic object to the object list.
45 */
46void
47_dl_add_object(elf_object_t *object)
48{
49
50	/*
51	 * if this is a new object, prev will be NULL
52	 * != NULL if an object already in the list
53	 * prev == NULL for the first item in the list, but that will
54	 * be the executable.
55	 */
56	if (object->prev != NULL)
57		return;
58
59	if (_dl_objects == NULL) {			/* First object ? */
60		_dl_last_object = _dl_objects = object;
61	} else {
62		_dl_last_object->next = object;
63		object->prev = _dl_last_object;
64		_dl_last_object = object;
65	}
66}
67
68/*
69 * Initialize a new dynamic object.
70 */
71elf_object_t *
72_dl_finalize_object(const char *objname, Elf_Dyn *dynp, const long *dl_data,
73    const int objtype, const long laddr, const long loff)
74{
75	elf_object_t *object;
76#if 0
77	_dl_printf("objname [%s], dynp %p, dl_data %p, objtype %x laddr %lx, loff %lx\n",
78	    objname, dynp, dl_data, objtype, laddr, loff);
79#endif
80	object = _dl_malloc(sizeof(elf_object_t));
81	object->prev = object->next = NULL;
82
83	object->load_dyn = dynp;
84	while (dynp->d_tag != DT_NULL) {
85		if (dynp->d_tag < DT_NUM)
86			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
87		else if (dynp->d_tag >= DT_LOPROC &&
88		    dynp->d_tag < DT_LOPROC + DT_NUM)
89			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] =
90			    dynp->d_un.d_val;
91		if (dynp->d_tag == DT_TEXTREL)
92			object->dyn.textrel = 1;
93		if (dynp->d_tag == DT_SYMBOLIC)
94			object->dyn.symbolic = 1;
95		if (dynp->d_tag == DT_BIND_NOW)
96			object->dyn.bind_now = 1;
97		dynp++;
98	}
99
100	/*
101	 *  Now relocate all pointer to dynamic info, but only
102	 *  the ones which have pointer values.
103	 */
104	if (object->Dyn.info[DT_PLTGOT])
105		object->Dyn.info[DT_PLTGOT] += loff;
106	if (object->Dyn.info[DT_HASH])
107		object->Dyn.info[DT_HASH] += loff;
108	if (object->Dyn.info[DT_STRTAB])
109		object->Dyn.info[DT_STRTAB] += loff;
110	if (object->Dyn.info[DT_SYMTAB])
111		object->Dyn.info[DT_SYMTAB] += loff;
112	if (object->Dyn.info[DT_RELA])
113		object->Dyn.info[DT_RELA] += loff;
114	if (object->Dyn.info[DT_SONAME])
115		object->Dyn.info[DT_SONAME] += loff;
116	if (object->Dyn.info[DT_RPATH])
117		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
118	if (object->Dyn.info[DT_REL])
119		object->Dyn.info[DT_REL] += loff;
120	if (object->Dyn.info[DT_INIT])
121		object->Dyn.info[DT_INIT] += loff;
122	if (object->Dyn.info[DT_FINI])
123		object->Dyn.info[DT_FINI] += loff;
124	if (object->Dyn.info[DT_JMPREL])
125		object->Dyn.info[DT_JMPREL] += loff;
126
127	if (object->Dyn.info[DT_HASH] != 0) {
128		Elf_Word *hashtab = (Elf_Word *)object->Dyn.info[DT_HASH];
129
130		object->nbuckets = hashtab[0];
131		object->nchains = hashtab[1];
132		object->buckets = hashtab + 2;
133		object->chains = object->buckets + object->nbuckets;
134	}
135
136	if (dl_data) {
137		object->phdrp = (Elf_Phdr *) dl_data[AUX_phdr];
138		object->phdrc = dl_data[AUX_phnum];
139	}
140	object->obj_type = objtype;
141	object->load_addr = laddr;
142	object->load_offs = loff;
143	object->load_name = _dl_strdup(objname);
144	if (_dl_loading_object == NULL) {
145		/*
146		 * no loading object, object is the loading object,
147		 * as it is either executable, or dlopened()
148		 */
149		_dl_loading_object = object->load_object = object;
150		DL_DEB(("head %s\n", object->load_name ));
151	} else {
152		object->load_object = _dl_loading_object;
153	}
154	DL_DEB(("obj %s has %s as head\n", object->load_name,
155	    _dl_loading_object->load_name ));
156	object->refcount = 1;
157	TAILQ_INIT(&object->child_list);
158	/* default dev, inode for dlopen-able objects. */
159	object->dev = 0;
160	object->inode = 0;
161	TAILQ_INIT(&object->dload_list);
162
163	return(object);
164}
165
166void
167_dl_remove_object(elf_object_t *object)
168{
169	elf_object_t *depobj;
170
171	object->prev->next = object->next;
172	if (object->next)
173		object->next->prev = object->prev;
174
175	if (_dl_last_object == object)
176		_dl_last_object = object->prev;
177
178	if (object->load_name)
179		_dl_free(object->load_name);
180
181	while ((depobj = object->dep_next)) {
182		object->dep_next = object->dep_next->dep_next;
183		_dl_free(depobj);
184	}
185	_dl_free(object);
186}
187
188
189elf_object_t *
190_dl_lookup_object(const char *name)
191{
192	elf_object_t *object;
193
194	object = _dl_objects;
195	while (object) {
196		if (_dl_strcmp(name, object->load_name) == 0)
197			return(object);
198		object = object->next;
199	}
200	return(0);
201}
202
203int _dl_find_symbol_obj(elf_object_t *object, const char *name,
204    unsigned long hash, int flags, const Elf_Sym **ref,
205    const Elf_Sym **weak_sym,
206    elf_object_t **weak_object);
207
208sym_cache *_dl_symcache;
209int _dl_symcachestat_hits;
210int _dl_symcachestat_lookups;
211
212Elf_Addr
213_dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
214    const Elf_Sym **ref, int flags, int req_size, const elf_object_t **pobj)
215{
216	Elf_Addr ret;
217	const Elf_Sym *sym;
218	const char *symn;
219	const elf_object_t *sobj;
220
221	_dl_symcachestat_lookups ++;
222	if ((_dl_symcache != NULL) &&
223	     (symidx < req_obj->nchains) &&
224	     (_dl_symcache[symidx].obj != NULL) &&
225	     (_dl_symcache[symidx].sym != NULL) &&
226	     (_dl_symcache[symidx].flags == flags)) {
227
228		_dl_symcachestat_hits++;
229		sobj = _dl_symcache[symidx].obj;
230		*ref = _dl_symcache[symidx].sym;
231		if (pobj)
232			*pobj = sobj;
233		return sobj->load_offs;
234	}
235
236	sym = req_obj->dyn.symtab;
237	sym += symidx;
238	symn = req_obj->dyn.strtab + sym->st_name;
239
240	ret = _dl_find_symbol(symn, ref, flags, req_size, req_obj, &sobj);
241
242	if (pobj)
243		*pobj = sobj;
244
245	if ((_dl_symcache != NULL) &&
246	     (symidx < req_obj->nchains)) {
247		_dl_symcache[symidx].sym = *ref;
248		_dl_symcache[symidx].obj = sobj;
249		_dl_symcache[symidx].flags = flags;
250	}
251
252	return ret;
253}
254
255Elf_Addr
256_dl_find_symbol(const char *name, const Elf_Sym **ref,
257    int flags, int req_size, elf_object_t *req_obj, const elf_object_t **pobj)
258{
259	const Elf_Sym *weak_sym = NULL;
260	unsigned long h = 0;
261	const char *p = name;
262	elf_object_t *object = NULL, *weak_object = NULL;
263	int found = 0, lastchance = 0;
264	struct dep_node *n, *m;
265
266
267	while (*p) {
268		unsigned long g;
269		h = (h << 4) + *p++;
270		if ((g = h & 0xf0000000))
271			h ^= g >> 24;
272		h &= ~g;
273	}
274
275	if (req_obj->dyn.symbolic)
276		if (_dl_find_symbol_obj(req_obj, name, h, flags, ref, &weak_sym,
277		    &weak_object)) {
278			object = req_obj;
279			found = 1;
280			goto found;
281		}
282
283
284retry_nonglobal_dlo:
285	if (flags & SYM_SEARCH_OBJ) {
286		if (_dl_find_symbol_obj(req_obj, name, h, flags, ref,
287		    &weak_sym, &weak_object)) {
288			object = req_obj;
289			found = 1;
290		}
291	} else if ((flags & SYM_SEARCH_SELF) || (flags & SYM_SEARCH_NEXT)) {
292		/* search after req_obj in the objects's load group */
293		int skip = 1;
294
295		TAILQ_FOREACH(n, &req_obj->load_object->dload_list, next_sib) {
296
297			if (n->data == req_obj && skip == 1) {
298				skip = 0;
299				if (flags & SYM_SEARCH_NEXT)
300					continue;
301			}
302			if (skip == 1)
303				continue;
304			if (_dl_find_symbol_obj(n->data, name, h, flags, ref,
305			    &weak_sym, &weak_object)) {
306				object = n->data;
307				found = 1;
308				break;
309			}
310		}
311	} else if (flags & SYM_DLSYM) {
312		if (_dl_find_symbol_obj(req_obj, name, h, flags, ref,
313		    &weak_sym, &weak_object)) {
314			object = req_obj;
315			found = 1;
316		}
317		if (weak_object != NULL && found == 0) {
318			object=weak_object;
319			*ref = weak_sym;
320			found = 1;
321		}
322		/* search dlopened obj and all children */
323
324		if (found == 0) {
325			TAILQ_FOREACH(n, &req_obj->load_object->dload_list,
326			    next_sib) {
327				if (_dl_find_symbol_obj(n->data, name, h,
328				    flags, ref,
329				    &weak_sym, &weak_object)) {
330					object = n->data;
331					found = 1;
332					break;
333				}
334			}
335		}
336	} else {
337		/* search main program and it's libs */
338		TAILQ_FOREACH(n, &_dl_objects->dload_list, next_sib) {
339			if ((flags & SYM_SEARCH_OTHER) &&
340			    (n->data == req_obj)) {
341				continue;
342			}
343
344			if (_dl_find_symbol_obj(n->data, name, h, flags,
345			    ref, &weak_sym, &weak_object)) {
346				object = n->data;
347				found = 1;
348				goto found;
349			}
350		}
351
352		/*
353		 * search dlopened objects: global or req_obj == dlopened_obj
354		 * and and it's children
355		 */
356		TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) {
357			if ((lastchance == 0) &&
358			    ((n->data->obj_flags & RTLD_GLOBAL) == 0) &&
359			    (n->data != req_obj->load_object))
360				continue;
361
362			TAILQ_FOREACH(m, &n->data->dload_list, next_sib) {
363				if ((flags & SYM_SEARCH_OTHER) &&
364				    (m->data == req_obj))
365					continue;
366				if (_dl_find_symbol_obj(m->data, name, h, flags,
367				    ref, &weak_sym, &weak_object)) {
368					object = m->data;
369					found = 1;
370					break;
371				}
372			}
373		}
374	}
375
376found:
377	if (weak_object != NULL && found == 0) {
378		object=weak_object;
379		*ref = weak_sym;
380		found = 1;
381	}
382
383
384	if (found == 0) {
385#if 1
386		if (lastchance == 0) {
387			lastchance = 1;
388			goto retry_nonglobal_dlo;
389		}
390#endif
391		if (flags & SYM_WARNNOTFOUND)
392			_dl_printf("%s:%s: undefined symbol '%s'\n",
393			    _dl_progname, req_obj->load_name, name);
394		return (0);
395	}
396
397	if (req_size != (*ref)->st_size && req_size != 0 &&
398	    (ELF_ST_TYPE((*ref)->st_info) != STT_FUNC)) {
399		_dl_printf("%s:%s: %s : WARNING: "
400		    "symbol(%s) size mismatch, relink your program\n",
401		    _dl_progname, req_obj->load_name,
402		    object->load_name, name);
403	}
404
405	if (pobj)
406		*pobj = object;
407
408	return (object->load_offs);
409}
410
411int
412_dl_find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash,
413    int flags, const Elf_Sym **ref, const Elf_Sym **weak_sym,
414    elf_object_t **weak_object)
415{
416	const Elf_Sym	*symt = object->dyn.symtab;
417	const char	*strt = object->dyn.strtab;
418	long	si;
419	const char *symn;
420
421	for (si = object->buckets[hash % object->nbuckets];
422	    si != STN_UNDEF; si = object->chains[si]) {
423		const Elf_Sym *sym = symt + si;
424
425		if (sym->st_value == 0)
426			continue;
427
428		if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
429		    ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
430		    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
431			continue;
432
433		symn = strt + sym->st_name;
434		if (sym != *ref && _dl_strcmp(symn, name))
435			continue;
436
437		/* allow this symbol if we are referring to a function
438		 * which has a value, even if section is UNDEF.
439		 * this allows &func to refer to PLT as per the
440		 * ELF spec. st_value is checked above.
441		 * if flags has SYM_PLT set, we must have actual
442		 * symbol, so this symbol is skipped.
443		 */
444		if (sym->st_shndx == SHN_UNDEF) {
445			if ((flags & SYM_PLT) || sym->st_value == 0 ||
446			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
447				continue;
448		}
449
450		if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
451			*ref = sym;
452			return 1;
453		} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
454			if (!*weak_sym) {
455				*weak_sym = sym;
456				*weak_object = object;
457			}
458		}
459	}
460	return 0;
461}
462