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