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