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