xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/dlfcns.c (revision 7eca354d)
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  */
215aefb655Srie 
227257d1b4Sraf /*
237257d1b4Sraf  *	Copyright (c) 1988 AT&T
247257d1b4Sraf  *	  All Rights Reserved
25f441771bSRod Evans  *
26f441771bSRod Evans  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
2738f4bdddSBryan Cantrill  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
287257d1b4Sraf  */
297257d1b4Sraf 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Programmatic interface to the run_time linker.
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
345aefb655Srie #include	<sys/debug.h>
35a194faf8Srie #include	<stdio.h>
367c478bd9Sstevel@tonic-gate #include	<string.h>
377c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
387c478bd9Sstevel@tonic-gate #include	<synch.h>
395aefb655Srie #include	<limits.h>
405aefb655Srie #include	<debug.h>
4198c080d5SRod Evans #include	<conv.h>
427c478bd9Sstevel@tonic-gate #include	"_rtld.h"
437c478bd9Sstevel@tonic-gate #include	"_audit.h"
447c478bd9Sstevel@tonic-gate #include	"_elf.h"
45f441771bSRod Evans #include	"_inline_gen.h"
467c478bd9Sstevel@tonic-gate #include	"msg.h"
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * Determine who called us - given a pc determine in which object it resides.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * For dlopen() the link map of the caller must be passed to load_so() so that
527c478bd9Sstevel@tonic-gate  * the appropriate search rules (4.x or 5.0) are used to locate any
537c478bd9Sstevel@tonic-gate  * dependencies.  Also, if we've been called from a 4.x module it may be
547c478bd9Sstevel@tonic-gate  * necessary to fix the specified pathname so that it conforms with the 5.0 elf
557c478bd9Sstevel@tonic-gate  * rules.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  * For dlsym() the link map of the caller is used to determine RTLD_NEXT
587c478bd9Sstevel@tonic-gate  * requests, together with requests based off of a dlopen(0).
597c478bd9Sstevel@tonic-gate  * For dladdr() this routines provides a generic means of scanning all loaded
607c478bd9Sstevel@tonic-gate  * segments.
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate Rt_map *
_caller(caddr_t cpc,int flags)637c478bd9Sstevel@tonic-gate _caller(caddr_t cpc, int flags)
647c478bd9Sstevel@tonic-gate {
6557ef7aa9SRod Evans 	Lm_list	*lml;
6657ef7aa9SRod Evans 	Aliste	idx1;
677c478bd9Sstevel@tonic-gate 
6857ef7aa9SRod Evans 	for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) {
6957ef7aa9SRod Evans 		Aliste	idx2;
707c478bd9Sstevel@tonic-gate 		Lm_cntl	*lmc;
717c478bd9Sstevel@tonic-gate 
7257ef7aa9SRod Evans 		for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) {
737c478bd9Sstevel@tonic-gate 			Rt_map	*lmp;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 			for (lmp = lmc->lc_head; lmp;
76cb511613SAli Bahrami 			    lmp = NEXT_RT_MAP(lmp)) {
7756deab07SRod Evans 
7856deab07SRod Evans 				if (find_segment(cpc, lmp))
7956deab07SRod Evans 					return (lmp);
807c478bd9Sstevel@tonic-gate 			}
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * No mapping can be determined.  If asked for a default, assume this
867c478bd9Sstevel@tonic-gate 	 * is from the executable.
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	if (flags & CL_EXECDEF)
897c478bd9Sstevel@tonic-gate 		return ((Rt_map *)lml_main.lm_head);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	return (0);
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947257d1b4Sraf #pragma weak _dlerror = dlerror
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * External entry for dlerror(3dl).  Returns a pointer to the string describing
987c478bd9Sstevel@tonic-gate  * the last occurring error.  The last occurring error is cleared.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate char *
dlerror()1017257d1b4Sraf dlerror()
1027c478bd9Sstevel@tonic-gate {
1035aefb655Srie 	char	*error;
1045aefb655Srie 	Rt_map	*clmp;
1055aefb655Srie 	int	entry;
1067c478bd9Sstevel@tonic-gate 
1078cd45542Sraf 	entry = enter(0);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
1107c478bd9Sstevel@tonic-gate 
11198c080d5SRod Evans 	DBG_CALL(Dbg_dl_dlerror(clmp, lasterr));
11298c080d5SRod Evans 
1137c478bd9Sstevel@tonic-gate 	error = lasterr;
11456deab07SRod Evans 	lasterr = NULL;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if (entry)
1178cd45542Sraf 		leave(LIST(clmp), 0);
1187c478bd9Sstevel@tonic-gate 	return (error);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Add a dependency as a group descriptor to a group handle.  Returns 0 on
1232017c965SRod Evans  * failure.  On success, returns the group descriptor, and if alep is non-NULL
1242017c965SRod Evans  * the *alep is set to ALE_EXISTS if the dependency already exists, or to
1252017c965SRod Evans  * ALE_CREATE if the dependency is newly created.
1267c478bd9Sstevel@tonic-gate  */
1272017c965SRod Evans Grp_desc *
hdl_add(Grp_hdl * ghp,Rt_map * lmp,uint_t dflags,int * alep)1282017c965SRod Evans hdl_add(Grp_hdl *ghp, Rt_map *lmp, uint_t dflags, int *alep)
1297c478bd9Sstevel@tonic-gate {
1305aefb655Srie 	Grp_desc	*gdp;
131cce0e03bSab 	Aliste		idx;
1322017c965SRod Evans 	int		ale = ALE_CREATE;
1338af2c5b9Srie 	uint_t		oflags;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/*
1367c478bd9Sstevel@tonic-gate 	 * Make sure this dependency hasn't already been recorded.
1377c478bd9Sstevel@tonic-gate 	 */
138cce0e03bSab 	for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1397c478bd9Sstevel@tonic-gate 		if (gdp->gd_depend == lmp) {
1402017c965SRod Evans 			ale = ALE_EXISTS;
1417c478bd9Sstevel@tonic-gate 			break;
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1452017c965SRod Evans 	if (ale == ALE_CREATE) {
1467c478bd9Sstevel@tonic-gate 		Grp_desc	gd;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		/*
1497c478bd9Sstevel@tonic-gate 		 * Create a new handle descriptor.
1507c478bd9Sstevel@tonic-gate 		 */
1517c478bd9Sstevel@tonic-gate 		gd.gd_depend = lmp;
1527c478bd9Sstevel@tonic-gate 		gd.gd_flags = 0;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		/*
1557c478bd9Sstevel@tonic-gate 		 * Indicate this object is a part of this handles group.
1567c478bd9Sstevel@tonic-gate 		 */
15756deab07SRod Evans 		if (aplist_append(&GROUPS(lmp), ghp, AL_CNT_GROUPS) == NULL)
1582017c965SRod Evans 			return (NULL);
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		/*
1617c478bd9Sstevel@tonic-gate 		 * Append the new dependency to this handle.
1627c478bd9Sstevel@tonic-gate 		 */
163cce0e03bSab 		if ((gdp = alist_append(&ghp->gh_depends, &gd,
16456deab07SRod Evans 		    sizeof (Grp_desc), AL_CNT_DEPENDS)) == NULL)
1652017c965SRod Evans 			return (NULL);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1688af2c5b9Srie 	oflags = gdp->gd_flags;
1692017c965SRod Evans 	gdp->gd_flags |= dflags;
1707c478bd9Sstevel@tonic-gate 
1718af2c5b9Srie 	if (DBG_ENABLED) {
172035eb791SToomas Soome 		if (ale == ALE_CREATE) {
1738af2c5b9Srie 			DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_ADD,
1748af2c5b9Srie 			    gdp->gd_flags));
175035eb791SToomas Soome 		} else if (gdp->gd_flags != oflags) {
1768af2c5b9Srie 			DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_UPDATE,
1778af2c5b9Srie 			    gdp->gd_flags));
178035eb791SToomas Soome 		}
1798af2c5b9Srie 	}
1802017c965SRod Evans 
1812017c965SRod Evans 	if (alep)
1822017c965SRod Evans 		*alep = ale;
1832017c965SRod Evans 	return (gdp);
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /*
1877c478bd9Sstevel@tonic-gate  * Create a handle.
1882017c965SRod Evans  *
1892017c965SRod Evans  *   rlmp -	represents the reference link-map for which the handle is being
1902017c965SRod Evans  *		created.
1912017c965SRod Evans  *   clmp -	represents the caller who is requesting the handle.
1922017c965SRod Evans  *   hflags -	provide group handle flags (GPH_*) that affect the use of the
1932017c965SRod Evans  *		handle, such as dlopen(0), or use or use of RTLD_FIRST.
1942017c965SRod Evans  *   rdflags -	provide group dependency flags for the reference link-map rlmp,
1952017c965SRod Evans  *		such as whether the dependency can be used for dlsym(), can be
1962017c965SRod Evans  *		relocated against, or whether this objects dependencies should
1972017c965SRod Evans  *		be processed.
1982017c965SRod Evans  *   cdflags -	provide group dependency flags for the caller.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate Grp_hdl *
hdl_create(Lm_list * lml,Rt_map * rlmp,Rt_map * clmp,uint_t hflags,uint_t rdflags,uint_t cdflags)2012017c965SRod Evans hdl_create(Lm_list *lml, Rt_map *rlmp, Rt_map *clmp, uint_t hflags,
2022017c965SRod Evans     uint_t rdflags, uint_t cdflags)
2037c478bd9Sstevel@tonic-gate {
2042017c965SRod Evans 	Grp_hdl	*ghp = NULL, *aghp;
205cce0e03bSab 	APlist	**alpp;
206cce0e03bSab 	Aliste	idx;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * For dlopen(0) the handle is maintained as part of the link-map list,
2102017c965SRod Evans 	 * otherwise the handle is associated with the reference link-map.
2117c478bd9Sstevel@tonic-gate 	 */
2128af2c5b9Srie 	if (hflags & GPH_ZERO)
2137c478bd9Sstevel@tonic-gate 		alpp = &(lml->lm_handle);
2147c478bd9Sstevel@tonic-gate 	else
2152017c965SRod Evans 		alpp = &(HANDLES(rlmp));
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2188af2c5b9Srie 	 * Objects can contain multiple handles depending on the handle flags
2198af2c5b9Srie 	 * supplied.  Most RTLD flags pertain to the object itself and the
2208af2c5b9Srie 	 * bindings that it can achieve.  Multiple handles for these flags
2218af2c5b9Srie 	 * don't make sense.  But if the flag determines how the handle might
2228af2c5b9Srie 	 * be used, then multiple handles may exist.  Presently this only makes
2238af2c5b9Srie 	 * sense for RTLD_FIRST.  Determine if an appropriate handle already
2248af2c5b9Srie 	 * exists.
2257c478bd9Sstevel@tonic-gate 	 */
2262017c965SRod Evans 	for (APLIST_TRAVERSE(*alpp, idx, aghp)) {
2272017c965SRod Evans 		if ((aghp->gh_flags & GPH_FIRST) == (hflags & GPH_FIRST)) {
2282017c965SRod Evans 			ghp = aghp;
2297c478bd9Sstevel@tonic-gate 			break;
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
233b4059b01SRod Evans 	if (ghp == NULL) {
23456deab07SRod Evans 		uint_t	ndx;
23556deab07SRod Evans 
2367c478bd9Sstevel@tonic-gate 		/*
2372017c965SRod Evans 		 * If this is the first request for this handle, allocate and
2382017c965SRod Evans 		 * initialize a new handle.
2397c478bd9Sstevel@tonic-gate 		 */
2402017c965SRod Evans 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_CREATE));
2412017c965SRod Evans 
24256deab07SRod Evans 		if ((ghp = malloc(sizeof (Grp_hdl))) == NULL)
24356deab07SRod Evans 			return (NULL);
24402ca3e02Srie 
24556deab07SRod Evans 		/*
24656deab07SRod Evans 		 * Associate the handle with the link-map list or the reference
24756deab07SRod Evans 		 * link-map as appropriate.
24856deab07SRod Evans 		 */
24956deab07SRod Evans 		if (aplist_append(alpp, ghp, AL_CNT_GROUPS) == NULL) {
25056deab07SRod Evans 			free(ghp);
25156deab07SRod Evans 			return (NULL);
25256deab07SRod Evans 		}
25356deab07SRod Evans 
25456deab07SRod Evans 		/*
25556deab07SRod Evans 		 * Record the existence of this handle for future verification.
25656deab07SRod Evans 		 */
25756deab07SRod Evans 		/* LINTED */
25856deab07SRod Evans 		ndx = (uintptr_t)ghp % HDLIST_SZ;
2597c478bd9Sstevel@tonic-gate 
26056deab07SRod Evans 		if (aplist_append(&hdl_alp[ndx], ghp, AL_CNT_HANDLES) == NULL) {
26156deab07SRod Evans 			(void) aplist_delete_value(*alpp, ghp);
26256deab07SRod Evans 			free(ghp);
26356deab07SRod Evans 			return (NULL);
26456deab07SRod Evans 		}
26556deab07SRod Evans 
26656deab07SRod Evans 		ghp->gh_depends = NULL;
2677c478bd9Sstevel@tonic-gate 		ghp->gh_refcnt = 1;
2688af2c5b9Srie 		ghp->gh_flags = hflags;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		/*
2717c478bd9Sstevel@tonic-gate 		 * A dlopen(0) handle is identified by the GPH_ZERO flag, the
2727c478bd9Sstevel@tonic-gate 		 * head of the link-map list is defined as the owner.  There is
2737c478bd9Sstevel@tonic-gate 		 * no need to maintain a list of dependencies, for when this
2747c478bd9Sstevel@tonic-gate 		 * handle is used (for dlsym()) a dynamic search through the
2757c478bd9Sstevel@tonic-gate 		 * entire link-map list provides for searching all objects with
2767c478bd9Sstevel@tonic-gate 		 * GLOBAL visibility.
2777c478bd9Sstevel@tonic-gate 		 */
2788af2c5b9Srie 		if (hflags & GPH_ZERO) {
2795aefb655Srie 			ghp->gh_ownlmp = lml->lm_head;
2805aefb655Srie 			ghp->gh_ownlml = lml;
2817c478bd9Sstevel@tonic-gate 		} else {
2822017c965SRod Evans 			ghp->gh_ownlmp = rlmp;
2832017c965SRod Evans 			ghp->gh_ownlml = LIST(rlmp);
2847c478bd9Sstevel@tonic-gate 
2852017c965SRod Evans 			if (hdl_add(ghp, rlmp, rdflags, NULL) == NULL)
28656deab07SRod Evans 				return (NULL);
28760758829Srie 
28860758829Srie 			/*
2892017c965SRod Evans 			 * If this new handle is a private handle, there's no
2902017c965SRod Evans 			 * need to track the caller, so we're done.
2912017c965SRod Evans 			 */
2922017c965SRod Evans 			if (hflags & GPH_PRIVATE)
2932017c965SRod Evans 				return (ghp);
2942017c965SRod Evans 
2952017c965SRod Evans 			/*
2962017c965SRod Evans 			 * If this new handle is public, and isn't a special
2972017c965SRod Evans 			 * handle representing ld.so.1, indicate that a local
2982017c965SRod Evans 			 * group now exists.  This state allows singleton
2992017c965SRod Evans 			 * searches to be optimized.
30060758829Srie 			 */
30160758829Srie 			if ((hflags & GPH_LDSO) == 0)
3022017c965SRod Evans 				LIST(rlmp)->lm_flags |= LML_FLG_GROUPSEXIST;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	} else {
3057c478bd9Sstevel@tonic-gate 		/*
30602ca3e02Srie 		 * If a handle already exists, bump its reference count.
30702ca3e02Srie 		 *
30802ca3e02Srie 		 * If the previous reference count was 0, then this is a handle
30902ca3e02Srie 		 * that an earlier call to dlclose() was unable to remove.  Such
31002ca3e02Srie 		 * handles are put on the orphan list.  As this handle is back
31102ca3e02Srie 		 * in use, it must be removed from the orphan list.
31202ca3e02Srie 		 *
31302ca3e02Srie 		 * Note, handles associated with a link-map list itself (i.e.
31402ca3e02Srie 		 * dlopen(0)) can have a reference count of 0.  However, these
31502ca3e02Srie 		 * handles are never deleted, and therefore are never moved to
31602ca3e02Srie 		 * the orphan list.
3177c478bd9Sstevel@tonic-gate 		 */
3187c478bd9Sstevel@tonic-gate 		if ((ghp->gh_refcnt++ == 0) &&
31902ca3e02Srie 		    ((ghp->gh_flags & GPH_ZERO) == 0)) {
3207c478bd9Sstevel@tonic-gate 			uint_t	ndx;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 			/* LINTED */
323b3fbe5e6Sseizo 			ndx = (uintptr_t)ghp % HDLIST_SZ;
3247c478bd9Sstevel@tonic-gate 
32556deab07SRod Evans 			(void) aplist_delete_value(hdl_alp[HDLIST_ORP], ghp);
32656deab07SRod Evans 			(void) aplist_append(&hdl_alp[ndx], ghp,
32756deab07SRod Evans 			    AL_CNT_HANDLES);
3287c478bd9Sstevel@tonic-gate 
3295aefb655Srie 			if (DBG_ENABLED) {
330cce0e03bSab 				Aliste		idx;
3312926dd2eSrie 				Grp_desc	*gdp;
3327c478bd9Sstevel@tonic-gate 
3338af2c5b9Srie 				DBG_CALL(Dbg_file_hdl_title(DBG_HDL_REINST));
334cce0e03bSab 				for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp))
3357c478bd9Sstevel@tonic-gate 					DBG_CALL(Dbg_file_hdl_action(ghp,
33602ca3e02Srie 					    gdp->gd_depend, DBG_DEP_REINST, 0));
3377c478bd9Sstevel@tonic-gate 			}
3387c478bd9Sstevel@tonic-gate 		}
3392017c965SRod Evans 
3402017c965SRod Evans 		/*
3412017c965SRod Evans 		 * If we've been asked to create a private handle, there's no
3422017c965SRod Evans 		 * need to track the caller.
3432017c965SRod Evans 		 */
3442017c965SRod Evans 		if (hflags & GPH_PRIVATE) {
3452017c965SRod Evans 			/*
3462017c965SRod Evans 			 * Negate the reference count increment.
3472017c965SRod Evans 			 */
3482017c965SRod Evans 			ghp->gh_refcnt--;
3492017c965SRod Evans 			return (ghp);
3502017c965SRod Evans 		} else {
3512017c965SRod Evans 			/*
3522017c965SRod Evans 			 * If a private handle already exists, promote this
3532017c965SRod Evans 			 * handle to public by initializing both the reference
3542017c965SRod Evans 			 * count and the handle flags.
3552017c965SRod Evans 			 */
3562017c965SRod Evans 			if (ghp->gh_flags & GPH_PRIVATE) {
3572017c965SRod Evans 				ghp->gh_refcnt = 1;
3582017c965SRod Evans 				ghp->gh_flags &= ~GPH_PRIVATE;
3592017c965SRod Evans 				ghp->gh_flags |= hflags;
3602017c965SRod Evans 			}
3612017c965SRod Evans 		}
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3652017c965SRod Evans 	 * Keep track of the parent (caller).  As this object can be referenced
3668af2c5b9Srie 	 * by different parents, this processing is carried out every time a
3678af2c5b9Srie 	 * handle is requested.
3687c478bd9Sstevel@tonic-gate 	 */
3692017c965SRod Evans 	if (clmp && (hdl_add(ghp, clmp, cdflags, NULL) == NULL))
37056deab07SRod Evans 		return (NULL);
371efb9e8b8Srie 
3727c478bd9Sstevel@tonic-gate 	return (ghp);
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate  * Initialize a handle that has been created for an object that is already
3777c478bd9Sstevel@tonic-gate  * loaded.  The handle is initialized with the present dependencies of that
3787c478bd9Sstevel@tonic-gate  * object.  Once this initialization has occurred, any new objects that might
3797c478bd9Sstevel@tonic-gate  * be loaded as dependencies (lazy-loading) are added to the handle as each new
3807c478bd9Sstevel@tonic-gate  * object is loaded.
3817c478bd9Sstevel@tonic-gate  */
3827c478bd9Sstevel@tonic-gate int
hdl_initialize(Grp_hdl * ghp,Rt_map * nlmp,int mode,int promote)38302ca3e02Srie hdl_initialize(Grp_hdl *ghp, Rt_map *nlmp, int mode, int promote)
3847c478bd9Sstevel@tonic-gate {
385cce0e03bSab 	Aliste		idx;
3867c478bd9Sstevel@tonic-gate 	Grp_desc	*gdp;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * If the handle has already been initialized, and the initial object's
3907c478bd9Sstevel@tonic-gate 	 * mode hasn't been promoted, there's no need to recompute the modes of
3917c478bd9Sstevel@tonic-gate 	 * any dependencies.  If the object we've added has just been opened,
3927c478bd9Sstevel@tonic-gate 	 * the objects dependencies will not yet have been processed.  These
3937c478bd9Sstevel@tonic-gate 	 * dependencies will be added on later calls to load_one().  Otherwise,
3947c478bd9Sstevel@tonic-gate 	 * this object already exists, so add all of its dependencies to the
3957c478bd9Sstevel@tonic-gate 	 * handle were operating on.
3967c478bd9Sstevel@tonic-gate 	 */
3977c478bd9Sstevel@tonic-gate 	if (((ghp->gh_flags & GPH_INITIAL) && (promote == 0)) ||
3987c478bd9Sstevel@tonic-gate 	    ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)) {
3997c478bd9Sstevel@tonic-gate 		ghp->gh_flags |= GPH_INITIAL;
4007c478bd9Sstevel@tonic-gate 		return (1);
4017c478bd9Sstevel@tonic-gate 	}
4027c478bd9Sstevel@tonic-gate 
4038af2c5b9Srie 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
404cce0e03bSab 	for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
4052017c965SRod Evans 		Rt_map		*lmp = gdp->gd_depend;
406cce0e03bSab 		Aliste		idx1;
407cce0e03bSab 		Bnd_desc	*bdp;
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 		/*
4107c478bd9Sstevel@tonic-gate 		 * If this dependency doesn't indicate that its dependencies
4117c478bd9Sstevel@tonic-gate 		 * should be added to a handle, ignore it.  This case identifies
4127c478bd9Sstevel@tonic-gate 		 * a parent of a dlopen(RTLD_PARENT) request.
4137c478bd9Sstevel@tonic-gate 		 */
4147c478bd9Sstevel@tonic-gate 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
4157c478bd9Sstevel@tonic-gate 			continue;
4167c478bd9Sstevel@tonic-gate 
417cce0e03bSab 		for (APLIST_TRAVERSE(DEPENDS(lmp), idx1, bdp)) {
418e0e63816SRod Evans 			Rt_map	*dlmp = bdp->b_depend;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 			if ((bdp->b_flags & BND_NEEDED) == 0)
4217c478bd9Sstevel@tonic-gate 				continue;
4227c478bd9Sstevel@tonic-gate 
423e0e63816SRod Evans 			if (hdl_add(ghp, dlmp,
424e0e63816SRod Evans 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), NULL) == NULL)
4257c478bd9Sstevel@tonic-gate 				return (0);
42602ca3e02Srie 
427e0e63816SRod Evans 			(void) update_mode(dlmp, MODE(dlmp), mode);
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 	}
4307c478bd9Sstevel@tonic-gate 	ghp->gh_flags |= GPH_INITIAL;
4317c478bd9Sstevel@tonic-gate 	return (1);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate /*
4357c478bd9Sstevel@tonic-gate  * Sanity check a program-provided handle.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate static int
hdl_validate(Grp_hdl * ghp)4385aefb655Srie hdl_validate(Grp_hdl *ghp)
4397c478bd9Sstevel@tonic-gate {
44056deab07SRod Evans 	Aliste		idx;
4415aefb655Srie 	Grp_hdl		*lghp;
4427c478bd9Sstevel@tonic-gate 	uint_t		ndx;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* LINTED */
445b3fbe5e6Sseizo 	ndx = (uintptr_t)ghp % HDLIST_SZ;
4467c478bd9Sstevel@tonic-gate 
44756deab07SRod Evans 	for (APLIST_TRAVERSE(hdl_alp[ndx], idx, lghp)) {
4485aefb655Srie 		if ((lghp == ghp) && (ghp->gh_refcnt != 0))
4497c478bd9Sstevel@tonic-gate 			return (1);
4505aefb655Srie 	}
4517c478bd9Sstevel@tonic-gate 	return (0);
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Core dlclose activity.
4567c478bd9Sstevel@tonic-gate  */
4577c478bd9Sstevel@tonic-gate int
dlclose_core(Grp_hdl * ghp,Rt_map * clmp,Lm_list * lml)45802ca3e02Srie dlclose_core(Grp_hdl *ghp, Rt_map *clmp, Lm_list *lml)
4597c478bd9Sstevel@tonic-gate {
46002ca3e02Srie 	int	error;
4612020b2b6SRod Evans 	Rt_map	*lmp;
46202ca3e02Srie 
4637c478bd9Sstevel@tonic-gate 	/*
4647c478bd9Sstevel@tonic-gate 	 * If we're already at atexit() there's no point processing further,
4657c478bd9Sstevel@tonic-gate 	 * all objects have already been tsorted for fini processing.
4667c478bd9Sstevel@tonic-gate 	 */
467dde769a2SRod Evans 	if (rtld_flags & RT_FL_ATEXIT)
4687c478bd9Sstevel@tonic-gate 		return (0);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Diagnose what we're up to.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	if (ghp->gh_flags & GPH_ZERO) {
47498c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlclose(clmp, MSG_ORIG(MSG_STR_ZERO),
4757c478bd9Sstevel@tonic-gate 		    DBG_DLCLOSE_IGNORE));
4767c478bd9Sstevel@tonic-gate 	} else {
47798c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlclose(clmp, NAME(ghp->gh_ownlmp),
47802ca3e02Srie 		    DBG_DLCLOSE_NULL));
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	 * Decrement reference count of this object.
4837c478bd9Sstevel@tonic-gate 	 */
4847c478bd9Sstevel@tonic-gate 	if (--(ghp->gh_refcnt))
4857c478bd9Sstevel@tonic-gate 		return (0);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * If this handle is special (dlopen(0)), then leave it around - it
4897c478bd9Sstevel@tonic-gate 	 * has little overhead.
4907c478bd9Sstevel@tonic-gate 	 */
4917c478bd9Sstevel@tonic-gate 	if (ghp->gh_flags & GPH_ZERO)
4927c478bd9Sstevel@tonic-gate 		return (0);
4937c478bd9Sstevel@tonic-gate 
4942020b2b6SRod Evans 	/*
4952020b2b6SRod Evans 	 * If this handle is associated with an object that is not on the base
4962020b2b6SRod Evans 	 * link-map control list, or it has not yet been relocated, then this
49734b5025bSRod Evans 	 * handle must have originated from an auditors interaction, or some
49834b5025bSRod Evans 	 * permutation of RTLD_CONFGEN use (crle(1), moe(1), etc.).  User code
4992020b2b6SRod Evans 	 * can only execute and bind to relocated objects on the base link-map
50034b5025bSRod Evans 	 * control list.  Outside of RTLD_CONFGEN use, a non-relocated object,
50134b5025bSRod Evans 	 * or an object on a non-base link-map control list, is in the process
50234b5025bSRod Evans 	 * of being loaded, and therefore we do not attempt to remove the
50334b5025bSRod Evans 	 * handle.
5042020b2b6SRod Evans 	 */
5052020b2b6SRod Evans 	if (((lmp = ghp->gh_ownlmp) != NULL) &&
50634b5025bSRod Evans 	    ((MODE(lmp) & RTLD_CONFGEN) == 0) &&
5072020b2b6SRod Evans 	    ((CNTL(lmp) != ALIST_OFF_DATA) ||
5082020b2b6SRod Evans 	    ((FLAGS(lmp) & FLG_RT_RELOCED) == 0)))
5092020b2b6SRod Evans 		return (0);
5102020b2b6SRod Evans 
5117c478bd9Sstevel@tonic-gate 	/*
51202ca3e02Srie 	 * This handle is no longer being referenced, remove it.  If this handle
51302ca3e02Srie 	 * is part of an alternative link-map list, determine if the whole list
51402ca3e02Srie 	 * can be removed also.
5157c478bd9Sstevel@tonic-gate 	 */
516b4059b01SRod Evans 	error = remove_hdl(ghp, clmp, NULL);
51702ca3e02Srie 
51802ca3e02Srie 	if ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)
51902ca3e02Srie 		remove_lml(lml);
52002ca3e02Srie 
52102ca3e02Srie 	return (error);
5227c478bd9Sstevel@tonic-gate }
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate  * Internal dlclose activity.  Called from user level or directly for internal
5267c478bd9Sstevel@tonic-gate  * error cleanup.
5277c478bd9Sstevel@tonic-gate  */
5287c478bd9Sstevel@tonic-gate int
dlclose_intn(Grp_hdl * ghp,Rt_map * clmp)5297c478bd9Sstevel@tonic-gate dlclose_intn(Grp_hdl *ghp, Rt_map *clmp)
5307c478bd9Sstevel@tonic-gate {
531b4059b01SRod Evans 	Rt_map	*nlmp = NULL;
532b4059b01SRod Evans 	Lm_list	*olml = NULL;
5335aefb655Srie 	int	error;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	/*
5367c478bd9Sstevel@tonic-gate 	 * Although we're deleting object(s) it's quite possible that additional
5377c478bd9Sstevel@tonic-gate 	 * objects get loaded from running the .fini section(s) of the objects
5387c478bd9Sstevel@tonic-gate 	 * being deleted.  These objects will have been added to the same
5397c478bd9Sstevel@tonic-gate 	 * link-map list as those objects being deleted.  Remember this list
5407c478bd9Sstevel@tonic-gate 	 * for later investigation.
5417c478bd9Sstevel@tonic-gate 	 */
5425aefb655Srie 	olml = ghp->gh_ownlml;
5437c478bd9Sstevel@tonic-gate 
54402ca3e02Srie 	error = dlclose_core(ghp, clmp, olml);
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	/*
5477c478bd9Sstevel@tonic-gate 	 * Determine whether the original link-map list still exists.  In the
5487c478bd9Sstevel@tonic-gate 	 * case of a dlclose of an alternative (dlmopen) link-map the whole
5497c478bd9Sstevel@tonic-gate 	 * list may have been removed.
5507c478bd9Sstevel@tonic-gate 	 */
5517c478bd9Sstevel@tonic-gate 	if (olml) {
55257ef7aa9SRod Evans 		Aliste	idx;
55357ef7aa9SRod Evans 		Lm_list	*lml;
5547c478bd9Sstevel@tonic-gate 
55557ef7aa9SRod Evans 		for (APLIST_TRAVERSE(dynlm_list, idx, lml)) {
5567c478bd9Sstevel@tonic-gate 			if (olml == lml) {
5577c478bd9Sstevel@tonic-gate 				nlmp = olml->lm_head;
5587c478bd9Sstevel@tonic-gate 				break;
5597c478bd9Sstevel@tonic-gate 			}
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 	}
5627247f888Srie 	load_completion(nlmp);
5637c478bd9Sstevel@tonic-gate 	return (error);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * Argument checking for dlclose.  Only called via external entry.
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate static int
dlclose_check(void * handle,Rt_map * clmp)5707c478bd9Sstevel@tonic-gate dlclose_check(void *handle, Rt_map *clmp)
5717c478bd9Sstevel@tonic-gate {
5725aefb655Srie 	Grp_hdl	*ghp = (Grp_hdl *)handle;
5737c478bd9Sstevel@tonic-gate 
5745aefb655Srie 	if (hdl_validate(ghp) == 0) {
57598c080d5SRod Evans 		Conv_inv_buf_t	inv_buf;
57698c080d5SRod Evans 
57798c080d5SRod Evans 		(void) conv_invalid_val(&inv_buf, EC_NATPTR(ghp), 0);
57898c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlclose(clmp, inv_buf.buf, DBG_DLCLOSE_NULL));
57998c080d5SRod Evans 
580b4059b01SRod Evans 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
581b4059b01SRod Evans 		    EC_NATPTR(handle));
5827c478bd9Sstevel@tonic-gate 		return (1);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	return (dlclose_intn(ghp, clmp));
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877257d1b4Sraf #pragma weak _dlclose = dlclose
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate /*
5907c478bd9Sstevel@tonic-gate  * External entry for dlclose(3dl).  Returns 0 for success, non-zero otherwise.
5917c478bd9Sstevel@tonic-gate  */
5927c478bd9Sstevel@tonic-gate int
dlclose(void * handle)5937257d1b4Sraf dlclose(void *handle)
5947c478bd9Sstevel@tonic-gate {
5957c478bd9Sstevel@tonic-gate 	int		error, entry;
5967c478bd9Sstevel@tonic-gate 	Rt_map		*clmp;
5977c478bd9Sstevel@tonic-gate 
5988cd45542Sraf 	entry = enter(0);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	error = dlclose_check(handle, clmp);
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if (entry)
6058cd45542Sraf 		leave(LIST(clmp), 0);
6067c478bd9Sstevel@tonic-gate 	return (error);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6095aefb655Srie static uint_t	lmid = 0;
6105aefb655Srie 
6115aefb655Srie /*
6125aefb655Srie  * The addition of new link-map lists is assumed to be in small quantities.
6135aefb655Srie  * Here, we assign a unique link-map id for diagnostic use.  Simply update the
6145aefb655Srie  * running link-map count until we max out.
6155aefb655Srie  */
6165aefb655Srie int
newlmid(Lm_list * lml)6175aefb655Srie newlmid(Lm_list *lml)
6185aefb655Srie {
6195aefb655Srie 	char	buffer[MSG_LMID_ALT_SIZE + 12];
6205aefb655Srie 
6215aefb655Srie 	if (lmid == UINT_MAX) {
6225aefb655Srie 		lml->lm_lmid = UINT_MAX;
6235aefb655Srie 		(void) strncpy(buffer, MSG_ORIG(MSG_LMID_MAXED),
6245aefb655Srie 		    MSG_LMID_ALT_SIZE + 12);
6255aefb655Srie 	} else {
6265aefb655Srie 		lml->lm_lmid = lmid++;
6275aefb655Srie 		(void) snprintf(buffer, MSG_LMID_ALT_SIZE + 12,
6285aefb655Srie 		    MSG_ORIG(MSG_LMID_FMT), MSG_ORIG(MSG_LMID_ALT),
6295aefb655Srie 		    lml->lm_lmid);
6305aefb655Srie 	}
631b4059b01SRod Evans 	if ((lml->lm_lmidstr = strdup(buffer)) == NULL)
6325aefb655Srie 		return (0);
6335aefb655Srie 
6345aefb655Srie 	return (1);
6355aefb655Srie }
6365aefb655Srie 
6377c478bd9Sstevel@tonic-gate /*
6387c478bd9Sstevel@tonic-gate  * Core dlopen activity.
6397c478bd9Sstevel@tonic-gate  */
6407c478bd9Sstevel@tonic-gate static Grp_hdl *
dlmopen_core(Lm_list * lml,Lm_list * olml,const char * path,int mode,Rt_map * clmp,uint_t flags,uint_t orig,int * in_nfavl)641dde769a2SRod Evans dlmopen_core(Lm_list *lml, Lm_list *olml, const char *path, int mode,
642dde769a2SRod Evans     Rt_map *clmp, uint_t flags, uint_t orig, int *in_nfavl)
6437c478bd9Sstevel@tonic-gate {
64456deab07SRod Evans 	Alist		*palp = NULL;
64556deab07SRod Evans 	Rt_map		*nlmp;
64656deab07SRod Evans 	Grp_hdl		*ghp;
64756deab07SRod Evans 	Aliste		olmco, nlmco;
6487c478bd9Sstevel@tonic-gate 
64998c080d5SRod Evans 	DBG_CALL(Dbg_dl_dlopen(clmp,
6509aa23310Srie 	    (path ? path : MSG_ORIG(MSG_STR_ZERO)), in_nfavl, mode));
6517c478bd9Sstevel@tonic-gate 
652ca4eed8bSAli Bahrami 	/*
653ca4eed8bSAli Bahrami 	 * Having diagnosed the originally defined modes, assign any defaults
654ca4eed8bSAli Bahrami 	 * or corrections.
655ca4eed8bSAli Bahrami 	 */
656ca4eed8bSAli Bahrami 	if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) &&
657ca4eed8bSAli Bahrami 	    ((mode & RTLD_NOLOAD) == 0))
658ca4eed8bSAli Bahrami 		mode |= (RTLD_GROUP | RTLD_WORLD);
659ca4eed8bSAli Bahrami 	if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) {
660ca4eed8bSAli Bahrami 		mode &= ~RTLD_NOW;
661ca4eed8bSAli Bahrami 		mode |= RTLD_LAZY;
662ca4eed8bSAli Bahrami 	}
663ca4eed8bSAli Bahrami 
6647c478bd9Sstevel@tonic-gate 	/*
6657c478bd9Sstevel@tonic-gate 	 * If the path specified is null then we're operating on global
6667c478bd9Sstevel@tonic-gate 	 * objects.  Associate a dummy handle with the link-map list.
6677c478bd9Sstevel@tonic-gate 	 */
668b4059b01SRod Evans 	if (path == NULL) {
6697c478bd9Sstevel@tonic-gate 		Grp_hdl *ghp;
6702017c965SRod Evans 		uint_t	hflags, rdflags, cdflags;
6717c478bd9Sstevel@tonic-gate 		int	promote = 0;
6727c478bd9Sstevel@tonic-gate 
6738af2c5b9Srie 		/*
6748af2c5b9Srie 		 * Establish any flags for the handle (Grp_hdl).
6758af2c5b9Srie 		 *
6762017c965SRod Evans 		 *  -	This is a dummy, public, handle (0) that provides for a
6772017c965SRod Evans 		 *	dynamic	search of all global objects within the process.
6782017c965SRod Evans 		 *  -   Use of the RTLD_FIRST mode indicates that only the first
6792017c965SRod Evans 		 *	dependency on the handle (the referenced object) can be
6802017c965SRod Evans 		 *	used to satisfy dlsym() requests.
6818af2c5b9Srie 		 */
6822017c965SRod Evans 		hflags = (GPH_PUBLIC | GPH_ZERO);
6837c478bd9Sstevel@tonic-gate 		if (mode & RTLD_FIRST)
6848af2c5b9Srie 			hflags |= GPH_FIRST;
6858af2c5b9Srie 
6868af2c5b9Srie 		/*
6872017c965SRod Evans 		 * Establish the flags for the referenced dependency descriptor
6888af2c5b9Srie 		 * (Grp_desc).
6898af2c5b9Srie 		 *
6902017c965SRod Evans 		 *  -	The referenced object is available for dlsym().
6912017c965SRod Evans 		 *  -	The referenced object is available to relocate against.
6922017c965SRod Evans 		 *  -	The referenced object should have it's dependencies
6932017c965SRod Evans 		 *	added to this handle.
6942017c965SRod Evans 		 */
6952017c965SRod Evans 		rdflags = (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS);
6962017c965SRod Evans 
6972017c965SRod Evans 		/*
6982017c965SRod Evans 		 * Establish the flags for this callers dependency descriptor
6992017c965SRod Evans 		 * (Grp_desc).
7008af2c5b9Srie 		 *
7012017c965SRod Evans 		 *  -	The explicit creation of a handle creates a descriptor
7022017c965SRod Evans 		 *	for the referenced object and the parent (caller).
7032017c965SRod Evans 		 *  -	Use of the RTLD_PARENT flag indicates that the parent
7048af2c5b9Srie 		 *	can be relocated against.
7058af2c5b9Srie 		 */
7062017c965SRod Evans 		cdflags = GPD_PARENT;
7078af2c5b9Srie 		if (mode & RTLD_PARENT)
7088af2c5b9Srie 			cdflags |= GPD_RELOC;
7097c478bd9Sstevel@tonic-gate 
7102017c965SRod Evans 		if ((ghp = hdl_create(lml, 0, clmp, hflags, rdflags,
7112017c965SRod Evans 		    cdflags)) == NULL)
712b4059b01SRod Evans 			return (NULL);
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 		/*
7157c478bd9Sstevel@tonic-gate 		 * Traverse the main link-map control list, updating the mode
7167c478bd9Sstevel@tonic-gate 		 * of any objects as necessary.  Call the relocation engine if
7177c478bd9Sstevel@tonic-gate 		 * this mode promotes the existing state of any relocations.
7187c478bd9Sstevel@tonic-gate 		 * crle()'s first pass loads all objects necessary for building
7197c478bd9Sstevel@tonic-gate 		 * a configuration file, however none of them are relocated.
7207c478bd9Sstevel@tonic-gate 		 * crle()'s second pass relocates objects in preparation for
7217c478bd9Sstevel@tonic-gate 		 * dldump()'ing using dlopen(0, RTLD_NOW).
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN)
7247c478bd9Sstevel@tonic-gate 			return (ghp);
7257c478bd9Sstevel@tonic-gate 
726cb511613SAli Bahrami 		for (nlmp = lml->lm_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
7277c478bd9Sstevel@tonic-gate 			if (((MODE(nlmp) & RTLD_GLOBAL) == 0) ||
7287c478bd9Sstevel@tonic-gate 			    (FLAGS(nlmp) & FLG_RT_DELETE))
7297c478bd9Sstevel@tonic-gate 				continue;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 			if (update_mode(nlmp, MODE(nlmp), mode))
7327c478bd9Sstevel@tonic-gate 				promote = 1;
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		if (promote)
735cce0e03bSab 			(void) relocate_lmc(lml, ALIST_OFF_DATA, clmp,
7369aa23310Srie 			    lml->lm_head, in_nfavl);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		return (ghp);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 	/*
7427c478bd9Sstevel@tonic-gate 	 * Fix the pathname.  If this object expands to multiple paths (ie.
7437c478bd9Sstevel@tonic-gate 	 * $ISALIST or $HWCAP have been used), then make sure the user has also
7447c478bd9Sstevel@tonic-gate 	 * furnished the RTLD_FIRST flag.  As yet, we don't support opening
7457c478bd9Sstevel@tonic-gate 	 * more than one object at a time, so enforcing the RTLD_FIRST flag
7467c478bd9Sstevel@tonic-gate 	 * provides flexibility should we be able to support dlopening more
7477c478bd9Sstevel@tonic-gate 	 * than one object in the future.
7487c478bd9Sstevel@tonic-gate 	 */
74967d74cc3SToomas Soome 	if (LM_FIX_NAME(clmp)(path, clmp, &palp, AL_CNT_NEEDED, orig) == 0)
750b4059b01SRod Evans 		return (NULL);
75102ca3e02Srie 
75256deab07SRod Evans 	if ((palp->al_arritems > 1) && ((mode & RTLD_FIRST) == 0)) {
7532020b2b6SRod Evans 		remove_alist(&palp, 1);
7545aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5));
755b4059b01SRod Evans 		return (NULL);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	/*
759dde769a2SRod Evans 	 * Establish a link-map control list for this request, and load the
7607c478bd9Sstevel@tonic-gate 	 * associated object.
7617c478bd9Sstevel@tonic-gate 	 */
76267d74cc3SToomas Soome 	if ((nlmco = create_cntl(lml, 1)) == 0) {
7632020b2b6SRod Evans 		remove_alist(&palp, 1);
764b4059b01SRod Evans 		return (NULL);
7657c478bd9Sstevel@tonic-gate 	}
766dde769a2SRod Evans 	olmco = nlmco;
7677c478bd9Sstevel@tonic-gate 
7682017c965SRod Evans 	nlmp = load_one(lml, nlmco, palp, clmp, mode, (flags | FLG_RT_PUBHDL),
76956deab07SRod Evans 	    &ghp, in_nfavl);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	/*
7727c478bd9Sstevel@tonic-gate 	 * Remove any expanded pathname infrastructure, and if the dependency
7737c478bd9Sstevel@tonic-gate 	 * couldn't be loaded, cleanup.
7747c478bd9Sstevel@tonic-gate 	 */
7752020b2b6SRod Evans 	remove_alist(&palp, 1);
776b4059b01SRod Evans 	if (nlmp == NULL) {
7777c478bd9Sstevel@tonic-gate 		remove_cntl(lml, olmco);
778b4059b01SRod Evans 		return (NULL);
7797c478bd9Sstevel@tonic-gate 	}
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	/*
7827c478bd9Sstevel@tonic-gate 	 * If loading an auditor was requested, and the auditor already existed,
7837c478bd9Sstevel@tonic-gate 	 * then the link-map returned will be to the original auditor.  The new
7847c478bd9Sstevel@tonic-gate 	 * link-map list that was initially created, and the associated link-map
7857c478bd9Sstevel@tonic-gate 	 * control list are no longer needed.  As the auditor is already loaded,
7867c478bd9Sstevel@tonic-gate 	 * we're probably done, but fall through in case additional relocations
7877c478bd9Sstevel@tonic-gate 	 * would be triggered by the mode of the caller.
7887c478bd9Sstevel@tonic-gate 	 */
7897c478bd9Sstevel@tonic-gate 	if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) {
7907c478bd9Sstevel@tonic-gate 		remove_cntl(lml, olmco);
7917c478bd9Sstevel@tonic-gate 		lml = LIST(nlmp);
7927c478bd9Sstevel@tonic-gate 		olmco = 0;
793cce0e03bSab 		nlmco = ALIST_OFF_DATA;
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * Finish processing the objects associated with this request.
7987c478bd9Sstevel@tonic-gate 	 */
7992020b2b6SRod Evans 	if (((nlmp = analyze_lmc(lml, nlmco, nlmp, clmp, in_nfavl)) == NULL) ||
8009aa23310Srie 	    (relocate_lmc(lml, nlmco, clmp, nlmp, in_nfavl) == 0)) {
801b4059b01SRod Evans 		ghp = NULL;
80256deab07SRod Evans 		nlmp = NULL;
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	/*
806481bba9eSRod Evans 	 * If the dlopen has failed, clean up any objects that might have been
807481bba9eSRod Evans 	 * loaded successfully on this new link-map control list.
80802ca3e02Srie 	 */
809dde769a2SRod Evans 	if (olmco && (nlmp == NULL))
810481bba9eSRod Evans 		remove_lmc(lml, clmp, olmco, path);
81102ca3e02Srie 
81202ca3e02Srie 	/*
813dde769a2SRod Evans 	 * Finally, remove any temporary link-map control list.  Note, if this
814dde769a2SRod Evans 	 * operation successfully established a new link-map list, then a base
815dde769a2SRod Evans 	 * link-map control list will have been created, which must remain.
8167c478bd9Sstevel@tonic-gate 	 */
817dde769a2SRod Evans 	if (olmco && ((nlmp == NULL) || (olml != (Lm_list *)LM_ID_NEWLM)))
8187c478bd9Sstevel@tonic-gate 		remove_cntl(lml, olmco);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	return (ghp);
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8239aa23310Srie /*
8249aa23310Srie  * dlopen() and dlsym() operations are the means by which a process can
8259aa23310Srie  * test for the existence of required dependencies.  If the necessary
8269aa23310Srie  * dependencies don't exist, then associated functionality can't be used.
8279aa23310Srie  * However, the lack of dependencies can be fixed, and the dlopen() and
8289aa23310Srie  * dlsym() requests can be repeated.  As we use a "not-found" AVL tree to
8299aa23310Srie  * cache any failed full path loads, secondary dlopen() and dlsym() requests
8309aa23310Srie  * will fail, even if the dependencies have been installed.
8319aa23310Srie  *
8329aa23310Srie  * dlopen() and dlsym() retry any failures by removing the "not-found" AVL
8339aa23310Srie  * tree.  Should any dependencies be found, their names are added to the
8349aa23310Srie  * FullPath AVL tree.  This routine removes any new "not-found" AVL tree,
8359aa23310Srie  * so that the dlopen() or dlsym() can replace the original "not-found" tree.
8369aa23310Srie  */
8379aa23310Srie inline static void
nfavl_remove(avl_tree_t * avlt)8389aa23310Srie nfavl_remove(avl_tree_t *avlt)
8399aa23310Srie {
8409aa23310Srie 	PathNode	*pnp;
8419aa23310Srie 	void		*cookie = NULL;
8429aa23310Srie 
8439aa23310Srie 	if (avlt) {
84456deab07SRod Evans 		while ((pnp = avl_destroy_nodes(avlt, &cookie)) != NULL)
8459aa23310Srie 			free(pnp);
84656deab07SRod Evans 
8479aa23310Srie 		avl_destroy(avlt);
8489aa23310Srie 		free(avlt);
8499aa23310Srie 	}
8509aa23310Srie }
8519aa23310Srie 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate  * Internal dlopen() activity.  Called from user level or directly for internal
8547c478bd9Sstevel@tonic-gate  * opens that require a handle.
8557c478bd9Sstevel@tonic-gate  */
8567c478bd9Sstevel@tonic-gate Grp_hdl *
dlmopen_intn(Lm_list * lml,const char * path,int mode,Rt_map * clmp,uint_t flags,uint_t orig)85702ca3e02Srie dlmopen_intn(Lm_list *lml, const char *path, int mode, Rt_map *clmp,
8589aa23310Srie     uint_t flags, uint_t orig)
8597c478bd9Sstevel@tonic-gate {
860dde769a2SRod Evans 	Lm_list	*olml = lml;
861b4059b01SRod Evans 	Rt_map	*dlmp = NULL;
8625aefb655Srie 	Grp_hdl	*ghp;
8639aa23310Srie 	int	in_nfavl = 0;
86410a4fa49Srie 
86510a4fa49Srie 	/*
86610a4fa49Srie 	 * Check for magic link-map list values:
86710a4fa49Srie 	 *
86810a4fa49Srie 	 *  LM_ID_BASE:		Operate on the PRIMARY (executables) link map
86910a4fa49Srie 	 *  LM_ID_LDSO:		Operation on ld.so.1's link map
87067d74cc3SToomas Soome 	 *  LM_ID_NEWLM:	Create a new link-map.
87110a4fa49Srie 	 */
87210a4fa49Srie 	if (lml == (Lm_list *)LM_ID_NEWLM) {
873*7eca354dSToomas Soome 		if ((lml = calloc(1, sizeof (Lm_list))) == NULL)
87456deab07SRod Evans 			return (NULL);
87510a4fa49Srie 
87610a4fa49Srie 		/*
87710a4fa49Srie 		 * Establish the new link-map flags from the callers and those
87810a4fa49Srie 		 * explicitly provided.
87910a4fa49Srie 		 */
88010a4fa49Srie 		lml->lm_tflags = LIST(clmp)->lm_tflags;
88110a4fa49Srie 		if (flags & FLG_RT_AUDIT) {
88210a4fa49Srie 			/*
88310a4fa49Srie 			 * Unset any auditing flags - an auditor shouldn't be
88410a4fa49Srie 			 * audited.  Insure all audit dependencies are loaded.
88510a4fa49Srie 			 */
88610a4fa49Srie 			lml->lm_tflags &= ~LML_TFLG_AUD_MASK;
8872020b2b6SRod Evans 			lml->lm_tflags |= (LML_TFLG_NOLAZYLD |
8882020b2b6SRod Evans 			    LML_TFLG_LOADFLTR | LML_TFLG_NOAUDIT);
88910a4fa49Srie 		}
89010a4fa49Srie 
89157ef7aa9SRod Evans 		if (aplist_append(&dynlm_list, lml, AL_CNT_DYNLIST) == NULL) {
89202ca3e02Srie 			free(lml);
89356deab07SRod Evans 			return (NULL);
89402ca3e02Srie 		}
89502ca3e02Srie 		if (newlmid(lml) == 0) {
89657ef7aa9SRod Evans 			(void) aplist_delete_value(dynlm_list, lml);
89710a4fa49Srie 			free(lml);
89856deab07SRod Evans 			return (NULL);
89910a4fa49Srie 		}
90010a4fa49Srie 	} else if ((uintptr_t)lml < LM_ID_NUM) {
90110a4fa49Srie 		if ((uintptr_t)lml == LM_ID_BASE)
90210a4fa49Srie 			lml = &lml_main;
90310a4fa49Srie 		else if ((uintptr_t)lml == LM_ID_LDSO)
90410a4fa49Srie 			lml = &lml_rtld;
90510a4fa49Srie 	}
90610a4fa49Srie 
9077c478bd9Sstevel@tonic-gate 	/*
90810a4fa49Srie 	 * Open the required object on the associated link-map list.
9097c478bd9Sstevel@tonic-gate 	 */
910dde769a2SRod Evans 	ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig, &in_nfavl);
9119aa23310Srie 
9129aa23310Srie 	/*
9139aa23310Srie 	 * If the object could not be found it is possible that the "not-found"
9149aa23310Srie 	 * AVL tree had indicated that the file does not exist.  In case the
91556deab07SRod Evans 	 * file system has changed since this "not-found" recording was made,
9169aa23310Srie 	 * retry the dlopen() with a clean "not-found" AVL tree.
9179aa23310Srie 	 */
918b4059b01SRod Evans 	if ((ghp == NULL) && in_nfavl) {
9199aa23310Srie 		avl_tree_t	*oavlt = nfavl;
9209aa23310Srie 
9219aa23310Srie 		nfavl = NULL;
922dde769a2SRod Evans 		ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig,
923dde769a2SRod Evans 		    NULL);
9249aa23310Srie 
9257c478bd9Sstevel@tonic-gate 		/*
9269aa23310Srie 		 * If the file is found, then its full path name will have been
9279aa23310Srie 		 * registered in the FullPath AVL tree.  Remove any new
9289aa23310Srie 		 * "not-found" AVL information, and restore the former AVL tree.
9297c478bd9Sstevel@tonic-gate 		 */
9309aa23310Srie 		nfavl_remove(nfavl);
9319aa23310Srie 		nfavl = oavlt;
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 
9349aa23310Srie 	/*
9359aa23310Srie 	 * Establish the new link-map from which .init processing will begin.
9369aa23310Srie 	 * Ignore .init firing when constructing a configuration file (crle(1)).
9379aa23310Srie 	 */
9389aa23310Srie 	if (ghp && ((mode & RTLD_CONFGEN) == 0))
9399aa23310Srie 		dlmp = ghp->gh_ownlmp;
9409aa23310Srie 
94102ca3e02Srie 	/*
94202ca3e02Srie 	 * If loading an auditor was requested, and the auditor already existed,
94302ca3e02Srie 	 * then the link-map returned will be to the original auditor.  Remove
94402ca3e02Srie 	 * the link-map control list that was created for this request.
94502ca3e02Srie 	 */
94602ca3e02Srie 	if (dlmp && (flags & FLG_RT_AUDIT) && (LIST(dlmp) != lml)) {
94702ca3e02Srie 		remove_lml(lml);
94802ca3e02Srie 		lml = LIST(dlmp);
94902ca3e02Srie 	}
95002ca3e02Srie 
95102ca3e02Srie 	/*
95202ca3e02Srie 	 * If this load failed, remove any alternative link-map list.
95302ca3e02Srie 	 */
954b4059b01SRod Evans 	if ((ghp == NULL) &&
95502ca3e02Srie 	    ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)) {
95602ca3e02Srie 		remove_lml(lml);
957b4059b01SRod Evans 		lml = NULL;
95802ca3e02Srie 	}
95902ca3e02Srie 
96002ca3e02Srie 	/*
96102ca3e02Srie 	 * Finish this load request.  If objects were loaded, .init processing
96202ca3e02Srie 	 * is computed.  Finally, the debuggers are informed of the link-map
96302ca3e02Srie 	 * lists being stable.
96402ca3e02Srie 	 */
9657247f888Srie 	load_completion(dlmp);
96602ca3e02Srie 
9677c478bd9Sstevel@tonic-gate 	return (ghp);
9687c478bd9Sstevel@tonic-gate }
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * Argument checking for dlopen.  Only called via external entry.
9727c478bd9Sstevel@tonic-gate  */
9737c478bd9Sstevel@tonic-gate static Grp_hdl *
dlmopen_check(Lm_list * lml,const char * path,int mode,Rt_map * clmp)9749aa23310Srie dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp)
9757c478bd9Sstevel@tonic-gate {
9767c478bd9Sstevel@tonic-gate 	/*
9777c478bd9Sstevel@tonic-gate 	 * Verify that a valid pathname has been supplied.
9787c478bd9Sstevel@tonic-gate 	 */
9797c478bd9Sstevel@tonic-gate 	if (path && (*path == '\0')) {
9805aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
9817c478bd9Sstevel@tonic-gate 		return (0);
9827c478bd9Sstevel@tonic-gate 	}
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * Historically we've always verified the mode is either RTLD_NOW or
9867c478bd9Sstevel@tonic-gate 	 * RTLD_LAZY.  RTLD_NOLOAD is valid by itself.  Use of LM_ID_NEWLM
9877c478bd9Sstevel@tonic-gate 	 * requires a specific pathname, and use of RTLD_PARENT is meaningless.
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) {
9905aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1));
9917c478bd9Sstevel@tonic-gate 		return (0);
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 	if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) {
9945aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2));
9957c478bd9Sstevel@tonic-gate 		return (0);
9967c478bd9Sstevel@tonic-gate 	}
997b4059b01SRod Evans 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == NULL)) {
9985aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3));
9997c478bd9Sstevel@tonic-gate 		return (0);
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) {
10025aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_4));
10037c478bd9Sstevel@tonic-gate 		return (0);
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10069aa23310Srie 	return (dlmopen_intn(lml, path, mode, clmp, 0, 0));
10077c478bd9Sstevel@tonic-gate }
10087c478bd9Sstevel@tonic-gate 
10097257d1b4Sraf #pragma weak _dlopen = dlopen
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate /*
10127c478bd9Sstevel@tonic-gate  * External entry for dlopen(3dl).  On success, returns a pointer (handle) to
10137c478bd9Sstevel@tonic-gate  * the structure containing information about the newly added object, ie. can
10147c478bd9Sstevel@tonic-gate  * be used by dlsym(). On failure, returns a null pointer.
10157c478bd9Sstevel@tonic-gate  */
10167c478bd9Sstevel@tonic-gate void *
dlopen(const char * path,int mode)10177257d1b4Sraf dlopen(const char *path, int mode)
10187c478bd9Sstevel@tonic-gate {
10199aa23310Srie 	int	entry;
10205aefb655Srie 	Rt_map	*clmp;
10215aefb655Srie 	Grp_hdl	*ghp;
10225aefb655Srie 	Lm_list	*lml;
10237c478bd9Sstevel@tonic-gate 
10248cd45542Sraf 	entry = enter(0);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
10277c478bd9Sstevel@tonic-gate 	lml = LIST(clmp);
10287c478bd9Sstevel@tonic-gate 
10299aa23310Srie 	ghp = dlmopen_check(lml, path, mode, clmp);
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 	if (entry)
10328cd45542Sraf 		leave(lml, 0);
10337c478bd9Sstevel@tonic-gate 	return ((void *)ghp);
10347c478bd9Sstevel@tonic-gate }
10357c478bd9Sstevel@tonic-gate 
10367257d1b4Sraf #pragma weak _dlmopen = dlmopen
10377257d1b4Sraf 
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate  * External entry for dlmopen(3dl).
10407c478bd9Sstevel@tonic-gate  */
10417c478bd9Sstevel@tonic-gate void *
dlmopen(Lmid_t lmid,const char * path,int mode)10427257d1b4Sraf dlmopen(Lmid_t lmid, const char *path, int mode)
10437c478bd9Sstevel@tonic-gate {
10449aa23310Srie 	int	entry;
10455aefb655Srie 	Rt_map	*clmp;
10465aefb655Srie 	Grp_hdl	*ghp;
10477c478bd9Sstevel@tonic-gate 
10488cd45542Sraf 	entry = enter(0);
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
10517c478bd9Sstevel@tonic-gate 
10529aa23310Srie 	ghp = dlmopen_check((Lm_list *)lmid, path, mode, clmp);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (entry)
10558cd45542Sraf 		leave(LIST(clmp), 0);
10567c478bd9Sstevel@tonic-gate 	return ((void *)ghp);
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate /*
10607c478bd9Sstevel@tonic-gate  * Handle processing for dlsym.
10617c478bd9Sstevel@tonic-gate  */
106208278a5eSRod Evans int
dlsym_handle(Grp_hdl * ghp,Slookup * slp,Sresult * srp,uint_t * binfo,int * in_nfavl)106308278a5eSRod Evans dlsym_handle(Grp_hdl *ghp, Slookup *slp, Sresult *srp, uint_t *binfo,
10649aa23310Srie     int *in_nfavl)
10657c478bd9Sstevel@tonic-gate {
10665aefb655Srie 	Rt_map		*nlmp, * lmp = ghp->gh_ownlmp;
10677c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
10687c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
10697c478bd9Sstevel@tonic-gate 	Slookup		sl = *slp;
10707c478bd9Sstevel@tonic-gate 
107108278a5eSRod Evans 	sl.sl_flags = (LKUP_FIRST | LKUP_DLSYM | LKUP_SPEC);
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	/*
10747c478bd9Sstevel@tonic-gate 	 * Continue processing a dlsym request.  Lookup the required symbol in
10757c478bd9Sstevel@tonic-gate 	 * each link-map specified by the handle.
10767c478bd9Sstevel@tonic-gate 	 *
10777c478bd9Sstevel@tonic-gate 	 * To leverage off of lazy loading, dlsym() requests can result in two
10787c478bd9Sstevel@tonic-gate 	 * passes.  The first descends the link-maps of any objects already in
10797c478bd9Sstevel@tonic-gate 	 * the address space.  If the symbol isn't located, and lazy
10807c478bd9Sstevel@tonic-gate 	 * dependencies still exist, then a second pass is made to load these
10817c478bd9Sstevel@tonic-gate 	 * dependencies if applicable.  This model means that in the case where
1082e0e63816SRod Evans 	 * a symbol exists in more than one object, the one located may not be
10837c478bd9Sstevel@tonic-gate 	 * constant - this is the standard issue with lazy loading. In addition,
10847c478bd9Sstevel@tonic-gate 	 * attempting to locate a symbol that doesn't exist will result in the
10857c478bd9Sstevel@tonic-gate 	 * loading of all lazy dependencies on the given handle, which can
10867c478bd9Sstevel@tonic-gate 	 * defeat some of the advantages of lazy loading (look out JVM).
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate 	if (ghp->gh_flags & GPH_ZERO) {
10892926dd2eSrie 		Lm_list	*lml;
1090e0e63816SRod Evans 		uint_t	lazy = 0;
10912926dd2eSrie 
10927c478bd9Sstevel@tonic-gate 		/*
10937c478bd9Sstevel@tonic-gate 		 * If this symbol lookup is triggered from a dlopen(0) handle,
10947c478bd9Sstevel@tonic-gate 		 * traverse the present link-map list looking for promiscuous
10957c478bd9Sstevel@tonic-gate 		 * entries.
10967c478bd9Sstevel@tonic-gate 		 */
1097cb511613SAli Bahrami 		for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
10987c478bd9Sstevel@tonic-gate 			/*
10997c478bd9Sstevel@tonic-gate 			 * If this handle indicates we're only to look in the
11007c478bd9Sstevel@tonic-gate 			 * first object check whether we're done.
11017c478bd9Sstevel@tonic-gate 			 */
11027c478bd9Sstevel@tonic-gate 			if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST))
110308278a5eSRod Evans 				return (0);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 			if (!(MODE(nlmp) & RTLD_GLOBAL))
11067c478bd9Sstevel@tonic-gate 				continue;
11077c478bd9Sstevel@tonic-gate 			if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
11087c478bd9Sstevel@tonic-gate 			    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
11097c478bd9Sstevel@tonic-gate 				continue;
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 			sl.sl_imap = nlmp;
111208278a5eSRod Evans 			if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl))
111308278a5eSRod Evans 				return (1);
1114e0e63816SRod Evans 
1115e0e63816SRod Evans 			/*
1116e0e63816SRod Evans 			 * Keep track of any global pending lazy loads.
1117e0e63816SRod Evans 			 */
1118e0e63816SRod Evans 			lazy += LAZY(nlmp);
11197c478bd9Sstevel@tonic-gate 		}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 		/*
1122e0e63816SRod Evans 		 * If we're unable to locate the symbol and this link-map list
1123e0e63816SRod Evans 		 * still has pending lazy dependencies, start loading them in an
11247c478bd9Sstevel@tonic-gate 		 * attempt to exhaust the search.  Note that as we're already
11257c478bd9Sstevel@tonic-gate 		 * traversing a dynamic linked list of link-maps there's no
11267c478bd9Sstevel@tonic-gate 		 * need for elf_lazy_find_sym() to descend the link-maps itself.
11277c478bd9Sstevel@tonic-gate 		 */
11282926dd2eSrie 		lml = LIST(lmp);
1129e0e63816SRod Evans 		if (lazy) {
11302926dd2eSrie 			DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 			sl.sl_flags |= LKUP_NODESCENT;
11337c478bd9Sstevel@tonic-gate 
1134cb511613SAli Bahrami 			for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 				if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp))
11377c478bd9Sstevel@tonic-gate 					continue;
11387c478bd9Sstevel@tonic-gate 				if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
11397c478bd9Sstevel@tonic-gate 				    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
11407c478bd9Sstevel@tonic-gate 					continue;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 				sl.sl_imap = nlmp;
114308278a5eSRod Evans 				if (elf_lazy_find_sym(&sl, srp, binfo,
11449aa23310Srie 				    in_nfavl))
114508278a5eSRod Evans 					return (1);
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 	} else {
11497c478bd9Sstevel@tonic-gate 		/*
1150e0e63816SRod Evans 		 * Traverse the dlopen() handle searching all presently loaded
11517c478bd9Sstevel@tonic-gate 		 * link-maps.
11527c478bd9Sstevel@tonic-gate 		 */
11532926dd2eSrie 		Grp_desc	*gdp;
1154cce0e03bSab 		Aliste		idx;
1155e0e63816SRod Evans 		uint_t		lazy = 0;
11567c478bd9Sstevel@tonic-gate 
1157cce0e03bSab 		for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1158e0e63816SRod Evans 			nlmp = gdp->gd_depend;
1159e0e63816SRod Evans 
1160efb9e8b8Srie 			if ((gdp->gd_flags & GPD_DLSYM) == 0)
11617c478bd9Sstevel@tonic-gate 				continue;
11627c478bd9Sstevel@tonic-gate 
1163e0e63816SRod Evans 			sl.sl_imap = nlmp;
116408278a5eSRod Evans 			if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl))
116508278a5eSRod Evans 				return (1);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 			if (ghp->gh_flags & GPH_FIRST)
116808278a5eSRod Evans 				return (0);
1169e0e63816SRod Evans 
1170e0e63816SRod Evans 			/*
1171e0e63816SRod Evans 			 * Keep track of any pending lazy loads associated
1172e0e63816SRod Evans 			 * with this handle.
1173e0e63816SRod Evans 			 */
1174e0e63816SRod Evans 			lazy += LAZY(nlmp);
11757c478bd9Sstevel@tonic-gate 		}
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 		/*
1178e0e63816SRod Evans 		 * If we're unable to locate the symbol and this handle still
1179e0e63816SRod Evans 		 * has pending lazy dependencies, start loading the lazy
1180e0e63816SRod Evans 		 * dependencies, in an attempt to exhaust the search.
11817c478bd9Sstevel@tonic-gate 		 */
1182e0e63816SRod Evans 		if (lazy) {
11835aefb655Srie 			DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name));
11847c478bd9Sstevel@tonic-gate 
1185cce0e03bSab 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
11867c478bd9Sstevel@tonic-gate 				nlmp = gdp->gd_depend;
11877c478bd9Sstevel@tonic-gate 
1188efb9e8b8Srie 				if (((gdp->gd_flags & GPD_DLSYM) == 0) ||
11897c478bd9Sstevel@tonic-gate 				    (LAZY(nlmp) == 0))
11907c478bd9Sstevel@tonic-gate 					continue;
11912926dd2eSrie 
11927c478bd9Sstevel@tonic-gate 				sl.sl_imap = nlmp;
119308278a5eSRod Evans 				if (elf_lazy_find_sym(&sl, srp, binfo,
119408278a5eSRod Evans 				    in_nfavl))
119508278a5eSRod Evans 					return (1);
11967c478bd9Sstevel@tonic-gate 			}
11977c478bd9Sstevel@tonic-gate 		}
11987c478bd9Sstevel@tonic-gate 	}
119908278a5eSRod Evans 	return (0);
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
1202f441771bSRod Evans /*
1203f441771bSRod Evans  * Determine whether a symbol resides in a caller.  This may be a reference,
1204f441771bSRod Evans  * which is associated with a specific dependency.
1205f441771bSRod Evans  */
1206f441771bSRod Evans inline static Sym *
sym_lookup_in_caller(Rt_map * clmp,Slookup * slp,Sresult * srp,uint_t * binfo)1207f441771bSRod Evans sym_lookup_in_caller(Rt_map *clmp, Slookup *slp, Sresult *srp, uint_t *binfo)
1208f441771bSRod Evans {
1209f441771bSRod Evans 	if (THIS_IS_ELF(clmp) && SYMINTP(clmp)(slp, srp, binfo, NULL)) {
1210f441771bSRod Evans 		Sym	*sym = srp->sr_sym;
1211f441771bSRod Evans 
1212f441771bSRod Evans 		slp->sl_rsymndx = (((ulong_t)sym -
1213f441771bSRod Evans 		    (ulong_t)SYMTAB(clmp)) / SYMENT(clmp));
1214f441771bSRod Evans 		slp->sl_rsym = sym;
1215f441771bSRod Evans 		return (sym);
1216f441771bSRod Evans 	}
1217f441771bSRod Evans 	return (NULL);
1218f441771bSRod Evans }
1219f441771bSRod Evans 
12207c478bd9Sstevel@tonic-gate /*
12217c478bd9Sstevel@tonic-gate  * Core dlsym activity.  Selects symbol lookup method from handle.
12227c478bd9Sstevel@tonic-gate  */
122308278a5eSRod Evans static void *
dlsym_core(void * handle,const char * name,Rt_map * clmp,Rt_map ** dlmp,int * in_nfavl)12249aa23310Srie dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp,
12259aa23310Srie     int *in_nfavl)
12267c478bd9Sstevel@tonic-gate {
1227f441771bSRod Evans 	Sym		*sym;
122808278a5eSRod Evans 	int		ret = 0;
1229660acd81Srie 	Syminfo		*sip;
12307c478bd9Sstevel@tonic-gate 	Slookup		sl;
123108278a5eSRod Evans 	Sresult		sr;
12327c478bd9Sstevel@tonic-gate 	uint_t		binfo;
12337c478bd9Sstevel@tonic-gate 
1234660acd81Srie 	/*
123575e7992aSrie 	 * Initialize the symbol lookup data structure.
123675e7992aSrie 	 *
1237660acd81Srie 	 * Standard relocations are evaluated using the symbol index of the
1238660acd81Srie 	 * associated relocation symbol.  This index provides for loading
1239660acd81Srie 	 * any lazy dependency and establishing a direct binding if necessary.
1240660acd81Srie 	 * If a dlsym() operation originates from an object that contains a
124175e7992aSrie 	 * symbol table entry for the same name, then we need to establish the
124275e7992aSrie 	 * symbol index so that any dependency requirements can be triggered.
124375e7992aSrie 	 *
124475e7992aSrie 	 * Therefore, the first symbol lookup that is carried out is for the
124575e7992aSrie 	 * symbol name within the calling object.  If this symbol exists, the
124675e7992aSrie 	 * symbols index is computed, added to the Slookup data, and thus used
124775e7992aSrie 	 * to seed the real symbol lookup.
1248660acd81Srie 	 */
124975e7992aSrie 	SLOOKUP_INIT(sl, name, clmp, clmp, ld_entry_cnt, elf_hash(name),
125075e7992aSrie 	    0, 0, 0, LKUP_SYMNDX);
125108278a5eSRod Evans 	SRESULT_INIT(sr, name);
1252f441771bSRod Evans 	sym = sym_lookup_in_caller(clmp, &sl, &sr, &binfo);
1253660acd81Srie 
125408278a5eSRod Evans 	SRESULT_INIT(sr, name);
125508278a5eSRod Evans 
125660758829Srie 	if (sym && (ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON)) {
125760758829Srie 		Rt_map	*hlmp = LIST(clmp)->lm_head;
125860758829Srie 
125960758829Srie 		/*
126060758829Srie 		 * If a symbol reference is known, and that reference indicates
126160758829Srie 		 * that the symbol is a singleton, then the search for the
126260758829Srie 		 * symbol must follow the default search path.
126360758829Srie 		 */
126498c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0,
12659aa23310Srie 		    DBG_DLSYM_SINGLETON));
126660758829Srie 
126760758829Srie 		sl.sl_imap = hlmp;
126860758829Srie 		if (handle == RTLD_PROBE)
1269f441771bSRod Evans 			sl.sl_flags = LKUP_NOFALLBACK;
1270f441771bSRod Evans 		else
1271f441771bSRod Evans 			sl.sl_flags = LKUP_SPEC;
127208278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
127360758829Srie 
127460758829Srie 	} else if (handle == RTLD_NEXT) {
12757c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp;
12767c478bd9Sstevel@tonic-gate 
1277660acd81Srie 		/*
1278660acd81Srie 		 * If this handle is RTLD_NEXT determine whether a lazy load
1279660acd81Srie 		 * from the caller might provide the next object.  This mimics
1280660acd81Srie 		 * the lazy loading initialization normally carried out by
1281660acd81Srie 		 * lookup_sym(), however here, we must do this up-front, as
1282660acd81Srie 		 * lookup_sym() will be used to inspect the next object.
1283660acd81Srie 		 */
1284b4059b01SRod Evans 		if ((sl.sl_rsymndx) && ((sip = SYMINFO(clmp)) != NULL)) {
1285660acd81Srie 			/* LINTED */
1286660acd81Srie 			sip = (Syminfo *)((char *)sip +
1287660acd81Srie 			    (sl.sl_rsymndx * SYMINENT(clmp)));
1288660acd81Srie 
1289660acd81Srie 			if ((sip->si_flags & SYMINFO_FLG_DIRECT) &&
1290660acd81Srie 			    (sip->si_boundto < SYMINFO_BT_LOWRESERVE))
129175e7992aSrie 				(void) elf_lazy_load(clmp, &sl,
12922017c965SRod Evans 				    sip->si_boundto, name, 0, NULL, in_nfavl);
1293660acd81Srie 
1294660acd81Srie 			/*
1295660acd81Srie 			 * Clear the symbol index, so as not to confuse
1296660acd81Srie 			 * lookup_sym() of the next object.
1297660acd81Srie 			 */
1298660acd81Srie 			sl.sl_rsymndx = 0;
1299b4059b01SRod Evans 			sl.sl_rsym = NULL;
1300660acd81Srie 		}
1301660acd81Srie 
13027c478bd9Sstevel@tonic-gate 		/*
1303f441771bSRod Evans 		 * If the handle is RTLD_NEXT, start searching in the next link
13047c478bd9Sstevel@tonic-gate 		 * map from the callers.  Determine permissions from the
13057c478bd9Sstevel@tonic-gate 		 * present link map.  Indicate to lookup_sym() that we're on an
13067c478bd9Sstevel@tonic-gate 		 * RTLD_NEXT request so that it will use the callers link map to
13077c478bd9Sstevel@tonic-gate 		 * start any possible lazy dependency loading.
13087c478bd9Sstevel@tonic-gate 		 */
1309cb511613SAli Bahrami 		sl.sl_imap = nlmp = NEXT_RT_MAP(clmp);
13107c478bd9Sstevel@tonic-gate 
131198c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl,
13129aa23310Srie 		    (nlmp ? NAME(nlmp) : MSG_INTL(MSG_STR_NULL)),
13139aa23310Srie 		    DBG_DLSYM_NEXT));
13147c478bd9Sstevel@tonic-gate 
1315b4059b01SRod Evans 		if (nlmp == NULL)
13167c478bd9Sstevel@tonic-gate 			return (0);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 		sl.sl_flags = LKUP_NEXT;
131908278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
13207c478bd9Sstevel@tonic-gate 
13217c478bd9Sstevel@tonic-gate 	} else if (handle == RTLD_SELF) {
13227c478bd9Sstevel@tonic-gate 		/*
13237c478bd9Sstevel@tonic-gate 		 * If the handle is RTLD_SELF start searching from the caller.
13247c478bd9Sstevel@tonic-gate 		 */
132598c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, NAME(clmp),
13267c478bd9Sstevel@tonic-gate 		    DBG_DLSYM_SELF));
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 		sl.sl_imap = clmp;
132960758829Srie 		sl.sl_flags = (LKUP_SPEC | LKUP_SELF);
133008278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
13317c478bd9Sstevel@tonic-gate 
1332660acd81Srie 	} else if (handle == RTLD_DEFAULT) {
13337c478bd9Sstevel@tonic-gate 		Rt_map	*hlmp = LIST(clmp)->lm_head;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 		/*
1336660acd81Srie 		 * If the handle is RTLD_DEFAULT mimic the standard symbol
1337660acd81Srie 		 * lookup as would be triggered by a relocation.
13387c478bd9Sstevel@tonic-gate 		 */
133998c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0,
13409aa23310Srie 		    DBG_DLSYM_DEFAULT));
13417c478bd9Sstevel@tonic-gate 
1342660acd81Srie 		sl.sl_imap = hlmp;
1343660acd81Srie 		sl.sl_flags = LKUP_SPEC;
134408278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
13457c478bd9Sstevel@tonic-gate 
1346660acd81Srie 	} else if (handle == RTLD_PROBE) {
1347660acd81Srie 		Rt_map	*hlmp = LIST(clmp)->lm_head;
1348660acd81Srie 
1349660acd81Srie 		/*
1350660acd81Srie 		 * If the handle is RTLD_PROBE, mimic the standard symbol
1351660acd81Srie 		 * lookup as would be triggered by a relocation, however do
1352660acd81Srie 		 * not fall back to a lazy loading rescan if the symbol can't be
1353660acd81Srie 		 * found within the currently loaded objects.  Note, a lazy
1354660acd81Srie 		 * loaded dependency required by the caller might still get
1355660acd81Srie 		 * loaded to satisfy this request, but no exhaustive lazy load
1356660acd81Srie 		 * rescan is carried out.
1357660acd81Srie 		 */
135898c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0,
13599aa23310Srie 		    DBG_DLSYM_PROBE));
1360660acd81Srie 
1361660acd81Srie 		sl.sl_imap = hlmp;
1362f441771bSRod Evans 		sl.sl_flags = LKUP_NOFALLBACK;
136308278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	} else {
13667c478bd9Sstevel@tonic-gate 		Grp_hdl *ghp = (Grp_hdl *)handle;
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate 		/*
13697c478bd9Sstevel@tonic-gate 		 * Look in the shared object specified by the handle and in all
13707c478bd9Sstevel@tonic-gate 		 * of its dependencies.
13717c478bd9Sstevel@tonic-gate 		 */
137298c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl,
13739aa23310Srie 		    NAME(ghp->gh_ownlmp), DBG_DLSYM_DEF));
13742926dd2eSrie 
137508278a5eSRod Evans 		ret = LM_DLSYM(clmp)(ghp, &sl, &sr, &binfo, in_nfavl);
13767c478bd9Sstevel@tonic-gate 	}
13777c478bd9Sstevel@tonic-gate 
137808278a5eSRod Evans 	if (ret && ((sym = sr.sr_sym) != NULL)) {
13795aefb655Srie 		Lm_list	*lml = LIST(clmp);
13807c478bd9Sstevel@tonic-gate 		Addr	addr = sym->st_value;
13817c478bd9Sstevel@tonic-gate 
138208278a5eSRod Evans 		*dlmp = sr.sr_dmap;
13837c478bd9Sstevel@tonic-gate 		if (!(FLAGS(*dlmp) & FLG_RT_FIXED))
13847c478bd9Sstevel@tonic-gate 			addr += ADDR(*dlmp);
13857c478bd9Sstevel@tonic-gate 
13869aa23310Srie 		/*
13879aa23310Srie 		 * Indicate that the defining object is now used.
13889aa23310Srie 		 */
13899aa23310Srie 		if (*dlmp != clmp)
13909aa23310Srie 			FLAGS1(*dlmp) |= FL1_RT_USED;
13919aa23310Srie 
13925aefb655Srie 		DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE,
139308278a5eSRod Evans 		    *dlmp, addr, sym->st_value, sr.sr_name, binfo));
13947c478bd9Sstevel@tonic-gate 
139538f4bdddSBryan Cantrill 		if ((lml->lm_tflags | AFLAGS(clmp) | AFLAGS(*dlmp)) &
139638f4bdddSBryan Cantrill 		    LML_TFLG_AUD_SYMBIND) {
13977c478bd9Sstevel@tonic-gate 			uint_t	sb_flags = LA_SYMB_DLSYM;
13987c478bd9Sstevel@tonic-gate 			/* LINTED */
13997c478bd9Sstevel@tonic-gate 			uint_t	symndx = (uint_t)(((Xword)sym -
14007c478bd9Sstevel@tonic-gate 			    (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp));
14017c478bd9Sstevel@tonic-gate 			addr = audit_symbind(clmp, *dlmp, sym, symndx, addr,
14027c478bd9Sstevel@tonic-gate 			    &sb_flags);
14037c478bd9Sstevel@tonic-gate 		}
14047c478bd9Sstevel@tonic-gate 		return ((void *)addr);
140508278a5eSRod Evans 	}
140608278a5eSRod Evans 
140708278a5eSRod Evans 	return (NULL);
14087c478bd9Sstevel@tonic-gate }
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate /*
14117c478bd9Sstevel@tonic-gate  * Internal dlsym activity.  Called from user level or directly for internal
14127c478bd9Sstevel@tonic-gate  * symbol lookup.
14137c478bd9Sstevel@tonic-gate  */
14147c478bd9Sstevel@tonic-gate void *
dlsym_intn(void * handle,const char * name,Rt_map * clmp,Rt_map ** dlmp)14157c478bd9Sstevel@tonic-gate dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
14167c478bd9Sstevel@tonic-gate {
1417b4059b01SRod Evans 	Rt_map		*llmp = NULL;
14185aefb655Srie 	void		*error;
1419cce0e03bSab 	Aliste		idx;
14205aefb655Srie 	Grp_desc	*gdp;
14219aa23310Srie 	int		in_nfavl = 0;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
14247c478bd9Sstevel@tonic-gate 	 * While looking for symbols it's quite possible that additional objects
14257c478bd9Sstevel@tonic-gate 	 * get loaded from lazy loading.  These objects will have been added to
14267c478bd9Sstevel@tonic-gate 	 * the same link-map list as those objects on the handle.  Remember this
14277c478bd9Sstevel@tonic-gate 	 * list for later investigation.
14287c478bd9Sstevel@tonic-gate 	 */
14297c478bd9Sstevel@tonic-gate 	if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) ||
14307c478bd9Sstevel@tonic-gate 	    (handle == RTLD_SELF) || (handle == RTLD_PROBE))
14317c478bd9Sstevel@tonic-gate 		llmp = LIST(clmp)->lm_tail;
14327c478bd9Sstevel@tonic-gate 	else {
14332926dd2eSrie 		Grp_hdl	*ghp = (Grp_hdl *)handle;
14347c478bd9Sstevel@tonic-gate 
14355aefb655Srie 		if (ghp->gh_ownlmp)
14365aefb655Srie 			llmp = LIST(ghp->gh_ownlmp)->lm_tail;
14377c478bd9Sstevel@tonic-gate 		else {
1438cce0e03bSab 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1439b4059b01SRod Evans 				if ((llmp =
1440b4059b01SRod Evans 				    LIST(gdp->gd_depend)->lm_tail) != NULL)
14417c478bd9Sstevel@tonic-gate 					break;
14427c478bd9Sstevel@tonic-gate 			}
14437c478bd9Sstevel@tonic-gate 		}
14447c478bd9Sstevel@tonic-gate 	}
14457c478bd9Sstevel@tonic-gate 
14469aa23310Srie 	error = dlsym_core(handle, name, clmp, dlmp, &in_nfavl);
14479aa23310Srie 
14489aa23310Srie 	/*
14499aa23310Srie 	 * If the symbol could not be found it is possible that the "not-found"
14509aa23310Srie 	 * AVL tree had indicated that a required file does not exist.  In case
14519aa23310Srie 	 * the file system has changed since this "not-found" recording was
14529aa23310Srie 	 * made, retry the dlsym() with a clean "not-found" AVL tree.
14539aa23310Srie 	 */
1454b4059b01SRod Evans 	if ((error == NULL) && in_nfavl) {
14559aa23310Srie 		avl_tree_t	*oavlt = nfavl;
14569aa23310Srie 
14579aa23310Srie 		nfavl = NULL;
14589aa23310Srie 		error = dlsym_core(handle, name, clmp, dlmp, NULL);
14599aa23310Srie 
14609aa23310Srie 		/*
14619aa23310Srie 		 * If the symbol is found, then any file that was loaded will
14629aa23310Srie 		 * have had its full path name registered in the FullPath AVL
14639aa23310Srie 		 * tree.  Remove any new "not-found" AVL information, and
14649aa23310Srie 		 * restore the former AVL tree.
14659aa23310Srie 		 */
14669aa23310Srie 		nfavl_remove(nfavl);
14679aa23310Srie 		nfavl = oavlt;
14689aa23310Srie 	}
14699aa23310Srie 
1470b4059b01SRod Evans 	if (error == NULL) {
14717c478bd9Sstevel@tonic-gate 		/*
14727c478bd9Sstevel@tonic-gate 		 * Cache the error message, as Java tends to fall through this
14737c478bd9Sstevel@tonic-gate 		 * code many times.
14747c478bd9Sstevel@tonic-gate 		 */
1475b4059b01SRod Evans 		if (nosym_str == NULL)
14767c478bd9Sstevel@tonic-gate 			nosym_str = MSG_INTL(MSG_GEN_NOSYM);
14775aefb655Srie 		eprintf(LIST(clmp), ERR_FATAL, nosym_str, name);
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807247f888Srie 	load_completion(llmp);
14817c478bd9Sstevel@tonic-gate 	return (error);
14827c478bd9Sstevel@tonic-gate }
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate /*
14857c478bd9Sstevel@tonic-gate  * Argument checking for dlsym.  Only called via external entry.
14867c478bd9Sstevel@tonic-gate  */
14877c478bd9Sstevel@tonic-gate static void *
dlsym_check(void * handle,const char * name,Rt_map * clmp,Rt_map ** dlmp)14887c478bd9Sstevel@tonic-gate dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	/*
14917c478bd9Sstevel@tonic-gate 	 * Verify the arguments.
14927c478bd9Sstevel@tonic-gate 	 */
1493b4059b01SRod Evans 	if (name == NULL) {
14945aefb655Srie 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM));
1495b4059b01SRod Evans 		return (NULL);
14967c478bd9Sstevel@tonic-gate 	}
14977c478bd9Sstevel@tonic-gate 	if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) &&
14987c478bd9Sstevel@tonic-gate 	    (handle != RTLD_SELF) && (handle != RTLD_PROBE) &&
14997c478bd9Sstevel@tonic-gate 	    (hdl_validate((Grp_hdl *)handle) == 0)) {
1500b4059b01SRod Evans 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
1501b4059b01SRod Evans 		    EC_NATPTR(handle));
1502b4059b01SRod Evans 		return (NULL);
15037c478bd9Sstevel@tonic-gate 	}
15047c478bd9Sstevel@tonic-gate 	return (dlsym_intn(handle, name, clmp, dlmp));
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate 
15087257d1b4Sraf #pragma weak _dlsym = dlsym
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate /*
15117c478bd9Sstevel@tonic-gate  * External entry for dlsym().  On success, returns the address of the specified
15127c478bd9Sstevel@tonic-gate  * symbol.  On error returns a null.
15137c478bd9Sstevel@tonic-gate  */
15147c478bd9Sstevel@tonic-gate void *
dlsym(void * handle,const char * name)15157257d1b4Sraf dlsym(void *handle, const char *name)
15167c478bd9Sstevel@tonic-gate {
15175aefb655Srie 	int	entry;
1518b4059b01SRod Evans 	Rt_map	*clmp, *dlmp = NULL;
15195aefb655Srie 	void	*addr;
15207c478bd9Sstevel@tonic-gate 
15218cd45542Sraf 	entry = enter(0);
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	addr = dlsym_check(handle, name, clmp, &dlmp);
15267c478bd9Sstevel@tonic-gate 
152756deab07SRod Evans 	if (entry) {
152856deab07SRod Evans 		if (dlmp)
152956deab07SRod Evans 			is_dep_init(dlmp, clmp);
15308cd45542Sraf 		leave(LIST(clmp), 0);
153156deab07SRod Evans 	}
15327c478bd9Sstevel@tonic-gate 	return (addr);
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate /*
15367c478bd9Sstevel@tonic-gate  * Core dladdr activity.
15377c478bd9Sstevel@tonic-gate  */
15387c478bd9Sstevel@tonic-gate static void
dladdr_core(Rt_map * almp,void * addr,Dl_info_t * dlip,void ** info,int flags)153998c080d5SRod Evans dladdr_core(Rt_map *almp, void *addr, Dl_info_t *dlip, void **info, int flags)
15407c478bd9Sstevel@tonic-gate {
15417c478bd9Sstevel@tonic-gate 	/*
15427c478bd9Sstevel@tonic-gate 	 * Set up generic information and any defaults.
15437c478bd9Sstevel@tonic-gate 	 */
154498c080d5SRod Evans 	dlip->dli_fname = PATHNAME(almp);
15457c478bd9Sstevel@tonic-gate 
154698c080d5SRod Evans 	dlip->dli_fbase = (void *)ADDR(almp);
1547b4059b01SRod Evans 	dlip->dli_sname = NULL;
1548b4059b01SRod Evans 	dlip->dli_saddr = NULL;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	/*
15517c478bd9Sstevel@tonic-gate 	 * Determine the nearest symbol to this address.
15527c478bd9Sstevel@tonic-gate 	 */
155398c080d5SRod Evans 	LM_DLADDR(almp)((ulong_t)addr, almp, dlip, info, flags);
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate 
15567257d1b4Sraf #pragma weak _dladdr = dladdr
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate /*
15597c478bd9Sstevel@tonic-gate  * External entry for dladdr(3dl) and dladdr1(3dl).  Returns an information
15607c478bd9Sstevel@tonic-gate  * structure that reflects the symbol closest to the address specified.
15617c478bd9Sstevel@tonic-gate  */
15627c478bd9Sstevel@tonic-gate int
dladdr(void * addr,Dl_info_t * dlip)1563959ee943SRod Evans dladdr(void *addr, Dl_info_t *dlip)
15647c478bd9Sstevel@tonic-gate {
156502938ba2SRod Evans 	int	entry, ret;
156698c080d5SRod Evans 	Rt_map	*clmp, *almp;
156702938ba2SRod Evans 	Lm_list	*clml;
15687c478bd9Sstevel@tonic-gate 
15698cd45542Sraf 	entry = enter(0);
15707c478bd9Sstevel@tonic-gate 
157198c080d5SRod Evans 	clmp = _caller(caller(), CL_EXECDEF);
157202938ba2SRod Evans 	clml = LIST(clmp);
157398c080d5SRod Evans 
157498c080d5SRod Evans 	DBG_CALL(Dbg_dl_dladdr(clmp, addr));
157598c080d5SRod Evans 
15767c478bd9Sstevel@tonic-gate 	/*
15777c478bd9Sstevel@tonic-gate 	 * Use our calling technique to determine what object is associated
15787c478bd9Sstevel@tonic-gate 	 * with the supplied address.  If a caller can't be determined,
15797c478bd9Sstevel@tonic-gate 	 * indicate the failure.
15807c478bd9Sstevel@tonic-gate 	 */
158198c080d5SRod Evans 	if ((almp = _caller(addr, CL_NONE)) == NULL) {
158202938ba2SRod Evans 		eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
15835aefb655Srie 		    EC_NATPTR(addr));
158402938ba2SRod Evans 		ret = 0;
15857c478bd9Sstevel@tonic-gate 	} else {
158698c080d5SRod Evans 		dladdr_core(almp, addr, dlip, 0, 0);
158702938ba2SRod Evans 		ret = 1;
15887c478bd9Sstevel@tonic-gate 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	if (entry)
159102938ba2SRod Evans 		leave(clml, 0);
159202938ba2SRod Evans 	return (ret);
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate 
15957257d1b4Sraf #pragma weak _dladdr1 = dladdr1
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate int
dladdr1(void * addr,Dl_info_t * dlip,void ** info,int flags)1598959ee943SRod Evans dladdr1(void *addr, Dl_info_t *dlip, void **info, int flags)
15997c478bd9Sstevel@tonic-gate {
160098c080d5SRod Evans 	int	entry, ret = 1;
160198c080d5SRod Evans 	Rt_map	*clmp, *almp;
160298c080d5SRod Evans 	Lm_list	*clml;
160398c080d5SRod Evans 
160498c080d5SRod Evans 	entry = enter(0);
160598c080d5SRod Evans 
160698c080d5SRod Evans 	clmp = _caller(caller(), CL_EXECDEF);
160798c080d5SRod Evans 	clml = LIST(clmp);
160898c080d5SRod Evans 
160998c080d5SRod Evans 	DBG_CALL(Dbg_dl_dladdr(clmp, addr));
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	/*
16127c478bd9Sstevel@tonic-gate 	 * Validate any flags.
16137c478bd9Sstevel@tonic-gate 	 */
16147c478bd9Sstevel@tonic-gate 	if (flags) {
16157c478bd9Sstevel@tonic-gate 		int	request;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 		if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) &&
16187c478bd9Sstevel@tonic-gate 		    (request != RTLD_DL_LINKMAP)) {
161998c080d5SRod Evans 			eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS),
16205aefb655Srie 			    flags);
162198c080d5SRod Evans 			ret = 0;
162298c080d5SRod Evans 
162398c080d5SRod Evans 		} else if (info == NULL) {
162498c080d5SRod Evans 			eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO),
162598c080d5SRod Evans 			    flags);
162698c080d5SRod Evans 			ret = 0;
16277c478bd9Sstevel@tonic-gate 		}
16287c478bd9Sstevel@tonic-gate 	}
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	/*
16317c478bd9Sstevel@tonic-gate 	 * Use our calling technique to determine what object is associated
16327c478bd9Sstevel@tonic-gate 	 * with the supplied address.  If a caller can't be determined,
16337c478bd9Sstevel@tonic-gate 	 * indicate the failure.
16347c478bd9Sstevel@tonic-gate 	 */
163598c080d5SRod Evans 	if (ret) {
163698c080d5SRod Evans 		if ((almp = _caller(addr, CL_NONE)) == NULL) {
163798c080d5SRod Evans 			eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
163898c080d5SRod Evans 			    EC_NATPTR(addr));
163998c080d5SRod Evans 			ret = 0;
164098c080d5SRod Evans 		} else
164198c080d5SRod Evans 			dladdr_core(almp, addr, dlip, info, flags);
16427c478bd9Sstevel@tonic-gate 	}
16435aefb655Srie 
16447c478bd9Sstevel@tonic-gate 	if (entry)
164598c080d5SRod Evans 		leave(clml, 0);
164698c080d5SRod Evans 	return (ret);
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate /*
16507c478bd9Sstevel@tonic-gate  * Core dldump activity.
16517c478bd9Sstevel@tonic-gate  */
16527c478bd9Sstevel@tonic-gate static int
dldump_core(Rt_map * clmp,Rt_map * lmp,const char * ipath,const char * opath,int flags)165398c080d5SRod Evans dldump_core(Rt_map *clmp, Rt_map *lmp, const char *ipath, const char *opath,
165498c080d5SRod Evans     int flags)
16557c478bd9Sstevel@tonic-gate {
165698c080d5SRod Evans 	Lm_list	*lml = LIST(clmp);
16575aefb655Srie 	Addr	addr = 0;
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate 	/*
16607c478bd9Sstevel@tonic-gate 	 * Verify any arguments first.
16617c478bd9Sstevel@tonic-gate 	 */
166298c080d5SRod Evans 	if ((opath == NULL) || (opath[0] == '\0') ||
166398c080d5SRod Evans 	    ((lmp == NULL) && (ipath[0] == '\0'))) {
16645aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
16657c478bd9Sstevel@tonic-gate 		return (1);
16667c478bd9Sstevel@tonic-gate 	}
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	/*
16695aefb655Srie 	 * If an input file is specified make sure its one of our dependencies
16705aefb655Srie 	 * on the main link-map list.  Note, this has really all evolved for
16715aefb655Srie 	 * crle(), which uses libcrle.so on an alternative link-map to trigger
16725aefb655Srie 	 * dumping objects from the main link-map list.   If we ever want to
16735aefb655Srie 	 * dump objects from alternative link-maps, this model is going to
16745aefb655Srie 	 * have to be revisited.
16757c478bd9Sstevel@tonic-gate 	 */
167698c080d5SRod Evans 	if (lmp == NULL) {
1677b4059b01SRod Evans 		if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == NULL) {
16785aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE),
16795aefb655Srie 			    ipath);
16807c478bd9Sstevel@tonic-gate 			return (1);
16817c478bd9Sstevel@tonic-gate 		}
16827c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_ALTER) {
16835aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath);
16847c478bd9Sstevel@tonic-gate 			return (1);
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_NODUMP) {
16875aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP),
16885aefb655Srie 			    ipath);
16897c478bd9Sstevel@tonic-gate 			return (1);
16907c478bd9Sstevel@tonic-gate 		}
169198c080d5SRod Evans 	}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	/*
16947c478bd9Sstevel@tonic-gate 	 * If the object being dump'ed isn't fixed identify its mapping.
16957c478bd9Sstevel@tonic-gate 	 */
16967c478bd9Sstevel@tonic-gate 	if (!(FLAGS(lmp) & FLG_RT_FIXED))
16977c478bd9Sstevel@tonic-gate 		addr = ADDR(lmp);
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * As rt_dldump() will effectively lazy load the necessary support
17017c478bd9Sstevel@tonic-gate 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
17027c478bd9Sstevel@tonic-gate 	 */
17037c478bd9Sstevel@tonic-gate 	if (elf_rtld_load() == 0)
17047c478bd9Sstevel@tonic-gate 		return (0);
17057c478bd9Sstevel@tonic-gate 
17067c478bd9Sstevel@tonic-gate 	/*
17077c478bd9Sstevel@tonic-gate 	 * Dump the required image.
17087c478bd9Sstevel@tonic-gate 	 */
17097c478bd9Sstevel@tonic-gate 	return (rt_dldump(lmp, opath, flags, addr));
17107c478bd9Sstevel@tonic-gate }
17117c478bd9Sstevel@tonic-gate 
17127257d1b4Sraf #pragma weak _dldump = dldump
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate /*
17155aefb655Srie  * External entry for dldump(3c).  Returns 0 on success, non-zero otherwise.
17167c478bd9Sstevel@tonic-gate  */
17177c478bd9Sstevel@tonic-gate int
dldump(const char * ipath,const char * opath,int flags)17187257d1b4Sraf dldump(const char *ipath, const char *opath, int flags)
17197c478bd9Sstevel@tonic-gate {
17205aefb655Srie 	int	error, entry;
172198c080d5SRod Evans 	Rt_map	*clmp, *lmp;
17227c478bd9Sstevel@tonic-gate 
17238cd45542Sraf 	entry = enter(0);
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
17267c478bd9Sstevel@tonic-gate 
172798c080d5SRod Evans 	if (ipath) {
172898c080d5SRod Evans 		lmp = NULL;
172998c080d5SRod Evans 	} else {
173098c080d5SRod Evans 		lmp = lml_main.lm_head;
173198c080d5SRod Evans 		ipath = NAME(lmp);
173298c080d5SRod Evans 	}
173398c080d5SRod Evans 
173498c080d5SRod Evans 	DBG_CALL(Dbg_dl_dldump(clmp, ipath, opath, flags));
173598c080d5SRod Evans 
173698c080d5SRod Evans 	error = dldump_core(clmp, lmp, ipath, opath, flags);
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	if (entry)
17398cd45542Sraf 		leave(LIST(clmp), 0);
17407c478bd9Sstevel@tonic-gate 	return (error);
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate /*
17447c478bd9Sstevel@tonic-gate  * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by
17457c478bd9Sstevel@tonic-gate  * the rtld_db and dlmopen() interfaces.  It checks to see if the Link_map is
17467c478bd9Sstevel@tonic-gate  * one of the primary ones and if so returns it's special token:
17477c478bd9Sstevel@tonic-gate  *		LM_ID_BASE
17487c478bd9Sstevel@tonic-gate  *		LM_ID_LDSO
17497c478bd9Sstevel@tonic-gate  *
17507c478bd9Sstevel@tonic-gate  * If it's not one of the primary link_map id's it will instead returns a
17517c478bd9Sstevel@tonic-gate  * pointer to the Lm_list structure which uniquely identifies the Link_map.
17527c478bd9Sstevel@tonic-gate  */
17537c478bd9Sstevel@tonic-gate Lmid_t
get_linkmap_id(Lm_list * lml)17547c478bd9Sstevel@tonic-gate get_linkmap_id(Lm_list *lml)
17557c478bd9Sstevel@tonic-gate {
17567c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_BASELM)
17577c478bd9Sstevel@tonic-gate 		return (LM_ID_BASE);
17587c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_RTLDLM)
17597c478bd9Sstevel@tonic-gate 		return (LM_ID_LDSO);
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	return ((Lmid_t)lml);
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate 
1764f441771bSRod Evans /*
1765f441771bSRod Evans  * Set a new deferred dependency name.
1766f441771bSRod Evans  */
1767f441771bSRod Evans static int
set_def_need(Lm_list * lml,Dyninfo * dyip,const char * name)1768f441771bSRod Evans set_def_need(Lm_list *lml, Dyninfo *dyip, const char *name)
1769f441771bSRod Evans {
1770f441771bSRod Evans 	/*
1771f441771bSRod Evans 	 * If this dependency has already been established, then this dlinfo()
1772f441771bSRod Evans 	 * call is too late.
1773f441771bSRod Evans 	 */
1774f441771bSRod Evans 	if (dyip->di_info) {
1775f441771bSRod Evans 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_DEPLOADED),
1776f441771bSRod Evans 		    dyip->di_name);
1777f441771bSRod Evans 		return (-1);
1778f441771bSRod Evans 	}
1779f441771bSRod Evans 
1780f441771bSRod Evans 	/*
1781f441771bSRod Evans 	 * Assign the new dependency name.
1782f441771bSRod Evans 	 */
1783f441771bSRod Evans 	DBG_CALL(Dbg_file_deferred(lml, dyip->di_name, name));
1784f441771bSRod Evans 	dyip->di_flags |= FLG_DI_DEF_DONE;
1785f441771bSRod Evans 	dyip->di_name = name;
1786f441771bSRod Evans 	return (0);
1787f441771bSRod Evans }
1788f441771bSRod Evans 
17897c478bd9Sstevel@tonic-gate /*
179041072f3cSrie  * Extract information for a dlopen() handle.
17917c478bd9Sstevel@tonic-gate  */
17927c478bd9Sstevel@tonic-gate static int
dlinfo_core(void * handle,int request,void * p,Rt_map * clmp)17937c478bd9Sstevel@tonic-gate dlinfo_core(void *handle, int request, void *p, Rt_map *clmp)
17947c478bd9Sstevel@tonic-gate {
179598c080d5SRod Evans 	Conv_inv_buf_t	inv_buf;
179698c080d5SRod Evans 	char		*handlename;
179798c080d5SRod Evans 	Lm_list		*lml = LIST(clmp);
179898c080d5SRod Evans 	Rt_map		*lmp = NULL;
179998c080d5SRod Evans 
180098c080d5SRod Evans 	/*
180198c080d5SRod Evans 	 * Determine whether a handle is provided.  A handle isn't needed for
180298c080d5SRod Evans 	 * all operations, but it is validated here for the initial diagnostic.
180398c080d5SRod Evans 	 */
180498c080d5SRod Evans 	if (handle == RTLD_SELF) {
180598c080d5SRod Evans 		lmp = clmp;
180698c080d5SRod Evans 	} else {
180798c080d5SRod Evans 		Grp_hdl	*ghp = (Grp_hdl *)handle;
18087c478bd9Sstevel@tonic-gate 
180998c080d5SRod Evans 		if (hdl_validate(ghp))
181098c080d5SRod Evans 			lmp = ghp->gh_ownlmp;
181198c080d5SRod Evans 	}
181298c080d5SRod Evans 	if (lmp) {
181398c080d5SRod Evans 		handlename = NAME(lmp);
181498c080d5SRod Evans 	} else {
181598c080d5SRod Evans 		(void) conv_invalid_val(&inv_buf, EC_NATPTR(handle), 0);
181698c080d5SRod Evans 		handlename = inv_buf.buf;
181798c080d5SRod Evans 	}
181898c080d5SRod Evans 
181998c080d5SRod Evans 	DBG_CALL(Dbg_dl_dlinfo(clmp, handlename, request, p));
182098c080d5SRod Evans 
182198c080d5SRod Evans 	/*
182298c080d5SRod Evans 	 * Validate the request and return buffer.
182398c080d5SRod Evans 	 */
1824b4059b01SRod Evans 	if ((request > RTLD_DI_MAX) || (p == NULL)) {
18255aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL));
18267c478bd9Sstevel@tonic-gate 		return (-1);
18277c478bd9Sstevel@tonic-gate 	}
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate 	/*
18307c478bd9Sstevel@tonic-gate 	 * Return configuration cache name and address.
18317c478bd9Sstevel@tonic-gate 	 */
18327c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_CONFIGADDR) {
1833959ee943SRod Evans 		Dl_info_t	*dlip = (Dl_info_t *)p;
18347c478bd9Sstevel@tonic-gate 
1835b4059b01SRod Evans 		if ((config->c_name == NULL) || (config->c_bgn == 0) ||
18367c478bd9Sstevel@tonic-gate 		    (config->c_end == 0)) {
18375aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG));
18387c478bd9Sstevel@tonic-gate 			return (-1);
18397c478bd9Sstevel@tonic-gate 		}
18407c478bd9Sstevel@tonic-gate 		dlip->dli_fname = config->c_name;
18417c478bd9Sstevel@tonic-gate 		dlip->dli_fbase = (void *)config->c_bgn;
18427c478bd9Sstevel@tonic-gate 		return (0);
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	/*
18467c478bd9Sstevel@tonic-gate 	 * Return profiled object name (used by ldprof audit library).
18477c478bd9Sstevel@tonic-gate 	 */
18487c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_PROFILENAME) {
184956deab07SRod Evans 		if (profile_name == NULL) {
18505aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME));
18517c478bd9Sstevel@tonic-gate 			return (-1);
18527c478bd9Sstevel@tonic-gate 		}
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 		*(const char **)p = profile_name;
18557c478bd9Sstevel@tonic-gate 		return (0);
18567c478bd9Sstevel@tonic-gate 	}
18577c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_PROFILEOUT) {
18587c478bd9Sstevel@tonic-gate 		/*
18597c478bd9Sstevel@tonic-gate 		 * If a profile destination directory hasn't been specified
18607c478bd9Sstevel@tonic-gate 		 * provide a default.
18617c478bd9Sstevel@tonic-gate 		 */
186256deab07SRod Evans 		if (profile_out == NULL)
18637c478bd9Sstevel@tonic-gate 			profile_out = MSG_ORIG(MSG_PTH_VARTMP);
18647c478bd9Sstevel@tonic-gate 
18657c478bd9Sstevel@tonic-gate 		*(const char **)p = profile_out;
18667c478bd9Sstevel@tonic-gate 		return (0);
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	/*
18707c478bd9Sstevel@tonic-gate 	 * Obtain or establish a termination signal.
18717c478bd9Sstevel@tonic-gate 	 */
18727c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_GETSIGNAL) {
18737c478bd9Sstevel@tonic-gate 		*(int *)p = killsig;
18747c478bd9Sstevel@tonic-gate 		return (0);
18757c478bd9Sstevel@tonic-gate 	}
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_SETSIGNAL) {
18787c478bd9Sstevel@tonic-gate 		sigset_t	set;
18797c478bd9Sstevel@tonic-gate 		int		sig = *(int *)p;
18807c478bd9Sstevel@tonic-gate 
18817c478bd9Sstevel@tonic-gate 		/*
18827c478bd9Sstevel@tonic-gate 		 * Determine whether the signal is in range.
18837c478bd9Sstevel@tonic-gate 		 */
18847c478bd9Sstevel@tonic-gate 		(void) sigfillset(&set);
18857c478bd9Sstevel@tonic-gate 		if (sigismember(&set, sig) != 1) {
18865aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig);
18877c478bd9Sstevel@tonic-gate 			return (-1);
18887c478bd9Sstevel@tonic-gate 		}
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate 		killsig = sig;
18917c478bd9Sstevel@tonic-gate 		return (0);
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	/*
18957c478bd9Sstevel@tonic-gate 	 * For any other request a link-map is required.  Verify the handle.
18967c478bd9Sstevel@tonic-gate 	 */
189798c080d5SRod Evans 	if (lmp == NULL) {
189898c080d5SRod Evans 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
189998c080d5SRod Evans 		    EC_NATPTR(handle));
190098c080d5SRod Evans 		return (-1);
19017c478bd9Sstevel@tonic-gate 	}
19027c478bd9Sstevel@tonic-gate 
190341072f3cSrie 	/*
190441072f3cSrie 	 * Obtain the process arguments, environment and auxv.  Note, as the
190541072f3cSrie 	 * environment can be modified by the user (putenv(3c)), reinitialize
190641072f3cSrie 	 * the environment pointer on each request.
190741072f3cSrie 	 */
190841072f3cSrie 	if (request == RTLD_DI_ARGSINFO) {
1909959ee943SRod Evans 		Dl_argsinfo_t	*aip = (Dl_argsinfo_t *)p;
191041072f3cSrie 		Lm_list		*lml = LIST(lmp);
191141072f3cSrie 
191241072f3cSrie 		*aip = argsinfo;
191341072f3cSrie 		if (lml->lm_flags & LML_FLG_ENVIRON)
191441072f3cSrie 			aip->dla_envp = *(lml->lm_environ);
191541072f3cSrie 
191641072f3cSrie 		return (0);
191741072f3cSrie 	}
191841072f3cSrie 
19197c478bd9Sstevel@tonic-gate 	/*
19207c478bd9Sstevel@tonic-gate 	 * Return Lmid_t of the Link-Map list that the specified object is
19217c478bd9Sstevel@tonic-gate 	 * loaded on.
19227c478bd9Sstevel@tonic-gate 	 */
19237c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_LMID) {
19247c478bd9Sstevel@tonic-gate 		*(Lmid_t *)p = get_linkmap_id(LIST(lmp));
19257c478bd9Sstevel@tonic-gate 		return (0);
19267c478bd9Sstevel@tonic-gate 	}
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	/*
19297c478bd9Sstevel@tonic-gate 	 * Return a pointer to the Link-Map structure associated with the
19307c478bd9Sstevel@tonic-gate 	 * specified object.
19317c478bd9Sstevel@tonic-gate 	 */
19327c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_LINKMAP) {
19337c478bd9Sstevel@tonic-gate 		*(Link_map **)p = (Link_map *)lmp;
19347c478bd9Sstevel@tonic-gate 		return (0);
19357c478bd9Sstevel@tonic-gate 	}
19367c478bd9Sstevel@tonic-gate 
19377c478bd9Sstevel@tonic-gate 	/*
19387c478bd9Sstevel@tonic-gate 	 * Return search path information, or the size of the buffer required
19397c478bd9Sstevel@tonic-gate 	 * to store the information.
19407c478bd9Sstevel@tonic-gate 	 */
19417c478bd9Sstevel@tonic-gate 	if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) {
194256deab07SRod Evans 		Spath_desc	sd = { search_rules, NULL, 0 };
194356deab07SRod Evans 		Pdesc		*pdp;
1944959ee943SRod Evans 		Dl_serinfo_t	*info;
1945959ee943SRod Evans 		Dl_serpath_t	*path;
19467c478bd9Sstevel@tonic-gate 		char		*strs;
1947959ee943SRod Evans 		size_t		size = sizeof (Dl_serinfo_t);
19487c478bd9Sstevel@tonic-gate 		uint_t		cnt = 0;
19497c478bd9Sstevel@tonic-gate 
1950959ee943SRod Evans 		info = (Dl_serinfo_t *)p;
19517c478bd9Sstevel@tonic-gate 		path = &info->dls_serpath[0];
19527c478bd9Sstevel@tonic-gate 		strs = (char *)&info->dls_serpath[info->dls_cnt];
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 		/*
19557c478bd9Sstevel@tonic-gate 		 * Traverse search path entries for this object.
19567c478bd9Sstevel@tonic-gate 		 */
1957b4059b01SRod Evans 		while ((pdp = get_next_dir(&sd, lmp, 0)) != NULL) {
19587c478bd9Sstevel@tonic-gate 			size_t	_size;
19597c478bd9Sstevel@tonic-gate 
1960b4059b01SRod Evans 			if (pdp->pd_pname == NULL)
19617c478bd9Sstevel@tonic-gate 				continue;
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 			/*
19647c478bd9Sstevel@tonic-gate 			 * If configuration information exists, it's possible
19657c478bd9Sstevel@tonic-gate 			 * this path has been identified as non-existent, if so
19667c478bd9Sstevel@tonic-gate 			 * ignore it.
19677c478bd9Sstevel@tonic-gate 			 */
196856deab07SRod Evans 			if (pdp->pd_info) {
196956deab07SRod Evans 				Rtc_obj	*dobj = (Rtc_obj *)pdp->pd_info;
19707c478bd9Sstevel@tonic-gate 				if (dobj->co_flags & RTC_OBJ_NOEXIST)
19717c478bd9Sstevel@tonic-gate 					continue;
19727c478bd9Sstevel@tonic-gate 			}
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 			/*
19757c478bd9Sstevel@tonic-gate 			 * Keep track of search path count and total info size.
19767c478bd9Sstevel@tonic-gate 			 */
19777c478bd9Sstevel@tonic-gate 			if (cnt++)
1978959ee943SRod Evans 				size += sizeof (Dl_serpath_t);
197956deab07SRod Evans 			_size = pdp->pd_plen + 1;
19807c478bd9Sstevel@tonic-gate 			size += _size;
19817c478bd9Sstevel@tonic-gate 
19827c478bd9Sstevel@tonic-gate 			if (request == RTLD_DI_SERINFOSIZE)
19837c478bd9Sstevel@tonic-gate 				continue;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 			/*
19867c478bd9Sstevel@tonic-gate 			 * If we're filling in search path information, confirm
19877c478bd9Sstevel@tonic-gate 			 * there's sufficient space.
19887c478bd9Sstevel@tonic-gate 			 */
19897c478bd9Sstevel@tonic-gate 			if (size > info->dls_size) {
19905aefb655Srie 				eprintf(lml, ERR_FATAL,
19915aefb655Srie 				    MSG_INTL(MSG_ARG_SERSIZE),
19927c478bd9Sstevel@tonic-gate 				    EC_OFF(info->dls_size));
19937c478bd9Sstevel@tonic-gate 				return (-1);
19947c478bd9Sstevel@tonic-gate 			}
19957c478bd9Sstevel@tonic-gate 			if (cnt > info->dls_cnt) {
19965aefb655Srie 				eprintf(lml, ERR_FATAL,
19975aefb655Srie 				    MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt);
19987c478bd9Sstevel@tonic-gate 				return (-1);
19997c478bd9Sstevel@tonic-gate 			}
20007c478bd9Sstevel@tonic-gate 
20017c478bd9Sstevel@tonic-gate 			/*
20027c478bd9Sstevel@tonic-gate 			 * Append the path to the information buffer.
20037c478bd9Sstevel@tonic-gate 			 */
200456deab07SRod Evans 			(void) strcpy(strs, pdp->pd_pname);
20057c478bd9Sstevel@tonic-gate 			path->dls_name = strs;
2006f441771bSRod Evans 			path->dls_flags = (pdp->pd_flags & LA_SER_MASK);
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 			strs = strs + _size;
20097c478bd9Sstevel@tonic-gate 			path++;
20107c478bd9Sstevel@tonic-gate 		}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 		/*
20137c478bd9Sstevel@tonic-gate 		 * If we're here to size the search buffer fill it in.
20147c478bd9Sstevel@tonic-gate 		 */
20157c478bd9Sstevel@tonic-gate 		if (request == RTLD_DI_SERINFOSIZE) {
20167c478bd9Sstevel@tonic-gate 			info->dls_size = size;
20177c478bd9Sstevel@tonic-gate 			info->dls_cnt = cnt;
20187c478bd9Sstevel@tonic-gate 		}
2019959ee943SRod Evans 
2020959ee943SRod Evans 		return (0);
20217c478bd9Sstevel@tonic-gate 	}
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate 	/*
20247c478bd9Sstevel@tonic-gate 	 * Return the origin of the object associated with this link-map.
20257c478bd9Sstevel@tonic-gate 	 * Basically return the dirname(1) of the objects fullpath.
20267c478bd9Sstevel@tonic-gate 	 */
20277c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_ORIGIN) {
202841072f3cSrie 		char	*str = (char *)p;
20297c478bd9Sstevel@tonic-gate 
203041072f3cSrie 		(void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp));
203141072f3cSrie 		str += DIRSZ(lmp);
20327c478bd9Sstevel@tonic-gate 		*str = '\0';
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 		return (0);
20357c478bd9Sstevel@tonic-gate 	}
20367c478bd9Sstevel@tonic-gate 
2037959ee943SRod Evans 	/*
2038959ee943SRod Evans 	 * Return the number of object mappings, or the mapping information for
2039959ee943SRod Evans 	 * this object.
2040959ee943SRod Evans 	 */
2041959ee943SRod Evans 	if (request == RTLD_DI_MMAPCNT) {
2042959ee943SRod Evans 		uint_t	*cnt = (uint_t *)p;
2043959ee943SRod Evans 
2044959ee943SRod Evans 		*cnt = MMAPCNT(lmp);
2045959ee943SRod Evans 		return (0);
2046959ee943SRod Evans 	}
2047959ee943SRod Evans 	if (request == RTLD_DI_MMAPS) {
2048959ee943SRod Evans 		Dl_mapinfo_t	*mip = (Dl_mapinfo_t *)p;
2049959ee943SRod Evans 
2050959ee943SRod Evans 		if (mip->dlm_acnt && mip->dlm_maps) {
2051959ee943SRod Evans 			uint_t	cnt = 0;
2052959ee943SRod Evans 
2053959ee943SRod Evans 			while ((cnt < mip->dlm_acnt) && (cnt < MMAPCNT(lmp))) {
2054959ee943SRod Evans 				mip->dlm_maps[cnt] = MMAPS(lmp)[cnt];
2055959ee943SRod Evans 				cnt++;
2056959ee943SRod Evans 			}
2057959ee943SRod Evans 			mip->dlm_rcnt = cnt;
2058959ee943SRod Evans 		}
2059959ee943SRod Evans 		return (0);
2060959ee943SRod Evans 	}
2061959ee943SRod Evans 
2062f441771bSRod Evans 	/*
2063f441771bSRod Evans 	 * Assign a new dependency name to a deferred dependency.
2064f441771bSRod Evans 	 */
2065f441771bSRod Evans 	if ((request == RTLD_DI_DEFERRED) ||
2066f441771bSRod Evans 	    (request == RTLD_DI_DEFERRED_SYM)) {
2067f441771bSRod Evans 		Dl_definfo_t	*dfip = (Dl_definfo_t *)p;
2068f441771bSRod Evans 		Dyninfo		*dyip;
2069f441771bSRod Evans 		const char	*dname, *rname;
2070f441771bSRod Evans 
2071f441771bSRod Evans 		/*
2072f441771bSRod Evans 		 * Verify the names.
2073f441771bSRod Evans 		 */
2074f441771bSRod Evans 		if ((dfip->dld_refname == NULL) ||
2075f441771bSRod Evans 		    (dfip->dld_depname == NULL)) {
2076f441771bSRod Evans 			eprintf(LIST(clmp), ERR_FATAL,
2077f441771bSRod Evans 			    MSG_INTL(MSG_ARG_ILLNAME));
2078f441771bSRod Evans 			return (-1);
2079f441771bSRod Evans 		}
2080f441771bSRod Evans 
2081f441771bSRod Evans 		dname = dfip->dld_depname;
2082f441771bSRod Evans 		rname = dfip->dld_refname;
2083f441771bSRod Evans 
2084f441771bSRod Evans 		/*
2085f441771bSRod Evans 		 * A deferred dependency can be determined by referencing a
2086f441771bSRod Evans 		 * symbol family member that is associated to the dependency,
2087f441771bSRod Evans 		 * or by looking for the dependency by its name.
2088f441771bSRod Evans 		 */
2089f441771bSRod Evans 		if (request == RTLD_DI_DEFERRED_SYM) {
2090f441771bSRod Evans 			Slookup		sl;
2091f441771bSRod Evans 			Sresult		sr;
2092f441771bSRod Evans 			uint_t		binfo;
2093f441771bSRod Evans 			Syminfo		*sip;
2094f441771bSRod Evans 
2095f441771bSRod Evans 			/*
2096f441771bSRod Evans 			 * Lookup the symbol in the associated object.
2097f441771bSRod Evans 			 */
2098f441771bSRod Evans 			SLOOKUP_INIT(sl, rname, lmp, lmp, ld_entry_cnt,
2099f441771bSRod Evans 			    elf_hash(rname), 0, 0, 0, LKUP_SYMNDX);
2100f441771bSRod Evans 			SRESULT_INIT(sr, rname);
2101f441771bSRod Evans 			if (sym_lookup_in_caller(clmp, &sl, &sr,
2102f441771bSRod Evans 			    &binfo) == NULL) {
2103f441771bSRod Evans 				eprintf(LIST(clmp), ERR_FATAL,
2104f441771bSRod Evans 				    MSG_INTL(MSG_DEF_NOSYMFOUND), rname);
2105f441771bSRod Evans 				return (-1);
2106f441771bSRod Evans 			}
2107f441771bSRod Evans 
2108f441771bSRod Evans 			/*
2109f441771bSRod Evans 			 * Use the symbols index to reference the Syminfo entry
2110f441771bSRod Evans 			 * and thus find the associated dependency.
2111f441771bSRod Evans 			 */
2112f441771bSRod Evans 			if (sl.sl_rsymndx && ((sip = SYMINFO(clmp)) != NULL)) {
2113f441771bSRod Evans 				/* LINTED */
2114f441771bSRod Evans 				sip = (Syminfo *)((char *)sip +
2115f441771bSRod Evans 				    (sl.sl_rsymndx * SYMINENT(lmp)));
2116f441771bSRod Evans 
2117f441771bSRod Evans 				if ((sip->si_flags & SYMINFO_FLG_DEFERRED) &&
2118f441771bSRod Evans 				    (sip->si_boundto < SYMINFO_BT_LOWRESERVE) &&
2119f441771bSRod Evans 				    ((dyip = DYNINFO(lmp)) != NULL)) {
2120f441771bSRod Evans 					dyip += sip->si_boundto;
2121f441771bSRod Evans 
2122f441771bSRod Evans 					if (!(dyip->di_flags & FLG_DI_IGNORE))
2123f441771bSRod Evans 						return (set_def_need(lml,
2124f441771bSRod Evans 						    dyip, dname));
2125f441771bSRod Evans 				}
2126f441771bSRod Evans 			}
2127f441771bSRod Evans 
2128f441771bSRod Evans 			/*
2129f441771bSRod Evans 			 * No deferred symbol found.
2130f441771bSRod Evans 			 */
2131f441771bSRod Evans 			eprintf(LIST(clmp), ERR_FATAL,
2132f441771bSRod Evans 			    MSG_INTL(MSG_DEF_NOSYMFOUND), rname);
2133f441771bSRod Evans 			return (-1);
2134f441771bSRod Evans 
2135f441771bSRod Evans 		} else {
2136f441771bSRod Evans 			Dyn	*dyn;
2137f441771bSRod Evans 
2138f441771bSRod Evans 			/*
2139f441771bSRod Evans 			 * Using the target objects dependency information, find
2140f441771bSRod Evans 			 * the associated deferred dependency.
2141f441771bSRod Evans 			 */
2142f441771bSRod Evans 			for (dyn = DYN(lmp), dyip = DYNINFO(lmp);
2143f441771bSRod Evans 			    !(dyip->di_flags & FLG_DI_IGNORE); dyn++, dyip++) {
2144f441771bSRod Evans 				const char	*oname;
2145f441771bSRod Evans 
2146f441771bSRod Evans 				if ((dyip->di_flags & FLG_DI_DEFERRED) == 0)
2147f441771bSRod Evans 					continue;
2148f441771bSRod Evans 
2149f441771bSRod Evans 				if (strcmp(rname, dyip->di_name) == 0)
2150f441771bSRod Evans 					return (set_def_need(lml, dyip, dname));
2151f441771bSRod Evans 
2152f441771bSRod Evans 				/*
2153f441771bSRod Evans 				 * If this dependency name has been changed by
2154f441771bSRod Evans 				 * a previous dlinfo(), check the original
2155f441771bSRod Evans 				 * dynamic entry string.  The user might be
2156f441771bSRod Evans 				 * attempting to re-change an entry using the
2157f441771bSRod Evans 				 * original name as the reference.
2158f441771bSRod Evans 				 */
2159f441771bSRod Evans 				if ((dyip->di_flags & FLG_DI_DEF_DONE) == 0)
2160f441771bSRod Evans 					continue;
2161f441771bSRod Evans 
2162f441771bSRod Evans 				oname = STRTAB(lmp) + dyn->d_un.d_val;
2163f441771bSRod Evans 				if (strcmp(rname, oname) == 0)
2164f441771bSRod Evans 					return (set_def_need(lml, dyip, dname));
2165f441771bSRod Evans 			}
2166f441771bSRod Evans 
2167f441771bSRod Evans 			/*
2168f441771bSRod Evans 			 * No deferred dependency found.
2169f441771bSRod Evans 			 */
2170f441771bSRod Evans 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_NODEPFOUND),
2171f441771bSRod Evans 			    rname);
2172f441771bSRod Evans 			return (-1);
2173f441771bSRod Evans 		}
2174f441771bSRod Evans 	}
21757c478bd9Sstevel@tonic-gate 	return (0);
21767c478bd9Sstevel@tonic-gate }
21777c478bd9Sstevel@tonic-gate 
21787257d1b4Sraf #pragma weak _dlinfo = dlinfo
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate /*
21817c478bd9Sstevel@tonic-gate  * External entry for dlinfo(3dl).
21827c478bd9Sstevel@tonic-gate  */
21837c478bd9Sstevel@tonic-gate int
dlinfo(void * handle,int request,void * p)21847257d1b4Sraf dlinfo(void *handle, int request, void *p)
21857c478bd9Sstevel@tonic-gate {
21867c478bd9Sstevel@tonic-gate 	int	error, entry;
21877c478bd9Sstevel@tonic-gate 	Rt_map	*clmp;
21887c478bd9Sstevel@tonic-gate 
21898cd45542Sraf 	entry = enter(0);
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 	error = dlinfo_core(handle, request, p, clmp);
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	if (entry)
21968cd45542Sraf 		leave(LIST(clmp), 0);
21977c478bd9Sstevel@tonic-gate 	return (error);
21987c478bd9Sstevel@tonic-gate }
219920272c2eSAli Bahrami 
220020272c2eSAli Bahrami /*
220120272c2eSAli Bahrami  * GNU defined function to iterate through the program headers for all
220220272c2eSAli Bahrami  * currently loaded dynamic objects. The caller supplies a callback function
220320272c2eSAli Bahrami  * which is called for each object.
220420272c2eSAli Bahrami  *
220520272c2eSAli Bahrami  * entry:
220620272c2eSAli Bahrami  *	callback - Callback function to call. The arguments to the callback
220720272c2eSAli Bahrami  *		function are:
220820272c2eSAli Bahrami  *		info - Address of dl_phdr_info structure
220920272c2eSAli Bahrami  *		size - sizeof (struct dl_phdr_info)
221020272c2eSAli Bahrami  *		data - Caller supplied value.
221120272c2eSAli Bahrami  *	data - Value supplied by caller, which is passed to callback without
221220272c2eSAli Bahrami  *		examination.
221320272c2eSAli Bahrami  *
221420272c2eSAli Bahrami  * exit:
221520272c2eSAli Bahrami  *	callback is called for each dynamic ELF object in the process address
221620272c2eSAli Bahrami  *	space, halting when a non-zero value is returned, or when the last
221720272c2eSAli Bahrami  *	object has been processed. The return value from the last call
221820272c2eSAli Bahrami  *	to callback is returned.
221920272c2eSAli Bahrami  *
222020272c2eSAli Bahrami  * note:
222120272c2eSAli Bahrami  *	The Linux implementation has added additional fields to the
222220272c2eSAli Bahrami  *	dl_phdr_info structure over time. The callback function is
222320272c2eSAli Bahrami  *	supposed to use the size field to determine which fields are
222420272c2eSAli Bahrami  *	present, and to avoid attempts to access non-existent fields.
222520272c2eSAli Bahrami  *	We have added those fields that are compatible with Solaris, and
222620272c2eSAli Bahrami  *	which are used by GNU C++ (g++) runtime exception handling support.
222720272c2eSAli Bahrami  *
222820272c2eSAli Bahrami  * note:
222920272c2eSAli Bahrami  *	We issue a callback for every ELF object mapped into the process
223020272c2eSAli Bahrami  *	address space at the time this routine is entered. These callbacks
223120272c2eSAli Bahrami  *	are arbitrary functions that can do anything, including possibly
223220272c2eSAli Bahrami  *	causing new objects to be mapped into the process, or unmapped.
223320272c2eSAli Bahrami  *	This complicates matters:
223420272c2eSAli Bahrami  *
223520272c2eSAli Bahrami  *	-	Adding new objects can cause the alists to be reallocated
223620272c2eSAli Bahrami  *		or for contents to move. This can happen explicitly via
223720272c2eSAli Bahrami  *		dlopen(), or implicitly via lazy loading. One might consider
223820272c2eSAli Bahrami  *		simply banning dlopen from a callback, but lazy loading must
223920272c2eSAli Bahrami  *		be allowed, in which case there's no reason to ban dlopen().
224020272c2eSAli Bahrami  *
224120272c2eSAli Bahrami  *	-	Removing objects can leave us holding references to freed
224220272c2eSAli Bahrami  *		memory that must not be accessed, and can cause the list
224320272c2eSAli Bahrami  *		items to move in a way that would cause us to miss reporting
224420272c2eSAli Bahrami  *		one, or double report others.
224520272c2eSAli Bahrami  *
224620272c2eSAli Bahrami  *	-	We cannot allocate memory to build a separate data structure,
224720272c2eSAli Bahrami  *		because the interface to dl_iterate_phdr() does not have a
224820272c2eSAli Bahrami  *		way to communicate allocation errors back to the caller.
224920272c2eSAli Bahrami  *		Even if we could, it would be difficult to do so efficiently.
225020272c2eSAli Bahrami  *
225120272c2eSAli Bahrami  *	-	It is possible for dl_iterate_phdr() to be called recursively
225220272c2eSAli Bahrami  *		from a callback, and there is no way for us to detect or manage
225320272c2eSAli Bahrami  *		this effectively, particularly as the user might use longjmp()
225420272c2eSAli Bahrami  *		to skip past us on return. Hence, we must be reentrant
225520272c2eSAli Bahrami  *		(stateless), further precluding the option of building a
225620272c2eSAli Bahrami  *		separate data structure.
225720272c2eSAli Bahrami  *
225820272c2eSAli Bahrami  *	Despite these constraints, we are able to traverse the link-map
225920272c2eSAli Bahrami  *	lists safely:
226020272c2eSAli Bahrami  *
226120272c2eSAli Bahrami  *	-	Once interposer (preload) objects have been processed at
226220272c2eSAli Bahrami  *		startup, we know that new objects are always placed at the
226320272c2eSAli Bahrami  *		end of the list. Hence, if we are reading a list when that
226420272c2eSAli Bahrami  *		happens, the new object will not alter the part of the list
226520272c2eSAli Bahrami  *		that we've already processed.
226620272c2eSAli Bahrami  *
226720272c2eSAli Bahrami  *	-	The alist _TRAVERSE macros recalculate the address of the
226820272c2eSAli Bahrami  *		current item from scratch on each iteration, rather than
226920272c2eSAli Bahrami  *		incrementing a pointer. Hence, alist additions that occur
227020272c2eSAli Bahrami  *		in mid-traverse will not cause confusion.
227120272c2eSAli Bahrami  *
227267d74cc3SToomas Soome  *	There is one limitation: We cannot continue operation if an object
227320272c2eSAli Bahrami  *	is removed from the process from within a callback. We detect when
227420272c2eSAli Bahrami  *	this happens and return immediately with a -1 return value.
227520272c2eSAli Bahrami  *
227620272c2eSAli Bahrami  * note:
227720272c2eSAli Bahrami  *	As currently implemented, if a callback causes an object to be loaded,
227820272c2eSAli Bahrami  *	that object may or may not be reported by the current invocation of
227920272c2eSAli Bahrami  *	dl_iterate_phdr(), based on whether or not we have already processed
228020272c2eSAli Bahrami  *	the link-map list that receives it. If we want to prevent this, it
228120272c2eSAli Bahrami  *	can be done efficiently by associating the current value of cnt_map
228220272c2eSAli Bahrami  *	with each new Rt_map entered into the system. Then this function can
228320272c2eSAli Bahrami  *	use that to detect and skip new objects that enter the system in
228420272c2eSAli Bahrami  *	mid-iteration. However, the Linux documentation is ambiguous on whether
228520272c2eSAli Bahrami  *	this is necessary, and it does not appear to matter in practice.
228620272c2eSAli Bahrami  *	We have therefore chosen not to do so at this time.
228720272c2eSAli Bahrami  */
228820272c2eSAli Bahrami int
dl_iterate_phdr(int (* callback)(struct dl_phdr_info *,size_t,void *),void * data)228920272c2eSAli Bahrami dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *),
229020272c2eSAli Bahrami     void *data)
229120272c2eSAli Bahrami {
229220272c2eSAli Bahrami 	struct dl_phdr_info	info;
229320272c2eSAli Bahrami 	u_longlong_t		l_cnt_map = cnt_map;
229420272c2eSAli Bahrami 	u_longlong_t		l_cnt_unmap = cnt_unmap;
229598c080d5SRod Evans 	Lm_list			*lml, *clml;
229620272c2eSAli Bahrami 	Lm_cntl			*lmc;
229720272c2eSAli Bahrami 	Rt_map			*lmp, *clmp;
229820272c2eSAli Bahrami 	Aliste			idx1, idx2;
229920272c2eSAli Bahrami 	Ehdr			*ehdr;
230020272c2eSAli Bahrami 	int			ret = 0;
230120272c2eSAli Bahrami 	int			entry;
230220272c2eSAli Bahrami 
230320272c2eSAli Bahrami 	entry = enter(0);
230498c080d5SRod Evans 	clmp = _caller(caller(), CL_EXECDEF);
230598c080d5SRod Evans 	clml = LIST(clmp);
230698c080d5SRod Evans 
230798c080d5SRod Evans 	DBG_CALL(Dbg_dl_iphdr_enter(clmp, cnt_map, cnt_unmap));
230820272c2eSAli Bahrami 
230920272c2eSAli Bahrami 	/* Issue a callback for each ELF object in the process */
231020272c2eSAli Bahrami 	for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) {
231120272c2eSAli Bahrami 		for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) {
231220272c2eSAli Bahrami 			for (lmp = lmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) {
231320272c2eSAli Bahrami #if defined(_sparc) && !defined(_LP64)
231420272c2eSAli Bahrami 				/*
231520272c2eSAli Bahrami 				 * On 32-bit sparc, the possibility exists that
231620272c2eSAli Bahrami 				 * this object is not ELF.
231720272c2eSAli Bahrami 				 */
231820272c2eSAli Bahrami 				if (THIS_IS_NOT_ELF(lmp))
231920272c2eSAli Bahrami 					continue;
232020272c2eSAli Bahrami #endif
232120272c2eSAli Bahrami 				/* Prepare the object information structure */
232220272c2eSAli Bahrami 				ehdr = (Ehdr *) ADDR(lmp);
232320272c2eSAli Bahrami 				info.dlpi_addr = (ehdr->e_type == ET_EXEC) ?
232420272c2eSAli Bahrami 				    0 : ADDR(lmp);
232520272c2eSAli Bahrami 				info.dlpi_name = lmp->rt_pathname;
232620272c2eSAli Bahrami 				info.dlpi_phdr = (Phdr *)
232720272c2eSAli Bahrami 				    (ADDR(lmp) + ehdr->e_phoff);
232820272c2eSAli Bahrami 				info.dlpi_phnum = ehdr->e_phnum;
232920272c2eSAli Bahrami 				info.dlpi_adds = cnt_map;
233020272c2eSAli Bahrami 				info.dlpi_subs = cnt_unmap;
233120272c2eSAli Bahrami 
233220272c2eSAli Bahrami 				/* Issue the callback */
233398c080d5SRod Evans 				DBG_CALL(Dbg_dl_iphdr_callback(clml, &info));
233498c080d5SRod Evans 				leave(clml, thr_flg_reenter);
233520272c2eSAli Bahrami 				ret = (* callback)(&info, sizeof (info), data);
233620272c2eSAli Bahrami 				(void) enter(thr_flg_reenter);
233720272c2eSAli Bahrami 
233820272c2eSAli Bahrami 				/* Return immediately on non-zero result */
233920272c2eSAli Bahrami 				if (ret != 0)
234020272c2eSAli Bahrami 					goto done;
234120272c2eSAli Bahrami 
234220272c2eSAli Bahrami 				/* Adapt to object mapping changes */
234398c080d5SRod Evans 				if ((cnt_map == l_cnt_map) &&
234498c080d5SRod Evans 				    (cnt_unmap == l_cnt_unmap))
234598c080d5SRod Evans 					continue;
234698c080d5SRod Evans 
234798c080d5SRod Evans 				DBG_CALL(Dbg_dl_iphdr_mapchange(clml, cnt_map,
234898c080d5SRod Evans 				    cnt_unmap));
234920272c2eSAli Bahrami 
235098c080d5SRod Evans 				/* Stop if an object was unmapped */
235198c080d5SRod Evans 				if (cnt_unmap == l_cnt_unmap) {
235220272c2eSAli Bahrami 					l_cnt_map = cnt_map;
235398c080d5SRod Evans 					continue;
235420272c2eSAli Bahrami 				}
235598c080d5SRod Evans 
235698c080d5SRod Evans 				ret = -1;
235798c080d5SRod Evans 				DBG_CALL(Dbg_dl_iphdr_unmap_ret(clml));
235898c080d5SRod Evans 				goto done;
235920272c2eSAli Bahrami 			}
236020272c2eSAli Bahrami 		}
236120272c2eSAli Bahrami 	}
236220272c2eSAli Bahrami 
236320272c2eSAli Bahrami done:
236420272c2eSAli Bahrami 	if (entry)
236520272c2eSAli Bahrami 		leave(LIST(clmp), 0);
236620272c2eSAli Bahrami 	return (ret);
236720272c2eSAli Bahrami }
2368