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