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