xref: /openbsd-src/libexec/ld.so/resolve.c (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
1 /*	$OpenBSD: resolve.c,v 1.100 2023/07/08 14:09:43 jasper 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;
231 
232 	/* syscall in ld.so returns 0/-errno, where libc returns char* */
233 	if (_dl___realpath(object->load_name, origin_path) < 0)
234 		return -1;
235 
236 	dirname_path = _dl_dirname(origin_path);
237 	if (dirname_path == NULL)
238 		return -1;
239 
240 	_dl_strlcpy(origin_path, dirname_path, PATH_MAX);
241 
242 	return 0;
243 }
244 
245 /*
246  * Perform $ORIGIN substitutions on runpath and rpath
247  */
248 static void
249 _dl_origin_subst(elf_object_t *object)
250 {
251 	char origin_path[PATH_MAX];
252 	char **pp;
253 
254 	if (_dl_origin_path(object, origin_path) != 0)
255 		return;
256 
257 	/* perform path substitutions on each segment of runpath and rpath */
258 	if (object->runpath != NULL) {
259 		for (pp = object->runpath; *pp != NULL; pp++)
260 			_dl_origin_subst_path(object, origin_path, pp);
261 	}
262 	if (object->rpath != NULL) {
263 		for (pp = object->rpath; *pp != NULL; pp++)
264 			_dl_origin_subst_path(object, origin_path, pp);
265 	}
266 }
267 
268 /*
269  * Initialize a new dynamic object.
270  */
271 elf_object_t *
272 _dl_finalize_object(const char *objname, Elf_Dyn *dynp, Elf_Phdr *phdrp,
273     int phdrc, const int objtype, const long lbase, const long obase)
274 {
275 	elf_object_t *object;
276 	Elf_Addr gnu_hash = 0;
277 
278 	DL_DEB(("objname [%s], dynp %p, objtype %x lbase %lx, obase %lx\n",
279 	    objname, dynp, objtype, lbase, obase));
280 
281 	object = _dl_calloc(1, sizeof(elf_object_t));
282 	if (object == NULL)
283 		_dl_oom();
284 	object->prev = object->next = NULL;
285 
286 	object->load_dyn = dynp;
287 	while (dynp->d_tag != DT_NULL) {
288 		if (dynp->d_tag < DT_NUM)
289 			object->Dyn.info[dynp->d_tag] = dynp->d_un.d_val;
290 		else if (dynp->d_tag >= DT_LOPROC &&
291 		    dynp->d_tag < DT_LOPROC + DT_PROCNUM)
292 			object->Dyn.info[dynp->d_tag + DT_NUM - DT_LOPROC] =
293 			    dynp->d_un.d_val;
294 		if (dynp->d_tag == DT_TEXTREL)
295 			object->dyn.textrel = 1;
296 		if (dynp->d_tag == DT_SYMBOLIC)
297 			object->dyn.symbolic = 1;
298 		if (dynp->d_tag == DT_BIND_NOW)
299 			object->obj_flags |= DF_1_NOW;
300 		if (dynp->d_tag == DT_FLAGS_1)
301 			object->obj_flags |= dynp->d_un.d_val;
302 		if (dynp->d_tag == DT_FLAGS) {
303 			object->dyn.flags |= dynp->d_un.d_val;
304 			if (dynp->d_un.d_val & DF_SYMBOLIC)
305 				object->dyn.symbolic = 1;
306 			if (dynp->d_un.d_val & DF_TEXTREL)
307 				object->dyn.textrel = 1;
308 			if (dynp->d_un.d_val & DF_ORIGIN)
309 				object->obj_flags |= DF_1_ORIGIN;
310 			if (dynp->d_un.d_val & DF_BIND_NOW)
311 				object->obj_flags |= DF_1_NOW;
312 		}
313 		if (dynp->d_tag == DT_RELACOUNT)
314 			object->relacount = dynp->d_un.d_val;
315 		if (dynp->d_tag == DT_RELCOUNT)
316 			object->relcount = dynp->d_un.d_val;
317 		if (dynp->d_tag == DT_GNU_HASH)
318 			gnu_hash = dynp->d_un.d_val;
319 		dynp++;
320 	}
321 	DL_DEB((" flags %s = 0x%x\n", objname, object->obj_flags ));
322 	object->obj_type = objtype;
323 
324 	if (_dl_loading_object == NULL) {
325 		/*
326 		 * no loading object, object is the loading object,
327 		 * as it is either executable, or dlopened()
328 		 */
329 		_dl_loading_object = object;
330 	}
331 
332 	if ((object->obj_flags & DF_1_NOOPEN) != 0 &&
333 	    _dl_loading_object->obj_type == OBJTYPE_DLO &&
334 	    !_dl_traceld) {
335 		_dl_free(object);
336 		_dl_errno = DL_CANT_LOAD_OBJ;
337 		return(NULL);
338 	}
339 
340 	/*
341 	 *  Now relocate all pointer to dynamic info, but only
342 	 *  the ones which have pointer values.
343 	 */
344 	if (object->Dyn.info[DT_PLTGOT])
345 		object->Dyn.info[DT_PLTGOT] += obase;
346 	if (object->Dyn.info[DT_STRTAB])
347 		object->Dyn.info[DT_STRTAB] += obase;
348 	if (object->Dyn.info[DT_SYMTAB])
349 		object->Dyn.info[DT_SYMTAB] += obase;
350 	if (object->Dyn.info[DT_RELA])
351 		object->Dyn.info[DT_RELA] += obase;
352 	if (object->Dyn.info[DT_SONAME])
353 		object->Dyn.info[DT_SONAME] += object->Dyn.info[DT_STRTAB];
354 	if (object->Dyn.info[DT_RPATH])
355 		object->Dyn.info[DT_RPATH] += object->Dyn.info[DT_STRTAB];
356 	if (object->Dyn.info[DT_RUNPATH])
357 		object->Dyn.info[DT_RUNPATH] += object->Dyn.info[DT_STRTAB];
358 	if (object->Dyn.info[DT_REL])
359 		object->Dyn.info[DT_REL] += obase;
360 	if (object->Dyn.info[DT_INIT])
361 		object->Dyn.info[DT_INIT] += obase;
362 	if (object->Dyn.info[DT_FINI])
363 		object->Dyn.info[DT_FINI] += obase;
364 	if (object->Dyn.info[DT_JMPREL])
365 		object->Dyn.info[DT_JMPREL] += obase;
366 	if (object->Dyn.info[DT_INIT_ARRAY])
367 		object->Dyn.info[DT_INIT_ARRAY] += obase;
368 	if (object->Dyn.info[DT_FINI_ARRAY])
369 		object->Dyn.info[DT_FINI_ARRAY] += obase;
370 	if (object->Dyn.info[DT_PREINIT_ARRAY])
371 		object->Dyn.info[DT_PREINIT_ARRAY] += obase;
372 	if (object->Dyn.info[DT_RELR])
373 		object->Dyn.info[DT_RELR] += obase;
374 
375 	if (gnu_hash) {
376 		Elf_Word *hashtab = (Elf_Word *)(gnu_hash + obase);
377 		Elf_Word nbuckets = hashtab[0];
378 		Elf_Word nmaskwords = hashtab[2];
379 
380 		/* validity check */
381 		if (nbuckets > 0 && (nmaskwords & (nmaskwords - 1)) == 0) {
382 			Elf_Word symndx = hashtab[1];
383 			int bloom_size32 = (ELFSIZE / 32) * nmaskwords;
384 
385 			object->nbuckets = nbuckets;
386 			object->symndx_gnu = symndx;
387 			object->mask_bm_gnu = nmaskwords - 1;
388 			object->shift2_gnu = hashtab[3];
389 			object->bloom_gnu = (Elf_Addr *)(hashtab + 4);
390 			object->buckets_gnu = hashtab + 4 + bloom_size32;
391 			object->chains_gnu = object->buckets_gnu + nbuckets
392 			    - symndx;
393 
394 			/*
395 			 * If the ELF hash is present, get the total symbol
396 			 * count ("nchains") from there.  Otherwise, count
397 			 * the entries in the GNU hash chain.
398 			 */
399 			if (object->Dyn.info[DT_HASH] == 0) {
400 				Elf_Word n;
401 
402 				for (n = 0; n < nbuckets; n++) {
403 					Elf_Word bkt = object->buckets_gnu[n];
404 					const Elf_Word *hashval;
405 					if (bkt == 0)
406 						continue;
407 					hashval = &object->chains_gnu[bkt];
408 					do {
409 						symndx++;
410 					} while ((*hashval++ & 1U) == 0);
411 				}
412 				object->nchains = symndx;
413 			}
414 			object->status |= STAT_GNU_HASH;
415 		}
416 	}
417 	if (object->Dyn.info[DT_HASH] != 0) {
418 		Elf_Hash_Word *hashtab =
419 		    (Elf_Hash_Word *)(object->Dyn.info[DT_HASH] + obase);
420 
421 		object->nchains = hashtab[1];
422 		if (object->nbuckets == 0) {
423 			object->nbuckets = hashtab[0];
424 			object->buckets_elf = hashtab + 2;
425 			object->chains_elf = object->buckets_elf +
426 			    object->nbuckets;
427 		}
428 	}
429 
430 	object->phdrp = phdrp;
431 	object->phdrc = phdrc;
432 	object->load_base = lbase;
433 	object->obj_base = obase;
434 	object->load_name = _dl_strdup(objname);
435 	if (object->load_name == NULL)
436 		_dl_oom();
437 	object->load_object = _dl_loading_object;
438 	if (object->load_object == object)
439 		DL_DEB(("head %s\n", object->load_name));
440 	DL_DEB(("obj %s has %s as head\n", object->load_name,
441 	    _dl_loading_object->load_name ));
442 	object->refcount = 0;
443 	object->opencount = 0;	/* # dlopen() & exe */
444 	object->grprefcount = 0;
445 	/* default dev, inode for dlopen-able objects. */
446 	object->dev = 0;
447 	object->inode = 0;
448 	object->grpsym_gen = 0;
449 	TAILQ_INIT(&object->grpref_list);
450 
451 	if (object->dyn.runpath)
452 		object->runpath = _dl_split_path(object->dyn.runpath);
453 	/*
454 	 * DT_RPATH is ignored if DT_RUNPATH is present...except in
455 	 * the exe, whose DT_RPATH is a fallback for libs that don't
456 	 * use DT_RUNPATH
457 	 */
458 	if (object->dyn.rpath && (object->runpath == NULL ||
459 	    objtype == OBJTYPE_EXE))
460 		object->rpath = _dl_split_path(object->dyn.rpath);
461 	if ((object->obj_flags & DF_1_ORIGIN) && _dl_trust)
462 		_dl_origin_subst(object);
463 
464 	_dl_trace_object_setup(object);
465 
466 	return (object);
467 }
468 
469 static void
470 _dl_tailq_free(struct dep_node *n)
471 {
472 	struct dep_node *next;
473 
474 	while (n != NULL) {
475 		next = TAILQ_NEXT(n, next_sib);
476 		_dl_free(n);
477 		n = next;
478 	}
479 }
480 
481 static elf_object_t *free_objects;
482 
483 void
484 _dl_cleanup_objects()
485 {
486 	elf_object_t *nobj, *head;
487 	struct dep_node *n, *next;
488 
489 	n = TAILQ_FIRST(&_dlopened_child_list);
490 	while (n != NULL) {
491 		next = TAILQ_NEXT(n, next_sib);
492 		if (OBJECT_DLREF_CNT(n->data) == 0) {
493 			TAILQ_REMOVE(&_dlopened_child_list, n, next_sib);
494 			_dl_free(n);
495 		}
496 		n = next;
497 	}
498 
499 	head = free_objects;
500 	free_objects = NULL;
501 	while (head != NULL) {
502 		_dl_free(head->load_name);
503 		_dl_free((char *)head->sod.sod_name);
504 		_dl_free_path(head->runpath);
505 		_dl_free_path(head->rpath);
506 		_dl_free(head->grpsym_vec.vec);
507 		_dl_free(head->child_vec.vec);
508 		_dl_tailq_free(TAILQ_FIRST(&head->grpref_list));
509 		nobj = head->next;
510 		_dl_free(head);
511 		head = nobj;
512 	}
513 }
514 
515 void
516 _dl_remove_object(elf_object_t *object)
517 {
518 	object->prev->next = object->next;
519 	if (object->next)
520 		object->next->prev = object->prev;
521 
522 	if (_dl_last_object == object)
523 		_dl_last_object = object->prev;
524 	object_count--;
525 
526 	object->next = free_objects;
527 	free_objects = object;
528 }
529 
530 static int
531 matched_symbol(elf_object_t *obj, const Elf_Sym *sym, struct symlookup *sl)
532 {
533 	switch (ELF_ST_TYPE(sym->st_info)) {
534 	case STT_FUNC:
535 		/*
536 		 * Allow this symbol if we are referring to a function which
537 		 * has a value, even if section is UNDEF.  This allows &func
538 		 * to refer to PLT as per the ELF spec.  If flags has SYM_PLT
539 		 * set, we must have actual symbol, so this symbol is skipped.
540 		 */
541 		if ((sl->sl_flags & SYM_PLT) && sym->st_shndx == SHN_UNDEF)
542 			return 0;
543 		if (sym->st_value == 0)
544 			return 0;
545 		break;
546 	case STT_NOTYPE:
547 	case STT_OBJECT:
548 		if (sym->st_value == 0)
549 			return 0;
550 #if 0
551 		/* FALLTHROUGH */
552 	case STT_TLS:
553 #endif
554 		if (sym->st_shndx == SHN_UNDEF)
555 			return 0;
556 		break;
557 	default:
558 		return 0;
559 	}
560 
561 	if (sym != sl->sl_out.sym &&
562 	    _dl_strcmp(sl->sl_name, obj->dyn.strtab + sym->st_name))
563 		return 0;
564 
565 	if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) {
566 		sl->sl_out.sym = sym;
567 		sl->sl_out.obj = obj;
568 		return 1;
569 	} else if (ELF_ST_BIND(sym->st_info) == STB_WEAK) {
570 		if (sl->sl_weak_out.sym == NULL) {
571 			sl->sl_weak_out.sym = sym;
572 			sl->sl_weak_out.obj = obj;
573 		}
574 		/* done with this object, but need to check other objects */
575 		return -1;
576 	}
577 	return 0;
578 }
579 
580 static int
581 _dl_find_symbol_obj(elf_object_t *obj, struct symlookup *sl)
582 {
583 	const Elf_Sym	*symt = obj->dyn.symtab;
584 
585 	if (obj->status & STAT_GNU_HASH) {
586 		uint32_t hash = sl->sl_gnu_hash;
587 		Elf_Addr bloom_word;
588 		unsigned int h1;
589 		unsigned int h2;
590 		Elf_Word bucket;
591 		const Elf_Word *hashval;
592 
593 		/* pick right bitmask word from Bloom filter array */
594 		bloom_word = obj->bloom_gnu[(hash / ELFSIZE) &
595 		    obj->mask_bm_gnu];
596 
597 		/* calculate modulus ELFSIZE of gnu hash and its derivative */
598 		h1 = hash & (ELFSIZE - 1);
599 		h2 = (hash >> obj->shift2_gnu) & (ELFSIZE - 1);
600 
601 		/* Filter out the "definitely not in set" queries */
602 		if (((bloom_word >> h1) & (bloom_word >> h2) & 1) == 0)
603 			return 0;
604 
605 		/* Locate hash chain and corresponding value element */
606 		bucket = obj->buckets_gnu[hash % obj->nbuckets];
607 		if (bucket == 0)
608 			return 0;
609 		hashval = &obj->chains_gnu[bucket];
610 		do {
611 			if (((*hashval ^ hash) >> 1) == 0) {
612 				const Elf_Sym *sym = symt +
613 				    (hashval - obj->chains_gnu);
614 
615 				int r = matched_symbol(obj, sym, sl);
616 				if (r)
617 					return r > 0;
618 			}
619 		} while ((*hashval++ & 1U) == 0);
620 	} else {
621 		Elf_Word si;
622 
623 		for (si = obj->buckets_elf[sl->sl_elf_hash % obj->nbuckets];
624 		    si != STN_UNDEF; si = obj->chains_elf[si]) {
625 			const Elf_Sym *sym = symt + si;
626 
627 			int r = matched_symbol(obj, sym, sl);
628 			if (r)
629 				return r > 0;
630 		}
631 	}
632 	return 0;
633 }
634 
635 struct sym_res
636 _dl_find_symbol(const char *name, int flags, const Elf_Sym *ref_sym,
637     elf_object_t *req_obj)
638 {
639 	const unsigned char *p;
640 	unsigned char c;
641 	struct symlookup sl = {
642 		.sl_name = name,
643 		.sl_out = { .sym = NULL },
644 		.sl_weak_out = { .sym = NULL },
645 		.sl_elf_hash = 0,
646 		.sl_gnu_hash = 5381,
647 		.sl_flags = flags,
648 	};
649 
650 	/* Calculate both hashes in one pass */
651 	for (p = (const unsigned char *)name; (c = *p) != '\0'; p++) {
652 		sl.sl_elf_hash = (sl.sl_elf_hash << 4) + c;
653 		sl.sl_elf_hash ^= (sl.sl_elf_hash >> 24) & 0xf0;
654 		sl.sl_gnu_hash = sl.sl_gnu_hash * 33 + c;
655 	}
656 	sl.sl_elf_hash &= 0x0fffffff;
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