xref: /onnv-gate/usr/src/cmd/sgs/rtld/common/util.c (revision 6387)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51618Srie  * Common Development and Distribution License (the "License").
61618Srie  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211109Srie 
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
240Sstevel@tonic-gate  *	  All Rights Reserved
250Sstevel@tonic-gate  *
265892Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
270Sstevel@tonic-gate  * Use is subject to license terms.
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * Utility routines for run-time linker.  some are duplicated here from libc
330Sstevel@tonic-gate  * (with different names) to avoid name space collisions.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate #include	"_synonyms.h"
360Sstevel@tonic-gate #include	<stdio.h>
370Sstevel@tonic-gate #include	<sys/types.h>
380Sstevel@tonic-gate #include	<sys/mman.h>
390Sstevel@tonic-gate #include	<sys/lwp.h>
400Sstevel@tonic-gate #include	<sys/debug.h>
410Sstevel@tonic-gate #include	<stdarg.h>
420Sstevel@tonic-gate #include	<fcntl.h>
430Sstevel@tonic-gate #include	<string.h>
440Sstevel@tonic-gate #include	<ctype.h>
450Sstevel@tonic-gate #include	<dlfcn.h>
460Sstevel@tonic-gate #include	<unistd.h>
470Sstevel@tonic-gate #include	<stdlib.h>
480Sstevel@tonic-gate #include	<sys/auxv.h>
491618Srie #include	<debug.h>
501618Srie #include	<conv.h>
510Sstevel@tonic-gate #include	"_rtld.h"
520Sstevel@tonic-gate #include	"_audit.h"
531824Srie #include	"_elf.h"
540Sstevel@tonic-gate #include	"msg.h"
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static int ld_flags_env(const char *, Word *, Word *, uint_t, int);
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * All error messages go through eprintf().  During process initialization these
600Sstevel@tonic-gate  * messages should be directed to the standard error, however once control has
610Sstevel@tonic-gate  * been passed to the applications code these messages should be stored in an
620Sstevel@tonic-gate  * internal buffer for use with dlerror().  Note, fatal error conditions that
630Sstevel@tonic-gate  * may occur while running the application will still cause a standard error
640Sstevel@tonic-gate  * message, see rtldexit() in this file for details.
650Sstevel@tonic-gate  * The `application' flag serves to indicate the transition between process
660Sstevel@tonic-gate  * initialization and when the applications code is running.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Null function used as place where a debugger can set a breakpoint.
710Sstevel@tonic-gate  */
720Sstevel@tonic-gate void
731618Srie rtld_db_dlactivity(Lm_list *lml)
740Sstevel@tonic-gate {
751618Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
761618Srie 	    r_debug.rtd_rdebug.r_state));
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Null function used as place where debugger can set a pre .init
810Sstevel@tonic-gate  * processing breakpoint.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate void
841618Srie rtld_db_preinit(Lm_list *lml)
850Sstevel@tonic-gate {
861618Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
871618Srie 	    r_debug.rtd_rdebug.r_state));
880Sstevel@tonic-gate }
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Null function used as place where debugger can set a post .init
920Sstevel@tonic-gate  * processing breakpoint.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate void
951618Srie rtld_db_postinit(Lm_list *lml)
960Sstevel@tonic-gate {
971618Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
981618Srie 	    r_debug.rtd_rdebug.r_state));
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate  * Debugger Event Notification
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  * This function centralizes all debugger event notification (ala rtld_db).
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * There's a simple intent, focused on insuring the primary link-map control
1070Sstevel@tonic-gate  * list (or each link-map list) is consistent, and the indication that objects
1080Sstevel@tonic-gate  * have been added or deleted from this list.  Although an RD_ADD and RD_DELETE
1090Sstevel@tonic-gate  * event are posted for each of these, most debuggers don't care, as their
1100Sstevel@tonic-gate  * view is that these events simply convey an "inconsistent" state.
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * We also don't want to trigger multiple RD_ADD/RD_DELETE events any time we
1130Sstevel@tonic-gate  * enter ld.so.1.
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  * With auditors, we may be in the process of relocating a collection of
1160Sstevel@tonic-gate  * objects, and will leave() ld.so.1 to call the auditor.  At this point we
1170Sstevel@tonic-gate  * must indicate an RD_CONSISTENT event, but librtld_db will not report an
1180Sstevel@tonic-gate  * object to the debuggers until relocation processing has been completed on it.
1190Sstevel@tonic-gate  * To allow for the collection of these objects that are pending relocation, an
1200Sstevel@tonic-gate  * RD_ADD event is set after completing a series of relocations on the primary
1210Sstevel@tonic-gate  * link-map control list.
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  * Set an RD_ADD/RD_DELETE event and indicate that an RD_CONSISTENT event is
1240Sstevel@tonic-gate  * required later (LML_FLG_DBNOTIF):
1250Sstevel@tonic-gate  *
1260Sstevel@tonic-gate  *  i	the first time we add or delete an object to the primary link-map
1270Sstevel@tonic-gate  *	control list.
1280Sstevel@tonic-gate  *  ii	the first time we move a secondary link-map control list to the primary
1290Sstevel@tonic-gate  *	link-map control list (effectively, this is like adding a group of
1300Sstevel@tonic-gate  *	objects to the primary link-map control list).
1310Sstevel@tonic-gate  *
1320Sstevel@tonic-gate  * Set an RD_CONSISTENT event when it is required (LML_FLG_DBNOTIF is set) and
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  *  i	each time we leave the runtime linker.
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate void
1370Sstevel@tonic-gate rd_event(Lm_list *lml, rd_event_e event, r_state_e state)
1380Sstevel@tonic-gate {
1391824Srie 	void	(*fptr)(Lm_list *);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	switch (event) {
1420Sstevel@tonic-gate 	case RD_PREINIT:
1430Sstevel@tonic-gate 		fptr = rtld_db_preinit;
1440Sstevel@tonic-gate 		break;
1450Sstevel@tonic-gate 	case RD_POSTINIT:
1460Sstevel@tonic-gate 		fptr = rtld_db_postinit;
1470Sstevel@tonic-gate 		break;
1480Sstevel@tonic-gate 	case RD_DLACTIVITY:
1490Sstevel@tonic-gate 		switch (state) {
1500Sstevel@tonic-gate 		case RT_CONSISTENT:
1510Sstevel@tonic-gate 			lml->lm_flags &= ~LML_FLG_DBNOTIF;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 			/*
1540Sstevel@tonic-gate 			 * Do we need to send a notification?
1550Sstevel@tonic-gate 			 */
1560Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_DBNOTIF) == 0)
1570Sstevel@tonic-gate 				return;
1580Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_DBNOTIF;
1590Sstevel@tonic-gate 			break;
1600Sstevel@tonic-gate 		case RT_ADD:
1610Sstevel@tonic-gate 		case RT_DELETE:
1620Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_DBNOTIF;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 			/*
1650Sstevel@tonic-gate 			 * If we are already in an inconsistent state, no
1660Sstevel@tonic-gate 			 * notification is required.
1670Sstevel@tonic-gate 			 */
1680Sstevel@tonic-gate 			if (rtld_flags & RT_FL_DBNOTIF)
1690Sstevel@tonic-gate 				return;
1700Sstevel@tonic-gate 			rtld_flags |= RT_FL_DBNOTIF;
1710Sstevel@tonic-gate 			break;
1720Sstevel@tonic-gate 		};
1730Sstevel@tonic-gate 		fptr = rtld_db_dlactivity;
1740Sstevel@tonic-gate 		break;
1750Sstevel@tonic-gate 	default:
1760Sstevel@tonic-gate 		/*
1770Sstevel@tonic-gate 		 * RD_NONE - do nothing
1780Sstevel@tonic-gate 		 */
1790Sstevel@tonic-gate 		break;
1800Sstevel@tonic-gate 	};
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	/*
1830Sstevel@tonic-gate 	 * Set event state and call 'notification' function.
1840Sstevel@tonic-gate 	 *
1850Sstevel@tonic-gate 	 * The debugging clients have previously been told about these
1860Sstevel@tonic-gate 	 * notification functions and have set breakpoints on them if they
1870Sstevel@tonic-gate 	 * are interested in the notification.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_state = state;
1900Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = event;
1911618Srie 	fptr(lml);
1920Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = RD_NONE;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1953731Srie #if	defined(__sparc) || defined(__x86)
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * Stack Cleanup.
1980Sstevel@tonic-gate  *
1990Sstevel@tonic-gate  * This function is invoked to 'remove' arguments that were passed in on the
2000Sstevel@tonic-gate  * stack.  This is most likely if ld.so.1 was invoked directly.  In that case
2010Sstevel@tonic-gate  * we want to remove ld.so.1 as well as it's arguments from the argv[] array.
2020Sstevel@tonic-gate  * Which means we then need to slide everything above it on the stack down
2030Sstevel@tonic-gate  * accordingly.
2040Sstevel@tonic-gate  *
2053731Srie  * While the stack layout is platform specific - it just so happens that __x86,
2063731Srie  * and __sparc platforms share the following initial stack layout.
2070Sstevel@tonic-gate  *
2080Sstevel@tonic-gate  *	!_______________________!  high addresses
2090Sstevel@tonic-gate  *	!			!
2100Sstevel@tonic-gate  *	!	Information	!
2110Sstevel@tonic-gate  *	!	Block		!
2120Sstevel@tonic-gate  *	!	(size varies)	!
2130Sstevel@tonic-gate  *	!_______________________!
2140Sstevel@tonic-gate  *	!	0 word		!
2150Sstevel@tonic-gate  *	!_______________________!
2160Sstevel@tonic-gate  *	!	Auxiliary	!
2170Sstevel@tonic-gate  *	!	vector		!
2180Sstevel@tonic-gate  *	!	2 word entries	!
2190Sstevel@tonic-gate  *	!			!
2200Sstevel@tonic-gate  *	!_______________________!
2210Sstevel@tonic-gate  *	!	0 word		!
2220Sstevel@tonic-gate  *	!_______________________!
2230Sstevel@tonic-gate  *	!	Environment	!
2240Sstevel@tonic-gate  *	!	pointers	!
2250Sstevel@tonic-gate  *	!	...		!
2260Sstevel@tonic-gate  *	!	(one word each)	!
2270Sstevel@tonic-gate  *	!_______________________!
2280Sstevel@tonic-gate  *	!	0 word		!
2290Sstevel@tonic-gate  *	!_______________________!
2300Sstevel@tonic-gate  *	!	Argument	! low addresses
2310Sstevel@tonic-gate  *	!	pointers	!
2320Sstevel@tonic-gate  *	!	Argc words	!
2330Sstevel@tonic-gate  *	!_______________________!
2340Sstevel@tonic-gate  *	!			!
2350Sstevel@tonic-gate  *	!	Argc		!
2360Sstevel@tonic-gate  *	!_______________________!
2370Sstevel@tonic-gate  *	!	...		!
2380Sstevel@tonic-gate  *
2390Sstevel@tonic-gate  */
2400Sstevel@tonic-gate static void
2416Srie stack_cleanup(char **argv, char ***envp, auxv_t **auxv, int rmcnt)
2420Sstevel@tonic-gate {
2436Srie 	int		ndx;
2440Sstevel@tonic-gate 	long		*argc;
2456Srie 	char		**oargv, **nargv;
2466Srie 	char		**oenvp, **nenvp;
2476Srie 	auxv_t		*oauxv, *nauxv;
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/*
2506Srie 	 * Slide ARGV[] and update argc.  The argv pointer remains the same,
2516Srie 	 * however slide the applications arguments over the arguments to
2526Srie 	 * ld.so.1.
2530Sstevel@tonic-gate 	 */
2546Srie 	nargv = &argv[0];
2556Srie 	oargv = &argv[rmcnt];
2566Srie 
2576Srie 	for (ndx = 0; oargv[ndx]; ndx++)
2586Srie 		nargv[ndx] = oargv[ndx];
2596Srie 	nargv[ndx] = oargv[ndx];
2606Srie 
2616Srie 	argc = (long *)((uintptr_t)argv - sizeof (long *));
2620Sstevel@tonic-gate 	*argc -= rmcnt;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2656Srie 	 * Slide ENVP[], and update the environment array pointer.
2660Sstevel@tonic-gate 	 */
2676Srie 	ndx++;
2686Srie 	nenvp = &nargv[ndx];
2696Srie 	oenvp = &oargv[ndx];
2706Srie 	*envp = nenvp;
2716Srie 
2726Srie 	for (ndx = 0; oenvp[ndx]; ndx++)
2736Srie 		nenvp[ndx] = oenvp[ndx];
2746Srie 	nenvp[ndx] = oenvp[ndx];
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2776Srie 	 * Slide AUXV[], and update the aux vector pointer.
2780Sstevel@tonic-gate 	 */
2796Srie 	ndx++;
2806Srie 	nauxv = (auxv_t *)&nenvp[ndx];
2816Srie 	oauxv = (auxv_t *)&oenvp[ndx];
2826Srie 	*auxv = nauxv;
2836Srie 
2846Srie 	for (ndx = 0; (oauxv[ndx].a_type != AT_NULL); ndx++)
2856Srie 		nauxv[ndx] = oauxv[ndx];
2866Srie 	nauxv[ndx] = oauxv[ndx];
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate #else
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate  * Verify that the above routine is appropriate for any new platforms.
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate #error	unsupported architecture!
2930Sstevel@tonic-gate #endif
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /*
2966Srie  * The only command line argument recognized is -e, followed by a runtime
2976Srie  * linker environment variable.
2980Sstevel@tonic-gate  */
2990Sstevel@tonic-gate int
3006Srie rtld_getopt(char **argv, char ***envp, auxv_t **auxv, Word *lmflags,
3016Srie     Word *lmtflags, int aout)
3020Sstevel@tonic-gate {
3036Srie 	int	ndx;
3046Srie 
3056Srie 	for (ndx = 1; argv[ndx]; ndx++) {
3060Sstevel@tonic-gate 		char	*str;
3070Sstevel@tonic-gate 
3086Srie 		if (argv[ndx][0] != '-')
3090Sstevel@tonic-gate 			break;
3100Sstevel@tonic-gate 
3116Srie 		if (argv[ndx][1] == '\0') {
3126Srie 			ndx++;
3130Sstevel@tonic-gate 			break;
3140Sstevel@tonic-gate 		}
3150Sstevel@tonic-gate 
3166Srie 		if (argv[ndx][1] != 'e')
3176Srie 			return (1);
3186Srie 
3196Srie 		if (argv[ndx][2] == '\0') {
3206Srie 			ndx++;
3216Srie 			if (argv[ndx] == NULL)
3226Srie 				return (1);
3236Srie 			str = argv[ndx];
3240Sstevel@tonic-gate 		} else
3256Srie 			str = &argv[ndx][2];
3266Srie 
3276Srie 		/*
3286Srie 		 * If the environment variable starts with LD_, strip the LD_.
3296Srie 		 * Otherwise, take things as is.
3306Srie 		 */
3316Srie 		if ((str[0] == 'L') && (str[1] == 'D') && (str[2] == '_') &&
3326Srie 		    (str[3] != '\0'))
3336Srie 			str += 3;
3340Sstevel@tonic-gate 		if (ld_flags_env(str, lmflags, lmtflags, 0, aout) == 1)
3350Sstevel@tonic-gate 			return (1);
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 
3386Srie 	/*
3396Srie 	 * Make sure an object file has been specified.
3406Srie 	 */
3416Srie 	if (argv[ndx] == 0)
3420Sstevel@tonic-gate 		return (1);
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 	/*
3450Sstevel@tonic-gate 	 * Having gotten the arguments, clean ourselves off of the stack.
3460Sstevel@tonic-gate 	 */
3476Srie 	stack_cleanup(argv, envp, auxv, ndx);
3480Sstevel@tonic-gate 	return (0);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*
352*6387Srie  * Compare function for PathNode AVL tree.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate static int
355*6387Srie pnavl_compare(const void *n1, const void *n2)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	uint_t		hash1, hash2;
3580Sstevel@tonic-gate 	const char	*st1, *st2;
3590Sstevel@tonic-gate 	int		rc;
3600Sstevel@tonic-gate 
361*6387Srie 	hash1 = ((PathNode *)n1)->pn_hash;
362*6387Srie 	hash2 = ((PathNode *)n2)->pn_hash;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	if (hash1 > hash2)
3650Sstevel@tonic-gate 		return (1);
3660Sstevel@tonic-gate 	if (hash1 < hash2)
3670Sstevel@tonic-gate 		return (-1);
3680Sstevel@tonic-gate 
369*6387Srie 	st1 = ((PathNode *)n1)->pn_name;
370*6387Srie 	st2 = ((PathNode *)n2)->pn_name;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	rc = strcmp(st1, st2);
3730Sstevel@tonic-gate 	if (rc > 0)
3740Sstevel@tonic-gate 		return (1);
3750Sstevel@tonic-gate 	if (rc < 0)
3760Sstevel@tonic-gate 		return (-1);
3770Sstevel@tonic-gate 	return (0);
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate /*
381*6387Srie  * Determine if a pathname has already been recorded on the full path name
382*6387Srie  * AVL tree.  This tree maintains a node for each path name that ld.so.1 has
383*6387Srie  * successfully loaded.  If the path name does not exist in this AVL tree, then
384*6387Srie  * the next insertion point is deposited in "where".  This value can be used by
385*6387Srie  * fpavl_insert() to expedite the insertion.
3860Sstevel@tonic-gate  */
3870Sstevel@tonic-gate Rt_map *
388*6387Srie fpavl_recorded(Lm_list *lml, const char *name, avl_index_t *where)
3890Sstevel@tonic-gate {
390*6387Srie 	FullPathNode	fpn, *fpnp;
3910Sstevel@tonic-gate 	avl_tree_t	*avlt;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	/*
3940Sstevel@tonic-gate 	 * Create the avl tree if required.
3950Sstevel@tonic-gate 	 */
3960Sstevel@tonic-gate 	if ((avlt = lml->lm_fpavl) == NULL) {
3970Sstevel@tonic-gate 		if ((avlt = calloc(sizeof (avl_tree_t), 1)) == 0)
3980Sstevel@tonic-gate 			return (0);
399*6387Srie 		avl_create(avlt, pnavl_compare, sizeof (FullPathNode),
400*6387Srie 		    SGSOFFSETOF(FullPathNode, fpn_node.pn_avl));
4010Sstevel@tonic-gate 		lml->lm_fpavl = avlt;
4020Sstevel@tonic-gate 	}
4030Sstevel@tonic-gate 
404*6387Srie 	fpn.fpn_node.pn_name = name;
405*6387Srie 	fpn.fpn_node.pn_hash = sgs_str_hash(name);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if ((fpnp = avl_find(lml->lm_fpavl, &fpn, where)) == NULL)
4080Sstevel@tonic-gate 		return (NULL);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	return (fpnp->fpn_lmp);
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate /*
414*6387Srie  * Insert a name into the FullPathNode AVL tree for the link-map list.  The
415422Srie  * objects NAME() is the path that would have originally been searched for, and
416422Srie  * is therefore the name to associate with any "where" value.  If the object has
4170Sstevel@tonic-gate  * a different PATHNAME(), perhaps because it has resolved to a different file
418*6387Srie  * (see fullpath()), then this name will be recorded as a separate FullPathNode
419*6387Srie  * (see load_file()).
4200Sstevel@tonic-gate  */
4210Sstevel@tonic-gate int
4220Sstevel@tonic-gate fpavl_insert(Lm_list *lml, Rt_map *lmp, const char *name, avl_index_t where)
4230Sstevel@tonic-gate {
424*6387Srie 	FullPathNode	*fpnp;
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 	if (where == 0) {
4270Sstevel@tonic-gate 		/* LINTED */
428*6387Srie 		Rt_map	*_lmp = fpavl_recorded(lml, name, &where);
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		/*
4310Sstevel@tonic-gate 		 * We better not get a hit now, we do not want duplicates in
4320Sstevel@tonic-gate 		 * the tree.
4330Sstevel@tonic-gate 		 */
4340Sstevel@tonic-gate 		ASSERT(_lmp == 0);
4350Sstevel@tonic-gate 	}
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	/*
438*6387Srie 	 * Insert new node in tree.
4390Sstevel@tonic-gate 	 */
440*6387Srie 	if ((fpnp = calloc(sizeof (FullPathNode), 1)) == 0)
4410Sstevel@tonic-gate 		return (0);
4420Sstevel@tonic-gate 
443*6387Srie 	fpnp->fpn_node.pn_name = name;
444*6387Srie 	fpnp->fpn_node.pn_hash = sgs_str_hash(name);
4450Sstevel@tonic-gate 	fpnp->fpn_lmp = lmp;
4460Sstevel@tonic-gate 
4475892Sab196087 	if (aplist_append(&FPNODE(lmp), fpnp, AL_CNT_FPNODE) == NULL) {
4480Sstevel@tonic-gate 		free(fpnp);
4490Sstevel@tonic-gate 		return (0);
4500Sstevel@tonic-gate 	}
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	ASSERT(lml->lm_fpavl != NULL);
4530Sstevel@tonic-gate 	avl_insert(lml->lm_fpavl, fpnp, where);
4540Sstevel@tonic-gate 	return (1);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate /*
458*6387Srie  * Remove an object from the FullPath AVL tree.  Note, this is called *before*
4590Sstevel@tonic-gate  * the objects link-map is torn down (remove_so), which is where any NAME() and
4600Sstevel@tonic-gate  * PATHNAME() strings will be deallocated.
4610Sstevel@tonic-gate  */
4620Sstevel@tonic-gate void
4630Sstevel@tonic-gate fpavl_remove(Rt_map *lmp)
4640Sstevel@tonic-gate {
465*6387Srie 	FullPathNode	*fpnp;
4665892Sab196087 	Aliste		idx;
4675892Sab196087 
4685892Sab196087 	for (APLIST_TRAVERSE(FPNODE(lmp), idx, fpnp)) {
4690Sstevel@tonic-gate 		avl_remove(LIST(lmp)->lm_fpavl, fpnp);
4700Sstevel@tonic-gate 		free(fpnp);
4710Sstevel@tonic-gate 	}
4720Sstevel@tonic-gate 	free(FPNODE(lmp));
4735892Sab196087 	FPNODE(lmp) = NULL;
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate 
476*6387Srie /*
477*6387Srie  * Determine if a pathname has already been recorded on the not-found AVL tree.
478*6387Srie  * This tree maintains a node for each path name that ld.so.1 has explicitly
479*6387Srie  * inspected, but has failed to load during a single ld.so.1 operation.  If the
480*6387Srie  * path name does not exist in this AVL tree, then the next insertion point is
481*6387Srie  * deposited in "where".  This value can be used by nfavl_insert() to expedite
482*6387Srie  * the insertion.
483*6387Srie  */
484*6387Srie int
485*6387Srie nfavl_recorded(const char *name, avl_index_t *where)
486*6387Srie {
487*6387Srie 	PathNode	pn;
488*6387Srie 	avl_tree_t	*avlt;
489*6387Srie 
490*6387Srie 	/*
491*6387Srie 	 * Create the avl tree if required.
492*6387Srie 	 */
493*6387Srie 	if ((avlt = nfavl) == NULL) {
494*6387Srie 		if ((avlt = calloc(sizeof (avl_tree_t), 1)) == 0)
495*6387Srie 			return (0);
496*6387Srie 		avl_create(avlt, pnavl_compare, sizeof (PathNode),
497*6387Srie 		    SGSOFFSETOF(PathNode, pn_avl));
498*6387Srie 		nfavl = avlt;
499*6387Srie 	}
500*6387Srie 
501*6387Srie 	pn.pn_name = name;
502*6387Srie 	pn.pn_hash = sgs_str_hash(name);
503*6387Srie 
504*6387Srie 	if (avl_find(avlt, &pn, where) == NULL)
505*6387Srie 		return (0);
506*6387Srie 
507*6387Srie 	return (1);
508*6387Srie }
509*6387Srie 
510*6387Srie /*
511*6387Srie  * Insert a name into the not-found AVL tree.
512*6387Srie  */
513*6387Srie void
514*6387Srie nfavl_insert(const char *name, avl_index_t where)
515*6387Srie {
516*6387Srie 	PathNode	*pnp;
517*6387Srie 
518*6387Srie 	if (where == 0) {
519*6387Srie 		/* LINTED */
520*6387Srie 		int	in_nfavl = nfavl_recorded(name, &where);
521*6387Srie 
522*6387Srie 		/*
523*6387Srie 		 * We better not get a hit now, we do not want duplicates in
524*6387Srie 		 * the tree.
525*6387Srie 		 */
526*6387Srie 		ASSERT(in_nfavl == 0);
527*6387Srie 	}
528*6387Srie 
529*6387Srie 	/*
530*6387Srie 	 * Insert new node in tree.
531*6387Srie 	 */
532*6387Srie 	if ((pnp = calloc(sizeof (PathNode), 1)) != 0) {
533*6387Srie 		pnp->pn_name = name;
534*6387Srie 		pnp->pn_hash = sgs_str_hash(name);
535*6387Srie 		avl_insert(nfavl, pnp, where);
536*6387Srie 	}
537*6387Srie }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * Prior to calling an object, either via a .plt or through dlsym(), make sure
5410Sstevel@tonic-gate  * its .init has fired.  Through topological sorting, ld.so.1 attempts to fire
5420Sstevel@tonic-gate  * init's in the correct order, however, this order is typically based on needed
5430Sstevel@tonic-gate  * dependencies and non-lazy relocation bindings.  Lazy relocations (.plts) can
5440Sstevel@tonic-gate  * still occur and result in bindings that were not captured during topological
5450Sstevel@tonic-gate  * sorting.  This routine compensates for this lack of binding information, and
5460Sstevel@tonic-gate  * provides for dynamic .init firing.
5470Sstevel@tonic-gate  */
5480Sstevel@tonic-gate void
549*6387Srie is_dep_init(Rt_map *dlmp, Rt_map *clmp)
5500Sstevel@tonic-gate {
551*6387Srie 	Rt_map	**tobj;
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	/*
5540Sstevel@tonic-gate 	 * If the caller is an auditor, and the destination isn't, then don't
5550Sstevel@tonic-gate 	 * run any .inits (see comments in load_completion()).
5560Sstevel@tonic-gate 	 */
5570Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
5580Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
5590Sstevel@tonic-gate 		return;
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if ((dlmp == clmp) || (rtld_flags & (RT_FL_BREADTH | RT_FL_INITFIRST)))
5620Sstevel@tonic-gate 		return;
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITDONE)) ==
5650Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITDONE))
5660Sstevel@tonic-gate 		return;
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITCALL)) ==
5690Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITCALL)) {
5701618Srie 		DBG_CALL(Dbg_util_no_init(dlmp));
5710Sstevel@tonic-gate 		return;
5720Sstevel@tonic-gate 	}
5730Sstevel@tonic-gate 
5740Sstevel@tonic-gate 	if ((tobj = calloc(2, sizeof (Rt_map *))) != NULL) {
5750Sstevel@tonic-gate 		tobj[0] = dlmp;
5760Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_DYN);
5770Sstevel@tonic-gate 	}
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * In a threaded environment insure the thread responsible for loading an object
5820Sstevel@tonic-gate  * has completed .init processing for that object before any new thread is
5830Sstevel@tonic-gate  * allowed to access the object.  This check is only valid with libthread
5840Sstevel@tonic-gate  * TI_VERSION 2, where ld.so.1 implements locking through low level mutexes.
5850Sstevel@tonic-gate  *
5860Sstevel@tonic-gate  * When a new link-map is created, the thread that causes it to be loaded is
5870Sstevel@tonic-gate  * identified by THREADID(dlmp).  Compare this with the current thread to
5880Sstevel@tonic-gate  * determine if it must be blocked.
5890Sstevel@tonic-gate  *
5900Sstevel@tonic-gate  * NOTE, there are a number of instances (typically only for .plt processing)
5910Sstevel@tonic-gate  * where we must skip this test:
5920Sstevel@tonic-gate  *
5930Sstevel@tonic-gate  *   .	any thread id of 0 - threads that call thr_exit() may be in this state
5940Sstevel@tonic-gate  *	thus we can't deduce what tid they used to be.  Also some of the
5950Sstevel@tonic-gate  *	lib/libthread worker threads have this id and must bind (to themselves
5960Sstevel@tonic-gate  *	or libc) for libthread to function.
5970Sstevel@tonic-gate  *
5980Sstevel@tonic-gate  *   .	libthread itself binds to libc, and as libthread is INITFIRST
5990Sstevel@tonic-gate  *	libc's .init can't have fired yet.  Luckly libc's .init is not required
6000Sstevel@tonic-gate  *	by libthreads binding.
6010Sstevel@tonic-gate  *
6020Sstevel@tonic-gate  *   .	if the caller is an auditor, and the destination isn't, then don't
6030Sstevel@tonic-gate  *	block (see comments in load_completion()).
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate void
606*6387Srie is_dep_ready(Rt_map *dlmp, Rt_map *clmp, int what)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	thread_t	tid;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
6110Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
6120Sstevel@tonic-gate 		return;
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_CONCUR) &&
6150Sstevel@tonic-gate 	    ((FLAGS(dlmp) & FLG_RT_INITDONE) == 0) &&
6160Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_INITFRST) == 0) &&
6170Sstevel@tonic-gate 	    ((tid = rt_thr_self()) != 0) && (THREADID(dlmp) != tid)) {
6180Sstevel@tonic-gate 		while ((FLAGS(dlmp) & FLG_RT_INITDONE) == 0) {
6190Sstevel@tonic-gate 			FLAGS1(dlmp) |= FL1_RT_INITWAIT;
6201618Srie 			DBG_CALL(Dbg_util_wait(clmp, dlmp, what));
6210Sstevel@tonic-gate 			(void) rt_cond_wait(CONDVAR(dlmp), &rtldlock);
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate }
6250Sstevel@tonic-gate 
6260Sstevel@tonic-gate /*
6270Sstevel@tonic-gate  * Execute .{preinit|init|fini}array sections
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate void
6301618Srie call_array(Addr *array, uint_t arraysz, Rt_map *lmp, Word shtype)
6310Sstevel@tonic-gate {
6321618Srie 	int	start, stop, incr, ndx;
6330Sstevel@tonic-gate 	uint_t	arraycnt = (uint_t)(arraysz / sizeof (Addr));
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (array == NULL)
6360Sstevel@tonic-gate 		return;
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/*
6390Sstevel@tonic-gate 	 * initarray & preinitarray are walked from beginning to end - while
6400Sstevel@tonic-gate 	 * finiarray is walked from end to beginning.
6410Sstevel@tonic-gate 	 */
6420Sstevel@tonic-gate 	if (shtype == SHT_FINI_ARRAY) {
6430Sstevel@tonic-gate 		start = arraycnt - 1;
6440Sstevel@tonic-gate 		stop = incr = -1;
6450Sstevel@tonic-gate 	} else {
6460Sstevel@tonic-gate 		start = 0;
6470Sstevel@tonic-gate 		stop = arraycnt;
6480Sstevel@tonic-gate 		incr = 1;
6490Sstevel@tonic-gate 	}
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * Call the .*array[] entries
6530Sstevel@tonic-gate 	 */
6541618Srie 	for (ndx = start; ndx != stop; ndx += incr) {
6551824Srie 		void (*fptr)(void) = (void(*)())array[ndx];
6561618Srie 
6571618Srie 		DBG_CALL(Dbg_util_call_array(lmp, (void *)fptr, ndx, shtype));
6581618Srie 
6590Sstevel@tonic-gate 		leave(LIST(lmp));
6600Sstevel@tonic-gate 		(*fptr)();
6610Sstevel@tonic-gate 		(void) enter();
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate /*
6670Sstevel@tonic-gate  * Execute any .init sections.  These are passed to us in an lmp array which
6680Sstevel@tonic-gate  * (by default) will have been sorted.
6690Sstevel@tonic-gate  */
6700Sstevel@tonic-gate void
671*6387Srie call_init(Rt_map **tobj, int flag)
6720Sstevel@tonic-gate {
673*6387Srie 	Rt_map		**_tobj, **_nobj;
6740Sstevel@tonic-gate 	static List	pending = { NULL, NULL };
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/*
6770Sstevel@tonic-gate 	 * If we're in the middle of an INITFIRST, this must complete before
6780Sstevel@tonic-gate 	 * any new init's are fired.  In this case add the object list to the
6790Sstevel@tonic-gate 	 * pending queue and return.  We'll pick up the queue after any
6800Sstevel@tonic-gate 	 * INITFIRST objects have their init's fired.
6810Sstevel@tonic-gate 	 */
6820Sstevel@tonic-gate 	if (rtld_flags & RT_FL_INITFIRST) {
6830Sstevel@tonic-gate 		(void) list_append(&pending, tobj);
6840Sstevel@tonic-gate 		return;
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * Traverse the tobj array firing each objects init.
6890Sstevel@tonic-gate 	 */
6900Sstevel@tonic-gate 	for (_tobj = _nobj = tobj, _nobj++; *_tobj != NULL; _tobj++, _nobj++) {
691*6387Srie 		Rt_map	*lmp = *_tobj;
692*6387Srie 		void	(*iptr)() = INIT(lmp);
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITCALL)
6950Sstevel@tonic-gate 			continue;
6960Sstevel@tonic-gate 
6970Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITCALL;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 		/*
7000Sstevel@tonic-gate 		 * Establish an initfirst state if necessary - no other inits
701280Srie 		 * will be fired (because of additional relocation bindings)
702280Srie 		 * when in this state.
7030Sstevel@tonic-gate 		 */
7040Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITFRST)
7050Sstevel@tonic-gate 			rtld_flags |= RT_FL_INITFIRST;
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr) {
7085892Sab196087 			Aliste		idx;
7095892Sab196087 			Bnd_desc 	*bdp;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 			/*
7120Sstevel@tonic-gate 			 * Make sure that all dependencies that have been
7130Sstevel@tonic-gate 			 * relocated to are initialized before this objects
7140Sstevel@tonic-gate 			 * .init is executed.  This insures that a dependency
7150Sstevel@tonic-gate 			 * on an external item that must first be initialized
7160Sstevel@tonic-gate 			 * by its associated object is satisfied.
7170Sstevel@tonic-gate 			 */
7185892Sab196087 			for (APLIST_TRAVERSE(DEPENDS(lmp), idx, bdp)) {
7190Sstevel@tonic-gate 				if ((bdp->b_flags & BND_REFER) == 0)
7200Sstevel@tonic-gate 					continue;
7210Sstevel@tonic-gate 				is_dep_ready(bdp->b_depend, lmp, DBG_WAIT_INIT);
7220Sstevel@tonic-gate 			}
7231618Srie 			DBG_CALL(Dbg_util_call_init(lmp, flag));
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 		if (iptr) {
7270Sstevel@tonic-gate 			leave(LIST(lmp));
7280Sstevel@tonic-gate 			(*iptr)();
7290Sstevel@tonic-gate 			(void) enter();
7300Sstevel@tonic-gate 		}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 		call_array(INITARRAY(lmp), INITARRAYSZ(lmp), lmp,
7330Sstevel@tonic-gate 		    SHT_INIT_ARRAY);
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr)
7361618Srie 			DBG_CALL(Dbg_util_call_init(lmp, DBG_INIT_DONE));
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 		/*
7390Sstevel@tonic-gate 		 * Set the initdone flag regardless of whether this object
7400Sstevel@tonic-gate 		 * actually contains an .init section.  This flag prevents us
7410Sstevel@tonic-gate 		 * from processing this section again for an .init and also
7420Sstevel@tonic-gate 		 * signifies that a .fini must be called should it exist.
7430Sstevel@tonic-gate 		 * Clear the sort field for use in later .fini processing.
7440Sstevel@tonic-gate 		 */
7450Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITDONE;
746280Srie 		SORTVAL(lmp) = -1;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		/*
7490Sstevel@tonic-gate 		 * Wake anyone up who might be waiting on this .init.
7500Sstevel@tonic-gate 		 */
7510Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_INITWAIT) {
7521618Srie 			DBG_CALL(Dbg_util_broadcast(lmp));
7530Sstevel@tonic-gate 			(void) rt_cond_broadcast(CONDVAR(lmp));
7540Sstevel@tonic-gate 			FLAGS1(lmp) &= ~FL1_RT_INITWAIT;
7550Sstevel@tonic-gate 		}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 		/*
7580Sstevel@tonic-gate 		 * If we're firing an INITFIRST object, and other objects must
7590Sstevel@tonic-gate 		 * be fired which are not INITFIRST, make sure we grab any
7600Sstevel@tonic-gate 		 * pending objects that might have been delayed as this
7610Sstevel@tonic-gate 		 * INITFIRST was processed.
7620Sstevel@tonic-gate 		 */
7630Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_INITFIRST) &&
7640Sstevel@tonic-gate 		    ((*_nobj == NULL) || !(FLAGS(*_nobj) & FLG_RT_INITFRST))) {
765*6387Srie 			Listnode	*lnp;
766*6387Srie 			Rt_map		**pobj;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_INITFIRST;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 			while ((lnp = pending.head) != NULL) {
7710Sstevel@tonic-gate 				if ((pending.head = lnp->next) == NULL)
7720Sstevel@tonic-gate 					pending.tail = NULL;
7730Sstevel@tonic-gate 				pobj = lnp->data;
7740Sstevel@tonic-gate 				free(lnp);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 				call_init(pobj, DBG_INIT_PEND);
7770Sstevel@tonic-gate 			}
7780Sstevel@tonic-gate 		}
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 	free(tobj);
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate /*
7840Sstevel@tonic-gate  * Function called by atexit(3C).  Calls all .fini sections related with the
7850Sstevel@tonic-gate  * mains dependent shared libraries in the order in which the shared libraries
7860Sstevel@tonic-gate  * have been loaded.  Skip any .fini defined in the main executable, as this
7870Sstevel@tonic-gate  * will be called by crt0 (main was never marked as initdone).
7880Sstevel@tonic-gate  */
7890Sstevel@tonic-gate void
7900Sstevel@tonic-gate call_fini(Lm_list * lml, Rt_map ** tobj)
7910Sstevel@tonic-gate {
792280Srie 	Rt_map **_tobj;
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	for (_tobj = tobj; *_tobj != NULL; _tobj++) {
7955892Sab196087 		Rt_map		*clmp, * lmp = *_tobj;
7965892Sab196087 		Aliste		idx;
7975892Sab196087 		Bnd_desc	*bdp;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 		/*
8000Sstevel@tonic-gate 		 * If concurrency checking isn't enabled only fire .fini if
8010Sstevel@tonic-gate 		 * .init has completed.  We collect all .fini sections of
8020Sstevel@tonic-gate 		 * objects that had their .init collected, but that doesn't
8030Sstevel@tonic-gate 		 * mean at the time that the .init had completed.
8040Sstevel@tonic-gate 		 */
8050Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_CONCUR) ||
8060Sstevel@tonic-gate 		    (FLAGS(lmp) & FLG_RT_INITDONE)) {
8071824Srie 			void	(*fptr)(void) = FINI(lmp);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 			if (FINIARRAY(lmp) || fptr) {
8100Sstevel@tonic-gate 				/*
8110Sstevel@tonic-gate 				 * If concurrency checking is enabled make sure
8120Sstevel@tonic-gate 				 * this object's .init is completed before
8130Sstevel@tonic-gate 				 * calling any .fini.
8140Sstevel@tonic-gate 				 */
8150Sstevel@tonic-gate 				is_dep_ready(lmp, lmp, DBG_WAIT_FINI);
8161618Srie 				DBG_CALL(Dbg_util_call_fini(lmp));
8170Sstevel@tonic-gate 			}
8180Sstevel@tonic-gate 
8191618Srie 			call_array(FINIARRAY(lmp), FINIARRAYSZ(lmp), lmp,
8201618Srie 			    SHT_FINI_ARRAY);
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 			if (fptr) {
8230Sstevel@tonic-gate 				leave(LIST(lmp));
8240Sstevel@tonic-gate 				(*fptr)();
8250Sstevel@tonic-gate 				(void) enter();
8260Sstevel@tonic-gate 			}
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 		/*
830280Srie 		 * Skip main, this is explicitly called last in atexit_fini().
831280Srie 		 */
832280Srie 		if (FLAGS(lmp) & FLG_RT_ISMAIN)
833280Srie 			continue;
834280Srie 
835280Srie 		/*
8360Sstevel@tonic-gate 		 * Audit `close' operations at this point.  The library has
8370Sstevel@tonic-gate 		 * exercised its last instructions (regardless of whether it
8380Sstevel@tonic-gate 		 * will be unmapped or not).
8390Sstevel@tonic-gate 		 *
8400Sstevel@tonic-gate 		 * First call any global auditing.
8410Sstevel@tonic-gate 		 */
8420Sstevel@tonic-gate 		if (lml->lm_tflags & LML_TFLG_AUD_OBJCLOSE)
8430Sstevel@tonic-gate 			_audit_objclose(&(auditors->ad_list), lmp);
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 		/*
8460Sstevel@tonic-gate 		 * Finally determine whether this object has local auditing
8470Sstevel@tonic-gate 		 * requirements by inspecting itself and then its dependencies.
8480Sstevel@tonic-gate 		 */
8490Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_LOCAUDIT) == 0)
8500Sstevel@tonic-gate 			continue;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 		if (FLAGS1(lmp) & LML_TFLG_AUD_OBJCLOSE)
8530Sstevel@tonic-gate 			_audit_objclose(&(AUDITORS(lmp)->ad_list), lmp);
8540Sstevel@tonic-gate 
8555892Sab196087 		for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
8560Sstevel@tonic-gate 			clmp = bdp->b_caller;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 			if (FLAGS1(clmp) & LML_TFLG_AUD_OBJCLOSE) {
8594362Srie 				_audit_objclose(&(AUDITORS(clmp)->ad_list),
8604362Srie 				    lmp);
8614362Srie 				break;
8620Sstevel@tonic-gate 			}
8630Sstevel@tonic-gate 		}
8640Sstevel@tonic-gate 	}
8651618Srie 	DBG_CALL(Dbg_bind_plt_summary(lml, M_MACH, pltcnt21d, pltcnt24d,
8661618Srie 	    pltcntu32, pltcntu44, pltcntfull, pltcntfar));
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 	free(tobj);
8690Sstevel@tonic-gate }
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate void
8720Sstevel@tonic-gate atexit_fini()
8730Sstevel@tonic-gate {
874*6387Srie 	Rt_map		**tobj, *lmp;
875*6387Srie 	Lm_list		*lml;
876*6387Srie 	Listnode	*lnp;
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate 	(void) enter();
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	rtld_flags |= RT_FL_ATEXIT;
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	lml = &lml_main;
883280Srie 	lml->lm_flags |= LML_FLG_ATEXIT;
8843817Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
8850Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	/*
8880Sstevel@tonic-gate 	 * Reverse topologically sort the main link-map for .fini execution.
8890Sstevel@tonic-gate 	 */
8900Sstevel@tonic-gate 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
8910Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
8920Sstevel@tonic-gate 		call_fini(lml, tobj);
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate 	/*
895280Srie 	 * Add an explicit close to main and ld.so.1.  Although main's .fini is
896280Srie 	 * collected in call_fini() to provide for FINITARRAY processing, its
897280Srie 	 * audit_objclose is explicitly skipped.  This provides for it to be
898280Srie 	 * called last, here.  This is the reverse of the explicit calls to
899280Srie 	 * audit_objopen() made in setup().
9000Sstevel@tonic-gate 	 */
9010Sstevel@tonic-gate 	if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_MASK) {
9020Sstevel@tonic-gate 		audit_objclose(lmp, (Rt_map *)lml_rtld.lm_head);
903280Srie 		audit_objclose(lmp, lmp);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	/*
9070Sstevel@tonic-gate 	 * Now that all .fini code has been run, see what unreferenced objects
908*6387Srie 	 * remain.
9090Sstevel@tonic-gate 	 */
9100Sstevel@tonic-gate 	unused(lml);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/*
9130Sstevel@tonic-gate 	 * Traverse any alternative link-map lists.
9140Sstevel@tonic-gate 	 */
9150Sstevel@tonic-gate 	for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
9161824Srie 		/*
9171824Srie 		 * Ignore the base-link-map list, which has already been
9181824Srie 		 * processed, and the runtime linkers link-map list, which is
9191824Srie 		 * typically processed last.
9201824Srie 		 */
9210Sstevel@tonic-gate 		if (lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM))
9220Sstevel@tonic-gate 			continue;
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 		if ((lmp = (Rt_map *)lml->lm_head) == 0)
9250Sstevel@tonic-gate 			continue;
9260Sstevel@tonic-gate 
927280Srie 		lml->lm_flags |= LML_FLG_ATEXIT;
9283817Srie 		lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
929280Srie 
9300Sstevel@tonic-gate 		/*
9310Sstevel@tonic-gate 		 * Reverse topologically sort the link-map for .fini execution.
9320Sstevel@tonic-gate 		 */
9330Sstevel@tonic-gate 		if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
9340Sstevel@tonic-gate 		    (tobj != (Rt_map **)S_ERROR))
9350Sstevel@tonic-gate 			call_fini(lml, tobj);
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 		unused(lml);
9380Sstevel@tonic-gate 	}
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate 	/*
9410Sstevel@tonic-gate 	 * Finally reverse topologically sort the runtime linkers link-map for
9420Sstevel@tonic-gate 	 * .fini execution.
9430Sstevel@tonic-gate 	 */
9440Sstevel@tonic-gate 	lml = &lml_rtld;
945280Srie 	lml->lm_flags |= LML_FLG_ATEXIT;
9463817Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
9470Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
9500Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
9510Sstevel@tonic-gate 		call_fini(lml, tobj);
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	leave(&lml_main);
9540Sstevel@tonic-gate }
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate /*
9580Sstevel@tonic-gate  * This routine is called to complete any runtime linker activity which may have
9590Sstevel@tonic-gate  * resulted in objects being loaded.  This is called from all user entry points
9600Sstevel@tonic-gate  * and from any internal dl*() requests.
9610Sstevel@tonic-gate  */
9620Sstevel@tonic-gate void
9634679Srie load_completion(Rt_map *nlmp)
9640Sstevel@tonic-gate {
9650Sstevel@tonic-gate 	Rt_map	**tobj = 0;
9664679Srie 	Lm_list	*nlml;
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	/*
9690Sstevel@tonic-gate 	 * Establish any .init processing.  Note, in a world of lazy loading,
9700Sstevel@tonic-gate 	 * objects may have been loaded regardless of whether the users request
9710Sstevel@tonic-gate 	 * was fulfilled (i.e., a dlsym() request may have failed to find a
9720Sstevel@tonic-gate 	 * symbol but objects might have been loaded during its search).  Thus,
9730Sstevel@tonic-gate 	 * any tsorting starts from the nlmp (new link-maps) pointer and not
9740Sstevel@tonic-gate 	 * necessarily from the link-map that may have satisfied the request.
9750Sstevel@tonic-gate 	 *
9761824Srie 	 * Note, the primary link-map has an initialization phase where dynamic
9771824Srie 	 * .init firing is suppressed.  This provides for a simple and clean
9781824Srie 	 * handshake with the primary link-maps libc, which is important for
9791824Srie 	 * establishing uberdata.  In addition, auditors often obtain handles
9801824Srie 	 * to primary link-map objects as the objects are loaded, so as to
9811824Srie 	 * inspect the link-map for symbols.  This inspection is allowed without
9821824Srie 	 * running any code on the primary link-map, as running this code may
9831824Srie 	 * reenter the auditor, who may not yet have finished its own
9840Sstevel@tonic-gate 	 * initialization.
9850Sstevel@tonic-gate 	 */
9861824Srie 	if (nlmp)
9871824Srie 		nlml = LIST(nlmp);
9881824Srie 
9896229Sedp 	if (nlmp && nlml->lm_init && ((nlml != &lml_main) ||
9906229Sedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
991280Srie 		if ((tobj = tsort(nlmp, LIST(nlmp)->lm_init,
992280Srie 		    RT_SORT_REV)) == (Rt_map **)S_ERROR)
9930Sstevel@tonic-gate 			tobj = 0;
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	/*
9971824Srie 	 * Make sure any alternative link-map retrieves any external interfaces
9981824Srie 	 * and initializes threads.
9991824Srie 	 */
10001824Srie 	if (nlmp && (nlml != &lml_main)) {
10011824Srie 		(void) rt_get_extern(nlml, nlmp);
10021824Srie 		rt_thr_init(nlml);
10031824Srie 	}
10041824Srie 
10051824Srie 	/*
10061824Srie 	 * Traverse the list of new link-maps and register any dynamic TLS.
10071824Srie 	 * This storage is established for any objects not on the primary
10081824Srie 	 * link-map, and for any objects added to the primary link-map after
10091824Srie 	 * static TLS has been registered.
10101824Srie 	 */
10116229Sedp 	if (nlmp && nlml->lm_tls && ((nlml != &lml_main) ||
10126229Sedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
10131824Srie 		Rt_map	*lmp;
10141824Srie 
10151824Srie 		for (lmp = nlmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
10161824Srie 			if (PTTLS(lmp) && PTTLS(lmp)->p_memsz)
10171824Srie 				tls_modaddrem(lmp, TM_FLG_MODADD);
10181824Srie 		}
10191824Srie 		nlml->lm_tls = 0;
10201824Srie 	}
10211824Srie 
10221824Srie 	/*
10230Sstevel@tonic-gate 	 * Fire any .init's.
10240Sstevel@tonic-gate 	 */
10250Sstevel@tonic-gate 	if (tobj)
10260Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_SORT);
10270Sstevel@tonic-gate }
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate /*
10300Sstevel@tonic-gate  * Append an item to the specified list, and return a pointer to the list
10310Sstevel@tonic-gate  * node created.
10320Sstevel@tonic-gate  */
10330Sstevel@tonic-gate Listnode *
10340Sstevel@tonic-gate list_append(List *lst, const void *item)
10350Sstevel@tonic-gate {
1036*6387Srie 	Listnode	*_lnp;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == 0)
10390Sstevel@tonic-gate 		return (0);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	_lnp->data = (void *)item;
10420Sstevel@tonic-gate 	_lnp->next = NULL;
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	if (lst->head == NULL)
10450Sstevel@tonic-gate 		lst->tail = lst->head = _lnp;
10460Sstevel@tonic-gate 	else {
10470Sstevel@tonic-gate 		lst->tail->next = _lnp;
10480Sstevel@tonic-gate 		lst->tail = lst->tail->next;
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 	return (_lnp);
10510Sstevel@tonic-gate }
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate /*
10550Sstevel@tonic-gate  * Add an item after specified listnode, and return a pointer to the list
10560Sstevel@tonic-gate  * node created.
10570Sstevel@tonic-gate  */
10580Sstevel@tonic-gate Listnode *
10590Sstevel@tonic-gate list_insert(List *lst, const void *item, Listnode *lnp)
10600Sstevel@tonic-gate {
1061*6387Srie 	Listnode	*_lnp;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
10640Sstevel@tonic-gate 		return (0);
10650Sstevel@tonic-gate 
10660Sstevel@tonic-gate 	_lnp->data = (void *)item;
10670Sstevel@tonic-gate 	_lnp->next = lnp->next;
10680Sstevel@tonic-gate 	if (_lnp->next == NULL)
10690Sstevel@tonic-gate 		lst->tail = _lnp;
10700Sstevel@tonic-gate 	lnp->next = _lnp;
10710Sstevel@tonic-gate 	return (_lnp);
10720Sstevel@tonic-gate }
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate /*
10750Sstevel@tonic-gate  * Prepend an item to the specified list, and return a pointer to the
10760Sstevel@tonic-gate  * list node created.
10770Sstevel@tonic-gate  */
10780Sstevel@tonic-gate Listnode *
10790Sstevel@tonic-gate list_prepend(List * lst, const void * item)
10800Sstevel@tonic-gate {
1081*6387Srie 	Listnode	*_lnp;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
10840Sstevel@tonic-gate 		return (0);
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	_lnp->data = (void *)item;
10870Sstevel@tonic-gate 
10880Sstevel@tonic-gate 	if (lst->head == NULL) {
10890Sstevel@tonic-gate 		_lnp->next = NULL;
10900Sstevel@tonic-gate 		lst->tail = lst->head = _lnp;
10910Sstevel@tonic-gate 	} else {
10920Sstevel@tonic-gate 		_lnp->next = lst->head;
10930Sstevel@tonic-gate 		lst->head = _lnp;
10940Sstevel@tonic-gate 	}
10950Sstevel@tonic-gate 	return (_lnp);
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate /*
11000Sstevel@tonic-gate  * Delete a 'listnode' from a list.
11010Sstevel@tonic-gate  */
11020Sstevel@tonic-gate void
11033731Srie list_delete(List *lst, void *item)
11040Sstevel@tonic-gate {
11053731Srie 	Listnode	*clnp, *plnp;
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	for (plnp = NULL, clnp = lst->head; clnp; clnp = clnp->next) {
11080Sstevel@tonic-gate 		if (item == clnp->data)
11090Sstevel@tonic-gate 			break;
11100Sstevel@tonic-gate 		plnp = clnp;
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate 
11130Sstevel@tonic-gate 	if (clnp == 0)
11140Sstevel@tonic-gate 		return;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	if (lst->head == clnp)
11170Sstevel@tonic-gate 		lst->head = clnp->next;
11180Sstevel@tonic-gate 	if (lst->tail == clnp)
11190Sstevel@tonic-gate 		lst->tail = plnp;
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	if (plnp)
11220Sstevel@tonic-gate 		plnp->next = clnp->next;
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	free(clnp);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate /*
11280Sstevel@tonic-gate  * Append an item to the specified link map control list.
11290Sstevel@tonic-gate  */
11300Sstevel@tonic-gate void
11310Sstevel@tonic-gate lm_append(Lm_list *lml, Aliste lmco, Rt_map *lmp)
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 	Lm_cntl	*lmc;
11340Sstevel@tonic-gate 	int	add = 1;
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/*
11370Sstevel@tonic-gate 	 * Indicate that this link-map list has a new object.
11380Sstevel@tonic-gate 	 */
11390Sstevel@tonic-gate 	(lml->lm_obj)++;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	/*
11422454Srie 	 * If we're about to add a new object to the main link-map control list,
11432454Srie 	 * alert the debuggers that we are about to mess with this list.
11442454Srie 	 * Additions of individual objects to the main link-map control list
11452454Srie 	 * occur during initial setup as the applications immediate dependencies
11462454Srie 	 * are loaded.  Individual objects are also loaded on the main link-map
11472454Srie 	 * control list of new alternative link-map control lists.
11480Sstevel@tonic-gate 	 */
11495892Sab196087 	if ((lmco == ALIST_OFF_DATA) &&
11505892Sab196087 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
11512454Srie 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 	/* LINTED */
11545892Sab196087 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, lmco);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/*
11570Sstevel@tonic-gate 	 * A link-map list header points to one of more link-map control lists
11580Sstevel@tonic-gate 	 * (see include/rtld.h).  The initial list, pointed to by lm_cntl, is
11590Sstevel@tonic-gate 	 * the list of relocated objects.  Other lists maintain objects that
11600Sstevel@tonic-gate 	 * are still being analyzed or relocated.  This list provides the core
11610Sstevel@tonic-gate 	 * link-map list information used by all ld.so.1 routines.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	if (lmc->lc_head == NULL) {
11640Sstevel@tonic-gate 		/*
11650Sstevel@tonic-gate 		 * If this is the first link-map for the given control list,
11660Sstevel@tonic-gate 		 * initialize the list.
11670Sstevel@tonic-gate 		 */
11680Sstevel@tonic-gate 		lmc->lc_head = lmc->lc_tail = lmp;
11690Sstevel@tonic-gate 		add = 0;
11700Sstevel@tonic-gate 
11713466Srie 	} else if (FLAGS(lmp) & FLG_RT_OBJINTPO) {
11720Sstevel@tonic-gate 		Rt_map	*tlmp;
11730Sstevel@tonic-gate 
11740Sstevel@tonic-gate 		/*
11750Sstevel@tonic-gate 		 * If this is an interposer then append the link-map following
11760Sstevel@tonic-gate 		 * any other interposers (these are objects that have been
11770Sstevel@tonic-gate 		 * previously preloaded, or were identified with -z interpose).
11780Sstevel@tonic-gate 		 * Interposers can only be inserted on the first link-map
11790Sstevel@tonic-gate 		 * control list, as once relocation has started, interposition
11800Sstevel@tonic-gate 		 * from new interposers can't be guaranteed.
11810Sstevel@tonic-gate 		 *
11820Sstevel@tonic-gate 		 * NOTE: We do not interpose on the head of a list.  This model
11830Sstevel@tonic-gate 		 * evolved because dynamic executables have already been fully
11840Sstevel@tonic-gate 		 * relocated within themselves and thus can't be interposed on.
11850Sstevel@tonic-gate 		 * Nowadays it's possible to have shared objects at the head of
11860Sstevel@tonic-gate 		 * a list, which conceptually means they could be interposed on.
11870Sstevel@tonic-gate 		 * But, shared objects can be created via dldump() and may only
11880Sstevel@tonic-gate 		 * be partially relocated (just relatives), in which case they
11890Sstevel@tonic-gate 		 * are interposable, but are marked as fixed (ET_EXEC).
11900Sstevel@tonic-gate 		 *
11910Sstevel@tonic-gate 		 * Thus we really don't have a clear method of deciding when the
11920Sstevel@tonic-gate 		 * head of a link-map is interposable.  So, to be consistent,
11930Sstevel@tonic-gate 		 * for now only add interposers after the link-map lists head
11940Sstevel@tonic-gate 		 * object.
11950Sstevel@tonic-gate 		 */
11960Sstevel@tonic-gate 		for (tlmp = (Rt_map *)NEXT(lmc->lc_head); tlmp;
11970Sstevel@tonic-gate 		    tlmp = (Rt_map *)NEXT(tlmp)) {
11980Sstevel@tonic-gate 
11993466Srie 			if (FLAGS(tlmp) & FLG_RT_OBJINTPO)
12000Sstevel@tonic-gate 				continue;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 			/*
12030Sstevel@tonic-gate 			 * Insert the new link-map before this non-interposer,
12040Sstevel@tonic-gate 			 * and indicate an interposer is found.
12050Sstevel@tonic-gate 			 */
12060Sstevel@tonic-gate 			NEXT((Rt_map *)PREV(tlmp)) = (Link_map *)lmp;
12070Sstevel@tonic-gate 			PREV(lmp) = PREV(tlmp);
12080Sstevel@tonic-gate 
12090Sstevel@tonic-gate 			NEXT(lmp) = (Link_map *)tlmp;
12100Sstevel@tonic-gate 			PREV(tlmp) = (Link_map *)lmp;
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 			lmc->lc_flags |= LMC_FLG_REANALYZE;
12130Sstevel@tonic-gate 			add = 0;
12140Sstevel@tonic-gate 			break;
12150Sstevel@tonic-gate 		}
12160Sstevel@tonic-gate 	}
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate 	/*
12190Sstevel@tonic-gate 	 * Fall through to appending the new link map to the tail of the list.
12200Sstevel@tonic-gate 	 * If we're processing the initial objects of this link-map list, add
12210Sstevel@tonic-gate 	 * them to the backward compatibility list.
12220Sstevel@tonic-gate 	 */
12230Sstevel@tonic-gate 	if (add) {
12240Sstevel@tonic-gate 		NEXT(lmc->lc_tail) = (Link_map *)lmp;
12250Sstevel@tonic-gate 		PREV(lmp) = (Link_map *)lmc->lc_tail;
12260Sstevel@tonic-gate 		lmc->lc_tail = lmp;
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	/*
12300Sstevel@tonic-gate 	 * Having added this link-map to a control list, indicate which control
12310Sstevel@tonic-gate 	 * list the link-map belongs to.  Note, control list information is
12320Sstevel@tonic-gate 	 * always maintained as an offset, as the Alist can be reallocated.
12330Sstevel@tonic-gate 	 */
12340Sstevel@tonic-gate 	CNTL(lmp) = lmco;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	/*
12370Sstevel@tonic-gate 	 * Indicate if an interposer is found.  Note that the first object on a
12380Sstevel@tonic-gate 	 * link-map can be explicitly defined as an interposer so that it can
12390Sstevel@tonic-gate 	 * provide interposition over direct binding requests.
12400Sstevel@tonic-gate 	 */
12413466Srie 	if (FLAGS(lmp) & MSK_RT_INTPOSE)
12420Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_INTRPOSE;
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 	/*
12450Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
12460Sstevel@tonic-gate 	 * pointers to the main control list.
12470Sstevel@tonic-gate 	 */
12485892Sab196087 	if (lmco == ALIST_OFF_DATA) {
12490Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12500Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12510Sstevel@tonic-gate 	}
12520Sstevel@tonic-gate }
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate /*
12550Sstevel@tonic-gate  * Delete an item from the specified link map control list.
12560Sstevel@tonic-gate  */
12570Sstevel@tonic-gate void
12580Sstevel@tonic-gate lm_delete(Lm_list *lml, Rt_map *lmp)
12590Sstevel@tonic-gate {
12600Sstevel@tonic-gate 	Lm_cntl	*lmc;
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	/*
12630Sstevel@tonic-gate 	 * If the control list pointer hasn't been initialized, this object
12640Sstevel@tonic-gate 	 * never got added to a link-map list.
12650Sstevel@tonic-gate 	 */
12660Sstevel@tonic-gate 	if (CNTL(lmp) == 0)
12670Sstevel@tonic-gate 		return;
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 	/*
12702454Srie 	 * If we're about to delete an object from the main link-map control
12712454Srie 	 * list, alert the debuggers that we are about to mess with this list.
12720Sstevel@tonic-gate 	 */
12735892Sab196087 	if ((CNTL(lmp) == ALIST_OFF_DATA) &&
12745892Sab196087 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
12750Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_DELETE);
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 	/* LINTED */
12785892Sab196087 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(lmp));
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	if (lmc->lc_head == lmp)
12810Sstevel@tonic-gate 		lmc->lc_head = (Rt_map *)NEXT(lmp);
12820Sstevel@tonic-gate 	else
12830Sstevel@tonic-gate 		NEXT((Rt_map *)PREV(lmp)) = (void *)NEXT(lmp);
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 	if (lmc->lc_tail == lmp)
12860Sstevel@tonic-gate 		lmc->lc_tail = (Rt_map *)PREV(lmp);
12870Sstevel@tonic-gate 	else
12880Sstevel@tonic-gate 		PREV((Rt_map *)NEXT(lmp)) = PREV(lmp);
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 	/*
12910Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
12920Sstevel@tonic-gate 	 * pointers to the main control list.
12930Sstevel@tonic-gate 	 */
12945892Sab196087 	if (lmc == (Lm_cntl *)&lml->lm_lists->al_data) {
12950Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12960Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12970Sstevel@tonic-gate 	}
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	/*
13000Sstevel@tonic-gate 	 * Indicate we have one less object on this control list.
13010Sstevel@tonic-gate 	 */
13020Sstevel@tonic-gate 	(lml->lm_obj)--;
13030Sstevel@tonic-gate }
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate /*
13060Sstevel@tonic-gate  * Move a link-map control list to another.  Objects that are being relocated
13070Sstevel@tonic-gate  * are maintained on secondary control lists.  Once their relocation is
13080Sstevel@tonic-gate  * complete, the entire list is appended to the previous control list, as this
13090Sstevel@tonic-gate  * list must have been the trigger for generating the new control list.
13100Sstevel@tonic-gate  */
13110Sstevel@tonic-gate void
13120Sstevel@tonic-gate lm_move(Lm_list *lml, Aliste nlmco, Aliste plmco, Lm_cntl *nlmc, Lm_cntl *plmc)
13130Sstevel@tonic-gate {
13140Sstevel@tonic-gate 	Rt_map	*lmp;
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	/*
13172454Srie 	 * If we're about to add a new family of objects to the main link-map
13182454Srie 	 * control list, alert the debuggers that we are about to mess with this
13192454Srie 	 * list.  Additions of object families to the main link-map control
13202454Srie 	 * list occur during lazy loading, filtering and dlopen().
13210Sstevel@tonic-gate 	 */
13225892Sab196087 	if ((plmco == ALIST_OFF_DATA) &&
13235892Sab196087 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
13240Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
13250Sstevel@tonic-gate 
13262454Srie 	DBG_CALL(Dbg_file_cntl(lml, nlmco, plmco));
13272454Srie 
13280Sstevel@tonic-gate 	/*
13290Sstevel@tonic-gate 	 * Indicate each new link-map has been moved to the previous link-map
13300Sstevel@tonic-gate 	 * control list.
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 	for (lmp = nlmc->lc_head; lmp; lmp = (Rt_map *)NEXT(lmp))
13330Sstevel@tonic-gate 		CNTL(lmp) = plmco;
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 	/*
13360Sstevel@tonic-gate 	 * Move the new link-map control list, to the callers link-map control
13370Sstevel@tonic-gate 	 * list.
13380Sstevel@tonic-gate 	 */
13390Sstevel@tonic-gate 	if (plmc->lc_head == 0) {
13400Sstevel@tonic-gate 		plmc->lc_head = nlmc->lc_head;
13410Sstevel@tonic-gate 		PREV(nlmc->lc_head) = 0;
13420Sstevel@tonic-gate 	} else {
13430Sstevel@tonic-gate 		NEXT(plmc->lc_tail) = (Link_map *)nlmc->lc_head;
13440Sstevel@tonic-gate 		PREV(nlmc->lc_head) = (Link_map *)plmc->lc_tail;
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	plmc->lc_tail = nlmc->lc_tail;
13480Sstevel@tonic-gate 	nlmc->lc_head = nlmc->lc_tail = 0;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	/*
13510Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
13520Sstevel@tonic-gate 	 * pointers to the main control list.
13530Sstevel@tonic-gate 	 */
13545892Sab196087 	if (plmco == ALIST_OFF_DATA) {
13550Sstevel@tonic-gate 		lml->lm_head = plmc->lc_head;
13560Sstevel@tonic-gate 		lml->lm_tail = plmc->lc_tail;
13570Sstevel@tonic-gate 	}
13580Sstevel@tonic-gate }
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate /*
13610Sstevel@tonic-gate  * Environment variables can have a variety of defined permutations, and thus
13620Sstevel@tonic-gate  * the following infrastructure exists to allow this variety and to select the
13630Sstevel@tonic-gate  * required definition.
13640Sstevel@tonic-gate  *
13650Sstevel@tonic-gate  * Environment variables can be defined as 32- or 64-bit specific, and if so
13660Sstevel@tonic-gate  * they will take precedence over any instruction set neutral form.  Typically
13670Sstevel@tonic-gate  * this is only useful when the environment value is an informational string.
13680Sstevel@tonic-gate  *
13690Sstevel@tonic-gate  * Environment variables may be obtained from the standard user environment or
13700Sstevel@tonic-gate  * from a configuration file.  The latter provides a fallback if no user
13710Sstevel@tonic-gate  * environment setting is found, and can take two forms:
13720Sstevel@tonic-gate  *
13730Sstevel@tonic-gate  *  .	a replaceable definition - this will be used if no user environment
13740Sstevel@tonic-gate  *	setting has been seen, or
13750Sstevel@tonic-gate  *
13760Sstevel@tonic-gate  *  .	an permanent definition - this will be used no matter what user
13770Sstevel@tonic-gate  *	environment setting is seen.  In the case of list variables it will be
13780Sstevel@tonic-gate  *	appended to any process environment setting seen.
13790Sstevel@tonic-gate  *
13800Sstevel@tonic-gate  * Environment variables can be defined without a value (ie. LD_XXXX=) so as to
13810Sstevel@tonic-gate  * override any replaceable environment variables from a configuration file.
13820Sstevel@tonic-gate  */
13830Sstevel@tonic-gate static	u_longlong_t		rplgen;		/* replaceable generic */
13840Sstevel@tonic-gate 						/*	variables */
13850Sstevel@tonic-gate static	u_longlong_t		rplisa;		/* replaceable ISA specific */
13860Sstevel@tonic-gate 						/*	variables */
13870Sstevel@tonic-gate static	u_longlong_t		prmgen;		/* permanent generic */
13880Sstevel@tonic-gate 						/*	variables */
13890Sstevel@tonic-gate static	u_longlong_t		prmisa;		/* permanent ISA specific */
13900Sstevel@tonic-gate 						/*	variables */
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate /*
13930Sstevel@tonic-gate  * Classify an environment variables type.
13940Sstevel@tonic-gate  */
13950Sstevel@tonic-gate #define	ENV_TYP_IGNORE		0x1		/* ignore - variable is for */
13960Sstevel@tonic-gate 						/*	the wrong ISA */
13970Sstevel@tonic-gate #define	ENV_TYP_ISA		0x2		/* variable is ISA specific */
13980Sstevel@tonic-gate #define	ENV_TYP_CONFIG		0x4		/* variable obtained from a */
13990Sstevel@tonic-gate 						/*	config file */
14000Sstevel@tonic-gate #define	ENV_TYP_PERMANT		0x8		/* variable is permanent */
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate /*
14030Sstevel@tonic-gate  * Identify all environment variables.
14040Sstevel@tonic-gate  */
14050Sstevel@tonic-gate #define	ENV_FLG_AUDIT		0x0000000001ULL
14060Sstevel@tonic-gate #define	ENV_FLG_AUDIT_ARGS	0x0000000002ULL
14070Sstevel@tonic-gate #define	ENV_FLG_BIND_NOW	0x0000000004ULL
14080Sstevel@tonic-gate #define	ENV_FLG_BIND_NOT	0x0000000008ULL
14090Sstevel@tonic-gate #define	ENV_FLG_BINDINGS	0x0000000010ULL
14100Sstevel@tonic-gate #define	ENV_FLG_CONCURRENCY	0x0000000020ULL
14110Sstevel@tonic-gate #define	ENV_FLG_CONFGEN		0x0000000040ULL
14120Sstevel@tonic-gate #define	ENV_FLG_CONFIG		0x0000000080ULL
14130Sstevel@tonic-gate #define	ENV_FLG_DEBUG		0x0000000100ULL
14140Sstevel@tonic-gate #define	ENV_FLG_DEBUG_OUTPUT	0x0000000200ULL
14150Sstevel@tonic-gate #define	ENV_FLG_DEMANGLE	0x0000000400ULL
14160Sstevel@tonic-gate #define	ENV_FLG_FLAGS		0x0000000800ULL
14170Sstevel@tonic-gate #define	ENV_FLG_INIT		0x0000001000ULL
14180Sstevel@tonic-gate #define	ENV_FLG_LIBPATH		0x0000002000ULL
14190Sstevel@tonic-gate #define	ENV_FLG_LOADAVAIL	0x0000004000ULL
14200Sstevel@tonic-gate #define	ENV_FLG_LOADFLTR	0x0000008000ULL
14210Sstevel@tonic-gate #define	ENV_FLG_NOAUDIT		0x0000010000ULL
14220Sstevel@tonic-gate #define	ENV_FLG_NOAUXFLTR	0x0000020000ULL
14230Sstevel@tonic-gate #define	ENV_FLG_NOBAPLT		0x0000040000ULL
14240Sstevel@tonic-gate #define	ENV_FLG_NOCONFIG	0x0000080000ULL
14250Sstevel@tonic-gate #define	ENV_FLG_NODIRCONFIG	0x0000100000ULL
14260Sstevel@tonic-gate #define	ENV_FLG_NODIRECT	0x0000200000ULL
14270Sstevel@tonic-gate #define	ENV_FLG_NOENVCONFIG	0x0000400000ULL
14280Sstevel@tonic-gate #define	ENV_FLG_NOLAZY		0x0000800000ULL
14290Sstevel@tonic-gate #define	ENV_FLG_NOOBJALTER	0x0001000000ULL
14300Sstevel@tonic-gate #define	ENV_FLG_NOVERSION	0x0002000000ULL
14310Sstevel@tonic-gate #define	ENV_FLG_PRELOAD		0x0004000000ULL
14320Sstevel@tonic-gate #define	ENV_FLG_PROFILE		0x0008000000ULL
14330Sstevel@tonic-gate #define	ENV_FLG_PROFILE_OUTPUT	0x0010000000ULL
14340Sstevel@tonic-gate #define	ENV_FLG_SIGNAL		0x0020000000ULL
14350Sstevel@tonic-gate #define	ENV_FLG_TRACE_OBJS	0x0040000000ULL
14360Sstevel@tonic-gate #define	ENV_FLG_TRACE_PTHS	0x0080000000ULL
14370Sstevel@tonic-gate #define	ENV_FLG_UNREF		0x0100000000ULL
14380Sstevel@tonic-gate #define	ENV_FLG_UNUSED		0x0200000000ULL
14390Sstevel@tonic-gate #define	ENV_FLG_VERBOSE		0x0400000000ULL
14400Sstevel@tonic-gate #define	ENV_FLG_WARN		0x0800000000ULL
14410Sstevel@tonic-gate #define	ENV_FLG_NOFLTCONFIG	0x1000000000ULL
1442280Srie #define	ENV_FLG_BIND_LAZY	0x2000000000ULL
14434947Srie #define	ENV_FLG_NOUNRESWEAK	0x4000000000ULL
14446150Srie #define	ENV_FLG_NOPAREXT	0x8000000000ULL
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
14470Sstevel@tonic-gate #define	ENV_FLG_FIX_1		0x8000000000ULL
14480Sstevel@tonic-gate #endif
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate #define	SEL_REPLACE		0x0001
14510Sstevel@tonic-gate #define	SEL_PERMANT		0x0002
14520Sstevel@tonic-gate #define	SEL_ACT_RT		0x0100	/* setting rtld_flags */
14530Sstevel@tonic-gate #define	SEL_ACT_RT2		0x0200	/* setting rtld_flags2 */
14540Sstevel@tonic-gate #define	SEL_ACT_STR		0x0400	/* setting string value */
14550Sstevel@tonic-gate #define	SEL_ACT_LML		0x0800	/* setting lml_flags */
14560Sstevel@tonic-gate #define	SEL_ACT_LMLT		0x1000	/* setting lml_tflags */
14570Sstevel@tonic-gate #define	SEL_ACT_SPEC_1		0x2000	/* For FLG_{FLAGS, LIBPATH} */
14580Sstevel@tonic-gate #define	SEL_ACT_SPEC_2		0x4000	/* need special handling */
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate /*
14610Sstevel@tonic-gate  * Pattern match an LD_XXXX environment variable.  s1 points to the XXXX part
14620Sstevel@tonic-gate  * and len specifies its length (comparing a strings length before the string
14630Sstevel@tonic-gate  * itself speed things up).  s2 points to the token itself which has already
14640Sstevel@tonic-gate  * had any leading white-space removed.
14650Sstevel@tonic-gate  */
14660Sstevel@tonic-gate static void
14670Sstevel@tonic-gate ld_generic_env(const char *s1, size_t len, const char *s2, Word *lmflags,
14680Sstevel@tonic-gate     Word *lmtflags, uint_t env_flags, int aout)
14690Sstevel@tonic-gate {
14700Sstevel@tonic-gate 	u_longlong_t	variable = 0;
14711824Srie 	ushort_t	select = 0;
14721824Srie 	const char	**str;
14731824Srie 	Word		val = 0;
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	/*
14760Sstevel@tonic-gate 	 * Determine whether we're dealing with a replaceable or permanent
14770Sstevel@tonic-gate 	 * string.
14780Sstevel@tonic-gate 	 */
14790Sstevel@tonic-gate 	if (env_flags & ENV_TYP_PERMANT) {
14800Sstevel@tonic-gate 		/*
14810Sstevel@tonic-gate 		 * If the string is from a configuration file and defined as
14820Sstevel@tonic-gate 		 * permanent, assign it as permanent.
14830Sstevel@tonic-gate 		 */
14840Sstevel@tonic-gate 		select |= SEL_PERMANT;
14850Sstevel@tonic-gate 	} else
14860Sstevel@tonic-gate 		select |= SEL_REPLACE;
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	/*
14890Sstevel@tonic-gate 	 * Parse the variable given.
14900Sstevel@tonic-gate 	 *
14910Sstevel@tonic-gate 	 * The LD_AUDIT family.
14920Sstevel@tonic-gate 	 */
14930Sstevel@tonic-gate 	if (*s1 == 'A') {
14940Sstevel@tonic-gate 		if ((len == MSG_LD_AUDIT_SIZE) && (strncmp(s1,
14950Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_AUDIT), MSG_LD_AUDIT_SIZE) == 0)) {
14960Sstevel@tonic-gate 			/*
14970Sstevel@tonic-gate 			 * Replaceable and permanent audit objects can exist.
14980Sstevel@tonic-gate 			 */
14990Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1500827Sseizo 			if (select & SEL_REPLACE)
1501827Sseizo 				str = &rpl_audit;
1502827Sseizo 			else {
1503827Sseizo 				str = &prm_audit;
1504827Sseizo 				rpl_audit = 0;
1505827Sseizo 			}
15060Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT;
15070Sstevel@tonic-gate 		} else if ((len == MSG_LD_AUDIT_ARGS_SIZE) &&
15080Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_AUDIT_ARGS),
15090Sstevel@tonic-gate 		    MSG_LD_AUDIT_ARGS_SIZE) == 0)) {
15100Sstevel@tonic-gate 			/*
15110Sstevel@tonic-gate 			 * A specialized variable for plt_exit() use, not
15120Sstevel@tonic-gate 			 * documented for general use.
15130Sstevel@tonic-gate 			 */
15140Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15150Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT_ARGS;
15160Sstevel@tonic-gate 		}
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate 	/*
15190Sstevel@tonic-gate 	 * The LD_BIND family and LD_BREADTH (historic).
15200Sstevel@tonic-gate 	 */
15210Sstevel@tonic-gate 	else if (*s1 == 'B') {
1522280Srie 		if ((len == MSG_LD_BIND_LAZY_SIZE) && (strncmp(s1,
1523280Srie 		    MSG_ORIG(MSG_LD_BIND_LAZY),
1524280Srie 		    MSG_LD_BIND_LAZY_SIZE) == 0)) {
1525280Srie 			select |= SEL_ACT_RT2;
1526280Srie 			val = RT_FL2_BINDLAZY;
1527280Srie 			variable = ENV_FLG_BIND_LAZY;
1528280Srie 		} else if ((len == MSG_LD_BIND_NOW_SIZE) && (strncmp(s1,
15290Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOW), MSG_LD_BIND_NOW_SIZE) == 0)) {
15300Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
15310Sstevel@tonic-gate 			val = RT_FL2_BINDNOW;
15320Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOW;
15330Sstevel@tonic-gate 		} else if ((len == MSG_LD_BIND_NOT_SIZE) && (strncmp(s1,
15340Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOT), MSG_LD_BIND_NOT_SIZE) == 0)) {
15350Sstevel@tonic-gate 			/*
15360Sstevel@tonic-gate 			 * Another trick, enabled to help debug AOUT
15370Sstevel@tonic-gate 			 * applications under BCP, but not documented for
15380Sstevel@tonic-gate 			 * general use.
15390Sstevel@tonic-gate 			 */
15400Sstevel@tonic-gate 			select |= SEL_ACT_RT;
15410Sstevel@tonic-gate 			val = RT_FL_NOBIND;
15420Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOT;
15430Sstevel@tonic-gate 		} else if ((len == MSG_LD_BINDINGS_SIZE) && (strncmp(s1,
15440Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BINDINGS), MSG_LD_BINDINGS_SIZE) == 0)) {
15450Sstevel@tonic-gate 			/*
15460Sstevel@tonic-gate 			 * This variable is simply for backward compatibility.
15470Sstevel@tonic-gate 			 * If this and LD_DEBUG are both specified, only one of
15480Sstevel@tonic-gate 			 * the strings is going to get processed.
15490Sstevel@tonic-gate 			 */
15500Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15510Sstevel@tonic-gate 			variable = ENV_FLG_BINDINGS;
15520Sstevel@tonic-gate #ifndef LD_BREADTH_DISABLED
15530Sstevel@tonic-gate 		} else if ((len == MSG_LD_BREADTH_SIZE) && (strncmp(s1,
15540Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BREADTH), MSG_LD_BREADTH_SIZE) == 0)) {
15550Sstevel@tonic-gate 			/*
15560Sstevel@tonic-gate 			 * Besides some old patches this is no longer available.
15570Sstevel@tonic-gate 			 */
15580Sstevel@tonic-gate 			rtld_flags |= RT_FL_BREADTH;
15590Sstevel@tonic-gate 			return;
15600Sstevel@tonic-gate #endif
15610Sstevel@tonic-gate 		}
15620Sstevel@tonic-gate 	}
15630Sstevel@tonic-gate 	/*
15640Sstevel@tonic-gate 	 * LD_CONCURRENCY and LD_CONFIG family.
15650Sstevel@tonic-gate 	 */
15660Sstevel@tonic-gate 	else if (*s1 == 'C') {
15670Sstevel@tonic-gate 		if ((len == MSG_LD_CONCURRENCY_SIZE) && (strncmp(s1,
15680Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONCURRENCY),
15690Sstevel@tonic-gate 		    MSG_LD_CONCURRENCY_SIZE) == 0)) {
15700Sstevel@tonic-gate 			/*
15710Sstevel@tonic-gate 			 * Waiting in the wings, as concurrency checking isn't
15720Sstevel@tonic-gate 			 * yet enabled.
15730Sstevel@tonic-gate 			 */
15740Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15750Sstevel@tonic-gate 			variable = ENV_FLG_CONCURRENCY;
15760Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFGEN_SIZE) && (strncmp(s1,
15770Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFGEN), MSG_LD_CONFGEN_SIZE) == 0)) {
15780Sstevel@tonic-gate 			/*
15790Sstevel@tonic-gate 			 * Set by crle(1) to indicate it's building a
15800Sstevel@tonic-gate 			 * configuration file, not documented for general use.
15810Sstevel@tonic-gate 			 */
15820Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15830Sstevel@tonic-gate 			variable = ENV_FLG_CONFGEN;
15840Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFIG_SIZE) && (strncmp(s1,
15850Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFIG), MSG_LD_CONFIG_SIZE) == 0)) {
15860Sstevel@tonic-gate 			/*
15870Sstevel@tonic-gate 			 * Secure applications must use a default configuration
15880Sstevel@tonic-gate 			 * file.  A setting from a configuration file doesn't
15890Sstevel@tonic-gate 			 * make sense (given we must be reading a configuration
15900Sstevel@tonic-gate 			 * file to have gotten this).
15910Sstevel@tonic-gate 			 */
15920Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_SECURE) ||
15930Sstevel@tonic-gate 			    (env_flags & ENV_TYP_CONFIG))
15940Sstevel@tonic-gate 				return;
15950Sstevel@tonic-gate 			select |= SEL_ACT_STR;
15960Sstevel@tonic-gate 			str = &config->c_name;
15970Sstevel@tonic-gate 			variable = ENV_FLG_CONFIG;
15980Sstevel@tonic-gate 		}
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 	/*
16010Sstevel@tonic-gate 	 * The LD_DEBUG family and LD_DEMANGLE.
16020Sstevel@tonic-gate 	 */
16030Sstevel@tonic-gate 	else if (*s1 == 'D') {
16040Sstevel@tonic-gate 		if ((len == MSG_LD_DEBUG_SIZE) && (strncmp(s1,
16050Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG), MSG_LD_DEBUG_SIZE) == 0)) {
16060Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1607827Sseizo 			if (select & SEL_REPLACE)
1608827Sseizo 				str = &rpl_debug;
1609827Sseizo 			else {
1610827Sseizo 				str = &prm_debug;
1611827Sseizo 				rpl_debug = 0;
1612827Sseizo 			}
16130Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG;
16140Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEBUG_OUTPUT_SIZE) && (strncmp(s1,
16150Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG_OUTPUT),
16160Sstevel@tonic-gate 		    MSG_LD_DEBUG_OUTPUT_SIZE) == 0)) {
16170Sstevel@tonic-gate 			select |= SEL_ACT_STR;
16180Sstevel@tonic-gate 			str = &dbg_file;
16190Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG_OUTPUT;
16200Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEMANGLE_SIZE) && (strncmp(s1,
16210Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEMANGLE), MSG_LD_DEMANGLE_SIZE) == 0)) {
16220Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16230Sstevel@tonic-gate 			val = RT_FL_DEMANGLE;
16240Sstevel@tonic-gate 			variable = ENV_FLG_DEMANGLE;
16250Sstevel@tonic-gate 		}
16260Sstevel@tonic-gate 	}
16270Sstevel@tonic-gate 	/*
16280Sstevel@tonic-gate 	 * LD_FLAGS - collect the best variable definition.  On completion of
16290Sstevel@tonic-gate 	 * environment variable processing pass the result to ld_flags_env()
16300Sstevel@tonic-gate 	 * where they'll be decomposed and passed back to this routine.
16310Sstevel@tonic-gate 	 */
16320Sstevel@tonic-gate 	else if (*s1 == 'F') {
16330Sstevel@tonic-gate 		if ((len == MSG_LD_FLAGS_SIZE) && (strncmp(s1,
16340Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FLAGS), MSG_LD_FLAGS_SIZE) == 0)) {
16350Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1636827Sseizo 			if (select & SEL_REPLACE)
1637827Sseizo 				str = &rpl_ldflags;
1638827Sseizo 			else {
1639827Sseizo 				str = &prm_ldflags;
1640827Sseizo 				rpl_ldflags = 0;
1641827Sseizo 			}
16420Sstevel@tonic-gate 			variable = ENV_FLG_FLAGS;
16430Sstevel@tonic-gate 		}
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 	/*
16460Sstevel@tonic-gate 	 * LD_INIT (internal, used by ldd(1)).
16470Sstevel@tonic-gate 	 */
16480Sstevel@tonic-gate 	else if (*s1 == 'I') {
16490Sstevel@tonic-gate 		if ((len == MSG_LD_INIT_SIZE) && (strncmp(s1,
16500Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_INIT), MSG_LD_INIT_SIZE) == 0)) {
16510Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16520Sstevel@tonic-gate 			val = LML_FLG_TRC_INIT;
16530Sstevel@tonic-gate 			variable = ENV_FLG_INIT;
16540Sstevel@tonic-gate 		}
16550Sstevel@tonic-gate 	}
16560Sstevel@tonic-gate 	/*
16570Sstevel@tonic-gate 	 * The LD_LIBRARY_PATH and LD_LOAD families.
16580Sstevel@tonic-gate 	 */
16590Sstevel@tonic-gate 	else if (*s1 == 'L') {
16600Sstevel@tonic-gate 		if ((len == MSG_LD_LIBPATH_SIZE) && (strncmp(s1,
16610Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LIBPATH), MSG_LD_LIBPATH_SIZE) == 0)) {
16620Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1663827Sseizo 			if (select & SEL_REPLACE)
1664827Sseizo 				str = &rpl_libpath;
1665827Sseizo 			else {
1666827Sseizo 				str = &prm_libpath;
1667827Sseizo 				rpl_libpath = 0;
1668827Sseizo 			}
16690Sstevel@tonic-gate 			variable = ENV_FLG_LIBPATH;
16700Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADAVAIL_SIZE) && (strncmp(s1,
16710Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADAVAIL), MSG_LD_LOADAVAIL_SIZE) == 0)) {
16720Sstevel@tonic-gate 			/*
16730Sstevel@tonic-gate 			 * Internal use by crle(1), not documented for general
16740Sstevel@tonic-gate 			 * use.
16750Sstevel@tonic-gate 			 */
16760Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16770Sstevel@tonic-gate 			val = LML_FLG_LOADAVAIL;
16780Sstevel@tonic-gate 			variable = ENV_FLG_LOADAVAIL;
16790Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADFLTR_SIZE) && (strncmp(s1,
16800Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADFLTR), MSG_LD_LOADFLTR_SIZE) == 0)) {
16810Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
16820Sstevel@tonic-gate 			variable = ENV_FLG_LOADFLTR;
16830Sstevel@tonic-gate 		}
16840Sstevel@tonic-gate 	}
16850Sstevel@tonic-gate 	/*
16860Sstevel@tonic-gate 	 * The LD_NO family.
16870Sstevel@tonic-gate 	 */
16880Sstevel@tonic-gate 	else if (*s1 == 'N') {
16890Sstevel@tonic-gate 		if ((len == MSG_LD_NOAUDIT_SIZE) && (strncmp(s1,
16900Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUDIT), MSG_LD_NOAUDIT_SIZE) == 0)) {
16910Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16920Sstevel@tonic-gate 			val = RT_FL_NOAUDIT;
16930Sstevel@tonic-gate 			variable = ENV_FLG_NOAUDIT;
16940Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOAUXFLTR_SIZE) && (strncmp(s1,
16950Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUXFLTR), MSG_LD_NOAUXFLTR_SIZE) == 0)) {
16960Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16970Sstevel@tonic-gate 			val = RT_FL_NOAUXFLTR;
16980Sstevel@tonic-gate 			variable = ENV_FLG_NOAUXFLTR;
16990Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOBAPLT_SIZE) && (strncmp(s1,
17000Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOBAPLT), MSG_LD_NOBAPLT_SIZE) == 0)) {
17010Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17020Sstevel@tonic-gate 			val = RT_FL_NOBAPLT;
17030Sstevel@tonic-gate 			variable = ENV_FLG_NOBAPLT;
17040Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOCONFIG_SIZE) && (strncmp(s1,
17050Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOCONFIG), MSG_LD_NOCONFIG_SIZE) == 0)) {
17060Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17070Sstevel@tonic-gate 			val = RT_FL_NOCFG;
17080Sstevel@tonic-gate 			variable = ENV_FLG_NOCONFIG;
17090Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRCONFIG_SIZE) && (strncmp(s1,
17100Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRCONFIG),
17110Sstevel@tonic-gate 		    MSG_LD_NODIRCONFIG_SIZE) == 0)) {
17120Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17130Sstevel@tonic-gate 			val = RT_FL_NODIRCFG;
17140Sstevel@tonic-gate 			variable = ENV_FLG_NODIRCONFIG;
17150Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRECT_SIZE) && (strncmp(s1,
17160Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRECT), MSG_LD_NODIRECT_SIZE) == 0)) {
17170Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
17180Sstevel@tonic-gate 			val = LML_TFLG_NODIRECT;
17190Sstevel@tonic-gate 			variable = ENV_FLG_NODIRECT;
17200Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOENVCONFIG_SIZE) && (strncmp(s1,
17210Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOENVCONFIG),
17220Sstevel@tonic-gate 		    MSG_LD_NOENVCONFIG_SIZE) == 0)) {
17230Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17240Sstevel@tonic-gate 			val = RT_FL_NOENVCFG;
17250Sstevel@tonic-gate 			variable = ENV_FLG_NOENVCONFIG;
17260Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOFLTCONFIG_SIZE) && (strncmp(s1,
17270Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOFLTCONFIG),
17280Sstevel@tonic-gate 		    MSG_LD_NOFLTCONFIG_SIZE) == 0)) {
17290Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
17300Sstevel@tonic-gate 			val = RT_FL2_NOFLTCFG;
17310Sstevel@tonic-gate 			variable = ENV_FLG_NOFLTCONFIG;
17320Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOLAZY_SIZE) && (strncmp(s1,
17330Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOLAZY), MSG_LD_NOLAZY_SIZE) == 0)) {
17340Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
17350Sstevel@tonic-gate 			val = LML_TFLG_NOLAZYLD;
17360Sstevel@tonic-gate 			variable = ENV_FLG_NOLAZY;
17370Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOOBJALTER_SIZE) && (strncmp(s1,
17380Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOOBJALTER),
17390Sstevel@tonic-gate 		    MSG_LD_NOOBJALTER_SIZE) == 0)) {
17400Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17410Sstevel@tonic-gate 			val = RT_FL_NOOBJALT;
17420Sstevel@tonic-gate 			variable = ENV_FLG_NOOBJALTER;
17430Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOVERSION_SIZE) && (strncmp(s1,
17440Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOVERSION), MSG_LD_NOVERSION_SIZE) == 0)) {
17450Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17460Sstevel@tonic-gate 			val = RT_FL_NOVERSION;
17470Sstevel@tonic-gate 			variable = ENV_FLG_NOVERSION;
17484947Srie 		} else if ((len == MSG_LD_NOUNRESWEAK_SIZE) && (strncmp(s1,
17494947Srie 		    MSG_ORIG(MSG_LD_NOUNRESWEAK),
17504947Srie 		    MSG_LD_NOUNRESWEAK_SIZE) == 0)) {
17514947Srie 			/*
17524947Srie 			 * LD_NOUNRESWEAK (internal, used by ldd(1)).
17534947Srie 			 */
17544947Srie 			select |= SEL_ACT_LML;
17554947Srie 			val = LML_FLG_TRC_NOUNRESWEAK;
17564947Srie 			variable = ENV_FLG_NOUNRESWEAK;
17576150Srie 		} else if ((len == MSG_LD_NOPAREXT_SIZE) && (strncmp(s1,
17586150Srie 		    MSG_ORIG(MSG_LD_NOPAREXT), MSG_LD_NOPAREXT_SIZE) == 0)) {
17596150Srie 			select |= SEL_ACT_LML;
17606150Srie 			val = LML_FLG_TRC_NOPAREXT;
17616150Srie 			variable = ENV_FLG_NOPAREXT;
17620Sstevel@tonic-gate 		}
17630Sstevel@tonic-gate 	}
17640Sstevel@tonic-gate 	/*
17650Sstevel@tonic-gate 	 * LD_ORIGIN.
17660Sstevel@tonic-gate 	 */
17670Sstevel@tonic-gate 	else if (*s1 == 'O') {
17680Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
17690Sstevel@tonic-gate 		if ((len == MSG_LD_ORIGIN_SIZE) && (strncmp(s1,
17700Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_ORIGIN), MSG_LD_ORIGIN_SIZE) == 0)) {
17710Sstevel@tonic-gate 			/*
17720Sstevel@tonic-gate 			 * Besides some old patches this is no longer required.
17730Sstevel@tonic-gate 			 */
17740Sstevel@tonic-gate 			rtld_flags |= RT_FL_RELATIVE;
17750Sstevel@tonic-gate 		}
17760Sstevel@tonic-gate #endif
17770Sstevel@tonic-gate 		return;
17780Sstevel@tonic-gate 	}
17790Sstevel@tonic-gate 	/*
17800Sstevel@tonic-gate 	 * LD_PRELOAD and LD_PROFILE family.
17810Sstevel@tonic-gate 	 */
17820Sstevel@tonic-gate 	else if (*s1 == 'P') {
17830Sstevel@tonic-gate 		if ((len == MSG_LD_PRELOAD_SIZE) && (strncmp(s1,
17840Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PRELOAD), MSG_LD_PRELOAD_SIZE) == 0)) {
17850Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1786827Sseizo 			if (select & SEL_REPLACE)
1787827Sseizo 				str = &rpl_preload;
1788827Sseizo 			else  {
1789827Sseizo 				str = &prm_preload;
1790827Sseizo 				rpl_preload = 0;
1791827Sseizo 			}
17920Sstevel@tonic-gate 			variable = ENV_FLG_PRELOAD;
17930Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_SIZE) && (strncmp(s1,
17940Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE), MSG_LD_PROFILE_SIZE) == 0)) {
17950Sstevel@tonic-gate 			/*
17960Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
17970Sstevel@tonic-gate 			 */
17980Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
17990Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE;
18000Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_OUTPUT_SIZE) && (strncmp(s1,
18010Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE_OUTPUT),
18020Sstevel@tonic-gate 		    MSG_LD_PROFILE_OUTPUT_SIZE) == 0)) {
18030Sstevel@tonic-gate 			/*
18040Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
18050Sstevel@tonic-gate 			 */
18060Sstevel@tonic-gate 			select |= SEL_ACT_STR;
18070Sstevel@tonic-gate 			str = &profile_out;
18080Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE_OUTPUT;
18090Sstevel@tonic-gate 		}
18100Sstevel@tonic-gate 	}
18110Sstevel@tonic-gate 	/*
18120Sstevel@tonic-gate 	 * LD_SIGNAL.
18130Sstevel@tonic-gate 	 */
18140Sstevel@tonic-gate 	else if (*s1 == 'S') {
18150Sstevel@tonic-gate 		if (rtld_flags & RT_FL_SECURE)
18160Sstevel@tonic-gate 			return;
18170Sstevel@tonic-gate 		if ((len == MSG_LD_SIGNAL_SIZE) &&
18180Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_SIGNAL),
18190Sstevel@tonic-gate 		    MSG_LD_SIGNAL_SIZE) == 0)) {
18200Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18210Sstevel@tonic-gate 			variable = ENV_FLG_SIGNAL;
18220Sstevel@tonic-gate 		}
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 	/*
18253511Srie 	 * The LD_TRACE family (internal, used by ldd(1)).  This definition is
18263511Srie 	 * the key to enabling all other ldd(1) specific environment variables.
18273511Srie 	 * In case an auditor is called, which in turn might exec(2) a
18283511Srie 	 * subprocess, this variable is disabled, so that any subprocess
18293511Srie 	 * escapes ldd(1) processing.
18300Sstevel@tonic-gate 	 */
18310Sstevel@tonic-gate 	else if (*s1 == 'T') {
18320Sstevel@tonic-gate 		if (((len == MSG_LD_TRACE_OBJS_SIZE) &&
18330Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS),
18340Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_SIZE) == 0)) ||
18350Sstevel@tonic-gate 		    ((len == MSG_LD_TRACE_OBJS_E_SIZE) &&
18360Sstevel@tonic-gate 		    (((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_E),
18370Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_E_SIZE) == 0) && !aout) ||
18380Sstevel@tonic-gate 		    ((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_A),
18390Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_A_SIZE) == 0) && aout)))) {
18403511Srie 			char	*s0 = (char *)s1;
18413511Srie 
18420Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18430Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_OBJS;
18443511Srie 
18453731Srie #if	defined(__sparc) || defined(__x86)
18463511Srie 			/*
18473511Srie 			 * The simplest way to "disable" this variable is to
18483511Srie 			 * truncate this string to "LD_'\0'". This string is
18493511Srie 			 * ignored by any ld.so.1 environment processing.
18503511Srie 			 * Use of such interfaces as unsetenv(3c) are overkill,
18513511Srie 			 * and would drag too much libc implementation detail
18523511Srie 			 * into ld.so.1.
18533511Srie 			 */
18544362Srie 			*s0 = '\0';
18553511Srie #else
18563511Srie /*
18573511Srie  * Verify that the above write is appropriate for any new platforms.
18583511Srie  */
18593511Srie #error	unsupported architecture!
18603511Srie #endif
18610Sstevel@tonic-gate 		} else if ((len == MSG_LD_TRACE_PTHS_SIZE) && (strncmp(s1,
18620Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_TRACE_PTHS),
18630Sstevel@tonic-gate 		    MSG_LD_TRACE_PTHS_SIZE) == 0)) {
18640Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18650Sstevel@tonic-gate 			val = LML_FLG_TRC_SEARCH;
18660Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_PTHS;
18670Sstevel@tonic-gate 		}
18680Sstevel@tonic-gate 	}
18690Sstevel@tonic-gate 	/*
18700Sstevel@tonic-gate 	 * LD_UNREF and LD_UNUSED (internal, used by ldd(1)).
18710Sstevel@tonic-gate 	 */
18720Sstevel@tonic-gate 	else if (*s1 == 'U') {
18730Sstevel@tonic-gate 		if ((len == MSG_LD_UNREF_SIZE) && (strncmp(s1,
18740Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNREF), MSG_LD_UNREF_SIZE) == 0)) {
18750Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18760Sstevel@tonic-gate 			val = LML_FLG_TRC_UNREF;
18770Sstevel@tonic-gate 			variable = ENV_FLG_UNREF;
18780Sstevel@tonic-gate 		} else if ((len == MSG_LD_UNUSED_SIZE) && (strncmp(s1,
18790Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNUSED), MSG_LD_UNUSED_SIZE) == 0)) {
18800Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18810Sstevel@tonic-gate 			val = LML_FLG_TRC_UNUSED;
18820Sstevel@tonic-gate 			variable = ENV_FLG_UNUSED;
18830Sstevel@tonic-gate 		}
18840Sstevel@tonic-gate 	}
18850Sstevel@tonic-gate 	/*
18860Sstevel@tonic-gate 	 * LD_VERBOSE (internal, used by ldd(1)).
18870Sstevel@tonic-gate 	 */
18880Sstevel@tonic-gate 	else if (*s1 == 'V') {
18890Sstevel@tonic-gate 		if ((len == MSG_LD_VERBOSE_SIZE) && (strncmp(s1,
18900Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_VERBOSE), MSG_LD_VERBOSE_SIZE) == 0)) {
18910Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18920Sstevel@tonic-gate 			val = LML_FLG_TRC_VERBOSE;
18930Sstevel@tonic-gate 			variable = ENV_FLG_VERBOSE;
18940Sstevel@tonic-gate 		}
18950Sstevel@tonic-gate 	}
18960Sstevel@tonic-gate 	/*
18970Sstevel@tonic-gate 	 * LD_WARN (internal, used by ldd(1)).
18980Sstevel@tonic-gate 	 */
18990Sstevel@tonic-gate 	else if (*s1 == 'W') {
19000Sstevel@tonic-gate 		if ((len == MSG_LD_WARN_SIZE) && (strncmp(s1,
19010Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_WARN), MSG_LD_WARN_SIZE) == 0)) {
19020Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19030Sstevel@tonic-gate 			val = LML_FLG_TRC_WARN;
19040Sstevel@tonic-gate 			variable = ENV_FLG_WARN;
19050Sstevel@tonic-gate 		}
19060Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
19070Sstevel@tonic-gate 	}
19080Sstevel@tonic-gate 	/*
19090Sstevel@tonic-gate 	 * LD__FIX__ (undocumented, enable future technology that can't be
19100Sstevel@tonic-gate 	 * delivered in a patch release).
19110Sstevel@tonic-gate 	 */
19120Sstevel@tonic-gate 	else if (*s1 == '_') {
19130Sstevel@tonic-gate 		if ((len == MSG_LD_FIX_1_SIZE) && (strncmp(s1,
19140Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FIX_1), MSG_LD_FIX_1_SIZE) == 0)) {
19150Sstevel@tonic-gate 			select |= SEL_ACT_RT;
19160Sstevel@tonic-gate 			val = RT_FL_DISFIX_1;
19170Sstevel@tonic-gate 			variable = ENV_FLG_FIX_1;
19180Sstevel@tonic-gate 		}
19190Sstevel@tonic-gate #endif
19200Sstevel@tonic-gate 	}
19210Sstevel@tonic-gate 	if (variable == 0)
19220Sstevel@tonic-gate 		return;
19230Sstevel@tonic-gate 
19240Sstevel@tonic-gate 	/*
19250Sstevel@tonic-gate 	 * If the variable is already processed with ISA specific variable,
19260Sstevel@tonic-gate 	 * no further processing needed.
19270Sstevel@tonic-gate 	 */
19280Sstevel@tonic-gate 	if (((select & SEL_REPLACE) && (rplisa & variable)) ||
19290Sstevel@tonic-gate 	    ((select & SEL_PERMANT) && (prmisa & variable)))
19300Sstevel@tonic-gate 		return;
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate 	/*
1933827Sseizo 	 * Now mark the appropriate variables.
1934827Sseizo 	 * If the replaceable variable is already set, then the
1935827Sseizo 	 * process environment variable must be set. Any replaceable
1936827Sseizo 	 * variable specified in a configuration file can be ignored.
19370Sstevel@tonic-gate 	 */
19380Sstevel@tonic-gate 	if (env_flags & ENV_TYP_ISA) {
19390Sstevel@tonic-gate 		/*
19400Sstevel@tonic-gate 		 * This is ISA setting. We do the setting
19410Sstevel@tonic-gate 		 * even if s2 is NULL.
19420Sstevel@tonic-gate 		 * If s2 is NULL, we might need to undo
19430Sstevel@tonic-gate 		 * the setting.
19440Sstevel@tonic-gate 		 */
19450Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1946827Sseizo 			if (rplisa & variable)
1947827Sseizo 				return;
19480Sstevel@tonic-gate 			rplisa |= variable;
19490Sstevel@tonic-gate 		} else {
19500Sstevel@tonic-gate 			prmisa |= variable;
19510Sstevel@tonic-gate 		}
19520Sstevel@tonic-gate 	} else if (s2) {
19530Sstevel@tonic-gate 		/*
19540Sstevel@tonic-gate 		 * This is non0-ISA setting
19550Sstevel@tonic-gate 		 */
19560Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1957827Sseizo 			if (rplgen & variable)
1958827Sseizo 				return;
19590Sstevel@tonic-gate 			rplgen |= variable;
19600Sstevel@tonic-gate 		} else
19610Sstevel@tonic-gate 			prmgen |= variable;
19620Sstevel@tonic-gate 	} else
19630Sstevel@tonic-gate 		/*
19640Sstevel@tonic-gate 		 * This is non-ISA setting which
19650Sstevel@tonic-gate 		 * can be ignored.
19660Sstevel@tonic-gate 		 */
19670Sstevel@tonic-gate 		return;
19680Sstevel@tonic-gate 
19690Sstevel@tonic-gate 	/*
19700Sstevel@tonic-gate 	 * Now perform the setting.
19710Sstevel@tonic-gate 	 */
19720Sstevel@tonic-gate 	if (select & SEL_ACT_RT) {
19730Sstevel@tonic-gate 		if (s2)
19740Sstevel@tonic-gate 			rtld_flags |= val;
19750Sstevel@tonic-gate 		else
19760Sstevel@tonic-gate 			rtld_flags &= ~val;
19770Sstevel@tonic-gate 	} else if (select & SEL_ACT_RT2) {
19780Sstevel@tonic-gate 		if (s2)
19790Sstevel@tonic-gate 			rtld_flags2 |= val;
19800Sstevel@tonic-gate 		else
19810Sstevel@tonic-gate 			rtld_flags2 &= ~val;
19820Sstevel@tonic-gate 	} else if (select & SEL_ACT_STR)
19830Sstevel@tonic-gate 		*str = s2;
19840Sstevel@tonic-gate 	else if (select & SEL_ACT_LML) {
19850Sstevel@tonic-gate 		if (s2)
19860Sstevel@tonic-gate 			*lmflags |= val;
19870Sstevel@tonic-gate 		else
19880Sstevel@tonic-gate 			*lmflags &= ~val;
19890Sstevel@tonic-gate 	} else if (select & SEL_ACT_LMLT) {
19900Sstevel@tonic-gate 		if (s2)
19910Sstevel@tonic-gate 			*lmtflags |= val;
19920Sstevel@tonic-gate 		else
19930Sstevel@tonic-gate 			*lmtflags &= ~val;
19940Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_1) {
19950Sstevel@tonic-gate 		/*
19960Sstevel@tonic-gate 		 * variable is either ENV_FLG_FLAGS or ENV_FLG_LIBPATH
19970Sstevel@tonic-gate 		 */
19980Sstevel@tonic-gate 		*str = s2;
19990Sstevel@tonic-gate 		if ((select & SEL_REPLACE) && (env_flags & ENV_TYP_CONFIG)) {
20000Sstevel@tonic-gate 			if (s2) {
20010Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
20020Sstevel@tonic-gate 					env_info |= ENV_INF_FLAGCFG;
20030Sstevel@tonic-gate 				else
20040Sstevel@tonic-gate 					env_info |= ENV_INF_PATHCFG;
20050Sstevel@tonic-gate 			} else {
20060Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
20070Sstevel@tonic-gate 					env_info &= ~ENV_INF_FLAGCFG;
20080Sstevel@tonic-gate 				else
20090Sstevel@tonic-gate 					env_info &= ~ENV_INF_PATHCFG;
20100Sstevel@tonic-gate 			}
20110Sstevel@tonic-gate 		}
20120Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_2) {
20130Sstevel@tonic-gate 		/*
20140Sstevel@tonic-gate 		 * variables can be: ENV_FLG_
20150Sstevel@tonic-gate 		 * 	AUDIT_ARGS, BINDING, CONCURRENCY, CONFGEN,
20160Sstevel@tonic-gate 		 *	LOADFLTR, PROFILE, SIGNAL, TRACE_OBJS
20170Sstevel@tonic-gate 		 */
20180Sstevel@tonic-gate 		if (variable == ENV_FLG_AUDIT_ARGS) {
20190Sstevel@tonic-gate 			if (s2) {
20200Sstevel@tonic-gate 				audit_argcnt = atoi(s2);
20210Sstevel@tonic-gate 				audit_argcnt += audit_argcnt % 2;
20220Sstevel@tonic-gate 			} else
20230Sstevel@tonic-gate 				audit_argcnt = 0;
20240Sstevel@tonic-gate 		} else if (variable == ENV_FLG_BINDINGS) {
20250Sstevel@tonic-gate 			if (s2)
20260Sstevel@tonic-gate 				rpl_debug = MSG_ORIG(MSG_TKN_BINDINGS);
20270Sstevel@tonic-gate 			else
20280Sstevel@tonic-gate 				rpl_debug = 0;
20290Sstevel@tonic-gate 		} else if (variable == ENV_FLG_CONCURRENCY) {
20300Sstevel@tonic-gate 			if (s2)
20310Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_NOCONCUR;
20320Sstevel@tonic-gate 			else
20330Sstevel@tonic-gate 				rtld_flags |= RT_FL_NOCONCUR;
20340Sstevel@tonic-gate 		} else if (variable == ENV_FLG_CONFGEN) {
20350Sstevel@tonic-gate 			if (s2) {
20360Sstevel@tonic-gate 				rtld_flags |= RT_FL_CONFGEN;
20370Sstevel@tonic-gate 				*lmflags |= LML_FLG_IGNRELERR;
20380Sstevel@tonic-gate 			} else {
20390Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_CONFGEN;
20400Sstevel@tonic-gate 				*lmflags &= ~LML_FLG_IGNRELERR;
20410Sstevel@tonic-gate 			}
20420Sstevel@tonic-gate 		} else if (variable == ENV_FLG_LOADFLTR) {
20430Sstevel@tonic-gate 			if (s2) {
20440Sstevel@tonic-gate 				*lmtflags |= LML_TFLG_LOADFLTR;
20450Sstevel@tonic-gate 				if (*s2 == '2')
20460Sstevel@tonic-gate 					rtld_flags |= RT_FL_WARNFLTR;
20470Sstevel@tonic-gate 			} else {
20480Sstevel@tonic-gate 				*lmtflags &= ~LML_TFLG_LOADFLTR;
20490Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_WARNFLTR;
20500Sstevel@tonic-gate 			}
20510Sstevel@tonic-gate 		} else if (variable == ENV_FLG_PROFILE) {
20520Sstevel@tonic-gate 			profile_name = s2;
20530Sstevel@tonic-gate 			if (s2) {
20540Sstevel@tonic-gate 				if (strcmp(s2, MSG_ORIG(MSG_FIL_RTLD)) == 0) {
20550Sstevel@tonic-gate 					return;
20560Sstevel@tonic-gate 				}
20574362Srie 				/* BEGIN CSTYLED */
20580Sstevel@tonic-gate 				if (rtld_flags & RT_FL_SECURE) {
20590Sstevel@tonic-gate 					profile_lib =
20600Sstevel@tonic-gate #if	defined(_ELF64)
20610Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE_64);
20620Sstevel@tonic-gate #else
20630Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE);
20640Sstevel@tonic-gate #endif
20650Sstevel@tonic-gate 				} else {
20660Sstevel@tonic-gate 					profile_lib =
20670Sstevel@tonic-gate #if	defined(_ELF64)
20680Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF_64);
20690Sstevel@tonic-gate #else
20700Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF);
20710Sstevel@tonic-gate #endif
20720Sstevel@tonic-gate 				}
20734362Srie 				/* END CSTYLED */
20740Sstevel@tonic-gate 			} else
20750Sstevel@tonic-gate 				profile_lib = 0;
20760Sstevel@tonic-gate 		} else if (variable == ENV_FLG_SIGNAL) {
20770Sstevel@tonic-gate 			killsig = s2 ? atoi(s2) : SIGKILL;
20780Sstevel@tonic-gate 		} else if (variable == ENV_FLG_TRACE_OBJS) {
20790Sstevel@tonic-gate 			if (s2) {
20800Sstevel@tonic-gate 				*lmflags |= LML_FLG_TRC_ENABLE;
20810Sstevel@tonic-gate 				if (*s2 == '2')
20820Sstevel@tonic-gate 					*lmflags |= LML_FLG_TRC_LDDSTUB;
20830Sstevel@tonic-gate 			} else
20840Sstevel@tonic-gate 				*lmflags &=
20854362Srie 				    ~(LML_FLG_TRC_ENABLE|LML_FLG_TRC_LDDSTUB);
20860Sstevel@tonic-gate 		}
20870Sstevel@tonic-gate 	}
20880Sstevel@tonic-gate }
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate /*
20910Sstevel@tonic-gate  * Determine whether we have an architecture specific environment variable.
20920Sstevel@tonic-gate  * If we do, and we're the wrong architecture, it'll just get ignored.
20930Sstevel@tonic-gate  * Otherwise the variable is processed in it's architecture neutral form.
20940Sstevel@tonic-gate  */
20950Sstevel@tonic-gate static int
20960Sstevel@tonic-gate ld_arch_env(const char *s1, size_t *len)
20970Sstevel@tonic-gate {
20980Sstevel@tonic-gate 	size_t	_len = *len - 3;
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 	if (s1[_len++] == '_') {
21010Sstevel@tonic-gate 		if ((s1[_len] == '3') && (s1[_len + 1] == '2')) {
21020Sstevel@tonic-gate #if	defined(_ELF64)
21030Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
21040Sstevel@tonic-gate #else
21050Sstevel@tonic-gate 			*len = *len - 3;
21060Sstevel@tonic-gate 			return (ENV_TYP_ISA);
21070Sstevel@tonic-gate #endif
21080Sstevel@tonic-gate 		}
21090Sstevel@tonic-gate 		if ((s1[_len] == '6') && (s1[_len + 1] == '4')) {
21100Sstevel@tonic-gate #if	defined(_ELF64)
21110Sstevel@tonic-gate 			*len = *len - 3;
21120Sstevel@tonic-gate 			return (ENV_TYP_ISA);
21130Sstevel@tonic-gate #else
21140Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
21150Sstevel@tonic-gate #endif
21160Sstevel@tonic-gate 		}
21170Sstevel@tonic-gate 	}
21180Sstevel@tonic-gate 	return (0);
21190Sstevel@tonic-gate }
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate /*
21230Sstevel@tonic-gate  * Process an LD_FLAGS environment variable.  The value can be a comma
21240Sstevel@tonic-gate  * separated set of tokens, which are sent (in upper case) into the generic
21250Sstevel@tonic-gate  * LD_XXXX environment variable engine.  For example:
21260Sstevel@tonic-gate  *
21270Sstevel@tonic-gate  *	LD_FLAGS=bind_now		->	LD_BIND_NOW=1
21280Sstevel@tonic-gate  *	LD_FLAGS=library_path=/foo:.	->	LD_LIBRARY_PATH=/foo:.
21290Sstevel@tonic-gate  *	LD_FLAGS=debug=files:detail	->	LD_DEBUG=files:detail
21300Sstevel@tonic-gate  * or
21310Sstevel@tonic-gate  *	LD_FLAGS=bind_now,library_path=/foo:.,debug=files:detail
21320Sstevel@tonic-gate  */
21330Sstevel@tonic-gate static int
21340Sstevel@tonic-gate ld_flags_env(const char *str, Word *lmflags, Word *lmtflags,
21350Sstevel@tonic-gate     uint_t env_flags, int aout)
21360Sstevel@tonic-gate {
21370Sstevel@tonic-gate 	char	*nstr, *sstr, *estr = 0;
21380Sstevel@tonic-gate 	size_t	nlen, len;
21390Sstevel@tonic-gate 
21400Sstevel@tonic-gate 	if (str == 0)
21410Sstevel@tonic-gate 		return (0);
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 	/*
21440Sstevel@tonic-gate 	 * Create a new string as we're going to transform the token(s) into
21450Sstevel@tonic-gate 	 * uppercase and separate tokens with nulls.
21460Sstevel@tonic-gate 	 */
21470Sstevel@tonic-gate 	len = strlen(str);
21480Sstevel@tonic-gate 	if ((nstr = malloc(len + 1)) == 0)
21490Sstevel@tonic-gate 		return (1);
21500Sstevel@tonic-gate 	(void) strcpy(nstr, str);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 	for (sstr = nstr; sstr; sstr++, len--) {
21530Sstevel@tonic-gate 		int	flags;
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 		if ((*sstr != '\0') && (*sstr != ',')) {
21560Sstevel@tonic-gate 			if (estr == 0) {
21570Sstevel@tonic-gate 				if (*sstr == '=')
21580Sstevel@tonic-gate 					estr = sstr;
21590Sstevel@tonic-gate 				else {
21600Sstevel@tonic-gate 					/*
21610Sstevel@tonic-gate 					 * Translate token to uppercase.  Don't
21620Sstevel@tonic-gate 					 * use toupper(3C) as including this
21630Sstevel@tonic-gate 					 * code doubles the size of ld.so.1.
21640Sstevel@tonic-gate 					 */
21650Sstevel@tonic-gate 					if ((*sstr >= 'a') && (*sstr <= 'z'))
21660Sstevel@tonic-gate 						*sstr = *sstr - ('a' - 'A');
21670Sstevel@tonic-gate 				}
21680Sstevel@tonic-gate 			}
21690Sstevel@tonic-gate 			continue;
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 
21720Sstevel@tonic-gate 		*sstr = '\0';
21730Sstevel@tonic-gate 		if (estr) {
21740Sstevel@tonic-gate 			nlen = estr - nstr;
21750Sstevel@tonic-gate 			if ((*++estr == '\0') || (*estr == ','))
21760Sstevel@tonic-gate 				estr = 0;
21770Sstevel@tonic-gate 		} else
21780Sstevel@tonic-gate 			nlen = sstr - nstr;
21790Sstevel@tonic-gate 
21800Sstevel@tonic-gate 		/*
21810Sstevel@tonic-gate 		 * Fabricate a boolean definition for any unqualified variable.
21820Sstevel@tonic-gate 		 * Thus LD_FLAGS=bind_now is represented as BIND_NOW=(null).
21830Sstevel@tonic-gate 		 * The value is sufficient to assert any boolean variables, plus
21840Sstevel@tonic-gate 		 * the term "(null)" is specifically chosen in case someone
21850Sstevel@tonic-gate 		 * mistakenly supplies something like LD_FLAGS=library_path.
21860Sstevel@tonic-gate 		 */
21870Sstevel@tonic-gate 		if (estr == 0)
21880Sstevel@tonic-gate 			estr = (char *)MSG_INTL(MSG_STR_NULL);
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 		/*
21910Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
21920Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
21930Sstevel@tonic-gate 		 * neutral portion of the string.
21940Sstevel@tonic-gate 		 */
21950Sstevel@tonic-gate 		if ((flags = ld_arch_env(nstr, &nlen)) != ENV_TYP_IGNORE) {
21960Sstevel@tonic-gate 			ld_generic_env(nstr, nlen, estr, lmflags,
21970Sstevel@tonic-gate 			    lmtflags, (env_flags | flags), aout);
21980Sstevel@tonic-gate 		}
21990Sstevel@tonic-gate 		if (len == 0)
22000Sstevel@tonic-gate 			return (0);
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 		nstr = sstr + 1;
22030Sstevel@tonic-gate 		estr = 0;
22040Sstevel@tonic-gate 	}
22050Sstevel@tonic-gate 	return (0);
22060Sstevel@tonic-gate }
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate /*
22100Sstevel@tonic-gate  * Process a single environment string.  Only strings starting with `LD_' are
22110Sstevel@tonic-gate  * reserved for our use.  By convention, all strings should be of the form
22120Sstevel@tonic-gate  * `LD_XXXX=', if the string is followed by a non-null value the appropriate
22130Sstevel@tonic-gate  * functionality is enabled.  Also pick off applicable locale variables.
22140Sstevel@tonic-gate  */
22150Sstevel@tonic-gate #define	LOC_LANG	1
22160Sstevel@tonic-gate #define	LOC_MESG	2
22170Sstevel@tonic-gate #define	LOC_ALL		3
22180Sstevel@tonic-gate 
22190Sstevel@tonic-gate static void
22200Sstevel@tonic-gate ld_str_env(const char *s1, Word *lmflags, Word *lmtflags, uint_t env_flags,
22210Sstevel@tonic-gate     int aout)
22220Sstevel@tonic-gate {
22230Sstevel@tonic-gate 	const char	*s2;
22241824Srie 	static		size_t	loc = 0;
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 	if (*s1++ != 'L')
22270Sstevel@tonic-gate 		return;
22280Sstevel@tonic-gate 
22290Sstevel@tonic-gate 	/*
22300Sstevel@tonic-gate 	 * See if we have any locale environment settings.  These environment
22310Sstevel@tonic-gate 	 * variables have a precedence, LC_ALL is higher than LC_MESSAGES which
22320Sstevel@tonic-gate 	 * is higher than LANG.
22330Sstevel@tonic-gate 	 */
22340Sstevel@tonic-gate 	s2 = s1;
22350Sstevel@tonic-gate 	if ((*s2++ == 'C') && (*s2++ == '_') && (*s2 != '\0')) {
22360Sstevel@tonic-gate 		if (strncmp(s2, MSG_ORIG(MSG_LC_ALL), MSG_LC_ALL_SIZE) == 0) {
22370Sstevel@tonic-gate 			s2 += MSG_LC_ALL_SIZE;
22380Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_ALL)) {
22391824Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22400Sstevel@tonic-gate 				loc = LOC_ALL;
22410Sstevel@tonic-gate 			}
22420Sstevel@tonic-gate 		} else if (strncmp(s2, MSG_ORIG(MSG_LC_MESSAGES),
22430Sstevel@tonic-gate 		    MSG_LC_MESSAGES_SIZE) == 0) {
22440Sstevel@tonic-gate 			s2 += MSG_LC_MESSAGES_SIZE;
22450Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_MESG)) {
22461824Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22470Sstevel@tonic-gate 				loc = LOC_MESG;
22480Sstevel@tonic-gate 			}
22490Sstevel@tonic-gate 		}
22500Sstevel@tonic-gate 		return;
22510Sstevel@tonic-gate 	}
22520Sstevel@tonic-gate 
22530Sstevel@tonic-gate 	s2 = s1;
22540Sstevel@tonic-gate 	if ((*s2++ == 'A') && (*s2++ == 'N') && (*s2++ == 'G') &&
22550Sstevel@tonic-gate 	    (*s2++ == '=') && (*s2 != '\0') && (loc < LOC_LANG)) {
22561824Srie 		glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22570Sstevel@tonic-gate 		loc = LOC_LANG;
22580Sstevel@tonic-gate 		return;
22590Sstevel@tonic-gate 	}
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 	/*
22620Sstevel@tonic-gate 	 * Pick off any LD_XXXX environment variables.
22630Sstevel@tonic-gate 	 */
22640Sstevel@tonic-gate 	if ((*s1++ == 'D') && (*s1++ == '_') && (*s1 != '\0')) {
22650Sstevel@tonic-gate 		size_t	len;
22660Sstevel@tonic-gate 		int	flags;
22670Sstevel@tonic-gate 
22680Sstevel@tonic-gate 		/*
22692712Snn35248 		 * In a branded process we must ignore all LD_XXXX env vars
22702712Snn35248 		 * because they are intended for the brand's linker.
22712712Snn35248 		 * To affect the Solaris linker, use LD_BRAND_XXXX instead.
22722712Snn35248 		 */
22732712Snn35248 		if (rtld_flags2 & RT_FL2_BRANDED) {
22742712Snn35248 			if (strncmp(s1, MSG_ORIG(MSG_LD_BRAND_PREFIX),
22752712Snn35248 			    MSG_LD_BRAND_PREFIX_SIZE) != 0)
22762712Snn35248 				return;
22772712Snn35248 			s1 += MSG_LD_BRAND_PREFIX_SIZE;
22782712Snn35248 		}
22792712Snn35248 
22802712Snn35248 		/*
22810Sstevel@tonic-gate 		 * Environment variables with no value (ie. LD_XXXX=) typically
22820Sstevel@tonic-gate 		 * have no impact, however if environment variables are defined
22830Sstevel@tonic-gate 		 * within a configuration file, these null user settings can be
22840Sstevel@tonic-gate 		 * used to disable any configuration replaceable definitions.
22850Sstevel@tonic-gate 		 */
22860Sstevel@tonic-gate 		if ((s2 = strchr(s1, '=')) == 0) {
22870Sstevel@tonic-gate 			len = strlen(s1);
22880Sstevel@tonic-gate 			s2 = 0;
22890Sstevel@tonic-gate 		} else if (*++s2 == '\0') {
22900Sstevel@tonic-gate 			len = strlen(s1) - 1;
22910Sstevel@tonic-gate 			s2 = 0;
22920Sstevel@tonic-gate 		} else {
22930Sstevel@tonic-gate 			len = s2 - s1 - 1;
22940Sstevel@tonic-gate 			while (isspace(*s2))
22950Sstevel@tonic-gate 				s2++;
22960Sstevel@tonic-gate 		}
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 		/*
22990Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
23000Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
23010Sstevel@tonic-gate 		 * neutral portion of the string.
23020Sstevel@tonic-gate 		 */
23030Sstevel@tonic-gate 		if ((flags = ld_arch_env(s1, &len)) == ENV_TYP_IGNORE)
23040Sstevel@tonic-gate 			return;
23050Sstevel@tonic-gate 		env_flags |= flags;
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 		ld_generic_env(s1, len, s2, lmflags, lmtflags, env_flags, aout);
23080Sstevel@tonic-gate 	}
23090Sstevel@tonic-gate }
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate /*
23120Sstevel@tonic-gate  * Internal getenv routine.  Called immediately after ld.so.1 initializes
23130Sstevel@tonic-gate  * itself.
23140Sstevel@tonic-gate  */
23150Sstevel@tonic-gate int
23160Sstevel@tonic-gate readenv_user(const char ** envp, Word *lmflags, Word *lmtflags, int aout)
23170Sstevel@tonic-gate {
23181824Srie 	char	*locale;
23191824Srie 
23200Sstevel@tonic-gate 	if (envp == (const char **)0)
23210Sstevel@tonic-gate 		return (0);
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 	while (*envp != (const char *)0)
23240Sstevel@tonic-gate 		ld_str_env(*envp++, lmflags, lmtflags, 0, aout);
23250Sstevel@tonic-gate 
23260Sstevel@tonic-gate 	/*
23270Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
23280Sstevel@tonic-gate 	 * these strings.
23290Sstevel@tonic-gate 	 */
23300Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
23310Sstevel@tonic-gate 		return (1);
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 	/*
23340Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
23350Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
23360Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
23370Sstevel@tonic-gate 	 */
23380Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
23390Sstevel@tonic-gate 		rpl_audit = profile_lib = profile_name = 0;
23400Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
23410Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 	/*
2344280Srie 	 * If both LD_BIND_NOW and LD_BIND_LAZY are specified, the former wins.
2345280Srie 	 */
2346280Srie 	if ((rtld_flags2 & (RT_FL2_BINDNOW | RT_FL2_BINDLAZY)) ==
2347280Srie 	    (RT_FL2_BINDNOW | RT_FL2_BINDLAZY))
2348280Srie 		rtld_flags2 &= ~RT_FL2_BINDLAZY;
2349280Srie 
2350280Srie 	/*
23516150Srie 	 * When using ldd(1) -r or -d against an executable, assert -p.
23526150Srie 	 */
23536150Srie 	if ((*lmflags &
23546150Srie 	    (LML_FLG_TRC_WARN | LML_FLG_TRC_LDDSTUB)) == LML_FLG_TRC_WARN)
23556150Srie 		*lmflags |= LML_FLG_TRC_NOPAREXT;
23566150Srie 
23576150Srie 	/*
23580Sstevel@tonic-gate 	 * If we have a locale setting make sure its worth processing further.
23593191Srie 	 * C and POSIX locales don't need any processing.  In addition, to
23603191Srie 	 * ensure no one escapes the /usr/lib/locale hierarchy, don't allow
23613191Srie 	 * the locale to contain a segment that leads upward in the file system
23623191Srie 	 * hierarchy (i.e. no '..' segments).   Given that we'll be confined to
23633191Srie 	 * the /usr/lib/locale hierarchy, there is no need to extensively
23643191Srie 	 * validate the mode or ownership of any message file (as libc's
23653191Srie 	 * generic handling of message files does).  Duplicate the string so
23663191Srie 	 * that new locale setting can generically cleanup any previous locales.
23670Sstevel@tonic-gate 	 */
23681824Srie 	if ((locale = glcs[CI_LCMESSAGES].lc_un.lc_ptr) != 0) {
23690Sstevel@tonic-gate 		if (((*locale == 'C') && (*(locale + 1) == '\0')) ||
23703191Srie 		    (strcmp(locale, MSG_ORIG(MSG_TKN_POSIX)) == 0) ||
23713191Srie 		    (strstr(locale, MSG_ORIG(MSG_TKN_DOTDOT)) != NULL))
23721824Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = 0;
23730Sstevel@tonic-gate 		else
23741824Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = strdup(locale);
23750Sstevel@tonic-gate 	}
23760Sstevel@tonic-gate 	return (0);
23770Sstevel@tonic-gate }
23780Sstevel@tonic-gate 
23790Sstevel@tonic-gate /*
23800Sstevel@tonic-gate  * Configuration environment processing.  Called after the a.out has been
23810Sstevel@tonic-gate  * processed (as the a.out can specify its own configuration file).
23820Sstevel@tonic-gate  */
23830Sstevel@tonic-gate int
23840Sstevel@tonic-gate readenv_config(Rtc_env * envtbl, Addr addr, int aout)
23850Sstevel@tonic-gate {
2386*6387Srie 	Word	*lmflags = &(lml_main.lm_flags);
2387*6387Srie 	Word	*lmtflags = &(lml_main.lm_tflags);
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 	if (envtbl == (Rtc_env *)0)
23900Sstevel@tonic-gate 		return (0);
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 	while (envtbl->env_str) {
23930Sstevel@tonic-gate 		uint_t	env_flags = ENV_TYP_CONFIG;
23940Sstevel@tonic-gate 
23950Sstevel@tonic-gate 		if (envtbl->env_flags & RTC_ENV_PERMANT)
23960Sstevel@tonic-gate 			env_flags |= ENV_TYP_PERMANT;
23970Sstevel@tonic-gate 
23980Sstevel@tonic-gate 		ld_str_env((const char *)(envtbl->env_str + addr),
23990Sstevel@tonic-gate 		    lmflags, lmtflags, env_flags, 0);
24000Sstevel@tonic-gate 		envtbl++;
24010Sstevel@tonic-gate 	}
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	/*
24040Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
24050Sstevel@tonic-gate 	 * these strings.
24060Sstevel@tonic-gate 	 */
24070Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
24080Sstevel@tonic-gate 		return (1);
24090Sstevel@tonic-gate 	if (ld_flags_env(prm_ldflags, lmflags, lmtflags, ENV_TYP_CONFIG,
24100Sstevel@tonic-gate 	    aout) == 1)
24110Sstevel@tonic-gate 		return (1);
24120Sstevel@tonic-gate 
24130Sstevel@tonic-gate 	/*
24140Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
24150Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
24160Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
24170Sstevel@tonic-gate 	 */
24180Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
24190Sstevel@tonic-gate 		prm_audit = profile_lib = profile_name = 0;
24200Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
24210Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
24220Sstevel@tonic-gate 
24230Sstevel@tonic-gate 	return (0);
24240Sstevel@tonic-gate }
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate int
24270Sstevel@tonic-gate dowrite(Prfbuf * prf)
24280Sstevel@tonic-gate {
24290Sstevel@tonic-gate 	/*
24300Sstevel@tonic-gate 	 * We do not have a valid file descriptor, so we are unable
24310Sstevel@tonic-gate 	 * to flush the buffer.
24320Sstevel@tonic-gate 	 */
24330Sstevel@tonic-gate 	if (prf->pr_fd == -1)
24340Sstevel@tonic-gate 		return (0);
24350Sstevel@tonic-gate 	(void) write(prf->pr_fd, prf->pr_buf, prf->pr_cur - prf->pr_buf);
24360Sstevel@tonic-gate 	prf->pr_cur = prf->pr_buf;
24370Sstevel@tonic-gate 	return (1);
24380Sstevel@tonic-gate }
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate /*
24410Sstevel@tonic-gate  * Simplified printing.  The following conversion specifications are supported:
24420Sstevel@tonic-gate  *
24430Sstevel@tonic-gate  *	% [#] [-] [min field width] [. precision] s|d|x|c
24440Sstevel@tonic-gate  *
24450Sstevel@tonic-gate  *
24460Sstevel@tonic-gate  * dorprf takes the output buffer in the form of Prfbuf which permits
24470Sstevel@tonic-gate  * the verification of the output buffer size and the concatenation
24480Sstevel@tonic-gate  * of data to an already existing output buffer.  The Prfbuf
24490Sstevel@tonic-gate  * structure contains the following:
24500Sstevel@tonic-gate  *
24510Sstevel@tonic-gate  *  pr_buf	pointer to the beginning of the output buffer.
24520Sstevel@tonic-gate  *  pr_cur	pointer to the next available byte in the output buffer.  By
24530Sstevel@tonic-gate  *		setting pr_cur ahead of pr_buf you can append to an already
24540Sstevel@tonic-gate  *		existing buffer.
24550Sstevel@tonic-gate  *  pr_len	the size of the output buffer.  By setting pr_len to '0' you
24560Sstevel@tonic-gate  *		disable protection from overflows in the output buffer.
24570Sstevel@tonic-gate  *  pr_fd	a pointer to the file-descriptor the buffer will eventually be
24580Sstevel@tonic-gate  *		output to.  If pr_fd is set to '-1' then it's assumed there is
24593191Srie  *		no output buffer, and doprf() will return with an error to
24603191Srie  *		indicate an output buffer overflow.  If pr_fd is > -1 then when
24613191Srie  *		the output buffer is filled it will be flushed to pr_fd and will
24623191Srie  *		then be	available for additional data.
24630Sstevel@tonic-gate  */
24640Sstevel@tonic-gate #define	FLG_UT_MINUS	0x0001	/* - */
24650Sstevel@tonic-gate #define	FLG_UT_SHARP	0x0002	/* # */
24660Sstevel@tonic-gate #define	FLG_UT_DOTSEEN	0x0008	/* dot appeared in format spec */
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate /*
24691824Srie  * This macro is for use from within doprf only.  It is to be used for checking
24701824Srie  * the output buffer size and placing characters into the buffer.
24710Sstevel@tonic-gate  */
24720Sstevel@tonic-gate #define	PUTC(c) \
24730Sstevel@tonic-gate 	{ \
24741824Srie 		char tmpc; \
24750Sstevel@tonic-gate 		\
24760Sstevel@tonic-gate 		tmpc = (c); \
24771824Srie 		if (bufsiz && (bp >= bufend)) { \
24780Sstevel@tonic-gate 			prf->pr_cur = bp; \
24790Sstevel@tonic-gate 			if (dowrite(prf) == 0) \
24800Sstevel@tonic-gate 				return (0); \
24810Sstevel@tonic-gate 			bp = prf->pr_cur; \
24820Sstevel@tonic-gate 		} \
24830Sstevel@tonic-gate 		*bp++ = tmpc; \
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 
24863191Srie /*
24873191Srie  * Define a local buffer size for building a numeric value - large enough to
24883191Srie  * hold a 64-bit value.
24893191Srie  */
24903304Srie #define	NUM_SIZE	22
24913191Srie 
24920Sstevel@tonic-gate size_t
24930Sstevel@tonic-gate doprf(const char *format, va_list args, Prfbuf *prf)
24940Sstevel@tonic-gate {
24950Sstevel@tonic-gate 	char	c;
24960Sstevel@tonic-gate 	char	*bp = prf->pr_cur;
24970Sstevel@tonic-gate 	char	*bufend = prf->pr_buf + prf->pr_len;
24980Sstevel@tonic-gate 	size_t	bufsiz = prf->pr_len;
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	while ((c = *format++) != '\0') {
25010Sstevel@tonic-gate 		if (c != '%') {
25020Sstevel@tonic-gate 			PUTC(c);
25030Sstevel@tonic-gate 		} else {
25040Sstevel@tonic-gate 			int	base = 0, flag = 0, width = 0, prec = 0;
25050Sstevel@tonic-gate 			size_t	_i;
25060Sstevel@tonic-gate 			int	_c, _n;
25070Sstevel@tonic-gate 			char	*_s;
25080Sstevel@tonic-gate 			int	ls = 0;
25090Sstevel@tonic-gate again:
25100Sstevel@tonic-gate 			c = *format++;
25110Sstevel@tonic-gate 			switch (c) {
25120Sstevel@tonic-gate 			case '-':
25130Sstevel@tonic-gate 				flag |= FLG_UT_MINUS;
25140Sstevel@tonic-gate 				goto again;
25150Sstevel@tonic-gate 			case '#':
25160Sstevel@tonic-gate 				flag |= FLG_UT_SHARP;
25170Sstevel@tonic-gate 				goto again;
25180Sstevel@tonic-gate 			case '.':
25190Sstevel@tonic-gate 				flag |= FLG_UT_DOTSEEN;
25200Sstevel@tonic-gate 				goto again;
25210Sstevel@tonic-gate 			case '0':
25220Sstevel@tonic-gate 			case '1':
25230Sstevel@tonic-gate 			case '2':
25240Sstevel@tonic-gate 			case '3':
25250Sstevel@tonic-gate 			case '4':
25260Sstevel@tonic-gate 			case '5':
25270Sstevel@tonic-gate 			case '6':
25280Sstevel@tonic-gate 			case '7':
25290Sstevel@tonic-gate 			case '8':
25300Sstevel@tonic-gate 			case '9':
25310Sstevel@tonic-gate 				if (flag & FLG_UT_DOTSEEN)
25320Sstevel@tonic-gate 					prec = (prec * 10) + c - '0';
25330Sstevel@tonic-gate 				else
25340Sstevel@tonic-gate 					width = (width * 10) + c - '0';
25350Sstevel@tonic-gate 				goto again;
25360Sstevel@tonic-gate 			case 'x':
25370Sstevel@tonic-gate 			case 'X':
25380Sstevel@tonic-gate 				base = 16;
25390Sstevel@tonic-gate 				break;
25400Sstevel@tonic-gate 			case 'd':
25410Sstevel@tonic-gate 			case 'D':
25420Sstevel@tonic-gate 			case 'u':
25430Sstevel@tonic-gate 				base = 10;
25440Sstevel@tonic-gate 				flag &= ~FLG_UT_SHARP;
25450Sstevel@tonic-gate 				break;
25460Sstevel@tonic-gate 			case 'l':
25470Sstevel@tonic-gate 				base = 10;
25480Sstevel@tonic-gate 				ls++; /* number of l's (long or long long) */
25490Sstevel@tonic-gate 				if ((*format == 'l') ||
25500Sstevel@tonic-gate 				    (*format == 'd') || (*format == 'D') ||
25510Sstevel@tonic-gate 				    (*format == 'x') || (*format == 'X') ||
25520Sstevel@tonic-gate 				    (*format == 'o') || (*format == 'O'))
25530Sstevel@tonic-gate 					goto again;
25540Sstevel@tonic-gate 				break;
25550Sstevel@tonic-gate 			case 'o':
25560Sstevel@tonic-gate 			case 'O':
25570Sstevel@tonic-gate 				base = 8;
25580Sstevel@tonic-gate 				break;
25590Sstevel@tonic-gate 			case 'c':
25600Sstevel@tonic-gate 				_c = va_arg(args, int);
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 				for (_i = 24; _i > 0; _i -= 8) {
25630Sstevel@tonic-gate 					if ((c = ((_c >> _i) & 0x7f)) != 0) {
25640Sstevel@tonic-gate 						PUTC(c);
25650Sstevel@tonic-gate 					}
25660Sstevel@tonic-gate 				}
25670Sstevel@tonic-gate 				if ((c = ((_c >> _i) & 0x7f)) != 0) {
25680Sstevel@tonic-gate 					PUTC(c);
25690Sstevel@tonic-gate 				}
25700Sstevel@tonic-gate 				break;
25710Sstevel@tonic-gate 			case 's':
25720Sstevel@tonic-gate 				_s = va_arg(args, char *);
25730Sstevel@tonic-gate 				_i = strlen(_s);
25740Sstevel@tonic-gate 				/* LINTED */
25750Sstevel@tonic-gate 				_n = (int)(width - _i);
25760Sstevel@tonic-gate 				if (!prec)
25770Sstevel@tonic-gate 					/* LINTED */
25780Sstevel@tonic-gate 					prec = (int)_i;
25790Sstevel@tonic-gate 
25800Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
25810Sstevel@tonic-gate 					while (_n-- > 0)
25820Sstevel@tonic-gate 						PUTC(' ');
25830Sstevel@tonic-gate 				}
25840Sstevel@tonic-gate 				while (((c = *_s++) != 0) && prec--) {
25850Sstevel@tonic-gate 					PUTC(c);
25860Sstevel@tonic-gate 				}
25870Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
25880Sstevel@tonic-gate 					while (_n-- > 0)
25890Sstevel@tonic-gate 						PUTC(' ');
25900Sstevel@tonic-gate 				}
25910Sstevel@tonic-gate 				break;
25920Sstevel@tonic-gate 			case '%':
25930Sstevel@tonic-gate 				PUTC('%');
25940Sstevel@tonic-gate 				break;
25950Sstevel@tonic-gate 			default:
25960Sstevel@tonic-gate 				break;
25970Sstevel@tonic-gate 			}
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 			/*
26000Sstevel@tonic-gate 			 * Numeric processing
26010Sstevel@tonic-gate 			 */
26020Sstevel@tonic-gate 			if (base) {
26033191Srie 				char		local[NUM_SIZE];
26043191Srie 				size_t		ssize = 0, psize = 0;
26050Sstevel@tonic-gate 				const char	*string =
26064362Srie 				    MSG_ORIG(MSG_STR_HEXNUM);
26070Sstevel@tonic-gate 				const char	*prefix =
26084362Srie 				    MSG_ORIG(MSG_STR_EMPTY);
26090Sstevel@tonic-gate 				u_longlong_t	num;
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 				switch (ls) {
26120Sstevel@tonic-gate 				case 0:	/* int */
26130Sstevel@tonic-gate 					num = (u_longlong_t)
26140Sstevel@tonic-gate 					    va_arg(args, uint_t);
26150Sstevel@tonic-gate 					break;
26160Sstevel@tonic-gate 				case 1:	/* long */
26170Sstevel@tonic-gate 					num = (u_longlong_t)
26180Sstevel@tonic-gate 					    va_arg(args, ulong_t);
26190Sstevel@tonic-gate 					break;
26200Sstevel@tonic-gate 				case 2:	/* long long */
26210Sstevel@tonic-gate 					num = va_arg(args, u_longlong_t);
26220Sstevel@tonic-gate 					break;
26230Sstevel@tonic-gate 				}
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 				if (flag & FLG_UT_SHARP) {
26260Sstevel@tonic-gate 					if (base == 16) {
26270Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_HEX);
26280Sstevel@tonic-gate 						psize = 2;
26290Sstevel@tonic-gate 					} else {
26300Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_ZERO);
26310Sstevel@tonic-gate 						psize = 1;
26320Sstevel@tonic-gate 					}
26330Sstevel@tonic-gate 				}
26340Sstevel@tonic-gate 				if ((base == 10) && (long)num < 0) {
26350Sstevel@tonic-gate 					prefix = MSG_ORIG(MSG_STR_NEGATE);
26360Sstevel@tonic-gate 					psize = MSG_STR_NEGATE_SIZE;
26370Sstevel@tonic-gate 					num = (u_longlong_t)(-(longlong_t)num);
26380Sstevel@tonic-gate 				}
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 				/*
26410Sstevel@tonic-gate 				 * Convert the numeric value into a local
26420Sstevel@tonic-gate 				 * string (stored in reverse order).
26430Sstevel@tonic-gate 				 */
26440Sstevel@tonic-gate 				_s = local;
26450Sstevel@tonic-gate 				do {
26460Sstevel@tonic-gate 					*_s++ = string[num % base];
26470Sstevel@tonic-gate 					num /= base;
26480Sstevel@tonic-gate 					ssize++;
26490Sstevel@tonic-gate 				} while (num);
26500Sstevel@tonic-gate 
26513191Srie 				ASSERT(ssize < sizeof (local));
26523191Srie 
26530Sstevel@tonic-gate 				/*
26540Sstevel@tonic-gate 				 * Provide any precision or width padding.
26550Sstevel@tonic-gate 				 */
26560Sstevel@tonic-gate 				if (prec) {
26570Sstevel@tonic-gate 					/* LINTED */
26580Sstevel@tonic-gate 					_n = (int)(prec - ssize);
26593191Srie 					while ((_n-- > 0) &&
26603191Srie 					    (ssize < sizeof (local))) {
26610Sstevel@tonic-gate 						*_s++ = '0';
26620Sstevel@tonic-gate 						ssize++;
26630Sstevel@tonic-gate 					}
26640Sstevel@tonic-gate 				}
26650Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
26660Sstevel@tonic-gate 					/* LINTED */
26670Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
26680Sstevel@tonic-gate 					while (_n-- > 0) {
26690Sstevel@tonic-gate 						PUTC(' ');
26700Sstevel@tonic-gate 					}
26710Sstevel@tonic-gate 				}
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate 				/*
26740Sstevel@tonic-gate 				 * Print any prefix and the numeric string
26750Sstevel@tonic-gate 				 */
26760Sstevel@tonic-gate 				while (*prefix)
26770Sstevel@tonic-gate 					PUTC(*prefix++);
26780Sstevel@tonic-gate 				do {
26790Sstevel@tonic-gate 					PUTC(*--_s);
26800Sstevel@tonic-gate 				} while (_s > local);
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate 				/*
26830Sstevel@tonic-gate 				 * Provide any width padding.
26840Sstevel@tonic-gate 				 */
26850Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
26860Sstevel@tonic-gate 					/* LINTED */
26870Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
26880Sstevel@tonic-gate 					while (_n-- > 0)
26890Sstevel@tonic-gate 						PUTC(' ');
26900Sstevel@tonic-gate 				}
26910Sstevel@tonic-gate 			}
26920Sstevel@tonic-gate 		}
26930Sstevel@tonic-gate 	}
26941824Srie 
26950Sstevel@tonic-gate 	PUTC('\0');
26960Sstevel@tonic-gate 	prf->pr_cur = bp;
26970Sstevel@tonic-gate 	return (1);
26980Sstevel@tonic-gate }
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate static int
27010Sstevel@tonic-gate doprintf(const char *format, va_list args, Prfbuf *prf)
27020Sstevel@tonic-gate {
27030Sstevel@tonic-gate 	char	*ocur = prf->pr_cur;
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 	if (doprf(format, args, prf) == 0)
27060Sstevel@tonic-gate 		return (0);
27070Sstevel@tonic-gate 	/* LINTED */
27080Sstevel@tonic-gate 	return ((int)(prf->pr_cur - ocur));
27090Sstevel@tonic-gate }
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate /* VARARGS2 */
27120Sstevel@tonic-gate int
27130Sstevel@tonic-gate sprintf(char *buf, const char *format, ...)
27140Sstevel@tonic-gate {
27150Sstevel@tonic-gate 	va_list	args;
27160Sstevel@tonic-gate 	int	len;
27170Sstevel@tonic-gate 	Prfbuf	prf;
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	va_start(args, format);
27200Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
27210Sstevel@tonic-gate 	prf.pr_len = 0;
27220Sstevel@tonic-gate 	prf.pr_fd = -1;
27230Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
27240Sstevel@tonic-gate 	va_end(args);
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	/*
27270Sstevel@tonic-gate 	 * sprintf() return value excludes the terminating null byte.
27280Sstevel@tonic-gate 	 */
27290Sstevel@tonic-gate 	return (len - 1);
27300Sstevel@tonic-gate }
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate /* VARARGS3 */
27330Sstevel@tonic-gate int
27340Sstevel@tonic-gate snprintf(char *buf, size_t n, const char *format, ...)
27350Sstevel@tonic-gate {
27360Sstevel@tonic-gate 	va_list	args;
27370Sstevel@tonic-gate 	int	len;
27380Sstevel@tonic-gate 	Prfbuf	prf;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	va_start(args, format);
27410Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
27420Sstevel@tonic-gate 	prf.pr_len = n;
27430Sstevel@tonic-gate 	prf.pr_fd = -1;
27440Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
27450Sstevel@tonic-gate 	va_end(args);
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 	return (len);
27480Sstevel@tonic-gate }
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate /* VARARGS2 */
27510Sstevel@tonic-gate int
27520Sstevel@tonic-gate bufprint(Prfbuf *prf, const char *format, ...)
27530Sstevel@tonic-gate {
27540Sstevel@tonic-gate 	va_list	args;
27550Sstevel@tonic-gate 	int	len;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 	va_start(args, format);
27580Sstevel@tonic-gate 	len = doprintf(format, args, prf);
27590Sstevel@tonic-gate 	va_end(args);
27600Sstevel@tonic-gate 
27610Sstevel@tonic-gate 	return (len);
27620Sstevel@tonic-gate }
27630Sstevel@tonic-gate 
27640Sstevel@tonic-gate /*PRINTFLIKE1*/
27650Sstevel@tonic-gate int
27660Sstevel@tonic-gate printf(const char *format, ...)
27670Sstevel@tonic-gate {
27680Sstevel@tonic-gate 	va_list	args;
27690Sstevel@tonic-gate 	char 	buffer[ERRSIZE];
27700Sstevel@tonic-gate 	Prfbuf	prf;
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate 	va_start(args, format);
27730Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buffer;
27740Sstevel@tonic-gate 	prf.pr_len = ERRSIZE;
27750Sstevel@tonic-gate 	prf.pr_fd = 1;
27760Sstevel@tonic-gate 	(void) doprf(format, args, &prf);
27770Sstevel@tonic-gate 	va_end(args);
27780Sstevel@tonic-gate 	/*
27790Sstevel@tonic-gate 	 * Trim trailing '\0' form buffer
27800Sstevel@tonic-gate 	 */
27810Sstevel@tonic-gate 	prf.pr_cur--;
27820Sstevel@tonic-gate 	return (dowrite(&prf));
27830Sstevel@tonic-gate }
27840Sstevel@tonic-gate 
27850Sstevel@tonic-gate static char	errbuf[ERRSIZE], *nextptr = errbuf, *prevptr = 0;
27860Sstevel@tonic-gate 
27871618Srie /*PRINTFLIKE3*/
27880Sstevel@tonic-gate void
27891618Srie eprintf(Lm_list *lml, Error error, const char *format, ...)
27900Sstevel@tonic-gate {
27910Sstevel@tonic-gate 	va_list		args;
27920Sstevel@tonic-gate 	int		overflow = 0;
27930Sstevel@tonic-gate 	static int	lock = 0;
27940Sstevel@tonic-gate 	Prfbuf		prf;
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate 	if (lock || (nextptr == (errbuf + ERRSIZE)))
27970Sstevel@tonic-gate 		return;
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate 	/*
28000Sstevel@tonic-gate 	 * Note: this lock is here to prevent the same thread from recursively
28010Sstevel@tonic-gate 	 * entering itself during a eprintf.  ie: during eprintf malloc() fails
28020Sstevel@tonic-gate 	 * and we try and call eprintf ... and then malloc() fails ....
28030Sstevel@tonic-gate 	 */
28040Sstevel@tonic-gate 	lock = 1;
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	/*
28070Sstevel@tonic-gate 	 * If we have completed startup initialization, all error messages
28080Sstevel@tonic-gate 	 * must be saved.  These are reported through dlerror().  If we're
28090Sstevel@tonic-gate 	 * still in the initialization stage, output the error directly and
28100Sstevel@tonic-gate 	 * add a newline.
28110Sstevel@tonic-gate 	 */
28120Sstevel@tonic-gate 	va_start(args, format);
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = nextptr;
28150Sstevel@tonic-gate 	prf.pr_len = ERRSIZE - (nextptr - errbuf);
28160Sstevel@tonic-gate 
28170Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_APPLIC))
28180Sstevel@tonic-gate 		prf.pr_fd = 2;
28190Sstevel@tonic-gate 	else
28200Sstevel@tonic-gate 		prf.pr_fd = -1;
28210Sstevel@tonic-gate 
28220Sstevel@tonic-gate 	if (error > ERR_NONE) {
28230Sstevel@tonic-gate 		if ((error == ERR_FATAL) && (rtld_flags2 & RT_FL2_FTL2WARN))
28240Sstevel@tonic-gate 			error = ERR_WARNING;
28250Sstevel@tonic-gate 		if (error == ERR_WARNING) {
28260Sstevel@tonic-gate 			if (err_strs[ERR_WARNING] == 0)
28274362Srie 				err_strs[ERR_WARNING] =
28284362Srie 				    MSG_INTL(MSG_ERR_WARNING);
28290Sstevel@tonic-gate 		} else if (error == ERR_FATAL) {
28300Sstevel@tonic-gate 			if (err_strs[ERR_FATAL] == 0)
28314362Srie 				err_strs[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
28320Sstevel@tonic-gate 		} else if (error == ERR_ELF) {
28330Sstevel@tonic-gate 			if (err_strs[ERR_ELF] == 0)
28344362Srie 				err_strs[ERR_ELF] = MSG_INTL(MSG_ERR_ELF);
28350Sstevel@tonic-gate 		}
28366Srie 		if (procname) {
28376Srie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR1),
28386Srie 			    rtldname, procname, err_strs[error]) == 0)
28396Srie 				overflow = 1;
28400Sstevel@tonic-gate 		} else {
28416Srie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
28426Srie 			    rtldname, err_strs[error]) == 0)
28436Srie 				overflow = 1;
28446Srie 		}
28456Srie 		if (overflow == 0) {
28460Sstevel@tonic-gate 			/*
28470Sstevel@tonic-gate 			 * Remove the terminating '\0'.
28480Sstevel@tonic-gate 			 */
28490Sstevel@tonic-gate 			prf.pr_cur--;
28500Sstevel@tonic-gate 		}
28510Sstevel@tonic-gate 	}
28520Sstevel@tonic-gate 
28530Sstevel@tonic-gate 	if ((overflow == 0) && doprf(format, args, &prf) == 0)
28540Sstevel@tonic-gate 		overflow = 1;
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate 	/*
28570Sstevel@tonic-gate 	 * If this is an ELF error, it will have been generated by a support
28580Sstevel@tonic-gate 	 * object that has a dependency on libelf.  ld.so.1 doesn't generate any
28590Sstevel@tonic-gate 	 * ELF error messages as it doesn't interact with libelf.  Determine the
28600Sstevel@tonic-gate 	 * ELF error string.
28610Sstevel@tonic-gate 	 */
28620Sstevel@tonic-gate 	if ((overflow == 0) && (error == ERR_ELF)) {
28630Sstevel@tonic-gate 		static int		(*elfeno)() = 0;
28640Sstevel@tonic-gate 		static const char	*(*elfemg)();
28650Sstevel@tonic-gate 		const char		*emsg;
28660Sstevel@tonic-gate 		Rt_map			*dlmp, *lmp = lml_rtld.lm_head;
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 		if (NEXT(lmp) && (elfeno == 0)) {
28690Sstevel@tonic-gate 			if (((elfemg = (const char *(*)())dlsym_intn(RTLD_NEXT,
28700Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_ELFERRMSG), lmp, &dlmp)) == 0) ||
28710Sstevel@tonic-gate 			    ((elfeno = (int (*)())dlsym_intn(RTLD_NEXT,
28720Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_ELFERRNO), lmp, &dlmp)) == 0))
28730Sstevel@tonic-gate 				elfeno = 0;
28740Sstevel@tonic-gate 		}
28750Sstevel@tonic-gate 
28760Sstevel@tonic-gate 		/*
28770Sstevel@tonic-gate 		 * Lookup the message; equivalent to elf_errmsg(elf_errno()).
28780Sstevel@tonic-gate 		 */
28790Sstevel@tonic-gate 		if (elfeno && ((emsg = (* elfemg)((* elfeno)())) != 0)) {
28800Sstevel@tonic-gate 			prf.pr_cur--;
28810Sstevel@tonic-gate 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
28820Sstevel@tonic-gate 			    emsg) == 0)
28830Sstevel@tonic-gate 				overflow = 1;
28840Sstevel@tonic-gate 		}
28850Sstevel@tonic-gate 	}
28860Sstevel@tonic-gate 
28870Sstevel@tonic-gate 	/*
28880Sstevel@tonic-gate 	 * Push out any message that's been built.  Note, in the case of an
28890Sstevel@tonic-gate 	 * overflow condition, this message may be incomplete, in which case
28900Sstevel@tonic-gate 	 * make sure any partial string is null terminated.
28910Sstevel@tonic-gate 	 */
28920Sstevel@tonic-gate 	if (overflow)
28930Sstevel@tonic-gate 		*(prf.pr_cur) = '\0';
28940Sstevel@tonic-gate 	if ((rtld_flags & (RT_FL_APPLIC | RT_FL_SILENCERR)) == 0) {
28950Sstevel@tonic-gate 		*(prf.pr_cur - 1) = '\n';
28960Sstevel@tonic-gate 		(void) dowrite(&prf);
28970Sstevel@tonic-gate 	}
28980Sstevel@tonic-gate 
28991618Srie 	DBG_CALL(Dbg_util_str(lml, nextptr));
29000Sstevel@tonic-gate 	va_end(args);
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 	/*
29030Sstevel@tonic-gate 	 * Determine if there was insufficient space left in the buffer to
29040Sstevel@tonic-gate 	 * complete the message.  If so, we'll have printed out as much as had
29050Sstevel@tonic-gate 	 * been processed if we're not yet executing the application.
29060Sstevel@tonic-gate 	 * Otherwise, there will be some debugging diagnostic indicating
29070Sstevel@tonic-gate 	 * as much of the error message as possible.  Write out a final buffer
29080Sstevel@tonic-gate 	 * overflow diagnostic - unlocalized, so we don't chance more errors.
29090Sstevel@tonic-gate 	 */
29100Sstevel@tonic-gate 	if (overflow) {
29110Sstevel@tonic-gate 		char	*str = (char *)MSG_INTL(MSG_EMG_BUFOVRFLW);
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) == 0) {
29140Sstevel@tonic-gate 			lasterr = str;
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_APPLIC) == 0) {
29170Sstevel@tonic-gate 				(void) write(2, str, strlen(str));
29180Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
29190Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
29200Sstevel@tonic-gate 			}
29210Sstevel@tonic-gate 		}
29221618Srie 		DBG_CALL(Dbg_util_str(lml, str));
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 		lock = 0;
29250Sstevel@tonic-gate 		nextptr = errbuf + ERRSIZE;
29260Sstevel@tonic-gate 		return;
29270Sstevel@tonic-gate 	}
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 	/*
29300Sstevel@tonic-gate 	 * If the application has started, then error messages are being saved
29310Sstevel@tonic-gate 	 * for retrieval by dlerror(), or possible flushing from rtldexit() in
29320Sstevel@tonic-gate 	 * the case of a fatal error.  In this case, establish the next error
29330Sstevel@tonic-gate 	 * pointer.  If we haven't started the application, the whole message
29340Sstevel@tonic-gate 	 * buffer can be reused.
29350Sstevel@tonic-gate 	 */
29360Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SILENCERR) == 0) {
29370Sstevel@tonic-gate 		lasterr = nextptr;
29380Sstevel@tonic-gate 
29390Sstevel@tonic-gate 		/*
29400Sstevel@tonic-gate 		 * Note, should we encounter an error such as ENOMEM, there may
29410Sstevel@tonic-gate 		 * be a number of the same error messages (ie. an operation
29420Sstevel@tonic-gate 		 * fails with ENOMEM, and then the attempts to construct the
29430Sstevel@tonic-gate 		 * error message itself, which incurs additional ENOMEM errors).
29440Sstevel@tonic-gate 		 * Compare any previous error message with the one we've just
29450Sstevel@tonic-gate 		 * created to prevent any duplication clutter.
29460Sstevel@tonic-gate 		 */
29470Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) &&
29480Sstevel@tonic-gate 		    ((prevptr == 0) || (strcmp(prevptr, nextptr) != 0))) {
29490Sstevel@tonic-gate 			prevptr = nextptr;
29500Sstevel@tonic-gate 			nextptr = prf.pr_cur;
29510Sstevel@tonic-gate 			*nextptr = '\0';
29520Sstevel@tonic-gate 		}
29530Sstevel@tonic-gate 	}
29540Sstevel@tonic-gate 	lock = 0;
29550Sstevel@tonic-gate }
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 
29580Sstevel@tonic-gate #if	DEBUG
29590Sstevel@tonic-gate /*
29600Sstevel@tonic-gate  * Provide assfail() for ASSERT() statements,
29610Sstevel@tonic-gate  * see <sys/debug.h> for further details.
29620Sstevel@tonic-gate  */
29630Sstevel@tonic-gate int
29640Sstevel@tonic-gate assfail(const char *a, const char *f, int l)
29650Sstevel@tonic-gate {
29660Sstevel@tonic-gate 	(void) printf("assertion failed: %s, file: %s, line: %d\n", a, f, l);
29670Sstevel@tonic-gate 	(void) _lwp_kill(_lwp_self(), SIGABRT);
29680Sstevel@tonic-gate 	return (0);
29690Sstevel@tonic-gate }
29700Sstevel@tonic-gate #endif
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate /*
29730Sstevel@tonic-gate  * Exit.  If we arrive here with a non zero status it's because of a fatal
29740Sstevel@tonic-gate  * error condition (most commonly a relocation error).  If the application has
29750Sstevel@tonic-gate  * already had control, then the actual fatal error message will have been
29760Sstevel@tonic-gate  * recorded in the dlerror() message buffer.  Print the message before really
29770Sstevel@tonic-gate  * exiting.
29780Sstevel@tonic-gate  */
29790Sstevel@tonic-gate void
29800Sstevel@tonic-gate rtldexit(Lm_list * lml, int status)
29810Sstevel@tonic-gate {
29820Sstevel@tonic-gate 	if (status) {
29830Sstevel@tonic-gate 		if (rtld_flags & RT_FL_APPLIC) {
29840Sstevel@tonic-gate 			/*
29850Sstevel@tonic-gate 			 * If the error buffer has been used, write out all
29860Sstevel@tonic-gate 			 * pending messages - lasterr is simply a pointer to
29870Sstevel@tonic-gate 			 * the last message in this buffer.  However, if the
29880Sstevel@tonic-gate 			 * buffer couldn't be created at all, lasterr points
29890Sstevel@tonic-gate 			 * to a constant error message string.
29900Sstevel@tonic-gate 			 */
29910Sstevel@tonic-gate 			if (*errbuf) {
29920Sstevel@tonic-gate 				char	*errptr = errbuf;
29930Sstevel@tonic-gate 				char	*errend = errbuf + ERRSIZE;
29940Sstevel@tonic-gate 
29950Sstevel@tonic-gate 				while ((errptr < errend) && *errptr) {
29960Sstevel@tonic-gate 					size_t	size = strlen(errptr);
29970Sstevel@tonic-gate 					(void) write(2, errptr, size);
29980Sstevel@tonic-gate 					(void) write(2, MSG_ORIG(MSG_STR_NL),
29990Sstevel@tonic-gate 					    MSG_STR_NL_SIZE);
30000Sstevel@tonic-gate 					errptr += (size + 1);
30010Sstevel@tonic-gate 				}
30020Sstevel@tonic-gate 			}
30030Sstevel@tonic-gate 			if (lasterr && ((lasterr < errbuf) ||
30040Sstevel@tonic-gate 			    (lasterr > (errbuf + ERRSIZE)))) {
30050Sstevel@tonic-gate 				(void) write(2, lasterr, strlen(lasterr));
30060Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
30070Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
30080Sstevel@tonic-gate 			}
30090Sstevel@tonic-gate 		}
30100Sstevel@tonic-gate 		leave(lml);
30110Sstevel@tonic-gate 		(void) _lwp_kill(_lwp_self(), killsig);
30120Sstevel@tonic-gate 	}
30130Sstevel@tonic-gate 	_exit(status);
30140Sstevel@tonic-gate }
30150Sstevel@tonic-gate 
30160Sstevel@tonic-gate /*
30170Sstevel@tonic-gate  * Routines to co-ordinate the opening of /dev/zero and /proc.
30180Sstevel@tonic-gate  * dz_fd is exported for possible use by libld.so, and to insure it gets
30190Sstevel@tonic-gate  * closed on leaving ld.so.1.
30200Sstevel@tonic-gate  */
30210Sstevel@tonic-gate int	dz_fd = FD_UNAVAIL;
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate void
30240Sstevel@tonic-gate dz_init(int fd)
30250Sstevel@tonic-gate {
30260Sstevel@tonic-gate 	dz_fd = fd;
30270Sstevel@tonic-gate }
30280Sstevel@tonic-gate 
30290Sstevel@tonic-gate 
30300Sstevel@tonic-gate /*
30310Sstevel@tonic-gate  * mmap() a page from MAP_ANON
30320Sstevel@tonic-gate  *
30330Sstevel@tonic-gate  * Note: MAP_ANON is only on Solaris8++, we use this routine to
30340Sstevel@tonic-gate  *       not only mmap(MAP_ANON) but to also probe if it is available
30350Sstevel@tonic-gate  *	 on the current OS.
30360Sstevel@tonic-gate  */
30370Sstevel@tonic-gate Am_ret
30381618Srie anon_map(Lm_list *lml, caddr_t *addr, size_t len, int prot, int flags)
30390Sstevel@tonic-gate {
30400Sstevel@tonic-gate #if defined(MAP_ANON)
30410Sstevel@tonic-gate 	static int	noanon = 0;
30420Sstevel@tonic-gate 	caddr_t		va;
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 	if (noanon == 0) {
30450Sstevel@tonic-gate 		if ((va = (caddr_t)mmap(*addr, len, prot,
30460Sstevel@tonic-gate 		    (flags | MAP_ANON), -1, 0)) != MAP_FAILED) {
30470Sstevel@tonic-gate 			*addr = va;
30480Sstevel@tonic-gate 			return (AM_OK);
30490Sstevel@tonic-gate 		}
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 		if ((errno != EBADF) && (errno != EINVAL)) {
30520Sstevel@tonic-gate 			int	err = errno;
30531618Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON),
30540Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
30550Sstevel@tonic-gate 			return (AM_ERROR);
30560Sstevel@tonic-gate 		} else
30570Sstevel@tonic-gate 			noanon = 1;
30580Sstevel@tonic-gate 	}
30590Sstevel@tonic-gate #endif
30600Sstevel@tonic-gate 	return (AM_NOSUP);
30610Sstevel@tonic-gate }
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate /*
30640Sstevel@tonic-gate  * Map anonymous memory from /dev/zero, or via MAP_ANON.
30650Sstevel@tonic-gate  *
30660Sstevel@tonic-gate  * (MAP_ANON only appears on Solaris 8, so we need fall-back
30670Sstevel@tonic-gate  * behavior for older systems.)
30680Sstevel@tonic-gate  */
30690Sstevel@tonic-gate caddr_t
30701618Srie dz_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
30710Sstevel@tonic-gate {
30720Sstevel@tonic-gate 	caddr_t	va;
30730Sstevel@tonic-gate 	int	err;
30740Sstevel@tonic-gate 	Am_ret	amret;
30750Sstevel@tonic-gate 
30761618Srie 	amret = anon_map(lml, &addr, len, prot, flags);
30770Sstevel@tonic-gate 
30780Sstevel@tonic-gate 	if (amret == AM_OK)
30790Sstevel@tonic-gate 		return (addr);
30800Sstevel@tonic-gate 	if (amret == AM_ERROR)
30810Sstevel@tonic-gate 		return (MAP_FAILED);
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	/* amret == AM_NOSUP -> fallback to a devzero mmaping */
30840Sstevel@tonic-gate 
30850Sstevel@tonic-gate 	if (dz_fd == FD_UNAVAIL) {
30860Sstevel@tonic-gate 		if ((dz_fd = open(MSG_ORIG(MSG_PTH_DEVZERO),
30870Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
30880Sstevel@tonic-gate 			err = errno;
30891618Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
30900Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
30910Sstevel@tonic-gate 			return (MAP_FAILED);
30920Sstevel@tonic-gate 		}
30930Sstevel@tonic-gate 	}
30940Sstevel@tonic-gate 
30950Sstevel@tonic-gate 	if ((va = mmap(addr, len, prot, flags, dz_fd, 0)) == MAP_FAILED) {
30960Sstevel@tonic-gate 		err = errno;
30971618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
30980Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
30990Sstevel@tonic-gate 	}
31000Sstevel@tonic-gate 	return (va);
31010Sstevel@tonic-gate }
31020Sstevel@tonic-gate 
31030Sstevel@tonic-gate static int	pr_fd = FD_UNAVAIL;
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate int
31061618Srie pr_open(Lm_list *lml)
31070Sstevel@tonic-gate {
31080Sstevel@tonic-gate 	char	proc[16];
31090Sstevel@tonic-gate 
31100Sstevel@tonic-gate 	if (pr_fd == FD_UNAVAIL) {
31110Sstevel@tonic-gate 		(void) snprintf(proc, 16, MSG_ORIG(MSG_FMT_PROC),
31124362Srie 		    (int)getpid());
31130Sstevel@tonic-gate 		if ((pr_fd = open(proc, O_RDONLY)) == FD_UNAVAIL) {
31140Sstevel@tonic-gate 			int	err = errno;
31150Sstevel@tonic-gate 
31161618Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), proc,
31170Sstevel@tonic-gate 			    strerror(err));
31180Sstevel@tonic-gate 		}
31190Sstevel@tonic-gate 	}
31200Sstevel@tonic-gate 	return (pr_fd);
31210Sstevel@tonic-gate }
31220Sstevel@tonic-gate 
31230Sstevel@tonic-gate static int	nu_fd = FD_UNAVAIL;
31240Sstevel@tonic-gate 
31250Sstevel@tonic-gate caddr_t
31261618Srie nu_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
31270Sstevel@tonic-gate {
31280Sstevel@tonic-gate 	caddr_t	va;
31290Sstevel@tonic-gate 	int	err;
31300Sstevel@tonic-gate 
31310Sstevel@tonic-gate 	if (nu_fd == FD_UNAVAIL) {
31320Sstevel@tonic-gate 		if ((nu_fd = open(MSG_ORIG(MSG_PTH_DEVNULL),
31330Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
31340Sstevel@tonic-gate 			err = errno;
31351618Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
31360Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
31370Sstevel@tonic-gate 			return (MAP_FAILED);
31380Sstevel@tonic-gate 		}
31390Sstevel@tonic-gate 	}
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	if ((va = (caddr_t)mmap(addr, len, prot, flags, nu_fd, 0)) ==
31420Sstevel@tonic-gate 	    MAP_FAILED) {
31430Sstevel@tonic-gate 		err = errno;
31441618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
31450Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
31460Sstevel@tonic-gate 	}
31470Sstevel@tonic-gate 	return (va);
31480Sstevel@tonic-gate }
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate /*
31515950Srie  * Generic entry point from user code - simply grabs a lock, and bumps the
31525950Srie  * entrance count.
31530Sstevel@tonic-gate  */
31540Sstevel@tonic-gate int
31550Sstevel@tonic-gate enter(void)
31560Sstevel@tonic-gate {
31570Sstevel@tonic-gate 	if (rt_bind_guard(THR_FLG_RTLD)) {
31580Sstevel@tonic-gate 		(void) rt_mutex_lock(&rtldlock);
3159*6387Srie 		if (rtld_flags & RT_FL_OPERATION)
3160*6387Srie 			ld_entry_cnt++;
31610Sstevel@tonic-gate 		return (1);
31620Sstevel@tonic-gate 	}
31630Sstevel@tonic-gate 	return (0);
31640Sstevel@tonic-gate }
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate /*
3167*6387Srie  * Determine whether a search path has been used.
3168*6387Srie  */
3169*6387Srie static void
3170*6387Srie is_path_used(Lm_list *lml, Word unref, int *nl, Pnode *pnp, const char *obj)
3171*6387Srie {
3172*6387Srie 	for (; pnp; pnp = pnp->p_next) {
3173*6387Srie 		const char	*fmt, *name;
3174*6387Srie 
3175*6387Srie 		if ((pnp->p_len == 0) || (pnp->p_orig & PN_FLG_USED))
3176*6387Srie 			continue;
3177*6387Srie 
3178*6387Srie 		/*
3179*6387Srie 		 * If this pathname originated from an expanded token, use the
3180*6387Srie 		 * original for any diagnostic output.
3181*6387Srie 		 */
3182*6387Srie 		if ((name = pnp->p_oname) == 0)
3183*6387Srie 			name = pnp->p_name;
3184*6387Srie 
3185*6387Srie 		if (unref == 0) {
3186*6387Srie 			if ((*nl)++ == 0)
3187*6387Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
3188*6387Srie 			DBG_CALL(Dbg_unused_path(lml, name, pnp->p_orig,
3189*6387Srie 			    (pnp->p_orig & PN_FLG_DUPLICAT), obj));
3190*6387Srie 			continue;
3191*6387Srie 		}
3192*6387Srie 
3193*6387Srie 		/*
3194*6387Srie 		 * For now, disable any unused search path processing that has
3195*6387Srie 		 * been triggered for ldd(1).
3196*6387Srie 		 */
3197*6387Srie 		if (unref)
3198*6387Srie 			continue;
3199*6387Srie 
3200*6387Srie 		if (pnp->p_orig & LA_SER_LIBPATH) {
3201*6387Srie 			if (pnp->p_orig & LA_SER_CONFIG) {
3202*6387Srie 				if (pnp->p_orig & PN_FLG_DUPLICAT)
3203*6387Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATHC);
3204*6387Srie 				else
3205*6387Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATHC);
3206*6387Srie 			} else {
3207*6387Srie 				if (pnp->p_orig & PN_FLG_DUPLICAT)
3208*6387Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATH);
3209*6387Srie 				else
3210*6387Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATH);
3211*6387Srie 			}
3212*6387Srie 		} else if (pnp->p_orig & LA_SER_RUNPATH) {
3213*6387Srie 			fmt = MSG_INTL(MSG_USD_RUNPATH);
3214*6387Srie 		} else
3215*6387Srie 			continue;
3216*6387Srie 
3217*6387Srie 		if ((*nl)++ == 0)
3218*6387Srie 			(void) printf(MSG_ORIG(MSG_STR_NL));
3219*6387Srie 		(void) printf(fmt, name, obj);
3220*6387Srie 	}
3221*6387Srie }
3222*6387Srie 
3223*6387Srie /*
32240Sstevel@tonic-gate  * Generate diagnostics as to whether an object has been used.  A symbolic
32250Sstevel@tonic-gate  * reference that gets bound to an object marks it as used.  Dependencies that
32260Sstevel@tonic-gate  * are unused when RTLD_NOW is in effect should be removed from future builds
32270Sstevel@tonic-gate  * of an object.  Dependencies that are unused without RTLD_NOW in effect are
32280Sstevel@tonic-gate  * candidates for lazy-loading.
3229*6387Srie  *
32300Sstevel@tonic-gate  * Unreferenced objects identify objects that are defined as dependencies but
3231*6387Srie  * are unreferenced by the caller.  These unreferenced objects may however be
3232*6387Srie  * referenced by other objects within the process, and therefore don't qualify
3233*6387Srie  * as completely unused.  They are still an unnecessary overhead.
3234*6387Srie  *
3235*6387Srie  * Unreferenced runpaths are also captured under ldd -U, or "unused,detail"
3236*6387Srie  * debugging.
32370Sstevel@tonic-gate  */
32380Sstevel@tonic-gate void
32390Sstevel@tonic-gate unused(Lm_list *lml)
32400Sstevel@tonic-gate {
32410Sstevel@tonic-gate 	Rt_map		*lmp;
32420Sstevel@tonic-gate 	int		nl = 0;
3243*6387Srie 	Word		unref, unuse;
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 	/*
32460Sstevel@tonic-gate 	 * If we're not tracing unused references or dependencies, or debugging
32470Sstevel@tonic-gate 	 * there's nothing to do.
32480Sstevel@tonic-gate 	 */
3249*6387Srie 	unref = lml->lm_flags & LML_FLG_TRC_UNREF;
3250*6387Srie 	unuse = lml->lm_flags & LML_FLG_TRC_UNUSED;
3251*6387Srie 
3252*6387Srie 	if ((unref == 0) && (unuse == 0) && (DBG_ENABLED == 0))
32530Sstevel@tonic-gate 		return;
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 	/*
3256*6387Srie 	 * Detect unused global search paths.
3257*6387Srie 	 */
3258*6387Srie 	if (rpl_libdirs)
3259*6387Srie 		is_path_used(lml, unref, &nl, rpl_libdirs, config->c_name);
3260*6387Srie 	if (prm_libdirs)
3261*6387Srie 		is_path_used(lml, unref, &nl, prm_libdirs, config->c_name);
3262*6387Srie 
3263*6387Srie 	nl = 0;
3264*6387Srie 	lmp = lml->lm_head;
3265*6387Srie 	if (RLIST(lmp))
3266*6387Srie 		is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
3267*6387Srie 
3268*6387Srie 	/*
32690Sstevel@tonic-gate 	 * Traverse the link-maps looking for unreferenced or unused
32700Sstevel@tonic-gate 	 * dependencies.  Ignore the first object on a link-map list, as this
3271*6387Srie 	 * is always used.
32720Sstevel@tonic-gate 	 */
3273*6387Srie 	nl = 0;
3274*6387Srie 	for (lmp = (Rt_map *)NEXT(lmp); lmp; lmp = (Rt_map *)NEXT(lmp)) {
3275*6387Srie 		/*
3276*6387Srie 		 * Determine if this object contains any runpaths that have
3277*6387Srie 		 * not been used.
3278*6387Srie 		 */
3279*6387Srie 		if (RLIST(lmp))
3280*6387Srie 			is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
3281*6387Srie 
32820Sstevel@tonic-gate 		/*
32830Sstevel@tonic-gate 		 * If tracing unreferenced objects, or under debugging,
32840Sstevel@tonic-gate 		 * determine whether any of this objects callers haven't
32850Sstevel@tonic-gate 		 * referenced it.
32860Sstevel@tonic-gate 		 */
3287*6387Srie 		if (unref || DBG_ENABLED) {
32885892Sab196087 			Bnd_desc	*bdp;
32895892Sab196087 			Aliste		idx;
32905892Sab196087 
32915892Sab196087 			for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
3292*6387Srie 				Rt_map	*clmp;
32930Sstevel@tonic-gate 
32940Sstevel@tonic-gate 				if (bdp->b_flags & BND_REFER)
32950Sstevel@tonic-gate 					continue;
32960Sstevel@tonic-gate 
32970Sstevel@tonic-gate 				clmp = bdp->b_caller;
32980Sstevel@tonic-gate 				if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
32990Sstevel@tonic-gate 					continue;
33000Sstevel@tonic-gate 
33014362Srie 				/* BEGIN CSTYLED */
33020Sstevel@tonic-gate 				if (nl++ == 0) {
3303*6387Srie 					if (unref)
33040Sstevel@tonic-gate 					    (void) printf(MSG_ORIG(MSG_STR_NL));
33050Sstevel@tonic-gate 					else
33061618Srie 					    DBG_CALL(Dbg_util_nl(lml,
33071618Srie 						DBG_NL_STD));
33080Sstevel@tonic-gate 				}
33090Sstevel@tonic-gate 
3310*6387Srie 				if (unref)
33110Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_UNREF_FMT),
33120Sstevel@tonic-gate 					NAME(lmp), NAME(clmp));
33130Sstevel@tonic-gate 				else
33141618Srie 				    DBG_CALL(Dbg_unused_unref(lmp, NAME(clmp)));
33154362Srie 				/* END CSTYLED */
33160Sstevel@tonic-gate 			}
33170Sstevel@tonic-gate 		}
33180Sstevel@tonic-gate 
33190Sstevel@tonic-gate 		/*
33200Sstevel@tonic-gate 		 * If tracing unused objects simply display those objects that
33210Sstevel@tonic-gate 		 * haven't been referenced by anyone.
33220Sstevel@tonic-gate 		 */
33230Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_USED)
33240Sstevel@tonic-gate 			continue;
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate 		if (nl++ == 0) {
3327*6387Srie 			if (unref || unuse)
33280Sstevel@tonic-gate 				(void) printf(MSG_ORIG(MSG_STR_NL));
33290Sstevel@tonic-gate 			else
33301618Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
33310Sstevel@tonic-gate 		}
33320Sstevel@tonic-gate 		if (CYCGROUP(lmp)) {
3333*6387Srie 			if (unref || unuse)
33340Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNCYC_FMT),
33350Sstevel@tonic-gate 				    NAME(lmp), CYCGROUP(lmp));
33360Sstevel@tonic-gate 			else
33371618Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0,
33380Sstevel@tonic-gate 				    CYCGROUP(lmp)));
33390Sstevel@tonic-gate 		} else {
3340*6387Srie 			if (unref || unuse)
33410Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNUSED_FMT),
33420Sstevel@tonic-gate 				    NAME(lmp));
33430Sstevel@tonic-gate 			else
33441618Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0, 0));
33450Sstevel@tonic-gate 		}
33460Sstevel@tonic-gate 	}
33470Sstevel@tonic-gate 
33481618Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
33490Sstevel@tonic-gate }
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate /*
33520Sstevel@tonic-gate  * Initialization routine for the Fmap structure.  If the fmap structure is
33530Sstevel@tonic-gate  * already in use, any mapping is released.  The structure is then initialized
33540Sstevel@tonic-gate  * in preparation for further use.
33550Sstevel@tonic-gate  */
33560Sstevel@tonic-gate void
33570Sstevel@tonic-gate fmap_setup()
33580Sstevel@tonic-gate {
33590Sstevel@tonic-gate #if defined(MAP_ALIGN)
33600Sstevel@tonic-gate 	/*
33610Sstevel@tonic-gate 	 * If MAP_ALIGN is set, the fm_addr has been seeded with an alignment
33620Sstevel@tonic-gate 	 * value.  Otherwise, if fm_addr is non-null it indicates a mapping that
33630Sstevel@tonic-gate 	 * should now be freed.
33640Sstevel@tonic-gate 	 */
33650Sstevel@tonic-gate 	if (fmap->fm_maddr && ((fmap->fm_mflags & MAP_ALIGN) == 0))
33660Sstevel@tonic-gate 		(void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize);
33670Sstevel@tonic-gate 
33680Sstevel@tonic-gate 	/*
33690Sstevel@tonic-gate 	 * Providing we haven't determined that this system doesn't support
33700Sstevel@tonic-gate 	 * MAP_ALIGN, initialize the mapping address with the default segment
33710Sstevel@tonic-gate 	 * alignment.
33720Sstevel@tonic-gate 	 */
33730Sstevel@tonic-gate 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
33740Sstevel@tonic-gate 		fmap->fm_maddr = (char *)M_SEGM_ALIGN;
33750Sstevel@tonic-gate 		fmap->fm_mflags = MAP_PRIVATE | MAP_ALIGN;
33760Sstevel@tonic-gate 	} else {
33770Sstevel@tonic-gate 		fmap->fm_maddr = 0;
33780Sstevel@tonic-gate 		fmap->fm_mflags = MAP_PRIVATE;
33790Sstevel@tonic-gate 	}
33800Sstevel@tonic-gate #else
33810Sstevel@tonic-gate 	if (fmap->fm_maddr)
33820Sstevel@tonic-gate 		(void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize);
33831109Srie 
33840Sstevel@tonic-gate 	fmap->fm_maddr = 0;
33850Sstevel@tonic-gate 	fmap->fm_mflags = MAP_PRIVATE;
33860Sstevel@tonic-gate #endif
33870Sstevel@tonic-gate 
33884362Srie 	fmap->fm_msize = FMAP_SIZE;
33890Sstevel@tonic-gate 	fmap->fm_hwptr = 0;
33900Sstevel@tonic-gate }
33910Sstevel@tonic-gate 
33920Sstevel@tonic-gate /*
33930Sstevel@tonic-gate  * Generic cleanup routine called prior to returning control to the user.
33940Sstevel@tonic-gate  * Insures that any ld.so.1 specific file descriptors or temporary mapping are
33950Sstevel@tonic-gate  * released, and any locks dropped.
33960Sstevel@tonic-gate  */
33970Sstevel@tonic-gate void
33982454Srie leave(Lm_list *lml)
33990Sstevel@tonic-gate {
34002454Srie 	Lm_list	*elml = lml;
34015892Sab196087 	Rt_map	*clmp;
34025892Sab196087 	Aliste	idx;
34032454Srie 
34040Sstevel@tonic-gate 	/*
34052454Srie 	 * Alert the debuggers that the link-maps are consistent.  Note, in the
34062454Srie 	 * case of tearing down a whole link-map list, lml will be null.  In
34072454Srie 	 * this case use the main link-map list to test for a notification.
34080Sstevel@tonic-gate 	 */
34092454Srie 	if (elml == 0)
34102454Srie 		elml = &lml_main;
34112454Srie 	if (elml->lm_flags & LML_FLG_DBNOTIF)
34122454Srie 		rd_event(elml, RD_DLACTIVITY, RT_CONSISTENT);
34130Sstevel@tonic-gate 
34144679Srie 	/*
34154679Srie 	 * Alert any auditors that the link-maps are consistent.
34164679Srie 	 */
34175892Sab196087 	for (APLIST_TRAVERSE(elml->lm_actaudit, idx, clmp)) {
34185892Sab196087 		audit_activity(clmp, LA_ACT_CONSISTENT);
34195892Sab196087 
34205892Sab196087 		aplist_delete(elml->lm_actaudit, &idx);
34214679Srie 	}
34224679Srie 
34230Sstevel@tonic-gate 	if (dz_fd != FD_UNAVAIL) {
34240Sstevel@tonic-gate 		(void) close(dz_fd);
34250Sstevel@tonic-gate 		dz_fd = FD_UNAVAIL;
34260Sstevel@tonic-gate 	}
34270Sstevel@tonic-gate 
34280Sstevel@tonic-gate 	if (pr_fd != FD_UNAVAIL) {
34290Sstevel@tonic-gate 		(void) close(pr_fd);
34300Sstevel@tonic-gate 		pr_fd = FD_UNAVAIL;
34310Sstevel@tonic-gate 	}
34320Sstevel@tonic-gate 
34330Sstevel@tonic-gate 	if (nu_fd != FD_UNAVAIL) {
34340Sstevel@tonic-gate 		(void) close(nu_fd);
34350Sstevel@tonic-gate 		nu_fd = FD_UNAVAIL;
34360Sstevel@tonic-gate 	}
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 	fmap_setup();
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate 	/*
34410Sstevel@tonic-gate 	 * Reinitialize error message pointer, and any overflow indication.
34420Sstevel@tonic-gate 	 */
34430Sstevel@tonic-gate 	nextptr = errbuf;
34440Sstevel@tonic-gate 	prevptr = 0;
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 	/*
34470Sstevel@tonic-gate 	 * Don't drop our lock if we are running on our link-map list as
34480Sstevel@tonic-gate 	 * there's little point in doing so since we are single-threaded.
34490Sstevel@tonic-gate 	 *
34500Sstevel@tonic-gate 	 * LML_FLG_HOLDLOCK is set for:
34510Sstevel@tonic-gate 	 *	*) The ld.so.1's link-map list.
34520Sstevel@tonic-gate 	 *	*) The auditor's link-map if the environment is
34530Sstevel@tonic-gate 	 *	   libc/libthread un-unified.
34540Sstevel@tonic-gate 	 */
34550Sstevel@tonic-gate 	if (lml && (lml->lm_flags & LML_FLG_HOLDLOCK))
34560Sstevel@tonic-gate 		return;
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	if (rt_bind_clear(0) & THR_FLG_RTLD) {
34590Sstevel@tonic-gate 		(void) rt_mutex_unlock(&rtldlock);
34600Sstevel@tonic-gate 		(void) rt_bind_clear(THR_FLG_RTLD);
34610Sstevel@tonic-gate 	}
34620Sstevel@tonic-gate }
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate int
34655220Srie callable(Rt_map *clmp, Rt_map *dlmp, Grp_hdl *ghp, uint_t slflags)
34660Sstevel@tonic-gate {
34675892Sab196087 	APlist		*calp, *dalp;
34685892Sab196087 	Aliste		idx1, idx2;
34695892Sab196087 	Grp_hdl		*ghp1, *ghp2;
34700Sstevel@tonic-gate 
34710Sstevel@tonic-gate 	/*
34720Sstevel@tonic-gate 	 * An object can always find symbols within itself.
34730Sstevel@tonic-gate 	 */
34740Sstevel@tonic-gate 	if (clmp == dlmp)
34750Sstevel@tonic-gate 		return (1);
34760Sstevel@tonic-gate 
34770Sstevel@tonic-gate 	/*
34785220Srie 	 * The search for a singleton must look in every loaded object.
34795220Srie 	 */
34805220Srie 	if (slflags & LKUP_SINGLETON)
34815220Srie 		return (1);
34825220Srie 
34835220Srie 	/*
34840Sstevel@tonic-gate 	 * Don't allow an object to bind to an object that is being deleted
34850Sstevel@tonic-gate 	 * unless the binder is also being deleted.
34860Sstevel@tonic-gate 	 */
34870Sstevel@tonic-gate 	if ((FLAGS(dlmp) & FLG_RT_DELETE) &&
34880Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
34890Sstevel@tonic-gate 		return (0);
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate 	/*
34920Sstevel@tonic-gate 	 * An object with world access can always bind to an object with global
34930Sstevel@tonic-gate 	 * visibility.
34940Sstevel@tonic-gate 	 */
34950Sstevel@tonic-gate 	if ((MODE(clmp) & RTLD_WORLD) && (MODE(dlmp) & RTLD_GLOBAL))
34960Sstevel@tonic-gate 		return (1);
34970Sstevel@tonic-gate 
34980Sstevel@tonic-gate 	/*
34990Sstevel@tonic-gate 	 * An object with local access can only bind to an object that is a
35000Sstevel@tonic-gate 	 * member of the same group.
35010Sstevel@tonic-gate 	 */
35020Sstevel@tonic-gate 	if (((MODE(clmp) & RTLD_GROUP) == 0) ||
35035892Sab196087 	    ((calp = GROUPS(clmp)) == NULL) || ((dalp = GROUPS(dlmp)) == NULL))
35040Sstevel@tonic-gate 		return (0);
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate 	/*
35070Sstevel@tonic-gate 	 * Traverse the list of groups the caller is a part of.
35080Sstevel@tonic-gate 	 */
35095892Sab196087 	for (APLIST_TRAVERSE(calp, idx1, ghp1)) {
35100Sstevel@tonic-gate 		/*
35110Sstevel@tonic-gate 		 * If we're testing for the ability of two objects to bind to
35120Sstevel@tonic-gate 		 * each other regardless of a specific group, ignore that group.
35130Sstevel@tonic-gate 		 */
35145892Sab196087 		if (ghp && (ghp1 == ghp))
35150Sstevel@tonic-gate 			continue;
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 		/*
35180Sstevel@tonic-gate 		 * Traverse the list of groups the destination is a part of.
35190Sstevel@tonic-gate 		 */
35205892Sab196087 		for (APLIST_TRAVERSE(dalp, idx2, ghp2)) {
35214699Srie 			Grp_desc	*gdp;
35225892Sab196087 			Aliste		idx3;
35235892Sab196087 
35245892Sab196087 			if (ghp1 != ghp2)
35254699Srie 				continue;
35264699Srie 
35274699Srie 			/*
35284699Srie 			 * Make sure the relationship between the destination
35294699Srie 			 * and the caller provide symbols for relocation.
35304699Srie 			 * Parents are maintained as callers, but unless the
35314699Srie 			 * destination object was opened with RTLD_PARENT, the
35324699Srie 			 * parent doesn't provide symbols for the destination
35334699Srie 			 * to relocate against.
35344699Srie 			 */
35355892Sab196087 			for (ALIST_TRAVERSE(ghp2->gh_depends, idx3, gdp)) {
35364699Srie 				if (dlmp != gdp->gd_depend)
35374699Srie 					continue;
35384699Srie 
35394699Srie 				if (gdp->gd_flags & GPD_RELOC)
35404699Srie 					return (1);
35414699Srie 			}
35420Sstevel@tonic-gate 		}
35430Sstevel@tonic-gate 	}
35440Sstevel@tonic-gate 	return (0);
35450Sstevel@tonic-gate }
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate /*
35480Sstevel@tonic-gate  * Initialize the environ symbol.  Traditionally this is carried out by the crt
35490Sstevel@tonic-gate  * code prior to jumping to main.  However, init sections get fired before this
35500Sstevel@tonic-gate  * variable is initialized, so ld.so.1 sets this directly from the AUX vector
35510Sstevel@tonic-gate  * information.  In addition, a process may have multiple link-maps (ld.so.1's
35520Sstevel@tonic-gate  * debugging and preloading objects), and link auditing, and each may need an
35530Sstevel@tonic-gate  * environ variable set.
35540Sstevel@tonic-gate  *
35550Sstevel@tonic-gate  * This routine is called after a relocation() pass, and thus provides for:
35560Sstevel@tonic-gate  *
35570Sstevel@tonic-gate  *  o	setting environ on the main link-map after the initial application and
35580Sstevel@tonic-gate  *	its dependencies have been established.  Typically environ lives in the
35590Sstevel@tonic-gate  *	application (provided by its crt), but in older applications it might
35600Sstevel@tonic-gate  *	be in libc.  Who knows what's expected of applications not built on
35610Sstevel@tonic-gate  *	Solaris.
35620Sstevel@tonic-gate  *
35630Sstevel@tonic-gate  *  o	after loading a new shared object.  We can add shared objects to various
35640Sstevel@tonic-gate  *	link-maps, and any link-map dependencies requiring getopt() require
35650Sstevel@tonic-gate  *	their own environ.  In addition, lazy loading might bring in the
35666Srie  *	supplier of environ (libc used to be a lazy loading candidate) after
35676Srie  *	the link-map has been established and other objects are present.
35680Sstevel@tonic-gate  *
35690Sstevel@tonic-gate  * This routine handles all these scenarios, without adding unnecessary overhead
35700Sstevel@tonic-gate  * to ld.so.1.
35710Sstevel@tonic-gate  */
35720Sstevel@tonic-gate void
35730Sstevel@tonic-gate set_environ(Lm_list *lml)
35740Sstevel@tonic-gate {
35755950Srie 	Rt_map		*dlmp;
35765950Srie 	Sym		*sym;
35770Sstevel@tonic-gate 	Slookup		sl;
35780Sstevel@tonic-gate 	uint_t		binfo;
35790Sstevel@tonic-gate 
35805950Srie 	/*
35815950Srie 	 * Initialize the symbol lookup data structure.
35825950Srie 	 */
35835950Srie 	SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_ENVIRON), lml->lm_head, lml->lm_head,
35845950Srie 	    ld_entry_cnt, 0, 0, 0, 0, LKUP_WEAK);
35850Sstevel@tonic-gate 
3586*6387Srie 	if (sym = LM_LOOKUP_SYM(lml->lm_head)(&sl, &dlmp, &binfo, 0)) {
35876Srie 		lml->lm_environ = (char ***)sym->st_value;
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 		if (!(FLAGS(dlmp) & FLG_RT_FIXED))
35906Srie 			lml->lm_environ =
35916Srie 			    (char ***)((uintptr_t)lml->lm_environ +
35926Srie 			    (uintptr_t)ADDR(dlmp));
35936Srie 		*(lml->lm_environ) = (char **)environ;
35940Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_ENVIRON;
35950Sstevel@tonic-gate 	}
35960Sstevel@tonic-gate }
35970Sstevel@tonic-gate 
35980Sstevel@tonic-gate /*
35990Sstevel@tonic-gate  * Determine whether we have a secure executable.  Uid and gid information
36000Sstevel@tonic-gate  * can be passed to us via the aux vector, however if these values are -1
36010Sstevel@tonic-gate  * then use the appropriate system call to obtain them.
36020Sstevel@tonic-gate  *
36030Sstevel@tonic-gate  *  o	If the user is the root they can do anything
36040Sstevel@tonic-gate  *
36050Sstevel@tonic-gate  *  o	If the real and effective uid's don't match, or the real and
36060Sstevel@tonic-gate  *	effective gid's don't match then this is determined to be a `secure'
36070Sstevel@tonic-gate  *	application.
36080Sstevel@tonic-gate  *
36090Sstevel@tonic-gate  * This function is called prior to any dependency processing (see _setup.c).
36100Sstevel@tonic-gate  * Any secure setting will remain in effect for the life of the process.
36110Sstevel@tonic-gate  */
36120Sstevel@tonic-gate void
36130Sstevel@tonic-gate security(uid_t uid, uid_t euid, gid_t gid, gid_t egid, int auxflags)
36140Sstevel@tonic-gate {
36150Sstevel@tonic-gate #ifdef AT_SUN_AUXFLAGS
36160Sstevel@tonic-gate 	if (auxflags != -1) {
36170Sstevel@tonic-gate 		if ((auxflags & AF_SUN_SETUGID) != 0)
36180Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
36190Sstevel@tonic-gate 		return;
36200Sstevel@tonic-gate 	}
36210Sstevel@tonic-gate #endif
36224362Srie 	if (uid == (uid_t)-1)
36230Sstevel@tonic-gate 		uid = getuid();
36240Sstevel@tonic-gate 	if (uid) {
36254362Srie 		if (euid == (uid_t)-1)
36260Sstevel@tonic-gate 			euid = geteuid();
36270Sstevel@tonic-gate 		if (uid != euid)
36280Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
36290Sstevel@tonic-gate 		else {
36304362Srie 			if (gid == (gid_t)-1)
36310Sstevel@tonic-gate 				gid = getgid();
36324362Srie 			if (egid == (gid_t)-1)
36330Sstevel@tonic-gate 				egid = getegid();
36340Sstevel@tonic-gate 			if (gid != egid)
36350Sstevel@tonic-gate 				rtld_flags |= RT_FL_SECURE;
36360Sstevel@tonic-gate 		}
36370Sstevel@tonic-gate 	}
36380Sstevel@tonic-gate }
36390Sstevel@tonic-gate 
36400Sstevel@tonic-gate /*
36410Sstevel@tonic-gate  * _REENTRANT code gets errno redefined to a function so provide for return
36420Sstevel@tonic-gate  * of the thread errno if applicable.  This has no meaning in ld.so.1 which
36430Sstevel@tonic-gate  * is basically singled threaded.  Provide the interface for our dependencies.
36440Sstevel@tonic-gate  */
36450Sstevel@tonic-gate #undef errno
36460Sstevel@tonic-gate #pragma weak _private___errno = ___errno
36470Sstevel@tonic-gate int *
36480Sstevel@tonic-gate ___errno()
36490Sstevel@tonic-gate {
36500Sstevel@tonic-gate 	extern	int	errno;
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	return (&errno);
36530Sstevel@tonic-gate }
36540Sstevel@tonic-gate 
36550Sstevel@tonic-gate /*
36560Sstevel@tonic-gate  * The interface with the c library which is supplied through libdl.so.1.
36570Sstevel@tonic-gate  * A non-null argument allows a function pointer array to be passed to us which
36580Sstevel@tonic-gate  * is used to re-initialize the linker libc table.
36590Sstevel@tonic-gate  */
36600Sstevel@tonic-gate void
36610Sstevel@tonic-gate _ld_libc(void * ptr)
36620Sstevel@tonic-gate {
36630Sstevel@tonic-gate 	get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr);
36640Sstevel@tonic-gate }
36650Sstevel@tonic-gate 
36660Sstevel@tonic-gate /*
36670Sstevel@tonic-gate  * Determine whether a symbol name should be demangled.
36680Sstevel@tonic-gate  */
36690Sstevel@tonic-gate const char *
36700Sstevel@tonic-gate demangle(const char *name)
36710Sstevel@tonic-gate {
36720Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DEMANGLE)
36731618Srie 		return (conv_demangle_name(name));
36740Sstevel@tonic-gate 	else
36750Sstevel@tonic-gate 		return (name);
36760Sstevel@tonic-gate }
3677