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