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