Home | History | Annotate | Download | only in common
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 /*
     28  *	Copyright (c) 1988 AT&T
     29  *	  All Rights Reserved
     30  */
     31 
     32 /*
     33  * Programmatic interface to the run_time linker.
     34  */
     35 
     36 #include	<sys/debug.h>
     37 #include	<stdio.h>
     38 #include	<string.h>
     39 #include	<dlfcn.h>
     40 #include	<synch.h>
     41 #include	<limits.h>
     42 #include	<debug.h>
     43 #include	"_rtld.h"
     44 #include	"_audit.h"
     45 #include	"_elf.h"
     46 #include	"_inline.h"
     47 #include	"msg.h"
     48 
     49 /*
     50  * Determine who called us - given a pc determine in which object it resides.
     51  *
     52  * For dlopen() the link map of the caller must be passed to load_so() so that
     53  * the appropriate search rules (4.x or 5.0) are used to locate any
     54  * dependencies.  Also, if we've been called from a 4.x module it may be
     55  * necessary to fix the specified pathname so that it conforms with the 5.0 elf
     56  * rules.
     57  *
     58  * For dlsym() the link map of the caller is used to determine RTLD_NEXT
     59  * requests, together with requests based off of a dlopen(0).
     60  * For dladdr() this routines provides a generic means of scanning all loaded
     61  * segments.
     62  */
     63 Rt_map *
     64 _caller(caddr_t cpc, int flags)
     65 {
     66 	Lm_list	*lml;
     67 	Aliste	idx1;
     68 
     69 	for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) {
     70 		Aliste	idx2;
     71 		Lm_cntl	*lmc;
     72 
     73 		for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) {
     74 			Rt_map	*lmp;
     75 
     76 			for (lmp = lmc->lc_head; lmp;
     77 			    lmp = NEXT_RT_MAP(lmp)) {
     78 
     79 				if (find_segment(cpc, lmp))
     80 					return (lmp);
     81 			}
     82 		}
     83 	}
     84 
     85 	/*
     86 	 * No mapping can be determined.  If asked for a default, assume this
     87 	 * is from the executable.
     88 	 */
     89 	if (flags & CL_EXECDEF)
     90 		return ((Rt_map *)lml_main.lm_head);
     91 
     92 	return (0);
     93 }
     94 
     95 #pragma weak _dlerror = dlerror
     96 
     97 /*
     98  * External entry for dlerror(3dl).  Returns a pointer to the string describing
     99  * the last occurring error.  The last occurring error is cleared.
    100  */
    101 char *
    102 dlerror()
    103 {
    104 	char	*error;
    105 	Rt_map	*clmp;
    106 	int	entry;
    107 
    108 	entry = enter(0);
    109 
    110 	clmp = _caller(caller(), CL_EXECDEF);
    111 
    112 	error = lasterr;
    113 	lasterr = NULL;
    114 
    115 	if (entry)
    116 		leave(LIST(clmp), 0);
    117 	return (error);
    118 }
    119 
    120 /*
    121  * Add a dependency as a group descriptor to a group handle.  Returns 0 on
    122  * failure.  On success, returns the group descriptor, and if alep is non-NULL
    123  * the *alep is set to ALE_EXISTS if the dependency already exists, or to
    124  * ALE_CREATE if the dependency is newly created.
    125  */
    126 Grp_desc *
    127 hdl_add(Grp_hdl *ghp, Rt_map *lmp, uint_t dflags, int *alep)
    128 {
    129 	Grp_desc	*gdp;
    130 	Aliste		idx;
    131 	int		ale = ALE_CREATE;
    132 	uint_t		oflags;
    133 
    134 	/*
    135 	 * Make sure this dependency hasn't already been recorded.
    136 	 */
    137 	for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
    138 		if (gdp->gd_depend == lmp) {
    139 			ale = ALE_EXISTS;
    140 			break;
    141 		}
    142 	}
    143 
    144 	if (ale == ALE_CREATE) {
    145 		Grp_desc	gd;
    146 
    147 		/*
    148 		 * Create a new handle descriptor.
    149 		 */
    150 		gd.gd_depend = lmp;
    151 		gd.gd_flags = 0;
    152 
    153 		/*
    154 		 * Indicate this object is a part of this handles group.
    155 		 */
    156 		if (aplist_append(&GROUPS(lmp), ghp, AL_CNT_GROUPS) == NULL)
    157 			return (NULL);
    158 
    159 		/*
    160 		 * Append the new dependency to this handle.
    161 		 */
    162 		if ((gdp = alist_append(&ghp->gh_depends, &gd,
    163 		    sizeof (Grp_desc), AL_CNT_DEPENDS)) == NULL)
    164 			return (NULL);
    165 	}
    166 
    167 	oflags = gdp->gd_flags;
    168 	gdp->gd_flags |= dflags;
    169 
    170 	if (DBG_ENABLED) {
    171 		if (ale == ALE_CREATE)
    172 			DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_ADD,
    173 			    gdp->gd_flags));
    174 		else if (gdp->gd_flags != oflags)
    175 			DBG_CALL(Dbg_file_hdl_action(ghp, lmp, DBG_DEP_UPDATE,
    176 			    gdp->gd_flags));
    177 	}
    178 
    179 	if (alep)
    180 		*alep = ale;
    181 	return (gdp);
    182 }
    183 
    184 /*
    185  * Create a handle.
    186  *
    187  *   rlmp -	represents the reference link-map for which the handle is being
    188  *		created.
    189  *   clmp -	represents the caller who is requesting the handle.
    190  *   hflags -	provide group handle flags (GPH_*) that affect the use of the
    191  *		handle, such as dlopen(0), or use or use of RTLD_FIRST.
    192  *   rdflags -	provide group dependency flags for the reference link-map rlmp,
    193  *		such as whether the dependency can be used for dlsym(), can be
    194  *		relocated against, or whether this objects dependencies should
    195  *		be processed.
    196  *   cdflags -	provide group dependency flags for the caller.
    197  */
    198 Grp_hdl *
    199 hdl_create(Lm_list *lml, Rt_map *rlmp, Rt_map *clmp, uint_t hflags,
    200     uint_t rdflags, uint_t cdflags)
    201 {
    202 	Grp_hdl	*ghp = NULL, *aghp;
    203 	APlist	**alpp;
    204 	Aliste	idx;
    205 
    206 	/*
    207 	 * For dlopen(0) the handle is maintained as part of the link-map list,
    208 	 * otherwise the handle is associated with the reference link-map.
    209 	 */
    210 	if (hflags & GPH_ZERO)
    211 		alpp = &(lml->lm_handle);
    212 	else
    213 		alpp = &(HANDLES(rlmp));
    214 
    215 	/*
    216 	 * Objects can contain multiple handles depending on the handle flags
    217 	 * supplied.  Most RTLD flags pertain to the object itself and the
    218 	 * bindings that it can achieve.  Multiple handles for these flags
    219 	 * don't make sense.  But if the flag determines how the handle might
    220 	 * be used, then multiple handles may exist.  Presently this only makes
    221 	 * sense for RTLD_FIRST.  Determine if an appropriate handle already
    222 	 * exists.
    223 	 */
    224 	for (APLIST_TRAVERSE(*alpp, idx, aghp)) {
    225 		if ((aghp->gh_flags & GPH_FIRST) == (hflags & GPH_FIRST)) {
    226 			ghp = aghp;
    227 			break;
    228 		}
    229 	}
    230 
    231 	if (ghp == NULL) {
    232 		uint_t	ndx;
    233 
    234 		/*
    235 		 * If this is the first request for this handle, allocate and
    236 		 * initialize a new handle.
    237 		 */
    238 		DBG_CALL(Dbg_file_hdl_title(DBG_HDL_CREATE));
    239 
    240 		if ((ghp = malloc(sizeof (Grp_hdl))) == NULL)
    241 			return (NULL);
    242 
    243 		/*
    244 		 * Associate the handle with the link-map list or the reference
    245 		 * link-map as appropriate.
    246 		 */
    247 		if (aplist_append(alpp, ghp, AL_CNT_GROUPS) == NULL) {
    248 			free(ghp);
    249 			return (NULL);
    250 		}
    251 
    252 		/*
    253 		 * Record the existence of this handle for future verification.
    254 		 */
    255 		/* LINTED */
    256 		ndx = (uintptr_t)ghp % HDLIST_SZ;
    257 
    258 		if (aplist_append(&hdl_alp[ndx], ghp, AL_CNT_HANDLES) == NULL) {
    259 			(void) aplist_delete_value(*alpp, ghp);
    260 			free(ghp);
    261 			return (NULL);
    262 		}
    263 
    264 		ghp->gh_depends = NULL;
    265 		ghp->gh_refcnt = 1;
    266 		ghp->gh_flags = hflags;
    267 
    268 		/*
    269 		 * A dlopen(0) handle is identified by the GPH_ZERO flag, the
    270 		 * head of the link-map list is defined as the owner.  There is
    271 		 * no need to maintain a list of dependencies, for when this
    272 		 * handle is used (for dlsym()) a dynamic search through the
    273 		 * entire link-map list provides for searching all objects with
    274 		 * GLOBAL visibility.
    275 		 */
    276 		if (hflags & GPH_ZERO) {
    277 			ghp->gh_ownlmp = lml->lm_head;
    278 			ghp->gh_ownlml = lml;
    279 		} else {
    280 			ghp->gh_ownlmp = rlmp;
    281 			ghp->gh_ownlml = LIST(rlmp);
    282 
    283 			if (hdl_add(ghp, rlmp, rdflags, NULL) == NULL)
    284 				return (NULL);
    285 
    286 			/*
    287 			 * If this new handle is a private handle, there's no
    288 			 * need to track the caller, so we're done.
    289 			 */
    290 			if (hflags & GPH_PRIVATE)
    291 				return (ghp);
    292 
    293 			/*
    294 			 * If this new handle is public, and isn't a special
    295 			 * handle representing ld.so.1, indicate that a local
    296 			 * group now exists.  This state allows singleton
    297 			 * searches to be optimized.
    298 			 */
    299 			if ((hflags & GPH_LDSO) == 0)
    300 				LIST(rlmp)->lm_flags |= LML_FLG_GROUPSEXIST;
    301 		}
    302 	} else {
    303 		/*
    304 		 * If a handle already exists, bump its reference count.
    305 		 *
    306 		 * If the previous reference count was 0, then this is a handle
    307 		 * that an earlier call to dlclose() was unable to remove.  Such
    308 		 * handles are put on the orphan list.  As this handle is back
    309 		 * in use, it must be removed from the orphan list.
    310 		 *
    311 		 * Note, handles associated with a link-map list itself (i.e.
    312 		 * dlopen(0)) can have a reference count of 0.  However, these
    313 		 * handles are never deleted, and therefore are never moved to
    314 		 * the orphan list.
    315 		 */
    316 		if ((ghp->gh_refcnt++ == 0) &&
    317 		    ((ghp->gh_flags & GPH_ZERO) == 0)) {
    318 			uint_t	ndx;
    319 
    320 			/* LINTED */
    321 			ndx = (uintptr_t)ghp % HDLIST_SZ;
    322 
    323 			(void) aplist_delete_value(hdl_alp[HDLIST_ORP], ghp);
    324 			(void) aplist_append(&hdl_alp[ndx], ghp,
    325 			    AL_CNT_HANDLES);
    326 
    327 			if (DBG_ENABLED) {
    328 				Aliste		idx;
    329 				Grp_desc	*gdp;
    330 
    331 				DBG_CALL(Dbg_file_hdl_title(DBG_HDL_REINST));
    332 				for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp))
    333 					DBG_CALL(Dbg_file_hdl_action(ghp,
    334 					    gdp->gd_depend, DBG_DEP_REINST, 0));
    335 			}
    336 		}
    337 
    338 		/*
    339 		 * If we've been asked to create a private handle, there's no
    340 		 * need to track the caller.
    341 		 */
    342 		if (hflags & GPH_PRIVATE) {
    343 			/*
    344 			 * Negate the reference count increment.
    345 			 */
    346 			ghp->gh_refcnt--;
    347 			return (ghp);
    348 		} else {
    349 			/*
    350 			 * If a private handle already exists, promote this
    351 			 * handle to public by initializing both the reference
    352 			 * count and the handle flags.
    353 			 */
    354 			if (ghp->gh_flags & GPH_PRIVATE) {
    355 				ghp->gh_refcnt = 1;
    356 				ghp->gh_flags &= ~GPH_PRIVATE;
    357 				ghp->gh_flags |= hflags;
    358 			}
    359 		}
    360 	}
    361 
    362 	/*
    363 	 * Keep track of the parent (caller).  As this object can be referenced
    364 	 * by different parents, this processing is carried out every time a
    365 	 * handle is requested.
    366 	 */
    367 	if (clmp && (hdl_add(ghp, clmp, cdflags, NULL) == NULL))
    368 		return (NULL);
    369 
    370 	return (ghp);
    371 }
    372 
    373 /*
    374  * Initialize a handle that has been created for an object that is already
    375  * loaded.  The handle is initialized with the present dependencies of that
    376  * object.  Once this initialization has occurred, any new objects that might
    377  * be loaded as dependencies (lazy-loading) are added to the handle as each new
    378  * object is loaded.
    379  */
    380 int
    381 hdl_initialize(Grp_hdl *ghp, Rt_map *nlmp, int mode, int promote)
    382 {
    383 	Aliste		idx;
    384 	Grp_desc	*gdp;
    385 
    386 	/*
    387 	 * If the handle has already been initialized, and the initial object's
    388 	 * mode hasn't been promoted, there's no need to recompute the modes of
    389 	 * any dependencies.  If the object we've added has just been opened,
    390 	 * the objects dependencies will not yet have been processed.  These
    391 	 * dependencies will be added on later calls to load_one().  Otherwise,
    392 	 * this object already exists, so add all of its dependencies to the
    393 	 * handle were operating on.
    394 	 */
    395 	if (((ghp->gh_flags & GPH_INITIAL) && (promote == 0)) ||
    396 	    ((FLAGS(nlmp) & FLG_RT_ANALYZED) == 0)) {
    397 		ghp->gh_flags |= GPH_INITIAL;
    398 		return (1);
    399 	}
    400 
    401 	DBG_CALL(Dbg_file_hdl_title(DBG_HDL_ADD));
    402 	for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
    403 		Rt_map		*lmp = gdp->gd_depend;
    404 		Aliste		idx1;
    405 		Bnd_desc	*bdp;
    406 
    407 		/*
    408 		 * If this dependency doesn't indicate that its dependencies
    409 		 * should be added to a handle, ignore it.  This case identifies
    410 		 * a parent of a dlopen(RTLD_PARENT) request.
    411 		 */
    412 		if ((gdp->gd_flags & GPD_ADDEPS) == 0)
    413 			continue;
    414 
    415 		for (APLIST_TRAVERSE(DEPENDS(lmp), idx1, bdp)) {
    416 			Rt_map	*dlmp = bdp->b_depend;
    417 
    418 			if ((bdp->b_flags & BND_NEEDED) == 0)
    419 				continue;
    420 
    421 			if (hdl_add(ghp, dlmp,
    422 			    (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS), NULL) == NULL)
    423 				return (0);
    424 
    425 			(void) update_mode(dlmp, MODE(dlmp), mode);
    426 		}
    427 	}
    428 	ghp->gh_flags |= GPH_INITIAL;
    429 	return (1);
    430 }
    431 
    432 /*
    433  * Sanity check a program-provided handle.
    434  */
    435 static int
    436 hdl_validate(Grp_hdl *ghp)
    437 {
    438 	Aliste		idx;
    439 	Grp_hdl		*lghp;
    440 	uint_t		ndx;
    441 
    442 	/* LINTED */
    443 	ndx = (uintptr_t)ghp % HDLIST_SZ;
    444 
    445 	for (APLIST_TRAVERSE(hdl_alp[ndx], idx, lghp)) {
    446 		if ((lghp == ghp) && (ghp->gh_refcnt != 0))
    447 			return (1);
    448 	}
    449 	return (0);
    450 }
    451 
    452 /*
    453  * Core dlclose activity.
    454  */
    455 int
    456 dlclose_core(Grp_hdl *ghp, Rt_map *clmp, Lm_list *lml)
    457 {
    458 	int	error;
    459 
    460 	/*
    461 	 * If we're already at atexit() there's no point processing further,
    462 	 * all objects have already been tsorted for fini processing.
    463 	 */
    464 	if (rtld_flags & RT_FL_ATEXIT)
    465 		return (0);
    466 
    467 	/*
    468 	 * Diagnose what we're up to.
    469 	 */
    470 	if (ghp->gh_flags & GPH_ZERO) {
    471 		DBG_CALL(Dbg_file_dlclose(LIST(clmp), MSG_ORIG(MSG_STR_ZERO),
    472 		    DBG_DLCLOSE_IGNORE));
    473 	} else {
    474 		DBG_CALL(Dbg_file_dlclose(LIST(clmp), NAME(ghp->gh_ownlmp),
    475 		    DBG_DLCLOSE_NULL));
    476 	}
    477 
    478 	/*
    479 	 * Decrement reference count of this object.
    480 	 */
    481 	if (--(ghp->gh_refcnt))
    482 		return (0);
    483 
    484 	/*
    485 	 * If this handle is special (dlopen(0)), then leave it around - it
    486 	 * has little overhead.
    487 	 */
    488 	if (ghp->gh_flags & GPH_ZERO)
    489 		return (0);
    490 
    491 	/*
    492 	 * This handle is no longer being referenced, remove it.  If this handle
    493 	 * is part of an alternative link-map list, determine if the whole list
    494 	 * can be removed also.
    495 	 */
    496 	error = remove_hdl(ghp, clmp, NULL);
    497 
    498 	if ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)
    499 		remove_lml(lml);
    500 
    501 	return (error);
    502 }
    503 
    504 /*
    505  * Internal dlclose activity.  Called from user level or directly for internal
    506  * error cleanup.
    507  */
    508 int
    509 dlclose_intn(Grp_hdl *ghp, Rt_map *clmp)
    510 {
    511 	Rt_map	*nlmp = NULL;
    512 	Lm_list	*olml = NULL;
    513 	int	error;
    514 
    515 	/*
    516 	 * Although we're deleting object(s) it's quite possible that additional
    517 	 * objects get loaded from running the .fini section(s) of the objects
    518 	 * being deleted.  These objects will have been added to the same
    519 	 * link-map list as those objects being deleted.  Remember this list
    520 	 * for later investigation.
    521 	 */
    522 	olml = ghp->gh_ownlml;
    523 
    524 	error = dlclose_core(ghp, clmp, olml);
    525 
    526 	/*
    527 	 * Determine whether the original link-map list still exists.  In the
    528 	 * case of a dlclose of an alternative (dlmopen) link-map the whole
    529 	 * list may have been removed.
    530 	 */
    531 	if (olml) {
    532 		Aliste	idx;
    533 		Lm_list	*lml;
    534 
    535 		for (APLIST_TRAVERSE(dynlm_list, idx, lml)) {
    536 			if (olml == lml) {
    537 				nlmp = olml->lm_head;
    538 				break;
    539 			}
    540 		}
    541 	}
    542 	load_completion(nlmp);
    543 	return (error);
    544 }
    545 
    546 /*
    547  * Argument checking for dlclose.  Only called via external entry.
    548  */
    549 static int
    550 dlclose_check(void *handle, Rt_map *clmp)
    551 {
    552 	Grp_hdl	*ghp = (Grp_hdl *)handle;
    553 
    554 	if (hdl_validate(ghp) == 0) {
    555 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
    556 		    EC_NATPTR(handle));
    557 		return (1);
    558 	}
    559 	return (dlclose_intn(ghp, clmp));
    560 }
    561 
    562 #pragma weak _dlclose = dlclose
    563 
    564 /*
    565  * External entry for dlclose(3dl).  Returns 0 for success, non-zero otherwise.
    566  */
    567 int
    568 dlclose(void *handle)
    569 {
    570 	int		error, entry;
    571 	Rt_map		*clmp;
    572 
    573 	entry = enter(0);
    574 
    575 	clmp = _caller(caller(), CL_EXECDEF);
    576 
    577 	error = dlclose_check(handle, clmp);
    578 
    579 	if (entry)
    580 		leave(LIST(clmp), 0);
    581 	return (error);
    582 }
    583 
    584 static uint_t	lmid = 0;
    585 
    586 /*
    587  * The addition of new link-map lists is assumed to be in small quantities.
    588  * Here, we assign a unique link-map id for diagnostic use.  Simply update the
    589  * running link-map count until we max out.
    590  */
    591 int
    592 newlmid(Lm_list *lml)
    593 {
    594 	char	buffer[MSG_LMID_ALT_SIZE + 12];
    595 
    596 	if (lmid == UINT_MAX) {
    597 		lml->lm_lmid = UINT_MAX;
    598 		(void) strncpy(buffer, MSG_ORIG(MSG_LMID_MAXED),
    599 		    MSG_LMID_ALT_SIZE + 12);
    600 	} else {
    601 		lml->lm_lmid = lmid++;
    602 		(void) snprintf(buffer, MSG_LMID_ALT_SIZE + 12,
    603 		    MSG_ORIG(MSG_LMID_FMT), MSG_ORIG(MSG_LMID_ALT),
    604 		    lml->lm_lmid);
    605 	}
    606 	if ((lml->lm_lmidstr = strdup(buffer)) == NULL)
    607 		return (0);
    608 
    609 	return (1);
    610 }
    611 
    612 /*
    613  * Core dlopen activity.
    614  */
    615 static Grp_hdl *
    616 dlmopen_core(Lm_list *lml, Lm_list *olml, const char *path, int mode,
    617     Rt_map *clmp, uint_t flags, uint_t orig, int *in_nfavl)
    618 {
    619 	Alist		*palp = NULL;
    620 	Rt_map		*nlmp;
    621 	Grp_hdl		*ghp;
    622 	Aliste		olmco, nlmco;
    623 
    624 	DBG_CALL(Dbg_file_dlopen(clmp,
    625 	    (path ? path : MSG_ORIG(MSG_STR_ZERO)), in_nfavl, mode));
    626 
    627 	/*
    628 	 * Having diagnosed the originally defined modes, assign any defaults
    629 	 * or corrections.
    630 	 */
    631 	if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) &&
    632 	    ((mode & RTLD_NOLOAD) == 0))
    633 		mode |= (RTLD_GROUP | RTLD_WORLD);
    634 	if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) {
    635 		mode &= ~RTLD_NOW;
    636 		mode |= RTLD_LAZY;
    637 	}
    638 
    639 	/*
    640 	 * If the path specified is null then we're operating on global
    641 	 * objects.  Associate a dummy handle with the link-map list.
    642 	 */
    643 	if (path == NULL) {
    644 		Grp_hdl *ghp;
    645 		uint_t	hflags, rdflags, cdflags;
    646 		int	promote = 0;
    647 
    648 		/*
    649 		 * Establish any flags for the handle (Grp_hdl).
    650 		 *
    651 		 *  -	This is a dummy, public, handle (0) that provides for a
    652 		 *	dynamic	search of all global objects within the process.
    653 		 *  -   Use of the RTLD_FIRST mode indicates that only the first
    654 		 *	dependency on the handle (the referenced object) can be
    655 		 *	used to satisfy dlsym() requests.
    656 		 */
    657 		hflags = (GPH_PUBLIC | GPH_ZERO);
    658 		if (mode & RTLD_FIRST)
    659 			hflags |= GPH_FIRST;
    660 
    661 		/*
    662 		 * Establish the flags for the referenced dependency descriptor
    663 		 * (Grp_desc).
    664 		 *
    665 		 *  -	The referenced object is available for dlsym().
    666 		 *  -	The referenced object is available to relocate against.
    667 		 *  -	The referenced object should have it's dependencies
    668 		 *	added to this handle.
    669 		 */
    670 		rdflags = (GPD_DLSYM | GPD_RELOC | GPD_ADDEPS);
    671 
    672 		/*
    673 		 * Establish the flags for this callers dependency descriptor
    674 		 * (Grp_desc).
    675 		 *
    676 		 *  -	The explicit creation of a handle creates a descriptor
    677 		 *	for the referenced object and the parent (caller).
    678 		 *  -	Use of the RTLD_PARENT flag indicates that the parent
    679 		 *	can be relocated against.
    680 		 */
    681 		cdflags = GPD_PARENT;
    682 		if (mode & RTLD_PARENT)
    683 			cdflags |= GPD_RELOC;
    684 
    685 		if ((ghp = hdl_create(lml, 0, clmp, hflags, rdflags,
    686 		    cdflags)) == NULL)
    687 			return (NULL);
    688 
    689 		/*
    690 		 * Traverse the main link-map control list, updating the mode
    691 		 * of any objects as necessary.  Call the relocation engine if
    692 		 * this mode promotes the existing state of any relocations.
    693 		 * crle()'s first pass loads all objects necessary for building
    694 		 * a configuration file, however none of them are relocated.
    695 		 * crle()'s second pass relocates objects in preparation for
    696 		 * dldump()'ing using dlopen(0, RTLD_NOW).
    697 		 */
    698 		if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN)
    699 			return (ghp);
    700 
    701 		for (nlmp = lml->lm_head; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
    702 			if (((MODE(nlmp) & RTLD_GLOBAL) == 0) ||
    703 			    (FLAGS(nlmp) & FLG_RT_DELETE))
    704 				continue;
    705 
    706 			if (update_mode(nlmp, MODE(nlmp), mode))
    707 				promote = 1;
    708 		}
    709 		if (promote)
    710 			(void) relocate_lmc(lml, ALIST_OFF_DATA, clmp,
    711 			    lml->lm_head, in_nfavl);
    712 
    713 		return (ghp);
    714 	}
    715 
    716 	/*
    717 	 * Fix the pathname.  If this object expands to multiple paths (ie.
    718 	 * $ISALIST or $HWCAP have been used), then make sure the user has also
    719 	 * furnished the RTLD_FIRST flag.  As yet, we don't support opening
    720 	 * more than one object at a time, so enforcing the RTLD_FIRST flag
    721 	 * provides flexibility should we be able to support dlopening more
    722 	 * than one object in the future.
    723 	 */
    724 	if (LM_FIX_NAME(clmp)(path, clmp, &palp, AL_CNT_NEEDED, orig) == NULL)
    725 		return (NULL);
    726 
    727 	if ((palp->al_arritems > 1) && ((mode & RTLD_FIRST) == 0)) {
    728 		remove_plist(&palp, 1);
    729 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5));
    730 		return (NULL);
    731 	}
    732 
    733 	/*
    734 	 * Establish a link-map control list for this request, and load the
    735 	 * associated object.
    736 	 */
    737 	if ((nlmco = create_cntl(lml, 1)) == NULL) {
    738 		remove_plist(&palp, 1);
    739 		return (NULL);
    740 	}
    741 	olmco = nlmco;
    742 
    743 	nlmp = load_one(lml, nlmco, palp, clmp, mode, (flags | FLG_RT_PUBHDL),
    744 	    &ghp, in_nfavl);
    745 
    746 	/*
    747 	 * Remove any expanded pathname infrastructure, and if the dependency
    748 	 * couldn't be loaded, cleanup.
    749 	 */
    750 	remove_plist(&palp, 1);
    751 	if (nlmp == NULL) {
    752 		remove_cntl(lml, olmco);
    753 		return (NULL);
    754 	}
    755 
    756 	/*
    757 	 * If loading an auditor was requested, and the auditor already existed,
    758 	 * then the link-map returned will be to the original auditor.  The new
    759 	 * link-map list that was initially created, and the associated link-map
    760 	 * control list are no longer needed.  As the auditor is already loaded,
    761 	 * we're probably done, but fall through in case additional relocations
    762 	 * would be triggered by the mode of the caller.
    763 	 */
    764 	if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) {
    765 		remove_cntl(lml, olmco);
    766 		lml = LIST(nlmp);
    767 		olmco = 0;
    768 		nlmco = ALIST_OFF_DATA;
    769 	}
    770 
    771 	/*
    772 	 * Finish processing the objects associated with this request.
    773 	 */
    774 	if (((nlmp = analyze_lmc(lml, nlmco, nlmp, in_nfavl)) == NULL) ||
    775 	    (relocate_lmc(lml, nlmco, clmp, nlmp, in_nfavl) == 0)) {
    776 		ghp = NULL;
    777 		nlmp = NULL;
    778 	}
    779 
    780 	/*
    781 	 * If the dlopen has failed, clean up any objects that might have been
    782 	 * loaded successfully on this new link-map control list.
    783 	 */
    784 	if (olmco && (nlmp == NULL))
    785 		remove_lmc(lml, clmp, olmco, path);
    786 
    787 	/*
    788 	 * Finally, remove any temporary link-map control list.  Note, if this
    789 	 * operation successfully established a new link-map list, then a base
    790 	 * link-map control list will have been created, which must remain.
    791 	 */
    792 	if (olmco && ((nlmp == NULL) || (olml != (Lm_list *)LM_ID_NEWLM)))
    793 		remove_cntl(lml, olmco);
    794 
    795 	return (ghp);
    796 }
    797 
    798 /*
    799  * dlopen() and dlsym() operations are the means by which a process can
    800  * test for the existence of required dependencies.  If the necessary
    801  * dependencies don't exist, then associated functionality can't be used.
    802  * However, the lack of dependencies can be fixed, and the dlopen() and
    803  * dlsym() requests can be repeated.  As we use a "not-found" AVL tree to
    804  * cache any failed full path loads, secondary dlopen() and dlsym() requests
    805  * will fail, even if the dependencies have been installed.
    806  *
    807  * dlopen() and dlsym() retry any failures by removing the "not-found" AVL
    808  * tree.  Should any dependencies be found, their names are added to the
    809  * FullPath AVL tree.  This routine removes any new "not-found" AVL tree,
    810  * so that the dlopen() or dlsym() can replace the original "not-found" tree.
    811  */
    812 inline static void
    813 nfavl_remove(avl_tree_t *avlt)
    814 {
    815 	PathNode	*pnp;
    816 	void		*cookie = NULL;
    817 
    818 	if (avlt) {
    819 		while ((pnp = avl_destroy_nodes(avlt, &cookie)) != NULL)
    820 			free(pnp);
    821 
    822 		avl_destroy(avlt);
    823 		free(avlt);
    824 	}
    825 }
    826 
    827 /*
    828  * Internal dlopen() activity.  Called from user level or directly for internal
    829  * opens that require a handle.
    830  */
    831 Grp_hdl *
    832 dlmopen_intn(Lm_list *lml, const char *path, int mode, Rt_map *clmp,
    833     uint_t flags, uint_t orig)
    834 {
    835 	Lm_list	*olml = lml;
    836 	Rt_map	*dlmp = NULL;
    837 	Grp_hdl	*ghp;
    838 	int	in_nfavl = 0;
    839 
    840 	/*
    841 	 * Check for magic link-map list values:
    842 	 *
    843 	 *  LM_ID_BASE:		Operate on the PRIMARY (executables) link map
    844 	 *  LM_ID_LDSO:		Operation on ld.so.1's link map
    845 	 *  LM_ID_NEWLM: 	Create a new link-map.
    846 	 */
    847 	if (lml == (Lm_list *)LM_ID_NEWLM) {
    848 		if ((lml = calloc(sizeof (Lm_list), 1)) == NULL)
    849 			return (NULL);
    850 
    851 		/*
    852 		 * Establish the new link-map flags from the callers and those
    853 		 * explicitly provided.
    854 		 */
    855 		lml->lm_tflags = LIST(clmp)->lm_tflags;
    856 		if (flags & FLG_RT_AUDIT) {
    857 			/*
    858 			 * Unset any auditing flags - an auditor shouldn't be
    859 			 * audited.  Insure all audit dependencies are loaded.
    860 			 */
    861 			lml->lm_tflags &= ~LML_TFLG_AUD_MASK;
    862 			lml->lm_tflags |=
    863 			    (LML_TFLG_NOLAZYLD | LML_TFLG_LOADFLTR);
    864 			lml->lm_flags |= LML_FLG_NOAUDIT;
    865 		}
    866 
    867 		if (aplist_append(&dynlm_list, lml, AL_CNT_DYNLIST) == NULL) {
    868 			free(lml);
    869 			return (NULL);
    870 		}
    871 		if (newlmid(lml) == 0) {
    872 			(void) aplist_delete_value(dynlm_list, lml);
    873 			free(lml);
    874 			return (NULL);
    875 		}
    876 	} else if ((uintptr_t)lml < LM_ID_NUM) {
    877 		if ((uintptr_t)lml == LM_ID_BASE)
    878 			lml = &lml_main;
    879 		else if ((uintptr_t)lml == LM_ID_LDSO)
    880 			lml = &lml_rtld;
    881 	}
    882 
    883 	/*
    884 	 * Open the required object on the associated link-map list.
    885 	 */
    886 	ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig, &in_nfavl);
    887 
    888 	/*
    889 	 * If the object could not be found it is possible that the "not-found"
    890 	 * AVL tree had indicated that the file does not exist.  In case the
    891 	 * file system has changed since this "not-found" recording was made,
    892 	 * retry the dlopen() with a clean "not-found" AVL tree.
    893 	 */
    894 	if ((ghp == NULL) && in_nfavl) {
    895 		avl_tree_t	*oavlt = nfavl;
    896 
    897 		nfavl = NULL;
    898 		ghp = dlmopen_core(lml, olml, path, mode, clmp, flags, orig,
    899 		    NULL);
    900 
    901 		/*
    902 		 * If the file is found, then its full path name will have been
    903 		 * registered in the FullPath AVL tree.  Remove any new
    904 		 * "not-found" AVL information, and restore the former AVL tree.
    905 		 */
    906 		nfavl_remove(nfavl);
    907 		nfavl = oavlt;
    908 	}
    909 
    910 	/*
    911 	 * Establish the new link-map from which .init processing will begin.
    912 	 * Ignore .init firing when constructing a configuration file (crle(1)).
    913 	 */
    914 	if (ghp && ((mode & RTLD_CONFGEN) == 0))
    915 		dlmp = ghp->gh_ownlmp;
    916 
    917 	/*
    918 	 * If loading an auditor was requested, and the auditor already existed,
    919 	 * then the link-map returned will be to the original auditor.  Remove
    920 	 * the link-map control list that was created for this request.
    921 	 */
    922 	if (dlmp && (flags & FLG_RT_AUDIT) && (LIST(dlmp) != lml)) {
    923 		remove_lml(lml);
    924 		lml = LIST(dlmp);
    925 	}
    926 
    927 	/*
    928 	 * If this load failed, remove any alternative link-map list.
    929 	 */
    930 	if ((ghp == NULL) &&
    931 	    ((lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM)) == 0)) {
    932 		remove_lml(lml);
    933 		lml = NULL;
    934 	}
    935 
    936 	/*
    937 	 * Finish this load request.  If objects were loaded, .init processing
    938 	 * is computed.  Finally, the debuggers are informed of the link-map
    939 	 * lists being stable.
    940 	 */
    941 	load_completion(dlmp);
    942 
    943 	return (ghp);
    944 }
    945 
    946 /*
    947  * Argument checking for dlopen.  Only called via external entry.
    948  */
    949 static Grp_hdl *
    950 dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp)
    951 {
    952 	/*
    953 	 * Verify that a valid pathname has been supplied.
    954 	 */
    955 	if (path && (*path == '\0')) {
    956 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
    957 		return (0);
    958 	}
    959 
    960 	/*
    961 	 * Historically we've always verified the mode is either RTLD_NOW or
    962 	 * RTLD_LAZY.  RTLD_NOLOAD is valid by itself.  Use of LM_ID_NEWLM
    963 	 * requires a specific pathname, and use of RTLD_PARENT is meaningless.
    964 	 */
    965 	if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) {
    966 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1));
    967 		return (0);
    968 	}
    969 	if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) {
    970 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2));
    971 		return (0);
    972 	}
    973 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == NULL)) {
    974 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3));
    975 		return (0);
    976 	}
    977 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) {
    978 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_4));
    979 		return (0);
    980 	}
    981 
    982 	return (dlmopen_intn(lml, path, mode, clmp, 0, 0));
    983 }
    984 
    985 #pragma weak _dlopen = dlopen
    986 
    987 /*
    988  * External entry for dlopen(3dl).  On success, returns a pointer (handle) to
    989  * the structure containing information about the newly added object, ie. can
    990  * be used by dlsym(). On failure, returns a null pointer.
    991  */
    992 void *
    993 dlopen(const char *path, int mode)
    994 {
    995 	int	entry;
    996 	Rt_map	*clmp;
    997 	Grp_hdl	*ghp;
    998 	Lm_list	*lml;
    999 
   1000 	entry = enter(0);
   1001 
   1002 	clmp = _caller(caller(), CL_EXECDEF);
   1003 	lml = LIST(clmp);
   1004 
   1005 	ghp = dlmopen_check(lml, path, mode, clmp);
   1006 
   1007 	if (entry)
   1008 		leave(lml, 0);
   1009 	return ((void *)ghp);
   1010 }
   1011 
   1012 #pragma weak _dlmopen = dlmopen
   1013 
   1014 /*
   1015  * External entry for dlmopen(3dl).
   1016  */
   1017 void *
   1018 dlmopen(Lmid_t lmid, const char *path, int mode)
   1019 {
   1020 	int	entry;
   1021 	Rt_map	*clmp;
   1022 	Grp_hdl	*ghp;
   1023 
   1024 	entry = enter(0);
   1025 
   1026 	clmp = _caller(caller(), CL_EXECDEF);
   1027 
   1028 	ghp = dlmopen_check((Lm_list *)lmid, path, mode, clmp);
   1029 
   1030 	if (entry)
   1031 		leave(LIST(clmp), 0);
   1032 	return ((void *)ghp);
   1033 }
   1034 
   1035 /*
   1036  * Handle processing for dlsym.
   1037  */
   1038 Sym *
   1039 dlsym_handle(Grp_hdl *ghp, Slookup *slp, Rt_map **_lmp, uint_t *binfo,
   1040     int *in_nfavl)
   1041 {
   1042 	Rt_map		*nlmp, * lmp = ghp->gh_ownlmp;
   1043 	Rt_map		*clmp = slp->sl_cmap;
   1044 	const char	*name = slp->sl_name;
   1045 	Sym		*sym = NULL;
   1046 	Slookup		sl = *slp;
   1047 
   1048 	sl.sl_flags = (LKUP_FIRST | LKUP_SPEC);
   1049 
   1050 	/*
   1051 	 * Continue processing a dlsym request.  Lookup the required symbol in
   1052 	 * each link-map specified by the handle.
   1053 	 *
   1054 	 * To leverage off of lazy loading, dlsym() requests can result in two
   1055 	 * passes.  The first descends the link-maps of any objects already in
   1056 	 * the address space.  If the symbol isn't located, and lazy
   1057 	 * dependencies still exist, then a second pass is made to load these
   1058 	 * dependencies if applicable.  This model means that in the case where
   1059 	 * a symbol exists in more than one object, the one located may not be
   1060 	 * constant - this is the standard issue with lazy loading. In addition,
   1061 	 * attempting to locate a symbol that doesn't exist will result in the
   1062 	 * loading of all lazy dependencies on the given handle, which can
   1063 	 * defeat some of the advantages of lazy loading (look out JVM).
   1064 	 */
   1065 	if (ghp->gh_flags & GPH_ZERO) {
   1066 		Lm_list	*lml;
   1067 		uint_t	lazy = 0;
   1068 
   1069 		/*
   1070 		 * If this symbol lookup is triggered from a dlopen(0) handle,
   1071 		 * traverse the present link-map list looking for promiscuous
   1072 		 * entries.
   1073 		 */
   1074 		for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
   1075 			/*
   1076 			 * If this handle indicates we're only to look in the
   1077 			 * first object check whether we're done.
   1078 			 */
   1079 			if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST))
   1080 				return (NULL);
   1081 
   1082 			if (!(MODE(nlmp) & RTLD_GLOBAL))
   1083 				continue;
   1084 			if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
   1085 			    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
   1086 				continue;
   1087 
   1088 			sl.sl_imap = nlmp;
   1089 			if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo,
   1090 			    in_nfavl))
   1091 				return (sym);
   1092 
   1093 			/*
   1094 			 * Keep track of any global pending lazy loads.
   1095 			 */
   1096 			lazy += LAZY(nlmp);
   1097 		}
   1098 
   1099 		/*
   1100 		 * If we're unable to locate the symbol and this link-map list
   1101 		 * still has pending lazy dependencies, start loading them in an
   1102 		 * attempt to exhaust the search.  Note that as we're already
   1103 		 * traversing a dynamic linked list of link-maps there's no
   1104 		 * need for elf_lazy_find_sym() to descend the link-maps itself.
   1105 		 */
   1106 		lml = LIST(lmp);
   1107 		if (lazy) {
   1108 			DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
   1109 
   1110 			sl.sl_flags |= LKUP_NODESCENT;
   1111 
   1112 			for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
   1113 
   1114 				if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp))
   1115 					continue;
   1116 				if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
   1117 				    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
   1118 					continue;
   1119 
   1120 				sl.sl_imap = nlmp;
   1121 				if (sym = elf_lazy_find_sym(&sl, _lmp, binfo,
   1122 				    in_nfavl))
   1123 					return (sym);
   1124 			}
   1125 		}
   1126 	} else {
   1127 		/*
   1128 		 * Traverse the dlopen() handle searching all presently loaded
   1129 		 * link-maps.
   1130 		 */
   1131 		Grp_desc	*gdp;
   1132 		Aliste		idx;
   1133 		uint_t		lazy = 0;
   1134 
   1135 		for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
   1136 			nlmp = gdp->gd_depend;
   1137 
   1138 			if ((gdp->gd_flags & GPD_DLSYM) == 0)
   1139 				continue;
   1140 
   1141 			sl.sl_imap = nlmp;
   1142 			if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo,
   1143 			    in_nfavl))
   1144 				return (sym);
   1145 
   1146 			if (ghp->gh_flags & GPH_FIRST)
   1147 				return (NULL);
   1148 
   1149 			/*
   1150 			 * Keep track of any pending lazy loads associated
   1151 			 * with this handle.
   1152 			 */
   1153 			lazy += LAZY(nlmp);
   1154 		}
   1155 
   1156 		/*
   1157 		 * If we're unable to locate the symbol and this handle still
   1158 		 * has pending lazy dependencies, start loading the lazy
   1159 		 * dependencies, in an attempt to exhaust the search.
   1160 		 */
   1161 		if (lazy) {
   1162 			DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name));
   1163 
   1164 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
   1165 				nlmp = gdp->gd_depend;
   1166 
   1167 				if (((gdp->gd_flags & GPD_DLSYM) == 0) ||
   1168 				    (LAZY(nlmp) == 0))
   1169 					continue;
   1170 
   1171 				sl.sl_imap = nlmp;
   1172 				if (sym = elf_lazy_find_sym(&sl, _lmp,
   1173 				    binfo, in_nfavl))
   1174 					return (sym);
   1175 			}
   1176 		}
   1177 	}
   1178 	return (NULL);
   1179 }
   1180 
   1181 /*
   1182  * Core dlsym activity.  Selects symbol lookup method from handle.
   1183  */
   1184 void *
   1185 dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp,
   1186     int *in_nfavl)
   1187 {
   1188 	Sym		*sym = NULL;
   1189 	Syminfo		*sip;
   1190 	Slookup		sl;
   1191 	uint_t		binfo;
   1192 
   1193 	/*
   1194 	 * Initialize the symbol lookup data structure.
   1195 	 *
   1196 	 * Standard relocations are evaluated using the symbol index of the
   1197 	 * associated relocation symbol.  This index provides for loading
   1198 	 * any lazy dependency and establishing a direct binding if necessary.
   1199 	 * If a dlsym() operation originates from an object that contains a
   1200 	 * symbol table entry for the same name, then we need to establish the
   1201 	 * symbol index so that any dependency requirements can be triggered.
   1202 	 *
   1203 	 * Therefore, the first symbol lookup that is carried out is for the
   1204 	 * symbol name within the calling object.  If this symbol exists, the
   1205 	 * symbols index is computed, added to the Slookup data, and thus used
   1206 	 * to seed the real symbol lookup.
   1207 	 */
   1208 	SLOOKUP_INIT(sl, name, clmp, clmp, ld_entry_cnt, elf_hash(name),
   1209 	    0, 0, 0, LKUP_SYMNDX);
   1210 
   1211 	if (THIS_IS_ELF(clmp) &&
   1212 	    ((sym = SYMINTP(clmp)(&sl, 0, 0, NULL)) != NULL)) {
   1213 		sl.sl_rsymndx = (((ulong_t)sym -
   1214 		    (ulong_t)SYMTAB(clmp)) / SYMENT(clmp));
   1215 		sl.sl_rsym = sym;
   1216 	}
   1217 
   1218 	if (sym && (ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON)) {
   1219 		Rt_map	*hlmp = LIST(clmp)->lm_head;
   1220 
   1221 		/*
   1222 		 * If a symbol reference is known, and that reference indicates
   1223 		 * that the symbol is a singleton, then the search for the
   1224 		 * symbol must follow the default search path.
   1225 		 */
   1226 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0,
   1227 		    DBG_DLSYM_SINGLETON));
   1228 
   1229 		sl.sl_imap = hlmp;
   1230 		sl.sl_flags = LKUP_SPEC;
   1231 		if (handle == RTLD_PROBE)
   1232 			sl.sl_flags |= LKUP_NOFALLBACK;
   1233 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl);
   1234 
   1235 	} else if (handle == RTLD_NEXT) {
   1236 		Rt_map	*nlmp;
   1237 
   1238 		/*
   1239 		 * If this handle is RTLD_NEXT determine whether a lazy load
   1240 		 * from the caller might provide the next object.  This mimics
   1241 		 * the lazy loading initialization normally carried out by
   1242 		 * lookup_sym(), however here, we must do this up-front, as
   1243 		 * lookup_sym() will be used to inspect the next object.
   1244 		 */
   1245 		if ((sl.sl_rsymndx) && ((sip = SYMINFO(clmp)) != NULL)) {
   1246 			/* LINTED */
   1247 			sip = (Syminfo *)((char *)sip +
   1248 			    (sl.sl_rsymndx * SYMINENT(clmp)));
   1249 
   1250 			if ((sip->si_flags & SYMINFO_FLG_DIRECT) &&
   1251 			    (sip->si_boundto < SYMINFO_BT_LOWRESERVE))
   1252 				(void) elf_lazy_load(clmp, &sl,
   1253 				    sip->si_boundto, name, 0, NULL, in_nfavl);
   1254 
   1255 			/*
   1256 			 * Clear the symbol index, so as not to confuse
   1257 			 * lookup_sym() of the next object.
   1258 			 */
   1259 			sl.sl_rsymndx = 0;
   1260 			sl.sl_rsym = NULL;
   1261 		}
   1262 
   1263 		/*
   1264 		 * If the handle is RTLD_NEXT start searching in the next link
   1265 		 * map from the callers.  Determine permissions from the
   1266 		 * present link map.  Indicate to lookup_sym() that we're on an
   1267 		 * RTLD_NEXT request so that it will use the callers link map to
   1268 		 * start any possible lazy dependency loading.
   1269 		 */
   1270 		sl.sl_imap = nlmp = NEXT_RT_MAP(clmp);
   1271 
   1272 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl,
   1273 		    (nlmp ? NAME(nlmp) : MSG_INTL(MSG_STR_NULL)),
   1274 		    DBG_DLSYM_NEXT));
   1275 
   1276 		if (nlmp == NULL)
   1277 			return (0);
   1278 
   1279 		sl.sl_flags = LKUP_NEXT;
   1280 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl);
   1281 
   1282 	} else if (handle == RTLD_SELF) {
   1283 		/*
   1284 		 * If the handle is RTLD_SELF start searching from the caller.
   1285 		 */
   1286 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, NAME(clmp),
   1287 		    DBG_DLSYM_SELF));
   1288 
   1289 		sl.sl_imap = clmp;
   1290 		sl.sl_flags = (LKUP_SPEC | LKUP_SELF);
   1291 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl);
   1292 
   1293 	} else if (handle == RTLD_DEFAULT) {
   1294 		Rt_map	*hlmp = LIST(clmp)->lm_head;
   1295 
   1296 		/*
   1297 		 * If the handle is RTLD_DEFAULT mimic the standard symbol
   1298 		 * lookup as would be triggered by a relocation.
   1299 		 */
   1300 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0,
   1301 		    DBG_DLSYM_DEFAULT));
   1302 
   1303 		sl.sl_imap = hlmp;
   1304 		sl.sl_flags = LKUP_SPEC;
   1305 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl);
   1306 
   1307 	} else if (handle == RTLD_PROBE) {
   1308 		Rt_map	*hlmp = LIST(clmp)->lm_head;
   1309 
   1310 		/*
   1311 		 * If the handle is RTLD_PROBE, mimic the standard symbol
   1312 		 * lookup as would be triggered by a relocation, however do
   1313 		 * not fall back to a lazy loading rescan if the symbol can't be
   1314 		 * found within the currently loaded objects.  Note, a lazy
   1315 		 * loaded dependency required by the caller might still get
   1316 		 * loaded to satisfy this request, but no exhaustive lazy load
   1317 		 * rescan is carried out.
   1318 		 */
   1319 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0,
   1320 		    DBG_DLSYM_PROBE));
   1321 
   1322 		sl.sl_imap = hlmp;
   1323 		sl.sl_flags = (LKUP_SPEC | LKUP_NOFALLBACK);
   1324 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo, in_nfavl);
   1325 
   1326 	} else {
   1327 		Grp_hdl *ghp = (Grp_hdl *)handle;
   1328 
   1329 		/*
   1330 		 * Look in the shared object specified by the handle and in all
   1331 		 * of its dependencies.
   1332 		 */
   1333 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl,
   1334 		    NAME(ghp->gh_ownlmp), DBG_DLSYM_DEF));
   1335 
   1336 		sym = LM_DLSYM(clmp)(ghp, &sl, dlmp, &binfo, in_nfavl);
   1337 	}
   1338 
   1339 	if (sym) {
   1340 		Lm_list	*lml = LIST(clmp);
   1341 		Addr	addr = sym->st_value;
   1342 
   1343 		if (!(FLAGS(*dlmp) & FLG_RT_FIXED))
   1344 			addr += ADDR(*dlmp);
   1345 
   1346 		/*
   1347 		 * Indicate that the defining object is now used.
   1348 		 */
   1349 		if (*dlmp != clmp)
   1350 			FLAGS1(*dlmp) |= FL1_RT_USED;
   1351 
   1352 		DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE,
   1353 		    *dlmp, addr, sym->st_value, name, binfo));
   1354 
   1355 		if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_SYMBIND) {
   1356 			uint_t	sb_flags = LA_SYMB_DLSYM;
   1357 			/* LINTED */
   1358 			uint_t	symndx = (uint_t)(((Xword)sym -
   1359 			    (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp));
   1360 			addr = audit_symbind(clmp, *dlmp, sym, symndx, addr,
   1361 			    &sb_flags);
   1362 		}
   1363 		return ((void *)addr);
   1364 	} else
   1365 		return (0);
   1366 }
   1367 
   1368 /*
   1369  * Internal dlsym activity.  Called from user level or directly for internal
   1370  * symbol lookup.
   1371  */
   1372 void *
   1373 dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
   1374 {
   1375 	Rt_map		*llmp = NULL;
   1376 	void		*error;
   1377 	Aliste		idx;
   1378 	Grp_desc	*gdp;
   1379 	int		in_nfavl = 0;
   1380 
   1381 	/*
   1382 	 * While looking for symbols it's quite possible that additional objects
   1383 	 * get loaded from lazy loading.  These objects will have been added to
   1384 	 * the same link-map list as those objects on the handle.  Remember this
   1385 	 * list for later investigation.
   1386 	 */
   1387 	if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) ||
   1388 	    (handle == RTLD_SELF) || (handle == RTLD_PROBE))
   1389 		llmp = LIST(clmp)->lm_tail;
   1390 	else {
   1391 		Grp_hdl	*ghp = (Grp_hdl *)handle;
   1392 
   1393 		if (ghp->gh_ownlmp)
   1394 			llmp = LIST(ghp->gh_ownlmp)->lm_tail;
   1395 		else {
   1396 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
   1397 				if ((llmp =
   1398 				    LIST(gdp->gd_depend)->lm_tail) != NULL)
   1399 					break;
   1400 			}
   1401 		}
   1402 	}
   1403 
   1404 	error = dlsym_core(handle, name, clmp, dlmp, &in_nfavl);
   1405 
   1406 	/*
   1407 	 * If the symbol could not be found it is possible that the "not-found"
   1408 	 * AVL tree had indicated that a required file does not exist.  In case
   1409 	 * the file system has changed since this "not-found" recording was
   1410 	 * made, retry the dlsym() with a clean "not-found" AVL tree.
   1411 	 */
   1412 	if ((error == NULL) && in_nfavl) {
   1413 		avl_tree_t	*oavlt = nfavl;
   1414 
   1415 		nfavl = NULL;
   1416 		error = dlsym_core(handle, name, clmp, dlmp, NULL);
   1417 
   1418 		/*
   1419 		 * If the symbol is found, then any file that was loaded will
   1420 		 * have had its full path name registered in the FullPath AVL
   1421 		 * tree.  Remove any new "not-found" AVL information, and
   1422 		 * restore the former AVL tree.
   1423 		 */
   1424 		nfavl_remove(nfavl);
   1425 		nfavl = oavlt;
   1426 	}
   1427 
   1428 	if (error == NULL) {
   1429 		/*
   1430 		 * Cache the error message, as Java tends to fall through this
   1431 		 * code many times.
   1432 		 */
   1433 		if (nosym_str == NULL)
   1434 			nosym_str = MSG_INTL(MSG_GEN_NOSYM);
   1435 		eprintf(LIST(clmp), ERR_FATAL, nosym_str, name);
   1436 	}
   1437 
   1438 	load_completion(llmp);
   1439 	return (error);
   1440 }
   1441 
   1442 /*
   1443  * Argument checking for dlsym.  Only called via external entry.
   1444  */
   1445 static void *
   1446 dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
   1447 {
   1448 	/*
   1449 	 * Verify the arguments.
   1450 	 */
   1451 	if (name == NULL) {
   1452 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM));
   1453 		return (NULL);
   1454 	}
   1455 	if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) &&
   1456 	    (handle != RTLD_SELF) && (handle != RTLD_PROBE) &&
   1457 	    (hdl_validate((Grp_hdl *)handle) == 0)) {
   1458 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
   1459 		    EC_NATPTR(handle));
   1460 		return (NULL);
   1461 	}
   1462 	return (dlsym_intn(handle, name, clmp, dlmp));
   1463 }
   1464 
   1465 
   1466 #pragma weak _dlsym = dlsym
   1467 
   1468 /*
   1469  * External entry for dlsym().  On success, returns the address of the specified
   1470  * symbol.  On error returns a null.
   1471  */
   1472 void *
   1473 dlsym(void *handle, const char *name)
   1474 {
   1475 	int	entry;
   1476 	Rt_map	*clmp, *dlmp = NULL;
   1477 	void	*addr;
   1478 
   1479 	entry = enter(0);
   1480 
   1481 	clmp = _caller(caller(), CL_EXECDEF);
   1482 
   1483 	addr = dlsym_check(handle, name, clmp, &dlmp);
   1484 
   1485 	if (entry) {
   1486 		if (dlmp)
   1487 			is_dep_init(dlmp, clmp);
   1488 		leave(LIST(clmp), 0);
   1489 	}
   1490 	return (addr);
   1491 }
   1492 
   1493 /*
   1494  * Core dladdr activity.
   1495  */
   1496 static void
   1497 dladdr_core(Rt_map *clmp, void *addr, Dl_info_t *dlip, void **info, int flags)
   1498 {
   1499 	/*
   1500 	 * Set up generic information and any defaults.
   1501 	 */
   1502 	dlip->dli_fname = PATHNAME(clmp);
   1503 
   1504 	dlip->dli_fbase = (void *)ADDR(clmp);
   1505 	dlip->dli_sname = NULL;
   1506 	dlip->dli_saddr = NULL;
   1507 
   1508 	/*
   1509 	 * Determine the nearest symbol to this address.
   1510 	 */
   1511 	LM_DLADDR(clmp)((ulong_t)addr, clmp, dlip, info, flags);
   1512 }
   1513 
   1514 #pragma weak _dladdr = dladdr
   1515 
   1516 /*
   1517  * External entry for dladdr(3dl) and dladdr1(3dl).  Returns an information
   1518  * structure that reflects the symbol closest to the address specified.
   1519  */
   1520 int
   1521 dladdr(void *addr, Dl_info_t *dlip)
   1522 {
   1523 	int	entry, error;
   1524 	Rt_map	*clmp;
   1525 
   1526 	entry = enter(0);
   1527 
   1528 	/*
   1529 	 * Use our calling technique to determine what object is associated
   1530 	 * with the supplied address.  If a caller can't be determined,
   1531 	 * indicate the failure.
   1532 	 */
   1533 	if ((clmp = _caller(addr, CL_NONE)) == NULL) {
   1534 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
   1535 		    EC_NATPTR(addr));
   1536 		error = 0;
   1537 	} else {
   1538 		dladdr_core(clmp, addr, dlip, 0, 0);
   1539 		error = 1;
   1540 	}
   1541 
   1542 	if (entry)
   1543 		leave(0, 0);
   1544 	return (error);
   1545 }
   1546 
   1547 #pragma weak _dladdr1 = dladdr1
   1548 
   1549 int
   1550 dladdr1(void *addr, Dl_info_t *dlip, void **info, int flags)
   1551 {
   1552 	int	entry, error = 0;
   1553 	Rt_map	*clmp;
   1554 
   1555 	/*
   1556 	 * Validate any flags.
   1557 	 */
   1558 	if (flags) {
   1559 		int	request;
   1560 
   1561 		if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) &&
   1562 		    (request != RTLD_DL_LINKMAP)) {
   1563 			eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS),
   1564 			    flags);
   1565 			return (0);
   1566 		}
   1567 		if (info == NULL) {
   1568 			eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO), flags);
   1569 			return (0);
   1570 		}
   1571 	}
   1572 
   1573 	entry = enter(0);
   1574 
   1575 	/*
   1576 	 * Use our calling technique to determine what object is associated
   1577 	 * with the supplied address.  If a caller can't be determined,
   1578 	 * indicate the failure.
   1579 	 */
   1580 	if ((clmp = _caller(addr, CL_NONE)) == NULL) {
   1581 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
   1582 		    EC_NATPTR(addr));
   1583 		error = 0;
   1584 	} else {
   1585 		dladdr_core(clmp, addr, dlip, info, flags);
   1586 		error = 1;
   1587 	}
   1588 
   1589 	if (entry)
   1590 		leave(0, 0);
   1591 	return (error);
   1592 }
   1593 
   1594 /*
   1595  * Core dldump activity.
   1596  */
   1597 static int
   1598 dldump_core(Lm_list *lml, const char *ipath, const char *opath, int flags)
   1599 {
   1600 	Addr	addr = 0;
   1601 	Rt_map	*lmp;
   1602 
   1603 	/*
   1604 	 * Verify any arguments first.
   1605 	 */
   1606 	if ((!opath || (*opath == '\0')) || (ipath && (*ipath == '\0'))) {
   1607 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
   1608 		return (1);
   1609 	}
   1610 
   1611 	/*
   1612 	 * If an input file is specified make sure its one of our dependencies
   1613 	 * on the main link-map list.  Note, this has really all evolved for
   1614 	 * crle(), which uses libcrle.so on an alternative link-map to trigger
   1615 	 * dumping objects from the main link-map list.   If we ever want to
   1616 	 * dump objects from alternative link-maps, this model is going to
   1617 	 * have to be revisited.
   1618 	 */
   1619 	if (ipath) {
   1620 		if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == NULL) {
   1621 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE),
   1622 			    ipath);
   1623 			return (1);
   1624 		}
   1625 		if (FLAGS(lmp) & FLG_RT_ALTER) {
   1626 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath);
   1627 			return (1);
   1628 		}
   1629 		if (FLAGS(lmp) & FLG_RT_NODUMP) {
   1630 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP),
   1631 			    ipath);
   1632 			return (1);
   1633 		}
   1634 	} else
   1635 		lmp = lml_main.lm_head;
   1636 
   1637 
   1638 	DBG_CALL(Dbg_file_dldump(lmp, opath, flags));
   1639 
   1640 	/*
   1641 	 * If the object being dump'ed isn't fixed identify its mapping.
   1642 	 */
   1643 	if (!(FLAGS(lmp) & FLG_RT_FIXED))
   1644 		addr = ADDR(lmp);
   1645 
   1646 	/*
   1647 	 * As rt_dldump() will effectively lazy load the necessary support
   1648 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
   1649 	 */
   1650 	if (elf_rtld_load() == 0)
   1651 		return (0);
   1652 
   1653 	/*
   1654 	 * Dump the required image.
   1655 	 */
   1656 	return (rt_dldump(lmp, opath, flags, addr));
   1657 }
   1658 
   1659 #pragma weak _dldump = dldump
   1660 
   1661 /*
   1662  * External entry for dldump(3c).  Returns 0 on success, non-zero otherwise.
   1663  */
   1664 int
   1665 dldump(const char *ipath, const char *opath, int flags)
   1666 {
   1667 	int	error, entry;
   1668 	Rt_map	*clmp;
   1669 
   1670 	entry = enter(0);
   1671 
   1672 	clmp = _caller(caller(), CL_EXECDEF);
   1673 
   1674 	error = dldump_core(LIST(clmp), ipath, opath, flags);
   1675 
   1676 	if (entry)
   1677 		leave(LIST(clmp), 0);
   1678 	return (error);
   1679 }
   1680 
   1681 /*
   1682  * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by
   1683  * the rtld_db and dlmopen() interfaces.  It checks to see if the Link_map is
   1684  * one of the primary ones and if so returns it's special token:
   1685  *		LM_ID_BASE
   1686  *		LM_ID_LDSO
   1687  *
   1688  * If it's not one of the primary link_map id's it will instead returns a
   1689  * pointer to the Lm_list structure which uniquely identifies the Link_map.
   1690  */
   1691 Lmid_t
   1692 get_linkmap_id(Lm_list *lml)
   1693 {
   1694 	if (lml->lm_flags & LML_FLG_BASELM)
   1695 		return (LM_ID_BASE);
   1696 	if (lml->lm_flags & LML_FLG_RTLDLM)
   1697 		return (LM_ID_LDSO);
   1698 
   1699 	return ((Lmid_t)lml);
   1700 }
   1701 
   1702 /*
   1703  * Extract information for a dlopen() handle.
   1704  */
   1705 static int
   1706 dlinfo_core(void *handle, int request, void *p, Rt_map *clmp)
   1707 {
   1708 	Lm_list	*lml = LIST(clmp);
   1709 	Rt_map	*lmp;
   1710 
   1711 	if ((request > RTLD_DI_MAX) || (p == NULL)) {
   1712 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL));
   1713 		return (-1);
   1714 	}
   1715 
   1716 	/*
   1717 	 * Return configuration cache name and address.
   1718 	 */
   1719 	if (request == RTLD_DI_CONFIGADDR) {
   1720 		Dl_info_t	*dlip = (Dl_info_t *)p;
   1721 
   1722 		if ((config->c_name == NULL) || (config->c_bgn == 0) ||
   1723 		    (config->c_end == 0)) {
   1724 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG));
   1725 			return (-1);
   1726 		}
   1727 		dlip->dli_fname = config->c_name;
   1728 		dlip->dli_fbase = (void *)config->c_bgn;
   1729 		return (0);
   1730 	}
   1731 
   1732 	/*
   1733 	 * Return profiled object name (used by ldprof audit library).
   1734 	 */
   1735 	if (request == RTLD_DI_PROFILENAME) {
   1736 		if (profile_name == NULL) {
   1737 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME));
   1738 			return (-1);
   1739 		}
   1740 
   1741 		*(const char **)p = profile_name;
   1742 		return (0);
   1743 	}
   1744 	if (request == RTLD_DI_PROFILEOUT) {
   1745 		/*
   1746 		 * If a profile destination directory hasn't been specified
   1747 		 * provide a default.
   1748 		 */
   1749 		if (profile_out == NULL)
   1750 			profile_out = MSG_ORIG(MSG_PTH_VARTMP);
   1751 
   1752 		*(const char **)p = profile_out;
   1753 		return (0);
   1754 	}
   1755 
   1756 	/*
   1757 	 * Obtain or establish a termination signal.
   1758 	 */
   1759 	if (request == RTLD_DI_GETSIGNAL) {
   1760 		*(int *)p = killsig;
   1761 		return (0);
   1762 	}
   1763 
   1764 	if (request == RTLD_DI_SETSIGNAL) {
   1765 		sigset_t	set;
   1766 		int		sig = *(int *)p;
   1767 
   1768 		/*
   1769 		 * Determine whether the signal is in range.
   1770 		 */
   1771 		(void) sigfillset(&set);
   1772 		if (sigismember(&set, sig) != 1) {
   1773 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig);
   1774 			return (-1);
   1775 		}
   1776 
   1777 		killsig = sig;
   1778 		return (0);
   1779 	}
   1780 
   1781 	/*
   1782 	 * For any other request a link-map is required.  Verify the handle.
   1783 	 */
   1784 	if (handle == RTLD_SELF)
   1785 		lmp = clmp;
   1786 	else {
   1787 		Grp_hdl	*ghp = (Grp_hdl *)handle;
   1788 
   1789 		if (!hdl_validate(ghp)) {
   1790 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
   1791 			    EC_NATPTR(handle));
   1792 			return (-1);
   1793 		}
   1794 		lmp = ghp->gh_ownlmp;
   1795 	}
   1796 
   1797 	/*
   1798 	 * Obtain the process arguments, environment and auxv.  Note, as the
   1799 	 * environment can be modified by the user (putenv(3c)), reinitialize
   1800 	 * the environment pointer on each request.
   1801 	 */
   1802 	if (request == RTLD_DI_ARGSINFO) {
   1803 		Dl_argsinfo_t	*aip = (Dl_argsinfo_t *)p;
   1804 		Lm_list		*lml = LIST(lmp);
   1805 
   1806 		*aip = argsinfo;
   1807 		if (lml->lm_flags & LML_FLG_ENVIRON)
   1808 			aip->dla_envp = *(lml->lm_environ);
   1809 
   1810 		return (0);
   1811 	}
   1812 
   1813 	/*
   1814 	 * Return Lmid_t of the Link-Map list that the specified object is
   1815 	 * loaded on.
   1816 	 */
   1817 	if (request == RTLD_DI_LMID) {
   1818 		*(Lmid_t *)p = get_linkmap_id(LIST(lmp));
   1819 		return (0);
   1820 	}
   1821 
   1822 	/*
   1823 	 * Return a pointer to the Link-Map structure associated with the
   1824 	 * specified object.
   1825 	 */
   1826 	if (request == RTLD_DI_LINKMAP) {
   1827 		*(Link_map **)p = (Link_map *)lmp;
   1828 		return (0);
   1829 	}
   1830 
   1831 	/*
   1832 	 * Return search path information, or the size of the buffer required
   1833 	 * to store the information.
   1834 	 */
   1835 	if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) {
   1836 		Spath_desc	sd = { search_rules, NULL, 0 };
   1837 		Pdesc		*pdp;
   1838 		Dl_serinfo_t	*info;
   1839 		Dl_serpath_t	*path;
   1840 		char		*strs;
   1841 		size_t		size = sizeof (Dl_serinfo_t);
   1842 		uint_t		cnt = 0;
   1843 
   1844 		info = (Dl_serinfo_t *)p;
   1845 		path = &info->dls_serpath[0];
   1846 		strs = (char *)&info->dls_serpath[info->dls_cnt];
   1847 
   1848 		/*
   1849 		 * Traverse search path entries for this object.
   1850 		 */
   1851 		while ((pdp = get_next_dir(&sd, lmp, 0)) != NULL) {
   1852 			size_t	_size;
   1853 
   1854 			if (pdp->pd_pname == NULL)
   1855 				continue;
   1856 
   1857 			/*
   1858 			 * If configuration information exists, it's possible
   1859 			 * this path has been identified as non-existent, if so
   1860 			 * ignore it.
   1861 			 */
   1862 			if (pdp->pd_info) {
   1863 				Rtc_obj	*dobj = (Rtc_obj *)pdp->pd_info;
   1864 				if (dobj->co_flags & RTC_OBJ_NOEXIST)
   1865 					continue;
   1866 			}
   1867 
   1868 			/*
   1869 			 * Keep track of search path count and total info size.
   1870 			 */
   1871 			if (cnt++)
   1872 				size += sizeof (Dl_serpath_t);
   1873 			_size = pdp->pd_plen + 1;
   1874 			size += _size;
   1875 
   1876 			if (request == RTLD_DI_SERINFOSIZE)
   1877 				continue;
   1878 
   1879 			/*
   1880 			 * If we're filling in search path information, confirm
   1881 			 * there's sufficient space.
   1882 			 */
   1883 			if (size > info->dls_size) {
   1884 				eprintf(lml, ERR_FATAL,
   1885 				    MSG_INTL(MSG_ARG_SERSIZE),
   1886 				    EC_OFF(info->dls_size));
   1887 				return (-1);
   1888 			}
   1889 			if (cnt > info->dls_cnt) {
   1890 				eprintf(lml, ERR_FATAL,
   1891 				    MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt);
   1892 				return (-1);
   1893 			}
   1894 
   1895 			/*
   1896 			 * Append the path to the information buffer.
   1897 			 */
   1898 			(void) strcpy(strs, pdp->pd_pname);
   1899 			path->dls_name = strs;
   1900 			path->dls_flags = pdp->pd_flags;
   1901 
   1902 			strs = strs + _size;
   1903 			path++;
   1904 		}
   1905 
   1906 		/*
   1907 		 * If we're here to size the search buffer fill it in.
   1908 		 */
   1909 		if (request == RTLD_DI_SERINFOSIZE) {
   1910 			info->dls_size = size;
   1911 			info->dls_cnt = cnt;
   1912 		}
   1913 
   1914 		return (0);
   1915 	}
   1916 
   1917 	/*
   1918 	 * Return the origin of the object associated with this link-map.
   1919 	 * Basically return the dirname(1) of the objects fullpath.
   1920 	 */
   1921 	if (request == RTLD_DI_ORIGIN) {
   1922 		char	*str = (char *)p;
   1923 
   1924 		(void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp));
   1925 		str += DIRSZ(lmp);
   1926 		*str = '\0';
   1927 
   1928 		return (0);
   1929 	}
   1930 
   1931 	/*
   1932 	 * Return the number of object mappings, or the mapping information for
   1933 	 * this object.
   1934 	 */
   1935 	if (request == RTLD_DI_MMAPCNT) {
   1936 		uint_t	*cnt = (uint_t *)p;
   1937 
   1938 		*cnt = MMAPCNT(lmp);
   1939 		return (0);
   1940 	}
   1941 	if (request == RTLD_DI_MMAPS) {
   1942 		Dl_mapinfo_t	*mip = (Dl_mapinfo_t *)p;
   1943 
   1944 		if (mip->dlm_acnt && mip->dlm_maps) {
   1945 			uint_t	cnt = 0;
   1946 
   1947 			while ((cnt < mip->dlm_acnt) && (cnt < MMAPCNT(lmp))) {
   1948 				mip->dlm_maps[cnt] = MMAPS(lmp)[cnt];
   1949 				cnt++;
   1950 			}
   1951 			mip->dlm_rcnt = cnt;
   1952 		}
   1953 		return (0);
   1954 	}
   1955 
   1956 	return (0);
   1957 }
   1958 
   1959 #pragma weak _dlinfo = dlinfo
   1960 
   1961 /*
   1962  * External entry for dlinfo(3dl).
   1963  */
   1964 int
   1965 dlinfo(void *handle, int request, void *p)
   1966 {
   1967 	int	error, entry;
   1968 	Rt_map	*clmp;
   1969 
   1970 	entry = enter(0);
   1971 
   1972 	clmp = _caller(caller(), CL_EXECDEF);
   1973 
   1974 	error = dlinfo_core(handle, request, p, clmp);
   1975 
   1976 	if (entry)
   1977 		leave(LIST(clmp), 0);
   1978 	return (error);
   1979 }
   1980