xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/analyze.c (revision fb1354ed)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*fb1354edSrie 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
257c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include	"_synonyms.h"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	<string.h>
357c478bd9Sstevel@tonic-gate #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	<unistd.h>
377c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
387c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
397c478bd9Sstevel@tonic-gate #include	<fcntl.h>
407c478bd9Sstevel@tonic-gate #include	<limits.h>
417c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
427c478bd9Sstevel@tonic-gate #include	<errno.h>
437c478bd9Sstevel@tonic-gate #include	<link.h>
447c478bd9Sstevel@tonic-gate #include	"_rtld.h"
457c478bd9Sstevel@tonic-gate #include	"_audit.h"
467c478bd9Sstevel@tonic-gate #include	"_elf.h"
477c478bd9Sstevel@tonic-gate #include	"msg.h"
487c478bd9Sstevel@tonic-gate #include	"debug.h"
497c478bd9Sstevel@tonic-gate #include	"conv.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate static Fct *	vector[] = {
527c478bd9Sstevel@tonic-gate 	&elf_fct,
537c478bd9Sstevel@tonic-gate #ifdef A_OUT
547c478bd9Sstevel@tonic-gate 	&aout_fct,
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate 	0
577c478bd9Sstevel@tonic-gate };
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * If a load filter flag is in effect, and this object is a filter, trigger the
617c478bd9Sstevel@tonic-gate  * loading of all its filtees.  The load filter flag is in effect when creating
627c478bd9Sstevel@tonic-gate  * configuration files, or when under the control of ldd(1), or the LD_LOADFLTR
637c478bd9Sstevel@tonic-gate  * environment variable is set, or this object was built with the -zloadfltr
647c478bd9Sstevel@tonic-gate  * flag.  Otherwise, filtee loading is deferred until triggered by a relocation.
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate static void
677c478bd9Sstevel@tonic-gate load_filtees(Rt_map *lmp)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	if ((FLAGS1(lmp) & MSK_RT_FILTER) &&
707c478bd9Sstevel@tonic-gate 	    ((FLAGS(lmp) & FLG_RT_LOADFLTR) ||
717c478bd9Sstevel@tonic-gate 	    (LIST(lmp)->lm_tflags & LML_TFLG_LOADFLTR))) {
727c478bd9Sstevel@tonic-gate 		Dyninfo *	dip =  DYNINFO(lmp);
737c478bd9Sstevel@tonic-gate 		uint_t		cnt, max = DYNINFOCNT(lmp);
747c478bd9Sstevel@tonic-gate 		Slookup		sl;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 		sl.sl_name = 0;
777c478bd9Sstevel@tonic-gate 		sl.sl_hash = 0;
787c478bd9Sstevel@tonic-gate 		sl.sl_imap = sl.sl_cmap = lmp;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		for (cnt = 0; cnt < max; cnt++, dip++) {
817c478bd9Sstevel@tonic-gate 			if (((dip->di_flags & MSK_DI_FILTER) == 0) ||
827c478bd9Sstevel@tonic-gate 			    ((dip->di_flags & FLG_DI_AUXFLTR) &&
837c478bd9Sstevel@tonic-gate 			    (rtld_flags & RT_FL_NOAUXFLTR)))
847c478bd9Sstevel@tonic-gate 				continue;
857c478bd9Sstevel@tonic-gate 			(void) elf_lookup_filtee(&sl, 0, 0, cnt);
867c478bd9Sstevel@tonic-gate 		}
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Analyze one or more link-maps of a link map control list.  This routine is
927c478bd9Sstevel@tonic-gate  * called at startup to continue the processing of the main executable.  It is
937c478bd9Sstevel@tonic-gate  * also called each time a new set of objects are loaded, ie. from filters,
947c478bd9Sstevel@tonic-gate  * lazy-loaded objects, or dlopen().
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * In each instance we traverse the link-map control list starting with the
977c478bd9Sstevel@tonic-gate  * initial object.  As dependencies are analyzed they are added to the link-map
987c478bd9Sstevel@tonic-gate  * control list.  Thus the list grows as we traverse it - this results in the
997c478bd9Sstevel@tonic-gate  * breadth first ordering of all needed objects.
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate int
1027c478bd9Sstevel@tonic-gate analyze_lmc(Lm_list *lml, Aliste nlmco, Rt_map *nlmp)
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate 	Rt_map	*lmp = nlmp;
1057c478bd9Sstevel@tonic-gate 	Lm_cntl	*nlmc;
1067c478bd9Sstevel@tonic-gate 	int	ret = 1;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
1097c478bd9Sstevel@tonic-gate 	 * If this link-map control list is being analyzed, return.  The object
1107c478bd9Sstevel@tonic-gate 	 * that has just been added will be picked up by the existing analysis
1117c478bd9Sstevel@tonic-gate 	 * thread.  Note, this is only really meaningful during process init-
1127c478bd9Sstevel@tonic-gate 	 * ialization, as objects are added to the main link-map control list.
1137c478bd9Sstevel@tonic-gate 	 * Following this initialization, each family of objects that are loaded
1147c478bd9Sstevel@tonic-gate 	 * are added to a new link-map control list.
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	/* LINTED */
1177c478bd9Sstevel@tonic-gate 	nlmc = (Lm_cntl *)((char *)lml->lm_lists + nlmco);
1187c478bd9Sstevel@tonic-gate 	if (nlmc->lc_flags & LMC_FLG_ANALYZING)
1197c478bd9Sstevel@tonic-gate 		return (1);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * If this object doesn't belong to the present link-map control list
1237c478bd9Sstevel@tonic-gate 	 * then it must already have been analyzed, or it is in the process of
1247c478bd9Sstevel@tonic-gate 	 * being analyzed prior to us recursing into this analysis.  In either
1257c478bd9Sstevel@tonic-gate 	 * case, ignore the object as it's already being taken care of.
1267c478bd9Sstevel@tonic-gate 	 */
1277c478bd9Sstevel@tonic-gate 	if (nlmco != CNTL(nlmp))
1287c478bd9Sstevel@tonic-gate 		return (1);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	nlmc->lc_flags |= LMC_FLG_ANALYZING;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
1337c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) &
1347c478bd9Sstevel@tonic-gate 		    (FLG_RT_ANALZING | FLG_RT_ANALYZED | FLG_RT_DELETE))
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		/*
1387c478bd9Sstevel@tonic-gate 		 * Indicate that analyzing is under way.
1397c478bd9Sstevel@tonic-gate 		 */
1407c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_ANALZING;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		/*
1437c478bd9Sstevel@tonic-gate 		 * If this link map represents a relocatable object, then we
1447c478bd9Sstevel@tonic-gate 		 * need to finish the link-editing of the object at this point.
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_OBJECT) {
1477c478bd9Sstevel@tonic-gate 			if (elf_obj_fini(lml, lmp) == 0) {
1487c478bd9Sstevel@tonic-gate 				if (lml->lm_flags & LML_FLG_TRC_ENABLE)
1497c478bd9Sstevel@tonic-gate 					continue;
1507c478bd9Sstevel@tonic-gate 				ret = 0;
1517c478bd9Sstevel@tonic-gate 				break;
1527c478bd9Sstevel@tonic-gate 			}
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_analyze(lmp));
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 		/*
1587c478bd9Sstevel@tonic-gate 		 * Establish any dependencies this object requires.
1597c478bd9Sstevel@tonic-gate 		 */
1607c478bd9Sstevel@tonic-gate 		if (LM_NEEDED(lmp)(lml, nlmco, lmp) == 0) {
1617c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE)
1627c478bd9Sstevel@tonic-gate 				continue;
1637c478bd9Sstevel@tonic-gate 			ret = 0;
1647c478bd9Sstevel@tonic-gate 			break;
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_ANALZING;
1687c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_ANALYZED;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 		/*
1717c478bd9Sstevel@tonic-gate 		 * If we're building a configuration file, determine if this
1727c478bd9Sstevel@tonic-gate 		 * object is a filter and if so load its filtees.  This
1737c478bd9Sstevel@tonic-gate 		 * traversal is only necessary for crle(1), as typical use of
1747c478bd9Sstevel@tonic-gate 		 * an object will load filters as part of relocation processing.
1757c478bd9Sstevel@tonic-gate 		 */
1767c478bd9Sstevel@tonic-gate 		if (MODE(nlmp) & RTLD_CONFGEN)
1777c478bd9Sstevel@tonic-gate 			load_filtees(lmp);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 		/*
1807c478bd9Sstevel@tonic-gate 		 * If an interposer has been added, it will have been inserted
1817c478bd9Sstevel@tonic-gate 		 * in the link-map before the link we're presently analyzing.
1827c478bd9Sstevel@tonic-gate 		 * Break out of this analysis loop and return to the head of
1837c478bd9Sstevel@tonic-gate 		 * the link-map control list to analyze the interposer.  Note
1847c478bd9Sstevel@tonic-gate 		 * that this rescan preserves the breadth first loading of
1857c478bd9Sstevel@tonic-gate 		 * dependencies.
1867c478bd9Sstevel@tonic-gate 		 */
18724a6229eSrie 		/* LINTED */
18824a6229eSrie 		nlmc = (Lm_cntl *)((char *)lml->lm_lists + nlmco);
1897c478bd9Sstevel@tonic-gate 		if (nlmc->lc_flags & LMC_FLG_REANALYZE) {
1907c478bd9Sstevel@tonic-gate 			nlmc->lc_flags &= ~LMC_FLG_REANALYZE;
1917c478bd9Sstevel@tonic-gate 			lmp = nlmc->lc_head;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
19524a6229eSrie 	/* LINTED */
19624a6229eSrie 	nlmc = (Lm_cntl *)((char *)lml->lm_lists + nlmco);
1977c478bd9Sstevel@tonic-gate 	nlmc->lc_flags &= ~LMC_FLG_ANALYZING;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	return (ret);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /*
2037c478bd9Sstevel@tonic-gate  * Copy relocation test.  If the symbol definition is within .bss, then it's
2047c478bd9Sstevel@tonic-gate  * zero filled, and as the destination is within .bss, we can skip copying
2057c478bd9Sstevel@tonic-gate  * zero's to zero's.  However, if the destination object has a MOVE table, it's
2067c478bd9Sstevel@tonic-gate  * .bss might contain non-zero data, in which case copy it regardless.
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate static int
2097c478bd9Sstevel@tonic-gate copy_zerobits(Rt_map *dlmp, Sym *dsym)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & FLG_RT_MOVE) == 0) {
2127c478bd9Sstevel@tonic-gate 		Mmap	*mmaps;
2137c478bd9Sstevel@tonic-gate 		caddr_t	daddr = (caddr_t)dsym->st_value;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		if ((FLAGS(dlmp) & FLG_RT_FIXED) == 0)
2167c478bd9Sstevel@tonic-gate 			daddr += ADDR(dlmp);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		for (mmaps = MMAPS(dlmp); mmaps->m_vaddr; mmaps++) {
2197c478bd9Sstevel@tonic-gate 			if ((mmaps->m_fsize != mmaps->m_msize) &&
2207c478bd9Sstevel@tonic-gate 			    (daddr >= (mmaps->m_vaddr + mmaps->m_fsize)) &&
2217c478bd9Sstevel@tonic-gate 			    (daddr < (mmaps->m_vaddr + mmaps->m_msize)))
2227c478bd9Sstevel@tonic-gate 				return (1);
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	return (0);
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Relocate an individual object.
2307c478bd9Sstevel@tonic-gate  */
2317c478bd9Sstevel@tonic-gate static int
2327c478bd9Sstevel@tonic-gate relocate_so(Lm_list *lml, Rt_map *lmp, int *relocated, int now)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * If we're running under ldd(1), and haven't been asked to trace any
2367c478bd9Sstevel@tonic-gate 	 * warnings, skip any actual relocation processing.
2377c478bd9Sstevel@tonic-gate 	 */
2387c478bd9Sstevel@tonic-gate 	if (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
2397c478bd9Sstevel@tonic-gate 	    (lml->lm_flags & LML_FLG_TRC_WARN)) {
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		if (relocated)
2427c478bd9Sstevel@tonic-gate 			(*relocated)++;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		if ((LM_RELOC(lmp)(lmp, now) == 0) &&
2457c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0))
2467c478bd9Sstevel@tonic-gate 			return (0);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	return (1);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * Relocate the objects on a link-map control list.
2537c478bd9Sstevel@tonic-gate  */
2547c478bd9Sstevel@tonic-gate static int
2557c478bd9Sstevel@tonic-gate _relocate_lmc(Lm_list *lml, Rt_map *nlmp, int *relocated)
2567c478bd9Sstevel@tonic-gate {
2577c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	for (lmp = nlmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
2607c478bd9Sstevel@tonic-gate 		/*
2617c478bd9Sstevel@tonic-gate 		 * If this object has already been relocated, we're done.  If
2627c478bd9Sstevel@tonic-gate 		 * this object is being deleted, skip it, there's probably a
2637c478bd9Sstevel@tonic-gate 		 * relocation error somewhere that's causing this deletion.
2647c478bd9Sstevel@tonic-gate 		 */
2657c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) &
2667c478bd9Sstevel@tonic-gate 		    (FLG_RT_RELOCING | FLG_RT_RELOCED | FLG_RT_DELETE))
2677c478bd9Sstevel@tonic-gate 			continue;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		/*
2707c478bd9Sstevel@tonic-gate 		 * Indicate that relocation processing is under way.
2717c478bd9Sstevel@tonic-gate 		 */
2727c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_RELOCING;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		/*
2757c478bd9Sstevel@tonic-gate 		 * Relocate the object.
2767c478bd9Sstevel@tonic-gate 		 */
2777c478bd9Sstevel@tonic-gate 		if (relocate_so(lml, lmp, relocated, 0) == 0)
2787c478bd9Sstevel@tonic-gate 			return (0);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		/*
2817c478bd9Sstevel@tonic-gate 		 * Indicate that the objects relocation is complete.
2827c478bd9Sstevel@tonic-gate 		 */
2837c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_RELOCING;
2847c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_RELOCED;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		/*
2877c478bd9Sstevel@tonic-gate 		 * Mark this object's init is available for harvesting.  Under
2887c478bd9Sstevel@tonic-gate 		 * ldd(1) this marking is necessary for -i (tsort) gathering.
2897c478bd9Sstevel@tonic-gate 		 */
2907c478bd9Sstevel@tonic-gate 		lml->lm_init++;
291dffec89cSrie 		lml->lm_flags |= LML_FLG_OBJADDED;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		/*
2947c478bd9Sstevel@tonic-gate 		 * None of the following processing is necessary under ldd().
2957c478bd9Sstevel@tonic-gate 		 */
2967c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) {
2977c478bd9Sstevel@tonic-gate 			/*
2987c478bd9Sstevel@tonic-gate 			 * Process any move data.
2997c478bd9Sstevel@tonic-gate 			 */
3007c478bd9Sstevel@tonic-gate 			if (FLAGS(lmp) & FLG_RT_MOVE)
3017c478bd9Sstevel@tonic-gate 				move_data(lmp);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			/*
3047c478bd9Sstevel@tonic-gate 			 * Process any DT_SUNW_RTLDINFO information now the
3057c478bd9Sstevel@tonic-gate 			 * object is relocated, and remove the RTLDINFO
3067c478bd9Sstevel@tonic-gate 			 * infrastructure as it won't be needed anymore.
3077c478bd9Sstevel@tonic-gate 			 *
3087c478bd9Sstevel@tonic-gate 			 * We wait until lmp == lm_info_lmp, as it's at this
3097c478bd9Sstevel@tonic-gate 			 * stage we know the object contributing RTLDINFO has
3107c478bd9Sstevel@tonic-gate 			 * been properly relocated.
3117c478bd9Sstevel@tonic-gate 			 */
3127c478bd9Sstevel@tonic-gate 			if ((FCT(lmp) == &elf_fct) && (lml->lm_rtldinfo) &&
3137c478bd9Sstevel@tonic-gate 			    (lmp == lml->lm_info_lmp)) {
3147c478bd9Sstevel@tonic-gate 				Aliste		off;
3157c478bd9Sstevel@tonic-gate 				Lc_interface **	funcs;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 				for (ALIST_TRAVERSE(lml->lm_rtldinfo,
3187c478bd9Sstevel@tonic-gate 				    off, funcs))
3197c478bd9Sstevel@tonic-gate 					get_lcinterface(lmp, *funcs);
3207c478bd9Sstevel@tonic-gate 				free(lml->lm_rtldinfo);
3217c478bd9Sstevel@tonic-gate 				lml->lm_rtldinfo = 0;
3227c478bd9Sstevel@tonic-gate 			}
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 		/*
3267c478bd9Sstevel@tonic-gate 		 * Determine if this object is a filter, and if a load filter
3277c478bd9Sstevel@tonic-gate 		 * flag is in effect, trigger the loading of all its filtees.
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		load_filtees(lmp);
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Perform special copy relocations.  These are only meaningful for
3347c478bd9Sstevel@tonic-gate 	 * dynamic executables (fixed and head of their link-map list).  If
3357c478bd9Sstevel@tonic-gate 	 * this ever has to change then the infrastructure of COPY() has to
3367c478bd9Sstevel@tonic-gate 	 * change as presently this element is used to capture both receiver
3377c478bd9Sstevel@tonic-gate 	 * and supplier of copy data.
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) & FLG_RT_FIXED) && (nlmp == LIST(nlmp)->lm_head) &&
3407c478bd9Sstevel@tonic-gate 	    (((lml->lm_flags & LML_FLG_TRC_ENABLE) == 0) ||
3417c478bd9Sstevel@tonic-gate 	    (lml->lm_flags & LML_FLG_TRC_WARN))) {
3427c478bd9Sstevel@tonic-gate 		Rt_map **	lmpp;
3437c478bd9Sstevel@tonic-gate 		Aliste		off1;
3447c478bd9Sstevel@tonic-gate 		Word		tracing;
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate #if	defined(__sparc) || defined(__amd64)
3477c478bd9Sstevel@tonic-gate /* XX64 don't need this once the compilers are fixed */
3487c478bd9Sstevel@tonic-gate #elif	defined(i386)
3497c478bd9Sstevel@tonic-gate 		if (elf_copy_gen(nlmp) == 0)
3507c478bd9Sstevel@tonic-gate 			return (0);
3517c478bd9Sstevel@tonic-gate #endif
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		if (COPY(nlmp) == 0)
3547c478bd9Sstevel@tonic-gate 			return (1);
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		if ((LIST(nlmp)->lm_flags & LML_FLG_TRC_ENABLE) &&
3577c478bd9Sstevel@tonic-gate 		    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
3587c478bd9Sstevel@tonic-gate 		    (LIST(nlmp)->lm_flags & LML_FLG_TRC_VERBOSE)))
3597c478bd9Sstevel@tonic-gate 			tracing = 1;
3607c478bd9Sstevel@tonic-gate 		else
3617c478bd9Sstevel@tonic-gate 			tracing = 0;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_util_nl());
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(COPY(nlmp), off1, lmpp)) {
3667c478bd9Sstevel@tonic-gate 			Rt_map *	lmp = *lmpp;
3677c478bd9Sstevel@tonic-gate 			Rel_copy *	rcp;
3687c478bd9Sstevel@tonic-gate 			Aliste		off2;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(COPY(lmp), off2, rcp)) {
3717c478bd9Sstevel@tonic-gate 				int zero;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 				/*
3747c478bd9Sstevel@tonic-gate 				 * Only copy the bits if it's from non-zero
3757c478bd9Sstevel@tonic-gate 				 * filled memory.
3767c478bd9Sstevel@tonic-gate 				 */
3777c478bd9Sstevel@tonic-gate 				zero = copy_zerobits(rcp->r_dlmp, rcp->r_dsym);
3787c478bd9Sstevel@tonic-gate 				DBG_CALL(Dbg_reloc_copy(NAME(rcp->r_dlmp),
3797c478bd9Sstevel@tonic-gate 				    NAME(nlmp), rcp->r_name, zero));
3807c478bd9Sstevel@tonic-gate 				if (zero)
3817c478bd9Sstevel@tonic-gate 					continue;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 				(void) memcpy(rcp->r_radd, rcp->r_dadd,
3847c478bd9Sstevel@tonic-gate 				    rcp->r_size);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 				if ((tracing == 0) || ((FLAGS1(rcp->r_dlmp) &
3877c478bd9Sstevel@tonic-gate 				    FL1_RT_DISPREL) == 0))
3887c478bd9Sstevel@tonic-gate 					continue;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_REL_CPYDISP),
3917c478bd9Sstevel@tonic-gate 				    demangle(rcp->r_name), NAME(rcp->r_dlmp));
3927c478bd9Sstevel@tonic-gate 			}
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_util_nl());
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		free(COPY(nlmp));
3987c478bd9Sstevel@tonic-gate 		COPY(nlmp) = 0;
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	return (1);
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate int
4047c478bd9Sstevel@tonic-gate relocate_lmc(Lm_list *lml, Aliste nlmco, Rt_map *nlmp)
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	int	lret = 1, pret = 1;
4077c478bd9Sstevel@tonic-gate 	Alist	*alp;
4087c478bd9Sstevel@tonic-gate 	Aliste	plmco;
4097c478bd9Sstevel@tonic-gate 	Lm_cntl	*plmc, *nlmc;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * If this link-map control list is being relocated, return.  The object
4137c478bd9Sstevel@tonic-gate 	 * that has just been added will be picked up by the existing relocation
4147c478bd9Sstevel@tonic-gate 	 * thread.  Note, this is only really meaningful during process init-
4157c478bd9Sstevel@tonic-gate 	 * ialization, as objects are added to the main link-map control list.
4167c478bd9Sstevel@tonic-gate 	 * Following this initialization, each family of objects that are loaded
4177c478bd9Sstevel@tonic-gate 	 * are added to a new link-map control list.
4187c478bd9Sstevel@tonic-gate 	 */
4197c478bd9Sstevel@tonic-gate 	/* LINTED */
4207c478bd9Sstevel@tonic-gate 	nlmc = (Lm_cntl *)((char *)lml->lm_lists + nlmco);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (nlmc->lc_flags & LMC_FLG_RELOCATING)
4237c478bd9Sstevel@tonic-gate 		return (1);
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	nlmc->lc_flags |= LMC_FLG_RELOCATING;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * Relocate one or more link-maps of a link map control list.  If this
4297c478bd9Sstevel@tonic-gate 	 * object doesn't belong to the present link-map control list then it
4307c478bd9Sstevel@tonic-gate 	 * must already have been relocated, or it is in the process of being
4317c478bd9Sstevel@tonic-gate 	 * relocated prior to us recursing into this relocation.  In either
4327c478bd9Sstevel@tonic-gate 	 * case, ignore the object as it's already being taken care of, however,
4337c478bd9Sstevel@tonic-gate 	 * fall through and capture any relocation promotions that might have
4347c478bd9Sstevel@tonic-gate 	 * been established from the reference mode of this object.
4357c478bd9Sstevel@tonic-gate 	 *
4367c478bd9Sstevel@tonic-gate 	 * If we're generating a configuration file using crle(1), two passes
4377c478bd9Sstevel@tonic-gate 	 * may be involved.  Under the first pass, RTLD_CONFGEN is set.  Under
4387c478bd9Sstevel@tonic-gate 	 * this pass, crle() loads objects into the process address space.  No
4397c478bd9Sstevel@tonic-gate 	 * relocation is necessary at this point, we simply need to analyze the
4407c478bd9Sstevel@tonic-gate 	 * objects to insure any directly bound dependencies, filtees, etc.
4417c478bd9Sstevel@tonic-gate 	 * get loaded. Although we skip the relocation, fall through to insure
4427c478bd9Sstevel@tonic-gate 	 * any control lists are maintained appropriately.
4437c478bd9Sstevel@tonic-gate 	 *
4447c478bd9Sstevel@tonic-gate 	 * If objects are to be dldump(3c)'ed, crle(1) makes a second pass,
4457c478bd9Sstevel@tonic-gate 	 * using RTLD_NOW and RTLD_CONFGEN.  The RTLD_NOW effectively carries
4467c478bd9Sstevel@tonic-gate 	 * out the relocations of all loaded objects.
4477c478bd9Sstevel@tonic-gate 	 */
4487c478bd9Sstevel@tonic-gate 	if ((nlmco == CNTL(nlmp)) &&
4497c478bd9Sstevel@tonic-gate 	    ((MODE(nlmp) & (RTLD_NOW | RTLD_CONFGEN)) != RTLD_CONFGEN)) {
4507c478bd9Sstevel@tonic-gate 		int	relocated = 0;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		/*
4537c478bd9Sstevel@tonic-gate 		 * Determine whether the initial link-map control list has
4547c478bd9Sstevel@tonic-gate 		 * started relocation.  From this point, should any interposing
4557c478bd9Sstevel@tonic-gate 		 * objects be added to this link-map control list, the objects
4567c478bd9Sstevel@tonic-gate 		 * are demoted to standard objects.  Their interposition can't
4577c478bd9Sstevel@tonic-gate 		 * be guaranteed once relocations have been carried out.
4587c478bd9Sstevel@tonic-gate 		 */
4597c478bd9Sstevel@tonic-gate 		if (nlmco == ALO_DATA)
4607c478bd9Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_STARTREL;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		/*
4637c478bd9Sstevel@tonic-gate 		 * Relocate the link-map control list.
4647c478bd9Sstevel@tonic-gate 		 */
4657c478bd9Sstevel@tonic-gate 		lret = _relocate_lmc(lml, nlmp, &relocated);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 		/*
4687c478bd9Sstevel@tonic-gate 		 * At this point we've completed the addition of a new group of
4697c478bd9Sstevel@tonic-gate 		 * objects, either the initial objects that start the process
4707c478bd9Sstevel@tonic-gate 		 * (called from setup()), a group added through lazy loading or
4717c478bd9Sstevel@tonic-gate 		 * filters, or from a dlopen() request.  Indicate to the
4727c478bd9Sstevel@tonic-gate 		 * debuggers that new objects have been added.
4737c478bd9Sstevel@tonic-gate 		 */
4747c478bd9Sstevel@tonic-gate 		if (relocated && lret &&
4757c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
4767c478bd9Sstevel@tonic-gate 			rd_event(lml, RD_DLACTIVITY, RT_ADD);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	/*
4807c478bd9Sstevel@tonic-gate 	 * Determine the new, and previous link-map control lists.
4817c478bd9Sstevel@tonic-gate 	 */
48224a6229eSrie 	/* LINTED */
48324a6229eSrie 	nlmc = (Lm_cntl *)((char *)lml->lm_lists + nlmco);
4846679fdc0Srie 	if (nlmco == ALO_DATA) {
4856679fdc0Srie 		plmco = nlmco;
4867c478bd9Sstevel@tonic-gate 		plmc = nlmc;
4876679fdc0Srie 	} else {
4887c478bd9Sstevel@tonic-gate 		plmco = nlmco - lml->lm_lists->al_size;
4897c478bd9Sstevel@tonic-gate 		/* LINTED */
4907c478bd9Sstevel@tonic-gate 		plmc = (Lm_cntl *)((char *)lml->lm_lists + plmco);
4917c478bd9Sstevel@tonic-gate 	}
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	/*
4947c478bd9Sstevel@tonic-gate 	 * Having completed this control list of objects, they can now be bound
4957c478bd9Sstevel@tonic-gate 	 * to from other objects.  Move this control list to the control list
4967c478bd9Sstevel@tonic-gate 	 * that precedes it.  Although this control list may have only bound to
4977c478bd9Sstevel@tonic-gate 	 * controls lists much higher up the control list stack, it must only
4987c478bd9Sstevel@tonic-gate 	 * be moved up one control list so as to preserve the link-map order
4997c478bd9Sstevel@tonic-gate 	 * that may have already been traversed in search of symbols.
5007c478bd9Sstevel@tonic-gate 	 */
5017c478bd9Sstevel@tonic-gate 	if (lret && (nlmco != ALO_DATA) && nlmc->lc_head)
5027c478bd9Sstevel@tonic-gate 		lm_move(lml, nlmco, plmco, nlmc, plmc);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * Determine whether existing objects that have already been relocated,
5067c478bd9Sstevel@tonic-gate 	 * need any additional relocations performed.  This can occur when new
5077c478bd9Sstevel@tonic-gate 	 * objects are loaded with RTLD_NOW, and these new objects have
5087c478bd9Sstevel@tonic-gate 	 * dependencies on objects that are already loaded.  Note, that we peel
5097c478bd9Sstevel@tonic-gate 	 * any relocation promotions off of one control list at a time.  This
5107c478bd9Sstevel@tonic-gate 	 * prevents relocations from being bound to objects that might yet fail
5117c478bd9Sstevel@tonic-gate 	 * to relocate themselves.
5127c478bd9Sstevel@tonic-gate 	 */
5137c478bd9Sstevel@tonic-gate 	while ((alp = plmc->lc_now) != 0) {
5147c478bd9Sstevel@tonic-gate 		Aliste	off;
5157c478bd9Sstevel@tonic-gate 		Rt_map	**lmpp;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		/*
5187c478bd9Sstevel@tonic-gate 		 * Remove the relocation promotion list, as performing more
5197c478bd9Sstevel@tonic-gate 		 * relocations may result in discovering more objects that need
5207c478bd9Sstevel@tonic-gate 		 * promotion.
5217c478bd9Sstevel@tonic-gate 		 */
5227c478bd9Sstevel@tonic-gate 		plmc->lc_now = 0;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(alp, off, lmpp)) {
5257c478bd9Sstevel@tonic-gate 			Rt_map	*lmp = *lmpp;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			/*
5287c478bd9Sstevel@tonic-gate 			 * If the original relocation of the link-map control
5297c478bd9Sstevel@tonic-gate 			 * list failed, or one of the relocation promotions of
5307c478bd9Sstevel@tonic-gate 			 * this loop has failed, demote any pending objects
5317c478bd9Sstevel@tonic-gate 			 * relocation mode.
5327c478bd9Sstevel@tonic-gate 			 */
5337c478bd9Sstevel@tonic-gate 			if ((lret == 0) || (pret == 0)) {
5347c478bd9Sstevel@tonic-gate 				MODE(lmp) &= ~RTLD_NOW;
5357c478bd9Sstevel@tonic-gate 				MODE(lmp) |= RTLD_LAZY;
5367c478bd9Sstevel@tonic-gate 				continue;
5377c478bd9Sstevel@tonic-gate 			}
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 			/*
5407c478bd9Sstevel@tonic-gate 			 * If a relocation fails, save the error condition.
5417c478bd9Sstevel@tonic-gate 			 * It's possible that all new objects on the original
5427c478bd9Sstevel@tonic-gate 			 * link-map control list have been relocated
5437c478bd9Sstevel@tonic-gate 			 * successfully, but if the user request requires
5447c478bd9Sstevel@tonic-gate 			 * promoting objects that have already been loaded, we
5457c478bd9Sstevel@tonic-gate 			 * have to indicate that this operation couldn't be
5467c478bd9Sstevel@tonic-gate 			 * performed.  The unrelocated objects are in use on
5477c478bd9Sstevel@tonic-gate 			 * another control list, and may continue to be used.
5487c478bd9Sstevel@tonic-gate 			 * If the .plt that resulted in the error is called,
5497c478bd9Sstevel@tonic-gate 			 * then the process will receive a fatal error at that
5507c478bd9Sstevel@tonic-gate 			 * time.  But, the .plt may never be called.
5517c478bd9Sstevel@tonic-gate 			 */
5527c478bd9Sstevel@tonic-gate 			if (relocate_so(lml, lmp, 0, 1) == 0)
5537c478bd9Sstevel@tonic-gate 				pret = 0;
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		/*
5577c478bd9Sstevel@tonic-gate 		 * Having promoted any objects, determine whether additional
5587c478bd9Sstevel@tonic-gate 		 * dependencies were added, and if so move them to the previous
5597c478bd9Sstevel@tonic-gate 		 * link-map control list.
5607c478bd9Sstevel@tonic-gate 		 */
56124a6229eSrie 		/* LINTED */
56224a6229eSrie 		nlmc = (Lm_cntl *)((char *)lml->lm_lists + nlmco);
56324a6229eSrie 		/* LINTED */
56424a6229eSrie 		plmc = (Lm_cntl *)((char *)lml->lm_lists + plmco);
5657c478bd9Sstevel@tonic-gate 		if ((nlmco != ALO_DATA) && nlmc->lc_head)
5667c478bd9Sstevel@tonic-gate 			lm_move(lml, nlmco, plmco, nlmc, plmc);
5677c478bd9Sstevel@tonic-gate 		(void) free(alp);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
57024a6229eSrie 	/*
57124a6229eSrie 	 * Indicate that relocations are no longer active for this control list.
57224a6229eSrie 	 */
57324a6229eSrie 	/* LINTED */
57424a6229eSrie 	nlmc = (Lm_cntl *)((char *)lml->lm_lists + nlmco);
5757c478bd9Sstevel@tonic-gate 	nlmc->lc_flags &= ~LMC_FLG_RELOCATING;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (lret && pret)
5787c478bd9Sstevel@tonic-gate 		return (1);
5797c478bd9Sstevel@tonic-gate 	else
5807c478bd9Sstevel@tonic-gate 		return (0);
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate /*
5847c478bd9Sstevel@tonic-gate  * Inherit the first rejection message for possible later diagnostics.
5857c478bd9Sstevel@tonic-gate  *
5867c478bd9Sstevel@tonic-gate  * Any attempt to process a file that is unsuccessful, should be accompanied
5877c478bd9Sstevel@tonic-gate  * with an error diagnostic.  However, some operations like searching for a
5887c478bd9Sstevel@tonic-gate  * simple filename, involve trying numerous paths, and an error message for each
5897c478bd9Sstevel@tonic-gate  * lookup is not required.  Although a multiple search can fail, it's possible
5907c478bd9Sstevel@tonic-gate  * that a file was found, but was rejected because it was the wrong type.
5917c478bd9Sstevel@tonic-gate  * To satisfy these possibilities, the first failure is recorded as a rejection
5927c478bd9Sstevel@tonic-gate  * message, and this message is used later for a more specific diagnostic.
5937c478bd9Sstevel@tonic-gate  *
5947c478bd9Sstevel@tonic-gate  * File searches are focused at load_one(), and from here a rejection descriptor
5957c478bd9Sstevel@tonic-gate  * is passed down to various child routines.  If these child routines can
5967c478bd9Sstevel@tonic-gate  * process multiple files, then they will maintain their own rejection desc-
5977c478bd9Sstevel@tonic-gate  * riptor.  This is filled in for any failures, and a diagnostic produced to
5987c478bd9Sstevel@tonic-gate  * reflect the failure.  The child routines then employ rejection_inherit() to
5997c478bd9Sstevel@tonic-gate  * pass the first rejection message back to load_one().
6007c478bd9Sstevel@tonic-gate  *
6017c478bd9Sstevel@tonic-gate  * Note that the name, and rejection string must be duplicated, as the name
6027c478bd9Sstevel@tonic-gate  * buffer and error string buffer (see conv_ routines) may be reused for
6037c478bd9Sstevel@tonic-gate  * additional processing or rejection messages.
6047c478bd9Sstevel@tonic-gate  *
6057c478bd9Sstevel@tonic-gate  * As this routine is called to clean up after a failed open, remove any
6067c478bd9Sstevel@tonic-gate  * resolved pathname that might have been allocated as the file was processed.
6077c478bd9Sstevel@tonic-gate  */
6087c478bd9Sstevel@tonic-gate void
6097c478bd9Sstevel@tonic-gate rejection_inherit(Rej_desc *rej1, Rej_desc *rej2, Fdesc *fdp)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate 	if (rej2->rej_type && (rej1->rej_type == 0)) {
6127c478bd9Sstevel@tonic-gate 		rej1->rej_type = rej2->rej_type;
6137c478bd9Sstevel@tonic-gate 		rej1->rej_info = rej2->rej_info;
6147c478bd9Sstevel@tonic-gate 		rej1->rej_flag = rej2->rej_flag;
6157c478bd9Sstevel@tonic-gate 		if (rej2->rej_name)
6167c478bd9Sstevel@tonic-gate 			rej1->rej_name = strdup(rej2->rej_name);
6177c478bd9Sstevel@tonic-gate 		if (rej2->rej_str) {
6187c478bd9Sstevel@tonic-gate 			if ((rej1->rej_str = strdup(rej2->rej_str)) == 0)
6197c478bd9Sstevel@tonic-gate 				rej1->rej_str = MSG_ORIG(MSG_EMG_ENOMEM);
6207c478bd9Sstevel@tonic-gate 		}
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate 	if (fdp && fdp->fd_nname && fdp->fd_pname &&
6237c478bd9Sstevel@tonic-gate 	    (fdp->fd_pname != fdp->fd_nname)) {
6247c478bd9Sstevel@tonic-gate 		free((void *)fdp->fd_pname);
6257c478bd9Sstevel@tonic-gate 		fdp->fd_pname = 0;
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate /*
6307c478bd9Sstevel@tonic-gate  * Determine the object type of a file.
6317c478bd9Sstevel@tonic-gate  */
6327c478bd9Sstevel@tonic-gate Fct *
6337c478bd9Sstevel@tonic-gate are_u_this(Rej_desc *rej, int fd, struct stat *status, const char *name)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	int	i;
6367c478bd9Sstevel@tonic-gate 	char	*maddr = 0;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	fmap->fm_fsize = status->st_size;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	/*
6417c478bd9Sstevel@tonic-gate 	 * If this is a directory (which can't be mmap()'ed) generate a precise
6427c478bd9Sstevel@tonic-gate 	 * error message.
6437c478bd9Sstevel@tonic-gate 	 */
6447c478bd9Sstevel@tonic-gate 	if ((status->st_mode & S_IFMT) == S_IFDIR) {
6457c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
6467c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(EISDIR);
6477c478bd9Sstevel@tonic-gate 		return (0);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	/*
6517c478bd9Sstevel@tonic-gate 	 * Map in the first page of the file.  When this buffer is first used,
6527c478bd9Sstevel@tonic-gate 	 * the mapping is a single system page.  This is typically enough to
6537c478bd9Sstevel@tonic-gate 	 * inspect the ehdr and phdrs of the file, and can be reused for each
6547c478bd9Sstevel@tonic-gate 	 * file that get loaded.  If a larger mapping is required to read the
6557c478bd9Sstevel@tonic-gate 	 * ehdr and phdrs, a new mapping is created (see elf_map_it()).  This
6567c478bd9Sstevel@tonic-gate 	 * new mapping is again used for each new file loaded.  Some objects,
6577c478bd9Sstevel@tonic-gate 	 * such as filters, only take up one page, and in this case this mapping
6587c478bd9Sstevel@tonic-gate 	 * will suffice for the file.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 	maddr = mmap(fmap->fm_maddr, fmap->fm_msize, (PROT_READ | PROT_EXEC),
6617c478bd9Sstevel@tonic-gate 	    fmap->fm_mflags, fd, 0);
6627c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
6637c478bd9Sstevel@tonic-gate 	if ((maddr == MAP_FAILED) && (errno == EINVAL)) {
6647c478bd9Sstevel@tonic-gate 		/*
6657c478bd9Sstevel@tonic-gate 		 * If the mapping failed, and we used MAP_ALIGN, assume we're
6667c478bd9Sstevel@tonic-gate 		 * on a system that doesn't support this option.  Try again
6677c478bd9Sstevel@tonic-gate 		 * without MAP_ALIGN.
6687c478bd9Sstevel@tonic-gate 		 */
6697c478bd9Sstevel@tonic-gate 		if (fmap->fm_mflags & MAP_ALIGN) {
6707c478bd9Sstevel@tonic-gate 			rtld_flags2 |= RT_FL2_NOMALIGN;
6717c478bd9Sstevel@tonic-gate 			fmap_setup();
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 			maddr = (char *)mmap(fmap->fm_maddr, fmap->fm_msize,
6747c478bd9Sstevel@tonic-gate 			    (PROT_READ | PROT_EXEC), fmap->fm_mflags, fd, 0);
6757c478bd9Sstevel@tonic-gate 		}
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate #endif
6787c478bd9Sstevel@tonic-gate 	if (maddr == MAP_FAILED) {
6797c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
6807c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(errno);
6817c478bd9Sstevel@tonic-gate 		return (0);
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	/*
6857c478bd9Sstevel@tonic-gate 	 * From now on we will re-use fmap->fm_maddr as the mapping address
6867c478bd9Sstevel@tonic-gate 	 * so we augment the flags with MAP_FIXED and drop any MAP_ALIGN.
6877c478bd9Sstevel@tonic-gate 	 */
6887c478bd9Sstevel@tonic-gate 	fmap->fm_maddr = maddr;
6897c478bd9Sstevel@tonic-gate 	fmap->fm_mflags |= MAP_FIXED;
6907c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
6917c478bd9Sstevel@tonic-gate 	fmap->fm_mflags &= ~MAP_ALIGN;
6927c478bd9Sstevel@tonic-gate #endif
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	/*
6957c478bd9Sstevel@tonic-gate 	 * Search through the object vectors to determine what kind of
6967c478bd9Sstevel@tonic-gate 	 * object we have.
6977c478bd9Sstevel@tonic-gate 	 */
6987c478bd9Sstevel@tonic-gate 	for (i = 0; vector[i]; i++) {
6997c478bd9Sstevel@tonic-gate 		if ((vector[i]->fct_are_u_this)(rej))
7007c478bd9Sstevel@tonic-gate 			return (vector[i]);
7017c478bd9Sstevel@tonic-gate 		else if (rej->rej_type) {
7027c478bd9Sstevel@tonic-gate 			Rt_map	*lmp;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 			/*
7057c478bd9Sstevel@tonic-gate 			 * If this object is an explicitly defined shared
7067c478bd9Sstevel@tonic-gate 			 * object under inspection by ldd, and contains a
7077c478bd9Sstevel@tonic-gate 			 * incompatible hardware capabilities requirement, then
7087c478bd9Sstevel@tonic-gate 			 * inform the user, but continue processing.
7097c478bd9Sstevel@tonic-gate 			 *
7107c478bd9Sstevel@tonic-gate 			 * XXXX - ldd -v for any rej failure.
7117c478bd9Sstevel@tonic-gate 			 */
7127c478bd9Sstevel@tonic-gate 			if ((rej->rej_type == SGS_REJ_HWCAP_1) &&
7137c478bd9Sstevel@tonic-gate 			    (lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) &&
7147c478bd9Sstevel@tonic-gate 			    ((lmp = lml_main.lm_head) != 0) &&
7157c478bd9Sstevel@tonic-gate 			    (FLAGS1(lmp) & FL1_RT_LDDSTUB) &&
7167c478bd9Sstevel@tonic-gate 			    (NEXT(lmp) == 0)) {
7177c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_GEN_HWCAP_1),
7187c478bd9Sstevel@tonic-gate 				    name, rej->rej_str);
7197c478bd9Sstevel@tonic-gate 				return (vector[i]);
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 			return (0);
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 	}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/*
7267c478bd9Sstevel@tonic-gate 	 * Unknown file type.
7277c478bd9Sstevel@tonic-gate 	 */
7287c478bd9Sstevel@tonic-gate 	rej->rej_type = SGS_REJ_UNKFILE;
7297c478bd9Sstevel@tonic-gate 	return (0);
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate /*
7347c478bd9Sstevel@tonic-gate  * Function that determines whether a file name has already been loaded; if so,
7357c478bd9Sstevel@tonic-gate  * returns a pointer to its link map structure; else returns a NULL pointer.
7367c478bd9Sstevel@tonic-gate  */
7377c478bd9Sstevel@tonic-gate static int
7387c478bd9Sstevel@tonic-gate _is_so_matched(const char *name, const char *str, int base)
7397c478bd9Sstevel@tonic-gate {
7407c478bd9Sstevel@tonic-gate 	const char	*_str;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (base && ((_str = strrchr(str, '/')) != NULL))
7437c478bd9Sstevel@tonic-gate 		_str++;
7447c478bd9Sstevel@tonic-gate 	else
7457c478bd9Sstevel@tonic-gate 		_str = str;
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	return (strcmp(name, _str));
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate static Rt_map *
7517c478bd9Sstevel@tonic-gate is_so_matched(Rt_map *lmp, const char *name, int base)
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	Aliste		off;
7547c478bd9Sstevel@tonic-gate 	const char	**cpp;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * Typically, dependencies are specified as simple file names
7587c478bd9Sstevel@tonic-gate 	 * (DT_NEEDED == libc.so.1), which are expanded to full pathnames to
7597c478bd9Sstevel@tonic-gate 	 * open the file.  The full pathname is NAME(), and the original name
7607c478bd9Sstevel@tonic-gate 	 * is maintained on the ALIAS() list. Look through the ALIAS list first,
7617c478bd9Sstevel@tonic-gate 	 * as this is most likely to match other dependency uses.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(ALIAS(lmp), off, cpp)) {
7647c478bd9Sstevel@tonic-gate 		if (_is_so_matched(name, *cpp, base) == 0)
7657c478bd9Sstevel@tonic-gate 			return (lmp);
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	/*
7697c478bd9Sstevel@tonic-gate 	 * Finally compare full paths, this is sometimes useful for catching
7707c478bd9Sstevel@tonic-gate 	 * filter names, or for those that dlopen() the dynamic executable.
7717c478bd9Sstevel@tonic-gate 	 */
7727c478bd9Sstevel@tonic-gate 	if (_is_so_matched(name, NAME(lmp), base) == 0)
7737c478bd9Sstevel@tonic-gate 		return (lmp);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	if (PATHNAME(lmp) != NAME(lmp)) {
7767c478bd9Sstevel@tonic-gate 		if (_is_so_matched(name, PATHNAME(lmp), base) == 0)
7777c478bd9Sstevel@tonic-gate 			return (lmp);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	return (0);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate Rt_map *
7837c478bd9Sstevel@tonic-gate is_so_loaded(Lm_list *lml, const char *name, int base)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
7867c478bd9Sstevel@tonic-gate 	const char	*_name;
7877c478bd9Sstevel@tonic-gate 	avl_index_t	where;
7887c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc;
7897c478bd9Sstevel@tonic-gate 	Aliste		off;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	/*
7927c478bd9Sstevel@tonic-gate 	 * If we've been asked to do a basename search, first determine if
7937c478bd9Sstevel@tonic-gate 	 * the pathname is registered in the FullpathNode AVL tree.
7947c478bd9Sstevel@tonic-gate 	 */
7957c478bd9Sstevel@tonic-gate 	if (base && (name[0] == '/') &&
7967c478bd9Sstevel@tonic-gate 	    ((lmp = fpavl_loaded(lml, name, &where)) != NULL) &&
7977c478bd9Sstevel@tonic-gate 	    ((FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE)) == 0))
7987c478bd9Sstevel@tonic-gate 		return (lmp);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	/*
8017c478bd9Sstevel@tonic-gate 	 * If we've been asked to do a basename search reduce the input name
8027c478bd9Sstevel@tonic-gate 	 * to its basename.
8037c478bd9Sstevel@tonic-gate 	 */
8047c478bd9Sstevel@tonic-gate 	if (base && ((_name = strrchr(name, '/')) != NULL))
8057c478bd9Sstevel@tonic-gate 		_name++;
8067c478bd9Sstevel@tonic-gate 	else
8077c478bd9Sstevel@tonic-gate 		_name = name;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/*
8107c478bd9Sstevel@tonic-gate 	 * Loop through the callers link-map lists.
8117c478bd9Sstevel@tonic-gate 	 */
8127c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(lml->lm_lists, off, lmc)) {
8137c478bd9Sstevel@tonic-gate 		for (lmp = lmc->lc_head; lmp; lmp = (Rt_map *)NEXT(lmp)) {
8147c478bd9Sstevel@tonic-gate 			if (FLAGS(lmp) & (FLG_RT_OBJECT | FLG_RT_DELETE))
8157c478bd9Sstevel@tonic-gate 				continue;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 			if (is_so_matched(lmp, _name, base))
8187c478bd9Sstevel@tonic-gate 				return (lmp);
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 	return ((Rt_map *)0);
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * Tracing is enabled by the LD_TRACE_LOADED_OPTIONS environment variable which
8277c478bd9Sstevel@tonic-gate  * is normally set from ldd(1).  For each link map we load, print the load name
8287c478bd9Sstevel@tonic-gate  * and the full pathname of the shared object.
8297c478bd9Sstevel@tonic-gate  */
8307c478bd9Sstevel@tonic-gate /* ARGSUSED4 */
8317c478bd9Sstevel@tonic-gate static void
8327c478bd9Sstevel@tonic-gate trace_so(Rt_map *clmp, Rej_desc *rej, const char *name, const char *path,
8337c478bd9Sstevel@tonic-gate     int alter, const char *nfound)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 	const char	*str = MSG_ORIG(MSG_STR_EMPTY);
8367c478bd9Sstevel@tonic-gate 	const char	*reject = MSG_ORIG(MSG_STR_EMPTY);
8377c478bd9Sstevel@tonic-gate 	char		_reject[PATH_MAX];
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	/*
8407c478bd9Sstevel@tonic-gate 	 * The first time through trace_so() will only have lddstub on the
8417c478bd9Sstevel@tonic-gate 	 * link-map list and the preloaded shared object is supplied as "path".
8427c478bd9Sstevel@tonic-gate 	 * As we don't want to print this shared object as a dependency, but
8437c478bd9Sstevel@tonic-gate 	 * instead inspect *its* dependencies, return.
8447c478bd9Sstevel@tonic-gate 	 */
8457c478bd9Sstevel@tonic-gate 	if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
8467c478bd9Sstevel@tonic-gate 		return;
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 	/*
8497c478bd9Sstevel@tonic-gate 	 * Without any rejection info, this is a supplied not-found condition.
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 	if (rej && (rej->rej_type == 0)) {
8527c478bd9Sstevel@tonic-gate 		(void) printf(nfound, name);
8537c478bd9Sstevel@tonic-gate 		return;
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/*
8577c478bd9Sstevel@tonic-gate 	 * If rejection information exists then establish what object was
8587c478bd9Sstevel@tonic-gate 	 * found and the reason for its rejection.
8597c478bd9Sstevel@tonic-gate 	 */
8607c478bd9Sstevel@tonic-gate 	if (rej) {
8617c478bd9Sstevel@tonic-gate 		/* LINTED */
8627c478bd9Sstevel@tonic-gate 		(void) snprintf(_reject, PATH_MAX,
8637c478bd9Sstevel@tonic-gate 		    MSG_INTL(ldd_reject[rej->rej_type]), conv_reject_str(rej));
8647c478bd9Sstevel@tonic-gate 		if (rej->rej_name)
8657c478bd9Sstevel@tonic-gate 			path = rej->rej_name;
8667c478bd9Sstevel@tonic-gate 		reject = (char *)_reject;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 		/*
8697c478bd9Sstevel@tonic-gate 		 * Was an alternative pathname defined (from a configuration
8707c478bd9Sstevel@tonic-gate 		 * file).
8717c478bd9Sstevel@tonic-gate 		 */
8727c478bd9Sstevel@tonic-gate 		if (rej->rej_flag & FLG_FD_ALTER)
8737c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
8747c478bd9Sstevel@tonic-gate 	} else {
8757c478bd9Sstevel@tonic-gate 		if (alter)
8767c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_LDD_FIL_ALTER);
8777c478bd9Sstevel@tonic-gate 	}
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	/*
8807c478bd9Sstevel@tonic-gate 	 * If the load name isn't a full pathname print its associated pathname
8817c478bd9Sstevel@tonic-gate 	 * together with all the other information we've gathered.
8827c478bd9Sstevel@tonic-gate 	 */
8837c478bd9Sstevel@tonic-gate 	if (*name == '/')
8847c478bd9Sstevel@tonic-gate 	    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH), path, str, reject);
8857c478bd9Sstevel@tonic-gate 	else
8867c478bd9Sstevel@tonic-gate 	    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV), name, path, str, reject);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * Establish a link-map mode, initializing it if it has just been loaded, or
8927c478bd9Sstevel@tonic-gate  * potentially updating it if it already exists.
8937c478bd9Sstevel@tonic-gate  */
8947c478bd9Sstevel@tonic-gate int
8957c478bd9Sstevel@tonic-gate update_mode(Rt_map *lmp, int omode, int nmode)
8967c478bd9Sstevel@tonic-gate {
897dffec89cSrie 	Lm_list	*lml = LIST(lmp);
8987c478bd9Sstevel@tonic-gate 	int	pmode = 0;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	/*
9017c478bd9Sstevel@tonic-gate 	 * A newly loaded object hasn't had its mode set yet.  Modes are used to
9027c478bd9Sstevel@tonic-gate 	 * load dependencies, so don't propagate any parent or no-load flags, as
9037c478bd9Sstevel@tonic-gate 	 * these would adversely affect this objects ability to load any of its
9047c478bd9Sstevel@tonic-gate 	 * dependencies that aren't already loaded.  RTLD_FIRST is applicable to
9057c478bd9Sstevel@tonic-gate 	 * this objects handle creation only, and should not be propagated.
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	if ((FLAGS(lmp) & FLG_RT_MODESET) == 0) {
9087c478bd9Sstevel@tonic-gate 		MODE(lmp) |= nmode & ~(RTLD_PARENT | RTLD_NOLOAD | RTLD_FIRST);
9097c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_MODESET;
9107c478bd9Sstevel@tonic-gate 		return (1);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/*
9147c478bd9Sstevel@tonic-gate 	 * Establish any new overriding modes.  RTLD_LAZY and RTLD_NOW should be
9157c478bd9Sstevel@tonic-gate 	 * represented individually (this is historic, as these two flags were
9167c478bd9Sstevel@tonic-gate 	 * the only flags originally available to dlopen()).  Other flags are
9177c478bd9Sstevel@tonic-gate 	 * accumulative, but have a hierarchy of preference.
9187c478bd9Sstevel@tonic-gate 	 */
9197c478bd9Sstevel@tonic-gate 	if ((omode & RTLD_LAZY) && (nmode & RTLD_NOW)) {
9207c478bd9Sstevel@tonic-gate 		MODE(lmp) &= ~RTLD_LAZY;
9217c478bd9Sstevel@tonic-gate 		pmode |= RTLD_NOW;
9227c478bd9Sstevel@tonic-gate 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	pmode |= ((~omode & nmode) &
9257c478bd9Sstevel@tonic-gate 	    (RTLD_GLOBAL | RTLD_WORLD | RTLD_NODELETE));
9267c478bd9Sstevel@tonic-gate 	if (pmode) {
9277c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_mode_promote(NAME(lmp), pmode));
9287c478bd9Sstevel@tonic-gate 		MODE(lmp) |= pmode;
9297c478bd9Sstevel@tonic-gate 	}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	/*
9327c478bd9Sstevel@tonic-gate 	 * If this load is an RTLD_NOW request and the object has already been
9337c478bd9Sstevel@tonic-gate 	 * loaded non-RTLD_NOW, append this object to the relocation-now list
9347c478bd9Sstevel@tonic-gate 	 * of the objects associated control list.  Note, if the object hasn't
9357c478bd9Sstevel@tonic-gate 	 * yet been relocated, setting its MODE() to RTLD_NOW will establish
9367c478bd9Sstevel@tonic-gate 	 * full relocation processing when it eventually gets relocated.
9377c478bd9Sstevel@tonic-gate 	 */
9387c478bd9Sstevel@tonic-gate 	if ((pmode & RTLD_NOW) &&
9397c478bd9Sstevel@tonic-gate 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))) {
9407c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 		/* LINTED */
9437c478bd9Sstevel@tonic-gate 		lmc = (Lm_cntl *)((char *)(LIST(lmp)->lm_lists) + CNTL(lmp));
9447c478bd9Sstevel@tonic-gate 		(void) alist_append(&(lmc->lc_now), &lmp, sizeof (Rt_map *),
9457c478bd9Sstevel@tonic-gate 		    AL_CNT_LMNOW);
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
9497c478bd9Sstevel@tonic-gate 	/*
9507c478bd9Sstevel@tonic-gate 	 * For patch backward compatibility the following .init collection
9517c478bd9Sstevel@tonic-gate 	 * is disabled.
9527c478bd9Sstevel@tonic-gate 	 */
9537c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DISFIX_1)
9547c478bd9Sstevel@tonic-gate 		return (pmode);
9557c478bd9Sstevel@tonic-gate #endif
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	/*
958dffec89cSrie 	 * If this objects .init has been collected but has not yet been called,
959dffec89cSrie 	 * it may be necessary to reevaluate the object using tsort().  For
9607c478bd9Sstevel@tonic-gate 	 * example, a new dlopen() hierarchy may bind to uninitialized objects
9617c478bd9Sstevel@tonic-gate 	 * that are already loaded, or a dlopen(RTLD_NOW) can establish new
9627c478bd9Sstevel@tonic-gate 	 * bindings between already loaded objects that require the tsort()
963dffec89cSrie 	 * information be recomputed.  If however, no new objects have been
964dffec89cSrie 	 * added to the process, and this object hasn't been promoted, don't
965dffec89cSrie 	 * bother reevaluating the .init.  The present tsort() information is
966dffec89cSrie 	 * probably as accurate as necessary, and by not establishing a parallel
967dffec89cSrie 	 * tsort() we can help reduce the amount of recursion possible between
968dffec89cSrie 	 * .inits.
9697c478bd9Sstevel@tonic-gate 	 */
970dffec89cSrie 	if (((FLAGS(lmp) &
971dffec89cSrie 	    (FLG_RT_INITCLCT | FLG_RT_INITCALL)) == FLG_RT_INITCLCT) &&
972dffec89cSrie 	    ((lml->lm_flags & LML_FLG_OBJADDED) || ((pmode & RTLD_NOW) &&
973dffec89cSrie 	    (FLAGS(lmp) & (FLG_RT_RELOCED | FLG_RT_RELOCING))))) {
9747c478bd9Sstevel@tonic-gate 		FLAGS(lmp) &= ~FLG_RT_INITCLCT;
9757c478bd9Sstevel@tonic-gate 		LIST(lmp)->lm_init++;
976dffec89cSrie 		LIST(lmp)->lm_flags |= LML_FLG_OBJREEVAL;
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	return (pmode);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate  * Determine whether an alias name already exists, and if not create one.  This
9847c478bd9Sstevel@tonic-gate  * is typically used to retain dependency names, such as "libc.so.1", which
9857c478bd9Sstevel@tonic-gate  * would have been expanded to full path names when they were loaded.  The
9867c478bd9Sstevel@tonic-gate  * full path names (NAME() and possibly PATHNAME()) are maintained as Fullpath
9877c478bd9Sstevel@tonic-gate  * AVL nodes, and thus would have been matched by fpavl_loaded() during
9887c478bd9Sstevel@tonic-gate  * file_open().
9897c478bd9Sstevel@tonic-gate  */
9907c478bd9Sstevel@tonic-gate int
9917c478bd9Sstevel@tonic-gate append_alias(Rt_map *lmp, const char *str, int *added)
9927c478bd9Sstevel@tonic-gate {
9937c478bd9Sstevel@tonic-gate 	Aliste	off;
9947c478bd9Sstevel@tonic-gate 	char	**cpp, *cp;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	/*
9977c478bd9Sstevel@tonic-gate 	 * Determine if this filename is already on the alias list.
9987c478bd9Sstevel@tonic-gate 	 */
9997c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(ALIAS(lmp), off, cpp)) {
10007c478bd9Sstevel@tonic-gate 		if (strcmp(*cpp, str) == 0)
10017c478bd9Sstevel@tonic-gate 			return (1);
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 	/*
10057c478bd9Sstevel@tonic-gate 	 * This is a new alias, append it to the alias list.
10067c478bd9Sstevel@tonic-gate 	 */
10077c478bd9Sstevel@tonic-gate 	if ((cp = strdup(str)) == 0)
10087c478bd9Sstevel@tonic-gate 		return (0);
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	if (alist_append(&ALIAS(lmp), &cp, sizeof (char *),
10117c478bd9Sstevel@tonic-gate 	    AL_CNT_ALIAS) == 0) {
10127c478bd9Sstevel@tonic-gate 		free(cp);
10137c478bd9Sstevel@tonic-gate 		return (0);
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 	if (added)
10167c478bd9Sstevel@tonic-gate 		*added = 1;
10177c478bd9Sstevel@tonic-gate 	return (1);
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate /*
10217c478bd9Sstevel@tonic-gate  * Determine whether a file is already loaded by comparing device and inode
10227c478bd9Sstevel@tonic-gate  * values.
10237c478bd9Sstevel@tonic-gate  */
10247c478bd9Sstevel@tonic-gate static Rt_map *
10257c478bd9Sstevel@tonic-gate is_devinode_loaded(struct stat *status, Lm_list *lml, const char *name,
10267c478bd9Sstevel@tonic-gate     uint_t flags)
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
10297c478bd9Sstevel@tonic-gate 	Aliste	off;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	/*
10327c478bd9Sstevel@tonic-gate 	 * If this is an auditor, it will have been opened on a new link-map.
10337c478bd9Sstevel@tonic-gate 	 * To prevent multiple occurrances of the same auditor on multiple
10347c478bd9Sstevel@tonic-gate 	 * link-maps, search the head of each link-map list and see if this
10357c478bd9Sstevel@tonic-gate 	 * object is already loaded as an auditor.
10367c478bd9Sstevel@tonic-gate 	 */
10377c478bd9Sstevel@tonic-gate 	if (flags & FLG_RT_AUDIT) {
10387c478bd9Sstevel@tonic-gate 		Lm_list *	lml;
10397c478bd9Sstevel@tonic-gate 		Listnode *	lnp;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
10427c478bd9Sstevel@tonic-gate 			Rt_map	*nlmp = lml->lm_head;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 			if (nlmp && ((FLAGS(nlmp) &
10457c478bd9Sstevel@tonic-gate 			    (FLG_RT_AUDIT | FLG_RT_DELETE)) == FLG_RT_AUDIT) &&
10467c478bd9Sstevel@tonic-gate 			    (STDEV(nlmp) == status->st_dev) &&
10477c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) == status->st_ino))
10487c478bd9Sstevel@tonic-gate 				return (nlmp);
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 		return ((Rt_map *)0);
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	/*
10547c478bd9Sstevel@tonic-gate 	 * If the file has been found determine from the new files status
10557c478bd9Sstevel@tonic-gate 	 * information if this file is actually linked to one we already have
10567c478bd9Sstevel@tonic-gate 	 * mapped.  This catches symlink names not caught by is_so_loaded().
10577c478bd9Sstevel@tonic-gate 	 */
10587c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(lml->lm_lists, off, lmc)) {
10597c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 		for (nlmp = lmc->lc_head; nlmp; nlmp = (Rt_map *)NEXT(nlmp)) {
10627c478bd9Sstevel@tonic-gate 			if ((FLAGS(nlmp) & FLG_RT_DELETE) ||
10637c478bd9Sstevel@tonic-gate 			    (FLAGS1(nlmp) & FL1_RT_LDDSTUB))
10647c478bd9Sstevel@tonic-gate 				continue;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 			if ((STDEV(nlmp) != status->st_dev) ||
10677c478bd9Sstevel@tonic-gate 			    (STINO(nlmp) != status->st_ino))
10687c478bd9Sstevel@tonic-gate 				continue;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_VERBOSE) {
10717c478bd9Sstevel@tonic-gate 				if (*name == '/')
10727c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
10737c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
10747c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
10757c478bd9Sstevel@tonic-gate 				else
10767c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
10777c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
10787c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
10797c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
10807c478bd9Sstevel@tonic-gate 			}
10817c478bd9Sstevel@tonic-gate 			return (nlmp);
10827c478bd9Sstevel@tonic-gate 		}
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate 	return ((Rt_map *)0);
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate /*
10887c478bd9Sstevel@tonic-gate  * Generate any error messages indicating a file could not be found.  When
10897c478bd9Sstevel@tonic-gate  * preloading or auditing a secure application, it can be a little more helpful
10907c478bd9Sstevel@tonic-gate  * to indicate that a search of secure directories has failed, so adjust the
10917c478bd9Sstevel@tonic-gate  * messages accordingly.
10927c478bd9Sstevel@tonic-gate  */
10937c478bd9Sstevel@tonic-gate void
10947c478bd9Sstevel@tonic-gate file_notfound(Lm_list *lml, const char *name, Rt_map *clmp, uint_t flags,
10957c478bd9Sstevel@tonic-gate     Rej_desc * rej)
10967c478bd9Sstevel@tonic-gate {
10977c478bd9Sstevel@tonic-gate 	int	secure = 0;
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SECURE) &&
11007c478bd9Sstevel@tonic-gate 	    (flags & (FLG_RT_PRELOAD | FLG_RT_AUDIT)))
11017c478bd9Sstevel@tonic-gate 		secure++;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
11047c478bd9Sstevel@tonic-gate 		/*
11057c478bd9Sstevel@tonic-gate 		 * Under ldd(1), auxiliary filtees that can't be loaded are
11067c478bd9Sstevel@tonic-gate 		 * ignored, unless verbose errors are requested.
11077c478bd9Sstevel@tonic-gate 		 */
11087c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) &&
11097c478bd9Sstevel@tonic-gate 		    ((lml->lm_flags & LML_FLG_TRC_VERBOSE) == 0))
11107c478bd9Sstevel@tonic-gate 			return;
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		if (secure)
11137c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
11147c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_SEC_NFOUND));
11157c478bd9Sstevel@tonic-gate 		else
11167c478bd9Sstevel@tonic-gate 			trace_so(clmp, rej, name, 0, 0,
11177c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_LDD_FIL_NFOUND));
11187c478bd9Sstevel@tonic-gate 		return;
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
11227c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(err_reject[rej->rej_type]),
11237c478bd9Sstevel@tonic-gate 		    rej->rej_name ? rej->rej_name : MSG_INTL(MSG_STR_UNKNOWN),
11247c478bd9Sstevel@tonic-gate 		    conv_reject_str(rej));
11257c478bd9Sstevel@tonic-gate 		return;
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (secure)
11297c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_SEC_OPEN), name);
11307c478bd9Sstevel@tonic-gate 	else
11317c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), name,
11327c478bd9Sstevel@tonic-gate 		    strerror(ENOENT));
11337c478bd9Sstevel@tonic-gate }
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate static int
11367c478bd9Sstevel@tonic-gate file_open(int err, Lm_list *lml, const char *oname, const char *nname,
11377c478bd9Sstevel@tonic-gate     Rt_map *clmp, uint_t flags, Fdesc * fdesc, Rej_desc *rej)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	struct stat	status;
11407c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	fdesc->fd_oname = oname;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	if ((err == 0) && (fdesc->fd_flags & FLG_FD_ALTER))
11457c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_config_obj(oname, 0, nname));
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	/*
11487c478bd9Sstevel@tonic-gate 	 * If we're dealing with a full pathname, determine whether this
11497c478bd9Sstevel@tonic-gate 	 * pathname is already known.  Other pathnames fall through to the
11507c478bd9Sstevel@tonic-gate 	 * dev/inode check, as even though the pathname may look the same as
11517c478bd9Sstevel@tonic-gate 	 * one previously used, the process may have changed directory.
11527c478bd9Sstevel@tonic-gate 	 */
11537c478bd9Sstevel@tonic-gate 	if ((err == 0) && (nname[0] == '/')) {
11547c478bd9Sstevel@tonic-gate 		if ((nlmp = fpavl_loaded(lml, nname,
11557c478bd9Sstevel@tonic-gate 		    &(fdesc->fd_avlwhere))) != NULL) {
11567c478bd9Sstevel@tonic-gate 			fdesc->fd_nname = nname;
11577c478bd9Sstevel@tonic-gate 			fdesc->fd_lmp = nlmp;
11587c478bd9Sstevel@tonic-gate 			return (1);
11597c478bd9Sstevel@tonic-gate 		}
11607c478bd9Sstevel@tonic-gate 	}
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 	if ((err == 0) && ((stat(nname, &status)) != -1)) {
11637c478bd9Sstevel@tonic-gate 		char	path[PATH_MAX];
11647c478bd9Sstevel@tonic-gate 		int	fd, size, added;
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 		/*
11677c478bd9Sstevel@tonic-gate 		 * If this path has been constructed as part of expanding a
11687c478bd9Sstevel@tonic-gate 		 * HWCAP directory, ignore any subdirectories.  For any other
11697c478bd9Sstevel@tonic-gate 		 * reference that expands to a directory, fall through to
11707c478bd9Sstevel@tonic-gate 		 * contruct a meaningful rejection message.
11717c478bd9Sstevel@tonic-gate 		 */
11727c478bd9Sstevel@tonic-gate 		if ((flags & FLG_RT_HWCAP) &&
11737c478bd9Sstevel@tonic-gate 		    ((status.st_mode & S_IFMT) == S_IFDIR))
11747c478bd9Sstevel@tonic-gate 			return (0);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		/*
11777c478bd9Sstevel@tonic-gate 		 * Resolve the filename and determine whether the resolved name
11787c478bd9Sstevel@tonic-gate 		 * is already known.  Typically, the previous fpavl_loaded()
11797c478bd9Sstevel@tonic-gate 		 * will have caught this, as both NAME() and PATHNAME() for a
11807c478bd9Sstevel@tonic-gate 		 * link-map are recorded in the FullNode AVL tree.  However,
11817c478bd9Sstevel@tonic-gate 		 * instances exist where a file can be replaced (loop-back
11827c478bd9Sstevel@tonic-gate 		 * mounts, bfu, etc.), and reference is made to the original
11837c478bd9Sstevel@tonic-gate 		 * file through a symbolic link.  By checking the pathname here,
11847c478bd9Sstevel@tonic-gate 		 * we don't fall through to the dev/inode check and conclude
11857c478bd9Sstevel@tonic-gate 		 * that a new file should be loaded.
11867c478bd9Sstevel@tonic-gate 		 */
11877c478bd9Sstevel@tonic-gate 		if ((nname[0] == '/') && (rtld_flags & RT_FL_EXECNAME) &&
11887c478bd9Sstevel@tonic-gate 		    ((size = resolvepath(nname, path, (PATH_MAX - 1))) > 0)) {
11897c478bd9Sstevel@tonic-gate 			path[size] = '\0';
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 			if (strcmp(nname, path)) {
119224a6229eSrie 				if ((nlmp =
119324a6229eSrie 				    fpavl_loaded(lml, path, 0)) != NULL) {
11947c478bd9Sstevel@tonic-gate 					added = 0;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 					if (append_alias(nlmp, nname,
11977c478bd9Sstevel@tonic-gate 					    &added) == 0)
11987c478bd9Sstevel@tonic-gate 						return (0);
11997c478bd9Sstevel@tonic-gate 					if (added)
12007c478bd9Sstevel@tonic-gate 						DBG_CALL(Dbg_file_skip(nname,
12017c478bd9Sstevel@tonic-gate 						    NAME(nlmp)));
12027c478bd9Sstevel@tonic-gate 					fdesc->fd_nname = nname;
12037c478bd9Sstevel@tonic-gate 					fdesc->fd_lmp = nlmp;
12047c478bd9Sstevel@tonic-gate 					return (1);
12057c478bd9Sstevel@tonic-gate 				}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 				/*
12087c478bd9Sstevel@tonic-gate 				 * If this pathname hasn't been loaded, save
12097c478bd9Sstevel@tonic-gate 				 * the resolved pathname so that it doesn't
12107c478bd9Sstevel@tonic-gate 				 * have to be recomputed as part of fullpath()
12117c478bd9Sstevel@tonic-gate 				 * processing.
12127c478bd9Sstevel@tonic-gate 				 */
12137c478bd9Sstevel@tonic-gate 				if ((fdesc->fd_pname = strdup(path)) == 0)
12147c478bd9Sstevel@tonic-gate 					return (0);
12157c478bd9Sstevel@tonic-gate 			} else {
12167c478bd9Sstevel@tonic-gate 				/*
12177c478bd9Sstevel@tonic-gate 				 * If the resolved name doesn't differ from the
12187c478bd9Sstevel@tonic-gate 				 * original, save it without duplication.
12197c478bd9Sstevel@tonic-gate 				 * Having fd_pname set indicates that no further
12207c478bd9Sstevel@tonic-gate 				 * resolvepath processing is necessary.
12217c478bd9Sstevel@tonic-gate 				 */
12227c478bd9Sstevel@tonic-gate 				fdesc->fd_pname = nname;
12237c478bd9Sstevel@tonic-gate 			}
12247c478bd9Sstevel@tonic-gate 		}
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 		if (nlmp = is_devinode_loaded(&status, lml, nname, flags)) {
12277c478bd9Sstevel@tonic-gate 			added = 0;
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 			if (append_alias(nlmp, nname, &added) == 0)
12307c478bd9Sstevel@tonic-gate 				return (0);
12317c478bd9Sstevel@tonic-gate 			if (added) {
12327c478bd9Sstevel@tonic-gate 				if ((nname[0] == '/') && (fpavl_insert(lml,
12337c478bd9Sstevel@tonic-gate 				    nlmp, nname, 0) == 0))
12347c478bd9Sstevel@tonic-gate 					return (0);
12357c478bd9Sstevel@tonic-gate 				DBG_CALL(Dbg_file_skip(nname, NAME(nlmp)));
12367c478bd9Sstevel@tonic-gate 			}
12377c478bd9Sstevel@tonic-gate 			fdesc->fd_nname = nname;
12387c478bd9Sstevel@tonic-gate 			fdesc->fd_lmp = nlmp;
12397c478bd9Sstevel@tonic-gate 			return (1);
12407c478bd9Sstevel@tonic-gate 		}
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		if ((fd = open(nname, O_RDONLY, 0)) == -1) {
12437c478bd9Sstevel@tonic-gate 			/*
12447c478bd9Sstevel@tonic-gate 			 * As the file must exist for the previous stat() to
12457c478bd9Sstevel@tonic-gate 			 * have succeeded, record the error condition.
12467c478bd9Sstevel@tonic-gate 			 */
12477c478bd9Sstevel@tonic-gate 			rej->rej_type = SGS_REJ_STR;
12487c478bd9Sstevel@tonic-gate 			rej->rej_str = strerror(errno);
12497c478bd9Sstevel@tonic-gate 		} else {
12507c478bd9Sstevel@tonic-gate 			Fct	*ftp;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 			if ((ftp = are_u_this(rej, fd, &status, nname)) != 0) {
12537c478bd9Sstevel@tonic-gate 				fdesc->fd_nname = nname;
12547c478bd9Sstevel@tonic-gate 				fdesc->fd_ftp = ftp;
12557c478bd9Sstevel@tonic-gate 				fdesc->fd_dev = status.st_dev;
12567c478bd9Sstevel@tonic-gate 				fdesc->fd_ino = status.st_ino;
12577c478bd9Sstevel@tonic-gate 				fdesc->fd_fd = fd;
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 				/*
12607c478bd9Sstevel@tonic-gate 				 * Trace that this open has succeeded.
12617c478bd9Sstevel@tonic-gate 				 */
12627c478bd9Sstevel@tonic-gate 				if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
12637c478bd9Sstevel@tonic-gate 				    trace_so(clmp, 0, oname, nname,
12647c478bd9Sstevel@tonic-gate 					(fdesc->fd_flags & FLG_FD_ALTER), 0);
12657c478bd9Sstevel@tonic-gate 				}
12667c478bd9Sstevel@tonic-gate 				return (1);
12677c478bd9Sstevel@tonic-gate 			}
12687c478bd9Sstevel@tonic-gate 			(void) close(fd);
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	} else if (errno != ENOENT) {
12727c478bd9Sstevel@tonic-gate 		/*
12737c478bd9Sstevel@tonic-gate 		 * If the open() failed for anything other than the file not
12747c478bd9Sstevel@tonic-gate 		 * existing, record the error condition.
12757c478bd9Sstevel@tonic-gate 		 */
12767c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_STR;
12777c478bd9Sstevel@tonic-gate 		rej->rej_str = strerror(errno);
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/*
12817c478bd9Sstevel@tonic-gate 	 * Indicate any rejection.
12827c478bd9Sstevel@tonic-gate 	 */
12837c478bd9Sstevel@tonic-gate 	if (rej->rej_type) {
12847c478bd9Sstevel@tonic-gate 		rej->rej_name = nname;
12857c478bd9Sstevel@tonic-gate 		rej->rej_flag = (fdesc->fd_flags & FLG_FD_ALTER);
12867c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_rejected(rej));
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 	return (0);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate  * Find a full pathname (it contains a "/").
12937c478bd9Sstevel@tonic-gate  */
12947c478bd9Sstevel@tonic-gate int
12957c478bd9Sstevel@tonic-gate find_path(Lm_list *lml, const char *oname, Rt_map *clmp, uint_t flags,
12967c478bd9Sstevel@tonic-gate     Fdesc * fdesc, Rej_desc *rej)
12977c478bd9Sstevel@tonic-gate {
12987c478bd9Sstevel@tonic-gate 	int	err = 0;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/*
13017c478bd9Sstevel@tonic-gate 	 * If directory configuration exists determine if this path is known.
13027c478bd9Sstevel@tonic-gate 	 */
13037c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DIRCFG) {
13047c478bd9Sstevel@tonic-gate 		Rtc_obj		*obj;
13057c478bd9Sstevel@tonic-gate 		const char	*aname;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 		if ((obj = elf_config_ent(oname, (Word)elf_hash(oname),
13087c478bd9Sstevel@tonic-gate 		    0, &aname)) != 0) {
13097c478bd9Sstevel@tonic-gate 			/*
13107c478bd9Sstevel@tonic-gate 			 * If the configuration file states that this path is a
13117c478bd9Sstevel@tonic-gate 			 * directory, or the path is explicitly defined as
13127c478bd9Sstevel@tonic-gate 			 * non-existent (ie. a unused platform specific
13137c478bd9Sstevel@tonic-gate 			 * library), then go no further.
13147c478bd9Sstevel@tonic-gate 			 */
13157c478bd9Sstevel@tonic-gate 			if (obj->co_flags & RTC_OBJ_DIRENT) {
13167c478bd9Sstevel@tonic-gate 				err = EISDIR;
13177c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags &
13187c478bd9Sstevel@tonic-gate 			    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) ==
13197c478bd9Sstevel@tonic-gate 			    RTC_OBJ_NOEXIST) {
13207c478bd9Sstevel@tonic-gate 				err = ENOENT;
13217c478bd9Sstevel@tonic-gate 			} else if ((obj->co_flags & RTC_OBJ_ALTER) &&
13227c478bd9Sstevel@tonic-gate 			    (rtld_flags & RT_FL_OBJALT) && (lml == &lml_main)) {
13237c478bd9Sstevel@tonic-gate 				int	ret;
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 				fdesc->fd_flags |= FLG_FD_ALTER;
13267c478bd9Sstevel@tonic-gate 				/*
13277c478bd9Sstevel@tonic-gate 				 * Attempt to open the alternative path.  If
13287c478bd9Sstevel@tonic-gate 				 * this fails, and the alternative is flagged
13297c478bd9Sstevel@tonic-gate 				 * as optional, fall through to open the
13307c478bd9Sstevel@tonic-gate 				 * original path.
13317c478bd9Sstevel@tonic-gate 				 */
13327c478bd9Sstevel@tonic-gate 				DBG_CALL(Dbg_libs_found(aname, FLG_FD_ALTER));
13337c478bd9Sstevel@tonic-gate 				if (((ret = file_open(0, lml, oname, aname,
13347c478bd9Sstevel@tonic-gate 				    clmp, flags, fdesc, rej)) != 0) ||
13357c478bd9Sstevel@tonic-gate 				    ((obj->co_flags & RTC_OBJ_OPTINAL) == 0))
13367c478bd9Sstevel@tonic-gate 					return (ret);
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate 				fdesc->fd_flags &= ~FLG_FD_ALTER;
13397c478bd9Sstevel@tonic-gate 			}
13407c478bd9Sstevel@tonic-gate 		}
13417c478bd9Sstevel@tonic-gate 	}
13427c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_libs_found(oname, 0));
13437c478bd9Sstevel@tonic-gate 	return (file_open(err, lml, oname, oname, clmp, flags, fdesc, rej));
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate /*
13477c478bd9Sstevel@tonic-gate  * Find a simple filename (it doesn't contain a "/").
13487c478bd9Sstevel@tonic-gate  */
13497c478bd9Sstevel@tonic-gate static int
13507c478bd9Sstevel@tonic-gate _find_file(Lm_list *lml, const char *oname, const char *nname, Rt_map *clmp,
13517c478bd9Sstevel@tonic-gate     uint_t flags, Fdesc * fdesc, Rej_desc *rej, Pnode * dir, int aflag)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_libs_found(nname, aflag));
13547c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_TRC_SEARCH) &&
13557c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
13567c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_PTH_TRYING), nname, aflag ?
13577c478bd9Sstevel@tonic-gate 		    MSG_INTL(MSG_LDD_FIL_ALTER) : MSG_ORIG(MSG_STR_EMPTY));
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	/*
13617c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're about
13627c478bd9Sstevel@tonic-gate 	 * to go search for.  The audit library may offer an alternative
13637c478bd9Sstevel@tonic-gate 	 * dependency, or indicate that this dependency should be ignored.
13647c478bd9Sstevel@tonic-gate 	 */
13657c478bd9Sstevel@tonic-gate 	if ((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
13667c478bd9Sstevel@tonic-gate 		char	*aname = audit_objsearch(clmp, nname, dir->p_orig);
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 		if (aname == 0)
13697c478bd9Sstevel@tonic-gate 			return (0);
13707c478bd9Sstevel@tonic-gate 		nname = aname;
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 	return (file_open(0, lml, oname, nname, clmp, flags, fdesc, rej));
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
1375390b98b5Srie static int
13767c478bd9Sstevel@tonic-gate find_file(Lm_list *lml, const char *oname, Rt_map *clmp, uint_t flags,
13777c478bd9Sstevel@tonic-gate     Fdesc * fdesc, Rej_desc *rej, Pnode * dir, Word * strhash, size_t olen)
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	static Rtc_obj	Obj = { 0 };
13807c478bd9Sstevel@tonic-gate 	Rtc_obj *	dobj;
13817c478bd9Sstevel@tonic-gate 	const char	*nname = oname;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	if (dir->p_name == 0)
13847c478bd9Sstevel@tonic-gate 		return (0);
13857c478bd9Sstevel@tonic-gate 	if (dir->p_info) {
13867c478bd9Sstevel@tonic-gate 		dobj = (Rtc_obj *)dir->p_info;
13877c478bd9Sstevel@tonic-gate 		if ((dobj->co_flags &
13887c478bd9Sstevel@tonic-gate 		    (RTC_OBJ_NOEXIST | RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
13897c478bd9Sstevel@tonic-gate 			return (0);
13907c478bd9Sstevel@tonic-gate 	} else
13917c478bd9Sstevel@tonic-gate 		dobj = 0;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	/*
13947c478bd9Sstevel@tonic-gate 	 * If configuration information exists see if this directory/file
13957c478bd9Sstevel@tonic-gate 	 * combination exists.
13967c478bd9Sstevel@tonic-gate 	 */
13977c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_DIRCFG) &&
13987c478bd9Sstevel@tonic-gate 	    ((dobj == 0) || (dobj->co_id != 0))) {
13997c478bd9Sstevel@tonic-gate 		Rtc_obj		*fobj;
14007c478bd9Sstevel@tonic-gate 		const char	*alt = 0;
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate 		/*
14037c478bd9Sstevel@tonic-gate 		 * If this pnode has not yet been searched for in the
14047c478bd9Sstevel@tonic-gate 		 * configuration file go find it.
14057c478bd9Sstevel@tonic-gate 		 */
14067c478bd9Sstevel@tonic-gate 		if (dobj == 0) {
14077c478bd9Sstevel@tonic-gate 			dobj = elf_config_ent(dir->p_name,
14087c478bd9Sstevel@tonic-gate 			    (Word)elf_hash(dir->p_name), 0, 0);
14097c478bd9Sstevel@tonic-gate 			if (dobj == 0)
14107c478bd9Sstevel@tonic-gate 				dobj = &Obj;
14117c478bd9Sstevel@tonic-gate 			dir->p_info = (void *)dobj;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 			if ((dobj->co_flags & (RTC_OBJ_NOEXIST |
14147c478bd9Sstevel@tonic-gate 			    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
14157c478bd9Sstevel@tonic-gate 				return (0);
14167c478bd9Sstevel@tonic-gate 		}
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 		/*
14197c478bd9Sstevel@tonic-gate 		 * If we found a directory search for the file.
14207c478bd9Sstevel@tonic-gate 		 */
14217c478bd9Sstevel@tonic-gate 		if (dobj->co_id != 0) {
14227c478bd9Sstevel@tonic-gate 			if (*strhash == 0)
14237c478bd9Sstevel@tonic-gate 				*strhash = (Word)elf_hash(nname);
14247c478bd9Sstevel@tonic-gate 			fobj = elf_config_ent(nname, *strhash,
14257c478bd9Sstevel@tonic-gate 			    dobj->co_id, &alt);
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate 			/*
14287c478bd9Sstevel@tonic-gate 			 * If this object specifically does not exist, or the
14297c478bd9Sstevel@tonic-gate 			 * object can't be found in a know-all-entries
14307c478bd9Sstevel@tonic-gate 			 * directory, continue looking.  If the object does
14317c478bd9Sstevel@tonic-gate 			 * exist determine if an alternative object exists.
14327c478bd9Sstevel@tonic-gate 			 */
14337c478bd9Sstevel@tonic-gate 			if (fobj == 0) {
14347c478bd9Sstevel@tonic-gate 				if (dobj->co_flags & RTC_OBJ_ALLENTS)
14357c478bd9Sstevel@tonic-gate 					return (0);
14367c478bd9Sstevel@tonic-gate 			} else {
14377c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & (RTC_OBJ_NOEXIST |
14387c478bd9Sstevel@tonic-gate 				    RTC_OBJ_ALTER)) == RTC_OBJ_NOEXIST)
14397c478bd9Sstevel@tonic-gate 					return (0);
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 				if ((fobj->co_flags & RTC_OBJ_ALTER) &&
14427c478bd9Sstevel@tonic-gate 				    (rtld_flags & RT_FL_OBJALT) &&
14437c478bd9Sstevel@tonic-gate 				    (lml == &lml_main)) {
14447c478bd9Sstevel@tonic-gate 					int	ret;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 					fdesc->fd_flags |= FLG_FD_ALTER;
14477c478bd9Sstevel@tonic-gate 					/*
14487c478bd9Sstevel@tonic-gate 					 * Attempt to open the alternative path.
14497c478bd9Sstevel@tonic-gate 					 * If this fails, and the alternative is
14507c478bd9Sstevel@tonic-gate 					 * flagged as optional, fall through to
14517c478bd9Sstevel@tonic-gate 					 * open the original path.
14527c478bd9Sstevel@tonic-gate 					 */
14537c478bd9Sstevel@tonic-gate 					ret = _find_file(lml, oname, alt, clmp,
14547c478bd9Sstevel@tonic-gate 					    flags, fdesc, rej, dir, 1);
14557c478bd9Sstevel@tonic-gate 					if (ret || ((fobj->co_flags &
14567c478bd9Sstevel@tonic-gate 					    RTC_OBJ_OPTINAL) == 0))
14577c478bd9Sstevel@tonic-gate 						return (ret);
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 					fdesc->fd_flags &= ~FLG_FD_ALTER;
14607c478bd9Sstevel@tonic-gate 				}
14617c478bd9Sstevel@tonic-gate 			}
14627c478bd9Sstevel@tonic-gate 		}
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	/*
14667c478bd9Sstevel@tonic-gate 	 * Protect ourselves from building an invalid pathname.
14677c478bd9Sstevel@tonic-gate 	 */
14687c478bd9Sstevel@tonic-gate 	if ((olen + dir->p_len + 1) >= PATH_MAX) {
14697c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), nname,
14707c478bd9Sstevel@tonic-gate 		    strerror(ENAMETOOLONG));
14717c478bd9Sstevel@tonic-gate 			return (0);
14727c478bd9Sstevel@tonic-gate 	}
14737c478bd9Sstevel@tonic-gate 	if ((nname = (LM_GET_SO(clmp)(dir->p_name, nname))) == 0)
14747c478bd9Sstevel@tonic-gate 		return (0);
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	return (_find_file(lml, oname, nname, clmp, flags, fdesc, rej, dir, 0));
14777c478bd9Sstevel@tonic-gate }
14787c478bd9Sstevel@tonic-gate 
14797c478bd9Sstevel@tonic-gate /*
14807c478bd9Sstevel@tonic-gate  * A unique file has been opened.  Create a link-map to represent it, and
14817c478bd9Sstevel@tonic-gate  * process the various names by which it can be referenced.
14827c478bd9Sstevel@tonic-gate  */
14837c478bd9Sstevel@tonic-gate static Rt_map *
1484*fb1354edSrie load_file(Lm_list *lml, Aliste lmco, Fdesc *fdesc)
14857c478bd9Sstevel@tonic-gate {
14867c478bd9Sstevel@tonic-gate 	const char	*oname = fdesc->fd_oname;
14877c478bd9Sstevel@tonic-gate 	const char	*nname = fdesc->fd_nname;
14887c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	/*
14917c478bd9Sstevel@tonic-gate 	 * Typically we call fct_map_so() with the full pathname of the opened
14927c478bd9Sstevel@tonic-gate 	 * file (nname) and the name that started the search (oname), thus for
14937c478bd9Sstevel@tonic-gate 	 * a typical dependency on libc this would be /usr/lib/libc.so.1 and
14947c478bd9Sstevel@tonic-gate 	 * libc.so.1 (DT_NEEDED).  The original name is maintained on an ALIAS
14957c478bd9Sstevel@tonic-gate 	 * list for comparison when bringing in new dependencies.  If the user
14967c478bd9Sstevel@tonic-gate 	 * specified name as a full path (from a dlopen() for example) then
14977c478bd9Sstevel@tonic-gate 	 * there's no need to create an ALIAS.
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	if (strcmp(oname, nname) == 0)
15007c478bd9Sstevel@tonic-gate 		oname = 0;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 	/*
15037c478bd9Sstevel@tonic-gate 	 * A new file has been opened, now map it into the process.  Close the
15047c478bd9Sstevel@tonic-gate 	 * original file so as not to accumulate file descriptors.
15057c478bd9Sstevel@tonic-gate 	 */
15067c478bd9Sstevel@tonic-gate 	nlmp = ((fdesc->fd_ftp)->fct_map_so)(lml, lmco, nname, oname,
15077c478bd9Sstevel@tonic-gate 	    fdesc->fd_fd);
15087c478bd9Sstevel@tonic-gate 	(void) close(fdesc->fd_fd);
15097c478bd9Sstevel@tonic-gate 	fdesc->fd_fd = 0;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	if (nlmp == 0)
15127c478bd9Sstevel@tonic-gate 		return (0);
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	/*
15157c478bd9Sstevel@tonic-gate 	 * Save the dev/inode information for later comparisons.
15167c478bd9Sstevel@tonic-gate 	 */
15177c478bd9Sstevel@tonic-gate 	STDEV(nlmp) = fdesc->fd_dev;
15187c478bd9Sstevel@tonic-gate 	STINO(nlmp) = fdesc->fd_ino;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	/*
15217c478bd9Sstevel@tonic-gate 	 * Insert the names of this link-map into the FullpathNode AVL tree.
15227c478bd9Sstevel@tonic-gate 	 * Save both the NAME() and PATHNAME() is they differ.
15237c478bd9Sstevel@tonic-gate 	 *
15247c478bd9Sstevel@tonic-gate 	 * If this is an OBJECT file, don't insert it yet as this is only a
15257c478bd9Sstevel@tonic-gate 	 * temporary link-map.  During elf_obj_fini() the final link-map is
15267c478bd9Sstevel@tonic-gate 	 * created, and its names will be inserted in the FullpathNode AVL
15277c478bd9Sstevel@tonic-gate 	 * tree at that time.
15287c478bd9Sstevel@tonic-gate 	 */
15297c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) & FLG_RT_OBJECT) == 0) {
15307c478bd9Sstevel@tonic-gate 		/*
15317c478bd9Sstevel@tonic-gate 		 * Update the objects full path information if necessary.
153211a2bb38Srie 		 * Note, with pathname expansion in effect, the fd_pname will
153311a2bb38Srie 		 * be used as PATHNAME().  This allocated string will be freed
153411a2bb38Srie 		 * should this object be deleted.  However, without pathname
153511a2bb38Srie 		 * expansion, the fd_name should be freed now, as it is no
153611a2bb38Srie 		 * longer referenced.
15377c478bd9Sstevel@tonic-gate 		 */
153811a2bb38Srie 		if (FLAGS1(nlmp) & FL1_RT_RELATIVE)
15397c478bd9Sstevel@tonic-gate 			(void) fullpath(nlmp, fdesc->fd_pname);
154011a2bb38Srie 		else if (fdesc->fd_pname != fdesc->fd_nname)
154111a2bb38Srie 			free((void *)fdesc->fd_pname);
154211a2bb38Srie 		fdesc->fd_pname = 0;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 		if ((NAME(nlmp)[0] == '/') && (fpavl_insert(lml, nlmp,
15457c478bd9Sstevel@tonic-gate 		    NAME(nlmp), fdesc->fd_avlwhere) == 0)) {
15467c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
15477c478bd9Sstevel@tonic-gate 			return (0);
15487c478bd9Sstevel@tonic-gate 		}
15497c478bd9Sstevel@tonic-gate 		if (((NAME(nlmp)[0] != '/') ||
15507c478bd9Sstevel@tonic-gate 		    (NAME(nlmp) != PATHNAME(nlmp))) &&
15517c478bd9Sstevel@tonic-gate 		    (fpavl_insert(lml, nlmp, PATHNAME(nlmp), 0) == 0)) {
15527c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
15537c478bd9Sstevel@tonic-gate 			return (0);
15547c478bd9Sstevel@tonic-gate 		}
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	/*
15587c478bd9Sstevel@tonic-gate 	 * If we're processing an alternative object reset the original name
15597c478bd9Sstevel@tonic-gate 	 * for possible $ORIGIN processing.
15607c478bd9Sstevel@tonic-gate 	 */
15617c478bd9Sstevel@tonic-gate 	if (fdesc->fd_flags & FLG_FD_ALTER) {
15627c478bd9Sstevel@tonic-gate 		const char	*odir;
15637c478bd9Sstevel@tonic-gate 		char		*ndir;
15647c478bd9Sstevel@tonic-gate 		size_t		olen;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) |= FLG_RT_ALTER;
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 		/*
15697c478bd9Sstevel@tonic-gate 		 * If we were given a pathname containing a slash then the
15707c478bd9Sstevel@tonic-gate 		 * original name is still in oname.  Otherwise the original
15717c478bd9Sstevel@tonic-gate 		 * directory is in dir->p_name (which is all we need for
15727c478bd9Sstevel@tonic-gate 		 * $ORIGIN).
15737c478bd9Sstevel@tonic-gate 		 */
15747c478bd9Sstevel@tonic-gate 		if (fdesc->fd_flags & FLG_FD_SLASH) {
15757c478bd9Sstevel@tonic-gate 			char	*ofil;
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 			odir = oname;
15787c478bd9Sstevel@tonic-gate 			ofil = strrchr(oname, '/');
15797c478bd9Sstevel@tonic-gate 			olen = ofil - odir + 1;
15807c478bd9Sstevel@tonic-gate 		} else {
15817c478bd9Sstevel@tonic-gate 			odir = fdesc->fd_odir;
15827c478bd9Sstevel@tonic-gate 			olen = strlen(odir) + 1;
15837c478bd9Sstevel@tonic-gate 		}
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 		if ((ndir = (char *)malloc(olen)) == 0) {
15867c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
15877c478bd9Sstevel@tonic-gate 			return (0);
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 		(void) strncpy(ndir, odir, olen);
15907c478bd9Sstevel@tonic-gate 		ndir[--olen] = '\0';
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 		ORIGNAME(nlmp) = ndir;
15937c478bd9Sstevel@tonic-gate 		DIRSZ(nlmp) = olen;
15947c478bd9Sstevel@tonic-gate 	}
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/*
1597390b98b5Srie 	 * Identify this as a new object.
15987c478bd9Sstevel@tonic-gate 	 */
1599390b98b5Srie 	FLAGS(nlmp) |= FLG_RT_NEWLOAD;
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	/*
16027c478bd9Sstevel@tonic-gate 	 * Report module loads to TLS module activity.
16037c478bd9Sstevel@tonic-gate 	 */
16047c478bd9Sstevel@tonic-gate 	if (nlmp)
16057c478bd9Sstevel@tonic-gate 		tls_modactivity(nlmp, TM_FLG_MODADD);
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	return (nlmp);
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate /*
16117c478bd9Sstevel@tonic-gate  * This function loads the named file and returns a pointer to its link map.
16127c478bd9Sstevel@tonic-gate  * It is assumed that the caller has already checked that the file is not
16137c478bd9Sstevel@tonic-gate  * already loaded before calling this function (refer is_so_loaded()).
16147c478bd9Sstevel@tonic-gate  * Find and open the file, map it into memory, add it to the end of the list
16157c478bd9Sstevel@tonic-gate  * of link maps and return a pointer to the new link map.  Return 0 on error.
16167c478bd9Sstevel@tonic-gate  */
16177c478bd9Sstevel@tonic-gate static Rt_map *
16187c478bd9Sstevel@tonic-gate load_so(Lm_list *lml, Aliste lmco, const char *oname, Rt_map *clmp,
1619*fb1354edSrie     uint_t flags, Fdesc *nfdp, Rej_desc *rej)
16207c478bd9Sstevel@tonic-gate {
16217c478bd9Sstevel@tonic-gate 	char		*name;
16227c478bd9Sstevel@tonic-gate 	uint_t		slash = 0;
16237c478bd9Sstevel@tonic-gate 	size_t		olen;
1624*fb1354edSrie 	Fdesc		fdesc = { 0 };
16257c478bd9Sstevel@tonic-gate 	Pnode		*dir;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	/*
16287c478bd9Sstevel@tonic-gate 	 * If the file is the run time linker then it's already loaded.
16297c478bd9Sstevel@tonic-gate 	 */
16307c478bd9Sstevel@tonic-gate 	if (interp && (strcmp(oname, NAME(lml_rtld.lm_head)) == 0))
16317c478bd9Sstevel@tonic-gate 		return (lml_rtld.lm_head);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	/*
16347c478bd9Sstevel@tonic-gate 	 * If this isn't a hardware capabilites pathname, which is already a
16357c478bd9Sstevel@tonic-gate 	 * full, duplicated pathname, determine whether the pathname contains
16367c478bd9Sstevel@tonic-gate 	 * a slash, and if not determine the input filename (for max path
16377c478bd9Sstevel@tonic-gate 	 * length verification).
16387c478bd9Sstevel@tonic-gate 	 */
16397c478bd9Sstevel@tonic-gate 	if ((flags & FLG_RT_HWCAP) == 0) {
16407c478bd9Sstevel@tonic-gate 		const char	*str;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 		for (str = oname; *str; str++) {
16437c478bd9Sstevel@tonic-gate 			if (*str == '/') {
16447c478bd9Sstevel@tonic-gate 				slash++;
16457c478bd9Sstevel@tonic-gate 				break;
16467c478bd9Sstevel@tonic-gate 			}
16477c478bd9Sstevel@tonic-gate 		}
16487c478bd9Sstevel@tonic-gate 		if (slash == 0)
16497c478bd9Sstevel@tonic-gate 			olen = (str - oname) + 1;
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 	/*
16537c478bd9Sstevel@tonic-gate 	 * If we are passed a 'null' link-map this means that this is the first
16547c478bd9Sstevel@tonic-gate 	 * object to be loaded on this link-map list.  In that case we set the
16557c478bd9Sstevel@tonic-gate 	 * link-map to ld.so.1's link-map.
16567c478bd9Sstevel@tonic-gate 	 *
16577c478bd9Sstevel@tonic-gate 	 * This link-map is referenced to determine what lookup rules to use
16587c478bd9Sstevel@tonic-gate 	 * when searching for files.  By using ld.so.1's we are defaulting to
16597c478bd9Sstevel@tonic-gate 	 * ELF look-up rules.
16607c478bd9Sstevel@tonic-gate 	 *
16617c478bd9Sstevel@tonic-gate 	 * Note: This case happens when loading the first object onto
16627c478bd9Sstevel@tonic-gate 	 *	 the plt_tracing link-map.
16637c478bd9Sstevel@tonic-gate 	 */
16647c478bd9Sstevel@tonic-gate 	if (clmp == 0)
16657c478bd9Sstevel@tonic-gate 		clmp = lml_rtld.lm_head;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	/*
16687c478bd9Sstevel@tonic-gate 	 * If this path resulted from a $HWCAP specification, then the best
16697c478bd9Sstevel@tonic-gate 	 * hardware capability object has already been establish, and is
16707c478bd9Sstevel@tonic-gate 	 * available in the calling file descriptor.  Perform some minor book-
16717c478bd9Sstevel@tonic-gate 	 * keeping so that we can fall through into common code.
16727c478bd9Sstevel@tonic-gate 	 */
16737c478bd9Sstevel@tonic-gate 	if (flags & FLG_RT_HWCAP) {
16747c478bd9Sstevel@tonic-gate 		/*
1675*fb1354edSrie 		 * If this object is already loaded, we're done.
16767c478bd9Sstevel@tonic-gate 		 */
1677*fb1354edSrie 		if (nfdp->fd_lmp)
1678*fb1354edSrie 			return (nfdp->fd_lmp);
16797c478bd9Sstevel@tonic-gate 
1680*fb1354edSrie 		/*
1681*fb1354edSrie 		 * Obtain the avl index for this object.
1682*fb1354edSrie 		 */
1683*fb1354edSrie 		(void) fpavl_loaded(lml, nfdp->fd_nname, &(nfdp->fd_avlwhere));
1684bbf522bdSrie 
1685*fb1354edSrie 		/*
1686*fb1354edSrie 		 * If the name and resolved pathname differ, duplicate the path
1687*fb1354edSrie 		 * name once more to provide for genric cleanup by the caller.
1688*fb1354edSrie 		 */
1689*fb1354edSrie 		if (nfdp->fd_pname && (nfdp->fd_nname != nfdp->fd_pname)) {
1690*fb1354edSrie 			char	*pname;
1691bbf522bdSrie 
1692*fb1354edSrie 			if ((pname = strdup(nfdp->fd_pname)) == 0)
1693*fb1354edSrie 				return (0);
1694*fb1354edSrie 			nfdp->fd_pname = pname;
16957c478bd9Sstevel@tonic-gate 		}
16967c478bd9Sstevel@tonic-gate 	} else if (slash) {
16977c478bd9Sstevel@tonic-gate 		Rej_desc	_rej = { 0 };
16987c478bd9Sstevel@tonic-gate 
1699*fb1354edSrie 		*nfdp = fdesc;
1700*fb1354edSrie 		nfdp->fd_flags = FLG_FD_SLASH;
17017c478bd9Sstevel@tonic-gate 
1702*fb1354edSrie 		if (find_path(lml, oname, clmp, flags, nfdp, &_rej) == 0) {
1703*fb1354edSrie 			rejection_inherit(rej, &_rej, nfdp);
17047c478bd9Sstevel@tonic-gate 			return (0);
17057c478bd9Sstevel@tonic-gate 		}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 		/*
17087c478bd9Sstevel@tonic-gate 		 * If this object is already loaded, we're done.
17097c478bd9Sstevel@tonic-gate 		 */
1710*fb1354edSrie 		if (nfdp->fd_lmp)
1711*fb1354edSrie 			return (nfdp->fd_lmp);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	} else {
17147c478bd9Sstevel@tonic-gate 		/*
17157c478bd9Sstevel@tonic-gate 		 * No '/' - for each directory on list, make a pathname using
17167c478bd9Sstevel@tonic-gate 		 * that directory and filename and try to open that file.
17177c478bd9Sstevel@tonic-gate 		 */
1718*fb1354edSrie 		Pnode		*dirlist = (Pnode *)0;
17197c478bd9Sstevel@tonic-gate 		Word		strhash = 0;
1720*fb1354edSrie #if	!defined(ISSOLOAD_BASENAME_DISABLED)
1721*fb1354edSrie 		Rt_map		*nlmp;
1722*fb1354edSrie #endif
17237c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_libs_find(oname));
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate #if	!defined(ISSOLOAD_BASENAME_DISABLED)
17267c478bd9Sstevel@tonic-gate 		if ((nlmp = is_so_loaded(lml, oname, 0)))
17277c478bd9Sstevel@tonic-gate 			return (nlmp);
17287c478bd9Sstevel@tonic-gate #endif
17297c478bd9Sstevel@tonic-gate 		/*
17307c478bd9Sstevel@tonic-gate 		 * Make sure we clear the file descriptor new name in case the
17317c478bd9Sstevel@tonic-gate 		 * following directory search doesn't provide any directories
17327c478bd9Sstevel@tonic-gate 		 * (odd, but this can be forced with a -znodefaultlib test).
17337c478bd9Sstevel@tonic-gate 		 */
1734*fb1354edSrie 		*nfdp = fdesc;
17357c478bd9Sstevel@tonic-gate 		for (dir = get_next_dir(&dirlist, clmp, flags); dir;
17367c478bd9Sstevel@tonic-gate 		    dir = get_next_dir(&dirlist, clmp, flags)) {
17377c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
17387c478bd9Sstevel@tonic-gate 
1739*fb1354edSrie 			*nfdp = fdesc;
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate 			/*
17427c478bd9Sstevel@tonic-gate 			 * Try and locate this file.  Make sure to clean up
17437c478bd9Sstevel@tonic-gate 			 * any rejection information should the file have
17447c478bd9Sstevel@tonic-gate 			 * been found, but not appropriate.
17457c478bd9Sstevel@tonic-gate 			 */
1746*fb1354edSrie 			if (find_file(lml, oname, clmp, flags, nfdp, &_rej,
17477c478bd9Sstevel@tonic-gate 			    dir, &strhash, olen) == 0) {
1748*fb1354edSrie 				rejection_inherit(rej, &_rej, nfdp);
17497c478bd9Sstevel@tonic-gate 				continue;
17507c478bd9Sstevel@tonic-gate 			}
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 			/*
17537c478bd9Sstevel@tonic-gate 			 * If this object is already loaded, we're done.
17547c478bd9Sstevel@tonic-gate 			 */
1755*fb1354edSrie 			if (nfdp->fd_lmp)
1756*fb1354edSrie 				return (nfdp->fd_lmp);
17577c478bd9Sstevel@tonic-gate 
1758*fb1354edSrie 			nfdp->fd_odir = dir->p_name;
17597c478bd9Sstevel@tonic-gate 			break;
17607c478bd9Sstevel@tonic-gate 		}
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 		/*
17637c478bd9Sstevel@tonic-gate 		 * If the file couldn't be loaded, do another comparison of
17647c478bd9Sstevel@tonic-gate 		 * loaded files using just the basename.  This catches folks
17657c478bd9Sstevel@tonic-gate 		 * who may have loaded multiple full pathname files (possibly
17667c478bd9Sstevel@tonic-gate 		 * from setxid applications) to satisfy dependency relationships
17677c478bd9Sstevel@tonic-gate 		 * (i.e., a file might have a dependency on foo.so.1 which has
17687c478bd9Sstevel@tonic-gate 		 * already been opened using its full pathname).
17697c478bd9Sstevel@tonic-gate 		 */
1770*fb1354edSrie 		if (nfdp->fd_nname == 0)
17717c478bd9Sstevel@tonic-gate 			return (is_so_loaded(lml, oname, 1));
17727c478bd9Sstevel@tonic-gate 	}
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate 	/*
17757c478bd9Sstevel@tonic-gate 	 * Duplicate the file name so that NAME() is available in core files.
17767c478bd9Sstevel@tonic-gate 	 * Note, that hardware capability names are already duplicated, but
17777c478bd9Sstevel@tonic-gate 	 * they get duplicated once more to insure consistent cleanup in the
17787c478bd9Sstevel@tonic-gate 	 * event of an error condition.
17797c478bd9Sstevel@tonic-gate 	 */
1780*fb1354edSrie 	if ((name = strdup(nfdp->fd_nname)) == 0)
17817c478bd9Sstevel@tonic-gate 		return (0);
1782*fb1354edSrie 
1783*fb1354edSrie 	if (nfdp->fd_nname == nfdp->fd_pname)
1784*fb1354edSrie 		nfdp->fd_nname = nfdp->fd_pname = name;
17857c478bd9Sstevel@tonic-gate 	else
1786*fb1354edSrie 		nfdp->fd_nname = name;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	/*
17897c478bd9Sstevel@tonic-gate 	 * Finish mapping the file and return the link-map descriptor.  Note,
17907c478bd9Sstevel@tonic-gate 	 * if this request originated from a HWCAP request, re-establish the
17917c478bd9Sstevel@tonic-gate 	 * fdesc information.  For single paged objects, such as filters, the
17927c478bd9Sstevel@tonic-gate 	 * original mapping may have been sufficient to capture the file, thus
17937c478bd9Sstevel@tonic-gate 	 * this mapping needs to be reset to insure it doesn't mistakenly get
17947c478bd9Sstevel@tonic-gate 	 * unmapped as part of HWCAP cleanup.
17957c478bd9Sstevel@tonic-gate 	 */
1796*fb1354edSrie 	return (load_file(lml, lmco, nfdp));
17977c478bd9Sstevel@tonic-gate }
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate /*
18007c478bd9Sstevel@tonic-gate  * Trace an attempt to load an object.
18017c478bd9Sstevel@tonic-gate  */
18027c478bd9Sstevel@tonic-gate const char *
18037c478bd9Sstevel@tonic-gate load_trace(Lm_list *lml, const char *name, Rt_map *clmp)
18047c478bd9Sstevel@tonic-gate {
18057c478bd9Sstevel@tonic-gate 	/*
18067c478bd9Sstevel@tonic-gate 	 * First generate any ldd(1) diagnostics.
18077c478bd9Sstevel@tonic-gate 	 */
18087c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
18097c478bd9Sstevel@tonic-gate 	    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
18107c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name, NAME(clmp));
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	/*
18137c478bd9Sstevel@tonic-gate 	 * If we're being audited tell the audit library of the file we're
18147c478bd9Sstevel@tonic-gate 	 * about to go search for.
18157c478bd9Sstevel@tonic-gate 	 */
18167c478bd9Sstevel@tonic-gate 	if (((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_ACTIVITY) &&
18177c478bd9Sstevel@tonic-gate 	    (lml == LIST(clmp)))
18187c478bd9Sstevel@tonic-gate 		audit_activity(clmp, LA_ACT_ADD);
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	if ((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_OBJSEARCH) {
18217c478bd9Sstevel@tonic-gate 		char	*_name;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 		/*
18247c478bd9Sstevel@tonic-gate 		 * The auditor can indicate that this object should be ignored.
18257c478bd9Sstevel@tonic-gate 		 */
18267c478bd9Sstevel@tonic-gate 		if ((_name = audit_objsearch(clmp, name, LA_SER_ORIG)) == 0) {
18277c478bd9Sstevel@tonic-gate 			eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_AUDITERM), name);
18287c478bd9Sstevel@tonic-gate 			return (0);
18297c478bd9Sstevel@tonic-gate 		}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 		/*
18327c478bd9Sstevel@tonic-gate 		 * The auditor can provide an alternative name.
18337c478bd9Sstevel@tonic-gate 		 */
18347c478bd9Sstevel@tonic-gate 		if (_name != name) {
18357c478bd9Sstevel@tonic-gate 			free((void *)name);
18367c478bd9Sstevel@tonic-gate 			name = strdup(_name);
18377c478bd9Sstevel@tonic-gate 		}
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 	return (name);
18407c478bd9Sstevel@tonic-gate }
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate /*
18437c478bd9Sstevel@tonic-gate  * Having loaded an object and created a link-map to describe it, finish
18447c478bd9Sstevel@tonic-gate  * processing this stage, including verifying any versioning requirements,
18457c478bd9Sstevel@tonic-gate  * updating the objects mode, creating a handle if necessary, and adding this
18467c478bd9Sstevel@tonic-gate  * object to existing handles if required.
18477c478bd9Sstevel@tonic-gate  */
1848390b98b5Srie static int
1849*fb1354edSrie load_finish(Lm_list *lml, const char *name, Rt_map *clmp, int nmode,
1850*fb1354edSrie     uint_t flags, Grp_hdl **hdl, Rt_map *nlmp)
18517c478bd9Sstevel@tonic-gate {
18527c478bd9Sstevel@tonic-gate 	Aliste		off;
18537c478bd9Sstevel@tonic-gate 	Grp_hdl		*ghp, **ghpp;
18547c478bd9Sstevel@tonic-gate 	int		promote;
18557c478bd9Sstevel@tonic-gate 
18567c478bd9Sstevel@tonic-gate 	/*
18577c478bd9Sstevel@tonic-gate 	 * If this dependency is associated with a required version insure that
18587c478bd9Sstevel@tonic-gate 	 * the version is present in the loaded file.
18597c478bd9Sstevel@tonic-gate 	 */
18607c478bd9Sstevel@tonic-gate 	if (((rtld_flags & RT_FL_NOVERSION) == 0) &&
18617c478bd9Sstevel@tonic-gate 	    (FCT(clmp) == &elf_fct) && VERNEED(clmp) &&
18627c478bd9Sstevel@tonic-gate 	    (LM_VERIFY_VERS(clmp)(name, clmp, nlmp) == 0))
18637c478bd9Sstevel@tonic-gate 		return (0);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	/*
18667c478bd9Sstevel@tonic-gate 	 * If this object has indicated that it should be isolated as a group
18677c478bd9Sstevel@tonic-gate 	 * (DT_FLAGS_1 contains DF_1_GROUP - object was built with -B group),
18687c478bd9Sstevel@tonic-gate 	 * or if the callers direct bindings indicate it should be isolated as
18697c478bd9Sstevel@tonic-gate 	 * a group (DYNINFO flags contains FLG_DI_GROUP - dependency followed
18707c478bd9Sstevel@tonic-gate 	 * -zgroupperm), establish the appropriate mode.
18717c478bd9Sstevel@tonic-gate 	 *
18727c478bd9Sstevel@tonic-gate 	 * The intent of an object defining itself as a group is to isolate the
18737c478bd9Sstevel@tonic-gate 	 * relocation of the group within its own members, however, unless
18747c478bd9Sstevel@tonic-gate 	 * opened through dlopen(), in which case we assume dlsym() will be used
18757c478bd9Sstevel@tonic-gate 	 * to located symbols in the new object, we still need to associate it
18767c478bd9Sstevel@tonic-gate 	 * with the caller for it to be bound with.  This is equivalent to a
18777c478bd9Sstevel@tonic-gate 	 * dlopen(RTLD_GROUP) and dlsym() using the returned handle.
18787c478bd9Sstevel@tonic-gate 	 */
18797c478bd9Sstevel@tonic-gate 	if ((FLAGS(nlmp) | flags) & FLG_RT_SETGROUP) {
18807c478bd9Sstevel@tonic-gate 		nmode &= ~RTLD_WORLD;
18817c478bd9Sstevel@tonic-gate 		nmode |= RTLD_GROUP;
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 		/*
18847c478bd9Sstevel@tonic-gate 		 * If the object wasn't explicitly dlopen()'ed associate it with
18857c478bd9Sstevel@tonic-gate 		 * the parent.
18867c478bd9Sstevel@tonic-gate 		 */
18877c478bd9Sstevel@tonic-gate 		if (flags != FLG_RT_HANDLE)
18887c478bd9Sstevel@tonic-gate 			nmode |= RTLD_PARENT;
18897c478bd9Sstevel@tonic-gate 	}
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	/*
18927c478bd9Sstevel@tonic-gate 	 * Establish new mode and flags.
18937c478bd9Sstevel@tonic-gate 	 *
18947c478bd9Sstevel@tonic-gate 	 * For patch backward compatibility, the following use of update_mode()
18957c478bd9Sstevel@tonic-gate 	 * is disabled.
18967c478bd9Sstevel@tonic-gate 	 */
18977c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
18987c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DISFIX_1)
18997c478bd9Sstevel@tonic-gate 		promote = MODE(nlmp) |=
19007c478bd9Sstevel@tonic-gate 		    (nmode & ~(RTLD_PARENT | RTLD_NOLOAD | RTLD_FIRST));
19017c478bd9Sstevel@tonic-gate 	else
19027c478bd9Sstevel@tonic-gate #endif
19037c478bd9Sstevel@tonic-gate 		promote = update_mode(nlmp, MODE(nlmp), nmode);
19047c478bd9Sstevel@tonic-gate 
19057c478bd9Sstevel@tonic-gate 	FLAGS(nlmp) |= flags;
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	/*
19087c478bd9Sstevel@tonic-gate 	 * If we've been asked to establish a handle create one for this object.
19097c478bd9Sstevel@tonic-gate 	 * Or, if this object has already been analyzed, but this reference
19107c478bd9Sstevel@tonic-gate 	 * requires that the mode of the object be promoted, also create a
19117c478bd9Sstevel@tonic-gate 	 * handle to propagate the new modes to all this objects dependencies.
19127c478bd9Sstevel@tonic-gate 	 */
19137c478bd9Sstevel@tonic-gate 	if (((FLAGS(nlmp) | flags) & FLG_RT_HANDLE) || (promote &&
19147c478bd9Sstevel@tonic-gate 	    (FLAGS(nlmp) & FLG_RT_ANALYZED))) {
19157c478bd9Sstevel@tonic-gate 		uint_t	oflags, hflags = 0;
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate 		if (nmode & RTLD_PARENT)
19187c478bd9Sstevel@tonic-gate 			hflags |=  GPH_PARENT;
19197c478bd9Sstevel@tonic-gate 		if (nmode & RTLD_FIRST)
19207c478bd9Sstevel@tonic-gate 			hflags |=  GPH_FIRST;
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 		/*
19237c478bd9Sstevel@tonic-gate 		 * Now that a handle is being created, remove this state from
19247c478bd9Sstevel@tonic-gate 		 * the object so that it doesn't mistakenly get inherited by
19257c478bd9Sstevel@tonic-gate 		 * a dependency.
19267c478bd9Sstevel@tonic-gate 		 */
19277c478bd9Sstevel@tonic-gate 		oflags = FLAGS(nlmp);
19287c478bd9Sstevel@tonic-gate 		FLAGS(nlmp) &= ~FLG_RT_HANDLE;
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate 		if ((ghp = hdl_create(lml, nlmp, clmp, hflags)) == 0)
19317c478bd9Sstevel@tonic-gate 			return (0);
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 		/*
19347c478bd9Sstevel@tonic-gate 		 * Add any dependencies that are already loaded, to the handle.
19357c478bd9Sstevel@tonic-gate 		 */
19367c478bd9Sstevel@tonic-gate 		if (hdl_initialize(ghp, nlmp, clmp, nmode, promote) == 0)
19377c478bd9Sstevel@tonic-gate 			return (0);
19387c478bd9Sstevel@tonic-gate 
19397c478bd9Sstevel@tonic-gate 		if (hdl)
19407c478bd9Sstevel@tonic-gate 			*hdl = ghp;
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 		/*
19437c478bd9Sstevel@tonic-gate 		 * If we were asked to create a handle, we're done.  Otherwise,
19447c478bd9Sstevel@tonic-gate 		 * remove the handle. The handle was only used to establish this
19457c478bd9Sstevel@tonic-gate 		 * objects dependencies and promote any modes, so we don't want
19467c478bd9Sstevel@tonic-gate 		 * this handle preventing the objects deletion.  Fall through to
19477c478bd9Sstevel@tonic-gate 		 * carry out any group processing.
19487c478bd9Sstevel@tonic-gate 		 */
19497c478bd9Sstevel@tonic-gate 		if ((oflags | flags) & FLG_RT_HANDLE)
19507c478bd9Sstevel@tonic-gate 			return (1);
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 		free_hdl(ghp);
19537c478bd9Sstevel@tonic-gate 	}
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	/*
19567c478bd9Sstevel@tonic-gate 	 * If the caller isn't part of a group we're done.
19577c478bd9Sstevel@tonic-gate 	 */
19587c478bd9Sstevel@tonic-gate 	if (GROUPS(clmp) == 0)
19597c478bd9Sstevel@tonic-gate 		return (1);
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	/*
19627c478bd9Sstevel@tonic-gate 	 * Determine if our caller is already associated with a handle, if so
19637c478bd9Sstevel@tonic-gate 	 * we need to add this object to any handles that already exist.
19647c478bd9Sstevel@tonic-gate 	 * Traverse the list of groups our caller is a member of and add this
19657c478bd9Sstevel@tonic-gate 	 * new link-map to those groups.
19667c478bd9Sstevel@tonic-gate 	 */
19677c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_file_hdl_title(DBG_DEP_ADD));
19687c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(GROUPS(clmp), off, ghpp)) {
19697c478bd9Sstevel@tonic-gate 		Aliste		off1;
19707c478bd9Sstevel@tonic-gate 		Grp_desc	*gdp;
19717c478bd9Sstevel@tonic-gate 		int		exist;
19727c478bd9Sstevel@tonic-gate 		Rt_map		**lmpp;
19737c478bd9Sstevel@tonic-gate 		Alist		*lmalp = 0;
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 		ghp = *ghpp;
19767c478bd9Sstevel@tonic-gate 
19777c478bd9Sstevel@tonic-gate 		/*
19787c478bd9Sstevel@tonic-gate 		 * If the caller doesn't indicate that its dependencies should
19797c478bd9Sstevel@tonic-gate 		 * be added to a handle, ignore it.  This case identifies a
19807c478bd9Sstevel@tonic-gate 		 * parent of a dlopen(RTLD_PARENT) request.
19817c478bd9Sstevel@tonic-gate 		 */
19827c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(ghp->gh_depends, off1, gdp)) {
19837c478bd9Sstevel@tonic-gate 			if (gdp->gd_depend == clmp)
19847c478bd9Sstevel@tonic-gate 				break;
19857c478bd9Sstevel@tonic-gate 		}
19867c478bd9Sstevel@tonic-gate 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
19877c478bd9Sstevel@tonic-gate 			continue;
19887c478bd9Sstevel@tonic-gate 
19897c478bd9Sstevel@tonic-gate 		if ((exist = hdl_add(ghp, nlmp,
19907c478bd9Sstevel@tonic-gate 		    (GPD_AVAIL | GPD_ADDEPS))) == 0)
19917c478bd9Sstevel@tonic-gate 			return (0);
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate 		/*
19947c478bd9Sstevel@tonic-gate 		 * If this member already exists then its dependencies will
19957c478bd9Sstevel@tonic-gate 		 * have already been processed.
19967c478bd9Sstevel@tonic-gate 		 */
19977c478bd9Sstevel@tonic-gate 		if (exist == ALE_EXISTS)
19987c478bd9Sstevel@tonic-gate 			continue;
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 		/*
20017c478bd9Sstevel@tonic-gate 		 * If the object we've added has just been opened, it will not
20027c478bd9Sstevel@tonic-gate 		 * yet have been processed for its dependencies, these will be
20037c478bd9Sstevel@tonic-gate 		 * added on later calls to load_one().  If it doesn't have any
20047c478bd9Sstevel@tonic-gate 		 * dependencies we're also done.
20057c478bd9Sstevel@tonic-gate 		 */
20067c478bd9Sstevel@tonic-gate 		if (((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0) ||
20077c478bd9Sstevel@tonic-gate 		    (DEPENDS(nlmp) == 0))
20087c478bd9Sstevel@tonic-gate 			continue;
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 		/*
20117c478bd9Sstevel@tonic-gate 		 * Otherwise, this object exists and has dependencies, so add
20127c478bd9Sstevel@tonic-gate 		 * all of its dependencies to the handle were operating on.
20137c478bd9Sstevel@tonic-gate 		 */
20147c478bd9Sstevel@tonic-gate 		if (alist_append(&lmalp, &nlmp, sizeof (Rt_map *),
20157c478bd9Sstevel@tonic-gate 		    AL_CNT_DEPCLCT) == 0)
20167c478bd9Sstevel@tonic-gate 			return (0);
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(lmalp, off1, lmpp)) {
20197c478bd9Sstevel@tonic-gate 			Rt_map *	dlmp1 = *lmpp;
20207c478bd9Sstevel@tonic-gate 			Aliste		off2;
20217c478bd9Sstevel@tonic-gate 			Bnd_desc **	bdpp;
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 			/*
20247c478bd9Sstevel@tonic-gate 			 * Add any dependencies of this dependency to the
20257c478bd9Sstevel@tonic-gate 			 * dynamic dependency list so they can be further
20267c478bd9Sstevel@tonic-gate 			 * processed.
20277c478bd9Sstevel@tonic-gate 			 */
20287c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(DEPENDS(dlmp1), off2, bdpp)) {
20297c478bd9Sstevel@tonic-gate 				Bnd_desc *	bdp = *bdpp;
20307c478bd9Sstevel@tonic-gate 				Rt_map *	dlmp2 = bdp->b_depend;
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 				if ((bdp->b_flags & BND_NEEDED) == 0)
20337c478bd9Sstevel@tonic-gate 					continue;
20347c478bd9Sstevel@tonic-gate 
20357c478bd9Sstevel@tonic-gate 				if (alist_test(&lmalp, dlmp2, sizeof (Rt_map *),
20367c478bd9Sstevel@tonic-gate 				    AL_CNT_DEPCLCT) == 0) {
20377c478bd9Sstevel@tonic-gate 					free(lmalp);
20387c478bd9Sstevel@tonic-gate 					return (0);
20397c478bd9Sstevel@tonic-gate 				}
20407c478bd9Sstevel@tonic-gate 			}
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 			if (nlmp == dlmp1)
20437c478bd9Sstevel@tonic-gate 				continue;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 			if ((exist = hdl_add(ghp, dlmp1,
20467c478bd9Sstevel@tonic-gate 			    (GPD_AVAIL | GPD_ADDEPS))) != 0) {
20477c478bd9Sstevel@tonic-gate 				if (exist == ALE_CREATE)
20487c478bd9Sstevel@tonic-gate 				    (void) update_mode(dlmp1, MODE(dlmp1),
20497c478bd9Sstevel@tonic-gate 					nmode);
20507c478bd9Sstevel@tonic-gate 				continue;
20517c478bd9Sstevel@tonic-gate 			}
20527c478bd9Sstevel@tonic-gate 			free(lmalp);
20537c478bd9Sstevel@tonic-gate 			return (0);
20547c478bd9Sstevel@tonic-gate 		}
20557c478bd9Sstevel@tonic-gate 		free(lmalp);
20567c478bd9Sstevel@tonic-gate 	}
20577c478bd9Sstevel@tonic-gate 	return (1);
20587c478bd9Sstevel@tonic-gate }
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate /*
20617c478bd9Sstevel@tonic-gate  * The central routine for loading shared objects.  Insures ldd() diagnostics,
20627c478bd9Sstevel@tonic-gate  * handles and any other related additions are all done in one place.
20637c478bd9Sstevel@tonic-gate  */
2064*fb1354edSrie static Rt_map *
2065*fb1354edSrie _load_path(Lm_list *lml, Aliste lmco, const char *name, Rt_map *clmp,
2066*fb1354edSrie     int nmode, uint_t flags, Grp_hdl ** hdl, Fdesc *nfdp, Rej_desc *rej)
20677c478bd9Sstevel@tonic-gate {
20687c478bd9Sstevel@tonic-gate 	Rt_map	*nlmp;
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 	if ((nmode & RTLD_NOLOAD) == 0) {
20717c478bd9Sstevel@tonic-gate 		/*
20727c478bd9Sstevel@tonic-gate 		 * If this isn't a noload request attempt to load the file.
20737c478bd9Sstevel@tonic-gate 		 */
20747c478bd9Sstevel@tonic-gate 		if ((name = load_trace(lml, name, clmp)) == 0)
20757c478bd9Sstevel@tonic-gate 			return (0);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 		if ((nlmp = load_so(lml, lmco, name, clmp, flags,
2078*fb1354edSrie 		    nfdp, rej)) == 0)
20797c478bd9Sstevel@tonic-gate 			return (0);
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 		/*
20827c478bd9Sstevel@tonic-gate 		 * If we've loaded a library which identifies itself as not
20837c478bd9Sstevel@tonic-gate 		 * being dlopen()'able catch it here.  Let non-dlopen()'able
20847c478bd9Sstevel@tonic-gate 		 * objects through under RTLD_CONFGEN as they're only being
20857c478bd9Sstevel@tonic-gate 		 * mapped to be dldump()'ed.
20867c478bd9Sstevel@tonic-gate 		 */
20877c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) && ((FLAGS(nlmp) &
20887c478bd9Sstevel@tonic-gate 		    (FLG_RT_NOOPEN | FLG_RT_RELOCED)) == FLG_RT_NOOPEN) &&
20897c478bd9Sstevel@tonic-gate 		    ((nmode & RTLD_CONFGEN) == 0)) {
20907c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
20937c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
20947c478bd9Sstevel@tonic-gate 			_rej.rej_str = MSG_INTL(MSG_GEN_NOOPEN);
20957c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_file_rejected(&_rej));
2096*fb1354edSrie 			rejection_inherit(rej, &_rej, nfdp);
20977c478bd9Sstevel@tonic-gate 			remove_so(lml, nlmp);
20987c478bd9Sstevel@tonic-gate 			return (0);
20997c478bd9Sstevel@tonic-gate 		}
21007c478bd9Sstevel@tonic-gate 	} else {
21017c478bd9Sstevel@tonic-gate 		/*
21027c478bd9Sstevel@tonic-gate 		 * If it's a NOLOAD request - check to see if the object
21037c478bd9Sstevel@tonic-gate 		 * has already been loaded.
21047c478bd9Sstevel@tonic-gate 		 */
21057c478bd9Sstevel@tonic-gate 		/* LINTED */
21067c478bd9Sstevel@tonic-gate 		if ((nlmp = is_so_loaded(lml, name, 0)) ||
21077c478bd9Sstevel@tonic-gate 		    (nlmp = is_so_loaded(lml, name, 1))) {
21087c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
21097c478bd9Sstevel@tonic-gate 			    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0)) {
21107c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FIND), name,
21117c478bd9Sstevel@tonic-gate 					NAME(clmp));
21127c478bd9Sstevel@tonic-gate 				if (*name == '/')
21137c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_PATH),
21147c478bd9Sstevel@tonic-gate 					name, MSG_ORIG(MSG_STR_EMPTY),
21157c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
21167c478bd9Sstevel@tonic-gate 				else
21177c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_FIL_EQUIV),
21187c478bd9Sstevel@tonic-gate 					name, NAME(nlmp),
21197c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY),
21207c478bd9Sstevel@tonic-gate 					MSG_ORIG(MSG_STR_EMPTY));
21217c478bd9Sstevel@tonic-gate 			}
21227c478bd9Sstevel@tonic-gate 		} else {
21237c478bd9Sstevel@tonic-gate 			Rej_desc	_rej = { 0 };
21247c478bd9Sstevel@tonic-gate 
21257c478bd9Sstevel@tonic-gate 			_rej.rej_name = name;
21267c478bd9Sstevel@tonic-gate 			_rej.rej_type = SGS_REJ_STR;
21277c478bd9Sstevel@tonic-gate 			_rej.rej_str = strerror(ENOENT);
21287c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_file_rejected(&_rej));
2129*fb1354edSrie 			rejection_inherit(rej, &_rej, nfdp);
21307c478bd9Sstevel@tonic-gate 			return (0);
21317c478bd9Sstevel@tonic-gate 		}
21327c478bd9Sstevel@tonic-gate 	}
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	/*
21357c478bd9Sstevel@tonic-gate 	 * Finish processing this loaded object.
21367c478bd9Sstevel@tonic-gate 	 */
2137390b98b5Srie 	if (load_finish(lml, name, clmp, nmode, flags, hdl, nlmp) == 0) {
2138390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2139390b98b5Srie 
2140390b98b5Srie 		/*
2141390b98b5Srie 		 * If this object has already been analyzed, then it is in use,
2142390b98b5Srie 		 * so even though this operation has failed, it should not be
2143390b98b5Srie 		 * torn down.
2144390b98b5Srie 		 */
2145390b98b5Srie 		if ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)
2146390b98b5Srie 			remove_so(lml, nlmp);
2147390b98b5Srie 		return (0);
2148390b98b5Srie 	}
21497c478bd9Sstevel@tonic-gate 
21507c478bd9Sstevel@tonic-gate 	/*
2151390b98b5Srie 	 * If this object is new, and we're being audited, tell the audit
2152390b98b5Srie 	 * library of the file we've just opened.  Note, if the new link-map
2153390b98b5Srie 	 * requires local auditing of its dependencies we also register its
2154390b98b5Srie 	 * opening.
21557c478bd9Sstevel@tonic-gate 	 */
2156390b98b5Srie 	if (FLAGS(nlmp) & FLG_RT_NEWLOAD) {
2157390b98b5Srie 		FLAGS(nlmp) &= ~FLG_RT_NEWLOAD;
2158390b98b5Srie 
2159390b98b5Srie 		if (((lml->lm_tflags | FLAGS1(clmp) | FLAGS1(nlmp)) &
2160390b98b5Srie 		    LML_TFLG_AUD_MASK) && (((lml->lm_flags |
2161390b98b5Srie 		    LIST(clmp)->lm_flags) & LML_FLG_NOAUDIT) == 0)) {
2162390b98b5Srie 			if (audit_objopen(clmp, nlmp) == 0) {
2163390b98b5Srie 				remove_so(lml, nlmp);
2164390b98b5Srie 				return (0);
2165390b98b5Srie 			}
2166390b98b5Srie 		}
2167390b98b5Srie 	}
2168390b98b5Srie 	return (nlmp);
21697c478bd9Sstevel@tonic-gate }
21707c478bd9Sstevel@tonic-gate 
2171*fb1354edSrie Rt_map *
2172*fb1354edSrie load_path(Lm_list *lml, Aliste lmco, const char *name, Rt_map *clmp,
2173*fb1354edSrie     int nmode, uint_t flags, Grp_hdl **hdl, Fdesc *cfdp, Rej_desc *rej)
2174*fb1354edSrie {
2175*fb1354edSrie 	Rt_map	*lmp;
2176*fb1354edSrie 	Fdesc	nfdp = { 0 };
2177*fb1354edSrie 
2178*fb1354edSrie 	/*
2179*fb1354edSrie 	 * If this path resulted from a $HWCAP specification, then the best
2180*fb1354edSrie 	 * hardware capability object has already been establish, and is
2181*fb1354edSrie 	 * available in the calling file descriptor.
2182*fb1354edSrie 	 */
2183*fb1354edSrie 	if (flags & FLG_RT_HWCAP) {
2184*fb1354edSrie 		if (cfdp->fd_lmp == 0) {
2185*fb1354edSrie 			/*
2186*fb1354edSrie 			 * If this object hasn't yet been mapped, re-establish
2187*fb1354edSrie 			 * the file descriptor structure to reflect this objects
2188*fb1354edSrie 			 * original initial page mapping.  Make sure any present
2189*fb1354edSrie 			 * file descriptor mapping is removed before overwriting
2190*fb1354edSrie 			 * the structure.
2191*fb1354edSrie 			 */
2192*fb1354edSrie #if	defined(MAP_ALIGN)
2193*fb1354edSrie 			if (fmap->fm_maddr &&
2194*fb1354edSrie 			    ((fmap->fm_mflags & MAP_ALIGN) == 0))
2195*fb1354edSrie #else
2196*fb1354edSrie 			if (fmap->fm_maddr)
2197*fb1354edSrie #endif
2198*fb1354edSrie 				(void) munmap(fmap->fm_maddr, fmap->fm_msize);
2199*fb1354edSrie 		}
2200*fb1354edSrie 		nfdp = *cfdp;
2201*fb1354edSrie 		*fmap = cfdp->fd_fmap;
2202*fb1354edSrie 	}
2203*fb1354edSrie 
2204*fb1354edSrie 	lmp = _load_path(lml, lmco, name, clmp, nmode, flags, hdl, &nfdp, rej);
2205*fb1354edSrie 
2206*fb1354edSrie 	/*
2207*fb1354edSrie 	 * If this path originated from a $HWCAP specification, re-establish the
2208*fb1354edSrie 	 * fdesc information.  For single paged objects, such as filters, the
2209*fb1354edSrie 	 * original mapping may have been sufficient to capture the file, thus
2210*fb1354edSrie 	 * this mapping needs to be reset to insure it doesn't mistakenly get
2211*fb1354edSrie 	 * unmapped as part of HWCAP cleanup.
2212*fb1354edSrie 	 */
2213*fb1354edSrie 	if (flags & FLG_RT_HWCAP) {
2214*fb1354edSrie 		cfdp->fd_fmap.fm_maddr = fmap->fm_maddr;
2215*fb1354edSrie 		cfdp->fd_fmap.fm_mflags = fmap->fm_mflags;
2216*fb1354edSrie 		cfdp->fd_fd = nfdp.fd_fd;
2217*fb1354edSrie 	}
2218*fb1354edSrie 
2219*fb1354edSrie 	return (lmp);
2220*fb1354edSrie }
2221*fb1354edSrie 
22227c478bd9Sstevel@tonic-gate /*
22237c478bd9Sstevel@tonic-gate  * Load one object from a possible list of objects.  Typically, for requests
22247c478bd9Sstevel@tonic-gate  * such as NEEDED's, only one object is specified.  However, this object could
22257c478bd9Sstevel@tonic-gate  * be specified using $ISALIST or $HWCAP, in which case only the first object
22267c478bd9Sstevel@tonic-gate  * that can be loaded is used (ie. the best).
22277c478bd9Sstevel@tonic-gate  */
22287c478bd9Sstevel@tonic-gate Rt_map *
22297c478bd9Sstevel@tonic-gate load_one(Lm_list *lml, Aliste lmco, Pnode *pnp, Rt_map *clmp, int mode,
22307c478bd9Sstevel@tonic-gate     uint_t flags, Grp_hdl ** hdl)
22317c478bd9Sstevel@tonic-gate {
22327c478bd9Sstevel@tonic-gate 	Rej_desc	rej = { 0 };
22337c478bd9Sstevel@tonic-gate 	Pnode   	*tpnp;
22347c478bd9Sstevel@tonic-gate 	const char	*name;
22357c478bd9Sstevel@tonic-gate 	Rt_map		*tlmp;
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate 	for (tpnp = pnp; tpnp && tpnp->p_name; tpnp = tpnp->p_next) {
22387c478bd9Sstevel@tonic-gate 		/*
22397c478bd9Sstevel@tonic-gate 		 * A Hardware capabilities requirement can itself expand into
22407c478bd9Sstevel@tonic-gate 		 * a number of candidates.
22417c478bd9Sstevel@tonic-gate 		 */
22427c478bd9Sstevel@tonic-gate 		if (tpnp->p_orig & PN_TKN_HWCAP) {
22437c478bd9Sstevel@tonic-gate 			if ((tlmp = load_hwcap(lml, lmco, tpnp->p_name, clmp,
22447c478bd9Sstevel@tonic-gate 			    mode, (flags | FLG_RT_HWCAP), hdl, &rej)) != 0) {
22457c478bd9Sstevel@tonic-gate 				remove_rej(&rej);
22467c478bd9Sstevel@tonic-gate 				return (tlmp);
22477c478bd9Sstevel@tonic-gate 			}
22487c478bd9Sstevel@tonic-gate 		} else {
22497c478bd9Sstevel@tonic-gate 			if ((tlmp = load_path(lml, lmco, tpnp->p_name, clmp,
22507c478bd9Sstevel@tonic-gate 			    mode, flags, hdl, 0, &rej)) != 0) {
22517c478bd9Sstevel@tonic-gate 				remove_rej(&rej);
22527c478bd9Sstevel@tonic-gate 				return (tlmp);
22537c478bd9Sstevel@tonic-gate 			}
22547c478bd9Sstevel@tonic-gate 		}
22557c478bd9Sstevel@tonic-gate 	}
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	/*
22587c478bd9Sstevel@tonic-gate 	 * If this pathname originated from an expanded token, use the original
22597c478bd9Sstevel@tonic-gate 	 * for any diagnostic output.
22607c478bd9Sstevel@tonic-gate 	 */
22617c478bd9Sstevel@tonic-gate 	if ((name = pnp->p_oname) == 0)
22627c478bd9Sstevel@tonic-gate 		name = pnp->p_name;
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate 	file_notfound(lml, name, clmp, flags, &rej);
22657c478bd9Sstevel@tonic-gate 	remove_rej(&rej);
22667c478bd9Sstevel@tonic-gate 	return (0);
22677c478bd9Sstevel@tonic-gate }
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate /*
22707c478bd9Sstevel@tonic-gate  * While processing direct or group bindings, determine whether the object to
22717c478bd9Sstevel@tonic-gate  * which we've bound can be interposed upon.  In this context, copy relocations
22727c478bd9Sstevel@tonic-gate  * are a form of interposition.
22737c478bd9Sstevel@tonic-gate  */
22747c478bd9Sstevel@tonic-gate static Sym *
22757c478bd9Sstevel@tonic-gate lookup_sym_interpose(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Lm_list * lml,
22767c478bd9Sstevel@tonic-gate     Sym * sym)
22777c478bd9Sstevel@tonic-gate {
22787c478bd9Sstevel@tonic-gate 	Rt_map *	lmp;
22797c478bd9Sstevel@tonic-gate 	Slookup		sl;
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	/*
22827c478bd9Sstevel@tonic-gate 	 * If we've bound to a copy relocation definition then we need to assign
22837c478bd9Sstevel@tonic-gate 	 * this binding to the original copy reference.  Fabricate an inter-
22847c478bd9Sstevel@tonic-gate 	 * position diagnostic, as this is a legitimate form of interposition.
22857c478bd9Sstevel@tonic-gate 	 */
22867c478bd9Sstevel@tonic-gate 	if (FLAGS1(*dlmp) & FL1_RT_COPYTOOK) {
22877c478bd9Sstevel@tonic-gate 		Rel_copy	*rcp;
22887c478bd9Sstevel@tonic-gate 		Aliste		off;
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(COPY(*dlmp), off, rcp)) {
22917c478bd9Sstevel@tonic-gate 			if ((sym == rcp->r_dsym) || (sym->st_value &&
22927c478bd9Sstevel@tonic-gate 			    (sym->st_value == rcp->r_dsym->st_value))) {
22937c478bd9Sstevel@tonic-gate 				*dlmp = rcp->r_rlmp;
22947c478bd9Sstevel@tonic-gate 				*binfo |=
22957c478bd9Sstevel@tonic-gate 				    (DBG_BINFO_INTERPOSE | DBG_BINFO_COPYREF);
22967c478bd9Sstevel@tonic-gate 				return (rcp->r_rsym);
22977c478bd9Sstevel@tonic-gate 			}
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	if ((lml->lm_flags & LML_FLG_INTRPOSE) == 0)
23027c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 	/*
23057c478bd9Sstevel@tonic-gate 	 * Traverse the list of known interposers to determine whether any
23067c478bd9Sstevel@tonic-gate 	 * offer the same symbol.  Note, the head of the link-map could be
23077c478bd9Sstevel@tonic-gate 	 * identified as an interposer.  If it is, make sure we only look for
23087c478bd9Sstevel@tonic-gate 	 * symbol definitions.  Otherwise, skip the head of the link-map, so
23097c478bd9Sstevel@tonic-gate 	 * that we don't bind to any .plt references, or copy-relocations
23107c478bd9Sstevel@tonic-gate 	 * unintentionally.
23117c478bd9Sstevel@tonic-gate 	 */
23127c478bd9Sstevel@tonic-gate 	lmp = lml->lm_head;
23137c478bd9Sstevel@tonic-gate 	sl = *slp;
23147c478bd9Sstevel@tonic-gate 	if (((FLAGS(lmp) & FLG_RT_INTRPOSE) == 0) || (sl.sl_flags & LKUP_COPY))
23157c478bd9Sstevel@tonic-gate 		lmp = (Rt_map *)NEXT(lmp);
23167c478bd9Sstevel@tonic-gate 	else
23177c478bd9Sstevel@tonic-gate 		sl.sl_flags &= ~LKUP_SPEC;
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
23207c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_DELETE)
23217c478bd9Sstevel@tonic-gate 			continue;
23227c478bd9Sstevel@tonic-gate 		if ((FLAGS(lmp) & FLG_RT_INTRPOSE) == 0)
23237c478bd9Sstevel@tonic-gate 			break;
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 		if (callable(lmp, *dlmp, 0)) {
23267c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
23277c478bd9Sstevel@tonic-gate 			if (sym = SYMINTP(lmp)(&sl, dlmp, binfo)) {
23287c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
23297c478bd9Sstevel@tonic-gate 				return (sym);
23307c478bd9Sstevel@tonic-gate 			}
23317c478bd9Sstevel@tonic-gate 		}
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate /*
23377c478bd9Sstevel@tonic-gate  * If an object specifies direct bindings (it contains a syminfo structure
23387c478bd9Sstevel@tonic-gate  * describing where each binding was established during link-editing, and the
23397c478bd9Sstevel@tonic-gate  * object was built -Bdirect), then look for the symbol in the specific object.
23407c478bd9Sstevel@tonic-gate  */
23417c478bd9Sstevel@tonic-gate static Sym *
23427c478bd9Sstevel@tonic-gate lookup_sym_direct(Slookup *slp, Rt_map **dlmp, uint_t *binfo, Syminfo *sip,
23437c478bd9Sstevel@tonic-gate     Rt_map *lmp)
23447c478bd9Sstevel@tonic-gate {
23457c478bd9Sstevel@tonic-gate 	Rt_map	*clmp = slp->sl_cmap;
23467c478bd9Sstevel@tonic-gate 	Sym	*sym;
23477c478bd9Sstevel@tonic-gate 	Slookup	sl;
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 	/*
23507c478bd9Sstevel@tonic-gate 	 * If a direct binding resolves to the definition of a copy relocated
23517c478bd9Sstevel@tonic-gate 	 * variable, it must be redirected to the copy (in the executable) that
23527c478bd9Sstevel@tonic-gate 	 * will eventually be made.  Typically, this redirection occurs in
23537c478bd9Sstevel@tonic-gate 	 * lookup_sym_interpose().  But, there's an edge condition.  If a
23547c478bd9Sstevel@tonic-gate 	 * directly bound executable contains pic code, there may be a
23557c478bd9Sstevel@tonic-gate 	 * reference to a definition that will eventually have a copy made.
23567c478bd9Sstevel@tonic-gate 	 * However, this copy relocation may not yet have occurred, because
23577c478bd9Sstevel@tonic-gate 	 * the relocation making this reference comes before the relocation
23587c478bd9Sstevel@tonic-gate 	 * that will create the copy.
23597c478bd9Sstevel@tonic-gate 	 * Under direct bindings, the syminfo indicates that a copy will be
23607c478bd9Sstevel@tonic-gate 	 * taken (SYMINFO_FLG_COPY).  This can only be set in an executable.
23617c478bd9Sstevel@tonic-gate 	 * Thus, the caller must be the executable, so bind to the destination
23627c478bd9Sstevel@tonic-gate 	 * of the copy within the executable.
23637c478bd9Sstevel@tonic-gate 	 */
23647c478bd9Sstevel@tonic-gate 	if (((slp->sl_flags & LKUP_COPY) == 0) &&
23657c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_COPY)) {
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate 		slp->sl_imap = LIST(clmp)->lm_head;
23687c478bd9Sstevel@tonic-gate 		if (sym = SYMINTP(clmp)(slp, dlmp, binfo))
23697c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_DIRECT | DBG_BINFO_COPYREF);
23707c478bd9Sstevel@tonic-gate 		return (sym);
23717c478bd9Sstevel@tonic-gate 	}
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 	/*
23747c478bd9Sstevel@tonic-gate 	 * If we need to direct bind to our parent start looking in each caller
23757c478bd9Sstevel@tonic-gate 	 * link map.
23767c478bd9Sstevel@tonic-gate 	 */
23777c478bd9Sstevel@tonic-gate 	sl = *slp;
23787c478bd9Sstevel@tonic-gate 	sl.sl_flags |= LKUP_DIRECT;
23797c478bd9Sstevel@tonic-gate 	sym = 0;
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	if (sip->si_boundto == SYMINFO_BT_PARENT) {
23827c478bd9Sstevel@tonic-gate 		Aliste		off;
23837c478bd9Sstevel@tonic-gate 		Bnd_desc **	bdpp;
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(CALLERS(clmp), off, bdpp)) {
23867c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp = (*bdpp)->b_caller;
23877c478bd9Sstevel@tonic-gate 			if ((sym = SYMINTP(lmp)(&sl, dlmp, binfo)) != 0)
23887c478bd9Sstevel@tonic-gate 				break;
23897c478bd9Sstevel@tonic-gate 		}
23907c478bd9Sstevel@tonic-gate 	} else {
23917c478bd9Sstevel@tonic-gate 		/*
23927c478bd9Sstevel@tonic-gate 		 * If we need to direct bind to anything else look in the
23937c478bd9Sstevel@tonic-gate 		 * link map associated with this symbol reference.
23947c478bd9Sstevel@tonic-gate 		 */
23957c478bd9Sstevel@tonic-gate 		if (sip->si_boundto == SYMINFO_BT_SELF)
23967c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp = clmp;
23977c478bd9Sstevel@tonic-gate 		else
23987c478bd9Sstevel@tonic-gate 			sl.sl_imap = lmp;
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 		if (lmp)
24017c478bd9Sstevel@tonic-gate 			sym = SYMINTP(lmp)(&sl, dlmp, binfo);
24027c478bd9Sstevel@tonic-gate 	}
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	if (sym)
24057c478bd9Sstevel@tonic-gate 		*binfo |= DBG_BINFO_DIRECT;
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	/*
24087c478bd9Sstevel@tonic-gate 	 * If we've bound to an object, determine whether that object can be
24097c478bd9Sstevel@tonic-gate 	 * interposed upon for this symbol.
24107c478bd9Sstevel@tonic-gate 	 */
24117c478bd9Sstevel@tonic-gate 	if (sym && (LIST(*dlmp)->lm_head != *dlmp) &&
24127c478bd9Sstevel@tonic-gate 	    (LIST(*dlmp) == LIST(clmp))) {
24137c478bd9Sstevel@tonic-gate 		Sym *	isym;
24147c478bd9Sstevel@tonic-gate 
24157c478bd9Sstevel@tonic-gate 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo,
24167c478bd9Sstevel@tonic-gate 		    LIST(*dlmp), sym)) != 0)
24177c478bd9Sstevel@tonic-gate 			return (isym);
24187c478bd9Sstevel@tonic-gate 	}
24197c478bd9Sstevel@tonic-gate 
24207c478bd9Sstevel@tonic-gate 	return (sym);
24217c478bd9Sstevel@tonic-gate }
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate static Sym *
24247c478bd9Sstevel@tonic-gate _lookup_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo,
24257c478bd9Sstevel@tonic-gate     Aliste off)
24267c478bd9Sstevel@tonic-gate {
24277c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	/*
24307c478bd9Sstevel@tonic-gate 	 * Copy relocations should start their search after the head of the
24317c478bd9Sstevel@tonic-gate 	 * main link-map control list.
24327c478bd9Sstevel@tonic-gate 	 */
24337c478bd9Sstevel@tonic-gate 	if ((off == ALO_DATA) && (slp->sl_flags & LKUP_COPY) && ilmp)
24347c478bd9Sstevel@tonic-gate 		lmp = (Rt_map *)NEXT(ilmp);
24357c478bd9Sstevel@tonic-gate 	else
24367c478bd9Sstevel@tonic-gate 		lmp = ilmp;
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	for (; lmp; lmp = (Rt_map *)NEXT(lmp)) {
24397c478bd9Sstevel@tonic-gate 		if (callable(slp->sl_cmap, lmp, 0)) {
24407c478bd9Sstevel@tonic-gate 			Sym	*sym;
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
24437c478bd9Sstevel@tonic-gate 			if ((sym = SYMINTP(lmp)(slp, dlmp, binfo)) != 0)
24447c478bd9Sstevel@tonic-gate 				return (sym);
24457c478bd9Sstevel@tonic-gate 		}
24467c478bd9Sstevel@tonic-gate 	}
24477c478bd9Sstevel@tonic-gate 	return (0);
24487c478bd9Sstevel@tonic-gate }
24497c478bd9Sstevel@tonic-gate 
24507c478bd9Sstevel@tonic-gate static Sym *
24517c478bd9Sstevel@tonic-gate _lazy_find_sym(Rt_map *ilmp, Slookup *slp, Rt_map **dlmp, uint_t *binfo)
24527c478bd9Sstevel@tonic-gate {
24537c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	for (lmp = ilmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
24567c478bd9Sstevel@tonic-gate 		if (LAZY(lmp) == 0)
24577c478bd9Sstevel@tonic-gate 			continue;
24587c478bd9Sstevel@tonic-gate 		if (callable(slp->sl_cmap, lmp, 0)) {
24597c478bd9Sstevel@tonic-gate 			Sym	*sym;
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 			slp->sl_imap = lmp;
24627c478bd9Sstevel@tonic-gate 			if ((sym = elf_lazy_find_sym(slp, dlmp, binfo)) != 0)
24637c478bd9Sstevel@tonic-gate 				return (sym);
24647c478bd9Sstevel@tonic-gate 		}
24657c478bd9Sstevel@tonic-gate 	}
24667c478bd9Sstevel@tonic-gate 	return (0);
24677c478bd9Sstevel@tonic-gate }
24687c478bd9Sstevel@tonic-gate 
24697c478bd9Sstevel@tonic-gate /*
24707c478bd9Sstevel@tonic-gate  * Symbol lookup routine.  Takes an ELF symbol name, and a list of link maps to
24717c478bd9Sstevel@tonic-gate  * search (if the flag indicates LKUP_FIRST only the first link map of the list
24727c478bd9Sstevel@tonic-gate  * is searched ie. we've been called from dlsym()).
24737c478bd9Sstevel@tonic-gate  * If successful, return a pointer to the symbol table entry and a pointer to
24747c478bd9Sstevel@tonic-gate  * the link map of the enclosing object.  Else return a null pointer.
24757c478bd9Sstevel@tonic-gate  *
24767c478bd9Sstevel@tonic-gate  * To improve elf performance, we first compute the elf hash value and pass
24777c478bd9Sstevel@tonic-gate  * it to each find_sym() routine.  The elf function will use this value to
24787c478bd9Sstevel@tonic-gate  * locate the symbol, the a.out function will simply ignore it.
24797c478bd9Sstevel@tonic-gate  */
24807c478bd9Sstevel@tonic-gate Sym *
24817c478bd9Sstevel@tonic-gate lookup_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
24827c478bd9Sstevel@tonic-gate {
24837c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
24847c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
24857c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap, *lmp;
24867c478bd9Sstevel@tonic-gate 	uint_t		flags = slp->sl_flags;
24877c478bd9Sstevel@tonic-gate 	ulong_t		rsymndx;
24887c478bd9Sstevel@tonic-gate 	Sym		*sym = 0;
24897c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
24907c478bd9Sstevel@tonic-gate 	Slookup		sl;
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	if (slp->sl_hash == 0)
24937c478bd9Sstevel@tonic-gate 		slp->sl_hash = elf_hash(name);
24947c478bd9Sstevel@tonic-gate 	*binfo = 0;
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	/*
24977c478bd9Sstevel@tonic-gate 	 * Search the initial link map for the required symbol (this category is
24987c478bd9Sstevel@tonic-gate 	 * selected by dlsym(), where individual link maps are searched for a
24997c478bd9Sstevel@tonic-gate 	 * required symbol.  Therefore, we know we have permission to look at
25007c478bd9Sstevel@tonic-gate 	 * the link map).
25017c478bd9Sstevel@tonic-gate 	 */
25027c478bd9Sstevel@tonic-gate 	if (flags & LKUP_FIRST)
25037c478bd9Sstevel@tonic-gate 		return (SYMINTP(ilmp)(slp, dlmp, binfo));
25047c478bd9Sstevel@tonic-gate 
25057c478bd9Sstevel@tonic-gate 	/*
25067c478bd9Sstevel@tonic-gate 	 * Determine whether this lookup can be satisfied by an objects direct,
25077c478bd9Sstevel@tonic-gate 	 * or lazy binding information.  This is triggered by a relocation from
25087c478bd9Sstevel@tonic-gate 	 * the object (hence rsymndx is set).
25097c478bd9Sstevel@tonic-gate 	 */
25107c478bd9Sstevel@tonic-gate 	if (((rsymndx = slp->sl_rsymndx) != 0) &&
25117c478bd9Sstevel@tonic-gate 	    ((sip = SYMINFO(clmp)) != 0)) {
25127c478bd9Sstevel@tonic-gate 
25137c478bd9Sstevel@tonic-gate 		/*
25147c478bd9Sstevel@tonic-gate 		 * Find the corresponding Syminfo entry for the original
25157c478bd9Sstevel@tonic-gate 		 * referencing symbol.
25167c478bd9Sstevel@tonic-gate 		 */
25177c478bd9Sstevel@tonic-gate 		/* LINTED */
25187c478bd9Sstevel@tonic-gate 		sip = (Syminfo *)((char *)sip + (rsymndx * SYMINENT(clmp)));
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 		/*
25217c478bd9Sstevel@tonic-gate 		 * If the symbol information indicates a direct binding,
25227c478bd9Sstevel@tonic-gate 		 * determine the link map that is required to satisfy the
25237c478bd9Sstevel@tonic-gate 		 * binding.  Note, if the dependency can not be found, but a
25247c478bd9Sstevel@tonic-gate 		 * direct binding isn't required, we will still fall through
25257c478bd9Sstevel@tonic-gate 		 * to perform any default symbol search.
25267c478bd9Sstevel@tonic-gate 		 */
25277c478bd9Sstevel@tonic-gate 		if (sip->si_flags & SYMINFO_FLG_DIRECT) {
25287c478bd9Sstevel@tonic-gate 			uint_t	bound = sip->si_boundto;
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 			lmp = 0;
25317c478bd9Sstevel@tonic-gate 			if (bound < SYMINFO_BT_LOWRESERVE)
25327c478bd9Sstevel@tonic-gate 				lmp = elf_lazy_load(clmp, bound, name);
25337c478bd9Sstevel@tonic-gate 
25347c478bd9Sstevel@tonic-gate 			/*
25357c478bd9Sstevel@tonic-gate 			 * If direct bindings have been disabled, and this isn't
25367c478bd9Sstevel@tonic-gate 			 * a translator, skip any direct binding now that we've
25377c478bd9Sstevel@tonic-gate 			 * insured the resolving object has been loaded.
25387c478bd9Sstevel@tonic-gate 			 *
25397c478bd9Sstevel@tonic-gate 			 * If we need to direct bind to anything, we look in
25407c478bd9Sstevel@tonic-gate 			 * ourselves, our parent, or in the link map we've just
25417c478bd9Sstevel@tonic-gate 			 * loaded.  Otherwise, even though we may have lazily
25427c478bd9Sstevel@tonic-gate 			 * loaded an object we still continue to search for
25437c478bd9Sstevel@tonic-gate 			 * symbols from the head of the link map list.
25447c478bd9Sstevel@tonic-gate 			 */
25457c478bd9Sstevel@tonic-gate 			if (((FLAGS(clmp) & FLG_RT_TRANS) ||
25467c478bd9Sstevel@tonic-gate 			    (!(LIST(clmp)->lm_tflags & LML_TFLG_NODIRECT))) &&
25477c478bd9Sstevel@tonic-gate 			    ((FLAGS(clmp) & FLG_RT_DIRECT) ||
25487c478bd9Sstevel@tonic-gate 			    (sip->si_flags & SYMINFO_FLG_DIRECTBIND))) {
25497c478bd9Sstevel@tonic-gate 				sym = lookup_sym_direct(slp, dlmp, binfo,
25507c478bd9Sstevel@tonic-gate 				    sip, lmp);
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 				/*
25537c478bd9Sstevel@tonic-gate 				 * If this direct binding has been disabled
25547c478bd9Sstevel@tonic-gate 				 * (presumably because the symbol definition has
25557c478bd9Sstevel@tonic-gate 				 * been changed since the referring object was
25567c478bd9Sstevel@tonic-gate 				 * built), fall back to a standard symbol
25577c478bd9Sstevel@tonic-gate 				 * search.
25587c478bd9Sstevel@tonic-gate 				 */
25597c478bd9Sstevel@tonic-gate 				if ((*binfo & BINFO_DIRECTDIS) == 0)
25607c478bd9Sstevel@tonic-gate 					return (sym);
25617c478bd9Sstevel@tonic-gate 			}
25627c478bd9Sstevel@tonic-gate 		}
25637c478bd9Sstevel@tonic-gate 	}
25647c478bd9Sstevel@tonic-gate 
25657c478bd9Sstevel@tonic-gate 	sl = *slp;
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	/*
25687c478bd9Sstevel@tonic-gate 	 * If the referencing object has the DF_SYMBOLIC flag set, look in the
25697c478bd9Sstevel@tonic-gate 	 * referencing object for the symbol first.  Failing that, fall back to
25707c478bd9Sstevel@tonic-gate 	 * our generic search.
25717c478bd9Sstevel@tonic-gate 	 */
25727c478bd9Sstevel@tonic-gate 	if (FLAGS1(clmp) & FL1_RT_SYMBOLIC) {
25737c478bd9Sstevel@tonic-gate 		sl.sl_imap = clmp;
25747c478bd9Sstevel@tonic-gate 		if (sym = SYMINTP(clmp)(&sl, dlmp, binfo))
25757c478bd9Sstevel@tonic-gate 			return (sym);
25767c478bd9Sstevel@tonic-gate 	}
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	/*
25797c478bd9Sstevel@tonic-gate 	 * If this lookup originates from a standard relocation, then traverse
25807c478bd9Sstevel@tonic-gate 	 * all link-map lists inspecting any object that is available to this
25817c478bd9Sstevel@tonic-gate 	 * caller.  Otherwise, traverse the link-map list associate with the
25827c478bd9Sstevel@tonic-gate 	 * caller.
25837c478bd9Sstevel@tonic-gate 	 */
25847c478bd9Sstevel@tonic-gate 	if (flags & LKUP_ALLCNTLIST) {
25857c478bd9Sstevel@tonic-gate 		Aliste	off;
25867c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate 		sym = 0;
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(LIST(clmp)->lm_lists, off, lmc)) {
25917c478bd9Sstevel@tonic-gate 			if ((sym = _lookup_sym(lmc->lc_head, &sl, dlmp,
25927c478bd9Sstevel@tonic-gate 			    binfo, off)) != 0)
25937c478bd9Sstevel@tonic-gate 				break;
25947c478bd9Sstevel@tonic-gate 		}
25957c478bd9Sstevel@tonic-gate 	} else
25967c478bd9Sstevel@tonic-gate 		sym = _lookup_sym(ilmp, &sl, dlmp, binfo, ALO_DATA);
25977c478bd9Sstevel@tonic-gate 
25987c478bd9Sstevel@tonic-gate 	/*
25997c478bd9Sstevel@tonic-gate 	 * To allow transitioning into a world of lazy loading dependencies see
26007c478bd9Sstevel@tonic-gate 	 * if this link map contains objects that have lazy dependencies still
26017c478bd9Sstevel@tonic-gate 	 * outstanding.  If so, and we haven't been able to locate a non-weak
26027c478bd9Sstevel@tonic-gate 	 * symbol reference, start bringing in any lazy dependencies to see if
26037c478bd9Sstevel@tonic-gate 	 * the reference can be satisfied.  Use of dlsym(RTLD_PROBE) sets the
26047c478bd9Sstevel@tonic-gate 	 * LKUP_NOFALBACK flag, and this flag disables this fall back.
26057c478bd9Sstevel@tonic-gate 	 */
26067c478bd9Sstevel@tonic-gate 	if ((sym == 0) && ((sl.sl_flags & LKUP_NOFALBACK) == 0)) {
26077c478bd9Sstevel@tonic-gate 		if ((lmp = ilmp) == 0)
26087c478bd9Sstevel@tonic-gate 			lmp = LIST(clmp)->lm_head;
26097c478bd9Sstevel@tonic-gate 		if ((flags & LKUP_WEAK) || (LIST(lmp)->lm_lazy == 0))
26107c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_syms_lazy_rescan(name));
26137c478bd9Sstevel@tonic-gate 
26147c478bd9Sstevel@tonic-gate 		/*
26157c478bd9Sstevel@tonic-gate 		 * If this request originated from a dlsym(RTLD_NEXT) then start
26167c478bd9Sstevel@tonic-gate 		 * looking for dependencies from the caller, otherwise use the
26177c478bd9Sstevel@tonic-gate 		 * initial link-map.
26187c478bd9Sstevel@tonic-gate 		 */
26197c478bd9Sstevel@tonic-gate 		if (flags & LKUP_NEXT)
26207c478bd9Sstevel@tonic-gate 			sym = _lazy_find_sym(clmp, &sl, dlmp, binfo);
26217c478bd9Sstevel@tonic-gate 		else {
26227c478bd9Sstevel@tonic-gate 			Aliste	off;
26237c478bd9Sstevel@tonic-gate 			Lm_cntl	*lmc;
26247c478bd9Sstevel@tonic-gate 
26257c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(LIST(clmp)->lm_lists, off, lmc)) {
26267c478bd9Sstevel@tonic-gate 				sl.sl_flags |= LKUP_NOFALBACK;
26277c478bd9Sstevel@tonic-gate 				if ((sym = _lazy_find_sym(lmc->lc_head, &sl,
26287c478bd9Sstevel@tonic-gate 				    dlmp, binfo)) != 0)
26297c478bd9Sstevel@tonic-gate 					break;
26307c478bd9Sstevel@tonic-gate 			}
26317c478bd9Sstevel@tonic-gate 		}
26327c478bd9Sstevel@tonic-gate 	}
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 	/*
26357c478bd9Sstevel@tonic-gate 	 * If the caller is restricted to a symbol search within its group,
26367c478bd9Sstevel@tonic-gate 	 * determine if it is necessary to follow a binding from outside of
26377c478bd9Sstevel@tonic-gate 	 * the group.
26387c478bd9Sstevel@tonic-gate 	 */
26397c478bd9Sstevel@tonic-gate 	if (sym && ((MODE(clmp) & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP)) {
26407c478bd9Sstevel@tonic-gate 		Sym *	isym;
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 		if ((isym = lookup_sym_interpose(slp, dlmp, binfo, LIST(*dlmp),
26437c478bd9Sstevel@tonic-gate 		    sym)) != 0)
26447c478bd9Sstevel@tonic-gate 			return (isym);
26457c478bd9Sstevel@tonic-gate 	}
26467c478bd9Sstevel@tonic-gate 	return (sym);
26477c478bd9Sstevel@tonic-gate }
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate /*
26507c478bd9Sstevel@tonic-gate  * Associate a binding descriptor with a caller and its dependency, or update
26517c478bd9Sstevel@tonic-gate  * an existing descriptor.
26527c478bd9Sstevel@tonic-gate  */
26537c478bd9Sstevel@tonic-gate int
26547c478bd9Sstevel@tonic-gate bind_one(Rt_map * clmp, Rt_map * dlmp, uint_t flags)
26557c478bd9Sstevel@tonic-gate {
26567c478bd9Sstevel@tonic-gate 	Bnd_desc **	bdpp, * bdp;
26577c478bd9Sstevel@tonic-gate 	Aliste		off;
26587c478bd9Sstevel@tonic-gate 	int		found = ALE_CREATE;
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	/*
26617c478bd9Sstevel@tonic-gate 	 * Determine whether a binding descriptor already exists between the
26627c478bd9Sstevel@tonic-gate 	 * two objects.
26637c478bd9Sstevel@tonic-gate 	 */
26647c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(DEPENDS(clmp), off, bdpp)) {
26657c478bd9Sstevel@tonic-gate 		bdp = *bdpp;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 		if (bdp->b_depend == dlmp) {
26687c478bd9Sstevel@tonic-gate 			found = ALE_EXISTS;
26697c478bd9Sstevel@tonic-gate 			break;
26707c478bd9Sstevel@tonic-gate 		}
26717c478bd9Sstevel@tonic-gate 	}
26727c478bd9Sstevel@tonic-gate 
26737c478bd9Sstevel@tonic-gate 	if (found == ALE_CREATE) {
26747c478bd9Sstevel@tonic-gate 		/*
26757c478bd9Sstevel@tonic-gate 		 * Create a new binding descriptor.
26767c478bd9Sstevel@tonic-gate 		 */
26777c478bd9Sstevel@tonic-gate 		if ((bdp = malloc(sizeof (Bnd_desc))) == 0)
26787c478bd9Sstevel@tonic-gate 			return (0);
26797c478bd9Sstevel@tonic-gate 
26807c478bd9Sstevel@tonic-gate 		bdp->b_caller = clmp;
26817c478bd9Sstevel@tonic-gate 		bdp->b_depend = dlmp;
26827c478bd9Sstevel@tonic-gate 		bdp->b_flags = 0;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 		/*
26857c478bd9Sstevel@tonic-gate 		 * Append the binding descriptor to the caller and the
26867c478bd9Sstevel@tonic-gate 		 * dependency.
26877c478bd9Sstevel@tonic-gate 		 */
26887c478bd9Sstevel@tonic-gate 		if (alist_append(&DEPENDS(clmp), &bdp,
26897c478bd9Sstevel@tonic-gate 		    sizeof (Bnd_desc *), AL_CNT_DEPENDS) == 0)
26907c478bd9Sstevel@tonic-gate 			return (0);
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 		if (alist_append(&CALLERS(dlmp), &bdp,
26937c478bd9Sstevel@tonic-gate 		    sizeof (Bnd_desc *), AL_CNT_CALLERS) == 0)
26947c478bd9Sstevel@tonic-gate 			return (0);
26957c478bd9Sstevel@tonic-gate 	}
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	if ((found == ALE_CREATE) || ((bdp->b_flags & flags) != flags)) {
26987c478bd9Sstevel@tonic-gate 		bdp->b_flags |= flags;
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 		if (flags & BND_REFER)
27017c478bd9Sstevel@tonic-gate 			FLAGS1(dlmp) |= FL1_RT_USED;
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_bind_entry(bdp));
27047c478bd9Sstevel@tonic-gate 	}
27057c478bd9Sstevel@tonic-gate 	return (found);
27067c478bd9Sstevel@tonic-gate }
27077c478bd9Sstevel@tonic-gate 
27087c478bd9Sstevel@tonic-gate /*
27097c478bd9Sstevel@tonic-gate  * Cleanup after relocation processing.
27107c478bd9Sstevel@tonic-gate  */
27117c478bd9Sstevel@tonic-gate int
27127c478bd9Sstevel@tonic-gate relocate_finish(Rt_map *lmp, Alist *bound, int textrel, int ret)
27137c478bd9Sstevel@tonic-gate {
27147c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_reloc_run(NAME(lmp), 0, ret, DBG_REL_FINISH));
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 	/*
27177c478bd9Sstevel@tonic-gate 	 * Establish bindings to all objects that have been bound to.
27187c478bd9Sstevel@tonic-gate 	 */
27197c478bd9Sstevel@tonic-gate 	if (bound) {
27207c478bd9Sstevel@tonic-gate 		Aliste	off;
27217c478bd9Sstevel@tonic-gate 		Rt_map	**lmpp;
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 		if (ret) {
27247c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(bound, off, lmpp)) {
27257c478bd9Sstevel@tonic-gate 				if (bind_one(lmp, *lmpp, BND_REFER) == 0) {
27267c478bd9Sstevel@tonic-gate 					ret = 0;
27277c478bd9Sstevel@tonic-gate 					break;
27287c478bd9Sstevel@tonic-gate 				}
27297c478bd9Sstevel@tonic-gate 			}
27307c478bd9Sstevel@tonic-gate 		}
27317c478bd9Sstevel@tonic-gate 		free(bound);
27327c478bd9Sstevel@tonic-gate 	}
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	/*
27357c478bd9Sstevel@tonic-gate 	 * If we write enabled the text segment to perform these relocations
27367c478bd9Sstevel@tonic-gate 	 * re-protect by disabling writes.
27377c478bd9Sstevel@tonic-gate 	 */
27387c478bd9Sstevel@tonic-gate 	if (textrel)
27397c478bd9Sstevel@tonic-gate 		(void) LM_SET_PROT(lmp)(lmp, 0);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	return (ret);
27427c478bd9Sstevel@tonic-gate }
2743