xref: /titanic_51/usr/src/cmd/sgs/rtld/common/dlfcns.c (revision a31148363f598def767ac48c5d82e1572e44b935)
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 2010 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 int
1039 dlsym_handle(Grp_hdl *ghp, Slookup *slp, Sresult *srp, 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 	Slookup		sl = *slp;
1046 
1047 	sl.sl_flags = (LKUP_FIRST | LKUP_DLSYM | LKUP_SPEC);
1048 
1049 	/*
1050 	 * Continue processing a dlsym request.  Lookup the required symbol in
1051 	 * each link-map specified by the handle.
1052 	 *
1053 	 * To leverage off of lazy loading, dlsym() requests can result in two
1054 	 * passes.  The first descends the link-maps of any objects already in
1055 	 * the address space.  If the symbol isn't located, and lazy
1056 	 * dependencies still exist, then a second pass is made to load these
1057 	 * dependencies if applicable.  This model means that in the case where
1058 	 * a symbol exists in more than one object, the one located may not be
1059 	 * constant - this is the standard issue with lazy loading. In addition,
1060 	 * attempting to locate a symbol that doesn't exist will result in the
1061 	 * loading of all lazy dependencies on the given handle, which can
1062 	 * defeat some of the advantages of lazy loading (look out JVM).
1063 	 */
1064 	if (ghp->gh_flags & GPH_ZERO) {
1065 		Lm_list	*lml;
1066 		uint_t	lazy = 0;
1067 
1068 		/*
1069 		 * If this symbol lookup is triggered from a dlopen(0) handle,
1070 		 * traverse the present link-map list looking for promiscuous
1071 		 * entries.
1072 		 */
1073 		for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
1074 			/*
1075 			 * If this handle indicates we're only to look in the
1076 			 * first object check whether we're done.
1077 			 */
1078 			if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST))
1079 				return (0);
1080 
1081 			if (!(MODE(nlmp) & RTLD_GLOBAL))
1082 				continue;
1083 			if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
1084 			    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
1085 				continue;
1086 
1087 			sl.sl_imap = nlmp;
1088 			if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl))
1089 				return (1);
1090 
1091 			/*
1092 			 * Keep track of any global pending lazy loads.
1093 			 */
1094 			lazy += LAZY(nlmp);
1095 		}
1096 
1097 		/*
1098 		 * If we're unable to locate the symbol and this link-map list
1099 		 * still has pending lazy dependencies, start loading them in an
1100 		 * attempt to exhaust the search.  Note that as we're already
1101 		 * traversing a dynamic linked list of link-maps there's no
1102 		 * need for elf_lazy_find_sym() to descend the link-maps itself.
1103 		 */
1104 		lml = LIST(lmp);
1105 		if (lazy) {
1106 			DBG_CALL(Dbg_syms_lazy_rescan(lml, name));
1107 
1108 			sl.sl_flags |= LKUP_NODESCENT;
1109 
1110 			for (nlmp = lmp; nlmp; nlmp = NEXT_RT_MAP(nlmp)) {
1111 
1112 				if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp))
1113 					continue;
1114 				if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
1115 				    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
1116 					continue;
1117 
1118 				sl.sl_imap = nlmp;
1119 				if (elf_lazy_find_sym(&sl, srp, binfo,
1120 				    in_nfavl))
1121 					return (1);
1122 			}
1123 		}
1124 	} else {
1125 		/*
1126 		 * Traverse the dlopen() handle searching all presently loaded
1127 		 * link-maps.
1128 		 */
1129 		Grp_desc	*gdp;
1130 		Aliste		idx;
1131 		uint_t		lazy = 0;
1132 
1133 		for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1134 			nlmp = gdp->gd_depend;
1135 
1136 			if ((gdp->gd_flags & GPD_DLSYM) == 0)
1137 				continue;
1138 
1139 			sl.sl_imap = nlmp;
1140 			if (LM_LOOKUP_SYM(clmp)(&sl, srp, binfo, in_nfavl))
1141 				return (1);
1142 
1143 			if (ghp->gh_flags & GPH_FIRST)
1144 				return (0);
1145 
1146 			/*
1147 			 * Keep track of any pending lazy loads associated
1148 			 * with this handle.
1149 			 */
1150 			lazy += LAZY(nlmp);
1151 		}
1152 
1153 		/*
1154 		 * If we're unable to locate the symbol and this handle still
1155 		 * has pending lazy dependencies, start loading the lazy
1156 		 * dependencies, in an attempt to exhaust the search.
1157 		 */
1158 		if (lazy) {
1159 			DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name));
1160 
1161 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1162 				nlmp = gdp->gd_depend;
1163 
1164 				if (((gdp->gd_flags & GPD_DLSYM) == 0) ||
1165 				    (LAZY(nlmp) == 0))
1166 					continue;
1167 
1168 				sl.sl_imap = nlmp;
1169 				if (elf_lazy_find_sym(&sl, srp, binfo,
1170 				    in_nfavl))
1171 					return (1);
1172 			}
1173 		}
1174 	}
1175 	return (0);
1176 }
1177 
1178 /*
1179  * Core dlsym activity.  Selects symbol lookup method from handle.
1180  */
1181 static void *
1182 dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp,
1183     int *in_nfavl)
1184 {
1185 	Sym		*sym = NULL;
1186 	int		ret = 0;
1187 	Syminfo		*sip;
1188 	Slookup		sl;
1189 	Sresult		sr;
1190 	uint_t		binfo;
1191 
1192 	/*
1193 	 * Initialize the symbol lookup data structure.
1194 	 *
1195 	 * Standard relocations are evaluated using the symbol index of the
1196 	 * associated relocation symbol.  This index provides for loading
1197 	 * any lazy dependency and establishing a direct binding if necessary.
1198 	 * If a dlsym() operation originates from an object that contains a
1199 	 * symbol table entry for the same name, then we need to establish the
1200 	 * symbol index so that any dependency requirements can be triggered.
1201 	 *
1202 	 * Therefore, the first symbol lookup that is carried out is for the
1203 	 * symbol name within the calling object.  If this symbol exists, the
1204 	 * symbols index is computed, added to the Slookup data, and thus used
1205 	 * to seed the real symbol lookup.
1206 	 */
1207 	SLOOKUP_INIT(sl, name, clmp, clmp, ld_entry_cnt, elf_hash(name),
1208 	    0, 0, 0, LKUP_SYMNDX);
1209 	SRESULT_INIT(sr, name);
1210 
1211 	if (THIS_IS_ELF(clmp) && SYMINTP(clmp)(&sl, &sr, &binfo, NULL)) {
1212 		sym = sr.sr_sym;
1213 
1214 		sl.sl_rsymndx = (((ulong_t)sym -
1215 		    (ulong_t)SYMTAB(clmp)) / SYMENT(clmp));
1216 		sl.sl_rsym = sym;
1217 	}
1218 
1219 	SRESULT_INIT(sr, name);
1220 
1221 	if (sym && (ELF_ST_VISIBILITY(sym->st_other) == STV_SINGLETON)) {
1222 		Rt_map	*hlmp = LIST(clmp)->lm_head;
1223 
1224 		/*
1225 		 * If a symbol reference is known, and that reference indicates
1226 		 * that the symbol is a singleton, then the search for the
1227 		 * symbol must follow the default search path.
1228 		 */
1229 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0,
1230 		    DBG_DLSYM_SINGLETON));
1231 
1232 		sl.sl_imap = hlmp;
1233 		sl.sl_flags = LKUP_SPEC;
1234 		if (handle == RTLD_PROBE)
1235 			sl.sl_flags |= LKUP_NOFALLBACK;
1236 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
1237 
1238 	} else if (handle == RTLD_NEXT) {
1239 		Rt_map	*nlmp;
1240 
1241 		/*
1242 		 * If this handle is RTLD_NEXT determine whether a lazy load
1243 		 * from the caller might provide the next object.  This mimics
1244 		 * the lazy loading initialization normally carried out by
1245 		 * lookup_sym(), however here, we must do this up-front, as
1246 		 * lookup_sym() will be used to inspect the next object.
1247 		 */
1248 		if ((sl.sl_rsymndx) && ((sip = SYMINFO(clmp)) != NULL)) {
1249 			/* LINTED */
1250 			sip = (Syminfo *)((char *)sip +
1251 			    (sl.sl_rsymndx * SYMINENT(clmp)));
1252 
1253 			if ((sip->si_flags & SYMINFO_FLG_DIRECT) &&
1254 			    (sip->si_boundto < SYMINFO_BT_LOWRESERVE))
1255 				(void) elf_lazy_load(clmp, &sl,
1256 				    sip->si_boundto, name, 0, NULL, in_nfavl);
1257 
1258 			/*
1259 			 * Clear the symbol index, so as not to confuse
1260 			 * lookup_sym() of the next object.
1261 			 */
1262 			sl.sl_rsymndx = 0;
1263 			sl.sl_rsym = NULL;
1264 		}
1265 
1266 		/*
1267 		 * If the handle is RTLD_NEXT start searching in the next link
1268 		 * map from the callers.  Determine permissions from the
1269 		 * present link map.  Indicate to lookup_sym() that we're on an
1270 		 * RTLD_NEXT request so that it will use the callers link map to
1271 		 * start any possible lazy dependency loading.
1272 		 */
1273 		sl.sl_imap = nlmp = NEXT_RT_MAP(clmp);
1274 
1275 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl,
1276 		    (nlmp ? NAME(nlmp) : MSG_INTL(MSG_STR_NULL)),
1277 		    DBG_DLSYM_NEXT));
1278 
1279 		if (nlmp == NULL)
1280 			return (0);
1281 
1282 		sl.sl_flags = LKUP_NEXT;
1283 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
1284 
1285 	} else if (handle == RTLD_SELF) {
1286 		/*
1287 		 * If the handle is RTLD_SELF start searching from the caller.
1288 		 */
1289 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, NAME(clmp),
1290 		    DBG_DLSYM_SELF));
1291 
1292 		sl.sl_imap = clmp;
1293 		sl.sl_flags = (LKUP_SPEC | LKUP_SELF);
1294 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
1295 
1296 	} else if (handle == RTLD_DEFAULT) {
1297 		Rt_map	*hlmp = LIST(clmp)->lm_head;
1298 
1299 		/*
1300 		 * If the handle is RTLD_DEFAULT mimic the standard symbol
1301 		 * lookup as would be triggered by a relocation.
1302 		 */
1303 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0,
1304 		    DBG_DLSYM_DEFAULT));
1305 
1306 		sl.sl_imap = hlmp;
1307 		sl.sl_flags = LKUP_SPEC;
1308 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
1309 
1310 	} else if (handle == RTLD_PROBE) {
1311 		Rt_map	*hlmp = LIST(clmp)->lm_head;
1312 
1313 		/*
1314 		 * If the handle is RTLD_PROBE, mimic the standard symbol
1315 		 * lookup as would be triggered by a relocation, however do
1316 		 * not fall back to a lazy loading rescan if the symbol can't be
1317 		 * found within the currently loaded objects.  Note, a lazy
1318 		 * loaded dependency required by the caller might still get
1319 		 * loaded to satisfy this request, but no exhaustive lazy load
1320 		 * rescan is carried out.
1321 		 */
1322 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl, 0,
1323 		    DBG_DLSYM_PROBE));
1324 
1325 		sl.sl_imap = hlmp;
1326 		sl.sl_flags = (LKUP_SPEC | LKUP_NOFALLBACK);
1327 		ret = LM_LOOKUP_SYM(clmp)(&sl, &sr, &binfo, in_nfavl);
1328 
1329 	} else {
1330 		Grp_hdl *ghp = (Grp_hdl *)handle;
1331 
1332 		/*
1333 		 * Look in the shared object specified by the handle and in all
1334 		 * of its dependencies.
1335 		 */
1336 		DBG_CALL(Dbg_syms_dlsym(clmp, name, in_nfavl,
1337 		    NAME(ghp->gh_ownlmp), DBG_DLSYM_DEF));
1338 
1339 		ret = LM_DLSYM(clmp)(ghp, &sl, &sr, &binfo, in_nfavl);
1340 	}
1341 
1342 	if (ret && ((sym = sr.sr_sym) != NULL)) {
1343 		Lm_list	*lml = LIST(clmp);
1344 		Addr	addr = sym->st_value;
1345 
1346 		*dlmp = sr.sr_dmap;
1347 		if (!(FLAGS(*dlmp) & FLG_RT_FIXED))
1348 			addr += ADDR(*dlmp);
1349 
1350 		/*
1351 		 * Indicate that the defining object is now used.
1352 		 */
1353 		if (*dlmp != clmp)
1354 			FLAGS1(*dlmp) |= FL1_RT_USED;
1355 
1356 		DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE,
1357 		    *dlmp, addr, sym->st_value, sr.sr_name, binfo));
1358 
1359 		if ((lml->lm_tflags | AFLAGS(clmp)) & LML_TFLG_AUD_SYMBIND) {
1360 			uint_t	sb_flags = LA_SYMB_DLSYM;
1361 			/* LINTED */
1362 			uint_t	symndx = (uint_t)(((Xword)sym -
1363 			    (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp));
1364 			addr = audit_symbind(clmp, *dlmp, sym, symndx, addr,
1365 			    &sb_flags);
1366 		}
1367 		return ((void *)addr);
1368 	}
1369 
1370 	return (NULL);
1371 }
1372 
1373 /*
1374  * Internal dlsym activity.  Called from user level or directly for internal
1375  * symbol lookup.
1376  */
1377 void *
1378 dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
1379 {
1380 	Rt_map		*llmp = NULL;
1381 	void		*error;
1382 	Aliste		idx;
1383 	Grp_desc	*gdp;
1384 	int		in_nfavl = 0;
1385 
1386 	/*
1387 	 * While looking for symbols it's quite possible that additional objects
1388 	 * get loaded from lazy loading.  These objects will have been added to
1389 	 * the same link-map list as those objects on the handle.  Remember this
1390 	 * list for later investigation.
1391 	 */
1392 	if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) ||
1393 	    (handle == RTLD_SELF) || (handle == RTLD_PROBE))
1394 		llmp = LIST(clmp)->lm_tail;
1395 	else {
1396 		Grp_hdl	*ghp = (Grp_hdl *)handle;
1397 
1398 		if (ghp->gh_ownlmp)
1399 			llmp = LIST(ghp->gh_ownlmp)->lm_tail;
1400 		else {
1401 			for (ALIST_TRAVERSE(ghp->gh_depends, idx, gdp)) {
1402 				if ((llmp =
1403 				    LIST(gdp->gd_depend)->lm_tail) != NULL)
1404 					break;
1405 			}
1406 		}
1407 	}
1408 
1409 	error = dlsym_core(handle, name, clmp, dlmp, &in_nfavl);
1410 
1411 	/*
1412 	 * If the symbol could not be found it is possible that the "not-found"
1413 	 * AVL tree had indicated that a required file does not exist.  In case
1414 	 * the file system has changed since this "not-found" recording was
1415 	 * made, retry the dlsym() with a clean "not-found" AVL tree.
1416 	 */
1417 	if ((error == NULL) && in_nfavl) {
1418 		avl_tree_t	*oavlt = nfavl;
1419 
1420 		nfavl = NULL;
1421 		error = dlsym_core(handle, name, clmp, dlmp, NULL);
1422 
1423 		/*
1424 		 * If the symbol is found, then any file that was loaded will
1425 		 * have had its full path name registered in the FullPath AVL
1426 		 * tree.  Remove any new "not-found" AVL information, and
1427 		 * restore the former AVL tree.
1428 		 */
1429 		nfavl_remove(nfavl);
1430 		nfavl = oavlt;
1431 	}
1432 
1433 	if (error == NULL) {
1434 		/*
1435 		 * Cache the error message, as Java tends to fall through this
1436 		 * code many times.
1437 		 */
1438 		if (nosym_str == NULL)
1439 			nosym_str = MSG_INTL(MSG_GEN_NOSYM);
1440 		eprintf(LIST(clmp), ERR_FATAL, nosym_str, name);
1441 	}
1442 
1443 	load_completion(llmp);
1444 	return (error);
1445 }
1446 
1447 /*
1448  * Argument checking for dlsym.  Only called via external entry.
1449  */
1450 static void *
1451 dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
1452 {
1453 	/*
1454 	 * Verify the arguments.
1455 	 */
1456 	if (name == NULL) {
1457 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM));
1458 		return (NULL);
1459 	}
1460 	if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) &&
1461 	    (handle != RTLD_SELF) && (handle != RTLD_PROBE) &&
1462 	    (hdl_validate((Grp_hdl *)handle) == 0)) {
1463 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
1464 		    EC_NATPTR(handle));
1465 		return (NULL);
1466 	}
1467 	return (dlsym_intn(handle, name, clmp, dlmp));
1468 }
1469 
1470 
1471 #pragma weak _dlsym = dlsym
1472 
1473 /*
1474  * External entry for dlsym().  On success, returns the address of the specified
1475  * symbol.  On error returns a null.
1476  */
1477 void *
1478 dlsym(void *handle, const char *name)
1479 {
1480 	int	entry;
1481 	Rt_map	*clmp, *dlmp = NULL;
1482 	void	*addr;
1483 
1484 	entry = enter(0);
1485 
1486 	clmp = _caller(caller(), CL_EXECDEF);
1487 
1488 	addr = dlsym_check(handle, name, clmp, &dlmp);
1489 
1490 	if (entry) {
1491 		if (dlmp)
1492 			is_dep_init(dlmp, clmp);
1493 		leave(LIST(clmp), 0);
1494 	}
1495 	return (addr);
1496 }
1497 
1498 /*
1499  * Core dladdr activity.
1500  */
1501 static void
1502 dladdr_core(Rt_map *clmp, void *addr, Dl_info_t *dlip, void **info, int flags)
1503 {
1504 	/*
1505 	 * Set up generic information and any defaults.
1506 	 */
1507 	dlip->dli_fname = PATHNAME(clmp);
1508 
1509 	dlip->dli_fbase = (void *)ADDR(clmp);
1510 	dlip->dli_sname = NULL;
1511 	dlip->dli_saddr = NULL;
1512 
1513 	/*
1514 	 * Determine the nearest symbol to this address.
1515 	 */
1516 	LM_DLADDR(clmp)((ulong_t)addr, clmp, dlip, info, flags);
1517 }
1518 
1519 #pragma weak _dladdr = dladdr
1520 
1521 /*
1522  * External entry for dladdr(3dl) and dladdr1(3dl).  Returns an information
1523  * structure that reflects the symbol closest to the address specified.
1524  */
1525 int
1526 dladdr(void *addr, Dl_info_t *dlip)
1527 {
1528 	int	entry, error;
1529 	Rt_map	*clmp;
1530 
1531 	entry = enter(0);
1532 
1533 	/*
1534 	 * Use our calling technique to determine what object is associated
1535 	 * with the supplied address.  If a caller can't be determined,
1536 	 * indicate the failure.
1537 	 */
1538 	if ((clmp = _caller(addr, CL_NONE)) == NULL) {
1539 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
1540 		    EC_NATPTR(addr));
1541 		error = 0;
1542 	} else {
1543 		dladdr_core(clmp, addr, dlip, 0, 0);
1544 		error = 1;
1545 	}
1546 
1547 	if (entry)
1548 		leave(0, 0);
1549 	return (error);
1550 }
1551 
1552 #pragma weak _dladdr1 = dladdr1
1553 
1554 int
1555 dladdr1(void *addr, Dl_info_t *dlip, void **info, int flags)
1556 {
1557 	int	entry, error = 0;
1558 	Rt_map	*clmp;
1559 
1560 	/*
1561 	 * Validate any flags.
1562 	 */
1563 	if (flags) {
1564 		int	request;
1565 
1566 		if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) &&
1567 		    (request != RTLD_DL_LINKMAP)) {
1568 			eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS),
1569 			    flags);
1570 			return (0);
1571 		}
1572 		if (info == NULL) {
1573 			eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO), flags);
1574 			return (0);
1575 		}
1576 	}
1577 
1578 	entry = enter(0);
1579 
1580 	/*
1581 	 * Use our calling technique to determine what object is associated
1582 	 * with the supplied address.  If a caller can't be determined,
1583 	 * indicate the failure.
1584 	 */
1585 	if ((clmp = _caller(addr, CL_NONE)) == NULL) {
1586 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
1587 		    EC_NATPTR(addr));
1588 		error = 0;
1589 	} else {
1590 		dladdr_core(clmp, addr, dlip, info, flags);
1591 		error = 1;
1592 	}
1593 
1594 	if (entry)
1595 		leave(0, 0);
1596 	return (error);
1597 }
1598 
1599 /*
1600  * Core dldump activity.
1601  */
1602 static int
1603 dldump_core(Lm_list *lml, const char *ipath, const char *opath, int flags)
1604 {
1605 	Addr	addr = 0;
1606 	Rt_map	*lmp;
1607 
1608 	/*
1609 	 * Verify any arguments first.
1610 	 */
1611 	if ((!opath || (*opath == '\0')) || (ipath && (*ipath == '\0'))) {
1612 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
1613 		return (1);
1614 	}
1615 
1616 	/*
1617 	 * If an input file is specified make sure its one of our dependencies
1618 	 * on the main link-map list.  Note, this has really all evolved for
1619 	 * crle(), which uses libcrle.so on an alternative link-map to trigger
1620 	 * dumping objects from the main link-map list.   If we ever want to
1621 	 * dump objects from alternative link-maps, this model is going to
1622 	 * have to be revisited.
1623 	 */
1624 	if (ipath) {
1625 		if ((lmp = is_so_loaded(&lml_main, ipath, NULL)) == NULL) {
1626 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE),
1627 			    ipath);
1628 			return (1);
1629 		}
1630 		if (FLAGS(lmp) & FLG_RT_ALTER) {
1631 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath);
1632 			return (1);
1633 		}
1634 		if (FLAGS(lmp) & FLG_RT_NODUMP) {
1635 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP),
1636 			    ipath);
1637 			return (1);
1638 		}
1639 	} else
1640 		lmp = lml_main.lm_head;
1641 
1642 
1643 	DBG_CALL(Dbg_file_dldump(lmp, opath, flags));
1644 
1645 	/*
1646 	 * If the object being dump'ed isn't fixed identify its mapping.
1647 	 */
1648 	if (!(FLAGS(lmp) & FLG_RT_FIXED))
1649 		addr = ADDR(lmp);
1650 
1651 	/*
1652 	 * As rt_dldump() will effectively lazy load the necessary support
1653 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
1654 	 */
1655 	if (elf_rtld_load() == 0)
1656 		return (0);
1657 
1658 	/*
1659 	 * Dump the required image.
1660 	 */
1661 	return (rt_dldump(lmp, opath, flags, addr));
1662 }
1663 
1664 #pragma weak _dldump = dldump
1665 
1666 /*
1667  * External entry for dldump(3c).  Returns 0 on success, non-zero otherwise.
1668  */
1669 int
1670 dldump(const char *ipath, const char *opath, int flags)
1671 {
1672 	int	error, entry;
1673 	Rt_map	*clmp;
1674 
1675 	entry = enter(0);
1676 
1677 	clmp = _caller(caller(), CL_EXECDEF);
1678 
1679 	error = dldump_core(LIST(clmp), ipath, opath, flags);
1680 
1681 	if (entry)
1682 		leave(LIST(clmp), 0);
1683 	return (error);
1684 }
1685 
1686 /*
1687  * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by
1688  * the rtld_db and dlmopen() interfaces.  It checks to see if the Link_map is
1689  * one of the primary ones and if so returns it's special token:
1690  *		LM_ID_BASE
1691  *		LM_ID_LDSO
1692  *
1693  * If it's not one of the primary link_map id's it will instead returns a
1694  * pointer to the Lm_list structure which uniquely identifies the Link_map.
1695  */
1696 Lmid_t
1697 get_linkmap_id(Lm_list *lml)
1698 {
1699 	if (lml->lm_flags & LML_FLG_BASELM)
1700 		return (LM_ID_BASE);
1701 	if (lml->lm_flags & LML_FLG_RTLDLM)
1702 		return (LM_ID_LDSO);
1703 
1704 	return ((Lmid_t)lml);
1705 }
1706 
1707 /*
1708  * Extract information for a dlopen() handle.
1709  */
1710 static int
1711 dlinfo_core(void *handle, int request, void *p, Rt_map *clmp)
1712 {
1713 	Lm_list	*lml = LIST(clmp);
1714 	Rt_map	*lmp;
1715 
1716 	if ((request > RTLD_DI_MAX) || (p == NULL)) {
1717 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL));
1718 		return (-1);
1719 	}
1720 
1721 	/*
1722 	 * Return configuration cache name and address.
1723 	 */
1724 	if (request == RTLD_DI_CONFIGADDR) {
1725 		Dl_info_t	*dlip = (Dl_info_t *)p;
1726 
1727 		if ((config->c_name == NULL) || (config->c_bgn == 0) ||
1728 		    (config->c_end == 0)) {
1729 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG));
1730 			return (-1);
1731 		}
1732 		dlip->dli_fname = config->c_name;
1733 		dlip->dli_fbase = (void *)config->c_bgn;
1734 		return (0);
1735 	}
1736 
1737 	/*
1738 	 * Return profiled object name (used by ldprof audit library).
1739 	 */
1740 	if (request == RTLD_DI_PROFILENAME) {
1741 		if (profile_name == NULL) {
1742 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME));
1743 			return (-1);
1744 		}
1745 
1746 		*(const char **)p = profile_name;
1747 		return (0);
1748 	}
1749 	if (request == RTLD_DI_PROFILEOUT) {
1750 		/*
1751 		 * If a profile destination directory hasn't been specified
1752 		 * provide a default.
1753 		 */
1754 		if (profile_out == NULL)
1755 			profile_out = MSG_ORIG(MSG_PTH_VARTMP);
1756 
1757 		*(const char **)p = profile_out;
1758 		return (0);
1759 	}
1760 
1761 	/*
1762 	 * Obtain or establish a termination signal.
1763 	 */
1764 	if (request == RTLD_DI_GETSIGNAL) {
1765 		*(int *)p = killsig;
1766 		return (0);
1767 	}
1768 
1769 	if (request == RTLD_DI_SETSIGNAL) {
1770 		sigset_t	set;
1771 		int		sig = *(int *)p;
1772 
1773 		/*
1774 		 * Determine whether the signal is in range.
1775 		 */
1776 		(void) sigfillset(&set);
1777 		if (sigismember(&set, sig) != 1) {
1778 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig);
1779 			return (-1);
1780 		}
1781 
1782 		killsig = sig;
1783 		return (0);
1784 	}
1785 
1786 	/*
1787 	 * For any other request a link-map is required.  Verify the handle.
1788 	 */
1789 	if (handle == RTLD_SELF)
1790 		lmp = clmp;
1791 	else {
1792 		Grp_hdl	*ghp = (Grp_hdl *)handle;
1793 
1794 		if (!hdl_validate(ghp)) {
1795 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL),
1796 			    EC_NATPTR(handle));
1797 			return (-1);
1798 		}
1799 		lmp = ghp->gh_ownlmp;
1800 	}
1801 
1802 	/*
1803 	 * Obtain the process arguments, environment and auxv.  Note, as the
1804 	 * environment can be modified by the user (putenv(3c)), reinitialize
1805 	 * the environment pointer on each request.
1806 	 */
1807 	if (request == RTLD_DI_ARGSINFO) {
1808 		Dl_argsinfo_t	*aip = (Dl_argsinfo_t *)p;
1809 		Lm_list		*lml = LIST(lmp);
1810 
1811 		*aip = argsinfo;
1812 		if (lml->lm_flags & LML_FLG_ENVIRON)
1813 			aip->dla_envp = *(lml->lm_environ);
1814 
1815 		return (0);
1816 	}
1817 
1818 	/*
1819 	 * Return Lmid_t of the Link-Map list that the specified object is
1820 	 * loaded on.
1821 	 */
1822 	if (request == RTLD_DI_LMID) {
1823 		*(Lmid_t *)p = get_linkmap_id(LIST(lmp));
1824 		return (0);
1825 	}
1826 
1827 	/*
1828 	 * Return a pointer to the Link-Map structure associated with the
1829 	 * specified object.
1830 	 */
1831 	if (request == RTLD_DI_LINKMAP) {
1832 		*(Link_map **)p = (Link_map *)lmp;
1833 		return (0);
1834 	}
1835 
1836 	/*
1837 	 * Return search path information, or the size of the buffer required
1838 	 * to store the information.
1839 	 */
1840 	if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) {
1841 		Spath_desc	sd = { search_rules, NULL, 0 };
1842 		Pdesc		*pdp;
1843 		Dl_serinfo_t	*info;
1844 		Dl_serpath_t	*path;
1845 		char		*strs;
1846 		size_t		size = sizeof (Dl_serinfo_t);
1847 		uint_t		cnt = 0;
1848 
1849 		info = (Dl_serinfo_t *)p;
1850 		path = &info->dls_serpath[0];
1851 		strs = (char *)&info->dls_serpath[info->dls_cnt];
1852 
1853 		/*
1854 		 * Traverse search path entries for this object.
1855 		 */
1856 		while ((pdp = get_next_dir(&sd, lmp, 0)) != NULL) {
1857 			size_t	_size;
1858 
1859 			if (pdp->pd_pname == NULL)
1860 				continue;
1861 
1862 			/*
1863 			 * If configuration information exists, it's possible
1864 			 * this path has been identified as non-existent, if so
1865 			 * ignore it.
1866 			 */
1867 			if (pdp->pd_info) {
1868 				Rtc_obj	*dobj = (Rtc_obj *)pdp->pd_info;
1869 				if (dobj->co_flags & RTC_OBJ_NOEXIST)
1870 					continue;
1871 			}
1872 
1873 			/*
1874 			 * Keep track of search path count and total info size.
1875 			 */
1876 			if (cnt++)
1877 				size += sizeof (Dl_serpath_t);
1878 			_size = pdp->pd_plen + 1;
1879 			size += _size;
1880 
1881 			if (request == RTLD_DI_SERINFOSIZE)
1882 				continue;
1883 
1884 			/*
1885 			 * If we're filling in search path information, confirm
1886 			 * there's sufficient space.
1887 			 */
1888 			if (size > info->dls_size) {
1889 				eprintf(lml, ERR_FATAL,
1890 				    MSG_INTL(MSG_ARG_SERSIZE),
1891 				    EC_OFF(info->dls_size));
1892 				return (-1);
1893 			}
1894 			if (cnt > info->dls_cnt) {
1895 				eprintf(lml, ERR_FATAL,
1896 				    MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt);
1897 				return (-1);
1898 			}
1899 
1900 			/*
1901 			 * Append the path to the information buffer.
1902 			 */
1903 			(void) strcpy(strs, pdp->pd_pname);
1904 			path->dls_name = strs;
1905 			path->dls_flags = pdp->pd_flags;
1906 
1907 			strs = strs + _size;
1908 			path++;
1909 		}
1910 
1911 		/*
1912 		 * If we're here to size the search buffer fill it in.
1913 		 */
1914 		if (request == RTLD_DI_SERINFOSIZE) {
1915 			info->dls_size = size;
1916 			info->dls_cnt = cnt;
1917 		}
1918 
1919 		return (0);
1920 	}
1921 
1922 	/*
1923 	 * Return the origin of the object associated with this link-map.
1924 	 * Basically return the dirname(1) of the objects fullpath.
1925 	 */
1926 	if (request == RTLD_DI_ORIGIN) {
1927 		char	*str = (char *)p;
1928 
1929 		(void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp));
1930 		str += DIRSZ(lmp);
1931 		*str = '\0';
1932 
1933 		return (0);
1934 	}
1935 
1936 	/*
1937 	 * Return the number of object mappings, or the mapping information for
1938 	 * this object.
1939 	 */
1940 	if (request == RTLD_DI_MMAPCNT) {
1941 		uint_t	*cnt = (uint_t *)p;
1942 
1943 		*cnt = MMAPCNT(lmp);
1944 		return (0);
1945 	}
1946 	if (request == RTLD_DI_MMAPS) {
1947 		Dl_mapinfo_t	*mip = (Dl_mapinfo_t *)p;
1948 
1949 		if (mip->dlm_acnt && mip->dlm_maps) {
1950 			uint_t	cnt = 0;
1951 
1952 			while ((cnt < mip->dlm_acnt) && (cnt < MMAPCNT(lmp))) {
1953 				mip->dlm_maps[cnt] = MMAPS(lmp)[cnt];
1954 				cnt++;
1955 			}
1956 			mip->dlm_rcnt = cnt;
1957 		}
1958 		return (0);
1959 	}
1960 
1961 	return (0);
1962 }
1963 
1964 #pragma weak _dlinfo = dlinfo
1965 
1966 /*
1967  * External entry for dlinfo(3dl).
1968  */
1969 int
1970 dlinfo(void *handle, int request, void *p)
1971 {
1972 	int	error, entry;
1973 	Rt_map	*clmp;
1974 
1975 	entry = enter(0);
1976 
1977 	clmp = _caller(caller(), CL_EXECDEF);
1978 
1979 	error = dlinfo_core(handle, request, p, clmp);
1980 
1981 	if (entry)
1982 		leave(LIST(clmp), 0);
1983 	return (error);
1984 }
1985 
1986 
1987 /*
1988  * GNU defined function to iterate through the program headers for all
1989  * currently loaded dynamic objects. The caller supplies a callback function
1990  * which is called for each object.
1991  *
1992  * entry:
1993  *	callback - Callback function to call. The arguments to the callback
1994  *		function are:
1995  *		info - Address of dl_phdr_info structure
1996  *		size - sizeof (struct dl_phdr_info)
1997  *		data - Caller supplied value.
1998  *	data - Value supplied by caller, which is passed to callback without
1999  *		examination.
2000  *
2001  * exit:
2002  *	callback is called for each dynamic ELF object in the process address
2003  *	space, halting when a non-zero value is returned, or when the last
2004  *	object has been processed. The return value from the last call
2005  *	to callback is returned.
2006  *
2007  * note:
2008  *	The Linux implementation has added additional fields to the
2009  *	dl_phdr_info structure over time. The callback function is
2010  *	supposed to use the size field to determine which fields are
2011  *	present, and to avoid attempts to access non-existent fields.
2012  *	We have added those fields that are compatible with Solaris, and
2013  *	which are used by GNU C++ (g++) runtime exception handling support.
2014  *
2015  * note:
2016  *	We issue a callback for every ELF object mapped into the process
2017  *	address space at the time this routine is entered. These callbacks
2018  *	are arbitrary functions that can do anything, including possibly
2019  *	causing new objects to be mapped into the process, or unmapped.
2020  *	This complicates matters:
2021  *
2022  *	-	Adding new objects can cause the alists to be reallocated
2023  *		or for contents to move. This can happen explicitly via
2024  *		dlopen(), or implicitly via lazy loading. One might consider
2025  *		simply banning dlopen from a callback, but lazy loading must
2026  *		be allowed, in which case there's no reason to ban dlopen().
2027  *
2028  *	-	Removing objects can leave us holding references to freed
2029  *		memory that must not be accessed, and can cause the list
2030  *		items to move in a way that would cause us to miss reporting
2031  *		one, or double report others.
2032  *
2033  *	-	We cannot allocate memory to build a separate data structure,
2034  *		because the interface to dl_iterate_phdr() does not have a
2035  *		way to communicate allocation errors back to the caller.
2036  *		Even if we could, it would be difficult to do so efficiently.
2037  *
2038  *	-	It is possible for dl_iterate_phdr() to be called recursively
2039  *		from a callback, and there is no way for us to detect or manage
2040  *		this effectively, particularly as the user might use longjmp()
2041  *		to skip past us on return. Hence, we must be reentrant
2042  *		(stateless), further precluding the option of building a
2043  *		separate data structure.
2044  *
2045  *	Despite these constraints, we are able to traverse the link-map
2046  *	lists safely:
2047  *
2048  *	-	Once interposer (preload) objects have been processed at
2049  *		startup, we know that new objects are always placed at the
2050  *		end of the list. Hence, if we are reading a list when that
2051  *		happens, the new object will not alter the part of the list
2052  *		that we've already processed.
2053  *
2054  *	-	The alist _TRAVERSE macros recalculate the address of the
2055  *		current item from scratch on each iteration, rather than
2056  *		incrementing a pointer. Hence, alist additions that occur
2057  *		in mid-traverse will not cause confusion.
2058  *
2059  * 	There is one limitation: We cannot continue operation if an object
2060  *	is removed from the process from within a callback. We detect when
2061  *	this happens and return immediately with a -1 return value.
2062  *
2063  * note:
2064  *	As currently implemented, if a callback causes an object to be loaded,
2065  *	that object may or may not be reported by the current invocation of
2066  *	dl_iterate_phdr(), based on whether or not we have already processed
2067  *	the link-map list that receives it. If we want to prevent this, it
2068  *	can be done efficiently by associating the current value of cnt_map
2069  *	with each new Rt_map entered into the system. Then this function can
2070  *	use that to detect and skip new objects that enter the system in
2071  *	mid-iteration. However, the Linux documentation is ambiguous on whether
2072  *	this is necessary, and it does not appear to matter in practice.
2073  *	We have therefore chosen not to do so at this time.
2074  */
2075 int
2076 dl_iterate_phdr(int (*callback)(struct dl_phdr_info *, size_t, void *),
2077     void *data)
2078 {
2079 	struct dl_phdr_info	info;
2080 	u_longlong_t		l_cnt_map = cnt_map;
2081 	u_longlong_t		l_cnt_unmap = cnt_unmap;
2082 	Lm_list			*lml;
2083 	Lm_cntl			*lmc;
2084 	Rt_map			*lmp, *clmp;
2085 	Aliste			idx1, idx2;
2086 	Ehdr			*ehdr;
2087 	int			ret = 0;
2088 	int			entry;
2089 
2090 
2091 	entry = enter(0);
2092 	clmp =  _caller(caller(), CL_EXECDEF);
2093 	DBG_CALL(Dbg_cb_iphdr_enter(LIST(clmp), cnt_map, cnt_unmap));
2094 
2095 	/* Issue a callback for each ELF object in the process */
2096 	for (APLIST_TRAVERSE(dynlm_list, idx1, lml)) {
2097 		for (ALIST_TRAVERSE(lml->lm_lists, idx2, lmc)) {
2098 			for (lmp = lmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) {
2099 #if defined(_sparc) && !defined(_LP64)
2100 				/*
2101 				 * On 32-bit sparc, the possibility exists that
2102 				 * this object is not ELF.
2103 				 */
2104 				if (THIS_IS_NOT_ELF(lmp))
2105 					continue;
2106 #endif
2107 
2108 				/* Prepare the object information structure */
2109 				ehdr = (Ehdr *) ADDR(lmp);
2110 				info.dlpi_addr = (ehdr->e_type == ET_EXEC) ?
2111 				    0 : ADDR(lmp);
2112 				info.dlpi_name = lmp->rt_pathname;
2113 				info.dlpi_phdr = (Phdr *)
2114 				    (ADDR(lmp) + ehdr->e_phoff);
2115 				info.dlpi_phnum = ehdr->e_phnum;
2116 				info.dlpi_adds = cnt_map;
2117 				info.dlpi_subs = cnt_unmap;
2118 
2119 				/* Issue the callback */
2120 				DBG_CALL(Dbg_cb_iphdr_callback(LIST(clmp),
2121 				    &info));
2122 				leave(LIST(clmp), thr_flg_reenter);
2123 				ret = (* callback)(&info, sizeof (info), data);
2124 				(void) enter(thr_flg_reenter);
2125 
2126 				/* Return immediately on non-zero result */
2127 				if (ret != 0)
2128 					goto done;
2129 
2130 				/* Adapt to object mapping changes */
2131 				if ((cnt_map != l_cnt_map) ||
2132 				    (cnt_unmap != l_cnt_unmap)) {
2133 					DBG_CALL(Dbg_cb_iphdr_mapchange(
2134 					    LIST(clmp), cnt_map, cnt_unmap));
2135 
2136 					/* Stop if an object was unmapped */
2137 					if (cnt_unmap != l_cnt_unmap) {
2138 						ret = -1;
2139 						DBG_CALL(Dbg_cb_iphdr_unmap_ret(
2140 						    LIST(clmp)));
2141 						goto done;
2142 					}
2143 
2144 					l_cnt_map = cnt_map;
2145 				}
2146 			}
2147 		}
2148 	}
2149 
2150 done:
2151 	if (entry)
2152 		leave(LIST(clmp), 0);
2153 	return (ret);
2154 }
2155