xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/elf.c (revision 08278a5e)
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 /*
2320272c2eSAli Bahrami  * Copyright 2010 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"
4856deab07SRod 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  */
5456deab07SRod Evans static Spath_defn _elf_def_dirs[] = {
557c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
5656deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIB_64),		MSG_PTH_LIB_64_SIZE },
5756deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIB_64),		MSG_PTH_USRLIB_64_SIZE },
587c478bd9Sstevel@tonic-gate #else
5956deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIB),		MSG_PTH_LIB_SIZE },
6056deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIB),		MSG_PTH_USRLIB_SIZE },
617c478bd9Sstevel@tonic-gate #endif
6256deab07SRod Evans 	{ 0, 0 }
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
6556deab07SRod Evans static Spath_defn _elf_sec_dirs[] = {
667c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
6756deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIBSE_64),		MSG_PTH_LIBSE_64_SIZE },
6856deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIBSE_64),	MSG_PTH_USRLIBSE_64_SIZE },
697c478bd9Sstevel@tonic-gate #else
7056deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_LIBSE),		MSG_PTH_LIBSE_SIZE },
7156deab07SRod Evans 	{ MSG_ORIG(MSG_PTH_USRLIBSE),		MSG_PTH_USRLIBSE_SIZE },
727c478bd9Sstevel@tonic-gate #endif
7356deab07SRod Evans 	{ 0, 0 }
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
7656deab07SRod Evans Alist	*elf_def_dirs = NULL;
7756deab07SRod Evans Alist	*elf_sec_dirs = NULL;
7856deab07SRod 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);
8356deab07SRod Evans static Addr	elf_entry_point(void);
8456deab07SRod Evans static int	elf_fix_name(const char *, Rt_map *, Alist **, Aliste, uint_t);
8556deab07SRod Evans static Alist	**elf_get_def_dirs(void);
8656deab07SRod Evans static Alist	**elf_get_sec_dirs(void);
8756deab07SRod 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 = {
9456deab07SRod Evans 	elf_verify,
9556deab07SRod Evans 	elf_new_lmp,
9656deab07SRod Evans 	elf_entry_point,
977c478bd9Sstevel@tonic-gate 	elf_needed,
987c478bd9Sstevel@tonic-gate 	lookup_sym,
997c478bd9Sstevel@tonic-gate 	elf_reloc,
10056deab07SRod Evans 	elf_get_def_dirs,
10156deab07SRod Evans 	elf_get_sec_dirs,
1027c478bd9Sstevel@tonic-gate 	elf_fix_name,
1037c478bd9Sstevel@tonic-gate 	elf_get_so,
1047c478bd9Sstevel@tonic-gate 	elf_dladdr,
10556deab07SRod Evans 	dlsym_handle
1067c478bd9Sstevel@tonic-gate };
1077c478bd9Sstevel@tonic-gate 
10856deab07SRod Evans /*
10956deab07SRod Evans  * Default and secure dependency search paths.
11056deab07SRod Evans  */
11156deab07SRod Evans static Alist **
11256deab07SRod Evans elf_get_def_dirs()
11356deab07SRod Evans {
11456deab07SRod Evans 	if (elf_def_dirs == NULL)
11556deab07SRod Evans 		set_dirs(&elf_def_dirs, _elf_def_dirs, LA_SER_DEFAULT);
11656deab07SRod Evans 	return (&elf_def_dirs);
11756deab07SRod Evans }
11856deab07SRod Evans 
11956deab07SRod Evans static Alist **
12056deab07SRod Evans elf_get_sec_dirs()
12156deab07SRod Evans {
12256deab07SRod Evans 	if (elf_sec_dirs == NULL)
12356deab07SRod Evans 		set_dirs(&elf_sec_dirs, _elf_sec_dirs, LA_SER_SECURE);
12456deab07SRod Evans 	return (&elf_sec_dirs);
12556deab07SRod Evans }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate /*
1287c478bd9Sstevel@tonic-gate  * Redefine NEEDED name if necessary.
1297c478bd9Sstevel@tonic-gate  */
13056deab07SRod Evans static int
13156deab07SRod Evans elf_fix_name(const char *name, Rt_map *clmp, Alist **alpp, Aliste alni,
13256deab07SRod 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 */
14756deab07SRod Evans 		Pdesc	*pdp;
1487c478bd9Sstevel@tonic-gate 
1495aefb655Srie 		DBG_CALL(Dbg_file_fixname(LIST(clmp), name,
1505aefb655Srie 		    MSG_ORIG(MSG_PTH_LIBSYS)));
151dde769a2SRod Evans 		if ((pdp = alist_append(alpp, NULL, sizeof (Pdesc),
152dde769a2SRod Evans 		    alni)) == NULL)
1537c478bd9Sstevel@tonic-gate 			return (0);
15456deab07SRod Evans 
15556deab07SRod Evans 		pdp->pd_pname = (char *)MSG_ORIG(MSG_PTH_LIBSYS);
15656deab07SRod Evans 		pdp->pd_plen = MSG_PTH_LIBSYS_SIZE;
15756deab07SRod Evans 		pdp->pd_flags = PD_FLG_PNSLASH;
15856deab07SRod Evans 
15956deab07SRod Evans 		return (1);
1607c478bd9Sstevel@tonic-gate 	}
1617c478bd9Sstevel@tonic-gate 
16256deab07SRod Evans 	return (expand_paths(clmp, name, alpp, alni, orig, 0));
16356deab07SRod Evans }
16456deab07SRod Evans 
16556deab07SRod Evans /*
166*08278a5eSRod Evans  * Determine whether this object requires capabilities.
16756deab07SRod Evans  */
168*08278a5eSRod Evans inline static int
16956deab07SRod Evans elf_cap_check(Fdesc *fdp, Ehdr *ehdr, Rej_desc *rej)
17056deab07SRod Evans {
17156deab07SRod Evans 	Phdr	*phdr;
172*08278a5eSRod Evans 	Cap	*cap = NULL;
173*08278a5eSRod Evans 	Dyn	*dyn = NULL;
174*08278a5eSRod Evans 	char	*str = NULL;
175*08278a5eSRod Evans 	Addr	base;
17656deab07SRod Evans 	int	cnt;
17756deab07SRod Evans 
178*08278a5eSRod Evans 	/*
179*08278a5eSRod Evans 	 * If this is a shared object, the base address of the shared object is
180*08278a5eSRod Evans 	 * added to all address values defined within the object.  Otherwise, if
181*08278a5eSRod Evans 	 * this is an executable, all object addresses are used as is.
182*08278a5eSRod Evans 	 */
183*08278a5eSRod Evans 	if (ehdr->e_type == ET_EXEC)
184*08278a5eSRod Evans 		base = 0;
185*08278a5eSRod Evans 	else
186*08278a5eSRod Evans 		base = (Addr)ehdr;
187*08278a5eSRod Evans 
18856deab07SRod Evans 	/* LINTED */
18956deab07SRod Evans 	phdr = (Phdr *)((char *)ehdr + ehdr->e_phoff);
19056deab07SRod Evans 	for (cnt = 0; cnt < ehdr->e_phnum; cnt++, phdr++) {
191*08278a5eSRod Evans 		if (phdr->p_type == PT_DYNAMIC) {
192*08278a5eSRod Evans 			/* LINTED */
193*08278a5eSRod Evans 			dyn = (Dyn *)((uintptr_t)phdr->p_vaddr + base);
194*08278a5eSRod Evans 		} else if (phdr->p_type == PT_SUNWCAP) {
195*08278a5eSRod Evans 			/* LINTED */
196*08278a5eSRod Evans 			cap = (Cap *)((uintptr_t)phdr->p_vaddr + base);
197*08278a5eSRod Evans 		}
198*08278a5eSRod Evans 	}
19956deab07SRod Evans 
200*08278a5eSRod Evans 	if (cap) {
201*08278a5eSRod Evans 		/*
202*08278a5eSRod Evans 		 * From the .dynamic section, determine the associated string
203*08278a5eSRod Evans 		 * table.  Required for CA_SUNW_MACH and CA_SUNW_PLAT
204*08278a5eSRod Evans 		 * processing.
205*08278a5eSRod Evans 		 */
206*08278a5eSRod Evans 		while (dyn) {
207*08278a5eSRod Evans 			if (dyn->d_tag == DT_NULL) {
208*08278a5eSRod Evans 				break;
209*08278a5eSRod Evans 			} else if (dyn->d_tag == DT_STRTAB) {
210*08278a5eSRod Evans 				str = (char *)(dyn->d_un.d_ptr + base);
211*08278a5eSRod Evans 				break;
21256deab07SRod Evans 			}
213*08278a5eSRod Evans 			dyn++;
21456deab07SRod Evans 		}
21556deab07SRod Evans 	}
216*08278a5eSRod Evans 
217*08278a5eSRod Evans 	/*
218*08278a5eSRod Evans 	 * Establish any alternative capabilities, and validate this object
219*08278a5eSRod Evans 	 * if it defines it's own capabilities information.
220*08278a5eSRod Evans 	 */
221*08278a5eSRod Evans 	return (cap_check_fdesc(fdp, cap, str, rej));
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /*
2257c478bd9Sstevel@tonic-gate  * Determine if we have been given an ELF file and if so determine if the file
2267c478bd9Sstevel@tonic-gate  * is compatible.  Returns 1 if true, else 0 and sets the reject descriptor
2277c478bd9Sstevel@tonic-gate  * with associated error information.
2287c478bd9Sstevel@tonic-gate  */
22956deab07SRod Evans Fct *
23056deab07SRod Evans elf_verify(caddr_t addr, size_t size, Fdesc *fdp, const char *name,
23156deab07SRod Evans     Rej_desc *rej)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	Ehdr	*ehdr;
23456deab07SRod Evans 	char	*caddr = (char *)addr;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	/*
2377c478bd9Sstevel@tonic-gate 	 * Determine if we're an elf file.  If not simply return, we don't set
2387c478bd9Sstevel@tonic-gate 	 * any rejection information as this test allows use to scroll through
2397c478bd9Sstevel@tonic-gate 	 * the objects we support (ELF, AOUT).
2407c478bd9Sstevel@tonic-gate 	 */
24156deab07SRod Evans 	if (size < sizeof (Ehdr) ||
24256deab07SRod Evans 	    caddr[EI_MAG0] != ELFMAG0 ||
24356deab07SRod Evans 	    caddr[EI_MAG1] != ELFMAG1 ||
24456deab07SRod Evans 	    caddr[EI_MAG2] != ELFMAG2 ||
24556deab07SRod Evans 	    caddr[EI_MAG3] != ELFMAG3) {
24656deab07SRod Evans 		return (NULL);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
2507c478bd9Sstevel@tonic-gate 	 * Check class and encoding.
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	/* LINTED */
25356deab07SRod Evans 	ehdr = (Ehdr *)addr;
2547c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_CLASS] != M_CLASS) {
2557c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_CLASS;
2567c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_CLASS];
25756deab07SRod Evans 		return (NULL);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	if (ehdr->e_ident[EI_DATA] != M_DATA) {
2607c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_DATA;
2617c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA];
26256deab07SRod Evans 		return (NULL);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 	if ((ehdr->e_type != ET_REL) && (ehdr->e_type != ET_EXEC) &&
2657c478bd9Sstevel@tonic-gate 	    (ehdr->e_type != ET_DYN)) {
2667c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_TYPE;
2677c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_type;
26856deab07SRod Evans 		return (NULL);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
27256deab07SRod Evans 	 * Verify ELF version.
2737c478bd9Sstevel@tonic-gate 	 */
2747c478bd9Sstevel@tonic-gate 	if (ehdr->e_version > EV_CURRENT) {
2757c478bd9Sstevel@tonic-gate 		rej->rej_type = SGS_REJ_VERSION;
2767c478bd9Sstevel@tonic-gate 		rej->rej_info = (uint_t)ehdr->e_version;
27756deab07SRod Evans 		return (NULL);
2787c478bd9Sstevel@tonic-gate 	}
27956deab07SRod Evans 
28056deab07SRod Evans 	/*
28156deab07SRod Evans 	 * Verify machine specific flags.
28256deab07SRod Evans 	 */
28356deab07SRod Evans 	if (elf_mach_flags_check(rej, ehdr) == 0)
28456deab07SRod Evans 		return (NULL);
28556deab07SRod Evans 
28656deab07SRod Evans 	/*
287*08278a5eSRod Evans 	 * Verify any capability requirements.  Note, if this object is a shared
288*08278a5eSRod Evans 	 * object that is explicitly defined on the ldd(1) command line, and it
289*08278a5eSRod Evans 	 * contains an incompatible capabilities requirement, then inform the
290*08278a5eSRod Evans 	 * user, but continue processing.
29156deab07SRod Evans 	 */
29256deab07SRod Evans 	if (elf_cap_check(fdp, ehdr, rej) == 0) {
29356deab07SRod Evans 		Rt_map	*lmp = lml_main.lm_head;
29456deab07SRod Evans 
295dde769a2SRod Evans 		if ((lml_main.lm_flags & LML_FLG_TRC_LDDSTUB) && lmp &&
296dde769a2SRod Evans 		    (FLAGS1(lmp) & FL1_RT_LDDSTUB) && (NEXT(lmp) == NULL)) {
297*08278a5eSRod Evans 			/* LINTED */
298*08278a5eSRod Evans 			(void) printf(MSG_INTL(ldd_warn[rej->rej_type]), name,
299*08278a5eSRod Evans 			    rej->rej_str);
30056deab07SRod Evans 			return (&elf_fct);
30156deab07SRod Evans 		}
30256deab07SRod Evans 		return (NULL);
30356deab07SRod Evans 	}
30456deab07SRod Evans 	return (&elf_fct);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate  * The runtime linker employs lazy loading to provide the libraries needed for
3097c478bd9Sstevel@tonic-gate  * debugging, preloading .o's and dldump().  As these are seldom used, the
3107c478bd9Sstevel@tonic-gate  * standard startup of ld.so.1 doesn't initialize all the information necessary
3117c478bd9Sstevel@tonic-gate  * to perform plt relocation on ld.so.1's link-map.  The first time lazy loading
3127c478bd9Sstevel@tonic-gate  * is called we get here to perform these initializations:
3137c478bd9Sstevel@tonic-gate  *
3142017c965SRod Evans  *  -	elf_needed() is called to set up the DYNINFO() indexes for each lazy
3157c478bd9Sstevel@tonic-gate  *	dependency.  Typically, for all other objects, this is called during
3167c478bd9Sstevel@tonic-gate  *	analyze_so(), but as ld.so.1 is set-contained we skip this processing.
3177c478bd9Sstevel@tonic-gate  *
3182017c965SRod Evans  *  -	For intel, ld.so.1's JMPSLOT relocations need relative updates. These
3197c478bd9Sstevel@tonic-gate  *	are by default skipped thus delaying all relative relocation processing
3207c478bd9Sstevel@tonic-gate  * 	on every invocation of ld.so.1.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate int
3237c478bd9Sstevel@tonic-gate elf_rtld_load()
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	Lm_list	*lml = &lml_rtld;
3267c478bd9Sstevel@tonic-gate 	Rt_map	*lmp = lml->lm_head;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_PLTREL)
3297c478bd9Sstevel@tonic-gate 		return (1);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * As we need to refer to the DYNINFO() information, insure that it has
3337c478bd9Sstevel@tonic-gate 	 * been initialized.
3347c478bd9Sstevel@tonic-gate 	 */
3359aa23310Srie 	if (elf_needed(lml, ALIST_OFF_DATA, lmp, NULL) == 0)
3367c478bd9Sstevel@tonic-gate 		return (0);
3377c478bd9Sstevel@tonic-gate 
33802ca3e02Srie #if	defined(__i386)
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * This is a kludge to give ld.so.1 a performance benefit on i386.
3417c478bd9Sstevel@tonic-gate 	 * It's based around two factors.
3427c478bd9Sstevel@tonic-gate 	 *
3432017c965SRod Evans 	 *  -	JMPSLOT relocations (PLT's) actually need a relative relocation
3447c478bd9Sstevel@tonic-gate 	 *	applied to the GOT entry so that they can find PLT0.
3457c478bd9Sstevel@tonic-gate 	 *
3462017c965SRod Evans 	 *  -	ld.so.1 does not exercise *any* PLT's before it has made a call
3477c478bd9Sstevel@tonic-gate 	 *	to elf_lazy_load().  This is because all dynamic dependencies
3487c478bd9Sstevel@tonic-gate 	 * 	are recorded as lazy dependencies.
3497c478bd9Sstevel@tonic-gate 	 */
35056deab07SRod Evans 	(void) elf_reloc_relative_count((ulong_t)JMPREL(lmp),
3517c478bd9Sstevel@tonic-gate 	    (ulong_t)(PLTRELSZ(lmp) / RELENT(lmp)), (ulong_t)RELENT(lmp),
35256deab07SRod Evans 	    (ulong_t)ADDR(lmp), lmp, NULL);
3537c478bd9Sstevel@tonic-gate #endif
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	lml->lm_flags |= LML_FLG_PLTREL;
3567c478bd9Sstevel@tonic-gate 	return (1);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * Lazy load an object.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate Rt_map *
3639aa23310Srie elf_lazy_load(Rt_map *clmp, Slookup *slp, uint_t ndx, const char *sym,
3642017c965SRod Evans     uint_t flags, Grp_hdl **hdl, int *in_nfavl)
3657c478bd9Sstevel@tonic-gate {
36656deab07SRod Evans 	Alist		*palp = NULL;
367dde769a2SRod Evans 	Rt_map		*nlmp;
36875e7992aSrie 	Dyninfo		*dip = &DYNINFO(clmp)[ndx], *pdip;
3697c478bd9Sstevel@tonic-gate 	const char	*name;
3707c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
3717c478bd9Sstevel@tonic-gate 	Aliste		lmco;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * If this dependency has already been processed, we're done.
3757c478bd9Sstevel@tonic-gate 	 */
37656deab07SRod Evans 	if (((nlmp = (Rt_map *)dip->di_info) != NULL) ||
37775e7992aSrie 	    (dip->di_flags & FLG_DI_LDD_DONE))
3787c478bd9Sstevel@tonic-gate 		return (nlmp);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/*
38175e7992aSrie 	 * If we're running under ldd(1), indicate that this dependency has been
38275e7992aSrie 	 * processed (see test above).  It doesn't matter whether the object is
38375e7992aSrie 	 * successfully loaded or not, this flag simply ensures that we don't
38475e7992aSrie 	 * repeatedly attempt to load an object that has already failed to load.
38575e7992aSrie 	 * To do so would create multiple failure diagnostics for the same
38675e7992aSrie 	 * object under ldd(1).
38775e7992aSrie 	 */
38875e7992aSrie 	if (lml->lm_flags & LML_FLG_TRC_ENABLE)
38975e7992aSrie 		dip->di_flags |= FLG_DI_LDD_DONE;
39075e7992aSrie 
39175e7992aSrie 	/*
39275e7992aSrie 	 * Determine the initial dependency name.
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	name = STRTAB(clmp) + DYN(clmp)[ndx].d_un.d_val;
3955aefb655Srie 	DBG_CALL(Dbg_file_lazyload(clmp, name, sym));
3967c478bd9Sstevel@tonic-gate 
39775e7992aSrie 	/*
39875e7992aSrie 	 * If this object needs to establish its own group, make sure a handle
39975e7992aSrie 	 * is created.
40075e7992aSrie 	 */
4017c478bd9Sstevel@tonic-gate 	if (dip->di_flags & FLG_DI_GROUP)
4022017c965SRod Evans 		flags |= (FLG_RT_SETGROUP | FLG_RT_PUBHDL);
4037c478bd9Sstevel@tonic-gate 
40475e7992aSrie 	/*
40575e7992aSrie 	 * Lazy dependencies are identified as DT_NEEDED entries with a
40675e7992aSrie 	 * DF_P1_LAZYLOAD flag in the previous DT_POSFLAG_1 element.  The
40775e7992aSrie 	 * dynamic information element that corresponds to the DT_POSFLAG_1
40875e7992aSrie 	 * entry is free, and thus used to store the present entrance
40975e7992aSrie 	 * identifier.  This identifier is used to prevent multiple attempts to
41075e7992aSrie 	 * load a failed lazy loadable dependency within the same runtime linker
41175e7992aSrie 	 * operation.  However, future attempts to reload this dependency are
41275e7992aSrie 	 * still possible.
41375e7992aSrie 	 */
41475e7992aSrie 	if (ndx && (pdip = dip - 1) && (pdip->di_flags & FLG_DI_POSFLAG1))
41575e7992aSrie 		pdip->di_info = (void *)slp->sl_id;
41675e7992aSrie 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * Expand the requested name if necessary.
4197c478bd9Sstevel@tonic-gate 	 */
42056deab07SRod Evans 	if (elf_fix_name(name, clmp, &palp, AL_CNT_NEEDED, 0) == 0)
42156deab07SRod Evans 		return (NULL);
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/*
424dde769a2SRod Evans 	 * Establish a link-map control list for this request.
4257c478bd9Sstevel@tonic-gate 	 */
426dde769a2SRod Evans 	if ((lmco = create_cntl(lml, 0)) == NULL) {
427dde769a2SRod Evans 		remove_plist(&palp, 1);
428dde769a2SRod Evans 		return (NULL);
429dde769a2SRod Evans 	}
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	/*
4327c478bd9Sstevel@tonic-gate 	 * Load the associated object.
4337c478bd9Sstevel@tonic-gate 	 */
4347c478bd9Sstevel@tonic-gate 	dip->di_info = nlmp =
4352017c965SRod Evans 	    load_one(lml, lmco, palp, clmp, MODE(clmp), flags, hdl, in_nfavl);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Remove any expanded pathname infrastructure.  Reduce the pending lazy
4397c478bd9Sstevel@tonic-gate 	 * dependency count of the caller, together with the link-map lists
4407c478bd9Sstevel@tonic-gate 	 * count of objects that still have lazy dependencies pending.
4417c478bd9Sstevel@tonic-gate 	 */
44256deab07SRod Evans 	remove_plist(&palp, 1);
4437c478bd9Sstevel@tonic-gate 	if (--LAZY(clmp) == 0)
4447c478bd9Sstevel@tonic-gate 		LIST(clmp)->lm_lazy--;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	/*
44702ca3e02Srie 	 * Finish processing the objects associated with this request, and
44802ca3e02Srie 	 * create an association between the caller and this dependency.
4497c478bd9Sstevel@tonic-gate 	 */
45075e7992aSrie 	if (nlmp && ((bind_one(clmp, nlmp, BND_NEEDED) == 0) ||
45156deab07SRod Evans 	    ((nlmp = analyze_lmc(lml, lmco, nlmp, in_nfavl)) == NULL) ||
4529aa23310Srie 	    (relocate_lmc(lml, lmco, clmp, nlmp, in_nfavl) == 0)))
45356deab07SRod Evans 		dip->di_info = nlmp = NULL;
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
45602ca3e02Srie 	 * If this lazyload has failed, and we've created a new link-map
45702ca3e02Srie 	 * control list to which this request has added objects, then remove
45802ca3e02Srie 	 * all the objects that have been associated to this request.
4597c478bd9Sstevel@tonic-gate 	 */
460481bba9eSRod Evans 	if ((nlmp == NULL) && (lmco != ALIST_OFF_DATA))
461481bba9eSRod Evans 		remove_lmc(lml, clmp, lmco, name);
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	/*
464dde769a2SRod Evans 	 * Remove any temporary link-map control list.
4657c478bd9Sstevel@tonic-gate 	 */
466481bba9eSRod Evans 	if (lmco != ALIST_OFF_DATA)
4677c478bd9Sstevel@tonic-gate 		remove_cntl(lml, lmco);
4687c478bd9Sstevel@tonic-gate 
46975e7992aSrie 	/*
47075e7992aSrie 	 * If this lazy loading failed, record the fact, and bump the lazy
47175e7992aSrie 	 * counts.
47275e7992aSrie 	 */
47356deab07SRod Evans 	if (nlmp == NULL) {
47475e7992aSrie 		dip->di_flags |= FLG_DI_LAZYFAIL;
47575e7992aSrie 		if (LAZY(clmp)++ == 0)
47675e7992aSrie 			LIST(clmp)->lm_lazy++;
47775e7992aSrie 	}
47875e7992aSrie 
4797c478bd9Sstevel@tonic-gate 	return (nlmp);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * Return the entry point of the ELF executable.
4847c478bd9Sstevel@tonic-gate  */
48556deab07SRod Evans static Addr
48656deab07SRod Evans elf_entry_point(void)
4877c478bd9Sstevel@tonic-gate {
48856deab07SRod Evans 	Rt_map	*lmp = lml_main.lm_head;
48956deab07SRod Evans 	Ehdr	*ehdr = (Ehdr *)ADDR(lmp);
49056deab07SRod Evans 	Addr	addr = (Addr)(ehdr->e_entry);
4917c478bd9Sstevel@tonic-gate 
49256deab07SRod Evans 	if ((FLAGS(lmp) & FLG_RT_FIXED) == 0)
49356deab07SRod Evans 		addr += ADDR(lmp);
4947c478bd9Sstevel@tonic-gate 
49556deab07SRod Evans 	return (addr);
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate /*
4997c478bd9Sstevel@tonic-gate  * Determine if a dependency requires a particular version and if so verify
5007c478bd9Sstevel@tonic-gate  * that the version exists in the dependency.
5017c478bd9Sstevel@tonic-gate  */
50256deab07SRod Evans int
5037c478bd9Sstevel@tonic-gate elf_verify_vers(const char *name, Rt_map *clmp, Rt_map *nlmp)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	Verneed		*vnd = VERNEED(clmp);
5067c478bd9Sstevel@tonic-gate 	int		_num, num = VERNEEDNUM(clmp);
5077c478bd9Sstevel@tonic-gate 	char		*cstrs = (char *)STRTAB(clmp);
5087c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(clmp);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	/*
5117c478bd9Sstevel@tonic-gate 	 * Traverse the callers version needed information and determine if any
5127c478bd9Sstevel@tonic-gate 	 * specific versions are required from the dependency.
5137c478bd9Sstevel@tonic-gate 	 */
5145aefb655Srie 	DBG_CALL(Dbg_ver_need_title(LIST(clmp), NAME(clmp)));
5157c478bd9Sstevel@tonic-gate 	for (_num = 1; _num <= num; _num++,
5167c478bd9Sstevel@tonic-gate 	    vnd = (Verneed *)((Xword)vnd + vnd->vn_next)) {
5177c478bd9Sstevel@tonic-gate 		Half		cnt = vnd->vn_cnt;
5187c478bd9Sstevel@tonic-gate 		Vernaux		*vnap;
5197c478bd9Sstevel@tonic-gate 		char		*nstrs, *need;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 		/*
5227c478bd9Sstevel@tonic-gate 		 * Determine if a needed entry matches this dependency.
5237c478bd9Sstevel@tonic-gate 		 */
5247c478bd9Sstevel@tonic-gate 		need = (char *)(cstrs + vnd->vn_file);
5257c478bd9Sstevel@tonic-gate 		if (strcmp(name, need) != 0)
5267c478bd9Sstevel@tonic-gate 			continue;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_TRC_VERBOSE) &&
5297c478bd9Sstevel@tonic-gate 		    ((FLAGS1(clmp) & FL1_RT_LDDSTUB) == 0))
5307c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_VER_FIND), name);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		/*
5337c478bd9Sstevel@tonic-gate 		 * Validate that each version required actually exists in the
5347c478bd9Sstevel@tonic-gate 		 * dependency.
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		nstrs = (char *)STRTAB(nlmp);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		for (vnap = (Vernaux *)((Xword)vnd + vnd->vn_aux); cnt;
5397c478bd9Sstevel@tonic-gate 		    cnt--, vnap = (Vernaux *)((Xword)vnap + vnap->vna_next)) {
5407c478bd9Sstevel@tonic-gate 			char		*version, *define;
5417c478bd9Sstevel@tonic-gate 			Verdef		*vdf = VERDEF(nlmp);
5427c478bd9Sstevel@tonic-gate 			ulong_t		_num, num = VERDEFNUM(nlmp);
5437c478bd9Sstevel@tonic-gate 			int		found = 0;
5447c478bd9Sstevel@tonic-gate 
545090a8d9eSAli Bahrami 			/*
546090a8d9eSAli Bahrami 			 * Skip validation of versions that are marked
547090a8d9eSAli Bahrami 			 * INFO. This optimization is used for versions
548090a8d9eSAli Bahrami 			 * that are inherited by another version. Verification
549090a8d9eSAli Bahrami 			 * of the inheriting version is sufficient.
550090a8d9eSAli Bahrami 			 *
551090a8d9eSAli Bahrami 			 * Such versions are recorded in the object for the
552090a8d9eSAli Bahrami 			 * benefit of VERSYM entries that refer to them. This
553*08278a5eSRod Evans 			 * provides a purely diagnostic benefit.
554090a8d9eSAli Bahrami 			 */
555090a8d9eSAli Bahrami 			if (vnap->vna_flags & VER_FLG_INFO)
556090a8d9eSAli Bahrami 				continue;
557090a8d9eSAli Bahrami 
5587c478bd9Sstevel@tonic-gate 			version = (char *)(cstrs + vnap->vna_name);
5595aefb655Srie 			DBG_CALL(Dbg_ver_need_entry(lml, 0, need, version));
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 			for (_num = 1; _num <= num; _num++,
5627c478bd9Sstevel@tonic-gate 			    vdf = (Verdef *)((Xword)vdf + vdf->vd_next)) {
5637c478bd9Sstevel@tonic-gate 				Verdaux		*vdap;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 				if (vnap->vna_hash != vdf->vd_hash)
5667c478bd9Sstevel@tonic-gate 					continue;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 				vdap = (Verdaux *)((Xword)vdf + vdf->vd_aux);
5697c478bd9Sstevel@tonic-gate 				define = (char *)(nstrs + vdap->vda_name);
5707c478bd9Sstevel@tonic-gate 				if (strcmp(version, define) != 0)
5717c478bd9Sstevel@tonic-gate 					continue;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 				found++;
5747c478bd9Sstevel@tonic-gate 				break;
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 			/*
5787c478bd9Sstevel@tonic-gate 			 * If we're being traced print out any matched version
5797c478bd9Sstevel@tonic-gate 			 * when the verbose (-v) option is in effect.  Always
5807c478bd9Sstevel@tonic-gate 			 * print any unmatched versions.
5817c478bd9Sstevel@tonic-gate 			 */
5827c478bd9Sstevel@tonic-gate 			if (lml->lm_flags & LML_FLG_TRC_ENABLE) {
583a953e2b1Srie 				/* BEGIN CSTYLED */
5847c478bd9Sstevel@tonic-gate 				if (found) {
5857c478bd9Sstevel@tonic-gate 				    if (!(lml->lm_flags & LML_FLG_TRC_VERBOSE))
5867c478bd9Sstevel@tonic-gate 					continue;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_ORIG(MSG_LDD_VER_FOUND),
5897c478bd9Sstevel@tonic-gate 					need, version, NAME(nlmp));
5907c478bd9Sstevel@tonic-gate 				} else {
5917c478bd9Sstevel@tonic-gate 				    if (rtld_flags & RT_FL_SILENCERR)
5927c478bd9Sstevel@tonic-gate 					continue;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_VER_NFOUND),
5957c478bd9Sstevel@tonic-gate 					need, version);
5967c478bd9Sstevel@tonic-gate 				}
597a953e2b1Srie 				/* END CSTYLED */
5987c478bd9Sstevel@tonic-gate 				continue;
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 			/*
6027c478bd9Sstevel@tonic-gate 			 * If the version hasn't been found then this is a
6037c478bd9Sstevel@tonic-gate 			 * candidate for a fatal error condition.  Weak
6047c478bd9Sstevel@tonic-gate 			 * version definition requirements are silently
6057c478bd9Sstevel@tonic-gate 			 * ignored.  Also, if the image inspected for a version
6067c478bd9Sstevel@tonic-gate 			 * definition has no versioning recorded at all then
6077c478bd9Sstevel@tonic-gate 			 * silently ignore this (this provides better backward
6087c478bd9Sstevel@tonic-gate 			 * compatibility to old images created prior to
6097c478bd9Sstevel@tonic-gate 			 * versioning being available).  Both of these skipped
6107c478bd9Sstevel@tonic-gate 			 * diagnostics are available under tracing (see above).
6117c478bd9Sstevel@tonic-gate 			 */
6127c478bd9Sstevel@tonic-gate 			if ((found == 0) && (num != 0) &&
6137c478bd9Sstevel@tonic-gate 			    (!(vnap->vna_flags & VER_FLG_WEAK))) {
6145aefb655Srie 				eprintf(lml, ERR_FATAL,
6155aefb655Srie 				    MSG_INTL(MSG_VER_NFOUND), need, version,
6165aefb655Srie 				    NAME(clmp));
6177c478bd9Sstevel@tonic-gate 				return (0);
6187c478bd9Sstevel@tonic-gate 			}
6197c478bd9Sstevel@tonic-gate 		}
6207c478bd9Sstevel@tonic-gate 	}
62120272c2eSAli Bahrami 	DBG_CALL(Dbg_ver_need_done(lml));
6227c478bd9Sstevel@tonic-gate 	return (1);
6237c478bd9Sstevel@tonic-gate }
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Search through the dynamic section for DT_NEEDED entries and perform one
6277c478bd9Sstevel@tonic-gate  * of two functions.  If only the first argument is specified then load the
6287c478bd9Sstevel@tonic-gate  * defined shared object, otherwise add the link map representing the defined
6297c478bd9Sstevel@tonic-gate  * link map the the dlopen list.
6307c478bd9Sstevel@tonic-gate  */
6317c478bd9Sstevel@tonic-gate static int
6329aa23310Srie elf_needed(Lm_list *lml, Aliste lmco, Rt_map *clmp, int *in_nfavl)
6337c478bd9Sstevel@tonic-gate {
63456deab07SRod Evans 	Alist		*palp = NULL;
63575e7992aSrie 	Dyn		*dyn, *pdyn;
6367c478bd9Sstevel@tonic-gate 	ulong_t		ndx = 0;
63775e7992aSrie 	uint_t		lazy, flags;
6387c478bd9Sstevel@tonic-gate 	Word		lmflags = lml->lm_flags;
6397c478bd9Sstevel@tonic-gate 	Word		lmtflags = lml->lm_tflags;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	/*
6427c478bd9Sstevel@tonic-gate 	 * Process each shared object on needed list.
6437c478bd9Sstevel@tonic-gate 	 */
644481bba9eSRod Evans 	if (DYN(clmp) == NULL)
6457c478bd9Sstevel@tonic-gate 		return (1);
6467c478bd9Sstevel@tonic-gate 
64775e7992aSrie 	for (dyn = (Dyn *)DYN(clmp), pdyn = NULL; dyn->d_tag != DT_NULL;
64875e7992aSrie 	    pdyn = dyn++, ndx++) {
6497c478bd9Sstevel@tonic-gate 		Dyninfo	*dip = &DYNINFO(clmp)[ndx];
650481bba9eSRod Evans 		Rt_map	*nlmp = NULL;
6517c478bd9Sstevel@tonic-gate 		char	*name;
6527c478bd9Sstevel@tonic-gate 		int	silent = 0;
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		switch (dyn->d_tag) {
6557c478bd9Sstevel@tonic-gate 		case DT_POSFLAG_1:
65675e7992aSrie 			dip->di_flags |= FLG_DI_POSFLAG1;
6577c478bd9Sstevel@tonic-gate 			continue;
6587c478bd9Sstevel@tonic-gate 		case DT_NEEDED:
6597c478bd9Sstevel@tonic-gate 		case DT_USED:
66075e7992aSrie 			lazy = flags = 0;
6617c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_NEEDED;
66275e7992aSrie 
66375e7992aSrie 			if (pdyn && (pdyn->d_tag == DT_POSFLAG_1)) {
66475e7992aSrie 				if ((pdyn->d_un.d_val & DF_P1_LAZYLOAD) &&
66575e7992aSrie 				    ((lmtflags & LML_TFLG_NOLAZYLD) == 0)) {
66675e7992aSrie 					dip->di_flags |= FLG_DI_LAZY;
66775e7992aSrie 					lazy = 1;
66875e7992aSrie 				}
66975e7992aSrie 				if (pdyn->d_un.d_val & DF_P1_GROUPPERM) {
67075e7992aSrie 					dip->di_flags |= FLG_DI_GROUP;
67175e7992aSrie 					flags =
6722017c965SRod Evans 					    (FLG_RT_SETGROUP | FLG_RT_PUBHDL);
67375e7992aSrie 				}
67475e7992aSrie 			}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			name = (char *)STRTAB(clmp) + dyn->d_un.d_val;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 			/*
6797c478bd9Sstevel@tonic-gate 			 * NOTE, libc.so.1 can't be lazy loaded.  Although a
6807c478bd9Sstevel@tonic-gate 			 * lazy position flag won't be produced when a RTLDINFO
6817c478bd9Sstevel@tonic-gate 			 * .dynamic entry is found (introduced with the UPM in
6827c478bd9Sstevel@tonic-gate 			 * Solaris 10), it was possible to mark libc for lazy
6837c478bd9Sstevel@tonic-gate 			 * loading on previous releases.  To reduce the overhead
6847c478bd9Sstevel@tonic-gate 			 * of testing for this occurrence, only carry out this
6857c478bd9Sstevel@tonic-gate 			 * check for the first object on the link-map list
6867c478bd9Sstevel@tonic-gate 			 * (there aren't many applications built without libc).
6877c478bd9Sstevel@tonic-gate 			 */
6887c478bd9Sstevel@tonic-gate 			if (lazy && (lml->lm_head == clmp) &&
6897c478bd9Sstevel@tonic-gate 			    (strcmp(name, MSG_ORIG(MSG_FIL_LIBC)) == 0))
6907c478bd9Sstevel@tonic-gate 				lazy = 0;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 			/*
6937c478bd9Sstevel@tonic-gate 			 * Don't bring in lazy loaded objects yet unless we've
6947c478bd9Sstevel@tonic-gate 			 * been asked to attempt to load all available objects
6957c478bd9Sstevel@tonic-gate 			 * (crle(1) sets LD_FLAGS=loadavail).  Even under
6967c478bd9Sstevel@tonic-gate 			 * RTLD_NOW we don't process this - RTLD_NOW will cause
6977c478bd9Sstevel@tonic-gate 			 * relocation processing which in turn might trigger
6987c478bd9Sstevel@tonic-gate 			 * lazy loading, but its possible that the object has a
6997c478bd9Sstevel@tonic-gate 			 * lazy loaded file with no bindings (i.e., it should
7007c478bd9Sstevel@tonic-gate 			 * never have been a dependency in the first place).
7017c478bd9Sstevel@tonic-gate 			 */
7027c478bd9Sstevel@tonic-gate 			if (lazy) {
7037c478bd9Sstevel@tonic-gate 				if ((lmflags & LML_FLG_LOADAVAIL) == 0) {
7047c478bd9Sstevel@tonic-gate 					LAZY(clmp)++;
7057c478bd9Sstevel@tonic-gate 					lazy = flags = 0;
7067c478bd9Sstevel@tonic-gate 					continue;
7077c478bd9Sstevel@tonic-gate 				}
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 				/*
7107c478bd9Sstevel@tonic-gate 				 * Silence any error messages - see description
7117c478bd9Sstevel@tonic-gate 				 * under elf_lookup_filtee().
7127c478bd9Sstevel@tonic-gate 				 */
7137c478bd9Sstevel@tonic-gate 				if ((rtld_flags & RT_FL_SILENCERR) == 0) {
7147c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_SILENCERR;
7157c478bd9Sstevel@tonic-gate 					silent = 1;
7167c478bd9Sstevel@tonic-gate 				}
7177c478bd9Sstevel@tonic-gate 			}
7187c478bd9Sstevel@tonic-gate 			break;
7197c478bd9Sstevel@tonic-gate 		case DT_AUXILIARY:
7207c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_AUXFLTR;
7217c478bd9Sstevel@tonic-gate 			continue;
7227c478bd9Sstevel@tonic-gate 		case DT_SUNW_AUXILIARY:
7237c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_AUXFLTR | FLG_DI_SYMFLTR);
7247c478bd9Sstevel@tonic-gate 			continue;
7257c478bd9Sstevel@tonic-gate 		case DT_FILTER:
7267c478bd9Sstevel@tonic-gate 			dip->di_flags |= FLG_DI_STDFLTR;
7277c478bd9Sstevel@tonic-gate 			continue;
7287c478bd9Sstevel@tonic-gate 		case DT_SUNW_FILTER:
7297c478bd9Sstevel@tonic-gate 			dip->di_flags |= (FLG_DI_STDFLTR | FLG_DI_SYMFLTR);
7307c478bd9Sstevel@tonic-gate 			continue;
7317c478bd9Sstevel@tonic-gate 		default:
7327c478bd9Sstevel@tonic-gate 			continue;
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 
7355aefb655Srie 		DBG_CALL(Dbg_file_needed(clmp, name));
73675e7992aSrie 
73775e7992aSrie 		/*
73875e7992aSrie 		 * If we're running under ldd(1), indicate that this dependency
73975e7992aSrie 		 * has been processed.  It doesn't matter whether the object is
74075e7992aSrie 		 * successfully loaded or not, this flag simply ensures that we
74175e7992aSrie 		 * don't repeatedly attempt to load an object that has already
74275e7992aSrie 		 * failed to load.  To do so would create multiple failure
74375e7992aSrie 		 * diagnostics for the same object under ldd(1).
74475e7992aSrie 		 */
7457c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & LML_FLG_TRC_ENABLE)
74675e7992aSrie 			dip->di_flags |= FLG_DI_LDD_DONE;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		/*
7497c478bd9Sstevel@tonic-gate 		 * Establish the objects name, load it and establish a binding
7507c478bd9Sstevel@tonic-gate 		 * with the caller.
7517c478bd9Sstevel@tonic-gate 		 */
75256deab07SRod Evans 		if ((elf_fix_name(name, clmp, &palp, AL_CNT_NEEDED, 0) == 0) ||
75356deab07SRod Evans 		    ((nlmp = load_one(lml, lmco, palp, clmp, MODE(clmp),
75456deab07SRod Evans 		    flags, 0, in_nfavl)) == NULL) ||
75556deab07SRod Evans 		    (bind_one(clmp, nlmp, BND_NEEDED) == 0))
756481bba9eSRod Evans 			nlmp = NULL;
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		/*
7597c478bd9Sstevel@tonic-gate 		 * Clean up any infrastructure, including the removal of the
7607c478bd9Sstevel@tonic-gate 		 * error suppression state, if it had been previously set in
7617c478bd9Sstevel@tonic-gate 		 * this routine.
7627c478bd9Sstevel@tonic-gate 		 */
76356deab07SRod Evans 		remove_plist(&palp, 0);
76456deab07SRod Evans 
7657c478bd9Sstevel@tonic-gate 		if (silent)
7667c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_SILENCERR;
76775e7992aSrie 
768481bba9eSRod Evans 		if ((dip->di_info = (void *)nlmp) == NULL) {
7697c478bd9Sstevel@tonic-gate 			/*
7707c478bd9Sstevel@tonic-gate 			 * If the object could not be mapped, continue if error
7717c478bd9Sstevel@tonic-gate 			 * suppression is established or we're here with ldd(1).
7727c478bd9Sstevel@tonic-gate 			 */
7737c478bd9Sstevel@tonic-gate 			if ((MODE(clmp) & RTLD_CONFGEN) || (lmflags &
7747c478bd9Sstevel@tonic-gate 			    (LML_FLG_LOADAVAIL | LML_FLG_TRC_ENABLE)))
7757c478bd9Sstevel@tonic-gate 				continue;
77656deab07SRod Evans 			else {
77756deab07SRod Evans 				remove_plist(&palp, 1);
7787c478bd9Sstevel@tonic-gate 				return (0);
77956deab07SRod Evans 			}
7807c478bd9Sstevel@tonic-gate 		}
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	if (LAZY(clmp))
7847c478bd9Sstevel@tonic-gate 		lml->lm_lazy++;
7857c478bd9Sstevel@tonic-gate 
78656deab07SRod Evans 	remove_plist(&palp, 1);
7877c478bd9Sstevel@tonic-gate 	return (1);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * A null symbol interpretor.  Used if a filter has no associated filtees.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
794*08278a5eSRod Evans static int
795*08278a5eSRod Evans elf_null_find_sym(Slookup *slp, Sresult *srp, uint_t *binfo, int *in_nfavl)
7967c478bd9Sstevel@tonic-gate {
797*08278a5eSRod Evans 	return (0);
7987c478bd9Sstevel@tonic-gate }
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate /*
8017c478bd9Sstevel@tonic-gate  * Disable filtee use.
8027c478bd9Sstevel@tonic-gate  */
8037c478bd9Sstevel@tonic-gate static void
8049a411307Srie elf_disable_filtee(Rt_map *lmp, Dyninfo *dip)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_SYMFLTR) == 0) {
8077c478bd9Sstevel@tonic-gate 		/*
80856deab07SRod Evans 		 * If this is an object filter, null out the reference name.
8097c478bd9Sstevel@tonic-gate 		 */
8107c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(lmp) != FLTR_DISABLED) {
81137ffaf83SRod Evans 			REFNAME(lmp) = NULL;
8127c478bd9Sstevel@tonic-gate 			OBJFLTRNDX(lmp) = FLTR_DISABLED;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 			/*
8157c478bd9Sstevel@tonic-gate 			 * Indicate that this filtee is no longer available.
8167c478bd9Sstevel@tonic-gate 			 */
8177c478bd9Sstevel@tonic-gate 			if (dip->di_flags & FLG_DI_STDFLTR)
8187c478bd9Sstevel@tonic-gate 				SYMINTP(lmp) = elf_null_find_sym;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		}
8217c478bd9Sstevel@tonic-gate 	} else if (dip->di_flags & FLG_DI_STDFLTR) {
8227c478bd9Sstevel@tonic-gate 		/*
8237c478bd9Sstevel@tonic-gate 		 * Indicate that this standard filtee is no longer available.
8247c478bd9Sstevel@tonic-gate 		 */
8257c478bd9Sstevel@tonic-gate 		if (SYMSFLTRCNT(lmp))
8267c478bd9Sstevel@tonic-gate 			SYMSFLTRCNT(lmp)--;
8277c478bd9Sstevel@tonic-gate 	} else {
8287c478bd9Sstevel@tonic-gate 		/*
8297c478bd9Sstevel@tonic-gate 		 * Indicate that this auxiliary filtee is no longer available.
8307c478bd9Sstevel@tonic-gate 		 */
8317c478bd9Sstevel@tonic-gate 		if (SYMAFLTRCNT(lmp))
8327c478bd9Sstevel@tonic-gate 			SYMAFLTRCNT(lmp)--;
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 	dip->di_flags &= ~MSK_DI_FILTER;
8357c478bd9Sstevel@tonic-gate }
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate /*
8387c478bd9Sstevel@tonic-gate  * Find symbol interpreter - filters.
8397c478bd9Sstevel@tonic-gate  * This function is called when the symbols from a shared object should
8407c478bd9Sstevel@tonic-gate  * be resolved from the shared objects filtees instead of from within itself.
8417c478bd9Sstevel@tonic-gate  *
8427c478bd9Sstevel@tonic-gate  * A symbol name of 0 is used to trigger filtee loading.
8437c478bd9Sstevel@tonic-gate  */
844*08278a5eSRod Evans static int
845*08278a5eSRod Evans _elf_lookup_filtee(Slookup *slp, Sresult *srp, uint_t *binfo, uint_t ndx,
8469aa23310Srie     int *in_nfavl)
8477c478bd9Sstevel@tonic-gate {
8487c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name, *filtees;
8497c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
8507c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
85156deab07SRod Evans 	Pdesc		*pdp;
8527c478bd9Sstevel@tonic-gate 	int		any;
8537c478bd9Sstevel@tonic-gate 	Dyninfo		*dip = &DYNINFO(ilmp)[ndx];
8547c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(ilmp);
85556deab07SRod Evans 	Aliste		idx;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	/*
8587c478bd9Sstevel@tonic-gate 	 * Indicate that the filter has been used.  If a binding already exists
8597c478bd9Sstevel@tonic-gate 	 * to the caller, indicate that this object is referenced.  This insures
8607c478bd9Sstevel@tonic-gate 	 * we don't generate false unreferenced diagnostics from ldd -u/U or
8617c478bd9Sstevel@tonic-gate 	 * debugging.  Don't create a binding regardless, as this filter may
8627c478bd9Sstevel@tonic-gate 	 * have been dlopen()'ed.
8637c478bd9Sstevel@tonic-gate 	 */
8647c478bd9Sstevel@tonic-gate 	if (name && (ilmp != clmp)) {
8657c478bd9Sstevel@tonic-gate 		Word	tracing = (LIST(clmp)->lm_flags &
8667c478bd9Sstevel@tonic-gate 		    (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED));
8677c478bd9Sstevel@tonic-gate 
8685aefb655Srie 		if (tracing || DBG_ENABLED) {
869cce0e03bSab 			Bnd_desc 	*bdp;
870cce0e03bSab 			Aliste		idx;
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 			FLAGS1(ilmp) |= FL1_RT_USED;
8737c478bd9Sstevel@tonic-gate 
8745aefb655Srie 			if ((tracing & LML_FLG_TRC_UNREF) || DBG_ENABLED) {
875cce0e03bSab 				for (APLIST_TRAVERSE(CALLERS(ilmp), idx, bdp)) {
8767c478bd9Sstevel@tonic-gate 					if (bdp->b_caller == clmp) {
8777c478bd9Sstevel@tonic-gate 						bdp->b_flags |= BND_REFER;
8787c478bd9Sstevel@tonic-gate 						break;
8797c478bd9Sstevel@tonic-gate 					}
8807c478bd9Sstevel@tonic-gate 				}
8817c478bd9Sstevel@tonic-gate 			}
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/*
8867c478bd9Sstevel@tonic-gate 	 * If this is the first call to process this filter, establish the
8877c478bd9Sstevel@tonic-gate 	 * filtee list.  If a configuration file exists, determine if any
8887c478bd9Sstevel@tonic-gate 	 * filtee associations for this filter, and its filtee reference, are
8897c478bd9Sstevel@tonic-gate 	 * defined.  Otherwise, process the filtee reference.  Any token
8907c478bd9Sstevel@tonic-gate 	 * expansion is also completed at this point (i.e., $PLATFORM).
8917c478bd9Sstevel@tonic-gate 	 */
8927c478bd9Sstevel@tonic-gate 	filtees = (char *)STRTAB(ilmp) + DYN(ilmp)[ndx].d_un.d_val;
893481bba9eSRod Evans 	if (dip->di_info == NULL) {
894*08278a5eSRod Evans 		if (rtld_flags2 & RT_FL2_FLTCFG) {
89556deab07SRod Evans 			elf_config_flt(lml, PATHNAME(ilmp), filtees,
89656deab07SRod Evans 			    (Alist **)&dip->di_info, AL_CNT_FILTEES);
897*08278a5eSRod Evans 		}
898481bba9eSRod Evans 		if (dip->di_info == NULL) {
8995aefb655Srie 			DBG_CALL(Dbg_file_filter(lml, NAME(ilmp), filtees, 0));
9007c478bd9Sstevel@tonic-gate 			if ((lml->lm_flags &
9017c478bd9Sstevel@tonic-gate 			    (LML_FLG_TRC_VERBOSE | LML_FLG_TRC_SEARCH)) &&
9027c478bd9Sstevel@tonic-gate 			    ((FLAGS1(ilmp) & FL1_RT_LDDSTUB) == 0))
9037c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_FIL_FILTER),
9047c478bd9Sstevel@tonic-gate 				    NAME(ilmp), filtees);
9057c478bd9Sstevel@tonic-gate 
90656deab07SRod Evans 			if (expand_paths(ilmp, filtees, (Alist **)&dip->di_info,
90756deab07SRod Evans 			    AL_CNT_FILTEES, 0, 0) == 0) {
9087c478bd9Sstevel@tonic-gate 				elf_disable_filtee(ilmp, dip);
909*08278a5eSRod Evans 				return (0);
9107c478bd9Sstevel@tonic-gate 			}
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	/*
9157c478bd9Sstevel@tonic-gate 	 * Traverse the filtee list, dlopen()'ing any objects specified and
9167c478bd9Sstevel@tonic-gate 	 * using their group handle to lookup the symbol.
9177c478bd9Sstevel@tonic-gate 	 */
91856deab07SRod Evans 	any = 0;
91956deab07SRod Evans 	for (ALIST_TRAVERSE((Alist *)dip->di_info, idx, pdp)) {
9207c478bd9Sstevel@tonic-gate 		int	mode;
9217c478bd9Sstevel@tonic-gate 		Grp_hdl	*ghp;
922481bba9eSRod Evans 		Rt_map	*nlmp = NULL;
9237c478bd9Sstevel@tonic-gate 
92456deab07SRod Evans 		if (pdp->pd_plen == 0)
9257c478bd9Sstevel@tonic-gate 			continue;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		/*
9287c478bd9Sstevel@tonic-gate 		 * Establish the mode of the filtee from the filter.  As filtees
9297c478bd9Sstevel@tonic-gate 		 * are loaded via a dlopen(), make sure that RTLD_GROUP is set
9307c478bd9Sstevel@tonic-gate 		 * and the filtees aren't global.  It would be nice to have
9317c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST used here also, but as filters got out long before
9327c478bd9Sstevel@tonic-gate 		 * RTLD_FIRST was introduced it's a little too late now.
9337c478bd9Sstevel@tonic-gate 		 */
9347c478bd9Sstevel@tonic-gate 		mode = MODE(ilmp) | RTLD_GROUP;
9357c478bd9Sstevel@tonic-gate 		mode &= ~RTLD_GLOBAL;
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 		/*
9387c478bd9Sstevel@tonic-gate 		 * Insure that any auxiliary filter can locate symbols from its
9397c478bd9Sstevel@tonic-gate 		 * caller.
9407c478bd9Sstevel@tonic-gate 		 */
9417c478bd9Sstevel@tonic-gate 		if (dip->di_flags & FLG_DI_AUXFLTR)
9427c478bd9Sstevel@tonic-gate 			mode |= RTLD_PARENT;
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		/*
945*08278a5eSRod Evans 		 * Process any capability directory.  Establish a new link-map
946*08278a5eSRod Evans 		 * control list from which to analyze any newly added objects.
9477c478bd9Sstevel@tonic-gate 		 */
948*08278a5eSRod Evans 		if ((pdp->pd_info == NULL) && (pdp->pd_flags & PD_TKN_CAP)) {
94956deab07SRod Evans 			const char	*dir = pdp->pd_pname;
95056deab07SRod Evans 			Aliste		lmco;
95111a2bb38Srie 
952dde769a2SRod Evans 			/*
953dde769a2SRod Evans 			 * Establish a link-map control list for this request.
954dde769a2SRod Evans 			 */
955dde769a2SRod Evans 			if ((lmco = create_cntl(lml, 0)) == NULL)
956dde769a2SRod Evans 				return (NULL);
9577c478bd9Sstevel@tonic-gate 
95856deab07SRod Evans 			/*
959*08278a5eSRod Evans 			 * Determine the capability filtees.  If none can be
960*08278a5eSRod Evans 			 * found, provide suitable diagnostics.
96156deab07SRod Evans 			 */
962*08278a5eSRod Evans 			DBG_CALL(Dbg_cap_filter(lml, dir, ilmp));
963*08278a5eSRod Evans 			if (cap_filtees((Alist **)&dip->di_info, idx, dir,
964481bba9eSRod Evans 			    lmco, ilmp, filtees, mode,
965*08278a5eSRod Evans 			    (FLG_RT_PUBHDL | FLG_RT_CAP), in_nfavl) == 0) {
96656deab07SRod Evans 				if ((lml->lm_flags & LML_FLG_TRC_ENABLE) &&
96756deab07SRod Evans 				    (dip->di_flags & FLG_DI_AUXFLTR) &&
96856deab07SRod Evans 				    (rtld_flags & RT_FL_WARNFLTR)) {
96956deab07SRod Evans 					(void) printf(
970*08278a5eSRod Evans 					    MSG_INTL(MSG_LDD_CAP_NFOUND), dir);
97156deab07SRod Evans 				}
972*08278a5eSRod Evans 				DBG_CALL(Dbg_cap_filter(lml, dir, 0));
97356deab07SRod Evans 			}
97456deab07SRod Evans 
97556deab07SRod Evans 			/*
976*08278a5eSRod Evans 			 * Re-establish the originating path name descriptor,
977*08278a5eSRod Evans 			 * as the expansion of capabilities filtees may have
978*08278a5eSRod Evans 			 * re-allocated the controlling Alist.  Mark this
97956deab07SRod Evans 			 * original pathname descriptor as unused so that the
98056deab07SRod Evans 			 * descriptor isn't revisited for processing.  Any real
981*08278a5eSRod Evans 			 * capabilities filtees have been added as new pathname
982*08278a5eSRod Evans 			 * descriptors following this descriptor.
98356deab07SRod Evans 			 */
98456deab07SRod Evans 			pdp = alist_item((Alist *)dip->di_info, idx);
985*08278a5eSRod Evans 			pdp->pd_flags &= ~PD_TKN_CAP;
98656deab07SRod Evans 			pdp->pd_plen = 0;
98711a2bb38Srie 
98811a2bb38Srie 			/*
989*08278a5eSRod Evans 			 * Now that any capability objects have been processed,
990*08278a5eSRod Evans 			 * remove any temporary link-map control list.
99111a2bb38Srie 			 */
992481bba9eSRod Evans 			if (lmco != ALIST_OFF_DATA)
99311a2bb38Srie 				remove_cntl(lml, lmco);
9947c478bd9Sstevel@tonic-gate 		}
9957c478bd9Sstevel@tonic-gate 
99656deab07SRod Evans 		if (pdp->pd_plen == 0)
9977c478bd9Sstevel@tonic-gate 			continue;
9987c478bd9Sstevel@tonic-gate 
9997c478bd9Sstevel@tonic-gate 		/*
10007c478bd9Sstevel@tonic-gate 		 * Process an individual filtee.
10017c478bd9Sstevel@tonic-gate 		 */
1002481bba9eSRod Evans 		if (pdp->pd_info == NULL) {
100356deab07SRod Evans 			const char	*filtee = pdp->pd_pname;
10047c478bd9Sstevel@tonic-gate 			int		audit = 0;
10057c478bd9Sstevel@tonic-gate 
10065aefb655Srie 			DBG_CALL(Dbg_file_filtee(lml, NAME(ilmp), filtee, 0));
10077c478bd9Sstevel@tonic-gate 
1008481bba9eSRod Evans 			ghp = NULL;
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 			/*
10117c478bd9Sstevel@tonic-gate 			 * Determine if the reference link map is already
10127c478bd9Sstevel@tonic-gate 			 * loaded.  As an optimization compare the filtee with
10137c478bd9Sstevel@tonic-gate 			 * our interpretor.  The most common filter is
10147c478bd9Sstevel@tonic-gate 			 * libdl.so.1, which is a filter on ld.so.1.
10157c478bd9Sstevel@tonic-gate 			 */
10167c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
10177c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD_64)) == 0) {
10187c478bd9Sstevel@tonic-gate #else
10197c478bd9Sstevel@tonic-gate 			if (strcmp(filtee, MSG_ORIG(MSG_PTH_RTLD)) == 0) {
10207c478bd9Sstevel@tonic-gate #endif
10212017c965SRod Evans 				uint_t	hflags, rdflags, cdflags;
10222017c965SRod Evans 
10232017c965SRod Evans 				/*
10242017c965SRod Evans 				 * Establish any flags for the handle (Grp_hdl).
10252017c965SRod Evans 				 *
10262017c965SRod Evans 				 *  -	This is a special, public, ld.so.1
10272017c965SRod Evans 				 *	handle.
10282017c965SRod Evans 				 *  -	Only the first object on this handle
10292017c965SRod Evans 				 *	can supply symbols.
10302017c965SRod Evans 				 *  -	This handle provides a filtee.
10312017c965SRod Evans 				 *
10322017c965SRod Evans 				 * Essentially, this handle allows a caller to
10332017c965SRod Evans 				 * reference the dl*() family of interfaces from
10342017c965SRod Evans 				 * ld.so.1.
10352017c965SRod Evans 				 */
10362017c965SRod Evans 				hflags = (GPH_PUBLIC | GPH_LDSO |
10372017c965SRod Evans 				    GPH_FIRST | GPH_FILTEE);
10382017c965SRod Evans 
10392017c965SRod Evans 				/*
10402017c965SRod Evans 				 * Establish the flags for the referenced
10412017c965SRod Evans 				 * dependency descriptor (Grp_desc).
10422017c965SRod Evans 				 *
10432017c965SRod Evans 				 *  -	ld.so.1 is available for dlsym().
10442017c965SRod Evans 				 *  -	ld.so.1 is available to relocate
10452017c965SRod Evans 				 *	against.
10462017c965SRod Evans 				 *  -	There's no need to add an dependencies
10472017c965SRod Evans 				 * 	to this handle.
10482017c965SRod Evans 				 */
10492017c965SRod Evans 				rdflags = (GPD_DLSYM | GPD_RELOC);
10502017c965SRod Evans 
10517c478bd9Sstevel@tonic-gate 				/*
10522017c965SRod Evans 				 * Establish the flags for this callers
10532017c965SRod Evans 				 * dependency descriptor (Grp_desc).
10542017c965SRod Evans 				 *
10552017c965SRod Evans 				 *  -   The explicit creation of a handle
10562017c965SRod Evans 				 *	creates a descriptor for the referenced
10572017c965SRod Evans 				 *	object and the parent (caller).
10587c478bd9Sstevel@tonic-gate 				 */
10592017c965SRod Evans 				cdflags = GPD_PARENT;
10602017c965SRod Evans 
10617c478bd9Sstevel@tonic-gate 				nlmp = lml_rtld.lm_head;
10627c478bd9Sstevel@tonic-gate 				if ((ghp = hdl_create(&lml_rtld, nlmp, ilmp,
10632017c965SRod Evans 				    hflags, rdflags, cdflags)) == NULL)
1064481bba9eSRod Evans 					nlmp = NULL;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 				/*
10677c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
10687c478bd9Sstevel@tonic-gate 				 * recursion.
10697c478bd9Sstevel@tonic-gate 				 */
10707c478bd9Sstevel@tonic-gate 				if (nlmp && ghp)
107156deab07SRod Evans 					pdp->pd_info = (void *)ghp;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 				/*
10747c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  Ignore
10757c478bd9Sstevel@tonic-gate 				 * any return from the auditor, as we can't
10767c478bd9Sstevel@tonic-gate 				 * allow ignore filtering to ld.so.1, otherwise
10777c478bd9Sstevel@tonic-gate 				 * nothing is going to work.
10787c478bd9Sstevel@tonic-gate 				 */
107956deab07SRod Evans 				if (nlmp && ((lml->lm_tflags | AFLAGS(ilmp)) &
108002ca3e02Srie 				    LML_TFLG_AUD_OBJFILTER))
10817c478bd9Sstevel@tonic-gate 					(void) audit_objfilter(ilmp, filtees,
10827c478bd9Sstevel@tonic-gate 					    nlmp, 0);
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 			} else {
10857c478bd9Sstevel@tonic-gate 				Rej_desc	rej = { 0 };
108656deab07SRod Evans 				Fdesc		fd = { 0 };
108711a2bb38Srie 				Aliste		lmco;
10887c478bd9Sstevel@tonic-gate 
108956deab07SRod Evans 				/*
109056deab07SRod Evans 				 * Trace the inspection of this file, determine
109156deab07SRod Evans 				 * any auditor substitution, and seed the file
109256deab07SRod Evans 				 * descriptor with the originating name.
109356deab07SRod Evans 				 */
109456deab07SRod Evans 				if (load_trace(lml, pdp, clmp, &fd) == NULL)
109556deab07SRod Evans 					continue;
109656deab07SRod Evans 
10977c478bd9Sstevel@tonic-gate 				/*
1098dde769a2SRod Evans 				 * Establish a link-map control list for this
1099dde769a2SRod Evans 				 * request.
11007c478bd9Sstevel@tonic-gate 				 */
1101dde769a2SRod Evans 				if ((lmco = create_cntl(lml, 0)) == NULL)
1102dde769a2SRod Evans 					return (NULL);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 				/*
110556deab07SRod Evans 				 * Locate and load the filtee.
11067c478bd9Sstevel@tonic-gate 				 */
110756deab07SRod Evans 				if ((nlmp = load_path(lml, lmco, ilmp, mode,
11082017c965SRod Evans 				    FLG_RT_PUBHDL, &ghp, &fd, &rej,
110956deab07SRod Evans 				    in_nfavl)) == NULL)
11107c478bd9Sstevel@tonic-gate 					file_notfound(LIST(ilmp), filtee, ilmp,
11112017c965SRod Evans 					    FLG_RT_PUBHDL, &rej);
111256deab07SRod Evans 
111356deab07SRod Evans 				filtee = pdp->pd_pname;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 				/*
11167c478bd9Sstevel@tonic-gate 				 * Establish the filter handle to prevent any
11177c478bd9Sstevel@tonic-gate 				 * recursion.
11187c478bd9Sstevel@tonic-gate 				 */
11197c478bd9Sstevel@tonic-gate 				if (nlmp && ghp) {
11207c478bd9Sstevel@tonic-gate 					ghp->gh_flags |= GPH_FILTEE;
112156deab07SRod Evans 					pdp->pd_info = (void *)ghp;
11229aa23310Srie 
11239aa23310Srie 					FLAGS1(nlmp) |= FL1_RT_USED;
11247c478bd9Sstevel@tonic-gate 				}
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 				/*
11277c478bd9Sstevel@tonic-gate 				 * Audit the filter/filtee established.  A
11287c478bd9Sstevel@tonic-gate 				 * return of 0 indicates the auditor wishes to
11297c478bd9Sstevel@tonic-gate 				 * ignore this filtee.
11307c478bd9Sstevel@tonic-gate 				 */
11317c478bd9Sstevel@tonic-gate 				if (nlmp && ((lml->lm_tflags | FLAGS1(ilmp)) &
11327c478bd9Sstevel@tonic-gate 				    LML_TFLG_AUD_OBJFILTER)) {
11337c478bd9Sstevel@tonic-gate 					if (audit_objfilter(ilmp, filtees,
11347c478bd9Sstevel@tonic-gate 					    nlmp, 0) == 0) {
11357c478bd9Sstevel@tonic-gate 						audit = 1;
1136481bba9eSRod Evans 						nlmp = NULL;
11377c478bd9Sstevel@tonic-gate 					}
11387c478bd9Sstevel@tonic-gate 				}
11397c478bd9Sstevel@tonic-gate 
11407c478bd9Sstevel@tonic-gate 				/*
11417c478bd9Sstevel@tonic-gate 				 * Finish processing the objects associated with
11427c478bd9Sstevel@tonic-gate 				 * this request.  Create an association between
11437c478bd9Sstevel@tonic-gate 				 * this object and the originating filter to
11447c478bd9Sstevel@tonic-gate 				 * provide sufficient information to tear down
11457c478bd9Sstevel@tonic-gate 				 * this filtee if necessary.
11467c478bd9Sstevel@tonic-gate 				 */
114756deab07SRod Evans 				if (nlmp && ghp && (((nlmp = analyze_lmc(lml,
114856deab07SRod Evans 				    lmco, nlmp, in_nfavl)) == NULL) ||
114956deab07SRod Evans 				    (relocate_lmc(lml, lmco, ilmp, nlmp,
115056deab07SRod Evans 				    in_nfavl) == 0)))
1151481bba9eSRod Evans 					nlmp = NULL;
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 				/*
11547c478bd9Sstevel@tonic-gate 				 * If the filtee has been successfully
115502ca3e02Srie 				 * processed, then create an association
115602ca3e02Srie 				 * between the filter and filtee.  This
115702ca3e02Srie 				 * association provides sufficient information
115802ca3e02Srie 				 * to tear down the filter and filtee if
115902ca3e02Srie 				 * necessary.
11607c478bd9Sstevel@tonic-gate 				 */
11618af2c5b9Srie 				DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
11622017c965SRod Evans 				if (nlmp && ghp && (hdl_add(ghp, ilmp,
11632017c965SRod Evans 				    GPD_FILTER, NULL) == NULL))
1164481bba9eSRod Evans 					nlmp = NULL;
116511a2bb38Srie 
116656deab07SRod Evans 				/*
116756deab07SRod Evans 				 * Generate a diagnostic if the filtee couldn't
116856deab07SRod Evans 				 * be loaded.
116956deab07SRod Evans 				 */
1170481bba9eSRod Evans 				if (nlmp == NULL)
117156deab07SRod Evans 					DBG_CALL(Dbg_file_filtee(lml, 0, filtee,
117256deab07SRod Evans 					    audit));
117356deab07SRod Evans 
117411a2bb38Srie 				/*
117502ca3e02Srie 				 * If this filtee loading has failed, and we've
117602ca3e02Srie 				 * created a new link-map control list to which
117702ca3e02Srie 				 * this request has added objects, then remove
117802ca3e02Srie 				 * all the objects that have been associated to
117902ca3e02Srie 				 * this request.
118011a2bb38Srie 				 */
1181481bba9eSRod Evans 				if ((nlmp == NULL) && (lmco != ALIST_OFF_DATA))
1182481bba9eSRod Evans 					remove_lmc(lml, clmp, lmco, name);
118302ca3e02Srie 
118402ca3e02Srie 				/*
1185dde769a2SRod Evans 				 * Remove any temporary link-map control list.
118602ca3e02Srie 				 */
1187481bba9eSRod Evans 				if (lmco != ALIST_OFF_DATA)
118811a2bb38Srie 					remove_cntl(lml, lmco);
11897c478bd9Sstevel@tonic-gate 			}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 			/*
119256deab07SRod Evans 			 * If the filtee couldn't be loaded, null out the
119356deab07SRod Evans 			 * path name descriptor entry, and continue the search.
119456deab07SRod Evans 			 * Otherwise, the group handle is retained for future
119556deab07SRod Evans 			 * symbol searches.
11967c478bd9Sstevel@tonic-gate 			 */
1197481bba9eSRod Evans 			if (nlmp == NULL) {
119856deab07SRod Evans 				pdp->pd_info = NULL;
119956deab07SRod Evans 				pdp->pd_plen = 0;
12007c478bd9Sstevel@tonic-gate 				continue;
12017c478bd9Sstevel@tonic-gate 			}
12027c478bd9Sstevel@tonic-gate 		}
12037c478bd9Sstevel@tonic-gate 
120456deab07SRod Evans 		ghp = (Grp_hdl *)pdp->pd_info;
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 		/*
1207*08278a5eSRod Evans 		 * If name is NULL, we're here to trigger filtee loading.
1208*08278a5eSRod Evans 		 * Skip the symbol lookup so that we'll continue looking for
1209*08278a5eSRod Evans 		 * additional filtees.
12107c478bd9Sstevel@tonic-gate 		 */
12117c478bd9Sstevel@tonic-gate 		if (name) {
12127c478bd9Sstevel@tonic-gate 			Grp_desc	*gdp;
1213*08278a5eSRod Evans 			int		ret = 0;
1214cce0e03bSab 			Aliste		idx;
12157c478bd9Sstevel@tonic-gate 			Slookup		sl = *slp;
12167c478bd9Sstevel@tonic-gate 
1217*08278a5eSRod Evans 			sl.sl_flags |= (LKUP_FIRST | LKUP_DLSYM);
12187c478bd9Sstevel@tonic-gate 			any++;
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate 			/*
12217c478bd9Sstevel@tonic-gate 			 * Look for the symbol in the handles dependencies.
12227c478bd9Sstevel@tonic-gate 			 */
1223cce0e03bSab 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1224efb9e8b8Srie 				if ((gdp->gd_flags & GPD_DLSYM) == 0)
12257c478bd9Sstevel@tonic-gate 					continue;
12267c478bd9Sstevel@tonic-gate 
12277c478bd9Sstevel@tonic-gate 				/*
12287c478bd9Sstevel@tonic-gate 				 * If our parent is a dependency don't look at
12297c478bd9Sstevel@tonic-gate 				 * it (otherwise we are in a recursive loop).
12307c478bd9Sstevel@tonic-gate 				 * This situation can occur with auxiliary
12317c478bd9Sstevel@tonic-gate 				 * filters if the filtee has a dependency on the
12327c478bd9Sstevel@tonic-gate 				 * filter.  This dependency isn't necessary as
12337c478bd9Sstevel@tonic-gate 				 * auxiliary filters are opened RTLD_PARENT, but
12347c478bd9Sstevel@tonic-gate 				 * users may still unknowingly add an explicit
12357c478bd9Sstevel@tonic-gate 				 * dependency to the parent.
12367c478bd9Sstevel@tonic-gate 				 */
12377c478bd9Sstevel@tonic-gate 				if ((sl.sl_imap = gdp->gd_depend) == ilmp)
12387c478bd9Sstevel@tonic-gate 					continue;
12397c478bd9Sstevel@tonic-gate 
1240*08278a5eSRod Evans 				if (((ret = SYMINTP(sl.sl_imap)(&sl, srp, binfo,
1241*08278a5eSRod Evans 				    in_nfavl)) != 0) ||
12427c478bd9Sstevel@tonic-gate 				    (ghp->gh_flags & GPH_FIRST))
12437c478bd9Sstevel@tonic-gate 					break;
12447c478bd9Sstevel@tonic-gate 			}
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 			/*
124702ca3e02Srie 			 * If a symbol has been found, indicate the binding
124802ca3e02Srie 			 * and return the symbol.
12497c478bd9Sstevel@tonic-gate 			 */
1250*08278a5eSRod Evans 			if (ret) {
12517c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_FILTEE;
1252*08278a5eSRod Evans 				return (1);
12537c478bd9Sstevel@tonic-gate 			}
12547c478bd9Sstevel@tonic-gate 		}
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 		/*
12577c478bd9Sstevel@tonic-gate 		 * If this object is tagged to terminate filtee processing we're
12587c478bd9Sstevel@tonic-gate 		 * done.
12597c478bd9Sstevel@tonic-gate 		 */
12605aefb655Srie 		if (FLAGS1(ghp->gh_ownlmp) & FL1_RT_ENDFILTE)
12617c478bd9Sstevel@tonic-gate 			break;
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	/*
12657c478bd9Sstevel@tonic-gate 	 * If we're just here to trigger filtee loading then we're done.
12667c478bd9Sstevel@tonic-gate 	 */
1267481bba9eSRod Evans 	if (name == NULL)
1268*08278a5eSRod Evans 		return (0);
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	/*
127156deab07SRod Evans 	 * If no filtees have been found for a filter, clean up any path name
127256deab07SRod Evans 	 * descriptors and disable their search completely.  For auxiliary
12737c478bd9Sstevel@tonic-gate 	 * filters we can reselect the symbol search function so that we never
12747c478bd9Sstevel@tonic-gate 	 * enter this routine again for this object.  For standard filters we
12757c478bd9Sstevel@tonic-gate 	 * use the null symbol routine.
12767c478bd9Sstevel@tonic-gate 	 */
12777c478bd9Sstevel@tonic-gate 	if (any == 0) {
127856deab07SRod Evans 		remove_plist((Alist **)&(dip->di_info), 1);
12797c478bd9Sstevel@tonic-gate 		elf_disable_filtee(ilmp, dip);
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
1282*08278a5eSRod Evans 	return (0);
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate /*
12867c478bd9Sstevel@tonic-gate  * Focal point for disabling error messages for auxiliary filters.  As an
12877c478bd9Sstevel@tonic-gate  * auxiliary filter allows for filtee use, but provides a fallback should a
12887c478bd9Sstevel@tonic-gate  * filtee not exist (or fail to load), any errors generated as a consequence of
12897c478bd9Sstevel@tonic-gate  * trying to load the filtees are typically suppressed.  Setting RT_FL_SILENCERR
1290*08278a5eSRod Evans  * suppresses errors generated by eprintf(), but ensures a debug diagnostic is
1291*08278a5eSRod Evans  * produced.  ldd(1) employs printf(), and here the selection of whether to
12927c478bd9Sstevel@tonic-gate  * print a diagnostic in regards to auxiliary filters is a little more complex.
12937c478bd9Sstevel@tonic-gate  *
12942017c965SRod Evans  *   -	The determination of whether to produce an ldd message, or a fatal
12957c478bd9Sstevel@tonic-gate  *	error message is driven by LML_FLG_TRC_ENABLE.
12962017c965SRod Evans  *   -	More detailed ldd messages may also be driven off of LML_FLG_TRC_WARN,
12977c478bd9Sstevel@tonic-gate  *	(ldd -d/-r), LML_FLG_TRC_VERBOSE (ldd -v), LML_FLG_TRC_SEARCH (ldd -s),
12987c478bd9Sstevel@tonic-gate  *	and LML_FLG_TRC_UNREF/LML_FLG_TRC_UNUSED (ldd -U/-u).
12992017c965SRod Evans  *   -	If the calling object is lddstub, then several classes of message are
13007c478bd9Sstevel@tonic-gate  *	suppressed.  The user isn't trying to diagnose lddstub, this is simply
13017c478bd9Sstevel@tonic-gate  *	a stub executable employed to preload a user specified library against.
13022017c965SRod Evans  *   -	If RT_FL_SILENCERR is in effect then any generic ldd() messages should
13037c478bd9Sstevel@tonic-gate  *	be suppressed.  All detailed ldd messages should still be produced.
13047c478bd9Sstevel@tonic-gate  */
1305*08278a5eSRod Evans int
1306*08278a5eSRod Evans elf_lookup_filtee(Slookup *slp, Sresult *srp, uint_t *binfo, uint_t ndx,
13079aa23310Srie     int *in_nfavl)
13087c478bd9Sstevel@tonic-gate {
13097c478bd9Sstevel@tonic-gate 	Dyninfo	*dip = &DYNINFO(slp->sl_imap)[ndx];
1310*08278a5eSRod Evans 	int	ret, silent = 0;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	/*
13137c478bd9Sstevel@tonic-gate 	 * Make sure this entry is still acting as a filter.  We may have tried
13147c478bd9Sstevel@tonic-gate 	 * to process this previously, and disabled it if the filtee couldn't
13157c478bd9Sstevel@tonic-gate 	 * be processed.  However, other entries may provide different filtees
13167c478bd9Sstevel@tonic-gate 	 * that are yet to be completed.
13177c478bd9Sstevel@tonic-gate 	 */
13187c478bd9Sstevel@tonic-gate 	if (dip->di_flags == 0)
1319*08278a5eSRod Evans 		return (0);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	/*
13227c478bd9Sstevel@tonic-gate 	 * Indicate whether an error message is required should this filtee not
13237c478bd9Sstevel@tonic-gate 	 * be found, based on the type of filter.
13247c478bd9Sstevel@tonic-gate 	 */
13257c478bd9Sstevel@tonic-gate 	if ((dip->di_flags & FLG_DI_AUXFLTR) &&
13267c478bd9Sstevel@tonic-gate 	    ((rtld_flags & (RT_FL_WARNFLTR | RT_FL_SILENCERR)) == 0)) {
13277c478bd9Sstevel@tonic-gate 		rtld_flags |= RT_FL_SILENCERR;
13287c478bd9Sstevel@tonic-gate 		silent = 1;
13297c478bd9Sstevel@tonic-gate 	}
13307c478bd9Sstevel@tonic-gate 
1331*08278a5eSRod Evans 	ret = _elf_lookup_filtee(slp, srp, binfo, ndx, in_nfavl);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (silent)
13347c478bd9Sstevel@tonic-gate 		rtld_flags &= ~RT_FL_SILENCERR;
13357c478bd9Sstevel@tonic-gate 
1336*08278a5eSRod Evans 	return (ret);
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate 
13397c478bd9Sstevel@tonic-gate /*
13407c478bd9Sstevel@tonic-gate  * Compute the elf hash value (as defined in the ELF access library).
13417c478bd9Sstevel@tonic-gate  * The form of the hash table is:
13427c478bd9Sstevel@tonic-gate  *
13437c478bd9Sstevel@tonic-gate  *	|--------------|
13447c478bd9Sstevel@tonic-gate  *	| # of buckets |
13457c478bd9Sstevel@tonic-gate  *	|--------------|
13467c478bd9Sstevel@tonic-gate  *	| # of chains  |
13477c478bd9Sstevel@tonic-gate  *	|--------------|
13487c478bd9Sstevel@tonic-gate  *	|   bucket[]   |
13497c478bd9Sstevel@tonic-gate  *	|--------------|
13507c478bd9Sstevel@tonic-gate  *	|   chain[]    |
13517c478bd9Sstevel@tonic-gate  *	|--------------|
13527c478bd9Sstevel@tonic-gate  */
13537c478bd9Sstevel@tonic-gate ulong_t
13547c478bd9Sstevel@tonic-gate elf_hash(const char *name)
13557c478bd9Sstevel@tonic-gate {
13567c478bd9Sstevel@tonic-gate 	uint_t	hval = 0;
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate 	while (*name) {
13597c478bd9Sstevel@tonic-gate 		uint_t	g;
13607c478bd9Sstevel@tonic-gate 		hval = (hval << 4) + *name++;
13617c478bd9Sstevel@tonic-gate 		if ((g = (hval & 0xf0000000)) != 0)
13627c478bd9Sstevel@tonic-gate 			hval ^= g >> 24;
13637c478bd9Sstevel@tonic-gate 		hval &= ~g;
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 	return ((ulong_t)hval);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate /*
1369*08278a5eSRod Evans  * Look up a symbol.  The callers lookup information is passed in the Slookup
1370*08278a5eSRod Evans  * structure, and any resultant binding information is returned in the Sresult
1371*08278a5eSRod Evans  * structure.
13727c478bd9Sstevel@tonic-gate  */
1373*08278a5eSRod Evans int
1374*08278a5eSRod Evans elf_find_sym(Slookup *slp, Sresult *srp, uint_t *binfo, int *in_nfavl)
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
13777c478bd9Sstevel@tonic-gate 	Rt_map		*ilmp = slp->sl_imap;
13787c478bd9Sstevel@tonic-gate 	ulong_t		hash = slp->sl_hash;
1379*08278a5eSRod Evans 	uint_t		ndx, hashoff, buckets, *chainptr;
13807c478bd9Sstevel@tonic-gate 	Sym		*sym, *symtabptr;
13817c478bd9Sstevel@tonic-gate 	char		*strtabptr, *strtabname;
13827c478bd9Sstevel@tonic-gate 	uint_t		flags1;
13837c478bd9Sstevel@tonic-gate 	Syminfo		*sip;
13847c478bd9Sstevel@tonic-gate 
1385660acd81Srie 	/*
1386660acd81Srie 	 * If we're only here to establish a symbols index, skip the diagnostic
1387660acd81Srie 	 * used to trace a symbol search.
1388660acd81Srie 	 */
13895aefb655Srie 	if ((slp->sl_flags & LKUP_SYMNDX) == 0)
13905aefb655Srie 		DBG_CALL(Dbg_syms_lookup(ilmp, name, MSG_ORIG(MSG_STR_ELF)));
13917c478bd9Sstevel@tonic-gate 
1392481bba9eSRod Evans 	if (HASH(ilmp) == NULL)
1393*08278a5eSRod Evans 		return (0);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 	buckets = HASH(ilmp)[0];
13967c478bd9Sstevel@tonic-gate 	/* LINTED */
1397*08278a5eSRod Evans 	hashoff = ((uint_t)hash % buckets) + 2;
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate 	/*
1400*08278a5eSRod Evans 	 * Get the first symbol from the hash chain and initialize the string
14017c478bd9Sstevel@tonic-gate 	 * and symbol table pointers.
14027c478bd9Sstevel@tonic-gate 	 */
1403*08278a5eSRod Evans 	if ((ndx = HASH(ilmp)[hashoff]) == 0)
1404*08278a5eSRod Evans 		return (0);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	chainptr = HASH(ilmp) + 2 + buckets;
14077c478bd9Sstevel@tonic-gate 	strtabptr = STRTAB(ilmp);
14087c478bd9Sstevel@tonic-gate 	symtabptr = SYMTAB(ilmp);
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	while (ndx) {
14117c478bd9Sstevel@tonic-gate 		sym = symtabptr + ndx;
14127c478bd9Sstevel@tonic-gate 		strtabname = strtabptr + sym->st_name;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 		/*
14157c478bd9Sstevel@tonic-gate 		 * Compare the symbol found with the name required.  If the
14167c478bd9Sstevel@tonic-gate 		 * names don't match continue with the next hash entry.
14177c478bd9Sstevel@tonic-gate 		 */
14187c478bd9Sstevel@tonic-gate 		if ((*strtabname++ != *name) || strcmp(strtabname, &name[1])) {
1419*08278a5eSRod Evans 			hashoff = ndx + buckets + 2;
14207c478bd9Sstevel@tonic-gate 			if ((ndx = chainptr[ndx]) != 0)
14217c478bd9Sstevel@tonic-gate 				continue;
1422*08278a5eSRod Evans 			return (0);
14237c478bd9Sstevel@tonic-gate 		}
14247c478bd9Sstevel@tonic-gate 
1425*08278a5eSRod Evans 		/*
1426*08278a5eSRod Evans 		 * Symbols that are defined as hidden within an object usually
1427*08278a5eSRod Evans 		 * have any references from within the same object bound at
1428*08278a5eSRod Evans 		 * link-edit time, thus ld.so.1 is not involved.  However, if
1429*08278a5eSRod Evans 		 * these are capabilities symbols, then references to them must
1430*08278a5eSRod Evans 		 * be resolved at runtime.  A hidden symbol can only be bound
1431*08278a5eSRod Evans 		 * to by the object that defines the symbol.
1432*08278a5eSRod Evans 		 */
1433*08278a5eSRod Evans 		if ((sym->st_shndx != SHN_UNDEF) &&
1434*08278a5eSRod Evans 		    (ELF_ST_VISIBILITY(sym->st_other) == STV_HIDDEN) &&
1435*08278a5eSRod Evans 		    (slp->sl_cmap != ilmp))
1436*08278a5eSRod Evans 			return (0);
1437*08278a5eSRod Evans 
14383b41b08bSab 		/*
1439d840867fSab 		 * The Solaris ld does not put DT_VERSYM in the dynamic
1440d840867fSab 		 * section, but the GNU ld does. The GNU runtime linker
1441d840867fSab 		 * interprets the top bit of the 16-bit Versym value
1442d840867fSab 		 * (0x8000) as the "hidden" bit. If this bit is set,
1443d840867fSab 		 * the linker is supposed to act as if that symbol does
1444d840867fSab 		 * not exist. The hidden bit supports their versioning
1445d840867fSab 		 * scheme, which allows multiple incompatible functions
1446d840867fSab 		 * with the same name to exist at different versions
1447d840867fSab 		 * within an object. The Solaris linker does not support this
1448d840867fSab 		 * mechanism, or the model of interface evolution that
1449d840867fSab 		 * it allows, but we honor the hidden bit in GNU ld
1450d840867fSab 		 * produced objects in order to interoperate with them.
14513b41b08bSab 		 */
1452*08278a5eSRod Evans 		if (VERSYM(ilmp) && (VERSYM(ilmp)[ndx] & 0x8000)) {
1453d840867fSab 			DBG_CALL(Dbg_syms_ignore_gnuver(ilmp, name,
14543b41b08bSab 			    ndx, VERSYM(ilmp)[ndx]));
1455*08278a5eSRod Evans 			return (0);
14563b41b08bSab 		}
14573b41b08bSab 
1458660acd81Srie 		/*
1459*08278a5eSRod Evans 		 * If we're only here to establish a symbol's index, we're done.
1460660acd81Srie 		 */
1461*08278a5eSRod Evans 		if (slp->sl_flags & LKUP_SYMNDX) {
1462*08278a5eSRod Evans 			srp->sr_dmap = ilmp;
1463*08278a5eSRod Evans 			srp->sr_sym = sym;
1464*08278a5eSRod Evans 			return (1);
1465*08278a5eSRod Evans 		}
1466660acd81Srie 
14677c478bd9Sstevel@tonic-gate 		/*
1468*08278a5eSRod Evans 		 * If we find a match and the symbol is defined, capture the
14697c478bd9Sstevel@tonic-gate 		 * symbol pointer and the link map in which it was found.
14707c478bd9Sstevel@tonic-gate 		 */
14717c478bd9Sstevel@tonic-gate 		if (sym->st_shndx != SHN_UNDEF) {
1472*08278a5eSRod Evans 			srp->sr_dmap = ilmp;
1473*08278a5eSRod Evans 			srp->sr_sym = sym;
14747c478bd9Sstevel@tonic-gate 			*binfo |= DBG_BINFO_FOUND;
1475*08278a5eSRod Evans 
14769a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
14779a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
14789a411307Srie 			    is_sym_interposer(ilmp, sym)))
14797c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
14807c478bd9Sstevel@tonic-gate 			break;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 		/*
14837c478bd9Sstevel@tonic-gate 		 * If we find a match and the symbol is undefined, the
14847c478bd9Sstevel@tonic-gate 		 * symbol type is a function, and the value of the symbol
14857c478bd9Sstevel@tonic-gate 		 * is non zero, then this is a special case.  This allows
14867c478bd9Sstevel@tonic-gate 		 * the resolution of a function address to the plt[] entry.
14877c478bd9Sstevel@tonic-gate 		 * See SPARC ABI, Dynamic Linking, Function Addresses for
14887c478bd9Sstevel@tonic-gate 		 * more details.
14897c478bd9Sstevel@tonic-gate 		 */
1490660acd81Srie 		} else if ((slp->sl_flags & LKUP_SPEC) &&
14917c478bd9Sstevel@tonic-gate 		    (FLAGS(ilmp) & FLG_RT_ISMAIN) && (sym->st_value != 0) &&
14927c478bd9Sstevel@tonic-gate 		    (ELF_ST_TYPE(sym->st_info) == STT_FUNC)) {
1493*08278a5eSRod Evans 			srp->sr_dmap = ilmp;
1494*08278a5eSRod Evans 			srp->sr_sym = sym;
14957c478bd9Sstevel@tonic-gate 			*binfo |= (DBG_BINFO_FOUND | DBG_BINFO_PLTADDR);
1496*08278a5eSRod Evans 
14979a411307Srie 			if ((FLAGS(ilmp) & FLG_RT_OBJINTPO) ||
14989a411307Srie 			    ((FLAGS(ilmp) & FLG_RT_SYMINTPO) &&
14999a411307Srie 			    is_sym_interposer(ilmp, sym)))
15007c478bd9Sstevel@tonic-gate 				*binfo |= DBG_BINFO_INTERPOSE;
1501*08278a5eSRod Evans 			return (1);
15027c478bd9Sstevel@tonic-gate 		}
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate 		/*
15057c478bd9Sstevel@tonic-gate 		 * Undefined symbol.
15067c478bd9Sstevel@tonic-gate 		 */
1507*08278a5eSRod Evans 		return (0);
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	/*
15117c478bd9Sstevel@tonic-gate 	 * We've found a match.  Determine if the defining object contains
15127c478bd9Sstevel@tonic-gate 	 * symbol binding information.
15137c478bd9Sstevel@tonic-gate 	 */
1514481bba9eSRod Evans 	if ((sip = SYMINFO(ilmp)) != NULL)
15159039eeafSab 		sip += ndx;
15167c478bd9Sstevel@tonic-gate 
151760758829Srie 	/*
151860758829Srie 	 * If this definition is a singleton, and we haven't followed a default
151960758829Srie 	 * symbol search knowing that we're looking for a singleton (presumably
152060758829Srie 	 * because the symbol definition has been changed since the referring
152160758829Srie 	 * object was built), then reject this binding so that the caller can
152260758829Srie 	 * fall back to a standard symbol search.
152360758829Srie 	 */
152460758829Srie 	if ((ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON) &&
152560758829Srie 	    (((slp->sl_flags & LKUP_STANDARD) == 0) ||
152660758829Srie 	    (((slp->sl_flags & LKUP_SINGLETON) == 0) &&
152760758829Srie 	    (LIST(ilmp)->lm_flags & LML_FLG_GROUPSEXIST)))) {
152860758829Srie 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
152960758829Srie 		    DBG_BNDREJ_SINGLE));
153060758829Srie 		*binfo |= BINFO_REJSINGLE;
153160758829Srie 		*binfo &= ~DBG_BINFO_MSK;
1532*08278a5eSRod Evans 		return (0);
153360758829Srie 	}
153460758829Srie 
15357c478bd9Sstevel@tonic-gate 	/*
15367c478bd9Sstevel@tonic-gate 	 * If this is a direct binding request, but the symbol definition has
15377c478bd9Sstevel@tonic-gate 	 * disabled directly binding to it (presumably because the symbol
15387c478bd9Sstevel@tonic-gate 	 * definition has been changed since the referring object was built),
153937ffaf83SRod Evans 	 * reject this binding so that the caller can fall back to a standard
154060758829Srie 	 * symbol search.
15417c478bd9Sstevel@tonic-gate 	 */
15427c478bd9Sstevel@tonic-gate 	if (sip && (slp->sl_flags & LKUP_DIRECT) &&
15437c478bd9Sstevel@tonic-gate 	    (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT)) {
154460758829Srie 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
154537ffaf83SRod Evans 		    DBG_BNDREJ_DIRECT));
154660758829Srie 		*binfo |= BINFO_REJDIRECT;
15477c478bd9Sstevel@tonic-gate 		*binfo &= ~DBG_BINFO_MSK;
1548*08278a5eSRod Evans 		return (0);
154937ffaf83SRod Evans 	}
155037ffaf83SRod Evans 
155137ffaf83SRod Evans 	/*
155237ffaf83SRod Evans 	 * If this is a binding request within an RTLD_GROUP family, and the
155337ffaf83SRod Evans 	 * symbol has disabled directly binding to it, reject this binding so
155437ffaf83SRod Evans 	 * that the caller can fall back to a standard symbol search.
155537ffaf83SRod Evans 	 *
155637ffaf83SRod Evans 	 * Effectively, an RTLD_GROUP family achieves what can now be
155737ffaf83SRod Evans 	 * established with direct bindings.  However, various symbols have
155837ffaf83SRod Evans 	 * been tagged as inappropriate for direct binding to (ie. libc:malloc).
155937ffaf83SRod Evans 	 *
156037ffaf83SRod Evans 	 * A symbol marked as no-direct cannot be used within a group without
156137ffaf83SRod Evans 	 * first ensuring that the symbol has not been interposed upon outside
156237ffaf83SRod Evans 	 * of the group.  A common example occurs when users implement their own
156337ffaf83SRod Evans 	 * version of malloc() in the executable.  Such a malloc() interposes on
156437ffaf83SRod Evans 	 * the libc:malloc, and this interposition must be honored within the
156537ffaf83SRod Evans 	 * group as well.
156637ffaf83SRod Evans 	 *
156737ffaf83SRod Evans 	 * Following any rejection, LKUP_WORLD is established as a means of
156837ffaf83SRod Evans 	 * overriding this test as we return to a standard search.
156937ffaf83SRod Evans 	 */
157037ffaf83SRod Evans 	if (sip && (sip->si_flags & SYMINFO_FLG_NOEXTDIRECT) &&
157137ffaf83SRod Evans 	    ((MODE(slp->sl_cmap) & (RTLD_GROUP | RTLD_WORLD)) == RTLD_GROUP) &&
157237ffaf83SRod Evans 	    ((slp->sl_flags & LKUP_WORLD) == 0)) {
157337ffaf83SRod Evans 		DBG_CALL(Dbg_bind_reject(slp->sl_cmap, ilmp, name,
157437ffaf83SRod Evans 		    DBG_BNDREJ_GROUP));
157537ffaf83SRod Evans 		*binfo |= BINFO_REJGROUP;
157637ffaf83SRod Evans 		*binfo &= ~DBG_BINFO_MSK;
1577*08278a5eSRod Evans 		return (0);
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 
1580*08278a5eSRod Evans 	/*
1581*08278a5eSRod Evans 	 * If this symbol is associated with capabilities, then each of the
1582*08278a5eSRod Evans 	 * capabilities instances needs to be compared against the system
1583*08278a5eSRod Evans 	 * capabilities.  The best instance will be chosen to satisfy this
1584*08278a5eSRod Evans 	 * binding.
1585*08278a5eSRod Evans 	 */
1586*08278a5eSRod Evans 	if (CAP(ilmp) && CAPINFO(ilmp) && ELF_C_GROUP(CAPINFO(ilmp)[ndx]) &&
1587*08278a5eSRod Evans 	    (cap_match(srp, ndx, symtabptr, strtabptr) == 0))
1588*08278a5eSRod Evans 		return (0);
1589*08278a5eSRod Evans 
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * Determine whether this object is acting as a filter.
15927c478bd9Sstevel@tonic-gate 	 */
15937c478bd9Sstevel@tonic-gate 	if (((flags1 = FLAGS1(ilmp)) & MSK_RT_FILTER) == 0)
1594*08278a5eSRod Evans 		return (1);
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate 	/*
15977c478bd9Sstevel@tonic-gate 	 * Determine if this object offers per-symbol filtering, and if so,
15987c478bd9Sstevel@tonic-gate 	 * whether this symbol references a filtee.
15997c478bd9Sstevel@tonic-gate 	 */
16007c478bd9Sstevel@tonic-gate 	if (sip && (flags1 & (FL1_RT_SYMSFLTR | FL1_RT_SYMAFLTR))) {
16017c478bd9Sstevel@tonic-gate 		/*
16027c478bd9Sstevel@tonic-gate 		 * If this is a standard filter reference, and no standard
16037c478bd9Sstevel@tonic-gate 		 * filtees remain to be inspected, we're done.  If this is an
16047c478bd9Sstevel@tonic-gate 		 * auxiliary filter reference, and no auxiliary filtees remain,
16057c478bd9Sstevel@tonic-gate 		 * we'll fall through in case any object filtering is available.
16067c478bd9Sstevel@tonic-gate 		 */
16077c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) &&
16087c478bd9Sstevel@tonic-gate 		    (SYMSFLTRCNT(ilmp) == 0))
1609*08278a5eSRod Evans 			return (0);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 		if ((sip->si_flags & SYMINFO_FLG_FILTER) ||
16127c478bd9Sstevel@tonic-gate 		    ((sip->si_flags & SYMINFO_FLG_AUXILIARY) &&
16137c478bd9Sstevel@tonic-gate 		    SYMAFLTRCNT(ilmp))) {
1614*08278a5eSRod Evans 			Sresult	sr;
1615*08278a5eSRod Evans 
1616*08278a5eSRod Evans 			/*
1617*08278a5eSRod Evans 			 * Initialize a local symbol result descriptor, using
1618*08278a5eSRod Evans 			 * the original symbol name.
1619*08278a5eSRod Evans 			 */
1620*08278a5eSRod Evans 			SRESULT_INIT(sr, slp->sl_name);
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 			/*
16237c478bd9Sstevel@tonic-gate 			 * This symbol has an associated filtee.  Lookup the
16247c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
16257c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
16267c478bd9Sstevel@tonic-gate 			 * filter, return an error, otherwise fall through to
16277c478bd9Sstevel@tonic-gate 			 * catch any object filtering that may be available.
16287c478bd9Sstevel@tonic-gate 			 */
1629*08278a5eSRod Evans 			if (elf_lookup_filtee(slp, &sr, binfo, sip->si_boundto,
1630*08278a5eSRod Evans 			    in_nfavl)) {
1631*08278a5eSRod Evans 				*srp = sr;
1632*08278a5eSRod Evans 				return (1);
1633*08278a5eSRod Evans 			}
16347c478bd9Sstevel@tonic-gate 			if (sip->si_flags & SYMINFO_FLG_FILTER)
1635*08278a5eSRod Evans 				return (0);
16367c478bd9Sstevel@tonic-gate 		}
16377c478bd9Sstevel@tonic-gate 	}
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	/*
16407c478bd9Sstevel@tonic-gate 	 * Determine if this object provides global filtering.
16417c478bd9Sstevel@tonic-gate 	 */
16427c478bd9Sstevel@tonic-gate 	if (flags1 & (FL1_RT_OBJSFLTR | FL1_RT_OBJAFLTR)) {
16437c478bd9Sstevel@tonic-gate 		if (OBJFLTRNDX(ilmp) != FLTR_DISABLED) {
1644*08278a5eSRod Evans 			Sresult	sr;
1645*08278a5eSRod Evans 
1646*08278a5eSRod Evans 			/*
1647*08278a5eSRod Evans 			 * Initialize a local symbol result descriptor, using
1648*08278a5eSRod Evans 			 * the original symbol name.
1649*08278a5eSRod Evans 			 */
1650*08278a5eSRod Evans 			SRESULT_INIT(sr, slp->sl_name);
1651*08278a5eSRod Evans 
16527c478bd9Sstevel@tonic-gate 			/*
16537c478bd9Sstevel@tonic-gate 			 * This object has an associated filtee.  Lookup the
16547c478bd9Sstevel@tonic-gate 			 * symbol in the filtee, and if it is found return it.
16557c478bd9Sstevel@tonic-gate 			 * If the symbol doesn't exist, and this is a standard
16567c478bd9Sstevel@tonic-gate 			 * filter, return and error, otherwise return the symbol
16577c478bd9Sstevel@tonic-gate 			 * within the filter itself.
16587c478bd9Sstevel@tonic-gate 			 */
1659*08278a5eSRod Evans 			if (elf_lookup_filtee(slp, &sr, binfo, OBJFLTRNDX(ilmp),
1660*08278a5eSRod Evans 			    in_nfavl)) {
1661*08278a5eSRod Evans 				*srp = sr;
1662*08278a5eSRod Evans 				return (1);
1663*08278a5eSRod Evans 			}
16647c478bd9Sstevel@tonic-gate 		}
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 		if (flags1 & FL1_RT_OBJSFLTR)
1667*08278a5eSRod Evans 			return (0);
16687c478bd9Sstevel@tonic-gate 	}
1669*08278a5eSRod Evans 	return (1);
16707c478bd9Sstevel@tonic-gate }
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate /*
16737c478bd9Sstevel@tonic-gate  * Create a new Rt_map structure for an ELF object and initialize
16747c478bd9Sstevel@tonic-gate  * all values.
16757c478bd9Sstevel@tonic-gate  */
16767c478bd9Sstevel@tonic-gate Rt_map *
167756deab07SRod Evans elf_new_lmp(Lm_list *lml, Aliste lmco, Fdesc *fdp, Addr addr, size_t msize,
167856deab07SRod Evans     void *odyn, int *in_nfavl)
16797c478bd9Sstevel@tonic-gate {
168056deab07SRod Evans 	const char	*name = fdp->fd_nname;
16817c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
16827c478bd9Sstevel@tonic-gate 	Ehdr		*ehdr = (Ehdr *)addr;
168356deab07SRod Evans 	Phdr		*phdr, *tphdr = NULL, *dphdr = NULL, *uphdr = NULL;
168456deab07SRod Evans 	Dyn		*dyn = (Dyn *)odyn;
168556deab07SRod Evans 	Cap		*cap = NULL;
168656deab07SRod Evans 	int		ndx;
168756deab07SRod Evans 	Addr		base, fltr = 0, audit = 0, cfile = 0, crle = 0;
168856deab07SRod Evans 	Xword		rpath = 0;
168956deab07SRod Evans 	size_t		lmsz, rtsz, epsz, dynsz = 0;
169056deab07SRod Evans 	uint_t		dyncnt = 0;
16917c478bd9Sstevel@tonic-gate 
169256deab07SRod Evans 	DBG_CALL(Dbg_file_elf(lml, name, addr, msize, lml->lm_lmidstr, lmco));
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 	/*
169556deab07SRod Evans 	 * If this is a shared object, the base address of the shared object is
169656deab07SRod Evans 	 * added to all address values defined within the object.  Otherwise, if
169756deab07SRod Evans 	 * this is an executable, all object addresses are used as is.
16987c478bd9Sstevel@tonic-gate 	 */
169956deab07SRod Evans 	if (ehdr->e_type == ET_EXEC)
170056deab07SRod Evans 		base = 0;
170156deab07SRod Evans 	else
170256deab07SRod Evans 		base = addr;
170356deab07SRod Evans 
170456deab07SRod Evans 	/*
170556deab07SRod Evans 	 * Traverse the program header table, picking off required items.  This
170656deab07SRod Evans 	 * traversal also provides for the sizing of the PT_DYNAMIC section.
170756deab07SRod Evans 	 */
170856deab07SRod Evans 	phdr = (Phdr *)((uintptr_t)ehdr + ehdr->e_phoff);
170956deab07SRod Evans 	for (ndx = 0; ndx < (int)ehdr->e_phnum; ndx++,
171056deab07SRod Evans 	    phdr = (Phdr *)((uintptr_t)phdr + ehdr->e_phentsize)) {
171156deab07SRod Evans 		switch (phdr->p_type) {
171256deab07SRod Evans 		case PT_DYNAMIC:
171356deab07SRod Evans 			dphdr = phdr;
171456deab07SRod Evans 			dyn = (Dyn *)((uintptr_t)phdr->p_vaddr + base);
171556deab07SRod Evans 			break;
171656deab07SRod Evans 		case PT_TLS:
171756deab07SRod Evans 			tphdr = phdr;
171856deab07SRod Evans 			break;
171956deab07SRod Evans 		case PT_SUNWCAP:
172056deab07SRod Evans 			cap = (Cap *)((uintptr_t)phdr->p_vaddr + base);
172156deab07SRod Evans 			break;
172256deab07SRod Evans 		case PT_SUNW_UNWIND:
17237e16fca0SAli Bahrami 		case PT_SUNW_EH_FRAME:
172456deab07SRod Evans 			uphdr = phdr;
172556deab07SRod Evans 			break;
172656deab07SRod Evans 		default:
172756deab07SRod Evans 			break;
172856deab07SRod Evans 		}
17297c478bd9Sstevel@tonic-gate 	}
17307c478bd9Sstevel@tonic-gate 
173156deab07SRod Evans 	/*
173256deab07SRod Evans 	 * Determine the number of PT_DYNAMIC entries for the DYNINFO()
173356deab07SRod Evans 	 * allocation.  Sadly, this is a little larger than we really need,
173456deab07SRod Evans 	 * as there are typically padding DT_NULL entries.  However, adding
173556deab07SRod Evans 	 * this data to the initial link-map allocation is a win.
173656deab07SRod Evans 	 */
173756deab07SRod Evans 	if (dyn) {
173856deab07SRod Evans 		dyncnt = dphdr->p_filesz / sizeof (Dyn);
173956deab07SRod Evans 		dynsz = dyncnt * sizeof (Dyninfo);
174056deab07SRod Evans 	}
174156deab07SRod Evans 
174256deab07SRod Evans 	/*
174356deab07SRod Evans 	 * Allocate space for the link-map, private elf information, and
174456deab07SRod Evans 	 * DYNINFO() data.  Once these are allocated and initialized,
174556deab07SRod Evans 	 * remove_so(0, lmp) can be used to tear down the link-map allocation
174656deab07SRod Evans 	 * should any failures occur.
174756deab07SRod Evans 	 */
174856deab07SRod Evans 	rtsz = S_DROUND(sizeof (Rt_map));
174956deab07SRod Evans 	epsz = S_DROUND(sizeof (Rt_elfp));
175056deab07SRod Evans 	lmsz = rtsz + epsz + dynsz;
175156deab07SRod Evans 	if ((lmp = calloc(lmsz, 1)) == NULL)
175256deab07SRod Evans 		return (NULL);
175356deab07SRod Evans 	ELFPRV(lmp) = (void *)((uintptr_t)lmp + rtsz);
175456deab07SRod Evans 	DYNINFO(lmp) = (Dyninfo *)((uintptr_t)lmp + rtsz + epsz);
175556deab07SRod Evans 	LMSIZE(lmp) = lmsz;
175656deab07SRod Evans 
17577c478bd9Sstevel@tonic-gate 	/*
17587c478bd9Sstevel@tonic-gate 	 * All fields not filled in were set to 0 by calloc.
17597c478bd9Sstevel@tonic-gate 	 */
176056deab07SRod Evans 	NAME(lmp) = (char *)name;
17617c478bd9Sstevel@tonic-gate 	ADDR(lmp) = addr;
17627c478bd9Sstevel@tonic-gate 	MSIZE(lmp) = msize;
17637c478bd9Sstevel@tonic-gate 	SYMINTP(lmp) = elf_find_sym;
17647c478bd9Sstevel@tonic-gate 	FCT(lmp) = &elf_fct;
17657c478bd9Sstevel@tonic-gate 	LIST(lmp) = lml;
17667c478bd9Sstevel@tonic-gate 	OBJFLTRNDX(lmp) = FLTR_DISABLED;
1767dffec89cSrie 	SORTVAL(lmp) = -1;
176856deab07SRod Evans 	DYN(lmp) = dyn;
176956deab07SRod Evans 	DYNINFOCNT(lmp) = dyncnt;
177056deab07SRod Evans 	PTUNWIND(lmp) = uphdr;
17717c478bd9Sstevel@tonic-gate 
177256deab07SRod Evans 	if (ehdr->e_type == ET_EXEC)
17737c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_FIXED;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 	/*
17767c478bd9Sstevel@tonic-gate 	 * Fill in rest of the link map entries with information from the file's
17777c478bd9Sstevel@tonic-gate 	 * dynamic structure.
17787c478bd9Sstevel@tonic-gate 	 */
177956deab07SRod Evans 	if (dyn) {
178075e7992aSrie 		uint_t		dynndx = 0;
178110a4fa49Srie 		Xword		pltpadsz = 0;
178210a4fa49Srie 		Rti_desc	*rti;
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 		/* CSTYLED */
178556deab07SRod Evans 		for ( ; dyn->d_tag != DT_NULL; ++dyn, dynndx++) {
178656deab07SRod Evans 			switch ((Xword)dyn->d_tag) {
17877c478bd9Sstevel@tonic-gate 			case DT_SYMTAB:
178856deab07SRod Evans 				SYMTAB(lmp) = (void *)(dyn->d_un.d_ptr + base);
17897c478bd9Sstevel@tonic-gate 				break;
17909039eeafSab 			case DT_SUNW_SYMTAB:
17919039eeafSab 				SUNWSYMTAB(lmp) =
179256deab07SRod Evans 				    (void *)(dyn->d_un.d_ptr + base);
17939039eeafSab 				break;
17949039eeafSab 			case DT_SUNW_SYMSZ:
179556deab07SRod Evans 				SUNWSYMSZ(lmp) = dyn->d_un.d_val;
17969039eeafSab 				break;
17977c478bd9Sstevel@tonic-gate 			case DT_STRTAB:
179856deab07SRod Evans 				STRTAB(lmp) = (void *)(dyn->d_un.d_ptr + base);
17997c478bd9Sstevel@tonic-gate 				break;
18007c478bd9Sstevel@tonic-gate 			case DT_SYMENT:
180156deab07SRod Evans 				SYMENT(lmp) = dyn->d_un.d_val;
18027c478bd9Sstevel@tonic-gate 				break;
18037c478bd9Sstevel@tonic-gate 			case DT_FEATURE_1:
180456deab07SRod Evans 				dyn->d_un.d_val |= DTF_1_PARINIT;
180556deab07SRod Evans 				if (dyn->d_un.d_val & DTF_1_CONFEXP)
18067c478bd9Sstevel@tonic-gate 					crle = 1;
18077c478bd9Sstevel@tonic-gate 				break;
18087c478bd9Sstevel@tonic-gate 			case DT_MOVESZ:
180956deab07SRod Evans 				MOVESZ(lmp) = dyn->d_un.d_val;
18107c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_MOVE;
18117c478bd9Sstevel@tonic-gate 				break;
18127c478bd9Sstevel@tonic-gate 			case DT_MOVEENT:
181356deab07SRod Evans 				MOVEENT(lmp) = dyn->d_un.d_val;
18147c478bd9Sstevel@tonic-gate 				break;
18157c478bd9Sstevel@tonic-gate 			case DT_MOVETAB:
181656deab07SRod Evans 				MOVETAB(lmp) = (void *)(dyn->d_un.d_ptr + base);
18177c478bd9Sstevel@tonic-gate 				break;
18187c478bd9Sstevel@tonic-gate 			case DT_REL:
18197c478bd9Sstevel@tonic-gate 			case DT_RELA:
18207c478bd9Sstevel@tonic-gate 				/*
182175e7992aSrie 				 * At this time, ld.so. can only handle one
182275e7992aSrie 				 * type of relocation per object.
18237c478bd9Sstevel@tonic-gate 				 */
182456deab07SRod Evans 				REL(lmp) = (void *)(dyn->d_un.d_ptr + base);
18257c478bd9Sstevel@tonic-gate 				break;
18267c478bd9Sstevel@tonic-gate 			case DT_RELSZ:
18277c478bd9Sstevel@tonic-gate 			case DT_RELASZ:
182856deab07SRod Evans 				RELSZ(lmp) = dyn->d_un.d_val;
18297c478bd9Sstevel@tonic-gate 				break;
18307c478bd9Sstevel@tonic-gate 			case DT_RELENT:
18317c478bd9Sstevel@tonic-gate 			case DT_RELAENT:
183256deab07SRod Evans 				RELENT(lmp) = dyn->d_un.d_val;
18337c478bd9Sstevel@tonic-gate 				break;
18347c478bd9Sstevel@tonic-gate 			case DT_RELCOUNT:
18357c478bd9Sstevel@tonic-gate 			case DT_RELACOUNT:
183656deab07SRod Evans 				RELACOUNT(lmp) = (uint_t)dyn->d_un.d_val;
18377c478bd9Sstevel@tonic-gate 				break;
18387c478bd9Sstevel@tonic-gate 			case DT_HASH:
183956deab07SRod Evans 				HASH(lmp) = (uint_t *)(dyn->d_un.d_ptr + base);
18407c478bd9Sstevel@tonic-gate 				break;
18417c478bd9Sstevel@tonic-gate 			case DT_PLTGOT:
184256deab07SRod Evans 				PLTGOT(lmp) =
184356deab07SRod Evans 				    (uint_t *)(dyn->d_un.d_ptr + base);
18447c478bd9Sstevel@tonic-gate 				break;
18457c478bd9Sstevel@tonic-gate 			case DT_PLTRELSZ:
184656deab07SRod Evans 				PLTRELSZ(lmp) = dyn->d_un.d_val;
18477c478bd9Sstevel@tonic-gate 				break;
18487c478bd9Sstevel@tonic-gate 			case DT_JMPREL:
184956deab07SRod Evans 				JMPREL(lmp) = (void *)(dyn->d_un.d_ptr + base);
18507c478bd9Sstevel@tonic-gate 				break;
18517c478bd9Sstevel@tonic-gate 			case DT_INIT:
185256deab07SRod Evans 				if (dyn->d_un.d_ptr != NULL)
18535b59e4caSab 					INIT(lmp) =
185456deab07SRod Evans 					    (void (*)())(dyn->d_un.d_ptr +
185556deab07SRod Evans 					    base);
18567c478bd9Sstevel@tonic-gate 				break;
18577c478bd9Sstevel@tonic-gate 			case DT_FINI:
185856deab07SRod Evans 				if (dyn->d_un.d_ptr != NULL)
18595b59e4caSab 					FINI(lmp) =
186056deab07SRod Evans 					    (void (*)())(dyn->d_un.d_ptr +
186156deab07SRod Evans 					    base);
18627c478bd9Sstevel@tonic-gate 				break;
18637c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAY:
186456deab07SRod Evans 				INITARRAY(lmp) = (Addr *)(dyn->d_un.d_ptr +
18657c478bd9Sstevel@tonic-gate 				    base);
18667c478bd9Sstevel@tonic-gate 				break;
18677c478bd9Sstevel@tonic-gate 			case DT_INIT_ARRAYSZ:
186856deab07SRod Evans 				INITARRAYSZ(lmp) = (uint_t)dyn->d_un.d_val;
18697c478bd9Sstevel@tonic-gate 				break;
18707c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAY:
187156deab07SRod Evans 				FINIARRAY(lmp) = (Addr *)(dyn->d_un.d_ptr +
18727c478bd9Sstevel@tonic-gate 				    base);
18737c478bd9Sstevel@tonic-gate 				break;
18747c478bd9Sstevel@tonic-gate 			case DT_FINI_ARRAYSZ:
187556deab07SRod Evans 				FINIARRAYSZ(lmp) = (uint_t)dyn->d_un.d_val;
18767c478bd9Sstevel@tonic-gate 				break;
18777c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAY:
187856deab07SRod Evans 				PREINITARRAY(lmp) = (Addr *)(dyn->d_un.d_ptr +
18797c478bd9Sstevel@tonic-gate 				    base);
18807c478bd9Sstevel@tonic-gate 				break;
18817c478bd9Sstevel@tonic-gate 			case DT_PREINIT_ARRAYSZ:
188256deab07SRod Evans 				PREINITARRAYSZ(lmp) = (uint_t)dyn->d_un.d_val;
18837c478bd9Sstevel@tonic-gate 				break;
18847c478bd9Sstevel@tonic-gate 			case DT_RPATH:
18857c478bd9Sstevel@tonic-gate 			case DT_RUNPATH:
188656deab07SRod Evans 				rpath = dyn->d_un.d_val;
18877c478bd9Sstevel@tonic-gate 				break;
18887c478bd9Sstevel@tonic-gate 			case DT_FILTER:
188956deab07SRod Evans 				fltr = dyn->d_un.d_val;
189075e7992aSrie 				OBJFLTRNDX(lmp) = dynndx;
18917c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJSFLTR;
18927c478bd9Sstevel@tonic-gate 				break;
18937c478bd9Sstevel@tonic-gate 			case DT_AUXILIARY:
18947c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
189556deab07SRod Evans 					fltr = dyn->d_un.d_val;
189675e7992aSrie 					OBJFLTRNDX(lmp) = dynndx;
18977c478bd9Sstevel@tonic-gate 				}
18987c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_OBJAFLTR;
18997c478bd9Sstevel@tonic-gate 				break;
19007c478bd9Sstevel@tonic-gate 			case DT_SUNW_FILTER:
19017c478bd9Sstevel@tonic-gate 				SYMSFLTRCNT(lmp)++;
19027c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMSFLTR;
19037c478bd9Sstevel@tonic-gate 				break;
19047c478bd9Sstevel@tonic-gate 			case DT_SUNW_AUXILIARY:
19057c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUXFLTR)) {
19067c478bd9Sstevel@tonic-gate 					SYMAFLTRCNT(lmp)++;
19077c478bd9Sstevel@tonic-gate 				}
19087c478bd9Sstevel@tonic-gate 				FLAGS1(lmp) |= FL1_RT_SYMAFLTR;
19097c478bd9Sstevel@tonic-gate 				break;
19107c478bd9Sstevel@tonic-gate 			case DT_DEPAUDIT:
19117c478bd9Sstevel@tonic-gate 				if (!(rtld_flags & RT_FL_NOAUDIT))
191256deab07SRod Evans 					audit = dyn->d_un.d_val;
19137c478bd9Sstevel@tonic-gate 				break;
19147c478bd9Sstevel@tonic-gate 			case DT_CONFIG:
191556deab07SRod Evans 				cfile = dyn->d_un.d_val;
19167c478bd9Sstevel@tonic-gate 				break;
19177c478bd9Sstevel@tonic-gate 			case DT_DEBUG:
19187c478bd9Sstevel@tonic-gate 				/*
19197c478bd9Sstevel@tonic-gate 				 * DT_DEBUG entries are only created in
19207c478bd9Sstevel@tonic-gate 				 * dynamic objects that require an interpretor
19217c478bd9Sstevel@tonic-gate 				 * (ie. all dynamic executables and some shared
19227c478bd9Sstevel@tonic-gate 				 * objects), and provide for a hand-shake with
1923dde769a2SRod Evans 				 * old debuggers.  This entry is initialized to
1924dde769a2SRod Evans 				 * zero by the link-editor.  If a debugger is
1925dde769a2SRod Evans 				 * monitoring us, and has updated this entry,
1926dde769a2SRod Evans 				 * set the debugger monitor flag, and finish
1927dde769a2SRod Evans 				 * initializing the debugging structure.  See
1928dde769a2SRod Evans 				 * setup().  Also, switch off any configuration
1929dde769a2SRod Evans 				 * object use as most debuggers can't handle
1930dde769a2SRod Evans 				 * fixed dynamic executables as dependencies.
19317c478bd9Sstevel@tonic-gate 				 */
193256deab07SRod Evans 				if (dyn->d_un.d_ptr)
19337c478bd9Sstevel@tonic-gate 					rtld_flags |=
19347c478bd9Sstevel@tonic-gate 					    (RT_FL_DEBUGGER | RT_FL_NOOBJALT);
193556deab07SRod Evans 				dyn->d_un.d_ptr = (Addr)&r_debug;
19367c478bd9Sstevel@tonic-gate 				break;
19377c478bd9Sstevel@tonic-gate 			case DT_VERNEED:
193856deab07SRod Evans 				VERNEED(lmp) = (Verneed *)(dyn->d_un.d_ptr +
19397c478bd9Sstevel@tonic-gate 				    base);
19407c478bd9Sstevel@tonic-gate 				break;
19417c478bd9Sstevel@tonic-gate 			case DT_VERNEEDNUM:
19427c478bd9Sstevel@tonic-gate 				/* LINTED */
194356deab07SRod Evans 				VERNEEDNUM(lmp) = (int)dyn->d_un.d_val;
19447c478bd9Sstevel@tonic-gate 				break;
19457c478bd9Sstevel@tonic-gate 			case DT_VERDEF:
194656deab07SRod Evans 				VERDEF(lmp) = (Verdef *)(dyn->d_un.d_ptr +
194756deab07SRod Evans 				    base);
19487c478bd9Sstevel@tonic-gate 				break;
19497c478bd9Sstevel@tonic-gate 			case DT_VERDEFNUM:
19507c478bd9Sstevel@tonic-gate 				/* LINTED */
195156deab07SRod Evans 				VERDEFNUM(lmp) = (int)dyn->d_un.d_val;
19527c478bd9Sstevel@tonic-gate 				break;
19533b41b08bSab 			case DT_VERSYM:
1954d840867fSab 				/*
1955d840867fSab 				 * The Solaris ld does not produce DT_VERSYM,
1956d840867fSab 				 * but the GNU ld does, in order to support
1957d840867fSab 				 * their style of versioning, which differs
1958d840867fSab 				 * from ours in some ways, while using the
1959d840867fSab 				 * same data structures. The presence of
1960d840867fSab 				 * DT_VERSYM therefore means that GNU
1961d840867fSab 				 * versioning rules apply to the given file.
1962d840867fSab 				 * If DT_VERSYM is not present, then Solaris
1963d840867fSab 				 * versioning rules apply.
1964d840867fSab 				 */
196556deab07SRod Evans 				VERSYM(lmp) = (Versym *)(dyn->d_un.d_ptr +
196656deab07SRod Evans 				    base);
19673b41b08bSab 				break;
19687c478bd9Sstevel@tonic-gate 			case DT_BIND_NOW:
196956deab07SRod Evans 				if ((dyn->d_un.d_val & DF_BIND_NOW) &&
197056deab07SRod Evans 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
19717c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
19727c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
19737c478bd9Sstevel@tonic-gate 				}
19747c478bd9Sstevel@tonic-gate 				break;
19757c478bd9Sstevel@tonic-gate 			case DT_FLAGS:
197656deab07SRod Evans 				FLAGS1(lmp) |= FL1_RT_DTFLAGS;
197756deab07SRod Evans 				if (dyn->d_un.d_val & DF_SYMBOLIC)
19787c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_SYMBOLIC;
197956deab07SRod Evans 				if ((dyn->d_un.d_val & DF_BIND_NOW) &&
1980dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
19817c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
19827c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
19837c478bd9Sstevel@tonic-gate 				}
1984d326b23bSrie 				/*
1985d326b23bSrie 				 * Capture any static TLS use, and enforce that
1986d326b23bSrie 				 * this object be non-deletable.
1987d326b23bSrie 				 */
198856deab07SRod Evans 				if (dyn->d_un.d_val & DF_STATIC_TLS) {
1989d326b23bSrie 					FLAGS1(lmp) |= FL1_RT_TLSSTAT;
1990d326b23bSrie 					MODE(lmp) |= RTLD_NODELETE;
1991d326b23bSrie 				}
19927c478bd9Sstevel@tonic-gate 				break;
19937c478bd9Sstevel@tonic-gate 			case DT_FLAGS_1:
199456deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_DISPRELPND)
19957c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_DISPREL;
199656deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_GROUP)
19977c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |=
19982017c965SRod Evans 					    (FLG_RT_SETGROUP | FLG_RT_PUBHDL);
199956deab07SRod Evans 				if ((dyn->d_un.d_val & DF_1_NOW) &&
2000dffec89cSrie 				    ((rtld_flags2 & RT_FL2_BINDLAZY) == 0)) {
20017c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NOW;
20027c478bd9Sstevel@tonic-gate 					MODE(lmp) &= ~RTLD_LAZY;
20037c478bd9Sstevel@tonic-gate 				}
200456deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NODELETE)
20057c478bd9Sstevel@tonic-gate 					MODE(lmp) |= RTLD_NODELETE;
200656deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_INITFIRST)
20077c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_INITFRST;
200856deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NOOPEN)
20097c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NOOPEN;
201056deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_LOADFLTR)
20117c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_LOADFLTR;
201256deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NODUMP)
20137c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_NODUMP;
201456deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_CONFALT)
20157c478bd9Sstevel@tonic-gate 					crle = 1;
201656deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_DIRECT)
20179a411307Srie 					FLAGS1(lmp) |= FL1_RT_DIRECT;
201856deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_NODEFLIB)
20197c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_NODEFLIB;
202056deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_ENDFILTEE)
20217c478bd9Sstevel@tonic-gate 					FLAGS1(lmp) |= FL1_RT_ENDFILTE;
202256deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_TRANS)
20237c478bd9Sstevel@tonic-gate 					FLAGS(lmp) |= FLG_RT_TRANS;
202456deab07SRod Evans 
20257247f888Srie 				/*
20267247f888Srie 				 * Global auditing is only meaningful when
20277247f888Srie 				 * specified by the initiating object of the
20287247f888Srie 				 * process - typically the dynamic executable.
2029*08278a5eSRod Evans 				 * If this is the initiating object, its link-
20307247f888Srie 				 * map will not yet have been added to the
20317247f888Srie 				 * link-map list, and consequently the link-map
20327247f888Srie 				 * list is empty.  (see setup()).
20337247f888Srie 				 */
203456deab07SRod Evans 				if (dyn->d_un.d_val & DF_1_GLOBAUDIT) {
2035481bba9eSRod Evans 					if (lml_main.lm_head == NULL)
20367247f888Srie 						FLAGS1(lmp) |= FL1_RT_GLOBAUD;
20377247f888Srie 					else
20387247f888Srie 						DBG_CALL(Dbg_audit_ignore(lmp));
20397247f888Srie 				}
20407247f888Srie 
20417c478bd9Sstevel@tonic-gate 				/*
20427c478bd9Sstevel@tonic-gate 				 * If this object identifies itself as an
20437c478bd9Sstevel@tonic-gate 				 * interposer, but relocation processing has
20447c478bd9Sstevel@tonic-gate 				 * already started, then demote it.  It's too
20457c478bd9Sstevel@tonic-gate 				 * late to guarantee complete interposition.
20467c478bd9Sstevel@tonic-gate 				 */
2047a953e2b1Srie 				/* BEGIN CSTYLED */
204856deab07SRod Evans 				if (dyn->d_un.d_val &
20499a411307Srie 				    (DF_1_INTERPOSE | DF_1_SYMINTPOSE)) {
20509a411307Srie 				    if (lml->lm_flags & LML_FLG_STARTREL) {
20515aefb655Srie 					DBG_CALL(Dbg_util_intoolate(lmp));
20527c478bd9Sstevel@tonic-gate 					if (lml->lm_flags & LML_FLG_TRC_ENABLE)
20537c478bd9Sstevel@tonic-gate 					    (void) printf(
20547c478bd9Sstevel@tonic-gate 						MSG_INTL(MSG_LDD_REL_ERR2),
20557c478bd9Sstevel@tonic-gate 						NAME(lmp));
205656deab07SRod Evans 				    } else if (dyn->d_un.d_val & DF_1_INTERPOSE)
20579a411307Srie 					FLAGS(lmp) |= FLG_RT_OBJINTPO;
20589a411307Srie 				    else
20599a411307Srie 					FLAGS(lmp) |= FLG_RT_SYMINTPO;
20607c478bd9Sstevel@tonic-gate 				}
2061a953e2b1Srie 				/* END CSTYLED */
20627c478bd9Sstevel@tonic-gate 				break;
20637c478bd9Sstevel@tonic-gate 			case DT_SYMINFO:
206456deab07SRod Evans 				SYMINFO(lmp) = (Syminfo *)(dyn->d_un.d_ptr +
20657c478bd9Sstevel@tonic-gate 				    base);
20667c478bd9Sstevel@tonic-gate 				break;
20677c478bd9Sstevel@tonic-gate 			case DT_SYMINENT:
206856deab07SRod Evans 				SYMINENT(lmp) = dyn->d_un.d_val;
20697c478bd9Sstevel@tonic-gate 				break;
20707c478bd9Sstevel@tonic-gate 			case DT_PLTPAD:
207156deab07SRod Evans 				PLTPAD(lmp) = (void *)(dyn->d_un.d_ptr + base);
20727c478bd9Sstevel@tonic-gate 				break;
20737c478bd9Sstevel@tonic-gate 			case DT_PLTPADSZ:
207456deab07SRod Evans 				pltpadsz = dyn->d_un.d_val;
20757c478bd9Sstevel@tonic-gate 				break;
20767c478bd9Sstevel@tonic-gate 			case DT_SUNW_RTLDINF:
20777c478bd9Sstevel@tonic-gate 				/*
207810a4fa49Srie 				 * Maintain a list of RTLDINFO structures.
207910a4fa49Srie 				 * Typically, libc is the only supplier, and
208010a4fa49Srie 				 * only one structure is provided.  However,
208110a4fa49Srie 				 * multiple suppliers and multiple structures
208210a4fa49Srie 				 * are supported.  For example, one structure
208310a4fa49Srie 				 * may provide thread_init, and another
208410a4fa49Srie 				 * structure may provide atexit reservations.
20857c478bd9Sstevel@tonic-gate 				 */
2086dde769a2SRod Evans 				if ((rti = alist_append(&lml->lm_rti, NULL,
208756deab07SRod Evans 				    sizeof (Rti_desc),
208856deab07SRod Evans 				    AL_CNT_RTLDINFO)) == NULL) {
20897c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
209056deab07SRod Evans 					return (NULL);
20917c478bd9Sstevel@tonic-gate 				}
209210a4fa49Srie 				rti->rti_lmp = lmp;
209356deab07SRod Evans 				rti->rti_info = (void *)(dyn->d_un.d_ptr +
209456deab07SRod Evans 				    base);
20957c478bd9Sstevel@tonic-gate 				break;
2096d579eb63Sab 			case DT_SUNW_SORTENT:
209756deab07SRod Evans 				SUNWSORTENT(lmp) = dyn->d_un.d_val;
2098d579eb63Sab 				break;
2099d579eb63Sab 			case DT_SUNW_SYMSORT:
2100d579eb63Sab 				SUNWSYMSORT(lmp) =
210156deab07SRod Evans 				    (void *)(dyn->d_un.d_ptr + base);
2102d579eb63Sab 				break;
2103d579eb63Sab 			case DT_SUNW_SYMSORTSZ:
210456deab07SRod Evans 				SUNWSYMSORTSZ(lmp) = dyn->d_un.d_val;
2105d579eb63Sab 				break;
21067c478bd9Sstevel@tonic-gate 			case DT_DEPRECATED_SPARC_REGISTER:
21077c478bd9Sstevel@tonic-gate 			case M_DT_REGISTER:
21087c478bd9Sstevel@tonic-gate 				FLAGS(lmp) |= FLG_RT_REGSYMS;
21097c478bd9Sstevel@tonic-gate 				break;
2110*08278a5eSRod Evans 			case DT_SUNW_CAP:
2111*08278a5eSRod Evans 				CAP(lmp) = (void *)(dyn->d_un.d_ptr + base);
2112*08278a5eSRod Evans 				break;
2113*08278a5eSRod Evans 			case DT_SUNW_CAPINFO:
2114*08278a5eSRod Evans 				CAPINFO(lmp) = (void *)(dyn->d_un.d_ptr + base);
2115*08278a5eSRod Evans 				break;
2116*08278a5eSRod Evans 			case DT_SUNW_CAPCHAIN:
2117*08278a5eSRod Evans 				CAPCHAIN(lmp) = (void *)(dyn->d_un.d_ptr +
2118*08278a5eSRod Evans 				    base);
2119*08278a5eSRod Evans 				break;
2120*08278a5eSRod Evans 			case DT_SUNW_CAPCHAINENT:
2121*08278a5eSRod Evans 				CAPCHAINENT(lmp) = dyn->d_un.d_val;
2122*08278a5eSRod Evans 				break;
2123*08278a5eSRod Evans 			case DT_SUNW_CAPCHAINSZ:
2124*08278a5eSRod Evans 				CAPCHAINSZ(lmp) = dyn->d_un.d_val;
2125*08278a5eSRod Evans 				break;
21267c478bd9Sstevel@tonic-gate 			}
21277c478bd9Sstevel@tonic-gate 		}
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 		if (PLTPAD(lmp)) {
21307c478bd9Sstevel@tonic-gate 			if (pltpadsz == (Xword)0)
2131481bba9eSRod Evans 				PLTPAD(lmp) = NULL;
21327c478bd9Sstevel@tonic-gate 			else
21337c478bd9Sstevel@tonic-gate 				PLTPADEND(lmp) = (void *)((Addr)PLTPAD(lmp) +
21347c478bd9Sstevel@tonic-gate 				    pltpadsz);
21357c478bd9Sstevel@tonic-gate 		}
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 
21389039eeafSab 	/*
21399039eeafSab 	 * A dynsym contains only global functions. We want to have
21409039eeafSab 	 * a version of it that also includes local functions, so that
21419039eeafSab 	 * dladdr() will be able to report names for local functions
21429039eeafSab 	 * when used to generate a stack trace for a stripped file.
21439039eeafSab 	 * This version of the dynsym is provided via DT_SUNW_SYMTAB.
21449039eeafSab 	 *
21459039eeafSab 	 * In producing DT_SUNW_SYMTAB, ld uses a non-obvious trick
21469039eeafSab 	 * in order to avoid having to have two copies of the global
21479039eeafSab 	 * symbols held in DT_SYMTAB: The local symbols are placed in
21489039eeafSab 	 * a separate section than the globals in the dynsym, but the
21499039eeafSab 	 * linker conspires to put the data for these two sections adjacent
21509039eeafSab 	 * to each other. DT_SUNW_SYMTAB points at the top of the local
21519039eeafSab 	 * symbols, and DT_SUNW_SYMSZ is the combined length of both tables.
21529039eeafSab 	 *
21539039eeafSab 	 * If the two sections are not adjacent, then something went wrong
21549039eeafSab 	 * at link time. We use ASSERT to kill the process if this is
21559039eeafSab 	 * a debug build. In a production build, we will silently ignore
21569039eeafSab 	 * the presence of the .ldynsym and proceed. We can detect this
21579039eeafSab 	 * situation by checking to see that DT_SYMTAB lies in
21589039eeafSab 	 * the range given by DT_SUNW_SYMTAB/DT_SUNW_SYMSZ.
21599039eeafSab 	 */
21609039eeafSab 	if ((SUNWSYMTAB(lmp) != NULL) &&
21619039eeafSab 	    (((char *)SYMTAB(lmp) <= (char *)SUNWSYMTAB(lmp)) ||
21629039eeafSab 	    (((char *)SYMTAB(lmp) >=
21639039eeafSab 	    (SUNWSYMSZ(lmp) + (char *)SUNWSYMTAB(lmp)))))) {
21649039eeafSab 		ASSERT(0);
21659039eeafSab 		SUNWSYMTAB(lmp) = NULL;
21669039eeafSab 		SUNWSYMSZ(lmp) = 0;
21679039eeafSab 	}
21689039eeafSab 
21697c478bd9Sstevel@tonic-gate 	/*
21707c478bd9Sstevel@tonic-gate 	 * If configuration file use hasn't been disabled, and a configuration
21717c478bd9Sstevel@tonic-gate 	 * file hasn't already been set via an environment variable, see if any
21727c478bd9Sstevel@tonic-gate 	 * application specific configuration file is specified.  An LD_CONFIG
21737c478bd9Sstevel@tonic-gate 	 * setting is used first, but if this image was generated via crle(1)
21747c478bd9Sstevel@tonic-gate 	 * then a default configuration file is a fall-back.
21757c478bd9Sstevel@tonic-gate 	 */
2176481bba9eSRod Evans 	if ((!(rtld_flags & RT_FL_NOCFG)) && (config->c_name == NULL)) {
21777c478bd9Sstevel@tonic-gate 		if (cfile)
21787c478bd9Sstevel@tonic-gate 			config->c_name = (const char *)(cfile +
21797c478bd9Sstevel@tonic-gate 			    (char *)STRTAB(lmp));
218056deab07SRod Evans 		else if (crle)
21817c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_CONFAPP;
21827c478bd9Sstevel@tonic-gate 	}
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	if (rpath)
21857c478bd9Sstevel@tonic-gate 		RPATH(lmp) = (char *)(rpath + (char *)STRTAB(lmp));
218656deab07SRod Evans 	if (fltr)
218756deab07SRod Evans 		REFNAME(lmp) = (char *)(fltr + (char *)STRTAB(lmp));
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	/*
21907c478bd9Sstevel@tonic-gate 	 * For Intel ABI compatibility.  It's possible that a JMPREL can be
21917c478bd9Sstevel@tonic-gate 	 * specified without any other relocations (e.g. a dynamic executable
21927c478bd9Sstevel@tonic-gate 	 * normally only contains .plt relocations).  If this is the case then
21937c478bd9Sstevel@tonic-gate 	 * no REL, RELSZ or RELENT will have been created.  For us to be able
21947c478bd9Sstevel@tonic-gate 	 * to traverse the .plt relocations under LD_BIND_NOW we need to know
21957c478bd9Sstevel@tonic-gate 	 * the RELENT for these relocations.  Refer to elf_reloc() for more
21967c478bd9Sstevel@tonic-gate 	 * details.
21977c478bd9Sstevel@tonic-gate 	 */
21987c478bd9Sstevel@tonic-gate 	if (!RELENT(lmp) && JMPREL(lmp))
219956deab07SRod Evans 		RELENT(lmp) = sizeof (M_RELOC);
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	/*
22027c478bd9Sstevel@tonic-gate 	 * Establish any per-object auditing.  If we're establishing `main's
22037c478bd9Sstevel@tonic-gate 	 * link-map its too early to go searching for audit objects so just
22047c478bd9Sstevel@tonic-gate 	 * hold the object name for later (see setup()).
22057c478bd9Sstevel@tonic-gate 	 */
22067c478bd9Sstevel@tonic-gate 	if (audit) {
22077c478bd9Sstevel@tonic-gate 		char	*cp = audit + (char *)STRTAB(lmp);
22087c478bd9Sstevel@tonic-gate 
22097c478bd9Sstevel@tonic-gate 		if (*cp) {
22107c478bd9Sstevel@tonic-gate 			if (((AUDITORS(lmp) =
221156deab07SRod Evans 			    calloc(1, sizeof (Audit_desc))) == NULL) ||
221256deab07SRod Evans 			    ((AUDITORS(lmp)->ad_name = strdup(cp)) == NULL)) {
22137c478bd9Sstevel@tonic-gate 				remove_so(0, lmp);
221456deab07SRod Evans 				return (NULL);
22157c478bd9Sstevel@tonic-gate 			}
221641072f3cSrie 			if (lml_main.lm_head) {
22179aa23310Srie 				if (audit_setup(lmp, AUDITORS(lmp), 0,
22189aa23310Srie 				    in_nfavl) == 0) {
22197c478bd9Sstevel@tonic-gate 					remove_so(0, lmp);
222056deab07SRod Evans 					return (NULL);
22217c478bd9Sstevel@tonic-gate 				}
222256deab07SRod Evans 				AFLAGS(lmp) |= AUDITORS(lmp)->ad_flags;
22237c478bd9Sstevel@tonic-gate 				lml->lm_flags |= LML_FLG_LOCAUDIT;
22247c478bd9Sstevel@tonic-gate 			}
22257c478bd9Sstevel@tonic-gate 		}
22267c478bd9Sstevel@tonic-gate 	}
22277c478bd9Sstevel@tonic-gate 
222856deab07SRod Evans 	if (tphdr && (tls_assign(lml, lmp, tphdr) == 0)) {
22297c478bd9Sstevel@tonic-gate 		remove_so(0, lmp);
223056deab07SRod Evans 		return (NULL);
22317c478bd9Sstevel@tonic-gate 	}
22327c478bd9Sstevel@tonic-gate 
2233*08278a5eSRod Evans 	/*
2234*08278a5eSRod Evans 	 * A capabilities section should be identified by a DT_SUNW_CAP entry,
2235*08278a5eSRod Evans 	 * and if non-empty object capabilities are included, a PT_SUNWCAP
2236*08278a5eSRod Evans 	 * header should reference the section.  Make sure CAP() is set
2237*08278a5eSRod Evans 	 * regardless.
2238*08278a5eSRod Evans 	 */
2239*08278a5eSRod Evans 	if ((CAP(lmp) == NULL) && cap)
2240*08278a5eSRod Evans 		CAP(lmp) = cap;
2241*08278a5eSRod Evans 
2242*08278a5eSRod Evans 	/*
2243*08278a5eSRod Evans 	 * Make sure any capabilities information or chain can be handled.
2244*08278a5eSRod Evans 	 */
2245*08278a5eSRod Evans 	if (CAPINFO(lmp) && (CAPINFO(lmp)[0] > CAPINFO_CURRENT))
2246*08278a5eSRod Evans 		CAPINFO(lmp) = NULL;
2247*08278a5eSRod Evans 	if (CAPCHAIN(lmp) && (CAPCHAIN(lmp)[0] > CAPCHAIN_CURRENT))
2248*08278a5eSRod Evans 		CAPCHAIN(lmp) = NULL;
2249*08278a5eSRod Evans 
2250*08278a5eSRod Evans 	/*
2251*08278a5eSRod Evans 	 * As part of processing dependencies, a file descriptor is populated
2252*08278a5eSRod Evans 	 * with capabilities information following validation.
2253*08278a5eSRod Evans 	 */
2254*08278a5eSRod Evans 	if (fdp->fd_flags & FLG_FD_ALTCHECK) {
2255*08278a5eSRod Evans 		FLAGS1(lmp) |= FL1_RT_ALTCHECK;
2256*08278a5eSRod Evans 		CAPSET(lmp) = fdp->fd_scapset;
2257*08278a5eSRod Evans 
2258*08278a5eSRod Evans 		if (fdp->fd_flags & FLG_FD_ALTCAP)
2259*08278a5eSRod Evans 			FLAGS1(lmp) |= FL1_RT_ALTCAP;
2260*08278a5eSRod Evans 
2261*08278a5eSRod Evans 	} else if ((cap = CAP(lmp)) != NULL) {
2262*08278a5eSRod Evans 		/*
2263*08278a5eSRod Evans 		 * Processing of the a.out and ld.so.1 does not involve a file
2264*08278a5eSRod Evans 		 * descriptor as exec() did all the work, so capture the
2265*08278a5eSRod Evans 		 * capabilities for these cases.
2266*08278a5eSRod Evans 		 */
2267*08278a5eSRod Evans 		while (cap->c_tag != CA_SUNW_NULL) {
2268*08278a5eSRod Evans 			switch (cap->c_tag) {
2269*08278a5eSRod Evans 			case CA_SUNW_HW_1:
2270*08278a5eSRod Evans 				CAPSET(lmp).sc_hw_1 = cap->c_un.c_val;
2271*08278a5eSRod Evans 				break;
2272*08278a5eSRod Evans 			case CA_SUNW_SF_1:
2273*08278a5eSRod Evans 				CAPSET(lmp).sc_sf_1 = cap->c_un.c_val;
2274*08278a5eSRod Evans 				break;
2275*08278a5eSRod Evans 			case CA_SUNW_HW_2:
2276*08278a5eSRod Evans 				CAPSET(lmp).sc_hw_2 = cap->c_un.c_val;
2277*08278a5eSRod Evans 				break;
2278*08278a5eSRod Evans 			case CA_SUNW_PLAT:
2279*08278a5eSRod Evans 				CAPSET(lmp).sc_plat = STRTAB(lmp) +
2280*08278a5eSRod Evans 				    cap->c_un.c_ptr;
2281*08278a5eSRod Evans 				break;
2282*08278a5eSRod Evans 			case CA_SUNW_MACH:
2283*08278a5eSRod Evans 				CAPSET(lmp).sc_mach = STRTAB(lmp) +
2284*08278a5eSRod Evans 				    cap->c_un.c_ptr;
2285*08278a5eSRod Evans 				break;
2286*08278a5eSRod Evans 			}
2287*08278a5eSRod Evans 			cap++;
2288*08278a5eSRod Evans 		}
2289*08278a5eSRod Evans 	}
2290*08278a5eSRod Evans 
2291*08278a5eSRod Evans 	/*
2292*08278a5eSRod Evans 	 * If a capabilities chain table exists, duplicate it.  The chain table
2293*08278a5eSRod Evans 	 * is inspected for each initial call to a capabilities family lead
2294*08278a5eSRod Evans 	 * symbol.  From this chain, each family member is inspected to
2295*08278a5eSRod Evans 	 * determine the 'best' family member.  The chain table is then updated
2296*08278a5eSRod Evans 	 * so that the best member is immediately selected for any further
2297*08278a5eSRod Evans 	 * family searches.
2298*08278a5eSRod Evans 	 */
2299*08278a5eSRod Evans 	if (CAPCHAIN(lmp)) {
2300*08278a5eSRod Evans 		Capchain	*capchain;
2301*08278a5eSRod Evans 
2302*08278a5eSRod Evans 		if ((capchain = calloc(CAPCHAINSZ(lmp), 1)) == NULL)
2303*08278a5eSRod Evans 			return (NULL);
2304*08278a5eSRod Evans 		(void) memcpy(capchain, CAPCHAIN(lmp), CAPCHAINSZ(lmp));
2305*08278a5eSRod Evans 		CAPCHAIN(lmp) = capchain;
2306*08278a5eSRod Evans 	}
230756deab07SRod Evans 
23087c478bd9Sstevel@tonic-gate 	/*
23097c478bd9Sstevel@tonic-gate 	 * Add the mapped object to the end of the link map list.
23107c478bd9Sstevel@tonic-gate 	 */
23117c478bd9Sstevel@tonic-gate 	lm_append(lml, lmco, lmp);
231256deab07SRod Evans 
231356deab07SRod Evans 	/*
231456deab07SRod Evans 	 * Start the system loading in the ELF information we'll be processing.
231556deab07SRod Evans 	 */
231656deab07SRod Evans 	if (REL(lmp)) {
231756deab07SRod Evans 		(void) madvise((void *)ADDR(lmp), (uintptr_t)REL(lmp) +
231856deab07SRod Evans 		    (uintptr_t)RELSZ(lmp) - (uintptr_t)ADDR(lmp),
231956deab07SRod Evans 		    MADV_WILLNEED);
232056deab07SRod Evans 	}
23217c478bd9Sstevel@tonic-gate 	return (lmp);
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate /*
23257c478bd9Sstevel@tonic-gate  * Build full pathname of shared object from given directory name and filename.
23267c478bd9Sstevel@tonic-gate  */
23277c478bd9Sstevel@tonic-gate static char *
232856deab07SRod Evans elf_get_so(const char *dir, const char *file, size_t dlen, size_t flen)
23297c478bd9Sstevel@tonic-gate {
23307c478bd9Sstevel@tonic-gate 	static char	pname[PATH_MAX];
23317c478bd9Sstevel@tonic-gate 
233256deab07SRod Evans 	(void) strncpy(pname, dir, dlen);
233356deab07SRod Evans 	pname[dlen++] = '/';
233456deab07SRod Evans 	(void) strncpy(&pname[dlen], file, flen + 1);
23357c478bd9Sstevel@tonic-gate 	return (pname);
23367c478bd9Sstevel@tonic-gate }
23377c478bd9Sstevel@tonic-gate 
23387c478bd9Sstevel@tonic-gate /*
23397c478bd9Sstevel@tonic-gate  * The copy relocation is recorded in a copy structure which will be applied
23407c478bd9Sstevel@tonic-gate  * after all other relocations are carried out.  This provides for copying data
23417c478bd9Sstevel@tonic-gate  * that must be relocated itself (ie. pointers in shared objects).  This
23427c478bd9Sstevel@tonic-gate  * structure also provides a means of binding RTLD_GROUP dependencies to any
23437c478bd9Sstevel@tonic-gate  * copy relocations that have been taken from any group members.
23447c478bd9Sstevel@tonic-gate  *
23457c478bd9Sstevel@tonic-gate  * If the size of the .bss area available for the copy information is not the
23467c478bd9Sstevel@tonic-gate  * same as the source of the data inform the user if we're under ldd(1) control
23477c478bd9Sstevel@tonic-gate  * (this checking was only established in 5.3, so by only issuing an error via
23487c478bd9Sstevel@tonic-gate  * ldd(1) we maintain the standard set by previous releases).
23497c478bd9Sstevel@tonic-gate  */
23507c478bd9Sstevel@tonic-gate int
23517c478bd9Sstevel@tonic-gate elf_copy_reloc(char *name, Sym *rsym, Rt_map *rlmp, void *radd, Sym *dsym,
23527c478bd9Sstevel@tonic-gate     Rt_map *dlmp, const void *dadd)
23537c478bd9Sstevel@tonic-gate {
23547c478bd9Sstevel@tonic-gate 	Rel_copy	rc;
23557c478bd9Sstevel@tonic-gate 	Lm_list		*lml = LIST(rlmp);
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate 	rc.r_name = name;
23587c478bd9Sstevel@tonic-gate 	rc.r_rsym = rsym;		/* the new reference symbol and its */
23597c478bd9Sstevel@tonic-gate 	rc.r_rlmp = rlmp;		/*	associated link-map */
23607c478bd9Sstevel@tonic-gate 	rc.r_dlmp = dlmp;		/* the defining link-map */
23617c478bd9Sstevel@tonic-gate 	rc.r_dsym = dsym;		/* the original definition */
23627c478bd9Sstevel@tonic-gate 	rc.r_radd = radd;
23637c478bd9Sstevel@tonic-gate 	rc.r_dadd = dadd;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	if (rsym->st_size > dsym->st_size)
23667c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)dsym->st_size;
23677c478bd9Sstevel@tonic-gate 	else
23687c478bd9Sstevel@tonic-gate 		rc.r_size = (size_t)rsym->st_size;
23697c478bd9Sstevel@tonic-gate 
2370cce0e03bSab 	if (alist_append(&COPY_R(dlmp), &rc, sizeof (Rel_copy),
237156deab07SRod Evans 	    AL_CNT_COPYREL) == NULL) {
23727c478bd9Sstevel@tonic-gate 		if (!(lml->lm_flags & LML_FLG_TRC_WARN))
23737c478bd9Sstevel@tonic-gate 			return (0);
23747c478bd9Sstevel@tonic-gate 		else
23757c478bd9Sstevel@tonic-gate 			return (1);
23767c478bd9Sstevel@tonic-gate 	}
23777c478bd9Sstevel@tonic-gate 	if (!(FLAGS1(dlmp) & FL1_RT_COPYTOOK)) {
2378cce0e03bSab 		if (aplist_append(&COPY_S(rlmp), dlmp,
2379cce0e03bSab 		    AL_CNT_COPYREL) == NULL) {
23807c478bd9Sstevel@tonic-gate 			if (!(lml->lm_flags & LML_FLG_TRC_WARN))
23817c478bd9Sstevel@tonic-gate 				return (0);
23827c478bd9Sstevel@tonic-gate 			else
23837c478bd9Sstevel@tonic-gate 				return (1);
23847c478bd9Sstevel@tonic-gate 		}
23857c478bd9Sstevel@tonic-gate 		FLAGS1(dlmp) |= FL1_RT_COPYTOOK;
23867c478bd9Sstevel@tonic-gate 	}
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 	/*
23897c478bd9Sstevel@tonic-gate 	 * If we are tracing (ldd), warn the user if
23907c478bd9Sstevel@tonic-gate 	 *	1) the size from the reference symbol differs from the
23917c478bd9Sstevel@tonic-gate 	 *	   copy definition. We can only copy as much data as the
23927c478bd9Sstevel@tonic-gate 	 *	   reference (dynamic executables) entry allows.
23937c478bd9Sstevel@tonic-gate 	 *	2) the copy definition has STV_PROTECTED visibility.
23947c478bd9Sstevel@tonic-gate 	 */
23957c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
23967c478bd9Sstevel@tonic-gate 		if (rsym->st_size != dsym->st_size) {
23977c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_SIZDIF),
23985aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
23997c478bd9Sstevel@tonic-gate 			    NAME(rlmp), EC_XWORD(rsym->st_size),
24007c478bd9Sstevel@tonic-gate 			    NAME(dlmp), EC_XWORD(dsym->st_size));
24017c478bd9Sstevel@tonic-gate 			if (rsym->st_size > dsym->st_size)
24027c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_INSDATA),
24037c478bd9Sstevel@tonic-gate 				    NAME(dlmp));
24047c478bd9Sstevel@tonic-gate 			else
24057c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_CPY_DATRUNC),
24067c478bd9Sstevel@tonic-gate 				    NAME(rlmp));
24077c478bd9Sstevel@tonic-gate 		}
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 		if (ELF_ST_VISIBILITY(dsym->st_other) == STV_PROTECTED) {
24107c478bd9Sstevel@tonic-gate 			(void) printf(MSG_INTL(MSG_LDD_CPY_PROT),
24115aefb655Srie 			    _conv_reloc_type(M_R_COPY), demangle(name),
2412a953e2b1Srie 			    NAME(dlmp));
24137c478bd9Sstevel@tonic-gate 		}
24147c478bd9Sstevel@tonic-gate 	}
24157c478bd9Sstevel@tonic-gate 
24165aefb655Srie 	DBG_CALL(Dbg_reloc_apply_val(lml, ELF_DBG_RTLD, (Xword)radd,
24175aefb655Srie 	    (Xword)rc.r_size));
24187c478bd9Sstevel@tonic-gate 	return (1);
24197c478bd9Sstevel@tonic-gate }
24207c478bd9Sstevel@tonic-gate 
24217c478bd9Sstevel@tonic-gate /*
24227c478bd9Sstevel@tonic-gate  * Determine the symbol location of an address within a link-map.  Look for
24237c478bd9Sstevel@tonic-gate  * the nearest symbol (whose value is less than or equal to the required
24247c478bd9Sstevel@tonic-gate  * address).  This is the object specific part of dladdr().
24257c478bd9Sstevel@tonic-gate  */
24267c478bd9Sstevel@tonic-gate static void
24277c478bd9Sstevel@tonic-gate elf_dladdr(ulong_t addr, Rt_map *lmp, Dl_info *dlip, void **info, int flags)
24287c478bd9Sstevel@tonic-gate {
24297c478bd9Sstevel@tonic-gate 	ulong_t		ndx, cnt, base, _value;
24306221fe92Sab 	Sym		*sym, *_sym = NULL;
24317c478bd9Sstevel@tonic-gate 	const char	*str;
24325343e1b3Sab 	int		_flags;
2433d579eb63Sab 	uint_t		*dynaddr_ndx;
2434d579eb63Sab 	uint_t		dynaddr_n = 0;
2435d579eb63Sab 	ulong_t		value;
24367c478bd9Sstevel@tonic-gate 
24377c478bd9Sstevel@tonic-gate 	/*
24389039eeafSab 	 * If SUNWSYMTAB() is non-NULL, then it sees a special version of
24399039eeafSab 	 * the dynsym that starts with any local function symbols that exist in
24409039eeafSab 	 * the library and then moves to the data held in SYMTAB(). In this
24419039eeafSab 	 * case, SUNWSYMSZ tells us how long the symbol table is. The
24429039eeafSab 	 * availability of local function symbols will enhance the results
24439039eeafSab 	 * we can provide.
24449039eeafSab 	 *
2445d579eb63Sab 	 * If SUNWSYMTAB() is non-NULL, then there might also be a
2446d579eb63Sab 	 * SUNWSYMSORT() vector associated with it. SUNWSYMSORT() contains
2447d579eb63Sab 	 * an array of indices into SUNWSYMTAB, sorted by increasing
2448d579eb63Sab 	 * address. We can use this to do an O(log N) search instead of a
2449d579eb63Sab 	 * brute force search.
2450d579eb63Sab 	 *
24519039eeafSab 	 * If SUNWSYMTAB() is NULL, then SYMTAB() references a dynsym that
24529039eeafSab 	 * contains only global symbols. In that case, the length of
24539039eeafSab 	 * the symbol table comes from the nchain field of the related
24549039eeafSab 	 * symbol lookup hash table.
24557c478bd9Sstevel@tonic-gate 	 */
24567c478bd9Sstevel@tonic-gate 	str = STRTAB(lmp);
24579039eeafSab 	if (SUNWSYMSZ(lmp) == NULL) {
24589039eeafSab 		sym = SYMTAB(lmp);
24599039eeafSab 		/*
24609039eeafSab 		 * If we don't have a .hash table there are no symbols
24619039eeafSab 		 * to look at.
24629039eeafSab 		 */
2463481bba9eSRod Evans 		if (HASH(lmp) == NULL)
24649039eeafSab 			return;
24659039eeafSab 		cnt = HASH(lmp)[1];
24669039eeafSab 	} else {
24679039eeafSab 		sym = SUNWSYMTAB(lmp);
24689039eeafSab 		cnt = SUNWSYMSZ(lmp) / SYMENT(lmp);
2469d579eb63Sab 		dynaddr_ndx = SUNWSYMSORT(lmp);
2470d579eb63Sab 		if (dynaddr_ndx != NULL)
2471d579eb63Sab 			dynaddr_n = SUNWSYMSORTSZ(lmp) / SUNWSORTENT(lmp);
24729039eeafSab 	}
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 	if (FLAGS(lmp) & FLG_RT_FIXED)
24757c478bd9Sstevel@tonic-gate 		base = 0;
24767c478bd9Sstevel@tonic-gate 	else
24777c478bd9Sstevel@tonic-gate 		base = ADDR(lmp);
24787c478bd9Sstevel@tonic-gate 
2479d579eb63Sab 	if (dynaddr_n > 0) {		/* Binary search */
2480d579eb63Sab 		long	low = 0, low_bnd;
2481d579eb63Sab 		long	high = dynaddr_n - 1, high_bnd;
2482d579eb63Sab 		long	mid;
2483d579eb63Sab 		Sym	*mid_sym;
24847c478bd9Sstevel@tonic-gate 
24859039eeafSab 		/*
2486d579eb63Sab 		 * Note that SUNWSYMSORT only contains symbols types that
2487d579eb63Sab 		 * supply memory addresses, so there's no need to check and
2488d579eb63Sab 		 * filter out any other types.
24899039eeafSab 		 */
2490d579eb63Sab 		low_bnd = low;
2491d579eb63Sab 		high_bnd = high;
2492d579eb63Sab 		while (low <= high) {
2493d579eb63Sab 			mid = (low + high) / 2;
2494d579eb63Sab 			mid_sym = &sym[dynaddr_ndx[mid]];
2495d579eb63Sab 			value = mid_sym->st_value + base;
2496d579eb63Sab 			if (addr < value) {
2497d579eb63Sab 				if ((sym[dynaddr_ndx[high]].st_value + base) >=
2498d579eb63Sab 				    addr)
2499d579eb63Sab 					high_bnd = high;
2500d579eb63Sab 				high = mid - 1;
2501d579eb63Sab 			} else if (addr > value) {
2502d579eb63Sab 				if ((sym[dynaddr_ndx[low]].st_value + base) <=
2503d579eb63Sab 				    addr)
2504d579eb63Sab 					low_bnd = low;
2505d579eb63Sab 				low = mid + 1;
2506d579eb63Sab 			} else {
2507d579eb63Sab 				_sym = mid_sym;
2508d579eb63Sab 				_value = value;
2509d579eb63Sab 				break;
2510d579eb63Sab 			}
2511d579eb63Sab 		}
2512d579eb63Sab 		/*
2513d579eb63Sab 		 * If the above didn't find it exactly, then we must
2514d579eb63Sab 		 * return the closest symbol with a value that doesn't
2515d579eb63Sab 		 * exceed the one we are looking for. If that symbol exists,
2516d579eb63Sab 		 * it will lie in the range bounded by low_bnd and
2517d579eb63Sab 		 * high_bnd. This is a linear search, but a short one.
2518d579eb63Sab 		 */
2519d579eb63Sab 		if (_sym == NULL) {
2520d579eb63Sab 			for (mid = low_bnd; mid <= high_bnd; mid++) {
2521d579eb63Sab 				mid_sym = &sym[dynaddr_ndx[mid]];
2522d579eb63Sab 				value = mid_sym->st_value + base;
2523d579eb63Sab 				if (addr >= value) {
2524d579eb63Sab 					_sym = mid_sym;
2525d579eb63Sab 					_value = value;
2526d579eb63Sab 				} else {
2527d579eb63Sab 					break;
2528d579eb63Sab 				}
2529d579eb63Sab 			}
2530d579eb63Sab 		}
2531d579eb63Sab 	} else {			/* Linear search */
2532d579eb63Sab 		for (_value = 0, sym++, ndx = 1; ndx < cnt; ndx++, sym++) {
2533d579eb63Sab 			/*
2534d579eb63Sab 			 * Skip expected symbol types that are not functions
2535d579eb63Sab 			 * or data:
2536d579eb63Sab 			 *	- A symbol table starts with an undefined symbol
2537d579eb63Sab 			 *		in slot 0. If we are using SUNWSYMTAB(),
2538d579eb63Sab 			 *		there will be a second undefined symbol
2539d579eb63Sab 			 *		right before the globals.
2540d579eb63Sab 			 *	- The local part of SUNWSYMTAB() contains a
2541d579eb63Sab 			 *		series of function symbols. Each section
2542d579eb63Sab 			 *		starts with an initial STT_FILE symbol.
2543d579eb63Sab 			 */
2544d579eb63Sab 			if ((sym->st_shndx == SHN_UNDEF) ||
2545d579eb63Sab 			    (ELF_ST_TYPE(sym->st_info) == STT_FILE))
2546d579eb63Sab 				continue;
25477c478bd9Sstevel@tonic-gate 
2548d579eb63Sab 			value = sym->st_value + base;
2549d579eb63Sab 			if (value > addr)
2550d579eb63Sab 				continue;
2551d579eb63Sab 			if (value < _value)
2552d579eb63Sab 				continue;
25537c478bd9Sstevel@tonic-gate 
2554d579eb63Sab 			_sym = sym;
2555d579eb63Sab 			_value = value;
25567c478bd9Sstevel@tonic-gate 
2557d579eb63Sab 			/*
2558d579eb63Sab 			 * Note, because we accept local and global symbols
2559d579eb63Sab 			 * we could find a section symbol that matches the
2560d579eb63Sab 			 * associated address, which means that the symbol
2561d579eb63Sab 			 * name will be null.  In this case continue the
2562d579eb63Sab 			 * search in case we can find a global symbol of
2563d579eb63Sab 			 * the same value.
2564d579eb63Sab 			 */
2565d579eb63Sab 			if ((value == addr) &&
2566d579eb63Sab 			    (ELF_ST_TYPE(sym->st_info) != STT_SECTION))
2567d579eb63Sab 				break;
2568d579eb63Sab 		}
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 
25715343e1b3Sab 	_flags = flags & RTLD_DL_MASK;
25727c478bd9Sstevel@tonic-gate 	if (_sym) {
25737c478bd9Sstevel@tonic-gate 		if (_flags == RTLD_DL_SYMENT)
25747c478bd9Sstevel@tonic-gate 			*info = (void *)_sym;
25757c478bd9Sstevel@tonic-gate 		else if (_flags == RTLD_DL_LINKMAP)
25767c478bd9Sstevel@tonic-gate 			*info = (void *)lmp;
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 		dlip->dli_sname = str + _sym->st_name;
25797c478bd9Sstevel@tonic-gate 		dlip->dli_saddr = (void *)_value;
25805343e1b3Sab 	} else {
25815343e1b3Sab 		/*
25825343e1b3Sab 		 * addr lies between the beginning of the mapped segment and
25835343e1b3Sab 		 * the first global symbol. We have no symbol to return
25845343e1b3Sab 		 * and the caller requires one. We use _START_, the base
25855343e1b3Sab 		 * address of the mapping.
25865343e1b3Sab 		 */
25875343e1b3Sab 
25885343e1b3Sab 		if (_flags == RTLD_DL_SYMENT) {
25895343e1b3Sab 			/*
25905343e1b3Sab 			 * An actual symbol struct is needed, so we
25915343e1b3Sab 			 * construct one for _START_. To do this in a
25925343e1b3Sab 			 * fully accurate way requires a different symbol
25935343e1b3Sab 			 * for each mapped segment. This requires the
25945343e1b3Sab 			 * use of dynamic memory and a mutex. That's too much
25955343e1b3Sab 			 * plumbing for a fringe case of limited importance.
25965343e1b3Sab 			 *
25975343e1b3Sab 			 * Fortunately, we can simplify:
25985343e1b3Sab 			 *    - Only the st_size and st_info fields are useful
25995343e1b3Sab 			 *	outside of the linker internals. The others
26005343e1b3Sab 			 *	reference things that outside code cannot see,
26015343e1b3Sab 			 *	and can be set to 0.
26025343e1b3Sab 			 *    - It's just a label and there is no size
26035343e1b3Sab 			 *	to report. So, the size should be 0.
26045343e1b3Sab 			 * This means that only st_info needs a non-zero
26055343e1b3Sab 			 * (constant) value. A static struct will suffice.
26065343e1b3Sab 			 * It must be const (readonly) so the caller can't
26075343e1b3Sab 			 * change its meaning for subsequent callers.
26085343e1b3Sab 			 */
26095343e1b3Sab 			static const Sym fsym = { 0, 0, 0,
261037ffaf83SRod Evans 			    ELF_ST_INFO(STB_LOCAL, STT_OBJECT) };
26115343e1b3Sab 			*info = (void *) &fsym;
26125343e1b3Sab 		}
26135343e1b3Sab 
26145343e1b3Sab 		dlip->dli_sname = MSG_ORIG(MSG_SYM_START);
26155343e1b3Sab 		dlip->dli_saddr = (void *) ADDR(lmp);
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate }
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate /*
262075e7992aSrie  * This routine is called as a last fall-back to search for a symbol from a
26212017c965SRod Evans  * standard relocation or dlsym().  To maintain lazy loadings goal of reducing
26222017c965SRod Evans  * the number of objects mapped, any symbol search is first carried out using
26232017c965SRod Evans  * the objects that already exist in the process (either on a link-map list or
26242017c965SRod Evans  * handle).  If a symbol can't be found, and lazy dependencies are still
26252017c965SRod Evans  * pending, this routine loads the dependencies in an attempt to locate the
26262017c965SRod Evans  * symbol.
26277c478bd9Sstevel@tonic-gate  */
2628*08278a5eSRod Evans int
2629*08278a5eSRod Evans elf_lazy_find_sym(Slookup *slp, Sresult *srp, uint_t *binfo, int *in_nfavl)
26307c478bd9Sstevel@tonic-gate {
26312017c965SRod Evans 	static APlist	*alist = NULL;
26322017c965SRod Evans 	Aliste		idx1;
26332017c965SRod Evans 	Rt_map		*lmp1, *lmp = slp->sl_imap, *clmp = slp->sl_cmap;
26347c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
26352017c965SRod Evans 	Slookup		sl1 = *slp;
26362017c965SRod Evans 	Lm_list		*lml;
26372017c965SRod Evans 	Lm_cntl		*lmc;
26382017c965SRod Evans 
26392017c965SRod Evans 	/*
26402017c965SRod Evans 	 * It's quite possible we've been here before to process objects,
26412017c965SRod Evans 	 * therefore reinitialize our dynamic list.
26422017c965SRod Evans 	 */
26432017c965SRod Evans 	if (alist)
26442017c965SRod Evans 		aplist_reset(alist);
26452017c965SRod Evans 
26462017c965SRod Evans 	/*
26472017c965SRod Evans 	 * Discard any relocation index from further symbol searches.  This
26482017c965SRod Evans 	 * index has already been used to trigger any necessary lazy-loads,
26492017c965SRod Evans 	 * and it might be because one of these lazy loads has failed that
26502017c965SRod Evans 	 * we're performing this fallback.  By removing the relocation index
26512017c965SRod Evans 	 * we don't try and perform the same failed lazy loading activity again.
26522017c965SRod Evans 	 */
26532017c965SRod Evans 	sl1.sl_rsymndx = 0;
26542017c965SRod Evans 
26552017c965SRod Evans 	/*
26562017c965SRod Evans 	 * Determine the callers link-map list so that we can monitor whether
26572017c965SRod Evans 	 * new objects have been added.
26582017c965SRod Evans 	 */
26592017c965SRod Evans 	lml = LIST(clmp);
26602017c965SRod Evans 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(clmp));
26617c478bd9Sstevel@tonic-gate 
266275e7992aSrie 	/*
266375e7992aSrie 	 * Generate a local list of new objects to process.  This list can grow
266475e7992aSrie 	 * as each object supplies its own lazy dependencies.
266575e7992aSrie 	 */
2666cce0e03bSab 	if (aplist_append(&alist, lmp, AL_CNT_LAZYFIND) == NULL)
2667cce0e03bSab 		return (NULL);
26687c478bd9Sstevel@tonic-gate 
26692017c965SRod Evans 	for (APLIST_TRAVERSE(alist, idx1, lmp1)) {
26707c478bd9Sstevel@tonic-gate 		uint_t	cnt = 0;
267175e7992aSrie 		Dyninfo	*dip, *pdip;
26727c478bd9Sstevel@tonic-gate 
267375e7992aSrie 		/*
267475e7992aSrie 		 * Loop through the lazy DT_NEEDED entries examining each object
267575e7992aSrie 		 * for the required symbol.  If the symbol is not found, the
267675e7992aSrie 		 * object is in turn added to the local alist, so that the
267775e7992aSrie 		 * objects lazy DT_NEEDED entries can be examined.
26787c478bd9Sstevel@tonic-gate 		 */
2679cce0e03bSab 		lmp = lmp1;
268075e7992aSrie 		for (dip = DYNINFO(lmp), pdip = NULL; cnt < DYNINFOCNT(lmp);
268175e7992aSrie 		    cnt++, pdip = dip++) {
26822017c965SRod Evans 			Grp_hdl		*ghp;
26832017c965SRod Evans 			Grp_desc	*gdp;
26842017c965SRod Evans 			Rt_map		*nlmp, *llmp;
26852017c965SRod Evans 			Slookup		sl2;
2686*08278a5eSRod Evans 			Sresult		sr;
26872017c965SRod Evans 			Aliste		idx2;
26887c478bd9Sstevel@tonic-gate 
268975e7992aSrie 			if (((dip->di_flags & FLG_DI_LAZY) == 0) ||
26907c478bd9Sstevel@tonic-gate 			    dip->di_info)
26917c478bd9Sstevel@tonic-gate 				continue;
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate 			/*
269475e7992aSrie 			 * If this object has already failed to lazy load, and
269575e7992aSrie 			 * we're still processing the same runtime linker
269675e7992aSrie 			 * operation that produced the failure, don't bother
269775e7992aSrie 			 * to try and load the object again.
269875e7992aSrie 			 */
269975e7992aSrie 			if ((dip->di_flags & FLG_DI_LAZYFAIL) && pdip &&
270075e7992aSrie 			    (pdip->di_flags & FLG_DI_POSFLAG1)) {
270175e7992aSrie 				if (pdip->di_info == (void *)ld_entry_cnt)
270275e7992aSrie 					continue;
270375e7992aSrie 
270475e7992aSrie 				dip->di_flags &= ~FLG_DI_LAZYFAIL;
270575e7992aSrie 				pdip->di_info = NULL;
270675e7992aSrie 			}
270775e7992aSrie 
27082017c965SRod Evans 			/*
27092017c965SRod Evans 			 * Determine the last link-map presently on the callers
27102017c965SRod Evans 			 * link-map control list.
27112017c965SRod Evans 			 */
27122017c965SRod Evans 			llmp = lmc->lc_tail;
27132017c965SRod Evans 
271475e7992aSrie 			/*
271575e7992aSrie 			 * Try loading this lazy dependency.  If the object
271675e7992aSrie 			 * can't be loaded, consider this non-fatal and continue
271775e7992aSrie 			 * the search.  Lazy loaded dependencies need not exist
271875e7992aSrie 			 * and their loading should only turn out to be fatal
271975e7992aSrie 			 * if they are required to satisfy a relocation.
27207c478bd9Sstevel@tonic-gate 			 *
27212017c965SRod Evans 			 * A successful lazy load can mean one of two things:
27222017c965SRod Evans 			 *
27232017c965SRod Evans 			 *  -	new objects have been loaded, in which case the
27242017c965SRod Evans 			 * 	objects will have been analyzed, relocated, and
27252017c965SRod Evans 			 * 	finally moved to the callers control list.
27262017c965SRod Evans 			 *  -	the objects are already loaded, and this lazy
27272017c965SRod Evans 			 *	load has simply associated the referenced object
27282017c965SRod Evans 			 *	with it's lazy dependencies.
27292017c965SRod Evans 			 *
27302017c965SRod Evans 			 * If new objects are loaded, look in these objects
27312017c965SRod Evans 			 * first.  Note, a new object can be the object being
27322017c965SRod Evans 			 * referenced by this lazy load, however we can also
27332017c965SRod Evans 			 * descend into multiple lazy loads as we relocate this
27342017c965SRod Evans 			 * reference.
27352017c965SRod Evans 			 *
27362017c965SRod Evans 			 * If the symbol hasn't been found, use the referenced
27372017c965SRod Evans 			 * objects handle, as it might have dependencies on
2738e0e63816SRod Evans 			 * objects that are already loaded.  Note that existing
2739e0e63816SRod Evans 			 * objects might have already been searched and skipped
2740e0e63816SRod Evans 			 * as non-available to this caller.   However, a lazy
2741e0e63816SRod Evans 			 * load might have caused the promotion of modes, or
2742e0e63816SRod Evans 			 * added this object to the family of the caller.  In
2743e0e63816SRod Evans 			 * either case, the handle associated with the object
2744e0e63816SRod Evans 			 * is then used to carry out the symbol search.
27457c478bd9Sstevel@tonic-gate 			 */
27462017c965SRod Evans 			if ((nlmp = elf_lazy_load(lmp, &sl1, cnt, name,
27472017c965SRod Evans 			    FLG_RT_PRIHDL, &ghp, in_nfavl)) == NULL)
27487c478bd9Sstevel@tonic-gate 				continue;
27497c478bd9Sstevel@tonic-gate 
27502017c965SRod Evans 			if (NEXT_RT_MAP(llmp)) {
27512017c965SRod Evans 				/*
27522017c965SRod Evans 				 * Look in any new objects.
27532017c965SRod Evans 				 */
27542017c965SRod Evans 				sl1.sl_imap = NEXT_RT_MAP(llmp);
27552017c965SRod Evans 				sl1.sl_flags &= ~LKUP_STDRELOC;
27562017c965SRod Evans 
2757*08278a5eSRod Evans 				/*
2758*08278a5eSRod Evans 				 * Initialize a local symbol result descriptor,
2759*08278a5eSRod Evans 				 * using the original symbol name.
2760*08278a5eSRod Evans 				 */
2761*08278a5eSRod Evans 				SRESULT_INIT(sr, slp->sl_name);
2762*08278a5eSRod Evans 
2763*08278a5eSRod Evans 				if (lookup_sym(&sl1, &sr, binfo, in_nfavl)) {
2764*08278a5eSRod Evans 					*srp = sr;
2765*08278a5eSRod Evans 					return (1);
2766*08278a5eSRod Evans 				}
27672017c965SRod Evans 			}
27682017c965SRod Evans 
27697c478bd9Sstevel@tonic-gate 			/*
2770e0e63816SRod Evans 			 * Use the objects handle to inspect the family of
2771e0e63816SRod Evans 			 * objects associated with the handle.  Note, there's
27722017c965SRod Evans 			 * a possibility of overlap with the above search,
27732017c965SRod Evans 			 * should a lazy load bring in new objects and
27742017c965SRod Evans 			 * reference existing objects.
27757c478bd9Sstevel@tonic-gate 			 */
27762017c965SRod Evans 			sl2 = sl1;
27772017c965SRod Evans 			for (ALIST_TRAVERSE(ghp->gh_depends, idx2, gdp)) {
2778e0e63816SRod Evans 				if ((gdp->gd_depend != NEXT_RT_MAP(llmp)) &&
27792017c965SRod Evans 				    (gdp->gd_flags & GPD_DLSYM)) {
27802017c965SRod Evans 
27812017c965SRod Evans 					sl2.sl_imap = gdp->gd_depend;
27822017c965SRod Evans 					sl2.sl_flags |= LKUP_FIRST;
27832017c965SRod Evans 
2784*08278a5eSRod Evans 					/*
2785*08278a5eSRod Evans 					 * Initialize a local symbol result
2786*08278a5eSRod Evans 					 * descriptor, using the original
2787*08278a5eSRod Evans 					 * symbol name.
2788*08278a5eSRod Evans 					 */
2789*08278a5eSRod Evans 					SRESULT_INIT(sr, slp->sl_name);
2790*08278a5eSRod Evans 
2791*08278a5eSRod Evans 					if (lookup_sym(&sl2, &sr, binfo,
2792*08278a5eSRod Evans 					    in_nfavl)) {
2793*08278a5eSRod Evans 						*srp = sr;
2794*08278a5eSRod Evans 						return (1);
2795*08278a5eSRod Evans 					}
27962017c965SRod Evans 				}
27972017c965SRod Evans 			}
27982017c965SRod Evans 
27997c478bd9Sstevel@tonic-gate 			/*
28007c478bd9Sstevel@tonic-gate 			 * Some dlsym() operations are already traversing a
28017c478bd9Sstevel@tonic-gate 			 * link-map (dlopen(0)), and thus there's no need to
28022017c965SRod Evans 			 * save them on the dynamic dependency list.
28037c478bd9Sstevel@tonic-gate 			 */
28042017c965SRod Evans 			if (slp->sl_flags & LKUP_NODESCENT)
28052017c965SRod Evans 				continue;
28062017c965SRod Evans 
28072017c965SRod Evans 			if (aplist_test(&alist, nlmp, AL_CNT_LAZYFIND) == NULL)
2808*08278a5eSRod Evans 				return (0);
28097c478bd9Sstevel@tonic-gate 		}
28107c478bd9Sstevel@tonic-gate 	}
28117c478bd9Sstevel@tonic-gate 
2812*08278a5eSRod Evans 	return (0);
28137c478bd9Sstevel@tonic-gate }
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate /*
28167c478bd9Sstevel@tonic-gate  * Warning message for bad r_offset.
28177c478bd9Sstevel@tonic-gate  */
28187c478bd9Sstevel@tonic-gate void
28197c478bd9Sstevel@tonic-gate elf_reloc_bad(Rt_map *lmp, void *rel, uchar_t rtype, ulong_t roffset,
28207c478bd9Sstevel@tonic-gate     ulong_t rsymndx)
28217c478bd9Sstevel@tonic-gate {
282237ffaf83SRod Evans 	const char	*name = NULL;
28235aefb655Srie 	Lm_list		*lml = LIST(lmp);
28247c478bd9Sstevel@tonic-gate 	int		trace;
28257c478bd9Sstevel@tonic-gate 
28265aefb655Srie 	if ((lml->lm_flags & LML_FLG_TRC_ENABLE) &&
28277c478bd9Sstevel@tonic-gate 	    (((rtld_flags & RT_FL_SILENCERR) == 0) ||
28285aefb655Srie 	    (lml->lm_flags & LML_FLG_TRC_VERBOSE)))
28297c478bd9Sstevel@tonic-gate 		trace = 1;
28307c478bd9Sstevel@tonic-gate 	else
28317c478bd9Sstevel@tonic-gate 		trace = 0;
28327c478bd9Sstevel@tonic-gate 
28335aefb655Srie 	if ((trace == 0) && (DBG_ENABLED == 0))
28347c478bd9Sstevel@tonic-gate 		return;
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate 	if (rsymndx) {
28377c478bd9Sstevel@tonic-gate 		Sym	*symref = (Sym *)((ulong_t)SYMTAB(lmp) +
2838a953e2b1Srie 		    (rsymndx * SYMENT(lmp)));
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 		if (ELF_ST_BIND(symref->st_info) != STB_LOCAL)
28417c478bd9Sstevel@tonic-gate 			name = (char *)(STRTAB(lmp) + symref->st_name);
28427c478bd9Sstevel@tonic-gate 	}
28437c478bd9Sstevel@tonic-gate 
2844481bba9eSRod Evans 	if (name == NULL)
284556deab07SRod Evans 		name = MSG_INTL(MSG_STR_UNKNOWN);
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	if (trace) {
28487c478bd9Sstevel@tonic-gate 		const char *rstr;
28497c478bd9Sstevel@tonic-gate 
28505aefb655Srie 		rstr = _conv_reloc_type((uint_t)rtype);
28517c478bd9Sstevel@tonic-gate 		(void) printf(MSG_INTL(MSG_LDD_REL_ERR1), rstr, name,
28527c478bd9Sstevel@tonic-gate 		    EC_ADDR(roffset));
28537c478bd9Sstevel@tonic-gate 		return;
28547c478bd9Sstevel@tonic-gate 	}
28557c478bd9Sstevel@tonic-gate 
28565aefb655Srie 	Dbg_reloc_error(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel, name);
28577c478bd9Sstevel@tonic-gate }
2858d326b23bSrie 
2859d326b23bSrie /*
2860d326b23bSrie  * Resolve a static TLS relocation.
2861d326b23bSrie  */
2862d326b23bSrie long
2863d326b23bSrie elf_static_tls(Rt_map *lmp, Sym *sym, void *rel, uchar_t rtype, char *name,
2864d326b23bSrie     ulong_t roffset, long value)
2865d326b23bSrie {
2866d326b23bSrie 	Lm_list	*lml = LIST(lmp);
2867d326b23bSrie 
2868d326b23bSrie 	/*
2869d326b23bSrie 	 * Relocations against a static TLS block have limited support once
2870d326b23bSrie 	 * process initialization has completed.  Any error condition should be
2871d326b23bSrie 	 * discovered by testing for DF_STATIC_TLS as part of loading an object,
2872d326b23bSrie 	 * however individual relocations are tested in case the dynamic flag
2873d326b23bSrie 	 * had not been set when this object was built.
2874d326b23bSrie 	 */
2875481bba9eSRod Evans 	if (PTTLS(lmp) == NULL) {
2876d326b23bSrie 		DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
2877e23c41c9SAli Bahrami 		    M_REL_SHT_TYPE, rel, NULL, 0, name));
2878d326b23bSrie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
2879d326b23bSrie 		    _conv_reloc_type((uint_t)rtype), NAME(lmp),
2880d326b23bSrie 		    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
2881d326b23bSrie 		return (0);
2882d326b23bSrie 	}
2883d326b23bSrie 
2884d326b23bSrie 	/*
2885d326b23bSrie 	 * If no static TLS has been set aside for this object, determine if
2886d326b23bSrie 	 * any can be obtained.  Enforce that any object using static TLS is
2887d326b23bSrie 	 * non-deletable.
2888d326b23bSrie 	 */
2889d326b23bSrie 	if (TLSSTATOFF(lmp) == 0) {
2890d326b23bSrie 		FLAGS1(lmp) |= FL1_RT_TLSSTAT;
2891d326b23bSrie 		MODE(lmp) |= RTLD_NODELETE;
2892d326b23bSrie 
2893d326b23bSrie 		if (tls_assign(lml, lmp, PTTLS(lmp)) == 0) {
2894d326b23bSrie 			DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH,
2895e23c41c9SAli Bahrami 			    M_REL_SHT_TYPE, rel, NULL, 0, name));
2896d326b23bSrie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_BADTLS),
2897d326b23bSrie 			    _conv_reloc_type((uint_t)rtype), NAME(lmp),
2898d326b23bSrie 			    name ? demangle(name) : MSG_INTL(MSG_STR_UNKNOWN));
2899d326b23bSrie 			return (0);
2900d326b23bSrie 		}
2901d326b23bSrie 	}
2902d326b23bSrie 
2903d326b23bSrie 	/*
2904d326b23bSrie 	 * Typically, a static TLS offset is maintained as a symbols value.
2905d326b23bSrie 	 * For local symbols that are not apart of the dynamic symbol table,
2906d326b23bSrie 	 * the TLS relocation points to a section symbol, and the static TLS
2907d326b23bSrie 	 * offset was deposited in the associated GOT table.  Make sure the GOT
2908d326b23bSrie 	 * is cleared, so that the value isn't reused in do_reloc().
2909d326b23bSrie 	 */
2910d326b23bSrie 	if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
2911d326b23bSrie 		if ((ELF_ST_TYPE(sym->st_info) == STT_SECTION)) {
2912d326b23bSrie 			value = *(long *)roffset;
2913d326b23bSrie 			*(long *)roffset = 0;
2914d326b23bSrie 		} else {
2915d326b23bSrie 			value = sym->st_value;
2916d326b23bSrie 		}
2917d326b23bSrie 	}
2918d326b23bSrie 	return (-(TLSSTATOFF(lmp) - value));
2919d326b23bSrie }
2920dae2dfb7Srie 
2921dae2dfb7Srie /*
2922dae2dfb7Srie  * If the symbol is not found and the reference was not to a weak symbol, report
2923dae2dfb7Srie  * an error.  Weak references may be unresolved.
2924dae2dfb7Srie  */
2925dae2dfb7Srie int
2926dae2dfb7Srie elf_reloc_error(Rt_map *lmp, const char *name, void *rel, uint_t binfo)
2927dae2dfb7Srie {
2928dae2dfb7Srie 	Lm_list	*lml = LIST(lmp);
2929dae2dfb7Srie 
2930dae2dfb7Srie 	/*
2931dae2dfb7Srie 	 * Under crle(1), relocation failures are ignored.
2932dae2dfb7Srie 	 */
2933dae2dfb7Srie 	if (lml->lm_flags & LML_FLG_IGNRELERR)
2934dae2dfb7Srie 		return (1);
2935dae2dfb7Srie 
2936dae2dfb7Srie 	/*
2937dae2dfb7Srie 	 * Under ldd(1), unresolved references are reported.  However, if the
2938dae2dfb7Srie 	 * original reference is EXTERN or PARENT these references are ignored
2939dae2dfb7Srie 	 * unless ldd's -p option is in effect.
2940dae2dfb7Srie 	 */
2941dae2dfb7Srie 	if (lml->lm_flags & LML_FLG_TRC_WARN) {
2942dae2dfb7Srie 		if (((binfo & DBG_BINFO_REF_MSK) == 0) ||
2943dae2dfb7Srie 		    ((lml->lm_flags & LML_FLG_TRC_NOPAREXT) != 0)) {
2944dae2dfb7Srie 			(void) printf(MSG_INTL(MSG_LDD_SYM_NFOUND),
2945dae2dfb7Srie 			    demangle(name), NAME(lmp));
2946dae2dfb7Srie 		}
2947dae2dfb7Srie 		return (1);
2948dae2dfb7Srie 	}
2949dae2dfb7Srie 
2950dae2dfb7Srie 	/*
2951dae2dfb7Srie 	 * Otherwise, the unresolved references is fatal.
2952dae2dfb7Srie 	 */
2953dae2dfb7Srie 	DBG_CALL(Dbg_reloc_in(lml, ELF_DBG_RTLD, M_MACH, M_REL_SHT_TYPE, rel,
2954e23c41c9SAli Bahrami 	    NULL, 0, name));
2955dae2dfb7Srie 	eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp),
2956dae2dfb7Srie 	    demangle(name));
2957dae2dfb7Srie 
2958dae2dfb7Srie 	return (0);
2959dae2dfb7Srie }
296056deab07SRod Evans 
296156deab07SRod Evans /*
296256deab07SRod Evans  * Generic relative relocation function.
296356deab07SRod Evans  */
296456deab07SRod Evans inline static ulong_t
296556deab07SRod Evans _elf_reloc_relative(ulong_t rbgn, ulong_t base, Rt_map *lmp, APlist **textrel)
296656deab07SRod Evans {
296756deab07SRod Evans 	mmapobj_result_t	*mpp;
296856deab07SRod Evans 	ulong_t			roffset;
296956deab07SRod Evans 
297056deab07SRod Evans 	roffset = ((M_RELOC *)rbgn)->r_offset;
297156deab07SRod Evans 	roffset += base;
297256deab07SRod Evans 
297356deab07SRod Evans 	/*
297456deab07SRod Evans 	 * If this relocation is against an address that is not associated with
297556deab07SRod Evans 	 * a mapped segment, fall back to the generic relocation loop to
297656deab07SRod Evans 	 * collect the associated error.
297756deab07SRod Evans 	 */
297856deab07SRod Evans 	if ((mpp = find_segment((caddr_t)roffset, lmp)) == NULL)
297956deab07SRod Evans 		return (0);
298056deab07SRod Evans 
298156deab07SRod Evans 	/*
298256deab07SRod Evans 	 * If this relocation is against a segment that does not provide write
298356deab07SRod Evans 	 * access, set the write permission for all non-writable mappings.
298456deab07SRod Evans 	 */
298556deab07SRod Evans 	if (((mpp->mr_prot & PROT_WRITE) == 0) && textrel &&
298656deab07SRod Evans 	    ((set_prot(lmp, mpp, 1) == 0) ||
298756deab07SRod Evans 	    (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL)))
298856deab07SRod Evans 		return (0);
298956deab07SRod Evans 
299056deab07SRod Evans 	/*
299156deab07SRod Evans 	 * Perform the actual relocation.  Note, for backward compatibility,
299256deab07SRod Evans 	 * SPARC relocations are added to the offset contents (there was a time
299356deab07SRod Evans 	 * when the offset was used to contain the addend, rather than using
299456deab07SRod Evans 	 * the addend itself).
299556deab07SRod Evans 	 */
299656deab07SRod Evans #if	defined(__sparc)
299756deab07SRod Evans 	*((ulong_t *)roffset) += base + ((M_RELOC *)rbgn)->r_addend;
299856deab07SRod Evans #elif	defined(__amd64)
299956deab07SRod Evans 	*((ulong_t *)roffset) = base + ((M_RELOC *)rbgn)->r_addend;
300056deab07SRod Evans #else
300156deab07SRod Evans 	*((ulong_t *)roffset) += base;
300256deab07SRod Evans #endif
300356deab07SRod Evans 	return (1);
300456deab07SRod Evans }
300556deab07SRod Evans 
300656deab07SRod Evans /*
300756deab07SRod Evans  * When a generic relocation loop realizes that it's dealing with relative
300856deab07SRod Evans  * relocations, but no DT_RELCOUNT .dynamic tag is present, this tighter loop
300956deab07SRod Evans  * is entered as an optimization.
301056deab07SRod Evans  */
301156deab07SRod Evans ulong_t
301256deab07SRod Evans elf_reloc_relative(ulong_t rbgn, ulong_t rend, ulong_t rsize, ulong_t base,
301356deab07SRod Evans     Rt_map *lmp, APlist **textrel)
301456deab07SRod Evans {
301556deab07SRod Evans 	char	rtype;
301656deab07SRod Evans 
301756deab07SRod Evans 	do {
301856deab07SRod Evans 		if (_elf_reloc_relative(rbgn, base, lmp, textrel) == 0)
301956deab07SRod Evans 			break;
302056deab07SRod Evans 
302156deab07SRod Evans 		rbgn += rsize;
302256deab07SRod Evans 		if (rbgn >= rend)
302356deab07SRod Evans 			break;
302456deab07SRod Evans 
302556deab07SRod Evans 		/*
302656deab07SRod Evans 		 * Make sure the next type is a relative relocation.
302756deab07SRod Evans 		 */
302856deab07SRod Evans 		rtype = ELF_R_TYPE(((M_RELOC *)rbgn)->r_info, M_MACH);
302956deab07SRod Evans 
303056deab07SRod Evans 	} while (rtype == M_R_RELATIVE);
303156deab07SRod Evans 
303256deab07SRod Evans 	return (rbgn);
303356deab07SRod Evans }
303456deab07SRod Evans 
303556deab07SRod Evans /*
303656deab07SRod Evans  * This is the tightest loop for RELATIVE relocations for those objects built
303756deab07SRod Evans  * with the DT_RELACOUNT .dynamic entry.
303856deab07SRod Evans  */
303956deab07SRod Evans ulong_t
304056deab07SRod Evans elf_reloc_relative_count(ulong_t rbgn, ulong_t rcount, ulong_t rsize,
304156deab07SRod Evans     ulong_t base, Rt_map *lmp, APlist **textrel)
304256deab07SRod Evans {
304356deab07SRod Evans 	for (; rcount; rcount--) {
304456deab07SRod Evans 		if (_elf_reloc_relative(rbgn, base, lmp, textrel) == 0)
304556deab07SRod Evans 			break;
304656deab07SRod Evans 
304756deab07SRod Evans 		rbgn += rsize;
304856deab07SRod Evans 	}
304956deab07SRod Evans 	return (rbgn);
305056deab07SRod Evans }
3051