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