xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/elf.c (revision 9a411307)
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  *
26*9a411307Srie  * 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 == '/') &&
1367c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
1377c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD_64)) == 0)) ||
1387c478bd9Sstevel@tonic-gate #else
1397c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD)) == 0)) ||
1407c478bd9Sstevel@tonic-gate #endif
1417c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_FIL_RTLD)) == 0)) {
1427c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
1437c478bd9Sstevel@tonic-gate 
1445aefb655Srie 		DBG_CALL(Dbg_file_fixname(LIST(clmp), name,
1455aefb655Srie 		    MSG_ORIG(MSG_PTH_LIBSYS)));
1467c478bd9Sstevel@tonic-gate 		if (((pnp = calloc(sizeof (Pnode), 1)) == 0) ||
1477c478bd9Sstevel@tonic-gate 		    ((pnp->p_name = strdup(MSG_ORIG(MSG_PTH_LIBSYS))) == 0)) {
1487c478bd9Sstevel@tonic-gate 			if (pnp)
1497c478bd9Sstevel@tonic-gate 				free(pnp);
1507c478bd9Sstevel@tonic-gate 			return (0);
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 		pnp->p_len = MSG_PTH_LIBSYS_SIZE;
1537c478bd9Sstevel@tonic-gate 		pnp->p_orig = (orig & PN_SER_MASK);
1547c478bd9Sstevel@tonic-gate 		return (pnp);
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	return (expand_paths(clmp, name, orig, 0));
1587c478bd9Sstevel@tonic-gate }
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Determine if we have been given an ELF file and if so determine if the file
1627c478bd9Sstevel@tonic-gate  * is compatible.  Returns 1 if true, else 0 and sets the reject descriptor
1637c478bd9Sstevel@tonic-gate  * with associated error information.
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate static int
1667c478bd9Sstevel@tonic-gate elf_are_u(Rej_desc *rej)
1677c478bd9Sstevel@tonic-gate {
1687c478bd9Sstevel@tonic-gate 	Ehdr	*ehdr;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Determine if we're an elf file.  If not simply return, we don't set
1727c478bd9Sstevel@tonic-gate 	 * any rejection information as this test allows use to scroll through
1737c478bd9Sstevel@tonic-gate 	 * the objects we support (ELF, AOUT).
1747c478bd9Sstevel@tonic-gate 	 */
1757c478bd9Sstevel@tonic-gate 	if (fmap->fm_fsize < sizeof (Ehdr) ||
1767c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG0] != ELFMAG0 ||
1777c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG1] != ELFMAG1 ||
1787c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG2] != ELFMAG2 ||
1797c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG3] != ELFMAG3) {
1807c478bd9Sstevel@tonic-gate 		return (0);
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Check class and encoding.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	/* LINTED */
1877c478bd9Sstevel@tonic-gate 	ehdr = (Ehdr *)fmap->fm_maddr;
1887c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_CLASS] != M_CLASS) {
1897c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_CLASS;
1907c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_CLASS];
1917c478bd9Sstevel@tonic-gate 		return (0);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_DATA] != M_DATA) {
1947c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
1957c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
1967c478bd9Sstevel@tonic-gate 		return (0);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC) &&
1997c478bd9Sstevel@tonic-gate 	    (ehdr->e_type != ET_DYN)) {
2007c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_TYPE;
2017c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_type;
2027c478bd9Sstevel@tonic-gate 		return (0);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	/*
2067c478bd9Sstevel@tonic-gate 	 * Verify machine specific flags, and hardware capability requirements.
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	if ((elf_mach_flags_check(rej, ehdr) == 0) ||
2097c478bd9Sstevel@tonic-gate 	    ((rtld_flags2 & RT_FL2_HWCAP) && (hwcap_check(rej, ehdr) == 0)))
2107c478bd9Sstevel@tonic-gate 		return (0);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/*
2137c478bd9Sstevel@tonic-gate 	 * Verify ELF version.  ??? is this too restrictive ???
2147c478bd9Sstevel@tonic-gate 	 */
2157c478bd9Sstevel@tonic-gate 	if (ehdr->e_version > EV_CURRENT) {
2167c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
2177c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
2187c478bd9Sstevel@tonic-gate 		return (0);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	return (1);
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * The runtime linker employs lazy loading to provide the libraries needed for
2257c478bd9Sstevel@tonic-gate  * debugging, preloading .o's and dldump().  As these are seldom used, the
2267c478bd9Sstevel@tonic-gate  * standard startup of ld.so.1 doesn't initialize all the information necessary
2277c478bd9Sstevel@tonic-gate  * to perform plt relocation on ld.so.1's link-map.  The first time lazy loading
2287c478bd9Sstevel@tonic-gate  * is called we get here to perform these initializations:
2297c478bd9Sstevel@tonic-gate  *
2307c478bd9Sstevel@tonic-gate  *  o	elf_needed() is called to set up the DYNINFO() indexes for each lazy
2317c478bd9Sstevel@tonic-gate  *	dependency.  Typically, for all other objects, this is called during
2327c478bd9Sstevel@tonic-gate  *	analyze_so(), but as ld.so.1 is set-contained we skip this processing.
2337c478bd9Sstevel@tonic-gate  *
2347c478bd9Sstevel@tonic-gate  *  o	For intel, ld.so.1's JMPSLOT relocations need relative updates. These
2357c478bd9Sstevel@tonic-gate  *	are by default skipped thus delaying all relative relocation processing
2367c478bd9Sstevel@tonic-gate  * 	on every invocation of ld.so.1.
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate int
2397c478bd9Sstevel@tonic-gate elf_rtld_load()
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	Lm_list	*lml = &lml_rtld;
2427c478bd9Sstevel@tonic-gate 	Rt_map	*lmp = lml->lm_head;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_PLTREL)
2457c478bd9Sstevel@tonic-gate 		return (1);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/*
2487c478bd9Sstevel@tonic-gate 	 * As we need to refer to the DYNINFO() information, insure that it has
2497c478bd9Sstevel@tonic-gate 	 * been initialized.
2507c478bd9Sstevel@tonic-gate 	 */
2517c478bd9Sstevel@tonic-gate 	if (elf_needed(lml, ALO_DATA, lmp) == 0)
2527c478bd9Sstevel@tonic-gate 		return (0);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate #if	defined(i386)
2557c478bd9Sstevel@tonic-gate 	/*
2567c478bd9Sstevel@tonic-gate 	 * This is a kludge to give ld.so.1 a performance benefit on i386.
2577c478bd9Sstevel@tonic-gate 	 * It's based around two factors.
2587c478bd9Sstevel@tonic-gate 	 *
2597c478bd9Sstevel@tonic-gate 	 *  o	JMPSLOT relocations (PLT's) actually need a relative relocation
2607c478bd9Sstevel@tonic-gate 	 *	applied to the GOT entry so that they can find PLT0.
2617c478bd9Sstevel@tonic-gate 	 *
2627c478bd9Sstevel@tonic-gate 	 *  o	ld.so.1 does not exercise *any* PLT's before it has made a call
2637c478bd9Sstevel@tonic-gate 	 *	to elf_lazy_load().  This is because all dynamic dependencies
2647c478bd9Sstevel@tonic-gate 	 * 	are recorded as lazy dependencies.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	(void) elf_reloc_relacount((ulong_t)JMPREL(lmp),
2677c478bd9Sstevel@tonic-gate 	    (ulong_t)(PLTRELSZ(lmp) / RELENT(lmp)), (ulong_t)RELENT(lmp),
2687c478bd9Sstevel@tonic-gate 	    (ulong_t)ADDR(lmp));
2697c478bd9Sstevel@tonic-gate #endif
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	lml->lm_flags |= LML_FLG_PLTREL;
2727c478bd9Sstevel@tonic-gate 	return (1);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Lazy load an object.
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate Rt_map *
2797c478bd9Sstevel@tonic-gate elf_lazy_load(Rt_map *clmp, uint_t ndx, const char *sym)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp, *hlmp;
2827c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(clmp)[ndx];
2837c478bd9Sstevel@tonic-gate 	uint_t		flags = 0;
2847c478bd9Sstevel@tonic-gate 	Pnode		*pnp;
2857c478bd9Sstevel@tonic-gate 	const char	*name;
2867c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
2877c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc;
2887c478bd9Sstevel@tonic-gate 	Aliste		lmco;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * If this dependency has already been processed, we're done.
2927c478bd9Sstevel@tonic-gate 	 */
2937c478bd9Sstevel@tonic-gate 	if (((nlmp = (Rt_map *)dip->di_info) != 0) ||
2947c478bd9Sstevel@tonic-gate 	    (dip->di_flags & FLG_DI_PROCESSD))
2957c478bd9Sstevel@tonic-gate 		return (nlmp);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/*
2987c478bd9Sstevel@tonic-gate 	 * Determine the initial dependency name, and indicate that this
2997c478bd9Sstevel@tonic-gate 	 * dependencies processing has initiated.
3007c478bd9Sstevel@tonic-gate 	 */
3017c478bd9Sstevel@tonic-gate 	name = STRTAB(clmp) + DYN(clmp)[ndx].d_un.d_val;
3025aefb655Srie 	DBG_CALL(Dbg_file_lazyload(clmp, name, sym));
3037c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_ENABLE)
3047c478bd9Sstevel@tonic-gate 		dip->di_flags |= FLG_DI_PROCESSD;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	if (dip->di_flags & FLG_DI_GROUP)
3077c478bd9Sstevel@tonic-gate 		flags |= (FLG_RT_SETGROUP | FLG_RT_HANDLE);
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	/*
3107c478bd9Sstevel@tonic-gate 	 * Expand the requested name if necessary.
3117c478bd9Sstevel@tonic-gate 	 */
3127c478bd9Sstevel@tonic-gate 	if ((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0)
3137c478bd9Sstevel@tonic-gate 		return (0);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/*
3167c478bd9Sstevel@tonic-gate 	 * Provided the object on the head of the link-map has completed its
3177c478bd9Sstevel@tonic-gate 	 * relocation, create a new link-map control list for this request.
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 	hlmp = lml->lm_head;
3207c478bd9Sstevel@tonic-gate 	if (FLAGS(hlmp) & FLG_RT_RELOCED) {
3217c478bd9Sstevel@tonic-gate 		if ((lmc = alist_append(&(lml->lm_lists), 0, sizeof (Lm_cntl),
3227c478bd9Sstevel@tonic-gate 		    AL_CNT_LMLISTS)) == 0) {
3237c478bd9Sstevel@tonic-gate 			remove_pnode(pnp);
3247c478bd9Sstevel@tonic-gate 			return (0);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		lmco = (Aliste)((char *)lmc - (char *)lml->lm_lists);
3277c478bd9Sstevel@tonic-gate 	} else {
3287c478bd9Sstevel@tonic-gate 		lmc = 0;
3297c478bd9Sstevel@tonic-gate 		lmco = ALO_DATA;
3307c478bd9Sstevel@tonic-gate 	}
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Load the associated object.
3347c478bd9Sstevel@tonic-gate 	 */
3357c478bd9Sstevel@tonic-gate 	dip->di_info = nlmp =
3367c478bd9Sstevel@tonic-gate 	    load_one(lml, lmco, pnp, clmp, MODE(clmp), flags, 0);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/*
3397c478bd9Sstevel@tonic-gate 	 * Remove any expanded pathname infrastructure.  Reduce the pending lazy
3407c478bd9Sstevel@tonic-gate 	 * dependency count of the caller, together with the link-map lists
3417c478bd9Sstevel@tonic-gate 	 * count of objects that still have lazy dependencies pending.
3427c478bd9Sstevel@tonic-gate 	 */
3437c478bd9Sstevel@tonic-gate 	remove_pnode(pnp);
3447c478bd9Sstevel@tonic-gate 	if (--LAZY(clmp) == 0)
3457c478bd9Sstevel@tonic-gate 		LIST(clmp)->lm_lazy--;
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	/*
3487c478bd9Sstevel@tonic-gate 	 * Finish processing the objects associated with this request.
3497c478bd9Sstevel@tonic-gate 	 */
3507c478bd9Sstevel@tonic-gate 	if (nlmp && ((analyze_lmc(lml, lmco, nlmp) == 0) ||
3517c478bd9Sstevel@tonic-gate 	    (relocate_lmc(lml, lmco, nlmp) == 0)))
3527c478bd9Sstevel@tonic-gate 		dip->di_info = nlmp = 0;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * If the dependency has been successfully processed, and it is part of
3567c478bd9Sstevel@tonic-gate 	 * a link-map control list that is equivalent, or less, that the callers
3577c478bd9Sstevel@tonic-gate 	 * control list, create an association between the caller and this
3587c478bd9Sstevel@tonic-gate 	 * dependency.  If this dependency isn't yet apart of the callers
3597c478bd9Sstevel@tonic-gate 	 * link-map control list, then it is still apart of a list that is being
3607c478bd9Sstevel@tonic-gate 	 * relocated.  As the relocation of an object on this list might still
3617c478bd9Sstevel@tonic-gate 	 * fail, we can't yet bind the caller to this object.  To do so, would
3627c478bd9Sstevel@tonic-gate 	 * be locking the object so that it couldn't be deleted.  Mark this
3637c478bd9Sstevel@tonic-gate 	 * object as free, and it will be reprocessed when this dependency is
3647c478bd9Sstevel@tonic-gate 	 * next referenced.
3657c478bd9Sstevel@tonic-gate 	 */
3667c478bd9Sstevel@tonic-gate 	if (nlmp) {
3677c478bd9Sstevel@tonic-gate 		if (CNTL(nlmp) <= CNTL(clmp)) {
3687c478bd9Sstevel@tonic-gate 			if (bind_one(clmp, nlmp, BND_NEEDED) == 0)
3697c478bd9Sstevel@tonic-gate 				dip->di_info = nlmp = 0;
3707c478bd9Sstevel@tonic-gate 		} else {
3717c478bd9Sstevel@tonic-gate 			dip->di_info = 0;
3727c478bd9Sstevel@tonic-gate 			dip->di_flags &= ~FLG_DI_PROCESSD;
3737c478bd9Sstevel@tonic-gate 			if (LAZY(clmp)++ == 0)
3747c478bd9Sstevel@tonic-gate 				LIST(clmp)->lm_lazy++;
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * After a successful load, any objects collected on the new link-map
3807c478bd9Sstevel@tonic-gate 	 * control list will have been moved to the callers link-map control
3817c478bd9Sstevel@tonic-gate 	 * list.  This control list can now be deleted.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	if (lmc) {
3847c478bd9Sstevel@tonic-gate 		if (nlmp == 0)
3857c478bd9Sstevel@tonic-gate 			remove_incomplete(lml, lmco);
3867c478bd9Sstevel@tonic-gate 		remove_cntl(lml, lmco);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	return (nlmp);
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * Return the entry point of the ELF executable.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate static ulong_t
3977c478bd9Sstevel@tonic-gate elf_entry_pt(void)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	return (ENTRY(lml_main.lm_head));
4007c478bd9Sstevel@tonic-gate }
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate /*
4037c478bd9Sstevel@tonic-gate  * Unmap a given ELF shared object from the address space.
4047c478bd9Sstevel@tonic-gate  */
4057c478bd9Sstevel@tonic-gate static void
4067c478bd9Sstevel@tonic-gate elf_unmap_so(Rt_map *lmp)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	caddr_t	addr;
4097c478bd9Sstevel@tonic-gate 	size_t	size;
4107c478bd9Sstevel@tonic-gate 	Mmap	*mmaps;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * If this link map represents a relocatable object concatenation, then
4147c478bd9Sstevel@tonic-gate 	 * the image was simply generated in allocated memory.  Free the memory.
4157c478bd9Sstevel@tonic-gate 	 *
4167c478bd9Sstevel@tonic-gate 	 * Note: the memory was originally allocated in the libelf:_elf_outmap
4177c478bd9Sstevel@tonic-gate 	 * routine and would normally have been free'd in elf_outsync(), but
4187c478bd9Sstevel@tonic-gate 	 * because we 'interpose' on that routine the memory  wasn't free'd at
4197c478bd9Sstevel@tonic-gate 	 * that time.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_IMGALLOC) {
4227c478bd9Sstevel@tonic-gate 		free((void *)ADDR(lmp));
4237c478bd9Sstevel@tonic-gate 		return;
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	/*
4277c478bd9Sstevel@tonic-gate 	 * If padding was enabled via rtld_db, then we have at least one page
4287c478bd9Sstevel@tonic-gate 	 * in front of the image - and possibly a trailing page.
4297c478bd9Sstevel@tonic-gate 	 * Unmap the front page first:
4307c478bd9Sstevel@tonic-gate 	 */
4317c478bd9Sstevel@tonic-gate 	if (PADSTART(lmp) != ADDR(lmp)) {
4327c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PTRUNC(PADSTART(lmp));
4337c478bd9Sstevel@tonic-gate 		size = ADDR(lmp) - (ulong_t)addr;
4347c478bd9Sstevel@tonic-gate 		(void) munmap(addr, size);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Unmap any trailing padding.
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	if (M_PROUND((PADSTART(lmp) + PADIMLEN(lmp))) >
4417c478bd9Sstevel@tonic-gate 	    M_PROUND(ADDR(lmp) + MSIZE(lmp))) {
4427c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PROUND(ADDR(lmp) + MSIZE(lmp));
4437c478bd9Sstevel@tonic-gate 		size = M_PROUND(PADSTART(lmp) + PADIMLEN(lmp)) - (ulong_t)addr;
4447c478bd9Sstevel@tonic-gate 		(void) munmap(addr, size);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * Unmmap all mapped segments.
4497c478bd9Sstevel@tonic-gate 	 */
4507c478bd9Sstevel@tonic-gate 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++)
4517c478bd9Sstevel@tonic-gate 		(void) munmap(mmaps->m_vaddr, mmaps->m_msize);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Determine if a dependency requires a particular version and if so verify
4567c478bd9Sstevel@tonic-gate  * that the version exists in the dependency.
4577c478bd9Sstevel@tonic-gate  */
4587c478bd9Sstevel@tonic-gate static int
4597c478bd9Sstevel@tonic-gate elf_verify_vers(const char *name, Rt_map *clmp, Rt_map *nlmp)
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	Verneed		*vnd = VERNEED(clmp);
4627c478bd9Sstevel@tonic-gate 	int		_num, num = VERNEEDNUM(clmp);
4637c478bd9Sstevel@tonic-gate 	char		*cstrs = (char *)STRTAB(clmp);
4647c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
4677c478bd9Sstevel@tonic-gate 	 * Traverse the callers version needed information and determine if any
4687c478bd9Sstevel@tonic-gate 	 * specific versions are required from the dependency.
4697c478bd9Sstevel@tonic-gate 	 */
4705aefb655Srie 	DBG_CALL(Dbg_ver_need_title(LIST(clmp), NAME(clmp)));
4717c478bd9Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
4727c478bd9Sstevel@tonic-gate 	    vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) {
4737c478bd9Sstevel@tonic-gate 		Half		cnt = vnd->vn_cnt;
4747c478bd9Sstevel@tonic-gate 		Vernaux		*vnap;
4757c478bd9Sstevel@tonic-gate 		char		*nstrs, *need;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		/*
4787c478bd9Sstevel@tonic-gate 		 * Determine if a needed entry matches this dependency.
4797c478bd9Sstevel@tonic-gate 		 */
4807c478bd9Sstevel@tonic-gate 		need = (char *)(cstrs + vnd->vn_file);
4817c478bd9Sstevel@tonic-gate 		if (strcmp(name, need) != 0)
4827c478bd9Sstevel@tonic-gate 			continue;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
4857c478bd9Sstevel@tonic-gate 		    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
4867c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_VER_FIND), name);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		/*
4897c478bd9Sstevel@tonic-gate 		 * Validate that each version required actually exists in the
4907c478bd9Sstevel@tonic-gate 		 * dependency.
4917c478bd9Sstevel@tonic-gate 		 */
4927c478bd9Sstevel@tonic-gate 		nstrs = (char *)STRTAB(nlmp);
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		for (vnap = (Vernaux *)((Xword)vnd + vnd->vn_aux); cnt;
4957c478bd9Sstevel@tonic-gate 		    cnt--, vnap = (Vernaux *)((Xword)vnap + vnap->vna_next)) {
4967c478bd9Sstevel@tonic-gate 			char		*version, *define;
4977c478bd9Sstevel@tonic-gate 			Verdef		*vdf = VERDEF(nlmp);
4987c478bd9Sstevel@tonic-gate 			ulong_t		_num, num = VERDEFNUM(nlmp);
4997c478bd9Sstevel@tonic-gate 			int		found = 0;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 			version = (char *)(cstrs + vnap->vna_name);
5025aefb655Srie 			DBG_CALL(Dbg_ver_need_entry(lml, 0, need, version));
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 			for (_num = 1; _num <= num; _num++,
5057c478bd9Sstevel@tonic-gate 			    vdf = (Verdef *)((Xword)vdf + vdf->vd_next)) {
5067c478bd9Sstevel@tonic-gate 				Verdaux		*vdap;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 				if (vnap->vna_hash != vdf->vd_hash)
5097c478bd9Sstevel@tonic-gate 					continue;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 				vdap = (Verdaux *)((Xword)vdf + vdf->vd_aux);
5127c478bd9Sstevel@tonic-gate 				define = (char *)(nstrs + vdap->vda_name);
5137c478bd9Sstevel@tonic-gate 				if (strcmp(version, define) != 0)
5147c478bd9Sstevel@tonic-gate 					continue;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 				found++;
5177c478bd9Sstevel@tonic-gate 				break;
5187c478bd9Sstevel@tonic-gate 			}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 			/*
5217c478bd9Sstevel@tonic-gate 			 * If we're being traced print out any matched version
5227c478bd9Sstevel@tonic-gate 			 * when the verbose (-v) option is in effect.  Always
5237c478bd9Sstevel@tonic-gate 			 * print any unmatched versions.
5247c478bd9Sstevel@tonic-gate 			 */
5257c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
5267c478bd9Sstevel@tonic-gate 				if (found) {
5277c478bd9Sstevel@tonic-gate 				    if (!(lml->lm_flags & LML_FLG_TRC_VERBOSE))
5287c478bd9Sstevel@tonic-gate 					continue;
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_VER_FOUND),
5317c478bd9Sstevel@tonic-gate 					need, version, NAME(nlmp));
5327c478bd9Sstevel@tonic-gate 				} else {
5337c478bd9Sstevel@tonic-gate 				    if (rtld_flags & RT_FL_SILENCERR)
5347c478bd9Sstevel@tonic-gate 					continue;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_VER_NFOUND),
5377c478bd9Sstevel@tonic-gate 					need, version);
5387c478bd9Sstevel@tonic-gate 				}
5397c478bd9Sstevel@tonic-gate 				continue;
5407c478bd9Sstevel@tonic-gate 			}
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 			/*
5437c478bd9Sstevel@tonic-gate 			 * If the version hasn't been found then this is a
5447c478bd9Sstevel@tonic-gate 			 * candidate for a fatal error condition.  Weak
5457c478bd9Sstevel@tonic-gate 			 * version definition requirements are silently
5467c478bd9Sstevel@tonic-gate 			 * ignored.  Also, if the image inspected for a version
5477c478bd9Sstevel@tonic-gate 			 * definition has no versioning recorded at all then
5487c478bd9Sstevel@tonic-gate 			 * silently ignore this (this provides better backward
5497c478bd9Sstevel@tonic-gate 			 * compatibility to old images created prior to
5507c478bd9Sstevel@tonic-gate 			 * versioning being available).  Both of these skipped
5517c478bd9Sstevel@tonic-gate 			 * diagnostics are available under tracing (see above).
5527c478bd9Sstevel@tonic-gate 			 */
5537c478bd9Sstevel@tonic-gate 			if ((found == 0) && (num != 0) &&
5547c478bd9Sstevel@tonic-gate 			    (!(vnap->vna_flags & VER_FLG_WEAK))) {
5555aefb655Srie 				eprintf(lml, ERR_FATAL,
5565aefb655Srie 				    MSG_INTL(MSG_VER_NFOUND), need, version,
5575aefb655Srie 				    NAME(clmp));
5587c478bd9Sstevel@tonic-gate 				return (0);
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 	}
5625aefb655Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
5637c478bd9Sstevel@tonic-gate 	return (1);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Search through the dynamic section for DT_NEEDED entries and perform one
5687c478bd9Sstevel@tonic-gate  * of two functions.  If only the first argument is specified then load the
5697c478bd9Sstevel@tonic-gate  * defined shared object, otherwise add the link map representing the defined
5707c478bd9Sstevel@tonic-gate  * link map the the dlopen list.
5717c478bd9Sstevel@tonic-gate  */
5727c478bd9Sstevel@tonic-gate static int
5737c478bd9Sstevel@tonic-gate elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp)
5747c478bd9Sstevel@tonic-gate {
5757c478bd9Sstevel@tonic-gate 	Dyn		*dyn;
5767c478bd9Sstevel@tonic-gate 	ulong_t		ndx = 0;
5777c478bd9Sstevel@tonic-gate 	uint_t		lazy = 0, flags = 0;
5787c478bd9Sstevel@tonic-gate 	Word		lmflags = lml->lm_flags;
5797c478bd9Sstevel@tonic-gate 	Word		lmtflags = lml->lm_tflags;
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	/*
5827c478bd9Sstevel@tonic-gate 	 * Process each shared object on needed list.
5837c478bd9Sstevel@tonic-gate 	 */
5847c478bd9Sstevel@tonic-gate 	if (DYN(clmp) == 0)
5857c478bd9Sstevel@tonic-gate 		return (1);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	for (dyn = (Dyn *)DYN(clmp); dyn->d_tag != DT_NULL; dyn++, ndx++) {
5887c478bd9Sstevel@tonic-gate 		Dyninfo	*dip = &DYNINFO(clmp)[ndx];
5897c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
5907c478bd9Sstevel@tonic-gate 		char	*name;
5917c478bd9Sstevel@tonic-gate 		int	silent = 0;
5927c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		switch (dyn->d_tag) {
5957c478bd9Sstevel@tonic-gate 		case DT_POSFLAG_1:
5967c478bd9Sstevel@tonic-gate 			if ((dyn->d_un.d_val & DF_P1_LAZYLOAD) &&
5977c478bd9Sstevel@tonic-gate 			    !(lmtflags & LML_TFLG_NOLAZYLD))
5987c478bd9Sstevel@tonic-gate 				lazy = 1;
5997c478bd9Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_P1_GROUPPERM)
6007c478bd9Sstevel@tonic-gate 				flags = (FLG_RT_SETGROUP | FLG_RT_HANDLE);
6017c478bd9Sstevel@tonic-gate 			continue;
6027c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
6037c478bd9Sstevel@tonic-gate 		case DT_USED:
6047c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_NEEDED;
6057c478bd9Sstevel@tonic-gate 			if (flags)
6067c478bd9Sstevel@tonic-gate 				dip->di_flags |= FLG_DI_GROUP;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 			name = (char *)STRTAB(clmp) + dyn->d_un.d_val;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 			/*
6117c478bd9Sstevel@tonic-gate 			 * NOTE, libc.so.1 can't be lazy loaded.  Although a
6127c478bd9Sstevel@tonic-gate 			 * lazy position flag won't be produced when a RTLDINFO
6137c478bd9Sstevel@tonic-gate 			 * .dynamic entry is found (introduced with the UPM in
6147c478bd9Sstevel@tonic-gate 			 * Solaris 10), it was possible to mark libc for lazy
6157c478bd9Sstevel@tonic-gate 			 * loading on previous releases.  To reduce the overhead
6167c478bd9Sstevel@tonic-gate 			 * of testing for this occurrence, only carry out this
6177c478bd9Sstevel@tonic-gate 			 * check for the first object on the link-map list
6187c478bd9Sstevel@tonic-gate 			 * (there aren't many applications built without libc).
6197c478bd9Sstevel@tonic-gate 			 */
6207c478bd9Sstevel@tonic-gate 			if (lazy && (lml->lm_head == clmp) &&
6217c478bd9Sstevel@tonic-gate 			    (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0))
6227c478bd9Sstevel@tonic-gate 				lazy = 0;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 			/*
6257c478bd9Sstevel@tonic-gate 			 * Don't bring in lazy loaded objects yet unless we've
6267c478bd9Sstevel@tonic-gate 			 * been asked to attempt to load all available objects
6277c478bd9Sstevel@tonic-gate 			 * (crle(1) sets LD_FLAGS=loadavail).  Even under
6287c478bd9Sstevel@tonic-gate 			 * RTLD_NOW we don't process this - RTLD_NOW will cause
6297c478bd9Sstevel@tonic-gate 			 * relocation processing which in turn might trigger
6307c478bd9Sstevel@tonic-gate 			 * lazy loading, but its possible that the object has a
6317c478bd9Sstevel@tonic-gate 			 * lazy loaded file with no bindings (i.e., it should
6327c478bd9Sstevel@tonic-gate 			 * never have been a dependency in the first place).
6337c478bd9Sstevel@tonic-gate 			 */
6347c478bd9Sstevel@tonic-gate 			if (lazy) {
6357c478bd9Sstevel@tonic-gate 				if ((lmflags & LML_FLG_LOADAVAIL) == 0) {
6367c478bd9Sstevel@tonic-gate 					LAZY(clmp)++;
6377c478bd9Sstevel@tonic-gate 					lazy = flags = 0;
6387c478bd9Sstevel@tonic-gate 					continue;
6397c478bd9Sstevel@tonic-gate 				}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 				/*
6427c478bd9Sstevel@tonic-gate 				 * Silence any error messages - see description
6437c478bd9Sstevel@tonic-gate 				 * under elf_lookup_filtee().
6447c478bd9Sstevel@tonic-gate 				 */
6457c478bd9Sstevel@tonic-gate 				if ((rtld_flags & RT_FL_SILENCERR) == 0) {
6467c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_SILENCERR;
6477c478bd9Sstevel@tonic-gate 					silent = 1;
6487c478bd9Sstevel@tonic-gate 				}
6497c478bd9Sstevel@tonic-gate 			}
6507c478bd9Sstevel@tonic-gate 			break;
6517c478bd9Sstevel@tonic-gate 		case DT_AUXILIARY:
6527c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_AUXFLTR;
6537c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6547c478bd9Sstevel@tonic-gate 			continue;
6557c478bd9Sstevel@tonic-gate 		case DT_SUNW_AUXILIARY:
6567c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR);
6577c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6587c478bd9Sstevel@tonic-gate 			continue;
6597c478bd9Sstevel@tonic-gate 		case DT_FILTER:
6607c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_STDFLTR;
6617c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6627c478bd9Sstevel@tonic-gate 			continue;
6637c478bd9Sstevel@tonic-gate 		case DT_SUNW_FILTER:
6647c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR);
6657c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6667c478bd9Sstevel@tonic-gate 			continue;
6677c478bd9Sstevel@tonic-gate 		default:
6687c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6697c478bd9Sstevel@tonic-gate 			continue;
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 
6725aefb655Srie 		DBG_CALL(Dbg_file_needed(clmp, name));
6737c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & LML_FLG_TRC_ENABLE)
6747c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_PROCESSD;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		/*
6777c478bd9Sstevel@tonic-gate 		 * Establish the objects name, load it and establish a binding
6787c478bd9Sstevel@tonic-gate 		 * with the caller.
6797c478bd9Sstevel@tonic-gate 		 */
6807c478bd9Sstevel@tonic-gate 		if (((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0) ||
6817c478bd9Sstevel@tonic-gate 		    ((nlmp = load_one(lml, lmco, pnp, clmp, MODE(clmp),
6827c478bd9Sstevel@tonic-gate 		    flags, 0)) == 0) || (bind_one(clmp, nlmp, BND_NEEDED) == 0))
6837c478bd9Sstevel@tonic-gate 			nlmp = 0;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		/*
6867c478bd9Sstevel@tonic-gate 		 * Clean up any infrastructure, including the removal of the
6877c478bd9Sstevel@tonic-gate 		 * error suppression state, if it had been previously set in
6887c478bd9Sstevel@tonic-gate 		 * this routine.
6897c478bd9Sstevel@tonic-gate 		 */
6907c478bd9Sstevel@tonic-gate 		if (pnp)
6917c478bd9Sstevel@tonic-gate 			remove_pnode(pnp);
6927c478bd9Sstevel@tonic-gate 		if (silent)
6937c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_SILENCERR;
6947c478bd9Sstevel@tonic-gate 		lazy = flags = 0;
6957c478bd9Sstevel@tonic-gate 		if ((dip->di_info = (void *)nlmp) == 0) {
6967c478bd9Sstevel@tonic-gate 			/*
6977c478bd9Sstevel@tonic-gate 			 * If the object could not be mapped, continue if error
6987c478bd9Sstevel@tonic-gate 			 * suppression is established or we're here with ldd(1).
6997c478bd9Sstevel@tonic-gate 			 */
7007c478bd9Sstevel@tonic-gate 			if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags &
7017c478bd9Sstevel@tonic-gate 			    (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE)))
7027c478bd9Sstevel@tonic-gate 				continue;
7037c478bd9Sstevel@tonic-gate 			else
7047c478bd9Sstevel@tonic-gate 				return (0);
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (LAZY(clmp))
7097c478bd9Sstevel@tonic-gate 		lml->lm_lazy++;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	return (1);
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate static int
7155aefb655Srie elf_map_check(Lm_list *lml, const char *name, caddr_t vaddr, Off size)
7167c478bd9Sstevel@tonic-gate {
7177c478bd9Sstevel@tonic-gate 	prmap_t		*maps, *_maps;
7187c478bd9Sstevel@tonic-gate 	int		pfd, num, _num;
7197c478bd9Sstevel@tonic-gate 	caddr_t		eaddr = vaddr + size;
7207c478bd9Sstevel@tonic-gate 	int		err;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/*
7237c478bd9Sstevel@tonic-gate 	 * If memory reservations have been established for alternative objects
7247c478bd9Sstevel@tonic-gate 	 * determine if this object falls within the reservation, if it does no
7257c478bd9Sstevel@tonic-gate 	 * further checking is required.
7267c478bd9Sstevel@tonic-gate 	 */
7277c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_MEMRESV) {
7287c478bd9Sstevel@tonic-gate 		Rtc_head	*head = (Rtc_head *)config->c_bgn;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 		if ((vaddr >= (caddr_t)(uintptr_t)head->ch_resbgn) &&
7317c478bd9Sstevel@tonic-gate 		    (eaddr <= (caddr_t)(uintptr_t)head->ch_resend))
7327c478bd9Sstevel@tonic-gate 			return (0);
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/*
7367c478bd9Sstevel@tonic-gate 	 * Determine the mappings presently in use by this process.
7377c478bd9Sstevel@tonic-gate 	 */
7385aefb655Srie 	if ((pfd = pr_open(lml)) == FD_UNAVAIL)
7397c478bd9Sstevel@tonic-gate 		return (1);
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCNMAP, (void *)&num) == -1) {
7427c478bd9Sstevel@tonic-gate 		err = errno;
7435aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name,
7445aefb655Srie 		    strerror(err));
7457c478bd9Sstevel@tonic-gate 		return (1);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if ((maps = malloc((num + 1) * sizeof (prmap_t))) == 0)
7497c478bd9Sstevel@tonic-gate 		return (1);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCMAP, (void *)maps) == -1) {
7527c478bd9Sstevel@tonic-gate 		err = errno;
7535aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name,
7545aefb655Srie 		    strerror(err));
7557c478bd9Sstevel@tonic-gate 		free(maps);
7567c478bd9Sstevel@tonic-gate 		return (1);
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Determine if the supplied address clashes with any of the present
7617c478bd9Sstevel@tonic-gate 	 * process mappings.
7627c478bd9Sstevel@tonic-gate 	 */
7637c478bd9Sstevel@tonic-gate 	for (_num = 0, _maps = maps; _num < num; _num++, _maps++) {
7647c478bd9Sstevel@tonic-gate 		caddr_t		_eaddr = _maps->pr_vaddr + _maps->pr_size;
7657c478bd9Sstevel@tonic-gate 		Rt_map		*lmp;
7667c478bd9Sstevel@tonic-gate 		const char	*str;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		if ((eaddr < _maps->pr_vaddr) || (vaddr >= _eaddr))
7697c478bd9Sstevel@tonic-gate 			continue;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		/*
7727c478bd9Sstevel@tonic-gate 		 * We have a memory clash.  See if one of the known dynamic
7737c478bd9Sstevel@tonic-gate 		 * dependency mappings represents this space so as to provide
7747c478bd9Sstevel@tonic-gate 		 * the user a more meaningful message.
7757c478bd9Sstevel@tonic-gate 		 */
7767c478bd9Sstevel@tonic-gate 		if ((lmp = _caller(vaddr, 0)) != 0)
7777c478bd9Sstevel@tonic-gate 			str = NAME(lmp);
7787c478bd9Sstevel@tonic-gate 		else
7797c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_STR_UNKNOWN);
7807c478bd9Sstevel@tonic-gate 
7815aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_MAPINUSE), name,
7825aefb655Srie 		    EC_NATPTR(vaddr), EC_OFF(size), str);
7837c478bd9Sstevel@tonic-gate 		return (1);
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 	free(maps);
7867c478bd9Sstevel@tonic-gate 	return (0);
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate /*
7907c478bd9Sstevel@tonic-gate  * Obtain a memory reservation.  On newer systems, both MAP_ANON and MAP_ALIGN
7917c478bd9Sstevel@tonic-gate  * are used to obtained an aligned reservation from anonymous memory.  If
7927c478bd9Sstevel@tonic-gate  * MAP_ANON isn't available, then MAP_ALIGN isn't either, so obtain a standard
7937c478bd9Sstevel@tonic-gate  * reservation using the file as backing.
7947c478bd9Sstevel@tonic-gate  */
7957c478bd9Sstevel@tonic-gate static Am_ret
7965aefb655Srie elf_map_reserve(Lm_list *lml, const char *name, caddr_t *maddr, Off msize,
7975aefb655Srie     int mperm, int fd, Xword align)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate 	Am_ret	amret;
8007c478bd9Sstevel@tonic-gate 	int	mflag = MAP_PRIVATE | MAP_NORESERVE;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
8037c478bd9Sstevel@tonic-gate 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
8047c478bd9Sstevel@tonic-gate 		mflag |= MAP_ALIGN;
8057c478bd9Sstevel@tonic-gate 		*maddr = (caddr_t)align;
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate #endif
8085aefb655Srie 	if ((amret = anon_map(lml, maddr, msize, PROT_NONE, mflag)) == AM_ERROR)
8097c478bd9Sstevel@tonic-gate 		return (amret);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	if (amret == AM_OK)
8127c478bd9Sstevel@tonic-gate 		return (AM_OK);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * If an anonymous memory request failed (which should only be the
8167c478bd9Sstevel@tonic-gate 	 * case if it is unsupported on the system we're running on), establish
8177c478bd9Sstevel@tonic-gate 	 * the initial mapping directly from the file.
8187c478bd9Sstevel@tonic-gate 	 */
8197c478bd9Sstevel@tonic-gate 	*maddr = 0;
8207c478bd9Sstevel@tonic-gate 	if ((*maddr = mmap(*maddr, msize, mperm, MAP_PRIVATE,
8217c478bd9Sstevel@tonic-gate 	    fd, 0)) == MAP_FAILED) {
8227c478bd9Sstevel@tonic-gate 		int	err = errno;
8235aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), name,
8245aefb655Srie 		    strerror(err));
8257c478bd9Sstevel@tonic-gate 		return (AM_ERROR);
8267c478bd9Sstevel@tonic-gate 	}
8277c478bd9Sstevel@tonic-gate 	return (AM_NOSUP);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate static void *
8317c478bd9Sstevel@tonic-gate elf_map_textdata(caddr_t addr, Off flen, int mperm, int phdr_mperm, int mflag,
8327c478bd9Sstevel@tonic-gate     int fd, Off foff)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
8357c478bd9Sstevel@tonic-gate 	static int	notd = 0;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/*
8387c478bd9Sstevel@tonic-gate 	 * If MAP_TEXT and MAP_INITDATA are available, select the appropriate
8397c478bd9Sstevel@tonic-gate 	 * flag.
8407c478bd9Sstevel@tonic-gate 	 */
8417c478bd9Sstevel@tonic-gate 	if (notd == 0) {
8427c478bd9Sstevel@tonic-gate 		if ((phdr_mperm & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC)
8437c478bd9Sstevel@tonic-gate 			mflag |= MAP_TEXT;
8447c478bd9Sstevel@tonic-gate 		else
8457c478bd9Sstevel@tonic-gate 			mflag |= MAP_INITDATA;
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate #endif
8487c478bd9Sstevel@tonic-gate 	if (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff) != MAP_FAILED)
8497c478bd9Sstevel@tonic-gate 		return (0);
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
8527c478bd9Sstevel@tonic-gate 	if ((notd == 0) && (errno == EINVAL)) {
8537c478bd9Sstevel@tonic-gate 		/*
8547c478bd9Sstevel@tonic-gate 		 * MAP_TEXT and MAP_INITDATA may not be supported on this
8557c478bd9Sstevel@tonic-gate 		 * platform, try again without.
8567c478bd9Sstevel@tonic-gate 		 */
8577c478bd9Sstevel@tonic-gate 		notd = 1;
8587c478bd9Sstevel@tonic-gate 		mflag &= ~(MAP_TEXT | MAP_INITDATA);
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 		return (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff));
8617c478bd9Sstevel@tonic-gate 	}
8627c478bd9Sstevel@tonic-gate #endif
8637c478bd9Sstevel@tonic-gate 	return (MAP_FAILED);
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate /*
8677c478bd9Sstevel@tonic-gate  * Map in a file.
8687c478bd9Sstevel@tonic-gate  */
8697c478bd9Sstevel@tonic-gate static caddr_t
8707c478bd9Sstevel@tonic-gate elf_map_it(
8715aefb655Srie 	Lm_list		*lml,		/* link-map list */
8727c478bd9Sstevel@tonic-gate 	const char	*name,		/* actual name stored for pathname */
8737c478bd9Sstevel@tonic-gate 	Off		fsize,		/* total mapping claim of the file */
8747c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr,		/* ELF header of file */
8757c478bd9Sstevel@tonic-gate 	Phdr		*fphdr,		/* first loadable Phdr */
8767c478bd9Sstevel@tonic-gate 	Phdr		*lphdr,		/* last loadable Phdr */
8777c478bd9Sstevel@tonic-gate 	Phdr		**rrphdr,	/* return first Phdr in reservation */
8787c478bd9Sstevel@tonic-gate 	caddr_t		*rraddr,	/* return start of reservation */
8797c478bd9Sstevel@tonic-gate 	Off		*rrsize,	/* return total size of reservation */
8807c478bd9Sstevel@tonic-gate 	int		fixed,		/* image is resolved to a fixed addr */
8817c478bd9Sstevel@tonic-gate 	int		fd,		/* images file descriptor */
8827c478bd9Sstevel@tonic-gate 	Xword		align,		/* image segments maximum alignment */
8837c478bd9Sstevel@tonic-gate 	Mmap		*mmaps,		/* mmap information array and */
8847c478bd9Sstevel@tonic-gate 	uint_t		*mmapcnt)	/* 	mapping count */
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 	caddr_t		raddr;		/* reservation address */
8877c478bd9Sstevel@tonic-gate 	Off		rsize;		/* reservation size */
8887c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* working program header poiner */
8897c478bd9Sstevel@tonic-gate 	caddr_t		maddr;		/* working mmap address */
8907c478bd9Sstevel@tonic-gate 	caddr_t		faddr;		/* working file address */
8917c478bd9Sstevel@tonic-gate 	size_t		padsize;	/* object padding requirement */
8927c478bd9Sstevel@tonic-gate 	size_t		padpsize = 0;	/* padding size rounded to next page */
8937c478bd9Sstevel@tonic-gate 	size_t		padmsize = 0;	/* padding size rounded for alignment */
8947c478bd9Sstevel@tonic-gate 	int		skipfseg;	/* skip mapping first segment */
8957c478bd9Sstevel@tonic-gate 	int		mperm;		/* segment permissions */
8967c478bd9Sstevel@tonic-gate 	Am_ret		amret = AM_NOSUP;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	/*
8997c478bd9Sstevel@tonic-gate 	 * If padding is required extend both the front and rear of the image.
9007c478bd9Sstevel@tonic-gate 	 * To insure the image itself is mapped at the correct alignment the
9017c478bd9Sstevel@tonic-gate 	 * initial padding is rounded up to the nearest page.  Once the image is
9027c478bd9Sstevel@tonic-gate 	 * mapped the excess can be pruned to the nearest page required for the
9037c478bd9Sstevel@tonic-gate 	 * actual padding itself.
9047c478bd9Sstevel@tonic-gate 	 */
9057c478bd9Sstevel@tonic-gate 	if ((padsize = r_debug.rtd_objpad) != 0) {
9067c478bd9Sstevel@tonic-gate 		padpsize = M_PROUND(padsize);
9077c478bd9Sstevel@tonic-gate 		if (fixed)
9087c478bd9Sstevel@tonic-gate 			padmsize = padpsize;
9097c478bd9Sstevel@tonic-gate 		else
9107c478bd9Sstevel@tonic-gate 			padmsize = S_ROUND(padsize, align);
9117c478bd9Sstevel@tonic-gate 	}
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	/*
9147c478bd9Sstevel@tonic-gate 	 * Determine the initial permissions used to map in the first segment.
9157c478bd9Sstevel@tonic-gate 	 * If this segments memsz is greater that its filesz then the difference
9167c478bd9Sstevel@tonic-gate 	 * must be zeroed.  Make sure this segment is writable.
9177c478bd9Sstevel@tonic-gate 	 */
9187c478bd9Sstevel@tonic-gate 	mperm = 0;
9197c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_R)
9207c478bd9Sstevel@tonic-gate 		mperm |= PROT_READ;
9217c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_X)
9227c478bd9Sstevel@tonic-gate 		mperm |= PROT_EXEC;
9237c478bd9Sstevel@tonic-gate 	if ((fphdr->p_flags & PF_W) || (fphdr->p_memsz > fphdr->p_filesz))
9247c478bd9Sstevel@tonic-gate 		mperm |= PROT_WRITE;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	/*
9277c478bd9Sstevel@tonic-gate 	 * Determine whether or not to let system reserve address space based on
9287c478bd9Sstevel@tonic-gate 	 * whether this is a dynamic executable (addresses in object are fixed)
9297c478bd9Sstevel@tonic-gate 	 * or a shared object (addresses in object are relative to the objects'
9307c478bd9Sstevel@tonic-gate 	 * base).
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	if (fixed) {
9337c478bd9Sstevel@tonic-gate 		/*
9347c478bd9Sstevel@tonic-gate 		 * Determine the reservation address and size, and insure that
9357c478bd9Sstevel@tonic-gate 		 * this reservation isn't already in use.
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 		faddr = maddr = (caddr_t)M_PTRUNC((ulong_t)fphdr->p_vaddr);
9387c478bd9Sstevel@tonic-gate 		raddr = maddr - padpsize;
9397c478bd9Sstevel@tonic-gate 		rsize = fsize + padpsize + padsize;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		if (lml_main.lm_head) {
9425aefb655Srie 			if (elf_map_check(lml, name, raddr, rsize) != 0)
9437c478bd9Sstevel@tonic-gate 				return (0);
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 		/*
9477c478bd9Sstevel@tonic-gate 		 * As this is a fixed image, all segments must be individually
9487c478bd9Sstevel@tonic-gate 		 * mapped.
9497c478bd9Sstevel@tonic-gate 		 */
9507c478bd9Sstevel@tonic-gate 		skipfseg = 0;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	} else {
9537c478bd9Sstevel@tonic-gate 		size_t	esize;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 		/*
9567c478bd9Sstevel@tonic-gate 		 * If this isn't a fixed image, reserve enough address space for
9577c478bd9Sstevel@tonic-gate 		 * the entire image to be mapped.  The amount of reservation is
9587c478bd9Sstevel@tonic-gate 		 * the range between the beginning of the first, and end of the
9597c478bd9Sstevel@tonic-gate 		 * last loadable segment, together with any padding, plus the
9607c478bd9Sstevel@tonic-gate 		 * alignment of the first segment.
9617c478bd9Sstevel@tonic-gate 		 *
9627c478bd9Sstevel@tonic-gate 		 * The optimal reservation is made as a no-reserve mapping from
9637c478bd9Sstevel@tonic-gate 		 * anonymous memory.  Each segment is then mapped into this
9647c478bd9Sstevel@tonic-gate 		 * reservation.  If the anonymous mapping capability isn't
9657c478bd9Sstevel@tonic-gate 		 * available, the reservation is obtained from the file itself.
9667c478bd9Sstevel@tonic-gate 		 * In this case the first segment of the image is mapped as part
9677c478bd9Sstevel@tonic-gate 		 * of the reservation, thus only the following segments need to
9687c478bd9Sstevel@tonic-gate 		 * be remapped.
9697c478bd9Sstevel@tonic-gate 		 */
9707c478bd9Sstevel@tonic-gate 		rsize = fsize + padmsize + padsize;
9715aefb655Srie 		if ((amret = elf_map_reserve(lml, name, &raddr, rsize, mperm,
9727c478bd9Sstevel@tonic-gate 		    fd, align)) == AM_ERROR)
9737c478bd9Sstevel@tonic-gate 			return (0);
9747c478bd9Sstevel@tonic-gate 		maddr = raddr + padmsize;
9757c478bd9Sstevel@tonic-gate 		faddr = (caddr_t)S_ROUND((Off)maddr, align);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		/*
9787c478bd9Sstevel@tonic-gate 		 * If this reservation has been obtained from anonymous memory,
9797c478bd9Sstevel@tonic-gate 		 * then all segments must be individually mapped.  Otherwise,
9807c478bd9Sstevel@tonic-gate 		 * the first segment heads the reservation.
9817c478bd9Sstevel@tonic-gate 		 */
9827c478bd9Sstevel@tonic-gate 		if (amret == AM_OK)
9837c478bd9Sstevel@tonic-gate 			skipfseg = 0;
9847c478bd9Sstevel@tonic-gate 		else
9857c478bd9Sstevel@tonic-gate 			skipfseg = 1;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		/*
9887c478bd9Sstevel@tonic-gate 		 * For backward compatibility (where MAP_ALIGN isn't available),
9897c478bd9Sstevel@tonic-gate 		 * insure the alignment of the reservation is adequate for this
9907c478bd9Sstevel@tonic-gate 		 * object, and if not remap the object to obtain the correct
9917c478bd9Sstevel@tonic-gate 		 * alignment.
9927c478bd9Sstevel@tonic-gate 		 */
9937c478bd9Sstevel@tonic-gate 		if (faddr != maddr) {
9947c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, rsize);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 			rsize += align;
9975aefb655Srie 			if ((amret = elf_map_reserve(lml, name, &raddr, rsize,
9985aefb655Srie 			    mperm, fd, align)) == AM_ERROR)
9997c478bd9Sstevel@tonic-gate 				return (0);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 			maddr = faddr = (caddr_t)S_ROUND((Off)(raddr +
10027c478bd9Sstevel@tonic-gate 			    padpsize), align);
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 			esize = maddr - raddr + padpsize;
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 			/*
10077c478bd9Sstevel@tonic-gate 			 * As ths image has been realigned, the first segment
10087c478bd9Sstevel@tonic-gate 			 * of the file needs to be remapped to its correct
10097c478bd9Sstevel@tonic-gate 			 * location.
10107c478bd9Sstevel@tonic-gate 			 */
10117c478bd9Sstevel@tonic-gate 			skipfseg = 0;
10127c478bd9Sstevel@tonic-gate 		} else
10137c478bd9Sstevel@tonic-gate 			esize = padmsize - padpsize;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		/*
10167c478bd9Sstevel@tonic-gate 		 * If this reservation included padding, remove any excess for
10177c478bd9Sstevel@tonic-gate 		 * the start of the image (the padding was adjusted to insure
10187c478bd9Sstevel@tonic-gate 		 * the image was aligned appropriately).
10197c478bd9Sstevel@tonic-gate 		 */
10207c478bd9Sstevel@tonic-gate 		if (esize) {
10217c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, esize);
10227c478bd9Sstevel@tonic-gate 			raddr += esize;
10237c478bd9Sstevel@tonic-gate 			rsize -= esize;
10247c478bd9Sstevel@tonic-gate 		}
10257c478bd9Sstevel@tonic-gate 	}
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * At this point we know the initial location of the image, and its
10297c478bd9Sstevel@tonic-gate 	 * size.  Pass these back to the caller for inclusion in the link-map
10307c478bd9Sstevel@tonic-gate 	 * that will eventually be created.
10317c478bd9Sstevel@tonic-gate 	 */
10327c478bd9Sstevel@tonic-gate 	*rraddr = raddr;
10337c478bd9Sstevel@tonic-gate 	*rrsize = rsize;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * The first loadable segment is now pointed to by maddr.  This segment
10377c478bd9Sstevel@tonic-gate 	 * will eventually contain the elf header and program headers, so reset
10387c478bd9Sstevel@tonic-gate 	 * the program header.  Pass this  back to the caller for inclusion in
10397c478bd9Sstevel@tonic-gate 	 * the link-map so it can be used for later unmapping operations.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	/* LINTED */
10427c478bd9Sstevel@tonic-gate 	*rrphdr = (Phdr *)((char *)maddr + ehdr->e_phoff);
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	/*
10457c478bd9Sstevel@tonic-gate 	 * If padding is required at the front of the image, obtain that now.
10467c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
10477c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
10487c478bd9Sstevel@tonic-gate 	 * Otherwise this reservation is backed by the file, or in the case of
10497c478bd9Sstevel@tonic-gate 	 * a fixed image, doesn't yet exist.  Map the padding so that it is
10507c478bd9Sstevel@tonic-gate 	 * suitably protected (PROT_NONE), and insure the first segment of the
10517c478bd9Sstevel@tonic-gate 	 * file is mapped to its correct location.
10527c478bd9Sstevel@tonic-gate 	 */
10537c478bd9Sstevel@tonic-gate 	if (padsize) {
10547c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
10555aefb655Srie 			if (dz_map(lml, raddr, padpsize, PROT_NONE,
10565aefb655Srie 			    (MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE)) ==
10575aefb655Srie 			    MAP_FAILED)
10587c478bd9Sstevel@tonic-gate 				return (0);
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 			skipfseg = 0;
10617c478bd9Sstevel@tonic-gate 		}
10627c478bd9Sstevel@tonic-gate 		rsize -= padpsize;
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * Map individual segments.  For a fixed image, these will each be
10677c478bd9Sstevel@tonic-gate 	 * unique mappings.  For a reservation these will fill in the
10687c478bd9Sstevel@tonic-gate 	 * reservation.
10697c478bd9Sstevel@tonic-gate 	 */
10707c478bd9Sstevel@tonic-gate 	for (phdr = fphdr; phdr <= lphdr;
10717c478bd9Sstevel@tonic-gate 	    phdr = (Phdr *)((Off)phdr + ehdr->e_phentsize)) {
10727c478bd9Sstevel@tonic-gate 		caddr_t	addr;
10737c478bd9Sstevel@tonic-gate 		Off	mlen, flen;
10747c478bd9Sstevel@tonic-gate 		size_t	size;
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 		/*
10777c478bd9Sstevel@tonic-gate 		 * Skip non-loadable segments or segments that don't occupy
10787c478bd9Sstevel@tonic-gate 		 * any memory.
10797c478bd9Sstevel@tonic-gate 		 */
10807c478bd9Sstevel@tonic-gate 		if (((phdr->p_type != PT_LOAD) &&
10817c478bd9Sstevel@tonic-gate 		    (phdr->p_type != PT_SUNWBSS)) || (phdr->p_memsz == 0))
10827c478bd9Sstevel@tonic-gate 			continue;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		/*
10857c478bd9Sstevel@tonic-gate 		 * Establish this segments address relative to our base.
10867c478bd9Sstevel@tonic-gate 		 */
10877c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PTRUNC((ulong_t)(phdr->p_vaddr +
10887c478bd9Sstevel@tonic-gate 		    (fixed ? 0 : faddr)));
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		/*
10917c478bd9Sstevel@tonic-gate 		 * Determine the mapping protection from the segment attributes.
10927c478bd9Sstevel@tonic-gate 		 * Also determine the etext address from the last loadable
10937c478bd9Sstevel@tonic-gate 		 * segment which has permissions but no write access.
10947c478bd9Sstevel@tonic-gate 		 */
10957c478bd9Sstevel@tonic-gate 		mperm = 0;
10967c478bd9Sstevel@tonic-gate 		if (phdr->p_flags) {
10977c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_R)
10987c478bd9Sstevel@tonic-gate 				mperm |= PROT_READ;
10997c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_X)
11007c478bd9Sstevel@tonic-gate 				mperm |= PROT_EXEC;
11017c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_W)
11027c478bd9Sstevel@tonic-gate 				mperm |= PROT_WRITE;
11037c478bd9Sstevel@tonic-gate 			else
11047c478bd9Sstevel@tonic-gate 				fmap->fm_etext = phdr->p_vaddr + phdr->p_memsz +
11057c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
11067c478bd9Sstevel@tonic-gate 		}
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 		/*
11097c478bd9Sstevel@tonic-gate 		 * Determine the type of mapping required.
11107c478bd9Sstevel@tonic-gate 		 */
11117c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_SUNWBSS) {
11127c478bd9Sstevel@tonic-gate 			/*
11137c478bd9Sstevel@tonic-gate 			 * Potentially, we can defer the loading of any SUNWBSS
11147c478bd9Sstevel@tonic-gate 			 * segment, depending on whether the symbols it provides
11157c478bd9Sstevel@tonic-gate 			 * have been bound to.  In this manner, large segments
11167c478bd9Sstevel@tonic-gate 			 * that are interposed upon between shared libraries
11177c478bd9Sstevel@tonic-gate 			 * may not require mapping.  Note, that the mapping
11187c478bd9Sstevel@tonic-gate 			 * information is recorded in our mapping descriptor at
11197c478bd9Sstevel@tonic-gate 			 * this time.
11207c478bd9Sstevel@tonic-gate 			 */
11217c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11227c478bd9Sstevel@tonic-gate 			flen = 0;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 		} else if ((phdr->p_filesz == 0) && (phdr->p_flags == 0)) {
11257c478bd9Sstevel@tonic-gate 			/*
11267c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file and no flags
11277c478bd9Sstevel@tonic-gate 			 * specified, then it defines a reservation.  At this
11287c478bd9Sstevel@tonic-gate 			 * point all standard loadable segments will have been
11297c478bd9Sstevel@tonic-gate 			 * processed.  The segment reservation is mapped
11307c478bd9Sstevel@tonic-gate 			 * directly from /dev/null.
11317c478bd9Sstevel@tonic-gate 			 */
11325aefb655Srie 			if (nu_map(lml, (caddr_t)addr, phdr->p_memsz, PROT_NONE,
11337c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
11347c478bd9Sstevel@tonic-gate 				return (0);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11377c478bd9Sstevel@tonic-gate 			flen = 0;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		} else if (phdr->p_filesz == 0) {
11407c478bd9Sstevel@tonic-gate 			/*
11417c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file then it defines a
11427c478bd9Sstevel@tonic-gate 			 * nobits segment and is mapped directly from /dev/zero.
11437c478bd9Sstevel@tonic-gate 			 */
11445aefb655Srie 			if (dz_map(lml, (caddr_t)addr, phdr->p_memsz, mperm,
11457c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
11467c478bd9Sstevel@tonic-gate 				return (0);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11497c478bd9Sstevel@tonic-gate 			flen = 0;
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate 		} else {
11527c478bd9Sstevel@tonic-gate 			Off	foff;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 			/*
11557c478bd9Sstevel@tonic-gate 			 * This mapping originates from the file.  Determine the
11567c478bd9Sstevel@tonic-gate 			 * file offset to which the mapping will be directed
11577c478bd9Sstevel@tonic-gate 			 * (must be aligned) and how much to map (might be more
11587c478bd9Sstevel@tonic-gate 			 * than the file in the case of .bss).
11597c478bd9Sstevel@tonic-gate 			 */
11607c478bd9Sstevel@tonic-gate 			foff = M_PTRUNC((ulong_t)phdr->p_offset);
11617c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz + (phdr->p_offset - foff);
11627c478bd9Sstevel@tonic-gate 			flen = phdr->p_filesz + (phdr->p_offset - foff);
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 			/*
11657c478bd9Sstevel@tonic-gate 			 * If this is a non-fixed, non-anonymous mapping, and no
11667c478bd9Sstevel@tonic-gate 			 * padding is involved, then the first loadable segment
11677c478bd9Sstevel@tonic-gate 			 * is already part of the initial reservation.  In this
11687c478bd9Sstevel@tonic-gate 			 * case there is no need to remap this segment.
11697c478bd9Sstevel@tonic-gate 			 */
11707c478bd9Sstevel@tonic-gate 			if ((skipfseg == 0) || (phdr != fphdr)) {
11717c478bd9Sstevel@tonic-gate 				int phdr_mperm = mperm;
11727c478bd9Sstevel@tonic-gate 				/*
11737c478bd9Sstevel@tonic-gate 				 * If this segments memsz is greater that its
11747c478bd9Sstevel@tonic-gate 				 * filesz then the difference must be zeroed.
11757c478bd9Sstevel@tonic-gate 				 * Make sure this segment is writable.
11767c478bd9Sstevel@tonic-gate 				 */
11777c478bd9Sstevel@tonic-gate 				if (phdr->p_memsz > phdr->p_filesz)
11787c478bd9Sstevel@tonic-gate 					mperm |= PROT_WRITE;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 				if (elf_map_textdata((caddr_t)addr, flen,
11817c478bd9Sstevel@tonic-gate 				    mperm, phdr_mperm,
11827c478bd9Sstevel@tonic-gate 				    (MAP_FIXED | MAP_PRIVATE), fd, foff) ==
11837c478bd9Sstevel@tonic-gate 				    MAP_FAILED) {
11847c478bd9Sstevel@tonic-gate 					int	err = errno;
11855aefb655Srie 					eprintf(lml, ERR_FATAL,
11867c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYS_MMAP), name,
11877c478bd9Sstevel@tonic-gate 					    strerror(err));
11887c478bd9Sstevel@tonic-gate 					return (0);
11897c478bd9Sstevel@tonic-gate 				}
11907c478bd9Sstevel@tonic-gate 			}
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 			/*
11937c478bd9Sstevel@tonic-gate 			 * If the memory occupancy of the segment overflows the
11947c478bd9Sstevel@tonic-gate 			 * definition in the file, we need to "zero out" the end
11957c478bd9Sstevel@tonic-gate 			 * of the mapping we've established, and if necessary,
11967c478bd9Sstevel@tonic-gate 			 * map some more space from /dev/zero.  Note, zero'ed
11977c478bd9Sstevel@tonic-gate 			 * memory must end on a double word boundary to satisfy
11987c478bd9Sstevel@tonic-gate 			 * zero().
11997c478bd9Sstevel@tonic-gate 			 */
12007c478bd9Sstevel@tonic-gate 			if (phdr->p_memsz > phdr->p_filesz) {
12017c478bd9Sstevel@tonic-gate 				caddr_t	zaddr;
12027c478bd9Sstevel@tonic-gate 				size_t	zlen, zplen;
12037c478bd9Sstevel@tonic-gate 				Off	fend;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 				foff = (Off)(phdr->p_vaddr + phdr->p_filesz +
12067c478bd9Sstevel@tonic-gate 				    (fixed ? 0 : faddr));
12077c478bd9Sstevel@tonic-gate 				zaddr = (caddr_t)M_PROUND(foff);
12087c478bd9Sstevel@tonic-gate 				zplen = (size_t)(zaddr - foff);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 				fend = (Off)S_DROUND((size_t)(phdr->p_vaddr +
12117c478bd9Sstevel@tonic-gate 				    phdr->p_memsz + (fixed ? 0 : faddr)));
12127c478bd9Sstevel@tonic-gate 				zlen = (size_t)(fend - foff);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 				/*
12157c478bd9Sstevel@tonic-gate 				 * Determine whether the number of bytes that
12167c478bd9Sstevel@tonic-gate 				 * must be zero'ed overflow to the next page.
12177c478bd9Sstevel@tonic-gate 				 * If not, simply clear the exact bytes
12187c478bd9Sstevel@tonic-gate 				 * (filesz to memsz) from this page.  Otherwise,
12197c478bd9Sstevel@tonic-gate 				 * clear the remaining bytes of this page, and
12207c478bd9Sstevel@tonic-gate 				 * map an following pages from /dev/zero.
12217c478bd9Sstevel@tonic-gate 				 */
12227c478bd9Sstevel@tonic-gate 				if (zlen < zplen)
12237c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zlen);
12247c478bd9Sstevel@tonic-gate 				else {
12257c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zplen);
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 					if ((zlen = (fend - (Off)zaddr)) > 0) {
12285aefb655Srie 						if (dz_map(lml, zaddr, zlen,
12295aefb655Srie 						    mperm,
12307c478bd9Sstevel@tonic-gate 						    MAP_FIXED | MAP_PRIVATE) ==
12317c478bd9Sstevel@tonic-gate 						    MAP_FAILED)
12327c478bd9Sstevel@tonic-gate 							return (0);
12337c478bd9Sstevel@tonic-gate 					}
12347c478bd9Sstevel@tonic-gate 				}
12357c478bd9Sstevel@tonic-gate 			}
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 		/*
12397c478bd9Sstevel@tonic-gate 		 * Unmap anything from the last mapping address to this one and
12407c478bd9Sstevel@tonic-gate 		 * update the mapping claim pointer.
12417c478bd9Sstevel@tonic-gate 		 */
12427c478bd9Sstevel@tonic-gate 		if ((fixed == 0) && ((size = addr - maddr) != 0)) {
12437c478bd9Sstevel@tonic-gate 			(void) munmap(maddr, size);
12447c478bd9Sstevel@tonic-gate 			rsize -= size;
12457c478bd9Sstevel@tonic-gate 		}
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		/*
12487c478bd9Sstevel@tonic-gate 		 * Retain this segments mapping information.
12497c478bd9Sstevel@tonic-gate 		 */
12507c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_vaddr = addr;
12517c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_msize = mlen;
12527c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_fsize = flen;
12537c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_perm = mperm;
12547c478bd9Sstevel@tonic-gate 		(*mmapcnt)++;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 		maddr = addr + M_PROUND(mlen);
12577c478bd9Sstevel@tonic-gate 		rsize -= M_PROUND(mlen);
12587c478bd9Sstevel@tonic-gate 	}
12597c478bd9Sstevel@tonic-gate 
12607c478bd9Sstevel@tonic-gate 	/*
12617c478bd9Sstevel@tonic-gate 	 * If padding is required at the end of the image, obtain that now.
12627c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
12637c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
12647c478bd9Sstevel@tonic-gate 	 */
12657c478bd9Sstevel@tonic-gate 	if (padsize) {
12667c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
12677c478bd9Sstevel@tonic-gate 			/*
12687c478bd9Sstevel@tonic-gate 			 * maddr is currently page aligned from the last segment
12697c478bd9Sstevel@tonic-gate 			 * mapping.
12707c478bd9Sstevel@tonic-gate 			 */
12715aefb655Srie 			if (dz_map(lml, maddr, padsize, PROT_NONE,
12725aefb655Srie 			    (MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE)) ==
12735aefb655Srie 			    MAP_FAILED)
12747c478bd9Sstevel@tonic-gate 				return (0);
12757c478bd9Sstevel@tonic-gate 		}
12767c478bd9Sstevel@tonic-gate 		maddr += padsize;
12777c478bd9Sstevel@tonic-gate 		rsize -= padsize;
12787c478bd9Sstevel@tonic-gate 	}
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/*
12817c478bd9Sstevel@tonic-gate 	 * Unmap any final reservation.
12827c478bd9Sstevel@tonic-gate 	 */
12837c478bd9Sstevel@tonic-gate 	if ((fixed == 0) && (rsize != 0))
12847c478bd9Sstevel@tonic-gate 		(void) munmap(maddr, rsize);
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate 	return (faddr);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate /*
12907c478bd9Sstevel@tonic-gate  * A null symbol interpretor.  Used if a filter has no associated filtees.
12917c478bd9Sstevel@tonic-gate  */
12927c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
12937c478bd9Sstevel@tonic-gate static Sym *
12947c478bd9Sstevel@tonic-gate elf_null_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
12977c478bd9Sstevel@tonic-gate }
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate /*
13007c478bd9Sstevel@tonic-gate  * Disable filtee use.
13017c478bd9Sstevel@tonic-gate  */
13027c478bd9Sstevel@tonic-gate static void
1303*9a411307Srie elf_disable_filtee(Rt_map *lmp, Dyninfo *dip)
13047c478bd9Sstevel@tonic-gate {
13057c478bd9Sstevel@tonic-gate 	dip->di_info = 0;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) {
13087c478bd9Sstevel@tonic-gate 		/*
13097c478bd9Sstevel@tonic-gate 		 * If this is an object filter, free the filtee's duplication.
13107c478bd9Sstevel@tonic-gate 		 */
13117c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(lmp) != FLTR_DISABLED) {
13127c478bd9Sstevel@tonic-gate 			free(REFNAME(lmp));
13137c478bd9Sstevel@tonic-gate 			REFNAME(lmp) = (char *)0;
13147c478bd9Sstevel@tonic-gate 			OBJFLTRNDX(lmp) = FLTR_DISABLED;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 			/*
13177c478bd9Sstevel@tonic-gate 			 * Indicate that this filtee is no longer available.
13187c478bd9Sstevel@tonic-gate 			 */
13197c478bd9Sstevel@tonic-gate 			if (dip->di_flags & FLG_DI_STDFLTR)
13207c478bd9Sstevel@tonic-gate 				SYMINTP(lmp) = elf_null_find_sym;
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 		}
13237c478bd9Sstevel@tonic-gate 	} else if (dip->di_flags & FLG_DI_STDFLTR) {
13247c478bd9Sstevel@tonic-gate 		/*
13257c478bd9Sstevel@tonic-gate 		 * Indicate that this standard filtee is no longer available.
13267c478bd9Sstevel@tonic-gate 		 */
13277c478bd9Sstevel@tonic-gate 		if (SYMSFLTRCNT(lmp))
13287c478bd9Sstevel@tonic-gate 			SYMSFLTRCNT(lmp)--;
13297c478bd9Sstevel@tonic-gate 	} else {
13307c478bd9Sstevel@tonic-gate 		/*
13317c478bd9Sstevel@tonic-gate 		 * Indicate that this auxiliary filtee is no longer available.
13327c478bd9Sstevel@tonic-gate 		 */
13337c478bd9Sstevel@tonic-gate 		if (SYMAFLTRCNT(lmp))
13347c478bd9Sstevel@tonic-gate 			SYMAFLTRCNT(lmp)--;
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate 	dip->di_flags &= ~MSK_DI_FILTER;
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate /*
13407c478bd9Sstevel@tonic-gate  * Find symbol interpreter - filters.
13417c478bd9Sstevel@tonic-gate  * This function is called when the symbols from a shared object should
13427c478bd9Sstevel@tonic-gate  * be resolved from the shared objects filtees instead of from within itself.
13437c478bd9Sstevel@tonic-gate  *
13447c478bd9Sstevel@tonic-gate  * A symbol name of 0 is used to trigger filtee loading.
13457c478bd9Sstevel@tonic-gate  */
13467c478bd9Sstevel@tonic-gate static Sym *
13477c478bd9Sstevel@tonic-gate _elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
13487c478bd9Sstevel@tonic-gate {
13497c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name, *filtees;
13507c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
13517c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
13527c478bd9Sstevel@tonic-gate 	Pnode		*pnp, **pnpp;
13537c478bd9Sstevel@tonic-gate 	int		any;
13547c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(ilmp)[ndx];
13557c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(ilmp);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	/*
13587c478bd9Sstevel@tonic-gate 	 * Indicate that the filter has been used.  If a binding already exists
13597c478bd9Sstevel@tonic-gate 	 * to the caller, indicate that this object is referenced.  This insures
13607c478bd9Sstevel@tonic-gate 	 * we don't generate false unreferenced diagnostics from ldd -u/U or
13617c478bd9Sstevel@tonic-gate 	 * debugging.  Don't create a binding regardless, as this filter may
13627c478bd9Sstevel@tonic-gate 	 * have been dlopen()'ed.
13637c478bd9Sstevel@tonic-gate 	 */
13647c478bd9Sstevel@tonic-gate 	if (name && (ilmp != clmp)) {
13657c478bd9Sstevel@tonic-gate 		Word	tracing = (LIST(clmp)->lm_flags &
13667c478bd9Sstevel@tonic-gate 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED));
13677c478bd9Sstevel@tonic-gate 
13685aefb655Srie 		if (tracing || DBG_ENABLED) {
13697c478bd9Sstevel@tonic-gate 			Bnd_desc **	bdpp;
13707c478bd9Sstevel@tonic-gate 			Aliste		off;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 			FLAGS1(ilmp) |= FL1_RT_USED;
13737c478bd9Sstevel@tonic-gate 
13745aefb655Srie 			if ((tracing & LML_FLG_TRC_UNREF) || DBG_ENABLED) {
13757c478bd9Sstevel@tonic-gate 				for (ALIST_TRAVERSE(CALLERS(ilmp), off, bdpp)) {
13767c478bd9Sstevel@tonic-gate 					Bnd_desc *	bdp = *bdpp;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 					if (bdp->b_caller == clmp) {
13797c478bd9Sstevel@tonic-gate 						bdp->b_flags |= BND_REFER;
13807c478bd9Sstevel@tonic-gate 						break;
13817c478bd9Sstevel@tonic-gate 					}
13827c478bd9Sstevel@tonic-gate 				}
13837c478bd9Sstevel@tonic-gate 			}
13847c478bd9Sstevel@tonic-gate 		}
13857c478bd9Sstevel@tonic-gate 	}
13867c478bd9Sstevel@tonic-gate 
13877c478bd9Sstevel@tonic-gate 	/*
13887c478bd9Sstevel@tonic-gate 	 * If this is the first call to process this filter, establish the
13897c478bd9Sstevel@tonic-gate 	 * filtee list.  If a configuration file exists, determine if any
13907c478bd9Sstevel@tonic-gate 	 * filtee associations for this filter, and its filtee reference, are
13917c478bd9Sstevel@tonic-gate 	 * defined.  Otherwise, process the filtee reference.  Any token
13927c478bd9Sstevel@tonic-gate 	 * expansion is also completed at this point (i.e., $PLATFORM).
13937c478bd9Sstevel@tonic-gate 	 */
13947c478bd9Sstevel@tonic-gate 	filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val;
13957c478bd9Sstevel@tonic-gate 	if (dip->di_info == 0) {
13967c478bd9Sstevel@tonic-gate 		if (rtld_flags2 & RT_FL2_FLTCFG)
13975aefb655Srie 			dip->di_info = elf_config_flt(lml, PATHNAME(ilmp),
13985aefb655Srie 			    filtees);
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 		if (dip->di_info == 0) {
14015aefb655Srie 			DBG_CALL(Dbg_file_filter(lml, NAME(ilmp), filtees, 0));
14027c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags &
14037c478bd9Sstevel@tonic-gate 			    (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
14047c478bd9Sstevel@tonic-gate 			    ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0))
14057c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FILTER),
14067c478bd9Sstevel@tonic-gate 				    NAME(ilmp), filtees);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 			if ((dip->di_info = (void *)expand_paths(ilmp,
14097c478bd9Sstevel@tonic-gate 			    filtees, PN_SER_FILTEE, 0)) == 0) {
14107c478bd9Sstevel@tonic-gate 				elf_disable_filtee(ilmp, dip);
14117c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
14127c478bd9Sstevel@tonic-gate 			}
14137c478bd9Sstevel@tonic-gate 		}
14147c478bd9Sstevel@tonic-gate 	}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	/*
14177c478bd9Sstevel@tonic-gate 	 * Traverse the filtee list, dlopen()'ing any objects specified and
14187c478bd9Sstevel@tonic-gate 	 * using their group handle to lookup the symbol.
14197c478bd9Sstevel@tonic-gate 	 */
14207c478bd9Sstevel@tonic-gate 	for (any = 0, pnpp = (Pnode **)&(dip->di_info), pnp = *pnpp; pnp;
1421*9a411307Srie 	    pnpp = &pnp->p_next, pnp = *pnpp) {
14227c478bd9Sstevel@tonic-gate 		int	mode;
14237c478bd9Sstevel@tonic-gate 		Grp_hdl	*ghp;
14247c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
14277c478bd9Sstevel@tonic-gate 			continue;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 		/*
14307c478bd9Sstevel@tonic-gate 		 * Establish the mode of the filtee from the filter.  As filtees
14317c478bd9Sstevel@tonic-gate 		 * are loaded via a dlopen(), make sure that RTLD_GROUP is set
14327c478bd9Sstevel@tonic-gate 		 * and the filtees aren't global.  It would be nice to have
14337c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST used here also, but as filters got out long before
14347c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST was introduced it's a little too late now.
14357c478bd9Sstevel@tonic-gate 		 */
14367c478bd9Sstevel@tonic-gate 		mode = MODE(ilmp) | RTLD_GROUP;
14377c478bd9Sstevel@tonic-gate 		mode &= ~RTLD_GLOBAL;
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 		/*
14407c478bd9Sstevel@tonic-gate 		 * Insure that any auxiliary filter can locate symbols from its
14417c478bd9Sstevel@tonic-gate 		 * caller.
14427c478bd9Sstevel@tonic-gate 		 */
14437c478bd9Sstevel@tonic-gate 		if (dip->di_flags & FLG_DI_AUXFLTR)
14447c478bd9Sstevel@tonic-gate 			mode |= RTLD_PARENT;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 		/*
14477c478bd9Sstevel@tonic-gate 		 * Process any hardware capability directory.  Establish a new
14487c478bd9Sstevel@tonic-gate 		 * link-map control list from which to analyze any newly added
144924a6229eSrie 		 * objects.
14507c478bd9Sstevel@tonic-gate 		 */
14517c478bd9Sstevel@tonic-gate 		if ((pnp->p_info == 0) && (pnp->p_orig & PN_TKN_HWCAP)) {
145211a2bb38Srie 			Lm_cntl	*lmc;
145311a2bb38Srie 			Aliste	lmco;
145411a2bb38Srie 
145524a6229eSrie 			if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
145624a6229eSrie 				if ((lmc = alist_append(&(lml->lm_lists), 0,
145724a6229eSrie 				    sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0)
145824a6229eSrie 					return ((Sym *)0);
14597c478bd9Sstevel@tonic-gate 				lmco = (Aliste)((char *)lmc -
14607c478bd9Sstevel@tonic-gate 				    (char *)lml->lm_lists);
146124a6229eSrie 			} else {
146224a6229eSrie 				lmc = 0;
14637c478bd9Sstevel@tonic-gate 				lmco = ALO_DATA;
146424a6229eSrie 			}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 			pnp = hwcap_filtees(pnpp, lmco, dip, ilmp, filtees,
14677c478bd9Sstevel@tonic-gate 			    mode, (FLG_RT_HANDLE | FLG_RT_HWCAP));
146811a2bb38Srie 
146911a2bb38Srie 			/*
147011a2bb38Srie 			 * Now that any hardware capability objects have been
147111a2bb38Srie 			 * processed, remove any link-map control list.
147211a2bb38Srie 			 */
147311a2bb38Srie 			if (lmc) {
147411a2bb38Srie 				if (pnp->p_len == 0)
147511a2bb38Srie 					(void) lm_salvage(lml, 0, lmco);
147611a2bb38Srie 				remove_cntl(lml, lmco);
147711a2bb38Srie 			}
14787c478bd9Sstevel@tonic-gate 		}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
14817c478bd9Sstevel@tonic-gate 			continue;
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 		/*
14847c478bd9Sstevel@tonic-gate 		 * Process an individual filtee.
14857c478bd9Sstevel@tonic-gate 		 */
14867c478bd9Sstevel@tonic-gate 		if (pnp->p_info == 0) {
14877c478bd9Sstevel@tonic-gate 			const char	*filtee = pnp->p_name;
14887c478bd9Sstevel@tonic-gate 			int		audit = 0;
14897c478bd9Sstevel@tonic-gate 
14905aefb655Srie 			DBG_CALL(Dbg_file_filtee(lml, NAME(ilmp), filtee, 0));
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 			ghp = 0;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 			/*
14957c478bd9Sstevel@tonic-gate 			 * Determine if the reference link map is already
14967c478bd9Sstevel@tonic-gate 			 * loaded.  As an optimization compare the filtee with
14977c478bd9Sstevel@tonic-gate 			 * our interpretor.  The most common filter is
14987c478bd9Sstevel@tonic-gate 			 * libdl.so.1, which is a filter on ld.so.1.
14997c478bd9Sstevel@tonic-gate 			 */
15007c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
15017c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) {
15027c478bd9Sstevel@tonic-gate #else
15037c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) {
15047c478bd9Sstevel@tonic-gate #endif
15057c478bd9Sstevel@tonic-gate 				/*
15067c478bd9Sstevel@tonic-gate 				 * Create an association between ld.so.1 and
15077c478bd9Sstevel@tonic-gate 				 * the filter.
15087c478bd9Sstevel@tonic-gate 				 */
15097c478bd9Sstevel@tonic-gate 				nlmp = lml_rtld.lm_head;
15107c478bd9Sstevel@tonic-gate 				if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp,
15117c478bd9Sstevel@tonic-gate 				    (GPH_LDSO | GPH_FIRST | GPH_FILTEE))) == 0)
15127c478bd9Sstevel@tonic-gate 					nlmp = 0;
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 				/*
15157c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
15167c478bd9Sstevel@tonic-gate 				 * recursion.
15177c478bd9Sstevel@tonic-gate 				 */
15187c478bd9Sstevel@tonic-gate 				if (nlmp && ghp)
15197c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 				/*
15227c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  Ignore
15237c478bd9Sstevel@tonic-gate 				 * any return from the auditor, as we can't
15247c478bd9Sstevel@tonic-gate 				 * allow ignore filtering to ld.so.1, otherwise
15257c478bd9Sstevel@tonic-gate 				 * nothing is going to work.
15267c478bd9Sstevel@tonic-gate 				 */
15277c478bd9Sstevel@tonic-gate 				if ((lml->lm_tflags | FLAGS1(ilmp)) &
15287c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)
15297c478bd9Sstevel@tonic-gate 					(void) audit_objfilter(ilmp, filtees,
15307c478bd9Sstevel@tonic-gate 					    nlmp, 0);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 			} else {
15337c478bd9Sstevel@tonic-gate 				Rej_desc	rej = { 0 };
153411a2bb38Srie 				Lm_cntl		*lmc;
153511a2bb38Srie 				Aliste		lmco;
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 				/*
15387c478bd9Sstevel@tonic-gate 				 * Establish a new link-map control list from
15397c478bd9Sstevel@tonic-gate 				 * which to analyze any newly added objects.
15407c478bd9Sstevel@tonic-gate 				 */
154124a6229eSrie 				if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
154224a6229eSrie 					if ((lmc =
154324a6229eSrie 					    alist_append(&(lml->lm_lists), 0,
154424a6229eSrie 					    sizeof (Lm_cntl),
154524a6229eSrie 					    AL_CNT_LMLISTS)) == 0)
154624a6229eSrie 						return ((Sym *)0);
15477c478bd9Sstevel@tonic-gate 					lmco = (Aliste)((char *)lmc -
15487c478bd9Sstevel@tonic-gate 					    (char *)lml->lm_lists);
154924a6229eSrie 				} else {
155024a6229eSrie 					lmc = 0;
15517c478bd9Sstevel@tonic-gate 					lmco = ALO_DATA;
155224a6229eSrie 				}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 				/*
15557c478bd9Sstevel@tonic-gate 				 * Load the filtee.
15567c478bd9Sstevel@tonic-gate 				 */
15577c478bd9Sstevel@tonic-gate 				if ((nlmp = load_path(lml, lmco, filtee, ilmp,
15587c478bd9Sstevel@tonic-gate 				    mode, FLG_RT_HANDLE, &ghp, 0, &rej)) == 0) {
15597c478bd9Sstevel@tonic-gate 					file_notfound(LIST(ilmp), filtee, ilmp,
15607c478bd9Sstevel@tonic-gate 					    FLG_RT_HANDLE, &rej);
15617c478bd9Sstevel@tonic-gate 					remove_rej(&rej);
15627c478bd9Sstevel@tonic-gate 				}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 				/*
15657c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
15667c478bd9Sstevel@tonic-gate 				 * recursion.
15677c478bd9Sstevel@tonic-gate 				 */
15687c478bd9Sstevel@tonic-gate 				if (nlmp && ghp) {
15697c478bd9Sstevel@tonic-gate 					ghp->gh_flags |= GPH_FILTEE;
15707c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
15717c478bd9Sstevel@tonic-gate 				}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 				/*
15747c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  A
15757c478bd9Sstevel@tonic-gate 				 * return of 0 indicates the auditor wishes to
15767c478bd9Sstevel@tonic-gate 				 * ignore this filtee.
15777c478bd9Sstevel@tonic-gate 				 */
15787c478bd9Sstevel@tonic-gate 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
15797c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)) {
15807c478bd9Sstevel@tonic-gate 					if (audit_objfilter(ilmp, filtees,
15817c478bd9Sstevel@tonic-gate 					    nlmp, 0) == 0) {
15827c478bd9Sstevel@tonic-gate 						audit = 1;
15837c478bd9Sstevel@tonic-gate 						nlmp = 0;
15847c478bd9Sstevel@tonic-gate 					}
15857c478bd9Sstevel@tonic-gate 				}
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 				/*
15887c478bd9Sstevel@tonic-gate 				 * Finish processing the objects associated with
15897c478bd9Sstevel@tonic-gate 				 * this request.  Create an association between
15907c478bd9Sstevel@tonic-gate 				 * this object and the originating filter to
15917c478bd9Sstevel@tonic-gate 				 * provide sufficient information to tear down
15927c478bd9Sstevel@tonic-gate 				 * this filtee if necessary.
15937c478bd9Sstevel@tonic-gate 				 */
15947c478bd9Sstevel@tonic-gate 				if (nlmp && ghp &&
15957c478bd9Sstevel@tonic-gate 				    ((analyze_lmc(lml, lmco, nlmp) == 0) ||
15967c478bd9Sstevel@tonic-gate 				    (relocate_lmc(lml, lmco, nlmp) == 0)))
15977c478bd9Sstevel@tonic-gate 					nlmp = 0;
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 				/*
16007c478bd9Sstevel@tonic-gate 				 * If the filtee has been successfully
16017c478bd9Sstevel@tonic-gate 				 * processed, and it is part of a link-map
16027c478bd9Sstevel@tonic-gate 				 * control list that is equivalent, or less,
16037c478bd9Sstevel@tonic-gate 				 * than the filter control list, create an
16047c478bd9Sstevel@tonic-gate 				 * association between the filter and filtee.
16057c478bd9Sstevel@tonic-gate 				 * This association provides sufficient
16067c478bd9Sstevel@tonic-gate 				 * information to tear down the filter and
16077c478bd9Sstevel@tonic-gate 				 * filtee if necessary.
16087c478bd9Sstevel@tonic-gate 				 */
16097c478bd9Sstevel@tonic-gate 				if (nlmp && ghp && (CNTL(nlmp) <= CNTL(ilmp)) &&
16107c478bd9Sstevel@tonic-gate 				    (hdl_add(ghp, ilmp, GPD_FILTER) == 0))
16117c478bd9Sstevel@tonic-gate 					nlmp = 0;
161211a2bb38Srie 
161311a2bb38Srie 				/*
161411a2bb38Srie 				 * Now that this object has been processed,
161511a2bb38Srie 				 * remove any link-map control list.
161611a2bb38Srie 				 */
161711a2bb38Srie 				if (lmc) {
161811a2bb38Srie 					if (nlmp == 0)
161911a2bb38Srie 						(void) lm_salvage(lml, 0, lmco);
162011a2bb38Srie 					remove_cntl(lml, lmco);
162111a2bb38Srie 				}
16227c478bd9Sstevel@tonic-gate 			}
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 			/*
16257c478bd9Sstevel@tonic-gate 			 * Generate a diagnostic if the filtee couldn't be
16267c478bd9Sstevel@tonic-gate 			 * loaded, null out the pnode entry, and continue
16277c478bd9Sstevel@tonic-gate 			 * the search.  Otherwise, retain this group handle
16287c478bd9Sstevel@tonic-gate 			 * for future symbol searches.
16297c478bd9Sstevel@tonic-gate 			 */
16307c478bd9Sstevel@tonic-gate 			if (nlmp == 0) {
16317c478bd9Sstevel@tonic-gate 				pnp->p_info = 0;
16325aefb655Srie 				DBG_CALL(Dbg_file_filtee(lml, 0, filtee,
16335aefb655Srie 				    audit));
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 				if (ghp)
16367c478bd9Sstevel@tonic-gate 					(void) dlclose_core(ghp, ilmp);
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate 				pnp->p_len = 0;
16397c478bd9Sstevel@tonic-gate 				continue;
16407c478bd9Sstevel@tonic-gate 			}
16417c478bd9Sstevel@tonic-gate 		}
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 		ghp = (Grp_hdl *)pnp->p_info;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 		/*
16467c478bd9Sstevel@tonic-gate 		 * If we're just here to trigger filtee loading skip the symbol
16477c478bd9Sstevel@tonic-gate 		 * lookup so we'll continue looking for additional filtees.
16487c478bd9Sstevel@tonic-gate 		 */
16497c478bd9Sstevel@tonic-gate 		if (name) {
16507c478bd9Sstevel@tonic-gate 			Grp_desc	*gdp;
16517c478bd9Sstevel@tonic-gate 			Sym		*sym = 0;
16527c478bd9Sstevel@tonic-gate 			Aliste		off;
16537c478bd9Sstevel@tonic-gate 			Slookup		sl = *slp;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 			sl.sl_flags |= LKUP_FIRST;
16567c478bd9Sstevel@tonic-gate 			any++;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 			/*
16597c478bd9Sstevel@tonic-gate 			 * Look for the symbol in the handles dependencies.
16607c478bd9Sstevel@tonic-gate 			 */
16617c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) {
16627c478bd9Sstevel@tonic-gate 				if ((gdp->gd_flags & GPD_AVAIL) == 0)
16637c478bd9Sstevel@tonic-gate 					continue;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 				/*
16667c478bd9Sstevel@tonic-gate 				 * If our parent is a dependency don't look at
16677c478bd9Sstevel@tonic-gate 				 * it (otherwise we are in a recursive loop).
16687c478bd9Sstevel@tonic-gate 				 * This situation can occur with auxiliary
16697c478bd9Sstevel@tonic-gate 				 * filters if the filtee has a dependency on the
16707c478bd9Sstevel@tonic-gate 				 * filter.  This dependency isn't necessary as
16717c478bd9Sstevel@tonic-gate 				 * auxiliary filters are opened RTLD_PARENT, but
16727c478bd9Sstevel@tonic-gate 				 * users may still unknowingly add an explicit
16737c478bd9Sstevel@tonic-gate 				 * dependency to the parent.
16747c478bd9Sstevel@tonic-gate 				 */
16757c478bd9Sstevel@tonic-gate 				if ((sl.sl_imap = gdp->gd_depend) == ilmp)
16767c478bd9Sstevel@tonic-gate 					continue;
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 				if (((sym = SYMINTP(sl.sl_imap)(&sl, dlmp,
16797c478bd9Sstevel@tonic-gate 				    binfo)) != 0) ||
16807c478bd9Sstevel@tonic-gate 				    (ghp->gh_flags & GPH_FIRST))
16817c478bd9Sstevel@tonic-gate 					break;
16827c478bd9Sstevel@tonic-gate 			}
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 			/*
16857c478bd9Sstevel@tonic-gate 			 * If this filtee has just been loaded (nlmp != 0),
16867c478bd9Sstevel@tonic-gate 			 * determine whether the filtee was triggered by a
16877c478bd9Sstevel@tonic-gate 			 * relocation from an object that is still being
16887c478bd9Sstevel@tonic-gate 			 * relocated on a leaf link-map control list.  As the
16897c478bd9Sstevel@tonic-gate 			 * relocation of an object on this list might still
16907c478bd9Sstevel@tonic-gate 			 * fail, we can't yet bind the filter to the filtee.
16917c478bd9Sstevel@tonic-gate 			 * To do so, would be locking the filtee so that it
16927c478bd9Sstevel@tonic-gate 			 * couldn't be deleted, and the filtee itself could have
16937c478bd9Sstevel@tonic-gate 			 * bound to an object that must be torn down.  Insure
16947c478bd9Sstevel@tonic-gate 			 * the caller isn't bound to the handle at this time.
16957c478bd9Sstevel@tonic-gate 			 * Any association will be reestablished when the filter
16967c478bd9Sstevel@tonic-gate 			 * is later referenced and the filtee has propagated to
16977c478bd9Sstevel@tonic-gate 			 * the same link-map control list.
16987c478bd9Sstevel@tonic-gate 			 */
16997c478bd9Sstevel@tonic-gate 			if (nlmp && (CNTL(nlmp) > CNTL(ilmp))) {
17007c478bd9Sstevel@tonic-gate 				remove_caller(ghp, ilmp);
17017c478bd9Sstevel@tonic-gate 				pnp->p_info = 0;
17027c478bd9Sstevel@tonic-gate 			}
17037c478bd9Sstevel@tonic-gate 			if (sym) {
17047c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_FILTEE;
17057c478bd9Sstevel@tonic-gate 				return (sym);
17067c478bd9Sstevel@tonic-gate 			}
17077c478bd9Sstevel@tonic-gate 		}
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate 		/*
17107c478bd9Sstevel@tonic-gate 		 * If this object is tagged to terminate filtee processing we're
17117c478bd9Sstevel@tonic-gate 		 * done.
17127c478bd9Sstevel@tonic-gate 		 */
17135aefb655Srie 		if (FLAGS1(ghp->gh_ownlmp) & FL1_RT_ENDFILTE)
17147c478bd9Sstevel@tonic-gate 			break;
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	/*
17187c478bd9Sstevel@tonic-gate 	 * If we're just here to trigger filtee loading then we're done.
17197c478bd9Sstevel@tonic-gate 	 */
17207c478bd9Sstevel@tonic-gate 	if (name == 0)
17217c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	/*
17247c478bd9Sstevel@tonic-gate 	 * If no filtees have been found for a filter, clean up any Pnode
17257c478bd9Sstevel@tonic-gate 	 * structures and disable their search completely.  For auxiliary
17267c478bd9Sstevel@tonic-gate 	 * filters we can reselect the symbol search function so that we never
17277c478bd9Sstevel@tonic-gate 	 * enter this routine again for this object.  For standard filters we
17287c478bd9Sstevel@tonic-gate 	 * use the null symbol routine.
17297c478bd9Sstevel@tonic-gate 	 */
17307c478bd9Sstevel@tonic-gate 	if (any == 0) {
17317c478bd9Sstevel@tonic-gate 		remove_pnode((Pnode *)dip->di_info);
17327c478bd9Sstevel@tonic-gate 		elf_disable_filtee(ilmp, dip);
17337c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17347c478bd9Sstevel@tonic-gate 	}
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate /*
17407c478bd9Sstevel@tonic-gate  * Focal point for disabling error messages for auxiliary filters.  As an
17417c478bd9Sstevel@tonic-gate  * auxiliary filter allows for filtee use, but provides a fallback should a
17427c478bd9Sstevel@tonic-gate  * filtee not exist (or fail to load), any errors generated as a consequence of
17437c478bd9Sstevel@tonic-gate  * trying to load the filtees are typically suppressed.  Setting RT_FL_SILENCERR
17447c478bd9Sstevel@tonic-gate  * suppresses errors generated by eprint(), but insures a debug diagnostic is
17457c478bd9Sstevel@tonic-gate  * produced.  ldd(1) employs printf(), and here, the selection of whether to
17467c478bd9Sstevel@tonic-gate  * print a diagnostic in regards to auxiliary filters is a little more complex.
17477c478bd9Sstevel@tonic-gate  *
17487c478bd9Sstevel@tonic-gate  *   .	The determination of whether to produce an ldd message, or a fatal
17497c478bd9Sstevel@tonic-gate  *	error message is driven by LML_FLG_TRC_ENABLE.
17507c478bd9Sstevel@tonic-gate  *   .	More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN,
17517c478bd9Sstevel@tonic-gate  *	(ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s),
17527c478bd9Sstevel@tonic-gate  *	and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u).
17537c478bd9Sstevel@tonic-gate  *
17547c478bd9Sstevel@tonic-gate  *   .	If the calling object is lddstub, then several classes of message are
17557c478bd9Sstevel@tonic-gate  *	suppressed.  The user isn't trying to diagnose lddstub, this is simply
17567c478bd9Sstevel@tonic-gate  *	a stub executable employed to preload a user specified library against.
17577c478bd9Sstevel@tonic-gate  *
17587c478bd9Sstevel@tonic-gate  *   .	If RT_FL_SILENCERR is in effect then any generic ldd() messages should
17597c478bd9Sstevel@tonic-gate  *	be suppressed.  All detailed ldd messages should still be produced.
17607c478bd9Sstevel@tonic-gate  */
17617c478bd9Sstevel@tonic-gate Sym *
17627c478bd9Sstevel@tonic-gate elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
17637c478bd9Sstevel@tonic-gate {
17647c478bd9Sstevel@tonic-gate 	Sym	*sym;
17657c478bd9Sstevel@tonic-gate 	Dyninfo	*dip = &DYNINFO(slp->sl_imap)[ndx];
17667c478bd9Sstevel@tonic-gate 	int	silent = 0;
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	/*
17697c478bd9Sstevel@tonic-gate 	 * Make sure this entry is still acting as a filter.  We may have tried
17707c478bd9Sstevel@tonic-gate 	 * to process this previously, and disabled it if the filtee couldn't
17717c478bd9Sstevel@tonic-gate 	 * be processed.  However, other entries may provide different filtees
17727c478bd9Sstevel@tonic-gate 	 * that are yet to be completed.
17737c478bd9Sstevel@tonic-gate 	 */
17747c478bd9Sstevel@tonic-gate 	if (dip->di_flags == 0)
17757c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	/*
17787c478bd9Sstevel@tonic-gate 	 * Indicate whether an error message is required should this filtee not
17797c478bd9Sstevel@tonic-gate 	 * be found, based on the type of filter.
17807c478bd9Sstevel@tonic-gate 	 */
17817c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_AUXFLTR) &&
17827c478bd9Sstevel@tonic-gate 	    ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) {
17837c478bd9Sstevel@tonic-gate 		rtld_flags |= RT_FL_SILENCERR;
17847c478bd9Sstevel@tonic-gate 		silent = 1;
17857c478bd9Sstevel@tonic-gate 	}
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate 	sym = _elf_lookup_filtee(slp, dlmp, binfo, ndx);
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	if (silent)
17907c478bd9Sstevel@tonic-gate 		rtld_flags &= ~RT_FL_SILENCERR;
17917c478bd9Sstevel@tonic-gate 
17927c478bd9Sstevel@tonic-gate 	return (sym);
17937c478bd9Sstevel@tonic-gate }
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate /*
17967c478bd9Sstevel@tonic-gate  * Compute the elf hash value (as defined in the ELF access library).
17977c478bd9Sstevel@tonic-gate  * The form of the hash table is:
17987c478bd9Sstevel@tonic-gate  *
17997c478bd9Sstevel@tonic-gate  *	|--------------|
18007c478bd9Sstevel@tonic-gate  *	| # of buckets |
18017c478bd9Sstevel@tonic-gate  *	|--------------|
18027c478bd9Sstevel@tonic-gate  *	| # of chains  |
18037c478bd9Sstevel@tonic-gate  *	|--------------|
18047c478bd9Sstevel@tonic-gate  *	|   bucket[]   |
18057c478bd9Sstevel@tonic-gate  *	|--------------|
18067c478bd9Sstevel@tonic-gate  *	|   chain[]    |
18077c478bd9Sstevel@tonic-gate  *	|--------------|
18087c478bd9Sstevel@tonic-gate  */
18097c478bd9Sstevel@tonic-gate ulong_t
18107c478bd9Sstevel@tonic-gate elf_hash(const char *name)
18117c478bd9Sstevel@tonic-gate {
18127c478bd9Sstevel@tonic-gate 	uint_t	hval = 0;
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	while (*name) {
18157c478bd9Sstevel@tonic-gate 		uint_t	g;
18167c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *name++;
18177c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
18187c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
18197c478bd9Sstevel@tonic-gate 		hval &= ~g;
18207c478bd9Sstevel@tonic-gate 	}
18217c478bd9Sstevel@tonic-gate 	return ((ulong_t)hval);
18227c478bd9Sstevel@tonic-gate }
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate /*
18257c478bd9Sstevel@tonic-gate  * If flag argument has LKUP_SPEC set, we treat undefined symbols of type
18267c478bd9Sstevel@tonic-gate  * function specially in the executable - if they have a value, even though
18277c478bd9Sstevel@tonic-gate  * undefined, we use that value.  This allows us to associate all references
18287c478bd9Sstevel@tonic-gate  * to a function's address to a single place in the process: the plt entry
18297c478bd9Sstevel@tonic-gate  * for that function in the executable.  Calls to lookup from plt binding
18307c478bd9Sstevel@tonic-gate  * routines do NOT set LKUP_SPEC in the flag.
18317c478bd9Sstevel@tonic-gate  */
18327c478bd9Sstevel@tonic-gate Sym *
18337c478bd9Sstevel@tonic-gate elf_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
18347c478bd9Sstevel@tonic-gate {
18357c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
18367c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
18377c478bd9Sstevel@tonic-gate 	ulong_t		hash = slp->sl_hash;
18387c478bd9Sstevel@tonic-gate 	uint_t		ndx, htmp, buckets, *chainptr;
18397c478bd9Sstevel@tonic-gate 	Sym		*sym, *symtabptr;
18407c478bd9Sstevel@tonic-gate 	char		*strtabptr, *strtabname;
18417c478bd9Sstevel@tonic-gate 	uint_t		flags1;
18427c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
18437c478bd9Sstevel@tonic-gate 
1844660acd81Srie 	/*
1845660acd81Srie 	 * If we're only here to establish a symbols index, skip the diagnostic
1846660acd81Srie 	 * used to trace a symbol search.
1847660acd81Srie 	 */
18485aefb655Srie 	if ((slp->sl_flags & LKUP_SYMNDX) == 0)
18495aefb655Srie 		DBG_CALL(Dbg_syms_lookup(ilmp, name, MSG_ORIG(MSG_STR_ELF)));
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	if (HASH(ilmp) == 0)
18527c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	buckets = HASH(ilmp)[0];
18557c478bd9Sstevel@tonic-gate 	/* LINTED */
18567c478bd9Sstevel@tonic-gate 	htmp = (uint_t)hash % buckets;
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	/*
18597c478bd9Sstevel@tonic-gate 	 * Get the first symbol on hash chain and initialize the string
18607c478bd9Sstevel@tonic-gate 	 * and symbol table pointers.
18617c478bd9Sstevel@tonic-gate 	 */
18627c478bd9Sstevel@tonic-gate 	if ((ndx = HASH(ilmp)[htmp + 2]) == 0)
18637c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 	chainptr = HASH(ilmp) + 2 + buckets;
18667c478bd9Sstevel@tonic-gate 	strtabptr = STRTAB(ilmp);
18677c478bd9Sstevel@tonic-gate 	symtabptr = SYMTAB(ilmp);
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	while (ndx) {
18707c478bd9Sstevel@tonic-gate 		sym = symtabptr + ndx;
18717c478bd9Sstevel@tonic-gate 		strtabname = strtabptr + sym->st_name;
18727c478bd9Sstevel@tonic-gate 
18737c478bd9Sstevel@tonic-gate 		/*
18747c478bd9Sstevel@tonic-gate 		 * Compare the symbol found with the name required.  If the
18757c478bd9Sstevel@tonic-gate 		 * names don't match continue with the next hash entry.
18767c478bd9Sstevel@tonic-gate 		 */
18777c478bd9Sstevel@tonic-gate 		if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
18787c478bd9Sstevel@tonic-gate 			if ((ndx = chainptr[ndx]) != 0)
18797c478bd9Sstevel@tonic-gate 				continue;
18807c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
18817c478bd9Sstevel@tonic-gate 		}
18827c478bd9Sstevel@tonic-gate 
1883660acd81Srie 		/*
1884660acd81Srie 		 * If we're only here to establish a symbols index, we're done.
1885660acd81Srie 		 */
1886660acd81Srie 		if (slp->sl_flags & LKUP_SYMNDX)
1887660acd81Srie 			return (sym);
1888660acd81Srie 
18897c478bd9Sstevel@tonic-gate 		/*
18907c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is defined, return the
18917c478bd9Sstevel@tonic-gate 		 * symbol pointer and the link map in which it was found.
18927c478bd9Sstevel@tonic-gate 		 */
18937c478bd9Sstevel@tonic-gate 		if (sym->st_shndx != SHN_UNDEF) {
18947c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
18957c478bd9Sstevel@tonic-gate 			*binfo |= DBG_BINFO_FOUND;
1896*9a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
1897*9a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
1898*9a411307Srie 			    is_sym_interposer(ilmp, sym)))
18997c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
19007c478bd9Sstevel@tonic-gate 			break;
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 		/*
19037c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is undefined, the
19047c478bd9Sstevel@tonic-gate 		 * symbol type is a function, and the value of the symbol
19057c478bd9Sstevel@tonic-gate 		 * is non zero, then this is a special case.  This allows
19067c478bd9Sstevel@tonic-gate 		 * the resolution of a function address to the plt[] entry.
19077c478bd9Sstevel@tonic-gate 		 * See SPARC ABI, Dynamic Linking, Function Addresses for
19087c478bd9Sstevel@tonic-gate 		 * more details.
19097c478bd9Sstevel@tonic-gate 		 */
1910660acd81Srie 		} else if ((slp->sl_flags & LKUP_SPEC) &&
19117c478bd9Sstevel@tonic-gate 		    (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) &&
19127c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
19137c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
19147c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR);
1915*9a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
1916*9a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
1917*9a411307Srie 			    is_sym_interposer(ilmp, sym)))
19187c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
19197c478bd9Sstevel@tonic-gate 			return (sym);
19207c478bd9Sstevel@tonic-gate 		}
19217c478bd9Sstevel@tonic-gate 
19227c478bd9Sstevel@tonic-gate 		/*
19237c478bd9Sstevel@tonic-gate 		 * Undefined symbol.
19247c478bd9Sstevel@tonic-gate 		 */
19257c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
19267c478bd9Sstevel@tonic-gate 	}
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/*
19297c478bd9Sstevel@tonic-gate 	 * We've found a match.  Determine if the defining object contains
19307c478bd9Sstevel@tonic-gate 	 * symbol binding information.
19317c478bd9Sstevel@tonic-gate 	 */
19327c478bd9Sstevel@tonic-gate 	if ((sip = SYMINFO(ilmp)) != 0)
19339039eeafSab 		sip += ndx;
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	/*
19367c478bd9Sstevel@tonic-gate 	 * If this is a direct binding request, but the symbol definition has
19377c478bd9Sstevel@tonic-gate 	 * disabled directly binding to it (presumably because the symbol
19387c478bd9Sstevel@tonic-gate 	 * definition has been changed since the referring object was built),
19397c478bd9Sstevel@tonic-gate 	 * indicate this failure so that the caller can fall back to a standard
19407c478bd9Sstevel@tonic-gate 	 * symbol search.  Clear any debug binding information for cleanliness.
19417c478bd9Sstevel@tonic-gate 	 */
19427c478bd9Sstevel@tonic-gate 	if (sip && (slp->sl_flags & LKUP_DIRECT) &&
19437c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) {
19447c478bd9Sstevel@tonic-gate 		*binfo |= BINFO_DIRECTDIS;
19457c478bd9Sstevel@tonic-gate 		*binfo &= ~DBG_BINFO_MSK;
19467c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
19477c478bd9Sstevel@tonic-gate 	}
19487c478bd9Sstevel@tonic-gate 
19497c478bd9Sstevel@tonic-gate 	/*
19507c478bd9Sstevel@tonic-gate 	 * Determine whether this object is acting as a filter.
19517c478bd9Sstevel@tonic-gate 	 */
19527c478bd9Sstevel@tonic-gate 	if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0)
19537c478bd9Sstevel@tonic-gate 		return (sym);
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	/*
19567c478bd9Sstevel@tonic-gate 	 * Determine if this object offers per-symbol filtering, and if so,
19577c478bd9Sstevel@tonic-gate 	 * whether this symbol references a filtee.
19587c478bd9Sstevel@tonic-gate 	 */
19597c478bd9Sstevel@tonic-gate 	if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) {
19607c478bd9Sstevel@tonic-gate 		/*
19617c478bd9Sstevel@tonic-gate 		 * If this is a standard filter reference, and no standard
19627c478bd9Sstevel@tonic-gate 		 * filtees remain to be inspected, we're done.  If this is an
19637c478bd9Sstevel@tonic-gate 		 * auxiliary filter reference, and no auxiliary filtees remain,
19647c478bd9Sstevel@tonic-gate 		 * we'll fall through in case any object filtering is available.
19657c478bd9Sstevel@tonic-gate 		 */
19667c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) &&
19677c478bd9Sstevel@tonic-gate 		    (SYMSFLTRCNT(ilmp) == 0))
19687c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) ||
19717c478bd9Sstevel@tonic-gate 		    ((sip->si_flags & SYMINFO_FLG_AUXILIARY) &&
19727c478bd9Sstevel@tonic-gate 		    SYMAFLTRCNT(ilmp))) {
19737c478bd9Sstevel@tonic-gate 			Sym *	fsym;
19747c478bd9Sstevel@tonic-gate 
19757c478bd9Sstevel@tonic-gate 			/*
19767c478bd9Sstevel@tonic-gate 			 * This symbol has an associated filtee.  Lookup the
19777c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
19787c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
19797c478bd9Sstevel@tonic-gate 			 * filter, return an error, otherwise fall through to
19807c478bd9Sstevel@tonic-gate 			 * catch any object filtering that may be available.
19817c478bd9Sstevel@tonic-gate 			 */
19827c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
19837c478bd9Sstevel@tonic-gate 			    sip->si_boundto)) != 0)
19847c478bd9Sstevel@tonic-gate 				return (fsym);
19857c478bd9Sstevel@tonic-gate 			if (sip->si_flags & SYMINFO_FLG_FILTER)
19867c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
19877c478bd9Sstevel@tonic-gate 		}
19887c478bd9Sstevel@tonic-gate 	}
19897c478bd9Sstevel@tonic-gate 
19907c478bd9Sstevel@tonic-gate 	/*
19917c478bd9Sstevel@tonic-gate 	 * Determine if this object provides global filtering.
19927c478bd9Sstevel@tonic-gate 	 */
19937c478bd9Sstevel@tonic-gate 	if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) {
19947c478bd9Sstevel@tonic-gate 		Sym *	fsym;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) {
19977c478bd9Sstevel@tonic-gate 			/*
19987c478bd9Sstevel@tonic-gate 			 * This object has an associated filtee.  Lookup the
19997c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
20007c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
20017c478bd9Sstevel@tonic-gate 			 * filter, return and error, otherwise return the symbol
20027c478bd9Sstevel@tonic-gate 			 * within the filter itself.
20037c478bd9Sstevel@tonic-gate 			 */
20047c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
20057c478bd9Sstevel@tonic-gate 			    OBJFLTRNDX(ilmp))) != 0)
20067c478bd9Sstevel@tonic-gate 				return (fsym);
20077c478bd9Sstevel@tonic-gate 		}
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 		if (flags1 & FL1_RT_OBJSFLTR)
20107c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
20117c478bd9Sstevel@tonic-gate 	}
20127c478bd9Sstevel@tonic-gate 	return (sym);
20137c478bd9Sstevel@tonic-gate }
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate /*
20167c478bd9Sstevel@tonic-gate  * Create a new Rt_map structure for an ELF object and initialize
20177c478bd9Sstevel@tonic-gate  * all values.
20187c478bd9Sstevel@tonic-gate  */
20197c478bd9Sstevel@tonic-gate Rt_map *
20207c478bd9Sstevel@tonic-gate elf_new_lm(Lm_list *lml, const char *pname, const char *oname, Dyn *ld,
20217c478bd9Sstevel@tonic-gate     ulong_t addr, ulong_t etext, Aliste lmco, ulong_t msize, ulong_t entry,
20227c478bd9Sstevel@tonic-gate     ulong_t paddr, ulong_t padimsize, Mmap *mmaps, uint_t mmapcnt)
20237c478bd9Sstevel@tonic-gate {
20247c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
20257c478bd9Sstevel@tonic-gate 	ulong_t		base, fltr = 0, audit = 0, cfile = 0, crle = 0;
20267c478bd9Sstevel@tonic-gate 	Xword		rpath = 0;
20277c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr = (Ehdr *)addr;
20287c478bd9Sstevel@tonic-gate 
20295aefb655Srie 	DBG_CALL(Dbg_file_elf(lml, pname, (ulong_t)ld, addr, msize, entry,
20305aefb655Srie 	    lml->lm_lmidstr, lmco));
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	/*
20337c478bd9Sstevel@tonic-gate 	 * Allocate space for the link-map and private elf information.  Once
20347c478bd9Sstevel@tonic-gate 	 * these are allocated and initialized, we can use remove_so(0, lmp) to
20357c478bd9Sstevel@tonic-gate 	 * tear down the link-map should any failures occur.
20367c478bd9Sstevel@tonic-gate 	 */
20377c478bd9Sstevel@tonic-gate 	if ((lmp = calloc(sizeof (Rt_map), 1)) == 0)
20387c478bd9Sstevel@tonic-gate 		return (0);
20397c478bd9Sstevel@tonic-gate 	if ((ELFPRV(lmp) = calloc(sizeof (Rt_elfp), 1)) == 0) {
20407c478bd9Sstevel@tonic-gate 		free(lmp);
20417c478bd9Sstevel@tonic-gate 		return (0);
20427c478bd9Sstevel@tonic-gate 	}
20437c478bd9Sstevel@tonic-gate 
20447c478bd9Sstevel@tonic-gate 	/*
20457c478bd9Sstevel@tonic-gate 	 * All fields not filled in were set to 0 by calloc.
20467c478bd9Sstevel@tonic-gate 	 */
20477c478bd9Sstevel@tonic-gate 	ORIGNAME(lmp) = PATHNAME(lmp) = NAME(lmp) = (char *)pname;
20487c478bd9Sstevel@tonic-gate 	DYN(lmp) = ld;
20497c478bd9Sstevel@tonic-gate 	ADDR(lmp) = addr;
20507c478bd9Sstevel@tonic-gate 	MSIZE(lmp) = msize;
20517c478bd9Sstevel@tonic-gate 	ENTRY(lmp) = (Addr)entry;
20527c478bd9Sstevel@tonic-gate 	SYMINTP(lmp) = elf_find_sym;
20537c478bd9Sstevel@tonic-gate 	ETEXT(lmp) = etext;
20547c478bd9Sstevel@tonic-gate 	FCT(lmp) = &elf_fct;
20557c478bd9Sstevel@tonic-gate 	LIST(lmp) = lml;
20567c478bd9Sstevel@tonic-gate 	PADSTART(lmp) = paddr;
20577c478bd9Sstevel@tonic-gate 	PADIMLEN(lmp) = padimsize;
20587c478bd9Sstevel@tonic-gate 	THREADID(lmp) = rt_thr_self();
20597c478bd9Sstevel@tonic-gate 	OBJFLTRNDX(lmp) = FLTR_DISABLED;
2060dffec89cSrie 	SORTVAL(lmp) = -1;
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate 	MMAPS(lmp) = mmaps;
20637c478bd9Sstevel@tonic-gate 	MMAPCNT(lmp) = mmapcnt;
20647c478bd9Sstevel@tonic-gate 	ASSERT(mmapcnt != 0);
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate 	/*
20677c478bd9Sstevel@tonic-gate 	 * If this is a shared object, add the base address to each address.
20687c478bd9Sstevel@tonic-gate 	 * if this is an executable, use address as is.
20697c478bd9Sstevel@tonic-gate 	 */
20707c478bd9Sstevel@tonic-gate 	if (ehdr->e_type == ET_EXEC) {
20717c478bd9Sstevel@tonic-gate 		base = 0;
20727c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_FIXED;
20737c478bd9Sstevel@tonic-gate 	} else
20747c478bd9Sstevel@tonic-gate 		base = addr;
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 	/*
20777c478bd9Sstevel@tonic-gate 	 * Fill in rest of the link map entries with information from the file's
20787c478bd9Sstevel@tonic-gate 	 * dynamic structure.
20797c478bd9Sstevel@tonic-gate 	 */
20807c478bd9Sstevel@tonic-gate 	if (ld) {
208110a4fa49Srie 		uint_t		dyncnt = 0;
208210a4fa49Srie 		Xword		pltpadsz = 0;
208310a4fa49Srie 		Rti_desc	*rti;
20847c478bd9Sstevel@tonic-gate 
20857c478bd9Sstevel@tonic-gate 		/* CSTYLED */
20867c478bd9Sstevel@tonic-gate 		for ( ; ld->d_tag != DT_NULL; ++ld, dyncnt++) {
20877c478bd9Sstevel@tonic-gate 			switch ((Xword)ld->d_tag) {
20887c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
20897c478bd9Sstevel@tonic-gate 				SYMTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
20907c478bd9Sstevel@tonic-gate 				break;
20919039eeafSab 			case DT_SUNW_SYMTAB:
20929039eeafSab 				SUNWSYMTAB(lmp) =
20939039eeafSab 				    (void *)(ld->d_un.d_ptr + base);
20949039eeafSab 				break;
20959039eeafSab 			case DT_SUNW_SYMSZ:
20969039eeafSab 				SUNWSYMSZ(lmp) = ld->d_un.d_val;
20979039eeafSab 				break;
20987c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
20997c478bd9Sstevel@tonic-gate 				STRTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
21007c478bd9Sstevel@tonic-gate 				break;
21017c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
21027c478bd9Sstevel@tonic-gate 				SYMENT(lmp) = ld->d_un.d_val;
21037c478bd9Sstevel@tonic-gate 				break;
21047c478bd9Sstevel@tonic-gate 			case DT_FEATURE_1:
21057c478bd9Sstevel@tonic-gate 				ld->d_un.d_val |= DTF_1_PARINIT;
21067c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DTF_1_CONFEXP)
21077c478bd9Sstevel@tonic-gate 					crle = 1;
21087c478bd9Sstevel@tonic-gate 				break;
21097c478bd9Sstevel@tonic-gate 			case DT_MOVESZ:
21107c478bd9Sstevel@tonic-gate 				MOVESZ(lmp) = ld->d_un.d_val;
21117c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_MOVE;
21127c478bd9Sstevel@tonic-gate 				break;
21137c478bd9Sstevel@tonic-gate 			case DT_MOVEENT:
21147c478bd9Sstevel@tonic-gate 				MOVEENT(lmp) = ld->d_un.d_val;
21157c478bd9Sstevel@tonic-gate 				break;
21167c478bd9Sstevel@tonic-gate 			case DT_MOVETAB:
21177c478bd9Sstevel@tonic-gate 				MOVETAB(lmp) = (void *)(ld->d_un.d_ptr + base);
21187c478bd9Sstevel@tonic-gate 				break;
21197c478bd9Sstevel@tonic-gate 			case DT_REL:
21207c478bd9Sstevel@tonic-gate 			case DT_RELA:
21217c478bd9Sstevel@tonic-gate 				/*
21227c478bd9Sstevel@tonic-gate 				 * At this time we can only handle 1 type of
21237c478bd9Sstevel@tonic-gate 				 * relocation per object.
21247c478bd9Sstevel@tonic-gate 				 */
21257c478bd9Sstevel@tonic-gate 				REL(lmp) = (void *)(ld->d_un.d_ptr + base);
21267c478bd9Sstevel@tonic-gate 				break;
21277c478bd9Sstevel@tonic-gate 			case DT_RELSZ:
21287c478bd9Sstevel@tonic-gate 			case DT_RELASZ:
21297c478bd9Sstevel@tonic-gate 				RELSZ(lmp) = ld->d_un.d_val;
21307c478bd9Sstevel@tonic-gate 				break;
21317c478bd9Sstevel@tonic-gate 			case DT_RELENT:
21327c478bd9Sstevel@tonic-gate 			case DT_RELAENT:
21337c478bd9Sstevel@tonic-gate 				RELENT(lmp) = ld->d_un.d_val;
21347c478bd9Sstevel@tonic-gate 				break;
21357c478bd9Sstevel@tonic-gate 			case DT_RELCOUNT:
21367c478bd9Sstevel@tonic-gate 			case DT_RELACOUNT:
21377c478bd9Sstevel@tonic-gate 				RELACOUNT(lmp) = (uint_t)ld->d_un.d_val;
21387c478bd9Sstevel@tonic-gate 				break;
21397c478bd9Sstevel@tonic-gate 			case DT_TEXTREL:
21407c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_TEXTREL;
21417c478bd9Sstevel@tonic-gate 				break;
21427c478bd9Sstevel@tonic-gate 			case DT_HASH:
21437c478bd9Sstevel@tonic-gate 				HASH(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
21447c478bd9Sstevel@tonic-gate 				break;
21457c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
21467c478bd9Sstevel@tonic-gate 				PLTGOT(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
21477c478bd9Sstevel@tonic-gate 				break;
21487c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
21497c478bd9Sstevel@tonic-gate 				PLTRELSZ(lmp) = ld->d_un.d_val;
21507c478bd9Sstevel@tonic-gate 				break;
21517c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
21527c478bd9Sstevel@tonic-gate 				JMPREL(lmp) = (void *)(ld->d_un.d_ptr + base);
21537c478bd9Sstevel@tonic-gate 				break;
21547c478bd9Sstevel@tonic-gate 			case DT_INIT:
21557c478bd9Sstevel@tonic-gate 				INIT(lmp) = (void (*)())(ld->d_un.d_ptr + base);
21567c478bd9Sstevel@tonic-gate 				break;
21577c478bd9Sstevel@tonic-gate 			case DT_FINI:
21587c478bd9Sstevel@tonic-gate 				FINI(lmp) = (void (*)())(ld->d_un.d_ptr + base);
21597c478bd9Sstevel@tonic-gate 				break;
21607c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAY:
21617c478bd9Sstevel@tonic-gate 				INITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21627c478bd9Sstevel@tonic-gate 				    base);
21637c478bd9Sstevel@tonic-gate 				break;
21647c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAYSZ:
21657c478bd9Sstevel@tonic-gate 				INITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21667c478bd9Sstevel@tonic-gate 				break;
21677c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAY:
21687c478bd9Sstevel@tonic-gate 				FINIARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21697c478bd9Sstevel@tonic-gate 				    base);
21707c478bd9Sstevel@tonic-gate 				break;
21717c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAYSZ:
21727c478bd9Sstevel@tonic-gate 				FINIARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21737c478bd9Sstevel@tonic-gate 				break;
21747c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAY:
21757c478bd9Sstevel@tonic-gate 				PREINITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21767c478bd9Sstevel@tonic-gate 				    base);
21777c478bd9Sstevel@tonic-gate 				break;
21787c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAYSZ:
21797c478bd9Sstevel@tonic-gate 				PREINITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21807c478bd9Sstevel@tonic-gate 				break;
21817c478bd9Sstevel@tonic-gate 			case DT_RPATH:
21827c478bd9Sstevel@tonic-gate 			case DT_RUNPATH:
21837c478bd9Sstevel@tonic-gate 				rpath = ld->d_un.d_val;
21847c478bd9Sstevel@tonic-gate 				break;
21857c478bd9Sstevel@tonic-gate 			case DT_FILTER:
21867c478bd9Sstevel@tonic-gate 				fltr = ld->d_un.d_val;
21877c478bd9Sstevel@tonic-gate 				OBJFLTRNDX(lmp) = dyncnt;
21887c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJSFLTR;
21897c478bd9Sstevel@tonic-gate 				break;
21907c478bd9Sstevel@tonic-gate 			case DT_AUXILIARY:
21917c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
21927c478bd9Sstevel@tonic-gate 					fltr = ld->d_un.d_val;
21937c478bd9Sstevel@tonic-gate 					OBJFLTRNDX(lmp) = dyncnt;
21947c478bd9Sstevel@tonic-gate 				}
21957c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJAFLTR;
21967c478bd9Sstevel@tonic-gate 				break;
21977c478bd9Sstevel@tonic-gate 			case DT_SUNW_FILTER:
21987c478bd9Sstevel@tonic-gate 				SYMSFLTRCNT(lmp)++;
21997c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMSFLTR;
22007c478bd9Sstevel@tonic-gate 				break;
22017c478bd9Sstevel@tonic-gate 			case DT_SUNW_AUXILIARY:
22027c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
22037c478bd9Sstevel@tonic-gate 					SYMAFLTRCNT(lmp)++;
22047c478bd9Sstevel@tonic-gate 				}
22057c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMAFLTR;
22067c478bd9Sstevel@tonic-gate 				break;
22077c478bd9Sstevel@tonic-gate 			case DT_DEPAUDIT:
22087c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUDIT))
22097c478bd9Sstevel@tonic-gate 					audit = ld->d_un.d_val;
22107c478bd9Sstevel@tonic-gate 				break;
22117c478bd9Sstevel@tonic-gate 			case DT_CONFIG:
22127c478bd9Sstevel@tonic-gate 				cfile = ld->d_un.d_val;
22137c478bd9Sstevel@tonic-gate 				break;
22147c478bd9Sstevel@tonic-gate 			case DT_DEBUG:
22157c478bd9Sstevel@tonic-gate 				/*
22167c478bd9Sstevel@tonic-gate 				 * DT_DEBUG entries are only created in
22177c478bd9Sstevel@tonic-gate 				 * dynamic objects that require an interpretor
22187c478bd9Sstevel@tonic-gate 				 * (ie. all dynamic executables and some shared
22197c478bd9Sstevel@tonic-gate 				 * objects), and provide for a hand-shake with
22207c478bd9Sstevel@tonic-gate 				 * debuggers.  This entry is initialized to
22217c478bd9Sstevel@tonic-gate 				 * zero by the link-editor.  If a debugger has
22227c478bd9Sstevel@tonic-gate 				 * us and updated this entry set the debugger
22237c478bd9Sstevel@tonic-gate 				 * flag, and finish initializing the debugging
22247c478bd9Sstevel@tonic-gate 				 * structure (see setup() also).  Switch off any
22257c478bd9Sstevel@tonic-gate 				 * configuration object use as most debuggers
22267c478bd9Sstevel@tonic-gate 				 * can't handle fixed dynamic executables as
22277c478bd9Sstevel@tonic-gate 				 * dependencies, and we can't handle requests
22287c478bd9Sstevel@tonic-gate 				 * like object padding for alternative objects.
22297c478bd9Sstevel@tonic-gate 				 */
22307c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_ptr)
22317c478bd9Sstevel@tonic-gate 					rtld_flags |=
22327c478bd9Sstevel@tonic-gate 					    (RT_FL_DEBUGGER | RT_FL_NOOBJALT);
22337c478bd9Sstevel@tonic-gate 				ld->d_un.d_ptr = (Addr)&r_debug;
22347c478bd9Sstevel@tonic-gate 				break;
22357c478bd9Sstevel@tonic-gate 			case DT_VERNEED:
22367c478bd9Sstevel@tonic-gate 				VERNEED(lmp) = (Verneed *)(ld->d_un.d_ptr +
22377c478bd9Sstevel@tonic-gate 				    base);
22387c478bd9Sstevel@tonic-gate 				break;
22397c478bd9Sstevel@tonic-gate 			case DT_VERNEEDNUM:
22407c478bd9Sstevel@tonic-gate 				/* LINTED */
22417c478bd9Sstevel@tonic-gate 				VERNEEDNUM(lmp) = (int)ld->d_un.d_val;
22427c478bd9Sstevel@tonic-gate 				break;
22437c478bd9Sstevel@tonic-gate 			case DT_VERDEF:
22447c478bd9Sstevel@tonic-gate 				VERDEF(lmp) = (Verdef *)(ld->d_un.d_ptr + base);
22457c478bd9Sstevel@tonic-gate 				break;
22467c478bd9Sstevel@tonic-gate 			case DT_VERDEFNUM:
22477c478bd9Sstevel@tonic-gate 				/* LINTED */
22487c478bd9Sstevel@tonic-gate 				VERDEFNUM(lmp) = (int)ld->d_un.d_val;
22497c478bd9Sstevel@tonic-gate 				break;
22507c478bd9Sstevel@tonic-gate 			case DT_BIND_NOW:
2251dffec89cSrie 				if ((ld->d_un.d_val & DF_BIND_NOW) &&
2252dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22537c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22547c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22557c478bd9Sstevel@tonic-gate 				}
22567c478bd9Sstevel@tonic-gate 				break;
22577c478bd9Sstevel@tonic-gate 			case DT_FLAGS:
22587c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_SYMBOLIC)
22597c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_SYMBOLIC;
22607c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_TEXTREL)
22617c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_TEXTREL;
2262dffec89cSrie 				if ((ld->d_un.d_val & DF_BIND_NOW) &&
2263dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22647c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22657c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22667c478bd9Sstevel@tonic-gate 				}
2267d326b23bSrie 				/*
2268d326b23bSrie 				 * Capture any static TLS use, and enforce that
2269d326b23bSrie 				 * this object be non-deletable.
2270d326b23bSrie 				 */
2271d326b23bSrie 				if (ld->d_un.d_val & DF_STATIC_TLS) {
2272d326b23bSrie 					FLAGS1(lmp) |= FL1_RT_TLSSTAT;
2273d326b23bSrie 					MODE(lmp) |= RTLD_NODELETE;
2274d326b23bSrie 				}
22757c478bd9Sstevel@tonic-gate 				break;
22767c478bd9Sstevel@tonic-gate 			case DT_FLAGS_1:
22777c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DISPRELPND)
22787c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_DISPREL;
22797c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_GROUP)
22807c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |=
22817c478bd9Sstevel@tonic-gate 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
2282dffec89cSrie 				if ((ld->d_un.d_val & DF_1_NOW) &&
2283dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22847c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22857c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22867c478bd9Sstevel@tonic-gate 				}
22877c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODELETE)
22887c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NODELETE;
22897c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_INITFIRST)
22907c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INITFRST;
22917c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NOOPEN)
22927c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NOOPEN;
22937c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_LOADFLTR)
22947c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_LOADFLTR;
22957c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODUMP)
22967c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NODUMP;
22977c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_CONFALT)
22987c478bd9Sstevel@tonic-gate 					crle = 1;
22997c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DIRECT)
2300*9a411307Srie 					FLAGS1(lmp) |= FL1_RT_DIRECT;
23017c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODEFLIB)
23027c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_NODEFLIB;
23037c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ENDFILTEE)
23047c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_ENDFILTE;
23057c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_TRANS)
23067c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_TRANS;
23077c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
23087c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ORIGIN)
23097c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_RELATIVE;
23107c478bd9Sstevel@tonic-gate #endif
23117c478bd9Sstevel@tonic-gate 				/*
23127c478bd9Sstevel@tonic-gate 				 * If this object identifies itself as an
23137c478bd9Sstevel@tonic-gate 				 * interposer, but relocation processing has
23147c478bd9Sstevel@tonic-gate 				 * already started, then demote it.  It's too
23157c478bd9Sstevel@tonic-gate 				 * late to guarantee complete interposition.
23167c478bd9Sstevel@tonic-gate 				 */
2317*9a411307Srie 				if (ld->d_un.d_val &
2318*9a411307Srie 				    (DF_1_INTERPOSE | DF_1_SYMINTPOSE)) {
2319*9a411307Srie 				    if (lml->lm_flags & LML_FLG_STARTREL) {
23205aefb655Srie 					DBG_CALL(Dbg_util_intoolate(lmp));
23217c478bd9Sstevel@tonic-gate 					if (lml->lm_flags & LML_FLG_TRC_ENABLE)
23227c478bd9Sstevel@tonic-gate 					    (void) printf(
23237c478bd9Sstevel@tonic-gate 						MSG_INTL(MSG_LDD_REL_ERR2),
23247c478bd9Sstevel@tonic-gate 						NAME(lmp));
2325*9a411307Srie 				    } else if (ld->d_un.d_val & DF_1_INTERPOSE)
2326*9a411307Srie 					FLAGS(lmp) |= FLG_RT_OBJINTPO;
2327*9a411307Srie 				    else
2328*9a411307Srie 					FLAGS(lmp) |= FLG_RT_SYMINTPO;
23297c478bd9Sstevel@tonic-gate 				}
23307c478bd9Sstevel@tonic-gate 				break;
23317c478bd9Sstevel@tonic-gate 			case DT_SYMINFO:
23327c478bd9Sstevel@tonic-gate 				SYMINFO(lmp) = (Syminfo *)(ld->d_un.d_ptr +
23337c478bd9Sstevel@tonic-gate 				    base);
23347c478bd9Sstevel@tonic-gate 				break;
23357c478bd9Sstevel@tonic-gate 			case DT_SYMINENT:
23367c478bd9Sstevel@tonic-gate 				SYMINENT(lmp) = ld->d_un.d_val;
23377c478bd9Sstevel@tonic-gate 				break;
23387c478bd9Sstevel@tonic-gate 			case DT_PLTPAD:
23397c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = (void *)(ld->d_un.d_ptr + base);
23407c478bd9Sstevel@tonic-gate 				break;
23417c478bd9Sstevel@tonic-gate 			case DT_PLTPADSZ:
23427c478bd9Sstevel@tonic-gate 				pltpadsz = ld->d_un.d_val;
23437c478bd9Sstevel@tonic-gate 				break;
23447c478bd9Sstevel@tonic-gate 			case DT_SUNW_RTLDINF:
23457c478bd9Sstevel@tonic-gate 				/*
234610a4fa49Srie 				 * Maintain a list of RTLDINFO structures.
234710a4fa49Srie 				 * Typically, libc is the only supplier, and
234810a4fa49Srie 				 * only one structure is provided.  However,
234910a4fa49Srie 				 * multiple suppliers and multiple structures
235010a4fa49Srie 				 * are supported.  For example, one structure
235110a4fa49Srie 				 * may provide thread_init, and another
235210a4fa49Srie 				 * structure may provide atexit reservations.
23537c478bd9Sstevel@tonic-gate 				 */
235410a4fa49Srie 				if ((rti = alist_append(&lml->lm_rti, 0,
235510a4fa49Srie 				    sizeof (Rti_desc), AL_CNT_RTLDINFO)) == 0) {
23567c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
23577c478bd9Sstevel@tonic-gate 					return (0);
23587c478bd9Sstevel@tonic-gate 				}
235910a4fa49Srie 				rti->rti_lmp = lmp;
236010a4fa49Srie 				rti->rti_info = (void *)(ld->d_un.d_ptr + base);
23617c478bd9Sstevel@tonic-gate 				break;
23627c478bd9Sstevel@tonic-gate 			case DT_DEPRECATED_SPARC_REGISTER:
23637c478bd9Sstevel@tonic-gate 			case M_DT_REGISTER:
23647c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_REGSYMS;
23657c478bd9Sstevel@tonic-gate 				break;
23667c478bd9Sstevel@tonic-gate 			case M_DT_PLTRESERVE:
23677c478bd9Sstevel@tonic-gate 				PLTRESERVE(lmp) = (void *)(ld->d_un.d_ptr +
23687c478bd9Sstevel@tonic-gate 				    base);
23697c478bd9Sstevel@tonic-gate 				break;
23707c478bd9Sstevel@tonic-gate 			}
23717c478bd9Sstevel@tonic-gate 		}
23727c478bd9Sstevel@tonic-gate 
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 		if (PLTPAD(lmp)) {
23757c478bd9Sstevel@tonic-gate 			if (pltpadsz == (Xword)0)
23767c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = 0;
23777c478bd9Sstevel@tonic-gate 			else
23787c478bd9Sstevel@tonic-gate 				PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) +
23797c478bd9Sstevel@tonic-gate 				    pltpadsz);
23807c478bd9Sstevel@tonic-gate 		}
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 		/*
23837c478bd9Sstevel@tonic-gate 		 * Allocate Dynamic Info structure
23847c478bd9Sstevel@tonic-gate 		 */
23857c478bd9Sstevel@tonic-gate 		if ((DYNINFO(lmp) = calloc((size_t)dyncnt,
23867c478bd9Sstevel@tonic-gate 		    sizeof (Dyninfo))) == 0) {
23877c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
23887c478bd9Sstevel@tonic-gate 			return (0);
23897c478bd9Sstevel@tonic-gate 		}
23907c478bd9Sstevel@tonic-gate 		DYNINFOCNT(lmp) = dyncnt;
23917c478bd9Sstevel@tonic-gate 	}
23927c478bd9Sstevel@tonic-gate 
23939039eeafSab 	/*
23949039eeafSab 	 * A dynsym contains only global functions. We want to have
23959039eeafSab 	 * a version of it that also includes local functions, so that
23969039eeafSab 	 * dladdr() will be able to report names for local functions
23979039eeafSab 	 * when used to generate a stack trace for a stripped file.
23989039eeafSab 	 * This version of the dynsym is provided via DT_SUNW_SYMTAB.
23999039eeafSab 	 *
24009039eeafSab 	 * In producing DT_SUNW_SYMTAB, ld uses a non-obvious trick
24019039eeafSab 	 * in order to avoid having to have two copies of the global
24029039eeafSab 	 * symbols held in DT_SYMTAB: The local symbols are placed in
24039039eeafSab 	 * a separate section than the globals in the dynsym, but the
24049039eeafSab 	 * linker conspires to put the data for these two sections adjacent
24059039eeafSab 	 * to each other. DT_SUNW_SYMTAB points at the top of the local
24069039eeafSab 	 * symbols, and DT_SUNW_SYMSZ is the combined length of both tables.
24079039eeafSab 	 *
24089039eeafSab 	 * If the two sections are not adjacent, then something went wrong
24099039eeafSab 	 * at link time. We use ASSERT to kill the process if this is
24109039eeafSab 	 * a debug build. In a production build, we will silently ignore
24119039eeafSab 	 * the presence of the .ldynsym and proceed. We can detect this
24129039eeafSab 	 * situation by checking to see that DT_SYMTAB lies in
24139039eeafSab 	 * the range given by DT_SUNW_SYMTAB/DT_SUNW_SYMSZ.
24149039eeafSab 	 */
24159039eeafSab 	if ((SUNWSYMTAB(lmp) != NULL) &&
24169039eeafSab 	    (((char *)SYMTAB(lmp) <= (char *)SUNWSYMTAB(lmp)) ||
24179039eeafSab 	    (((char *)SYMTAB(lmp) >=
24189039eeafSab 	    (SUNWSYMSZ(lmp) + (char *)SUNWSYMTAB(lmp)))))) {
24199039eeafSab 		ASSERT(0);
24209039eeafSab 		SUNWSYMTAB(lmp) = NULL;
24219039eeafSab 		SUNWSYMSZ(lmp) = 0;
24229039eeafSab 	}
24239039eeafSab 
24247c478bd9Sstevel@tonic-gate 	/*
24257c478bd9Sstevel@tonic-gate 	 * If configuration file use hasn't been disabled, and a configuration
24267c478bd9Sstevel@tonic-gate 	 * file hasn't already been set via an environment variable, see if any
24277c478bd9Sstevel@tonic-gate 	 * application specific configuration file is specified.  An LD_CONFIG
24287c478bd9Sstevel@tonic-gate 	 * setting is used first, but if this image was generated via crle(1)
24297c478bd9Sstevel@tonic-gate 	 * then a default configuration file is a fall-back.
24307c478bd9Sstevel@tonic-gate 	 */
24317c478bd9Sstevel@tonic-gate 	if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == 0)) {
24327c478bd9Sstevel@tonic-gate 		if (cfile)
24337c478bd9Sstevel@tonic-gate 			config->c_name = (const char *)(cfile +
24347c478bd9Sstevel@tonic-gate 			    (char *)STRTAB(lmp));
24357c478bd9Sstevel@tonic-gate 		else if (crle) {
24367c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_CONFAPP;
24377c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
24387c478bd9Sstevel@tonic-gate 			FLAGS1(lmp) |= FL1_RT_RELATIVE;
24397c478bd9Sstevel@tonic-gate #endif
24407c478bd9Sstevel@tonic-gate 		}
24417c478bd9Sstevel@tonic-gate 	}
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	if (rpath)
24447c478bd9Sstevel@tonic-gate 		RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp));
24457c478bd9Sstevel@tonic-gate 	if (fltr) {
24467c478bd9Sstevel@tonic-gate 		/*
24477c478bd9Sstevel@tonic-gate 		 * If this object is a global filter, duplicate the filtee
24487c478bd9Sstevel@tonic-gate 		 * string name(s) so that REFNAME() is available in core files.
24497c478bd9Sstevel@tonic-gate 		 * This cludge was useful for debuggers at one point, but only
24507c478bd9Sstevel@tonic-gate 		 * when the filtee name was an individual full path.
24517c478bd9Sstevel@tonic-gate 		 */
24527c478bd9Sstevel@tonic-gate 		if ((REFNAME(lmp) = strdup(fltr + (char *)STRTAB(lmp))) == 0) {
24537c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
24547c478bd9Sstevel@tonic-gate 			return (0);
24557c478bd9Sstevel@tonic-gate 		}
24567c478bd9Sstevel@tonic-gate 	}
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_RELATIVE)
24597c478bd9Sstevel@tonic-gate 		FLAGS1(lmp) |= FL1_RT_RELATIVE;
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 	/*
24627c478bd9Sstevel@tonic-gate 	 * For Intel ABI compatibility.  It's possible that a JMPREL can be
24637c478bd9Sstevel@tonic-gate 	 * specified without any other relocations (e.g. a dynamic executable
24647c478bd9Sstevel@tonic-gate 	 * normally only contains .plt relocations).  If this is the case then
24657c478bd9Sstevel@tonic-gate 	 * no REL, RELSZ or RELENT will have been created.  For us to be able
24667c478bd9Sstevel@tonic-gate 	 * to traverse the .plt relocations under LD_BIND_NOW we need to know
24677c478bd9Sstevel@tonic-gate 	 * the RELENT for these relocations.  Refer to elf_reloc() for more
24687c478bd9Sstevel@tonic-gate 	 * details.
24697c478bd9Sstevel@tonic-gate 	 */
24707c478bd9Sstevel@tonic-gate 	if (!RELENT(lmp) && JMPREL(lmp))
24717c478bd9Sstevel@tonic-gate 		RELENT(lmp) = sizeof (Rel);
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	/*
24747c478bd9Sstevel@tonic-gate 	 * Establish any per-object auditing.  If we're establishing `main's
24757c478bd9Sstevel@tonic-gate 	 * link-map its too early to go searching for audit objects so just
24767c478bd9Sstevel@tonic-gate 	 * hold the object name for later (see setup()).
24777c478bd9Sstevel@tonic-gate 	 */
24787c478bd9Sstevel@tonic-gate 	if (audit) {
24797c478bd9Sstevel@tonic-gate 		char	*cp = audit + (char *)STRTAB(lmp);
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 		if (*cp) {
24827c478bd9Sstevel@tonic-gate 			if (((AUDITORS(lmp) =
24837c478bd9Sstevel@tonic-gate 			    calloc(1, sizeof (Audit_desc))) == 0) ||
24847c478bd9Sstevel@tonic-gate 			    ((AUDITORS(lmp)->ad_name = strdup(cp)) == 0)) {
24857c478bd9Sstevel@tonic-gate 				remove_so(0, lmp);
24867c478bd9Sstevel@tonic-gate 				return (0);
24877c478bd9Sstevel@tonic-gate 			}
248841072f3cSrie 			if (lml_main.lm_head) {
248956d7adc6Srie 				if (audit_setup(lmp, AUDITORS(lmp), 0) == 0) {
24907c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
24917c478bd9Sstevel@tonic-gate 					return (0);
24927c478bd9Sstevel@tonic-gate 				}
24937c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= AUDITORS(lmp)->ad_flags;
24947c478bd9Sstevel@tonic-gate 				lml->lm_flags |= LML_FLG_LOCAUDIT;
24957c478bd9Sstevel@tonic-gate 			}
24967c478bd9Sstevel@tonic-gate 		}
24977c478bd9Sstevel@tonic-gate 	}
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 	if ((CONDVAR(lmp) = rt_cond_create()) == 0) {
25007c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
25017c478bd9Sstevel@tonic-gate 		return (0);
25027c478bd9Sstevel@tonic-gate 	}
25037c478bd9Sstevel@tonic-gate 	if (oname && ((append_alias(lmp, oname, 0)) == 0)) {
25047c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
25057c478bd9Sstevel@tonic-gate 		return (0);
25067c478bd9Sstevel@tonic-gate 	}
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	/*
25097c478bd9Sstevel@tonic-gate 	 * Add the mapped object to the end of the link map list.
25107c478bd9Sstevel@tonic-gate 	 */
25117c478bd9Sstevel@tonic-gate 	lm_append(lml, lmco, lmp);
25127c478bd9Sstevel@tonic-gate 	return (lmp);
25137c478bd9Sstevel@tonic-gate }
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate /*
25167c478bd9Sstevel@tonic-gate  * Assign hardware/software capabilities.
25177c478bd9Sstevel@tonic-gate  */
25187c478bd9Sstevel@tonic-gate void
25197c478bd9Sstevel@tonic-gate cap_assign(Cap *cap, Rt_map *lmp)
25207c478bd9Sstevel@tonic-gate {
25217c478bd9Sstevel@tonic-gate 	while (cap->c_tag != CA_SUNW_NULL) {
25227c478bd9Sstevel@tonic-gate 		switch (cap->c_tag) {
25237c478bd9Sstevel@tonic-gate 		case CA_SUNW_HW_1:
25247c478bd9Sstevel@tonic-gate 			HWCAP(lmp) = cap->c_un.c_val;
25257c478bd9Sstevel@tonic-gate 			break;
25267c478bd9Sstevel@tonic-gate 		case CA_SUNW_SF_1:
25277c478bd9Sstevel@tonic-gate 			SFCAP(lmp) = cap->c_un.c_val;
25287c478bd9Sstevel@tonic-gate 		}
25297c478bd9Sstevel@tonic-gate 		cap++;
25307c478bd9Sstevel@tonic-gate 	}
25317c478bd9Sstevel@tonic-gate }
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate /*
25347c478bd9Sstevel@tonic-gate  * Map in an ELF object.
25357c478bd9Sstevel@tonic-gate  * Takes an open file descriptor for the object to map and its pathname; returns
25367c478bd9Sstevel@tonic-gate  * a pointer to a Rt_map structure for this object, or 0 on error.
25377c478bd9Sstevel@tonic-gate  */
25387c478bd9Sstevel@tonic-gate static Rt_map *
25397c478bd9Sstevel@tonic-gate elf_map_so(Lm_list *lml, Aliste lmco, const char *pname, const char *oname,
25407c478bd9Sstevel@tonic-gate     int fd)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	int		i; 		/* general temporary */
25437c478bd9Sstevel@tonic-gate 	Off		memsize = 0;	/* total memory size of pathname */
25447c478bd9Sstevel@tonic-gate 	Off		mentry;		/* entry point */
25457c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr;		/* ELF header of ld.so */
25467c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* first Phdr in file */
25477c478bd9Sstevel@tonic-gate 	Phdr		*phdr0;		/* Saved first Phdr in file */
25487c478bd9Sstevel@tonic-gate 	Phdr		*pptr;		/* working Phdr */
25497c478bd9Sstevel@tonic-gate 	Phdr		*fph = 0;	/* first loadable Phdr */
25507c478bd9Sstevel@tonic-gate 	Phdr		*lph;		/* last loadable Phdr */
25517c478bd9Sstevel@tonic-gate 	Phdr		*lfph = 0;	/* last loadable (filesz != 0) Phdr */
25527c478bd9Sstevel@tonic-gate 	Phdr		*lmph = 0;	/* last loadable (memsz != 0) Phdr */
25537c478bd9Sstevel@tonic-gate 	Phdr		*swph = 0;	/* program header for SUNWBSS */
25547c478bd9Sstevel@tonic-gate 	Phdr		*tlph = 0;	/* program header for PT_TLS */
25557c478bd9Sstevel@tonic-gate 	Phdr		*unwindph = 0;	/* program header for PT_SUNW_UNWIND */
25567c478bd9Sstevel@tonic-gate 	Cap		*cap = 0;	/* program header for SUNWCAP */
25577c478bd9Sstevel@tonic-gate 	Dyn		*mld = 0;	/* DYNAMIC structure for pathname */
25587c478bd9Sstevel@tonic-gate 	size_t		size;		/* size of elf and program headers */
25597c478bd9Sstevel@tonic-gate 	caddr_t		faddr = 0;	/* mapping address of pathname */
25607c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;		/* link map created */
25617c478bd9Sstevel@tonic-gate 	caddr_t		paddr;		/* start of padded image */
25627c478bd9Sstevel@tonic-gate 	Off		plen;		/* size of image including padding */
25637c478bd9Sstevel@tonic-gate 	Half		etype;
25647c478bd9Sstevel@tonic-gate 	int		fixed;
25657c478bd9Sstevel@tonic-gate 	Mmap		*mmaps;
25667c478bd9Sstevel@tonic-gate 	uint_t		mmapcnt = 0;
25677c478bd9Sstevel@tonic-gate 	Xword		align = 0;
25687c478bd9Sstevel@tonic-gate 
25697c478bd9Sstevel@tonic-gate 	/* LINTED */
25707c478bd9Sstevel@tonic-gate 	ehdr = (Ehdr *)fmap->fm_maddr;
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 	/*
25737c478bd9Sstevel@tonic-gate 	 * If this a relocatable object then special processing is required.
25747c478bd9Sstevel@tonic-gate 	 */
25757c478bd9Sstevel@tonic-gate 	if ((etype = ehdr->e_type) == ET_REL)
257641072f3cSrie 		return (elf_obj_file(lml, lmco, pname, fd));
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 	/*
25797c478bd9Sstevel@tonic-gate 	 * If this isn't a dynamic executable or shared object we can't process
25807c478bd9Sstevel@tonic-gate 	 * it.  If this is a dynamic executable then all addresses are fixed.
25817c478bd9Sstevel@tonic-gate 	 */
25827c478bd9Sstevel@tonic-gate 	if (etype == ET_EXEC)
25837c478bd9Sstevel@tonic-gate 		fixed = 1;
25847c478bd9Sstevel@tonic-gate 	else if (etype == ET_DYN)
25857c478bd9Sstevel@tonic-gate 		fixed = 0;
25867c478bd9Sstevel@tonic-gate 	else {
25875aefb655Srie 		eprintf(lml, ERR_ELF, MSG_INTL(MSG_GEN_BADTYPE), pname,
2588c13de8f6Sab 		    conv_ehdr_type(etype, 0));
25897c478bd9Sstevel@tonic-gate 		return (0);
25907c478bd9Sstevel@tonic-gate 	}
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 	/*
25937c478bd9Sstevel@tonic-gate 	 * If our original mapped page was not large enough to hold all the
25947c478bd9Sstevel@tonic-gate 	 * program headers remap them.
25957c478bd9Sstevel@tonic-gate 	 */
25967c478bd9Sstevel@tonic-gate 	size = (size_t)((char *)ehdr->e_phoff +
25977c478bd9Sstevel@tonic-gate 		(ehdr->e_phnum * ehdr->e_phentsize));
25987c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_fsize) {
25995aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
26007c478bd9Sstevel@tonic-gate 		return (0);
26017c478bd9Sstevel@tonic-gate 	}
26027c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_msize) {
26037c478bd9Sstevel@tonic-gate 		fmap_setup();
26047c478bd9Sstevel@tonic-gate 		if ((fmap->fm_maddr = mmap(fmap->fm_maddr, size, PROT_READ,
26057c478bd9Sstevel@tonic-gate 		    fmap->fm_mflags, fd, 0)) == MAP_FAILED) {
26067c478bd9Sstevel@tonic-gate 			int	err = errno;
26075aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), pname,
26087c478bd9Sstevel@tonic-gate 			    strerror(err));
26097c478bd9Sstevel@tonic-gate 			return (0);
26107c478bd9Sstevel@tonic-gate 		}
26117c478bd9Sstevel@tonic-gate 		fmap->fm_msize = size;
26127c478bd9Sstevel@tonic-gate 		/* LINTED */
26137c478bd9Sstevel@tonic-gate 		ehdr = (Ehdr *)fmap->fm_maddr;
26147c478bd9Sstevel@tonic-gate 	}
26157c478bd9Sstevel@tonic-gate 	/* LINTED */
26167c478bd9Sstevel@tonic-gate 	phdr0 = phdr = (Phdr *)((char *)ehdr + ehdr->e_ehsize);
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	/*
26197c478bd9Sstevel@tonic-gate 	 * Get entry point.
26207c478bd9Sstevel@tonic-gate 	 */
26217c478bd9Sstevel@tonic-gate 	mentry = ehdr->e_entry;
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	/*
26247c478bd9Sstevel@tonic-gate 	 * Point at program headers and perform some basic validation.
26257c478bd9Sstevel@tonic-gate 	 */
26267c478bd9Sstevel@tonic-gate 	for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
26277c478bd9Sstevel@tonic-gate 	    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
26287c478bd9Sstevel@tonic-gate 		if ((pptr->p_type == PT_LOAD) ||
26297c478bd9Sstevel@tonic-gate 		    (pptr->p_type == PT_SUNWBSS)) {
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 			if (fph == 0) {
26327c478bd9Sstevel@tonic-gate 				fph = pptr;
26337c478bd9Sstevel@tonic-gate 			/* LINTED argument lph is initialized in first pass */
26347c478bd9Sstevel@tonic-gate 			} else if (pptr->p_vaddr <= lph->p_vaddr) {
26355aefb655Srie 				eprintf(lml, ERR_ELF,
26365aefb655Srie 				    MSG_INTL(MSG_GEN_INVPRGHDR), pname);
26377c478bd9Sstevel@tonic-gate 				return (0);
26387c478bd9Sstevel@tonic-gate 			}
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 			lph = pptr;
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 			if (pptr->p_memsz)
26437c478bd9Sstevel@tonic-gate 				lmph = pptr;
26447c478bd9Sstevel@tonic-gate 			if (pptr->p_filesz)
26457c478bd9Sstevel@tonic-gate 				lfph = pptr;
26467c478bd9Sstevel@tonic-gate 			if (pptr->p_type == PT_SUNWBSS)
26477c478bd9Sstevel@tonic-gate 				swph = pptr;
26487c478bd9Sstevel@tonic-gate 			if (pptr->p_align > align)
26497c478bd9Sstevel@tonic-gate 				align = pptr->p_align;
26507c478bd9Sstevel@tonic-gate 
265110a4fa49Srie 		} else if (pptr->p_type == PT_DYNAMIC) {
26527c478bd9Sstevel@tonic-gate 			mld = (Dyn *)(pptr->p_vaddr);
265310a4fa49Srie 		} else if ((pptr->p_type == PT_TLS) && pptr->p_memsz) {
26547c478bd9Sstevel@tonic-gate 			tlph = pptr;
265510a4fa49Srie 		} else if (pptr->p_type == PT_SUNWCAP) {
26567c478bd9Sstevel@tonic-gate 			cap = (Cap *)(pptr->p_vaddr);
265710a4fa49Srie 		} else if (pptr->p_type == PT_SUNW_UNWIND) {
26587c478bd9Sstevel@tonic-gate 			unwindph = pptr;
265910a4fa49Srie 		}
26607c478bd9Sstevel@tonic-gate 	}
26617c478bd9Sstevel@tonic-gate 
26627c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
26637c478bd9Sstevel@tonic-gate 	/*
26647c478bd9Sstevel@tonic-gate 	 * Make sure the maximum page alignment is a power of 2 >= the system
26657c478bd9Sstevel@tonic-gate 	 * page size, for use with MAP_ALIGN.
26667c478bd9Sstevel@tonic-gate 	 */
26677c478bd9Sstevel@tonic-gate 	align = M_PROUND(align);
26687c478bd9Sstevel@tonic-gate #endif
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	/*
26717c478bd9Sstevel@tonic-gate 	 * We'd better have at least one loadable segment, together with some
26727c478bd9Sstevel@tonic-gate 	 * specified file and memory size.
26737c478bd9Sstevel@tonic-gate 	 */
26747c478bd9Sstevel@tonic-gate 	if ((fph == 0) || (lmph == 0) || (lfph == 0)) {
26755aefb655Srie 		eprintf(lml, ERR_ELF, MSG_INTL(MSG_GEN_NOLOADSEG), pname);
26767c478bd9Sstevel@tonic-gate 		return (0);
26777c478bd9Sstevel@tonic-gate 	}
26787c478bd9Sstevel@tonic-gate 
26797c478bd9Sstevel@tonic-gate 	/*
26807c478bd9Sstevel@tonic-gate 	 * Check that the files size accounts for the loadable sections
26817c478bd9Sstevel@tonic-gate 	 * we're going to map in (failure to do this may cause spurious
26827c478bd9Sstevel@tonic-gate 	 * bus errors if we're given a truncated file).
26837c478bd9Sstevel@tonic-gate 	 */
26847c478bd9Sstevel@tonic-gate 	if (fmap->fm_fsize < ((size_t)lfph->p_offset + lfph->p_filesz)) {
26855aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
26867c478bd9Sstevel@tonic-gate 		return (0);
26877c478bd9Sstevel@tonic-gate 	}
26887c478bd9Sstevel@tonic-gate 
26897c478bd9Sstevel@tonic-gate 	/*
26907c478bd9Sstevel@tonic-gate 	 * Memsize must be page rounded so that if we add object padding
26917c478bd9Sstevel@tonic-gate 	 * at the end it will start at the beginning of a page.
26927c478bd9Sstevel@tonic-gate 	 */
26937c478bd9Sstevel@tonic-gate 	plen = memsize = M_PROUND((lmph->p_vaddr + lmph->p_memsz) -
26947c478bd9Sstevel@tonic-gate 	    M_PTRUNC((ulong_t)fph->p_vaddr));
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 	/*
26977c478bd9Sstevel@tonic-gate 	 * Determine if an existing mapping is acceptable.
26987c478bd9Sstevel@tonic-gate 	 */
26997c478bd9Sstevel@tonic-gate 	if (interp && (lml->lm_flags & LML_FLG_BASELM) &&
270041072f3cSrie 	    (strcmp(pname, interp->i_name) == 0)) {
27017c478bd9Sstevel@tonic-gate 		/*
27027c478bd9Sstevel@tonic-gate 		 * If this is the interpreter then it has already been mapped
27037c478bd9Sstevel@tonic-gate 		 * and we have the address so don't map it again.  Note that
27047c478bd9Sstevel@tonic-gate 		 * the common occurrence of a reference to the interpretor
27057c478bd9Sstevel@tonic-gate 		 * (libdl -> ld.so.1) will have been caught during filter
27067c478bd9Sstevel@tonic-gate 		 * initialization (see elf_lookup_filtee()).  However, some
27077c478bd9Sstevel@tonic-gate 		 * ELF implementations are known to record libc.so.1 as the
27087c478bd9Sstevel@tonic-gate 		 * interpretor, and thus this test catches this behavior.
27097c478bd9Sstevel@tonic-gate 		 */
27107c478bd9Sstevel@tonic-gate 		paddr = faddr = interp->i_faddr;
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	} else if ((fixed == 0) && (r_debug.rtd_objpad == 0) &&
27137c478bd9Sstevel@tonic-gate 	    (memsize <= fmap->fm_msize) && ((fph->p_flags & PF_W) == 0) &&
27147c478bd9Sstevel@tonic-gate 	    (fph->p_filesz == fph->p_memsz) &&
27157c478bd9Sstevel@tonic-gate 	    (((Xword)fmap->fm_maddr % align) == 0)) {
27167c478bd9Sstevel@tonic-gate 		/*
27177c478bd9Sstevel@tonic-gate 		 * If the mapping required has already been established from
27187c478bd9Sstevel@tonic-gate 		 * the initial page we don't need to do anything more.  Reset
27197c478bd9Sstevel@tonic-gate 		 * the fmap address so then any later files start a new fmap.
27207c478bd9Sstevel@tonic-gate 		 * This is really an optimization for filters, such as libdl.so,
27217c478bd9Sstevel@tonic-gate 		 * which should only require one page.
27227c478bd9Sstevel@tonic-gate 		 */
27237c478bd9Sstevel@tonic-gate 		paddr = faddr = fmap->fm_maddr;
27247c478bd9Sstevel@tonic-gate 		fmap->fm_maddr = 0;
27257c478bd9Sstevel@tonic-gate 		fmap_setup();
27267c478bd9Sstevel@tonic-gate 	}
27277c478bd9Sstevel@tonic-gate 
27287c478bd9Sstevel@tonic-gate 	/*
27297c478bd9Sstevel@tonic-gate 	 * Allocate a mapping array to retain mapped segment information.
27307c478bd9Sstevel@tonic-gate 	 */
27317c478bd9Sstevel@tonic-gate 	if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
27327c478bd9Sstevel@tonic-gate 		return (0);
27337c478bd9Sstevel@tonic-gate 
27347c478bd9Sstevel@tonic-gate 	/*
27357c478bd9Sstevel@tonic-gate 	 * If we're reusing an existing mapping determine the objects etext
27367c478bd9Sstevel@tonic-gate 	 * address.  Otherwise map the file (which will calculate the etext
27377c478bd9Sstevel@tonic-gate 	 * address as part of the mapping process).
27387c478bd9Sstevel@tonic-gate 	 */
27397c478bd9Sstevel@tonic-gate 	if (faddr) {
27407c478bd9Sstevel@tonic-gate 		caddr_t	base;
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate 		if (fixed)
27437c478bd9Sstevel@tonic-gate 			base = 0;
27447c478bd9Sstevel@tonic-gate 		else
27457c478bd9Sstevel@tonic-gate 			base = faddr;
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 		/* LINTED */
27487c478bd9Sstevel@tonic-gate 		phdr0 = phdr = (Phdr *)((char *)faddr + ehdr->e_ehsize);
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 		for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
27517c478bd9Sstevel@tonic-gate 		    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
27527c478bd9Sstevel@tonic-gate 			if (pptr->p_type != PT_LOAD)
27537c478bd9Sstevel@tonic-gate 				continue;
27547c478bd9Sstevel@tonic-gate 
27557c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_vaddr = (pptr->p_vaddr + base);
27567c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_msize = pptr->p_memsz;
27577c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_fsize = pptr->p_filesz;
27587c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_perm = (PROT_READ | PROT_EXEC);
27597c478bd9Sstevel@tonic-gate 			mmapcnt++;
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate 			if (!(pptr->p_flags & PF_W)) {
27627c478bd9Sstevel@tonic-gate 				fmap->fm_etext = (ulong_t)pptr->p_vaddr +
27637c478bd9Sstevel@tonic-gate 				    (ulong_t)pptr->p_memsz +
27647c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
27657c478bd9Sstevel@tonic-gate 			}
27667c478bd9Sstevel@tonic-gate 		}
27677c478bd9Sstevel@tonic-gate 	} else {
27687c478bd9Sstevel@tonic-gate 		/*
27697c478bd9Sstevel@tonic-gate 		 * Map the file.
27707c478bd9Sstevel@tonic-gate 		 */
27715aefb655Srie 		if (!(faddr = elf_map_it(lml, pname, memsize, ehdr, fph, lph,
27727c478bd9Sstevel@tonic-gate 		    &phdr, &paddr, &plen, fixed, fd, align, mmaps, &mmapcnt)))
27737c478bd9Sstevel@tonic-gate 			return (0);
27747c478bd9Sstevel@tonic-gate 	}
27757c478bd9Sstevel@tonic-gate 
27767c478bd9Sstevel@tonic-gate 	/*
27777c478bd9Sstevel@tonic-gate 	 * Calculate absolute base addresses and entry points.
27787c478bd9Sstevel@tonic-gate 	 */
27797c478bd9Sstevel@tonic-gate 	if (!fixed) {
27807c478bd9Sstevel@tonic-gate 		if (mld)
27817c478bd9Sstevel@tonic-gate 			/* LINTED */
27827c478bd9Sstevel@tonic-gate 			mld = (Dyn *)((Off)mld + faddr);
27837c478bd9Sstevel@tonic-gate 		if (cap)
27847c478bd9Sstevel@tonic-gate 			/* LINTED */
27857c478bd9Sstevel@tonic-gate 			cap = (Cap *)((Off)cap + faddr);
27867c478bd9Sstevel@tonic-gate 		mentry += (Off)faddr;
27877c478bd9Sstevel@tonic-gate 	}
27887c478bd9Sstevel@tonic-gate 
27897c478bd9Sstevel@tonic-gate 	/*
27907c478bd9Sstevel@tonic-gate 	 * Create new link map structure for newly mapped shared object.
27917c478bd9Sstevel@tonic-gate 	 */
27927c478bd9Sstevel@tonic-gate 	if (!(lmp = elf_new_lm(lml, pname, oname, mld, (ulong_t)faddr,
27937c478bd9Sstevel@tonic-gate 	    fmap->fm_etext, lmco, memsize, mentry, (ulong_t)paddr, plen, mmaps,
27947c478bd9Sstevel@tonic-gate 	    mmapcnt))) {
27957c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)faddr, memsize);
27967c478bd9Sstevel@tonic-gate 		return (0);
27977c478bd9Sstevel@tonic-gate 	}
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	/*
28007c478bd9Sstevel@tonic-gate 	 * Start the system loading in the ELF information we'll be processing.
28017c478bd9Sstevel@tonic-gate 	 */
28027c478bd9Sstevel@tonic-gate 	if (REL(lmp)) {
28037c478bd9Sstevel@tonic-gate 		(void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) +
28047c478bd9Sstevel@tonic-gate 		    (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp),
28057c478bd9Sstevel@tonic-gate 		    MADV_WILLNEED);
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	/*
2809d326b23bSrie 	 * If this shared object contains any special segments, record them.
28107c478bd9Sstevel@tonic-gate 	 */
28117c478bd9Sstevel@tonic-gate 	if (swph) {
28127c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_SUNWBSS;
28137c478bd9Sstevel@tonic-gate 		SUNWBSS(lmp) = phdr + (swph - phdr0);
28147c478bd9Sstevel@tonic-gate 	}
2815d326b23bSrie 	if (tlph && (tls_assign(lml, lmp, (phdr + (tlph - phdr0))) == 0)) {
2816d326b23bSrie 		remove_so(lml, lmp);
2817d326b23bSrie 		return (0);
28187c478bd9Sstevel@tonic-gate 	}
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	if (unwindph)
28217c478bd9Sstevel@tonic-gate 		PTUNWIND(lmp) = phdr + (unwindph - phdr0);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	if (cap)
28247c478bd9Sstevel@tonic-gate 		cap_assign(cap, lmp);
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	return (lmp);
28277c478bd9Sstevel@tonic-gate }
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate /*
28307c478bd9Sstevel@tonic-gate  * Function to correct protection settings.  Segments are all mapped initially
28317c478bd9Sstevel@tonic-gate  * with permissions as given in the segment header.  We need to turn on write
28327c478bd9Sstevel@tonic-gate  * permissions on a text segment if there are any relocations against that
28337c478bd9Sstevel@tonic-gate  * segment, and them turn write permission back off again before returning
28347c478bd9Sstevel@tonic-gate  * control to the user.  This function turns the permission on or off depending
28357c478bd9Sstevel@tonic-gate  * on the value of the argument.
28367c478bd9Sstevel@tonic-gate  */
28377c478bd9Sstevel@tonic-gate int
28385aefb655Srie elf_set_prot(Rt_map *lmp, int permission)
28397c478bd9Sstevel@tonic-gate {
28407c478bd9Sstevel@tonic-gate 	Mmap	*mmaps;
28417c478bd9Sstevel@tonic-gate 
28427c478bd9Sstevel@tonic-gate 	/*
28437c478bd9Sstevel@tonic-gate 	 * If this is an allocated image (ie. a relocatable object) we can't
28447c478bd9Sstevel@tonic-gate 	 * mprotect() anything.
28457c478bd9Sstevel@tonic-gate 	 */
28467c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
28477c478bd9Sstevel@tonic-gate 		return (1);
28487c478bd9Sstevel@tonic-gate 
28495aefb655Srie 	DBG_CALL(Dbg_file_prot(lmp, permission));
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++) {
28527c478bd9Sstevel@tonic-gate 		if (mmaps->m_perm & PROT_WRITE)
28537c478bd9Sstevel@tonic-gate 			continue;
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 		if (mprotect(mmaps->m_vaddr, mmaps->m_msize,
28567c478bd9Sstevel@tonic-gate 		    (mmaps->m_perm | permission)) == -1) {
28577c478bd9Sstevel@tonic-gate 			int	err = errno;
28585aefb655Srie 			eprintf(LIST(lmp), ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
28597c478bd9Sstevel@tonic-gate 			    NAME(lmp), strerror(err));
28607c478bd9Sstevel@tonic-gate 			return (0);
28617c478bd9Sstevel@tonic-gate 		}
28627c478bd9Sstevel@tonic-gate 	}
28637c478bd9Sstevel@tonic-gate 	return (1);
28647c478bd9Sstevel@tonic-gate }
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate /*
28677c478bd9Sstevel@tonic-gate  * Build full pathname of shared object from given directory name and filename.
28687c478bd9Sstevel@tonic-gate  */
28697c478bd9Sstevel@tonic-gate static char *
28707c478bd9Sstevel@tonic-gate elf_get_so(const char *dir, const char *file)
28717c478bd9Sstevel@tonic-gate {
28727c478bd9Sstevel@tonic-gate 	static char	pname[PATH_MAX];
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, MSG_ORIG(MSG_FMT_PATH), dir, file);
28757c478bd9Sstevel@tonic-gate 	return (pname);
28767c478bd9Sstevel@tonic-gate }
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate /*
28797c478bd9Sstevel@tonic-gate  * The copy relocation is recorded in a copy structure which will be applied
28807c478bd9Sstevel@tonic-gate  * after all other relocations are carried out.  This provides for copying data
28817c478bd9Sstevel@tonic-gate  * that must be relocated itself (ie. pointers in shared objects).  This
28827c478bd9Sstevel@tonic-gate  * structure also provides a means of binding RTLD_GROUP dependencies to any
28837c478bd9Sstevel@tonic-gate  * copy relocations that have been taken from any group members.
28847c478bd9Sstevel@tonic-gate  *
28857c478bd9Sstevel@tonic-gate  * If the size of the .bss area available for the copy information is not the
28867c478bd9Sstevel@tonic-gate  * same as the source of the data inform the user if we're under ldd(1) control
28877c478bd9Sstevel@tonic-gate  * (this checking was only established in 5.3, so by only issuing an error via
28887c478bd9Sstevel@tonic-gate  * ldd(1) we maintain the standard set by previous releases).
28897c478bd9Sstevel@tonic-gate  */
28907c478bd9Sstevel@tonic-gate int
28917c478bd9Sstevel@tonic-gate elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym,
28927c478bd9Sstevel@tonic-gate     Rt_map *dlmp, const void *dadd)
28937c478bd9Sstevel@tonic-gate {
28947c478bd9Sstevel@tonic-gate 	Rel_copy	rc;
28957c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(rlmp);
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	rc.r_name = name;
28987c478bd9Sstevel@tonic-gate 	rc.r_rsym = rsym;		/* the new reference symbol and its */
28997c478bd9Sstevel@tonic-gate 	rc.r_rlmp = rlmp;		/*	associated link-map */
29007c478bd9Sstevel@tonic-gate 	rc.r_dlmp = dlmp;		/* the defining link-map */
29017c478bd9Sstevel@tonic-gate 	rc.r_dsym = dsym;		/* the original definition */
29027c478bd9Sstevel@tonic-gate 	rc.r_radd = radd;
29037c478bd9Sstevel@tonic-gate 	rc.r_dadd = dadd;
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	if (rsym->st_size > dsym->st_size)
29067c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)dsym->st_size;
29077c478bd9Sstevel@tonic-gate 	else
29087c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)rsym->st_size;
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 	if (alist_append(&COPY(dlmp), &rc, sizeof (Rel_copy),
29117c478bd9Sstevel@tonic-gate 	    AL_CNT_COPYREL) == 0) {
29127c478bd9Sstevel@tonic-gate 		if (!(lml->lm_flags & LML_FLG_TRC_WARN))
29137c478bd9Sstevel@tonic-gate 			return (0);
29147c478bd9Sstevel@tonic-gate 		else
29157c478bd9Sstevel@tonic-gate 			return (1);
29167c478bd9Sstevel@tonic-gate 	}
29177c478bd9Sstevel@tonic-gate 	if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) {
29187c478bd9Sstevel@tonic-gate 		if (alist_append(&COPY(rlmp), &dlmp,
29197c478bd9Sstevel@tonic-gate 		    sizeof (Rt_map *), AL_CNT_COPYREL) == 0) {
29207c478bd9Sstevel@tonic-gate 			if (!(lml->lm_flags & LML_FLG_TRC_WARN))
29217c478bd9Sstevel@tonic-gate 				return (0);
29227c478bd9Sstevel@tonic-gate 			else
29237c478bd9Sstevel@tonic-gate 				return (1);
29247c478bd9Sstevel@tonic-gate 		}
29257c478bd9Sstevel@tonic-gate 		FLAGS1(dlmp) |= FL1_RT_COPYTOOK;
29267c478bd9Sstevel@tonic-gate 	}
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	/*
29297c478bd9Sstevel@tonic-gate 	 * If we are tracing (ldd), warn the user if
29307c478bd9Sstevel@tonic-gate 	 *	1) the size from the reference symbol differs from the
29317c478bd9Sstevel@tonic-gate 	 *	   copy definition. We can only copy as much data as the
29327c478bd9Sstevel@tonic-gate 	 *	   reference (dynamic executables) entry allows.
29337c478bd9Sstevel@tonic-gate 	 *	2) the copy definition has STV_PROTECTED visibility.
29347c478bd9Sstevel@tonic-gate 	 */
29357c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
29367c478bd9Sstevel@tonic-gate 		if (rsym->st_size != dsym->st_size) {
29377c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF),
29385aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
29397c478bd9Sstevel@tonic-gate 			    NAME(rlmp), EC_XWORD(rsym->st_size),
29407c478bd9Sstevel@tonic-gate 			    NAME(dlmp), EC_XWORD(dsym->st_size));
29417c478bd9Sstevel@tonic-gate 			if (rsym->st_size > dsym->st_size)
29427c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA),
29437c478bd9Sstevel@tonic-gate 				    NAME(dlmp));
29447c478bd9Sstevel@tonic-gate 			else
29457c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC),
29467c478bd9Sstevel@tonic-gate 				    NAME(rlmp));
29477c478bd9Sstevel@tonic-gate 		}
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 		if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) {
29507c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_PROT),
29515aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
29527c478bd9Sstevel@tonic-gate 				NAME(dlmp));
29537c478bd9Sstevel@tonic-gate 		}
29547c478bd9Sstevel@tonic-gate 	}
29557c478bd9Sstevel@tonic-gate 
29565aefb655Srie 	DBG_CALL(Dbg_reloc_apply_val(lml, ELF_DBG_RTLD, (Xword)radd,
29575aefb655Srie 	    (Xword)rc.r_size));
29587c478bd9Sstevel@tonic-gate 	return (1);
29597c478bd9Sstevel@tonic-gate }
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate /*
29627c478bd9Sstevel@tonic-gate  * Determine the symbol location of an address within a link-map.  Look for
29637c478bd9Sstevel@tonic-gate  * the nearest symbol (whose value is less than or equal to the required
29647c478bd9Sstevel@tonic-gate  * address).  This is the object specific part of dladdr().
29657c478bd9Sstevel@tonic-gate  */
29667c478bd9Sstevel@tonic-gate static void
29677c478bd9Sstevel@tonic-gate elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags)
29687c478bd9Sstevel@tonic-gate {
29697c478bd9Sstevel@tonic-gate 	ulong_t		ndx, cnt, base, _value;
29707c478bd9Sstevel@tonic-gate 	Sym		*sym, *_sym;
29717c478bd9Sstevel@tonic-gate 	const char	*str;
29725343e1b3Sab 	int		_flags;
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	/*
29759039eeafSab 	 * If SUNWSYMTAB() is non-NULL, then it sees a special version of
29769039eeafSab 	 * the dynsym that starts with any local function symbols that exist in
29779039eeafSab 	 * the library and then moves to the data held in SYMTAB(). In this
29789039eeafSab 	 * case, SUNWSYMSZ tells us how long the symbol table is. The
29799039eeafSab 	 * availability of local function symbols will enhance the results
29809039eeafSab 	 * we can provide.
29819039eeafSab 	 *
29829039eeafSab 	 * If SUNWSYMTAB() is NULL, then SYMTAB() references a dynsym that
29839039eeafSab 	 * contains only global symbols. In that case, the length of
29849039eeafSab 	 * the symbol table comes from the nchain field of the related
29859039eeafSab 	 * symbol lookup hash table.
29867c478bd9Sstevel@tonic-gate 	 */
29877c478bd9Sstevel@tonic-gate 	str = STRTAB(lmp);
29889039eeafSab 	if (SUNWSYMSZ(lmp) == NULL) {
29899039eeafSab 		sym = SYMTAB(lmp);
29909039eeafSab 		/*
29919039eeafSab 		 * If we don't have a .hash table there are no symbols
29929039eeafSab 		 * to look at.
29939039eeafSab 		 */
29949039eeafSab 		if (HASH(lmp) == 0)
29959039eeafSab 			return;
29969039eeafSab 		cnt = HASH(lmp)[1];
29979039eeafSab 	} else {
29989039eeafSab 		sym = SUNWSYMTAB(lmp);
29999039eeafSab 		cnt = SUNWSYMSZ(lmp) / SYMENT(lmp);
30009039eeafSab 	}
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_FIXED)
30037c478bd9Sstevel@tonic-gate 		base = 0;
30047c478bd9Sstevel@tonic-gate 	else
30057c478bd9Sstevel@tonic-gate 		base = ADDR(lmp);
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	for (_sym = 0, _value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) {
30087c478bd9Sstevel@tonic-gate 		ulong_t	value;
30097c478bd9Sstevel@tonic-gate 
30109039eeafSab 		/*
30119039eeafSab 		 * Skip expected symbol types that are not functions
30129039eeafSab 		 * or data:
30139039eeafSab 		 *	- A symbol table starts with an undefined symbol
30149039eeafSab 		 *		in slot 0. If we are using SUNWSYMTAB(),
30159039eeafSab 		 *		there will be a second undefined symbol
30169039eeafSab 		 *		right before the globals.
30179039eeafSab 		 *	- The local part of SUNWSYMTAB() contains a
30189039eeafSab 		 *		series of function symbols. Each section
30199039eeafSab 		 *		starts with an initial STT_FILE symbol.
30209039eeafSab 		 */
30219039eeafSab 		if ((sym->st_shndx == SHN_UNDEF) ||
30229039eeafSab 		    (ELF_ST_TYPE(sym->st_info) == STT_FILE))
30237c478bd9Sstevel@tonic-gate 			continue;
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 		value = sym->st_value + base;
30267c478bd9Sstevel@tonic-gate 		if (value > addr)
30277c478bd9Sstevel@tonic-gate 			continue;
30287c478bd9Sstevel@tonic-gate 		if (value < _value)
30297c478bd9Sstevel@tonic-gate 			continue;
30307c478bd9Sstevel@tonic-gate 
30317c478bd9Sstevel@tonic-gate 		_sym = sym;
30327c478bd9Sstevel@tonic-gate 		_value = value;
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 		/*
30357c478bd9Sstevel@tonic-gate 		 * Note, because we accept local and global symbols we could
30367c478bd9Sstevel@tonic-gate 		 * find a section symbol that matches the associated address,
30377c478bd9Sstevel@tonic-gate 		 * which means that the symbol name will be null.  In this
30387c478bd9Sstevel@tonic-gate 		 * case continue the search in case we can find a global
30397c478bd9Sstevel@tonic-gate 		 * symbol of the same value.
30407c478bd9Sstevel@tonic-gate 		 */
30417c478bd9Sstevel@tonic-gate 		if ((value == addr) &&
30427c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) != STT_SECTION))
30437c478bd9Sstevel@tonic-gate 			break;
30447c478bd9Sstevel@tonic-gate 	}
30457c478bd9Sstevel@tonic-gate 
30465343e1b3Sab 	_flags = flags & RTLD_DL_MASK;
30477c478bd9Sstevel@tonic-gate 	if (_sym) {
30487c478bd9Sstevel@tonic-gate 		if (_flags == RTLD_DL_SYMENT)
30497c478bd9Sstevel@tonic-gate 			*info = (void *)_sym;
30507c478bd9Sstevel@tonic-gate 		else if (_flags == RTLD_DL_LINKMAP)
30517c478bd9Sstevel@tonic-gate 			*info = (void *)lmp;
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate 		dlip->dli_sname = str + _sym->st_name;
30547c478bd9Sstevel@tonic-gate 		dlip->dli_saddr = (void *)_value;
30555343e1b3Sab 	} else {
30565343e1b3Sab 		/*
30575343e1b3Sab 		 * addr lies between the beginning of the mapped segment and
30585343e1b3Sab 		 * the first global symbol. We have no symbol to return
30595343e1b3Sab 		 * and the caller requires one. We use _START_, the base
30605343e1b3Sab 		 * address of the mapping.
30615343e1b3Sab 		 */
30625343e1b3Sab 
30635343e1b3Sab 		if (_flags == RTLD_DL_SYMENT) {
30645343e1b3Sab 			/*
30655343e1b3Sab 			 * An actual symbol struct is needed, so we
30665343e1b3Sab 			 * construct one for _START_. To do this in a
30675343e1b3Sab 			 * fully accurate way requires a different symbol
30685343e1b3Sab 			 * for each mapped segment. This requires the
30695343e1b3Sab 			 * use of dynamic memory and a mutex. That's too much
30705343e1b3Sab 			 * plumbing for a fringe case of limited importance.
30715343e1b3Sab 			 *
30725343e1b3Sab 			 * Fortunately, we can simplify:
30735343e1b3Sab 			 *    - Only the st_size and st_info fields are useful
30745343e1b3Sab 			 *	outside of the linker internals. The others
30755343e1b3Sab 			 *	reference things that outside code cannot see,
30765343e1b3Sab 			 *	and can be set to 0.
30775343e1b3Sab 			 *    - It's just a label and there is no size
30785343e1b3Sab 			 *	to report. So, the size should be 0.
30795343e1b3Sab 			 * This means that only st_info needs a non-zero
30805343e1b3Sab 			 * (constant) value. A static struct will suffice.
30815343e1b3Sab 			 * It must be const (readonly) so the caller can't
30825343e1b3Sab 			 * change its meaning for subsequent callers.
30835343e1b3Sab 			 */
30845343e1b3Sab 			static const Sym fsym = { 0, 0, 0,
30855343e1b3Sab 				ELF_ST_INFO(STB_LOCAL, STT_OBJECT) };
30865343e1b3Sab 			*info = (void *) &fsym;
30875343e1b3Sab 		}
30885343e1b3Sab 
30895343e1b3Sab 		dlip->dli_sname = MSG_ORIG(MSG_SYM_START);
30905343e1b3Sab 		dlip->dli_saddr = (void *) ADDR(lmp);
30917c478bd9Sstevel@tonic-gate 	}
30927c478bd9Sstevel@tonic-gate }
30937c478bd9Sstevel@tonic-gate 
30947c478bd9Sstevel@tonic-gate static void
3095*9a411307Srie elf_lazy_cleanup(Alist *alp)
30967c478bd9Sstevel@tonic-gate {
3097*9a411307Srie 	Rt_map	**lmpp;
3098*9a411307Srie 	Aliste	off;
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	/*
31017c478bd9Sstevel@tonic-gate 	 * Cleanup any link-maps added to this dynamic list and free it.
31027c478bd9Sstevel@tonic-gate 	 */
31037c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alp, off, lmpp))
31047c478bd9Sstevel@tonic-gate 		FLAGS(*lmpp) &= ~FLG_RT_DLSYM;
31057c478bd9Sstevel@tonic-gate 	free(alp);
31067c478bd9Sstevel@tonic-gate }
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate /*
31097c478bd9Sstevel@tonic-gate  * This routine is called upon to search for a symbol from the dependencies of
31107c478bd9Sstevel@tonic-gate  * the initial link-map.  To maintain lazy loadings goal of reducing the number
31117c478bd9Sstevel@tonic-gate  * of objects mapped, any symbol search is first carried out using the objects
31127c478bd9Sstevel@tonic-gate  * that already exist in the process (either on a link-map list or handle).
31137c478bd9Sstevel@tonic-gate  * If a symbol can't be found, and lazy dependencies are still pending, this
31147c478bd9Sstevel@tonic-gate  * routine loads the dependencies in an attempt to locate the symbol.
31157c478bd9Sstevel@tonic-gate  *
31167c478bd9Sstevel@tonic-gate  * Only new objects are inspected as we will have already inspected presently
31177c478bd9Sstevel@tonic-gate  * loaded objects before calling this routine.  However, a new object may not
31187c478bd9Sstevel@tonic-gate  * be new - although the di_lmp might be zero, the object may have been mapped
31197c478bd9Sstevel@tonic-gate  * as someone elses dependency.  Thus there's a possibility of some symbol
31207c478bd9Sstevel@tonic-gate  * search duplication.
31217c478bd9Sstevel@tonic-gate  */
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate Sym *
31247c478bd9Sstevel@tonic-gate elf_lazy_find_sym(Slookup *slp, Rt_map **_lmp, uint_t *binfo)
31257c478bd9Sstevel@tonic-gate {
31267c478bd9Sstevel@tonic-gate 	Sym		*sym = 0;
31277c478bd9Sstevel@tonic-gate 	Alist *		alist = 0;
31287c478bd9Sstevel@tonic-gate 	Aliste		off;
31297c478bd9Sstevel@tonic-gate 	Rt_map **	lmpp, *	lmp = slp->sl_imap;
31307c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	if (alist_append(&alist, &lmp, sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0)
31337c478bd9Sstevel@tonic-gate 		return (0);
31347c478bd9Sstevel@tonic-gate 	FLAGS(lmp) |= FLG_RT_DLSYM;
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alist, off, lmpp)) {
31377c478bd9Sstevel@tonic-gate 		uint_t	cnt = 0;
31387c478bd9Sstevel@tonic-gate 		Slookup	sl = *slp;
31397c478bd9Sstevel@tonic-gate 		Dyninfo	*dip;
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 		/*
31427c478bd9Sstevel@tonic-gate 		 * Loop through the DT_NEEDED entries examining each object for
31437c478bd9Sstevel@tonic-gate 		 * the symbol.  If the symbol is not found the object is in turn
31447c478bd9Sstevel@tonic-gate 		 * added to the alist, so that its DT_NEEDED entires may be
31457c478bd9Sstevel@tonic-gate 		 * examined.
31467c478bd9Sstevel@tonic-gate 		 */
31477c478bd9Sstevel@tonic-gate 		lmp = *lmpp;
31487c478bd9Sstevel@tonic-gate 		for (dip = DYNINFO(lmp); cnt < DYNINFOCNT(lmp); cnt++, dip++) {
31497c478bd9Sstevel@tonic-gate 			Rt_map *nlmp;
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 			if (((dip->di_flags & FLG_DI_NEEDED) == 0) ||
31527c478bd9Sstevel@tonic-gate 			    dip->di_info)
31537c478bd9Sstevel@tonic-gate 				continue;
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 			/*
31567c478bd9Sstevel@tonic-gate 			 * If this entry defines a lazy dependency try loading
31577c478bd9Sstevel@tonic-gate 			 * it.  If the file can't be loaded, consider this
31587c478bd9Sstevel@tonic-gate 			 * non-fatal and continue the search (lazy loaded
31597c478bd9Sstevel@tonic-gate 			 * dependencies need not exist and their loading should
31607c478bd9Sstevel@tonic-gate 			 * only be fatal if called from a relocation).
31617c478bd9Sstevel@tonic-gate 			 *
31627c478bd9Sstevel@tonic-gate 			 * If the file is already loaded and relocated we must
31637c478bd9Sstevel@tonic-gate 			 * still inspect it for symbols, even though it might
31647c478bd9Sstevel@tonic-gate 			 * have already been searched.  This lazy load operation
31657c478bd9Sstevel@tonic-gate 			 * might have promoted the permissions of the object,
31667c478bd9Sstevel@tonic-gate 			 * and thus made the object applicable for this symbol
31677c478bd9Sstevel@tonic-gate 			 * search, whereas before the object might have been
31687c478bd9Sstevel@tonic-gate 			 * skipped.
31697c478bd9Sstevel@tonic-gate 			 */
31707c478bd9Sstevel@tonic-gate 			if ((nlmp = elf_lazy_load(lmp, cnt, name)) == 0)
31717c478bd9Sstevel@tonic-gate 				continue;
31727c478bd9Sstevel@tonic-gate 
31737c478bd9Sstevel@tonic-gate 			/*
31747c478bd9Sstevel@tonic-gate 			 * If this object isn't yet a part of the dynamic list
31757c478bd9Sstevel@tonic-gate 			 * then inspect it for the symbol.  If the symbol isn't
31767c478bd9Sstevel@tonic-gate 			 * found add the object to the dynamic list so that we
31777c478bd9Sstevel@tonic-gate 			 * can inspect its dependencies.
31787c478bd9Sstevel@tonic-gate 			 */
31797c478bd9Sstevel@tonic-gate 			if (FLAGS(nlmp) & FLG_RT_DLSYM)
31807c478bd9Sstevel@tonic-gate 				continue;
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 			sl.sl_imap = nlmp;
31837c478bd9Sstevel@tonic-gate 			if (sym = LM_LOOKUP_SYM(sl.sl_cmap)(&sl, _lmp, binfo))
31847c478bd9Sstevel@tonic-gate 				break;
31857c478bd9Sstevel@tonic-gate 
31867c478bd9Sstevel@tonic-gate 			/*
31877c478bd9Sstevel@tonic-gate 			 * Some dlsym() operations are already traversing a
31887c478bd9Sstevel@tonic-gate 			 * link-map (dlopen(0)), and thus there's no need to
31897c478bd9Sstevel@tonic-gate 			 * build our own dynamic dependency list.
31907c478bd9Sstevel@tonic-gate 			 */
31917c478bd9Sstevel@tonic-gate 			if ((sl.sl_flags & LKUP_NODESCENT) == 0) {
31927c478bd9Sstevel@tonic-gate 				if (alist_append(&alist, &nlmp,
31937c478bd9Sstevel@tonic-gate 				    sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0) {
31947c478bd9Sstevel@tonic-gate 					elf_lazy_cleanup(alist);
31957c478bd9Sstevel@tonic-gate 					return (0);
31967c478bd9Sstevel@tonic-gate 				}
31977c478bd9Sstevel@tonic-gate 				FLAGS(nlmp) |= FLG_RT_DLSYM;
31987c478bd9Sstevel@tonic-gate 			}
31997c478bd9Sstevel@tonic-gate 		}
32007c478bd9Sstevel@tonic-gate 		if (sym)
32017c478bd9Sstevel@tonic-gate 			break;
32027c478bd9Sstevel@tonic-gate 	}
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	elf_lazy_cleanup(alist);
32057c478bd9Sstevel@tonic-gate 	return (sym);
32067c478bd9Sstevel@tonic-gate }
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate /*
32097c478bd9Sstevel@tonic-gate  * Warning message for bad r_offset.
32107c478bd9Sstevel@tonic-gate  */
32117c478bd9Sstevel@tonic-gate void
32127c478bd9Sstevel@tonic-gate elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset,
32137c478bd9Sstevel@tonic-gate     ulong_t rsymndx)
32147c478bd9Sstevel@tonic-gate {
32157c478bd9Sstevel@tonic-gate 	const char	*name = (char *)0;
32165aefb655Srie 	Lm_list		*lml = LIST(lmp);
32177c478bd9Sstevel@tonic-gate 	int		trace;
32187c478bd9Sstevel@tonic-gate 
32195aefb655Srie 	if ((lml->lm_flags & LML_FLG_TRC_ENABLE) &&
32207c478bd9Sstevel@tonic-gate 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
32215aefb655Srie 	    (lml->lm_flags & LML_FLG_TRC_VERBOSE)))
32227c478bd9Sstevel@tonic-gate 		trace = 1;
32237c478bd9Sstevel@tonic-gate 	else
32247c478bd9Sstevel@tonic-gate 		trace = 0;
32257c478bd9Sstevel@tonic-gate 
32265aefb655Srie 	if ((trace == 0) && (DBG_ENABLED == 0))
32277c478bd9Sstevel@tonic-gate 		return;
32287c478bd9Sstevel@tonic-gate 
32297c478bd9Sstevel@tonic-gate 	if (rsymndx) {
32307c478bd9Sstevel@tonic-gate 		Sym	*symref = (Sym *)((ulong_t)SYMTAB(lmp) +
32317c478bd9Sstevel@tonic-gate 				(rsymndx * SYMENT(lmp)));
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(symref->st_info) != STB_LOCAL)
32347c478bd9Sstevel@tonic-gate 			name = (char *)(STRTAB(lmp) + symref->st_name);
32357c478bd9Sstevel@tonic-gate 	}
32367c478bd9Sstevel@tonic-gate 
32377c478bd9Sstevel@tonic-gate 	if (name == 0)
32387c478bd9Sstevel@tonic-gate 		name = MSG_ORIG(MSG_STR_EMPTY);
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	if (trace) {
32417c478bd9Sstevel@tonic-gate 		const char *rstr;
32427c478bd9Sstevel@tonic-gate 
32435aefb655Srie 		rstr = _conv_reloc_type((uint_t)rtype);
32447c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name,
32457c478bd9Sstevel@tonic-gate 		    EC_ADDR(roffset));
32467c478bd9Sstevel@tonic-gate 		return;
32477c478bd9Sstevel@tonic-gate 	}
32487c478bd9Sstevel@tonic-gate 
32495aefb655Srie 	Dbg_reloc_error(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel, name);
32507c478bd9Sstevel@tonic-gate }
3251d326b23bSrie 
3252d326b23bSrie /*
3253d326b23bSrie  * Resolve a static TLS relocation.
3254d326b23bSrie  */
3255d326b23bSrie long
3256d326b23bSrie elf_static_tls(Rt_map *lmp, Sym *sym, void *rel, uchar_t rtype, char *name,
3257d326b23bSrie     ulong_t roffset, long value)
3258d326b23bSrie {
3259d326b23bSrie 	Lm_list	*lml = LIST(lmp);
3260d326b23bSrie 
3261d326b23bSrie 	/*
3262d326b23bSrie 	 * Relocations against a static TLS block have limited support once
3263d326b23bSrie 	 * process initialization has completed.  Any error condition should be
3264d326b23bSrie 	 * discovered by testing for DF_STATIC_TLS as part of loading an object,
3265d326b23bSrie 	 * however individual relocations are tested in case the dynamic flag
3266d326b23bSrie 	 * had not been set when this object was built.
3267d326b23bSrie 	 */
3268d326b23bSrie 	if (PTTLS(lmp) == 0) {
3269d326b23bSrie 		DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
3270d326b23bSrie 		    M_REL_SHT_TYPE, rel, NULL, name));
3271d326b23bSrie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
3272d326b23bSrie 		    _conv_reloc_type((uint_t)rtype), NAME(lmp),
3273d326b23bSrie 		    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
3274d326b23bSrie 		return (0);
3275d326b23bSrie 	}
3276d326b23bSrie 
3277d326b23bSrie 	/*
3278d326b23bSrie 	 * If no static TLS has been set aside for this object, determine if
3279d326b23bSrie 	 * any can be obtained.  Enforce that any object using static TLS is
3280d326b23bSrie 	 * non-deletable.
3281d326b23bSrie 	 */
3282d326b23bSrie 	if (TLSSTATOFF(lmp) == 0) {
3283d326b23bSrie 		FLAGS1(lmp) |= FL1_RT_TLSSTAT;
3284d326b23bSrie 		MODE(lmp) |= RTLD_NODELETE;
3285d326b23bSrie 
3286d326b23bSrie 		if (tls_assign(lml, lmp, PTTLS(lmp)) == 0) {
3287d326b23bSrie 			DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
3288d326b23bSrie 			    M_REL_SHT_TYPE, rel, NULL, name));
3289d326b23bSrie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
3290d326b23bSrie 			    _conv_reloc_type((uint_t)rtype), NAME(lmp),
3291d326b23bSrie 			    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
3292d326b23bSrie 			return (0);
3293d326b23bSrie 		}
3294d326b23bSrie 	}
3295d326b23bSrie 
3296d326b23bSrie 	/*
3297d326b23bSrie 	 * Typically, a static TLS offset is maintained as a symbols value.
3298d326b23bSrie 	 * For local symbols that are not apart of the dynamic symbol table,
3299d326b23bSrie 	 * the TLS relocation points to a section symbol, and the static TLS
3300d326b23bSrie 	 * offset was deposited in the associated GOT table.  Make sure the GOT
3301d326b23bSrie 	 * is cleared, so that the value isn't reused in do_reloc().
3302d326b23bSrie 	 */
3303d326b23bSrie 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
3304d326b23bSrie 		if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION)) {
3305d326b23bSrie 			value = *(long *)roffset;
3306d326b23bSrie 			*(long *)roffset = 0;
3307d326b23bSrie 		} else {
3308d326b23bSrie 			value = sym->st_value;
3309d326b23bSrie 		}
3310d326b23bSrie 	}
3311d326b23bSrie 	return (-(TLSSTATOFF(lmp) - value));
3312d326b23bSrie }
3313