xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/analyze.c (revision 481bba9e)
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))) {
6575e7992aSrie 		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 */
112cce0e03bSab 	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 */
202cce0e03bSab 		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 */
210cce0e03bSab 	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;
23356deab07SRod Evans 	caddr_t			daddr = (caddr_t)dsym->st_value;
2347c478bd9Sstevel@tonic-gate 
235466e2a62Srie 	if ((FLAGS(dlmp) & FLG_RT_FIXED) == 0)
236466e2a62Srie 		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)
244466e2a62Srie 		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))
250466e2a62Srie 		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) {
37456deab07SRod Evans 			lml->lm_init++;
37556deab07SRod Evans 			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
389cce0e03bSab 	 * change. Presently, a given link map can only have a receiver or
390cce0e03bSab 	 * supplier of copy data, so a union is used to overlap the storage
391cce0e03bSab 	 * for the COPY_R() and COPY_S() lists. These lists would need to
392cce0e03bSab 	 * 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))) {
397cce0e03bSab 		Rt_map		*lmp;
398cce0e03bSab 		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
405cce0e03bSab 		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 
417cce0e03bSab 		for (APLIST_TRAVERSE(COPY_S(nlmp), idx1, lmp)) {
418aa736cbeSrie 			Rel_copy	*rcp;
419cce0e03bSab 			Aliste		idx2;
4207c478bd9Sstevel@tonic-gate 
421cce0e03bSab 			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 
449cce0e03bSab 		free(COPY_S(nlmp));
450cce0e03bSab 		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;
460cce0e03bSab 	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 */
473cce0e03bSab 	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 		 */
512cce0e03bSab 		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);
524cce0e03bSab 		if ((lret == 0) && (nlmco != ALIST_OFF_DATA))
525*481bba9eSRod 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 */
532cce0e03bSab 	nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
533cce0e03bSab 	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 */
539cce0e03bSab 		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 	 */
550cce0e03bSab 	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 	 */
562cce0e03bSab 	while ((alp = plmc->lc_now) != NULL) {
563cce0e03bSab 		Aliste	idx;
564cce0e03bSab 		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 		 */
571cce0e03bSab 		plmc->lc_now = NULL;
5727c478bd9Sstevel@tonic-gate 
573cce0e03bSab 		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 */
609cce0e03bSab 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
61024a6229eSrie 		/* LINTED */
611cce0e03bSab 		plmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, plmco);
612cce0e03bSab 		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) {
62402ca3e02Srie 		/* LINTED */
625cce0e03bSab 		nlmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, nlmco);
62602ca3e02Srie 		nlmc->lc_flags &= ~LMC_FLG_RELOCATING;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		return (1);
62902ca3e02Srie 	}
63002ca3e02Srie 
63102ca3e02Srie 	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
65731fdd7caSab 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))
66756deab07SRod Evans 			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 {
717cce0e03bSab 	Aliste		idx;
718cce0e03bSab 	const char	*cp;
7197c478bd9Sstevel@tonic-gate 
72056deab07SRod Evans 	if (_is_so_matched(name, NAME(lmp), path) == 0)
72156deab07SRod Evans 		return (lmp);
7220aa3cd4dSrie 
72356deab07SRod Evans 	if (PATHNAME(lmp) != NAME(lmp)) {
72456deab07SRod Evans 		if (_is_so_matched(name, PATHNAME(lmp), path) == 0)
72556deab07SRod Evans 			return (lmp);
7260aa3cd4dSrie 	}
7270aa3cd4dSrie 
728cce0e03bSab 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
729cce0e03bSab 		if (_is_so_matched(name, cp, path) == 0)
7307c478bd9Sstevel@tonic-gate 			return (lmp);
7317c478bd9Sstevel@tonic-gate 	}
7327c478bd9Sstevel@tonic-gate 
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;
773cce0e03bSab 	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) &&
7849aa23310Srie 		    ((FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE)) == 0))
7859aa23310Srie 			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 	 */
808cce0e03bSab 	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) {
856de777a60Sab 		Conv_reject_desc_buf_t rej_buf;
857de777a60Sab 
8587c478bd9Sstevel@tonic-gate 		/* LINTED */
8597c478bd9Sstevel@tonic-gate 		(void) snprintf(_reject, PATH_MAX,
860de777a60Sab 		    MSG_INTL(ldd_reject[rej->rej_type]),
861ba2be530Sab 		    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 == '/')
8827247f888Srie 		(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 */
942cce0e03bSab 		lmc = (Lm_cntl *)alist_item_by_offset(LIST(lmp)->lm_lists,
943cce0e03bSab 		    CNTL(lmp));
944cce0e03bSab 		(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;
98456deab07SRod Evans 	Aliste		idx;
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/*
9877c478bd9Sstevel@tonic-gate 	 * Determine if this filename is already on the alias list.
9887c478bd9Sstevel@tonic-gate 	 */
989cce0e03bSab 	for (APLIST_TRAVERSE(ALIAS(lmp), idx, cp)) {
990cce0e03bSab 		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;
1015cce0e03bSab 	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) {
1024aa736cbeSrie 		Lm_list		*lml;
1025aa736cbeSrie 		Listnode	*lnp;
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 		for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
10287c478bd9Sstevel@tonic-gate 			Rt_map	*nlmp = lml->lm_head;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 			if (nlmp && ((FLAGS(nlmp) &
10317c478bd9Sstevel@tonic-gate 			    (FLG_RT_AUDIT | FLG_RT_DELETE)) == FLG_RT_AUDIT) &&
10327c478bd9Sstevel@tonic-gate 			    (STDEV(nlmp) == status->st_dev) &&
10337c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) == status->st_ino))
10347c478bd9Sstevel@tonic-gate 				return (nlmp);
10357c478bd9Sstevel@tonic-gate 		}
103637ffaf83SRod Evans 		return (NULL);
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/*
10407c478bd9Sstevel@tonic-gate 	 * If the file has been found determine from the new files status
10417c478bd9Sstevel@tonic-gate 	 * information if this file is actually linked to one we already have
10427c478bd9Sstevel@tonic-gate 	 * mapped.  This catches symlink names not caught by is_so_loaded().
10437c478bd9Sstevel@tonic-gate 	 */
1044cce0e03bSab 	for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
10457c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp;
10467c478bd9Sstevel@tonic-gate 
1047cb511613SAli Bahrami 		for (nlmp = lmc->lc_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
10487c478bd9Sstevel@tonic-gate 			if ((FLAGS(nlmp) & FLG_RT_DELETE) ||
10497c478bd9Sstevel@tonic-gate 			    (FLAGS1(nlmp) & FL1_RT_LDDSTUB))
10507c478bd9Sstevel@tonic-gate 				continue;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 			if ((STDEV(nlmp) != status->st_dev) ||
10537c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) != status->st_ino))
10547c478bd9Sstevel@tonic-gate 				continue;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_VERBOSE) {
10577247f888Srie 				/* BEGIN CSTYLED */
10587c478bd9Sstevel@tonic-gate 				if (*name == '/')
10597c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
10607c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
10617c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
10627c478bd9Sstevel@tonic-gate 				else
10637c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
10647c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
10657c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
10667c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
10677247f888Srie 				/* END CSTYLED */
10687c478bd9Sstevel@tonic-gate 			}
10697c478bd9Sstevel@tonic-gate 			return (nlmp);
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 	}
107237ffaf83SRod Evans 	return (NULL);
10737c478bd9Sstevel@tonic-gate }
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate /*
10767c478bd9Sstevel@tonic-gate  * Generate any error messages indicating a file could not be found.  When
10777c478bd9Sstevel@tonic-gate  * preloading or auditing a secure application, it can be a little more helpful
10787c478bd9Sstevel@tonic-gate  * to indicate that a search of secure directories has failed, so adjust the
10797c478bd9Sstevel@tonic-gate  * messages accordingly.
10807c478bd9Sstevel@tonic-gate  */
10817c478bd9Sstevel@tonic-gate void
10827c478bd9Sstevel@tonic-gate file_notfound(Lm_list *lml, const char *name, Rt_map *clmp, uint_t flags,
108356deab07SRod Evans     Rej_desc *rej)
10847c478bd9Sstevel@tonic-gate {
10857c478bd9Sstevel@tonic-gate 	int	secure = 0;
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SECURE) &&
10887c478bd9Sstevel@tonic-gate 	    (flags & (FLG_RT_PRELOAD | FLG_RT_AUDIT)))
10897c478bd9Sstevel@tonic-gate 		secure++;
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
10927c478bd9Sstevel@tonic-gate 		/*
10937c478bd9Sstevel@tonic-gate 		 * Under ldd(1), auxiliary filtees that can't be loaded are
10947c478bd9Sstevel@tonic-gate 		 * ignored, unless verbose errors are requested.
10957c478bd9Sstevel@tonic-gate 		 */
10967c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) &&
10977c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_TRC_VERBOSE) == 0))
10987c478bd9Sstevel@tonic-gate 			return;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		if (secure)
11017c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
11027c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_SEC_NFOUND));
11037c478bd9Sstevel@tonic-gate 		else
11047c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
11057c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_FIL_NFOUND));
11067c478bd9Sstevel@tonic-gate 		return;
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
1110de777a60Sab 		Conv_reject_desc_buf_t rej_buf;
1111de777a60Sab 
11125aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(err_reject[rej->rej_type]),
11137c478bd9Sstevel@tonic-gate 		    rej->rej_name ? rej->rej_name : MSG_INTL(MSG_STR_UNKNOWN),
1114ba2be530Sab 		    conv_reject_desc(rej, &rej_buf, M_MACH));
11157c478bd9Sstevel@tonic-gate 		return;
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	if (secure)
11195aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SEC_OPEN), name);
11207c478bd9Sstevel@tonic-gate 	else
11215aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), name,
11227c478bd9Sstevel@tonic-gate 		    strerror(ENOENT));
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate static int
112656deab07SRod Evans file_open(int err, Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp,
112756deab07SRod Evans     Rej_desc *rej, int *in_nfavl)
11287c478bd9Sstevel@tonic-gate {
1129cb511613SAli Bahrami 	rtld_stat_t	status;
11307c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
11319aa23310Srie 	avl_index_t	nfavlwhere = 0;
113256deab07SRod Evans 	const char	*oname = fdp->fd_oname, *nname = fdp->fd_nname;
113356deab07SRod Evans 	uint_t		hash = sgs_str_hash(nname);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 
113656deab07SRod Evans 	if ((nname = stravl_insert(fdp->fd_nname, hash, 0, 0)) == NULL)
113756deab07SRod Evans 		return (0);
113856deab07SRod Evans 	fdp->fd_nname = nname;
113956deab07SRod Evans 
114056deab07SRod Evans 	if ((err == 0) && (fdp->fd_flags & FLG_FD_ALTER))
11415aefb655Srie 		DBG_CALL(Dbg_file_config_obj(lml, oname, 0, nname));
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	/*
11447c478bd9Sstevel@tonic-gate 	 * If we're dealing with a full pathname, determine whether this
11457c478bd9Sstevel@tonic-gate 	 * pathname is already known.  Other pathnames fall through to the
11467c478bd9Sstevel@tonic-gate 	 * dev/inode check, as even though the pathname may look the same as
11477c478bd9Sstevel@tonic-gate 	 * one previously used, the process may have changed directory.
11487c478bd9Sstevel@tonic-gate 	 */
11497c478bd9Sstevel@tonic-gate 	if ((err == 0) && (nname[0] == '/')) {
115056deab07SRod Evans 		if ((nlmp = fpavl_recorded(lml, nname, hash,
115156deab07SRod Evans 		    &(fdp->fd_avlwhere))) != NULL) {
115256deab07SRod Evans 			fdp->fd_lmp = nlmp;
11537c478bd9Sstevel@tonic-gate 			return (1);
11547c478bd9Sstevel@tonic-gate 		}
115556deab07SRod Evans 		if (nfavl_recorded(nname, hash, &nfavlwhere)) {
11569aa23310Srie 			/*
11579aa23310Srie 			 * For dlopen() and dlsym() fall backs, indicate that
11589aa23310Srie 			 * a registered not-found path has indicated that this
11599aa23310Srie 			 * object does not exist.  If this path has been
11609aa23310Srie 			 * constructed as part of expanding a HWCAP directory,
116156deab07SRod Evans 			 * this is a silent failure, where no rejection message
116256deab07SRod Evans 			 * is created.
11639aa23310Srie 			 */
11649aa23310Srie 			if (in_nfavl)
11659aa23310Srie 				(*in_nfavl)++;
11669aa23310Srie 			return (0);
11679aa23310Srie 		}
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
1170cb511613SAli Bahrami 	if ((err == 0) && ((rtld_stat(nname, &status)) != -1)) {
11717c478bd9Sstevel@tonic-gate 		char	path[PATH_MAX];
11727c478bd9Sstevel@tonic-gate 		int	fd, size, added;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 		/*
11757c478bd9Sstevel@tonic-gate 		 * If this path has been constructed as part of expanding a
11768521e5e6Srie 		 * HWCAP directory, ignore any subdirectories.  As this is a
117756deab07SRod Evans 		 * silent failure, no rejection message is created.  For any
117856deab07SRod Evans 		 * other reference that expands to a directory, fall through
11798af2c5b9Srie 		 * to construct a meaningful rejection message.
11807c478bd9Sstevel@tonic-gate 		 */
11817c478bd9Sstevel@tonic-gate 		if ((flags & FLG_RT_HWCAP) &&
118256deab07SRod Evans 		    ((status.st_mode & S_IFMT) == S_IFDIR))
118356deab07SRod Evans 			return (0);
118456deab07SRod Evans 
118556deab07SRod Evans 		/*
118656deab07SRod Evans 		 * If this is a directory (which can't be mmap()'ed) generate a
118756deab07SRod Evans 		 * precise error message.
118856deab07SRod Evans 		 */
118956deab07SRod Evans 		if ((status.st_mode & S_IFMT) == S_IFDIR) {
119056deab07SRod Evans 			rej->rej_name = nname;
119156deab07SRod Evans 			if (fdp->fd_flags & FLG_FD_ALTER)
119256deab07SRod Evans 				rej->rej_flags = FLG_REJ_ALTER;
119356deab07SRod Evans 			rej->rej_type = SGS_REJ_STR;
119456deab07SRod Evans 			rej->rej_str = strerror(EISDIR);
119556deab07SRod Evans 			DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
11967c478bd9Sstevel@tonic-gate 			return (0);
11978521e5e6Srie 		}
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		/*
12007c478bd9Sstevel@tonic-gate 		 * Resolve the filename and determine whether the resolved name
12017c478bd9Sstevel@tonic-gate 		 * is already known.  Typically, the previous fpavl_loaded()
12027c478bd9Sstevel@tonic-gate 		 * will have caught this, as both NAME() and PATHNAME() for a
120356deab07SRod Evans 		 * link-map are recorded in the FullPathNode AVL tree.  However,
12047c478bd9Sstevel@tonic-gate 		 * instances exist where a file can be replaced (loop-back
12057c478bd9Sstevel@tonic-gate 		 * mounts, bfu, etc.), and reference is made to the original
12067c478bd9Sstevel@tonic-gate 		 * file through a symbolic link.  By checking the pathname here,
12077c478bd9Sstevel@tonic-gate 		 * we don't fall through to the dev/inode check and conclude
12087c478bd9Sstevel@tonic-gate 		 * that a new file should be loaded.
12097c478bd9Sstevel@tonic-gate 		 */
121056deab07SRod Evans 		if ((nname[0] == '/') &&
12117c478bd9Sstevel@tonic-gate 		    ((size = resolvepath(nname, path, (PATH_MAX - 1))) > 0)) {
12127c478bd9Sstevel@tonic-gate 			path[size] = '\0';
12137c478bd9Sstevel@tonic-gate 
121456deab07SRod Evans 			fdp->fd_flags |= FLG_FD_RESOLVED;
121556deab07SRod Evans 
12167c478bd9Sstevel@tonic-gate 			if (strcmp(nname, path)) {
121724a6229eSrie 				if ((nlmp =
121856deab07SRod Evans 				    fpavl_recorded(lml, path, 0, 0)) != NULL) {
12197c478bd9Sstevel@tonic-gate 					added = 0;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 					if (append_alias(nlmp, nname,
12227c478bd9Sstevel@tonic-gate 					    &added) == 0)
12237c478bd9Sstevel@tonic-gate 						return (0);
12247247f888Srie 					/* BEGIN CSTYLED */
12257c478bd9Sstevel@tonic-gate 					if (added)
122610a4fa49Srie 					    DBG_CALL(Dbg_file_skip(LIST(clmp),
12275aefb655Srie 						NAME(nlmp), nname));
12287247f888Srie 					/* END CSTYLED */
122956deab07SRod Evans 					fdp->fd_lmp = nlmp;
12307c478bd9Sstevel@tonic-gate 					return (1);
12317c478bd9Sstevel@tonic-gate 				}
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 				/*
12347c478bd9Sstevel@tonic-gate 				 * If this pathname hasn't been loaded, save
12357c478bd9Sstevel@tonic-gate 				 * the resolved pathname so that it doesn't
12367c478bd9Sstevel@tonic-gate 				 * have to be recomputed as part of fullpath()
12377c478bd9Sstevel@tonic-gate 				 * processing.
12387c478bd9Sstevel@tonic-gate 				 */
123956deab07SRod Evans 				if ((fdp->fd_pname = stravl_insert(path, 0,
124056deab07SRod Evans 				    (size + 1), 0)) == NULL)
12417c478bd9Sstevel@tonic-gate 					return (0);
12427c478bd9Sstevel@tonic-gate 			}
12437c478bd9Sstevel@tonic-gate 		}
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 		if (nlmp = is_devinode_loaded(&status, lml, nname, flags)) {
1246c75e1b9dSrie 			if (flags & FLG_RT_AUDIT) {
1247c75e1b9dSrie 				/*
1248c75e1b9dSrie 				 * If we've been requested to load an auditor,
1249c75e1b9dSrie 				 * and an auditor of the same name already
1250c75e1b9dSrie 				 * exists, then the original auditor is used.
1251c75e1b9dSrie 				 */
1252c75e1b9dSrie 				DBG_CALL(Dbg_audit_skip(LIST(clmp),
1253c75e1b9dSrie 				    NAME(nlmp), LIST(nlmp)->lm_lmidstr));
1254c75e1b9dSrie 			} else {
1255c75e1b9dSrie 				/*
1256c75e1b9dSrie 				 * Otherwise, if an alternatively named file
1257c75e1b9dSrie 				 * has been found for the same dev/inode, add
125856deab07SRod Evans 				 * a new name alias.  Insert any alias full path
125956deab07SRod Evans 				 * name in the FullPathNode AVL tree.
1260c75e1b9dSrie 				 */
1261c75e1b9dSrie 				added = 0;
12627c478bd9Sstevel@tonic-gate 
1263c75e1b9dSrie 				if (append_alias(nlmp, nname, &added) == 0)
12647c478bd9Sstevel@tonic-gate 					return (0);
1265c75e1b9dSrie 				if (added) {
1266c75e1b9dSrie 					if ((nname[0] == '/') &&
1267c75e1b9dSrie 					    (fpavl_insert(lml, nlmp,
1268c75e1b9dSrie 					    nname, 0) == 0))
1269c75e1b9dSrie 						return (0);
1270c75e1b9dSrie 					DBG_CALL(Dbg_file_skip(LIST(clmp),
1271c75e1b9dSrie 					    NAME(nlmp), nname));
1272c75e1b9dSrie 				}
12737c478bd9Sstevel@tonic-gate 			}
1274c75e1b9dSrie 
1275c75e1b9dSrie 			/*
1276c75e1b9dSrie 			 * Record in the file descriptor the existing object
1277c75e1b9dSrie 			 * that satisfies this open request.
1278c75e1b9dSrie 			 */
127956deab07SRod Evans 			fdp->fd_lmp = nlmp;
12807c478bd9Sstevel@tonic-gate 			return (1);
12817c478bd9Sstevel@tonic-gate 		}
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 		if ((fd = open(nname, O_RDONLY, 0)) == -1) {
12847c478bd9Sstevel@tonic-gate 			/*
12857c478bd9Sstevel@tonic-gate 			 * As the file must exist for the previous stat() to
12867c478bd9Sstevel@tonic-gate 			 * have succeeded, record the error condition.
12877c478bd9Sstevel@tonic-gate 			 */
12887c478bd9Sstevel@tonic-gate 			rej->rej_type = SGS_REJ_STR;
12897c478bd9Sstevel@tonic-gate 			rej->rej_str = strerror(errno);
12907c478bd9Sstevel@tonic-gate 		} else {
129156deab07SRod Evans 			/*
129256deab07SRod Evans 			 * Map the object.  A successful return indicates that
129356deab07SRod Evans 			 * the object is appropriate for ld.so.1 processing.
129456deab07SRod Evans 			 */
129556deab07SRod Evans 			fdp->fd_ftp = map_obj(lml, fdp, status.st_size, nname,
129656deab07SRod Evans 			    fd, rej);
129756deab07SRod Evans 			(void) close(fd);
12987c478bd9Sstevel@tonic-gate 
129956deab07SRod Evans 			if (fdp->fd_ftp != NULL) {
130056deab07SRod Evans 				fdp->fd_dev = status.st_dev;
130156deab07SRod Evans 				fdp->fd_ino = status.st_ino;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 				/*
13047c478bd9Sstevel@tonic-gate 				 * Trace that this open has succeeded.
13057c478bd9Sstevel@tonic-gate 				 */
13067c478bd9Sstevel@tonic-gate 				if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
13077247f888Srie 					trace_so(clmp, 0, oname, nname,
130856deab07SRod Evans 					    (fdp->fd_flags & FLG_FD_ALTER), 0);
13097c478bd9Sstevel@tonic-gate 				}
13107c478bd9Sstevel@tonic-gate 				return (1);
13117c478bd9Sstevel@tonic-gate 			}
13127c478bd9Sstevel@tonic-gate 		}
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	} else if (errno != ENOENT) {
13157c478bd9Sstevel@tonic-gate 		/*
13167c478bd9Sstevel@tonic-gate 		 * If the open() failed for anything other than the file not
13177c478bd9Sstevel@tonic-gate 		 * existing, record the error condition.
13187c478bd9Sstevel@tonic-gate 		 */
13197c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
13207c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(errno);
13217c478bd9Sstevel@tonic-gate 	}
13227c478bd9Sstevel@tonic-gate 
13239aa23310Srie 	/*
13249aa23310Srie 	 * Regardless of error, duplicate and record any full path names that
13259aa23310Srie 	 * can't be used on the "not-found" AVL tree.
13269aa23310Srie 	 */
132756deab07SRod Evans 	if (nname[0] == '/')
132856deab07SRod Evans 		nfavl_insert(nname, nfavlwhere);
13299aa23310Srie 
13307c478bd9Sstevel@tonic-gate 	/*
13317c478bd9Sstevel@tonic-gate 	 * Indicate any rejection.
13327c478bd9Sstevel@tonic-gate 	 */
13337c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
13347c478bd9Sstevel@tonic-gate 		rej->rej_name = nname;
133556deab07SRod Evans 		if (fdp->fd_flags & FLG_FD_ALTER)
133656deab07SRod Evans 			rej->rej_flags = FLG_REJ_ALTER;
1337ba2be530Sab 		DBG_CALL(Dbg_file_rejected(lml, rej, M_MACH));
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 	return (0);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * Find a full pathname (it contains a "/").
13447c478bd9Sstevel@tonic-gate  */
13457c478bd9Sstevel@tonic-gate int
134656deab07SRod Evans find_path(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
134756deab07SRod Evans     int *in_nfavl)
13487c478bd9Sstevel@tonic-gate {
134956deab07SRod Evans 	const char	*oname = fdp->fd_oname;
135056deab07SRod Evans 	int		err = 0;
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	/*
13537c478bd9Sstevel@tonic-gate 	 * If directory configuration exists determine if this path is known.
13547c478bd9Sstevel@tonic-gate 	 */
13557c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DIRCFG) {
13567c478bd9Sstevel@tonic-gate 		Rtc_obj		*obj;
13577c478bd9Sstevel@tonic-gate 		const char	*aname;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 		if ((obj = elf_config_ent(oname, (Word)elf_hash(oname),
13607c478bd9Sstevel@tonic-gate 		    0, &aname)) != 0) {
13617c478bd9Sstevel@tonic-gate 			/*
13627c478bd9Sstevel@tonic-gate 			 * If the configuration file states that this path is a
13637c478bd9Sstevel@tonic-gate 			 * directory, or the path is explicitly defined as
13647c478bd9Sstevel@tonic-gate 			 * non-existent (ie. a unused platform specific
13657c478bd9Sstevel@tonic-gate 			 * library), then go no further.
13667c478bd9Sstevel@tonic-gate 			 */
13677c478bd9Sstevel@tonic-gate 			if (obj->co_flags & RTC_OBJ_DIRENT) {
13687c478bd9Sstevel@tonic-gate 				err = EISDIR;
13697c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags &
13707c478bd9Sstevel@tonic-gate 			    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) ==
13717c478bd9Sstevel@tonic-gate 			    RTC_OBJ_NOEXIST) {
13727c478bd9Sstevel@tonic-gate 				err = ENOENT;
13737c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags & RTC_OBJ_ALTER) &&
13747c478bd9Sstevel@tonic-gate 			    (rtld_flags & RT_FL_OBJALT) && (lml == &lml_main)) {
13757c478bd9Sstevel@tonic-gate 				int	ret;
13767c478bd9Sstevel@tonic-gate 
137756deab07SRod Evans 				fdp->fd_flags |= FLG_FD_ALTER;
137856deab07SRod Evans 				fdp->fd_nname = aname;
137956deab07SRod Evans 
13807c478bd9Sstevel@tonic-gate 				/*
13817c478bd9Sstevel@tonic-gate 				 * Attempt to open the alternative path.  If
13827c478bd9Sstevel@tonic-gate 				 * this fails, and the alternative is flagged
13837c478bd9Sstevel@tonic-gate 				 * as optional, fall through to open the
13847c478bd9Sstevel@tonic-gate 				 * original path.
13857c478bd9Sstevel@tonic-gate 				 */
13865aefb655Srie 				DBG_CALL(Dbg_libs_found(lml, aname,
13875aefb655Srie 				    FLG_FD_ALTER));
138856deab07SRod Evans 				ret = file_open(0, lml, clmp, flags, fdp,
138956deab07SRod Evans 				    rej, in_nfavl);
139056deab07SRod Evans 				if (ret || ((obj->co_flags &
139156deab07SRod Evans 				    RTC_OBJ_OPTINAL) == 0))
13927c478bd9Sstevel@tonic-gate 					return (ret);
13937c478bd9Sstevel@tonic-gate 
139456deab07SRod Evans 				fdp->fd_flags &= ~FLG_FD_ALTER;
13957c478bd9Sstevel@tonic-gate 			}
13967c478bd9Sstevel@tonic-gate 		}
13977c478bd9Sstevel@tonic-gate 	}
13985aefb655Srie 	DBG_CALL(Dbg_libs_found(lml, oname, 0));
139956deab07SRod Evans 	fdp->fd_nname = oname;
140056deab07SRod Evans 	return (file_open(err, lml, clmp, flags, fdp, rej, in_nfavl));
14017c478bd9Sstevel@tonic-gate }
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate /*
14047c478bd9Sstevel@tonic-gate  * Find a simple filename (it doesn't contain a "/").
14057c478bd9Sstevel@tonic-gate  */
14067c478bd9Sstevel@tonic-gate static int
140756deab07SRod Evans _find_file(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
140856deab07SRod Evans     Pdesc *pdp, int aflag, int *in_nfavl)
14097c478bd9Sstevel@tonic-gate {
141056deab07SRod Evans 	const char	*nname = fdp->fd_nname;
141156deab07SRod Evans 
14125aefb655Srie 	DBG_CALL(Dbg_libs_found(lml, nname, aflag));
14137c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_TRC_SEARCH) &&
14147c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
14157c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_PTH_TRYING), nname, aflag ?
14167c478bd9Sstevel@tonic-gate 		    MSG_INTL(MSG_LDD_FIL_ALTER) : MSG_ORIG(MSG_STR_EMPTY));
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	/*
14207c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're about
14217c478bd9Sstevel@tonic-gate 	 * to go search for.  The audit library may offer an alternative
14227c478bd9Sstevel@tonic-gate 	 * dependency, or indicate that this dependency should be ignored.
14237c478bd9Sstevel@tonic-gate 	 */
142456deab07SRod Evans 	if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
14259aa23310Srie 		char	*aname;
14267c478bd9Sstevel@tonic-gate 
14279aa23310Srie 		if ((aname = audit_objsearch(clmp, nname,
142856deab07SRod Evans 		    (pdp->pd_flags & LA_SER_MASK))) == NULL) {
14297247f888Srie 			DBG_CALL(Dbg_audit_terminate(lml, nname));
14307c478bd9Sstevel@tonic-gate 			return (0);
14317247f888Srie 		}
14327247f888Srie 
143356deab07SRod Evans 		if (aname != nname) {
143456deab07SRod Evans 			fdp->fd_flags &= ~FLG_FD_SLASH;
143556deab07SRod Evans 			fdp->fd_nname = aname;
143656deab07SRod Evans 		}
14377c478bd9Sstevel@tonic-gate 	}
143856deab07SRod Evans 	return (file_open(0, lml, clmp, flags, fdp, rej, in_nfavl));
14397c478bd9Sstevel@tonic-gate }
14407c478bd9Sstevel@tonic-gate 
1441390b98b5Srie static int
144256deab07SRod Evans find_file(Lm_list *lml, Rt_map *clmp, uint_t flags, Fdesc *fdp, Rej_desc *rej,
144356deab07SRod Evans     Pdesc *pdp, Word *strhash, int *in_nfavl)
14447c478bd9Sstevel@tonic-gate {
14457c478bd9Sstevel@tonic-gate 	static Rtc_obj	Obj = { 0 };
1446aa736cbeSrie 	Rtc_obj		*dobj;
144756deab07SRod Evans 	const char	*oname = fdp->fd_oname;
144856deab07SRod Evans 	size_t		olen = strlen(oname);
14497c478bd9Sstevel@tonic-gate 
145056deab07SRod Evans 	if (pdp->pd_pname == NULL)
14517c478bd9Sstevel@tonic-gate 		return (0);
145256deab07SRod Evans 	if (pdp->pd_info) {
145356deab07SRod Evans 		dobj = (Rtc_obj *)pdp->pd_info;
14547c478bd9Sstevel@tonic-gate 		if ((dobj->co_flags &
14557c478bd9Sstevel@tonic-gate 		    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
14567c478bd9Sstevel@tonic-gate 			return (0);
14577c478bd9Sstevel@tonic-gate 	} else
145856deab07SRod Evans 		dobj = NULL;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	/*
14617c478bd9Sstevel@tonic-gate 	 * If configuration information exists see if this directory/file
14627c478bd9Sstevel@tonic-gate 	 * combination exists.
14637c478bd9Sstevel@tonic-gate 	 */
14647c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_DIRCFG) &&
146556deab07SRod Evans 	    ((dobj == NULL) || (dobj->co_id != 0))) {
14667c478bd9Sstevel@tonic-gate 		Rtc_obj		*fobj;
146756deab07SRod Evans 		const char	*aname = NULL;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate 		/*
147056deab07SRod Evans 		 * If this object descriptor has not yet been searched for in
147156deab07SRod Evans 		 * the configuration file go find it.
14727c478bd9Sstevel@tonic-gate 		 */
147356deab07SRod Evans 		if (dobj == NULL) {
147456deab07SRod Evans 			dobj = elf_config_ent(pdp->pd_pname,
147556deab07SRod Evans 			    (Word)elf_hash(pdp->pd_pname), 0, 0);
147656deab07SRod Evans 			if (dobj == NULL)
14777c478bd9Sstevel@tonic-gate 				dobj = &Obj;
147856deab07SRod Evans 			pdp->pd_info = (void *)dobj;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 			if ((dobj->co_flags & (RTC_OBJ_NOEXIST |
14817c478bd9Sstevel@tonic-gate 			    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
14827c478bd9Sstevel@tonic-gate 				return (0);
14837c478bd9Sstevel@tonic-gate 		}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 		/*
14867c478bd9Sstevel@tonic-gate 		 * If we found a directory search for the file.
14877c478bd9Sstevel@tonic-gate 		 */
14887c478bd9Sstevel@tonic-gate 		if (dobj->co_id != 0) {
148956deab07SRod Evans 			if (*strhash == NULL)
149056deab07SRod Evans 				*strhash = (Word)elf_hash(oname);
149156deab07SRod Evans 			fobj = elf_config_ent(oname, *strhash,
149256deab07SRod Evans 			    dobj->co_id, &aname);
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 			/*
14957c478bd9Sstevel@tonic-gate 			 * If this object specifically does not exist, or the
14967c478bd9Sstevel@tonic-gate 			 * object can't be found in a know-all-entries
14977c478bd9Sstevel@tonic-gate 			 * directory, continue looking.  If the object does
14987c478bd9Sstevel@tonic-gate 			 * exist determine if an alternative object exists.
14997c478bd9Sstevel@tonic-gate 			 */
150056deab07SRod Evans 			if (fobj == NULL) {
15017c478bd9Sstevel@tonic-gate 				if (dobj->co_flags & RTC_OBJ_ALLENTS)
15027c478bd9Sstevel@tonic-gate 					return (0);
15037c478bd9Sstevel@tonic-gate 			} else {
15047c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & (RTC_OBJ_NOEXIST |
15057c478bd9Sstevel@tonic-gate 				    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
15067c478bd9Sstevel@tonic-gate 					return (0);
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & RTC_OBJ_ALTER) &&
15097c478bd9Sstevel@tonic-gate 				    (rtld_flags & RT_FL_OBJALT) &&
15107c478bd9Sstevel@tonic-gate 				    (lml == &lml_main)) {
15117c478bd9Sstevel@tonic-gate 					int	ret;
15127c478bd9Sstevel@tonic-gate 
151356deab07SRod Evans 					fdp->fd_flags |= FLG_FD_ALTER;
151456deab07SRod Evans 					fdp->fd_nname = aname;
151556deab07SRod Evans 
15167c478bd9Sstevel@tonic-gate 					/*
15177c478bd9Sstevel@tonic-gate 					 * Attempt to open the alternative path.
15187c478bd9Sstevel@tonic-gate 					 * If this fails, and the alternative is
15197c478bd9Sstevel@tonic-gate 					 * flagged as optional, fall through to
15207c478bd9Sstevel@tonic-gate 					 * open the original path.
15217c478bd9Sstevel@tonic-gate 					 */
152256deab07SRod Evans 					ret = _find_file(lml, clmp, flags, fdp,
152356deab07SRod Evans 					    rej, pdp, 1, in_nfavl);
15247c478bd9Sstevel@tonic-gate 					if (ret || ((fobj->co_flags &
15257c478bd9Sstevel@tonic-gate 					    RTC_OBJ_OPTINAL) == 0))
15267c478bd9Sstevel@tonic-gate 						return (ret);
15277c478bd9Sstevel@tonic-gate 
152856deab07SRod Evans 					fdp->fd_flags &= ~FLG_FD_ALTER;
15297c478bd9Sstevel@tonic-gate 				}
15307c478bd9Sstevel@tonic-gate 			}
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 	}
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate 	/*
15357c478bd9Sstevel@tonic-gate 	 * Protect ourselves from building an invalid pathname.
15367c478bd9Sstevel@tonic-gate 	 */
153756deab07SRod Evans 	if ((olen + pdp->pd_plen + 1) >= PATH_MAX) {
153856deab07SRod Evans 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), oname,
15397c478bd9Sstevel@tonic-gate 		    strerror(ENAMETOOLONG));
15407c478bd9Sstevel@tonic-gate 			return (0);
15417c478bd9Sstevel@tonic-gate 	}
154256deab07SRod Evans 	if ((fdp->fd_nname = (LM_GET_SO(clmp)(pdp->pd_pname, oname,
154356deab07SRod Evans 	    pdp->pd_plen, olen))) == NULL)
15447c478bd9Sstevel@tonic-gate 		return (0);
15457c478bd9Sstevel@tonic-gate 
154656deab07SRod Evans 	return (_find_file(lml, clmp, flags, fdp, rej, pdp, 0, in_nfavl));
15477c478bd9Sstevel@tonic-gate }
15487c478bd9Sstevel@tonic-gate 
154956deab07SRod Evans static Fct	*Vector[] = {
155056deab07SRod Evans 	&elf_fct,
155156deab07SRod Evans #ifdef	A_OUT
155256deab07SRod Evans 	&aout_fct,
155356deab07SRod Evans #endif
155456deab07SRod Evans 	0
155556deab07SRod Evans };
155656deab07SRod Evans 
15577c478bd9Sstevel@tonic-gate /*
155856deab07SRod Evans  * Remap the first page of a file to provide a better diagnostic as to why
155956deab07SRod Evans  * an mmapobj(2) operation on this file failed.  Sadly, mmapobj(), and all
156056deab07SRod Evans  * system calls for that matter, only pass back a generic failure in errno.
156156deab07SRod Evans  * Hopefully one day this will be improved, but in the mean time we repeat
156256deab07SRod Evans  * the kernels ELF verification to try and provide more detailed information.
15637c478bd9Sstevel@tonic-gate  */
156456deab07SRod Evans static int
156556deab07SRod Evans map_fail(Fdesc *fdp, size_t fsize, const char *name, int fd, Rej_desc *rej)
15667c478bd9Sstevel@tonic-gate {
156756deab07SRod Evans 	caddr_t	addr;
156856deab07SRod Evans 	int	vnum;
156956deab07SRod Evans 	size_t	size;
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	/*
157256deab07SRod Evans 	 * Use the original file size to determine what to map, and catch the
157356deab07SRod Evans 	 * obvious error of a zero sized file.
15747c478bd9Sstevel@tonic-gate 	 */
157556deab07SRod Evans 	if (fsize == 0) {
157656deab07SRod Evans 		rej->rej_type = SGS_REJ_UNKFILE;
157756deab07SRod Evans 		return (1);
157856deab07SRod Evans 	} else if (fsize < syspagsz)
157956deab07SRod Evans 		size = fsize;
158056deab07SRod Evans 	else
158156deab07SRod Evans 		size = syspagsz;
158256deab07SRod Evans 
158356deab07SRod Evans 	if ((addr = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED)
158456deab07SRod Evans 		return (0);
158556deab07SRod Evans 
158656deab07SRod Evans 	rej->rej_type = 0;
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	/*
158956deab07SRod Evans 	 * Validate the file against each supported file type.  Should a
159056deab07SRod Evans 	 * characteristic of the file be found invalid for this platform, a
159156deab07SRod Evans 	 * rejection message will have been recorded.
15927c478bd9Sstevel@tonic-gate 	 */
159356deab07SRod Evans 	for (vnum = 0; Vector[vnum]; vnum++) {
159456deab07SRod Evans 		if (((Vector[vnum]->fct_verify_file)(addr, size,
159556deab07SRod Evans 		    fdp, name, rej) == 0) && rej->rej_type)
159656deab07SRod Evans 			break;
159756deab07SRod Evans 	}
15987c478bd9Sstevel@tonic-gate 
159956deab07SRod Evans 	/*
160056deab07SRod Evans 	 * If no rejection message has been recorded, then this is simply an
160156deab07SRod Evans 	 * unknown file type.
160256deab07SRod Evans 	 */
160356deab07SRod Evans 	if (rej->rej_type == 0)
160456deab07SRod Evans 		rej->rej_type = SGS_REJ_UNKFILE;
160556deab07SRod Evans 
160656deab07SRod Evans 	(void) munmap(addr, size);
160756deab07SRod Evans 	return (1);
160856deab07SRod Evans }
160956deab07SRod Evans 
161056deab07SRod Evans /*
161156deab07SRod Evans  * Unmap a file.
161256deab07SRod Evans  */
161356deab07SRod Evans void
161456deab07SRod Evans unmap_obj(mmapobj_result_t *mpp, uint_t mapnum)
161556deab07SRod Evans {
161656deab07SRod Evans 	uint_t	num;
161756deab07SRod Evans 
161856deab07SRod Evans 	for (num = 0; num < mapnum; num++) {
161956deab07SRod Evans 		/* LINTED */
162056deab07SRod Evans 		(void) munmap((void *)(uintptr_t)mpp[num].mr_addr,
162156deab07SRod Evans 		    mpp[num].mr_msize);
162256deab07SRod Evans 	}
162356deab07SRod Evans }
162456deab07SRod Evans 
162556deab07SRod Evans /*
162656deab07SRod Evans  * Map a file.
162756deab07SRod Evans  */
162856deab07SRod Evans Fct *
162956deab07SRod Evans map_obj(Lm_list *lml, Fdesc *fdp, size_t fsize, const char *name, int fd,
163056deab07SRod Evans     Rej_desc *rej)
163156deab07SRod Evans {
163256deab07SRod Evans 	static mmapobj_result_t	*smpp = NULL;
163356deab07SRod Evans 	static uint_t		smapnum;
163456deab07SRod Evans 	mmapobj_result_t	*mpp;
163556deab07SRod Evans 	uint_t			mnum, mapnum, mflags;
163656deab07SRod Evans 	void			*padding;
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 	/*
163956deab07SRod Evans 	 * Allocate an initial mapping array.  The initial size should be large
164056deab07SRod Evans 	 * enough to handle the normal ELF objects we come across.
16417c478bd9Sstevel@tonic-gate 	 */
164256deab07SRod Evans 	if (smpp == NULL) {
164356deab07SRod Evans 		smpp = malloc(sizeof (mmapobj_result_t) * MMAPFD_NUM);
164456deab07SRod Evans 		if (smpp == NULL)
164556deab07SRod Evans 			return (NULL);
164656deab07SRod Evans 		smapnum = MMAPFD_NUM;
164756deab07SRod Evans 	}
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/*
165056deab07SRod Evans 	 * If object padding is required, set the necessary flags.
165156deab07SRod Evans 	 */
165256deab07SRod Evans 	if (r_debug.rtd_objpad) {
165356deab07SRod Evans 		mflags = MMOBJ_INTERPRET | MMOBJ_PADDING;
165456deab07SRod Evans 		padding = &r_debug.rtd_objpad;
165556deab07SRod Evans 	} else {
165656deab07SRod Evans 		mflags = MMOBJ_INTERPRET;
165756deab07SRod Evans 		padding = NULL;
165856deab07SRod Evans 	}
165956deab07SRod Evans 
166056deab07SRod Evans 	/*
166156deab07SRod Evans 	 * Map the file.  If the number of mappings required by this file
166256deab07SRod Evans 	 * exceeds the present mapping structure, an error indicating the
166356deab07SRod Evans 	 * return data is too big is returned.  Bail on any other error.
166456deab07SRod Evans 	 */
166556deab07SRod Evans 	mapnum = smapnum;
166656deab07SRod Evans 	if (mmapobj(fd, mflags, smpp, &mapnum, padding) == -1) {
166756deab07SRod Evans 		if (errno != E2BIG) {
166856deab07SRod Evans 			int	err = errno;
166956deab07SRod Evans 
167056deab07SRod Evans 			/*
167156deab07SRod Evans 			 * An unsupported error indicates that there's something
167256deab07SRod Evans 			 * incompatible with this ELF file, and the process that
167356deab07SRod Evans 			 * is already running.  Map the first page of the file
167456deab07SRod Evans 			 * and see if we can generate a better error message.
167556deab07SRod Evans 			 */
167656deab07SRod Evans 			if ((errno == ENOTSUP) && map_fail(fdp, fsize, name,
167756deab07SRod Evans 			    fd, rej))
167856deab07SRod Evans 				return (NULL);
167956deab07SRod Evans 
168056deab07SRod Evans 			rej->rej_type = SGS_REJ_STR;
168156deab07SRod Evans 			rej->rej_str = strerror(err);
168237ffaf83SRod Evans 			return (NULL);
16837c478bd9Sstevel@tonic-gate 		}
168456deab07SRod Evans 
168556deab07SRod Evans 		/*
168656deab07SRod Evans 		 * The mapping requirement exceeds the present mapping
168756deab07SRod Evans 		 * structure, however the number of mapping required is
168856deab07SRod Evans 		 * available in the mapping number.
168956deab07SRod Evans 		 */
169056deab07SRod Evans 		free((void *)smpp);
169156deab07SRod Evans 		if ((smpp = malloc(sizeof (mmapobj_result_t) * mapnum)) == NULL)
169256deab07SRod Evans 			return (NULL);
169356deab07SRod Evans 		smapnum = mapnum;
169456deab07SRod Evans 
169556deab07SRod Evans 		/*
169656deab07SRod Evans 		 * With the appropriate mapping structure, try the mapping
169756deab07SRod Evans 		 * request again.
169856deab07SRod Evans 		 */
169956deab07SRod Evans 		if (mmapobj(fd, mflags, smpp, &mapnum, padding) == -1) {
170056deab07SRod Evans 			rej->rej_type = SGS_REJ_STR;
170156deab07SRod Evans 			rej->rej_str = strerror(errno);
170237ffaf83SRod Evans 			return (NULL);
17037c478bd9Sstevel@tonic-gate 		}
170456deab07SRod Evans 	}
170556deab07SRod Evans 	ASSERT(mapnum != 0);
170656deab07SRod Evans 
170756deab07SRod Evans 	/*
170856deab07SRod Evans 	 * Traverse the mappings in search of a file type ld.so.1 can process.
170956deab07SRod Evans 	 * If the file type is verified as one ld.so.1 can process, retain the
171056deab07SRod Evans 	 * mapping information, and the number of mappings this object uses,
171156deab07SRod Evans 	 * and clear the static mapping pointer for the next map_obj() use of
171256deab07SRod Evans 	 * mmapobj().
171356deab07SRod Evans 	 */
171456deab07SRod Evans 	DBG_CALL(Dbg_file_mmapobj(lml, name, smpp, mapnum));
171556deab07SRod Evans 
171656deab07SRod Evans 	for (mnum = 0, mpp = smpp; mnum < mapnum; mnum++, mpp++) {
171756deab07SRod Evans 		uint_t	flags = (mpp->mr_flags & MR_TYPE_MASK);
171856deab07SRod Evans 		Fct	*fptr = NULL;
171956deab07SRod Evans 
172056deab07SRod Evans 		if (flags == MR_HDR_ELF) {
172156deab07SRod Evans 			fptr = elf_verify((mpp->mr_addr + mpp->mr_offset),
172256deab07SRod Evans 			    mpp->mr_fsize, fdp, name, rej);
172356deab07SRod Evans 		}
172456deab07SRod Evans #ifdef	A_OUT
172556deab07SRod Evans 		if (flags == MR_HDR_AOUT) {
172656deab07SRod Evans 			fptr = aout_verify((mpp->mr_addr + mpp->mr_offset),
172756deab07SRod Evans 			    mpp->mr_fsize, fdp, name, rej);
172856deab07SRod Evans 		}
172956deab07SRod Evans #endif
173056deab07SRod Evans 		if (fptr) {
173156deab07SRod Evans 			fdp->fd_mapn = mapnum;
173256deab07SRod Evans 			fdp->fd_mapp = smpp;
173356deab07SRod Evans 
173456deab07SRod Evans 			smpp = NULL;
173556deab07SRod Evans 
173656deab07SRod Evans 			return (fptr);
173756deab07SRod Evans 		}
173856deab07SRod Evans 	}
173956deab07SRod Evans 
174056deab07SRod Evans 	/*
174156deab07SRod Evans 	 * If the mapped file is inappropriate, indicate that the file type is
174256deab07SRod Evans 	 * unknown, and free the mapping.
174356deab07SRod Evans 	 */
174456deab07SRod Evans 	if (rej->rej_type == 0)
174556deab07SRod Evans 		rej->rej_type = SGS_REJ_UNKFILE;
174656deab07SRod Evans 	unmap_obj(smpp, mapnum);
174756deab07SRod Evans 
174856deab07SRod Evans 	return (NULL);
174956deab07SRod Evans }
175056deab07SRod Evans 
175156deab07SRod Evans /*
175256deab07SRod Evans  * A unique file has been opened.  Create a link-map to represent it, and
175356deab07SRod Evans  * process the various names by which it can be referenced.
175456deab07SRod Evans  */
175556deab07SRod Evans Rt_map *
175656deab07SRod Evans load_file(Lm_list *lml, Aliste lmco, Fdesc *fdp, int *in_nfavl)
175756deab07SRod Evans {
175856deab07SRod Evans 	mmapobj_result_t	*fpmpp = NULL, *fmpp = NULL, *lpmpp, *lmpp;
175956deab07SRod Evans 	mmapobj_result_t	*hmpp, *mpp, *ompp = fdp->fd_mapp;
176056deab07SRod Evans 	uint_t			mnum, omapnum = fdp->fd_mapn;
176156deab07SRod Evans 	const char		*nname = fdp->fd_nname;
176256deab07SRod Evans 	Rt_map			*nlmp;
176356deab07SRod Evans 	Ehdr			*ehdr = NULL;
176456deab07SRod Evans 
176556deab07SRod Evans 	/*
176656deab07SRod Evans 	 * Traverse the mappings for the input file to capture generic mapping
176756deab07SRod Evans 	 * information, and create a link-map to represent the file.
176856deab07SRod Evans 	 */
176956deab07SRod Evans 	for (mnum = 0, mpp = ompp; mnum < omapnum; mnum++, mpp++) {
177056deab07SRod Evans 		uint_t	flags = (mpp->mr_flags & MR_TYPE_MASK);
17713dbfc803SRod Evans 
17723dbfc803SRod Evans 		/*
177356deab07SRod Evans 		 * Keep track of the first and last mappings that may include
177456deab07SRod Evans 		 * padding.
177556deab07SRod Evans 		 */
177656deab07SRod Evans 		if (fpmpp == NULL)
177756deab07SRod Evans 			fpmpp = mpp;
177856deab07SRod Evans 		lpmpp = mpp;
177956deab07SRod Evans 
178056deab07SRod Evans 		/*
178156deab07SRod Evans 		 * Keep track of the first and last mappings that do not include
178256deab07SRod Evans 		 * padding.
178356deab07SRod Evans 		 */
178456deab07SRod Evans 		if (flags != MR_PADDING) {
178556deab07SRod Evans 			if (fmpp == NULL)
178656deab07SRod Evans 				fmpp = mpp;
178756deab07SRod Evans 			lmpp = mpp;
17883dbfc803SRod Evans 		}
178956deab07SRod Evans 		if (flags == MR_HDR_ELF) {
179056deab07SRod Evans 			/* LINTED */
179156deab07SRod Evans 			ehdr = (Ehdr *)(mpp->mr_addr + mpp->mr_offset);
179256deab07SRod Evans 			hmpp = mpp;
179356deab07SRod Evans 		} else if (flags == MR_HDR_AOUT)
179456deab07SRod Evans 			hmpp = mpp;
179556deab07SRod Evans 	}
179656deab07SRod Evans 
179756deab07SRod Evans 	/*
179856deab07SRod Evans 	 * The only ELF files we can handle are ET_EXEC, ET_DYN, and ET_REL.
179956deab07SRod Evans 	 *
180056deab07SRod Evans 	 * ET_REL must be processed by ld(1) to create an in-memory ET_DYN.
180156deab07SRod Evans 	 * The initial processing carried out by elf_obj_file() creates a
180256deab07SRod Evans 	 * temporary link-map, that acts as a place holder, until the objects
180356deab07SRod Evans 	 * processing is finished with elf_obj_fini().
180456deab07SRod Evans 	 */
180556deab07SRod Evans 	if (ehdr && (ehdr->e_type == ET_REL)) {
180656deab07SRod Evans 		if ((nlmp = elf_obj_file(lml, lmco, nname, hmpp, ompp,
180756deab07SRod Evans 		    omapnum)) == NULL)
180856deab07SRod Evans 			return (nlmp);
180956deab07SRod Evans 	} else {
181056deab07SRod Evans 		Addr	addr;
181156deab07SRod Evans 		size_t	msize;
181256deab07SRod Evans 
181356deab07SRod Evans 		/*
181456deab07SRod Evans 		 * The size of the total reservation, and the padding range,
181556deab07SRod Evans 		 * are a historic artifact required by debuggers.  Although
181656deab07SRod Evans 		 * these values express the range of the associated mappings,
181756deab07SRod Evans 		 * there can be holes between segments (in which small objects
181856deab07SRod Evans 		 * could be mapped).  Anyone who needs to verify offsets
181956deab07SRod Evans 		 * against segments should analyze all the object mappings,
182056deab07SRod Evans 		 * rather than relying on these address ranges.
182156deab07SRod Evans 		 */
182256deab07SRod Evans 		addr = (Addr)(hmpp->mr_addr + hmpp->mr_offset);
182356deab07SRod Evans 		msize = lmpp->mr_addr + lmpp->mr_msize - fmpp->mr_addr;
182456deab07SRod Evans 
182556deab07SRod Evans 		if ((nlmp = ((fdp->fd_ftp)->fct_new_lmp)(lml, lmco, fdp, addr,
182656deab07SRod Evans 		    msize, NULL, in_nfavl)) == NULL)
182756deab07SRod Evans 			return (NULL);
182856deab07SRod Evans 
182956deab07SRod Evans 		/*
183056deab07SRod Evans 		 * Save generic mapping information.
183156deab07SRod Evans 		 */
183256deab07SRod Evans 		MMAPS(nlmp) = ompp;
183356deab07SRod Evans 		MMAPCNT(nlmp) = omapnum;
183456deab07SRod Evans 		PADSTART(nlmp) = (ulong_t)fpmpp->mr_addr;
183556deab07SRod Evans 		PADIMLEN(nlmp) = lpmpp->mr_addr + lpmpp->mr_msize -
183656deab07SRod Evans 		    fpmpp->mr_addr;
183756deab07SRod Evans 	}
183856deab07SRod Evans 
183956deab07SRod Evans 	/*
184056deab07SRod Evans 	 * Save the dev/inode information for later comparisons, and identify
184156deab07SRod Evans 	 * this as a new object.
184256deab07SRod Evans 	 */
184356deab07SRod Evans 	STDEV(nlmp) = fdp->fd_dev;
184456deab07SRod Evans 	STINO(nlmp) = fdp->fd_ino;
184556deab07SRod Evans 	FLAGS(nlmp) |= FLG_RT_NEWLOAD;
184656deab07SRod Evans 
184756deab07SRod Evans 	/*
184856deab07SRod Evans 	 * If this is ELF relocatable object, we're done for now.
184956deab07SRod Evans 	 */
185056deab07SRod Evans 	if (ehdr && (ehdr->e_type == ET_REL))
185156deab07SRod Evans 		return (nlmp);
185256deab07SRod Evans 
185356deab07SRod Evans 	/*
185456deab07SRod Evans 	 * Insert the names of this link-map into the FullPathNode AVL tree.
185556deab07SRod Evans 	 * Save both the NAME() and PATHNAME() if the names differ.
185656deab07SRod Evans 	 */
185756deab07SRod Evans 	(void) fullpath(nlmp, fdp);
185856deab07SRod Evans 
185956deab07SRod Evans 	if ((NAME(nlmp)[0] == '/') && (fpavl_insert(lml, nlmp, NAME(nlmp),
186056deab07SRod Evans 	    fdp->fd_avlwhere) == 0)) {
186156deab07SRod Evans 		remove_so(lml, nlmp);
186256deab07SRod Evans 		return (NULL);
186356deab07SRod Evans 	}
186456deab07SRod Evans 	if (((NAME(nlmp)[0] != '/') || (NAME(nlmp) != PATHNAME(nlmp))) &&
186556deab07SRod Evans 	    (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)) {
186656deab07SRod Evans 		remove_so(lml, nlmp);
186756deab07SRod Evans 		return (NULL);
186856deab07SRod Evans 	}
186956deab07SRod Evans 
187056deab07SRod Evans 	/*
187156deab07SRod Evans 	 * If this is a secure application, record any full path name directory
187256deab07SRod Evans 	 * in which this dependency has been found.  This directory can be
187356deab07SRod Evans 	 * deemed safe (as we've already found a dependency here).  This
187456deab07SRod Evans 	 * recording provides a fall-back should another objects $ORIGIN
187556deab07SRod Evans 	 * definition expands to this directory, an expansion that would
187656deab07SRod Evans 	 * ordinarily be deemed insecure.
187756deab07SRod Evans 	 */
187856deab07SRod Evans 	if (rtld_flags & RT_FL_SECURE) {
187956deab07SRod Evans 		if (NAME(nlmp)[0] == '/')
188056deab07SRod Evans 			spavl_insert(NAME(nlmp));
188156deab07SRod Evans 		if ((NAME(nlmp) != PATHNAME(nlmp)) &&
188256deab07SRod Evans 		    (PATHNAME(nlmp)[0] == '/'))
188356deab07SRod Evans 			spavl_insert(PATHNAME(nlmp));
18847c478bd9Sstevel@tonic-gate 	}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 	/*
18877c478bd9Sstevel@tonic-gate 	 * If we're processing an alternative object reset the original name
18887c478bd9Sstevel@tonic-gate 	 * for possible $ORIGIN processing.
18897c478bd9Sstevel@tonic-gate 	 */
189056deab07SRod Evans 	if (fdp->fd_flags & FLG_FD_ALTER) {
189156deab07SRod Evans 		const char	*odir, *ndir;
18927c478bd9Sstevel@tonic-gate 		size_t		olen;
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) |= FLG_RT_ALTER;
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		/*
18977c478bd9Sstevel@tonic-gate 		 * If we were given a pathname containing a slash then the
18987c478bd9Sstevel@tonic-gate 		 * original name is still in oname.  Otherwise the original
18997c478bd9Sstevel@tonic-gate 		 * directory is in dir->p_name (which is all we need for
19007c478bd9Sstevel@tonic-gate 		 * $ORIGIN).
19017c478bd9Sstevel@tonic-gate 		 */
190256deab07SRod Evans 		if (fdp->fd_flags & FLG_FD_SLASH) {
19037c478bd9Sstevel@tonic-gate 			char	*ofil;
19047c478bd9Sstevel@tonic-gate 
190556deab07SRod Evans 			odir = fdp->fd_oname;
190656deab07SRod Evans 			ofil = strrchr(fdp->fd_oname, '/');
19077c478bd9Sstevel@tonic-gate 			olen = ofil - odir + 1;
19087c478bd9Sstevel@tonic-gate 		} else {
190956deab07SRod Evans 			odir = fdp->fd_odir;
19107c478bd9Sstevel@tonic-gate 			olen = strlen(odir) + 1;
19117c478bd9Sstevel@tonic-gate 		}
191256deab07SRod Evans 		if ((ndir = stravl_insert(odir, 0, olen, 1)) == NULL) {
19137c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
191437ffaf83SRod Evans 			return (NULL);
19157c478bd9Sstevel@tonic-gate 		}
19167c478bd9Sstevel@tonic-gate 		ORIGNAME(nlmp) = ndir;
191756deab07SRod Evans 		DIRSZ(nlmp) = --olen;
19187c478bd9Sstevel@tonic-gate 	}
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	return (nlmp);
19217c478bd9Sstevel@tonic-gate }
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate /*
19247c478bd9Sstevel@tonic-gate  * This function loads the named file and returns a pointer to its link map.
19257c478bd9Sstevel@tonic-gate  * It is assumed that the caller has already checked that the file is not
19267c478bd9Sstevel@tonic-gate  * already loaded before calling this function (refer is_so_loaded()).
19277c478bd9Sstevel@tonic-gate  * Find and open the file, map it into memory, add it to the end of the list
19287c478bd9Sstevel@tonic-gate  * of link maps and return a pointer to the new link map.  Return 0 on error.
19297c478bd9Sstevel@tonic-gate  */
19307c478bd9Sstevel@tonic-gate static Rt_map *
193156deab07SRod Evans load_so(Lm_list *lml, Aliste lmco, Rt_map *clmp, uint_t flags,
193256deab07SRod Evans     Fdesc *fdp, Rej_desc *rej, int *in_nfavl)
19337c478bd9Sstevel@tonic-gate {
193456deab07SRod Evans 	const char	*oname = fdp->fd_oname;
193556deab07SRod Evans 	Pdesc		*pdp;
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	/*
193856deab07SRod Evans 	 * If this path name hasn't already been identified as containing a
193956deab07SRod Evans 	 * slash, check the path name.  Most paths have been constructed
194056deab07SRod Evans 	 * through appending a file name to a search path, and/or have been
194156deab07SRod Evans 	 * inspected by expand(), and thus have a slash.  However, we can
194256deab07SRod Evans 	 * receive path names via auditors or configuration files, and thus
194356deab07SRod Evans 	 * an evaluation here catches these instances.
19447c478bd9Sstevel@tonic-gate 	 */
194556deab07SRod Evans 	if ((fdp->fd_flags & FLG_FD_SLASH) == 0) {
19467c478bd9Sstevel@tonic-gate 		const char	*str;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 		for (str = oname; *str; str++) {
19497c478bd9Sstevel@tonic-gate 			if (*str == '/') {
195056deab07SRod Evans 				fdp->fd_flags |= FLG_FD_SLASH;
19517c478bd9Sstevel@tonic-gate 				break;
19527c478bd9Sstevel@tonic-gate 			}
19537c478bd9Sstevel@tonic-gate 		}
19547c478bd9Sstevel@tonic-gate 	}
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	/*
19577c478bd9Sstevel@tonic-gate 	 * If we are passed a 'null' link-map this means that this is the first
19587c478bd9Sstevel@tonic-gate 	 * object to be loaded on this link-map list.  In that case we set the
19597c478bd9Sstevel@tonic-gate 	 * link-map to ld.so.1's link-map.
19607c478bd9Sstevel@tonic-gate 	 *
19617c478bd9Sstevel@tonic-gate 	 * This link-map is referenced to determine what lookup rules to use
19627c478bd9Sstevel@tonic-gate 	 * when searching for files.  By using ld.so.1's we are defaulting to
19637c478bd9Sstevel@tonic-gate 	 * ELF look-up rules.
19647c478bd9Sstevel@tonic-gate 	 *
19657c478bd9Sstevel@tonic-gate 	 * Note: This case happens when loading the first object onto
19667c478bd9Sstevel@tonic-gate 	 *	 the plt_tracing link-map.
19677c478bd9Sstevel@tonic-gate 	 */
19687c478bd9Sstevel@tonic-gate 	if (clmp == 0)
19697c478bd9Sstevel@tonic-gate 		clmp = lml_rtld.lm_head;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	/*
19727c478bd9Sstevel@tonic-gate 	 * If this path resulted from a $HWCAP specification, then the best
19737c478bd9Sstevel@tonic-gate 	 * hardware capability object has already been establish, and is
19747c478bd9Sstevel@tonic-gate 	 * available in the calling file descriptor.  Perform some minor book-
19757c478bd9Sstevel@tonic-gate 	 * keeping so that we can fall through into common code.
19767c478bd9Sstevel@tonic-gate 	 */
19777c478bd9Sstevel@tonic-gate 	if (flags & FLG_RT_HWCAP) {
19787c478bd9Sstevel@tonic-gate 		/*
1979fb1354edSrie 		 * If this object is already loaded, we're done.
19807c478bd9Sstevel@tonic-gate 		 */
198156deab07SRod Evans 		if (fdp->fd_lmp)
198256deab07SRod Evans 			return (fdp->fd_lmp);
19837c478bd9Sstevel@tonic-gate 
1984fb1354edSrie 		/*
1985fb1354edSrie 		 * Obtain the avl index for this object.
1986fb1354edSrie 		 */
198756deab07SRod Evans 		(void) fpavl_recorded(lml, fdp->fd_nname, 0,
198856deab07SRod Evans 		    &(fdp->fd_avlwhere));
19894464de07SAli Bahrami 
199056deab07SRod Evans 	} else if (fdp->fd_flags & FLG_FD_SLASH) {
19917c478bd9Sstevel@tonic-gate 		Rej_desc	_rej = { 0 };
19927c478bd9Sstevel@tonic-gate 
199356deab07SRod Evans 		if (find_path(lml, clmp, flags, fdp, &_rej, in_nfavl) == 0) {
199431fdd7caSab 			rejection_inherit(rej, &_rej);
199537ffaf83SRod Evans 			return (NULL);
19967c478bd9Sstevel@tonic-gate 		}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 		/*
19997c478bd9Sstevel@tonic-gate 		 * If this object is already loaded, we're done.
20007c478bd9Sstevel@tonic-gate 		 */
200156deab07SRod Evans 		if (fdp->fd_lmp)
200256deab07SRod Evans 			return (fdp->fd_lmp);
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	} else {
20057c478bd9Sstevel@tonic-gate 		/*
20067c478bd9Sstevel@tonic-gate 		 * No '/' - for each directory on list, make a pathname using
20077c478bd9Sstevel@tonic-gate 		 * that directory and filename and try to open that file.
20087c478bd9Sstevel@tonic-gate 		 */
200956deab07SRod Evans 		Spath_desc	sd = { search_rules, NULL, 0 };
20107c478bd9Sstevel@tonic-gate 		Word		strhash = 0;
201156deab07SRod Evans 		int		found = 0;
201256deab07SRod Evans 
20135aefb655Srie 		DBG_CALL(Dbg_libs_find(lml, oname));
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 		/*
201656deab07SRod Evans 		 * Traverse the search path lists, creating full pathnames and
201756deab07SRod Evans 		 * attempt to load each path.
20187c478bd9Sstevel@tonic-gate 		 */
201956deab07SRod Evans 		for (pdp = get_next_dir(&sd, clmp, flags); pdp;
202056deab07SRod Evans 		    pdp = get_next_dir(&sd, clmp, flags)) {
20217c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
202256deab07SRod Evans 			Fdesc		fd = { 0 };
20237c478bd9Sstevel@tonic-gate 
20249aa23310Srie 			/*
20259aa23310Srie 			 * Under debugging, duplicate path name entries are
20269aa23310Srie 			 * tagged but remain part of the search path list so
20279aa23310Srie 			 * that they can be diagnosed under "unused" processing.
20289aa23310Srie 			 * Skip these entries, as this path would have already
20299aa23310Srie 			 * been attempted.
20309aa23310Srie 			 */
203156deab07SRod Evans 			if (pdp->pd_flags & PD_FLG_DUPLICAT)
20329aa23310Srie 				continue;
20339aa23310Srie 
203456deab07SRod Evans 			fd = *fdp;
203556deab07SRod Evans 
20367c478bd9Sstevel@tonic-gate 			/*
20377c478bd9Sstevel@tonic-gate 			 * Try and locate this file.  Make sure to clean up
20387c478bd9Sstevel@tonic-gate 			 * any rejection information should the file have
20397c478bd9Sstevel@tonic-gate 			 * been found, but not appropriate.
20407c478bd9Sstevel@tonic-gate 			 */
204156deab07SRod Evans 			if (find_file(lml, clmp, flags, &fd, &_rej, pdp,
204256deab07SRod Evans 			    &strhash, in_nfavl) == 0) {
204331fdd7caSab 				rejection_inherit(rej, &_rej);
20447c478bd9Sstevel@tonic-gate 				continue;
20457c478bd9Sstevel@tonic-gate 			}
20467c478bd9Sstevel@tonic-gate 
20479aa23310Srie 			/*
20489aa23310Srie 			 * Indicate that this search path has been used.  If
20499aa23310Srie 			 * this is an LD_LIBRARY_PATH setting, ignore any use
20509aa23310Srie 			 * by ld.so.1 itself.
20519aa23310Srie 			 */
205256deab07SRod Evans 			if (((pdp->pd_flags & LA_SER_LIBPATH) == 0) ||
20539aa23310Srie 			    ((lml->lm_flags & LML_FLG_RTLDLM) == 0))
205456deab07SRod Evans 				pdp->pd_flags |= PD_FLG_USED;
20559aa23310Srie 
20567c478bd9Sstevel@tonic-gate 			/*
20577c478bd9Sstevel@tonic-gate 			 * If this object is already loaded, we're done.
20587c478bd9Sstevel@tonic-gate 			 */
205956deab07SRod Evans 			*fdp = fd;
206056deab07SRod Evans 			if (fdp->fd_lmp)
206156deab07SRod Evans 				return (fdp->fd_lmp);
20627c478bd9Sstevel@tonic-gate 
206356deab07SRod Evans 			fdp->fd_odir = pdp->pd_pname;
206456deab07SRod Evans 			found = 1;
20657c478bd9Sstevel@tonic-gate 			break;
20667c478bd9Sstevel@tonic-gate 		}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 		/*
20697c478bd9Sstevel@tonic-gate 		 * If the file couldn't be loaded, do another comparison of
20707c478bd9Sstevel@tonic-gate 		 * loaded files using just the basename.  This catches folks
20717c478bd9Sstevel@tonic-gate 		 * who may have loaded multiple full pathname files (possibly
20727c478bd9Sstevel@tonic-gate 		 * from setxid applications) to satisfy dependency relationships
20737c478bd9Sstevel@tonic-gate 		 * (i.e., a file might have a dependency on foo.so.1 which has
20747c478bd9Sstevel@tonic-gate 		 * already been opened using its full pathname).
20757c478bd9Sstevel@tonic-gate 		 */
207656deab07SRod Evans 		if (found == 0)
20779aa23310Srie 			return (is_so_loaded(lml, oname, in_nfavl));
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate 	/*
20817c478bd9Sstevel@tonic-gate 	 * Finish mapping the file and return the link-map descriptor.  Note,
20827c478bd9Sstevel@tonic-gate 	 * if this request originated from a HWCAP request, re-establish the
20837c478bd9Sstevel@tonic-gate 	 * fdesc information.  For single paged objects, such as filters, the
20847c478bd9Sstevel@tonic-gate 	 * original mapping may have been sufficient to capture the file, thus
20857c478bd9Sstevel@tonic-gate 	 * this mapping needs to be reset to insure it doesn't mistakenly get
20867c478bd9Sstevel@tonic-gate 	 * unmapped as part of HWCAP cleanup.
20877c478bd9Sstevel@tonic-gate 	 */
208856deab07SRod Evans 	return (load_file(lml, lmco, fdp, in_nfavl));
20897c478bd9Sstevel@tonic-gate }
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate /*
209256deab07SRod Evans  * Trace an attempt to load an object, and seed the originating name.
20937c478bd9Sstevel@tonic-gate  */
209456deab07SRod Evans const char *
209556deab07SRod Evans load_trace(Lm_list *lml, Pdesc *pdp, Rt_map *clmp, Fdesc *fdp)
20967c478bd9Sstevel@tonic-gate {
209756deab07SRod Evans 	const char	*name = pdp->pd_pname;
20987247f888Srie 
20997c478bd9Sstevel@tonic-gate 	/*
21007c478bd9Sstevel@tonic-gate 	 * First generate any ldd(1) diagnostics.
21017c478bd9Sstevel@tonic-gate 	 */
21027c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
21037c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
21047c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name, NAME(clmp));
21057c478bd9Sstevel@tonic-gate 
210656deab07SRod Evans 	/*
210756deab07SRod Evans 	 * Propagate any knowledge of a slash within the path name.
210856deab07SRod Evans 	 */
210956deab07SRod Evans 	if (pdp->pd_flags & PD_FLG_PNSLASH)
211056deab07SRod Evans 		fdp->fd_flags |= FLG_FD_SLASH;
211156deab07SRod Evans 
21127c478bd9Sstevel@tonic-gate 	/*
21137c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're
21147c478bd9Sstevel@tonic-gate 	 * about to go search for.
21157c478bd9Sstevel@tonic-gate 	 */
211656deab07SRod Evans 	if (((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_ACTIVITY) &&
21177c478bd9Sstevel@tonic-gate 	    (lml == LIST(clmp)))
21187c478bd9Sstevel@tonic-gate 		audit_activity(clmp, LA_ACT_ADD);
21197c478bd9Sstevel@tonic-gate 
212056deab07SRod Evans 	if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
212156deab07SRod Evans 		char	*aname;
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 		/*
21247c478bd9Sstevel@tonic-gate 		 * The auditor can indicate that this object should be ignored.
21257c478bd9Sstevel@tonic-gate 		 */
212656deab07SRod Evans 		if ((aname =
212756deab07SRod Evans 		    audit_objsearch(clmp, name, LA_SER_ORIG)) == NULL) {
21287247f888Srie 			DBG_CALL(Dbg_audit_terminate(lml, name));
212956deab07SRod Evans 			return (NULL);
21307c478bd9Sstevel@tonic-gate 		}
21317c478bd9Sstevel@tonic-gate 
21327247f888Srie 		if (name != aname) {
213356deab07SRod Evans 			fdp->fd_flags &= ~FLG_FD_SLASH;
213456deab07SRod Evans 			name = aname;
21357c478bd9Sstevel@tonic-gate 		}
21367c478bd9Sstevel@tonic-gate 	}
213756deab07SRod Evans 	fdp->fd_oname = name;
213856deab07SRod Evans 	return (name);
21397c478bd9Sstevel@tonic-gate }
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate /*
21427c478bd9Sstevel@tonic-gate  * Having loaded an object and created a link-map to describe it, finish
21437c478bd9Sstevel@tonic-gate  * processing this stage, including verifying any versioning requirements,
21447c478bd9Sstevel@tonic-gate  * updating the objects mode, creating a handle if necessary, and adding this
21457c478bd9Sstevel@tonic-gate  * object to existing handles if required.
21467c478bd9Sstevel@tonic-gate  */
2147390b98b5Srie static int
2148fb1354edSrie load_finish(Lm_list *lml, const char *name, Rt_map *clmp, int nmode,
2149fb1354edSrie     uint_t flags, Grp_hdl **hdl, Rt_map *nlmp)
21507c478bd9Sstevel@tonic-gate {
2151cce0e03bSab 	Aliste		idx;
2152cce0e03bSab 	Grp_hdl		*ghp;
21537c478bd9Sstevel@tonic-gate 	int		promote;
21547c478bd9Sstevel@tonic-gate 
21557c478bd9Sstevel@tonic-gate 	/*
21567c478bd9Sstevel@tonic-gate 	 * If this dependency is associated with a required version insure that
21577c478bd9Sstevel@tonic-gate 	 * the version is present in the loaded file.
21587c478bd9Sstevel@tonic-gate 	 */
215956deab07SRod Evans 	if (((rtld_flags & RT_FL_NOVERSION) == 0) && THIS_IS_ELF(clmp) &&
216056deab07SRod Evans 	    VERNEED(clmp) && (elf_verify_vers(name, clmp, nlmp) == 0))
21617c478bd9Sstevel@tonic-gate 		return (0);
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	/*
21647c478bd9Sstevel@tonic-gate 	 * If this object has indicated that it should be isolated as a group
21657c478bd9Sstevel@tonic-gate 	 * (DT_FLAGS_1 contains DF_1_GROUP - object was built with -B group),
21667c478bd9Sstevel@tonic-gate 	 * or if the callers direct bindings indicate it should be isolated as
21677c478bd9Sstevel@tonic-gate 	 * a group (DYNINFO flags contains FLG_DI_GROUP - dependency followed
21687c478bd9Sstevel@tonic-gate 	 * -zgroupperm), establish the appropriate mode.
21697c478bd9Sstevel@tonic-gate 	 *
21707c478bd9Sstevel@tonic-gate 	 * The intent of an object defining itself as a group is to isolate the
21717c478bd9Sstevel@tonic-gate 	 * relocation of the group within its own members, however, unless
21727c478bd9Sstevel@tonic-gate 	 * opened through dlopen(), in which case we assume dlsym() will be used
21737c478bd9Sstevel@tonic-gate 	 * to located symbols in the new object, we still need to associate it
21747c478bd9Sstevel@tonic-gate 	 * with the caller for it to be bound with.  This is equivalent to a
21757c478bd9Sstevel@tonic-gate 	 * dlopen(RTLD_GROUP) and dlsym() using the returned handle.
21767c478bd9Sstevel@tonic-gate 	 */
21777c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) | flags) & FLG_RT_SETGROUP) {
21787c478bd9Sstevel@tonic-gate 		nmode &= ~RTLD_WORLD;
21797c478bd9Sstevel@tonic-gate 		nmode |= RTLD_GROUP;
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 		/*
21827c478bd9Sstevel@tonic-gate 		 * If the object wasn't explicitly dlopen()'ed associate it with
21837c478bd9Sstevel@tonic-gate 		 * the parent.
21847c478bd9Sstevel@tonic-gate 		 */
21859a411307Srie 		if ((flags & FLG_RT_HANDLE) == 0)
21867c478bd9Sstevel@tonic-gate 			nmode |= RTLD_PARENT;
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	/*
21907c478bd9Sstevel@tonic-gate 	 * Establish new mode and flags.
21917c478bd9Sstevel@tonic-gate 	 */
219256deab07SRod Evans 	promote = update_mode(nlmp, MODE(nlmp), nmode);
21937c478bd9Sstevel@tonic-gate 	FLAGS(nlmp) |= flags;
21947c478bd9Sstevel@tonic-gate 
21952926dd2eSrie 	/*
21962926dd2eSrie 	 * If this is a global object, ensure the associated link-map list can
21972926dd2eSrie 	 * be rescanned for global, lazy dependencies.
21982926dd2eSrie 	 */
21992926dd2eSrie 	if (MODE(nlmp) & RTLD_GLOBAL)
22002926dd2eSrie 		LIST(nlmp)->lm_flags &= ~LML_FLG_NOPENDGLBLAZY;
22012926dd2eSrie 
22027c478bd9Sstevel@tonic-gate 	/*
22037c478bd9Sstevel@tonic-gate 	 * If we've been asked to establish a handle create one for this object.
22047c478bd9Sstevel@tonic-gate 	 * Or, if this object has already been analyzed, but this reference
22057c478bd9Sstevel@tonic-gate 	 * requires that the mode of the object be promoted, also create a
22067c478bd9Sstevel@tonic-gate 	 * handle to propagate the new modes to all this objects dependencies.
22077c478bd9Sstevel@tonic-gate 	 */
22087c478bd9Sstevel@tonic-gate 	if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) || (promote &&
22097c478bd9Sstevel@tonic-gate 	    (FLAGS(nlmp) & FLG_RT_ANALYZED))) {
22108af2c5b9Srie 		uint_t	oflags, hflags = 0, cdflags;
22117c478bd9Sstevel@tonic-gate 
22128af2c5b9Srie 		/*
22138af2c5b9Srie 		 * Establish any flags for the handle (Grp_hdl).
22148af2c5b9Srie 		 *
22158af2c5b9Srie 		 *  .	Use of the RTLD_FIRST flag indicates that only the first
22168af2c5b9Srie 		 *	dependency on the handle (the new object) can be used
22178af2c5b9Srie 		 *	to satisfy dlsym() requests.
22188af2c5b9Srie 		 */
22197c478bd9Sstevel@tonic-gate 		if (nmode & RTLD_FIRST)
22208af2c5b9Srie 			hflags = GPH_FIRST;
22218af2c5b9Srie 
22228af2c5b9Srie 		/*
22238af2c5b9Srie 		 * Establish the flags for this callers dependency descriptor
22248af2c5b9Srie 		 * (Grp_desc).
22258af2c5b9Srie 		 *
22268af2c5b9Srie 		 *  .	The creation of a handle associated a descriptor for the
22278af2c5b9Srie 		 *	new object and descriptor for the parent (caller).
22288af2c5b9Srie 		 *	Typically, the handle is created for dlopen() or for
22298af2c5b9Srie 		 *	filtering.  A handle may also be created to promote
22308af2c5b9Srie 		 *	the callers modes (RTLD_NOW) to the new object.  In this
22318af2c5b9Srie 		 *	latter case, the handle/descriptor are torn down once
22328af2c5b9Srie 		 *	the mode propagation has occurred.
22338af2c5b9Srie 		 *
22348af2c5b9Srie 		 *  .	Use of the RTLD_PARENT flag indicates that the parent
22358af2c5b9Srie 		 *	can be relocated against.
22368af2c5b9Srie 		 */
22378af2c5b9Srie 		if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) == 0)
22388af2c5b9Srie 			cdflags = GPD_PROMOTE;
22398af2c5b9Srie 		else
22408af2c5b9Srie 			cdflags = GPD_PARENT;
22418af2c5b9Srie 		if (nmode & RTLD_PARENT)
22428af2c5b9Srie 			cdflags |= GPD_RELOC;
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 		/*
22457c478bd9Sstevel@tonic-gate 		 * Now that a handle is being created, remove this state from
22467c478bd9Sstevel@tonic-gate 		 * the object so that it doesn't mistakenly get inherited by
22477c478bd9Sstevel@tonic-gate 		 * a dependency.
22487c478bd9Sstevel@tonic-gate 		 */
22497c478bd9Sstevel@tonic-gate 		oflags = FLAGS(nlmp);
22507c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) &= ~FLG_RT_HANDLE;
22517c478bd9Sstevel@tonic-gate 
22528af2c5b9Srie 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
22538af2c5b9Srie 		if ((ghp = hdl_create(lml, nlmp, clmp, hflags,
22548af2c5b9Srie 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), cdflags)) == 0)
22557c478bd9Sstevel@tonic-gate 			return (0);
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 		/*
22587c478bd9Sstevel@tonic-gate 		 * Add any dependencies that are already loaded, to the handle.
22597c478bd9Sstevel@tonic-gate 		 */
226002ca3e02Srie 		if (hdl_initialize(ghp, nlmp, nmode, promote) == 0)
22617c478bd9Sstevel@tonic-gate 			return (0);
22627c478bd9Sstevel@tonic-gate 
22637c478bd9Sstevel@tonic-gate 		if (hdl)
22647c478bd9Sstevel@tonic-gate 			*hdl = ghp;
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 		/*
22678af2c5b9Srie 		 * If we were asked to create a handle, we're done.
22687c478bd9Sstevel@tonic-gate 		 */
22697c478bd9Sstevel@tonic-gate 		if ((oflags | flags) & FLG_RT_HANDLE)
22707c478bd9Sstevel@tonic-gate 			return (1);
22717c478bd9Sstevel@tonic-gate 
22728af2c5b9Srie 		/*
22738af2c5b9Srie 		 * If the handle was created to promote modes from the parent
22748af2c5b9Srie 		 * (caller) to the new object, then this relationship needs to
22758af2c5b9Srie 		 * be removed to ensure the handle doesn't prevent the new
22768af2c5b9Srie 		 * objects from being deleted if required.  If the parent is
22778af2c5b9Srie 		 * the only dependency on the handle, then the handle can be
22788af2c5b9Srie 		 * completely removed.  However, the handle may have already
22798af2c5b9Srie 		 * existed, in which case only the parent descriptor can be
22808af2c5b9Srie 		 * deleted from the handle, or at least the GPD_PROMOTE flag
22818af2c5b9Srie 		 * removed from the descriptor.
22828af2c5b9Srie 		 *
22838af2c5b9Srie 		 * Fall through to carry out any group processing.
22848af2c5b9Srie 		 */
22858af2c5b9Srie 		free_hdl(ghp, clmp, GPD_PROMOTE);
22867c478bd9Sstevel@tonic-gate 	}
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate 	/*
22897c478bd9Sstevel@tonic-gate 	 * If the caller isn't part of a group we're done.
22907c478bd9Sstevel@tonic-gate 	 */
2291cce0e03bSab 	if (GROUPS(clmp) == NULL)
22927c478bd9Sstevel@tonic-gate 		return (1);
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 	/*
22957c478bd9Sstevel@tonic-gate 	 * Determine if our caller is already associated with a handle, if so
22967c478bd9Sstevel@tonic-gate 	 * we need to add this object to any handles that already exist.
22977c478bd9Sstevel@tonic-gate 	 * Traverse the list of groups our caller is a member of and add this
22987c478bd9Sstevel@tonic-gate 	 * new link-map to those groups.
22997c478bd9Sstevel@tonic-gate 	 */
23008af2c5b9Srie 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
2301cce0e03bSab 	for (APLIST_TRAVERSE(GROUPS(clmp), idx, ghp)) {
2302cce0e03bSab 		Aliste		idx1;
23037c478bd9Sstevel@tonic-gate 		Grp_desc	*gdp;
23047c478bd9Sstevel@tonic-gate 		int		exist;
2305cce0e03bSab 		Rt_map		*dlmp1;
2306cce0e03bSab 		APlist		*lmalp = NULL;
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 		/*
23097c478bd9Sstevel@tonic-gate 		 * If the caller doesn't indicate that its dependencies should
23107c478bd9Sstevel@tonic-gate 		 * be added to a handle, ignore it.  This case identifies a
23117c478bd9Sstevel@tonic-gate 		 * parent of a dlopen(RTLD_PARENT) request.
23127c478bd9Sstevel@tonic-gate 		 */
2313cce0e03bSab 		for (ALIST_TRAVERSE(ghp->gh_depends, idx1, gdp)) {
23147c478bd9Sstevel@tonic-gate 			if (gdp->gd_depend == clmp)
23157c478bd9Sstevel@tonic-gate 				break;
23167c478bd9Sstevel@tonic-gate 		}
23177c478bd9Sstevel@tonic-gate 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
23187c478bd9Sstevel@tonic-gate 			continue;
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 		if ((exist = hdl_add(ghp, nlmp,
2321efb9e8b8Srie 		    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0)
23227c478bd9Sstevel@tonic-gate 			return (0);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 		/*
23257c478bd9Sstevel@tonic-gate 		 * If this member already exists then its dependencies will
23267c478bd9Sstevel@tonic-gate 		 * have already been processed.
23277c478bd9Sstevel@tonic-gate 		 */
23287c478bd9Sstevel@tonic-gate 		if (exist == ALE_EXISTS)
23297c478bd9Sstevel@tonic-gate 			continue;
23307c478bd9Sstevel@tonic-gate 
23317c478bd9Sstevel@tonic-gate 		/*
23327c478bd9Sstevel@tonic-gate 		 * If the object we've added has just been opened, it will not
23337c478bd9Sstevel@tonic-gate 		 * yet have been processed for its dependencies, these will be
23347c478bd9Sstevel@tonic-gate 		 * added on later calls to load_one().  If it doesn't have any
23357c478bd9Sstevel@tonic-gate 		 * dependencies we're also done.
23367c478bd9Sstevel@tonic-gate 		 */
23377c478bd9Sstevel@tonic-gate 		if (((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0) ||
2338cce0e03bSab 		    (DEPENDS(nlmp) == NULL))
23397c478bd9Sstevel@tonic-gate 			continue;
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 		/*
23427c478bd9Sstevel@tonic-gate 		 * Otherwise, this object exists and has dependencies, so add
23437c478bd9Sstevel@tonic-gate 		 * all of its dependencies to the handle were operating on.
23447c478bd9Sstevel@tonic-gate 		 */
234556deab07SRod Evans 		if (aplist_append(&lmalp, nlmp, AL_CNT_DEPCLCT) == NULL)
23467c478bd9Sstevel@tonic-gate 			return (0);
23477c478bd9Sstevel@tonic-gate 
2348cce0e03bSab 		for (APLIST_TRAVERSE(lmalp, idx1, dlmp1)) {
2349cce0e03bSab 			Aliste		idx2;
2350cce0e03bSab 			Bnd_desc 	*bdp;
23517c478bd9Sstevel@tonic-gate 
23527c478bd9Sstevel@tonic-gate 			/*
23537c478bd9Sstevel@tonic-gate 			 * Add any dependencies of this dependency to the
23547c478bd9Sstevel@tonic-gate 			 * dynamic dependency list so they can be further
23557c478bd9Sstevel@tonic-gate 			 * processed.
23567c478bd9Sstevel@tonic-gate 			 */
2357cce0e03bSab 			for (APLIST_TRAVERSE(DEPENDS(dlmp1), idx2, bdp)) {
2358aa736cbeSrie 				Rt_map	*dlmp2 = bdp->b_depend;
23597c478bd9Sstevel@tonic-gate 
23607c478bd9Sstevel@tonic-gate 				if ((bdp->b_flags & BND_NEEDED) == 0)
23617c478bd9Sstevel@tonic-gate 					continue;
23627c478bd9Sstevel@tonic-gate 
2363cce0e03bSab 				if (aplist_test(&lmalp, dlmp2,
23647c478bd9Sstevel@tonic-gate 				    AL_CNT_DEPCLCT) == 0) {
23657c478bd9Sstevel@tonic-gate 					free(lmalp);
23667c478bd9Sstevel@tonic-gate 					return (0);
23677c478bd9Sstevel@tonic-gate 				}
23687c478bd9Sstevel@tonic-gate 			}
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 			if (nlmp == dlmp1)
23717c478bd9Sstevel@tonic-gate 				continue;
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 			if ((exist = hdl_add(ghp, dlmp1,
237456deab07SRod Evans 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS))) == 0) {
237556deab07SRod Evans 				free(lmalp);
237656deab07SRod Evans 				return (0);
23777c478bd9Sstevel@tonic-gate 			}
237856deab07SRod Evans 
237956deab07SRod Evans 			if (exist == ALE_CREATE)
238056deab07SRod Evans 				(void) update_mode(dlmp1, MODE(dlmp1), nmode);
23817c478bd9Sstevel@tonic-gate 		}
23827c478bd9Sstevel@tonic-gate 		free(lmalp);
23837c478bd9Sstevel@tonic-gate 	}
23847c478bd9Sstevel@tonic-gate 	return (1);
23857c478bd9Sstevel@tonic-gate }
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate /*
23887c478bd9Sstevel@tonic-gate  * The central routine for loading shared objects.  Insures ldd() diagnostics,
23897c478bd9Sstevel@tonic-gate  * handles and any other related additions are all done in one place.
23907c478bd9Sstevel@tonic-gate  */
239156deab07SRod Evans Rt_map *
239256deab07SRod Evans load_path(Lm_list *lml, Aliste lmco, Rt_map *clmp, int nmode, uint_t flags,
239356deab07SRod Evans     Grp_hdl **hdl, Fdesc *fdp, Rej_desc *rej, int *in_nfavl)
23947c478bd9Sstevel@tonic-gate {
239556deab07SRod Evans 	const char	*name = fdp->fd_oname;
23967247f888Srie 	Rt_map		*nlmp;
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	if ((nmode & RTLD_NOLOAD) == 0) {
23997c478bd9Sstevel@tonic-gate 		/*
24007c478bd9Sstevel@tonic-gate 		 * If this isn't a noload request attempt to load the file.
24017c478bd9Sstevel@tonic-gate 		 */
240256deab07SRod Evans 		if ((nlmp = load_so(lml, lmco, clmp, flags, fdp, rej,
240356deab07SRod Evans 		    in_nfavl)) == NULL)
240437ffaf83SRod Evans 			return (NULL);
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate 		/*
24077c478bd9Sstevel@tonic-gate 		 * If we've loaded a library which identifies itself as not
24087c478bd9Sstevel@tonic-gate 		 * being dlopen()'able catch it here.  Let non-dlopen()'able
24097c478bd9Sstevel@tonic-gate 		 * objects through under RTLD_CONFGEN as they're only being
24107c478bd9Sstevel@tonic-gate 		 * mapped to be dldump()'ed.
24117c478bd9Sstevel@tonic-gate 		 */
24127c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) && ((FLAGS(nlmp) &
24137c478bd9Sstevel@tonic-gate 		    (FLG_RT_NOOPEN | FLG_RT_RELOCED)) == FLG_RT_NOOPEN) &&
24147c478bd9Sstevel@tonic-gate 		    ((nmode & RTLD_CONFGEN) == 0)) {
24157c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
24187c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
24197c478bd9Sstevel@tonic-gate 			_rej.rej_str = MSG_INTL(MSG_GEN_NOOPEN);
2420ba2be530Sab 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
242131fdd7caSab 			rejection_inherit(rej, &_rej);
24227c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
242337ffaf83SRod Evans 			return (NULL);
24247c478bd9Sstevel@tonic-gate 		}
24257c478bd9Sstevel@tonic-gate 	} else {
24267c478bd9Sstevel@tonic-gate 		/*
24277c478bd9Sstevel@tonic-gate 		 * If it's a NOLOAD request - check to see if the object
24287c478bd9Sstevel@tonic-gate 		 * has already been loaded.
24297c478bd9Sstevel@tonic-gate 		 */
24307c478bd9Sstevel@tonic-gate 		/* LINTED */
24319aa23310Srie 		if (nlmp = is_so_loaded(lml, name, in_nfavl)) {
24327c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
24337c478bd9Sstevel@tonic-gate 			    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
24347c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name,
24357247f888Srie 				    NAME(clmp));
24367247f888Srie 				/* BEGIN CSTYLED */
24377c478bd9Sstevel@tonic-gate 				if (*name == '/')
24387c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
24397c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
24407c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
24417c478bd9Sstevel@tonic-gate 				else
24427c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
24437c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
24447c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
24457c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
24467247f888Srie 				/* END CSTYLED */
24477c478bd9Sstevel@tonic-gate 			}
24487c478bd9Sstevel@tonic-gate 		} else {
24497c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
24527c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
24537c478bd9Sstevel@tonic-gate 			_rej.rej_str = strerror(ENOENT);
2454ba2be530Sab 			DBG_CALL(Dbg_file_rejected(lml, &_rej, M_MACH));
245531fdd7caSab 			rejection_inherit(rej, &_rej);
245637ffaf83SRod Evans 			return (NULL);
24577c478bd9Sstevel@tonic-gate 		}
24587c478bd9Sstevel@tonic-gate 	}
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	/*
24617c478bd9Sstevel@tonic-gate 	 * Finish processing this loaded object.
24627c478bd9Sstevel@tonic-gate 	 */
2463390b98b5Srie 	if (load_finish(lml, name, clmp, nmode, flags, hdl, nlmp) == 0) {
2464390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2465390b98b5Srie 
2466390b98b5Srie 		/*
2467390b98b5Srie 		 * If this object has already been analyzed, then it is in use,
2468390b98b5Srie 		 * so even though this operation has failed, it should not be
2469390b98b5Srie 		 * torn down.
2470390b98b5Srie 		 */
2471390b98b5Srie 		if ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)
2472390b98b5Srie 			remove_so(lml, nlmp);
247337ffaf83SRod Evans 		return (NULL);
2474390b98b5Srie 	}
24757c478bd9Sstevel@tonic-gate 
24767c478bd9Sstevel@tonic-gate 	/*
2477390b98b5Srie 	 * If this object is new, and we're being audited, tell the audit
2478390b98b5Srie 	 * library of the file we've just opened.  Note, if the new link-map
2479390b98b5Srie 	 * requires local auditing of its dependencies we also register its
2480390b98b5Srie 	 * opening.
24817c478bd9Sstevel@tonic-gate 	 */
2482390b98b5Srie 	if (FLAGS(nlmp) & FLG_RT_NEWLOAD) {
2483390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2484390b98b5Srie 
248556deab07SRod Evans 		if (((lml->lm_tflags | AFLAGS(clmp) | AFLAGS(nlmp)) &
2486390b98b5Srie 		    LML_TFLG_AUD_MASK) && (((lml->lm_flags |
2487390b98b5Srie 		    LIST(clmp)->lm_flags) & LML_FLG_NOAUDIT) == 0)) {
2488390b98b5Srie 			if (audit_objopen(clmp, nlmp) == 0) {
2489390b98b5Srie 				remove_so(lml, nlmp);
249037ffaf83SRod Evans 				return (NULL);
2491390b98b5Srie 			}
2492390b98b5Srie 		}
2493390b98b5Srie 	}
2494390b98b5Srie 	return (nlmp);
24957c478bd9Sstevel@tonic-gate }
24967c478bd9Sstevel@tonic-gate 
24977c478bd9Sstevel@tonic-gate /*
24987c478bd9Sstevel@tonic-gate  * Load one object from a possible list of objects.  Typically, for requests
24997c478bd9Sstevel@tonic-gate  * such as NEEDED's, only one object is specified.  However, this object could
25007c478bd9Sstevel@tonic-gate  * be specified using $ISALIST or $HWCAP, in which case only the first object
25017c478bd9Sstevel@tonic-gate  * that can be loaded is used (ie. the best).
25027c478bd9Sstevel@tonic-gate  */
25037c478bd9Sstevel@tonic-gate Rt_map *
250456deab07SRod Evans load_one(Lm_list *lml, Aliste lmco, Alist *palp, Rt_map *clmp, int mode,
25059aa23310Srie     uint_t flags, Grp_hdl **hdl, int *in_nfavl)
25067c478bd9Sstevel@tonic-gate {
25077c478bd9Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
250856deab07SRod Evans 	Aliste		idx;
250956deab07SRod Evans 	Pdesc   	*pdp;
25107c478bd9Sstevel@tonic-gate 	const char	*name;
25117c478bd9Sstevel@tonic-gate 
251256deab07SRod Evans 	for (ALIST_TRAVERSE(palp, idx, pdp)) {
251356deab07SRod Evans 		Rt_map	*lmp = NULL;
25147247f888Srie 
25157c478bd9Sstevel@tonic-gate 		/*
25167c478bd9Sstevel@tonic-gate 		 * A Hardware capabilities requirement can itself expand into
25177c478bd9Sstevel@tonic-gate 		 * a number of candidates.
25187c478bd9Sstevel@tonic-gate 		 */
251956deab07SRod Evans 		if (pdp->pd_flags & PD_TKN_HWCAP) {
252056deab07SRod Evans 			lmp = load_hwcap(lml, lmco, pdp->pd_pname, clmp,
252156deab07SRod Evans 			    mode, (flags | FLG_RT_HWCAP), hdl, &rej, in_nfavl);
25227c478bd9Sstevel@tonic-gate 		} else {
252356deab07SRod Evans 			Fdesc	fd = { 0 };
252456deab07SRod Evans 
252556deab07SRod Evans 			/*
252656deab07SRod Evans 			 * Trace the inspection of this file, determine any
252756deab07SRod Evans 			 * auditor substitution, and seed the file descriptor
252856deab07SRod Evans 			 * with the originating name.
252956deab07SRod Evans 			 */
253056deab07SRod Evans 			if (load_trace(lml, pdp, clmp, &fd) == NULL)
253156deab07SRod Evans 				continue;
253256deab07SRod Evans 
253356deab07SRod Evans 			/*
253456deab07SRod Evans 			 * Locate and load the file.
253556deab07SRod Evans 			 */
253656deab07SRod Evans 			lmp = load_path(lml, lmco, clmp, mode, flags, hdl, &fd,
253756deab07SRod Evans 			    &rej, in_nfavl);
25387c478bd9Sstevel@tonic-gate 		}
253956deab07SRod Evans 		if (lmp)
254056deab07SRod Evans 			return (lmp);
25417c478bd9Sstevel@tonic-gate 	}
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate 	/*
254456deab07SRod Evans 	 * If no objects can be found, use the first path name from the Alist
254556deab07SRod Evans 	 * to provide a diagnostic.  If this pathname originated from an
254656deab07SRod Evans 	 * expanded token, use the original name for any diagnostic output.
25477c478bd9Sstevel@tonic-gate 	 */
254856deab07SRod Evans 	pdp = alist_item(palp, 0);
254956deab07SRod Evans 
255056deab07SRod Evans 	if ((name = pdp->pd_oname) == 0)
255156deab07SRod Evans 		name = pdp->pd_pname;
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	file_notfound(lml, name, clmp, flags, &rej);
255437ffaf83SRod Evans 	return (NULL);
25557c478bd9Sstevel@tonic-gate }
25567c478bd9Sstevel@tonic-gate 
25579a411307Srie /*
25589a411307Srie  * Determine whether a symbol is defined as an interposer.
25599a411307Srie  */
25609a411307Srie int
25619a411307Srie is_sym_interposer(Rt_map *lmp, Sym *sym)
25629a411307Srie {
25639a411307Srie 	Syminfo	*sip = SYMINFO(lmp);
25649a411307Srie 
25659a411307Srie 	if (sip) {
25669a411307Srie 		ulong_t	ndx;
25679a411307Srie 
25689a411307Srie 		ndx = (((ulong_t)sym - (ulong_t)SYMTAB(lmp)) / SYMENT(lmp));
25699a411307Srie 		/* LINTED */
25709a411307Srie 		sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(lmp)));
25719a411307Srie 		if (sip->si_flags & SYMINFO_FLG_INTERPOSE)
25729a411307Srie 			return (1);
25739a411307Srie 	}
25749a411307Srie 	return (0);
25759a411307Srie }
25769a411307Srie 
25777c478bd9Sstevel@tonic-gate /*
25787c478bd9Sstevel@tonic-gate  * While processing direct or group bindings, determine whether the object to
25797c478bd9Sstevel@tonic-gate  * which we've bound can be interposed upon.  In this context, copy relocations
25807c478bd9Sstevel@tonic-gate  * are a form of interposition.
25817c478bd9Sstevel@tonic-gate  */
25827c478bd9Sstevel@tonic-gate static Sym *
2583adbfe822Srie lookup_sym_interpose(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Sym *osym,
2584adbfe822Srie     int *in_nfavl)
25857c478bd9Sstevel@tonic-gate {
2586adbfe822Srie 	Rt_map		*lmp, *clmp;
25877c478bd9Sstevel@tonic-gate 	Slookup		sl;
2588adbfe822Srie 	Lm_list		*lml;
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	/*
25917c478bd9Sstevel@tonic-gate 	 * If we've bound to a copy relocation definition then we need to assign
25927c478bd9Sstevel@tonic-gate 	 * this binding to the original copy reference.  Fabricate an inter-
25937c478bd9Sstevel@tonic-gate 	 * position diagnostic, as this is a legitimate form of interposition.
25947c478bd9Sstevel@tonic-gate 	 */
2595adbfe822Srie 	if (osym && (FLAGS1(*dlmp) & FL1_RT_COPYTOOK)) {
25967c478bd9Sstevel@tonic-gate 		Rel_copy	*rcp;
2597cce0e03bSab 		Aliste		idx;
25987c478bd9Sstevel@tonic-gate 
2599cce0e03bSab 		for (ALIST_TRAVERSE(COPY_R(*dlmp), idx, rcp)) {
2600aa736cbeSrie 			if ((osym == rcp->r_dsym) || (osym->st_value &&
2601aa736cbeSrie 			    (osym->st_value == rcp->r_dsym->st_value))) {
26027c478bd9Sstevel@tonic-gate 				*dlmp = rcp->r_rlmp;
26037c478bd9Sstevel@tonic-gate 				*binfo |=
26047c478bd9Sstevel@tonic-gate 				    (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
26057c478bd9Sstevel@tonic-gate 				return (rcp->r_rsym);
26067c478bd9Sstevel@tonic-gate 			}
26077c478bd9Sstevel@tonic-gate 		}
26087c478bd9Sstevel@tonic-gate 	}
26097c478bd9Sstevel@tonic-gate 
2610adbfe822Srie 	/*
2611adbfe822Srie 	 * If a symbol binding has been established, inspect the link-map list
2612adbfe822Srie 	 * of the destination object, otherwise use the link-map list of the
2613adbfe822Srie 	 * original caller.
2614adbfe822Srie 	 */
2615adbfe822Srie 	if (osym)
2616adbfe822Srie 		clmp = *dlmp;
2617adbfe822Srie 	else
2618adbfe822Srie 		clmp = slp->sl_cmap;
2619adbfe822Srie 
2620adbfe822Srie 	lml = LIST(clmp);
2621adbfe822Srie 	lmp = lml->lm_head;
2622adbfe822Srie 
2623aa736cbeSrie 	/*
2624aa736cbeSrie 	 * Prior to Solaris 8, external references from an executable that were
2625aa736cbeSrie 	 * bound to an uninitialized variable (.bss) within a shared object did
2626aa736cbeSrie 	 * not establish a copy relocation.  This was thought to be an
2627aa736cbeSrie 	 * optimization, to prevent copying zero's to zero's.  Typically,
2628aa736cbeSrie 	 * interposition took its course, with the shared object binding to the
2629aa736cbeSrie 	 * executables data definition.
2630aa736cbeSrie 	 *
2631aa736cbeSrie 	 * This scenario can be broken when this old executable runs against a
2632aa736cbeSrie 	 * new shared object that is directly bound.  With no copy-relocation
2633aa736cbeSrie 	 * record, ld.so.1 has no data to trigger the normal vectoring of the
2634aa736cbeSrie 	 * binding to the executable.
2635aa736cbeSrie 	 *
2636aa736cbeSrie 	 * Starting with Solaris 8, a DT_FLAGS entry is written to all objects,
2637aa736cbeSrie 	 * regardless of there being any DF_ flags entries.  Therefore, an
2638aa736cbeSrie 	 * object without this dynamic tag is susceptible to the copy relocation
2639aa736cbeSrie 	 * issue.  If the executable has no DT_FLAGS tag, and contains the same
2640aa736cbeSrie 	 * .bss symbol definition as has been directly bound to, redirect the
2641aa736cbeSrie 	 * binding to the executables data definition.
2642aa736cbeSrie 	 */
264356deab07SRod Evans 	if (osym && ((FLAGS1(lmp) & FL1_RT_DTFLAGS) == 0) &&
2644adbfe822Srie 	    (FCT(lmp) == &elf_fct) &&
2645466e2a62Srie 	    (ELF_ST_TYPE(osym->st_info) != STT_FUNC) &&
2646466e2a62Srie 	    are_bits_zero(*dlmp, osym, 0)) {
2647aa736cbeSrie 		Rt_map	*ilmp;
2648aa736cbeSrie 		Sym	*isym;
2649aa736cbeSrie 
2650adbfe822Srie 		sl = *slp;
2651adbfe822Srie 		sl.sl_imap = lmp;
2652adbfe822Srie 
2653aa736cbeSrie 		/*
2654aa736cbeSrie 		 * Determine whether the same symbol name exists within the
2655aa736cbeSrie 		 * executable, that the size and type of symbol are the same,
2656aa736cbeSrie 		 * and that the symbol is also associated with .bss.
2657aa736cbeSrie 		 */
26589aa23310Srie 		if (((isym = SYMINTP(lmp)(&sl, &ilmp, binfo,
26599aa23310Srie 		    in_nfavl)) != NULL) && (isym->st_size == osym->st_size) &&
2660aa736cbeSrie 		    (isym->st_info == osym->st_info) &&
2661466e2a62Srie 		    are_bits_zero(lmp, isym, 1)) {
2662aa736cbeSrie 			*dlmp = lmp;
2663aa736cbeSrie 			*binfo |= (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
2664aa736cbeSrie 			return (isym);
2665aa736cbeSrie 		}
2666aa736cbeSrie 	}
2667aa736cbeSrie 
26687c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_INTRPOSE) == 0)
266937ffaf83SRod Evans 		return (NULL);
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 	/*
26727c478bd9Sstevel@tonic-gate 	 * Traverse the list of known interposers to determine whether any
26737c478bd9Sstevel@tonic-gate 	 * offer the same symbol.  Note, the head of the link-map could be
2674aa736cbeSrie 	 * identified as an interposer.  Otherwise, skip the head of the
2675aa736cbeSrie 	 * link-map, so that we don't bind to any .plt references, or
2676aa736cbeSrie 	 * copy-relocation destinations unintentionally.
26777c478bd9Sstevel@tonic-gate 	 */
26787c478bd9Sstevel@tonic-gate 	lmp = lml->lm_head;
26797c478bd9Sstevel@tonic-gate 	sl = *slp;
2680adbfe822Srie 
26819a411307Srie 	if (((FLAGS(lmp) & MSK_RT_INTPOSE) == 0) || (sl.sl_flags & LKUP_COPY))
2682cb511613SAli Bahrami 		lmp = NEXT_RT_MAP(lmp);
26837c478bd9Sstevel@tonic-gate 
2684cb511613SAli Bahrami 	for (; lmp; lmp = NEXT_RT_MAP(lmp)) {
26857c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_DELETE)
26867c478bd9Sstevel@tonic-gate 			continue;
26879a411307Srie 		if ((FLAGS(lmp) & MSK_RT_INTPOSE) == 0)
26887c478bd9Sstevel@tonic-gate 			break;
26897c478bd9Sstevel@tonic-gate 
2690adbfe822Srie 		if (callable(lmp, clmp, 0, sl.sl_flags)) {
26919a411307Srie 			Rt_map	*ilmp;
2692aa736cbeSrie 			Sym	*isym;
26939a411307Srie 
26947c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
26959aa23310Srie 			if (isym = SYMINTP(lmp)(&sl, &ilmp, binfo, in_nfavl)) {
26969a411307Srie 				/*
26979a411307Srie 				 * If this object provides individual symbol
26989a411307Srie 				 * interposers, make sure that the symbol we
26999a411307Srie 				 * have found is tagged as an interposer.
27009a411307Srie 				 */
27019a411307Srie 				if ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
2702aa736cbeSrie 				    (is_sym_interposer(ilmp, isym) == 0))
27039a411307Srie 					continue;
27049a411307Srie 
27059a411307Srie 				/*
27069a411307Srie 				 * Indicate this binding has occurred to an
27079a411307Srie 				 * interposer, and return the symbol.
27089a411307Srie 				 */
27097c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
27109a411307Srie 				*dlmp = ilmp;
2711aa736cbeSrie 				return (isym);
27127c478bd9Sstevel@tonic-gate 			}
27137c478bd9Sstevel@tonic-gate 		}
27147c478bd9Sstevel@tonic-gate 	}
271537ffaf83SRod Evans 	return (NULL);
27167c478bd9Sstevel@tonic-gate }
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate /*
27197c478bd9Sstevel@tonic-gate  * If an object specifies direct bindings (it contains a syminfo structure
27207c478bd9Sstevel@tonic-gate  * describing where each binding was established during link-editing, and the
27217c478bd9Sstevel@tonic-gate  * object was built -Bdirect), then look for the symbol in the specific object.
27227c478bd9Sstevel@tonic-gate  */
27237c478bd9Sstevel@tonic-gate static Sym *
27247c478bd9Sstevel@tonic-gate lookup_sym_direct(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Syminfo *sip,
27259aa23310Srie     Rt_map *lmp, int *in_nfavl)
27267c478bd9Sstevel@tonic-gate {
27277c478bd9Sstevel@tonic-gate 	Rt_map	*clmp = slp->sl_cmap;
27287c478bd9Sstevel@tonic-gate 	Sym	*sym;
27297c478bd9Sstevel@tonic-gate 	Slookup	sl;
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate 	/*
27327c478bd9Sstevel@tonic-gate 	 * If a direct binding resolves to the definition of a copy relocated
27337c478bd9Sstevel@tonic-gate 	 * variable, it must be redirected to the copy (in the executable) that
27347c478bd9Sstevel@tonic-gate 	 * will eventually be made.  Typically, this redirection occurs in
27357c478bd9Sstevel@tonic-gate 	 * lookup_sym_interpose().  But, there's an edge condition.  If a
27367c478bd9Sstevel@tonic-gate 	 * directly bound executable contains pic code, there may be a
27377c478bd9Sstevel@tonic-gate 	 * reference to a definition that will eventually have a copy made.
27387c478bd9Sstevel@tonic-gate 	 * However, this copy relocation may not yet have occurred, because
27397c478bd9Sstevel@tonic-gate 	 * the relocation making this reference comes before the relocation
27407c478bd9Sstevel@tonic-gate 	 * that will create the copy.
27417c478bd9Sstevel@tonic-gate 	 * Under direct bindings, the syminfo indicates that a copy will be
27427c478bd9Sstevel@tonic-gate 	 * taken (SYMINFO_FLG_COPY).  This can only be set in an executable.
27437c478bd9Sstevel@tonic-gate 	 * Thus, the caller must be the executable, so bind to the destination
27447c478bd9Sstevel@tonic-gate 	 * of the copy within the executable.
27457c478bd9Sstevel@tonic-gate 	 */
27467c478bd9Sstevel@tonic-gate 	if (((slp->sl_flags & LKUP_COPY) == 0) &&
27477c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_COPY)) {
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 		slp->sl_imap = LIST(clmp)->lm_head;
27509aa23310Srie 		if (sym = SYMINTP(clmp)(slp, dlmp, binfo, in_nfavl))
27517c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_DIRECT | DBG_BINFO_COPYREF);
27527c478bd9Sstevel@tonic-gate 		return (sym);
27537c478bd9Sstevel@tonic-gate 	}
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 	/*
2756efb9e8b8Srie 	 * If we need to directly bind to our parent, start looking in each
2757efb9e8b8Srie 	 * callers link map.
27587c478bd9Sstevel@tonic-gate 	 */
27597c478bd9Sstevel@tonic-gate 	sl = *slp;
27607c478bd9Sstevel@tonic-gate 	sl.sl_flags |= LKUP_DIRECT;
2761adbfe822Srie 	sym = NULL;
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 	if (sip->si_boundto == SYMINFO_BT_PARENT) {
2764cce0e03bSab 		Aliste		idx1;
2765cce0e03bSab 		Bnd_desc	*bdp;
2766cce0e03bSab 		Grp_hdl		*ghp;
27677c478bd9Sstevel@tonic-gate 
2768efb9e8b8Srie 		/*
2769efb9e8b8Srie 		 * Determine the parent of this explicit dependency from its
2770efb9e8b8Srie 		 * CALLERS()'s list.
2771efb9e8b8Srie 		 */
2772cce0e03bSab 		for (APLIST_TRAVERSE(CALLERS(clmp), idx1, bdp)) {
2773cce0e03bSab 			sl.sl_imap = lmp = bdp->b_caller;
27749aa23310Srie 			if ((sym = SYMINTP(lmp)(&sl, dlmp, binfo,
27759aa23310Srie 			    in_nfavl)) != NULL)
2776efb9e8b8Srie 				goto found;
2777efb9e8b8Srie 		}
2778efb9e8b8Srie 
2779efb9e8b8Srie 		/*
2780efb9e8b8Srie 		 * A caller can also be defined as the parent of a dlopen()
2781efb9e8b8Srie 		 * call.  Determine whether this object has any handles.  The
2782efb9e8b8Srie 		 * dependencies maintained with the handle represent the
2783efb9e8b8Srie 		 * explicit dependencies of the dlopen()'ed object, and the
2784efb9e8b8Srie 		 * calling parent.
2785efb9e8b8Srie 		 */
2786cce0e03bSab 		for (APLIST_TRAVERSE(HANDLES(clmp), idx1, ghp)) {
2787efb9e8b8Srie 			Grp_desc	*gdp;
2788cce0e03bSab 			Aliste		idx2;
2789efb9e8b8Srie 
2790cce0e03bSab 			for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
2791efb9e8b8Srie 				if ((gdp->gd_flags & GPD_PARENT) == 0)
2792efb9e8b8Srie 					continue;
2793efb9e8b8Srie 				sl.sl_imap = lmp = gdp->gd_depend;
2794aa736cbeSrie 				if ((sym = SYMINTP(lmp)(&sl, dlmp,
27959aa23310Srie 				    binfo, in_nfavl)) != NULL)
2796efb9e8b8Srie 					goto found;
2797efb9e8b8Srie 			}
27987c478bd9Sstevel@tonic-gate 		}
27997c478bd9Sstevel@tonic-gate 	} else {
28007c478bd9Sstevel@tonic-gate 		/*
28017c478bd9Sstevel@tonic-gate 		 * If we need to direct bind to anything else look in the
28027c478bd9Sstevel@tonic-gate 		 * link map associated with this symbol reference.
28037c478bd9Sstevel@tonic-gate 		 */
28047c478bd9Sstevel@tonic-gate 		if (sip->si_boundto == SYMINFO_BT_SELF)
28057c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp = clmp;
28067c478bd9Sstevel@tonic-gate 		else
28077c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
28087c478bd9Sstevel@tonic-gate 
28097c478bd9Sstevel@tonic-gate 		if (lmp)
28109aa23310Srie 			sym = SYMINTP(lmp)(&sl, dlmp, binfo, in_nfavl);
28117c478bd9Sstevel@tonic-gate 	}
2812efb9e8b8Srie found:
28137c478bd9Sstevel@tonic-gate 	if (sym)
28147c478bd9Sstevel@tonic-gate 		*binfo |= DBG_BINFO_DIRECT;
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	/*
2817adbfe822Srie 	 * If a reference to a directly bound symbol can't be satisfied, then
2818adbfe822Srie 	 * determine whether an interposer can provide the missing symbol.  If
2819adbfe822Srie 	 * a reference to a directly bound symbol is satisfied, then determine
2820adbfe822Srie 	 * whether that object can be interposed upon for this symbol.
28217c478bd9Sstevel@tonic-gate 	 */
2822adbfe822Srie 	if ((sym == NULL) || ((LIST(*dlmp)->lm_head != *dlmp) &&
2823adbfe822Srie 	    (LIST(*dlmp) == LIST(clmp)))) {
2824aa736cbeSrie 		Sym	*isym;
28257c478bd9Sstevel@tonic-gate 
2826adbfe822Srie 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
2827adbfe822Srie 		    in_nfavl)) != 0)
28287c478bd9Sstevel@tonic-gate 			return (isym);
28297c478bd9Sstevel@tonic-gate 	}
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 	return (sym);
28327c478bd9Sstevel@tonic-gate }
28337c478bd9Sstevel@tonic-gate 
28347c478bd9Sstevel@tonic-gate static Sym *
283560758829Srie core_lookup_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
28369aa23310Srie     Aliste off, int *in_nfavl)
28377c478bd9Sstevel@tonic-gate {
28387c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 	/*
28417c478bd9Sstevel@tonic-gate 	 * Copy relocations should start their search after the head of the
28427c478bd9Sstevel@tonic-gate 	 * main link-map control list.
28437c478bd9Sstevel@tonic-gate 	 */
2844cce0e03bSab 	if ((off == ALIST_OFF_DATA) && (slp->sl_flags & LKUP_COPY) && ilmp)
2845cb511613SAli Bahrami 		lmp = NEXT_RT_MAP(ilmp);
28467c478bd9Sstevel@tonic-gate 	else
28477c478bd9Sstevel@tonic-gate 		lmp = ilmp;
28487c478bd9Sstevel@tonic-gate 
2849cb511613SAli Bahrami 	for (; lmp; lmp = NEXT_RT_MAP(lmp)) {
285060758829Srie 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
28517c478bd9Sstevel@tonic-gate 			Sym	*sym;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
28549aa23310Srie 			if (((sym = SYMINTP(lmp)(slp, dlmp, binfo,
285537ffaf83SRod Evans 			    in_nfavl)) != NULL) ||
285637ffaf83SRod Evans 			    (*binfo & BINFO_MSK_TRYAGAIN))
28577c478bd9Sstevel@tonic-gate 				return (sym);
28587c478bd9Sstevel@tonic-gate 		}
28597c478bd9Sstevel@tonic-gate 	}
286037ffaf83SRod Evans 	return (NULL);
28617c478bd9Sstevel@tonic-gate }
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate static Sym *
28649aa23310Srie _lazy_find_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
28659aa23310Srie     int *in_nfavl)
28667c478bd9Sstevel@tonic-gate {
28677c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
28687c478bd9Sstevel@tonic-gate 
2869cb511613SAli Bahrami 	for (lmp = ilmp; lmp; lmp = NEXT_RT_MAP(lmp)) {
28707c478bd9Sstevel@tonic-gate 		if (LAZY(lmp) == 0)
28717c478bd9Sstevel@tonic-gate 			continue;
287260758829Srie 		if (callable(slp->sl_cmap, lmp, 0, slp->sl_flags)) {
28737c478bd9Sstevel@tonic-gate 			Sym	*sym;
28747c478bd9Sstevel@tonic-gate 
28757c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
28769aa23310Srie 			if ((sym = elf_lazy_find_sym(slp, dlmp, binfo,
28779aa23310Srie 			    in_nfavl)) != 0)
28787c478bd9Sstevel@tonic-gate 				return (sym);
28797c478bd9Sstevel@tonic-gate 		}
28807c478bd9Sstevel@tonic-gate 	}
288137ffaf83SRod Evans 	return (NULL);
28827c478bd9Sstevel@tonic-gate }
28837c478bd9Sstevel@tonic-gate 
288460758829Srie static Sym *
28859aa23310Srie _lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
28867c478bd9Sstevel@tonic-gate {
28877c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
28887c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
2889dae2dfb7Srie 	Lm_list		*lml = LIST(clmp);
28907c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap, *lmp;
28917c478bd9Sstevel@tonic-gate 	ulong_t		rsymndx;
289260758829Srie 	Sym		*sym;
28937c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
28947c478bd9Sstevel@tonic-gate 	Slookup		sl;
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 	/*
28977c478bd9Sstevel@tonic-gate 	 * Search the initial link map for the required symbol (this category is
28987c478bd9Sstevel@tonic-gate 	 * selected by dlsym(), where individual link maps are searched for a
28997c478bd9Sstevel@tonic-gate 	 * required symbol.  Therefore, we know we have permission to look at
29007c478bd9Sstevel@tonic-gate 	 * the link map).
29017c478bd9Sstevel@tonic-gate 	 */
290260758829Srie 	if (slp->sl_flags & LKUP_FIRST)
29039aa23310Srie 		return (SYMINTP(ilmp)(slp, dlmp, binfo, in_nfavl));
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	/*
29067c478bd9Sstevel@tonic-gate 	 * Determine whether this lookup can be satisfied by an objects direct,
29077c478bd9Sstevel@tonic-gate 	 * or lazy binding information.  This is triggered by a relocation from
29087c478bd9Sstevel@tonic-gate 	 * the object (hence rsymndx is set).
29097c478bd9Sstevel@tonic-gate 	 */
29107c478bd9Sstevel@tonic-gate 	if (((rsymndx = slp->sl_rsymndx) != 0) &&
2911aa736cbeSrie 	    ((sip = SYMINFO(clmp)) != NULL)) {
2912dae2dfb7Srie 		uint_t	bound;
2913dae2dfb7Srie 
29147c478bd9Sstevel@tonic-gate 		/*
29157c478bd9Sstevel@tonic-gate 		 * Find the corresponding Syminfo entry for the original
29167c478bd9Sstevel@tonic-gate 		 * referencing symbol.
29177c478bd9Sstevel@tonic-gate 		 */
29187c478bd9Sstevel@tonic-gate 		/* LINTED */
29197c478bd9Sstevel@tonic-gate 		sip = (Syminfo *)((char *)sip + (rsymndx * SYMINENT(clmp)));
2920dae2dfb7Srie 		bound = sip->si_boundto;
2921dae2dfb7Srie 
2922dae2dfb7Srie 		/*
2923dae2dfb7Srie 		 * Identify any EXTERN or PARENT references for ldd(1).
2924dae2dfb7Srie 		 */
2925dae2dfb7Srie 		if ((lml->lm_flags & LML_FLG_TRC_WARN) &&
2926dae2dfb7Srie 		    (bound > SYMINFO_BT_LOWRESERVE)) {
2927dae2dfb7Srie 			if (bound == SYMINFO_BT_PARENT)
2928dae2dfb7Srie 				*binfo |= DBG_BINFO_REF_PARENT;
2929dae2dfb7Srie 			if (bound == SYMINFO_BT_EXTERN)
2930dae2dfb7Srie 				*binfo |= DBG_BINFO_REF_EXTERN;
2931dae2dfb7Srie 		}
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 		/*
29347c478bd9Sstevel@tonic-gate 		 * If the symbol information indicates a direct binding,
29357c478bd9Sstevel@tonic-gate 		 * determine the link map that is required to satisfy the
29367c478bd9Sstevel@tonic-gate 		 * binding.  Note, if the dependency can not be found, but a
29377c478bd9Sstevel@tonic-gate 		 * direct binding isn't required, we will still fall through
29387c478bd9Sstevel@tonic-gate 		 * to perform any default symbol search.
29397c478bd9Sstevel@tonic-gate 		 */
29407c478bd9Sstevel@tonic-gate 		if (sip->si_flags & SYMINFO_FLG_DIRECT) {
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 			lmp = 0;
29437c478bd9Sstevel@tonic-gate 			if (bound < SYMINFO_BT_LOWRESERVE)
29449aa23310Srie 				lmp = elf_lazy_load(clmp, slp, bound,
29459aa23310Srie 				    name, in_nfavl);
29467c478bd9Sstevel@tonic-gate 
29477c478bd9Sstevel@tonic-gate 			/*
29487c478bd9Sstevel@tonic-gate 			 * If direct bindings have been disabled, and this isn't
29497c478bd9Sstevel@tonic-gate 			 * a translator, skip any direct binding now that we've
295060758829Srie 			 * ensured the resolving object has been loaded.
29517c478bd9Sstevel@tonic-gate 			 *
29527c478bd9Sstevel@tonic-gate 			 * If we need to direct bind to anything, we look in
29537c478bd9Sstevel@tonic-gate 			 * ourselves, our parent, or in the link map we've just
29547c478bd9Sstevel@tonic-gate 			 * loaded.  Otherwise, even though we may have lazily
29557c478bd9Sstevel@tonic-gate 			 * loaded an object we still continue to search for
29567c478bd9Sstevel@tonic-gate 			 * symbols from the head of the link map list.
29577c478bd9Sstevel@tonic-gate 			 */
29587c478bd9Sstevel@tonic-gate 			if (((FLAGS(clmp) & FLG_RT_TRANS) ||
2959dae2dfb7Srie 			    (((lml->lm_tflags & LML_TFLG_NODIRECT) == 0) &&
2960dae2dfb7Srie 			    ((slp->sl_flags & LKUP_SINGLETON) == 0))) &&
29619a411307Srie 			    ((FLAGS1(clmp) & FL1_RT_DIRECT) ||
29627c478bd9Sstevel@tonic-gate 			    (sip->si_flags & SYMINFO_FLG_DIRECTBIND))) {
29637c478bd9Sstevel@tonic-gate 				sym = lookup_sym_direct(slp, dlmp, binfo,
29649aa23310Srie 				    sip, lmp, in_nfavl);
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 				/*
296760758829Srie 				 * Determine whether this direct binding has
296860758829Srie 				 * been rejected.  If we've bound to a singleton
296960758829Srie 				 * without following a singleton search, then
297060758829Srie 				 * return.  The caller detects this condition
297160758829Srie 				 * and will trigger a new singleton search.
297260758829Srie 				 *
297360758829Srie 				 * For any other rejection (such as binding to
297460758829Srie 				 * a symbol labeled as nodirect - presumably
297560758829Srie 				 * because the symbol definition has been
297660758829Srie 				 * changed since the referring object was last
297760758829Srie 				 * built), fall through to a standard symbol
29787c478bd9Sstevel@tonic-gate 				 * search.
29797c478bd9Sstevel@tonic-gate 				 */
298037ffaf83SRod Evans 				if (((*binfo & BINFO_MSK_REJECTED) == 0) ||
298137ffaf83SRod Evans 				    (*binfo & BINFO_MSK_TRYAGAIN))
29827c478bd9Sstevel@tonic-gate 					return (sym);
298360758829Srie 
298437ffaf83SRod Evans 				*binfo &= ~BINFO_MSK_REJECTED;
29857c478bd9Sstevel@tonic-gate 			}
29867c478bd9Sstevel@tonic-gate 		}
29877c478bd9Sstevel@tonic-gate 	}
29887c478bd9Sstevel@tonic-gate 
298960758829Srie 	/*
299060758829Srie 	 * Duplicate the lookup information, as we'll need to modify this
299160758829Srie 	 * information for some of the following searches.
299260758829Srie 	 */
29937c478bd9Sstevel@tonic-gate 	sl = *slp;
29947c478bd9Sstevel@tonic-gate 
29957c478bd9Sstevel@tonic-gate 	/*
29967c478bd9Sstevel@tonic-gate 	 * If the referencing object has the DF_SYMBOLIC flag set, look in the
29977c478bd9Sstevel@tonic-gate 	 * referencing object for the symbol first.  Failing that, fall back to
29987c478bd9Sstevel@tonic-gate 	 * our generic search.
29997c478bd9Sstevel@tonic-gate 	 */
300060758829Srie 	if ((FLAGS1(clmp) & FL1_RT_SYMBOLIC) &&
300160758829Srie 	    ((sl.sl_flags & LKUP_SINGLETON) == 0)) {
30027c478bd9Sstevel@tonic-gate 		sl.sl_imap = clmp;
30039aa23310Srie 		if (sym = SYMINTP(clmp)(&sl, dlmp, binfo, in_nfavl)) {
30049a411307Srie 			ulong_t	dsymndx = (((ulong_t)sym -
30057247f888Srie 			    (ulong_t)SYMTAB(*dlmp)) / SYMENT(*dlmp));
30069a411307Srie 
30079a411307Srie 			/*
30089a411307Srie 			 * Make sure this symbol hasn't explicitly been defined
30099a411307Srie 			 * as nodirect.
30109a411307Srie 			 */
30119a411307Srie 			if (((sip = SYMINFO(*dlmp)) == 0) ||
30129a411307Srie 			    /* LINTED */
30139a411307Srie 			    ((sip = (Syminfo *)((char *)sip +
30149a411307Srie 			    (dsymndx * SYMINENT(*dlmp)))) == 0) ||
30159a411307Srie 			    ((sip->si_flags & SYMINFO_FLG_NOEXTDIRECT) == 0))
30169a411307Srie 				return (sym);
30179a411307Srie 		}
30187c478bd9Sstevel@tonic-gate 	}
30197c478bd9Sstevel@tonic-gate 
302060758829Srie 	sl.sl_flags |= LKUP_STANDARD;
302160758829Srie 
30227c478bd9Sstevel@tonic-gate 	/*
30237c478bd9Sstevel@tonic-gate 	 * If this lookup originates from a standard relocation, then traverse
302460758829Srie 	 * all link-map control lists, inspecting any object that is available
302560758829Srie 	 * to this caller.  Otherwise, traverse the link-map control list
302660758829Srie 	 * associated with the caller.
30277c478bd9Sstevel@tonic-gate 	 */
302860758829Srie 	if (sl.sl_flags & LKUP_STDRELOC) {
30297c478bd9Sstevel@tonic-gate 		Aliste	off;
30307c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
30317c478bd9Sstevel@tonic-gate 
303260758829Srie 		sym = NULL;
30337c478bd9Sstevel@tonic-gate 
3034dae2dfb7Srie 		for (ALIST_TRAVERSE_BY_OFFSET(lml->lm_lists, off, lmc)) {
303560758829Srie 			if (((sym = core_lookup_sym(lmc->lc_head, &sl, dlmp,
30369aa23310Srie 			    binfo, off, in_nfavl)) != NULL) ||
303737ffaf83SRod Evans 			    (*binfo & BINFO_MSK_TRYAGAIN))
30387c478bd9Sstevel@tonic-gate 				break;
30397c478bd9Sstevel@tonic-gate 		}
30407c478bd9Sstevel@tonic-gate 	} else
30419aa23310Srie 		sym = core_lookup_sym(ilmp, &sl, dlmp, binfo, ALIST_OFF_DATA,
30429aa23310Srie 		    in_nfavl);
304360758829Srie 
304460758829Srie 	/*
304537ffaf83SRod Evans 	 * If a symbol binding should be retried, return so that the search can
304637ffaf83SRod Evans 	 * be repeated.
304760758829Srie 	 */
304837ffaf83SRod Evans 	if (*binfo & BINFO_MSK_TRYAGAIN)
304960758829Srie 		return (sym);
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 	/*
30527c478bd9Sstevel@tonic-gate 	 * To allow transitioning into a world of lazy loading dependencies see
30537c478bd9Sstevel@tonic-gate 	 * if this link map contains objects that have lazy dependencies still
30547c478bd9Sstevel@tonic-gate 	 * outstanding.  If so, and we haven't been able to locate a non-weak
30557c478bd9Sstevel@tonic-gate 	 * symbol reference, start bringing in any lazy dependencies to see if
30567c478bd9Sstevel@tonic-gate 	 * the reference can be satisfied.  Use of dlsym(RTLD_PROBE) sets the
305775e7992aSrie 	 * LKUP_NOFALLBACK flag, and this flag disables this fall back.
30587c478bd9Sstevel@tonic-gate 	 */
305975e7992aSrie 	if ((sym == NULL) && ((sl.sl_flags & LKUP_NOFALLBACK) == 0)) {
30607c478bd9Sstevel@tonic-gate 		if ((lmp = ilmp) == 0)
30617c478bd9Sstevel@tonic-gate 			lmp = LIST(clmp)->lm_head;
306275e7992aSrie 
3063dae2dfb7Srie 		lml = LIST(lmp);
3064dae2dfb7Srie 		if ((sl.sl_flags & LKUP_WEAK) || (lml->lm_lazy == 0))
306537ffaf83SRod Evans 			return (NULL);
30667c478bd9Sstevel@tonic-gate 
3067dae2dfb7Srie 		DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 		/*
30707c478bd9Sstevel@tonic-gate 		 * If this request originated from a dlsym(RTLD_NEXT) then start
30717c478bd9Sstevel@tonic-gate 		 * looking for dependencies from the caller, otherwise use the
30727c478bd9Sstevel@tonic-gate 		 * initial link-map.
30737c478bd9Sstevel@tonic-gate 		 */
307460758829Srie 		if (sl.sl_flags & LKUP_NEXT)
30759aa23310Srie 			sym = _lazy_find_sym(clmp, &sl, dlmp, binfo, in_nfavl);
30767c478bd9Sstevel@tonic-gate 		else {
3077cce0e03bSab 			Aliste	idx;
30787c478bd9Sstevel@tonic-gate 			Lm_cntl	*lmc;
30797c478bd9Sstevel@tonic-gate 
3080dae2dfb7Srie 			for (ALIST_TRAVERSE(lml->lm_lists, idx, lmc)) {
308175e7992aSrie 				sl.sl_flags |= LKUP_NOFALLBACK;
30827c478bd9Sstevel@tonic-gate 				if ((sym = _lazy_find_sym(lmc->lc_head, &sl,
30839aa23310Srie 				    dlmp, binfo, in_nfavl)) != 0)
30847c478bd9Sstevel@tonic-gate 					break;
30857c478bd9Sstevel@tonic-gate 			}
30867c478bd9Sstevel@tonic-gate 		}
30877c478bd9Sstevel@tonic-gate 	}
308860758829Srie 	return (sym);
308960758829Srie }
309060758829Srie 
309160758829Srie /*
309260758829Srie  * Symbol lookup routine.  Takes an ELF symbol name, and a list of link maps to
309360758829Srie  * search.  If successful, return a pointer to the symbol table entry, a
309460758829Srie  * pointer to the link map of the enclosing object, and information relating
309560758829Srie  * to the type of binding.  Else return a null pointer.
309660758829Srie  *
309760758829Srie  * To improve elf performance, we first compute the elf hash value and pass
309860758829Srie  * it to each find_sym() routine.  The elf function will use this value to
309960758829Srie  * locate the symbol, the a.out function will simply ignore it.
310060758829Srie  */
310160758829Srie Sym *
31029aa23310Srie lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
310360758829Srie {
310460758829Srie 	Rt_map		*clmp = slp->sl_cmap;
310560758829Srie 	Sym		*rsym = slp->sl_rsym, *sym = 0;
310660758829Srie 	uchar_t		rtype = slp->sl_rtype;
310737ffaf83SRod Evans 	int		mode;
310860758829Srie 
310960758829Srie 	if (slp->sl_hash == 0)
311075e7992aSrie 		slp->sl_hash = elf_hash(slp->sl_name);
311160758829Srie 	*binfo = 0;
311260758829Srie 
311360758829Srie 	/*
311460758829Srie 	 * Establish any state that might be associated with a symbol reference.
311560758829Srie 	 */
311660758829Srie 	if (rsym) {
311760758829Srie 		if ((slp->sl_flags & LKUP_STDRELOC) &&
311860758829Srie 		    (ELF_ST_BIND(rsym->st_info) == STB_WEAK))
311960758829Srie 			slp->sl_flags |= LKUP_WEAK;
312060758829Srie 
312160758829Srie 		if (ELF_ST_VISIBILITY(rsym->st_other) == STV_SINGLETON)
312260758829Srie 			slp->sl_flags |= LKUP_SINGLETON;
312360758829Srie 	}
312460758829Srie 
312560758829Srie 	/*
312660758829Srie 	 * Establish any lookup state required for this type of relocation.
312760758829Srie 	 */
312860758829Srie 	if ((slp->sl_flags & LKUP_STDRELOC) && rtype) {
312960758829Srie 		if (rtype == M_R_COPY)
313060758829Srie 			slp->sl_flags |= LKUP_COPY;
313160758829Srie 
313260758829Srie 		if (rtype != M_R_JMP_SLOT)
313360758829Srie 			slp->sl_flags |= LKUP_SPEC;
313460758829Srie 	}
313560758829Srie 
313660758829Srie 	/*
313760758829Srie 	 * Under ldd -w, any unresolved weak references are diagnosed.  Set the
313860758829Srie 	 * symbol binding as global to trigger a relocation error if the symbol
313960758829Srie 	 * can not be found.
314060758829Srie 	 */
314160758829Srie 	if (rsym) {
314260758829Srie 		if (LIST(slp->sl_cmap)->lm_flags & LML_FLG_TRC_NOUNRESWEAK)
314360758829Srie 			slp->sl_bind = STB_GLOBAL;
314460758829Srie 		else if ((slp->sl_bind = ELF_ST_BIND(rsym->st_info)) ==
314560758829Srie 		    STB_WEAK)
314660758829Srie 			slp->sl_flags |= LKUP_WEAK;
314760758829Srie 	}
314860758829Srie 
314937ffaf83SRod Evans 	/*
315037ffaf83SRod Evans 	 * Save the callers MODE().
315137ffaf83SRod Evans 	 */
315237ffaf83SRod Evans 	mode = MODE(clmp);
315337ffaf83SRod Evans 
315460758829Srie 	/*
315560758829Srie 	 * Carry out an initial symbol search.  This search takes into account
315660758829Srie 	 * all the modes of the requested search.
315760758829Srie 	 */
31589aa23310Srie 	if (((sym = _lookup_sym(slp, dlmp, binfo, in_nfavl)) == NULL) &&
315937ffaf83SRod Evans 	    (*binfo & BINFO_MSK_TRYAGAIN)) {
316060758829Srie 		Slookup	sl = *slp;
316160758829Srie 
316260758829Srie 		/*
316337ffaf83SRod Evans 		 * Try the symbol search again.  This retry can be necessary if:
316437ffaf83SRod Evans 		 *
316537ffaf83SRod Evans 		 *  .	a binding has been rejected because of binding to a
316637ffaf83SRod Evans 		 *	singleton without going through a singleton search.
316737ffaf83SRod Evans 		 *  .	a group binding has resulted in binding to a symbol
316837ffaf83SRod Evans 		 *	that indicates no-direct binding.
316937ffaf83SRod Evans 		 *
317037ffaf83SRod Evans 		 * Reset the lookup data, and try again.
317160758829Srie 		 */
317260758829Srie 		sl.sl_imap = LIST(sl.sl_cmap)->lm_head;
317360758829Srie 		sl.sl_flags &= ~(LKUP_FIRST | LKUP_SELF | LKUP_NEXT);
317460758829Srie 		sl.sl_rsymndx = 0;
317537ffaf83SRod Evans 
317637ffaf83SRod Evans 		if (*binfo & BINFO_REJSINGLE)
317737ffaf83SRod Evans 			sl.sl_flags |= LKUP_SINGLETON;
317837ffaf83SRod Evans 		if (*binfo & BINFO_REJGROUP) {
317937ffaf83SRod Evans 			sl.sl_flags |= LKUP_WORLD;
318037ffaf83SRod Evans 			mode |= RTLD_WORLD;
318137ffaf83SRod Evans 		}
318237ffaf83SRod Evans 		*binfo &= ~BINFO_MSK_REJECTED;
318337ffaf83SRod Evans 
31849aa23310Srie 		sym = _lookup_sym(&sl, dlmp, binfo, in_nfavl);
318560758829Srie 	}
31867c478bd9Sstevel@tonic-gate 
31877c478bd9Sstevel@tonic-gate 	/*
31887c478bd9Sstevel@tonic-gate 	 * If the caller is restricted to a symbol search within its group,
31897c478bd9Sstevel@tonic-gate 	 * determine if it is necessary to follow a binding from outside of
31907c478bd9Sstevel@tonic-gate 	 * the group.
31917c478bd9Sstevel@tonic-gate 	 */
319237ffaf83SRod Evans 	if ((mode & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP) {
3193aa736cbeSrie 		Sym	*isym;
31947c478bd9Sstevel@tonic-gate 
3195adbfe822Srie 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, sym,
319637ffaf83SRod Evans 		    in_nfavl)) != NULL)
31977c478bd9Sstevel@tonic-gate 			return (isym);
31987c478bd9Sstevel@tonic-gate 	}
31997c478bd9Sstevel@tonic-gate 	return (sym);
32007c478bd9Sstevel@tonic-gate }
32017c478bd9Sstevel@tonic-gate 
32027c478bd9Sstevel@tonic-gate /*
32037c478bd9Sstevel@tonic-gate  * Associate a binding descriptor with a caller and its dependency, or update
32047c478bd9Sstevel@tonic-gate  * an existing descriptor.
32057c478bd9Sstevel@tonic-gate  */
32067c478bd9Sstevel@tonic-gate int
32075aefb655Srie bind_one(Rt_map *clmp, Rt_map *dlmp, uint_t flags)
32087c478bd9Sstevel@tonic-gate {
3209cce0e03bSab 	Bnd_desc	*bdp;
3210cce0e03bSab 	Aliste		idx;
32117c478bd9Sstevel@tonic-gate 	int		found = ALE_CREATE;
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	/*
32147c478bd9Sstevel@tonic-gate 	 * Determine whether a binding descriptor already exists between the
32157c478bd9Sstevel@tonic-gate 	 * two objects.
32167c478bd9Sstevel@tonic-gate 	 */
3217cce0e03bSab 	for (APLIST_TRAVERSE(DEPENDS(clmp), idx, bdp)) {
32187c478bd9Sstevel@tonic-gate 		if (bdp->b_depend == dlmp) {
32197c478bd9Sstevel@tonic-gate 			found = ALE_EXISTS;
32207c478bd9Sstevel@tonic-gate 			break;
32217c478bd9Sstevel@tonic-gate 		}
32227c478bd9Sstevel@tonic-gate 	}
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate 	if (found == ALE_CREATE) {
32257c478bd9Sstevel@tonic-gate 		/*
32267c478bd9Sstevel@tonic-gate 		 * Create a new binding descriptor.
32277c478bd9Sstevel@tonic-gate 		 */
322837ffaf83SRod Evans 		if ((bdp = malloc(sizeof (Bnd_desc))) == NULL)
32297c478bd9Sstevel@tonic-gate 			return (0);
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 		bdp->b_caller = clmp;
32327c478bd9Sstevel@tonic-gate 		bdp->b_depend = dlmp;
32337c478bd9Sstevel@tonic-gate 		bdp->b_flags = 0;
32347c478bd9Sstevel@tonic-gate 
32357c478bd9Sstevel@tonic-gate 		/*
32367c478bd9Sstevel@tonic-gate 		 * Append the binding descriptor to the caller and the
32377c478bd9Sstevel@tonic-gate 		 * dependency.
32387c478bd9Sstevel@tonic-gate 		 */
323956deab07SRod Evans 		if (aplist_append(&DEPENDS(clmp), bdp, AL_CNT_DEPENDS) == NULL)
32407c478bd9Sstevel@tonic-gate 			return (0);
32417c478bd9Sstevel@tonic-gate 
324256deab07SRod Evans 		if (aplist_append(&CALLERS(dlmp), bdp, AL_CNT_CALLERS) == NULL)
32437c478bd9Sstevel@tonic-gate 			return (0);
32447c478bd9Sstevel@tonic-gate 	}
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	if ((found == ALE_CREATE) || ((bdp->b_flags & flags) != flags)) {
32477c478bd9Sstevel@tonic-gate 		bdp->b_flags |= flags;
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 		if (flags & BND_REFER)
32507c478bd9Sstevel@tonic-gate 			FLAGS1(dlmp) |= FL1_RT_USED;
32517c478bd9Sstevel@tonic-gate 
32525aefb655Srie 		DBG_CALL(Dbg_file_bind_entry(LIST(clmp), bdp));
32537c478bd9Sstevel@tonic-gate 	}
32547c478bd9Sstevel@tonic-gate 	return (found);
32557c478bd9Sstevel@tonic-gate }
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate /*
32587c478bd9Sstevel@tonic-gate  * Cleanup after relocation processing.
32597c478bd9Sstevel@tonic-gate  */
32607c478bd9Sstevel@tonic-gate int
326156deab07SRod Evans relocate_finish(Rt_map *lmp, APlist *bound, int ret)
32627c478bd9Sstevel@tonic-gate {
32635aefb655Srie 	DBG_CALL(Dbg_reloc_run(lmp, 0, ret, DBG_REL_FINISH));
32647c478bd9Sstevel@tonic-gate 
32657c478bd9Sstevel@tonic-gate 	/*
32667c478bd9Sstevel@tonic-gate 	 * Establish bindings to all objects that have been bound to.
32677c478bd9Sstevel@tonic-gate 	 */
32687c478bd9Sstevel@tonic-gate 	if (bound) {
3269cce0e03bSab 		Rt_map	*_lmp;
32708a20d9f8Srie 		Word	used;
32717c478bd9Sstevel@tonic-gate 
32728a20d9f8Srie 		/*
32738a20d9f8Srie 		 * Only create bindings if the callers relocation was
32748a20d9f8Srie 		 * successful (ret != 0), otherwise the object will eventually
32758a20d9f8Srie 		 * be torn down.  Create these bindings if running under ldd(1)
32768a20d9f8Srie 		 * with the -U/-u options regardless of relocation errors, as
32778a20d9f8Srie 		 * the unused processing needs to traverse these bindings to
32788a20d9f8Srie 		 * diagnose unused objects.
32798a20d9f8Srie 		 */
32808a20d9f8Srie 		used = LIST(lmp)->lm_flags &
32818a20d9f8Srie 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED);
32828a20d9f8Srie 
32838a20d9f8Srie 		if (ret || used) {
328456deab07SRod Evans 			Aliste	idx;
328556deab07SRod Evans 
3286cce0e03bSab 			for (APLIST_TRAVERSE(bound, idx, _lmp)) {
32878a20d9f8Srie 				if (bind_one(lmp, _lmp, BND_REFER) || used)
32888a20d9f8Srie 					continue;
32898a20d9f8Srie 
32908a20d9f8Srie 				ret = 0;
32918a20d9f8Srie 				break;
32927c478bd9Sstevel@tonic-gate 			}
32937c478bd9Sstevel@tonic-gate 		}
32947c478bd9Sstevel@tonic-gate 		free(bound);
32957c478bd9Sstevel@tonic-gate 	}
32967c478bd9Sstevel@tonic-gate 
329756deab07SRod Evans 	return (ret);
329856deab07SRod Evans }
329956deab07SRod Evans 
330056deab07SRod Evans /*
330156deab07SRod Evans  * Function to correct protection settings.  Segments are all mapped initially
330256deab07SRod Evans  * with permissions as given in the segment header.  We need to turn on write
330356deab07SRod Evans  * permissions on a text segment if there are any relocations against that
330456deab07SRod Evans  * segment, and then turn write permission back off again before returning
330556deab07SRod Evans  * control to the caller.  This function turns the permission on or off
330656deab07SRod Evans  * depending on the value of the permission argument.
330756deab07SRod Evans  */
330856deab07SRod Evans int
330956deab07SRod Evans set_prot(Rt_map *lmp, mmapobj_result_t *mpp, int perm)
331056deab07SRod Evans {
331156deab07SRod Evans 	int	prot;
331256deab07SRod Evans 
33137c478bd9Sstevel@tonic-gate 	/*
331456deab07SRod Evans 	 * If this is an allocated image (ie. a relocatable object) we can't
331556deab07SRod Evans 	 * mprotect() anything.
33167c478bd9Sstevel@tonic-gate 	 */
331756deab07SRod Evans 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
331856deab07SRod Evans 		return (1);
33197c478bd9Sstevel@tonic-gate 
332056deab07SRod Evans 	DBG_CALL(Dbg_file_prot(lmp, perm));
332156deab07SRod Evans 
332256deab07SRod Evans 	if (perm)
332356deab07SRod Evans 		prot = mpp->mr_prot | PROT_WRITE;
332456deab07SRod Evans 	else
332556deab07SRod Evans 		prot = mpp->mr_prot & ~PROT_WRITE;
332656deab07SRod Evans 
332756deab07SRod Evans 	if (mprotect((void *)(uintptr_t)mpp->mr_addr,
332856deab07SRod Evans 	    mpp->mr_msize, prot) == -1) {
332956deab07SRod Evans 		int	err = errno;
333056deab07SRod Evans 		eprintf(LIST(lmp), ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
333156deab07SRod Evans 		    NAME(lmp), strerror(err));
333256deab07SRod Evans 		return (0);
333356deab07SRod Evans 	}
333456deab07SRod Evans 	mpp->mr_prot = prot;
333556deab07SRod Evans 	return (1);
33367c478bd9Sstevel@tonic-gate }
3337