xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/analyze.c (revision 3dbfc803)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fb1354edSrie 
227c478bd9Sstevel@tonic-gate /*
23cce0e03bSab  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277257d1b4Sraf /*
287257d1b4Sraf  *	Copyright (c) 1988 AT&T
297257d1b4Sraf  *	  All Rights Reserved
307257d1b4Sraf  */
317257d1b4Sraf 
327c478bd9Sstevel@tonic-gate #include	<string.h>
337c478bd9Sstevel@tonic-gate #include	<stdio.h>
347c478bd9Sstevel@tonic-gate #include	<unistd.h>
357c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
367c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
377c478bd9Sstevel@tonic-gate #include	<fcntl.h>
387c478bd9Sstevel@tonic-gate #include	<limits.h>
397c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
407c478bd9Sstevel@tonic-gate #include	<errno.h>
417c478bd9Sstevel@tonic-gate #include	<link.h>
425aefb655Srie #include	<debug.h>
435aefb655Srie #include	<conv.h>
447c478bd9Sstevel@tonic-gate #include	"_rtld.h"
457c478bd9Sstevel@tonic-gate #include	"_audit.h"
467c478bd9Sstevel@tonic-gate #include	"_elf.h"
477c478bd9Sstevel@tonic-gate #include	"msg.h"
487c478bd9Sstevel@tonic-gate 
49aa736cbeSrie static Fct	*vector[] = {
507c478bd9Sstevel@tonic-gate 	&elf_fct,
517c478bd9Sstevel@tonic-gate #ifdef A_OUT
527c478bd9Sstevel@tonic-gate 	&aout_fct,
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 	0
557c478bd9Sstevel@tonic-gate };
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * If a load filter flag is in effect, and this object is a filter, trigger the
597c478bd9Sstevel@tonic-gate  * loading of all its filtees.  The load filter flag is in effect when creating
607c478bd9Sstevel@tonic-gate  * configuration files, or when under the control of ldd(1), or the LD_LOADFLTR
617c478bd9Sstevel@tonic-gate  * environment variable is set, or this object was built with the -zloadfltr
627c478bd9Sstevel@tonic-gate  * flag.  Otherwise, filtee loading is deferred until triggered by a relocation.
637c478bd9Sstevel@tonic-gate  */
647c478bd9Sstevel@tonic-gate static void
659aa23310Srie load_filtees(Rt_map *lmp, int *in_nfavl)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	if ((FLAGS1(lmp) & MSK_RT_FILTER) &&
687c478bd9Sstevel@tonic-gate 	    ((FLAGS(lmp) & FLG_RT_LOADFLTR) ||
697c478bd9Sstevel@tonic-gate 	    (LIST(lmp)->lm_tflags & LML_TFLG_LOADFLTR))) {
7075e7992aSrie 		Dyninfo		*dip =  DYNINFO(lmp);
717c478bd9Sstevel@tonic-gate 		uint_t		cnt, max = DYNINFOCNT(lmp);
727c478bd9Sstevel@tonic-gate 		Slookup		sl;
737c478bd9Sstevel@tonic-gate 
7475e7992aSrie 		/*
7575e7992aSrie 		 * Initialize the symbol lookup data structure.
7675e7992aSrie 		 */
7775e7992aSrie 		SLOOKUP_INIT(sl, 0, lmp, lmp, ld_entry_cnt, 0, 0, 0, 0, 0);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < max; cnt++, dip++) {
807c478bd9Sstevel@tonic-gate 			if (((dip->di_flags & MSK_DI_FILTER) == 0) ||
817c478bd9Sstevel@tonic-gate 			    ((dip->di_flags & FLG_DI_AUXFLTR) &&
827c478bd9Sstevel@tonic-gate 			    (rtld_flags & RT_FL_NOAUXFLTR)))
837c478bd9Sstevel@tonic-gate 				continue;
849aa23310Srie 			(void) elf_lookup_filtee(&sl, 0, 0, cnt, in_nfavl);
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Analyze one or more link-maps of a link map control list.  This routine is
917c478bd9Sstevel@tonic-gate  * called at startup to continue the processing of the main executable.  It is
927c478bd9Sstevel@tonic-gate  * also called each time a new set of objects are loaded, ie. from filters,
937c478bd9Sstevel@tonic-gate  * lazy-loaded objects, or dlopen().
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * In each instance we traverse the link-map control list starting with the
967c478bd9Sstevel@tonic-gate  * initial object.  As dependencies are analyzed they are added to the link-map
977c478bd9Sstevel@tonic-gate  * control list.  Thus the list grows as we traverse it - this results in the
987c478bd9Sstevel@tonic-gate  * breadth first ordering of all needed objects.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate int
1019aa23310Srie analyze_lmc(Lm_list *lml, Aliste nlmco, Rt_map *nlmp, int *in_nfavl)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	Rt_map	*lmp = nlmp;
1047c478bd9Sstevel@tonic-gate 	Lm_cntl	*nlmc;
1057c478bd9Sstevel@tonic-gate 	int	ret = 1;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	/*
1087c478bd9Sstevel@tonic-gate 	 * If this link-map control list is being analyzed, return.  The object
1097c478bd9Sstevel@tonic-gate 	 * that has just been added will be picked up by the existing analysis
1107c478bd9Sstevel@tonic-gate 	 * thread.  Note, this is only really meaningful during process init-
1117c478bd9Sstevel@tonic-gate 	 * ialization, as objects are added to the main link-map control list.
1127c478bd9Sstevel@tonic-gate 	 * Following this initialization, each family of objects that are loaded
1137c478bd9Sstevel@tonic-gate 	 * are added to a new link-map control list.
1147c478bd9Sstevel@tonic-gate 	 */
1157c478bd9Sstevel@tonic-gate 	/* LINTED */
116cce0e03bSab 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
1177c478bd9Sstevel@tonic-gate 	if (nlmc->lc_flags & LMC_FLG_ANALYZING)
1187c478bd9Sstevel@tonic-gate 		return (1);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	/*
1217c478bd9Sstevel@tonic-gate 	 * If this object doesn't belong to the present link-map control list
1227c478bd9Sstevel@tonic-gate 	 * then it must already have been analyzed, or it is in the process of
1237c478bd9Sstevel@tonic-gate 	 * being analyzed prior to us recursing into this analysis.  In either
1247c478bd9Sstevel@tonic-gate 	 * case, ignore the object as it's already being taken care of.
1257c478bd9Sstevel@tonic-gate 	 */
1267c478bd9Sstevel@tonic-gate 	if (nlmco != CNTL(nlmp))
1277c478bd9Sstevel@tonic-gate 		return (1);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	nlmc->lc_flags |= LMC_FLG_ANALYZING;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
1327c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) &
1337c478bd9Sstevel@tonic-gate 		    (FLG_RT_ANALZING | FLG_RT_ANALYZED | FLG_RT_DELETE))
1347c478bd9Sstevel@tonic-gate 			continue;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		/*
1377c478bd9Sstevel@tonic-gate 		 * Indicate that analyzing is under way.
1387c478bd9Sstevel@tonic-gate 		 */
1397c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_ANALZING;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		/*
1427c478bd9Sstevel@tonic-gate 		 * If this link map represents a relocatable object, then we
1437c478bd9Sstevel@tonic-gate 		 * need to finish the link-editing of the object at this point.
1447c478bd9Sstevel@tonic-gate 		 */
1457c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_OBJECT) {
1469aa23310Srie 			if (elf_obj_fini(lml, lmp, in_nfavl) == 0) {
1477c478bd9Sstevel@tonic-gate 				if (lml->lm_flags & LML_FLG_TRC_ENABLE)
1487c478bd9Sstevel@tonic-gate 					continue;
1497c478bd9Sstevel@tonic-gate 				ret = 0;
1507c478bd9Sstevel@tonic-gate 				break;
1517c478bd9Sstevel@tonic-gate 			}
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_analyze(lmp));
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		/*
1577c478bd9Sstevel@tonic-gate 		 * Establish any dependencies this object requires.
1587c478bd9Sstevel@tonic-gate 		 */
1599aa23310Srie 		if (LM_NEEDED(lmp)(lml, nlmco, lmp, in_nfavl) == 0) {
1607c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE)
1617c478bd9Sstevel@tonic-gate 				continue;
1627c478bd9Sstevel@tonic-gate 			ret = 0;
1637c478bd9Sstevel@tonic-gate 			break;
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_ANALZING;
1677c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_ANALYZED;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		/*
1707c478bd9Sstevel@tonic-gate 		 * If we're building a configuration file, determine if this
1717c478bd9Sstevel@tonic-gate 		 * object is a filter and if so load its filtees.  This
1727c478bd9Sstevel@tonic-gate 		 * traversal is only necessary for crle(1), as typical use of
1737c478bd9Sstevel@tonic-gate 		 * an object will load filters as part of relocation processing.
1747c478bd9Sstevel@tonic-gate 		 */
1757c478bd9Sstevel@tonic-gate 		if (MODE(nlmp) & RTLD_CONFGEN)
1769aa23310Srie 			load_filtees(lmp, in_nfavl);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		/*
1797c478bd9Sstevel@tonic-gate 		 * If an interposer has been added, it will have been inserted
1807c478bd9Sstevel@tonic-gate 		 * in the link-map before the link we're presently analyzing.
1817c478bd9Sstevel@tonic-gate 		 * Break out of this analysis loop and return to the head of
1827c478bd9Sstevel@tonic-gate 		 * the link-map control list to analyze the interposer.  Note
1837c478bd9Sstevel@tonic-gate 		 * that this rescan preserves the breadth first loading of
1847c478bd9Sstevel@tonic-gate 		 * dependencies.
1857c478bd9Sstevel@tonic-gate 		 */
18624a6229eSrie 		/* LINTED */
187cce0e03bSab 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
1887c478bd9Sstevel@tonic-gate 		if (nlmc->lc_flags & LMC_FLG_REANALYZE) {
1897c478bd9Sstevel@tonic-gate 			nlmc->lc_flags &= ~LMC_FLG_REANALYZE;
1907c478bd9Sstevel@tonic-gate 			lmp = nlmc->lc_head;
1917c478bd9Sstevel@tonic-gate 		}
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 
19424a6229eSrie 	/* LINTED */
195cce0e03bSab 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
1967c478bd9Sstevel@tonic-gate 	nlmc->lc_flags &= ~LMC_FLG_ANALYZING;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	return (ret);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
202466e2a62Srie  * Determine whether a symbol represents zero, .bss, bits.  Most commonly this
203466e2a62Srie  * function is used to determine whether the data for a copy relocation refers
204466e2a62Srie  * to initialized data or .bss.  If the data definition is within .bss, then the
205466e2a62Srie  * data is zero filled, and as the copy destination within the executable is
206466e2a62Srie  * .bss, we can skip copying zero's to zero's.
207466e2a62Srie  *
208466e2a62Srie  * However, if the defining object has MOVE data, it's .bss might contain
209466e2a62Srie  * non-zero data, in which case copy the definition regardless.
210466e2a62Srie  *
211466e2a62Srie  * For backward compatibility copy relocation processing, this routine can be
212466e2a62Srie  * used to determine precisely if a copy destination is a move record recipient.
2137c478bd9Sstevel@tonic-gate  */
2147c478bd9Sstevel@tonic-gate static int
215466e2a62Srie are_bits_zero(Rt_map *dlmp, Sym *dsym, int dest)
2167c478bd9Sstevel@tonic-gate {
217466e2a62Srie 	Mmap	*mmap = NULL, *mmaps;
218466e2a62Srie 	caddr_t	daddr = (caddr_t)dsym->st_value;
2197c478bd9Sstevel@tonic-gate 
220466e2a62Srie 	if ((FLAGS(dlmp) & FLG_RT_FIXED) == 0)
221466e2a62Srie 		daddr += ADDR(dlmp);
2227c478bd9Sstevel@tonic-gate 
223466e2a62Srie 	/*
224466e2a62Srie 	 * Determine the segment that contains the copy definition.  Given that
225466e2a62Srie 	 * the copy relocation records have already been captured and verified,
226466e2a62Srie 	 * a segment must be found (but we add an escape clause never the less).
227466e2a62Srie 	 */
228466e2a62Srie 	for (mmaps = MMAPS(dlmp); mmaps->m_vaddr; mmaps++) {
229466e2a62Srie 		if ((daddr >= mmaps->m_vaddr) &&
230466e2a62Srie 		    (daddr < (mmaps->m_vaddr + mmaps->m_msize))) {
231466e2a62Srie 			mmap = mmaps;
232466e2a62Srie 			break;
2337c478bd9Sstevel@tonic-gate 		}
2347c478bd9Sstevel@tonic-gate 	}
235466e2a62Srie 	if (mmap == NULL)
236466e2a62Srie 		return (1);
237466e2a62Srie 
238466e2a62Srie 	/*
239466e2a62Srie 	 * If the definition is not within .bss, indicate this is not zero data.
240466e2a62Srie 	 */
241466e2a62Srie 	if (daddr < (mmap->m_vaddr + mmaps->m_fsize))
242466e2a62Srie 		return (0);
243466e2a62Srie 
244466e2a62Srie 	/*
245466e2a62Srie 	 * If the definition is within .bss, make sure the definition isn't the
246466e2a62Srie 	 * recipient of a move record.  Note, we don't precisely analyze whether
247466e2a62Srie 	 * the address is a move record recipient, as the infrastructure to
248466e2a62Srie 	 * prepare for, and carry out this analysis, is probably more costly
249466e2a62Srie 	 * than just copying the bytes regardless.
250466e2a62Srie 	 */
251466e2a62Srie 	if ((FLAGS(dlmp) & FLG_RT_MOVE) == 0)
252466e2a62Srie 		return (1);
253466e2a62Srie 
254466e2a62Srie 	/*
255466e2a62Srie 	 * However, for backward compatibility copy relocation processing, we
256466e2a62Srie 	 * can afford to work a little harder.  Here, determine precisely
257466e2a62Srie 	 * whether the destination in the executable is a move record recipient.
258466e2a62Srie 	 * See comments in lookup_sym_interpose(), below.
259466e2a62Srie 	 */
260466e2a62Srie 	if (dest && is_move_data(daddr))
261466e2a62Srie 		return (0);
262466e2a62Srie 
263466e2a62Srie 	return (1);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Relocate an individual object.
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate static int
2709aa23310Srie relocate_so(Lm_list *lml, Rt_map *lmp, int *relocated, int now, int *in_nfavl)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * If we're running under ldd(1), and haven't been asked to trace any
2747c478bd9Sstevel@tonic-gate 	 * warnings, skip any actual relocation processing.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
2777c478bd9Sstevel@tonic-gate 	    (lml->lm_flags & LML_FLG_TRC_WARN)) {
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 		if (relocated)
2807c478bd9Sstevel@tonic-gate 			(*relocated)++;
2817c478bd9Sstevel@tonic-gate 
2829aa23310Srie 		if ((LM_RELOC(lmp)(lmp, now, in_nfavl) == 0) &&
2837c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0))
2847c478bd9Sstevel@tonic-gate 			return (0);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	return (1);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Relocate the objects on a link-map control list.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate static int
2939aa23310Srie _relocate_lmc(Lm_list *lml, Rt_map *nlmp, int *relocated, int *in_nfavl)
2947c478bd9Sstevel@tonic-gate {
2957c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	for (lmp = nlmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
2987c478bd9Sstevel@tonic-gate 		/*
2997c478bd9Sstevel@tonic-gate 		 * If this object has already been relocated, we're done.  If
3007c478bd9Sstevel@tonic-gate 		 * this object is being deleted, skip it, there's probably a
3017c478bd9Sstevel@tonic-gate 		 * relocation error somewhere that's causing this deletion.
3027c478bd9Sstevel@tonic-gate 		 */
3037c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) &
3047c478bd9Sstevel@tonic-gate 		    (FLG_RT_RELOCING | FLG_RT_RELOCED | FLG_RT_DELETE))
3057c478bd9Sstevel@tonic-gate 			continue;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		/*
3087c478bd9Sstevel@tonic-gate 		 * Indicate that relocation processing is under way.
3097c478bd9Sstevel@tonic-gate 		 */
3107c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_RELOCING;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		/*
3137c478bd9Sstevel@tonic-gate 		 * Relocate the object.
3147c478bd9Sstevel@tonic-gate 		 */
3159aa23310Srie 		if (relocate_so(lml, lmp, relocated, 0, in_nfavl) == 0)
3167c478bd9Sstevel@tonic-gate 			return (0);
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		/*
3197c478bd9Sstevel@tonic-gate 		 * Indicate that the objects relocation is complete.
3207c478bd9Sstevel@tonic-gate 		 */
3217c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_RELOCING;
3227c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_RELOCED;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		/*
3257c478bd9Sstevel@tonic-gate 		 * Mark this object's init is available for harvesting.  Under
3267c478bd9Sstevel@tonic-gate 		 * ldd(1) this marking is necessary for -i (tsort) gathering.
3277c478bd9Sstevel@tonic-gate 		 */
3287c478bd9Sstevel@tonic-gate 		lml->lm_init++;
329dffec89cSrie 		lml->lm_flags |= LML_FLG_OBJADDED;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		/*
332466e2a62Srie 		 * Process any move data.  Note, this is carried out under ldd
333466e2a62Srie 		 * under relocation processing too, as it can flush out move
334466e2a62Srie 		 * errors, and enables lari(1) to provide a true representation
335466e2a62Srie 		 * of the runtime bindings.
3367c478bd9Sstevel@tonic-gate 		 */
33710a4fa49Srie 		if ((FLAGS(lmp) & FLG_RT_MOVE) &&
338466e2a62Srie 		    (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
339466e2a62Srie 		    (lml->lm_flags & LML_FLG_TRC_WARN))) {
340466e2a62Srie 			if (move_data(lmp) == 0)
341466e2a62Srie 				return (0);
342466e2a62Srie 		}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		/*
3457c478bd9Sstevel@tonic-gate 		 * Determine if this object is a filter, and if a load filter
3467c478bd9Sstevel@tonic-gate 		 * flag is in effect, trigger the loading of all its filtees.
3477c478bd9Sstevel@tonic-gate 		 */
3489aa23310Srie 		load_filtees(lmp, in_nfavl);
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	/*
3527c478bd9Sstevel@tonic-gate 	 * Perform special copy relocations.  These are only meaningful for
3537c478bd9Sstevel@tonic-gate 	 * dynamic executables (fixed and head of their link-map list).  If
3547c478bd9Sstevel@tonic-gate 	 * this ever has to change then the infrastructure of COPY() has to
355cce0e03bSab 	 * change. Presently, a given link map can only have a receiver or
356cce0e03bSab 	 * supplier of copy data, so a union is used to overlap the storage
357cce0e03bSab 	 * for the COPY_R() and COPY_S() lists. These lists would need to
358cce0e03bSab 	 * be separated.
3597c478bd9Sstevel@tonic-gate 	 */
3607c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) & FLG_RT_FIXED) && (nlmp == LIST(nlmp)->lm_head) &&
3617c478bd9Sstevel@tonic-gate 	    (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
3627c478bd9Sstevel@tonic-gate 	    (lml->lm_flags & LML_FLG_TRC_WARN))) {
363cce0e03bSab 		Rt_map		*lmp;
364cce0e03bSab 		Aliste		idx1;
3657c478bd9Sstevel@tonic-gate 		Word		tracing;
3667c478bd9Sstevel@tonic-gate 
36702ca3e02Srie #if	defined(__i386)
3687c478bd9Sstevel@tonic-gate 		if (elf_copy_gen(nlmp) == 0)
3697c478bd9Sstevel@tonic-gate 			return (0);
3707c478bd9Sstevel@tonic-gate #endif
371cce0e03bSab 		if (COPY_S(nlmp) == NULL)
3727c478bd9Sstevel@tonic-gate 			return (1);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		if ((LIST(nlmp)->lm_flags & LML_FLG_TRC_ENABLE) &&
3757c478bd9Sstevel@tonic-gate 		    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
3767c478bd9Sstevel@tonic-gate 		    (LIST(nlmp)->lm_flags & LML_FLG_TRC_VERBOSE)))
3777c478bd9Sstevel@tonic-gate 			tracing = 1;
3787c478bd9Sstevel@tonic-gate 		else
3797c478bd9Sstevel@tonic-gate 			tracing = 0;
3807c478bd9Sstevel@tonic-gate 
3815aefb655Srie 		DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
3827c478bd9Sstevel@tonic-gate 
383cce0e03bSab 		for (APLIST_TRAVERSE(COPY_S(nlmp), idx1, lmp)) {
384aa736cbeSrie 			Rel_copy	*rcp;
385cce0e03bSab 			Aliste		idx2;
3867c478bd9Sstevel@tonic-gate 
387cce0e03bSab 			for (ALIST_TRAVERSE(COPY_R(lmp), idx2, rcp)) {
3887c478bd9Sstevel@tonic-gate 				int zero;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 				/*
391466e2a62Srie 				 * Only copy the data if the data is from
392466e2a62Srie 				 * a non-zero definition (ie. not .bss).
3937c478bd9Sstevel@tonic-gate 				 */
394466e2a62Srie 				zero = are_bits_zero(rcp->r_dlmp,
395466e2a62Srie 				    rcp->r_dsym, 0);
3965aefb655Srie 				DBG_CALL(Dbg_reloc_copy(rcp->r_dlmp, nlmp,
3975aefb655Srie 				    rcp->r_name, zero));
3987c478bd9Sstevel@tonic-gate 				if (zero)
3997c478bd9Sstevel@tonic-gate 					continue;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 				(void) memcpy(rcp->r_radd, rcp->r_dadd,
4027c478bd9Sstevel@tonic-gate 				    rcp->r_size);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 				if ((tracing == 0) || ((FLAGS1(rcp->r_dlmp) &
4057c478bd9Sstevel@tonic-gate 				    FL1_RT_DISPREL) == 0))
4067c478bd9Sstevel@tonic-gate 					continue;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_REL_CPYDISP),
4097c478bd9Sstevel@tonic-gate 				    demangle(rcp->r_name), NAME(rcp->r_dlmp));
4107c478bd9Sstevel@tonic-gate 			}
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 
4135aefb655Srie 		DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
4147c478bd9Sstevel@tonic-gate 
415cce0e03bSab 		free(COPY_S(nlmp));
416cce0e03bSab 		COPY_S(nlmp) = 0;
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	return (1);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate int
4229aa23310Srie relocate_lmc(Lm_list *lml, Aliste nlmco, Rt_map *clmp, Rt_map *nlmp,
4239aa23310Srie     int *in_nfavl)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	int	lret = 1, pret = 1;
426cce0e03bSab 	APlist	*alp;
4277c478bd9Sstevel@tonic-gate 	Aliste	plmco;
4287c478bd9Sstevel@tonic-gate 	Lm_cntl	*plmc, *nlmc;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	/*
4317c478bd9Sstevel@tonic-gate 	 * If this link-map control list is being relocated, return.  The object
4327c478bd9Sstevel@tonic-gate 	 * that has just been added will be picked up by the existing relocation
4337c478bd9Sstevel@tonic-gate 	 * thread.  Note, this is only really meaningful during process init-
4347c478bd9Sstevel@tonic-gate 	 * ialization, as objects are added to the main link-map control list.
4357c478bd9Sstevel@tonic-gate 	 * Following this initialization, each family of objects that are loaded
4367c478bd9Sstevel@tonic-gate 	 * are added to a new link-map control list.
4377c478bd9Sstevel@tonic-gate 	 */
4387c478bd9Sstevel@tonic-gate 	/* LINTED */
439cce0e03bSab 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (nlmc->lc_flags & LMC_FLG_RELOCATING)
4427c478bd9Sstevel@tonic-gate 		return (1);
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	nlmc->lc_flags |= LMC_FLG_RELOCATING;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/*
4477c478bd9Sstevel@tonic-gate 	 * Relocate one or more link-maps of a link map control list.  If this
4487c478bd9Sstevel@tonic-gate 	 * object doesn't belong to the present link-map control list then it
4497c478bd9Sstevel@tonic-gate 	 * must already have been relocated, or it is in the process of being
4507c478bd9Sstevel@tonic-gate 	 * relocated prior to us recursing into this relocation.  In either
4517c478bd9Sstevel@tonic-gate 	 * case, ignore the object as it's already being taken care of, however,
4527c478bd9Sstevel@tonic-gate 	 * fall through and capture any relocation promotions that might have
4537c478bd9Sstevel@tonic-gate 	 * been established from the reference mode of this object.
4547c478bd9Sstevel@tonic-gate 	 *
4557c478bd9Sstevel@tonic-gate 	 * If we're generating a configuration file using crle(1), two passes
4567c478bd9Sstevel@tonic-gate 	 * may be involved.  Under the first pass, RTLD_CONFGEN is set.  Under
4577c478bd9Sstevel@tonic-gate 	 * this pass, crle() loads objects into the process address space.  No
4587c478bd9Sstevel@tonic-gate 	 * relocation is necessary at this point, we simply need to analyze the
4597c478bd9Sstevel@tonic-gate 	 * objects to insure any directly bound dependencies, filtees, etc.
4607c478bd9Sstevel@tonic-gate 	 * get loaded. Although we skip the relocation, fall through to insure
4617c478bd9Sstevel@tonic-gate 	 * any control lists are maintained appropriately.
4627c478bd9Sstevel@tonic-gate 	 *
4637c478bd9Sstevel@tonic-gate 	 * If objects are to be dldump(3c)'ed, crle(1) makes a second pass,
4647c478bd9Sstevel@tonic-gate 	 * using RTLD_NOW and RTLD_CONFGEN.  The RTLD_NOW effectively carries
4657c478bd9Sstevel@tonic-gate 	 * out the relocations of all loaded objects.
4667c478bd9Sstevel@tonic-gate 	 */
4677c478bd9Sstevel@tonic-gate 	if ((nlmco == CNTL(nlmp)) &&
4687c478bd9Sstevel@tonic-gate 	    ((MODE(nlmp) & (RTLD_NOW | RTLD_CONFGEN)) != RTLD_CONFGEN)) {
4697c478bd9Sstevel@tonic-gate 		int	relocated = 0;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 		/*
4727c478bd9Sstevel@tonic-gate 		 * Determine whether the initial link-map control list has
4737c478bd9Sstevel@tonic-gate 		 * started relocation.  From this point, should any interposing
4747c478bd9Sstevel@tonic-gate 		 * objects be added to this link-map control list, the objects
4757c478bd9Sstevel@tonic-gate 		 * are demoted to standard objects.  Their interposition can't
4767c478bd9Sstevel@tonic-gate 		 * be guaranteed once relocations have been carried out.
4777c478bd9Sstevel@tonic-gate 		 */
478cce0e03bSab 		if (nlmco == ALIST_OFF_DATA)
4797c478bd9Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_STARTREL;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 		/*
48202ca3e02Srie 		 * Relocate the link-map control list.  Should this relocation
48302ca3e02Srie 		 * fail, clean up this link-map list.  Relocations within this
48402ca3e02Srie 		 * list may have required relocation promotions on other lists,
48502ca3e02Srie 		 * so before acting upon these, and possibly adding more objects
48602ca3e02Srie 		 * to the present link-map control list, try and clean up any
48702ca3e02Srie 		 * failed objects now.
4887c478bd9Sstevel@tonic-gate 		 */
4899aa23310Srie 		lret = _relocate_lmc(lml, nlmp, &relocated, in_nfavl);
490cce0e03bSab 		if ((lret == 0) && (nlmco != ALIST_OFF_DATA))
49102ca3e02Srie 			remove_lmc(lml, clmp, nlmc, nlmco, NAME(nlmp));
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/*
4957c478bd9Sstevel@tonic-gate 	 * Determine the new, and previous link-map control lists.
4967c478bd9Sstevel@tonic-gate 	 */
49724a6229eSrie 	/* LINTED */
498cce0e03bSab 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
499cce0e03bSab 	if (nlmco == ALIST_OFF_DATA) {
5006679fdc0Srie 		plmco = nlmco;
5017c478bd9Sstevel@tonic-gate 		plmc = nlmc;
5026679fdc0Srie 	} else {
5037c478bd9Sstevel@tonic-gate 		plmco = nlmco - lml->lm_lists->al_size;
5047c478bd9Sstevel@tonic-gate 		/* LINTED */
505cce0e03bSab 		plmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, plmco);
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	/*
5097c478bd9Sstevel@tonic-gate 	 * Having completed this control list of objects, they can now be bound
5107c478bd9Sstevel@tonic-gate 	 * to from other objects.  Move this control list to the control list
5117c478bd9Sstevel@tonic-gate 	 * that precedes it.  Although this control list may have only bound to
5127c478bd9Sstevel@tonic-gate 	 * controls lists much higher up the control list stack, it must only
5137c478bd9Sstevel@tonic-gate 	 * be moved up one control list so as to preserve the link-map order
5147c478bd9Sstevel@tonic-gate 	 * that may have already been traversed in search of symbols.
5157c478bd9Sstevel@tonic-gate 	 */
516cce0e03bSab 	if (lret && (nlmco != ALIST_OFF_DATA) && nlmc->lc_head)
5177c478bd9Sstevel@tonic-gate 		lm_move(lml, nlmco, plmco, nlmc, plmc);
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	/*
5207c478bd9Sstevel@tonic-gate 	 * Determine whether existing objects that have already been relocated,
5217c478bd9Sstevel@tonic-gate 	 * need any additional relocations performed.  This can occur when new
5227c478bd9Sstevel@tonic-gate 	 * objects are loaded with RTLD_NOW, and these new objects have
5237c478bd9Sstevel@tonic-gate 	 * dependencies on objects that are already loaded.  Note, that we peel
5247c478bd9Sstevel@tonic-gate 	 * any relocation promotions off of one control list at a time.  This
5257c478bd9Sstevel@tonic-gate 	 * prevents relocations from being bound to objects that might yet fail
5267c478bd9Sstevel@tonic-gate 	 * to relocate themselves.
5277c478bd9Sstevel@tonic-gate 	 */
528cce0e03bSab 	while ((alp = plmc->lc_now) != NULL) {
529cce0e03bSab 		Aliste	idx;
530cce0e03bSab 		Rt_map	*lmp;
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		/*
5337c478bd9Sstevel@tonic-gate 		 * Remove the relocation promotion list, as performing more
5347c478bd9Sstevel@tonic-gate 		 * relocations may result in discovering more objects that need
5357c478bd9Sstevel@tonic-gate 		 * promotion.
5367c478bd9Sstevel@tonic-gate 		 */
537cce0e03bSab 		plmc->lc_now = NULL;
5387c478bd9Sstevel@tonic-gate 
539cce0e03bSab 		for (APLIST_TRAVERSE(alp, idx, lmp)) {
5407c478bd9Sstevel@tonic-gate 			/*
5417c478bd9Sstevel@tonic-gate 			 * If the original relocation of the link-map control
5427c478bd9Sstevel@tonic-gate 			 * list failed, or one of the relocation promotions of
5437c478bd9Sstevel@tonic-gate 			 * this loop has failed, demote any pending objects
5447c478bd9Sstevel@tonic-gate 			 * relocation mode.
5457c478bd9Sstevel@tonic-gate 			 */
5467c478bd9Sstevel@tonic-gate 			if ((lret == 0) || (pret == 0)) {
5477c478bd9Sstevel@tonic-gate 				MODE(lmp) &= ~RTLD_NOW;
5487c478bd9Sstevel@tonic-gate 				MODE(lmp) |= RTLD_LAZY;
5497c478bd9Sstevel@tonic-gate 				continue;
5507c478bd9Sstevel@tonic-gate 			}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 			/*
5537c478bd9Sstevel@tonic-gate 			 * If a relocation fails, save the error condition.
5547c478bd9Sstevel@tonic-gate 			 * It's possible that all new objects on the original
5557c478bd9Sstevel@tonic-gate 			 * link-map control list have been relocated
5567c478bd9Sstevel@tonic-gate 			 * successfully, but if the user request requires
5577c478bd9Sstevel@tonic-gate 			 * promoting objects that have already been loaded, we
5587c478bd9Sstevel@tonic-gate 			 * have to indicate that this operation couldn't be
5597c478bd9Sstevel@tonic-gate 			 * performed.  The unrelocated objects are in use on
5607c478bd9Sstevel@tonic-gate 			 * another control list, and may continue to be used.
5617c478bd9Sstevel@tonic-gate 			 * If the .plt that resulted in the error is called,
5627c478bd9Sstevel@tonic-gate 			 * then the process will receive a fatal error at that
5637c478bd9Sstevel@tonic-gate 			 * time.  But, the .plt may never be called.
5647c478bd9Sstevel@tonic-gate 			 */
5659aa23310Srie 			if (relocate_so(lml, lmp, 0, 1, in_nfavl) == 0)
5667c478bd9Sstevel@tonic-gate 				pret = 0;
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 		/*
5707c478bd9Sstevel@tonic-gate 		 * Having promoted any objects, determine whether additional
5717c478bd9Sstevel@tonic-gate 		 * dependencies were added, and if so move them to the previous
5727c478bd9Sstevel@tonic-gate 		 * link-map control list.
5737c478bd9Sstevel@tonic-gate 		 */
57424a6229eSrie 		/* LINTED */
575cce0e03bSab 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
57624a6229eSrie 		/* LINTED */
577cce0e03bSab 		plmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, plmco);
578cce0e03bSab 		if ((nlmco != ALIST_OFF_DATA) && nlmc->lc_head)
5797c478bd9Sstevel@tonic-gate 			lm_move(lml, nlmco, plmco, nlmc, plmc);
5808521e5e6Srie 		free(alp);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
58324a6229eSrie 	/*
58402ca3e02Srie 	 * If relocations have been successful, indicate that relocations are
58502ca3e02Srie 	 * no longer active for this control list.  Otherwise, leave the
58602ca3e02Srie 	 * relocation flag, as this flag is used to determine the style of
58702ca3e02Srie 	 * cleanup (see remove_lmc()).
58824a6229eSrie 	 */
58902ca3e02Srie 	if (lret && pret) {
59002ca3e02Srie 		/* LINTED */
591cce0e03bSab 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
59202ca3e02Srie 		nlmc->lc_flags &= ~LMC_FLG_RELOCATING;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		return (1);
59502ca3e02Srie 	}
59602ca3e02Srie 
59702ca3e02Srie 	return (0);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate /*
6017c478bd9Sstevel@tonic-gate  * Inherit the first rejection message for possible later diagnostics.
6027c478bd9Sstevel@tonic-gate  *
6037c478bd9Sstevel@tonic-gate  * Any attempt to process a file that is unsuccessful, should be accompanied
6047c478bd9Sstevel@tonic-gate  * with an error diagnostic.  However, some operations like searching for a
6057c478bd9Sstevel@tonic-gate  * simple filename, involve trying numerous paths, and an error message for each
6067c478bd9Sstevel@tonic-gate  * lookup is not required.  Although a multiple search can fail, it's possible
6077c478bd9Sstevel@tonic-gate  * that a file was found, but was rejected because it was the wrong type.
6087c478bd9Sstevel@tonic-gate  * To satisfy these possibilities, the first failure is recorded as a rejection
6097c478bd9Sstevel@tonic-gate  * message, and this message is used later for a more specific diagnostic.
6107c478bd9Sstevel@tonic-gate  *
6117c478bd9Sstevel@tonic-gate  * File searches are focused at load_one(), and from here a rejection descriptor
6127c478bd9Sstevel@tonic-gate  * is passed down to various child routines.  If these child routines can
6137c478bd9Sstevel@tonic-gate  * process multiple files, then they will maintain their own rejection desc-
6147c478bd9Sstevel@tonic-gate  * riptor.  This is filled in for any failures, and a diagnostic produced to
6157c478bd9Sstevel@tonic-gate  * reflect the failure.  The child routines then employ rejection_inherit() to
6167c478bd9Sstevel@tonic-gate  * pass the first rejection message back to load_one().
6177c478bd9Sstevel@tonic-gate  *
6187c478bd9Sstevel@tonic-gate  * Note that the name, and rejection string must be duplicated, as the name
6197c478bd9Sstevel@tonic-gate  * buffer and error string buffer (see conv_ routines) may be reused for
6207c478bd9Sstevel@tonic-gate  * additional processing or rejection messages.
6217c478bd9Sstevel@tonic-gate  */
6227c478bd9Sstevel@tonic-gate void
62331fdd7caSab rejection_inherit(Rej_desc *rej1, Rej_desc *rej2)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	if (rej2->rej_type && (rej1->rej_type == 0)) {
6267c478bd9Sstevel@tonic-gate 		rej1->rej_type = rej2->rej_type;
6277c478bd9Sstevel@tonic-gate 		rej1->rej_info = rej2->rej_info;
6287c478bd9Sstevel@tonic-gate 		rej1->rej_flag = rej2->rej_flag;
6297c478bd9Sstevel@tonic-gate 		if (rej2->rej_name)
6307c478bd9Sstevel@tonic-gate 			rej1->rej_name = strdup(rej2->rej_name);
6317c478bd9Sstevel@tonic-gate 		if (rej2->rej_str) {
6328521e5e6Srie 			if ((rej1->rej_str = strdup(rej2->rej_str)) == NULL)
6337c478bd9Sstevel@tonic-gate 				rej1->rej_str = MSG_ORIG(MSG_EMG_ENOMEM);
6347c478bd9Sstevel@tonic-gate 		}
6357c478bd9Sstevel@tonic-gate 	}
6367c478bd9Sstevel@tonic-gate }
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate /*
6397c478bd9Sstevel@tonic-gate  * Determine the object type of a file.
6407c478bd9Sstevel@tonic-gate  */
6417c478bd9Sstevel@tonic-gate Fct *
6427c478bd9Sstevel@tonic-gate are_u_this(Rej_desc *rej, int fd, struct stat *status, const char *name)
6437c478bd9Sstevel@tonic-gate {
6447c478bd9Sstevel@tonic-gate 	int	i;
6457c478bd9Sstevel@tonic-gate 	char	*maddr = 0;
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	fmap->fm_fsize = status->st_size;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	/*
6507c478bd9Sstevel@tonic-gate 	 * If this is a directory (which can't be mmap()'ed) generate a precise
6517c478bd9Sstevel@tonic-gate 	 * error message.
6527c478bd9Sstevel@tonic-gate 	 */
6537c478bd9Sstevel@tonic-gate 	if ((status->st_mode & S_IFMT) == S_IFDIR) {
6547c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
6557c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(EISDIR);
6567c478bd9Sstevel@tonic-gate 		return (0);
6577c478bd9Sstevel@tonic-gate 	}
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 	/*
6607c478bd9Sstevel@tonic-gate 	 * Map in the first page of the file.  When this buffer is first used,
6617c478bd9Sstevel@tonic-gate 	 * the mapping is a single system page.  This is typically enough to
6627c478bd9Sstevel@tonic-gate 	 * inspect the ehdr and phdrs of the file, and can be reused for each
6637c478bd9Sstevel@tonic-gate 	 * file that get loaded.  If a larger mapping is required to read the
6647c478bd9Sstevel@tonic-gate 	 * ehdr and phdrs, a new mapping is created (see elf_map_it()).  This
6657c478bd9Sstevel@tonic-gate 	 * new mapping is again used for each new file loaded.  Some objects,
6667c478bd9Sstevel@tonic-gate 	 * such as filters, only take up one page, and in this case this mapping
6677c478bd9Sstevel@tonic-gate 	 * will suffice for the file.
6687c478bd9Sstevel@tonic-gate 	 */
6697c478bd9Sstevel@tonic-gate 	maddr = mmap(fmap->fm_maddr, fmap->fm_msize, (PROT_READ | PROT_EXEC),
6707c478bd9Sstevel@tonic-gate 	    fmap->fm_mflags, fd, 0);
6717c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
6727c478bd9Sstevel@tonic-gate 	if ((maddr == MAP_FAILED) && (errno == EINVAL)) {
6737c478bd9Sstevel@tonic-gate 		/*
6747c478bd9Sstevel@tonic-gate 		 * If the mapping failed, and we used MAP_ALIGN, assume we're
6757c478bd9Sstevel@tonic-gate 		 * on a system that doesn't support this option.  Try again
6767c478bd9Sstevel@tonic-gate 		 * without MAP_ALIGN.
6777c478bd9Sstevel@tonic-gate 		 */
6787c478bd9Sstevel@tonic-gate 		if (fmap->fm_mflags & MAP_ALIGN) {
6797c478bd9Sstevel@tonic-gate 			rtld_flags2 |= RT_FL2_NOMALIGN;
6807c478bd9Sstevel@tonic-gate 			fmap_setup();
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			maddr = (char *)mmap(fmap->fm_maddr, fmap->fm_msize,
6837c478bd9Sstevel@tonic-gate 			    (PROT_READ | PROT_EXEC), fmap->fm_mflags, fd, 0);
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate #endif
6877c478bd9Sstevel@tonic-gate 	if (maddr == MAP_FAILED) {
6887c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
6897c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(errno);
6907c478bd9Sstevel@tonic-gate 		return (0);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/*
6947c478bd9Sstevel@tonic-gate 	 * From now on we will re-use fmap->fm_maddr as the mapping address
6957c478bd9Sstevel@tonic-gate 	 * so we augment the flags with MAP_FIXED and drop any MAP_ALIGN.
6967c478bd9Sstevel@tonic-gate 	 */
6977c478bd9Sstevel@tonic-gate 	fmap->fm_maddr = maddr;
6987c478bd9Sstevel@tonic-gate 	fmap->fm_mflags |= MAP_FIXED;
6997c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
7007c478bd9Sstevel@tonic-gate 	fmap->fm_mflags &= ~MAP_ALIGN;
7017c478bd9Sstevel@tonic-gate #endif
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * Search through the object vectors to determine what kind of
7057c478bd9Sstevel@tonic-gate 	 * object we have.
7067c478bd9Sstevel@tonic-gate 	 */
7077c478bd9Sstevel@tonic-gate 	for (i = 0; vector[i]; i++) {
7087c478bd9Sstevel@tonic-gate 		if ((vector[i]->fct_are_u_this)(rej))
7097c478bd9Sstevel@tonic-gate 			return (vector[i]);
7107c478bd9Sstevel@tonic-gate 		else if (rej->rej_type) {
7117c478bd9Sstevel@tonic-gate 			Rt_map	*lmp;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 			/*
7147c478bd9Sstevel@tonic-gate 			 * If this object is an explicitly defined shared
7157c478bd9Sstevel@tonic-gate 			 * object under inspection by ldd, and contains a
7167c478bd9Sstevel@tonic-gate 			 * incompatible hardware capabilities requirement, then
7177c478bd9Sstevel@tonic-gate 			 * inform the user, but continue processing.
7187c478bd9Sstevel@tonic-gate 			 *
7197c478bd9Sstevel@tonic-gate 			 * XXXX - ldd -v for any rej failure.
7207c478bd9Sstevel@tonic-gate 			 */
7217c478bd9Sstevel@tonic-gate 			if ((rej->rej_type == SGS_REJ_HWCAP_1) &&
7227c478bd9Sstevel@tonic-gate 			    (lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) &&
7237c478bd9Sstevel@tonic-gate 			    ((lmp = lml_main.lm_head) != 0) &&
7247c478bd9Sstevel@tonic-gate 			    (FLAGS1(lmp) & FL1_RT_LDDSTUB) &&
7257c478bd9Sstevel@tonic-gate 			    (NEXT(lmp) == 0)) {
7267c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1),
7277c478bd9Sstevel@tonic-gate 				    name, rej->rej_str);
7287c478bd9Sstevel@tonic-gate 				return (vector[i]);
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 			return (0);
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * Unknown file type.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	rej->rej_type = SGS_REJ_UNKFILE;
7387c478bd9Sstevel@tonic-gate 	return (0);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7420aa3cd4dSrie  * Helper routine for is_so_matched() that consolidates matching a path name,
7430aa3cd4dSrie  * or file name component of a link-map name.
7447c478bd9Sstevel@tonic-gate  */
7457c478bd9Sstevel@tonic-gate static int
7460aa3cd4dSrie _is_so_matched(const char *name, const char *str, int path)
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	const char	*_str;
7497c478bd9Sstevel@tonic-gate 
7500aa3cd4dSrie 	if ((path == 0) && ((_str = strrchr(str, '/')) != NULL))
7517c478bd9Sstevel@tonic-gate 		_str++;
7527c478bd9Sstevel@tonic-gate 	else
7537c478bd9Sstevel@tonic-gate 		_str = str;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	return (strcmp(name, _str));
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7580aa3cd4dSrie /*
7590aa3cd4dSrie  * Determine whether a search name matches one of the names associated with a
7600aa3cd4dSrie  * link-map.  A link-map contains several names:
7610aa3cd4dSrie  *
7620aa3cd4dSrie  *  .	a NAME() - typically the full pathname of an object that has been
7630aa3cd4dSrie  *	loaded.  For example, when looking for the dependency "libc.so.1", a
764aa736cbeSrie  *	search path is applied, with the eventual NAME() being "/lib/ld.so.1".
7650aa3cd4dSrie  *	The name of the executable is typically a simple filename, such as
7660aa3cd4dSrie  *	"main", as this is the name passed to exec() to start the process.
7670aa3cd4dSrie  *
7680aa3cd4dSrie  *  .	a PATHNAME() - this is maintained if the resolved NAME() is different
7690aa3cd4dSrie  * 	to NAME(), ie. the original name is a symbolic link.  This is also
7700aa3cd4dSrie  * 	the resolved full pathname for a dynamic executable.
7710aa3cd4dSrie  *
7720aa3cd4dSrie  *  .	a list of ALIAS() names - these are alternative names by which the
7730aa3cd4dSrie  *	object has been found, ie. when dependencies are loaded through a
7740aa3cd4dSrie  * 	variety of different symbolic links.
7750aa3cd4dSrie  *
7760aa3cd4dSrie  * The name pattern matching can differ depending on whether we are looking
7770aa3cd4dSrie  * for a full path name (path != 0), or a simple file name (path == 0).  Full
7780aa3cd4dSrie  * path names typically match NAME() or PATHNAME() entries, so these link-map
7790aa3cd4dSrie  * names are inspected first when a full path name is being searched for.
7800aa3cd4dSrie  * Simple file names typically match ALIAS() names, so these link-map names are
7810aa3cd4dSrie  * inspected first when a simple file name is being searched for.
7820aa3cd4dSrie  *
7830aa3cd4dSrie  * For all full path name searches, the link-map names are taken as is.  For
7840aa3cd4dSrie  * simple file name searches, only the file name component of any link-map
7850aa3cd4dSrie  * names are used for comparison.
7860aa3cd4dSrie  */
7877c478bd9Sstevel@tonic-gate static Rt_map *
7880aa3cd4dSrie is_so_matched(Rt_map *lmp, const char *name, int path)
7897c478bd9Sstevel@tonic-gate {
790cce0e03bSab 	Aliste		idx;
791cce0e03bSab 	const char	*cp;
7927c478bd9Sstevel@tonic-gate 
7930aa3cd4dSrie 	/*
7940aa3cd4dSrie 	 * A pathname is typically going to match a NAME() or PATHNAME(), so
7950aa3cd4dSrie 	 * check these first.
7960aa3cd4dSrie 	 */
7970aa3cd4dSrie 	if (path) {
7980aa3cd4dSrie 		if (strcmp(name, NAME(lmp)) == 0)
7990aa3cd4dSrie 			return (lmp);
8000aa3cd4dSrie 
8010aa3cd4dSrie 		if (PATHNAME(lmp) != NAME(lmp)) {
8020aa3cd4dSrie 			if (strcmp(name, PATHNAME(lmp)) == 0)
8030aa3cd4dSrie 				return (lmp);
8040aa3cd4dSrie 		}
8050aa3cd4dSrie 	}
8060aa3cd4dSrie 
8077c478bd9Sstevel@tonic-gate 	/*
8087c478bd9Sstevel@tonic-gate 	 * Typically, dependencies are specified as simple file names
8097c478bd9Sstevel@tonic-gate 	 * (DT_NEEDED == libc.so.1), which are expanded to full pathnames to
8107c478bd9Sstevel@tonic-gate 	 * open the file.  The full pathname is NAME(), and the original name
8110aa3cd4dSrie 	 * is maintained on the ALIAS() list.
8120aa3cd4dSrie 	 *
8130aa3cd4dSrie 	 * If this is a simple filename, or a pathname has failed to match the
8140aa3cd4dSrie 	 * NAME() and PATHNAME() check above, look through the ALIAS() list.
8157c478bd9Sstevel@tonic-gate 	 */
816cce0e03bSab 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
8170aa3cd4dSrie 		/*
8180aa3cd4dSrie 		 * If we're looking for a simple filename, _is_so_matched()
8190aa3cd4dSrie 		 * will reduce the ALIAS name to its simple name.
8200aa3cd4dSrie 		 */
821cce0e03bSab 		if (_is_so_matched(name, cp, path) == 0)
8227c478bd9Sstevel@tonic-gate 			return (lmp);
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8260aa3cd4dSrie 	 * Finally, if this is a simple file name, and any ALIAS() search has
8270aa3cd4dSrie 	 * been completed, match the simple file name of NAME() and PATHNAME().
8287c478bd9Sstevel@tonic-gate 	 */
8290aa3cd4dSrie 	if (path == 0) {
8300aa3cd4dSrie 		if (_is_so_matched(name, NAME(lmp), 0) == 0)
8317c478bd9Sstevel@tonic-gate 			return (lmp);
8320aa3cd4dSrie 
8330aa3cd4dSrie 		if (PATHNAME(lmp) != NAME(lmp)) {
8340aa3cd4dSrie 			if (_is_so_matched(name, PATHNAME(lmp), 0) == 0)
8350aa3cd4dSrie 				return (lmp);
8360aa3cd4dSrie 		}
8377c478bd9Sstevel@tonic-gate 	}
8380aa3cd4dSrie 
8397c478bd9Sstevel@tonic-gate 	return (0);
8407c478bd9Sstevel@tonic-gate }
8417c478bd9Sstevel@tonic-gate 
8420aa3cd4dSrie /*
8430aa3cd4dSrie  * Files are opened by ld.so.1 to satisfy dependencies, filtees and dlopen()
8440aa3cd4dSrie  * requests.  Each request investigates the file based upon the callers
8459aa23310Srie  * environment.  Once a full path name has been established, the following
8469aa23310Srie  * checks are made:
8470aa3cd4dSrie  *
8489aa23310Srie  *  .	does the path exist in the link-map lists FullPathNode AVL tree?  if
8499aa23310Srie  *	so, the file is already loaded, and its associated link-map pointer
8509aa23310Srie  *	is returned.
8519aa23310Srie  *  .	does the path exist in the not-found AVL tree?  if so, this path has
8529aa23310Srie  *	already been determined to not exist, and a failure is returned.
8539aa23310Srie  *  .	a device/inode check, to ensure the same file isn't mapped multiple
8549aa23310Srie  *	times through different paths.  See file_open().
8559aa23310Srie  *
8569aa23310Srie  * However, there are cases where a test for an existing file name needs to be
8579aa23310Srie  * carried out, such as dlopen(NOLOAD) requests, dldump() requests, and as a
8589aa23310Srie  * final fallback to dependency loading.  These requests are handled by
8599aa23310Srie  * is_so_loaded().
8600aa3cd4dSrie  *
8610aa3cd4dSrie  * A traversal through the callers link-map list is carried out, and from each
8620aa3cd4dSrie  * link-map, a comparison is made against all of the various names by which the
8639aa23310Srie  * object has been referenced.  is_so_matched() is used to compares the link-map
8649aa23310Srie  * names against the name being searched for.  Whether the search name is a full
8659aa23310Srie  * path name or a simple file name, governs what comparisons are made.
8660aa3cd4dSrie  *
8670aa3cd4dSrie  * A full path name, which is a fully resolved path name that starts with a "/"
8680aa3cd4dSrie  * character, or a relative path name that includes a "/" character, must match
8690aa3cd4dSrie  * the link-map names explicitly.  A simple file name, which is any name *not*
8700aa3cd4dSrie  * containing a "/" character, are matched against the file name component of
8710aa3cd4dSrie  * any link-map names.
8720aa3cd4dSrie  */
8737c478bd9Sstevel@tonic-gate Rt_map *
8749aa23310Srie is_so_loaded(Lm_list *lml, const char *name, int *in_nfavl)
8757c478bd9Sstevel@tonic-gate {
8767c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
8777c478bd9Sstevel@tonic-gate 	avl_index_t	where;
8787c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc;
879cce0e03bSab 	Aliste		idx;
8800aa3cd4dSrie 	int		path = 0;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/*
8830aa3cd4dSrie 	 * If the name is a full path name, first determine if the path name is
8849aa23310Srie 	 * registered on the FullPathNode AVL, or not-found AVL trees.
8857c478bd9Sstevel@tonic-gate 	 */
8869aa23310Srie 	if (name[0] == '/') {
8879aa23310Srie 		if (((lmp = fpavl_recorded(lml, name, &where)) != NULL) &&
8889aa23310Srie 		    ((FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE)) == 0))
8899aa23310Srie 			return (lmp);
8909aa23310Srie 		if (nfavl_recorded(name, 0)) {
8919aa23310Srie 			/*
8929aa23310Srie 			 * For dlopen() and dlsym() fall backs, indicate that
8939aa23310Srie 			 * a registered not-found path has indicated that this
8949aa23310Srie 			 * object does not exist.
8959aa23310Srie 			 */
8969aa23310Srie 			if (in_nfavl)
8979aa23310Srie 				(*in_nfavl)++;
8989aa23310Srie 			return (0);
8999aa23310Srie 		}
9009aa23310Srie 	}
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	/*
9030aa3cd4dSrie 	 * Determine whether the name is a simple file name, or a path name.
9047c478bd9Sstevel@tonic-gate 	 */
9050aa3cd4dSrie 	if (strchr(name, '/'))
9060aa3cd4dSrie 		path++;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * Loop through the callers link-map lists.
9107c478bd9Sstevel@tonic-gate 	 */
911cce0e03bSab 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
9127c478bd9Sstevel@tonic-gate 		for (lmp = lmc->lc_head; lmp; lmp = (Rt_map *)NEXT(lmp)) {
9137c478bd9Sstevel@tonic-gate 			if (FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE))
9147c478bd9Sstevel@tonic-gate 				continue;
9157c478bd9Sstevel@tonic-gate 
9160aa3cd4dSrie 			if (is_so_matched(lmp, name, path))
9177c478bd9Sstevel@tonic-gate 				return (lmp);
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	return ((Rt_map *)0);
9217c478bd9Sstevel@tonic-gate }
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate /*
9247c478bd9Sstevel@tonic-gate  * Tracing is enabled by the LD_TRACE_LOADED_OPTIONS environment variable which
9257c478bd9Sstevel@tonic-gate  * is normally set from ldd(1).  For each link map we load, print the load name
9267c478bd9Sstevel@tonic-gate  * and the full pathname of the shared object.
9277c478bd9Sstevel@tonic-gate  */
9287c478bd9Sstevel@tonic-gate /* ARGSUSED4 */
9297c478bd9Sstevel@tonic-gate static void
9307c478bd9Sstevel@tonic-gate trace_so(Rt_map *clmp, Rej_desc *rej, const char *name, const char *path,
9317c478bd9Sstevel@tonic-gate     int alter, const char *nfound)
9327c478bd9Sstevel@tonic-gate {
9337c478bd9Sstevel@tonic-gate 	const char	*str = MSG_ORIG(MSG_STR_EMPTY);
9347c478bd9Sstevel@tonic-gate 	const char	*reject = MSG_ORIG(MSG_STR_EMPTY);
9357c478bd9Sstevel@tonic-gate 	char		_reject[PATH_MAX];
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	/*
9387c478bd9Sstevel@tonic-gate 	 * The first time through trace_so() will only have lddstub on the
9397c478bd9Sstevel@tonic-gate 	 * link-map list and the preloaded shared object is supplied as "path".
9407c478bd9Sstevel@tonic-gate 	 * As we don't want to print this shared object as a dependency, but
9417c478bd9Sstevel@tonic-gate 	 * instead inspect *its* dependencies, return.
9427c478bd9Sstevel@tonic-gate 	 */
9437c478bd9Sstevel@tonic-gate 	if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
9447c478bd9Sstevel@tonic-gate 		return;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * Without any rejection info, this is a supplied not-found condition.
9487c478bd9Sstevel@tonic-gate 	 */
9497c478bd9Sstevel@tonic-gate 	if (rej && (rej->rej_type == 0)) {
9507c478bd9Sstevel@tonic-gate 		(void) printf(nfound, name);
9517c478bd9Sstevel@tonic-gate 		return;
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	/*
9557c478bd9Sstevel@tonic-gate 	 * If rejection information exists then establish what object was
9567c478bd9Sstevel@tonic-gate 	 * found and the reason for its rejection.
9577c478bd9Sstevel@tonic-gate 	 */
9587c478bd9Sstevel@tonic-gate 	if (rej) {
959de777a60Sab 		Conv_reject_desc_buf_t rej_buf;
960de777a60Sab 
9617c478bd9Sstevel@tonic-gate 		/* LINTED */
9627c478bd9Sstevel@tonic-gate 		(void) snprintf(_reject, PATH_MAX,
963de777a60Sab 		    MSG_INTL(ldd_reject[rej->rej_type]),
964ba2be530Sab 		    conv_reject_desc(rej, &rej_buf, M_MACH));
9657c478bd9Sstevel@tonic-gate 		if (rej->rej_name)
9667c478bd9Sstevel@tonic-gate 			path = rej->rej_name;
9677c478bd9Sstevel@tonic-gate 		reject = (char *)_reject;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * Was an alternative pathname defined (from a configuration
9717c478bd9Sstevel@tonic-gate 		 * file).
9727c478bd9Sstevel@tonic-gate 		 */
9737c478bd9Sstevel@tonic-gate 		if (rej->rej_flag & FLG_FD_ALTER)
9747c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
9757c478bd9Sstevel@tonic-gate 	} else {
9767c478bd9Sstevel@tonic-gate 		if (alter)
9777c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
9787c478bd9Sstevel@tonic-gate 	}
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	/*
9817c478bd9Sstevel@tonic-gate 	 * If the load name isn't a full pathname print its associated pathname
9827c478bd9Sstevel@tonic-gate 	 * together with all the other information we've gathered.
9837c478bd9Sstevel@tonic-gate 	 */
9847c478bd9Sstevel@tonic-gate 	if (*name == '/')
9857247f888Srie 		(void) printf(MSG_ORIG(MSG_LDD_FIL_PATH), path, str, reject);
9867c478bd9Sstevel@tonic-gate 	else
9877247f888Srie 		(void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV), name, path, str,
9887247f888Srie 		    reject);
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate /*
9937c478bd9Sstevel@tonic-gate  * Establish a link-map mode, initializing it if it has just been loaded, or
9947c478bd9Sstevel@tonic-gate  * potentially updating it if it already exists.
9957c478bd9Sstevel@tonic-gate  */
9967c478bd9Sstevel@tonic-gate int
9977c478bd9Sstevel@tonic-gate update_mode(Rt_map *lmp, int omode, int nmode)
9987c478bd9Sstevel@tonic-gate {
999dffec89cSrie 	Lm_list	*lml = LIST(lmp);
10007c478bd9Sstevel@tonic-gate 	int	pmode = 0;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	/*
10037c478bd9Sstevel@tonic-gate 	 * A newly loaded object hasn't had its mode set yet.  Modes are used to
10047c478bd9Sstevel@tonic-gate 	 * load dependencies, so don't propagate any parent or no-load flags, as
10057c478bd9Sstevel@tonic-gate 	 * these would adversely affect this objects ability to load any of its
10067c478bd9Sstevel@tonic-gate 	 * dependencies that aren't already loaded.  RTLD_FIRST is applicable to
10077c478bd9Sstevel@tonic-gate 	 * this objects handle creation only, and should not be propagated.
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 	if ((FLAGS(lmp) & FLG_RT_MODESET) == 0) {
10107c478bd9Sstevel@tonic-gate 		MODE(lmp) |= nmode & ~(RTLD_PARENT | RTLD_NOLOAD | RTLD_FIRST);
10117c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_MODESET;
10127c478bd9Sstevel@tonic-gate 		return (1);
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	/*
10167c478bd9Sstevel@tonic-gate 	 * Establish any new overriding modes.  RTLD_LAZY and RTLD_NOW should be
10177c478bd9Sstevel@tonic-gate 	 * represented individually (this is historic, as these two flags were
10187c478bd9Sstevel@tonic-gate 	 * the only flags originally available to dlopen()).  Other flags are
10197c478bd9Sstevel@tonic-gate 	 * accumulative, but have a hierarchy of preference.
10207c478bd9Sstevel@tonic-gate 	 */
10217c478bd9Sstevel@tonic-gate 	if ((omode & RTLD_LAZY) && (nmode & RTLD_NOW)) {
10227c478bd9Sstevel@tonic-gate 		MODE(lmp) &= ~RTLD_LAZY;
10237c478bd9Sstevel@tonic-gate 		pmode |= RTLD_NOW;
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	pmode |= ((~omode & nmode) &
10277c478bd9Sstevel@tonic-gate 	    (RTLD_GLOBAL | RTLD_WORLD | RTLD_NODELETE));
10287c478bd9Sstevel@tonic-gate 	if (pmode) {
10295aefb655Srie 		DBG_CALL(Dbg_file_mode_promote(lmp, pmode));
10307c478bd9Sstevel@tonic-gate 		MODE(lmp) |= pmode;
10317c478bd9Sstevel@tonic-gate 	}
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	/*
10347c478bd9Sstevel@tonic-gate 	 * If this load is an RTLD_NOW request and the object has already been
10357c478bd9Sstevel@tonic-gate 	 * loaded non-RTLD_NOW, append this object to the relocation-now list
10367c478bd9Sstevel@tonic-gate 	 * of the objects associated control list.  Note, if the object hasn't
10377c478bd9Sstevel@tonic-gate 	 * yet been relocated, setting its MODE() to RTLD_NOW will establish
10387c478bd9Sstevel@tonic-gate 	 * full relocation processing when it eventually gets relocated.
10397c478bd9Sstevel@tonic-gate 	 */
10407c478bd9Sstevel@tonic-gate 	if ((pmode & RTLD_NOW) &&
10417c478bd9Sstevel@tonic-gate 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))) {
10427c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 		/* LINTED */
1045cce0e03bSab 		lmc = (Lm_cntl *)alist_item_by_offset(LIST(lmp)->lm_lists,
1046cce0e03bSab 		    CNTL(lmp));
1047cce0e03bSab 		(void) aplist_append(&lmc->lc_now, lmp, AL_CNT_LMNOW);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
10517c478bd9Sstevel@tonic-gate 	/*
10527c478bd9Sstevel@tonic-gate 	 * For patch backward compatibility the following .init collection
10537c478bd9Sstevel@tonic-gate 	 * is disabled.
10547c478bd9Sstevel@tonic-gate 	 */
10557c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DISFIX_1)
10567c478bd9Sstevel@tonic-gate 		return (pmode);
10577c478bd9Sstevel@tonic-gate #endif
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
1060dffec89cSrie 	 * If this objects .init has been collected but has not yet been called,
1061dffec89cSrie 	 * it may be necessary to reevaluate the object using tsort().  For
10627c478bd9Sstevel@tonic-gate 	 * example, a new dlopen() hierarchy may bind to uninitialized objects
10637c478bd9Sstevel@tonic-gate 	 * that are already loaded, or a dlopen(RTLD_NOW) can establish new
10647c478bd9Sstevel@tonic-gate 	 * bindings between already loaded objects that require the tsort()
1065dffec89cSrie 	 * information be recomputed.  If however, no new objects have been
1066dffec89cSrie 	 * added to the process, and this object hasn't been promoted, don't
1067dffec89cSrie 	 * bother reevaluating the .init.  The present tsort() information is
1068dffec89cSrie 	 * probably as accurate as necessary, and by not establishing a parallel
1069dffec89cSrie 	 * tsort() we can help reduce the amount of recursion possible between
1070dffec89cSrie 	 * .inits.
10717c478bd9Sstevel@tonic-gate 	 */
1072dffec89cSrie 	if (((FLAGS(lmp) &
1073dffec89cSrie 	    (FLG_RT_INITCLCT | FLG_RT_INITCALL)) == FLG_RT_INITCLCT) &&
1074dffec89cSrie 	    ((lml->lm_flags & LML_FLG_OBJADDED) || ((pmode & RTLD_NOW) &&
1075dffec89cSrie 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))))) {
10767c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_INITCLCT;
10777c478bd9Sstevel@tonic-gate 		LIST(lmp)->lm_init++;
1078dffec89cSrie 		LIST(lmp)->lm_flags |= LML_FLG_OBJREEVAL;
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	return (pmode);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Determine whether an alias name already exists, and if not create one.  This
10867c478bd9Sstevel@tonic-gate  * is typically used to retain dependency names, such as "libc.so.1", which
10877c478bd9Sstevel@tonic-gate  * would have been expanded to full path names when they were loaded.  The
10887c478bd9Sstevel@tonic-gate  * full path names (NAME() and possibly PATHNAME()) are maintained as Fullpath
10897c478bd9Sstevel@tonic-gate  * AVL nodes, and thus would have been matched by fpavl_loaded() during
10907c478bd9Sstevel@tonic-gate  * file_open().
10917c478bd9Sstevel@tonic-gate  */
10927c478bd9Sstevel@tonic-gate int
10937c478bd9Sstevel@tonic-gate append_alias(Rt_map *lmp, const char *str, int *added)
10947c478bd9Sstevel@tonic-gate {
1095cce0e03bSab 	Aliste	idx;
1096cce0e03bSab 	char	*cp;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	/*
10997c478bd9Sstevel@tonic-gate 	 * Determine if this filename is already on the alias list.
11007c478bd9Sstevel@tonic-gate 	 */
1101cce0e03bSab 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
1102cce0e03bSab 		if (strcmp(cp, str) == 0)
11037c478bd9Sstevel@tonic-gate 			return (1);
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	/*
11077c478bd9Sstevel@tonic-gate 	 * This is a new alias, append it to the alias list.
11087c478bd9Sstevel@tonic-gate 	 */
11098521e5e6Srie 	if ((cp = strdup(str)) == NULL)
11107c478bd9Sstevel@tonic-gate 		return (0);
11117c478bd9Sstevel@tonic-gate 
1112cce0e03bSab 	if (aplist_append(&ALIAS(lmp), cp, AL_CNT_ALIAS) == NULL) {
11137c478bd9Sstevel@tonic-gate 		free(cp);
11147c478bd9Sstevel@tonic-gate 		return (0);
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 	if (added)
11177c478bd9Sstevel@tonic-gate 		*added = 1;
11187c478bd9Sstevel@tonic-gate 	return (1);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate /*
11227c478bd9Sstevel@tonic-gate  * Determine whether a file is already loaded by comparing device and inode
11237c478bd9Sstevel@tonic-gate  * values.
11247c478bd9Sstevel@tonic-gate  */
11257c478bd9Sstevel@tonic-gate static Rt_map *
11267c478bd9Sstevel@tonic-gate is_devinode_loaded(struct stat *status, Lm_list *lml, const char *name,
11277c478bd9Sstevel@tonic-gate     uint_t flags)
11287c478bd9Sstevel@tonic-gate {
11297c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
1130cce0e03bSab 	Aliste	idx;
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	/*
11337c478bd9Sstevel@tonic-gate 	 * If this is an auditor, it will have been opened on a new link-map.
11348af2c5b9Srie 	 * To prevent multiple occurrences of the same auditor on multiple
11357c478bd9Sstevel@tonic-gate 	 * link-maps, search the head of each link-map list and see if this
11367c478bd9Sstevel@tonic-gate 	 * object is already loaded as an auditor.
11377c478bd9Sstevel@tonic-gate 	 */
11387c478bd9Sstevel@tonic-gate 	if (flags & FLG_RT_AUDIT) {
1139aa736cbeSrie 		Lm_list		*lml;
1140aa736cbeSrie 		Listnode	*lnp;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 		for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
11437c478bd9Sstevel@tonic-gate 			Rt_map	*nlmp = lml->lm_head;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 			if (nlmp && ((FLAGS(nlmp) &
11467c478bd9Sstevel@tonic-gate 			    (FLG_RT_AUDIT | FLG_RT_DELETE)) == FLG_RT_AUDIT) &&
11477c478bd9Sstevel@tonic-gate 			    (STDEV(nlmp) == status->st_dev) &&
11487c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) == status->st_ino))
11497c478bd9Sstevel@tonic-gate 				return (nlmp);
11507c478bd9Sstevel@tonic-gate 		}
11517c478bd9Sstevel@tonic-gate 		return ((Rt_map *)0);
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	/*
11557c478bd9Sstevel@tonic-gate 	 * If the file has been found determine from the new files status
11567c478bd9Sstevel@tonic-gate 	 * information if this file is actually linked to one we already have
11577c478bd9Sstevel@tonic-gate 	 * mapped.  This catches symlink names not caught by is_so_loaded().
11587c478bd9Sstevel@tonic-gate 	 */
1159cce0e03bSab 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
11607c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 		for (nlmp = lmc->lc_head; nlmp; nlmp = (Rt_map *)NEXT(nlmp)) {
11637c478bd9Sstevel@tonic-gate 			if ((FLAGS(nlmp) & FLG_RT_DELETE) ||
11647c478bd9Sstevel@tonic-gate 			    (FLAGS1(nlmp) & FL1_RT_LDDSTUB))
11657c478bd9Sstevel@tonic-gate 				continue;
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 			if ((STDEV(nlmp) != status->st_dev) ||
11687c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) != status->st_ino))
11697c478bd9Sstevel@tonic-gate 				continue;
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_VERBOSE) {
11727247f888Srie 				/* BEGIN CSTYLED */
11737c478bd9Sstevel@tonic-gate 				if (*name == '/')
11747c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
11757c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
11767c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
11777c478bd9Sstevel@tonic-gate 				else
11787c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
11797c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
11807c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
11817c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
11827247f888Srie 				/* END CSTYLED */
11837c478bd9Sstevel@tonic-gate 			}
11847c478bd9Sstevel@tonic-gate 			return (nlmp);
11857c478bd9Sstevel@tonic-gate 		}
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 	return ((Rt_map *)0);
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate /*
11917c478bd9Sstevel@tonic-gate  * Generate any error messages indicating a file could not be found.  When
11927c478bd9Sstevel@tonic-gate  * preloading or auditing a secure application, it can be a little more helpful
11937c478bd9Sstevel@tonic-gate  * to indicate that a search of secure directories has failed, so adjust the
11947c478bd9Sstevel@tonic-gate  * messages accordingly.
11957c478bd9Sstevel@tonic-gate  */
11967c478bd9Sstevel@tonic-gate void
11977c478bd9Sstevel@tonic-gate file_notfound(Lm_list *lml, const char *name, Rt_map *clmp, uint_t flags,
11987c478bd9Sstevel@tonic-gate     Rej_desc * rej)
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate 	int	secure = 0;
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SECURE) &&
12037c478bd9Sstevel@tonic-gate 	    (flags & (FLG_RT_PRELOAD | FLG_RT_AUDIT)))
12047c478bd9Sstevel@tonic-gate 		secure++;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
12077c478bd9Sstevel@tonic-gate 		/*
12087c478bd9Sstevel@tonic-gate 		 * Under ldd(1), auxiliary filtees that can't be loaded are
12097c478bd9Sstevel@tonic-gate 		 * ignored, unless verbose errors are requested.
12107c478bd9Sstevel@tonic-gate 		 */
12117c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) &&
12127c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_TRC_VERBOSE) == 0))
12137c478bd9Sstevel@tonic-gate 			return;
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 		if (secure)
12167c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
12177c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_SEC_NFOUND));
12187c478bd9Sstevel@tonic-gate 		else
12197c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
12207c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_FIL_NFOUND));
12217c478bd9Sstevel@tonic-gate 		return;
12227c478bd9Sstevel@tonic-gate 	}
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
1225de777a60Sab 		Conv_reject_desc_buf_t rej_buf;
1226de777a60Sab 
12275aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(err_reject[rej->rej_type]),
12287c478bd9Sstevel@tonic-gate 		    rej->rej_name ? rej->rej_name : MSG_INTL(MSG_STR_UNKNOWN),
1229ba2be530Sab 		    conv_reject_desc(rej, &rej_buf, M_MACH));
12307c478bd9Sstevel@tonic-gate 		return;
12317c478bd9Sstevel@tonic-gate 	}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	if (secure)
12345aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SEC_OPEN), name);
12357c478bd9Sstevel@tonic-gate 	else
12365aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), name,
12377c478bd9Sstevel@tonic-gate 		    strerror(ENOENT));
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate static int
12417c478bd9Sstevel@tonic-gate file_open(int err, Lm_list *lml, const char *oname, const char *nname,
12429aa23310Srie     Rt_map *clmp, uint_t flags, Fdesc *fdesc, Rej_desc *rej, int *in_nfavl)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 	struct stat	status;
12457c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
12468521e5e6Srie 	int		resolved = 0;
12479aa23310Srie 	char		*name;
12489aa23310Srie 	avl_index_t	nfavlwhere = 0;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	fdesc->fd_oname = oname;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if ((err == 0) && (fdesc->fd_flags & FLG_FD_ALTER))
12535aefb655Srie 		DBG_CALL(Dbg_file_config_obj(lml, oname, 0, nname));
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	/*
12567c478bd9Sstevel@tonic-gate 	 * If we're dealing with a full pathname, determine whether this
12577c478bd9Sstevel@tonic-gate 	 * pathname is already known.  Other pathnames fall through to the
12587c478bd9Sstevel@tonic-gate 	 * dev/inode check, as even though the pathname may look the same as
12597c478bd9Sstevel@tonic-gate 	 * one previously used, the process may have changed directory.
12607c478bd9Sstevel@tonic-gate 	 */
12617c478bd9Sstevel@tonic-gate 	if ((err == 0) && (nname[0] == '/')) {
12629aa23310Srie 		if ((nlmp = fpavl_recorded(lml, nname,
12637c478bd9Sstevel@tonic-gate 		    &(fdesc->fd_avlwhere))) != NULL) {
12647c478bd9Sstevel@tonic-gate 			fdesc->fd_nname = nname;
12657c478bd9Sstevel@tonic-gate 			fdesc->fd_lmp = nlmp;
12667c478bd9Sstevel@tonic-gate 			return (1);
12677c478bd9Sstevel@tonic-gate 		}
12689aa23310Srie 		if (nfavl_recorded(nname, &nfavlwhere)) {
12699aa23310Srie 			/*
12709aa23310Srie 			 * For dlopen() and dlsym() fall backs, indicate that
12719aa23310Srie 			 * a registered not-found path has indicated that this
12729aa23310Srie 			 * object does not exist.  If this path has been
12739aa23310Srie 			 * constructed as part of expanding a HWCAP directory,
12749aa23310Srie 			 * and as this is a silent failure, where no rejection
12759aa23310Srie 			 * message is created, free the original name to
12769aa23310Srie 			 * simplify the life of the caller.
12779aa23310Srie 			 */
12789aa23310Srie 			if (in_nfavl)
12799aa23310Srie 				(*in_nfavl)++;
12809aa23310Srie 			if (flags & FLG_RT_HWCAP)
12819aa23310Srie 				free((void *)nname);
12829aa23310Srie 			return (0);
12839aa23310Srie 		}
12847c478bd9Sstevel@tonic-gate 	}
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	if ((err == 0) && ((stat(nname, &status)) != -1)) {
12877c478bd9Sstevel@tonic-gate 		char	path[PATH_MAX];
12887c478bd9Sstevel@tonic-gate 		int	fd, size, added;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 		/*
12917c478bd9Sstevel@tonic-gate 		 * If this path has been constructed as part of expanding a
12928521e5e6Srie 		 * HWCAP directory, ignore any subdirectories.  As this is a
12938521e5e6Srie 		 * silent failure, where no rejection message is created, free
12948521e5e6Srie 		 * the original name to simplify the life of the caller.  For
12958521e5e6Srie 		 * any other reference that expands to a directory, fall through
12968af2c5b9Srie 		 * to construct a meaningful rejection message.
12977c478bd9Sstevel@tonic-gate 		 */
12987c478bd9Sstevel@tonic-gate 		if ((flags & FLG_RT_HWCAP) &&
12998521e5e6Srie 		    ((status.st_mode & S_IFMT) == S_IFDIR)) {
13008521e5e6Srie 			free((void *)nname);
13017c478bd9Sstevel@tonic-gate 			return (0);
13028521e5e6Srie 		}
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 		/*
13057c478bd9Sstevel@tonic-gate 		 * Resolve the filename and determine whether the resolved name
13067c478bd9Sstevel@tonic-gate 		 * is already known.  Typically, the previous fpavl_loaded()
13077c478bd9Sstevel@tonic-gate 		 * will have caught this, as both NAME() and PATHNAME() for a
13087c478bd9Sstevel@tonic-gate 		 * link-map are recorded in the FullNode AVL tree.  However,
13097c478bd9Sstevel@tonic-gate 		 * instances exist where a file can be replaced (loop-back
13107c478bd9Sstevel@tonic-gate 		 * mounts, bfu, etc.), and reference is made to the original
13117c478bd9Sstevel@tonic-gate 		 * file through a symbolic link.  By checking the pathname here,
13127c478bd9Sstevel@tonic-gate 		 * we don't fall through to the dev/inode check and conclude
13137c478bd9Sstevel@tonic-gate 		 * that a new file should be loaded.
13147c478bd9Sstevel@tonic-gate 		 */
13157c478bd9Sstevel@tonic-gate 		if ((nname[0] == '/') && (rtld_flags & RT_FL_EXECNAME) &&
13167c478bd9Sstevel@tonic-gate 		    ((size = resolvepath(nname, path, (PATH_MAX - 1))) > 0)) {
13177c478bd9Sstevel@tonic-gate 			path[size] = '\0';
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 			if (strcmp(nname, path)) {
132024a6229eSrie 				if ((nlmp =
13219aa23310Srie 				    fpavl_recorded(lml, path, 0)) != NULL) {
13227c478bd9Sstevel@tonic-gate 					added = 0;
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 					if (append_alias(nlmp, nname,
13257c478bd9Sstevel@tonic-gate 					    &added) == 0)
13267c478bd9Sstevel@tonic-gate 						return (0);
13277247f888Srie 					/* BEGIN CSTYLED */
13287c478bd9Sstevel@tonic-gate 					if (added)
132910a4fa49Srie 					    DBG_CALL(Dbg_file_skip(LIST(clmp),
13305aefb655Srie 						NAME(nlmp), nname));
13317247f888Srie 					/* END CSTYLED */
13327c478bd9Sstevel@tonic-gate 					fdesc->fd_nname = nname;
13337c478bd9Sstevel@tonic-gate 					fdesc->fd_lmp = nlmp;
13347c478bd9Sstevel@tonic-gate 					return (1);
13357c478bd9Sstevel@tonic-gate 				}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 				/*
13387c478bd9Sstevel@tonic-gate 				 * If this pathname hasn't been loaded, save
13397c478bd9Sstevel@tonic-gate 				 * the resolved pathname so that it doesn't
13407c478bd9Sstevel@tonic-gate 				 * have to be recomputed as part of fullpath()
13417c478bd9Sstevel@tonic-gate 				 * processing.
13427c478bd9Sstevel@tonic-gate 				 */
13438521e5e6Srie 				if ((fdesc->fd_pname = strdup(path)) == NULL)
13447c478bd9Sstevel@tonic-gate 					return (0);
13458521e5e6Srie 				resolved = 1;
13467c478bd9Sstevel@tonic-gate 			} else {
13477c478bd9Sstevel@tonic-gate 				/*
13487c478bd9Sstevel@tonic-gate 				 * If the resolved name doesn't differ from the
13497c478bd9Sstevel@tonic-gate 				 * original, save it without duplication.
13507c478bd9Sstevel@tonic-gate 				 * Having fd_pname set indicates that no further
13517c478bd9Sstevel@tonic-gate 				 * resolvepath processing is necessary.
13527c478bd9Sstevel@tonic-gate 				 */
13537c478bd9Sstevel@tonic-gate 				fdesc->fd_pname = nname;
13547c478bd9Sstevel@tonic-gate 			}
13557c478bd9Sstevel@tonic-gate 		}
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 		if (nlmp = is_devinode_loaded(&status, lml, nname, flags)) {
1358c75e1b9dSrie 			if (flags & FLG_RT_AUDIT) {
1359c75e1b9dSrie 				/*
1360c75e1b9dSrie 				 * If we've been requested to load an auditor,
1361c75e1b9dSrie 				 * and an auditor of the same name already
1362c75e1b9dSrie 				 * exists, then the original auditor is used.
1363c75e1b9dSrie 				 */
1364c75e1b9dSrie 				DBG_CALL(Dbg_audit_skip(LIST(clmp),
1365c75e1b9dSrie 				    NAME(nlmp), LIST(nlmp)->lm_lmidstr));
1366c75e1b9dSrie 			} else {
1367c75e1b9dSrie 				/*
1368c75e1b9dSrie 				 * Otherwise, if an alternatively named file
1369c75e1b9dSrie 				 * has been found for the same dev/inode, add
1370c75e1b9dSrie 				 * a new name alias, and insert any alias full
1371c75e1b9dSrie 				 * pathname in the link-map lists AVL tree.
1372c75e1b9dSrie 				 */
1373c75e1b9dSrie 				added = 0;
13747c478bd9Sstevel@tonic-gate 
1375c75e1b9dSrie 				if (append_alias(nlmp, nname, &added) == 0)
13767c478bd9Sstevel@tonic-gate 					return (0);
1377c75e1b9dSrie 				if (added) {
1378c75e1b9dSrie 					if ((nname[0] == '/') &&
1379c75e1b9dSrie 					    (fpavl_insert(lml, nlmp,
1380c75e1b9dSrie 					    nname, 0) == 0))
1381c75e1b9dSrie 						return (0);
1382c75e1b9dSrie 					DBG_CALL(Dbg_file_skip(LIST(clmp),
1383c75e1b9dSrie 					    NAME(nlmp), nname));
1384c75e1b9dSrie 				}
13857c478bd9Sstevel@tonic-gate 			}
1386c75e1b9dSrie 
1387c75e1b9dSrie 			/*
1388c75e1b9dSrie 			 * Record in the file descriptor the existing object
1389c75e1b9dSrie 			 * that satisfies this open request.
1390c75e1b9dSrie 			 */
13917c478bd9Sstevel@tonic-gate 			fdesc->fd_nname = nname;
13927c478bd9Sstevel@tonic-gate 			fdesc->fd_lmp = nlmp;
13937c478bd9Sstevel@tonic-gate 			return (1);
13947c478bd9Sstevel@tonic-gate 		}
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 		if ((fd = open(nname, O_RDONLY, 0)) == -1) {
13977c478bd9Sstevel@tonic-gate 			/*
13987c478bd9Sstevel@tonic-gate 			 * As the file must exist for the previous stat() to
13997c478bd9Sstevel@tonic-gate 			 * have succeeded, record the error condition.
14007c478bd9Sstevel@tonic-gate 			 */
14017c478bd9Sstevel@tonic-gate 			rej->rej_type = SGS_REJ_STR;
14027c478bd9Sstevel@tonic-gate 			rej->rej_str = strerror(errno);
14037c478bd9Sstevel@tonic-gate 		} else {
14047c478bd9Sstevel@tonic-gate 			Fct	*ftp;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 			if ((ftp = are_u_this(rej, fd, &status, nname)) != 0) {
14077c478bd9Sstevel@tonic-gate 				fdesc->fd_nname = nname;
14087c478bd9Sstevel@tonic-gate 				fdesc->fd_ftp = ftp;
14097c478bd9Sstevel@tonic-gate 				fdesc->fd_dev = status.st_dev;
14107c478bd9Sstevel@tonic-gate 				fdesc->fd_ino = status.st_ino;
14117c478bd9Sstevel@tonic-gate 				fdesc->fd_fd = fd;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 				/*
14147c478bd9Sstevel@tonic-gate 				 * Trace that this open has succeeded.
14157c478bd9Sstevel@tonic-gate 				 */
14167c478bd9Sstevel@tonic-gate 				if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
14177247f888Srie 					trace_so(clmp, 0, oname, nname,
14187247f888Srie 					    (fdesc->fd_flags & FLG_FD_ALTER),
14197247f888Srie 					    0);
14207c478bd9Sstevel@tonic-gate 				}
14217c478bd9Sstevel@tonic-gate 				return (1);
14227c478bd9Sstevel@tonic-gate 			}
14237c478bd9Sstevel@tonic-gate 			(void) close(fd);
14247c478bd9Sstevel@tonic-gate 		}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 	} else if (errno != ENOENT) {
14277c478bd9Sstevel@tonic-gate 		/*
14287c478bd9Sstevel@tonic-gate 		 * If the open() failed for anything other than the file not
14297c478bd9Sstevel@tonic-gate 		 * existing, record the error condition.
14307c478bd9Sstevel@tonic-gate 		 */
14317c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
14327c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(errno);
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 
14359aa23310Srie 	/*
14369aa23310Srie 	 * Regardless of error, duplicate and record any full path names that
14379aa23310Srie 	 * can't be used on the "not-found" AVL tree.
14389aa23310Srie 	 */
14399aa23310Srie 	if ((nname[0] == '/') && ((name = strdup(nname)) != NULL))
14409aa23310Srie 		nfavl_insert(name, nfavlwhere);
14419aa23310Srie 
14427c478bd9Sstevel@tonic-gate 	/*
14437c478bd9Sstevel@tonic-gate 	 * Indicate any rejection.
14447c478bd9Sstevel@tonic-gate 	 */
14457c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
14468521e5e6Srie 		/*
14478521e5e6Srie 		 * If this pathname was resolved and duplicated, remove the
14488521e5e6Srie 		 * allocated name to simplify the cleanup of the callers.
14498521e5e6Srie 		 */
14508521e5e6Srie 		if (resolved) {
14518521e5e6Srie 			free((void *)fdesc->fd_pname);
14528521e5e6Srie 			fdesc->fd_pname = NULL;
14538521e5e6Srie 		}
14547c478bd9Sstevel@tonic-gate 		rej->rej_name = nname;
14557c478bd9Sstevel@tonic-gate 		rej->rej_flag = (fdesc->fd_flags & FLG_FD_ALTER);
1456ba2be530Sab 		DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 	return (0);
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate /*
14627c478bd9Sstevel@tonic-gate  * Find a full pathname (it contains a "/").
14637c478bd9Sstevel@tonic-gate  */
14647c478bd9Sstevel@tonic-gate int
14657c478bd9Sstevel@tonic-gate find_path(Lm_list *lml, const char *oname, Rt_map *clmp, uint_t flags,
14669aa23310Srie     Fdesc *fdesc, Rej_desc *rej, int *in_nfavl)
14677c478bd9Sstevel@tonic-gate {
14687c478bd9Sstevel@tonic-gate 	int	err = 0;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	/*
14717c478bd9Sstevel@tonic-gate 	 * If directory configuration exists determine if this path is known.
14727c478bd9Sstevel@tonic-gate 	 */
14737c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DIRCFG) {
14747c478bd9Sstevel@tonic-gate 		Rtc_obj		*obj;
14757c478bd9Sstevel@tonic-gate 		const char	*aname;
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 		if ((obj = elf_config_ent(oname, (Word)elf_hash(oname),
14787c478bd9Sstevel@tonic-gate 		    0, &aname)) != 0) {
14797c478bd9Sstevel@tonic-gate 			/*
14807c478bd9Sstevel@tonic-gate 			 * If the configuration file states that this path is a
14817c478bd9Sstevel@tonic-gate 			 * directory, or the path is explicitly defined as
14827c478bd9Sstevel@tonic-gate 			 * non-existent (ie. a unused platform specific
14837c478bd9Sstevel@tonic-gate 			 * library), then go no further.
14847c478bd9Sstevel@tonic-gate 			 */
14857c478bd9Sstevel@tonic-gate 			if (obj->co_flags & RTC_OBJ_DIRENT) {
14867c478bd9Sstevel@tonic-gate 				err = EISDIR;
14877c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags &
14887c478bd9Sstevel@tonic-gate 			    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) ==
14897c478bd9Sstevel@tonic-gate 			    RTC_OBJ_NOEXIST) {
14907c478bd9Sstevel@tonic-gate 				err = ENOENT;
14917c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags & RTC_OBJ_ALTER) &&
14927c478bd9Sstevel@tonic-gate 			    (rtld_flags & RT_FL_OBJALT) && (lml == &lml_main)) {
14937c478bd9Sstevel@tonic-gate 				int	ret;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 				fdesc->fd_flags |= FLG_FD_ALTER;
14967c478bd9Sstevel@tonic-gate 				/*
14977c478bd9Sstevel@tonic-gate 				 * Attempt to open the alternative path.  If
14987c478bd9Sstevel@tonic-gate 				 * this fails, and the alternative is flagged
14997c478bd9Sstevel@tonic-gate 				 * as optional, fall through to open the
15007c478bd9Sstevel@tonic-gate 				 * original path.
15017c478bd9Sstevel@tonic-gate 				 */
15025aefb655Srie 				DBG_CALL(Dbg_libs_found(lml, aname,
15035aefb655Srie 				    FLG_FD_ALTER));
15047c478bd9Sstevel@tonic-gate 				if (((ret = file_open(0, lml, oname, aname,
15059aa23310Srie 				    clmp, flags, fdesc, rej, in_nfavl)) != 0) ||
15067c478bd9Sstevel@tonic-gate 				    ((obj->co_flags & RTC_OBJ_OPTINAL) == 0))
15077c478bd9Sstevel@tonic-gate 					return (ret);
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 				fdesc->fd_flags &= ~FLG_FD_ALTER;
15107c478bd9Sstevel@tonic-gate 			}
15117c478bd9Sstevel@tonic-gate 		}
15127c478bd9Sstevel@tonic-gate 	}
15135aefb655Srie 	DBG_CALL(Dbg_libs_found(lml, oname, 0));
15149aa23310Srie 	return (file_open(err, lml, oname, oname, clmp, flags, fdesc,
15159aa23310Srie 	    rej, in_nfavl));
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate /*
15197c478bd9Sstevel@tonic-gate  * Find a simple filename (it doesn't contain a "/").
15207c478bd9Sstevel@tonic-gate  */
15217c478bd9Sstevel@tonic-gate static int
15227c478bd9Sstevel@tonic-gate _find_file(Lm_list *lml, const char *oname, const char *nname, Rt_map *clmp,
15239aa23310Srie     uint_t flags, Fdesc *fdesc, Rej_desc *rej, Pnode *dir, int aflag,
15249aa23310Srie     int *in_nfavl)
15257c478bd9Sstevel@tonic-gate {
15265aefb655Srie 	DBG_CALL(Dbg_libs_found(lml, nname, aflag));
15277c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_TRC_SEARCH) &&
15287c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
15297c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_PTH_TRYING), nname, aflag ?
15307c478bd9Sstevel@tonic-gate 		    MSG_INTL(MSG_LDD_FIL_ALTER) : MSG_ORIG(MSG_STR_EMPTY));
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	/*
15347c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're about
15357c478bd9Sstevel@tonic-gate 	 * to go search for.  The audit library may offer an alternative
15367c478bd9Sstevel@tonic-gate 	 * dependency, or indicate that this dependency should be ignored.
15377c478bd9Sstevel@tonic-gate 	 */
15387c478bd9Sstevel@tonic-gate 	if ((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
15399aa23310Srie 		char	*aname;
15407c478bd9Sstevel@tonic-gate 
15419aa23310Srie 		if ((aname = audit_objsearch(clmp, nname,
15429aa23310Srie 		    (dir->p_orig & LA_SER_MASK))) == 0) {
15437247f888Srie 			DBG_CALL(Dbg_audit_terminate(lml, nname));
15447c478bd9Sstevel@tonic-gate 			return (0);
15457247f888Srie 		}
15467247f888Srie 
15477247f888Srie 		/*
15487247f888Srie 		 * Protect ourselves from auditor mischief, by copying any
15497247f888Srie 		 * alternative name over the present name (the present name is
15507247f888Srie 		 * maintained in a static buffer - see elf_get_so());
15517247f888Srie 		 */
15527247f888Srie 		if (nname != aname)
15537247f888Srie 			(void) strncpy((char *)nname, aname, PATH_MAX);
15547c478bd9Sstevel@tonic-gate 	}
15559aa23310Srie 	return (file_open(0, lml, oname, nname, clmp, flags, fdesc,
15569aa23310Srie 	    rej, in_nfavl));
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate 
1559390b98b5Srie static int
15607c478bd9Sstevel@tonic-gate find_file(Lm_list *lml, const char *oname, Rt_map *clmp, uint_t flags,
15619aa23310Srie     Fdesc *fdesc, Rej_desc *rej, Pnode *dir, Word * strhash, size_t olen,
15629aa23310Srie     int *in_nfavl)
15637c478bd9Sstevel@tonic-gate {
15647c478bd9Sstevel@tonic-gate 	static Rtc_obj	Obj = { 0 };
1565aa736cbeSrie 	Rtc_obj		*dobj;
15667c478bd9Sstevel@tonic-gate 	const char	*nname = oname;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	if (dir->p_name == 0)
15697c478bd9Sstevel@tonic-gate 		return (0);
15707c478bd9Sstevel@tonic-gate 	if (dir->p_info) {
15717c478bd9Sstevel@tonic-gate 		dobj = (Rtc_obj *)dir->p_info;
15727c478bd9Sstevel@tonic-gate 		if ((dobj->co_flags &
15737c478bd9Sstevel@tonic-gate 		    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
15747c478bd9Sstevel@tonic-gate 			return (0);
15757c478bd9Sstevel@tonic-gate 	} else
15767c478bd9Sstevel@tonic-gate 		dobj = 0;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	/*
15797c478bd9Sstevel@tonic-gate 	 * If configuration information exists see if this directory/file
15807c478bd9Sstevel@tonic-gate 	 * combination exists.
15817c478bd9Sstevel@tonic-gate 	 */
15827c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_DIRCFG) &&
15837c478bd9Sstevel@tonic-gate 	    ((dobj == 0) || (dobj->co_id != 0))) {
15847c478bd9Sstevel@tonic-gate 		Rtc_obj		*fobj;
15857c478bd9Sstevel@tonic-gate 		const char	*alt = 0;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 		/*
15887c478bd9Sstevel@tonic-gate 		 * If this pnode has not yet been searched for in the
15897c478bd9Sstevel@tonic-gate 		 * configuration file go find it.
15907c478bd9Sstevel@tonic-gate 		 */
15917c478bd9Sstevel@tonic-gate 		if (dobj == 0) {
15927c478bd9Sstevel@tonic-gate 			dobj = elf_config_ent(dir->p_name,
15937c478bd9Sstevel@tonic-gate 			    (Word)elf_hash(dir->p_name), 0, 0);
15947c478bd9Sstevel@tonic-gate 			if (dobj == 0)
15957c478bd9Sstevel@tonic-gate 				dobj = &Obj;
15967c478bd9Sstevel@tonic-gate 			dir->p_info = (void *)dobj;
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate 			if ((dobj->co_flags & (RTC_OBJ_NOEXIST |
15997c478bd9Sstevel@tonic-gate 			    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
16007c478bd9Sstevel@tonic-gate 				return (0);
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 		/*
16047c478bd9Sstevel@tonic-gate 		 * If we found a directory search for the file.
16057c478bd9Sstevel@tonic-gate 		 */
16067c478bd9Sstevel@tonic-gate 		if (dobj->co_id != 0) {
16077c478bd9Sstevel@tonic-gate 			if (*strhash == 0)
16087c478bd9Sstevel@tonic-gate 				*strhash = (Word)elf_hash(nname);
16097c478bd9Sstevel@tonic-gate 			fobj = elf_config_ent(nname, *strhash,
16107c478bd9Sstevel@tonic-gate 			    dobj->co_id, &alt);
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 			/*
16137c478bd9Sstevel@tonic-gate 			 * If this object specifically does not exist, or the
16147c478bd9Sstevel@tonic-gate 			 * object can't be found in a know-all-entries
16157c478bd9Sstevel@tonic-gate 			 * directory, continue looking.  If the object does
16167c478bd9Sstevel@tonic-gate 			 * exist determine if an alternative object exists.
16177c478bd9Sstevel@tonic-gate 			 */
16187c478bd9Sstevel@tonic-gate 			if (fobj == 0) {
16197c478bd9Sstevel@tonic-gate 				if (dobj->co_flags & RTC_OBJ_ALLENTS)
16207c478bd9Sstevel@tonic-gate 					return (0);
16217c478bd9Sstevel@tonic-gate 			} else {
16227c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & (RTC_OBJ_NOEXIST |
16237c478bd9Sstevel@tonic-gate 				    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
16247c478bd9Sstevel@tonic-gate 					return (0);
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & RTC_OBJ_ALTER) &&
16277c478bd9Sstevel@tonic-gate 				    (rtld_flags & RT_FL_OBJALT) &&
16287c478bd9Sstevel@tonic-gate 				    (lml == &lml_main)) {
16297c478bd9Sstevel@tonic-gate 					int	ret;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 					fdesc->fd_flags |= FLG_FD_ALTER;
16327c478bd9Sstevel@tonic-gate 					/*
16337c478bd9Sstevel@tonic-gate 					 * Attempt to open the alternative path.
16347c478bd9Sstevel@tonic-gate 					 * If this fails, and the alternative is
16357c478bd9Sstevel@tonic-gate 					 * flagged as optional, fall through to
16367c478bd9Sstevel@tonic-gate 					 * open the original path.
16377c478bd9Sstevel@tonic-gate 					 */
16387c478bd9Sstevel@tonic-gate 					ret = _find_file(lml, oname, alt, clmp,
16399aa23310Srie 					    flags, fdesc, rej, dir, 1,
16409aa23310Srie 					    in_nfavl);
16417c478bd9Sstevel@tonic-gate 					if (ret || ((fobj->co_flags &
16427c478bd9Sstevel@tonic-gate 					    RTC_OBJ_OPTINAL) == 0))
16437c478bd9Sstevel@tonic-gate 						return (ret);
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 					fdesc->fd_flags &= ~FLG_FD_ALTER;
16467c478bd9Sstevel@tonic-gate 				}
16477c478bd9Sstevel@tonic-gate 			}
16487c478bd9Sstevel@tonic-gate 		}
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	/*
16527c478bd9Sstevel@tonic-gate 	 * Protect ourselves from building an invalid pathname.
16537c478bd9Sstevel@tonic-gate 	 */
16547c478bd9Sstevel@tonic-gate 	if ((olen + dir->p_len + 1) >= PATH_MAX) {
16555aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), nname,
16567c478bd9Sstevel@tonic-gate 		    strerror(ENAMETOOLONG));
16577c478bd9Sstevel@tonic-gate 			return (0);
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 	if ((nname = (LM_GET_SO(clmp)(dir->p_name, nname))) == 0)
16607c478bd9Sstevel@tonic-gate 		return (0);
16617c478bd9Sstevel@tonic-gate 
16629aa23310Srie 	return (_find_file(lml, oname, nname, clmp, flags, fdesc, rej,
16639aa23310Srie 	    dir, 0, in_nfavl));
16647c478bd9Sstevel@tonic-gate }
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate /*
16677c478bd9Sstevel@tonic-gate  * A unique file has been opened.  Create a link-map to represent it, and
16687c478bd9Sstevel@tonic-gate  * process the various names by which it can be referenced.
16697c478bd9Sstevel@tonic-gate  */
16707c478bd9Sstevel@tonic-gate static Rt_map *
16719aa23310Srie load_file(Lm_list *lml, Aliste lmco, Fdesc *fdesc, int *in_nfavl)
16727c478bd9Sstevel@tonic-gate {
16737c478bd9Sstevel@tonic-gate 	const char	*oname = fdesc->fd_oname;
16747c478bd9Sstevel@tonic-gate 	const char	*nname = fdesc->fd_nname;
16757c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 	/*
16787c478bd9Sstevel@tonic-gate 	 * Typically we call fct_map_so() with the full pathname of the opened
16797c478bd9Sstevel@tonic-gate 	 * file (nname) and the name that started the search (oname), thus for
16807c478bd9Sstevel@tonic-gate 	 * a typical dependency on libc this would be /usr/lib/libc.so.1 and
16817c478bd9Sstevel@tonic-gate 	 * libc.so.1 (DT_NEEDED).  The original name is maintained on an ALIAS
16827c478bd9Sstevel@tonic-gate 	 * list for comparison when bringing in new dependencies.  If the user
16837c478bd9Sstevel@tonic-gate 	 * specified name as a full path (from a dlopen() for example) then
16847c478bd9Sstevel@tonic-gate 	 * there's no need to create an ALIAS.
16857c478bd9Sstevel@tonic-gate 	 */
16867c478bd9Sstevel@tonic-gate 	if (strcmp(oname, nname) == 0)
16877c478bd9Sstevel@tonic-gate 		oname = 0;
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 	/*
16907c478bd9Sstevel@tonic-gate 	 * A new file has been opened, now map it into the process.  Close the
16917c478bd9Sstevel@tonic-gate 	 * original file so as not to accumulate file descriptors.
16927c478bd9Sstevel@tonic-gate 	 */
16937c478bd9Sstevel@tonic-gate 	nlmp = ((fdesc->fd_ftp)->fct_map_so)(lml, lmco, nname, oname,
16949aa23310Srie 	    fdesc->fd_fd, in_nfavl);
16957c478bd9Sstevel@tonic-gate 	(void) close(fdesc->fd_fd);
16967c478bd9Sstevel@tonic-gate 	fdesc->fd_fd = 0;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	if (nlmp == 0)
16997c478bd9Sstevel@tonic-gate 		return (0);
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 	/*
17027c478bd9Sstevel@tonic-gate 	 * Save the dev/inode information for later comparisons.
17037c478bd9Sstevel@tonic-gate 	 */
17047c478bd9Sstevel@tonic-gate 	STDEV(nlmp) = fdesc->fd_dev;
17057c478bd9Sstevel@tonic-gate 	STINO(nlmp) = fdesc->fd_ino;
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	/*
17087c478bd9Sstevel@tonic-gate 	 * Insert the names of this link-map into the FullpathNode AVL tree.
17097c478bd9Sstevel@tonic-gate 	 * Save both the NAME() and PATHNAME() is they differ.
17107c478bd9Sstevel@tonic-gate 	 *
17117c478bd9Sstevel@tonic-gate 	 * If this is an OBJECT file, don't insert it yet as this is only a
17127c478bd9Sstevel@tonic-gate 	 * temporary link-map.  During elf_obj_fini() the final link-map is
17137c478bd9Sstevel@tonic-gate 	 * created, and its names will be inserted in the FullpathNode AVL
17147c478bd9Sstevel@tonic-gate 	 * tree at that time.
17157c478bd9Sstevel@tonic-gate 	 */
17167c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) & FLG_RT_OBJECT) == 0) {
17177c478bd9Sstevel@tonic-gate 		/*
17187c478bd9Sstevel@tonic-gate 		 * Update the objects full path information if necessary.
171911a2bb38Srie 		 * Note, with pathname expansion in effect, the fd_pname will
172011a2bb38Srie 		 * be used as PATHNAME().  This allocated string will be freed
172111a2bb38Srie 		 * should this object be deleted.  However, without pathname
172211a2bb38Srie 		 * expansion, the fd_name should be freed now, as it is no
172311a2bb38Srie 		 * longer referenced.
17247c478bd9Sstevel@tonic-gate 		 */
172511a2bb38Srie 		if (FLAGS1(nlmp) & FL1_RT_RELATIVE)
17267c478bd9Sstevel@tonic-gate 			(void) fullpath(nlmp, fdesc->fd_pname);
172711a2bb38Srie 		else if (fdesc->fd_pname != fdesc->fd_nname)
172811a2bb38Srie 			free((void *)fdesc->fd_pname);
172911a2bb38Srie 		fdesc->fd_pname = 0;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 		if ((NAME(nlmp)[0] == '/') && (fpavl_insert(lml, nlmp,
17327c478bd9Sstevel@tonic-gate 		    NAME(nlmp), fdesc->fd_avlwhere) == 0)) {
17337c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
17347c478bd9Sstevel@tonic-gate 			return (0);
17357c478bd9Sstevel@tonic-gate 		}
17367c478bd9Sstevel@tonic-gate 		if (((NAME(nlmp)[0] != '/') ||
17377c478bd9Sstevel@tonic-gate 		    (NAME(nlmp) != PATHNAME(nlmp))) &&
17387c478bd9Sstevel@tonic-gate 		    (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)) {
17397c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
17407c478bd9Sstevel@tonic-gate 			return (0);
17417c478bd9Sstevel@tonic-gate 		}
1742*3dbfc803SRod Evans 
1743*3dbfc803SRod Evans 		/*
1744*3dbfc803SRod Evans 		 * If this is a secure application, record any full path name
1745*3dbfc803SRod Evans 		 * directory in which this dependency has been found.  This
1746*3dbfc803SRod Evans 		 * directory can be deemed safe (as we've already found a
1747*3dbfc803SRod Evans 		 * dependency here).  This recording provides a fall-back
1748*3dbfc803SRod Evans 		 * should another objects $ORIGIN definition expands to this
1749*3dbfc803SRod Evans 		 * directory, an expansion that would ordinarily be deemed
1750*3dbfc803SRod Evans 		 * insecure.
1751*3dbfc803SRod Evans 		 */
1752*3dbfc803SRod Evans 		if (rtld_flags & RT_FL_SECURE) {
1753*3dbfc803SRod Evans 			if (NAME(nlmp)[0] == '/')
1754*3dbfc803SRod Evans 				spavl_insert(NAME(nlmp));
1755*3dbfc803SRod Evans 			if ((NAME(nlmp) != PATHNAME(nlmp)) &&
1756*3dbfc803SRod Evans 			    (PATHNAME(nlmp)[0] == '/'))
1757*3dbfc803SRod Evans 				spavl_insert(PATHNAME(nlmp));
1758*3dbfc803SRod Evans 		}
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	/*
17627c478bd9Sstevel@tonic-gate 	 * If we're processing an alternative object reset the original name
17637c478bd9Sstevel@tonic-gate 	 * for possible $ORIGIN processing.
17647c478bd9Sstevel@tonic-gate 	 */
17657c478bd9Sstevel@tonic-gate 	if (fdesc->fd_flags & FLG_FD_ALTER) {
17667c478bd9Sstevel@tonic-gate 		const char	*odir;
17677c478bd9Sstevel@tonic-gate 		char		*ndir;
17687c478bd9Sstevel@tonic-gate 		size_t		olen;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) |= FLG_RT_ALTER;
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 		/*
17737c478bd9Sstevel@tonic-gate 		 * If we were given a pathname containing a slash then the
17747c478bd9Sstevel@tonic-gate 		 * original name is still in oname.  Otherwise the original
17757c478bd9Sstevel@tonic-gate 		 * directory is in dir->p_name (which is all we need for
17767c478bd9Sstevel@tonic-gate 		 * $ORIGIN).
17777c478bd9Sstevel@tonic-gate 		 */
17787c478bd9Sstevel@tonic-gate 		if (fdesc->fd_flags & FLG_FD_SLASH) {
17797c478bd9Sstevel@tonic-gate 			char	*ofil;
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 			odir = oname;
17827c478bd9Sstevel@tonic-gate 			ofil = strrchr(oname, '/');
17837c478bd9Sstevel@tonic-gate 			olen = ofil - odir + 1;
17847c478bd9Sstevel@tonic-gate 		} else {
17857c478bd9Sstevel@tonic-gate 			odir = fdesc->fd_odir;
17867c478bd9Sstevel@tonic-gate 			olen = strlen(odir) + 1;
17877c478bd9Sstevel@tonic-gate 		}
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 		if ((ndir = (char *)malloc(olen)) == 0) {
17907c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
17917c478bd9Sstevel@tonic-gate 			return (0);
17927c478bd9Sstevel@tonic-gate 		}
17937c478bd9Sstevel@tonic-gate 		(void) strncpy(ndir, odir, olen);
17947c478bd9Sstevel@tonic-gate 		ndir[--olen] = '\0';
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 		ORIGNAME(nlmp) = ndir;
17977c478bd9Sstevel@tonic-gate 		DIRSZ(nlmp) = olen;
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	/*
1801390b98b5Srie 	 * Identify this as a new object.
18027c478bd9Sstevel@tonic-gate 	 */
1803390b98b5Srie 	FLAGS(nlmp) |= FLG_RT_NEWLOAD;
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate 	return (nlmp);
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate /*
18097c478bd9Sstevel@tonic-gate  * This function loads the named file and returns a pointer to its link map.
18107c478bd9Sstevel@tonic-gate  * It is assumed that the caller has already checked that the file is not
18117c478bd9Sstevel@tonic-gate  * already loaded before calling this function (refer is_so_loaded()).
18127c478bd9Sstevel@tonic-gate  * Find and open the file, map it into memory, add it to the end of the list
18137c478bd9Sstevel@tonic-gate  * of link maps and return a pointer to the new link map.  Return 0 on error.
18147c478bd9Sstevel@tonic-gate  */
18157c478bd9Sstevel@tonic-gate static Rt_map *
18167c478bd9Sstevel@tonic-gate load_so(Lm_list *lml, Aliste lmco, const char *oname, Rt_map *clmp,
18179aa23310Srie     uint_t flags, Fdesc *nfdp, Rej_desc *rej, int *in_nfavl)
18187c478bd9Sstevel@tonic-gate {
18197c478bd9Sstevel@tonic-gate 	char		*name;
18207c478bd9Sstevel@tonic-gate 	uint_t		slash = 0;
18217c478bd9Sstevel@tonic-gate 	size_t		olen;
1822fb1354edSrie 	Fdesc		fdesc = { 0 };
18237c478bd9Sstevel@tonic-gate 	Pnode		*dir;
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	/*
18267c478bd9Sstevel@tonic-gate 	 * If the file is the run time linker then it's already loaded.
18277c478bd9Sstevel@tonic-gate 	 */
18287c478bd9Sstevel@tonic-gate 	if (interp && (strcmp(oname, NAME(lml_rtld.lm_head)) == 0))
18297c478bd9Sstevel@tonic-gate 		return (lml_rtld.lm_head);
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	/*
18328af2c5b9Srie 	 * If this isn't a hardware capabilities pathname, which is already a
18337c478bd9Sstevel@tonic-gate 	 * full, duplicated pathname, determine whether the pathname contains
18347c478bd9Sstevel@tonic-gate 	 * a slash, and if not determine the input filename (for max path
18357c478bd9Sstevel@tonic-gate 	 * length verification).
18367c478bd9Sstevel@tonic-gate 	 */
18377c478bd9Sstevel@tonic-gate 	if ((flags & FLG_RT_HWCAP) == 0) {
18387c478bd9Sstevel@tonic-gate 		const char	*str;
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 		for (str = oname; *str; str++) {
18417c478bd9Sstevel@tonic-gate 			if (*str == '/') {
18427c478bd9Sstevel@tonic-gate 				slash++;
18437c478bd9Sstevel@tonic-gate 				break;
18447c478bd9Sstevel@tonic-gate 			}
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 		if (slash == 0)
18477c478bd9Sstevel@tonic-gate 			olen = (str - oname) + 1;
18487c478bd9Sstevel@tonic-gate 	}
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/*
18517c478bd9Sstevel@tonic-gate 	 * If we are passed a 'null' link-map this means that this is the first
18527c478bd9Sstevel@tonic-gate 	 * object to be loaded on this link-map list.  In that case we set the
18537c478bd9Sstevel@tonic-gate 	 * link-map to ld.so.1's link-map.
18547c478bd9Sstevel@tonic-gate 	 *
18557c478bd9Sstevel@tonic-gate 	 * This link-map is referenced to determine what lookup rules to use
18567c478bd9Sstevel@tonic-gate 	 * when searching for files.  By using ld.so.1's we are defaulting to
18577c478bd9Sstevel@tonic-gate 	 * ELF look-up rules.
18587c478bd9Sstevel@tonic-gate 	 *
18597c478bd9Sstevel@tonic-gate 	 * Note: This case happens when loading the first object onto
18607c478bd9Sstevel@tonic-gate 	 *	 the plt_tracing link-map.
18617c478bd9Sstevel@tonic-gate 	 */
18627c478bd9Sstevel@tonic-gate 	if (clmp == 0)
18637c478bd9Sstevel@tonic-gate 		clmp = lml_rtld.lm_head;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/*
18667c478bd9Sstevel@tonic-gate 	 * If this path resulted from a $HWCAP specification, then the best
18677c478bd9Sstevel@tonic-gate 	 * hardware capability object has already been establish, and is
18687c478bd9Sstevel@tonic-gate 	 * available in the calling file descriptor.  Perform some minor book-
18697c478bd9Sstevel@tonic-gate 	 * keeping so that we can fall through into common code.
18707c478bd9Sstevel@tonic-gate 	 */
18717c478bd9Sstevel@tonic-gate 	if (flags & FLG_RT_HWCAP) {
18727c478bd9Sstevel@tonic-gate 		/*
1873fb1354edSrie 		 * If this object is already loaded, we're done.
18747c478bd9Sstevel@tonic-gate 		 */
1875fb1354edSrie 		if (nfdp->fd_lmp)
1876fb1354edSrie 			return (nfdp->fd_lmp);
18777c478bd9Sstevel@tonic-gate 
1878fb1354edSrie 		/*
1879fb1354edSrie 		 * Obtain the avl index for this object.
1880fb1354edSrie 		 */
18819aa23310Srie 		(void) fpavl_recorded(lml, nfdp->fd_nname,
18829aa23310Srie 		    &(nfdp->fd_avlwhere));
1883bbf522bdSrie 
1884fb1354edSrie 		/*
1885fb1354edSrie 		 * If the name and resolved pathname differ, duplicate the path
18868af2c5b9Srie 		 * name once more to provide for generic cleanup by the caller.
1887fb1354edSrie 		 */
1888fb1354edSrie 		if (nfdp->fd_pname && (nfdp->fd_nname != nfdp->fd_pname)) {
1889fb1354edSrie 			char	*pname;
1890bbf522bdSrie 
18918521e5e6Srie 			if ((pname = strdup(nfdp->fd_pname)) == NULL)
1892fb1354edSrie 				return (0);
1893fb1354edSrie 			nfdp->fd_pname = pname;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 	} else if (slash) {
18967c478bd9Sstevel@tonic-gate 		Rej_desc	_rej = { 0 };
18977c478bd9Sstevel@tonic-gate 
1898fb1354edSrie 		*nfdp = fdesc;
1899fb1354edSrie 		nfdp->fd_flags = FLG_FD_SLASH;
19007c478bd9Sstevel@tonic-gate 
19019aa23310Srie 		if (find_path(lml, oname, clmp, flags, nfdp,
19029aa23310Srie 		    &_rej, in_nfavl) == 0) {
190331fdd7caSab 			rejection_inherit(rej, &_rej);
19047c478bd9Sstevel@tonic-gate 			return (0);
19057c478bd9Sstevel@tonic-gate 		}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 		/*
19087c478bd9Sstevel@tonic-gate 		 * If this object is already loaded, we're done.
19097c478bd9Sstevel@tonic-gate 		 */
1910fb1354edSrie 		if (nfdp->fd_lmp)
1911fb1354edSrie 			return (nfdp->fd_lmp);
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	} else {
19147c478bd9Sstevel@tonic-gate 		/*
19157c478bd9Sstevel@tonic-gate 		 * No '/' - for each directory on list, make a pathname using
19167c478bd9Sstevel@tonic-gate 		 * that directory and filename and try to open that file.
19177c478bd9Sstevel@tonic-gate 		 */
1918fb1354edSrie 		Pnode		*dirlist = (Pnode *)0;
19197c478bd9Sstevel@tonic-gate 		Word		strhash = 0;
1920fb1354edSrie #if	!defined(ISSOLOAD_BASENAME_DISABLED)
1921fb1354edSrie 		Rt_map		*nlmp;
1922fb1354edSrie #endif
19235aefb655Srie 		DBG_CALL(Dbg_libs_find(lml, oname));
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate #if	!defined(ISSOLOAD_BASENAME_DISABLED)
19269aa23310Srie 		if ((nlmp = is_so_loaded(lml, oname, in_nfavl)))
19277c478bd9Sstevel@tonic-gate 			return (nlmp);
19287c478bd9Sstevel@tonic-gate #endif
19297c478bd9Sstevel@tonic-gate 		/*
19307c478bd9Sstevel@tonic-gate 		 * Make sure we clear the file descriptor new name in case the
19317c478bd9Sstevel@tonic-gate 		 * following directory search doesn't provide any directories
19327c478bd9Sstevel@tonic-gate 		 * (odd, but this can be forced with a -znodefaultlib test).
19337c478bd9Sstevel@tonic-gate 		 */
1934fb1354edSrie 		*nfdp = fdesc;
19357c478bd9Sstevel@tonic-gate 		for (dir = get_next_dir(&dirlist, clmp, flags); dir;
19367c478bd9Sstevel@tonic-gate 		    dir = get_next_dir(&dirlist, clmp, flags)) {
19377c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
19387c478bd9Sstevel@tonic-gate 
1939fb1354edSrie 			*nfdp = fdesc;
19407c478bd9Sstevel@tonic-gate 
19419aa23310Srie 			/*
19429aa23310Srie 			 * Under debugging, duplicate path name entries are
19439aa23310Srie 			 * tagged but remain part of the search path list so
19449aa23310Srie 			 * that they can be diagnosed under "unused" processing.
19459aa23310Srie 			 * Skip these entries, as this path would have already
19469aa23310Srie 			 * been attempted.
19479aa23310Srie 			 */
19489aa23310Srie 			if (dir->p_orig & PN_FLG_DUPLICAT)
19499aa23310Srie 				continue;
19509aa23310Srie 
19517c478bd9Sstevel@tonic-gate 			/*
19527c478bd9Sstevel@tonic-gate 			 * Try and locate this file.  Make sure to clean up
19537c478bd9Sstevel@tonic-gate 			 * any rejection information should the file have
19547c478bd9Sstevel@tonic-gate 			 * been found, but not appropriate.
19557c478bd9Sstevel@tonic-gate 			 */
1956fb1354edSrie 			if (find_file(lml, oname, clmp, flags, nfdp, &_rej,
19579aa23310Srie 			    dir, &strhash, olen, in_nfavl) == 0) {
195831fdd7caSab 				rejection_inherit(rej, &_rej);
19597c478bd9Sstevel@tonic-gate 				continue;
19607c478bd9Sstevel@tonic-gate 			}
19617c478bd9Sstevel@tonic-gate 
19629aa23310Srie 			/*
19639aa23310Srie 			 * Indicate that this search path has been used.  If
19649aa23310Srie 			 * this is an LD_LIBRARY_PATH setting, ignore any use
19659aa23310Srie 			 * by ld.so.1 itself.
19669aa23310Srie 			 */
19679aa23310Srie 			if (((dir->p_orig & LA_SER_LIBPATH) == 0) ||
19689aa23310Srie 			    ((lml->lm_flags & LML_FLG_RTLDLM) == 0))
19699aa23310Srie 				dir->p_orig |= PN_FLG_USED;
19709aa23310Srie 
19717c478bd9Sstevel@tonic-gate 			/*
19727c478bd9Sstevel@tonic-gate 			 * If this object is already loaded, we're done.
19737c478bd9Sstevel@tonic-gate 			 */
1974fb1354edSrie 			if (nfdp->fd_lmp)
1975fb1354edSrie 				return (nfdp->fd_lmp);
19767c478bd9Sstevel@tonic-gate 
1977fb1354edSrie 			nfdp->fd_odir = dir->p_name;
19787c478bd9Sstevel@tonic-gate 			break;
19797c478bd9Sstevel@tonic-gate 		}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		/*
19827c478bd9Sstevel@tonic-gate 		 * If the file couldn't be loaded, do another comparison of
19837c478bd9Sstevel@tonic-gate 		 * loaded files using just the basename.  This catches folks
19847c478bd9Sstevel@tonic-gate 		 * who may have loaded multiple full pathname files (possibly
19857c478bd9Sstevel@tonic-gate 		 * from setxid applications) to satisfy dependency relationships
19867c478bd9Sstevel@tonic-gate 		 * (i.e., a file might have a dependency on foo.so.1 which has
19877c478bd9Sstevel@tonic-gate 		 * already been opened using its full pathname).
19887c478bd9Sstevel@tonic-gate 		 */
19898521e5e6Srie 		if (nfdp->fd_nname == NULL)
19909aa23310Srie 			return (is_so_loaded(lml, oname, in_nfavl));
19917c478bd9Sstevel@tonic-gate 	}
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 	/*
19947c478bd9Sstevel@tonic-gate 	 * Duplicate the file name so that NAME() is available in core files.
19957c478bd9Sstevel@tonic-gate 	 * Note, that hardware capability names are already duplicated, but
19967c478bd9Sstevel@tonic-gate 	 * they get duplicated once more to insure consistent cleanup in the
19977c478bd9Sstevel@tonic-gate 	 * event of an error condition.
19987c478bd9Sstevel@tonic-gate 	 */
19998521e5e6Srie 	if ((name = strdup(nfdp->fd_nname)) == NULL)
20007c478bd9Sstevel@tonic-gate 		return (0);
2001fb1354edSrie 
2002fb1354edSrie 	if (nfdp->fd_nname == nfdp->fd_pname)
2003fb1354edSrie 		nfdp->fd_nname = nfdp->fd_pname = name;
20047c478bd9Sstevel@tonic-gate 	else
2005fb1354edSrie 		nfdp->fd_nname = name;
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 	/*
20087c478bd9Sstevel@tonic-gate 	 * Finish mapping the file and return the link-map descriptor.  Note,
20097c478bd9Sstevel@tonic-gate 	 * if this request originated from a HWCAP request, re-establish the
20107c478bd9Sstevel@tonic-gate 	 * fdesc information.  For single paged objects, such as filters, the
20117c478bd9Sstevel@tonic-gate 	 * original mapping may have been sufficient to capture the file, thus
20127c478bd9Sstevel@tonic-gate 	 * this mapping needs to be reset to insure it doesn't mistakenly get
20137c478bd9Sstevel@tonic-gate 	 * unmapped as part of HWCAP cleanup.
20147c478bd9Sstevel@tonic-gate 	 */
20159aa23310Srie 	return (load_file(lml, lmco, nfdp, in_nfavl));
20167c478bd9Sstevel@tonic-gate }
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate /*
20197c478bd9Sstevel@tonic-gate  * Trace an attempt to load an object.
20207c478bd9Sstevel@tonic-gate  */
20217247f888Srie int
20227247f888Srie load_trace(Lm_list *lml, const char **oname, Rt_map *clmp)
20237c478bd9Sstevel@tonic-gate {
20247247f888Srie 	const char	*name = *oname;
20257247f888Srie 
20267c478bd9Sstevel@tonic-gate 	/*
20277c478bd9Sstevel@tonic-gate 	 * First generate any ldd(1) diagnostics.
20287c478bd9Sstevel@tonic-gate 	 */
20297c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
20307c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
20317c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name, NAME(clmp));
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	/*
20347c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're
20357c478bd9Sstevel@tonic-gate 	 * about to go search for.
20367c478bd9Sstevel@tonic-gate 	 */
20377c478bd9Sstevel@tonic-gate 	if (((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_ACTIVITY) &&
20387c478bd9Sstevel@tonic-gate 	    (lml == LIST(clmp)))
20397c478bd9Sstevel@tonic-gate 		audit_activity(clmp, LA_ACT_ADD);
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	if ((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
20427247f888Srie 		char	*aname = audit_objsearch(clmp, name, LA_SER_ORIG);
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 		/*
20457c478bd9Sstevel@tonic-gate 		 * The auditor can indicate that this object should be ignored.
20467c478bd9Sstevel@tonic-gate 		 */
20477247f888Srie 		if (aname == NULL) {
20487247f888Srie 			DBG_CALL(Dbg_audit_terminate(lml, name));
20497c478bd9Sstevel@tonic-gate 			return (0);
20507c478bd9Sstevel@tonic-gate 		}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 		/*
20537247f888Srie 		 * Protect ourselves from auditor mischief, by duplicating any
20547247f888Srie 		 * alternative name.  The original name has been allocated from
20557247f888Srie 		 * expand(), so free this allocation before using the audit
20567247f888Srie 		 * alternative.
20577c478bd9Sstevel@tonic-gate 		 */
20587247f888Srie 		if (name != aname) {
20597247f888Srie 			if ((aname = strdup(aname)) == NULL) {
20607247f888Srie 				eprintf(lml, ERR_FATAL,
20617247f888Srie 				    MSG_INTL(MSG_GEN_AUDITERM), name);
20627247f888Srie 				return (0);
20637247f888Srie 			}
20647247f888Srie 			free((void *)*oname);
20657247f888Srie 			*oname = aname;
20667c478bd9Sstevel@tonic-gate 		}
20677c478bd9Sstevel@tonic-gate 	}
20687247f888Srie 	return (1);
20697c478bd9Sstevel@tonic-gate }
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate /*
20727c478bd9Sstevel@tonic-gate  * Having loaded an object and created a link-map to describe it, finish
20737c478bd9Sstevel@tonic-gate  * processing this stage, including verifying any versioning requirements,
20747c478bd9Sstevel@tonic-gate  * updating the objects mode, creating a handle if necessary, and adding this
20757c478bd9Sstevel@tonic-gate  * object to existing handles if required.
20767c478bd9Sstevel@tonic-gate  */
2077390b98b5Srie static int
2078fb1354edSrie load_finish(Lm_list *lml, const char *name, Rt_map *clmp, int nmode,
2079fb1354edSrie     uint_t flags, Grp_hdl **hdl, Rt_map *nlmp)
20807c478bd9Sstevel@tonic-gate {
2081cce0e03bSab 	Aliste		idx;
2082cce0e03bSab 	Grp_hdl		*ghp;
20837c478bd9Sstevel@tonic-gate 	int		promote;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 	/*
20867c478bd9Sstevel@tonic-gate 	 * If this dependency is associated with a required version insure that
20877c478bd9Sstevel@tonic-gate 	 * the version is present in the loaded file.
20887c478bd9Sstevel@tonic-gate 	 */
20897c478bd9Sstevel@tonic-gate 	if (((rtld_flags & RT_FL_NOVERSION) == 0) &&
20907c478bd9Sstevel@tonic-gate 	    (FCT(clmp) == &elf_fct) && VERNEED(clmp) &&
20917c478bd9Sstevel@tonic-gate 	    (LM_VERIFY_VERS(clmp)(name, clmp, nlmp) == 0))
20927c478bd9Sstevel@tonic-gate 		return (0);
20937c478bd9Sstevel@tonic-gate 
20947c478bd9Sstevel@tonic-gate 	/*
20957c478bd9Sstevel@tonic-gate 	 * If this object has indicated that it should be isolated as a group
20967c478bd9Sstevel@tonic-gate 	 * (DT_FLAGS_1 contains DF_1_GROUP - object was built with -B group),
20977c478bd9Sstevel@tonic-gate 	 * or if the callers direct bindings indicate it should be isolated as
20987c478bd9Sstevel@tonic-gate 	 * a group (DYNINFO flags contains FLG_DI_GROUP - dependency followed
20997c478bd9Sstevel@tonic-gate 	 * -zgroupperm), establish the appropriate mode.
21007c478bd9Sstevel@tonic-gate 	 *
21017c478bd9Sstevel@tonic-gate 	 * The intent of an object defining itself as a group is to isolate the
21027c478bd9Sstevel@tonic-gate 	 * relocation of the group within its own members, however, unless
21037c478bd9Sstevel@tonic-gate 	 * opened through dlopen(), in which case we assume dlsym() will be used
21047c478bd9Sstevel@tonic-gate 	 * to located symbols in the new object, we still need to associate it
21057c478bd9Sstevel@tonic-gate 	 * with the caller for it to be bound with.  This is equivalent to a
21067c478bd9Sstevel@tonic-gate 	 * dlopen(RTLD_GROUP) and dlsym() using the returned handle.
21077c478bd9Sstevel@tonic-gate 	 */
21087c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) | flags) & FLG_RT_SETGROUP) {
21097c478bd9Sstevel@tonic-gate 		nmode &= ~RTLD_WORLD;
21107c478bd9Sstevel@tonic-gate 		nmode |= RTLD_GROUP;
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 		/*
21137c478bd9Sstevel@tonic-gate 		 * If the object wasn't explicitly dlopen()'ed associate it with
21147c478bd9Sstevel@tonic-gate 		 * the parent.
21157c478bd9Sstevel@tonic-gate 		 */
21169a411307Srie 		if ((flags & FLG_RT_HANDLE) == 0)
21177c478bd9Sstevel@tonic-gate 			nmode |= RTLD_PARENT;
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 
21207c478bd9Sstevel@tonic-gate 	/*
21217c478bd9Sstevel@tonic-gate 	 * Establish new mode and flags.
21227c478bd9Sstevel@tonic-gate 	 *
21237c478bd9Sstevel@tonic-gate 	 * For patch backward compatibility, the following use of update_mode()
21247c478bd9Sstevel@tonic-gate 	 * is disabled.
21257c478bd9Sstevel@tonic-gate 	 */
21267c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
21277c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DISFIX_1)
21287c478bd9Sstevel@tonic-gate 		promote = MODE(nlmp) |=
21297c478bd9Sstevel@tonic-gate 		    (nmode & ~(RTLD_PARENT | RTLD_NOLOAD | RTLD_FIRST));
21307c478bd9Sstevel@tonic-gate 	else
21317c478bd9Sstevel@tonic-gate #endif
21327c478bd9Sstevel@tonic-gate 		promote = update_mode(nlmp, MODE(nlmp), nmode);
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	FLAGS(nlmp) |= flags;
21357c478bd9Sstevel@tonic-gate 
21362926dd2eSrie 	/*
21372926dd2eSrie 	 * If this is a global object, ensure the associated link-map list can
21382926dd2eSrie 	 * be rescanned for global, lazy dependencies.
21392926dd2eSrie 	 */
21402926dd2eSrie 	if (MODE(nlmp) & RTLD_GLOBAL)
21412926dd2eSrie 		LIST(nlmp)->lm_flags &= ~LML_FLG_NOPENDGLBLAZY;
21422926dd2eSrie 
21437c478bd9Sstevel@tonic-gate 	/*
21447c478bd9Sstevel@tonic-gate 	 * If we've been asked to establish a handle create one for this object.
21457c478bd9Sstevel@tonic-gate 	 * Or, if this object has already been analyzed, but this reference
21467c478bd9Sstevel@tonic-gate 	 * requires that the mode of the object be promoted, also create a
21477c478bd9Sstevel@tonic-gate 	 * handle to propagate the new modes to all this objects dependencies.
21487c478bd9Sstevel@tonic-gate 	 */
21497c478bd9Sstevel@tonic-gate 	if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) || (promote &&
21507c478bd9Sstevel@tonic-gate 	    (FLAGS(nlmp) & FLG_RT_ANALYZED))) {
21518af2c5b9Srie 		uint_t	oflags, hflags = 0, cdflags;
21527c478bd9Sstevel@tonic-gate 
21538af2c5b9Srie 		/*
21548af2c5b9Srie 		 * Establish any flags for the handle (Grp_hdl).
21558af2c5b9Srie 		 *
21568af2c5b9Srie 		 *  .	Use of the RTLD_FIRST flag indicates that only the first
21578af2c5b9Srie 		 *	dependency on the handle (the new object) can be used
21588af2c5b9Srie 		 *	to satisfy dlsym() requests.
21598af2c5b9Srie 		 */
21607c478bd9Sstevel@tonic-gate 		if (nmode & RTLD_FIRST)
21618af2c5b9Srie 			hflags = GPH_FIRST;
21628af2c5b9Srie 
21638af2c5b9Srie 		/*
21648af2c5b9Srie 		 * Establish the flags for this callers dependency descriptor
21658af2c5b9Srie 		 * (Grp_desc).
21668af2c5b9Srie 		 *
21678af2c5b9Srie 		 *  .	The creation of a handle associated a descriptor for the
21688af2c5b9Srie 		 *	new object and descriptor for the parent (caller).
21698af2c5b9Srie 		 *	Typically, the handle is created for dlopen() or for
21708af2c5b9Srie 		 *	filtering.  A handle may also be created to promote
21718af2c5b9Srie 		 *	the callers modes (RTLD_NOW) to the new object.  In this
21728af2c5b9Srie 		 *	latter case, the handle/descriptor are torn down once
21738af2c5b9Srie 		 *	the mode propagation has occurred.
21748af2c5b9Srie 		 *
21758af2c5b9Srie 		 *  .	Use of the RTLD_PARENT flag indicates that the parent
21768af2c5b9Srie 		 *	can be relocated against.
21778af2c5b9Srie 		 */
21788af2c5b9Srie 		if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) == 0)
21798af2c5b9Srie 			cdflags = GPD_PROMOTE;
21808af2c5b9Srie 		else
21818af2c5b9Srie 			cdflags = GPD_PARENT;
21828af2c5b9Srie 		if (nmode & RTLD_PARENT)
21838af2c5b9Srie 			cdflags |= GPD_RELOC;
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate 		/*
21867c478bd9Sstevel@tonic-gate 		 * Now that a handle is being created, remove this state from
21877c478bd9Sstevel@tonic-gate 		 * the object so that it doesn't mistakenly get inherited by
21887c478bd9Sstevel@tonic-gate 		 * a dependency.
21897c478bd9Sstevel@tonic-gate 		 */
21907c478bd9Sstevel@tonic-gate 		oflags = FLAGS(nlmp);
21917c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) &= ~FLG_RT_HANDLE;
21927c478bd9Sstevel@tonic-gate 
21938af2c5b9Srie 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
21948af2c5b9Srie 		if ((ghp = hdl_create(lml, nlmp, clmp, hflags,
21958af2c5b9Srie 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), cdflags)) == 0)
21967c478bd9Sstevel@tonic-gate 			return (0);
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 		/*
21997c478bd9Sstevel@tonic-gate 		 * Add any dependencies that are already loaded, to the handle.
22007c478bd9Sstevel@tonic-gate 		 */
220102ca3e02Srie 		if (hdl_initialize(ghp, nlmp, nmode, promote) == 0)
22027c478bd9Sstevel@tonic-gate 			return (0);
22037c478bd9Sstevel@tonic-gate 
22047c478bd9Sstevel@tonic-gate 		if (hdl)
22057c478bd9Sstevel@tonic-gate 			*hdl = ghp;
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 		/*
22088af2c5b9Srie 		 * If we were asked to create a handle, we're done.
22097c478bd9Sstevel@tonic-gate 		 */
22107c478bd9Sstevel@tonic-gate 		if ((oflags | flags) & FLG_RT_HANDLE)
22117c478bd9Sstevel@tonic-gate 			return (1);
22127c478bd9Sstevel@tonic-gate 
22138af2c5b9Srie 		/*
22148af2c5b9Srie 		 * If the handle was created to promote modes from the parent
22158af2c5b9Srie 		 * (caller) to the new object, then this relationship needs to
22168af2c5b9Srie 		 * be removed to ensure the handle doesn't prevent the new
22178af2c5b9Srie 		 * objects from being deleted if required.  If the parent is
22188af2c5b9Srie 		 * the only dependency on the handle, then the handle can be
22198af2c5b9Srie 		 * completely removed.  However, the handle may have already
22208af2c5b9Srie 		 * existed, in which case only the parent descriptor can be
22218af2c5b9Srie 		 * deleted from the handle, or at least the GPD_PROMOTE flag
22228af2c5b9Srie 		 * removed from the descriptor.
22238af2c5b9Srie 		 *
22248af2c5b9Srie 		 * Fall through to carry out any group processing.
22258af2c5b9Srie 		 */
22268af2c5b9Srie 		free_hdl(ghp, clmp, GPD_PROMOTE);
22277c478bd9Sstevel@tonic-gate 	}
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	/*
22307c478bd9Sstevel@tonic-gate 	 * If the caller isn't part of a group we're done.
22317c478bd9Sstevel@tonic-gate 	 */
2232cce0e03bSab 	if (GROUPS(clmp) == NULL)
22337c478bd9Sstevel@tonic-gate 		return (1);
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	/*
22367c478bd9Sstevel@tonic-gate 	 * Determine if our caller is already associated with a handle, if so
22377c478bd9Sstevel@tonic-gate 	 * we need to add this object to any handles that already exist.
22387c478bd9Sstevel@tonic-gate 	 * Traverse the list of groups our caller is a member of and add this
22397c478bd9Sstevel@tonic-gate 	 * new link-map to those groups.
22407c478bd9Sstevel@tonic-gate 	 */
22418af2c5b9Srie 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
2242cce0e03bSab 	for (APLIST_TRAVERSE(GROUPS(clmp), idx, ghp)) {
2243cce0e03bSab 		Aliste		idx1;
22447c478bd9Sstevel@tonic-gate 		Grp_desc	*gdp;
22457c478bd9Sstevel@tonic-gate 		int		exist;
2246cce0e03bSab 		Rt_map		*dlmp1;
2247cce0e03bSab 		APlist		*lmalp = NULL;
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 		/*
22507c478bd9Sstevel@tonic-gate 		 * If the caller doesn't indicate that its dependencies should
22517c478bd9Sstevel@tonic-gate 		 * be added to a handle, ignore it.  This case identifies a
22527c478bd9Sstevel@tonic-gate 		 * parent of a dlopen(RTLD_PARENT) request.
22537c478bd9Sstevel@tonic-gate 		 */
2254cce0e03bSab 		for (ALIST_TRAVERSE(ghp->gh_depends, idx1, gdp)) {
22557c478bd9Sstevel@tonic-gate 			if (gdp->gd_depend == clmp)
22567c478bd9Sstevel@tonic-gate 				break;
22577c478bd9Sstevel@tonic-gate 		}
22587c478bd9Sstevel@tonic-gate 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
22597c478bd9Sstevel@tonic-gate 			continue;
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 		if ((exist = hdl_add(ghp, nlmp,
2262efb9e8b8Srie 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0)
22637c478bd9Sstevel@tonic-gate 			return (0);
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 		/*
22667c478bd9Sstevel@tonic-gate 		 * If this member already exists then its dependencies will
22677c478bd9Sstevel@tonic-gate 		 * have already been processed.
22687c478bd9Sstevel@tonic-gate 		 */
22697c478bd9Sstevel@tonic-gate 		if (exist == ALE_EXISTS)
22707c478bd9Sstevel@tonic-gate 			continue;
22717c478bd9Sstevel@tonic-gate 
22727c478bd9Sstevel@tonic-gate 		/*
22737c478bd9Sstevel@tonic-gate 		 * If the object we've added has just been opened, it will not
22747c478bd9Sstevel@tonic-gate 		 * yet have been processed for its dependencies, these will be
22757c478bd9Sstevel@tonic-gate 		 * added on later calls to load_one().  If it doesn't have any
22767c478bd9Sstevel@tonic-gate 		 * dependencies we're also done.
22777c478bd9Sstevel@tonic-gate 		 */
22787c478bd9Sstevel@tonic-gate 		if (((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0) ||
2279cce0e03bSab 		    (DEPENDS(nlmp) == NULL))
22807c478bd9Sstevel@tonic-gate 			continue;
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 		/*
22837c478bd9Sstevel@tonic-gate 		 * Otherwise, this object exists and has dependencies, so add
22847c478bd9Sstevel@tonic-gate 		 * all of its dependencies to the handle were operating on.
22857c478bd9Sstevel@tonic-gate 		 */
2286cce0e03bSab 		if (aplist_append(&lmalp, nlmp, AL_CNT_DEPCLCT) == 0)
22877c478bd9Sstevel@tonic-gate 			return (0);
22887c478bd9Sstevel@tonic-gate 
2289cce0e03bSab 		for (APLIST_TRAVERSE(lmalp, idx1, dlmp1)) {
2290cce0e03bSab 			Aliste		idx2;
2291cce0e03bSab 			Bnd_desc 	*bdp;
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 			/*
22947c478bd9Sstevel@tonic-gate 			 * Add any dependencies of this dependency to the
22957c478bd9Sstevel@tonic-gate 			 * dynamic dependency list so they can be further
22967c478bd9Sstevel@tonic-gate 			 * processed.
22977c478bd9Sstevel@tonic-gate 			 */
2298cce0e03bSab 			for (APLIST_TRAVERSE(DEPENDS(dlmp1), idx2, bdp)) {
2299aa736cbeSrie 				Rt_map	*dlmp2 = bdp->b_depend;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 				if ((bdp->b_flags & BND_NEEDED) == 0)
23027c478bd9Sstevel@tonic-gate 					continue;
23037c478bd9Sstevel@tonic-gate 
2304cce0e03bSab 				if (aplist_test(&lmalp, dlmp2,
23057c478bd9Sstevel@tonic-gate 				    AL_CNT_DEPCLCT) == 0) {
23067c478bd9Sstevel@tonic-gate 					free(lmalp);
23077c478bd9Sstevel@tonic-gate 					return (0);
23087c478bd9Sstevel@tonic-gate 				}
23097c478bd9Sstevel@tonic-gate 			}
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate 			if (nlmp == dlmp1)
23127c478bd9Sstevel@tonic-gate 				continue;
23137c478bd9Sstevel@tonic-gate 
23147c478bd9Sstevel@tonic-gate 			if ((exist = hdl_add(ghp, dlmp1,
2315efb9e8b8Srie 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) != 0) {
23167247f888Srie 				if (exist == ALE_CREATE) {
23177247f888Srie 					(void) update_mode(dlmp1, MODE(dlmp1),
23187247f888Srie 					    nmode);
23197247f888Srie 				}
23207c478bd9Sstevel@tonic-gate 				continue;
23217c478bd9Sstevel@tonic-gate 			}
23227c478bd9Sstevel@tonic-gate 			free(lmalp);
23237c478bd9Sstevel@tonic-gate 			return (0);
23247c478bd9Sstevel@tonic-gate 		}
23257c478bd9Sstevel@tonic-gate 		free(lmalp);
23267c478bd9Sstevel@tonic-gate 	}
23277c478bd9Sstevel@tonic-gate 	return (1);
23287c478bd9Sstevel@tonic-gate }
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate /*
23317c478bd9Sstevel@tonic-gate  * The central routine for loading shared objects.  Insures ldd() diagnostics,
23327c478bd9Sstevel@tonic-gate  * handles and any other related additions are all done in one place.
23337c478bd9Sstevel@tonic-gate  */
2334fb1354edSrie static Rt_map *
23357247f888Srie _load_path(Lm_list *lml, Aliste lmco, const char **oname, Rt_map *clmp,
23369aa23310Srie     int nmode, uint_t flags, Grp_hdl ** hdl, Fdesc *nfdp, Rej_desc *rej,
23379aa23310Srie     int *in_nfavl)
23387c478bd9Sstevel@tonic-gate {
23397247f888Srie 	Rt_map		*nlmp;
23407247f888Srie 	const char	*name = *oname;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	if ((nmode & RTLD_NOLOAD) == 0) {
23437c478bd9Sstevel@tonic-gate 		/*
23447c478bd9Sstevel@tonic-gate 		 * If this isn't a noload request attempt to load the file.
23457247f888Srie 		 * Note, the name of the file may be changed by an auditor.
23467c478bd9Sstevel@tonic-gate 		 */
23477247f888Srie 		if ((load_trace(lml, oname, clmp)) == 0)
23487c478bd9Sstevel@tonic-gate 			return (0);
23497c478bd9Sstevel@tonic-gate 
23507247f888Srie 		name = *oname;
23517247f888Srie 
23527c478bd9Sstevel@tonic-gate 		if ((nlmp = load_so(lml, lmco, name, clmp, flags,
23539aa23310Srie 		    nfdp, rej, in_nfavl)) == 0)
23547c478bd9Sstevel@tonic-gate 			return (0);
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 		/*
23577c478bd9Sstevel@tonic-gate 		 * If we've loaded a library which identifies itself as not
23587c478bd9Sstevel@tonic-gate 		 * being dlopen()'able catch it here.  Let non-dlopen()'able
23597c478bd9Sstevel@tonic-gate 		 * objects through under RTLD_CONFGEN as they're only being
23607c478bd9Sstevel@tonic-gate 		 * mapped to be dldump()'ed.
23617c478bd9Sstevel@tonic-gate 		 */
23627c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) && ((FLAGS(nlmp) &
23637c478bd9Sstevel@tonic-gate 		    (FLG_RT_NOOPEN | FLG_RT_RELOCED)) == FLG_RT_NOOPEN) &&
23647c478bd9Sstevel@tonic-gate 		    ((nmode & RTLD_CONFGEN) == 0)) {
23657c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
23687c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
23697c478bd9Sstevel@tonic-gate 			_rej.rej_str = MSG_INTL(MSG_GEN_NOOPEN);
2370ba2be530Sab 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
237131fdd7caSab 			rejection_inherit(rej, &_rej);
23727c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
23737c478bd9Sstevel@tonic-gate 			return (0);
23747c478bd9Sstevel@tonic-gate 		}
23757c478bd9Sstevel@tonic-gate 	} else {
23767c478bd9Sstevel@tonic-gate 		/*
23777c478bd9Sstevel@tonic-gate 		 * If it's a NOLOAD request - check to see if the object
23787c478bd9Sstevel@tonic-gate 		 * has already been loaded.
23797c478bd9Sstevel@tonic-gate 		 */
23807c478bd9Sstevel@tonic-gate 		/* LINTED */
23819aa23310Srie 		if (nlmp = is_so_loaded(lml, name, in_nfavl)) {
23827c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
23837c478bd9Sstevel@tonic-gate 			    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
23847c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name,
23857247f888Srie 				    NAME(clmp));
23867247f888Srie 				/* BEGIN CSTYLED */
23877c478bd9Sstevel@tonic-gate 				if (*name == '/')
23887c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
23897c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
23907c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
23917c478bd9Sstevel@tonic-gate 				else
23927c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
23937c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
23947c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
23957c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
23967247f888Srie 				/* END CSTYLED */
23977c478bd9Sstevel@tonic-gate 			}
23987c478bd9Sstevel@tonic-gate 		} else {
23997c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
24027c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
24037c478bd9Sstevel@tonic-gate 			_rej.rej_str = strerror(ENOENT);
2404ba2be530Sab 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
240531fdd7caSab 			rejection_inherit(rej, &_rej);
24067c478bd9Sstevel@tonic-gate 			return (0);
24077c478bd9Sstevel@tonic-gate 		}
24087c478bd9Sstevel@tonic-gate 	}
24097c478bd9Sstevel@tonic-gate 
24107c478bd9Sstevel@tonic-gate 	/*
24117c478bd9Sstevel@tonic-gate 	 * Finish processing this loaded object.
24127c478bd9Sstevel@tonic-gate 	 */
2413390b98b5Srie 	if (load_finish(lml, name, clmp, nmode, flags, hdl, nlmp) == 0) {
2414390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2415390b98b5Srie 
2416390b98b5Srie 		/*
2417390b98b5Srie 		 * If this object has already been analyzed, then it is in use,
2418390b98b5Srie 		 * so even though this operation has failed, it should not be
2419390b98b5Srie 		 * torn down.
2420390b98b5Srie 		 */
2421390b98b5Srie 		if ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)
2422390b98b5Srie 			remove_so(lml, nlmp);
2423390b98b5Srie 		return (0);
2424390b98b5Srie 	}
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 	/*
2427390b98b5Srie 	 * If this object is new, and we're being audited, tell the audit
2428390b98b5Srie 	 * library of the file we've just opened.  Note, if the new link-map
2429390b98b5Srie 	 * requires local auditing of its dependencies we also register its
2430390b98b5Srie 	 * opening.
24317c478bd9Sstevel@tonic-gate 	 */
2432390b98b5Srie 	if (FLAGS(nlmp) & FLG_RT_NEWLOAD) {
2433390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2434390b98b5Srie 
2435390b98b5Srie 		if (((lml->lm_tflags | FLAGS1(clmp) | FLAGS1(nlmp)) &
2436390b98b5Srie 		    LML_TFLG_AUD_MASK) && (((lml->lm_flags |
2437390b98b5Srie 		    LIST(clmp)->lm_flags) & LML_FLG_NOAUDIT) == 0)) {
2438390b98b5Srie 			if (audit_objopen(clmp, nlmp) == 0) {
2439390b98b5Srie 				remove_so(lml, nlmp);
2440390b98b5Srie 				return (0);
2441390b98b5Srie 			}
2442390b98b5Srie 		}
2443390b98b5Srie 	}
2444390b98b5Srie 	return (nlmp);
24457c478bd9Sstevel@tonic-gate }
24467c478bd9Sstevel@tonic-gate 
2447fb1354edSrie Rt_map *
24489aa23310Srie load_path(Lm_list *lml, Aliste lmco, const char **name, Rt_map *clmp, int nmode,
24499aa23310Srie     uint_t flags, Grp_hdl **hdl, Fdesc *cfdp, Rej_desc *rej, int *in_nfavl)
2450fb1354edSrie {
2451fb1354edSrie 	Rt_map	*lmp;
2452fb1354edSrie 	Fdesc	nfdp = { 0 };
2453fb1354edSrie 
2454fb1354edSrie 	/*
2455fb1354edSrie 	 * If this path resulted from a $HWCAP specification, then the best
2456fb1354edSrie 	 * hardware capability object has already been establish, and is
2457fb1354edSrie 	 * available in the calling file descriptor.
2458fb1354edSrie 	 */
2459fb1354edSrie 	if (flags & FLG_RT_HWCAP) {
2460fb1354edSrie 		if (cfdp->fd_lmp == 0) {
2461fb1354edSrie 			/*
2462fb1354edSrie 			 * If this object hasn't yet been mapped, re-establish
2463fb1354edSrie 			 * the file descriptor structure to reflect this objects
2464fb1354edSrie 			 * original initial page mapping.  Make sure any present
2465fb1354edSrie 			 * file descriptor mapping is removed before overwriting
2466fb1354edSrie 			 * the structure.
2467fb1354edSrie 			 */
2468fb1354edSrie #if	defined(MAP_ALIGN)
2469fb1354edSrie 			if (fmap->fm_maddr &&
2470fb1354edSrie 			    ((fmap->fm_mflags & MAP_ALIGN) == 0))
2471fb1354edSrie #else
2472fb1354edSrie 			if (fmap->fm_maddr)
2473fb1354edSrie #endif
2474fb1354edSrie 				(void) munmap(fmap->fm_maddr, fmap->fm_msize);
247546d4d531Srie 
247646d4d531Srie 			*fmap = cfdp->fd_fmap;
2477fb1354edSrie 		}
2478fb1354edSrie 		nfdp = *cfdp;
2479fb1354edSrie 	}
2480fb1354edSrie 
24819aa23310Srie 	lmp = _load_path(lml, lmco, name, clmp, nmode, flags, hdl, &nfdp,
24829aa23310Srie 	    rej, in_nfavl);
2483fb1354edSrie 
2484fb1354edSrie 	/*
2485fb1354edSrie 	 * If this path originated from a $HWCAP specification, re-establish the
2486fb1354edSrie 	 * fdesc information.  For single paged objects, such as filters, the
2487fb1354edSrie 	 * original mapping may have been sufficient to capture the file, thus
2488fb1354edSrie 	 * this mapping needs to be reset to insure it doesn't mistakenly get
2489fb1354edSrie 	 * unmapped as part of HWCAP cleanup.
2490fb1354edSrie 	 */
249146d4d531Srie 	if ((flags & FLG_RT_HWCAP) && (cfdp->fd_lmp == 0)) {
2492fb1354edSrie 		cfdp->fd_fmap.fm_maddr = fmap->fm_maddr;
2493fb1354edSrie 		cfdp->fd_fmap.fm_mflags = fmap->fm_mflags;
2494fb1354edSrie 		cfdp->fd_fd = nfdp.fd_fd;
2495fb1354edSrie 	}
2496fb1354edSrie 
2497fb1354edSrie 	return (lmp);
2498fb1354edSrie }
2499fb1354edSrie 
25007c478bd9Sstevel@tonic-gate /*
25017c478bd9Sstevel@tonic-gate  * Load one object from a possible list of objects.  Typically, for requests
25027c478bd9Sstevel@tonic-gate  * such as NEEDED's, only one object is specified.  However, this object could
25037c478bd9Sstevel@tonic-gate  * be specified using $ISALIST or $HWCAP, in which case only the first object
25047c478bd9Sstevel@tonic-gate  * that can be loaded is used (ie. the best).
25057c478bd9Sstevel@tonic-gate  */
25067c478bd9Sstevel@tonic-gate Rt_map *
25077c478bd9Sstevel@tonic-gate load_one(Lm_list *lml, Aliste lmco, Pnode *pnp, Rt_map *clmp, int mode,
25089aa23310Srie     uint_t flags, Grp_hdl **hdl, int *in_nfavl)
25097c478bd9Sstevel@tonic-gate {
25107c478bd9Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
25117c478bd9Sstevel@tonic-gate 	Pnode   	*tpnp;
25127c478bd9Sstevel@tonic-gate 	const char	*name;
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 	for (tpnp = pnp; tpnp && tpnp->p_name; tpnp = tpnp->p_next) {
25157247f888Srie 		Rt_map	*tlmp;
25167247f888Srie 
25177c478bd9Sstevel@tonic-gate 		/*
25187c478bd9Sstevel@tonic-gate 		 * A Hardware capabilities requirement can itself expand into
25197c478bd9Sstevel@tonic-gate 		 * a number of candidates.
25207c478bd9Sstevel@tonic-gate 		 */
25217c478bd9Sstevel@tonic-gate 		if (tpnp->p_orig & PN_TKN_HWCAP) {
25227c478bd9Sstevel@tonic-gate 			if ((tlmp = load_hwcap(lml, lmco, tpnp->p_name, clmp,
25239aa23310Srie 			    mode, (flags | FLG_RT_HWCAP), hdl, &rej,
25249aa23310Srie 			    in_nfavl)) != 0) {
25257c478bd9Sstevel@tonic-gate 				remove_rej(&rej);
25267c478bd9Sstevel@tonic-gate 				return (tlmp);
25277c478bd9Sstevel@tonic-gate 			}
25287c478bd9Sstevel@tonic-gate 		} else {
25297247f888Srie 			if ((tlmp = load_path(lml, lmco, &tpnp->p_name, clmp,
25309aa23310Srie 			    mode, flags, hdl, 0, &rej, in_nfavl)) != 0) {
25317c478bd9Sstevel@tonic-gate 				remove_rej(&rej);
25327c478bd9Sstevel@tonic-gate 				return (tlmp);
25337c478bd9Sstevel@tonic-gate 			}
25347c478bd9Sstevel@tonic-gate 		}
25357c478bd9Sstevel@tonic-gate 	}
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	/*
25387c478bd9Sstevel@tonic-gate 	 * If this pathname originated from an expanded token, use the original
25397c478bd9Sstevel@tonic-gate 	 * for any diagnostic output.
25407c478bd9Sstevel@tonic-gate 	 */
25417c478bd9Sstevel@tonic-gate 	if ((name = pnp->p_oname) == 0)
25427c478bd9Sstevel@tonic-gate 		name = pnp->p_name;
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	file_notfound(lml, name, clmp, flags, &rej);
25457c478bd9Sstevel@tonic-gate 	remove_rej(&rej);
25467c478bd9Sstevel@tonic-gate 	return (0);
25477c478bd9Sstevel@tonic-gate }
25487c478bd9Sstevel@tonic-gate 
25499a411307Srie /*
25509a411307Srie  * Determine whether a symbol is defined as an interposer.
25519a411307Srie  */
25529a411307Srie int
25539a411307Srie is_sym_interposer(Rt_map *lmp, Sym *sym)
25549a411307Srie {
25559a411307Srie 	Syminfo	*sip = SYMINFO(lmp);
25569a411307Srie 
25579a411307Srie 	if (sip) {
25589a411307Srie 		ulong_t	ndx;
25599a411307Srie 
25609a411307Srie 		ndx = (((ulong_t)sym - (ulong_t)SYMTAB(lmp)) / SYMENT(lmp));
25619a411307Srie 		/* LINTED */
25629a411307Srie 		sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(lmp)));
25639a411307Srie 		if (sip->si_flags & SYMINFO_FLG_INTERPOSE)
25649a411307Srie 			return (1);
25659a411307Srie 	}
25669a411307Srie 	return (0);
25679a411307Srie }
25689a411307Srie 
25697c478bd9Sstevel@tonic-gate /*
25707c478bd9Sstevel@tonic-gate  * While processing direct or group bindings, determine whether the object to
25717c478bd9Sstevel@tonic-gate  * which we've bound can be interposed upon.  In this context, copy relocations
25727c478bd9Sstevel@tonic-gate  * are a form of interposition.
25737c478bd9Sstevel@tonic-gate  */
25747c478bd9Sstevel@tonic-gate static Sym *
2575adbfe822Srie lookup_sym_interpose(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Sym *osym,
2576adbfe822Srie     int *in_nfavl)
25777c478bd9Sstevel@tonic-gate {
2578adbfe822Srie 	Rt_map		*lmp, *clmp;
25797c478bd9Sstevel@tonic-gate 	Slookup		sl;
2580adbfe822Srie 	Lm_list		*lml;
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	/*
25837c478bd9Sstevel@tonic-gate 	 * If we've bound to a copy relocation definition then we need to assign
25847c478bd9Sstevel@tonic-gate 	 * this binding to the original copy reference.  Fabricate an inter-
25857c478bd9Sstevel@tonic-gate 	 * position diagnostic, as this is a legitimate form of interposition.
25867c478bd9Sstevel@tonic-gate 	 */
2587adbfe822Srie 	if (osym && (FLAGS1(*dlmp) & FL1_RT_COPYTOOK)) {
25887c478bd9Sstevel@tonic-gate 		Rel_copy	*rcp;
2589cce0e03bSab 		Aliste		idx;
25907c478bd9Sstevel@tonic-gate 
2591cce0e03bSab 		for (ALIST_TRAVERSE(COPY_R(*dlmp), idx, rcp)) {
2592aa736cbeSrie 			if ((osym == rcp->r_dsym) || (osym->st_value &&
2593aa736cbeSrie 			    (osym->st_value == rcp->r_dsym->st_value))) {
25947c478bd9Sstevel@tonic-gate 				*dlmp = rcp->r_rlmp;
25957c478bd9Sstevel@tonic-gate 				*binfo |=
25967c478bd9Sstevel@tonic-gate 				    (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
25977c478bd9Sstevel@tonic-gate 				return (rcp->r_rsym);
25987c478bd9Sstevel@tonic-gate 			}
25997c478bd9Sstevel@tonic-gate 		}
26007c478bd9Sstevel@tonic-gate 	}
26017c478bd9Sstevel@tonic-gate 
2602adbfe822Srie 	/*
2603adbfe822Srie 	 * If a symbol binding has been established, inspect the link-map list
2604adbfe822Srie 	 * of the destination object, otherwise use the link-map list of the
2605adbfe822Srie 	 * original caller.
2606adbfe822Srie 	 */
2607adbfe822Srie 	if (osym)
2608adbfe822Srie 		clmp = *dlmp;
2609adbfe822Srie 	else
2610adbfe822Srie 		clmp = slp->sl_cmap;
2611adbfe822Srie 
2612adbfe822Srie 	lml = LIST(clmp);
2613adbfe822Srie 	lmp = lml->lm_head;
2614adbfe822Srie 
2615aa736cbeSrie 	/*
2616aa736cbeSrie 	 * Prior to Solaris 8, external references from an executable that were
2617aa736cbeSrie 	 * bound to an uninitialized variable (.bss) within a shared object did
2618aa736cbeSrie 	 * not establish a copy relocation.  This was thought to be an
2619aa736cbeSrie 	 * optimization, to prevent copying zero's to zero's.  Typically,
2620aa736cbeSrie 	 * interposition took its course, with the shared object binding to the
2621aa736cbeSrie 	 * executables data definition.
2622aa736cbeSrie 	 *
2623aa736cbeSrie 	 * This scenario can be broken when this old executable runs against a
2624aa736cbeSrie 	 * new shared object that is directly bound.  With no copy-relocation
2625aa736cbeSrie 	 * record, ld.so.1 has no data to trigger the normal vectoring of the
2626aa736cbeSrie 	 * binding to the executable.
2627aa736cbeSrie 	 *
2628aa736cbeSrie 	 * Starting with Solaris 8, a DT_FLAGS entry is written to all objects,
2629aa736cbeSrie 	 * regardless of there being any DF_ flags entries.  Therefore, an
2630aa736cbeSrie 	 * object without this dynamic tag is susceptible to the copy relocation
2631aa736cbeSrie 	 * issue.  If the executable has no DT_FLAGS tag, and contains the same
2632aa736cbeSrie 	 * .bss symbol definition as has been directly bound to, redirect the
2633aa736cbeSrie 	 * binding to the executables data definition.
2634aa736cbeSrie 	 */
2635adbfe822Srie 	if (osym && ((FLAGS2(lmp) & FL2_RT_DTFLAGS) == 0) &&
2636adbfe822Srie 	    (FCT(lmp) == &elf_fct) &&
2637466e2a62Srie 	    (ELF_ST_TYPE(osym->st_info) != STT_FUNC) &&
2638466e2a62Srie 	    are_bits_zero(*dlmp, osym, 0)) {
2639aa736cbeSrie 		Rt_map	*ilmp;
2640aa736cbeSrie 		Sym	*isym;
2641aa736cbeSrie 
2642adbfe822Srie 		sl = *slp;
2643adbfe822Srie 		sl.sl_imap = lmp;
2644adbfe822Srie 
2645aa736cbeSrie 		/*
2646aa736cbeSrie 		 * Determine whether the same symbol name exists within the
2647aa736cbeSrie 		 * executable, that the size and type of symbol are the same,
2648aa736cbeSrie 		 * and that the symbol is also associated with .bss.
2649aa736cbeSrie 		 */
26509aa23310Srie 		if (((isym = SYMINTP(lmp)(&sl, &ilmp, binfo,
26519aa23310Srie 		    in_nfavl)) != NULL) && (isym->st_size == osym->st_size) &&
2652aa736cbeSrie 		    (isym->st_info == osym->st_info) &&
2653466e2a62Srie 		    are_bits_zero(lmp, isym, 1)) {
2654aa736cbeSrie 			*dlmp = lmp;
2655aa736cbeSrie 			*binfo |= (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
2656aa736cbeSrie 			return (isym);
2657aa736cbeSrie 		}
2658aa736cbeSrie 	}
2659aa736cbeSrie 
26607c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_INTRPOSE) == 0)
26617c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
26627c478bd9Sstevel@tonic-gate 
26637c478bd9Sstevel@tonic-gate 	/*
26647c478bd9Sstevel@tonic-gate 	 * Traverse the list of known interposers to determine whether any
26657c478bd9Sstevel@tonic-gate 	 * offer the same symbol.  Note, the head of the link-map could be
2666aa736cbeSrie 	 * identified as an interposer.  Otherwise, skip the head of the
2667aa736cbeSrie 	 * link-map, so that we don't bind to any .plt references, or
2668aa736cbeSrie 	 * copy-relocation destinations unintentionally.
26697c478bd9Sstevel@tonic-gate 	 */
26707c478bd9Sstevel@tonic-gate 	lmp = lml->lm_head;
26717c478bd9Sstevel@tonic-gate 	sl = *slp;
2672adbfe822Srie 
26739a411307Srie 	if (((FLAGS(lmp) & MSK_RT_INTPOSE) == 0) || (sl.sl_flags & LKUP_COPY))
26747c478bd9Sstevel@tonic-gate 		lmp = (Rt_map *)NEXT(lmp);
26757c478bd9Sstevel@tonic-gate 
26767c478bd9Sstevel@tonic-gate 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
26777c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_DELETE)
26787c478bd9Sstevel@tonic-gate 			continue;
26799a411307Srie 		if ((FLAGS(lmp) & MSK_RT_INTPOSE) == 0)
26807c478bd9Sstevel@tonic-gate 			break;
26817c478bd9Sstevel@tonic-gate 
2682adbfe822Srie 		if (callable(lmp, clmp, 0, sl.sl_flags)) {
26839a411307Srie 			Rt_map	*ilmp;
2684aa736cbeSrie 			Sym	*isym;
26859a411307Srie 
26867c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
26879aa23310Srie 			if (isym = SYMINTP(lmp)(&sl, &ilmp, binfo, in_nfavl)) {
26889a411307Srie 				/*
26899a411307Srie 				 * If this object provides individual symbol
26909a411307Srie 				 * interposers, make sure that the symbol we
26919a411307Srie 				 * have found is tagged as an interposer.
26929a411307Srie 				 */
26939a411307Srie 				if ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
2694aa736cbeSrie 				    (is_sym_interposer(ilmp, isym) == 0))
26959a411307Srie 					continue;
26969a411307Srie 
26979a411307Srie 				/*
26989a411307Srie 				 * Indicate this binding has occurred to an
26999a411307Srie 				 * interposer, and return the symbol.
27009a411307Srie 				 */
27017c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
27029a411307Srie 				*dlmp = ilmp;
2703aa736cbeSrie 				return (isym);
27047c478bd9Sstevel@tonic-gate 			}
27057c478bd9Sstevel@tonic-gate 		}
27067c478bd9Sstevel@tonic-gate 	}
27077c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
27087c478bd9Sstevel@tonic-gate }
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate /*
27117c478bd9Sstevel@tonic-gate  * If an object specifies direct bindings (it contains a syminfo structure
27127c478bd9Sstevel@tonic-gate  * describing where each binding was established during link-editing, and the
27137c478bd9Sstevel@tonic-gate  * object was built -Bdirect), then look for the symbol in the specific object.
27147c478bd9Sstevel@tonic-gate  */
27157c478bd9Sstevel@tonic-gate static Sym *
27167c478bd9Sstevel@tonic-gate lookup_sym_direct(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Syminfo *sip,
27179aa23310Srie     Rt_map *lmp, int *in_nfavl)
27187c478bd9Sstevel@tonic-gate {
27197c478bd9Sstevel@tonic-gate 	Rt_map	*clmp = slp->sl_cmap;
27207c478bd9Sstevel@tonic-gate 	Sym	*sym;
27217c478bd9Sstevel@tonic-gate 	Slookup	sl;
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	/*
27247c478bd9Sstevel@tonic-gate 	 * If a direct binding resolves to the definition of a copy relocated
27257c478bd9Sstevel@tonic-gate 	 * variable, it must be redirected to the copy (in the executable) that
27267c478bd9Sstevel@tonic-gate 	 * will eventually be made.  Typically, this redirection occurs in
27277c478bd9Sstevel@tonic-gate 	 * lookup_sym_interpose().  But, there's an edge condition.  If a
27287c478bd9Sstevel@tonic-gate 	 * directly bound executable contains pic code, there may be a
27297c478bd9Sstevel@tonic-gate 	 * reference to a definition that will eventually have a copy made.
27307c478bd9Sstevel@tonic-gate 	 * However, this copy relocation may not yet have occurred, because
27317c478bd9Sstevel@tonic-gate 	 * the relocation making this reference comes before the relocation
27327c478bd9Sstevel@tonic-gate 	 * that will create the copy.
27337c478bd9Sstevel@tonic-gate 	 * Under direct bindings, the syminfo indicates that a copy will be
27347c478bd9Sstevel@tonic-gate 	 * taken (SYMINFO_FLG_COPY).  This can only be set in an executable.
27357c478bd9Sstevel@tonic-gate 	 * Thus, the caller must be the executable, so bind to the destination
27367c478bd9Sstevel@tonic-gate 	 * of the copy within the executable.
27377c478bd9Sstevel@tonic-gate 	 */
27387c478bd9Sstevel@tonic-gate 	if (((slp->sl_flags & LKUP_COPY) == 0) &&
27397c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_COPY)) {
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 		slp->sl_imap = LIST(clmp)->lm_head;
27429aa23310Srie 		if (sym = SYMINTP(clmp)(slp, dlmp, binfo, in_nfavl))
27437c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_DIRECT | DBG_BINFO_COPYREF);
27447c478bd9Sstevel@tonic-gate 		return (sym);
27457c478bd9Sstevel@tonic-gate 	}
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 	/*
2748efb9e8b8Srie 	 * If we need to directly bind to our parent, start looking in each
2749efb9e8b8Srie 	 * callers link map.
27507c478bd9Sstevel@tonic-gate 	 */
27517c478bd9Sstevel@tonic-gate 	sl = *slp;
27527c478bd9Sstevel@tonic-gate 	sl.sl_flags |= LKUP_DIRECT;
2753adbfe822Srie 	sym = NULL;
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 	if (sip->si_boundto == SYMINFO_BT_PARENT) {
2756cce0e03bSab 		Aliste		idx1;
2757cce0e03bSab 		Bnd_desc	*bdp;
2758cce0e03bSab 		Grp_hdl		*ghp;
27597c478bd9Sstevel@tonic-gate 
2760efb9e8b8Srie 		/*
2761efb9e8b8Srie 		 * Determine the parent of this explicit dependency from its
2762efb9e8b8Srie 		 * CALLERS()'s list.
2763efb9e8b8Srie 		 */
2764cce0e03bSab 		for (APLIST_TRAVERSE(CALLERS(clmp), idx1, bdp)) {
2765cce0e03bSab 			sl.sl_imap = lmp = bdp->b_caller;
27669aa23310Srie 			if ((sym = SYMINTP(lmp)(&sl, dlmp, binfo,
27679aa23310Srie 			    in_nfavl)) != NULL)
2768efb9e8b8Srie 				goto found;
2769efb9e8b8Srie 		}
2770efb9e8b8Srie 
2771efb9e8b8Srie 		/*
2772efb9e8b8Srie 		 * A caller can also be defined as the parent of a dlopen()
2773efb9e8b8Srie 		 * call.  Determine whether this object has any handles.  The
2774efb9e8b8Srie 		 * dependencies maintained with the handle represent the
2775efb9e8b8Srie 		 * explicit dependencies of the dlopen()'ed object, and the
2776efb9e8b8Srie 		 * calling parent.
2777efb9e8b8Srie 		 */
2778cce0e03bSab 		for (APLIST_TRAVERSE(HANDLES(clmp), idx1, ghp)) {
2779efb9e8b8Srie 			Grp_desc	*gdp;
2780cce0e03bSab 			Aliste		idx2;
2781efb9e8b8Srie 
2782cce0e03bSab 			for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
2783efb9e8b8Srie 				if ((gdp->gd_flags & GPD_PARENT) == 0)
2784efb9e8b8Srie 					continue;
2785efb9e8b8Srie 				sl.sl_imap = lmp = gdp->gd_depend;
2786aa736cbeSrie 				if ((sym = SYMINTP(lmp)(&sl, dlmp,
27879aa23310Srie 				    binfo, in_nfavl)) != NULL)
2788efb9e8b8Srie 					goto found;
2789efb9e8b8Srie 			}
27907c478bd9Sstevel@tonic-gate 		}
27917c478bd9Sstevel@tonic-gate 	} else {
27927c478bd9Sstevel@tonic-gate 		/*
27937c478bd9Sstevel@tonic-gate 		 * If we need to direct bind to anything else look in the
27947c478bd9Sstevel@tonic-gate 		 * link map associated with this symbol reference.
27957c478bd9Sstevel@tonic-gate 		 */
27967c478bd9Sstevel@tonic-gate 		if (sip->si_boundto == SYMINFO_BT_SELF)
27977c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp = clmp;
27987c478bd9Sstevel@tonic-gate 		else
27997c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 		if (lmp)
28029aa23310Srie 			sym = SYMINTP(lmp)(&sl, dlmp, binfo, in_nfavl);
28037c478bd9Sstevel@tonic-gate 	}
2804efb9e8b8Srie found:
28057c478bd9Sstevel@tonic-gate 	if (sym)
28067c478bd9Sstevel@tonic-gate 		*binfo |= DBG_BINFO_DIRECT;
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	/*
2809adbfe822Srie 	 * If a reference to a directly bound symbol can't be satisfied, then
2810adbfe822Srie 	 * determine whether an interposer can provide the missing symbol.  If
2811adbfe822Srie 	 * a reference to a directly bound symbol is satisfied, then determine
2812adbfe822Srie 	 * whether that object can be interposed upon for this symbol.
28137c478bd9Sstevel@tonic-gate 	 */
2814adbfe822Srie 	if ((sym == NULL) || ((LIST(*dlmp)->lm_head != *dlmp) &&
2815adbfe822Srie 	    (LIST(*dlmp) == LIST(clmp)))) {
2816aa736cbeSrie 		Sym	*isym;
28177c478bd9Sstevel@tonic-gate 
2818adbfe822Srie 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
2819adbfe822Srie 		    in_nfavl)) != 0)
28207c478bd9Sstevel@tonic-gate 			return (isym);
28217c478bd9Sstevel@tonic-gate 	}
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	return (sym);
28247c478bd9Sstevel@tonic-gate }
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate static Sym *
282760758829Srie core_lookup_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
28289aa23310Srie     Aliste off, int *in_nfavl)
28297c478bd9Sstevel@tonic-gate {
28307c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	/*
28337c478bd9Sstevel@tonic-gate 	 * Copy relocations should start their search after the head of the
28347c478bd9Sstevel@tonic-gate 	 * main link-map control list.
28357c478bd9Sstevel@tonic-gate 	 */
2836cce0e03bSab 	if ((off == ALIST_OFF_DATA) && (slp->sl_flags & LKUP_COPY) && ilmp)
28377c478bd9Sstevel@tonic-gate 		lmp = (Rt_map *)NEXT(ilmp);
28387c478bd9Sstevel@tonic-gate 	else
28397c478bd9Sstevel@tonic-gate 		lmp = ilmp;
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
284260758829Srie 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
28437c478bd9Sstevel@tonic-gate 			Sym	*sym;
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
28469aa23310Srie 			if (((sym = SYMINTP(lmp)(slp, dlmp, binfo,
28479aa23310Srie 			    in_nfavl)) != NULL) || (*binfo & BINFO_REJSINGLE))
28487c478bd9Sstevel@tonic-gate 				return (sym);
28497c478bd9Sstevel@tonic-gate 		}
28507c478bd9Sstevel@tonic-gate 	}
28517c478bd9Sstevel@tonic-gate 	return (0);
28527c478bd9Sstevel@tonic-gate }
28537c478bd9Sstevel@tonic-gate 
28547c478bd9Sstevel@tonic-gate static Sym *
28559aa23310Srie _lazy_find_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
28569aa23310Srie     int *in_nfavl)
28577c478bd9Sstevel@tonic-gate {
28587c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	for (lmp = ilmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
28617c478bd9Sstevel@tonic-gate 		if (LAZY(lmp) == 0)
28627c478bd9Sstevel@tonic-gate 			continue;
286360758829Srie 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
28647c478bd9Sstevel@tonic-gate 			Sym	*sym;
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
28679aa23310Srie 			if ((sym = elf_lazy_find_sym(slp, dlmp, binfo,
28689aa23310Srie 			    in_nfavl)) != 0)
28697c478bd9Sstevel@tonic-gate 				return (sym);
28707c478bd9Sstevel@tonic-gate 		}
28717c478bd9Sstevel@tonic-gate 	}
28727c478bd9Sstevel@tonic-gate 	return (0);
28737c478bd9Sstevel@tonic-gate }
28747c478bd9Sstevel@tonic-gate 
287560758829Srie static Sym *
28769aa23310Srie _lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
28777c478bd9Sstevel@tonic-gate {
28787c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
28797c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
2880dae2dfb7Srie 	Lm_list		*lml = LIST(clmp);
28817c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap, *lmp;
28827c478bd9Sstevel@tonic-gate 	ulong_t		rsymndx;
288360758829Srie 	Sym		*sym;
28847c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
28857c478bd9Sstevel@tonic-gate 	Slookup		sl;
28867c478bd9Sstevel@tonic-gate 
28877c478bd9Sstevel@tonic-gate 	/*
28887c478bd9Sstevel@tonic-gate 	 * Search the initial link map for the required symbol (this category is
28897c478bd9Sstevel@tonic-gate 	 * selected by dlsym(), where individual link maps are searched for a
28907c478bd9Sstevel@tonic-gate 	 * required symbol.  Therefore, we know we have permission to look at
28917c478bd9Sstevel@tonic-gate 	 * the link map).
28927c478bd9Sstevel@tonic-gate 	 */
289360758829Srie 	if (slp->sl_flags & LKUP_FIRST)
28949aa23310Srie 		return (SYMINTP(ilmp)(slp, dlmp, binfo, in_nfavl));
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	/*
28977c478bd9Sstevel@tonic-gate 	 * Determine whether this lookup can be satisfied by an objects direct,
28987c478bd9Sstevel@tonic-gate 	 * or lazy binding information.  This is triggered by a relocation from
28997c478bd9Sstevel@tonic-gate 	 * the object (hence rsymndx is set).
29007c478bd9Sstevel@tonic-gate 	 */
29017c478bd9Sstevel@tonic-gate 	if (((rsymndx = slp->sl_rsymndx) != 0) &&
2902aa736cbeSrie 	    ((sip = SYMINFO(clmp)) != NULL)) {
2903dae2dfb7Srie 		uint_t	bound;
2904dae2dfb7Srie 
29057c478bd9Sstevel@tonic-gate 		/*
29067c478bd9Sstevel@tonic-gate 		 * Find the corresponding Syminfo entry for the original
29077c478bd9Sstevel@tonic-gate 		 * referencing symbol.
29087c478bd9Sstevel@tonic-gate 		 */
29097c478bd9Sstevel@tonic-gate 		/* LINTED */
29107c478bd9Sstevel@tonic-gate 		sip = (Syminfo *)((char *)sip + (rsymndx * SYMINENT(clmp)));
2911dae2dfb7Srie 		bound = sip->si_boundto;
2912dae2dfb7Srie 
2913dae2dfb7Srie 		/*
2914dae2dfb7Srie 		 * Identify any EXTERN or PARENT references for ldd(1).
2915dae2dfb7Srie 		 */
2916dae2dfb7Srie 		if ((lml->lm_flags & LML_FLG_TRC_WARN) &&
2917dae2dfb7Srie 		    (bound > SYMINFO_BT_LOWRESERVE)) {
2918dae2dfb7Srie 			if (bound == SYMINFO_BT_PARENT)
2919dae2dfb7Srie 				*binfo |= DBG_BINFO_REF_PARENT;
2920dae2dfb7Srie 			if (bound == SYMINFO_BT_EXTERN)
2921dae2dfb7Srie 				*binfo |= DBG_BINFO_REF_EXTERN;
2922dae2dfb7Srie 		}
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 		/*
29257c478bd9Sstevel@tonic-gate 		 * If the symbol information indicates a direct binding,
29267c478bd9Sstevel@tonic-gate 		 * determine the link map that is required to satisfy the
29277c478bd9Sstevel@tonic-gate 		 * binding.  Note, if the dependency can not be found, but a
29287c478bd9Sstevel@tonic-gate 		 * direct binding isn't required, we will still fall through
29297c478bd9Sstevel@tonic-gate 		 * to perform any default symbol search.
29307c478bd9Sstevel@tonic-gate 		 */
29317c478bd9Sstevel@tonic-gate 		if (sip->si_flags & SYMINFO_FLG_DIRECT) {
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 			lmp = 0;
29347c478bd9Sstevel@tonic-gate 			if (bound < SYMINFO_BT_LOWRESERVE)
29359aa23310Srie 				lmp = elf_lazy_load(clmp, slp, bound,
29369aa23310Srie 				    name, in_nfavl);
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 			/*
29397c478bd9Sstevel@tonic-gate 			 * If direct bindings have been disabled, and this isn't
29407c478bd9Sstevel@tonic-gate 			 * a translator, skip any direct binding now that we've
294160758829Srie 			 * ensured the resolving object has been loaded.
29427c478bd9Sstevel@tonic-gate 			 *
29437c478bd9Sstevel@tonic-gate 			 * If we need to direct bind to anything, we look in
29447c478bd9Sstevel@tonic-gate 			 * ourselves, our parent, or in the link map we've just
29457c478bd9Sstevel@tonic-gate 			 * loaded.  Otherwise, even though we may have lazily
29467c478bd9Sstevel@tonic-gate 			 * loaded an object we still continue to search for
29477c478bd9Sstevel@tonic-gate 			 * symbols from the head of the link map list.
29487c478bd9Sstevel@tonic-gate 			 */
29497c478bd9Sstevel@tonic-gate 			if (((FLAGS(clmp) & FLG_RT_TRANS) ||
2950dae2dfb7Srie 			    (((lml->lm_tflags & LML_TFLG_NODIRECT) == 0) &&
2951dae2dfb7Srie 			    ((slp->sl_flags & LKUP_SINGLETON) == 0))) &&
29529a411307Srie 			    ((FLAGS1(clmp) & FL1_RT_DIRECT) ||
29537c478bd9Sstevel@tonic-gate 			    (sip->si_flags & SYMINFO_FLG_DIRECTBIND))) {
29547c478bd9Sstevel@tonic-gate 				sym = lookup_sym_direct(slp, dlmp, binfo,
29559aa23310Srie 				    sip, lmp, in_nfavl);
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 				/*
295860758829Srie 				 * Determine whether this direct binding has
295960758829Srie 				 * been rejected.  If we've bound to a singleton
296060758829Srie 				 * without following a singleton search, then
296160758829Srie 				 * return.  The caller detects this condition
296260758829Srie 				 * and will trigger a new singleton search.
296360758829Srie 				 *
296460758829Srie 				 * For any other rejection (such as binding to
296560758829Srie 				 * a symbol labeled as nodirect - presumably
296660758829Srie 				 * because the symbol definition has been
296760758829Srie 				 * changed since the referring object was last
296860758829Srie 				 * built), fall through to a standard symbol
29697c478bd9Sstevel@tonic-gate 				 * search.
29707c478bd9Sstevel@tonic-gate 				 */
297160758829Srie 				if (((*binfo & BINFO_REJECTED) == 0) ||
297260758829Srie 				    (*binfo & BINFO_REJSINGLE))
29737c478bd9Sstevel@tonic-gate 					return (sym);
297460758829Srie 
297560758829Srie 				*binfo &= ~BINFO_REJECTED;
29767c478bd9Sstevel@tonic-gate 			}
29777c478bd9Sstevel@tonic-gate 		}
29787c478bd9Sstevel@tonic-gate 	}
29797c478bd9Sstevel@tonic-gate 
298060758829Srie 	/*
298160758829Srie 	 * Duplicate the lookup information, as we'll need to modify this
298260758829Srie 	 * information for some of the following searches.
298360758829Srie 	 */
29847c478bd9Sstevel@tonic-gate 	sl = *slp;
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate 	/*
29877c478bd9Sstevel@tonic-gate 	 * If the referencing object has the DF_SYMBOLIC flag set, look in the
29887c478bd9Sstevel@tonic-gate 	 * referencing object for the symbol first.  Failing that, fall back to
29897c478bd9Sstevel@tonic-gate 	 * our generic search.
29907c478bd9Sstevel@tonic-gate 	 */
299160758829Srie 	if ((FLAGS1(clmp) & FL1_RT_SYMBOLIC) &&
299260758829Srie 	    ((sl.sl_flags & LKUP_SINGLETON) == 0)) {
29937c478bd9Sstevel@tonic-gate 		sl.sl_imap = clmp;
29949aa23310Srie 		if (sym = SYMINTP(clmp)(&sl, dlmp, binfo, in_nfavl)) {
29959a411307Srie 			ulong_t	dsymndx = (((ulong_t)sym -
29967247f888Srie 			    (ulong_t)SYMTAB(*dlmp)) / SYMENT(*dlmp));
29979a411307Srie 
29989a411307Srie 			/*
29999a411307Srie 			 * Make sure this symbol hasn't explicitly been defined
30009a411307Srie 			 * as nodirect.
30019a411307Srie 			 */
30029a411307Srie 			if (((sip = SYMINFO(*dlmp)) == 0) ||
30039a411307Srie 			    /* LINTED */
30049a411307Srie 			    ((sip = (Syminfo *)((char *)sip +
30059a411307Srie 			    (dsymndx * SYMINENT(*dlmp)))) == 0) ||
30069a411307Srie 			    ((sip->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0))
30079a411307Srie 				return (sym);
30089a411307Srie 		}
30097c478bd9Sstevel@tonic-gate 	}
30107c478bd9Sstevel@tonic-gate 
301160758829Srie 	sl.sl_flags |= LKUP_STANDARD;
301260758829Srie 
30137c478bd9Sstevel@tonic-gate 	/*
30147c478bd9Sstevel@tonic-gate 	 * If this lookup originates from a standard relocation, then traverse
301560758829Srie 	 * all link-map control lists, inspecting any object that is available
301660758829Srie 	 * to this caller.  Otherwise, traverse the link-map control list
301760758829Srie 	 * associated with the caller.
30187c478bd9Sstevel@tonic-gate 	 */
301960758829Srie 	if (sl.sl_flags & LKUP_STDRELOC) {
30207c478bd9Sstevel@tonic-gate 		Aliste	off;
30217c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
30227c478bd9Sstevel@tonic-gate 
302360758829Srie 		sym = NULL;
30247c478bd9Sstevel@tonic-gate 
3025dae2dfb7Srie 		for (ALIST_TRAVERSE_BY_OFFSET(lml->lm_lists, off, lmc)) {
302660758829Srie 			if (((sym = core_lookup_sym(lmc->lc_head, &sl, dlmp,
30279aa23310Srie 			    binfo, off, in_nfavl)) != NULL) ||
302860758829Srie 			    (*binfo & BINFO_REJSINGLE))
30297c478bd9Sstevel@tonic-gate 				break;
30307c478bd9Sstevel@tonic-gate 		}
30317c478bd9Sstevel@tonic-gate 	} else
30329aa23310Srie 		sym = core_lookup_sym(ilmp, &sl, dlmp, binfo, ALIST_OFF_DATA,
30339aa23310Srie 		    in_nfavl);
303460758829Srie 
303560758829Srie 	/*
303660758829Srie 	 * If a symbol binding was rejected, because a binding occurred to a
303760758829Srie 	 * singleton without following the default symbol search, return so
303860758829Srie 	 * that the search can be repreated.
303960758829Srie 	 */
304060758829Srie 	if (*binfo & BINFO_REJSINGLE)
304160758829Srie 		return (sym);
30427c478bd9Sstevel@tonic-gate 
30437c478bd9Sstevel@tonic-gate 	/*
30447c478bd9Sstevel@tonic-gate 	 * To allow transitioning into a world of lazy loading dependencies see
30457c478bd9Sstevel@tonic-gate 	 * if this link map contains objects that have lazy dependencies still
30467c478bd9Sstevel@tonic-gate 	 * outstanding.  If so, and we haven't been able to locate a non-weak
30477c478bd9Sstevel@tonic-gate 	 * symbol reference, start bringing in any lazy dependencies to see if
30487c478bd9Sstevel@tonic-gate 	 * the reference can be satisfied.  Use of dlsym(RTLD_PROBE) sets the
304975e7992aSrie 	 * LKUP_NOFALLBACK flag, and this flag disables this fall back.
30507c478bd9Sstevel@tonic-gate 	 */
305175e7992aSrie 	if ((sym == NULL) && ((sl.sl_flags & LKUP_NOFALLBACK) == 0)) {
30527c478bd9Sstevel@tonic-gate 		if ((lmp = ilmp) == 0)
30537c478bd9Sstevel@tonic-gate 			lmp = LIST(clmp)->lm_head;
305475e7992aSrie 
3055dae2dfb7Srie 		lml = LIST(lmp);
3056dae2dfb7Srie 		if ((sl.sl_flags & LKUP_WEAK) || (lml->lm_lazy == 0))
30577c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
30587c478bd9Sstevel@tonic-gate 
3059dae2dfb7Srie 		DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
30607c478bd9Sstevel@tonic-gate 
30617c478bd9Sstevel@tonic-gate 		/*
30627c478bd9Sstevel@tonic-gate 		 * If this request originated from a dlsym(RTLD_NEXT) then start
30637c478bd9Sstevel@tonic-gate 		 * looking for dependencies from the caller, otherwise use the
30647c478bd9Sstevel@tonic-gate 		 * initial link-map.
30657c478bd9Sstevel@tonic-gate 		 */
306660758829Srie 		if (sl.sl_flags & LKUP_NEXT)
30679aa23310Srie 			sym = _lazy_find_sym(clmp, &sl, dlmp, binfo, in_nfavl);
30687c478bd9Sstevel@tonic-gate 		else {
3069cce0e03bSab 			Aliste	idx;
30707c478bd9Sstevel@tonic-gate 			Lm_cntl	*lmc;
30717c478bd9Sstevel@tonic-gate 
3072dae2dfb7Srie 			for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
307375e7992aSrie 				sl.sl_flags |= LKUP_NOFALLBACK;
30747c478bd9Sstevel@tonic-gate 				if ((sym = _lazy_find_sym(lmc->lc_head, &sl,
30759aa23310Srie 				    dlmp, binfo, in_nfavl)) != 0)
30767c478bd9Sstevel@tonic-gate 					break;
30777c478bd9Sstevel@tonic-gate 			}
30787c478bd9Sstevel@tonic-gate 		}
30797c478bd9Sstevel@tonic-gate 	}
308060758829Srie 	return (sym);
308160758829Srie }
308260758829Srie 
308360758829Srie /*
308460758829Srie  * Symbol lookup routine.  Takes an ELF symbol name, and a list of link maps to
308560758829Srie  * search.  If successful, return a pointer to the symbol table entry, a
308660758829Srie  * pointer to the link map of the enclosing object, and information relating
308760758829Srie  * to the type of binding.  Else return a null pointer.
308860758829Srie  *
308960758829Srie  * To improve elf performance, we first compute the elf hash value and pass
309060758829Srie  * it to each find_sym() routine.  The elf function will use this value to
309160758829Srie  * locate the symbol, the a.out function will simply ignore it.
309260758829Srie  */
309360758829Srie Sym *
30949aa23310Srie lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
309560758829Srie {
309660758829Srie 	Rt_map		*clmp = slp->sl_cmap;
309760758829Srie 	Sym		*rsym = slp->sl_rsym, *sym = 0;
309860758829Srie 	uchar_t		rtype = slp->sl_rtype;
309960758829Srie 
310060758829Srie 	if (slp->sl_hash == 0)
310175e7992aSrie 		slp->sl_hash = elf_hash(slp->sl_name);
310260758829Srie 	*binfo = 0;
310360758829Srie 
310460758829Srie 	/*
310560758829Srie 	 * Establish any state that might be associated with a symbol reference.
310660758829Srie 	 */
310760758829Srie 	if (rsym) {
310860758829Srie 		if ((slp->sl_flags & LKUP_STDRELOC) &&
310960758829Srie 		    (ELF_ST_BIND(rsym->st_info) == STB_WEAK))
311060758829Srie 			slp->sl_flags |= LKUP_WEAK;
311160758829Srie 
311260758829Srie 		if (ELF_ST_VISIBILITY(rsym->st_other) == STV_SINGLETON)
311360758829Srie 			slp->sl_flags |= LKUP_SINGLETON;
311460758829Srie 	}
311560758829Srie 
311660758829Srie 	/*
311760758829Srie 	 * Establish any lookup state required for this type of relocation.
311860758829Srie 	 */
311960758829Srie 	if ((slp->sl_flags & LKUP_STDRELOC) && rtype) {
312060758829Srie 		if (rtype == M_R_COPY)
312160758829Srie 			slp->sl_flags |= LKUP_COPY;
312260758829Srie 
312360758829Srie 		if (rtype != M_R_JMP_SLOT)
312460758829Srie 			slp->sl_flags |= LKUP_SPEC;
312560758829Srie 	}
312660758829Srie 
312760758829Srie 	/*
312860758829Srie 	 * Under ldd -w, any unresolved weak references are diagnosed.  Set the
312960758829Srie 	 * symbol binding as global to trigger a relocation error if the symbol
313060758829Srie 	 * can not be found.
313160758829Srie 	 */
313260758829Srie 	if (rsym) {
313360758829Srie 		if (LIST(slp->sl_cmap)->lm_flags & LML_FLG_TRC_NOUNRESWEAK)
313460758829Srie 			slp->sl_bind = STB_GLOBAL;
313560758829Srie 		else if ((slp->sl_bind = ELF_ST_BIND(rsym->st_info)) ==
313660758829Srie 		    STB_WEAK)
313760758829Srie 			slp->sl_flags |= LKUP_WEAK;
313860758829Srie 	}
313960758829Srie 
314060758829Srie 	/*
314160758829Srie 	 * Carry out an initial symbol search.  This search takes into account
314260758829Srie 	 * all the modes of the requested search.
314360758829Srie 	 */
31449aa23310Srie 	if (((sym = _lookup_sym(slp, dlmp, binfo, in_nfavl)) == NULL) &&
314560758829Srie 	    (*binfo & BINFO_REJSINGLE)) {
314660758829Srie 		Slookup	sl = *slp;
314760758829Srie 
314860758829Srie 		/*
314960758829Srie 		 * If a binding has been rejected because of binding to a
315060758829Srie 		 * singleton without going through a singleton search, then
315160758829Srie 		 * reset the lookup data, and try again.
315260758829Srie 		 */
315360758829Srie 		sl.sl_imap = LIST(sl.sl_cmap)->lm_head;
315460758829Srie 		sl.sl_flags &= ~(LKUP_FIRST | LKUP_SELF | LKUP_NEXT);
315560758829Srie 		sl.sl_flags |= LKUP_SINGLETON;
315660758829Srie 		sl.sl_rsymndx = 0;
315760758829Srie 		*binfo &= ~BINFO_REJECTED;
31589aa23310Srie 		sym = _lookup_sym(&sl, dlmp, binfo, in_nfavl);
315960758829Srie 	}
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate 	/*
31627c478bd9Sstevel@tonic-gate 	 * If the caller is restricted to a symbol search within its group,
31637c478bd9Sstevel@tonic-gate 	 * determine if it is necessary to follow a binding from outside of
31647c478bd9Sstevel@tonic-gate 	 * the group.
31657c478bd9Sstevel@tonic-gate 	 */
3166adbfe822Srie 	if ((MODE(clmp) & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP) {
3167aa736cbeSrie 		Sym	*isym;
31687c478bd9Sstevel@tonic-gate 
3169adbfe822Srie 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
3170adbfe822Srie 		    in_nfavl)) != 0)
31717c478bd9Sstevel@tonic-gate 			return (isym);
31727c478bd9Sstevel@tonic-gate 	}
31737c478bd9Sstevel@tonic-gate 	return (sym);
31747c478bd9Sstevel@tonic-gate }
31757c478bd9Sstevel@tonic-gate 
31767c478bd9Sstevel@tonic-gate /*
31777c478bd9Sstevel@tonic-gate  * Associate a binding descriptor with a caller and its dependency, or update
31787c478bd9Sstevel@tonic-gate  * an existing descriptor.
31797c478bd9Sstevel@tonic-gate  */
31807c478bd9Sstevel@tonic-gate int
31815aefb655Srie bind_one(Rt_map *clmp, Rt_map *dlmp, uint_t flags)
31827c478bd9Sstevel@tonic-gate {
3183cce0e03bSab 	Bnd_desc	*bdp;
3184cce0e03bSab 	Aliste		idx;
31857c478bd9Sstevel@tonic-gate 	int		found = ALE_CREATE;
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	/*
31887c478bd9Sstevel@tonic-gate 	 * Determine whether a binding descriptor already exists between the
31897c478bd9Sstevel@tonic-gate 	 * two objects.
31907c478bd9Sstevel@tonic-gate 	 */
3191cce0e03bSab 	for (APLIST_TRAVERSE(DEPENDS(clmp), idx, bdp)) {
31927c478bd9Sstevel@tonic-gate 		if (bdp->b_depend == dlmp) {
31937c478bd9Sstevel@tonic-gate 			found = ALE_EXISTS;
31947c478bd9Sstevel@tonic-gate 			break;
31957c478bd9Sstevel@tonic-gate 		}
31967c478bd9Sstevel@tonic-gate 	}
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 	if (found == ALE_CREATE) {
31997c478bd9Sstevel@tonic-gate 		/*
32007c478bd9Sstevel@tonic-gate 		 * Create a new binding descriptor.
32017c478bd9Sstevel@tonic-gate 		 */
32027c478bd9Sstevel@tonic-gate 		if ((bdp = malloc(sizeof (Bnd_desc))) == 0)
32037c478bd9Sstevel@tonic-gate 			return (0);
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 		bdp->b_caller = clmp;
32067c478bd9Sstevel@tonic-gate 		bdp->b_depend = dlmp;
32077c478bd9Sstevel@tonic-gate 		bdp->b_flags = 0;
32087c478bd9Sstevel@tonic-gate 
32097c478bd9Sstevel@tonic-gate 		/*
32107c478bd9Sstevel@tonic-gate 		 * Append the binding descriptor to the caller and the
32117c478bd9Sstevel@tonic-gate 		 * dependency.
32127c478bd9Sstevel@tonic-gate 		 */
3213cce0e03bSab 		if (aplist_append(&DEPENDS(clmp), bdp, AL_CNT_DEPENDS) == 0)
32147c478bd9Sstevel@tonic-gate 			return (0);
32157c478bd9Sstevel@tonic-gate 
3216cce0e03bSab 		if (aplist_append(&CALLERS(dlmp), bdp, AL_CNT_CALLERS) == 0)
32177c478bd9Sstevel@tonic-gate 			return (0);
32187c478bd9Sstevel@tonic-gate 	}
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 	if ((found == ALE_CREATE) || ((bdp->b_flags & flags) != flags)) {
32217c478bd9Sstevel@tonic-gate 		bdp->b_flags |= flags;
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 		if (flags & BND_REFER)
32247c478bd9Sstevel@tonic-gate 			FLAGS1(dlmp) |= FL1_RT_USED;
32257c478bd9Sstevel@tonic-gate 
32265aefb655Srie 		DBG_CALL(Dbg_file_bind_entry(LIST(clmp), bdp));
32277c478bd9Sstevel@tonic-gate 	}
32287c478bd9Sstevel@tonic-gate 	return (found);
32297c478bd9Sstevel@tonic-gate }
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate /*
32327c478bd9Sstevel@tonic-gate  * Cleanup after relocation processing.
32337c478bd9Sstevel@tonic-gate  */
32347c478bd9Sstevel@tonic-gate int
3235cce0e03bSab relocate_finish(Rt_map *lmp, APlist *bound, int textrel, int ret)
32367c478bd9Sstevel@tonic-gate {
32375aefb655Srie 	DBG_CALL(Dbg_reloc_run(lmp, 0, ret, DBG_REL_FINISH));
32387c478bd9Sstevel@tonic-gate 
32397c478bd9Sstevel@tonic-gate 	/*
32407c478bd9Sstevel@tonic-gate 	 * Establish bindings to all objects that have been bound to.
32417c478bd9Sstevel@tonic-gate 	 */
32427c478bd9Sstevel@tonic-gate 	if (bound) {
3243cce0e03bSab 		Aliste	idx;
3244cce0e03bSab 		Rt_map	*_lmp;
32458a20d9f8Srie 		Word	used;
32467c478bd9Sstevel@tonic-gate 
32478a20d9f8Srie 		/*
32488a20d9f8Srie 		 * Only create bindings if the callers relocation was
32498a20d9f8Srie 		 * successful (ret != 0), otherwise the object will eventually
32508a20d9f8Srie 		 * be torn down.  Create these bindings if running under ldd(1)
32518a20d9f8Srie 		 * with the -U/-u options regardless of relocation errors, as
32528a20d9f8Srie 		 * the unused processing needs to traverse these bindings to
32538a20d9f8Srie 		 * diagnose unused objects.
32548a20d9f8Srie 		 */
32558a20d9f8Srie 		used = LIST(lmp)->lm_flags &
32568a20d9f8Srie 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED);
32578a20d9f8Srie 
32588a20d9f8Srie 		if (ret || used) {
3259cce0e03bSab 			for (APLIST_TRAVERSE(bound, idx, _lmp)) {
32608a20d9f8Srie 				if (bind_one(lmp, _lmp, BND_REFER) || used)
32618a20d9f8Srie 					continue;
32628a20d9f8Srie 
32638a20d9f8Srie 				ret = 0;
32648a20d9f8Srie 				break;
32657c478bd9Sstevel@tonic-gate 			}
32667c478bd9Sstevel@tonic-gate 		}
32677c478bd9Sstevel@tonic-gate 		free(bound);
32687c478bd9Sstevel@tonic-gate 	}
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	/*
32717c478bd9Sstevel@tonic-gate 	 * If we write enabled the text segment to perform these relocations
32727c478bd9Sstevel@tonic-gate 	 * re-protect by disabling writes.
32737c478bd9Sstevel@tonic-gate 	 */
32747c478bd9Sstevel@tonic-gate 	if (textrel)
32757c478bd9Sstevel@tonic-gate 		(void) LM_SET_PROT(lmp)(lmp, 0);
32767c478bd9Sstevel@tonic-gate 
32777c478bd9Sstevel@tonic-gate 	return (ret);
32787c478bd9Sstevel@tonic-gate }
3279