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