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