resolve.c revision 1.61
1/*	$OpenBSD: resolve.c,v 1.61 2013/04/05 12:58:03 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 <limits.h>
34#include <nlist.h>
35#include <link.h>
36#include "syscall.h"
37#include "archdep.h"
38#include "path.h"
39#include "resolve.h"
40#include "dl_prebind.h"
41
42/* substitution types */
43typedef enum {
44        SUBST_UNKNOWN, SUBST_ORIGIN, SUBST_OSNAME, SUBST_OSREL, SUBST_PLATFORM
45} SUBST_TYPES;
46
47elf_object_t *_dl_objects;
48elf_object_t *_dl_last_object;
49elf_object_t *_dl_loading_object;
50
51/*
52 * Add a new dynamic object to the object list.
53 */
54void
55_dl_add_object(elf_object_t *object)
56{
57	/* if a .so is marked nodelete, then add a reference */
58	if (object->obj_flags & DF_1_NODELETE &&
59	    (object->status & STAT_NODELETE) == 0) {
60		DL_DEB(("objname %s is nodelete\n", object->load_name));
61		object->refcount++;
62		object->status |= STAT_NODELETE;
63	}
64
65	/*
66	 * if this is a new object, prev will be NULL
67	 * != NULL if an object already in the list
68	 * prev == NULL for the first item in the list, but that will
69	 * be the executable.
70	 */
71	if (object->prev != NULL)
72		return;
73
74	if (_dl_objects == NULL) {			/* First object ? */
75		_dl_last_object = _dl_objects = object;
76	} else {
77		_dl_last_object->next = object;
78		object->prev = _dl_last_object;
79		_dl_last_object = object;
80	}
81}
82
83/*
84 * Identify substitution sequence name.
85 */
86static int
87_dl_subst_name(const char *name, size_t siz) {
88	switch (siz) {
89	case 5:
90		if (_dl_strncmp(name, "OSREL", 5) == 0)
91			return SUBST_OSREL;
92		break;
93	case 6:
94		if (_dl_strncmp(name, "ORIGIN", 6) == 0)
95			return SUBST_ORIGIN;
96		if (_dl_strncmp(name, "OSNAME", 6) == 0)
97			return SUBST_OSNAME;
98		break;
99	case 8:
100		if (_dl_strncmp(name, "PLATFORM", 8) == 0)
101			return SUBST_PLATFORM;
102		break;
103	}
104
105	return (SUBST_UNKNOWN);
106}
107
108/*
109 * Perform $ORIGIN substitutions on path
110 */
111static void
112_dl_origin_subst_path(elf_object_t *object, const char *origin_path,
113    char **path)
114{
115	char tmp_path[PATH_MAX];
116	char *new_path, *tp;
117	const char *pp, *name, *value;
118	static struct utsname uts;
119	size_t value_len;
120	int skip_brace;
121
122	if (uts.sysname[0] == '\0') {
123		if (_dl_uname(&uts) != 0)
124			return;
125	}
126
127	tp = tmp_path;
128	pp = *path;
129
130	while (*pp != '\0' && (tp - tmp_path) < sizeof(tmp_path)) {
131
132		/* copy over chars up to but not including $ */
133		while (*pp != '\0' && *pp != '$' &&
134		    (tp - tmp_path) < sizeof(tmp_path))
135			*tp++ = *pp++;
136
137		/* substitution sequence detected */
138		if (*pp == '$' && (tp - tmp_path) < sizeof(tmp_path)) {
139			pp++;
140
141			if ((skip_brace = (*pp == '{')))
142				pp++;
143
144			/* skip over name */
145			name = pp;
146			while (_dl_isalnum(*pp) || *pp == '_')
147				pp++;
148
149			switch (_dl_subst_name(name, pp - name)) {
150			case SUBST_ORIGIN:
151				value = origin_path;
152				break;
153			case SUBST_OSNAME:
154				value = uts.sysname;
155				break;
156			case SUBST_OSREL:
157				value = uts.release;
158				break;
159			case SUBST_PLATFORM:
160				value = uts.machine;
161				break;
162			default:
163				value = "";
164			}
165
166			value_len = _dl_strlen(value);
167			if (value_len >= sizeof(tmp_path) - (tp - tmp_path))
168				return;
169
170			_dl_bcopy(value, tp, value_len);
171			tp += value_len;
172
173			if (skip_brace && *pp == '}')
174				pp++;
175		}
176	}
177
178	/* no substitution made if result exceeds sizeof(tmp_path) */
179	if (tp - tmp_path >= sizeof(tmp_path))
180		return;
181
182	/* NULL terminate tmp_path */
183        *tp = '\0';
184
185	if (_dl_strcmp(tmp_path, *path) == 0)
186		return;
187
188	new_path = _dl_strdup(tmp_path);
189	if (new_path == NULL)
190		return;
191
192	DL_DEB(("orig_path %s\n", *path));
193	DL_DEB(("new_path  %s\n", new_path));
194
195	_dl_free(*path);
196	*path = new_path;
197}
198
199/*
200 * Determine origin_path from object load_name. The origin_path argument
201 * must refer to a buffer capable of storing at least PATH_MAX characters.
202 * Returns 0 on success.
203 */
204static int
205_dl_origin_path(elf_object_t *object, char *origin_path)
206{
207	const char *dirname_path = _dl_dirname(object->load_name);
208
209	if (dirname_path == NULL)
210		return -1;
211
212	if (_dl_realpath(dirname_path, origin_path) == NULL)
213		return -1;
214
215	return 0;
216}
217
218/*
219 * Perform $ORIGIN substitutions on rpath
220 */
221static void
222_dl_origin_subst(elf_object_t *object)
223{
224	char origin_path[PATH_MAX];
225	char **pp;
226
227	if (_dl_origin_path(object, origin_path) != 0)
228		return;
229
230	/* perform path substitutions on each segment of rpath */
231	for (pp = object->rpath; *pp != NULL; pp++) {
232		_dl_origin_subst_path(object, origin_path, pp);
233	}
234}
235
236/*
237 * Initialize a new dynamic object.
238 */
239elf_object_t *
240_dl_finalize_object(const char *objname, Elf_Dyn *dynp, Elf_Phdr *phdrp,
241    int phdrc, const int objtype, const long lbase, const long obase)
242{
243	elf_object_t *object;
244#if 0
245	_dl_printf("objname [%s], dynp %p, objtype %x lbase %lx, obase %lx\n",
246	    objname, dynp, objtype, lbase, obase);
247#endif
248	object = _dl_malloc(sizeof(elf_object_t));
249	object->prev = object->next = NULL;
250
251	object->load_dyn = dynp;
252	while (dynp->d_tag != DT_NULL) {
253		if (dynp->d_tag < DT_NUM)
254			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
255		else if (dynp->d_tag >= DT_LOPROC &&
256		    dynp->d_tag < DT_LOPROC + DT_PROCNUM)
257			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] =
258			    dynp->d_un.d_val;
259		if (dynp->d_tag == DT_TEXTREL)
260			object->dyn.textrel = 1;
261		if (dynp->d_tag == DT_SYMBOLIC)
262			object->dyn.symbolic = 1;
263		if (dynp->d_tag == DT_BIND_NOW)
264			object->obj_flags |= DF_1_NOW;
265		if (dynp->d_tag == DT_FLAGS_1)
266			object->obj_flags |= dynp->d_un.d_val;
267		dynp++;
268	}
269	DL_DEB((" flags %s = 0x%x\n", objname, object->obj_flags ));
270	object->obj_type = objtype;
271
272	if (_dl_loading_object == NULL) {
273		/*
274		 * no loading object, object is the loading object,
275		 * as it is either executable, or dlopened()
276		 */
277		_dl_loading_object = object;
278	}
279
280	if ((object->obj_flags & DF_1_NOOPEN) != 0 &&
281	    _dl_loading_object->obj_type == OBJTYPE_DLO &&
282	    _dl_traceld == NULL) {
283		_dl_free(object);
284		_dl_errno = DL_CANT_LOAD_OBJ;
285		return(NULL);
286	}
287
288	/*
289	 *  Now relocate all pointer to dynamic info, but only
290	 *  the ones which have pointer values.
291	 */
292	if (object->Dyn.info[DT_PLTGOT])
293		object->Dyn.info[DT_PLTGOT] += obase;
294	if (object->Dyn.info[DT_HASH])
295		object->Dyn.info[DT_HASH] += obase;
296	if (object->Dyn.info[DT_STRTAB])
297		object->Dyn.info[DT_STRTAB] += obase;
298	if (object->Dyn.info[DT_SYMTAB])
299		object->Dyn.info[DT_SYMTAB] += obase;
300	if (object->Dyn.info[DT_RELA])
301		object->Dyn.info[DT_RELA] += obase;
302	if (object->Dyn.info[DT_SONAME])
303		object->Dyn.info[DT_SONAME] += object->Dyn.info[DT_STRTAB];
304	if (object->Dyn.info[DT_RPATH])
305		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
306	if (object->Dyn.info[DT_REL])
307		object->Dyn.info[DT_REL] += obase;
308	if (object->Dyn.info[DT_INIT])
309		object->Dyn.info[DT_INIT] += obase;
310	if (object->Dyn.info[DT_FINI])
311		object->Dyn.info[DT_FINI] += obase;
312	if (object->Dyn.info[DT_JMPREL])
313		object->Dyn.info[DT_JMPREL] += obase;
314
315	if (object->Dyn.info[DT_HASH] != 0) {
316		Elf_Word *hashtab = (Elf_Word *)object->Dyn.info[DT_HASH];
317
318		object->nbuckets = hashtab[0];
319		object->nchains = hashtab[1];
320		object->buckets = hashtab + 2;
321		object->chains = object->buckets + object->nbuckets;
322	}
323
324	object->phdrp = phdrp;
325	object->phdrc = phdrc;
326	object->load_base = lbase;
327	object->obj_base = obase;
328	object->load_name = _dl_strdup(objname);
329	object->load_object = _dl_loading_object;
330	if (object->load_object == object)
331		DL_DEB(("head %s\n", object->load_name));
332	DL_DEB(("obj %s has %s as head\n", object->load_name,
333	    _dl_loading_object->load_name ));
334	object->refcount = 0;
335	TAILQ_INIT(&object->child_list);
336	object->opencount = 0;	/* # dlopen() & exe */
337	object->grprefcount = 0;
338	/* default dev, inode for dlopen-able objects. */
339	object->dev = 0;
340	object->inode = 0;
341	object->lastlookup = 0;
342	TAILQ_INIT(&object->grpsym_list);
343	TAILQ_INIT(&object->grpref_list);
344
345	if (object->dyn.rpath) {
346		object->rpath = _dl_split_path(object->dyn.rpath);
347		if ((object->obj_flags & DF_1_ORIGIN) && _dl_trust)
348			_dl_origin_subst(object);
349	}
350
351	return (object);
352}
353
354void
355_dl_tailq_free(struct dep_node *n)
356{
357	struct dep_node *next;
358
359	while (n != NULL) {
360		next = TAILQ_NEXT(n, next_sib);
361		_dl_free(n);
362		n = next;
363	}
364}
365
366elf_object_t *free_objects;
367
368void _dl_cleanup_objects(void);
369void
370_dl_cleanup_objects()
371{
372	elf_object_t *nobj, *head;
373	struct dep_node *n, *next;
374
375	n = TAILQ_FIRST(&_dlopened_child_list);
376	while (n != NULL) {
377		next = TAILQ_NEXT(n, next_sib);
378		if (OBJECT_DLREF_CNT(n->data) == 0) {
379			TAILQ_REMOVE(&_dlopened_child_list, n, next_sib);
380			_dl_free(n);
381		}
382		n = next;
383	}
384
385	head = free_objects;
386	free_objects = NULL;
387	while (head != NULL) {
388		if (head->load_name)
389			_dl_free(head->load_name);
390		if (head->sod.sod_name)
391			_dl_free((char *)head->sod.sod_name);
392		if (head->rpath)
393			_dl_free_path(head->rpath);
394		_dl_tailq_free(TAILQ_FIRST(&head->grpsym_list));
395		_dl_tailq_free(TAILQ_FIRST(&head->child_list));
396		_dl_tailq_free(TAILQ_FIRST(&head->grpref_list));
397		nobj = head->next;
398		_dl_free(head);
399		head = nobj;
400	}
401}
402
403void
404_dl_remove_object(elf_object_t *object)
405{
406	object->prev->next = object->next;
407	if (object->next)
408		object->next->prev = object->prev;
409
410	if (_dl_last_object == object)
411		_dl_last_object = object->prev;
412
413	object->next = free_objects;
414	free_objects = object;
415}
416
417
418int _dl_find_symbol_obj(elf_object_t *object, const char *name,
419    unsigned long hash, int flags, const Elf_Sym **ref,
420    const Elf_Sym **weak_sym,
421    elf_object_t **weak_object);
422
423sym_cache *_dl_symcache;
424int _dl_symcachestat_hits;
425int _dl_symcachestat_lookups;
426
427
428Elf_Addr
429_dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
430    const Elf_Sym **this, int flags, const Elf_Sym *ref_sym, const elf_object_t **pobj)
431{
432	Elf_Addr ret;
433	const Elf_Sym *sym;
434	const char *symn;
435	const elf_object_t *sobj;
436
437	_dl_symcachestat_lookups ++;
438	if (_dl_symcache != NULL &&
439	    symidx < req_obj->nchains &&
440	    _dl_symcache[symidx].obj != NULL &&
441	    _dl_symcache[symidx].sym != NULL &&
442	    _dl_symcache[symidx].flags == flags) {
443
444		_dl_symcachestat_hits++;
445		sobj = _dl_symcache[symidx].obj;
446		*this = _dl_symcache[symidx].sym;
447		if (pobj)
448			*pobj = sobj;
449		if (_dl_prebind_validate) /* XXX */
450			prebind_validate(req_obj, symidx, flags, ref_sym);
451		return sobj->obj_base;
452	}
453
454	sym = req_obj->dyn.symtab;
455	sym += symidx;
456	symn = req_obj->dyn.strtab + sym->st_name;
457
458	ret = _dl_find_symbol(symn, this, flags, ref_sym, req_obj, &sobj);
459
460	if (pobj)
461		*pobj = sobj;
462
463	if (_dl_symcache != NULL && symidx < req_obj->nchains) {
464#if 0
465		DL_DEB(("cache miss %d %p %p, %p %p %s %s %d %d %s\n",
466		    symidx,
467		    _dl_symcache[symidx].sym, *this,
468		    _dl_symcache[symidx].obj, sobj, sobj->load_name,
469		    sobj->dyn.strtab + (*this)->st_name,
470		    _dl_symcache[symidx].flags, flags, req_obj->load_name));
471#endif
472
473		_dl_symcache[symidx].sym = *this;
474		_dl_symcache[symidx].obj = sobj;
475		_dl_symcache[symidx].flags = flags;
476	}
477
478	return ret;
479}
480
481int _dl_searchnum = 0;
482void
483_dl_newsymsearch(void)
484{
485	_dl_searchnum += 1;
486
487	if (_dl_searchnum < 0) {
488		/*
489		 * If the signed number rolls over, reset all counters so
490		 * we dont get accidental collision.
491		 */
492		elf_object_t *walkobj;
493		for (walkobj = _dl_objects;
494		    walkobj != NULL;
495		    walkobj = walkobj->next) {
496			walkobj->lastlookup = 0;
497		}
498		_dl_searchnum = 1;
499	}
500}
501
502Elf_Addr
503_dl_find_symbol(const char *name, const Elf_Sym **this,
504    int flags, const Elf_Sym *ref_sym, elf_object_t *req_obj,
505    const elf_object_t **pobj)
506{
507	const Elf_Sym *weak_sym = NULL;
508	unsigned long h = 0;
509	const char *p = name;
510	elf_object_t *object = NULL, *weak_object = NULL;
511	int found = 0;
512	struct dep_node *n, *m;
513
514
515	while (*p) {
516		unsigned long g;
517		h = (h << 4) + *p++;
518		if ((g = h & 0xf0000000))
519			h ^= g >> 24;
520		h &= ~g;
521	}
522
523	if (req_obj->dyn.symbolic)
524		if (_dl_find_symbol_obj(req_obj, name, h, flags, this, &weak_sym,
525		    &weak_object)) {
526			object = req_obj;
527			found = 1;
528			goto found;
529		}
530
531	if (flags & SYM_SEARCH_OBJ) {
532		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
533		    &weak_sym, &weak_object)) {
534			object = req_obj;
535			found = 1;
536		}
537	} else if (flags & SYM_DLSYM) {
538		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
539		    &weak_sym, &weak_object)) {
540			object = req_obj;
541			found = 1;
542		}
543		if (weak_object != NULL && found == 0) {
544			object=weak_object;
545			*this = weak_sym;
546			found = 1;
547		}
548		/* search dlopened obj and all children */
549
550		if (found == 0) {
551			TAILQ_FOREACH(n, &req_obj->load_object->grpsym_list,
552			    next_sib) {
553				if (_dl_find_symbol_obj(n->data, name, h,
554				    flags, this,
555				    &weak_sym, &weak_object)) {
556					object = n->data;
557					found = 1;
558					break;
559				}
560			}
561		}
562	} else {
563		int skip = 0;
564
565		if ((flags & SYM_SEARCH_SELF) || (flags & SYM_SEARCH_NEXT))
566			skip = 1;
567
568		_dl_newsymsearch();
569
570		/*
571		 * search dlopened objects: global or req_obj == dlopened_obj
572		 * and and it's children
573		 */
574		TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) {
575			if (((n->data->obj_flags & DF_1_GLOBAL) == 0) &&
576			    (n->data != req_obj->load_object))
577				continue;
578
579			n->data->lastlookup_head = _dl_searchnum;
580			TAILQ_FOREACH(m, &n->data->grpsym_list, next_sib) {
581				if (skip == 1) {
582					if (m->data == req_obj) {
583						skip = 0;
584						if (flags & SYM_SEARCH_NEXT)
585							continue;
586					} else
587						continue;
588				}
589				if ((flags & SYM_SEARCH_OTHER) &&
590				    (m->data == req_obj))
591					continue;
592				m->data->lastlookup = _dl_searchnum;
593				if (_dl_find_symbol_obj(m->data, name, h, flags,
594				    this, &weak_sym, &weak_object)) {
595					object = m->data;
596					found = 1;
597					goto found;
598				}
599			}
600		}
601	}
602
603found:
604	if (weak_object != NULL && found == 0) {
605		object=weak_object;
606		*this = weak_sym;
607		found = 1;
608	}
609
610
611	if (found == 0) {
612		if ((ref_sym == NULL ||
613		    (ELF_ST_BIND(ref_sym->st_info) != STB_WEAK)) &&
614		    (flags & SYM_WARNNOTFOUND))
615			_dl_printf("%s:%s: undefined symbol '%s'\n",
616			    _dl_progname, req_obj->load_name, name);
617		return (0);
618	}
619
620	if (ref_sym != NULL && ref_sym->st_size != 0 &&
621	    (ref_sym->st_size != (*this)->st_size)  &&
622	    (ELF_ST_TYPE((*this)->st_info) != STT_FUNC) ) {
623		_dl_printf("%s:%s: %s : WARNING: "
624		    "symbol(%s) size mismatch, relink your program\n",
625		    _dl_progname, req_obj->load_name,
626		    object->load_name, name);
627	}
628
629	if (pobj)
630		*pobj = object;
631
632	return (object->obj_base);
633}
634
635int
636_dl_find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash,
637    int flags, const Elf_Sym **this, const Elf_Sym **weak_sym,
638    elf_object_t **weak_object)
639{
640	const Elf_Sym	*symt = object->dyn.symtab;
641	const char	*strt = object->dyn.strtab;
642	long	si;
643	const char *symn;
644
645	for (si = object->buckets[hash % object->nbuckets];
646	    si != STN_UNDEF; si = object->chains[si]) {
647		const Elf_Sym *sym = symt + si;
648
649		if (sym->st_value == 0)
650			continue;
651
652		if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
653		    ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
654		    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
655			continue;
656
657		symn = strt + sym->st_name;
658		if (sym != *this && _dl_strcmp(symn, name))
659			continue;
660
661		/* allow this symbol if we are referring to a function
662		 * which has a value, even if section is UNDEF.
663		 * this allows &func to refer to PLT as per the
664		 * ELF spec. st_value is checked above.
665		 * if flags has SYM_PLT set, we must have actual
666		 * symbol, so this symbol is skipped.
667		 */
668		if (sym->st_shndx == SHN_UNDEF) {
669			if ((flags & SYM_PLT) || sym->st_value == 0 ||
670			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
671				continue;
672		}
673
674		if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
675			*this = sym;
676			return 1;
677		} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
678			if (!*weak_sym) {
679				*weak_sym = sym;
680				*weak_object = object;
681			}
682		}
683	}
684	return 0;
685}
686
687void
688_dl_debug_state(void)
689{
690        /* Debugger stub */
691}
692