xref: /openbsd-src/libexec/ld.so/resolve.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: resolve.c,v 1.75 2016/08/23 06:46:17 kettenis 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 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 rpath */
236 	for (pp = object->rpath; *pp != NULL; pp++) {
237 		_dl_origin_subst_path(object, origin_path, pp);
238 	}
239 }
240 
241 /*
242  * Initialize a new dynamic object.
243  */
244 elf_object_t *
245 _dl_finalize_object(const char *objname, Elf_Dyn *dynp, Elf_Phdr *phdrp,
246     int phdrc, const int objtype, const long lbase, const long obase)
247 {
248 	elf_object_t *object;
249 
250 #if 0
251 	_dl_printf("objname [%s], dynp %p, objtype %x lbase %lx, obase %lx\n",
252 	    objname, dynp, objtype, lbase, obase);
253 #endif
254 	object = _dl_calloc(1, sizeof(elf_object_t));
255 	if (object == NULL)
256 		_dl_exit(7);
257 	object->prev = object->next = NULL;
258 
259 	object->load_dyn = dynp;
260 	while (dynp->d_tag != DT_NULL) {
261 		if (dynp->d_tag < DT_NUM)
262 			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
263 		else if (dynp->d_tag >= DT_LOPROC &&
264 		    dynp->d_tag < DT_LOPROC + DT_PROCNUM)
265 			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] =
266 			    dynp->d_un.d_val;
267 		if (dynp->d_tag == DT_TEXTREL)
268 			object->dyn.textrel = 1;
269 		if (dynp->d_tag == DT_SYMBOLIC)
270 			object->dyn.symbolic = 1;
271 		if (dynp->d_tag == DT_BIND_NOW)
272 			object->obj_flags |= DF_1_NOW;
273 		if (dynp->d_tag == DT_FLAGS_1)
274 			object->obj_flags |= dynp->d_un.d_val;
275 		if (dynp->d_tag == DT_RELACOUNT)
276 			object->relacount = dynp->d_un.d_val;
277 		if (dynp->d_tag == DT_RELCOUNT)
278 			object->relcount = dynp->d_un.d_val;
279 		dynp++;
280 	}
281 	DL_DEB((" flags %s = 0x%x\n", objname, object->obj_flags ));
282 	object->obj_type = objtype;
283 
284 	if (_dl_loading_object == NULL) {
285 		/*
286 		 * no loading object, object is the loading object,
287 		 * as it is either executable, or dlopened()
288 		 */
289 		_dl_loading_object = object;
290 	}
291 
292 	if ((object->obj_flags & DF_1_NOOPEN) != 0 &&
293 	    _dl_loading_object->obj_type == OBJTYPE_DLO &&
294 	    _dl_traceld == NULL) {
295 		_dl_free(object);
296 		_dl_errno = DL_CANT_LOAD_OBJ;
297 		return(NULL);
298 	}
299 
300 	/*
301 	 *  Now relocate all pointer to dynamic info, but only
302 	 *  the ones which have pointer values.
303 	 */
304 	if (object->Dyn.info[DT_PLTGOT])
305 		object->Dyn.info[DT_PLTGOT] += obase;
306 	if (object->Dyn.info[DT_HASH])
307 		object->Dyn.info[DT_HASH] += obase;
308 	if (object->Dyn.info[DT_STRTAB])
309 		object->Dyn.info[DT_STRTAB] += obase;
310 	if (object->Dyn.info[DT_SYMTAB])
311 		object->Dyn.info[DT_SYMTAB] += obase;
312 	if (object->Dyn.info[DT_RELA])
313 		object->Dyn.info[DT_RELA] += obase;
314 	if (object->Dyn.info[DT_SONAME])
315 		object->Dyn.info[DT_SONAME] += object->Dyn.info[DT_STRTAB];
316 	if (object->Dyn.info[DT_RPATH])
317 		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
318 	if (object->Dyn.info[DT_REL])
319 		object->Dyn.info[DT_REL] += obase;
320 	if (object->Dyn.info[DT_INIT])
321 		object->Dyn.info[DT_INIT] += obase;
322 	if (object->Dyn.info[DT_FINI])
323 		object->Dyn.info[DT_FINI] += obase;
324 	if (object->Dyn.info[DT_JMPREL])
325 		object->Dyn.info[DT_JMPREL] += obase;
326 	if (object->Dyn.info[DT_INIT_ARRAY])
327 		object->Dyn.info[DT_INIT_ARRAY] += obase;
328 	if (object->Dyn.info[DT_FINI_ARRAY])
329 		object->Dyn.info[DT_FINI_ARRAY] += obase;
330 	if (object->Dyn.info[DT_PREINIT_ARRAY])
331 		object->Dyn.info[DT_PREINIT_ARRAY] += obase;
332 
333 	if (object->Dyn.info[DT_HASH] != 0) {
334 		Elf_Word *hashtab = (Elf_Word *)object->Dyn.info[DT_HASH];
335 
336 		object->nbuckets = hashtab[0];
337 		object->nchains = hashtab[1];
338 		object->buckets = hashtab + 2;
339 		object->chains = object->buckets + object->nbuckets;
340 	}
341 
342 	object->phdrp = phdrp;
343 	object->phdrc = phdrc;
344 	object->load_base = lbase;
345 	object->obj_base = obase;
346 	object->load_name = _dl_strdup(objname);
347 	if (object->load_name == NULL)
348 		_dl_exit(7);
349 	object->load_object = _dl_loading_object;
350 	if (object->load_object == object)
351 		DL_DEB(("head %s\n", object->load_name));
352 	DL_DEB(("obj %s has %s as head\n", object->load_name,
353 	    _dl_loading_object->load_name ));
354 	object->refcount = 0;
355 	TAILQ_INIT(&object->child_list);
356 	object->opencount = 0;	/* # dlopen() & exe */
357 	object->grprefcount = 0;
358 	/* default dev, inode for dlopen-able objects. */
359 	object->dev = 0;
360 	object->inode = 0;
361 	object->grpsym_gen = 0;
362 	TAILQ_INIT(&object->grpsym_list);
363 	TAILQ_INIT(&object->grpref_list);
364 
365 	if (object->dyn.rpath) {
366 		object->rpath = _dl_split_path(object->dyn.rpath);
367 		if ((object->obj_flags & DF_1_ORIGIN) && _dl_trust)
368 			_dl_origin_subst(object);
369 	}
370 
371 	_dl_trace_object_setup(object);
372 
373 	return (object);
374 }
375 
376 static void
377 _dl_tailq_free(struct dep_node *n)
378 {
379 	struct dep_node *next;
380 
381 	while (n != NULL) {
382 		next = TAILQ_NEXT(n, next_sib);
383 		_dl_free(n);
384 		n = next;
385 	}
386 }
387 
388 elf_object_t *free_objects;
389 
390 void
391 _dl_cleanup_objects()
392 {
393 	elf_object_t *nobj, *head;
394 	struct dep_node *n, *next;
395 
396 	n = TAILQ_FIRST(&_dlopened_child_list);
397 	while (n != NULL) {
398 		next = TAILQ_NEXT(n, next_sib);
399 		if (OBJECT_DLREF_CNT(n->data) == 0) {
400 			TAILQ_REMOVE(&_dlopened_child_list, n, next_sib);
401 			_dl_free(n);
402 		}
403 		n = next;
404 	}
405 
406 	head = free_objects;
407 	free_objects = NULL;
408 	while (head != NULL) {
409 		if (head->load_name)
410 			_dl_free(head->load_name);
411 		if (head->sod.sod_name)
412 			_dl_free((char *)head->sod.sod_name);
413 		if (head->rpath)
414 			_dl_free_path(head->rpath);
415 		_dl_tailq_free(TAILQ_FIRST(&head->grpsym_list));
416 		_dl_tailq_free(TAILQ_FIRST(&head->child_list));
417 		_dl_tailq_free(TAILQ_FIRST(&head->grpref_list));
418 		nobj = head->next;
419 		_dl_free(head);
420 		head = nobj;
421 	}
422 }
423 
424 void
425 _dl_remove_object(elf_object_t *object)
426 {
427 	object->prev->next = object->next;
428 	if (object->next)
429 		object->next->prev = object->prev;
430 
431 	if (_dl_last_object == object)
432 		_dl_last_object = object->prev;
433 
434 	object->next = free_objects;
435 	free_objects = object;
436 }
437 
438 /*
439  * mprotect a segment to the indicated protection.  If 'addr' is non-zero,
440  * then it's the start address, else the value of 'start_sym' is the start.
441  * The value of 'end_sym' is the end address.  The start is rounded down
442  * and the end is rounded up to page boundaries.  Returns 'addr' or the
443  * address of the start symbol.
444  */
445 void *
446 _dl_protect_segment(elf_object_t *object, Elf_Addr addr,
447     const char *start_sym, const char *end_sym, int prot)
448 {
449 	const Elf_Sym *this;
450 	Elf_Addr ooff, start, end;
451 
452 	if (addr == 0 && start_sym[2] == 'g' &&
453 	    (addr = object->relro_addr) != 0) {
454 		DL_DEB(("protect start RELRO = 0x%lx in %s\n",
455 		    addr, object->load_name));
456 	}
457 	else if (addr == 0) {
458 		this = NULL;
459 		ooff = _dl_find_symbol(start_sym, &this,
460 		    SYM_SEARCH_OBJ | SYM_NOWARNNOTFOUND | SYM_PLT, NULL,
461 		    object, NULL);
462 		/* If not found, nothing to do */
463 		if (this == NULL) {
464 			DL_DEB(("protect start \"%s\" not found in %s\n",
465 			    start_sym, object->load_name));
466 			return (NULL);
467 		}
468 		addr = ooff + this->st_value;
469 		DL_DEB(("protect start \"%s\" to %x = 0x%lx in %s\n",
470 		    start_sym, prot, addr, object->load_name));
471 	}
472 
473 	if (object->relro_addr != 0 && start_sym[2] == 'g') {
474 		end = object->relro_addr + object->relro_size;
475 		DL_DEB(("protect end RELRO = 0x%lx in %s\n",
476 		    end, object->load_name));
477 	} else {
478 		this = NULL;
479 		ooff = _dl_find_symbol(end_sym, &this,
480 		    SYM_SEARCH_OBJ | SYM_NOWARNNOTFOUND | SYM_PLT, NULL,
481 		    object, NULL);
482 		if (this == NULL) {
483 			DL_DEB(("protect end \"%s\" not found in %s\n",
484 			    end_sym, object->load_name));
485 			addr = 0;
486 		} else {
487 			end = ooff + this->st_value;
488 			DL_DEB(("protect end \"%s\" = 0x%lx in %s\n",
489 			    end_sym, end, object->load_name));
490 		}
491 	}
492 
493 	if (addr != 0 && addr < end) {
494 		start = ELF_TRUNC(addr, _dl_pagesz);
495 		end = ELF_ROUND(end, _dl_pagesz);
496 		_dl_mprotect((void *)start, end - start, prot);
497 	}
498 
499 	return ((void *)addr);
500 }
501 
502 
503 sym_cache *_dl_symcache;
504 int _dl_symcachestat_hits;
505 int _dl_symcachestat_lookups;
506 
507 
508 Elf_Addr
509 _dl_find_symbol_bysym(elf_object_t *req_obj, unsigned int symidx,
510     const Elf_Sym **this, int flags, const Elf_Sym *ref_sym, const elf_object_t **pobj)
511 {
512 	Elf_Addr ret;
513 	const Elf_Sym *sym;
514 	const char *symn;
515 	const elf_object_t *sobj;
516 
517 	_dl_symcachestat_lookups ++;
518 	if (_dl_symcache != NULL &&
519 	    symidx < req_obj->nchains &&
520 	    _dl_symcache[symidx].obj != NULL &&
521 	    _dl_symcache[symidx].sym != NULL &&
522 	    _dl_symcache[symidx].flags == flags) {
523 
524 		_dl_symcachestat_hits++;
525 		sobj = _dl_symcache[symidx].obj;
526 		*this = _dl_symcache[symidx].sym;
527 		if (pobj)
528 			*pobj = sobj;
529 		return sobj->obj_base;
530 	}
531 
532 	sym = req_obj->dyn.symtab;
533 	sym += symidx;
534 	symn = req_obj->dyn.strtab + sym->st_name;
535 
536 	ret = _dl_find_symbol(symn, this, flags, ref_sym, req_obj, &sobj);
537 
538 	if (pobj)
539 		*pobj = sobj;
540 
541 	if (_dl_symcache != NULL && symidx < req_obj->nchains) {
542 #if 0
543 		DL_DEB(("cache miss %d %p %p, %p %p %s %s %d %d %s\n",
544 		    symidx,
545 		    _dl_symcache[symidx].sym, *this,
546 		    _dl_symcache[symidx].obj, sobj, sobj->load_name,
547 		    sobj->dyn.strtab + (*this)->st_name,
548 		    _dl_symcache[symidx].flags, flags, req_obj->load_name));
549 #endif
550 
551 		_dl_symcache[symidx].sym = *this;
552 		_dl_symcache[symidx].obj = sobj;
553 		_dl_symcache[symidx].flags = flags;
554 	}
555 
556 	return ret;
557 }
558 
559 static int
560 _dl_find_symbol_obj(elf_object_t *object, const char *name, unsigned long hash,
561     int flags, const Elf_Sym **this, const Elf_Sym **weak_sym,
562     elf_object_t **weak_object)
563 {
564 	const Elf_Sym	*symt = object->dyn.symtab;
565 	const char	*strt = object->dyn.strtab;
566 	long	si;
567 	const char *symn;
568 
569 	for (si = object->buckets[hash % object->nbuckets];
570 	    si != STN_UNDEF; si = object->chains[si]) {
571 		const Elf_Sym *sym = symt + si;
572 
573 		if (sym->st_value == 0)
574 			continue;
575 
576 		if (ELF_ST_TYPE(sym->st_info) != STT_NOTYPE &&
577 		    ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
578 		    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
579 			continue;
580 
581 		symn = strt + sym->st_name;
582 		if (sym != *this && _dl_strcmp(symn, name))
583 			continue;
584 
585 		/* allow this symbol if we are referring to a function
586 		 * which has a value, even if section is UNDEF.
587 		 * this allows &func to refer to PLT as per the
588 		 * ELF spec. st_value is checked above.
589 		 * if flags has SYM_PLT set, we must have actual
590 		 * symbol, so this symbol is skipped.
591 		 */
592 		if (sym->st_shndx == SHN_UNDEF) {
593 			if ((flags & SYM_PLT) || sym->st_value == 0 ||
594 			    ELF_ST_TYPE(sym->st_info) != STT_FUNC)
595 				continue;
596 		}
597 
598 		if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
599 			*this = sym;
600 			return 1;
601 		} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
602 			if (!*weak_sym) {
603 				*weak_sym = sym;
604 				*weak_object = object;
605 			}
606 		}
607 	}
608 	return 0;
609 }
610 
611 Elf_Addr
612 _dl_find_symbol(const char *name, const Elf_Sym **this,
613     int flags, const Elf_Sym *ref_sym, elf_object_t *req_obj,
614     const elf_object_t **pobj)
615 {
616 	const Elf_Sym *weak_sym = NULL;
617 	unsigned long h = 0;
618 	const char *p = name;
619 	elf_object_t *object = NULL, *weak_object = NULL;
620 	int found = 0;
621 	struct dep_node *n, *m;
622 
623 
624 	while (*p) {
625 		unsigned long g;
626 		h = (h << 4) + *p++;
627 		if ((g = h & 0xf0000000))
628 			h ^= g >> 24;
629 		h &= ~g;
630 	}
631 
632 	if (req_obj->dyn.symbolic)
633 		if (_dl_find_symbol_obj(req_obj, name, h, flags, this, &weak_sym,
634 		    &weak_object)) {
635 			object = req_obj;
636 			found = 1;
637 			goto found;
638 		}
639 
640 	if (flags & SYM_SEARCH_OBJ) {
641 		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
642 		    &weak_sym, &weak_object)) {
643 			object = req_obj;
644 			found = 1;
645 		}
646 	} else if (flags & SYM_DLSYM) {
647 		if (_dl_find_symbol_obj(req_obj, name, h, flags, this,
648 		    &weak_sym, &weak_object)) {
649 			object = req_obj;
650 			found = 1;
651 		}
652 		if (weak_object != NULL && found == 0) {
653 			object=weak_object;
654 			*this = weak_sym;
655 			found = 1;
656 		}
657 		/* search dlopened obj and all children */
658 
659 		if (found == 0) {
660 			TAILQ_FOREACH(n, &req_obj->load_object->grpsym_list,
661 			    next_sib) {
662 				if (_dl_find_symbol_obj(n->data, name, h,
663 				    flags, this,
664 				    &weak_sym, &weak_object)) {
665 					object = n->data;
666 					found = 1;
667 					break;
668 				}
669 			}
670 		}
671 	} else {
672 		int skip = 0;
673 
674 		if ((flags & SYM_SEARCH_SELF) || (flags & SYM_SEARCH_NEXT))
675 			skip = 1;
676 
677 		/*
678 		 * search dlopened objects: global or req_obj == dlopened_obj
679 		 * and and it's children
680 		 */
681 		TAILQ_FOREACH(n, &_dlopened_child_list, next_sib) {
682 			if (((n->data->obj_flags & DF_1_GLOBAL) == 0) &&
683 			    (n->data != req_obj->load_object))
684 				continue;
685 
686 			TAILQ_FOREACH(m, &n->data->grpsym_list, next_sib) {
687 				if (skip == 1) {
688 					if (m->data == req_obj) {
689 						skip = 0;
690 						if (flags & SYM_SEARCH_NEXT)
691 							continue;
692 					} else
693 						continue;
694 				}
695 				if ((flags & SYM_SEARCH_OTHER) &&
696 				    (m->data == req_obj))
697 					continue;
698 				if (_dl_find_symbol_obj(m->data, name, h, flags,
699 				    this, &weak_sym, &weak_object)) {
700 					object = m->data;
701 					found = 1;
702 					goto found;
703 				}
704 			}
705 		}
706 	}
707 
708 found:
709 	if (weak_object != NULL && found == 0) {
710 		object=weak_object;
711 		*this = weak_sym;
712 		found = 1;
713 	}
714 
715 
716 	if (found == 0) {
717 		if ((ref_sym == NULL ||
718 		    (ELF_ST_BIND(ref_sym->st_info) != STB_WEAK)) &&
719 		    (flags & SYM_WARNNOTFOUND))
720 			_dl_printf("%s:%s: undefined symbol '%s'\n",
721 			    __progname, req_obj->load_name, name);
722 		return (0);
723 	}
724 
725 	if (ref_sym != NULL && ref_sym->st_size != 0 &&
726 	    (ref_sym->st_size != (*this)->st_size)  &&
727 	    (ELF_ST_TYPE((*this)->st_info) != STT_FUNC) ) {
728 		_dl_printf("%s:%s: %s : WARNING: "
729 		    "symbol(%s) size mismatch, relink your program\n",
730 		    __progname, req_obj->load_name, object->load_name, name);
731 	}
732 
733 	if (pobj)
734 		*pobj = object;
735 
736 	return (object->obj_base);
737 }
738 
739 void
740 _dl_debug_state(void)
741 {
742 	/* Debugger stub */
743 }
744