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