xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/analyze.c (revision c211fc479225fa54805cf480633bf6689ca9a2db)
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 		Listnode	*lnp;
1026 
1027 		for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
1028 			Rt_map	*nlmp = lml->lm_head;
1029 
1030 			if (nlmp && ((FLAGS(nlmp) &
1031 			    (FLG_RT_AUDIT | FLG_RT_DELETE)) == FLG_RT_AUDIT) &&
1032 			    (STDEV(nlmp) == status->st_dev) &&
1033 			    (STINO(nlmp) == status->st_ino))
1034 				return (nlmp);
1035 		}
1036 		return (NULL);
1037 	}
1038 
1039 	/*
1040 	 * If the file has been found determine from the new files status
1041 	 * information if this file is actually linked to one we already have
1042 	 * mapped.  This catches symlink names not caught by is_so_loaded().
1043 	 */
1044 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
1045 		Rt_map	*nlmp;
1046 
1047 		for (nlmp = lmc->lc_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
1048 			if ((FLAGS(nlmp) & FLG_RT_DELETE) ||
1049 			    (FLAGS1(nlmp) & FL1_RT_LDDSTUB))
1050 				continue;
1051 
1052 			if ((STDEV(nlmp) != status->st_dev) ||
1053 			    (STINO(nlmp) != status->st_ino))
1054 				continue;
1055 
1056 			if (lml->lm_flags & LML_FLG_TRC_VERBOSE) {
1057 				/* BEGIN CSTYLED */
1058 				if (*name == '/')
1059 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
1060 					name, MSG_ORIG(MSG_STR_EMPTY),
1061 					MSG_ORIG(MSG_STR_EMPTY));
1062 				else
1063 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
1064 					name, NAME(nlmp),
1065 					MSG_ORIG(MSG_STR_EMPTY),
1066 					MSG_ORIG(MSG_STR_EMPTY));
1067 				/* END CSTYLED */
1068 			}
1069 			return (nlmp);
1070 		}
1071 	}
1072 	return (NULL);
1073 }
1074 
1075 /*
1076  * Generate any error messages indicating a file could not be found.  When
1077  * preloading or auditing a secure application, it can be a little more helpful
1078  * to indicate that a search of secure directories has failed, so adjust the
1079  * messages accordingly.
1080  */
1081 void
1082 file_notfound(Lm_list *lml, const char *name, Rt_map *clmp, uint_t flags,
1083     Rej_desc *rej)
1084 {
1085 	int	secure = 0;
1086 
1087 	if ((rtld_flags & RT_FL_SECURE) &&
1088 	    (flags & (FLG_RT_PRELOAD | FLG_RT_AUDIT)))
1089 		secure++;
1090 
1091 	if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
1092 		/*
1093 		 * Under ldd(1), auxiliary filtees that can't be loaded are
1094 		 * ignored, unless verbose errors are requested.
1095 		 */
1096 		if ((rtld_flags & RT_FL_SILENCERR) &&
1097 		    ((lml->lm_flags & LML_FLG_TRC_VERBOSE) == 0))
1098 			return;
1099 
1100 		if (secure)
1101 			trace_so(clmp, rej, name, 0, 0,
1102 			    MSG_INTL(MSG_LDD_SEC_NFOUND));
1103 		else
1104 			trace_so(clmp, rej, name, 0, 0,
1105 			    MSG_INTL(MSG_LDD_FIL_NFOUND));
1106 		return;
1107 	}
1108 
1109 	if (rej->rej_type) {
1110 		Conv_reject_desc_buf_t rej_buf;
1111 
1112 		eprintf(lml, ERR_FATAL, MSG_INTL(err_reject[rej->rej_type]),
1113 		    rej->rej_name ? rej->rej_name : MSG_INTL(MSG_STR_UNKNOWN),
1114 		    conv_reject_desc(rej, &rej_buf, M_MACH));
1115 		return;
1116 	}
1117 
1118 	if (secure)
1119 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SEC_OPEN), name);
1120 	else
1121 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), name,
1122 		    strerror(ENOENT));
1123 }
1124 
1125 static int
1126 file_open(int err, Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp,
1127     Rej_desc *rej, int *in_nfavl)
1128 {
1129 	rtld_stat_t	status;
1130 	Rt_map		*nlmp;
1131 	avl_index_t	nfavlwhere = 0;
1132 	const char	*oname = fdp->fd_oname, *nname = fdp->fd_nname;
1133 	uint_t		hash = sgs_str_hash(nname);
1134 
1135 
1136 	if ((nname = stravl_insert(fdp->fd_nname, hash, 0, 0)) == NULL)
1137 		return (0);
1138 	fdp->fd_nname = nname;
1139 
1140 	if ((err == 0) && (fdp->fd_flags & FLG_FD_ALTER))
1141 		DBG_CALL(Dbg_file_config_obj(lml, oname, 0, nname));
1142 
1143 	/*
1144 	 * If we're dealing with a full pathname, determine whether this
1145 	 * pathname is already known.  Other pathnames fall through to the
1146 	 * dev/inode check, as even though the pathname may look the same as
1147 	 * one previously used, the process may have changed directory.
1148 	 */
1149 	if ((err == 0) && (nname[0] == '/')) {
1150 		if ((nlmp = fpavl_recorded(lml, nname, hash,
1151 		    &(fdp->fd_avlwhere))) != NULL) {
1152 			fdp->fd_lmp = nlmp;
1153 			return (1);
1154 		}
1155 		if (nfavl_recorded(nname, hash, &nfavlwhere)) {
1156 			/*
1157 			 * For dlopen() and dlsym() fall backs, indicate that
1158 			 * a registered not-found path has indicated that this
1159 			 * object does not exist.  If this path has been
1160 			 * constructed as part of expanding a HWCAP directory,
1161 			 * this is a silent failure, where no rejection message
1162 			 * is created.
1163 			 */
1164 			if (in_nfavl)
1165 				(*in_nfavl)++;
1166 			return (0);
1167 		}
1168 	}
1169 
1170 	if ((err == 0) && ((rtld_stat(nname, &status)) != -1)) {
1171 		char	path[PATH_MAX];
1172 		int	fd, size, added;
1173 
1174 		/*
1175 		 * If this path has been constructed as part of expanding a
1176 		 * HWCAP directory, ignore any subdirectories.  As this is a
1177 		 * silent failure, no rejection message is created.  For any
1178 		 * other reference that expands to a directory, fall through
1179 		 * to construct a meaningful rejection message.
1180 		 */
1181 		if ((flags & FLG_RT_HWCAP) &&
1182 		    ((status.st_mode & S_IFMT) == S_IFDIR))
1183 			return (0);
1184 
1185 		/*
1186 		 * If this is a directory (which can't be mmap()'ed) generate a
1187 		 * precise error message.
1188 		 */
1189 		if ((status.st_mode & S_IFMT) == S_IFDIR) {
1190 			rej->rej_name = nname;
1191 			if (fdp->fd_flags & FLG_FD_ALTER)
1192 				rej->rej_flags = FLG_REJ_ALTER;
1193 			rej->rej_type = SGS_REJ_STR;
1194 			rej->rej_str = strerror(EISDIR);
1195 			DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
1196 			return (0);
1197 		}
1198 
1199 		/*
1200 		 * Resolve the filename and determine whether the resolved name
1201 		 * is already known.  Typically, the previous fpavl_loaded()
1202 		 * will have caught this, as both NAME() and PATHNAME() for a
1203 		 * link-map are recorded in the FullPathNode AVL tree.  However,
1204 		 * instances exist where a file can be replaced (loop-back
1205 		 * mounts, bfu, etc.), and reference is made to the original
1206 		 * file through a symbolic link.  By checking the pathname here,
1207 		 * we don't fall through to the dev/inode check and conclude
1208 		 * that a new file should be loaded.
1209 		 */
1210 		if ((nname[0] == '/') &&
1211 		    ((size = resolvepath(nname, path, (PATH_MAX - 1))) > 0)) {
1212 			path[size] = '\0';
1213 
1214 			fdp->fd_flags |= FLG_FD_RESOLVED;
1215 
1216 			if (strcmp(nname, path)) {
1217 				if ((nlmp =
1218 				    fpavl_recorded(lml, path, 0, 0)) != NULL) {
1219 					added = 0;
1220 
1221 					if (append_alias(nlmp, nname,
1222 					    &added) == 0)
1223 						return (0);
1224 					/* BEGIN CSTYLED */
1225 					if (added)
1226 					    DBG_CALL(Dbg_file_skip(LIST(clmp),
1227 						NAME(nlmp), nname));
1228 					/* END CSTYLED */
1229 					fdp->fd_lmp = nlmp;
1230 					return (1);
1231 				}
1232 
1233 				/*
1234 				 * If this pathname hasn't been loaded, save
1235 				 * the resolved pathname so that it doesn't
1236 				 * have to be recomputed as part of fullpath()
1237 				 * processing.
1238 				 */
1239 				if ((fdp->fd_pname = stravl_insert(path, 0,
1240 				    (size + 1), 0)) == NULL)
1241 					return (0);
1242 			}
1243 		}
1244 
1245 		if (nlmp = is_devinode_loaded(&status, lml, nname, flags)) {
1246 			if (flags & FLG_RT_AUDIT) {
1247 				/*
1248 				 * If we've been requested to load an auditor,
1249 				 * and an auditor of the same name already
1250 				 * exists, then the original auditor is used.
1251 				 */
1252 				DBG_CALL(Dbg_audit_skip(LIST(clmp),
1253 				    NAME(nlmp), LIST(nlmp)->lm_lmidstr));
1254 			} else {
1255 				/*
1256 				 * Otherwise, if an alternatively named file
1257 				 * has been found for the same dev/inode, add
1258 				 * a new name alias.  Insert any alias full path
1259 				 * name in the FullPathNode AVL tree.
1260 				 */
1261 				added = 0;
1262 
1263 				if (append_alias(nlmp, nname, &added) == 0)
1264 					return (0);
1265 				if (added) {
1266 					if ((nname[0] == '/') &&
1267 					    (fpavl_insert(lml, nlmp,
1268 					    nname, 0) == 0))
1269 						return (0);
1270 					DBG_CALL(Dbg_file_skip(LIST(clmp),
1271 					    NAME(nlmp), nname));
1272 				}
1273 			}
1274 
1275 			/*
1276 			 * Record in the file descriptor the existing object
1277 			 * that satisfies this open request.
1278 			 */
1279 			fdp->fd_lmp = nlmp;
1280 			return (1);
1281 		}
1282 
1283 		if ((fd = open(nname, O_RDONLY, 0)) == -1) {
1284 			/*
1285 			 * As the file must exist for the previous stat() to
1286 			 * have succeeded, record the error condition.
1287 			 */
1288 			rej->rej_type = SGS_REJ_STR;
1289 			rej->rej_str = strerror(errno);
1290 		} else {
1291 			/*
1292 			 * Map the object.  A successful return indicates that
1293 			 * the object is appropriate for ld.so.1 processing.
1294 			 */
1295 			fdp->fd_ftp = map_obj(lml, fdp, status.st_size, nname,
1296 			    fd, rej);
1297 			(void) close(fd);
1298 
1299 			if (fdp->fd_ftp != NULL) {
1300 				fdp->fd_dev = status.st_dev;
1301 				fdp->fd_ino = status.st_ino;
1302 
1303 				/*
1304 				 * Trace that this open has succeeded.
1305 				 */
1306 				if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
1307 					trace_so(clmp, 0, oname, nname,
1308 					    (fdp->fd_flags & FLG_FD_ALTER), 0);
1309 				}
1310 				return (1);
1311 			}
1312 		}
1313 
1314 	} else if (errno != ENOENT) {
1315 		/*
1316 		 * If the open() failed for anything other than the file not
1317 		 * existing, record the error condition.
1318 		 */
1319 		rej->rej_type = SGS_REJ_STR;
1320 		rej->rej_str = strerror(errno);
1321 	}
1322 
1323 	/*
1324 	 * Regardless of error, duplicate and record any full path names that
1325 	 * can't be used on the "not-found" AVL tree.
1326 	 */
1327 	if (nname[0] == '/')
1328 		nfavl_insert(nname, nfavlwhere);
1329 
1330 	/*
1331 	 * Indicate any rejection.
1332 	 */
1333 	if (rej->rej_type) {
1334 		rej->rej_name = nname;
1335 		if (fdp->fd_flags & FLG_FD_ALTER)
1336 			rej->rej_flags = FLG_REJ_ALTER;
1337 		DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
1338 	}
1339 	return (0);
1340 }
1341 
1342 /*
1343  * Find a full pathname (it contains a "/").
1344  */
1345 int
1346 find_path(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
1347     int *in_nfavl)
1348 {
1349 	const char	*oname = fdp->fd_oname;
1350 	int		err = 0;
1351 
1352 	/*
1353 	 * If directory configuration exists determine if this path is known.
1354 	 */
1355 	if (rtld_flags & RT_FL_DIRCFG) {
1356 		Rtc_obj		*obj;
1357 		const char	*aname;
1358 
1359 		if ((obj = elf_config_ent(oname, (Word)elf_hash(oname),
1360 		    0, &aname)) != 0) {
1361 			/*
1362 			 * If the configuration file states that this path is a
1363 			 * directory, or the path is explicitly defined as
1364 			 * non-existent (ie. a unused platform specific
1365 			 * library), then go no further.
1366 			 */
1367 			if (obj->co_flags & RTC_OBJ_DIRENT) {
1368 				err = EISDIR;
1369 			} else if ((obj->co_flags &
1370 			    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) ==
1371 			    RTC_OBJ_NOEXIST) {
1372 				err = ENOENT;
1373 			} else if ((obj->co_flags & RTC_OBJ_ALTER) &&
1374 			    (rtld_flags & RT_FL_OBJALT) && (lml == &lml_main)) {
1375 				int	ret;
1376 
1377 				fdp->fd_flags |= FLG_FD_ALTER;
1378 				fdp->fd_nname = aname;
1379 
1380 				/*
1381 				 * Attempt to open the alternative path.  If
1382 				 * this fails, and the alternative is flagged
1383 				 * as optional, fall through to open the
1384 				 * original path.
1385 				 */
1386 				DBG_CALL(Dbg_libs_found(lml, aname,
1387 				    FLG_FD_ALTER));
1388 				ret = file_open(0, lml, clmp, flags, fdp,
1389 				    rej, in_nfavl);
1390 				if (ret || ((obj->co_flags &
1391 				    RTC_OBJ_OPTINAL) == 0))
1392 					return (ret);
1393 
1394 				fdp->fd_flags &= ~FLG_FD_ALTER;
1395 			}
1396 		}
1397 	}
1398 	DBG_CALL(Dbg_libs_found(lml, oname, 0));
1399 	fdp->fd_nname = oname;
1400 	return (file_open(err, lml, clmp, flags, fdp, rej, in_nfavl));
1401 }
1402 
1403 /*
1404  * Find a simple filename (it doesn't contain a "/").
1405  */
1406 static int
1407 _find_file(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
1408     Pdesc *pdp, int aflag, int *in_nfavl)
1409 {
1410 	const char	*nname = fdp->fd_nname;
1411 
1412 	DBG_CALL(Dbg_libs_found(lml, nname, aflag));
1413 	if ((lml->lm_flags & LML_FLG_TRC_SEARCH) &&
1414 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
1415 		(void) printf(MSG_INTL(MSG_LDD_PTH_TRYING), nname, aflag ?
1416 		    MSG_INTL(MSG_LDD_FIL_ALTER) : MSG_ORIG(MSG_STR_EMPTY));
1417 	}
1418 
1419 	/*
1420 	 * If we're being audited tell the audit library of the file we're about
1421 	 * to go search for.  The audit library may offer an alternative
1422 	 * dependency, or indicate that this dependency should be ignored.
1423 	 */
1424 	if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
1425 		char	*aname;
1426 
1427 		if ((aname = audit_objsearch(clmp, nname,
1428 		    (pdp->pd_flags & LA_SER_MASK))) == NULL) {
1429 			DBG_CALL(Dbg_audit_terminate(lml, nname));
1430 			return (0);
1431 		}
1432 
1433 		if (aname != nname) {
1434 			fdp->fd_flags &= ~FLG_FD_SLASH;
1435 			fdp->fd_nname = aname;
1436 		}
1437 	}
1438 	return (file_open(0, lml, clmp, flags, fdp, rej, in_nfavl));
1439 }
1440 
1441 static int
1442 find_file(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
1443     Pdesc *pdp, Word *strhash, int *in_nfavl)
1444 {
1445 	static Rtc_obj	Obj = { 0 };
1446 	Rtc_obj		*dobj;
1447 	const char	*oname = fdp->fd_oname;
1448 	size_t		olen = strlen(oname);
1449 
1450 	if (pdp->pd_pname == NULL)
1451 		return (0);
1452 	if (pdp->pd_info) {
1453 		dobj = (Rtc_obj *)pdp->pd_info;
1454 		if ((dobj->co_flags &
1455 		    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
1456 			return (0);
1457 	} else
1458 		dobj = NULL;
1459 
1460 	/*
1461 	 * If configuration information exists see if this directory/file
1462 	 * combination exists.
1463 	 */
1464 	if ((rtld_flags & RT_FL_DIRCFG) &&
1465 	    ((dobj == NULL) || (dobj->co_id != 0))) {
1466 		Rtc_obj		*fobj;
1467 		const char	*aname = NULL;
1468 
1469 		/*
1470 		 * If this object descriptor has not yet been searched for in
1471 		 * the configuration file go find it.
1472 		 */
1473 		if (dobj == NULL) {
1474 			dobj = elf_config_ent(pdp->pd_pname,
1475 			    (Word)elf_hash(pdp->pd_pname), 0, 0);
1476 			if (dobj == NULL)
1477 				dobj = &Obj;
1478 			pdp->pd_info = (void *)dobj;
1479 
1480 			if ((dobj->co_flags & (RTC_OBJ_NOEXIST |
1481 			    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
1482 				return (0);
1483 		}
1484 
1485 		/*
1486 		 * If we found a directory search for the file.
1487 		 */
1488 		if (dobj->co_id != 0) {
1489 			if (*strhash == NULL)
1490 				*strhash = (Word)elf_hash(oname);
1491 			fobj = elf_config_ent(oname, *strhash,
1492 			    dobj->co_id, &aname);
1493 
1494 			/*
1495 			 * If this object specifically does not exist, or the
1496 			 * object can't be found in a know-all-entries
1497 			 * directory, continue looking.  If the object does
1498 			 * exist determine if an alternative object exists.
1499 			 */
1500 			if (fobj == NULL) {
1501 				if (dobj->co_flags & RTC_OBJ_ALLENTS)
1502 					return (0);
1503 			} else {
1504 				if ((fobj->co_flags & (RTC_OBJ_NOEXIST |
1505 				    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
1506 					return (0);
1507 
1508 				if ((fobj->co_flags & RTC_OBJ_ALTER) &&
1509 				    (rtld_flags & RT_FL_OBJALT) &&
1510 				    (lml == &lml_main)) {
1511 					int	ret;
1512 
1513 					fdp->fd_flags |= FLG_FD_ALTER;
1514 					fdp->fd_nname = aname;
1515 
1516 					/*
1517 					 * Attempt to open the alternative path.
1518 					 * If this fails, and the alternative is
1519 					 * flagged as optional, fall through to
1520 					 * open the original path.
1521 					 */
1522 					ret = _find_file(lml, clmp, flags, fdp,
1523 					    rej, pdp, 1, in_nfavl);
1524 					if (ret || ((fobj->co_flags &
1525 					    RTC_OBJ_OPTINAL) == 0))
1526 						return (ret);
1527 
1528 					fdp->fd_flags &= ~FLG_FD_ALTER;
1529 				}
1530 			}
1531 		}
1532 	}
1533 
1534 	/*
1535 	 * Protect ourselves from building an invalid pathname.
1536 	 */
1537 	if ((olen + pdp->pd_plen + 1) >= PATH_MAX) {
1538 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), oname,
1539 		    strerror(ENAMETOOLONG));
1540 			return (0);
1541 	}
1542 	if ((fdp->fd_nname = (LM_GET_SO(clmp)(pdp->pd_pname, oname,
1543 	    pdp->pd_plen, olen))) == NULL)
1544 		return (0);
1545 
1546 	return (_find_file(lml, clmp, flags, fdp, rej, pdp, 0, in_nfavl));
1547 }
1548 
1549 static Fct	*Vector[] = {
1550 	&elf_fct,
1551 #ifdef	A_OUT
1552 	&aout_fct,
1553 #endif
1554 	0
1555 };
1556 
1557 /*
1558  * Remap the first page of a file to provide a better diagnostic as to why
1559  * an mmapobj(2) operation on this file failed.  Sadly, mmapobj(), and all
1560  * system calls for that matter, only pass back a generic failure in errno.
1561  * Hopefully one day this will be improved, but in the mean time we repeat
1562  * the kernels ELF verification to try and provide more detailed information.
1563  */
1564 static int
1565 map_fail(Fdesc *fdp, size_t fsize, const char *name, int fd, Rej_desc *rej)
1566 {
1567 	caddr_t	addr;
1568 	int	vnum;
1569 	size_t	size;
1570 
1571 	/*
1572 	 * Use the original file size to determine what to map, and catch the
1573 	 * obvious error of a zero sized file.
1574 	 */
1575 	if (fsize == 0) {
1576 		rej->rej_type = SGS_REJ_UNKFILE;
1577 		return (1);
1578 	} else if (fsize < syspagsz)
1579 		size = fsize;
1580 	else
1581 		size = syspagsz;
1582 
1583 	if ((addr = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
1584 		return (0);
1585 
1586 	rej->rej_type = 0;
1587 
1588 	/*
1589 	 * Validate the file against each supported file type.  Should a
1590 	 * characteristic of the file be found invalid for this platform, a
1591 	 * rejection message will have been recorded.
1592 	 */
1593 	for (vnum = 0; Vector[vnum]; vnum++) {
1594 		if (((Vector[vnum]->fct_verify_file)(addr, size,
1595 		    fdp, name, rej) == 0) && rej->rej_type)
1596 			break;
1597 	}
1598 
1599 	/*
1600 	 * If no rejection message has been recorded, then this is simply an
1601 	 * unknown file type.
1602 	 */
1603 	if (rej->rej_type == 0)
1604 		rej->rej_type = SGS_REJ_UNKFILE;
1605 
1606 	(void) munmap(addr, size);
1607 	return (1);
1608 }
1609 
1610 /*
1611  * Unmap a file.
1612  */
1613 void
1614 unmap_obj(mmapobj_result_t *mpp, uint_t mapnum)
1615 {
1616 	uint_t	num;
1617 
1618 	for (num = 0; num < mapnum; num++) {
1619 		/* LINTED */
1620 		(void) munmap((void *)(uintptr_t)mpp[num].mr_addr,
1621 		    mpp[num].mr_msize);
1622 	}
1623 }
1624 
1625 /*
1626  * Map a file.
1627  */
1628 Fct *
1629 map_obj(Lm_list *lml, Fdesc *fdp, size_t fsize, const char *name, int fd,
1630     Rej_desc *rej)
1631 {
1632 	static mmapobj_result_t	*smpp = NULL;
1633 	static uint_t		smapnum;
1634 	mmapobj_result_t	*mpp;
1635 	uint_t			mnum, mapnum, mflags;
1636 	void			*padding;
1637 
1638 	/*
1639 	 * Allocate an initial mapping array.  The initial size should be large
1640 	 * enough to handle the normal ELF objects we come across.
1641 	 */
1642 	if (smpp == NULL) {
1643 		smpp = malloc(sizeof (mmapobj_result_t) * MMAPFD_NUM);
1644 		if (smpp == NULL)
1645 			return (NULL);
1646 		smapnum = MMAPFD_NUM;
1647 	}
1648 
1649 	/*
1650 	 * If object padding is required, set the necessary flags.
1651 	 */
1652 	if (r_debug.rtd_objpad) {
1653 		mflags = MMOBJ_INTERPRET | MMOBJ_PADDING;
1654 		padding = &r_debug.rtd_objpad;
1655 	} else {
1656 		mflags = MMOBJ_INTERPRET;
1657 		padding = NULL;
1658 	}
1659 
1660 	/*
1661 	 * Map the file.  If the number of mappings required by this file
1662 	 * exceeds the present mapping structure, an error indicating the
1663 	 * return data is too big is returned.  Bail on any other error.
1664 	 */
1665 	mapnum = smapnum;
1666 	if (mmapobj(fd, mflags, smpp, &mapnum, padding) == -1) {
1667 		if (errno != E2BIG) {
1668 			int	err = errno;
1669 
1670 			/*
1671 			 * An unsupported error indicates that there's something
1672 			 * incompatible with this ELF file, and the process that
1673 			 * is already running.  Map the first page of the file
1674 			 * and see if we can generate a better error message.
1675 			 */
1676 			if ((errno == ENOTSUP) && map_fail(fdp, fsize, name,
1677 			    fd, rej))
1678 				return (NULL);
1679 
1680 			rej->rej_type = SGS_REJ_STR;
1681 			rej->rej_str = strerror(err);
1682 			return (NULL);
1683 		}
1684 
1685 		/*
1686 		 * The mapping requirement exceeds the present mapping
1687 		 * structure, however the number of mapping required is
1688 		 * available in the mapping number.
1689 		 */
1690 		free((void *)smpp);
1691 		if ((smpp = malloc(sizeof (mmapobj_result_t) * mapnum)) == NULL)
1692 			return (NULL);
1693 		smapnum = mapnum;
1694 
1695 		/*
1696 		 * With the appropriate mapping structure, try the mapping
1697 		 * request again.
1698 		 */
1699 		if (mmapobj(fd, mflags, smpp, &mapnum, padding) == -1) {
1700 			rej->rej_type = SGS_REJ_STR;
1701 			rej->rej_str = strerror(errno);
1702 			return (NULL);
1703 		}
1704 	}
1705 	ASSERT(mapnum != 0);
1706 
1707 	/*
1708 	 * Traverse the mappings in search of a file type ld.so.1 can process.
1709 	 * If the file type is verified as one ld.so.1 can process, retain the
1710 	 * mapping information, and the number of mappings this object uses,
1711 	 * and clear the static mapping pointer for the next map_obj() use of
1712 	 * mmapobj().
1713 	 */
1714 	DBG_CALL(Dbg_file_mmapobj(lml, name, smpp, mapnum));
1715 
1716 	for (mnum = 0, mpp = smpp; mnum < mapnum; mnum++, mpp++) {
1717 		uint_t	flags = (mpp->mr_flags & MR_TYPE_MASK);
1718 		Fct	*fptr = NULL;
1719 
1720 		if (flags == MR_HDR_ELF) {
1721 			fptr = elf_verify((mpp->mr_addr + mpp->mr_offset),
1722 			    mpp->mr_fsize, fdp, name, rej);
1723 		}
1724 #ifdef	A_OUT
1725 		if (flags == MR_HDR_AOUT) {
1726 			fptr = aout_verify((mpp->mr_addr + mpp->mr_offset),
1727 			    mpp->mr_fsize, fdp, name, rej);
1728 		}
1729 #endif
1730 		if (fptr) {
1731 			fdp->fd_mapn = mapnum;
1732 			fdp->fd_mapp = smpp;
1733 
1734 			smpp = NULL;
1735 
1736 			return (fptr);
1737 		}
1738 	}
1739 
1740 	/*
1741 	 * If the mapped file is inappropriate, indicate that the file type is
1742 	 * unknown, and free the mapping.
1743 	 */
1744 	if (rej->rej_type == 0)
1745 		rej->rej_type = SGS_REJ_UNKFILE;
1746 	unmap_obj(smpp, mapnum);
1747 
1748 	return (NULL);
1749 }
1750 
1751 /*
1752  * A unique file has been opened.  Create a link-map to represent it, and
1753  * process the various names by which it can be referenced.
1754  */
1755 Rt_map *
1756 load_file(Lm_list *lml, Aliste lmco, Fdesc *fdp, int *in_nfavl)
1757 {
1758 	mmapobj_result_t	*fpmpp = NULL, *fmpp = NULL, *lpmpp, *lmpp;
1759 	mmapobj_result_t	*hmpp, *mpp, *ompp = fdp->fd_mapp;
1760 	uint_t			mnum, omapnum = fdp->fd_mapn;
1761 	const char		*nname = fdp->fd_nname;
1762 	Rt_map			*nlmp;
1763 	Ehdr			*ehdr = NULL;
1764 
1765 	/*
1766 	 * Traverse the mappings for the input file to capture generic mapping
1767 	 * information, and create a link-map to represent the file.
1768 	 */
1769 	for (mnum = 0, mpp = ompp; mnum < omapnum; mnum++, mpp++) {
1770 		uint_t	flags = (mpp->mr_flags & MR_TYPE_MASK);
1771 
1772 		/*
1773 		 * Keep track of the first and last mappings that may include
1774 		 * padding.
1775 		 */
1776 		if (fpmpp == NULL)
1777 			fpmpp = mpp;
1778 		lpmpp = mpp;
1779 
1780 		/*
1781 		 * Keep track of the first and last mappings that do not include
1782 		 * padding.
1783 		 */
1784 		if (flags != MR_PADDING) {
1785 			if (fmpp == NULL)
1786 				fmpp = mpp;
1787 			lmpp = mpp;
1788 		}
1789 		if (flags == MR_HDR_ELF) {
1790 			/* LINTED */
1791 			ehdr = (Ehdr *)(mpp->mr_addr + mpp->mr_offset);
1792 			hmpp = mpp;
1793 		} else if (flags == MR_HDR_AOUT)
1794 			hmpp = mpp;
1795 	}
1796 
1797 	/*
1798 	 * The only ELF files we can handle are ET_EXEC, ET_DYN, and ET_REL.
1799 	 *
1800 	 * ET_REL must be processed by ld(1) to create an in-memory ET_DYN.
1801 	 * The initial processing carried out by elf_obj_file() creates a
1802 	 * temporary link-map, that acts as a place holder, until the objects
1803 	 * processing is finished with elf_obj_fini().
1804 	 */
1805 	if (ehdr && (ehdr->e_type == ET_REL)) {
1806 		if ((nlmp = elf_obj_file(lml, lmco, nname, hmpp, ompp,
1807 		    omapnum)) == NULL)
1808 			return (nlmp);
1809 	} else {
1810 		Addr	addr;
1811 		size_t	msize;
1812 
1813 		/*
1814 		 * The size of the total reservation, and the padding range,
1815 		 * are a historic artifact required by debuggers.  Although
1816 		 * these values express the range of the associated mappings,
1817 		 * there can be holes between segments (in which small objects
1818 		 * could be mapped).  Anyone who needs to verify offsets
1819 		 * against segments should analyze all the object mappings,
1820 		 * rather than relying on these address ranges.
1821 		 */
1822 		addr = (Addr)(hmpp->mr_addr + hmpp->mr_offset);
1823 		msize = lmpp->mr_addr + lmpp->mr_msize - fmpp->mr_addr;
1824 
1825 		if ((nlmp = ((fdp->fd_ftp)->fct_new_lmp)(lml, lmco, fdp, addr,
1826 		    msize, NULL, in_nfavl)) == NULL)
1827 			return (NULL);
1828 
1829 		/*
1830 		 * Save generic mapping information.
1831 		 */
1832 		MMAPS(nlmp) = ompp;
1833 		MMAPCNT(nlmp) = omapnum;
1834 		PADSTART(nlmp) = (ulong_t)fpmpp->mr_addr;
1835 		PADIMLEN(nlmp) = lpmpp->mr_addr + lpmpp->mr_msize -
1836 		    fpmpp->mr_addr;
1837 	}
1838 
1839 	/*
1840 	 * Save the dev/inode information for later comparisons, and identify
1841 	 * this as a new object.
1842 	 */
1843 	STDEV(nlmp) = fdp->fd_dev;
1844 	STINO(nlmp) = fdp->fd_ino;
1845 	FLAGS(nlmp) |= FLG_RT_NEWLOAD;
1846 
1847 	/*
1848 	 * If this is ELF relocatable object, we're done for now.
1849 	 */
1850 	if (ehdr && (ehdr->e_type == ET_REL))
1851 		return (nlmp);
1852 
1853 	/*
1854 	 * Insert the names of this link-map into the FullPathNode AVL tree.
1855 	 * Save both the NAME() and PATHNAME() if the names differ.
1856 	 */
1857 	(void) fullpath(nlmp, fdp);
1858 
1859 	if ((NAME(nlmp)[0] == '/') && (fpavl_insert(lml, nlmp, NAME(nlmp),
1860 	    fdp->fd_avlwhere) == 0)) {
1861 		remove_so(lml, nlmp);
1862 		return (NULL);
1863 	}
1864 	if (((NAME(nlmp)[0] != '/') || (NAME(nlmp) != PATHNAME(nlmp))) &&
1865 	    (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)) {
1866 		remove_so(lml, nlmp);
1867 		return (NULL);
1868 	}
1869 
1870 	/*
1871 	 * If this is a secure application, record any full path name directory
1872 	 * in which this dependency has been found.  This directory can be
1873 	 * deemed safe (as we've already found a dependency here).  This
1874 	 * recording provides a fall-back should another objects $ORIGIN
1875 	 * definition expands to this directory, an expansion that would
1876 	 * ordinarily be deemed insecure.
1877 	 */
1878 	if (rtld_flags & RT_FL_SECURE) {
1879 		if (NAME(nlmp)[0] == '/')
1880 			spavl_insert(NAME(nlmp));
1881 		if ((NAME(nlmp) != PATHNAME(nlmp)) &&
1882 		    (PATHNAME(nlmp)[0] == '/'))
1883 			spavl_insert(PATHNAME(nlmp));
1884 	}
1885 
1886 	/*
1887 	 * If we're processing an alternative object reset the original name
1888 	 * for possible $ORIGIN processing.
1889 	 */
1890 	if (fdp->fd_flags & FLG_FD_ALTER) {
1891 		const char	*odir, *ndir;
1892 		size_t		olen;
1893 
1894 		FLAGS(nlmp) |= FLG_RT_ALTER;
1895 
1896 		/*
1897 		 * If we were given a pathname containing a slash then the
1898 		 * original name is still in oname.  Otherwise the original
1899 		 * directory is in dir->p_name (which is all we need for
1900 		 * $ORIGIN).
1901 		 */
1902 		if (fdp->fd_flags & FLG_FD_SLASH) {
1903 			char	*ofil;
1904 
1905 			odir = fdp->fd_oname;
1906 			ofil = strrchr(fdp->fd_oname, '/');
1907 			olen = ofil - odir + 1;
1908 		} else {
1909 			odir = fdp->fd_odir;
1910 			olen = strlen(odir) + 1;
1911 		}
1912 		if ((ndir = stravl_insert(odir, 0, olen, 1)) == NULL) {
1913 			remove_so(lml, nlmp);
1914 			return (NULL);
1915 		}
1916 		ORIGNAME(nlmp) = ndir;
1917 		DIRSZ(nlmp) = --olen;
1918 	}
1919 
1920 	return (nlmp);
1921 }
1922 
1923 /*
1924  * This function loads the named file and returns a pointer to its link map.
1925  * It is assumed that the caller has already checked that the file is not
1926  * already loaded before calling this function (refer is_so_loaded()).
1927  * Find and open the file, map it into memory, add it to the end of the list
1928  * of link maps and return a pointer to the new link map.  Return 0 on error.
1929  */
1930 static Rt_map *
1931 load_so(Lm_list *lml, Aliste lmco, Rt_map *clmp, uint_t flags,
1932     Fdesc *fdp, Rej_desc *rej, int *in_nfavl)
1933 {
1934 	const char	*oname = fdp->fd_oname;
1935 	Pdesc		*pdp;
1936 
1937 	/*
1938 	 * If this path name hasn't already been identified as containing a
1939 	 * slash, check the path name.  Most paths have been constructed
1940 	 * through appending a file name to a search path, and/or have been
1941 	 * inspected by expand(), and thus have a slash.  However, we can
1942 	 * receive path names via auditors or configuration files, and thus
1943 	 * an evaluation here catches these instances.
1944 	 */
1945 	if ((fdp->fd_flags & FLG_FD_SLASH) == 0) {
1946 		const char	*str;
1947 
1948 		for (str = oname; *str; str++) {
1949 			if (*str == '/') {
1950 				fdp->fd_flags |= FLG_FD_SLASH;
1951 				break;
1952 			}
1953 		}
1954 	}
1955 
1956 	/*
1957 	 * If we are passed a 'null' link-map this means that this is the first
1958 	 * object to be loaded on this link-map list.  In that case we set the
1959 	 * link-map to ld.so.1's link-map.
1960 	 *
1961 	 * This link-map is referenced to determine what lookup rules to use
1962 	 * when searching for files.  By using ld.so.1's we are defaulting to
1963 	 * ELF look-up rules.
1964 	 *
1965 	 * Note: This case happens when loading the first object onto
1966 	 *	 the plt_tracing link-map.
1967 	 */
1968 	if (clmp == 0)
1969 		clmp = lml_rtld.lm_head;
1970 
1971 	/*
1972 	 * If this path resulted from a $HWCAP specification, then the best
1973 	 * hardware capability object has already been establish, and is
1974 	 * available in the calling file descriptor.  Perform some minor book-
1975 	 * keeping so that we can fall through into common code.
1976 	 */
1977 	if (flags & FLG_RT_HWCAP) {
1978 		/*
1979 		 * If this object is already loaded, we're done.
1980 		 */
1981 		if (fdp->fd_lmp)
1982 			return (fdp->fd_lmp);
1983 
1984 		/*
1985 		 * Obtain the avl index for this object.
1986 		 */
1987 		(void) fpavl_recorded(lml, fdp->fd_nname, 0,
1988 		    &(fdp->fd_avlwhere));
1989 
1990 	} else if (fdp->fd_flags & FLG_FD_SLASH) {
1991 		Rej_desc	_rej = { 0 };
1992 
1993 		if (find_path(lml, clmp, flags, fdp, &_rej, in_nfavl) == 0) {
1994 			rejection_inherit(rej, &_rej);
1995 			return (NULL);
1996 		}
1997 
1998 		/*
1999 		 * If this object is already loaded, we're done.
2000 		 */
2001 		if (fdp->fd_lmp)
2002 			return (fdp->fd_lmp);
2003 
2004 	} else {
2005 		/*
2006 		 * No '/' - for each directory on list, make a pathname using
2007 		 * that directory and filename and try to open that file.
2008 		 */
2009 		Spath_desc	sd = { search_rules, NULL, 0 };
2010 		Word		strhash = 0;
2011 		int		found = 0;
2012 
2013 		DBG_CALL(Dbg_libs_find(lml, oname));
2014 
2015 		/*
2016 		 * Traverse the search path lists, creating full pathnames and
2017 		 * attempt to load each path.
2018 		 */
2019 		for (pdp = get_next_dir(&sd, clmp, flags); pdp;
2020 		    pdp = get_next_dir(&sd, clmp, flags)) {
2021 			Rej_desc	_rej = { 0 };
2022 			Fdesc		fd = { 0 };
2023 
2024 			/*
2025 			 * Under debugging, duplicate path name entries are
2026 			 * tagged but remain part of the search path list so
2027 			 * that they can be diagnosed under "unused" processing.
2028 			 * Skip these entries, as this path would have already
2029 			 * been attempted.
2030 			 */
2031 			if (pdp->pd_flags & PD_FLG_DUPLICAT)
2032 				continue;
2033 
2034 			fd = *fdp;
2035 
2036 			/*
2037 			 * Try and locate this file.  Make sure to clean up
2038 			 * any rejection information should the file have
2039 			 * been found, but not appropriate.
2040 			 */
2041 			if (find_file(lml, clmp, flags, &fd, &_rej, pdp,
2042 			    &strhash, in_nfavl) == 0) {
2043 				rejection_inherit(rej, &_rej);
2044 				continue;
2045 			}
2046 
2047 			/*
2048 			 * Indicate that this search path has been used.  If
2049 			 * this is an LD_LIBRARY_PATH setting, ignore any use
2050 			 * by ld.so.1 itself.
2051 			 */
2052 			if (((pdp->pd_flags & LA_SER_LIBPATH) == 0) ||
2053 			    ((lml->lm_flags & LML_FLG_RTLDLM) == 0))
2054 				pdp->pd_flags |= PD_FLG_USED;
2055 
2056 			/*
2057 			 * If this object is already loaded, we're done.
2058 			 */
2059 			*fdp = fd;
2060 			if (fdp->fd_lmp)
2061 				return (fdp->fd_lmp);
2062 
2063 			fdp->fd_odir = pdp->pd_pname;
2064 			found = 1;
2065 			break;
2066 		}
2067 
2068 		/*
2069 		 * If the file couldn't be loaded, do another comparison of
2070 		 * loaded files using just the basename.  This catches folks
2071 		 * who may have loaded multiple full pathname files (possibly
2072 		 * from setxid applications) to satisfy dependency relationships
2073 		 * (i.e., a file might have a dependency on foo.so.1 which has
2074 		 * already been opened using its full pathname).
2075 		 */
2076 		if (found == 0)
2077 			return (is_so_loaded(lml, oname, in_nfavl));
2078 	}
2079 
2080 	/*
2081 	 * Finish mapping the file and return the link-map descriptor.  Note,
2082 	 * if this request originated from a HWCAP request, re-establish the
2083 	 * fdesc information.  For single paged objects, such as filters, the
2084 	 * original mapping may have been sufficient to capture the file, thus
2085 	 * this mapping needs to be reset to insure it doesn't mistakenly get
2086 	 * unmapped as part of HWCAP cleanup.
2087 	 */
2088 	return (load_file(lml, lmco, fdp, in_nfavl));
2089 }
2090 
2091 /*
2092  * Trace an attempt to load an object, and seed the originating name.
2093  */
2094 const char *
2095 load_trace(Lm_list *lml, Pdesc *pdp, Rt_map *clmp, Fdesc *fdp)
2096 {
2097 	const char	*name = pdp->pd_pname;
2098 
2099 	/*
2100 	 * First generate any ldd(1) diagnostics.
2101 	 */
2102 	if ((lml->lm_flags & (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
2103 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
2104 		(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name, NAME(clmp));
2105 
2106 	/*
2107 	 * Propagate any knowledge of a slash within the path name.
2108 	 */
2109 	if (pdp->pd_flags & PD_FLG_PNSLASH)
2110 		fdp->fd_flags |= FLG_FD_SLASH;
2111 
2112 	/*
2113 	 * If we're being audited tell the audit library of the file we're
2114 	 * about to go search for.
2115 	 */
2116 	if (((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_ACTIVITY) &&
2117 	    (lml == LIST(clmp)))
2118 		audit_activity(clmp, LA_ACT_ADD);
2119 
2120 	if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
2121 		char	*aname;
2122 
2123 		/*
2124 		 * The auditor can indicate that this object should be ignored.
2125 		 */
2126 		if ((aname =
2127 		    audit_objsearch(clmp, name, LA_SER_ORIG)) == NULL) {
2128 			DBG_CALL(Dbg_audit_terminate(lml, name));
2129 			return (NULL);
2130 		}
2131 
2132 		if (name != aname) {
2133 			fdp->fd_flags &= ~FLG_FD_SLASH;
2134 			name = aname;
2135 		}
2136 	}
2137 	fdp->fd_oname = name;
2138 	return (name);
2139 }
2140 
2141 /*
2142  * Having loaded an object and created a link-map to describe it, finish
2143  * processing this stage, including verifying any versioning requirements,
2144  * updating the objects mode, creating a handle if necessary, and adding this
2145  * object to existing handles if required.
2146  */
2147 static int
2148 load_finish(Lm_list *lml, const char *name, Rt_map *clmp, int nmode,
2149     uint_t flags, Grp_hdl **hdl, Rt_map *nlmp)
2150 {
2151 	Aliste		idx;
2152 	Grp_hdl		*ghp;
2153 	int		promote;
2154 
2155 	/*
2156 	 * If this dependency is associated with a required version insure that
2157 	 * the version is present in the loaded file.
2158 	 */
2159 	if (((rtld_flags & RT_FL_NOVERSION) == 0) && THIS_IS_ELF(clmp) &&
2160 	    VERNEED(clmp) && (elf_verify_vers(name, clmp, nlmp) == 0))
2161 		return (0);
2162 
2163 	/*
2164 	 * If this object has indicated that it should be isolated as a group
2165 	 * (DT_FLAGS_1 contains DF_1_GROUP - object was built with -B group),
2166 	 * or if the callers direct bindings indicate it should be isolated as
2167 	 * a group (DYNINFO flags contains FLG_DI_GROUP - dependency followed
2168 	 * -zgroupperm), establish the appropriate mode.
2169 	 *
2170 	 * The intent of an object defining itself as a group is to isolate the
2171 	 * relocation of the group within its own members, however, unless
2172 	 * opened through dlopen(), in which case we assume dlsym() will be used
2173 	 * to located symbols in the new object, we still need to associate it
2174 	 * with the caller for it to be bound with.  This is equivalent to a
2175 	 * dlopen(RTLD_GROUP) and dlsym() using the returned handle.
2176 	 */
2177 	if ((FLAGS(nlmp) | flags) & FLG_RT_SETGROUP) {
2178 		nmode &= ~RTLD_WORLD;
2179 		nmode |= RTLD_GROUP;
2180 
2181 		/*
2182 		 * If the object wasn't explicitly dlopen()'ed associate it with
2183 		 * the parent.
2184 		 */
2185 		if ((flags & FLG_RT_HANDLE) == 0)
2186 			nmode |= RTLD_PARENT;
2187 	}
2188 
2189 	/*
2190 	 * Establish new mode and flags.
2191 	 */
2192 	promote = update_mode(nlmp, MODE(nlmp), nmode);
2193 	FLAGS(nlmp) |= flags;
2194 
2195 	/*
2196 	 * If this is a global object, ensure the associated link-map list can
2197 	 * be rescanned for global, lazy dependencies.
2198 	 */
2199 	if (MODE(nlmp) & RTLD_GLOBAL)
2200 		LIST(nlmp)->lm_flags &= ~LML_FLG_NOPENDGLBLAZY;
2201 
2202 	/*
2203 	 * If we've been asked to establish a handle create one for this object.
2204 	 * Or, if this object has already been analyzed, but this reference
2205 	 * requires that the mode of the object be promoted, also create a
2206 	 * handle to propagate the new modes to all this objects dependencies.
2207 	 */
2208 	if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) || (promote &&
2209 	    (FLAGS(nlmp) & FLG_RT_ANALYZED))) {
2210 		uint_t	oflags, hflags = 0, cdflags;
2211 
2212 		/*
2213 		 * Establish any flags for the handle (Grp_hdl).
2214 		 *
2215 		 *  .	Use of the RTLD_FIRST flag indicates that only the first
2216 		 *	dependency on the handle (the new object) can be used
2217 		 *	to satisfy dlsym() requests.
2218 		 */
2219 		if (nmode & RTLD_FIRST)
2220 			hflags = GPH_FIRST;
2221 
2222 		/*
2223 		 * Establish the flags for this callers dependency descriptor
2224 		 * (Grp_desc).
2225 		 *
2226 		 *  .	The creation of a handle associated a descriptor for the
2227 		 *	new object and descriptor for the parent (caller).
2228 		 *	Typically, the handle is created for dlopen() or for
2229 		 *	filtering.  A handle may also be created to promote
2230 		 *	the callers modes (RTLD_NOW) to the new object.  In this
2231 		 *	latter case, the handle/descriptor are torn down once
2232 		 *	the mode propagation has occurred.
2233 		 *
2234 		 *  .	Use of the RTLD_PARENT flag indicates that the parent
2235 		 *	can be relocated against.
2236 		 */
2237 		if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) == 0)
2238 			cdflags = GPD_PROMOTE;
2239 		else
2240 			cdflags = GPD_PARENT;
2241 		if (nmode & RTLD_PARENT)
2242 			cdflags |= GPD_RELOC;
2243 
2244 		/*
2245 		 * Now that a handle is being created, remove this state from
2246 		 * the object so that it doesn't mistakenly get inherited by
2247 		 * a dependency.
2248 		 */
2249 		oflags = FLAGS(nlmp);
2250 		FLAGS(nlmp) &= ~FLG_RT_HANDLE;
2251 
2252 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
2253 		if ((ghp = hdl_create(lml, nlmp, clmp, hflags,
2254 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), cdflags)) == 0)
2255 			return (0);
2256 
2257 		/*
2258 		 * Add any dependencies that are already loaded, to the handle.
2259 		 */
2260 		if (hdl_initialize(ghp, nlmp, nmode, promote) == 0)
2261 			return (0);
2262 
2263 		if (hdl)
2264 			*hdl = ghp;
2265 
2266 		/*
2267 		 * If we were asked to create a handle, we're done.
2268 		 */
2269 		if ((oflags | flags) & FLG_RT_HANDLE)
2270 			return (1);
2271 
2272 		/*
2273 		 * If the handle was created to promote modes from the parent
2274 		 * (caller) to the new object, then this relationship needs to
2275 		 * be removed to ensure the handle doesn't prevent the new
2276 		 * objects from being deleted if required.  If the parent is
2277 		 * the only dependency on the handle, then the handle can be
2278 		 * completely removed.  However, the handle may have already
2279 		 * existed, in which case only the parent descriptor can be
2280 		 * deleted from the handle, or at least the GPD_PROMOTE flag
2281 		 * removed from the descriptor.
2282 		 *
2283 		 * Fall through to carry out any group processing.
2284 		 */
2285 		free_hdl(ghp, clmp, GPD_PROMOTE);
2286 	}
2287 
2288 	/*
2289 	 * If the caller isn't part of a group we're done.
2290 	 */
2291 	if (GROUPS(clmp) == NULL)
2292 		return (1);
2293 
2294 	/*
2295 	 * Determine if our caller is already associated with a handle, if so
2296 	 * we need to add this object to any handles that already exist.
2297 	 * Traverse the list of groups our caller is a member of and add this
2298 	 * new link-map to those groups.
2299 	 */
2300 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
2301 	for (APLIST_TRAVERSE(GROUPS(clmp), idx, ghp)) {
2302 		Aliste		idx1;
2303 		Grp_desc	*gdp;
2304 		int		exist;
2305 		Rt_map		*dlmp1;
2306 		APlist		*lmalp = NULL;
2307 
2308 		/*
2309 		 * If the caller doesn't indicate that its dependencies should
2310 		 * be added to a handle, ignore it.  This case identifies a
2311 		 * parent of a dlopen(RTLD_PARENT) request.
2312 		 */
2313 		for (ALIST_TRAVERSE(ghp->gh_depends, idx1, gdp)) {
2314 			if (gdp->gd_depend == clmp)
2315 				break;
2316 		}
2317 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
2318 			continue;
2319 
2320 		if ((exist = hdl_add(ghp, nlmp,
2321 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0)
2322 			return (0);
2323 
2324 		/*
2325 		 * If this member already exists then its dependencies will
2326 		 * have already been processed.
2327 		 */
2328 		if (exist == ALE_EXISTS)
2329 			continue;
2330 
2331 		/*
2332 		 * If the object we've added has just been opened, it will not
2333 		 * yet have been processed for its dependencies, these will be
2334 		 * added on later calls to load_one().  If it doesn't have any
2335 		 * dependencies we're also done.
2336 		 */
2337 		if (((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0) ||
2338 		    (DEPENDS(nlmp) == NULL))
2339 			continue;
2340 
2341 		/*
2342 		 * Otherwise, this object exists and has dependencies, so add
2343 		 * all of its dependencies to the handle were operating on.
2344 		 */
2345 		if (aplist_append(&lmalp, nlmp, AL_CNT_DEPCLCT) == NULL)
2346 			return (0);
2347 
2348 		for (APLIST_TRAVERSE(lmalp, idx1, dlmp1)) {
2349 			Aliste		idx2;
2350 			Bnd_desc 	*bdp;
2351 
2352 			/*
2353 			 * Add any dependencies of this dependency to the
2354 			 * dynamic dependency list so they can be further
2355 			 * processed.
2356 			 */
2357 			for (APLIST_TRAVERSE(DEPENDS(dlmp1), idx2, bdp)) {
2358 				Rt_map	*dlmp2 = bdp->b_depend;
2359 
2360 				if ((bdp->b_flags & BND_NEEDED) == 0)
2361 					continue;
2362 
2363 				if (aplist_test(&lmalp, dlmp2,
2364 				    AL_CNT_DEPCLCT) == 0) {
2365 					free(lmalp);
2366 					return (0);
2367 				}
2368 			}
2369 
2370 			if (nlmp == dlmp1)
2371 				continue;
2372 
2373 			if ((exist = hdl_add(ghp, dlmp1,
2374 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0) {
2375 				free(lmalp);
2376 				return (0);
2377 			}
2378 
2379 			if (exist == ALE_CREATE)
2380 				(void) update_mode(dlmp1, MODE(dlmp1), nmode);
2381 		}
2382 		free(lmalp);
2383 	}
2384 	return (1);
2385 }
2386 
2387 /*
2388  * The central routine for loading shared objects.  Insures ldd() diagnostics,
2389  * handles and any other related additions are all done in one place.
2390  */
2391 Rt_map *
2392 load_path(Lm_list *lml, Aliste lmco, Rt_map *clmp, int nmode, uint_t flags,
2393     Grp_hdl **hdl, Fdesc *fdp, Rej_desc *rej, int *in_nfavl)
2394 {
2395 	const char	*name = fdp->fd_oname;
2396 	Rt_map		*nlmp;
2397 
2398 	if ((nmode & RTLD_NOLOAD) == 0) {
2399 		/*
2400 		 * If this isn't a noload request attempt to load the file.
2401 		 */
2402 		if ((nlmp = load_so(lml, lmco, clmp, flags, fdp, rej,
2403 		    in_nfavl)) == NULL)
2404 			return (NULL);
2405 
2406 		/*
2407 		 * If we've loaded a library which identifies itself as not
2408 		 * being dlopen()'able catch it here.  Let non-dlopen()'able
2409 		 * objects through under RTLD_CONFGEN as they're only being
2410 		 * mapped to be dldump()'ed.
2411 		 */
2412 		if ((rtld_flags & RT_FL_APPLIC) && ((FLAGS(nlmp) &
2413 		    (FLG_RT_NOOPEN | FLG_RT_RELOCED)) == FLG_RT_NOOPEN) &&
2414 		    ((nmode & RTLD_CONFGEN) == 0)) {
2415 			Rej_desc	_rej = { 0 };
2416 
2417 			_rej.rej_name = name;
2418 			_rej.rej_type = SGS_REJ_STR;
2419 			_rej.rej_str = MSG_INTL(MSG_GEN_NOOPEN);
2420 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
2421 			rejection_inherit(rej, &_rej);
2422 			remove_so(lml, nlmp);
2423 			return (NULL);
2424 		}
2425 	} else {
2426 		/*
2427 		 * If it's a NOLOAD request - check to see if the object
2428 		 * has already been loaded.
2429 		 */
2430 		/* LINTED */
2431 		if (nlmp = is_so_loaded(lml, name, in_nfavl)) {
2432 			if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
2433 			    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
2434 				(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name,
2435 				    NAME(clmp));
2436 				/* BEGIN CSTYLED */
2437 				if (*name == '/')
2438 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
2439 					name, MSG_ORIG(MSG_STR_EMPTY),
2440 					MSG_ORIG(MSG_STR_EMPTY));
2441 				else
2442 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
2443 					name, NAME(nlmp),
2444 					MSG_ORIG(MSG_STR_EMPTY),
2445 					MSG_ORIG(MSG_STR_EMPTY));
2446 				/* END CSTYLED */
2447 			}
2448 		} else {
2449 			Rej_desc	_rej = { 0 };
2450 
2451 			_rej.rej_name = name;
2452 			_rej.rej_type = SGS_REJ_STR;
2453 			_rej.rej_str = strerror(ENOENT);
2454 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
2455 			rejection_inherit(rej, &_rej);
2456 			return (NULL);
2457 		}
2458 	}
2459 
2460 	/*
2461 	 * Finish processing this loaded object.
2462 	 */
2463 	if (load_finish(lml, name, clmp, nmode, flags, hdl, nlmp) == 0) {
2464 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2465 
2466 		/*
2467 		 * If this object has already been analyzed, then it is in use,
2468 		 * so even though this operation has failed, it should not be
2469 		 * torn down.
2470 		 */
2471 		if ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)
2472 			remove_so(lml, nlmp);
2473 		return (NULL);
2474 	}
2475 
2476 	/*
2477 	 * If this object is new, and we're being audited, tell the audit
2478 	 * library of the file we've just opened.  Note, if the new link-map
2479 	 * requires local auditing of its dependencies we also register its
2480 	 * opening.
2481 	 */
2482 	if (FLAGS(nlmp) & FLG_RT_NEWLOAD) {
2483 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2484 
2485 		if (((lml->lm_tflags | AFLAGS(clmp) | AFLAGS(nlmp)) &
2486 		    LML_TFLG_AUD_MASK) && (((lml->lm_flags |
2487 		    LIST(clmp)->lm_flags) & LML_FLG_NOAUDIT) == 0)) {
2488 			if (audit_objopen(clmp, nlmp) == 0) {
2489 				remove_so(lml, nlmp);
2490 				return (NULL);
2491 			}
2492 		}
2493 	}
2494 	return (nlmp);
2495 }
2496 
2497 /*
2498  * Load one object from a possible list of objects.  Typically, for requests
2499  * such as NEEDED's, only one object is specified.  However, this object could
2500  * be specified using $ISALIST or $HWCAP, in which case only the first object
2501  * that can be loaded is used (ie. the best).
2502  */
2503 Rt_map *
2504 load_one(Lm_list *lml, Aliste lmco, Alist *palp, Rt_map *clmp, int mode,
2505     uint_t flags, Grp_hdl **hdl, int *in_nfavl)
2506 {
2507 	Rej_desc	rej = { 0 };
2508 	Aliste		idx;
2509 	Pdesc   	*pdp;
2510 	const char	*name;
2511 
2512 	for (ALIST_TRAVERSE(palp, idx, pdp)) {
2513 		Rt_map	*lmp = NULL;
2514 
2515 		/*
2516 		 * A Hardware capabilities requirement can itself expand into
2517 		 * a number of candidates.
2518 		 */
2519 		if (pdp->pd_flags & PD_TKN_HWCAP) {
2520 			lmp = load_hwcap(lml, lmco, pdp->pd_pname, clmp,
2521 			    mode, (flags | FLG_RT_HWCAP), hdl, &rej, in_nfavl);
2522 		} else {
2523 			Fdesc	fd = { 0 };
2524 
2525 			/*
2526 			 * Trace the inspection of this file, determine any
2527 			 * auditor substitution, and seed the file descriptor
2528 			 * with the originating name.
2529 			 */
2530 			if (load_trace(lml, pdp, clmp, &fd) == NULL)
2531 				continue;
2532 
2533 			/*
2534 			 * Locate and load the file.
2535 			 */
2536 			lmp = load_path(lml, lmco, clmp, mode, flags, hdl, &fd,
2537 			    &rej, in_nfavl);
2538 		}
2539 		if (lmp)
2540 			return (lmp);
2541 	}
2542 
2543 	/*
2544 	 * If no objects can be found, use the first path name from the Alist
2545 	 * to provide a diagnostic.  If this pathname originated from an
2546 	 * expanded token, use the original name for any diagnostic output.
2547 	 */
2548 	pdp = alist_item(palp, 0);
2549 
2550 	if ((name = pdp->pd_oname) == 0)
2551 		name = pdp->pd_pname;
2552 
2553 	file_notfound(lml, name, clmp, flags, &rej);
2554 	return (NULL);
2555 }
2556 
2557 /*
2558  * Determine whether a symbol is defined as an interposer.
2559  */
2560 int
2561 is_sym_interposer(Rt_map *lmp, Sym *sym)
2562 {
2563 	Syminfo	*sip = SYMINFO(lmp);
2564 
2565 	if (sip) {
2566 		ulong_t	ndx;
2567 
2568 		ndx = (((ulong_t)sym - (ulong_t)SYMTAB(lmp)) / SYMENT(lmp));
2569 		/* LINTED */
2570 		sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(lmp)));
2571 		if (sip->si_flags & SYMINFO_FLG_INTERPOSE)
2572 			return (1);
2573 	}
2574 	return (0);
2575 }
2576 
2577 /*
2578  * While processing direct or group bindings, determine whether the object to
2579  * which we've bound can be interposed upon.  In this context, copy relocations
2580  * are a form of interposition.
2581  */
2582 static Sym *
2583 lookup_sym_interpose(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Sym *osym,
2584     int *in_nfavl)
2585 {
2586 	Rt_map		*lmp, *clmp;
2587 	Slookup		sl;
2588 	Lm_list		*lml;
2589 
2590 	/*
2591 	 * If we've bound to a copy relocation definition then we need to assign
2592 	 * this binding to the original copy reference.  Fabricate an inter-
2593 	 * position diagnostic, as this is a legitimate form of interposition.
2594 	 */
2595 	if (osym && (FLAGS1(*dlmp) & FL1_RT_COPYTOOK)) {
2596 		Rel_copy	*rcp;
2597 		Aliste		idx;
2598 
2599 		for (ALIST_TRAVERSE(COPY_R(*dlmp), idx, rcp)) {
2600 			if ((osym == rcp->r_dsym) || (osym->st_value &&
2601 			    (osym->st_value == rcp->r_dsym->st_value))) {
2602 				*dlmp = rcp->r_rlmp;
2603 				*binfo |=
2604 				    (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
2605 				return (rcp->r_rsym);
2606 			}
2607 		}
2608 	}
2609 
2610 	/*
2611 	 * If a symbol binding has been established, inspect the link-map list
2612 	 * of the destination object, otherwise use the link-map list of the
2613 	 * original caller.
2614 	 */
2615 	if (osym)
2616 		clmp = *dlmp;
2617 	else
2618 		clmp = slp->sl_cmap;
2619 
2620 	lml = LIST(clmp);
2621 	lmp = lml->lm_head;
2622 
2623 	/*
2624 	 * Prior to Solaris 8, external references from an executable that were
2625 	 * bound to an uninitialized variable (.bss) within a shared object did
2626 	 * not establish a copy relocation.  This was thought to be an
2627 	 * optimization, to prevent copying zero's to zero's.  Typically,
2628 	 * interposition took its course, with the shared object binding to the
2629 	 * executables data definition.
2630 	 *
2631 	 * This scenario can be broken when this old executable runs against a
2632 	 * new shared object that is directly bound.  With no copy-relocation
2633 	 * record, ld.so.1 has no data to trigger the normal vectoring of the
2634 	 * binding to the executable.
2635 	 *
2636 	 * Starting with Solaris 8, a DT_FLAGS entry is written to all objects,
2637 	 * regardless of there being any DF_ flags entries.  Therefore, an
2638 	 * object without this dynamic tag is susceptible to the copy relocation
2639 	 * issue.  If the executable has no DT_FLAGS tag, and contains the same
2640 	 * .bss symbol definition as has been directly bound to, redirect the
2641 	 * binding to the executables data definition.
2642 	 */
2643 	if (osym && ((FLAGS1(lmp) & FL1_RT_DTFLAGS) == 0) &&
2644 	    (FCT(lmp) == &elf_fct) &&
2645 	    (ELF_ST_TYPE(osym->st_info) != STT_FUNC) &&
2646 	    are_bits_zero(*dlmp, osym, 0)) {
2647 		Rt_map	*ilmp;
2648 		Sym	*isym;
2649 
2650 		sl = *slp;
2651 		sl.sl_imap = lmp;
2652 
2653 		/*
2654 		 * Determine whether the same symbol name exists within the
2655 		 * executable, that the size and type of symbol are the same,
2656 		 * and that the symbol is also associated with .bss.
2657 		 */
2658 		if (((isym = SYMINTP(lmp)(&sl, &ilmp, binfo,
2659 		    in_nfavl)) != NULL) && (isym->st_size == osym->st_size) &&
2660 		    (isym->st_info == osym->st_info) &&
2661 		    are_bits_zero(lmp, isym, 1)) {
2662 			*dlmp = lmp;
2663 			*binfo |= (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
2664 			return (isym);
2665 		}
2666 	}
2667 
2668 	if ((lml->lm_flags & LML_FLG_INTRPOSE) == 0)
2669 		return (NULL);
2670 
2671 	/*
2672 	 * Traverse the list of known interposers to determine whether any
2673 	 * offer the same symbol.  Note, the head of the link-map could be
2674 	 * identified as an interposer.  Otherwise, skip the head of the
2675 	 * link-map, so that we don't bind to any .plt references, or
2676 	 * copy-relocation destinations unintentionally.
2677 	 */
2678 	lmp = lml->lm_head;
2679 	sl = *slp;
2680 
2681 	if (((FLAGS(lmp) & MSK_RT_INTPOSE) == 0) || (sl.sl_flags & LKUP_COPY))
2682 		lmp = NEXT_RT_MAP(lmp);
2683 
2684 	for (; lmp; lmp = NEXT_RT_MAP(lmp)) {
2685 		if (FLAGS(lmp) & FLG_RT_DELETE)
2686 			continue;
2687 		if ((FLAGS(lmp) & MSK_RT_INTPOSE) == 0)
2688 			break;
2689 
2690 		if (callable(lmp, clmp, 0, sl.sl_flags)) {
2691 			Rt_map	*ilmp;
2692 			Sym	*isym;
2693 
2694 			sl.sl_imap = lmp;
2695 			if (isym = SYMINTP(lmp)(&sl, &ilmp, binfo, in_nfavl)) {
2696 				/*
2697 				 * If this object provides individual symbol
2698 				 * interposers, make sure that the symbol we
2699 				 * have found is tagged as an interposer.
2700 				 */
2701 				if ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
2702 				    (is_sym_interposer(ilmp, isym) == 0))
2703 					continue;
2704 
2705 				/*
2706 				 * Indicate this binding has occurred to an
2707 				 * interposer, and return the symbol.
2708 				 */
2709 				*binfo |= DBG_BINFO_INTERPOSE;
2710 				*dlmp = ilmp;
2711 				return (isym);
2712 			}
2713 		}
2714 	}
2715 	return (NULL);
2716 }
2717 
2718 /*
2719  * If an object specifies direct bindings (it contains a syminfo structure
2720  * describing where each binding was established during link-editing, and the
2721  * object was built -Bdirect), then look for the symbol in the specific object.
2722  */
2723 static Sym *
2724 lookup_sym_direct(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Syminfo *sip,
2725     Rt_map *lmp, int *in_nfavl)
2726 {
2727 	Rt_map	*clmp = slp->sl_cmap;
2728 	Sym	*sym;
2729 	Slookup	sl;
2730 
2731 	/*
2732 	 * If a direct binding resolves to the definition of a copy relocated
2733 	 * variable, it must be redirected to the copy (in the executable) that
2734 	 * will eventually be made.  Typically, this redirection occurs in
2735 	 * lookup_sym_interpose().  But, there's an edge condition.  If a
2736 	 * directly bound executable contains pic code, there may be a
2737 	 * reference to a definition that will eventually have a copy made.
2738 	 * However, this copy relocation may not yet have occurred, because
2739 	 * the relocation making this reference comes before the relocation
2740 	 * that will create the copy.
2741 	 * Under direct bindings, the syminfo indicates that a copy will be
2742 	 * taken (SYMINFO_FLG_COPY).  This can only be set in an executable.
2743 	 * Thus, the caller must be the executable, so bind to the destination
2744 	 * of the copy within the executable.
2745 	 */
2746 	if (((slp->sl_flags & LKUP_COPY) == 0) &&
2747 	    (sip->si_flags & SYMINFO_FLG_COPY)) {
2748 
2749 		slp->sl_imap = LIST(clmp)->lm_head;
2750 		if (sym = SYMINTP(clmp)(slp, dlmp, binfo, in_nfavl))
2751 			*binfo |= (DBG_BINFO_DIRECT | DBG_BINFO_COPYREF);
2752 		return (sym);
2753 	}
2754 
2755 	/*
2756 	 * If we need to directly bind to our parent, start looking in each
2757 	 * callers link map.
2758 	 */
2759 	sl = *slp;
2760 	sl.sl_flags |= LKUP_DIRECT;
2761 	sym = NULL;
2762 
2763 	if (sip->si_boundto == SYMINFO_BT_PARENT) {
2764 		Aliste		idx1;
2765 		Bnd_desc	*bdp;
2766 		Grp_hdl		*ghp;
2767 
2768 		/*
2769 		 * Determine the parent of this explicit dependency from its
2770 		 * CALLERS()'s list.
2771 		 */
2772 		for (APLIST_TRAVERSE(CALLERS(clmp), idx1, bdp)) {
2773 			sl.sl_imap = lmp = bdp->b_caller;
2774 			if ((sym = SYMINTP(lmp)(&sl, dlmp, binfo,
2775 			    in_nfavl)) != NULL)
2776 				goto found;
2777 		}
2778 
2779 		/*
2780 		 * A caller can also be defined as the parent of a dlopen()
2781 		 * call.  Determine whether this object has any handles.  The
2782 		 * dependencies maintained with the handle represent the
2783 		 * explicit dependencies of the dlopen()'ed object, and the
2784 		 * calling parent.
2785 		 */
2786 		for (APLIST_TRAVERSE(HANDLES(clmp), idx1, ghp)) {
2787 			Grp_desc	*gdp;
2788 			Aliste		idx2;
2789 
2790 			for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
2791 				if ((gdp->gd_flags & GPD_PARENT) == 0)
2792 					continue;
2793 				sl.sl_imap = lmp = gdp->gd_depend;
2794 				if ((sym = SYMINTP(lmp)(&sl, dlmp,
2795 				    binfo, in_nfavl)) != NULL)
2796 					goto found;
2797 			}
2798 		}
2799 	} else {
2800 		/*
2801 		 * If we need to direct bind to anything else look in the
2802 		 * link map associated with this symbol reference.
2803 		 */
2804 		if (sip->si_boundto == SYMINFO_BT_SELF)
2805 			sl.sl_imap = lmp = clmp;
2806 		else
2807 			sl.sl_imap = lmp;
2808 
2809 		if (lmp)
2810 			sym = SYMINTP(lmp)(&sl, dlmp, binfo, in_nfavl);
2811 	}
2812 found:
2813 	if (sym)
2814 		*binfo |= DBG_BINFO_DIRECT;
2815 
2816 	/*
2817 	 * If a reference to a directly bound symbol can't be satisfied, then
2818 	 * determine whether an interposer can provide the missing symbol.  If
2819 	 * a reference to a directly bound symbol is satisfied, then determine
2820 	 * whether that object can be interposed upon for this symbol.
2821 	 */
2822 	if ((sym == NULL) || ((LIST(*dlmp)->lm_head != *dlmp) &&
2823 	    (LIST(*dlmp) == LIST(clmp)))) {
2824 		Sym	*isym;
2825 
2826 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
2827 		    in_nfavl)) != 0)
2828 			return (isym);
2829 	}
2830 
2831 	return (sym);
2832 }
2833 
2834 static Sym *
2835 core_lookup_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
2836     Aliste off, int *in_nfavl)
2837 {
2838 	Rt_map	*lmp;
2839 
2840 	/*
2841 	 * Copy relocations should start their search after the head of the
2842 	 * main link-map control list.
2843 	 */
2844 	if ((off == ALIST_OFF_DATA) && (slp->sl_flags & LKUP_COPY) && ilmp)
2845 		lmp = NEXT_RT_MAP(ilmp);
2846 	else
2847 		lmp = ilmp;
2848 
2849 	for (; lmp; lmp = NEXT_RT_MAP(lmp)) {
2850 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
2851 			Sym	*sym;
2852 
2853 			slp->sl_imap = lmp;
2854 			if (((sym = SYMINTP(lmp)(slp, dlmp, binfo,
2855 			    in_nfavl)) != NULL) ||
2856 			    (*binfo & BINFO_MSK_TRYAGAIN))
2857 				return (sym);
2858 		}
2859 	}
2860 	return (NULL);
2861 }
2862 
2863 static Sym *
2864 _lazy_find_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
2865     int *in_nfavl)
2866 {
2867 	Rt_map	*lmp;
2868 
2869 	for (lmp = ilmp; lmp; lmp = NEXT_RT_MAP(lmp)) {
2870 		if (LAZY(lmp) == 0)
2871 			continue;
2872 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
2873 			Sym	*sym;
2874 
2875 			slp->sl_imap = lmp;
2876 			if ((sym = elf_lazy_find_sym(slp, dlmp, binfo,
2877 			    in_nfavl)) != 0)
2878 				return (sym);
2879 		}
2880 	}
2881 	return (NULL);
2882 }
2883 
2884 static Sym *
2885 _lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
2886 {
2887 	const char	*name = slp->sl_name;
2888 	Rt_map		*clmp = slp->sl_cmap;
2889 	Lm_list		*lml = LIST(clmp);
2890 	Rt_map		*ilmp = slp->sl_imap, *lmp;
2891 	ulong_t		rsymndx;
2892 	Sym		*sym;
2893 	Syminfo		*sip;
2894 	Slookup		sl;
2895 
2896 	/*
2897 	 * Search the initial link map for the required symbol (this category is
2898 	 * selected by dlsym(), where individual link maps are searched for a
2899 	 * required symbol.  Therefore, we know we have permission to look at
2900 	 * the link map).
2901 	 */
2902 	if (slp->sl_flags & LKUP_FIRST)
2903 		return (SYMINTP(ilmp)(slp, dlmp, binfo, in_nfavl));
2904 
2905 	/*
2906 	 * Determine whether this lookup can be satisfied by an objects direct,
2907 	 * or lazy binding information.  This is triggered by a relocation from
2908 	 * the object (hence rsymndx is set).
2909 	 */
2910 	if (((rsymndx = slp->sl_rsymndx) != 0) &&
2911 	    ((sip = SYMINFO(clmp)) != NULL)) {
2912 		uint_t	bound;
2913 
2914 		/*
2915 		 * Find the corresponding Syminfo entry for the original
2916 		 * referencing symbol.
2917 		 */
2918 		/* LINTED */
2919 		sip = (Syminfo *)((char *)sip + (rsymndx * SYMINENT(clmp)));
2920 		bound = sip->si_boundto;
2921 
2922 		/*
2923 		 * Identify any EXTERN or PARENT references for ldd(1).
2924 		 */
2925 		if ((lml->lm_flags & LML_FLG_TRC_WARN) &&
2926 		    (bound > SYMINFO_BT_LOWRESERVE)) {
2927 			if (bound == SYMINFO_BT_PARENT)
2928 				*binfo |= DBG_BINFO_REF_PARENT;
2929 			if (bound == SYMINFO_BT_EXTERN)
2930 				*binfo |= DBG_BINFO_REF_EXTERN;
2931 		}
2932 
2933 		/*
2934 		 * If the symbol information indicates a direct binding,
2935 		 * determine the link map that is required to satisfy the
2936 		 * binding.  Note, if the dependency can not be found, but a
2937 		 * direct binding isn't required, we will still fall through
2938 		 * to perform any default symbol search.
2939 		 */
2940 		if (sip->si_flags & SYMINFO_FLG_DIRECT) {
2941 
2942 			lmp = 0;
2943 			if (bound < SYMINFO_BT_LOWRESERVE)
2944 				lmp = elf_lazy_load(clmp, slp, bound,
2945 				    name, in_nfavl);
2946 
2947 			/*
2948 			 * If direct bindings have been disabled, and this isn't
2949 			 * a translator, skip any direct binding now that we've
2950 			 * ensured the resolving object has been loaded.
2951 			 *
2952 			 * If we need to direct bind to anything, we look in
2953 			 * ourselves, our parent, or in the link map we've just
2954 			 * loaded.  Otherwise, even though we may have lazily
2955 			 * loaded an object we still continue to search for
2956 			 * symbols from the head of the link map list.
2957 			 */
2958 			if (((FLAGS(clmp) & FLG_RT_TRANS) ||
2959 			    (((lml->lm_tflags & LML_TFLG_NODIRECT) == 0) &&
2960 			    ((slp->sl_flags & LKUP_SINGLETON) == 0))) &&
2961 			    ((FLAGS1(clmp) & FL1_RT_DIRECT) ||
2962 			    (sip->si_flags & SYMINFO_FLG_DIRECTBIND))) {
2963 				sym = lookup_sym_direct(slp, dlmp, binfo,
2964 				    sip, lmp, in_nfavl);
2965 
2966 				/*
2967 				 * Determine whether this direct binding has
2968 				 * been rejected.  If we've bound to a singleton
2969 				 * without following a singleton search, then
2970 				 * return.  The caller detects this condition
2971 				 * and will trigger a new singleton search.
2972 				 *
2973 				 * For any other rejection (such as binding to
2974 				 * a symbol labeled as nodirect - presumably
2975 				 * because the symbol definition has been
2976 				 * changed since the referring object was last
2977 				 * built), fall through to a standard symbol
2978 				 * search.
2979 				 */
2980 				if (((*binfo & BINFO_MSK_REJECTED) == 0) ||
2981 				    (*binfo & BINFO_MSK_TRYAGAIN))
2982 					return (sym);
2983 
2984 				*binfo &= ~BINFO_MSK_REJECTED;
2985 			}
2986 		}
2987 	}
2988 
2989 	/*
2990 	 * Duplicate the lookup information, as we'll need to modify this
2991 	 * information for some of the following searches.
2992 	 */
2993 	sl = *slp;
2994 
2995 	/*
2996 	 * If the referencing object has the DF_SYMBOLIC flag set, look in the
2997 	 * referencing object for the symbol first.  Failing that, fall back to
2998 	 * our generic search.
2999 	 */
3000 	if ((FLAGS1(clmp) & FL1_RT_SYMBOLIC) &&
3001 	    ((sl.sl_flags & LKUP_SINGLETON) == 0)) {
3002 		sl.sl_imap = clmp;
3003 		if (sym = SYMINTP(clmp)(&sl, dlmp, binfo, in_nfavl)) {
3004 			ulong_t	dsymndx = (((ulong_t)sym -
3005 			    (ulong_t)SYMTAB(*dlmp)) / SYMENT(*dlmp));
3006 
3007 			/*
3008 			 * Make sure this symbol hasn't explicitly been defined
3009 			 * as nodirect.
3010 			 */
3011 			if (((sip = SYMINFO(*dlmp)) == 0) ||
3012 			    /* LINTED */
3013 			    ((sip = (Syminfo *)((char *)sip +
3014 			    (dsymndx * SYMINENT(*dlmp)))) == 0) ||
3015 			    ((sip->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0))
3016 				return (sym);
3017 		}
3018 	}
3019 
3020 	sl.sl_flags |= LKUP_STANDARD;
3021 
3022 	/*
3023 	 * If this lookup originates from a standard relocation, then traverse
3024 	 * all link-map control lists, inspecting any object that is available
3025 	 * to this caller.  Otherwise, traverse the link-map control list
3026 	 * associated with the caller.
3027 	 */
3028 	if (sl.sl_flags & LKUP_STDRELOC) {
3029 		Aliste	off;
3030 		Lm_cntl	*lmc;
3031 
3032 		sym = NULL;
3033 
3034 		for (ALIST_TRAVERSE_BY_OFFSET(lml->lm_lists, off, lmc)) {
3035 			if (((sym = core_lookup_sym(lmc->lc_head, &sl, dlmp,
3036 			    binfo, off, in_nfavl)) != NULL) ||
3037 			    (*binfo & BINFO_MSK_TRYAGAIN))
3038 				break;
3039 		}
3040 	} else
3041 		sym = core_lookup_sym(ilmp, &sl, dlmp, binfo, ALIST_OFF_DATA,
3042 		    in_nfavl);
3043 
3044 	/*
3045 	 * If a symbol binding should be retried, return so that the search can
3046 	 * be repeated.
3047 	 */
3048 	if (*binfo & BINFO_MSK_TRYAGAIN)
3049 		return (sym);
3050 
3051 	/*
3052 	 * To allow transitioning into a world of lazy loading dependencies see
3053 	 * if this link map contains objects that have lazy dependencies still
3054 	 * outstanding.  If so, and we haven't been able to locate a non-weak
3055 	 * symbol reference, start bringing in any lazy dependencies to see if
3056 	 * the reference can be satisfied.  Use of dlsym(RTLD_PROBE) sets the
3057 	 * LKUP_NOFALLBACK flag, and this flag disables this fall back.
3058 	 */
3059 	if ((sym == NULL) && ((sl.sl_flags & LKUP_NOFALLBACK) == 0)) {
3060 		if ((lmp = ilmp) == 0)
3061 			lmp = LIST(clmp)->lm_head;
3062 
3063 		lml = LIST(lmp);
3064 		if ((sl.sl_flags & LKUP_WEAK) || (lml->lm_lazy == 0))
3065 			return (NULL);
3066 
3067 		DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
3068 
3069 		/*
3070 		 * If this request originated from a dlsym(RTLD_NEXT) then start
3071 		 * looking for dependencies from the caller, otherwise use the
3072 		 * initial link-map.
3073 		 */
3074 		if (sl.sl_flags & LKUP_NEXT)
3075 			sym = _lazy_find_sym(clmp, &sl, dlmp, binfo, in_nfavl);
3076 		else {
3077 			Aliste	idx;
3078 			Lm_cntl	*lmc;
3079 
3080 			for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
3081 				sl.sl_flags |= LKUP_NOFALLBACK;
3082 				if ((sym = _lazy_find_sym(lmc->lc_head, &sl,
3083 				    dlmp, binfo, in_nfavl)) != 0)
3084 					break;
3085 			}
3086 		}
3087 	}
3088 	return (sym);
3089 }
3090 
3091 /*
3092  * Symbol lookup routine.  Takes an ELF symbol name, and a list of link maps to
3093  * search.  If successful, return a pointer to the symbol table entry, a
3094  * pointer to the link map of the enclosing object, and information relating
3095  * to the type of binding.  Else return a null pointer.
3096  *
3097  * To improve elf performance, we first compute the elf hash value and pass
3098  * it to each find_sym() routine.  The elf function will use this value to
3099  * locate the symbol, the a.out function will simply ignore it.
3100  */
3101 Sym *
3102 lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
3103 {
3104 	Rt_map		*clmp = slp->sl_cmap;
3105 	Sym		*rsym = slp->sl_rsym, *sym = 0;
3106 	uchar_t		rtype = slp->sl_rtype;
3107 	int		mode;
3108 
3109 	if (slp->sl_hash == 0)
3110 		slp->sl_hash = elf_hash(slp->sl_name);
3111 	*binfo = 0;
3112 
3113 	/*
3114 	 * Establish any state that might be associated with a symbol reference.
3115 	 */
3116 	if (rsym) {
3117 		if ((slp->sl_flags & LKUP_STDRELOC) &&
3118 		    (ELF_ST_BIND(rsym->st_info) == STB_WEAK))
3119 			slp->sl_flags |= LKUP_WEAK;
3120 
3121 		if (ELF_ST_VISIBILITY(rsym->st_other) == STV_SINGLETON)
3122 			slp->sl_flags |= LKUP_SINGLETON;
3123 	}
3124 
3125 	/*
3126 	 * Establish any lookup state required for this type of relocation.
3127 	 */
3128 	if ((slp->sl_flags & LKUP_STDRELOC) && rtype) {
3129 		if (rtype == M_R_COPY)
3130 			slp->sl_flags |= LKUP_COPY;
3131 
3132 		if (rtype != M_R_JMP_SLOT)
3133 			slp->sl_flags |= LKUP_SPEC;
3134 	}
3135 
3136 	/*
3137 	 * Under ldd -w, any unresolved weak references are diagnosed.  Set the
3138 	 * symbol binding as global to trigger a relocation error if the symbol
3139 	 * can not be found.
3140 	 */
3141 	if (rsym) {
3142 		if (LIST(slp->sl_cmap)->lm_flags & LML_FLG_TRC_NOUNRESWEAK)
3143 			slp->sl_bind = STB_GLOBAL;
3144 		else if ((slp->sl_bind = ELF_ST_BIND(rsym->st_info)) ==
3145 		    STB_WEAK)
3146 			slp->sl_flags |= LKUP_WEAK;
3147 	}
3148 
3149 	/*
3150 	 * Save the callers MODE().
3151 	 */
3152 	mode = MODE(clmp);
3153 
3154 	/*
3155 	 * Carry out an initial symbol search.  This search takes into account
3156 	 * all the modes of the requested search.
3157 	 */
3158 	if (((sym = _lookup_sym(slp, dlmp, binfo, in_nfavl)) == NULL) &&
3159 	    (*binfo & BINFO_MSK_TRYAGAIN)) {
3160 		Slookup	sl = *slp;
3161 
3162 		/*
3163 		 * Try the symbol search again.  This retry can be necessary if:
3164 		 *
3165 		 *  .	a binding has been rejected because of binding to a
3166 		 *	singleton without going through a singleton search.
3167 		 *  .	a group binding has resulted in binding to a symbol
3168 		 *	that indicates no-direct binding.
3169 		 *
3170 		 * Reset the lookup data, and try again.
3171 		 */
3172 		sl.sl_imap = LIST(sl.sl_cmap)->lm_head;
3173 		sl.sl_flags &= ~(LKUP_FIRST | LKUP_SELF | LKUP_NEXT);
3174 		sl.sl_rsymndx = 0;
3175 
3176 		if (*binfo & BINFO_REJSINGLE)
3177 			sl.sl_flags |= LKUP_SINGLETON;
3178 		if (*binfo & BINFO_REJGROUP) {
3179 			sl.sl_flags |= LKUP_WORLD;
3180 			mode |= RTLD_WORLD;
3181 		}
3182 		*binfo &= ~BINFO_MSK_REJECTED;
3183 
3184 		sym = _lookup_sym(&sl, dlmp, binfo, in_nfavl);
3185 	}
3186 
3187 	/*
3188 	 * If the caller is restricted to a symbol search within its group,
3189 	 * determine if it is necessary to follow a binding from outside of
3190 	 * the group.
3191 	 */
3192 	if ((mode & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP) {
3193 		Sym	*isym;
3194 
3195 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
3196 		    in_nfavl)) != NULL)
3197 			return (isym);
3198 	}
3199 	return (sym);
3200 }
3201 
3202 /*
3203  * Associate a binding descriptor with a caller and its dependency, or update
3204  * an existing descriptor.
3205  */
3206 int
3207 bind_one(Rt_map *clmp, Rt_map *dlmp, uint_t flags)
3208 {
3209 	Bnd_desc	*bdp;
3210 	Aliste		idx;
3211 	int		found = ALE_CREATE;
3212 
3213 	/*
3214 	 * Determine whether a binding descriptor already exists between the
3215 	 * two objects.
3216 	 */
3217 	for (APLIST_TRAVERSE(DEPENDS(clmp), idx, bdp)) {
3218 		if (bdp->b_depend == dlmp) {
3219 			found = ALE_EXISTS;
3220 			break;
3221 		}
3222 	}
3223 
3224 	if (found == ALE_CREATE) {
3225 		/*
3226 		 * Create a new binding descriptor.
3227 		 */
3228 		if ((bdp = malloc(sizeof (Bnd_desc))) == NULL)
3229 			return (0);
3230 
3231 		bdp->b_caller = clmp;
3232 		bdp->b_depend = dlmp;
3233 		bdp->b_flags = 0;
3234 
3235 		/*
3236 		 * Append the binding descriptor to the caller and the
3237 		 * dependency.
3238 		 */
3239 		if (aplist_append(&DEPENDS(clmp), bdp, AL_CNT_DEPENDS) == NULL)
3240 			return (0);
3241 
3242 		if (aplist_append(&CALLERS(dlmp), bdp, AL_CNT_CALLERS) == NULL)
3243 			return (0);
3244 	}
3245 
3246 	if ((found == ALE_CREATE) || ((bdp->b_flags & flags) != flags)) {
3247 		bdp->b_flags |= flags;
3248 
3249 		if (flags & BND_REFER)
3250 			FLAGS1(dlmp) |= FL1_RT_USED;
3251 
3252 		DBG_CALL(Dbg_file_bind_entry(LIST(clmp), bdp));
3253 	}
3254 	return (found);
3255 }
3256 
3257 /*
3258  * Cleanup after relocation processing.
3259  */
3260 int
3261 relocate_finish(Rt_map *lmp, APlist *bound, int ret)
3262 {
3263 	DBG_CALL(Dbg_reloc_run(lmp, 0, ret, DBG_REL_FINISH));
3264 
3265 	/*
3266 	 * Establish bindings to all objects that have been bound to.
3267 	 */
3268 	if (bound) {
3269 		Rt_map	*_lmp;
3270 		Word	used;
3271 
3272 		/*
3273 		 * Only create bindings if the callers relocation was
3274 		 * successful (ret != 0), otherwise the object will eventually
3275 		 * be torn down.  Create these bindings if running under ldd(1)
3276 		 * with the -U/-u options regardless of relocation errors, as
3277 		 * the unused processing needs to traverse these bindings to
3278 		 * diagnose unused objects.
3279 		 */
3280 		used = LIST(lmp)->lm_flags &
3281 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED);
3282 
3283 		if (ret || used) {
3284 			Aliste	idx;
3285 
3286 			for (APLIST_TRAVERSE(bound, idx, _lmp)) {
3287 				if (bind_one(lmp, _lmp, BND_REFER) || used)
3288 					continue;
3289 
3290 				ret = 0;
3291 				break;
3292 			}
3293 		}
3294 		free(bound);
3295 	}
3296 
3297 	return (ret);
3298 }
3299 
3300 /*
3301  * Function to correct protection settings.  Segments are all mapped initially
3302  * with permissions as given in the segment header.  We need to turn on write
3303  * permissions on a text segment if there are any relocations against that
3304  * segment, and then turn write permission back off again before returning
3305  * control to the caller.  This function turns the permission on or off
3306  * depending on the value of the permission argument.
3307  */
3308 int
3309 set_prot(Rt_map *lmp, mmapobj_result_t *mpp, int perm)
3310 {
3311 	int	prot;
3312 
3313 	/*
3314 	 * If this is an allocated image (ie. a relocatable object) we can't
3315 	 * mprotect() anything.
3316 	 */
3317 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
3318 		return (1);
3319 
3320 	DBG_CALL(Dbg_file_prot(lmp, perm));
3321 
3322 	if (perm)
3323 		prot = mpp->mr_prot | PROT_WRITE;
3324 	else
3325 		prot = mpp->mr_prot & ~PROT_WRITE;
3326 
3327 	if (mprotect((void *)(uintptr_t)mpp->mr_addr,
3328 	    mpp->mr_msize, prot) == -1) {
3329 		int	err = errno;
3330 		eprintf(LIST(lmp), ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
3331 		    NAME(lmp), strerror(err));
3332 		return (0);
3333 	}
3334 	mpp->mr_prot = prot;
3335 	return (1);
3336 }
3337