xref: /titanic_41/usr/src/cmd/sgs/rtld/common/analyze.c (revision 67ce1dada345581246cd990d73516418f321a793)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  *	Copyright (c) 1988 AT&T
29  *	  All Rights Reserved
30  */
31 
32 #include	<string.h>
33 #include	<stdio.h>
34 #include	<unistd.h>
35 #include	<sys/stat.h>
36 #include	<sys/mman.h>
37 #include	<fcntl.h>
38 #include	<limits.h>
39 #include	<dlfcn.h>
40 #include	<errno.h>
41 #include	<link.h>
42 #include	<debug.h>
43 #include	<conv.h>
44 #include	"_rtld.h"
45 #include	"_audit.h"
46 #include	"_elf.h"
47 #include	"msg.h"
48 
49 static Fct	*vector[] = {
50 	&elf_fct,
51 #ifdef A_OUT
52 	&aout_fct,
53 #endif
54 	0
55 };
56 
57 /*
58  * If a load filter flag is in effect, and this object is a filter, trigger the
59  * loading of all its filtees.  The load filter flag is in effect when creating
60  * configuration files, or when under the control of ldd(1), or the LD_LOADFLTR
61  * environment variable is set, or this object was built with the -zloadfltr
62  * flag.  Otherwise, filtee loading is deferred until triggered by a relocation.
63  */
64 static void
65 load_filtees(Rt_map *lmp, int *in_nfavl)
66 {
67 	if ((FLAGS1(lmp) & MSK_RT_FILTER) &&
68 	    ((FLAGS(lmp) & FLG_RT_LOADFLTR) ||
69 	    (LIST(lmp)->lm_tflags & LML_TFLG_LOADFLTR))) {
70 		Dyninfo		*dip =  DYNINFO(lmp);
71 		uint_t		cnt, max = DYNINFOCNT(lmp);
72 		Slookup		sl;
73 
74 		/*
75 		 * Initialize the symbol lookup data structure.
76 		 */
77 		SLOOKUP_INIT(sl, 0, lmp, lmp, ld_entry_cnt, 0, 0, 0, 0, 0);
78 
79 		for (cnt = 0; cnt < max; cnt++, dip++) {
80 			if (((dip->di_flags & MSK_DI_FILTER) == 0) ||
81 			    ((dip->di_flags & FLG_DI_AUXFLTR) &&
82 			    (rtld_flags & RT_FL_NOAUXFLTR)))
83 				continue;
84 			(void) elf_lookup_filtee(&sl, 0, 0, cnt, in_nfavl);
85 		}
86 	}
87 }
88 
89 /*
90  * Analyze one or more link-maps of a link map control list.  This routine is
91  * called at startup to continue the processing of the main executable.  It is
92  * also called each time a new set of objects are loaded, ie. from filters,
93  * lazy-loaded objects, or dlopen().
94  *
95  * In each instance we traverse the link-map control list starting with the
96  * initial object.  As dependencies are analyzed they are added to the link-map
97  * control list.  Thus the list grows as we traverse it - this results in the
98  * breadth first ordering of all needed objects.
99  */
100 int
101 analyze_lmc(Lm_list *lml, Aliste nlmco, Rt_map *nlmp, int *in_nfavl)
102 {
103 	Rt_map	*lmp = nlmp;
104 	Lm_cntl	*nlmc;
105 	int	ret = 1;
106 
107 	/*
108 	 * If this link-map control list is being analyzed, return.  The object
109 	 * that has just been added will be picked up by the existing analysis
110 	 * thread.  Note, this is only really meaningful during process init-
111 	 * ialization, as objects are added to the main link-map control list.
112 	 * Following this initialization, each family of objects that are loaded
113 	 * are added to a new link-map control list.
114 	 */
115 	/* LINTED */
116 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
117 	if (nlmc->lc_flags & LMC_FLG_ANALYZING)
118 		return (1);
119 
120 	/*
121 	 * If this object doesn't belong to the present link-map control list
122 	 * then it must already have been analyzed, or it is in the process of
123 	 * being analyzed prior to us recursing into this analysis.  In either
124 	 * case, ignore the object as it's already being taken care of.
125 	 */
126 	if (nlmco != CNTL(nlmp))
127 		return (1);
128 
129 	nlmc->lc_flags |= LMC_FLG_ANALYZING;
130 
131 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
132 		if (FLAGS(lmp) &
133 		    (FLG_RT_ANALZING | FLG_RT_ANALYZED | FLG_RT_DELETE))
134 			continue;
135 
136 		/*
137 		 * Indicate that analyzing is under way.
138 		 */
139 		FLAGS(lmp) |= FLG_RT_ANALZING;
140 
141 		/*
142 		 * If this link map represents a relocatable object, then we
143 		 * need to finish the link-editing of the object at this point.
144 		 */
145 		if (FLAGS(lmp) & FLG_RT_OBJECT) {
146 			if (elf_obj_fini(lml, lmp, in_nfavl) == 0) {
147 				if (lml->lm_flags & LML_FLG_TRC_ENABLE)
148 					continue;
149 				ret = 0;
150 				break;
151 			}
152 		}
153 
154 		DBG_CALL(Dbg_file_analyze(lmp));
155 
156 		/*
157 		 * Establish any dependencies this object requires.
158 		 */
159 		if (LM_NEEDED(lmp)(lml, nlmco, lmp, in_nfavl) == 0) {
160 			if (lml->lm_flags & LML_FLG_TRC_ENABLE)
161 				continue;
162 			ret = 0;
163 			break;
164 		}
165 
166 		FLAGS(lmp) &= ~FLG_RT_ANALZING;
167 		FLAGS(lmp) |= FLG_RT_ANALYZED;
168 
169 		/*
170 		 * If we're building a configuration file, determine if this
171 		 * object is a filter and if so load its filtees.  This
172 		 * traversal is only necessary for crle(1), as typical use of
173 		 * an object will load filters as part of relocation processing.
174 		 */
175 		if (MODE(nlmp) & RTLD_CONFGEN)
176 			load_filtees(lmp, in_nfavl);
177 
178 		/*
179 		 * If an interposer has been added, it will have been inserted
180 		 * in the link-map before the link we're presently analyzing.
181 		 * Break out of this analysis loop and return to the head of
182 		 * the link-map control list to analyze the interposer.  Note
183 		 * that this rescan preserves the breadth first loading of
184 		 * dependencies.
185 		 */
186 		/* LINTED */
187 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
188 		if (nlmc->lc_flags & LMC_FLG_REANALYZE) {
189 			nlmc->lc_flags &= ~LMC_FLG_REANALYZE;
190 			lmp = nlmc->lc_head;
191 		}
192 	}
193 
194 	/* LINTED */
195 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
196 	nlmc->lc_flags &= ~LMC_FLG_ANALYZING;
197 
198 	return (ret);
199 }
200 
201 /*
202  * Determine whether a symbol represents zero, .bss, bits.  Most commonly this
203  * function is used to determine whether the data for a copy relocation refers
204  * to initialized data or .bss.  If the data definition is within .bss, then the
205  * data is zero filled, and as the copy destination within the executable is
206  * .bss, we can skip copying zero's to zero's.
207  *
208  * However, if the defining object has MOVE data, it's .bss might contain
209  * non-zero data, in which case copy the definition regardless.
210  *
211  * For backward compatibility copy relocation processing, this routine can be
212  * used to determine precisely if a copy destination is a move record recipient.
213  */
214 static int
215 are_bits_zero(Rt_map *dlmp, Sym *dsym, int dest)
216 {
217 	Mmap	*mmap = NULL, *mmaps;
218 	caddr_t	daddr = (caddr_t)dsym->st_value;
219 
220 	if ((FLAGS(dlmp) & FLG_RT_FIXED) == 0)
221 		daddr += ADDR(dlmp);
222 
223 	/*
224 	 * Determine the segment that contains the copy definition.  Given that
225 	 * the copy relocation records have already been captured and verified,
226 	 * a segment must be found (but we add an escape clause never the less).
227 	 */
228 	for (mmaps = MMAPS(dlmp); mmaps->m_vaddr; mmaps++) {
229 		if ((daddr >= mmaps->m_vaddr) &&
230 		    (daddr < (mmaps->m_vaddr + mmaps->m_msize))) {
231 			mmap = mmaps;
232 			break;
233 		}
234 	}
235 	if (mmap == NULL)
236 		return (1);
237 
238 	/*
239 	 * If the definition is not within .bss, indicate this is not zero data.
240 	 */
241 	if (daddr < (mmap->m_vaddr + mmaps->m_fsize))
242 		return (0);
243 
244 	/*
245 	 * If the definition is within .bss, make sure the definition isn't the
246 	 * recipient of a move record.  Note, we don't precisely analyze whether
247 	 * the address is a move record recipient, as the infrastructure to
248 	 * prepare for, and carry out this analysis, is probably more costly
249 	 * than just copying the bytes regardless.
250 	 */
251 	if ((FLAGS(dlmp) & FLG_RT_MOVE) == 0)
252 		return (1);
253 
254 	/*
255 	 * However, for backward compatibility copy relocation processing, we
256 	 * can afford to work a little harder.  Here, determine precisely
257 	 * whether the destination in the executable is a move record recipient.
258 	 * See comments in lookup_sym_interpose(), below.
259 	 */
260 	if (dest && is_move_data(daddr))
261 		return (0);
262 
263 	return (1);
264 }
265 
266 /*
267  * Relocate an individual object.
268  */
269 static int
270 relocate_so(Lm_list *lml, Rt_map *lmp, int *relocated, int now, int *in_nfavl)
271 {
272 	/*
273 	 * If we're running under ldd(1), and haven't been asked to trace any
274 	 * warnings, skip any actual relocation processing.
275 	 */
276 	if (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
277 	    (lml->lm_flags & LML_FLG_TRC_WARN)) {
278 
279 		if (relocated)
280 			(*relocated)++;
281 
282 		if ((LM_RELOC(lmp)(lmp, now, in_nfavl) == 0) &&
283 		    ((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0))
284 			return (0);
285 	}
286 	return (1);
287 }
288 
289 /*
290  * Relocate the objects on a link-map control list.
291  */
292 static int
293 _relocate_lmc(Lm_list *lml, Rt_map *nlmp, int *relocated, int *in_nfavl)
294 {
295 	Rt_map	*lmp;
296 
297 	for (lmp = nlmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
298 		/*
299 		 * If this object has already been relocated, we're done.  If
300 		 * this object is being deleted, skip it, there's probably a
301 		 * relocation error somewhere that's causing this deletion.
302 		 */
303 		if (FLAGS(lmp) &
304 		    (FLG_RT_RELOCING | FLG_RT_RELOCED | FLG_RT_DELETE))
305 			continue;
306 
307 		/*
308 		 * Indicate that relocation processing is under way.
309 		 */
310 		FLAGS(lmp) |= FLG_RT_RELOCING;
311 
312 		/*
313 		 * Relocate the object.
314 		 */
315 		if (relocate_so(lml, lmp, relocated, 0, in_nfavl) == 0)
316 			return (0);
317 
318 		/*
319 		 * Indicate that the objects relocation is complete.
320 		 */
321 		FLAGS(lmp) &= ~FLG_RT_RELOCING;
322 		FLAGS(lmp) |= FLG_RT_RELOCED;
323 
324 		/*
325 		 * Mark this object's init is available for harvesting.  Under
326 		 * ldd(1) this marking is necessary for -i (tsort) gathering.
327 		 */
328 		lml->lm_init++;
329 		lml->lm_flags |= LML_FLG_OBJADDED;
330 
331 		/*
332 		 * Process any move data.  Note, this is carried out under ldd
333 		 * under relocation processing too, as it can flush out move
334 		 * errors, and enables lari(1) to provide a true representation
335 		 * of the runtime bindings.
336 		 */
337 		if ((FLAGS(lmp) & FLG_RT_MOVE) &&
338 		    (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
339 		    (lml->lm_flags & LML_FLG_TRC_WARN))) {
340 			if (move_data(lmp) == 0)
341 				return (0);
342 		}
343 
344 		/*
345 		 * Determine if this object is a filter, and if a load filter
346 		 * flag is in effect, trigger the loading of all its filtees.
347 		 */
348 		load_filtees(lmp, in_nfavl);
349 	}
350 
351 	/*
352 	 * Perform special copy relocations.  These are only meaningful for
353 	 * dynamic executables (fixed and head of their link-map list).  If
354 	 * this ever has to change then the infrastructure of COPY() has to
355 	 * change. Presently, a given link map can only have a receiver or
356 	 * supplier of copy data, so a union is used to overlap the storage
357 	 * for the COPY_R() and COPY_S() lists. These lists would need to
358 	 * be separated.
359 	 */
360 	if ((FLAGS(nlmp) & FLG_RT_FIXED) && (nlmp == LIST(nlmp)->lm_head) &&
361 	    (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
362 	    (lml->lm_flags & LML_FLG_TRC_WARN))) {
363 		Rt_map		*lmp;
364 		Aliste		idx1;
365 		Word		tracing;
366 
367 #if	defined(__i386)
368 		if (elf_copy_gen(nlmp) == 0)
369 			return (0);
370 #endif
371 		if (COPY_S(nlmp) == NULL)
372 			return (1);
373 
374 		if ((LIST(nlmp)->lm_flags & LML_FLG_TRC_ENABLE) &&
375 		    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
376 		    (LIST(nlmp)->lm_flags & LML_FLG_TRC_VERBOSE)))
377 			tracing = 1;
378 		else
379 			tracing = 0;
380 
381 		DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
382 
383 		for (APLIST_TRAVERSE(COPY_S(nlmp), idx1, lmp)) {
384 			Rel_copy	*rcp;
385 			Aliste		idx2;
386 
387 			for (ALIST_TRAVERSE(COPY_R(lmp), idx2, rcp)) {
388 				int zero;
389 
390 				/*
391 				 * Only copy the data if the data is from
392 				 * a non-zero definition (ie. not .bss).
393 				 */
394 				zero = are_bits_zero(rcp->r_dlmp,
395 				    rcp->r_dsym, 0);
396 				DBG_CALL(Dbg_reloc_copy(rcp->r_dlmp, nlmp,
397 				    rcp->r_name, zero));
398 				if (zero)
399 					continue;
400 
401 				(void) memcpy(rcp->r_radd, rcp->r_dadd,
402 				    rcp->r_size);
403 
404 				if ((tracing == 0) || ((FLAGS1(rcp->r_dlmp) &
405 				    FL1_RT_DISPREL) == 0))
406 					continue;
407 
408 				(void) printf(MSG_INTL(MSG_LDD_REL_CPYDISP),
409 				    demangle(rcp->r_name), NAME(rcp->r_dlmp));
410 			}
411 		}
412 
413 		DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
414 
415 		free(COPY_S(nlmp));
416 		COPY_S(nlmp) = 0;
417 	}
418 	return (1);
419 }
420 
421 int
422 relocate_lmc(Lm_list *lml, Aliste nlmco, Rt_map *clmp, Rt_map *nlmp,
423     int *in_nfavl)
424 {
425 	int	lret = 1, pret = 1;
426 	APlist	*alp;
427 	Aliste	plmco;
428 	Lm_cntl	*plmc, *nlmc;
429 
430 	/*
431 	 * If this link-map control list is being relocated, return.  The object
432 	 * that has just been added will be picked up by the existing relocation
433 	 * thread.  Note, this is only really meaningful during process init-
434 	 * ialization, as objects are added to the main link-map control list.
435 	 * Following this initialization, each family of objects that are loaded
436 	 * are added to a new link-map control list.
437 	 */
438 	/* LINTED */
439 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
440 
441 	if (nlmc->lc_flags & LMC_FLG_RELOCATING)
442 		return (1);
443 
444 	nlmc->lc_flags |= LMC_FLG_RELOCATING;
445 
446 	/*
447 	 * Relocate one or more link-maps of a link map control list.  If this
448 	 * object doesn't belong to the present link-map control list then it
449 	 * must already have been relocated, or it is in the process of being
450 	 * relocated prior to us recursing into this relocation.  In either
451 	 * case, ignore the object as it's already being taken care of, however,
452 	 * fall through and capture any relocation promotions that might have
453 	 * been established from the reference mode of this object.
454 	 *
455 	 * If we're generating a configuration file using crle(1), two passes
456 	 * may be involved.  Under the first pass, RTLD_CONFGEN is set.  Under
457 	 * this pass, crle() loads objects into the process address space.  No
458 	 * relocation is necessary at this point, we simply need to analyze the
459 	 * objects to insure any directly bound dependencies, filtees, etc.
460 	 * get loaded. Although we skip the relocation, fall through to insure
461 	 * any control lists are maintained appropriately.
462 	 *
463 	 * If objects are to be dldump(3c)'ed, crle(1) makes a second pass,
464 	 * using RTLD_NOW and RTLD_CONFGEN.  The RTLD_NOW effectively carries
465 	 * out the relocations of all loaded objects.
466 	 */
467 	if ((nlmco == CNTL(nlmp)) &&
468 	    ((MODE(nlmp) & (RTLD_NOW | RTLD_CONFGEN)) != RTLD_CONFGEN)) {
469 		int	relocated = 0;
470 
471 		/*
472 		 * Determine whether the initial link-map control list has
473 		 * started relocation.  From this point, should any interposing
474 		 * objects be added to this link-map control list, the objects
475 		 * are demoted to standard objects.  Their interposition can't
476 		 * be guaranteed once relocations have been carried out.
477 		 */
478 		if (nlmco == ALIST_OFF_DATA)
479 			lml->lm_flags |= LML_FLG_STARTREL;
480 
481 		/*
482 		 * Relocate the link-map control list.  Should this relocation
483 		 * fail, clean up this link-map list.  Relocations within this
484 		 * list may have required relocation promotions on other lists,
485 		 * so before acting upon these, and possibly adding more objects
486 		 * to the present link-map control list, try and clean up any
487 		 * failed objects now.
488 		 */
489 		lret = _relocate_lmc(lml, nlmp, &relocated, in_nfavl);
490 		if ((lret == 0) && (nlmco != ALIST_OFF_DATA))
491 			remove_lmc(lml, clmp, nlmc, nlmco, NAME(nlmp));
492 	}
493 
494 	/*
495 	 * Determine the new, and previous link-map control lists.
496 	 */
497 	/* LINTED */
498 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
499 	if (nlmco == ALIST_OFF_DATA) {
500 		plmco = nlmco;
501 		plmc = nlmc;
502 	} else {
503 		plmco = nlmco - lml->lm_lists->al_size;
504 		/* LINTED */
505 		plmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, plmco);
506 	}
507 
508 	/*
509 	 * Having completed this control list of objects, they can now be bound
510 	 * to from other objects.  Move this control list to the control list
511 	 * that precedes it.  Although this control list may have only bound to
512 	 * controls lists much higher up the control list stack, it must only
513 	 * be moved up one control list so as to preserve the link-map order
514 	 * that may have already been traversed in search of symbols.
515 	 */
516 	if (lret && (nlmco != ALIST_OFF_DATA) && nlmc->lc_head)
517 		lm_move(lml, nlmco, plmco, nlmc, plmc);
518 
519 	/*
520 	 * Determine whether existing objects that have already been relocated,
521 	 * need any additional relocations performed.  This can occur when new
522 	 * objects are loaded with RTLD_NOW, and these new objects have
523 	 * dependencies on objects that are already loaded.  Note, that we peel
524 	 * any relocation promotions off of one control list at a time.  This
525 	 * prevents relocations from being bound to objects that might yet fail
526 	 * to relocate themselves.
527 	 */
528 	while ((alp = plmc->lc_now) != NULL) {
529 		Aliste	idx;
530 		Rt_map	*lmp;
531 
532 		/*
533 		 * Remove the relocation promotion list, as performing more
534 		 * relocations may result in discovering more objects that need
535 		 * promotion.
536 		 */
537 		plmc->lc_now = NULL;
538 
539 		for (APLIST_TRAVERSE(alp, idx, lmp)) {
540 			/*
541 			 * If the original relocation of the link-map control
542 			 * list failed, or one of the relocation promotions of
543 			 * this loop has failed, demote any pending objects
544 			 * relocation mode.
545 			 */
546 			if ((lret == 0) || (pret == 0)) {
547 				MODE(lmp) &= ~RTLD_NOW;
548 				MODE(lmp) |= RTLD_LAZY;
549 				continue;
550 			}
551 
552 			/*
553 			 * If a relocation fails, save the error condition.
554 			 * It's possible that all new objects on the original
555 			 * link-map control list have been relocated
556 			 * successfully, but if the user request requires
557 			 * promoting objects that have already been loaded, we
558 			 * have to indicate that this operation couldn't be
559 			 * performed.  The unrelocated objects are in use on
560 			 * another control list, and may continue to be used.
561 			 * If the .plt that resulted in the error is called,
562 			 * then the process will receive a fatal error at that
563 			 * time.  But, the .plt may never be called.
564 			 */
565 			if (relocate_so(lml, lmp, 0, 1, in_nfavl) == 0)
566 				pret = 0;
567 		}
568 
569 		/*
570 		 * Having promoted any objects, determine whether additional
571 		 * dependencies were added, and if so move them to the previous
572 		 * link-map control list.
573 		 */
574 		/* LINTED */
575 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
576 		/* LINTED */
577 		plmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, plmco);
578 		if ((nlmco != ALIST_OFF_DATA) && nlmc->lc_head)
579 			lm_move(lml, nlmco, plmco, nlmc, plmc);
580 		free(alp);
581 	}
582 
583 	/*
584 	 * If relocations have been successful, indicate that relocations are
585 	 * no longer active for this control list.  Otherwise, leave the
586 	 * relocation flag, as this flag is used to determine the style of
587 	 * cleanup (see remove_lmc()).
588 	 */
589 	if (lret && pret) {
590 		/* LINTED */
591 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
592 		nlmc->lc_flags &= ~LMC_FLG_RELOCATING;
593 
594 		return (1);
595 	}
596 
597 	return (0);
598 }
599 
600 /*
601  * Inherit the first rejection message for possible later diagnostics.
602  *
603  * Any attempt to process a file that is unsuccessful, should be accompanied
604  * with an error diagnostic.  However, some operations like searching for a
605  * simple filename, involve trying numerous paths, and an error message for each
606  * lookup is not required.  Although a multiple search can fail, it's possible
607  * that a file was found, but was rejected because it was the wrong type.
608  * To satisfy these possibilities, the first failure is recorded as a rejection
609  * message, and this message is used later for a more specific diagnostic.
610  *
611  * File searches are focused at load_one(), and from here a rejection descriptor
612  * is passed down to various child routines.  If these child routines can
613  * process multiple files, then they will maintain their own rejection desc-
614  * riptor.  This is filled in for any failures, and a diagnostic produced to
615  * reflect the failure.  The child routines then employ rejection_inherit() to
616  * pass the first rejection message back to load_one().
617  *
618  * Note that the name, and rejection string must be duplicated, as the name
619  * buffer and error string buffer (see conv_ routines) may be reused for
620  * additional processing or rejection messages.
621  */
622 void
623 rejection_inherit(Rej_desc *rej1, Rej_desc *rej2)
624 {
625 	if (rej2->rej_type && (rej1->rej_type == 0)) {
626 		rej1->rej_type = rej2->rej_type;
627 		rej1->rej_info = rej2->rej_info;
628 		rej1->rej_flag = rej2->rej_flag;
629 		if (rej2->rej_name)
630 			rej1->rej_name = strdup(rej2->rej_name);
631 		if (rej2->rej_str) {
632 			if ((rej1->rej_str = strdup(rej2->rej_str)) == NULL)
633 				rej1->rej_str = MSG_ORIG(MSG_EMG_ENOMEM);
634 		}
635 	}
636 }
637 
638 /*
639  * Determine the object type of a file.
640  */
641 Fct *
642 are_u_this(Rej_desc *rej, int fd, struct stat *status, const char *name)
643 {
644 	int	i;
645 	char	*maddr = 0;
646 
647 	fmap->fm_fsize = status->st_size;
648 
649 	/*
650 	 * If this is a directory (which can't be mmap()'ed) generate a precise
651 	 * error message.
652 	 */
653 	if ((status->st_mode & S_IFMT) == S_IFDIR) {
654 		rej->rej_type = SGS_REJ_STR;
655 		rej->rej_str = strerror(EISDIR);
656 		return (0);
657 	}
658 
659 	/*
660 	 * Map in the first page of the file.  When this buffer is first used,
661 	 * the mapping is a single system page.  This is typically enough to
662 	 * inspect the ehdr and phdrs of the file, and can be reused for each
663 	 * file that get loaded.  If a larger mapping is required to read the
664 	 * ehdr and phdrs, a new mapping is created (see elf_map_it()).  This
665 	 * new mapping is again used for each new file loaded.  Some objects,
666 	 * such as filters, only take up one page, and in this case this mapping
667 	 * will suffice for the file.
668 	 */
669 	maddr = mmap(fmap->fm_maddr, fmap->fm_msize, (PROT_READ | PROT_EXEC),
670 	    fmap->fm_mflags, fd, 0);
671 #if defined(MAP_ALIGN)
672 	if ((maddr == MAP_FAILED) && (errno == EINVAL)) {
673 		/*
674 		 * If the mapping failed, and we used MAP_ALIGN, assume we're
675 		 * on a system that doesn't support this option.  Try again
676 		 * without MAP_ALIGN.
677 		 */
678 		if (fmap->fm_mflags & MAP_ALIGN) {
679 			rtld_flags2 |= RT_FL2_NOMALIGN;
680 			fmap_setup();
681 
682 			maddr = (char *)mmap(fmap->fm_maddr, fmap->fm_msize,
683 			    (PROT_READ | PROT_EXEC), fmap->fm_mflags, fd, 0);
684 		}
685 	}
686 #endif
687 	if (maddr == MAP_FAILED) {
688 		rej->rej_type = SGS_REJ_STR;
689 		rej->rej_str = strerror(errno);
690 		return (0);
691 	}
692 
693 	/*
694 	 * From now on we will re-use fmap->fm_maddr as the mapping address
695 	 * so we augment the flags with MAP_FIXED and drop any MAP_ALIGN.
696 	 */
697 	fmap->fm_maddr = maddr;
698 	fmap->fm_mflags |= MAP_FIXED;
699 #if defined(MAP_ALIGN)
700 	fmap->fm_mflags &= ~MAP_ALIGN;
701 #endif
702 
703 	/*
704 	 * Search through the object vectors to determine what kind of
705 	 * object we have.
706 	 */
707 	for (i = 0; vector[i]; i++) {
708 		if ((vector[i]->fct_are_u_this)(rej))
709 			return (vector[i]);
710 		else if (rej->rej_type) {
711 			Rt_map	*lmp;
712 
713 			/*
714 			 * If this object is an explicitly defined shared
715 			 * object under inspection by ldd, and contains a
716 			 * incompatible hardware capabilities requirement, then
717 			 * inform the user, but continue processing.
718 			 *
719 			 * XXXX - ldd -v for any rej failure.
720 			 */
721 			if ((rej->rej_type == SGS_REJ_HWCAP_1) &&
722 			    (lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) &&
723 			    ((lmp = lml_main.lm_head) != 0) &&
724 			    (FLAGS1(lmp) & FL1_RT_LDDSTUB) &&
725 			    (NEXT(lmp) == 0)) {
726 				(void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1),
727 				    name, rej->rej_str);
728 				return (vector[i]);
729 			}
730 			return (0);
731 		}
732 	}
733 
734 	/*
735 	 * Unknown file type.
736 	 */
737 	rej->rej_type = SGS_REJ_UNKFILE;
738 	return (0);
739 }
740 
741 /*
742  * Helper routine for is_so_matched() that consolidates matching a path name,
743  * or file name component of a link-map name.
744  */
745 static int
746 _is_so_matched(const char *name, const char *str, int path)
747 {
748 	const char	*_str;
749 
750 	if ((path == 0) && ((_str = strrchr(str, '/')) != NULL))
751 		_str++;
752 	else
753 		_str = str;
754 
755 	return (strcmp(name, _str));
756 }
757 
758 /*
759  * Determine whether a search name matches one of the names associated with a
760  * link-map.  A link-map contains several names:
761  *
762  *  .	a NAME() - typically the full pathname of an object that has been
763  *	loaded.  For example, when looking for the dependency "libc.so.1", a
764  *	search path is applied, with the eventual NAME() being "/lib/ld.so.1".
765  *	The name of the executable is typically a simple filename, such as
766  *	"main", as this is the name passed to exec() to start the process.
767  *
768  *  .	a PATHNAME() - this is maintained if the resolved NAME() is different
769  * 	to NAME(), ie. the original name is a symbolic link.  This is also
770  * 	the resolved full pathname for a dynamic executable.
771  *
772  *  .	a list of ALIAS() names - these are alternative names by which the
773  *	object has been found, ie. when dependencies are loaded through a
774  * 	variety of different symbolic links.
775  *
776  * The name pattern matching can differ depending on whether we are looking
777  * for a full path name (path != 0), or a simple file name (path == 0).  Full
778  * path names typically match NAME() or PATHNAME() entries, so these link-map
779  * names are inspected first when a full path name is being searched for.
780  * Simple file names typically match ALIAS() names, so these link-map names are
781  * inspected first when a simple file name is being searched for.
782  *
783  * For all full path name searches, the link-map names are taken as is.  For
784  * simple file name searches, only the file name component of any link-map
785  * names are used for comparison.
786  */
787 static Rt_map *
788 is_so_matched(Rt_map *lmp, const char *name, int path)
789 {
790 	Aliste		idx;
791 	const char	*cp;
792 
793 	/*
794 	 * A pathname is typically going to match a NAME() or PATHNAME(), so
795 	 * check these first.
796 	 */
797 	if (path) {
798 		if (strcmp(name, NAME(lmp)) == 0)
799 			return (lmp);
800 
801 		if (PATHNAME(lmp) != NAME(lmp)) {
802 			if (strcmp(name, PATHNAME(lmp)) == 0)
803 				return (lmp);
804 		}
805 	}
806 
807 	/*
808 	 * Typically, dependencies are specified as simple file names
809 	 * (DT_NEEDED == libc.so.1), which are expanded to full pathnames to
810 	 * open the file.  The full pathname is NAME(), and the original name
811 	 * is maintained on the ALIAS() list.
812 	 *
813 	 * If this is a simple filename, or a pathname has failed to match the
814 	 * NAME() and PATHNAME() check above, look through the ALIAS() list.
815 	 */
816 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
817 		/*
818 		 * If we're looking for a simple filename, _is_so_matched()
819 		 * will reduce the ALIAS name to its simple name.
820 		 */
821 		if (_is_so_matched(name, cp, path) == 0)
822 			return (lmp);
823 	}
824 
825 	/*
826 	 * Finally, if this is a simple file name, and any ALIAS() search has
827 	 * been completed, match the simple file name of NAME() and PATHNAME().
828 	 */
829 	if (path == 0) {
830 		if (_is_so_matched(name, NAME(lmp), 0) == 0)
831 			return (lmp);
832 
833 		if (PATHNAME(lmp) != NAME(lmp)) {
834 			if (_is_so_matched(name, PATHNAME(lmp), 0) == 0)
835 				return (lmp);
836 		}
837 	}
838 
839 	return (0);
840 }
841 
842 /*
843  * Files are opened by ld.so.1 to satisfy dependencies, filtees and dlopen()
844  * requests.  Each request investigates the file based upon the callers
845  * environment.  Once a full path name has been established, the following
846  * checks are made:
847  *
848  *  .	does the path exist in the link-map lists FullPathNode AVL tree?  if
849  *	so, the file is already loaded, and its associated link-map pointer
850  *	is returned.
851  *  .	does the path exist in the not-found AVL tree?  if so, this path has
852  *	already been determined to not exist, and a failure is returned.
853  *  .	a device/inode check, to ensure the same file isn't mapped multiple
854  *	times through different paths.  See file_open().
855  *
856  * However, there are cases where a test for an existing file name needs to be
857  * carried out, such as dlopen(NOLOAD) requests, dldump() requests, and as a
858  * final fallback to dependency loading.  These requests are handled by
859  * is_so_loaded().
860  *
861  * A traversal through the callers link-map list is carried out, and from each
862  * link-map, a comparison is made against all of the various names by which the
863  * object has been referenced.  is_so_matched() is used to compares the link-map
864  * names against the name being searched for.  Whether the search name is a full
865  * path name or a simple file name, governs what comparisons are made.
866  *
867  * A full path name, which is a fully resolved path name that starts with a "/"
868  * character, or a relative path name that includes a "/" character, must match
869  * the link-map names explicitly.  A simple file name, which is any name *not*
870  * containing a "/" character, are matched against the file name component of
871  * any link-map names.
872  */
873 Rt_map *
874 is_so_loaded(Lm_list *lml, const char *name, int *in_nfavl)
875 {
876 	Rt_map		*lmp;
877 	avl_index_t	where;
878 	Lm_cntl		*lmc;
879 	Aliste		idx;
880 	int		path = 0;
881 
882 	/*
883 	 * If the name is a full path name, first determine if the path name is
884 	 * registered on the FullPathNode AVL, or not-found AVL trees.
885 	 */
886 	if (name[0] == '/') {
887 		if (((lmp = fpavl_recorded(lml, name, &where)) != NULL) &&
888 		    ((FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE)) == 0))
889 			return (lmp);
890 		if (nfavl_recorded(name, 0)) {
891 			/*
892 			 * For dlopen() and dlsym() fall backs, indicate that
893 			 * a registered not-found path has indicated that this
894 			 * object does not exist.
895 			 */
896 			if (in_nfavl)
897 				(*in_nfavl)++;
898 			return (0);
899 		}
900 	}
901 
902 	/*
903 	 * Determine whether the name is a simple file name, or a path name.
904 	 */
905 	if (strchr(name, '/'))
906 		path++;
907 
908 	/*
909 	 * Loop through the callers link-map lists.
910 	 */
911 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
912 		for (lmp = lmc->lc_head; lmp; lmp = (Rt_map *)NEXT(lmp)) {
913 			if (FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE))
914 				continue;
915 
916 			if (is_so_matched(lmp, name, path))
917 				return (lmp);
918 		}
919 	}
920 	return ((Rt_map *)0);
921 }
922 
923 /*
924  * Tracing is enabled by the LD_TRACE_LOADED_OPTIONS environment variable which
925  * is normally set from ldd(1).  For each link map we load, print the load name
926  * and the full pathname of the shared object.
927  */
928 /* ARGSUSED4 */
929 static void
930 trace_so(Rt_map *clmp, Rej_desc *rej, const char *name, const char *path,
931     int alter, const char *nfound)
932 {
933 	const char	*str = MSG_ORIG(MSG_STR_EMPTY);
934 	const char	*reject = MSG_ORIG(MSG_STR_EMPTY);
935 	char		_reject[PATH_MAX];
936 
937 	/*
938 	 * The first time through trace_so() will only have lddstub on the
939 	 * link-map list and the preloaded shared object is supplied as "path".
940 	 * As we don't want to print this shared object as a dependency, but
941 	 * instead inspect *its* dependencies, return.
942 	 */
943 	if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
944 		return;
945 
946 	/*
947 	 * Without any rejection info, this is a supplied not-found condition.
948 	 */
949 	if (rej && (rej->rej_type == 0)) {
950 		(void) printf(nfound, name);
951 		return;
952 	}
953 
954 	/*
955 	 * If rejection information exists then establish what object was
956 	 * found and the reason for its rejection.
957 	 */
958 	if (rej) {
959 		Conv_reject_desc_buf_t rej_buf;
960 
961 		/* LINTED */
962 		(void) snprintf(_reject, PATH_MAX,
963 		    MSG_INTL(ldd_reject[rej->rej_type]),
964 		    conv_reject_desc(rej, &rej_buf, M_MACH));
965 		if (rej->rej_name)
966 			path = rej->rej_name;
967 		reject = (char *)_reject;
968 
969 		/*
970 		 * Was an alternative pathname defined (from a configuration
971 		 * file).
972 		 */
973 		if (rej->rej_flag & FLG_FD_ALTER)
974 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
975 	} else {
976 		if (alter)
977 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
978 	}
979 
980 	/*
981 	 * If the load name isn't a full pathname print its associated pathname
982 	 * together with all the other information we've gathered.
983 	 */
984 	if (*name == '/')
985 		(void) printf(MSG_ORIG(MSG_LDD_FIL_PATH), path, str, reject);
986 	else
987 		(void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV), name, path, str,
988 		    reject);
989 }
990 
991 
992 /*
993  * Establish a link-map mode, initializing it if it has just been loaded, or
994  * potentially updating it if it already exists.
995  */
996 int
997 update_mode(Rt_map *lmp, int omode, int nmode)
998 {
999 	Lm_list	*lml = LIST(lmp);
1000 	int	pmode = 0;
1001 
1002 	/*
1003 	 * A newly loaded object hasn't had its mode set yet.  Modes are used to
1004 	 * load dependencies, so don't propagate any parent or no-load flags, as
1005 	 * these would adversely affect this objects ability to load any of its
1006 	 * dependencies that aren't already loaded.  RTLD_FIRST is applicable to
1007 	 * this objects handle creation only, and should not be propagated.
1008 	 */
1009 	if ((FLAGS(lmp) & FLG_RT_MODESET) == 0) {
1010 		MODE(lmp) |= nmode & ~(RTLD_PARENT | RTLD_NOLOAD | RTLD_FIRST);
1011 		FLAGS(lmp) |= FLG_RT_MODESET;
1012 		return (1);
1013 	}
1014 
1015 	/*
1016 	 * Establish any new overriding modes.  RTLD_LAZY and RTLD_NOW should be
1017 	 * represented individually (this is historic, as these two flags were
1018 	 * the only flags originally available to dlopen()).  Other flags are
1019 	 * accumulative, but have a hierarchy of preference.
1020 	 */
1021 	if ((omode & RTLD_LAZY) && (nmode & RTLD_NOW)) {
1022 		MODE(lmp) &= ~RTLD_LAZY;
1023 		pmode |= RTLD_NOW;
1024 	}
1025 
1026 	pmode |= ((~omode & nmode) &
1027 	    (RTLD_GLOBAL | RTLD_WORLD | RTLD_NODELETE));
1028 	if (pmode) {
1029 		DBG_CALL(Dbg_file_mode_promote(lmp, pmode));
1030 		MODE(lmp) |= pmode;
1031 	}
1032 
1033 	/*
1034 	 * If this load is an RTLD_NOW request and the object has already been
1035 	 * loaded non-RTLD_NOW, append this object to the relocation-now list
1036 	 * of the objects associated control list.  Note, if the object hasn't
1037 	 * yet been relocated, setting its MODE() to RTLD_NOW will establish
1038 	 * full relocation processing when it eventually gets relocated.
1039 	 */
1040 	if ((pmode & RTLD_NOW) &&
1041 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))) {
1042 		Lm_cntl	*lmc;
1043 
1044 		/* LINTED */
1045 		lmc = (Lm_cntl *)alist_item_by_offset(LIST(lmp)->lm_lists,
1046 		    CNTL(lmp));
1047 		(void) aplist_append(&lmc->lc_now, lmp, AL_CNT_LMNOW);
1048 	}
1049 
1050 #ifdef	SIEBEL_DISABLE
1051 	/*
1052 	 * For patch backward compatibility the following .init collection
1053 	 * is disabled.
1054 	 */
1055 	if (rtld_flags & RT_FL_DISFIX_1)
1056 		return (pmode);
1057 #endif
1058 
1059 	/*
1060 	 * If this objects .init has been collected but has not yet been called,
1061 	 * it may be necessary to reevaluate the object using tsort().  For
1062 	 * example, a new dlopen() hierarchy may bind to uninitialized objects
1063 	 * that are already loaded, or a dlopen(RTLD_NOW) can establish new
1064 	 * bindings between already loaded objects that require the tsort()
1065 	 * information be recomputed.  If however, no new objects have been
1066 	 * added to the process, and this object hasn't been promoted, don't
1067 	 * bother reevaluating the .init.  The present tsort() information is
1068 	 * probably as accurate as necessary, and by not establishing a parallel
1069 	 * tsort() we can help reduce the amount of recursion possible between
1070 	 * .inits.
1071 	 */
1072 	if (((FLAGS(lmp) &
1073 	    (FLG_RT_INITCLCT | FLG_RT_INITCALL)) == FLG_RT_INITCLCT) &&
1074 	    ((lml->lm_flags & LML_FLG_OBJADDED) || ((pmode & RTLD_NOW) &&
1075 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))))) {
1076 		FLAGS(lmp) &= ~FLG_RT_INITCLCT;
1077 		LIST(lmp)->lm_init++;
1078 		LIST(lmp)->lm_flags |= LML_FLG_OBJREEVAL;
1079 	}
1080 
1081 	return (pmode);
1082 }
1083 
1084 /*
1085  * Determine whether an alias name already exists, and if not create one.  This
1086  * is typically used to retain dependency names, such as "libc.so.1", which
1087  * would have been expanded to full path names when they were loaded.  The
1088  * full path names (NAME() and possibly PATHNAME()) are maintained as Fullpath
1089  * AVL nodes, and thus would have been matched by fpavl_loaded() during
1090  * file_open().
1091  */
1092 int
1093 append_alias(Rt_map *lmp, const char *str, int *added)
1094 {
1095 	Aliste	idx;
1096 	char	*cp;
1097 
1098 	/*
1099 	 * Determine if this filename is already on the alias list.
1100 	 */
1101 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
1102 		if (strcmp(cp, str) == 0)
1103 			return (1);
1104 	}
1105 
1106 	/*
1107 	 * This is a new alias, append it to the alias list.
1108 	 */
1109 	if ((cp = strdup(str)) == NULL)
1110 		return (0);
1111 
1112 	if (aplist_append(&ALIAS(lmp), cp, AL_CNT_ALIAS) == NULL) {
1113 		free(cp);
1114 		return (0);
1115 	}
1116 	if (added)
1117 		*added = 1;
1118 	return (1);
1119 }
1120 
1121 /*
1122  * Determine whether a file is already loaded by comparing device and inode
1123  * values.
1124  */
1125 static Rt_map *
1126 is_devinode_loaded(struct stat *status, Lm_list *lml, const char *name,
1127     uint_t flags)
1128 {
1129 	Lm_cntl	*lmc;
1130 	Aliste	idx;
1131 
1132 	/*
1133 	 * If this is an auditor, it will have been opened on a new link-map.
1134 	 * To prevent multiple occurrences of the same auditor on multiple
1135 	 * link-maps, search the head of each link-map list and see if this
1136 	 * object is already loaded as an auditor.
1137 	 */
1138 	if (flags & FLG_RT_AUDIT) {
1139 		Lm_list		*lml;
1140 		Listnode	*lnp;
1141 
1142 		for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
1143 			Rt_map	*nlmp = lml->lm_head;
1144 
1145 			if (nlmp && ((FLAGS(nlmp) &
1146 			    (FLG_RT_AUDIT | FLG_RT_DELETE)) == FLG_RT_AUDIT) &&
1147 			    (STDEV(nlmp) == status->st_dev) &&
1148 			    (STINO(nlmp) == status->st_ino))
1149 				return (nlmp);
1150 		}
1151 		return ((Rt_map *)0);
1152 	}
1153 
1154 	/*
1155 	 * If the file has been found determine from the new files status
1156 	 * information if this file is actually linked to one we already have
1157 	 * mapped.  This catches symlink names not caught by is_so_loaded().
1158 	 */
1159 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
1160 		Rt_map	*nlmp;
1161 
1162 		for (nlmp = lmc->lc_head; nlmp; nlmp = (Rt_map *)NEXT(nlmp)) {
1163 			if ((FLAGS(nlmp) & FLG_RT_DELETE) ||
1164 			    (FLAGS1(nlmp) & FL1_RT_LDDSTUB))
1165 				continue;
1166 
1167 			if ((STDEV(nlmp) != status->st_dev) ||
1168 			    (STINO(nlmp) != status->st_ino))
1169 				continue;
1170 
1171 			if (lml->lm_flags & LML_FLG_TRC_VERBOSE) {
1172 				/* BEGIN CSTYLED */
1173 				if (*name == '/')
1174 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
1175 					name, MSG_ORIG(MSG_STR_EMPTY),
1176 					MSG_ORIG(MSG_STR_EMPTY));
1177 				else
1178 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
1179 					name, NAME(nlmp),
1180 					MSG_ORIG(MSG_STR_EMPTY),
1181 					MSG_ORIG(MSG_STR_EMPTY));
1182 				/* END CSTYLED */
1183 			}
1184 			return (nlmp);
1185 		}
1186 	}
1187 	return ((Rt_map *)0);
1188 }
1189 
1190 /*
1191  * Generate any error messages indicating a file could not be found.  When
1192  * preloading or auditing a secure application, it can be a little more helpful
1193  * to indicate that a search of secure directories has failed, so adjust the
1194  * messages accordingly.
1195  */
1196 void
1197 file_notfound(Lm_list *lml, const char *name, Rt_map *clmp, uint_t flags,
1198     Rej_desc * rej)
1199 {
1200 	int	secure = 0;
1201 
1202 	if ((rtld_flags & RT_FL_SECURE) &&
1203 	    (flags & (FLG_RT_PRELOAD | FLG_RT_AUDIT)))
1204 		secure++;
1205 
1206 	if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
1207 		/*
1208 		 * Under ldd(1), auxiliary filtees that can't be loaded are
1209 		 * ignored, unless verbose errors are requested.
1210 		 */
1211 		if ((rtld_flags & RT_FL_SILENCERR) &&
1212 		    ((lml->lm_flags & LML_FLG_TRC_VERBOSE) == 0))
1213 			return;
1214 
1215 		if (secure)
1216 			trace_so(clmp, rej, name, 0, 0,
1217 			    MSG_INTL(MSG_LDD_SEC_NFOUND));
1218 		else
1219 			trace_so(clmp, rej, name, 0, 0,
1220 			    MSG_INTL(MSG_LDD_FIL_NFOUND));
1221 		return;
1222 	}
1223 
1224 	if (rej->rej_type) {
1225 		Conv_reject_desc_buf_t rej_buf;
1226 
1227 		eprintf(lml, ERR_FATAL, MSG_INTL(err_reject[rej->rej_type]),
1228 		    rej->rej_name ? rej->rej_name : MSG_INTL(MSG_STR_UNKNOWN),
1229 		    conv_reject_desc(rej, &rej_buf, M_MACH));
1230 		return;
1231 	}
1232 
1233 	if (secure)
1234 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SEC_OPEN), name);
1235 	else
1236 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), name,
1237 		    strerror(ENOENT));
1238 }
1239 
1240 static int
1241 file_open(int err, Lm_list *lml, const char *oname, const char *nname,
1242     Rt_map *clmp, uint_t flags, Fdesc *fdesc, Rej_desc *rej, int *in_nfavl)
1243 {
1244 	struct stat	status;
1245 	Rt_map		*nlmp;
1246 	int		resolved = 0;
1247 	char		*name;
1248 	avl_index_t	nfavlwhere = 0;
1249 
1250 	fdesc->fd_oname = oname;
1251 
1252 	if ((err == 0) && (fdesc->fd_flags & FLG_FD_ALTER))
1253 		DBG_CALL(Dbg_file_config_obj(lml, oname, 0, nname));
1254 
1255 	/*
1256 	 * If we're dealing with a full pathname, determine whether this
1257 	 * pathname is already known.  Other pathnames fall through to the
1258 	 * dev/inode check, as even though the pathname may look the same as
1259 	 * one previously used, the process may have changed directory.
1260 	 */
1261 	if ((err == 0) && (nname[0] == '/')) {
1262 		if ((nlmp = fpavl_recorded(lml, nname,
1263 		    &(fdesc->fd_avlwhere))) != NULL) {
1264 			fdesc->fd_nname = nname;
1265 			fdesc->fd_lmp = nlmp;
1266 			return (1);
1267 		}
1268 		if (nfavl_recorded(nname, &nfavlwhere)) {
1269 			/*
1270 			 * For dlopen() and dlsym() fall backs, indicate that
1271 			 * a registered not-found path has indicated that this
1272 			 * object does not exist.  If this path has been
1273 			 * constructed as part of expanding a HWCAP directory,
1274 			 * and as this is a silent failure, where no rejection
1275 			 * message is created, free the original name to
1276 			 * simplify the life of the caller.
1277 			 */
1278 			if (in_nfavl)
1279 				(*in_nfavl)++;
1280 			if (flags & FLG_RT_HWCAP)
1281 				free((void *)nname);
1282 			return (0);
1283 		}
1284 	}
1285 
1286 	if ((err == 0) && ((stat(nname, &status)) != -1)) {
1287 		char	path[PATH_MAX];
1288 		int	fd, size, added;
1289 
1290 		/*
1291 		 * If this path has been constructed as part of expanding a
1292 		 * HWCAP directory, ignore any subdirectories.  As this is a
1293 		 * silent failure, where no rejection message is created, free
1294 		 * the original name to simplify the life of the caller.  For
1295 		 * any other reference that expands to a directory, fall through
1296 		 * to construct a meaningful rejection message.
1297 		 */
1298 		if ((flags & FLG_RT_HWCAP) &&
1299 		    ((status.st_mode & S_IFMT) == S_IFDIR)) {
1300 			free((void *)nname);
1301 			return (0);
1302 		}
1303 
1304 		/*
1305 		 * Resolve the filename and determine whether the resolved name
1306 		 * is already known.  Typically, the previous fpavl_loaded()
1307 		 * will have caught this, as both NAME() and PATHNAME() for a
1308 		 * link-map are recorded in the FullNode AVL tree.  However,
1309 		 * instances exist where a file can be replaced (loop-back
1310 		 * mounts, bfu, etc.), and reference is made to the original
1311 		 * file through a symbolic link.  By checking the pathname here,
1312 		 * we don't fall through to the dev/inode check and conclude
1313 		 * that a new file should be loaded.
1314 		 */
1315 		if ((nname[0] == '/') && (rtld_flags & RT_FL_EXECNAME) &&
1316 		    ((size = resolvepath(nname, path, (PATH_MAX - 1))) > 0)) {
1317 			path[size] = '\0';
1318 
1319 			if (strcmp(nname, path)) {
1320 				if ((nlmp =
1321 				    fpavl_recorded(lml, path, 0)) != NULL) {
1322 					added = 0;
1323 
1324 					if (append_alias(nlmp, nname,
1325 					    &added) == 0)
1326 						return (0);
1327 					/* BEGIN CSTYLED */
1328 					if (added)
1329 					    DBG_CALL(Dbg_file_skip(LIST(clmp),
1330 						NAME(nlmp), nname));
1331 					/* END CSTYLED */
1332 					fdesc->fd_nname = nname;
1333 					fdesc->fd_lmp = nlmp;
1334 					return (1);
1335 				}
1336 
1337 				/*
1338 				 * If this pathname hasn't been loaded, save
1339 				 * the resolved pathname so that it doesn't
1340 				 * have to be recomputed as part of fullpath()
1341 				 * processing.
1342 				 */
1343 				if ((fdesc->fd_pname = strdup(path)) == NULL)
1344 					return (0);
1345 				resolved = 1;
1346 			} else {
1347 				/*
1348 				 * If the resolved name doesn't differ from the
1349 				 * original, save it without duplication.
1350 				 * Having fd_pname set indicates that no further
1351 				 * resolvepath processing is necessary.
1352 				 */
1353 				fdesc->fd_pname = nname;
1354 			}
1355 		}
1356 
1357 		if (nlmp = is_devinode_loaded(&status, lml, nname, flags)) {
1358 			if (flags & FLG_RT_AUDIT) {
1359 				/*
1360 				 * If we've been requested to load an auditor,
1361 				 * and an auditor of the same name already
1362 				 * exists, then the original auditor is used.
1363 				 */
1364 				DBG_CALL(Dbg_audit_skip(LIST(clmp),
1365 				    NAME(nlmp), LIST(nlmp)->lm_lmidstr));
1366 			} else {
1367 				/*
1368 				 * Otherwise, if an alternatively named file
1369 				 * has been found for the same dev/inode, add
1370 				 * a new name alias, and insert any alias full
1371 				 * pathname in the link-map lists AVL tree.
1372 				 */
1373 				added = 0;
1374 
1375 				if (append_alias(nlmp, nname, &added) == 0)
1376 					return (0);
1377 				if (added) {
1378 					if ((nname[0] == '/') &&
1379 					    (fpavl_insert(lml, nlmp,
1380 					    nname, 0) == 0))
1381 						return (0);
1382 					DBG_CALL(Dbg_file_skip(LIST(clmp),
1383 					    NAME(nlmp), nname));
1384 				}
1385 			}
1386 
1387 			/*
1388 			 * Record in the file descriptor the existing object
1389 			 * that satisfies this open request.
1390 			 */
1391 			fdesc->fd_nname = nname;
1392 			fdesc->fd_lmp = nlmp;
1393 			return (1);
1394 		}
1395 
1396 		if ((fd = open(nname, O_RDONLY, 0)) == -1) {
1397 			/*
1398 			 * As the file must exist for the previous stat() to
1399 			 * have succeeded, record the error condition.
1400 			 */
1401 			rej->rej_type = SGS_REJ_STR;
1402 			rej->rej_str = strerror(errno);
1403 		} else {
1404 			Fct	*ftp;
1405 
1406 			if ((ftp = are_u_this(rej, fd, &status, nname)) != 0) {
1407 				fdesc->fd_nname = nname;
1408 				fdesc->fd_ftp = ftp;
1409 				fdesc->fd_dev = status.st_dev;
1410 				fdesc->fd_ino = status.st_ino;
1411 				fdesc->fd_fd = fd;
1412 
1413 				/*
1414 				 * Trace that this open has succeeded.
1415 				 */
1416 				if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
1417 					trace_so(clmp, 0, oname, nname,
1418 					    (fdesc->fd_flags & FLG_FD_ALTER),
1419 					    0);
1420 				}
1421 				return (1);
1422 			}
1423 			(void) close(fd);
1424 		}
1425 
1426 	} else if (errno != ENOENT) {
1427 		/*
1428 		 * If the open() failed for anything other than the file not
1429 		 * existing, record the error condition.
1430 		 */
1431 		rej->rej_type = SGS_REJ_STR;
1432 		rej->rej_str = strerror(errno);
1433 	}
1434 
1435 	/*
1436 	 * Regardless of error, duplicate and record any full path names that
1437 	 * can't be used on the "not-found" AVL tree.
1438 	 */
1439 	if ((nname[0] == '/') && ((name = strdup(nname)) != NULL))
1440 		nfavl_insert(name, nfavlwhere);
1441 
1442 	/*
1443 	 * Indicate any rejection.
1444 	 */
1445 	if (rej->rej_type) {
1446 		/*
1447 		 * If this pathname was resolved and duplicated, remove the
1448 		 * allocated name to simplify the cleanup of the callers.
1449 		 */
1450 		if (resolved) {
1451 			free((void *)fdesc->fd_pname);
1452 			fdesc->fd_pname = NULL;
1453 		}
1454 		rej->rej_name = nname;
1455 		rej->rej_flag = (fdesc->fd_flags & FLG_FD_ALTER);
1456 		DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
1457 	}
1458 	return (0);
1459 }
1460 
1461 /*
1462  * Find a full pathname (it contains a "/").
1463  */
1464 int
1465 find_path(Lm_list *lml, const char *oname, Rt_map *clmp, uint_t flags,
1466     Fdesc *fdesc, Rej_desc *rej, int *in_nfavl)
1467 {
1468 	int	err = 0;
1469 
1470 	/*
1471 	 * If directory configuration exists determine if this path is known.
1472 	 */
1473 	if (rtld_flags & RT_FL_DIRCFG) {
1474 		Rtc_obj		*obj;
1475 		const char	*aname;
1476 
1477 		if ((obj = elf_config_ent(oname, (Word)elf_hash(oname),
1478 		    0, &aname)) != 0) {
1479 			/*
1480 			 * If the configuration file states that this path is a
1481 			 * directory, or the path is explicitly defined as
1482 			 * non-existent (ie. a unused platform specific
1483 			 * library), then go no further.
1484 			 */
1485 			if (obj->co_flags & RTC_OBJ_DIRENT) {
1486 				err = EISDIR;
1487 			} else if ((obj->co_flags &
1488 			    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) ==
1489 			    RTC_OBJ_NOEXIST) {
1490 				err = ENOENT;
1491 			} else if ((obj->co_flags & RTC_OBJ_ALTER) &&
1492 			    (rtld_flags & RT_FL_OBJALT) && (lml == &lml_main)) {
1493 				int	ret;
1494 
1495 				fdesc->fd_flags |= FLG_FD_ALTER;
1496 				/*
1497 				 * Attempt to open the alternative path.  If
1498 				 * this fails, and the alternative is flagged
1499 				 * as optional, fall through to open the
1500 				 * original path.
1501 				 */
1502 				DBG_CALL(Dbg_libs_found(lml, aname,
1503 				    FLG_FD_ALTER));
1504 				if (((ret = file_open(0, lml, oname, aname,
1505 				    clmp, flags, fdesc, rej, in_nfavl)) != 0) ||
1506 				    ((obj->co_flags & RTC_OBJ_OPTINAL) == 0))
1507 					return (ret);
1508 
1509 				fdesc->fd_flags &= ~FLG_FD_ALTER;
1510 			}
1511 		}
1512 	}
1513 	DBG_CALL(Dbg_libs_found(lml, oname, 0));
1514 	return (file_open(err, lml, oname, oname, clmp, flags, fdesc,
1515 	    rej, in_nfavl));
1516 }
1517 
1518 /*
1519  * Find a simple filename (it doesn't contain a "/").
1520  */
1521 static int
1522 _find_file(Lm_list *lml, const char *oname, const char *nname, Rt_map *clmp,
1523     uint_t flags, Fdesc *fdesc, Rej_desc *rej, Pnode *dir, int aflag,
1524     int *in_nfavl)
1525 {
1526 	DBG_CALL(Dbg_libs_found(lml, nname, aflag));
1527 	if ((lml->lm_flags & LML_FLG_TRC_SEARCH) &&
1528 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
1529 		(void) printf(MSG_INTL(MSG_LDD_PTH_TRYING), nname, aflag ?
1530 		    MSG_INTL(MSG_LDD_FIL_ALTER) : MSG_ORIG(MSG_STR_EMPTY));
1531 	}
1532 
1533 	/*
1534 	 * If we're being audited tell the audit library of the file we're about
1535 	 * to go search for.  The audit library may offer an alternative
1536 	 * dependency, or indicate that this dependency should be ignored.
1537 	 */
1538 	if ((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
1539 		char	*aname;
1540 
1541 		if ((aname = audit_objsearch(clmp, nname,
1542 		    (dir->p_orig & LA_SER_MASK))) == 0) {
1543 			DBG_CALL(Dbg_audit_terminate(lml, nname));
1544 			return (0);
1545 		}
1546 
1547 		/*
1548 		 * Protect ourselves from auditor mischief, by copying any
1549 		 * alternative name over the present name (the present name is
1550 		 * maintained in a static buffer - see elf_get_so());
1551 		 */
1552 		if (nname != aname)
1553 			(void) strncpy((char *)nname, aname, PATH_MAX);
1554 	}
1555 	return (file_open(0, lml, oname, nname, clmp, flags, fdesc,
1556 	    rej, in_nfavl));
1557 }
1558 
1559 static int
1560 find_file(Lm_list *lml, const char *oname, Rt_map *clmp, uint_t flags,
1561     Fdesc *fdesc, Rej_desc *rej, Pnode *dir, Word * strhash, size_t olen,
1562     int *in_nfavl)
1563 {
1564 	static Rtc_obj	Obj = { 0 };
1565 	Rtc_obj		*dobj;
1566 	const char	*nname = oname;
1567 
1568 	if (dir->p_name == 0)
1569 		return (0);
1570 	if (dir->p_info) {
1571 		dobj = (Rtc_obj *)dir->p_info;
1572 		if ((dobj->co_flags &
1573 		    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
1574 			return (0);
1575 	} else
1576 		dobj = 0;
1577 
1578 	/*
1579 	 * If configuration information exists see if this directory/file
1580 	 * combination exists.
1581 	 */
1582 	if ((rtld_flags & RT_FL_DIRCFG) &&
1583 	    ((dobj == 0) || (dobj->co_id != 0))) {
1584 		Rtc_obj		*fobj;
1585 		const char	*alt = 0;
1586 
1587 		/*
1588 		 * If this pnode has not yet been searched for in the
1589 		 * configuration file go find it.
1590 		 */
1591 		if (dobj == 0) {
1592 			dobj = elf_config_ent(dir->p_name,
1593 			    (Word)elf_hash(dir->p_name), 0, 0);
1594 			if (dobj == 0)
1595 				dobj = &Obj;
1596 			dir->p_info = (void *)dobj;
1597 
1598 			if ((dobj->co_flags & (RTC_OBJ_NOEXIST |
1599 			    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
1600 				return (0);
1601 		}
1602 
1603 		/*
1604 		 * If we found a directory search for the file.
1605 		 */
1606 		if (dobj->co_id != 0) {
1607 			if (*strhash == 0)
1608 				*strhash = (Word)elf_hash(nname);
1609 			fobj = elf_config_ent(nname, *strhash,
1610 			    dobj->co_id, &alt);
1611 
1612 			/*
1613 			 * If this object specifically does not exist, or the
1614 			 * object can't be found in a know-all-entries
1615 			 * directory, continue looking.  If the object does
1616 			 * exist determine if an alternative object exists.
1617 			 */
1618 			if (fobj == 0) {
1619 				if (dobj->co_flags & RTC_OBJ_ALLENTS)
1620 					return (0);
1621 			} else {
1622 				if ((fobj->co_flags & (RTC_OBJ_NOEXIST |
1623 				    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
1624 					return (0);
1625 
1626 				if ((fobj->co_flags & RTC_OBJ_ALTER) &&
1627 				    (rtld_flags & RT_FL_OBJALT) &&
1628 				    (lml == &lml_main)) {
1629 					int	ret;
1630 
1631 					fdesc->fd_flags |= FLG_FD_ALTER;
1632 					/*
1633 					 * Attempt to open the alternative path.
1634 					 * If this fails, and the alternative is
1635 					 * flagged as optional, fall through to
1636 					 * open the original path.
1637 					 */
1638 					ret = _find_file(lml, oname, alt, clmp,
1639 					    flags, fdesc, rej, dir, 1,
1640 					    in_nfavl);
1641 					if (ret || ((fobj->co_flags &
1642 					    RTC_OBJ_OPTINAL) == 0))
1643 						return (ret);
1644 
1645 					fdesc->fd_flags &= ~FLG_FD_ALTER;
1646 				}
1647 			}
1648 		}
1649 	}
1650 
1651 	/*
1652 	 * Protect ourselves from building an invalid pathname.
1653 	 */
1654 	if ((olen + dir->p_len + 1) >= PATH_MAX) {
1655 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), nname,
1656 		    strerror(ENAMETOOLONG));
1657 			return (0);
1658 	}
1659 	if ((nname = (LM_GET_SO(clmp)(dir->p_name, nname))) == 0)
1660 		return (0);
1661 
1662 	return (_find_file(lml, oname, nname, clmp, flags, fdesc, rej,
1663 	    dir, 0, in_nfavl));
1664 }
1665 
1666 /*
1667  * A unique file has been opened.  Create a link-map to represent it, and
1668  * process the various names by which it can be referenced.
1669  */
1670 static Rt_map *
1671 load_file(Lm_list *lml, Aliste lmco, Fdesc *fdesc, int *in_nfavl)
1672 {
1673 	const char	*oname = fdesc->fd_oname;
1674 	const char	*nname = fdesc->fd_nname;
1675 	Rt_map		*nlmp;
1676 
1677 	/*
1678 	 * Typically we call fct_map_so() with the full pathname of the opened
1679 	 * file (nname) and the name that started the search (oname), thus for
1680 	 * a typical dependency on libc this would be /usr/lib/libc.so.1 and
1681 	 * libc.so.1 (DT_NEEDED).  The original name is maintained on an ALIAS
1682 	 * list for comparison when bringing in new dependencies.  If the user
1683 	 * specified name as a full path (from a dlopen() for example) then
1684 	 * there's no need to create an ALIAS.
1685 	 */
1686 	if (strcmp(oname, nname) == 0)
1687 		oname = 0;
1688 
1689 	/*
1690 	 * A new file has been opened, now map it into the process.  Close the
1691 	 * original file so as not to accumulate file descriptors.
1692 	 */
1693 	nlmp = ((fdesc->fd_ftp)->fct_map_so)(lml, lmco, nname, oname,
1694 	    fdesc->fd_fd, in_nfavl);
1695 	(void) close(fdesc->fd_fd);
1696 	fdesc->fd_fd = 0;
1697 
1698 	if (nlmp == 0)
1699 		return (0);
1700 
1701 	/*
1702 	 * Save the dev/inode information for later comparisons.
1703 	 */
1704 	STDEV(nlmp) = fdesc->fd_dev;
1705 	STINO(nlmp) = fdesc->fd_ino;
1706 
1707 	/*
1708 	 * Insert the names of this link-map into the FullpathNode AVL tree.
1709 	 * Save both the NAME() and PATHNAME() is they differ.
1710 	 *
1711 	 * If this is an OBJECT file, don't insert it yet as this is only a
1712 	 * temporary link-map.  During elf_obj_fini() the final link-map is
1713 	 * created, and its names will be inserted in the FullpathNode AVL
1714 	 * tree at that time.
1715 	 */
1716 	if ((FLAGS(nlmp) & FLG_RT_OBJECT) == 0) {
1717 		/*
1718 		 * Update the objects full path information if necessary.
1719 		 * Note, with pathname expansion in effect, the fd_pname will
1720 		 * be used as PATHNAME().  This allocated string will be freed
1721 		 * should this object be deleted.  However, without pathname
1722 		 * expansion, the fd_name should be freed now, as it is no
1723 		 * longer referenced.
1724 		 */
1725 		if (FLAGS1(nlmp) & FL1_RT_RELATIVE)
1726 			(void) fullpath(nlmp, fdesc->fd_pname);
1727 		else if (fdesc->fd_pname != fdesc->fd_nname)
1728 			free((void *)fdesc->fd_pname);
1729 		fdesc->fd_pname = 0;
1730 
1731 		if ((NAME(nlmp)[0] == '/') && (fpavl_insert(lml, nlmp,
1732 		    NAME(nlmp), fdesc->fd_avlwhere) == 0)) {
1733 			remove_so(lml, nlmp);
1734 			return (0);
1735 		}
1736 		if (((NAME(nlmp)[0] != '/') ||
1737 		    (NAME(nlmp) != PATHNAME(nlmp))) &&
1738 		    (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)) {
1739 			remove_so(lml, nlmp);
1740 			return (0);
1741 		}
1742 
1743 		/*
1744 		 * If this is a secure application, record any full path name
1745 		 * directory in which this dependency has been found.  This
1746 		 * directory can be deemed safe (as we've already found a
1747 		 * dependency here).  This recording provides a fall-back
1748 		 * should another objects $ORIGIN definition expands to this
1749 		 * directory, an expansion that would ordinarily be deemed
1750 		 * insecure.
1751 		 */
1752 		if (rtld_flags & RT_FL_SECURE) {
1753 			if (NAME(nlmp)[0] == '/')
1754 				spavl_insert(NAME(nlmp));
1755 			if ((NAME(nlmp) != PATHNAME(nlmp)) &&
1756 			    (PATHNAME(nlmp)[0] == '/'))
1757 				spavl_insert(PATHNAME(nlmp));
1758 		}
1759 	}
1760 
1761 	/*
1762 	 * If we're processing an alternative object reset the original name
1763 	 * for possible $ORIGIN processing.
1764 	 */
1765 	if (fdesc->fd_flags & FLG_FD_ALTER) {
1766 		const char	*odir;
1767 		char		*ndir;
1768 		size_t		olen;
1769 
1770 		FLAGS(nlmp) |= FLG_RT_ALTER;
1771 
1772 		/*
1773 		 * If we were given a pathname containing a slash then the
1774 		 * original name is still in oname.  Otherwise the original
1775 		 * directory is in dir->p_name (which is all we need for
1776 		 * $ORIGIN).
1777 		 */
1778 		if (fdesc->fd_flags & FLG_FD_SLASH) {
1779 			char	*ofil;
1780 
1781 			odir = oname;
1782 			ofil = strrchr(oname, '/');
1783 			olen = ofil - odir + 1;
1784 		} else {
1785 			odir = fdesc->fd_odir;
1786 			olen = strlen(odir) + 1;
1787 		}
1788 
1789 		if ((ndir = (char *)malloc(olen)) == 0) {
1790 			remove_so(lml, nlmp);
1791 			return (0);
1792 		}
1793 		(void) strncpy(ndir, odir, olen);
1794 		ndir[--olen] = '\0';
1795 
1796 		ORIGNAME(nlmp) = ndir;
1797 		DIRSZ(nlmp) = olen;
1798 	}
1799 
1800 	/*
1801 	 * Identify this as a new object.
1802 	 */
1803 	FLAGS(nlmp) |= FLG_RT_NEWLOAD;
1804 
1805 	return (nlmp);
1806 }
1807 
1808 /*
1809  * This function loads the named file and returns a pointer to its link map.
1810  * It is assumed that the caller has already checked that the file is not
1811  * already loaded before calling this function (refer is_so_loaded()).
1812  * Find and open the file, map it into memory, add it to the end of the list
1813  * of link maps and return a pointer to the new link map.  Return 0 on error.
1814  */
1815 static Rt_map *
1816 load_so(Lm_list *lml, Aliste lmco, const char *oname, Rt_map *clmp,
1817     uint_t flags, Fdesc *nfdp, Rej_desc *rej, int *in_nfavl)
1818 {
1819 	char		*name;
1820 	uint_t		slash = 0;
1821 	size_t		olen;
1822 	Fdesc		fdesc = { 0 };
1823 	Pnode		*dir;
1824 
1825 	/*
1826 	 * If the file is the run time linker then it's already loaded.
1827 	 */
1828 	if (interp && (strcmp(oname, NAME(lml_rtld.lm_head)) == 0))
1829 		return (lml_rtld.lm_head);
1830 
1831 	/*
1832 	 * If this isn't a hardware capabilities pathname, which is already a
1833 	 * full, duplicated pathname, determine whether the pathname contains
1834 	 * a slash, and if not determine the input filename (for max path
1835 	 * length verification).
1836 	 */
1837 	if ((flags & FLG_RT_HWCAP) == 0) {
1838 		const char	*str;
1839 
1840 		for (str = oname; *str; str++) {
1841 			if (*str == '/') {
1842 				slash++;
1843 				break;
1844 			}
1845 		}
1846 		if (slash == 0)
1847 			olen = (str - oname) + 1;
1848 	}
1849 
1850 	/*
1851 	 * If we are passed a 'null' link-map this means that this is the first
1852 	 * object to be loaded on this link-map list.  In that case we set the
1853 	 * link-map to ld.so.1's link-map.
1854 	 *
1855 	 * This link-map is referenced to determine what lookup rules to use
1856 	 * when searching for files.  By using ld.so.1's we are defaulting to
1857 	 * ELF look-up rules.
1858 	 *
1859 	 * Note: This case happens when loading the first object onto
1860 	 *	 the plt_tracing link-map.
1861 	 */
1862 	if (clmp == 0)
1863 		clmp = lml_rtld.lm_head;
1864 
1865 	/*
1866 	 * If this path resulted from a $HWCAP specification, then the best
1867 	 * hardware capability object has already been establish, and is
1868 	 * available in the calling file descriptor.  Perform some minor book-
1869 	 * keeping so that we can fall through into common code.
1870 	 */
1871 	if (flags & FLG_RT_HWCAP) {
1872 		/*
1873 		 * If this object is already loaded, we're done.
1874 		 */
1875 		if (nfdp->fd_lmp)
1876 			return (nfdp->fd_lmp);
1877 
1878 		/*
1879 		 * Obtain the avl index for this object.
1880 		 */
1881 		(void) fpavl_recorded(lml, nfdp->fd_nname,
1882 		    &(nfdp->fd_avlwhere));
1883 
1884 		/*
1885 		 * If the name and resolved pathname differ, duplicate the path
1886 		 * name once more to provide for generic cleanup by the caller.
1887 		 */
1888 		if (nfdp->fd_pname && (nfdp->fd_nname != nfdp->fd_pname)) {
1889 			char	*pname;
1890 
1891 			if ((pname = strdup(nfdp->fd_pname)) == NULL)
1892 				return (0);
1893 			nfdp->fd_pname = pname;
1894 		}
1895 	} else if (slash) {
1896 		Rej_desc	_rej = { 0 };
1897 
1898 		*nfdp = fdesc;
1899 		nfdp->fd_flags = FLG_FD_SLASH;
1900 
1901 		if (find_path(lml, oname, clmp, flags, nfdp,
1902 		    &_rej, in_nfavl) == 0) {
1903 			rejection_inherit(rej, &_rej);
1904 			return (0);
1905 		}
1906 
1907 		/*
1908 		 * If this object is already loaded, we're done.
1909 		 */
1910 		if (nfdp->fd_lmp)
1911 			return (nfdp->fd_lmp);
1912 
1913 	} else {
1914 		/*
1915 		 * No '/' - for each directory on list, make a pathname using
1916 		 * that directory and filename and try to open that file.
1917 		 */
1918 		Pnode		*dirlist = (Pnode *)0;
1919 		Word		strhash = 0;
1920 #if	!defined(ISSOLOAD_BASENAME_DISABLED)
1921 		Rt_map		*nlmp;
1922 #endif
1923 		DBG_CALL(Dbg_libs_find(lml, oname));
1924 
1925 #if	!defined(ISSOLOAD_BASENAME_DISABLED)
1926 		if ((nlmp = is_so_loaded(lml, oname, in_nfavl)))
1927 			return (nlmp);
1928 #endif
1929 		/*
1930 		 * Make sure we clear the file descriptor new name in case the
1931 		 * following directory search doesn't provide any directories
1932 		 * (odd, but this can be forced with a -znodefaultlib test).
1933 		 */
1934 		*nfdp = fdesc;
1935 		for (dir = get_next_dir(&dirlist, clmp, flags); dir;
1936 		    dir = get_next_dir(&dirlist, clmp, flags)) {
1937 			Rej_desc	_rej = { 0 };
1938 
1939 			*nfdp = fdesc;
1940 
1941 			/*
1942 			 * Under debugging, duplicate path name entries are
1943 			 * tagged but remain part of the search path list so
1944 			 * that they can be diagnosed under "unused" processing.
1945 			 * Skip these entries, as this path would have already
1946 			 * been attempted.
1947 			 */
1948 			if (dir->p_orig & PN_FLG_DUPLICAT)
1949 				continue;
1950 
1951 			/*
1952 			 * Try and locate this file.  Make sure to clean up
1953 			 * any rejection information should the file have
1954 			 * been found, but not appropriate.
1955 			 */
1956 			if (find_file(lml, oname, clmp, flags, nfdp, &_rej,
1957 			    dir, &strhash, olen, in_nfavl) == 0) {
1958 				rejection_inherit(rej, &_rej);
1959 				continue;
1960 			}
1961 
1962 			/*
1963 			 * Indicate that this search path has been used.  If
1964 			 * this is an LD_LIBRARY_PATH setting, ignore any use
1965 			 * by ld.so.1 itself.
1966 			 */
1967 			if (((dir->p_orig & LA_SER_LIBPATH) == 0) ||
1968 			    ((lml->lm_flags & LML_FLG_RTLDLM) == 0))
1969 				dir->p_orig |= PN_FLG_USED;
1970 
1971 			/*
1972 			 * If this object is already loaded, we're done.
1973 			 */
1974 			if (nfdp->fd_lmp)
1975 				return (nfdp->fd_lmp);
1976 
1977 			nfdp->fd_odir = dir->p_name;
1978 			break;
1979 		}
1980 
1981 		/*
1982 		 * If the file couldn't be loaded, do another comparison of
1983 		 * loaded files using just the basename.  This catches folks
1984 		 * who may have loaded multiple full pathname files (possibly
1985 		 * from setxid applications) to satisfy dependency relationships
1986 		 * (i.e., a file might have a dependency on foo.so.1 which has
1987 		 * already been opened using its full pathname).
1988 		 */
1989 		if (nfdp->fd_nname == NULL)
1990 			return (is_so_loaded(lml, oname, in_nfavl));
1991 	}
1992 
1993 	/*
1994 	 * Duplicate the file name so that NAME() is available in core files.
1995 	 * Note, that hardware capability names are already duplicated, but
1996 	 * they get duplicated once more to insure consistent cleanup in the
1997 	 * event of an error condition.
1998 	 */
1999 	if ((name = strdup(nfdp->fd_nname)) == NULL)
2000 		return (0);
2001 
2002 	if (nfdp->fd_nname == nfdp->fd_pname)
2003 		nfdp->fd_nname = nfdp->fd_pname = name;
2004 	else
2005 		nfdp->fd_nname = name;
2006 
2007 	/*
2008 	 * Finish mapping the file and return the link-map descriptor.  Note,
2009 	 * if this request originated from a HWCAP request, re-establish the
2010 	 * fdesc information.  For single paged objects, such as filters, the
2011 	 * original mapping may have been sufficient to capture the file, thus
2012 	 * this mapping needs to be reset to insure it doesn't mistakenly get
2013 	 * unmapped as part of HWCAP cleanup.
2014 	 */
2015 	return (load_file(lml, lmco, nfdp, in_nfavl));
2016 }
2017 
2018 /*
2019  * Trace an attempt to load an object.
2020  */
2021 int
2022 load_trace(Lm_list *lml, const char **oname, Rt_map *clmp)
2023 {
2024 	const char	*name = *oname;
2025 
2026 	/*
2027 	 * First generate any ldd(1) diagnostics.
2028 	 */
2029 	if ((lml->lm_flags & (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
2030 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
2031 		(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name, NAME(clmp));
2032 
2033 	/*
2034 	 * If we're being audited tell the audit library of the file we're
2035 	 * about to go search for.
2036 	 */
2037 	if (((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_ACTIVITY) &&
2038 	    (lml == LIST(clmp)))
2039 		audit_activity(clmp, LA_ACT_ADD);
2040 
2041 	if ((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
2042 		char	*aname = audit_objsearch(clmp, name, LA_SER_ORIG);
2043 
2044 		/*
2045 		 * The auditor can indicate that this object should be ignored.
2046 		 */
2047 		if (aname == NULL) {
2048 			DBG_CALL(Dbg_audit_terminate(lml, name));
2049 			return (0);
2050 		}
2051 
2052 		/*
2053 		 * Protect ourselves from auditor mischief, by duplicating any
2054 		 * alternative name.  The original name has been allocated from
2055 		 * expand(), so free this allocation before using the audit
2056 		 * alternative.
2057 		 */
2058 		if (name != aname) {
2059 			if ((aname = strdup(aname)) == NULL) {
2060 				eprintf(lml, ERR_FATAL,
2061 				    MSG_INTL(MSG_GEN_AUDITERM), name);
2062 				return (0);
2063 			}
2064 			free((void *)*oname);
2065 			*oname = aname;
2066 		}
2067 	}
2068 	return (1);
2069 }
2070 
2071 /*
2072  * Having loaded an object and created a link-map to describe it, finish
2073  * processing this stage, including verifying any versioning requirements,
2074  * updating the objects mode, creating a handle if necessary, and adding this
2075  * object to existing handles if required.
2076  */
2077 static int
2078 load_finish(Lm_list *lml, const char *name, Rt_map *clmp, int nmode,
2079     uint_t flags, Grp_hdl **hdl, Rt_map *nlmp)
2080 {
2081 	Aliste		idx;
2082 	Grp_hdl		*ghp;
2083 	int		promote;
2084 
2085 	/*
2086 	 * If this dependency is associated with a required version insure that
2087 	 * the version is present in the loaded file.
2088 	 */
2089 	if (((rtld_flags & RT_FL_NOVERSION) == 0) &&
2090 	    (FCT(clmp) == &elf_fct) && VERNEED(clmp) &&
2091 	    (LM_VERIFY_VERS(clmp)(name, clmp, nlmp) == 0))
2092 		return (0);
2093 
2094 	/*
2095 	 * If this object has indicated that it should be isolated as a group
2096 	 * (DT_FLAGS_1 contains DF_1_GROUP - object was built with -B group),
2097 	 * or if the callers direct bindings indicate it should be isolated as
2098 	 * a group (DYNINFO flags contains FLG_DI_GROUP - dependency followed
2099 	 * -zgroupperm), establish the appropriate mode.
2100 	 *
2101 	 * The intent of an object defining itself as a group is to isolate the
2102 	 * relocation of the group within its own members, however, unless
2103 	 * opened through dlopen(), in which case we assume dlsym() will be used
2104 	 * to located symbols in the new object, we still need to associate it
2105 	 * with the caller for it to be bound with.  This is equivalent to a
2106 	 * dlopen(RTLD_GROUP) and dlsym() using the returned handle.
2107 	 */
2108 	if ((FLAGS(nlmp) | flags) & FLG_RT_SETGROUP) {
2109 		nmode &= ~RTLD_WORLD;
2110 		nmode |= RTLD_GROUP;
2111 
2112 		/*
2113 		 * If the object wasn't explicitly dlopen()'ed associate it with
2114 		 * the parent.
2115 		 */
2116 		if ((flags & FLG_RT_HANDLE) == 0)
2117 			nmode |= RTLD_PARENT;
2118 	}
2119 
2120 	/*
2121 	 * Establish new mode and flags.
2122 	 *
2123 	 * For patch backward compatibility, the following use of update_mode()
2124 	 * is disabled.
2125 	 */
2126 #ifdef	SIEBEL_DISABLE
2127 	if (rtld_flags & RT_FL_DISFIX_1)
2128 		promote = MODE(nlmp) |=
2129 		    (nmode & ~(RTLD_PARENT | RTLD_NOLOAD | RTLD_FIRST));
2130 	else
2131 #endif
2132 		promote = update_mode(nlmp, MODE(nlmp), nmode);
2133 
2134 	FLAGS(nlmp) |= flags;
2135 
2136 	/*
2137 	 * If this is a global object, ensure the associated link-map list can
2138 	 * be rescanned for global, lazy dependencies.
2139 	 */
2140 	if (MODE(nlmp) & RTLD_GLOBAL)
2141 		LIST(nlmp)->lm_flags &= ~LML_FLG_NOPENDGLBLAZY;
2142 
2143 	/*
2144 	 * If we've been asked to establish a handle create one for this object.
2145 	 * Or, if this object has already been analyzed, but this reference
2146 	 * requires that the mode of the object be promoted, also create a
2147 	 * handle to propagate the new modes to all this objects dependencies.
2148 	 */
2149 	if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) || (promote &&
2150 	    (FLAGS(nlmp) & FLG_RT_ANALYZED))) {
2151 		uint_t	oflags, hflags = 0, cdflags;
2152 
2153 		/*
2154 		 * Establish any flags for the handle (Grp_hdl).
2155 		 *
2156 		 *  .	Use of the RTLD_FIRST flag indicates that only the first
2157 		 *	dependency on the handle (the new object) can be used
2158 		 *	to satisfy dlsym() requests.
2159 		 */
2160 		if (nmode & RTLD_FIRST)
2161 			hflags = GPH_FIRST;
2162 
2163 		/*
2164 		 * Establish the flags for this callers dependency descriptor
2165 		 * (Grp_desc).
2166 		 *
2167 		 *  .	The creation of a handle associated a descriptor for the
2168 		 *	new object and descriptor for the parent (caller).
2169 		 *	Typically, the handle is created for dlopen() or for
2170 		 *	filtering.  A handle may also be created to promote
2171 		 *	the callers modes (RTLD_NOW) to the new object.  In this
2172 		 *	latter case, the handle/descriptor are torn down once
2173 		 *	the mode propagation has occurred.
2174 		 *
2175 		 *  .	Use of the RTLD_PARENT flag indicates that the parent
2176 		 *	can be relocated against.
2177 		 */
2178 		if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) == 0)
2179 			cdflags = GPD_PROMOTE;
2180 		else
2181 			cdflags = GPD_PARENT;
2182 		if (nmode & RTLD_PARENT)
2183 			cdflags |= GPD_RELOC;
2184 
2185 		/*
2186 		 * Now that a handle is being created, remove this state from
2187 		 * the object so that it doesn't mistakenly get inherited by
2188 		 * a dependency.
2189 		 */
2190 		oflags = FLAGS(nlmp);
2191 		FLAGS(nlmp) &= ~FLG_RT_HANDLE;
2192 
2193 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
2194 		if ((ghp = hdl_create(lml, nlmp, clmp, hflags,
2195 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), cdflags)) == 0)
2196 			return (0);
2197 
2198 		/*
2199 		 * Add any dependencies that are already loaded, to the handle.
2200 		 */
2201 		if (hdl_initialize(ghp, nlmp, nmode, promote) == 0)
2202 			return (0);
2203 
2204 		if (hdl)
2205 			*hdl = ghp;
2206 
2207 		/*
2208 		 * If we were asked to create a handle, we're done.
2209 		 */
2210 		if ((oflags | flags) & FLG_RT_HANDLE)
2211 			return (1);
2212 
2213 		/*
2214 		 * If the handle was created to promote modes from the parent
2215 		 * (caller) to the new object, then this relationship needs to
2216 		 * be removed to ensure the handle doesn't prevent the new
2217 		 * objects from being deleted if required.  If the parent is
2218 		 * the only dependency on the handle, then the handle can be
2219 		 * completely removed.  However, the handle may have already
2220 		 * existed, in which case only the parent descriptor can be
2221 		 * deleted from the handle, or at least the GPD_PROMOTE flag
2222 		 * removed from the descriptor.
2223 		 *
2224 		 * Fall through to carry out any group processing.
2225 		 */
2226 		free_hdl(ghp, clmp, GPD_PROMOTE);
2227 	}
2228 
2229 	/*
2230 	 * If the caller isn't part of a group we're done.
2231 	 */
2232 	if (GROUPS(clmp) == NULL)
2233 		return (1);
2234 
2235 	/*
2236 	 * Determine if our caller is already associated with a handle, if so
2237 	 * we need to add this object to any handles that already exist.
2238 	 * Traverse the list of groups our caller is a member of and add this
2239 	 * new link-map to those groups.
2240 	 */
2241 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
2242 	for (APLIST_TRAVERSE(GROUPS(clmp), idx, ghp)) {
2243 		Aliste		idx1;
2244 		Grp_desc	*gdp;
2245 		int		exist;
2246 		Rt_map		*dlmp1;
2247 		APlist		*lmalp = NULL;
2248 
2249 		/*
2250 		 * If the caller doesn't indicate that its dependencies should
2251 		 * be added to a handle, ignore it.  This case identifies a
2252 		 * parent of a dlopen(RTLD_PARENT) request.
2253 		 */
2254 		for (ALIST_TRAVERSE(ghp->gh_depends, idx1, gdp)) {
2255 			if (gdp->gd_depend == clmp)
2256 				break;
2257 		}
2258 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
2259 			continue;
2260 
2261 		if ((exist = hdl_add(ghp, nlmp,
2262 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0)
2263 			return (0);
2264 
2265 		/*
2266 		 * If this member already exists then its dependencies will
2267 		 * have already been processed.
2268 		 */
2269 		if (exist == ALE_EXISTS)
2270 			continue;
2271 
2272 		/*
2273 		 * If the object we've added has just been opened, it will not
2274 		 * yet have been processed for its dependencies, these will be
2275 		 * added on later calls to load_one().  If it doesn't have any
2276 		 * dependencies we're also done.
2277 		 */
2278 		if (((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0) ||
2279 		    (DEPENDS(nlmp) == NULL))
2280 			continue;
2281 
2282 		/*
2283 		 * Otherwise, this object exists and has dependencies, so add
2284 		 * all of its dependencies to the handle were operating on.
2285 		 */
2286 		if (aplist_append(&lmalp, nlmp, AL_CNT_DEPCLCT) == 0)
2287 			return (0);
2288 
2289 		for (APLIST_TRAVERSE(lmalp, idx1, dlmp1)) {
2290 			Aliste		idx2;
2291 			Bnd_desc 	*bdp;
2292 
2293 			/*
2294 			 * Add any dependencies of this dependency to the
2295 			 * dynamic dependency list so they can be further
2296 			 * processed.
2297 			 */
2298 			for (APLIST_TRAVERSE(DEPENDS(dlmp1), idx2, bdp)) {
2299 				Rt_map	*dlmp2 = bdp->b_depend;
2300 
2301 				if ((bdp->b_flags & BND_NEEDED) == 0)
2302 					continue;
2303 
2304 				if (aplist_test(&lmalp, dlmp2,
2305 				    AL_CNT_DEPCLCT) == 0) {
2306 					free(lmalp);
2307 					return (0);
2308 				}
2309 			}
2310 
2311 			if (nlmp == dlmp1)
2312 				continue;
2313 
2314 			if ((exist = hdl_add(ghp, dlmp1,
2315 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) != 0) {
2316 				if (exist == ALE_CREATE) {
2317 					(void) update_mode(dlmp1, MODE(dlmp1),
2318 					    nmode);
2319 				}
2320 				continue;
2321 			}
2322 			free(lmalp);
2323 			return (0);
2324 		}
2325 		free(lmalp);
2326 	}
2327 	return (1);
2328 }
2329 
2330 /*
2331  * The central routine for loading shared objects.  Insures ldd() diagnostics,
2332  * handles and any other related additions are all done in one place.
2333  */
2334 static Rt_map *
2335 _load_path(Lm_list *lml, Aliste lmco, const char **oname, Rt_map *clmp,
2336     int nmode, uint_t flags, Grp_hdl ** hdl, Fdesc *nfdp, Rej_desc *rej,
2337     int *in_nfavl)
2338 {
2339 	Rt_map		*nlmp;
2340 	const char	*name = *oname;
2341 
2342 	if ((nmode & RTLD_NOLOAD) == 0) {
2343 		/*
2344 		 * If this isn't a noload request attempt to load the file.
2345 		 * Note, the name of the file may be changed by an auditor.
2346 		 */
2347 		if ((load_trace(lml, oname, clmp)) == 0)
2348 			return (0);
2349 
2350 		name = *oname;
2351 
2352 		if ((nlmp = load_so(lml, lmco, name, clmp, flags,
2353 		    nfdp, rej, in_nfavl)) == 0)
2354 			return (0);
2355 
2356 		/*
2357 		 * If we've loaded a library which identifies itself as not
2358 		 * being dlopen()'able catch it here.  Let non-dlopen()'able
2359 		 * objects through under RTLD_CONFGEN as they're only being
2360 		 * mapped to be dldump()'ed.
2361 		 */
2362 		if ((rtld_flags & RT_FL_APPLIC) && ((FLAGS(nlmp) &
2363 		    (FLG_RT_NOOPEN | FLG_RT_RELOCED)) == FLG_RT_NOOPEN) &&
2364 		    ((nmode & RTLD_CONFGEN) == 0)) {
2365 			Rej_desc	_rej = { 0 };
2366 
2367 			_rej.rej_name = name;
2368 			_rej.rej_type = SGS_REJ_STR;
2369 			_rej.rej_str = MSG_INTL(MSG_GEN_NOOPEN);
2370 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
2371 			rejection_inherit(rej, &_rej);
2372 			remove_so(lml, nlmp);
2373 			return (0);
2374 		}
2375 	} else {
2376 		/*
2377 		 * If it's a NOLOAD request - check to see if the object
2378 		 * has already been loaded.
2379 		 */
2380 		/* LINTED */
2381 		if (nlmp = is_so_loaded(lml, name, in_nfavl)) {
2382 			if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
2383 			    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
2384 				(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name,
2385 				    NAME(clmp));
2386 				/* BEGIN CSTYLED */
2387 				if (*name == '/')
2388 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
2389 					name, MSG_ORIG(MSG_STR_EMPTY),
2390 					MSG_ORIG(MSG_STR_EMPTY));
2391 				else
2392 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
2393 					name, NAME(nlmp),
2394 					MSG_ORIG(MSG_STR_EMPTY),
2395 					MSG_ORIG(MSG_STR_EMPTY));
2396 				/* END CSTYLED */
2397 			}
2398 		} else {
2399 			Rej_desc	_rej = { 0 };
2400 
2401 			_rej.rej_name = name;
2402 			_rej.rej_type = SGS_REJ_STR;
2403 			_rej.rej_str = strerror(ENOENT);
2404 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
2405 			rejection_inherit(rej, &_rej);
2406 			return (0);
2407 		}
2408 	}
2409 
2410 	/*
2411 	 * Finish processing this loaded object.
2412 	 */
2413 	if (load_finish(lml, name, clmp, nmode, flags, hdl, nlmp) == 0) {
2414 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2415 
2416 		/*
2417 		 * If this object has already been analyzed, then it is in use,
2418 		 * so even though this operation has failed, it should not be
2419 		 * torn down.
2420 		 */
2421 		if ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)
2422 			remove_so(lml, nlmp);
2423 		return (0);
2424 	}
2425 
2426 	/*
2427 	 * If this object is new, and we're being audited, tell the audit
2428 	 * library of the file we've just opened.  Note, if the new link-map
2429 	 * requires local auditing of its dependencies we also register its
2430 	 * opening.
2431 	 */
2432 	if (FLAGS(nlmp) & FLG_RT_NEWLOAD) {
2433 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2434 
2435 		if (((lml->lm_tflags | FLAGS1(clmp) | FLAGS1(nlmp)) &
2436 		    LML_TFLG_AUD_MASK) && (((lml->lm_flags |
2437 		    LIST(clmp)->lm_flags) & LML_FLG_NOAUDIT) == 0)) {
2438 			if (audit_objopen(clmp, nlmp) == 0) {
2439 				remove_so(lml, nlmp);
2440 				return (0);
2441 			}
2442 		}
2443 	}
2444 	return (nlmp);
2445 }
2446 
2447 Rt_map *
2448 load_path(Lm_list *lml, Aliste lmco, const char **name, Rt_map *clmp, int nmode,
2449     uint_t flags, Grp_hdl **hdl, Fdesc *cfdp, Rej_desc *rej, int *in_nfavl)
2450 {
2451 	Rt_map	*lmp;
2452 	Fdesc	nfdp = { 0 };
2453 
2454 	/*
2455 	 * If this path resulted from a $HWCAP specification, then the best
2456 	 * hardware capability object has already been establish, and is
2457 	 * available in the calling file descriptor.
2458 	 */
2459 	if (flags & FLG_RT_HWCAP) {
2460 		if (cfdp->fd_lmp == 0) {
2461 			/*
2462 			 * If this object hasn't yet been mapped, re-establish
2463 			 * the file descriptor structure to reflect this objects
2464 			 * original initial page mapping.  Make sure any present
2465 			 * file descriptor mapping is removed before overwriting
2466 			 * the structure.
2467 			 */
2468 #if	defined(MAP_ALIGN)
2469 			if (fmap->fm_maddr &&
2470 			    ((fmap->fm_mflags & MAP_ALIGN) == 0))
2471 #else
2472 			if (fmap->fm_maddr)
2473 #endif
2474 				(void) munmap(fmap->fm_maddr, fmap->fm_msize);
2475 
2476 			*fmap = cfdp->fd_fmap;
2477 		}
2478 		nfdp = *cfdp;
2479 	}
2480 
2481 	lmp = _load_path(lml, lmco, name, clmp, nmode, flags, hdl, &nfdp,
2482 	    rej, in_nfavl);
2483 
2484 	/*
2485 	 * If this path originated from a $HWCAP specification, re-establish the
2486 	 * fdesc information.  For single paged objects, such as filters, the
2487 	 * original mapping may have been sufficient to capture the file, thus
2488 	 * this mapping needs to be reset to insure it doesn't mistakenly get
2489 	 * unmapped as part of HWCAP cleanup.
2490 	 */
2491 	if ((flags & FLG_RT_HWCAP) && (cfdp->fd_lmp == 0)) {
2492 		cfdp->fd_fmap.fm_maddr = fmap->fm_maddr;
2493 		cfdp->fd_fmap.fm_mflags = fmap->fm_mflags;
2494 		cfdp->fd_fd = nfdp.fd_fd;
2495 	}
2496 
2497 	return (lmp);
2498 }
2499 
2500 /*
2501  * Load one object from a possible list of objects.  Typically, for requests
2502  * such as NEEDED's, only one object is specified.  However, this object could
2503  * be specified using $ISALIST or $HWCAP, in which case only the first object
2504  * that can be loaded is used (ie. the best).
2505  */
2506 Rt_map *
2507 load_one(Lm_list *lml, Aliste lmco, Pnode *pnp, Rt_map *clmp, int mode,
2508     uint_t flags, Grp_hdl **hdl, int *in_nfavl)
2509 {
2510 	Rej_desc	rej = { 0 };
2511 	Pnode   	*tpnp;
2512 	const char	*name;
2513 
2514 	for (tpnp = pnp; tpnp && tpnp->p_name; tpnp = tpnp->p_next) {
2515 		Rt_map	*tlmp;
2516 
2517 		/*
2518 		 * A Hardware capabilities requirement can itself expand into
2519 		 * a number of candidates.
2520 		 */
2521 		if (tpnp->p_orig & PN_TKN_HWCAP) {
2522 			if ((tlmp = load_hwcap(lml, lmco, tpnp->p_name, clmp,
2523 			    mode, (flags | FLG_RT_HWCAP), hdl, &rej,
2524 			    in_nfavl)) != 0) {
2525 				remove_rej(&rej);
2526 				return (tlmp);
2527 			}
2528 		} else {
2529 			if ((tlmp = load_path(lml, lmco, &tpnp->p_name, clmp,
2530 			    mode, flags, hdl, 0, &rej, in_nfavl)) != 0) {
2531 				remove_rej(&rej);
2532 				return (tlmp);
2533 			}
2534 		}
2535 	}
2536 
2537 	/*
2538 	 * If this pathname originated from an expanded token, use the original
2539 	 * for any diagnostic output.
2540 	 */
2541 	if ((name = pnp->p_oname) == 0)
2542 		name = pnp->p_name;
2543 
2544 	file_notfound(lml, name, clmp, flags, &rej);
2545 	remove_rej(&rej);
2546 	return (0);
2547 }
2548 
2549 /*
2550  * Determine whether a symbol is defined as an interposer.
2551  */
2552 int
2553 is_sym_interposer(Rt_map *lmp, Sym *sym)
2554 {
2555 	Syminfo	*sip = SYMINFO(lmp);
2556 
2557 	if (sip) {
2558 		ulong_t	ndx;
2559 
2560 		ndx = (((ulong_t)sym - (ulong_t)SYMTAB(lmp)) / SYMENT(lmp));
2561 		/* LINTED */
2562 		sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(lmp)));
2563 		if (sip->si_flags & SYMINFO_FLG_INTERPOSE)
2564 			return (1);
2565 	}
2566 	return (0);
2567 }
2568 
2569 /*
2570  * While processing direct or group bindings, determine whether the object to
2571  * which we've bound can be interposed upon.  In this context, copy relocations
2572  * are a form of interposition.
2573  */
2574 static Sym *
2575 lookup_sym_interpose(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Sym *osym,
2576     int *in_nfavl)
2577 {
2578 	Rt_map		*lmp, *clmp;
2579 	Slookup		sl;
2580 	Lm_list		*lml;
2581 
2582 	/*
2583 	 * If we've bound to a copy relocation definition then we need to assign
2584 	 * this binding to the original copy reference.  Fabricate an inter-
2585 	 * position diagnostic, as this is a legitimate form of interposition.
2586 	 */
2587 	if (osym && (FLAGS1(*dlmp) & FL1_RT_COPYTOOK)) {
2588 		Rel_copy	*rcp;
2589 		Aliste		idx;
2590 
2591 		for (ALIST_TRAVERSE(COPY_R(*dlmp), idx, rcp)) {
2592 			if ((osym == rcp->r_dsym) || (osym->st_value &&
2593 			    (osym->st_value == rcp->r_dsym->st_value))) {
2594 				*dlmp = rcp->r_rlmp;
2595 				*binfo |=
2596 				    (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
2597 				return (rcp->r_rsym);
2598 			}
2599 		}
2600 	}
2601 
2602 	/*
2603 	 * If a symbol binding has been established, inspect the link-map list
2604 	 * of the destination object, otherwise use the link-map list of the
2605 	 * original caller.
2606 	 */
2607 	if (osym)
2608 		clmp = *dlmp;
2609 	else
2610 		clmp = slp->sl_cmap;
2611 
2612 	lml = LIST(clmp);
2613 	lmp = lml->lm_head;
2614 
2615 	/*
2616 	 * Prior to Solaris 8, external references from an executable that were
2617 	 * bound to an uninitialized variable (.bss) within a shared object did
2618 	 * not establish a copy relocation.  This was thought to be an
2619 	 * optimization, to prevent copying zero's to zero's.  Typically,
2620 	 * interposition took its course, with the shared object binding to the
2621 	 * executables data definition.
2622 	 *
2623 	 * This scenario can be broken when this old executable runs against a
2624 	 * new shared object that is directly bound.  With no copy-relocation
2625 	 * record, ld.so.1 has no data to trigger the normal vectoring of the
2626 	 * binding to the executable.
2627 	 *
2628 	 * Starting with Solaris 8, a DT_FLAGS entry is written to all objects,
2629 	 * regardless of there being any DF_ flags entries.  Therefore, an
2630 	 * object without this dynamic tag is susceptible to the copy relocation
2631 	 * issue.  If the executable has no DT_FLAGS tag, and contains the same
2632 	 * .bss symbol definition as has been directly bound to, redirect the
2633 	 * binding to the executables data definition.
2634 	 */
2635 	if (osym && ((FLAGS2(lmp) & FL2_RT_DTFLAGS) == 0) &&
2636 	    (FCT(lmp) == &elf_fct) &&
2637 	    (ELF_ST_TYPE(osym->st_info) != STT_FUNC) &&
2638 	    are_bits_zero(*dlmp, osym, 0)) {
2639 		Rt_map	*ilmp;
2640 		Sym	*isym;
2641 
2642 		sl = *slp;
2643 		sl.sl_imap = lmp;
2644 
2645 		/*
2646 		 * Determine whether the same symbol name exists within the
2647 		 * executable, that the size and type of symbol are the same,
2648 		 * and that the symbol is also associated with .bss.
2649 		 */
2650 		if (((isym = SYMINTP(lmp)(&sl, &ilmp, binfo,
2651 		    in_nfavl)) != NULL) && (isym->st_size == osym->st_size) &&
2652 		    (isym->st_info == osym->st_info) &&
2653 		    are_bits_zero(lmp, isym, 1)) {
2654 			*dlmp = lmp;
2655 			*binfo |= (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
2656 			return (isym);
2657 		}
2658 	}
2659 
2660 	if ((lml->lm_flags & LML_FLG_INTRPOSE) == 0)
2661 		return ((Sym *)0);
2662 
2663 	/*
2664 	 * Traverse the list of known interposers to determine whether any
2665 	 * offer the same symbol.  Note, the head of the link-map could be
2666 	 * identified as an interposer.  Otherwise, skip the head of the
2667 	 * link-map, so that we don't bind to any .plt references, or
2668 	 * copy-relocation destinations unintentionally.
2669 	 */
2670 	lmp = lml->lm_head;
2671 	sl = *slp;
2672 
2673 	if (((FLAGS(lmp) & MSK_RT_INTPOSE) == 0) || (sl.sl_flags & LKUP_COPY))
2674 		lmp = (Rt_map *)NEXT(lmp);
2675 
2676 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
2677 		if (FLAGS(lmp) & FLG_RT_DELETE)
2678 			continue;
2679 		if ((FLAGS(lmp) & MSK_RT_INTPOSE) == 0)
2680 			break;
2681 
2682 		if (callable(lmp, clmp, 0, sl.sl_flags)) {
2683 			Rt_map	*ilmp;
2684 			Sym	*isym;
2685 
2686 			sl.sl_imap = lmp;
2687 			if (isym = SYMINTP(lmp)(&sl, &ilmp, binfo, in_nfavl)) {
2688 				/*
2689 				 * If this object provides individual symbol
2690 				 * interposers, make sure that the symbol we
2691 				 * have found is tagged as an interposer.
2692 				 */
2693 				if ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
2694 				    (is_sym_interposer(ilmp, isym) == 0))
2695 					continue;
2696 
2697 				/*
2698 				 * Indicate this binding has occurred to an
2699 				 * interposer, and return the symbol.
2700 				 */
2701 				*binfo |= DBG_BINFO_INTERPOSE;
2702 				*dlmp = ilmp;
2703 				return (isym);
2704 			}
2705 		}
2706 	}
2707 	return ((Sym *)0);
2708 }
2709 
2710 /*
2711  * If an object specifies direct bindings (it contains a syminfo structure
2712  * describing where each binding was established during link-editing, and the
2713  * object was built -Bdirect), then look for the symbol in the specific object.
2714  */
2715 static Sym *
2716 lookup_sym_direct(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Syminfo *sip,
2717     Rt_map *lmp, int *in_nfavl)
2718 {
2719 	Rt_map	*clmp = slp->sl_cmap;
2720 	Sym	*sym;
2721 	Slookup	sl;
2722 
2723 	/*
2724 	 * If a direct binding resolves to the definition of a copy relocated
2725 	 * variable, it must be redirected to the copy (in the executable) that
2726 	 * will eventually be made.  Typically, this redirection occurs in
2727 	 * lookup_sym_interpose().  But, there's an edge condition.  If a
2728 	 * directly bound executable contains pic code, there may be a
2729 	 * reference to a definition that will eventually have a copy made.
2730 	 * However, this copy relocation may not yet have occurred, because
2731 	 * the relocation making this reference comes before the relocation
2732 	 * that will create the copy.
2733 	 * Under direct bindings, the syminfo indicates that a copy will be
2734 	 * taken (SYMINFO_FLG_COPY).  This can only be set in an executable.
2735 	 * Thus, the caller must be the executable, so bind to the destination
2736 	 * of the copy within the executable.
2737 	 */
2738 	if (((slp->sl_flags & LKUP_COPY) == 0) &&
2739 	    (sip->si_flags & SYMINFO_FLG_COPY)) {
2740 
2741 		slp->sl_imap = LIST(clmp)->lm_head;
2742 		if (sym = SYMINTP(clmp)(slp, dlmp, binfo, in_nfavl))
2743 			*binfo |= (DBG_BINFO_DIRECT | DBG_BINFO_COPYREF);
2744 		return (sym);
2745 	}
2746 
2747 	/*
2748 	 * If we need to directly bind to our parent, start looking in each
2749 	 * callers link map.
2750 	 */
2751 	sl = *slp;
2752 	sl.sl_flags |= LKUP_DIRECT;
2753 	sym = NULL;
2754 
2755 	if (sip->si_boundto == SYMINFO_BT_PARENT) {
2756 		Aliste		idx1;
2757 		Bnd_desc	*bdp;
2758 		Grp_hdl		*ghp;
2759 
2760 		/*
2761 		 * Determine the parent of this explicit dependency from its
2762 		 * CALLERS()'s list.
2763 		 */
2764 		for (APLIST_TRAVERSE(CALLERS(clmp), idx1, bdp)) {
2765 			sl.sl_imap = lmp = bdp->b_caller;
2766 			if ((sym = SYMINTP(lmp)(&sl, dlmp, binfo,
2767 			    in_nfavl)) != NULL)
2768 				goto found;
2769 		}
2770 
2771 		/*
2772 		 * A caller can also be defined as the parent of a dlopen()
2773 		 * call.  Determine whether this object has any handles.  The
2774 		 * dependencies maintained with the handle represent the
2775 		 * explicit dependencies of the dlopen()'ed object, and the
2776 		 * calling parent.
2777 		 */
2778 		for (APLIST_TRAVERSE(HANDLES(clmp), idx1, ghp)) {
2779 			Grp_desc	*gdp;
2780 			Aliste		idx2;
2781 
2782 			for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
2783 				if ((gdp->gd_flags & GPD_PARENT) == 0)
2784 					continue;
2785 				sl.sl_imap = lmp = gdp->gd_depend;
2786 				if ((sym = SYMINTP(lmp)(&sl, dlmp,
2787 				    binfo, in_nfavl)) != NULL)
2788 					goto found;
2789 			}
2790 		}
2791 	} else {
2792 		/*
2793 		 * If we need to direct bind to anything else look in the
2794 		 * link map associated with this symbol reference.
2795 		 */
2796 		if (sip->si_boundto == SYMINFO_BT_SELF)
2797 			sl.sl_imap = lmp = clmp;
2798 		else
2799 			sl.sl_imap = lmp;
2800 
2801 		if (lmp)
2802 			sym = SYMINTP(lmp)(&sl, dlmp, binfo, in_nfavl);
2803 	}
2804 found:
2805 	if (sym)
2806 		*binfo |= DBG_BINFO_DIRECT;
2807 
2808 	/*
2809 	 * If a reference to a directly bound symbol can't be satisfied, then
2810 	 * determine whether an interposer can provide the missing symbol.  If
2811 	 * a reference to a directly bound symbol is satisfied, then determine
2812 	 * whether that object can be interposed upon for this symbol.
2813 	 */
2814 	if ((sym == NULL) || ((LIST(*dlmp)->lm_head != *dlmp) &&
2815 	    (LIST(*dlmp) == LIST(clmp)))) {
2816 		Sym	*isym;
2817 
2818 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
2819 		    in_nfavl)) != 0)
2820 			return (isym);
2821 	}
2822 
2823 	return (sym);
2824 }
2825 
2826 static Sym *
2827 core_lookup_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
2828     Aliste off, int *in_nfavl)
2829 {
2830 	Rt_map	*lmp;
2831 
2832 	/*
2833 	 * Copy relocations should start their search after the head of the
2834 	 * main link-map control list.
2835 	 */
2836 	if ((off == ALIST_OFF_DATA) && (slp->sl_flags & LKUP_COPY) && ilmp)
2837 		lmp = (Rt_map *)NEXT(ilmp);
2838 	else
2839 		lmp = ilmp;
2840 
2841 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
2842 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
2843 			Sym	*sym;
2844 
2845 			slp->sl_imap = lmp;
2846 			if (((sym = SYMINTP(lmp)(slp, dlmp, binfo,
2847 			    in_nfavl)) != NULL) || (*binfo & BINFO_REJSINGLE))
2848 				return (sym);
2849 		}
2850 	}
2851 	return (0);
2852 }
2853 
2854 static Sym *
2855 _lazy_find_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
2856     int *in_nfavl)
2857 {
2858 	Rt_map	*lmp;
2859 
2860 	for (lmp = ilmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
2861 		if (LAZY(lmp) == 0)
2862 			continue;
2863 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
2864 			Sym	*sym;
2865 
2866 			slp->sl_imap = lmp;
2867 			if ((sym = elf_lazy_find_sym(slp, dlmp, binfo,
2868 			    in_nfavl)) != 0)
2869 				return (sym);
2870 		}
2871 	}
2872 	return (0);
2873 }
2874 
2875 static Sym *
2876 _lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
2877 {
2878 	const char	*name = slp->sl_name;
2879 	Rt_map		*clmp = slp->sl_cmap;
2880 	Lm_list		*lml = LIST(clmp);
2881 	Rt_map		*ilmp = slp->sl_imap, *lmp;
2882 	ulong_t		rsymndx;
2883 	Sym		*sym;
2884 	Syminfo		*sip;
2885 	Slookup		sl;
2886 
2887 	/*
2888 	 * Search the initial link map for the required symbol (this category is
2889 	 * selected by dlsym(), where individual link maps are searched for a
2890 	 * required symbol.  Therefore, we know we have permission to look at
2891 	 * the link map).
2892 	 */
2893 	if (slp->sl_flags & LKUP_FIRST)
2894 		return (SYMINTP(ilmp)(slp, dlmp, binfo, in_nfavl));
2895 
2896 	/*
2897 	 * Determine whether this lookup can be satisfied by an objects direct,
2898 	 * or lazy binding information.  This is triggered by a relocation from
2899 	 * the object (hence rsymndx is set).
2900 	 */
2901 	if (((rsymndx = slp->sl_rsymndx) != 0) &&
2902 	    ((sip = SYMINFO(clmp)) != NULL)) {
2903 		uint_t	bound;
2904 
2905 		/*
2906 		 * Find the corresponding Syminfo entry for the original
2907 		 * referencing symbol.
2908 		 */
2909 		/* LINTED */
2910 		sip = (Syminfo *)((char *)sip + (rsymndx * SYMINENT(clmp)));
2911 		bound = sip->si_boundto;
2912 
2913 		/*
2914 		 * Identify any EXTERN or PARENT references for ldd(1).
2915 		 */
2916 		if ((lml->lm_flags & LML_FLG_TRC_WARN) &&
2917 		    (bound > SYMINFO_BT_LOWRESERVE)) {
2918 			if (bound == SYMINFO_BT_PARENT)
2919 				*binfo |= DBG_BINFO_REF_PARENT;
2920 			if (bound == SYMINFO_BT_EXTERN)
2921 				*binfo |= DBG_BINFO_REF_EXTERN;
2922 		}
2923 
2924 		/*
2925 		 * If the symbol information indicates a direct binding,
2926 		 * determine the link map that is required to satisfy the
2927 		 * binding.  Note, if the dependency can not be found, but a
2928 		 * direct binding isn't required, we will still fall through
2929 		 * to perform any default symbol search.
2930 		 */
2931 		if (sip->si_flags & SYMINFO_FLG_DIRECT) {
2932 
2933 			lmp = 0;
2934 			if (bound < SYMINFO_BT_LOWRESERVE)
2935 				lmp = elf_lazy_load(clmp, slp, bound,
2936 				    name, in_nfavl);
2937 
2938 			/*
2939 			 * If direct bindings have been disabled, and this isn't
2940 			 * a translator, skip any direct binding now that we've
2941 			 * ensured the resolving object has been loaded.
2942 			 *
2943 			 * If we need to direct bind to anything, we look in
2944 			 * ourselves, our parent, or in the link map we've just
2945 			 * loaded.  Otherwise, even though we may have lazily
2946 			 * loaded an object we still continue to search for
2947 			 * symbols from the head of the link map list.
2948 			 */
2949 			if (((FLAGS(clmp) & FLG_RT_TRANS) ||
2950 			    (((lml->lm_tflags & LML_TFLG_NODIRECT) == 0) &&
2951 			    ((slp->sl_flags & LKUP_SINGLETON) == 0))) &&
2952 			    ((FLAGS1(clmp) & FL1_RT_DIRECT) ||
2953 			    (sip->si_flags & SYMINFO_FLG_DIRECTBIND))) {
2954 				sym = lookup_sym_direct(slp, dlmp, binfo,
2955 				    sip, lmp, in_nfavl);
2956 
2957 				/*
2958 				 * Determine whether this direct binding has
2959 				 * been rejected.  If we've bound to a singleton
2960 				 * without following a singleton search, then
2961 				 * return.  The caller detects this condition
2962 				 * and will trigger a new singleton search.
2963 				 *
2964 				 * For any other rejection (such as binding to
2965 				 * a symbol labeled as nodirect - presumably
2966 				 * because the symbol definition has been
2967 				 * changed since the referring object was last
2968 				 * built), fall through to a standard symbol
2969 				 * search.
2970 				 */
2971 				if (((*binfo & BINFO_REJECTED) == 0) ||
2972 				    (*binfo & BINFO_REJSINGLE))
2973 					return (sym);
2974 
2975 				*binfo &= ~BINFO_REJECTED;
2976 			}
2977 		}
2978 	}
2979 
2980 	/*
2981 	 * Duplicate the lookup information, as we'll need to modify this
2982 	 * information for some of the following searches.
2983 	 */
2984 	sl = *slp;
2985 
2986 	/*
2987 	 * If the referencing object has the DF_SYMBOLIC flag set, look in the
2988 	 * referencing object for the symbol first.  Failing that, fall back to
2989 	 * our generic search.
2990 	 */
2991 	if ((FLAGS1(clmp) & FL1_RT_SYMBOLIC) &&
2992 	    ((sl.sl_flags & LKUP_SINGLETON) == 0)) {
2993 		sl.sl_imap = clmp;
2994 		if (sym = SYMINTP(clmp)(&sl, dlmp, binfo, in_nfavl)) {
2995 			ulong_t	dsymndx = (((ulong_t)sym -
2996 			    (ulong_t)SYMTAB(*dlmp)) / SYMENT(*dlmp));
2997 
2998 			/*
2999 			 * Make sure this symbol hasn't explicitly been defined
3000 			 * as nodirect.
3001 			 */
3002 			if (((sip = SYMINFO(*dlmp)) == 0) ||
3003 			    /* LINTED */
3004 			    ((sip = (Syminfo *)((char *)sip +
3005 			    (dsymndx * SYMINENT(*dlmp)))) == 0) ||
3006 			    ((sip->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0))
3007 				return (sym);
3008 		}
3009 	}
3010 
3011 	sl.sl_flags |= LKUP_STANDARD;
3012 
3013 	/*
3014 	 * If this lookup originates from a standard relocation, then traverse
3015 	 * all link-map control lists, inspecting any object that is available
3016 	 * to this caller.  Otherwise, traverse the link-map control list
3017 	 * associated with the caller.
3018 	 */
3019 	if (sl.sl_flags & LKUP_STDRELOC) {
3020 		Aliste	off;
3021 		Lm_cntl	*lmc;
3022 
3023 		sym = NULL;
3024 
3025 		for (ALIST_TRAVERSE_BY_OFFSET(lml->lm_lists, off, lmc)) {
3026 			if (((sym = core_lookup_sym(lmc->lc_head, &sl, dlmp,
3027 			    binfo, off, in_nfavl)) != NULL) ||
3028 			    (*binfo & BINFO_REJSINGLE))
3029 				break;
3030 		}
3031 	} else
3032 		sym = core_lookup_sym(ilmp, &sl, dlmp, binfo, ALIST_OFF_DATA,
3033 		    in_nfavl);
3034 
3035 	/*
3036 	 * If a symbol binding was rejected, because a binding occurred to a
3037 	 * singleton without following the default symbol search, return so
3038 	 * that the search can be repreated.
3039 	 */
3040 	if (*binfo & BINFO_REJSINGLE)
3041 		return (sym);
3042 
3043 	/*
3044 	 * To allow transitioning into a world of lazy loading dependencies see
3045 	 * if this link map contains objects that have lazy dependencies still
3046 	 * outstanding.  If so, and we haven't been able to locate a non-weak
3047 	 * symbol reference, start bringing in any lazy dependencies to see if
3048 	 * the reference can be satisfied.  Use of dlsym(RTLD_PROBE) sets the
3049 	 * LKUP_NOFALLBACK flag, and this flag disables this fall back.
3050 	 */
3051 	if ((sym == NULL) && ((sl.sl_flags & LKUP_NOFALLBACK) == 0)) {
3052 		if ((lmp = ilmp) == 0)
3053 			lmp = LIST(clmp)->lm_head;
3054 
3055 		lml = LIST(lmp);
3056 		if ((sl.sl_flags & LKUP_WEAK) || (lml->lm_lazy == 0))
3057 			return ((Sym *)0);
3058 
3059 		DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
3060 
3061 		/*
3062 		 * If this request originated from a dlsym(RTLD_NEXT) then start
3063 		 * looking for dependencies from the caller, otherwise use the
3064 		 * initial link-map.
3065 		 */
3066 		if (sl.sl_flags & LKUP_NEXT)
3067 			sym = _lazy_find_sym(clmp, &sl, dlmp, binfo, in_nfavl);
3068 		else {
3069 			Aliste	idx;
3070 			Lm_cntl	*lmc;
3071 
3072 			for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
3073 				sl.sl_flags |= LKUP_NOFALLBACK;
3074 				if ((sym = _lazy_find_sym(lmc->lc_head, &sl,
3075 				    dlmp, binfo, in_nfavl)) != 0)
3076 					break;
3077 			}
3078 		}
3079 	}
3080 	return (sym);
3081 }
3082 
3083 /*
3084  * Symbol lookup routine.  Takes an ELF symbol name, and a list of link maps to
3085  * search.  If successful, return a pointer to the symbol table entry, a
3086  * pointer to the link map of the enclosing object, and information relating
3087  * to the type of binding.  Else return a null pointer.
3088  *
3089  * To improve elf performance, we first compute the elf hash value and pass
3090  * it to each find_sym() routine.  The elf function will use this value to
3091  * locate the symbol, the a.out function will simply ignore it.
3092  */
3093 Sym *
3094 lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
3095 {
3096 	Rt_map		*clmp = slp->sl_cmap;
3097 	Sym		*rsym = slp->sl_rsym, *sym = 0;
3098 	uchar_t		rtype = slp->sl_rtype;
3099 
3100 	if (slp->sl_hash == 0)
3101 		slp->sl_hash = elf_hash(slp->sl_name);
3102 	*binfo = 0;
3103 
3104 	/*
3105 	 * Establish any state that might be associated with a symbol reference.
3106 	 */
3107 	if (rsym) {
3108 		if ((slp->sl_flags & LKUP_STDRELOC) &&
3109 		    (ELF_ST_BIND(rsym->st_info) == STB_WEAK))
3110 			slp->sl_flags |= LKUP_WEAK;
3111 
3112 		if (ELF_ST_VISIBILITY(rsym->st_other) == STV_SINGLETON)
3113 			slp->sl_flags |= LKUP_SINGLETON;
3114 	}
3115 
3116 	/*
3117 	 * Establish any lookup state required for this type of relocation.
3118 	 */
3119 	if ((slp->sl_flags & LKUP_STDRELOC) && rtype) {
3120 		if (rtype == M_R_COPY)
3121 			slp->sl_flags |= LKUP_COPY;
3122 
3123 		if (rtype != M_R_JMP_SLOT)
3124 			slp->sl_flags |= LKUP_SPEC;
3125 	}
3126 
3127 	/*
3128 	 * Under ldd -w, any unresolved weak references are diagnosed.  Set the
3129 	 * symbol binding as global to trigger a relocation error if the symbol
3130 	 * can not be found.
3131 	 */
3132 	if (rsym) {
3133 		if (LIST(slp->sl_cmap)->lm_flags & LML_FLG_TRC_NOUNRESWEAK)
3134 			slp->sl_bind = STB_GLOBAL;
3135 		else if ((slp->sl_bind = ELF_ST_BIND(rsym->st_info)) ==
3136 		    STB_WEAK)
3137 			slp->sl_flags |= LKUP_WEAK;
3138 	}
3139 
3140 	/*
3141 	 * Carry out an initial symbol search.  This search takes into account
3142 	 * all the modes of the requested search.
3143 	 */
3144 	if (((sym = _lookup_sym(slp, dlmp, binfo, in_nfavl)) == NULL) &&
3145 	    (*binfo & BINFO_REJSINGLE)) {
3146 		Slookup	sl = *slp;
3147 
3148 		/*
3149 		 * If a binding has been rejected because of binding to a
3150 		 * singleton without going through a singleton search, then
3151 		 * reset the lookup data, and try again.
3152 		 */
3153 		sl.sl_imap = LIST(sl.sl_cmap)->lm_head;
3154 		sl.sl_flags &= ~(LKUP_FIRST | LKUP_SELF | LKUP_NEXT);
3155 		sl.sl_flags |= LKUP_SINGLETON;
3156 		sl.sl_rsymndx = 0;
3157 		*binfo &= ~BINFO_REJECTED;
3158 		sym = _lookup_sym(&sl, dlmp, binfo, in_nfavl);
3159 	}
3160 
3161 	/*
3162 	 * If the caller is restricted to a symbol search within its group,
3163 	 * determine if it is necessary to follow a binding from outside of
3164 	 * the group.
3165 	 */
3166 	if ((MODE(clmp) & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP) {
3167 		Sym	*isym;
3168 
3169 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
3170 		    in_nfavl)) != 0)
3171 			return (isym);
3172 	}
3173 	return (sym);
3174 }
3175 
3176 /*
3177  * Associate a binding descriptor with a caller and its dependency, or update
3178  * an existing descriptor.
3179  */
3180 int
3181 bind_one(Rt_map *clmp, Rt_map *dlmp, uint_t flags)
3182 {
3183 	Bnd_desc	*bdp;
3184 	Aliste		idx;
3185 	int		found = ALE_CREATE;
3186 
3187 	/*
3188 	 * Determine whether a binding descriptor already exists between the
3189 	 * two objects.
3190 	 */
3191 	for (APLIST_TRAVERSE(DEPENDS(clmp), idx, bdp)) {
3192 		if (bdp->b_depend == dlmp) {
3193 			found = ALE_EXISTS;
3194 			break;
3195 		}
3196 	}
3197 
3198 	if (found == ALE_CREATE) {
3199 		/*
3200 		 * Create a new binding descriptor.
3201 		 */
3202 		if ((bdp = malloc(sizeof (Bnd_desc))) == 0)
3203 			return (0);
3204 
3205 		bdp->b_caller = clmp;
3206 		bdp->b_depend = dlmp;
3207 		bdp->b_flags = 0;
3208 
3209 		/*
3210 		 * Append the binding descriptor to the caller and the
3211 		 * dependency.
3212 		 */
3213 		if (aplist_append(&DEPENDS(clmp), bdp, AL_CNT_DEPENDS) == 0)
3214 			return (0);
3215 
3216 		if (aplist_append(&CALLERS(dlmp), bdp, AL_CNT_CALLERS) == 0)
3217 			return (0);
3218 	}
3219 
3220 	if ((found == ALE_CREATE) || ((bdp->b_flags & flags) != flags)) {
3221 		bdp->b_flags |= flags;
3222 
3223 		if (flags & BND_REFER)
3224 			FLAGS1(dlmp) |= FL1_RT_USED;
3225 
3226 		DBG_CALL(Dbg_file_bind_entry(LIST(clmp), bdp));
3227 	}
3228 	return (found);
3229 }
3230 
3231 /*
3232  * Cleanup after relocation processing.
3233  */
3234 int
3235 relocate_finish(Rt_map *lmp, APlist *bound, int textrel, int ret)
3236 {
3237 	DBG_CALL(Dbg_reloc_run(lmp, 0, ret, DBG_REL_FINISH));
3238 
3239 	/*
3240 	 * Establish bindings to all objects that have been bound to.
3241 	 */
3242 	if (bound) {
3243 		Aliste	idx;
3244 		Rt_map	*_lmp;
3245 		Word	used;
3246 
3247 		/*
3248 		 * Only create bindings if the callers relocation was
3249 		 * successful (ret != 0), otherwise the object will eventually
3250 		 * be torn down.  Create these bindings if running under ldd(1)
3251 		 * with the -U/-u options regardless of relocation errors, as
3252 		 * the unused processing needs to traverse these bindings to
3253 		 * diagnose unused objects.
3254 		 */
3255 		used = LIST(lmp)->lm_flags &
3256 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED);
3257 
3258 		if (ret || used) {
3259 			for (APLIST_TRAVERSE(bound, idx, _lmp)) {
3260 				if (bind_one(lmp, _lmp, BND_REFER) || used)
3261 					continue;
3262 
3263 				ret = 0;
3264 				break;
3265 			}
3266 		}
3267 		free(bound);
3268 	}
3269 
3270 	/*
3271 	 * If we write enabled the text segment to perform these relocations
3272 	 * re-protect by disabling writes.
3273 	 */
3274 	if (textrel)
3275 		(void) LM_SET_PROT(lmp)(lmp, 0);
3276 
3277 	return (ret);
3278 }
3279