xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/analyze.c (revision 57ef7aa924e4bfdf3118d9b5b4285dfc94b632f3)
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 /*
2356deab07SRod Evans  * Copyright 2009 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>
3756deab07SRod Evans #include	<sys/debug.h>
387c478bd9Sstevel@tonic-gate #include	<fcntl.h>
397c478bd9Sstevel@tonic-gate #include	<limits.h>
407c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
417c478bd9Sstevel@tonic-gate #include	<errno.h>
427c478bd9Sstevel@tonic-gate #include	<link.h>
435aefb655Srie #include	<debug.h>
445aefb655Srie #include	<conv.h>
457c478bd9Sstevel@tonic-gate #include	"_rtld.h"
467c478bd9Sstevel@tonic-gate #include	"_audit.h"
477c478bd9Sstevel@tonic-gate #include	"_elf.h"
4856deab07SRod Evans #include	"_a.out.h"
4956deab07SRod Evans #include	"_inline.h"
507c478bd9Sstevel@tonic-gate #include	"msg.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * If a load filter flag is in effect, and this object is a filter, trigger the
547c478bd9Sstevel@tonic-gate  * loading of all its filtees.  The load filter flag is in effect when creating
557c478bd9Sstevel@tonic-gate  * configuration files, or when under the control of ldd(1), or the LD_LOADFLTR
567c478bd9Sstevel@tonic-gate  * environment variable is set, or this object was built with the -zloadfltr
577c478bd9Sstevel@tonic-gate  * flag.  Otherwise, filtee loading is deferred until triggered by a relocation.
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate static void
609aa23310Srie load_filtees(Rt_map *lmp, int *in_nfavl)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	if ((FLAGS1(lmp) & MSK_RT_FILTER) &&
637c478bd9Sstevel@tonic-gate 	    ((FLAGS(lmp) & FLG_RT_LOADFLTR) ||
647c478bd9Sstevel@tonic-gate 	    (LIST(lmp)->lm_tflags & LML_TFLG_LOADFLTR))) {
657c478bd9Sstevel@tonic-gate 		Dyninfo		*dip =  DYNINFO(lmp);
667c478bd9Sstevel@tonic-gate 		uint_t		cnt, max = DYNINFOCNT(lmp);
677c478bd9Sstevel@tonic-gate 		Slookup		sl;
687c478bd9Sstevel@tonic-gate 
6975e7992aSrie 		/*
7075e7992aSrie 		 * Initialize the symbol lookup data structure.
7175e7992aSrie 		 */
7275e7992aSrie 		SLOOKUP_INIT(sl, 0, lmp, lmp, ld_entry_cnt, 0, 0, 0, 0, 0);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < max; cnt++, dip++) {
757c478bd9Sstevel@tonic-gate 			if (((dip->di_flags & MSK_DI_FILTER) == 0) ||
767c478bd9Sstevel@tonic-gate 			    ((dip->di_flags & FLG_DI_AUXFLTR) &&
777c478bd9Sstevel@tonic-gate 			    (rtld_flags & RT_FL_NOAUXFLTR)))
787c478bd9Sstevel@tonic-gate 				continue;
799aa23310Srie 			(void) elf_lookup_filtee(&sl, 0, 0, cnt, in_nfavl);
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate  * Analyze one or more link-maps of a link map control list.  This routine is
867c478bd9Sstevel@tonic-gate  * called at startup to continue the processing of the main executable.  It is
877c478bd9Sstevel@tonic-gate  * also called each time a new set of objects are loaded, ie. from filters,
887c478bd9Sstevel@tonic-gate  * lazy-loaded objects, or dlopen().
897c478bd9Sstevel@tonic-gate  *
907c478bd9Sstevel@tonic-gate  * In each instance we traverse the link-map control list starting with the
917c478bd9Sstevel@tonic-gate  * initial object.  As dependencies are analyzed they are added to the link-map
927c478bd9Sstevel@tonic-gate  * control list.  Thus the list grows as we traverse it - this results in the
937c478bd9Sstevel@tonic-gate  * breadth first ordering of all needed objects.
9456deab07SRod Evans  *
9556deab07SRod Evans  * Return the initial link-map from which analysis starts for relocate_lmc().
967c478bd9Sstevel@tonic-gate  */
9756deab07SRod Evans Rt_map *
989aa23310Srie analyze_lmc(Lm_list *lml, Aliste nlmco, Rt_map *nlmp, int *in_nfavl)
997c478bd9Sstevel@tonic-gate {
10056deab07SRod Evans 	Rt_map	*lmp;
1017c478bd9Sstevel@tonic-gate 	Lm_cntl	*nlmc;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	/*
1047c478bd9Sstevel@tonic-gate 	 * If this link-map control list is being analyzed, return.  The object
1057c478bd9Sstevel@tonic-gate 	 * that has just been added will be picked up by the existing analysis
1067c478bd9Sstevel@tonic-gate 	 * thread.  Note, this is only really meaningful during process init-
1077c478bd9Sstevel@tonic-gate 	 * ialization, as objects are added to the main link-map control list.
1087c478bd9Sstevel@tonic-gate 	 * Following this initialization, each family of objects that are loaded
1097c478bd9Sstevel@tonic-gate 	 * are added to a new link-map control list.
1107c478bd9Sstevel@tonic-gate 	 */
1117c478bd9Sstevel@tonic-gate 	/* LINTED */
112cce0e03bSab196087 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
1137c478bd9Sstevel@tonic-gate 	if (nlmc->lc_flags & LMC_FLG_ANALYZING)
11456deab07SRod Evans 		return (nlmp);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	/*
1177c478bd9Sstevel@tonic-gate 	 * If this object doesn't belong to the present link-map control list
1187c478bd9Sstevel@tonic-gate 	 * then it must already have been analyzed, or it is in the process of
1197c478bd9Sstevel@tonic-gate 	 * being analyzed prior to us recursing into this analysis.  In either
1207c478bd9Sstevel@tonic-gate 	 * case, ignore the object as it's already being taken care of.
1217c478bd9Sstevel@tonic-gate 	 */
1227c478bd9Sstevel@tonic-gate 	if (nlmco != CNTL(nlmp))
12356deab07SRod Evans 		return (nlmp);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	nlmc->lc_flags |= LMC_FLG_ANALYZING;
1267c478bd9Sstevel@tonic-gate 
12756deab07SRod Evans 	for (lmp = nlmp; lmp; lmp = NEXT_RT_MAP(lmp)) {
1287c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) &
1297c478bd9Sstevel@tonic-gate 		    (FLG_RT_ANALZING | FLG_RT_ANALYZED | FLG_RT_DELETE))
1307c478bd9Sstevel@tonic-gate 			continue;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		/*
1337c478bd9Sstevel@tonic-gate 		 * Indicate that analyzing is under way.
1347c478bd9Sstevel@tonic-gate 		 */
1357c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_ANALZING;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		/*
1387c478bd9Sstevel@tonic-gate 		 * If this link map represents a relocatable object, then we
1397c478bd9Sstevel@tonic-gate 		 * need to finish the link-editing of the object at this point.
1407c478bd9Sstevel@tonic-gate 		 */
1417c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_OBJECT) {
14256deab07SRod Evans 			Rt_map	*olmp;
14356deab07SRod Evans 
14456deab07SRod Evans 			if ((olmp = elf_obj_fini(lml, lmp, in_nfavl)) == NULL) {
1457c478bd9Sstevel@tonic-gate 				if (lml->lm_flags & LML_FLG_TRC_ENABLE)
1467c478bd9Sstevel@tonic-gate 					continue;
14756deab07SRod Evans 				nlmp = NULL;
1487c478bd9Sstevel@tonic-gate 				break;
1497c478bd9Sstevel@tonic-gate 			}
15056deab07SRod Evans 
15156deab07SRod Evans 			/*
15256deab07SRod Evans 			 * The original link-map that captured a relocatable
15356deab07SRod Evans 			 * object is a temporary link-map, that basically acts
15456deab07SRod Evans 			 * as a place holder in the link-map list.  On
15556deab07SRod Evans 			 * completion of relocatable object processing, a new
15656deab07SRod Evans 			 * link-map is created, and switched with the place
15756deab07SRod Evans 			 * holder.  Therefore, reassign both the present
15856deab07SRod Evans 			 * link-map pointer and the return link-map pointer.
15956deab07SRod Evans 			 * The former resets this routines link-map processing,
16056deab07SRod Evans 			 * while the latter provides for later functions, like
16156deab07SRod Evans 			 * relocate_lmc(), to start processing from this new
16256deab07SRod Evans 			 * link-map.
16356deab07SRod Evans 			 */
16456deab07SRod Evans 			if (nlmp == lmp)
16556deab07SRod Evans 				nlmp = olmp;
16656deab07SRod Evans 			lmp = olmp;
1677c478bd9Sstevel@tonic-gate 		}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_analyze(lmp));
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 		/*
1727c478bd9Sstevel@tonic-gate 		 * Establish any dependencies this object requires.
1737c478bd9Sstevel@tonic-gate 		 */
1749aa23310Srie 		if (LM_NEEDED(lmp)(lml, nlmco, lmp, in_nfavl) == 0) {
1757c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE)
1767c478bd9Sstevel@tonic-gate 				continue;
17756deab07SRod Evans 			nlmp = NULL;
1787c478bd9Sstevel@tonic-gate 			break;
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_ANALZING;
1827c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_ANALYZED;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		/*
1857c478bd9Sstevel@tonic-gate 		 * If we're building a configuration file, determine if this
1867c478bd9Sstevel@tonic-gate 		 * object is a filter and if so load its filtees.  This
1877c478bd9Sstevel@tonic-gate 		 * traversal is only necessary for crle(1), as typical use of
1887c478bd9Sstevel@tonic-gate 		 * an object will load filters as part of relocation processing.
1897c478bd9Sstevel@tonic-gate 		 */
1907c478bd9Sstevel@tonic-gate 		if (MODE(nlmp) & RTLD_CONFGEN)
1919aa23310Srie 			load_filtees(lmp, in_nfavl);
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		/*
1947c478bd9Sstevel@tonic-gate 		 * If an interposer has been added, it will have been inserted
1957c478bd9Sstevel@tonic-gate 		 * in the link-map before the link we're presently analyzing.
1967c478bd9Sstevel@tonic-gate 		 * Break out of this analysis loop and return to the head of
1977c478bd9Sstevel@tonic-gate 		 * the link-map control list to analyze the interposer.  Note
1987c478bd9Sstevel@tonic-gate 		 * that this rescan preserves the breadth first loading of
1997c478bd9Sstevel@tonic-gate 		 * dependencies.
2007c478bd9Sstevel@tonic-gate 		 */
20124a6229eSrie 		/* LINTED */
202cce0e03bSab196087 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
2037c478bd9Sstevel@tonic-gate 		if (nlmc->lc_flags & LMC_FLG_REANALYZE) {
2047c478bd9Sstevel@tonic-gate 			nlmc->lc_flags &= ~LMC_FLG_REANALYZE;
2057c478bd9Sstevel@tonic-gate 			lmp = nlmc->lc_head;
2067c478bd9Sstevel@tonic-gate 		}
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 
20924a6229eSrie 	/* LINTED */
210cce0e03bSab196087 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
2117c478bd9Sstevel@tonic-gate 	nlmc->lc_flags &= ~LMC_FLG_ANALYZING;
2127c478bd9Sstevel@tonic-gate 
21356deab07SRod Evans 	return (nlmp);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
217466e2a62Srie  * Determine whether a symbol represents zero, .bss, bits.  Most commonly this
218466e2a62Srie  * function is used to determine whether the data for a copy relocation refers
219466e2a62Srie  * to initialized data or .bss.  If the data definition is within .bss, then the
220466e2a62Srie  * data is zero filled, and as the copy destination within the executable is
221466e2a62Srie  * .bss, we can skip copying zero's to zero's.
222466e2a62Srie  *
223466e2a62Srie  * However, if the defining object has MOVE data, it's .bss might contain
224466e2a62Srie  * non-zero data, in which case copy the definition regardless.
225466e2a62Srie  *
226466e2a62Srie  * For backward compatibility copy relocation processing, this routine can be
227466e2a62Srie  * used to determine precisely if a copy destination is a move record recipient.
2287c478bd9Sstevel@tonic-gate  */
2297c478bd9Sstevel@tonic-gate static int
230466e2a62Srie are_bits_zero(Rt_map *dlmp, Sym *dsym, int dest)
2317c478bd9Sstevel@tonic-gate {
23256deab07SRod Evans 	mmapobj_result_t	*mpp;
2337c478bd9Sstevel@tonic-gate 	caddr_t			daddr = (caddr_t)dsym->st_value;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & FLG_RT_FIXED) == 0)
2367c478bd9Sstevel@tonic-gate 		daddr += ADDR(dlmp);
2377c478bd9Sstevel@tonic-gate 
238466e2a62Srie 	/*
239466e2a62Srie 	 * Determine the segment that contains the copy definition.  Given that
240466e2a62Srie 	 * the copy relocation records have already been captured and verified,
241466e2a62Srie 	 * a segment must be found (but we add an escape clause never the less).
242466e2a62Srie 	 */
24356deab07SRod Evans 	if ((mpp = find_segment(daddr, dlmp)) == NULL)
2447c478bd9Sstevel@tonic-gate 		return (1);
245466e2a62Srie 
246466e2a62Srie 	/*
247466e2a62Srie 	 * If the definition is not within .bss, indicate this is not zero data.
248466e2a62Srie 	 */
24956deab07SRod Evans 	if (daddr < (mpp->mr_addr + mpp->mr_offset + mpp->mr_fsize))
2507c478bd9Sstevel@tonic-gate 		return (0);
251466e2a62Srie 
252466e2a62Srie 	/*
253466e2a62Srie 	 * If the definition is within .bss, make sure the definition isn't the
254466e2a62Srie 	 * recipient of a move record.  Note, we don't precisely analyze whether
255466e2a62Srie 	 * the address is a move record recipient, as the infrastructure to
256466e2a62Srie 	 * prepare for, and carry out this analysis, is probably more costly
257466e2a62Srie 	 * than just copying the bytes regardless.
258466e2a62Srie 	 */
259466e2a62Srie 	if ((FLAGS(dlmp) & FLG_RT_MOVE) == 0)
260466e2a62Srie 		return (1);
261466e2a62Srie 
262466e2a62Srie 	/*
263466e2a62Srie 	 * However, for backward compatibility copy relocation processing, we
264466e2a62Srie 	 * can afford to work a little harder.  Here, determine precisely
265466e2a62Srie 	 * whether the destination in the executable is a move record recipient.
266466e2a62Srie 	 * See comments in lookup_sym_interpose(), below.
267466e2a62Srie 	 */
268466e2a62Srie 	if (dest && is_move_data(daddr))
269466e2a62Srie 		return (0);
270466e2a62Srie 
271466e2a62Srie 	return (1);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate /*
2757c478bd9Sstevel@tonic-gate  * Relocate an individual object.
2767c478bd9Sstevel@tonic-gate  */
2777c478bd9Sstevel@tonic-gate static int
2789aa23310Srie relocate_so(Lm_list *lml, Rt_map *lmp, int *relocated, int now, int *in_nfavl)
2797c478bd9Sstevel@tonic-gate {
28056deab07SRod Evans 	APlist	*textrel = NULL;
28156deab07SRod Evans 	int	ret = 1;
28256deab07SRod Evans 
2837c478bd9Sstevel@tonic-gate 	/*
2847c478bd9Sstevel@tonic-gate 	 * If we're running under ldd(1), and haven't been asked to trace any
2857c478bd9Sstevel@tonic-gate 	 * warnings, skip any actual relocation processing.
2867c478bd9Sstevel@tonic-gate 	 */
2877c478bd9Sstevel@tonic-gate 	if (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
2887c478bd9Sstevel@tonic-gate 	    (lml->lm_flags & LML_FLG_TRC_WARN)) {
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		if (relocated)
2917c478bd9Sstevel@tonic-gate 			(*relocated)++;
2927c478bd9Sstevel@tonic-gate 
29356deab07SRod Evans 		if ((LM_RELOC(lmp)(lmp, now, in_nfavl, &textrel) == 0) &&
2947c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0))
29556deab07SRod Evans 			ret = 0;
29656deab07SRod Evans 
29756deab07SRod Evans 		/*
29856deab07SRod Evans 		 * Finally process any move data.  Note, this is carried out
29956deab07SRod Evans 		 * with ldd(1) under relocation processing too, as it can flush
30056deab07SRod Evans 		 * out move errors, and enables lari(1) to provide a true
30156deab07SRod Evans 		 * representation of the runtime bindings.
30256deab07SRod Evans 		 */
30356deab07SRod Evans 		if ((FLAGS(lmp) & FLG_RT_MOVE) &&
30456deab07SRod Evans 		    (move_data(lmp, &textrel) == 0) &&
30556deab07SRod Evans 		    ((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0))
30656deab07SRod Evans 			ret = 0;
3077c478bd9Sstevel@tonic-gate 	}
30856deab07SRod Evans 
30956deab07SRod Evans 	/*
31056deab07SRod Evans 	 * If a text segment was write enabled to perform any relocations or
31156deab07SRod Evans 	 * move records, then re-protect the segment by disabling writes.
31256deab07SRod Evans 	 */
31356deab07SRod Evans 	if (textrel) {
31456deab07SRod Evans 		mmapobj_result_t	*mpp;
31556deab07SRod Evans 		Aliste			idx;
31656deab07SRod Evans 
31756deab07SRod Evans 		for (APLIST_TRAVERSE(textrel, idx, mpp))
31856deab07SRod Evans 			(void) set_prot(lmp, mpp, 0);
31956deab07SRod Evans 		free(textrel);
32056deab07SRod Evans 	}
32156deab07SRod Evans 
32256deab07SRod Evans 	return (ret);
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Relocate the objects on a link-map control list.
3277c478bd9Sstevel@tonic-gate  */
3287c478bd9Sstevel@tonic-gate static int
32956deab07SRod Evans _relocate_lmc(Lm_list *lml, Aliste lmco, Rt_map *nlmp, int *relocated,
33056deab07SRod Evans     int *in_nfavl)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
3337c478bd9Sstevel@tonic-gate 
334cb511613SAli Bahrami 	for (lmp = nlmp; lmp; lmp = NEXT_RT_MAP(lmp)) {
3357c478bd9Sstevel@tonic-gate 		/*
3367c478bd9Sstevel@tonic-gate 		 * If this object has already been relocated, we're done.  If
3377c478bd9Sstevel@tonic-gate 		 * this object is being deleted, skip it, there's probably a
3387c478bd9Sstevel@tonic-gate 		 * relocation error somewhere that's causing this deletion.
3397c478bd9Sstevel@tonic-gate 		 */
3407c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) &
3417c478bd9Sstevel@tonic-gate 		    (FLG_RT_RELOCING | FLG_RT_RELOCED | FLG_RT_DELETE))
3427c478bd9Sstevel@tonic-gate 			continue;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		/*
3457c478bd9Sstevel@tonic-gate 		 * Indicate that relocation processing is under way.
3467c478bd9Sstevel@tonic-gate 		 */
3477c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_RELOCING;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		/*
3507c478bd9Sstevel@tonic-gate 		 * Relocate the object.
3517c478bd9Sstevel@tonic-gate 		 */
3529aa23310Srie 		if (relocate_so(lml, lmp, relocated, 0, in_nfavl) == 0)
3537c478bd9Sstevel@tonic-gate 			return (0);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		/*
3567c478bd9Sstevel@tonic-gate 		 * Indicate that the objects relocation is complete.
3577c478bd9Sstevel@tonic-gate 		 */
3587c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_RELOCING;
3597c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_RELOCED;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		/*
36256deab07SRod Evans 		 * If this object is being relocated on the main link-map list
36356deab07SRod Evans 		 * indicate that this object's init is available for harvesting.
36456deab07SRod Evans 		 * Objects that are being collected on other link-map lists
36556deab07SRod Evans 		 * will have there init availability tagged when the objects
36656deab07SRod Evans 		 * are move to the main link-map list (ie, after we know they,
36756deab07SRod Evans 		 * and their dependencies, are fully relocated and ready for
36856deab07SRod Evans 		 * use).
36956deab07SRod Evans 		 *
37056deab07SRod Evans 		 * Note, even under ldd(1) this init identification is necessary
37156deab07SRod Evans 		 * for -i (tsort) gathering.
3727c478bd9Sstevel@tonic-gate 		 */
37356deab07SRod Evans 		if (lmco == ALIST_OFF_DATA) {
3747c478bd9Sstevel@tonic-gate 			lml->lm_init++;
375dffec89cSrie 			lml->lm_flags |= LML_FLG_OBJADDED;
376466e2a62Srie 		}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		/*
3797c478bd9Sstevel@tonic-gate 		 * Determine if this object is a filter, and if a load filter
3807c478bd9Sstevel@tonic-gate 		 * flag is in effect, trigger the loading of all its filtees.
3817c478bd9Sstevel@tonic-gate 		 */
3829aa23310Srie 		load_filtees(lmp, in_nfavl);
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	/*
3867c478bd9Sstevel@tonic-gate 	 * Perform special copy relocations.  These are only meaningful for
3877c478bd9Sstevel@tonic-gate 	 * dynamic executables (fixed and head of their link-map list).  If
3887c478bd9Sstevel@tonic-gate 	 * this ever has to change then the infrastructure of COPY() has to
389cce0e03bSab196087 	 * change. Presently, a given link map can only have a receiver or
390cce0e03bSab196087 	 * supplier of copy data, so a union is used to overlap the storage
391cce0e03bSab196087 	 * for the COPY_R() and COPY_S() lists. These lists would need to
392cce0e03bSab196087 	 * be separated.
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) & FLG_RT_FIXED) && (nlmp == LIST(nlmp)->lm_head) &&
3957c478bd9Sstevel@tonic-gate 	    (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
3967c478bd9Sstevel@tonic-gate 	    (lml->lm_flags & LML_FLG_TRC_WARN))) {
397cce0e03bSab196087 		Rt_map		*lmp;
398cce0e03bSab196087 		Aliste		idx1;
3997c478bd9Sstevel@tonic-gate 		Word		tracing;
4007c478bd9Sstevel@tonic-gate 
40102ca3e02Srie #if	defined(__i386)
4027c478bd9Sstevel@tonic-gate 		if (elf_copy_gen(nlmp) == 0)
4037c478bd9Sstevel@tonic-gate 			return (0);
4047c478bd9Sstevel@tonic-gate #endif
405cce0e03bSab196087 		if (COPY_S(nlmp) == NULL)
4067c478bd9Sstevel@tonic-gate 			return (1);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		if ((LIST(nlmp)->lm_flags & LML_FLG_TRC_ENABLE) &&
4097c478bd9Sstevel@tonic-gate 		    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
4107c478bd9Sstevel@tonic-gate 		    (LIST(nlmp)->lm_flags & LML_FLG_TRC_VERBOSE)))
4117c478bd9Sstevel@tonic-gate 			tracing = 1;
4127c478bd9Sstevel@tonic-gate 		else
4137c478bd9Sstevel@tonic-gate 			tracing = 0;
4147c478bd9Sstevel@tonic-gate 
4155aefb655Srie 		DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
4167c478bd9Sstevel@tonic-gate 
417cce0e03bSab196087 		for (APLIST_TRAVERSE(COPY_S(nlmp), idx1, lmp)) {
4187c478bd9Sstevel@tonic-gate 			Rel_copy	*rcp;
419cce0e03bSab196087 			Aliste		idx2;
4207c478bd9Sstevel@tonic-gate 
421cce0e03bSab196087 			for (ALIST_TRAVERSE(COPY_R(lmp), idx2, rcp)) {
4227c478bd9Sstevel@tonic-gate 				int zero;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 				/*
425466e2a62Srie 				 * Only copy the data if the data is from
426466e2a62Srie 				 * a non-zero definition (ie. not .bss).
4277c478bd9Sstevel@tonic-gate 				 */
428466e2a62Srie 				zero = are_bits_zero(rcp->r_dlmp,
429466e2a62Srie 				    rcp->r_dsym, 0);
4305aefb655Srie 				DBG_CALL(Dbg_reloc_copy(rcp->r_dlmp, nlmp,
4315aefb655Srie 				    rcp->r_name, zero));
4327c478bd9Sstevel@tonic-gate 				if (zero)
4337c478bd9Sstevel@tonic-gate 					continue;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 				(void) memcpy(rcp->r_radd, rcp->r_dadd,
4367c478bd9Sstevel@tonic-gate 				    rcp->r_size);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 				if ((tracing == 0) || ((FLAGS1(rcp->r_dlmp) &
4397c478bd9Sstevel@tonic-gate 				    FL1_RT_DISPREL) == 0))
4407c478bd9Sstevel@tonic-gate 					continue;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_REL_CPYDISP),
4437c478bd9Sstevel@tonic-gate 				    demangle(rcp->r_name), NAME(rcp->r_dlmp));
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 
4475aefb655Srie 		DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
4487c478bd9Sstevel@tonic-gate 
449cce0e03bSab196087 		free(COPY_S(nlmp));
450cce0e03bSab196087 		COPY_S(nlmp) = 0;
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	return (1);
4537c478bd9Sstevel@tonic-gate }
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate int
4569aa23310Srie relocate_lmc(Lm_list *lml, Aliste nlmco, Rt_map *clmp, Rt_map *nlmp,
4579aa23310Srie     int *in_nfavl)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 	int	lret = 1, pret = 1;
460cce0e03bSab196087 	APlist	*alp;
4617c478bd9Sstevel@tonic-gate 	Aliste	plmco;
4627c478bd9Sstevel@tonic-gate 	Lm_cntl	*plmc, *nlmc;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	/*
4657c478bd9Sstevel@tonic-gate 	 * If this link-map control list is being relocated, return.  The object
4667c478bd9Sstevel@tonic-gate 	 * that has just been added will be picked up by the existing relocation
4677c478bd9Sstevel@tonic-gate 	 * thread.  Note, this is only really meaningful during process init-
4687c478bd9Sstevel@tonic-gate 	 * ialization, as objects are added to the main link-map control list.
4697c478bd9Sstevel@tonic-gate 	 * Following this initialization, each family of objects that are loaded
4707c478bd9Sstevel@tonic-gate 	 * are added to a new link-map control list.
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	/* LINTED */
473cce0e03bSab196087 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (nlmc->lc_flags & LMC_FLG_RELOCATING)
4767c478bd9Sstevel@tonic-gate 		return (1);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	nlmc->lc_flags |= LMC_FLG_RELOCATING;
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Relocate one or more link-maps of a link map control list.  If this
4827c478bd9Sstevel@tonic-gate 	 * object doesn't belong to the present link-map control list then it
4837c478bd9Sstevel@tonic-gate 	 * must already have been relocated, or it is in the process of being
4847c478bd9Sstevel@tonic-gate 	 * relocated prior to us recursing into this relocation.  In either
4857c478bd9Sstevel@tonic-gate 	 * case, ignore the object as it's already being taken care of, however,
4867c478bd9Sstevel@tonic-gate 	 * fall through and capture any relocation promotions that might have
4877c478bd9Sstevel@tonic-gate 	 * been established from the reference mode of this object.
4887c478bd9Sstevel@tonic-gate 	 *
4897c478bd9Sstevel@tonic-gate 	 * If we're generating a configuration file using crle(1), two passes
4907c478bd9Sstevel@tonic-gate 	 * may be involved.  Under the first pass, RTLD_CONFGEN is set.  Under
4917c478bd9Sstevel@tonic-gate 	 * this pass, crle() loads objects into the process address space.  No
4927c478bd9Sstevel@tonic-gate 	 * relocation is necessary at this point, we simply need to analyze the
4937c478bd9Sstevel@tonic-gate 	 * objects to insure any directly bound dependencies, filtees, etc.
4947c478bd9Sstevel@tonic-gate 	 * get loaded. Although we skip the relocation, fall through to insure
4957c478bd9Sstevel@tonic-gate 	 * any control lists are maintained appropriately.
4967c478bd9Sstevel@tonic-gate 	 *
4977c478bd9Sstevel@tonic-gate 	 * If objects are to be dldump(3c)'ed, crle(1) makes a second pass,
4987c478bd9Sstevel@tonic-gate 	 * using RTLD_NOW and RTLD_CONFGEN.  The RTLD_NOW effectively carries
4997c478bd9Sstevel@tonic-gate 	 * out the relocations of all loaded objects.
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	if ((nlmco == CNTL(nlmp)) &&
5027c478bd9Sstevel@tonic-gate 	    ((MODE(nlmp) & (RTLD_NOW | RTLD_CONFGEN)) != RTLD_CONFGEN)) {
5037c478bd9Sstevel@tonic-gate 		int	relocated = 0;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 		/*
5067c478bd9Sstevel@tonic-gate 		 * Determine whether the initial link-map control list has
5077c478bd9Sstevel@tonic-gate 		 * started relocation.  From this point, should any interposing
5087c478bd9Sstevel@tonic-gate 		 * objects be added to this link-map control list, the objects
5097c478bd9Sstevel@tonic-gate 		 * are demoted to standard objects.  Their interposition can't
5107c478bd9Sstevel@tonic-gate 		 * be guaranteed once relocations have been carried out.
5117c478bd9Sstevel@tonic-gate 		 */
512cce0e03bSab196087 		if (nlmco == ALIST_OFF_DATA)
5137c478bd9Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_STARTREL;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		/*
51602ca3e02Srie 		 * Relocate the link-map control list.  Should this relocation
51702ca3e02Srie 		 * fail, clean up this link-map list.  Relocations within this
51802ca3e02Srie 		 * list may have required relocation promotions on other lists,
51902ca3e02Srie 		 * so before acting upon these, and possibly adding more objects
52002ca3e02Srie 		 * to the present link-map control list, try and clean up any
52102ca3e02Srie 		 * failed objects now.
5227c478bd9Sstevel@tonic-gate 		 */
52356deab07SRod Evans 		lret = _relocate_lmc(lml, nlmco, nlmp, &relocated, in_nfavl);
524cce0e03bSab196087 		if ((lret == 0) && (nlmco != ALIST_OFF_DATA))
525481bba9eSRod Evans 			remove_lmc(lml, clmp, nlmco, NAME(nlmp));
5267c478bd9Sstevel@tonic-gate 	}
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * Determine the new, and previous link-map control lists.
5307c478bd9Sstevel@tonic-gate 	 */
53124a6229eSrie 	/* LINTED */
532cce0e03bSab196087 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
533cce0e03bSab196087 	if (nlmco == ALIST_OFF_DATA) {
5346679fdc0Srie 		plmco = nlmco;
5357c478bd9Sstevel@tonic-gate 		plmc = nlmc;
5366679fdc0Srie 	} else {
5377c478bd9Sstevel@tonic-gate 		plmco = nlmco - lml->lm_lists->al_size;
5387c478bd9Sstevel@tonic-gate 		/* LINTED */
539cce0e03bSab196087 		plmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, plmco);
5407c478bd9Sstevel@tonic-gate 	}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	/*
5437c478bd9Sstevel@tonic-gate 	 * Having completed this control list of objects, they can now be bound
5447c478bd9Sstevel@tonic-gate 	 * to from other objects.  Move this control list to the control list
5457c478bd9Sstevel@tonic-gate 	 * that precedes it.  Although this control list may have only bound to
5467c478bd9Sstevel@tonic-gate 	 * controls lists much higher up the control list stack, it must only
5477c478bd9Sstevel@tonic-gate 	 * be moved up one control list so as to preserve the link-map order
5487c478bd9Sstevel@tonic-gate 	 * that may have already been traversed in search of symbols.
5497c478bd9Sstevel@tonic-gate 	 */
550cce0e03bSab196087 	if (lret && (nlmco != ALIST_OFF_DATA) && nlmc->lc_head)
5517c478bd9Sstevel@tonic-gate 		lm_move(lml, nlmco, plmco, nlmc, plmc);
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	/*
5547c478bd9Sstevel@tonic-gate 	 * Determine whether existing objects that have already been relocated,
5557c478bd9Sstevel@tonic-gate 	 * need any additional relocations performed.  This can occur when new
5567c478bd9Sstevel@tonic-gate 	 * objects are loaded with RTLD_NOW, and these new objects have
5577c478bd9Sstevel@tonic-gate 	 * dependencies on objects that are already loaded.  Note, that we peel
5587c478bd9Sstevel@tonic-gate 	 * any relocation promotions off of one control list at a time.  This
5597c478bd9Sstevel@tonic-gate 	 * prevents relocations from being bound to objects that might yet fail
5607c478bd9Sstevel@tonic-gate 	 * to relocate themselves.
5617c478bd9Sstevel@tonic-gate 	 */
562cce0e03bSab196087 	while ((alp = plmc->lc_now) != NULL) {
563cce0e03bSab196087 		Aliste	idx;
564cce0e03bSab196087 		Rt_map	*lmp;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 		/*
5677c478bd9Sstevel@tonic-gate 		 * Remove the relocation promotion list, as performing more
5687c478bd9Sstevel@tonic-gate 		 * relocations may result in discovering more objects that need
5697c478bd9Sstevel@tonic-gate 		 * promotion.
5707c478bd9Sstevel@tonic-gate 		 */
571cce0e03bSab196087 		plmc->lc_now = NULL;
5727c478bd9Sstevel@tonic-gate 
573cce0e03bSab196087 		for (APLIST_TRAVERSE(alp, idx, lmp)) {
5747c478bd9Sstevel@tonic-gate 			/*
5757c478bd9Sstevel@tonic-gate 			 * If the original relocation of the link-map control
5767c478bd9Sstevel@tonic-gate 			 * list failed, or one of the relocation promotions of
5777c478bd9Sstevel@tonic-gate 			 * this loop has failed, demote any pending objects
5787c478bd9Sstevel@tonic-gate 			 * relocation mode.
5797c478bd9Sstevel@tonic-gate 			 */
5807c478bd9Sstevel@tonic-gate 			if ((lret == 0) || (pret == 0)) {
5817c478bd9Sstevel@tonic-gate 				MODE(lmp) &= ~RTLD_NOW;
5827c478bd9Sstevel@tonic-gate 				MODE(lmp) |= RTLD_LAZY;
5837c478bd9Sstevel@tonic-gate 				continue;
5847c478bd9Sstevel@tonic-gate 			}
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 			/*
5877c478bd9Sstevel@tonic-gate 			 * If a relocation fails, save the error condition.
5887c478bd9Sstevel@tonic-gate 			 * It's possible that all new objects on the original
5897c478bd9Sstevel@tonic-gate 			 * link-map control list have been relocated
5907c478bd9Sstevel@tonic-gate 			 * successfully, but if the user request requires
5917c478bd9Sstevel@tonic-gate 			 * promoting objects that have already been loaded, we
5927c478bd9Sstevel@tonic-gate 			 * have to indicate that this operation couldn't be
5937c478bd9Sstevel@tonic-gate 			 * performed.  The unrelocated objects are in use on
5947c478bd9Sstevel@tonic-gate 			 * another control list, and may continue to be used.
5957c478bd9Sstevel@tonic-gate 			 * If the .plt that resulted in the error is called,
5967c478bd9Sstevel@tonic-gate 			 * then the process will receive a fatal error at that
5977c478bd9Sstevel@tonic-gate 			 * time.  But, the .plt may never be called.
5987c478bd9Sstevel@tonic-gate 			 */
5999aa23310Srie 			if (relocate_so(lml, lmp, 0, 1, in_nfavl) == 0)
6007c478bd9Sstevel@tonic-gate 				pret = 0;
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		/*
6047c478bd9Sstevel@tonic-gate 		 * Having promoted any objects, determine whether additional
6057c478bd9Sstevel@tonic-gate 		 * dependencies were added, and if so move them to the previous
6067c478bd9Sstevel@tonic-gate 		 * link-map control list.
6077c478bd9Sstevel@tonic-gate 		 */
60824a6229eSrie 		/* LINTED */
609cce0e03bSab196087 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
61024a6229eSrie 		/* LINTED */
611cce0e03bSab196087 		plmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, plmco);
612cce0e03bSab196087 		if ((nlmco != ALIST_OFF_DATA) && nlmc->lc_head)
6137c478bd9Sstevel@tonic-gate 			lm_move(lml, nlmco, plmco, nlmc, plmc);
6148521e5e6Srie 		free(alp);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 
61724a6229eSrie 	/*
61802ca3e02Srie 	 * If relocations have been successful, indicate that relocations are
61902ca3e02Srie 	 * no longer active for this control list.  Otherwise, leave the
62002ca3e02Srie 	 * relocation flag, as this flag is used to determine the style of
62102ca3e02Srie 	 * cleanup (see remove_lmc()).
62224a6229eSrie 	 */
62302ca3e02Srie 	if (lret && pret) {
62424a6229eSrie 		/* LINTED */
625cce0e03bSab196087 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
6267c478bd9Sstevel@tonic-gate 		nlmc->lc_flags &= ~LMC_FLG_RELOCATING;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		return (1);
62902ca3e02Srie 	}
63002ca3e02Srie 
6317c478bd9Sstevel@tonic-gate 	return (0);
6327c478bd9Sstevel@tonic-gate }
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate /*
6357c478bd9Sstevel@tonic-gate  * Inherit the first rejection message for possible later diagnostics.
6367c478bd9Sstevel@tonic-gate  *
6377c478bd9Sstevel@tonic-gate  * Any attempt to process a file that is unsuccessful, should be accompanied
6387c478bd9Sstevel@tonic-gate  * with an error diagnostic.  However, some operations like searching for a
6397c478bd9Sstevel@tonic-gate  * simple filename, involve trying numerous paths, and an error message for each
6407c478bd9Sstevel@tonic-gate  * lookup is not required.  Although a multiple search can fail, it's possible
6417c478bd9Sstevel@tonic-gate  * that a file was found, but was rejected because it was the wrong type.
6427c478bd9Sstevel@tonic-gate  * To satisfy these possibilities, the first failure is recorded as a rejection
6437c478bd9Sstevel@tonic-gate  * message, and this message is used later for a more specific diagnostic.
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  * File searches are focused at load_one(), and from here a rejection descriptor
6467c478bd9Sstevel@tonic-gate  * is passed down to various child routines.  If these child routines can
6477c478bd9Sstevel@tonic-gate  * process multiple files, then they will maintain their own rejection desc-
6487c478bd9Sstevel@tonic-gate  * riptor.  This is filled in for any failures, and a diagnostic produced to
6497c478bd9Sstevel@tonic-gate  * reflect the failure.  The child routines then employ rejection_inherit() to
6507c478bd9Sstevel@tonic-gate  * pass the first rejection message back to load_one().
6517c478bd9Sstevel@tonic-gate  *
6527c478bd9Sstevel@tonic-gate  * Note that the name, and rejection string must be duplicated, as the name
6537c478bd9Sstevel@tonic-gate  * buffer and error string buffer (see conv_ routines) may be reused for
6547c478bd9Sstevel@tonic-gate  * additional processing or rejection messages.
6557c478bd9Sstevel@tonic-gate  */
6567c478bd9Sstevel@tonic-gate void
65731fdd7caSab196087 rejection_inherit(Rej_desc *rej1, Rej_desc *rej2)
6587c478bd9Sstevel@tonic-gate {
6597c478bd9Sstevel@tonic-gate 	if (rej2->rej_type && (rej1->rej_type == 0)) {
6607c478bd9Sstevel@tonic-gate 		rej1->rej_type = rej2->rej_type;
6617c478bd9Sstevel@tonic-gate 		rej1->rej_info = rej2->rej_info;
66256deab07SRod Evans 		rej1->rej_flags = rej2->rej_flags;
6637c478bd9Sstevel@tonic-gate 		if (rej2->rej_name)
66456deab07SRod Evans 			rej1->rej_name = stravl_insert(rej2->rej_name, 0, 0, 0);
66556deab07SRod Evans 		if ((rej2->rej_str) && ((rej1->rej_str =
66656deab07SRod Evans 		    stravl_insert(rej2->rej_str, 0, 0, 0)) == NULL))
6677c478bd9Sstevel@tonic-gate 			rej1->rej_str = MSG_ORIG(MSG_EMG_ENOMEM);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /*
6720aa3cd4dSrie  * Helper routine for is_so_matched() that consolidates matching a path name,
6730aa3cd4dSrie  * or file name component of a link-map name.
6747c478bd9Sstevel@tonic-gate  */
67556deab07SRod Evans inline static int
6760aa3cd4dSrie _is_so_matched(const char *name, const char *str, int path)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	const char	*_str;
6797c478bd9Sstevel@tonic-gate 
6800aa3cd4dSrie 	if ((path == 0) && ((_str = strrchr(str, '/')) != NULL))
6817c478bd9Sstevel@tonic-gate 		_str++;
6827c478bd9Sstevel@tonic-gate 	else
6837c478bd9Sstevel@tonic-gate 		_str = str;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	return (strcmp(name, _str));
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
6880aa3cd4dSrie /*
6890aa3cd4dSrie  * Determine whether a search name matches one of the names associated with a
6900aa3cd4dSrie  * link-map.  A link-map contains several names:
6910aa3cd4dSrie  *
6920aa3cd4dSrie  *  .	a NAME() - typically the full pathname of an object that has been
6930aa3cd4dSrie  *	loaded.  For example, when looking for the dependency "libc.so.1", a
69456deab07SRod Evans  *	search path is applied, with the eventual NAME() being "/lib/libc.so.1".
69556deab07SRod Evans  *	The name of a dynamic executable can be a simple filename, such as
69656deab07SRod Evans  *	"main", as this can be the name passed to exec() to start the process.
6970aa3cd4dSrie  *
6980aa3cd4dSrie  *  .	a PATHNAME() - this is maintained if the resolved NAME() is different
69956deab07SRod Evans  * 	than NAME(), ie. a component of the original name is a symbolic link.
70056deab07SRod Evans  *	This is also the resolved full pathname for a simple dynamic executable.
7010aa3cd4dSrie  *
70256deab07SRod Evans  *  .	an ALIAS() name - these are alternative names by which the object has
70356deab07SRod Evans  *	been found, ie. when dependencies are loaded through a variety of
70456deab07SRod Evans  *	different symbolic links.
7050aa3cd4dSrie  *
7060aa3cd4dSrie  * The name pattern matching can differ depending on whether we are looking
7070aa3cd4dSrie  * for a full path name (path != 0), or a simple file name (path == 0).  Full
70856deab07SRod Evans  * path names typically match NAME() or PATHNAME() entries.
7090aa3cd4dSrie  *
7100aa3cd4dSrie  * For all full path name searches, the link-map names are taken as is.  For
7110aa3cd4dSrie  * simple file name searches, only the file name component of any link-map
7120aa3cd4dSrie  * names are used for comparison.
7130aa3cd4dSrie  */
71456deab07SRod Evans inline static Rt_map *
7150aa3cd4dSrie is_so_matched(Rt_map *lmp, const char *name, int path)
7167c478bd9Sstevel@tonic-gate {
717cce0e03bSab196087 	Aliste		idx;
718cce0e03bSab196087 	const char	*cp;
7197c478bd9Sstevel@tonic-gate 
72056deab07SRod Evans 	if (_is_so_matched(name, NAME(lmp), path) == 0)
7217c478bd9Sstevel@tonic-gate 		return (lmp);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (PATHNAME(lmp) != NAME(lmp)) {
72456deab07SRod Evans 		if (_is_so_matched(name, PATHNAME(lmp), path) == 0)
7257c478bd9Sstevel@tonic-gate 			return (lmp);
7267c478bd9Sstevel@tonic-gate 	}
7270aa3cd4dSrie 
728cce0e03bSab196087 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
729cce0e03bSab196087 		if (_is_so_matched(name, cp, path) == 0)
7300aa3cd4dSrie 			return (lmp);
7310aa3cd4dSrie 	}
7320aa3cd4dSrie 
73337ffaf83SRod Evans 	return (NULL);
7347c478bd9Sstevel@tonic-gate }
7357c478bd9Sstevel@tonic-gate 
7360aa3cd4dSrie /*
7370aa3cd4dSrie  * Files are opened by ld.so.1 to satisfy dependencies, filtees and dlopen()
7380aa3cd4dSrie  * requests.  Each request investigates the file based upon the callers
7399aa23310Srie  * environment.  Once a full path name has been established, the following
7409aa23310Srie  * checks are made:
7410aa3cd4dSrie  *
7429aa23310Srie  *  .	does the path exist in the link-map lists FullPathNode AVL tree?  if
7439aa23310Srie  *	so, the file is already loaded, and its associated link-map pointer
7449aa23310Srie  *	is returned.
7459aa23310Srie  *  .	does the path exist in the not-found AVL tree?  if so, this path has
7469aa23310Srie  *	already been determined to not exist, and a failure is returned.
7479aa23310Srie  *  .	a device/inode check, to ensure the same file isn't mapped multiple
7489aa23310Srie  *	times through different paths.  See file_open().
7499aa23310Srie  *
7509aa23310Srie  * However, there are cases where a test for an existing file name needs to be
7519aa23310Srie  * carried out, such as dlopen(NOLOAD) requests, dldump() requests, and as a
7529aa23310Srie  * final fallback to dependency loading.  These requests are handled by
7539aa23310Srie  * is_so_loaded().
7540aa3cd4dSrie  *
7550aa3cd4dSrie  * A traversal through the callers link-map list is carried out, and from each
7560aa3cd4dSrie  * link-map, a comparison is made against all of the various names by which the
7579aa23310Srie  * object has been referenced.  is_so_matched() is used to compares the link-map
7589aa23310Srie  * names against the name being searched for.  Whether the search name is a full
7599aa23310Srie  * path name or a simple file name, governs what comparisons are made.
7600aa3cd4dSrie  *
7610aa3cd4dSrie  * A full path name, which is a fully resolved path name that starts with a "/"
7620aa3cd4dSrie  * character, or a relative path name that includes a "/" character, must match
76356deab07SRod Evans  * the link-map names exactly.  A simple file name, which is any name *not*
7640aa3cd4dSrie  * containing a "/" character, are matched against the file name component of
7650aa3cd4dSrie  * any link-map names.
7660aa3cd4dSrie  */
7677c478bd9Sstevel@tonic-gate Rt_map *
7689aa23310Srie is_so_loaded(Lm_list *lml, const char *name, int *in_nfavl)
7697c478bd9Sstevel@tonic-gate {
7707c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
7717c478bd9Sstevel@tonic-gate 	avl_index_t	where;
7727c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc;
773cce0e03bSab196087 	Aliste		idx;
7740aa3cd4dSrie 	int		path = 0;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	/*
7770aa3cd4dSrie 	 * If the name is a full path name, first determine if the path name is
7789aa23310Srie 	 * registered on the FullPathNode AVL, or not-found AVL trees.
7797c478bd9Sstevel@tonic-gate 	 */
7809aa23310Srie 	if (name[0] == '/') {
78156deab07SRod Evans 		uint_t	hash = sgs_str_hash(name);
78256deab07SRod Evans 
78356deab07SRod Evans 		if (((lmp = fpavl_recorded(lml, name, hash, &where)) != NULL) &&
7847c478bd9Sstevel@tonic-gate 		    ((FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE)) == 0))
7857c478bd9Sstevel@tonic-gate 			return (lmp);
78656deab07SRod Evans 
78756deab07SRod Evans 		if (nfavl_recorded(name, hash, 0)) {
7889aa23310Srie 			/*
7899aa23310Srie 			 * For dlopen() and dlsym() fall backs, indicate that
7909aa23310Srie 			 * a registered not-found path has indicated that this
7919aa23310Srie 			 * object does not exist.
7929aa23310Srie 			 */
7939aa23310Srie 			if (in_nfavl)
7949aa23310Srie 				(*in_nfavl)++;
79537ffaf83SRod Evans 			return (NULL);
7969aa23310Srie 		}
7979aa23310Srie 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	/*
8000aa3cd4dSrie 	 * Determine whether the name is a simple file name, or a path name.
8017c478bd9Sstevel@tonic-gate 	 */
8020aa3cd4dSrie 	if (strchr(name, '/'))
8030aa3cd4dSrie 		path++;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/*
8067c478bd9Sstevel@tonic-gate 	 * Loop through the callers link-map lists.
8077c478bd9Sstevel@tonic-gate 	 */
808cce0e03bSab196087 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
809cb511613SAli Bahrami 		for (lmp = lmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) {
8107c478bd9Sstevel@tonic-gate 			if (FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE))
8117c478bd9Sstevel@tonic-gate 				continue;
8127c478bd9Sstevel@tonic-gate 
8130aa3cd4dSrie 			if (is_so_matched(lmp, name, path))
8147c478bd9Sstevel@tonic-gate 				return (lmp);
8157c478bd9Sstevel@tonic-gate 		}
8167c478bd9Sstevel@tonic-gate 	}
81737ffaf83SRod Evans 	return (NULL);
8187c478bd9Sstevel@tonic-gate }
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate /*
8217c478bd9Sstevel@tonic-gate  * Tracing is enabled by the LD_TRACE_LOADED_OPTIONS environment variable which
8227c478bd9Sstevel@tonic-gate  * is normally set from ldd(1).  For each link map we load, print the load name
8237c478bd9Sstevel@tonic-gate  * and the full pathname of the shared object.
8247c478bd9Sstevel@tonic-gate  */
8257c478bd9Sstevel@tonic-gate /* ARGSUSED4 */
8267c478bd9Sstevel@tonic-gate static void
8277c478bd9Sstevel@tonic-gate trace_so(Rt_map *clmp, Rej_desc *rej, const char *name, const char *path,
8287c478bd9Sstevel@tonic-gate     int alter, const char *nfound)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	const char	*str = MSG_ORIG(MSG_STR_EMPTY);
8317c478bd9Sstevel@tonic-gate 	const char	*reject = MSG_ORIG(MSG_STR_EMPTY);
8327c478bd9Sstevel@tonic-gate 	char		_reject[PATH_MAX];
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	/*
8357c478bd9Sstevel@tonic-gate 	 * The first time through trace_so() will only have lddstub on the
8367c478bd9Sstevel@tonic-gate 	 * link-map list and the preloaded shared object is supplied as "path".
8377c478bd9Sstevel@tonic-gate 	 * As we don't want to print this shared object as a dependency, but
8387c478bd9Sstevel@tonic-gate 	 * instead inspect *its* dependencies, return.
8397c478bd9Sstevel@tonic-gate 	 */
8407c478bd9Sstevel@tonic-gate 	if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
8417c478bd9Sstevel@tonic-gate 		return;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	/*
8447c478bd9Sstevel@tonic-gate 	 * Without any rejection info, this is a supplied not-found condition.
8457c478bd9Sstevel@tonic-gate 	 */
8467c478bd9Sstevel@tonic-gate 	if (rej && (rej->rej_type == 0)) {
8477c478bd9Sstevel@tonic-gate 		(void) printf(nfound, name);
8487c478bd9Sstevel@tonic-gate 		return;
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 * If rejection information exists then establish what object was
8537c478bd9Sstevel@tonic-gate 	 * found and the reason for its rejection.
8547c478bd9Sstevel@tonic-gate 	 */
8557c478bd9Sstevel@tonic-gate 	if (rej) {
856de777a60Sab196087 		Conv_reject_desc_buf_t rej_buf;
857de777a60Sab196087 
8587c478bd9Sstevel@tonic-gate 		/* LINTED */
8597c478bd9Sstevel@tonic-gate 		(void) snprintf(_reject, PATH_MAX,
860de777a60Sab196087 		    MSG_INTL(ldd_reject[rej->rej_type]),
861ba2be530Sab196087 		    conv_reject_desc(rej, &rej_buf, M_MACH));
8627c478bd9Sstevel@tonic-gate 		if (rej->rej_name)
8637c478bd9Sstevel@tonic-gate 			path = rej->rej_name;
8647c478bd9Sstevel@tonic-gate 		reject = (char *)_reject;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 		/*
8677c478bd9Sstevel@tonic-gate 		 * Was an alternative pathname defined (from a configuration
8687c478bd9Sstevel@tonic-gate 		 * file).
8697c478bd9Sstevel@tonic-gate 		 */
87056deab07SRod Evans 		if (rej->rej_flags & FLG_REJ_ALTER)
8717c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
8727c478bd9Sstevel@tonic-gate 	} else {
8737c478bd9Sstevel@tonic-gate 		if (alter)
8747c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	/*
8787c478bd9Sstevel@tonic-gate 	 * If the load name isn't a full pathname print its associated pathname
8797c478bd9Sstevel@tonic-gate 	 * together with all the other information we've gathered.
8807c478bd9Sstevel@tonic-gate 	 */
8817c478bd9Sstevel@tonic-gate 	if (*name == '/')
8827c478bd9Sstevel@tonic-gate 		(void) printf(MSG_ORIG(MSG_LDD_FIL_PATH), path, str, reject);
8837c478bd9Sstevel@tonic-gate 	else
8847247f888Srie 		(void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV), name, path, str,
8857247f888Srie 		    reject);
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  * Establish a link-map mode, initializing it if it has just been loaded, or
8917c478bd9Sstevel@tonic-gate  * potentially updating it if it already exists.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate int
8947c478bd9Sstevel@tonic-gate update_mode(Rt_map *lmp, int omode, int nmode)
8957c478bd9Sstevel@tonic-gate {
896dffec89cSrie 	Lm_list	*lml = LIST(lmp);
8977c478bd9Sstevel@tonic-gate 	int	pmode = 0;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * A newly loaded object hasn't had its mode set yet.  Modes are used to
9017c478bd9Sstevel@tonic-gate 	 * load dependencies, so don't propagate any parent or no-load flags, as
9027c478bd9Sstevel@tonic-gate 	 * these would adversely affect this objects ability to load any of its
9037c478bd9Sstevel@tonic-gate 	 * dependencies that aren't already loaded.  RTLD_FIRST is applicable to
9047c478bd9Sstevel@tonic-gate 	 * this objects handle creation only, and should not be propagated.
9057c478bd9Sstevel@tonic-gate 	 */
9067c478bd9Sstevel@tonic-gate 	if ((FLAGS(lmp) & FLG_RT_MODESET) == 0) {
9077c478bd9Sstevel@tonic-gate 		MODE(lmp) |= nmode & ~(RTLD_PARENT | RTLD_NOLOAD | RTLD_FIRST);
9087c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_MODESET;
9097c478bd9Sstevel@tonic-gate 		return (1);
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * Establish any new overriding modes.  RTLD_LAZY and RTLD_NOW should be
9147c478bd9Sstevel@tonic-gate 	 * represented individually (this is historic, as these two flags were
9157c478bd9Sstevel@tonic-gate 	 * the only flags originally available to dlopen()).  Other flags are
9167c478bd9Sstevel@tonic-gate 	 * accumulative, but have a hierarchy of preference.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 	if ((omode & RTLD_LAZY) && (nmode & RTLD_NOW)) {
9197c478bd9Sstevel@tonic-gate 		MODE(lmp) &= ~RTLD_LAZY;
9207c478bd9Sstevel@tonic-gate 		pmode |= RTLD_NOW;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	pmode |= ((~omode & nmode) &
9247c478bd9Sstevel@tonic-gate 	    (RTLD_GLOBAL | RTLD_WORLD | RTLD_NODELETE));
9257c478bd9Sstevel@tonic-gate 	if (pmode) {
9265aefb655Srie 		DBG_CALL(Dbg_file_mode_promote(lmp, pmode));
9277c478bd9Sstevel@tonic-gate 		MODE(lmp) |= pmode;
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	/*
9317c478bd9Sstevel@tonic-gate 	 * If this load is an RTLD_NOW request and the object has already been
9327c478bd9Sstevel@tonic-gate 	 * loaded non-RTLD_NOW, append this object to the relocation-now list
9337c478bd9Sstevel@tonic-gate 	 * of the objects associated control list.  Note, if the object hasn't
9347c478bd9Sstevel@tonic-gate 	 * yet been relocated, setting its MODE() to RTLD_NOW will establish
9357c478bd9Sstevel@tonic-gate 	 * full relocation processing when it eventually gets relocated.
9367c478bd9Sstevel@tonic-gate 	 */
9377c478bd9Sstevel@tonic-gate 	if ((pmode & RTLD_NOW) &&
9387c478bd9Sstevel@tonic-gate 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))) {
9397c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		/* LINTED */
942cce0e03bSab196087 		lmc = (Lm_cntl *)alist_item_by_offset(LIST(lmp)->lm_lists,
943cce0e03bSab196087 		    CNTL(lmp));
944cce0e03bSab196087 		(void) aplist_append(&lmc->lc_now, lmp, AL_CNT_LMNOW);
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	/*
948dffec89cSrie 	 * If this objects .init has been collected but has not yet been called,
949dffec89cSrie 	 * it may be necessary to reevaluate the object using tsort().  For
9507c478bd9Sstevel@tonic-gate 	 * example, a new dlopen() hierarchy may bind to uninitialized objects
9517c478bd9Sstevel@tonic-gate 	 * that are already loaded, or a dlopen(RTLD_NOW) can establish new
9527c478bd9Sstevel@tonic-gate 	 * bindings between already loaded objects that require the tsort()
953dffec89cSrie 	 * information be recomputed.  If however, no new objects have been
954dffec89cSrie 	 * added to the process, and this object hasn't been promoted, don't
955dffec89cSrie 	 * bother reevaluating the .init.  The present tsort() information is
956dffec89cSrie 	 * probably as accurate as necessary, and by not establishing a parallel
957dffec89cSrie 	 * tsort() we can help reduce the amount of recursion possible between
958dffec89cSrie 	 * .inits.
9597c478bd9Sstevel@tonic-gate 	 */
960dffec89cSrie 	if (((FLAGS(lmp) &
961dffec89cSrie 	    (FLG_RT_INITCLCT | FLG_RT_INITCALL)) == FLG_RT_INITCLCT) &&
962dffec89cSrie 	    ((lml->lm_flags & LML_FLG_OBJADDED) || ((pmode & RTLD_NOW) &&
963dffec89cSrie 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))))) {
9647c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_INITCLCT;
9657c478bd9Sstevel@tonic-gate 		LIST(lmp)->lm_init++;
966dffec89cSrie 		LIST(lmp)->lm_flags |= LML_FLG_OBJREEVAL;
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	return (pmode);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /*
9737c478bd9Sstevel@tonic-gate  * Determine whether an alias name already exists, and if not create one.  This
9747c478bd9Sstevel@tonic-gate  * is typically used to retain dependency names, such as "libc.so.1", which
9757c478bd9Sstevel@tonic-gate  * would have been expanded to full path names when they were loaded.  The
97656deab07SRod Evans  * full path names (NAME() and possibly PATHNAME()) are maintained on the
97756deab07SRod Evans  * FullPathNode AVL tree, and thus would have been matched by fpavl_loaded()
97856deab07SRod Evans  * during file_open().
9797c478bd9Sstevel@tonic-gate  */
9807c478bd9Sstevel@tonic-gate int
9817c478bd9Sstevel@tonic-gate append_alias(Rt_map *lmp, const char *str, int *added)
9827c478bd9Sstevel@tonic-gate {
98356deab07SRod Evans 	const char	*cp;
984cce0e03bSab196087 	Aliste		idx;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/*
9877c478bd9Sstevel@tonic-gate 	 * Determine if this filename is already on the alias list.
9887c478bd9Sstevel@tonic-gate 	 */
989cce0e03bSab196087 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
990cce0e03bSab196087 		if (strcmp(cp, str) == 0)
9917c478bd9Sstevel@tonic-gate 			return (1);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	/*
9957c478bd9Sstevel@tonic-gate 	 * This is a new alias, append it to the alias list.
9967c478bd9Sstevel@tonic-gate 	 */
99756deab07SRod Evans 	if (((cp = stravl_insert(str, 0, 0, 0)) == NULL) ||
99856deab07SRod Evans 	    (aplist_append(&ALIAS(lmp), cp, AL_CNT_ALIAS) == NULL))
9997c478bd9Sstevel@tonic-gate 		return (0);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	if (added)
10027c478bd9Sstevel@tonic-gate 		*added = 1;
10037c478bd9Sstevel@tonic-gate 	return (1);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate /*
10077c478bd9Sstevel@tonic-gate  * Determine whether a file is already loaded by comparing device and inode
10087c478bd9Sstevel@tonic-gate  * values.
10097c478bd9Sstevel@tonic-gate  */
10107c478bd9Sstevel@tonic-gate static Rt_map *
1011cb511613SAli Bahrami is_devinode_loaded(rtld_stat_t *status, Lm_list *lml, const char *name,
10127c478bd9Sstevel@tonic-gate     uint_t flags)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
1015cce0e03bSab196087 	Aliste	idx;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	/*
10187c478bd9Sstevel@tonic-gate 	 * If this is an auditor, it will have been opened on a new link-map.
10198af2c5b9Srie 	 * To prevent multiple occurrences of the same auditor on multiple
10207c478bd9Sstevel@tonic-gate 	 * link-maps, search the head of each link-map list and see if this
10217c478bd9Sstevel@tonic-gate 	 * object is already loaded as an auditor.
10227c478bd9Sstevel@tonic-gate 	 */
10237c478bd9Sstevel@tonic-gate 	if (flags & FLG_RT_AUDIT) {
10247c478bd9Sstevel@tonic-gate 		Lm_list	*lml;
10257c478bd9Sstevel@tonic-gate 
1026*57ef7aa9SRod Evans 		for (APLIST_TRAVERSE(dynlm_list, idx, lml)) {
10277c478bd9Sstevel@tonic-gate 			Rt_map	*nlmp = lml->lm_head;
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 			if (nlmp && ((FLAGS(nlmp) &
10307c478bd9Sstevel@tonic-gate 			    (FLG_RT_AUDIT | FLG_RT_DELETE)) == FLG_RT_AUDIT) &&
10317c478bd9Sstevel@tonic-gate 			    (STDEV(nlmp) == status->st_dev) &&
10327c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) == status->st_ino))
10337c478bd9Sstevel@tonic-gate 				return (nlmp);
10347c478bd9Sstevel@tonic-gate 		}
103537ffaf83SRod Evans 		return (NULL);
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	/*
10397c478bd9Sstevel@tonic-gate 	 * If the file has been found determine from the new files status
10407c478bd9Sstevel@tonic-gate 	 * information if this file is actually linked to one we already have
10417c478bd9Sstevel@tonic-gate 	 * mapped.  This catches symlink names not caught by is_so_loaded().
10427c478bd9Sstevel@tonic-gate 	 */
1043cce0e03bSab196087 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
10447c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp;
10457c478bd9Sstevel@tonic-gate 
1046cb511613SAli Bahrami 		for (nlmp = lmc->lc_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
10477c478bd9Sstevel@tonic-gate 			if ((FLAGS(nlmp) & FLG_RT_DELETE) ||
10487c478bd9Sstevel@tonic-gate 			    (FLAGS1(nlmp) & FL1_RT_LDDSTUB))
10497c478bd9Sstevel@tonic-gate 				continue;
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 			if ((STDEV(nlmp) != status->st_dev) ||
10527c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) != status->st_ino))
10537c478bd9Sstevel@tonic-gate 				continue;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_VERBOSE) {
10567247f888Srie 				/* BEGIN CSTYLED */
10577c478bd9Sstevel@tonic-gate 				if (*name == '/')
10587c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
10597c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
10607c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
10617c478bd9Sstevel@tonic-gate 				else
10627c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
10637c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
10647c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
10657c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
10667247f888Srie 				/* END CSTYLED */
10677c478bd9Sstevel@tonic-gate 			}
10687c478bd9Sstevel@tonic-gate 			return (nlmp);
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 	}
107137ffaf83SRod Evans 	return (NULL);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate  * Generate any error messages indicating a file could not be found.  When
10767c478bd9Sstevel@tonic-gate  * preloading or auditing a secure application, it can be a little more helpful
10777c478bd9Sstevel@tonic-gate  * to indicate that a search of secure directories has failed, so adjust the
10787c478bd9Sstevel@tonic-gate  * messages accordingly.
10797c478bd9Sstevel@tonic-gate  */
10807c478bd9Sstevel@tonic-gate void
10817c478bd9Sstevel@tonic-gate file_notfound(Lm_list *lml, const char *name, Rt_map *clmp, uint_t flags,
10827c478bd9Sstevel@tonic-gate     Rej_desc *rej)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	int	secure = 0;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SECURE) &&
10877c478bd9Sstevel@tonic-gate 	    (flags & (FLG_RT_PRELOAD | FLG_RT_AUDIT)))
10887c478bd9Sstevel@tonic-gate 		secure++;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
10917c478bd9Sstevel@tonic-gate 		/*
10927c478bd9Sstevel@tonic-gate 		 * Under ldd(1), auxiliary filtees that can't be loaded are
10937c478bd9Sstevel@tonic-gate 		 * ignored, unless verbose errors are requested.
10947c478bd9Sstevel@tonic-gate 		 */
10957c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) &&
10967c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_TRC_VERBOSE) == 0))
10977c478bd9Sstevel@tonic-gate 			return;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		if (secure)
11007c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
11017c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_SEC_NFOUND));
11027c478bd9Sstevel@tonic-gate 		else
11037c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
11047c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_FIL_NFOUND));
11057c478bd9Sstevel@tonic-gate 		return;
11067c478bd9Sstevel@tonic-gate 	}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
1109de777a60Sab196087 		Conv_reject_desc_buf_t rej_buf;
1110de777a60Sab196087 
11115aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(err_reject[rej->rej_type]),
11127c478bd9Sstevel@tonic-gate 		    rej->rej_name ? rej->rej_name : MSG_INTL(MSG_STR_UNKNOWN),
1113ba2be530Sab196087 		    conv_reject_desc(rej, &rej_buf, M_MACH));
11147c478bd9Sstevel@tonic-gate 		return;
11157c478bd9Sstevel@tonic-gate 	}
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 	if (secure)
11185aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SEC_OPEN), name);
11197c478bd9Sstevel@tonic-gate 	else
11205aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), name,
11217c478bd9Sstevel@tonic-gate 		    strerror(ENOENT));
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate static int
112556deab07SRod Evans file_open(int err, Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp,
112656deab07SRod Evans     Rej_desc *rej, int *in_nfavl)
11277c478bd9Sstevel@tonic-gate {
1128cb511613SAli Bahrami 	rtld_stat_t	status;
11297c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
11309aa23310Srie 	avl_index_t	nfavlwhere = 0;
113156deab07SRod Evans 	const char	*oname = fdp->fd_oname, *nname = fdp->fd_nname;
113256deab07SRod Evans 	uint_t		hash = sgs_str_hash(nname);
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 
113556deab07SRod Evans 	if ((nname = stravl_insert(fdp->fd_nname, hash, 0, 0)) == NULL)
113656deab07SRod Evans 		return (0);
113756deab07SRod Evans 	fdp->fd_nname = nname;
113856deab07SRod Evans 
113956deab07SRod Evans 	if ((err == 0) && (fdp->fd_flags & FLG_FD_ALTER))
11405aefb655Srie 		DBG_CALL(Dbg_file_config_obj(lml, oname, 0, nname));
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	/*
11437c478bd9Sstevel@tonic-gate 	 * If we're dealing with a full pathname, determine whether this
11447c478bd9Sstevel@tonic-gate 	 * pathname is already known.  Other pathnames fall through to the
11457c478bd9Sstevel@tonic-gate 	 * dev/inode check, as even though the pathname may look the same as
11467c478bd9Sstevel@tonic-gate 	 * one previously used, the process may have changed directory.
11477c478bd9Sstevel@tonic-gate 	 */
11487c478bd9Sstevel@tonic-gate 	if ((err == 0) && (nname[0] == '/')) {
114956deab07SRod Evans 		if ((nlmp = fpavl_recorded(lml, nname, hash,
115056deab07SRod Evans 		    &(fdp->fd_avlwhere))) != NULL) {
115156deab07SRod Evans 			fdp->fd_lmp = nlmp;
11527c478bd9Sstevel@tonic-gate 			return (1);
11537c478bd9Sstevel@tonic-gate 		}
115456deab07SRod Evans 		if (nfavl_recorded(nname, hash, &nfavlwhere)) {
11559aa23310Srie 			/*
11569aa23310Srie 			 * For dlopen() and dlsym() fall backs, indicate that
11579aa23310Srie 			 * a registered not-found path has indicated that this
11589aa23310Srie 			 * object does not exist.  If this path has been
11599aa23310Srie 			 * constructed as part of expanding a HWCAP directory,
116056deab07SRod Evans 			 * this is a silent failure, where no rejection message
116156deab07SRod Evans 			 * is created.
11629aa23310Srie 			 */
11639aa23310Srie 			if (in_nfavl)
11649aa23310Srie 				(*in_nfavl)++;
11659aa23310Srie 			return (0);
11669aa23310Srie 		}
11677c478bd9Sstevel@tonic-gate 	}
11687c478bd9Sstevel@tonic-gate 
1169cb511613SAli Bahrami 	if ((err == 0) && ((rtld_stat(nname, &status)) != -1)) {
11707c478bd9Sstevel@tonic-gate 		char	path[PATH_MAX];
11717c478bd9Sstevel@tonic-gate 		int	fd, size, added;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 		/*
11747c478bd9Sstevel@tonic-gate 		 * If this path has been constructed as part of expanding a
11758521e5e6Srie 		 * HWCAP directory, ignore any subdirectories.  As this is a
117656deab07SRod Evans 		 * silent failure, no rejection message is created.  For any
117756deab07SRod Evans 		 * other reference that expands to a directory, fall through
11788af2c5b9Srie 		 * to construct a meaningful rejection message.
11797c478bd9Sstevel@tonic-gate 		 */
11807c478bd9Sstevel@tonic-gate 		if ((flags & FLG_RT_HWCAP) &&
118156deab07SRod Evans 		    ((status.st_mode & S_IFMT) == S_IFDIR))
118256deab07SRod Evans 			return (0);
118356deab07SRod Evans 
118456deab07SRod Evans 		/*
118556deab07SRod Evans 		 * If this is a directory (which can't be mmap()'ed) generate a
118656deab07SRod Evans 		 * precise error message.
118756deab07SRod Evans 		 */
118856deab07SRod Evans 		if ((status.st_mode & S_IFMT) == S_IFDIR) {
118956deab07SRod Evans 			rej->rej_name = nname;
119056deab07SRod Evans 			if (fdp->fd_flags & FLG_FD_ALTER)
119156deab07SRod Evans 				rej->rej_flags = FLG_REJ_ALTER;
119256deab07SRod Evans 			rej->rej_type = SGS_REJ_STR;
119356deab07SRod Evans 			rej->rej_str = strerror(EISDIR);
119456deab07SRod Evans 			DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
11957c478bd9Sstevel@tonic-gate 			return (0);
11968521e5e6Srie 		}
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 		/*
11997c478bd9Sstevel@tonic-gate 		 * Resolve the filename and determine whether the resolved name
12007c478bd9Sstevel@tonic-gate 		 * is already known.  Typically, the previous fpavl_loaded()
12017c478bd9Sstevel@tonic-gate 		 * will have caught this, as both NAME() and PATHNAME() for a
120256deab07SRod Evans 		 * link-map are recorded in the FullPathNode AVL tree.  However,
12037c478bd9Sstevel@tonic-gate 		 * instances exist where a file can be replaced (loop-back
12047c478bd9Sstevel@tonic-gate 		 * mounts, bfu, etc.), and reference is made to the original
12057c478bd9Sstevel@tonic-gate 		 * file through a symbolic link.  By checking the pathname here,
12067c478bd9Sstevel@tonic-gate 		 * we don't fall through to the dev/inode check and conclude
12077c478bd9Sstevel@tonic-gate 		 * that a new file should be loaded.
12087c478bd9Sstevel@tonic-gate 		 */
120956deab07SRod Evans 		if ((nname[0] == '/') &&
12107c478bd9Sstevel@tonic-gate 		    ((size = resolvepath(nname, path, (PATH_MAX - 1))) > 0)) {
12117c478bd9Sstevel@tonic-gate 			path[size] = '\0';
12127c478bd9Sstevel@tonic-gate 
121356deab07SRod Evans 			fdp->fd_flags |= FLG_FD_RESOLVED;
121456deab07SRod Evans 
12157c478bd9Sstevel@tonic-gate 			if (strcmp(nname, path)) {
121624a6229eSrie 				if ((nlmp =
121756deab07SRod Evans 				    fpavl_recorded(lml, path, 0, 0)) != NULL) {
12187c478bd9Sstevel@tonic-gate 					added = 0;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 					if (append_alias(nlmp, nname,
12217c478bd9Sstevel@tonic-gate 					    &added) == 0)
12227c478bd9Sstevel@tonic-gate 						return (0);
12237247f888Srie 					/* BEGIN CSTYLED */
12247c478bd9Sstevel@tonic-gate 					if (added)
122510a4fa49Srie 					    DBG_CALL(Dbg_file_skip(LIST(clmp),
12265aefb655Srie 						NAME(nlmp), nname));
12277247f888Srie 					/* END CSTYLED */
122856deab07SRod Evans 					fdp->fd_lmp = nlmp;
12297c478bd9Sstevel@tonic-gate 					return (1);
12307c478bd9Sstevel@tonic-gate 				}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 				/*
12337c478bd9Sstevel@tonic-gate 				 * If this pathname hasn't been loaded, save
12347c478bd9Sstevel@tonic-gate 				 * the resolved pathname so that it doesn't
12357c478bd9Sstevel@tonic-gate 				 * have to be recomputed as part of fullpath()
12367c478bd9Sstevel@tonic-gate 				 * processing.
12377c478bd9Sstevel@tonic-gate 				 */
123856deab07SRod Evans 				if ((fdp->fd_pname = stravl_insert(path, 0,
123956deab07SRod Evans 				    (size + 1), 0)) == NULL)
12407c478bd9Sstevel@tonic-gate 					return (0);
12417c478bd9Sstevel@tonic-gate 			}
12427c478bd9Sstevel@tonic-gate 		}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 		if (nlmp = is_devinode_loaded(&status, lml, nname, flags)) {
1245c75e1b9dSrie 			if (flags & FLG_RT_AUDIT) {
1246c75e1b9dSrie 				/*
1247c75e1b9dSrie 				 * If we've been requested to load an auditor,
1248c75e1b9dSrie 				 * and an auditor of the same name already
1249c75e1b9dSrie 				 * exists, then the original auditor is used.
1250c75e1b9dSrie 				 */
1251c75e1b9dSrie 				DBG_CALL(Dbg_audit_skip(LIST(clmp),
1252c75e1b9dSrie 				    NAME(nlmp), LIST(nlmp)->lm_lmidstr));
1253c75e1b9dSrie 			} else {
1254c75e1b9dSrie 				/*
1255c75e1b9dSrie 				 * Otherwise, if an alternatively named file
1256c75e1b9dSrie 				 * has been found for the same dev/inode, add
125756deab07SRod Evans 				 * a new name alias.  Insert any alias full path
125856deab07SRod Evans 				 * name in the FullPathNode AVL tree.
1259c75e1b9dSrie 				 */
12607c478bd9Sstevel@tonic-gate 				added = 0;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 				if (append_alias(nlmp, nname, &added) == 0)
12637c478bd9Sstevel@tonic-gate 					return (0);
12647c478bd9Sstevel@tonic-gate 				if (added) {
1265c75e1b9dSrie 					if ((nname[0] == '/') &&
1266c75e1b9dSrie 					    (fpavl_insert(lml, nlmp,
1267c75e1b9dSrie 					    nname, 0) == 0))
12687c478bd9Sstevel@tonic-gate 						return (0);
1269c75e1b9dSrie 					DBG_CALL(Dbg_file_skip(LIST(clmp),
1270c75e1b9dSrie 					    NAME(nlmp), nname));
12717c478bd9Sstevel@tonic-gate 				}
1272c75e1b9dSrie 			}
1273c75e1b9dSrie 
1274c75e1b9dSrie 			/*
1275c75e1b9dSrie 			 * Record in the file descriptor the existing object
1276c75e1b9dSrie 			 * that satisfies this open request.
1277c75e1b9dSrie 			 */
127856deab07SRod Evans 			fdp->fd_lmp = nlmp;
12797c478bd9Sstevel@tonic-gate 			return (1);
12807c478bd9Sstevel@tonic-gate 		}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 		if ((fd = open(nname, O_RDONLY, 0)) == -1) {
12837c478bd9Sstevel@tonic-gate 			/*
12847c478bd9Sstevel@tonic-gate 			 * As the file must exist for the previous stat() to
12857c478bd9Sstevel@tonic-gate 			 * have succeeded, record the error condition.
12867c478bd9Sstevel@tonic-gate 			 */
12877c478bd9Sstevel@tonic-gate 			rej->rej_type = SGS_REJ_STR;
12887c478bd9Sstevel@tonic-gate 			rej->rej_str = strerror(errno);
12897c478bd9Sstevel@tonic-gate 		} else {
129056deab07SRod Evans 			/*
129156deab07SRod Evans 			 * Map the object.  A successful return indicates that
129256deab07SRod Evans 			 * the object is appropriate for ld.so.1 processing.
129356deab07SRod Evans 			 */
129456deab07SRod Evans 			fdp->fd_ftp = map_obj(lml, fdp, status.st_size, nname,
129556deab07SRod Evans 			    fd, rej);
129656deab07SRod Evans 			(void) close(fd);
12977c478bd9Sstevel@tonic-gate 
129856deab07SRod Evans 			if (fdp->fd_ftp != NULL) {
129956deab07SRod Evans 				fdp->fd_dev = status.st_dev;
130056deab07SRod Evans 				fdp->fd_ino = status.st_ino;
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 				/*
13037c478bd9Sstevel@tonic-gate 				 * Trace that this open has succeeded.
13047c478bd9Sstevel@tonic-gate 				 */
13057c478bd9Sstevel@tonic-gate 				if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
13067c478bd9Sstevel@tonic-gate 					trace_so(clmp, 0, oname, nname,
130756deab07SRod Evans 					    (fdp->fd_flags & FLG_FD_ALTER), 0);
13087c478bd9Sstevel@tonic-gate 				}
13097c478bd9Sstevel@tonic-gate 				return (1);
13107c478bd9Sstevel@tonic-gate 			}
13117c478bd9Sstevel@tonic-gate 		}
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate 	} else if (errno != ENOENT) {
13147c478bd9Sstevel@tonic-gate 		/*
13157c478bd9Sstevel@tonic-gate 		 * If the open() failed for anything other than the file not
13167c478bd9Sstevel@tonic-gate 		 * existing, record the error condition.
13177c478bd9Sstevel@tonic-gate 		 */
13187c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
13197c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(errno);
13207c478bd9Sstevel@tonic-gate 	}
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	/*
13239aa23310Srie 	 * Regardless of error, duplicate and record any full path names that
13249aa23310Srie 	 * can't be used on the "not-found" AVL tree.
13259aa23310Srie 	 */
132656deab07SRod Evans 	if (nname[0] == '/')
132756deab07SRod Evans 		nfavl_insert(nname, nfavlwhere);
13289aa23310Srie 
13299aa23310Srie 	/*
13307c478bd9Sstevel@tonic-gate 	 * Indicate any rejection.
13317c478bd9Sstevel@tonic-gate 	 */
13327c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
13337c478bd9Sstevel@tonic-gate 		rej->rej_name = nname;
133456deab07SRod Evans 		if (fdp->fd_flags & FLG_FD_ALTER)
133556deab07SRod Evans 			rej->rej_flags = FLG_REJ_ALTER;
1336ba2be530Sab196087 		DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
13377c478bd9Sstevel@tonic-gate 	}
13387c478bd9Sstevel@tonic-gate 	return (0);
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate /*
13427c478bd9Sstevel@tonic-gate  * Find a full pathname (it contains a "/").
13437c478bd9Sstevel@tonic-gate  */
13447c478bd9Sstevel@tonic-gate int
134556deab07SRod Evans find_path(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
134656deab07SRod Evans     int *in_nfavl)
13477c478bd9Sstevel@tonic-gate {
134856deab07SRod Evans 	const char	*oname = fdp->fd_oname;
13497c478bd9Sstevel@tonic-gate 	int		err = 0;
13507c478bd9Sstevel@tonic-gate 
13517c478bd9Sstevel@tonic-gate 	/*
13527c478bd9Sstevel@tonic-gate 	 * If directory configuration exists determine if this path is known.
13537c478bd9Sstevel@tonic-gate 	 */
13547c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DIRCFG) {
13557c478bd9Sstevel@tonic-gate 		Rtc_obj		*obj;
13567c478bd9Sstevel@tonic-gate 		const char	*aname;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 		if ((obj = elf_config_ent(oname, (Word)elf_hash(oname),
13597c478bd9Sstevel@tonic-gate 		    0, &aname)) != 0) {
13607c478bd9Sstevel@tonic-gate 			/*
13617c478bd9Sstevel@tonic-gate 			 * If the configuration file states that this path is a
13627c478bd9Sstevel@tonic-gate 			 * directory, or the path is explicitly defined as
13637c478bd9Sstevel@tonic-gate 			 * non-existent (ie. a unused platform specific
13647c478bd9Sstevel@tonic-gate 			 * library), then go no further.
13657c478bd9Sstevel@tonic-gate 			 */
13667c478bd9Sstevel@tonic-gate 			if (obj->co_flags & RTC_OBJ_DIRENT) {
13677c478bd9Sstevel@tonic-gate 				err = EISDIR;
13687c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags &
13697c478bd9Sstevel@tonic-gate 			    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) ==
13707c478bd9Sstevel@tonic-gate 			    RTC_OBJ_NOEXIST) {
13717c478bd9Sstevel@tonic-gate 				err = ENOENT;
13727c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags & RTC_OBJ_ALTER) &&
13737c478bd9Sstevel@tonic-gate 			    (rtld_flags & RT_FL_OBJALT) && (lml == &lml_main)) {
13747c478bd9Sstevel@tonic-gate 				int	ret;
13757c478bd9Sstevel@tonic-gate 
137656deab07SRod Evans 				fdp->fd_flags |= FLG_FD_ALTER;
137756deab07SRod Evans 				fdp->fd_nname = aname;
137856deab07SRod Evans 
13797c478bd9Sstevel@tonic-gate 				/*
13807c478bd9Sstevel@tonic-gate 				 * Attempt to open the alternative path.  If
13817c478bd9Sstevel@tonic-gate 				 * this fails, and the alternative is flagged
13827c478bd9Sstevel@tonic-gate 				 * as optional, fall through to open the
13837c478bd9Sstevel@tonic-gate 				 * original path.
13847c478bd9Sstevel@tonic-gate 				 */
13855aefb655Srie 				DBG_CALL(Dbg_libs_found(lml, aname,
13865aefb655Srie 				    FLG_FD_ALTER));
138756deab07SRod Evans 				ret = file_open(0, lml, clmp, flags, fdp,
138856deab07SRod Evans 				    rej, in_nfavl);
138956deab07SRod Evans 				if (ret || ((obj->co_flags &
139056deab07SRod Evans 				    RTC_OBJ_OPTINAL) == 0))
13917c478bd9Sstevel@tonic-gate 					return (ret);
13927c478bd9Sstevel@tonic-gate 
139356deab07SRod Evans 				fdp->fd_flags &= ~FLG_FD_ALTER;
13947c478bd9Sstevel@tonic-gate 			}
13957c478bd9Sstevel@tonic-gate 		}
13967c478bd9Sstevel@tonic-gate 	}
13975aefb655Srie 	DBG_CALL(Dbg_libs_found(lml, oname, 0));
139856deab07SRod Evans 	fdp->fd_nname = oname;
139956deab07SRod Evans 	return (file_open(err, lml, clmp, flags, fdp, rej, in_nfavl));
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate /*
14037c478bd9Sstevel@tonic-gate  * Find a simple filename (it doesn't contain a "/").
14047c478bd9Sstevel@tonic-gate  */
14057c478bd9Sstevel@tonic-gate static int
140656deab07SRod Evans _find_file(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
140756deab07SRod Evans     Pdesc *pdp, int aflag, int *in_nfavl)
14087c478bd9Sstevel@tonic-gate {
140956deab07SRod Evans 	const char	*nname = fdp->fd_nname;
141056deab07SRod Evans 
14115aefb655Srie 	DBG_CALL(Dbg_libs_found(lml, nname, aflag));
14127c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_TRC_SEARCH) &&
14137c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
14147c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_PTH_TRYING), nname, aflag ?
14157c478bd9Sstevel@tonic-gate 		    MSG_INTL(MSG_LDD_FIL_ALTER) : MSG_ORIG(MSG_STR_EMPTY));
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	/*
14197c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're about
14207c478bd9Sstevel@tonic-gate 	 * to go search for.  The audit library may offer an alternative
14217c478bd9Sstevel@tonic-gate 	 * dependency, or indicate that this dependency should be ignored.
14227c478bd9Sstevel@tonic-gate 	 */
142356deab07SRod Evans 	if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
14249aa23310Srie 		char	*aname;
14257c478bd9Sstevel@tonic-gate 
14269aa23310Srie 		if ((aname = audit_objsearch(clmp, nname,
142756deab07SRod Evans 		    (pdp->pd_flags & LA_SER_MASK))) == NULL) {
14287247f888Srie 			DBG_CALL(Dbg_audit_terminate(lml, nname));
14297c478bd9Sstevel@tonic-gate 			return (0);
14307247f888Srie 		}
14317247f888Srie 
143256deab07SRod Evans 		if (aname != nname) {
143356deab07SRod Evans 			fdp->fd_flags &= ~FLG_FD_SLASH;
143456deab07SRod Evans 			fdp->fd_nname = aname;
14357c478bd9Sstevel@tonic-gate 		}
143656deab07SRod Evans 	}
143756deab07SRod Evans 	return (file_open(0, lml, clmp, flags, fdp, rej, in_nfavl));
14387c478bd9Sstevel@tonic-gate }
14397c478bd9Sstevel@tonic-gate 
1440390b98b5Srie static int
144156deab07SRod Evans find_file(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
144256deab07SRod Evans     Pdesc *pdp, Word *strhash, int *in_nfavl)
14437c478bd9Sstevel@tonic-gate {
14447c478bd9Sstevel@tonic-gate 	static Rtc_obj	Obj = { 0 };
14457c478bd9Sstevel@tonic-gate 	Rtc_obj		*dobj;
144656deab07SRod Evans 	const char	*oname = fdp->fd_oname;
144756deab07SRod Evans 	size_t		olen = strlen(oname);
14487c478bd9Sstevel@tonic-gate 
144956deab07SRod Evans 	if (pdp->pd_pname == NULL)
14507c478bd9Sstevel@tonic-gate 		return (0);
145156deab07SRod Evans 	if (pdp->pd_info) {
145256deab07SRod Evans 		dobj = (Rtc_obj *)pdp->pd_info;
14537c478bd9Sstevel@tonic-gate 		if ((dobj->co_flags &
14547c478bd9Sstevel@tonic-gate 		    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
14557c478bd9Sstevel@tonic-gate 			return (0);
14567c478bd9Sstevel@tonic-gate 	} else
145756deab07SRod Evans 		dobj = NULL;
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	/*
14607c478bd9Sstevel@tonic-gate 	 * If configuration information exists see if this directory/file
14617c478bd9Sstevel@tonic-gate 	 * combination exists.
14627c478bd9Sstevel@tonic-gate 	 */
14637c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_DIRCFG) &&
146456deab07SRod Evans 	    ((dobj == NULL) || (dobj->co_id != 0))) {
14657c478bd9Sstevel@tonic-gate 		Rtc_obj		*fobj;
146656deab07SRod Evans 		const char	*aname = NULL;
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 		/*
146956deab07SRod Evans 		 * If this object descriptor has not yet been searched for in
147056deab07SRod Evans 		 * the configuration file go find it.
14717c478bd9Sstevel@tonic-gate 		 */
147256deab07SRod Evans 		if (dobj == NULL) {
147356deab07SRod Evans 			dobj = elf_config_ent(pdp->pd_pname,
147456deab07SRod Evans 			    (Word)elf_hash(pdp->pd_pname), 0, 0);
147556deab07SRod Evans 			if (dobj == NULL)
14767c478bd9Sstevel@tonic-gate 				dobj = &Obj;
147756deab07SRod Evans 			pdp->pd_info = (void *)dobj;
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate 			if ((dobj->co_flags & (RTC_OBJ_NOEXIST |
14807c478bd9Sstevel@tonic-gate 			    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
14817c478bd9Sstevel@tonic-gate 				return (0);
14827c478bd9Sstevel@tonic-gate 		}
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 		/*
14857c478bd9Sstevel@tonic-gate 		 * If we found a directory search for the file.
14867c478bd9Sstevel@tonic-gate 		 */
14877c478bd9Sstevel@tonic-gate 		if (dobj->co_id != 0) {
148856deab07SRod Evans 			if (*strhash == NULL)
148956deab07SRod Evans 				*strhash = (Word)elf_hash(oname);
149056deab07SRod Evans 			fobj = elf_config_ent(oname, *strhash,
149156deab07SRod Evans 			    dobj->co_id, &aname);
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 			/*
14947c478bd9Sstevel@tonic-gate 			 * If this object specifically does not exist, or the
14957c478bd9Sstevel@tonic-gate 			 * object can't be found in a know-all-entries
14967c478bd9Sstevel@tonic-gate 			 * directory, continue looking.  If the object does
14977c478bd9Sstevel@tonic-gate 			 * exist determine if an alternative object exists.
14987c478bd9Sstevel@tonic-gate 			 */
149956deab07SRod Evans 			if (fobj == NULL) {
15007c478bd9Sstevel@tonic-gate 				if (dobj->co_flags & RTC_OBJ_ALLENTS)
15017c478bd9Sstevel@tonic-gate 					return (0);
15027c478bd9Sstevel@tonic-gate 			} else {
15037c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & (RTC_OBJ_NOEXIST |
15047c478bd9Sstevel@tonic-gate 				    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
15057c478bd9Sstevel@tonic-gate 					return (0);
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & RTC_OBJ_ALTER) &&
15087c478bd9Sstevel@tonic-gate 				    (rtld_flags & RT_FL_OBJALT) &&
15097c478bd9Sstevel@tonic-gate 				    (lml == &lml_main)) {
15107c478bd9Sstevel@tonic-gate 					int	ret;
15117c478bd9Sstevel@tonic-gate 
151256deab07SRod Evans 					fdp->fd_flags |= FLG_FD_ALTER;
151356deab07SRod Evans 					fdp->fd_nname = aname;
151456deab07SRod Evans 
15157c478bd9Sstevel@tonic-gate 					/*
15167c478bd9Sstevel@tonic-gate 					 * Attempt to open the alternative path.
15177c478bd9Sstevel@tonic-gate 					 * If this fails, and the alternative is
15187c478bd9Sstevel@tonic-gate 					 * flagged as optional, fall through to
15197c478bd9Sstevel@tonic-gate 					 * open the original path.
15207c478bd9Sstevel@tonic-gate 					 */
152156deab07SRod Evans 					ret = _find_file(lml, clmp, flags, fdp,
152256deab07SRod Evans 					    rej, pdp, 1, in_nfavl);
15237c478bd9Sstevel@tonic-gate 					if (ret || ((fobj->co_flags &
15247c478bd9Sstevel@tonic-gate 					    RTC_OBJ_OPTINAL) == 0))
15257c478bd9Sstevel@tonic-gate 						return (ret);
15267c478bd9Sstevel@tonic-gate 
152756deab07SRod Evans 					fdp->fd_flags &= ~FLG_FD_ALTER;
15287c478bd9Sstevel@tonic-gate 				}
15297c478bd9Sstevel@tonic-gate 			}
15307c478bd9Sstevel@tonic-gate 		}
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	/*
15347c478bd9Sstevel@tonic-gate 	 * Protect ourselves from building an invalid pathname.
15357c478bd9Sstevel@tonic-gate 	 */
153656deab07SRod Evans 	if ((olen + pdp->pd_plen + 1) >= PATH_MAX) {
153756deab07SRod Evans 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), oname,
15387c478bd9Sstevel@tonic-gate 		    strerror(ENAMETOOLONG));
15397c478bd9Sstevel@tonic-gate 			return (0);
15407c478bd9Sstevel@tonic-gate 	}
154156deab07SRod Evans 	if ((fdp->fd_nname = (LM_GET_SO(clmp)(pdp->pd_pname, oname,
154256deab07SRod Evans 	    pdp->pd_plen, olen))) == NULL)
15437c478bd9Sstevel@tonic-gate 		return (0);
15447c478bd9Sstevel@tonic-gate 
154556deab07SRod Evans 	return (_find_file(lml, clmp, flags, fdp, rej, pdp, 0, in_nfavl));
154656deab07SRod Evans }
154756deab07SRod Evans 
154856deab07SRod Evans static Fct	*Vector[] = {
154956deab07SRod Evans 	&elf_fct,
155056deab07SRod Evans #ifdef	A_OUT
155156deab07SRod Evans 	&aout_fct,
155256deab07SRod Evans #endif
155356deab07SRod Evans 	0
155456deab07SRod Evans };
155556deab07SRod Evans 
155656deab07SRod Evans /*
155756deab07SRod Evans  * Remap the first page of a file to provide a better diagnostic as to why
155856deab07SRod Evans  * an mmapobj(2) operation on this file failed.  Sadly, mmapobj(), and all
155956deab07SRod Evans  * system calls for that matter, only pass back a generic failure in errno.
156056deab07SRod Evans  * Hopefully one day this will be improved, but in the mean time we repeat
156156deab07SRod Evans  * the kernels ELF verification to try and provide more detailed information.
156256deab07SRod Evans  */
156356deab07SRod Evans static int
156456deab07SRod Evans map_fail(Fdesc *fdp, size_t fsize, const char *name, int fd, Rej_desc *rej)
156556deab07SRod Evans {
156656deab07SRod Evans 	caddr_t	addr;
156756deab07SRod Evans 	int	vnum;
156856deab07SRod Evans 	size_t	size;
156956deab07SRod Evans 
157056deab07SRod Evans 	/*
157156deab07SRod Evans 	 * Use the original file size to determine what to map, and catch the
157256deab07SRod Evans 	 * obvious error of a zero sized file.
157356deab07SRod Evans 	 */
157456deab07SRod Evans 	if (fsize == 0) {
157556deab07SRod Evans 		rej->rej_type = SGS_REJ_UNKFILE;
157656deab07SRod Evans 		return (1);
157756deab07SRod Evans 	} else if (fsize < syspagsz)
157856deab07SRod Evans 		size = fsize;
157956deab07SRod Evans 	else
158056deab07SRod Evans 		size = syspagsz;
158156deab07SRod Evans 
158256deab07SRod Evans 	if ((addr = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
158356deab07SRod Evans 		return (0);
158456deab07SRod Evans 
158556deab07SRod Evans 	rej->rej_type = 0;
158656deab07SRod Evans 
158756deab07SRod Evans 	/*
158856deab07SRod Evans 	 * Validate the file against each supported file type.  Should a
158956deab07SRod Evans 	 * characteristic of the file be found invalid for this platform, a
159056deab07SRod Evans 	 * rejection message will have been recorded.
159156deab07SRod Evans 	 */
159256deab07SRod Evans 	for (vnum = 0; Vector[vnum]; vnum++) {
159356deab07SRod Evans 		if (((Vector[vnum]->fct_verify_file)(addr, size,
159456deab07SRod Evans 		    fdp, name, rej) == 0) && rej->rej_type)
159556deab07SRod Evans 			break;
159656deab07SRod Evans 	}
159756deab07SRod Evans 
159856deab07SRod Evans 	/*
159956deab07SRod Evans 	 * If no rejection message has been recorded, then this is simply an
160056deab07SRod Evans 	 * unknown file type.
160156deab07SRod Evans 	 */
160256deab07SRod Evans 	if (rej->rej_type == 0)
160356deab07SRod Evans 		rej->rej_type = SGS_REJ_UNKFILE;
160456deab07SRod Evans 
160556deab07SRod Evans 	(void) munmap(addr, size);
160656deab07SRod Evans 	return (1);
160756deab07SRod Evans }
160856deab07SRod Evans 
160956deab07SRod Evans /*
161056deab07SRod Evans  * Unmap a file.
161156deab07SRod Evans  */
161256deab07SRod Evans void
161356deab07SRod Evans unmap_obj(mmapobj_result_t *mpp, uint_t mapnum)
161456deab07SRod Evans {
161556deab07SRod Evans 	uint_t	num;
161656deab07SRod Evans 
161756deab07SRod Evans 	for (num = 0; num < mapnum; num++) {
161856deab07SRod Evans 		/* LINTED */
161956deab07SRod Evans 		(void) munmap((void *)(uintptr_t)mpp[num].mr_addr,
162056deab07SRod Evans 		    mpp[num].mr_msize);
162156deab07SRod Evans 	}
162256deab07SRod Evans }
162356deab07SRod Evans 
162456deab07SRod Evans /*
162556deab07SRod Evans  * Map a file.
162656deab07SRod Evans  */
162756deab07SRod Evans Fct *
162856deab07SRod Evans map_obj(Lm_list *lml, Fdesc *fdp, size_t fsize, const char *name, int fd,
162956deab07SRod Evans     Rej_desc *rej)
163056deab07SRod Evans {
163156deab07SRod Evans 	static mmapobj_result_t	*smpp = NULL;
163256deab07SRod Evans 	static uint_t		smapnum;
163356deab07SRod Evans 	mmapobj_result_t	*mpp;
163456deab07SRod Evans 	uint_t			mnum, mapnum, mflags;
163556deab07SRod Evans 	void			*padding;
163656deab07SRod Evans 
163756deab07SRod Evans 	/*
163856deab07SRod Evans 	 * Allocate an initial mapping array.  The initial size should be large
163956deab07SRod Evans 	 * enough to handle the normal ELF objects we come across.
164056deab07SRod Evans 	 */
164156deab07SRod Evans 	if (smpp == NULL) {
164256deab07SRod Evans 		smpp = malloc(sizeof (mmapobj_result_t) * MMAPFD_NUM);
164356deab07SRod Evans 		if (smpp == NULL)
164456deab07SRod Evans 			return (NULL);
164556deab07SRod Evans 		smapnum = MMAPFD_NUM;
164656deab07SRod Evans 	}
164756deab07SRod Evans 
164856deab07SRod Evans 	/*
164956deab07SRod Evans 	 * If object padding is required, set the necessary flags.
165056deab07SRod Evans 	 */
165156deab07SRod Evans 	if (r_debug.rtd_objpad) {
165256deab07SRod Evans 		mflags = MMOBJ_INTERPRET | MMOBJ_PADDING;
165356deab07SRod Evans 		padding = &r_debug.rtd_objpad;
165456deab07SRod Evans 	} else {
165556deab07SRod Evans 		mflags = MMOBJ_INTERPRET;
165656deab07SRod Evans 		padding = NULL;
165756deab07SRod Evans 	}
165856deab07SRod Evans 
165956deab07SRod Evans 	/*
166056deab07SRod Evans 	 * Map the file.  If the number of mappings required by this file
166156deab07SRod Evans 	 * exceeds the present mapping structure, an error indicating the
166256deab07SRod Evans 	 * return data is too big is returned.  Bail on any other error.
166356deab07SRod Evans 	 */
166456deab07SRod Evans 	mapnum = smapnum;
166556deab07SRod Evans 	if (mmapobj(fd, mflags, smpp, &mapnum, padding) == -1) {
166656deab07SRod Evans 		if (errno != E2BIG) {
166756deab07SRod Evans 			int	err = errno;
166856deab07SRod Evans 
166956deab07SRod Evans 			/*
167056deab07SRod Evans 			 * An unsupported error indicates that there's something
167156deab07SRod Evans 			 * incompatible with this ELF file, and the process that
167256deab07SRod Evans 			 * is already running.  Map the first page of the file
167356deab07SRod Evans 			 * and see if we can generate a better error message.
167456deab07SRod Evans 			 */
167556deab07SRod Evans 			if ((errno == ENOTSUP) && map_fail(fdp, fsize, name,
167656deab07SRod Evans 			    fd, rej))
167756deab07SRod Evans 				return (NULL);
167856deab07SRod Evans 
167956deab07SRod Evans 			rej->rej_type = SGS_REJ_STR;
168056deab07SRod Evans 			rej->rej_str = strerror(err);
168156deab07SRod Evans 			return (NULL);
168256deab07SRod Evans 		}
168356deab07SRod Evans 
168456deab07SRod Evans 		/*
168556deab07SRod Evans 		 * The mapping requirement exceeds the present mapping
168656deab07SRod Evans 		 * structure, however the number of mapping required is
168756deab07SRod Evans 		 * available in the mapping number.
168856deab07SRod Evans 		 */
168956deab07SRod Evans 		free((void *)smpp);
169056deab07SRod Evans 		if ((smpp = malloc(sizeof (mmapobj_result_t) * mapnum)) == NULL)
169156deab07SRod Evans 			return (NULL);
169256deab07SRod Evans 		smapnum = mapnum;
169356deab07SRod Evans 
169456deab07SRod Evans 		/*
169556deab07SRod Evans 		 * With the appropriate mapping structure, try the mapping
169656deab07SRod Evans 		 * request again.
169756deab07SRod Evans 		 */
169856deab07SRod Evans 		if (mmapobj(fd, mflags, smpp, &mapnum, padding) == -1) {
169956deab07SRod Evans 			rej->rej_type = SGS_REJ_STR;
170056deab07SRod Evans 			rej->rej_str = strerror(errno);
170156deab07SRod Evans 			return (NULL);
170256deab07SRod Evans 		}
170356deab07SRod Evans 	}
170456deab07SRod Evans 	ASSERT(mapnum != 0);
170556deab07SRod Evans 
170656deab07SRod Evans 	/*
170756deab07SRod Evans 	 * Traverse the mappings in search of a file type ld.so.1 can process.
170856deab07SRod Evans 	 * If the file type is verified as one ld.so.1 can process, retain the
170956deab07SRod Evans 	 * mapping information, and the number of mappings this object uses,
171056deab07SRod Evans 	 * and clear the static mapping pointer for the next map_obj() use of
171156deab07SRod Evans 	 * mmapobj().
171256deab07SRod Evans 	 */
171356deab07SRod Evans 	DBG_CALL(Dbg_file_mmapobj(lml, name, smpp, mapnum));
171456deab07SRod Evans 
171556deab07SRod Evans 	for (mnum = 0, mpp = smpp; mnum < mapnum; mnum++, mpp++) {
171656deab07SRod Evans 		uint_t	flags = (mpp->mr_flags & MR_TYPE_MASK);
171756deab07SRod Evans 		Fct	*fptr = NULL;
171856deab07SRod Evans 
171956deab07SRod Evans 		if (flags == MR_HDR_ELF) {
172056deab07SRod Evans 			fptr = elf_verify((mpp->mr_addr + mpp->mr_offset),
172156deab07SRod Evans 			    mpp->mr_fsize, fdp, name, rej);
172256deab07SRod Evans 		}
172356deab07SRod Evans #ifdef	A_OUT
172456deab07SRod Evans 		if (flags == MR_HDR_AOUT) {
172556deab07SRod Evans 			fptr = aout_verify((mpp->mr_addr + mpp->mr_offset),
172656deab07SRod Evans 			    mpp->mr_fsize, fdp, name, rej);
172756deab07SRod Evans 		}
172856deab07SRod Evans #endif
172956deab07SRod Evans 		if (fptr) {
173056deab07SRod Evans 			fdp->fd_mapn = mapnum;
173156deab07SRod Evans 			fdp->fd_mapp = smpp;
173256deab07SRod Evans 
173356deab07SRod Evans 			smpp = NULL;
173456deab07SRod Evans 
173556deab07SRod Evans 			return (fptr);
173656deab07SRod Evans 		}
173756deab07SRod Evans 	}
173856deab07SRod Evans 
173956deab07SRod Evans 	/*
174056deab07SRod Evans 	 * If the mapped file is inappropriate, indicate that the file type is
174156deab07SRod Evans 	 * unknown, and free the mapping.
174256deab07SRod Evans 	 */
174356deab07SRod Evans 	if (rej->rej_type == 0)
174456deab07SRod Evans 		rej->rej_type = SGS_REJ_UNKFILE;
174556deab07SRod Evans 	unmap_obj(smpp, mapnum);
174656deab07SRod Evans 
174756deab07SRod Evans 	return (NULL);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /*
17517c478bd9Sstevel@tonic-gate  * A unique file has been opened.  Create a link-map to represent it, and
17527c478bd9Sstevel@tonic-gate  * process the various names by which it can be referenced.
17537c478bd9Sstevel@tonic-gate  */
175456deab07SRod Evans Rt_map *
175556deab07SRod Evans load_file(Lm_list *lml, Aliste lmco, Fdesc *fdp, int *in_nfavl)
17567c478bd9Sstevel@tonic-gate {
175756deab07SRod Evans 	mmapobj_result_t	*fpmpp = NULL, *fmpp = NULL, *lpmpp, *lmpp;
175856deab07SRod Evans 	mmapobj_result_t	*hmpp, *mpp, *ompp = fdp->fd_mapp;
175956deab07SRod Evans 	uint_t			mnum, omapnum = fdp->fd_mapn;
176056deab07SRod Evans 	const char		*nname = fdp->fd_nname;
17617c478bd9Sstevel@tonic-gate 	Rt_map			*nlmp;
176256deab07SRod Evans 	Ehdr			*ehdr = NULL;
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	/*
176556deab07SRod Evans 	 * Traverse the mappings for the input file to capture generic mapping
176656deab07SRod Evans 	 * information, and create a link-map to represent the file.
17677c478bd9Sstevel@tonic-gate 	 */
176856deab07SRod Evans 	for (mnum = 0, mpp = ompp; mnum < omapnum; mnum++, mpp++) {
176956deab07SRod Evans 		uint_t	flags = (mpp->mr_flags & MR_TYPE_MASK);
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 		/*
177256deab07SRod Evans 		 * Keep track of the first and last mappings that may include
177356deab07SRod Evans 		 * padding.
17747c478bd9Sstevel@tonic-gate 		 */
177556deab07SRod Evans 		if (fpmpp == NULL)
177656deab07SRod Evans 			fpmpp = mpp;
177756deab07SRod Evans 		lpmpp = mpp;
17787c478bd9Sstevel@tonic-gate 
177956deab07SRod Evans 		/*
178056deab07SRod Evans 		 * Keep track of the first and last mappings that do not include
178156deab07SRod Evans 		 * padding.
178256deab07SRod Evans 		 */
178356deab07SRod Evans 		if (flags != MR_PADDING) {
178456deab07SRod Evans 			if (fmpp == NULL)
178556deab07SRod Evans 				fmpp = mpp;
178656deab07SRod Evans 			lmpp = mpp;
178756deab07SRod Evans 		}
178856deab07SRod Evans 		if (flags == MR_HDR_ELF) {
178956deab07SRod Evans 			/* LINTED */
179056deab07SRod Evans 			ehdr = (Ehdr *)(mpp->mr_addr + mpp->mr_offset);
179156deab07SRod Evans 			hmpp = mpp;
179256deab07SRod Evans 		} else if (flags == MR_HDR_AOUT)
179356deab07SRod Evans 			hmpp = mpp;
179456deab07SRod Evans 	}
179556deab07SRod Evans 
179656deab07SRod Evans 	/*
179756deab07SRod Evans 	 * The only ELF files we can handle are ET_EXEC, ET_DYN, and ET_REL.
179856deab07SRod Evans 	 *
179956deab07SRod Evans 	 * ET_REL must be processed by ld(1) to create an in-memory ET_DYN.
180056deab07SRod Evans 	 * The initial processing carried out by elf_obj_file() creates a
180156deab07SRod Evans 	 * temporary link-map, that acts as a place holder, until the objects
180256deab07SRod Evans 	 * processing is finished with elf_obj_fini().
180356deab07SRod Evans 	 */
180456deab07SRod Evans 	if (ehdr && (ehdr->e_type == ET_REL)) {
180556deab07SRod Evans 		if ((nlmp = elf_obj_file(lml, lmco, nname, hmpp, ompp,
180656deab07SRod Evans 		    omapnum)) == NULL)
180756deab07SRod Evans 			return (nlmp);
180856deab07SRod Evans 	} else {
180956deab07SRod Evans 		Addr	addr;
181056deab07SRod Evans 		size_t	msize;
181156deab07SRod Evans 
181256deab07SRod Evans 		/*
181356deab07SRod Evans 		 * The size of the total reservation, and the padding range,
181456deab07SRod Evans 		 * are a historic artifact required by debuggers.  Although
181556deab07SRod Evans 		 * these values express the range of the associated mappings,
181656deab07SRod Evans 		 * there can be holes between segments (in which small objects
181756deab07SRod Evans 		 * could be mapped).  Anyone who needs to verify offsets
181856deab07SRod Evans 		 * against segments should analyze all the object mappings,
181956deab07SRod Evans 		 * rather than relying on these address ranges.
182056deab07SRod Evans 		 */
182156deab07SRod Evans 		addr = (Addr)(hmpp->mr_addr + hmpp->mr_offset);
182256deab07SRod Evans 		msize = lmpp->mr_addr + lmpp->mr_msize - fmpp->mr_addr;
182356deab07SRod Evans 
182456deab07SRod Evans 		if ((nlmp = ((fdp->fd_ftp)->fct_new_lmp)(lml, lmco, fdp, addr,
182556deab07SRod Evans 		    msize, NULL, in_nfavl)) == NULL)
182637ffaf83SRod Evans 			return (NULL);
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 		/*
182956deab07SRod Evans 		 * Save generic mapping information.
18307c478bd9Sstevel@tonic-gate 		 */
183156deab07SRod Evans 		MMAPS(nlmp) = ompp;
183256deab07SRod Evans 		MMAPCNT(nlmp) = omapnum;
183356deab07SRod Evans 		PADSTART(nlmp) = (ulong_t)fpmpp->mr_addr;
183456deab07SRod Evans 		PADIMLEN(nlmp) = lpmpp->mr_addr + lpmpp->mr_msize -
183556deab07SRod Evans 		    fpmpp->mr_addr;
183656deab07SRod Evans 	}
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	/*
183956deab07SRod Evans 	 * Save the dev/inode information for later comparisons, and identify
184056deab07SRod Evans 	 * this as a new object.
18417c478bd9Sstevel@tonic-gate 	 */
184256deab07SRod Evans 	STDEV(nlmp) = fdp->fd_dev;
184356deab07SRod Evans 	STINO(nlmp) = fdp->fd_ino;
184456deab07SRod Evans 	FLAGS(nlmp) |= FLG_RT_NEWLOAD;
18457c478bd9Sstevel@tonic-gate 
184656deab07SRod Evans 	/*
184756deab07SRod Evans 	 * If this is ELF relocatable object, we're done for now.
184856deab07SRod Evans 	 */
184956deab07SRod Evans 	if (ehdr && (ehdr->e_type == ET_REL))
185056deab07SRod Evans 		return (nlmp);
185156deab07SRod Evans 
185256deab07SRod Evans 	/*
185356deab07SRod Evans 	 * Insert the names of this link-map into the FullPathNode AVL tree.
185456deab07SRod Evans 	 * Save both the NAME() and PATHNAME() if the names differ.
185556deab07SRod Evans 	 */
185656deab07SRod Evans 	(void) fullpath(nlmp, fdp);
185756deab07SRod Evans 
185856deab07SRod Evans 	if ((NAME(nlmp)[0] == '/') && (fpavl_insert(lml, nlmp, NAME(nlmp),
185956deab07SRod Evans 	    fdp->fd_avlwhere) == 0)) {
18607c478bd9Sstevel@tonic-gate 		remove_so(lml, nlmp);
186137ffaf83SRod Evans 		return (NULL);
18627c478bd9Sstevel@tonic-gate 	}
186356deab07SRod Evans 	if (((NAME(nlmp)[0] != '/') || (NAME(nlmp) != PATHNAME(nlmp))) &&
18647c478bd9Sstevel@tonic-gate 	    (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)) {
18657c478bd9Sstevel@tonic-gate 		remove_so(lml, nlmp);
186637ffaf83SRod Evans 		return (NULL);
18677c478bd9Sstevel@tonic-gate 	}
18683dbfc803SRod Evans 
18693dbfc803SRod Evans 	/*
187056deab07SRod Evans 	 * If this is a secure application, record any full path name directory
187156deab07SRod Evans 	 * in which this dependency has been found.  This directory can be
187256deab07SRod Evans 	 * deemed safe (as we've already found a dependency here).  This
187356deab07SRod Evans 	 * recording provides a fall-back should another objects $ORIGIN
187456deab07SRod Evans 	 * definition expands to this directory, an expansion that would
187556deab07SRod Evans 	 * ordinarily be deemed insecure.
18763dbfc803SRod Evans 	 */
18773dbfc803SRod Evans 	if (rtld_flags & RT_FL_SECURE) {
18783dbfc803SRod Evans 		if (NAME(nlmp)[0] == '/')
18793dbfc803SRod Evans 			spavl_insert(NAME(nlmp));
18803dbfc803SRod Evans 		if ((NAME(nlmp) != PATHNAME(nlmp)) &&
18813dbfc803SRod Evans 		    (PATHNAME(nlmp)[0] == '/'))
18823dbfc803SRod Evans 			spavl_insert(PATHNAME(nlmp));
18833dbfc803SRod Evans 	}
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	/*
18867c478bd9Sstevel@tonic-gate 	 * If we're processing an alternative object reset the original name
18877c478bd9Sstevel@tonic-gate 	 * for possible $ORIGIN processing.
18887c478bd9Sstevel@tonic-gate 	 */
188956deab07SRod Evans 	if (fdp->fd_flags & FLG_FD_ALTER) {
189056deab07SRod Evans 		const char	*odir, *ndir;
18917c478bd9Sstevel@tonic-gate 		size_t		olen;
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) |= FLG_RT_ALTER;
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 		/*
18967c478bd9Sstevel@tonic-gate 		 * If we were given a pathname containing a slash then the
18977c478bd9Sstevel@tonic-gate 		 * original name is still in oname.  Otherwise the original
18987c478bd9Sstevel@tonic-gate 		 * directory is in dir->p_name (which is all we need for
18997c478bd9Sstevel@tonic-gate 		 * $ORIGIN).
19007c478bd9Sstevel@tonic-gate 		 */
190156deab07SRod Evans 		if (fdp->fd_flags & FLG_FD_SLASH) {
19027c478bd9Sstevel@tonic-gate 			char	*ofil;
19037c478bd9Sstevel@tonic-gate 
190456deab07SRod Evans 			odir = fdp->fd_oname;
190556deab07SRod Evans 			ofil = strrchr(fdp->fd_oname, '/');
19067c478bd9Sstevel@tonic-gate 			olen = ofil - odir + 1;
19077c478bd9Sstevel@tonic-gate 		} else {
190856deab07SRod Evans 			odir = fdp->fd_odir;
19097c478bd9Sstevel@tonic-gate 			olen = strlen(odir) + 1;
19107c478bd9Sstevel@tonic-gate 		}
191156deab07SRod Evans 		if ((ndir = stravl_insert(odir, 0, olen, 1)) == NULL) {
19127c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
191337ffaf83SRod Evans 			return (NULL);
19147c478bd9Sstevel@tonic-gate 		}
19157c478bd9Sstevel@tonic-gate 		ORIGNAME(nlmp) = ndir;
191656deab07SRod Evans 		DIRSZ(nlmp) = --olen;
19177c478bd9Sstevel@tonic-gate 	}
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 	return (nlmp);
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate /*
19237c478bd9Sstevel@tonic-gate  * This function loads the named file and returns a pointer to its link map.
19247c478bd9Sstevel@tonic-gate  * It is assumed that the caller has already checked that the file is not
19257c478bd9Sstevel@tonic-gate  * already loaded before calling this function (refer is_so_loaded()).
19267c478bd9Sstevel@tonic-gate  * Find and open the file, map it into memory, add it to the end of the list
19277c478bd9Sstevel@tonic-gate  * of link maps and return a pointer to the new link map.  Return 0 on error.
19287c478bd9Sstevel@tonic-gate  */
19297c478bd9Sstevel@tonic-gate static Rt_map *
193056deab07SRod Evans load_so(Lm_list *lml, Aliste lmco, Rt_map *clmp, uint_t flags,
193156deab07SRod Evans     Fdesc *fdp, Rej_desc *rej, int *in_nfavl)
19327c478bd9Sstevel@tonic-gate {
193356deab07SRod Evans 	const char	*oname = fdp->fd_oname;
193456deab07SRod Evans 	Pdesc		*pdp;
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	/*
193756deab07SRod Evans 	 * If this path name hasn't already been identified as containing a
193856deab07SRod Evans 	 * slash, check the path name.  Most paths have been constructed
193956deab07SRod Evans 	 * through appending a file name to a search path, and/or have been
194056deab07SRod Evans 	 * inspected by expand(), and thus have a slash.  However, we can
194156deab07SRod Evans 	 * receive path names via auditors or configuration files, and thus
194256deab07SRod Evans 	 * an evaluation here catches these instances.
19437c478bd9Sstevel@tonic-gate 	 */
194456deab07SRod Evans 	if ((fdp->fd_flags & FLG_FD_SLASH) == 0) {
19457c478bd9Sstevel@tonic-gate 		const char	*str;
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 		for (str = oname; *str; str++) {
19487c478bd9Sstevel@tonic-gate 			if (*str == '/') {
194956deab07SRod Evans 				fdp->fd_flags |= FLG_FD_SLASH;
19507c478bd9Sstevel@tonic-gate 				break;
19517c478bd9Sstevel@tonic-gate 			}
19527c478bd9Sstevel@tonic-gate 		}
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	/*
19567c478bd9Sstevel@tonic-gate 	 * If we are passed a 'null' link-map this means that this is the first
19577c478bd9Sstevel@tonic-gate 	 * object to be loaded on this link-map list.  In that case we set the
19587c478bd9Sstevel@tonic-gate 	 * link-map to ld.so.1's link-map.
19597c478bd9Sstevel@tonic-gate 	 *
19607c478bd9Sstevel@tonic-gate 	 * This link-map is referenced to determine what lookup rules to use
19617c478bd9Sstevel@tonic-gate 	 * when searching for files.  By using ld.so.1's we are defaulting to
19627c478bd9Sstevel@tonic-gate 	 * ELF look-up rules.
19637c478bd9Sstevel@tonic-gate 	 *
19647c478bd9Sstevel@tonic-gate 	 * Note: This case happens when loading the first object onto
19657c478bd9Sstevel@tonic-gate 	 *	 the plt_tracing link-map.
19667c478bd9Sstevel@tonic-gate 	 */
19677c478bd9Sstevel@tonic-gate 	if (clmp == 0)
19687c478bd9Sstevel@tonic-gate 		clmp = lml_rtld.lm_head;
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 	/*
19717c478bd9Sstevel@tonic-gate 	 * If this path resulted from a $HWCAP specification, then the best
19727c478bd9Sstevel@tonic-gate 	 * hardware capability object has already been establish, and is
19737c478bd9Sstevel@tonic-gate 	 * available in the calling file descriptor.  Perform some minor book-
19747c478bd9Sstevel@tonic-gate 	 * keeping so that we can fall through into common code.
19757c478bd9Sstevel@tonic-gate 	 */
19767c478bd9Sstevel@tonic-gate 	if (flags & FLG_RT_HWCAP) {
19777c478bd9Sstevel@tonic-gate 		/*
1978fb1354edSrie 		 * If this object is already loaded, we're done.
19797c478bd9Sstevel@tonic-gate 		 */
198056deab07SRod Evans 		if (fdp->fd_lmp)
198156deab07SRod Evans 			return (fdp->fd_lmp);
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 		/*
19847c478bd9Sstevel@tonic-gate 		 * Obtain the avl index for this object.
19857c478bd9Sstevel@tonic-gate 		 */
198656deab07SRod Evans 		(void) fpavl_recorded(lml, fdp->fd_nname, 0,
198756deab07SRod Evans 		    &(fdp->fd_avlwhere));
1988bbf522bdSrie 
198956deab07SRod Evans 	} else if (fdp->fd_flags & FLG_FD_SLASH) {
19907c478bd9Sstevel@tonic-gate 		Rej_desc	_rej = { 0 };
19917c478bd9Sstevel@tonic-gate 
199256deab07SRod Evans 		if (find_path(lml, clmp, flags, fdp, &_rej, in_nfavl) == 0) {
199331fdd7caSab196087 			rejection_inherit(rej, &_rej);
199437ffaf83SRod Evans 			return (NULL);
19957c478bd9Sstevel@tonic-gate 		}
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 		/*
19987c478bd9Sstevel@tonic-gate 		 * If this object is already loaded, we're done.
19997c478bd9Sstevel@tonic-gate 		 */
200056deab07SRod Evans 		if (fdp->fd_lmp)
200156deab07SRod Evans 			return (fdp->fd_lmp);
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	} else {
20047c478bd9Sstevel@tonic-gate 		/*
20057c478bd9Sstevel@tonic-gate 		 * No '/' - for each directory on list, make a pathname using
20067c478bd9Sstevel@tonic-gate 		 * that directory and filename and try to open that file.
20077c478bd9Sstevel@tonic-gate 		 */
200856deab07SRod Evans 		Spath_desc	sd = { search_rules, NULL, 0 };
20097c478bd9Sstevel@tonic-gate 		Word		strhash = 0;
201056deab07SRod Evans 		int		found = 0;
201156deab07SRod Evans 
20125aefb655Srie 		DBG_CALL(Dbg_libs_find(lml, oname));
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 		/*
201556deab07SRod Evans 		 * Traverse the search path lists, creating full pathnames and
201656deab07SRod Evans 		 * attempt to load each path.
20177c478bd9Sstevel@tonic-gate 		 */
201856deab07SRod Evans 		for (pdp = get_next_dir(&sd, clmp, flags); pdp;
201956deab07SRod Evans 		    pdp = get_next_dir(&sd, clmp, flags)) {
20207c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
202156deab07SRod Evans 			Fdesc		fd = { 0 };
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 			/*
20249aa23310Srie 			 * Under debugging, duplicate path name entries are
20259aa23310Srie 			 * tagged but remain part of the search path list so
20269aa23310Srie 			 * that they can be diagnosed under "unused" processing.
20279aa23310Srie 			 * Skip these entries, as this path would have already
20289aa23310Srie 			 * been attempted.
20299aa23310Srie 			 */
203056deab07SRod Evans 			if (pdp->pd_flags & PD_FLG_DUPLICAT)
20319aa23310Srie 				continue;
20329aa23310Srie 
203356deab07SRod Evans 			fd = *fdp;
203456deab07SRod Evans 
20359aa23310Srie 			/*
20367c478bd9Sstevel@tonic-gate 			 * Try and locate this file.  Make sure to clean up
20377c478bd9Sstevel@tonic-gate 			 * any rejection information should the file have
20387c478bd9Sstevel@tonic-gate 			 * been found, but not appropriate.
20397c478bd9Sstevel@tonic-gate 			 */
204056deab07SRod Evans 			if (find_file(lml, clmp, flags, &fd, &_rej, pdp,
204156deab07SRod Evans 			    &strhash, in_nfavl) == 0) {
204231fdd7caSab196087 				rejection_inherit(rej, &_rej);
20437c478bd9Sstevel@tonic-gate 				continue;
20447c478bd9Sstevel@tonic-gate 			}
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 			/*
20479aa23310Srie 			 * Indicate that this search path has been used.  If
20489aa23310Srie 			 * this is an LD_LIBRARY_PATH setting, ignore any use
20499aa23310Srie 			 * by ld.so.1 itself.
20509aa23310Srie 			 */
205156deab07SRod Evans 			if (((pdp->pd_flags & LA_SER_LIBPATH) == 0) ||
20529aa23310Srie 			    ((lml->lm_flags & LML_FLG_RTLDLM) == 0))
205356deab07SRod Evans 				pdp->pd_flags |= PD_FLG_USED;
20549aa23310Srie 
20559aa23310Srie 			/*
20567c478bd9Sstevel@tonic-gate 			 * If this object is already loaded, we're done.
20577c478bd9Sstevel@tonic-gate 			 */
205856deab07SRod Evans 			*fdp = fd;
205956deab07SRod Evans 			if (fdp->fd_lmp)
206056deab07SRod Evans 				return (fdp->fd_lmp);
20617c478bd9Sstevel@tonic-gate 
206256deab07SRod Evans 			fdp->fd_odir = pdp->pd_pname;
206356deab07SRod Evans 			found = 1;
20647c478bd9Sstevel@tonic-gate 			break;
20657c478bd9Sstevel@tonic-gate 		}
20667c478bd9Sstevel@tonic-gate 
20677c478bd9Sstevel@tonic-gate 		/*
20687c478bd9Sstevel@tonic-gate 		 * If the file couldn't be loaded, do another comparison of
20697c478bd9Sstevel@tonic-gate 		 * loaded files using just the basename.  This catches folks
20707c478bd9Sstevel@tonic-gate 		 * who may have loaded multiple full pathname files (possibly
20717c478bd9Sstevel@tonic-gate 		 * from setxid applications) to satisfy dependency relationships
20727c478bd9Sstevel@tonic-gate 		 * (i.e., a file might have a dependency on foo.so.1 which has
20737c478bd9Sstevel@tonic-gate 		 * already been opened using its full pathname).
20747c478bd9Sstevel@tonic-gate 		 */
207556deab07SRod Evans 		if (found == 0)
20769aa23310Srie 			return (is_so_loaded(lml, oname, in_nfavl));
20777c478bd9Sstevel@tonic-gate 	}
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 	/*
20807c478bd9Sstevel@tonic-gate 	 * Finish mapping the file and return the link-map descriptor.  Note,
20817c478bd9Sstevel@tonic-gate 	 * if this request originated from a HWCAP request, re-establish the
20827c478bd9Sstevel@tonic-gate 	 * fdesc information.  For single paged objects, such as filters, the
20837c478bd9Sstevel@tonic-gate 	 * original mapping may have been sufficient to capture the file, thus
20847c478bd9Sstevel@tonic-gate 	 * this mapping needs to be reset to insure it doesn't mistakenly get
20857c478bd9Sstevel@tonic-gate 	 * unmapped as part of HWCAP cleanup.
20867c478bd9Sstevel@tonic-gate 	 */
208756deab07SRod Evans 	return (load_file(lml, lmco, fdp, in_nfavl));
20887c478bd9Sstevel@tonic-gate }
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate /*
209156deab07SRod Evans  * Trace an attempt to load an object, and seed the originating name.
20927c478bd9Sstevel@tonic-gate  */
209356deab07SRod Evans const char *
209456deab07SRod Evans load_trace(Lm_list *lml, Pdesc *pdp, Rt_map *clmp, Fdesc *fdp)
20957c478bd9Sstevel@tonic-gate {
209656deab07SRod Evans 	const char	*name = pdp->pd_pname;
20977247f888Srie 
20987c478bd9Sstevel@tonic-gate 	/*
20997c478bd9Sstevel@tonic-gate 	 * First generate any ldd(1) diagnostics.
21007c478bd9Sstevel@tonic-gate 	 */
21017c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
21027c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
21037c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name, NAME(clmp));
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	/*
210656deab07SRod Evans 	 * Propagate any knowledge of a slash within the path name.
210756deab07SRod Evans 	 */
210856deab07SRod Evans 	if (pdp->pd_flags & PD_FLG_PNSLASH)
210956deab07SRod Evans 		fdp->fd_flags |= FLG_FD_SLASH;
211056deab07SRod Evans 
211156deab07SRod Evans 	/*
21127c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're
21137c478bd9Sstevel@tonic-gate 	 * about to go search for.
21147c478bd9Sstevel@tonic-gate 	 */
211556deab07SRod Evans 	if (((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_ACTIVITY) &&
21167c478bd9Sstevel@tonic-gate 	    (lml == LIST(clmp)))
21177c478bd9Sstevel@tonic-gate 		audit_activity(clmp, LA_ACT_ADD);
21187c478bd9Sstevel@tonic-gate 
211956deab07SRod Evans 	if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
212056deab07SRod Evans 		char	*aname;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 		/*
21237c478bd9Sstevel@tonic-gate 		 * The auditor can indicate that this object should be ignored.
21247c478bd9Sstevel@tonic-gate 		 */
212556deab07SRod Evans 		if ((aname =
212656deab07SRod Evans 		    audit_objsearch(clmp, name, LA_SER_ORIG)) == NULL) {
21277247f888Srie 			DBG_CALL(Dbg_audit_terminate(lml, name));
212856deab07SRod Evans 			return (NULL);
21297c478bd9Sstevel@tonic-gate 		}
21307c478bd9Sstevel@tonic-gate 
21317247f888Srie 		if (name != aname) {
213256deab07SRod Evans 			fdp->fd_flags &= ~FLG_FD_SLASH;
213356deab07SRod Evans 			name = aname;
21347c478bd9Sstevel@tonic-gate 		}
21357c478bd9Sstevel@tonic-gate 	}
213656deab07SRod Evans 	fdp->fd_oname = name;
213756deab07SRod Evans 	return (name);
21387c478bd9Sstevel@tonic-gate }
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate /*
21417c478bd9Sstevel@tonic-gate  * Having loaded an object and created a link-map to describe it, finish
21427c478bd9Sstevel@tonic-gate  * processing this stage, including verifying any versioning requirements,
21437c478bd9Sstevel@tonic-gate  * updating the objects mode, creating a handle if necessary, and adding this
21447c478bd9Sstevel@tonic-gate  * object to existing handles if required.
21457c478bd9Sstevel@tonic-gate  */
2146390b98b5Srie static int
21477c478bd9Sstevel@tonic-gate load_finish(Lm_list *lml, const char *name, Rt_map *clmp, int nmode,
21487c478bd9Sstevel@tonic-gate     uint_t flags, Grp_hdl **hdl, Rt_map *nlmp)
21497c478bd9Sstevel@tonic-gate {
2150cce0e03bSab196087 	Aliste		idx;
2151cce0e03bSab196087 	Grp_hdl		*ghp;
21527c478bd9Sstevel@tonic-gate 	int		promote;
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 	/*
21557c478bd9Sstevel@tonic-gate 	 * If this dependency is associated with a required version insure that
21567c478bd9Sstevel@tonic-gate 	 * the version is present in the loaded file.
21577c478bd9Sstevel@tonic-gate 	 */
215856deab07SRod Evans 	if (((rtld_flags & RT_FL_NOVERSION) == 0) && THIS_IS_ELF(clmp) &&
215956deab07SRod Evans 	    VERNEED(clmp) && (elf_verify_vers(name, clmp, nlmp) == 0))
21607c478bd9Sstevel@tonic-gate 		return (0);
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate 	/*
21637c478bd9Sstevel@tonic-gate 	 * If this object has indicated that it should be isolated as a group
21647c478bd9Sstevel@tonic-gate 	 * (DT_FLAGS_1 contains DF_1_GROUP - object was built with -B group),
21657c478bd9Sstevel@tonic-gate 	 * or if the callers direct bindings indicate it should be isolated as
21667c478bd9Sstevel@tonic-gate 	 * a group (DYNINFO flags contains FLG_DI_GROUP - dependency followed
21677c478bd9Sstevel@tonic-gate 	 * -zgroupperm), establish the appropriate mode.
21687c478bd9Sstevel@tonic-gate 	 *
21697c478bd9Sstevel@tonic-gate 	 * The intent of an object defining itself as a group is to isolate the
21707c478bd9Sstevel@tonic-gate 	 * relocation of the group within its own members, however, unless
21717c478bd9Sstevel@tonic-gate 	 * opened through dlopen(), in which case we assume dlsym() will be used
21727c478bd9Sstevel@tonic-gate 	 * to located symbols in the new object, we still need to associate it
21737c478bd9Sstevel@tonic-gate 	 * with the caller for it to be bound with.  This is equivalent to a
21747c478bd9Sstevel@tonic-gate 	 * dlopen(RTLD_GROUP) and dlsym() using the returned handle.
21757c478bd9Sstevel@tonic-gate 	 */
21767c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) | flags) & FLG_RT_SETGROUP) {
21777c478bd9Sstevel@tonic-gate 		nmode &= ~RTLD_WORLD;
21787c478bd9Sstevel@tonic-gate 		nmode |= RTLD_GROUP;
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate 		/*
21817c478bd9Sstevel@tonic-gate 		 * If the object wasn't explicitly dlopen()'ed associate it with
21827c478bd9Sstevel@tonic-gate 		 * the parent.
21837c478bd9Sstevel@tonic-gate 		 */
21849a411307Srie 		if ((flags & FLG_RT_HANDLE) == 0)
21857c478bd9Sstevel@tonic-gate 			nmode |= RTLD_PARENT;
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	/*
21897c478bd9Sstevel@tonic-gate 	 * Establish new mode and flags.
21907c478bd9Sstevel@tonic-gate 	 */
21917c478bd9Sstevel@tonic-gate 	promote = update_mode(nlmp, MODE(nlmp), nmode);
21927c478bd9Sstevel@tonic-gate 	FLAGS(nlmp) |= flags;
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	/*
21952926dd2eSrie 	 * If this is a global object, ensure the associated link-map list can
21962926dd2eSrie 	 * be rescanned for global, lazy dependencies.
21972926dd2eSrie 	 */
21982926dd2eSrie 	if (MODE(nlmp) & RTLD_GLOBAL)
21992926dd2eSrie 		LIST(nlmp)->lm_flags &= ~LML_FLG_NOPENDGLBLAZY;
22002926dd2eSrie 
22012926dd2eSrie 	/*
22027c478bd9Sstevel@tonic-gate 	 * If we've been asked to establish a handle create one for this object.
22037c478bd9Sstevel@tonic-gate 	 * Or, if this object has already been analyzed, but this reference
22047c478bd9Sstevel@tonic-gate 	 * requires that the mode of the object be promoted, also create a
22057c478bd9Sstevel@tonic-gate 	 * handle to propagate the new modes to all this objects dependencies.
22067c478bd9Sstevel@tonic-gate 	 */
22077c478bd9Sstevel@tonic-gate 	if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) || (promote &&
22087c478bd9Sstevel@tonic-gate 	    (FLAGS(nlmp) & FLG_RT_ANALYZED))) {
22098af2c5b9Srie 		uint_t	oflags, hflags = 0, cdflags;
22107c478bd9Sstevel@tonic-gate 
22118af2c5b9Srie 		/*
22128af2c5b9Srie 		 * Establish any flags for the handle (Grp_hdl).
22138af2c5b9Srie 		 *
22148af2c5b9Srie 		 *  .	Use of the RTLD_FIRST flag indicates that only the first
22158af2c5b9Srie 		 *	dependency on the handle (the new object) can be used
22168af2c5b9Srie 		 *	to satisfy dlsym() requests.
22178af2c5b9Srie 		 */
22187c478bd9Sstevel@tonic-gate 		if (nmode & RTLD_FIRST)
22198af2c5b9Srie 			hflags = GPH_FIRST;
22208af2c5b9Srie 
22218af2c5b9Srie 		/*
22228af2c5b9Srie 		 * Establish the flags for this callers dependency descriptor
22238af2c5b9Srie 		 * (Grp_desc).
22248af2c5b9Srie 		 *
22258af2c5b9Srie 		 *  .	The creation of a handle associated a descriptor for the
22268af2c5b9Srie 		 *	new object and descriptor for the parent (caller).
22278af2c5b9Srie 		 *	Typically, the handle is created for dlopen() or for
22288af2c5b9Srie 		 *	filtering.  A handle may also be created to promote
22298af2c5b9Srie 		 *	the callers modes (RTLD_NOW) to the new object.  In this
22308af2c5b9Srie 		 *	latter case, the handle/descriptor are torn down once
22318af2c5b9Srie 		 *	the mode propagation has occurred.
22328af2c5b9Srie 		 *
22338af2c5b9Srie 		 *  .	Use of the RTLD_PARENT flag indicates that the parent
22348af2c5b9Srie 		 *	can be relocated against.
22358af2c5b9Srie 		 */
22368af2c5b9Srie 		if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) == 0)
22378af2c5b9Srie 			cdflags = GPD_PROMOTE;
22388af2c5b9Srie 		else
22398af2c5b9Srie 			cdflags = GPD_PARENT;
22408af2c5b9Srie 		if (nmode & RTLD_PARENT)
22418af2c5b9Srie 			cdflags |= GPD_RELOC;
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 		/*
22447c478bd9Sstevel@tonic-gate 		 * Now that a handle is being created, remove this state from
22457c478bd9Sstevel@tonic-gate 		 * the object so that it doesn't mistakenly get inherited by
22467c478bd9Sstevel@tonic-gate 		 * a dependency.
22477c478bd9Sstevel@tonic-gate 		 */
22487c478bd9Sstevel@tonic-gate 		oflags = FLAGS(nlmp);
22497c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) &= ~FLG_RT_HANDLE;
22507c478bd9Sstevel@tonic-gate 
22518af2c5b9Srie 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
22528af2c5b9Srie 		if ((ghp = hdl_create(lml, nlmp, clmp, hflags,
22538af2c5b9Srie 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), cdflags)) == 0)
22547c478bd9Sstevel@tonic-gate 			return (0);
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 		/*
22577c478bd9Sstevel@tonic-gate 		 * Add any dependencies that are already loaded, to the handle.
22587c478bd9Sstevel@tonic-gate 		 */
225902ca3e02Srie 		if (hdl_initialize(ghp, nlmp, nmode, promote) == 0)
22607c478bd9Sstevel@tonic-gate 			return (0);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 		if (hdl)
22637c478bd9Sstevel@tonic-gate 			*hdl = ghp;
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 		/*
22668af2c5b9Srie 		 * If we were asked to create a handle, we're done.
22677c478bd9Sstevel@tonic-gate 		 */
22687c478bd9Sstevel@tonic-gate 		if ((oflags | flags) & FLG_RT_HANDLE)
22697c478bd9Sstevel@tonic-gate 			return (1);
22707c478bd9Sstevel@tonic-gate 
22718af2c5b9Srie 		/*
22728af2c5b9Srie 		 * If the handle was created to promote modes from the parent
22738af2c5b9Srie 		 * (caller) to the new object, then this relationship needs to
22748af2c5b9Srie 		 * be removed to ensure the handle doesn't prevent the new
22758af2c5b9Srie 		 * objects from being deleted if required.  If the parent is
22768af2c5b9Srie 		 * the only dependency on the handle, then the handle can be
22778af2c5b9Srie 		 * completely removed.  However, the handle may have already
22788af2c5b9Srie 		 * existed, in which case only the parent descriptor can be
22798af2c5b9Srie 		 * deleted from the handle, or at least the GPD_PROMOTE flag
22808af2c5b9Srie 		 * removed from the descriptor.
22818af2c5b9Srie 		 *
22828af2c5b9Srie 		 * Fall through to carry out any group processing.
22838af2c5b9Srie 		 */
22848af2c5b9Srie 		free_hdl(ghp, clmp, GPD_PROMOTE);
22857c478bd9Sstevel@tonic-gate 	}
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	/*
22887c478bd9Sstevel@tonic-gate 	 * If the caller isn't part of a group we're done.
22897c478bd9Sstevel@tonic-gate 	 */
2290cce0e03bSab196087 	if (GROUPS(clmp) == NULL)
22917c478bd9Sstevel@tonic-gate 		return (1);
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 	/*
22947c478bd9Sstevel@tonic-gate 	 * Determine if our caller is already associated with a handle, if so
22957c478bd9Sstevel@tonic-gate 	 * we need to add this object to any handles that already exist.
22967c478bd9Sstevel@tonic-gate 	 * Traverse the list of groups our caller is a member of and add this
22977c478bd9Sstevel@tonic-gate 	 * new link-map to those groups.
22987c478bd9Sstevel@tonic-gate 	 */
22998af2c5b9Srie 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
2300cce0e03bSab196087 	for (APLIST_TRAVERSE(GROUPS(clmp), idx, ghp)) {
2301cce0e03bSab196087 		Aliste		idx1;
23027c478bd9Sstevel@tonic-gate 		Grp_desc	*gdp;
23037c478bd9Sstevel@tonic-gate 		int		exist;
2304cce0e03bSab196087 		Rt_map		*dlmp1;
2305cce0e03bSab196087 		APlist		*lmalp = NULL;
23067c478bd9Sstevel@tonic-gate 
23077c478bd9Sstevel@tonic-gate 		/*
23087c478bd9Sstevel@tonic-gate 		 * If the caller doesn't indicate that its dependencies should
23097c478bd9Sstevel@tonic-gate 		 * be added to a handle, ignore it.  This case identifies a
23107c478bd9Sstevel@tonic-gate 		 * parent of a dlopen(RTLD_PARENT) request.
23117c478bd9Sstevel@tonic-gate 		 */
2312cce0e03bSab196087 		for (ALIST_TRAVERSE(ghp->gh_depends, idx1, gdp)) {
23137c478bd9Sstevel@tonic-gate 			if (gdp->gd_depend == clmp)
23147c478bd9Sstevel@tonic-gate 				break;
23157c478bd9Sstevel@tonic-gate 		}
23167c478bd9Sstevel@tonic-gate 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
23177c478bd9Sstevel@tonic-gate 			continue;
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 		if ((exist = hdl_add(ghp, nlmp,
2320efb9e8b8Srie 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0)
23217c478bd9Sstevel@tonic-gate 			return (0);
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 		/*
23247c478bd9Sstevel@tonic-gate 		 * If this member already exists then its dependencies will
23257c478bd9Sstevel@tonic-gate 		 * have already been processed.
23267c478bd9Sstevel@tonic-gate 		 */
23277c478bd9Sstevel@tonic-gate 		if (exist == ALE_EXISTS)
23287c478bd9Sstevel@tonic-gate 			continue;
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 		/*
23317c478bd9Sstevel@tonic-gate 		 * If the object we've added has just been opened, it will not
23327c478bd9Sstevel@tonic-gate 		 * yet have been processed for its dependencies, these will be
23337c478bd9Sstevel@tonic-gate 		 * added on later calls to load_one().  If it doesn't have any
23347c478bd9Sstevel@tonic-gate 		 * dependencies we're also done.
23357c478bd9Sstevel@tonic-gate 		 */
23367c478bd9Sstevel@tonic-gate 		if (((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0) ||
2337cce0e03bSab196087 		    (DEPENDS(nlmp) == NULL))
23387c478bd9Sstevel@tonic-gate 			continue;
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 		/*
23417c478bd9Sstevel@tonic-gate 		 * Otherwise, this object exists and has dependencies, so add
23427c478bd9Sstevel@tonic-gate 		 * all of its dependencies to the handle were operating on.
23437c478bd9Sstevel@tonic-gate 		 */
234456deab07SRod Evans 		if (aplist_append(&lmalp, nlmp, AL_CNT_DEPCLCT) == NULL)
23457c478bd9Sstevel@tonic-gate 			return (0);
23467c478bd9Sstevel@tonic-gate 
2347cce0e03bSab196087 		for (APLIST_TRAVERSE(lmalp, idx1, dlmp1)) {
2348cce0e03bSab196087 			Aliste		idx2;
2349cce0e03bSab196087 			Bnd_desc 	*bdp;
23507c478bd9Sstevel@tonic-gate 
23517c478bd9Sstevel@tonic-gate 			/*
23527c478bd9Sstevel@tonic-gate 			 * Add any dependencies of this dependency to the
23537c478bd9Sstevel@tonic-gate 			 * dynamic dependency list so they can be further
23547c478bd9Sstevel@tonic-gate 			 * processed.
23557c478bd9Sstevel@tonic-gate 			 */
2356cce0e03bSab196087 			for (APLIST_TRAVERSE(DEPENDS(dlmp1), idx2, bdp)) {
23577c478bd9Sstevel@tonic-gate 				Rt_map	*dlmp2 = bdp->b_depend;
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 				if ((bdp->b_flags & BND_NEEDED) == 0)
23607c478bd9Sstevel@tonic-gate 					continue;
23617c478bd9Sstevel@tonic-gate 
2362cce0e03bSab196087 				if (aplist_test(&lmalp, dlmp2,
23637c478bd9Sstevel@tonic-gate 				    AL_CNT_DEPCLCT) == 0) {
23647c478bd9Sstevel@tonic-gate 					free(lmalp);
23657c478bd9Sstevel@tonic-gate 					return (0);
23667c478bd9Sstevel@tonic-gate 				}
23677c478bd9Sstevel@tonic-gate 			}
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 			if (nlmp == dlmp1)
23707c478bd9Sstevel@tonic-gate 				continue;
23717c478bd9Sstevel@tonic-gate 
23727c478bd9Sstevel@tonic-gate 			if ((exist = hdl_add(ghp, dlmp1,
237356deab07SRod Evans 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0) {
23747c478bd9Sstevel@tonic-gate 				free(lmalp);
23757c478bd9Sstevel@tonic-gate 				return (0);
23767c478bd9Sstevel@tonic-gate 			}
237756deab07SRod Evans 
237856deab07SRod Evans 			if (exist == ALE_CREATE)
237956deab07SRod Evans 				(void) update_mode(dlmp1, MODE(dlmp1), nmode);
238056deab07SRod Evans 		}
23817c478bd9Sstevel@tonic-gate 		free(lmalp);
23827c478bd9Sstevel@tonic-gate 	}
23837c478bd9Sstevel@tonic-gate 	return (1);
23847c478bd9Sstevel@tonic-gate }
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate /*
23877c478bd9Sstevel@tonic-gate  * The central routine for loading shared objects.  Insures ldd() diagnostics,
23887c478bd9Sstevel@tonic-gate  * handles and any other related additions are all done in one place.
23897c478bd9Sstevel@tonic-gate  */
239056deab07SRod Evans Rt_map *
239156deab07SRod Evans load_path(Lm_list *lml, Aliste lmco, Rt_map *clmp, int nmode, uint_t flags,
239256deab07SRod Evans     Grp_hdl **hdl, Fdesc *fdp, Rej_desc *rej, int *in_nfavl)
23937c478bd9Sstevel@tonic-gate {
239456deab07SRod Evans 	const char	*name = fdp->fd_oname;
23957c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
23967c478bd9Sstevel@tonic-gate 
23977c478bd9Sstevel@tonic-gate 	if ((nmode & RTLD_NOLOAD) == 0) {
23987c478bd9Sstevel@tonic-gate 		/*
23997c478bd9Sstevel@tonic-gate 		 * If this isn't a noload request attempt to load the file.
24007c478bd9Sstevel@tonic-gate 		 */
240156deab07SRod Evans 		if ((nlmp = load_so(lml, lmco, clmp, flags, fdp, rej,
240256deab07SRod Evans 		    in_nfavl)) == NULL)
240337ffaf83SRod Evans 			return (NULL);
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 		/*
24067c478bd9Sstevel@tonic-gate 		 * If we've loaded a library which identifies itself as not
24077c478bd9Sstevel@tonic-gate 		 * being dlopen()'able catch it here.  Let non-dlopen()'able
24087c478bd9Sstevel@tonic-gate 		 * objects through under RTLD_CONFGEN as they're only being
24097c478bd9Sstevel@tonic-gate 		 * mapped to be dldump()'ed.
24107c478bd9Sstevel@tonic-gate 		 */
24117c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) && ((FLAGS(nlmp) &
24127c478bd9Sstevel@tonic-gate 		    (FLG_RT_NOOPEN | FLG_RT_RELOCED)) == FLG_RT_NOOPEN) &&
24137c478bd9Sstevel@tonic-gate 		    ((nmode & RTLD_CONFGEN) == 0)) {
24147c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
24157c478bd9Sstevel@tonic-gate 
24167c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
24177c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
24187c478bd9Sstevel@tonic-gate 			_rej.rej_str = MSG_INTL(MSG_GEN_NOOPEN);
2419ba2be530Sab196087 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
242031fdd7caSab196087 			rejection_inherit(rej, &_rej);
24217c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
242237ffaf83SRod Evans 			return (NULL);
24237c478bd9Sstevel@tonic-gate 		}
24247c478bd9Sstevel@tonic-gate 	} else {
24257c478bd9Sstevel@tonic-gate 		/*
24267c478bd9Sstevel@tonic-gate 		 * If it's a NOLOAD request - check to see if the object
24277c478bd9Sstevel@tonic-gate 		 * has already been loaded.
24287c478bd9Sstevel@tonic-gate 		 */
24297c478bd9Sstevel@tonic-gate 		/* LINTED */
24309aa23310Srie 		if (nlmp = is_so_loaded(lml, name, in_nfavl)) {
24317c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
24327c478bd9Sstevel@tonic-gate 			    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
24337c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name,
24347c478bd9Sstevel@tonic-gate 				    NAME(clmp));
24357247f888Srie 				/* BEGIN CSTYLED */
24367c478bd9Sstevel@tonic-gate 				if (*name == '/')
24377c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
24387c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
24397c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
24407c478bd9Sstevel@tonic-gate 				else
24417c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
24427c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
24437c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
24447c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
24457247f888Srie 				/* END CSTYLED */
24467c478bd9Sstevel@tonic-gate 			}
24477c478bd9Sstevel@tonic-gate 		} else {
24487c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
24517c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
24527c478bd9Sstevel@tonic-gate 			_rej.rej_str = strerror(ENOENT);
2453ba2be530Sab196087 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
245431fdd7caSab196087 			rejection_inherit(rej, &_rej);
245537ffaf83SRod Evans 			return (NULL);
24567c478bd9Sstevel@tonic-gate 		}
24577c478bd9Sstevel@tonic-gate 	}
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	/*
24607c478bd9Sstevel@tonic-gate 	 * Finish processing this loaded object.
24617c478bd9Sstevel@tonic-gate 	 */
2462390b98b5Srie 	if (load_finish(lml, name, clmp, nmode, flags, hdl, nlmp) == 0) {
2463390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 		/*
2466390b98b5Srie 		 * If this object has already been analyzed, then it is in use,
2467390b98b5Srie 		 * so even though this operation has failed, it should not be
2468390b98b5Srie 		 * torn down.
24697c478bd9Sstevel@tonic-gate 		 */
24707c478bd9Sstevel@tonic-gate 		if ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)
24717c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
247237ffaf83SRod Evans 		return (NULL);
24737c478bd9Sstevel@tonic-gate 	}
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	/*
2476390b98b5Srie 	 * If this object is new, and we're being audited, tell the audit
2477390b98b5Srie 	 * library of the file we've just opened.  Note, if the new link-map
2478390b98b5Srie 	 * requires local auditing of its dependencies we also register its
2479390b98b5Srie 	 * opening.
2480390b98b5Srie 	 */
2481390b98b5Srie 	if (FLAGS(nlmp) & FLG_RT_NEWLOAD) {
2482390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2483390b98b5Srie 
248456deab07SRod Evans 		if (((lml->lm_tflags | AFLAGS(clmp) | AFLAGS(nlmp)) &
2485390b98b5Srie 		    LML_TFLG_AUD_MASK) && (((lml->lm_flags |
2486390b98b5Srie 		    LIST(clmp)->lm_flags) & LML_FLG_NOAUDIT) == 0)) {
2487390b98b5Srie 			if (audit_objopen(clmp, nlmp) == 0) {
2488390b98b5Srie 				remove_so(lml, nlmp);
248937ffaf83SRod Evans 				return (NULL);
2490390b98b5Srie 			}
2491390b98b5Srie 		}
2492390b98b5Srie 	}
2493390b98b5Srie 	return (nlmp);
2494390b98b5Srie }
2495390b98b5Srie 
2496390b98b5Srie /*
24977c478bd9Sstevel@tonic-gate  * Load one object from a possible list of objects.  Typically, for requests
24987c478bd9Sstevel@tonic-gate  * such as NEEDED's, only one object is specified.  However, this object could
24997c478bd9Sstevel@tonic-gate  * be specified using $ISALIST or $HWCAP, in which case only the first object
25007c478bd9Sstevel@tonic-gate  * that can be loaded is used (ie. the best).
25017c478bd9Sstevel@tonic-gate  */
25027c478bd9Sstevel@tonic-gate Rt_map *
250356deab07SRod Evans load_one(Lm_list *lml, Aliste lmco, Alist *palp, Rt_map *clmp, int mode,
25049aa23310Srie     uint_t flags, Grp_hdl **hdl, int *in_nfavl)
25057c478bd9Sstevel@tonic-gate {
25067c478bd9Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
250756deab07SRod Evans 	Aliste		idx;
250856deab07SRod Evans 	Pdesc   	*pdp;
25097c478bd9Sstevel@tonic-gate 	const char	*name;
25107c478bd9Sstevel@tonic-gate 
251156deab07SRod Evans 	for (ALIST_TRAVERSE(palp, idx, pdp)) {
251256deab07SRod Evans 		Rt_map	*lmp = NULL;
25137247f888Srie 
25147c478bd9Sstevel@tonic-gate 		/*
25157c478bd9Sstevel@tonic-gate 		 * A Hardware capabilities requirement can itself expand into
25167c478bd9Sstevel@tonic-gate 		 * a number of candidates.
25177c478bd9Sstevel@tonic-gate 		 */
251856deab07SRod Evans 		if (pdp->pd_flags & PD_TKN_HWCAP) {
251956deab07SRod Evans 			lmp = load_hwcap(lml, lmco, pdp->pd_pname, clmp,
252056deab07SRod Evans 			    mode, (flags | FLG_RT_HWCAP), hdl, &rej, in_nfavl);
25217c478bd9Sstevel@tonic-gate 		} else {
252256deab07SRod Evans 			Fdesc	fd = { 0 };
252356deab07SRod Evans 
252456deab07SRod Evans 			/*
252556deab07SRod Evans 			 * Trace the inspection of this file, determine any
252656deab07SRod Evans 			 * auditor substitution, and seed the file descriptor
252756deab07SRod Evans 			 * with the originating name.
252856deab07SRod Evans 			 */
252956deab07SRod Evans 			if (load_trace(lml, pdp, clmp, &fd) == NULL)
253056deab07SRod Evans 				continue;
253156deab07SRod Evans 
253256deab07SRod Evans 			/*
253356deab07SRod Evans 			 * Locate and load the file.
253456deab07SRod Evans 			 */
253556deab07SRod Evans 			lmp = load_path(lml, lmco, clmp, mode, flags, hdl, &fd,
253656deab07SRod Evans 			    &rej, in_nfavl);
25377c478bd9Sstevel@tonic-gate 		}
253856deab07SRod Evans 		if (lmp)
253956deab07SRod Evans 			return (lmp);
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	/*
254356deab07SRod Evans 	 * If no objects can be found, use the first path name from the Alist
254456deab07SRod Evans 	 * to provide a diagnostic.  If this pathname originated from an
254556deab07SRod Evans 	 * expanded token, use the original name for any diagnostic output.
25467c478bd9Sstevel@tonic-gate 	 */
254756deab07SRod Evans 	pdp = alist_item(palp, 0);
254856deab07SRod Evans 
254956deab07SRod Evans 	if ((name = pdp->pd_oname) == 0)
255056deab07SRod Evans 		name = pdp->pd_pname;
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	file_notfound(lml, name, clmp, flags, &rej);
255337ffaf83SRod Evans 	return (NULL);
25547c478bd9Sstevel@tonic-gate }
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate /*
25579a411307Srie  * Determine whether a symbol is defined as an interposer.
25589a411307Srie  */
25599a411307Srie int
25609a411307Srie is_sym_interposer(Rt_map *lmp, Sym *sym)
25619a411307Srie {
25629a411307Srie 	Syminfo	*sip = SYMINFO(lmp);
25639a411307Srie 
25649a411307Srie 	if (sip) {
25659a411307Srie 		ulong_t	ndx;
25669a411307Srie 
25679a411307Srie 		ndx = (((ulong_t)sym - (ulong_t)SYMTAB(lmp)) / SYMENT(lmp));
25689a411307Srie 		/* LINTED */
25699a411307Srie 		sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(lmp)));
25709a411307Srie 		if (sip->si_flags & SYMINFO_FLG_INTERPOSE)
25719a411307Srie 			return (1);
25729a411307Srie 	}
25739a411307Srie 	return (0);
25749a411307Srie }
25759a411307Srie 
25769a411307Srie /*
25777c478bd9Sstevel@tonic-gate  * While processing direct or group bindings, determine whether the object to
25787c478bd9Sstevel@tonic-gate  * which we've bound can be interposed upon.  In this context, copy relocations
25797c478bd9Sstevel@tonic-gate  * are a form of interposition.
25807c478bd9Sstevel@tonic-gate  */
25817c478bd9Sstevel@tonic-gate static Sym *
2582adbfe822Srie lookup_sym_interpose(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Sym *osym,
2583adbfe822Srie     int *in_nfavl)
25847c478bd9Sstevel@tonic-gate {
2585adbfe822Srie 	Rt_map		*lmp, *clmp;
25867c478bd9Sstevel@tonic-gate 	Slookup		sl;
2587adbfe822Srie 	Lm_list		*lml;
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	/*
25907c478bd9Sstevel@tonic-gate 	 * If we've bound to a copy relocation definition then we need to assign
25917c478bd9Sstevel@tonic-gate 	 * this binding to the original copy reference.  Fabricate an inter-
25927c478bd9Sstevel@tonic-gate 	 * position diagnostic, as this is a legitimate form of interposition.
25937c478bd9Sstevel@tonic-gate 	 */
2594adbfe822Srie 	if (osym && (FLAGS1(*dlmp) & FL1_RT_COPYTOOK)) {
25957c478bd9Sstevel@tonic-gate 		Rel_copy	*rcp;
2596cce0e03bSab196087 		Aliste		idx;
25977c478bd9Sstevel@tonic-gate 
2598cce0e03bSab196087 		for (ALIST_TRAVERSE(COPY_R(*dlmp), idx, rcp)) {
2599aa736cbeSrie 			if ((osym == rcp->r_dsym) || (osym->st_value &&
2600aa736cbeSrie 			    (osym->st_value == rcp->r_dsym->st_value))) {
26017c478bd9Sstevel@tonic-gate 				*dlmp = rcp->r_rlmp;
26027c478bd9Sstevel@tonic-gate 				*binfo |=
26037c478bd9Sstevel@tonic-gate 				    (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
26047c478bd9Sstevel@tonic-gate 				return (rcp->r_rsym);
26057c478bd9Sstevel@tonic-gate 			}
26067c478bd9Sstevel@tonic-gate 		}
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate 
2609aa736cbeSrie 	/*
2610adbfe822Srie 	 * If a symbol binding has been established, inspect the link-map list
2611adbfe822Srie 	 * of the destination object, otherwise use the link-map list of the
2612adbfe822Srie 	 * original caller.
2613adbfe822Srie 	 */
2614adbfe822Srie 	if (osym)
2615adbfe822Srie 		clmp = *dlmp;
2616adbfe822Srie 	else
2617adbfe822Srie 		clmp = slp->sl_cmap;
2618adbfe822Srie 
2619adbfe822Srie 	lml = LIST(clmp);
2620adbfe822Srie 	lmp = lml->lm_head;
2621adbfe822Srie 
2622adbfe822Srie 	/*
2623aa736cbeSrie 	 * Prior to Solaris 8, external references from an executable that were
2624aa736cbeSrie 	 * bound to an uninitialized variable (.bss) within a shared object did
2625aa736cbeSrie 	 * not establish a copy relocation.  This was thought to be an
2626aa736cbeSrie 	 * optimization, to prevent copying zero's to zero's.  Typically,
2627aa736cbeSrie 	 * interposition took its course, with the shared object binding to the
2628aa736cbeSrie 	 * executables data definition.
2629aa736cbeSrie 	 *
2630aa736cbeSrie 	 * This scenario can be broken when this old executable runs against a
2631aa736cbeSrie 	 * new shared object that is directly bound.  With no copy-relocation
2632aa736cbeSrie 	 * record, ld.so.1 has no data to trigger the normal vectoring of the
2633aa736cbeSrie 	 * binding to the executable.
2634aa736cbeSrie 	 *
2635aa736cbeSrie 	 * Starting with Solaris 8, a DT_FLAGS entry is written to all objects,
2636aa736cbeSrie 	 * regardless of there being any DF_ flags entries.  Therefore, an
2637aa736cbeSrie 	 * object without this dynamic tag is susceptible to the copy relocation
2638aa736cbeSrie 	 * issue.  If the executable has no DT_FLAGS tag, and contains the same
2639aa736cbeSrie 	 * .bss symbol definition as has been directly bound to, redirect the
2640aa736cbeSrie 	 * binding to the executables data definition.
2641aa736cbeSrie 	 */
264256deab07SRod Evans 	if (osym && ((FLAGS1(lmp) & FL1_RT_DTFLAGS) == 0) &&
2643adbfe822Srie 	    (FCT(lmp) == &elf_fct) &&
2644466e2a62Srie 	    (ELF_ST_TYPE(osym->st_info) != STT_FUNC) &&
2645466e2a62Srie 	    are_bits_zero(*dlmp, osym, 0)) {
2646aa736cbeSrie 		Rt_map	*ilmp;
2647aa736cbeSrie 		Sym	*isym;
2648aa736cbeSrie 
2649adbfe822Srie 		sl = *slp;
2650adbfe822Srie 		sl.sl_imap = lmp;
2651adbfe822Srie 
2652aa736cbeSrie 		/*
2653aa736cbeSrie 		 * Determine whether the same symbol name exists within the
2654aa736cbeSrie 		 * executable, that the size and type of symbol are the same,
2655aa736cbeSrie 		 * and that the symbol is also associated with .bss.
2656aa736cbeSrie 		 */
26579aa23310Srie 		if (((isym = SYMINTP(lmp)(&sl, &ilmp, binfo,
26589aa23310Srie 		    in_nfavl)) != NULL) && (isym->st_size == osym->st_size) &&
2659aa736cbeSrie 		    (isym->st_info == osym->st_info) &&
2660466e2a62Srie 		    are_bits_zero(lmp, isym, 1)) {
2661aa736cbeSrie 			*dlmp = lmp;
2662aa736cbeSrie 			*binfo |= (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
2663aa736cbeSrie 			return (isym);
2664aa736cbeSrie 		}
2665aa736cbeSrie 	}
2666aa736cbeSrie 
26677c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_INTRPOSE) == 0)
266837ffaf83SRod Evans 		return (NULL);
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	/*
26717c478bd9Sstevel@tonic-gate 	 * Traverse the list of known interposers to determine whether any
26727c478bd9Sstevel@tonic-gate 	 * offer the same symbol.  Note, the head of the link-map could be
2673aa736cbeSrie 	 * identified as an interposer.  Otherwise, skip the head of the
2674aa736cbeSrie 	 * link-map, so that we don't bind to any .plt references, or
2675aa736cbeSrie 	 * copy-relocation destinations unintentionally.
26767c478bd9Sstevel@tonic-gate 	 */
26777c478bd9Sstevel@tonic-gate 	lmp = lml->lm_head;
26787c478bd9Sstevel@tonic-gate 	sl = *slp;
2679adbfe822Srie 
26809a411307Srie 	if (((FLAGS(lmp) & MSK_RT_INTPOSE) == 0) || (sl.sl_flags & LKUP_COPY))
2681cb511613SAli Bahrami 		lmp = NEXT_RT_MAP(lmp);
26827c478bd9Sstevel@tonic-gate 
2683cb511613SAli Bahrami 	for (; lmp; lmp = NEXT_RT_MAP(lmp)) {
26847c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_DELETE)
26857c478bd9Sstevel@tonic-gate 			continue;
26869a411307Srie 		if ((FLAGS(lmp) & MSK_RT_INTPOSE) == 0)
26877c478bd9Sstevel@tonic-gate 			break;
26887c478bd9Sstevel@tonic-gate 
2689adbfe822Srie 		if (callable(lmp, clmp, 0, sl.sl_flags)) {
26909a411307Srie 			Rt_map	*ilmp;
2691aa736cbeSrie 			Sym	*isym;
26929a411307Srie 
26937c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
26949aa23310Srie 			if (isym = SYMINTP(lmp)(&sl, &ilmp, binfo, in_nfavl)) {
26959a411307Srie 				/*
26969a411307Srie 				 * If this object provides individual symbol
26979a411307Srie 				 * interposers, make sure that the symbol we
26989a411307Srie 				 * have found is tagged as an interposer.
26999a411307Srie 				 */
27009a411307Srie 				if ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
2701aa736cbeSrie 				    (is_sym_interposer(ilmp, isym) == 0))
27029a411307Srie 					continue;
27039a411307Srie 
27049a411307Srie 				/*
27059a411307Srie 				 * Indicate this binding has occurred to an
27069a411307Srie 				 * interposer, and return the symbol.
27079a411307Srie 				 */
27087c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
27099a411307Srie 				*dlmp = ilmp;
2710aa736cbeSrie 				return (isym);
27117c478bd9Sstevel@tonic-gate 			}
27127c478bd9Sstevel@tonic-gate 		}
27137c478bd9Sstevel@tonic-gate 	}
271437ffaf83SRod Evans 	return (NULL);
27157c478bd9Sstevel@tonic-gate }
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate /*
27187c478bd9Sstevel@tonic-gate  * If an object specifies direct bindings (it contains a syminfo structure
27197c478bd9Sstevel@tonic-gate  * describing where each binding was established during link-editing, and the
27207c478bd9Sstevel@tonic-gate  * object was built -Bdirect), then look for the symbol in the specific object.
27217c478bd9Sstevel@tonic-gate  */
27227c478bd9Sstevel@tonic-gate static Sym *
27237c478bd9Sstevel@tonic-gate lookup_sym_direct(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Syminfo *sip,
27249aa23310Srie     Rt_map *lmp, int *in_nfavl)
27257c478bd9Sstevel@tonic-gate {
27267c478bd9Sstevel@tonic-gate 	Rt_map	*clmp = slp->sl_cmap;
27277c478bd9Sstevel@tonic-gate 	Sym	*sym;
27287c478bd9Sstevel@tonic-gate 	Slookup	sl;
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 	/*
27317c478bd9Sstevel@tonic-gate 	 * If a direct binding resolves to the definition of a copy relocated
27327c478bd9Sstevel@tonic-gate 	 * variable, it must be redirected to the copy (in the executable) that
27337c478bd9Sstevel@tonic-gate 	 * will eventually be made.  Typically, this redirection occurs in
27347c478bd9Sstevel@tonic-gate 	 * lookup_sym_interpose().  But, there's an edge condition.  If a
27357c478bd9Sstevel@tonic-gate 	 * directly bound executable contains pic code, there may be a
27367c478bd9Sstevel@tonic-gate 	 * reference to a definition that will eventually have a copy made.
27377c478bd9Sstevel@tonic-gate 	 * However, this copy relocation may not yet have occurred, because
27387c478bd9Sstevel@tonic-gate 	 * the relocation making this reference comes before the relocation
27397c478bd9Sstevel@tonic-gate 	 * that will create the copy.
27407c478bd9Sstevel@tonic-gate 	 * Under direct bindings, the syminfo indicates that a copy will be
27417c478bd9Sstevel@tonic-gate 	 * taken (SYMINFO_FLG_COPY).  This can only be set in an executable.
27427c478bd9Sstevel@tonic-gate 	 * Thus, the caller must be the executable, so bind to the destination
27437c478bd9Sstevel@tonic-gate 	 * of the copy within the executable.
27447c478bd9Sstevel@tonic-gate 	 */
27457c478bd9Sstevel@tonic-gate 	if (((slp->sl_flags & LKUP_COPY) == 0) &&
27467c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_COPY)) {
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 		slp->sl_imap = LIST(clmp)->lm_head;
27499aa23310Srie 		if (sym = SYMINTP(clmp)(slp, dlmp, binfo, in_nfavl))
27507c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_DIRECT | DBG_BINFO_COPYREF);
27517c478bd9Sstevel@tonic-gate 		return (sym);
27527c478bd9Sstevel@tonic-gate 	}
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 	/*
2755efb9e8b8Srie 	 * If we need to directly bind to our parent, start looking in each
2756efb9e8b8Srie 	 * callers link map.
27577c478bd9Sstevel@tonic-gate 	 */
27587c478bd9Sstevel@tonic-gate 	sl = *slp;
27597c478bd9Sstevel@tonic-gate 	sl.sl_flags |= LKUP_DIRECT;
2760adbfe822Srie 	sym = NULL;
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 	if (sip->si_boundto == SYMINFO_BT_PARENT) {
2763cce0e03bSab196087 		Aliste		idx1;
2764cce0e03bSab196087 		Bnd_desc	*bdp;
2765cce0e03bSab196087 		Grp_hdl		*ghp;
27667c478bd9Sstevel@tonic-gate 
2767efb9e8b8Srie 		/*
2768efb9e8b8Srie 		 * Determine the parent of this explicit dependency from its
2769efb9e8b8Srie 		 * CALLERS()'s list.
2770efb9e8b8Srie 		 */
2771cce0e03bSab196087 		for (APLIST_TRAVERSE(CALLERS(clmp), idx1, bdp)) {
2772cce0e03bSab196087 			sl.sl_imap = lmp = bdp->b_caller;
27739aa23310Srie 			if ((sym = SYMINTP(lmp)(&sl, dlmp, binfo,
27749aa23310Srie 			    in_nfavl)) != NULL)
2775efb9e8b8Srie 				goto found;
2776efb9e8b8Srie 		}
2777efb9e8b8Srie 
2778efb9e8b8Srie 		/*
2779efb9e8b8Srie 		 * A caller can also be defined as the parent of a dlopen()
2780efb9e8b8Srie 		 * call.  Determine whether this object has any handles.  The
2781efb9e8b8Srie 		 * dependencies maintained with the handle represent the
2782efb9e8b8Srie 		 * explicit dependencies of the dlopen()'ed object, and the
2783efb9e8b8Srie 		 * calling parent.
2784efb9e8b8Srie 		 */
2785cce0e03bSab196087 		for (APLIST_TRAVERSE(HANDLES(clmp), idx1, ghp)) {
2786efb9e8b8Srie 			Grp_desc	*gdp;
2787cce0e03bSab196087 			Aliste		idx2;
2788efb9e8b8Srie 
2789cce0e03bSab196087 			for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
2790efb9e8b8Srie 				if ((gdp->gd_flags & GPD_PARENT) == 0)
2791efb9e8b8Srie 					continue;
2792efb9e8b8Srie 				sl.sl_imap = lmp = gdp->gd_depend;
2793aa736cbeSrie 				if ((sym = SYMINTP(lmp)(&sl, dlmp,
27949aa23310Srie 				    binfo, in_nfavl)) != NULL)
2795efb9e8b8Srie 					goto found;
2796efb9e8b8Srie 			}
27977c478bd9Sstevel@tonic-gate 		}
27987c478bd9Sstevel@tonic-gate 	} else {
27997c478bd9Sstevel@tonic-gate 		/*
28007c478bd9Sstevel@tonic-gate 		 * If we need to direct bind to anything else look in the
28017c478bd9Sstevel@tonic-gate 		 * link map associated with this symbol reference.
28027c478bd9Sstevel@tonic-gate 		 */
28037c478bd9Sstevel@tonic-gate 		if (sip->si_boundto == SYMINFO_BT_SELF)
28047c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp = clmp;
28057c478bd9Sstevel@tonic-gate 		else
28067c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 		if (lmp)
28099aa23310Srie 			sym = SYMINTP(lmp)(&sl, dlmp, binfo, in_nfavl);
28107c478bd9Sstevel@tonic-gate 	}
2811efb9e8b8Srie found:
28127c478bd9Sstevel@tonic-gate 	if (sym)
28137c478bd9Sstevel@tonic-gate 		*binfo |= DBG_BINFO_DIRECT;
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	/*
2816adbfe822Srie 	 * If a reference to a directly bound symbol can't be satisfied, then
2817adbfe822Srie 	 * determine whether an interposer can provide the missing symbol.  If
2818adbfe822Srie 	 * a reference to a directly bound symbol is satisfied, then determine
2819adbfe822Srie 	 * whether that object can be interposed upon for this symbol.
28207c478bd9Sstevel@tonic-gate 	 */
2821adbfe822Srie 	if ((sym == NULL) || ((LIST(*dlmp)->lm_head != *dlmp) &&
2822adbfe822Srie 	    (LIST(*dlmp) == LIST(clmp)))) {
28237c478bd9Sstevel@tonic-gate 		Sym	*isym;
28247c478bd9Sstevel@tonic-gate 
2825adbfe822Srie 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
2826adbfe822Srie 		    in_nfavl)) != 0)
28277c478bd9Sstevel@tonic-gate 			return (isym);
28287c478bd9Sstevel@tonic-gate 	}
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	return (sym);
28317c478bd9Sstevel@tonic-gate }
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate static Sym *
283460758829Srie core_lookup_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
28359aa23310Srie     Aliste off, int *in_nfavl)
28367c478bd9Sstevel@tonic-gate {
28377c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	/*
28407c478bd9Sstevel@tonic-gate 	 * Copy relocations should start their search after the head of the
28417c478bd9Sstevel@tonic-gate 	 * main link-map control list.
28427c478bd9Sstevel@tonic-gate 	 */
2843cce0e03bSab196087 	if ((off == ALIST_OFF_DATA) && (slp->sl_flags & LKUP_COPY) && ilmp)
2844cb511613SAli Bahrami 		lmp = NEXT_RT_MAP(ilmp);
28457c478bd9Sstevel@tonic-gate 	else
28467c478bd9Sstevel@tonic-gate 		lmp = ilmp;
28477c478bd9Sstevel@tonic-gate 
2848cb511613SAli Bahrami 	for (; lmp; lmp = NEXT_RT_MAP(lmp)) {
284960758829Srie 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
28507c478bd9Sstevel@tonic-gate 			Sym	*sym;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
28539aa23310Srie 			if (((sym = SYMINTP(lmp)(slp, dlmp, binfo,
285437ffaf83SRod Evans 			    in_nfavl)) != NULL) ||
285537ffaf83SRod Evans 			    (*binfo & BINFO_MSK_TRYAGAIN))
28567c478bd9Sstevel@tonic-gate 				return (sym);
28577c478bd9Sstevel@tonic-gate 		}
28587c478bd9Sstevel@tonic-gate 	}
285937ffaf83SRod Evans 	return (NULL);
28607c478bd9Sstevel@tonic-gate }
28617c478bd9Sstevel@tonic-gate 
28627c478bd9Sstevel@tonic-gate static Sym *
28639aa23310Srie _lazy_find_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
28649aa23310Srie     int *in_nfavl)
28657c478bd9Sstevel@tonic-gate {
28667c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
28677c478bd9Sstevel@tonic-gate 
2868cb511613SAli Bahrami 	for (lmp = ilmp; lmp; lmp = NEXT_RT_MAP(lmp)) {
28697c478bd9Sstevel@tonic-gate 		if (LAZY(lmp) == 0)
28707c478bd9Sstevel@tonic-gate 			continue;
287160758829Srie 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
28727c478bd9Sstevel@tonic-gate 			Sym	*sym;
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
28759aa23310Srie 			if ((sym = elf_lazy_find_sym(slp, dlmp, binfo,
28769aa23310Srie 			    in_nfavl)) != 0)
28777c478bd9Sstevel@tonic-gate 				return (sym);
28787c478bd9Sstevel@tonic-gate 		}
28797c478bd9Sstevel@tonic-gate 	}
288037ffaf83SRod Evans 	return (NULL);
28817c478bd9Sstevel@tonic-gate }
28827c478bd9Sstevel@tonic-gate 
288360758829Srie static Sym *
28849aa23310Srie _lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
28857c478bd9Sstevel@tonic-gate {
28867c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
28877c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
2888dae2dfb7Srie 	Lm_list		*lml = LIST(clmp);
28897c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap, *lmp;
28907c478bd9Sstevel@tonic-gate 	ulong_t		rsymndx;
289160758829Srie 	Sym		*sym;
28927c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
28937c478bd9Sstevel@tonic-gate 	Slookup		sl;
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	/*
28967c478bd9Sstevel@tonic-gate 	 * Search the initial link map for the required symbol (this category is
28977c478bd9Sstevel@tonic-gate 	 * selected by dlsym(), where individual link maps are searched for a
28987c478bd9Sstevel@tonic-gate 	 * required symbol.  Therefore, we know we have permission to look at
28997c478bd9Sstevel@tonic-gate 	 * the link map).
29007c478bd9Sstevel@tonic-gate 	 */
290160758829Srie 	if (slp->sl_flags & LKUP_FIRST)
29029aa23310Srie 		return (SYMINTP(ilmp)(slp, dlmp, binfo, in_nfavl));
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	/*
29057c478bd9Sstevel@tonic-gate 	 * Determine whether this lookup can be satisfied by an objects direct,
29067c478bd9Sstevel@tonic-gate 	 * or lazy binding information.  This is triggered by a relocation from
29077c478bd9Sstevel@tonic-gate 	 * the object (hence rsymndx is set).
29087c478bd9Sstevel@tonic-gate 	 */
29097c478bd9Sstevel@tonic-gate 	if (((rsymndx = slp->sl_rsymndx) != 0) &&
2910aa736cbeSrie 	    ((sip = SYMINFO(clmp)) != NULL)) {
2911dae2dfb7Srie 		uint_t	bound;
2912dae2dfb7Srie 
29137c478bd9Sstevel@tonic-gate 		/*
29147c478bd9Sstevel@tonic-gate 		 * Find the corresponding Syminfo entry for the original
29157c478bd9Sstevel@tonic-gate 		 * referencing symbol.
29167c478bd9Sstevel@tonic-gate 		 */
29177c478bd9Sstevel@tonic-gate 		/* LINTED */
29187c478bd9Sstevel@tonic-gate 		sip = (Syminfo *)((char *)sip + (rsymndx * SYMINENT(clmp)));
2919dae2dfb7Srie 		bound = sip->si_boundto;
2920dae2dfb7Srie 
2921dae2dfb7Srie 		/*
2922dae2dfb7Srie 		 * Identify any EXTERN or PARENT references for ldd(1).
2923dae2dfb7Srie 		 */
2924dae2dfb7Srie 		if ((lml->lm_flags & LML_FLG_TRC_WARN) &&
2925dae2dfb7Srie 		    (bound > SYMINFO_BT_LOWRESERVE)) {
2926dae2dfb7Srie 			if (bound == SYMINFO_BT_PARENT)
2927dae2dfb7Srie 				*binfo |= DBG_BINFO_REF_PARENT;
2928dae2dfb7Srie 			if (bound == SYMINFO_BT_EXTERN)
2929dae2dfb7Srie 				*binfo |= DBG_BINFO_REF_EXTERN;
2930dae2dfb7Srie 		}
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 		/*
29337c478bd9Sstevel@tonic-gate 		 * If the symbol information indicates a direct binding,
29347c478bd9Sstevel@tonic-gate 		 * determine the link map that is required to satisfy the
29357c478bd9Sstevel@tonic-gate 		 * binding.  Note, if the dependency can not be found, but a
29367c478bd9Sstevel@tonic-gate 		 * direct binding isn't required, we will still fall through
29377c478bd9Sstevel@tonic-gate 		 * to perform any default symbol search.
29387c478bd9Sstevel@tonic-gate 		 */
29397c478bd9Sstevel@tonic-gate 		if (sip->si_flags & SYMINFO_FLG_DIRECT) {
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 			lmp = 0;
29427c478bd9Sstevel@tonic-gate 			if (bound < SYMINFO_BT_LOWRESERVE)
29439aa23310Srie 				lmp = elf_lazy_load(clmp, slp, bound,
29449aa23310Srie 				    name, in_nfavl);
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate 			/*
29477c478bd9Sstevel@tonic-gate 			 * If direct bindings have been disabled, and this isn't
29487c478bd9Sstevel@tonic-gate 			 * a translator, skip any direct binding now that we've
294960758829Srie 			 * ensured the resolving object has been loaded.
29507c478bd9Sstevel@tonic-gate 			 *
29517c478bd9Sstevel@tonic-gate 			 * If we need to direct bind to anything, we look in
29527c478bd9Sstevel@tonic-gate 			 * ourselves, our parent, or in the link map we've just
29537c478bd9Sstevel@tonic-gate 			 * loaded.  Otherwise, even though we may have lazily
29547c478bd9Sstevel@tonic-gate 			 * loaded an object we still continue to search for
29557c478bd9Sstevel@tonic-gate 			 * symbols from the head of the link map list.
29567c478bd9Sstevel@tonic-gate 			 */
29577c478bd9Sstevel@tonic-gate 			if (((FLAGS(clmp) & FLG_RT_TRANS) ||
2958dae2dfb7Srie 			    (((lml->lm_tflags & LML_TFLG_NODIRECT) == 0) &&
2959dae2dfb7Srie 			    ((slp->sl_flags & LKUP_SINGLETON) == 0))) &&
29609a411307Srie 			    ((FLAGS1(clmp) & FL1_RT_DIRECT) ||
29617c478bd9Sstevel@tonic-gate 			    (sip->si_flags & SYMINFO_FLG_DIRECTBIND))) {
29627c478bd9Sstevel@tonic-gate 				sym = lookup_sym_direct(slp, dlmp, binfo,
29639aa23310Srie 				    sip, lmp, in_nfavl);
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 				/*
296660758829Srie 				 * Determine whether this direct binding has
296760758829Srie 				 * been rejected.  If we've bound to a singleton
296860758829Srie 				 * without following a singleton search, then
296960758829Srie 				 * return.  The caller detects this condition
297060758829Srie 				 * and will trigger a new singleton search.
297160758829Srie 				 *
297260758829Srie 				 * For any other rejection (such as binding to
297360758829Srie 				 * a symbol labeled as nodirect - presumably
297460758829Srie 				 * because the symbol definition has been
297560758829Srie 				 * changed since the referring object was last
297660758829Srie 				 * built), fall through to a standard symbol
29777c478bd9Sstevel@tonic-gate 				 * search.
29787c478bd9Sstevel@tonic-gate 				 */
297937ffaf83SRod Evans 				if (((*binfo & BINFO_MSK_REJECTED) == 0) ||
298037ffaf83SRod Evans 				    (*binfo & BINFO_MSK_TRYAGAIN))
29817c478bd9Sstevel@tonic-gate 					return (sym);
298260758829Srie 
298337ffaf83SRod Evans 				*binfo &= ~BINFO_MSK_REJECTED;
29847c478bd9Sstevel@tonic-gate 			}
29857c478bd9Sstevel@tonic-gate 		}
29867c478bd9Sstevel@tonic-gate 	}
29877c478bd9Sstevel@tonic-gate 
298860758829Srie 	/*
298960758829Srie 	 * Duplicate the lookup information, as we'll need to modify this
299060758829Srie 	 * information for some of the following searches.
299160758829Srie 	 */
29927c478bd9Sstevel@tonic-gate 	sl = *slp;
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	/*
29957c478bd9Sstevel@tonic-gate 	 * If the referencing object has the DF_SYMBOLIC flag set, look in the
29967c478bd9Sstevel@tonic-gate 	 * referencing object for the symbol first.  Failing that, fall back to
29977c478bd9Sstevel@tonic-gate 	 * our generic search.
29987c478bd9Sstevel@tonic-gate 	 */
299960758829Srie 	if ((FLAGS1(clmp) & FL1_RT_SYMBOLIC) &&
300060758829Srie 	    ((sl.sl_flags & LKUP_SINGLETON) == 0)) {
30017c478bd9Sstevel@tonic-gate 		sl.sl_imap = clmp;
30029aa23310Srie 		if (sym = SYMINTP(clmp)(&sl, dlmp, binfo, in_nfavl)) {
30039a411307Srie 			ulong_t	dsymndx = (((ulong_t)sym -
30049a411307Srie 			    (ulong_t)SYMTAB(*dlmp)) / SYMENT(*dlmp));
30059a411307Srie 
30069a411307Srie 			/*
30079a411307Srie 			 * Make sure this symbol hasn't explicitly been defined
30089a411307Srie 			 * as nodirect.
30099a411307Srie 			 */
30109a411307Srie 			if (((sip = SYMINFO(*dlmp)) == 0) ||
30119a411307Srie 			    /* LINTED */
30129a411307Srie 			    ((sip = (Syminfo *)((char *)sip +
30139a411307Srie 			    (dsymndx * SYMINENT(*dlmp)))) == 0) ||
30149a411307Srie 			    ((sip->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0))
30157c478bd9Sstevel@tonic-gate 				return (sym);
30167c478bd9Sstevel@tonic-gate 		}
30179a411307Srie 	}
30187c478bd9Sstevel@tonic-gate 
301960758829Srie 	sl.sl_flags |= LKUP_STANDARD;
302060758829Srie 
30217c478bd9Sstevel@tonic-gate 	/*
30227c478bd9Sstevel@tonic-gate 	 * If this lookup originates from a standard relocation, then traverse
302360758829Srie 	 * all link-map control lists, inspecting any object that is available
302460758829Srie 	 * to this caller.  Otherwise, traverse the link-map control list
302560758829Srie 	 * associated with the caller.
30267c478bd9Sstevel@tonic-gate 	 */
302760758829Srie 	if (sl.sl_flags & LKUP_STDRELOC) {
30287c478bd9Sstevel@tonic-gate 		Aliste	off;
30297c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
30307c478bd9Sstevel@tonic-gate 
303160758829Srie 		sym = NULL;
30327c478bd9Sstevel@tonic-gate 
3033dae2dfb7Srie 		for (ALIST_TRAVERSE_BY_OFFSET(lml->lm_lists, off, lmc)) {
303460758829Srie 			if (((sym = core_lookup_sym(lmc->lc_head, &sl, dlmp,
30359aa23310Srie 			    binfo, off, in_nfavl)) != NULL) ||
303637ffaf83SRod Evans 			    (*binfo & BINFO_MSK_TRYAGAIN))
30377c478bd9Sstevel@tonic-gate 				break;
30387c478bd9Sstevel@tonic-gate 		}
30397c478bd9Sstevel@tonic-gate 	} else
30409aa23310Srie 		sym = core_lookup_sym(ilmp, &sl, dlmp, binfo, ALIST_OFF_DATA,
30419aa23310Srie 		    in_nfavl);
304260758829Srie 
304360758829Srie 	/*
304437ffaf83SRod Evans 	 * If a symbol binding should be retried, return so that the search can
304537ffaf83SRod Evans 	 * be repeated.
304660758829Srie 	 */
304737ffaf83SRod Evans 	if (*binfo & BINFO_MSK_TRYAGAIN)
304860758829Srie 		return (sym);
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	/*
30517c478bd9Sstevel@tonic-gate 	 * To allow transitioning into a world of lazy loading dependencies see
30527c478bd9Sstevel@tonic-gate 	 * if this link map contains objects that have lazy dependencies still
30537c478bd9Sstevel@tonic-gate 	 * outstanding.  If so, and we haven't been able to locate a non-weak
30547c478bd9Sstevel@tonic-gate 	 * symbol reference, start bringing in any lazy dependencies to see if
30557c478bd9Sstevel@tonic-gate 	 * the reference can be satisfied.  Use of dlsym(RTLD_PROBE) sets the
305675e7992aSrie 	 * LKUP_NOFALLBACK flag, and this flag disables this fall back.
30577c478bd9Sstevel@tonic-gate 	 */
305875e7992aSrie 	if ((sym == NULL) && ((sl.sl_flags & LKUP_NOFALLBACK) == 0)) {
30597c478bd9Sstevel@tonic-gate 		if ((lmp = ilmp) == 0)
30607c478bd9Sstevel@tonic-gate 			lmp = LIST(clmp)->lm_head;
306175e7992aSrie 
3062dae2dfb7Srie 		lml = LIST(lmp);
3063dae2dfb7Srie 		if ((sl.sl_flags & LKUP_WEAK) || (lml->lm_lazy == 0))
306437ffaf83SRod Evans 			return (NULL);
30657c478bd9Sstevel@tonic-gate 
3066dae2dfb7Srie 		DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 		/*
30697c478bd9Sstevel@tonic-gate 		 * If this request originated from a dlsym(RTLD_NEXT) then start
30707c478bd9Sstevel@tonic-gate 		 * looking for dependencies from the caller, otherwise use the
30717c478bd9Sstevel@tonic-gate 		 * initial link-map.
30727c478bd9Sstevel@tonic-gate 		 */
307360758829Srie 		if (sl.sl_flags & LKUP_NEXT)
30749aa23310Srie 			sym = _lazy_find_sym(clmp, &sl, dlmp, binfo, in_nfavl);
30757c478bd9Sstevel@tonic-gate 		else {
3076cce0e03bSab196087 			Aliste	idx;
30777c478bd9Sstevel@tonic-gate 			Lm_cntl	*lmc;
30787c478bd9Sstevel@tonic-gate 
3079dae2dfb7Srie 			for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
308075e7992aSrie 				sl.sl_flags |= LKUP_NOFALLBACK;
30817c478bd9Sstevel@tonic-gate 				if ((sym = _lazy_find_sym(lmc->lc_head, &sl,
30829aa23310Srie 				    dlmp, binfo, in_nfavl)) != 0)
30837c478bd9Sstevel@tonic-gate 					break;
30847c478bd9Sstevel@tonic-gate 			}
30857c478bd9Sstevel@tonic-gate 		}
30867c478bd9Sstevel@tonic-gate 	}
308760758829Srie 	return (sym);
308860758829Srie }
308960758829Srie 
309060758829Srie /*
309160758829Srie  * Symbol lookup routine.  Takes an ELF symbol name, and a list of link maps to
309260758829Srie  * search.  If successful, return a pointer to the symbol table entry, a
309360758829Srie  * pointer to the link map of the enclosing object, and information relating
309460758829Srie  * to the type of binding.  Else return a null pointer.
309560758829Srie  *
309660758829Srie  * To improve elf performance, we first compute the elf hash value and pass
309760758829Srie  * it to each find_sym() routine.  The elf function will use this value to
309860758829Srie  * locate the symbol, the a.out function will simply ignore it.
309960758829Srie  */
310060758829Srie Sym *
31019aa23310Srie lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
310260758829Srie {
310360758829Srie 	Rt_map		*clmp = slp->sl_cmap;
310460758829Srie 	Sym		*rsym = slp->sl_rsym, *sym = 0;
310560758829Srie 	uchar_t		rtype = slp->sl_rtype;
310637ffaf83SRod Evans 	int		mode;
310760758829Srie 
310860758829Srie 	if (slp->sl_hash == 0)
310975e7992aSrie 		slp->sl_hash = elf_hash(slp->sl_name);
311060758829Srie 	*binfo = 0;
311160758829Srie 
311260758829Srie 	/*
311360758829Srie 	 * Establish any state that might be associated with a symbol reference.
311460758829Srie 	 */
311560758829Srie 	if (rsym) {
311660758829Srie 		if ((slp->sl_flags & LKUP_STDRELOC) &&
311760758829Srie 		    (ELF_ST_BIND(rsym->st_info) == STB_WEAK))
311860758829Srie 			slp->sl_flags |= LKUP_WEAK;
311960758829Srie 
312060758829Srie 		if (ELF_ST_VISIBILITY(rsym->st_other) == STV_SINGLETON)
312160758829Srie 			slp->sl_flags |= LKUP_SINGLETON;
312260758829Srie 	}
312360758829Srie 
312460758829Srie 	/*
312560758829Srie 	 * Establish any lookup state required for this type of relocation.
312660758829Srie 	 */
312760758829Srie 	if ((slp->sl_flags & LKUP_STDRELOC) && rtype) {
312860758829Srie 		if (rtype == M_R_COPY)
312960758829Srie 			slp->sl_flags |= LKUP_COPY;
313060758829Srie 
313160758829Srie 		if (rtype != M_R_JMP_SLOT)
313260758829Srie 			slp->sl_flags |= LKUP_SPEC;
313360758829Srie 	}
313460758829Srie 
313560758829Srie 	/*
313660758829Srie 	 * Under ldd -w, any unresolved weak references are diagnosed.  Set the
313760758829Srie 	 * symbol binding as global to trigger a relocation error if the symbol
313860758829Srie 	 * can not be found.
313960758829Srie 	 */
314060758829Srie 	if (rsym) {
314160758829Srie 		if (LIST(slp->sl_cmap)->lm_flags & LML_FLG_TRC_NOUNRESWEAK)
314260758829Srie 			slp->sl_bind = STB_GLOBAL;
314360758829Srie 		else if ((slp->sl_bind = ELF_ST_BIND(rsym->st_info)) ==
314460758829Srie 		    STB_WEAK)
314560758829Srie 			slp->sl_flags |= LKUP_WEAK;
314660758829Srie 	}
314760758829Srie 
314860758829Srie 	/*
314937ffaf83SRod Evans 	 * Save the callers MODE().
315037ffaf83SRod Evans 	 */
315137ffaf83SRod Evans 	mode = MODE(clmp);
315237ffaf83SRod Evans 
315337ffaf83SRod Evans 	/*
315460758829Srie 	 * Carry out an initial symbol search.  This search takes into account
315560758829Srie 	 * all the modes of the requested search.
315660758829Srie 	 */
31579aa23310Srie 	if (((sym = _lookup_sym(slp, dlmp, binfo, in_nfavl)) == NULL) &&
315837ffaf83SRod Evans 	    (*binfo & BINFO_MSK_TRYAGAIN)) {
315960758829Srie 		Slookup	sl = *slp;
316060758829Srie 
316160758829Srie 		/*
316237ffaf83SRod Evans 		 * Try the symbol search again.  This retry can be necessary if:
316337ffaf83SRod Evans 		 *
316437ffaf83SRod Evans 		 *  .	a binding has been rejected because of binding to a
316537ffaf83SRod Evans 		 *	singleton without going through a singleton search.
316637ffaf83SRod Evans 		 *  .	a group binding has resulted in binding to a symbol
316737ffaf83SRod Evans 		 *	that indicates no-direct binding.
316837ffaf83SRod Evans 		 *
316937ffaf83SRod Evans 		 * Reset the lookup data, and try again.
317060758829Srie 		 */
317160758829Srie 		sl.sl_imap = LIST(sl.sl_cmap)->lm_head;
317260758829Srie 		sl.sl_flags &= ~(LKUP_FIRST | LKUP_SELF | LKUP_NEXT);
317360758829Srie 		sl.sl_rsymndx = 0;
317437ffaf83SRod Evans 
317537ffaf83SRod Evans 		if (*binfo & BINFO_REJSINGLE)
317637ffaf83SRod Evans 			sl.sl_flags |= LKUP_SINGLETON;
317737ffaf83SRod Evans 		if (*binfo & BINFO_REJGROUP) {
317837ffaf83SRod Evans 			sl.sl_flags |= LKUP_WORLD;
317937ffaf83SRod Evans 			mode |= RTLD_WORLD;
318037ffaf83SRod Evans 		}
318137ffaf83SRod Evans 		*binfo &= ~BINFO_MSK_REJECTED;
318237ffaf83SRod Evans 
31839aa23310Srie 		sym = _lookup_sym(&sl, dlmp, binfo, in_nfavl);
318460758829Srie 	}
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 	/*
31877c478bd9Sstevel@tonic-gate 	 * If the caller is restricted to a symbol search within its group,
31887c478bd9Sstevel@tonic-gate 	 * determine if it is necessary to follow a binding from outside of
31897c478bd9Sstevel@tonic-gate 	 * the group.
31907c478bd9Sstevel@tonic-gate 	 */
319137ffaf83SRod Evans 	if ((mode & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP) {
31927c478bd9Sstevel@tonic-gate 		Sym	*isym;
31937c478bd9Sstevel@tonic-gate 
3194adbfe822Srie 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
319537ffaf83SRod Evans 		    in_nfavl)) != NULL)
31967c478bd9Sstevel@tonic-gate 			return (isym);
31977c478bd9Sstevel@tonic-gate 	}
31987c478bd9Sstevel@tonic-gate 	return (sym);
31997c478bd9Sstevel@tonic-gate }
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate /*
32027c478bd9Sstevel@tonic-gate  * Associate a binding descriptor with a caller and its dependency, or update
32037c478bd9Sstevel@tonic-gate  * an existing descriptor.
32047c478bd9Sstevel@tonic-gate  */
32057c478bd9Sstevel@tonic-gate int
32067c478bd9Sstevel@tonic-gate bind_one(Rt_map *clmp, Rt_map *dlmp, uint_t flags)
32077c478bd9Sstevel@tonic-gate {
3208cce0e03bSab196087 	Bnd_desc	*bdp;
3209cce0e03bSab196087 	Aliste		idx;
32107c478bd9Sstevel@tonic-gate 	int		found = ALE_CREATE;
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	/*
32137c478bd9Sstevel@tonic-gate 	 * Determine whether a binding descriptor already exists between the
32147c478bd9Sstevel@tonic-gate 	 * two objects.
32157c478bd9Sstevel@tonic-gate 	 */
3216cce0e03bSab196087 	for (APLIST_TRAVERSE(DEPENDS(clmp), idx, bdp)) {
32177c478bd9Sstevel@tonic-gate 		if (bdp->b_depend == dlmp) {
32187c478bd9Sstevel@tonic-gate 			found = ALE_EXISTS;
32197c478bd9Sstevel@tonic-gate 			break;
32207c478bd9Sstevel@tonic-gate 		}
32217c478bd9Sstevel@tonic-gate 	}
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	if (found == ALE_CREATE) {
32247c478bd9Sstevel@tonic-gate 		/*
32257c478bd9Sstevel@tonic-gate 		 * Create a new binding descriptor.
32267c478bd9Sstevel@tonic-gate 		 */
322737ffaf83SRod Evans 		if ((bdp = malloc(sizeof (Bnd_desc))) == NULL)
32287c478bd9Sstevel@tonic-gate 			return (0);
32297c478bd9Sstevel@tonic-gate 
32307c478bd9Sstevel@tonic-gate 		bdp->b_caller = clmp;
32317c478bd9Sstevel@tonic-gate 		bdp->b_depend = dlmp;
32327c478bd9Sstevel@tonic-gate 		bdp->b_flags = 0;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 		/*
32357c478bd9Sstevel@tonic-gate 		 * Append the binding descriptor to the caller and the
32367c478bd9Sstevel@tonic-gate 		 * dependency.
32377c478bd9Sstevel@tonic-gate 		 */
323856deab07SRod Evans 		if (aplist_append(&DEPENDS(clmp), bdp, AL_CNT_DEPENDS) == NULL)
32397c478bd9Sstevel@tonic-gate 			return (0);
32407c478bd9Sstevel@tonic-gate 
324156deab07SRod Evans 		if (aplist_append(&CALLERS(dlmp), bdp, AL_CNT_CALLERS) == NULL)
32427c478bd9Sstevel@tonic-gate 			return (0);
32437c478bd9Sstevel@tonic-gate 	}
32447c478bd9Sstevel@tonic-gate 
32457c478bd9Sstevel@tonic-gate 	if ((found == ALE_CREATE) || ((bdp->b_flags & flags) != flags)) {
32467c478bd9Sstevel@tonic-gate 		bdp->b_flags |= flags;
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 		if (flags & BND_REFER)
32497c478bd9Sstevel@tonic-gate 			FLAGS1(dlmp) |= FL1_RT_USED;
32507c478bd9Sstevel@tonic-gate 
32515aefb655Srie 		DBG_CALL(Dbg_file_bind_entry(LIST(clmp), bdp));
32527c478bd9Sstevel@tonic-gate 	}
32537c478bd9Sstevel@tonic-gate 	return (found);
32547c478bd9Sstevel@tonic-gate }
32557c478bd9Sstevel@tonic-gate 
32567c478bd9Sstevel@tonic-gate /*
32577c478bd9Sstevel@tonic-gate  * Cleanup after relocation processing.
32587c478bd9Sstevel@tonic-gate  */
32597c478bd9Sstevel@tonic-gate int
326056deab07SRod Evans relocate_finish(Rt_map *lmp, APlist *bound, int ret)
32617c478bd9Sstevel@tonic-gate {
32625aefb655Srie 	DBG_CALL(Dbg_reloc_run(lmp, 0, ret, DBG_REL_FINISH));
32637c478bd9Sstevel@tonic-gate 
32647c478bd9Sstevel@tonic-gate 	/*
32657c478bd9Sstevel@tonic-gate 	 * Establish bindings to all objects that have been bound to.
32667c478bd9Sstevel@tonic-gate 	 */
32677c478bd9Sstevel@tonic-gate 	if (bound) {
3268cce0e03bSab196087 		Rt_map	*_lmp;
32698a20d9f8Srie 		Word	used;
32707c478bd9Sstevel@tonic-gate 
32718a20d9f8Srie 		/*
32728a20d9f8Srie 		 * Only create bindings if the callers relocation was
32738a20d9f8Srie 		 * successful (ret != 0), otherwise the object will eventually
32748a20d9f8Srie 		 * be torn down.  Create these bindings if running under ldd(1)
32758a20d9f8Srie 		 * with the -U/-u options regardless of relocation errors, as
32768a20d9f8Srie 		 * the unused processing needs to traverse these bindings to
32778a20d9f8Srie 		 * diagnose unused objects.
32788a20d9f8Srie 		 */
32798a20d9f8Srie 		used = LIST(lmp)->lm_flags &
32808a20d9f8Srie 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED);
32818a20d9f8Srie 
32828a20d9f8Srie 		if (ret || used) {
328356deab07SRod Evans 			Aliste	idx;
328456deab07SRod Evans 
3285cce0e03bSab196087 			for (APLIST_TRAVERSE(bound, idx, _lmp)) {
32868a20d9f8Srie 				if (bind_one(lmp, _lmp, BND_REFER) || used)
32878a20d9f8Srie 					continue;
32888a20d9f8Srie 
32897c478bd9Sstevel@tonic-gate 				ret = 0;
32907c478bd9Sstevel@tonic-gate 				break;
32917c478bd9Sstevel@tonic-gate 			}
32927c478bd9Sstevel@tonic-gate 		}
32937c478bd9Sstevel@tonic-gate 		free(bound);
32947c478bd9Sstevel@tonic-gate 	}
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	return (ret);
32977c478bd9Sstevel@tonic-gate }
329856deab07SRod Evans 
329956deab07SRod Evans /*
330056deab07SRod Evans  * Function to correct protection settings.  Segments are all mapped initially
330156deab07SRod Evans  * with permissions as given in the segment header.  We need to turn on write
330256deab07SRod Evans  * permissions on a text segment if there are any relocations against that
330356deab07SRod Evans  * segment, and then turn write permission back off again before returning
330456deab07SRod Evans  * control to the caller.  This function turns the permission on or off
330556deab07SRod Evans  * depending on the value of the permission argument.
330656deab07SRod Evans  */
330756deab07SRod Evans int
330856deab07SRod Evans set_prot(Rt_map *lmp, mmapobj_result_t *mpp, int perm)
330956deab07SRod Evans {
331056deab07SRod Evans 	int	prot;
331156deab07SRod Evans 
331256deab07SRod Evans 	/*
331356deab07SRod Evans 	 * If this is an allocated image (ie. a relocatable object) we can't
331456deab07SRod Evans 	 * mprotect() anything.
331556deab07SRod Evans 	 */
331656deab07SRod Evans 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
331756deab07SRod Evans 		return (1);
331856deab07SRod Evans 
331956deab07SRod Evans 	DBG_CALL(Dbg_file_prot(lmp, perm));
332056deab07SRod Evans 
332156deab07SRod Evans 	if (perm)
332256deab07SRod Evans 		prot = mpp->mr_prot | PROT_WRITE;
332356deab07SRod Evans 	else
332456deab07SRod Evans 		prot = mpp->mr_prot & ~PROT_WRITE;
332556deab07SRod Evans 
332656deab07SRod Evans 	if (mprotect((void *)(uintptr_t)mpp->mr_addr,
332756deab07SRod Evans 	    mpp->mr_msize, prot) == -1) {
332856deab07SRod Evans 		int	err = errno;
332956deab07SRod Evans 		eprintf(LIST(lmp), ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
333056deab07SRod Evans 		    NAME(lmp), strerror(err));
333156deab07SRod Evans 		return (0);
333256deab07SRod Evans 	}
333356deab07SRod Evans 	mpp->mr_prot = prot;
333456deab07SRod Evans 	return (1);
333556deab07SRod Evans }
3336