xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/elf.c (revision 56deab07)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2111a2bb38Srie 
227c478bd9Sstevel@tonic-gate /*
23*56deab07SRod Evans  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267257d1b4Sraf 
277257d1b4Sraf /*
287257d1b4Sraf  *	Copyright (c) 1988 AT&T
297257d1b4Sraf  *	  All Rights Reserved
307257d1b4Sraf  */
317257d1b4Sraf 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Object file dependent support for ELF objects.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	<stdio.h>
377c478bd9Sstevel@tonic-gate #include	<sys/procfs.h>
387c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
397c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
407c478bd9Sstevel@tonic-gate #include	<string.h>
417c478bd9Sstevel@tonic-gate #include	<limits.h>
427c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
435aefb655Srie #include	<debug.h>
445aefb655Srie #include	<conv.h>
457c478bd9Sstevel@tonic-gate #include	"_rtld.h"
467c478bd9Sstevel@tonic-gate #include	"_audit.h"
477c478bd9Sstevel@tonic-gate #include	"_elf.h"
48*56deab07SRod Evans #include	"_inline.h"
497c478bd9Sstevel@tonic-gate #include	"msg.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Default and secure dependency search paths.
537c478bd9Sstevel@tonic-gate  */
54*56deab07SRod Evans static Spath_defn _elf_def_dirs[] = {
557c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
56*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIB_64),		MSG_PTH_LIB_64_SIZE },
57*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIB_64),		MSG_PTH_USRLIB_64_SIZE },
587c478bd9Sstevel@tonic-gate #else
59*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIB),		MSG_PTH_LIB_SIZE },
60*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIB),		MSG_PTH_USRLIB_SIZE },
617c478bd9Sstevel@tonic-gate #endif
62*56deab07SRod Evans 	{ 0, 0 }
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
65*56deab07SRod Evans static Spath_defn _elf_sec_dirs[] = {
667c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
67*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIBSE_64),		MSG_PTH_LIBSE_64_SIZE },
68*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIBSE_64),	MSG_PTH_USRLIBSE_64_SIZE },
697c478bd9Sstevel@tonic-gate #else
70*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIBSE),		MSG_PTH_LIBSE_SIZE },
71*56deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIBSE),		MSG_PTH_USRLIBSE_SIZE },
727c478bd9Sstevel@tonic-gate #endif
73*56deab07SRod Evans 	{ 0, 0 }
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
76*56deab07SRod Evans Alist	*elf_def_dirs = NULL;
77*56deab07SRod Evans Alist	*elf_sec_dirs = NULL;
78*56deab07SRod Evans 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Defines for local functions.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static void	elf_dladdr(ulong_t, Rt_map *, Dl_info *, void **, int);
83*56deab07SRod Evans static Addr	elf_entry_point(void);
84*56deab07SRod Evans static int	elf_fix_name(const char *, Rt_map *, Alist **, Aliste, uint_t);
85*56deab07SRod Evans static Alist	**elf_get_def_dirs(void);
86*56deab07SRod Evans static Alist	**elf_get_sec_dirs(void);
87*56deab07SRod Evans static char	*elf_get_so(const char *, const char *, size_t, size_t);
889aa23310Srie static int	elf_needed(Lm_list *, Aliste, Rt_map *, int *);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Functions and data accessed through indirect pointers.
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate Fct elf_fct = {
94*56deab07SRod Evans 	elf_verify,
95*56deab07SRod Evans 	elf_new_lmp,
96*56deab07SRod Evans 	elf_entry_point,
977c478bd9Sstevel@tonic-gate 	elf_needed,
987c478bd9Sstevel@tonic-gate 	lookup_sym,
997c478bd9Sstevel@tonic-gate 	elf_reloc,
100*56deab07SRod Evans 	elf_get_def_dirs,
101*56deab07SRod Evans 	elf_get_sec_dirs,
1027c478bd9Sstevel@tonic-gate 	elf_fix_name,
1037c478bd9Sstevel@tonic-gate 	elf_get_so,
1047c478bd9Sstevel@tonic-gate 	elf_dladdr,
105*56deab07SRod Evans 	dlsym_handle
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate 
108*56deab07SRod Evans /*
109*56deab07SRod Evans  * Default and secure dependency search paths.
110*56deab07SRod Evans  */
111*56deab07SRod Evans static Alist **
112*56deab07SRod Evans elf_get_def_dirs()
113*56deab07SRod Evans {
114*56deab07SRod Evans 	if (elf_def_dirs == NULL)
115*56deab07SRod Evans 		set_dirs(&elf_def_dirs, _elf_def_dirs, LA_SER_DEFAULT);
116*56deab07SRod Evans 	return (&elf_def_dirs);
117*56deab07SRod Evans }
118*56deab07SRod Evans 
119*56deab07SRod Evans static Alist **
120*56deab07SRod Evans elf_get_sec_dirs()
121*56deab07SRod Evans {
122*56deab07SRod Evans 	if (elf_sec_dirs == NULL)
123*56deab07SRod Evans 		set_dirs(&elf_sec_dirs, _elf_sec_dirs, LA_SER_SECURE);
124*56deab07SRod Evans 	return (&elf_sec_dirs);
125*56deab07SRod Evans }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Redefine NEEDED name if necessary.
1297c478bd9Sstevel@tonic-gate  */
130*56deab07SRod Evans static int
131*56deab07SRod Evans elf_fix_name(const char *name, Rt_map *clmp, Alist **alpp, Aliste alni,
132*56deab07SRod Evans     uint_t orig)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	/*
1357c478bd9Sstevel@tonic-gate 	 * For ABI compliance, if we are asked for ld.so.1, then really give
1367c478bd9Sstevel@tonic-gate 	 * them libsys.so.1 (the SONAME of libsys.so.1 is ld.so.1).
1377c478bd9Sstevel@tonic-gate 	 */
1387c478bd9Sstevel@tonic-gate 	if (((*name == '/') &&
139a953e2b1Srie 	/* BEGIN CSTYLED */
1407c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
1417c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD_64)) == 0)) ||
1427c478bd9Sstevel@tonic-gate #else
1437c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_PTH_RTLD)) == 0)) ||
1447c478bd9Sstevel@tonic-gate #endif
1457c478bd9Sstevel@tonic-gate 	    (strcmp(name, MSG_ORIG(MSG_FIL_RTLD)) == 0)) {
146a953e2b1Srie 		/* END CSTYLED */
147*56deab07SRod Evans 		Pdesc	*pdp;
1487c478bd9Sstevel@tonic-gate 
1495aefb655Srie 		DBG_CALL(Dbg_file_fixname(LIST(clmp), name,
1505aefb655Srie 		    MSG_ORIG(MSG_PTH_LIBSYS)));
151*56deab07SRod Evans 		if ((pdp = alist_append(alpp, 0, sizeof (Pdesc), alni)) == NULL)
1527c478bd9Sstevel@tonic-gate 			return (0);
153*56deab07SRod Evans 
154*56deab07SRod Evans 		pdp->pd_pname = (char *)MSG_ORIG(MSG_PTH_LIBSYS);
155*56deab07SRod Evans 		pdp->pd_plen = MSG_PTH_LIBSYS_SIZE;
156*56deab07SRod Evans 		pdp->pd_flags = PD_FLG_PNSLASH;
157*56deab07SRod Evans 
158*56deab07SRod Evans 		return (1);
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
161*56deab07SRod Evans 	return (expand_paths(clmp, name, alpp, alni, orig, 0));
162*56deab07SRod Evans }
163*56deab07SRod Evans 
164*56deab07SRod Evans /*
165*56deab07SRod Evans  * Determine whether this object requires any hardware or software capabilities.
166*56deab07SRod Evans  */
167*56deab07SRod Evans static int
168*56deab07SRod Evans elf_cap_check(Fdesc *fdp, Ehdr *ehdr, Rej_desc *rej)
169*56deab07SRod Evans {
170*56deab07SRod Evans 	Phdr	*phdr;
171*56deab07SRod Evans 	int	cnt;
172*56deab07SRod Evans 
173*56deab07SRod Evans 	/* LINTED */
174*56deab07SRod Evans 	phdr = (Phdr *)((char *)ehdr + ehdr->e_phoff);
175*56deab07SRod Evans 	for (cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) {
176*56deab07SRod Evans 		Cap	*cptr;
177*56deab07SRod Evans 
178*56deab07SRod Evans 		if (phdr->p_type != PT_SUNWCAP)
179*56deab07SRod Evans 			continue;
180*56deab07SRod Evans 
181*56deab07SRod Evans 		/* LINTED */
182*56deab07SRod Evans 		cptr = (Cap *)((char *)ehdr + phdr->p_offset);
183*56deab07SRod Evans 		while (cptr->c_tag != CA_SUNW_NULL) {
184*56deab07SRod Evans 			if (cptr->c_tag == CA_SUNW_HW_1) {
185*56deab07SRod Evans 				/*
186*56deab07SRod Evans 				 * Verify the hardware capabilities.
187*56deab07SRod Evans 				 */
188*56deab07SRod Evans 				if (hwcap_check(cptr->c_un.c_val, rej) == 0)
189*56deab07SRod Evans 					return (0);
190*56deab07SRod Evans 
191*56deab07SRod Evans 				/*
192*56deab07SRod Evans 				 * Retain this hardware capabilities value for
193*56deab07SRod Evans 				 * possible later inspection should this object
194*56deab07SRod Evans 				 * be processed as a filtee.
195*56deab07SRod Evans 				 */
196*56deab07SRod Evans 				fdp->fd_hwcap = cptr->c_un.c_val;
197*56deab07SRod Evans 			}
198*56deab07SRod Evans 			if (cptr->c_tag == CA_SUNW_SF_1) {
199*56deab07SRod Evans 				/*
200*56deab07SRod Evans 				 * Verify the software capabilities.
201*56deab07SRod Evans 				 */
202*56deab07SRod Evans 				if (sfcap_check(cptr->c_un.c_val, rej) == 0)
203*56deab07SRod Evans 					return (0);
204*56deab07SRod Evans 			}
205*56deab07SRod Evans 			cptr++;
206*56deab07SRod Evans 		}
207*56deab07SRod Evans 	}
208*56deab07SRod Evans 	return (1);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /*
2127c478bd9Sstevel@tonic-gate  * Determine if we have been given an ELF file and if so determine if the file
2137c478bd9Sstevel@tonic-gate  * is compatible.  Returns 1 if true, else 0 and sets the reject descriptor
2147c478bd9Sstevel@tonic-gate  * with associated error information.
2157c478bd9Sstevel@tonic-gate  */
216*56deab07SRod Evans Fct *
217*56deab07SRod Evans elf_verify(caddr_t addr, size_t size, Fdesc *fdp, const char *name,
218*56deab07SRod Evans     Rej_desc *rej)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	Ehdr	*ehdr;
221*56deab07SRod Evans 	char	*caddr = (char *)addr;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	/*
2247c478bd9Sstevel@tonic-gate 	 * Determine if we're an elf file.  If not simply return, we don't set
2257c478bd9Sstevel@tonic-gate 	 * any rejection information as this test allows use to scroll through
2267c478bd9Sstevel@tonic-gate 	 * the objects we support (ELF, AOUT).
2277c478bd9Sstevel@tonic-gate 	 */
228*56deab07SRod Evans 	if (size < sizeof (Ehdr) ||
229*56deab07SRod Evans 	    caddr[EI_MAG0] != ELFMAG0 ||
230*56deab07SRod Evans 	    caddr[EI_MAG1] != ELFMAG1 ||
231*56deab07SRod Evans 	    caddr[EI_MAG2] != ELFMAG2 ||
232*56deab07SRod Evans 	    caddr[EI_MAG3] != ELFMAG3) {
233*56deab07SRod Evans 		return (NULL);
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Check class and encoding.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	/* LINTED */
240*56deab07SRod Evans 	ehdr = (Ehdr *)addr;
2417c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_CLASS] != M_CLASS) {
2427c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_CLASS;
2437c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_CLASS];
244*56deab07SRod Evans 		return (NULL);
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_DATA] != M_DATA) {
2477c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
2487c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
249*56deab07SRod Evans 		return (NULL);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC) &&
2527c478bd9Sstevel@tonic-gate 	    (ehdr->e_type != ET_DYN)) {
2537c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_TYPE;
2547c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_type;
255*56deab07SRod Evans 		return (NULL);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
259*56deab07SRod Evans 	 * Verify ELF version.
2607c478bd9Sstevel@tonic-gate 	 */
2617c478bd9Sstevel@tonic-gate 	if (ehdr->e_version > EV_CURRENT) {
2627c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
2637c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
264*56deab07SRod Evans 		return (NULL);
2657c478bd9Sstevel@tonic-gate 	}
266*56deab07SRod Evans 
267*56deab07SRod Evans 	/*
268*56deab07SRod Evans 	 * Verify machine specific flags.
269*56deab07SRod Evans 	 */
270*56deab07SRod Evans 	if (elf_mach_flags_check(rej, ehdr) == 0)
271*56deab07SRod Evans 		return (NULL);
272*56deab07SRod Evans 
273*56deab07SRod Evans 	/*
274*56deab07SRod Evans 	 * Verify any hardware/software capability requirements.  Note, if this
275*56deab07SRod Evans 	 * object is an explicitly defined shared object under inspection by
276*56deab07SRod Evans 	 * ldd(1), and contains an incompatible hardware capabilities
277*56deab07SRod Evans 	 * requirement, then inform the user, but continue processing.
278*56deab07SRod Evans 	 */
279*56deab07SRod Evans 	if (elf_cap_check(fdp, ehdr, rej) == 0) {
280*56deab07SRod Evans 		Rt_map	*lmp = lml_main.lm_head;
281*56deab07SRod Evans 
282*56deab07SRod Evans 		if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) &&
283*56deab07SRod Evans 		    (lmp != NULL) && (FLAGS1(lmp) & FL1_RT_LDDSTUB) &&
284*56deab07SRod Evans 		    (NEXT(lmp) == NULL)) {
285*56deab07SRod Evans 			const char	*fmt;
286*56deab07SRod Evans 
287*56deab07SRod Evans 			if (rej->rej_type == SGS_REJ_HWCAP_1)
288*56deab07SRod Evans 				fmt = MSG_INTL(MSG_LDD_GEN_HWCAP_1);
289*56deab07SRod Evans 			else
290*56deab07SRod Evans 				fmt = MSG_INTL(MSG_LDD_GEN_SFCAP_1);
291*56deab07SRod Evans 			(void) printf(fmt, name, rej->rej_str);
292*56deab07SRod Evans 			return (&elf_fct);
293*56deab07SRod Evans 		}
294*56deab07SRod Evans 		return (NULL);
295*56deab07SRod Evans 	}
296*56deab07SRod Evans 	return (&elf_fct);
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * The runtime linker employs lazy loading to provide the libraries needed for
3017c478bd9Sstevel@tonic-gate  * debugging, preloading .o's and dldump().  As these are seldom used, the
3027c478bd9Sstevel@tonic-gate  * standard startup of ld.so.1 doesn't initialize all the information necessary
3037c478bd9Sstevel@tonic-gate  * to perform plt relocation on ld.so.1's link-map.  The first time lazy loading
3047c478bd9Sstevel@tonic-gate  * is called we get here to perform these initializations:
3057c478bd9Sstevel@tonic-gate  *
3067c478bd9Sstevel@tonic-gate  *  o	elf_needed() is called to set up the DYNINFO() indexes for each lazy
3077c478bd9Sstevel@tonic-gate  *	dependency.  Typically, for all other objects, this is called during
3087c478bd9Sstevel@tonic-gate  *	analyze_so(), but as ld.so.1 is set-contained we skip this processing.
3097c478bd9Sstevel@tonic-gate  *
3107c478bd9Sstevel@tonic-gate  *  o	For intel, ld.so.1's JMPSLOT relocations need relative updates. These
3117c478bd9Sstevel@tonic-gate  *	are by default skipped thus delaying all relative relocation processing
3127c478bd9Sstevel@tonic-gate  * 	on every invocation of ld.so.1.
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate int
3157c478bd9Sstevel@tonic-gate elf_rtld_load()
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	Lm_list	*lml = &lml_rtld;
3187c478bd9Sstevel@tonic-gate 	Rt_map	*lmp = lml->lm_head;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_PLTREL)
3217c478bd9Sstevel@tonic-gate 		return (1);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/*
3247c478bd9Sstevel@tonic-gate 	 * As we need to refer to the DYNINFO() information, insure that it has
3257c478bd9Sstevel@tonic-gate 	 * been initialized.
3267c478bd9Sstevel@tonic-gate 	 */
3279aa23310Srie 	if (elf_needed(lml, ALIST_OFF_DATA, lmp, NULL) == 0)
3287c478bd9Sstevel@tonic-gate 		return (0);
3297c478bd9Sstevel@tonic-gate 
33002ca3e02Srie #if	defined(__i386)
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * This is a kludge to give ld.so.1 a performance benefit on i386.
3337c478bd9Sstevel@tonic-gate 	 * It's based around two factors.
3347c478bd9Sstevel@tonic-gate 	 *
3357c478bd9Sstevel@tonic-gate 	 *  o	JMPSLOT relocations (PLT's) actually need a relative relocation
3367c478bd9Sstevel@tonic-gate 	 *	applied to the GOT entry so that they can find PLT0.
3377c478bd9Sstevel@tonic-gate 	 *
3387c478bd9Sstevel@tonic-gate 	 *  o	ld.so.1 does not exercise *any* PLT's before it has made a call
3397c478bd9Sstevel@tonic-gate 	 *	to elf_lazy_load().  This is because all dynamic dependencies
3407c478bd9Sstevel@tonic-gate 	 * 	are recorded as lazy dependencies.
3417c478bd9Sstevel@tonic-gate 	 */
342*56deab07SRod Evans 	(void) elf_reloc_relative_count((ulong_t)JMPREL(lmp),
3437c478bd9Sstevel@tonic-gate 	    (ulong_t)(PLTRELSZ(lmp) / RELENT(lmp)), (ulong_t)RELENT(lmp),
344*56deab07SRod Evans 	    (ulong_t)ADDR(lmp), lmp, NULL);
3457c478bd9Sstevel@tonic-gate #endif
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	lml->lm_flags |= LML_FLG_PLTREL;
3487c478bd9Sstevel@tonic-gate 	return (1);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * Lazy load an object.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate Rt_map *
3559aa23310Srie elf_lazy_load(Rt_map *clmp, Slookup *slp, uint_t ndx, const char *sym,
3569aa23310Srie     int *in_nfavl)
3577c478bd9Sstevel@tonic-gate {
358*56deab07SRod Evans 	Alist		*palp = NULL;
3597c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp, *hlmp;
36075e7992aSrie 	Dyninfo		*dip = &DYNINFO(clmp)[ndx], *pdip;
3617c478bd9Sstevel@tonic-gate 	uint_t		flags = 0;
3627c478bd9Sstevel@tonic-gate 	const char	*name;
3637c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
3647c478bd9Sstevel@tonic-gate 	Lm_cntl		*lmc;
3657c478bd9Sstevel@tonic-gate 	Aliste		lmco;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * If this dependency has already been processed, we're done.
3697c478bd9Sstevel@tonic-gate 	 */
370*56deab07SRod Evans 	if (((nlmp = (Rt_map *)dip->di_info) != NULL) ||
37175e7992aSrie 	    (dip->di_flags & FLG_DI_LDD_DONE))
3727c478bd9Sstevel@tonic-gate 		return (nlmp);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
37575e7992aSrie 	 * If we're running under ldd(1), indicate that this dependency has been
37675e7992aSrie 	 * processed (see test above).  It doesn't matter whether the object is
37775e7992aSrie 	 * successfully loaded or not, this flag simply ensures that we don't
37875e7992aSrie 	 * repeatedly attempt to load an object that has already failed to load.
37975e7992aSrie 	 * To do so would create multiple failure diagnostics for the same
38075e7992aSrie 	 * object under ldd(1).
38175e7992aSrie 	 */
38275e7992aSrie 	if (lml->lm_flags & LML_FLG_TRC_ENABLE)
38375e7992aSrie 		dip->di_flags |= FLG_DI_LDD_DONE;
38475e7992aSrie 
38575e7992aSrie 	/*
38675e7992aSrie 	 * Determine the initial dependency name.
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 	name = STRTAB(clmp) + DYN(clmp)[ndx].d_un.d_val;
3895aefb655Srie 	DBG_CALL(Dbg_file_lazyload(clmp, name, sym));
3907c478bd9Sstevel@tonic-gate 
39175e7992aSrie 	/*
39275e7992aSrie 	 * If this object needs to establish its own group, make sure a handle
39375e7992aSrie 	 * is created.
39475e7992aSrie 	 */
3957c478bd9Sstevel@tonic-gate 	if (dip->di_flags & FLG_DI_GROUP)
3967c478bd9Sstevel@tonic-gate 		flags |= (FLG_RT_SETGROUP | FLG_RT_HANDLE);
3977c478bd9Sstevel@tonic-gate 
39875e7992aSrie 	/*
39975e7992aSrie 	 * Lazy dependencies are identified as DT_NEEDED entries with a
40075e7992aSrie 	 * DF_P1_LAZYLOAD flag in the previous DT_POSFLAG_1 element.  The
40175e7992aSrie 	 * dynamic information element that corresponds to the DT_POSFLAG_1
40275e7992aSrie 	 * entry is free, and thus used to store the present entrance
40375e7992aSrie 	 * identifier.  This identifier is used to prevent multiple attempts to
40475e7992aSrie 	 * load a failed lazy loadable dependency within the same runtime linker
40575e7992aSrie 	 * operation.  However, future attempts to reload this dependency are
40675e7992aSrie 	 * still possible.
40775e7992aSrie 	 */
40875e7992aSrie 	if (ndx && (pdip = dip - 1) && (pdip->di_flags & FLG_DI_POSFLAG1))
40975e7992aSrie 		pdip->di_info = (void *)slp->sl_id;
41075e7992aSrie 
4117c478bd9Sstevel@tonic-gate 	/*
4127c478bd9Sstevel@tonic-gate 	 * Expand the requested name if necessary.
4137c478bd9Sstevel@tonic-gate 	 */
414*56deab07SRod Evans 	if (elf_fix_name(name, clmp, &palp, AL_CNT_NEEDED, 0) == 0)
415*56deab07SRod Evans 		return (NULL);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * Provided the object on the head of the link-map has completed its
4197c478bd9Sstevel@tonic-gate 	 * relocation, create a new link-map control list for this request.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	hlmp = lml->lm_head;
4227c478bd9Sstevel@tonic-gate 	if (FLAGS(hlmp) & FLG_RT_RELOCED) {
423cce0e03bSab 		if ((lmc = alist_append(&lml->lm_lists, 0, sizeof (Lm_cntl),
424*56deab07SRod Evans 		    AL_CNT_LMLISTS)) == NULL) {
425*56deab07SRod Evans 			remove_plist(&palp, 1);
426*56deab07SRod Evans 			return (NULL);
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 		lmco = (Aliste)((char *)lmc - (char *)lml->lm_lists);
4297c478bd9Sstevel@tonic-gate 	} else {
4307c478bd9Sstevel@tonic-gate 		lmc = 0;
431cce0e03bSab 		lmco = ALIST_OFF_DATA;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	/*
4357c478bd9Sstevel@tonic-gate 	 * Load the associated object.
4367c478bd9Sstevel@tonic-gate 	 */
4377c478bd9Sstevel@tonic-gate 	dip->di_info = nlmp =
438*56deab07SRod Evans 	    load_one(lml, lmco, palp, clmp, MODE(clmp), flags, 0, in_nfavl);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * Remove any expanded pathname infrastructure.  Reduce the pending lazy
4427c478bd9Sstevel@tonic-gate 	 * dependency count of the caller, together with the link-map lists
4437c478bd9Sstevel@tonic-gate 	 * count of objects that still have lazy dependencies pending.
4447c478bd9Sstevel@tonic-gate 	 */
445*56deab07SRod Evans 	remove_plist(&palp, 1);
4467c478bd9Sstevel@tonic-gate 	if (--LAZY(clmp) == 0)
4477c478bd9Sstevel@tonic-gate 		LIST(clmp)->lm_lazy--;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/*
45002ca3e02Srie 	 * Finish processing the objects associated with this request, and
45102ca3e02Srie 	 * create an association between the caller and this dependency.
4527c478bd9Sstevel@tonic-gate 	 */
45375e7992aSrie 	if (nlmp && ((bind_one(clmp, nlmp, BND_NEEDED) == 0) ||
454*56deab07SRod Evans 	    ((nlmp = analyze_lmc(lml, lmco, nlmp, in_nfavl)) == NULL) ||
4559aa23310Srie 	    (relocate_lmc(lml, lmco, clmp, nlmp, in_nfavl) == 0)))
456*56deab07SRod Evans 		dip->di_info = nlmp = NULL;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	/*
45902ca3e02Srie 	 * If this lazyload has failed, and we've created a new link-map
46002ca3e02Srie 	 * control list to which this request has added objects, then remove
46102ca3e02Srie 	 * all the objects that have been associated to this request.
4627c478bd9Sstevel@tonic-gate 	 */
463*56deab07SRod Evans 	if ((nlmp == NULL) && lmc && lmc->lc_head)
46402ca3e02Srie 		remove_lmc(lml, clmp, lmc, lmco, name);
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/*
46702ca3e02Srie 	 * Finally, remove any link-map control list that was created.
4687c478bd9Sstevel@tonic-gate 	 */
46902ca3e02Srie 	if (lmc)
4707c478bd9Sstevel@tonic-gate 		remove_cntl(lml, lmco);
4717c478bd9Sstevel@tonic-gate 
47275e7992aSrie 	/*
47375e7992aSrie 	 * If this lazy loading failed, record the fact, and bump the lazy
47475e7992aSrie 	 * counts.
47575e7992aSrie 	 */
476*56deab07SRod Evans 	if (nlmp == NULL) {
47775e7992aSrie 		dip->di_flags |= FLG_DI_LAZYFAIL;
47875e7992aSrie 		if (LAZY(clmp)++ == 0)
47975e7992aSrie 			LIST(clmp)->lm_lazy++;
48075e7992aSrie 	}
48175e7992aSrie 
4827c478bd9Sstevel@tonic-gate 	return (nlmp);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate /*
4867c478bd9Sstevel@tonic-gate  * Return the entry point of the ELF executable.
4877c478bd9Sstevel@tonic-gate  */
488*56deab07SRod Evans static Addr
489*56deab07SRod Evans elf_entry_point(void)
4907c478bd9Sstevel@tonic-gate {
491*56deab07SRod Evans 	Rt_map	*lmp = lml_main.lm_head;
492*56deab07SRod Evans 	Ehdr	*ehdr = (Ehdr *)ADDR(lmp);
493*56deab07SRod Evans 	Addr	addr = (Addr)(ehdr->e_entry);
4947c478bd9Sstevel@tonic-gate 
495*56deab07SRod Evans 	if ((FLAGS(lmp) & FLG_RT_FIXED) == 0)
496*56deab07SRod Evans 		addr += ADDR(lmp);
4977c478bd9Sstevel@tonic-gate 
498*56deab07SRod Evans 	return (addr);
4997c478bd9Sstevel@tonic-gate }
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate /*
5027c478bd9Sstevel@tonic-gate  * Determine if a dependency requires a particular version and if so verify
5037c478bd9Sstevel@tonic-gate  * that the version exists in the dependency.
5047c478bd9Sstevel@tonic-gate  */
505*56deab07SRod Evans int
5067c478bd9Sstevel@tonic-gate elf_verify_vers(const char *name, Rt_map *clmp, Rt_map *nlmp)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	Verneed		*vnd = VERNEED(clmp);
5097c478bd9Sstevel@tonic-gate 	int		_num, num = VERNEEDNUM(clmp);
5107c478bd9Sstevel@tonic-gate 	char		*cstrs = (char *)STRTAB(clmp);
5117c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/*
5147c478bd9Sstevel@tonic-gate 	 * Traverse the callers version needed information and determine if any
5157c478bd9Sstevel@tonic-gate 	 * specific versions are required from the dependency.
5167c478bd9Sstevel@tonic-gate 	 */
5175aefb655Srie 	DBG_CALL(Dbg_ver_need_title(LIST(clmp), NAME(clmp)));
5187c478bd9Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
5197c478bd9Sstevel@tonic-gate 	    vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) {
5207c478bd9Sstevel@tonic-gate 		Half		cnt = vnd->vn_cnt;
5217c478bd9Sstevel@tonic-gate 		Vernaux		*vnap;
5227c478bd9Sstevel@tonic-gate 		char		*nstrs, *need;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		/*
5257c478bd9Sstevel@tonic-gate 		 * Determine if a needed entry matches this dependency.
5267c478bd9Sstevel@tonic-gate 		 */
5277c478bd9Sstevel@tonic-gate 		need = (char *)(cstrs + vnd->vn_file);
5287c478bd9Sstevel@tonic-gate 		if (strcmp(name, need) != 0)
5297c478bd9Sstevel@tonic-gate 			continue;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
5327c478bd9Sstevel@tonic-gate 		    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
5337c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_VER_FIND), name);
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		/*
5367c478bd9Sstevel@tonic-gate 		 * Validate that each version required actually exists in the
5377c478bd9Sstevel@tonic-gate 		 * dependency.
5387c478bd9Sstevel@tonic-gate 		 */
5397c478bd9Sstevel@tonic-gate 		nstrs = (char *)STRTAB(nlmp);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 		for (vnap = (Vernaux *)((Xword)vnd + vnd->vn_aux); cnt;
5427c478bd9Sstevel@tonic-gate 		    cnt--, vnap = (Vernaux *)((Xword)vnap + vnap->vna_next)) {
5437c478bd9Sstevel@tonic-gate 			char		*version, *define;
5447c478bd9Sstevel@tonic-gate 			Verdef		*vdf = VERDEF(nlmp);
5457c478bd9Sstevel@tonic-gate 			ulong_t		_num, num = VERDEFNUM(nlmp);
5467c478bd9Sstevel@tonic-gate 			int		found = 0;
5477c478bd9Sstevel@tonic-gate 
548090a8d9eSAli Bahrami 			/*
549090a8d9eSAli Bahrami 			 * Skip validation of versions that are marked
550090a8d9eSAli Bahrami 			 * INFO. This optimization is used for versions
551090a8d9eSAli Bahrami 			 * that are inherited by another version. Verification
552090a8d9eSAli Bahrami 			 * of the inheriting version is sufficient.
553090a8d9eSAli Bahrami 			 *
554090a8d9eSAli Bahrami 			 * Such versions are recorded in the object for the
555090a8d9eSAli Bahrami 			 * benefit of VERSYM entries that refer to them. This
556090a8d9eSAli Bahrami 			 * provides a purely diagnositic benefit.
557090a8d9eSAli Bahrami 			 */
558090a8d9eSAli Bahrami 			if (vnap->vna_flags & VER_FLG_INFO)
559090a8d9eSAli Bahrami 				continue;
560090a8d9eSAli Bahrami 
5617c478bd9Sstevel@tonic-gate 			version = (char *)(cstrs + vnap->vna_name);
5625aefb655Srie 			DBG_CALL(Dbg_ver_need_entry(lml, 0, need, version));
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 			for (_num = 1; _num <= num; _num++,
5657c478bd9Sstevel@tonic-gate 			    vdf = (Verdef *)((Xword)vdf + vdf->vd_next)) {
5667c478bd9Sstevel@tonic-gate 				Verdaux		*vdap;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 				if (vnap->vna_hash != vdf->vd_hash)
5697c478bd9Sstevel@tonic-gate 					continue;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 				vdap = (Verdaux *)((Xword)vdf + vdf->vd_aux);
5727c478bd9Sstevel@tonic-gate 				define = (char *)(nstrs + vdap->vda_name);
5737c478bd9Sstevel@tonic-gate 				if (strcmp(version, define) != 0)
5747c478bd9Sstevel@tonic-gate 					continue;
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 				found++;
5777c478bd9Sstevel@tonic-gate 				break;
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 			/*
5817c478bd9Sstevel@tonic-gate 			 * If we're being traced print out any matched version
5827c478bd9Sstevel@tonic-gate 			 * when the verbose (-v) option is in effect.  Always
5837c478bd9Sstevel@tonic-gate 			 * print any unmatched versions.
5847c478bd9Sstevel@tonic-gate 			 */
5857c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
586a953e2b1Srie 				/* BEGIN CSTYLED */
5877c478bd9Sstevel@tonic-gate 				if (found) {
5887c478bd9Sstevel@tonic-gate 				    if (!(lml->lm_flags & LML_FLG_TRC_VERBOSE))
5897c478bd9Sstevel@tonic-gate 					continue;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_VER_FOUND),
5927c478bd9Sstevel@tonic-gate 					need, version, NAME(nlmp));
5937c478bd9Sstevel@tonic-gate 				} else {
5947c478bd9Sstevel@tonic-gate 				    if (rtld_flags & RT_FL_SILENCERR)
5957c478bd9Sstevel@tonic-gate 					continue;
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_VER_NFOUND),
5987c478bd9Sstevel@tonic-gate 					need, version);
5997c478bd9Sstevel@tonic-gate 				}
600a953e2b1Srie 				/* END CSTYLED */
6017c478bd9Sstevel@tonic-gate 				continue;
6027c478bd9Sstevel@tonic-gate 			}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 			/*
6057c478bd9Sstevel@tonic-gate 			 * If the version hasn't been found then this is a
6067c478bd9Sstevel@tonic-gate 			 * candidate for a fatal error condition.  Weak
6077c478bd9Sstevel@tonic-gate 			 * version definition requirements are silently
6087c478bd9Sstevel@tonic-gate 			 * ignored.  Also, if the image inspected for a version
6097c478bd9Sstevel@tonic-gate 			 * definition has no versioning recorded at all then
6107c478bd9Sstevel@tonic-gate 			 * silently ignore this (this provides better backward
6117c478bd9Sstevel@tonic-gate 			 * compatibility to old images created prior to
6127c478bd9Sstevel@tonic-gate 			 * versioning being available).  Both of these skipped
6137c478bd9Sstevel@tonic-gate 			 * diagnostics are available under tracing (see above).
6147c478bd9Sstevel@tonic-gate 			 */
6157c478bd9Sstevel@tonic-gate 			if ((found == 0) && (num != 0) &&
6167c478bd9Sstevel@tonic-gate 			    (!(vnap->vna_flags & VER_FLG_WEAK))) {
6175aefb655Srie 				eprintf(lml, ERR_FATAL,
6185aefb655Srie 				    MSG_INTL(MSG_VER_NFOUND), need, version,
6195aefb655Srie 				    NAME(clmp));
6207c478bd9Sstevel@tonic-gate 				return (0);
6217c478bd9Sstevel@tonic-gate 			}
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 	}
6245aefb655Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
6257c478bd9Sstevel@tonic-gate 	return (1);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate  * Search through the dynamic section for DT_NEEDED entries and perform one
6307c478bd9Sstevel@tonic-gate  * of two functions.  If only the first argument is specified then load the
6317c478bd9Sstevel@tonic-gate  * defined shared object, otherwise add the link map representing the defined
6327c478bd9Sstevel@tonic-gate  * link map the the dlopen list.
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate static int
6359aa23310Srie elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp, int *in_nfavl)
6367c478bd9Sstevel@tonic-gate {
637*56deab07SRod Evans 	Alist		*palp = NULL;
63875e7992aSrie 	Dyn		*dyn, *pdyn;
6397c478bd9Sstevel@tonic-gate 	ulong_t		ndx = 0;
64075e7992aSrie 	uint_t		lazy, flags;
6417c478bd9Sstevel@tonic-gate 	Word		lmflags = lml->lm_flags;
6427c478bd9Sstevel@tonic-gate 	Word		lmtflags = lml->lm_tflags;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	/*
6457c478bd9Sstevel@tonic-gate 	 * Process each shared object on needed list.
6467c478bd9Sstevel@tonic-gate 	 */
6477c478bd9Sstevel@tonic-gate 	if (DYN(clmp) == 0)
6487c478bd9Sstevel@tonic-gate 		return (1);
6497c478bd9Sstevel@tonic-gate 
65075e7992aSrie 	for (dyn = (Dyn *)DYN(clmp), pdyn = NULL; dyn->d_tag != DT_NULL;
65175e7992aSrie 	    pdyn = dyn++, ndx++) {
6527c478bd9Sstevel@tonic-gate 		Dyninfo	*dip = &DYNINFO(clmp)[ndx];
6537c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
6547c478bd9Sstevel@tonic-gate 		char	*name;
6557c478bd9Sstevel@tonic-gate 		int	silent = 0;
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		switch (dyn->d_tag) {
6587c478bd9Sstevel@tonic-gate 		case DT_POSFLAG_1:
65975e7992aSrie 			dip->di_flags |= FLG_DI_POSFLAG1;
6607c478bd9Sstevel@tonic-gate 			continue;
6617c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
6627c478bd9Sstevel@tonic-gate 		case DT_USED:
66375e7992aSrie 			lazy = flags = 0;
6647c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_NEEDED;
66575e7992aSrie 
66675e7992aSrie 			if (pdyn && (pdyn->d_tag == DT_POSFLAG_1)) {
66775e7992aSrie 				if ((pdyn->d_un.d_val & DF_P1_LAZYLOAD) &&
66875e7992aSrie 				    ((lmtflags & LML_TFLG_NOLAZYLD) == 0)) {
66975e7992aSrie 					dip->di_flags |= FLG_DI_LAZY;
67075e7992aSrie 					lazy = 1;
67175e7992aSrie 				}
67275e7992aSrie 				if (pdyn->d_un.d_val & DF_P1_GROUPPERM) {
67375e7992aSrie 					dip->di_flags |= FLG_DI_GROUP;
67475e7992aSrie 					flags =
67575e7992aSrie 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
67675e7992aSrie 				}
67775e7992aSrie 			}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 			name = (char *)STRTAB(clmp) + dyn->d_un.d_val;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 			/*
6827c478bd9Sstevel@tonic-gate 			 * NOTE, libc.so.1 can't be lazy loaded.  Although a
6837c478bd9Sstevel@tonic-gate 			 * lazy position flag won't be produced when a RTLDINFO
6847c478bd9Sstevel@tonic-gate 			 * .dynamic entry is found (introduced with the UPM in
6857c478bd9Sstevel@tonic-gate 			 * Solaris 10), it was possible to mark libc for lazy
6867c478bd9Sstevel@tonic-gate 			 * loading on previous releases.  To reduce the overhead
6877c478bd9Sstevel@tonic-gate 			 * of testing for this occurrence, only carry out this
6887c478bd9Sstevel@tonic-gate 			 * check for the first object on the link-map list
6897c478bd9Sstevel@tonic-gate 			 * (there aren't many applications built without libc).
6907c478bd9Sstevel@tonic-gate 			 */
6917c478bd9Sstevel@tonic-gate 			if (lazy && (lml->lm_head == clmp) &&
6927c478bd9Sstevel@tonic-gate 			    (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0))
6937c478bd9Sstevel@tonic-gate 				lazy = 0;
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 			/*
6967c478bd9Sstevel@tonic-gate 			 * Don't bring in lazy loaded objects yet unless we've
6977c478bd9Sstevel@tonic-gate 			 * been asked to attempt to load all available objects
6987c478bd9Sstevel@tonic-gate 			 * (crle(1) sets LD_FLAGS=loadavail).  Even under
6997c478bd9Sstevel@tonic-gate 			 * RTLD_NOW we don't process this - RTLD_NOW will cause
7007c478bd9Sstevel@tonic-gate 			 * relocation processing which in turn might trigger
7017c478bd9Sstevel@tonic-gate 			 * lazy loading, but its possible that the object has a
7027c478bd9Sstevel@tonic-gate 			 * lazy loaded file with no bindings (i.e., it should
7037c478bd9Sstevel@tonic-gate 			 * never have been a dependency in the first place).
7047c478bd9Sstevel@tonic-gate 			 */
7057c478bd9Sstevel@tonic-gate 			if (lazy) {
7067c478bd9Sstevel@tonic-gate 				if ((lmflags & LML_FLG_LOADAVAIL) == 0) {
7077c478bd9Sstevel@tonic-gate 					LAZY(clmp)++;
7087c478bd9Sstevel@tonic-gate 					lazy = flags = 0;
7097c478bd9Sstevel@tonic-gate 					continue;
7107c478bd9Sstevel@tonic-gate 				}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 				/*
7137c478bd9Sstevel@tonic-gate 				 * Silence any error messages - see description
7147c478bd9Sstevel@tonic-gate 				 * under elf_lookup_filtee().
7157c478bd9Sstevel@tonic-gate 				 */
7167c478bd9Sstevel@tonic-gate 				if ((rtld_flags & RT_FL_SILENCERR) == 0) {
7177c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_SILENCERR;
7187c478bd9Sstevel@tonic-gate 					silent = 1;
7197c478bd9Sstevel@tonic-gate 				}
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 			break;
7227c478bd9Sstevel@tonic-gate 		case DT_AUXILIARY:
7237c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_AUXFLTR;
7247c478bd9Sstevel@tonic-gate 			continue;
7257c478bd9Sstevel@tonic-gate 		case DT_SUNW_AUXILIARY:
7267c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR);
7277c478bd9Sstevel@tonic-gate 			continue;
7287c478bd9Sstevel@tonic-gate 		case DT_FILTER:
7297c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_STDFLTR;
7307c478bd9Sstevel@tonic-gate 			continue;
7317c478bd9Sstevel@tonic-gate 		case DT_SUNW_FILTER:
7327c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR);
7337c478bd9Sstevel@tonic-gate 			continue;
7347c478bd9Sstevel@tonic-gate 		default:
7357c478bd9Sstevel@tonic-gate 			continue;
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 
7385aefb655Srie 		DBG_CALL(Dbg_file_needed(clmp, name));
73975e7992aSrie 
74075e7992aSrie 		/*
74175e7992aSrie 		 * If we're running under ldd(1), indicate that this dependency
74275e7992aSrie 		 * has been processed.  It doesn't matter whether the object is
74375e7992aSrie 		 * successfully loaded or not, this flag simply ensures that we
74475e7992aSrie 		 * don't repeatedly attempt to load an object that has already
74575e7992aSrie 		 * failed to load.  To do so would create multiple failure
74675e7992aSrie 		 * diagnostics for the same object under ldd(1).
74775e7992aSrie 		 */
7487c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & LML_FLG_TRC_ENABLE)
74975e7992aSrie 			dip->di_flags |= FLG_DI_LDD_DONE;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		/*
7527c478bd9Sstevel@tonic-gate 		 * Establish the objects name, load it and establish a binding
7537c478bd9Sstevel@tonic-gate 		 * with the caller.
7547c478bd9Sstevel@tonic-gate 		 */
755*56deab07SRod Evans 		if ((elf_fix_name(name, clmp, &palp, AL_CNT_NEEDED, 0) == 0) ||
756*56deab07SRod Evans 		    ((nlmp = load_one(lml, lmco, palp, clmp, MODE(clmp),
757*56deab07SRod Evans 		    flags, 0, in_nfavl)) == NULL) ||
758*56deab07SRod Evans 		    (bind_one(clmp, nlmp, BND_NEEDED) == 0))
7597c478bd9Sstevel@tonic-gate 			nlmp = 0;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		/*
7627c478bd9Sstevel@tonic-gate 		 * Clean up any infrastructure, including the removal of the
7637c478bd9Sstevel@tonic-gate 		 * error suppression state, if it had been previously set in
7647c478bd9Sstevel@tonic-gate 		 * this routine.
7657c478bd9Sstevel@tonic-gate 		 */
766*56deab07SRod Evans 		remove_plist(&palp, 0);
767*56deab07SRod Evans 
7687c478bd9Sstevel@tonic-gate 		if (silent)
7697c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_SILENCERR;
77075e7992aSrie 
7717c478bd9Sstevel@tonic-gate 		if ((dip->di_info = (void *)nlmp) == 0) {
7727c478bd9Sstevel@tonic-gate 			/*
7737c478bd9Sstevel@tonic-gate 			 * If the object could not be mapped, continue if error
7747c478bd9Sstevel@tonic-gate 			 * suppression is established or we're here with ldd(1).
7757c478bd9Sstevel@tonic-gate 			 */
7767c478bd9Sstevel@tonic-gate 			if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags &
7777c478bd9Sstevel@tonic-gate 			    (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE)))
7787c478bd9Sstevel@tonic-gate 				continue;
779*56deab07SRod Evans 			else {
780*56deab07SRod Evans 				remove_plist(&palp, 1);
7817c478bd9Sstevel@tonic-gate 				return (0);
782*56deab07SRod Evans 			}
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	if (LAZY(clmp))
7877c478bd9Sstevel@tonic-gate 		lml->lm_lazy++;
7887c478bd9Sstevel@tonic-gate 
789*56deab07SRod Evans 	remove_plist(&palp, 1);
7907c478bd9Sstevel@tonic-gate 	return (1);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate /*
7947c478bd9Sstevel@tonic-gate  * A null symbol interpretor.  Used if a filter has no associated filtees.
7957c478bd9Sstevel@tonic-gate  */
7967c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
7977c478bd9Sstevel@tonic-gate static Sym *
7989aa23310Srie elf_null_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
7997c478bd9Sstevel@tonic-gate {
80037ffaf83SRod Evans 	return (NULL);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
8047c478bd9Sstevel@tonic-gate  * Disable filtee use.
8057c478bd9Sstevel@tonic-gate  */
8067c478bd9Sstevel@tonic-gate static void
8079a411307Srie elf_disable_filtee(Rt_map *lmp, Dyninfo *dip)
8087c478bd9Sstevel@tonic-gate {
8097c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) {
8107c478bd9Sstevel@tonic-gate 		/*
811*56deab07SRod Evans 		 * If this is an object filter, null out the reference name.
8127c478bd9Sstevel@tonic-gate 		 */
8137c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(lmp) != FLTR_DISABLED) {
81437ffaf83SRod Evans 			REFNAME(lmp) = NULL;
8157c478bd9Sstevel@tonic-gate 			OBJFLTRNDX(lmp) = FLTR_DISABLED;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 			/*
8187c478bd9Sstevel@tonic-gate 			 * Indicate that this filtee is no longer available.
8197c478bd9Sstevel@tonic-gate 			 */
8207c478bd9Sstevel@tonic-gate 			if (dip->di_flags & FLG_DI_STDFLTR)
8217c478bd9Sstevel@tonic-gate 				SYMINTP(lmp) = elf_null_find_sym;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 	} else if (dip->di_flags & FLG_DI_STDFLTR) {
8257c478bd9Sstevel@tonic-gate 		/*
8267c478bd9Sstevel@tonic-gate 		 * Indicate that this standard filtee is no longer available.
8277c478bd9Sstevel@tonic-gate 		 */
8287c478bd9Sstevel@tonic-gate 		if (SYMSFLTRCNT(lmp))
8297c478bd9Sstevel@tonic-gate 			SYMSFLTRCNT(lmp)--;
8307c478bd9Sstevel@tonic-gate 	} else {
8317c478bd9Sstevel@tonic-gate 		/*
8327c478bd9Sstevel@tonic-gate 		 * Indicate that this auxiliary filtee is no longer available.
8337c478bd9Sstevel@tonic-gate 		 */
8347c478bd9Sstevel@tonic-gate 		if (SYMAFLTRCNT(lmp))
8357c478bd9Sstevel@tonic-gate 			SYMAFLTRCNT(lmp)--;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 	dip->di_flags &= ~MSK_DI_FILTER;
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate /*
8417c478bd9Sstevel@tonic-gate  * Find symbol interpreter - filters.
8427c478bd9Sstevel@tonic-gate  * This function is called when the symbols from a shared object should
8437c478bd9Sstevel@tonic-gate  * be resolved from the shared objects filtees instead of from within itself.
8447c478bd9Sstevel@tonic-gate  *
8457c478bd9Sstevel@tonic-gate  * A symbol name of 0 is used to trigger filtee loading.
8467c478bd9Sstevel@tonic-gate  */
8477c478bd9Sstevel@tonic-gate static Sym *
8489aa23310Srie _elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx,
8499aa23310Srie     int *in_nfavl)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name, *filtees;
8527c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
8537c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
854*56deab07SRod Evans 	Pdesc		*pdp;
8557c478bd9Sstevel@tonic-gate 	int		any;
8567c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(ilmp)[ndx];
8577c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(ilmp);
858*56deab07SRod Evans 	Aliste		idx;
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate 	/*
8617c478bd9Sstevel@tonic-gate 	 * Indicate that the filter has been used.  If a binding already exists
8627c478bd9Sstevel@tonic-gate 	 * to the caller, indicate that this object is referenced.  This insures
8637c478bd9Sstevel@tonic-gate 	 * we don't generate false unreferenced diagnostics from ldd -u/U or
8647c478bd9Sstevel@tonic-gate 	 * debugging.  Don't create a binding regardless, as this filter may
8657c478bd9Sstevel@tonic-gate 	 * have been dlopen()'ed.
8667c478bd9Sstevel@tonic-gate 	 */
8677c478bd9Sstevel@tonic-gate 	if (name && (ilmp != clmp)) {
8687c478bd9Sstevel@tonic-gate 		Word	tracing = (LIST(clmp)->lm_flags &
8697c478bd9Sstevel@tonic-gate 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED));
8707c478bd9Sstevel@tonic-gate 
8715aefb655Srie 		if (tracing || DBG_ENABLED) {
872cce0e03bSab 			Bnd_desc 	*bdp;
873cce0e03bSab 			Aliste		idx;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 			FLAGS1(ilmp) |= FL1_RT_USED;
8767c478bd9Sstevel@tonic-gate 
8775aefb655Srie 			if ((tracing & LML_FLG_TRC_UNREF) || DBG_ENABLED) {
878cce0e03bSab 				for (APLIST_TRAVERSE(CALLERS(ilmp), idx, bdp)) {
8797c478bd9Sstevel@tonic-gate 					if (bdp->b_caller == clmp) {
8807c478bd9Sstevel@tonic-gate 						bdp->b_flags |= BND_REFER;
8817c478bd9Sstevel@tonic-gate 						break;
8827c478bd9Sstevel@tonic-gate 					}
8837c478bd9Sstevel@tonic-gate 				}
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 		}
8867c478bd9Sstevel@tonic-gate 	}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 	/*
8897c478bd9Sstevel@tonic-gate 	 * If this is the first call to process this filter, establish the
8907c478bd9Sstevel@tonic-gate 	 * filtee list.  If a configuration file exists, determine if any
8917c478bd9Sstevel@tonic-gate 	 * filtee associations for this filter, and its filtee reference, are
8927c478bd9Sstevel@tonic-gate 	 * defined.  Otherwise, process the filtee reference.  Any token
8937c478bd9Sstevel@tonic-gate 	 * expansion is also completed at this point (i.e., $PLATFORM).
8947c478bd9Sstevel@tonic-gate 	 */
8957c478bd9Sstevel@tonic-gate 	filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val;
8967c478bd9Sstevel@tonic-gate 	if (dip->di_info == 0) {
8977c478bd9Sstevel@tonic-gate 		if (rtld_flags2 & RT_FL2_FLTCFG)
898*56deab07SRod Evans 			elf_config_flt(lml, PATHNAME(ilmp), filtees,
899*56deab07SRod Evans 			    (Alist **)&dip->di_info, AL_CNT_FILTEES);
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 		if (dip->di_info == 0) {
9025aefb655Srie 			DBG_CALL(Dbg_file_filter(lml, NAME(ilmp), filtees, 0));
9037c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags &
9047c478bd9Sstevel@tonic-gate 			    (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
9057c478bd9Sstevel@tonic-gate 			    ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0))
9067c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FILTER),
9077c478bd9Sstevel@tonic-gate 				    NAME(ilmp), filtees);
9087c478bd9Sstevel@tonic-gate 
909*56deab07SRod Evans 			if (expand_paths(ilmp, filtees, (Alist **)&dip->di_info,
910*56deab07SRod Evans 			    AL_CNT_FILTEES, 0, 0) == 0) {
9117c478bd9Sstevel@tonic-gate 				elf_disable_filtee(ilmp, dip);
91237ffaf83SRod Evans 				return (NULL);
9137c478bd9Sstevel@tonic-gate 			}
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 	}
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/*
9187c478bd9Sstevel@tonic-gate 	 * Traverse the filtee list, dlopen()'ing any objects specified and
9197c478bd9Sstevel@tonic-gate 	 * using their group handle to lookup the symbol.
9207c478bd9Sstevel@tonic-gate 	 */
921*56deab07SRod Evans 	any = 0;
922*56deab07SRod Evans 	for (ALIST_TRAVERSE((Alist *)dip->di_info, idx, pdp)) {
9237c478bd9Sstevel@tonic-gate 		int	mode;
9247c478bd9Sstevel@tonic-gate 		Grp_hdl	*ghp;
9257c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp = 0;
9267c478bd9Sstevel@tonic-gate 
927*56deab07SRod Evans 		if (pdp->pd_plen == 0)
9287c478bd9Sstevel@tonic-gate 			continue;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		/*
9317c478bd9Sstevel@tonic-gate 		 * Establish the mode of the filtee from the filter.  As filtees
9327c478bd9Sstevel@tonic-gate 		 * are loaded via a dlopen(), make sure that RTLD_GROUP is set
9337c478bd9Sstevel@tonic-gate 		 * and the filtees aren't global.  It would be nice to have
9347c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST used here also, but as filters got out long before
9357c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST was introduced it's a little too late now.
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 		mode = MODE(ilmp) | RTLD_GROUP;
9387c478bd9Sstevel@tonic-gate 		mode &= ~RTLD_GLOBAL;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 		/*
9417c478bd9Sstevel@tonic-gate 		 * Insure that any auxiliary filter can locate symbols from its
9427c478bd9Sstevel@tonic-gate 		 * caller.
9437c478bd9Sstevel@tonic-gate 		 */
9447c478bd9Sstevel@tonic-gate 		if (dip->di_flags & FLG_DI_AUXFLTR)
9457c478bd9Sstevel@tonic-gate 			mode |= RTLD_PARENT;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		/*
9487c478bd9Sstevel@tonic-gate 		 * Process any hardware capability directory.  Establish a new
9497c478bd9Sstevel@tonic-gate 		 * link-map control list from which to analyze any newly added
95024a6229eSrie 		 * objects.
9517c478bd9Sstevel@tonic-gate 		 */
952*56deab07SRod Evans 		if ((pdp->pd_info == 0) && (pdp->pd_flags & PD_TKN_HWCAP)) {
953*56deab07SRod Evans 			const char	*dir = pdp->pd_pname;
954*56deab07SRod Evans 			Lm_cntl		*lmc;
955*56deab07SRod Evans 			Aliste		lmco;
95611a2bb38Srie 
95724a6229eSrie 			if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
958cce0e03bSab 				if ((lmc = alist_append(&lml->lm_lists, 0,
959*56deab07SRod Evans 				    sizeof (Lm_cntl), AL_CNT_LMLISTS)) == NULL)
96037ffaf83SRod Evans 					return (NULL);
9617c478bd9Sstevel@tonic-gate 				lmco = (Aliste)((char *)lmc -
9627c478bd9Sstevel@tonic-gate 				    (char *)lml->lm_lists);
96324a6229eSrie 			} else {
96424a6229eSrie 				lmc = 0;
965cce0e03bSab 				lmco = ALIST_OFF_DATA;
96624a6229eSrie 			}
9677c478bd9Sstevel@tonic-gate 
968*56deab07SRod Evans 			/*
969*56deab07SRod Evans 			 * Determine the hardware capability filtees.  If none
970*56deab07SRod Evans 			 * can be found, provide suitable diagnostics.
971*56deab07SRod Evans 			 */
972*56deab07SRod Evans 			DBG_CALL(Dbg_cap_hw_filter(lml, dir, ilmp));
973*56deab07SRod Evans 			if (hwcap_filtees((Alist **)&dip->di_info, idx, dir,
974*56deab07SRod Evans 			    lmco, lmc, ilmp, filtees, mode,
975*56deab07SRod Evans 			    (FLG_RT_HANDLE | FLG_RT_HWCAP), in_nfavl) == 0) {
976*56deab07SRod Evans 				if ((lml->lm_flags & LML_FLG_TRC_ENABLE) &&
977*56deab07SRod Evans 				    (dip->di_flags & FLG_DI_AUXFLTR) &&
978*56deab07SRod Evans 				    (rtld_flags & RT_FL_WARNFLTR)) {
979*56deab07SRod Evans 					(void) printf(
980*56deab07SRod Evans 					    MSG_INTL(MSG_LDD_HWCAP_NFOUND),
981*56deab07SRod Evans 					    dir);
982*56deab07SRod Evans 				}
983*56deab07SRod Evans 				DBG_CALL(Dbg_cap_hw_filter(lml, dir, 0));
984*56deab07SRod Evans 			}
985*56deab07SRod Evans 
986*56deab07SRod Evans 			/*
987*56deab07SRod Evans 			 * Re-establish the originating path name descriptor, as
988*56deab07SRod Evans 			 * the expansion of hardware capabilities filtees may
989*56deab07SRod Evans 			 * have re-allocated the controlling Alist.  Mark this
990*56deab07SRod Evans 			 * original pathname descriptor as unused so that the
991*56deab07SRod Evans 			 * descriptor isn't revisited for processing.  Any real
992*56deab07SRod Evans 			 * hardware capabilities filtees have been added as new
993*56deab07SRod Evans 			 * pathname descriptors following this descriptor.
994*56deab07SRod Evans 			 */
995*56deab07SRod Evans 			pdp = alist_item((Alist *)dip->di_info, idx);
996*56deab07SRod Evans 			pdp->pd_flags &= ~PD_TKN_HWCAP;
997*56deab07SRod Evans 			pdp->pd_plen = 0;
99811a2bb38Srie 
99911a2bb38Srie 			/*
100011a2bb38Srie 			 * Now that any hardware capability objects have been
100111a2bb38Srie 			 * processed, remove any link-map control list.
100211a2bb38Srie 			 */
100302ca3e02Srie 			if (lmc)
100411a2bb38Srie 				remove_cntl(lml, lmco);
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 
1007*56deab07SRod Evans 		if (pdp->pd_plen == 0)
10087c478bd9Sstevel@tonic-gate 			continue;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 		/*
10117c478bd9Sstevel@tonic-gate 		 * Process an individual filtee.
10127c478bd9Sstevel@tonic-gate 		 */
1013*56deab07SRod Evans 		if (pdp->pd_info == 0) {
1014*56deab07SRod Evans 			const char	*filtee = pdp->pd_pname;
10157c478bd9Sstevel@tonic-gate 			int		audit = 0;
10167c478bd9Sstevel@tonic-gate 
10175aefb655Srie 			DBG_CALL(Dbg_file_filtee(lml, NAME(ilmp), filtee, 0));
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 			ghp = 0;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 			/*
10227c478bd9Sstevel@tonic-gate 			 * Determine if the reference link map is already
10237c478bd9Sstevel@tonic-gate 			 * loaded.  As an optimization compare the filtee with
10247c478bd9Sstevel@tonic-gate 			 * our interpretor.  The most common filter is
10257c478bd9Sstevel@tonic-gate 			 * libdl.so.1, which is a filter on ld.so.1.
10267c478bd9Sstevel@tonic-gate 			 */
10277c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
10287c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) {
10297c478bd9Sstevel@tonic-gate #else
10307c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) {
10317c478bd9Sstevel@tonic-gate #endif
10327c478bd9Sstevel@tonic-gate 				/*
10338af2c5b9Srie 				 * Create an association between ld.so.1 and the
10348af2c5b9Srie 				 * filter.  As an optimization, a handle for
10358af2c5b9Srie 				 * ld.so.1 itself (required for the dlopen()
10368af2c5b9Srie 				 * family filtering mechanism) shouldn't search
10378af2c5b9Srie 				 * any dependencies of ld.so.1.  Omitting
10388af2c5b9Srie 				 * GPD_ADDEPS prevents the addition of any
10398af2c5b9Srie 				 * ld.so.1 dependencies to this handle.
10407c478bd9Sstevel@tonic-gate 				 */
10417c478bd9Sstevel@tonic-gate 				nlmp = lml_rtld.lm_head;
10427c478bd9Sstevel@tonic-gate 				if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp,
10438af2c5b9Srie 				    (GPH_LDSO | GPH_FIRST | GPH_FILTEE),
10448af2c5b9Srie 				    (GPD_DLSYM | GPD_RELOC), GPD_PARENT)) == 0)
10457c478bd9Sstevel@tonic-gate 					nlmp = 0;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 				/*
10487c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
10497c478bd9Sstevel@tonic-gate 				 * recursion.
10507c478bd9Sstevel@tonic-gate 				 */
10517c478bd9Sstevel@tonic-gate 				if (nlmp && ghp)
1052*56deab07SRod Evans 					pdp->pd_info = (void *)ghp;
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 				/*
10557c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  Ignore
10567c478bd9Sstevel@tonic-gate 				 * any return from the auditor, as we can't
10577c478bd9Sstevel@tonic-gate 				 * allow ignore filtering to ld.so.1, otherwise
10587c478bd9Sstevel@tonic-gate 				 * nothing is going to work.
10597c478bd9Sstevel@tonic-gate 				 */
1060*56deab07SRod Evans 				if (nlmp && ((lml->lm_tflags | AFLAGS(ilmp)) &
106102ca3e02Srie 				    LML_TFLG_AUD_OBJFILTER))
10627c478bd9Sstevel@tonic-gate 					(void) audit_objfilter(ilmp, filtees,
10637c478bd9Sstevel@tonic-gate 					    nlmp, 0);
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 			} else {
10667c478bd9Sstevel@tonic-gate 				Rej_desc	rej = { 0 };
1067*56deab07SRod Evans 				Fdesc		fd = { 0 };
106811a2bb38Srie 				Lm_cntl		*lmc;
106911a2bb38Srie 				Aliste		lmco;
10707c478bd9Sstevel@tonic-gate 
1071*56deab07SRod Evans 				/*
1072*56deab07SRod Evans 				 * Trace the inspection of this file, determine
1073*56deab07SRod Evans 				 * any auditor substitution, and seed the file
1074*56deab07SRod Evans 				 * descriptor with the originating name.
1075*56deab07SRod Evans 				 */
1076*56deab07SRod Evans 				if (load_trace(lml, pdp, clmp, &fd) == NULL)
1077*56deab07SRod Evans 					continue;
1078*56deab07SRod Evans 
10797c478bd9Sstevel@tonic-gate 				/*
10807c478bd9Sstevel@tonic-gate 				 * Establish a new link-map control list from
10817c478bd9Sstevel@tonic-gate 				 * which to analyze any newly added objects.
10827c478bd9Sstevel@tonic-gate 				 */
108324a6229eSrie 				if (FLAGS(lml->lm_head) & FLG_RT_RELOCED) {
108424a6229eSrie 					if ((lmc =
1085cce0e03bSab 					    alist_append(&lml->lm_lists, 0,
108624a6229eSrie 					    sizeof (Lm_cntl),
1087*56deab07SRod Evans 					    AL_CNT_LMLISTS)) == NULL)
108837ffaf83SRod Evans 						return (NULL);
10897c478bd9Sstevel@tonic-gate 					lmco = (Aliste)((char *)lmc -
10907c478bd9Sstevel@tonic-gate 					    (char *)lml->lm_lists);
109124a6229eSrie 				} else {
109224a6229eSrie 					lmc = 0;
1093cce0e03bSab 					lmco = ALIST_OFF_DATA;
109424a6229eSrie 				}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 				/*
1097*56deab07SRod Evans 				 * Locate and load the filtee.
10987c478bd9Sstevel@tonic-gate 				 */
1099*56deab07SRod Evans 				if ((nlmp = load_path(lml, lmco, ilmp, mode,
1100*56deab07SRod Evans 				    FLG_RT_HANDLE, &ghp, &fd, &rej,
1101*56deab07SRod Evans 				    in_nfavl)) == NULL)
11027c478bd9Sstevel@tonic-gate 					file_notfound(LIST(ilmp), filtee, ilmp,
11037c478bd9Sstevel@tonic-gate 					    FLG_RT_HANDLE, &rej);
1104*56deab07SRod Evans 
1105*56deab07SRod Evans 				filtee = pdp->pd_pname;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 				/*
11087c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
11097c478bd9Sstevel@tonic-gate 				 * recursion.
11107c478bd9Sstevel@tonic-gate 				 */
11117c478bd9Sstevel@tonic-gate 				if (nlmp && ghp) {
11127c478bd9Sstevel@tonic-gate 					ghp->gh_flags |= GPH_FILTEE;
1113*56deab07SRod Evans 					pdp->pd_info = (void *)ghp;
11149aa23310Srie 
11159aa23310Srie 					FLAGS1(nlmp) |= FL1_RT_USED;
11167c478bd9Sstevel@tonic-gate 				}
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 				/*
11197c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  A
11207c478bd9Sstevel@tonic-gate 				 * return of 0 indicates the auditor wishes to
11217c478bd9Sstevel@tonic-gate 				 * ignore this filtee.
11227c478bd9Sstevel@tonic-gate 				 */
11237c478bd9Sstevel@tonic-gate 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
11247c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)) {
11257c478bd9Sstevel@tonic-gate 					if (audit_objfilter(ilmp, filtees,
11267c478bd9Sstevel@tonic-gate 					    nlmp, 0) == 0) {
11277c478bd9Sstevel@tonic-gate 						audit = 1;
11287c478bd9Sstevel@tonic-gate 						nlmp = 0;
11297c478bd9Sstevel@tonic-gate 					}
11307c478bd9Sstevel@tonic-gate 				}
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 				/*
11337c478bd9Sstevel@tonic-gate 				 * Finish processing the objects associated with
11347c478bd9Sstevel@tonic-gate 				 * this request.  Create an association between
11357c478bd9Sstevel@tonic-gate 				 * this object and the originating filter to
11367c478bd9Sstevel@tonic-gate 				 * provide sufficient information to tear down
11377c478bd9Sstevel@tonic-gate 				 * this filtee if necessary.
11387c478bd9Sstevel@tonic-gate 				 */
1139*56deab07SRod Evans 				if (nlmp && ghp && (((nlmp = analyze_lmc(lml,
1140*56deab07SRod Evans 				    lmco, nlmp, in_nfavl)) == NULL) ||
1141*56deab07SRod Evans 				    (relocate_lmc(lml, lmco, ilmp, nlmp,
1142*56deab07SRod Evans 				    in_nfavl) == 0)))
11437c478bd9Sstevel@tonic-gate 					nlmp = 0;
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 				/*
11467c478bd9Sstevel@tonic-gate 				 * If the filtee has been successfully
114702ca3e02Srie 				 * processed, then create an association
114802ca3e02Srie 				 * between the filter and filtee.  This
114902ca3e02Srie 				 * association provides sufficient information
115002ca3e02Srie 				 * to tear down the filter and filtee if
115102ca3e02Srie 				 * necessary.
11527c478bd9Sstevel@tonic-gate 				 */
11538af2c5b9Srie 				DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
115402ca3e02Srie 				if (nlmp && ghp &&
11557c478bd9Sstevel@tonic-gate 				    (hdl_add(ghp, ilmp, GPD_FILTER) == 0))
11567c478bd9Sstevel@tonic-gate 					nlmp = 0;
115711a2bb38Srie 
1158*56deab07SRod Evans 				/*
1159*56deab07SRod Evans 				 * Generate a diagnostic if the filtee couldn't
1160*56deab07SRod Evans 				 * be loaded.
1161*56deab07SRod Evans 				 */
1162*56deab07SRod Evans 				if (nlmp == 0)
1163*56deab07SRod Evans 					DBG_CALL(Dbg_file_filtee(lml, 0, filtee,
1164*56deab07SRod Evans 					    audit));
1165*56deab07SRod Evans 
116611a2bb38Srie 				/*
116702ca3e02Srie 				 * If this filtee loading has failed, and we've
116802ca3e02Srie 				 * created a new link-map control list to which
116902ca3e02Srie 				 * this request has added objects, then remove
117002ca3e02Srie 				 * all the objects that have been associated to
117102ca3e02Srie 				 * this request.
117211a2bb38Srie 				 */
117302ca3e02Srie 				if ((nlmp == 0) && lmc && lmc->lc_head)
117402ca3e02Srie 					remove_lmc(lml, clmp, lmc, lmco, name);
117502ca3e02Srie 
117602ca3e02Srie 				/*
117702ca3e02Srie 				 * Remove any link-map control list that was
117802ca3e02Srie 				 * created.
117902ca3e02Srie 				 */
118002ca3e02Srie 				if (lmc)
118111a2bb38Srie 					remove_cntl(lml, lmco);
11827c478bd9Sstevel@tonic-gate 			}
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 			/*
1185*56deab07SRod Evans 			 * If the filtee couldn't be loaded, null out the
1186*56deab07SRod Evans 			 * path name descriptor entry, and continue the search.
1187*56deab07SRod Evans 			 * Otherwise, the group handle is retained for future
1188*56deab07SRod Evans 			 * symbol searches.
11897c478bd9Sstevel@tonic-gate 			 */
11907c478bd9Sstevel@tonic-gate 			if (nlmp == 0) {
1191*56deab07SRod Evans 				pdp->pd_info = NULL;
1192*56deab07SRod Evans 				pdp->pd_plen = 0;
11937c478bd9Sstevel@tonic-gate 				continue;
11947c478bd9Sstevel@tonic-gate 			}
11957c478bd9Sstevel@tonic-gate 		}
11967c478bd9Sstevel@tonic-gate 
1197*56deab07SRod Evans 		ghp = (Grp_hdl *)pdp->pd_info;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 		/*
12007c478bd9Sstevel@tonic-gate 		 * If we're just here to trigger filtee loading skip the symbol
12017c478bd9Sstevel@tonic-gate 		 * lookup so we'll continue looking for additional filtees.
12027c478bd9Sstevel@tonic-gate 		 */
12037c478bd9Sstevel@tonic-gate 		if (name) {
12047c478bd9Sstevel@tonic-gate 			Grp_desc	*gdp;
120537ffaf83SRod Evans 			Sym		*sym = NULL;
1206cce0e03bSab 			Aliste		idx;
12077c478bd9Sstevel@tonic-gate 			Slookup		sl = *slp;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			sl.sl_flags |= LKUP_FIRST;
12107c478bd9Sstevel@tonic-gate 			any++;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 			/*
12137c478bd9Sstevel@tonic-gate 			 * Look for the symbol in the handles dependencies.
12147c478bd9Sstevel@tonic-gate 			 */
1215cce0e03bSab 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1216efb9e8b8Srie 				if ((gdp->gd_flags & GPD_DLSYM) == 0)
12177c478bd9Sstevel@tonic-gate 					continue;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 				/*
12207c478bd9Sstevel@tonic-gate 				 * If our parent is a dependency don't look at
12217c478bd9Sstevel@tonic-gate 				 * it (otherwise we are in a recursive loop).
12227c478bd9Sstevel@tonic-gate 				 * This situation can occur with auxiliary
12237c478bd9Sstevel@tonic-gate 				 * filters if the filtee has a dependency on the
12247c478bd9Sstevel@tonic-gate 				 * filter.  This dependency isn't necessary as
12257c478bd9Sstevel@tonic-gate 				 * auxiliary filters are opened RTLD_PARENT, but
12267c478bd9Sstevel@tonic-gate 				 * users may still unknowingly add an explicit
12277c478bd9Sstevel@tonic-gate 				 * dependency to the parent.
12287c478bd9Sstevel@tonic-gate 				 */
12297c478bd9Sstevel@tonic-gate 				if ((sl.sl_imap = gdp->gd_depend) == ilmp)
12307c478bd9Sstevel@tonic-gate 					continue;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 				if (((sym = SYMINTP(sl.sl_imap)(&sl, dlmp,
123376396fccSrie 				    binfo, in_nfavl)) != 0) ||
12347c478bd9Sstevel@tonic-gate 				    (ghp->gh_flags & GPH_FIRST))
12357c478bd9Sstevel@tonic-gate 					break;
12367c478bd9Sstevel@tonic-gate 			}
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 			/*
123902ca3e02Srie 			 * If a symbol has been found, indicate the binding
124002ca3e02Srie 			 * and return the symbol.
12417c478bd9Sstevel@tonic-gate 			 */
12427c478bd9Sstevel@tonic-gate 			if (sym) {
12437c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_FILTEE;
12447c478bd9Sstevel@tonic-gate 				return (sym);
12457c478bd9Sstevel@tonic-gate 			}
12467c478bd9Sstevel@tonic-gate 		}
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 		/*
12497c478bd9Sstevel@tonic-gate 		 * If this object is tagged to terminate filtee processing we're
12507c478bd9Sstevel@tonic-gate 		 * done.
12517c478bd9Sstevel@tonic-gate 		 */
12525aefb655Srie 		if (FLAGS1(ghp->gh_ownlmp) & FL1_RT_ENDFILTE)
12537c478bd9Sstevel@tonic-gate 			break;
12547c478bd9Sstevel@tonic-gate 	}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	/*
12577c478bd9Sstevel@tonic-gate 	 * If we're just here to trigger filtee loading then we're done.
12587c478bd9Sstevel@tonic-gate 	 */
12597c478bd9Sstevel@tonic-gate 	if (name == 0)
126037ffaf83SRod Evans 		return (NULL);
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	/*
1263*56deab07SRod Evans 	 * If no filtees have been found for a filter, clean up any path name
1264*56deab07SRod Evans 	 * descriptors and disable their search completely.  For auxiliary
12657c478bd9Sstevel@tonic-gate 	 * filters we can reselect the symbol search function so that we never
12667c478bd9Sstevel@tonic-gate 	 * enter this routine again for this object.  For standard filters we
12677c478bd9Sstevel@tonic-gate 	 * use the null symbol routine.
12687c478bd9Sstevel@tonic-gate 	 */
12697c478bd9Sstevel@tonic-gate 	if (any == 0) {
1270*56deab07SRod Evans 		remove_plist((Alist **)&(dip->di_info), 1);
12717c478bd9Sstevel@tonic-gate 		elf_disable_filtee(ilmp, dip);
127237ffaf83SRod Evans 		return (NULL);
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 
127537ffaf83SRod Evans 	return (NULL);
12767c478bd9Sstevel@tonic-gate }
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate  * Focal point for disabling error messages for auxiliary filters.  As an
12807c478bd9Sstevel@tonic-gate  * auxiliary filter allows for filtee use, but provides a fallback should a
12817c478bd9Sstevel@tonic-gate  * filtee not exist (or fail to load), any errors generated as a consequence of
12827c478bd9Sstevel@tonic-gate  * trying to load the filtees are typically suppressed.  Setting RT_FL_SILENCERR
12837c478bd9Sstevel@tonic-gate  * suppresses errors generated by eprint(), but insures a debug diagnostic is
12847c478bd9Sstevel@tonic-gate  * produced.  ldd(1) employs printf(), and here, the selection of whether to
12857c478bd9Sstevel@tonic-gate  * print a diagnostic in regards to auxiliary filters is a little more complex.
12867c478bd9Sstevel@tonic-gate  *
12877c478bd9Sstevel@tonic-gate  *   .	The determination of whether to produce an ldd message, or a fatal
12887c478bd9Sstevel@tonic-gate  *	error message is driven by LML_FLG_TRC_ENABLE.
12897c478bd9Sstevel@tonic-gate  *   .	More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN,
12907c478bd9Sstevel@tonic-gate  *	(ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s),
12917c478bd9Sstevel@tonic-gate  *	and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u).
12927c478bd9Sstevel@tonic-gate  *
12937c478bd9Sstevel@tonic-gate  *   .	If the calling object is lddstub, then several classes of message are
12947c478bd9Sstevel@tonic-gate  *	suppressed.  The user isn't trying to diagnose lddstub, this is simply
12957c478bd9Sstevel@tonic-gate  *	a stub executable employed to preload a user specified library against.
12967c478bd9Sstevel@tonic-gate  *
12977c478bd9Sstevel@tonic-gate  *   .	If RT_FL_SILENCERR is in effect then any generic ldd() messages should
12987c478bd9Sstevel@tonic-gate  *	be suppressed.  All detailed ldd messages should still be produced.
12997c478bd9Sstevel@tonic-gate  */
13007c478bd9Sstevel@tonic-gate Sym *
13019aa23310Srie elf_lookup_filtee(Slookup *slp, Rt_map **dlmp, uint_t *binfo, uint_t ndx,
13029aa23310Srie     int *in_nfavl)
13037c478bd9Sstevel@tonic-gate {
13047c478bd9Sstevel@tonic-gate 	Sym	*sym;
13057c478bd9Sstevel@tonic-gate 	Dyninfo	*dip = &DYNINFO(slp->sl_imap)[ndx];
13067c478bd9Sstevel@tonic-gate 	int	silent = 0;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * Make sure this entry is still acting as a filter.  We may have tried
13107c478bd9Sstevel@tonic-gate 	 * to process this previously, and disabled it if the filtee couldn't
13117c478bd9Sstevel@tonic-gate 	 * be processed.  However, other entries may provide different filtees
13127c478bd9Sstevel@tonic-gate 	 * that are yet to be completed.
13137c478bd9Sstevel@tonic-gate 	 */
13147c478bd9Sstevel@tonic-gate 	if (dip->di_flags == 0)
131537ffaf83SRod Evans 		return (NULL);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 	/*
13187c478bd9Sstevel@tonic-gate 	 * Indicate whether an error message is required should this filtee not
13197c478bd9Sstevel@tonic-gate 	 * be found, based on the type of filter.
13207c478bd9Sstevel@tonic-gate 	 */
13217c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_AUXFLTR) &&
13227c478bd9Sstevel@tonic-gate 	    ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) {
13237c478bd9Sstevel@tonic-gate 		rtld_flags |= RT_FL_SILENCERR;
13247c478bd9Sstevel@tonic-gate 		silent = 1;
13257c478bd9Sstevel@tonic-gate 	}
13267c478bd9Sstevel@tonic-gate 
13279aa23310Srie 	sym = _elf_lookup_filtee(slp, dlmp, binfo, ndx, in_nfavl);
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate 	if (silent)
13307c478bd9Sstevel@tonic-gate 		rtld_flags &= ~RT_FL_SILENCERR;
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate 	return (sym);
13337c478bd9Sstevel@tonic-gate }
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate /*
13367c478bd9Sstevel@tonic-gate  * Compute the elf hash value (as defined in the ELF access library).
13377c478bd9Sstevel@tonic-gate  * The form of the hash table is:
13387c478bd9Sstevel@tonic-gate  *
13397c478bd9Sstevel@tonic-gate  *	|--------------|
13407c478bd9Sstevel@tonic-gate  *	| # of buckets |
13417c478bd9Sstevel@tonic-gate  *	|--------------|
13427c478bd9Sstevel@tonic-gate  *	| # of chains  |
13437c478bd9Sstevel@tonic-gate  *	|--------------|
13447c478bd9Sstevel@tonic-gate  *	|   bucket[]   |
13457c478bd9Sstevel@tonic-gate  *	|--------------|
13467c478bd9Sstevel@tonic-gate  *	|   chain[]    |
13477c478bd9Sstevel@tonic-gate  *	|--------------|
13487c478bd9Sstevel@tonic-gate  */
13497c478bd9Sstevel@tonic-gate ulong_t
13507c478bd9Sstevel@tonic-gate elf_hash(const char *name)
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	uint_t	hval = 0;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 	while (*name) {
13557c478bd9Sstevel@tonic-gate 		uint_t	g;
13567c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *name++;
13577c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
13587c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
13597c478bd9Sstevel@tonic-gate 		hval &= ~g;
13607c478bd9Sstevel@tonic-gate 	}
13617c478bd9Sstevel@tonic-gate 	return ((ulong_t)hval);
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate /*
13657c478bd9Sstevel@tonic-gate  * If flag argument has LKUP_SPEC set, we treat undefined symbols of type
13667c478bd9Sstevel@tonic-gate  * function specially in the executable - if they have a value, even though
13677c478bd9Sstevel@tonic-gate  * undefined, we use that value.  This allows us to associate all references
13687c478bd9Sstevel@tonic-gate  * to a function's address to a single place in the process: the plt entry
13697c478bd9Sstevel@tonic-gate  * for that function in the executable.  Calls to lookup from plt binding
13707c478bd9Sstevel@tonic-gate  * routines do NOT set LKUP_SPEC in the flag.
13717c478bd9Sstevel@tonic-gate  */
13727c478bd9Sstevel@tonic-gate Sym *
13739aa23310Srie elf_find_sym(Slookup *slp, Rt_map **dlmp, uint_t *binfo, int *in_nfavl)
13747c478bd9Sstevel@tonic-gate {
13757c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
13767c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
13777c478bd9Sstevel@tonic-gate 	ulong_t		hash = slp->sl_hash;
13787c478bd9Sstevel@tonic-gate 	uint_t		ndx, htmp, buckets, *chainptr;
13797c478bd9Sstevel@tonic-gate 	Sym		*sym, *symtabptr;
13807c478bd9Sstevel@tonic-gate 	char		*strtabptr, *strtabname;
13817c478bd9Sstevel@tonic-gate 	uint_t		flags1;
13827c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
13837c478bd9Sstevel@tonic-gate 
1384660acd81Srie 	/*
1385660acd81Srie 	 * If we're only here to establish a symbols index, skip the diagnostic
1386660acd81Srie 	 * used to trace a symbol search.
1387660acd81Srie 	 */
13885aefb655Srie 	if ((slp->sl_flags & LKUP_SYMNDX) == 0)
13895aefb655Srie 		DBG_CALL(Dbg_syms_lookup(ilmp, name, MSG_ORIG(MSG_STR_ELF)));
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate 	if (HASH(ilmp) == 0)
139237ffaf83SRod Evans 		return (NULL);
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	buckets = HASH(ilmp)[0];
13957c478bd9Sstevel@tonic-gate 	/* LINTED */
13967c478bd9Sstevel@tonic-gate 	htmp = (uint_t)hash % buckets;
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	/*
13997c478bd9Sstevel@tonic-gate 	 * Get the first symbol on hash chain and initialize the string
14007c478bd9Sstevel@tonic-gate 	 * and symbol table pointers.
14017c478bd9Sstevel@tonic-gate 	 */
14027c478bd9Sstevel@tonic-gate 	if ((ndx = HASH(ilmp)[htmp + 2]) == 0)
140337ffaf83SRod Evans 		return (NULL);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	chainptr = HASH(ilmp) + 2 + buckets;
14067c478bd9Sstevel@tonic-gate 	strtabptr = STRTAB(ilmp);
14077c478bd9Sstevel@tonic-gate 	symtabptr = SYMTAB(ilmp);
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 	while (ndx) {
14107c478bd9Sstevel@tonic-gate 		sym = symtabptr + ndx;
14117c478bd9Sstevel@tonic-gate 		strtabname = strtabptr + sym->st_name;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 		/*
14147c478bd9Sstevel@tonic-gate 		 * Compare the symbol found with the name required.  If the
14157c478bd9Sstevel@tonic-gate 		 * names don't match continue with the next hash entry.
14167c478bd9Sstevel@tonic-gate 		 */
14177c478bd9Sstevel@tonic-gate 		if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
14187c478bd9Sstevel@tonic-gate 			if ((ndx = chainptr[ndx]) != 0)
14197c478bd9Sstevel@tonic-gate 				continue;
142037ffaf83SRod Evans 			return (NULL);
14217c478bd9Sstevel@tonic-gate 		}
14227c478bd9Sstevel@tonic-gate 
14233b41b08bSab 		/*
1424d840867fSab 		 * The Solaris ld does not put DT_VERSYM in the dynamic
1425d840867fSab 		 * section, but the GNU ld does. The GNU runtime linker
1426d840867fSab 		 * interprets the top bit of the 16-bit Versym value
1427d840867fSab 		 * (0x8000) as the "hidden" bit. If this bit is set,
1428d840867fSab 		 * the linker is supposed to act as if that symbol does
1429d840867fSab 		 * not exist. The hidden bit supports their versioning
1430d840867fSab 		 * scheme, which allows multiple incompatible functions
1431d840867fSab 		 * with the same name to exist at different versions
1432d840867fSab 		 * within an object. The Solaris linker does not support this
1433d840867fSab 		 * mechanism, or the model of interface evolution that
1434d840867fSab 		 * it allows, but we honor the hidden bit in GNU ld
1435d840867fSab 		 * produced objects in order to interoperate with them.
14363b41b08bSab 		 */
1437d840867fSab 		if ((VERSYM(ilmp) != NULL) &&
1438d840867fSab 		    ((VERSYM(ilmp)[ndx] & 0x8000) != 0)) {
1439d840867fSab 			DBG_CALL(Dbg_syms_ignore_gnuver(ilmp, name,
14403b41b08bSab 			    ndx, VERSYM(ilmp)[ndx]));
14413b41b08bSab 			if ((ndx = chainptr[ndx]) != 0)
14423b41b08bSab 				continue;
144337ffaf83SRod Evans 			return (NULL);
14443b41b08bSab 		}
14453b41b08bSab 
1446660acd81Srie 		/*
1447660acd81Srie 		 * If we're only here to establish a symbols index, we're done.
1448660acd81Srie 		 */
1449660acd81Srie 		if (slp->sl_flags & LKUP_SYMNDX)
1450660acd81Srie 			return (sym);
1451660acd81Srie 
14527c478bd9Sstevel@tonic-gate 		/*
14537c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is defined, return the
14547c478bd9Sstevel@tonic-gate 		 * symbol pointer and the link map in which it was found.
14557c478bd9Sstevel@tonic-gate 		 */
14567c478bd9Sstevel@tonic-gate 		if (sym->st_shndx != SHN_UNDEF) {
14577c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
14587c478bd9Sstevel@tonic-gate 			*binfo |= DBG_BINFO_FOUND;
14599a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
14609a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
14619a411307Srie 			    is_sym_interposer(ilmp, sym)))
14627c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
14637c478bd9Sstevel@tonic-gate 			break;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 		/*
14667c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is undefined, the
14677c478bd9Sstevel@tonic-gate 		 * symbol type is a function, and the value of the symbol
14687c478bd9Sstevel@tonic-gate 		 * is non zero, then this is a special case.  This allows
14697c478bd9Sstevel@tonic-gate 		 * the resolution of a function address to the plt[] entry.
14707c478bd9Sstevel@tonic-gate 		 * See SPARC ABI, Dynamic Linking, Function Addresses for
14717c478bd9Sstevel@tonic-gate 		 * more details.
14727c478bd9Sstevel@tonic-gate 		 */
1473660acd81Srie 		} else if ((slp->sl_flags & LKUP_SPEC) &&
14747c478bd9Sstevel@tonic-gate 		    (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) &&
14757c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
14767c478bd9Sstevel@tonic-gate 			*dlmp = ilmp;
14777c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR);
14789a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
14799a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
14809a411307Srie 			    is_sym_interposer(ilmp, sym)))
14817c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
14827c478bd9Sstevel@tonic-gate 			return (sym);
14837c478bd9Sstevel@tonic-gate 		}
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate 		/*
14867c478bd9Sstevel@tonic-gate 		 * Undefined symbol.
14877c478bd9Sstevel@tonic-gate 		 */
148837ffaf83SRod Evans 		return (NULL);
14897c478bd9Sstevel@tonic-gate 	}
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	/*
14927c478bd9Sstevel@tonic-gate 	 * We've found a match.  Determine if the defining object contains
14937c478bd9Sstevel@tonic-gate 	 * symbol binding information.
14947c478bd9Sstevel@tonic-gate 	 */
14957c478bd9Sstevel@tonic-gate 	if ((sip = SYMINFO(ilmp)) != 0)
14969039eeafSab 		sip += ndx;
14977c478bd9Sstevel@tonic-gate 
149860758829Srie 	/*
149960758829Srie 	 * If this definition is a singleton, and we haven't followed a default
150060758829Srie 	 * symbol search knowing that we're looking for a singleton (presumably
150160758829Srie 	 * because the symbol definition has been changed since the referring
150260758829Srie 	 * object was built), then reject this binding so that the caller can
150360758829Srie 	 * fall back to a standard symbol search.
150460758829Srie 	 */
150560758829Srie 	if ((ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON) &&
150660758829Srie 	    (((slp->sl_flags & LKUP_STANDARD) == 0) ||
150760758829Srie 	    (((slp->sl_flags & LKUP_SINGLETON) == 0) &&
150860758829Srie 	    (LIST(ilmp)->lm_flags & LML_FLG_GROUPSEXIST)))) {
150960758829Srie 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
151060758829Srie 		    DBG_BNDREJ_SINGLE));
151160758829Srie 		*binfo |= BINFO_REJSINGLE;
151260758829Srie 		*binfo &= ~DBG_BINFO_MSK;
151337ffaf83SRod Evans 		return (NULL);
151460758829Srie 	}
151560758829Srie 
15167c478bd9Sstevel@tonic-gate 	/*
15177c478bd9Sstevel@tonic-gate 	 * If this is a direct binding request, but the symbol definition has
15187c478bd9Sstevel@tonic-gate 	 * disabled directly binding to it (presumably because the symbol
15197c478bd9Sstevel@tonic-gate 	 * definition has been changed since the referring object was built),
152037ffaf83SRod Evans 	 * reject this binding so that the caller can fall back to a standard
152160758829Srie 	 * symbol search.
15227c478bd9Sstevel@tonic-gate 	 */
15237c478bd9Sstevel@tonic-gate 	if (sip && (slp->sl_flags & LKUP_DIRECT) &&
15247c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) {
152560758829Srie 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
152637ffaf83SRod Evans 		    DBG_BNDREJ_DIRECT));
152760758829Srie 		*binfo |= BINFO_REJDIRECT;
15287c478bd9Sstevel@tonic-gate 		*binfo &= ~DBG_BINFO_MSK;
152937ffaf83SRod Evans 		return (NULL);
153037ffaf83SRod Evans 	}
153137ffaf83SRod Evans 
153237ffaf83SRod Evans 	/*
153337ffaf83SRod Evans 	 * If this is a binding request within an RTLD_GROUP family, and the
153437ffaf83SRod Evans 	 * symbol has disabled directly binding to it, reject this binding so
153537ffaf83SRod Evans 	 * that the caller can fall back to a standard symbol search.
153637ffaf83SRod Evans 	 *
153737ffaf83SRod Evans 	 * Effectively, an RTLD_GROUP family achieves what can now be
153837ffaf83SRod Evans 	 * established with direct bindings.  However, various symbols have
153937ffaf83SRod Evans 	 * been tagged as inappropriate for direct binding to (ie. libc:malloc).
154037ffaf83SRod Evans 	 *
154137ffaf83SRod Evans 	 * A symbol marked as no-direct cannot be used within a group without
154237ffaf83SRod Evans 	 * first ensuring that the symbol has not been interposed upon outside
154337ffaf83SRod Evans 	 * of the group.  A common example occurs when users implement their own
154437ffaf83SRod Evans 	 * version of malloc() in the executable.  Such a malloc() interposes on
154537ffaf83SRod Evans 	 * the libc:malloc, and this interposition must be honored within the
154637ffaf83SRod Evans 	 * group as well.
154737ffaf83SRod Evans 	 *
154837ffaf83SRod Evans 	 * Following any rejection, LKUP_WORLD is established as a means of
154937ffaf83SRod Evans 	 * overriding this test as we return to a standard search.
155037ffaf83SRod Evans 	 */
155137ffaf83SRod Evans 	if (sip && (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT) &&
155237ffaf83SRod Evans 	    ((MODE(slp->sl_cmap) & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP) &&
155337ffaf83SRod Evans 	    ((slp->sl_flags & LKUP_WORLD) == 0)) {
155437ffaf83SRod Evans 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
155537ffaf83SRod Evans 		    DBG_BNDREJ_GROUP));
155637ffaf83SRod Evans 		*binfo |= BINFO_REJGROUP;
155737ffaf83SRod Evans 		*binfo &= ~DBG_BINFO_MSK;
155837ffaf83SRod Evans 		return (NULL);
15597c478bd9Sstevel@tonic-gate 	}
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	/*
15627c478bd9Sstevel@tonic-gate 	 * Determine whether this object is acting as a filter.
15637c478bd9Sstevel@tonic-gate 	 */
15647c478bd9Sstevel@tonic-gate 	if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0)
15657c478bd9Sstevel@tonic-gate 		return (sym);
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * Determine if this object offers per-symbol filtering, and if so,
15697c478bd9Sstevel@tonic-gate 	 * whether this symbol references a filtee.
15707c478bd9Sstevel@tonic-gate 	 */
15717c478bd9Sstevel@tonic-gate 	if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) {
15727c478bd9Sstevel@tonic-gate 		/*
15737c478bd9Sstevel@tonic-gate 		 * If this is a standard filter reference, and no standard
15747c478bd9Sstevel@tonic-gate 		 * filtees remain to be inspected, we're done.  If this is an
15757c478bd9Sstevel@tonic-gate 		 * auxiliary filter reference, and no auxiliary filtees remain,
15767c478bd9Sstevel@tonic-gate 		 * we'll fall through in case any object filtering is available.
15777c478bd9Sstevel@tonic-gate 		 */
15787c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) &&
15797c478bd9Sstevel@tonic-gate 		    (SYMSFLTRCNT(ilmp) == 0))
158037ffaf83SRod Evans 			return (NULL);
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) ||
15837c478bd9Sstevel@tonic-gate 		    ((sip->si_flags & SYMINFO_FLG_AUXILIARY) &&
15847c478bd9Sstevel@tonic-gate 		    SYMAFLTRCNT(ilmp))) {
158575e7992aSrie 			Sym	*fsym;
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 			/*
15887c478bd9Sstevel@tonic-gate 			 * This symbol has an associated filtee.  Lookup the
15897c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
15907c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
15917c478bd9Sstevel@tonic-gate 			 * filter, return an error, otherwise fall through to
15927c478bd9Sstevel@tonic-gate 			 * catch any object filtering that may be available.
15937c478bd9Sstevel@tonic-gate 			 */
15947c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
15959aa23310Srie 			    sip->si_boundto, in_nfavl)) != 0)
15967c478bd9Sstevel@tonic-gate 				return (fsym);
15977c478bd9Sstevel@tonic-gate 			if (sip->si_flags & SYMINFO_FLG_FILTER)
159837ffaf83SRod Evans 				return (NULL);
15997c478bd9Sstevel@tonic-gate 		}
16007c478bd9Sstevel@tonic-gate 	}
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	/*
16037c478bd9Sstevel@tonic-gate 	 * Determine if this object provides global filtering.
16047c478bd9Sstevel@tonic-gate 	 */
16057c478bd9Sstevel@tonic-gate 	if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) {
160675e7992aSrie 		Sym	*fsym;
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) {
16097c478bd9Sstevel@tonic-gate 			/*
16107c478bd9Sstevel@tonic-gate 			 * This object has an associated filtee.  Lookup the
16117c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
16127c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
16137c478bd9Sstevel@tonic-gate 			 * filter, return and error, otherwise return the symbol
16147c478bd9Sstevel@tonic-gate 			 * within the filter itself.
16157c478bd9Sstevel@tonic-gate 			 */
16167c478bd9Sstevel@tonic-gate 			if ((fsym = elf_lookup_filtee(slp, dlmp, binfo,
16179aa23310Srie 			    OBJFLTRNDX(ilmp), in_nfavl)) != 0)
16187c478bd9Sstevel@tonic-gate 				return (fsym);
16197c478bd9Sstevel@tonic-gate 		}
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 		if (flags1 & FL1_RT_OBJSFLTR)
162237ffaf83SRod Evans 			return (NULL);
16237c478bd9Sstevel@tonic-gate 	}
16247c478bd9Sstevel@tonic-gate 	return (sym);
16257c478bd9Sstevel@tonic-gate }
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate /*
16287c478bd9Sstevel@tonic-gate  * Create a new Rt_map structure for an ELF object and initialize
16297c478bd9Sstevel@tonic-gate  * all values.
16307c478bd9Sstevel@tonic-gate  */
16317c478bd9Sstevel@tonic-gate Rt_map *
1632*56deab07SRod Evans elf_new_lmp(Lm_list *lml, Aliste lmco, Fdesc *fdp, Addr addr, size_t msize,
1633*56deab07SRod Evans     void *odyn, int *in_nfavl)
16347c478bd9Sstevel@tonic-gate {
1635*56deab07SRod Evans 	const char	*name = fdp->fd_nname;
16367c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
16377c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr = (Ehdr *)addr;
1638*56deab07SRod Evans 	Phdr		*phdr, *tphdr = NULL, *dphdr = NULL, *uphdr = NULL;
1639*56deab07SRod Evans 	Dyn		*dyn = (Dyn *)odyn;
1640*56deab07SRod Evans 	Cap		*cap = NULL;
1641*56deab07SRod Evans 	int		ndx;
1642*56deab07SRod Evans 	Addr		base, fltr = 0, audit = 0, cfile = 0, crle = 0;
1643*56deab07SRod Evans 	Xword		rpath = 0;
1644*56deab07SRod Evans 	size_t		lmsz, rtsz, epsz, dynsz = 0;
1645*56deab07SRod Evans 	uint_t		dyncnt = 0;
16467c478bd9Sstevel@tonic-gate 
1647*56deab07SRod Evans 	DBG_CALL(Dbg_file_elf(lml, name, addr, msize, lml->lm_lmidstr, lmco));
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	/*
1650*56deab07SRod Evans 	 * If this is a shared object, the base address of the shared object is
1651*56deab07SRod Evans 	 * added to all address values defined within the object.  Otherwise, if
1652*56deab07SRod Evans 	 * this is an executable, all object addresses are used as is.
16537c478bd9Sstevel@tonic-gate 	 */
1654*56deab07SRod Evans 	if (ehdr->e_type == ET_EXEC)
1655*56deab07SRod Evans 		base = 0;
1656*56deab07SRod Evans 	else
1657*56deab07SRod Evans 		base = addr;
1658*56deab07SRod Evans 
1659*56deab07SRod Evans 	/*
1660*56deab07SRod Evans 	 * Traverse the program header table, picking off required items.  This
1661*56deab07SRod Evans 	 * traversal also provides for the sizing of the PT_DYNAMIC section.
1662*56deab07SRod Evans 	 */
1663*56deab07SRod Evans 	phdr = (Phdr *)((uintptr_t)ehdr + ehdr->e_phoff);
1664*56deab07SRod Evans 	for (ndx = 0; ndx < (int)ehdr->e_phnum; ndx++,
1665*56deab07SRod Evans 	    phdr = (Phdr *)((uintptr_t)phdr + ehdr->e_phentsize)) {
1666*56deab07SRod Evans 		switch (phdr->p_type) {
1667*56deab07SRod Evans 		case PT_DYNAMIC:
1668*56deab07SRod Evans 			dphdr = phdr;
1669*56deab07SRod Evans 			dyn = (Dyn *)((uintptr_t)phdr->p_vaddr + base);
1670*56deab07SRod Evans 			break;
1671*56deab07SRod Evans 		case PT_TLS:
1672*56deab07SRod Evans 			tphdr = phdr;
1673*56deab07SRod Evans 			break;
1674*56deab07SRod Evans 		case PT_SUNWCAP:
1675*56deab07SRod Evans 			cap = (Cap *)((uintptr_t)phdr->p_vaddr + base);
1676*56deab07SRod Evans 			break;
1677*56deab07SRod Evans 		case PT_SUNW_UNWIND:
1678*56deab07SRod Evans 			uphdr = phdr;
1679*56deab07SRod Evans 			break;
1680*56deab07SRod Evans 		default:
1681*56deab07SRod Evans 			break;
1682*56deab07SRod Evans 		}
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate 
1685*56deab07SRod Evans 	/*
1686*56deab07SRod Evans 	 * Determine the number of PT_DYNAMIC entries for the DYNINFO()
1687*56deab07SRod Evans 	 * allocation.  Sadly, this is a little larger than we really need,
1688*56deab07SRod Evans 	 * as there are typically padding DT_NULL entries.  However, adding
1689*56deab07SRod Evans 	 * this data to the initial link-map allocation is a win.
1690*56deab07SRod Evans 	 */
1691*56deab07SRod Evans 	if (dyn) {
1692*56deab07SRod Evans 		dyncnt = dphdr->p_filesz / sizeof (Dyn);
1693*56deab07SRod Evans 		dynsz = dyncnt * sizeof (Dyninfo);
1694*56deab07SRod Evans 	}
1695*56deab07SRod Evans 
1696*56deab07SRod Evans 	/*
1697*56deab07SRod Evans 	 * Allocate space for the link-map, private elf information, and
1698*56deab07SRod Evans 	 * DYNINFO() data.  Once these are allocated and initialized,
1699*56deab07SRod Evans 	 * remove_so(0, lmp) can be used to tear down the link-map allocation
1700*56deab07SRod Evans 	 * should any failures occur.
1701*56deab07SRod Evans 	 */
1702*56deab07SRod Evans 	rtsz = S_DROUND(sizeof (Rt_map));
1703*56deab07SRod Evans 	epsz = S_DROUND(sizeof (Rt_elfp));
1704*56deab07SRod Evans 	lmsz = rtsz + epsz + dynsz;
1705*56deab07SRod Evans 	if ((lmp = calloc(lmsz, 1)) == NULL)
1706*56deab07SRod Evans 		return (NULL);
1707*56deab07SRod Evans 	ELFPRV(lmp) = (void *)((uintptr_t)lmp + rtsz);
1708*56deab07SRod Evans 	DYNINFO(lmp) = (Dyninfo *)((uintptr_t)lmp + rtsz + epsz);
1709*56deab07SRod Evans 	LMSIZE(lmp) = lmsz;
1710*56deab07SRod Evans 
17117c478bd9Sstevel@tonic-gate 	/*
17127c478bd9Sstevel@tonic-gate 	 * All fields not filled in were set to 0 by calloc.
17137c478bd9Sstevel@tonic-gate 	 */
1714*56deab07SRod Evans 	NAME(lmp) = (char *)name;
17157c478bd9Sstevel@tonic-gate 	ADDR(lmp) = addr;
17167c478bd9Sstevel@tonic-gate 	MSIZE(lmp) = msize;
17177c478bd9Sstevel@tonic-gate 	SYMINTP(lmp) = elf_find_sym;
17187c478bd9Sstevel@tonic-gate 	FCT(lmp) = &elf_fct;
17197c478bd9Sstevel@tonic-gate 	LIST(lmp) = lml;
17207c478bd9Sstevel@tonic-gate 	OBJFLTRNDX(lmp) = FLTR_DISABLED;
1721dffec89cSrie 	SORTVAL(lmp) = -1;
1722*56deab07SRod Evans 	DYN(lmp) = dyn;
1723*56deab07SRod Evans 	DYNINFOCNT(lmp) = dyncnt;
1724*56deab07SRod Evans 	PTUNWIND(lmp) = uphdr;
17257c478bd9Sstevel@tonic-gate 
1726*56deab07SRod Evans 	if (ehdr->e_type == ET_EXEC)
17277c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_FIXED;
17287c478bd9Sstevel@tonic-gate 
17297c478bd9Sstevel@tonic-gate 	/*
17307c478bd9Sstevel@tonic-gate 	 * Fill in rest of the link map entries with information from the file's
17317c478bd9Sstevel@tonic-gate 	 * dynamic structure.
17327c478bd9Sstevel@tonic-gate 	 */
1733*56deab07SRod Evans 	if (dyn) {
173475e7992aSrie 		uint_t		dynndx = 0;
173510a4fa49Srie 		Xword		pltpadsz = 0;
173610a4fa49Srie 		Rti_desc	*rti;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		/* CSTYLED */
1739*56deab07SRod Evans 		for ( ; dyn->d_tag != DT_NULL; ++dyn, dynndx++) {
1740*56deab07SRod Evans 			switch ((Xword)dyn->d_tag) {
17417c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
1742*56deab07SRod Evans 				SYMTAB(lmp) = (void *)(dyn->d_un.d_ptr + base);
17437c478bd9Sstevel@tonic-gate 				break;
17449039eeafSab 			case DT_SUNW_SYMTAB:
17459039eeafSab 				SUNWSYMTAB(lmp) =
1746*56deab07SRod Evans 				    (void *)(dyn->d_un.d_ptr + base);
17479039eeafSab 				break;
17489039eeafSab 			case DT_SUNW_SYMSZ:
1749*56deab07SRod Evans 				SUNWSYMSZ(lmp) = dyn->d_un.d_val;
17509039eeafSab 				break;
17517c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
1752*56deab07SRod Evans 				STRTAB(lmp) = (void *)(dyn->d_un.d_ptr + base);
17537c478bd9Sstevel@tonic-gate 				break;
17547c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
1755*56deab07SRod Evans 				SYMENT(lmp) = dyn->d_un.d_val;
17567c478bd9Sstevel@tonic-gate 				break;
17577c478bd9Sstevel@tonic-gate 			case DT_FEATURE_1:
1758*56deab07SRod Evans 				dyn->d_un.d_val |= DTF_1_PARINIT;
1759*56deab07SRod Evans 				if (dyn->d_un.d_val & DTF_1_CONFEXP)
17607c478bd9Sstevel@tonic-gate 					crle = 1;
17617c478bd9Sstevel@tonic-gate 				break;
17627c478bd9Sstevel@tonic-gate 			case DT_MOVESZ:
1763*56deab07SRod Evans 				MOVESZ(lmp) = dyn->d_un.d_val;
17647c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_MOVE;
17657c478bd9Sstevel@tonic-gate 				break;
17667c478bd9Sstevel@tonic-gate 			case DT_MOVEENT:
1767*56deab07SRod Evans 				MOVEENT(lmp) = dyn->d_un.d_val;
17687c478bd9Sstevel@tonic-gate 				break;
17697c478bd9Sstevel@tonic-gate 			case DT_MOVETAB:
1770*56deab07SRod Evans 				MOVETAB(lmp) = (void *)(dyn->d_un.d_ptr + base);
17717c478bd9Sstevel@tonic-gate 				break;
17727c478bd9Sstevel@tonic-gate 			case DT_REL:
17737c478bd9Sstevel@tonic-gate 			case DT_RELA:
17747c478bd9Sstevel@tonic-gate 				/*
177575e7992aSrie 				 * At this time, ld.so. can only handle one
177675e7992aSrie 				 * type of relocation per object.
17777c478bd9Sstevel@tonic-gate 				 */
1778*56deab07SRod Evans 				REL(lmp) = (void *)(dyn->d_un.d_ptr + base);
17797c478bd9Sstevel@tonic-gate 				break;
17807c478bd9Sstevel@tonic-gate 			case DT_RELSZ:
17817c478bd9Sstevel@tonic-gate 			case DT_RELASZ:
1782*56deab07SRod Evans 				RELSZ(lmp) = dyn->d_un.d_val;
17837c478bd9Sstevel@tonic-gate 				break;
17847c478bd9Sstevel@tonic-gate 			case DT_RELENT:
17857c478bd9Sstevel@tonic-gate 			case DT_RELAENT:
1786*56deab07SRod Evans 				RELENT(lmp) = dyn->d_un.d_val;
17877c478bd9Sstevel@tonic-gate 				break;
17887c478bd9Sstevel@tonic-gate 			case DT_RELCOUNT:
17897c478bd9Sstevel@tonic-gate 			case DT_RELACOUNT:
1790*56deab07SRod Evans 				RELACOUNT(lmp) = (uint_t)dyn->d_un.d_val;
17917c478bd9Sstevel@tonic-gate 				break;
17927c478bd9Sstevel@tonic-gate 			case DT_HASH:
1793*56deab07SRod Evans 				HASH(lmp) = (uint_t *)(dyn->d_un.d_ptr + base);
17947c478bd9Sstevel@tonic-gate 				break;
17957c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
1796*56deab07SRod Evans 				PLTGOT(lmp) =
1797*56deab07SRod Evans 				    (uint_t *)(dyn->d_un.d_ptr + base);
17987c478bd9Sstevel@tonic-gate 				break;
17997c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
1800*56deab07SRod Evans 				PLTRELSZ(lmp) = dyn->d_un.d_val;
18017c478bd9Sstevel@tonic-gate 				break;
18027c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
1803*56deab07SRod Evans 				JMPREL(lmp) = (void *)(dyn->d_un.d_ptr + base);
18047c478bd9Sstevel@tonic-gate 				break;
18057c478bd9Sstevel@tonic-gate 			case DT_INIT:
1806*56deab07SRod Evans 				if (dyn->d_un.d_ptr != NULL)
18075b59e4caSab 					INIT(lmp) =
1808*56deab07SRod Evans 					    (void (*)())(dyn->d_un.d_ptr +
1809*56deab07SRod Evans 					    base);
18107c478bd9Sstevel@tonic-gate 				break;
18117c478bd9Sstevel@tonic-gate 			case DT_FINI:
1812*56deab07SRod Evans 				if (dyn->d_un.d_ptr != NULL)
18135b59e4caSab 					FINI(lmp) =
1814*56deab07SRod Evans 					    (void (*)())(dyn->d_un.d_ptr +
1815*56deab07SRod Evans 					    base);
18167c478bd9Sstevel@tonic-gate 				break;
18177c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAY:
1818*56deab07SRod Evans 				INITARRAY(lmp) = (Addr *)(dyn->d_un.d_ptr +
18197c478bd9Sstevel@tonic-gate 				    base);
18207c478bd9Sstevel@tonic-gate 				break;
18217c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAYSZ:
1822*56deab07SRod Evans 				INITARRAYSZ(lmp) = (uint_t)dyn->d_un.d_val;
18237c478bd9Sstevel@tonic-gate 				break;
18247c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAY:
1825*56deab07SRod Evans 				FINIARRAY(lmp) = (Addr *)(dyn->d_un.d_ptr +
18267c478bd9Sstevel@tonic-gate 				    base);
18277c478bd9Sstevel@tonic-gate 				break;
18287c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAYSZ:
1829*56deab07SRod Evans 				FINIARRAYSZ(lmp) = (uint_t)dyn->d_un.d_val;
18307c478bd9Sstevel@tonic-gate 				break;
18317c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAY:
1832*56deab07SRod Evans 				PREINITARRAY(lmp) = (Addr *)(dyn->d_un.d_ptr +
18337c478bd9Sstevel@tonic-gate 				    base);
18347c478bd9Sstevel@tonic-gate 				break;
18357c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAYSZ:
1836*56deab07SRod Evans 				PREINITARRAYSZ(lmp) = (uint_t)dyn->d_un.d_val;
18377c478bd9Sstevel@tonic-gate 				break;
18387c478bd9Sstevel@tonic-gate 			case DT_RPATH:
18397c478bd9Sstevel@tonic-gate 			case DT_RUNPATH:
1840*56deab07SRod Evans 				rpath = dyn->d_un.d_val;
18417c478bd9Sstevel@tonic-gate 				break;
18427c478bd9Sstevel@tonic-gate 			case DT_FILTER:
1843*56deab07SRod Evans 				fltr = dyn->d_un.d_val;
184475e7992aSrie 				OBJFLTRNDX(lmp) = dynndx;
18457c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJSFLTR;
18467c478bd9Sstevel@tonic-gate 				break;
18477c478bd9Sstevel@tonic-gate 			case DT_AUXILIARY:
18487c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
1849*56deab07SRod Evans 					fltr = dyn->d_un.d_val;
185075e7992aSrie 					OBJFLTRNDX(lmp) = dynndx;
18517c478bd9Sstevel@tonic-gate 				}
18527c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJAFLTR;
18537c478bd9Sstevel@tonic-gate 				break;
18547c478bd9Sstevel@tonic-gate 			case DT_SUNW_FILTER:
18557c478bd9Sstevel@tonic-gate 				SYMSFLTRCNT(lmp)++;
18567c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMSFLTR;
18577c478bd9Sstevel@tonic-gate 				break;
18587c478bd9Sstevel@tonic-gate 			case DT_SUNW_AUXILIARY:
18597c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
18607c478bd9Sstevel@tonic-gate 					SYMAFLTRCNT(lmp)++;
18617c478bd9Sstevel@tonic-gate 				}
18627c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMAFLTR;
18637c478bd9Sstevel@tonic-gate 				break;
18647c478bd9Sstevel@tonic-gate 			case DT_DEPAUDIT:
18657c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUDIT))
1866*56deab07SRod Evans 					audit = dyn->d_un.d_val;
18677c478bd9Sstevel@tonic-gate 				break;
18687c478bd9Sstevel@tonic-gate 			case DT_CONFIG:
1869*56deab07SRod Evans 				cfile = dyn->d_un.d_val;
18707c478bd9Sstevel@tonic-gate 				break;
18717c478bd9Sstevel@tonic-gate 			case DT_DEBUG:
18727c478bd9Sstevel@tonic-gate 				/*
18737c478bd9Sstevel@tonic-gate 				 * DT_DEBUG entries are only created in
18747c478bd9Sstevel@tonic-gate 				 * dynamic objects that require an interpretor
18757c478bd9Sstevel@tonic-gate 				 * (ie. all dynamic executables and some shared
18767c478bd9Sstevel@tonic-gate 				 * objects), and provide for a hand-shake with
18777c478bd9Sstevel@tonic-gate 				 * debuggers.  This entry is initialized to
18787c478bd9Sstevel@tonic-gate 				 * zero by the link-editor.  If a debugger has
18797c478bd9Sstevel@tonic-gate 				 * us and updated this entry set the debugger
18807c478bd9Sstevel@tonic-gate 				 * flag, and finish initializing the debugging
18817c478bd9Sstevel@tonic-gate 				 * structure (see setup() also).  Switch off any
18827c478bd9Sstevel@tonic-gate 				 * configuration object use as most debuggers
18837c478bd9Sstevel@tonic-gate 				 * can't handle fixed dynamic executables as
18847c478bd9Sstevel@tonic-gate 				 * dependencies, and we can't handle requests
18857c478bd9Sstevel@tonic-gate 				 * like object padding for alternative objects.
18867c478bd9Sstevel@tonic-gate 				 */
1887*56deab07SRod Evans 				if (dyn->d_un.d_ptr)
18887c478bd9Sstevel@tonic-gate 					rtld_flags |=
18897c478bd9Sstevel@tonic-gate 					    (RT_FL_DEBUGGER | RT_FL_NOOBJALT);
1890*56deab07SRod Evans 				dyn->d_un.d_ptr = (Addr)&r_debug;
18917c478bd9Sstevel@tonic-gate 				break;
18927c478bd9Sstevel@tonic-gate 			case DT_VERNEED:
1893*56deab07SRod Evans 				VERNEED(lmp) = (Verneed *)(dyn->d_un.d_ptr +
18947c478bd9Sstevel@tonic-gate 				    base);
18957c478bd9Sstevel@tonic-gate 				break;
18967c478bd9Sstevel@tonic-gate 			case DT_VERNEEDNUM:
18977c478bd9Sstevel@tonic-gate 				/* LINTED */
1898*56deab07SRod Evans 				VERNEEDNUM(lmp) = (int)dyn->d_un.d_val;
18997c478bd9Sstevel@tonic-gate 				break;
19007c478bd9Sstevel@tonic-gate 			case DT_VERDEF:
1901*56deab07SRod Evans 				VERDEF(lmp) = (Verdef *)(dyn->d_un.d_ptr +
1902*56deab07SRod Evans 				    base);
19037c478bd9Sstevel@tonic-gate 				break;
19047c478bd9Sstevel@tonic-gate 			case DT_VERDEFNUM:
19057c478bd9Sstevel@tonic-gate 				/* LINTED */
1906*56deab07SRod Evans 				VERDEFNUM(lmp) = (int)dyn->d_un.d_val;
19077c478bd9Sstevel@tonic-gate 				break;
19083b41b08bSab 			case DT_VERSYM:
1909d840867fSab 				/*
1910d840867fSab 				 * The Solaris ld does not produce DT_VERSYM,
1911d840867fSab 				 * but the GNU ld does, in order to support
1912d840867fSab 				 * their style of versioning, which differs
1913d840867fSab 				 * from ours in some ways, while using the
1914d840867fSab 				 * same data structures. The presence of
1915d840867fSab 				 * DT_VERSYM therefore means that GNU
1916d840867fSab 				 * versioning rules apply to the given file.
1917d840867fSab 				 * If DT_VERSYM is not present, then Solaris
1918d840867fSab 				 * versioning rules apply.
1919d840867fSab 				 */
1920*56deab07SRod Evans 				VERSYM(lmp) = (Versym *)(dyn->d_un.d_ptr +
1921*56deab07SRod Evans 				    base);
19223b41b08bSab 				break;
19237c478bd9Sstevel@tonic-gate 			case DT_BIND_NOW:
1924*56deab07SRod Evans 				if ((dyn->d_un.d_val & DF_BIND_NOW) &&
1925*56deab07SRod Evans 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
19267c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
19277c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
19287c478bd9Sstevel@tonic-gate 				}
19297c478bd9Sstevel@tonic-gate 				break;
19307c478bd9Sstevel@tonic-gate 			case DT_FLAGS:
1931*56deab07SRod Evans 				FLAGS1(lmp) |= FL1_RT_DTFLAGS;
1932*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_SYMBOLIC)
19337c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_SYMBOLIC;
1934*56deab07SRod Evans 				if ((dyn->d_un.d_val & DF_BIND_NOW) &&
1935dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
19367c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
19377c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
19387c478bd9Sstevel@tonic-gate 				}
1939d326b23bSrie 				/*
1940d326b23bSrie 				 * Capture any static TLS use, and enforce that
1941d326b23bSrie 				 * this object be non-deletable.
1942d326b23bSrie 				 */
1943*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_STATIC_TLS) {
1944d326b23bSrie 					FLAGS1(lmp) |= FL1_RT_TLSSTAT;
1945d326b23bSrie 					MODE(lmp) |= RTLD_NODELETE;
1946d326b23bSrie 				}
19477c478bd9Sstevel@tonic-gate 				break;
19487c478bd9Sstevel@tonic-gate 			case DT_FLAGS_1:
1949*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_DISPRELPND)
19507c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_DISPREL;
1951*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_GROUP)
19527c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |=
19537c478bd9Sstevel@tonic-gate 					    (FLG_RT_SETGROUP | FLG_RT_HANDLE);
1954*56deab07SRod Evans 				if ((dyn->d_un.d_val & DF_1_NOW) &&
1955dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
19567c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
19577c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
19587c478bd9Sstevel@tonic-gate 				}
1959*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NODELETE)
19607c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NODELETE;
1961*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_INITFIRST)
19627c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INITFRST;
1963*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NOOPEN)
19647c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NOOPEN;
1965*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_LOADFLTR)
19667c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_LOADFLTR;
1967*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NODUMP)
19687c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NODUMP;
1969*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_CONFALT)
19707c478bd9Sstevel@tonic-gate 					crle = 1;
1971*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_DIRECT)
19729a411307Srie 					FLAGS1(lmp) |= FL1_RT_DIRECT;
1973*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NODEFLIB)
19747c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_NODEFLIB;
1975*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_ENDFILTEE)
19767c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_ENDFILTE;
1977*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_TRANS)
19787c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_TRANS;
1979*56deab07SRod Evans 
19807247f888Srie 				/*
19817247f888Srie 				 * Global auditing is only meaningful when
19827247f888Srie 				 * specified by the initiating object of the
19837247f888Srie 				 * process - typically the dynamic executable.
19847247f888Srie 				 * If this is the initiaiting object, its link-
19857247f888Srie 				 * map will not yet have been added to the
19867247f888Srie 				 * link-map list, and consequently the link-map
19877247f888Srie 				 * list is empty.  (see setup()).
19887247f888Srie 				 */
1989*56deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_GLOBAUDIT) {
19907247f888Srie 					if (lml_main.lm_head == 0)
19917247f888Srie 						FLAGS1(lmp) |= FL1_RT_GLOBAUD;
19927247f888Srie 					else
19937247f888Srie 						DBG_CALL(Dbg_audit_ignore(lmp));
19947247f888Srie 				}
19957247f888Srie 
19967c478bd9Sstevel@tonic-gate 				/*
19977c478bd9Sstevel@tonic-gate 				 * If this object identifies itself as an
19987c478bd9Sstevel@tonic-gate 				 * interposer, but relocation processing has
19997c478bd9Sstevel@tonic-gate 				 * already started, then demote it.  It's too
20007c478bd9Sstevel@tonic-gate 				 * late to guarantee complete interposition.
20017c478bd9Sstevel@tonic-gate 				 */
2002a953e2b1Srie 				/* BEGIN CSTYLED */
2003*56deab07SRod Evans 				if (dyn->d_un.d_val &
20049a411307Srie 				    (DF_1_INTERPOSE | DF_1_SYMINTPOSE)) {
20059a411307Srie 				    if (lml->lm_flags & LML_FLG_STARTREL) {
20065aefb655Srie 					DBG_CALL(Dbg_util_intoolate(lmp));
20077c478bd9Sstevel@tonic-gate 					if (lml->lm_flags & LML_FLG_TRC_ENABLE)
20087c478bd9Sstevel@tonic-gate 					    (void) printf(
20097c478bd9Sstevel@tonic-gate 						MSG_INTL(MSG_LDD_REL_ERR2),
20107c478bd9Sstevel@tonic-gate 						NAME(lmp));
2011*56deab07SRod Evans 				    } else if (dyn->d_un.d_val & DF_1_INTERPOSE)
20129a411307Srie 					FLAGS(lmp) |= FLG_RT_OBJINTPO;
20139a411307Srie 				    else
20149a411307Srie 					FLAGS(lmp) |= FLG_RT_SYMINTPO;
20157c478bd9Sstevel@tonic-gate 				}
2016a953e2b1Srie 				/* END CSTYLED */
20177c478bd9Sstevel@tonic-gate 				break;
20187c478bd9Sstevel@tonic-gate 			case DT_SYMINFO:
2019*56deab07SRod Evans 				SYMINFO(lmp) = (Syminfo *)(dyn->d_un.d_ptr +
20207c478bd9Sstevel@tonic-gate 				    base);
20217c478bd9Sstevel@tonic-gate 				break;
20227c478bd9Sstevel@tonic-gate 			case DT_SYMINENT:
2023*56deab07SRod Evans 				SYMINENT(lmp) = dyn->d_un.d_val;
20247c478bd9Sstevel@tonic-gate 				break;
20257c478bd9Sstevel@tonic-gate 			case DT_PLTPAD:
2026*56deab07SRod Evans 				PLTPAD(lmp) = (void *)(dyn->d_un.d_ptr + base);
20277c478bd9Sstevel@tonic-gate 				break;
20287c478bd9Sstevel@tonic-gate 			case DT_PLTPADSZ:
2029*56deab07SRod Evans 				pltpadsz = dyn->d_un.d_val;
20307c478bd9Sstevel@tonic-gate 				break;
20317c478bd9Sstevel@tonic-gate 			case DT_SUNW_RTLDINF:
20327c478bd9Sstevel@tonic-gate 				/*
203310a4fa49Srie 				 * Maintain a list of RTLDINFO structures.
203410a4fa49Srie 				 * Typically, libc is the only supplier, and
203510a4fa49Srie 				 * only one structure is provided.  However,
203610a4fa49Srie 				 * multiple suppliers and multiple structures
203710a4fa49Srie 				 * are supported.  For example, one structure
203810a4fa49Srie 				 * may provide thread_init, and another
203910a4fa49Srie 				 * structure may provide atexit reservations.
20407c478bd9Sstevel@tonic-gate 				 */
204110a4fa49Srie 				if ((rti = alist_append(&lml->lm_rti, 0,
2042*56deab07SRod Evans 				    sizeof (Rti_desc),
2043*56deab07SRod Evans 				    AL_CNT_RTLDINFO)) == NULL) {
20447c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
2045*56deab07SRod Evans 					return (NULL);
20467c478bd9Sstevel@tonic-gate 				}
204710a4fa49Srie 				rti->rti_lmp = lmp;
2048*56deab07SRod Evans 				rti->rti_info = (void *)(dyn->d_un.d_ptr +
2049*56deab07SRod Evans 				    base);
20507c478bd9Sstevel@tonic-gate 				break;
2051d579eb63Sab 			case DT_SUNW_SORTENT:
2052*56deab07SRod Evans 				SUNWSORTENT(lmp) = dyn->d_un.d_val;
2053d579eb63Sab 				break;
2054d579eb63Sab 			case DT_SUNW_SYMSORT:
2055d579eb63Sab 				SUNWSYMSORT(lmp) =
2056*56deab07SRod Evans 				    (void *)(dyn->d_un.d_ptr + base);
2057d579eb63Sab 				break;
2058d579eb63Sab 			case DT_SUNW_SYMSORTSZ:
2059*56deab07SRod Evans 				SUNWSYMSORTSZ(lmp) = dyn->d_un.d_val;
2060d579eb63Sab 				break;
20617c478bd9Sstevel@tonic-gate 			case DT_DEPRECATED_SPARC_REGISTER:
20627c478bd9Sstevel@tonic-gate 			case M_DT_REGISTER:
20637c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_REGSYMS;
20647c478bd9Sstevel@tonic-gate 				break;
20657c478bd9Sstevel@tonic-gate 			}
20667c478bd9Sstevel@tonic-gate 		}
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 		if (PLTPAD(lmp)) {
20697c478bd9Sstevel@tonic-gate 			if (pltpadsz == (Xword)0)
20707c478bd9Sstevel@tonic-gate 				PLTPAD(lmp) = 0;
20717c478bd9Sstevel@tonic-gate 			else
20727c478bd9Sstevel@tonic-gate 				PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) +
20737c478bd9Sstevel@tonic-gate 				    pltpadsz);
20747c478bd9Sstevel@tonic-gate 		}
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
20779039eeafSab 	/*
20789039eeafSab 	 * A dynsym contains only global functions. We want to have
20799039eeafSab 	 * a version of it that also includes local functions, so that
20809039eeafSab 	 * dladdr() will be able to report names for local functions
20819039eeafSab 	 * when used to generate a stack trace for a stripped file.
20829039eeafSab 	 * This version of the dynsym is provided via DT_SUNW_SYMTAB.
20839039eeafSab 	 *
20849039eeafSab 	 * In producing DT_SUNW_SYMTAB, ld uses a non-obvious trick
20859039eeafSab 	 * in order to avoid having to have two copies of the global
20869039eeafSab 	 * symbols held in DT_SYMTAB: The local symbols are placed in
20879039eeafSab 	 * a separate section than the globals in the dynsym, but the
20889039eeafSab 	 * linker conspires to put the data for these two sections adjacent
20899039eeafSab 	 * to each other. DT_SUNW_SYMTAB points at the top of the local
20909039eeafSab 	 * symbols, and DT_SUNW_SYMSZ is the combined length of both tables.
20919039eeafSab 	 *
20929039eeafSab 	 * If the two sections are not adjacent, then something went wrong
20939039eeafSab 	 * at link time. We use ASSERT to kill the process if this is
20949039eeafSab 	 * a debug build. In a production build, we will silently ignore
20959039eeafSab 	 * the presence of the .ldynsym and proceed. We can detect this
20969039eeafSab 	 * situation by checking to see that DT_SYMTAB lies in
20979039eeafSab 	 * the range given by DT_SUNW_SYMTAB/DT_SUNW_SYMSZ.
20989039eeafSab 	 */
20999039eeafSab 	if ((SUNWSYMTAB(lmp) != NULL) &&
21009039eeafSab 	    (((char *)SYMTAB(lmp) <= (char *)SUNWSYMTAB(lmp)) ||
21019039eeafSab 	    (((char *)SYMTAB(lmp) >=
21029039eeafSab 	    (SUNWSYMSZ(lmp) + (char *)SUNWSYMTAB(lmp)))))) {
21039039eeafSab 		ASSERT(0);
21049039eeafSab 		SUNWSYMTAB(lmp) = NULL;
21059039eeafSab 		SUNWSYMSZ(lmp) = 0;
21069039eeafSab 	}
21079039eeafSab 
21087c478bd9Sstevel@tonic-gate 	/*
21097c478bd9Sstevel@tonic-gate 	 * If configuration file use hasn't been disabled, and a configuration
21107c478bd9Sstevel@tonic-gate 	 * file hasn't already been set via an environment variable, see if any
21117c478bd9Sstevel@tonic-gate 	 * application specific configuration file is specified.  An LD_CONFIG
21127c478bd9Sstevel@tonic-gate 	 * setting is used first, but if this image was generated via crle(1)
21137c478bd9Sstevel@tonic-gate 	 * then a default configuration file is a fall-back.
21147c478bd9Sstevel@tonic-gate 	 */
21157c478bd9Sstevel@tonic-gate 	if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == 0)) {
21167c478bd9Sstevel@tonic-gate 		if (cfile)
21177c478bd9Sstevel@tonic-gate 			config->c_name = (const char *)(cfile +
21187c478bd9Sstevel@tonic-gate 			    (char *)STRTAB(lmp));
2119*56deab07SRod Evans 		else if (crle)
21207c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_CONFAPP;
21217c478bd9Sstevel@tonic-gate 	}
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 	if (rpath)
21247c478bd9Sstevel@tonic-gate 		RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp));
2125*56deab07SRod Evans 	if (fltr)
2126*56deab07SRod Evans 		REFNAME(lmp) = (char *)(fltr + (char *)STRTAB(lmp));
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	/*
21297c478bd9Sstevel@tonic-gate 	 * For Intel ABI compatibility.  It's possible that a JMPREL can be
21307c478bd9Sstevel@tonic-gate 	 * specified without any other relocations (e.g. a dynamic executable
21317c478bd9Sstevel@tonic-gate 	 * normally only contains .plt relocations).  If this is the case then
21327c478bd9Sstevel@tonic-gate 	 * no REL, RELSZ or RELENT will have been created.  For us to be able
21337c478bd9Sstevel@tonic-gate 	 * to traverse the .plt relocations under LD_BIND_NOW we need to know
21347c478bd9Sstevel@tonic-gate 	 * the RELENT for these relocations.  Refer to elf_reloc() for more
21357c478bd9Sstevel@tonic-gate 	 * details.
21367c478bd9Sstevel@tonic-gate 	 */
21377c478bd9Sstevel@tonic-gate 	if (!RELENT(lmp) && JMPREL(lmp))
2138*56deab07SRod Evans 		RELENT(lmp) = sizeof (M_RELOC);
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	/*
21417c478bd9Sstevel@tonic-gate 	 * Establish any per-object auditing.  If we're establishing `main's
21427c478bd9Sstevel@tonic-gate 	 * link-map its too early to go searching for audit objects so just
21437c478bd9Sstevel@tonic-gate 	 * hold the object name for later (see setup()).
21447c478bd9Sstevel@tonic-gate 	 */
21457c478bd9Sstevel@tonic-gate 	if (audit) {
21467c478bd9Sstevel@tonic-gate 		char	*cp = audit + (char *)STRTAB(lmp);
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 		if (*cp) {
21497c478bd9Sstevel@tonic-gate 			if (((AUDITORS(lmp) =
2150*56deab07SRod Evans 			    calloc(1, sizeof (Audit_desc))) == NULL) ||
2151*56deab07SRod Evans 			    ((AUDITORS(lmp)->ad_name = strdup(cp)) == NULL)) {
21527c478bd9Sstevel@tonic-gate 				remove_so(0, lmp);
2153*56deab07SRod Evans 				return (NULL);
21547c478bd9Sstevel@tonic-gate 			}
215541072f3cSrie 			if (lml_main.lm_head) {
21569aa23310Srie 				if (audit_setup(lmp, AUDITORS(lmp), 0,
21579aa23310Srie 				    in_nfavl) == 0) {
21587c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
2159*56deab07SRod Evans 					return (NULL);
21607c478bd9Sstevel@tonic-gate 				}
2161*56deab07SRod Evans 				AFLAGS(lmp) |= AUDITORS(lmp)->ad_flags;
21627c478bd9Sstevel@tonic-gate 				lml->lm_flags |= LML_FLG_LOCAUDIT;
21637c478bd9Sstevel@tonic-gate 			}
21647c478bd9Sstevel@tonic-gate 		}
21657c478bd9Sstevel@tonic-gate 	}
21667c478bd9Sstevel@tonic-gate 
2167*56deab07SRod Evans 	if (tphdr && (tls_assign(lml, lmp, tphdr) == 0)) {
21687c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
2169*56deab07SRod Evans 		return (NULL);
21707c478bd9Sstevel@tonic-gate 	}
21717c478bd9Sstevel@tonic-gate 
2172*56deab07SRod Evans 	if (cap)
2173*56deab07SRod Evans 		cap_assign(cap, lmp);
2174*56deab07SRod Evans 
21757c478bd9Sstevel@tonic-gate 	/*
21767c478bd9Sstevel@tonic-gate 	 * Add the mapped object to the end of the link map list.
21777c478bd9Sstevel@tonic-gate 	 */
21787c478bd9Sstevel@tonic-gate 	lm_append(lml, lmco, lmp);
2179*56deab07SRod Evans 
2180*56deab07SRod Evans 	/*
2181*56deab07SRod Evans 	 * Start the system loading in the ELF information we'll be processing.
2182*56deab07SRod Evans 	 */
2183*56deab07SRod Evans 	if (REL(lmp)) {
2184*56deab07SRod Evans 		(void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) +
2185*56deab07SRod Evans 		    (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp),
2186*56deab07SRod Evans 		    MADV_WILLNEED);
2187*56deab07SRod Evans 	}
21887c478bd9Sstevel@tonic-gate 	return (lmp);
21897c478bd9Sstevel@tonic-gate }
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate /*
21927c478bd9Sstevel@tonic-gate  * Assign hardware/software capabilities.
21937c478bd9Sstevel@tonic-gate  */
21947c478bd9Sstevel@tonic-gate void
21957c478bd9Sstevel@tonic-gate cap_assign(Cap *cap, Rt_map *lmp)
21967c478bd9Sstevel@tonic-gate {
21977c478bd9Sstevel@tonic-gate 	while (cap->c_tag != CA_SUNW_NULL) {
21987c478bd9Sstevel@tonic-gate 		switch (cap->c_tag) {
21997c478bd9Sstevel@tonic-gate 		case CA_SUNW_HW_1:
22007c478bd9Sstevel@tonic-gate 			HWCAP(lmp) = cap->c_un.c_val;
22017c478bd9Sstevel@tonic-gate 			break;
22027c478bd9Sstevel@tonic-gate 		case CA_SUNW_SF_1:
22037c478bd9Sstevel@tonic-gate 			SFCAP(lmp) = cap->c_un.c_val;
22047c478bd9Sstevel@tonic-gate 		}
22057c478bd9Sstevel@tonic-gate 		cap++;
22067c478bd9Sstevel@tonic-gate 	}
22077c478bd9Sstevel@tonic-gate }
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate /*
22107c478bd9Sstevel@tonic-gate  * Build full pathname of shared object from given directory name and filename.
22117c478bd9Sstevel@tonic-gate  */
22127c478bd9Sstevel@tonic-gate static char *
2213*56deab07SRod Evans elf_get_so(const char *dir, const char *file, size_t dlen, size_t flen)
22147c478bd9Sstevel@tonic-gate {
22157c478bd9Sstevel@tonic-gate 	static char	pname[PATH_MAX];
22167c478bd9Sstevel@tonic-gate 
2217*56deab07SRod Evans 	(void) strncpy(pname, dir, dlen);
2218*56deab07SRod Evans 	pname[dlen++] = '/';
2219*56deab07SRod Evans 	(void) strncpy(&pname[dlen], file, flen + 1);
22207c478bd9Sstevel@tonic-gate 	return (pname);
22217c478bd9Sstevel@tonic-gate }
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate /*
22247c478bd9Sstevel@tonic-gate  * The copy relocation is recorded in a copy structure which will be applied
22257c478bd9Sstevel@tonic-gate  * after all other relocations are carried out.  This provides for copying data
22267c478bd9Sstevel@tonic-gate  * that must be relocated itself (ie. pointers in shared objects).  This
22277c478bd9Sstevel@tonic-gate  * structure also provides a means of binding RTLD_GROUP dependencies to any
22287c478bd9Sstevel@tonic-gate  * copy relocations that have been taken from any group members.
22297c478bd9Sstevel@tonic-gate  *
22307c478bd9Sstevel@tonic-gate  * If the size of the .bss area available for the copy information is not the
22317c478bd9Sstevel@tonic-gate  * same as the source of the data inform the user if we're under ldd(1) control
22327c478bd9Sstevel@tonic-gate  * (this checking was only established in 5.3, so by only issuing an error via
22337c478bd9Sstevel@tonic-gate  * ldd(1) we maintain the standard set by previous releases).
22347c478bd9Sstevel@tonic-gate  */
22357c478bd9Sstevel@tonic-gate int
22367c478bd9Sstevel@tonic-gate elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym,
22377c478bd9Sstevel@tonic-gate     Rt_map *dlmp, const void *dadd)
22387c478bd9Sstevel@tonic-gate {
22397c478bd9Sstevel@tonic-gate 	Rel_copy	rc;
22407c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(rlmp);
22417c478bd9Sstevel@tonic-gate 
22427c478bd9Sstevel@tonic-gate 	rc.r_name = name;
22437c478bd9Sstevel@tonic-gate 	rc.r_rsym = rsym;		/* the new reference symbol and its */
22447c478bd9Sstevel@tonic-gate 	rc.r_rlmp = rlmp;		/*	associated link-map */
22457c478bd9Sstevel@tonic-gate 	rc.r_dlmp = dlmp;		/* the defining link-map */
22467c478bd9Sstevel@tonic-gate 	rc.r_dsym = dsym;		/* the original definition */
22477c478bd9Sstevel@tonic-gate 	rc.r_radd = radd;
22487c478bd9Sstevel@tonic-gate 	rc.r_dadd = dadd;
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	if (rsym->st_size > dsym->st_size)
22517c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)dsym->st_size;
22527c478bd9Sstevel@tonic-gate 	else
22537c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)rsym->st_size;
22547c478bd9Sstevel@tonic-gate 
2255cce0e03bSab 	if (alist_append(&COPY_R(dlmp), &rc, sizeof (Rel_copy),
2256*56deab07SRod Evans 	    AL_CNT_COPYREL) == NULL) {
22577c478bd9Sstevel@tonic-gate 		if (!(lml->lm_flags & LML_FLG_TRC_WARN))
22587c478bd9Sstevel@tonic-gate 			return (0);
22597c478bd9Sstevel@tonic-gate 		else
22607c478bd9Sstevel@tonic-gate 			return (1);
22617c478bd9Sstevel@tonic-gate 	}
22627c478bd9Sstevel@tonic-gate 	if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) {
2263cce0e03bSab 		if (aplist_append(&COPY_S(rlmp), dlmp,
2264cce0e03bSab 		    AL_CNT_COPYREL) == NULL) {
22657c478bd9Sstevel@tonic-gate 			if (!(lml->lm_flags & LML_FLG_TRC_WARN))
22667c478bd9Sstevel@tonic-gate 				return (0);
22677c478bd9Sstevel@tonic-gate 			else
22687c478bd9Sstevel@tonic-gate 				return (1);
22697c478bd9Sstevel@tonic-gate 		}
22707c478bd9Sstevel@tonic-gate 		FLAGS1(dlmp) |= FL1_RT_COPYTOOK;
22717c478bd9Sstevel@tonic-gate 	}
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate 	/*
22747c478bd9Sstevel@tonic-gate 	 * If we are tracing (ldd), warn the user if
22757c478bd9Sstevel@tonic-gate 	 *	1) the size from the reference symbol differs from the
22767c478bd9Sstevel@tonic-gate 	 *	   copy definition. We can only copy as much data as the
22777c478bd9Sstevel@tonic-gate 	 *	   reference (dynamic executables) entry allows.
22787c478bd9Sstevel@tonic-gate 	 *	2) the copy definition has STV_PROTECTED visibility.
22797c478bd9Sstevel@tonic-gate 	 */
22807c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
22817c478bd9Sstevel@tonic-gate 		if (rsym->st_size != dsym->st_size) {
22827c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF),
22835aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
22847c478bd9Sstevel@tonic-gate 			    NAME(rlmp), EC_XWORD(rsym->st_size),
22857c478bd9Sstevel@tonic-gate 			    NAME(dlmp), EC_XWORD(dsym->st_size));
22867c478bd9Sstevel@tonic-gate 			if (rsym->st_size > dsym->st_size)
22877c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA),
22887c478bd9Sstevel@tonic-gate 				    NAME(dlmp));
22897c478bd9Sstevel@tonic-gate 			else
22907c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC),
22917c478bd9Sstevel@tonic-gate 				    NAME(rlmp));
22927c478bd9Sstevel@tonic-gate 		}
22937c478bd9Sstevel@tonic-gate 
22947c478bd9Sstevel@tonic-gate 		if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) {
22957c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_PROT),
22965aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
2297a953e2b1Srie 			    NAME(dlmp));
22987c478bd9Sstevel@tonic-gate 		}
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate 
23015aefb655Srie 	DBG_CALL(Dbg_reloc_apply_val(lml, ELF_DBG_RTLD, (Xword)radd,
23025aefb655Srie 	    (Xword)rc.r_size));
23037c478bd9Sstevel@tonic-gate 	return (1);
23047c478bd9Sstevel@tonic-gate }
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate /*
23077c478bd9Sstevel@tonic-gate  * Determine the symbol location of an address within a link-map.  Look for
23087c478bd9Sstevel@tonic-gate  * the nearest symbol (whose value is less than or equal to the required
23097c478bd9Sstevel@tonic-gate  * address).  This is the object specific part of dladdr().
23107c478bd9Sstevel@tonic-gate  */
23117c478bd9Sstevel@tonic-gate static void
23127c478bd9Sstevel@tonic-gate elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags)
23137c478bd9Sstevel@tonic-gate {
23147c478bd9Sstevel@tonic-gate 	ulong_t		ndx, cnt, base, _value;
23156221fe92Sab 	Sym		*sym, *_sym = NULL;
23167c478bd9Sstevel@tonic-gate 	const char	*str;
23175343e1b3Sab 	int		_flags;
2318d579eb63Sab 	uint_t		*dynaddr_ndx;
2319d579eb63Sab 	uint_t		dynaddr_n = 0;
2320d579eb63Sab 	ulong_t		value;
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 	/*
23239039eeafSab 	 * If SUNWSYMTAB() is non-NULL, then it sees a special version of
23249039eeafSab 	 * the dynsym that starts with any local function symbols that exist in
23259039eeafSab 	 * the library and then moves to the data held in SYMTAB(). In this
23269039eeafSab 	 * case, SUNWSYMSZ tells us how long the symbol table is. The
23279039eeafSab 	 * availability of local function symbols will enhance the results
23289039eeafSab 	 * we can provide.
23299039eeafSab 	 *
2330d579eb63Sab 	 * If SUNWSYMTAB() is non-NULL, then there might also be a
2331d579eb63Sab 	 * SUNWSYMSORT() vector associated with it. SUNWSYMSORT() contains
2332d579eb63Sab 	 * an array of indices into SUNWSYMTAB, sorted by increasing
2333d579eb63Sab 	 * address. We can use this to do an O(log N) search instead of a
2334d579eb63Sab 	 * brute force search.
2335d579eb63Sab 	 *
23369039eeafSab 	 * If SUNWSYMTAB() is NULL, then SYMTAB() references a dynsym that
23379039eeafSab 	 * contains only global symbols. In that case, the length of
23389039eeafSab 	 * the symbol table comes from the nchain field of the related
23399039eeafSab 	 * symbol lookup hash table.
23407c478bd9Sstevel@tonic-gate 	 */
23417c478bd9Sstevel@tonic-gate 	str = STRTAB(lmp);
23429039eeafSab 	if (SUNWSYMSZ(lmp) == NULL) {
23439039eeafSab 		sym = SYMTAB(lmp);
23449039eeafSab 		/*
23459039eeafSab 		 * If we don't have a .hash table there are no symbols
23469039eeafSab 		 * to look at.
23479039eeafSab 		 */
23489039eeafSab 		if (HASH(lmp) == 0)
23499039eeafSab 			return;
23509039eeafSab 		cnt = HASH(lmp)[1];
23519039eeafSab 	} else {
23529039eeafSab 		sym = SUNWSYMTAB(lmp);
23539039eeafSab 		cnt = SUNWSYMSZ(lmp) / SYMENT(lmp);
2354d579eb63Sab 		dynaddr_ndx = SUNWSYMSORT(lmp);
2355d579eb63Sab 		if (dynaddr_ndx != NULL)
2356d579eb63Sab 			dynaddr_n = SUNWSYMSORTSZ(lmp) / SUNWSORTENT(lmp);
23579039eeafSab 	}
23587c478bd9Sstevel@tonic-gate 
23597c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_FIXED)
23607c478bd9Sstevel@tonic-gate 		base = 0;
23617c478bd9Sstevel@tonic-gate 	else
23627c478bd9Sstevel@tonic-gate 		base = ADDR(lmp);
23637c478bd9Sstevel@tonic-gate 
2364d579eb63Sab 	if (dynaddr_n > 0) {		/* Binary search */
2365d579eb63Sab 		long	low = 0, low_bnd;
2366d579eb63Sab 		long	high = dynaddr_n - 1, high_bnd;
2367d579eb63Sab 		long	mid;
2368d579eb63Sab 		Sym	*mid_sym;
23697c478bd9Sstevel@tonic-gate 
23709039eeafSab 		/*
2371d579eb63Sab 		 * Note that SUNWSYMSORT only contains symbols types that
2372d579eb63Sab 		 * supply memory addresses, so there's no need to check and
2373d579eb63Sab 		 * filter out any other types.
23749039eeafSab 		 */
2375d579eb63Sab 		low_bnd = low;
2376d579eb63Sab 		high_bnd = high;
2377d579eb63Sab 		while (low <= high) {
2378d579eb63Sab 			mid = (low + high) / 2;
2379d579eb63Sab 			mid_sym = &sym[dynaddr_ndx[mid]];
2380d579eb63Sab 			value = mid_sym->st_value + base;
2381d579eb63Sab 			if (addr < value) {
2382d579eb63Sab 				if ((sym[dynaddr_ndx[high]].st_value + base) >=
2383d579eb63Sab 				    addr)
2384d579eb63Sab 					high_bnd = high;
2385d579eb63Sab 				high = mid - 1;
2386d579eb63Sab 			} else if (addr > value) {
2387d579eb63Sab 				if ((sym[dynaddr_ndx[low]].st_value + base) <=
2388d579eb63Sab 				    addr)
2389d579eb63Sab 					low_bnd = low;
2390d579eb63Sab 				low = mid + 1;
2391d579eb63Sab 			} else {
2392d579eb63Sab 				_sym = mid_sym;
2393d579eb63Sab 				_value = value;
2394d579eb63Sab 				break;
2395d579eb63Sab 			}
2396d579eb63Sab 		}
2397d579eb63Sab 		/*
2398d579eb63Sab 		 * If the above didn't find it exactly, then we must
2399d579eb63Sab 		 * return the closest symbol with a value that doesn't
2400d579eb63Sab 		 * exceed the one we are looking for. If that symbol exists,
2401d579eb63Sab 		 * it will lie in the range bounded by low_bnd and
2402d579eb63Sab 		 * high_bnd. This is a linear search, but a short one.
2403d579eb63Sab 		 */
2404d579eb63Sab 		if (_sym == NULL) {
2405d579eb63Sab 			for (mid = low_bnd; mid <= high_bnd; mid++) {
2406d579eb63Sab 				mid_sym = &sym[dynaddr_ndx[mid]];
2407d579eb63Sab 				value = mid_sym->st_value + base;
2408d579eb63Sab 				if (addr >= value) {
2409d579eb63Sab 					_sym = mid_sym;
2410d579eb63Sab 					_value = value;
2411d579eb63Sab 				} else {
2412d579eb63Sab 					break;
2413d579eb63Sab 				}
2414d579eb63Sab 			}
2415d579eb63Sab 		}
2416d579eb63Sab 	} else {			/* Linear search */
2417d579eb63Sab 		for (_value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) {
2418d579eb63Sab 			/*
2419d579eb63Sab 			 * Skip expected symbol types that are not functions
2420d579eb63Sab 			 * or data:
2421d579eb63Sab 			 *	- A symbol table starts with an undefined symbol
2422d579eb63Sab 			 *		in slot 0. If we are using SUNWSYMTAB(),
2423d579eb63Sab 			 *		there will be a second undefined symbol
2424d579eb63Sab 			 *		right before the globals.
2425d579eb63Sab 			 *	- The local part of SUNWSYMTAB() contains a
2426d579eb63Sab 			 *		series of function symbols. Each section
2427d579eb63Sab 			 *		starts with an initial STT_FILE symbol.
2428d579eb63Sab 			 */
2429d579eb63Sab 			if ((sym->st_shndx == SHN_UNDEF) ||
2430d579eb63Sab 			    (ELF_ST_TYPE(sym->st_info) == STT_FILE))
2431d579eb63Sab 				continue;
24327c478bd9Sstevel@tonic-gate 
2433d579eb63Sab 			value = sym->st_value + base;
2434d579eb63Sab 			if (value > addr)
2435d579eb63Sab 				continue;
2436d579eb63Sab 			if (value < _value)
2437d579eb63Sab 				continue;
24387c478bd9Sstevel@tonic-gate 
2439d579eb63Sab 			_sym = sym;
2440d579eb63Sab 			_value = value;
24417c478bd9Sstevel@tonic-gate 
2442d579eb63Sab 			/*
2443d579eb63Sab 			 * Note, because we accept local and global symbols
2444d579eb63Sab 			 * we could find a section symbol that matches the
2445d579eb63Sab 			 * associated address, which means that the symbol
2446d579eb63Sab 			 * name will be null.  In this case continue the
2447d579eb63Sab 			 * search in case we can find a global symbol of
2448d579eb63Sab 			 * the same value.
2449d579eb63Sab 			 */
2450d579eb63Sab 			if ((value == addr) &&
2451d579eb63Sab 			    (ELF_ST_TYPE(sym->st_info) != STT_SECTION))
2452d579eb63Sab 				break;
2453d579eb63Sab 		}
24547c478bd9Sstevel@tonic-gate 	}
24557c478bd9Sstevel@tonic-gate 
24565343e1b3Sab 	_flags = flags & RTLD_DL_MASK;
24577c478bd9Sstevel@tonic-gate 	if (_sym) {
24587c478bd9Sstevel@tonic-gate 		if (_flags == RTLD_DL_SYMENT)
24597c478bd9Sstevel@tonic-gate 			*info = (void *)_sym;
24607c478bd9Sstevel@tonic-gate 		else if (_flags == RTLD_DL_LINKMAP)
24617c478bd9Sstevel@tonic-gate 			*info = (void *)lmp;
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 		dlip->dli_sname = str + _sym->st_name;
24647c478bd9Sstevel@tonic-gate 		dlip->dli_saddr = (void *)_value;
24655343e1b3Sab 	} else {
24665343e1b3Sab 		/*
24675343e1b3Sab 		 * addr lies between the beginning of the mapped segment and
24685343e1b3Sab 		 * the first global symbol. We have no symbol to return
24695343e1b3Sab 		 * and the caller requires one. We use _START_, the base
24705343e1b3Sab 		 * address of the mapping.
24715343e1b3Sab 		 */
24725343e1b3Sab 
24735343e1b3Sab 		if (_flags == RTLD_DL_SYMENT) {
24745343e1b3Sab 			/*
24755343e1b3Sab 			 * An actual symbol struct is needed, so we
24765343e1b3Sab 			 * construct one for _START_. To do this in a
24775343e1b3Sab 			 * fully accurate way requires a different symbol
24785343e1b3Sab 			 * for each mapped segment. This requires the
24795343e1b3Sab 			 * use of dynamic memory and a mutex. That's too much
24805343e1b3Sab 			 * plumbing for a fringe case of limited importance.
24815343e1b3Sab 			 *
24825343e1b3Sab 			 * Fortunately, we can simplify:
24835343e1b3Sab 			 *    - Only the st_size and st_info fields are useful
24845343e1b3Sab 			 *	outside of the linker internals. The others
24855343e1b3Sab 			 *	reference things that outside code cannot see,
24865343e1b3Sab 			 *	and can be set to 0.
24875343e1b3Sab 			 *    - It's just a label and there is no size
24885343e1b3Sab 			 *	to report. So, the size should be 0.
24895343e1b3Sab 			 * This means that only st_info needs a non-zero
24905343e1b3Sab 			 * (constant) value. A static struct will suffice.
24915343e1b3Sab 			 * It must be const (readonly) so the caller can't
24925343e1b3Sab 			 * change its meaning for subsequent callers.
24935343e1b3Sab 			 */
24945343e1b3Sab 			static const Sym fsym = { 0, 0, 0,
249537ffaf83SRod Evans 			    ELF_ST_INFO(STB_LOCAL, STT_OBJECT) };
24965343e1b3Sab 			*info = (void *) &fsym;
24975343e1b3Sab 		}
24985343e1b3Sab 
24995343e1b3Sab 		dlip->dli_sname = MSG_ORIG(MSG_SYM_START);
25005343e1b3Sab 		dlip->dli_saddr = (void *) ADDR(lmp);
25017c478bd9Sstevel@tonic-gate 	}
25027c478bd9Sstevel@tonic-gate }
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate static void
2505cce0e03bSab elf_lazy_cleanup(APlist *alp)
25067c478bd9Sstevel@tonic-gate {
2507cce0e03bSab 	Rt_map	*lmp;
2508cce0e03bSab 	Aliste	idx;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	/*
25117c478bd9Sstevel@tonic-gate 	 * Cleanup any link-maps added to this dynamic list and free it.
25127c478bd9Sstevel@tonic-gate 	 */
2513cce0e03bSab 	for (APLIST_TRAVERSE(alp, idx, lmp))
251475e7992aSrie 		FLAGS(lmp) &= ~FLG_RT_TMPLIST;
25157c478bd9Sstevel@tonic-gate 	free(alp);
25167c478bd9Sstevel@tonic-gate }
25177c478bd9Sstevel@tonic-gate 
25187c478bd9Sstevel@tonic-gate /*
251975e7992aSrie  * This routine is called as a last fall-back to search for a symbol from a
252075e7992aSrie  * standard relocation.  To maintain lazy loadings goal of reducing the number
25217c478bd9Sstevel@tonic-gate  * of objects mapped, any symbol search is first carried out using the objects
25227c478bd9Sstevel@tonic-gate  * that already exist in the process (either on a link-map list or handle).
25237c478bd9Sstevel@tonic-gate  * If a symbol can't be found, and lazy dependencies are still pending, this
25247c478bd9Sstevel@tonic-gate  * routine loads the dependencies in an attempt to locate the symbol.
25257c478bd9Sstevel@tonic-gate  *
25267c478bd9Sstevel@tonic-gate  * Only new objects are inspected as we will have already inspected presently
25277c478bd9Sstevel@tonic-gate  * loaded objects before calling this routine.  However, a new object may not
25287c478bd9Sstevel@tonic-gate  * be new - although the di_lmp might be zero, the object may have been mapped
25297c478bd9Sstevel@tonic-gate  * as someone elses dependency.  Thus there's a possibility of some symbol
25307c478bd9Sstevel@tonic-gate  * search duplication.
25317c478bd9Sstevel@tonic-gate  */
25327c478bd9Sstevel@tonic-gate Sym *
25339aa23310Srie elf_lazy_find_sym(Slookup *slp, Rt_map **_lmp, uint_t *binfo, int *in_nfavl)
25347c478bd9Sstevel@tonic-gate {
253537ffaf83SRod Evans 	Sym		*sym = NULL;
2536cce0e03bSab 	APlist		*alist = NULL;
2537cce0e03bSab 	Aliste		idx;
2538cce0e03bSab 	Rt_map		*lmp1, *lmp = slp->sl_imap;
25397c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
25407c478bd9Sstevel@tonic-gate 
254175e7992aSrie 	/*
254275e7992aSrie 	 * Generate a local list of new objects to process.  This list can grow
254375e7992aSrie 	 * as each object supplies its own lazy dependencies.
254475e7992aSrie 	 */
2545cce0e03bSab 	if (aplist_append(&alist, lmp, AL_CNT_LAZYFIND) == NULL)
2546cce0e03bSab 		return (NULL);
254775e7992aSrie 	FLAGS(lmp) |= FLG_RT_TMPLIST;
25487c478bd9Sstevel@tonic-gate 
2549cce0e03bSab 	for (APLIST_TRAVERSE(alist, idx, lmp1)) {
25507c478bd9Sstevel@tonic-gate 		uint_t	cnt = 0;
25517c478bd9Sstevel@tonic-gate 		Slookup	sl = *slp;
255275e7992aSrie 		Dyninfo	*dip, *pdip;
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 		/*
255575e7992aSrie 		 * Discard any relocation index from further symbol searches.
255675e7992aSrie 		 * This index will have already been used to trigger any
255775e7992aSrie 		 * necessary lazy-loads, and it might be because one of these
255875e7992aSrie 		 * lazy loads have failed that we're here performing this
255975e7992aSrie 		 * fallback.  By removing the relocation index we don't try
256075e7992aSrie 		 * and perform the same failed lazy loading activity again.
256175e7992aSrie 		 */
256275e7992aSrie 		sl.sl_rsymndx = 0;
256375e7992aSrie 
256475e7992aSrie 		/*
256575e7992aSrie 		 * Loop through the lazy DT_NEEDED entries examining each object
256675e7992aSrie 		 * for the required symbol.  If the symbol is not found, the
256775e7992aSrie 		 * object is in turn added to the local alist, so that the
256875e7992aSrie 		 * objects lazy DT_NEEDED entries can be examined.
25697c478bd9Sstevel@tonic-gate 		 */
2570cce0e03bSab 		lmp = lmp1;
257175e7992aSrie 		for (dip = DYNINFO(lmp), pdip = NULL; cnt < DYNINFOCNT(lmp);
257275e7992aSrie 		    cnt++, pdip = dip++) {
25737c478bd9Sstevel@tonic-gate 			Rt_map *nlmp;
25747c478bd9Sstevel@tonic-gate 
257575e7992aSrie 			if (((dip->di_flags & FLG_DI_LAZY) == 0) ||
25767c478bd9Sstevel@tonic-gate 			    dip->di_info)
25777c478bd9Sstevel@tonic-gate 				continue;
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 			/*
258075e7992aSrie 			 * If this object has already failed to lazy load, and
258175e7992aSrie 			 * we're still processing the same runtime linker
258275e7992aSrie 			 * operation that produced the failure, don't bother
258375e7992aSrie 			 * to try and load the object again.
258475e7992aSrie 			 */
258575e7992aSrie 			if ((dip->di_flags & FLG_DI_LAZYFAIL) && pdip &&
258675e7992aSrie 			    (pdip->di_flags & FLG_DI_POSFLAG1)) {
258775e7992aSrie 				if (pdip->di_info == (void *)ld_entry_cnt)
258875e7992aSrie 					continue;
258975e7992aSrie 
259075e7992aSrie 				dip->di_flags &= ~FLG_DI_LAZYFAIL;
259175e7992aSrie 				pdip->di_info = NULL;
259275e7992aSrie 			}
259375e7992aSrie 
259475e7992aSrie 			/*
259575e7992aSrie 			 * Try loading this lazy dependency.  If the object
259675e7992aSrie 			 * can't be loaded, consider this non-fatal and continue
259775e7992aSrie 			 * the search.  Lazy loaded dependencies need not exist
259875e7992aSrie 			 * and their loading should only turn out to be fatal
259975e7992aSrie 			 * if they are required to satisfy a relocation.
26007c478bd9Sstevel@tonic-gate 			 *
26017c478bd9Sstevel@tonic-gate 			 * If the file is already loaded and relocated we must
26027c478bd9Sstevel@tonic-gate 			 * still inspect it for symbols, even though it might
26037c478bd9Sstevel@tonic-gate 			 * have already been searched.  This lazy load operation
26047c478bd9Sstevel@tonic-gate 			 * might have promoted the permissions of the object,
26057c478bd9Sstevel@tonic-gate 			 * and thus made the object applicable for this symbol
26067c478bd9Sstevel@tonic-gate 			 * search, whereas before the object might have been
26077c478bd9Sstevel@tonic-gate 			 * skipped.
26087c478bd9Sstevel@tonic-gate 			 */
26099aa23310Srie 			if ((nlmp = elf_lazy_load(lmp, &sl, cnt,
26109aa23310Srie 			    name, in_nfavl)) == 0)
26117c478bd9Sstevel@tonic-gate 				continue;
26127c478bd9Sstevel@tonic-gate 
26137c478bd9Sstevel@tonic-gate 			/*
26147c478bd9Sstevel@tonic-gate 			 * If this object isn't yet a part of the dynamic list
26157c478bd9Sstevel@tonic-gate 			 * then inspect it for the symbol.  If the symbol isn't
26167c478bd9Sstevel@tonic-gate 			 * found add the object to the dynamic list so that we
26177c478bd9Sstevel@tonic-gate 			 * can inspect its dependencies.
26187c478bd9Sstevel@tonic-gate 			 */
261975e7992aSrie 			if (FLAGS(nlmp) & FLG_RT_TMPLIST)
26207c478bd9Sstevel@tonic-gate 				continue;
26217c478bd9Sstevel@tonic-gate 
26227c478bd9Sstevel@tonic-gate 			sl.sl_imap = nlmp;
26239aa23310Srie 			if (sym = LM_LOOKUP_SYM(sl.sl_cmap)(&sl, _lmp,
26249aa23310Srie 			    binfo, in_nfavl))
26257c478bd9Sstevel@tonic-gate 				break;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 			/*
26287c478bd9Sstevel@tonic-gate 			 * Some dlsym() operations are already traversing a
26297c478bd9Sstevel@tonic-gate 			 * link-map (dlopen(0)), and thus there's no need to
26307c478bd9Sstevel@tonic-gate 			 * build our own dynamic dependency list.
26317c478bd9Sstevel@tonic-gate 			 */
26327c478bd9Sstevel@tonic-gate 			if ((sl.sl_flags & LKUP_NODESCENT) == 0) {
2633cce0e03bSab 				if (aplist_append(&alist, nlmp,
2634*56deab07SRod Evans 				    AL_CNT_LAZYFIND) == NULL) {
26357c478bd9Sstevel@tonic-gate 					elf_lazy_cleanup(alist);
2636*56deab07SRod Evans 					return (NULL);
26377c478bd9Sstevel@tonic-gate 				}
263875e7992aSrie 				FLAGS(nlmp) |= FLG_RT_TMPLIST;
26397c478bd9Sstevel@tonic-gate 			}
26407c478bd9Sstevel@tonic-gate 		}
26417c478bd9Sstevel@tonic-gate 		if (sym)
26427c478bd9Sstevel@tonic-gate 			break;
26437c478bd9Sstevel@tonic-gate 	}
26447c478bd9Sstevel@tonic-gate 
26457c478bd9Sstevel@tonic-gate 	elf_lazy_cleanup(alist);
26467c478bd9Sstevel@tonic-gate 	return (sym);
26477c478bd9Sstevel@tonic-gate }
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate /*
26507c478bd9Sstevel@tonic-gate  * Warning message for bad r_offset.
26517c478bd9Sstevel@tonic-gate  */
26527c478bd9Sstevel@tonic-gate void
26537c478bd9Sstevel@tonic-gate elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset,
26547c478bd9Sstevel@tonic-gate     ulong_t rsymndx)
26557c478bd9Sstevel@tonic-gate {
265637ffaf83SRod Evans 	const char	*name = NULL;
26575aefb655Srie 	Lm_list		*lml = LIST(lmp);
26587c478bd9Sstevel@tonic-gate 	int		trace;
26597c478bd9Sstevel@tonic-gate 
26605aefb655Srie 	if ((lml->lm_flags & LML_FLG_TRC_ENABLE) &&
26617c478bd9Sstevel@tonic-gate 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
26625aefb655Srie 	    (lml->lm_flags & LML_FLG_TRC_VERBOSE)))
26637c478bd9Sstevel@tonic-gate 		trace = 1;
26647c478bd9Sstevel@tonic-gate 	else
26657c478bd9Sstevel@tonic-gate 		trace = 0;
26667c478bd9Sstevel@tonic-gate 
26675aefb655Srie 	if ((trace == 0) && (DBG_ENABLED == 0))
26687c478bd9Sstevel@tonic-gate 		return;
26697c478bd9Sstevel@tonic-gate 
26707c478bd9Sstevel@tonic-gate 	if (rsymndx) {
26717c478bd9Sstevel@tonic-gate 		Sym	*symref = (Sym *)((ulong_t)SYMTAB(lmp) +
2672a953e2b1Srie 		    (rsymndx * SYMENT(lmp)));
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(symref->st_info) != STB_LOCAL)
26757c478bd9Sstevel@tonic-gate 			name = (char *)(STRTAB(lmp) + symref->st_name);
26767c478bd9Sstevel@tonic-gate 	}
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate 	if (name == 0)
2679*56deab07SRod Evans 		name = MSG_INTL(MSG_STR_UNKNOWN);
26807c478bd9Sstevel@tonic-gate 
26817c478bd9Sstevel@tonic-gate 	if (trace) {
26827c478bd9Sstevel@tonic-gate 		const char *rstr;
26837c478bd9Sstevel@tonic-gate 
26845aefb655Srie 		rstr = _conv_reloc_type((uint_t)rtype);
26857c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name,
26867c478bd9Sstevel@tonic-gate 		    EC_ADDR(roffset));
26877c478bd9Sstevel@tonic-gate 		return;
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 
26905aefb655Srie 	Dbg_reloc_error(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel, name);
26917c478bd9Sstevel@tonic-gate }
2692d326b23bSrie 
2693d326b23bSrie /*
2694d326b23bSrie  * Resolve a static TLS relocation.
2695d326b23bSrie  */
2696d326b23bSrie long
2697d326b23bSrie elf_static_tls(Rt_map *lmp, Sym *sym, void *rel, uchar_t rtype, char *name,
2698d326b23bSrie     ulong_t roffset, long value)
2699d326b23bSrie {
2700d326b23bSrie 	Lm_list	*lml = LIST(lmp);
2701d326b23bSrie 
2702d326b23bSrie 	/*
2703d326b23bSrie 	 * Relocations against a static TLS block have limited support once
2704d326b23bSrie 	 * process initialization has completed.  Any error condition should be
2705d326b23bSrie 	 * discovered by testing for DF_STATIC_TLS as part of loading an object,
2706d326b23bSrie 	 * however individual relocations are tested in case the dynamic flag
2707d326b23bSrie 	 * had not been set when this object was built.
2708d326b23bSrie 	 */
2709d326b23bSrie 	if (PTTLS(lmp) == 0) {
2710d326b23bSrie 		DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
2711d326b23bSrie 		    M_REL_SHT_TYPE, rel, NULL, name));
2712d326b23bSrie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
2713d326b23bSrie 		    _conv_reloc_type((uint_t)rtype), NAME(lmp),
2714d326b23bSrie 		    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
2715d326b23bSrie 		return (0);
2716d326b23bSrie 	}
2717d326b23bSrie 
2718d326b23bSrie 	/*
2719d326b23bSrie 	 * If no static TLS has been set aside for this object, determine if
2720d326b23bSrie 	 * any can be obtained.  Enforce that any object using static TLS is
2721d326b23bSrie 	 * non-deletable.
2722d326b23bSrie 	 */
2723d326b23bSrie 	if (TLSSTATOFF(lmp) == 0) {
2724d326b23bSrie 		FLAGS1(lmp) |= FL1_RT_TLSSTAT;
2725d326b23bSrie 		MODE(lmp) |= RTLD_NODELETE;
2726d326b23bSrie 
2727d326b23bSrie 		if (tls_assign(lml, lmp, PTTLS(lmp)) == 0) {
2728d326b23bSrie 			DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
2729d326b23bSrie 			    M_REL_SHT_TYPE, rel, NULL, name));
2730d326b23bSrie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
2731d326b23bSrie 			    _conv_reloc_type((uint_t)rtype), NAME(lmp),
2732d326b23bSrie 			    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
2733d326b23bSrie 			return (0);
2734d326b23bSrie 		}
2735d326b23bSrie 	}
2736d326b23bSrie 
2737d326b23bSrie 	/*
2738d326b23bSrie 	 * Typically, a static TLS offset is maintained as a symbols value.
2739d326b23bSrie 	 * For local symbols that are not apart of the dynamic symbol table,
2740d326b23bSrie 	 * the TLS relocation points to a section symbol, and the static TLS
2741d326b23bSrie 	 * offset was deposited in the associated GOT table.  Make sure the GOT
2742d326b23bSrie 	 * is cleared, so that the value isn't reused in do_reloc().
2743d326b23bSrie 	 */
2744d326b23bSrie 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
2745d326b23bSrie 		if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION)) {
2746d326b23bSrie 			value = *(long *)roffset;
2747d326b23bSrie 			*(long *)roffset = 0;
2748d326b23bSrie 		} else {
2749d326b23bSrie 			value = sym->st_value;
2750d326b23bSrie 		}
2751d326b23bSrie 	}
2752d326b23bSrie 	return (-(TLSSTATOFF(lmp) - value));
2753d326b23bSrie }
2754dae2dfb7Srie 
2755dae2dfb7Srie /*
2756dae2dfb7Srie  * If the symbol is not found and the reference was not to a weak symbol, report
2757dae2dfb7Srie  * an error.  Weak references may be unresolved.
2758dae2dfb7Srie  */
2759dae2dfb7Srie int
2760dae2dfb7Srie elf_reloc_error(Rt_map *lmp, const char *name, void *rel, uint_t binfo)
2761dae2dfb7Srie {
2762dae2dfb7Srie 	Lm_list	*lml = LIST(lmp);
2763dae2dfb7Srie 
2764dae2dfb7Srie 	/*
2765dae2dfb7Srie 	 * Under crle(1), relocation failures are ignored.
2766dae2dfb7Srie 	 */
2767dae2dfb7Srie 	if (lml->lm_flags & LML_FLG_IGNRELERR)
2768dae2dfb7Srie 		return (1);
2769dae2dfb7Srie 
2770dae2dfb7Srie 	/*
2771dae2dfb7Srie 	 * Under ldd(1), unresolved references are reported.  However, if the
2772dae2dfb7Srie 	 * original reference is EXTERN or PARENT these references are ignored
2773dae2dfb7Srie 	 * unless ldd's -p option is in effect.
2774dae2dfb7Srie 	 */
2775dae2dfb7Srie 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
2776dae2dfb7Srie 		if (((binfo & DBG_BINFO_REF_MSK) == 0) ||
2777dae2dfb7Srie 		    ((lml->lm_flags & LML_FLG_TRC_NOPAREXT) != 0)) {
2778dae2dfb7Srie 			(void) printf(MSG_INTL(MSG_LDD_SYM_NFOUND),
2779dae2dfb7Srie 			    demangle(name), NAME(lmp));
2780dae2dfb7Srie 		}
2781dae2dfb7Srie 		return (1);
2782dae2dfb7Srie 	}
2783dae2dfb7Srie 
2784dae2dfb7Srie 	/*
2785dae2dfb7Srie 	 * Otherwise, the unresolved references is fatal.
2786dae2dfb7Srie 	 */
2787dae2dfb7Srie 	DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel,
2788dae2dfb7Srie 	    NULL, name));
2789dae2dfb7Srie 	eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
2790dae2dfb7Srie 	    demangle(name));
2791dae2dfb7Srie 
2792dae2dfb7Srie 	return (0);
2793dae2dfb7Srie }
2794*56deab07SRod Evans 
2795*56deab07SRod Evans /*
2796*56deab07SRod Evans  * Generic relative relocation function.
2797*56deab07SRod Evans  */
2798*56deab07SRod Evans inline static ulong_t
2799*56deab07SRod Evans _elf_reloc_relative(ulong_t rbgn, ulong_t base, Rt_map *lmp, APlist **textrel)
2800*56deab07SRod Evans {
2801*56deab07SRod Evans 	mmapobj_result_t	*mpp;
2802*56deab07SRod Evans 	ulong_t			roffset;
2803*56deab07SRod Evans 
2804*56deab07SRod Evans 	roffset = ((M_RELOC *)rbgn)->r_offset;
2805*56deab07SRod Evans 	roffset += base;
2806*56deab07SRod Evans 
2807*56deab07SRod Evans 	/*
2808*56deab07SRod Evans 	 * If this relocation is against an address that is not associated with
2809*56deab07SRod Evans 	 * a mapped segment, fall back to the generic relocation loop to
2810*56deab07SRod Evans 	 * collect the associated error.
2811*56deab07SRod Evans 	 */
2812*56deab07SRod Evans 	if ((mpp = find_segment((caddr_t)roffset, lmp)) == NULL)
2813*56deab07SRod Evans 		return (0);
2814*56deab07SRod Evans 
2815*56deab07SRod Evans 	/*
2816*56deab07SRod Evans 	 * If this relocation is against a segment that does not provide write
2817*56deab07SRod Evans 	 * access, set the write permission for all non-writable mappings.
2818*56deab07SRod Evans 	 */
2819*56deab07SRod Evans 	if (((mpp->mr_prot & PROT_WRITE) == 0) && textrel &&
2820*56deab07SRod Evans 	    ((set_prot(lmp, mpp, 1) == 0) ||
2821*56deab07SRod Evans 	    (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL)))
2822*56deab07SRod Evans 		return (0);
2823*56deab07SRod Evans 
2824*56deab07SRod Evans 	/*
2825*56deab07SRod Evans 	 * Perform the actual relocation.  Note, for backward compatibility,
2826*56deab07SRod Evans 	 * SPARC relocations are added to the offset contents (there was a time
2827*56deab07SRod Evans 	 * when the offset was used to contain the addend, rather than using
2828*56deab07SRod Evans 	 * the addend itself).
2829*56deab07SRod Evans 	 */
2830*56deab07SRod Evans #if	defined(__sparc)
2831*56deab07SRod Evans 	*((ulong_t *)roffset) += base + ((M_RELOC *)rbgn)->r_addend;
2832*56deab07SRod Evans #elif	defined(__amd64)
2833*56deab07SRod Evans 	*((ulong_t *)roffset) = base + ((M_RELOC *)rbgn)->r_addend;
2834*56deab07SRod Evans #else
2835*56deab07SRod Evans 	*((ulong_t *)roffset) += base;
2836*56deab07SRod Evans #endif
2837*56deab07SRod Evans 	return (1);
2838*56deab07SRod Evans }
2839*56deab07SRod Evans 
2840*56deab07SRod Evans /*
2841*56deab07SRod Evans  * When a generic relocation loop realizes that it's dealing with relative
2842*56deab07SRod Evans  * relocations, but no DT_RELCOUNT .dynamic tag is present, this tighter loop
2843*56deab07SRod Evans  * is entered as an optimization.
2844*56deab07SRod Evans  */
2845*56deab07SRod Evans ulong_t
2846*56deab07SRod Evans elf_reloc_relative(ulong_t rbgn, ulong_t rend, ulong_t rsize, ulong_t base,
2847*56deab07SRod Evans     Rt_map *lmp, APlist **textrel)
2848*56deab07SRod Evans {
2849*56deab07SRod Evans 	char	rtype;
2850*56deab07SRod Evans 
2851*56deab07SRod Evans 	do {
2852*56deab07SRod Evans 		if (_elf_reloc_relative(rbgn, base, lmp, textrel) == 0)
2853*56deab07SRod Evans 			break;
2854*56deab07SRod Evans 
2855*56deab07SRod Evans 		rbgn += rsize;
2856*56deab07SRod Evans 		if (rbgn >= rend)
2857*56deab07SRod Evans 			break;
2858*56deab07SRod Evans 
2859*56deab07SRod Evans 		/*
2860*56deab07SRod Evans 		 * Make sure the next type is a relative relocation.
2861*56deab07SRod Evans 		 */
2862*56deab07SRod Evans 		rtype = ELF_R_TYPE(((M_RELOC *)rbgn)->r_info, M_MACH);
2863*56deab07SRod Evans 
2864*56deab07SRod Evans 	} while (rtype == M_R_RELATIVE);
2865*56deab07SRod Evans 
2866*56deab07SRod Evans 	return (rbgn);
2867*56deab07SRod Evans }
2868*56deab07SRod Evans 
2869*56deab07SRod Evans /*
2870*56deab07SRod Evans  * This is the tightest loop for RELATIVE relocations for those objects built
2871*56deab07SRod Evans  * with the DT_RELACOUNT .dynamic entry.
2872*56deab07SRod Evans  */
2873*56deab07SRod Evans ulong_t
2874*56deab07SRod Evans elf_reloc_relative_count(ulong_t rbgn, ulong_t rcount, ulong_t rsize,
2875*56deab07SRod Evans     ulong_t base, Rt_map *lmp, APlist **textrel)
2876*56deab07SRod Evans {
2877*56deab07SRod Evans 	for (; rcount; rcount--) {
2878*56deab07SRod Evans 		if (_elf_reloc_relative(rbgn, base, lmp, textrel) == 0)
2879*56deab07SRod Evans 			break;
2880*56deab07SRod Evans 
2881*56deab07SRod Evans 		rbgn += rsize;
2882*56deab07SRod Evans 	}
2883*56deab07SRod Evans 	return (rbgn);
2884*56deab07SRod Evans }
2885