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