xref: /openbsd-src/libexec/ld.so/resolve.c (revision cce76cd5870e881eac2963c65f3440b2f17333f4)
1 /*	$OpenBSD: resolve.c,v 1.54 2011/05/22 22:43:47 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 #include "dl_prebind.h"
39 
40 elf_object_t *_dl_objects;
41 elf_object_t *_dl_last_object;
42 elf_object_t *_dl_loading_object;
43 
44 /*
45  * Add a new dynamic object to the object list.
46  */
47 void
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  */
72 elf_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 
169 void
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 
181 elf_object_t *free_objects;
182 
183 void _dl_cleanup_objects(void);
184 void
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 
216 struct dep_node_head _dlsym_search_list;
217 int _dl_search_list_valid = 0;
218 
219 void
220 _dl_rebuild_allobj_grouplist()
221 {
222 	struct dep_node *n, *m;
223 	int global;
224 	static int maxgrouplist = 0;
225 	static int maxchildlist = 0;
226 	int childlistlen = 0, grouplistlen = 0;
227 
228 	DL_DEB(("rebuil\n"));
229 
230 	/* get rid of old list */
231 	while( (n = TAILQ_FIRST(&_dlsym_search_list)) != NULL) {
232 		TAILQ_REMOVE(&_dlsym_search_list, n, next_sib);
233 		n->data->obj_global = 0; /* clear the cached global flag */
234 		_dl_free(n);
235 	}
236 
237 	/* rebuild list */
238 	_dl_newsymsearch();
239 	/*
240 	 * search dlopened objects: global or req_obj == dlopened_obj
241 	 * and and it's children
242 	 */
243 
244 	 _dl_newsymsearch();
245 
246 	TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) {
247 		childlistlen++;
248 #if 0
249 		DL_DEB(("opened list: %s\n", n->data->load_name));
250 #endif
251 		global = n->data->obj_flags & RTLD_GLOBAL;
252 
253 		if (n->data->lastlookup == _dl_searchnum)
254 			continue;
255 
256 		grouplistlen = 0;
257 		TAILQ_FOREACH(m, &n->data->grpsym_list, next_sib) {
258 			grouplistlen++;
259 			if (m->data->lastlookup == _dl_searchnum)
260 				continue;
261 			if (!global && m->data != n->data) {
262 				continue;
263 			}
264 			m->data->obj_global |= global & RTLD_GLOBAL;
265 
266 			_dl_append_search(m->data);
267 		}
268 	}
269 	if (grouplistlen > maxgrouplist) {
270 		maxgrouplist = grouplistlen ;
271 		DL_DEB(("maxgrouplist = %d\n", maxgrouplist));
272 	}
273 	if (childlistlen > maxchildlist) {
274 		maxchildlist = childlistlen;
275 		DL_DEB(("maxchildlist = %d\n", maxchildlist));
276 	}
277 
278 #if 0
279 	TAILQ_FOREACH(n, &_dlsym_search_list, next_sib) {
280 		DL_DEB(("objects: %s global %d\n",
281 		    n->data->load_name,
282 		    n->data->obj_global));
283 	}
284 #endif
285 
286 	_dl_search_list_valid = 1;
287 }
288 
289 void
290 _dl_append_search(elf_object_t *object)
291 {
292 	struct dep_node *n;
293 	n = _dl_malloc(sizeof *n);
294 	n->data = object;
295 
296 	object->lastlookup = _dl_searchnum;
297 	TAILQ_INSERT_TAIL(&_dlsym_search_list, n, next_sib);
298 }
299 
300 void
301 _dl_remove_object(elf_object_t *object)
302 {
303 	object->prev->next = object->next;
304 	if (object->next)
305 		object->next->prev = object->prev;
306 
307 	if (_dl_last_object == object)
308 		_dl_last_object = object->prev;
309 
310 	object->next = free_objects;
311 	free_objects = object;
312 }
313 
314 
315 int _dl_find_symbol_obj(elf_object_t *object, const char *name,
316     unsigned long hash, int flags, const Elf_Sym **ref,
317     const Elf_Sym **weak_sym,
318     elf_object_t **weak_object);
319 
320 sym_cache *_dl_symcache;
321 int _dl_symcachestat_hits;
322 int _dl_symcachestat_lookups;
323 
324 
325 Elf_Addr
326 _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
327     const Elf_Sym **this, int flags, const Elf_Sym *ref_sym, const elf_object_t **pobj)
328 {
329 	Elf_Addr ret;
330 	const Elf_Sym *sym;
331 	const char *symn;
332 	const elf_object_t *sobj;
333 
334 	_dl_symcachestat_lookups ++;
335 	if (_dl_symcache != NULL &&
336 	    symidx < req_obj->nchains &&
337 	    _dl_symcache[symidx].obj != NULL &&
338 	    _dl_symcache[symidx].sym != NULL &&
339 	    _dl_symcache[symidx].flags == flags) {
340 
341 		_dl_symcachestat_hits++;
342 		sobj = _dl_symcache[symidx].obj;
343 		*this = _dl_symcache[symidx].sym;
344 		if (pobj)
345 			*pobj = sobj;
346 		if (_dl_prebind_validate) /* XXX */
347 			prebind_validate(req_obj, symidx, flags, ref_sym);
348 		return sobj->obj_base;
349 	}
350 
351 	sym = req_obj->dyn.symtab;
352 	sym += symidx;
353 	symn = req_obj->dyn.strtab + sym->st_name;
354 
355 	ret = _dl_find_symbol(symn, this, flags, ref_sym, req_obj, &sobj);
356 
357 	if (pobj)
358 		*pobj = sobj;
359 
360 	if (_dl_symcache != NULL && symidx < req_obj->nchains) {
361 #if 0
362 		DL_DEB(("cache miss %d %p %p, %p %p %s %s %d %d %s\n",
363 		    symidx,
364 		    _dl_symcache[symidx].sym, *this,
365 		    _dl_symcache[symidx].obj, sobj, sobj->load_name,
366 		    sobj->dyn.strtab + (*this)->st_name,
367 		    _dl_symcache[symidx].flags, flags, req_obj->load_name));
368 #endif
369 
370 		_dl_symcache[symidx].sym = *this;
371 		_dl_symcache[symidx].obj = sobj;
372 		_dl_symcache[symidx].flags = flags;
373 	}
374 
375 	return ret;
376 }
377 
378 int _dl_searchnum = 0;
379 void
380 _dl_newsymsearch(void)
381 {
382 	_dl_searchnum += 1;
383 
384 	if (_dl_searchnum < 0) {
385 		/*
386 		 * If the signed number rolls over, reset all counters so
387 		 * we dont get accidental collision.
388 		 */
389 		elf_object_t *walkobj;
390 		for (walkobj = _dl_objects;
391 		    walkobj != NULL;
392 		    walkobj = walkobj->next) {
393 			walkobj->lastlookup = 0;
394 		}
395 		_dl_searchnum = 1;
396 	}
397 }
398 
399 Elf_Addr
400 _dl_find_symbol(const char *name, const Elf_Sym **this,
401     int flags, const Elf_Sym *ref_sym, elf_object_t *req_obj,
402     const elf_object_t **pobj)
403 {
404 	const Elf_Sym *weak_sym = NULL;
405 	unsigned long h = 0;
406 	const char *p = name;
407 	elf_object_t *object = NULL, *weak_object = NULL;
408 	int found = 0;
409 	int visit = 0;
410 	static int maxvisit = 0;
411 	struct dep_node *n, *m;
412 
413 
414 	while (*p) {
415 		unsigned long g;
416 		h = (h << 4) + *p++;
417 		if ((g = h & 0xf0000000))
418 			h ^= g >> 24;
419 		h &= ~g;
420 	}
421 
422 	if (req_obj->dyn.symbolic)
423 		if (_dl_find_symbol_obj(req_obj, name, h, flags, this, &weak_sym,
424 		    &weak_object)) {
425 			object = req_obj;
426 			found = 1;
427 			goto found;
428 		}
429 
430 	if (flags & SYM_SEARCH_OBJ) {
431 		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
432 		    &weak_sym, &weak_object)) {
433 			object = req_obj;
434 			found = 1;
435 		}
436 	} else if (flags & SYM_DLSYM) {
437 		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
438 		    &weak_sym, &weak_object)) {
439 			object = req_obj;
440 			found = 1;
441 		}
442 		if (weak_object != NULL && found == 0) {
443 			object=weak_object;
444 			*this = weak_sym;
445 			found = 1;
446 		}
447 		/* search dlopened obj and all children */
448 
449 		if (found == 0) {
450 			TAILQ_FOREACH(n, &req_obj->load_object->grpsym_list,
451 			    next_sib) {
452 				if (_dl_find_symbol_obj(n->data, name, h,
453 				    flags, this,
454 				    &weak_sym, &weak_object)) {
455 					object = n->data;
456 					found = 1;
457 					break;
458 				}
459 			}
460 		}
461 	} else if ((flags & (SYM_SEARCH_NEXT|SYM_SEARCH_SELF|SYM_SEARCH_OTHER))) {
462 		int skip = 0;
463 
464 		if ((flags & SYM_SEARCH_SELF) || (flags & SYM_SEARCH_NEXT))
465 			skip = 1;
466 
467 		/*
468 		 * search dlopened objects: global or req_obj == dlopened_obj
469 		 * and and it's children
470 		 */
471 		TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) {
472 			if (((n->data->obj_flags & RTLD_GLOBAL) == 0) &&
473 			    (n->data != req_obj->load_object))
474 				continue;
475 
476 			TAILQ_FOREACH(m, &n->data->grpsym_list, next_sib) {
477 				if (skip == 1) {
478 					if (m->data == req_obj) {
479 						skip = 0;
480 						if (flags & SYM_SEARCH_NEXT)
481 							continue;
482 					} else
483 						continue;
484 				}
485 				if ((flags & SYM_SEARCH_OTHER) &&
486 				    (m->data == req_obj))
487 					continue;
488 				if (_dl_find_symbol_obj(m->data, name, h, flags,
489 				    this, &weak_sym, &weak_object)) {
490 					object = m->data;
491 					found = 1;
492 					goto found;
493 				}
494 			}
495 		}
496 	} else {
497 		if (_dl_search_list_valid == 0) {
498 			_dl_rebuild_allobj_grouplist();
499 		}
500 
501 		TAILQ_FOREACH(n, &_dlsym_search_list, next_sib) {
502 			if (n->data == req_obj->load_object) {
503 				TAILQ_FOREACH(m, &n->data->grpsym_list,
504 				    next_sib) {
505 					visit++;
506 					if (_dl_find_symbol_obj(m->data, name,
507 					    h, flags, this, &weak_sym,
508 					    &weak_object)) {
509 						object = m->data;
510 						found = 1;
511 						goto found;
512 					}
513 				}
514 			}
515 			if (((n->data->obj_global & RTLD_GLOBAL) == 0) &&
516 			    (n->data != req_obj->load_object))
517 				continue;
518 
519 			//DL_DEB(("searching for %s in %s\n", name, n->data->load_name));
520 			visit++;
521 			if (_dl_find_symbol_obj(n->data, name, h, flags,
522 			    this, &weak_sym, &weak_object)) {
523 				object = n->data;
524 				found = 1;
525 				DL_DEB(("sym %s is in %s\n", name, object->load_name));
526 				goto found;
527 			}
528 		}
529 	}
530 
531 found:
532 	if (visit > maxvisit) {
533 		maxvisit = visit;
534 		DL_DEB(("maxvisit is %d\n", maxvisit));
535 	}
536 	if (weak_object != NULL && found == 0) {
537 		object=weak_object;
538 		*this = weak_sym;
539 		found = 1;
540 	}
541 
542 
543 	if (found == 0) {
544 		if ((ref_sym == NULL ||
545 		    (ELF_ST_BIND(ref_sym->st_info) != STB_WEAK)) &&
546 		    (flags & SYM_WARNNOTFOUND))
547 			_dl_printf("%s:%s: undefined symbol '%s'\n",
548 			    _dl_progname, req_obj->load_name, name);
549 		return (0);
550 	}
551 
552 	if (ref_sym != NULL && ref_sym->st_size != 0 &&
553 	    (ref_sym->st_size != (*this)->st_size)  &&
554 	    (ELF_ST_TYPE((*this)->st_info) != STT_FUNC) ) {
555 		_dl_printf("%s:%s: %s : WARNING: "
556 		    "symbol(%s) size mismatch, relink your program\n",
557 		    _dl_progname, req_obj->load_name,
558 		    object->load_name, name);
559 	}
560 
561 	if (pobj)
562 		*pobj = object;
563 
564 	return (object->obj_base);
565 }
566 
567 int
568 _dl_find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash,
569     int flags, const Elf_Sym **this, const Elf_Sym **weak_sym,
570     elf_object_t **weak_object)
571 {
572 	const Elf_Sym	*symt = object->dyn.symtab;
573 	const char	*strt = object->dyn.strtab;
574 	long	si;
575 	const char *symn;
576 
577 	for (si = object->buckets[hash % object->nbuckets];
578 	    si != STN_UNDEF; si = object->chains[si]) {
579 		const Elf_Sym *sym = symt + si;
580 
581 		if (sym->st_value == 0)
582 			continue;
583 
584 		if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
585 		    ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
586 		    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
587 			continue;
588 
589 		symn = strt + sym->st_name;
590 		if (sym != *this && _dl_strcmp(symn, name))
591 			continue;
592 
593 		/* allow this symbol if we are referring to a function
594 		 * which has a value, even if section is UNDEF.
595 		 * this allows &func to refer to PLT as per the
596 		 * ELF spec. st_value is checked above.
597 		 * if flags has SYM_PLT set, we must have actual
598 		 * symbol, so this symbol is skipped.
599 		 */
600 		if (sym->st_shndx == SHN_UNDEF) {
601 			if ((flags & SYM_PLT) || sym->st_value == 0 ||
602 			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
603 				continue;
604 		}
605 
606 		if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
607 			*this = sym;
608 			return 1;
609 		} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
610 			if (!*weak_sym) {
611 				*weak_sym = sym;
612 				*weak_object = object;
613 			}
614 		}
615 	}
616 	return 0;
617 }
618 
619 void
620 _dl_debug_state(void)
621 {
622         /* Debugger stub */
623 }
624