xref: /titanic_50/usr/src/cmd/sgs/rtld/common/dlfcns.c (revision 38f4bddda7216cf3550c325e8cabe56d08a2bce9)
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 
227c478bd9Sstevel@tonic-gate /*
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.
27*38f4bdddSBryan 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 {
657c478bd9Sstevel@tonic-gate 	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)) {
777c478bd9Sstevel@tonic-gate 
7856deab07SRod Evans 				if (find_segment(cpc, lmp))
797c478bd9Sstevel@tonic-gate 					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 {
1037c478bd9Sstevel@tonic-gate 	char	*error;
1047c478bd9Sstevel@tonic-gate 	Rt_map	*clmp;
1057c478bd9Sstevel@tonic-gate 	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 {
1307c478bd9Sstevel@tonic-gate 	Grp_desc	*gdp;
131cce0e03bSab196087 	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 	 */
138cce0e03bSab196087 	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 		 */
163cce0e03bSab196087 		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) {
1722017c965SRod Evans 		if (ale == ALE_CREATE)
1738af2c5b9Srie 			DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_ADD,
1748af2c5b9Srie 			    gdp->gd_flags));
1758af2c5b9Srie 		else if (gdp->gd_flags != oflags)
1768af2c5b9Srie 			DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_UPDATE,
1778af2c5b9Srie 			    gdp->gd_flags));
1788af2c5b9Srie 	}
1792017c965SRod Evans 
1802017c965SRod Evans 	if (alep)
1812017c965SRod Evans 		*alep = ale;
1822017c965SRod Evans 	return (gdp);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Create a handle.
1872017c965SRod Evans  *
1882017c965SRod Evans  *   rlmp -	represents the reference link-map for which the handle is being
1892017c965SRod Evans  *		created.
1902017c965SRod Evans  *   clmp -	represents the caller who is requesting the handle.
1912017c965SRod Evans  *   hflags -	provide group handle flags (GPH_*) that affect the use of the
1922017c965SRod Evans  *		handle, such as dlopen(0), or use or use of RTLD_FIRST.
1932017c965SRod Evans  *   rdflags -	provide group dependency flags for the reference link-map rlmp,
1942017c965SRod Evans  *		such as whether the dependency can be used for dlsym(), can be
1952017c965SRod Evans  *		relocated against, or whether this objects dependencies should
1962017c965SRod Evans  *		be processed.
1972017c965SRod Evans  *   cdflags -	provide group dependency flags for the caller.
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate Grp_hdl *
hdl_create(Lm_list * lml,Rt_map * rlmp,Rt_map * clmp,uint_t hflags,uint_t rdflags,uint_t cdflags)2002017c965SRod Evans hdl_create(Lm_list *lml, Rt_map *rlmp, Rt_map *clmp, uint_t hflags,
2012017c965SRod Evans     uint_t rdflags, uint_t cdflags)
2027c478bd9Sstevel@tonic-gate {
2032017c965SRod Evans 	Grp_hdl	*ghp = NULL, *aghp;
204cce0e03bSab196087 	APlist	**alpp;
205cce0e03bSab196087 	Aliste	idx;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	/*
2087c478bd9Sstevel@tonic-gate 	 * For dlopen(0) the handle is maintained as part of the link-map list,
2092017c965SRod Evans 	 * otherwise the handle is associated with the reference link-map.
2107c478bd9Sstevel@tonic-gate 	 */
2118af2c5b9Srie 	if (hflags & GPH_ZERO)
2127c478bd9Sstevel@tonic-gate 		alpp = &(lml->lm_handle);
2137c478bd9Sstevel@tonic-gate 	else
2142017c965SRod Evans 		alpp = &(HANDLES(rlmp));
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	/*
2178af2c5b9Srie 	 * Objects can contain multiple handles depending on the handle flags
2188af2c5b9Srie 	 * supplied.  Most RTLD flags pertain to the object itself and the
2198af2c5b9Srie 	 * bindings that it can achieve.  Multiple handles for these flags
2208af2c5b9Srie 	 * don't make sense.  But if the flag determines how the handle might
2218af2c5b9Srie 	 * be used, then multiple handles may exist.  Presently this only makes
2228af2c5b9Srie 	 * sense for RTLD_FIRST.  Determine if an appropriate handle already
2238af2c5b9Srie 	 * exists.
2247c478bd9Sstevel@tonic-gate 	 */
2252017c965SRod Evans 	for (APLIST_TRAVERSE(*alpp, idx, aghp)) {
2262017c965SRod Evans 		if ((aghp->gh_flags & GPH_FIRST) == (hflags & GPH_FIRST)) {
2272017c965SRod Evans 			ghp = aghp;
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 	}
2317c478bd9Sstevel@tonic-gate 
232b4059b01SRod Evans 	if (ghp == NULL) {
23356deab07SRod Evans 		uint_t	ndx;
23456deab07SRod Evans 
2352017c965SRod Evans 		/*
2362017c965SRod Evans 		 * If this is the first request for this handle, allocate and
2372017c965SRod Evans 		 * initialize a new handle.
2382017c965SRod Evans 		 */
2398af2c5b9Srie 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_CREATE));
2407c478bd9Sstevel@tonic-gate 
24156deab07SRod Evans 		if ((ghp = malloc(sizeof (Grp_hdl))) == NULL)
24256deab07SRod Evans 			return (NULL);
24302ca3e02Srie 
24456deab07SRod Evans 		/*
24556deab07SRod Evans 		 * Associate the handle with the link-map list or the reference
24656deab07SRod Evans 		 * link-map as appropriate.
24756deab07SRod Evans 		 */
24856deab07SRod Evans 		if (aplist_append(alpp, ghp, AL_CNT_GROUPS) == NULL) {
24956deab07SRod Evans 			free(ghp);
25056deab07SRod Evans 			return (NULL);
25156deab07SRod Evans 		}
2527c478bd9Sstevel@tonic-gate 
25356deab07SRod Evans 		/*
25456deab07SRod Evans 		 * Record the existence of this handle for future verification.
25556deab07SRod Evans 		 */
25656deab07SRod Evans 		/* LINTED */
25756deab07SRod Evans 		ndx = (uintptr_t)ghp % HDLIST_SZ;
25856deab07SRod Evans 
25956deab07SRod Evans 		if (aplist_append(&hdl_alp[ndx], ghp, AL_CNT_HANDLES) == NULL) {
26056deab07SRod Evans 			(void) aplist_delete_value(*alpp, ghp);
26156deab07SRod Evans 			free(ghp);
26256deab07SRod Evans 			return (NULL);
26356deab07SRod Evans 		}
26456deab07SRod Evans 
26556deab07SRod Evans 		ghp->gh_depends = NULL;
2667c478bd9Sstevel@tonic-gate 		ghp->gh_refcnt = 1;
2678af2c5b9Srie 		ghp->gh_flags = hflags;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		/*
2707c478bd9Sstevel@tonic-gate 		 * A dlopen(0) handle is identified by the GPH_ZERO flag, the
2717c478bd9Sstevel@tonic-gate 		 * head of the link-map list is defined as the owner.  There is
2727c478bd9Sstevel@tonic-gate 		 * no need to maintain a list of dependencies, for when this
2737c478bd9Sstevel@tonic-gate 		 * handle is used (for dlsym()) a dynamic search through the
2747c478bd9Sstevel@tonic-gate 		 * entire link-map list provides for searching all objects with
2757c478bd9Sstevel@tonic-gate 		 * GLOBAL visibility.
2767c478bd9Sstevel@tonic-gate 		 */
2778af2c5b9Srie 		if (hflags & GPH_ZERO) {
2785aefb655Srie 			ghp->gh_ownlmp = lml->lm_head;
2795aefb655Srie 			ghp->gh_ownlml = lml;
2807c478bd9Sstevel@tonic-gate 		} else {
2812017c965SRod Evans 			ghp->gh_ownlmp = rlmp;
2822017c965SRod Evans 			ghp->gh_ownlml = LIST(rlmp);
2837c478bd9Sstevel@tonic-gate 
2842017c965SRod Evans 			if (hdl_add(ghp, rlmp, rdflags, NULL) == NULL)
28556deab07SRod Evans 				return (NULL);
28660758829Srie 
28760758829Srie 			/*
2882017c965SRod Evans 			 * If this new handle is a private handle, there's no
2892017c965SRod Evans 			 * need to track the caller, so we're done.
2902017c965SRod Evans 			 */
2912017c965SRod Evans 			if (hflags & GPH_PRIVATE)
2922017c965SRod Evans 				return (ghp);
2932017c965SRod Evans 
2942017c965SRod Evans 			/*
2952017c965SRod Evans 			 * If this new handle is public, and isn't a special
2962017c965SRod Evans 			 * handle representing ld.so.1, indicate that a local
2972017c965SRod Evans 			 * group now exists.  This state allows singleton
2982017c965SRod Evans 			 * searches to be optimized.
29960758829Srie 			 */
30060758829Srie 			if ((hflags & GPH_LDSO) == 0)
3012017c965SRod Evans 				LIST(rlmp)->lm_flags |= LML_FLG_GROUPSEXIST;
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 	} else {
3047c478bd9Sstevel@tonic-gate 		/*
30502ca3e02Srie 		 * If a handle already exists, bump its reference count.
30602ca3e02Srie 		 *
30702ca3e02Srie 		 * If the previous reference count was 0, then this is a handle
30802ca3e02Srie 		 * that an earlier call to dlclose() was unable to remove.  Such
30902ca3e02Srie 		 * handles are put on the orphan list.  As this handle is back
31002ca3e02Srie 		 * in use, it must be removed from the orphan list.
31102ca3e02Srie 		 *
31202ca3e02Srie 		 * Note, handles associated with a link-map list itself (i.e.
31302ca3e02Srie 		 * dlopen(0)) can have a reference count of 0.  However, these
31402ca3e02Srie 		 * handles are never deleted, and therefore are never moved to
31502ca3e02Srie 		 * the orphan list.
3167c478bd9Sstevel@tonic-gate 		 */
3177c478bd9Sstevel@tonic-gate 		if ((ghp->gh_refcnt++ == 0) &&
31802ca3e02Srie 		    ((ghp->gh_flags & GPH_ZERO) == 0)) {
3197c478bd9Sstevel@tonic-gate 			uint_t	ndx;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 			/* LINTED */
322b3fbe5e6Sseizo 			ndx = (uintptr_t)ghp % HDLIST_SZ;
3237c478bd9Sstevel@tonic-gate 
32456deab07SRod Evans 			(void) aplist_delete_value(hdl_alp[HDLIST_ORP], ghp);
32556deab07SRod Evans 			(void) aplist_append(&hdl_alp[ndx], ghp,
32656deab07SRod Evans 			    AL_CNT_HANDLES);
3277c478bd9Sstevel@tonic-gate 
3285aefb655Srie 			if (DBG_ENABLED) {
329cce0e03bSab196087 				Aliste		idx;
3307c478bd9Sstevel@tonic-gate 				Grp_desc	*gdp;
3317c478bd9Sstevel@tonic-gate 
3328af2c5b9Srie 				DBG_CALL(Dbg_file_hdl_title(DBG_HDL_REINST));
333cce0e03bSab196087 				for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp))
3347c478bd9Sstevel@tonic-gate 					DBG_CALL(Dbg_file_hdl_action(ghp,
33502ca3e02Srie 					    gdp->gd_depend, DBG_DEP_REINST, 0));
3367c478bd9Sstevel@tonic-gate 			}
3377c478bd9Sstevel@tonic-gate 		}
3382017c965SRod Evans 
3392017c965SRod Evans 		/*
3402017c965SRod Evans 		 * If we've been asked to create a private handle, there's no
3412017c965SRod Evans 		 * need to track the caller.
3422017c965SRod Evans 		 */
3432017c965SRod Evans 		if (hflags & GPH_PRIVATE) {
3442017c965SRod Evans 			/*
3452017c965SRod Evans 			 * Negate the reference count increment.
3462017c965SRod Evans 			 */
3472017c965SRod Evans 			ghp->gh_refcnt--;
3482017c965SRod Evans 			return (ghp);
3492017c965SRod Evans 		} else {
3502017c965SRod Evans 			/*
3512017c965SRod Evans 			 * If a private handle already exists, promote this
3522017c965SRod Evans 			 * handle to public by initializing both the reference
3532017c965SRod Evans 			 * count and the handle flags.
3542017c965SRod Evans 			 */
3552017c965SRod Evans 			if (ghp->gh_flags & GPH_PRIVATE) {
3562017c965SRod Evans 				ghp->gh_refcnt = 1;
3572017c965SRod Evans 				ghp->gh_flags &= ~GPH_PRIVATE;
3582017c965SRod Evans 				ghp->gh_flags |= hflags;
3592017c965SRod Evans 			}
3602017c965SRod Evans 		}
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
3642017c965SRod Evans 	 * Keep track of the parent (caller).  As this object can be referenced
3658af2c5b9Srie 	 * by different parents, this processing is carried out every time a
3668af2c5b9Srie 	 * handle is requested.
3677c478bd9Sstevel@tonic-gate 	 */
3682017c965SRod Evans 	if (clmp && (hdl_add(ghp, clmp, cdflags, NULL) == NULL))
36956deab07SRod Evans 		return (NULL);
3708af2c5b9Srie 
3717c478bd9Sstevel@tonic-gate 	return (ghp);
3727c478bd9Sstevel@tonic-gate }
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate /*
3757c478bd9Sstevel@tonic-gate  * Initialize a handle that has been created for an object that is already
3767c478bd9Sstevel@tonic-gate  * loaded.  The handle is initialized with the present dependencies of that
3777c478bd9Sstevel@tonic-gate  * object.  Once this initialization has occurred, any new objects that might
3787c478bd9Sstevel@tonic-gate  * be loaded as dependencies (lazy-loading) are added to the handle as each new
3797c478bd9Sstevel@tonic-gate  * object is loaded.
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate int
hdl_initialize(Grp_hdl * ghp,Rt_map * nlmp,int mode,int promote)38202ca3e02Srie hdl_initialize(Grp_hdl *ghp, Rt_map *nlmp, int mode, int promote)
3837c478bd9Sstevel@tonic-gate {
384cce0e03bSab196087 	Aliste		idx;
3857c478bd9Sstevel@tonic-gate 	Grp_desc	*gdp;
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	/*
3887c478bd9Sstevel@tonic-gate 	 * If the handle has already been initialized, and the initial object's
3897c478bd9Sstevel@tonic-gate 	 * mode hasn't been promoted, there's no need to recompute the modes of
3907c478bd9Sstevel@tonic-gate 	 * any dependencies.  If the object we've added has just been opened,
3917c478bd9Sstevel@tonic-gate 	 * the objects dependencies will not yet have been processed.  These
3927c478bd9Sstevel@tonic-gate 	 * dependencies will be added on later calls to load_one().  Otherwise,
3937c478bd9Sstevel@tonic-gate 	 * this object already exists, so add all of its dependencies to the
3947c478bd9Sstevel@tonic-gate 	 * handle were operating on.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	if (((ghp->gh_flags & GPH_INITIAL) && (promote == 0)) ||
3977c478bd9Sstevel@tonic-gate 	    ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)) {
3987c478bd9Sstevel@tonic-gate 		ghp->gh_flags |= GPH_INITIAL;
3997c478bd9Sstevel@tonic-gate 		return (1);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 
4028af2c5b9Srie 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
403cce0e03bSab196087 	for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
4047c478bd9Sstevel@tonic-gate 		Rt_map		*lmp = gdp->gd_depend;
405cce0e03bSab196087 		Aliste		idx1;
406cce0e03bSab196087 		Bnd_desc	*bdp;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 		/*
4097c478bd9Sstevel@tonic-gate 		 * If this dependency doesn't indicate that its dependencies
4107c478bd9Sstevel@tonic-gate 		 * should be added to a handle, ignore it.  This case identifies
4117c478bd9Sstevel@tonic-gate 		 * a parent of a dlopen(RTLD_PARENT) request.
4127c478bd9Sstevel@tonic-gate 		 */
4137c478bd9Sstevel@tonic-gate 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
4147c478bd9Sstevel@tonic-gate 			continue;
4157c478bd9Sstevel@tonic-gate 
416cce0e03bSab196087 		for (APLIST_TRAVERSE(DEPENDS(lmp), idx1, bdp)) {
4177c478bd9Sstevel@tonic-gate 			Rt_map	*dlmp = bdp->b_depend;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 			if ((bdp->b_flags & BND_NEEDED) == 0)
4207c478bd9Sstevel@tonic-gate 				continue;
4217c478bd9Sstevel@tonic-gate 
422e0e63816SRod Evans 			if (hdl_add(ghp, dlmp,
423e0e63816SRod Evans 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), NULL) == NULL)
4247c478bd9Sstevel@tonic-gate 				return (0);
42502ca3e02Srie 
426e0e63816SRod Evans 			(void) update_mode(dlmp, MODE(dlmp), mode);
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	ghp->gh_flags |= GPH_INITIAL;
4307c478bd9Sstevel@tonic-gate 	return (1);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * Sanity check a program-provided handle.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate static int
hdl_validate(Grp_hdl * ghp)4377c478bd9Sstevel@tonic-gate hdl_validate(Grp_hdl *ghp)
4387c478bd9Sstevel@tonic-gate {
43956deab07SRod Evans 	Aliste		idx;
4405aefb655Srie 	Grp_hdl		*lghp;
4417c478bd9Sstevel@tonic-gate 	uint_t		ndx;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	/* LINTED */
444b3fbe5e6Sseizo 	ndx = (uintptr_t)ghp % HDLIST_SZ;
4457c478bd9Sstevel@tonic-gate 
44656deab07SRod Evans 	for (APLIST_TRAVERSE(hdl_alp[ndx], idx, lghp)) {
4475aefb655Srie 		if ((lghp == ghp) && (ghp->gh_refcnt != 0))
4487c478bd9Sstevel@tonic-gate 			return (1);
4495aefb655Srie 	}
4507c478bd9Sstevel@tonic-gate 	return (0);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate /*
4547c478bd9Sstevel@tonic-gate  * Core dlclose activity.
4557c478bd9Sstevel@tonic-gate  */
4567c478bd9Sstevel@tonic-gate int
dlclose_core(Grp_hdl * ghp,Rt_map * clmp,Lm_list * lml)45702ca3e02Srie dlclose_core(Grp_hdl *ghp, Rt_map *clmp, Lm_list *lml)
4587c478bd9Sstevel@tonic-gate {
45902ca3e02Srie 	int	error;
4602020b2b6SRod Evans 	Rt_map	*lmp;
46102ca3e02Srie 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * If we're already at atexit() there's no point processing further,
4647c478bd9Sstevel@tonic-gate 	 * all objects have already been tsorted for fini processing.
4657c478bd9Sstevel@tonic-gate 	 */
466dde769a2SRod Evans 	if (rtld_flags & RT_FL_ATEXIT)
4677c478bd9Sstevel@tonic-gate 		return (0);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * Diagnose what we're up to.
4717c478bd9Sstevel@tonic-gate 	 */
4727c478bd9Sstevel@tonic-gate 	if (ghp->gh_flags & GPH_ZERO) {
47398c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlclose(clmp, MSG_ORIG(MSG_STR_ZERO),
4747c478bd9Sstevel@tonic-gate 		    DBG_DLCLOSE_IGNORE));
4757c478bd9Sstevel@tonic-gate 	} else {
47698c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlclose(clmp, NAME(ghp->gh_ownlmp),
47702ca3e02Srie 		    DBG_DLCLOSE_NULL));
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 	/*
4817c478bd9Sstevel@tonic-gate 	 * Decrement reference count of this object.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	if (--(ghp->gh_refcnt))
4847c478bd9Sstevel@tonic-gate 		return (0);
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/*
4877c478bd9Sstevel@tonic-gate 	 * If this handle is special (dlopen(0)), then leave it around - it
4887c478bd9Sstevel@tonic-gate 	 * has little overhead.
4897c478bd9Sstevel@tonic-gate 	 */
4907c478bd9Sstevel@tonic-gate 	if (ghp->gh_flags & GPH_ZERO)
4917c478bd9Sstevel@tonic-gate 		return (0);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	/*
4942020b2b6SRod Evans 	 * If this handle is associated with an object that is not on the base
4952020b2b6SRod Evans 	 * link-map control list, or it has not yet been relocated, then this
49634b5025bSRod Evans 	 * handle must have originated from an auditors interaction, or some
49734b5025bSRod Evans 	 * permutation of RTLD_CONFGEN use (crle(1), moe(1), etc.).  User code
4982020b2b6SRod Evans 	 * can only execute and bind to relocated objects on the base link-map
49934b5025bSRod Evans 	 * control list.  Outside of RTLD_CONFGEN use, a non-relocated object,
50034b5025bSRod Evans 	 * or an object on a non-base link-map control list, is in the process
50134b5025bSRod Evans 	 * of being loaded, and therefore we do not attempt to remove the
50234b5025bSRod Evans 	 * handle.
5032020b2b6SRod Evans 	 */
5042020b2b6SRod Evans 	if (((lmp = ghp->gh_ownlmp) != NULL) &&
50534b5025bSRod Evans 	    ((MODE(lmp) & RTLD_CONFGEN) == 0) &&
5062020b2b6SRod Evans 	    ((CNTL(lmp) != ALIST_OFF_DATA) ||
5072020b2b6SRod Evans 	    ((FLAGS(lmp) & FLG_RT_RELOCED) == 0)))
5082020b2b6SRod Evans 		return (0);
5092020b2b6SRod Evans 
5102020b2b6SRod Evans 	/*
51102ca3e02Srie 	 * This handle is no longer being referenced, remove it.  If this handle
51202ca3e02Srie 	 * is part of an alternative link-map list, determine if the whole list
51302ca3e02Srie 	 * can be removed also.
5147c478bd9Sstevel@tonic-gate 	 */
515b4059b01SRod Evans 	error = remove_hdl(ghp, clmp, NULL);
51602ca3e02Srie 
51702ca3e02Srie 	if ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)
51802ca3e02Srie 		remove_lml(lml);
51902ca3e02Srie 
52002ca3e02Srie 	return (error);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Internal dlclose activity.  Called from user level or directly for internal
5257c478bd9Sstevel@tonic-gate  * error cleanup.
5267c478bd9Sstevel@tonic-gate  */
5277c478bd9Sstevel@tonic-gate int
dlclose_intn(Grp_hdl * ghp,Rt_map * clmp)5287c478bd9Sstevel@tonic-gate dlclose_intn(Grp_hdl *ghp, Rt_map *clmp)
5297c478bd9Sstevel@tonic-gate {
530b4059b01SRod Evans 	Rt_map	*nlmp = NULL;
531b4059b01SRod Evans 	Lm_list	*olml = NULL;
5327c478bd9Sstevel@tonic-gate 	int	error;
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * Although we're deleting object(s) it's quite possible that additional
5367c478bd9Sstevel@tonic-gate 	 * objects get loaded from running the .fini section(s) of the objects
5377c478bd9Sstevel@tonic-gate 	 * being deleted.  These objects will have been added to the same
5387c478bd9Sstevel@tonic-gate 	 * link-map list as those objects being deleted.  Remember this list
5397c478bd9Sstevel@tonic-gate 	 * for later investigation.
5407c478bd9Sstevel@tonic-gate 	 */
5415aefb655Srie 	olml = ghp->gh_ownlml;
5427c478bd9Sstevel@tonic-gate 
54302ca3e02Srie 	error = dlclose_core(ghp, clmp, olml);
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	/*
5467c478bd9Sstevel@tonic-gate 	 * Determine whether the original link-map list still exists.  In the
5477c478bd9Sstevel@tonic-gate 	 * case of a dlclose of an alternative (dlmopen) link-map the whole
5487c478bd9Sstevel@tonic-gate 	 * list may have been removed.
5497c478bd9Sstevel@tonic-gate 	 */
5507c478bd9Sstevel@tonic-gate 	if (olml) {
55157ef7aa9SRod Evans 		Aliste	idx;
5527c478bd9Sstevel@tonic-gate 		Lm_list	*lml;
5537c478bd9Sstevel@tonic-gate 
55457ef7aa9SRod Evans 		for (APLIST_TRAVERSE(dynlm_list, idx, lml)) {
5557c478bd9Sstevel@tonic-gate 			if (olml == lml) {
5567c478bd9Sstevel@tonic-gate 				nlmp = olml->lm_head;
5577c478bd9Sstevel@tonic-gate 				break;
5587c478bd9Sstevel@tonic-gate 			}
5597c478bd9Sstevel@tonic-gate 		}
5607c478bd9Sstevel@tonic-gate 	}
5617247f888Srie 	load_completion(nlmp);
5627c478bd9Sstevel@tonic-gate 	return (error);
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate  * Argument checking for dlclose.  Only called via external entry.
5677c478bd9Sstevel@tonic-gate  */
5687c478bd9Sstevel@tonic-gate static int
dlclose_check(void * handle,Rt_map * clmp)5697c478bd9Sstevel@tonic-gate dlclose_check(void *handle, Rt_map *clmp)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	Grp_hdl	*ghp = (Grp_hdl *)handle;
5727c478bd9Sstevel@tonic-gate 
5735aefb655Srie 	if (hdl_validate(ghp) == 0) {
57498c080d5SRod Evans 		Conv_inv_buf_t	inv_buf;
57598c080d5SRod Evans 
57698c080d5SRod Evans 		(void) conv_invalid_val(&inv_buf, EC_NATPTR(ghp), 0);
57798c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlclose(clmp, inv_buf.buf, DBG_DLCLOSE_NULL));
57898c080d5SRod Evans 
579b4059b01SRod Evans 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
580b4059b01SRod Evans 		    EC_NATPTR(handle));
5817c478bd9Sstevel@tonic-gate 		return (1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	return (dlclose_intn(ghp, clmp));
5847c478bd9Sstevel@tonic-gate }
5857c478bd9Sstevel@tonic-gate 
5867257d1b4Sraf #pragma weak _dlclose = dlclose
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate  * External entry for dlclose(3dl).  Returns 0 for success, non-zero otherwise.
5907c478bd9Sstevel@tonic-gate  */
5917c478bd9Sstevel@tonic-gate int
dlclose(void * handle)5927257d1b4Sraf dlclose(void *handle)
5937c478bd9Sstevel@tonic-gate {
5947c478bd9Sstevel@tonic-gate 	int		error, entry;
5957c478bd9Sstevel@tonic-gate 	Rt_map		*clmp;
5967c478bd9Sstevel@tonic-gate 
5978cd45542Sraf 	entry = enter(0);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 	error = dlclose_check(handle, clmp);
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if (entry)
6048cd45542Sraf 		leave(LIST(clmp), 0);
6057c478bd9Sstevel@tonic-gate 	return (error);
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6085aefb655Srie static uint_t	lmid = 0;
6095aefb655Srie 
6105aefb655Srie /*
6115aefb655Srie  * The addition of new link-map lists is assumed to be in small quantities.
6125aefb655Srie  * Here, we assign a unique link-map id for diagnostic use.  Simply update the
6135aefb655Srie  * running link-map count until we max out.
6145aefb655Srie  */
6155aefb655Srie int
newlmid(Lm_list * lml)6165aefb655Srie newlmid(Lm_list *lml)
6175aefb655Srie {
6185aefb655Srie 	char	buffer[MSG_LMID_ALT_SIZE + 12];
6195aefb655Srie 
6205aefb655Srie 	if (lmid == UINT_MAX) {
6215aefb655Srie 		lml->lm_lmid = UINT_MAX;
6225aefb655Srie 		(void) strncpy(buffer, MSG_ORIG(MSG_LMID_MAXED),
6235aefb655Srie 		    MSG_LMID_ALT_SIZE + 12);
6245aefb655Srie 	} else {
6255aefb655Srie 		lml->lm_lmid = lmid++;
6265aefb655Srie 		(void) snprintf(buffer, MSG_LMID_ALT_SIZE + 12,
6275aefb655Srie 		    MSG_ORIG(MSG_LMID_FMT), MSG_ORIG(MSG_LMID_ALT),
6285aefb655Srie 		    lml->lm_lmid);
6295aefb655Srie 	}
630b4059b01SRod Evans 	if ((lml->lm_lmidstr = strdup(buffer)) == NULL)
6315aefb655Srie 		return (0);
6325aefb655Srie 
6335aefb655Srie 	return (1);
6345aefb655Srie }
6355aefb655Srie 
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate  * Core dlopen activity.
6387c478bd9Sstevel@tonic-gate  */
6397c478bd9Sstevel@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)640dde769a2SRod Evans dlmopen_core(Lm_list *lml, Lm_list *olml, const char *path, int mode,
641dde769a2SRod Evans     Rt_map *clmp, uint_t flags, uint_t orig, int *in_nfavl)
6427c478bd9Sstevel@tonic-gate {
64356deab07SRod Evans 	Alist		*palp = NULL;
6447c478bd9Sstevel@tonic-gate 	Rt_map		*nlmp;
6457c478bd9Sstevel@tonic-gate 	Grp_hdl		*ghp;
6467c478bd9Sstevel@tonic-gate 	Aliste		olmco, nlmco;
6477c478bd9Sstevel@tonic-gate 
64898c080d5SRod Evans 	DBG_CALL(Dbg_dl_dlopen(clmp,
6499aa23310Srie 	    (path ? path : MSG_ORIG(MSG_STR_ZERO)), in_nfavl, mode));
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	/*
652ca4eed8bSAli Bahrami 	 * Having diagnosed the originally defined modes, assign any defaults
653ca4eed8bSAli Bahrami 	 * or corrections.
654ca4eed8bSAli Bahrami 	 */
655ca4eed8bSAli Bahrami 	if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) &&
656ca4eed8bSAli Bahrami 	    ((mode & RTLD_NOLOAD) == 0))
657ca4eed8bSAli Bahrami 		mode |= (RTLD_GROUP | RTLD_WORLD);
658ca4eed8bSAli Bahrami 	if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) {
659ca4eed8bSAli Bahrami 		mode &= ~RTLD_NOW;
660ca4eed8bSAli Bahrami 		mode |= RTLD_LAZY;
661ca4eed8bSAli Bahrami 	}
662ca4eed8bSAli Bahrami 
663ca4eed8bSAli Bahrami 	/*
6647c478bd9Sstevel@tonic-gate 	 * If the path specified is null then we're operating on global
6657c478bd9Sstevel@tonic-gate 	 * objects.  Associate a dummy handle with the link-map list.
6667c478bd9Sstevel@tonic-gate 	 */
667b4059b01SRod Evans 	if (path == NULL) {
6687c478bd9Sstevel@tonic-gate 		Grp_hdl *ghp;
6692017c965SRod Evans 		uint_t	hflags, rdflags, cdflags;
6707c478bd9Sstevel@tonic-gate 		int	promote = 0;
6717c478bd9Sstevel@tonic-gate 
6728af2c5b9Srie 		/*
6738af2c5b9Srie 		 * Establish any flags for the handle (Grp_hdl).
6748af2c5b9Srie 		 *
6752017c965SRod Evans 		 *  -	This is a dummy, public, handle (0) that provides for a
6762017c965SRod Evans 		 *	dynamic	search of all global objects within the process.
6772017c965SRod Evans 		 *  -   Use of the RTLD_FIRST mode indicates that only the first
6782017c965SRod Evans 		 *	dependency on the handle (the referenced object) can be
6792017c965SRod Evans 		 *	used to satisfy dlsym() requests.
6808af2c5b9Srie 		 */
6812017c965SRod Evans 		hflags = (GPH_PUBLIC | GPH_ZERO);
6827c478bd9Sstevel@tonic-gate 		if (mode & RTLD_FIRST)
6837c478bd9Sstevel@tonic-gate 			hflags |= GPH_FIRST;
6847c478bd9Sstevel@tonic-gate 
6858af2c5b9Srie 		/*
6862017c965SRod Evans 		 * Establish the flags for the referenced dependency descriptor
6872017c965SRod Evans 		 * (Grp_desc).
6882017c965SRod Evans 		 *
6892017c965SRod Evans 		 *  -	The referenced object is available for dlsym().
6902017c965SRod Evans 		 *  -	The referenced object is available to relocate against.
6912017c965SRod Evans 		 *  -	The referenced object should have it's dependencies
6922017c965SRod Evans 		 *	added to this handle.
6932017c965SRod Evans 		 */
6942017c965SRod Evans 		rdflags = (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS);
6952017c965SRod Evans 
6962017c965SRod Evans 		/*
6978af2c5b9Srie 		 * Establish the flags for this callers dependency descriptor
6988af2c5b9Srie 		 * (Grp_desc).
6998af2c5b9Srie 		 *
7002017c965SRod Evans 		 *  -	The explicit creation of a handle creates a descriptor
7012017c965SRod Evans 		 *	for the referenced object and the parent (caller).
7022017c965SRod Evans 		 *  -	Use of the RTLD_PARENT flag indicates that the parent
7038af2c5b9Srie 		 *	can be relocated against.
7048af2c5b9Srie 		 */
7052017c965SRod Evans 		cdflags = GPD_PARENT;
7068af2c5b9Srie 		if (mode & RTLD_PARENT)
7078af2c5b9Srie 			cdflags |= GPD_RELOC;
7088af2c5b9Srie 
7092017c965SRod Evans 		if ((ghp = hdl_create(lml, 0, clmp, hflags, rdflags,
7102017c965SRod Evans 		    cdflags)) == NULL)
711b4059b01SRod Evans 			return (NULL);
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		/*
7147c478bd9Sstevel@tonic-gate 		 * Traverse the main link-map control list, updating the mode
7157c478bd9Sstevel@tonic-gate 		 * of any objects as necessary.  Call the relocation engine if
7167c478bd9Sstevel@tonic-gate 		 * this mode promotes the existing state of any relocations.
7177c478bd9Sstevel@tonic-gate 		 * crle()'s first pass loads all objects necessary for building
7187c478bd9Sstevel@tonic-gate 		 * a configuration file, however none of them are relocated.
7197c478bd9Sstevel@tonic-gate 		 * crle()'s second pass relocates objects in preparation for
7207c478bd9Sstevel@tonic-gate 		 * dldump()'ing using dlopen(0, RTLD_NOW).
7217c478bd9Sstevel@tonic-gate 		 */
7227c478bd9Sstevel@tonic-gate 		if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN)
7237c478bd9Sstevel@tonic-gate 			return (ghp);
7247c478bd9Sstevel@tonic-gate 
725cb511613SAli Bahrami 		for (nlmp = lml->lm_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
7267c478bd9Sstevel@tonic-gate 			if (((MODE(nlmp) & RTLD_GLOBAL) == 0) ||
7277c478bd9Sstevel@tonic-gate 			    (FLAGS(nlmp) & FLG_RT_DELETE))
7287c478bd9Sstevel@tonic-gate 				continue;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 			if (update_mode(nlmp, MODE(nlmp), mode))
7317c478bd9Sstevel@tonic-gate 				promote = 1;
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 		if (promote)
734cce0e03bSab196087 			(void) relocate_lmc(lml, ALIST_OFF_DATA, clmp,
7359aa23310Srie 			    lml->lm_head, in_nfavl);
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 		return (ghp);
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/*
7417c478bd9Sstevel@tonic-gate 	 * Fix the pathname.  If this object expands to multiple paths (ie.
7427c478bd9Sstevel@tonic-gate 	 * $ISALIST or $HWCAP have been used), then make sure the user has also
7437c478bd9Sstevel@tonic-gate 	 * furnished the RTLD_FIRST flag.  As yet, we don't support opening
7447c478bd9Sstevel@tonic-gate 	 * more than one object at a time, so enforcing the RTLD_FIRST flag
7457c478bd9Sstevel@tonic-gate 	 * provides flexibility should we be able to support dlopening more
7467c478bd9Sstevel@tonic-gate 	 * than one object in the future.
7477c478bd9Sstevel@tonic-gate 	 */
748b4059b01SRod Evans 	if (LM_FIX_NAME(clmp)(path, clmp, &palp, AL_CNT_NEEDED, orig) == NULL)
749b4059b01SRod Evans 		return (NULL);
75002ca3e02Srie 
75156deab07SRod Evans 	if ((palp->al_arritems > 1) && ((mode & RTLD_FIRST) == 0)) {
7522020b2b6SRod Evans 		remove_alist(&palp, 1);
7535aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5));
754b4059b01SRod Evans 		return (NULL);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	/*
758dde769a2SRod Evans 	 * Establish a link-map control list for this request, and load the
7597c478bd9Sstevel@tonic-gate 	 * associated object.
7607c478bd9Sstevel@tonic-gate 	 */
761dde769a2SRod Evans 	if ((nlmco = create_cntl(lml, 1)) == NULL) {
7622020b2b6SRod Evans 		remove_alist(&palp, 1);
763b4059b01SRod Evans 		return (NULL);
7647c478bd9Sstevel@tonic-gate 	}
765dde769a2SRod Evans 	olmco = nlmco;
7667c478bd9Sstevel@tonic-gate 
7672017c965SRod Evans 	nlmp = load_one(lml, nlmco, palp, clmp, mode, (flags | FLG_RT_PUBHDL),
76856deab07SRod Evans 	    &ghp, in_nfavl);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	/*
7717c478bd9Sstevel@tonic-gate 	 * Remove any expanded pathname infrastructure, and if the dependency
7727c478bd9Sstevel@tonic-gate 	 * couldn't be loaded, cleanup.
7737c478bd9Sstevel@tonic-gate 	 */
7742020b2b6SRod Evans 	remove_alist(&palp, 1);
775b4059b01SRod Evans 	if (nlmp == NULL) {
7767c478bd9Sstevel@tonic-gate 		remove_cntl(lml, olmco);
777b4059b01SRod Evans 		return (NULL);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	/*
7817c478bd9Sstevel@tonic-gate 	 * If loading an auditor was requested, and the auditor already existed,
7827c478bd9Sstevel@tonic-gate 	 * then the link-map returned will be to the original auditor.  The new
7837c478bd9Sstevel@tonic-gate 	 * link-map list that was initially created, and the associated link-map
7847c478bd9Sstevel@tonic-gate 	 * control list are no longer needed.  As the auditor is already loaded,
7857c478bd9Sstevel@tonic-gate 	 * we're probably done, but fall through in case additional relocations
7867c478bd9Sstevel@tonic-gate 	 * would be triggered by the mode of the caller.
7877c478bd9Sstevel@tonic-gate 	 */
7887c478bd9Sstevel@tonic-gate 	if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) {
7897c478bd9Sstevel@tonic-gate 		remove_cntl(lml, olmco);
7907c478bd9Sstevel@tonic-gate 		lml = LIST(nlmp);
7917c478bd9Sstevel@tonic-gate 		olmco = 0;
792cce0e03bSab196087 		nlmco = ALIST_OFF_DATA;
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 	/*
7967c478bd9Sstevel@tonic-gate 	 * Finish processing the objects associated with this request.
7977c478bd9Sstevel@tonic-gate 	 */
7982020b2b6SRod Evans 	if (((nlmp = analyze_lmc(lml, nlmco, nlmp, clmp, in_nfavl)) == NULL) ||
7999aa23310Srie 	    (relocate_lmc(lml, nlmco, clmp, nlmp, in_nfavl) == 0)) {
800b4059b01SRod Evans 		ghp = NULL;
80156deab07SRod Evans 		nlmp = NULL;
8027c478bd9Sstevel@tonic-gate 	}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 	/*
805481bba9eSRod Evans 	 * If the dlopen has failed, clean up any objects that might have been
806481bba9eSRod Evans 	 * loaded successfully on this new link-map control list.
80702ca3e02Srie 	 */
808dde769a2SRod Evans 	if (olmco && (nlmp == NULL))
809481bba9eSRod Evans 		remove_lmc(lml, clmp, olmco, path);
81002ca3e02Srie 
81102ca3e02Srie 	/*
812dde769a2SRod Evans 	 * Finally, remove any temporary link-map control list.  Note, if this
813dde769a2SRod Evans 	 * operation successfully established a new link-map list, then a base
814dde769a2SRod Evans 	 * link-map control list will have been created, which must remain.
8157c478bd9Sstevel@tonic-gate 	 */
816dde769a2SRod Evans 	if (olmco && ((nlmp == NULL) || (olml != (Lm_list *)LM_ID_NEWLM)))
8177c478bd9Sstevel@tonic-gate 		remove_cntl(lml, olmco);
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	return (ghp);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate /*
8239aa23310Srie  * dlopen() and dlsym() operations are the means by which a process can
8249aa23310Srie  * test for the existence of required dependencies.  If the necessary
8259aa23310Srie  * dependencies don't exist, then associated functionality can't be used.
8269aa23310Srie  * However, the lack of dependencies can be fixed, and the dlopen() and
8279aa23310Srie  * dlsym() requests can be repeated.  As we use a "not-found" AVL tree to
8289aa23310Srie  * cache any failed full path loads, secondary dlopen() and dlsym() requests
8299aa23310Srie  * will fail, even if the dependencies have been installed.
8309aa23310Srie  *
8319aa23310Srie  * dlopen() and dlsym() retry any failures by removing the "not-found" AVL
8329aa23310Srie  * tree.  Should any dependencies be found, their names are added to the
8339aa23310Srie  * FullPath AVL tree.  This routine removes any new "not-found" AVL tree,
8349aa23310Srie  * so that the dlopen() or dlsym() can replace the original "not-found" tree.
8359aa23310Srie  */
8369aa23310Srie inline static void
nfavl_remove(avl_tree_t * avlt)8379aa23310Srie nfavl_remove(avl_tree_t *avlt)
8389aa23310Srie {
8399aa23310Srie 	PathNode	*pnp;
8409aa23310Srie 	void		*cookie = NULL;
8419aa23310Srie 
8429aa23310Srie 	if (avlt) {
84356deab07SRod Evans 		while ((pnp = avl_destroy_nodes(avlt, &cookie)) != NULL)
8449aa23310Srie 			free(pnp);
84556deab07SRod Evans 
8469aa23310Srie 		avl_destroy(avlt);
8479aa23310Srie 		free(avlt);
8489aa23310Srie 	}
8499aa23310Srie }
8509aa23310Srie 
8519aa23310Srie /*
8527c478bd9Sstevel@tonic-gate  * Internal dlopen() activity.  Called from user level or directly for internal
8537c478bd9Sstevel@tonic-gate  * opens that require a handle.
8547c478bd9Sstevel@tonic-gate  */
8557c478bd9Sstevel@tonic-gate Grp_hdl *
dlmopen_intn(Lm_list * lml,const char * path,int mode,Rt_map * clmp,uint_t flags,uint_t orig)8567c478bd9Sstevel@tonic-gate dlmopen_intn(Lm_list *lml, const char *path, int mode, Rt_map *clmp,
8579aa23310Srie     uint_t flags, uint_t orig)
8587c478bd9Sstevel@tonic-gate {
859dde769a2SRod Evans 	Lm_list	*olml = lml;
860b4059b01SRod Evans 	Rt_map	*dlmp = NULL;
8617c478bd9Sstevel@tonic-gate 	Grp_hdl	*ghp;
8629aa23310Srie 	int	in_nfavl = 0;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/*
86510a4fa49Srie 	 * Check for magic link-map list values:
86610a4fa49Srie 	 *
86710a4fa49Srie 	 *  LM_ID_BASE:		Operate on the PRIMARY (executables) link map
86810a4fa49Srie 	 *  LM_ID_LDSO:		Operation on ld.so.1's link map
86910a4fa49Srie 	 *  LM_ID_NEWLM: 	Create a new link-map.
87010a4fa49Srie 	 */
87110a4fa49Srie 	if (lml == (Lm_list *)LM_ID_NEWLM) {
87256deab07SRod Evans 		if ((lml = calloc(sizeof (Lm_list), 1)) == NULL)
87356deab07SRod Evans 			return (NULL);
87410a4fa49Srie 
87510a4fa49Srie 		/*
87610a4fa49Srie 		 * Establish the new link-map flags from the callers and those
87710a4fa49Srie 		 * explicitly provided.
87810a4fa49Srie 		 */
87910a4fa49Srie 		lml->lm_tflags = LIST(clmp)->lm_tflags;
88010a4fa49Srie 		if (flags & FLG_RT_AUDIT) {
88110a4fa49Srie 			/*
88210a4fa49Srie 			 * Unset any auditing flags - an auditor shouldn't be
88310a4fa49Srie 			 * audited.  Insure all audit dependencies are loaded.
88410a4fa49Srie 			 */
88510a4fa49Srie 			lml->lm_tflags &= ~LML_TFLG_AUD_MASK;
8862020b2b6SRod Evans 			lml->lm_tflags |= (LML_TFLG_NOLAZYLD |
8872020b2b6SRod Evans 			    LML_TFLG_LOADFLTR | LML_TFLG_NOAUDIT);
88810a4fa49Srie 		}
88910a4fa49Srie 
89057ef7aa9SRod Evans 		if (aplist_append(&dynlm_list, lml, AL_CNT_DYNLIST) == NULL) {
89102ca3e02Srie 			free(lml);
89256deab07SRod Evans 			return (NULL);
89302ca3e02Srie 		}
89402ca3e02Srie 		if (newlmid(lml) == 0) {
89557ef7aa9SRod Evans 			(void) aplist_delete_value(dynlm_list, lml);
89610a4fa49Srie 			free(lml);
89756deab07SRod Evans 			return (NULL);
89810a4fa49Srie 		}
89910a4fa49Srie 	} else if ((uintptr_t)lml < LM_ID_NUM) {
90010a4fa49Srie 		if ((uintptr_t)lml == LM_ID_BASE)
90110a4fa49Srie 			lml = &lml_main;
90210a4fa49Srie 		else if ((uintptr_t)lml == LM_ID_LDSO)
90310a4fa49Srie 			lml = &lml_rtld;
90410a4fa49Srie 	}
90510a4fa49Srie 
90610a4fa49Srie 	/*
90710a4fa49Srie 	 * Open the required object on the associated link-map list.
9087c478bd9Sstevel@tonic-gate 	 */
909dde769a2SRod Evans 	ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig, &in_nfavl);
9109aa23310Srie 
9117c478bd9Sstevel@tonic-gate 	/*
9129aa23310Srie 	 * If the object could not be found it is possible that the "not-found"
9139aa23310Srie 	 * AVL tree had indicated that the file does not exist.  In case the
91456deab07SRod Evans 	 * file system has changed since this "not-found" recording was made,
9159aa23310Srie 	 * retry the dlopen() with a clean "not-found" AVL tree.
9167c478bd9Sstevel@tonic-gate 	 */
917b4059b01SRod Evans 	if ((ghp == NULL) && in_nfavl) {
9189aa23310Srie 		avl_tree_t	*oavlt = nfavl;
9199aa23310Srie 
9209aa23310Srie 		nfavl = NULL;
921dde769a2SRod Evans 		ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig,
922dde769a2SRod Evans 		    NULL);
9239aa23310Srie 
9249aa23310Srie 		/*
9259aa23310Srie 		 * If the file is found, then its full path name will have been
9269aa23310Srie 		 * registered in the FullPath AVL tree.  Remove any new
9279aa23310Srie 		 * "not-found" AVL information, and restore the former AVL tree.
9289aa23310Srie 		 */
9299aa23310Srie 		nfavl_remove(nfavl);
9309aa23310Srie 		nfavl = oavlt;
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	/*
9349aa23310Srie 	 * Establish the new link-map from which .init processing will begin.
9359aa23310Srie 	 * Ignore .init firing when constructing a configuration file (crle(1)).
9369aa23310Srie 	 */
9379aa23310Srie 	if (ghp && ((mode & RTLD_CONFGEN) == 0))
9389aa23310Srie 		dlmp = ghp->gh_ownlmp;
9399aa23310Srie 
9409aa23310Srie 	/*
94102ca3e02Srie 	 * If loading an auditor was requested, and the auditor already existed,
94202ca3e02Srie 	 * then the link-map returned will be to the original auditor.  Remove
94302ca3e02Srie 	 * the link-map control list that was created for this request.
94402ca3e02Srie 	 */
94502ca3e02Srie 	if (dlmp && (flags & FLG_RT_AUDIT) && (LIST(dlmp) != lml)) {
94602ca3e02Srie 		remove_lml(lml);
94702ca3e02Srie 		lml = LIST(dlmp);
94802ca3e02Srie 	}
94902ca3e02Srie 
95002ca3e02Srie 	/*
95102ca3e02Srie 	 * If this load failed, remove any alternative link-map list.
95202ca3e02Srie 	 */
953b4059b01SRod Evans 	if ((ghp == NULL) &&
95402ca3e02Srie 	    ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)) {
95502ca3e02Srie 		remove_lml(lml);
956b4059b01SRod Evans 		lml = NULL;
95702ca3e02Srie 	}
95802ca3e02Srie 
95902ca3e02Srie 	/*
96002ca3e02Srie 	 * Finish this load request.  If objects were loaded, .init processing
96102ca3e02Srie 	 * is computed.  Finally, the debuggers are informed of the link-map
96202ca3e02Srie 	 * lists being stable.
96302ca3e02Srie 	 */
9647247f888Srie 	load_completion(dlmp);
96502ca3e02Srie 
9667c478bd9Sstevel@tonic-gate 	return (ghp);
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate /*
9707c478bd9Sstevel@tonic-gate  * Argument checking for dlopen.  Only called via external entry.
9717c478bd9Sstevel@tonic-gate  */
9727c478bd9Sstevel@tonic-gate static Grp_hdl *
dlmopen_check(Lm_list * lml,const char * path,int mode,Rt_map * clmp)9739aa23310Srie dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 	/*
9767c478bd9Sstevel@tonic-gate 	 * Verify that a valid pathname has been supplied.
9777c478bd9Sstevel@tonic-gate 	 */
9787c478bd9Sstevel@tonic-gate 	if (path && (*path == '\0')) {
9795aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
9807c478bd9Sstevel@tonic-gate 		return (0);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	/*
9847c478bd9Sstevel@tonic-gate 	 * Historically we've always verified the mode is either RTLD_NOW or
9857c478bd9Sstevel@tonic-gate 	 * RTLD_LAZY.  RTLD_NOLOAD is valid by itself.  Use of LM_ID_NEWLM
9867c478bd9Sstevel@tonic-gate 	 * requires a specific pathname, and use of RTLD_PARENT is meaningless.
9877c478bd9Sstevel@tonic-gate 	 */
9887c478bd9Sstevel@tonic-gate 	if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) {
9895aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1));
9907c478bd9Sstevel@tonic-gate 		return (0);
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 	if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) {
9935aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2));
9947c478bd9Sstevel@tonic-gate 		return (0);
9957c478bd9Sstevel@tonic-gate 	}
996b4059b01SRod Evans 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == NULL)) {
9975aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3));
9987c478bd9Sstevel@tonic-gate 		return (0);
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) {
10015aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_4));
10027c478bd9Sstevel@tonic-gate 		return (0);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10059aa23310Srie 	return (dlmopen_intn(lml, path, mode, clmp, 0, 0));
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087257d1b4Sraf #pragma weak _dlopen = dlopen
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate /*
10117c478bd9Sstevel@tonic-gate  * External entry for dlopen(3dl).  On success, returns a pointer (handle) to
10127c478bd9Sstevel@tonic-gate  * the structure containing information about the newly added object, ie. can
10137c478bd9Sstevel@tonic-gate  * be used by dlsym(). On failure, returns a null pointer.
10147c478bd9Sstevel@tonic-gate  */
10157c478bd9Sstevel@tonic-gate void *
dlopen(const char * path,int mode)10167257d1b4Sraf dlopen(const char *path, int mode)
10177c478bd9Sstevel@tonic-gate {
10189aa23310Srie 	int	entry;
10197c478bd9Sstevel@tonic-gate 	Rt_map	*clmp;
10207c478bd9Sstevel@tonic-gate 	Grp_hdl	*ghp;
10217c478bd9Sstevel@tonic-gate 	Lm_list	*lml;
10227c478bd9Sstevel@tonic-gate 
10238cd45542Sraf 	entry = enter(0);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
10267c478bd9Sstevel@tonic-gate 	lml = LIST(clmp);
10277c478bd9Sstevel@tonic-gate 
10289aa23310Srie 	ghp = dlmopen_check(lml, path, mode, clmp);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	if (entry)
10318cd45542Sraf 		leave(lml, 0);
10327c478bd9Sstevel@tonic-gate 	return ((void *)ghp);
10337c478bd9Sstevel@tonic-gate }
10347c478bd9Sstevel@tonic-gate 
10357257d1b4Sraf #pragma weak _dlmopen = dlmopen
10367257d1b4Sraf 
10377c478bd9Sstevel@tonic-gate /*
10387c478bd9Sstevel@tonic-gate  * External entry for dlmopen(3dl).
10397c478bd9Sstevel@tonic-gate  */
10407c478bd9Sstevel@tonic-gate void *
dlmopen(Lmid_t lmid,const char * path,int mode)10417257d1b4Sraf dlmopen(Lmid_t lmid, const char *path, int mode)
10427c478bd9Sstevel@tonic-gate {
10439aa23310Srie 	int	entry;
10447c478bd9Sstevel@tonic-gate 	Rt_map	*clmp;
10457c478bd9Sstevel@tonic-gate 	Grp_hdl	*ghp;
10467c478bd9Sstevel@tonic-gate 
10478cd45542Sraf 	entry = enter(0);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
10507c478bd9Sstevel@tonic-gate 
10519aa23310Srie 	ghp = dlmopen_check((Lm_list *)lmid, path, mode, clmp);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	if (entry)
10548cd45542Sraf 		leave(LIST(clmp), 0);
10557c478bd9Sstevel@tonic-gate 	return ((void *)ghp);
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate /*
10597c478bd9Sstevel@tonic-gate  * Handle processing for dlsym.
10607c478bd9Sstevel@tonic-gate  */
106108278a5eSRod Evans int
dlsym_handle(Grp_hdl * ghp,Slookup * slp,Sresult * srp,uint_t * binfo,int * in_nfavl)106208278a5eSRod Evans dlsym_handle(Grp_hdl *ghp, Slookup *slp, Sresult *srp, uint_t *binfo,
10639aa23310Srie     int *in_nfavl)
10647c478bd9Sstevel@tonic-gate {
10655aefb655Srie 	Rt_map		*nlmp, * lmp = ghp->gh_ownlmp;
10667c478bd9Sstevel@tonic-gate 	Rt_map		*clmp = slp->sl_cmap;
10677c478bd9Sstevel@tonic-gate 	const char	*name = slp->sl_name;
10687c478bd9Sstevel@tonic-gate 	Slookup		sl = *slp;
10697c478bd9Sstevel@tonic-gate 
107008278a5eSRod Evans 	sl.sl_flags = (LKUP_FIRST | LKUP_DLSYM | LKUP_SPEC);
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	/*
10737c478bd9Sstevel@tonic-gate 	 * Continue processing a dlsym request.  Lookup the required symbol in
10747c478bd9Sstevel@tonic-gate 	 * each link-map specified by the handle.
10757c478bd9Sstevel@tonic-gate 	 *
10767c478bd9Sstevel@tonic-gate 	 * To leverage off of lazy loading, dlsym() requests can result in two
10777c478bd9Sstevel@tonic-gate 	 * passes.  The first descends the link-maps of any objects already in
10787c478bd9Sstevel@tonic-gate 	 * the address space.  If the symbol isn't located, and lazy
10797c478bd9Sstevel@tonic-gate 	 * dependencies still exist, then a second pass is made to load these
10807c478bd9Sstevel@tonic-gate 	 * dependencies if applicable.  This model means that in the case where
1081e0e63816SRod Evans 	 * a symbol exists in more than one object, the one located may not be
10827c478bd9Sstevel@tonic-gate 	 * constant - this is the standard issue with lazy loading. In addition,
10837c478bd9Sstevel@tonic-gate 	 * attempting to locate a symbol that doesn't exist will result in the
10847c478bd9Sstevel@tonic-gate 	 * loading of all lazy dependencies on the given handle, which can
10857c478bd9Sstevel@tonic-gate 	 * defeat some of the advantages of lazy loading (look out JVM).
10867c478bd9Sstevel@tonic-gate 	 */
10877c478bd9Sstevel@tonic-gate 	if (ghp->gh_flags & GPH_ZERO) {
10882926dd2eSrie 		Lm_list	*lml;
1089e0e63816SRod Evans 		uint_t	lazy = 0;
10902926dd2eSrie 
10917c478bd9Sstevel@tonic-gate 		/*
10927c478bd9Sstevel@tonic-gate 		 * If this symbol lookup is triggered from a dlopen(0) handle,
10937c478bd9Sstevel@tonic-gate 		 * traverse the present link-map list looking for promiscuous
10947c478bd9Sstevel@tonic-gate 		 * entries.
10957c478bd9Sstevel@tonic-gate 		 */
1096cb511613SAli Bahrami 		for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
10977c478bd9Sstevel@tonic-gate 			/*
10987c478bd9Sstevel@tonic-gate 			 * If this handle indicates we're only to look in the
10997c478bd9Sstevel@tonic-gate 			 * first object check whether we're done.
11007c478bd9Sstevel@tonic-gate 			 */
11017c478bd9Sstevel@tonic-gate 			if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST))
110208278a5eSRod Evans 				return (0);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 			if (!(MODE(nlmp) & RTLD_GLOBAL))
11057c478bd9Sstevel@tonic-gate 				continue;
11067c478bd9Sstevel@tonic-gate 			if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
11077c478bd9Sstevel@tonic-gate 			    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
11087c478bd9Sstevel@tonic-gate 				continue;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 			sl.sl_imap = nlmp;
111108278a5eSRod Evans 			if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl))
111208278a5eSRod Evans 				return (1);
1113e0e63816SRod Evans 
1114e0e63816SRod Evans 			/*
1115e0e63816SRod Evans 			 * Keep track of any global pending lazy loads.
1116e0e63816SRod Evans 			 */
1117e0e63816SRod Evans 			lazy += LAZY(nlmp);
11187c478bd9Sstevel@tonic-gate 		}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 		/*
1121e0e63816SRod Evans 		 * If we're unable to locate the symbol and this link-map list
1122e0e63816SRod Evans 		 * still has pending lazy dependencies, start loading them in an
11237c478bd9Sstevel@tonic-gate 		 * attempt to exhaust the search.  Note that as we're already
11247c478bd9Sstevel@tonic-gate 		 * traversing a dynamic linked list of link-maps there's no
11257c478bd9Sstevel@tonic-gate 		 * need for elf_lazy_find_sym() to descend the link-maps itself.
11267c478bd9Sstevel@tonic-gate 		 */
11272926dd2eSrie 		lml = LIST(lmp);
1128e0e63816SRod Evans 		if (lazy) {
11292926dd2eSrie 			DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 			sl.sl_flags |= LKUP_NODESCENT;
11327c478bd9Sstevel@tonic-gate 
1133cb511613SAli Bahrami 			for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 				if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp))
11367c478bd9Sstevel@tonic-gate 					continue;
11377c478bd9Sstevel@tonic-gate 				if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
11387c478bd9Sstevel@tonic-gate 				    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
11397c478bd9Sstevel@tonic-gate 					continue;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 				sl.sl_imap = nlmp;
114208278a5eSRod Evans 				if (elf_lazy_find_sym(&sl, srp, binfo,
11439aa23310Srie 				    in_nfavl))
114408278a5eSRod Evans 					return (1);
11457c478bd9Sstevel@tonic-gate 			}
11467c478bd9Sstevel@tonic-gate 		}
11477c478bd9Sstevel@tonic-gate 	} else {
11487c478bd9Sstevel@tonic-gate 		/*
1149e0e63816SRod Evans 		 * Traverse the dlopen() handle searching all presently loaded
11507c478bd9Sstevel@tonic-gate 		 * link-maps.
11517c478bd9Sstevel@tonic-gate 		 */
11527c478bd9Sstevel@tonic-gate 		Grp_desc	*gdp;
1153cce0e03bSab196087 		Aliste		idx;
1154e0e63816SRod Evans 		uint_t		lazy = 0;
11557c478bd9Sstevel@tonic-gate 
1156cce0e03bSab196087 		for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1157e0e63816SRod Evans 			nlmp = gdp->gd_depend;
1158e0e63816SRod Evans 
1159efb9e8b8Srie 			if ((gdp->gd_flags & GPD_DLSYM) == 0)
11607c478bd9Sstevel@tonic-gate 				continue;
11617c478bd9Sstevel@tonic-gate 
1162e0e63816SRod Evans 			sl.sl_imap = nlmp;
116308278a5eSRod Evans 			if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl))
116408278a5eSRod Evans 				return (1);
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 			if (ghp->gh_flags & GPH_FIRST)
116708278a5eSRod Evans 				return (0);
1168e0e63816SRod Evans 
1169e0e63816SRod Evans 			/*
1170e0e63816SRod Evans 			 * Keep track of any pending lazy loads associated
1171e0e63816SRod Evans 			 * with this handle.
1172e0e63816SRod Evans 			 */
1173e0e63816SRod Evans 			lazy += LAZY(nlmp);
11747c478bd9Sstevel@tonic-gate 		}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 		/*
1177e0e63816SRod Evans 		 * If we're unable to locate the symbol and this handle still
1178e0e63816SRod Evans 		 * has pending lazy dependencies, start loading the lazy
1179e0e63816SRod Evans 		 * dependencies, in an attempt to exhaust the search.
11807c478bd9Sstevel@tonic-gate 		 */
1181e0e63816SRod Evans 		if (lazy) {
11825aefb655Srie 			DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name));
11837c478bd9Sstevel@tonic-gate 
1184cce0e03bSab196087 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
11857c478bd9Sstevel@tonic-gate 				nlmp = gdp->gd_depend;
11867c478bd9Sstevel@tonic-gate 
1187efb9e8b8Srie 				if (((gdp->gd_flags & GPD_DLSYM) == 0) ||
11887c478bd9Sstevel@tonic-gate 				    (LAZY(nlmp) == 0))
11897c478bd9Sstevel@tonic-gate 					continue;
11902926dd2eSrie 
11917c478bd9Sstevel@tonic-gate 				sl.sl_imap = nlmp;
119208278a5eSRod Evans 				if (elf_lazy_find_sym(&sl, srp, binfo,
119308278a5eSRod Evans 				    in_nfavl))
119408278a5eSRod Evans 					return (1);
11957c478bd9Sstevel@tonic-gate 			}
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate 	}
119808278a5eSRod Evans 	return (0);
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate /*
1202f441771bSRod Evans  * Determine whether a symbol resides in a caller.  This may be a reference,
1203f441771bSRod Evans  * which is associated with a specific dependency.
1204f441771bSRod Evans  */
1205f441771bSRod Evans inline static Sym *
sym_lookup_in_caller(Rt_map * clmp,Slookup * slp,Sresult * srp,uint_t * binfo)1206f441771bSRod Evans sym_lookup_in_caller(Rt_map *clmp, Slookup *slp, Sresult *srp, uint_t *binfo)
1207f441771bSRod Evans {
1208f441771bSRod Evans 	if (THIS_IS_ELF(clmp) && SYMINTP(clmp)(slp, srp, binfo, NULL)) {
1209f441771bSRod Evans 		Sym	*sym = srp->sr_sym;
1210f441771bSRod Evans 
1211f441771bSRod Evans 		slp->sl_rsymndx = (((ulong_t)sym -
1212f441771bSRod Evans 		    (ulong_t)SYMTAB(clmp)) / SYMENT(clmp));
1213f441771bSRod Evans 		slp->sl_rsym = sym;
1214f441771bSRod Evans 		return (sym);
1215f441771bSRod Evans 	}
1216f441771bSRod Evans 	return (NULL);
1217f441771bSRod Evans }
1218f441771bSRod Evans 
1219f441771bSRod Evans /*
12207c478bd9Sstevel@tonic-gate  * Core dlsym activity.  Selects symbol lookup method from handle.
12217c478bd9Sstevel@tonic-gate  */
122208278a5eSRod Evans static void *
dlsym_core(void * handle,const char * name,Rt_map * clmp,Rt_map ** dlmp,int * in_nfavl)12239aa23310Srie dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp,
12249aa23310Srie     int *in_nfavl)
12257c478bd9Sstevel@tonic-gate {
1226f441771bSRod Evans 	Sym		*sym;
122708278a5eSRod Evans 	int		ret = 0;
1228660acd81Srie 	Syminfo		*sip;
12297c478bd9Sstevel@tonic-gate 	Slookup		sl;
123008278a5eSRod Evans 	Sresult		sr;
12317c478bd9Sstevel@tonic-gate 	uint_t		binfo;
12327c478bd9Sstevel@tonic-gate 
1233660acd81Srie 	/*
123475e7992aSrie 	 * Initialize the symbol lookup data structure.
123575e7992aSrie 	 *
1236660acd81Srie 	 * Standard relocations are evaluated using the symbol index of the
1237660acd81Srie 	 * associated relocation symbol.  This index provides for loading
1238660acd81Srie 	 * any lazy dependency and establishing a direct binding if necessary.
1239660acd81Srie 	 * If a dlsym() operation originates from an object that contains a
124075e7992aSrie 	 * symbol table entry for the same name, then we need to establish the
124175e7992aSrie 	 * symbol index so that any dependency requirements can be triggered.
124275e7992aSrie 	 *
124375e7992aSrie 	 * Therefore, the first symbol lookup that is carried out is for the
124475e7992aSrie 	 * symbol name within the calling object.  If this symbol exists, the
124575e7992aSrie 	 * symbols index is computed, added to the Slookup data, and thus used
124675e7992aSrie 	 * to seed the real symbol lookup.
1247660acd81Srie 	 */
124875e7992aSrie 	SLOOKUP_INIT(sl, name, clmp, clmp, ld_entry_cnt, elf_hash(name),
124975e7992aSrie 	    0, 0, 0, LKUP_SYMNDX);
125008278a5eSRod Evans 	SRESULT_INIT(sr, name);
1251f441771bSRod Evans 	sym = sym_lookup_in_caller(clmp, &sl, &sr, &binfo);
1252660acd81Srie 
125308278a5eSRod Evans 	SRESULT_INIT(sr, name);
125408278a5eSRod Evans 
125560758829Srie 	if (sym && (ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON)) {
125660758829Srie 		Rt_map	*hlmp = LIST(clmp)->lm_head;
125760758829Srie 
125860758829Srie 		/*
125960758829Srie 		 * If a symbol reference is known, and that reference indicates
126060758829Srie 		 * that the symbol is a singleton, then the search for the
126160758829Srie 		 * symbol must follow the default search path.
126260758829Srie 		 */
126398c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0,
12649aa23310Srie 		    DBG_DLSYM_SINGLETON));
126560758829Srie 
126660758829Srie 		sl.sl_imap = hlmp;
126760758829Srie 		if (handle == RTLD_PROBE)
1268f441771bSRod Evans 			sl.sl_flags = LKUP_NOFALLBACK;
1269f441771bSRod Evans 		else
1270f441771bSRod Evans 			sl.sl_flags = LKUP_SPEC;
127108278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
127260758829Srie 
127360758829Srie 	} else if (handle == RTLD_NEXT) {
12747c478bd9Sstevel@tonic-gate 		Rt_map	*nlmp;
12757c478bd9Sstevel@tonic-gate 
12767c478bd9Sstevel@tonic-gate 		/*
1277660acd81Srie 		 * If this handle is RTLD_NEXT determine whether a lazy load
1278660acd81Srie 		 * from the caller might provide the next object.  This mimics
1279660acd81Srie 		 * the lazy loading initialization normally carried out by
1280660acd81Srie 		 * lookup_sym(), however here, we must do this up-front, as
1281660acd81Srie 		 * lookup_sym() will be used to inspect the next object.
1282660acd81Srie 		 */
1283b4059b01SRod Evans 		if ((sl.sl_rsymndx) && ((sip = SYMINFO(clmp)) != NULL)) {
1284660acd81Srie 			/* LINTED */
1285660acd81Srie 			sip = (Syminfo *)((char *)sip +
1286660acd81Srie 			    (sl.sl_rsymndx * SYMINENT(clmp)));
1287660acd81Srie 
1288660acd81Srie 			if ((sip->si_flags & SYMINFO_FLG_DIRECT) &&
1289660acd81Srie 			    (sip->si_boundto < SYMINFO_BT_LOWRESERVE))
129075e7992aSrie 				(void) elf_lazy_load(clmp, &sl,
12912017c965SRod Evans 				    sip->si_boundto, name, 0, NULL, in_nfavl);
1292660acd81Srie 
1293660acd81Srie 			/*
1294660acd81Srie 			 * Clear the symbol index, so as not to confuse
1295660acd81Srie 			 * lookup_sym() of the next object.
1296660acd81Srie 			 */
1297660acd81Srie 			sl.sl_rsymndx = 0;
1298b4059b01SRod Evans 			sl.sl_rsym = NULL;
1299660acd81Srie 		}
1300660acd81Srie 
1301660acd81Srie 		/*
1302f441771bSRod Evans 		 * If the handle is RTLD_NEXT, start searching in the next link
13037c478bd9Sstevel@tonic-gate 		 * map from the callers.  Determine permissions from the
13047c478bd9Sstevel@tonic-gate 		 * present link map.  Indicate to lookup_sym() that we're on an
13057c478bd9Sstevel@tonic-gate 		 * RTLD_NEXT request so that it will use the callers link map to
13067c478bd9Sstevel@tonic-gate 		 * start any possible lazy dependency loading.
13077c478bd9Sstevel@tonic-gate 		 */
1308cb511613SAli Bahrami 		sl.sl_imap = nlmp = NEXT_RT_MAP(clmp);
13097c478bd9Sstevel@tonic-gate 
131098c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl,
13119aa23310Srie 		    (nlmp ? NAME(nlmp) : MSG_INTL(MSG_STR_NULL)),
13129aa23310Srie 		    DBG_DLSYM_NEXT));
13137c478bd9Sstevel@tonic-gate 
1314b4059b01SRod Evans 		if (nlmp == NULL)
13157c478bd9Sstevel@tonic-gate 			return (0);
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate 		sl.sl_flags = LKUP_NEXT;
131808278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	} else if (handle == RTLD_SELF) {
13217c478bd9Sstevel@tonic-gate 		/*
13227c478bd9Sstevel@tonic-gate 		 * If the handle is RTLD_SELF start searching from the caller.
13237c478bd9Sstevel@tonic-gate 		 */
132498c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, NAME(clmp),
13257c478bd9Sstevel@tonic-gate 		    DBG_DLSYM_SELF));
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate 		sl.sl_imap = clmp;
132860758829Srie 		sl.sl_flags = (LKUP_SPEC | LKUP_SELF);
132908278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
13307c478bd9Sstevel@tonic-gate 
1331660acd81Srie 	} else if (handle == RTLD_DEFAULT) {
13327c478bd9Sstevel@tonic-gate 		Rt_map	*hlmp = LIST(clmp)->lm_head;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 		/*
1335660acd81Srie 		 * If the handle is RTLD_DEFAULT mimic the standard symbol
1336660acd81Srie 		 * lookup as would be triggered by a relocation.
13377c478bd9Sstevel@tonic-gate 		 */
133898c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0,
13399aa23310Srie 		    DBG_DLSYM_DEFAULT));
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 		sl.sl_imap = hlmp;
13427c478bd9Sstevel@tonic-gate 		sl.sl_flags = LKUP_SPEC;
134308278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
1344660acd81Srie 
1345660acd81Srie 	} else if (handle == RTLD_PROBE) {
1346660acd81Srie 		Rt_map	*hlmp = LIST(clmp)->lm_head;
1347660acd81Srie 
1348660acd81Srie 		/*
1349660acd81Srie 		 * If the handle is RTLD_PROBE, mimic the standard symbol
1350660acd81Srie 		 * lookup as would be triggered by a relocation, however do
1351660acd81Srie 		 * not fall back to a lazy loading rescan if the symbol can't be
1352660acd81Srie 		 * found within the currently loaded objects.  Note, a lazy
1353660acd81Srie 		 * loaded dependency required by the caller might still get
1354660acd81Srie 		 * loaded to satisfy this request, but no exhaustive lazy load
1355660acd81Srie 		 * rescan is carried out.
1356660acd81Srie 		 */
135798c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl, 0,
13589aa23310Srie 		    DBG_DLSYM_PROBE));
1359660acd81Srie 
1360660acd81Srie 		sl.sl_imap = hlmp;
1361f441771bSRod Evans 		sl.sl_flags = LKUP_NOFALLBACK;
136208278a5eSRod Evans 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
1363660acd81Srie 
13647c478bd9Sstevel@tonic-gate 	} else {
13657c478bd9Sstevel@tonic-gate 		Grp_hdl *ghp = (Grp_hdl *)handle;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		/*
13687c478bd9Sstevel@tonic-gate 		 * Look in the shared object specified by the handle and in all
13697c478bd9Sstevel@tonic-gate 		 * of its dependencies.
13707c478bd9Sstevel@tonic-gate 		 */
137198c080d5SRod Evans 		DBG_CALL(Dbg_dl_dlsym(clmp, name, in_nfavl,
13729aa23310Srie 		    NAME(ghp->gh_ownlmp), DBG_DLSYM_DEF));
13732926dd2eSrie 
137408278a5eSRod Evans 		ret = LM_DLSYM(clmp)(ghp, &sl, &sr, &binfo, in_nfavl);
13757c478bd9Sstevel@tonic-gate 	}
13767c478bd9Sstevel@tonic-gate 
137708278a5eSRod Evans 	if (ret && ((sym = sr.sr_sym) != NULL)) {
13785aefb655Srie 		Lm_list	*lml = LIST(clmp);
13797c478bd9Sstevel@tonic-gate 		Addr	addr = sym->st_value;
13807c478bd9Sstevel@tonic-gate 
138108278a5eSRod Evans 		*dlmp = sr.sr_dmap;
13827c478bd9Sstevel@tonic-gate 		if (!(FLAGS(*dlmp) & FLG_RT_FIXED))
13837c478bd9Sstevel@tonic-gate 			addr += ADDR(*dlmp);
13847c478bd9Sstevel@tonic-gate 
13859aa23310Srie 		/*
13869aa23310Srie 		 * Indicate that the defining object is now used.
13879aa23310Srie 		 */
13889aa23310Srie 		if (*dlmp != clmp)
13899aa23310Srie 			FLAGS1(*dlmp) |= FL1_RT_USED;
13909aa23310Srie 
13915aefb655Srie 		DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE,
139208278a5eSRod Evans 		    *dlmp, addr, sym->st_value, sr.sr_name, binfo));
13937c478bd9Sstevel@tonic-gate 
1394*38f4bdddSBryan Cantrill 		if ((lml->lm_tflags | AFLAGS(clmp) | AFLAGS(*dlmp)) &
1395*38f4bdddSBryan Cantrill 		    LML_TFLG_AUD_SYMBIND) {
13967c478bd9Sstevel@tonic-gate 			uint_t	sb_flags = LA_SYMB_DLSYM;
13977c478bd9Sstevel@tonic-gate 			/* LINTED */
13987c478bd9Sstevel@tonic-gate 			uint_t	symndx = (uint_t)(((Xword)sym -
13997c478bd9Sstevel@tonic-gate 			    (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp));
14007c478bd9Sstevel@tonic-gate 			addr = audit_symbind(clmp, *dlmp, sym, symndx, addr,
14017c478bd9Sstevel@tonic-gate 			    &sb_flags);
14027c478bd9Sstevel@tonic-gate 		}
14037c478bd9Sstevel@tonic-gate 		return ((void *)addr);
140408278a5eSRod Evans 	}
140508278a5eSRod Evans 
140608278a5eSRod Evans 	return (NULL);
14077c478bd9Sstevel@tonic-gate }
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate /*
14107c478bd9Sstevel@tonic-gate  * Internal dlsym activity.  Called from user level or directly for internal
14117c478bd9Sstevel@tonic-gate  * symbol lookup.
14127c478bd9Sstevel@tonic-gate  */
14137c478bd9Sstevel@tonic-gate void *
dlsym_intn(void * handle,const char * name,Rt_map * clmp,Rt_map ** dlmp)14147c478bd9Sstevel@tonic-gate dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
14157c478bd9Sstevel@tonic-gate {
1416b4059b01SRod Evans 	Rt_map		*llmp = NULL;
14177c478bd9Sstevel@tonic-gate 	void		*error;
1418cce0e03bSab196087 	Aliste		idx;
14197c478bd9Sstevel@tonic-gate 	Grp_desc	*gdp;
14209aa23310Srie 	int		in_nfavl = 0;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	/*
14237c478bd9Sstevel@tonic-gate 	 * While looking for symbols it's quite possible that additional objects
14247c478bd9Sstevel@tonic-gate 	 * get loaded from lazy loading.  These objects will have been added to
14257c478bd9Sstevel@tonic-gate 	 * the same link-map list as those objects on the handle.  Remember this
14267c478bd9Sstevel@tonic-gate 	 * list for later investigation.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) ||
14297c478bd9Sstevel@tonic-gate 	    (handle == RTLD_SELF) || (handle == RTLD_PROBE))
14307c478bd9Sstevel@tonic-gate 		llmp = LIST(clmp)->lm_tail;
14317c478bd9Sstevel@tonic-gate 	else {
14327c478bd9Sstevel@tonic-gate 		Grp_hdl	*ghp = (Grp_hdl *)handle;
14337c478bd9Sstevel@tonic-gate 
14345aefb655Srie 		if (ghp->gh_ownlmp)
14355aefb655Srie 			llmp = LIST(ghp->gh_ownlmp)->lm_tail;
14367c478bd9Sstevel@tonic-gate 		else {
1437cce0e03bSab196087 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1438b4059b01SRod Evans 				if ((llmp =
1439b4059b01SRod Evans 				    LIST(gdp->gd_depend)->lm_tail) != NULL)
14407c478bd9Sstevel@tonic-gate 					break;
14417c478bd9Sstevel@tonic-gate 			}
14427c478bd9Sstevel@tonic-gate 		}
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14459aa23310Srie 	error = dlsym_core(handle, name, clmp, dlmp, &in_nfavl);
14469aa23310Srie 
14479aa23310Srie 	/*
14489aa23310Srie 	 * If the symbol could not be found it is possible that the "not-found"
14499aa23310Srie 	 * AVL tree had indicated that a required file does not exist.  In case
14509aa23310Srie 	 * the file system has changed since this "not-found" recording was
14519aa23310Srie 	 * made, retry the dlsym() with a clean "not-found" AVL tree.
14529aa23310Srie 	 */
1453b4059b01SRod Evans 	if ((error == NULL) && in_nfavl) {
14549aa23310Srie 		avl_tree_t	*oavlt = nfavl;
14559aa23310Srie 
14569aa23310Srie 		nfavl = NULL;
14579aa23310Srie 		error = dlsym_core(handle, name, clmp, dlmp, NULL);
14589aa23310Srie 
14599aa23310Srie 		/*
14609aa23310Srie 		 * If the symbol is found, then any file that was loaded will
14619aa23310Srie 		 * have had its full path name registered in the FullPath AVL
14629aa23310Srie 		 * tree.  Remove any new "not-found" AVL information, and
14639aa23310Srie 		 * restore the former AVL tree.
14649aa23310Srie 		 */
14659aa23310Srie 		nfavl_remove(nfavl);
14669aa23310Srie 		nfavl = oavlt;
14679aa23310Srie 	}
14689aa23310Srie 
1469b4059b01SRod Evans 	if (error == NULL) {
14707c478bd9Sstevel@tonic-gate 		/*
14717c478bd9Sstevel@tonic-gate 		 * Cache the error message, as Java tends to fall through this
14727c478bd9Sstevel@tonic-gate 		 * code many times.
14737c478bd9Sstevel@tonic-gate 		 */
1474b4059b01SRod Evans 		if (nosym_str == NULL)
14757c478bd9Sstevel@tonic-gate 			nosym_str = MSG_INTL(MSG_GEN_NOSYM);
14765aefb655Srie 		eprintf(LIST(clmp), ERR_FATAL, nosym_str, name);
14777c478bd9Sstevel@tonic-gate 	}
14787c478bd9Sstevel@tonic-gate 
14797247f888Srie 	load_completion(llmp);
14807c478bd9Sstevel@tonic-gate 	return (error);
14817c478bd9Sstevel@tonic-gate }
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate /*
14847c478bd9Sstevel@tonic-gate  * Argument checking for dlsym.  Only called via external entry.
14857c478bd9Sstevel@tonic-gate  */
14867c478bd9Sstevel@tonic-gate static void *
dlsym_check(void * handle,const char * name,Rt_map * clmp,Rt_map ** dlmp)14877c478bd9Sstevel@tonic-gate dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
14887c478bd9Sstevel@tonic-gate {
14897c478bd9Sstevel@tonic-gate 	/*
14907c478bd9Sstevel@tonic-gate 	 * Verify the arguments.
14917c478bd9Sstevel@tonic-gate 	 */
1492b4059b01SRod Evans 	if (name == NULL) {
14935aefb655Srie 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM));
1494b4059b01SRod Evans 		return (NULL);
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 	if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) &&
14977c478bd9Sstevel@tonic-gate 	    (handle != RTLD_SELF) && (handle != RTLD_PROBE) &&
14987c478bd9Sstevel@tonic-gate 	    (hdl_validate((Grp_hdl *)handle) == 0)) {
1499b4059b01SRod Evans 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
1500b4059b01SRod Evans 		    EC_NATPTR(handle));
1501b4059b01SRod Evans 		return (NULL);
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 	return (dlsym_intn(handle, name, clmp, dlmp));
15047c478bd9Sstevel@tonic-gate }
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 
15077257d1b4Sraf #pragma weak _dlsym = dlsym
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate /*
15107c478bd9Sstevel@tonic-gate  * External entry for dlsym().  On success, returns the address of the specified
15117c478bd9Sstevel@tonic-gate  * symbol.  On error returns a null.
15127c478bd9Sstevel@tonic-gate  */
15137c478bd9Sstevel@tonic-gate void *
dlsym(void * handle,const char * name)15147257d1b4Sraf dlsym(void *handle, const char *name)
15157c478bd9Sstevel@tonic-gate {
15167c478bd9Sstevel@tonic-gate 	int	entry;
1517b4059b01SRod Evans 	Rt_map	*clmp, *dlmp = NULL;
15187c478bd9Sstevel@tonic-gate 	void	*addr;
15197c478bd9Sstevel@tonic-gate 
15208cd45542Sraf 	entry = enter(0);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	addr = dlsym_check(handle, name, clmp, &dlmp);
15257c478bd9Sstevel@tonic-gate 
152656deab07SRod Evans 	if (entry) {
15277c478bd9Sstevel@tonic-gate 		if (dlmp)
15287c478bd9Sstevel@tonic-gate 			is_dep_init(dlmp, clmp);
15298cd45542Sraf 		leave(LIST(clmp), 0);
153056deab07SRod Evans 	}
15317c478bd9Sstevel@tonic-gate 	return (addr);
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate /*
15357c478bd9Sstevel@tonic-gate  * Core dladdr activity.
15367c478bd9Sstevel@tonic-gate  */
15377c478bd9Sstevel@tonic-gate static void
dladdr_core(Rt_map * almp,void * addr,Dl_info_t * dlip,void ** info,int flags)153898c080d5SRod Evans dladdr_core(Rt_map *almp, void *addr, Dl_info_t *dlip, void **info, int flags)
15397c478bd9Sstevel@tonic-gate {
15407c478bd9Sstevel@tonic-gate 	/*
15417c478bd9Sstevel@tonic-gate 	 * Set up generic information and any defaults.
15427c478bd9Sstevel@tonic-gate 	 */
154398c080d5SRod Evans 	dlip->dli_fname = PATHNAME(almp);
15447c478bd9Sstevel@tonic-gate 
154598c080d5SRod Evans 	dlip->dli_fbase = (void *)ADDR(almp);
1546b4059b01SRod Evans 	dlip->dli_sname = NULL;
1547b4059b01SRod Evans 	dlip->dli_saddr = NULL;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	/*
15507c478bd9Sstevel@tonic-gate 	 * Determine the nearest symbol to this address.
15517c478bd9Sstevel@tonic-gate 	 */
155298c080d5SRod Evans 	LM_DLADDR(almp)((ulong_t)addr, almp, dlip, info, flags);
15537c478bd9Sstevel@tonic-gate }
15547c478bd9Sstevel@tonic-gate 
15557257d1b4Sraf #pragma weak _dladdr = dladdr
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate /*
15587c478bd9Sstevel@tonic-gate  * External entry for dladdr(3dl) and dladdr1(3dl).  Returns an information
15597c478bd9Sstevel@tonic-gate  * structure that reflects the symbol closest to the address specified.
15607c478bd9Sstevel@tonic-gate  */
15617c478bd9Sstevel@tonic-gate int
dladdr(void * addr,Dl_info_t * dlip)1562959ee943SRod Evans dladdr(void *addr, Dl_info_t *dlip)
15637c478bd9Sstevel@tonic-gate {
156402938ba2SRod Evans 	int	entry, ret;
156598c080d5SRod Evans 	Rt_map	*clmp, *almp;
156602938ba2SRod Evans 	Lm_list	*clml;
15677c478bd9Sstevel@tonic-gate 
15688cd45542Sraf 	entry = enter(0);
15697c478bd9Sstevel@tonic-gate 
157098c080d5SRod Evans 	clmp = _caller(caller(), CL_EXECDEF);
157102938ba2SRod Evans 	clml = LIST(clmp);
157298c080d5SRod Evans 
157398c080d5SRod Evans 	DBG_CALL(Dbg_dl_dladdr(clmp, addr));
157498c080d5SRod Evans 
15757c478bd9Sstevel@tonic-gate 	/*
15767c478bd9Sstevel@tonic-gate 	 * Use our calling technique to determine what object is associated
15777c478bd9Sstevel@tonic-gate 	 * with the supplied address.  If a caller can't be determined,
15787c478bd9Sstevel@tonic-gate 	 * indicate the failure.
15797c478bd9Sstevel@tonic-gate 	 */
158098c080d5SRod Evans 	if ((almp = _caller(addr, CL_NONE)) == NULL) {
158102938ba2SRod Evans 		eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
15825aefb655Srie 		    EC_NATPTR(addr));
158302938ba2SRod Evans 		ret = 0;
15847c478bd9Sstevel@tonic-gate 	} else {
158598c080d5SRod Evans 		dladdr_core(almp, addr, dlip, 0, 0);
158602938ba2SRod Evans 		ret = 1;
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 	if (entry)
159002938ba2SRod Evans 		leave(clml, 0);
159102938ba2SRod Evans 	return (ret);
15927c478bd9Sstevel@tonic-gate }
15937c478bd9Sstevel@tonic-gate 
15947257d1b4Sraf #pragma weak _dladdr1 = dladdr1
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate int
dladdr1(void * addr,Dl_info_t * dlip,void ** info,int flags)1597959ee943SRod Evans dladdr1(void *addr, Dl_info_t *dlip, void **info, int flags)
15987c478bd9Sstevel@tonic-gate {
159998c080d5SRod Evans 	int	entry, ret = 1;
160098c080d5SRod Evans 	Rt_map	*clmp, *almp;
160198c080d5SRod Evans 	Lm_list	*clml;
160298c080d5SRod Evans 
160398c080d5SRod Evans 	entry = enter(0);
160498c080d5SRod Evans 
160598c080d5SRod Evans 	clmp = _caller(caller(), CL_EXECDEF);
160698c080d5SRod Evans 	clml = LIST(clmp);
160798c080d5SRod Evans 
160898c080d5SRod Evans 	DBG_CALL(Dbg_dl_dladdr(clmp, addr));
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	/*
16117c478bd9Sstevel@tonic-gate 	 * Validate any flags.
16127c478bd9Sstevel@tonic-gate 	 */
16137c478bd9Sstevel@tonic-gate 	if (flags) {
16147c478bd9Sstevel@tonic-gate 		int	request;
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 		if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) &&
16177c478bd9Sstevel@tonic-gate 		    (request != RTLD_DL_LINKMAP)) {
161898c080d5SRod Evans 			eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS),
16195aefb655Srie 			    flags);
162098c080d5SRod Evans 			ret = 0;
16217c478bd9Sstevel@tonic-gate 
162298c080d5SRod Evans 		} else if (info == NULL) {
162398c080d5SRod Evans 			eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO),
162498c080d5SRod Evans 			    flags);
162598c080d5SRod Evans 			ret = 0;
162698c080d5SRod Evans 		}
162798c080d5SRod Evans 	}
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	/*
16307c478bd9Sstevel@tonic-gate 	 * Use our calling technique to determine what object is associated
16317c478bd9Sstevel@tonic-gate 	 * with the supplied address.  If a caller can't be determined,
16327c478bd9Sstevel@tonic-gate 	 * indicate the failure.
16337c478bd9Sstevel@tonic-gate 	 */
163498c080d5SRod Evans 	if (ret) {
163598c080d5SRod Evans 		if ((almp = _caller(addr, CL_NONE)) == NULL) {
163698c080d5SRod Evans 			eprintf(clml, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
16375aefb655Srie 			    EC_NATPTR(addr));
163898c080d5SRod Evans 			ret = 0;
163998c080d5SRod Evans 		} else
164098c080d5SRod Evans 			dladdr_core(almp, addr, dlip, info, flags);
16417c478bd9Sstevel@tonic-gate 	}
16425aefb655Srie 
16437c478bd9Sstevel@tonic-gate 	if (entry)
164498c080d5SRod Evans 		leave(clml, 0);
164598c080d5SRod Evans 	return (ret);
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate /*
16497c478bd9Sstevel@tonic-gate  * Core dldump activity.
16507c478bd9Sstevel@tonic-gate  */
16517c478bd9Sstevel@tonic-gate static int
dldump_core(Rt_map * clmp,Rt_map * lmp,const char * ipath,const char * opath,int flags)165298c080d5SRod Evans dldump_core(Rt_map *clmp, Rt_map *lmp, const char *ipath, const char *opath,
165398c080d5SRod Evans     int flags)
16547c478bd9Sstevel@tonic-gate {
165598c080d5SRod Evans 	Lm_list	*lml = LIST(clmp);
16567c478bd9Sstevel@tonic-gate 	Addr	addr = 0;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	/*
16597c478bd9Sstevel@tonic-gate 	 * Verify any arguments first.
16607c478bd9Sstevel@tonic-gate 	 */
166198c080d5SRod Evans 	if ((opath == NULL) || (opath[0] == '\0') ||
166298c080d5SRod Evans 	    ((lmp == NULL) && (ipath[0] == '\0'))) {
16635aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
16647c478bd9Sstevel@tonic-gate 		return (1);
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	/*
16685aefb655Srie 	 * If an input file is specified make sure its one of our dependencies
16695aefb655Srie 	 * on the main link-map list.  Note, this has really all evolved for
16705aefb655Srie 	 * crle(), which uses libcrle.so on an alternative link-map to trigger
16715aefb655Srie 	 * dumping objects from the main link-map list.   If we ever want to
16725aefb655Srie 	 * dump objects from alternative link-maps, this model is going to
16735aefb655Srie 	 * have to be revisited.
16747c478bd9Sstevel@tonic-gate 	 */
167598c080d5SRod Evans 	if (lmp == NULL) {
1676b4059b01SRod Evans 		if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == NULL) {
16775aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE),
16785aefb655Srie 			    ipath);
16797c478bd9Sstevel@tonic-gate 			return (1);
16807c478bd9Sstevel@tonic-gate 		}
16817c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_ALTER) {
16825aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath);
16837c478bd9Sstevel@tonic-gate 			return (1);
16847c478bd9Sstevel@tonic-gate 		}
16857c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_NODUMP) {
16865aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP),
16875aefb655Srie 			    ipath);
16887c478bd9Sstevel@tonic-gate 			return (1);
16897c478bd9Sstevel@tonic-gate 		}
169098c080d5SRod Evans 	}
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	/*
16937c478bd9Sstevel@tonic-gate 	 * If the object being dump'ed isn't fixed identify its mapping.
16947c478bd9Sstevel@tonic-gate 	 */
16957c478bd9Sstevel@tonic-gate 	if (!(FLAGS(lmp) & FLG_RT_FIXED))
16967c478bd9Sstevel@tonic-gate 		addr = ADDR(lmp);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	/*
16997c478bd9Sstevel@tonic-gate 	 * As rt_dldump() will effectively lazy load the necessary support
17007c478bd9Sstevel@tonic-gate 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
17017c478bd9Sstevel@tonic-gate 	 */
17027c478bd9Sstevel@tonic-gate 	if (elf_rtld_load() == 0)
17037c478bd9Sstevel@tonic-gate 		return (0);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	/*
17067c478bd9Sstevel@tonic-gate 	 * Dump the required image.
17077c478bd9Sstevel@tonic-gate 	 */
17087c478bd9Sstevel@tonic-gate 	return (rt_dldump(lmp, opath, flags, addr));
17097c478bd9Sstevel@tonic-gate }
17107c478bd9Sstevel@tonic-gate 
17117257d1b4Sraf #pragma weak _dldump = dldump
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate /*
17145aefb655Srie  * External entry for dldump(3c).  Returns 0 on success, non-zero otherwise.
17157c478bd9Sstevel@tonic-gate  */
17167c478bd9Sstevel@tonic-gate int
dldump(const char * ipath,const char * opath,int flags)17177257d1b4Sraf dldump(const char *ipath, const char *opath, int flags)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate 	int	error, entry;
172098c080d5SRod Evans 	Rt_map	*clmp, *lmp;
17217c478bd9Sstevel@tonic-gate 
17228cd45542Sraf 	entry = enter(0);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
17257c478bd9Sstevel@tonic-gate 
172698c080d5SRod Evans 	if (ipath) {
172798c080d5SRod Evans 		lmp = NULL;
172898c080d5SRod Evans 	} else {
172998c080d5SRod Evans 		lmp = lml_main.lm_head;
173098c080d5SRod Evans 		ipath = NAME(lmp);
173198c080d5SRod Evans 	}
173298c080d5SRod Evans 
173398c080d5SRod Evans 	DBG_CALL(Dbg_dl_dldump(clmp, ipath, opath, flags));
173498c080d5SRod Evans 
173598c080d5SRod Evans 	error = dldump_core(clmp, lmp, ipath, opath, flags);
17367c478bd9Sstevel@tonic-gate 
17377c478bd9Sstevel@tonic-gate 	if (entry)
17388cd45542Sraf 		leave(LIST(clmp), 0);
17397c478bd9Sstevel@tonic-gate 	return (error);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate /*
17437c478bd9Sstevel@tonic-gate  * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by
17447c478bd9Sstevel@tonic-gate  * the rtld_db and dlmopen() interfaces.  It checks to see if the Link_map is
17457c478bd9Sstevel@tonic-gate  * one of the primary ones and if so returns it's special token:
17467c478bd9Sstevel@tonic-gate  *		LM_ID_BASE
17477c478bd9Sstevel@tonic-gate  *		LM_ID_LDSO
17487c478bd9Sstevel@tonic-gate  *
17497c478bd9Sstevel@tonic-gate  * If it's not one of the primary link_map id's it will instead returns a
17507c478bd9Sstevel@tonic-gate  * pointer to the Lm_list structure which uniquely identifies the Link_map.
17517c478bd9Sstevel@tonic-gate  */
17527c478bd9Sstevel@tonic-gate Lmid_t
get_linkmap_id(Lm_list * lml)17537c478bd9Sstevel@tonic-gate get_linkmap_id(Lm_list *lml)
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_BASELM)
17567c478bd9Sstevel@tonic-gate 		return (LM_ID_BASE);
17577c478bd9Sstevel@tonic-gate 	if (lml->lm_flags & LML_FLG_RTLDLM)
17587c478bd9Sstevel@tonic-gate 		return (LM_ID_LDSO);
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	return ((Lmid_t)lml);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate /*
1764f441771bSRod Evans  * Set a new deferred dependency name.
1765f441771bSRod Evans  */
1766f441771bSRod Evans static int
set_def_need(Lm_list * lml,Dyninfo * dyip,const char * name)1767f441771bSRod Evans set_def_need(Lm_list *lml, Dyninfo *dyip, const char *name)
1768f441771bSRod Evans {
1769f441771bSRod Evans 	/*
1770f441771bSRod Evans 	 * If this dependency has already been established, then this dlinfo()
1771f441771bSRod Evans 	 * call is too late.
1772f441771bSRod Evans 	 */
1773f441771bSRod Evans 	if (dyip->di_info) {
1774f441771bSRod Evans 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_DEPLOADED),
1775f441771bSRod Evans 		    dyip->di_name);
1776f441771bSRod Evans 		return (-1);
1777f441771bSRod Evans 	}
1778f441771bSRod Evans 
1779f441771bSRod Evans 	/*
1780f441771bSRod Evans 	 * Assign the new dependency name.
1781f441771bSRod Evans 	 */
1782f441771bSRod Evans 	DBG_CALL(Dbg_file_deferred(lml, dyip->di_name, name));
1783f441771bSRod Evans 	dyip->di_flags |= FLG_DI_DEF_DONE;
1784f441771bSRod Evans 	dyip->di_name = name;
1785f441771bSRod Evans 	return (0);
1786f441771bSRod Evans }
1787f441771bSRod Evans 
1788f441771bSRod Evans /*
178941072f3cSrie  * Extract information for a dlopen() handle.
17907c478bd9Sstevel@tonic-gate  */
17917c478bd9Sstevel@tonic-gate static int
dlinfo_core(void * handle,int request,void * p,Rt_map * clmp)17927c478bd9Sstevel@tonic-gate dlinfo_core(void *handle, int request, void *p, Rt_map *clmp)
17937c478bd9Sstevel@tonic-gate {
179498c080d5SRod Evans 	Conv_inv_buf_t	inv_buf;
179598c080d5SRod Evans 	char		*handlename;
17965aefb655Srie 	Lm_list		*lml = LIST(clmp);
179798c080d5SRod Evans 	Rt_map		*lmp = NULL;
17987c478bd9Sstevel@tonic-gate 
179998c080d5SRod Evans 	/*
180098c080d5SRod Evans 	 * Determine whether a handle is provided.  A handle isn't needed for
180198c080d5SRod Evans 	 * all operations, but it is validated here for the initial diagnostic.
180298c080d5SRod Evans 	 */
180398c080d5SRod Evans 	if (handle == RTLD_SELF) {
180498c080d5SRod Evans 		lmp = clmp;
180598c080d5SRod Evans 	} else {
180698c080d5SRod Evans 		Grp_hdl	*ghp = (Grp_hdl *)handle;
180798c080d5SRod Evans 
180898c080d5SRod Evans 		if (hdl_validate(ghp))
180998c080d5SRod Evans 			lmp = ghp->gh_ownlmp;
181098c080d5SRod Evans 	}
181198c080d5SRod Evans 	if (lmp) {
181298c080d5SRod Evans 		handlename = NAME(lmp);
181398c080d5SRod Evans 	} else {
181498c080d5SRod Evans 		(void) conv_invalid_val(&inv_buf, EC_NATPTR(handle), 0);
181598c080d5SRod Evans 		handlename = inv_buf.buf;
181698c080d5SRod Evans 	}
181798c080d5SRod Evans 
181898c080d5SRod Evans 	DBG_CALL(Dbg_dl_dlinfo(clmp, handlename, request, p));
181998c080d5SRod Evans 
182098c080d5SRod Evans 	/*
182198c080d5SRod Evans 	 * Validate the request and return buffer.
182298c080d5SRod Evans 	 */
1823b4059b01SRod Evans 	if ((request > RTLD_DI_MAX) || (p == NULL)) {
18245aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL));
18257c478bd9Sstevel@tonic-gate 		return (-1);
18267c478bd9Sstevel@tonic-gate 	}
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	/*
18297c478bd9Sstevel@tonic-gate 	 * Return configuration cache name and address.
18307c478bd9Sstevel@tonic-gate 	 */
18317c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_CONFIGADDR) {
1832959ee943SRod Evans 		Dl_info_t	*dlip = (Dl_info_t *)p;
18337c478bd9Sstevel@tonic-gate 
1834b4059b01SRod Evans 		if ((config->c_name == NULL) || (config->c_bgn == 0) ||
18357c478bd9Sstevel@tonic-gate 		    (config->c_end == 0)) {
18365aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG));
18377c478bd9Sstevel@tonic-gate 			return (-1);
18387c478bd9Sstevel@tonic-gate 		}
18397c478bd9Sstevel@tonic-gate 		dlip->dli_fname = config->c_name;
18407c478bd9Sstevel@tonic-gate 		dlip->dli_fbase = (void *)config->c_bgn;
18417c478bd9Sstevel@tonic-gate 		return (0);
18427c478bd9Sstevel@tonic-gate 	}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate 	/*
18457c478bd9Sstevel@tonic-gate 	 * Return profiled object name (used by ldprof audit library).
18467c478bd9Sstevel@tonic-gate 	 */
18477c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_PROFILENAME) {
184856deab07SRod Evans 		if (profile_name == NULL) {
18495aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME));
18507c478bd9Sstevel@tonic-gate 			return (-1);
18517c478bd9Sstevel@tonic-gate 		}
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 		*(const char **)p = profile_name;
18547c478bd9Sstevel@tonic-gate 		return (0);
18557c478bd9Sstevel@tonic-gate 	}
18567c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_PROFILEOUT) {
18577c478bd9Sstevel@tonic-gate 		/*
18587c478bd9Sstevel@tonic-gate 		 * If a profile destination directory hasn't been specified
18597c478bd9Sstevel@tonic-gate 		 * provide a default.
18607c478bd9Sstevel@tonic-gate 		 */
186156deab07SRod Evans 		if (profile_out == NULL)
18627c478bd9Sstevel@tonic-gate 			profile_out = MSG_ORIG(MSG_PTH_VARTMP);
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 		*(const char **)p = profile_out;
18657c478bd9Sstevel@tonic-gate 		return (0);
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/*
18697c478bd9Sstevel@tonic-gate 	 * Obtain or establish a termination signal.
18707c478bd9Sstevel@tonic-gate 	 */
18717c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_GETSIGNAL) {
18727c478bd9Sstevel@tonic-gate 		*(int *)p = killsig;
18737c478bd9Sstevel@tonic-gate 		return (0);
18747c478bd9Sstevel@tonic-gate 	}
18757c478bd9Sstevel@tonic-gate 
18767c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_SETSIGNAL) {
18777c478bd9Sstevel@tonic-gate 		sigset_t	set;
18787c478bd9Sstevel@tonic-gate 		int		sig = *(int *)p;
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 		/*
18817c478bd9Sstevel@tonic-gate 		 * Determine whether the signal is in range.
18827c478bd9Sstevel@tonic-gate 		 */
18837c478bd9Sstevel@tonic-gate 		(void) sigfillset(&set);
18847c478bd9Sstevel@tonic-gate 		if (sigismember(&set, sig) != 1) {
18855aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig);
18867c478bd9Sstevel@tonic-gate 			return (-1);
18877c478bd9Sstevel@tonic-gate 		}
18887c478bd9Sstevel@tonic-gate 
18897c478bd9Sstevel@tonic-gate 		killsig = sig;
18907c478bd9Sstevel@tonic-gate 		return (0);
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	/*
18947c478bd9Sstevel@tonic-gate 	 * For any other request a link-map is required.  Verify the handle.
18957c478bd9Sstevel@tonic-gate 	 */
189698c080d5SRod Evans 	if (lmp == NULL) {
1897b4059b01SRod Evans 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
1898b4059b01SRod Evans 		    EC_NATPTR(handle));
18997c478bd9Sstevel@tonic-gate 		return (-1);
19007c478bd9Sstevel@tonic-gate 	}
19017c478bd9Sstevel@tonic-gate 
19027c478bd9Sstevel@tonic-gate 	/*
190341072f3cSrie 	 * Obtain the process arguments, environment and auxv.  Note, as the
190441072f3cSrie 	 * environment can be modified by the user (putenv(3c)), reinitialize
190541072f3cSrie 	 * the environment pointer on each request.
190641072f3cSrie 	 */
190741072f3cSrie 	if (request == RTLD_DI_ARGSINFO) {
1908959ee943SRod Evans 		Dl_argsinfo_t	*aip = (Dl_argsinfo_t *)p;
190941072f3cSrie 		Lm_list		*lml = LIST(lmp);
191041072f3cSrie 
191141072f3cSrie 		*aip = argsinfo;
191241072f3cSrie 		if (lml->lm_flags & LML_FLG_ENVIRON)
191341072f3cSrie 			aip->dla_envp = *(lml->lm_environ);
191441072f3cSrie 
191541072f3cSrie 		return (0);
191641072f3cSrie 	}
191741072f3cSrie 
191841072f3cSrie 	/*
19197c478bd9Sstevel@tonic-gate 	 * Return Lmid_t of the Link-Map list that the specified object is
19207c478bd9Sstevel@tonic-gate 	 * loaded on.
19217c478bd9Sstevel@tonic-gate 	 */
19227c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_LMID) {
19237c478bd9Sstevel@tonic-gate 		*(Lmid_t *)p = get_linkmap_id(LIST(lmp));
19247c478bd9Sstevel@tonic-gate 		return (0);
19257c478bd9Sstevel@tonic-gate 	}
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	/*
19287c478bd9Sstevel@tonic-gate 	 * Return a pointer to the Link-Map structure associated with the
19297c478bd9Sstevel@tonic-gate 	 * specified object.
19307c478bd9Sstevel@tonic-gate 	 */
19317c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_LINKMAP) {
19327c478bd9Sstevel@tonic-gate 		*(Link_map **)p = (Link_map *)lmp;
19337c478bd9Sstevel@tonic-gate 		return (0);
19347c478bd9Sstevel@tonic-gate 	}
19357c478bd9Sstevel@tonic-gate 
19367c478bd9Sstevel@tonic-gate 	/*
19377c478bd9Sstevel@tonic-gate 	 * Return search path information, or the size of the buffer required
19387c478bd9Sstevel@tonic-gate 	 * to store the information.
19397c478bd9Sstevel@tonic-gate 	 */
19407c478bd9Sstevel@tonic-gate 	if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) {
194156deab07SRod Evans 		Spath_desc	sd = { search_rules, NULL, 0 };
194256deab07SRod Evans 		Pdesc		*pdp;
1943959ee943SRod Evans 		Dl_serinfo_t	*info;
1944959ee943SRod Evans 		Dl_serpath_t	*path;
19457c478bd9Sstevel@tonic-gate 		char		*strs;
1946959ee943SRod Evans 		size_t		size = sizeof (Dl_serinfo_t);
19477c478bd9Sstevel@tonic-gate 		uint_t		cnt = 0;
19487c478bd9Sstevel@tonic-gate 
1949959ee943SRod Evans 		info = (Dl_serinfo_t *)p;
19507c478bd9Sstevel@tonic-gate 		path = &info->dls_serpath[0];
19517c478bd9Sstevel@tonic-gate 		strs = (char *)&info->dls_serpath[info->dls_cnt];
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 		/*
19547c478bd9Sstevel@tonic-gate 		 * Traverse search path entries for this object.
19557c478bd9Sstevel@tonic-gate 		 */
1956b4059b01SRod Evans 		while ((pdp = get_next_dir(&sd, lmp, 0)) != NULL) {
19577c478bd9Sstevel@tonic-gate 			size_t	_size;
19587c478bd9Sstevel@tonic-gate 
1959b4059b01SRod Evans 			if (pdp->pd_pname == NULL)
19607c478bd9Sstevel@tonic-gate 				continue;
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate 			/*
19637c478bd9Sstevel@tonic-gate 			 * If configuration information exists, it's possible
19647c478bd9Sstevel@tonic-gate 			 * this path has been identified as non-existent, if so
19657c478bd9Sstevel@tonic-gate 			 * ignore it.
19667c478bd9Sstevel@tonic-gate 			 */
196756deab07SRod Evans 			if (pdp->pd_info) {
196856deab07SRod Evans 				Rtc_obj	*dobj = (Rtc_obj *)pdp->pd_info;
19697c478bd9Sstevel@tonic-gate 				if (dobj->co_flags & RTC_OBJ_NOEXIST)
19707c478bd9Sstevel@tonic-gate 					continue;
19717c478bd9Sstevel@tonic-gate 			}
19727c478bd9Sstevel@tonic-gate 
19737c478bd9Sstevel@tonic-gate 			/*
19747c478bd9Sstevel@tonic-gate 			 * Keep track of search path count and total info size.
19757c478bd9Sstevel@tonic-gate 			 */
19767c478bd9Sstevel@tonic-gate 			if (cnt++)
1977959ee943SRod Evans 				size += sizeof (Dl_serpath_t);
197856deab07SRod Evans 			_size = pdp->pd_plen + 1;
19797c478bd9Sstevel@tonic-gate 			size += _size;
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 			if (request == RTLD_DI_SERINFOSIZE)
19827c478bd9Sstevel@tonic-gate 				continue;
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 			/*
19857c478bd9Sstevel@tonic-gate 			 * If we're filling in search path information, confirm
19867c478bd9Sstevel@tonic-gate 			 * there's sufficient space.
19877c478bd9Sstevel@tonic-gate 			 */
19887c478bd9Sstevel@tonic-gate 			if (size > info->dls_size) {
19895aefb655Srie 				eprintf(lml, ERR_FATAL,
19905aefb655Srie 				    MSG_INTL(MSG_ARG_SERSIZE),
19917c478bd9Sstevel@tonic-gate 				    EC_OFF(info->dls_size));
19927c478bd9Sstevel@tonic-gate 				return (-1);
19937c478bd9Sstevel@tonic-gate 			}
19947c478bd9Sstevel@tonic-gate 			if (cnt > info->dls_cnt) {
19955aefb655Srie 				eprintf(lml, ERR_FATAL,
19965aefb655Srie 				    MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt);
19977c478bd9Sstevel@tonic-gate 				return (-1);
19987c478bd9Sstevel@tonic-gate 			}
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 			/*
20017c478bd9Sstevel@tonic-gate 			 * Append the path to the information buffer.
20027c478bd9Sstevel@tonic-gate 			 */
200356deab07SRod Evans 			(void) strcpy(strs, pdp->pd_pname);
20047c478bd9Sstevel@tonic-gate 			path->dls_name = strs;
2005f441771bSRod Evans 			path->dls_flags = (pdp->pd_flags & LA_SER_MASK);
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate 			strs = strs + _size;
20087c478bd9Sstevel@tonic-gate 			path++;
20097c478bd9Sstevel@tonic-gate 		}
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 		/*
20127c478bd9Sstevel@tonic-gate 		 * If we're here to size the search buffer fill it in.
20137c478bd9Sstevel@tonic-gate 		 */
20147c478bd9Sstevel@tonic-gate 		if (request == RTLD_DI_SERINFOSIZE) {
20157c478bd9Sstevel@tonic-gate 			info->dls_size = size;
20167c478bd9Sstevel@tonic-gate 			info->dls_cnt = cnt;
20177c478bd9Sstevel@tonic-gate 		}
2018959ee943SRod Evans 
2019959ee943SRod Evans 		return (0);
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	/*
20237c478bd9Sstevel@tonic-gate 	 * Return the origin of the object associated with this link-map.
20247c478bd9Sstevel@tonic-gate 	 * Basically return the dirname(1) of the objects fullpath.
20257c478bd9Sstevel@tonic-gate 	 */
20267c478bd9Sstevel@tonic-gate 	if (request == RTLD_DI_ORIGIN) {
202741072f3cSrie 		char	*str = (char *)p;
20287c478bd9Sstevel@tonic-gate 
202941072f3cSrie 		(void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp));
203041072f3cSrie 		str += DIRSZ(lmp);
20317c478bd9Sstevel@tonic-gate 		*str = '\0';
20327c478bd9Sstevel@tonic-gate 
20337c478bd9Sstevel@tonic-gate 		return (0);
20347c478bd9Sstevel@tonic-gate 	}
20357c478bd9Sstevel@tonic-gate 
2036959ee943SRod Evans 	/*
2037959ee943SRod Evans 	 * Return the number of object mappings, or the mapping information for
2038959ee943SRod Evans 	 * this object.
2039959ee943SRod Evans 	 */
2040959ee943SRod Evans 	if (request == RTLD_DI_MMAPCNT) {
2041959ee943SRod Evans 		uint_t	*cnt = (uint_t *)p;
2042959ee943SRod Evans 
2043959ee943SRod Evans 		*cnt = MMAPCNT(lmp);
2044959ee943SRod Evans 		return (0);
2045959ee943SRod Evans 	}
2046959ee943SRod Evans 	if (request == RTLD_DI_MMAPS) {
2047959ee943SRod Evans 		Dl_mapinfo_t	*mip = (Dl_mapinfo_t *)p;
2048959ee943SRod Evans 
2049959ee943SRod Evans 		if (mip->dlm_acnt && mip->dlm_maps) {
2050959ee943SRod Evans 			uint_t	cnt = 0;
2051959ee943SRod Evans 
2052959ee943SRod Evans 			while ((cnt < mip->dlm_acnt) && (cnt < MMAPCNT(lmp))) {
2053959ee943SRod Evans 				mip->dlm_maps[cnt] = MMAPS(lmp)[cnt];
2054959ee943SRod Evans 				cnt++;
2055959ee943SRod Evans 			}
2056959ee943SRod Evans 			mip->dlm_rcnt = cnt;
2057959ee943SRod Evans 		}
2058959ee943SRod Evans 		return (0);
2059959ee943SRod Evans 	}
2060959ee943SRod Evans 
2061f441771bSRod Evans 	/*
2062f441771bSRod Evans 	 * Assign a new dependency name to a deferred dependency.
2063f441771bSRod Evans 	 */
2064f441771bSRod Evans 	if ((request == RTLD_DI_DEFERRED) ||
2065f441771bSRod Evans 	    (request == RTLD_DI_DEFERRED_SYM)) {
2066f441771bSRod Evans 		Dl_definfo_t	*dfip = (Dl_definfo_t *)p;
2067f441771bSRod Evans 		Dyninfo		*dyip;
2068f441771bSRod Evans 		const char	*dname, *rname;
2069f441771bSRod Evans 
2070f441771bSRod Evans 		/*
2071f441771bSRod Evans 		 * Verify the names.
2072f441771bSRod Evans 		 */
2073f441771bSRod Evans 		if ((dfip->dld_refname == NULL) ||
2074f441771bSRod Evans 		    (dfip->dld_depname == NULL)) {
2075f441771bSRod Evans 			eprintf(LIST(clmp), ERR_FATAL,
2076f441771bSRod Evans 			    MSG_INTL(MSG_ARG_ILLNAME));
2077f441771bSRod Evans 			return (-1);
2078f441771bSRod Evans 		}
2079f441771bSRod Evans 
2080f441771bSRod Evans 		dname = dfip->dld_depname;
2081f441771bSRod Evans 		rname = dfip->dld_refname;
2082f441771bSRod Evans 
2083f441771bSRod Evans 		/*
2084f441771bSRod Evans 		 * A deferred dependency can be determined by referencing a
2085f441771bSRod Evans 		 * symbol family member that is associated to the dependency,
2086f441771bSRod Evans 		 * or by looking for the dependency by its name.
2087f441771bSRod Evans 		 */
2088f441771bSRod Evans 		if (request == RTLD_DI_DEFERRED_SYM) {
2089f441771bSRod Evans 			Slookup		sl;
2090f441771bSRod Evans 			Sresult		sr;
2091f441771bSRod Evans 			uint_t		binfo;
2092f441771bSRod Evans 			Syminfo		*sip;
2093f441771bSRod Evans 
2094f441771bSRod Evans 			/*
2095f441771bSRod Evans 			 * Lookup the symbol in the associated object.
2096f441771bSRod Evans 			 */
2097f441771bSRod Evans 			SLOOKUP_INIT(sl, rname, lmp, lmp, ld_entry_cnt,
2098f441771bSRod Evans 			    elf_hash(rname), 0, 0, 0, LKUP_SYMNDX);
2099f441771bSRod Evans 			SRESULT_INIT(sr, rname);
2100f441771bSRod Evans 			if (sym_lookup_in_caller(clmp, &sl, &sr,
2101f441771bSRod Evans 			    &binfo) == NULL) {
2102f441771bSRod Evans 				eprintf(LIST(clmp), ERR_FATAL,
2103f441771bSRod Evans 				    MSG_INTL(MSG_DEF_NOSYMFOUND), rname);
2104f441771bSRod Evans 				return (-1);
2105f441771bSRod Evans 			}
2106f441771bSRod Evans 
2107f441771bSRod Evans 			/*
2108f441771bSRod Evans 			 * Use the symbols index to reference the Syminfo entry
2109f441771bSRod Evans 			 * and thus find the associated dependency.
2110f441771bSRod Evans 			 */
2111f441771bSRod Evans 			if (sl.sl_rsymndx && ((sip = SYMINFO(clmp)) != NULL)) {
2112f441771bSRod Evans 				/* LINTED */
2113f441771bSRod Evans 				sip = (Syminfo *)((char *)sip +
2114f441771bSRod Evans 				    (sl.sl_rsymndx * SYMINENT(lmp)));
2115f441771bSRod Evans 
2116f441771bSRod Evans 				if ((sip->si_flags & SYMINFO_FLG_DEFERRED) &&
2117f441771bSRod Evans 				    (sip->si_boundto < SYMINFO_BT_LOWRESERVE) &&
2118f441771bSRod Evans 				    ((dyip = DYNINFO(lmp)) != NULL)) {
2119f441771bSRod Evans 					dyip += sip->si_boundto;
2120f441771bSRod Evans 
2121f441771bSRod Evans 					if (!(dyip->di_flags & FLG_DI_IGNORE))
2122f441771bSRod Evans 						return (set_def_need(lml,
2123f441771bSRod Evans 						    dyip, dname));
2124f441771bSRod Evans 				}
2125f441771bSRod Evans 			}
2126f441771bSRod Evans 
2127f441771bSRod Evans 			/*
2128f441771bSRod Evans 			 * No deferred symbol found.
2129f441771bSRod Evans 			 */
2130f441771bSRod Evans 			eprintf(LIST(clmp), ERR_FATAL,
2131f441771bSRod Evans 			    MSG_INTL(MSG_DEF_NOSYMFOUND), rname);
2132f441771bSRod Evans 			return (-1);
2133f441771bSRod Evans 
2134f441771bSRod Evans 		} else {
2135f441771bSRod Evans 			Dyn	*dyn;
2136f441771bSRod Evans 
2137f441771bSRod Evans 			/*
2138f441771bSRod Evans 			 * Using the target objects dependency information, find
2139f441771bSRod Evans 			 * the associated deferred dependency.
2140f441771bSRod Evans 			 */
2141f441771bSRod Evans 			for (dyn = DYN(lmp), dyip = DYNINFO(lmp);
2142f441771bSRod Evans 			    !(dyip->di_flags & FLG_DI_IGNORE); dyn++, dyip++) {
2143f441771bSRod Evans 				const char	*oname;
2144f441771bSRod Evans 
2145f441771bSRod Evans 				if ((dyip->di_flags & FLG_DI_DEFERRED) == 0)
2146f441771bSRod Evans 					continue;
2147f441771bSRod Evans 
2148f441771bSRod Evans 				if (strcmp(rname, dyip->di_name) == 0)
2149f441771bSRod Evans 					return (set_def_need(lml, dyip, dname));
2150f441771bSRod Evans 
2151f441771bSRod Evans 				/*
2152f441771bSRod Evans 				 * If this dependency name has been changed by
2153f441771bSRod Evans 				 * a previous dlinfo(), check the original
2154f441771bSRod Evans 				 * dynamic entry string.  The user might be
2155f441771bSRod Evans 				 * attempting to re-change an entry using the
2156f441771bSRod Evans 				 * original name as the reference.
2157f441771bSRod Evans 				 */
2158f441771bSRod Evans 				if ((dyip->di_flags & FLG_DI_DEF_DONE) == 0)
2159f441771bSRod Evans 					continue;
2160f441771bSRod Evans 
2161f441771bSRod Evans 				oname = STRTAB(lmp) + dyn->d_un.d_val;
2162f441771bSRod Evans 				if (strcmp(rname, oname) == 0)
2163f441771bSRod Evans 					return (set_def_need(lml, dyip, dname));
2164f441771bSRod Evans 			}
2165f441771bSRod Evans 
2166f441771bSRod Evans 			/*
2167f441771bSRod Evans 			 * No deferred dependency found.
2168f441771bSRod Evans 			 */
2169f441771bSRod Evans 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_DEF_NODEPFOUND),
2170f441771bSRod Evans 			    rname);
2171f441771bSRod Evans 			return (-1);
2172f441771bSRod Evans 		}
2173f441771bSRod Evans 	}
21747c478bd9Sstevel@tonic-gate 	return (0);
21757c478bd9Sstevel@tonic-gate }
21767c478bd9Sstevel@tonic-gate 
21777257d1b4Sraf #pragma weak _dlinfo = dlinfo
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate /*
21807c478bd9Sstevel@tonic-gate  * External entry for dlinfo(3dl).
21817c478bd9Sstevel@tonic-gate  */
21827c478bd9Sstevel@tonic-gate int
dlinfo(void * handle,int request,void * p)21837257d1b4Sraf dlinfo(void *handle, int request, void *p)
21847c478bd9Sstevel@tonic-gate {
21857c478bd9Sstevel@tonic-gate 	int	error, entry;
21867c478bd9Sstevel@tonic-gate 	Rt_map	*clmp;
21877c478bd9Sstevel@tonic-gate 
21888cd45542Sraf 	entry = enter(0);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	clmp = _caller(caller(), CL_EXECDEF);
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate 	error = dlinfo_core(handle, request, p, clmp);
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	if (entry)
21958cd45542Sraf 		leave(LIST(clmp), 0);
21967c478bd9Sstevel@tonic-gate 	return (error);
21977c478bd9Sstevel@tonic-gate }
219820272c2eSAli Bahrami 
219920272c2eSAli Bahrami /*
220020272c2eSAli Bahrami  * GNU defined function to iterate through the program headers for all
220120272c2eSAli Bahrami  * currently loaded dynamic objects. The caller supplies a callback function
220220272c2eSAli Bahrami  * which is called for each object.
220320272c2eSAli Bahrami  *
220420272c2eSAli Bahrami  * entry:
220520272c2eSAli Bahrami  *	callback - Callback function to call. The arguments to the callback
220620272c2eSAli Bahrami  *		function are:
220720272c2eSAli Bahrami  *		info - Address of dl_phdr_info structure
220820272c2eSAli Bahrami  *		size - sizeof (struct dl_phdr_info)
220920272c2eSAli Bahrami  *		data - Caller supplied value.
221020272c2eSAli Bahrami  *	data - Value supplied by caller, which is passed to callback without
221120272c2eSAli Bahrami  *		examination.
221220272c2eSAli Bahrami  *
221320272c2eSAli Bahrami  * exit:
221420272c2eSAli Bahrami  *	callback is called for each dynamic ELF object in the process address
221520272c2eSAli Bahrami  *	space, halting when a non-zero value is returned, or when the last
221620272c2eSAli Bahrami  *	object has been processed. The return value from the last call
221720272c2eSAli Bahrami  *	to callback is returned.
221820272c2eSAli Bahrami  *
221920272c2eSAli Bahrami  * note:
222020272c2eSAli Bahrami  *	The Linux implementation has added additional fields to the
222120272c2eSAli Bahrami  *	dl_phdr_info structure over time. The callback function is
222220272c2eSAli Bahrami  *	supposed to use the size field to determine which fields are
222320272c2eSAli Bahrami  *	present, and to avoid attempts to access non-existent fields.
222420272c2eSAli Bahrami  *	We have added those fields that are compatible with Solaris, and
222520272c2eSAli Bahrami  *	which are used by GNU C++ (g++) runtime exception handling support.
222620272c2eSAli Bahrami  *
222720272c2eSAli Bahrami  * note:
222820272c2eSAli Bahrami  *	We issue a callback for every ELF object mapped into the process
222920272c2eSAli Bahrami  *	address space at the time this routine is entered. These callbacks
223020272c2eSAli Bahrami  *	are arbitrary functions that can do anything, including possibly
223120272c2eSAli Bahrami  *	causing new objects to be mapped into the process, or unmapped.
223220272c2eSAli Bahrami  *	This complicates matters:
223320272c2eSAli Bahrami  *
223420272c2eSAli Bahrami  *	-	Adding new objects can cause the alists to be reallocated
223520272c2eSAli Bahrami  *		or for contents to move. This can happen explicitly via
223620272c2eSAli Bahrami  *		dlopen(), or implicitly via lazy loading. One might consider
223720272c2eSAli Bahrami  *		simply banning dlopen from a callback, but lazy loading must
223820272c2eSAli Bahrami  *		be allowed, in which case there's no reason to ban dlopen().
223920272c2eSAli Bahrami  *
224020272c2eSAli Bahrami  *	-	Removing objects can leave us holding references to freed
224120272c2eSAli Bahrami  *		memory that must not be accessed, and can cause the list
224220272c2eSAli Bahrami  *		items to move in a way that would cause us to miss reporting
224320272c2eSAli Bahrami  *		one, or double report others.
224420272c2eSAli Bahrami  *
224520272c2eSAli Bahrami  *	-	We cannot allocate memory to build a separate data structure,
224620272c2eSAli Bahrami  *		because the interface to dl_iterate_phdr() does not have a
224720272c2eSAli Bahrami  *		way to communicate allocation errors back to the caller.
224820272c2eSAli Bahrami  *		Even if we could, it would be difficult to do so efficiently.
224920272c2eSAli Bahrami  *
225020272c2eSAli Bahrami  *	-	It is possible for dl_iterate_phdr() to be called recursively
225120272c2eSAli Bahrami  *		from a callback, and there is no way for us to detect or manage
225220272c2eSAli Bahrami  *		this effectively, particularly as the user might use longjmp()
225320272c2eSAli Bahrami  *		to skip past us on return. Hence, we must be reentrant
225420272c2eSAli Bahrami  *		(stateless), further precluding the option of building a
225520272c2eSAli Bahrami  *		separate data structure.
225620272c2eSAli Bahrami  *
225720272c2eSAli Bahrami  *	Despite these constraints, we are able to traverse the link-map
225820272c2eSAli Bahrami  *	lists safely:
225920272c2eSAli Bahrami  *
226020272c2eSAli Bahrami  *	-	Once interposer (preload) objects have been processed at
226120272c2eSAli Bahrami  *		startup, we know that new objects are always placed at the
226220272c2eSAli Bahrami  *		end of the list. Hence, if we are reading a list when that
226320272c2eSAli Bahrami  *		happens, the new object will not alter the part of the list
226420272c2eSAli Bahrami  *		that we've already processed.
226520272c2eSAli Bahrami  *
226620272c2eSAli Bahrami  *	-	The alist _TRAVERSE macros recalculate the address of the
226720272c2eSAli Bahrami  *		current item from scratch on each iteration, rather than
226820272c2eSAli Bahrami  *		incrementing a pointer. Hence, alist additions that occur
226920272c2eSAli Bahrami  *		in mid-traverse will not cause confusion.
227020272c2eSAli Bahrami  *
227120272c2eSAli Bahrami  * 	There is one limitation: We cannot continue operation if an object
227220272c2eSAli Bahrami  *	is removed from the process from within a callback. We detect when
227320272c2eSAli Bahrami  *	this happens and return immediately with a -1 return value.
227420272c2eSAli Bahrami  *
227520272c2eSAli Bahrami  * note:
227620272c2eSAli Bahrami  *	As currently implemented, if a callback causes an object to be loaded,
227720272c2eSAli Bahrami  *	that object may or may not be reported by the current invocation of
227820272c2eSAli Bahrami  *	dl_iterate_phdr(), based on whether or not we have already processed
227920272c2eSAli Bahrami  *	the link-map list that receives it. If we want to prevent this, it
228020272c2eSAli Bahrami  *	can be done efficiently by associating the current value of cnt_map
228120272c2eSAli Bahrami  *	with each new Rt_map entered into the system. Then this function can
228220272c2eSAli Bahrami  *	use that to detect and skip new objects that enter the system in
228320272c2eSAli Bahrami  *	mid-iteration. However, the Linux documentation is ambiguous on whether
228420272c2eSAli Bahrami  *	this is necessary, and it does not appear to matter in practice.
228520272c2eSAli Bahrami  *	We have therefore chosen not to do so at this time.
228620272c2eSAli Bahrami  */
228720272c2eSAli Bahrami int
dl_iterate_phdr(int (* callback)(struct dl_phdr_info *,size_t,void *),void * data)228820272c2eSAli Bahrami dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *),
228920272c2eSAli Bahrami     void *data)
229020272c2eSAli Bahrami {
229120272c2eSAli Bahrami 	struct dl_phdr_info	info;
229220272c2eSAli Bahrami 	u_longlong_t		l_cnt_map = cnt_map;
229320272c2eSAli Bahrami 	u_longlong_t		l_cnt_unmap = cnt_unmap;
229498c080d5SRod Evans 	Lm_list			*lml, *clml;
229520272c2eSAli Bahrami 	Lm_cntl			*lmc;
229620272c2eSAli Bahrami 	Rt_map			*lmp, *clmp;
229720272c2eSAli Bahrami 	Aliste			idx1, idx2;
229820272c2eSAli Bahrami 	Ehdr			*ehdr;
229920272c2eSAli Bahrami 	int			ret = 0;
230020272c2eSAli Bahrami 	int			entry;
230120272c2eSAli Bahrami 
230220272c2eSAli Bahrami 	entry = enter(0);
230320272c2eSAli Bahrami 	clmp = _caller(caller(), CL_EXECDEF);
230498c080d5SRod Evans 	clml = LIST(clmp);
230598c080d5SRod Evans 
230698c080d5SRod Evans 	DBG_CALL(Dbg_dl_iphdr_enter(clmp, cnt_map, cnt_unmap));
230720272c2eSAli Bahrami 
230820272c2eSAli Bahrami 	/* Issue a callback for each ELF object in the process */
230920272c2eSAli Bahrami 	for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) {
231020272c2eSAli Bahrami 		for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) {
231120272c2eSAli Bahrami 			for (lmp = lmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) {
231220272c2eSAli Bahrami #if defined(_sparc) && !defined(_LP64)
231320272c2eSAli Bahrami 				/*
231420272c2eSAli Bahrami 				 * On 32-bit sparc, the possibility exists that
231520272c2eSAli Bahrami 				 * this object is not ELF.
231620272c2eSAli Bahrami 				 */
231720272c2eSAli Bahrami 				if (THIS_IS_NOT_ELF(lmp))
231820272c2eSAli Bahrami 					continue;
231920272c2eSAli Bahrami #endif
232020272c2eSAli Bahrami 				/* Prepare the object information structure */
232120272c2eSAli Bahrami 				ehdr = (Ehdr *) ADDR(lmp);
232220272c2eSAli Bahrami 				info.dlpi_addr = (ehdr->e_type == ET_EXEC) ?
232320272c2eSAli Bahrami 				    0 : ADDR(lmp);
232420272c2eSAli Bahrami 				info.dlpi_name = lmp->rt_pathname;
232520272c2eSAli Bahrami 				info.dlpi_phdr = (Phdr *)
232620272c2eSAli Bahrami 				    (ADDR(lmp) + ehdr->e_phoff);
232720272c2eSAli Bahrami 				info.dlpi_phnum = ehdr->e_phnum;
232820272c2eSAli Bahrami 				info.dlpi_adds = cnt_map;
232920272c2eSAli Bahrami 				info.dlpi_subs = cnt_unmap;
233020272c2eSAli Bahrami 
233120272c2eSAli Bahrami 				/* Issue the callback */
233298c080d5SRod Evans 				DBG_CALL(Dbg_dl_iphdr_callback(clml, &info));
233398c080d5SRod Evans 				leave(clml, thr_flg_reenter);
233420272c2eSAli Bahrami 				ret = (* callback)(&info, sizeof (info), data);
233520272c2eSAli Bahrami 				(void) enter(thr_flg_reenter);
233620272c2eSAli Bahrami 
233720272c2eSAli Bahrami 				/* Return immediately on non-zero result */
233820272c2eSAli Bahrami 				if (ret != 0)
233920272c2eSAli Bahrami 					goto done;
234020272c2eSAli Bahrami 
234120272c2eSAli Bahrami 				/* Adapt to object mapping changes */
234298c080d5SRod Evans 				if ((cnt_map == l_cnt_map) &&
234398c080d5SRod Evans 				    (cnt_unmap == l_cnt_unmap))
234498c080d5SRod Evans 					continue;
234598c080d5SRod Evans 
234698c080d5SRod Evans 				DBG_CALL(Dbg_dl_iphdr_mapchange(clml, cnt_map,
234798c080d5SRod Evans 				    cnt_unmap));
234820272c2eSAli Bahrami 
234920272c2eSAli Bahrami 				/* Stop if an object was unmapped */
235098c080d5SRod Evans 				if (cnt_unmap == l_cnt_unmap) {
235198c080d5SRod Evans 					l_cnt_map = cnt_map;
235298c080d5SRod Evans 					continue;
235320272c2eSAli Bahrami 				}
235420272c2eSAli Bahrami 
235598c080d5SRod Evans 				ret = -1;
235698c080d5SRod Evans 				DBG_CALL(Dbg_dl_iphdr_unmap_ret(clml));
235798c080d5SRod Evans 				goto done;
235820272c2eSAli Bahrami 			}
235920272c2eSAli Bahrami 		}
236020272c2eSAli Bahrami 	}
236120272c2eSAli Bahrami 
236220272c2eSAli Bahrami done:
236320272c2eSAli Bahrami 	if (entry)
236420272c2eSAli Bahrami 		leave(LIST(clmp), 0);
236520272c2eSAli Bahrami 	return (ret);
236620272c2eSAli Bahrami }
2367