xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/elf.c (revision a953e2b1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2111a2bb38Srie 
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
247c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
257c478bd9Sstevel@tonic-gate  *
269a411307Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Object file dependent support for ELF objects.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate #include	"_synonyms.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	<stdio.h>
377c478bd9Sstevel@tonic-gate #include	<sys/procfs.h>
387c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
397c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
407c478bd9Sstevel@tonic-gate #include	<string.h>
417c478bd9Sstevel@tonic-gate #include	<limits.h>
427c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
435aefb655Srie #include	<debug.h>
445aefb655Srie #include	<conv.h>
457c478bd9Sstevel@tonic-gate #include	"_rtld.h"
467c478bd9Sstevel@tonic-gate #include	"_audit.h"
477c478bd9Sstevel@tonic-gate #include	"_elf.h"
487c478bd9Sstevel@tonic-gate #include	"msg.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Default and secure dependency search paths.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate static Pnode		elf_dflt_dirs[] = {
547c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
557c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
567c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIB_64),		0,	MSG_PTH_LIB_64_SIZE,
577c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIB_64),		0,	MSG_PTH_USRLIB_64_SIZE,
607c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0, 0 }
617c478bd9Sstevel@tonic-gate #else
627c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
637c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIB),		0,	MSG_PTH_LIB_SIZE,
647c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
657c478bd9Sstevel@tonic-gate #endif
667c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIB),		0,	MSG_PTH_USRLIB_SIZE,
677c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0, 0 }
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate };
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate static Pnode		elf_secure_dirs[] = {
727c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
737c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
747c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIBSE_64),		0,	MSG_PTH_LIBSE_64_SIZE,
757c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIBSE_64),	0,
787c478bd9Sstevel@tonic-gate 		MSG_PTH_USRLIBSE_64_SIZE,
797c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0, 0 }
807c478bd9Sstevel@tonic-gate #else
817c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
827c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIBSE),		0,	MSG_PTH_LIBSE_SIZE,
837c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIBSE),		0,	MSG_PTH_USRLIBSE_SIZE,
867c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0, 0 }
877c478bd9Sstevel@tonic-gate #endif
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Defines for local functions.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate static Pnode	*elf_fix_name(const char *, Rt_map *, uint_t);
947c478bd9Sstevel@tonic-gate static int	elf_are_u(Rej_desc *);
957c478bd9Sstevel@tonic-gate static void	elf_dladdr(ulong_t, Rt_map *, Dl_info *, void **, int);
967c478bd9Sstevel@tonic-gate static ulong_t	elf_entry_pt(void);
977c478bd9Sstevel@tonic-gate static char	*elf_get_so(const char *, const char *);
987c478bd9Sstevel@tonic-gate static Rt_map	*elf_map_so(Lm_list *, Aliste, const char *, const char *, int);
997c478bd9Sstevel@tonic-gate static int	elf_needed(Lm_list *, Aliste, Rt_map *);
1007c478bd9Sstevel@tonic-gate static void	elf_unmap_so(Rt_map *);
1017c478bd9Sstevel@tonic-gate static int	elf_verify_vers(const char *, Rt_map *, Rt_map *);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  * Functions and data accessed through indirect pointers.
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate Fct elf_fct = {
1077c478bd9Sstevel@tonic-gate 	elf_are_u,
1087c478bd9Sstevel@tonic-gate 	elf_entry_pt,
1097c478bd9Sstevel@tonic-gate 	elf_map_so,
1107c478bd9Sstevel@tonic-gate 	elf_unmap_so,
1117c478bd9Sstevel@tonic-gate 	elf_needed,
1127c478bd9Sstevel@tonic-gate 	lookup_sym,
1137c478bd9Sstevel@tonic-gate 	elf_reloc,
1147c478bd9Sstevel@tonic-gate 	elf_dflt_dirs,
1157c478bd9Sstevel@tonic-gate 	elf_secure_dirs,
1167c478bd9Sstevel@tonic-gate 	elf_fix_name,
1177c478bd9Sstevel@tonic-gate 	elf_get_so,
1187c478bd9Sstevel@tonic-gate 	elf_dladdr,
1197c478bd9Sstevel@tonic-gate 	dlsym_handle,
1207c478bd9Sstevel@tonic-gate 	elf_verify_vers,
1217c478bd9Sstevel@tonic-gate 	elf_set_prot
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate  * Redefine NEEDED name if necessary.
1277c478bd9Sstevel@tonic-gate  */
1287c478bd9Sstevel@tonic-gate static Pnode *
1297c478bd9Sstevel@tonic-gate elf_fix_name(const char *name, Rt_map *clmp, uint_t orig)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * For ABI compliance, if we are asked for ld.so.1, then really give
1337c478bd9Sstevel@tonic-gate 	 * them libsys.so.1 (the SONAME of libsys.so.1 is ld.so.1).
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	if (((*name == '/') &&
136*a953e2b1Srie 	/* BEGIN CSTYLED */
1377c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
1387c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD_64)) == 0)) ||
1397c478bd9Sstevel@tonic-gate #else
1407c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD)) == 0)) ||
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_FIL_RTLD)) == 0)) {
143*a953e2b1Srie 		/* END CSTYLED */
1447c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
1457c478bd9Sstevel@tonic-gate 
1465aefb655Srie 		DBG_CALL(Dbg_file_fixname(LIST(clmp), name,
1475aefb655Srie 		    MSG_ORIG(MSG_PTH_LIBSYS)));
1487c478bd9Sstevel@tonic-gate 		if (((pnp = calloc(sizeof (Pnode), 1)) == 0) ||
1497c478bd9Sstevel@tonic-gate 		    ((pnp->p_name = strdup(MSG_ORIG(MSG_PTH_LIBSYS))) == 0)) {
1507c478bd9Sstevel@tonic-gate 			if (pnp)
1517c478bd9Sstevel@tonic-gate 				free(pnp);
1527c478bd9Sstevel@tonic-gate 			return (0);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 		pnp->p_len = MSG_PTH_LIBSYS_SIZE;
1557c478bd9Sstevel@tonic-gate 		pnp->p_orig = (orig & PN_SER_MASK);
1567c478bd9Sstevel@tonic-gate 		return (pnp);
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	return (expand_paths(clmp, name, orig, 0));
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Determine if we have been given an ELF file and if so determine if the file
1647c478bd9Sstevel@tonic-gate  * is compatible.  Returns 1 if true, else 0 and sets the reject descriptor
1657c478bd9Sstevel@tonic-gate  * with associated error information.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate static int
1687c478bd9Sstevel@tonic-gate elf_are_u(Rej_desc *rej)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	Ehdr	*ehdr;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Determine if we're an elf file.  If not simply return, we don't set
1747c478bd9Sstevel@tonic-gate 	 * any rejection information as this test allows use to scroll through
1757c478bd9Sstevel@tonic-gate 	 * the objects we support (ELF, AOUT).
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	if (fmap->fm_fsize < sizeof (Ehdr) ||
1787c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG0] != ELFMAG0 ||
1797c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG1] != ELFMAG1 ||
1807c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG2] != ELFMAG2 ||
1817c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG3] != ELFMAG3) {
1827c478bd9Sstevel@tonic-gate 		return (0);
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	/*
1867c478bd9Sstevel@tonic-gate 	 * Check class and encoding.
1877c478bd9Sstevel@tonic-gate 	 */
1887c478bd9Sstevel@tonic-gate 	/* LINTED */
1897c478bd9Sstevel@tonic-gate 	ehdr = (Ehdr *)fmap->fm_maddr;
1907c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_CLASS] != M_CLASS) {
1917c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_CLASS;
1927c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_CLASS];
1937c478bd9Sstevel@tonic-gate 		return (0);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_DATA] != M_DATA) {
1967c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
1977c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
1987c478bd9Sstevel@tonic-gate 		return (0);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 	if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC) &&
2017c478bd9Sstevel@tonic-gate 	    (ehdr->e_type != ET_DYN)) {
2027c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_TYPE;
2037c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_type;
2047c478bd9Sstevel@tonic-gate 		return (0);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * Verify machine specific flags, and hardware capability requirements.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 	if ((elf_mach_flags_check(rej, ehdr) == 0) ||
2117c478bd9Sstevel@tonic-gate 	    ((rtld_flags2 & RT_FL2_HWCAP) && (hwcap_check(rej, ehdr) == 0)))
2127c478bd9Sstevel@tonic-gate 		return (0);
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Verify ELF version.  ??? is this too restrictive ???
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	if (ehdr->e_version > EV_CURRENT) {
2187c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
2197c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
2207c478bd9Sstevel@tonic-gate 		return (0);
2217c478bd9Sstevel@tonic-gate 	}
2227c478bd9Sstevel@tonic-gate 	return (1);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate  * The runtime linker employs lazy loading to provide the libraries needed for
2277c478bd9Sstevel@tonic-gate  * debugging, preloading .o's and dldump().  As these are seldom used, the
2287c478bd9Sstevel@tonic-gate  * standard startup of ld.so.1 doesn't initialize all the information necessary
2297c478bd9Sstevel@tonic-gate  * to perform plt relocation on ld.so.1's link-map.  The first time lazy loading
2307c478bd9Sstevel@tonic-gate  * is called we get here to perform these initializations:
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  *  o	elf_needed() is called to set up the DYNINFO() indexes for each lazy
2337c478bd9Sstevel@tonic-gate  *	dependency.  Typically, for all other objects, this is called during
2347c478bd9Sstevel@tonic-gate  *	analyze_so(), but as ld.so.1 is set-contained we skip this processing.
2357c478bd9Sstevel@tonic-gate  *
2367c478bd9Sstevel@tonic-gate  *  o	For intel, ld.so.1's JMPSLOT relocations need relative updates. These
2377c478bd9Sstevel@tonic-gate  *	are by default skipped thus delaying all relative relocation processing
2387c478bd9Sstevel@tonic-gate  * 	on every invocation of ld.so.1.
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate int
2417c478bd9Sstevel@tonic-gate elf_rtld_load()
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	Lm_list	*lml = &lml_rtld;
2447c478bd9Sstevel@tonic-gate 	Rt_map	*lmp = lml->lm_head;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_PLTREL)
2477c478bd9Sstevel@tonic-gate 		return (1);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * As we need to refer to the DYNINFO() information, insure that it has
2517c478bd9Sstevel@tonic-gate 	 * been initialized.
2527c478bd9Sstevel@tonic-gate 	 */
2537c478bd9Sstevel@tonic-gate 	if (elf_needed(lml, ALO_DATA, lmp) == 0)
2547c478bd9Sstevel@tonic-gate 		return (0);
2557c478bd9Sstevel@tonic-gate 
25602ca3e02Srie #if	defined(__i386)
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * This is a kludge to give ld.so.1 a performance benefit on i386.
2597c478bd9Sstevel@tonic-gate 	 * It's based around two factors.
2607c478bd9Sstevel@tonic-gate 	 *
2617c478bd9Sstevel@tonic-gate 	 *  o	JMPSLOT relocations (PLT's) actually need a relative relocation
2627c478bd9Sstevel@tonic-gate 	 *	applied to the GOT entry so that they can find PLT0.
2637c478bd9Sstevel@tonic-gate 	 *
2647c478bd9Sstevel@tonic-gate 	 *  o	ld.so.1 does not exercise *any* PLT's before it has made a call
2657c478bd9Sstevel@tonic-gate 	 *	to elf_lazy_load().  This is because all dynamic dependencies
2667c478bd9Sstevel@tonic-gate 	 * 	are recorded as lazy dependencies.
2677c478bd9Sstevel@tonic-gate 	 */
2687c478bd9Sstevel@tonic-gate 	(void) elf_reloc_relacount((ulong_t)JMPREL(lmp),
2697c478bd9Sstevel@tonic-gate 	    (ulong_t)(PLTRELSZ(lmp) / RELENT(lmp)), (ulong_t)RELENT(lmp),
2707c478bd9Sstevel@tonic-gate 	    (ulong_t)ADDR(lmp));
2717c478bd9Sstevel@tonic-gate #endif
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	lml->lm_flags |= LML_FLG_PLTREL;
2747c478bd9Sstevel@tonic-gate 	return (1);
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Lazy load an object.
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate Rt_map *
2817c478bd9Sstevel@tonic-gate elf_lazy_load(Rt_map *clmp, uint_t ndx, const char *sym)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp, *hlmp;
2847c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(clmp)[ndx];
2857c478bd9Sstevel@tonic-gate 	uint_t		flags = 0;
2867c478bd9Sstevel@tonic-gate 	Pnode		*pnp;
2877c478bd9Sstevel@tonic-gate 	const char	*name;
2887c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
2897c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc;
2907c478bd9Sstevel@tonic-gate 	Aliste		lmco;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/*
2937c478bd9Sstevel@tonic-gate 	 * If this dependency has already been processed, we're done.
2947c478bd9Sstevel@tonic-gate 	 */
2957c478bd9Sstevel@tonic-gate 	if (((nlmp = (Rt_map *)dip->di_info) != 0) ||
2967c478bd9Sstevel@tonic-gate 	    (dip->di_flags & FLG_DI_PROCESSD))
2977c478bd9Sstevel@tonic-gate 		return (nlmp);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * Determine the initial dependency name, and indicate that this
3017c478bd9Sstevel@tonic-gate 	 * dependencies processing has initiated.
3027c478bd9Sstevel@tonic-gate 	 */
3037c478bd9Sstevel@tonic-gate 	name = STRTAB(clmp) + DYN(clmp)[ndx].d_un.d_val;
3045aefb655Srie 	DBG_CALL(Dbg_file_lazyload(clmp, name, sym));
3057c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_ENABLE)
3067c478bd9Sstevel@tonic-gate 		dip->di_flags |= FLG_DI_PROCESSD;
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	if (dip->di_flags & FLG_DI_GROUP)
3097c478bd9Sstevel@tonic-gate 		flags |= (FLG_RT_SETGROUP | FLG_RT_HANDLE);
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	/*
3127c478bd9Sstevel@tonic-gate 	 * Expand the requested name if necessary.
3137c478bd9Sstevel@tonic-gate 	 */
3147c478bd9Sstevel@tonic-gate 	if ((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0)
3157c478bd9Sstevel@tonic-gate 		return (0);
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/*
3187c478bd9Sstevel@tonic-gate 	 * Provided the object on the head of the link-map has completed its
3197c478bd9Sstevel@tonic-gate 	 * relocation, create a new link-map control list for this request.
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	hlmp = lml->lm_head;
3227c478bd9Sstevel@tonic-gate 	if (FLAGS(hlmp) & FLG_RT_RELOCED) {
3237c478bd9Sstevel@tonic-gate 		if ((lmc = alist_append(&(lml->lm_lists), 0, sizeof (Lm_cntl),
3247c478bd9Sstevel@tonic-gate 		    AL_CNT_LMLISTS)) == 0) {
3257c478bd9Sstevel@tonic-gate 			remove_pnode(pnp);
3267c478bd9Sstevel@tonic-gate 			return (0);
3277c478bd9Sstevel@tonic-gate 		}
3287c478bd9Sstevel@tonic-gate 		lmco = (Aliste)((char *)lmc - (char *)lml->lm_lists);
3297c478bd9Sstevel@tonic-gate 	} else {
3307c478bd9Sstevel@tonic-gate 		lmc = 0;
3317c478bd9Sstevel@tonic-gate 		lmco = ALO_DATA;
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/*
3357c478bd9Sstevel@tonic-gate 	 * Load the associated object.
3367c478bd9Sstevel@tonic-gate 	 */
3377c478bd9Sstevel@tonic-gate 	dip->di_info = nlmp =
3387c478bd9Sstevel@tonic-gate 	    load_one(lml, lmco, pnp, clmp, MODE(clmp), flags, 0);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/*
3417c478bd9Sstevel@tonic-gate 	 * Remove any expanded pathname infrastructure.  Reduce the pending lazy
3427c478bd9Sstevel@tonic-gate 	 * dependency count of the caller, together with the link-map lists
3437c478bd9Sstevel@tonic-gate 	 * count of objects that still have lazy dependencies pending.
3447c478bd9Sstevel@tonic-gate 	 */
3457c478bd9Sstevel@tonic-gate 	remove_pnode(pnp);
3467c478bd9Sstevel@tonic-gate 	if (--LAZY(clmp) == 0)
3477c478bd9Sstevel@tonic-gate 		LIST(clmp)->lm_lazy--;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	/*
35002ca3e02Srie 	 * Finish processing the objects associated with this request, and
35102ca3e02Srie 	 * create an association between the caller and this dependency.
3527c478bd9Sstevel@tonic-gate 	 */
35302ca3e02Srie 	if (nlmp && (((analyze_lmc(lml, lmco, nlmp) == 0)) ||
35402ca3e02Srie 	    (relocate_lmc(lml, lmco, clmp, nlmp) == 0) ||
35502ca3e02Srie 	    (bind_one(clmp, nlmp, BND_NEEDED) == 0)))
3567c478bd9Sstevel@tonic-gate 		dip->di_info = nlmp = 0;
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
35902ca3e02Srie 	 * If this lazyload has failed, and we've created a new link-map
36002ca3e02Srie 	 * control list to which this request has added objects, then remove
36102ca3e02Srie 	 * all the objects that have been associated to this request.
3627c478bd9Sstevel@tonic-gate 	 */
36302ca3e02Srie 	if ((nlmp == 0) && lmc && lmc->lc_head)
36402ca3e02Srie 		remove_lmc(lml, clmp, lmc, lmco, name);
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
36702ca3e02Srie 	 * Finally, remove any link-map control list that was created.
3687c478bd9Sstevel@tonic-gate 	 */
36902ca3e02Srie 	if (lmc)
3707c478bd9Sstevel@tonic-gate 		remove_cntl(lml, lmco);
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	return (nlmp);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * Return the entry point of the ELF executable.
3777c478bd9Sstevel@tonic-gate  */
3787c478bd9Sstevel@tonic-gate static ulong_t
3797c478bd9Sstevel@tonic-gate elf_entry_pt(void)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	return (ENTRY(lml_main.lm_head));
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * Unmap a given ELF shared object from the address space.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static void
3887c478bd9Sstevel@tonic-gate elf_unmap_so(Rt_map *lmp)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	caddr_t	addr;
3917c478bd9Sstevel@tonic-gate 	size_t	size;
3927c478bd9Sstevel@tonic-gate 	Mmap	*mmaps;
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/*
3957c478bd9Sstevel@tonic-gate 	 * If this link map represents a relocatable object concatenation, then
3967c478bd9Sstevel@tonic-gate 	 * the image was simply generated in allocated memory.  Free the memory.
3977c478bd9Sstevel@tonic-gate 	 *
3987c478bd9Sstevel@tonic-gate 	 * Note: the memory was originally allocated in the libelf:_elf_outmap
3997c478bd9Sstevel@tonic-gate 	 * routine and would normally have been free'd in elf_outsync(), but
4007c478bd9Sstevel@tonic-gate 	 * because we 'interpose' on that routine the memory  wasn't free'd at
4017c478bd9Sstevel@tonic-gate 	 * that time.
4027c478bd9Sstevel@tonic-gate 	 */
4037c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_IMGALLOC) {
4047c478bd9Sstevel@tonic-gate 		free((void *)ADDR(lmp));
4057c478bd9Sstevel@tonic-gate 		return;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	/*
4097c478bd9Sstevel@tonic-gate 	 * If padding was enabled via rtld_db, then we have at least one page
4107c478bd9Sstevel@tonic-gate 	 * in front of the image - and possibly a trailing page.
4117c478bd9Sstevel@tonic-gate 	 * Unmap the front page first:
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	if (PADSTART(lmp) != ADDR(lmp)) {
4147c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PTRUNC(PADSTART(lmp));
4157c478bd9Sstevel@tonic-gate 		size = ADDR(lmp) - (ulong_t)addr;
4167c478bd9Sstevel@tonic-gate 		(void) munmap(addr, size);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	/*
4207c478bd9Sstevel@tonic-gate 	 * Unmap any trailing padding.
4217c478bd9Sstevel@tonic-gate 	 */
4227c478bd9Sstevel@tonic-gate 	if (M_PROUND((PADSTART(lmp) + PADIMLEN(lmp))) >
4237c478bd9Sstevel@tonic-gate 	    M_PROUND(ADDR(lmp) + MSIZE(lmp))) {
4247c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PROUND(ADDR(lmp) + MSIZE(lmp));
4257c478bd9Sstevel@tonic-gate 		size = M_PROUND(PADSTART(lmp) + PADIMLEN(lmp)) - (ulong_t)addr;
4267c478bd9Sstevel@tonic-gate 		(void) munmap(addr, size);
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * Unmmap all mapped segments.
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++)
4337c478bd9Sstevel@tonic-gate 		(void) munmap(mmaps->m_vaddr, mmaps->m_msize);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * Determine if a dependency requires a particular version and if so verify
4387c478bd9Sstevel@tonic-gate  * that the version exists in the dependency.
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate static int
4417c478bd9Sstevel@tonic-gate elf_verify_vers(const char *name, Rt_map *clmp, Rt_map *nlmp)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	Verneed		*vnd = VERNEED(clmp);
4447c478bd9Sstevel@tonic-gate 	int		_num, num = VERNEEDNUM(clmp);
4457c478bd9Sstevel@tonic-gate 	char		*cstrs = (char *)STRTAB(clmp);
4467c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	/*
4497c478bd9Sstevel@tonic-gate 	 * Traverse the callers version needed information and determine if any
4507c478bd9Sstevel@tonic-gate 	 * specific versions are required from the dependency.
4517c478bd9Sstevel@tonic-gate 	 */
4525aefb655Srie 	DBG_CALL(Dbg_ver_need_title(LIST(clmp), NAME(clmp)));
4537c478bd9Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
4547c478bd9Sstevel@tonic-gate 	    vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) {
4557c478bd9Sstevel@tonic-gate 		Half		cnt = vnd->vn_cnt;
4567c478bd9Sstevel@tonic-gate 		Vernaux		*vnap;
4577c478bd9Sstevel@tonic-gate 		char		*nstrs, *need;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 		/*
4607c478bd9Sstevel@tonic-gate 		 * Determine if a needed entry matches this dependency.
4617c478bd9Sstevel@tonic-gate 		 */
4627c478bd9Sstevel@tonic-gate 		need = (char *)(cstrs + vnd->vn_file);
4637c478bd9Sstevel@tonic-gate 		if (strcmp(name, need) != 0)
4647c478bd9Sstevel@tonic-gate 			continue;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
4677c478bd9Sstevel@tonic-gate 		    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
4687c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_VER_FIND), name);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 		/*
4717c478bd9Sstevel@tonic-gate 		 * Validate that each version required actually exists in the
4727c478bd9Sstevel@tonic-gate 		 * dependency.
4737c478bd9Sstevel@tonic-gate 		 */
4747c478bd9Sstevel@tonic-gate 		nstrs = (char *)STRTAB(nlmp);
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		for (vnap = (Vernaux *)((Xword)vnd + vnd->vn_aux); cnt;
4777c478bd9Sstevel@tonic-gate 		    cnt--, vnap = (Vernaux *)((Xword)vnap + vnap->vna_next)) {
4787c478bd9Sstevel@tonic-gate 			char		*version, *define;
4797c478bd9Sstevel@tonic-gate 			Verdef		*vdf = VERDEF(nlmp);
4807c478bd9Sstevel@tonic-gate 			ulong_t		_num, num = VERDEFNUM(nlmp);
4817c478bd9Sstevel@tonic-gate 			int		found = 0;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 			version = (char *)(cstrs + vnap->vna_name);
4845aefb655Srie 			DBG_CALL(Dbg_ver_need_entry(lml, 0, need, version));
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 			for (_num = 1; _num <= num; _num++,
4877c478bd9Sstevel@tonic-gate 			    vdf = (Verdef *)((Xword)vdf + vdf->vd_next)) {
4887c478bd9Sstevel@tonic-gate 				Verdaux		*vdap;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 				if (vnap->vna_hash != vdf->vd_hash)
4917c478bd9Sstevel@tonic-gate 					continue;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 				vdap = (Verdaux *)((Xword)vdf + vdf->vd_aux);
4947c478bd9Sstevel@tonic-gate 				define = (char *)(nstrs + vdap->vda_name);
4957c478bd9Sstevel@tonic-gate 				if (strcmp(version, define) != 0)
4967c478bd9Sstevel@tonic-gate 					continue;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 				found++;
4997c478bd9Sstevel@tonic-gate 				break;
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 			/*
5037c478bd9Sstevel@tonic-gate 			 * If we're being traced print out any matched version
5047c478bd9Sstevel@tonic-gate 			 * when the verbose (-v) option is in effect.  Always
5057c478bd9Sstevel@tonic-gate 			 * print any unmatched versions.
5067c478bd9Sstevel@tonic-gate 			 */
5077c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
508*a953e2b1Srie 				/* BEGIN CSTYLED */
5097c478bd9Sstevel@tonic-gate 				if (found) {
5107c478bd9Sstevel@tonic-gate 				    if (!(lml->lm_flags & LML_FLG_TRC_VERBOSE))
5117c478bd9Sstevel@tonic-gate 					continue;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_VER_FOUND),
5147c478bd9Sstevel@tonic-gate 					need, version, NAME(nlmp));
5157c478bd9Sstevel@tonic-gate 				} else {
5167c478bd9Sstevel@tonic-gate 				    if (rtld_flags & RT_FL_SILENCERR)
5177c478bd9Sstevel@tonic-gate 					continue;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_VER_NFOUND),
5207c478bd9Sstevel@tonic-gate 					need, version);
5217c478bd9Sstevel@tonic-gate 				}
522*a953e2b1Srie 				/* END CSTYLED */
5237c478bd9Sstevel@tonic-gate 				continue;
5247c478bd9Sstevel@tonic-gate 			}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 			/*
5277c478bd9Sstevel@tonic-gate 			 * If the version hasn't been found then this is a
5287c478bd9Sstevel@tonic-gate 			 * candidate for a fatal error condition.  Weak
5297c478bd9Sstevel@tonic-gate 			 * version definition requirements are silently
5307c478bd9Sstevel@tonic-gate 			 * ignored.  Also, if the image inspected for a version
5317c478bd9Sstevel@tonic-gate 			 * definition has no versioning recorded at all then
5327c478bd9Sstevel@tonic-gate 			 * silently ignore this (this provides better backward
5337c478bd9Sstevel@tonic-gate 			 * compatibility to old images created prior to
5347c478bd9Sstevel@tonic-gate 			 * versioning being available).  Both of these skipped
5357c478bd9Sstevel@tonic-gate 			 * diagnostics are available under tracing (see above).
5367c478bd9Sstevel@tonic-gate 			 */
5377c478bd9Sstevel@tonic-gate 			if ((found == 0) && (num != 0) &&
5387c478bd9Sstevel@tonic-gate 			    (!(vnap->vna_flags & VER_FLG_WEAK))) {
5395aefb655Srie 				eprintf(lml, ERR_FATAL,
5405aefb655Srie 				    MSG_INTL(MSG_VER_NFOUND), need, version,
5415aefb655Srie 				    NAME(clmp));
5427c478bd9Sstevel@tonic-gate 				return (0);
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 	}
5465aefb655Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
5477c478bd9Sstevel@tonic-gate 	return (1);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate  * Search through the dynamic section for DT_NEEDED entries and perform one
5527c478bd9Sstevel@tonic-gate  * of two functions.  If only the first argument is specified then load the
5537c478bd9Sstevel@tonic-gate  * defined shared object, otherwise add the link map representing the defined
5547c478bd9Sstevel@tonic-gate  * link map the the dlopen list.
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate static int
5577c478bd9Sstevel@tonic-gate elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	Dyn		*dyn;
5607c478bd9Sstevel@tonic-gate 	ulong_t		ndx = 0;
5617c478bd9Sstevel@tonic-gate 	uint_t		lazy = 0, flags = 0;
5627c478bd9Sstevel@tonic-gate 	Word		lmflags = lml->lm_flags;
5637c478bd9Sstevel@tonic-gate 	Word		lmtflags = lml->lm_tflags;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	/*
5667c478bd9Sstevel@tonic-gate 	 * Process each shared object on needed list.
5677c478bd9Sstevel@tonic-gate 	 */
5687c478bd9Sstevel@tonic-gate 	if (DYN(clmp) == 0)
5697c478bd9Sstevel@tonic-gate 		return (1);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	for (dyn = (Dyn *)DYN(clmp); dyn->d_tag != DT_NULL; dyn++, ndx++) {
5727c478bd9Sstevel@tonic-gate 		Dyninfo	*dip = &DYNINFO(clmp)[ndx];
5737c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
5747c478bd9Sstevel@tonic-gate 		char	*name;
5757c478bd9Sstevel@tonic-gate 		int	silent = 0;
5767c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 		switch (dyn->d_tag) {
5797c478bd9Sstevel@tonic-gate 		case DT_POSFLAG_1:
5807c478bd9Sstevel@tonic-gate 			if ((dyn->d_un.d_val & DF_P1_LAZYLOAD) &&
5817c478bd9Sstevel@tonic-gate 			    !(lmtflags & LML_TFLG_NOLAZYLD))
5827c478bd9Sstevel@tonic-gate 				lazy = 1;
5837c478bd9Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_P1_GROUPPERM)
5847c478bd9Sstevel@tonic-gate 				flags = (FLG_RT_SETGROUP | FLG_RT_HANDLE);
5857c478bd9Sstevel@tonic-gate 			continue;
5867c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
5877c478bd9Sstevel@tonic-gate 		case DT_USED:
5887c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_NEEDED;
5897c478bd9Sstevel@tonic-gate 			if (flags)
5907c478bd9Sstevel@tonic-gate 				dip->di_flags |= FLG_DI_GROUP;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 			name = (char *)STRTAB(clmp) + dyn->d_un.d_val;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			/*
5957c478bd9Sstevel@tonic-gate 			 * NOTE, libc.so.1 can't be lazy loaded.  Although a
5967c478bd9Sstevel@tonic-gate 			 * lazy position flag won't be produced when a RTLDINFO
5977c478bd9Sstevel@tonic-gate 			 * .dynamic entry is found (introduced with the UPM in
5987c478bd9Sstevel@tonic-gate 			 * Solaris 10), it was possible to mark libc for lazy
5997c478bd9Sstevel@tonic-gate 			 * loading on previous releases.  To reduce the overhead
6007c478bd9Sstevel@tonic-gate 			 * of testing for this occurrence, only carry out this
6017c478bd9Sstevel@tonic-gate 			 * check for the first object on the link-map list
6027c478bd9Sstevel@tonic-gate 			 * (there aren't many applications built without libc).
6037c478bd9Sstevel@tonic-gate 			 */
6047c478bd9Sstevel@tonic-gate 			if (lazy && (lml->lm_head == clmp) &&
6057c478bd9Sstevel@tonic-gate 			    (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0))
6067c478bd9Sstevel@tonic-gate 				lazy = 0;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 			/*
6097c478bd9Sstevel@tonic-gate 			 * Don't bring in lazy loaded objects yet unless we've
6107c478bd9Sstevel@tonic-gate 			 * been asked to attempt to load all available objects
6117c478bd9Sstevel@tonic-gate 			 * (crle(1) sets LD_FLAGS=loadavail).  Even under
6127c478bd9Sstevel@tonic-gate 			 * RTLD_NOW we don't process this - RTLD_NOW will cause
6137c478bd9Sstevel@tonic-gate 			 * relocation processing which in turn might trigger
6147c478bd9Sstevel@tonic-gate 			 * lazy loading, but its possible that the object has a
6157c478bd9Sstevel@tonic-gate 			 * lazy loaded file with no bindings (i.e., it should
6167c478bd9Sstevel@tonic-gate 			 * never have been a dependency in the first place).
6177c478bd9Sstevel@tonic-gate 			 */
6187c478bd9Sstevel@tonic-gate 			if (lazy) {
6197c478bd9Sstevel@tonic-gate 				if ((lmflags & LML_FLG_LOADAVAIL) == 0) {
6207c478bd9Sstevel@tonic-gate 					LAZY(clmp)++;
6217c478bd9Sstevel@tonic-gate 					lazy = flags = 0;
6227c478bd9Sstevel@tonic-gate 					continue;
6237c478bd9Sstevel@tonic-gate 				}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 				/*
6267c478bd9Sstevel@tonic-gate 				 * Silence any error messages - see description
6277c478bd9Sstevel@tonic-gate 				 * under elf_lookup_filtee().
6287c478bd9Sstevel@tonic-gate 				 */
6297c478bd9Sstevel@tonic-gate 				if ((rtld_flags & RT_FL_SILENCERR) == 0) {
6307c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_SILENCERR;
6317c478bd9Sstevel@tonic-gate 					silent = 1;
6327c478bd9Sstevel@tonic-gate 				}
6337c478bd9Sstevel@tonic-gate 			}
6347c478bd9Sstevel@tonic-gate 			break;
6357c478bd9Sstevel@tonic-gate 		case DT_AUXILIARY:
6367c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_AUXFLTR;
6377c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6387c478bd9Sstevel@tonic-gate 			continue;
6397c478bd9Sstevel@tonic-gate 		case DT_SUNW_AUXILIARY:
6407c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR);
6417c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6427c478bd9Sstevel@tonic-gate 			continue;
6437c478bd9Sstevel@tonic-gate 		case DT_FILTER:
6447c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_STDFLTR;
6457c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6467c478bd9Sstevel@tonic-gate 			continue;
6477c478bd9Sstevel@tonic-gate 		case DT_SUNW_FILTER:
6487c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR);
6497c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6507c478bd9Sstevel@tonic-gate 			continue;
6517c478bd9Sstevel@tonic-gate 		default:
6527c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6537c478bd9Sstevel@tonic-gate 			continue;
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 
6565aefb655Srie 		DBG_CALL(Dbg_file_needed(clmp, name));
6577c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & LML_FLG_TRC_ENABLE)
6587c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_PROCESSD;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 		/*
6617c478bd9Sstevel@tonic-gate 		 * Establish the objects name, load it and establish a binding
6627c478bd9Sstevel@tonic-gate 		 * with the caller.
6637c478bd9Sstevel@tonic-gate 		 */
6647c478bd9Sstevel@tonic-gate 		if (((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0) ||
6657c478bd9Sstevel@tonic-gate 		    ((nlmp = load_one(lml, lmco, pnp, clmp, MODE(clmp),
6667c478bd9Sstevel@tonic-gate 		    flags, 0)) == 0) || (bind_one(clmp, nlmp, BND_NEEDED) == 0))
6677c478bd9Sstevel@tonic-gate 			nlmp = 0;
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		/*
6707c478bd9Sstevel@tonic-gate 		 * Clean up any infrastructure, including the removal of the
6717c478bd9Sstevel@tonic-gate 		 * error suppression state, if it had been previously set in
6727c478bd9Sstevel@tonic-gate 		 * this routine.
6737c478bd9Sstevel@tonic-gate 		 */
6747c478bd9Sstevel@tonic-gate 		if (pnp)
6757c478bd9Sstevel@tonic-gate 			remove_pnode(pnp);
6767c478bd9Sstevel@tonic-gate 		if (silent)
6777c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_SILENCERR;
6787c478bd9Sstevel@tonic-gate 		lazy = flags = 0;
6797c478bd9Sstevel@tonic-gate 		if ((dip->di_info = (void *)nlmp) == 0) {
6807c478bd9Sstevel@tonic-gate 			/*
6817c478bd9Sstevel@tonic-gate 			 * If the object could not be mapped, continue if error
6827c478bd9Sstevel@tonic-gate 			 * suppression is established or we're here with ldd(1).
6837c478bd9Sstevel@tonic-gate 			 */
6847c478bd9Sstevel@tonic-gate 			if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags &
6857c478bd9Sstevel@tonic-gate 			    (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE)))
6867c478bd9Sstevel@tonic-gate 				continue;
6877c478bd9Sstevel@tonic-gate 			else
6887c478bd9Sstevel@tonic-gate 				return (0);
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 	}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	if (LAZY(clmp))
6937c478bd9Sstevel@tonic-gate 		lml->lm_lazy++;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	return (1);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate static int
6995aefb655Srie elf_map_check(Lm_list *lml, const char *name, caddr_t vaddr, Off size)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	prmap_t		*maps, *_maps;
7027c478bd9Sstevel@tonic-gate 	int		pfd, num, _num;
7037c478bd9Sstevel@tonic-gate 	caddr_t		eaddr = vaddr + size;
7047c478bd9Sstevel@tonic-gate 	int		err;
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/*
7077c478bd9Sstevel@tonic-gate 	 * If memory reservations have been established for alternative objects
7087c478bd9Sstevel@tonic-gate 	 * determine if this object falls within the reservation, if it does no
7097c478bd9Sstevel@tonic-gate 	 * further checking is required.
7107c478bd9Sstevel@tonic-gate 	 */
7117c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_MEMRESV) {
7127c478bd9Sstevel@tonic-gate 		Rtc_head	*head = (Rtc_head *)config->c_bgn;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		if ((vaddr >= (caddr_t)(uintptr_t)head->ch_resbgn) &&
7157c478bd9Sstevel@tonic-gate 		    (eaddr <= (caddr_t)(uintptr_t)head->ch_resend))
7167c478bd9Sstevel@tonic-gate 			return (0);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/*
7207c478bd9Sstevel@tonic-gate 	 * Determine the mappings presently in use by this process.
7217c478bd9Sstevel@tonic-gate 	 */
7225aefb655Srie 	if ((pfd = pr_open(lml)) == FD_UNAVAIL)
7237c478bd9Sstevel@tonic-gate 		return (1);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCNMAP, (void *)&num) == -1) {
7267c478bd9Sstevel@tonic-gate 		err = errno;
7275aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name,
7285aefb655Srie 		    strerror(err));
7297c478bd9Sstevel@tonic-gate 		return (1);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	if ((maps = malloc((num + 1) * sizeof (prmap_t))) == 0)
7337c478bd9Sstevel@tonic-gate 		return (1);
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCMAP, (void *)maps) == -1) {
7367c478bd9Sstevel@tonic-gate 		err = errno;
7375aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name,
7385aefb655Srie 		    strerror(err));
7397c478bd9Sstevel@tonic-gate 		free(maps);
7407c478bd9Sstevel@tonic-gate 		return (1);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/*
7447c478bd9Sstevel@tonic-gate 	 * Determine if the supplied address clashes with any of the present
7457c478bd9Sstevel@tonic-gate 	 * process mappings.
7467c478bd9Sstevel@tonic-gate 	 */
7477c478bd9Sstevel@tonic-gate 	for (_num = 0, _maps = maps; _num < num; _num++, _maps++) {
7487c478bd9Sstevel@tonic-gate 		caddr_t		_eaddr = _maps->pr_vaddr + _maps->pr_size;
7497c478bd9Sstevel@tonic-gate 		Rt_map		*lmp;
7507c478bd9Sstevel@tonic-gate 		const char	*str;
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 		if ((eaddr < _maps->pr_vaddr) || (vaddr >= _eaddr))
7537c478bd9Sstevel@tonic-gate 			continue;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		/*
7567c478bd9Sstevel@tonic-gate 		 * We have a memory clash.  See if one of the known dynamic
7577c478bd9Sstevel@tonic-gate 		 * dependency mappings represents this space so as to provide
7587c478bd9Sstevel@tonic-gate 		 * the user a more meaningful message.
7597c478bd9Sstevel@tonic-gate 		 */
7607c478bd9Sstevel@tonic-gate 		if ((lmp = _caller(vaddr, 0)) != 0)
7617c478bd9Sstevel@tonic-gate 			str = NAME(lmp);
7627c478bd9Sstevel@tonic-gate 		else
7637c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_STR_UNKNOWN);
7647c478bd9Sstevel@tonic-gate 
7655aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_MAPINUSE), name,
7665aefb655Srie 		    EC_NATPTR(vaddr), EC_OFF(size), str);
7677c478bd9Sstevel@tonic-gate 		return (1);
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	free(maps);
7707c478bd9Sstevel@tonic-gate 	return (0);
7717c478bd9Sstevel@tonic-gate }
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate /*
7747c478bd9Sstevel@tonic-gate  * Obtain a memory reservation.  On newer systems, both MAP_ANON and MAP_ALIGN
7757c478bd9Sstevel@tonic-gate  * are used to obtained an aligned reservation from anonymous memory.  If
7767c478bd9Sstevel@tonic-gate  * MAP_ANON isn't available, then MAP_ALIGN isn't either, so obtain a standard
7777c478bd9Sstevel@tonic-gate  * reservation using the file as backing.
7787c478bd9Sstevel@tonic-gate  */
7797c478bd9Sstevel@tonic-gate static Am_ret
7805aefb655Srie elf_map_reserve(Lm_list *lml, const char *name, caddr_t *maddr, Off msize,
7815aefb655Srie     int mperm, int fd, Xword align)
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate 	Am_ret	amret;
7847c478bd9Sstevel@tonic-gate 	int	mflag = MAP_PRIVATE | MAP_NORESERVE;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
7877c478bd9Sstevel@tonic-gate 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
7887c478bd9Sstevel@tonic-gate 		mflag |= MAP_ALIGN;
7897c478bd9Sstevel@tonic-gate 		*maddr = (caddr_t)align;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate #endif
7925aefb655Srie 	if ((amret = anon_map(lml, maddr, msize, PROT_NONE, mflag)) == AM_ERROR)
7937c478bd9Sstevel@tonic-gate 		return (amret);
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	if (amret == AM_OK)
7967c478bd9Sstevel@tonic-gate 		return (AM_OK);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	/*
7997c478bd9Sstevel@tonic-gate 	 * If an anonymous memory request failed (which should only be the
8007c478bd9Sstevel@tonic-gate 	 * case if it is unsupported on the system we're running on), establish
8017c478bd9Sstevel@tonic-gate 	 * the initial mapping directly from the file.
8027c478bd9Sstevel@tonic-gate 	 */
8037c478bd9Sstevel@tonic-gate 	*maddr = 0;
8047c478bd9Sstevel@tonic-gate 	if ((*maddr = mmap(*maddr, msize, mperm, MAP_PRIVATE,
8057c478bd9Sstevel@tonic-gate 	    fd, 0)) == MAP_FAILED) {
8067c478bd9Sstevel@tonic-gate 		int	err = errno;
8075aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), name,
8085aefb655Srie 		    strerror(err));
8097c478bd9Sstevel@tonic-gate 		return (AM_ERROR);
8107c478bd9Sstevel@tonic-gate 	}
8117c478bd9Sstevel@tonic-gate 	return (AM_NOSUP);
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate static void *
8157c478bd9Sstevel@tonic-gate elf_map_textdata(caddr_t addr, Off flen, int mperm, int phdr_mperm, int mflag,
8167c478bd9Sstevel@tonic-gate     int fd, Off foff)
8177c478bd9Sstevel@tonic-gate {
8187c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
8197c478bd9Sstevel@tonic-gate 	static int	notd = 0;
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	/*
8227c478bd9Sstevel@tonic-gate 	 * If MAP_TEXT and MAP_INITDATA are available, select the appropriate
8237c478bd9Sstevel@tonic-gate 	 * flag.
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 	if (notd == 0) {
8267c478bd9Sstevel@tonic-gate 		if ((phdr_mperm & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC)
8277c478bd9Sstevel@tonic-gate 			mflag |= MAP_TEXT;
8287c478bd9Sstevel@tonic-gate 		else
8297c478bd9Sstevel@tonic-gate 			mflag |= MAP_INITDATA;
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate #endif
8327c478bd9Sstevel@tonic-gate 	if (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff) != MAP_FAILED)
8337c478bd9Sstevel@tonic-gate 		return (0);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
8367c478bd9Sstevel@tonic-gate 	if ((notd == 0) && (errno == EINVAL)) {
8377c478bd9Sstevel@tonic-gate 		/*
8387c478bd9Sstevel@tonic-gate 		 * MAP_TEXT and MAP_INITDATA may not be supported on this
8397c478bd9Sstevel@tonic-gate 		 * platform, try again without.
8407c478bd9Sstevel@tonic-gate 		 */
8417c478bd9Sstevel@tonic-gate 		notd = 1;
8427c478bd9Sstevel@tonic-gate 		mflag &= ~(MAP_TEXT | MAP_INITDATA);
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 		return (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff));
8457c478bd9Sstevel@tonic-gate 	}
8467c478bd9Sstevel@tonic-gate #endif
8477c478bd9Sstevel@tonic-gate 	return (MAP_FAILED);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate  * Map in a file.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate static caddr_t
8547c478bd9Sstevel@tonic-gate elf_map_it(
8555aefb655Srie 	Lm_list		*lml,		/* link-map list */
8567c478bd9Sstevel@tonic-gate 	const char	*name,		/* actual name stored for pathname */
8577c478bd9Sstevel@tonic-gate 	Off		fsize,		/* total mapping claim of the file */
8587c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr,		/* ELF header of file */
8597c478bd9Sstevel@tonic-gate 	Phdr		*fphdr,		/* first loadable Phdr */
8607c478bd9Sstevel@tonic-gate 	Phdr		*lphdr,		/* last loadable Phdr */
8617c478bd9Sstevel@tonic-gate 	Phdr		**rrphdr,	/* return first Phdr in reservation */
8627c478bd9Sstevel@tonic-gate 	caddr_t		*rraddr,	/* return start of reservation */
8637c478bd9Sstevel@tonic-gate 	Off		*rrsize,	/* return total size of reservation */
8647c478bd9Sstevel@tonic-gate 	int		fixed,		/* image is resolved to a fixed addr */
8657c478bd9Sstevel@tonic-gate 	int		fd,		/* images file descriptor */
8667c478bd9Sstevel@tonic-gate 	Xword		align,		/* image segments maximum alignment */
8677c478bd9Sstevel@tonic-gate 	Mmap		*mmaps,		/* mmap information array and */
8687c478bd9Sstevel@tonic-gate 	uint_t		*mmapcnt)	/* 	mapping count */
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	caddr_t		raddr;		/* reservation address */
8717c478bd9Sstevel@tonic-gate 	Off		rsize;		/* reservation size */
8727c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* working program header poiner */
8737c478bd9Sstevel@tonic-gate 	caddr_t		maddr;		/* working mmap address */
8747c478bd9Sstevel@tonic-gate 	caddr_t		faddr;		/* working file address */
8757c478bd9Sstevel@tonic-gate 	size_t		padsize;	/* object padding requirement */
8767c478bd9Sstevel@tonic-gate 	size_t		padpsize = 0;	/* padding size rounded to next page */
8777c478bd9Sstevel@tonic-gate 	size_t		padmsize = 0;	/* padding size rounded for alignment */
8787c478bd9Sstevel@tonic-gate 	int		skipfseg;	/* skip mapping first segment */
8797c478bd9Sstevel@tonic-gate 	int		mperm;		/* segment permissions */
8807c478bd9Sstevel@tonic-gate 	Am_ret		amret = AM_NOSUP;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	/*
8837c478bd9Sstevel@tonic-gate 	 * If padding is required extend both the front and rear of the image.
8847c478bd9Sstevel@tonic-gate 	 * To insure the image itself is mapped at the correct alignment the
8857c478bd9Sstevel@tonic-gate 	 * initial padding is rounded up to the nearest page.  Once the image is
8867c478bd9Sstevel@tonic-gate 	 * mapped the excess can be pruned to the nearest page required for the
8877c478bd9Sstevel@tonic-gate 	 * actual padding itself.
8887c478bd9Sstevel@tonic-gate 	 */
8897c478bd9Sstevel@tonic-gate 	if ((padsize = r_debug.rtd_objpad) != 0) {
8907c478bd9Sstevel@tonic-gate 		padpsize = M_PROUND(padsize);
8917c478bd9Sstevel@tonic-gate 		if (fixed)
8927c478bd9Sstevel@tonic-gate 			padmsize = padpsize;
8937c478bd9Sstevel@tonic-gate 		else
8947c478bd9Sstevel@tonic-gate 			padmsize = S_ROUND(padsize, align);
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/*
8987c478bd9Sstevel@tonic-gate 	 * Determine the initial permissions used to map in the first segment.
8997c478bd9Sstevel@tonic-gate 	 * If this segments memsz is greater that its filesz then the difference
9007c478bd9Sstevel@tonic-gate 	 * must be zeroed.  Make sure this segment is writable.
9017c478bd9Sstevel@tonic-gate 	 */
9027c478bd9Sstevel@tonic-gate 	mperm = 0;
9037c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_R)
9047c478bd9Sstevel@tonic-gate 		mperm |= PROT_READ;
9057c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_X)
9067c478bd9Sstevel@tonic-gate 		mperm |= PROT_EXEC;
9077c478bd9Sstevel@tonic-gate 	if ((fphdr->p_flags & PF_W) || (fphdr->p_memsz > fphdr->p_filesz))
9087c478bd9Sstevel@tonic-gate 		mperm |= PROT_WRITE;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/*
9117c478bd9Sstevel@tonic-gate 	 * Determine whether or not to let system reserve address space based on
9127c478bd9Sstevel@tonic-gate 	 * whether this is a dynamic executable (addresses in object are fixed)
9137c478bd9Sstevel@tonic-gate 	 * or a shared object (addresses in object are relative to the objects'
9147c478bd9Sstevel@tonic-gate 	 * base).
9157c478bd9Sstevel@tonic-gate 	 */
9167c478bd9Sstevel@tonic-gate 	if (fixed) {
9177c478bd9Sstevel@tonic-gate 		/*
9187c478bd9Sstevel@tonic-gate 		 * Determine the reservation address and size, and insure that
9197c478bd9Sstevel@tonic-gate 		 * this reservation isn't already in use.
9207c478bd9Sstevel@tonic-gate 		 */
9217c478bd9Sstevel@tonic-gate 		faddr = maddr = (caddr_t)M_PTRUNC((ulong_t)fphdr->p_vaddr);
9227c478bd9Sstevel@tonic-gate 		raddr = maddr - padpsize;
9237c478bd9Sstevel@tonic-gate 		rsize = fsize + padpsize + padsize;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 		if (lml_main.lm_head) {
9265aefb655Srie 			if (elf_map_check(lml, name, raddr, rsize) != 0)
9277c478bd9Sstevel@tonic-gate 				return (0);
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		/*
9317c478bd9Sstevel@tonic-gate 		 * As this is a fixed image, all segments must be individually
9327c478bd9Sstevel@tonic-gate 		 * mapped.
9337c478bd9Sstevel@tonic-gate 		 */
9347c478bd9Sstevel@tonic-gate 		skipfseg = 0;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	} else {
9377c478bd9Sstevel@tonic-gate 		size_t	esize;
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 		/*
9407c478bd9Sstevel@tonic-gate 		 * If this isn't a fixed image, reserve enough address space for
9417c478bd9Sstevel@tonic-gate 		 * the entire image to be mapped.  The amount of reservation is
9427c478bd9Sstevel@tonic-gate 		 * the range between the beginning of the first, and end of the
9437c478bd9Sstevel@tonic-gate 		 * last loadable segment, together with any padding, plus the
9447c478bd9Sstevel@tonic-gate 		 * alignment of the first segment.
9457c478bd9Sstevel@tonic-gate 		 *
9467c478bd9Sstevel@tonic-gate 		 * The optimal reservation is made as a no-reserve mapping from
9477c478bd9Sstevel@tonic-gate 		 * anonymous memory.  Each segment is then mapped into this
9487c478bd9Sstevel@tonic-gate 		 * reservation.  If the anonymous mapping capability isn't
9497c478bd9Sstevel@tonic-gate 		 * available, the reservation is obtained from the file itself.
9507c478bd9Sstevel@tonic-gate 		 * In this case the first segment of the image is mapped as part
9517c478bd9Sstevel@tonic-gate 		 * of the reservation, thus only the following segments need to
9527c478bd9Sstevel@tonic-gate 		 * be remapped.
9537c478bd9Sstevel@tonic-gate 		 */
9547c478bd9Sstevel@tonic-gate 		rsize = fsize + padmsize + padsize;
9555aefb655Srie 		if ((amret = elf_map_reserve(lml, name, &raddr, rsize, mperm,
9567c478bd9Sstevel@tonic-gate 		    fd, align)) == AM_ERROR)
9577c478bd9Sstevel@tonic-gate 			return (0);
9587c478bd9Sstevel@tonic-gate 		maddr = raddr + padmsize;
9597c478bd9Sstevel@tonic-gate 		faddr = (caddr_t)S_ROUND((Off)maddr, align);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 		/*
9627c478bd9Sstevel@tonic-gate 		 * If this reservation has been obtained from anonymous memory,
9637c478bd9Sstevel@tonic-gate 		 * then all segments must be individually mapped.  Otherwise,
9647c478bd9Sstevel@tonic-gate 		 * the first segment heads the reservation.
9657c478bd9Sstevel@tonic-gate 		 */
9667c478bd9Sstevel@tonic-gate 		if (amret == AM_OK)
9677c478bd9Sstevel@tonic-gate 			skipfseg = 0;
9687c478bd9Sstevel@tonic-gate 		else
9697c478bd9Sstevel@tonic-gate 			skipfseg = 1;
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 		/*
9727c478bd9Sstevel@tonic-gate 		 * For backward compatibility (where MAP_ALIGN isn't available),
9737c478bd9Sstevel@tonic-gate 		 * insure the alignment of the reservation is adequate for this
9747c478bd9Sstevel@tonic-gate 		 * object, and if not remap the object to obtain the correct
9757c478bd9Sstevel@tonic-gate 		 * alignment.
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		if (faddr != maddr) {
9787c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, rsize);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 			rsize += align;
9815aefb655Srie 			if ((amret = elf_map_reserve(lml, name, &raddr, rsize,
9825aefb655Srie 			    mperm, fd, align)) == AM_ERROR)
9837c478bd9Sstevel@tonic-gate 				return (0);
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate 			maddr = faddr = (caddr_t)S_ROUND((Off)(raddr +
9867c478bd9Sstevel@tonic-gate 			    padpsize), align);
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 			esize = maddr - raddr + padpsize;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 			/*
9917c478bd9Sstevel@tonic-gate 			 * As ths image has been realigned, the first segment
9927c478bd9Sstevel@tonic-gate 			 * of the file needs to be remapped to its correct
9937c478bd9Sstevel@tonic-gate 			 * location.
9947c478bd9Sstevel@tonic-gate 			 */
9957c478bd9Sstevel@tonic-gate 			skipfseg = 0;
9967c478bd9Sstevel@tonic-gate 		} else
9977c478bd9Sstevel@tonic-gate 			esize = padmsize - padpsize;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		/*
10007c478bd9Sstevel@tonic-gate 		 * If this reservation included padding, remove any excess for
10017c478bd9Sstevel@tonic-gate 		 * the start of the image (the padding was adjusted to insure
10027c478bd9Sstevel@tonic-gate 		 * the image was aligned appropriately).
10037c478bd9Sstevel@tonic-gate 		 */
10047c478bd9Sstevel@tonic-gate 		if (esize) {
10057c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, esize);
10067c478bd9Sstevel@tonic-gate 			raddr += esize;
10077c478bd9Sstevel@tonic-gate 			rsize -= esize;
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 	}
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	/*
10127c478bd9Sstevel@tonic-gate 	 * At this point we know the initial location of the image, and its
10137c478bd9Sstevel@tonic-gate 	 * size.  Pass these back to the caller for inclusion in the link-map
10147c478bd9Sstevel@tonic-gate 	 * that will eventually be created.
10157c478bd9Sstevel@tonic-gate 	 */
10167c478bd9Sstevel@tonic-gate 	*rraddr = raddr;
10177c478bd9Sstevel@tonic-gate 	*rrsize = rsize;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/*
10207c478bd9Sstevel@tonic-gate 	 * The first loadable segment is now pointed to by maddr.  This segment
10217c478bd9Sstevel@tonic-gate 	 * will eventually contain the elf header and program headers, so reset
10227c478bd9Sstevel@tonic-gate 	 * the program header.  Pass this  back to the caller for inclusion in
10237c478bd9Sstevel@tonic-gate 	 * the link-map so it can be used for later unmapping operations.
10247c478bd9Sstevel@tonic-gate 	 */
10257c478bd9Sstevel@tonic-gate 	/* LINTED */
10267c478bd9Sstevel@tonic-gate 	*rrphdr = (Phdr *)((char *)maddr + ehdr->e_phoff);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * If padding is required at the front of the image, obtain that now.
10307c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
10317c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
10327c478bd9Sstevel@tonic-gate 	 * Otherwise this reservation is backed by the file, or in the case of
10337c478bd9Sstevel@tonic-gate 	 * a fixed image, doesn't yet exist.  Map the padding so that it is
10347c478bd9Sstevel@tonic-gate 	 * suitably protected (PROT_NONE), and insure the first segment of the
10357c478bd9Sstevel@tonic-gate 	 * file is mapped to its correct location.
10367c478bd9Sstevel@tonic-gate 	 */
10377c478bd9Sstevel@tonic-gate 	if (padsize) {
10387c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
10395aefb655Srie 			if (dz_map(lml, raddr, padpsize, PROT_NONE,
10405aefb655Srie 			    (MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE)) ==
10415aefb655Srie 			    MAP_FAILED)
10427c478bd9Sstevel@tonic-gate 				return (0);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 			skipfseg = 0;
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 		rsize -= padpsize;
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	/*
10507c478bd9Sstevel@tonic-gate 	 * Map individual segments.  For a fixed image, these will each be
10517c478bd9Sstevel@tonic-gate 	 * unique mappings.  For a reservation these will fill in the
10527c478bd9Sstevel@tonic-gate 	 * reservation.
10537c478bd9Sstevel@tonic-gate 	 */
10547c478bd9Sstevel@tonic-gate 	for (phdr = fphdr; phdr <= lphdr;
10557c478bd9Sstevel@tonic-gate 	    phdr = (Phdr *)((Off)phdr + ehdr->e_phentsize)) {
10567c478bd9Sstevel@tonic-gate 		caddr_t	addr;
10577c478bd9Sstevel@tonic-gate 		Off	mlen, flen;
10587c478bd9Sstevel@tonic-gate 		size_t	size;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 		/*
10617c478bd9Sstevel@tonic-gate 		 * Skip non-loadable segments or segments that don't occupy
10627c478bd9Sstevel@tonic-gate 		 * any memory.
10637c478bd9Sstevel@tonic-gate 		 */
10647c478bd9Sstevel@tonic-gate 		if (((phdr->p_type != PT_LOAD) &&
10657c478bd9Sstevel@tonic-gate 		    (phdr->p_type != PT_SUNWBSS)) || (phdr->p_memsz == 0))
10667c478bd9Sstevel@tonic-gate 			continue;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 		/*
10697c478bd9Sstevel@tonic-gate 		 * Establish this segments address relative to our base.
10707c478bd9Sstevel@tonic-gate 		 */
10717c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PTRUNC((ulong_t)(phdr->p_vaddr +
10727c478bd9Sstevel@tonic-gate 		    (fixed ? 0 : faddr)));
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 		/*
10757c478bd9Sstevel@tonic-gate 		 * Determine the mapping protection from the segment attributes.
10767c478bd9Sstevel@tonic-gate 		 * Also determine the etext address from the last loadable
10777c478bd9Sstevel@tonic-gate 		 * segment which has permissions but no write access.
10787c478bd9Sstevel@tonic-gate 		 */
10797c478bd9Sstevel@tonic-gate 		mperm = 0;
10807c478bd9Sstevel@tonic-gate 		if (phdr->p_flags) {
10817c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_R)
10827c478bd9Sstevel@tonic-gate 				mperm |= PROT_READ;
10837c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_X)
10847c478bd9Sstevel@tonic-gate 				mperm |= PROT_EXEC;
10857c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_W)
10867c478bd9Sstevel@tonic-gate 				mperm |= PROT_WRITE;
10877c478bd9Sstevel@tonic-gate 			else
10887c478bd9Sstevel@tonic-gate 				fmap->fm_etext = phdr->p_vaddr + phdr->p_memsz +
10897c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
10907c478bd9Sstevel@tonic-gate 		}
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 		/*
10937c478bd9Sstevel@tonic-gate 		 * Determine the type of mapping required.
10947c478bd9Sstevel@tonic-gate 		 */
10957c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_SUNWBSS) {
10967c478bd9Sstevel@tonic-gate 			/*
10977c478bd9Sstevel@tonic-gate 			 * Potentially, we can defer the loading of any SUNWBSS
10987c478bd9Sstevel@tonic-gate 			 * segment, depending on whether the symbols it provides
10997c478bd9Sstevel@tonic-gate 			 * have been bound to.  In this manner, large segments
11007c478bd9Sstevel@tonic-gate 			 * that are interposed upon between shared libraries
11017c478bd9Sstevel@tonic-gate 			 * may not require mapping.  Note, that the mapping
11027c478bd9Sstevel@tonic-gate 			 * information is recorded in our mapping descriptor at
11037c478bd9Sstevel@tonic-gate 			 * this time.
11047c478bd9Sstevel@tonic-gate 			 */
11057c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11067c478bd9Sstevel@tonic-gate 			flen = 0;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		} else if ((phdr->p_filesz == 0) && (phdr->p_flags == 0)) {
11097c478bd9Sstevel@tonic-gate 			/*
11107c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file and no flags
11117c478bd9Sstevel@tonic-gate 			 * specified, then it defines a reservation.  At this
11127c478bd9Sstevel@tonic-gate 			 * point all standard loadable segments will have been
11137c478bd9Sstevel@tonic-gate 			 * processed.  The segment reservation is mapped
11147c478bd9Sstevel@tonic-gate 			 * directly from /dev/null.
11157c478bd9Sstevel@tonic-gate 			 */
11165aefb655Srie 			if (nu_map(lml, (caddr_t)addr, phdr->p_memsz, PROT_NONE,
11177c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
11187c478bd9Sstevel@tonic-gate 				return (0);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11217c478bd9Sstevel@tonic-gate 			flen = 0;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 		} else if (phdr->p_filesz == 0) {
11247c478bd9Sstevel@tonic-gate 			/*
11257c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file then it defines a
11267c478bd9Sstevel@tonic-gate 			 * nobits segment and is mapped directly from /dev/zero.
11277c478bd9Sstevel@tonic-gate 			 */
11285aefb655Srie 			if (dz_map(lml, (caddr_t)addr, phdr->p_memsz, mperm,
11297c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
11307c478bd9Sstevel@tonic-gate 				return (0);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11337c478bd9Sstevel@tonic-gate 			flen = 0;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 		} else {
11367c478bd9Sstevel@tonic-gate 			Off	foff;
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 			/*
11397c478bd9Sstevel@tonic-gate 			 * This mapping originates from the file.  Determine the
11407c478bd9Sstevel@tonic-gate 			 * file offset to which the mapping will be directed
11417c478bd9Sstevel@tonic-gate 			 * (must be aligned) and how much to map (might be more
11427c478bd9Sstevel@tonic-gate 			 * than the file in the case of .bss).
11437c478bd9Sstevel@tonic-gate 			 */
11447c478bd9Sstevel@tonic-gate 			foff = M_PTRUNC((ulong_t)phdr->p_offset);
11457c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz + (phdr->p_offset - foff);
11467c478bd9Sstevel@tonic-gate 			flen = phdr->p_filesz + (phdr->p_offset - foff);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 			/*
11497c478bd9Sstevel@tonic-gate 			 * If this is a non-fixed, non-anonymous mapping, and no
11507c478bd9Sstevel@tonic-gate 			 * padding is involved, then the first loadable segment
11517c478bd9Sstevel@tonic-gate 			 * is already part of the initial reservation.  In this
11527c478bd9Sstevel@tonic-gate 			 * case there is no need to remap this segment.
11537c478bd9Sstevel@tonic-gate 			 */
11547c478bd9Sstevel@tonic-gate 			if ((skipfseg == 0) || (phdr != fphdr)) {
11557c478bd9Sstevel@tonic-gate 				int phdr_mperm = mperm;
11567c478bd9Sstevel@tonic-gate 				/*
11577c478bd9Sstevel@tonic-gate 				 * If this segments memsz is greater that its
11587c478bd9Sstevel@tonic-gate 				 * filesz then the difference must be zeroed.
11597c478bd9Sstevel@tonic-gate 				 * Make sure this segment is writable.
11607c478bd9Sstevel@tonic-gate 				 */
11617c478bd9Sstevel@tonic-gate 				if (phdr->p_memsz > phdr->p_filesz)
11627c478bd9Sstevel@tonic-gate 					mperm |= PROT_WRITE;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 				if (elf_map_textdata((caddr_t)addr, flen,
11657c478bd9Sstevel@tonic-gate 				    mperm, phdr_mperm,
11667c478bd9Sstevel@tonic-gate 				    (MAP_FIXED | MAP_PRIVATE), fd, foff) ==
11677c478bd9Sstevel@tonic-gate 				    MAP_FAILED) {
11687c478bd9Sstevel@tonic-gate 					int	err = errno;
11695aefb655Srie 					eprintf(lml, ERR_FATAL,
11707c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYS_MMAP), name,
11717c478bd9Sstevel@tonic-gate 					    strerror(err));
11727c478bd9Sstevel@tonic-gate 					return (0);
11737c478bd9Sstevel@tonic-gate 				}
11747c478bd9Sstevel@tonic-gate 			}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 			/*
11777c478bd9Sstevel@tonic-gate 			 * If the memory occupancy of the segment overflows the
11787c478bd9Sstevel@tonic-gate 			 * definition in the file, we need to "zero out" the end
11797c478bd9Sstevel@tonic-gate 			 * of the mapping we've established, and if necessary,
11807c478bd9Sstevel@tonic-gate 			 * map some more space from /dev/zero.  Note, zero'ed
11817c478bd9Sstevel@tonic-gate 			 * memory must end on a double word boundary to satisfy
11827c478bd9Sstevel@tonic-gate 			 * zero().
11837c478bd9Sstevel@tonic-gate 			 */
11847c478bd9Sstevel@tonic-gate 			if (phdr->p_memsz > phdr->p_filesz) {
11857c478bd9Sstevel@tonic-gate 				caddr_t	zaddr;
11867c478bd9Sstevel@tonic-gate 				size_t	zlen, zplen;
11877c478bd9Sstevel@tonic-gate 				Off	fend;
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 				foff = (Off)(phdr->p_vaddr + phdr->p_filesz +
11907c478bd9Sstevel@tonic-gate 				    (fixed ? 0 : faddr));
11917c478bd9Sstevel@tonic-gate 				zaddr = (caddr_t)M_PROUND(foff);
11927c478bd9Sstevel@tonic-gate 				zplen = (size_t)(zaddr - foff);
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate 				fend = (Off)S_DROUND((size_t)(phdr->p_vaddr +
11957c478bd9Sstevel@tonic-gate 				    phdr->p_memsz + (fixed ? 0 : faddr)));
11967c478bd9Sstevel@tonic-gate 				zlen = (size_t)(fend - foff);
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 				/*
11997c478bd9Sstevel@tonic-gate 				 * Determine whether the number of bytes that
12007c478bd9Sstevel@tonic-gate 				 * must be zero'ed overflow to the next page.
12017c478bd9Sstevel@tonic-gate 				 * If not, simply clear the exact bytes
12027c478bd9Sstevel@tonic-gate 				 * (filesz to memsz) from this page.  Otherwise,
12037c478bd9Sstevel@tonic-gate 				 * clear the remaining bytes of this page, and
12047c478bd9Sstevel@tonic-gate 				 * map an following pages from /dev/zero.
12057c478bd9Sstevel@tonic-gate 				 */
12067c478bd9Sstevel@tonic-gate 				if (zlen < zplen)
12077c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zlen);
12087c478bd9Sstevel@tonic-gate 				else {
12097c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zplen);
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 					if ((zlen = (fend - (Off)zaddr)) > 0) {
12125aefb655Srie 						if (dz_map(lml, zaddr, zlen,
12135aefb655Srie 						    mperm,
12147c478bd9Sstevel@tonic-gate 						    MAP_FIXED | MAP_PRIVATE) ==
12157c478bd9Sstevel@tonic-gate 						    MAP_FAILED)
12167c478bd9Sstevel@tonic-gate 							return (0);
12177c478bd9Sstevel@tonic-gate 					}
12187c478bd9Sstevel@tonic-gate 				}
12197c478bd9Sstevel@tonic-gate 			}
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		/*
12237c478bd9Sstevel@tonic-gate 		 * Unmap anything from the last mapping address to this one and
12247c478bd9Sstevel@tonic-gate 		 * update the mapping claim pointer.
12257c478bd9Sstevel@tonic-gate 		 */
12267c478bd9Sstevel@tonic-gate 		if ((fixed == 0) && ((size = addr - maddr) != 0)) {
12277c478bd9Sstevel@tonic-gate 			(void) munmap(maddr, size);
12287c478bd9Sstevel@tonic-gate 			rsize -= size;
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		/*
12327c478bd9Sstevel@tonic-gate 		 * Retain this segments mapping information.
12337c478bd9Sstevel@tonic-gate 		 */
12347c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_vaddr = addr;
12357c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_msize = mlen;
12367c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_fsize = flen;
12377c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_perm = mperm;
12387c478bd9Sstevel@tonic-gate 		(*mmapcnt)++;
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		maddr = addr + M_PROUND(mlen);
12417c478bd9Sstevel@tonic-gate 		rsize -= M_PROUND(mlen);
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	/*
12457c478bd9Sstevel@tonic-gate 	 * If padding is required at the end of the image, obtain that now.
12467c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
12477c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
12487c478bd9Sstevel@tonic-gate 	 */
12497c478bd9Sstevel@tonic-gate 	if (padsize) {
12507c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
12517c478bd9Sstevel@tonic-gate 			/*
12527c478bd9Sstevel@tonic-gate 			 * maddr is currently page aligned from the last segment
12537c478bd9Sstevel@tonic-gate 			 * mapping.
12547c478bd9Sstevel@tonic-gate 			 */
12555aefb655Srie 			if (dz_map(lml, maddr, padsize, PROT_NONE,
12565aefb655Srie 			    (MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE)) ==
12575aefb655Srie 			    MAP_FAILED)
12587c478bd9Sstevel@tonic-gate 				return (0);
12597c478bd9Sstevel@tonic-gate 		}
12607c478bd9Sstevel@tonic-gate 		maddr += padsize;
12617c478bd9Sstevel@tonic-gate 		rsize -= padsize;
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	/*
12657c478bd9Sstevel@tonic-gate 	 * Unmap any final reservation.
12667c478bd9Sstevel@tonic-gate 	 */
12677c478bd9Sstevel@tonic-gate 	if ((fixed == 0) && (rsize != 0))
12687c478bd9Sstevel@tonic-gate 		(void) munmap(maddr, rsize);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	return (faddr);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate /*
12747c478bd9Sstevel@tonic-gate  * A null symbol interpretor.  Used if a filter has no associated filtees.
12757c478bd9Sstevel@tonic-gate  */
12767c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
12777c478bd9Sstevel@tonic-gate static Sym *
12787c478bd9Sstevel@tonic-gate elf_null_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate /*
12847c478bd9Sstevel@tonic-gate  * Disable filtee use.
12857c478bd9Sstevel@tonic-gate  */
12867c478bd9Sstevel@tonic-gate static void
12879a411307Srie elf_disable_filtee(Rt_map *lmp, Dyninfo *dip)
12887c478bd9Sstevel@tonic-gate {
12897c478bd9Sstevel@tonic-gate 	dip->di_info = 0;
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) {
12927c478bd9Sstevel@tonic-gate 		/*
12937c478bd9Sstevel@tonic-gate 		 * If this is an object filter, free the filtee's duplication.
12947c478bd9Sstevel@tonic-gate 		 */
12957c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(lmp) != FLTR_DISABLED) {
12967c478bd9Sstevel@tonic-gate 			free(REFNAME(lmp));
12977c478bd9Sstevel@tonic-gate 			REFNAME(lmp) = (char *)0;
12987c478bd9Sstevel@tonic-gate 			OBJFLTRNDX(lmp) = FLTR_DISABLED;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 			/*
13017c478bd9Sstevel@tonic-gate 			 * Indicate that this filtee is no longer available.
13027c478bd9Sstevel@tonic-gate 			 */
13037c478bd9Sstevel@tonic-gate 			if (dip->di_flags & FLG_DI_STDFLTR)
13047c478bd9Sstevel@tonic-gate 				SYMINTP(lmp) = elf_null_find_sym;
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 	} else if (dip->di_flags & FLG_DI_STDFLTR) {
13087c478bd9Sstevel@tonic-gate 		/*
13097c478bd9Sstevel@tonic-gate 		 * Indicate that this standard filtee is no longer available.
13107c478bd9Sstevel@tonic-gate 		 */
13117c478bd9Sstevel@tonic-gate 		if (SYMSFLTRCNT(lmp))
13127c478bd9Sstevel@tonic-gate 			SYMSFLTRCNT(lmp)--;
13137c478bd9Sstevel@tonic-gate 	} else {
13147c478bd9Sstevel@tonic-gate 		/*
13157c478bd9Sstevel@tonic-gate 		 * Indicate that this auxiliary filtee is no longer available.
13167c478bd9Sstevel@tonic-gate 		 */
13177c478bd9Sstevel@tonic-gate 		if (SYMAFLTRCNT(lmp))
13187c478bd9Sstevel@tonic-gate 			SYMAFLTRCNT(lmp)--;
13197c478bd9Sstevel@tonic-gate 	}
13207c478bd9Sstevel@tonic-gate 	dip->di_flags &= ~MSK_DI_FILTER;
13217c478bd9Sstevel@tonic-gate }
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate /*
13247c478bd9Sstevel@tonic-gate  * Find symbol interpreter - filters.
13257c478bd9Sstevel@tonic-gate  * This function is called when the symbols from a shared object should
13267c478bd9Sstevel@tonic-gate  * be resolved from the shared objects filtees instead of from within itself.
13277c478bd9Sstevel@tonic-gate  *
13287c478bd9Sstevel@tonic-gate  * A symbol name of 0 is used to trigger filtee loading.
13297c478bd9Sstevel@tonic-gate  */
13307c478bd9Sstevel@tonic-gate static Sym *
13317c478bd9Sstevel@tonic-gate _elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name, *filtees;
13347c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
13357c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
13367c478bd9Sstevel@tonic-gate 	Pnode		*pnp, **pnpp;
13377c478bd9Sstevel@tonic-gate 	int		any;
13387c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(ilmp)[ndx];
13397c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(ilmp);
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	/*
13427c478bd9Sstevel@tonic-gate 	 * Indicate that the filter has been used.  If a binding already exists
13437c478bd9Sstevel@tonic-gate 	 * to the caller, indicate that this object is referenced.  This insures
13447c478bd9Sstevel@tonic-gate 	 * we don't generate false unreferenced diagnostics from ldd -u/U or
13457c478bd9Sstevel@tonic-gate 	 * debugging.  Don't create a binding regardless, as this filter may
13467c478bd9Sstevel@tonic-gate 	 * have been dlopen()'ed.
13477c478bd9Sstevel@tonic-gate 	 */
13487c478bd9Sstevel@tonic-gate 	if (name && (ilmp != clmp)) {
13497c478bd9Sstevel@tonic-gate 		Word	tracing = (LIST(clmp)->lm_flags &
13507c478bd9Sstevel@tonic-gate 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED));
13517c478bd9Sstevel@tonic-gate 
13525aefb655Srie 		if (tracing || DBG_ENABLED) {
13537c478bd9Sstevel@tonic-gate 			Bnd_desc **	bdpp;
13547c478bd9Sstevel@tonic-gate 			Aliste		off;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 			FLAGS1(ilmp) |= FL1_RT_USED;
13577c478bd9Sstevel@tonic-gate 
13585aefb655Srie 			if ((tracing & LML_FLG_TRC_UNREF) || DBG_ENABLED) {
13597c478bd9Sstevel@tonic-gate 				for (ALIST_TRAVERSE(CALLERS(ilmp), off, bdpp)) {
13607c478bd9Sstevel@tonic-gate 					Bnd_desc *	bdp = *bdpp;
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 					if (bdp->b_caller == clmp) {
13637c478bd9Sstevel@tonic-gate 						bdp->b_flags |= BND_REFER;
13647c478bd9Sstevel@tonic-gate 						break;
13657c478bd9Sstevel@tonic-gate 					}
13667c478bd9Sstevel@tonic-gate 				}
13677c478bd9Sstevel@tonic-gate 			}
13687c478bd9Sstevel@tonic-gate 		}
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/*
13727c478bd9Sstevel@tonic-gate 	 * If this is the first call to process this filter, establish the
13737c478bd9Sstevel@tonic-gate 	 * filtee list.  If a configuration file exists, determine if any
13747c478bd9Sstevel@tonic-gate 	 * filtee associations for this filter, and its filtee reference, are
13757c478bd9Sstevel@tonic-gate 	 * defined.  Otherwise, process the filtee reference.  Any token
13767c478bd9Sstevel@tonic-gate 	 * expansion is also completed at this point (i.e., $PLATFORM).
13777c478bd9Sstevel@tonic-gate 	 */
13787c478bd9Sstevel@tonic-gate 	filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val;
13797c478bd9Sstevel@tonic-gate 	if (dip->di_info == 0) {
13807c478bd9Sstevel@tonic-gate 		if (rtld_flags2 & RT_FL2_FLTCFG)
13815aefb655Srie 			dip->di_info = elf_config_flt(lml, PATHNAME(ilmp),
13825aefb655Srie 			    filtees);
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 		if (dip->di_info == 0) {
13855aefb655Srie 			DBG_CALL(Dbg_file_filter(lml, NAME(ilmp), filtees, 0));
13867c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags &
13877c478bd9Sstevel@tonic-gate 			    (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
13887c478bd9Sstevel@tonic-gate 			    ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0))
13897c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FILTER),
13907c478bd9Sstevel@tonic-gate 				    NAME(ilmp), filtees);
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate 			if ((dip->di_info = (void *)expand_paths(ilmp,
13937c478bd9Sstevel@tonic-gate 			    filtees, PN_SER_FILTEE, 0)) == 0) {
13947c478bd9Sstevel@tonic-gate 				elf_disable_filtee(ilmp, dip);
13957c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
13967c478bd9Sstevel@tonic-gate 			}
13977c478bd9Sstevel@tonic-gate 		}
13987c478bd9Sstevel@tonic-gate 	}
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	/*
14017c478bd9Sstevel@tonic-gate 	 * Traverse the filtee list, dlopen()'ing any objects specified and
14027c478bd9Sstevel@tonic-gate 	 * using their group handle to lookup the symbol.
14037c478bd9Sstevel@tonic-gate 	 */
14047c478bd9Sstevel@tonic-gate 	for (any = 0, pnpp = (Pnode **)&(dip->di_info), pnp = *pnpp; pnp;
14059a411307Srie 	    pnpp = &pnp->p_next, pnp = *pnpp) {
14067c478bd9Sstevel@tonic-gate 		int	mode;
14077c478bd9Sstevel@tonic-gate 		Grp_hdl	*ghp;
14087c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
14117c478bd9Sstevel@tonic-gate 			continue;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 		/*
14147c478bd9Sstevel@tonic-gate 		 * Establish the mode of the filtee from the filter.  As filtees
14157c478bd9Sstevel@tonic-gate 		 * are loaded via a dlopen(), make sure that RTLD_GROUP is set
14167c478bd9Sstevel@tonic-gate 		 * and the filtees aren't global.  It would be nice to have
14177c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST used here also, but as filters got out long before
14187c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST was introduced it's a little too late now.
14197c478bd9Sstevel@tonic-gate 		 */
14207c478bd9Sstevel@tonic-gate 		mode = MODE(ilmp) | RTLD_GROUP;
14217c478bd9Sstevel@tonic-gate 		mode &= ~RTLD_GLOBAL;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 		/*
14247c478bd9Sstevel@tonic-gate 		 * Insure that any auxiliary filter can locate symbols from its
14257c478bd9Sstevel@tonic-gate 		 * caller.
14267c478bd9Sstevel@tonic-gate 		 */
14277c478bd9Sstevel@tonic-gate 		if (dip->di_flags & FLG_DI_AUXFLTR)
14287c478bd9Sstevel@tonic-gate 			mode |= RTLD_PARENT;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 		/*
14317c478bd9Sstevel@tonic-gate 		 * Process any hardware capability directory.  Establish a new
14327c478bd9Sstevel@tonic-gate 		 * link-map control list from which to analyze any newly added
143324a6229eSrie 		 * objects.
14347c478bd9Sstevel@tonic-gate 		 */
14357c478bd9Sstevel@tonic-gate 		if ((pnp->p_info == 0) && (pnp->p_orig & PN_TKN_HWCAP)) {
143611a2bb38Srie 			Lm_cntl	*lmc;
143711a2bb38Srie 			Aliste	lmco;
143811a2bb38Srie 
143924a6229eSrie 			if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
144024a6229eSrie 				if ((lmc = alist_append(&(lml->lm_lists), 0,
144124a6229eSrie 				    sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0)
144224a6229eSrie 					return ((Sym *)0);
14437c478bd9Sstevel@tonic-gate 				lmco = (Aliste)((char *)lmc -
14447c478bd9Sstevel@tonic-gate 				    (char *)lml->lm_lists);
144524a6229eSrie 			} else {
144624a6229eSrie 				lmc = 0;
14477c478bd9Sstevel@tonic-gate 				lmco = ALO_DATA;
144824a6229eSrie 			}
14497c478bd9Sstevel@tonic-gate 
145002ca3e02Srie 			pnp = hwcap_filtees(pnpp, lmco, lmc, dip, ilmp, filtees,
14517c478bd9Sstevel@tonic-gate 			    mode, (FLG_RT_HANDLE | FLG_RT_HWCAP));
145211a2bb38Srie 
145311a2bb38Srie 			/*
145411a2bb38Srie 			 * Now that any hardware capability objects have been
145511a2bb38Srie 			 * processed, remove any link-map control list.
145611a2bb38Srie 			 */
145702ca3e02Srie 			if (lmc)
145811a2bb38Srie 				remove_cntl(lml, lmco);
14597c478bd9Sstevel@tonic-gate 		}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
14627c478bd9Sstevel@tonic-gate 			continue;
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate 		/*
14657c478bd9Sstevel@tonic-gate 		 * Process an individual filtee.
14667c478bd9Sstevel@tonic-gate 		 */
14677c478bd9Sstevel@tonic-gate 		if (pnp->p_info == 0) {
14687c478bd9Sstevel@tonic-gate 			const char	*filtee = pnp->p_name;
14697c478bd9Sstevel@tonic-gate 			int		audit = 0;
14707c478bd9Sstevel@tonic-gate 
14715aefb655Srie 			DBG_CALL(Dbg_file_filtee(lml, NAME(ilmp), filtee, 0));
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate 			ghp = 0;
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate 			/*
14767c478bd9Sstevel@tonic-gate 			 * Determine if the reference link map is already
14777c478bd9Sstevel@tonic-gate 			 * loaded.  As an optimization compare the filtee with
14787c478bd9Sstevel@tonic-gate 			 * our interpretor.  The most common filter is
14797c478bd9Sstevel@tonic-gate 			 * libdl.so.1, which is a filter on ld.so.1.
14807c478bd9Sstevel@tonic-gate 			 */
14817c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
14827c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) {
14837c478bd9Sstevel@tonic-gate #else
14847c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) {
14857c478bd9Sstevel@tonic-gate #endif
14867c478bd9Sstevel@tonic-gate 				/*
14877c478bd9Sstevel@tonic-gate 				 * Create an association between ld.so.1 and
14887c478bd9Sstevel@tonic-gate 				 * the filter.
14897c478bd9Sstevel@tonic-gate 				 */
14907c478bd9Sstevel@tonic-gate 				nlmp = lml_rtld.lm_head;
14917c478bd9Sstevel@tonic-gate 				if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp,
14927c478bd9Sstevel@tonic-gate 				    (GPH_LDSO | GPH_FIRST | GPH_FILTEE))) == 0)
14937c478bd9Sstevel@tonic-gate 					nlmp = 0;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 				/*
14967c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
14977c478bd9Sstevel@tonic-gate 				 * recursion.
14987c478bd9Sstevel@tonic-gate 				 */
14997c478bd9Sstevel@tonic-gate 				if (nlmp && ghp)
15007c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate 				/*
15037c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  Ignore
15047c478bd9Sstevel@tonic-gate 				 * any return from the auditor, as we can't
15057c478bd9Sstevel@tonic-gate 				 * allow ignore filtering to ld.so.1, otherwise
15067c478bd9Sstevel@tonic-gate 				 * nothing is going to work.
15077c478bd9Sstevel@tonic-gate 				 */
150802ca3e02Srie 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
150902ca3e02Srie 				    LML_TFLG_AUD_OBJFILTER))
15107c478bd9Sstevel@tonic-gate 					(void) audit_objfilter(ilmp, filtees,
15117c478bd9Sstevel@tonic-gate 					    nlmp, 0);
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 			} else {
15147c478bd9Sstevel@tonic-gate 				Rej_desc	rej = { 0 };
151511a2bb38Srie 				Lm_cntl		*lmc;
151611a2bb38Srie 				Aliste		lmco;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 				/*
15197c478bd9Sstevel@tonic-gate 				 * Establish a new link-map control list from
15207c478bd9Sstevel@tonic-gate 				 * which to analyze any newly added objects.
15217c478bd9Sstevel@tonic-gate 				 */
152224a6229eSrie 				if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
152324a6229eSrie 					if ((lmc =
152424a6229eSrie 					    alist_append(&(lml->lm_lists), 0,
152524a6229eSrie 					    sizeof (Lm_cntl),
152624a6229eSrie 					    AL_CNT_LMLISTS)) == 0)
152724a6229eSrie 						return ((Sym *)0);
15287c478bd9Sstevel@tonic-gate 					lmco = (Aliste)((char *)lmc -
15297c478bd9Sstevel@tonic-gate 					    (char *)lml->lm_lists);
153024a6229eSrie 				} else {
153124a6229eSrie 					lmc = 0;
15327c478bd9Sstevel@tonic-gate 					lmco = ALO_DATA;
153324a6229eSrie 				}
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 				/*
15367c478bd9Sstevel@tonic-gate 				 * Load the filtee.
15377c478bd9Sstevel@tonic-gate 				 */
15387c478bd9Sstevel@tonic-gate 				if ((nlmp = load_path(lml, lmco, filtee, ilmp,
15397c478bd9Sstevel@tonic-gate 				    mode, FLG_RT_HANDLE, &ghp, 0, &rej)) == 0) {
15407c478bd9Sstevel@tonic-gate 					file_notfound(LIST(ilmp), filtee, ilmp,
15417c478bd9Sstevel@tonic-gate 					    FLG_RT_HANDLE, &rej);
15427c478bd9Sstevel@tonic-gate 					remove_rej(&rej);
15437c478bd9Sstevel@tonic-gate 				}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 				/*
15467c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
15477c478bd9Sstevel@tonic-gate 				 * recursion.
15487c478bd9Sstevel@tonic-gate 				 */
15497c478bd9Sstevel@tonic-gate 				if (nlmp && ghp) {
15507c478bd9Sstevel@tonic-gate 					ghp->gh_flags |= GPH_FILTEE;
15517c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
15527c478bd9Sstevel@tonic-gate 				}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 				/*
15557c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  A
15567c478bd9Sstevel@tonic-gate 				 * return of 0 indicates the auditor wishes to
15577c478bd9Sstevel@tonic-gate 				 * ignore this filtee.
15587c478bd9Sstevel@tonic-gate 				 */
15597c478bd9Sstevel@tonic-gate 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
15607c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)) {
15617c478bd9Sstevel@tonic-gate 					if (audit_objfilter(ilmp, filtees,
15627c478bd9Sstevel@tonic-gate 					    nlmp, 0) == 0) {
15637c478bd9Sstevel@tonic-gate 						audit = 1;
15647c478bd9Sstevel@tonic-gate 						nlmp = 0;
15657c478bd9Sstevel@tonic-gate 					}
15667c478bd9Sstevel@tonic-gate 				}
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 				/*
15697c478bd9Sstevel@tonic-gate 				 * Finish processing the objects associated with
15707c478bd9Sstevel@tonic-gate 				 * this request.  Create an association between
15717c478bd9Sstevel@tonic-gate 				 * this object and the originating filter to
15727c478bd9Sstevel@tonic-gate 				 * provide sufficient information to tear down
15737c478bd9Sstevel@tonic-gate 				 * this filtee if necessary.
15747c478bd9Sstevel@tonic-gate 				 */
15757c478bd9Sstevel@tonic-gate 				if (nlmp && ghp &&
15767c478bd9Sstevel@tonic-gate 				    ((analyze_lmc(lml, lmco, nlmp) == 0) ||
157702ca3e02Srie 				    (relocate_lmc(lml, lmco, ilmp, nlmp) == 0)))
15787c478bd9Sstevel@tonic-gate 					nlmp = 0;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 				/*
15817c478bd9Sstevel@tonic-gate 				 * If the filtee has been successfully
158202ca3e02Srie 				 * processed, then create an association
158302ca3e02Srie 				 * between the filter and filtee.  This
158402ca3e02Srie 				 * association provides sufficient information
158502ca3e02Srie 				 * to tear down the filter and filtee if
158602ca3e02Srie 				 * necessary.
15877c478bd9Sstevel@tonic-gate 				 */
158802ca3e02Srie 				DBG_CALL(Dbg_file_hdl_title(DBG_DEP_ADD));
158902ca3e02Srie 				if (nlmp && ghp &&
15907c478bd9Sstevel@tonic-gate 				    (hdl_add(ghp, ilmp, GPD_FILTER) == 0))
15917c478bd9Sstevel@tonic-gate 					nlmp = 0;
159211a2bb38Srie 
159311a2bb38Srie 				/*
159402ca3e02Srie 				 * If this filtee loading has failed, and we've
159502ca3e02Srie 				 * created a new link-map control list to which
159602ca3e02Srie 				 * this request has added objects, then remove
159702ca3e02Srie 				 * all the objects that have been associated to
159802ca3e02Srie 				 * this request.
159911a2bb38Srie 				 */
160002ca3e02Srie 				if ((nlmp == 0) && lmc && lmc->lc_head)
160102ca3e02Srie 					remove_lmc(lml, clmp, lmc, lmco, name);
160202ca3e02Srie 
160302ca3e02Srie 				/*
160402ca3e02Srie 				 * Remove any link-map control list that was
160502ca3e02Srie 				 * created.
160602ca3e02Srie 				 */
160702ca3e02Srie 				if (lmc)
160811a2bb38Srie 					remove_cntl(lml, lmco);
16097c478bd9Sstevel@tonic-gate 			}
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 			/*
16127c478bd9Sstevel@tonic-gate 			 * Generate a diagnostic if the filtee couldn't be
16137c478bd9Sstevel@tonic-gate 			 * loaded, null out the pnode entry, and continue
16147c478bd9Sstevel@tonic-gate 			 * the search.  Otherwise, retain this group handle
16157c478bd9Sstevel@tonic-gate 			 * for future symbol searches.
16167c478bd9Sstevel@tonic-gate 			 */
16177c478bd9Sstevel@tonic-gate 			if (nlmp == 0) {
16185aefb655Srie 				DBG_CALL(Dbg_file_filtee(lml, 0, filtee,
16195aefb655Srie 				    audit));
16207c478bd9Sstevel@tonic-gate 
162102ca3e02Srie 				pnp->p_info = 0;
16227c478bd9Sstevel@tonic-gate 				pnp->p_len = 0;
16237c478bd9Sstevel@tonic-gate 				continue;
16247c478bd9Sstevel@tonic-gate 			}
16257c478bd9Sstevel@tonic-gate 		}
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 		ghp = (Grp_hdl *)pnp->p_info;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 		/*
16307c478bd9Sstevel@tonic-gate 		 * If we're just here to trigger filtee loading skip the symbol
16317c478bd9Sstevel@tonic-gate 		 * lookup so we'll continue looking for additional filtees.
16327c478bd9Sstevel@tonic-gate 		 */
16337c478bd9Sstevel@tonic-gate 		if (name) {
16347c478bd9Sstevel@tonic-gate 			Grp_desc	*gdp;
16357c478bd9Sstevel@tonic-gate 			Sym		*sym = 0;
16367c478bd9Sstevel@tonic-gate 			Aliste		off;
16377c478bd9Sstevel@tonic-gate 			Slookup		sl = *slp;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 			sl.sl_flags |= LKUP_FIRST;
16407c478bd9Sstevel@tonic-gate 			any++;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 			/*
16437c478bd9Sstevel@tonic-gate 			 * Look for the symbol in the handles dependencies.
16447c478bd9Sstevel@tonic-gate 			 */
16457c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) {
16467c478bd9Sstevel@tonic-gate 				if ((gdp->gd_flags & GPD_AVAIL) == 0)
16477c478bd9Sstevel@tonic-gate 					continue;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 				/*
16507c478bd9Sstevel@tonic-gate 				 * If our parent is a dependency don't look at
16517c478bd9Sstevel@tonic-gate 				 * it (otherwise we are in a recursive loop).
16527c478bd9Sstevel@tonic-gate 				 * This situation can occur with auxiliary
16537c478bd9Sstevel@tonic-gate 				 * filters if the filtee has a dependency on the
16547c478bd9Sstevel@tonic-gate 				 * filter.  This dependency isn't necessary as
16557c478bd9Sstevel@tonic-gate 				 * auxiliary filters are opened RTLD_PARENT, but
16567c478bd9Sstevel@tonic-gate 				 * users may still unknowingly add an explicit
16577c478bd9Sstevel@tonic-gate 				 * dependency to the parent.
16587c478bd9Sstevel@tonic-gate 				 */
16597c478bd9Sstevel@tonic-gate 				if ((sl.sl_imap = gdp->gd_depend) == ilmp)
16607c478bd9Sstevel@tonic-gate 					continue;
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 				if (((sym = SYMINTP(sl.sl_imap)(&sl, dlmp,
16637c478bd9Sstevel@tonic-gate 				    binfo)) != 0) ||
16647c478bd9Sstevel@tonic-gate 				    (ghp->gh_flags & GPH_FIRST))
16657c478bd9Sstevel@tonic-gate 					break;
16667c478bd9Sstevel@tonic-gate 			}
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 			/*
166902ca3e02Srie 			 * If a symbol has been found, indicate the binding
167002ca3e02Srie 			 * and return the symbol.
16717c478bd9Sstevel@tonic-gate 			 */
16727c478bd9Sstevel@tonic-gate 			if (sym) {
16737c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_FILTEE;
16747c478bd9Sstevel@tonic-gate 				return (sym);
16757c478bd9Sstevel@tonic-gate 			}
16767c478bd9Sstevel@tonic-gate 		}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		/*
16797c478bd9Sstevel@tonic-gate 		 * If this object is tagged to terminate filtee processing we're
16807c478bd9Sstevel@tonic-gate 		 * done.
16817c478bd9Sstevel@tonic-gate 		 */
16825aefb655Srie 		if (FLAGS1(ghp->gh_ownlmp) & FL1_RT_ENDFILTE)
16837c478bd9Sstevel@tonic-gate 			break;
16847c478bd9Sstevel@tonic-gate 	}
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	/*
16877c478bd9Sstevel@tonic-gate 	 * If we're just here to trigger filtee loading then we're done.
16887c478bd9Sstevel@tonic-gate 	 */
16897c478bd9Sstevel@tonic-gate 	if (name == 0)
16907c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	/*
16937c478bd9Sstevel@tonic-gate 	 * If no filtees have been found for a filter, clean up any Pnode
16947c478bd9Sstevel@tonic-gate 	 * structures and disable their search completely.  For auxiliary
16957c478bd9Sstevel@tonic-gate 	 * filters we can reselect the symbol search function so that we never
16967c478bd9Sstevel@tonic-gate 	 * enter this routine again for this object.  For standard filters we
16977c478bd9Sstevel@tonic-gate 	 * use the null symbol routine.
16987c478bd9Sstevel@tonic-gate 	 */
16997c478bd9Sstevel@tonic-gate 	if (any == 0) {
17007c478bd9Sstevel@tonic-gate 		remove_pnode((Pnode *)dip->di_info);
17017c478bd9Sstevel@tonic-gate 		elf_disable_filtee(ilmp, dip);
17027c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17037c478bd9Sstevel@tonic-gate 	}
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate /*
17097c478bd9Sstevel@tonic-gate  * Focal point for disabling error messages for auxiliary filters.  As an
17107c478bd9Sstevel@tonic-gate  * auxiliary filter allows for filtee use, but provides a fallback should a
17117c478bd9Sstevel@tonic-gate  * filtee not exist (or fail to load), any errors generated as a consequence of
17127c478bd9Sstevel@tonic-gate  * trying to load the filtees are typically suppressed.  Setting RT_FL_SILENCERR
17137c478bd9Sstevel@tonic-gate  * suppresses errors generated by eprint(), but insures a debug diagnostic is
17147c478bd9Sstevel@tonic-gate  * produced.  ldd(1) employs printf(), and here, the selection of whether to
17157c478bd9Sstevel@tonic-gate  * print a diagnostic in regards to auxiliary filters is a little more complex.
17167c478bd9Sstevel@tonic-gate  *
17177c478bd9Sstevel@tonic-gate  *   .	The determination of whether to produce an ldd message, or a fatal
17187c478bd9Sstevel@tonic-gate  *	error message is driven by LML_FLG_TRC_ENABLE.
17197c478bd9Sstevel@tonic-gate  *   .	More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN,
17207c478bd9Sstevel@tonic-gate  *	(ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s),
17217c478bd9Sstevel@tonic-gate  *	and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u).
17227c478bd9Sstevel@tonic-gate  *
17237c478bd9Sstevel@tonic-gate  *   .	If the calling object is lddstub, then several classes of message are
17247c478bd9Sstevel@tonic-gate  *	suppressed.  The user isn't trying to diagnose lddstub, this is simply
17257c478bd9Sstevel@tonic-gate  *	a stub executable employed to preload a user specified library against.
17267c478bd9Sstevel@tonic-gate  *
17277c478bd9Sstevel@tonic-gate  *   .	If RT_FL_SILENCERR is in effect then any generic ldd() messages should
17287c478bd9Sstevel@tonic-gate  *	be suppressed.  All detailed ldd messages should still be produced.
17297c478bd9Sstevel@tonic-gate  */
17307c478bd9Sstevel@tonic-gate Sym *
17317c478bd9Sstevel@tonic-gate elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	Sym	*sym;
17347c478bd9Sstevel@tonic-gate 	Dyninfo	*dip = &DYNINFO(slp->sl_imap)[ndx];
17357c478bd9Sstevel@tonic-gate 	int	silent = 0;
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	/*
17387c478bd9Sstevel@tonic-gate 	 * Make sure this entry is still acting as a filter.  We may have tried
17397c478bd9Sstevel@tonic-gate 	 * to process this previously, and disabled it if the filtee couldn't
17407c478bd9Sstevel@tonic-gate 	 * be processed.  However, other entries may provide different filtees
17417c478bd9Sstevel@tonic-gate 	 * that are yet to be completed.
17427c478bd9Sstevel@tonic-gate 	 */
17437c478bd9Sstevel@tonic-gate 	if (dip->di_flags == 0)
17447c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate 	/*
17477c478bd9Sstevel@tonic-gate 	 * Indicate whether an error message is required should this filtee not
17487c478bd9Sstevel@tonic-gate 	 * be found, based on the type of filter.
17497c478bd9Sstevel@tonic-gate 	 */
17507c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_AUXFLTR) &&
17517c478bd9Sstevel@tonic-gate 	    ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) {
17527c478bd9Sstevel@tonic-gate 		rtld_flags |= RT_FL_SILENCERR;
17537c478bd9Sstevel@tonic-gate 		silent = 1;
17547c478bd9Sstevel@tonic-gate 	}
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	sym = _elf_lookup_filtee(slp, dlmp, binfo, ndx);
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	if (silent)
17597c478bd9Sstevel@tonic-gate 		rtld_flags &= ~RT_FL_SILENCERR;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	return (sym);
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate /*
17657c478bd9Sstevel@tonic-gate  * Compute the elf hash value (as defined in the ELF access library).
17667c478bd9Sstevel@tonic-gate  * The form of the hash table is:
17677c478bd9Sstevel@tonic-gate  *
17687c478bd9Sstevel@tonic-gate  *	|--------------|
17697c478bd9Sstevel@tonic-gate  *	| # of buckets |
17707c478bd9Sstevel@tonic-gate  *	|--------------|
17717c478bd9Sstevel@tonic-gate  *	| # of chains  |
17727c478bd9Sstevel@tonic-gate  *	|--------------|
17737c478bd9Sstevel@tonic-gate  *	|   bucket[]   |
17747c478bd9Sstevel@tonic-gate  *	|--------------|
17757c478bd9Sstevel@tonic-gate  *	|   chain[]    |
17767c478bd9Sstevel@tonic-gate  *	|--------------|
17777c478bd9Sstevel@tonic-gate  */
17787c478bd9Sstevel@tonic-gate ulong_t
17797c478bd9Sstevel@tonic-gate elf_hash(const char *name)
17807c478bd9Sstevel@tonic-gate {
17817c478bd9Sstevel@tonic-gate 	uint_t	hval = 0;
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	while (*name) {
17847c478bd9Sstevel@tonic-gate 		uint_t	g;
17857c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *name++;
17867c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
17877c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
17887c478bd9Sstevel@tonic-gate 		hval &= ~g;
17897c478bd9Sstevel@tonic-gate 	}
17907c478bd9Sstevel@tonic-gate 	return ((ulong_t)hval);
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate /*
17947c478bd9Sstevel@tonic-gate  * If flag argument has LKUP_SPEC set, we treat undefined symbols of type
17957c478bd9Sstevel@tonic-gate  * function specially in the executable - if they have a value, even though
17967c478bd9Sstevel@tonic-gate  * undefined, we use that value.  This allows us to associate all references
17977c478bd9Sstevel@tonic-gate  * to a function's address to a single place in the process: the plt entry
17987c478bd9Sstevel@tonic-gate  * for that function in the executable.  Calls to lookup from plt binding
17997c478bd9Sstevel@tonic-gate  * routines do NOT set LKUP_SPEC in the flag.
18007c478bd9Sstevel@tonic-gate  */
18017c478bd9Sstevel@tonic-gate Sym *
18027c478bd9Sstevel@tonic-gate elf_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
18037c478bd9Sstevel@tonic-gate {
18047c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
18057c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
18067c478bd9Sstevel@tonic-gate 	ulong_t		hash = slp->sl_hash;
18077c478bd9Sstevel@tonic-gate 	uint_t		ndx, htmp, buckets, *chainptr;
18087c478bd9Sstevel@tonic-gate 	Sym		*sym, *symtabptr;
18097c478bd9Sstevel@tonic-gate 	char		*strtabptr, *strtabname;
18107c478bd9Sstevel@tonic-gate 	uint_t		flags1;
18117c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
18127c478bd9Sstevel@tonic-gate 
1813660acd81Srie 	/*
1814660acd81Srie 	 * If we're only here to establish a symbols index, skip the diagnostic
1815660acd81Srie 	 * used to trace a symbol search.
1816660acd81Srie 	 */
18175aefb655Srie 	if ((slp->sl_flags & LKUP_SYMNDX) == 0)
18185aefb655Srie 		DBG_CALL(Dbg_syms_lookup(ilmp, name, MSG_ORIG(MSG_STR_ELF)));
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	if (HASH(ilmp) == 0)
18217c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	buckets = HASH(ilmp)[0];
18247c478bd9Sstevel@tonic-gate 	/* LINTED */
18257c478bd9Sstevel@tonic-gate 	htmp = (uint_t)hash % buckets;
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	/*
18287c478bd9Sstevel@tonic-gate 	 * Get the first symbol on hash chain and initialize the string
18297c478bd9Sstevel@tonic-gate 	 * and symbol table pointers.
18307c478bd9Sstevel@tonic-gate 	 */
18317c478bd9Sstevel@tonic-gate 	if ((ndx = HASH(ilmp)[htmp + 2]) == 0)
18327c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	chainptr = HASH(ilmp) + 2 + buckets;
18357c478bd9Sstevel@tonic-gate 	strtabptr = STRTAB(ilmp);
18367c478bd9Sstevel@tonic-gate 	symtabptr = SYMTAB(ilmp);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	while (ndx) {
18397c478bd9Sstevel@tonic-gate 		sym = symtabptr + ndx;
18407c478bd9Sstevel@tonic-gate 		strtabname = strtabptr + sym->st_name;
18417c478bd9Sstevel@tonic-gate 
18427c478bd9Sstevel@tonic-gate 		/*
18437c478bd9Sstevel@tonic-gate 		 * Compare the symbol found with the name required.  If the
18447c478bd9Sstevel@tonic-gate 		 * names don't match continue with the next hash entry.
18457c478bd9Sstevel@tonic-gate 		 */
18467c478bd9Sstevel@tonic-gate 		if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
18477c478bd9Sstevel@tonic-gate 			if ((ndx = chainptr[ndx]) != 0)
18487c478bd9Sstevel@tonic-gate 				continue;
18497c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
18507c478bd9Sstevel@tonic-gate 		}
18517c478bd9Sstevel@tonic-gate 
18523b41b08bSab 		/*
18533b41b08bSab 		 * To accomodate objects built with the GNU ld, we quietly
18543b41b08bSab 		 * ignore symbols with a version that is outside the range
18553b41b08bSab 		 * of the valid versions supplied by the file. See the
18563b41b08bSab 		 * comment that accompanies the VERSYM_INVALID macro in libld.h
18573b41b08bSab 		 * for additional details.
18583b41b08bSab 		 */
18593b41b08bSab 		if (VERNDX_INVALID(sym->st_shndx, VERDEFNUM(ilmp),
18603b41b08bSab 		    VERSYM(ilmp), VERSYM(ilmp)[ndx])) {
18613b41b08bSab 			DBG_CALL(Dbg_syms_ignore_badver(ilmp, name,
18623b41b08bSab 			    ndx, VERSYM(ilmp)[ndx]));
18633b41b08bSab 			if ((ndx = chainptr[ndx]) != 0)
18643b41b08bSab 				continue;
18653b41b08bSab 			return ((Sym *)0);
18663b41b08bSab 		}
18673b41b08bSab 
1868660acd81Srie 		/*
1869660acd81Srie 		 * If we're only here to establish a symbols index, we're done.
1870660acd81Srie 		 */
1871660acd81Srie 		if (slp->sl_flags & LKUP_SYMNDX)
1872660acd81Srie 			return (sym);
1873660acd81Srie 
18747c478bd9Sstevel@tonic-gate 		/*
18757c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is defined, return the
18767c478bd9Sstevel@tonic-gate 		 * symbol pointer and the link map in which it was found.
18777c478bd9Sstevel@tonic-gate 		 */
18787c478bd9Sstevel@tonic-gate 		if (sym->st_shndx != SHN_UNDEF) {
18797c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
18807c478bd9Sstevel@tonic-gate 			*binfo |= DBG_BINFO_FOUND;
18819a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
18829a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
18839a411307Srie 			    is_sym_interposer(ilmp, sym)))
18847c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
18857c478bd9Sstevel@tonic-gate 			break;
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 		/*
18887c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is undefined, the
18897c478bd9Sstevel@tonic-gate 		 * symbol type is a function, and the value of the symbol
18907c478bd9Sstevel@tonic-gate 		 * is non zero, then this is a special case.  This allows
18917c478bd9Sstevel@tonic-gate 		 * the resolution of a function address to the plt[] entry.
18927c478bd9Sstevel@tonic-gate 		 * See SPARC ABI, Dynamic Linking, Function Addresses for
18937c478bd9Sstevel@tonic-gate 		 * more details.
18947c478bd9Sstevel@tonic-gate 		 */
1895660acd81Srie 		} else if ((slp->sl_flags & LKUP_SPEC) &&
18967c478bd9Sstevel@tonic-gate 		    (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) &&
18977c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
18987c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
18997c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR);
19009a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
19019a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
19029a411307Srie 			    is_sym_interposer(ilmp, sym)))
19037c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
19047c478bd9Sstevel@tonic-gate 			return (sym);
19057c478bd9Sstevel@tonic-gate 		}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 		/*
19087c478bd9Sstevel@tonic-gate 		 * Undefined symbol.
19097c478bd9Sstevel@tonic-gate 		 */
19107c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
19117c478bd9Sstevel@tonic-gate 	}
19127c478bd9Sstevel@tonic-gate 
19137c478bd9Sstevel@tonic-gate 	/*
19147c478bd9Sstevel@tonic-gate 	 * We've found a match.  Determine if the defining object contains
19157c478bd9Sstevel@tonic-gate 	 * symbol binding information.
19167c478bd9Sstevel@tonic-gate 	 */
19177c478bd9Sstevel@tonic-gate 	if ((sip = SYMINFO(ilmp)) != 0)
19189039eeafSab 		sip += ndx;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	/*
19217c478bd9Sstevel@tonic-gate 	 * If this is a direct binding request, but the symbol definition has
19227c478bd9Sstevel@tonic-gate 	 * disabled directly binding to it (presumably because the symbol
19237c478bd9Sstevel@tonic-gate 	 * definition has been changed since the referring object was built),
19247c478bd9Sstevel@tonic-gate 	 * indicate this failure so that the caller can fall back to a standard
19257c478bd9Sstevel@tonic-gate 	 * symbol search.  Clear any debug binding information for cleanliness.
19267c478bd9Sstevel@tonic-gate 	 */
19277c478bd9Sstevel@tonic-gate 	if (sip && (slp->sl_flags & LKUP_DIRECT) &&
19287c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) {
19297c478bd9Sstevel@tonic-gate 		*binfo |= BINFO_DIRECTDIS;
19307c478bd9Sstevel@tonic-gate 		*binfo &= ~DBG_BINFO_MSK;
19317c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
19327c478bd9Sstevel@tonic-gate 	}
19337c478bd9Sstevel@tonic-gate 
19347c478bd9Sstevel@tonic-gate 	/*
19357c478bd9Sstevel@tonic-gate 	 * Determine whether this object is acting as a filter.
19367c478bd9Sstevel@tonic-gate 	 */
19377c478bd9Sstevel@tonic-gate 	if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0)
19387c478bd9Sstevel@tonic-gate 		return (sym);
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate 	/*
19417c478bd9Sstevel@tonic-gate 	 * Determine if this object offers per-symbol filtering, and if so,
19427c478bd9Sstevel@tonic-gate 	 * whether this symbol references a filtee.
19437c478bd9Sstevel@tonic-gate 	 */
19447c478bd9Sstevel@tonic-gate 	if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) {
19457c478bd9Sstevel@tonic-gate 		/*
19467c478bd9Sstevel@tonic-gate 		 * If this is a standard filter reference, and no standard
19477c478bd9Sstevel@tonic-gate 		 * filtees remain to be inspected, we're done.  If this is an
19487c478bd9Sstevel@tonic-gate 		 * auxiliary filter reference, and no auxiliary filtees remain,
19497c478bd9Sstevel@tonic-gate 		 * we'll fall through in case any object filtering is available.
19507c478bd9Sstevel@tonic-gate 		 */
19517c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) &&
19527c478bd9Sstevel@tonic-gate 		    (SYMSFLTRCNT(ilmp) == 0))
19537c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) ||
19567c478bd9Sstevel@tonic-gate 		    ((sip->si_flags & SYMINFO_FLG_AUXILIARY) &&
19577c478bd9Sstevel@tonic-gate 		    SYMAFLTRCNT(ilmp))) {
19587c478bd9Sstevel@tonic-gate 			Sym *	fsym;
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 			/*
19617c478bd9Sstevel@tonic-gate 			 * This symbol has an associated filtee.  Lookup the
19627c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
19637c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
19647c478bd9Sstevel@tonic-gate 			 * filter, return an error, otherwise fall through to
19657c478bd9Sstevel@tonic-gate 			 * catch any object filtering that may be available.
19667c478bd9Sstevel@tonic-gate 			 */
19677c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
19687c478bd9Sstevel@tonic-gate 			    sip->si_boundto)) != 0)
19697c478bd9Sstevel@tonic-gate 				return (fsym);
19707c478bd9Sstevel@tonic-gate 			if (sip->si_flags & SYMINFO_FLG_FILTER)
19717c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
19727c478bd9Sstevel@tonic-gate 		}
19737c478bd9Sstevel@tonic-gate 	}
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 	/*
19767c478bd9Sstevel@tonic-gate 	 * Determine if this object provides global filtering.
19777c478bd9Sstevel@tonic-gate 	 */
19787c478bd9Sstevel@tonic-gate 	if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) {
19797c478bd9Sstevel@tonic-gate 		Sym *	fsym;
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) {
19827c478bd9Sstevel@tonic-gate 			/*
19837c478bd9Sstevel@tonic-gate 			 * This object has an associated filtee.  Lookup the
19847c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
19857c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
19867c478bd9Sstevel@tonic-gate 			 * filter, return and error, otherwise return the symbol
19877c478bd9Sstevel@tonic-gate 			 * within the filter itself.
19887c478bd9Sstevel@tonic-gate 			 */
19897c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
19907c478bd9Sstevel@tonic-gate 			    OBJFLTRNDX(ilmp))) != 0)
19917c478bd9Sstevel@tonic-gate 				return (fsym);
19927c478bd9Sstevel@tonic-gate 		}
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 		if (flags1 & FL1_RT_OBJSFLTR)
19957c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
19967c478bd9Sstevel@tonic-gate 	}
19977c478bd9Sstevel@tonic-gate 	return (sym);
19987c478bd9Sstevel@tonic-gate }
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate /*
20017c478bd9Sstevel@tonic-gate  * Create a new Rt_map structure for an ELF object and initialize
20027c478bd9Sstevel@tonic-gate  * all values.
20037c478bd9Sstevel@tonic-gate  */
20047c478bd9Sstevel@tonic-gate Rt_map *
20057c478bd9Sstevel@tonic-gate elf_new_lm(Lm_list *lml, const char *pname, const char *oname, Dyn *ld,
20067c478bd9Sstevel@tonic-gate     ulong_t addr, ulong_t etext, Aliste lmco, ulong_t msize, ulong_t entry,
20077c478bd9Sstevel@tonic-gate     ulong_t paddr, ulong_t padimsize, Mmap *mmaps, uint_t mmapcnt)
20087c478bd9Sstevel@tonic-gate {
20097c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
20107c478bd9Sstevel@tonic-gate 	ulong_t		base, fltr = 0, audit = 0, cfile = 0, crle = 0;
20117c478bd9Sstevel@tonic-gate 	Xword		rpath = 0;
20127c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr = (Ehdr *)addr;
20137c478bd9Sstevel@tonic-gate 
20145aefb655Srie 	DBG_CALL(Dbg_file_elf(lml, pname, (ulong_t)ld, addr, msize, entry,
20155aefb655Srie 	    lml->lm_lmidstr, lmco));
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	/*
20187c478bd9Sstevel@tonic-gate 	 * Allocate space for the link-map and private elf information.  Once
20197c478bd9Sstevel@tonic-gate 	 * these are allocated and initialized, we can use remove_so(0, lmp) to
20207c478bd9Sstevel@tonic-gate 	 * tear down the link-map should any failures occur.
20217c478bd9Sstevel@tonic-gate 	 */
20227c478bd9Sstevel@tonic-gate 	if ((lmp = calloc(sizeof (Rt_map), 1)) == 0)
20237c478bd9Sstevel@tonic-gate 		return (0);
20247c478bd9Sstevel@tonic-gate 	if ((ELFPRV(lmp) = calloc(sizeof (Rt_elfp), 1)) == 0) {
20257c478bd9Sstevel@tonic-gate 		free(lmp);
20267c478bd9Sstevel@tonic-gate 		return (0);
20277c478bd9Sstevel@tonic-gate 	}
20287c478bd9Sstevel@tonic-gate 
20297c478bd9Sstevel@tonic-gate 	/*
20307c478bd9Sstevel@tonic-gate 	 * All fields not filled in were set to 0 by calloc.
20317c478bd9Sstevel@tonic-gate 	 */
20327c478bd9Sstevel@tonic-gate 	ORIGNAME(lmp) = PATHNAME(lmp) = NAME(lmp) = (char *)pname;
20337c478bd9Sstevel@tonic-gate 	DYN(lmp) = ld;
20347c478bd9Sstevel@tonic-gate 	ADDR(lmp) = addr;
20357c478bd9Sstevel@tonic-gate 	MSIZE(lmp) = msize;
20367c478bd9Sstevel@tonic-gate 	ENTRY(lmp) = (Addr)entry;
20377c478bd9Sstevel@tonic-gate 	SYMINTP(lmp) = elf_find_sym;
20387c478bd9Sstevel@tonic-gate 	ETEXT(lmp) = etext;
20397c478bd9Sstevel@tonic-gate 	FCT(lmp) = &elf_fct;
20407c478bd9Sstevel@tonic-gate 	LIST(lmp) = lml;
20417c478bd9Sstevel@tonic-gate 	PADSTART(lmp) = paddr;
20427c478bd9Sstevel@tonic-gate 	PADIMLEN(lmp) = padimsize;
20437c478bd9Sstevel@tonic-gate 	THREADID(lmp) = rt_thr_self();
20447c478bd9Sstevel@tonic-gate 	OBJFLTRNDX(lmp) = FLTR_DISABLED;
2045dffec89cSrie 	SORTVAL(lmp) = -1;
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	MMAPS(lmp) = mmaps;
20487c478bd9Sstevel@tonic-gate 	MMAPCNT(lmp) = mmapcnt;
20497c478bd9Sstevel@tonic-gate 	ASSERT(mmapcnt != 0);
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	/*
20527c478bd9Sstevel@tonic-gate 	 * If this is a shared object, add the base address to each address.
20537c478bd9Sstevel@tonic-gate 	 * if this is an executable, use address as is.
20547c478bd9Sstevel@tonic-gate 	 */
20557c478bd9Sstevel@tonic-gate 	if (ehdr->e_type == ET_EXEC) {
20567c478bd9Sstevel@tonic-gate 		base = 0;
20577c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_FIXED;
20587c478bd9Sstevel@tonic-gate 	} else
20597c478bd9Sstevel@tonic-gate 		base = addr;
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	/*
20627c478bd9Sstevel@tonic-gate 	 * Fill in rest of the link map entries with information from the file's
20637c478bd9Sstevel@tonic-gate 	 * dynamic structure.
20647c478bd9Sstevel@tonic-gate 	 */
20657c478bd9Sstevel@tonic-gate 	if (ld) {
206610a4fa49Srie 		uint_t		dyncnt = 0;
206710a4fa49Srie 		Xword		pltpadsz = 0;
206810a4fa49Srie 		Rti_desc	*rti;
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 		/* CSTYLED */
20717c478bd9Sstevel@tonic-gate 		for ( ; ld->d_tag != DT_NULL; ++ld, dyncnt++) {
20727c478bd9Sstevel@tonic-gate 			switch ((Xword)ld->d_tag) {
20737c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
20747c478bd9Sstevel@tonic-gate 				SYMTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
20757c478bd9Sstevel@tonic-gate 				break;
20769039eeafSab 			case DT_SUNW_SYMTAB:
20779039eeafSab 				SUNWSYMTAB(lmp) =
20789039eeafSab 				    (void *)(ld->d_un.d_ptr + base);
20799039eeafSab 				break;
20809039eeafSab 			case DT_SUNW_SYMSZ:
20819039eeafSab 				SUNWSYMSZ(lmp) = ld->d_un.d_val;
20829039eeafSab 				break;
20837c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
20847c478bd9Sstevel@tonic-gate 				STRTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
20857c478bd9Sstevel@tonic-gate 				break;
20867c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
20877c478bd9Sstevel@tonic-gate 				SYMENT(lmp) = ld->d_un.d_val;
20887c478bd9Sstevel@tonic-gate 				break;
20897c478bd9Sstevel@tonic-gate 			case DT_FEATURE_1:
20907c478bd9Sstevel@tonic-gate 				ld->d_un.d_val |= DTF_1_PARINIT;
20917c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DTF_1_CONFEXP)
20927c478bd9Sstevel@tonic-gate 					crle = 1;
20937c478bd9Sstevel@tonic-gate 				break;
20947c478bd9Sstevel@tonic-gate 			case DT_MOVESZ:
20957c478bd9Sstevel@tonic-gate 				MOVESZ(lmp) = ld->d_un.d_val;
20967c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_MOVE;
20977c478bd9Sstevel@tonic-gate 				break;
20987c478bd9Sstevel@tonic-gate 			case DT_MOVEENT:
20997c478bd9Sstevel@tonic-gate 				MOVEENT(lmp) = ld->d_un.d_val;
21007c478bd9Sstevel@tonic-gate 				break;
21017c478bd9Sstevel@tonic-gate 			case DT_MOVETAB:
21027c478bd9Sstevel@tonic-gate 				MOVETAB(lmp) = (void *)(ld->d_un.d_ptr + base);
21037c478bd9Sstevel@tonic-gate 				break;
21047c478bd9Sstevel@tonic-gate 			case DT_REL:
21057c478bd9Sstevel@tonic-gate 			case DT_RELA:
21067c478bd9Sstevel@tonic-gate 				/*
21077c478bd9Sstevel@tonic-gate 				 * At this time we can only handle 1 type of
21087c478bd9Sstevel@tonic-gate 				 * relocation per object.
21097c478bd9Sstevel@tonic-gate 				 */
21107c478bd9Sstevel@tonic-gate 				REL(lmp) = (void *)(ld->d_un.d_ptr + base);
21117c478bd9Sstevel@tonic-gate 				break;
21127c478bd9Sstevel@tonic-gate 			case DT_RELSZ:
21137c478bd9Sstevel@tonic-gate 			case DT_RELASZ:
21147c478bd9Sstevel@tonic-gate 				RELSZ(lmp) = ld->d_un.d_val;
21157c478bd9Sstevel@tonic-gate 				break;
21167c478bd9Sstevel@tonic-gate 			case DT_RELENT:
21177c478bd9Sstevel@tonic-gate 			case DT_RELAENT:
21187c478bd9Sstevel@tonic-gate 				RELENT(lmp) = ld->d_un.d_val;
21197c478bd9Sstevel@tonic-gate 				break;
21207c478bd9Sstevel@tonic-gate 			case DT_RELCOUNT:
21217c478bd9Sstevel@tonic-gate 			case DT_RELACOUNT:
21227c478bd9Sstevel@tonic-gate 				RELACOUNT(lmp) = (uint_t)ld->d_un.d_val;
21237c478bd9Sstevel@tonic-gate 				break;
21247c478bd9Sstevel@tonic-gate 			case DT_TEXTREL:
21257c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_TEXTREL;
21267c478bd9Sstevel@tonic-gate 				break;
21277c478bd9Sstevel@tonic-gate 			case DT_HASH:
21287c478bd9Sstevel@tonic-gate 				HASH(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
21297c478bd9Sstevel@tonic-gate 				break;
21307c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
21317c478bd9Sstevel@tonic-gate 				PLTGOT(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
21327c478bd9Sstevel@tonic-gate 				break;
21337c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
21347c478bd9Sstevel@tonic-gate 				PLTRELSZ(lmp) = ld->d_un.d_val;
21357c478bd9Sstevel@tonic-gate 				break;
21367c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
21377c478bd9Sstevel@tonic-gate 				JMPREL(lmp) = (void *)(ld->d_un.d_ptr + base);
21387c478bd9Sstevel@tonic-gate 				break;
21397c478bd9Sstevel@tonic-gate 			case DT_INIT:
21405b59e4caSab 				if (ld->d_un.d_ptr != NULL)
21415b59e4caSab 					INIT(lmp) =
21425b59e4caSab 					    (void (*)())(ld->d_un.d_ptr + base);
21437c478bd9Sstevel@tonic-gate 				break;
21447c478bd9Sstevel@tonic-gate 			case DT_FINI:
21455b59e4caSab 				if (ld->d_un.d_ptr != NULL)
21465b59e4caSab 					FINI(lmp) =
21475b59e4caSab 					    (void (*)())(ld->d_un.d_ptr + base);
21487c478bd9Sstevel@tonic-gate 				break;
21497c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAY:
21507c478bd9Sstevel@tonic-gate 				INITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21517c478bd9Sstevel@tonic-gate 				    base);
21527c478bd9Sstevel@tonic-gate 				break;
21537c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAYSZ:
21547c478bd9Sstevel@tonic-gate 				INITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21557c478bd9Sstevel@tonic-gate 				break;
21567c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAY:
21577c478bd9Sstevel@tonic-gate 				FINIARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21587c478bd9Sstevel@tonic-gate 				    base);
21597c478bd9Sstevel@tonic-gate 				break;
21607c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAYSZ:
21617c478bd9Sstevel@tonic-gate 				FINIARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21627c478bd9Sstevel@tonic-gate 				break;
21637c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAY:
21647c478bd9Sstevel@tonic-gate 				PREINITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21657c478bd9Sstevel@tonic-gate 				    base);
21667c478bd9Sstevel@tonic-gate 				break;
21677c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAYSZ:
21687c478bd9Sstevel@tonic-gate 				PREINITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21697c478bd9Sstevel@tonic-gate 				break;
21707c478bd9Sstevel@tonic-gate 			case DT_RPATH:
21717c478bd9Sstevel@tonic-gate 			case DT_RUNPATH:
21727c478bd9Sstevel@tonic-gate 				rpath = ld->d_un.d_val;
21737c478bd9Sstevel@tonic-gate 				break;
21747c478bd9Sstevel@tonic-gate 			case DT_FILTER:
21757c478bd9Sstevel@tonic-gate 				fltr = ld->d_un.d_val;
21767c478bd9Sstevel@tonic-gate 				OBJFLTRNDX(lmp) = dyncnt;
21777c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJSFLTR;
21787c478bd9Sstevel@tonic-gate 				break;
21797c478bd9Sstevel@tonic-gate 			case DT_AUXILIARY:
21807c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
21817c478bd9Sstevel@tonic-gate 					fltr = ld->d_un.d_val;
21827c478bd9Sstevel@tonic-gate 					OBJFLTRNDX(lmp) = dyncnt;
21837c478bd9Sstevel@tonic-gate 				}
21847c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJAFLTR;
21857c478bd9Sstevel@tonic-gate 				break;
21867c478bd9Sstevel@tonic-gate 			case DT_SUNW_FILTER:
21877c478bd9Sstevel@tonic-gate 				SYMSFLTRCNT(lmp)++;
21887c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMSFLTR;
21897c478bd9Sstevel@tonic-gate 				break;
21907c478bd9Sstevel@tonic-gate 			case DT_SUNW_AUXILIARY:
21917c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
21927c478bd9Sstevel@tonic-gate 					SYMAFLTRCNT(lmp)++;
21937c478bd9Sstevel@tonic-gate 				}
21947c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMAFLTR;
21957c478bd9Sstevel@tonic-gate 				break;
21967c478bd9Sstevel@tonic-gate 			case DT_DEPAUDIT:
21977c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUDIT))
21987c478bd9Sstevel@tonic-gate 					audit = ld->d_un.d_val;
21997c478bd9Sstevel@tonic-gate 				break;
22007c478bd9Sstevel@tonic-gate 			case DT_CONFIG:
22017c478bd9Sstevel@tonic-gate 				cfile = ld->d_un.d_val;
22027c478bd9Sstevel@tonic-gate 				break;
22037c478bd9Sstevel@tonic-gate 			case DT_DEBUG:
22047c478bd9Sstevel@tonic-gate 				/*
22057c478bd9Sstevel@tonic-gate 				 * DT_DEBUG entries are only created in
22067c478bd9Sstevel@tonic-gate 				 * dynamic objects that require an interpretor
22077c478bd9Sstevel@tonic-gate 				 * (ie. all dynamic executables and some shared
22087c478bd9Sstevel@tonic-gate 				 * objects), and provide for a hand-shake with
22097c478bd9Sstevel@tonic-gate 				 * debuggers.  This entry is initialized to
22107c478bd9Sstevel@tonic-gate 				 * zero by the link-editor.  If a debugger has
22117c478bd9Sstevel@tonic-gate 				 * us and updated this entry set the debugger
22127c478bd9Sstevel@tonic-gate 				 * flag, and finish initializing the debugging
22137c478bd9Sstevel@tonic-gate 				 * structure (see setup() also).  Switch off any
22147c478bd9Sstevel@tonic-gate 				 * configuration object use as most debuggers
22157c478bd9Sstevel@tonic-gate 				 * can't handle fixed dynamic executables as
22167c478bd9Sstevel@tonic-gate 				 * dependencies, and we can't handle requests
22177c478bd9Sstevel@tonic-gate 				 * like object padding for alternative objects.
22187c478bd9Sstevel@tonic-gate 				 */
22197c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_ptr)
22207c478bd9Sstevel@tonic-gate 					rtld_flags |=
22217c478bd9Sstevel@tonic-gate 					    (RT_FL_DEBUGGER | RT_FL_NOOBJALT);
22227c478bd9Sstevel@tonic-gate 				ld->d_un.d_ptr = (Addr)&r_debug;
22237c478bd9Sstevel@tonic-gate 				break;
22247c478bd9Sstevel@tonic-gate 			case DT_VERNEED:
22257c478bd9Sstevel@tonic-gate 				VERNEED(lmp) = (Verneed *)(ld->d_un.d_ptr +
22267c478bd9Sstevel@tonic-gate 				    base);
22277c478bd9Sstevel@tonic-gate 				break;
22287c478bd9Sstevel@tonic-gate 			case DT_VERNEEDNUM:
22297c478bd9Sstevel@tonic-gate 				/* LINTED */
22307c478bd9Sstevel@tonic-gate 				VERNEEDNUM(lmp) = (int)ld->d_un.d_val;
22317c478bd9Sstevel@tonic-gate 				break;
22327c478bd9Sstevel@tonic-gate 			case DT_VERDEF:
22337c478bd9Sstevel@tonic-gate 				VERDEF(lmp) = (Verdef *)(ld->d_un.d_ptr + base);
22347c478bd9Sstevel@tonic-gate 				break;
22357c478bd9Sstevel@tonic-gate 			case DT_VERDEFNUM:
22367c478bd9Sstevel@tonic-gate 				/* LINTED */
22377c478bd9Sstevel@tonic-gate 				VERDEFNUM(lmp) = (int)ld->d_un.d_val;
22387c478bd9Sstevel@tonic-gate 				break;
22393b41b08bSab 			case DT_VERSYM:
22403b41b08bSab 				VERSYM(lmp) = (Versym *)(ld->d_un.d_ptr + base);
22413b41b08bSab 				break;
22427c478bd9Sstevel@tonic-gate 			case DT_BIND_NOW:
2243dffec89cSrie 				if ((ld->d_un.d_val & DF_BIND_NOW) &&
2244dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22457c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22467c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22477c478bd9Sstevel@tonic-gate 				}
22487c478bd9Sstevel@tonic-gate 				break;
22497c478bd9Sstevel@tonic-gate 			case DT_FLAGS:
22507c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_SYMBOLIC)
22517c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_SYMBOLIC;
22527c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_TEXTREL)
22537c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_TEXTREL;
2254dffec89cSrie 				if ((ld->d_un.d_val & DF_BIND_NOW) &&
2255dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22567c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22577c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22587c478bd9Sstevel@tonic-gate 				}
2259d326b23bSrie 				/*
2260d326b23bSrie 				 * Capture any static TLS use, and enforce that
2261d326b23bSrie 				 * this object be non-deletable.
2262d326b23bSrie 				 */
2263d326b23bSrie 				if (ld->d_un.d_val & DF_STATIC_TLS) {
2264d326b23bSrie 					FLAGS1(lmp) |= FL1_RT_TLSSTAT;
2265d326b23bSrie 					MODE(lmp) |= RTLD_NODELETE;
2266d326b23bSrie 				}
22677c478bd9Sstevel@tonic-gate 				break;
22687c478bd9Sstevel@tonic-gate 			case DT_FLAGS_1:
22697c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DISPRELPND)
22707c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_DISPREL;
22717c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_GROUP)
22727c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |=
22737c478bd9Sstevel@tonic-gate 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
2274dffec89cSrie 				if ((ld->d_un.d_val & DF_1_NOW) &&
2275dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22767c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22777c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22787c478bd9Sstevel@tonic-gate 				}
22797c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODELETE)
22807c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NODELETE;
22817c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_INITFIRST)
22827c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INITFRST;
22837c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NOOPEN)
22847c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NOOPEN;
22857c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_LOADFLTR)
22867c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_LOADFLTR;
22877c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODUMP)
22887c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NODUMP;
22897c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_CONFALT)
22907c478bd9Sstevel@tonic-gate 					crle = 1;
22917c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DIRECT)
22929a411307Srie 					FLAGS1(lmp) |= FL1_RT_DIRECT;
22937c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODEFLIB)
22947c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_NODEFLIB;
22957c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ENDFILTEE)
22967c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_ENDFILTE;
22977c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_TRANS)
22987c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_TRANS;
22997c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
23007c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ORIGIN)
23017c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_RELATIVE;
23027c478bd9Sstevel@tonic-gate #endif
23037c478bd9Sstevel@tonic-gate 				/*
23047c478bd9Sstevel@tonic-gate 				 * If this object identifies itself as an
23057c478bd9Sstevel@tonic-gate 				 * interposer, but relocation processing has
23067c478bd9Sstevel@tonic-gate 				 * already started, then demote it.  It's too
23077c478bd9Sstevel@tonic-gate 				 * late to guarantee complete interposition.
23087c478bd9Sstevel@tonic-gate 				 */
2309*a953e2b1Srie 				/* BEGIN CSTYLED */
23109a411307Srie 				if (ld->d_un.d_val &
23119a411307Srie 				    (DF_1_INTERPOSE | DF_1_SYMINTPOSE)) {
23129a411307Srie 				    if (lml->lm_flags & LML_FLG_STARTREL) {
23135aefb655Srie 					DBG_CALL(Dbg_util_intoolate(lmp));
23147c478bd9Sstevel@tonic-gate 					if (lml->lm_flags & LML_FLG_TRC_ENABLE)
23157c478bd9Sstevel@tonic-gate 					    (void) printf(
23167c478bd9Sstevel@tonic-gate 						MSG_INTL(MSG_LDD_REL_ERR2),
23177c478bd9Sstevel@tonic-gate 						NAME(lmp));
23189a411307Srie 				    } else if (ld->d_un.d_val & DF_1_INTERPOSE)
23199a411307Srie 					FLAGS(lmp) |= FLG_RT_OBJINTPO;
23209a411307Srie 				    else
23219a411307Srie 					FLAGS(lmp) |= FLG_RT_SYMINTPO;
23227c478bd9Sstevel@tonic-gate 				}
2323*a953e2b1Srie 				/* END CSTYLED */
23247c478bd9Sstevel@tonic-gate 				break;
23257c478bd9Sstevel@tonic-gate 			case DT_SYMINFO:
23267c478bd9Sstevel@tonic-gate 				SYMINFO(lmp) = (Syminfo *)(ld->d_un.d_ptr +
23277c478bd9Sstevel@tonic-gate 				    base);
23287c478bd9Sstevel@tonic-gate 				break;
23297c478bd9Sstevel@tonic-gate 			case DT_SYMINENT:
23307c478bd9Sstevel@tonic-gate 				SYMINENT(lmp) = ld->d_un.d_val;
23317c478bd9Sstevel@tonic-gate 				break;
23327c478bd9Sstevel@tonic-gate 			case DT_PLTPAD:
23337c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = (void *)(ld->d_un.d_ptr + base);
23347c478bd9Sstevel@tonic-gate 				break;
23357c478bd9Sstevel@tonic-gate 			case DT_PLTPADSZ:
23367c478bd9Sstevel@tonic-gate 				pltpadsz = ld->d_un.d_val;
23377c478bd9Sstevel@tonic-gate 				break;
23387c478bd9Sstevel@tonic-gate 			case DT_SUNW_RTLDINF:
23397c478bd9Sstevel@tonic-gate 				/*
234010a4fa49Srie 				 * Maintain a list of RTLDINFO structures.
234110a4fa49Srie 				 * Typically, libc is the only supplier, and
234210a4fa49Srie 				 * only one structure is provided.  However,
234310a4fa49Srie 				 * multiple suppliers and multiple structures
234410a4fa49Srie 				 * are supported.  For example, one structure
234510a4fa49Srie 				 * may provide thread_init, and another
234610a4fa49Srie 				 * structure may provide atexit reservations.
23477c478bd9Sstevel@tonic-gate 				 */
234810a4fa49Srie 				if ((rti = alist_append(&lml->lm_rti, 0,
234910a4fa49Srie 				    sizeof (Rti_desc), AL_CNT_RTLDINFO)) == 0) {
23507c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
23517c478bd9Sstevel@tonic-gate 					return (0);
23527c478bd9Sstevel@tonic-gate 				}
235310a4fa49Srie 				rti->rti_lmp = lmp;
235410a4fa49Srie 				rti->rti_info = (void *)(ld->d_un.d_ptr + base);
23557c478bd9Sstevel@tonic-gate 				break;
2356d579eb63Sab 			case DT_SUNW_SORTENT:
2357d579eb63Sab 				SUNWSORTENT(lmp) = ld->d_un.d_val;
2358d579eb63Sab 				break;
2359d579eb63Sab 			case DT_SUNW_SYMSORT:
2360d579eb63Sab 				SUNWSYMSORT(lmp) =
2361d579eb63Sab 				    (void *)(ld->d_un.d_ptr + base);
2362d579eb63Sab 				break;
2363d579eb63Sab 			case DT_SUNW_SYMSORTSZ:
2364d579eb63Sab 				SUNWSYMSORTSZ(lmp) = ld->d_un.d_val;
2365d579eb63Sab 				break;
23667c478bd9Sstevel@tonic-gate 			case DT_DEPRECATED_SPARC_REGISTER:
23677c478bd9Sstevel@tonic-gate 			case M_DT_REGISTER:
23687c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_REGSYMS;
23697c478bd9Sstevel@tonic-gate 				break;
23707c478bd9Sstevel@tonic-gate 			case M_DT_PLTRESERVE:
23717c478bd9Sstevel@tonic-gate 				PLTRESERVE(lmp) = (void *)(ld->d_un.d_ptr +
23727c478bd9Sstevel@tonic-gate 				    base);
23737c478bd9Sstevel@tonic-gate 				break;
23747c478bd9Sstevel@tonic-gate 			}
23757c478bd9Sstevel@tonic-gate 		}
23767c478bd9Sstevel@tonic-gate 
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 		if (PLTPAD(lmp)) {
23797c478bd9Sstevel@tonic-gate 			if (pltpadsz == (Xword)0)
23807c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = 0;
23817c478bd9Sstevel@tonic-gate 			else
23827c478bd9Sstevel@tonic-gate 				PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) +
23837c478bd9Sstevel@tonic-gate 				    pltpadsz);
23847c478bd9Sstevel@tonic-gate 		}
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 		/*
23877c478bd9Sstevel@tonic-gate 		 * Allocate Dynamic Info structure
23887c478bd9Sstevel@tonic-gate 		 */
23897c478bd9Sstevel@tonic-gate 		if ((DYNINFO(lmp) = calloc((size_t)dyncnt,
23907c478bd9Sstevel@tonic-gate 		    sizeof (Dyninfo))) == 0) {
23917c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
23927c478bd9Sstevel@tonic-gate 			return (0);
23937c478bd9Sstevel@tonic-gate 		}
23947c478bd9Sstevel@tonic-gate 		DYNINFOCNT(lmp) = dyncnt;
23957c478bd9Sstevel@tonic-gate 	}
23967c478bd9Sstevel@tonic-gate 
23979039eeafSab 	/*
23989039eeafSab 	 * A dynsym contains only global functions. We want to have
23999039eeafSab 	 * a version of it that also includes local functions, so that
24009039eeafSab 	 * dladdr() will be able to report names for local functions
24019039eeafSab 	 * when used to generate a stack trace for a stripped file.
24029039eeafSab 	 * This version of the dynsym is provided via DT_SUNW_SYMTAB.
24039039eeafSab 	 *
24049039eeafSab 	 * In producing DT_SUNW_SYMTAB, ld uses a non-obvious trick
24059039eeafSab 	 * in order to avoid having to have two copies of the global
24069039eeafSab 	 * symbols held in DT_SYMTAB: The local symbols are placed in
24079039eeafSab 	 * a separate section than the globals in the dynsym, but the
24089039eeafSab 	 * linker conspires to put the data for these two sections adjacent
24099039eeafSab 	 * to each other. DT_SUNW_SYMTAB points at the top of the local
24109039eeafSab 	 * symbols, and DT_SUNW_SYMSZ is the combined length of both tables.
24119039eeafSab 	 *
24129039eeafSab 	 * If the two sections are not adjacent, then something went wrong
24139039eeafSab 	 * at link time. We use ASSERT to kill the process if this is
24149039eeafSab 	 * a debug build. In a production build, we will silently ignore
24159039eeafSab 	 * the presence of the .ldynsym and proceed. We can detect this
24169039eeafSab 	 * situation by checking to see that DT_SYMTAB lies in
24179039eeafSab 	 * the range given by DT_SUNW_SYMTAB/DT_SUNW_SYMSZ.
24189039eeafSab 	 */
24199039eeafSab 	if ((SUNWSYMTAB(lmp) != NULL) &&
24209039eeafSab 	    (((char *)SYMTAB(lmp) <= (char *)SUNWSYMTAB(lmp)) ||
24219039eeafSab 	    (((char *)SYMTAB(lmp) >=
24229039eeafSab 	    (SUNWSYMSZ(lmp) + (char *)SUNWSYMTAB(lmp)))))) {
24239039eeafSab 		ASSERT(0);
24249039eeafSab 		SUNWSYMTAB(lmp) = NULL;
24259039eeafSab 		SUNWSYMSZ(lmp) = 0;
24269039eeafSab 	}
24279039eeafSab 
24287c478bd9Sstevel@tonic-gate 	/*
24297c478bd9Sstevel@tonic-gate 	 * If configuration file use hasn't been disabled, and a configuration
24307c478bd9Sstevel@tonic-gate 	 * file hasn't already been set via an environment variable, see if any
24317c478bd9Sstevel@tonic-gate 	 * application specific configuration file is specified.  An LD_CONFIG
24327c478bd9Sstevel@tonic-gate 	 * setting is used first, but if this image was generated via crle(1)
24337c478bd9Sstevel@tonic-gate 	 * then a default configuration file is a fall-back.
24347c478bd9Sstevel@tonic-gate 	 */
24357c478bd9Sstevel@tonic-gate 	if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == 0)) {
24367c478bd9Sstevel@tonic-gate 		if (cfile)
24377c478bd9Sstevel@tonic-gate 			config->c_name = (const char *)(cfile +
24387c478bd9Sstevel@tonic-gate 			    (char *)STRTAB(lmp));
24397c478bd9Sstevel@tonic-gate 		else if (crle) {
24407c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_CONFAPP;
24417c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
24427c478bd9Sstevel@tonic-gate 			FLAGS1(lmp) |= FL1_RT_RELATIVE;
24437c478bd9Sstevel@tonic-gate #endif
24447c478bd9Sstevel@tonic-gate 		}
24457c478bd9Sstevel@tonic-gate 	}
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	if (rpath)
24487c478bd9Sstevel@tonic-gate 		RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp));
24497c478bd9Sstevel@tonic-gate 	if (fltr) {
24507c478bd9Sstevel@tonic-gate 		/*
24517c478bd9Sstevel@tonic-gate 		 * If this object is a global filter, duplicate the filtee
24527c478bd9Sstevel@tonic-gate 		 * string name(s) so that REFNAME() is available in core files.
24537c478bd9Sstevel@tonic-gate 		 * This cludge was useful for debuggers at one point, but only
24547c478bd9Sstevel@tonic-gate 		 * when the filtee name was an individual full path.
24557c478bd9Sstevel@tonic-gate 		 */
24567c478bd9Sstevel@tonic-gate 		if ((REFNAME(lmp) = strdup(fltr + (char *)STRTAB(lmp))) == 0) {
24577c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
24587c478bd9Sstevel@tonic-gate 			return (0);
24597c478bd9Sstevel@tonic-gate 		}
24607c478bd9Sstevel@tonic-gate 	}
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_RELATIVE)
24637c478bd9Sstevel@tonic-gate 		FLAGS1(lmp) |= FL1_RT_RELATIVE;
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate 	/*
24667c478bd9Sstevel@tonic-gate 	 * For Intel ABI compatibility.  It's possible that a JMPREL can be
24677c478bd9Sstevel@tonic-gate 	 * specified without any other relocations (e.g. a dynamic executable
24687c478bd9Sstevel@tonic-gate 	 * normally only contains .plt relocations).  If this is the case then
24697c478bd9Sstevel@tonic-gate 	 * no REL, RELSZ or RELENT will have been created.  For us to be able
24707c478bd9Sstevel@tonic-gate 	 * to traverse the .plt relocations under LD_BIND_NOW we need to know
24717c478bd9Sstevel@tonic-gate 	 * the RELENT for these relocations.  Refer to elf_reloc() for more
24727c478bd9Sstevel@tonic-gate 	 * details.
24737c478bd9Sstevel@tonic-gate 	 */
24747c478bd9Sstevel@tonic-gate 	if (!RELENT(lmp) && JMPREL(lmp))
24757c478bd9Sstevel@tonic-gate 		RELENT(lmp) = sizeof (Rel);
24767c478bd9Sstevel@tonic-gate 
24777c478bd9Sstevel@tonic-gate 	/*
24787c478bd9Sstevel@tonic-gate 	 * Establish any per-object auditing.  If we're establishing `main's
24797c478bd9Sstevel@tonic-gate 	 * link-map its too early to go searching for audit objects so just
24807c478bd9Sstevel@tonic-gate 	 * hold the object name for later (see setup()).
24817c478bd9Sstevel@tonic-gate 	 */
24827c478bd9Sstevel@tonic-gate 	if (audit) {
24837c478bd9Sstevel@tonic-gate 		char	*cp = audit + (char *)STRTAB(lmp);
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 		if (*cp) {
24867c478bd9Sstevel@tonic-gate 			if (((AUDITORS(lmp) =
24877c478bd9Sstevel@tonic-gate 			    calloc(1, sizeof (Audit_desc))) == 0) ||
24887c478bd9Sstevel@tonic-gate 			    ((AUDITORS(lmp)->ad_name = strdup(cp)) == 0)) {
24897c478bd9Sstevel@tonic-gate 				remove_so(0, lmp);
24907c478bd9Sstevel@tonic-gate 				return (0);
24917c478bd9Sstevel@tonic-gate 			}
249241072f3cSrie 			if (lml_main.lm_head) {
249356d7adc6Srie 				if (audit_setup(lmp, AUDITORS(lmp), 0) == 0) {
24947c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
24957c478bd9Sstevel@tonic-gate 					return (0);
24967c478bd9Sstevel@tonic-gate 				}
24977c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= AUDITORS(lmp)->ad_flags;
24987c478bd9Sstevel@tonic-gate 				lml->lm_flags |= LML_FLG_LOCAUDIT;
24997c478bd9Sstevel@tonic-gate 			}
25007c478bd9Sstevel@tonic-gate 		}
25017c478bd9Sstevel@tonic-gate 	}
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	if ((CONDVAR(lmp) = rt_cond_create()) == 0) {
25047c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
25057c478bd9Sstevel@tonic-gate 		return (0);
25067c478bd9Sstevel@tonic-gate 	}
25077c478bd9Sstevel@tonic-gate 	if (oname && ((append_alias(lmp, oname, 0)) == 0)) {
25087c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
25097c478bd9Sstevel@tonic-gate 		return (0);
25107c478bd9Sstevel@tonic-gate 	}
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	/*
25137c478bd9Sstevel@tonic-gate 	 * Add the mapped object to the end of the link map list.
25147c478bd9Sstevel@tonic-gate 	 */
25157c478bd9Sstevel@tonic-gate 	lm_append(lml, lmco, lmp);
25167c478bd9Sstevel@tonic-gate 	return (lmp);
25177c478bd9Sstevel@tonic-gate }
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate /*
25207c478bd9Sstevel@tonic-gate  * Assign hardware/software capabilities.
25217c478bd9Sstevel@tonic-gate  */
25227c478bd9Sstevel@tonic-gate void
25237c478bd9Sstevel@tonic-gate cap_assign(Cap *cap, Rt_map *lmp)
25247c478bd9Sstevel@tonic-gate {
25257c478bd9Sstevel@tonic-gate 	while (cap->c_tag != CA_SUNW_NULL) {
25267c478bd9Sstevel@tonic-gate 		switch (cap->c_tag) {
25277c478bd9Sstevel@tonic-gate 		case CA_SUNW_HW_1:
25287c478bd9Sstevel@tonic-gate 			HWCAP(lmp) = cap->c_un.c_val;
25297c478bd9Sstevel@tonic-gate 			break;
25307c478bd9Sstevel@tonic-gate 		case CA_SUNW_SF_1:
25317c478bd9Sstevel@tonic-gate 			SFCAP(lmp) = cap->c_un.c_val;
25327c478bd9Sstevel@tonic-gate 		}
25337c478bd9Sstevel@tonic-gate 		cap++;
25347c478bd9Sstevel@tonic-gate 	}
25357c478bd9Sstevel@tonic-gate }
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate /*
25387c478bd9Sstevel@tonic-gate  * Map in an ELF object.
25397c478bd9Sstevel@tonic-gate  * Takes an open file descriptor for the object to map and its pathname; returns
25407c478bd9Sstevel@tonic-gate  * a pointer to a Rt_map structure for this object, or 0 on error.
25417c478bd9Sstevel@tonic-gate  */
25427c478bd9Sstevel@tonic-gate static Rt_map *
25437c478bd9Sstevel@tonic-gate elf_map_so(Lm_list *lml, Aliste lmco, const char *pname, const char *oname,
25447c478bd9Sstevel@tonic-gate     int fd)
25457c478bd9Sstevel@tonic-gate {
25467c478bd9Sstevel@tonic-gate 	int		i; 		/* general temporary */
25477c478bd9Sstevel@tonic-gate 	Off		memsize = 0;	/* total memory size of pathname */
25487c478bd9Sstevel@tonic-gate 	Off		mentry;		/* entry point */
25497c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr;		/* ELF header of ld.so */
25507c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* first Phdr in file */
25517c478bd9Sstevel@tonic-gate 	Phdr		*phdr0;		/* Saved first Phdr in file */
25527c478bd9Sstevel@tonic-gate 	Phdr		*pptr;		/* working Phdr */
25537c478bd9Sstevel@tonic-gate 	Phdr		*fph = 0;	/* first loadable Phdr */
25547c478bd9Sstevel@tonic-gate 	Phdr		*lph;		/* last loadable Phdr */
25557c478bd9Sstevel@tonic-gate 	Phdr		*lfph = 0;	/* last loadable (filesz != 0) Phdr */
25567c478bd9Sstevel@tonic-gate 	Phdr		*lmph = 0;	/* last loadable (memsz != 0) Phdr */
25577c478bd9Sstevel@tonic-gate 	Phdr		*swph = 0;	/* program header for SUNWBSS */
25587c478bd9Sstevel@tonic-gate 	Phdr		*tlph = 0;	/* program header for PT_TLS */
25597c478bd9Sstevel@tonic-gate 	Phdr		*unwindph = 0;	/* program header for PT_SUNW_UNWIND */
25607c478bd9Sstevel@tonic-gate 	Cap		*cap = 0;	/* program header for SUNWCAP */
25617c478bd9Sstevel@tonic-gate 	Dyn		*mld = 0;	/* DYNAMIC structure for pathname */
25627c478bd9Sstevel@tonic-gate 	size_t		size;		/* size of elf and program headers */
25637c478bd9Sstevel@tonic-gate 	caddr_t		faddr = 0;	/* mapping address of pathname */
25647c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;		/* link map created */
25657c478bd9Sstevel@tonic-gate 	caddr_t		paddr;		/* start of padded image */
25667c478bd9Sstevel@tonic-gate 	Off		plen;		/* size of image including padding */
25677c478bd9Sstevel@tonic-gate 	Half		etype;
25687c478bd9Sstevel@tonic-gate 	int		fixed;
25697c478bd9Sstevel@tonic-gate 	Mmap		*mmaps;
25707c478bd9Sstevel@tonic-gate 	uint_t		mmapcnt = 0;
25717c478bd9Sstevel@tonic-gate 	Xword		align = 0;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	/* LINTED */
25747c478bd9Sstevel@tonic-gate 	ehdr = (Ehdr *)fmap->fm_maddr;
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 	/*
25777c478bd9Sstevel@tonic-gate 	 * If this a relocatable object then special processing is required.
25787c478bd9Sstevel@tonic-gate 	 */
25797c478bd9Sstevel@tonic-gate 	if ((etype = ehdr->e_type) == ET_REL)
258041072f3cSrie 		return (elf_obj_file(lml, lmco, pname, fd));
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	/*
25837c478bd9Sstevel@tonic-gate 	 * If this isn't a dynamic executable or shared object we can't process
25847c478bd9Sstevel@tonic-gate 	 * it.  If this is a dynamic executable then all addresses are fixed.
25857c478bd9Sstevel@tonic-gate 	 */
25867c478bd9Sstevel@tonic-gate 	if (etype == ET_EXEC)
25877c478bd9Sstevel@tonic-gate 		fixed = 1;
25887c478bd9Sstevel@tonic-gate 	else if (etype == ET_DYN)
25897c478bd9Sstevel@tonic-gate 		fixed = 0;
25907c478bd9Sstevel@tonic-gate 	else {
25915aefb655Srie 		eprintf(lml, ERR_ELF, MSG_INTL(MSG_GEN_BADTYPE), pname,
2592c13de8f6Sab 		    conv_ehdr_type(etype, 0));
25937c478bd9Sstevel@tonic-gate 		return (0);
25947c478bd9Sstevel@tonic-gate 	}
25957c478bd9Sstevel@tonic-gate 
25967c478bd9Sstevel@tonic-gate 	/*
25977c478bd9Sstevel@tonic-gate 	 * If our original mapped page was not large enough to hold all the
25987c478bd9Sstevel@tonic-gate 	 * program headers remap them.
25997c478bd9Sstevel@tonic-gate 	 */
26007c478bd9Sstevel@tonic-gate 	size = (size_t)((char *)ehdr->e_phoff +
2601*a953e2b1Srie 	    (ehdr->e_phnum * ehdr->e_phentsize));
26027c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_fsize) {
26035aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
26047c478bd9Sstevel@tonic-gate 		return (0);
26057c478bd9Sstevel@tonic-gate 	}
26067c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_msize) {
26077c478bd9Sstevel@tonic-gate 		fmap_setup();
26087c478bd9Sstevel@tonic-gate 		if ((fmap->fm_maddr = mmap(fmap->fm_maddr, size, PROT_READ,
26097c478bd9Sstevel@tonic-gate 		    fmap->fm_mflags, fd, 0)) == MAP_FAILED) {
26107c478bd9Sstevel@tonic-gate 			int	err = errno;
26115aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), pname,
26127c478bd9Sstevel@tonic-gate 			    strerror(err));
26137c478bd9Sstevel@tonic-gate 			return (0);
26147c478bd9Sstevel@tonic-gate 		}
26157c478bd9Sstevel@tonic-gate 		fmap->fm_msize = size;
26167c478bd9Sstevel@tonic-gate 		/* LINTED */
26177c478bd9Sstevel@tonic-gate 		ehdr = (Ehdr *)fmap->fm_maddr;
26187c478bd9Sstevel@tonic-gate 	}
26197c478bd9Sstevel@tonic-gate 	/* LINTED */
26207c478bd9Sstevel@tonic-gate 	phdr0 = phdr = (Phdr *)((char *)ehdr + ehdr->e_ehsize);
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 	/*
26237c478bd9Sstevel@tonic-gate 	 * Get entry point.
26247c478bd9Sstevel@tonic-gate 	 */
26257c478bd9Sstevel@tonic-gate 	mentry = ehdr->e_entry;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	/*
26287c478bd9Sstevel@tonic-gate 	 * Point at program headers and perform some basic validation.
26297c478bd9Sstevel@tonic-gate 	 */
26307c478bd9Sstevel@tonic-gate 	for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
26317c478bd9Sstevel@tonic-gate 	    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
26327c478bd9Sstevel@tonic-gate 		if ((pptr->p_type == PT_LOAD) ||
26337c478bd9Sstevel@tonic-gate 		    (pptr->p_type == PT_SUNWBSS)) {
26347c478bd9Sstevel@tonic-gate 
26357c478bd9Sstevel@tonic-gate 			if (fph == 0) {
26367c478bd9Sstevel@tonic-gate 				fph = pptr;
26377c478bd9Sstevel@tonic-gate 			/* LINTED argument lph is initialized in first pass */
26387c478bd9Sstevel@tonic-gate 			} else if (pptr->p_vaddr <= lph->p_vaddr) {
26395aefb655Srie 				eprintf(lml, ERR_ELF,
26405aefb655Srie 				    MSG_INTL(MSG_GEN_INVPRGHDR), pname);
26417c478bd9Sstevel@tonic-gate 				return (0);
26427c478bd9Sstevel@tonic-gate 			}
26437c478bd9Sstevel@tonic-gate 
26447c478bd9Sstevel@tonic-gate 			lph = pptr;
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 			if (pptr->p_memsz)
26477c478bd9Sstevel@tonic-gate 				lmph = pptr;
26487c478bd9Sstevel@tonic-gate 			if (pptr->p_filesz)
26497c478bd9Sstevel@tonic-gate 				lfph = pptr;
26507c478bd9Sstevel@tonic-gate 			if (pptr->p_type == PT_SUNWBSS)
26517c478bd9Sstevel@tonic-gate 				swph = pptr;
26527c478bd9Sstevel@tonic-gate 			if (pptr->p_align > align)
26537c478bd9Sstevel@tonic-gate 				align = pptr->p_align;
26547c478bd9Sstevel@tonic-gate 
265510a4fa49Srie 		} else if (pptr->p_type == PT_DYNAMIC) {
26567c478bd9Sstevel@tonic-gate 			mld = (Dyn *)(pptr->p_vaddr);
265710a4fa49Srie 		} else if ((pptr->p_type == PT_TLS) && pptr->p_memsz) {
26587c478bd9Sstevel@tonic-gate 			tlph = pptr;
265910a4fa49Srie 		} else if (pptr->p_type == PT_SUNWCAP) {
26607c478bd9Sstevel@tonic-gate 			cap = (Cap *)(pptr->p_vaddr);
266110a4fa49Srie 		} else if (pptr->p_type == PT_SUNW_UNWIND) {
26627c478bd9Sstevel@tonic-gate 			unwindph = pptr;
266310a4fa49Srie 		}
26647c478bd9Sstevel@tonic-gate 	}
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
26677c478bd9Sstevel@tonic-gate 	/*
2668*a953e2b1Srie 	 * Make sure the maximum page alignment is a power of 2 >= the default
2669*a953e2b1Srie 	 * segment alignment, for use with MAP_ALIGN.
26707c478bd9Sstevel@tonic-gate 	 */
2671*a953e2b1Srie 	align = S_ROUND(align, M_SEGM_ALIGN);
26727c478bd9Sstevel@tonic-gate #endif
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	/*
26757c478bd9Sstevel@tonic-gate 	 * We'd better have at least one loadable segment, together with some
26767c478bd9Sstevel@tonic-gate 	 * specified file and memory size.
26777c478bd9Sstevel@tonic-gate 	 */
26787c478bd9Sstevel@tonic-gate 	if ((fph == 0) || (lmph == 0) || (lfph == 0)) {
26795aefb655Srie 		eprintf(lml, ERR_ELF, MSG_INTL(MSG_GEN_NOLOADSEG), pname);
26807c478bd9Sstevel@tonic-gate 		return (0);
26817c478bd9Sstevel@tonic-gate 	}
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 	/*
26847c478bd9Sstevel@tonic-gate 	 * Check that the files size accounts for the loadable sections
26857c478bd9Sstevel@tonic-gate 	 * we're going to map in (failure to do this may cause spurious
26867c478bd9Sstevel@tonic-gate 	 * bus errors if we're given a truncated file).
26877c478bd9Sstevel@tonic-gate 	 */
26887c478bd9Sstevel@tonic-gate 	if (fmap->fm_fsize < ((size_t)lfph->p_offset + lfph->p_filesz)) {
26895aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
26907c478bd9Sstevel@tonic-gate 		return (0);
26917c478bd9Sstevel@tonic-gate 	}
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 	/*
26947c478bd9Sstevel@tonic-gate 	 * Memsize must be page rounded so that if we add object padding
26957c478bd9Sstevel@tonic-gate 	 * at the end it will start at the beginning of a page.
26967c478bd9Sstevel@tonic-gate 	 */
26977c478bd9Sstevel@tonic-gate 	plen = memsize = M_PROUND((lmph->p_vaddr + lmph->p_memsz) -
26987c478bd9Sstevel@tonic-gate 	    M_PTRUNC((ulong_t)fph->p_vaddr));
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	/*
27017c478bd9Sstevel@tonic-gate 	 * Determine if an existing mapping is acceptable.
27027c478bd9Sstevel@tonic-gate 	 */
27037c478bd9Sstevel@tonic-gate 	if (interp && (lml->lm_flags & LML_FLG_BASELM) &&
270441072f3cSrie 	    (strcmp(pname, interp->i_name) == 0)) {
27057c478bd9Sstevel@tonic-gate 		/*
27067c478bd9Sstevel@tonic-gate 		 * If this is the interpreter then it has already been mapped
27077c478bd9Sstevel@tonic-gate 		 * and we have the address so don't map it again.  Note that
27087c478bd9Sstevel@tonic-gate 		 * the common occurrence of a reference to the interpretor
27097c478bd9Sstevel@tonic-gate 		 * (libdl -> ld.so.1) will have been caught during filter
27107c478bd9Sstevel@tonic-gate 		 * initialization (see elf_lookup_filtee()).  However, some
27117c478bd9Sstevel@tonic-gate 		 * ELF implementations are known to record libc.so.1 as the
27127c478bd9Sstevel@tonic-gate 		 * interpretor, and thus this test catches this behavior.
27137c478bd9Sstevel@tonic-gate 		 */
27147c478bd9Sstevel@tonic-gate 		paddr = faddr = interp->i_faddr;
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 	} else if ((fixed == 0) && (r_debug.rtd_objpad == 0) &&
27177c478bd9Sstevel@tonic-gate 	    (memsize <= fmap->fm_msize) && ((fph->p_flags & PF_W) == 0) &&
2718*a953e2b1Srie 	    (fph == lph) && (fph->p_filesz == fph->p_memsz) &&
27197c478bd9Sstevel@tonic-gate 	    (((Xword)fmap->fm_maddr % align) == 0)) {
2720*a953e2b1Srie 		size_t	rsize;
2721*a953e2b1Srie 
27227c478bd9Sstevel@tonic-gate 		/*
2723*a953e2b1Srie 		 * If the file contains a single segment, and the mapping
2724*a953e2b1Srie 		 * required has already been established from the initial fmap
2725*a953e2b1Srie 		 * mapping, then we don't need to do anything more.  Reset the
2726*a953e2b1Srie 		 * fmap address so that any later files start a new fmap.  This
2727*a953e2b1Srie 		 * is really an optimization for filters, such as libdl.so,
2728*a953e2b1Srie 		 * libthread, etc. that are constructed to be a single text
2729*a953e2b1Srie 		 * segment.
27307c478bd9Sstevel@tonic-gate 		 */
27317c478bd9Sstevel@tonic-gate 		paddr = faddr = fmap->fm_maddr;
2732*a953e2b1Srie 
2733*a953e2b1Srie 		/*
2734*a953e2b1Srie 		 * Free any unused mapping by assigning the fmap buffer to the
2735*a953e2b1Srie 		 * unused region.  fmap_setup() will unmap this area and
2736*a953e2b1Srie 		 * establish defaults for future mappings.
2737*a953e2b1Srie 		 */
2738*a953e2b1Srie 		rsize = M_PROUND(fph->p_filesz);
2739*a953e2b1Srie 		fmap->fm_maddr += rsize;
2740*a953e2b1Srie 		fmap->fm_msize -= rsize;
27417c478bd9Sstevel@tonic-gate 		fmap_setup();
27427c478bd9Sstevel@tonic-gate 	}
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	/*
27457c478bd9Sstevel@tonic-gate 	 * Allocate a mapping array to retain mapped segment information.
27467c478bd9Sstevel@tonic-gate 	 */
27477c478bd9Sstevel@tonic-gate 	if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
27487c478bd9Sstevel@tonic-gate 		return (0);
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 	/*
27517c478bd9Sstevel@tonic-gate 	 * If we're reusing an existing mapping determine the objects etext
27527c478bd9Sstevel@tonic-gate 	 * address.  Otherwise map the file (which will calculate the etext
27537c478bd9Sstevel@tonic-gate 	 * address as part of the mapping process).
27547c478bd9Sstevel@tonic-gate 	 */
27557c478bd9Sstevel@tonic-gate 	if (faddr) {
27567c478bd9Sstevel@tonic-gate 		caddr_t	base;
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 		if (fixed)
27597c478bd9Sstevel@tonic-gate 			base = 0;
27607c478bd9Sstevel@tonic-gate 		else
27617c478bd9Sstevel@tonic-gate 			base = faddr;
27627c478bd9Sstevel@tonic-gate 
27637c478bd9Sstevel@tonic-gate 		/* LINTED */
27647c478bd9Sstevel@tonic-gate 		phdr0 = phdr = (Phdr *)((char *)faddr + ehdr->e_ehsize);
27657c478bd9Sstevel@tonic-gate 
27667c478bd9Sstevel@tonic-gate 		for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
27677c478bd9Sstevel@tonic-gate 		    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
27687c478bd9Sstevel@tonic-gate 			if (pptr->p_type != PT_LOAD)
27697c478bd9Sstevel@tonic-gate 				continue;
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_vaddr = (pptr->p_vaddr + base);
27727c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_msize = pptr->p_memsz;
27737c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_fsize = pptr->p_filesz;
27747c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_perm = (PROT_READ | PROT_EXEC);
27757c478bd9Sstevel@tonic-gate 			mmapcnt++;
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 			if (!(pptr->p_flags & PF_W)) {
27787c478bd9Sstevel@tonic-gate 				fmap->fm_etext = (ulong_t)pptr->p_vaddr +
27797c478bd9Sstevel@tonic-gate 				    (ulong_t)pptr->p_memsz +
27807c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
27817c478bd9Sstevel@tonic-gate 			}
27827c478bd9Sstevel@tonic-gate 		}
27837c478bd9Sstevel@tonic-gate 	} else {
27847c478bd9Sstevel@tonic-gate 		/*
27857c478bd9Sstevel@tonic-gate 		 * Map the file.
27867c478bd9Sstevel@tonic-gate 		 */
27875aefb655Srie 		if (!(faddr = elf_map_it(lml, pname, memsize, ehdr, fph, lph,
27887c478bd9Sstevel@tonic-gate 		    &phdr, &paddr, &plen, fixed, fd, align, mmaps, &mmapcnt)))
27897c478bd9Sstevel@tonic-gate 			return (0);
27907c478bd9Sstevel@tonic-gate 	}
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	/*
27937c478bd9Sstevel@tonic-gate 	 * Calculate absolute base addresses and entry points.
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	if (!fixed) {
27967c478bd9Sstevel@tonic-gate 		if (mld)
27977c478bd9Sstevel@tonic-gate 			/* LINTED */
27987c478bd9Sstevel@tonic-gate 			mld = (Dyn *)((Off)mld + faddr);
27997c478bd9Sstevel@tonic-gate 		if (cap)
28007c478bd9Sstevel@tonic-gate 			/* LINTED */
28017c478bd9Sstevel@tonic-gate 			cap = (Cap *)((Off)cap + faddr);
28027c478bd9Sstevel@tonic-gate 		mentry += (Off)faddr;
28037c478bd9Sstevel@tonic-gate 	}
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 	/*
28067c478bd9Sstevel@tonic-gate 	 * Create new link map structure for newly mapped shared object.
28077c478bd9Sstevel@tonic-gate 	 */
28087c478bd9Sstevel@tonic-gate 	if (!(lmp = elf_new_lm(lml, pname, oname, mld, (ulong_t)faddr,
28097c478bd9Sstevel@tonic-gate 	    fmap->fm_etext, lmco, memsize, mentry, (ulong_t)paddr, plen, mmaps,
28107c478bd9Sstevel@tonic-gate 	    mmapcnt))) {
28117c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)faddr, memsize);
28127c478bd9Sstevel@tonic-gate 		return (0);
28137c478bd9Sstevel@tonic-gate 	}
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	/*
28167c478bd9Sstevel@tonic-gate 	 * Start the system loading in the ELF information we'll be processing.
28177c478bd9Sstevel@tonic-gate 	 */
28187c478bd9Sstevel@tonic-gate 	if (REL(lmp)) {
28197c478bd9Sstevel@tonic-gate 		(void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) +
28207c478bd9Sstevel@tonic-gate 		    (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp),
28217c478bd9Sstevel@tonic-gate 		    MADV_WILLNEED);
28227c478bd9Sstevel@tonic-gate 	}
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	/*
2825d326b23bSrie 	 * If this shared object contains any special segments, record them.
28267c478bd9Sstevel@tonic-gate 	 */
28277c478bd9Sstevel@tonic-gate 	if (swph) {
28287c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_SUNWBSS;
28297c478bd9Sstevel@tonic-gate 		SUNWBSS(lmp) = phdr + (swph - phdr0);
28307c478bd9Sstevel@tonic-gate 	}
2831d326b23bSrie 	if (tlph && (tls_assign(lml, lmp, (phdr + (tlph - phdr0))) == 0)) {
2832d326b23bSrie 		remove_so(lml, lmp);
2833d326b23bSrie 		return (0);
28347c478bd9Sstevel@tonic-gate 	}
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	if (unwindph)
28377c478bd9Sstevel@tonic-gate 		PTUNWIND(lmp) = phdr + (unwindph - phdr0);
28387c478bd9Sstevel@tonic-gate 
28397c478bd9Sstevel@tonic-gate 	if (cap)
28407c478bd9Sstevel@tonic-gate 		cap_assign(cap, lmp);
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	return (lmp);
28437c478bd9Sstevel@tonic-gate }
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate /*
28467c478bd9Sstevel@tonic-gate  * Function to correct protection settings.  Segments are all mapped initially
28477c478bd9Sstevel@tonic-gate  * with permissions as given in the segment header.  We need to turn on write
28487c478bd9Sstevel@tonic-gate  * permissions on a text segment if there are any relocations against that
28497c478bd9Sstevel@tonic-gate  * segment, and them turn write permission back off again before returning
28507c478bd9Sstevel@tonic-gate  * control to the user.  This function turns the permission on or off depending
28517c478bd9Sstevel@tonic-gate  * on the value of the argument.
28527c478bd9Sstevel@tonic-gate  */
28537c478bd9Sstevel@tonic-gate int
28545aefb655Srie elf_set_prot(Rt_map *lmp, int permission)
28557c478bd9Sstevel@tonic-gate {
28567c478bd9Sstevel@tonic-gate 	Mmap	*mmaps;
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	/*
28597c478bd9Sstevel@tonic-gate 	 * If this is an allocated image (ie. a relocatable object) we can't
28607c478bd9Sstevel@tonic-gate 	 * mprotect() anything.
28617c478bd9Sstevel@tonic-gate 	 */
28627c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
28637c478bd9Sstevel@tonic-gate 		return (1);
28647c478bd9Sstevel@tonic-gate 
28655aefb655Srie 	DBG_CALL(Dbg_file_prot(lmp, permission));
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++) {
28687c478bd9Sstevel@tonic-gate 		if (mmaps->m_perm & PROT_WRITE)
28697c478bd9Sstevel@tonic-gate 			continue;
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 		if (mprotect(mmaps->m_vaddr, mmaps->m_msize,
28727c478bd9Sstevel@tonic-gate 		    (mmaps->m_perm | permission)) == -1) {
28737c478bd9Sstevel@tonic-gate 			int	err = errno;
28745aefb655Srie 			eprintf(LIST(lmp), ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
28757c478bd9Sstevel@tonic-gate 			    NAME(lmp), strerror(err));
28767c478bd9Sstevel@tonic-gate 			return (0);
28777c478bd9Sstevel@tonic-gate 		}
28787c478bd9Sstevel@tonic-gate 	}
28797c478bd9Sstevel@tonic-gate 	return (1);
28807c478bd9Sstevel@tonic-gate }
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate /*
28837c478bd9Sstevel@tonic-gate  * Build full pathname of shared object from given directory name and filename.
28847c478bd9Sstevel@tonic-gate  */
28857c478bd9Sstevel@tonic-gate static char *
28867c478bd9Sstevel@tonic-gate elf_get_so(const char *dir, const char *file)
28877c478bd9Sstevel@tonic-gate {
28887c478bd9Sstevel@tonic-gate 	static char	pname[PATH_MAX];
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, MSG_ORIG(MSG_FMT_PATH), dir, file);
28917c478bd9Sstevel@tonic-gate 	return (pname);
28927c478bd9Sstevel@tonic-gate }
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate /*
28957c478bd9Sstevel@tonic-gate  * The copy relocation is recorded in a copy structure which will be applied
28967c478bd9Sstevel@tonic-gate  * after all other relocations are carried out.  This provides for copying data
28977c478bd9Sstevel@tonic-gate  * that must be relocated itself (ie. pointers in shared objects).  This
28987c478bd9Sstevel@tonic-gate  * structure also provides a means of binding RTLD_GROUP dependencies to any
28997c478bd9Sstevel@tonic-gate  * copy relocations that have been taken from any group members.
29007c478bd9Sstevel@tonic-gate  *
29017c478bd9Sstevel@tonic-gate  * If the size of the .bss area available for the copy information is not the
29027c478bd9Sstevel@tonic-gate  * same as the source of the data inform the user if we're under ldd(1) control
29037c478bd9Sstevel@tonic-gate  * (this checking was only established in 5.3, so by only issuing an error via
29047c478bd9Sstevel@tonic-gate  * ldd(1) we maintain the standard set by previous releases).
29057c478bd9Sstevel@tonic-gate  */
29067c478bd9Sstevel@tonic-gate int
29077c478bd9Sstevel@tonic-gate elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym,
29087c478bd9Sstevel@tonic-gate     Rt_map *dlmp, const void *dadd)
29097c478bd9Sstevel@tonic-gate {
29107c478bd9Sstevel@tonic-gate 	Rel_copy	rc;
29117c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(rlmp);
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	rc.r_name = name;
29147c478bd9Sstevel@tonic-gate 	rc.r_rsym = rsym;		/* the new reference symbol and its */
29157c478bd9Sstevel@tonic-gate 	rc.r_rlmp = rlmp;		/*	associated link-map */
29167c478bd9Sstevel@tonic-gate 	rc.r_dlmp = dlmp;		/* the defining link-map */
29177c478bd9Sstevel@tonic-gate 	rc.r_dsym = dsym;		/* the original definition */
29187c478bd9Sstevel@tonic-gate 	rc.r_radd = radd;
29197c478bd9Sstevel@tonic-gate 	rc.r_dadd = dadd;
29207c478bd9Sstevel@tonic-gate 
29217c478bd9Sstevel@tonic-gate 	if (rsym->st_size > dsym->st_size)
29227c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)dsym->st_size;
29237c478bd9Sstevel@tonic-gate 	else
29247c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)rsym->st_size;
29257c478bd9Sstevel@tonic-gate 
29267c478bd9Sstevel@tonic-gate 	if (alist_append(&COPY(dlmp), &rc, sizeof (Rel_copy),
29277c478bd9Sstevel@tonic-gate 	    AL_CNT_COPYREL) == 0) {
29287c478bd9Sstevel@tonic-gate 		if (!(lml->lm_flags & LML_FLG_TRC_WARN))
29297c478bd9Sstevel@tonic-gate 			return (0);
29307c478bd9Sstevel@tonic-gate 		else
29317c478bd9Sstevel@tonic-gate 			return (1);
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 	if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) {
29347c478bd9Sstevel@tonic-gate 		if (alist_append(&COPY(rlmp), &dlmp,
29357c478bd9Sstevel@tonic-gate 		    sizeof (Rt_map *), AL_CNT_COPYREL) == 0) {
29367c478bd9Sstevel@tonic-gate 			if (!(lml->lm_flags & LML_FLG_TRC_WARN))
29377c478bd9Sstevel@tonic-gate 				return (0);
29387c478bd9Sstevel@tonic-gate 			else
29397c478bd9Sstevel@tonic-gate 				return (1);
29407c478bd9Sstevel@tonic-gate 		}
29417c478bd9Sstevel@tonic-gate 		FLAGS1(dlmp) |= FL1_RT_COPYTOOK;
29427c478bd9Sstevel@tonic-gate 	}
29437c478bd9Sstevel@tonic-gate 
29447c478bd9Sstevel@tonic-gate 	/*
29457c478bd9Sstevel@tonic-gate 	 * If we are tracing (ldd), warn the user if
29467c478bd9Sstevel@tonic-gate 	 *	1) the size from the reference symbol differs from the
29477c478bd9Sstevel@tonic-gate 	 *	   copy definition. We can only copy as much data as the
29487c478bd9Sstevel@tonic-gate 	 *	   reference (dynamic executables) entry allows.
29497c478bd9Sstevel@tonic-gate 	 *	2) the copy definition has STV_PROTECTED visibility.
29507c478bd9Sstevel@tonic-gate 	 */
29517c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
29527c478bd9Sstevel@tonic-gate 		if (rsym->st_size != dsym->st_size) {
29537c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF),
29545aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
29557c478bd9Sstevel@tonic-gate 			    NAME(rlmp), EC_XWORD(rsym->st_size),
29567c478bd9Sstevel@tonic-gate 			    NAME(dlmp), EC_XWORD(dsym->st_size));
29577c478bd9Sstevel@tonic-gate 			if (rsym->st_size > dsym->st_size)
29587c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA),
29597c478bd9Sstevel@tonic-gate 				    NAME(dlmp));
29607c478bd9Sstevel@tonic-gate 			else
29617c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC),
29627c478bd9Sstevel@tonic-gate 				    NAME(rlmp));
29637c478bd9Sstevel@tonic-gate 		}
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 		if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) {
29667c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_PROT),
29675aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
2968*a953e2b1Srie 			    NAME(dlmp));
29697c478bd9Sstevel@tonic-gate 		}
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate 
29725aefb655Srie 	DBG_CALL(Dbg_reloc_apply_val(lml, ELF_DBG_RTLD, (Xword)radd,
29735aefb655Srie 	    (Xword)rc.r_size));
29747c478bd9Sstevel@tonic-gate 	return (1);
29757c478bd9Sstevel@tonic-gate }
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate /*
29787c478bd9Sstevel@tonic-gate  * Determine the symbol location of an address within a link-map.  Look for
29797c478bd9Sstevel@tonic-gate  * the nearest symbol (whose value is less than or equal to the required
29807c478bd9Sstevel@tonic-gate  * address).  This is the object specific part of dladdr().
29817c478bd9Sstevel@tonic-gate  */
29827c478bd9Sstevel@tonic-gate static void
29837c478bd9Sstevel@tonic-gate elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags)
29847c478bd9Sstevel@tonic-gate {
29857c478bd9Sstevel@tonic-gate 	ulong_t		ndx, cnt, base, _value;
29866221fe92Sab 	Sym		*sym, *_sym = NULL;
29877c478bd9Sstevel@tonic-gate 	const char	*str;
29885343e1b3Sab 	int		_flags;
2989d579eb63Sab 	uint_t		*dynaddr_ndx;
2990d579eb63Sab 	uint_t		dynaddr_n = 0;
2991d579eb63Sab 	ulong_t		value;
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	/*
29949039eeafSab 	 * If SUNWSYMTAB() is non-NULL, then it sees a special version of
29959039eeafSab 	 * the dynsym that starts with any local function symbols that exist in
29969039eeafSab 	 * the library and then moves to the data held in SYMTAB(). In this
29979039eeafSab 	 * case, SUNWSYMSZ tells us how long the symbol table is. The
29989039eeafSab 	 * availability of local function symbols will enhance the results
29999039eeafSab 	 * we can provide.
30009039eeafSab 	 *
3001d579eb63Sab 	 * If SUNWSYMTAB() is non-NULL, then there might also be a
3002d579eb63Sab 	 * SUNWSYMSORT() vector associated with it. SUNWSYMSORT() contains
3003d579eb63Sab 	 * an array of indices into SUNWSYMTAB, sorted by increasing
3004d579eb63Sab 	 * address. We can use this to do an O(log N) search instead of a
3005d579eb63Sab 	 * brute force search.
3006d579eb63Sab 	 *
30079039eeafSab 	 * If SUNWSYMTAB() is NULL, then SYMTAB() references a dynsym that
30089039eeafSab 	 * contains only global symbols. In that case, the length of
30099039eeafSab 	 * the symbol table comes from the nchain field of the related
30109039eeafSab 	 * symbol lookup hash table.
30117c478bd9Sstevel@tonic-gate 	 */
30127c478bd9Sstevel@tonic-gate 	str = STRTAB(lmp);
30139039eeafSab 	if (SUNWSYMSZ(lmp) == NULL) {
30149039eeafSab 		sym = SYMTAB(lmp);
30159039eeafSab 		/*
30169039eeafSab 		 * If we don't have a .hash table there are no symbols
30179039eeafSab 		 * to look at.
30189039eeafSab 		 */
30199039eeafSab 		if (HASH(lmp) == 0)
30209039eeafSab 			return;
30219039eeafSab 		cnt = HASH(lmp)[1];
30229039eeafSab 	} else {
30239039eeafSab 		sym = SUNWSYMTAB(lmp);
30249039eeafSab 		cnt = SUNWSYMSZ(lmp) / SYMENT(lmp);
3025d579eb63Sab 		dynaddr_ndx = SUNWSYMSORT(lmp);
3026d579eb63Sab 		if (dynaddr_ndx != NULL)
3027d579eb63Sab 			dynaddr_n = SUNWSYMSORTSZ(lmp) / SUNWSORTENT(lmp);
30289039eeafSab 	}
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_FIXED)
30317c478bd9Sstevel@tonic-gate 		base = 0;
30327c478bd9Sstevel@tonic-gate 	else
30337c478bd9Sstevel@tonic-gate 		base = ADDR(lmp);
30347c478bd9Sstevel@tonic-gate 
3035d579eb63Sab 	if (dynaddr_n > 0) {		/* Binary search */
3036d579eb63Sab 		long	low = 0, low_bnd;
3037d579eb63Sab 		long	high = dynaddr_n - 1, high_bnd;
3038d579eb63Sab 		long	mid;
3039d579eb63Sab 		Sym	*mid_sym;
30407c478bd9Sstevel@tonic-gate 
30419039eeafSab 		/*
3042d579eb63Sab 		 * Note that SUNWSYMSORT only contains symbols types that
3043d579eb63Sab 		 * supply memory addresses, so there's no need to check and
3044d579eb63Sab 		 * filter out any other types.
30459039eeafSab 		 */
3046d579eb63Sab 		low_bnd = low;
3047d579eb63Sab 		high_bnd = high;
3048d579eb63Sab 		while (low <= high) {
3049d579eb63Sab 			mid = (low + high) / 2;
3050d579eb63Sab 			mid_sym = &sym[dynaddr_ndx[mid]];
3051d579eb63Sab 			value = mid_sym->st_value + base;
3052d579eb63Sab 			if (addr < value) {
3053d579eb63Sab 				if ((sym[dynaddr_ndx[high]].st_value + base) >=
3054d579eb63Sab 				    addr)
3055d579eb63Sab 					high_bnd = high;
3056d579eb63Sab 				high = mid - 1;
3057d579eb63Sab 			} else if (addr > value) {
3058d579eb63Sab 				if ((sym[dynaddr_ndx[low]].st_value + base) <=
3059d579eb63Sab 				    addr)
3060d579eb63Sab 					low_bnd = low;
3061d579eb63Sab 				low = mid + 1;
3062d579eb63Sab 			} else {
3063d579eb63Sab 				_sym = mid_sym;
3064d579eb63Sab 				_value = value;
3065d579eb63Sab 				break;
3066d579eb63Sab 			}
3067d579eb63Sab 		}
3068d579eb63Sab 		/*
3069d579eb63Sab 		 * If the above didn't find it exactly, then we must
3070d579eb63Sab 		 * return the closest symbol with a value that doesn't
3071d579eb63Sab 		 * exceed the one we are looking for. If that symbol exists,
3072d579eb63Sab 		 * it will lie in the range bounded by low_bnd and
3073d579eb63Sab 		 * high_bnd. This is a linear search, but a short one.
3074d579eb63Sab 		 */
3075d579eb63Sab 		if (_sym == NULL) {
3076d579eb63Sab 			for (mid = low_bnd; mid <= high_bnd; mid++) {
3077d579eb63Sab 				mid_sym = &sym[dynaddr_ndx[mid]];
3078d579eb63Sab 				value = mid_sym->st_value + base;
3079d579eb63Sab 				if (addr >= value) {
3080d579eb63Sab 					_sym = mid_sym;
3081d579eb63Sab 					_value = value;
3082d579eb63Sab 				} else {
3083d579eb63Sab 					break;
3084d579eb63Sab 				}
3085d579eb63Sab 			}
3086d579eb63Sab 		}
3087d579eb63Sab 	} else {			/* Linear search */
3088d579eb63Sab 		for (_value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) {
3089d579eb63Sab 			/*
3090d579eb63Sab 			 * Skip expected symbol types that are not functions
3091d579eb63Sab 			 * or data:
3092d579eb63Sab 			 *	- A symbol table starts with an undefined symbol
3093d579eb63Sab 			 *		in slot 0. If we are using SUNWSYMTAB(),
3094d579eb63Sab 			 *		there will be a second undefined symbol
3095d579eb63Sab 			 *		right before the globals.
3096d579eb63Sab 			 *	- The local part of SUNWSYMTAB() contains a
3097d579eb63Sab 			 *		series of function symbols. Each section
3098d579eb63Sab 			 *		starts with an initial STT_FILE symbol.
3099d579eb63Sab 			 */
3100d579eb63Sab 			if ((sym->st_shndx == SHN_UNDEF) ||
3101d579eb63Sab 			    (ELF_ST_TYPE(sym->st_info) == STT_FILE))
3102d579eb63Sab 				continue;
31037c478bd9Sstevel@tonic-gate 
3104d579eb63Sab 			value = sym->st_value + base;
3105d579eb63Sab 			if (value > addr)
3106d579eb63Sab 				continue;
3107d579eb63Sab 			if (value < _value)
3108d579eb63Sab 				continue;
31097c478bd9Sstevel@tonic-gate 
3110d579eb63Sab 			_sym = sym;
3111d579eb63Sab 			_value = value;
31127c478bd9Sstevel@tonic-gate 
3113d579eb63Sab 			/*
3114d579eb63Sab 			 * Note, because we accept local and global symbols
3115d579eb63Sab 			 * we could find a section symbol that matches the
3116d579eb63Sab 			 * associated address, which means that the symbol
3117d579eb63Sab 			 * name will be null.  In this case continue the
3118d579eb63Sab 			 * search in case we can find a global symbol of
3119d579eb63Sab 			 * the same value.
3120d579eb63Sab 			 */
3121d579eb63Sab 			if ((value == addr) &&
3122d579eb63Sab 			    (ELF_ST_TYPE(sym->st_info) != STT_SECTION))
3123d579eb63Sab 				break;
3124d579eb63Sab 		}
31257c478bd9Sstevel@tonic-gate 	}
31267c478bd9Sstevel@tonic-gate 
31275343e1b3Sab 	_flags = flags & RTLD_DL_MASK;
31287c478bd9Sstevel@tonic-gate 	if (_sym) {
31297c478bd9Sstevel@tonic-gate 		if (_flags == RTLD_DL_SYMENT)
31307c478bd9Sstevel@tonic-gate 			*info = (void *)_sym;
31317c478bd9Sstevel@tonic-gate 		else if (_flags == RTLD_DL_LINKMAP)
31327c478bd9Sstevel@tonic-gate 			*info = (void *)lmp;
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 		dlip->dli_sname = str + _sym->st_name;
31357c478bd9Sstevel@tonic-gate 		dlip->dli_saddr = (void *)_value;
31365343e1b3Sab 	} else {
31375343e1b3Sab 		/*
31385343e1b3Sab 		 * addr lies between the beginning of the mapped segment and
31395343e1b3Sab 		 * the first global symbol. We have no symbol to return
31405343e1b3Sab 		 * and the caller requires one. We use _START_, the base
31415343e1b3Sab 		 * address of the mapping.
31425343e1b3Sab 		 */
31435343e1b3Sab 
31445343e1b3Sab 		if (_flags == RTLD_DL_SYMENT) {
31455343e1b3Sab 			/*
31465343e1b3Sab 			 * An actual symbol struct is needed, so we
31475343e1b3Sab 			 * construct one for _START_. To do this in a
31485343e1b3Sab 			 * fully accurate way requires a different symbol
31495343e1b3Sab 			 * for each mapped segment. This requires the
31505343e1b3Sab 			 * use of dynamic memory and a mutex. That's too much
31515343e1b3Sab 			 * plumbing for a fringe case of limited importance.
31525343e1b3Sab 			 *
31535343e1b3Sab 			 * Fortunately, we can simplify:
31545343e1b3Sab 			 *    - Only the st_size and st_info fields are useful
31555343e1b3Sab 			 *	outside of the linker internals. The others
31565343e1b3Sab 			 *	reference things that outside code cannot see,
31575343e1b3Sab 			 *	and can be set to 0.
31585343e1b3Sab 			 *    - It's just a label and there is no size
31595343e1b3Sab 			 *	to report. So, the size should be 0.
31605343e1b3Sab 			 * This means that only st_info needs a non-zero
31615343e1b3Sab 			 * (constant) value. A static struct will suffice.
31625343e1b3Sab 			 * It must be const (readonly) so the caller can't
31635343e1b3Sab 			 * change its meaning for subsequent callers.
31645343e1b3Sab 			 */
31655343e1b3Sab 			static const Sym fsym = { 0, 0, 0,
31665343e1b3Sab 				ELF_ST_INFO(STB_LOCAL, STT_OBJECT) };
31675343e1b3Sab 			*info = (void *) &fsym;
31685343e1b3Sab 		}
31695343e1b3Sab 
31705343e1b3Sab 		dlip->dli_sname = MSG_ORIG(MSG_SYM_START);
31715343e1b3Sab 		dlip->dli_saddr = (void *) ADDR(lmp);
31727c478bd9Sstevel@tonic-gate 	}
31737c478bd9Sstevel@tonic-gate }
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate static void
31769a411307Srie elf_lazy_cleanup(Alist *alp)
31777c478bd9Sstevel@tonic-gate {
31789a411307Srie 	Rt_map	**lmpp;
31799a411307Srie 	Aliste	off;
31807c478bd9Sstevel@tonic-gate 
31817c478bd9Sstevel@tonic-gate 	/*
31827c478bd9Sstevel@tonic-gate 	 * Cleanup any link-maps added to this dynamic list and free it.
31837c478bd9Sstevel@tonic-gate 	 */
31847c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alp, off, lmpp))
31857c478bd9Sstevel@tonic-gate 		FLAGS(*lmpp) &= ~FLG_RT_DLSYM;
31867c478bd9Sstevel@tonic-gate 	free(alp);
31877c478bd9Sstevel@tonic-gate }
31887c478bd9Sstevel@tonic-gate 
31897c478bd9Sstevel@tonic-gate /*
31907c478bd9Sstevel@tonic-gate  * This routine is called upon to search for a symbol from the dependencies of
31917c478bd9Sstevel@tonic-gate  * the initial link-map.  To maintain lazy loadings goal of reducing the number
31927c478bd9Sstevel@tonic-gate  * of objects mapped, any symbol search is first carried out using the objects
31937c478bd9Sstevel@tonic-gate  * that already exist in the process (either on a link-map list or handle).
31947c478bd9Sstevel@tonic-gate  * If a symbol can't be found, and lazy dependencies are still pending, this
31957c478bd9Sstevel@tonic-gate  * routine loads the dependencies in an attempt to locate the symbol.
31967c478bd9Sstevel@tonic-gate  *
31977c478bd9Sstevel@tonic-gate  * Only new objects are inspected as we will have already inspected presently
31987c478bd9Sstevel@tonic-gate  * loaded objects before calling this routine.  However, a new object may not
31997c478bd9Sstevel@tonic-gate  * be new - although the di_lmp might be zero, the object may have been mapped
32007c478bd9Sstevel@tonic-gate  * as someone elses dependency.  Thus there's a possibility of some symbol
32017c478bd9Sstevel@tonic-gate  * search duplication.
32027c478bd9Sstevel@tonic-gate  */
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate Sym *
32057c478bd9Sstevel@tonic-gate elf_lazy_find_sym(Slookup *slp, Rt_map **_lmp, uint_t *binfo)
32067c478bd9Sstevel@tonic-gate {
32077c478bd9Sstevel@tonic-gate 	Sym		*sym = 0;
32087c478bd9Sstevel@tonic-gate 	Alist *		alist = 0;
32097c478bd9Sstevel@tonic-gate 	Aliste		off;
32107c478bd9Sstevel@tonic-gate 	Rt_map **	lmpp, *	lmp = slp->sl_imap;
32117c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	if (alist_append(&alist, &lmp, sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0)
32147c478bd9Sstevel@tonic-gate 		return (0);
32157c478bd9Sstevel@tonic-gate 	FLAGS(lmp) |= FLG_RT_DLSYM;
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alist, off, lmpp)) {
32187c478bd9Sstevel@tonic-gate 		uint_t	cnt = 0;
32197c478bd9Sstevel@tonic-gate 		Slookup	sl = *slp;
32207c478bd9Sstevel@tonic-gate 		Dyninfo	*dip;
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate 		/*
32237c478bd9Sstevel@tonic-gate 		 * Loop through the DT_NEEDED entries examining each object for
32247c478bd9Sstevel@tonic-gate 		 * the symbol.  If the symbol is not found the object is in turn
32257c478bd9Sstevel@tonic-gate 		 * added to the alist, so that its DT_NEEDED entires may be
32267c478bd9Sstevel@tonic-gate 		 * examined.
32277c478bd9Sstevel@tonic-gate 		 */
32287c478bd9Sstevel@tonic-gate 		lmp = *lmpp;
32297c478bd9Sstevel@tonic-gate 		for (dip = DYNINFO(lmp); cnt < DYNINFOCNT(lmp); cnt++, dip++) {
32307c478bd9Sstevel@tonic-gate 			Rt_map *nlmp;
32317c478bd9Sstevel@tonic-gate 
32327c478bd9Sstevel@tonic-gate 			if (((dip->di_flags & FLG_DI_NEEDED) == 0) ||
32337c478bd9Sstevel@tonic-gate 			    dip->di_info)
32347c478bd9Sstevel@tonic-gate 				continue;
32357c478bd9Sstevel@tonic-gate 
32367c478bd9Sstevel@tonic-gate 			/*
32377c478bd9Sstevel@tonic-gate 			 * If this entry defines a lazy dependency try loading
32387c478bd9Sstevel@tonic-gate 			 * it.  If the file can't be loaded, consider this
32397c478bd9Sstevel@tonic-gate 			 * non-fatal and continue the search (lazy loaded
32407c478bd9Sstevel@tonic-gate 			 * dependencies need not exist and their loading should
32417c478bd9Sstevel@tonic-gate 			 * only be fatal if called from a relocation).
32427c478bd9Sstevel@tonic-gate 			 *
32437c478bd9Sstevel@tonic-gate 			 * If the file is already loaded and relocated we must
32447c478bd9Sstevel@tonic-gate 			 * still inspect it for symbols, even though it might
32457c478bd9Sstevel@tonic-gate 			 * have already been searched.  This lazy load operation
32467c478bd9Sstevel@tonic-gate 			 * might have promoted the permissions of the object,
32477c478bd9Sstevel@tonic-gate 			 * and thus made the object applicable for this symbol
32487c478bd9Sstevel@tonic-gate 			 * search, whereas before the object might have been
32497c478bd9Sstevel@tonic-gate 			 * skipped.
32507c478bd9Sstevel@tonic-gate 			 */
32517c478bd9Sstevel@tonic-gate 			if ((nlmp = elf_lazy_load(lmp, cnt, name)) == 0)
32527c478bd9Sstevel@tonic-gate 				continue;
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 			/*
32557c478bd9Sstevel@tonic-gate 			 * If this object isn't yet a part of the dynamic list
32567c478bd9Sstevel@tonic-gate 			 * then inspect it for the symbol.  If the symbol isn't
32577c478bd9Sstevel@tonic-gate 			 * found add the object to the dynamic list so that we
32587c478bd9Sstevel@tonic-gate 			 * can inspect its dependencies.
32597c478bd9Sstevel@tonic-gate 			 */
32607c478bd9Sstevel@tonic-gate 			if (FLAGS(nlmp) & FLG_RT_DLSYM)
32617c478bd9Sstevel@tonic-gate 				continue;
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 			sl.sl_imap = nlmp;
32647c478bd9Sstevel@tonic-gate 			if (sym = LM_LOOKUP_SYM(sl.sl_cmap)(&sl, _lmp, binfo))
32657c478bd9Sstevel@tonic-gate 				break;
32667c478bd9Sstevel@tonic-gate 
32677c478bd9Sstevel@tonic-gate 			/*
32687c478bd9Sstevel@tonic-gate 			 * Some dlsym() operations are already traversing a
32697c478bd9Sstevel@tonic-gate 			 * link-map (dlopen(0)), and thus there's no need to
32707c478bd9Sstevel@tonic-gate 			 * build our own dynamic dependency list.
32717c478bd9Sstevel@tonic-gate 			 */
32727c478bd9Sstevel@tonic-gate 			if ((sl.sl_flags & LKUP_NODESCENT) == 0) {
32737c478bd9Sstevel@tonic-gate 				if (alist_append(&alist, &nlmp,
32747c478bd9Sstevel@tonic-gate 				    sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0) {
32757c478bd9Sstevel@tonic-gate 					elf_lazy_cleanup(alist);
32767c478bd9Sstevel@tonic-gate 					return (0);
32777c478bd9Sstevel@tonic-gate 				}
32787c478bd9Sstevel@tonic-gate 				FLAGS(nlmp) |= FLG_RT_DLSYM;
32797c478bd9Sstevel@tonic-gate 			}
32807c478bd9Sstevel@tonic-gate 		}
32817c478bd9Sstevel@tonic-gate 		if (sym)
32827c478bd9Sstevel@tonic-gate 			break;
32837c478bd9Sstevel@tonic-gate 	}
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 	elf_lazy_cleanup(alist);
32867c478bd9Sstevel@tonic-gate 	return (sym);
32877c478bd9Sstevel@tonic-gate }
32887c478bd9Sstevel@tonic-gate 
32897c478bd9Sstevel@tonic-gate /*
32907c478bd9Sstevel@tonic-gate  * Warning message for bad r_offset.
32917c478bd9Sstevel@tonic-gate  */
32927c478bd9Sstevel@tonic-gate void
32937c478bd9Sstevel@tonic-gate elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset,
32947c478bd9Sstevel@tonic-gate     ulong_t rsymndx)
32957c478bd9Sstevel@tonic-gate {
32967c478bd9Sstevel@tonic-gate 	const char	*name = (char *)0;
32975aefb655Srie 	Lm_list		*lml = LIST(lmp);
32987c478bd9Sstevel@tonic-gate 	int		trace;
32997c478bd9Sstevel@tonic-gate 
33005aefb655Srie 	if ((lml->lm_flags & LML_FLG_TRC_ENABLE) &&
33017c478bd9Sstevel@tonic-gate 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
33025aefb655Srie 	    (lml->lm_flags & LML_FLG_TRC_VERBOSE)))
33037c478bd9Sstevel@tonic-gate 		trace = 1;
33047c478bd9Sstevel@tonic-gate 	else
33057c478bd9Sstevel@tonic-gate 		trace = 0;
33067c478bd9Sstevel@tonic-gate 
33075aefb655Srie 	if ((trace == 0) && (DBG_ENABLED == 0))
33087c478bd9Sstevel@tonic-gate 		return;
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	if (rsymndx) {
33117c478bd9Sstevel@tonic-gate 		Sym	*symref = (Sym *)((ulong_t)SYMTAB(lmp) +
3312*a953e2b1Srie 		    (rsymndx * SYMENT(lmp)));
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(symref->st_info) != STB_LOCAL)
33157c478bd9Sstevel@tonic-gate 			name = (char *)(STRTAB(lmp) + symref->st_name);
33167c478bd9Sstevel@tonic-gate 	}
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	if (name == 0)
33197c478bd9Sstevel@tonic-gate 		name = MSG_ORIG(MSG_STR_EMPTY);
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	if (trace) {
33227c478bd9Sstevel@tonic-gate 		const char *rstr;
33237c478bd9Sstevel@tonic-gate 
33245aefb655Srie 		rstr = _conv_reloc_type((uint_t)rtype);
33257c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name,
33267c478bd9Sstevel@tonic-gate 		    EC_ADDR(roffset));
33277c478bd9Sstevel@tonic-gate 		return;
33287c478bd9Sstevel@tonic-gate 	}
33297c478bd9Sstevel@tonic-gate 
33305aefb655Srie 	Dbg_reloc_error(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel, name);
33317c478bd9Sstevel@tonic-gate }
3332d326b23bSrie 
3333d326b23bSrie /*
3334d326b23bSrie  * Resolve a static TLS relocation.
3335d326b23bSrie  */
3336d326b23bSrie long
3337d326b23bSrie elf_static_tls(Rt_map *lmp, Sym *sym, void *rel, uchar_t rtype, char *name,
3338d326b23bSrie     ulong_t roffset, long value)
3339d326b23bSrie {
3340d326b23bSrie 	Lm_list	*lml = LIST(lmp);
3341d326b23bSrie 
3342d326b23bSrie 	/*
3343d326b23bSrie 	 * Relocations against a static TLS block have limited support once
3344d326b23bSrie 	 * process initialization has completed.  Any error condition should be
3345d326b23bSrie 	 * discovered by testing for DF_STATIC_TLS as part of loading an object,
3346d326b23bSrie 	 * however individual relocations are tested in case the dynamic flag
3347d326b23bSrie 	 * had not been set when this object was built.
3348d326b23bSrie 	 */
3349d326b23bSrie 	if (PTTLS(lmp) == 0) {
3350d326b23bSrie 		DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
3351d326b23bSrie 		    M_REL_SHT_TYPE, rel, NULL, name));
3352d326b23bSrie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
3353d326b23bSrie 		    _conv_reloc_type((uint_t)rtype), NAME(lmp),
3354d326b23bSrie 		    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
3355d326b23bSrie 		return (0);
3356d326b23bSrie 	}
3357d326b23bSrie 
3358d326b23bSrie 	/*
3359d326b23bSrie 	 * If no static TLS has been set aside for this object, determine if
3360d326b23bSrie 	 * any can be obtained.  Enforce that any object using static TLS is
3361d326b23bSrie 	 * non-deletable.
3362d326b23bSrie 	 */
3363d326b23bSrie 	if (TLSSTATOFF(lmp) == 0) {
3364d326b23bSrie 		FLAGS1(lmp) |= FL1_RT_TLSSTAT;
3365d326b23bSrie 		MODE(lmp) |= RTLD_NODELETE;
3366d326b23bSrie 
3367d326b23bSrie 		if (tls_assign(lml, lmp, PTTLS(lmp)) == 0) {
3368d326b23bSrie 			DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
3369d326b23bSrie 			    M_REL_SHT_TYPE, rel, NULL, name));
3370d326b23bSrie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
3371d326b23bSrie 			    _conv_reloc_type((uint_t)rtype), NAME(lmp),
3372d326b23bSrie 			    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
3373d326b23bSrie 			return (0);
3374d326b23bSrie 		}
3375d326b23bSrie 	}
3376d326b23bSrie 
3377d326b23bSrie 	/*
3378d326b23bSrie 	 * Typically, a static TLS offset is maintained as a symbols value.
3379d326b23bSrie 	 * For local symbols that are not apart of the dynamic symbol table,
3380d326b23bSrie 	 * the TLS relocation points to a section symbol, and the static TLS
3381d326b23bSrie 	 * offset was deposited in the associated GOT table.  Make sure the GOT
3382d326b23bSrie 	 * is cleared, so that the value isn't reused in do_reloc().
3383d326b23bSrie 	 */
3384d326b23bSrie 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
3385d326b23bSrie 		if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION)) {
3386d326b23bSrie 			value = *(long *)roffset;
3387d326b23bSrie 			*(long *)roffset = 0;
3388d326b23bSrie 		} else {
3389d326b23bSrie 			value = sym->st_value;
3390d326b23bSrie 		}
3391d326b23bSrie 	}
3392d326b23bSrie 	return (-(TLSSTATOFF(lmp) - value));
3393d326b23bSrie }
3394