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