xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/elf.c (revision 11a2bb38)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*11a2bb38Srie 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
257c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Object file dependent support for ELF objects.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate #include	"_synonyms.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include	<stdio.h>
387c478bd9Sstevel@tonic-gate #include	<sys/procfs.h>
397c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
407c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
417c478bd9Sstevel@tonic-gate #include	<string.h>
427c478bd9Sstevel@tonic-gate #include	<limits.h>
437c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
447c478bd9Sstevel@tonic-gate #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 #include	"debug.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Default and secure dependency search paths.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate static Pnode		elf_dflt_dirs[] = {
557c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
567c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
577c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIB_64),		0,	MSG_PTH_LIB_64_SIZE,
587c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIB_64),		0,	MSG_PTH_USRLIB_64_SIZE,
617c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0, 0 }
627c478bd9Sstevel@tonic-gate #else
637c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
647c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIB),		0,	MSG_PTH_LIB_SIZE,
657c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIB),		0,	MSG_PTH_USRLIB_SIZE,
687c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0, 0 }
697c478bd9Sstevel@tonic-gate #endif
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static Pnode		elf_secure_dirs[] = {
737c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
747c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
757c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIBSE_64),		0,	MSG_PTH_LIBSE_64_SIZE,
767c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
777c478bd9Sstevel@tonic-gate #endif
787c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIBSE_64),	0,
797c478bd9Sstevel@tonic-gate 		MSG_PTH_USRLIBSE_64_SIZE,
807c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0, 0 }
817c478bd9Sstevel@tonic-gate #else
827c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
837c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIBSE),		0,	MSG_PTH_LIBSE_SIZE,
847c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
857c478bd9Sstevel@tonic-gate #endif
867c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIBSE),		0,	MSG_PTH_USRLIBSE_SIZE,
877c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0, 0 }
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate };
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Defines for local functions.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate static Pnode	*elf_fix_name(const char *, Rt_map *, uint_t);
957c478bd9Sstevel@tonic-gate static int	elf_are_u(Rej_desc *);
967c478bd9Sstevel@tonic-gate static void	elf_dladdr(ulong_t, Rt_map *, Dl_info *, void **, int);
977c478bd9Sstevel@tonic-gate static ulong_t	elf_entry_pt(void);
987c478bd9Sstevel@tonic-gate static char	*elf_get_so(const char *, const char *);
997c478bd9Sstevel@tonic-gate static Rt_map	*elf_map_so(Lm_list *, Aliste, const char *, const char *, int);
1007c478bd9Sstevel@tonic-gate static int	elf_needed(Lm_list *, Aliste, Rt_map *);
1017c478bd9Sstevel@tonic-gate static void	elf_unmap_so(Rt_map *);
1027c478bd9Sstevel@tonic-gate static int	elf_verify_vers(const char *, Rt_map *, Rt_map *);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * Functions and data accessed through indirect pointers.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate Fct elf_fct = {
1087c478bd9Sstevel@tonic-gate 	elf_are_u,
1097c478bd9Sstevel@tonic-gate 	elf_entry_pt,
1107c478bd9Sstevel@tonic-gate 	elf_map_so,
1117c478bd9Sstevel@tonic-gate 	elf_unmap_so,
1127c478bd9Sstevel@tonic-gate 	elf_needed,
1137c478bd9Sstevel@tonic-gate 	lookup_sym,
1147c478bd9Sstevel@tonic-gate 	elf_reloc,
1157c478bd9Sstevel@tonic-gate 	elf_dflt_dirs,
1167c478bd9Sstevel@tonic-gate 	elf_secure_dirs,
1177c478bd9Sstevel@tonic-gate 	elf_fix_name,
1187c478bd9Sstevel@tonic-gate 	elf_get_so,
1197c478bd9Sstevel@tonic-gate 	elf_dladdr,
1207c478bd9Sstevel@tonic-gate 	dlsym_handle,
1217c478bd9Sstevel@tonic-gate 	elf_verify_vers,
1227c478bd9Sstevel@tonic-gate 	elf_set_prot
1237c478bd9Sstevel@tonic-gate };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /*
1277c478bd9Sstevel@tonic-gate  * Redefine NEEDED name if necessary.
1287c478bd9Sstevel@tonic-gate  */
1297c478bd9Sstevel@tonic-gate static Pnode *
1307c478bd9Sstevel@tonic-gate elf_fix_name(const char *name, Rt_map *clmp, uint_t orig)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * For ABI compliance, if we are asked for ld.so.1, then really give
1347c478bd9Sstevel@tonic-gate 	 * them libsys.so.1 (the SONAME of libsys.so.1 is ld.so.1).
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	if (((*name == '/') &&
1377c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
1387c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD_64)) == 0)) ||
1397c478bd9Sstevel@tonic-gate #else
1407c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD)) == 0)) ||
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_FIL_RTLD)) == 0)) {
1437c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_fixname(name, 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;
3027c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_file_lazyload(name, NAME(clmp), 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 	 */
4707c478bd9Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
4717c478bd9Sstevel@tonic-gate 	    vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) {
4727c478bd9Sstevel@tonic-gate 		Half		cnt = vnd->vn_cnt;
4737c478bd9Sstevel@tonic-gate 		Vernaux		*vnap;
4747c478bd9Sstevel@tonic-gate 		char		*nstrs, *need;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		/*
4777c478bd9Sstevel@tonic-gate 		 * Determine if a needed entry matches this dependency.
4787c478bd9Sstevel@tonic-gate 		 */
4797c478bd9Sstevel@tonic-gate 		need = (char *)(cstrs + vnd->vn_file);
4807c478bd9Sstevel@tonic-gate 		if (strcmp(name, need) != 0)
4817c478bd9Sstevel@tonic-gate 			continue;
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_ver_need_title(NAME(clmp)));
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);
5027c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_ver_need_entry(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))) {
5557c478bd9Sstevel@tonic-gate 				eprintf(ERR_FATAL, MSG_INTL(MSG_VER_NFOUND),
5567c478bd9Sstevel@tonic-gate 				    need, version, NAME(clmp));
5577c478bd9Sstevel@tonic-gate 				return (0);
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 		return (1);
5617c478bd9Sstevel@tonic-gate 	}
5627c478bd9Sstevel@tonic-gate 	return (1);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Search through the dynamic section for DT_NEEDED entries and perform one
5677c478bd9Sstevel@tonic-gate  * of two functions.  If only the first argument is specified then load the
5687c478bd9Sstevel@tonic-gate  * defined shared object, otherwise add the link map representing the defined
5697c478bd9Sstevel@tonic-gate  * link map the the dlopen list.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate static int
5727c478bd9Sstevel@tonic-gate elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate 	Dyn		*dyn;
5757c478bd9Sstevel@tonic-gate 	ulong_t		ndx = 0;
5767c478bd9Sstevel@tonic-gate 	uint_t		lazy = 0, flags = 0;
5777c478bd9Sstevel@tonic-gate 	Word		lmflags = lml->lm_flags;
5787c478bd9Sstevel@tonic-gate 	Word		lmtflags = lml->lm_tflags;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	/*
5817c478bd9Sstevel@tonic-gate 	 * Process each shared object on needed list.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	if (DYN(clmp) == 0)
5847c478bd9Sstevel@tonic-gate 		return (1);
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	for (dyn = (Dyn *)DYN(clmp); dyn->d_tag != DT_NULL; dyn++, ndx++) {
5877c478bd9Sstevel@tonic-gate 		Dyninfo	*dip = &DYNINFO(clmp)[ndx];
5887c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
5897c478bd9Sstevel@tonic-gate 		char	*name;
5907c478bd9Sstevel@tonic-gate 		int	silent = 0;
5917c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		switch (dyn->d_tag) {
5947c478bd9Sstevel@tonic-gate 		case DT_POSFLAG_1:
5957c478bd9Sstevel@tonic-gate 			if ((dyn->d_un.d_val & DF_P1_LAZYLOAD) &&
5967c478bd9Sstevel@tonic-gate 			    !(lmtflags & LML_TFLG_NOLAZYLD))
5977c478bd9Sstevel@tonic-gate 				lazy = 1;
5987c478bd9Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_P1_GROUPPERM)
5997c478bd9Sstevel@tonic-gate 				flags = (FLG_RT_SETGROUP | FLG_RT_HANDLE);
6007c478bd9Sstevel@tonic-gate 			continue;
6017c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
6027c478bd9Sstevel@tonic-gate 		case DT_USED:
6037c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_NEEDED;
6047c478bd9Sstevel@tonic-gate 			if (flags)
6057c478bd9Sstevel@tonic-gate 				dip->di_flags |= FLG_DI_GROUP;
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 			name = (char *)STRTAB(clmp) + dyn->d_un.d_val;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 			/*
6107c478bd9Sstevel@tonic-gate 			 * NOTE, libc.so.1 can't be lazy loaded.  Although a
6117c478bd9Sstevel@tonic-gate 			 * lazy position flag won't be produced when a RTLDINFO
6127c478bd9Sstevel@tonic-gate 			 * .dynamic entry is found (introduced with the UPM in
6137c478bd9Sstevel@tonic-gate 			 * Solaris 10), it was possible to mark libc for lazy
6147c478bd9Sstevel@tonic-gate 			 * loading on previous releases.  To reduce the overhead
6157c478bd9Sstevel@tonic-gate 			 * of testing for this occurrence, only carry out this
6167c478bd9Sstevel@tonic-gate 			 * check for the first object on the link-map list
6177c478bd9Sstevel@tonic-gate 			 * (there aren't many applications built without libc).
6187c478bd9Sstevel@tonic-gate 			 */
6197c478bd9Sstevel@tonic-gate 			if (lazy && (lml->lm_head == clmp) &&
6207c478bd9Sstevel@tonic-gate 			    (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0))
6217c478bd9Sstevel@tonic-gate 				lazy = 0;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			/*
6247c478bd9Sstevel@tonic-gate 			 * Don't bring in lazy loaded objects yet unless we've
6257c478bd9Sstevel@tonic-gate 			 * been asked to attempt to load all available objects
6267c478bd9Sstevel@tonic-gate 			 * (crle(1) sets LD_FLAGS=loadavail).  Even under
6277c478bd9Sstevel@tonic-gate 			 * RTLD_NOW we don't process this - RTLD_NOW will cause
6287c478bd9Sstevel@tonic-gate 			 * relocation processing which in turn might trigger
6297c478bd9Sstevel@tonic-gate 			 * lazy loading, but its possible that the object has a
6307c478bd9Sstevel@tonic-gate 			 * lazy loaded file with no bindings (i.e., it should
6317c478bd9Sstevel@tonic-gate 			 * never have been a dependency in the first place).
6327c478bd9Sstevel@tonic-gate 			 */
6337c478bd9Sstevel@tonic-gate 			if (lazy) {
6347c478bd9Sstevel@tonic-gate 				if ((lmflags & LML_FLG_LOADAVAIL) == 0) {
6357c478bd9Sstevel@tonic-gate 					LAZY(clmp)++;
6367c478bd9Sstevel@tonic-gate 					lazy = flags = 0;
6377c478bd9Sstevel@tonic-gate 					continue;
6387c478bd9Sstevel@tonic-gate 				}
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 				/*
6417c478bd9Sstevel@tonic-gate 				 * Silence any error messages - see description
6427c478bd9Sstevel@tonic-gate 				 * under elf_lookup_filtee().
6437c478bd9Sstevel@tonic-gate 				 */
6447c478bd9Sstevel@tonic-gate 				if ((rtld_flags & RT_FL_SILENCERR) == 0) {
6457c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_SILENCERR;
6467c478bd9Sstevel@tonic-gate 					silent = 1;
6477c478bd9Sstevel@tonic-gate 				}
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 			break;
6507c478bd9Sstevel@tonic-gate 		case DT_AUXILIARY:
6517c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_AUXFLTR;
6527c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6537c478bd9Sstevel@tonic-gate 			continue;
6547c478bd9Sstevel@tonic-gate 		case DT_SUNW_AUXILIARY:
6557c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR);
6567c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6577c478bd9Sstevel@tonic-gate 			continue;
6587c478bd9Sstevel@tonic-gate 		case DT_FILTER:
6597c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_STDFLTR;
6607c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6617c478bd9Sstevel@tonic-gate 			continue;
6627c478bd9Sstevel@tonic-gate 		case DT_SUNW_FILTER:
6637c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR);
6647c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6657c478bd9Sstevel@tonic-gate 			continue;
6667c478bd9Sstevel@tonic-gate 		default:
6677c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
6687c478bd9Sstevel@tonic-gate 			continue;
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_needed(name, NAME(clmp)));
6727c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & LML_FLG_TRC_ENABLE)
6737c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_PROCESSD;
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		/*
6767c478bd9Sstevel@tonic-gate 		 * Establish the objects name, load it and establish a binding
6777c478bd9Sstevel@tonic-gate 		 * with the caller.
6787c478bd9Sstevel@tonic-gate 		 */
6797c478bd9Sstevel@tonic-gate 		if (((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0) ||
6807c478bd9Sstevel@tonic-gate 		    ((nlmp = load_one(lml, lmco, pnp, clmp, MODE(clmp),
6817c478bd9Sstevel@tonic-gate 		    flags, 0)) == 0) || (bind_one(clmp, nlmp, BND_NEEDED) == 0))
6827c478bd9Sstevel@tonic-gate 			nlmp = 0;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		/*
6857c478bd9Sstevel@tonic-gate 		 * Clean up any infrastructure, including the removal of the
6867c478bd9Sstevel@tonic-gate 		 * error suppression state, if it had been previously set in
6877c478bd9Sstevel@tonic-gate 		 * this routine.
6887c478bd9Sstevel@tonic-gate 		 */
6897c478bd9Sstevel@tonic-gate 		if (pnp)
6907c478bd9Sstevel@tonic-gate 			remove_pnode(pnp);
6917c478bd9Sstevel@tonic-gate 		if (silent)
6927c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_SILENCERR;
6937c478bd9Sstevel@tonic-gate 		lazy = flags = 0;
6947c478bd9Sstevel@tonic-gate 		if ((dip->di_info = (void *)nlmp) == 0) {
6957c478bd9Sstevel@tonic-gate 			/*
6967c478bd9Sstevel@tonic-gate 			 * If the object could not be mapped, continue if error
6977c478bd9Sstevel@tonic-gate 			 * suppression is established or we're here with ldd(1).
6987c478bd9Sstevel@tonic-gate 			 */
6997c478bd9Sstevel@tonic-gate 			if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags &
7007c478bd9Sstevel@tonic-gate 			    (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE)))
7017c478bd9Sstevel@tonic-gate 				continue;
7027c478bd9Sstevel@tonic-gate 			else
7037c478bd9Sstevel@tonic-gate 				return (0);
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if (LAZY(clmp))
7087c478bd9Sstevel@tonic-gate 		lml->lm_lazy++;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	return (1);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate static int
7147c478bd9Sstevel@tonic-gate elf_map_check(const char *name, caddr_t vaddr, Off size)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	prmap_t		*maps, *_maps;
7177c478bd9Sstevel@tonic-gate 	int		pfd, num, _num;
7187c478bd9Sstevel@tonic-gate 	caddr_t		eaddr = vaddr + size;
7197c478bd9Sstevel@tonic-gate 	int		err;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	/*
7227c478bd9Sstevel@tonic-gate 	 * If memory reservations have been established for alternative objects
7237c478bd9Sstevel@tonic-gate 	 * determine if this object falls within the reservation, if it does no
7247c478bd9Sstevel@tonic-gate 	 * further checking is required.
7257c478bd9Sstevel@tonic-gate 	 */
7267c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_MEMRESV) {
7277c478bd9Sstevel@tonic-gate 		Rtc_head	*head = (Rtc_head *)config->c_bgn;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		if ((vaddr >= (caddr_t)(uintptr_t)head->ch_resbgn) &&
7307c478bd9Sstevel@tonic-gate 		    (eaddr <= (caddr_t)(uintptr_t)head->ch_resend))
7317c478bd9Sstevel@tonic-gate 			return (0);
7327c478bd9Sstevel@tonic-gate 	}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * Determine the mappings presently in use by this process.
7367c478bd9Sstevel@tonic-gate 	 */
7377c478bd9Sstevel@tonic-gate 	if ((pfd = pr_open()) == FD_UNAVAIL)
7387c478bd9Sstevel@tonic-gate 		return (1);
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCNMAP, (void *)&num) == -1) {
7417c478bd9Sstevel@tonic-gate 		err = errno;
74241072f3cSrie 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name, strerror(err));
7437c478bd9Sstevel@tonic-gate 		return (1);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	if ((maps = malloc((num + 1) * sizeof (prmap_t))) == 0)
7477c478bd9Sstevel@tonic-gate 		return (1);
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCMAP, (void *)maps) == -1) {
7507c478bd9Sstevel@tonic-gate 		err = errno;
75141072f3cSrie 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_PROC), name, strerror(err));
7527c478bd9Sstevel@tonic-gate 		free(maps);
7537c478bd9Sstevel@tonic-gate 		return (1);
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	/*
7577c478bd9Sstevel@tonic-gate 	 * Determine if the supplied address clashes with any of the present
7587c478bd9Sstevel@tonic-gate 	 * process mappings.
7597c478bd9Sstevel@tonic-gate 	 */
7607c478bd9Sstevel@tonic-gate 	for (_num = 0, _maps = maps; _num < num; _num++, _maps++) {
7617c478bd9Sstevel@tonic-gate 		caddr_t		_eaddr = _maps->pr_vaddr + _maps->pr_size;
7627c478bd9Sstevel@tonic-gate 		Rt_map		*lmp;
7637c478bd9Sstevel@tonic-gate 		const char	*str;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		if ((eaddr < _maps->pr_vaddr) || (vaddr >= _eaddr))
7667c478bd9Sstevel@tonic-gate 			continue;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 		/*
7697c478bd9Sstevel@tonic-gate 		 * We have a memory clash.  See if one of the known dynamic
7707c478bd9Sstevel@tonic-gate 		 * dependency mappings represents this space so as to provide
7717c478bd9Sstevel@tonic-gate 		 * the user a more meaningful message.
7727c478bd9Sstevel@tonic-gate 		 */
7737c478bd9Sstevel@tonic-gate 		if ((lmp = _caller(vaddr, 0)) != 0)
7747c478bd9Sstevel@tonic-gate 			str = NAME(lmp);
7757c478bd9Sstevel@tonic-gate 		else
7767c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_STR_UNKNOWN);
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_MAPINUSE), name,
7797c478bd9Sstevel@tonic-gate 		    EC_ADDR(vaddr), EC_OFF(size), str);
7807c478bd9Sstevel@tonic-gate 		return (1);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	free(maps);
7837c478bd9Sstevel@tonic-gate 	return (0);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate /*
7877c478bd9Sstevel@tonic-gate  * Obtain a memory reservation.  On newer systems, both MAP_ANON and MAP_ALIGN
7887c478bd9Sstevel@tonic-gate  * are used to obtained an aligned reservation from anonymous memory.  If
7897c478bd9Sstevel@tonic-gate  * MAP_ANON isn't available, then MAP_ALIGN isn't either, so obtain a standard
7907c478bd9Sstevel@tonic-gate  * reservation using the file as backing.
7917c478bd9Sstevel@tonic-gate  */
7927c478bd9Sstevel@tonic-gate static Am_ret
7937c478bd9Sstevel@tonic-gate elf_map_reserve(const char *name, caddr_t *maddr, Off msize, int mperm,
7947c478bd9Sstevel@tonic-gate     int fd, Xword align)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	Am_ret	amret;
7977c478bd9Sstevel@tonic-gate 	int	mflag = MAP_PRIVATE | MAP_NORESERVE;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
8007c478bd9Sstevel@tonic-gate 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
8017c478bd9Sstevel@tonic-gate 		mflag |= MAP_ALIGN;
8027c478bd9Sstevel@tonic-gate 		*maddr = (caddr_t)align;
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate #endif
8057c478bd9Sstevel@tonic-gate 	if ((amret = anon_map(maddr, msize, PROT_NONE, mflag)) == AM_ERROR)
8067c478bd9Sstevel@tonic-gate 		return (amret);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	if (amret == AM_OK)
8097c478bd9Sstevel@tonic-gate 		return (AM_OK);
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * If an anonymous memory request failed (which should only be the
8137c478bd9Sstevel@tonic-gate 	 * case if it is unsupported on the system we're running on), establish
8147c478bd9Sstevel@tonic-gate 	 * the initial mapping directly from the file.
8157c478bd9Sstevel@tonic-gate 	 */
8167c478bd9Sstevel@tonic-gate 	*maddr = 0;
8177c478bd9Sstevel@tonic-gate 	if ((*maddr = mmap(*maddr, msize, mperm, MAP_PRIVATE,
8187c478bd9Sstevel@tonic-gate 	    fd, 0)) == MAP_FAILED) {
8197c478bd9Sstevel@tonic-gate 		int	err = errno;
8207c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), name, strerror(err));
8217c478bd9Sstevel@tonic-gate 		return (AM_ERROR);
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	return (AM_NOSUP);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate static void *
8277c478bd9Sstevel@tonic-gate elf_map_textdata(caddr_t addr, Off flen, int mperm, int phdr_mperm, int mflag,
8287c478bd9Sstevel@tonic-gate     int fd, Off foff)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
8317c478bd9Sstevel@tonic-gate 	static int	notd = 0;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	/*
8347c478bd9Sstevel@tonic-gate 	 * If MAP_TEXT and MAP_INITDATA are available, select the appropriate
8357c478bd9Sstevel@tonic-gate 	 * flag.
8367c478bd9Sstevel@tonic-gate 	 */
8377c478bd9Sstevel@tonic-gate 	if (notd == 0) {
8387c478bd9Sstevel@tonic-gate 		if ((phdr_mperm & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC)
8397c478bd9Sstevel@tonic-gate 			mflag |= MAP_TEXT;
8407c478bd9Sstevel@tonic-gate 		else
8417c478bd9Sstevel@tonic-gate 			mflag |= MAP_INITDATA;
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate #endif
8447c478bd9Sstevel@tonic-gate 	if (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff) != MAP_FAILED)
8457c478bd9Sstevel@tonic-gate 		return (0);
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
8487c478bd9Sstevel@tonic-gate 	if ((notd == 0) && (errno == EINVAL)) {
8497c478bd9Sstevel@tonic-gate 		/*
8507c478bd9Sstevel@tonic-gate 		 * MAP_TEXT and MAP_INITDATA may not be supported on this
8517c478bd9Sstevel@tonic-gate 		 * platform, try again without.
8527c478bd9Sstevel@tonic-gate 		 */
8537c478bd9Sstevel@tonic-gate 		notd = 1;
8547c478bd9Sstevel@tonic-gate 		mflag &= ~(MAP_TEXT | MAP_INITDATA);
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 		return (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff));
8577c478bd9Sstevel@tonic-gate 	}
8587c478bd9Sstevel@tonic-gate #endif
8597c478bd9Sstevel@tonic-gate 	return (MAP_FAILED);
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate /*
8637c478bd9Sstevel@tonic-gate  * Map in a file.
8647c478bd9Sstevel@tonic-gate  */
8657c478bd9Sstevel@tonic-gate static caddr_t
8667c478bd9Sstevel@tonic-gate elf_map_it(
8677c478bd9Sstevel@tonic-gate 	const char	*name,		/* actual name stored for pathname */
8687c478bd9Sstevel@tonic-gate 	Off		fsize,		/* total mapping claim of the file */
8697c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr,		/* ELF header of file */
8707c478bd9Sstevel@tonic-gate 	Phdr		*fphdr,		/* first loadable Phdr */
8717c478bd9Sstevel@tonic-gate 	Phdr		*lphdr,		/* last loadable Phdr */
8727c478bd9Sstevel@tonic-gate 	Phdr		**rrphdr,	/* return first Phdr in reservation */
8737c478bd9Sstevel@tonic-gate 	caddr_t		*rraddr,	/* return start of reservation */
8747c478bd9Sstevel@tonic-gate 	Off		*rrsize,	/* return total size of reservation */
8757c478bd9Sstevel@tonic-gate 	int		fixed,		/* image is resolved to a fixed addr */
8767c478bd9Sstevel@tonic-gate 	int		fd,		/* images file descriptor */
8777c478bd9Sstevel@tonic-gate 	Xword		align,		/* image segments maximum alignment */
8787c478bd9Sstevel@tonic-gate 	Mmap		*mmaps,		/* mmap information array and */
8797c478bd9Sstevel@tonic-gate 	uint_t		*mmapcnt)	/* 	mapping count */
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	caddr_t		raddr;		/* reservation address */
8827c478bd9Sstevel@tonic-gate 	Off		rsize;		/* reservation size */
8837c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* working program header poiner */
8847c478bd9Sstevel@tonic-gate 	caddr_t		maddr;		/* working mmap address */
8857c478bd9Sstevel@tonic-gate 	caddr_t		faddr;		/* working file address */
8867c478bd9Sstevel@tonic-gate 	size_t		padsize;	/* object padding requirement */
8877c478bd9Sstevel@tonic-gate 	size_t		padpsize = 0;	/* padding size rounded to next page */
8887c478bd9Sstevel@tonic-gate 	size_t		padmsize = 0;	/* padding size rounded for alignment */
8897c478bd9Sstevel@tonic-gate 	int		skipfseg;	/* skip mapping first segment */
8907c478bd9Sstevel@tonic-gate 	int		mperm;		/* segment permissions */
8917c478bd9Sstevel@tonic-gate 	Am_ret		amret = AM_NOSUP;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * If padding is required extend both the front and rear of the image.
8957c478bd9Sstevel@tonic-gate 	 * To insure the image itself is mapped at the correct alignment the
8967c478bd9Sstevel@tonic-gate 	 * initial padding is rounded up to the nearest page.  Once the image is
8977c478bd9Sstevel@tonic-gate 	 * mapped the excess can be pruned to the nearest page required for the
8987c478bd9Sstevel@tonic-gate 	 * actual padding itself.
8997c478bd9Sstevel@tonic-gate 	 */
9007c478bd9Sstevel@tonic-gate 	if ((padsize = r_debug.rtd_objpad) != 0) {
9017c478bd9Sstevel@tonic-gate 		padpsize = M_PROUND(padsize);
9027c478bd9Sstevel@tonic-gate 		if (fixed)
9037c478bd9Sstevel@tonic-gate 			padmsize = padpsize;
9047c478bd9Sstevel@tonic-gate 		else
9057c478bd9Sstevel@tonic-gate 			padmsize = S_ROUND(padsize, align);
9067c478bd9Sstevel@tonic-gate 	}
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * Determine the initial permissions used to map in the first segment.
9107c478bd9Sstevel@tonic-gate 	 * If this segments memsz is greater that its filesz then the difference
9117c478bd9Sstevel@tonic-gate 	 * must be zeroed.  Make sure this segment is writable.
9127c478bd9Sstevel@tonic-gate 	 */
9137c478bd9Sstevel@tonic-gate 	mperm = 0;
9147c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_R)
9157c478bd9Sstevel@tonic-gate 		mperm |= PROT_READ;
9167c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_X)
9177c478bd9Sstevel@tonic-gate 		mperm |= PROT_EXEC;
9187c478bd9Sstevel@tonic-gate 	if ((fphdr->p_flags & PF_W) || (fphdr->p_memsz > fphdr->p_filesz))
9197c478bd9Sstevel@tonic-gate 		mperm |= PROT_WRITE;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * Determine whether or not to let system reserve address space based on
9237c478bd9Sstevel@tonic-gate 	 * whether this is a dynamic executable (addresses in object are fixed)
9247c478bd9Sstevel@tonic-gate 	 * or a shared object (addresses in object are relative to the objects'
9257c478bd9Sstevel@tonic-gate 	 * base).
9267c478bd9Sstevel@tonic-gate 	 */
9277c478bd9Sstevel@tonic-gate 	if (fixed) {
9287c478bd9Sstevel@tonic-gate 		/*
9297c478bd9Sstevel@tonic-gate 		 * Determine the reservation address and size, and insure that
9307c478bd9Sstevel@tonic-gate 		 * this reservation isn't already in use.
9317c478bd9Sstevel@tonic-gate 		 */
9327c478bd9Sstevel@tonic-gate 		faddr = maddr = (caddr_t)M_PTRUNC((ulong_t)fphdr->p_vaddr);
9337c478bd9Sstevel@tonic-gate 		raddr = maddr - padpsize;
9347c478bd9Sstevel@tonic-gate 		rsize = fsize + padpsize + padsize;
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 		if (lml_main.lm_head) {
9377c478bd9Sstevel@tonic-gate 			if (elf_map_check(name, raddr, rsize) != 0)
9387c478bd9Sstevel@tonic-gate 				return (0);
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		/*
9427c478bd9Sstevel@tonic-gate 		 * As this is a fixed image, all segments must be individually
9437c478bd9Sstevel@tonic-gate 		 * mapped.
9447c478bd9Sstevel@tonic-gate 		 */
9457c478bd9Sstevel@tonic-gate 		skipfseg = 0;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	} else {
9487c478bd9Sstevel@tonic-gate 		size_t	esize;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 		/*
9517c478bd9Sstevel@tonic-gate 		 * If this isn't a fixed image, reserve enough address space for
9527c478bd9Sstevel@tonic-gate 		 * the entire image to be mapped.  The amount of reservation is
9537c478bd9Sstevel@tonic-gate 		 * the range between the beginning of the first, and end of the
9547c478bd9Sstevel@tonic-gate 		 * last loadable segment, together with any padding, plus the
9557c478bd9Sstevel@tonic-gate 		 * alignment of the first segment.
9567c478bd9Sstevel@tonic-gate 		 *
9577c478bd9Sstevel@tonic-gate 		 * The optimal reservation is made as a no-reserve mapping from
9587c478bd9Sstevel@tonic-gate 		 * anonymous memory.  Each segment is then mapped into this
9597c478bd9Sstevel@tonic-gate 		 * reservation.  If the anonymous mapping capability isn't
9607c478bd9Sstevel@tonic-gate 		 * available, the reservation is obtained from the file itself.
9617c478bd9Sstevel@tonic-gate 		 * In this case the first segment of the image is mapped as part
9627c478bd9Sstevel@tonic-gate 		 * of the reservation, thus only the following segments need to
9637c478bd9Sstevel@tonic-gate 		 * be remapped.
9647c478bd9Sstevel@tonic-gate 		 */
9657c478bd9Sstevel@tonic-gate 		rsize = fsize + padmsize + padsize;
9667c478bd9Sstevel@tonic-gate 		if ((amret = elf_map_reserve(name, &raddr, rsize, mperm,
9677c478bd9Sstevel@tonic-gate 		    fd, align)) == AM_ERROR)
9687c478bd9Sstevel@tonic-gate 			return (0);
9697c478bd9Sstevel@tonic-gate 		maddr = raddr + padmsize;
9707c478bd9Sstevel@tonic-gate 		faddr = (caddr_t)S_ROUND((Off)maddr, align);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		/*
9737c478bd9Sstevel@tonic-gate 		 * If this reservation has been obtained from anonymous memory,
9747c478bd9Sstevel@tonic-gate 		 * then all segments must be individually mapped.  Otherwise,
9757c478bd9Sstevel@tonic-gate 		 * the first segment heads the reservation.
9767c478bd9Sstevel@tonic-gate 		 */
9777c478bd9Sstevel@tonic-gate 		if (amret == AM_OK)
9787c478bd9Sstevel@tonic-gate 			skipfseg = 0;
9797c478bd9Sstevel@tonic-gate 		else
9807c478bd9Sstevel@tonic-gate 			skipfseg = 1;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 		/*
9837c478bd9Sstevel@tonic-gate 		 * For backward compatibility (where MAP_ALIGN isn't available),
9847c478bd9Sstevel@tonic-gate 		 * insure the alignment of the reservation is adequate for this
9857c478bd9Sstevel@tonic-gate 		 * object, and if not remap the object to obtain the correct
9867c478bd9Sstevel@tonic-gate 		 * alignment.
9877c478bd9Sstevel@tonic-gate 		 */
9887c478bd9Sstevel@tonic-gate 		if (faddr != maddr) {
9897c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, rsize);
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 			rsize += align;
9927c478bd9Sstevel@tonic-gate 			if ((amret = elf_map_reserve(name, &raddr, rsize, mperm,
9937c478bd9Sstevel@tonic-gate 			    fd, align)) == AM_ERROR)
9947c478bd9Sstevel@tonic-gate 				return (0);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 			maddr = faddr = (caddr_t)S_ROUND((Off)(raddr +
9977c478bd9Sstevel@tonic-gate 			    padpsize), align);
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 			esize = maddr - raddr + padpsize;
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 			/*
10027c478bd9Sstevel@tonic-gate 			 * As ths image has been realigned, the first segment
10037c478bd9Sstevel@tonic-gate 			 * of the file needs to be remapped to its correct
10047c478bd9Sstevel@tonic-gate 			 * location.
10057c478bd9Sstevel@tonic-gate 			 */
10067c478bd9Sstevel@tonic-gate 			skipfseg = 0;
10077c478bd9Sstevel@tonic-gate 		} else
10087c478bd9Sstevel@tonic-gate 			esize = padmsize - padpsize;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 		/*
10117c478bd9Sstevel@tonic-gate 		 * If this reservation included padding, remove any excess for
10127c478bd9Sstevel@tonic-gate 		 * the start of the image (the padding was adjusted to insure
10137c478bd9Sstevel@tonic-gate 		 * the image was aligned appropriately).
10147c478bd9Sstevel@tonic-gate 		 */
10157c478bd9Sstevel@tonic-gate 		if (esize) {
10167c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, esize);
10177c478bd9Sstevel@tonic-gate 			raddr += esize;
10187c478bd9Sstevel@tonic-gate 			rsize -= esize;
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	/*
10237c478bd9Sstevel@tonic-gate 	 * At this point we know the initial location of the image, and its
10247c478bd9Sstevel@tonic-gate 	 * size.  Pass these back to the caller for inclusion in the link-map
10257c478bd9Sstevel@tonic-gate 	 * that will eventually be created.
10267c478bd9Sstevel@tonic-gate 	 */
10277c478bd9Sstevel@tonic-gate 	*rraddr = raddr;
10287c478bd9Sstevel@tonic-gate 	*rrsize = rsize;
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	/*
10317c478bd9Sstevel@tonic-gate 	 * The first loadable segment is now pointed to by maddr.  This segment
10327c478bd9Sstevel@tonic-gate 	 * will eventually contain the elf header and program headers, so reset
10337c478bd9Sstevel@tonic-gate 	 * the program header.  Pass this  back to the caller for inclusion in
10347c478bd9Sstevel@tonic-gate 	 * the link-map so it can be used for later unmapping operations.
10357c478bd9Sstevel@tonic-gate 	 */
10367c478bd9Sstevel@tonic-gate 	/* LINTED */
10377c478bd9Sstevel@tonic-gate 	*rrphdr = (Phdr *)((char *)maddr + ehdr->e_phoff);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	/*
10407c478bd9Sstevel@tonic-gate 	 * If padding is required at the front of the image, obtain that now.
10417c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
10427c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
10437c478bd9Sstevel@tonic-gate 	 * Otherwise this reservation is backed by the file, or in the case of
10447c478bd9Sstevel@tonic-gate 	 * a fixed image, doesn't yet exist.  Map the padding so that it is
10457c478bd9Sstevel@tonic-gate 	 * suitably protected (PROT_NONE), and insure the first segment of the
10467c478bd9Sstevel@tonic-gate 	 * file is mapped to its correct location.
10477c478bd9Sstevel@tonic-gate 	 */
10487c478bd9Sstevel@tonic-gate 	if (padsize) {
10497c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
10507c478bd9Sstevel@tonic-gate 			if (dz_map(raddr, padpsize, PROT_NONE, (MAP_PRIVATE |
10517c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_NORESERVE)) == MAP_FAILED)
10527c478bd9Sstevel@tonic-gate 				return (0);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 			skipfseg = 0;
10557c478bd9Sstevel@tonic-gate 		}
10567c478bd9Sstevel@tonic-gate 		rsize -= padpsize;
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * Map individual segments.  For a fixed image, these will each be
10617c478bd9Sstevel@tonic-gate 	 * unique mappings.  For a reservation these will fill in the
10627c478bd9Sstevel@tonic-gate 	 * reservation.
10637c478bd9Sstevel@tonic-gate 	 */
10647c478bd9Sstevel@tonic-gate 	for (phdr = fphdr; phdr <= lphdr;
10657c478bd9Sstevel@tonic-gate 	    phdr = (Phdr *)((Off)phdr + ehdr->e_phentsize)) {
10667c478bd9Sstevel@tonic-gate 		caddr_t	addr;
10677c478bd9Sstevel@tonic-gate 		Off	mlen, flen;
10687c478bd9Sstevel@tonic-gate 		size_t	size;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 		/*
10717c478bd9Sstevel@tonic-gate 		 * Skip non-loadable segments or segments that don't occupy
10727c478bd9Sstevel@tonic-gate 		 * any memory.
10737c478bd9Sstevel@tonic-gate 		 */
10747c478bd9Sstevel@tonic-gate 		if (((phdr->p_type != PT_LOAD) &&
10757c478bd9Sstevel@tonic-gate 		    (phdr->p_type != PT_SUNWBSS)) || (phdr->p_memsz == 0))
10767c478bd9Sstevel@tonic-gate 			continue;
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 		/*
10797c478bd9Sstevel@tonic-gate 		 * Establish this segments address relative to our base.
10807c478bd9Sstevel@tonic-gate 		 */
10817c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PTRUNC((ulong_t)(phdr->p_vaddr +
10827c478bd9Sstevel@tonic-gate 		    (fixed ? 0 : faddr)));
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 		/*
10857c478bd9Sstevel@tonic-gate 		 * Determine the mapping protection from the segment attributes.
10867c478bd9Sstevel@tonic-gate 		 * Also determine the etext address from the last loadable
10877c478bd9Sstevel@tonic-gate 		 * segment which has permissions but no write access.
10887c478bd9Sstevel@tonic-gate 		 */
10897c478bd9Sstevel@tonic-gate 		mperm = 0;
10907c478bd9Sstevel@tonic-gate 		if (phdr->p_flags) {
10917c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_R)
10927c478bd9Sstevel@tonic-gate 				mperm |= PROT_READ;
10937c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_X)
10947c478bd9Sstevel@tonic-gate 				mperm |= PROT_EXEC;
10957c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_W)
10967c478bd9Sstevel@tonic-gate 				mperm |= PROT_WRITE;
10977c478bd9Sstevel@tonic-gate 			else
10987c478bd9Sstevel@tonic-gate 				fmap->fm_etext = phdr->p_vaddr + phdr->p_memsz +
10997c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
11007c478bd9Sstevel@tonic-gate 		}
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		/*
11037c478bd9Sstevel@tonic-gate 		 * Determine the type of mapping required.
11047c478bd9Sstevel@tonic-gate 		 */
11057c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_SUNWBSS) {
11067c478bd9Sstevel@tonic-gate 			/*
11077c478bd9Sstevel@tonic-gate 			 * Potentially, we can defer the loading of any SUNWBSS
11087c478bd9Sstevel@tonic-gate 			 * segment, depending on whether the symbols it provides
11097c478bd9Sstevel@tonic-gate 			 * have been bound to.  In this manner, large segments
11107c478bd9Sstevel@tonic-gate 			 * that are interposed upon between shared libraries
11117c478bd9Sstevel@tonic-gate 			 * may not require mapping.  Note, that the mapping
11127c478bd9Sstevel@tonic-gate 			 * information is recorded in our mapping descriptor at
11137c478bd9Sstevel@tonic-gate 			 * this time.
11147c478bd9Sstevel@tonic-gate 			 */
11157c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11167c478bd9Sstevel@tonic-gate 			flen = 0;
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		} else if ((phdr->p_filesz == 0) && (phdr->p_flags == 0)) {
11197c478bd9Sstevel@tonic-gate 			/*
11207c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file and no flags
11217c478bd9Sstevel@tonic-gate 			 * specified, then it defines a reservation.  At this
11227c478bd9Sstevel@tonic-gate 			 * point all standard loadable segments will have been
11237c478bd9Sstevel@tonic-gate 			 * processed.  The segment reservation is mapped
11247c478bd9Sstevel@tonic-gate 			 * directly from /dev/null.
11257c478bd9Sstevel@tonic-gate 			 */
11267c478bd9Sstevel@tonic-gate 			if (nu_map((caddr_t)addr, phdr->p_memsz, PROT_NONE,
11277c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
11287c478bd9Sstevel@tonic-gate 				return (0);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11317c478bd9Sstevel@tonic-gate 			flen = 0;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 		} else if (phdr->p_filesz == 0) {
11347c478bd9Sstevel@tonic-gate 			/*
11357c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file then it defines a
11367c478bd9Sstevel@tonic-gate 			 * nobits segment and is mapped directly from /dev/zero.
11377c478bd9Sstevel@tonic-gate 			 */
11387c478bd9Sstevel@tonic-gate 			if (dz_map((caddr_t)addr, phdr->p_memsz, mperm,
11397c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
11407c478bd9Sstevel@tonic-gate 				return (0);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
11437c478bd9Sstevel@tonic-gate 			flen = 0;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		} else {
11467c478bd9Sstevel@tonic-gate 			Off	foff;
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 			/*
11497c478bd9Sstevel@tonic-gate 			 * This mapping originates from the file.  Determine the
11507c478bd9Sstevel@tonic-gate 			 * file offset to which the mapping will be directed
11517c478bd9Sstevel@tonic-gate 			 * (must be aligned) and how much to map (might be more
11527c478bd9Sstevel@tonic-gate 			 * than the file in the case of .bss).
11537c478bd9Sstevel@tonic-gate 			 */
11547c478bd9Sstevel@tonic-gate 			foff = M_PTRUNC((ulong_t)phdr->p_offset);
11557c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz + (phdr->p_offset - foff);
11567c478bd9Sstevel@tonic-gate 			flen = phdr->p_filesz + (phdr->p_offset - foff);
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 			/*
11597c478bd9Sstevel@tonic-gate 			 * If this is a non-fixed, non-anonymous mapping, and no
11607c478bd9Sstevel@tonic-gate 			 * padding is involved, then the first loadable segment
11617c478bd9Sstevel@tonic-gate 			 * is already part of the initial reservation.  In this
11627c478bd9Sstevel@tonic-gate 			 * case there is no need to remap this segment.
11637c478bd9Sstevel@tonic-gate 			 */
11647c478bd9Sstevel@tonic-gate 			if ((skipfseg == 0) || (phdr != fphdr)) {
11657c478bd9Sstevel@tonic-gate 				int phdr_mperm = mperm;
11667c478bd9Sstevel@tonic-gate 				/*
11677c478bd9Sstevel@tonic-gate 				 * If this segments memsz is greater that its
11687c478bd9Sstevel@tonic-gate 				 * filesz then the difference must be zeroed.
11697c478bd9Sstevel@tonic-gate 				 * Make sure this segment is writable.
11707c478bd9Sstevel@tonic-gate 				 */
11717c478bd9Sstevel@tonic-gate 				if (phdr->p_memsz > phdr->p_filesz)
11727c478bd9Sstevel@tonic-gate 					mperm |= PROT_WRITE;
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 				if (elf_map_textdata((caddr_t)addr, flen,
11757c478bd9Sstevel@tonic-gate 				    mperm, phdr_mperm,
11767c478bd9Sstevel@tonic-gate 				    (MAP_FIXED | MAP_PRIVATE), fd, foff) ==
11777c478bd9Sstevel@tonic-gate 				    MAP_FAILED) {
11787c478bd9Sstevel@tonic-gate 					int	err = errno;
11797c478bd9Sstevel@tonic-gate 					eprintf(ERR_FATAL,
11807c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYS_MMAP), name,
11817c478bd9Sstevel@tonic-gate 					    strerror(err));
11827c478bd9Sstevel@tonic-gate 					return (0);
11837c478bd9Sstevel@tonic-gate 				}
11847c478bd9Sstevel@tonic-gate 			}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 			/*
11877c478bd9Sstevel@tonic-gate 			 * If the memory occupancy of the segment overflows the
11887c478bd9Sstevel@tonic-gate 			 * definition in the file, we need to "zero out" the end
11897c478bd9Sstevel@tonic-gate 			 * of the mapping we've established, and if necessary,
11907c478bd9Sstevel@tonic-gate 			 * map some more space from /dev/zero.  Note, zero'ed
11917c478bd9Sstevel@tonic-gate 			 * memory must end on a double word boundary to satisfy
11927c478bd9Sstevel@tonic-gate 			 * zero().
11937c478bd9Sstevel@tonic-gate 			 */
11947c478bd9Sstevel@tonic-gate 			if (phdr->p_memsz > phdr->p_filesz) {
11957c478bd9Sstevel@tonic-gate 				caddr_t	zaddr;
11967c478bd9Sstevel@tonic-gate 				size_t	zlen, zplen;
11977c478bd9Sstevel@tonic-gate 				Off	fend;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 				foff = (Off)(phdr->p_vaddr + phdr->p_filesz +
12007c478bd9Sstevel@tonic-gate 				    (fixed ? 0 : faddr));
12017c478bd9Sstevel@tonic-gate 				zaddr = (caddr_t)M_PROUND(foff);
12027c478bd9Sstevel@tonic-gate 				zplen = (size_t)(zaddr - foff);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 				fend = (Off)S_DROUND((size_t)(phdr->p_vaddr +
12057c478bd9Sstevel@tonic-gate 				    phdr->p_memsz + (fixed ? 0 : faddr)));
12067c478bd9Sstevel@tonic-gate 				zlen = (size_t)(fend - foff);
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 				/*
12097c478bd9Sstevel@tonic-gate 				 * Determine whether the number of bytes that
12107c478bd9Sstevel@tonic-gate 				 * must be zero'ed overflow to the next page.
12117c478bd9Sstevel@tonic-gate 				 * If not, simply clear the exact bytes
12127c478bd9Sstevel@tonic-gate 				 * (filesz to memsz) from this page.  Otherwise,
12137c478bd9Sstevel@tonic-gate 				 * clear the remaining bytes of this page, and
12147c478bd9Sstevel@tonic-gate 				 * map an following pages from /dev/zero.
12157c478bd9Sstevel@tonic-gate 				 */
12167c478bd9Sstevel@tonic-gate 				if (zlen < zplen)
12177c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zlen);
12187c478bd9Sstevel@tonic-gate 				else {
12197c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zplen);
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 					if ((zlen = (fend - (Off)zaddr)) > 0) {
12227c478bd9Sstevel@tonic-gate 						if (dz_map(zaddr, zlen, mperm,
12237c478bd9Sstevel@tonic-gate 						    MAP_FIXED | MAP_PRIVATE) ==
12247c478bd9Sstevel@tonic-gate 						    MAP_FAILED)
12257c478bd9Sstevel@tonic-gate 							return (0);
12267c478bd9Sstevel@tonic-gate 					}
12277c478bd9Sstevel@tonic-gate 				}
12287c478bd9Sstevel@tonic-gate 			}
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 		/*
12327c478bd9Sstevel@tonic-gate 		 * Unmap anything from the last mapping address to this one and
12337c478bd9Sstevel@tonic-gate 		 * update the mapping claim pointer.
12347c478bd9Sstevel@tonic-gate 		 */
12357c478bd9Sstevel@tonic-gate 		if ((fixed == 0) && ((size = addr - maddr) != 0)) {
12367c478bd9Sstevel@tonic-gate 			(void) munmap(maddr, size);
12377c478bd9Sstevel@tonic-gate 			rsize -= size;
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 
12407c478bd9Sstevel@tonic-gate 		/*
12417c478bd9Sstevel@tonic-gate 		 * Retain this segments mapping information.
12427c478bd9Sstevel@tonic-gate 		 */
12437c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_vaddr = addr;
12447c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_msize = mlen;
12457c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_fsize = flen;
12467c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_perm = mperm;
12477c478bd9Sstevel@tonic-gate 		(*mmapcnt)++;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 		maddr = addr + M_PROUND(mlen);
12507c478bd9Sstevel@tonic-gate 		rsize -= M_PROUND(mlen);
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	/*
12547c478bd9Sstevel@tonic-gate 	 * If padding is required at the end of the image, obtain that now.
12557c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
12567c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
12577c478bd9Sstevel@tonic-gate 	 */
12587c478bd9Sstevel@tonic-gate 	if (padsize) {
12597c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
12607c478bd9Sstevel@tonic-gate 			/*
12617c478bd9Sstevel@tonic-gate 			 * maddr is currently page aligned from the last segment
12627c478bd9Sstevel@tonic-gate 			 * mapping.
12637c478bd9Sstevel@tonic-gate 			 */
12647c478bd9Sstevel@tonic-gate 			if (dz_map(maddr, padsize, PROT_NONE, (MAP_PRIVATE |
12657c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_NORESERVE)) == MAP_FAILED)
12667c478bd9Sstevel@tonic-gate 				return (0);
12677c478bd9Sstevel@tonic-gate 		}
12687c478bd9Sstevel@tonic-gate 		maddr += padsize;
12697c478bd9Sstevel@tonic-gate 		rsize -= padsize;
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	/*
12737c478bd9Sstevel@tonic-gate 	 * Unmap any final reservation.
12747c478bd9Sstevel@tonic-gate 	 */
12757c478bd9Sstevel@tonic-gate 	if ((fixed == 0) && (rsize != 0))
12767c478bd9Sstevel@tonic-gate 		(void) munmap(maddr, rsize);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	return (faddr);
12797c478bd9Sstevel@tonic-gate }
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate /*
12827c478bd9Sstevel@tonic-gate  * A null symbol interpretor.  Used if a filter has no associated filtees.
12837c478bd9Sstevel@tonic-gate  */
12847c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
12857c478bd9Sstevel@tonic-gate static Sym *
12867c478bd9Sstevel@tonic-gate elf_null_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
12877c478bd9Sstevel@tonic-gate {
12887c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
12897c478bd9Sstevel@tonic-gate }
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate /*
12927c478bd9Sstevel@tonic-gate  * Disable filtee use.
12937c478bd9Sstevel@tonic-gate  */
12947c478bd9Sstevel@tonic-gate static void
12957c478bd9Sstevel@tonic-gate elf_disable_filtee(Rt_map * lmp, Dyninfo * dip)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	dip->di_info = 0;
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) {
13007c478bd9Sstevel@tonic-gate 		/*
13017c478bd9Sstevel@tonic-gate 		 * If this is an object filter, free the filtee's duplication.
13027c478bd9Sstevel@tonic-gate 		 */
13037c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(lmp) != FLTR_DISABLED) {
13047c478bd9Sstevel@tonic-gate 			free(REFNAME(lmp));
13057c478bd9Sstevel@tonic-gate 			REFNAME(lmp) = (char *)0;
13067c478bd9Sstevel@tonic-gate 			OBJFLTRNDX(lmp) = FLTR_DISABLED;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 			/*
13097c478bd9Sstevel@tonic-gate 			 * Indicate that this filtee is no longer available.
13107c478bd9Sstevel@tonic-gate 			 */
13117c478bd9Sstevel@tonic-gate 			if (dip->di_flags & FLG_DI_STDFLTR)
13127c478bd9Sstevel@tonic-gate 				SYMINTP(lmp) = elf_null_find_sym;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 		}
13157c478bd9Sstevel@tonic-gate 	} else if (dip->di_flags & FLG_DI_STDFLTR) {
13167c478bd9Sstevel@tonic-gate 		/*
13177c478bd9Sstevel@tonic-gate 		 * Indicate that this standard filtee is no longer available.
13187c478bd9Sstevel@tonic-gate 		 */
13197c478bd9Sstevel@tonic-gate 		if (SYMSFLTRCNT(lmp))
13207c478bd9Sstevel@tonic-gate 			SYMSFLTRCNT(lmp)--;
13217c478bd9Sstevel@tonic-gate 	} else {
13227c478bd9Sstevel@tonic-gate 		/*
13237c478bd9Sstevel@tonic-gate 		 * Indicate that this auxiliary filtee is no longer available.
13247c478bd9Sstevel@tonic-gate 		 */
13257c478bd9Sstevel@tonic-gate 		if (SYMAFLTRCNT(lmp))
13267c478bd9Sstevel@tonic-gate 			SYMAFLTRCNT(lmp)--;
13277c478bd9Sstevel@tonic-gate 	}
13287c478bd9Sstevel@tonic-gate 	dip->di_flags &= ~MSK_DI_FILTER;
13297c478bd9Sstevel@tonic-gate }
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate /*
13327c478bd9Sstevel@tonic-gate  * Find symbol interpreter - filters.
13337c478bd9Sstevel@tonic-gate  * This function is called when the symbols from a shared object should
13347c478bd9Sstevel@tonic-gate  * be resolved from the shared objects filtees instead of from within itself.
13357c478bd9Sstevel@tonic-gate  *
13367c478bd9Sstevel@tonic-gate  * A symbol name of 0 is used to trigger filtee loading.
13377c478bd9Sstevel@tonic-gate  */
13387c478bd9Sstevel@tonic-gate static Sym *
13397c478bd9Sstevel@tonic-gate _elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
13407c478bd9Sstevel@tonic-gate {
13417c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name, *filtees;
13427c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
13437c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
13447c478bd9Sstevel@tonic-gate 	Pnode		*pnp, **pnpp;
13457c478bd9Sstevel@tonic-gate 	int		any;
13467c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(ilmp)[ndx];
13477c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(ilmp);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	/*
13507c478bd9Sstevel@tonic-gate 	 * Indicate that the filter has been used.  If a binding already exists
13517c478bd9Sstevel@tonic-gate 	 * to the caller, indicate that this object is referenced.  This insures
13527c478bd9Sstevel@tonic-gate 	 * we don't generate false unreferenced diagnostics from ldd -u/U or
13537c478bd9Sstevel@tonic-gate 	 * debugging.  Don't create a binding regardless, as this filter may
13547c478bd9Sstevel@tonic-gate 	 * have been dlopen()'ed.
13557c478bd9Sstevel@tonic-gate 	 */
13567c478bd9Sstevel@tonic-gate 	if (name && (ilmp != clmp)) {
13577c478bd9Sstevel@tonic-gate 		Word	tracing = (LIST(clmp)->lm_flags &
13587c478bd9Sstevel@tonic-gate 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED));
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		if (tracing || dbg_mask) {
13617c478bd9Sstevel@tonic-gate 			Bnd_desc **	bdpp;
13627c478bd9Sstevel@tonic-gate 			Aliste		off;
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 			FLAGS1(ilmp) |= FL1_RT_USED;
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 			if ((tracing & LML_FLG_TRC_UNREF) || dbg_mask) {
13677c478bd9Sstevel@tonic-gate 				for (ALIST_TRAVERSE(CALLERS(ilmp), off, bdpp)) {
13687c478bd9Sstevel@tonic-gate 					Bnd_desc *	bdp = *bdpp;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 					if (bdp->b_caller == clmp) {
13717c478bd9Sstevel@tonic-gate 						bdp->b_flags |= BND_REFER;
13727c478bd9Sstevel@tonic-gate 						break;
13737c478bd9Sstevel@tonic-gate 					}
13747c478bd9Sstevel@tonic-gate 				}
13757c478bd9Sstevel@tonic-gate 			}
13767c478bd9Sstevel@tonic-gate 		}
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	/*
13807c478bd9Sstevel@tonic-gate 	 * If this is the first call to process this filter, establish the
13817c478bd9Sstevel@tonic-gate 	 * filtee list.  If a configuration file exists, determine if any
13827c478bd9Sstevel@tonic-gate 	 * filtee associations for this filter, and its filtee reference, are
13837c478bd9Sstevel@tonic-gate 	 * defined.  Otherwise, process the filtee reference.  Any token
13847c478bd9Sstevel@tonic-gate 	 * expansion is also completed at this point (i.e., $PLATFORM).
13857c478bd9Sstevel@tonic-gate 	 */
13867c478bd9Sstevel@tonic-gate 	filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val;
13877c478bd9Sstevel@tonic-gate 	if (dip->di_info == 0) {
13887c478bd9Sstevel@tonic-gate 		if (rtld_flags2 & RT_FL2_FLTCFG)
13897c478bd9Sstevel@tonic-gate 			dip->di_info = elf_config_flt(PATHNAME(ilmp), filtees);
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 		if (dip->di_info == 0) {
13927c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_file_filter(NAME(ilmp), filtees, 0));
13937c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags &
13947c478bd9Sstevel@tonic-gate 			    (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
13957c478bd9Sstevel@tonic-gate 			    ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0))
13967c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FILTER),
13977c478bd9Sstevel@tonic-gate 				    NAME(ilmp), filtees);
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 			if ((dip->di_info = (void *)expand_paths(ilmp,
14007c478bd9Sstevel@tonic-gate 			    filtees, PN_SER_FILTEE, 0)) == 0) {
14017c478bd9Sstevel@tonic-gate 				elf_disable_filtee(ilmp, dip);
14027c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
14037c478bd9Sstevel@tonic-gate 			}
14047c478bd9Sstevel@tonic-gate 		}
14057c478bd9Sstevel@tonic-gate 	}
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	/*
14087c478bd9Sstevel@tonic-gate 	 * Traverse the filtee list, dlopen()'ing any objects specified and
14097c478bd9Sstevel@tonic-gate 	 * using their group handle to lookup the symbol.
14107c478bd9Sstevel@tonic-gate 	 */
14117c478bd9Sstevel@tonic-gate 	for (any = 0, pnpp = (Pnode **)&(dip->di_info), pnp = *pnpp; pnp;
14127c478bd9Sstevel@tonic-gate 	    pnpp = &pnp->p_next, pnp = * pnpp) {
14137c478bd9Sstevel@tonic-gate 		int	mode;
14147c478bd9Sstevel@tonic-gate 		Grp_hdl	*ghp;
14157c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
14187c478bd9Sstevel@tonic-gate 			continue;
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 		/*
14217c478bd9Sstevel@tonic-gate 		 * Establish the mode of the filtee from the filter.  As filtees
14227c478bd9Sstevel@tonic-gate 		 * are loaded via a dlopen(), make sure that RTLD_GROUP is set
14237c478bd9Sstevel@tonic-gate 		 * and the filtees aren't global.  It would be nice to have
14247c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST used here also, but as filters got out long before
14257c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST was introduced it's a little too late now.
14267c478bd9Sstevel@tonic-gate 		 */
14277c478bd9Sstevel@tonic-gate 		mode = MODE(ilmp) | RTLD_GROUP;
14287c478bd9Sstevel@tonic-gate 		mode &= ~RTLD_GLOBAL;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 		/*
14317c478bd9Sstevel@tonic-gate 		 * Insure that any auxiliary filter can locate symbols from its
14327c478bd9Sstevel@tonic-gate 		 * caller.
14337c478bd9Sstevel@tonic-gate 		 */
14347c478bd9Sstevel@tonic-gate 		if (dip->di_flags & FLG_DI_AUXFLTR)
14357c478bd9Sstevel@tonic-gate 			mode |= RTLD_PARENT;
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate 		/*
14387c478bd9Sstevel@tonic-gate 		 * Process any hardware capability directory.  Establish a new
14397c478bd9Sstevel@tonic-gate 		 * link-map control list from which to analyze any newly added
144024a6229eSrie 		 * objects.
14417c478bd9Sstevel@tonic-gate 		 */
14427c478bd9Sstevel@tonic-gate 		if ((pnp->p_info == 0) && (pnp->p_orig & PN_TKN_HWCAP)) {
1443*11a2bb38Srie 			Lm_cntl	*lmc;
1444*11a2bb38Srie 			Aliste	lmco;
1445*11a2bb38Srie 
144624a6229eSrie 			if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
144724a6229eSrie 				if ((lmc = alist_append(&(lml->lm_lists), 0,
144824a6229eSrie 				    sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0)
144924a6229eSrie 					return ((Sym *)0);
14507c478bd9Sstevel@tonic-gate 				lmco = (Aliste)((char *)lmc -
14517c478bd9Sstevel@tonic-gate 				    (char *)lml->lm_lists);
145224a6229eSrie 			} else {
145324a6229eSrie 				lmc = 0;
14547c478bd9Sstevel@tonic-gate 				lmco = ALO_DATA;
145524a6229eSrie 			}
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 			pnp = hwcap_filtees(pnpp, lmco, dip, ilmp, filtees,
14587c478bd9Sstevel@tonic-gate 			    mode, (FLG_RT_HANDLE | FLG_RT_HWCAP));
1459*11a2bb38Srie 
1460*11a2bb38Srie 			/*
1461*11a2bb38Srie 			 * Now that any hardware capability objects have been
1462*11a2bb38Srie 			 * processed, remove any link-map control list.
1463*11a2bb38Srie 			 */
1464*11a2bb38Srie 			if (lmc) {
1465*11a2bb38Srie 				if (pnp->p_len == 0)
1466*11a2bb38Srie 					(void) lm_salvage(lml, 0, lmco);
1467*11a2bb38Srie 				remove_cntl(lml, lmco);
1468*11a2bb38Srie 			}
14697c478bd9Sstevel@tonic-gate 		}
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
14727c478bd9Sstevel@tonic-gate 			continue;
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 		/*
14757c478bd9Sstevel@tonic-gate 		 * Process an individual filtee.
14767c478bd9Sstevel@tonic-gate 		 */
14777c478bd9Sstevel@tonic-gate 		if (pnp->p_info == 0) {
14787c478bd9Sstevel@tonic-gate 			const char	*filtee = pnp->p_name;
14797c478bd9Sstevel@tonic-gate 			int		audit = 0;
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_file_filtee(NAME(ilmp), filtee, 0));
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 			ghp = 0;
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 			/*
14867c478bd9Sstevel@tonic-gate 			 * Determine if the reference link map is already
14877c478bd9Sstevel@tonic-gate 			 * loaded.  As an optimization compare the filtee with
14887c478bd9Sstevel@tonic-gate 			 * our interpretor.  The most common filter is
14897c478bd9Sstevel@tonic-gate 			 * libdl.so.1, which is a filter on ld.so.1.
14907c478bd9Sstevel@tonic-gate 			 */
14917c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
14927c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) {
14937c478bd9Sstevel@tonic-gate #else
14947c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) {
14957c478bd9Sstevel@tonic-gate #endif
14967c478bd9Sstevel@tonic-gate 				/*
14977c478bd9Sstevel@tonic-gate 				 * Create an association between ld.so.1 and
14987c478bd9Sstevel@tonic-gate 				 * the filter.
14997c478bd9Sstevel@tonic-gate 				 */
15007c478bd9Sstevel@tonic-gate 				nlmp = lml_rtld.lm_head;
15017c478bd9Sstevel@tonic-gate 				if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp,
15027c478bd9Sstevel@tonic-gate 				    (GPH_LDSO | GPH_FIRST | GPH_FILTEE))) == 0)
15037c478bd9Sstevel@tonic-gate 					nlmp = 0;
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 				/*
15067c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
15077c478bd9Sstevel@tonic-gate 				 * recursion.
15087c478bd9Sstevel@tonic-gate 				 */
15097c478bd9Sstevel@tonic-gate 				if (nlmp && ghp)
15107c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 				/*
15137c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  Ignore
15147c478bd9Sstevel@tonic-gate 				 * any return from the auditor, as we can't
15157c478bd9Sstevel@tonic-gate 				 * allow ignore filtering to ld.so.1, otherwise
15167c478bd9Sstevel@tonic-gate 				 * nothing is going to work.
15177c478bd9Sstevel@tonic-gate 				 */
15187c478bd9Sstevel@tonic-gate 				if ((lml->lm_tflags | FLAGS1(ilmp)) &
15197c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)
15207c478bd9Sstevel@tonic-gate 					(void) audit_objfilter(ilmp, filtees,
15217c478bd9Sstevel@tonic-gate 					    nlmp, 0);
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 			} else {
15247c478bd9Sstevel@tonic-gate 				Rej_desc	rej = { 0 };
1525*11a2bb38Srie 				Lm_cntl		*lmc;
1526*11a2bb38Srie 				Aliste		lmco;
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 				/*
15297c478bd9Sstevel@tonic-gate 				 * Establish a new link-map control list from
15307c478bd9Sstevel@tonic-gate 				 * which to analyze any newly added objects.
15317c478bd9Sstevel@tonic-gate 				 */
153224a6229eSrie 				if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
153324a6229eSrie 					if ((lmc =
153424a6229eSrie 					    alist_append(&(lml->lm_lists), 0,
153524a6229eSrie 					    sizeof (Lm_cntl),
153624a6229eSrie 					    AL_CNT_LMLISTS)) == 0)
153724a6229eSrie 						return ((Sym *)0);
15387c478bd9Sstevel@tonic-gate 					lmco = (Aliste)((char *)lmc -
15397c478bd9Sstevel@tonic-gate 					    (char *)lml->lm_lists);
154024a6229eSrie 				} else {
154124a6229eSrie 					lmc = 0;
15427c478bd9Sstevel@tonic-gate 					lmco = ALO_DATA;
154324a6229eSrie 				}
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 				/*
15467c478bd9Sstevel@tonic-gate 				 * Load the filtee.
15477c478bd9Sstevel@tonic-gate 				 */
15487c478bd9Sstevel@tonic-gate 				if ((nlmp = load_path(lml, lmco, filtee, ilmp,
15497c478bd9Sstevel@tonic-gate 				    mode, FLG_RT_HANDLE, &ghp, 0, &rej)) == 0) {
15507c478bd9Sstevel@tonic-gate 					file_notfound(LIST(ilmp), filtee, ilmp,
15517c478bd9Sstevel@tonic-gate 					    FLG_RT_HANDLE, &rej);
15527c478bd9Sstevel@tonic-gate 					remove_rej(&rej);
15537c478bd9Sstevel@tonic-gate 				}
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 				/*
15567c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
15577c478bd9Sstevel@tonic-gate 				 * recursion.
15587c478bd9Sstevel@tonic-gate 				 */
15597c478bd9Sstevel@tonic-gate 				if (nlmp && ghp) {
15607c478bd9Sstevel@tonic-gate 					ghp->gh_flags |= GPH_FILTEE;
15617c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
15627c478bd9Sstevel@tonic-gate 				}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 				/*
15657c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  A
15667c478bd9Sstevel@tonic-gate 				 * return of 0 indicates the auditor wishes to
15677c478bd9Sstevel@tonic-gate 				 * ignore this filtee.
15687c478bd9Sstevel@tonic-gate 				 */
15697c478bd9Sstevel@tonic-gate 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
15707c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)) {
15717c478bd9Sstevel@tonic-gate 					if (audit_objfilter(ilmp, filtees,
15727c478bd9Sstevel@tonic-gate 					    nlmp, 0) == 0) {
15737c478bd9Sstevel@tonic-gate 						audit = 1;
15747c478bd9Sstevel@tonic-gate 						nlmp = 0;
15757c478bd9Sstevel@tonic-gate 					}
15767c478bd9Sstevel@tonic-gate 				}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 				/*
15797c478bd9Sstevel@tonic-gate 				 * Finish processing the objects associated with
15807c478bd9Sstevel@tonic-gate 				 * this request.  Create an association between
15817c478bd9Sstevel@tonic-gate 				 * this object and the originating filter to
15827c478bd9Sstevel@tonic-gate 				 * provide sufficient information to tear down
15837c478bd9Sstevel@tonic-gate 				 * this filtee if necessary.
15847c478bd9Sstevel@tonic-gate 				 */
15857c478bd9Sstevel@tonic-gate 				if (nlmp && ghp &&
15867c478bd9Sstevel@tonic-gate 				    ((analyze_lmc(lml, lmco, nlmp) == 0) ||
15877c478bd9Sstevel@tonic-gate 				    (relocate_lmc(lml, lmco, nlmp) == 0)))
15887c478bd9Sstevel@tonic-gate 					nlmp = 0;
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 				/*
15917c478bd9Sstevel@tonic-gate 				 * If the filtee has been successfully
15927c478bd9Sstevel@tonic-gate 				 * processed, and it is part of a link-map
15937c478bd9Sstevel@tonic-gate 				 * control list that is equivalent, or less,
15947c478bd9Sstevel@tonic-gate 				 * than the filter control list, create an
15957c478bd9Sstevel@tonic-gate 				 * association between the filter and filtee.
15967c478bd9Sstevel@tonic-gate 				 * This association provides sufficient
15977c478bd9Sstevel@tonic-gate 				 * information to tear down the filter and
15987c478bd9Sstevel@tonic-gate 				 * filtee if necessary.
15997c478bd9Sstevel@tonic-gate 				 */
16007c478bd9Sstevel@tonic-gate 				if (nlmp && ghp && (CNTL(nlmp) <= CNTL(ilmp)) &&
16017c478bd9Sstevel@tonic-gate 				    (hdl_add(ghp, ilmp, GPD_FILTER) == 0))
16027c478bd9Sstevel@tonic-gate 					nlmp = 0;
1603*11a2bb38Srie 
1604*11a2bb38Srie 				/*
1605*11a2bb38Srie 				 * Now that this object has been processed,
1606*11a2bb38Srie 				 * remove any link-map control list.
1607*11a2bb38Srie 				 */
1608*11a2bb38Srie 				if (lmc) {
1609*11a2bb38Srie 					if (nlmp == 0)
1610*11a2bb38Srie 						(void) lm_salvage(lml, 0, lmco);
1611*11a2bb38Srie 					remove_cntl(lml, lmco);
1612*11a2bb38Srie 				}
16137c478bd9Sstevel@tonic-gate 			}
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 			/*
16167c478bd9Sstevel@tonic-gate 			 * Generate a diagnostic if the filtee couldn't be
16177c478bd9Sstevel@tonic-gate 			 * loaded, null out the pnode entry, and continue
16187c478bd9Sstevel@tonic-gate 			 * the search.  Otherwise, retain this group handle
16197c478bd9Sstevel@tonic-gate 			 * for future symbol searches.
16207c478bd9Sstevel@tonic-gate 			 */
16217c478bd9Sstevel@tonic-gate 			if (nlmp == 0) {
16227c478bd9Sstevel@tonic-gate 				pnp->p_info = 0;
16237c478bd9Sstevel@tonic-gate 				DBG_CALL(Dbg_file_filtee(0, filtee, audit));
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate 				if (ghp)
16267c478bd9Sstevel@tonic-gate 					(void) dlclose_core(ghp, ilmp);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate 				pnp->p_len = 0;
16297c478bd9Sstevel@tonic-gate 				continue;
16307c478bd9Sstevel@tonic-gate 			}
16317c478bd9Sstevel@tonic-gate 		}
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 		ghp = (Grp_hdl *)pnp->p_info;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 		/*
16367c478bd9Sstevel@tonic-gate 		 * If we're just here to trigger filtee loading skip the symbol
16377c478bd9Sstevel@tonic-gate 		 * lookup so we'll continue looking for additional filtees.
16387c478bd9Sstevel@tonic-gate 		 */
16397c478bd9Sstevel@tonic-gate 		if (name) {
16407c478bd9Sstevel@tonic-gate 			Grp_desc	*gdp;
16417c478bd9Sstevel@tonic-gate 			Sym		*sym = 0;
16427c478bd9Sstevel@tonic-gate 			Aliste		off;
16437c478bd9Sstevel@tonic-gate 			Slookup		sl = *slp;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 			sl.sl_flags |= LKUP_FIRST;
16467c478bd9Sstevel@tonic-gate 			any++;
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 			/*
16497c478bd9Sstevel@tonic-gate 			 * Look for the symbol in the handles dependencies.
16507c478bd9Sstevel@tonic-gate 			 */
16517c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) {
16527c478bd9Sstevel@tonic-gate 				if ((gdp->gd_flags & GPD_AVAIL) == 0)
16537c478bd9Sstevel@tonic-gate 					continue;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 				/*
16567c478bd9Sstevel@tonic-gate 				 * If our parent is a dependency don't look at
16577c478bd9Sstevel@tonic-gate 				 * it (otherwise we are in a recursive loop).
16587c478bd9Sstevel@tonic-gate 				 * This situation can occur with auxiliary
16597c478bd9Sstevel@tonic-gate 				 * filters if the filtee has a dependency on the
16607c478bd9Sstevel@tonic-gate 				 * filter.  This dependency isn't necessary as
16617c478bd9Sstevel@tonic-gate 				 * auxiliary filters are opened RTLD_PARENT, but
16627c478bd9Sstevel@tonic-gate 				 * users may still unknowingly add an explicit
16637c478bd9Sstevel@tonic-gate 				 * dependency to the parent.
16647c478bd9Sstevel@tonic-gate 				 */
16657c478bd9Sstevel@tonic-gate 				if ((sl.sl_imap = gdp->gd_depend) == ilmp)
16667c478bd9Sstevel@tonic-gate 					continue;
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 				if (((sym = SYMINTP(sl.sl_imap)(&sl, dlmp,
16697c478bd9Sstevel@tonic-gate 				    binfo)) != 0) ||
16707c478bd9Sstevel@tonic-gate 				    (ghp->gh_flags & GPH_FIRST))
16717c478bd9Sstevel@tonic-gate 					break;
16727c478bd9Sstevel@tonic-gate 			}
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 			/*
16757c478bd9Sstevel@tonic-gate 			 * If this filtee has just been loaded (nlmp != 0),
16767c478bd9Sstevel@tonic-gate 			 * determine whether the filtee was triggered by a
16777c478bd9Sstevel@tonic-gate 			 * relocation from an object that is still being
16787c478bd9Sstevel@tonic-gate 			 * relocated on a leaf link-map control list.  As the
16797c478bd9Sstevel@tonic-gate 			 * relocation of an object on this list might still
16807c478bd9Sstevel@tonic-gate 			 * fail, we can't yet bind the filter to the filtee.
16817c478bd9Sstevel@tonic-gate 			 * To do so, would be locking the filtee so that it
16827c478bd9Sstevel@tonic-gate 			 * couldn't be deleted, and the filtee itself could have
16837c478bd9Sstevel@tonic-gate 			 * bound to an object that must be torn down.  Insure
16847c478bd9Sstevel@tonic-gate 			 * the caller isn't bound to the handle at this time.
16857c478bd9Sstevel@tonic-gate 			 * Any association will be reestablished when the filter
16867c478bd9Sstevel@tonic-gate 			 * is later referenced and the filtee has propagated to
16877c478bd9Sstevel@tonic-gate 			 * the same link-map control list.
16887c478bd9Sstevel@tonic-gate 			 */
16897c478bd9Sstevel@tonic-gate 			if (nlmp && (CNTL(nlmp) > CNTL(ilmp))) {
16907c478bd9Sstevel@tonic-gate 				remove_caller(ghp, ilmp);
16917c478bd9Sstevel@tonic-gate 				pnp->p_info = 0;
16927c478bd9Sstevel@tonic-gate 			}
16937c478bd9Sstevel@tonic-gate 			if (sym) {
16947c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_FILTEE;
16957c478bd9Sstevel@tonic-gate 				return (sym);
16967c478bd9Sstevel@tonic-gate 			}
16977c478bd9Sstevel@tonic-gate 		}
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 		/*
17007c478bd9Sstevel@tonic-gate 		 * If this object is tagged to terminate filtee processing we're
17017c478bd9Sstevel@tonic-gate 		 * done.
17027c478bd9Sstevel@tonic-gate 		 */
17037c478bd9Sstevel@tonic-gate 		if (FLAGS1(ghp->gh_owner) & FL1_RT_ENDFILTE)
17047c478bd9Sstevel@tonic-gate 			break;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	/*
17087c478bd9Sstevel@tonic-gate 	 * If we're just here to trigger filtee loading then we're done.
17097c478bd9Sstevel@tonic-gate 	 */
17107c478bd9Sstevel@tonic-gate 	if (name == 0)
17117c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 	/*
17147c478bd9Sstevel@tonic-gate 	 * If no filtees have been found for a filter, clean up any Pnode
17157c478bd9Sstevel@tonic-gate 	 * structures and disable their search completely.  For auxiliary
17167c478bd9Sstevel@tonic-gate 	 * filters we can reselect the symbol search function so that we never
17177c478bd9Sstevel@tonic-gate 	 * enter this routine again for this object.  For standard filters we
17187c478bd9Sstevel@tonic-gate 	 * use the null symbol routine.
17197c478bd9Sstevel@tonic-gate 	 */
17207c478bd9Sstevel@tonic-gate 	if (any == 0) {
17217c478bd9Sstevel@tonic-gate 		remove_pnode((Pnode *)dip->di_info);
17227c478bd9Sstevel@tonic-gate 		elf_disable_filtee(ilmp, dip);
17237c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17247c478bd9Sstevel@tonic-gate 	}
17257c478bd9Sstevel@tonic-gate 
17267c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate /*
17307c478bd9Sstevel@tonic-gate  * Focal point for disabling error messages for auxiliary filters.  As an
17317c478bd9Sstevel@tonic-gate  * auxiliary filter allows for filtee use, but provides a fallback should a
17327c478bd9Sstevel@tonic-gate  * filtee not exist (or fail to load), any errors generated as a consequence of
17337c478bd9Sstevel@tonic-gate  * trying to load the filtees are typically suppressed.  Setting RT_FL_SILENCERR
17347c478bd9Sstevel@tonic-gate  * suppresses errors generated by eprint(), but insures a debug diagnostic is
17357c478bd9Sstevel@tonic-gate  * produced.  ldd(1) employs printf(), and here, the selection of whether to
17367c478bd9Sstevel@tonic-gate  * print a diagnostic in regards to auxiliary filters is a little more complex.
17377c478bd9Sstevel@tonic-gate  *
17387c478bd9Sstevel@tonic-gate  *   .	The determination of whether to produce an ldd message, or a fatal
17397c478bd9Sstevel@tonic-gate  *	error message is driven by LML_FLG_TRC_ENABLE.
17407c478bd9Sstevel@tonic-gate  *   .	More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN,
17417c478bd9Sstevel@tonic-gate  *	(ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s),
17427c478bd9Sstevel@tonic-gate  *	and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u).
17437c478bd9Sstevel@tonic-gate  *
17447c478bd9Sstevel@tonic-gate  *   .	If the calling object is lddstub, then several classes of message are
17457c478bd9Sstevel@tonic-gate  *	suppressed.  The user isn't trying to diagnose lddstub, this is simply
17467c478bd9Sstevel@tonic-gate  *	a stub executable employed to preload a user specified library against.
17477c478bd9Sstevel@tonic-gate  *
17487c478bd9Sstevel@tonic-gate  *   .	If RT_FL_SILENCERR is in effect then any generic ldd() messages should
17497c478bd9Sstevel@tonic-gate  *	be suppressed.  All detailed ldd messages should still be produced.
17507c478bd9Sstevel@tonic-gate  */
17517c478bd9Sstevel@tonic-gate Sym *
17527c478bd9Sstevel@tonic-gate elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
17537c478bd9Sstevel@tonic-gate {
17547c478bd9Sstevel@tonic-gate 	Sym	*sym;
17557c478bd9Sstevel@tonic-gate 	Dyninfo	*dip = &DYNINFO(slp->sl_imap)[ndx];
17567c478bd9Sstevel@tonic-gate 	int	silent = 0;
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	/*
17597c478bd9Sstevel@tonic-gate 	 * Make sure this entry is still acting as a filter.  We may have tried
17607c478bd9Sstevel@tonic-gate 	 * to process this previously, and disabled it if the filtee couldn't
17617c478bd9Sstevel@tonic-gate 	 * be processed.  However, other entries may provide different filtees
17627c478bd9Sstevel@tonic-gate 	 * that are yet to be completed.
17637c478bd9Sstevel@tonic-gate 	 */
17647c478bd9Sstevel@tonic-gate 	if (dip->di_flags == 0)
17657c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	/*
17687c478bd9Sstevel@tonic-gate 	 * Indicate whether an error message is required should this filtee not
17697c478bd9Sstevel@tonic-gate 	 * be found, based on the type of filter.
17707c478bd9Sstevel@tonic-gate 	 */
17717c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_AUXFLTR) &&
17727c478bd9Sstevel@tonic-gate 	    ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) {
17737c478bd9Sstevel@tonic-gate 		rtld_flags |= RT_FL_SILENCERR;
17747c478bd9Sstevel@tonic-gate 		silent = 1;
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	sym = _elf_lookup_filtee(slp, dlmp, binfo, ndx);
17787c478bd9Sstevel@tonic-gate 
17797c478bd9Sstevel@tonic-gate 	if (silent)
17807c478bd9Sstevel@tonic-gate 		rtld_flags &= ~RT_FL_SILENCERR;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	return (sym);
17837c478bd9Sstevel@tonic-gate }
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate /*
17867c478bd9Sstevel@tonic-gate  * Compute the elf hash value (as defined in the ELF access library).
17877c478bd9Sstevel@tonic-gate  * The form of the hash table is:
17887c478bd9Sstevel@tonic-gate  *
17897c478bd9Sstevel@tonic-gate  *	|--------------|
17907c478bd9Sstevel@tonic-gate  *	| # of buckets |
17917c478bd9Sstevel@tonic-gate  *	|--------------|
17927c478bd9Sstevel@tonic-gate  *	| # of chains  |
17937c478bd9Sstevel@tonic-gate  *	|--------------|
17947c478bd9Sstevel@tonic-gate  *	|   bucket[]   |
17957c478bd9Sstevel@tonic-gate  *	|--------------|
17967c478bd9Sstevel@tonic-gate  *	|   chain[]    |
17977c478bd9Sstevel@tonic-gate  *	|--------------|
17987c478bd9Sstevel@tonic-gate  */
17997c478bd9Sstevel@tonic-gate ulong_t
18007c478bd9Sstevel@tonic-gate elf_hash(const char *name)
18017c478bd9Sstevel@tonic-gate {
18027c478bd9Sstevel@tonic-gate 	uint_t	hval = 0;
18037c478bd9Sstevel@tonic-gate 
18047c478bd9Sstevel@tonic-gate 	while (*name) {
18057c478bd9Sstevel@tonic-gate 		uint_t	g;
18067c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *name++;
18077c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
18087c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
18097c478bd9Sstevel@tonic-gate 		hval &= ~g;
18107c478bd9Sstevel@tonic-gate 	}
18117c478bd9Sstevel@tonic-gate 	return ((ulong_t)hval);
18127c478bd9Sstevel@tonic-gate }
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate /*
18157c478bd9Sstevel@tonic-gate  * If flag argument has LKUP_SPEC set, we treat undefined symbols of type
18167c478bd9Sstevel@tonic-gate  * function specially in the executable - if they have a value, even though
18177c478bd9Sstevel@tonic-gate  * undefined, we use that value.  This allows us to associate all references
18187c478bd9Sstevel@tonic-gate  * to a function's address to a single place in the process: the plt entry
18197c478bd9Sstevel@tonic-gate  * for that function in the executable.  Calls to lookup from plt binding
18207c478bd9Sstevel@tonic-gate  * routines do NOT set LKUP_SPEC in the flag.
18217c478bd9Sstevel@tonic-gate  */
18227c478bd9Sstevel@tonic-gate Sym *
18237c478bd9Sstevel@tonic-gate elf_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
18247c478bd9Sstevel@tonic-gate {
18257c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
18267c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
18277c478bd9Sstevel@tonic-gate 	ulong_t		hash = slp->sl_hash;
18287c478bd9Sstevel@tonic-gate 	uint_t		ndx, htmp, buckets, *chainptr;
18297c478bd9Sstevel@tonic-gate 	Sym		*sym, *symtabptr;
18307c478bd9Sstevel@tonic-gate 	char		*strtabptr, *strtabname;
18317c478bd9Sstevel@tonic-gate 	uint_t		flags1;
18327c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
18337c478bd9Sstevel@tonic-gate 
1834660acd81Srie 	/*
1835660acd81Srie 	 * If we're only here to establish a symbols index, skip the diagnostic
1836660acd81Srie 	 * used to trace a symbol search.
1837660acd81Srie 	 */
1838660acd81Srie 	if ((slp->sl_flags & LKUP_SYMNDX) == 0) {
1839660acd81Srie 		DBG_CALL(Dbg_syms_lookup(name, NAME(ilmp),
1840660acd81Srie 		    MSG_ORIG(MSG_STR_ELF)));
1841660acd81Srie 	}
18427c478bd9Sstevel@tonic-gate 
18437c478bd9Sstevel@tonic-gate 	if (HASH(ilmp) == 0)
18447c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
18457c478bd9Sstevel@tonic-gate 
18467c478bd9Sstevel@tonic-gate 	buckets = HASH(ilmp)[0];
18477c478bd9Sstevel@tonic-gate 	/* LINTED */
18487c478bd9Sstevel@tonic-gate 	htmp = (uint_t)hash % buckets;
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	/*
18517c478bd9Sstevel@tonic-gate 	 * Get the first symbol on hash chain and initialize the string
18527c478bd9Sstevel@tonic-gate 	 * and symbol table pointers.
18537c478bd9Sstevel@tonic-gate 	 */
18547c478bd9Sstevel@tonic-gate 	if ((ndx = HASH(ilmp)[htmp + 2]) == 0)
18557c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	chainptr = HASH(ilmp) + 2 + buckets;
18587c478bd9Sstevel@tonic-gate 	strtabptr = STRTAB(ilmp);
18597c478bd9Sstevel@tonic-gate 	symtabptr = SYMTAB(ilmp);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 	while (ndx) {
18627c478bd9Sstevel@tonic-gate 		sym = symtabptr + ndx;
18637c478bd9Sstevel@tonic-gate 		strtabname = strtabptr + sym->st_name;
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 		/*
18667c478bd9Sstevel@tonic-gate 		 * Compare the symbol found with the name required.  If the
18677c478bd9Sstevel@tonic-gate 		 * names don't match continue with the next hash entry.
18687c478bd9Sstevel@tonic-gate 		 */
18697c478bd9Sstevel@tonic-gate 		if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
18707c478bd9Sstevel@tonic-gate 			if ((ndx = chainptr[ndx]) != 0)
18717c478bd9Sstevel@tonic-gate 				continue;
18727c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
18737c478bd9Sstevel@tonic-gate 		}
18747c478bd9Sstevel@tonic-gate 
1875660acd81Srie 		/*
1876660acd81Srie 		 * If we're only here to establish a symbols index, we're done.
1877660acd81Srie 		 */
1878660acd81Srie 		if (slp->sl_flags & LKUP_SYMNDX)
1879660acd81Srie 			return (sym);
1880660acd81Srie 
18817c478bd9Sstevel@tonic-gate 		/*
18827c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is defined, return the
18837c478bd9Sstevel@tonic-gate 		 * symbol pointer and the link map in which it was found.
18847c478bd9Sstevel@tonic-gate 		 */
18857c478bd9Sstevel@tonic-gate 		if (sym->st_shndx != SHN_UNDEF) {
18867c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
18877c478bd9Sstevel@tonic-gate 			*binfo |= DBG_BINFO_FOUND;
18887c478bd9Sstevel@tonic-gate 			if (FLAGS(ilmp) & FLG_RT_INTRPOSE)
18897c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
18907c478bd9Sstevel@tonic-gate 			break;
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 		/*
18937c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is undefined, the
18947c478bd9Sstevel@tonic-gate 		 * symbol type is a function, and the value of the symbol
18957c478bd9Sstevel@tonic-gate 		 * is non zero, then this is a special case.  This allows
18967c478bd9Sstevel@tonic-gate 		 * the resolution of a function address to the plt[] entry.
18977c478bd9Sstevel@tonic-gate 		 * See SPARC ABI, Dynamic Linking, Function Addresses for
18987c478bd9Sstevel@tonic-gate 		 * more details.
18997c478bd9Sstevel@tonic-gate 		 */
1900660acd81Srie 		} else if ((slp->sl_flags & LKUP_SPEC) &&
19017c478bd9Sstevel@tonic-gate 		    (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) &&
19027c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
19037c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
19047c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR);
19057c478bd9Sstevel@tonic-gate 			if (FLAGS(ilmp) & FLG_RT_INTRPOSE)
19067c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
19077c478bd9Sstevel@tonic-gate 			return (sym);
19087c478bd9Sstevel@tonic-gate 		}
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 		/*
19117c478bd9Sstevel@tonic-gate 		 * Undefined symbol.
19127c478bd9Sstevel@tonic-gate 		 */
19137c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
19147c478bd9Sstevel@tonic-gate 	}
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 	/*
19177c478bd9Sstevel@tonic-gate 	 * We've found a match.  Determine if the defining object contains
19187c478bd9Sstevel@tonic-gate 	 * symbol binding information.
19197c478bd9Sstevel@tonic-gate 	 */
19207c478bd9Sstevel@tonic-gate 	if ((sip = SYMINFO(ilmp)) != 0)
19217c478bd9Sstevel@tonic-gate 		/* LINTED */
19227c478bd9Sstevel@tonic-gate 		sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(ilmp)));
19237c478bd9Sstevel@tonic-gate 
19247c478bd9Sstevel@tonic-gate 	/*
19257c478bd9Sstevel@tonic-gate 	 * If this is a direct binding request, but the symbol definition has
19267c478bd9Sstevel@tonic-gate 	 * disabled directly binding to it (presumably because the symbol
19277c478bd9Sstevel@tonic-gate 	 * definition has been changed since the referring object was built),
19287c478bd9Sstevel@tonic-gate 	 * indicate this failure so that the caller can fall back to a standard
19297c478bd9Sstevel@tonic-gate 	 * symbol search.  Clear any debug binding information for cleanliness.
19307c478bd9Sstevel@tonic-gate 	 */
19317c478bd9Sstevel@tonic-gate 	if (sip && (slp->sl_flags & LKUP_DIRECT) &&
19327c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) {
19337c478bd9Sstevel@tonic-gate 		*binfo |= BINFO_DIRECTDIS;
19347c478bd9Sstevel@tonic-gate 		*binfo &= ~DBG_BINFO_MSK;
19357c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate 	/*
19397c478bd9Sstevel@tonic-gate 	 * Determine whether this object is acting as a filter.
19407c478bd9Sstevel@tonic-gate 	 */
19417c478bd9Sstevel@tonic-gate 	if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0)
19427c478bd9Sstevel@tonic-gate 		return (sym);
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	/*
19457c478bd9Sstevel@tonic-gate 	 * Determine if this object offers per-symbol filtering, and if so,
19467c478bd9Sstevel@tonic-gate 	 * whether this symbol references a filtee.
19477c478bd9Sstevel@tonic-gate 	 */
19487c478bd9Sstevel@tonic-gate 	if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) {
19497c478bd9Sstevel@tonic-gate 		/*
19507c478bd9Sstevel@tonic-gate 		 * If this is a standard filter reference, and no standard
19517c478bd9Sstevel@tonic-gate 		 * filtees remain to be inspected, we're done.  If this is an
19527c478bd9Sstevel@tonic-gate 		 * auxiliary filter reference, and no auxiliary filtees remain,
19537c478bd9Sstevel@tonic-gate 		 * we'll fall through in case any object filtering is available.
19547c478bd9Sstevel@tonic-gate 		 */
19557c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) &&
19567c478bd9Sstevel@tonic-gate 		    (SYMSFLTRCNT(ilmp) == 0))
19577c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) ||
19607c478bd9Sstevel@tonic-gate 		    ((sip->si_flags & SYMINFO_FLG_AUXILIARY) &&
19617c478bd9Sstevel@tonic-gate 		    SYMAFLTRCNT(ilmp))) {
19627c478bd9Sstevel@tonic-gate 			Sym *	fsym;
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 			/*
19657c478bd9Sstevel@tonic-gate 			 * This symbol has an associated filtee.  Lookup the
19667c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
19677c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
19687c478bd9Sstevel@tonic-gate 			 * filter, return an error, otherwise fall through to
19697c478bd9Sstevel@tonic-gate 			 * catch any object filtering that may be available.
19707c478bd9Sstevel@tonic-gate 			 */
19717c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
19727c478bd9Sstevel@tonic-gate 			    sip->si_boundto)) != 0)
19737c478bd9Sstevel@tonic-gate 				return (fsym);
19747c478bd9Sstevel@tonic-gate 			if (sip->si_flags & SYMINFO_FLG_FILTER)
19757c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
19767c478bd9Sstevel@tonic-gate 		}
19777c478bd9Sstevel@tonic-gate 	}
19787c478bd9Sstevel@tonic-gate 
19797c478bd9Sstevel@tonic-gate 	/*
19807c478bd9Sstevel@tonic-gate 	 * Determine if this object provides global filtering.
19817c478bd9Sstevel@tonic-gate 	 */
19827c478bd9Sstevel@tonic-gate 	if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) {
19837c478bd9Sstevel@tonic-gate 		Sym *	fsym;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) {
19867c478bd9Sstevel@tonic-gate 			/*
19877c478bd9Sstevel@tonic-gate 			 * This object has an associated filtee.  Lookup the
19887c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
19897c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
19907c478bd9Sstevel@tonic-gate 			 * filter, return and error, otherwise return the symbol
19917c478bd9Sstevel@tonic-gate 			 * within the filter itself.
19927c478bd9Sstevel@tonic-gate 			 */
19937c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
19947c478bd9Sstevel@tonic-gate 			    OBJFLTRNDX(ilmp))) != 0)
19957c478bd9Sstevel@tonic-gate 				return (fsym);
19967c478bd9Sstevel@tonic-gate 		}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 		if (flags1 & FL1_RT_OBJSFLTR)
19997c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
20007c478bd9Sstevel@tonic-gate 	}
20017c478bd9Sstevel@tonic-gate 	return (sym);
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate /*
20057c478bd9Sstevel@tonic-gate  * Create a new Rt_map structure for an ELF object and initialize
20067c478bd9Sstevel@tonic-gate  * all values.
20077c478bd9Sstevel@tonic-gate  */
20087c478bd9Sstevel@tonic-gate Rt_map *
20097c478bd9Sstevel@tonic-gate elf_new_lm(Lm_list *lml, const char *pname, const char *oname, Dyn *ld,
20107c478bd9Sstevel@tonic-gate     ulong_t addr, ulong_t etext, Aliste lmco, ulong_t msize, ulong_t entry,
20117c478bd9Sstevel@tonic-gate     ulong_t paddr, ulong_t padimsize, Mmap *mmaps, uint_t mmapcnt)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
20147c478bd9Sstevel@tonic-gate 	ulong_t		base, fltr = 0, audit = 0, cfile = 0, crle = 0;
20157c478bd9Sstevel@tonic-gate 	Xword		rpath = 0;
20167c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr = (Ehdr *)addr;
20177c478bd9Sstevel@tonic-gate 
20187c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_file_elf(pname, (ulong_t)ld, addr, msize, entry,
20197c478bd9Sstevel@tonic-gate 	    get_linkmap_id(lml), lmco));
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	/*
20227c478bd9Sstevel@tonic-gate 	 * Allocate space for the link-map and private elf information.  Once
20237c478bd9Sstevel@tonic-gate 	 * these are allocated and initialized, we can use remove_so(0, lmp) to
20247c478bd9Sstevel@tonic-gate 	 * tear down the link-map should any failures occur.
20257c478bd9Sstevel@tonic-gate 	 */
20267c478bd9Sstevel@tonic-gate 	if ((lmp = calloc(sizeof (Rt_map), 1)) == 0)
20277c478bd9Sstevel@tonic-gate 		return (0);
20287c478bd9Sstevel@tonic-gate 	if ((ELFPRV(lmp) = calloc(sizeof (Rt_elfp), 1)) == 0) {
20297c478bd9Sstevel@tonic-gate 		free(lmp);
20307c478bd9Sstevel@tonic-gate 		return (0);
20317c478bd9Sstevel@tonic-gate 	}
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 	/*
20347c478bd9Sstevel@tonic-gate 	 * All fields not filled in were set to 0 by calloc.
20357c478bd9Sstevel@tonic-gate 	 */
20367c478bd9Sstevel@tonic-gate 	ORIGNAME(lmp) = PATHNAME(lmp) = NAME(lmp) = (char *)pname;
20377c478bd9Sstevel@tonic-gate 	DYN(lmp) = ld;
20387c478bd9Sstevel@tonic-gate 	ADDR(lmp) = addr;
20397c478bd9Sstevel@tonic-gate 	MSIZE(lmp) = msize;
20407c478bd9Sstevel@tonic-gate 	ENTRY(lmp) = (Addr)entry;
20417c478bd9Sstevel@tonic-gate 	SYMINTP(lmp) = elf_find_sym;
20427c478bd9Sstevel@tonic-gate 	ETEXT(lmp) = etext;
20437c478bd9Sstevel@tonic-gate 	FCT(lmp) = &elf_fct;
20447c478bd9Sstevel@tonic-gate 	LIST(lmp) = lml;
20457c478bd9Sstevel@tonic-gate 	PADSTART(lmp) = paddr;
20467c478bd9Sstevel@tonic-gate 	PADIMLEN(lmp) = padimsize;
20477c478bd9Sstevel@tonic-gate 	THREADID(lmp) = rt_thr_self();
20487c478bd9Sstevel@tonic-gate 	OBJFLTRNDX(lmp) = FLTR_DISABLED;
2049dffec89cSrie 	SORTVAL(lmp) = -1;
20507c478bd9Sstevel@tonic-gate 
20517c478bd9Sstevel@tonic-gate 	MMAPS(lmp) = mmaps;
20527c478bd9Sstevel@tonic-gate 	MMAPCNT(lmp) = mmapcnt;
20537c478bd9Sstevel@tonic-gate 	ASSERT(mmapcnt != 0);
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 	/*
20567c478bd9Sstevel@tonic-gate 	 * If this is a shared object, add the base address to each address.
20577c478bd9Sstevel@tonic-gate 	 * if this is an executable, use address as is.
20587c478bd9Sstevel@tonic-gate 	 */
20597c478bd9Sstevel@tonic-gate 	if (ehdr->e_type == ET_EXEC) {
20607c478bd9Sstevel@tonic-gate 		base = 0;
20617c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_FIXED;
20627c478bd9Sstevel@tonic-gate 	} else
20637c478bd9Sstevel@tonic-gate 		base = addr;
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	/*
20667c478bd9Sstevel@tonic-gate 	 * Fill in rest of the link map entries with information from the file's
20677c478bd9Sstevel@tonic-gate 	 * dynamic structure.
20687c478bd9Sstevel@tonic-gate 	 */
20697c478bd9Sstevel@tonic-gate 	if (ld) {
20707c478bd9Sstevel@tonic-gate 		uint_t	dyncnt = 0;
20717c478bd9Sstevel@tonic-gate 		Xword	pltpadsz = 0;
20727c478bd9Sstevel@tonic-gate 		void	*rtldinfo;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 		/* CSTYLED */
20757c478bd9Sstevel@tonic-gate 		for ( ; ld->d_tag != DT_NULL; ++ld, dyncnt++) {
20767c478bd9Sstevel@tonic-gate 			switch ((Xword)ld->d_tag) {
20777c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
20787c478bd9Sstevel@tonic-gate 				SYMTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
20797c478bd9Sstevel@tonic-gate 				break;
20807c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
20817c478bd9Sstevel@tonic-gate 				STRTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
20827c478bd9Sstevel@tonic-gate 				break;
20837c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
20847c478bd9Sstevel@tonic-gate 				SYMENT(lmp) = ld->d_un.d_val;
20857c478bd9Sstevel@tonic-gate 				break;
20867c478bd9Sstevel@tonic-gate 			case DT_FEATURE_1:
20877c478bd9Sstevel@tonic-gate 				ld->d_un.d_val |= DTF_1_PARINIT;
20887c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DTF_1_CONFEXP)
20897c478bd9Sstevel@tonic-gate 					crle = 1;
20907c478bd9Sstevel@tonic-gate 				break;
20917c478bd9Sstevel@tonic-gate 			case DT_MOVESZ:
20927c478bd9Sstevel@tonic-gate 				MOVESZ(lmp) = ld->d_un.d_val;
20937c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_MOVE;
20947c478bd9Sstevel@tonic-gate 				break;
20957c478bd9Sstevel@tonic-gate 			case DT_MOVEENT:
20967c478bd9Sstevel@tonic-gate 				MOVEENT(lmp) = ld->d_un.d_val;
20977c478bd9Sstevel@tonic-gate 				break;
20987c478bd9Sstevel@tonic-gate 			case DT_MOVETAB:
20997c478bd9Sstevel@tonic-gate 				MOVETAB(lmp) = (void *)(ld->d_un.d_ptr + base);
21007c478bd9Sstevel@tonic-gate 				break;
21017c478bd9Sstevel@tonic-gate 			case DT_REL:
21027c478bd9Sstevel@tonic-gate 			case DT_RELA:
21037c478bd9Sstevel@tonic-gate 				/*
21047c478bd9Sstevel@tonic-gate 				 * At this time we can only handle 1 type of
21057c478bd9Sstevel@tonic-gate 				 * relocation per object.
21067c478bd9Sstevel@tonic-gate 				 */
21077c478bd9Sstevel@tonic-gate 				REL(lmp) = (void *)(ld->d_un.d_ptr + base);
21087c478bd9Sstevel@tonic-gate 				break;
21097c478bd9Sstevel@tonic-gate 			case DT_RELSZ:
21107c478bd9Sstevel@tonic-gate 			case DT_RELASZ:
21117c478bd9Sstevel@tonic-gate 				RELSZ(lmp) = ld->d_un.d_val;
21127c478bd9Sstevel@tonic-gate 				break;
21137c478bd9Sstevel@tonic-gate 			case DT_RELENT:
21147c478bd9Sstevel@tonic-gate 			case DT_RELAENT:
21157c478bd9Sstevel@tonic-gate 				RELENT(lmp) = ld->d_un.d_val;
21167c478bd9Sstevel@tonic-gate 				break;
21177c478bd9Sstevel@tonic-gate 			case DT_RELCOUNT:
21187c478bd9Sstevel@tonic-gate 			case DT_RELACOUNT:
21197c478bd9Sstevel@tonic-gate 				RELACOUNT(lmp) = (uint_t)ld->d_un.d_val;
21207c478bd9Sstevel@tonic-gate 				break;
21217c478bd9Sstevel@tonic-gate 			case DT_TEXTREL:
21227c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_TEXTREL;
21237c478bd9Sstevel@tonic-gate 				break;
21247c478bd9Sstevel@tonic-gate 			case DT_HASH:
21257c478bd9Sstevel@tonic-gate 				HASH(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
21267c478bd9Sstevel@tonic-gate 				break;
21277c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
21287c478bd9Sstevel@tonic-gate 				PLTGOT(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
21297c478bd9Sstevel@tonic-gate 				break;
21307c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
21317c478bd9Sstevel@tonic-gate 				PLTRELSZ(lmp) = ld->d_un.d_val;
21327c478bd9Sstevel@tonic-gate 				break;
21337c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
21347c478bd9Sstevel@tonic-gate 				JMPREL(lmp) = (void *)(ld->d_un.d_ptr + base);
21357c478bd9Sstevel@tonic-gate 				break;
21367c478bd9Sstevel@tonic-gate 			case DT_INIT:
21377c478bd9Sstevel@tonic-gate 				INIT(lmp) = (void (*)())(ld->d_un.d_ptr + base);
21387c478bd9Sstevel@tonic-gate 				break;
21397c478bd9Sstevel@tonic-gate 			case DT_FINI:
21407c478bd9Sstevel@tonic-gate 				FINI(lmp) = (void (*)())(ld->d_un.d_ptr + base);
21417c478bd9Sstevel@tonic-gate 				break;
21427c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAY:
21437c478bd9Sstevel@tonic-gate 				INITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21447c478bd9Sstevel@tonic-gate 				    base);
21457c478bd9Sstevel@tonic-gate 				break;
21467c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAYSZ:
21477c478bd9Sstevel@tonic-gate 				INITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21487c478bd9Sstevel@tonic-gate 				break;
21497c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAY:
21507c478bd9Sstevel@tonic-gate 				FINIARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21517c478bd9Sstevel@tonic-gate 				    base);
21527c478bd9Sstevel@tonic-gate 				break;
21537c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAYSZ:
21547c478bd9Sstevel@tonic-gate 				FINIARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21557c478bd9Sstevel@tonic-gate 				break;
21567c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAY:
21577c478bd9Sstevel@tonic-gate 				PREINITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
21587c478bd9Sstevel@tonic-gate 				    base);
21597c478bd9Sstevel@tonic-gate 				break;
21607c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAYSZ:
21617c478bd9Sstevel@tonic-gate 				PREINITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
21627c478bd9Sstevel@tonic-gate 				break;
21637c478bd9Sstevel@tonic-gate 			case DT_RPATH:
21647c478bd9Sstevel@tonic-gate 			case DT_RUNPATH:
21657c478bd9Sstevel@tonic-gate 				rpath = ld->d_un.d_val;
21667c478bd9Sstevel@tonic-gate 				break;
21677c478bd9Sstevel@tonic-gate 			case DT_FILTER:
21687c478bd9Sstevel@tonic-gate 				fltr = ld->d_un.d_val;
21697c478bd9Sstevel@tonic-gate 				OBJFLTRNDX(lmp) = dyncnt;
21707c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJSFLTR;
21717c478bd9Sstevel@tonic-gate 				break;
21727c478bd9Sstevel@tonic-gate 			case DT_AUXILIARY:
21737c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
21747c478bd9Sstevel@tonic-gate 					fltr = ld->d_un.d_val;
21757c478bd9Sstevel@tonic-gate 					OBJFLTRNDX(lmp) = dyncnt;
21767c478bd9Sstevel@tonic-gate 				}
21777c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJAFLTR;
21787c478bd9Sstevel@tonic-gate 				break;
21797c478bd9Sstevel@tonic-gate 			case DT_SUNW_FILTER:
21807c478bd9Sstevel@tonic-gate 				SYMSFLTRCNT(lmp)++;
21817c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMSFLTR;
21827c478bd9Sstevel@tonic-gate 				break;
21837c478bd9Sstevel@tonic-gate 			case DT_SUNW_AUXILIARY:
21847c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
21857c478bd9Sstevel@tonic-gate 					SYMAFLTRCNT(lmp)++;
21867c478bd9Sstevel@tonic-gate 				}
21877c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMAFLTR;
21887c478bd9Sstevel@tonic-gate 				break;
21897c478bd9Sstevel@tonic-gate 			case DT_DEPAUDIT:
21907c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUDIT))
21917c478bd9Sstevel@tonic-gate 					audit = ld->d_un.d_val;
21927c478bd9Sstevel@tonic-gate 				break;
21937c478bd9Sstevel@tonic-gate 			case DT_CONFIG:
21947c478bd9Sstevel@tonic-gate 				cfile = ld->d_un.d_val;
21957c478bd9Sstevel@tonic-gate 				break;
21967c478bd9Sstevel@tonic-gate 			case DT_DEBUG:
21977c478bd9Sstevel@tonic-gate 				/*
21987c478bd9Sstevel@tonic-gate 				 * DT_DEBUG entries are only created in
21997c478bd9Sstevel@tonic-gate 				 * dynamic objects that require an interpretor
22007c478bd9Sstevel@tonic-gate 				 * (ie. all dynamic executables and some shared
22017c478bd9Sstevel@tonic-gate 				 * objects), and provide for a hand-shake with
22027c478bd9Sstevel@tonic-gate 				 * debuggers.  This entry is initialized to
22037c478bd9Sstevel@tonic-gate 				 * zero by the link-editor.  If a debugger has
22047c478bd9Sstevel@tonic-gate 				 * us and updated this entry set the debugger
22057c478bd9Sstevel@tonic-gate 				 * flag, and finish initializing the debugging
22067c478bd9Sstevel@tonic-gate 				 * structure (see setup() also).  Switch off any
22077c478bd9Sstevel@tonic-gate 				 * configuration object use as most debuggers
22087c478bd9Sstevel@tonic-gate 				 * can't handle fixed dynamic executables as
22097c478bd9Sstevel@tonic-gate 				 * dependencies, and we can't handle requests
22107c478bd9Sstevel@tonic-gate 				 * like object padding for alternative objects.
22117c478bd9Sstevel@tonic-gate 				 */
22127c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_ptr)
22137c478bd9Sstevel@tonic-gate 					rtld_flags |=
22147c478bd9Sstevel@tonic-gate 					    (RT_FL_DEBUGGER | RT_FL_NOOBJALT);
22157c478bd9Sstevel@tonic-gate 				ld->d_un.d_ptr = (Addr)&r_debug;
22167c478bd9Sstevel@tonic-gate 				break;
22177c478bd9Sstevel@tonic-gate 			case DT_VERNEED:
22187c478bd9Sstevel@tonic-gate 				VERNEED(lmp) = (Verneed *)(ld->d_un.d_ptr +
22197c478bd9Sstevel@tonic-gate 				    base);
22207c478bd9Sstevel@tonic-gate 				break;
22217c478bd9Sstevel@tonic-gate 			case DT_VERNEEDNUM:
22227c478bd9Sstevel@tonic-gate 				/* LINTED */
22237c478bd9Sstevel@tonic-gate 				VERNEEDNUM(lmp) = (int)ld->d_un.d_val;
22247c478bd9Sstevel@tonic-gate 				break;
22257c478bd9Sstevel@tonic-gate 			case DT_VERDEF:
22267c478bd9Sstevel@tonic-gate 				VERDEF(lmp) = (Verdef *)(ld->d_un.d_ptr + base);
22277c478bd9Sstevel@tonic-gate 				break;
22287c478bd9Sstevel@tonic-gate 			case DT_VERDEFNUM:
22297c478bd9Sstevel@tonic-gate 				/* LINTED */
22307c478bd9Sstevel@tonic-gate 				VERDEFNUM(lmp) = (int)ld->d_un.d_val;
22317c478bd9Sstevel@tonic-gate 				break;
22327c478bd9Sstevel@tonic-gate 			case DT_BIND_NOW:
2233dffec89cSrie 				if ((ld->d_un.d_val & DF_BIND_NOW) &&
2234dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22357c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22367c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22377c478bd9Sstevel@tonic-gate 				}
22387c478bd9Sstevel@tonic-gate 				break;
22397c478bd9Sstevel@tonic-gate 			case DT_FLAGS:
22407c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_SYMBOLIC)
22417c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_SYMBOLIC;
22427c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_TEXTREL)
22437c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_TEXTREL;
2244dffec89cSrie 				if ((ld->d_un.d_val & DF_BIND_NOW) &&
2245dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22467c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22477c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22487c478bd9Sstevel@tonic-gate 				}
22497c478bd9Sstevel@tonic-gate 				break;
22507c478bd9Sstevel@tonic-gate 			case DT_FLAGS_1:
22517c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DISPRELPND)
22527c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_DISPREL;
22537c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_GROUP)
22547c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |=
22557c478bd9Sstevel@tonic-gate 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
2256dffec89cSrie 				if ((ld->d_un.d_val & DF_1_NOW) &&
2257dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
22587c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
22597c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
22607c478bd9Sstevel@tonic-gate 				}
22617c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODELETE)
22627c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NODELETE;
22637c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_INITFIRST)
22647c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INITFRST;
22657c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NOOPEN)
22667c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NOOPEN;
22677c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_LOADFLTR)
22687c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_LOADFLTR;
22697c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODUMP)
22707c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NODUMP;
22717c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_CONFALT)
22727c478bd9Sstevel@tonic-gate 					crle = 1;
22737c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DIRECT)
22747c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_DIRECT;
22757c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODEFLIB)
22767c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_NODEFLIB;
22777c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ENDFILTEE)
22787c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_ENDFILTE;
22797c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_TRANS)
22807c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_TRANS;
22817c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
22827c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ORIGIN)
22837c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_RELATIVE;
22847c478bd9Sstevel@tonic-gate #endif
22857c478bd9Sstevel@tonic-gate 				/*
22867c478bd9Sstevel@tonic-gate 				 * If this object identifies itself as an
22877c478bd9Sstevel@tonic-gate 				 * interposer, but relocation processing has
22887c478bd9Sstevel@tonic-gate 				 * already started, then demote it.  It's too
22897c478bd9Sstevel@tonic-gate 				 * late to guarantee complete interposition.
22907c478bd9Sstevel@tonic-gate 				 */
22917c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_INTERPOSE) {
22927c478bd9Sstevel@tonic-gate 				    if ((lml->lm_flags & LML_FLG_STARTREL) == 0)
22937c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INTRPOSE;
22947c478bd9Sstevel@tonic-gate 				    else {
22957c478bd9Sstevel@tonic-gate 					DBG_CALL(Dbg_util_intoolate(NAME(lmp)));
22967c478bd9Sstevel@tonic-gate 					if (lml->lm_flags & LML_FLG_TRC_ENABLE)
22977c478bd9Sstevel@tonic-gate 					    (void) printf(
22987c478bd9Sstevel@tonic-gate 						MSG_INTL(MSG_LDD_REL_ERR2),
22997c478bd9Sstevel@tonic-gate 						NAME(lmp));
23007c478bd9Sstevel@tonic-gate 				    }
23017c478bd9Sstevel@tonic-gate 				}
23027c478bd9Sstevel@tonic-gate 				break;
23037c478bd9Sstevel@tonic-gate 			case DT_SYMINFO:
23047c478bd9Sstevel@tonic-gate 				SYMINFO(lmp) = (Syminfo *)(ld->d_un.d_ptr +
23057c478bd9Sstevel@tonic-gate 				    base);
23067c478bd9Sstevel@tonic-gate 				break;
23077c478bd9Sstevel@tonic-gate 			case DT_SYMINENT:
23087c478bd9Sstevel@tonic-gate 				SYMINENT(lmp) = ld->d_un.d_val;
23097c478bd9Sstevel@tonic-gate 				break;
23107c478bd9Sstevel@tonic-gate 			case DT_PLTPAD:
23117c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = (void *)(ld->d_un.d_ptr + base);
23127c478bd9Sstevel@tonic-gate 				break;
23137c478bd9Sstevel@tonic-gate 			case DT_PLTPADSZ:
23147c478bd9Sstevel@tonic-gate 				pltpadsz = ld->d_un.d_val;
23157c478bd9Sstevel@tonic-gate 				break;
23167c478bd9Sstevel@tonic-gate 			case DT_SUNW_RTLDINF:
23177c478bd9Sstevel@tonic-gate 				if ((lml->lm_info_lmp != 0) &&
23187c478bd9Sstevel@tonic-gate 				    (lml->lm_info_lmp != lmp)) {
23197c478bd9Sstevel@tonic-gate 					DBG_CALL(Dbg_unused_rtldinfo(
23207c478bd9Sstevel@tonic-gate 						NAME(lmp),
23217c478bd9Sstevel@tonic-gate 						NAME(lml->lm_info_lmp)));
23227c478bd9Sstevel@tonic-gate 					break;
23237c478bd9Sstevel@tonic-gate 				}
23247c478bd9Sstevel@tonic-gate 				lml->lm_info_lmp = lmp;
23257c478bd9Sstevel@tonic-gate 				rtldinfo = (void *)(ld->d_un.d_ptr + base);
23267c478bd9Sstevel@tonic-gate 
23277c478bd9Sstevel@tonic-gate 				/*
23287c478bd9Sstevel@tonic-gate 				 * We maintain a list of DT_SUNW_RTLDINFO
23297c478bd9Sstevel@tonic-gate 				 * structures for a given object.  This permits
23307c478bd9Sstevel@tonic-gate 				 * the RTLDINFO structures to be grouped
23317c478bd9Sstevel@tonic-gate 				 * functionly inside of a shared object.
23327c478bd9Sstevel@tonic-gate 				 *
23337c478bd9Sstevel@tonic-gate 				 * For example, we could have one for
23347c478bd9Sstevel@tonic-gate 				 * thread_init, and another for atexit
23357c478bd9Sstevel@tonic-gate 				 * reservations.
23367c478bd9Sstevel@tonic-gate 				 */
23377c478bd9Sstevel@tonic-gate 				if (alist_append(&lml->lm_rtldinfo, &rtldinfo,
23387c478bd9Sstevel@tonic-gate 				    sizeof (void *), AL_CNT_RTLDINFO) == 0) {
23397c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
23407c478bd9Sstevel@tonic-gate 					return (0);
23417c478bd9Sstevel@tonic-gate 				}
23427c478bd9Sstevel@tonic-gate 				break;
23437c478bd9Sstevel@tonic-gate 			case DT_DEPRECATED_SPARC_REGISTER:
23447c478bd9Sstevel@tonic-gate 			case M_DT_REGISTER:
23457c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_REGSYMS;
23467c478bd9Sstevel@tonic-gate 				break;
23477c478bd9Sstevel@tonic-gate 			case M_DT_PLTRESERVE:
23487c478bd9Sstevel@tonic-gate 				PLTRESERVE(lmp) = (void *)(ld->d_un.d_ptr +
23497c478bd9Sstevel@tonic-gate 				    base);
23507c478bd9Sstevel@tonic-gate 				break;
23517c478bd9Sstevel@tonic-gate 			}
23527c478bd9Sstevel@tonic-gate 		}
23537c478bd9Sstevel@tonic-gate 
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 		if (PLTPAD(lmp)) {
23567c478bd9Sstevel@tonic-gate 			if (pltpadsz == (Xword)0)
23577c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = 0;
23587c478bd9Sstevel@tonic-gate 			else
23597c478bd9Sstevel@tonic-gate 				PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) +
23607c478bd9Sstevel@tonic-gate 				    pltpadsz);
23617c478bd9Sstevel@tonic-gate 		}
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 		/*
23647c478bd9Sstevel@tonic-gate 		 * Allocate Dynamic Info structure
23657c478bd9Sstevel@tonic-gate 		 */
23667c478bd9Sstevel@tonic-gate 		if ((DYNINFO(lmp) = calloc((size_t)dyncnt,
23677c478bd9Sstevel@tonic-gate 		    sizeof (Dyninfo))) == 0) {
23687c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
23697c478bd9Sstevel@tonic-gate 			return (0);
23707c478bd9Sstevel@tonic-gate 		}
23717c478bd9Sstevel@tonic-gate 		DYNINFOCNT(lmp) = dyncnt;
23727c478bd9Sstevel@tonic-gate 	}
23737c478bd9Sstevel@tonic-gate 
23747c478bd9Sstevel@tonic-gate 	/*
23757c478bd9Sstevel@tonic-gate 	 * If configuration file use hasn't been disabled, and a configuration
23767c478bd9Sstevel@tonic-gate 	 * file hasn't already been set via an environment variable, see if any
23777c478bd9Sstevel@tonic-gate 	 * application specific configuration file is specified.  An LD_CONFIG
23787c478bd9Sstevel@tonic-gate 	 * setting is used first, but if this image was generated via crle(1)
23797c478bd9Sstevel@tonic-gate 	 * then a default configuration file is a fall-back.
23807c478bd9Sstevel@tonic-gate 	 */
23817c478bd9Sstevel@tonic-gate 	if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == 0)) {
23827c478bd9Sstevel@tonic-gate 		if (cfile)
23837c478bd9Sstevel@tonic-gate 			config->c_name = (const char *)(cfile +
23847c478bd9Sstevel@tonic-gate 			    (char *)STRTAB(lmp));
23857c478bd9Sstevel@tonic-gate 		else if (crle) {
23867c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_CONFAPP;
23877c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
23887c478bd9Sstevel@tonic-gate 			FLAGS1(lmp) |= FL1_RT_RELATIVE;
23897c478bd9Sstevel@tonic-gate #endif
23907c478bd9Sstevel@tonic-gate 		}
23917c478bd9Sstevel@tonic-gate 	}
23927c478bd9Sstevel@tonic-gate 
23937c478bd9Sstevel@tonic-gate 	if (rpath)
23947c478bd9Sstevel@tonic-gate 		RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp));
23957c478bd9Sstevel@tonic-gate 	if (fltr) {
23967c478bd9Sstevel@tonic-gate 		/*
23977c478bd9Sstevel@tonic-gate 		 * If this object is a global filter, duplicate the filtee
23987c478bd9Sstevel@tonic-gate 		 * string name(s) so that REFNAME() is available in core files.
23997c478bd9Sstevel@tonic-gate 		 * This cludge was useful for debuggers at one point, but only
24007c478bd9Sstevel@tonic-gate 		 * when the filtee name was an individual full path.
24017c478bd9Sstevel@tonic-gate 		 */
24027c478bd9Sstevel@tonic-gate 		if ((REFNAME(lmp) = strdup(fltr + (char *)STRTAB(lmp))) == 0) {
24037c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
24047c478bd9Sstevel@tonic-gate 			return (0);
24057c478bd9Sstevel@tonic-gate 		}
24067c478bd9Sstevel@tonic-gate 	}
24077c478bd9Sstevel@tonic-gate 
24087c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_RELATIVE)
24097c478bd9Sstevel@tonic-gate 		FLAGS1(lmp) |= FL1_RT_RELATIVE;
24107c478bd9Sstevel@tonic-gate 
24117c478bd9Sstevel@tonic-gate 	/*
24127c478bd9Sstevel@tonic-gate 	 * For Intel ABI compatibility.  It's possible that a JMPREL can be
24137c478bd9Sstevel@tonic-gate 	 * specified without any other relocations (e.g. a dynamic executable
24147c478bd9Sstevel@tonic-gate 	 * normally only contains .plt relocations).  If this is the case then
24157c478bd9Sstevel@tonic-gate 	 * no REL, RELSZ or RELENT will have been created.  For us to be able
24167c478bd9Sstevel@tonic-gate 	 * to traverse the .plt relocations under LD_BIND_NOW we need to know
24177c478bd9Sstevel@tonic-gate 	 * the RELENT for these relocations.  Refer to elf_reloc() for more
24187c478bd9Sstevel@tonic-gate 	 * details.
24197c478bd9Sstevel@tonic-gate 	 */
24207c478bd9Sstevel@tonic-gate 	if (!RELENT(lmp) && JMPREL(lmp))
24217c478bd9Sstevel@tonic-gate 		RELENT(lmp) = sizeof (Rel);
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate 	/*
24247c478bd9Sstevel@tonic-gate 	 * Establish any per-object auditing.  If we're establishing `main's
24257c478bd9Sstevel@tonic-gate 	 * link-map its too early to go searching for audit objects so just
24267c478bd9Sstevel@tonic-gate 	 * hold the object name for later (see setup()).
24277c478bd9Sstevel@tonic-gate 	 */
24287c478bd9Sstevel@tonic-gate 	if (audit) {
24297c478bd9Sstevel@tonic-gate 		char	*cp = audit + (char *)STRTAB(lmp);
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate 		if (*cp) {
24327c478bd9Sstevel@tonic-gate 			if (((AUDITORS(lmp) =
24337c478bd9Sstevel@tonic-gate 			    calloc(1, sizeof (Audit_desc))) == 0) ||
24347c478bd9Sstevel@tonic-gate 			    ((AUDITORS(lmp)->ad_name = strdup(cp)) == 0)) {
24357c478bd9Sstevel@tonic-gate 				remove_so(0, lmp);
24367c478bd9Sstevel@tonic-gate 				return (0);
24377c478bd9Sstevel@tonic-gate 			}
243841072f3cSrie 			if (lml_main.lm_head) {
243956d7adc6Srie 				if (audit_setup(lmp, AUDITORS(lmp), 0) == 0) {
24407c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
24417c478bd9Sstevel@tonic-gate 					return (0);
24427c478bd9Sstevel@tonic-gate 				}
24437c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= AUDITORS(lmp)->ad_flags;
24447c478bd9Sstevel@tonic-gate 				lml->lm_flags |= LML_FLG_LOCAUDIT;
24457c478bd9Sstevel@tonic-gate 			}
24467c478bd9Sstevel@tonic-gate 		}
24477c478bd9Sstevel@tonic-gate 	}
24487c478bd9Sstevel@tonic-gate 
24497c478bd9Sstevel@tonic-gate 	if ((CONDVAR(lmp) = rt_cond_create()) == 0) {
24507c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
24517c478bd9Sstevel@tonic-gate 		return (0);
24527c478bd9Sstevel@tonic-gate 	}
24537c478bd9Sstevel@tonic-gate 	if (oname && ((append_alias(lmp, oname, 0)) == 0)) {
24547c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
24557c478bd9Sstevel@tonic-gate 		return (0);
24567c478bd9Sstevel@tonic-gate 	}
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	/*
24597c478bd9Sstevel@tonic-gate 	 * Add the mapped object to the end of the link map list.
24607c478bd9Sstevel@tonic-gate 	 */
24617c478bd9Sstevel@tonic-gate 	lm_append(lml, lmco, lmp);
24627c478bd9Sstevel@tonic-gate 	return (lmp);
24637c478bd9Sstevel@tonic-gate }
24647c478bd9Sstevel@tonic-gate 
24657c478bd9Sstevel@tonic-gate /*
24667c478bd9Sstevel@tonic-gate  * Assign hardware/software capabilities.
24677c478bd9Sstevel@tonic-gate  */
24687c478bd9Sstevel@tonic-gate void
24697c478bd9Sstevel@tonic-gate cap_assign(Cap *cap, Rt_map *lmp)
24707c478bd9Sstevel@tonic-gate {
24717c478bd9Sstevel@tonic-gate 	while (cap->c_tag != CA_SUNW_NULL) {
24727c478bd9Sstevel@tonic-gate 		switch (cap->c_tag) {
24737c478bd9Sstevel@tonic-gate 		case CA_SUNW_HW_1:
24747c478bd9Sstevel@tonic-gate 			HWCAP(lmp) = cap->c_un.c_val;
24757c478bd9Sstevel@tonic-gate 			break;
24767c478bd9Sstevel@tonic-gate 		case CA_SUNW_SF_1:
24777c478bd9Sstevel@tonic-gate 			SFCAP(lmp) = cap->c_un.c_val;
24787c478bd9Sstevel@tonic-gate 		}
24797c478bd9Sstevel@tonic-gate 		cap++;
24807c478bd9Sstevel@tonic-gate 	}
24817c478bd9Sstevel@tonic-gate }
24827c478bd9Sstevel@tonic-gate 
24837c478bd9Sstevel@tonic-gate /*
24847c478bd9Sstevel@tonic-gate  * Map in an ELF object.
24857c478bd9Sstevel@tonic-gate  * Takes an open file descriptor for the object to map and its pathname; returns
24867c478bd9Sstevel@tonic-gate  * a pointer to a Rt_map structure for this object, or 0 on error.
24877c478bd9Sstevel@tonic-gate  */
24887c478bd9Sstevel@tonic-gate static Rt_map *
24897c478bd9Sstevel@tonic-gate elf_map_so(Lm_list *lml, Aliste lmco, const char *pname, const char *oname,
24907c478bd9Sstevel@tonic-gate     int fd)
24917c478bd9Sstevel@tonic-gate {
24927c478bd9Sstevel@tonic-gate 	int		i; 		/* general temporary */
24937c478bd9Sstevel@tonic-gate 	Off		memsize = 0;	/* total memory size of pathname */
24947c478bd9Sstevel@tonic-gate 	Off		mentry;		/* entry point */
24957c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr;		/* ELF header of ld.so */
24967c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* first Phdr in file */
24977c478bd9Sstevel@tonic-gate 	Phdr		*phdr0;		/* Saved first Phdr in file */
24987c478bd9Sstevel@tonic-gate 	Phdr		*pptr;		/* working Phdr */
24997c478bd9Sstevel@tonic-gate 	Phdr		*fph = 0;	/* first loadable Phdr */
25007c478bd9Sstevel@tonic-gate 	Phdr		*lph;		/* last loadable Phdr */
25017c478bd9Sstevel@tonic-gate 	Phdr		*lfph = 0;	/* last loadable (filesz != 0) Phdr */
25027c478bd9Sstevel@tonic-gate 	Phdr		*lmph = 0;	/* last loadable (memsz != 0) Phdr */
25037c478bd9Sstevel@tonic-gate 	Phdr		*swph = 0;	/* program header for SUNWBSS */
25047c478bd9Sstevel@tonic-gate 	Phdr		*tlph = 0;	/* program header for PT_TLS */
25057c478bd9Sstevel@tonic-gate 	Phdr		*unwindph = 0;	/* program header for PT_SUNW_UNWIND */
25067c478bd9Sstevel@tonic-gate 	Cap		*cap = 0;	/* program header for SUNWCAP */
25077c478bd9Sstevel@tonic-gate 	Dyn		*mld = 0;	/* DYNAMIC structure for pathname */
25087c478bd9Sstevel@tonic-gate 	size_t		size;		/* size of elf and program headers */
25097c478bd9Sstevel@tonic-gate 	caddr_t		faddr = 0;	/* mapping address of pathname */
25107c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;		/* link map created */
25117c478bd9Sstevel@tonic-gate 	caddr_t		paddr;		/* start of padded image */
25127c478bd9Sstevel@tonic-gate 	Off		plen;		/* size of image including padding */
25137c478bd9Sstevel@tonic-gate 	Half		etype;
25147c478bd9Sstevel@tonic-gate 	int		fixed;
25157c478bd9Sstevel@tonic-gate 	Mmap		*mmaps;
25167c478bd9Sstevel@tonic-gate 	uint_t		mmapcnt = 0;
25177c478bd9Sstevel@tonic-gate 	Xword		align = 0;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	/* LINTED */
25207c478bd9Sstevel@tonic-gate 	ehdr = (Ehdr *)fmap->fm_maddr;
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 	/*
25237c478bd9Sstevel@tonic-gate 	 * If this a relocatable object then special processing is required.
25247c478bd9Sstevel@tonic-gate 	 */
25257c478bd9Sstevel@tonic-gate 	if ((etype = ehdr->e_type) == ET_REL)
252641072f3cSrie 		return (elf_obj_file(lml, lmco, pname, fd));
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	/*
25297c478bd9Sstevel@tonic-gate 	 * If this isn't a dynamic executable or shared object we can't process
25307c478bd9Sstevel@tonic-gate 	 * it.  If this is a dynamic executable then all addresses are fixed.
25317c478bd9Sstevel@tonic-gate 	 */
25327c478bd9Sstevel@tonic-gate 	if (etype == ET_EXEC)
25337c478bd9Sstevel@tonic-gate 		fixed = 1;
25347c478bd9Sstevel@tonic-gate 	else if (etype == ET_DYN)
25357c478bd9Sstevel@tonic-gate 		fixed = 0;
25367c478bd9Sstevel@tonic-gate 	else {
253741072f3cSrie 		eprintf(ERR_ELF, MSG_INTL(MSG_GEN_BADTYPE), pname,
25387c478bd9Sstevel@tonic-gate 		    conv_etype_str(etype));
25397c478bd9Sstevel@tonic-gate 		return (0);
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	/*
25437c478bd9Sstevel@tonic-gate 	 * If our original mapped page was not large enough to hold all the
25447c478bd9Sstevel@tonic-gate 	 * program headers remap them.
25457c478bd9Sstevel@tonic-gate 	 */
25467c478bd9Sstevel@tonic-gate 	size = (size_t)((char *)ehdr->e_phoff +
25477c478bd9Sstevel@tonic-gate 		(ehdr->e_phnum * ehdr->e_phentsize));
25487c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_fsize) {
254941072f3cSrie 		eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
25507c478bd9Sstevel@tonic-gate 		return (0);
25517c478bd9Sstevel@tonic-gate 	}
25527c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_msize) {
25537c478bd9Sstevel@tonic-gate 		fmap_setup();
25547c478bd9Sstevel@tonic-gate 		if ((fmap->fm_maddr = mmap(fmap->fm_maddr, size, PROT_READ,
25557c478bd9Sstevel@tonic-gate 		    fmap->fm_mflags, fd, 0)) == MAP_FAILED) {
25567c478bd9Sstevel@tonic-gate 			int	err = errno;
255741072f3cSrie 			eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), pname,
25587c478bd9Sstevel@tonic-gate 			    strerror(err));
25597c478bd9Sstevel@tonic-gate 			return (0);
25607c478bd9Sstevel@tonic-gate 		}
25617c478bd9Sstevel@tonic-gate 		fmap->fm_msize = size;
25627c478bd9Sstevel@tonic-gate 		/* LINTED */
25637c478bd9Sstevel@tonic-gate 		ehdr = (Ehdr *)fmap->fm_maddr;
25647c478bd9Sstevel@tonic-gate 	}
25657c478bd9Sstevel@tonic-gate 	/* LINTED */
25667c478bd9Sstevel@tonic-gate 	phdr0 = phdr = (Phdr *)((char *)ehdr + ehdr->e_ehsize);
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate 	/*
25697c478bd9Sstevel@tonic-gate 	 * Get entry point.
25707c478bd9Sstevel@tonic-gate 	 */
25717c478bd9Sstevel@tonic-gate 	mentry = ehdr->e_entry;
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	/*
25747c478bd9Sstevel@tonic-gate 	 * Point at program headers and perform some basic validation.
25757c478bd9Sstevel@tonic-gate 	 */
25767c478bd9Sstevel@tonic-gate 	for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
25777c478bd9Sstevel@tonic-gate 	    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
25787c478bd9Sstevel@tonic-gate 		if ((pptr->p_type == PT_LOAD) ||
25797c478bd9Sstevel@tonic-gate 		    (pptr->p_type == PT_SUNWBSS)) {
25807c478bd9Sstevel@tonic-gate 
25817c478bd9Sstevel@tonic-gate 			if (fph == 0) {
25827c478bd9Sstevel@tonic-gate 				fph = pptr;
25837c478bd9Sstevel@tonic-gate 			/* LINTED argument lph is initialized in first pass */
25847c478bd9Sstevel@tonic-gate 			} else if (pptr->p_vaddr <= lph->p_vaddr) {
25857c478bd9Sstevel@tonic-gate 				eprintf(ERR_ELF, MSG_INTL(MSG_GEN_INVPRGHDR),
258641072f3cSrie 				    pname);
25877c478bd9Sstevel@tonic-gate 				return (0);
25887c478bd9Sstevel@tonic-gate 			}
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 			lph = pptr;
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate 			if (pptr->p_memsz)
25937c478bd9Sstevel@tonic-gate 				lmph = pptr;
25947c478bd9Sstevel@tonic-gate 			if (pptr->p_filesz)
25957c478bd9Sstevel@tonic-gate 				lfph = pptr;
25967c478bd9Sstevel@tonic-gate 			if (pptr->p_type == PT_SUNWBSS)
25977c478bd9Sstevel@tonic-gate 				swph = pptr;
25987c478bd9Sstevel@tonic-gate 			if (pptr->p_align > align)
25997c478bd9Sstevel@tonic-gate 				align = pptr->p_align;
26007c478bd9Sstevel@tonic-gate 
26017c478bd9Sstevel@tonic-gate 		} else if (pptr->p_type == PT_DYNAMIC)
26027c478bd9Sstevel@tonic-gate 			mld = (Dyn *)(pptr->p_vaddr);
26037c478bd9Sstevel@tonic-gate 		else if (pptr->p_type == PT_TLS)
26047c478bd9Sstevel@tonic-gate 			tlph = pptr;
26057c478bd9Sstevel@tonic-gate 		else if (pptr->p_type == PT_SUNWCAP)
26067c478bd9Sstevel@tonic-gate 			cap = (Cap *)(pptr->p_vaddr);
26077c478bd9Sstevel@tonic-gate 		else if (pptr->p_type == PT_SUNW_UNWIND)
26087c478bd9Sstevel@tonic-gate 			unwindph = pptr;
26097c478bd9Sstevel@tonic-gate 	}
26107c478bd9Sstevel@tonic-gate 
26117c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
26127c478bd9Sstevel@tonic-gate 	/*
26137c478bd9Sstevel@tonic-gate 	 * Make sure the maximum page alignment is a power of 2 >= the system
26147c478bd9Sstevel@tonic-gate 	 * page size, for use with MAP_ALIGN.
26157c478bd9Sstevel@tonic-gate 	 */
26167c478bd9Sstevel@tonic-gate 	align = M_PROUND(align);
26177c478bd9Sstevel@tonic-gate #endif
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 	/*
26207c478bd9Sstevel@tonic-gate 	 * We'd better have at least one loadable segment, together with some
26217c478bd9Sstevel@tonic-gate 	 * specified file and memory size.
26227c478bd9Sstevel@tonic-gate 	 */
26237c478bd9Sstevel@tonic-gate 	if ((fph == 0) || (lmph == 0) || (lfph == 0)) {
262441072f3cSrie 		eprintf(ERR_ELF, MSG_INTL(MSG_GEN_NOLOADSEG), pname);
26257c478bd9Sstevel@tonic-gate 		return (0);
26267c478bd9Sstevel@tonic-gate 	}
26277c478bd9Sstevel@tonic-gate 
26287c478bd9Sstevel@tonic-gate 	/*
26297c478bd9Sstevel@tonic-gate 	 * Check that the files size accounts for the loadable sections
26307c478bd9Sstevel@tonic-gate 	 * we're going to map in (failure to do this may cause spurious
26317c478bd9Sstevel@tonic-gate 	 * bus errors if we're given a truncated file).
26327c478bd9Sstevel@tonic-gate 	 */
26337c478bd9Sstevel@tonic-gate 	if (fmap->fm_fsize < ((size_t)lfph->p_offset + lfph->p_filesz)) {
263441072f3cSrie 		eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), pname);
26357c478bd9Sstevel@tonic-gate 		return (0);
26367c478bd9Sstevel@tonic-gate 	}
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate 	/*
26397c478bd9Sstevel@tonic-gate 	 * Memsize must be page rounded so that if we add object padding
26407c478bd9Sstevel@tonic-gate 	 * at the end it will start at the beginning of a page.
26417c478bd9Sstevel@tonic-gate 	 */
26427c478bd9Sstevel@tonic-gate 	plen = memsize = M_PROUND((lmph->p_vaddr + lmph->p_memsz) -
26437c478bd9Sstevel@tonic-gate 	    M_PTRUNC((ulong_t)fph->p_vaddr));
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	/*
26467c478bd9Sstevel@tonic-gate 	 * Determine if an existing mapping is acceptable.
26477c478bd9Sstevel@tonic-gate 	 */
26487c478bd9Sstevel@tonic-gate 	if (interp && (lml->lm_flags & LML_FLG_BASELM) &&
264941072f3cSrie 	    (strcmp(pname, interp->i_name) == 0)) {
26507c478bd9Sstevel@tonic-gate 		/*
26517c478bd9Sstevel@tonic-gate 		 * If this is the interpreter then it has already been mapped
26527c478bd9Sstevel@tonic-gate 		 * and we have the address so don't map it again.  Note that
26537c478bd9Sstevel@tonic-gate 		 * the common occurrence of a reference to the interpretor
26547c478bd9Sstevel@tonic-gate 		 * (libdl -> ld.so.1) will have been caught during filter
26557c478bd9Sstevel@tonic-gate 		 * initialization (see elf_lookup_filtee()).  However, some
26567c478bd9Sstevel@tonic-gate 		 * ELF implementations are known to record libc.so.1 as the
26577c478bd9Sstevel@tonic-gate 		 * interpretor, and thus this test catches this behavior.
26587c478bd9Sstevel@tonic-gate 		 */
26597c478bd9Sstevel@tonic-gate 		paddr = faddr = interp->i_faddr;
26607c478bd9Sstevel@tonic-gate 
26617c478bd9Sstevel@tonic-gate 	} else if ((fixed == 0) && (r_debug.rtd_objpad == 0) &&
26627c478bd9Sstevel@tonic-gate 	    (memsize <= fmap->fm_msize) && ((fph->p_flags & PF_W) == 0) &&
26637c478bd9Sstevel@tonic-gate 	    (fph->p_filesz == fph->p_memsz) &&
26647c478bd9Sstevel@tonic-gate 	    (((Xword)fmap->fm_maddr % align) == 0)) {
26657c478bd9Sstevel@tonic-gate 		/*
26667c478bd9Sstevel@tonic-gate 		 * If the mapping required has already been established from
26677c478bd9Sstevel@tonic-gate 		 * the initial page we don't need to do anything more.  Reset
26687c478bd9Sstevel@tonic-gate 		 * the fmap address so then any later files start a new fmap.
26697c478bd9Sstevel@tonic-gate 		 * This is really an optimization for filters, such as libdl.so,
26707c478bd9Sstevel@tonic-gate 		 * which should only require one page.
26717c478bd9Sstevel@tonic-gate 		 */
26727c478bd9Sstevel@tonic-gate 		paddr = faddr = fmap->fm_maddr;
26737c478bd9Sstevel@tonic-gate 		fmap->fm_maddr = 0;
26747c478bd9Sstevel@tonic-gate 		fmap_setup();
26757c478bd9Sstevel@tonic-gate 	}
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate 	/*
26787c478bd9Sstevel@tonic-gate 	 * Allocate a mapping array to retain mapped segment information.
26797c478bd9Sstevel@tonic-gate 	 */
26807c478bd9Sstevel@tonic-gate 	if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
26817c478bd9Sstevel@tonic-gate 		return (0);
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 	/*
26847c478bd9Sstevel@tonic-gate 	 * If we're reusing an existing mapping determine the objects etext
26857c478bd9Sstevel@tonic-gate 	 * address.  Otherwise map the file (which will calculate the etext
26867c478bd9Sstevel@tonic-gate 	 * address as part of the mapping process).
26877c478bd9Sstevel@tonic-gate 	 */
26887c478bd9Sstevel@tonic-gate 	if (faddr) {
26897c478bd9Sstevel@tonic-gate 		caddr_t	base;
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate 		if (fixed)
26927c478bd9Sstevel@tonic-gate 			base = 0;
26937c478bd9Sstevel@tonic-gate 		else
26947c478bd9Sstevel@tonic-gate 			base = faddr;
26957c478bd9Sstevel@tonic-gate 
26967c478bd9Sstevel@tonic-gate 		/* LINTED */
26977c478bd9Sstevel@tonic-gate 		phdr0 = phdr = (Phdr *)((char *)faddr + ehdr->e_ehsize);
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 		for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
27007c478bd9Sstevel@tonic-gate 		    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
27017c478bd9Sstevel@tonic-gate 			if (pptr->p_type != PT_LOAD)
27027c478bd9Sstevel@tonic-gate 				continue;
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_vaddr = (pptr->p_vaddr + base);
27057c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_msize = pptr->p_memsz;
27067c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_fsize = pptr->p_filesz;
27077c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_perm = (PROT_READ | PROT_EXEC);
27087c478bd9Sstevel@tonic-gate 			mmapcnt++;
27097c478bd9Sstevel@tonic-gate 
27107c478bd9Sstevel@tonic-gate 			if (!(pptr->p_flags & PF_W)) {
27117c478bd9Sstevel@tonic-gate 				fmap->fm_etext = (ulong_t)pptr->p_vaddr +
27127c478bd9Sstevel@tonic-gate 				    (ulong_t)pptr->p_memsz +
27137c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
27147c478bd9Sstevel@tonic-gate 			}
27157c478bd9Sstevel@tonic-gate 		}
27167c478bd9Sstevel@tonic-gate 	} else {
27177c478bd9Sstevel@tonic-gate 		/*
27187c478bd9Sstevel@tonic-gate 		 * Map the file.
27197c478bd9Sstevel@tonic-gate 		 */
272041072f3cSrie 		if (!(faddr = elf_map_it(pname, memsize, ehdr, fph, lph,
27217c478bd9Sstevel@tonic-gate 		    &phdr, &paddr, &plen, fixed, fd, align, mmaps, &mmapcnt)))
27227c478bd9Sstevel@tonic-gate 			return (0);
27237c478bd9Sstevel@tonic-gate 	}
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	/*
27267c478bd9Sstevel@tonic-gate 	 * Calculate absolute base addresses and entry points.
27277c478bd9Sstevel@tonic-gate 	 */
27287c478bd9Sstevel@tonic-gate 	if (!fixed) {
27297c478bd9Sstevel@tonic-gate 		if (mld)
27307c478bd9Sstevel@tonic-gate 			/* LINTED */
27317c478bd9Sstevel@tonic-gate 			mld = (Dyn *)((Off)mld + faddr);
27327c478bd9Sstevel@tonic-gate 		if (cap)
27337c478bd9Sstevel@tonic-gate 			/* LINTED */
27347c478bd9Sstevel@tonic-gate 			cap = (Cap *)((Off)cap + faddr);
27357c478bd9Sstevel@tonic-gate 		mentry += (Off)faddr;
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	/*
27397c478bd9Sstevel@tonic-gate 	 * Create new link map structure for newly mapped shared object.
27407c478bd9Sstevel@tonic-gate 	 */
27417c478bd9Sstevel@tonic-gate 	if (!(lmp = elf_new_lm(lml, pname, oname, mld, (ulong_t)faddr,
27427c478bd9Sstevel@tonic-gate 	    fmap->fm_etext, lmco, memsize, mentry, (ulong_t)paddr, plen, mmaps,
27437c478bd9Sstevel@tonic-gate 	    mmapcnt))) {
27447c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)faddr, memsize);
27457c478bd9Sstevel@tonic-gate 		return (0);
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	/*
27497c478bd9Sstevel@tonic-gate 	 * Start the system loading in the ELF information we'll be processing.
27507c478bd9Sstevel@tonic-gate 	 */
27517c478bd9Sstevel@tonic-gate 	if (REL(lmp)) {
27527c478bd9Sstevel@tonic-gate 		(void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) +
27537c478bd9Sstevel@tonic-gate 		    (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp),
27547c478bd9Sstevel@tonic-gate 		    MADV_WILLNEED);
27557c478bd9Sstevel@tonic-gate 	}
27567c478bd9Sstevel@tonic-gate 
27577c478bd9Sstevel@tonic-gate 	/*
27587c478bd9Sstevel@tonic-gate 	 * If this shared object contains a any special segments, record them.
27597c478bd9Sstevel@tonic-gate 	 */
27607c478bd9Sstevel@tonic-gate 	if (swph) {
27617c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_SUNWBSS;
27627c478bd9Sstevel@tonic-gate 		SUNWBSS(lmp) = phdr + (swph - phdr0);
27637c478bd9Sstevel@tonic-gate 	}
27647c478bd9Sstevel@tonic-gate 	if (tlph) {
27657c478bd9Sstevel@tonic-gate 		PTTLS(lmp) = phdr + (tlph - phdr0);
27667c478bd9Sstevel@tonic-gate 		tls_assign_soffset(lmp);
27677c478bd9Sstevel@tonic-gate 	}
27687c478bd9Sstevel@tonic-gate 
27697c478bd9Sstevel@tonic-gate 	if (unwindph)
27707c478bd9Sstevel@tonic-gate 		PTUNWIND(lmp) = phdr + (unwindph - phdr0);
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 	if (cap)
27737c478bd9Sstevel@tonic-gate 		cap_assign(cap, lmp);
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	return (lmp);
27767c478bd9Sstevel@tonic-gate }
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate /*
27807c478bd9Sstevel@tonic-gate  * Function to correct protection settings.  Segments are all mapped initially
27817c478bd9Sstevel@tonic-gate  * with permissions as given in the segment header.  We need to turn on write
27827c478bd9Sstevel@tonic-gate  * permissions on a text segment if there are any relocations against that
27837c478bd9Sstevel@tonic-gate  * segment, and them turn write permission back off again before returning
27847c478bd9Sstevel@tonic-gate  * control to the user.  This function turns the permission on or off depending
27857c478bd9Sstevel@tonic-gate  * on the value of the argument.
27867c478bd9Sstevel@tonic-gate  */
27877c478bd9Sstevel@tonic-gate int
27887c478bd9Sstevel@tonic-gate elf_set_prot(Rt_map * lmp, int permission)
27897c478bd9Sstevel@tonic-gate {
27907c478bd9Sstevel@tonic-gate 	Mmap	*mmaps;
27917c478bd9Sstevel@tonic-gate 
27927c478bd9Sstevel@tonic-gate 	/*
27937c478bd9Sstevel@tonic-gate 	 * If this is an allocated image (ie. a relocatable object) we can't
27947c478bd9Sstevel@tonic-gate 	 * mprotect() anything.
27957c478bd9Sstevel@tonic-gate 	 */
27967c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
27977c478bd9Sstevel@tonic-gate 		return (1);
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_file_prot(NAME(lmp), permission));
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++) {
28027c478bd9Sstevel@tonic-gate 		if (mmaps->m_perm & PROT_WRITE)
28037c478bd9Sstevel@tonic-gate 			continue;
28047c478bd9Sstevel@tonic-gate 
28057c478bd9Sstevel@tonic-gate 		if (mprotect(mmaps->m_vaddr, mmaps->m_msize,
28067c478bd9Sstevel@tonic-gate 		    (mmaps->m_perm | permission)) == -1) {
28077c478bd9Sstevel@tonic-gate 			int	err = errno;
28087c478bd9Sstevel@tonic-gate 			eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
28097c478bd9Sstevel@tonic-gate 			    NAME(lmp), strerror(err));
28107c478bd9Sstevel@tonic-gate 			return (0);
28117c478bd9Sstevel@tonic-gate 		}
28127c478bd9Sstevel@tonic-gate 	}
28137c478bd9Sstevel@tonic-gate 	return (1);
28147c478bd9Sstevel@tonic-gate }
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate /*
28177c478bd9Sstevel@tonic-gate  * Build full pathname of shared object from given directory name and filename.
28187c478bd9Sstevel@tonic-gate  */
28197c478bd9Sstevel@tonic-gate static char *
28207c478bd9Sstevel@tonic-gate elf_get_so(const char *dir, const char *file)
28217c478bd9Sstevel@tonic-gate {
28227c478bd9Sstevel@tonic-gate 	static char	pname[PATH_MAX];
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, MSG_ORIG(MSG_FMT_PATH), dir, file);
28257c478bd9Sstevel@tonic-gate 	return (pname);
28267c478bd9Sstevel@tonic-gate }
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate /*
28297c478bd9Sstevel@tonic-gate  * The copy relocation is recorded in a copy structure which will be applied
28307c478bd9Sstevel@tonic-gate  * after all other relocations are carried out.  This provides for copying data
28317c478bd9Sstevel@tonic-gate  * that must be relocated itself (ie. pointers in shared objects).  This
28327c478bd9Sstevel@tonic-gate  * structure also provides a means of binding RTLD_GROUP dependencies to any
28337c478bd9Sstevel@tonic-gate  * copy relocations that have been taken from any group members.
28347c478bd9Sstevel@tonic-gate  *
28357c478bd9Sstevel@tonic-gate  * If the size of the .bss area available for the copy information is not the
28367c478bd9Sstevel@tonic-gate  * same as the source of the data inform the user if we're under ldd(1) control
28377c478bd9Sstevel@tonic-gate  * (this checking was only established in 5.3, so by only issuing an error via
28387c478bd9Sstevel@tonic-gate  * ldd(1) we maintain the standard set by previous releases).
28397c478bd9Sstevel@tonic-gate  */
28407c478bd9Sstevel@tonic-gate int
28417c478bd9Sstevel@tonic-gate elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym,
28427c478bd9Sstevel@tonic-gate     Rt_map *dlmp, const void *dadd)
28437c478bd9Sstevel@tonic-gate {
28447c478bd9Sstevel@tonic-gate 	Rel_copy	rc;
28457c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(rlmp);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	rc.r_name = name;
28487c478bd9Sstevel@tonic-gate 	rc.r_rsym = rsym;		/* the new reference symbol and its */
28497c478bd9Sstevel@tonic-gate 	rc.r_rlmp = rlmp;		/*	associated link-map */
28507c478bd9Sstevel@tonic-gate 	rc.r_dlmp = dlmp;		/* the defining link-map */
28517c478bd9Sstevel@tonic-gate 	rc.r_dsym = dsym;		/* the original definition */
28527c478bd9Sstevel@tonic-gate 	rc.r_radd = radd;
28537c478bd9Sstevel@tonic-gate 	rc.r_dadd = dadd;
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	if (rsym->st_size > dsym->st_size)
28567c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)dsym->st_size;
28577c478bd9Sstevel@tonic-gate 	else
28587c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)rsym->st_size;
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 	if (alist_append(&COPY(dlmp), &rc, sizeof (Rel_copy),
28617c478bd9Sstevel@tonic-gate 	    AL_CNT_COPYREL) == 0) {
28627c478bd9Sstevel@tonic-gate 		if (!(lml->lm_flags & LML_FLG_TRC_WARN))
28637c478bd9Sstevel@tonic-gate 			return (0);
28647c478bd9Sstevel@tonic-gate 		else
28657c478bd9Sstevel@tonic-gate 			return (1);
28667c478bd9Sstevel@tonic-gate 	}
28677c478bd9Sstevel@tonic-gate 	if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) {
28687c478bd9Sstevel@tonic-gate 		if (alist_append(&COPY(rlmp), &dlmp,
28697c478bd9Sstevel@tonic-gate 		    sizeof (Rt_map *), AL_CNT_COPYREL) == 0) {
28707c478bd9Sstevel@tonic-gate 			if (!(lml->lm_flags & LML_FLG_TRC_WARN))
28717c478bd9Sstevel@tonic-gate 				return (0);
28727c478bd9Sstevel@tonic-gate 			else
28737c478bd9Sstevel@tonic-gate 				return (1);
28747c478bd9Sstevel@tonic-gate 		}
28757c478bd9Sstevel@tonic-gate 		FLAGS1(dlmp) |= FL1_RT_COPYTOOK;
28767c478bd9Sstevel@tonic-gate 	}
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	/*
28797c478bd9Sstevel@tonic-gate 	 * If we are tracing (ldd), warn the user if
28807c478bd9Sstevel@tonic-gate 	 *	1) the size from the reference symbol differs from the
28817c478bd9Sstevel@tonic-gate 	 *	   copy definition. We can only copy as much data as the
28827c478bd9Sstevel@tonic-gate 	 *	   reference (dynamic executables) entry allows.
28837c478bd9Sstevel@tonic-gate 	 *	2) the copy definition has STV_PROTECTED visibility.
28847c478bd9Sstevel@tonic-gate 	 */
28857c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
28867c478bd9Sstevel@tonic-gate 		if (rsym->st_size != dsym->st_size) {
28877c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF),
28887c478bd9Sstevel@tonic-gate 			    _conv_reloc_type_str(M_R_COPY), demangle(name),
28897c478bd9Sstevel@tonic-gate 			    NAME(rlmp), EC_XWORD(rsym->st_size),
28907c478bd9Sstevel@tonic-gate 			    NAME(dlmp), EC_XWORD(dsym->st_size));
28917c478bd9Sstevel@tonic-gate 			if (rsym->st_size > dsym->st_size)
28927c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA),
28937c478bd9Sstevel@tonic-gate 				    NAME(dlmp));
28947c478bd9Sstevel@tonic-gate 			else
28957c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC),
28967c478bd9Sstevel@tonic-gate 				    NAME(rlmp));
28977c478bd9Sstevel@tonic-gate 		}
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate 		if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) {
29007c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_PROT),
29017c478bd9Sstevel@tonic-gate 			    _conv_reloc_type_str(M_R_COPY), demangle(name),
29027c478bd9Sstevel@tonic-gate 				NAME(dlmp));
29037c478bd9Sstevel@tonic-gate 		}
29047c478bd9Sstevel@tonic-gate 	}
29057c478bd9Sstevel@tonic-gate 
29067c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_reloc_apply((Xword)radd, (Xword)rc.r_size));
29077c478bd9Sstevel@tonic-gate 	return (1);
29087c478bd9Sstevel@tonic-gate }
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate /*
29117c478bd9Sstevel@tonic-gate  * Determine the symbol location of an address within a link-map.  Look for
29127c478bd9Sstevel@tonic-gate  * the nearest symbol (whose value is less than or equal to the required
29137c478bd9Sstevel@tonic-gate  * address).  This is the object specific part of dladdr().
29147c478bd9Sstevel@tonic-gate  */
29157c478bd9Sstevel@tonic-gate static void
29167c478bd9Sstevel@tonic-gate elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags)
29177c478bd9Sstevel@tonic-gate {
29187c478bd9Sstevel@tonic-gate 	ulong_t		ndx, cnt, base, _value;
29197c478bd9Sstevel@tonic-gate 	Sym		*sym, *_sym;
29207c478bd9Sstevel@tonic-gate 	const char	*str;
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	/*
29237c478bd9Sstevel@tonic-gate 	 * If we don't have a .hash table there are no symbols to look at.
29247c478bd9Sstevel@tonic-gate 	 */
29257c478bd9Sstevel@tonic-gate 	if (HASH(lmp) == 0)
29267c478bd9Sstevel@tonic-gate 		return;
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	cnt = HASH(lmp)[1];
29297c478bd9Sstevel@tonic-gate 	str = STRTAB(lmp);
29307c478bd9Sstevel@tonic-gate 	sym = SYMTAB(lmp);
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_FIXED)
29337c478bd9Sstevel@tonic-gate 		base = 0;
29347c478bd9Sstevel@tonic-gate 	else
29357c478bd9Sstevel@tonic-gate 		base = ADDR(lmp);
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate 	for (_sym = 0, _value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) {
29387c478bd9Sstevel@tonic-gate 		ulong_t	value;
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 		if (sym->st_shndx == SHN_UNDEF)
29417c478bd9Sstevel@tonic-gate 			continue;
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate 		value = sym->st_value + base;
29447c478bd9Sstevel@tonic-gate 		if (value > addr)
29457c478bd9Sstevel@tonic-gate 			continue;
29467c478bd9Sstevel@tonic-gate 		if (value < _value)
29477c478bd9Sstevel@tonic-gate 			continue;
29487c478bd9Sstevel@tonic-gate 
29497c478bd9Sstevel@tonic-gate 		_sym = sym;
29507c478bd9Sstevel@tonic-gate 		_value = value;
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 		/*
29537c478bd9Sstevel@tonic-gate 		 * Note, because we accept local and global symbols we could
29547c478bd9Sstevel@tonic-gate 		 * find a section symbol that matches the associated address,
29557c478bd9Sstevel@tonic-gate 		 * which means that the symbol name will be null.  In this
29567c478bd9Sstevel@tonic-gate 		 * case continue the search in case we can find a global
29577c478bd9Sstevel@tonic-gate 		 * symbol of the same value.
29587c478bd9Sstevel@tonic-gate 		 */
29597c478bd9Sstevel@tonic-gate 		if ((value == addr) &&
29607c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) != STT_SECTION))
29617c478bd9Sstevel@tonic-gate 			break;
29627c478bd9Sstevel@tonic-gate 	}
29637c478bd9Sstevel@tonic-gate 
29647c478bd9Sstevel@tonic-gate 	if (_sym) {
29657c478bd9Sstevel@tonic-gate 		int	_flags = flags & RTLD_DL_MASK;
29667c478bd9Sstevel@tonic-gate 
29677c478bd9Sstevel@tonic-gate 		if (_flags == RTLD_DL_SYMENT)
29687c478bd9Sstevel@tonic-gate 			*info = (void *)_sym;
29697c478bd9Sstevel@tonic-gate 		else if (_flags == RTLD_DL_LINKMAP)
29707c478bd9Sstevel@tonic-gate 			*info = (void *)lmp;
29717c478bd9Sstevel@tonic-gate 
29727c478bd9Sstevel@tonic-gate 		dlip->dli_sname = str + _sym->st_name;
29737c478bd9Sstevel@tonic-gate 		dlip->dli_saddr = (void *)_value;
29747c478bd9Sstevel@tonic-gate 	}
29757c478bd9Sstevel@tonic-gate }
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate static void
29787c478bd9Sstevel@tonic-gate elf_lazy_cleanup(Alist * alp)
29797c478bd9Sstevel@tonic-gate {
29807c478bd9Sstevel@tonic-gate 	Rt_map **	lmpp;
29817c478bd9Sstevel@tonic-gate 	Aliste		off;
29827c478bd9Sstevel@tonic-gate 
29837c478bd9Sstevel@tonic-gate 	/*
29847c478bd9Sstevel@tonic-gate 	 * Cleanup any link-maps added to this dynamic list and free it.
29857c478bd9Sstevel@tonic-gate 	 */
29867c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alp, off, lmpp))
29877c478bd9Sstevel@tonic-gate 		FLAGS(*lmpp) &= ~FLG_RT_DLSYM;
29887c478bd9Sstevel@tonic-gate 	free(alp);
29897c478bd9Sstevel@tonic-gate }
29907c478bd9Sstevel@tonic-gate 
29917c478bd9Sstevel@tonic-gate /*
29927c478bd9Sstevel@tonic-gate  * This routine is called upon to search for a symbol from the dependencies of
29937c478bd9Sstevel@tonic-gate  * the initial link-map.  To maintain lazy loadings goal of reducing the number
29947c478bd9Sstevel@tonic-gate  * of objects mapped, any symbol search is first carried out using the objects
29957c478bd9Sstevel@tonic-gate  * that already exist in the process (either on a link-map list or handle).
29967c478bd9Sstevel@tonic-gate  * If a symbol can't be found, and lazy dependencies are still pending, this
29977c478bd9Sstevel@tonic-gate  * routine loads the dependencies in an attempt to locate the symbol.
29987c478bd9Sstevel@tonic-gate  *
29997c478bd9Sstevel@tonic-gate  * Only new objects are inspected as we will have already inspected presently
30007c478bd9Sstevel@tonic-gate  * loaded objects before calling this routine.  However, a new object may not
30017c478bd9Sstevel@tonic-gate  * be new - although the di_lmp might be zero, the object may have been mapped
30027c478bd9Sstevel@tonic-gate  * as someone elses dependency.  Thus there's a possibility of some symbol
30037c478bd9Sstevel@tonic-gate  * search duplication.
30047c478bd9Sstevel@tonic-gate  */
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate Sym *
30077c478bd9Sstevel@tonic-gate elf_lazy_find_sym(Slookup *slp, Rt_map **_lmp, uint_t *binfo)
30087c478bd9Sstevel@tonic-gate {
30097c478bd9Sstevel@tonic-gate 	Sym		*sym = 0;
30107c478bd9Sstevel@tonic-gate 	Alist *		alist = 0;
30117c478bd9Sstevel@tonic-gate 	Aliste		off;
30127c478bd9Sstevel@tonic-gate 	Rt_map **	lmpp, *	lmp = slp->sl_imap;
30137c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate 	if (alist_append(&alist, &lmp, sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0)
30167c478bd9Sstevel@tonic-gate 		return (0);
30177c478bd9Sstevel@tonic-gate 	FLAGS(lmp) |= FLG_RT_DLSYM;
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alist, off, lmpp)) {
30207c478bd9Sstevel@tonic-gate 		uint_t	cnt = 0;
30217c478bd9Sstevel@tonic-gate 		Slookup	sl = *slp;
30227c478bd9Sstevel@tonic-gate 		Dyninfo	*dip;
30237c478bd9Sstevel@tonic-gate 
30247c478bd9Sstevel@tonic-gate 		/*
30257c478bd9Sstevel@tonic-gate 		 * Loop through the DT_NEEDED entries examining each object for
30267c478bd9Sstevel@tonic-gate 		 * the symbol.  If the symbol is not found the object is in turn
30277c478bd9Sstevel@tonic-gate 		 * added to the alist, so that its DT_NEEDED entires may be
30287c478bd9Sstevel@tonic-gate 		 * examined.
30297c478bd9Sstevel@tonic-gate 		 */
30307c478bd9Sstevel@tonic-gate 		lmp = *lmpp;
30317c478bd9Sstevel@tonic-gate 		for (dip = DYNINFO(lmp); cnt < DYNINFOCNT(lmp); cnt++, dip++) {
30327c478bd9Sstevel@tonic-gate 			Rt_map *nlmp;
30337c478bd9Sstevel@tonic-gate 
30347c478bd9Sstevel@tonic-gate 			if (((dip->di_flags & FLG_DI_NEEDED) == 0) ||
30357c478bd9Sstevel@tonic-gate 			    dip->di_info)
30367c478bd9Sstevel@tonic-gate 				continue;
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 			/*
30397c478bd9Sstevel@tonic-gate 			 * If this entry defines a lazy dependency try loading
30407c478bd9Sstevel@tonic-gate 			 * it.  If the file can't be loaded, consider this
30417c478bd9Sstevel@tonic-gate 			 * non-fatal and continue the search (lazy loaded
30427c478bd9Sstevel@tonic-gate 			 * dependencies need not exist and their loading should
30437c478bd9Sstevel@tonic-gate 			 * only be fatal if called from a relocation).
30447c478bd9Sstevel@tonic-gate 			 *
30457c478bd9Sstevel@tonic-gate 			 * If the file is already loaded and relocated we must
30467c478bd9Sstevel@tonic-gate 			 * still inspect it for symbols, even though it might
30477c478bd9Sstevel@tonic-gate 			 * have already been searched.  This lazy load operation
30487c478bd9Sstevel@tonic-gate 			 * might have promoted the permissions of the object,
30497c478bd9Sstevel@tonic-gate 			 * and thus made the object applicable for this symbol
30507c478bd9Sstevel@tonic-gate 			 * search, whereas before the object might have been
30517c478bd9Sstevel@tonic-gate 			 * skipped.
30527c478bd9Sstevel@tonic-gate 			 */
30537c478bd9Sstevel@tonic-gate 			if ((nlmp = elf_lazy_load(lmp, cnt, name)) == 0)
30547c478bd9Sstevel@tonic-gate 				continue;
30557c478bd9Sstevel@tonic-gate 
30567c478bd9Sstevel@tonic-gate 			/*
30577c478bd9Sstevel@tonic-gate 			 * If this object isn't yet a part of the dynamic list
30587c478bd9Sstevel@tonic-gate 			 * then inspect it for the symbol.  If the symbol isn't
30597c478bd9Sstevel@tonic-gate 			 * found add the object to the dynamic list so that we
30607c478bd9Sstevel@tonic-gate 			 * can inspect its dependencies.
30617c478bd9Sstevel@tonic-gate 			 */
30627c478bd9Sstevel@tonic-gate 			if (FLAGS(nlmp) & FLG_RT_DLSYM)
30637c478bd9Sstevel@tonic-gate 				continue;
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 			sl.sl_imap = nlmp;
30667c478bd9Sstevel@tonic-gate 			if (sym = LM_LOOKUP_SYM(sl.sl_cmap)(&sl, _lmp, binfo))
30677c478bd9Sstevel@tonic-gate 				break;
30687c478bd9Sstevel@tonic-gate 
30697c478bd9Sstevel@tonic-gate 			/*
30707c478bd9Sstevel@tonic-gate 			 * Some dlsym() operations are already traversing a
30717c478bd9Sstevel@tonic-gate 			 * link-map (dlopen(0)), and thus there's no need to
30727c478bd9Sstevel@tonic-gate 			 * build our own dynamic dependency list.
30737c478bd9Sstevel@tonic-gate 			 */
30747c478bd9Sstevel@tonic-gate 			if ((sl.sl_flags & LKUP_NODESCENT) == 0) {
30757c478bd9Sstevel@tonic-gate 				if (alist_append(&alist, &nlmp,
30767c478bd9Sstevel@tonic-gate 				    sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0) {
30777c478bd9Sstevel@tonic-gate 					elf_lazy_cleanup(alist);
30787c478bd9Sstevel@tonic-gate 					return (0);
30797c478bd9Sstevel@tonic-gate 				}
30807c478bd9Sstevel@tonic-gate 				FLAGS(nlmp) |= FLG_RT_DLSYM;
30817c478bd9Sstevel@tonic-gate 			}
30827c478bd9Sstevel@tonic-gate 		}
30837c478bd9Sstevel@tonic-gate 		if (sym)
30847c478bd9Sstevel@tonic-gate 			break;
30857c478bd9Sstevel@tonic-gate 	}
30867c478bd9Sstevel@tonic-gate 
30877c478bd9Sstevel@tonic-gate 	elf_lazy_cleanup(alist);
30887c478bd9Sstevel@tonic-gate 	return (sym);
30897c478bd9Sstevel@tonic-gate }
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate /*
30927c478bd9Sstevel@tonic-gate  * Warning message for bad r_offset.
30937c478bd9Sstevel@tonic-gate  */
30947c478bd9Sstevel@tonic-gate void
30957c478bd9Sstevel@tonic-gate elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset,
30967c478bd9Sstevel@tonic-gate     ulong_t rsymndx)
30977c478bd9Sstevel@tonic-gate {
30987c478bd9Sstevel@tonic-gate 	const char	*name = (char *)0;
30997c478bd9Sstevel@tonic-gate 	int		trace;
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	if ((LIST(lmp)->lm_flags & LML_FLG_TRC_ENABLE) &&
31027c478bd9Sstevel@tonic-gate 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
31037c478bd9Sstevel@tonic-gate 	    (LIST(lmp)->lm_flags & LML_FLG_TRC_VERBOSE)))
31047c478bd9Sstevel@tonic-gate 		trace = 1;
31057c478bd9Sstevel@tonic-gate 	else
31067c478bd9Sstevel@tonic-gate 		trace = 0;
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 	if ((trace == 0) && (dbg_mask == 0))
31097c478bd9Sstevel@tonic-gate 		return;
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 	if (rsymndx) {
31127c478bd9Sstevel@tonic-gate 		Sym	*symref = (Sym *)((ulong_t)SYMTAB(lmp) +
31137c478bd9Sstevel@tonic-gate 				(rsymndx * SYMENT(lmp)));
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(symref->st_info) != STB_LOCAL)
31167c478bd9Sstevel@tonic-gate 			name = (char *)(STRTAB(lmp) + symref->st_name);
31177c478bd9Sstevel@tonic-gate 	}
31187c478bd9Sstevel@tonic-gate 
31197c478bd9Sstevel@tonic-gate 	if (name == 0)
31207c478bd9Sstevel@tonic-gate 		name = MSG_ORIG(MSG_STR_EMPTY);
31217c478bd9Sstevel@tonic-gate 
31227c478bd9Sstevel@tonic-gate 	if (trace) {
31237c478bd9Sstevel@tonic-gate 		const char *rstr;
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 		rstr = _conv_reloc_type_str((uint_t)rtype);
31267c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name,
31277c478bd9Sstevel@tonic-gate 		    EC_ADDR(roffset));
31287c478bd9Sstevel@tonic-gate 		return;
31297c478bd9Sstevel@tonic-gate 	}
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	Dbg_reloc_error(M_MACH, M_REL_SHT_TYPE, rel, name,
31327c478bd9Sstevel@tonic-gate 		MSG_ORIG(MSG_REL_BADROFFSET));
31337c478bd9Sstevel@tonic-gate }
3134