xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/elf.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
24*7c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
25*7c478bd9Sstevel@tonic-gate  *
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  * Object file dependent support for ELF objects.
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate #include	"_synonyms.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include	<stdio.h>
39*7c478bd9Sstevel@tonic-gate #include	<sys/procfs.h>
40*7c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
41*7c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
42*7c478bd9Sstevel@tonic-gate #include	<string.h>
43*7c478bd9Sstevel@tonic-gate #include	<limits.h>
44*7c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
45*7c478bd9Sstevel@tonic-gate #include	"conv.h"
46*7c478bd9Sstevel@tonic-gate #include	"_rtld.h"
47*7c478bd9Sstevel@tonic-gate #include	"_audit.h"
48*7c478bd9Sstevel@tonic-gate #include	"_elf.h"
49*7c478bd9Sstevel@tonic-gate #include	"msg.h"
50*7c478bd9Sstevel@tonic-gate #include	"debug.h"
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Default and secure dependency search paths.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate static Pnode		elf_dflt_dirs[] = {
56*7c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
57*7c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
58*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIB_64),		0,	MSG_PTH_LIB_64_SIZE,
59*7c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
60*7c478bd9Sstevel@tonic-gate #endif
61*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIB_64),		0,	MSG_PTH_USRLIB_64_SIZE,
62*7c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0, 0 }
63*7c478bd9Sstevel@tonic-gate #else
64*7c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
65*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIB),		0,	MSG_PTH_LIB_SIZE,
66*7c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0,	&elf_dflt_dirs[1] },
67*7c478bd9Sstevel@tonic-gate #endif
68*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIB),		0,	MSG_PTH_USRLIB_SIZE,
69*7c478bd9Sstevel@tonic-gate 		LA_SER_DEFAULT,			0, 0 }
70*7c478bd9Sstevel@tonic-gate #endif
71*7c478bd9Sstevel@tonic-gate };
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static Pnode		elf_secure_dirs[] = {
74*7c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
75*7c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
76*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIBSE_64),		0,	MSG_PTH_LIBSE_64_SIZE,
77*7c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
78*7c478bd9Sstevel@tonic-gate #endif
79*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIBSE_64),	0,
80*7c478bd9Sstevel@tonic-gate 		MSG_PTH_USRLIBSE_64_SIZE,
81*7c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0, 0 }
82*7c478bd9Sstevel@tonic-gate #else
83*7c478bd9Sstevel@tonic-gate #ifndef	SGS_PRE_UNIFIED_PROCESS
84*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_LIBSE),		0,	MSG_PTH_LIBSE_SIZE,
85*7c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0,	&elf_secure_dirs[1] },
86*7c478bd9Sstevel@tonic-gate #endif
87*7c478bd9Sstevel@tonic-gate 	{ MSG_ORIG(MSG_PTH_USRLIBSE),		0,	MSG_PTH_USRLIBSE_SIZE,
88*7c478bd9Sstevel@tonic-gate 		LA_SER_SECURE,			0, 0 }
89*7c478bd9Sstevel@tonic-gate #endif
90*7c478bd9Sstevel@tonic-gate };
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate  * Defines for local functions.
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate static Pnode	*elf_fix_name(const char *, Rt_map *, uint_t);
96*7c478bd9Sstevel@tonic-gate static int	elf_are_u(Rej_desc *);
97*7c478bd9Sstevel@tonic-gate static void	elf_dladdr(ulong_t, Rt_map *, Dl_info *, void **, int);
98*7c478bd9Sstevel@tonic-gate static ulong_t	elf_entry_pt(void);
99*7c478bd9Sstevel@tonic-gate static char	*elf_get_so(const char *, const char *);
100*7c478bd9Sstevel@tonic-gate static Rt_map	*elf_map_so(Lm_list *, Aliste, const char *, const char *, int);
101*7c478bd9Sstevel@tonic-gate static int	elf_needed(Lm_list *, Aliste, Rt_map *);
102*7c478bd9Sstevel@tonic-gate static void	elf_unmap_so(Rt_map *);
103*7c478bd9Sstevel@tonic-gate static int	elf_verify_vers(const char *, Rt_map *, Rt_map *);
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate /*
106*7c478bd9Sstevel@tonic-gate  * Functions and data accessed through indirect pointers.
107*7c478bd9Sstevel@tonic-gate  */
108*7c478bd9Sstevel@tonic-gate Fct elf_fct = {
109*7c478bd9Sstevel@tonic-gate 	elf_are_u,
110*7c478bd9Sstevel@tonic-gate 	elf_entry_pt,
111*7c478bd9Sstevel@tonic-gate 	elf_map_so,
112*7c478bd9Sstevel@tonic-gate 	elf_unmap_so,
113*7c478bd9Sstevel@tonic-gate 	elf_needed,
114*7c478bd9Sstevel@tonic-gate 	lookup_sym,
115*7c478bd9Sstevel@tonic-gate 	elf_reloc,
116*7c478bd9Sstevel@tonic-gate 	elf_dflt_dirs,
117*7c478bd9Sstevel@tonic-gate 	elf_secure_dirs,
118*7c478bd9Sstevel@tonic-gate 	elf_fix_name,
119*7c478bd9Sstevel@tonic-gate 	elf_get_so,
120*7c478bd9Sstevel@tonic-gate 	elf_dladdr,
121*7c478bd9Sstevel@tonic-gate 	dlsym_handle,
122*7c478bd9Sstevel@tonic-gate 	elf_verify_vers,
123*7c478bd9Sstevel@tonic-gate 	elf_set_prot
124*7c478bd9Sstevel@tonic-gate };
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate /*
128*7c478bd9Sstevel@tonic-gate  * Redefine NEEDED name if necessary.
129*7c478bd9Sstevel@tonic-gate  */
130*7c478bd9Sstevel@tonic-gate static Pnode *
131*7c478bd9Sstevel@tonic-gate elf_fix_name(const char *name, Rt_map *clmp, uint_t orig)
132*7c478bd9Sstevel@tonic-gate {
133*7c478bd9Sstevel@tonic-gate 	/*
134*7c478bd9Sstevel@tonic-gate 	 * For ABI compliance, if we are asked for ld.so.1, then really give
135*7c478bd9Sstevel@tonic-gate 	 * them libsys.so.1 (the SONAME of libsys.so.1 is ld.so.1).
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	if (((*name == '/') &&
138*7c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
139*7c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD_64)) == 0)) ||
140*7c478bd9Sstevel@tonic-gate #else
141*7c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD)) == 0)) ||
142*7c478bd9Sstevel@tonic-gate #endif
143*7c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_FIL_RTLD)) == 0)) {
144*7c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_fixname(name, MSG_ORIG(MSG_PTH_LIBSYS)));
147*7c478bd9Sstevel@tonic-gate 		if (((pnp = calloc(sizeof (Pnode), 1)) == 0) ||
148*7c478bd9Sstevel@tonic-gate 		    ((pnp->p_name = strdup(MSG_ORIG(MSG_PTH_LIBSYS))) == 0)) {
149*7c478bd9Sstevel@tonic-gate 			if (pnp)
150*7c478bd9Sstevel@tonic-gate 				free(pnp);
151*7c478bd9Sstevel@tonic-gate 			return (0);
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		pnp->p_len = MSG_PTH_LIBSYS_SIZE;
154*7c478bd9Sstevel@tonic-gate 		pnp->p_orig = (orig & PN_SER_MASK);
155*7c478bd9Sstevel@tonic-gate 		return (pnp);
156*7c478bd9Sstevel@tonic-gate 	}
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	return (expand_paths(clmp, name, orig, 0));
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /*
162*7c478bd9Sstevel@tonic-gate  * Determine if we have been given an ELF file and if so determine if the file
163*7c478bd9Sstevel@tonic-gate  * is compatible.  Returns 1 if true, else 0 and sets the reject descriptor
164*7c478bd9Sstevel@tonic-gate  * with associated error information.
165*7c478bd9Sstevel@tonic-gate  */
166*7c478bd9Sstevel@tonic-gate static int
167*7c478bd9Sstevel@tonic-gate elf_are_u(Rej_desc *rej)
168*7c478bd9Sstevel@tonic-gate {
169*7c478bd9Sstevel@tonic-gate 	Ehdr	*ehdr;
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	/*
172*7c478bd9Sstevel@tonic-gate 	 * Determine if we're an elf file.  If not simply return, we don't set
173*7c478bd9Sstevel@tonic-gate 	 * any rejection information as this test allows use to scroll through
174*7c478bd9Sstevel@tonic-gate 	 * the objects we support (ELF, AOUT).
175*7c478bd9Sstevel@tonic-gate 	 */
176*7c478bd9Sstevel@tonic-gate 	if (fmap->fm_fsize < sizeof (Ehdr) ||
177*7c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG0] != ELFMAG0 ||
178*7c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG1] != ELFMAG1 ||
179*7c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG2] != ELFMAG2 ||
180*7c478bd9Sstevel@tonic-gate 	    fmap->fm_maddr[EI_MAG3] != ELFMAG3) {
181*7c478bd9Sstevel@tonic-gate 		return (0);
182*7c478bd9Sstevel@tonic-gate 	}
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	/*
185*7c478bd9Sstevel@tonic-gate 	 * Check class and encoding.
186*7c478bd9Sstevel@tonic-gate 	 */
187*7c478bd9Sstevel@tonic-gate 	/* LINTED */
188*7c478bd9Sstevel@tonic-gate 	ehdr = (Ehdr *)fmap->fm_maddr;
189*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_CLASS] != M_CLASS) {
190*7c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_CLASS;
191*7c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_CLASS];
192*7c478bd9Sstevel@tonic-gate 		return (0);
193*7c478bd9Sstevel@tonic-gate 	}
194*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_DATA] != M_DATA) {
195*7c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
196*7c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
197*7c478bd9Sstevel@tonic-gate 		return (0);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 	if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC) &&
200*7c478bd9Sstevel@tonic-gate 	    (ehdr->e_type != ET_DYN)) {
201*7c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_TYPE;
202*7c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_type;
203*7c478bd9Sstevel@tonic-gate 		return (0);
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	/*
207*7c478bd9Sstevel@tonic-gate 	 * Verify machine specific flags, and hardware capability requirements.
208*7c478bd9Sstevel@tonic-gate 	 */
209*7c478bd9Sstevel@tonic-gate 	if ((elf_mach_flags_check(rej, ehdr) == 0) ||
210*7c478bd9Sstevel@tonic-gate 	    ((rtld_flags2 & RT_FL2_HWCAP) && (hwcap_check(rej, ehdr) == 0)))
211*7c478bd9Sstevel@tonic-gate 		return (0);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	/*
214*7c478bd9Sstevel@tonic-gate 	 * Verify ELF version.  ??? is this too restrictive ???
215*7c478bd9Sstevel@tonic-gate 	 */
216*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_version > EV_CURRENT) {
217*7c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
218*7c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
219*7c478bd9Sstevel@tonic-gate 		return (0);
220*7c478bd9Sstevel@tonic-gate 	}
221*7c478bd9Sstevel@tonic-gate 	return (1);
222*7c478bd9Sstevel@tonic-gate }
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate /*
225*7c478bd9Sstevel@tonic-gate  * The runtime linker employs lazy loading to provide the libraries needed for
226*7c478bd9Sstevel@tonic-gate  * debugging, preloading .o's and dldump().  As these are seldom used, the
227*7c478bd9Sstevel@tonic-gate  * standard startup of ld.so.1 doesn't initialize all the information necessary
228*7c478bd9Sstevel@tonic-gate  * to perform plt relocation on ld.so.1's link-map.  The first time lazy loading
229*7c478bd9Sstevel@tonic-gate  * is called we get here to perform these initializations:
230*7c478bd9Sstevel@tonic-gate  *
231*7c478bd9Sstevel@tonic-gate  *  o	elf_needed() is called to set up the DYNINFO() indexes for each lazy
232*7c478bd9Sstevel@tonic-gate  *	dependency.  Typically, for all other objects, this is called during
233*7c478bd9Sstevel@tonic-gate  *	analyze_so(), but as ld.so.1 is set-contained we skip this processing.
234*7c478bd9Sstevel@tonic-gate  *
235*7c478bd9Sstevel@tonic-gate  *  o	For intel, ld.so.1's JMPSLOT relocations need relative updates. These
236*7c478bd9Sstevel@tonic-gate  *	are by default skipped thus delaying all relative relocation processing
237*7c478bd9Sstevel@tonic-gate  * 	on every invocation of ld.so.1.
238*7c478bd9Sstevel@tonic-gate  */
239*7c478bd9Sstevel@tonic-gate int
240*7c478bd9Sstevel@tonic-gate elf_rtld_load()
241*7c478bd9Sstevel@tonic-gate {
242*7c478bd9Sstevel@tonic-gate 	Lm_list	*lml = &lml_rtld;
243*7c478bd9Sstevel@tonic-gate 	Rt_map	*lmp = lml->lm_head;
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_PLTREL)
246*7c478bd9Sstevel@tonic-gate 		return (1);
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	/*
249*7c478bd9Sstevel@tonic-gate 	 * As we need to refer to the DYNINFO() information, insure that it has
250*7c478bd9Sstevel@tonic-gate 	 * been initialized.
251*7c478bd9Sstevel@tonic-gate 	 */
252*7c478bd9Sstevel@tonic-gate 	if (elf_needed(lml, ALO_DATA, lmp) == 0)
253*7c478bd9Sstevel@tonic-gate 		return (0);
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate #if	defined(i386)
256*7c478bd9Sstevel@tonic-gate 	/*
257*7c478bd9Sstevel@tonic-gate 	 * This is a kludge to give ld.so.1 a performance benefit on i386.
258*7c478bd9Sstevel@tonic-gate 	 * It's based around two factors.
259*7c478bd9Sstevel@tonic-gate 	 *
260*7c478bd9Sstevel@tonic-gate 	 *  o	JMPSLOT relocations (PLT's) actually need a relative relocation
261*7c478bd9Sstevel@tonic-gate 	 *	applied to the GOT entry so that they can find PLT0.
262*7c478bd9Sstevel@tonic-gate 	 *
263*7c478bd9Sstevel@tonic-gate 	 *  o	ld.so.1 does not exercise *any* PLT's before it has made a call
264*7c478bd9Sstevel@tonic-gate 	 *	to elf_lazy_load().  This is because all dynamic dependencies
265*7c478bd9Sstevel@tonic-gate 	 * 	are recorded as lazy dependencies.
266*7c478bd9Sstevel@tonic-gate 	 */
267*7c478bd9Sstevel@tonic-gate 	(void) elf_reloc_relacount((ulong_t)JMPREL(lmp),
268*7c478bd9Sstevel@tonic-gate 	    (ulong_t)(PLTRELSZ(lmp) / RELENT(lmp)), (ulong_t)RELENT(lmp),
269*7c478bd9Sstevel@tonic-gate 	    (ulong_t)ADDR(lmp));
270*7c478bd9Sstevel@tonic-gate #endif
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	lml->lm_flags |= LML_FLG_PLTREL;
273*7c478bd9Sstevel@tonic-gate 	return (1);
274*7c478bd9Sstevel@tonic-gate }
275*7c478bd9Sstevel@tonic-gate 
276*7c478bd9Sstevel@tonic-gate /*
277*7c478bd9Sstevel@tonic-gate  * Lazy load an object.
278*7c478bd9Sstevel@tonic-gate  */
279*7c478bd9Sstevel@tonic-gate Rt_map *
280*7c478bd9Sstevel@tonic-gate elf_lazy_load(Rt_map *clmp, uint_t ndx, const char *sym)
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp, *hlmp;
283*7c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(clmp)[ndx];
284*7c478bd9Sstevel@tonic-gate 	uint_t		flags = 0;
285*7c478bd9Sstevel@tonic-gate 	Pnode		*pnp;
286*7c478bd9Sstevel@tonic-gate 	const char	*name;
287*7c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
288*7c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc;
289*7c478bd9Sstevel@tonic-gate 	Aliste		lmco;
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	/*
292*7c478bd9Sstevel@tonic-gate 	 * If this dependency has already been processed, we're done.
293*7c478bd9Sstevel@tonic-gate 	 */
294*7c478bd9Sstevel@tonic-gate 	if (((nlmp = (Rt_map *)dip->di_info) != 0) ||
295*7c478bd9Sstevel@tonic-gate 	    (dip->di_flags & FLG_DI_PROCESSD))
296*7c478bd9Sstevel@tonic-gate 		return (nlmp);
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/*
299*7c478bd9Sstevel@tonic-gate 	 * Determine the initial dependency name, and indicate that this
300*7c478bd9Sstevel@tonic-gate 	 * dependencies processing has initiated.
301*7c478bd9Sstevel@tonic-gate 	 */
302*7c478bd9Sstevel@tonic-gate 	name = STRTAB(clmp) + DYN(clmp)[ndx].d_un.d_val;
303*7c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_file_lazyload(name, NAME(clmp), sym));
304*7c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_ENABLE)
305*7c478bd9Sstevel@tonic-gate 		dip->di_flags |= FLG_DI_PROCESSD;
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	if (dip->di_flags & FLG_DI_GROUP)
308*7c478bd9Sstevel@tonic-gate 		flags |= (FLG_RT_SETGROUP | FLG_RT_HANDLE);
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	/*
311*7c478bd9Sstevel@tonic-gate 	 * Expand the requested name if necessary.
312*7c478bd9Sstevel@tonic-gate 	 */
313*7c478bd9Sstevel@tonic-gate 	if ((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0)
314*7c478bd9Sstevel@tonic-gate 		return (0);
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate 	/*
317*7c478bd9Sstevel@tonic-gate 	 * Provided the object on the head of the link-map has completed its
318*7c478bd9Sstevel@tonic-gate 	 * relocation, create a new link-map control list for this request.
319*7c478bd9Sstevel@tonic-gate 	 */
320*7c478bd9Sstevel@tonic-gate 	hlmp = lml->lm_head;
321*7c478bd9Sstevel@tonic-gate 	if (FLAGS(hlmp) & FLG_RT_RELOCED) {
322*7c478bd9Sstevel@tonic-gate 		if ((lmc = alist_append(&(lml->lm_lists), 0, sizeof (Lm_cntl),
323*7c478bd9Sstevel@tonic-gate 		    AL_CNT_LMLISTS)) == 0) {
324*7c478bd9Sstevel@tonic-gate 			remove_pnode(pnp);
325*7c478bd9Sstevel@tonic-gate 			return (0);
326*7c478bd9Sstevel@tonic-gate 		}
327*7c478bd9Sstevel@tonic-gate 		lmco = (Aliste)((char *)lmc - (char *)lml->lm_lists);
328*7c478bd9Sstevel@tonic-gate 	} else {
329*7c478bd9Sstevel@tonic-gate 		lmc = 0;
330*7c478bd9Sstevel@tonic-gate 		lmco = ALO_DATA;
331*7c478bd9Sstevel@tonic-gate 	}
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	/*
334*7c478bd9Sstevel@tonic-gate 	 * Load the associated object.
335*7c478bd9Sstevel@tonic-gate 	 */
336*7c478bd9Sstevel@tonic-gate 	dip->di_info = nlmp =
337*7c478bd9Sstevel@tonic-gate 	    load_one(lml, lmco, pnp, clmp, MODE(clmp), flags, 0);
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	/*
340*7c478bd9Sstevel@tonic-gate 	 * Remove any expanded pathname infrastructure.  Reduce the pending lazy
341*7c478bd9Sstevel@tonic-gate 	 * dependency count of the caller, together with the link-map lists
342*7c478bd9Sstevel@tonic-gate 	 * count of objects that still have lazy dependencies pending.
343*7c478bd9Sstevel@tonic-gate 	 */
344*7c478bd9Sstevel@tonic-gate 	remove_pnode(pnp);
345*7c478bd9Sstevel@tonic-gate 	if (--LAZY(clmp) == 0)
346*7c478bd9Sstevel@tonic-gate 		LIST(clmp)->lm_lazy--;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	/*
349*7c478bd9Sstevel@tonic-gate 	 * Finish processing the objects associated with this request.
350*7c478bd9Sstevel@tonic-gate 	 */
351*7c478bd9Sstevel@tonic-gate 	if (nlmp && ((analyze_lmc(lml, lmco, nlmp) == 0) ||
352*7c478bd9Sstevel@tonic-gate 	    (relocate_lmc(lml, lmco, nlmp) == 0)))
353*7c478bd9Sstevel@tonic-gate 		dip->di_info = nlmp = 0;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 	/*
356*7c478bd9Sstevel@tonic-gate 	 * If the dependency has been successfully processed, and it is part of
357*7c478bd9Sstevel@tonic-gate 	 * a link-map control list that is equivalent, or less, that the callers
358*7c478bd9Sstevel@tonic-gate 	 * control list, create an association between the caller and this
359*7c478bd9Sstevel@tonic-gate 	 * dependency.  If this dependency isn't yet apart of the callers
360*7c478bd9Sstevel@tonic-gate 	 * link-map control list, then it is still apart of a list that is being
361*7c478bd9Sstevel@tonic-gate 	 * relocated.  As the relocation of an object on this list might still
362*7c478bd9Sstevel@tonic-gate 	 * fail, we can't yet bind the caller to this object.  To do so, would
363*7c478bd9Sstevel@tonic-gate 	 * be locking the object so that it couldn't be deleted.  Mark this
364*7c478bd9Sstevel@tonic-gate 	 * object as free, and it will be reprocessed when this dependency is
365*7c478bd9Sstevel@tonic-gate 	 * next referenced.
366*7c478bd9Sstevel@tonic-gate 	 */
367*7c478bd9Sstevel@tonic-gate 	if (nlmp) {
368*7c478bd9Sstevel@tonic-gate 		if (CNTL(nlmp) <= CNTL(clmp)) {
369*7c478bd9Sstevel@tonic-gate 			if (bind_one(clmp, nlmp, BND_NEEDED) == 0)
370*7c478bd9Sstevel@tonic-gate 				dip->di_info = nlmp = 0;
371*7c478bd9Sstevel@tonic-gate 		} else {
372*7c478bd9Sstevel@tonic-gate 			dip->di_info = 0;
373*7c478bd9Sstevel@tonic-gate 			dip->di_flags &= ~FLG_DI_PROCESSD;
374*7c478bd9Sstevel@tonic-gate 			if (LAZY(clmp)++ == 0)
375*7c478bd9Sstevel@tonic-gate 				LIST(clmp)->lm_lazy++;
376*7c478bd9Sstevel@tonic-gate 		}
377*7c478bd9Sstevel@tonic-gate 	}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	/*
380*7c478bd9Sstevel@tonic-gate 	 * After a successful load, any objects collected on the new link-map
381*7c478bd9Sstevel@tonic-gate 	 * control list will have been moved to the callers link-map control
382*7c478bd9Sstevel@tonic-gate 	 * list.  This control list can now be deleted.
383*7c478bd9Sstevel@tonic-gate 	 */
384*7c478bd9Sstevel@tonic-gate 	if (lmc) {
385*7c478bd9Sstevel@tonic-gate 		if (nlmp == 0)
386*7c478bd9Sstevel@tonic-gate 			remove_incomplete(lml, lmco);
387*7c478bd9Sstevel@tonic-gate 		remove_cntl(lml, lmco);
388*7c478bd9Sstevel@tonic-gate 	}
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	return (nlmp);
391*7c478bd9Sstevel@tonic-gate }
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate /*
395*7c478bd9Sstevel@tonic-gate  * Return the entry point of the ELF executable.
396*7c478bd9Sstevel@tonic-gate  */
397*7c478bd9Sstevel@tonic-gate static ulong_t
398*7c478bd9Sstevel@tonic-gate elf_entry_pt(void)
399*7c478bd9Sstevel@tonic-gate {
400*7c478bd9Sstevel@tonic-gate 	return (ENTRY(lml_main.lm_head));
401*7c478bd9Sstevel@tonic-gate }
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate /*
404*7c478bd9Sstevel@tonic-gate  * Unmap a given ELF shared object from the address space.
405*7c478bd9Sstevel@tonic-gate  */
406*7c478bd9Sstevel@tonic-gate static void
407*7c478bd9Sstevel@tonic-gate elf_unmap_so(Rt_map *lmp)
408*7c478bd9Sstevel@tonic-gate {
409*7c478bd9Sstevel@tonic-gate 	caddr_t	addr;
410*7c478bd9Sstevel@tonic-gate 	size_t	size;
411*7c478bd9Sstevel@tonic-gate 	Mmap	*mmaps;
412*7c478bd9Sstevel@tonic-gate 
413*7c478bd9Sstevel@tonic-gate 	/*
414*7c478bd9Sstevel@tonic-gate 	 * If this link map represents a relocatable object concatenation, then
415*7c478bd9Sstevel@tonic-gate 	 * the image was simply generated in allocated memory.  Free the memory.
416*7c478bd9Sstevel@tonic-gate 	 *
417*7c478bd9Sstevel@tonic-gate 	 * Note: the memory was originally allocated in the libelf:_elf_outmap
418*7c478bd9Sstevel@tonic-gate 	 * routine and would normally have been free'd in elf_outsync(), but
419*7c478bd9Sstevel@tonic-gate 	 * because we 'interpose' on that routine the memory  wasn't free'd at
420*7c478bd9Sstevel@tonic-gate 	 * that time.
421*7c478bd9Sstevel@tonic-gate 	 */
422*7c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_IMGALLOC) {
423*7c478bd9Sstevel@tonic-gate 		free((void *)ADDR(lmp));
424*7c478bd9Sstevel@tonic-gate 		return;
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	/*
428*7c478bd9Sstevel@tonic-gate 	 * If padding was enabled via rtld_db, then we have at least one page
429*7c478bd9Sstevel@tonic-gate 	 * in front of the image - and possibly a trailing page.
430*7c478bd9Sstevel@tonic-gate 	 * Unmap the front page first:
431*7c478bd9Sstevel@tonic-gate 	 */
432*7c478bd9Sstevel@tonic-gate 	if (PADSTART(lmp) != ADDR(lmp)) {
433*7c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PTRUNC(PADSTART(lmp));
434*7c478bd9Sstevel@tonic-gate 		size = ADDR(lmp) - (ulong_t)addr;
435*7c478bd9Sstevel@tonic-gate 		(void) munmap(addr, size);
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 	/*
439*7c478bd9Sstevel@tonic-gate 	 * Unmap any trailing padding.
440*7c478bd9Sstevel@tonic-gate 	 */
441*7c478bd9Sstevel@tonic-gate 	if (M_PROUND((PADSTART(lmp) + PADIMLEN(lmp))) >
442*7c478bd9Sstevel@tonic-gate 	    M_PROUND(ADDR(lmp) + MSIZE(lmp))) {
443*7c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PROUND(ADDR(lmp) + MSIZE(lmp));
444*7c478bd9Sstevel@tonic-gate 		size = M_PROUND(PADSTART(lmp) + PADIMLEN(lmp)) - (ulong_t)addr;
445*7c478bd9Sstevel@tonic-gate 		(void) munmap(addr, size);
446*7c478bd9Sstevel@tonic-gate 	}
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	/*
449*7c478bd9Sstevel@tonic-gate 	 * Unmmap all mapped segments.
450*7c478bd9Sstevel@tonic-gate 	 */
451*7c478bd9Sstevel@tonic-gate 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++)
452*7c478bd9Sstevel@tonic-gate 		(void) munmap(mmaps->m_vaddr, mmaps->m_msize);
453*7c478bd9Sstevel@tonic-gate }
454*7c478bd9Sstevel@tonic-gate 
455*7c478bd9Sstevel@tonic-gate /*
456*7c478bd9Sstevel@tonic-gate  * Determine if a dependency requires a particular version and if so verify
457*7c478bd9Sstevel@tonic-gate  * that the version exists in the dependency.
458*7c478bd9Sstevel@tonic-gate  */
459*7c478bd9Sstevel@tonic-gate static int
460*7c478bd9Sstevel@tonic-gate elf_verify_vers(const char *name, Rt_map *clmp, Rt_map *nlmp)
461*7c478bd9Sstevel@tonic-gate {
462*7c478bd9Sstevel@tonic-gate 	Verneed		*vnd = VERNEED(clmp);
463*7c478bd9Sstevel@tonic-gate 	int		_num, num = VERNEEDNUM(clmp);
464*7c478bd9Sstevel@tonic-gate 	char		*cstrs = (char *)STRTAB(clmp);
465*7c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 	/*
468*7c478bd9Sstevel@tonic-gate 	 * Traverse the callers version needed information and determine if any
469*7c478bd9Sstevel@tonic-gate 	 * specific versions are required from the dependency.
470*7c478bd9Sstevel@tonic-gate 	 */
471*7c478bd9Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
472*7c478bd9Sstevel@tonic-gate 	    vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) {
473*7c478bd9Sstevel@tonic-gate 		Half		cnt = vnd->vn_cnt;
474*7c478bd9Sstevel@tonic-gate 		Vernaux		*vnap;
475*7c478bd9Sstevel@tonic-gate 		char		*nstrs, *need;
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 		/*
478*7c478bd9Sstevel@tonic-gate 		 * Determine if a needed entry matches this dependency.
479*7c478bd9Sstevel@tonic-gate 		 */
480*7c478bd9Sstevel@tonic-gate 		need = (char *)(cstrs + vnd->vn_file);
481*7c478bd9Sstevel@tonic-gate 		if (strcmp(name, need) != 0)
482*7c478bd9Sstevel@tonic-gate 			continue;
483*7c478bd9Sstevel@tonic-gate 
484*7c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_ver_need_title(NAME(clmp)));
485*7c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
486*7c478bd9Sstevel@tonic-gate 		    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
487*7c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_VER_FIND), name);
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 		/*
490*7c478bd9Sstevel@tonic-gate 		 * Validate that each version required actually exists in the
491*7c478bd9Sstevel@tonic-gate 		 * dependency.
492*7c478bd9Sstevel@tonic-gate 		 */
493*7c478bd9Sstevel@tonic-gate 		nstrs = (char *)STRTAB(nlmp);
494*7c478bd9Sstevel@tonic-gate 
495*7c478bd9Sstevel@tonic-gate 		for (vnap = (Vernaux *)((Xword)vnd + vnd->vn_aux); cnt;
496*7c478bd9Sstevel@tonic-gate 		    cnt--, vnap = (Vernaux *)((Xword)vnap + vnap->vna_next)) {
497*7c478bd9Sstevel@tonic-gate 			char		*version, *define;
498*7c478bd9Sstevel@tonic-gate 			Verdef		*vdf = VERDEF(nlmp);
499*7c478bd9Sstevel@tonic-gate 			ulong_t		_num, num = VERDEFNUM(nlmp);
500*7c478bd9Sstevel@tonic-gate 			int		found = 0;
501*7c478bd9Sstevel@tonic-gate 
502*7c478bd9Sstevel@tonic-gate 			version = (char *)(cstrs + vnap->vna_name);
503*7c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_ver_need_entry(0, need, version));
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 			for (_num = 1; _num <= num; _num++,
506*7c478bd9Sstevel@tonic-gate 			    vdf = (Verdef *)((Xword)vdf + vdf->vd_next)) {
507*7c478bd9Sstevel@tonic-gate 				Verdaux		*vdap;
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 				if (vnap->vna_hash != vdf->vd_hash)
510*7c478bd9Sstevel@tonic-gate 					continue;
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 				vdap = (Verdaux *)((Xword)vdf + vdf->vd_aux);
513*7c478bd9Sstevel@tonic-gate 				define = (char *)(nstrs + vdap->vda_name);
514*7c478bd9Sstevel@tonic-gate 				if (strcmp(version, define) != 0)
515*7c478bd9Sstevel@tonic-gate 					continue;
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 				found++;
518*7c478bd9Sstevel@tonic-gate 				break;
519*7c478bd9Sstevel@tonic-gate 			}
520*7c478bd9Sstevel@tonic-gate 
521*7c478bd9Sstevel@tonic-gate 			/*
522*7c478bd9Sstevel@tonic-gate 			 * If we're being traced print out any matched version
523*7c478bd9Sstevel@tonic-gate 			 * when the verbose (-v) option is in effect.  Always
524*7c478bd9Sstevel@tonic-gate 			 * print any unmatched versions.
525*7c478bd9Sstevel@tonic-gate 			 */
526*7c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
527*7c478bd9Sstevel@tonic-gate 				if (found) {
528*7c478bd9Sstevel@tonic-gate 				    if (!(lml->lm_flags & LML_FLG_TRC_VERBOSE))
529*7c478bd9Sstevel@tonic-gate 					continue;
530*7c478bd9Sstevel@tonic-gate 
531*7c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_VER_FOUND),
532*7c478bd9Sstevel@tonic-gate 					need, version, NAME(nlmp));
533*7c478bd9Sstevel@tonic-gate 				} else {
534*7c478bd9Sstevel@tonic-gate 				    if (rtld_flags & RT_FL_SILENCERR)
535*7c478bd9Sstevel@tonic-gate 					continue;
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_VER_NFOUND),
538*7c478bd9Sstevel@tonic-gate 					need, version);
539*7c478bd9Sstevel@tonic-gate 				}
540*7c478bd9Sstevel@tonic-gate 				continue;
541*7c478bd9Sstevel@tonic-gate 			}
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 			/*
544*7c478bd9Sstevel@tonic-gate 			 * If the version hasn't been found then this is a
545*7c478bd9Sstevel@tonic-gate 			 * candidate for a fatal error condition.  Weak
546*7c478bd9Sstevel@tonic-gate 			 * version definition requirements are silently
547*7c478bd9Sstevel@tonic-gate 			 * ignored.  Also, if the image inspected for a version
548*7c478bd9Sstevel@tonic-gate 			 * definition has no versioning recorded at all then
549*7c478bd9Sstevel@tonic-gate 			 * silently ignore this (this provides better backward
550*7c478bd9Sstevel@tonic-gate 			 * compatibility to old images created prior to
551*7c478bd9Sstevel@tonic-gate 			 * versioning being available).  Both of these skipped
552*7c478bd9Sstevel@tonic-gate 			 * diagnostics are available under tracing (see above).
553*7c478bd9Sstevel@tonic-gate 			 */
554*7c478bd9Sstevel@tonic-gate 			if ((found == 0) && (num != 0) &&
555*7c478bd9Sstevel@tonic-gate 			    (!(vnap->vna_flags & VER_FLG_WEAK))) {
556*7c478bd9Sstevel@tonic-gate 				eprintf(ERR_FATAL, MSG_INTL(MSG_VER_NFOUND),
557*7c478bd9Sstevel@tonic-gate 				    need, version, NAME(clmp));
558*7c478bd9Sstevel@tonic-gate 				return (0);
559*7c478bd9Sstevel@tonic-gate 			}
560*7c478bd9Sstevel@tonic-gate 		}
561*7c478bd9Sstevel@tonic-gate 		return (1);
562*7c478bd9Sstevel@tonic-gate 	}
563*7c478bd9Sstevel@tonic-gate 	return (1);
564*7c478bd9Sstevel@tonic-gate }
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate /*
567*7c478bd9Sstevel@tonic-gate  * Search through the dynamic section for DT_NEEDED entries and perform one
568*7c478bd9Sstevel@tonic-gate  * of two functions.  If only the first argument is specified then load the
569*7c478bd9Sstevel@tonic-gate  * defined shared object, otherwise add the link map representing the defined
570*7c478bd9Sstevel@tonic-gate  * link map the the dlopen list.
571*7c478bd9Sstevel@tonic-gate  */
572*7c478bd9Sstevel@tonic-gate static int
573*7c478bd9Sstevel@tonic-gate elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp)
574*7c478bd9Sstevel@tonic-gate {
575*7c478bd9Sstevel@tonic-gate 	Dyn		*dyn;
576*7c478bd9Sstevel@tonic-gate 	ulong_t		ndx = 0;
577*7c478bd9Sstevel@tonic-gate 	uint_t		lazy = 0, flags = 0;
578*7c478bd9Sstevel@tonic-gate 	Word		lmflags = lml->lm_flags;
579*7c478bd9Sstevel@tonic-gate 	Word		lmtflags = lml->lm_tflags;
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 	/*
582*7c478bd9Sstevel@tonic-gate 	 * Process each shared object on needed list.
583*7c478bd9Sstevel@tonic-gate 	 */
584*7c478bd9Sstevel@tonic-gate 	if (DYN(clmp) == 0)
585*7c478bd9Sstevel@tonic-gate 		return (1);
586*7c478bd9Sstevel@tonic-gate 
587*7c478bd9Sstevel@tonic-gate 	for (dyn = (Dyn *)DYN(clmp); dyn->d_tag != DT_NULL; dyn++, ndx++) {
588*7c478bd9Sstevel@tonic-gate 		Dyninfo	*dip = &DYNINFO(clmp)[ndx];
589*7c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
590*7c478bd9Sstevel@tonic-gate 		char	*name;
591*7c478bd9Sstevel@tonic-gate 		int	silent = 0;
592*7c478bd9Sstevel@tonic-gate 		Pnode	*pnp;
593*7c478bd9Sstevel@tonic-gate 
594*7c478bd9Sstevel@tonic-gate 		switch (dyn->d_tag) {
595*7c478bd9Sstevel@tonic-gate 		case DT_POSFLAG_1:
596*7c478bd9Sstevel@tonic-gate 			if ((dyn->d_un.d_val & DF_P1_LAZYLOAD) &&
597*7c478bd9Sstevel@tonic-gate 			    !(lmtflags & LML_TFLG_NOLAZYLD))
598*7c478bd9Sstevel@tonic-gate 				lazy = 1;
599*7c478bd9Sstevel@tonic-gate 			if (dyn->d_un.d_val & DF_P1_GROUPPERM)
600*7c478bd9Sstevel@tonic-gate 				flags = (FLG_RT_SETGROUP | FLG_RT_HANDLE);
601*7c478bd9Sstevel@tonic-gate 			continue;
602*7c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
603*7c478bd9Sstevel@tonic-gate 		case DT_USED:
604*7c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_NEEDED;
605*7c478bd9Sstevel@tonic-gate 			if (flags)
606*7c478bd9Sstevel@tonic-gate 				dip->di_flags |= FLG_DI_GROUP;
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 			name = (char *)STRTAB(clmp) + dyn->d_un.d_val;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 			/*
611*7c478bd9Sstevel@tonic-gate 			 * NOTE, libc.so.1 can't be lazy loaded.  Although a
612*7c478bd9Sstevel@tonic-gate 			 * lazy position flag won't be produced when a RTLDINFO
613*7c478bd9Sstevel@tonic-gate 			 * .dynamic entry is found (introduced with the UPM in
614*7c478bd9Sstevel@tonic-gate 			 * Solaris 10), it was possible to mark libc for lazy
615*7c478bd9Sstevel@tonic-gate 			 * loading on previous releases.  To reduce the overhead
616*7c478bd9Sstevel@tonic-gate 			 * of testing for this occurrence, only carry out this
617*7c478bd9Sstevel@tonic-gate 			 * check for the first object on the link-map list
618*7c478bd9Sstevel@tonic-gate 			 * (there aren't many applications built without libc).
619*7c478bd9Sstevel@tonic-gate 			 */
620*7c478bd9Sstevel@tonic-gate 			if (lazy && (lml->lm_head == clmp) &&
621*7c478bd9Sstevel@tonic-gate 			    (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0))
622*7c478bd9Sstevel@tonic-gate 				lazy = 0;
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 			/*
625*7c478bd9Sstevel@tonic-gate 			 * Don't bring in lazy loaded objects yet unless we've
626*7c478bd9Sstevel@tonic-gate 			 * been asked to attempt to load all available objects
627*7c478bd9Sstevel@tonic-gate 			 * (crle(1) sets LD_FLAGS=loadavail).  Even under
628*7c478bd9Sstevel@tonic-gate 			 * RTLD_NOW we don't process this - RTLD_NOW will cause
629*7c478bd9Sstevel@tonic-gate 			 * relocation processing which in turn might trigger
630*7c478bd9Sstevel@tonic-gate 			 * lazy loading, but its possible that the object has a
631*7c478bd9Sstevel@tonic-gate 			 * lazy loaded file with no bindings (i.e., it should
632*7c478bd9Sstevel@tonic-gate 			 * never have been a dependency in the first place).
633*7c478bd9Sstevel@tonic-gate 			 */
634*7c478bd9Sstevel@tonic-gate 			if (lazy) {
635*7c478bd9Sstevel@tonic-gate 				if ((lmflags & LML_FLG_LOADAVAIL) == 0) {
636*7c478bd9Sstevel@tonic-gate 					LAZY(clmp)++;
637*7c478bd9Sstevel@tonic-gate 					lazy = flags = 0;
638*7c478bd9Sstevel@tonic-gate 					continue;
639*7c478bd9Sstevel@tonic-gate 				}
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 				/*
642*7c478bd9Sstevel@tonic-gate 				 * Silence any error messages - see description
643*7c478bd9Sstevel@tonic-gate 				 * under elf_lookup_filtee().
644*7c478bd9Sstevel@tonic-gate 				 */
645*7c478bd9Sstevel@tonic-gate 				if ((rtld_flags & RT_FL_SILENCERR) == 0) {
646*7c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_SILENCERR;
647*7c478bd9Sstevel@tonic-gate 					silent = 1;
648*7c478bd9Sstevel@tonic-gate 				}
649*7c478bd9Sstevel@tonic-gate 			}
650*7c478bd9Sstevel@tonic-gate 			break;
651*7c478bd9Sstevel@tonic-gate 		case DT_AUXILIARY:
652*7c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_AUXFLTR;
653*7c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
654*7c478bd9Sstevel@tonic-gate 			continue;
655*7c478bd9Sstevel@tonic-gate 		case DT_SUNW_AUXILIARY:
656*7c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR);
657*7c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
658*7c478bd9Sstevel@tonic-gate 			continue;
659*7c478bd9Sstevel@tonic-gate 		case DT_FILTER:
660*7c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_STDFLTR;
661*7c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
662*7c478bd9Sstevel@tonic-gate 			continue;
663*7c478bd9Sstevel@tonic-gate 		case DT_SUNW_FILTER:
664*7c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR);
665*7c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
666*7c478bd9Sstevel@tonic-gate 			continue;
667*7c478bd9Sstevel@tonic-gate 		default:
668*7c478bd9Sstevel@tonic-gate 			lazy = flags = 0;
669*7c478bd9Sstevel@tonic-gate 			continue;
670*7c478bd9Sstevel@tonic-gate 		}
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 		DBG_CALL(Dbg_file_needed(name, NAME(clmp)));
673*7c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & LML_FLG_TRC_ENABLE)
674*7c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_PROCESSD;
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 		/*
677*7c478bd9Sstevel@tonic-gate 		 * Establish the objects name, load it and establish a binding
678*7c478bd9Sstevel@tonic-gate 		 * with the caller.
679*7c478bd9Sstevel@tonic-gate 		 */
680*7c478bd9Sstevel@tonic-gate 		if (((pnp = elf_fix_name(name, clmp, PN_SER_NEEDED)) == 0) ||
681*7c478bd9Sstevel@tonic-gate 		    ((nlmp = load_one(lml, lmco, pnp, clmp, MODE(clmp),
682*7c478bd9Sstevel@tonic-gate 		    flags, 0)) == 0) || (bind_one(clmp, nlmp, BND_NEEDED) == 0))
683*7c478bd9Sstevel@tonic-gate 			nlmp = 0;
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 		/*
686*7c478bd9Sstevel@tonic-gate 		 * Clean up any infrastructure, including the removal of the
687*7c478bd9Sstevel@tonic-gate 		 * error suppression state, if it had been previously set in
688*7c478bd9Sstevel@tonic-gate 		 * this routine.
689*7c478bd9Sstevel@tonic-gate 		 */
690*7c478bd9Sstevel@tonic-gate 		if (pnp)
691*7c478bd9Sstevel@tonic-gate 			remove_pnode(pnp);
692*7c478bd9Sstevel@tonic-gate 		if (silent)
693*7c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_SILENCERR;
694*7c478bd9Sstevel@tonic-gate 		lazy = flags = 0;
695*7c478bd9Sstevel@tonic-gate 		if ((dip->di_info = (void *)nlmp) == 0) {
696*7c478bd9Sstevel@tonic-gate 			/*
697*7c478bd9Sstevel@tonic-gate 			 * If the object could not be mapped, continue if error
698*7c478bd9Sstevel@tonic-gate 			 * suppression is established or we're here with ldd(1).
699*7c478bd9Sstevel@tonic-gate 			 */
700*7c478bd9Sstevel@tonic-gate 			if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags &
701*7c478bd9Sstevel@tonic-gate 			    (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE)))
702*7c478bd9Sstevel@tonic-gate 				continue;
703*7c478bd9Sstevel@tonic-gate 			else
704*7c478bd9Sstevel@tonic-gate 				return (0);
705*7c478bd9Sstevel@tonic-gate 		}
706*7c478bd9Sstevel@tonic-gate 	}
707*7c478bd9Sstevel@tonic-gate 
708*7c478bd9Sstevel@tonic-gate 	if (LAZY(clmp))
709*7c478bd9Sstevel@tonic-gate 		lml->lm_lazy++;
710*7c478bd9Sstevel@tonic-gate 
711*7c478bd9Sstevel@tonic-gate 	return (1);
712*7c478bd9Sstevel@tonic-gate }
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate static int
715*7c478bd9Sstevel@tonic-gate elf_map_check(const char *name, caddr_t vaddr, Off size)
716*7c478bd9Sstevel@tonic-gate {
717*7c478bd9Sstevel@tonic-gate 	prmap_t		*maps, *_maps;
718*7c478bd9Sstevel@tonic-gate 	int		pfd, num, _num;
719*7c478bd9Sstevel@tonic-gate 	caddr_t		eaddr = vaddr + size;
720*7c478bd9Sstevel@tonic-gate 	int		err;
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 	/*
723*7c478bd9Sstevel@tonic-gate 	 * If memory reservations have been established for alternative objects
724*7c478bd9Sstevel@tonic-gate 	 * determine if this object falls within the reservation, if it does no
725*7c478bd9Sstevel@tonic-gate 	 * further checking is required.
726*7c478bd9Sstevel@tonic-gate 	 */
727*7c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_MEMRESV) {
728*7c478bd9Sstevel@tonic-gate 		Rtc_head	*head = (Rtc_head *)config->c_bgn;
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate 		if ((vaddr >= (caddr_t)(uintptr_t)head->ch_resbgn) &&
731*7c478bd9Sstevel@tonic-gate 		    (eaddr <= (caddr_t)(uintptr_t)head->ch_resend))
732*7c478bd9Sstevel@tonic-gate 			return (0);
733*7c478bd9Sstevel@tonic-gate 	}
734*7c478bd9Sstevel@tonic-gate 
735*7c478bd9Sstevel@tonic-gate 	/*
736*7c478bd9Sstevel@tonic-gate 	 * Determine the mappings presently in use by this process.
737*7c478bd9Sstevel@tonic-gate 	 */
738*7c478bd9Sstevel@tonic-gate 	if ((pfd = pr_open()) == FD_UNAVAIL)
739*7c478bd9Sstevel@tonic-gate 		return (1);
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCNMAP, (void *)&num) == -1) {
742*7c478bd9Sstevel@tonic-gate 		err = errno;
743*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_PROC), pr_name,
744*7c478bd9Sstevel@tonic-gate 		    strerror(err));
745*7c478bd9Sstevel@tonic-gate 		return (1);
746*7c478bd9Sstevel@tonic-gate 	}
747*7c478bd9Sstevel@tonic-gate 
748*7c478bd9Sstevel@tonic-gate 	if ((maps = malloc((num + 1) * sizeof (prmap_t))) == 0)
749*7c478bd9Sstevel@tonic-gate 		return (1);
750*7c478bd9Sstevel@tonic-gate 
751*7c478bd9Sstevel@tonic-gate 	if (ioctl(pfd, PIOCMAP, (void *)maps) == -1) {
752*7c478bd9Sstevel@tonic-gate 		err = errno;
753*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_PROC), pr_name,
754*7c478bd9Sstevel@tonic-gate 		    strerror(err));
755*7c478bd9Sstevel@tonic-gate 		free(maps);
756*7c478bd9Sstevel@tonic-gate 		return (1);
757*7c478bd9Sstevel@tonic-gate 	}
758*7c478bd9Sstevel@tonic-gate 
759*7c478bd9Sstevel@tonic-gate 	/*
760*7c478bd9Sstevel@tonic-gate 	 * Determine if the supplied address clashes with any of the present
761*7c478bd9Sstevel@tonic-gate 	 * process mappings.
762*7c478bd9Sstevel@tonic-gate 	 */
763*7c478bd9Sstevel@tonic-gate 	for (_num = 0, _maps = maps; _num < num; _num++, _maps++) {
764*7c478bd9Sstevel@tonic-gate 		caddr_t		_eaddr = _maps->pr_vaddr + _maps->pr_size;
765*7c478bd9Sstevel@tonic-gate 		Rt_map		*lmp;
766*7c478bd9Sstevel@tonic-gate 		const char	*str;
767*7c478bd9Sstevel@tonic-gate 
768*7c478bd9Sstevel@tonic-gate 		if ((eaddr < _maps->pr_vaddr) || (vaddr >= _eaddr))
769*7c478bd9Sstevel@tonic-gate 			continue;
770*7c478bd9Sstevel@tonic-gate 
771*7c478bd9Sstevel@tonic-gate 		/*
772*7c478bd9Sstevel@tonic-gate 		 * We have a memory clash.  See if one of the known dynamic
773*7c478bd9Sstevel@tonic-gate 		 * dependency mappings represents this space so as to provide
774*7c478bd9Sstevel@tonic-gate 		 * the user a more meaningful message.
775*7c478bd9Sstevel@tonic-gate 		 */
776*7c478bd9Sstevel@tonic-gate 		if ((lmp = _caller(vaddr, 0)) != 0)
777*7c478bd9Sstevel@tonic-gate 			str = NAME(lmp);
778*7c478bd9Sstevel@tonic-gate 		else
779*7c478bd9Sstevel@tonic-gate 			str = MSG_INTL(MSG_STR_UNKNOWN);
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_MAPINUSE), name,
782*7c478bd9Sstevel@tonic-gate 		    EC_ADDR(vaddr), EC_OFF(size), str);
783*7c478bd9Sstevel@tonic-gate 		return (1);
784*7c478bd9Sstevel@tonic-gate 	}
785*7c478bd9Sstevel@tonic-gate 	free(maps);
786*7c478bd9Sstevel@tonic-gate 	return (0);
787*7c478bd9Sstevel@tonic-gate }
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate /*
790*7c478bd9Sstevel@tonic-gate  * Obtain a memory reservation.  On newer systems, both MAP_ANON and MAP_ALIGN
791*7c478bd9Sstevel@tonic-gate  * are used to obtained an aligned reservation from anonymous memory.  If
792*7c478bd9Sstevel@tonic-gate  * MAP_ANON isn't available, then MAP_ALIGN isn't either, so obtain a standard
793*7c478bd9Sstevel@tonic-gate  * reservation using the file as backing.
794*7c478bd9Sstevel@tonic-gate  */
795*7c478bd9Sstevel@tonic-gate static Am_ret
796*7c478bd9Sstevel@tonic-gate elf_map_reserve(const char *name, caddr_t *maddr, Off msize, int mperm,
797*7c478bd9Sstevel@tonic-gate     int fd, Xword align)
798*7c478bd9Sstevel@tonic-gate {
799*7c478bd9Sstevel@tonic-gate 	Am_ret	amret;
800*7c478bd9Sstevel@tonic-gate 	int	mflag = MAP_PRIVATE | MAP_NORESERVE;
801*7c478bd9Sstevel@tonic-gate 
802*7c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
803*7c478bd9Sstevel@tonic-gate 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
804*7c478bd9Sstevel@tonic-gate 		mflag |= MAP_ALIGN;
805*7c478bd9Sstevel@tonic-gate 		*maddr = (caddr_t)align;
806*7c478bd9Sstevel@tonic-gate 	}
807*7c478bd9Sstevel@tonic-gate #endif
808*7c478bd9Sstevel@tonic-gate 	if ((amret = anon_map(maddr, msize, PROT_NONE, mflag)) == AM_ERROR)
809*7c478bd9Sstevel@tonic-gate 		return (amret);
810*7c478bd9Sstevel@tonic-gate 
811*7c478bd9Sstevel@tonic-gate 	if (amret == AM_OK)
812*7c478bd9Sstevel@tonic-gate 		return (AM_OK);
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 	/*
815*7c478bd9Sstevel@tonic-gate 	 * If an anonymous memory request failed (which should only be the
816*7c478bd9Sstevel@tonic-gate 	 * case if it is unsupported on the system we're running on), establish
817*7c478bd9Sstevel@tonic-gate 	 * the initial mapping directly from the file.
818*7c478bd9Sstevel@tonic-gate 	 */
819*7c478bd9Sstevel@tonic-gate 	*maddr = 0;
820*7c478bd9Sstevel@tonic-gate 	if ((*maddr = mmap(*maddr, msize, mperm, MAP_PRIVATE,
821*7c478bd9Sstevel@tonic-gate 	    fd, 0)) == MAP_FAILED) {
822*7c478bd9Sstevel@tonic-gate 		int	err = errno;
823*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), name, strerror(err));
824*7c478bd9Sstevel@tonic-gate 		return (AM_ERROR);
825*7c478bd9Sstevel@tonic-gate 	}
826*7c478bd9Sstevel@tonic-gate 	return (AM_NOSUP);
827*7c478bd9Sstevel@tonic-gate }
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate static void *
830*7c478bd9Sstevel@tonic-gate elf_map_textdata(caddr_t addr, Off flen, int mperm, int phdr_mperm, int mflag,
831*7c478bd9Sstevel@tonic-gate     int fd, Off foff)
832*7c478bd9Sstevel@tonic-gate {
833*7c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
834*7c478bd9Sstevel@tonic-gate 	static int	notd = 0;
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 	/*
837*7c478bd9Sstevel@tonic-gate 	 * If MAP_TEXT and MAP_INITDATA are available, select the appropriate
838*7c478bd9Sstevel@tonic-gate 	 * flag.
839*7c478bd9Sstevel@tonic-gate 	 */
840*7c478bd9Sstevel@tonic-gate 	if (notd == 0) {
841*7c478bd9Sstevel@tonic-gate 		if ((phdr_mperm & (PROT_WRITE | PROT_EXEC)) == PROT_EXEC)
842*7c478bd9Sstevel@tonic-gate 			mflag |= MAP_TEXT;
843*7c478bd9Sstevel@tonic-gate 		else
844*7c478bd9Sstevel@tonic-gate 			mflag |= MAP_INITDATA;
845*7c478bd9Sstevel@tonic-gate 	}
846*7c478bd9Sstevel@tonic-gate #endif
847*7c478bd9Sstevel@tonic-gate 	if (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff) != MAP_FAILED)
848*7c478bd9Sstevel@tonic-gate 		return (0);
849*7c478bd9Sstevel@tonic-gate 
850*7c478bd9Sstevel@tonic-gate #if	defined(MAP_TEXT) && defined(MAP_INITDATA)
851*7c478bd9Sstevel@tonic-gate 	if ((notd == 0) && (errno == EINVAL)) {
852*7c478bd9Sstevel@tonic-gate 		/*
853*7c478bd9Sstevel@tonic-gate 		 * MAP_TEXT and MAP_INITDATA may not be supported on this
854*7c478bd9Sstevel@tonic-gate 		 * platform, try again without.
855*7c478bd9Sstevel@tonic-gate 		 */
856*7c478bd9Sstevel@tonic-gate 		notd = 1;
857*7c478bd9Sstevel@tonic-gate 		mflag &= ~(MAP_TEXT | MAP_INITDATA);
858*7c478bd9Sstevel@tonic-gate 
859*7c478bd9Sstevel@tonic-gate 		return (mmap((caddr_t)addr, flen, mperm, mflag, fd, foff));
860*7c478bd9Sstevel@tonic-gate 	}
861*7c478bd9Sstevel@tonic-gate #endif
862*7c478bd9Sstevel@tonic-gate 	return (MAP_FAILED);
863*7c478bd9Sstevel@tonic-gate }
864*7c478bd9Sstevel@tonic-gate 
865*7c478bd9Sstevel@tonic-gate /*
866*7c478bd9Sstevel@tonic-gate  * Map in a file.
867*7c478bd9Sstevel@tonic-gate  */
868*7c478bd9Sstevel@tonic-gate static caddr_t
869*7c478bd9Sstevel@tonic-gate elf_map_it(
870*7c478bd9Sstevel@tonic-gate 	const char	*name,		/* actual name stored for pathname */
871*7c478bd9Sstevel@tonic-gate 	Off		fsize,		/* total mapping claim of the file */
872*7c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr,		/* ELF header of file */
873*7c478bd9Sstevel@tonic-gate 	Phdr		*fphdr,		/* first loadable Phdr */
874*7c478bd9Sstevel@tonic-gate 	Phdr		*lphdr,		/* last loadable Phdr */
875*7c478bd9Sstevel@tonic-gate 	Phdr		**rrphdr,	/* return first Phdr in reservation */
876*7c478bd9Sstevel@tonic-gate 	caddr_t		*rraddr,	/* return start of reservation */
877*7c478bd9Sstevel@tonic-gate 	Off		*rrsize,	/* return total size of reservation */
878*7c478bd9Sstevel@tonic-gate 	int		fixed,		/* image is resolved to a fixed addr */
879*7c478bd9Sstevel@tonic-gate 	int		fd,		/* images file descriptor */
880*7c478bd9Sstevel@tonic-gate 	Xword		align,		/* image segments maximum alignment */
881*7c478bd9Sstevel@tonic-gate 	Mmap		*mmaps,		/* mmap information array and */
882*7c478bd9Sstevel@tonic-gate 	uint_t		*mmapcnt)	/* 	mapping count */
883*7c478bd9Sstevel@tonic-gate {
884*7c478bd9Sstevel@tonic-gate 	caddr_t		raddr;		/* reservation address */
885*7c478bd9Sstevel@tonic-gate 	Off		rsize;		/* reservation size */
886*7c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* working program header poiner */
887*7c478bd9Sstevel@tonic-gate 	caddr_t		maddr;		/* working mmap address */
888*7c478bd9Sstevel@tonic-gate 	caddr_t		faddr;		/* working file address */
889*7c478bd9Sstevel@tonic-gate 	size_t		padsize;	/* object padding requirement */
890*7c478bd9Sstevel@tonic-gate 	size_t		padpsize = 0;	/* padding size rounded to next page */
891*7c478bd9Sstevel@tonic-gate 	size_t		padmsize = 0;	/* padding size rounded for alignment */
892*7c478bd9Sstevel@tonic-gate 	int		skipfseg;	/* skip mapping first segment */
893*7c478bd9Sstevel@tonic-gate 	int		mperm;		/* segment permissions */
894*7c478bd9Sstevel@tonic-gate 	Am_ret		amret = AM_NOSUP;
895*7c478bd9Sstevel@tonic-gate 
896*7c478bd9Sstevel@tonic-gate 	/*
897*7c478bd9Sstevel@tonic-gate 	 * If padding is required extend both the front and rear of the image.
898*7c478bd9Sstevel@tonic-gate 	 * To insure the image itself is mapped at the correct alignment the
899*7c478bd9Sstevel@tonic-gate 	 * initial padding is rounded up to the nearest page.  Once the image is
900*7c478bd9Sstevel@tonic-gate 	 * mapped the excess can be pruned to the nearest page required for the
901*7c478bd9Sstevel@tonic-gate 	 * actual padding itself.
902*7c478bd9Sstevel@tonic-gate 	 */
903*7c478bd9Sstevel@tonic-gate 	if ((padsize = r_debug.rtd_objpad) != 0) {
904*7c478bd9Sstevel@tonic-gate 		padpsize = M_PROUND(padsize);
905*7c478bd9Sstevel@tonic-gate 		if (fixed)
906*7c478bd9Sstevel@tonic-gate 			padmsize = padpsize;
907*7c478bd9Sstevel@tonic-gate 		else
908*7c478bd9Sstevel@tonic-gate 			padmsize = S_ROUND(padsize, align);
909*7c478bd9Sstevel@tonic-gate 	}
910*7c478bd9Sstevel@tonic-gate 
911*7c478bd9Sstevel@tonic-gate 	/*
912*7c478bd9Sstevel@tonic-gate 	 * Determine the initial permissions used to map in the first segment.
913*7c478bd9Sstevel@tonic-gate 	 * If this segments memsz is greater that its filesz then the difference
914*7c478bd9Sstevel@tonic-gate 	 * must be zeroed.  Make sure this segment is writable.
915*7c478bd9Sstevel@tonic-gate 	 */
916*7c478bd9Sstevel@tonic-gate 	mperm = 0;
917*7c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_R)
918*7c478bd9Sstevel@tonic-gate 		mperm |= PROT_READ;
919*7c478bd9Sstevel@tonic-gate 	if (fphdr->p_flags & PF_X)
920*7c478bd9Sstevel@tonic-gate 		mperm |= PROT_EXEC;
921*7c478bd9Sstevel@tonic-gate 	if ((fphdr->p_flags & PF_W) || (fphdr->p_memsz > fphdr->p_filesz))
922*7c478bd9Sstevel@tonic-gate 		mperm |= PROT_WRITE;
923*7c478bd9Sstevel@tonic-gate 
924*7c478bd9Sstevel@tonic-gate 	/*
925*7c478bd9Sstevel@tonic-gate 	 * Determine whether or not to let system reserve address space based on
926*7c478bd9Sstevel@tonic-gate 	 * whether this is a dynamic executable (addresses in object are fixed)
927*7c478bd9Sstevel@tonic-gate 	 * or a shared object (addresses in object are relative to the objects'
928*7c478bd9Sstevel@tonic-gate 	 * base).
929*7c478bd9Sstevel@tonic-gate 	 */
930*7c478bd9Sstevel@tonic-gate 	if (fixed) {
931*7c478bd9Sstevel@tonic-gate 		/*
932*7c478bd9Sstevel@tonic-gate 		 * Determine the reservation address and size, and insure that
933*7c478bd9Sstevel@tonic-gate 		 * this reservation isn't already in use.
934*7c478bd9Sstevel@tonic-gate 		 */
935*7c478bd9Sstevel@tonic-gate 		faddr = maddr = (caddr_t)M_PTRUNC((ulong_t)fphdr->p_vaddr);
936*7c478bd9Sstevel@tonic-gate 		raddr = maddr - padpsize;
937*7c478bd9Sstevel@tonic-gate 		rsize = fsize + padpsize + padsize;
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate 		if (lml_main.lm_head) {
940*7c478bd9Sstevel@tonic-gate 			if (elf_map_check(name, raddr, rsize) != 0)
941*7c478bd9Sstevel@tonic-gate 				return (0);
942*7c478bd9Sstevel@tonic-gate 		}
943*7c478bd9Sstevel@tonic-gate 
944*7c478bd9Sstevel@tonic-gate 		/*
945*7c478bd9Sstevel@tonic-gate 		 * As this is a fixed image, all segments must be individually
946*7c478bd9Sstevel@tonic-gate 		 * mapped.
947*7c478bd9Sstevel@tonic-gate 		 */
948*7c478bd9Sstevel@tonic-gate 		skipfseg = 0;
949*7c478bd9Sstevel@tonic-gate 
950*7c478bd9Sstevel@tonic-gate 	} else {
951*7c478bd9Sstevel@tonic-gate 		size_t	esize;
952*7c478bd9Sstevel@tonic-gate 
953*7c478bd9Sstevel@tonic-gate 		/*
954*7c478bd9Sstevel@tonic-gate 		 * If this isn't a fixed image, reserve enough address space for
955*7c478bd9Sstevel@tonic-gate 		 * the entire image to be mapped.  The amount of reservation is
956*7c478bd9Sstevel@tonic-gate 		 * the range between the beginning of the first, and end of the
957*7c478bd9Sstevel@tonic-gate 		 * last loadable segment, together with any padding, plus the
958*7c478bd9Sstevel@tonic-gate 		 * alignment of the first segment.
959*7c478bd9Sstevel@tonic-gate 		 *
960*7c478bd9Sstevel@tonic-gate 		 * The optimal reservation is made as a no-reserve mapping from
961*7c478bd9Sstevel@tonic-gate 		 * anonymous memory.  Each segment is then mapped into this
962*7c478bd9Sstevel@tonic-gate 		 * reservation.  If the anonymous mapping capability isn't
963*7c478bd9Sstevel@tonic-gate 		 * available, the reservation is obtained from the file itself.
964*7c478bd9Sstevel@tonic-gate 		 * In this case the first segment of the image is mapped as part
965*7c478bd9Sstevel@tonic-gate 		 * of the reservation, thus only the following segments need to
966*7c478bd9Sstevel@tonic-gate 		 * be remapped.
967*7c478bd9Sstevel@tonic-gate 		 */
968*7c478bd9Sstevel@tonic-gate 		rsize = fsize + padmsize + padsize;
969*7c478bd9Sstevel@tonic-gate 		if ((amret = elf_map_reserve(name, &raddr, rsize, mperm,
970*7c478bd9Sstevel@tonic-gate 		    fd, align)) == AM_ERROR)
971*7c478bd9Sstevel@tonic-gate 			return (0);
972*7c478bd9Sstevel@tonic-gate 		maddr = raddr + padmsize;
973*7c478bd9Sstevel@tonic-gate 		faddr = (caddr_t)S_ROUND((Off)maddr, align);
974*7c478bd9Sstevel@tonic-gate 
975*7c478bd9Sstevel@tonic-gate 		/*
976*7c478bd9Sstevel@tonic-gate 		 * If this reservation has been obtained from anonymous memory,
977*7c478bd9Sstevel@tonic-gate 		 * then all segments must be individually mapped.  Otherwise,
978*7c478bd9Sstevel@tonic-gate 		 * the first segment heads the reservation.
979*7c478bd9Sstevel@tonic-gate 		 */
980*7c478bd9Sstevel@tonic-gate 		if (amret == AM_OK)
981*7c478bd9Sstevel@tonic-gate 			skipfseg = 0;
982*7c478bd9Sstevel@tonic-gate 		else
983*7c478bd9Sstevel@tonic-gate 			skipfseg = 1;
984*7c478bd9Sstevel@tonic-gate 
985*7c478bd9Sstevel@tonic-gate 		/*
986*7c478bd9Sstevel@tonic-gate 		 * For backward compatibility (where MAP_ALIGN isn't available),
987*7c478bd9Sstevel@tonic-gate 		 * insure the alignment of the reservation is adequate for this
988*7c478bd9Sstevel@tonic-gate 		 * object, and if not remap the object to obtain the correct
989*7c478bd9Sstevel@tonic-gate 		 * alignment.
990*7c478bd9Sstevel@tonic-gate 		 */
991*7c478bd9Sstevel@tonic-gate 		if (faddr != maddr) {
992*7c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, rsize);
993*7c478bd9Sstevel@tonic-gate 
994*7c478bd9Sstevel@tonic-gate 			rsize += align;
995*7c478bd9Sstevel@tonic-gate 			if ((amret = elf_map_reserve(name, &raddr, rsize, mperm,
996*7c478bd9Sstevel@tonic-gate 			    fd, align)) == AM_ERROR)
997*7c478bd9Sstevel@tonic-gate 				return (0);
998*7c478bd9Sstevel@tonic-gate 
999*7c478bd9Sstevel@tonic-gate 			maddr = faddr = (caddr_t)S_ROUND((Off)(raddr +
1000*7c478bd9Sstevel@tonic-gate 			    padpsize), align);
1001*7c478bd9Sstevel@tonic-gate 
1002*7c478bd9Sstevel@tonic-gate 			esize = maddr - raddr + padpsize;
1003*7c478bd9Sstevel@tonic-gate 
1004*7c478bd9Sstevel@tonic-gate 			/*
1005*7c478bd9Sstevel@tonic-gate 			 * As ths image has been realigned, the first segment
1006*7c478bd9Sstevel@tonic-gate 			 * of the file needs to be remapped to its correct
1007*7c478bd9Sstevel@tonic-gate 			 * location.
1008*7c478bd9Sstevel@tonic-gate 			 */
1009*7c478bd9Sstevel@tonic-gate 			skipfseg = 0;
1010*7c478bd9Sstevel@tonic-gate 		} else
1011*7c478bd9Sstevel@tonic-gate 			esize = padmsize - padpsize;
1012*7c478bd9Sstevel@tonic-gate 
1013*7c478bd9Sstevel@tonic-gate 		/*
1014*7c478bd9Sstevel@tonic-gate 		 * If this reservation included padding, remove any excess for
1015*7c478bd9Sstevel@tonic-gate 		 * the start of the image (the padding was adjusted to insure
1016*7c478bd9Sstevel@tonic-gate 		 * the image was aligned appropriately).
1017*7c478bd9Sstevel@tonic-gate 		 */
1018*7c478bd9Sstevel@tonic-gate 		if (esize) {
1019*7c478bd9Sstevel@tonic-gate 			(void) munmap(raddr, esize);
1020*7c478bd9Sstevel@tonic-gate 			raddr += esize;
1021*7c478bd9Sstevel@tonic-gate 			rsize -= esize;
1022*7c478bd9Sstevel@tonic-gate 		}
1023*7c478bd9Sstevel@tonic-gate 	}
1024*7c478bd9Sstevel@tonic-gate 
1025*7c478bd9Sstevel@tonic-gate 	/*
1026*7c478bd9Sstevel@tonic-gate 	 * At this point we know the initial location of the image, and its
1027*7c478bd9Sstevel@tonic-gate 	 * size.  Pass these back to the caller for inclusion in the link-map
1028*7c478bd9Sstevel@tonic-gate 	 * that will eventually be created.
1029*7c478bd9Sstevel@tonic-gate 	 */
1030*7c478bd9Sstevel@tonic-gate 	*rraddr = raddr;
1031*7c478bd9Sstevel@tonic-gate 	*rrsize = rsize;
1032*7c478bd9Sstevel@tonic-gate 
1033*7c478bd9Sstevel@tonic-gate 	/*
1034*7c478bd9Sstevel@tonic-gate 	 * The first loadable segment is now pointed to by maddr.  This segment
1035*7c478bd9Sstevel@tonic-gate 	 * will eventually contain the elf header and program headers, so reset
1036*7c478bd9Sstevel@tonic-gate 	 * the program header.  Pass this  back to the caller for inclusion in
1037*7c478bd9Sstevel@tonic-gate 	 * the link-map so it can be used for later unmapping operations.
1038*7c478bd9Sstevel@tonic-gate 	 */
1039*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1040*7c478bd9Sstevel@tonic-gate 	*rrphdr = (Phdr *)((char *)maddr + ehdr->e_phoff);
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate 	/*
1043*7c478bd9Sstevel@tonic-gate 	 * If padding is required at the front of the image, obtain that now.
1044*7c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
1045*7c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
1046*7c478bd9Sstevel@tonic-gate 	 * Otherwise this reservation is backed by the file, or in the case of
1047*7c478bd9Sstevel@tonic-gate 	 * a fixed image, doesn't yet exist.  Map the padding so that it is
1048*7c478bd9Sstevel@tonic-gate 	 * suitably protected (PROT_NONE), and insure the first segment of the
1049*7c478bd9Sstevel@tonic-gate 	 * file is mapped to its correct location.
1050*7c478bd9Sstevel@tonic-gate 	 */
1051*7c478bd9Sstevel@tonic-gate 	if (padsize) {
1052*7c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
1053*7c478bd9Sstevel@tonic-gate 			if (dz_map(raddr, padpsize, PROT_NONE, (MAP_PRIVATE |
1054*7c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_NORESERVE)) == MAP_FAILED)
1055*7c478bd9Sstevel@tonic-gate 				return (0);
1056*7c478bd9Sstevel@tonic-gate 
1057*7c478bd9Sstevel@tonic-gate 			skipfseg = 0;
1058*7c478bd9Sstevel@tonic-gate 		}
1059*7c478bd9Sstevel@tonic-gate 		rsize -= padpsize;
1060*7c478bd9Sstevel@tonic-gate 	}
1061*7c478bd9Sstevel@tonic-gate 
1062*7c478bd9Sstevel@tonic-gate 	/*
1063*7c478bd9Sstevel@tonic-gate 	 * Map individual segments.  For a fixed image, these will each be
1064*7c478bd9Sstevel@tonic-gate 	 * unique mappings.  For a reservation these will fill in the
1065*7c478bd9Sstevel@tonic-gate 	 * reservation.
1066*7c478bd9Sstevel@tonic-gate 	 */
1067*7c478bd9Sstevel@tonic-gate 	for (phdr = fphdr; phdr <= lphdr;
1068*7c478bd9Sstevel@tonic-gate 	    phdr = (Phdr *)((Off)phdr + ehdr->e_phentsize)) {
1069*7c478bd9Sstevel@tonic-gate 		caddr_t	addr;
1070*7c478bd9Sstevel@tonic-gate 		Off	mlen, flen;
1071*7c478bd9Sstevel@tonic-gate 		size_t	size;
1072*7c478bd9Sstevel@tonic-gate 
1073*7c478bd9Sstevel@tonic-gate 		/*
1074*7c478bd9Sstevel@tonic-gate 		 * Skip non-loadable segments or segments that don't occupy
1075*7c478bd9Sstevel@tonic-gate 		 * any memory.
1076*7c478bd9Sstevel@tonic-gate 		 */
1077*7c478bd9Sstevel@tonic-gate 		if (((phdr->p_type != PT_LOAD) &&
1078*7c478bd9Sstevel@tonic-gate 		    (phdr->p_type != PT_SUNWBSS)) || (phdr->p_memsz == 0))
1079*7c478bd9Sstevel@tonic-gate 			continue;
1080*7c478bd9Sstevel@tonic-gate 
1081*7c478bd9Sstevel@tonic-gate 		/*
1082*7c478bd9Sstevel@tonic-gate 		 * Establish this segments address relative to our base.
1083*7c478bd9Sstevel@tonic-gate 		 */
1084*7c478bd9Sstevel@tonic-gate 		addr = (caddr_t)M_PTRUNC((ulong_t)(phdr->p_vaddr +
1085*7c478bd9Sstevel@tonic-gate 		    (fixed ? 0 : faddr)));
1086*7c478bd9Sstevel@tonic-gate 
1087*7c478bd9Sstevel@tonic-gate 		/*
1088*7c478bd9Sstevel@tonic-gate 		 * Determine the mapping protection from the segment attributes.
1089*7c478bd9Sstevel@tonic-gate 		 * Also determine the etext address from the last loadable
1090*7c478bd9Sstevel@tonic-gate 		 * segment which has permissions but no write access.
1091*7c478bd9Sstevel@tonic-gate 		 */
1092*7c478bd9Sstevel@tonic-gate 		mperm = 0;
1093*7c478bd9Sstevel@tonic-gate 		if (phdr->p_flags) {
1094*7c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_R)
1095*7c478bd9Sstevel@tonic-gate 				mperm |= PROT_READ;
1096*7c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_X)
1097*7c478bd9Sstevel@tonic-gate 				mperm |= PROT_EXEC;
1098*7c478bd9Sstevel@tonic-gate 			if (phdr->p_flags & PF_W)
1099*7c478bd9Sstevel@tonic-gate 				mperm |= PROT_WRITE;
1100*7c478bd9Sstevel@tonic-gate 			else
1101*7c478bd9Sstevel@tonic-gate 				fmap->fm_etext = phdr->p_vaddr + phdr->p_memsz +
1102*7c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
1103*7c478bd9Sstevel@tonic-gate 		}
1104*7c478bd9Sstevel@tonic-gate 
1105*7c478bd9Sstevel@tonic-gate 		/*
1106*7c478bd9Sstevel@tonic-gate 		 * Determine the type of mapping required.
1107*7c478bd9Sstevel@tonic-gate 		 */
1108*7c478bd9Sstevel@tonic-gate 		if (phdr->p_type == PT_SUNWBSS) {
1109*7c478bd9Sstevel@tonic-gate 			/*
1110*7c478bd9Sstevel@tonic-gate 			 * Potentially, we can defer the loading of any SUNWBSS
1111*7c478bd9Sstevel@tonic-gate 			 * segment, depending on whether the symbols it provides
1112*7c478bd9Sstevel@tonic-gate 			 * have been bound to.  In this manner, large segments
1113*7c478bd9Sstevel@tonic-gate 			 * that are interposed upon between shared libraries
1114*7c478bd9Sstevel@tonic-gate 			 * may not require mapping.  Note, that the mapping
1115*7c478bd9Sstevel@tonic-gate 			 * information is recorded in our mapping descriptor at
1116*7c478bd9Sstevel@tonic-gate 			 * this time.
1117*7c478bd9Sstevel@tonic-gate 			 */
1118*7c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
1119*7c478bd9Sstevel@tonic-gate 			flen = 0;
1120*7c478bd9Sstevel@tonic-gate 
1121*7c478bd9Sstevel@tonic-gate 		} else if ((phdr->p_filesz == 0) && (phdr->p_flags == 0)) {
1122*7c478bd9Sstevel@tonic-gate 			/*
1123*7c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file and no flags
1124*7c478bd9Sstevel@tonic-gate 			 * specified, then it defines a reservation.  At this
1125*7c478bd9Sstevel@tonic-gate 			 * point all standard loadable segments will have been
1126*7c478bd9Sstevel@tonic-gate 			 * processed.  The segment reservation is mapped
1127*7c478bd9Sstevel@tonic-gate 			 * directly from /dev/null.
1128*7c478bd9Sstevel@tonic-gate 			 */
1129*7c478bd9Sstevel@tonic-gate 			if (nu_map((caddr_t)addr, phdr->p_memsz, PROT_NONE,
1130*7c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
1131*7c478bd9Sstevel@tonic-gate 				return (0);
1132*7c478bd9Sstevel@tonic-gate 
1133*7c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
1134*7c478bd9Sstevel@tonic-gate 			flen = 0;
1135*7c478bd9Sstevel@tonic-gate 
1136*7c478bd9Sstevel@tonic-gate 		} else if (phdr->p_filesz == 0) {
1137*7c478bd9Sstevel@tonic-gate 			/*
1138*7c478bd9Sstevel@tonic-gate 			 * If this segment has no backing file then it defines a
1139*7c478bd9Sstevel@tonic-gate 			 * nobits segment and is mapped directly from /dev/zero.
1140*7c478bd9Sstevel@tonic-gate 			 */
1141*7c478bd9Sstevel@tonic-gate 			if (dz_map((caddr_t)addr, phdr->p_memsz, mperm,
1142*7c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_PRIVATE) == MAP_FAILED)
1143*7c478bd9Sstevel@tonic-gate 				return (0);
1144*7c478bd9Sstevel@tonic-gate 
1145*7c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz;
1146*7c478bd9Sstevel@tonic-gate 			flen = 0;
1147*7c478bd9Sstevel@tonic-gate 
1148*7c478bd9Sstevel@tonic-gate 		} else {
1149*7c478bd9Sstevel@tonic-gate 			Off	foff;
1150*7c478bd9Sstevel@tonic-gate 
1151*7c478bd9Sstevel@tonic-gate 			/*
1152*7c478bd9Sstevel@tonic-gate 			 * This mapping originates from the file.  Determine the
1153*7c478bd9Sstevel@tonic-gate 			 * file offset to which the mapping will be directed
1154*7c478bd9Sstevel@tonic-gate 			 * (must be aligned) and how much to map (might be more
1155*7c478bd9Sstevel@tonic-gate 			 * than the file in the case of .bss).
1156*7c478bd9Sstevel@tonic-gate 			 */
1157*7c478bd9Sstevel@tonic-gate 			foff = M_PTRUNC((ulong_t)phdr->p_offset);
1158*7c478bd9Sstevel@tonic-gate 			mlen = phdr->p_memsz + (phdr->p_offset - foff);
1159*7c478bd9Sstevel@tonic-gate 			flen = phdr->p_filesz + (phdr->p_offset - foff);
1160*7c478bd9Sstevel@tonic-gate 
1161*7c478bd9Sstevel@tonic-gate 			/*
1162*7c478bd9Sstevel@tonic-gate 			 * If this is a non-fixed, non-anonymous mapping, and no
1163*7c478bd9Sstevel@tonic-gate 			 * padding is involved, then the first loadable segment
1164*7c478bd9Sstevel@tonic-gate 			 * is already part of the initial reservation.  In this
1165*7c478bd9Sstevel@tonic-gate 			 * case there is no need to remap this segment.
1166*7c478bd9Sstevel@tonic-gate 			 */
1167*7c478bd9Sstevel@tonic-gate 			if ((skipfseg == 0) || (phdr != fphdr)) {
1168*7c478bd9Sstevel@tonic-gate 				int phdr_mperm = mperm;
1169*7c478bd9Sstevel@tonic-gate 				/*
1170*7c478bd9Sstevel@tonic-gate 				 * If this segments memsz is greater that its
1171*7c478bd9Sstevel@tonic-gate 				 * filesz then the difference must be zeroed.
1172*7c478bd9Sstevel@tonic-gate 				 * Make sure this segment is writable.
1173*7c478bd9Sstevel@tonic-gate 				 */
1174*7c478bd9Sstevel@tonic-gate 				if (phdr->p_memsz > phdr->p_filesz)
1175*7c478bd9Sstevel@tonic-gate 					mperm |= PROT_WRITE;
1176*7c478bd9Sstevel@tonic-gate 
1177*7c478bd9Sstevel@tonic-gate 				if (elf_map_textdata((caddr_t)addr, flen,
1178*7c478bd9Sstevel@tonic-gate 				    mperm, phdr_mperm,
1179*7c478bd9Sstevel@tonic-gate 				    (MAP_FIXED | MAP_PRIVATE), fd, foff) ==
1180*7c478bd9Sstevel@tonic-gate 				    MAP_FAILED) {
1181*7c478bd9Sstevel@tonic-gate 					int	err = errno;
1182*7c478bd9Sstevel@tonic-gate 					eprintf(ERR_FATAL,
1183*7c478bd9Sstevel@tonic-gate 					    MSG_INTL(MSG_SYS_MMAP), name,
1184*7c478bd9Sstevel@tonic-gate 					    strerror(err));
1185*7c478bd9Sstevel@tonic-gate 					return (0);
1186*7c478bd9Sstevel@tonic-gate 				}
1187*7c478bd9Sstevel@tonic-gate 			}
1188*7c478bd9Sstevel@tonic-gate 
1189*7c478bd9Sstevel@tonic-gate 			/*
1190*7c478bd9Sstevel@tonic-gate 			 * If the memory occupancy of the segment overflows the
1191*7c478bd9Sstevel@tonic-gate 			 * definition in the file, we need to "zero out" the end
1192*7c478bd9Sstevel@tonic-gate 			 * of the mapping we've established, and if necessary,
1193*7c478bd9Sstevel@tonic-gate 			 * map some more space from /dev/zero.  Note, zero'ed
1194*7c478bd9Sstevel@tonic-gate 			 * memory must end on a double word boundary to satisfy
1195*7c478bd9Sstevel@tonic-gate 			 * zero().
1196*7c478bd9Sstevel@tonic-gate 			 */
1197*7c478bd9Sstevel@tonic-gate 			if (phdr->p_memsz > phdr->p_filesz) {
1198*7c478bd9Sstevel@tonic-gate 				caddr_t	zaddr;
1199*7c478bd9Sstevel@tonic-gate 				size_t	zlen, zplen;
1200*7c478bd9Sstevel@tonic-gate 				Off	fend;
1201*7c478bd9Sstevel@tonic-gate 
1202*7c478bd9Sstevel@tonic-gate 				foff = (Off)(phdr->p_vaddr + phdr->p_filesz +
1203*7c478bd9Sstevel@tonic-gate 				    (fixed ? 0 : faddr));
1204*7c478bd9Sstevel@tonic-gate 				zaddr = (caddr_t)M_PROUND(foff);
1205*7c478bd9Sstevel@tonic-gate 				zplen = (size_t)(zaddr - foff);
1206*7c478bd9Sstevel@tonic-gate 
1207*7c478bd9Sstevel@tonic-gate 				fend = (Off)S_DROUND((size_t)(phdr->p_vaddr +
1208*7c478bd9Sstevel@tonic-gate 				    phdr->p_memsz + (fixed ? 0 : faddr)));
1209*7c478bd9Sstevel@tonic-gate 				zlen = (size_t)(fend - foff);
1210*7c478bd9Sstevel@tonic-gate 
1211*7c478bd9Sstevel@tonic-gate 				/*
1212*7c478bd9Sstevel@tonic-gate 				 * Determine whether the number of bytes that
1213*7c478bd9Sstevel@tonic-gate 				 * must be zero'ed overflow to the next page.
1214*7c478bd9Sstevel@tonic-gate 				 * If not, simply clear the exact bytes
1215*7c478bd9Sstevel@tonic-gate 				 * (filesz to memsz) from this page.  Otherwise,
1216*7c478bd9Sstevel@tonic-gate 				 * clear the remaining bytes of this page, and
1217*7c478bd9Sstevel@tonic-gate 				 * map an following pages from /dev/zero.
1218*7c478bd9Sstevel@tonic-gate 				 */
1219*7c478bd9Sstevel@tonic-gate 				if (zlen < zplen)
1220*7c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zlen);
1221*7c478bd9Sstevel@tonic-gate 				else {
1222*7c478bd9Sstevel@tonic-gate 					zero((caddr_t)foff, (long)zplen);
1223*7c478bd9Sstevel@tonic-gate 
1224*7c478bd9Sstevel@tonic-gate 					if ((zlen = (fend - (Off)zaddr)) > 0) {
1225*7c478bd9Sstevel@tonic-gate 						if (dz_map(zaddr, zlen, mperm,
1226*7c478bd9Sstevel@tonic-gate 						    MAP_FIXED | MAP_PRIVATE) ==
1227*7c478bd9Sstevel@tonic-gate 						    MAP_FAILED)
1228*7c478bd9Sstevel@tonic-gate 							return (0);
1229*7c478bd9Sstevel@tonic-gate 					}
1230*7c478bd9Sstevel@tonic-gate 				}
1231*7c478bd9Sstevel@tonic-gate 			}
1232*7c478bd9Sstevel@tonic-gate 		}
1233*7c478bd9Sstevel@tonic-gate 
1234*7c478bd9Sstevel@tonic-gate 		/*
1235*7c478bd9Sstevel@tonic-gate 		 * Unmap anything from the last mapping address to this one and
1236*7c478bd9Sstevel@tonic-gate 		 * update the mapping claim pointer.
1237*7c478bd9Sstevel@tonic-gate 		 */
1238*7c478bd9Sstevel@tonic-gate 		if ((fixed == 0) && ((size = addr - maddr) != 0)) {
1239*7c478bd9Sstevel@tonic-gate 			(void) munmap(maddr, size);
1240*7c478bd9Sstevel@tonic-gate 			rsize -= size;
1241*7c478bd9Sstevel@tonic-gate 		}
1242*7c478bd9Sstevel@tonic-gate 
1243*7c478bd9Sstevel@tonic-gate 		/*
1244*7c478bd9Sstevel@tonic-gate 		 * Retain this segments mapping information.
1245*7c478bd9Sstevel@tonic-gate 		 */
1246*7c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_vaddr = addr;
1247*7c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_msize = mlen;
1248*7c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_fsize = flen;
1249*7c478bd9Sstevel@tonic-gate 		mmaps[*mmapcnt].m_perm = mperm;
1250*7c478bd9Sstevel@tonic-gate 		(*mmapcnt)++;
1251*7c478bd9Sstevel@tonic-gate 
1252*7c478bd9Sstevel@tonic-gate 		maddr = addr + M_PROUND(mlen);
1253*7c478bd9Sstevel@tonic-gate 		rsize -= M_PROUND(mlen);
1254*7c478bd9Sstevel@tonic-gate 	}
1255*7c478bd9Sstevel@tonic-gate 
1256*7c478bd9Sstevel@tonic-gate 	/*
1257*7c478bd9Sstevel@tonic-gate 	 * If padding is required at the end of the image, obtain that now.
1258*7c478bd9Sstevel@tonic-gate 	 * Note, if we've already obtained a reservation from anonymous memory
1259*7c478bd9Sstevel@tonic-gate 	 * then this reservation will already include suitable padding.
1260*7c478bd9Sstevel@tonic-gate 	 */
1261*7c478bd9Sstevel@tonic-gate 	if (padsize) {
1262*7c478bd9Sstevel@tonic-gate 		if (amret == AM_NOSUP) {
1263*7c478bd9Sstevel@tonic-gate 			/*
1264*7c478bd9Sstevel@tonic-gate 			 * maddr is currently page aligned from the last segment
1265*7c478bd9Sstevel@tonic-gate 			 * mapping.
1266*7c478bd9Sstevel@tonic-gate 			 */
1267*7c478bd9Sstevel@tonic-gate 			if (dz_map(maddr, padsize, PROT_NONE, (MAP_PRIVATE |
1268*7c478bd9Sstevel@tonic-gate 			    MAP_FIXED | MAP_NORESERVE)) == MAP_FAILED)
1269*7c478bd9Sstevel@tonic-gate 				return (0);
1270*7c478bd9Sstevel@tonic-gate 		}
1271*7c478bd9Sstevel@tonic-gate 		maddr += padsize;
1272*7c478bd9Sstevel@tonic-gate 		rsize -= padsize;
1273*7c478bd9Sstevel@tonic-gate 	}
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate 	/*
1276*7c478bd9Sstevel@tonic-gate 	 * Unmap any final reservation.
1277*7c478bd9Sstevel@tonic-gate 	 */
1278*7c478bd9Sstevel@tonic-gate 	if ((fixed == 0) && (rsize != 0))
1279*7c478bd9Sstevel@tonic-gate 		(void) munmap(maddr, rsize);
1280*7c478bd9Sstevel@tonic-gate 
1281*7c478bd9Sstevel@tonic-gate 	return (faddr);
1282*7c478bd9Sstevel@tonic-gate }
1283*7c478bd9Sstevel@tonic-gate 
1284*7c478bd9Sstevel@tonic-gate /*
1285*7c478bd9Sstevel@tonic-gate  * A null symbol interpretor.  Used if a filter has no associated filtees.
1286*7c478bd9Sstevel@tonic-gate  */
1287*7c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
1288*7c478bd9Sstevel@tonic-gate static Sym *
1289*7c478bd9Sstevel@tonic-gate elf_null_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
1290*7c478bd9Sstevel@tonic-gate {
1291*7c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
1292*7c478bd9Sstevel@tonic-gate }
1293*7c478bd9Sstevel@tonic-gate 
1294*7c478bd9Sstevel@tonic-gate /*
1295*7c478bd9Sstevel@tonic-gate  * Disable filtee use.
1296*7c478bd9Sstevel@tonic-gate  */
1297*7c478bd9Sstevel@tonic-gate static void
1298*7c478bd9Sstevel@tonic-gate elf_disable_filtee(Rt_map * lmp, Dyninfo * dip)
1299*7c478bd9Sstevel@tonic-gate {
1300*7c478bd9Sstevel@tonic-gate 	dip->di_info = 0;
1301*7c478bd9Sstevel@tonic-gate 
1302*7c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) {
1303*7c478bd9Sstevel@tonic-gate 		/*
1304*7c478bd9Sstevel@tonic-gate 		 * If this is an object filter, free the filtee's duplication.
1305*7c478bd9Sstevel@tonic-gate 		 */
1306*7c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(lmp) != FLTR_DISABLED) {
1307*7c478bd9Sstevel@tonic-gate 			free(REFNAME(lmp));
1308*7c478bd9Sstevel@tonic-gate 			REFNAME(lmp) = (char *)0;
1309*7c478bd9Sstevel@tonic-gate 			OBJFLTRNDX(lmp) = FLTR_DISABLED;
1310*7c478bd9Sstevel@tonic-gate 
1311*7c478bd9Sstevel@tonic-gate 			/*
1312*7c478bd9Sstevel@tonic-gate 			 * Indicate that this filtee is no longer available.
1313*7c478bd9Sstevel@tonic-gate 			 */
1314*7c478bd9Sstevel@tonic-gate 			if (dip->di_flags & FLG_DI_STDFLTR)
1315*7c478bd9Sstevel@tonic-gate 				SYMINTP(lmp) = elf_null_find_sym;
1316*7c478bd9Sstevel@tonic-gate 
1317*7c478bd9Sstevel@tonic-gate 		}
1318*7c478bd9Sstevel@tonic-gate 	} else if (dip->di_flags & FLG_DI_STDFLTR) {
1319*7c478bd9Sstevel@tonic-gate 		/*
1320*7c478bd9Sstevel@tonic-gate 		 * Indicate that this standard filtee is no longer available.
1321*7c478bd9Sstevel@tonic-gate 		 */
1322*7c478bd9Sstevel@tonic-gate 		if (SYMSFLTRCNT(lmp))
1323*7c478bd9Sstevel@tonic-gate 			SYMSFLTRCNT(lmp)--;
1324*7c478bd9Sstevel@tonic-gate 	} else {
1325*7c478bd9Sstevel@tonic-gate 		/*
1326*7c478bd9Sstevel@tonic-gate 		 * Indicate that this auxiliary filtee is no longer available.
1327*7c478bd9Sstevel@tonic-gate 		 */
1328*7c478bd9Sstevel@tonic-gate 		if (SYMAFLTRCNT(lmp))
1329*7c478bd9Sstevel@tonic-gate 			SYMAFLTRCNT(lmp)--;
1330*7c478bd9Sstevel@tonic-gate 	}
1331*7c478bd9Sstevel@tonic-gate 	dip->di_flags &= ~MSK_DI_FILTER;
1332*7c478bd9Sstevel@tonic-gate }
1333*7c478bd9Sstevel@tonic-gate 
1334*7c478bd9Sstevel@tonic-gate /*
1335*7c478bd9Sstevel@tonic-gate  * Find symbol interpreter - filters.
1336*7c478bd9Sstevel@tonic-gate  * This function is called when the symbols from a shared object should
1337*7c478bd9Sstevel@tonic-gate  * be resolved from the shared objects filtees instead of from within itself.
1338*7c478bd9Sstevel@tonic-gate  *
1339*7c478bd9Sstevel@tonic-gate  * A symbol name of 0 is used to trigger filtee loading.
1340*7c478bd9Sstevel@tonic-gate  */
1341*7c478bd9Sstevel@tonic-gate static Sym *
1342*7c478bd9Sstevel@tonic-gate _elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
1343*7c478bd9Sstevel@tonic-gate {
1344*7c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name, *filtees;
1345*7c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
1346*7c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
1347*7c478bd9Sstevel@tonic-gate 	Pnode		*pnp, **pnpp;
1348*7c478bd9Sstevel@tonic-gate 	int		any;
1349*7c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(ilmp)[ndx];
1350*7c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(ilmp);
1351*7c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc = 0;
1352*7c478bd9Sstevel@tonic-gate 	Aliste		lmco;
1353*7c478bd9Sstevel@tonic-gate 
1354*7c478bd9Sstevel@tonic-gate 	/*
1355*7c478bd9Sstevel@tonic-gate 	 * Indicate that the filter has been used.  If a binding already exists
1356*7c478bd9Sstevel@tonic-gate 	 * to the caller, indicate that this object is referenced.  This insures
1357*7c478bd9Sstevel@tonic-gate 	 * we don't generate false unreferenced diagnostics from ldd -u/U or
1358*7c478bd9Sstevel@tonic-gate 	 * debugging.  Don't create a binding regardless, as this filter may
1359*7c478bd9Sstevel@tonic-gate 	 * have been dlopen()'ed.
1360*7c478bd9Sstevel@tonic-gate 	 */
1361*7c478bd9Sstevel@tonic-gate 	if (name && (ilmp != clmp)) {
1362*7c478bd9Sstevel@tonic-gate 		Word	tracing = (LIST(clmp)->lm_flags &
1363*7c478bd9Sstevel@tonic-gate 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED));
1364*7c478bd9Sstevel@tonic-gate 
1365*7c478bd9Sstevel@tonic-gate 		if (tracing || dbg_mask) {
1366*7c478bd9Sstevel@tonic-gate 			Bnd_desc **	bdpp;
1367*7c478bd9Sstevel@tonic-gate 			Aliste		off;
1368*7c478bd9Sstevel@tonic-gate 
1369*7c478bd9Sstevel@tonic-gate 			FLAGS1(ilmp) |= FL1_RT_USED;
1370*7c478bd9Sstevel@tonic-gate 
1371*7c478bd9Sstevel@tonic-gate 			if ((tracing & LML_FLG_TRC_UNREF) || dbg_mask) {
1372*7c478bd9Sstevel@tonic-gate 				for (ALIST_TRAVERSE(CALLERS(ilmp), off, bdpp)) {
1373*7c478bd9Sstevel@tonic-gate 					Bnd_desc *	bdp = *bdpp;
1374*7c478bd9Sstevel@tonic-gate 
1375*7c478bd9Sstevel@tonic-gate 					if (bdp->b_caller == clmp) {
1376*7c478bd9Sstevel@tonic-gate 						bdp->b_flags |= BND_REFER;
1377*7c478bd9Sstevel@tonic-gate 						break;
1378*7c478bd9Sstevel@tonic-gate 					}
1379*7c478bd9Sstevel@tonic-gate 				}
1380*7c478bd9Sstevel@tonic-gate 			}
1381*7c478bd9Sstevel@tonic-gate 		}
1382*7c478bd9Sstevel@tonic-gate 	}
1383*7c478bd9Sstevel@tonic-gate 
1384*7c478bd9Sstevel@tonic-gate 	/*
1385*7c478bd9Sstevel@tonic-gate 	 * If this is the first call to process this filter, establish the
1386*7c478bd9Sstevel@tonic-gate 	 * filtee list.  If a configuration file exists, determine if any
1387*7c478bd9Sstevel@tonic-gate 	 * filtee associations for this filter, and its filtee reference, are
1388*7c478bd9Sstevel@tonic-gate 	 * defined.  Otherwise, process the filtee reference.  Any token
1389*7c478bd9Sstevel@tonic-gate 	 * expansion is also completed at this point (i.e., $PLATFORM).
1390*7c478bd9Sstevel@tonic-gate 	 */
1391*7c478bd9Sstevel@tonic-gate 	filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val;
1392*7c478bd9Sstevel@tonic-gate 	if (dip->di_info == 0) {
1393*7c478bd9Sstevel@tonic-gate 		if (rtld_flags2 & RT_FL2_FLTCFG)
1394*7c478bd9Sstevel@tonic-gate 			dip->di_info = elf_config_flt(PATHNAME(ilmp), filtees);
1395*7c478bd9Sstevel@tonic-gate 
1396*7c478bd9Sstevel@tonic-gate 		if (dip->di_info == 0) {
1397*7c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_file_filter(NAME(ilmp), filtees, 0));
1398*7c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags &
1399*7c478bd9Sstevel@tonic-gate 			    (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
1400*7c478bd9Sstevel@tonic-gate 			    ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0))
1401*7c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FILTER),
1402*7c478bd9Sstevel@tonic-gate 				    NAME(ilmp), filtees);
1403*7c478bd9Sstevel@tonic-gate 
1404*7c478bd9Sstevel@tonic-gate 			if ((dip->di_info = (void *)expand_paths(ilmp,
1405*7c478bd9Sstevel@tonic-gate 			    filtees, PN_SER_FILTEE, 0)) == 0) {
1406*7c478bd9Sstevel@tonic-gate 				elf_disable_filtee(ilmp, dip);
1407*7c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
1408*7c478bd9Sstevel@tonic-gate 			}
1409*7c478bd9Sstevel@tonic-gate 		}
1410*7c478bd9Sstevel@tonic-gate 	}
1411*7c478bd9Sstevel@tonic-gate 
1412*7c478bd9Sstevel@tonic-gate 	/*
1413*7c478bd9Sstevel@tonic-gate 	 * Traverse the filtee list, dlopen()'ing any objects specified and
1414*7c478bd9Sstevel@tonic-gate 	 * using their group handle to lookup the symbol.
1415*7c478bd9Sstevel@tonic-gate 	 */
1416*7c478bd9Sstevel@tonic-gate 	for (any = 0, pnpp = (Pnode **)&(dip->di_info), pnp = *pnpp; pnp;
1417*7c478bd9Sstevel@tonic-gate 	    pnpp = &pnp->p_next, pnp = * pnpp) {
1418*7c478bd9Sstevel@tonic-gate 		int	mode;
1419*7c478bd9Sstevel@tonic-gate 		Grp_hdl	*ghp;
1420*7c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
1421*7c478bd9Sstevel@tonic-gate 
1422*7c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
1423*7c478bd9Sstevel@tonic-gate 			continue;
1424*7c478bd9Sstevel@tonic-gate 
1425*7c478bd9Sstevel@tonic-gate 		/*
1426*7c478bd9Sstevel@tonic-gate 		 * Establish the mode of the filtee from the filter.  As filtees
1427*7c478bd9Sstevel@tonic-gate 		 * are loaded via a dlopen(), make sure that RTLD_GROUP is set
1428*7c478bd9Sstevel@tonic-gate 		 * and the filtees aren't global.  It would be nice to have
1429*7c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST used here also, but as filters got out long before
1430*7c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST was introduced it's a little too late now.
1431*7c478bd9Sstevel@tonic-gate 		 */
1432*7c478bd9Sstevel@tonic-gate 		mode = MODE(ilmp) | RTLD_GROUP;
1433*7c478bd9Sstevel@tonic-gate 		mode &= ~RTLD_GLOBAL;
1434*7c478bd9Sstevel@tonic-gate 
1435*7c478bd9Sstevel@tonic-gate 		/*
1436*7c478bd9Sstevel@tonic-gate 		 * Insure that any auxiliary filter can locate symbols from its
1437*7c478bd9Sstevel@tonic-gate 		 * caller.
1438*7c478bd9Sstevel@tonic-gate 		 */
1439*7c478bd9Sstevel@tonic-gate 		if (dip->di_flags & FLG_DI_AUXFLTR)
1440*7c478bd9Sstevel@tonic-gate 			mode |= RTLD_PARENT;
1441*7c478bd9Sstevel@tonic-gate 
1442*7c478bd9Sstevel@tonic-gate 		/*
1443*7c478bd9Sstevel@tonic-gate 		 * Process any hardware capability directory.  Establish a new
1444*7c478bd9Sstevel@tonic-gate 		 * link-map control list from which to analyze any newly added
1445*7c478bd9Sstevel@tonic-gate 		 * objects.  Note that an lmc may already be allocated from a
1446*7c478bd9Sstevel@tonic-gate 		 * previous filtee dlopen() that failed.
1447*7c478bd9Sstevel@tonic-gate 		 */
1448*7c478bd9Sstevel@tonic-gate 		if ((pnp->p_info == 0) && (pnp->p_orig & PN_TKN_HWCAP)) {
1449*7c478bd9Sstevel@tonic-gate 			if ((lmc == 0) &&
1450*7c478bd9Sstevel@tonic-gate 			    (FLAGS(lml->lm_head) & FLG_RT_RELOCED) &&
1451*7c478bd9Sstevel@tonic-gate 			    ((lmc = alist_append(&(lml->lm_lists), 0,
1452*7c478bd9Sstevel@tonic-gate 			    sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0))
1453*7c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
1454*7c478bd9Sstevel@tonic-gate 
1455*7c478bd9Sstevel@tonic-gate 			if (lmc)
1456*7c478bd9Sstevel@tonic-gate 				lmco = (Aliste)((char *)lmc -
1457*7c478bd9Sstevel@tonic-gate 				    (char *)lml->lm_lists);
1458*7c478bd9Sstevel@tonic-gate 			else
1459*7c478bd9Sstevel@tonic-gate 				lmco = ALO_DATA;
1460*7c478bd9Sstevel@tonic-gate 
1461*7c478bd9Sstevel@tonic-gate 			pnp = hwcap_filtees(pnpp, lmco, dip, ilmp, filtees,
1462*7c478bd9Sstevel@tonic-gate 			    mode, (FLG_RT_HANDLE | FLG_RT_HWCAP));
1463*7c478bd9Sstevel@tonic-gate 		}
1464*7c478bd9Sstevel@tonic-gate 
1465*7c478bd9Sstevel@tonic-gate 		if (pnp->p_len == 0)
1466*7c478bd9Sstevel@tonic-gate 			continue;
1467*7c478bd9Sstevel@tonic-gate 
1468*7c478bd9Sstevel@tonic-gate 		/*
1469*7c478bd9Sstevel@tonic-gate 		 * Process an individual filtee.
1470*7c478bd9Sstevel@tonic-gate 		 */
1471*7c478bd9Sstevel@tonic-gate 		if (pnp->p_info == 0) {
1472*7c478bd9Sstevel@tonic-gate 			const char	*filtee = pnp->p_name;
1473*7c478bd9Sstevel@tonic-gate 			int		audit = 0;
1474*7c478bd9Sstevel@tonic-gate 
1475*7c478bd9Sstevel@tonic-gate 			DBG_CALL(Dbg_file_filtee(NAME(ilmp), filtee, 0));
1476*7c478bd9Sstevel@tonic-gate 
1477*7c478bd9Sstevel@tonic-gate 			ghp = 0;
1478*7c478bd9Sstevel@tonic-gate 
1479*7c478bd9Sstevel@tonic-gate 			/*
1480*7c478bd9Sstevel@tonic-gate 			 * Determine if the reference link map is already
1481*7c478bd9Sstevel@tonic-gate 			 * loaded.  As an optimization compare the filtee with
1482*7c478bd9Sstevel@tonic-gate 			 * our interpretor.  The most common filter is
1483*7c478bd9Sstevel@tonic-gate 			 * libdl.so.1, which is a filter on ld.so.1.
1484*7c478bd9Sstevel@tonic-gate 			 */
1485*7c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
1486*7c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) {
1487*7c478bd9Sstevel@tonic-gate #else
1488*7c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) {
1489*7c478bd9Sstevel@tonic-gate #endif
1490*7c478bd9Sstevel@tonic-gate 				/*
1491*7c478bd9Sstevel@tonic-gate 				 * Create an association between ld.so.1 and
1492*7c478bd9Sstevel@tonic-gate 				 * the filter.
1493*7c478bd9Sstevel@tonic-gate 				 */
1494*7c478bd9Sstevel@tonic-gate 				nlmp = lml_rtld.lm_head;
1495*7c478bd9Sstevel@tonic-gate 				if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp,
1496*7c478bd9Sstevel@tonic-gate 				    (GPH_LDSO | GPH_FIRST | GPH_FILTEE))) == 0)
1497*7c478bd9Sstevel@tonic-gate 					nlmp = 0;
1498*7c478bd9Sstevel@tonic-gate 
1499*7c478bd9Sstevel@tonic-gate 				/*
1500*7c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
1501*7c478bd9Sstevel@tonic-gate 				 * recursion.
1502*7c478bd9Sstevel@tonic-gate 				 */
1503*7c478bd9Sstevel@tonic-gate 				if (nlmp && ghp)
1504*7c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
1505*7c478bd9Sstevel@tonic-gate 
1506*7c478bd9Sstevel@tonic-gate 				/*
1507*7c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  Ignore
1508*7c478bd9Sstevel@tonic-gate 				 * any return from the auditor, as we can't
1509*7c478bd9Sstevel@tonic-gate 				 * allow ignore filtering to ld.so.1, otherwise
1510*7c478bd9Sstevel@tonic-gate 				 * nothing is going to work.
1511*7c478bd9Sstevel@tonic-gate 				 */
1512*7c478bd9Sstevel@tonic-gate 				if ((lml->lm_tflags | FLAGS1(ilmp)) &
1513*7c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)
1514*7c478bd9Sstevel@tonic-gate 					(void) audit_objfilter(ilmp, filtees,
1515*7c478bd9Sstevel@tonic-gate 					    nlmp, 0);
1516*7c478bd9Sstevel@tonic-gate 
1517*7c478bd9Sstevel@tonic-gate 			} else {
1518*7c478bd9Sstevel@tonic-gate 				Rej_desc	rej = { 0 };
1519*7c478bd9Sstevel@tonic-gate 
1520*7c478bd9Sstevel@tonic-gate 				/*
1521*7c478bd9Sstevel@tonic-gate 				 * Establish a new link-map control list from
1522*7c478bd9Sstevel@tonic-gate 				 * which to analyze any newly added objects.
1523*7c478bd9Sstevel@tonic-gate 				 * Note that an lmc may already be allocated
1524*7c478bd9Sstevel@tonic-gate 				 * from a previous filtee dlopen() that failed.
1525*7c478bd9Sstevel@tonic-gate 				 */
1526*7c478bd9Sstevel@tonic-gate 				if ((lmc == 0) &&
1527*7c478bd9Sstevel@tonic-gate 				    (FLAGS(lml->lm_head) & FLG_RT_RELOCED) &&
1528*7c478bd9Sstevel@tonic-gate 				    ((lmc = alist_append(&(lml->lm_lists),
1529*7c478bd9Sstevel@tonic-gate 				    0, sizeof (Lm_cntl), AL_CNT_LMLISTS)) == 0))
1530*7c478bd9Sstevel@tonic-gate 					return ((Sym *)0);
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate 				if (lmc)
1533*7c478bd9Sstevel@tonic-gate 					lmco = (Aliste)((char *)lmc -
1534*7c478bd9Sstevel@tonic-gate 					    (char *)lml->lm_lists);
1535*7c478bd9Sstevel@tonic-gate 				else
1536*7c478bd9Sstevel@tonic-gate 					lmco = ALO_DATA;
1537*7c478bd9Sstevel@tonic-gate 
1538*7c478bd9Sstevel@tonic-gate 				/*
1539*7c478bd9Sstevel@tonic-gate 				 * Load the filtee.
1540*7c478bd9Sstevel@tonic-gate 				 */
1541*7c478bd9Sstevel@tonic-gate 				if ((nlmp = load_path(lml, lmco, filtee, ilmp,
1542*7c478bd9Sstevel@tonic-gate 				    mode, FLG_RT_HANDLE, &ghp, 0, &rej)) == 0) {
1543*7c478bd9Sstevel@tonic-gate 					file_notfound(LIST(ilmp), filtee, ilmp,
1544*7c478bd9Sstevel@tonic-gate 					    FLG_RT_HANDLE, &rej);
1545*7c478bd9Sstevel@tonic-gate 					remove_rej(&rej);
1546*7c478bd9Sstevel@tonic-gate 				}
1547*7c478bd9Sstevel@tonic-gate 
1548*7c478bd9Sstevel@tonic-gate 				/*
1549*7c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
1550*7c478bd9Sstevel@tonic-gate 				 * recursion.
1551*7c478bd9Sstevel@tonic-gate 				 */
1552*7c478bd9Sstevel@tonic-gate 				if (nlmp && ghp) {
1553*7c478bd9Sstevel@tonic-gate 					ghp->gh_flags |= GPH_FILTEE;
1554*7c478bd9Sstevel@tonic-gate 					pnp->p_info = (void *)ghp;
1555*7c478bd9Sstevel@tonic-gate 				}
1556*7c478bd9Sstevel@tonic-gate 
1557*7c478bd9Sstevel@tonic-gate 				/*
1558*7c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  A
1559*7c478bd9Sstevel@tonic-gate 				 * return of 0 indicates the auditor wishes to
1560*7c478bd9Sstevel@tonic-gate 				 * ignore this filtee.
1561*7c478bd9Sstevel@tonic-gate 				 */
1562*7c478bd9Sstevel@tonic-gate 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
1563*7c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)) {
1564*7c478bd9Sstevel@tonic-gate 					if (audit_objfilter(ilmp, filtees,
1565*7c478bd9Sstevel@tonic-gate 					    nlmp, 0) == 0) {
1566*7c478bd9Sstevel@tonic-gate 						audit = 1;
1567*7c478bd9Sstevel@tonic-gate 						nlmp = 0;
1568*7c478bd9Sstevel@tonic-gate 					}
1569*7c478bd9Sstevel@tonic-gate 				}
1570*7c478bd9Sstevel@tonic-gate 
1571*7c478bd9Sstevel@tonic-gate 				/*
1572*7c478bd9Sstevel@tonic-gate 				 * Finish processing the objects associated with
1573*7c478bd9Sstevel@tonic-gate 				 * this request.  Create an association between
1574*7c478bd9Sstevel@tonic-gate 				 * this object and the originating filter to
1575*7c478bd9Sstevel@tonic-gate 				 * provide sufficient information to tear down
1576*7c478bd9Sstevel@tonic-gate 				 * this filtee if necessary.
1577*7c478bd9Sstevel@tonic-gate 				 */
1578*7c478bd9Sstevel@tonic-gate 				if (nlmp && ghp &&
1579*7c478bd9Sstevel@tonic-gate 				    ((analyze_lmc(lml, lmco, nlmp) == 0) ||
1580*7c478bd9Sstevel@tonic-gate 				    (relocate_lmc(lml, lmco, nlmp) == 0)))
1581*7c478bd9Sstevel@tonic-gate 					nlmp = 0;
1582*7c478bd9Sstevel@tonic-gate 
1583*7c478bd9Sstevel@tonic-gate 				/*
1584*7c478bd9Sstevel@tonic-gate 				 * If the filtee has been successfully
1585*7c478bd9Sstevel@tonic-gate 				 * processed, and it is part of a link-map
1586*7c478bd9Sstevel@tonic-gate 				 * control list that is equivalent, or less,
1587*7c478bd9Sstevel@tonic-gate 				 * than the filter control list, create an
1588*7c478bd9Sstevel@tonic-gate 				 * association between the filter and filtee.
1589*7c478bd9Sstevel@tonic-gate 				 * This association provides sufficient
1590*7c478bd9Sstevel@tonic-gate 				 * information to tear down the filter and
1591*7c478bd9Sstevel@tonic-gate 				 * filtee if necessary.
1592*7c478bd9Sstevel@tonic-gate 				 */
1593*7c478bd9Sstevel@tonic-gate 				if (nlmp && ghp && (CNTL(nlmp) <= CNTL(ilmp)) &&
1594*7c478bd9Sstevel@tonic-gate 				    (hdl_add(ghp, ilmp, GPD_FILTER) == 0))
1595*7c478bd9Sstevel@tonic-gate 					nlmp = 0;
1596*7c478bd9Sstevel@tonic-gate 			}
1597*7c478bd9Sstevel@tonic-gate 
1598*7c478bd9Sstevel@tonic-gate 			/*
1599*7c478bd9Sstevel@tonic-gate 			 * Generate a diagnostic if the filtee couldn't be
1600*7c478bd9Sstevel@tonic-gate 			 * loaded, null out the pnode entry, and continue
1601*7c478bd9Sstevel@tonic-gate 			 * the search.  Otherwise, retain this group handle
1602*7c478bd9Sstevel@tonic-gate 			 * for future symbol searches.
1603*7c478bd9Sstevel@tonic-gate 			 */
1604*7c478bd9Sstevel@tonic-gate 			if (nlmp == 0) {
1605*7c478bd9Sstevel@tonic-gate 				pnp->p_info = 0;
1606*7c478bd9Sstevel@tonic-gate 				DBG_CALL(Dbg_file_filtee(0, filtee, audit));
1607*7c478bd9Sstevel@tonic-gate 
1608*7c478bd9Sstevel@tonic-gate 				if (ghp)
1609*7c478bd9Sstevel@tonic-gate 					(void) dlclose_core(ghp, ilmp);
1610*7c478bd9Sstevel@tonic-gate 
1611*7c478bd9Sstevel@tonic-gate 				if (lmc) {
1612*7c478bd9Sstevel@tonic-gate 					(void) lm_salvage(lml, 0, lmco);
1613*7c478bd9Sstevel@tonic-gate 					lmc->lc_head = lmc->lc_tail = 0;
1614*7c478bd9Sstevel@tonic-gate 				}
1615*7c478bd9Sstevel@tonic-gate 				pnp->p_len = 0;
1616*7c478bd9Sstevel@tonic-gate 				continue;
1617*7c478bd9Sstevel@tonic-gate 			}
1618*7c478bd9Sstevel@tonic-gate 		}
1619*7c478bd9Sstevel@tonic-gate 
1620*7c478bd9Sstevel@tonic-gate 		ghp = (Grp_hdl *)pnp->p_info;
1621*7c478bd9Sstevel@tonic-gate 
1622*7c478bd9Sstevel@tonic-gate 		/*
1623*7c478bd9Sstevel@tonic-gate 		 * If we're just here to trigger filtee loading skip the symbol
1624*7c478bd9Sstevel@tonic-gate 		 * lookup so we'll continue looking for additional filtees.
1625*7c478bd9Sstevel@tonic-gate 		 */
1626*7c478bd9Sstevel@tonic-gate 		if (name) {
1627*7c478bd9Sstevel@tonic-gate 			Grp_desc	*gdp;
1628*7c478bd9Sstevel@tonic-gate 			Sym		*sym = 0;
1629*7c478bd9Sstevel@tonic-gate 			Aliste		off;
1630*7c478bd9Sstevel@tonic-gate 			Slookup		sl = *slp;
1631*7c478bd9Sstevel@tonic-gate 
1632*7c478bd9Sstevel@tonic-gate 			sl.sl_flags |= LKUP_FIRST;
1633*7c478bd9Sstevel@tonic-gate 			any++;
1634*7c478bd9Sstevel@tonic-gate 
1635*7c478bd9Sstevel@tonic-gate 			/*
1636*7c478bd9Sstevel@tonic-gate 			 * Look for the symbol in the handles dependencies.
1637*7c478bd9Sstevel@tonic-gate 			 */
1638*7c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) {
1639*7c478bd9Sstevel@tonic-gate 				if ((gdp->gd_flags & GPD_AVAIL) == 0)
1640*7c478bd9Sstevel@tonic-gate 					continue;
1641*7c478bd9Sstevel@tonic-gate 
1642*7c478bd9Sstevel@tonic-gate 				/*
1643*7c478bd9Sstevel@tonic-gate 				 * If our parent is a dependency don't look at
1644*7c478bd9Sstevel@tonic-gate 				 * it (otherwise we are in a recursive loop).
1645*7c478bd9Sstevel@tonic-gate 				 * This situation can occur with auxiliary
1646*7c478bd9Sstevel@tonic-gate 				 * filters if the filtee has a dependency on the
1647*7c478bd9Sstevel@tonic-gate 				 * filter.  This dependency isn't necessary as
1648*7c478bd9Sstevel@tonic-gate 				 * auxiliary filters are opened RTLD_PARENT, but
1649*7c478bd9Sstevel@tonic-gate 				 * users may still unknowingly add an explicit
1650*7c478bd9Sstevel@tonic-gate 				 * dependency to the parent.
1651*7c478bd9Sstevel@tonic-gate 				 */
1652*7c478bd9Sstevel@tonic-gate 				if ((sl.sl_imap = gdp->gd_depend) == ilmp)
1653*7c478bd9Sstevel@tonic-gate 					continue;
1654*7c478bd9Sstevel@tonic-gate 
1655*7c478bd9Sstevel@tonic-gate 				if (((sym = SYMINTP(sl.sl_imap)(&sl, dlmp,
1656*7c478bd9Sstevel@tonic-gate 				    binfo)) != 0) ||
1657*7c478bd9Sstevel@tonic-gate 				    (ghp->gh_flags & GPH_FIRST))
1658*7c478bd9Sstevel@tonic-gate 					break;
1659*7c478bd9Sstevel@tonic-gate 			}
1660*7c478bd9Sstevel@tonic-gate 
1661*7c478bd9Sstevel@tonic-gate 			/*
1662*7c478bd9Sstevel@tonic-gate 			 * If this filtee has just been loaded (nlmp != 0),
1663*7c478bd9Sstevel@tonic-gate 			 * determine whether the filtee was triggered by a
1664*7c478bd9Sstevel@tonic-gate 			 * relocation from an object that is still being
1665*7c478bd9Sstevel@tonic-gate 			 * relocated on a leaf link-map control list.  As the
1666*7c478bd9Sstevel@tonic-gate 			 * relocation of an object on this list might still
1667*7c478bd9Sstevel@tonic-gate 			 * fail, we can't yet bind the filter to the filtee.
1668*7c478bd9Sstevel@tonic-gate 			 * To do so, would be locking the filtee so that it
1669*7c478bd9Sstevel@tonic-gate 			 * couldn't be deleted, and the filtee itself could have
1670*7c478bd9Sstevel@tonic-gate 			 * bound to an object that must be torn down.  Insure
1671*7c478bd9Sstevel@tonic-gate 			 * the caller isn't bound to the handle at this time.
1672*7c478bd9Sstevel@tonic-gate 			 * Any association will be reestablished when the filter
1673*7c478bd9Sstevel@tonic-gate 			 * is later referenced and the filtee has propagated to
1674*7c478bd9Sstevel@tonic-gate 			 * the same link-map control list.
1675*7c478bd9Sstevel@tonic-gate 			 */
1676*7c478bd9Sstevel@tonic-gate 			if (nlmp && (CNTL(nlmp) > CNTL(ilmp))) {
1677*7c478bd9Sstevel@tonic-gate 				remove_caller(ghp, ilmp);
1678*7c478bd9Sstevel@tonic-gate 				pnp->p_info = 0;
1679*7c478bd9Sstevel@tonic-gate 			}
1680*7c478bd9Sstevel@tonic-gate 			if (sym) {
1681*7c478bd9Sstevel@tonic-gate 				if (lmc)
1682*7c478bd9Sstevel@tonic-gate 					remove_cntl(lml, lmco);
1683*7c478bd9Sstevel@tonic-gate 
1684*7c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_FILTEE;
1685*7c478bd9Sstevel@tonic-gate 				return (sym);
1686*7c478bd9Sstevel@tonic-gate 			}
1687*7c478bd9Sstevel@tonic-gate 		}
1688*7c478bd9Sstevel@tonic-gate 
1689*7c478bd9Sstevel@tonic-gate 		/*
1690*7c478bd9Sstevel@tonic-gate 		 * If this object is tagged to terminate filtee processing we're
1691*7c478bd9Sstevel@tonic-gate 		 * done.
1692*7c478bd9Sstevel@tonic-gate 		 */
1693*7c478bd9Sstevel@tonic-gate 		if (FLAGS1(ghp->gh_owner) & FL1_RT_ENDFILTE)
1694*7c478bd9Sstevel@tonic-gate 			break;
1695*7c478bd9Sstevel@tonic-gate 	}
1696*7c478bd9Sstevel@tonic-gate 
1697*7c478bd9Sstevel@tonic-gate 	if (lmc)
1698*7c478bd9Sstevel@tonic-gate 		remove_cntl(lml, lmco);
1699*7c478bd9Sstevel@tonic-gate 
1700*7c478bd9Sstevel@tonic-gate 	/*
1701*7c478bd9Sstevel@tonic-gate 	 * If we're just here to trigger filtee loading then we're done.
1702*7c478bd9Sstevel@tonic-gate 	 */
1703*7c478bd9Sstevel@tonic-gate 	if (name == 0)
1704*7c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
1705*7c478bd9Sstevel@tonic-gate 
1706*7c478bd9Sstevel@tonic-gate 	/*
1707*7c478bd9Sstevel@tonic-gate 	 * If no filtees have been found for a filter, clean up any Pnode
1708*7c478bd9Sstevel@tonic-gate 	 * structures and disable their search completely.  For auxiliary
1709*7c478bd9Sstevel@tonic-gate 	 * filters we can reselect the symbol search function so that we never
1710*7c478bd9Sstevel@tonic-gate 	 * enter this routine again for this object.  For standard filters we
1711*7c478bd9Sstevel@tonic-gate 	 * use the null symbol routine.
1712*7c478bd9Sstevel@tonic-gate 	 */
1713*7c478bd9Sstevel@tonic-gate 	if (any == 0) {
1714*7c478bd9Sstevel@tonic-gate 		remove_pnode((Pnode *)dip->di_info);
1715*7c478bd9Sstevel@tonic-gate 		elf_disable_filtee(ilmp, dip);
1716*7c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
1717*7c478bd9Sstevel@tonic-gate 	}
1718*7c478bd9Sstevel@tonic-gate 
1719*7c478bd9Sstevel@tonic-gate 	return ((Sym *)0);
1720*7c478bd9Sstevel@tonic-gate }
1721*7c478bd9Sstevel@tonic-gate 
1722*7c478bd9Sstevel@tonic-gate /*
1723*7c478bd9Sstevel@tonic-gate  * Focal point for disabling error messages for auxiliary filters.  As an
1724*7c478bd9Sstevel@tonic-gate  * auxiliary filter allows for filtee use, but provides a fallback should a
1725*7c478bd9Sstevel@tonic-gate  * filtee not exist (or fail to load), any errors generated as a consequence of
1726*7c478bd9Sstevel@tonic-gate  * trying to load the filtees are typically suppressed.  Setting RT_FL_SILENCERR
1727*7c478bd9Sstevel@tonic-gate  * suppresses errors generated by eprint(), but insures a debug diagnostic is
1728*7c478bd9Sstevel@tonic-gate  * produced.  ldd(1) employs printf(), and here, the selection of whether to
1729*7c478bd9Sstevel@tonic-gate  * print a diagnostic in regards to auxiliary filters is a little more complex.
1730*7c478bd9Sstevel@tonic-gate  *
1731*7c478bd9Sstevel@tonic-gate  *   .	The determination of whether to produce an ldd message, or a fatal
1732*7c478bd9Sstevel@tonic-gate  *	error message is driven by LML_FLG_TRC_ENABLE.
1733*7c478bd9Sstevel@tonic-gate  *   .	More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN,
1734*7c478bd9Sstevel@tonic-gate  *	(ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s),
1735*7c478bd9Sstevel@tonic-gate  *	and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u).
1736*7c478bd9Sstevel@tonic-gate  *
1737*7c478bd9Sstevel@tonic-gate  *   .	If the calling object is lddstub, then several classes of message are
1738*7c478bd9Sstevel@tonic-gate  *	suppressed.  The user isn't trying to diagnose lddstub, this is simply
1739*7c478bd9Sstevel@tonic-gate  *	a stub executable employed to preload a user specified library against.
1740*7c478bd9Sstevel@tonic-gate  *
1741*7c478bd9Sstevel@tonic-gate  *   .	If RT_FL_SILENCERR is in effect then any generic ldd() messages should
1742*7c478bd9Sstevel@tonic-gate  *	be suppressed.  All detailed ldd messages should still be produced.
1743*7c478bd9Sstevel@tonic-gate  */
1744*7c478bd9Sstevel@tonic-gate Sym *
1745*7c478bd9Sstevel@tonic-gate elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx)
1746*7c478bd9Sstevel@tonic-gate {
1747*7c478bd9Sstevel@tonic-gate 	Sym	*sym;
1748*7c478bd9Sstevel@tonic-gate 	Dyninfo	*dip = &DYNINFO(slp->sl_imap)[ndx];
1749*7c478bd9Sstevel@tonic-gate 	int	silent = 0;
1750*7c478bd9Sstevel@tonic-gate 
1751*7c478bd9Sstevel@tonic-gate 	/*
1752*7c478bd9Sstevel@tonic-gate 	 * Make sure this entry is still acting as a filter.  We may have tried
1753*7c478bd9Sstevel@tonic-gate 	 * to process this previously, and disabled it if the filtee couldn't
1754*7c478bd9Sstevel@tonic-gate 	 * be processed.  However, other entries may provide different filtees
1755*7c478bd9Sstevel@tonic-gate 	 * that are yet to be completed.
1756*7c478bd9Sstevel@tonic-gate 	 */
1757*7c478bd9Sstevel@tonic-gate 	if (dip->di_flags == 0)
1758*7c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
1759*7c478bd9Sstevel@tonic-gate 
1760*7c478bd9Sstevel@tonic-gate 	/*
1761*7c478bd9Sstevel@tonic-gate 	 * Indicate whether an error message is required should this filtee not
1762*7c478bd9Sstevel@tonic-gate 	 * be found, based on the type of filter.
1763*7c478bd9Sstevel@tonic-gate 	 */
1764*7c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_AUXFLTR) &&
1765*7c478bd9Sstevel@tonic-gate 	    ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) {
1766*7c478bd9Sstevel@tonic-gate 		rtld_flags |= RT_FL_SILENCERR;
1767*7c478bd9Sstevel@tonic-gate 		silent = 1;
1768*7c478bd9Sstevel@tonic-gate 	}
1769*7c478bd9Sstevel@tonic-gate 
1770*7c478bd9Sstevel@tonic-gate 	sym = _elf_lookup_filtee(slp, dlmp, binfo, ndx);
1771*7c478bd9Sstevel@tonic-gate 
1772*7c478bd9Sstevel@tonic-gate 	if (silent)
1773*7c478bd9Sstevel@tonic-gate 		rtld_flags &= ~RT_FL_SILENCERR;
1774*7c478bd9Sstevel@tonic-gate 
1775*7c478bd9Sstevel@tonic-gate 	return (sym);
1776*7c478bd9Sstevel@tonic-gate }
1777*7c478bd9Sstevel@tonic-gate 
1778*7c478bd9Sstevel@tonic-gate /*
1779*7c478bd9Sstevel@tonic-gate  * Compute the elf hash value (as defined in the ELF access library).
1780*7c478bd9Sstevel@tonic-gate  * The form of the hash table is:
1781*7c478bd9Sstevel@tonic-gate  *
1782*7c478bd9Sstevel@tonic-gate  *	|--------------|
1783*7c478bd9Sstevel@tonic-gate  *	| # of buckets |
1784*7c478bd9Sstevel@tonic-gate  *	|--------------|
1785*7c478bd9Sstevel@tonic-gate  *	| # of chains  |
1786*7c478bd9Sstevel@tonic-gate  *	|--------------|
1787*7c478bd9Sstevel@tonic-gate  *	|   bucket[]   |
1788*7c478bd9Sstevel@tonic-gate  *	|--------------|
1789*7c478bd9Sstevel@tonic-gate  *	|   chain[]    |
1790*7c478bd9Sstevel@tonic-gate  *	|--------------|
1791*7c478bd9Sstevel@tonic-gate  */
1792*7c478bd9Sstevel@tonic-gate ulong_t
1793*7c478bd9Sstevel@tonic-gate elf_hash(const char *name)
1794*7c478bd9Sstevel@tonic-gate {
1795*7c478bd9Sstevel@tonic-gate 	uint_t	hval = 0;
1796*7c478bd9Sstevel@tonic-gate 
1797*7c478bd9Sstevel@tonic-gate 	while (*name) {
1798*7c478bd9Sstevel@tonic-gate 		uint_t	g;
1799*7c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *name++;
1800*7c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
1801*7c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
1802*7c478bd9Sstevel@tonic-gate 		hval &= ~g;
1803*7c478bd9Sstevel@tonic-gate 	}
1804*7c478bd9Sstevel@tonic-gate 	return ((ulong_t)hval);
1805*7c478bd9Sstevel@tonic-gate }
1806*7c478bd9Sstevel@tonic-gate 
1807*7c478bd9Sstevel@tonic-gate /*
1808*7c478bd9Sstevel@tonic-gate  * If flag argument has LKUP_SPEC set, we treat undefined symbols of type
1809*7c478bd9Sstevel@tonic-gate  * function specially in the executable - if they have a value, even though
1810*7c478bd9Sstevel@tonic-gate  * undefined, we use that value.  This allows us to associate all references
1811*7c478bd9Sstevel@tonic-gate  * to a function's address to a single place in the process: the plt entry
1812*7c478bd9Sstevel@tonic-gate  * for that function in the executable.  Calls to lookup from plt binding
1813*7c478bd9Sstevel@tonic-gate  * routines do NOT set LKUP_SPEC in the flag.
1814*7c478bd9Sstevel@tonic-gate  */
1815*7c478bd9Sstevel@tonic-gate Sym *
1816*7c478bd9Sstevel@tonic-gate elf_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo)
1817*7c478bd9Sstevel@tonic-gate {
1818*7c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
1819*7c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
1820*7c478bd9Sstevel@tonic-gate 	ulong_t		hash = slp->sl_hash;
1821*7c478bd9Sstevel@tonic-gate 	uint_t		ndx, htmp, buckets, *chainptr;
1822*7c478bd9Sstevel@tonic-gate 	Sym		*sym, *symtabptr;
1823*7c478bd9Sstevel@tonic-gate 	char		*strtabptr, *strtabname;
1824*7c478bd9Sstevel@tonic-gate 	uint_t		flags1;
1825*7c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
1826*7c478bd9Sstevel@tonic-gate 
1827*7c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_syms_lookup(name, NAME(ilmp), MSG_ORIG(MSG_STR_ELF)));
1828*7c478bd9Sstevel@tonic-gate 
1829*7c478bd9Sstevel@tonic-gate 	if (HASH(ilmp) == 0)
1830*7c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
1831*7c478bd9Sstevel@tonic-gate 
1832*7c478bd9Sstevel@tonic-gate 	buckets = HASH(ilmp)[0];
1833*7c478bd9Sstevel@tonic-gate 	/* LINTED */
1834*7c478bd9Sstevel@tonic-gate 	htmp = (uint_t)hash % buckets;
1835*7c478bd9Sstevel@tonic-gate 
1836*7c478bd9Sstevel@tonic-gate 	/*
1837*7c478bd9Sstevel@tonic-gate 	 * Get the first symbol on hash chain and initialize the string
1838*7c478bd9Sstevel@tonic-gate 	 * and symbol table pointers.
1839*7c478bd9Sstevel@tonic-gate 	 */
1840*7c478bd9Sstevel@tonic-gate 	if ((ndx = HASH(ilmp)[htmp + 2]) == 0)
1841*7c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
1842*7c478bd9Sstevel@tonic-gate 
1843*7c478bd9Sstevel@tonic-gate 	chainptr = HASH(ilmp) + 2 + buckets;
1844*7c478bd9Sstevel@tonic-gate 	strtabptr = STRTAB(ilmp);
1845*7c478bd9Sstevel@tonic-gate 	symtabptr = SYMTAB(ilmp);
1846*7c478bd9Sstevel@tonic-gate 
1847*7c478bd9Sstevel@tonic-gate 	while (ndx) {
1848*7c478bd9Sstevel@tonic-gate 		sym = symtabptr + ndx;
1849*7c478bd9Sstevel@tonic-gate 		strtabname = strtabptr + sym->st_name;
1850*7c478bd9Sstevel@tonic-gate 
1851*7c478bd9Sstevel@tonic-gate 		/*
1852*7c478bd9Sstevel@tonic-gate 		 * Compare the symbol found with the name required.  If the
1853*7c478bd9Sstevel@tonic-gate 		 * names don't match continue with the next hash entry.
1854*7c478bd9Sstevel@tonic-gate 		 */
1855*7c478bd9Sstevel@tonic-gate 		if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
1856*7c478bd9Sstevel@tonic-gate 			if ((ndx = chainptr[ndx]) != 0)
1857*7c478bd9Sstevel@tonic-gate 				continue;
1858*7c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
1859*7c478bd9Sstevel@tonic-gate 		}
1860*7c478bd9Sstevel@tonic-gate 
1861*7c478bd9Sstevel@tonic-gate 		/*
1862*7c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is defined, return the
1863*7c478bd9Sstevel@tonic-gate 		 * symbol pointer and the link map in which it was found.
1864*7c478bd9Sstevel@tonic-gate 		 */
1865*7c478bd9Sstevel@tonic-gate 		if (sym->st_shndx != SHN_UNDEF) {
1866*7c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
1867*7c478bd9Sstevel@tonic-gate 			*binfo |= DBG_BINFO_FOUND;
1868*7c478bd9Sstevel@tonic-gate 			if (FLAGS(ilmp) & FLG_RT_INTRPOSE)
1869*7c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
1870*7c478bd9Sstevel@tonic-gate 			if (slp->sl_flags & LKUP_SELF)
1871*7c478bd9Sstevel@tonic-gate 				return (sym);
1872*7c478bd9Sstevel@tonic-gate 			break;
1873*7c478bd9Sstevel@tonic-gate 
1874*7c478bd9Sstevel@tonic-gate 		/*
1875*7c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is undefined, the
1876*7c478bd9Sstevel@tonic-gate 		 * symbol type is a function, and the value of the symbol
1877*7c478bd9Sstevel@tonic-gate 		 * is non zero, then this is a special case.  This allows
1878*7c478bd9Sstevel@tonic-gate 		 * the resolution of a function address to the plt[] entry.
1879*7c478bd9Sstevel@tonic-gate 		 * See SPARC ABI, Dynamic Linking, Function Addresses for
1880*7c478bd9Sstevel@tonic-gate 		 * more details.
1881*7c478bd9Sstevel@tonic-gate 		 */
1882*7c478bd9Sstevel@tonic-gate 		} else if ((slp->sl_flags & (LKUP_SPEC | LKUP_SELF)) &&
1883*7c478bd9Sstevel@tonic-gate 		    (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) &&
1884*7c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
1885*7c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
1886*7c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR);
1887*7c478bd9Sstevel@tonic-gate 			if (FLAGS(ilmp) & FLG_RT_INTRPOSE)
1888*7c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
1889*7c478bd9Sstevel@tonic-gate 			return (sym);
1890*7c478bd9Sstevel@tonic-gate 		}
1891*7c478bd9Sstevel@tonic-gate 
1892*7c478bd9Sstevel@tonic-gate 		/*
1893*7c478bd9Sstevel@tonic-gate 		 * Undefined symbol.
1894*7c478bd9Sstevel@tonic-gate 		 */
1895*7c478bd9Sstevel@tonic-gate 		if (slp->sl_flags & LKUP_SELF)
1896*7c478bd9Sstevel@tonic-gate 			return (sym);
1897*7c478bd9Sstevel@tonic-gate 
1898*7c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
1899*7c478bd9Sstevel@tonic-gate 	}
1900*7c478bd9Sstevel@tonic-gate 
1901*7c478bd9Sstevel@tonic-gate 	/*
1902*7c478bd9Sstevel@tonic-gate 	 * We've found a match.  Determine if the defining object contains
1903*7c478bd9Sstevel@tonic-gate 	 * symbol binding information.
1904*7c478bd9Sstevel@tonic-gate 	 */
1905*7c478bd9Sstevel@tonic-gate 	if ((sip = SYMINFO(ilmp)) != 0)
1906*7c478bd9Sstevel@tonic-gate 		/* LINTED */
1907*7c478bd9Sstevel@tonic-gate 		sip = (Syminfo *)((char *)sip + (ndx * SYMINENT(ilmp)));
1908*7c478bd9Sstevel@tonic-gate 
1909*7c478bd9Sstevel@tonic-gate 	/*
1910*7c478bd9Sstevel@tonic-gate 	 * If this is a direct binding request, but the symbol definition has
1911*7c478bd9Sstevel@tonic-gate 	 * disabled directly binding to it (presumably because the symbol
1912*7c478bd9Sstevel@tonic-gate 	 * definition has been changed since the referring object was built),
1913*7c478bd9Sstevel@tonic-gate 	 * indicate this failure so that the caller can fall back to a standard
1914*7c478bd9Sstevel@tonic-gate 	 * symbol search.  Clear any debug binding information for cleanliness.
1915*7c478bd9Sstevel@tonic-gate 	 */
1916*7c478bd9Sstevel@tonic-gate 	if (sip && (slp->sl_flags & LKUP_DIRECT) &&
1917*7c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) {
1918*7c478bd9Sstevel@tonic-gate 		*binfo |= BINFO_DIRECTDIS;
1919*7c478bd9Sstevel@tonic-gate 		*binfo &= ~DBG_BINFO_MSK;
1920*7c478bd9Sstevel@tonic-gate 		return ((Sym *)0);
1921*7c478bd9Sstevel@tonic-gate 	}
1922*7c478bd9Sstevel@tonic-gate 
1923*7c478bd9Sstevel@tonic-gate 	/*
1924*7c478bd9Sstevel@tonic-gate 	 * Determine whether this object is acting as a filter.
1925*7c478bd9Sstevel@tonic-gate 	 */
1926*7c478bd9Sstevel@tonic-gate 	if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0)
1927*7c478bd9Sstevel@tonic-gate 		return (sym);
1928*7c478bd9Sstevel@tonic-gate 
1929*7c478bd9Sstevel@tonic-gate 	/*
1930*7c478bd9Sstevel@tonic-gate 	 * Determine if this object offers per-symbol filtering, and if so,
1931*7c478bd9Sstevel@tonic-gate 	 * whether this symbol references a filtee.
1932*7c478bd9Sstevel@tonic-gate 	 */
1933*7c478bd9Sstevel@tonic-gate 	if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) {
1934*7c478bd9Sstevel@tonic-gate 		/*
1935*7c478bd9Sstevel@tonic-gate 		 * If this is a standard filter reference, and no standard
1936*7c478bd9Sstevel@tonic-gate 		 * filtees remain to be inspected, we're done.  If this is an
1937*7c478bd9Sstevel@tonic-gate 		 * auxiliary filter reference, and no auxiliary filtees remain,
1938*7c478bd9Sstevel@tonic-gate 		 * we'll fall through in case any object filtering is available.
1939*7c478bd9Sstevel@tonic-gate 		 */
1940*7c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) &&
1941*7c478bd9Sstevel@tonic-gate 		    (SYMSFLTRCNT(ilmp) == 0))
1942*7c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
1943*7c478bd9Sstevel@tonic-gate 
1944*7c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) ||
1945*7c478bd9Sstevel@tonic-gate 		    ((sip->si_flags & SYMINFO_FLG_AUXILIARY) &&
1946*7c478bd9Sstevel@tonic-gate 		    SYMAFLTRCNT(ilmp))) {
1947*7c478bd9Sstevel@tonic-gate 			Sym *	fsym;
1948*7c478bd9Sstevel@tonic-gate 
1949*7c478bd9Sstevel@tonic-gate 			/*
1950*7c478bd9Sstevel@tonic-gate 			 * This symbol has an associated filtee.  Lookup the
1951*7c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
1952*7c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
1953*7c478bd9Sstevel@tonic-gate 			 * filter, return an error, otherwise fall through to
1954*7c478bd9Sstevel@tonic-gate 			 * catch any object filtering that may be available.
1955*7c478bd9Sstevel@tonic-gate 			 */
1956*7c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
1957*7c478bd9Sstevel@tonic-gate 			    sip->si_boundto)) != 0)
1958*7c478bd9Sstevel@tonic-gate 				return (fsym);
1959*7c478bd9Sstevel@tonic-gate 			if (sip->si_flags & SYMINFO_FLG_FILTER)
1960*7c478bd9Sstevel@tonic-gate 				return ((Sym *)0);
1961*7c478bd9Sstevel@tonic-gate 		}
1962*7c478bd9Sstevel@tonic-gate 	}
1963*7c478bd9Sstevel@tonic-gate 
1964*7c478bd9Sstevel@tonic-gate 	/*
1965*7c478bd9Sstevel@tonic-gate 	 * Determine if this object provides global filtering.
1966*7c478bd9Sstevel@tonic-gate 	 */
1967*7c478bd9Sstevel@tonic-gate 	if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) {
1968*7c478bd9Sstevel@tonic-gate 		Sym *	fsym;
1969*7c478bd9Sstevel@tonic-gate 
1970*7c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) {
1971*7c478bd9Sstevel@tonic-gate 			/*
1972*7c478bd9Sstevel@tonic-gate 			 * This object has an associated filtee.  Lookup the
1973*7c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
1974*7c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
1975*7c478bd9Sstevel@tonic-gate 			 * filter, return and error, otherwise return the symbol
1976*7c478bd9Sstevel@tonic-gate 			 * within the filter itself.
1977*7c478bd9Sstevel@tonic-gate 			 */
1978*7c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
1979*7c478bd9Sstevel@tonic-gate 			    OBJFLTRNDX(ilmp))) != 0)
1980*7c478bd9Sstevel@tonic-gate 				return (fsym);
1981*7c478bd9Sstevel@tonic-gate 		}
1982*7c478bd9Sstevel@tonic-gate 
1983*7c478bd9Sstevel@tonic-gate 		if (flags1 & FL1_RT_OBJSFLTR)
1984*7c478bd9Sstevel@tonic-gate 			return ((Sym *)0);
1985*7c478bd9Sstevel@tonic-gate 	}
1986*7c478bd9Sstevel@tonic-gate 	return (sym);
1987*7c478bd9Sstevel@tonic-gate }
1988*7c478bd9Sstevel@tonic-gate 
1989*7c478bd9Sstevel@tonic-gate /*
1990*7c478bd9Sstevel@tonic-gate  * Create a new Rt_map structure for an ELF object and initialize
1991*7c478bd9Sstevel@tonic-gate  * all values.
1992*7c478bd9Sstevel@tonic-gate  */
1993*7c478bd9Sstevel@tonic-gate Rt_map *
1994*7c478bd9Sstevel@tonic-gate elf_new_lm(Lm_list *lml, const char *pname, const char *oname, Dyn *ld,
1995*7c478bd9Sstevel@tonic-gate     ulong_t addr, ulong_t etext, Aliste lmco, ulong_t msize, ulong_t entry,
1996*7c478bd9Sstevel@tonic-gate     ulong_t paddr, ulong_t padimsize, Mmap *mmaps, uint_t mmapcnt)
1997*7c478bd9Sstevel@tonic-gate {
1998*7c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
1999*7c478bd9Sstevel@tonic-gate 	ulong_t		base, fltr = 0, audit = 0, cfile = 0, crle = 0;
2000*7c478bd9Sstevel@tonic-gate 	Xword		rpath = 0;
2001*7c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr = (Ehdr *)addr;
2002*7c478bd9Sstevel@tonic-gate 
2003*7c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_file_elf(pname, (ulong_t)ld, addr, msize, entry,
2004*7c478bd9Sstevel@tonic-gate 	    get_linkmap_id(lml), lmco));
2005*7c478bd9Sstevel@tonic-gate 
2006*7c478bd9Sstevel@tonic-gate 	/*
2007*7c478bd9Sstevel@tonic-gate 	 * Allocate space for the link-map and private elf information.  Once
2008*7c478bd9Sstevel@tonic-gate 	 * these are allocated and initialized, we can use remove_so(0, lmp) to
2009*7c478bd9Sstevel@tonic-gate 	 * tear down the link-map should any failures occur.
2010*7c478bd9Sstevel@tonic-gate 	 */
2011*7c478bd9Sstevel@tonic-gate 	if ((lmp = calloc(sizeof (Rt_map), 1)) == 0)
2012*7c478bd9Sstevel@tonic-gate 		return (0);
2013*7c478bd9Sstevel@tonic-gate 	if ((ELFPRV(lmp) = calloc(sizeof (Rt_elfp), 1)) == 0) {
2014*7c478bd9Sstevel@tonic-gate 		free(lmp);
2015*7c478bd9Sstevel@tonic-gate 		return (0);
2016*7c478bd9Sstevel@tonic-gate 	}
2017*7c478bd9Sstevel@tonic-gate 
2018*7c478bd9Sstevel@tonic-gate 	/*
2019*7c478bd9Sstevel@tonic-gate 	 * All fields not filled in were set to 0 by calloc.
2020*7c478bd9Sstevel@tonic-gate 	 */
2021*7c478bd9Sstevel@tonic-gate 	ORIGNAME(lmp) = PATHNAME(lmp) = NAME(lmp) = (char *)pname;
2022*7c478bd9Sstevel@tonic-gate 	DYN(lmp) = ld;
2023*7c478bd9Sstevel@tonic-gate 	ADDR(lmp) = addr;
2024*7c478bd9Sstevel@tonic-gate 	MSIZE(lmp) = msize;
2025*7c478bd9Sstevel@tonic-gate 	ENTRY(lmp) = (Addr)entry;
2026*7c478bd9Sstevel@tonic-gate 	SYMINTP(lmp) = elf_find_sym;
2027*7c478bd9Sstevel@tonic-gate 	ETEXT(lmp) = etext;
2028*7c478bd9Sstevel@tonic-gate 	FCT(lmp) = &elf_fct;
2029*7c478bd9Sstevel@tonic-gate 	LIST(lmp) = lml;
2030*7c478bd9Sstevel@tonic-gate 	PADSTART(lmp) = paddr;
2031*7c478bd9Sstevel@tonic-gate 	PADIMLEN(lmp) = padimsize;
2032*7c478bd9Sstevel@tonic-gate 	THREADID(lmp) = rt_thr_self();
2033*7c478bd9Sstevel@tonic-gate 	OBJFLTRNDX(lmp) = FLTR_DISABLED;
2034*7c478bd9Sstevel@tonic-gate 
2035*7c478bd9Sstevel@tonic-gate 	MMAPS(lmp) = mmaps;
2036*7c478bd9Sstevel@tonic-gate 	MMAPCNT(lmp) = mmapcnt;
2037*7c478bd9Sstevel@tonic-gate 	ASSERT(mmapcnt != 0);
2038*7c478bd9Sstevel@tonic-gate 
2039*7c478bd9Sstevel@tonic-gate 	/*
2040*7c478bd9Sstevel@tonic-gate 	 * If this is a shared object, add the base address to each address.
2041*7c478bd9Sstevel@tonic-gate 	 * if this is an executable, use address as is.
2042*7c478bd9Sstevel@tonic-gate 	 */
2043*7c478bd9Sstevel@tonic-gate 	if (ehdr->e_type == ET_EXEC) {
2044*7c478bd9Sstevel@tonic-gate 		base = 0;
2045*7c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_FIXED;
2046*7c478bd9Sstevel@tonic-gate 	} else
2047*7c478bd9Sstevel@tonic-gate 		base = addr;
2048*7c478bd9Sstevel@tonic-gate 
2049*7c478bd9Sstevel@tonic-gate 	/*
2050*7c478bd9Sstevel@tonic-gate 	 * Fill in rest of the link map entries with information from the file's
2051*7c478bd9Sstevel@tonic-gate 	 * dynamic structure.
2052*7c478bd9Sstevel@tonic-gate 	 */
2053*7c478bd9Sstevel@tonic-gate 	if (ld) {
2054*7c478bd9Sstevel@tonic-gate 		uint_t	dyncnt = 0;
2055*7c478bd9Sstevel@tonic-gate 		Xword	pltpadsz = 0;
2056*7c478bd9Sstevel@tonic-gate 		void	*rtldinfo;
2057*7c478bd9Sstevel@tonic-gate 
2058*7c478bd9Sstevel@tonic-gate 		/* CSTYLED */
2059*7c478bd9Sstevel@tonic-gate 		for ( ; ld->d_tag != DT_NULL; ++ld, dyncnt++) {
2060*7c478bd9Sstevel@tonic-gate 			switch ((Xword)ld->d_tag) {
2061*7c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
2062*7c478bd9Sstevel@tonic-gate 				SYMTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
2063*7c478bd9Sstevel@tonic-gate 				break;
2064*7c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
2065*7c478bd9Sstevel@tonic-gate 				STRTAB(lmp) = (void *)(ld->d_un.d_ptr + base);
2066*7c478bd9Sstevel@tonic-gate 				break;
2067*7c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
2068*7c478bd9Sstevel@tonic-gate 				SYMENT(lmp) = ld->d_un.d_val;
2069*7c478bd9Sstevel@tonic-gate 				break;
2070*7c478bd9Sstevel@tonic-gate 			case DT_FEATURE_1:
2071*7c478bd9Sstevel@tonic-gate 				ld->d_un.d_val |= DTF_1_PARINIT;
2072*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DTF_1_CONFEXP)
2073*7c478bd9Sstevel@tonic-gate 					crle = 1;
2074*7c478bd9Sstevel@tonic-gate 				break;
2075*7c478bd9Sstevel@tonic-gate 			case DT_MOVESZ:
2076*7c478bd9Sstevel@tonic-gate 				MOVESZ(lmp) = ld->d_un.d_val;
2077*7c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_MOVE;
2078*7c478bd9Sstevel@tonic-gate 				break;
2079*7c478bd9Sstevel@tonic-gate 			case DT_MOVEENT:
2080*7c478bd9Sstevel@tonic-gate 				MOVEENT(lmp) = ld->d_un.d_val;
2081*7c478bd9Sstevel@tonic-gate 				break;
2082*7c478bd9Sstevel@tonic-gate 			case DT_MOVETAB:
2083*7c478bd9Sstevel@tonic-gate 				MOVETAB(lmp) = (void *)(ld->d_un.d_ptr + base);
2084*7c478bd9Sstevel@tonic-gate 				break;
2085*7c478bd9Sstevel@tonic-gate 			case DT_REL:
2086*7c478bd9Sstevel@tonic-gate 			case DT_RELA:
2087*7c478bd9Sstevel@tonic-gate 				/*
2088*7c478bd9Sstevel@tonic-gate 				 * At this time we can only handle 1 type of
2089*7c478bd9Sstevel@tonic-gate 				 * relocation per object.
2090*7c478bd9Sstevel@tonic-gate 				 */
2091*7c478bd9Sstevel@tonic-gate 				REL(lmp) = (void *)(ld->d_un.d_ptr + base);
2092*7c478bd9Sstevel@tonic-gate 				break;
2093*7c478bd9Sstevel@tonic-gate 			case DT_RELSZ:
2094*7c478bd9Sstevel@tonic-gate 			case DT_RELASZ:
2095*7c478bd9Sstevel@tonic-gate 				RELSZ(lmp) = ld->d_un.d_val;
2096*7c478bd9Sstevel@tonic-gate 				break;
2097*7c478bd9Sstevel@tonic-gate 			case DT_RELENT:
2098*7c478bd9Sstevel@tonic-gate 			case DT_RELAENT:
2099*7c478bd9Sstevel@tonic-gate 				RELENT(lmp) = ld->d_un.d_val;
2100*7c478bd9Sstevel@tonic-gate 				break;
2101*7c478bd9Sstevel@tonic-gate 			case DT_RELCOUNT:
2102*7c478bd9Sstevel@tonic-gate 			case DT_RELACOUNT:
2103*7c478bd9Sstevel@tonic-gate 				RELACOUNT(lmp) = (uint_t)ld->d_un.d_val;
2104*7c478bd9Sstevel@tonic-gate 				break;
2105*7c478bd9Sstevel@tonic-gate 			case DT_TEXTREL:
2106*7c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_TEXTREL;
2107*7c478bd9Sstevel@tonic-gate 				break;
2108*7c478bd9Sstevel@tonic-gate 			case DT_HASH:
2109*7c478bd9Sstevel@tonic-gate 				HASH(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
2110*7c478bd9Sstevel@tonic-gate 				break;
2111*7c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
2112*7c478bd9Sstevel@tonic-gate 				PLTGOT(lmp) = (uint_t *)(ld->d_un.d_ptr + base);
2113*7c478bd9Sstevel@tonic-gate 				break;
2114*7c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
2115*7c478bd9Sstevel@tonic-gate 				PLTRELSZ(lmp) = ld->d_un.d_val;
2116*7c478bd9Sstevel@tonic-gate 				break;
2117*7c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
2118*7c478bd9Sstevel@tonic-gate 				JMPREL(lmp) = (void *)(ld->d_un.d_ptr + base);
2119*7c478bd9Sstevel@tonic-gate 				break;
2120*7c478bd9Sstevel@tonic-gate 			case DT_INIT:
2121*7c478bd9Sstevel@tonic-gate 				INIT(lmp) = (void (*)())(ld->d_un.d_ptr + base);
2122*7c478bd9Sstevel@tonic-gate 				break;
2123*7c478bd9Sstevel@tonic-gate 			case DT_FINI:
2124*7c478bd9Sstevel@tonic-gate 				FINI(lmp) = (void (*)())(ld->d_un.d_ptr + base);
2125*7c478bd9Sstevel@tonic-gate 				break;
2126*7c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAY:
2127*7c478bd9Sstevel@tonic-gate 				INITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
2128*7c478bd9Sstevel@tonic-gate 				    base);
2129*7c478bd9Sstevel@tonic-gate 				break;
2130*7c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAYSZ:
2131*7c478bd9Sstevel@tonic-gate 				INITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
2132*7c478bd9Sstevel@tonic-gate 				break;
2133*7c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAY:
2134*7c478bd9Sstevel@tonic-gate 				FINIARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
2135*7c478bd9Sstevel@tonic-gate 				    base);
2136*7c478bd9Sstevel@tonic-gate 				break;
2137*7c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAYSZ:
2138*7c478bd9Sstevel@tonic-gate 				FINIARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
2139*7c478bd9Sstevel@tonic-gate 				break;
2140*7c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAY:
2141*7c478bd9Sstevel@tonic-gate 				PREINITARRAY(lmp) = (Addr *)(ld->d_un.d_ptr +
2142*7c478bd9Sstevel@tonic-gate 				    base);
2143*7c478bd9Sstevel@tonic-gate 				break;
2144*7c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAYSZ:
2145*7c478bd9Sstevel@tonic-gate 				PREINITARRAYSZ(lmp) = (uint_t)ld->d_un.d_val;
2146*7c478bd9Sstevel@tonic-gate 				break;
2147*7c478bd9Sstevel@tonic-gate 			case DT_RPATH:
2148*7c478bd9Sstevel@tonic-gate 			case DT_RUNPATH:
2149*7c478bd9Sstevel@tonic-gate 				rpath = ld->d_un.d_val;
2150*7c478bd9Sstevel@tonic-gate 				break;
2151*7c478bd9Sstevel@tonic-gate 			case DT_FILTER:
2152*7c478bd9Sstevel@tonic-gate 				fltr = ld->d_un.d_val;
2153*7c478bd9Sstevel@tonic-gate 				OBJFLTRNDX(lmp) = dyncnt;
2154*7c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJSFLTR;
2155*7c478bd9Sstevel@tonic-gate 				break;
2156*7c478bd9Sstevel@tonic-gate 			case DT_AUXILIARY:
2157*7c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
2158*7c478bd9Sstevel@tonic-gate 					fltr = ld->d_un.d_val;
2159*7c478bd9Sstevel@tonic-gate 					OBJFLTRNDX(lmp) = dyncnt;
2160*7c478bd9Sstevel@tonic-gate 				}
2161*7c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJAFLTR;
2162*7c478bd9Sstevel@tonic-gate 				break;
2163*7c478bd9Sstevel@tonic-gate 			case DT_SUNW_FILTER:
2164*7c478bd9Sstevel@tonic-gate 				SYMSFLTRCNT(lmp)++;
2165*7c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMSFLTR;
2166*7c478bd9Sstevel@tonic-gate 				break;
2167*7c478bd9Sstevel@tonic-gate 			case DT_SUNW_AUXILIARY:
2168*7c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
2169*7c478bd9Sstevel@tonic-gate 					SYMAFLTRCNT(lmp)++;
2170*7c478bd9Sstevel@tonic-gate 				}
2171*7c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMAFLTR;
2172*7c478bd9Sstevel@tonic-gate 				break;
2173*7c478bd9Sstevel@tonic-gate 			case DT_DEPAUDIT:
2174*7c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUDIT))
2175*7c478bd9Sstevel@tonic-gate 					audit = ld->d_un.d_val;
2176*7c478bd9Sstevel@tonic-gate 				break;
2177*7c478bd9Sstevel@tonic-gate 			case DT_CONFIG:
2178*7c478bd9Sstevel@tonic-gate 				cfile = ld->d_un.d_val;
2179*7c478bd9Sstevel@tonic-gate 				break;
2180*7c478bd9Sstevel@tonic-gate 			case DT_DEBUG:
2181*7c478bd9Sstevel@tonic-gate 				/*
2182*7c478bd9Sstevel@tonic-gate 				 * DT_DEBUG entries are only created in
2183*7c478bd9Sstevel@tonic-gate 				 * dynamic objects that require an interpretor
2184*7c478bd9Sstevel@tonic-gate 				 * (ie. all dynamic executables and some shared
2185*7c478bd9Sstevel@tonic-gate 				 * objects), and provide for a hand-shake with
2186*7c478bd9Sstevel@tonic-gate 				 * debuggers.  This entry is initialized to
2187*7c478bd9Sstevel@tonic-gate 				 * zero by the link-editor.  If a debugger has
2188*7c478bd9Sstevel@tonic-gate 				 * us and updated this entry set the debugger
2189*7c478bd9Sstevel@tonic-gate 				 * flag, and finish initializing the debugging
2190*7c478bd9Sstevel@tonic-gate 				 * structure (see setup() also).  Switch off any
2191*7c478bd9Sstevel@tonic-gate 				 * configuration object use as most debuggers
2192*7c478bd9Sstevel@tonic-gate 				 * can't handle fixed dynamic executables as
2193*7c478bd9Sstevel@tonic-gate 				 * dependencies, and we can't handle requests
2194*7c478bd9Sstevel@tonic-gate 				 * like object padding for alternative objects.
2195*7c478bd9Sstevel@tonic-gate 				 */
2196*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_ptr)
2197*7c478bd9Sstevel@tonic-gate 					rtld_flags |=
2198*7c478bd9Sstevel@tonic-gate 					    (RT_FL_DEBUGGER | RT_FL_NOOBJALT);
2199*7c478bd9Sstevel@tonic-gate 				ld->d_un.d_ptr = (Addr)&r_debug;
2200*7c478bd9Sstevel@tonic-gate 				break;
2201*7c478bd9Sstevel@tonic-gate 			case DT_VERNEED:
2202*7c478bd9Sstevel@tonic-gate 				VERNEED(lmp) = (Verneed *)(ld->d_un.d_ptr +
2203*7c478bd9Sstevel@tonic-gate 				    base);
2204*7c478bd9Sstevel@tonic-gate 				break;
2205*7c478bd9Sstevel@tonic-gate 			case DT_VERNEEDNUM:
2206*7c478bd9Sstevel@tonic-gate 				/* LINTED */
2207*7c478bd9Sstevel@tonic-gate 				VERNEEDNUM(lmp) = (int)ld->d_un.d_val;
2208*7c478bd9Sstevel@tonic-gate 				break;
2209*7c478bd9Sstevel@tonic-gate 			case DT_VERDEF:
2210*7c478bd9Sstevel@tonic-gate 				VERDEF(lmp) = (Verdef *)(ld->d_un.d_ptr + base);
2211*7c478bd9Sstevel@tonic-gate 				break;
2212*7c478bd9Sstevel@tonic-gate 			case DT_VERDEFNUM:
2213*7c478bd9Sstevel@tonic-gate 				/* LINTED */
2214*7c478bd9Sstevel@tonic-gate 				VERDEFNUM(lmp) = (int)ld->d_un.d_val;
2215*7c478bd9Sstevel@tonic-gate 				break;
2216*7c478bd9Sstevel@tonic-gate 			case DT_BIND_NOW:
2217*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_BIND_NOW) {
2218*7c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
2219*7c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
2220*7c478bd9Sstevel@tonic-gate 				}
2221*7c478bd9Sstevel@tonic-gate 				break;
2222*7c478bd9Sstevel@tonic-gate 			case DT_FLAGS:
2223*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_SYMBOLIC)
2224*7c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_SYMBOLIC;
2225*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_TEXTREL)
2226*7c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_TEXTREL;
2227*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_BIND_NOW) {
2228*7c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
2229*7c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
2230*7c478bd9Sstevel@tonic-gate 				}
2231*7c478bd9Sstevel@tonic-gate 				break;
2232*7c478bd9Sstevel@tonic-gate 			case DT_FLAGS_1:
2233*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DISPRELPND)
2234*7c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_DISPREL;
2235*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_GROUP)
2236*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |=
2237*7c478bd9Sstevel@tonic-gate 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
2238*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NOW) {
2239*7c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
2240*7c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
2241*7c478bd9Sstevel@tonic-gate 				}
2242*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODELETE)
2243*7c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NODELETE;
2244*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_INITFIRST)
2245*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INITFRST;
2246*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NOOPEN)
2247*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NOOPEN;
2248*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_LOADFLTR)
2249*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_LOADFLTR;
2250*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODUMP)
2251*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NODUMP;
2252*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_CONFALT)
2253*7c478bd9Sstevel@tonic-gate 					crle = 1;
2254*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_DIRECT)
2255*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_DIRECT;
2256*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_NODEFLIB)
2257*7c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_NODEFLIB;
2258*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ENDFILTEE)
2259*7c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_ENDFILTE;
2260*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_TRANS)
2261*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_TRANS;
2262*7c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
2263*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_ORIGIN)
2264*7c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_RELATIVE;
2265*7c478bd9Sstevel@tonic-gate #endif
2266*7c478bd9Sstevel@tonic-gate 				/*
2267*7c478bd9Sstevel@tonic-gate 				 * If this object identifies itself as an
2268*7c478bd9Sstevel@tonic-gate 				 * interposer, but relocation processing has
2269*7c478bd9Sstevel@tonic-gate 				 * already started, then demote it.  It's too
2270*7c478bd9Sstevel@tonic-gate 				 * late to guarantee complete interposition.
2271*7c478bd9Sstevel@tonic-gate 				 */
2272*7c478bd9Sstevel@tonic-gate 				if (ld->d_un.d_val & DF_1_INTERPOSE) {
2273*7c478bd9Sstevel@tonic-gate 				    if ((lml->lm_flags & LML_FLG_STARTREL) == 0)
2274*7c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INTRPOSE;
2275*7c478bd9Sstevel@tonic-gate 				    else {
2276*7c478bd9Sstevel@tonic-gate 					DBG_CALL(Dbg_util_intoolate(NAME(lmp)));
2277*7c478bd9Sstevel@tonic-gate 					if (lml->lm_flags & LML_FLG_TRC_ENABLE)
2278*7c478bd9Sstevel@tonic-gate 					    (void) printf(
2279*7c478bd9Sstevel@tonic-gate 						MSG_INTL(MSG_LDD_REL_ERR2),
2280*7c478bd9Sstevel@tonic-gate 						NAME(lmp));
2281*7c478bd9Sstevel@tonic-gate 				    }
2282*7c478bd9Sstevel@tonic-gate 				}
2283*7c478bd9Sstevel@tonic-gate 				break;
2284*7c478bd9Sstevel@tonic-gate 			case DT_SYMINFO:
2285*7c478bd9Sstevel@tonic-gate 				SYMINFO(lmp) = (Syminfo *)(ld->d_un.d_ptr +
2286*7c478bd9Sstevel@tonic-gate 				    base);
2287*7c478bd9Sstevel@tonic-gate 				break;
2288*7c478bd9Sstevel@tonic-gate 			case DT_SYMINENT:
2289*7c478bd9Sstevel@tonic-gate 				SYMINENT(lmp) = ld->d_un.d_val;
2290*7c478bd9Sstevel@tonic-gate 				break;
2291*7c478bd9Sstevel@tonic-gate 			case DT_PLTPAD:
2292*7c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = (void *)(ld->d_un.d_ptr + base);
2293*7c478bd9Sstevel@tonic-gate 				break;
2294*7c478bd9Sstevel@tonic-gate 			case DT_PLTPADSZ:
2295*7c478bd9Sstevel@tonic-gate 				pltpadsz = ld->d_un.d_val;
2296*7c478bd9Sstevel@tonic-gate 				break;
2297*7c478bd9Sstevel@tonic-gate 			case DT_SUNW_RTLDINF:
2298*7c478bd9Sstevel@tonic-gate 				if ((lml->lm_info_lmp != 0) &&
2299*7c478bd9Sstevel@tonic-gate 				    (lml->lm_info_lmp != lmp)) {
2300*7c478bd9Sstevel@tonic-gate 					DBG_CALL(Dbg_unused_rtldinfo(
2301*7c478bd9Sstevel@tonic-gate 						NAME(lmp),
2302*7c478bd9Sstevel@tonic-gate 						NAME(lml->lm_info_lmp)));
2303*7c478bd9Sstevel@tonic-gate 					break;
2304*7c478bd9Sstevel@tonic-gate 				}
2305*7c478bd9Sstevel@tonic-gate 				lml->lm_info_lmp = lmp;
2306*7c478bd9Sstevel@tonic-gate 				rtldinfo = (void *)(ld->d_un.d_ptr + base);
2307*7c478bd9Sstevel@tonic-gate 
2308*7c478bd9Sstevel@tonic-gate 				/*
2309*7c478bd9Sstevel@tonic-gate 				 * We maintain a list of DT_SUNW_RTLDINFO
2310*7c478bd9Sstevel@tonic-gate 				 * structures for a given object.  This permits
2311*7c478bd9Sstevel@tonic-gate 				 * the RTLDINFO structures to be grouped
2312*7c478bd9Sstevel@tonic-gate 				 * functionly inside of a shared object.
2313*7c478bd9Sstevel@tonic-gate 				 *
2314*7c478bd9Sstevel@tonic-gate 				 * For example, we could have one for
2315*7c478bd9Sstevel@tonic-gate 				 * thread_init, and another for atexit
2316*7c478bd9Sstevel@tonic-gate 				 * reservations.
2317*7c478bd9Sstevel@tonic-gate 				 */
2318*7c478bd9Sstevel@tonic-gate 				if (alist_append(&lml->lm_rtldinfo, &rtldinfo,
2319*7c478bd9Sstevel@tonic-gate 				    sizeof (void *), AL_CNT_RTLDINFO) == 0) {
2320*7c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
2321*7c478bd9Sstevel@tonic-gate 					return (0);
2322*7c478bd9Sstevel@tonic-gate 				}
2323*7c478bd9Sstevel@tonic-gate 				break;
2324*7c478bd9Sstevel@tonic-gate 			case DT_DEPRECATED_SPARC_REGISTER:
2325*7c478bd9Sstevel@tonic-gate 			case M_DT_REGISTER:
2326*7c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_REGSYMS;
2327*7c478bd9Sstevel@tonic-gate 				break;
2328*7c478bd9Sstevel@tonic-gate 			case M_DT_PLTRESERVE:
2329*7c478bd9Sstevel@tonic-gate 				PLTRESERVE(lmp) = (void *)(ld->d_un.d_ptr +
2330*7c478bd9Sstevel@tonic-gate 				    base);
2331*7c478bd9Sstevel@tonic-gate 				break;
2332*7c478bd9Sstevel@tonic-gate 			}
2333*7c478bd9Sstevel@tonic-gate 		}
2334*7c478bd9Sstevel@tonic-gate 
2335*7c478bd9Sstevel@tonic-gate 
2336*7c478bd9Sstevel@tonic-gate 		if (PLTPAD(lmp)) {
2337*7c478bd9Sstevel@tonic-gate 			if (pltpadsz == (Xword)0)
2338*7c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = 0;
2339*7c478bd9Sstevel@tonic-gate 			else
2340*7c478bd9Sstevel@tonic-gate 				PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) +
2341*7c478bd9Sstevel@tonic-gate 				    pltpadsz);
2342*7c478bd9Sstevel@tonic-gate 		}
2343*7c478bd9Sstevel@tonic-gate 
2344*7c478bd9Sstevel@tonic-gate 		/*
2345*7c478bd9Sstevel@tonic-gate 		 * Allocate Dynamic Info structure
2346*7c478bd9Sstevel@tonic-gate 		 */
2347*7c478bd9Sstevel@tonic-gate 		if ((DYNINFO(lmp) = calloc((size_t)dyncnt,
2348*7c478bd9Sstevel@tonic-gate 		    sizeof (Dyninfo))) == 0) {
2349*7c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
2350*7c478bd9Sstevel@tonic-gate 			return (0);
2351*7c478bd9Sstevel@tonic-gate 		}
2352*7c478bd9Sstevel@tonic-gate 		DYNINFOCNT(lmp) = dyncnt;
2353*7c478bd9Sstevel@tonic-gate 	}
2354*7c478bd9Sstevel@tonic-gate 
2355*7c478bd9Sstevel@tonic-gate 	/*
2356*7c478bd9Sstevel@tonic-gate 	 * If configuration file use hasn't been disabled, and a configuration
2357*7c478bd9Sstevel@tonic-gate 	 * file hasn't already been set via an environment variable, see if any
2358*7c478bd9Sstevel@tonic-gate 	 * application specific configuration file is specified.  An LD_CONFIG
2359*7c478bd9Sstevel@tonic-gate 	 * setting is used first, but if this image was generated via crle(1)
2360*7c478bd9Sstevel@tonic-gate 	 * then a default configuration file is a fall-back.
2361*7c478bd9Sstevel@tonic-gate 	 */
2362*7c478bd9Sstevel@tonic-gate 	if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == 0)) {
2363*7c478bd9Sstevel@tonic-gate 		if (cfile)
2364*7c478bd9Sstevel@tonic-gate 			config->c_name = (const char *)(cfile +
2365*7c478bd9Sstevel@tonic-gate 			    (char *)STRTAB(lmp));
2366*7c478bd9Sstevel@tonic-gate 		else if (crle) {
2367*7c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_CONFAPP;
2368*7c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
2369*7c478bd9Sstevel@tonic-gate 			FLAGS1(lmp) |= FL1_RT_RELATIVE;
2370*7c478bd9Sstevel@tonic-gate #endif
2371*7c478bd9Sstevel@tonic-gate 		}
2372*7c478bd9Sstevel@tonic-gate 	}
2373*7c478bd9Sstevel@tonic-gate 
2374*7c478bd9Sstevel@tonic-gate 	if (rpath)
2375*7c478bd9Sstevel@tonic-gate 		RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp));
2376*7c478bd9Sstevel@tonic-gate 	if (fltr) {
2377*7c478bd9Sstevel@tonic-gate 		/*
2378*7c478bd9Sstevel@tonic-gate 		 * If this object is a global filter, duplicate the filtee
2379*7c478bd9Sstevel@tonic-gate 		 * string name(s) so that REFNAME() is available in core files.
2380*7c478bd9Sstevel@tonic-gate 		 * This cludge was useful for debuggers at one point, but only
2381*7c478bd9Sstevel@tonic-gate 		 * when the filtee name was an individual full path.
2382*7c478bd9Sstevel@tonic-gate 		 */
2383*7c478bd9Sstevel@tonic-gate 		if ((REFNAME(lmp) = strdup(fltr + (char *)STRTAB(lmp))) == 0) {
2384*7c478bd9Sstevel@tonic-gate 			remove_so(0, lmp);
2385*7c478bd9Sstevel@tonic-gate 			return (0);
2386*7c478bd9Sstevel@tonic-gate 		}
2387*7c478bd9Sstevel@tonic-gate 	}
2388*7c478bd9Sstevel@tonic-gate 
2389*7c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_RELATIVE)
2390*7c478bd9Sstevel@tonic-gate 		FLAGS1(lmp) |= FL1_RT_RELATIVE;
2391*7c478bd9Sstevel@tonic-gate 
2392*7c478bd9Sstevel@tonic-gate 	/*
2393*7c478bd9Sstevel@tonic-gate 	 * For Intel ABI compatibility.  It's possible that a JMPREL can be
2394*7c478bd9Sstevel@tonic-gate 	 * specified without any other relocations (e.g. a dynamic executable
2395*7c478bd9Sstevel@tonic-gate 	 * normally only contains .plt relocations).  If this is the case then
2396*7c478bd9Sstevel@tonic-gate 	 * no REL, RELSZ or RELENT will have been created.  For us to be able
2397*7c478bd9Sstevel@tonic-gate 	 * to traverse the .plt relocations under LD_BIND_NOW we need to know
2398*7c478bd9Sstevel@tonic-gate 	 * the RELENT for these relocations.  Refer to elf_reloc() for more
2399*7c478bd9Sstevel@tonic-gate 	 * details.
2400*7c478bd9Sstevel@tonic-gate 	 */
2401*7c478bd9Sstevel@tonic-gate 	if (!RELENT(lmp) && JMPREL(lmp))
2402*7c478bd9Sstevel@tonic-gate 		RELENT(lmp) = sizeof (Rel);
2403*7c478bd9Sstevel@tonic-gate 
2404*7c478bd9Sstevel@tonic-gate 	/*
2405*7c478bd9Sstevel@tonic-gate 	 * Establish any per-object auditing.  If we're establishing `main's
2406*7c478bd9Sstevel@tonic-gate 	 * link-map its too early to go searching for audit objects so just
2407*7c478bd9Sstevel@tonic-gate 	 * hold the object name for later (see setup()).
2408*7c478bd9Sstevel@tonic-gate 	 */
2409*7c478bd9Sstevel@tonic-gate 	if (audit) {
2410*7c478bd9Sstevel@tonic-gate 		char	*cp = audit + (char *)STRTAB(lmp);
2411*7c478bd9Sstevel@tonic-gate 
2412*7c478bd9Sstevel@tonic-gate 		if (*cp) {
2413*7c478bd9Sstevel@tonic-gate 			if (((AUDITORS(lmp) =
2414*7c478bd9Sstevel@tonic-gate 			    calloc(1, sizeof (Audit_desc))) == 0) ||
2415*7c478bd9Sstevel@tonic-gate 			    ((AUDITORS(lmp)->ad_name = strdup(cp)) == 0)) {
2416*7c478bd9Sstevel@tonic-gate 				remove_so(0, lmp);
2417*7c478bd9Sstevel@tonic-gate 				return (0);
2418*7c478bd9Sstevel@tonic-gate 			}
2419*7c478bd9Sstevel@tonic-gate 			if (NAME(lmp)) {
2420*7c478bd9Sstevel@tonic-gate 				if (audit_setup(lmp, AUDITORS(lmp)) == 0) {
2421*7c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
2422*7c478bd9Sstevel@tonic-gate 					return (0);
2423*7c478bd9Sstevel@tonic-gate 				}
2424*7c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= AUDITORS(lmp)->ad_flags;
2425*7c478bd9Sstevel@tonic-gate 				lml->lm_flags |= LML_FLG_LOCAUDIT;
2426*7c478bd9Sstevel@tonic-gate 			}
2427*7c478bd9Sstevel@tonic-gate 		}
2428*7c478bd9Sstevel@tonic-gate 	}
2429*7c478bd9Sstevel@tonic-gate 
2430*7c478bd9Sstevel@tonic-gate 	if ((CONDVAR(lmp) = rt_cond_create()) == 0) {
2431*7c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
2432*7c478bd9Sstevel@tonic-gate 		return (0);
2433*7c478bd9Sstevel@tonic-gate 	}
2434*7c478bd9Sstevel@tonic-gate 	if (oname && ((append_alias(lmp, oname, 0)) == 0)) {
2435*7c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
2436*7c478bd9Sstevel@tonic-gate 		return (0);
2437*7c478bd9Sstevel@tonic-gate 	}
2438*7c478bd9Sstevel@tonic-gate 
2439*7c478bd9Sstevel@tonic-gate 	/*
2440*7c478bd9Sstevel@tonic-gate 	 * Add the mapped object to the end of the link map list.
2441*7c478bd9Sstevel@tonic-gate 	 */
2442*7c478bd9Sstevel@tonic-gate 	lm_append(lml, lmco, lmp);
2443*7c478bd9Sstevel@tonic-gate 	return (lmp);
2444*7c478bd9Sstevel@tonic-gate }
2445*7c478bd9Sstevel@tonic-gate 
2446*7c478bd9Sstevel@tonic-gate /*
2447*7c478bd9Sstevel@tonic-gate  * Assign hardware/software capabilities.
2448*7c478bd9Sstevel@tonic-gate  */
2449*7c478bd9Sstevel@tonic-gate void
2450*7c478bd9Sstevel@tonic-gate cap_assign(Cap *cap, Rt_map *lmp)
2451*7c478bd9Sstevel@tonic-gate {
2452*7c478bd9Sstevel@tonic-gate 	while (cap->c_tag != CA_SUNW_NULL) {
2453*7c478bd9Sstevel@tonic-gate 		switch (cap->c_tag) {
2454*7c478bd9Sstevel@tonic-gate 		case CA_SUNW_HW_1:
2455*7c478bd9Sstevel@tonic-gate 			HWCAP(lmp) = cap->c_un.c_val;
2456*7c478bd9Sstevel@tonic-gate 			break;
2457*7c478bd9Sstevel@tonic-gate 		case CA_SUNW_SF_1:
2458*7c478bd9Sstevel@tonic-gate 			SFCAP(lmp) = cap->c_un.c_val;
2459*7c478bd9Sstevel@tonic-gate 		}
2460*7c478bd9Sstevel@tonic-gate 		cap++;
2461*7c478bd9Sstevel@tonic-gate 	}
2462*7c478bd9Sstevel@tonic-gate }
2463*7c478bd9Sstevel@tonic-gate 
2464*7c478bd9Sstevel@tonic-gate /*
2465*7c478bd9Sstevel@tonic-gate  * Map in an ELF object.
2466*7c478bd9Sstevel@tonic-gate  * Takes an open file descriptor for the object to map and its pathname; returns
2467*7c478bd9Sstevel@tonic-gate  * a pointer to a Rt_map structure for this object, or 0 on error.
2468*7c478bd9Sstevel@tonic-gate  */
2469*7c478bd9Sstevel@tonic-gate static Rt_map *
2470*7c478bd9Sstevel@tonic-gate elf_map_so(Lm_list *lml, Aliste lmco, const char *pname, const char *oname,
2471*7c478bd9Sstevel@tonic-gate     int fd)
2472*7c478bd9Sstevel@tonic-gate {
2473*7c478bd9Sstevel@tonic-gate 	int		i; 		/* general temporary */
2474*7c478bd9Sstevel@tonic-gate 	Off		memsize = 0;	/* total memory size of pathname */
2475*7c478bd9Sstevel@tonic-gate 	Off		mentry;		/* entry point */
2476*7c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr;		/* ELF header of ld.so */
2477*7c478bd9Sstevel@tonic-gate 	Phdr		*phdr;		/* first Phdr in file */
2478*7c478bd9Sstevel@tonic-gate 	Phdr		*phdr0;		/* Saved first Phdr in file */
2479*7c478bd9Sstevel@tonic-gate 	Phdr		*pptr;		/* working Phdr */
2480*7c478bd9Sstevel@tonic-gate 	Phdr		*fph = 0;	/* first loadable Phdr */
2481*7c478bd9Sstevel@tonic-gate 	Phdr		*lph;		/* last loadable Phdr */
2482*7c478bd9Sstevel@tonic-gate 	Phdr		*lfph = 0;	/* last loadable (filesz != 0) Phdr */
2483*7c478bd9Sstevel@tonic-gate 	Phdr		*lmph = 0;	/* last loadable (memsz != 0) Phdr */
2484*7c478bd9Sstevel@tonic-gate 	Phdr		*swph = 0;	/* program header for SUNWBSS */
2485*7c478bd9Sstevel@tonic-gate 	Phdr		*tlph = 0;	/* program header for PT_TLS */
2486*7c478bd9Sstevel@tonic-gate 	Phdr		*unwindph = 0;	/* program header for PT_SUNW_UNWIND */
2487*7c478bd9Sstevel@tonic-gate 	Cap		*cap = 0;	/* program header for SUNWCAP */
2488*7c478bd9Sstevel@tonic-gate 	Dyn		*mld = 0;	/* DYNAMIC structure for pathname */
2489*7c478bd9Sstevel@tonic-gate 	size_t		size;		/* size of elf and program headers */
2490*7c478bd9Sstevel@tonic-gate 	caddr_t		faddr = 0;	/* mapping address of pathname */
2491*7c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;		/* link map created */
2492*7c478bd9Sstevel@tonic-gate 	caddr_t		paddr;		/* start of padded image */
2493*7c478bd9Sstevel@tonic-gate 	Off		plen;		/* size of image including padding */
2494*7c478bd9Sstevel@tonic-gate 	const char	*name;
2495*7c478bd9Sstevel@tonic-gate 	Half		etype;
2496*7c478bd9Sstevel@tonic-gate 	int		fixed;
2497*7c478bd9Sstevel@tonic-gate 	Mmap		*mmaps;
2498*7c478bd9Sstevel@tonic-gate 	uint_t		mmapcnt = 0;
2499*7c478bd9Sstevel@tonic-gate 	Xword		align = 0;
2500*7c478bd9Sstevel@tonic-gate 
2501*7c478bd9Sstevel@tonic-gate 	/*
2502*7c478bd9Sstevel@tonic-gate 	 * Establish the objects name.
2503*7c478bd9Sstevel@tonic-gate 	 */
2504*7c478bd9Sstevel@tonic-gate 	if (pname == (char *)0)
2505*7c478bd9Sstevel@tonic-gate 		name = pr_name;
2506*7c478bd9Sstevel@tonic-gate 	else
2507*7c478bd9Sstevel@tonic-gate 		name = pname;
2508*7c478bd9Sstevel@tonic-gate 
2509*7c478bd9Sstevel@tonic-gate 	/* LINTED */
2510*7c478bd9Sstevel@tonic-gate 	ehdr = (Ehdr *)fmap->fm_maddr;
2511*7c478bd9Sstevel@tonic-gate 
2512*7c478bd9Sstevel@tonic-gate 	/*
2513*7c478bd9Sstevel@tonic-gate 	 * If this a relocatable object then special processing is required.
2514*7c478bd9Sstevel@tonic-gate 	 */
2515*7c478bd9Sstevel@tonic-gate 	if ((etype = ehdr->e_type) == ET_REL)
2516*7c478bd9Sstevel@tonic-gate 		return (elf_obj_file(lml, lmco, name, fd));
2517*7c478bd9Sstevel@tonic-gate 
2518*7c478bd9Sstevel@tonic-gate 	/*
2519*7c478bd9Sstevel@tonic-gate 	 * If this isn't a dynamic executable or shared object we can't process
2520*7c478bd9Sstevel@tonic-gate 	 * it.  If this is a dynamic executable then all addresses are fixed.
2521*7c478bd9Sstevel@tonic-gate 	 */
2522*7c478bd9Sstevel@tonic-gate 	if (etype == ET_EXEC)
2523*7c478bd9Sstevel@tonic-gate 		fixed = 1;
2524*7c478bd9Sstevel@tonic-gate 	else if (etype == ET_DYN)
2525*7c478bd9Sstevel@tonic-gate 		fixed = 0;
2526*7c478bd9Sstevel@tonic-gate 	else {
2527*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_ELF, MSG_INTL(MSG_GEN_BADTYPE), name,
2528*7c478bd9Sstevel@tonic-gate 		    conv_etype_str(etype));
2529*7c478bd9Sstevel@tonic-gate 		return (0);
2530*7c478bd9Sstevel@tonic-gate 	}
2531*7c478bd9Sstevel@tonic-gate 
2532*7c478bd9Sstevel@tonic-gate 	/*
2533*7c478bd9Sstevel@tonic-gate 	 * If our original mapped page was not large enough to hold all the
2534*7c478bd9Sstevel@tonic-gate 	 * program headers remap them.
2535*7c478bd9Sstevel@tonic-gate 	 */
2536*7c478bd9Sstevel@tonic-gate 	size = (size_t)((char *)ehdr->e_phoff +
2537*7c478bd9Sstevel@tonic-gate 		(ehdr->e_phnum * ehdr->e_phentsize));
2538*7c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_fsize) {
2539*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), name);
2540*7c478bd9Sstevel@tonic-gate 		return (0);
2541*7c478bd9Sstevel@tonic-gate 	}
2542*7c478bd9Sstevel@tonic-gate 	if (size > fmap->fm_msize) {
2543*7c478bd9Sstevel@tonic-gate 		fmap_setup();
2544*7c478bd9Sstevel@tonic-gate 		if ((fmap->fm_maddr = mmap(fmap->fm_maddr, size, PROT_READ,
2545*7c478bd9Sstevel@tonic-gate 		    fmap->fm_mflags, fd, 0)) == MAP_FAILED) {
2546*7c478bd9Sstevel@tonic-gate 			int	err = errno;
2547*7c478bd9Sstevel@tonic-gate 			eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MMAP), name,
2548*7c478bd9Sstevel@tonic-gate 			    strerror(err));
2549*7c478bd9Sstevel@tonic-gate 			return (0);
2550*7c478bd9Sstevel@tonic-gate 		}
2551*7c478bd9Sstevel@tonic-gate 		fmap->fm_msize = size;
2552*7c478bd9Sstevel@tonic-gate 		/* LINTED */
2553*7c478bd9Sstevel@tonic-gate 		ehdr = (Ehdr *)fmap->fm_maddr;
2554*7c478bd9Sstevel@tonic-gate 	}
2555*7c478bd9Sstevel@tonic-gate 	/* LINTED */
2556*7c478bd9Sstevel@tonic-gate 	phdr0 = phdr = (Phdr *)((char *)ehdr + ehdr->e_ehsize);
2557*7c478bd9Sstevel@tonic-gate 
2558*7c478bd9Sstevel@tonic-gate 	/*
2559*7c478bd9Sstevel@tonic-gate 	 * Get entry point.
2560*7c478bd9Sstevel@tonic-gate 	 */
2561*7c478bd9Sstevel@tonic-gate 	mentry = ehdr->e_entry;
2562*7c478bd9Sstevel@tonic-gate 
2563*7c478bd9Sstevel@tonic-gate 	/*
2564*7c478bd9Sstevel@tonic-gate 	 * Point at program headers and perform some basic validation.
2565*7c478bd9Sstevel@tonic-gate 	 */
2566*7c478bd9Sstevel@tonic-gate 	for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
2567*7c478bd9Sstevel@tonic-gate 	    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
2568*7c478bd9Sstevel@tonic-gate 		if ((pptr->p_type == PT_LOAD) ||
2569*7c478bd9Sstevel@tonic-gate 		    (pptr->p_type == PT_SUNWBSS)) {
2570*7c478bd9Sstevel@tonic-gate 
2571*7c478bd9Sstevel@tonic-gate 			if (fph == 0) {
2572*7c478bd9Sstevel@tonic-gate 				fph = pptr;
2573*7c478bd9Sstevel@tonic-gate 			/* LINTED argument lph is initialized in first pass */
2574*7c478bd9Sstevel@tonic-gate 			} else if (pptr->p_vaddr <= lph->p_vaddr) {
2575*7c478bd9Sstevel@tonic-gate 				eprintf(ERR_ELF, MSG_INTL(MSG_GEN_INVPRGHDR),
2576*7c478bd9Sstevel@tonic-gate 				    name);
2577*7c478bd9Sstevel@tonic-gate 				return (0);
2578*7c478bd9Sstevel@tonic-gate 			}
2579*7c478bd9Sstevel@tonic-gate 
2580*7c478bd9Sstevel@tonic-gate 			lph = pptr;
2581*7c478bd9Sstevel@tonic-gate 
2582*7c478bd9Sstevel@tonic-gate 			if (pptr->p_memsz)
2583*7c478bd9Sstevel@tonic-gate 				lmph = pptr;
2584*7c478bd9Sstevel@tonic-gate 			if (pptr->p_filesz)
2585*7c478bd9Sstevel@tonic-gate 				lfph = pptr;
2586*7c478bd9Sstevel@tonic-gate 			if (pptr->p_type == PT_SUNWBSS)
2587*7c478bd9Sstevel@tonic-gate 				swph = pptr;
2588*7c478bd9Sstevel@tonic-gate 			if (pptr->p_align > align)
2589*7c478bd9Sstevel@tonic-gate 				align = pptr->p_align;
2590*7c478bd9Sstevel@tonic-gate 
2591*7c478bd9Sstevel@tonic-gate 		} else if (pptr->p_type == PT_DYNAMIC)
2592*7c478bd9Sstevel@tonic-gate 			mld = (Dyn *)(pptr->p_vaddr);
2593*7c478bd9Sstevel@tonic-gate 		else if (pptr->p_type == PT_TLS)
2594*7c478bd9Sstevel@tonic-gate 			tlph = pptr;
2595*7c478bd9Sstevel@tonic-gate 		else if (pptr->p_type == PT_SUNWCAP)
2596*7c478bd9Sstevel@tonic-gate 			cap = (Cap *)(pptr->p_vaddr);
2597*7c478bd9Sstevel@tonic-gate 		else if (pptr->p_type == PT_SUNW_UNWIND)
2598*7c478bd9Sstevel@tonic-gate 			unwindph = pptr;
2599*7c478bd9Sstevel@tonic-gate 	}
2600*7c478bd9Sstevel@tonic-gate 
2601*7c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
2602*7c478bd9Sstevel@tonic-gate 	/*
2603*7c478bd9Sstevel@tonic-gate 	 * Make sure the maximum page alignment is a power of 2 >= the system
2604*7c478bd9Sstevel@tonic-gate 	 * page size, for use with MAP_ALIGN.
2605*7c478bd9Sstevel@tonic-gate 	 */
2606*7c478bd9Sstevel@tonic-gate 	align = M_PROUND(align);
2607*7c478bd9Sstevel@tonic-gate #endif
2608*7c478bd9Sstevel@tonic-gate 
2609*7c478bd9Sstevel@tonic-gate 	/*
2610*7c478bd9Sstevel@tonic-gate 	 * We'd better have at least one loadable segment, together with some
2611*7c478bd9Sstevel@tonic-gate 	 * specified file and memory size.
2612*7c478bd9Sstevel@tonic-gate 	 */
2613*7c478bd9Sstevel@tonic-gate 	if ((fph == 0) || (lmph == 0) || (lfph == 0)) {
2614*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_ELF, MSG_INTL(MSG_GEN_NOLOADSEG), name);
2615*7c478bd9Sstevel@tonic-gate 		return (0);
2616*7c478bd9Sstevel@tonic-gate 	}
2617*7c478bd9Sstevel@tonic-gate 
2618*7c478bd9Sstevel@tonic-gate 	/*
2619*7c478bd9Sstevel@tonic-gate 	 * Check that the files size accounts for the loadable sections
2620*7c478bd9Sstevel@tonic-gate 	 * we're going to map in (failure to do this may cause spurious
2621*7c478bd9Sstevel@tonic-gate 	 * bus errors if we're given a truncated file).
2622*7c478bd9Sstevel@tonic-gate 	 */
2623*7c478bd9Sstevel@tonic-gate 	if (fmap->fm_fsize < ((size_t)lfph->p_offset + lfph->p_filesz)) {
2624*7c478bd9Sstevel@tonic-gate 		eprintf(ERR_FATAL, MSG_INTL(MSG_GEN_CORTRUNC), name);
2625*7c478bd9Sstevel@tonic-gate 		return (0);
2626*7c478bd9Sstevel@tonic-gate 	}
2627*7c478bd9Sstevel@tonic-gate 
2628*7c478bd9Sstevel@tonic-gate 	/*
2629*7c478bd9Sstevel@tonic-gate 	 * Memsize must be page rounded so that if we add object padding
2630*7c478bd9Sstevel@tonic-gate 	 * at the end it will start at the beginning of a page.
2631*7c478bd9Sstevel@tonic-gate 	 */
2632*7c478bd9Sstevel@tonic-gate 	plen = memsize = M_PROUND((lmph->p_vaddr + lmph->p_memsz) -
2633*7c478bd9Sstevel@tonic-gate 	    M_PTRUNC((ulong_t)fph->p_vaddr));
2634*7c478bd9Sstevel@tonic-gate 
2635*7c478bd9Sstevel@tonic-gate 	/*
2636*7c478bd9Sstevel@tonic-gate 	 * Determine if an existing mapping is acceptable.
2637*7c478bd9Sstevel@tonic-gate 	 */
2638*7c478bd9Sstevel@tonic-gate 	if (interp && (lml->lm_flags & LML_FLG_BASELM) &&
2639*7c478bd9Sstevel@tonic-gate 	    (strcmp(name, interp->i_name) == 0)) {
2640*7c478bd9Sstevel@tonic-gate 		/*
2641*7c478bd9Sstevel@tonic-gate 		 * If this is the interpreter then it has already been mapped
2642*7c478bd9Sstevel@tonic-gate 		 * and we have the address so don't map it again.  Note that
2643*7c478bd9Sstevel@tonic-gate 		 * the common occurrence of a reference to the interpretor
2644*7c478bd9Sstevel@tonic-gate 		 * (libdl -> ld.so.1) will have been caught during filter
2645*7c478bd9Sstevel@tonic-gate 		 * initialization (see elf_lookup_filtee()).  However, some
2646*7c478bd9Sstevel@tonic-gate 		 * ELF implementations are known to record libc.so.1 as the
2647*7c478bd9Sstevel@tonic-gate 		 * interpretor, and thus this test catches this behavior.
2648*7c478bd9Sstevel@tonic-gate 		 */
2649*7c478bd9Sstevel@tonic-gate 		paddr = faddr = interp->i_faddr;
2650*7c478bd9Sstevel@tonic-gate 
2651*7c478bd9Sstevel@tonic-gate 	} else if ((fixed == 0) && (r_debug.rtd_objpad == 0) &&
2652*7c478bd9Sstevel@tonic-gate 	    (memsize <= fmap->fm_msize) && ((fph->p_flags & PF_W) == 0) &&
2653*7c478bd9Sstevel@tonic-gate 	    (fph->p_filesz == fph->p_memsz) &&
2654*7c478bd9Sstevel@tonic-gate 	    (((Xword)fmap->fm_maddr % align) == 0)) {
2655*7c478bd9Sstevel@tonic-gate 		/*
2656*7c478bd9Sstevel@tonic-gate 		 * If the mapping required has already been established from
2657*7c478bd9Sstevel@tonic-gate 		 * the initial page we don't need to do anything more.  Reset
2658*7c478bd9Sstevel@tonic-gate 		 * the fmap address so then any later files start a new fmap.
2659*7c478bd9Sstevel@tonic-gate 		 * This is really an optimization for filters, such as libdl.so,
2660*7c478bd9Sstevel@tonic-gate 		 * which should only require one page.
2661*7c478bd9Sstevel@tonic-gate 		 */
2662*7c478bd9Sstevel@tonic-gate 		paddr = faddr = fmap->fm_maddr;
2663*7c478bd9Sstevel@tonic-gate 		fmap->fm_maddr = 0;
2664*7c478bd9Sstevel@tonic-gate 		fmap_setup();
2665*7c478bd9Sstevel@tonic-gate 	}
2666*7c478bd9Sstevel@tonic-gate 
2667*7c478bd9Sstevel@tonic-gate 	/*
2668*7c478bd9Sstevel@tonic-gate 	 * Allocate a mapping array to retain mapped segment information.
2669*7c478bd9Sstevel@tonic-gate 	 */
2670*7c478bd9Sstevel@tonic-gate 	if ((mmaps = calloc(ehdr->e_phnum, sizeof (Mmap))) == 0)
2671*7c478bd9Sstevel@tonic-gate 		return (0);
2672*7c478bd9Sstevel@tonic-gate 
2673*7c478bd9Sstevel@tonic-gate 	/*
2674*7c478bd9Sstevel@tonic-gate 	 * If we're reusing an existing mapping determine the objects etext
2675*7c478bd9Sstevel@tonic-gate 	 * address.  Otherwise map the file (which will calculate the etext
2676*7c478bd9Sstevel@tonic-gate 	 * address as part of the mapping process).
2677*7c478bd9Sstevel@tonic-gate 	 */
2678*7c478bd9Sstevel@tonic-gate 	if (faddr) {
2679*7c478bd9Sstevel@tonic-gate 		caddr_t	base;
2680*7c478bd9Sstevel@tonic-gate 
2681*7c478bd9Sstevel@tonic-gate 		if (fixed)
2682*7c478bd9Sstevel@tonic-gate 			base = 0;
2683*7c478bd9Sstevel@tonic-gate 		else
2684*7c478bd9Sstevel@tonic-gate 			base = faddr;
2685*7c478bd9Sstevel@tonic-gate 
2686*7c478bd9Sstevel@tonic-gate 		/* LINTED */
2687*7c478bd9Sstevel@tonic-gate 		phdr0 = phdr = (Phdr *)((char *)faddr + ehdr->e_ehsize);
2688*7c478bd9Sstevel@tonic-gate 
2689*7c478bd9Sstevel@tonic-gate 		for (i = 0, pptr = phdr; i < (int)ehdr->e_phnum; i++,
2690*7c478bd9Sstevel@tonic-gate 		    pptr = (Phdr *)((Off)pptr + ehdr->e_phentsize)) {
2691*7c478bd9Sstevel@tonic-gate 			if (pptr->p_type != PT_LOAD)
2692*7c478bd9Sstevel@tonic-gate 				continue;
2693*7c478bd9Sstevel@tonic-gate 
2694*7c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_vaddr = (pptr->p_vaddr + base);
2695*7c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_msize = pptr->p_memsz;
2696*7c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_fsize = pptr->p_filesz;
2697*7c478bd9Sstevel@tonic-gate 			mmaps[mmapcnt].m_perm = (PROT_READ | PROT_EXEC);
2698*7c478bd9Sstevel@tonic-gate 			mmapcnt++;
2699*7c478bd9Sstevel@tonic-gate 
2700*7c478bd9Sstevel@tonic-gate 			if (!(pptr->p_flags & PF_W)) {
2701*7c478bd9Sstevel@tonic-gate 				fmap->fm_etext = (ulong_t)pptr->p_vaddr +
2702*7c478bd9Sstevel@tonic-gate 				    (ulong_t)pptr->p_memsz +
2703*7c478bd9Sstevel@tonic-gate 				    (ulong_t)(fixed ? 0 : faddr);
2704*7c478bd9Sstevel@tonic-gate 			}
2705*7c478bd9Sstevel@tonic-gate 		}
2706*7c478bd9Sstevel@tonic-gate 	} else {
2707*7c478bd9Sstevel@tonic-gate 		/*
2708*7c478bd9Sstevel@tonic-gate 		 * Map the file.
2709*7c478bd9Sstevel@tonic-gate 		 */
2710*7c478bd9Sstevel@tonic-gate 		if (!(faddr = elf_map_it(name, memsize, ehdr, fph, lph,
2711*7c478bd9Sstevel@tonic-gate 		    &phdr, &paddr, &plen, fixed, fd, align, mmaps, &mmapcnt)))
2712*7c478bd9Sstevel@tonic-gate 			return (0);
2713*7c478bd9Sstevel@tonic-gate 	}
2714*7c478bd9Sstevel@tonic-gate 
2715*7c478bd9Sstevel@tonic-gate 	/*
2716*7c478bd9Sstevel@tonic-gate 	 * Calculate absolute base addresses and entry points.
2717*7c478bd9Sstevel@tonic-gate 	 */
2718*7c478bd9Sstevel@tonic-gate 	if (!fixed) {
2719*7c478bd9Sstevel@tonic-gate 		if (mld)
2720*7c478bd9Sstevel@tonic-gate 			/* LINTED */
2721*7c478bd9Sstevel@tonic-gate 			mld = (Dyn *)((Off)mld + faddr);
2722*7c478bd9Sstevel@tonic-gate 		if (cap)
2723*7c478bd9Sstevel@tonic-gate 			/* LINTED */
2724*7c478bd9Sstevel@tonic-gate 			cap = (Cap *)((Off)cap + faddr);
2725*7c478bd9Sstevel@tonic-gate 		mentry += (Off)faddr;
2726*7c478bd9Sstevel@tonic-gate 	}
2727*7c478bd9Sstevel@tonic-gate 
2728*7c478bd9Sstevel@tonic-gate 	/*
2729*7c478bd9Sstevel@tonic-gate 	 * Create new link map structure for newly mapped shared object.
2730*7c478bd9Sstevel@tonic-gate 	 */
2731*7c478bd9Sstevel@tonic-gate 	if (!(lmp = elf_new_lm(lml, pname, oname, mld, (ulong_t)faddr,
2732*7c478bd9Sstevel@tonic-gate 	    fmap->fm_etext, lmco, memsize, mentry, (ulong_t)paddr, plen, mmaps,
2733*7c478bd9Sstevel@tonic-gate 	    mmapcnt))) {
2734*7c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)faddr, memsize);
2735*7c478bd9Sstevel@tonic-gate 		return (0);
2736*7c478bd9Sstevel@tonic-gate 	}
2737*7c478bd9Sstevel@tonic-gate 
2738*7c478bd9Sstevel@tonic-gate 	/*
2739*7c478bd9Sstevel@tonic-gate 	 * Start the system loading in the ELF information we'll be processing.
2740*7c478bd9Sstevel@tonic-gate 	 */
2741*7c478bd9Sstevel@tonic-gate 	if (REL(lmp)) {
2742*7c478bd9Sstevel@tonic-gate 		(void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) +
2743*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp),
2744*7c478bd9Sstevel@tonic-gate 		    MADV_WILLNEED);
2745*7c478bd9Sstevel@tonic-gate 	}
2746*7c478bd9Sstevel@tonic-gate 
2747*7c478bd9Sstevel@tonic-gate 	/*
2748*7c478bd9Sstevel@tonic-gate 	 * If this shared object contains a any special segments, record them.
2749*7c478bd9Sstevel@tonic-gate 	 */
2750*7c478bd9Sstevel@tonic-gate 	if (swph) {
2751*7c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_SUNWBSS;
2752*7c478bd9Sstevel@tonic-gate 		SUNWBSS(lmp) = phdr + (swph - phdr0);
2753*7c478bd9Sstevel@tonic-gate 	}
2754*7c478bd9Sstevel@tonic-gate 	if (tlph) {
2755*7c478bd9Sstevel@tonic-gate 		PTTLS(lmp) = phdr + (tlph - phdr0);
2756*7c478bd9Sstevel@tonic-gate 		tls_assign_soffset(lmp);
2757*7c478bd9Sstevel@tonic-gate 	}
2758*7c478bd9Sstevel@tonic-gate 
2759*7c478bd9Sstevel@tonic-gate 	if (unwindph)
2760*7c478bd9Sstevel@tonic-gate 		PTUNWIND(lmp) = phdr + (unwindph - phdr0);
2761*7c478bd9Sstevel@tonic-gate 
2762*7c478bd9Sstevel@tonic-gate 	if (cap)
2763*7c478bd9Sstevel@tonic-gate 		cap_assign(cap, lmp);
2764*7c478bd9Sstevel@tonic-gate 
2765*7c478bd9Sstevel@tonic-gate 	return (lmp);
2766*7c478bd9Sstevel@tonic-gate }
2767*7c478bd9Sstevel@tonic-gate 
2768*7c478bd9Sstevel@tonic-gate 
2769*7c478bd9Sstevel@tonic-gate /*
2770*7c478bd9Sstevel@tonic-gate  * Function to correct protection settings.  Segments are all mapped initially
2771*7c478bd9Sstevel@tonic-gate  * with permissions as given in the segment header.  We need to turn on write
2772*7c478bd9Sstevel@tonic-gate  * permissions on a text segment if there are any relocations against that
2773*7c478bd9Sstevel@tonic-gate  * segment, and them turn write permission back off again before returning
2774*7c478bd9Sstevel@tonic-gate  * control to the user.  This function turns the permission on or off depending
2775*7c478bd9Sstevel@tonic-gate  * on the value of the argument.
2776*7c478bd9Sstevel@tonic-gate  */
2777*7c478bd9Sstevel@tonic-gate int
2778*7c478bd9Sstevel@tonic-gate elf_set_prot(Rt_map * lmp, int permission)
2779*7c478bd9Sstevel@tonic-gate {
2780*7c478bd9Sstevel@tonic-gate 	Mmap	*mmaps;
2781*7c478bd9Sstevel@tonic-gate 
2782*7c478bd9Sstevel@tonic-gate 	/*
2783*7c478bd9Sstevel@tonic-gate 	 * If this is an allocated image (ie. a relocatable object) we can't
2784*7c478bd9Sstevel@tonic-gate 	 * mprotect() anything.
2785*7c478bd9Sstevel@tonic-gate 	 */
2786*7c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_IMGALLOC)
2787*7c478bd9Sstevel@tonic-gate 		return (1);
2788*7c478bd9Sstevel@tonic-gate 
2789*7c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_file_prot(NAME(lmp), permission));
2790*7c478bd9Sstevel@tonic-gate 
2791*7c478bd9Sstevel@tonic-gate 	for (mmaps = MMAPS(lmp); mmaps->m_vaddr; mmaps++) {
2792*7c478bd9Sstevel@tonic-gate 		if (mmaps->m_perm & PROT_WRITE)
2793*7c478bd9Sstevel@tonic-gate 			continue;
2794*7c478bd9Sstevel@tonic-gate 
2795*7c478bd9Sstevel@tonic-gate 		if (mprotect(mmaps->m_vaddr, mmaps->m_msize,
2796*7c478bd9Sstevel@tonic-gate 		    (mmaps->m_perm | permission)) == -1) {
2797*7c478bd9Sstevel@tonic-gate 			int	err = errno;
2798*7c478bd9Sstevel@tonic-gate 			eprintf(ERR_FATAL, MSG_INTL(MSG_SYS_MPROT),
2799*7c478bd9Sstevel@tonic-gate 			    NAME(lmp), strerror(err));
2800*7c478bd9Sstevel@tonic-gate 			return (0);
2801*7c478bd9Sstevel@tonic-gate 		}
2802*7c478bd9Sstevel@tonic-gate 	}
2803*7c478bd9Sstevel@tonic-gate 	return (1);
2804*7c478bd9Sstevel@tonic-gate }
2805*7c478bd9Sstevel@tonic-gate 
2806*7c478bd9Sstevel@tonic-gate /*
2807*7c478bd9Sstevel@tonic-gate  * Build full pathname of shared object from given directory name and filename.
2808*7c478bd9Sstevel@tonic-gate  */
2809*7c478bd9Sstevel@tonic-gate static char *
2810*7c478bd9Sstevel@tonic-gate elf_get_so(const char *dir, const char *file)
2811*7c478bd9Sstevel@tonic-gate {
2812*7c478bd9Sstevel@tonic-gate 	static char	pname[PATH_MAX];
2813*7c478bd9Sstevel@tonic-gate 
2814*7c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, MSG_ORIG(MSG_FMT_PATH), dir, file);
2815*7c478bd9Sstevel@tonic-gate 	return (pname);
2816*7c478bd9Sstevel@tonic-gate }
2817*7c478bd9Sstevel@tonic-gate 
2818*7c478bd9Sstevel@tonic-gate /*
2819*7c478bd9Sstevel@tonic-gate  * The copy relocation is recorded in a copy structure which will be applied
2820*7c478bd9Sstevel@tonic-gate  * after all other relocations are carried out.  This provides for copying data
2821*7c478bd9Sstevel@tonic-gate  * that must be relocated itself (ie. pointers in shared objects).  This
2822*7c478bd9Sstevel@tonic-gate  * structure also provides a means of binding RTLD_GROUP dependencies to any
2823*7c478bd9Sstevel@tonic-gate  * copy relocations that have been taken from any group members.
2824*7c478bd9Sstevel@tonic-gate  *
2825*7c478bd9Sstevel@tonic-gate  * If the size of the .bss area available for the copy information is not the
2826*7c478bd9Sstevel@tonic-gate  * same as the source of the data inform the user if we're under ldd(1) control
2827*7c478bd9Sstevel@tonic-gate  * (this checking was only established in 5.3, so by only issuing an error via
2828*7c478bd9Sstevel@tonic-gate  * ldd(1) we maintain the standard set by previous releases).
2829*7c478bd9Sstevel@tonic-gate  */
2830*7c478bd9Sstevel@tonic-gate int
2831*7c478bd9Sstevel@tonic-gate elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym,
2832*7c478bd9Sstevel@tonic-gate     Rt_map *dlmp, const void *dadd)
2833*7c478bd9Sstevel@tonic-gate {
2834*7c478bd9Sstevel@tonic-gate 	Rel_copy	rc;
2835*7c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(rlmp);
2836*7c478bd9Sstevel@tonic-gate 
2837*7c478bd9Sstevel@tonic-gate 	rc.r_name = name;
2838*7c478bd9Sstevel@tonic-gate 	rc.r_rsym = rsym;		/* the new reference symbol and its */
2839*7c478bd9Sstevel@tonic-gate 	rc.r_rlmp = rlmp;		/*	associated link-map */
2840*7c478bd9Sstevel@tonic-gate 	rc.r_dlmp = dlmp;		/* the defining link-map */
2841*7c478bd9Sstevel@tonic-gate 	rc.r_dsym = dsym;		/* the original definition */
2842*7c478bd9Sstevel@tonic-gate 	rc.r_radd = radd;
2843*7c478bd9Sstevel@tonic-gate 	rc.r_dadd = dadd;
2844*7c478bd9Sstevel@tonic-gate 
2845*7c478bd9Sstevel@tonic-gate 	if (rsym->st_size > dsym->st_size)
2846*7c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)dsym->st_size;
2847*7c478bd9Sstevel@tonic-gate 	else
2848*7c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)rsym->st_size;
2849*7c478bd9Sstevel@tonic-gate 
2850*7c478bd9Sstevel@tonic-gate 	if (alist_append(&COPY(dlmp), &rc, sizeof (Rel_copy),
2851*7c478bd9Sstevel@tonic-gate 	    AL_CNT_COPYREL) == 0) {
2852*7c478bd9Sstevel@tonic-gate 		if (!(lml->lm_flags & LML_FLG_TRC_WARN))
2853*7c478bd9Sstevel@tonic-gate 			return (0);
2854*7c478bd9Sstevel@tonic-gate 		else
2855*7c478bd9Sstevel@tonic-gate 			return (1);
2856*7c478bd9Sstevel@tonic-gate 	}
2857*7c478bd9Sstevel@tonic-gate 	if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) {
2858*7c478bd9Sstevel@tonic-gate 		if (alist_append(&COPY(rlmp), &dlmp,
2859*7c478bd9Sstevel@tonic-gate 		    sizeof (Rt_map *), AL_CNT_COPYREL) == 0) {
2860*7c478bd9Sstevel@tonic-gate 			if (!(lml->lm_flags & LML_FLG_TRC_WARN))
2861*7c478bd9Sstevel@tonic-gate 				return (0);
2862*7c478bd9Sstevel@tonic-gate 			else
2863*7c478bd9Sstevel@tonic-gate 				return (1);
2864*7c478bd9Sstevel@tonic-gate 		}
2865*7c478bd9Sstevel@tonic-gate 		FLAGS1(dlmp) |= FL1_RT_COPYTOOK;
2866*7c478bd9Sstevel@tonic-gate 	}
2867*7c478bd9Sstevel@tonic-gate 
2868*7c478bd9Sstevel@tonic-gate 	/*
2869*7c478bd9Sstevel@tonic-gate 	 * If we are tracing (ldd), warn the user if
2870*7c478bd9Sstevel@tonic-gate 	 *	1) the size from the reference symbol differs from the
2871*7c478bd9Sstevel@tonic-gate 	 *	   copy definition. We can only copy as much data as the
2872*7c478bd9Sstevel@tonic-gate 	 *	   reference (dynamic executables) entry allows.
2873*7c478bd9Sstevel@tonic-gate 	 *	2) the copy definition has STV_PROTECTED visibility.
2874*7c478bd9Sstevel@tonic-gate 	 */
2875*7c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
2876*7c478bd9Sstevel@tonic-gate 		if (rsym->st_size != dsym->st_size) {
2877*7c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF),
2878*7c478bd9Sstevel@tonic-gate 			    _conv_reloc_type_str(M_R_COPY), demangle(name),
2879*7c478bd9Sstevel@tonic-gate 			    NAME(rlmp), EC_XWORD(rsym->st_size),
2880*7c478bd9Sstevel@tonic-gate 			    NAME(dlmp), EC_XWORD(dsym->st_size));
2881*7c478bd9Sstevel@tonic-gate 			if (rsym->st_size > dsym->st_size)
2882*7c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA),
2883*7c478bd9Sstevel@tonic-gate 				    NAME(dlmp));
2884*7c478bd9Sstevel@tonic-gate 			else
2885*7c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC),
2886*7c478bd9Sstevel@tonic-gate 				    NAME(rlmp));
2887*7c478bd9Sstevel@tonic-gate 		}
2888*7c478bd9Sstevel@tonic-gate 
2889*7c478bd9Sstevel@tonic-gate 		if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) {
2890*7c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_PROT),
2891*7c478bd9Sstevel@tonic-gate 			    _conv_reloc_type_str(M_R_COPY), demangle(name),
2892*7c478bd9Sstevel@tonic-gate 				NAME(dlmp));
2893*7c478bd9Sstevel@tonic-gate 		}
2894*7c478bd9Sstevel@tonic-gate 	}
2895*7c478bd9Sstevel@tonic-gate 
2896*7c478bd9Sstevel@tonic-gate 	DBG_CALL(Dbg_reloc_apply((Xword)radd, (Xword)rc.r_size));
2897*7c478bd9Sstevel@tonic-gate 	return (1);
2898*7c478bd9Sstevel@tonic-gate }
2899*7c478bd9Sstevel@tonic-gate 
2900*7c478bd9Sstevel@tonic-gate /*
2901*7c478bd9Sstevel@tonic-gate  * Determine the symbol location of an address within a link-map.  Look for
2902*7c478bd9Sstevel@tonic-gate  * the nearest symbol (whose value is less than or equal to the required
2903*7c478bd9Sstevel@tonic-gate  * address).  This is the object specific part of dladdr().
2904*7c478bd9Sstevel@tonic-gate  */
2905*7c478bd9Sstevel@tonic-gate static void
2906*7c478bd9Sstevel@tonic-gate elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags)
2907*7c478bd9Sstevel@tonic-gate {
2908*7c478bd9Sstevel@tonic-gate 	ulong_t		ndx, cnt, base, _value;
2909*7c478bd9Sstevel@tonic-gate 	Sym		*sym, *_sym;
2910*7c478bd9Sstevel@tonic-gate 	const char	*str;
2911*7c478bd9Sstevel@tonic-gate 
2912*7c478bd9Sstevel@tonic-gate 	/*
2913*7c478bd9Sstevel@tonic-gate 	 * If we don't have a .hash table there are no symbols to look at.
2914*7c478bd9Sstevel@tonic-gate 	 */
2915*7c478bd9Sstevel@tonic-gate 	if (HASH(lmp) == 0)
2916*7c478bd9Sstevel@tonic-gate 		return;
2917*7c478bd9Sstevel@tonic-gate 
2918*7c478bd9Sstevel@tonic-gate 	cnt = HASH(lmp)[1];
2919*7c478bd9Sstevel@tonic-gate 	str = STRTAB(lmp);
2920*7c478bd9Sstevel@tonic-gate 	sym = SYMTAB(lmp);
2921*7c478bd9Sstevel@tonic-gate 
2922*7c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_FIXED)
2923*7c478bd9Sstevel@tonic-gate 		base = 0;
2924*7c478bd9Sstevel@tonic-gate 	else
2925*7c478bd9Sstevel@tonic-gate 		base = ADDR(lmp);
2926*7c478bd9Sstevel@tonic-gate 
2927*7c478bd9Sstevel@tonic-gate 	for (_sym = 0, _value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) {
2928*7c478bd9Sstevel@tonic-gate 		ulong_t	value;
2929*7c478bd9Sstevel@tonic-gate 
2930*7c478bd9Sstevel@tonic-gate 		if (sym->st_shndx == SHN_UNDEF)
2931*7c478bd9Sstevel@tonic-gate 			continue;
2932*7c478bd9Sstevel@tonic-gate 
2933*7c478bd9Sstevel@tonic-gate 		value = sym->st_value + base;
2934*7c478bd9Sstevel@tonic-gate 		if (value > addr)
2935*7c478bd9Sstevel@tonic-gate 			continue;
2936*7c478bd9Sstevel@tonic-gate 		if (value < _value)
2937*7c478bd9Sstevel@tonic-gate 			continue;
2938*7c478bd9Sstevel@tonic-gate 
2939*7c478bd9Sstevel@tonic-gate 		_sym = sym;
2940*7c478bd9Sstevel@tonic-gate 		_value = value;
2941*7c478bd9Sstevel@tonic-gate 
2942*7c478bd9Sstevel@tonic-gate 		/*
2943*7c478bd9Sstevel@tonic-gate 		 * Note, because we accept local and global symbols we could
2944*7c478bd9Sstevel@tonic-gate 		 * find a section symbol that matches the associated address,
2945*7c478bd9Sstevel@tonic-gate 		 * which means that the symbol name will be null.  In this
2946*7c478bd9Sstevel@tonic-gate 		 * case continue the search in case we can find a global
2947*7c478bd9Sstevel@tonic-gate 		 * symbol of the same value.
2948*7c478bd9Sstevel@tonic-gate 		 */
2949*7c478bd9Sstevel@tonic-gate 		if ((value == addr) &&
2950*7c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) != STT_SECTION))
2951*7c478bd9Sstevel@tonic-gate 			break;
2952*7c478bd9Sstevel@tonic-gate 	}
2953*7c478bd9Sstevel@tonic-gate 
2954*7c478bd9Sstevel@tonic-gate 	if (_sym) {
2955*7c478bd9Sstevel@tonic-gate 		int	_flags = flags & RTLD_DL_MASK;
2956*7c478bd9Sstevel@tonic-gate 
2957*7c478bd9Sstevel@tonic-gate 		if (_flags == RTLD_DL_SYMENT)
2958*7c478bd9Sstevel@tonic-gate 			*info = (void *)_sym;
2959*7c478bd9Sstevel@tonic-gate 		else if (_flags == RTLD_DL_LINKMAP)
2960*7c478bd9Sstevel@tonic-gate 			*info = (void *)lmp;
2961*7c478bd9Sstevel@tonic-gate 
2962*7c478bd9Sstevel@tonic-gate 		dlip->dli_sname = str + _sym->st_name;
2963*7c478bd9Sstevel@tonic-gate 		dlip->dli_saddr = (void *)_value;
2964*7c478bd9Sstevel@tonic-gate 	}
2965*7c478bd9Sstevel@tonic-gate }
2966*7c478bd9Sstevel@tonic-gate 
2967*7c478bd9Sstevel@tonic-gate static void
2968*7c478bd9Sstevel@tonic-gate elf_lazy_cleanup(Alist * alp)
2969*7c478bd9Sstevel@tonic-gate {
2970*7c478bd9Sstevel@tonic-gate 	Rt_map **	lmpp;
2971*7c478bd9Sstevel@tonic-gate 	Aliste		off;
2972*7c478bd9Sstevel@tonic-gate 
2973*7c478bd9Sstevel@tonic-gate 	/*
2974*7c478bd9Sstevel@tonic-gate 	 * Cleanup any link-maps added to this dynamic list and free it.
2975*7c478bd9Sstevel@tonic-gate 	 */
2976*7c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alp, off, lmpp))
2977*7c478bd9Sstevel@tonic-gate 		FLAGS(*lmpp) &= ~FLG_RT_DLSYM;
2978*7c478bd9Sstevel@tonic-gate 	free(alp);
2979*7c478bd9Sstevel@tonic-gate }
2980*7c478bd9Sstevel@tonic-gate 
2981*7c478bd9Sstevel@tonic-gate /*
2982*7c478bd9Sstevel@tonic-gate  * This routine is called upon to search for a symbol from the dependencies of
2983*7c478bd9Sstevel@tonic-gate  * the initial link-map.  To maintain lazy loadings goal of reducing the number
2984*7c478bd9Sstevel@tonic-gate  * of objects mapped, any symbol search is first carried out using the objects
2985*7c478bd9Sstevel@tonic-gate  * that already exist in the process (either on a link-map list or handle).
2986*7c478bd9Sstevel@tonic-gate  * If a symbol can't be found, and lazy dependencies are still pending, this
2987*7c478bd9Sstevel@tonic-gate  * routine loads the dependencies in an attempt to locate the symbol.
2988*7c478bd9Sstevel@tonic-gate  *
2989*7c478bd9Sstevel@tonic-gate  * Only new objects are inspected as we will have already inspected presently
2990*7c478bd9Sstevel@tonic-gate  * loaded objects before calling this routine.  However, a new object may not
2991*7c478bd9Sstevel@tonic-gate  * be new - although the di_lmp might be zero, the object may have been mapped
2992*7c478bd9Sstevel@tonic-gate  * as someone elses dependency.  Thus there's a possibility of some symbol
2993*7c478bd9Sstevel@tonic-gate  * search duplication.
2994*7c478bd9Sstevel@tonic-gate  */
2995*7c478bd9Sstevel@tonic-gate 
2996*7c478bd9Sstevel@tonic-gate Sym *
2997*7c478bd9Sstevel@tonic-gate elf_lazy_find_sym(Slookup *slp, Rt_map **_lmp, uint_t *binfo)
2998*7c478bd9Sstevel@tonic-gate {
2999*7c478bd9Sstevel@tonic-gate 	Sym		*sym = 0;
3000*7c478bd9Sstevel@tonic-gate 	Alist *		alist = 0;
3001*7c478bd9Sstevel@tonic-gate 	Aliste		off;
3002*7c478bd9Sstevel@tonic-gate 	Rt_map **	lmpp, *	lmp = slp->sl_imap;
3003*7c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
3004*7c478bd9Sstevel@tonic-gate 
3005*7c478bd9Sstevel@tonic-gate 	if (alist_append(&alist, &lmp, sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0)
3006*7c478bd9Sstevel@tonic-gate 		return (0);
3007*7c478bd9Sstevel@tonic-gate 	FLAGS(lmp) |= FLG_RT_DLSYM;
3008*7c478bd9Sstevel@tonic-gate 
3009*7c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(alist, off, lmpp)) {
3010*7c478bd9Sstevel@tonic-gate 		uint_t	cnt = 0;
3011*7c478bd9Sstevel@tonic-gate 		Slookup	sl = *slp;
3012*7c478bd9Sstevel@tonic-gate 		Dyninfo	*dip;
3013*7c478bd9Sstevel@tonic-gate 
3014*7c478bd9Sstevel@tonic-gate 		/*
3015*7c478bd9Sstevel@tonic-gate 		 * Loop through the DT_NEEDED entries examining each object for
3016*7c478bd9Sstevel@tonic-gate 		 * the symbol.  If the symbol is not found the object is in turn
3017*7c478bd9Sstevel@tonic-gate 		 * added to the alist, so that its DT_NEEDED entires may be
3018*7c478bd9Sstevel@tonic-gate 		 * examined.
3019*7c478bd9Sstevel@tonic-gate 		 */
3020*7c478bd9Sstevel@tonic-gate 		lmp = *lmpp;
3021*7c478bd9Sstevel@tonic-gate 		for (dip = DYNINFO(lmp); cnt < DYNINFOCNT(lmp); cnt++, dip++) {
3022*7c478bd9Sstevel@tonic-gate 			Rt_map *nlmp;
3023*7c478bd9Sstevel@tonic-gate 
3024*7c478bd9Sstevel@tonic-gate 			if (((dip->di_flags & FLG_DI_NEEDED) == 0) ||
3025*7c478bd9Sstevel@tonic-gate 			    dip->di_info)
3026*7c478bd9Sstevel@tonic-gate 				continue;
3027*7c478bd9Sstevel@tonic-gate 
3028*7c478bd9Sstevel@tonic-gate 			/*
3029*7c478bd9Sstevel@tonic-gate 			 * If this entry defines a lazy dependency try loading
3030*7c478bd9Sstevel@tonic-gate 			 * it.  If the file can't be loaded, consider this
3031*7c478bd9Sstevel@tonic-gate 			 * non-fatal and continue the search (lazy loaded
3032*7c478bd9Sstevel@tonic-gate 			 * dependencies need not exist and their loading should
3033*7c478bd9Sstevel@tonic-gate 			 * only be fatal if called from a relocation).
3034*7c478bd9Sstevel@tonic-gate 			 *
3035*7c478bd9Sstevel@tonic-gate 			 * If the file is already loaded and relocated we must
3036*7c478bd9Sstevel@tonic-gate 			 * still inspect it for symbols, even though it might
3037*7c478bd9Sstevel@tonic-gate 			 * have already been searched.  This lazy load operation
3038*7c478bd9Sstevel@tonic-gate 			 * might have promoted the permissions of the object,
3039*7c478bd9Sstevel@tonic-gate 			 * and thus made the object applicable for this symbol
3040*7c478bd9Sstevel@tonic-gate 			 * search, whereas before the object might have been
3041*7c478bd9Sstevel@tonic-gate 			 * skipped.
3042*7c478bd9Sstevel@tonic-gate 			 */
3043*7c478bd9Sstevel@tonic-gate 			if ((nlmp = elf_lazy_load(lmp, cnt, name)) == 0)
3044*7c478bd9Sstevel@tonic-gate 				continue;
3045*7c478bd9Sstevel@tonic-gate 
3046*7c478bd9Sstevel@tonic-gate 			/*
3047*7c478bd9Sstevel@tonic-gate 			 * If this object isn't yet a part of the dynamic list
3048*7c478bd9Sstevel@tonic-gate 			 * then inspect it for the symbol.  If the symbol isn't
3049*7c478bd9Sstevel@tonic-gate 			 * found add the object to the dynamic list so that we
3050*7c478bd9Sstevel@tonic-gate 			 * can inspect its dependencies.
3051*7c478bd9Sstevel@tonic-gate 			 */
3052*7c478bd9Sstevel@tonic-gate 			if (FLAGS(nlmp) & FLG_RT_DLSYM)
3053*7c478bd9Sstevel@tonic-gate 				continue;
3054*7c478bd9Sstevel@tonic-gate 
3055*7c478bd9Sstevel@tonic-gate 			sl.sl_imap = nlmp;
3056*7c478bd9Sstevel@tonic-gate 			if (sym = LM_LOOKUP_SYM(sl.sl_cmap)(&sl, _lmp, binfo))
3057*7c478bd9Sstevel@tonic-gate 				break;
3058*7c478bd9Sstevel@tonic-gate 
3059*7c478bd9Sstevel@tonic-gate 			/*
3060*7c478bd9Sstevel@tonic-gate 			 * Some dlsym() operations are already traversing a
3061*7c478bd9Sstevel@tonic-gate 			 * link-map (dlopen(0)), and thus there's no need to
3062*7c478bd9Sstevel@tonic-gate 			 * build our own dynamic dependency list.
3063*7c478bd9Sstevel@tonic-gate 			 */
3064*7c478bd9Sstevel@tonic-gate 			if ((sl.sl_flags & LKUP_NODESCENT) == 0) {
3065*7c478bd9Sstevel@tonic-gate 				if (alist_append(&alist, &nlmp,
3066*7c478bd9Sstevel@tonic-gate 				    sizeof (Rt_map *), AL_CNT_LAZYFIND) == 0) {
3067*7c478bd9Sstevel@tonic-gate 					elf_lazy_cleanup(alist);
3068*7c478bd9Sstevel@tonic-gate 					return (0);
3069*7c478bd9Sstevel@tonic-gate 				}
3070*7c478bd9Sstevel@tonic-gate 				FLAGS(nlmp) |= FLG_RT_DLSYM;
3071*7c478bd9Sstevel@tonic-gate 			}
3072*7c478bd9Sstevel@tonic-gate 		}
3073*7c478bd9Sstevel@tonic-gate 		if (sym)
3074*7c478bd9Sstevel@tonic-gate 			break;
3075*7c478bd9Sstevel@tonic-gate 	}
3076*7c478bd9Sstevel@tonic-gate 
3077*7c478bd9Sstevel@tonic-gate 	elf_lazy_cleanup(alist);
3078*7c478bd9Sstevel@tonic-gate 	return (sym);
3079*7c478bd9Sstevel@tonic-gate }
3080*7c478bd9Sstevel@tonic-gate 
3081*7c478bd9Sstevel@tonic-gate /*
3082*7c478bd9Sstevel@tonic-gate  * Warning message for bad r_offset.
3083*7c478bd9Sstevel@tonic-gate  */
3084*7c478bd9Sstevel@tonic-gate void
3085*7c478bd9Sstevel@tonic-gate elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset,
3086*7c478bd9Sstevel@tonic-gate     ulong_t rsymndx)
3087*7c478bd9Sstevel@tonic-gate {
3088*7c478bd9Sstevel@tonic-gate 	const char	*name = (char *)0;
3089*7c478bd9Sstevel@tonic-gate 	int		trace;
3090*7c478bd9Sstevel@tonic-gate 
3091*7c478bd9Sstevel@tonic-gate 	if ((LIST(lmp)->lm_flags & LML_FLG_TRC_ENABLE) &&
3092*7c478bd9Sstevel@tonic-gate 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
3093*7c478bd9Sstevel@tonic-gate 	    (LIST(lmp)->lm_flags & LML_FLG_TRC_VERBOSE)))
3094*7c478bd9Sstevel@tonic-gate 		trace = 1;
3095*7c478bd9Sstevel@tonic-gate 	else
3096*7c478bd9Sstevel@tonic-gate 		trace = 0;
3097*7c478bd9Sstevel@tonic-gate 
3098*7c478bd9Sstevel@tonic-gate 	if ((trace == 0) && (dbg_mask == 0))
3099*7c478bd9Sstevel@tonic-gate 		return;
3100*7c478bd9Sstevel@tonic-gate 
3101*7c478bd9Sstevel@tonic-gate 	if (rsymndx) {
3102*7c478bd9Sstevel@tonic-gate 		Sym	*symref = (Sym *)((ulong_t)SYMTAB(lmp) +
3103*7c478bd9Sstevel@tonic-gate 				(rsymndx * SYMENT(lmp)));
3104*7c478bd9Sstevel@tonic-gate 
3105*7c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(symref->st_info) != STB_LOCAL)
3106*7c478bd9Sstevel@tonic-gate 			name = (char *)(STRTAB(lmp) + symref->st_name);
3107*7c478bd9Sstevel@tonic-gate 	}
3108*7c478bd9Sstevel@tonic-gate 
3109*7c478bd9Sstevel@tonic-gate 	if (name == 0)
3110*7c478bd9Sstevel@tonic-gate 		name = MSG_ORIG(MSG_STR_EMPTY);
3111*7c478bd9Sstevel@tonic-gate 
3112*7c478bd9Sstevel@tonic-gate 	if (trace) {
3113*7c478bd9Sstevel@tonic-gate 		const char *rstr;
3114*7c478bd9Sstevel@tonic-gate 
3115*7c478bd9Sstevel@tonic-gate 		rstr = _conv_reloc_type_str((uint_t)rtype);
3116*7c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name,
3117*7c478bd9Sstevel@tonic-gate 		    EC_ADDR(roffset));
3118*7c478bd9Sstevel@tonic-gate 		return;
3119*7c478bd9Sstevel@tonic-gate 	}
3120*7c478bd9Sstevel@tonic-gate 
3121*7c478bd9Sstevel@tonic-gate 	Dbg_reloc_error(M_MACH, M_REL_SHT_TYPE, rel, name,
3122*7c478bd9Sstevel@tonic-gate 		MSG_ORIG(MSG_REL_BADROFFSET));
3123*7c478bd9Sstevel@tonic-gate }
3124