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