resolve.c revision 1.38
1/*	$OpenBSD: resolve.c,v 1.38 2005/10/03 19:48:24 kurt 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	/* refcount handled in _dl_link_sub, _dl_boot & dlopen */
157	object->refcount = 0;
158	TAILQ_INIT(&object->child_list);
159	object->opencount = 0;	/* # dlopen() */
160	/* default dev, inode for dlopen-able objects. */
161	object->dev = 0;
162	object->inode = 0;
163	TAILQ_INIT(&object->dload_list);
164
165	return(object);
166}
167
168elf_object_t *free_objects;
169
170void _dl_cleanup_objects(void);
171void
172_dl_cleanup_objects()
173{
174	elf_object_t *nobj, *head;
175	struct dep_node *n;
176
177retry:
178	TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) {
179		if (n->data->refcount == 0) {
180			TAILQ_REMOVE(&_dlopened_child_list, n, next_sib);
181			_dl_free(n);
182			goto retry;
183		}
184	}
185
186	head = free_objects;
187	free_objects = NULL;
188	while (head != NULL) {
189		nobj = head->next;
190		_dl_free(head);
191		head = nobj;
192	}
193}
194
195void
196_dl_remove_object(elf_object_t *object)
197{
198	elf_object_t *depobj;
199
200	object->prev->next = object->next;
201	if (object->next)
202		object->next->prev = object->prev;
203
204	if (_dl_last_object == object)
205		_dl_last_object = object->prev;
206
207	if (object->load_name)
208		_dl_free(object->load_name);
209
210	while ((depobj = object->dep_next)) {
211		object->dep_next = object->dep_next->dep_next;
212		_dl_free(depobj);
213	}
214	object->next = free_objects;
215	free_objects = object->next;
216}
217
218
219elf_object_t *
220_dl_lookup_object(const char *name)
221{
222	elf_object_t *object;
223
224	object = _dl_objects;
225	while (object) {
226		if (_dl_strcmp(name, object->load_name) == 0)
227			return(object);
228		object = object->next;
229	}
230	return(0);
231}
232
233int _dl_find_symbol_obj(elf_object_t *object, const char *name,
234    unsigned long hash, int flags, const Elf_Sym **ref,
235    const Elf_Sym **weak_sym,
236    elf_object_t **weak_object);
237
238sym_cache *_dl_symcache;
239int _dl_symcachestat_hits;
240int _dl_symcachestat_lookups;
241
242Elf_Addr
243_dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
244    const Elf_Sym **this, int flags, const Elf_Sym *ref_sym, const elf_object_t **pobj)
245{
246	Elf_Addr ret;
247	const Elf_Sym *sym;
248	const char *symn;
249	const elf_object_t *sobj;
250
251	_dl_symcachestat_lookups ++;
252	if (_dl_symcache != NULL &&
253	    symidx < req_obj->nchains &&
254	    _dl_symcache[symidx].obj != NULL &&
255	    _dl_symcache[symidx].sym != NULL &&
256	    _dl_symcache[symidx].flags == flags) {
257
258		_dl_symcachestat_hits++;
259		sobj = _dl_symcache[symidx].obj;
260		*this = _dl_symcache[symidx].sym;
261		if (pobj)
262			*pobj = sobj;
263		return sobj->load_offs;
264	}
265
266	sym = req_obj->dyn.symtab;
267	sym += symidx;
268	symn = req_obj->dyn.strtab + sym->st_name;
269
270	ret = _dl_find_symbol(symn, this, flags, ref_sym, req_obj, &sobj);
271
272	if (pobj)
273		*pobj = sobj;
274
275	if (_dl_symcache != NULL && symidx < req_obj->nchains) {
276		_dl_symcache[symidx].sym = *this;
277		_dl_symcache[symidx].obj = sobj;
278		_dl_symcache[symidx].flags = flags;
279	}
280
281	return ret;
282}
283
284Elf_Addr
285_dl_find_symbol(const char *name, const Elf_Sym **this,
286    int flags, const Elf_Sym *ref_sym, elf_object_t *req_obj,
287    const elf_object_t **pobj)
288{
289	const Elf_Sym *weak_sym = NULL;
290	unsigned long h = 0;
291	const char *p = name;
292	elf_object_t *object = NULL, *weak_object = NULL;
293	int found = 0;
294	struct dep_node *n, *m;
295
296
297	while (*p) {
298		unsigned long g;
299		h = (h << 4) + *p++;
300		if ((g = h & 0xf0000000))
301			h ^= g >> 24;
302		h &= ~g;
303	}
304
305	if (req_obj->dyn.symbolic)
306		if (_dl_find_symbol_obj(req_obj, name, h, flags, this, &weak_sym,
307		    &weak_object)) {
308			object = req_obj;
309			found = 1;
310			goto found;
311		}
312
313	if (flags & SYM_SEARCH_OBJ) {
314		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
315		    &weak_sym, &weak_object)) {
316			object = req_obj;
317			found = 1;
318		}
319	} else if (flags & SYM_DLSYM) {
320		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
321		    &weak_sym, &weak_object)) {
322			object = req_obj;
323			found = 1;
324		}
325		if (weak_object != NULL && found == 0) {
326			object=weak_object;
327			*this = weak_sym;
328			found = 1;
329		}
330		/* search dlopened obj and all children */
331
332		if (found == 0) {
333			TAILQ_FOREACH(n, &req_obj->load_object->dload_list,
334			    next_sib) {
335				if (_dl_find_symbol_obj(n->data, name, h,
336				    flags, this,
337				    &weak_sym, &weak_object)) {
338					object = n->data;
339					found = 1;
340					break;
341				}
342			}
343		}
344	} else {
345		int skip = 0;
346
347		if ((flags & SYM_SEARCH_SELF) || (flags & SYM_SEARCH_NEXT))
348			skip = 1;
349
350		/*
351		 * search dlopened objects: global or req_obj == dlopened_obj
352		 * and and it's children
353		 */
354		TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) {
355			if (((n->data->obj_flags & RTLD_GLOBAL) == 0) &&
356			    (n->data != req_obj->load_object))
357				continue;
358
359			TAILQ_FOREACH(m, &n->data->dload_list, next_sib) {
360				if (skip == 1) {
361					if (m->data == req_obj) {
362						skip = 0;
363						if (flags & SYM_SEARCH_NEXT)
364							continue;
365					} else
366						continue;
367				}
368				if ((flags & SYM_SEARCH_OTHER) &&
369				    (m->data == req_obj))
370					continue;
371				if (_dl_find_symbol_obj(m->data, name, h, flags,
372				    this, &weak_sym, &weak_object)) {
373					object = m->data;
374					found = 1;
375					goto found;
376				}
377			}
378		}
379	}
380
381found:
382	if (weak_object != NULL && found == 0) {
383		object=weak_object;
384		*this = weak_sym;
385		found = 1;
386	}
387
388
389	if (found == 0) {
390		if ((ref_sym == NULL ||
391		    (ELF_ST_BIND(ref_sym->st_info) != STB_WEAK)) &&
392		    (flags & SYM_WARNNOTFOUND))
393			_dl_printf("%s:%s: undefined symbol '%s'\n",
394			    _dl_progname, req_obj->load_name, name);
395		return (0);
396	}
397
398	if (ref_sym != NULL && ref_sym->st_size != 0 &&
399	    (ref_sym->st_size != (*this)->st_size)  &&
400	    (ELF_ST_TYPE((*this)->st_info) != STT_FUNC) ) {
401		_dl_printf("%s:%s: %s : WARNING: "
402		    "symbol(%s) size mismatch, relink your program\n",
403		    _dl_progname, req_obj->load_name,
404		    object->load_name, name);
405	}
406
407	if (pobj)
408		*pobj = object;
409
410	return (object->load_offs);
411}
412
413int
414_dl_find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash,
415    int flags, const Elf_Sym **this, const Elf_Sym **weak_sym,
416    elf_object_t **weak_object)
417{
418	const Elf_Sym	*symt = object->dyn.symtab;
419	const char	*strt = object->dyn.strtab;
420	long	si;
421	const char *symn;
422
423	for (si = object->buckets[hash % object->nbuckets];
424	    si != STN_UNDEF; si = object->chains[si]) {
425		const Elf_Sym *sym = symt + si;
426
427		if (sym->st_value == 0)
428			continue;
429
430		if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
431		    ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
432		    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
433			continue;
434
435		symn = strt + sym->st_name;
436		if (sym != *this && _dl_strcmp(symn, name))
437			continue;
438
439		/* allow this symbol if we are referring to a function
440		 * which has a value, even if section is UNDEF.
441		 * this allows &func to refer to PLT as per the
442		 * ELF spec. st_value is checked above.
443		 * if flags has SYM_PLT set, we must have actual
444		 * symbol, so this symbol is skipped.
445		 */
446		if (sym->st_shndx == SHN_UNDEF) {
447			if ((flags & SYM_PLT) || sym->st_value == 0 ||
448			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
449				continue;
450		}
451
452		if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
453			*this = sym;
454			return 1;
455		} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
456			if (!*weak_sym) {
457				*weak_sym = sym;
458				*weak_object = object;
459			}
460		}
461	}
462	return 0;
463}
464