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