xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/dlfcns.c (revision d6bb6a8465e557cb946ef49d56ed3202f6218652)
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 	 * Check for magic link-map list values:
609 	 *
610 	 *  LM_ID_BASE:		Operate on the PRIMARY (executables) link map
611 	 *  LM_ID_LDSO:		Operation on ld.so.1's link map
612 	 *  LM_ID_NEWLM: 	Create a new link-map.
613 	 */
614 	if (lml == (Lm_list *)LM_ID_NEWLM) {
615 		if ((lml = calloc(sizeof (Lm_list), 1)) == 0)
616 			return (0);
617 
618 		/*
619 		 * Establish the new link-map flags from the callers and those
620 		 * explicitly provided.
621 		 */
622 		lml->lm_tflags = LIST(clmp)->lm_tflags;
623 		if (flags & FLG_RT_AUDIT) {
624 			/*
625 			 * Unset any auditing flags - an auditor shouldn't be
626 			 * audited.  Insure all audit dependencies are loaded.
627 			 */
628 			lml->lm_tflags &= ~LML_TFLG_AUD_MASK;
629 			lml->lm_tflags |=
630 			    (LML_TFLG_NOLAZYLD | LML_TFLG_LOADFLTR);
631 			lml->lm_flags |= LML_FLG_NOAUDIT;
632 		}
633 
634 		if ((list_append(&dynlm_list, lml) == 0) ||
635 		    (newlmid(lml) == 0)) {
636 			free(lml);
637 			return (0);
638 		}
639 	} else if ((uintptr_t)lml < LM_ID_NUM) {
640 		if ((uintptr_t)lml == LM_ID_BASE)
641 			lml = &lml_main;
642 		else if ((uintptr_t)lml == LM_ID_LDSO)
643 			lml = &lml_rtld;
644 	}
645 
646 	/*
647 	 * If the path specified is null then we're operating on global
648 	 * objects.  Associate a dummy handle with the link-map list.
649 	 */
650 	if (path == 0) {
651 		Grp_hdl *ghp;
652 		uint_t	hflags = GPH_ZERO;
653 		int	promote = 0;
654 
655 		if (mode & RTLD_PARENT)
656 			hflags |=  GPH_PARENT;
657 		if (mode & RTLD_FIRST)
658 			hflags |=  GPH_FIRST;
659 
660 		if ((ghp = hdl_create(lml, 0, clmp, hflags)) == 0)
661 			return (0);
662 
663 		/*
664 		 * Traverse the main link-map control list, updating the mode
665 		 * of any objects as necessary.  Call the relocation engine if
666 		 * this mode promotes the existing state of any relocations.
667 		 * crle()'s first pass loads all objects necessary for building
668 		 * a configuration file, however none of them are relocated.
669 		 * crle()'s second pass relocates objects in preparation for
670 		 * dldump()'ing using dlopen(0, RTLD_NOW).
671 		 */
672 		if ((mode & (RTLD_NOW | RTLD_CONFGEN)) == RTLD_CONFGEN)
673 			return (ghp);
674 
675 		for (nlmp = lml->lm_head; nlmp; nlmp = (Rt_map *)NEXT(nlmp)) {
676 			if (((MODE(nlmp) & RTLD_GLOBAL) == 0) ||
677 			    (FLAGS(nlmp) & FLG_RT_DELETE))
678 				continue;
679 
680 			if (update_mode(nlmp, MODE(nlmp), mode))
681 				promote = 1;
682 		}
683 		if (promote)
684 			(void) relocate_lmc(lml, ALO_DATA, lml->lm_head);
685 
686 		return (ghp);
687 	}
688 
689 	/*
690 	 * Fix the pathname.  If this object expands to multiple paths (ie.
691 	 * $ISALIST or $HWCAP have been used), then make sure the user has also
692 	 * furnished the RTLD_FIRST flag.  As yet, we don't support opening
693 	 * more than one object at a time, so enforcing the RTLD_FIRST flag
694 	 * provides flexibility should we be able to support dlopening more
695 	 * than one object in the future.
696 	 */
697 	if ((pnp = LM_FIX_NAME(clmp)(path, clmp, orig)) == 0) {
698 		remove_lml(lml);
699 		return (0);
700 	}
701 	if (((pnp->p_orig & (PN_TKN_ISALIST | PN_TKN_HWCAP)) || pnp->p_next) &&
702 	    ((mode & RTLD_FIRST) == 0)) {
703 		remove_pnode(pnp);
704 		remove_lml(lml);
705 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_5));
706 		return (0);
707 	}
708 
709 	/*
710 	 * Create a new link-map control list for this request, and load the
711 	 * associated object.
712 	 */
713 	if ((lmc = alist_append(&(lml->lm_lists), 0, sizeof (Lm_cntl),
714 	    AL_CNT_LMLISTS)) == 0) {
715 		remove_pnode(pnp);
716 		remove_lml(lml);
717 		return (0);
718 	}
719 	olmco = nlmco = (Aliste)((char *)lmc - (char *)lml->lm_lists);
720 
721 	nlmp = load_one(lml, nlmco, pnp, clmp, mode,
722 	    (flags | FLG_RT_HANDLE), &ghp);
723 
724 	/*
725 	 * Remove any expanded pathname infrastructure, and if the dependency
726 	 * couldn't be loaded, cleanup.
727 	 */
728 	remove_pnode(pnp);
729 	if (nlmp == 0) {
730 		remove_cntl(lml, olmco);
731 		remove_lml(lml);
732 		return (0);
733 	}
734 
735 	/*
736 	 * If loading an auditor was requested, and the auditor already existed,
737 	 * then the link-map returned will be to the original auditor.  The new
738 	 * link-map list that was initially created, and the associated link-map
739 	 * control list are no longer needed.  As the auditor is already loaded,
740 	 * we're probably done, but fall through in case additional relocations
741 	 * would be triggered by the mode of the caller.
742 	 */
743 	if ((flags & FLG_RT_AUDIT) && (LIST(nlmp) != lml)) {
744 		remove_cntl(lml, olmco);
745 		remove_lml(lml);
746 		lml = LIST(nlmp);
747 		olmco = 0;
748 		nlmco = ALO_DATA;
749 	}
750 
751 	/*
752 	 * Finish processing the objects associated with this request.
753 	 */
754 	if ((analyze_lmc(lml, nlmco, nlmp) == 0) ||
755 	    (relocate_lmc(lml, nlmco, nlmp) == 0)) {
756 		(void) dlclose_core(ghp, clmp);
757 		if (olmco && lm_salvage(lml, 1, olmco)) {
758 			remove_cntl(lml, olmco);
759 			remove_lml(lml);
760 		}
761 		return (0);
762 	}
763 
764 	/*
765 	 * After a successful load, any objects collected on the new link-map
766 	 * control list will have been moved to the callers link-map control
767 	 * list.  This control list can now be deleted.
768 	 */
769 	if (olmco)
770 		remove_cntl(lml, olmco);
771 
772 	return (ghp);
773 }
774 
775 /*
776  * Internal dlopen() activity.  Called from user level or directly for internal
777  * opens that require a handle.
778  */
779 Grp_hdl *
780 dlmopen_intn(Lm_list * lml, const char *path, int mode, Rt_map * clmp,
781     uint_t flags, uint_t orig, int *loaded)
782 {
783 	Rt_map	*dlmp = 0;
784 	Grp_hdl	*ghp;
785 
786 	/*
787 	 * Determine the link-map that has just been loaded.
788 	 */
789 	if ((ghp = dlmopen_core(lml, path, mode, clmp, flags,
790 	    (orig | PN_SER_DLOPEN))) != 0) {
791 		/*
792 		 * Establish the new link-map from which .init processing will
793 		 * begin.  Ignore .init firing when constructing a configuration
794 		 * file (crle(1)).
795 		 */
796 		if ((mode & RTLD_CONFGEN) == 0)
797 			dlmp = ghp->gh_ownlmp;
798 	}
799 
800 	/*
801 	 * Return the number of objects loaded if required.  This is used to
802 	 * trigger used() processing on return from a dlopen().
803 	 */
804 	if (loaded && dlmp)
805 		*loaded = LIST(dlmp)->lm_init;
806 
807 	load_completion(dlmp, clmp);
808 	return (ghp);
809 }
810 
811 /*
812  * Argument checking for dlopen.  Only called via external entry.
813  */
814 static Grp_hdl *
815 dlmopen_check(Lm_list *lml, const char *path, int mode, Rt_map *clmp,
816     int *loaded)
817 {
818 	/*
819 	 * Verify that a valid pathname has been supplied.
820 	 */
821 	if (path && (*path == '\0')) {
822 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
823 		return (0);
824 	}
825 
826 	/*
827 	 * Historically we've always verified the mode is either RTLD_NOW or
828 	 * RTLD_LAZY.  RTLD_NOLOAD is valid by itself.  Use of LM_ID_NEWLM
829 	 * requires a specific pathname, and use of RTLD_PARENT is meaningless.
830 	 */
831 	if ((mode & (RTLD_NOW | RTLD_LAZY | RTLD_NOLOAD)) == 0) {
832 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_1));
833 		return (0);
834 	}
835 	if ((mode & (RTLD_NOW | RTLD_LAZY)) == (RTLD_NOW | RTLD_LAZY)) {
836 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_2));
837 		return (0);
838 	}
839 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (path == 0)) {
840 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_3));
841 		return (0);
842 	}
843 	if ((lml == (Lm_list *)LM_ID_NEWLM) && (mode & RTLD_PARENT)) {
844 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLMODE_4));
845 		return (0);
846 	}
847 	if (((mode & (RTLD_GROUP | RTLD_WORLD)) == 0) &&
848 	    ((mode & RTLD_NOLOAD) == 0))
849 		mode |= (RTLD_GROUP | RTLD_WORLD);
850 	if ((mode & RTLD_NOW) && (rtld_flags2 & RT_FL2_BINDLAZY)) {
851 		mode &= ~RTLD_NOW;
852 		mode |= RTLD_LAZY;
853 	}
854 
855 	return (dlmopen_intn(lml, path, mode, clmp, 0, 0, loaded));
856 }
857 
858 #pragma weak dlopen = _dlopen
859 
860 /*
861  * External entry for dlopen(3dl).  On success, returns a pointer (handle) to
862  * the structure containing information about the newly added object, ie. can
863  * be used by dlsym(). On failure, returns a null pointer.
864  */
865 void *
866 _dlopen(const char *path, int mode)
867 {
868 	int	entry, loaded = 0;
869 	Rt_map	*clmp;
870 	Grp_hdl	*ghp;
871 	Lm_list	*lml;
872 
873 	entry = enter();
874 
875 	clmp = _caller(caller(), CL_EXECDEF);
876 	lml = LIST(clmp);
877 
878 	ghp = dlmopen_check(lml, path, mode, clmp, &loaded);
879 
880 	if (entry && ghp && loaded)
881 		unused(lml);
882 
883 	if (entry)
884 		leave(lml);
885 	return ((void *)ghp);
886 }
887 
888 /*
889  * External entry for dlmopen(3dl).
890  */
891 #pragma weak dlmopen = _dlmopen
892 
893 void *
894 _dlmopen(Lmid_t lmid, const char *path, int mode)
895 {
896 	int	entry, loaded = 0;
897 	Rt_map	*clmp;
898 	Grp_hdl	*ghp;
899 
900 	entry = enter();
901 
902 	clmp = _caller(caller(), CL_EXECDEF);
903 
904 	ghp = dlmopen_check((Lm_list *)lmid, path, mode, clmp, &loaded);
905 
906 	if (entry && ghp && ghp->gh_ownlmp && loaded)
907 		unused(LIST(ghp->gh_ownlmp));
908 
909 	if (entry)
910 		leave(LIST(clmp));
911 	return ((void *)ghp);
912 }
913 
914 /*
915  * Handle processing for dlsym.
916  */
917 Sym *
918 dlsym_handle(Grp_hdl * ghp, Slookup * slp, Rt_map ** _lmp, uint_t *binfo)
919 {
920 	Rt_map		*nlmp, * lmp = ghp->gh_ownlmp;
921 	Rt_map		*clmp = slp->sl_cmap;
922 	const char	*name = slp->sl_name;
923 	Sym		*sym = 0;
924 	Slookup		sl = *slp;
925 
926 	sl.sl_flags = (LKUP_FIRST | LKUP_SPEC);
927 
928 	/*
929 	 * Continue processing a dlsym request.  Lookup the required symbol in
930 	 * each link-map specified by the handle.
931 	 *
932 	 * To leverage off of lazy loading, dlsym() requests can result in two
933 	 * passes.  The first descends the link-maps of any objects already in
934 	 * the address space.  If the symbol isn't located, and lazy
935 	 * dependencies still exist, then a second pass is made to load these
936 	 * dependencies if applicable.  This model means that in the case where
937 	 * a symbols exists in more than one object, the one located may not be
938 	 * constant - this is the standard issue with lazy loading. In addition,
939 	 * attempting to locate a symbol that doesn't exist will result in the
940 	 * loading of all lazy dependencies on the given handle, which can
941 	 * defeat some of the advantages of lazy loading (look out JVM).
942 	 */
943 	if (ghp->gh_flags & GPH_ZERO) {
944 		/*
945 		 * If this symbol lookup is triggered from a dlopen(0) handle,
946 		 * traverse the present link-map list looking for promiscuous
947 		 * entries.
948 		 */
949 		for (nlmp = lmp; nlmp; nlmp = (Rt_map *)NEXT(nlmp)) {
950 
951 			/*
952 			 * If this handle indicates we're only to look in the
953 			 * first object check whether we're done.
954 			 */
955 			if ((nlmp != lmp) && (ghp->gh_flags & GPH_FIRST))
956 				return ((Sym *)0);
957 
958 			if (!(MODE(nlmp) & RTLD_GLOBAL))
959 				continue;
960 			if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
961 			    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
962 				continue;
963 
964 			sl.sl_imap = nlmp;
965 			if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo))
966 				return (sym);
967 		}
968 
969 		/*
970 		 * If we're unable to locate the symbol and this link-map still
971 		 * has pending lazy dependencies, start loading them in an
972 		 * attempt to exhaust the search.  Note that as we're already
973 		 * traversing a dynamic linked list of link-maps there's no
974 		 * need for elf_lazy_find_sym() to descend the link-maps itself.
975 		 */
976 		if (LIST(lmp)->lm_lazy) {
977 			DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name));
978 
979 			sl.sl_flags |= LKUP_NODESCENT;
980 
981 			for (nlmp = lmp; nlmp; nlmp = (Rt_map *)NEXT(nlmp)) {
982 
983 				if (!(MODE(nlmp) & RTLD_GLOBAL) || !LAZY(nlmp))
984 					continue;
985 				if ((FLAGS(nlmp) & FLG_RT_DELETE) &&
986 				    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
987 					continue;
988 
989 				sl.sl_imap = nlmp;
990 				if (sym = elf_lazy_find_sym(&sl, _lmp, binfo))
991 					return (sym);
992 			}
993 		}
994 	} else {
995 		/*
996 		 * Traverse the dlopen() handle for the presently loaded
997 		 * link-maps.
998 		 */
999 		Grp_desc *	gdp;
1000 		Aliste		off;
1001 
1002 		for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) {
1003 			if ((gdp->gd_flags & GPD_AVAIL) == 0)
1004 				continue;
1005 
1006 			sl.sl_imap = gdp->gd_depend;
1007 			if (sym = LM_LOOKUP_SYM(clmp)(&sl, _lmp, binfo))
1008 				return (sym);
1009 
1010 			if (ghp->gh_flags & GPH_FIRST)
1011 				return ((Sym *)0);
1012 		}
1013 
1014 		/*
1015 		 * If we're unable to locate the symbol and this link-map still
1016 		 * has pending lazy dependencies, start loading them in an
1017 		 * attempt to exhaust the search.
1018 		 */
1019 		if (LIST(lmp)->lm_lazy) {
1020 			DBG_CALL(Dbg_syms_lazy_rescan(LIST(lmp), name));
1021 
1022 			for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) {
1023 				nlmp = gdp->gd_depend;
1024 
1025 				if (((gdp->gd_flags & GPD_AVAIL) == 0) ||
1026 				    (LAZY(nlmp) == 0))
1027 					continue;
1028 				sl.sl_imap = nlmp;
1029 				if (sym = elf_lazy_find_sym(&sl, _lmp, binfo))
1030 					return (sym);
1031 			}
1032 		}
1033 	}
1034 	return ((Sym *)0);
1035 }
1036 
1037 /*
1038  * Core dlsym activity.  Selects symbol lookup method from handle.
1039  */
1040 void *
1041 dlsym_core(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
1042 {
1043 	Sym		*sym;
1044 	Syminfo		*sip;
1045 	Slookup		sl;
1046 	uint_t		binfo;
1047 
1048 	sl.sl_name = name;
1049 	sl.sl_cmap = clmp;
1050 	sl.sl_hash = 0;
1051 	sl.sl_rsymndx = 0;
1052 
1053 	/*
1054 	 * Standard relocations are evaluated using the symbol index of the
1055 	 * associated relocation symbol.  This index provides for loading
1056 	 * any lazy dependency and establishing a direct binding if necessary.
1057 	 * If a dlsym() operation originates from an object that contains a
1058 	 * symbol table entry for the same name, then establish the symbol
1059 	 * index so that any dependency requirements can be triggered.
1060 	 */
1061 	if (((intptr_t)handle < 0) && (sip = SYMINFO(clmp)) != 0) {
1062 		sl.sl_imap = clmp;
1063 		sl.sl_flags = LKUP_SYMNDX;
1064 		sl.sl_hash = elf_hash(name);
1065 
1066 		if ((sym = SYMINTP(clmp)(&sl, 0, 0)) != NULL) {
1067 			sl.sl_rsymndx = (((ulong_t)sym -
1068 			    (ulong_t)SYMTAB(clmp)) / SYMENT(clmp));
1069 		}
1070 	}
1071 
1072 	if (handle == RTLD_NEXT) {
1073 		Rt_map	*nlmp;
1074 
1075 		/*
1076 		 * If this handle is RTLD_NEXT determine whether a lazy load
1077 		 * from the caller might provide the next object.  This mimics
1078 		 * the lazy loading initialization normally carried out by
1079 		 * lookup_sym(), however here, we must do this up-front, as
1080 		 * lookup_sym() will be used to inspect the next object.
1081 		 */
1082 		if (sl.sl_rsymndx) {
1083 			/* LINTED */
1084 			sip = (Syminfo *)((char *)sip +
1085 			    (sl.sl_rsymndx * SYMINENT(clmp)));
1086 
1087 			if ((sip->si_flags & SYMINFO_FLG_DIRECT) &&
1088 			    (sip->si_boundto < SYMINFO_BT_LOWRESERVE))
1089 				(void) elf_lazy_load(clmp,
1090 				    sip->si_boundto, name);
1091 
1092 			/*
1093 			 * Clear the symbol index, so as not to confuse
1094 			 * lookup_sym() of the next object.
1095 			 */
1096 			sl.sl_rsymndx = 0;
1097 		}
1098 
1099 		/*
1100 		 * If the handle is RTLD_NEXT start searching in the next link
1101 		 * map from the callers.  Determine permissions from the
1102 		 * present link map.  Indicate to lookup_sym() that we're on an
1103 		 * RTLD_NEXT request so that it will use the callers link map to
1104 		 * start any possible lazy dependency loading.
1105 		 */
1106 		sl.sl_imap = nlmp = (Rt_map *)NEXT(clmp);
1107 
1108 		DBG_CALL(Dbg_syms_dlsym(clmp, name, (nlmp ? NAME(nlmp) :
1109 		    MSG_INTL(MSG_STR_NULL)), DBG_DLSYM_NEXT));
1110 
1111 		if (nlmp == 0)
1112 			return (0);
1113 
1114 		sl.sl_flags = LKUP_NEXT;
1115 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo);
1116 
1117 	} else if (handle == RTLD_SELF) {
1118 		/*
1119 		 * If the handle is RTLD_SELF start searching from the caller.
1120 		 */
1121 		DBG_CALL(Dbg_syms_dlsym(clmp, name, NAME(clmp),
1122 		    DBG_DLSYM_SELF));
1123 
1124 		sl.sl_imap = clmp;
1125 		sl.sl_flags = LKUP_SPEC;
1126 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo);
1127 
1128 	} else if (handle == RTLD_DEFAULT) {
1129 		Rt_map	*hlmp = LIST(clmp)->lm_head;
1130 
1131 		/*
1132 		 * If the handle is RTLD_DEFAULT mimic the standard symbol
1133 		 * lookup as would be triggered by a relocation.
1134 		 */
1135 		DBG_CALL(Dbg_syms_dlsym(clmp, name, 0, DBG_DLSYM_DEFAULT));
1136 
1137 		sl.sl_imap = hlmp;
1138 		sl.sl_flags = LKUP_SPEC;
1139 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo);
1140 
1141 	} else if (handle == RTLD_PROBE) {
1142 		Rt_map	*hlmp = LIST(clmp)->lm_head;
1143 
1144 		/*
1145 		 * If the handle is RTLD_PROBE, mimic the standard symbol
1146 		 * lookup as would be triggered by a relocation, however do
1147 		 * not fall back to a lazy loading rescan if the symbol can't be
1148 		 * found within the currently loaded objects.  Note, a lazy
1149 		 * loaded dependency required by the caller might still get
1150 		 * loaded to satisfy this request, but no exhaustive lazy load
1151 		 * rescan is carried out.
1152 		 */
1153 		DBG_CALL(Dbg_syms_dlsym(clmp, name, 0, DBG_DLSYM_PROBE));
1154 
1155 		sl.sl_imap = hlmp;
1156 		sl.sl_flags = (LKUP_SPEC | LKUP_NOFALBACK);
1157 		sym = LM_LOOKUP_SYM(clmp)(&sl, dlmp, &binfo);
1158 
1159 	} else {
1160 		Grp_hdl *ghp = (Grp_hdl *)handle;
1161 
1162 		/*
1163 		 * Look in the shared object specified by the handle and in all
1164 		 * of its dependencies.
1165 		 */
1166 		DBG_CALL(Dbg_syms_dlsym(clmp, name, NAME(ghp->gh_ownlmp),
1167 		    DBG_DLSYM_DEF));
1168 		sym = LM_DLSYM(clmp)(ghp, &sl, dlmp, &binfo);
1169 	}
1170 
1171 	if (sym) {
1172 		Lm_list	*lml = LIST(clmp);
1173 		Addr	addr = sym->st_value;
1174 
1175 		if (!(FLAGS(*dlmp) & FLG_RT_FIXED))
1176 			addr += ADDR(*dlmp);
1177 
1178 		DBG_CALL(Dbg_bind_global(clmp, 0, 0, (Xword)-1, PLT_T_NONE,
1179 		    *dlmp, addr, sym->st_value, name, binfo));
1180 
1181 		if ((lml->lm_tflags | FLAGS1(clmp)) & LML_TFLG_AUD_SYMBIND) {
1182 			uint_t	sb_flags = LA_SYMB_DLSYM;
1183 			/* LINTED */
1184 			uint_t	symndx = (uint_t)(((Xword)sym -
1185 			    (Xword)SYMTAB(*dlmp)) / SYMENT(*dlmp));
1186 			addr = audit_symbind(clmp, *dlmp, sym, symndx, addr,
1187 			    &sb_flags);
1188 		}
1189 		return ((void *)addr);
1190 	} else
1191 		return (0);
1192 }
1193 
1194 /*
1195  * Internal dlsym activity.  Called from user level or directly for internal
1196  * symbol lookup.
1197  */
1198 void *
1199 dlsym_intn(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
1200 {
1201 	Rt_map		*llmp = 0;
1202 	void		*error;
1203 	Aliste		off;
1204 	Grp_desc	*gdp;
1205 
1206 	/*
1207 	 * While looking for symbols it's quite possible that additional objects
1208 	 * get loaded from lazy loading.  These objects will have been added to
1209 	 * the same link-map list as those objects on the handle.  Remember this
1210 	 * list for later investigation.
1211 	 */
1212 	if ((handle == RTLD_NEXT) || (handle == RTLD_DEFAULT) ||
1213 	    (handle == RTLD_SELF) || (handle == RTLD_PROBE))
1214 		llmp = LIST(clmp)->lm_tail;
1215 	else {
1216 		Grp_hdl *	ghp = (Grp_hdl *)handle;
1217 
1218 		if (ghp->gh_ownlmp)
1219 			llmp = LIST(ghp->gh_ownlmp)->lm_tail;
1220 		else {
1221 			for (ALIST_TRAVERSE(ghp->gh_depends, off, gdp)) {
1222 				if ((llmp = LIST(gdp->gd_depend)->lm_tail) != 0)
1223 					break;
1224 			}
1225 		}
1226 	}
1227 
1228 	if ((error = dlsym_core(handle, name, clmp, dlmp)) == 0) {
1229 		/*
1230 		 * Cache the error message, as Java tends to fall through this
1231 		 * code many times.
1232 		 */
1233 		if (nosym_str == 0)
1234 			nosym_str = MSG_INTL(MSG_GEN_NOSYM);
1235 		eprintf(LIST(clmp), ERR_FATAL, nosym_str, name);
1236 	}
1237 
1238 	load_completion(llmp, clmp);
1239 
1240 	return (error);
1241 }
1242 
1243 /*
1244  * Argument checking for dlsym.  Only called via external entry.
1245  */
1246 static void *
1247 dlsym_check(void *handle, const char *name, Rt_map *clmp, Rt_map **dlmp)
1248 {
1249 	/*
1250 	 * Verify the arguments.
1251 	 */
1252 	if (name == 0) {
1253 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_ILLSYM));
1254 		return (0);
1255 	}
1256 	if ((handle != RTLD_NEXT) && (handle != RTLD_DEFAULT) &&
1257 	    (handle != RTLD_SELF) && (handle != RTLD_PROBE) &&
1258 	    (hdl_validate((Grp_hdl *)handle) == 0)) {
1259 		eprintf(LIST(clmp), ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL));
1260 		return (0);
1261 	}
1262 	return (dlsym_intn(handle, name, clmp, dlmp));
1263 }
1264 
1265 
1266 #pragma weak dlsym = _dlsym
1267 
1268 /*
1269  * External entry for dlsym().  On success, returns the address of the specified
1270  * symbol.  On error returns a null.
1271  */
1272 void *
1273 _dlsym(void *handle, const char *name)
1274 {
1275 	int	entry;
1276 	Rt_map	*clmp, *dlmp = 0;
1277 	void	*addr;
1278 
1279 	entry = enter();
1280 
1281 	clmp = _caller(caller(), CL_EXECDEF);
1282 
1283 	addr = dlsym_check(handle, name, clmp, &dlmp);
1284 
1285 	if (dlmp)
1286 		is_dep_ready(dlmp, clmp, DBG_WAIT_SYMBOL);
1287 
1288 	if (entry && dlmp)
1289 		is_dep_init(dlmp, clmp);
1290 
1291 	if (entry)
1292 		leave(LIST(clmp));
1293 	return (addr);
1294 }
1295 
1296 /*
1297  * Core dladdr activity.
1298  */
1299 static void
1300 dladdr_core(Rt_map *clmp, void *addr, Dl_info *dlip, void **info, int flags)
1301 {
1302 	/*
1303 	 * Set up generic information and any defaults.
1304 	 */
1305 	dlip->dli_fname = PATHNAME(clmp);
1306 
1307 	dlip->dli_fbase = (void *)ADDR(clmp);
1308 	dlip->dli_sname = 0;
1309 	dlip->dli_saddr = 0;
1310 
1311 	/*
1312 	 * Determine the nearest symbol to this address.
1313 	 */
1314 	LM_DLADDR(clmp)((ulong_t)addr, clmp, dlip, info, flags);
1315 }
1316 
1317 #pragma weak dladdr = _dladdr
1318 
1319 /*
1320  * External entry for dladdr(3dl) and dladdr1(3dl).  Returns an information
1321  * structure that reflects the symbol closest to the address specified.
1322  */
1323 int
1324 _dladdr(void *addr, Dl_info *dlip)
1325 {
1326 	int	entry, error;
1327 	Rt_map	*clmp;
1328 
1329 	entry = enter();
1330 
1331 	/*
1332 	 * Use our calling technique to determine what object is associated
1333 	 * with the supplied address.  If a caller can't be determined,
1334 	 * indicate the failure.
1335 	 */
1336 	if ((clmp = _caller((caddr_t)addr, CL_NONE)) == 0) {
1337 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
1338 		    EC_NATPTR(addr));
1339 		error = 0;
1340 	} else {
1341 		dladdr_core(clmp, addr, dlip, 0, 0);
1342 		error = 1;
1343 	}
1344 
1345 	if (entry)
1346 		leave(0);
1347 	return (error);
1348 }
1349 
1350 #pragma weak dladdr1 = _dladdr1
1351 
1352 int
1353 _dladdr1(void *addr, Dl_info *dlip, void **info, int flags)
1354 {
1355 	int	entry, error = 0;
1356 	Rt_map	*clmp;
1357 
1358 	/*
1359 	 * Validate any flags.
1360 	 */
1361 	if (flags) {
1362 		int	request;
1363 
1364 		if (((request = (flags & RTLD_DL_MASK)) != RTLD_DL_SYMENT) &&
1365 		    (request != RTLD_DL_LINKMAP)) {
1366 			eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLFLAGS),
1367 			    flags);
1368 			return (0);
1369 		}
1370 		if (info == 0) {
1371 			eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_ILLINFO), flags);
1372 			return (0);
1373 		}
1374 	}
1375 
1376 	entry = enter();
1377 
1378 	/*
1379 	 * Use our calling technique to determine what object is associated
1380 	 * with the supplied address.  If a caller can't be determined,
1381 	 * indicate the failure.
1382 	 */
1383 	if ((clmp = _caller((caddr_t)addr, CL_NONE)) == 0) {
1384 		eprintf(0, ERR_FATAL, MSG_INTL(MSG_ARG_INVADDR),
1385 		    EC_NATPTR(addr));
1386 		error = 0;
1387 	} else {
1388 		dladdr_core(clmp, addr, dlip, info, flags);
1389 		error = 1;
1390 	}
1391 
1392 	if (entry)
1393 		leave(0);
1394 	return (error);
1395 }
1396 
1397 /*
1398  * Core dldump activity.
1399  */
1400 static int
1401 dldump_core(Lm_list *lml, const char *ipath, const char *opath, int flags)
1402 {
1403 	Addr	addr = 0;
1404 	Rt_map	*lmp;
1405 
1406 	/*
1407 	 * Verify any arguments first.
1408 	 */
1409 	if ((!opath || (*opath == '\0')) || (ipath && (*ipath == '\0'))) {
1410 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLPATH));
1411 		return (1);
1412 	}
1413 
1414 	/*
1415 	 * If an input file is specified make sure its one of our dependencies
1416 	 * on the main link-map list.  Note, this has really all evolved for
1417 	 * crle(), which uses libcrle.so on an alternative link-map to trigger
1418 	 * dumping objects from the main link-map list.   If we ever want to
1419 	 * dump objects from alternative link-maps, this model is going to
1420 	 * have to be revisited.
1421 	 */
1422 	if (ipath) {
1423 		if ((lmp = is_so_loaded(&lml_main, ipath, 0)) == 0)
1424 			lmp = is_so_loaded(&lml_main, ipath, 1);
1425 
1426 		if (lmp == 0) {
1427 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NOFILE),
1428 			    ipath);
1429 			return (1);
1430 		}
1431 		if (FLAGS(lmp) & FLG_RT_ALTER) {
1432 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_ALTER), ipath);
1433 			return (1);
1434 		}
1435 		if (FLAGS(lmp) & FLG_RT_NODUMP) {
1436 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_GEN_NODUMP),
1437 			    ipath);
1438 			return (1);
1439 		}
1440 	} else
1441 		lmp = lml_main.lm_head;
1442 
1443 
1444 	DBG_CALL(Dbg_file_dldump(lmp, opath, flags));
1445 
1446 	/*
1447 	 * If the object being dump'ed isn't fixed identify its mapping.
1448 	 */
1449 	if (!(FLAGS(lmp) & FLG_RT_FIXED))
1450 		addr = ADDR(lmp);
1451 
1452 	/*
1453 	 * As rt_dldump() will effectively lazy load the necessary support
1454 	 * libraries, make sure ld.so.1 is initialized for plt relocations.
1455 	 */
1456 	if (elf_rtld_load() == 0)
1457 		return (0);
1458 
1459 	/*
1460 	 * Dump the required image.
1461 	 */
1462 	return (rt_dldump(lmp, opath, flags, addr));
1463 }
1464 
1465 #pragma weak dldump = _dldump
1466 
1467 /*
1468  * External entry for dldump(3c).  Returns 0 on success, non-zero otherwise.
1469  */
1470 int
1471 _dldump(const char *ipath, const char *opath, int flags)
1472 {
1473 	int	error, entry;
1474 	Rt_map	*clmp;
1475 
1476 	entry = enter();
1477 
1478 	clmp = _caller(caller(), CL_EXECDEF);
1479 
1480 	error = dldump_core(LIST(clmp), ipath, opath, flags);
1481 
1482 	if (entry)
1483 		leave(LIST(clmp));
1484 	return (error);
1485 }
1486 
1487 /*
1488  * get_linkmap_id() translates Lm_list * pointers to the Link_map id as used by
1489  * the rtld_db and dlmopen() interfaces.  It checks to see if the Link_map is
1490  * one of the primary ones and if so returns it's special token:
1491  *		LM_ID_BASE
1492  *		LM_ID_LDSO
1493  *
1494  * If it's not one of the primary link_map id's it will instead returns a
1495  * pointer to the Lm_list structure which uniquely identifies the Link_map.
1496  */
1497 Lmid_t
1498 get_linkmap_id(Lm_list *lml)
1499 {
1500 	if (lml->lm_flags & LML_FLG_BASELM)
1501 		return (LM_ID_BASE);
1502 	if (lml->lm_flags & LML_FLG_RTLDLM)
1503 		return (LM_ID_LDSO);
1504 
1505 	return ((Lmid_t)lml);
1506 }
1507 
1508 /*
1509  * Extract information for a dlopen() handle.
1510  */
1511 static int
1512 dlinfo_core(void *handle, int request, void *p, Rt_map *clmp)
1513 {
1514 	Lm_list	*lml = LIST(clmp);
1515 	Rt_map	*lmp;
1516 
1517 	if ((request > RTLD_DI_MAX) || (p == 0)) {
1518 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_ILLVAL));
1519 		return (-1);
1520 	}
1521 
1522 	/*
1523 	 * Return configuration cache name and address.
1524 	 */
1525 	if (request == RTLD_DI_CONFIGADDR) {
1526 		Dl_info	*dlip = (Dl_info *)p;
1527 
1528 		if ((config->c_name == 0) || (config->c_bgn == 0) ||
1529 		    (config->c_end == 0)) {
1530 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOCONFIG));
1531 			return (-1);
1532 		}
1533 		dlip->dli_fname = config->c_name;
1534 		dlip->dli_fbase = (void *)config->c_bgn;
1535 		return (0);
1536 	}
1537 
1538 	/*
1539 	 * Return profiled object name (used by ldprof audit library).
1540 	 */
1541 	if (request == RTLD_DI_PROFILENAME) {
1542 		if (profile_name == 0) {
1543 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_NOPROFNAME));
1544 			return (-1);
1545 		}
1546 
1547 		*(const char **)p = profile_name;
1548 		return (0);
1549 	}
1550 	if (request == RTLD_DI_PROFILEOUT) {
1551 		/*
1552 		 * If a profile destination directory hasn't been specified
1553 		 * provide a default.
1554 		 */
1555 		if (profile_out == 0)
1556 			profile_out = MSG_ORIG(MSG_PTH_VARTMP);
1557 
1558 		*(const char **)p = profile_out;
1559 		return (0);
1560 	}
1561 
1562 	/*
1563 	 * Obtain or establish a termination signal.
1564 	 */
1565 	if (request == RTLD_DI_GETSIGNAL) {
1566 		*(int *)p = killsig;
1567 		return (0);
1568 	}
1569 
1570 	if (request == RTLD_DI_SETSIGNAL) {
1571 		sigset_t	set;
1572 		int		sig = *(int *)p;
1573 
1574 		/*
1575 		 * Determine whether the signal is in range.
1576 		 */
1577 		(void) sigfillset(&set);
1578 		if (sigismember(&set, sig) != 1) {
1579 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVSIG), sig);
1580 			return (-1);
1581 		}
1582 
1583 		killsig = sig;
1584 		return (0);
1585 	}
1586 
1587 	/*
1588 	 * For any other request a link-map is required.  Verify the handle.
1589 	 */
1590 	if (handle == RTLD_SELF)
1591 		lmp = clmp;
1592 	else {
1593 		Grp_hdl *	ghp = (Grp_hdl *)handle;
1594 
1595 		if (!hdl_validate(ghp)) {
1596 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_ARG_INVHNDL));
1597 			return (-1);
1598 		}
1599 		lmp = ghp->gh_ownlmp;
1600 	}
1601 
1602 	/*
1603 	 * Obtain the process arguments, environment and auxv.  Note, as the
1604 	 * environment can be modified by the user (putenv(3c)), reinitialize
1605 	 * the environment pointer on each request.
1606 	 */
1607 	if (request == RTLD_DI_ARGSINFO) {
1608 		Dl_argsinfo	*aip = (Dl_argsinfo *)p;
1609 		Lm_list		*lml = LIST(lmp);
1610 
1611 		*aip = argsinfo;
1612 		if (lml->lm_flags & LML_FLG_ENVIRON)
1613 			aip->dla_envp = *(lml->lm_environ);
1614 
1615 		return (0);
1616 	}
1617 
1618 	/*
1619 	 * Return Lmid_t of the Link-Map list that the specified object is
1620 	 * loaded on.
1621 	 */
1622 	if (request == RTLD_DI_LMID) {
1623 		*(Lmid_t *)p = get_linkmap_id(LIST(lmp));
1624 		return (0);
1625 	}
1626 
1627 	/*
1628 	 * Return a pointer to the Link-Map structure associated with the
1629 	 * specified object.
1630 	 */
1631 	if (request == RTLD_DI_LINKMAP) {
1632 		*(Link_map **)p = (Link_map *)lmp;
1633 		return (0);
1634 	}
1635 
1636 	/*
1637 	 * Return search path information, or the size of the buffer required
1638 	 * to store the information.
1639 	 */
1640 	if ((request == RTLD_DI_SERINFO) || (request == RTLD_DI_SERINFOSIZE)) {
1641 		Pnode		*dir, *dirlist = (Pnode *)0;
1642 		Dl_serinfo	*info;
1643 		Dl_serpath	*path;
1644 		char		*strs;
1645 		size_t		size = sizeof (Dl_serinfo);
1646 		uint_t		cnt = 0;
1647 
1648 		info = (Dl_serinfo *)p;
1649 		path = &info->dls_serpath[0];
1650 		strs = (char *)&info->dls_serpath[info->dls_cnt];
1651 
1652 		/*
1653 		 * Traverse search path entries for this object.
1654 		 */
1655 		while ((dir = get_next_dir(&dirlist, lmp, 0)) != 0) {
1656 			size_t	_size;
1657 
1658 			if (dir->p_name == 0)
1659 				continue;
1660 
1661 			/*
1662 			 * If configuration information exists, it's possible
1663 			 * this path has been identified as non-existent, if so
1664 			 * ignore it.
1665 			 */
1666 			if (dir->p_info) {
1667 				Rtc_obj	*dobj = (Rtc_obj *)dir->p_info;
1668 				if (dobj->co_flags & RTC_OBJ_NOEXIST)
1669 					continue;
1670 			}
1671 
1672 			/*
1673 			 * Keep track of search path count and total info size.
1674 			 */
1675 			if (cnt++)
1676 				size += sizeof (Dl_serpath);
1677 			_size = strlen(dir->p_name) + 1;
1678 			size += _size;
1679 
1680 			if (request == RTLD_DI_SERINFOSIZE)
1681 				continue;
1682 
1683 			/*
1684 			 * If we're filling in search path information, confirm
1685 			 * there's sufficient space.
1686 			 */
1687 			if (size > info->dls_size) {
1688 				eprintf(lml, ERR_FATAL,
1689 				    MSG_INTL(MSG_ARG_SERSIZE),
1690 				    EC_OFF(info->dls_size));
1691 				return (-1);
1692 			}
1693 			if (cnt > info->dls_cnt) {
1694 				eprintf(lml, ERR_FATAL,
1695 				    MSG_INTL(MSG_ARG_SERCNT), info->dls_cnt);
1696 				return (-1);
1697 			}
1698 
1699 			/*
1700 			 * Append the path to the information buffer.
1701 			 */
1702 			(void) strcpy(strs, dir->p_name);
1703 			path->dls_name = strs;
1704 			path->dls_flags = dir->p_orig;
1705 
1706 			strs = strs + _size;
1707 			path++;
1708 		}
1709 
1710 		/*
1711 		 * If we're here to size the search buffer fill it in.
1712 		 */
1713 		if (request == RTLD_DI_SERINFOSIZE) {
1714 			info->dls_size = size;
1715 			info->dls_cnt = cnt;
1716 		}
1717 	}
1718 
1719 	/*
1720 	 * Return the origin of the object associated with this link-map.
1721 	 * Basically return the dirname(1) of the objects fullpath.
1722 	 */
1723 	if (request == RTLD_DI_ORIGIN) {
1724 		char	*str = (char *)p;
1725 
1726 		if (DIRSZ(lmp) == 0)
1727 			(void) fullpath(lmp, 0);
1728 
1729 		(void) strncpy(str, ORIGNAME(lmp), DIRSZ(lmp));
1730 		str += DIRSZ(lmp);
1731 		*str = '\0';
1732 
1733 		return (0);
1734 	}
1735 
1736 	return (0);
1737 }
1738 
1739 #pragma weak dlinfo = _dlinfo
1740 
1741 /*
1742  * External entry for dlinfo(3dl).
1743  */
1744 int
1745 _dlinfo(void *handle, int request, void *p)
1746 {
1747 	int	error, entry;
1748 	Rt_map	*clmp;
1749 
1750 	entry = enter();
1751 
1752 	clmp = _caller(caller(), CL_EXECDEF);
1753 
1754 	error = dlinfo_core(handle, request, p, clmp);
1755 
1756 	if (entry)
1757 		leave(LIST(clmp));
1758 	return (error);
1759 }
1760