xref: /onnv-gate/usr/src/cmd/sgs/rtld/common/util.c (revision 9577:dca915fd85d3)
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 /*
238598SRod.Evans@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
266812Sraf 
276812Sraf /*
286812Sraf  *	Copyright (c) 1988 AT&T
296812Sraf  *	  All Rights Reserved
306812Sraf  */
316812Sraf 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Utility routines for run-time linker.  some are duplicated here from libc
340Sstevel@tonic-gate  * (with different names) to avoid name space collisions.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate #include	<stdio.h>
37*9577SRod.Evans@Sun.COM #include	<sys/time.h>
380Sstevel@tonic-gate #include	<sys/types.h>
390Sstevel@tonic-gate #include	<sys/mman.h>
400Sstevel@tonic-gate #include	<sys/lwp.h>
410Sstevel@tonic-gate #include	<sys/debug.h>
420Sstevel@tonic-gate #include	<stdarg.h>
430Sstevel@tonic-gate #include	<fcntl.h>
440Sstevel@tonic-gate #include	<string.h>
450Sstevel@tonic-gate #include	<dlfcn.h>
460Sstevel@tonic-gate #include	<unistd.h>
470Sstevel@tonic-gate #include	<stdlib.h>
488598SRod.Evans@Sun.COM #include	<sys/auxv.h>
497668SRod.Evans@Sun.COM #include	<limits.h>
501618Srie #include	<debug.h>
511618Srie #include	<conv.h>
520Sstevel@tonic-gate #include	"_rtld.h"
530Sstevel@tonic-gate #include	"_audit.h"
541824Srie #include	"_elf.h"
550Sstevel@tonic-gate #include	"msg.h"
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static int ld_flags_env(const char *, Word *, Word *, uint_t, int);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Null function used as place where a debugger can set a breakpoint.
610Sstevel@tonic-gate  */
620Sstevel@tonic-gate void
631618Srie rtld_db_dlactivity(Lm_list *lml)
640Sstevel@tonic-gate {
651618Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
661618Srie 	    r_debug.rtd_rdebug.r_state));
670Sstevel@tonic-gate }
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * Null function used as place where debugger can set a pre .init
710Sstevel@tonic-gate  * processing breakpoint.
720Sstevel@tonic-gate  */
730Sstevel@tonic-gate void
741618Srie rtld_db_preinit(Lm_list *lml)
750Sstevel@tonic-gate {
761618Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
771618Srie 	    r_debug.rtd_rdebug.r_state));
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Null function used as place where debugger can set a post .init
820Sstevel@tonic-gate  * processing breakpoint.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate void
851618Srie rtld_db_postinit(Lm_list *lml)
860Sstevel@tonic-gate {
871618Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
881618Srie 	    r_debug.rtd_rdebug.r_state));
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /*
920Sstevel@tonic-gate  * Debugger Event Notification
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * This function centralizes all debugger event notification (ala rtld_db).
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * There's a simple intent, focused on insuring the primary link-map control
970Sstevel@tonic-gate  * list (or each link-map list) is consistent, and the indication that objects
980Sstevel@tonic-gate  * have been added or deleted from this list.  Although an RD_ADD and RD_DELETE
990Sstevel@tonic-gate  * event are posted for each of these, most debuggers don't care, as their
1000Sstevel@tonic-gate  * view is that these events simply convey an "inconsistent" state.
1010Sstevel@tonic-gate  *
1020Sstevel@tonic-gate  * We also don't want to trigger multiple RD_ADD/RD_DELETE events any time we
1030Sstevel@tonic-gate  * enter ld.so.1.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  * With auditors, we may be in the process of relocating a collection of
1060Sstevel@tonic-gate  * objects, and will leave() ld.so.1 to call the auditor.  At this point we
1070Sstevel@tonic-gate  * must indicate an RD_CONSISTENT event, but librtld_db will not report an
1080Sstevel@tonic-gate  * object to the debuggers until relocation processing has been completed on it.
1090Sstevel@tonic-gate  * To allow for the collection of these objects that are pending relocation, an
1100Sstevel@tonic-gate  * RD_ADD event is set after completing a series of relocations on the primary
1110Sstevel@tonic-gate  * link-map control list.
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * Set an RD_ADD/RD_DELETE event and indicate that an RD_CONSISTENT event is
1140Sstevel@tonic-gate  * required later (LML_FLG_DBNOTIF):
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  *  i	the first time we add or delete an object to the primary link-map
1170Sstevel@tonic-gate  *	control list.
1180Sstevel@tonic-gate  *  ii	the first time we move a secondary link-map control list to the primary
1190Sstevel@tonic-gate  *	link-map control list (effectively, this is like adding a group of
1200Sstevel@tonic-gate  *	objects to the primary link-map control list).
1210Sstevel@tonic-gate  *
1220Sstevel@tonic-gate  * Set an RD_CONSISTENT event when it is required (LML_FLG_DBNOTIF is set) and
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  *  i	each time we leave the runtime linker.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate void
1270Sstevel@tonic-gate rd_event(Lm_list *lml, rd_event_e event, r_state_e state)
1280Sstevel@tonic-gate {
1291824Srie 	void	(*fptr)(Lm_list *);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	switch (event) {
1320Sstevel@tonic-gate 	case RD_PREINIT:
1330Sstevel@tonic-gate 		fptr = rtld_db_preinit;
1340Sstevel@tonic-gate 		break;
1350Sstevel@tonic-gate 	case RD_POSTINIT:
1360Sstevel@tonic-gate 		fptr = rtld_db_postinit;
1370Sstevel@tonic-gate 		break;
1380Sstevel@tonic-gate 	case RD_DLACTIVITY:
1390Sstevel@tonic-gate 		switch (state) {
1400Sstevel@tonic-gate 		case RT_CONSISTENT:
1410Sstevel@tonic-gate 			lml->lm_flags &= ~LML_FLG_DBNOTIF;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 			/*
1440Sstevel@tonic-gate 			 * Do we need to send a notification?
1450Sstevel@tonic-gate 			 */
1460Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_DBNOTIF) == 0)
1470Sstevel@tonic-gate 				return;
1480Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_DBNOTIF;
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 		case RT_ADD:
1510Sstevel@tonic-gate 		case RT_DELETE:
1520Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_DBNOTIF;
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 			/*
1550Sstevel@tonic-gate 			 * If we are already in an inconsistent state, no
1560Sstevel@tonic-gate 			 * notification is required.
1570Sstevel@tonic-gate 			 */
1580Sstevel@tonic-gate 			if (rtld_flags & RT_FL_DBNOTIF)
1590Sstevel@tonic-gate 				return;
1600Sstevel@tonic-gate 			rtld_flags |= RT_FL_DBNOTIF;
1610Sstevel@tonic-gate 			break;
1620Sstevel@tonic-gate 		};
1630Sstevel@tonic-gate 		fptr = rtld_db_dlactivity;
1640Sstevel@tonic-gate 		break;
1650Sstevel@tonic-gate 	default:
1660Sstevel@tonic-gate 		/*
1670Sstevel@tonic-gate 		 * RD_NONE - do nothing
1680Sstevel@tonic-gate 		 */
1690Sstevel@tonic-gate 		break;
1700Sstevel@tonic-gate 	};
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/*
1730Sstevel@tonic-gate 	 * Set event state and call 'notification' function.
1740Sstevel@tonic-gate 	 *
1750Sstevel@tonic-gate 	 * The debugging clients have previously been told about these
1760Sstevel@tonic-gate 	 * notification functions and have set breakpoints on them if they
1770Sstevel@tonic-gate 	 * are interested in the notification.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_state = state;
1800Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = event;
1811618Srie 	fptr(lml);
1820Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = RD_NONE;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate 
1853731Srie #if	defined(__sparc) || defined(__x86)
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * Stack Cleanup.
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  * This function is invoked to 'remove' arguments that were passed in on the
1900Sstevel@tonic-gate  * stack.  This is most likely if ld.so.1 was invoked directly.  In that case
1910Sstevel@tonic-gate  * we want to remove ld.so.1 as well as it's arguments from the argv[] array.
1920Sstevel@tonic-gate  * Which means we then need to slide everything above it on the stack down
1930Sstevel@tonic-gate  * accordingly.
1940Sstevel@tonic-gate  *
1953731Srie  * While the stack layout is platform specific - it just so happens that __x86,
1963731Srie  * and __sparc platforms share the following initial stack layout.
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  *	!_______________________!  high addresses
1990Sstevel@tonic-gate  *	!			!
2000Sstevel@tonic-gate  *	!	Information	!
2010Sstevel@tonic-gate  *	!	Block		!
2020Sstevel@tonic-gate  *	!	(size varies)	!
2030Sstevel@tonic-gate  *	!_______________________!
2040Sstevel@tonic-gate  *	!	0 word		!
2050Sstevel@tonic-gate  *	!_______________________!
2060Sstevel@tonic-gate  *	!	Auxiliary	!
2070Sstevel@tonic-gate  *	!	vector		!
2080Sstevel@tonic-gate  *	!	2 word entries	!
2090Sstevel@tonic-gate  *	!			!
2100Sstevel@tonic-gate  *	!_______________________!
2110Sstevel@tonic-gate  *	!	0 word		!
2120Sstevel@tonic-gate  *	!_______________________!
2130Sstevel@tonic-gate  *	!	Environment	!
2140Sstevel@tonic-gate  *	!	pointers	!
2150Sstevel@tonic-gate  *	!	...		!
2160Sstevel@tonic-gate  *	!	(one word each)	!
2170Sstevel@tonic-gate  *	!_______________________!
2180Sstevel@tonic-gate  *	!	0 word		!
2190Sstevel@tonic-gate  *	!_______________________!
2200Sstevel@tonic-gate  *	!	Argument	! low addresses
2210Sstevel@tonic-gate  *	!	pointers	!
2220Sstevel@tonic-gate  *	!	Argc words	!
2230Sstevel@tonic-gate  *	!_______________________!
2240Sstevel@tonic-gate  *	!			!
2250Sstevel@tonic-gate  *	!	Argc		!
2260Sstevel@tonic-gate  *	!_______________________!
2270Sstevel@tonic-gate  *	!	...		!
2280Sstevel@tonic-gate  *
2290Sstevel@tonic-gate  */
2300Sstevel@tonic-gate static void
2316Srie stack_cleanup(char **argv, char ***envp, auxv_t **auxv, int rmcnt)
2320Sstevel@tonic-gate {
2336Srie 	int		ndx;
2340Sstevel@tonic-gate 	long		*argc;
2356Srie 	char		**oargv, **nargv;
2366Srie 	char		**oenvp, **nenvp;
2376Srie 	auxv_t		*oauxv, *nauxv;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/*
2406Srie 	 * Slide ARGV[] and update argc.  The argv pointer remains the same,
2416Srie 	 * however slide the applications arguments over the arguments to
2426Srie 	 * ld.so.1.
2430Sstevel@tonic-gate 	 */
2446Srie 	nargv = &argv[0];
2456Srie 	oargv = &argv[rmcnt];
2466Srie 
2476Srie 	for (ndx = 0; oargv[ndx]; ndx++)
2486Srie 		nargv[ndx] = oargv[ndx];
2496Srie 	nargv[ndx] = oargv[ndx];
2506Srie 
2516Srie 	argc = (long *)((uintptr_t)argv - sizeof (long *));
2520Sstevel@tonic-gate 	*argc -= rmcnt;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	/*
2556Srie 	 * Slide ENVP[], and update the environment array pointer.
2560Sstevel@tonic-gate 	 */
2576Srie 	ndx++;
2586Srie 	nenvp = &nargv[ndx];
2596Srie 	oenvp = &oargv[ndx];
2606Srie 	*envp = nenvp;
2616Srie 
2626Srie 	for (ndx = 0; oenvp[ndx]; ndx++)
2636Srie 		nenvp[ndx] = oenvp[ndx];
2646Srie 	nenvp[ndx] = oenvp[ndx];
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	/*
2676Srie 	 * Slide AUXV[], and update the aux vector pointer.
2680Sstevel@tonic-gate 	 */
2696Srie 	ndx++;
2706Srie 	nauxv = (auxv_t *)&nenvp[ndx];
2716Srie 	oauxv = (auxv_t *)&oenvp[ndx];
2726Srie 	*auxv = nauxv;
2736Srie 
2746Srie 	for (ndx = 0; (oauxv[ndx].a_type != AT_NULL); ndx++)
2756Srie 		nauxv[ndx] = oauxv[ndx];
2766Srie 	nauxv[ndx] = oauxv[ndx];
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate #else
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * Verify that the above routine is appropriate for any new platforms.
2810Sstevel@tonic-gate  */
2820Sstevel@tonic-gate #error	unsupported architecture!
2830Sstevel@tonic-gate #endif
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /*
2866Srie  * The only command line argument recognized is -e, followed by a runtime
2876Srie  * linker environment variable.
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate int
2906Srie rtld_getopt(char **argv, char ***envp, auxv_t **auxv, Word *lmflags,
2916Srie     Word *lmtflags, int aout)
2920Sstevel@tonic-gate {
2936Srie 	int	ndx;
2946Srie 
2956Srie 	for (ndx = 1; argv[ndx]; ndx++) {
2960Sstevel@tonic-gate 		char	*str;
2970Sstevel@tonic-gate 
2986Srie 		if (argv[ndx][0] != '-')
2990Sstevel@tonic-gate 			break;
3000Sstevel@tonic-gate 
3016Srie 		if (argv[ndx][1] == '\0') {
3026Srie 			ndx++;
3030Sstevel@tonic-gate 			break;
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 
3066Srie 		if (argv[ndx][1] != 'e')
3076Srie 			return (1);
3086Srie 
3096Srie 		if (argv[ndx][2] == '\0') {
3106Srie 			ndx++;
3116Srie 			if (argv[ndx] == NULL)
3126Srie 				return (1);
3136Srie 			str = argv[ndx];
3140Sstevel@tonic-gate 		} else
3156Srie 			str = &argv[ndx][2];
3166Srie 
3176Srie 		/*
3186Srie 		 * If the environment variable starts with LD_, strip the LD_.
3196Srie 		 * Otherwise, take things as is.
3206Srie 		 */
3216Srie 		if ((str[0] == 'L') && (str[1] == 'D') && (str[2] == '_') &&
3226Srie 		    (str[3] != '\0'))
3236Srie 			str += 3;
3240Sstevel@tonic-gate 		if (ld_flags_env(str, lmflags, lmtflags, 0, aout) == 1)
3250Sstevel@tonic-gate 			return (1);
3260Sstevel@tonic-gate 	}
3270Sstevel@tonic-gate 
3286Srie 	/*
3296Srie 	 * Make sure an object file has been specified.
3306Srie 	 */
3318883SRod.Evans@Sun.COM 	if (argv[ndx] == NULL)
3320Sstevel@tonic-gate 		return (1);
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 	/*
3350Sstevel@tonic-gate 	 * Having gotten the arguments, clean ourselves off of the stack.
3360Sstevel@tonic-gate 	 */
3376Srie 	stack_cleanup(argv, envp, auxv, ndx);
3380Sstevel@tonic-gate 	return (0);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate /*
3426387Srie  * Compare function for PathNode AVL tree.
3430Sstevel@tonic-gate  */
3440Sstevel@tonic-gate static int
3456387Srie pnavl_compare(const void *n1, const void *n2)
3460Sstevel@tonic-gate {
3470Sstevel@tonic-gate 	uint_t		hash1, hash2;
3480Sstevel@tonic-gate 	const char	*st1, *st2;
3490Sstevel@tonic-gate 	int		rc;
3500Sstevel@tonic-gate 
3516387Srie 	hash1 = ((PathNode *)n1)->pn_hash;
3526387Srie 	hash2 = ((PathNode *)n2)->pn_hash;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (hash1 > hash2)
3550Sstevel@tonic-gate 		return (1);
3560Sstevel@tonic-gate 	if (hash1 < hash2)
3570Sstevel@tonic-gate 		return (-1);
3580Sstevel@tonic-gate 
3596387Srie 	st1 = ((PathNode *)n1)->pn_name;
3606387Srie 	st2 = ((PathNode *)n2)->pn_name;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	rc = strcmp(st1, st2);
3630Sstevel@tonic-gate 	if (rc > 0)
3640Sstevel@tonic-gate 		return (1);
3650Sstevel@tonic-gate 	if (rc < 0)
3660Sstevel@tonic-gate 		return (-1);
3670Sstevel@tonic-gate 	return (0);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3717668SRod.Evans@Sun.COM  * Create an AVL tree.
3727668SRod.Evans@Sun.COM  */
3737668SRod.Evans@Sun.COM static avl_tree_t *
3747668SRod.Evans@Sun.COM pnavl_create(size_t size)
3757668SRod.Evans@Sun.COM {
3767668SRod.Evans@Sun.COM 	avl_tree_t	*avlt;
3777668SRod.Evans@Sun.COM 
3787668SRod.Evans@Sun.COM 	if ((avlt = malloc(sizeof (avl_tree_t))) == NULL)
3797668SRod.Evans@Sun.COM 		return (NULL);
3807668SRod.Evans@Sun.COM 	avl_create(avlt, pnavl_compare, size, SGSOFFSETOF(PathNode, pn_avl));
3817668SRod.Evans@Sun.COM 	return (avlt);
3827668SRod.Evans@Sun.COM }
3837668SRod.Evans@Sun.COM 
3847668SRod.Evans@Sun.COM /*
3856387Srie  * Determine if a pathname has already been recorded on the full path name
3866387Srie  * AVL tree.  This tree maintains a node for each path name that ld.so.1 has
3876387Srie  * successfully loaded.  If the path name does not exist in this AVL tree, then
3886387Srie  * the next insertion point is deposited in "where".  This value can be used by
3896387Srie  * fpavl_insert() to expedite the insertion.
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate Rt_map *
3928598SRod.Evans@Sun.COM fpavl_recorded(Lm_list *lml, const char *name, uint_t hash, avl_index_t *where)
3930Sstevel@tonic-gate {
3946387Srie 	FullPathNode	fpn, *fpnp;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	/*
3970Sstevel@tonic-gate 	 * Create the avl tree if required.
3980Sstevel@tonic-gate 	 */
3997668SRod.Evans@Sun.COM 	if ((lml->lm_fpavl == NULL) &&
4007668SRod.Evans@Sun.COM 	    ((lml->lm_fpavl = pnavl_create(sizeof (FullPathNode))) == NULL))
4017668SRod.Evans@Sun.COM 		return (NULL);
4020Sstevel@tonic-gate 
4036387Srie 	fpn.fpn_node.pn_name = name;
4048598SRod.Evans@Sun.COM 	if ((fpn.fpn_node.pn_hash = hash) == 0)
4058598SRod.Evans@Sun.COM 		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 /*
4146387Srie  * 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
4186387Srie  * (see fullpath()), then this name will be recorded as a separate FullPathNode
4196387Srie  * (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 {
4246387Srie 	FullPathNode	*fpnp;
4258598SRod.Evans@Sun.COM 	uint_t		hash = sgs_str_hash(name);
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (where == 0) {
4280Sstevel@tonic-gate 		/* LINTED */
4298598SRod.Evans@Sun.COM 		Rt_map	*_lmp = fpavl_recorded(lml, name, hash, &where);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		/*
4320Sstevel@tonic-gate 		 * We better not get a hit now, we do not want duplicates in
4330Sstevel@tonic-gate 		 * the tree.
4340Sstevel@tonic-gate 		 */
4358883SRod.Evans@Sun.COM 		ASSERT(_lmp == NULL);
4360Sstevel@tonic-gate 	}
4370Sstevel@tonic-gate 
4380Sstevel@tonic-gate 	/*
4396387Srie 	 * Insert new node in tree.
4400Sstevel@tonic-gate 	 */
4417668SRod.Evans@Sun.COM 	if ((fpnp = calloc(sizeof (FullPathNode), 1)) == NULL)
4420Sstevel@tonic-gate 		return (0);
4430Sstevel@tonic-gate 
4446387Srie 	fpnp->fpn_node.pn_name = name;
4458598SRod.Evans@Sun.COM 	fpnp->fpn_node.pn_hash = hash;
4460Sstevel@tonic-gate 	fpnp->fpn_lmp = lmp;
4470Sstevel@tonic-gate 
4485892Sab196087 	if (aplist_append(&FPNODE(lmp), fpnp, AL_CNT_FPNODE) == NULL) {
4490Sstevel@tonic-gate 		free(fpnp);
4500Sstevel@tonic-gate 		return (0);
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	ASSERT(lml->lm_fpavl != NULL);
4540Sstevel@tonic-gate 	avl_insert(lml->lm_fpavl, fpnp, where);
4550Sstevel@tonic-gate 	return (1);
4560Sstevel@tonic-gate }
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate /*
4598598SRod.Evans@Sun.COM  * Remove an object from the FullPathNode AVL tree.
4600Sstevel@tonic-gate  */
4610Sstevel@tonic-gate void
4620Sstevel@tonic-gate fpavl_remove(Rt_map *lmp)
4630Sstevel@tonic-gate {
4646387Srie 	FullPathNode	*fpnp;
4655892Sab196087 	Aliste		idx;
4665892Sab196087 
4675892Sab196087 	for (APLIST_TRAVERSE(FPNODE(lmp), idx, fpnp)) {
4680Sstevel@tonic-gate 		avl_remove(LIST(lmp)->lm_fpavl, fpnp);
4690Sstevel@tonic-gate 		free(fpnp);
4700Sstevel@tonic-gate 	}
4710Sstevel@tonic-gate 	free(FPNODE(lmp));
4725892Sab196087 	FPNODE(lmp) = NULL;
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4756387Srie /*
4766387Srie  * Determine if a pathname has already been recorded on the not-found AVL tree.
4776387Srie  * This tree maintains a node for each path name that ld.so.1 has explicitly
4786387Srie  * inspected, but has failed to load during a single ld.so.1 operation.  If the
4796387Srie  * path name does not exist in this AVL tree, then the next insertion point is
4806387Srie  * deposited in "where".  This value can be used by nfavl_insert() to expedite
4816387Srie  * the insertion.
4826387Srie  */
4836387Srie int
4848598SRod.Evans@Sun.COM nfavl_recorded(const char *name, uint_t hash, avl_index_t *where)
4856387Srie {
4866387Srie 	PathNode	pn;
4876387Srie 
4886387Srie 	/*
4896387Srie 	 * Create the avl tree if required.
4906387Srie 	 */
4917668SRod.Evans@Sun.COM 	if ((nfavl == NULL) &&
4927668SRod.Evans@Sun.COM 	    ((nfavl = pnavl_create(sizeof (PathNode))) == NULL))
4938598SRod.Evans@Sun.COM 		return (NULL);
4946387Srie 
4956387Srie 	pn.pn_name = name;
4968598SRod.Evans@Sun.COM 	if ((pn.pn_hash = hash) == 0)
4978598SRod.Evans@Sun.COM 		pn.pn_hash = sgs_str_hash(name);
4986387Srie 
4997668SRod.Evans@Sun.COM 	if (avl_find(nfavl, &pn, where) == NULL)
5006387Srie 		return (0);
5016387Srie 
5026387Srie 	return (1);
5036387Srie }
5046387Srie 
5056387Srie /*
5066387Srie  * Insert a name into the not-found AVL tree.
5076387Srie  */
5086387Srie void
5096387Srie nfavl_insert(const char *name, avl_index_t where)
5106387Srie {
5116387Srie 	PathNode	*pnp;
5128598SRod.Evans@Sun.COM 	uint_t		hash = sgs_str_hash(name);
5136387Srie 
5146387Srie 	if (where == 0) {
5156387Srie 		/* LINTED */
5168598SRod.Evans@Sun.COM 		int	in_nfavl = nfavl_recorded(name, hash, &where);
5176387Srie 
5186387Srie 		/*
5196387Srie 		 * We better not get a hit now, we do not want duplicates in
5206387Srie 		 * the tree.
5216387Srie 		 */
5226387Srie 		ASSERT(in_nfavl == 0);
5236387Srie 	}
5246387Srie 
5256387Srie 	/*
5266387Srie 	 * Insert new node in tree.
5276387Srie 	 */
5288598SRod.Evans@Sun.COM 	if ((pnp = calloc(sizeof (PathNode), 1)) != NULL) {
5296387Srie 		pnp->pn_name = name;
5308598SRod.Evans@Sun.COM 		pnp->pn_hash = hash;
5316387Srie 		avl_insert(nfavl, pnp, where);
5326387Srie 	}
5336387Srie }
5340Sstevel@tonic-gate 
5357668SRod.Evans@Sun.COM static avl_tree_t	*spavl = NULL;
5367668SRod.Evans@Sun.COM 
5377668SRod.Evans@Sun.COM /*
5387668SRod.Evans@Sun.COM  * Search for a path name within the secure path AVL tree.  This tree is used
5397668SRod.Evans@Sun.COM  * to maintain a list of directories in which the dependencies of a secure
5407668SRod.Evans@Sun.COM  * process have been found.  This list provides a fall-back in the case that a
5417668SRod.Evans@Sun.COM  * $ORIGIN expansion is deemed insecure, when the expansion results in a path
5427668SRod.Evans@Sun.COM  * name that has already provided dependencies.
5437668SRod.Evans@Sun.COM  */
5447668SRod.Evans@Sun.COM int
5457668SRod.Evans@Sun.COM spavl_recorded(const char *name, avl_index_t *where)
5467668SRod.Evans@Sun.COM {
5477668SRod.Evans@Sun.COM 	PathNode	pn;
5487668SRod.Evans@Sun.COM 
5497668SRod.Evans@Sun.COM 	/*
5507668SRod.Evans@Sun.COM 	 * Create the avl tree if required.
5517668SRod.Evans@Sun.COM 	 */
5527668SRod.Evans@Sun.COM 	if ((spavl == NULL) &&
5537668SRod.Evans@Sun.COM 	    ((spavl = pnavl_create(sizeof (PathNode))) == NULL))
5547668SRod.Evans@Sun.COM 		return (0);
5557668SRod.Evans@Sun.COM 
5567668SRod.Evans@Sun.COM 	pn.pn_name = name;
5577668SRod.Evans@Sun.COM 	pn.pn_hash = sgs_str_hash(name);
5587668SRod.Evans@Sun.COM 
5597668SRod.Evans@Sun.COM 	if (avl_find(spavl, &pn, where) == NULL)
5607668SRod.Evans@Sun.COM 		return (0);
5617668SRod.Evans@Sun.COM 
5627668SRod.Evans@Sun.COM 	return (1);
5637668SRod.Evans@Sun.COM }
5647668SRod.Evans@Sun.COM 
5657668SRod.Evans@Sun.COM /*
5667668SRod.Evans@Sun.COM  * Insert the directory name, of a full path name,  into the secure path AVL
5677668SRod.Evans@Sun.COM  * tree.
5687668SRod.Evans@Sun.COM  */
5697668SRod.Evans@Sun.COM void
5707668SRod.Evans@Sun.COM spavl_insert(const char *name)
5717668SRod.Evans@Sun.COM {
5727668SRod.Evans@Sun.COM 	char		buffer[PATH_MAX], *str;
5737668SRod.Evans@Sun.COM 	size_t		size;
5747668SRod.Evans@Sun.COM 	avl_index_t	where;
5757668SRod.Evans@Sun.COM 	PathNode	*pnp;
5767668SRod.Evans@Sun.COM 
5777668SRod.Evans@Sun.COM 	/*
5787668SRod.Evans@Sun.COM 	 * Separate the directory name from the path name.
5797668SRod.Evans@Sun.COM 	 */
5807668SRod.Evans@Sun.COM 	if ((str = strrchr(name, '/')) == name)
5817668SRod.Evans@Sun.COM 		size = 1;
5827668SRod.Evans@Sun.COM 	else
5837668SRod.Evans@Sun.COM 		size = str - name;
5847668SRod.Evans@Sun.COM 
5857668SRod.Evans@Sun.COM 	(void) strncpy(buffer, name, size);
5867668SRod.Evans@Sun.COM 	buffer[size] = '\0';
5877668SRod.Evans@Sun.COM 
5887668SRod.Evans@Sun.COM 	/*
5897668SRod.Evans@Sun.COM 	 * Determine whether this directory name is already recorded, or if
5907668SRod.Evans@Sun.COM 	 * not, 'where" will provide the insertion point for the new string.
5917668SRod.Evans@Sun.COM 	 */
5927668SRod.Evans@Sun.COM 	if (spavl_recorded(buffer, &where))
5937668SRod.Evans@Sun.COM 		return;
5947668SRod.Evans@Sun.COM 
5957668SRod.Evans@Sun.COM 	/*
5967668SRod.Evans@Sun.COM 	 * Insert new node in tree.
5977668SRod.Evans@Sun.COM 	 */
5988883SRod.Evans@Sun.COM 	if ((pnp = calloc(sizeof (PathNode), 1)) != NULL) {
5997668SRod.Evans@Sun.COM 		pnp->pn_name = strdup(buffer);
6007668SRod.Evans@Sun.COM 		pnp->pn_hash = sgs_str_hash(buffer);
6017668SRod.Evans@Sun.COM 		avl_insert(spavl, pnp, where);
6027668SRod.Evans@Sun.COM 	}
6037668SRod.Evans@Sun.COM }
6047668SRod.Evans@Sun.COM 
6050Sstevel@tonic-gate /*
6068598SRod.Evans@Sun.COM  * Inspect the generic string AVL tree for the given string.  If the string is
6078598SRod.Evans@Sun.COM  * not present, duplicate it, and insert the string in the AVL tree.  Return the
6088598SRod.Evans@Sun.COM  * duplicated string to the caller.
6098598SRod.Evans@Sun.COM  *
6108598SRod.Evans@Sun.COM  * These strings are maintained for the life of ld.so.1 and represent path
6118598SRod.Evans@Sun.COM  * names, file names, and search paths.  All other AVL trees that maintain
6128598SRod.Evans@Sun.COM  * FullPathNode and not-found path names use the same string pointer
6138598SRod.Evans@Sun.COM  * established for this string.
6148598SRod.Evans@Sun.COM  */
6158598SRod.Evans@Sun.COM static avl_tree_t	*stravl = NULL;
6168598SRod.Evans@Sun.COM static char		*strbuf = NULL;
6178598SRod.Evans@Sun.COM static PathNode		*pnbuf = NULL;
6188598SRod.Evans@Sun.COM static size_t		strsize = 0, pnsize = 0;
6198598SRod.Evans@Sun.COM 
6208598SRod.Evans@Sun.COM const char *
6218598SRod.Evans@Sun.COM stravl_insert(const char *name, uint_t hash, size_t nsize, int substr)
6228598SRod.Evans@Sun.COM {
6238598SRod.Evans@Sun.COM 	char		str[PATH_MAX];
6248598SRod.Evans@Sun.COM 	PathNode	*pnp;
6258598SRod.Evans@Sun.COM 	avl_index_t	where;
6268598SRod.Evans@Sun.COM 
6278598SRod.Evans@Sun.COM 	/*
6288598SRod.Evans@Sun.COM 	 * Create the avl tree if required.
6298598SRod.Evans@Sun.COM 	 */
6308598SRod.Evans@Sun.COM 	if ((stravl == NULL) &&
6318598SRod.Evans@Sun.COM 	    ((stravl = pnavl_create(sizeof (PathNode))) == NULL))
6328598SRod.Evans@Sun.COM 		return (NULL);
6338598SRod.Evans@Sun.COM 
6348598SRod.Evans@Sun.COM 	/*
6358598SRod.Evans@Sun.COM 	 * Determine the string size if not provided by the caller.
6368598SRod.Evans@Sun.COM 	 */
6378598SRod.Evans@Sun.COM 	if (nsize == 0)
6388598SRod.Evans@Sun.COM 		nsize = strlen(name) + 1;
6398598SRod.Evans@Sun.COM 	else if (substr) {
6408598SRod.Evans@Sun.COM 		/*
6418598SRod.Evans@Sun.COM 		 * The string passed to us may be a multiple path string for
6428598SRod.Evans@Sun.COM 		 * which we only need the first component.  Using the provided
6438598SRod.Evans@Sun.COM 		 * size, strip out the required string.
6448598SRod.Evans@Sun.COM 		 */
6458598SRod.Evans@Sun.COM 		(void) strncpy(str, name, nsize);
6468598SRod.Evans@Sun.COM 		str[nsize - 1] = '\0';
6478598SRod.Evans@Sun.COM 		name = str;
6488598SRod.Evans@Sun.COM 	}
6498598SRod.Evans@Sun.COM 
6508598SRod.Evans@Sun.COM 	/*
6518598SRod.Evans@Sun.COM 	 * Allocate a PathNode buffer if one doesn't exist, or any existing
6528598SRod.Evans@Sun.COM 	 * buffer has been used up.
6538598SRod.Evans@Sun.COM 	 */
6548598SRod.Evans@Sun.COM 	if ((pnbuf == NULL) || (sizeof (PathNode) > pnsize)) {
6558598SRod.Evans@Sun.COM 		pnsize = syspagsz;
6568598SRod.Evans@Sun.COM 		if ((pnbuf = dz_map(0, 0, pnsize, (PROT_READ | PROT_WRITE),
6578598SRod.Evans@Sun.COM 		    MAP_PRIVATE)) == MAP_FAILED)
6588598SRod.Evans@Sun.COM 			return (NULL);
6598598SRod.Evans@Sun.COM 	}
6608598SRod.Evans@Sun.COM 	/*
6618598SRod.Evans@Sun.COM 	 * Determine whether this string already exists.
6628598SRod.Evans@Sun.COM 	 */
6638598SRod.Evans@Sun.COM 	pnbuf->pn_name = name;
6648598SRod.Evans@Sun.COM 	if ((pnbuf->pn_hash = hash) == 0)
6658598SRod.Evans@Sun.COM 		pnbuf->pn_hash = sgs_str_hash(name);
6668598SRod.Evans@Sun.COM 
6678598SRod.Evans@Sun.COM 	if ((pnp = avl_find(stravl, pnbuf, &where)) != NULL)
6688598SRod.Evans@Sun.COM 		return (pnp->pn_name);
6698598SRod.Evans@Sun.COM 
6708598SRod.Evans@Sun.COM 	/*
6718598SRod.Evans@Sun.COM 	 * Allocate a string buffer if one does not exist, or if there is
6728598SRod.Evans@Sun.COM 	 * insufficient space for the new string in any existing buffer.
6738598SRod.Evans@Sun.COM 	 */
6748598SRod.Evans@Sun.COM 	if ((strbuf == NULL) || (nsize > strsize)) {
6758598SRod.Evans@Sun.COM 		strsize = S_ROUND(nsize, syspagsz);
6768598SRod.Evans@Sun.COM 
6778598SRod.Evans@Sun.COM 		if ((strbuf = dz_map(0, 0, strsize, (PROT_READ | PROT_WRITE),
6788598SRod.Evans@Sun.COM 		    MAP_PRIVATE)) == MAP_FAILED)
6798598SRod.Evans@Sun.COM 			return (NULL);
6808598SRod.Evans@Sun.COM 	}
6818598SRod.Evans@Sun.COM 
6828598SRod.Evans@Sun.COM 	(void) memcpy(strbuf, name, nsize);
6838598SRod.Evans@Sun.COM 	pnp = pnbuf;
6848598SRod.Evans@Sun.COM 	pnp->pn_name = strbuf;
6858598SRod.Evans@Sun.COM 	avl_insert(stravl, pnp, where);
6868598SRod.Evans@Sun.COM 
6878598SRod.Evans@Sun.COM 	strbuf += nsize;
6888598SRod.Evans@Sun.COM 	strsize -= nsize;
6898598SRod.Evans@Sun.COM 	pnbuf++;
6908598SRod.Evans@Sun.COM 	pnsize -= sizeof (PathNode);
6918598SRod.Evans@Sun.COM 	return (pnp->pn_name);
6928598SRod.Evans@Sun.COM }
6938598SRod.Evans@Sun.COM 
6948598SRod.Evans@Sun.COM /*
6950Sstevel@tonic-gate  * Prior to calling an object, either via a .plt or through dlsym(), make sure
6960Sstevel@tonic-gate  * its .init has fired.  Through topological sorting, ld.so.1 attempts to fire
6970Sstevel@tonic-gate  * init's in the correct order, however, this order is typically based on needed
6980Sstevel@tonic-gate  * dependencies and non-lazy relocation bindings.  Lazy relocations (.plts) can
6990Sstevel@tonic-gate  * still occur and result in bindings that were not captured during topological
7000Sstevel@tonic-gate  * sorting.  This routine compensates for this lack of binding information, and
7010Sstevel@tonic-gate  * provides for dynamic .init firing.
7020Sstevel@tonic-gate  */
7030Sstevel@tonic-gate void
7046387Srie is_dep_init(Rt_map *dlmp, Rt_map *clmp)
7050Sstevel@tonic-gate {
7066387Srie 	Rt_map	**tobj;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/*
7090Sstevel@tonic-gate 	 * If the caller is an auditor, and the destination isn't, then don't
7100Sstevel@tonic-gate 	 * run any .inits (see comments in load_completion()).
7110Sstevel@tonic-gate 	 */
7120Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
7130Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
7140Sstevel@tonic-gate 		return;
7150Sstevel@tonic-gate 
7168598SRod.Evans@Sun.COM 	if ((dlmp == clmp) || (rtld_flags & RT_FL_INITFIRST))
7170Sstevel@tonic-gate 		return;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITDONE)) ==
7200Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITDONE))
7210Sstevel@tonic-gate 		return;
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITCALL)) ==
7240Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITCALL)) {
7251618Srie 		DBG_CALL(Dbg_util_no_init(dlmp));
7260Sstevel@tonic-gate 		return;
7270Sstevel@tonic-gate 	}
7280Sstevel@tonic-gate 
7290Sstevel@tonic-gate 	if ((tobj = calloc(2, sizeof (Rt_map *))) != NULL) {
7300Sstevel@tonic-gate 		tobj[0] = dlmp;
7310Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_DYN);
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate /*
7360Sstevel@tonic-gate  * Execute .{preinit|init|fini}array sections
7370Sstevel@tonic-gate  */
7380Sstevel@tonic-gate void
7391618Srie call_array(Addr *array, uint_t arraysz, Rt_map *lmp, Word shtype)
7400Sstevel@tonic-gate {
7411618Srie 	int	start, stop, incr, ndx;
7420Sstevel@tonic-gate 	uint_t	arraycnt = (uint_t)(arraysz / sizeof (Addr));
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	if (array == NULL)
7450Sstevel@tonic-gate 		return;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/*
7480Sstevel@tonic-gate 	 * initarray & preinitarray are walked from beginning to end - while
7490Sstevel@tonic-gate 	 * finiarray is walked from end to beginning.
7500Sstevel@tonic-gate 	 */
7510Sstevel@tonic-gate 	if (shtype == SHT_FINI_ARRAY) {
7520Sstevel@tonic-gate 		start = arraycnt - 1;
7530Sstevel@tonic-gate 		stop = incr = -1;
7540Sstevel@tonic-gate 	} else {
7550Sstevel@tonic-gate 		start = 0;
7560Sstevel@tonic-gate 		stop = arraycnt;
7570Sstevel@tonic-gate 		incr = 1;
7580Sstevel@tonic-gate 	}
7590Sstevel@tonic-gate 
7600Sstevel@tonic-gate 	/*
7610Sstevel@tonic-gate 	 * Call the .*array[] entries
7620Sstevel@tonic-gate 	 */
7631618Srie 	for (ndx = start; ndx != stop; ndx += incr) {
7641824Srie 		void (*fptr)(void) = (void(*)())array[ndx];
7651618Srie 
7661618Srie 		DBG_CALL(Dbg_util_call_array(lmp, (void *)fptr, ndx, shtype));
7671618Srie 
7686652Srie 		leave(LIST(lmp), 0);
7690Sstevel@tonic-gate 		(*fptr)();
7706652Srie 		(void) enter(0);
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate /*
7760Sstevel@tonic-gate  * Execute any .init sections.  These are passed to us in an lmp array which
7770Sstevel@tonic-gate  * (by default) will have been sorted.
7780Sstevel@tonic-gate  */
7790Sstevel@tonic-gate void
7806387Srie call_init(Rt_map **tobj, int flag)
7810Sstevel@tonic-gate {
7826387Srie 	Rt_map		**_tobj, **_nobj;
7839131SRod.Evans@Sun.COM 	static APlist	*pending = NULL;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * If we're in the middle of an INITFIRST, this must complete before
7870Sstevel@tonic-gate 	 * any new init's are fired.  In this case add the object list to the
7880Sstevel@tonic-gate 	 * pending queue and return.  We'll pick up the queue after any
7890Sstevel@tonic-gate 	 * INITFIRST objects have their init's fired.
7900Sstevel@tonic-gate 	 */
7910Sstevel@tonic-gate 	if (rtld_flags & RT_FL_INITFIRST) {
7929131SRod.Evans@Sun.COM 		(void) aplist_append(&pending, tobj, AL_CNT_PENDING);
7930Sstevel@tonic-gate 		return;
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	/*
7970Sstevel@tonic-gate 	 * Traverse the tobj array firing each objects init.
7980Sstevel@tonic-gate 	 */
7990Sstevel@tonic-gate 	for (_tobj = _nobj = tobj, _nobj++; *_tobj != NULL; _tobj++, _nobj++) {
8006387Srie 		Rt_map	*lmp = *_tobj;
8016387Srie 		void	(*iptr)() = INIT(lmp);
8029340SRod.Evans@Sun.COM 		uint_t	rtldflags;
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITCALL)
8050Sstevel@tonic-gate 			continue;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITCALL;
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 		/*
8109340SRod.Evans@Sun.COM 		 * It is possible, that during the initial handshake with libc,
8119340SRod.Evans@Sun.COM 		 * an interposition object has resolved a symbol binding, and
8129340SRod.Evans@Sun.COM 		 * that this objects .init must be fired.  As we're about to
8139340SRod.Evans@Sun.COM 		 * run user code, make sure any dynamic linking errors remain
8149340SRod.Evans@Sun.COM 		 * internal (ie., only obtainable from dlerror()), and are not
8159340SRod.Evans@Sun.COM 		 * flushed to stderr.
8169340SRod.Evans@Sun.COM 		 */
8179340SRod.Evans@Sun.COM 		rtldflags = (rtld_flags & RT_FL_APPLIC) ? 0 : RT_FL_APPLIC;
8189340SRod.Evans@Sun.COM 		rtld_flags |= rtldflags;
8199340SRod.Evans@Sun.COM 
8209340SRod.Evans@Sun.COM 		/*
8210Sstevel@tonic-gate 		 * Establish an initfirst state if necessary - no other inits
822280Srie 		 * will be fired (because of additional relocation bindings)
823280Srie 		 * when in this state.
8240Sstevel@tonic-gate 		 */
8250Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITFRST)
8260Sstevel@tonic-gate 			rtld_flags |= RT_FL_INITFIRST;
8270Sstevel@tonic-gate 
8288598SRod.Evans@Sun.COM 		if (INITARRAY(lmp) || iptr)
8291618Srie 			DBG_CALL(Dbg_util_call_init(lmp, flag));
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 		if (iptr) {
8326652Srie 			leave(LIST(lmp), 0);
8330Sstevel@tonic-gate 			(*iptr)();
8346652Srie 			(void) enter(0);
8350Sstevel@tonic-gate 		}
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 		call_array(INITARRAY(lmp), INITARRAYSZ(lmp), lmp,
8380Sstevel@tonic-gate 		    SHT_INIT_ARRAY);
8390Sstevel@tonic-gate 
8400Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr)
8411618Srie 			DBG_CALL(Dbg_util_call_init(lmp, DBG_INIT_DONE));
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 		/*
8449340SRod.Evans@Sun.COM 		 * Return to a non-application setting if necessary.
8459340SRod.Evans@Sun.COM 		 */
8469340SRod.Evans@Sun.COM 		rtld_flags &= ~rtldflags;
8479340SRod.Evans@Sun.COM 
8489340SRod.Evans@Sun.COM 		/*
8490Sstevel@tonic-gate 		 * Set the initdone flag regardless of whether this object
8500Sstevel@tonic-gate 		 * actually contains an .init section.  This flag prevents us
8510Sstevel@tonic-gate 		 * from processing this section again for an .init and also
8520Sstevel@tonic-gate 		 * signifies that a .fini must be called should it exist.
8530Sstevel@tonic-gate 		 * Clear the sort field for use in later .fini processing.
8540Sstevel@tonic-gate 		 */
8550Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITDONE;
856280Srie 		SORTVAL(lmp) = -1;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 		/*
8590Sstevel@tonic-gate 		 * If we're firing an INITFIRST object, and other objects must
8600Sstevel@tonic-gate 		 * be fired which are not INITFIRST, make sure we grab any
8610Sstevel@tonic-gate 		 * pending objects that might have been delayed as this
8620Sstevel@tonic-gate 		 * INITFIRST was processed.
8630Sstevel@tonic-gate 		 */
8640Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_INITFIRST) &&
8650Sstevel@tonic-gate 		    ((*_nobj == NULL) || !(FLAGS(*_nobj) & FLG_RT_INITFRST))) {
8669131SRod.Evans@Sun.COM 			Aliste	idx;
8679131SRod.Evans@Sun.COM 			Rt_map	**pobj;
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_INITFIRST;
8700Sstevel@tonic-gate 
8719131SRod.Evans@Sun.COM 			for (APLIST_TRAVERSE(pending, idx, pobj)) {
8729131SRod.Evans@Sun.COM 				aplist_delete(pending, &idx);
8730Sstevel@tonic-gate 				call_init(pobj, DBG_INIT_PEND);
8740Sstevel@tonic-gate 			}
8750Sstevel@tonic-gate 		}
8760Sstevel@tonic-gate 	}
8770Sstevel@tonic-gate 	free(tobj);
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate /*
8810Sstevel@tonic-gate  * Function called by atexit(3C).  Calls all .fini sections related with the
8820Sstevel@tonic-gate  * mains dependent shared libraries in the order in which the shared libraries
8830Sstevel@tonic-gate  * have been loaded.  Skip any .fini defined in the main executable, as this
8840Sstevel@tonic-gate  * will be called by crt0 (main was never marked as initdone).
8850Sstevel@tonic-gate  */
8860Sstevel@tonic-gate void
8870Sstevel@tonic-gate call_fini(Lm_list * lml, Rt_map ** tobj)
8880Sstevel@tonic-gate {
889280Srie 	Rt_map **_tobj;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	for (_tobj = tobj; *_tobj != NULL; _tobj++) {
8925892Sab196087 		Rt_map		*clmp, * lmp = *_tobj;
8935892Sab196087 		Aliste		idx;
8945892Sab196087 		Bnd_desc	*bdp;
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 		/*
8978598SRod.Evans@Sun.COM 		 * Only fire a .fini if the objects corresponding .init has
8988598SRod.Evans@Sun.COM 		 * completed.  We collect all .fini sections of objects that
8998598SRod.Evans@Sun.COM 		 * had their .init collected, but that doesn't mean that at
9008598SRod.Evans@Sun.COM 		 * the time of collection, that the .init had completed.
9010Sstevel@tonic-gate 		 */
9028598SRod.Evans@Sun.COM 		if (FLAGS(lmp) & FLG_RT_INITDONE) {
9031824Srie 			void	(*fptr)(void) = FINI(lmp);
9040Sstevel@tonic-gate 
9058598SRod.Evans@Sun.COM 			if (FINIARRAY(lmp) || fptr)
9061618Srie 				DBG_CALL(Dbg_util_call_fini(lmp));
9070Sstevel@tonic-gate 
9081618Srie 			call_array(FINIARRAY(lmp), FINIARRAYSZ(lmp), lmp,
9091618Srie 			    SHT_FINI_ARRAY);
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 			if (fptr) {
9126652Srie 				leave(LIST(lmp), 0);
9130Sstevel@tonic-gate 				(*fptr)();
9146652Srie 				(void) enter(0);
9150Sstevel@tonic-gate 			}
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 		/*
919280Srie 		 * Skip main, this is explicitly called last in atexit_fini().
920280Srie 		 */
921280Srie 		if (FLAGS(lmp) & FLG_RT_ISMAIN)
922280Srie 			continue;
923280Srie 
924280Srie 		/*
9250Sstevel@tonic-gate 		 * Audit `close' operations at this point.  The library has
9260Sstevel@tonic-gate 		 * exercised its last instructions (regardless of whether it
9270Sstevel@tonic-gate 		 * will be unmapped or not).
9280Sstevel@tonic-gate 		 *
9290Sstevel@tonic-gate 		 * First call any global auditing.
9300Sstevel@tonic-gate 		 */
9310Sstevel@tonic-gate 		if (lml->lm_tflags & LML_TFLG_AUD_OBJCLOSE)
9329131SRod.Evans@Sun.COM 			_audit_objclose(auditors->ad_list, lmp);
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 		/*
9350Sstevel@tonic-gate 		 * Finally determine whether this object has local auditing
9360Sstevel@tonic-gate 		 * requirements by inspecting itself and then its dependencies.
9370Sstevel@tonic-gate 		 */
9380Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_LOCAUDIT) == 0)
9390Sstevel@tonic-gate 			continue;
9400Sstevel@tonic-gate 
9418598SRod.Evans@Sun.COM 		if (AFLAGS(lmp) & LML_TFLG_AUD_OBJCLOSE)
9429131SRod.Evans@Sun.COM 			_audit_objclose(AUDITORS(lmp)->ad_list, lmp);
9430Sstevel@tonic-gate 
9445892Sab196087 		for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
9450Sstevel@tonic-gate 			clmp = bdp->b_caller;
9460Sstevel@tonic-gate 
9478598SRod.Evans@Sun.COM 			if (AFLAGS(clmp) & LML_TFLG_AUD_OBJCLOSE) {
9489131SRod.Evans@Sun.COM 				_audit_objclose(AUDITORS(clmp)->ad_list, lmp);
9494362Srie 				break;
9500Sstevel@tonic-gate 			}
9510Sstevel@tonic-gate 		}
9520Sstevel@tonic-gate 	}
9531618Srie 	DBG_CALL(Dbg_bind_plt_summary(lml, M_MACH, pltcnt21d, pltcnt24d,
9541618Srie 	    pltcntu32, pltcntu44, pltcntfull, pltcntfar));
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate 	free(tobj);
9570Sstevel@tonic-gate }
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate void
9600Sstevel@tonic-gate atexit_fini()
9610Sstevel@tonic-gate {
9629131SRod.Evans@Sun.COM 	Rt_map	**tobj, *lmp;
9639131SRod.Evans@Sun.COM 	Lm_list	*lml;
9649131SRod.Evans@Sun.COM 	Aliste	idx;
9650Sstevel@tonic-gate 
9666515Sraf 	(void) enter(0);
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 	rtld_flags |= RT_FL_ATEXIT;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	lml = &lml_main;
971280Srie 	lml->lm_flags |= LML_FLG_ATEXIT;
9723817Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
9730Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate 	/*
9760Sstevel@tonic-gate 	 * Reverse topologically sort the main link-map for .fini execution.
9770Sstevel@tonic-gate 	 */
9788883SRod.Evans@Sun.COM 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != NULL) &&
9790Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
9800Sstevel@tonic-gate 		call_fini(lml, tobj);
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate 	/*
983280Srie 	 * Add an explicit close to main and ld.so.1.  Although main's .fini is
984280Srie 	 * collected in call_fini() to provide for FINITARRAY processing, its
985280Srie 	 * audit_objclose is explicitly skipped.  This provides for it to be
986280Srie 	 * called last, here.  This is the reverse of the explicit calls to
987280Srie 	 * audit_objopen() made in setup().
9880Sstevel@tonic-gate 	 */
9898598SRod.Evans@Sun.COM 	if ((lml->lm_tflags | AFLAGS(lmp)) & LML_TFLG_AUD_MASK) {
9900Sstevel@tonic-gate 		audit_objclose(lmp, (Rt_map *)lml_rtld.lm_head);
991280Srie 		audit_objclose(lmp, lmp);
9920Sstevel@tonic-gate 	}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 	/*
9950Sstevel@tonic-gate 	 * Now that all .fini code has been run, see what unreferenced objects
9966387Srie 	 * remain.
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	unused(lml);
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	/*
10010Sstevel@tonic-gate 	 * Traverse any alternative link-map lists.
10020Sstevel@tonic-gate 	 */
10039131SRod.Evans@Sun.COM 	for (APLIST_TRAVERSE(dynlm_list, idx, lml)) {
10041824Srie 		/*
10051824Srie 		 * Ignore the base-link-map list, which has already been
10061824Srie 		 * processed, and the runtime linkers link-map list, which is
10071824Srie 		 * typically processed last.
10081824Srie 		 */
10090Sstevel@tonic-gate 		if (lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM))
10100Sstevel@tonic-gate 			continue;
10110Sstevel@tonic-gate 
10128883SRod.Evans@Sun.COM 		if ((lmp = (Rt_map *)lml->lm_head) == NULL)
10130Sstevel@tonic-gate 			continue;
10140Sstevel@tonic-gate 
1015280Srie 		lml->lm_flags |= LML_FLG_ATEXIT;
10163817Srie 		lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
1017280Srie 
10180Sstevel@tonic-gate 		/*
10190Sstevel@tonic-gate 		 * Reverse topologically sort the link-map for .fini execution.
10200Sstevel@tonic-gate 		 */
10218883SRod.Evans@Sun.COM 		if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != NULL) &&
10220Sstevel@tonic-gate 		    (tobj != (Rt_map **)S_ERROR))
10230Sstevel@tonic-gate 			call_fini(lml, tobj);
10240Sstevel@tonic-gate 
10250Sstevel@tonic-gate 		unused(lml);
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	/*
10290Sstevel@tonic-gate 	 * Finally reverse topologically sort the runtime linkers link-map for
10300Sstevel@tonic-gate 	 * .fini execution.
10310Sstevel@tonic-gate 	 */
10320Sstevel@tonic-gate 	lml = &lml_rtld;
1033280Srie 	lml->lm_flags |= LML_FLG_ATEXIT;
10343817Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
10350Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
10360Sstevel@tonic-gate 
10378883SRod.Evans@Sun.COM 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != NULL) &&
10380Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
10390Sstevel@tonic-gate 		call_fini(lml, tobj);
10400Sstevel@tonic-gate 
10416515Sraf 	leave(&lml_main, 0);
10420Sstevel@tonic-gate }
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate  * This routine is called to complete any runtime linker activity which may have
10470Sstevel@tonic-gate  * resulted in objects being loaded.  This is called from all user entry points
10480Sstevel@tonic-gate  * and from any internal dl*() requests.
10490Sstevel@tonic-gate  */
10500Sstevel@tonic-gate void
10514679Srie load_completion(Rt_map *nlmp)
10520Sstevel@tonic-gate {
10538883SRod.Evans@Sun.COM 	Rt_map	**tobj = NULL;
10544679Srie 	Lm_list	*nlml;
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate 	/*
10570Sstevel@tonic-gate 	 * Establish any .init processing.  Note, in a world of lazy loading,
10580Sstevel@tonic-gate 	 * objects may have been loaded regardless of whether the users request
10590Sstevel@tonic-gate 	 * was fulfilled (i.e., a dlsym() request may have failed to find a
10600Sstevel@tonic-gate 	 * symbol but objects might have been loaded during its search).  Thus,
10610Sstevel@tonic-gate 	 * any tsorting starts from the nlmp (new link-maps) pointer and not
10620Sstevel@tonic-gate 	 * necessarily from the link-map that may have satisfied the request.
10630Sstevel@tonic-gate 	 *
10641824Srie 	 * Note, the primary link-map has an initialization phase where dynamic
10651824Srie 	 * .init firing is suppressed.  This provides for a simple and clean
10661824Srie 	 * handshake with the primary link-maps libc, which is important for
10671824Srie 	 * establishing uberdata.  In addition, auditors often obtain handles
10681824Srie 	 * to primary link-map objects as the objects are loaded, so as to
10691824Srie 	 * inspect the link-map for symbols.  This inspection is allowed without
10701824Srie 	 * running any code on the primary link-map, as running this code may
10711824Srie 	 * reenter the auditor, who may not yet have finished its own
10720Sstevel@tonic-gate 	 * initialization.
10730Sstevel@tonic-gate 	 */
10741824Srie 	if (nlmp)
10751824Srie 		nlml = LIST(nlmp);
10761824Srie 
10776229Sedp 	if (nlmp && nlml->lm_init && ((nlml != &lml_main) ||
10786229Sedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
10798598SRod.Evans@Sun.COM 		if ((tobj = tsort(nlmp, nlml->lm_init,
1080280Srie 		    RT_SORT_REV)) == (Rt_map **)S_ERROR)
10818883SRod.Evans@Sun.COM 			tobj = NULL;
10820Sstevel@tonic-gate 	}
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate 	/*
10851824Srie 	 * Make sure any alternative link-map retrieves any external interfaces
10861824Srie 	 * and initializes threads.
10871824Srie 	 */
10881824Srie 	if (nlmp && (nlml != &lml_main)) {
10891824Srie 		(void) rt_get_extern(nlml, nlmp);
10901824Srie 		rt_thr_init(nlml);
10911824Srie 	}
10921824Srie 
10931824Srie 	/*
10941824Srie 	 * Traverse the list of new link-maps and register any dynamic TLS.
10951824Srie 	 * This storage is established for any objects not on the primary
10961824Srie 	 * link-map, and for any objects added to the primary link-map after
10971824Srie 	 * static TLS has been registered.
10981824Srie 	 */
10996229Sedp 	if (nlmp && nlml->lm_tls && ((nlml != &lml_main) ||
11006229Sedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
11011824Srie 		Rt_map	*lmp;
11021824Srie 
11038394SAli.Bahrami@Sun.COM 		for (lmp = nlmp; lmp; lmp = NEXT_RT_MAP(lmp)) {
11041824Srie 			if (PTTLS(lmp) && PTTLS(lmp)->p_memsz)
11051824Srie 				tls_modaddrem(lmp, TM_FLG_MODADD);
11061824Srie 		}
11071824Srie 		nlml->lm_tls = 0;
11081824Srie 	}
11091824Srie 
11101824Srie 	/*
11110Sstevel@tonic-gate 	 * Fire any .init's.
11120Sstevel@tonic-gate 	 */
11130Sstevel@tonic-gate 	if (tobj)
11140Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_SORT);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * Append an item to the specified link map control list.
11190Sstevel@tonic-gate  */
11200Sstevel@tonic-gate void
11210Sstevel@tonic-gate lm_append(Lm_list *lml, Aliste lmco, Rt_map *lmp)
11220Sstevel@tonic-gate {
11230Sstevel@tonic-gate 	Lm_cntl	*lmc;
11240Sstevel@tonic-gate 	int	add = 1;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	/*
11270Sstevel@tonic-gate 	 * Indicate that this link-map list has a new object.
11280Sstevel@tonic-gate 	 */
11290Sstevel@tonic-gate 	(lml->lm_obj)++;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	/*
11322454Srie 	 * If we're about to add a new object to the main link-map control list,
11332454Srie 	 * alert the debuggers that we are about to mess with this list.
11342454Srie 	 * Additions of individual objects to the main link-map control list
11352454Srie 	 * occur during initial setup as the applications immediate dependencies
11362454Srie 	 * are loaded.  Individual objects are also loaded on the main link-map
11372454Srie 	 * control list of new alternative link-map control lists.
11380Sstevel@tonic-gate 	 */
11395892Sab196087 	if ((lmco == ALIST_OFF_DATA) &&
11405892Sab196087 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
11412454Srie 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	/* LINTED */
11445892Sab196087 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, lmco);
11450Sstevel@tonic-gate 
11460Sstevel@tonic-gate 	/*
11470Sstevel@tonic-gate 	 * A link-map list header points to one of more link-map control lists
11480Sstevel@tonic-gate 	 * (see include/rtld.h).  The initial list, pointed to by lm_cntl, is
11490Sstevel@tonic-gate 	 * the list of relocated objects.  Other lists maintain objects that
11500Sstevel@tonic-gate 	 * are still being analyzed or relocated.  This list provides the core
11510Sstevel@tonic-gate 	 * link-map list information used by all ld.so.1 routines.
11520Sstevel@tonic-gate 	 */
11530Sstevel@tonic-gate 	if (lmc->lc_head == NULL) {
11540Sstevel@tonic-gate 		/*
11550Sstevel@tonic-gate 		 * If this is the first link-map for the given control list,
11560Sstevel@tonic-gate 		 * initialize the list.
11570Sstevel@tonic-gate 		 */
11580Sstevel@tonic-gate 		lmc->lc_head = lmc->lc_tail = lmp;
11590Sstevel@tonic-gate 		add = 0;
11600Sstevel@tonic-gate 
11613466Srie 	} else if (FLAGS(lmp) & FLG_RT_OBJINTPO) {
11620Sstevel@tonic-gate 		Rt_map	*tlmp;
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 		/*
11650Sstevel@tonic-gate 		 * If this is an interposer then append the link-map following
11660Sstevel@tonic-gate 		 * any other interposers (these are objects that have been
11670Sstevel@tonic-gate 		 * previously preloaded, or were identified with -z interpose).
11680Sstevel@tonic-gate 		 * Interposers can only be inserted on the first link-map
11690Sstevel@tonic-gate 		 * control list, as once relocation has started, interposition
11700Sstevel@tonic-gate 		 * from new interposers can't be guaranteed.
11710Sstevel@tonic-gate 		 *
11720Sstevel@tonic-gate 		 * NOTE: We do not interpose on the head of a list.  This model
11730Sstevel@tonic-gate 		 * evolved because dynamic executables have already been fully
11740Sstevel@tonic-gate 		 * relocated within themselves and thus can't be interposed on.
11750Sstevel@tonic-gate 		 * Nowadays it's possible to have shared objects at the head of
11760Sstevel@tonic-gate 		 * a list, which conceptually means they could be interposed on.
11770Sstevel@tonic-gate 		 * But, shared objects can be created via dldump() and may only
11780Sstevel@tonic-gate 		 * be partially relocated (just relatives), in which case they
11790Sstevel@tonic-gate 		 * are interposable, but are marked as fixed (ET_EXEC).
11800Sstevel@tonic-gate 		 *
11810Sstevel@tonic-gate 		 * Thus we really don't have a clear method of deciding when the
11820Sstevel@tonic-gate 		 * head of a link-map is interposable.  So, to be consistent,
11830Sstevel@tonic-gate 		 * for now only add interposers after the link-map lists head
11840Sstevel@tonic-gate 		 * object.
11850Sstevel@tonic-gate 		 */
11868394SAli.Bahrami@Sun.COM 		for (tlmp = NEXT_RT_MAP(lmc->lc_head); tlmp;
11878394SAli.Bahrami@Sun.COM 		    tlmp = NEXT_RT_MAP(tlmp)) {
11880Sstevel@tonic-gate 
11893466Srie 			if (FLAGS(tlmp) & FLG_RT_OBJINTPO)
11900Sstevel@tonic-gate 				continue;
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 			/*
11930Sstevel@tonic-gate 			 * Insert the new link-map before this non-interposer,
11940Sstevel@tonic-gate 			 * and indicate an interposer is found.
11950Sstevel@tonic-gate 			 */
11968394SAli.Bahrami@Sun.COM 			NEXT(PREV_RT_MAP(tlmp)) = (Link_map *)lmp;
11970Sstevel@tonic-gate 			PREV(lmp) = PREV(tlmp);
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 			NEXT(lmp) = (Link_map *)tlmp;
12000Sstevel@tonic-gate 			PREV(tlmp) = (Link_map *)lmp;
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 			lmc->lc_flags |= LMC_FLG_REANALYZE;
12030Sstevel@tonic-gate 			add = 0;
12040Sstevel@tonic-gate 			break;
12050Sstevel@tonic-gate 		}
12060Sstevel@tonic-gate 	}
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate 	/*
12090Sstevel@tonic-gate 	 * Fall through to appending the new link map to the tail of the list.
12100Sstevel@tonic-gate 	 * If we're processing the initial objects of this link-map list, add
12110Sstevel@tonic-gate 	 * them to the backward compatibility list.
12120Sstevel@tonic-gate 	 */
12130Sstevel@tonic-gate 	if (add) {
12140Sstevel@tonic-gate 		NEXT(lmc->lc_tail) = (Link_map *)lmp;
12150Sstevel@tonic-gate 		PREV(lmp) = (Link_map *)lmc->lc_tail;
12160Sstevel@tonic-gate 		lmc->lc_tail = lmp;
12170Sstevel@tonic-gate 	}
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 	/*
12200Sstevel@tonic-gate 	 * Having added this link-map to a control list, indicate which control
12210Sstevel@tonic-gate 	 * list the link-map belongs to.  Note, control list information is
12220Sstevel@tonic-gate 	 * always maintained as an offset, as the Alist can be reallocated.
12230Sstevel@tonic-gate 	 */
12240Sstevel@tonic-gate 	CNTL(lmp) = lmco;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	/*
12270Sstevel@tonic-gate 	 * Indicate if an interposer is found.  Note that the first object on a
12280Sstevel@tonic-gate 	 * link-map can be explicitly defined as an interposer so that it can
12290Sstevel@tonic-gate 	 * provide interposition over direct binding requests.
12300Sstevel@tonic-gate 	 */
12313466Srie 	if (FLAGS(lmp) & MSK_RT_INTPOSE)
12320Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_INTRPOSE;
12330Sstevel@tonic-gate 
12340Sstevel@tonic-gate 	/*
12350Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
12360Sstevel@tonic-gate 	 * pointers to the main control list.
12370Sstevel@tonic-gate 	 */
12385892Sab196087 	if (lmco == ALIST_OFF_DATA) {
12390Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12400Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate /*
12450Sstevel@tonic-gate  * Delete an item from the specified link map control list.
12460Sstevel@tonic-gate  */
12470Sstevel@tonic-gate void
12480Sstevel@tonic-gate lm_delete(Lm_list *lml, Rt_map *lmp)
12490Sstevel@tonic-gate {
12500Sstevel@tonic-gate 	Lm_cntl	*lmc;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 	/*
12530Sstevel@tonic-gate 	 * If the control list pointer hasn't been initialized, this object
12540Sstevel@tonic-gate 	 * never got added to a link-map list.
12550Sstevel@tonic-gate 	 */
12560Sstevel@tonic-gate 	if (CNTL(lmp) == 0)
12570Sstevel@tonic-gate 		return;
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	/*
12602454Srie 	 * If we're about to delete an object from the main link-map control
12612454Srie 	 * list, alert the debuggers that we are about to mess with this list.
12620Sstevel@tonic-gate 	 */
12635892Sab196087 	if ((CNTL(lmp) == ALIST_OFF_DATA) &&
12645892Sab196087 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
12650Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_DELETE);
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	/* LINTED */
12685892Sab196087 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(lmp));
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	if (lmc->lc_head == lmp)
12718394SAli.Bahrami@Sun.COM 		lmc->lc_head = NEXT_RT_MAP(lmp);
12720Sstevel@tonic-gate 	else
12738394SAli.Bahrami@Sun.COM 		NEXT(PREV_RT_MAP(lmp)) = (void *)NEXT(lmp);
12740Sstevel@tonic-gate 
12750Sstevel@tonic-gate 	if (lmc->lc_tail == lmp)
12768394SAli.Bahrami@Sun.COM 		lmc->lc_tail = PREV_RT_MAP(lmp);
12770Sstevel@tonic-gate 	else
12788394SAli.Bahrami@Sun.COM 		PREV(NEXT_RT_MAP(lmp)) = PREV(lmp);
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 	/*
12810Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
12820Sstevel@tonic-gate 	 * pointers to the main control list.
12830Sstevel@tonic-gate 	 */
12845892Sab196087 	if (lmc == (Lm_cntl *)&lml->lm_lists->al_data) {
12850Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12860Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	/*
12900Sstevel@tonic-gate 	 * Indicate we have one less object on this control list.
12910Sstevel@tonic-gate 	 */
12920Sstevel@tonic-gate 	(lml->lm_obj)--;
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate /*
12960Sstevel@tonic-gate  * Move a link-map control list to another.  Objects that are being relocated
12970Sstevel@tonic-gate  * are maintained on secondary control lists.  Once their relocation is
12980Sstevel@tonic-gate  * complete, the entire list is appended to the previous control list, as this
12990Sstevel@tonic-gate  * list must have been the trigger for generating the new control list.
13000Sstevel@tonic-gate  */
13010Sstevel@tonic-gate void
13020Sstevel@tonic-gate lm_move(Lm_list *lml, Aliste nlmco, Aliste plmco, Lm_cntl *nlmc, Lm_cntl *plmc)
13030Sstevel@tonic-gate {
13040Sstevel@tonic-gate 	Rt_map	*lmp;
13050Sstevel@tonic-gate 
13060Sstevel@tonic-gate 	/*
13072454Srie 	 * If we're about to add a new family of objects to the main link-map
13082454Srie 	 * control list, alert the debuggers that we are about to mess with this
13092454Srie 	 * list.  Additions of object families to the main link-map control
13102454Srie 	 * list occur during lazy loading, filtering and dlopen().
13110Sstevel@tonic-gate 	 */
13125892Sab196087 	if ((plmco == ALIST_OFF_DATA) &&
13135892Sab196087 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
13140Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
13150Sstevel@tonic-gate 
13162454Srie 	DBG_CALL(Dbg_file_cntl(lml, nlmco, plmco));
13172454Srie 
13180Sstevel@tonic-gate 	/*
13190Sstevel@tonic-gate 	 * Indicate each new link-map has been moved to the previous link-map
13200Sstevel@tonic-gate 	 * control list.
13210Sstevel@tonic-gate 	 */
13228598SRod.Evans@Sun.COM 	for (lmp = nlmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) {
13230Sstevel@tonic-gate 		CNTL(lmp) = plmco;
13240Sstevel@tonic-gate 
13258598SRod.Evans@Sun.COM 		/*
13268598SRod.Evans@Sun.COM 		 * If these objects are being added to the main link-map
13278598SRod.Evans@Sun.COM 		 * control list, indicate that there are init's available
13288598SRod.Evans@Sun.COM 		 * for harvesting.
13298598SRod.Evans@Sun.COM 		 */
13308598SRod.Evans@Sun.COM 		if (plmco == ALIST_OFF_DATA) {
13318598SRod.Evans@Sun.COM 			lml->lm_init++;
13328598SRod.Evans@Sun.COM 			lml->lm_flags |= LML_FLG_OBJADDED;
13338598SRod.Evans@Sun.COM 		}
13348598SRod.Evans@Sun.COM 	}
13358598SRod.Evans@Sun.COM 
13360Sstevel@tonic-gate 	/*
13370Sstevel@tonic-gate 	 * Move the new link-map control list, to the callers link-map control
13380Sstevel@tonic-gate 	 * list.
13390Sstevel@tonic-gate 	 */
13407668SRod.Evans@Sun.COM 	if (plmc->lc_head == NULL) {
13410Sstevel@tonic-gate 		plmc->lc_head = nlmc->lc_head;
13427668SRod.Evans@Sun.COM 		PREV(nlmc->lc_head) = NULL;
13430Sstevel@tonic-gate 	} else {
13440Sstevel@tonic-gate 		NEXT(plmc->lc_tail) = (Link_map *)nlmc->lc_head;
13450Sstevel@tonic-gate 		PREV(nlmc->lc_head) = (Link_map *)plmc->lc_tail;
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	plmc->lc_tail = nlmc->lc_tail;
13497668SRod.Evans@Sun.COM 	nlmc->lc_head = nlmc->lc_tail = NULL;
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 	/*
13520Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
13530Sstevel@tonic-gate 	 * pointers to the main control list.
13540Sstevel@tonic-gate 	 */
13555892Sab196087 	if (plmco == ALIST_OFF_DATA) {
13560Sstevel@tonic-gate 		lml->lm_head = plmc->lc_head;
13570Sstevel@tonic-gate 		lml->lm_tail = plmc->lc_tail;
13580Sstevel@tonic-gate 	}
13590Sstevel@tonic-gate }
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate /*
13629340SRod.Evans@Sun.COM  * Create, or assign a link-map control list.  Each link-map list contains a
13639340SRod.Evans@Sun.COM  * main control list, which has an Alist offset of ALIST_OFF_DATA (see the
13649340SRod.Evans@Sun.COM  * description in include/rtld.h).  During the initial construction of a
13659340SRod.Evans@Sun.COM  * process, objects are added to this main control list.  This control list is
13669340SRod.Evans@Sun.COM  * never deleted, unless an alternate link-map list has been requested (say for
13679340SRod.Evans@Sun.COM  * auditors), and the associated objects could not be loaded or relocated.
13689340SRod.Evans@Sun.COM  *
13699340SRod.Evans@Sun.COM  * Once relocation has started, any lazy loadable objects, or filtees, are
13709340SRod.Evans@Sun.COM  * processed on a new, temporary control list.  Only when these objects have
13719340SRod.Evans@Sun.COM  * been fully relocated, are they moved to the main link-map control list.
13729340SRod.Evans@Sun.COM  * Once the objects are moved, this temporary control list is deleted (see
13739340SRod.Evans@Sun.COM  * remove_cntl()).
13749340SRod.Evans@Sun.COM  *
13759340SRod.Evans@Sun.COM  * A dlopen() always requires a new temporary link-map control list.
13769340SRod.Evans@Sun.COM  * Typically, a dlopen() occurs on a link-map list that had already started
13779340SRod.Evans@Sun.COM  * relocation, however, auditors can dlopen() objects on the main link-map
13789340SRod.Evans@Sun.COM  * list while under initial construction, before any relocation has begun.
13799340SRod.Evans@Sun.COM  * Hence, dlopen() requests are explicitly flagged.
13809340SRod.Evans@Sun.COM  */
13819340SRod.Evans@Sun.COM Aliste
13829340SRod.Evans@Sun.COM create_cntl(Lm_list *lml, int dlopen)
13839340SRod.Evans@Sun.COM {
13849340SRod.Evans@Sun.COM 	/*
13859340SRod.Evans@Sun.COM 	 * If the head link-map object has already been relocated, create a
13869340SRod.Evans@Sun.COM 	 * new, temporary, control list.
13879340SRod.Evans@Sun.COM 	 */
13889340SRod.Evans@Sun.COM 	if (dlopen || (lml->lm_head == NULL) ||
13899340SRod.Evans@Sun.COM 	    (FLAGS(lml->lm_head) & FLG_RT_RELOCED)) {
13909340SRod.Evans@Sun.COM 		Lm_cntl *lmc;
13919340SRod.Evans@Sun.COM 
13929340SRod.Evans@Sun.COM 		if ((lmc = alist_append(&lml->lm_lists, NULL, sizeof (Lm_cntl),
13939340SRod.Evans@Sun.COM 		    AL_CNT_LMLISTS)) == NULL)
13949340SRod.Evans@Sun.COM 			return (NULL);
13959340SRod.Evans@Sun.COM 
13969340SRod.Evans@Sun.COM 		return ((Aliste)((char *)lmc - (char *)lml->lm_lists));
13979340SRod.Evans@Sun.COM 	}
13989340SRod.Evans@Sun.COM 
13999340SRod.Evans@Sun.COM 	return (ALIST_OFF_DATA);
14009340SRod.Evans@Sun.COM }
14019340SRod.Evans@Sun.COM 
14029340SRod.Evans@Sun.COM /*
14030Sstevel@tonic-gate  * Environment variables can have a variety of defined permutations, and thus
14040Sstevel@tonic-gate  * the following infrastructure exists to allow this variety and to select the
14050Sstevel@tonic-gate  * required definition.
14060Sstevel@tonic-gate  *
14070Sstevel@tonic-gate  * Environment variables can be defined as 32- or 64-bit specific, and if so
14080Sstevel@tonic-gate  * they will take precedence over any instruction set neutral form.  Typically
14090Sstevel@tonic-gate  * this is only useful when the environment value is an informational string.
14100Sstevel@tonic-gate  *
14110Sstevel@tonic-gate  * Environment variables may be obtained from the standard user environment or
14120Sstevel@tonic-gate  * from a configuration file.  The latter provides a fallback if no user
14130Sstevel@tonic-gate  * environment setting is found, and can take two forms:
14140Sstevel@tonic-gate  *
14158883SRod.Evans@Sun.COM  *  -	a replaceable definition - this will be used if no user environment
14160Sstevel@tonic-gate  *	setting has been seen, or
14170Sstevel@tonic-gate  *
14188883SRod.Evans@Sun.COM  *  -	an permanent definition - this will be used no matter what user
14190Sstevel@tonic-gate  *	environment setting is seen.  In the case of list variables it will be
14200Sstevel@tonic-gate  *	appended to any process environment setting seen.
14210Sstevel@tonic-gate  *
14220Sstevel@tonic-gate  * Environment variables can be defined without a value (ie. LD_XXXX=) so as to
14230Sstevel@tonic-gate  * override any replaceable environment variables from a configuration file.
14240Sstevel@tonic-gate  */
14250Sstevel@tonic-gate static	u_longlong_t		rplgen;		/* replaceable generic */
14260Sstevel@tonic-gate 						/*	variables */
14270Sstevel@tonic-gate static	u_longlong_t		rplisa;		/* replaceable ISA specific */
14280Sstevel@tonic-gate 						/*	variables */
14290Sstevel@tonic-gate static	u_longlong_t		prmgen;		/* permanent generic */
14300Sstevel@tonic-gate 						/*	variables */
14310Sstevel@tonic-gate static	u_longlong_t		prmisa;		/* permanent ISA specific */
14320Sstevel@tonic-gate 						/*	variables */
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate /*
14350Sstevel@tonic-gate  * Classify an environment variables type.
14360Sstevel@tonic-gate  */
14370Sstevel@tonic-gate #define	ENV_TYP_IGNORE		0x1		/* ignore - variable is for */
14380Sstevel@tonic-gate 						/*	the wrong ISA */
14390Sstevel@tonic-gate #define	ENV_TYP_ISA		0x2		/* variable is ISA specific */
14400Sstevel@tonic-gate #define	ENV_TYP_CONFIG		0x4		/* variable obtained from a */
14410Sstevel@tonic-gate 						/*	config file */
14420Sstevel@tonic-gate #define	ENV_TYP_PERMANT		0x8		/* variable is permanent */
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate /*
14450Sstevel@tonic-gate  * Identify all environment variables.
14460Sstevel@tonic-gate  */
14470Sstevel@tonic-gate #define	ENV_FLG_AUDIT		0x0000000001ULL
14480Sstevel@tonic-gate #define	ENV_FLG_AUDIT_ARGS	0x0000000002ULL
14490Sstevel@tonic-gate #define	ENV_FLG_BIND_NOW	0x0000000004ULL
14500Sstevel@tonic-gate #define	ENV_FLG_BIND_NOT	0x0000000008ULL
14510Sstevel@tonic-gate #define	ENV_FLG_BINDINGS	0x0000000010ULL
14528598SRod.Evans@Sun.COM 
14530Sstevel@tonic-gate #define	ENV_FLG_CONFGEN		0x0000000040ULL
14540Sstevel@tonic-gate #define	ENV_FLG_CONFIG		0x0000000080ULL
14550Sstevel@tonic-gate #define	ENV_FLG_DEBUG		0x0000000100ULL
14560Sstevel@tonic-gate #define	ENV_FLG_DEBUG_OUTPUT	0x0000000200ULL
14570Sstevel@tonic-gate #define	ENV_FLG_DEMANGLE	0x0000000400ULL
14580Sstevel@tonic-gate #define	ENV_FLG_FLAGS		0x0000000800ULL
14590Sstevel@tonic-gate #define	ENV_FLG_INIT		0x0000001000ULL
14600Sstevel@tonic-gate #define	ENV_FLG_LIBPATH		0x0000002000ULL
14610Sstevel@tonic-gate #define	ENV_FLG_LOADAVAIL	0x0000004000ULL
14620Sstevel@tonic-gate #define	ENV_FLG_LOADFLTR	0x0000008000ULL
14630Sstevel@tonic-gate #define	ENV_FLG_NOAUDIT		0x0000010000ULL
14640Sstevel@tonic-gate #define	ENV_FLG_NOAUXFLTR	0x0000020000ULL
14650Sstevel@tonic-gate #define	ENV_FLG_NOBAPLT		0x0000040000ULL
14660Sstevel@tonic-gate #define	ENV_FLG_NOCONFIG	0x0000080000ULL
14670Sstevel@tonic-gate #define	ENV_FLG_NODIRCONFIG	0x0000100000ULL
14680Sstevel@tonic-gate #define	ENV_FLG_NODIRECT	0x0000200000ULL
14690Sstevel@tonic-gate #define	ENV_FLG_NOENVCONFIG	0x0000400000ULL
14700Sstevel@tonic-gate #define	ENV_FLG_NOLAZY		0x0000800000ULL
14710Sstevel@tonic-gate #define	ENV_FLG_NOOBJALTER	0x0001000000ULL
14720Sstevel@tonic-gate #define	ENV_FLG_NOVERSION	0x0002000000ULL
14730Sstevel@tonic-gate #define	ENV_FLG_PRELOAD		0x0004000000ULL
14740Sstevel@tonic-gate #define	ENV_FLG_PROFILE		0x0008000000ULL
14750Sstevel@tonic-gate #define	ENV_FLG_PROFILE_OUTPUT	0x0010000000ULL
14760Sstevel@tonic-gate #define	ENV_FLG_SIGNAL		0x0020000000ULL
14770Sstevel@tonic-gate #define	ENV_FLG_TRACE_OBJS	0x0040000000ULL
14780Sstevel@tonic-gate #define	ENV_FLG_TRACE_PTHS	0x0080000000ULL
14790Sstevel@tonic-gate #define	ENV_FLG_UNREF		0x0100000000ULL
14800Sstevel@tonic-gate #define	ENV_FLG_UNUSED		0x0200000000ULL
14810Sstevel@tonic-gate #define	ENV_FLG_VERBOSE		0x0400000000ULL
14820Sstevel@tonic-gate #define	ENV_FLG_WARN		0x0800000000ULL
14830Sstevel@tonic-gate #define	ENV_FLG_NOFLTCONFIG	0x1000000000ULL
1484280Srie #define	ENV_FLG_BIND_LAZY	0x2000000000ULL
14854947Srie #define	ENV_FLG_NOUNRESWEAK	0x4000000000ULL
14866150Srie #define	ENV_FLG_NOPAREXT	0x8000000000ULL
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate #define	SEL_REPLACE		0x0001
14890Sstevel@tonic-gate #define	SEL_PERMANT		0x0002
14900Sstevel@tonic-gate #define	SEL_ACT_RT		0x0100	/* setting rtld_flags */
14910Sstevel@tonic-gate #define	SEL_ACT_RT2		0x0200	/* setting rtld_flags2 */
14920Sstevel@tonic-gate #define	SEL_ACT_STR		0x0400	/* setting string value */
14930Sstevel@tonic-gate #define	SEL_ACT_LML		0x0800	/* setting lml_flags */
14940Sstevel@tonic-gate #define	SEL_ACT_LMLT		0x1000	/* setting lml_tflags */
14950Sstevel@tonic-gate #define	SEL_ACT_SPEC_1		0x2000	/* For FLG_{FLAGS, LIBPATH} */
14960Sstevel@tonic-gate #define	SEL_ACT_SPEC_2		0x4000	/* need special handling */
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate /*
14990Sstevel@tonic-gate  * Pattern match an LD_XXXX environment variable.  s1 points to the XXXX part
15000Sstevel@tonic-gate  * and len specifies its length (comparing a strings length before the string
15010Sstevel@tonic-gate  * itself speed things up).  s2 points to the token itself which has already
15020Sstevel@tonic-gate  * had any leading white-space removed.
15030Sstevel@tonic-gate  */
15040Sstevel@tonic-gate static void
15050Sstevel@tonic-gate ld_generic_env(const char *s1, size_t len, const char *s2, Word *lmflags,
15060Sstevel@tonic-gate     Word *lmtflags, uint_t env_flags, int aout)
15070Sstevel@tonic-gate {
15080Sstevel@tonic-gate 	u_longlong_t	variable = 0;
15091824Srie 	ushort_t	select = 0;
15101824Srie 	const char	**str;
15111824Srie 	Word		val = 0;
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	/*
15140Sstevel@tonic-gate 	 * Determine whether we're dealing with a replaceable or permanent
15150Sstevel@tonic-gate 	 * string.
15160Sstevel@tonic-gate 	 */
15170Sstevel@tonic-gate 	if (env_flags & ENV_TYP_PERMANT) {
15180Sstevel@tonic-gate 		/*
15190Sstevel@tonic-gate 		 * If the string is from a configuration file and defined as
15200Sstevel@tonic-gate 		 * permanent, assign it as permanent.
15210Sstevel@tonic-gate 		 */
15220Sstevel@tonic-gate 		select |= SEL_PERMANT;
15230Sstevel@tonic-gate 	} else
15240Sstevel@tonic-gate 		select |= SEL_REPLACE;
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	/*
15270Sstevel@tonic-gate 	 * Parse the variable given.
15280Sstevel@tonic-gate 	 *
15290Sstevel@tonic-gate 	 * The LD_AUDIT family.
15300Sstevel@tonic-gate 	 */
15310Sstevel@tonic-gate 	if (*s1 == 'A') {
15320Sstevel@tonic-gate 		if ((len == MSG_LD_AUDIT_SIZE) && (strncmp(s1,
15330Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_AUDIT), MSG_LD_AUDIT_SIZE) == 0)) {
15340Sstevel@tonic-gate 			/*
15350Sstevel@tonic-gate 			 * Replaceable and permanent audit objects can exist.
15360Sstevel@tonic-gate 			 */
15370Sstevel@tonic-gate 			select |= SEL_ACT_STR;
15389340SRod.Evans@Sun.COM 			str = (select & SEL_REPLACE) ? &rpl_audit : &prm_audit;
15390Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT;
15400Sstevel@tonic-gate 		} else if ((len == MSG_LD_AUDIT_ARGS_SIZE) &&
15410Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_AUDIT_ARGS),
15420Sstevel@tonic-gate 		    MSG_LD_AUDIT_ARGS_SIZE) == 0)) {
15430Sstevel@tonic-gate 			/*
15440Sstevel@tonic-gate 			 * A specialized variable for plt_exit() use, not
15450Sstevel@tonic-gate 			 * documented for general use.
15460Sstevel@tonic-gate 			 */
15470Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15480Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT_ARGS;
15490Sstevel@tonic-gate 		}
15500Sstevel@tonic-gate 	}
15510Sstevel@tonic-gate 	/*
15528598SRod.Evans@Sun.COM 	 * The LD_BIND family.
15530Sstevel@tonic-gate 	 */
15540Sstevel@tonic-gate 	else if (*s1 == 'B') {
1555280Srie 		if ((len == MSG_LD_BIND_LAZY_SIZE) && (strncmp(s1,
1556280Srie 		    MSG_ORIG(MSG_LD_BIND_LAZY),
1557280Srie 		    MSG_LD_BIND_LAZY_SIZE) == 0)) {
1558280Srie 			select |= SEL_ACT_RT2;
1559280Srie 			val = RT_FL2_BINDLAZY;
1560280Srie 			variable = ENV_FLG_BIND_LAZY;
1561280Srie 		} else if ((len == MSG_LD_BIND_NOW_SIZE) && (strncmp(s1,
15620Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOW), MSG_LD_BIND_NOW_SIZE) == 0)) {
15630Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
15640Sstevel@tonic-gate 			val = RT_FL2_BINDNOW;
15650Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOW;
15660Sstevel@tonic-gate 		} else if ((len == MSG_LD_BIND_NOT_SIZE) && (strncmp(s1,
15670Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOT), MSG_LD_BIND_NOT_SIZE) == 0)) {
15680Sstevel@tonic-gate 			/*
15690Sstevel@tonic-gate 			 * Another trick, enabled to help debug AOUT
15700Sstevel@tonic-gate 			 * applications under BCP, but not documented for
15710Sstevel@tonic-gate 			 * general use.
15720Sstevel@tonic-gate 			 */
15730Sstevel@tonic-gate 			select |= SEL_ACT_RT;
15740Sstevel@tonic-gate 			val = RT_FL_NOBIND;
15750Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOT;
15760Sstevel@tonic-gate 		} else if ((len == MSG_LD_BINDINGS_SIZE) && (strncmp(s1,
15770Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BINDINGS), MSG_LD_BINDINGS_SIZE) == 0)) {
15780Sstevel@tonic-gate 			/*
15790Sstevel@tonic-gate 			 * This variable is simply for backward compatibility.
15800Sstevel@tonic-gate 			 * If this and LD_DEBUG are both specified, only one of
15810Sstevel@tonic-gate 			 * the strings is going to get processed.
15820Sstevel@tonic-gate 			 */
15830Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15840Sstevel@tonic-gate 			variable = ENV_FLG_BINDINGS;
15850Sstevel@tonic-gate 		}
15860Sstevel@tonic-gate 	}
15870Sstevel@tonic-gate 	/*
15888598SRod.Evans@Sun.COM 	 * LD_CONFIG family.
15890Sstevel@tonic-gate 	 */
15900Sstevel@tonic-gate 	else if (*s1 == 'C') {
15918598SRod.Evans@Sun.COM 		if ((len == MSG_LD_CONFGEN_SIZE) && (strncmp(s1,
15920Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFGEN), MSG_LD_CONFGEN_SIZE) == 0)) {
15930Sstevel@tonic-gate 			/*
15940Sstevel@tonic-gate 			 * Set by crle(1) to indicate it's building a
15950Sstevel@tonic-gate 			 * configuration file, not documented for general use.
15960Sstevel@tonic-gate 			 */
15970Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15980Sstevel@tonic-gate 			variable = ENV_FLG_CONFGEN;
15990Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFIG_SIZE) && (strncmp(s1,
16000Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFIG), MSG_LD_CONFIG_SIZE) == 0)) {
16010Sstevel@tonic-gate 			/*
16020Sstevel@tonic-gate 			 * Secure applications must use a default configuration
16030Sstevel@tonic-gate 			 * file.  A setting from a configuration file doesn't
16040Sstevel@tonic-gate 			 * make sense (given we must be reading a configuration
16050Sstevel@tonic-gate 			 * file to have gotten this).
16060Sstevel@tonic-gate 			 */
16070Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_SECURE) ||
16080Sstevel@tonic-gate 			    (env_flags & ENV_TYP_CONFIG))
16090Sstevel@tonic-gate 				return;
16100Sstevel@tonic-gate 			select |= SEL_ACT_STR;
16110Sstevel@tonic-gate 			str = &config->c_name;
16120Sstevel@tonic-gate 			variable = ENV_FLG_CONFIG;
16130Sstevel@tonic-gate 		}
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 	/*
16160Sstevel@tonic-gate 	 * The LD_DEBUG family and LD_DEMANGLE.
16170Sstevel@tonic-gate 	 */
16180Sstevel@tonic-gate 	else if (*s1 == 'D') {
16190Sstevel@tonic-gate 		if ((len == MSG_LD_DEBUG_SIZE) && (strncmp(s1,
16200Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG), MSG_LD_DEBUG_SIZE) == 0)) {
16210Sstevel@tonic-gate 			select |= SEL_ACT_STR;
16229340SRod.Evans@Sun.COM 			str = (select & SEL_REPLACE) ? &rpl_debug : &prm_debug;
16230Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG;
16240Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEBUG_OUTPUT_SIZE) && (strncmp(s1,
16250Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG_OUTPUT),
16260Sstevel@tonic-gate 		    MSG_LD_DEBUG_OUTPUT_SIZE) == 0)) {
16270Sstevel@tonic-gate 			select |= SEL_ACT_STR;
16280Sstevel@tonic-gate 			str = &dbg_file;
16290Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG_OUTPUT;
16300Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEMANGLE_SIZE) && (strncmp(s1,
16310Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEMANGLE), MSG_LD_DEMANGLE_SIZE) == 0)) {
16320Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16330Sstevel@tonic-gate 			val = RT_FL_DEMANGLE;
16340Sstevel@tonic-gate 			variable = ENV_FLG_DEMANGLE;
16350Sstevel@tonic-gate 		}
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate 	/*
16380Sstevel@tonic-gate 	 * LD_FLAGS - collect the best variable definition.  On completion of
16390Sstevel@tonic-gate 	 * environment variable processing pass the result to ld_flags_env()
16400Sstevel@tonic-gate 	 * where they'll be decomposed and passed back to this routine.
16410Sstevel@tonic-gate 	 */
16420Sstevel@tonic-gate 	else if (*s1 == 'F') {
16430Sstevel@tonic-gate 		if ((len == MSG_LD_FLAGS_SIZE) && (strncmp(s1,
16440Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FLAGS), MSG_LD_FLAGS_SIZE) == 0)) {
16450Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
16469340SRod.Evans@Sun.COM 			str = (select & SEL_REPLACE) ? &rpl_ldflags :
16479340SRod.Evans@Sun.COM 			    &prm_ldflags;
16480Sstevel@tonic-gate 			variable = ENV_FLG_FLAGS;
16490Sstevel@tonic-gate 		}
16500Sstevel@tonic-gate 	}
16510Sstevel@tonic-gate 	/*
16520Sstevel@tonic-gate 	 * LD_INIT (internal, used by ldd(1)).
16530Sstevel@tonic-gate 	 */
16540Sstevel@tonic-gate 	else if (*s1 == 'I') {
16550Sstevel@tonic-gate 		if ((len == MSG_LD_INIT_SIZE) && (strncmp(s1,
16560Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_INIT), MSG_LD_INIT_SIZE) == 0)) {
16570Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16580Sstevel@tonic-gate 			val = LML_FLG_TRC_INIT;
16590Sstevel@tonic-gate 			variable = ENV_FLG_INIT;
16600Sstevel@tonic-gate 		}
16610Sstevel@tonic-gate 	}
16620Sstevel@tonic-gate 	/*
16630Sstevel@tonic-gate 	 * The LD_LIBRARY_PATH and LD_LOAD families.
16640Sstevel@tonic-gate 	 */
16650Sstevel@tonic-gate 	else if (*s1 == 'L') {
16660Sstevel@tonic-gate 		if ((len == MSG_LD_LIBPATH_SIZE) && (strncmp(s1,
16670Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LIBPATH), MSG_LD_LIBPATH_SIZE) == 0)) {
16680Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
16699340SRod.Evans@Sun.COM 			str = (select & SEL_REPLACE) ? &rpl_libpath :
16709340SRod.Evans@Sun.COM 			    &prm_libpath;
16710Sstevel@tonic-gate 			variable = ENV_FLG_LIBPATH;
16720Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADAVAIL_SIZE) && (strncmp(s1,
16730Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADAVAIL), MSG_LD_LOADAVAIL_SIZE) == 0)) {
16740Sstevel@tonic-gate 			/*
16750Sstevel@tonic-gate 			 * Internal use by crle(1), not documented for general
16760Sstevel@tonic-gate 			 * use.
16770Sstevel@tonic-gate 			 */
16780Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16790Sstevel@tonic-gate 			val = LML_FLG_LOADAVAIL;
16800Sstevel@tonic-gate 			variable = ENV_FLG_LOADAVAIL;
16810Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADFLTR_SIZE) && (strncmp(s1,
16820Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADFLTR), MSG_LD_LOADFLTR_SIZE) == 0)) {
16830Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
16840Sstevel@tonic-gate 			variable = ENV_FLG_LOADFLTR;
16850Sstevel@tonic-gate 		}
16860Sstevel@tonic-gate 	}
16870Sstevel@tonic-gate 	/*
16880Sstevel@tonic-gate 	 * The LD_NO family.
16890Sstevel@tonic-gate 	 */
16900Sstevel@tonic-gate 	else if (*s1 == 'N') {
16910Sstevel@tonic-gate 		if ((len == MSG_LD_NOAUDIT_SIZE) && (strncmp(s1,
16920Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUDIT), MSG_LD_NOAUDIT_SIZE) == 0)) {
16930Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16940Sstevel@tonic-gate 			val = RT_FL_NOAUDIT;
16950Sstevel@tonic-gate 			variable = ENV_FLG_NOAUDIT;
16960Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOAUXFLTR_SIZE) && (strncmp(s1,
16970Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUXFLTR), MSG_LD_NOAUXFLTR_SIZE) == 0)) {
16980Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16990Sstevel@tonic-gate 			val = RT_FL_NOAUXFLTR;
17000Sstevel@tonic-gate 			variable = ENV_FLG_NOAUXFLTR;
17010Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOBAPLT_SIZE) && (strncmp(s1,
17020Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOBAPLT), MSG_LD_NOBAPLT_SIZE) == 0)) {
17030Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17040Sstevel@tonic-gate 			val = RT_FL_NOBAPLT;
17050Sstevel@tonic-gate 			variable = ENV_FLG_NOBAPLT;
17060Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOCONFIG_SIZE) && (strncmp(s1,
17070Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOCONFIG), MSG_LD_NOCONFIG_SIZE) == 0)) {
17080Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17090Sstevel@tonic-gate 			val = RT_FL_NOCFG;
17100Sstevel@tonic-gate 			variable = ENV_FLG_NOCONFIG;
17110Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRCONFIG_SIZE) && (strncmp(s1,
17120Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRCONFIG),
17130Sstevel@tonic-gate 		    MSG_LD_NODIRCONFIG_SIZE) == 0)) {
17140Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17150Sstevel@tonic-gate 			val = RT_FL_NODIRCFG;
17160Sstevel@tonic-gate 			variable = ENV_FLG_NODIRCONFIG;
17170Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRECT_SIZE) && (strncmp(s1,
17180Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRECT), MSG_LD_NODIRECT_SIZE) == 0)) {
17190Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
17200Sstevel@tonic-gate 			val = LML_TFLG_NODIRECT;
17210Sstevel@tonic-gate 			variable = ENV_FLG_NODIRECT;
17220Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOENVCONFIG_SIZE) && (strncmp(s1,
17230Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOENVCONFIG),
17240Sstevel@tonic-gate 		    MSG_LD_NOENVCONFIG_SIZE) == 0)) {
17250Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17260Sstevel@tonic-gate 			val = RT_FL_NOENVCFG;
17270Sstevel@tonic-gate 			variable = ENV_FLG_NOENVCONFIG;
17280Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOFLTCONFIG_SIZE) && (strncmp(s1,
17290Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOFLTCONFIG),
17300Sstevel@tonic-gate 		    MSG_LD_NOFLTCONFIG_SIZE) == 0)) {
17310Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
17320Sstevel@tonic-gate 			val = RT_FL2_NOFLTCFG;
17330Sstevel@tonic-gate 			variable = ENV_FLG_NOFLTCONFIG;
17340Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOLAZY_SIZE) && (strncmp(s1,
17350Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOLAZY), MSG_LD_NOLAZY_SIZE) == 0)) {
17360Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
17370Sstevel@tonic-gate 			val = LML_TFLG_NOLAZYLD;
17380Sstevel@tonic-gate 			variable = ENV_FLG_NOLAZY;
17390Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOOBJALTER_SIZE) && (strncmp(s1,
17400Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOOBJALTER),
17410Sstevel@tonic-gate 		    MSG_LD_NOOBJALTER_SIZE) == 0)) {
17420Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17430Sstevel@tonic-gate 			val = RT_FL_NOOBJALT;
17440Sstevel@tonic-gate 			variable = ENV_FLG_NOOBJALTER;
17450Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOVERSION_SIZE) && (strncmp(s1,
17460Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOVERSION), MSG_LD_NOVERSION_SIZE) == 0)) {
17470Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17480Sstevel@tonic-gate 			val = RT_FL_NOVERSION;
17490Sstevel@tonic-gate 			variable = ENV_FLG_NOVERSION;
17504947Srie 		} else if ((len == MSG_LD_NOUNRESWEAK_SIZE) && (strncmp(s1,
17514947Srie 		    MSG_ORIG(MSG_LD_NOUNRESWEAK),
17524947Srie 		    MSG_LD_NOUNRESWEAK_SIZE) == 0)) {
17534947Srie 			/*
17544947Srie 			 * LD_NOUNRESWEAK (internal, used by ldd(1)).
17554947Srie 			 */
17564947Srie 			select |= SEL_ACT_LML;
17574947Srie 			val = LML_FLG_TRC_NOUNRESWEAK;
17584947Srie 			variable = ENV_FLG_NOUNRESWEAK;
17596150Srie 		} else if ((len == MSG_LD_NOPAREXT_SIZE) && (strncmp(s1,
17606150Srie 		    MSG_ORIG(MSG_LD_NOPAREXT), MSG_LD_NOPAREXT_SIZE) == 0)) {
17616150Srie 			select |= SEL_ACT_LML;
17626150Srie 			val = LML_FLG_TRC_NOPAREXT;
17636150Srie 			variable = ENV_FLG_NOPAREXT;
17640Sstevel@tonic-gate 		}
17650Sstevel@tonic-gate 	}
17660Sstevel@tonic-gate 	/*
17670Sstevel@tonic-gate 	 * LD_PRELOAD and LD_PROFILE family.
17680Sstevel@tonic-gate 	 */
17690Sstevel@tonic-gate 	else if (*s1 == 'P') {
17700Sstevel@tonic-gate 		if ((len == MSG_LD_PRELOAD_SIZE) && (strncmp(s1,
17710Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PRELOAD), MSG_LD_PRELOAD_SIZE) == 0)) {
17720Sstevel@tonic-gate 			select |= SEL_ACT_STR;
17739340SRod.Evans@Sun.COM 			str = (select & SEL_REPLACE) ? &rpl_preload :
17749340SRod.Evans@Sun.COM 			    &prm_preload;
17750Sstevel@tonic-gate 			variable = ENV_FLG_PRELOAD;
17760Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_SIZE) && (strncmp(s1,
17770Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE), MSG_LD_PROFILE_SIZE) == 0)) {
17780Sstevel@tonic-gate 			/*
17790Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
17800Sstevel@tonic-gate 			 */
17810Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
17820Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE;
17830Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_OUTPUT_SIZE) && (strncmp(s1,
17840Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE_OUTPUT),
17850Sstevel@tonic-gate 		    MSG_LD_PROFILE_OUTPUT_SIZE) == 0)) {
17860Sstevel@tonic-gate 			/*
17870Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
17880Sstevel@tonic-gate 			 */
17890Sstevel@tonic-gate 			select |= SEL_ACT_STR;
17900Sstevel@tonic-gate 			str = &profile_out;
17910Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE_OUTPUT;
17920Sstevel@tonic-gate 		}
17930Sstevel@tonic-gate 	}
17940Sstevel@tonic-gate 	/*
17950Sstevel@tonic-gate 	 * LD_SIGNAL.
17960Sstevel@tonic-gate 	 */
17970Sstevel@tonic-gate 	else if (*s1 == 'S') {
17980Sstevel@tonic-gate 		if (rtld_flags & RT_FL_SECURE)
17990Sstevel@tonic-gate 			return;
18000Sstevel@tonic-gate 		if ((len == MSG_LD_SIGNAL_SIZE) &&
18010Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_SIGNAL),
18020Sstevel@tonic-gate 		    MSG_LD_SIGNAL_SIZE) == 0)) {
18030Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18040Sstevel@tonic-gate 			variable = ENV_FLG_SIGNAL;
18050Sstevel@tonic-gate 		}
18060Sstevel@tonic-gate 	}
18070Sstevel@tonic-gate 	/*
18083511Srie 	 * The LD_TRACE family (internal, used by ldd(1)).  This definition is
18093511Srie 	 * the key to enabling all other ldd(1) specific environment variables.
18103511Srie 	 * In case an auditor is called, which in turn might exec(2) a
18113511Srie 	 * subprocess, this variable is disabled, so that any subprocess
18123511Srie 	 * escapes ldd(1) processing.
18130Sstevel@tonic-gate 	 */
18140Sstevel@tonic-gate 	else if (*s1 == 'T') {
18150Sstevel@tonic-gate 		if (((len == MSG_LD_TRACE_OBJS_SIZE) &&
18160Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS),
18170Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_SIZE) == 0)) ||
18180Sstevel@tonic-gate 		    ((len == MSG_LD_TRACE_OBJS_E_SIZE) &&
18190Sstevel@tonic-gate 		    (((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_E),
18200Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_E_SIZE) == 0) && !aout) ||
18210Sstevel@tonic-gate 		    ((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_A),
18220Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_A_SIZE) == 0) && aout)))) {
18233511Srie 			char	*s0 = (char *)s1;
18243511Srie 
18250Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18260Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_OBJS;
18273511Srie 
18283731Srie #if	defined(__sparc) || defined(__x86)
18293511Srie 			/*
18303511Srie 			 * The simplest way to "disable" this variable is to
18313511Srie 			 * truncate this string to "LD_'\0'". This string is
18323511Srie 			 * ignored by any ld.so.1 environment processing.
18333511Srie 			 * Use of such interfaces as unsetenv(3c) are overkill,
18343511Srie 			 * and would drag too much libc implementation detail
18353511Srie 			 * into ld.so.1.
18363511Srie 			 */
18374362Srie 			*s0 = '\0';
18383511Srie #else
18393511Srie /*
18403511Srie  * Verify that the above write is appropriate for any new platforms.
18413511Srie  */
18423511Srie #error	unsupported architecture!
18433511Srie #endif
18440Sstevel@tonic-gate 		} else if ((len == MSG_LD_TRACE_PTHS_SIZE) && (strncmp(s1,
18450Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_TRACE_PTHS),
18460Sstevel@tonic-gate 		    MSG_LD_TRACE_PTHS_SIZE) == 0)) {
18470Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18480Sstevel@tonic-gate 			val = LML_FLG_TRC_SEARCH;
18490Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_PTHS;
18500Sstevel@tonic-gate 		}
18510Sstevel@tonic-gate 	}
18520Sstevel@tonic-gate 	/*
18530Sstevel@tonic-gate 	 * LD_UNREF and LD_UNUSED (internal, used by ldd(1)).
18540Sstevel@tonic-gate 	 */
18550Sstevel@tonic-gate 	else if (*s1 == 'U') {
18560Sstevel@tonic-gate 		if ((len == MSG_LD_UNREF_SIZE) && (strncmp(s1,
18570Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNREF), MSG_LD_UNREF_SIZE) == 0)) {
18580Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18590Sstevel@tonic-gate 			val = LML_FLG_TRC_UNREF;
18600Sstevel@tonic-gate 			variable = ENV_FLG_UNREF;
18610Sstevel@tonic-gate 		} else if ((len == MSG_LD_UNUSED_SIZE) && (strncmp(s1,
18620Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNUSED), MSG_LD_UNUSED_SIZE) == 0)) {
18630Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18640Sstevel@tonic-gate 			val = LML_FLG_TRC_UNUSED;
18650Sstevel@tonic-gate 			variable = ENV_FLG_UNUSED;
18660Sstevel@tonic-gate 		}
18670Sstevel@tonic-gate 	}
18680Sstevel@tonic-gate 	/*
18690Sstevel@tonic-gate 	 * LD_VERBOSE (internal, used by ldd(1)).
18700Sstevel@tonic-gate 	 */
18710Sstevel@tonic-gate 	else if (*s1 == 'V') {
18720Sstevel@tonic-gate 		if ((len == MSG_LD_VERBOSE_SIZE) && (strncmp(s1,
18730Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_VERBOSE), MSG_LD_VERBOSE_SIZE) == 0)) {
18740Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18750Sstevel@tonic-gate 			val = LML_FLG_TRC_VERBOSE;
18760Sstevel@tonic-gate 			variable = ENV_FLG_VERBOSE;
18770Sstevel@tonic-gate 		}
18780Sstevel@tonic-gate 	}
18790Sstevel@tonic-gate 	/*
18800Sstevel@tonic-gate 	 * LD_WARN (internal, used by ldd(1)).
18810Sstevel@tonic-gate 	 */
18820Sstevel@tonic-gate 	else if (*s1 == 'W') {
18830Sstevel@tonic-gate 		if ((len == MSG_LD_WARN_SIZE) && (strncmp(s1,
18840Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_WARN), MSG_LD_WARN_SIZE) == 0)) {
18850Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18860Sstevel@tonic-gate 			val = LML_FLG_TRC_WARN;
18870Sstevel@tonic-gate 			variable = ENV_FLG_WARN;
18880Sstevel@tonic-gate 		}
18890Sstevel@tonic-gate 	}
18908598SRod.Evans@Sun.COM 
18910Sstevel@tonic-gate 	if (variable == 0)
18920Sstevel@tonic-gate 		return;
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate 	/*
18950Sstevel@tonic-gate 	 * If the variable is already processed with ISA specific variable,
18960Sstevel@tonic-gate 	 * no further processing needed.
18970Sstevel@tonic-gate 	 */
18980Sstevel@tonic-gate 	if (((select & SEL_REPLACE) && (rplisa & variable)) ||
18990Sstevel@tonic-gate 	    ((select & SEL_PERMANT) && (prmisa & variable)))
19000Sstevel@tonic-gate 		return;
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 	/*
1903827Sseizo 	 * Now mark the appropriate variables.
1904827Sseizo 	 * If the replaceable variable is already set, then the
1905827Sseizo 	 * process environment variable must be set. Any replaceable
1906827Sseizo 	 * variable specified in a configuration file can be ignored.
19070Sstevel@tonic-gate 	 */
19080Sstevel@tonic-gate 	if (env_flags & ENV_TYP_ISA) {
19090Sstevel@tonic-gate 		/*
19109340SRod.Evans@Sun.COM 		 * This is an ISA setting. We do the setting even if s2 is
19119340SRod.Evans@Sun.COM 		 * NULL.  If s2 is NULL, we might need to undo the setting.
19120Sstevel@tonic-gate 		 */
19130Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1914827Sseizo 			if (rplisa & variable)
1915827Sseizo 				return;
19160Sstevel@tonic-gate 			rplisa |= variable;
19170Sstevel@tonic-gate 		} else {
19180Sstevel@tonic-gate 			prmisa |= variable;
19190Sstevel@tonic-gate 		}
19200Sstevel@tonic-gate 	} else if (s2) {
19210Sstevel@tonic-gate 		/*
19229340SRod.Evans@Sun.COM 		 * This is a non-ISA setting.
19230Sstevel@tonic-gate 		 */
19240Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1925827Sseizo 			if (rplgen & variable)
1926827Sseizo 				return;
19270Sstevel@tonic-gate 			rplgen |= variable;
19280Sstevel@tonic-gate 		} else
19290Sstevel@tonic-gate 			prmgen |= variable;
19300Sstevel@tonic-gate 	} else
19310Sstevel@tonic-gate 		/*
19329340SRod.Evans@Sun.COM 		 * This is a non-ISA setting which can be ignored.
19330Sstevel@tonic-gate 		 */
19340Sstevel@tonic-gate 		return;
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 	/*
19370Sstevel@tonic-gate 	 * Now perform the setting.
19380Sstevel@tonic-gate 	 */
19390Sstevel@tonic-gate 	if (select & SEL_ACT_RT) {
19400Sstevel@tonic-gate 		if (s2)
19410Sstevel@tonic-gate 			rtld_flags |= val;
19420Sstevel@tonic-gate 		else
19430Sstevel@tonic-gate 			rtld_flags &= ~val;
19440Sstevel@tonic-gate 	} else if (select & SEL_ACT_RT2) {
19450Sstevel@tonic-gate 		if (s2)
19460Sstevel@tonic-gate 			rtld_flags2 |= val;
19470Sstevel@tonic-gate 		else
19480Sstevel@tonic-gate 			rtld_flags2 &= ~val;
19490Sstevel@tonic-gate 	} else if (select & SEL_ACT_STR)
19500Sstevel@tonic-gate 		*str = s2;
19510Sstevel@tonic-gate 	else if (select & SEL_ACT_LML) {
19520Sstevel@tonic-gate 		if (s2)
19530Sstevel@tonic-gate 			*lmflags |= val;
19540Sstevel@tonic-gate 		else
19550Sstevel@tonic-gate 			*lmflags &= ~val;
19560Sstevel@tonic-gate 	} else if (select & SEL_ACT_LMLT) {
19570Sstevel@tonic-gate 		if (s2)
19580Sstevel@tonic-gate 			*lmtflags |= val;
19590Sstevel@tonic-gate 		else
19600Sstevel@tonic-gate 			*lmtflags &= ~val;
19610Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_1) {
19620Sstevel@tonic-gate 		/*
19630Sstevel@tonic-gate 		 * variable is either ENV_FLG_FLAGS or ENV_FLG_LIBPATH
19640Sstevel@tonic-gate 		 */
19650Sstevel@tonic-gate 		*str = s2;
19660Sstevel@tonic-gate 		if ((select & SEL_REPLACE) && (env_flags & ENV_TYP_CONFIG)) {
19670Sstevel@tonic-gate 			if (s2) {
19680Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
19690Sstevel@tonic-gate 					env_info |= ENV_INF_FLAGCFG;
19700Sstevel@tonic-gate 				else
19710Sstevel@tonic-gate 					env_info |= ENV_INF_PATHCFG;
19720Sstevel@tonic-gate 			} else {
19730Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
19740Sstevel@tonic-gate 					env_info &= ~ENV_INF_FLAGCFG;
19750Sstevel@tonic-gate 				else
19760Sstevel@tonic-gate 					env_info &= ~ENV_INF_PATHCFG;
19770Sstevel@tonic-gate 			}
19780Sstevel@tonic-gate 		}
19790Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_2) {
19800Sstevel@tonic-gate 		/*
19810Sstevel@tonic-gate 		 * variables can be: ENV_FLG_
19820Sstevel@tonic-gate 		 * 	AUDIT_ARGS, BINDING, CONCURRENCY, CONFGEN,
19830Sstevel@tonic-gate 		 *	LOADFLTR, PROFILE, SIGNAL, TRACE_OBJS
19840Sstevel@tonic-gate 		 */
19850Sstevel@tonic-gate 		if (variable == ENV_FLG_AUDIT_ARGS) {
19860Sstevel@tonic-gate 			if (s2) {
19870Sstevel@tonic-gate 				audit_argcnt = atoi(s2);
19880Sstevel@tonic-gate 				audit_argcnt += audit_argcnt % 2;
19890Sstevel@tonic-gate 			} else
19900Sstevel@tonic-gate 				audit_argcnt = 0;
19910Sstevel@tonic-gate 		} else if (variable == ENV_FLG_BINDINGS) {
19920Sstevel@tonic-gate 			if (s2)
19930Sstevel@tonic-gate 				rpl_debug = MSG_ORIG(MSG_TKN_BINDINGS);
19940Sstevel@tonic-gate 			else
19958883SRod.Evans@Sun.COM 				rpl_debug = NULL;
19960Sstevel@tonic-gate 		} else if (variable == ENV_FLG_CONFGEN) {
19970Sstevel@tonic-gate 			if (s2) {
19980Sstevel@tonic-gate 				rtld_flags |= RT_FL_CONFGEN;
19990Sstevel@tonic-gate 				*lmflags |= LML_FLG_IGNRELERR;
20000Sstevel@tonic-gate 			} else {
20010Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_CONFGEN;
20020Sstevel@tonic-gate 				*lmflags &= ~LML_FLG_IGNRELERR;
20030Sstevel@tonic-gate 			}
20040Sstevel@tonic-gate 		} else if (variable == ENV_FLG_LOADFLTR) {
20050Sstevel@tonic-gate 			if (s2) {
20060Sstevel@tonic-gate 				*lmtflags |= LML_TFLG_LOADFLTR;
20070Sstevel@tonic-gate 				if (*s2 == '2')
20080Sstevel@tonic-gate 					rtld_flags |= RT_FL_WARNFLTR;
20090Sstevel@tonic-gate 			} else {
20100Sstevel@tonic-gate 				*lmtflags &= ~LML_TFLG_LOADFLTR;
20110Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_WARNFLTR;
20120Sstevel@tonic-gate 			}
20130Sstevel@tonic-gate 		} else if (variable == ENV_FLG_PROFILE) {
20140Sstevel@tonic-gate 			profile_name = s2;
20150Sstevel@tonic-gate 			if (s2) {
20160Sstevel@tonic-gate 				if (strcmp(s2, MSG_ORIG(MSG_FIL_RTLD)) == 0) {
20170Sstevel@tonic-gate 					return;
20180Sstevel@tonic-gate 				}
20194362Srie 				/* BEGIN CSTYLED */
20200Sstevel@tonic-gate 				if (rtld_flags & RT_FL_SECURE) {
20210Sstevel@tonic-gate 					profile_lib =
20220Sstevel@tonic-gate #if	defined(_ELF64)
20230Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE_64);
20240Sstevel@tonic-gate #else
20250Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE);
20260Sstevel@tonic-gate #endif
20270Sstevel@tonic-gate 				} else {
20280Sstevel@tonic-gate 					profile_lib =
20290Sstevel@tonic-gate #if	defined(_ELF64)
20300Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF_64);
20310Sstevel@tonic-gate #else
20320Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF);
20330Sstevel@tonic-gate #endif
20340Sstevel@tonic-gate 				}
20354362Srie 				/* END CSTYLED */
20360Sstevel@tonic-gate 			} else
20378598SRod.Evans@Sun.COM 				profile_lib = NULL;
20380Sstevel@tonic-gate 		} else if (variable == ENV_FLG_SIGNAL) {
20390Sstevel@tonic-gate 			killsig = s2 ? atoi(s2) : SIGKILL;
20400Sstevel@tonic-gate 		} else if (variable == ENV_FLG_TRACE_OBJS) {
20410Sstevel@tonic-gate 			if (s2) {
20420Sstevel@tonic-gate 				*lmflags |= LML_FLG_TRC_ENABLE;
20430Sstevel@tonic-gate 				if (*s2 == '2')
20440Sstevel@tonic-gate 					*lmflags |= LML_FLG_TRC_LDDSTUB;
20450Sstevel@tonic-gate 			} else
20460Sstevel@tonic-gate 				*lmflags &=
20479340SRod.Evans@Sun.COM 				    ~(LML_FLG_TRC_ENABLE | LML_FLG_TRC_LDDSTUB);
20480Sstevel@tonic-gate 		}
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate }
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate /*
20530Sstevel@tonic-gate  * Determine whether we have an architecture specific environment variable.
20540Sstevel@tonic-gate  * If we do, and we're the wrong architecture, it'll just get ignored.
20550Sstevel@tonic-gate  * Otherwise the variable is processed in it's architecture neutral form.
20560Sstevel@tonic-gate  */
20570Sstevel@tonic-gate static int
20580Sstevel@tonic-gate ld_arch_env(const char *s1, size_t *len)
20590Sstevel@tonic-gate {
20600Sstevel@tonic-gate 	size_t	_len = *len - 3;
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	if (s1[_len++] == '_') {
20630Sstevel@tonic-gate 		if ((s1[_len] == '3') && (s1[_len + 1] == '2')) {
20640Sstevel@tonic-gate #if	defined(_ELF64)
20650Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
20660Sstevel@tonic-gate #else
20670Sstevel@tonic-gate 			*len = *len - 3;
20680Sstevel@tonic-gate 			return (ENV_TYP_ISA);
20690Sstevel@tonic-gate #endif
20700Sstevel@tonic-gate 		}
20710Sstevel@tonic-gate 		if ((s1[_len] == '6') && (s1[_len + 1] == '4')) {
20720Sstevel@tonic-gate #if	defined(_ELF64)
20730Sstevel@tonic-gate 			*len = *len - 3;
20740Sstevel@tonic-gate 			return (ENV_TYP_ISA);
20750Sstevel@tonic-gate #else
20760Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
20770Sstevel@tonic-gate #endif
20780Sstevel@tonic-gate 		}
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 	return (0);
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 
20840Sstevel@tonic-gate /*
20850Sstevel@tonic-gate  * Process an LD_FLAGS environment variable.  The value can be a comma
20860Sstevel@tonic-gate  * separated set of tokens, which are sent (in upper case) into the generic
20870Sstevel@tonic-gate  * LD_XXXX environment variable engine.  For example:
20880Sstevel@tonic-gate  *
20890Sstevel@tonic-gate  *	LD_FLAGS=bind_now		->	LD_BIND_NOW=1
20900Sstevel@tonic-gate  *	LD_FLAGS=library_path=/foo:.	->	LD_LIBRARY_PATH=/foo:.
20910Sstevel@tonic-gate  *	LD_FLAGS=debug=files:detail	->	LD_DEBUG=files:detail
20920Sstevel@tonic-gate  * or
20930Sstevel@tonic-gate  *	LD_FLAGS=bind_now,library_path=/foo:.,debug=files:detail
20940Sstevel@tonic-gate  */
20950Sstevel@tonic-gate static int
20960Sstevel@tonic-gate ld_flags_env(const char *str, Word *lmflags, Word *lmtflags,
20970Sstevel@tonic-gate     uint_t env_flags, int aout)
20980Sstevel@tonic-gate {
20997668SRod.Evans@Sun.COM 	char	*nstr, *sstr, *estr = NULL;
21000Sstevel@tonic-gate 	size_t	nlen, len;
21010Sstevel@tonic-gate 
21027668SRod.Evans@Sun.COM 	if (str == NULL)
21030Sstevel@tonic-gate 		return (0);
21040Sstevel@tonic-gate 
21050Sstevel@tonic-gate 	/*
21060Sstevel@tonic-gate 	 * Create a new string as we're going to transform the token(s) into
21070Sstevel@tonic-gate 	 * uppercase and separate tokens with nulls.
21080Sstevel@tonic-gate 	 */
21090Sstevel@tonic-gate 	len = strlen(str);
21107668SRod.Evans@Sun.COM 	if ((nstr = malloc(len + 1)) == NULL)
21110Sstevel@tonic-gate 		return (1);
21120Sstevel@tonic-gate 	(void) strcpy(nstr, str);
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	for (sstr = nstr; sstr; sstr++, len--) {
21150Sstevel@tonic-gate 		int	flags;
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 		if ((*sstr != '\0') && (*sstr != ',')) {
21187668SRod.Evans@Sun.COM 			if (estr == NULL) {
21190Sstevel@tonic-gate 				if (*sstr == '=')
21200Sstevel@tonic-gate 					estr = sstr;
21210Sstevel@tonic-gate 				else {
21220Sstevel@tonic-gate 					/*
21230Sstevel@tonic-gate 					 * Translate token to uppercase.  Don't
21240Sstevel@tonic-gate 					 * use toupper(3C) as including this
21250Sstevel@tonic-gate 					 * code doubles the size of ld.so.1.
21260Sstevel@tonic-gate 					 */
21270Sstevel@tonic-gate 					if ((*sstr >= 'a') && (*sstr <= 'z'))
21280Sstevel@tonic-gate 						*sstr = *sstr - ('a' - 'A');
21290Sstevel@tonic-gate 				}
21300Sstevel@tonic-gate 			}
21310Sstevel@tonic-gate 			continue;
21320Sstevel@tonic-gate 		}
21330Sstevel@tonic-gate 
21340Sstevel@tonic-gate 		*sstr = '\0';
21350Sstevel@tonic-gate 		if (estr) {
21360Sstevel@tonic-gate 			nlen = estr - nstr;
21370Sstevel@tonic-gate 			if ((*++estr == '\0') || (*estr == ','))
21388883SRod.Evans@Sun.COM 				estr = NULL;
21390Sstevel@tonic-gate 		} else
21400Sstevel@tonic-gate 			nlen = sstr - nstr;
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 		/*
21430Sstevel@tonic-gate 		 * Fabricate a boolean definition for any unqualified variable.
21440Sstevel@tonic-gate 		 * Thus LD_FLAGS=bind_now is represented as BIND_NOW=(null).
21450Sstevel@tonic-gate 		 * The value is sufficient to assert any boolean variables, plus
21460Sstevel@tonic-gate 		 * the term "(null)" is specifically chosen in case someone
21470Sstevel@tonic-gate 		 * mistakenly supplies something like LD_FLAGS=library_path.
21480Sstevel@tonic-gate 		 */
21497668SRod.Evans@Sun.COM 		if (estr == NULL)
21500Sstevel@tonic-gate 			estr = (char *)MSG_INTL(MSG_STR_NULL);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 		/*
21530Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
21540Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
21550Sstevel@tonic-gate 		 * neutral portion of the string.
21560Sstevel@tonic-gate 		 */
21570Sstevel@tonic-gate 		if ((flags = ld_arch_env(nstr, &nlen)) != ENV_TYP_IGNORE) {
21580Sstevel@tonic-gate 			ld_generic_env(nstr, nlen, estr, lmflags,
21590Sstevel@tonic-gate 			    lmtflags, (env_flags | flags), aout);
21600Sstevel@tonic-gate 		}
21610Sstevel@tonic-gate 		if (len == 0)
21620Sstevel@tonic-gate 			return (0);
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate 		nstr = sstr + 1;
21658883SRod.Evans@Sun.COM 		estr = NULL;
21660Sstevel@tonic-gate 	}
21670Sstevel@tonic-gate 	return (0);
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate 
21700Sstevel@tonic-gate 
21710Sstevel@tonic-gate /*
21720Sstevel@tonic-gate  * Process a single environment string.  Only strings starting with `LD_' are
21730Sstevel@tonic-gate  * reserved for our use.  By convention, all strings should be of the form
21740Sstevel@tonic-gate  * `LD_XXXX=', if the string is followed by a non-null value the appropriate
21750Sstevel@tonic-gate  * functionality is enabled.  Also pick off applicable locale variables.
21760Sstevel@tonic-gate  */
21770Sstevel@tonic-gate #define	LOC_LANG	1
21780Sstevel@tonic-gate #define	LOC_MESG	2
21790Sstevel@tonic-gate #define	LOC_ALL		3
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate static void
21820Sstevel@tonic-gate ld_str_env(const char *s1, Word *lmflags, Word *lmtflags, uint_t env_flags,
21830Sstevel@tonic-gate     int aout)
21840Sstevel@tonic-gate {
21850Sstevel@tonic-gate 	const char	*s2;
21861824Srie 	static		size_t	loc = 0;
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	if (*s1++ != 'L')
21890Sstevel@tonic-gate 		return;
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate 	/*
21920Sstevel@tonic-gate 	 * See if we have any locale environment settings.  These environment
21930Sstevel@tonic-gate 	 * variables have a precedence, LC_ALL is higher than LC_MESSAGES which
21940Sstevel@tonic-gate 	 * is higher than LANG.
21950Sstevel@tonic-gate 	 */
21960Sstevel@tonic-gate 	s2 = s1;
21970Sstevel@tonic-gate 	if ((*s2++ == 'C') && (*s2++ == '_') && (*s2 != '\0')) {
21980Sstevel@tonic-gate 		if (strncmp(s2, MSG_ORIG(MSG_LC_ALL), MSG_LC_ALL_SIZE) == 0) {
21990Sstevel@tonic-gate 			s2 += MSG_LC_ALL_SIZE;
22000Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_ALL)) {
22011824Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22020Sstevel@tonic-gate 				loc = LOC_ALL;
22030Sstevel@tonic-gate 			}
22040Sstevel@tonic-gate 		} else if (strncmp(s2, MSG_ORIG(MSG_LC_MESSAGES),
22050Sstevel@tonic-gate 		    MSG_LC_MESSAGES_SIZE) == 0) {
22060Sstevel@tonic-gate 			s2 += MSG_LC_MESSAGES_SIZE;
22070Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_MESG)) {
22081824Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22090Sstevel@tonic-gate 				loc = LOC_MESG;
22100Sstevel@tonic-gate 			}
22110Sstevel@tonic-gate 		}
22120Sstevel@tonic-gate 		return;
22130Sstevel@tonic-gate 	}
22140Sstevel@tonic-gate 
22150Sstevel@tonic-gate 	s2 = s1;
22160Sstevel@tonic-gate 	if ((*s2++ == 'A') && (*s2++ == 'N') && (*s2++ == 'G') &&
22170Sstevel@tonic-gate 	    (*s2++ == '=') && (*s2 != '\0') && (loc < LOC_LANG)) {
22181824Srie 		glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22190Sstevel@tonic-gate 		loc = LOC_LANG;
22200Sstevel@tonic-gate 		return;
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 	/*
22240Sstevel@tonic-gate 	 * Pick off any LD_XXXX environment variables.
22250Sstevel@tonic-gate 	 */
22260Sstevel@tonic-gate 	if ((*s1++ == 'D') && (*s1++ == '_') && (*s1 != '\0')) {
22270Sstevel@tonic-gate 		size_t	len;
22280Sstevel@tonic-gate 		int	flags;
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 		/*
22312712Snn35248 		 * In a branded process we must ignore all LD_XXXX env vars
22322712Snn35248 		 * because they are intended for the brand's linker.
22332712Snn35248 		 * To affect the Solaris linker, use LD_BRAND_XXXX instead.
22342712Snn35248 		 */
22352712Snn35248 		if (rtld_flags2 & RT_FL2_BRANDED) {
22362712Snn35248 			if (strncmp(s1, MSG_ORIG(MSG_LD_BRAND_PREFIX),
22372712Snn35248 			    MSG_LD_BRAND_PREFIX_SIZE) != 0)
22382712Snn35248 				return;
22392712Snn35248 			s1 += MSG_LD_BRAND_PREFIX_SIZE;
22402712Snn35248 		}
22412712Snn35248 
22422712Snn35248 		/*
22430Sstevel@tonic-gate 		 * Environment variables with no value (ie. LD_XXXX=) typically
22440Sstevel@tonic-gate 		 * have no impact, however if environment variables are defined
22450Sstevel@tonic-gate 		 * within a configuration file, these null user settings can be
22460Sstevel@tonic-gate 		 * used to disable any configuration replaceable definitions.
22470Sstevel@tonic-gate 		 */
22487668SRod.Evans@Sun.COM 		if ((s2 = strchr(s1, '=')) == NULL) {
22490Sstevel@tonic-gate 			len = strlen(s1);
22508883SRod.Evans@Sun.COM 			s2 = NULL;
22510Sstevel@tonic-gate 		} else if (*++s2 == '\0') {
22520Sstevel@tonic-gate 			len = strlen(s1) - 1;
22538883SRod.Evans@Sun.COM 			s2 = NULL;
22540Sstevel@tonic-gate 		} else {
22550Sstevel@tonic-gate 			len = s2 - s1 - 1;
22569406SAli.Bahrami@Sun.COM 			while (conv_strproc_isspace(*s2))
22570Sstevel@tonic-gate 				s2++;
22580Sstevel@tonic-gate 		}
22590Sstevel@tonic-gate 
22600Sstevel@tonic-gate 		/*
22610Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
22620Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
22630Sstevel@tonic-gate 		 * neutral portion of the string.
22640Sstevel@tonic-gate 		 */
22650Sstevel@tonic-gate 		if ((flags = ld_arch_env(s1, &len)) == ENV_TYP_IGNORE)
22660Sstevel@tonic-gate 			return;
22670Sstevel@tonic-gate 		env_flags |= flags;
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 		ld_generic_env(s1, len, s2, lmflags, lmtflags, env_flags, aout);
22700Sstevel@tonic-gate 	}
22710Sstevel@tonic-gate }
22720Sstevel@tonic-gate 
22730Sstevel@tonic-gate /*
22740Sstevel@tonic-gate  * Internal getenv routine.  Called immediately after ld.so.1 initializes
22750Sstevel@tonic-gate  * itself.
22760Sstevel@tonic-gate  */
22770Sstevel@tonic-gate int
22788598SRod.Evans@Sun.COM readenv_user(const char **envp, Word *lmflags, Word *lmtflags, int aout)
22790Sstevel@tonic-gate {
22801824Srie 	char	*locale;
22811824Srie 
22828598SRod.Evans@Sun.COM 	if (envp == NULL)
22830Sstevel@tonic-gate 		return (0);
22840Sstevel@tonic-gate 
22858598SRod.Evans@Sun.COM 	while (*envp != NULL)
22860Sstevel@tonic-gate 		ld_str_env(*envp++, lmflags, lmtflags, 0, aout);
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 	/*
22890Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
22900Sstevel@tonic-gate 	 * these strings.
22910Sstevel@tonic-gate 	 */
22920Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
22930Sstevel@tonic-gate 		return (1);
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 	/*
22960Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
22970Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
22980Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
22990Sstevel@tonic-gate 	 */
23000Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
23018598SRod.Evans@Sun.COM 		rpl_audit = profile_lib = profile_name = NULL;
23020Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
23030Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
23040Sstevel@tonic-gate 
23050Sstevel@tonic-gate 	/*
2306280Srie 	 * If both LD_BIND_NOW and LD_BIND_LAZY are specified, the former wins.
2307280Srie 	 */
2308280Srie 	if ((rtld_flags2 & (RT_FL2_BINDNOW | RT_FL2_BINDLAZY)) ==
2309280Srie 	    (RT_FL2_BINDNOW | RT_FL2_BINDLAZY))
2310280Srie 		rtld_flags2 &= ~RT_FL2_BINDLAZY;
2311280Srie 
2312280Srie 	/*
23136150Srie 	 * When using ldd(1) -r or -d against an executable, assert -p.
23146150Srie 	 */
23156150Srie 	if ((*lmflags &
23166150Srie 	    (LML_FLG_TRC_WARN | LML_FLG_TRC_LDDSTUB)) == LML_FLG_TRC_WARN)
23176150Srie 		*lmflags |= LML_FLG_TRC_NOPAREXT;
23186150Srie 
23196150Srie 	/*
23200Sstevel@tonic-gate 	 * If we have a locale setting make sure its worth processing further.
23213191Srie 	 * C and POSIX locales don't need any processing.  In addition, to
23223191Srie 	 * ensure no one escapes the /usr/lib/locale hierarchy, don't allow
23233191Srie 	 * the locale to contain a segment that leads upward in the file system
23243191Srie 	 * hierarchy (i.e. no '..' segments).   Given that we'll be confined to
23253191Srie 	 * the /usr/lib/locale hierarchy, there is no need to extensively
23263191Srie 	 * validate the mode or ownership of any message file (as libc's
23273191Srie 	 * generic handling of message files does).  Duplicate the string so
23283191Srie 	 * that new locale setting can generically cleanup any previous locales.
23290Sstevel@tonic-gate 	 */
23308883SRod.Evans@Sun.COM 	if ((locale = glcs[CI_LCMESSAGES].lc_un.lc_ptr) != NULL) {
23310Sstevel@tonic-gate 		if (((*locale == 'C') && (*(locale + 1) == '\0')) ||
23323191Srie 		    (strcmp(locale, MSG_ORIG(MSG_TKN_POSIX)) == 0) ||
23333191Srie 		    (strstr(locale, MSG_ORIG(MSG_TKN_DOTDOT)) != NULL))
23348883SRod.Evans@Sun.COM 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = NULL;
23350Sstevel@tonic-gate 		else
23361824Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = strdup(locale);
23370Sstevel@tonic-gate 	}
23380Sstevel@tonic-gate 	return (0);
23390Sstevel@tonic-gate }
23400Sstevel@tonic-gate 
23410Sstevel@tonic-gate /*
23420Sstevel@tonic-gate  * Configuration environment processing.  Called after the a.out has been
23430Sstevel@tonic-gate  * processed (as the a.out can specify its own configuration file).
23440Sstevel@tonic-gate  */
23450Sstevel@tonic-gate int
23460Sstevel@tonic-gate readenv_config(Rtc_env * envtbl, Addr addr, int aout)
23470Sstevel@tonic-gate {
23486387Srie 	Word	*lmflags = &(lml_main.lm_flags);
23496387Srie 	Word	*lmtflags = &(lml_main.lm_tflags);
23500Sstevel@tonic-gate 
23518598SRod.Evans@Sun.COM 	if (envtbl == NULL)
23520Sstevel@tonic-gate 		return (0);
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	while (envtbl->env_str) {
23550Sstevel@tonic-gate 		uint_t	env_flags = ENV_TYP_CONFIG;
23560Sstevel@tonic-gate 
23570Sstevel@tonic-gate 		if (envtbl->env_flags & RTC_ENV_PERMANT)
23580Sstevel@tonic-gate 			env_flags |= ENV_TYP_PERMANT;
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate 		ld_str_env((const char *)(envtbl->env_str + addr),
23610Sstevel@tonic-gate 		    lmflags, lmtflags, env_flags, 0);
23620Sstevel@tonic-gate 		envtbl++;
23630Sstevel@tonic-gate 	}
23640Sstevel@tonic-gate 
23650Sstevel@tonic-gate 	/*
23660Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
23670Sstevel@tonic-gate 	 * these strings.
23680Sstevel@tonic-gate 	 */
23690Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
23700Sstevel@tonic-gate 		return (1);
23710Sstevel@tonic-gate 	if (ld_flags_env(prm_ldflags, lmflags, lmtflags, ENV_TYP_CONFIG,
23720Sstevel@tonic-gate 	    aout) == 1)
23730Sstevel@tonic-gate 		return (1);
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	/*
23760Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
23770Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
23780Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
23790Sstevel@tonic-gate 	 */
23800Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
23818598SRod.Evans@Sun.COM 		prm_audit = profile_lib = profile_name = NULL;
23820Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
23830Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate 	return (0);
23860Sstevel@tonic-gate }
23870Sstevel@tonic-gate 
23880Sstevel@tonic-gate int
23890Sstevel@tonic-gate dowrite(Prfbuf * prf)
23900Sstevel@tonic-gate {
23910Sstevel@tonic-gate 	/*
23920Sstevel@tonic-gate 	 * We do not have a valid file descriptor, so we are unable
23930Sstevel@tonic-gate 	 * to flush the buffer.
23940Sstevel@tonic-gate 	 */
23950Sstevel@tonic-gate 	if (prf->pr_fd == -1)
23960Sstevel@tonic-gate 		return (0);
23970Sstevel@tonic-gate 	(void) write(prf->pr_fd, prf->pr_buf, prf->pr_cur - prf->pr_buf);
23980Sstevel@tonic-gate 	prf->pr_cur = prf->pr_buf;
23990Sstevel@tonic-gate 	return (1);
24000Sstevel@tonic-gate }
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate /*
24030Sstevel@tonic-gate  * Simplified printing.  The following conversion specifications are supported:
24040Sstevel@tonic-gate  *
24050Sstevel@tonic-gate  *	% [#] [-] [min field width] [. precision] s|d|x|c
24060Sstevel@tonic-gate  *
24070Sstevel@tonic-gate  *
24080Sstevel@tonic-gate  * dorprf takes the output buffer in the form of Prfbuf which permits
24090Sstevel@tonic-gate  * the verification of the output buffer size and the concatenation
24100Sstevel@tonic-gate  * of data to an already existing output buffer.  The Prfbuf
24110Sstevel@tonic-gate  * structure contains the following:
24120Sstevel@tonic-gate  *
24130Sstevel@tonic-gate  *  pr_buf	pointer to the beginning of the output buffer.
24140Sstevel@tonic-gate  *  pr_cur	pointer to the next available byte in the output buffer.  By
24150Sstevel@tonic-gate  *		setting pr_cur ahead of pr_buf you can append to an already
24160Sstevel@tonic-gate  *		existing buffer.
24170Sstevel@tonic-gate  *  pr_len	the size of the output buffer.  By setting pr_len to '0' you
24180Sstevel@tonic-gate  *		disable protection from overflows in the output buffer.
24190Sstevel@tonic-gate  *  pr_fd	a pointer to the file-descriptor the buffer will eventually be
24200Sstevel@tonic-gate  *		output to.  If pr_fd is set to '-1' then it's assumed there is
24213191Srie  *		no output buffer, and doprf() will return with an error to
24223191Srie  *		indicate an output buffer overflow.  If pr_fd is > -1 then when
24233191Srie  *		the output buffer is filled it will be flushed to pr_fd and will
24243191Srie  *		then be	available for additional data.
24250Sstevel@tonic-gate  */
24260Sstevel@tonic-gate #define	FLG_UT_MINUS	0x0001	/* - */
24270Sstevel@tonic-gate #define	FLG_UT_SHARP	0x0002	/* # */
24280Sstevel@tonic-gate #define	FLG_UT_DOTSEEN	0x0008	/* dot appeared in format spec */
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate /*
24311824Srie  * This macro is for use from within doprf only.  It is to be used for checking
24321824Srie  * the output buffer size and placing characters into the buffer.
24330Sstevel@tonic-gate  */
24340Sstevel@tonic-gate #define	PUTC(c) \
24350Sstevel@tonic-gate 	{ \
24361824Srie 		char tmpc; \
24370Sstevel@tonic-gate 		\
24380Sstevel@tonic-gate 		tmpc = (c); \
24391824Srie 		if (bufsiz && (bp >= bufend)) { \
24400Sstevel@tonic-gate 			prf->pr_cur = bp; \
24410Sstevel@tonic-gate 			if (dowrite(prf) == 0) \
24420Sstevel@tonic-gate 				return (0); \
24430Sstevel@tonic-gate 			bp = prf->pr_cur; \
24440Sstevel@tonic-gate 		} \
24450Sstevel@tonic-gate 		*bp++ = tmpc; \
24460Sstevel@tonic-gate 	}
24470Sstevel@tonic-gate 
24483191Srie /*
24493191Srie  * Define a local buffer size for building a numeric value - large enough to
24503191Srie  * hold a 64-bit value.
24513191Srie  */
24523304Srie #define	NUM_SIZE	22
24533191Srie 
24540Sstevel@tonic-gate size_t
24550Sstevel@tonic-gate doprf(const char *format, va_list args, Prfbuf *prf)
24560Sstevel@tonic-gate {
24570Sstevel@tonic-gate 	char	c;
24580Sstevel@tonic-gate 	char	*bp = prf->pr_cur;
24590Sstevel@tonic-gate 	char	*bufend = prf->pr_buf + prf->pr_len;
24600Sstevel@tonic-gate 	size_t	bufsiz = prf->pr_len;
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 	while ((c = *format++) != '\0') {
24630Sstevel@tonic-gate 		if (c != '%') {
24640Sstevel@tonic-gate 			PUTC(c);
24650Sstevel@tonic-gate 		} else {
24660Sstevel@tonic-gate 			int	base = 0, flag = 0, width = 0, prec = 0;
24670Sstevel@tonic-gate 			size_t	_i;
24680Sstevel@tonic-gate 			int	_c, _n;
24690Sstevel@tonic-gate 			char	*_s;
24700Sstevel@tonic-gate 			int	ls = 0;
24710Sstevel@tonic-gate again:
24720Sstevel@tonic-gate 			c = *format++;
24730Sstevel@tonic-gate 			switch (c) {
24740Sstevel@tonic-gate 			case '-':
24750Sstevel@tonic-gate 				flag |= FLG_UT_MINUS;
24760Sstevel@tonic-gate 				goto again;
24770Sstevel@tonic-gate 			case '#':
24780Sstevel@tonic-gate 				flag |= FLG_UT_SHARP;
24790Sstevel@tonic-gate 				goto again;
24800Sstevel@tonic-gate 			case '.':
24810Sstevel@tonic-gate 				flag |= FLG_UT_DOTSEEN;
24820Sstevel@tonic-gate 				goto again;
24830Sstevel@tonic-gate 			case '0':
24840Sstevel@tonic-gate 			case '1':
24850Sstevel@tonic-gate 			case '2':
24860Sstevel@tonic-gate 			case '3':
24870Sstevel@tonic-gate 			case '4':
24880Sstevel@tonic-gate 			case '5':
24890Sstevel@tonic-gate 			case '6':
24900Sstevel@tonic-gate 			case '7':
24910Sstevel@tonic-gate 			case '8':
24920Sstevel@tonic-gate 			case '9':
24930Sstevel@tonic-gate 				if (flag & FLG_UT_DOTSEEN)
24940Sstevel@tonic-gate 					prec = (prec * 10) + c - '0';
24950Sstevel@tonic-gate 				else
24960Sstevel@tonic-gate 					width = (width * 10) + c - '0';
24970Sstevel@tonic-gate 				goto again;
24980Sstevel@tonic-gate 			case 'x':
24990Sstevel@tonic-gate 			case 'X':
25000Sstevel@tonic-gate 				base = 16;
25010Sstevel@tonic-gate 				break;
25020Sstevel@tonic-gate 			case 'd':
25030Sstevel@tonic-gate 			case 'D':
25040Sstevel@tonic-gate 			case 'u':
25050Sstevel@tonic-gate 				base = 10;
25060Sstevel@tonic-gate 				flag &= ~FLG_UT_SHARP;
25070Sstevel@tonic-gate 				break;
25080Sstevel@tonic-gate 			case 'l':
25090Sstevel@tonic-gate 				base = 10;
25100Sstevel@tonic-gate 				ls++; /* number of l's (long or long long) */
25110Sstevel@tonic-gate 				if ((*format == 'l') ||
25120Sstevel@tonic-gate 				    (*format == 'd') || (*format == 'D') ||
25130Sstevel@tonic-gate 				    (*format == 'x') || (*format == 'X') ||
25140Sstevel@tonic-gate 				    (*format == 'o') || (*format == 'O'))
25150Sstevel@tonic-gate 					goto again;
25160Sstevel@tonic-gate 				break;
25170Sstevel@tonic-gate 			case 'o':
25180Sstevel@tonic-gate 			case 'O':
25190Sstevel@tonic-gate 				base = 8;
25200Sstevel@tonic-gate 				break;
25210Sstevel@tonic-gate 			case 'c':
25220Sstevel@tonic-gate 				_c = va_arg(args, int);
25230Sstevel@tonic-gate 
25240Sstevel@tonic-gate 				for (_i = 24; _i > 0; _i -= 8) {
25250Sstevel@tonic-gate 					if ((c = ((_c >> _i) & 0x7f)) != 0) {
25260Sstevel@tonic-gate 						PUTC(c);
25270Sstevel@tonic-gate 					}
25280Sstevel@tonic-gate 				}
25290Sstevel@tonic-gate 				if ((c = ((_c >> _i) & 0x7f)) != 0) {
25300Sstevel@tonic-gate 					PUTC(c);
25310Sstevel@tonic-gate 				}
25320Sstevel@tonic-gate 				break;
25330Sstevel@tonic-gate 			case 's':
25340Sstevel@tonic-gate 				_s = va_arg(args, char *);
25350Sstevel@tonic-gate 				_i = strlen(_s);
25360Sstevel@tonic-gate 				/* LINTED */
25370Sstevel@tonic-gate 				_n = (int)(width - _i);
25380Sstevel@tonic-gate 				if (!prec)
25390Sstevel@tonic-gate 					/* LINTED */
25400Sstevel@tonic-gate 					prec = (int)_i;
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
25430Sstevel@tonic-gate 					while (_n-- > 0)
25440Sstevel@tonic-gate 						PUTC(' ');
25450Sstevel@tonic-gate 				}
25460Sstevel@tonic-gate 				while (((c = *_s++) != 0) && prec--) {
25470Sstevel@tonic-gate 					PUTC(c);
25480Sstevel@tonic-gate 				}
25490Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
25500Sstevel@tonic-gate 					while (_n-- > 0)
25510Sstevel@tonic-gate 						PUTC(' ');
25520Sstevel@tonic-gate 				}
25530Sstevel@tonic-gate 				break;
25540Sstevel@tonic-gate 			case '%':
25550Sstevel@tonic-gate 				PUTC('%');
25560Sstevel@tonic-gate 				break;
25570Sstevel@tonic-gate 			default:
25580Sstevel@tonic-gate 				break;
25590Sstevel@tonic-gate 			}
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 			/*
25620Sstevel@tonic-gate 			 * Numeric processing
25630Sstevel@tonic-gate 			 */
25640Sstevel@tonic-gate 			if (base) {
25653191Srie 				char		local[NUM_SIZE];
25663191Srie 				size_t		ssize = 0, psize = 0;
25670Sstevel@tonic-gate 				const char	*string =
25684362Srie 				    MSG_ORIG(MSG_STR_HEXNUM);
25690Sstevel@tonic-gate 				const char	*prefix =
25704362Srie 				    MSG_ORIG(MSG_STR_EMPTY);
25710Sstevel@tonic-gate 				u_longlong_t	num;
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 				switch (ls) {
25740Sstevel@tonic-gate 				case 0:	/* int */
25750Sstevel@tonic-gate 					num = (u_longlong_t)
25760Sstevel@tonic-gate 					    va_arg(args, uint_t);
25770Sstevel@tonic-gate 					break;
25780Sstevel@tonic-gate 				case 1:	/* long */
25790Sstevel@tonic-gate 					num = (u_longlong_t)
25800Sstevel@tonic-gate 					    va_arg(args, ulong_t);
25810Sstevel@tonic-gate 					break;
25820Sstevel@tonic-gate 				case 2:	/* long long */
25830Sstevel@tonic-gate 					num = va_arg(args, u_longlong_t);
25840Sstevel@tonic-gate 					break;
25850Sstevel@tonic-gate 				}
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 				if (flag & FLG_UT_SHARP) {
25880Sstevel@tonic-gate 					if (base == 16) {
25890Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_HEX);
25900Sstevel@tonic-gate 						psize = 2;
25910Sstevel@tonic-gate 					} else {
25920Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_ZERO);
25930Sstevel@tonic-gate 						psize = 1;
25940Sstevel@tonic-gate 					}
25950Sstevel@tonic-gate 				}
25960Sstevel@tonic-gate 				if ((base == 10) && (long)num < 0) {
25970Sstevel@tonic-gate 					prefix = MSG_ORIG(MSG_STR_NEGATE);
25980Sstevel@tonic-gate 					psize = MSG_STR_NEGATE_SIZE;
25990Sstevel@tonic-gate 					num = (u_longlong_t)(-(longlong_t)num);
26000Sstevel@tonic-gate 				}
26010Sstevel@tonic-gate 
26020Sstevel@tonic-gate 				/*
26030Sstevel@tonic-gate 				 * Convert the numeric value into a local
26040Sstevel@tonic-gate 				 * string (stored in reverse order).
26050Sstevel@tonic-gate 				 */
26060Sstevel@tonic-gate 				_s = local;
26070Sstevel@tonic-gate 				do {
26080Sstevel@tonic-gate 					*_s++ = string[num % base];
26090Sstevel@tonic-gate 					num /= base;
26100Sstevel@tonic-gate 					ssize++;
26110Sstevel@tonic-gate 				} while (num);
26120Sstevel@tonic-gate 
26133191Srie 				ASSERT(ssize < sizeof (local));
26143191Srie 
26150Sstevel@tonic-gate 				/*
26160Sstevel@tonic-gate 				 * Provide any precision or width padding.
26170Sstevel@tonic-gate 				 */
26180Sstevel@tonic-gate 				if (prec) {
26190Sstevel@tonic-gate 					/* LINTED */
26200Sstevel@tonic-gate 					_n = (int)(prec - ssize);
26213191Srie 					while ((_n-- > 0) &&
26223191Srie 					    (ssize < sizeof (local))) {
26230Sstevel@tonic-gate 						*_s++ = '0';
26240Sstevel@tonic-gate 						ssize++;
26250Sstevel@tonic-gate 					}
26260Sstevel@tonic-gate 				}
26270Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
26280Sstevel@tonic-gate 					/* LINTED */
26290Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
26300Sstevel@tonic-gate 					while (_n-- > 0) {
26310Sstevel@tonic-gate 						PUTC(' ');
26320Sstevel@tonic-gate 					}
26330Sstevel@tonic-gate 				}
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 				/*
26360Sstevel@tonic-gate 				 * Print any prefix and the numeric string
26370Sstevel@tonic-gate 				 */
26380Sstevel@tonic-gate 				while (*prefix)
26390Sstevel@tonic-gate 					PUTC(*prefix++);
26400Sstevel@tonic-gate 				do {
26410Sstevel@tonic-gate 					PUTC(*--_s);
26420Sstevel@tonic-gate 				} while (_s > local);
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 				/*
26450Sstevel@tonic-gate 				 * Provide any width padding.
26460Sstevel@tonic-gate 				 */
26470Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
26480Sstevel@tonic-gate 					/* LINTED */
26490Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
26500Sstevel@tonic-gate 					while (_n-- > 0)
26510Sstevel@tonic-gate 						PUTC(' ');
26520Sstevel@tonic-gate 				}
26530Sstevel@tonic-gate 			}
26540Sstevel@tonic-gate 		}
26550Sstevel@tonic-gate 	}
26561824Srie 
26570Sstevel@tonic-gate 	PUTC('\0');
26580Sstevel@tonic-gate 	prf->pr_cur = bp;
26590Sstevel@tonic-gate 	return (1);
26600Sstevel@tonic-gate }
26610Sstevel@tonic-gate 
26620Sstevel@tonic-gate static int
26630Sstevel@tonic-gate doprintf(const char *format, va_list args, Prfbuf *prf)
26640Sstevel@tonic-gate {
26650Sstevel@tonic-gate 	char	*ocur = prf->pr_cur;
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	if (doprf(format, args, prf) == 0)
26680Sstevel@tonic-gate 		return (0);
26690Sstevel@tonic-gate 	/* LINTED */
26700Sstevel@tonic-gate 	return ((int)(prf->pr_cur - ocur));
26710Sstevel@tonic-gate }
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate /* VARARGS2 */
26740Sstevel@tonic-gate int
26750Sstevel@tonic-gate sprintf(char *buf, const char *format, ...)
26760Sstevel@tonic-gate {
26770Sstevel@tonic-gate 	va_list	args;
26780Sstevel@tonic-gate 	int	len;
26790Sstevel@tonic-gate 	Prfbuf	prf;
26800Sstevel@tonic-gate 
26810Sstevel@tonic-gate 	va_start(args, format);
26820Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
26830Sstevel@tonic-gate 	prf.pr_len = 0;
26840Sstevel@tonic-gate 	prf.pr_fd = -1;
26850Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
26860Sstevel@tonic-gate 	va_end(args);
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 	/*
26890Sstevel@tonic-gate 	 * sprintf() return value excludes the terminating null byte.
26900Sstevel@tonic-gate 	 */
26910Sstevel@tonic-gate 	return (len - 1);
26920Sstevel@tonic-gate }
26930Sstevel@tonic-gate 
26940Sstevel@tonic-gate /* VARARGS3 */
26950Sstevel@tonic-gate int
26960Sstevel@tonic-gate snprintf(char *buf, size_t n, const char *format, ...)
26970Sstevel@tonic-gate {
26980Sstevel@tonic-gate 	va_list	args;
26990Sstevel@tonic-gate 	int	len;
27000Sstevel@tonic-gate 	Prfbuf	prf;
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	va_start(args, format);
27030Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
27040Sstevel@tonic-gate 	prf.pr_len = n;
27050Sstevel@tonic-gate 	prf.pr_fd = -1;
27060Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
27070Sstevel@tonic-gate 	va_end(args);
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 	return (len);
27100Sstevel@tonic-gate }
27110Sstevel@tonic-gate 
27120Sstevel@tonic-gate /* VARARGS2 */
27130Sstevel@tonic-gate int
27140Sstevel@tonic-gate bufprint(Prfbuf *prf, const char *format, ...)
27150Sstevel@tonic-gate {
27160Sstevel@tonic-gate 	va_list	args;
27170Sstevel@tonic-gate 	int	len;
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	va_start(args, format);
27200Sstevel@tonic-gate 	len = doprintf(format, args, prf);
27210Sstevel@tonic-gate 	va_end(args);
27220Sstevel@tonic-gate 
27230Sstevel@tonic-gate 	return (len);
27240Sstevel@tonic-gate }
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate /*PRINTFLIKE1*/
27270Sstevel@tonic-gate int
27280Sstevel@tonic-gate printf(const char *format, ...)
27290Sstevel@tonic-gate {
27300Sstevel@tonic-gate 	va_list	args;
27310Sstevel@tonic-gate 	char 	buffer[ERRSIZE];
27320Sstevel@tonic-gate 	Prfbuf	prf;
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	va_start(args, format);
27350Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buffer;
27360Sstevel@tonic-gate 	prf.pr_len = ERRSIZE;
27370Sstevel@tonic-gate 	prf.pr_fd = 1;
27380Sstevel@tonic-gate 	(void) doprf(format, args, &prf);
27390Sstevel@tonic-gate 	va_end(args);
27400Sstevel@tonic-gate 	/*
27410Sstevel@tonic-gate 	 * Trim trailing '\0' form buffer
27420Sstevel@tonic-gate 	 */
27430Sstevel@tonic-gate 	prf.pr_cur--;
27440Sstevel@tonic-gate 	return (dowrite(&prf));
27450Sstevel@tonic-gate }
27460Sstevel@tonic-gate 
27478883SRod.Evans@Sun.COM static char	errbuf[ERRSIZE], *nextptr = errbuf, *prevptr = NULL;
27480Sstevel@tonic-gate 
27498598SRod.Evans@Sun.COM /*
27508598SRod.Evans@Sun.COM  * All error messages go through eprintf().  During process initialization,
27518598SRod.Evans@Sun.COM  * these messages are directed to the standard error, however once control has
27528598SRod.Evans@Sun.COM  * been passed to the applications code these messages are stored in an internal
27538598SRod.Evans@Sun.COM  * buffer for use with dlerror().  Note, fatal error conditions that may occur
27548598SRod.Evans@Sun.COM  * while running the application will still cause a standard error message, see
27558598SRod.Evans@Sun.COM  * rtldexit() in this file for details.
27568598SRod.Evans@Sun.COM  * The RT_FL_APPLIC flag serves to indicate the transition between process
27578598SRod.Evans@Sun.COM  * initialization and when the applications code is running.
27588598SRod.Evans@Sun.COM  */
27591618Srie /*PRINTFLIKE3*/
27600Sstevel@tonic-gate void
27611618Srie eprintf(Lm_list *lml, Error error, const char *format, ...)
27620Sstevel@tonic-gate {
27630Sstevel@tonic-gate 	va_list		args;
27640Sstevel@tonic-gate 	int		overflow = 0;
27650Sstevel@tonic-gate 	static int	lock = 0;
27660Sstevel@tonic-gate 	Prfbuf		prf;
27670Sstevel@tonic-gate 
27680Sstevel@tonic-gate 	if (lock || (nextptr == (errbuf + ERRSIZE)))
27690Sstevel@tonic-gate 		return;
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 	/*
27720Sstevel@tonic-gate 	 * Note: this lock is here to prevent the same thread from recursively
27730Sstevel@tonic-gate 	 * entering itself during a eprintf.  ie: during eprintf malloc() fails
27740Sstevel@tonic-gate 	 * and we try and call eprintf ... and then malloc() fails ....
27750Sstevel@tonic-gate 	 */
27760Sstevel@tonic-gate 	lock = 1;
27770Sstevel@tonic-gate 
27780Sstevel@tonic-gate 	/*
27790Sstevel@tonic-gate 	 * If we have completed startup initialization, all error messages
27800Sstevel@tonic-gate 	 * must be saved.  These are reported through dlerror().  If we're
27810Sstevel@tonic-gate 	 * still in the initialization stage, output the error directly and
27820Sstevel@tonic-gate 	 * add a newline.
27830Sstevel@tonic-gate 	 */
27840Sstevel@tonic-gate 	va_start(args, format);
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = nextptr;
27870Sstevel@tonic-gate 	prf.pr_len = ERRSIZE - (nextptr - errbuf);
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_APPLIC))
27900Sstevel@tonic-gate 		prf.pr_fd = 2;
27910Sstevel@tonic-gate 	else
27920Sstevel@tonic-gate 		prf.pr_fd = -1;
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	if (error > ERR_NONE) {
27950Sstevel@tonic-gate 		if ((error == ERR_FATAL) && (rtld_flags2 & RT_FL2_FTL2WARN))
27960Sstevel@tonic-gate 			error = ERR_WARNING;
27970Sstevel@tonic-gate 		if (error == ERR_WARNING) {
27988883SRod.Evans@Sun.COM 			if (err_strs[ERR_WARNING] == NULL)
27994362Srie 				err_strs[ERR_WARNING] =
28004362Srie 				    MSG_INTL(MSG_ERR_WARNING);
28010Sstevel@tonic-gate 		} else if (error == ERR_FATAL) {
28028883SRod.Evans@Sun.COM 			if (err_strs[ERR_FATAL] == NULL)
28034362Srie 				err_strs[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
28040Sstevel@tonic-gate 		} else if (error == ERR_ELF) {
28058883SRod.Evans@Sun.COM 			if (err_strs[ERR_ELF] == NULL)
28064362Srie 				err_strs[ERR_ELF] = MSG_INTL(MSG_ERR_ELF);
28070Sstevel@tonic-gate 		}
28086Srie 		if (procname) {
28096Srie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR1),
28106Srie 			    rtldname, procname, err_strs[error]) == 0)
28116Srie 				overflow = 1;
28120Sstevel@tonic-gate 		} else {
28136Srie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
28146Srie 			    rtldname, err_strs[error]) == 0)
28156Srie 				overflow = 1;
28166Srie 		}
28176Srie 		if (overflow == 0) {
28180Sstevel@tonic-gate 			/*
28190Sstevel@tonic-gate 			 * Remove the terminating '\0'.
28200Sstevel@tonic-gate 			 */
28210Sstevel@tonic-gate 			prf.pr_cur--;
28220Sstevel@tonic-gate 		}
28230Sstevel@tonic-gate 	}
28240Sstevel@tonic-gate 
28250Sstevel@tonic-gate 	if ((overflow == 0) && doprf(format, args, &prf) == 0)
28260Sstevel@tonic-gate 		overflow = 1;
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	/*
28290Sstevel@tonic-gate 	 * If this is an ELF error, it will have been generated by a support
28300Sstevel@tonic-gate 	 * object that has a dependency on libelf.  ld.so.1 doesn't generate any
28310Sstevel@tonic-gate 	 * ELF error messages as it doesn't interact with libelf.  Determine the
28320Sstevel@tonic-gate 	 * ELF error string.
28330Sstevel@tonic-gate 	 */
28340Sstevel@tonic-gate 	if ((overflow == 0) && (error == ERR_ELF)) {
28350Sstevel@tonic-gate 		static int		(*elfeno)() = 0;
28360Sstevel@tonic-gate 		static const char	*(*elfemg)();
28370Sstevel@tonic-gate 		const char		*emsg;
28380Sstevel@tonic-gate 		Rt_map			*dlmp, *lmp = lml_rtld.lm_head;
28390Sstevel@tonic-gate 
28400Sstevel@tonic-gate 		if (NEXT(lmp) && (elfeno == 0)) {
28410Sstevel@tonic-gate 			if (((elfemg = (const char *(*)())dlsym_intn(RTLD_NEXT,
28428883SRod.Evans@Sun.COM 			    MSG_ORIG(MSG_SYM_ELFERRMSG),
28438883SRod.Evans@Sun.COM 			    lmp, &dlmp)) == NULL) ||
28440Sstevel@tonic-gate 			    ((elfeno = (int (*)())dlsym_intn(RTLD_NEXT,
28458883SRod.Evans@Sun.COM 			    MSG_ORIG(MSG_SYM_ELFERRNO), lmp, &dlmp)) == NULL))
28460Sstevel@tonic-gate 				elfeno = 0;
28470Sstevel@tonic-gate 		}
28480Sstevel@tonic-gate 
28490Sstevel@tonic-gate 		/*
28500Sstevel@tonic-gate 		 * Lookup the message; equivalent to elf_errmsg(elf_errno()).
28510Sstevel@tonic-gate 		 */
28528883SRod.Evans@Sun.COM 		if (elfeno && ((emsg = (* elfemg)((* elfeno)())) != NULL)) {
28530Sstevel@tonic-gate 			prf.pr_cur--;
28540Sstevel@tonic-gate 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
28550Sstevel@tonic-gate 			    emsg) == 0)
28560Sstevel@tonic-gate 				overflow = 1;
28570Sstevel@tonic-gate 		}
28580Sstevel@tonic-gate 	}
28590Sstevel@tonic-gate 
28600Sstevel@tonic-gate 	/*
28610Sstevel@tonic-gate 	 * Push out any message that's been built.  Note, in the case of an
28620Sstevel@tonic-gate 	 * overflow condition, this message may be incomplete, in which case
28630Sstevel@tonic-gate 	 * make sure any partial string is null terminated.
28640Sstevel@tonic-gate 	 */
28650Sstevel@tonic-gate 	if ((rtld_flags & (RT_FL_APPLIC | RT_FL_SILENCERR)) == 0) {
28660Sstevel@tonic-gate 		*(prf.pr_cur - 1) = '\n';
28670Sstevel@tonic-gate 		(void) dowrite(&prf);
28680Sstevel@tonic-gate 	}
28698598SRod.Evans@Sun.COM 	if (overflow)
28708598SRod.Evans@Sun.COM 		*(prf.pr_cur - 1) = '\0';
28710Sstevel@tonic-gate 
28721618Srie 	DBG_CALL(Dbg_util_str(lml, nextptr));
28730Sstevel@tonic-gate 	va_end(args);
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 	/*
28760Sstevel@tonic-gate 	 * Determine if there was insufficient space left in the buffer to
28770Sstevel@tonic-gate 	 * complete the message.  If so, we'll have printed out as much as had
28780Sstevel@tonic-gate 	 * been processed if we're not yet executing the application.
28790Sstevel@tonic-gate 	 * Otherwise, there will be some debugging diagnostic indicating
28800Sstevel@tonic-gate 	 * as much of the error message as possible.  Write out a final buffer
28810Sstevel@tonic-gate 	 * overflow diagnostic - unlocalized, so we don't chance more errors.
28820Sstevel@tonic-gate 	 */
28830Sstevel@tonic-gate 	if (overflow) {
28840Sstevel@tonic-gate 		char	*str = (char *)MSG_INTL(MSG_EMG_BUFOVRFLW);
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) == 0) {
28870Sstevel@tonic-gate 			lasterr = str;
28880Sstevel@tonic-gate 
28890Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_APPLIC) == 0) {
28900Sstevel@tonic-gate 				(void) write(2, str, strlen(str));
28910Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
28920Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
28930Sstevel@tonic-gate 			}
28940Sstevel@tonic-gate 		}
28951618Srie 		DBG_CALL(Dbg_util_str(lml, str));
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 		lock = 0;
28980Sstevel@tonic-gate 		nextptr = errbuf + ERRSIZE;
28990Sstevel@tonic-gate 		return;
29000Sstevel@tonic-gate 	}
29010Sstevel@tonic-gate 
29020Sstevel@tonic-gate 	/*
29030Sstevel@tonic-gate 	 * If the application has started, then error messages are being saved
29040Sstevel@tonic-gate 	 * for retrieval by dlerror(), or possible flushing from rtldexit() in
29050Sstevel@tonic-gate 	 * the case of a fatal error.  In this case, establish the next error
29060Sstevel@tonic-gate 	 * pointer.  If we haven't started the application, the whole message
29070Sstevel@tonic-gate 	 * buffer can be reused.
29080Sstevel@tonic-gate 	 */
29090Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SILENCERR) == 0) {
29100Sstevel@tonic-gate 		lasterr = nextptr;
29110Sstevel@tonic-gate 
29120Sstevel@tonic-gate 		/*
29130Sstevel@tonic-gate 		 * Note, should we encounter an error such as ENOMEM, there may
29140Sstevel@tonic-gate 		 * be a number of the same error messages (ie. an operation
29150Sstevel@tonic-gate 		 * fails with ENOMEM, and then the attempts to construct the
29160Sstevel@tonic-gate 		 * error message itself, which incurs additional ENOMEM errors).
29170Sstevel@tonic-gate 		 * Compare any previous error message with the one we've just
29180Sstevel@tonic-gate 		 * created to prevent any duplication clutter.
29190Sstevel@tonic-gate 		 */
29200Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) &&
29218883SRod.Evans@Sun.COM 		    ((prevptr == NULL) || (strcmp(prevptr, nextptr) != 0))) {
29220Sstevel@tonic-gate 			prevptr = nextptr;
29230Sstevel@tonic-gate 			nextptr = prf.pr_cur;
29240Sstevel@tonic-gate 			*nextptr = '\0';
29250Sstevel@tonic-gate 		}
29260Sstevel@tonic-gate 	}
29270Sstevel@tonic-gate 	lock = 0;
29280Sstevel@tonic-gate }
29290Sstevel@tonic-gate 
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate #if	DEBUG
29320Sstevel@tonic-gate /*
29338598SRod.Evans@Sun.COM  * Provide assfail() for ASSERT() statements.  See <sys/debug.h> for further
29348598SRod.Evans@Sun.COM  * details.
29350Sstevel@tonic-gate  */
29360Sstevel@tonic-gate int
29370Sstevel@tonic-gate assfail(const char *a, const char *f, int l)
29380Sstevel@tonic-gate {
29390Sstevel@tonic-gate 	(void) printf("assertion failed: %s, file: %s, line: %d\n", a, f, l);
29400Sstevel@tonic-gate 	(void) _lwp_kill(_lwp_self(), SIGABRT);
29410Sstevel@tonic-gate 	return (0);
29420Sstevel@tonic-gate }
29430Sstevel@tonic-gate #endif
29440Sstevel@tonic-gate 
29450Sstevel@tonic-gate /*
29460Sstevel@tonic-gate  * Exit.  If we arrive here with a non zero status it's because of a fatal
29470Sstevel@tonic-gate  * error condition (most commonly a relocation error).  If the application has
29480Sstevel@tonic-gate  * already had control, then the actual fatal error message will have been
29490Sstevel@tonic-gate  * recorded in the dlerror() message buffer.  Print the message before really
29500Sstevel@tonic-gate  * exiting.
29510Sstevel@tonic-gate  */
29520Sstevel@tonic-gate void
29530Sstevel@tonic-gate rtldexit(Lm_list * lml, int status)
29540Sstevel@tonic-gate {
29550Sstevel@tonic-gate 	if (status) {
29560Sstevel@tonic-gate 		if (rtld_flags & RT_FL_APPLIC) {
29570Sstevel@tonic-gate 			/*
29580Sstevel@tonic-gate 			 * If the error buffer has been used, write out all
29590Sstevel@tonic-gate 			 * pending messages - lasterr is simply a pointer to
29600Sstevel@tonic-gate 			 * the last message in this buffer.  However, if the
29610Sstevel@tonic-gate 			 * buffer couldn't be created at all, lasterr points
29620Sstevel@tonic-gate 			 * to a constant error message string.
29630Sstevel@tonic-gate 			 */
29640Sstevel@tonic-gate 			if (*errbuf) {
29650Sstevel@tonic-gate 				char	*errptr = errbuf;
29660Sstevel@tonic-gate 				char	*errend = errbuf + ERRSIZE;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 				while ((errptr < errend) && *errptr) {
29690Sstevel@tonic-gate 					size_t	size = strlen(errptr);
29700Sstevel@tonic-gate 					(void) write(2, errptr, size);
29710Sstevel@tonic-gate 					(void) write(2, MSG_ORIG(MSG_STR_NL),
29720Sstevel@tonic-gate 					    MSG_STR_NL_SIZE);
29730Sstevel@tonic-gate 					errptr += (size + 1);
29740Sstevel@tonic-gate 				}
29750Sstevel@tonic-gate 			}
29760Sstevel@tonic-gate 			if (lasterr && ((lasterr < errbuf) ||
29770Sstevel@tonic-gate 			    (lasterr > (errbuf + ERRSIZE)))) {
29780Sstevel@tonic-gate 				(void) write(2, lasterr, strlen(lasterr));
29790Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
29800Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
29810Sstevel@tonic-gate 			}
29820Sstevel@tonic-gate 		}
29836515Sraf 		leave(lml, 0);
29840Sstevel@tonic-gate 		(void) _lwp_kill(_lwp_self(), killsig);
29850Sstevel@tonic-gate 	}
29860Sstevel@tonic-gate 	_exit(status);
29870Sstevel@tonic-gate }
29880Sstevel@tonic-gate 
29890Sstevel@tonic-gate /*
29908598SRod.Evans@Sun.COM  * Map anonymous memory via MAP_ANON (added in Solaris 8).
29910Sstevel@tonic-gate  */
29928598SRod.Evans@Sun.COM void *
29931618Srie dz_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
29940Sstevel@tonic-gate {
29950Sstevel@tonic-gate 	caddr_t	va;
29968598SRod.Evans@Sun.COM 
29978598SRod.Evans@Sun.COM 	if ((va = (caddr_t)mmap(addr, len, prot,
29988598SRod.Evans@Sun.COM 	    (flags | MAP_ANON), -1, 0)) == MAP_FAILED) {
29998598SRod.Evans@Sun.COM 		int	err = errno;
30008598SRod.Evans@Sun.COM 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON),
30018598SRod.Evans@Sun.COM 		    strerror(err));
30020Sstevel@tonic-gate 		return (MAP_FAILED);
30030Sstevel@tonic-gate 	}
30040Sstevel@tonic-gate 	return (va);
30050Sstevel@tonic-gate }
30060Sstevel@tonic-gate 
30070Sstevel@tonic-gate static int	nu_fd = FD_UNAVAIL;
30080Sstevel@tonic-gate 
30098598SRod.Evans@Sun.COM void *
30101618Srie nu_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
30110Sstevel@tonic-gate {
30120Sstevel@tonic-gate 	caddr_t	va;
30130Sstevel@tonic-gate 	int	err;
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	if (nu_fd == FD_UNAVAIL) {
30160Sstevel@tonic-gate 		if ((nu_fd = open(MSG_ORIG(MSG_PTH_DEVNULL),
30170Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
30180Sstevel@tonic-gate 			err = errno;
30191618Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
30200Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
30210Sstevel@tonic-gate 			return (MAP_FAILED);
30220Sstevel@tonic-gate 		}
30230Sstevel@tonic-gate 	}
30240Sstevel@tonic-gate 
30250Sstevel@tonic-gate 	if ((va = (caddr_t)mmap(addr, len, prot, flags, nu_fd, 0)) ==
30260Sstevel@tonic-gate 	    MAP_FAILED) {
30270Sstevel@tonic-gate 		err = errno;
30281618Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
30290Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
30300Sstevel@tonic-gate 	}
30310Sstevel@tonic-gate 	return (va);
30320Sstevel@tonic-gate }
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate /*
30355950Srie  * Generic entry point from user code - simply grabs a lock, and bumps the
30365950Srie  * entrance count.
30370Sstevel@tonic-gate  */
30380Sstevel@tonic-gate int
30396515Sraf enter(int flags)
30400Sstevel@tonic-gate {
30416515Sraf 	if (rt_bind_guard(THR_FLG_RTLD | thr_flg_nolock | flags)) {
30426515Sraf 		if (!thr_flg_nolock)
30436515Sraf 			(void) rt_mutex_lock(&rtldlock);
3044*9577SRod.Evans@Sun.COM 		if (rtld_flags & RT_FL_OPERATION) {
30456387Srie 			ld_entry_cnt++;
3046*9577SRod.Evans@Sun.COM 
3047*9577SRod.Evans@Sun.COM 			/*
3048*9577SRod.Evans@Sun.COM 			 * Reset the diagnostic time information for each new
3049*9577SRod.Evans@Sun.COM 			 * "operation".  Thus timing diagnostics are relative
3050*9577SRod.Evans@Sun.COM 			 * to entering ld.so.1.
3051*9577SRod.Evans@Sun.COM 			 */
3052*9577SRod.Evans@Sun.COM 			if (DBG_ISTIME() &&
3053*9577SRod.Evans@Sun.COM 			    (gettimeofday(&DBG_TOTALTIME, NULL) == 0)) {
3054*9577SRod.Evans@Sun.COM 				DBG_DELTATIME = DBG_TOTALTIME;
3055*9577SRod.Evans@Sun.COM 				DBG_ONRESET();
3056*9577SRod.Evans@Sun.COM 			}
3057*9577SRod.Evans@Sun.COM 		}
30580Sstevel@tonic-gate 		return (1);
30590Sstevel@tonic-gate 	}
30600Sstevel@tonic-gate 	return (0);
30610Sstevel@tonic-gate }
30620Sstevel@tonic-gate 
30630Sstevel@tonic-gate /*
30646387Srie  * Determine whether a search path has been used.
30656387Srie  */
30666387Srie static void
30678598SRod.Evans@Sun.COM is_path_used(Lm_list *lml, Word unref, int *nl, Alist *alp, const char *obj)
30686387Srie {
30698598SRod.Evans@Sun.COM 	Pdesc	*pdp;
30708598SRod.Evans@Sun.COM 	Aliste	idx;
30718598SRod.Evans@Sun.COM 
30728598SRod.Evans@Sun.COM 	for (ALIST_TRAVERSE(alp, idx, pdp)) {
30736387Srie 		const char	*fmt, *name;
30746387Srie 
30758598SRod.Evans@Sun.COM 		if ((pdp->pd_plen == 0) || (pdp->pd_flags & PD_FLG_USED))
30766387Srie 			continue;
30776387Srie 
30786387Srie 		/*
30796387Srie 		 * If this pathname originated from an expanded token, use the
30806387Srie 		 * original for any diagnostic output.
30816387Srie 		 */
30828598SRod.Evans@Sun.COM 		if ((name = pdp->pd_oname) == NULL)
30838598SRod.Evans@Sun.COM 			name = pdp->pd_pname;
30846387Srie 
30856387Srie 		if (unref == 0) {
30866387Srie 			if ((*nl)++ == 0)
30876387Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
30888598SRod.Evans@Sun.COM 			DBG_CALL(Dbg_unused_path(lml, name, pdp->pd_flags,
30898598SRod.Evans@Sun.COM 			    (pdp->pd_flags & PD_FLG_DUPLICAT), obj));
30906387Srie 			continue;
30916387Srie 		}
30926387Srie 
30938598SRod.Evans@Sun.COM 		if (pdp->pd_flags & LA_SER_LIBPATH) {
30948598SRod.Evans@Sun.COM 			if (pdp->pd_flags & LA_SER_CONFIG) {
30958598SRod.Evans@Sun.COM 				if (pdp->pd_flags & PD_FLG_DUPLICAT)
30966387Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATHC);
30976387Srie 				else
30986387Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATHC);
30996387Srie 			} else {
31008598SRod.Evans@Sun.COM 				if (pdp->pd_flags & PD_FLG_DUPLICAT)
31016387Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATH);
31026387Srie 				else
31036387Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATH);
31046387Srie 			}
31058598SRod.Evans@Sun.COM 		} else if (pdp->pd_flags & LA_SER_RUNPATH) {
31066387Srie 			fmt = MSG_INTL(MSG_USD_RUNPATH);
31076387Srie 		} else
31086387Srie 			continue;
31096387Srie 
31106387Srie 		if ((*nl)++ == 0)
31116387Srie 			(void) printf(MSG_ORIG(MSG_STR_NL));
31126387Srie 		(void) printf(fmt, name, obj);
31136387Srie 	}
31146387Srie }
31156387Srie 
31166387Srie /*
31170Sstevel@tonic-gate  * Generate diagnostics as to whether an object has been used.  A symbolic
31180Sstevel@tonic-gate  * reference that gets bound to an object marks it as used.  Dependencies that
31190Sstevel@tonic-gate  * are unused when RTLD_NOW is in effect should be removed from future builds
31200Sstevel@tonic-gate  * of an object.  Dependencies that are unused without RTLD_NOW in effect are
31210Sstevel@tonic-gate  * candidates for lazy-loading.
31226387Srie  *
31230Sstevel@tonic-gate  * Unreferenced objects identify objects that are defined as dependencies but
31246387Srie  * are unreferenced by the caller.  These unreferenced objects may however be
31256387Srie  * referenced by other objects within the process, and therefore don't qualify
31266387Srie  * as completely unused.  They are still an unnecessary overhead.
31276387Srie  *
31286387Srie  * Unreferenced runpaths are also captured under ldd -U, or "unused,detail"
31296387Srie  * debugging.
31300Sstevel@tonic-gate  */
31310Sstevel@tonic-gate void
31320Sstevel@tonic-gate unused(Lm_list *lml)
31330Sstevel@tonic-gate {
31340Sstevel@tonic-gate 	Rt_map		*lmp;
31350Sstevel@tonic-gate 	int		nl = 0;
31366387Srie 	Word		unref, unuse;
31370Sstevel@tonic-gate 
31380Sstevel@tonic-gate 	/*
31390Sstevel@tonic-gate 	 * If we're not tracing unused references or dependencies, or debugging
31400Sstevel@tonic-gate 	 * there's nothing to do.
31410Sstevel@tonic-gate 	 */
31426387Srie 	unref = lml->lm_flags & LML_FLG_TRC_UNREF;
31436387Srie 	unuse = lml->lm_flags & LML_FLG_TRC_UNUSED;
31446387Srie 
31456387Srie 	if ((unref == 0) && (unuse == 0) && (DBG_ENABLED == 0))
31460Sstevel@tonic-gate 		return;
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 	/*
31496387Srie 	 * Detect unused global search paths.
31506387Srie 	 */
31516387Srie 	if (rpl_libdirs)
31526387Srie 		is_path_used(lml, unref, &nl, rpl_libdirs, config->c_name);
31536387Srie 	if (prm_libdirs)
31546387Srie 		is_path_used(lml, unref, &nl, prm_libdirs, config->c_name);
31556387Srie 
31566387Srie 	nl = 0;
31576387Srie 	lmp = lml->lm_head;
31586387Srie 	if (RLIST(lmp))
31596387Srie 		is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
31606387Srie 
31616387Srie 	/*
31620Sstevel@tonic-gate 	 * Traverse the link-maps looking for unreferenced or unused
31630Sstevel@tonic-gate 	 * dependencies.  Ignore the first object on a link-map list, as this
31646387Srie 	 * is always used.
31650Sstevel@tonic-gate 	 */
31666387Srie 	nl = 0;
31678394SAli.Bahrami@Sun.COM 	for (lmp = NEXT_RT_MAP(lmp); lmp; lmp = NEXT_RT_MAP(lmp)) {
31686387Srie 		/*
31696387Srie 		 * Determine if this object contains any runpaths that have
31706387Srie 		 * not been used.
31716387Srie 		 */
31726387Srie 		if (RLIST(lmp))
31736387Srie 			is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
31746387Srie 
31750Sstevel@tonic-gate 		/*
31760Sstevel@tonic-gate 		 * If tracing unreferenced objects, or under debugging,
31770Sstevel@tonic-gate 		 * determine whether any of this objects callers haven't
31780Sstevel@tonic-gate 		 * referenced it.
31790Sstevel@tonic-gate 		 */
31806387Srie 		if (unref || DBG_ENABLED) {
31815892Sab196087 			Bnd_desc	*bdp;
31825892Sab196087 			Aliste		idx;
31835892Sab196087 
31845892Sab196087 			for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
31856387Srie 				Rt_map	*clmp;
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 				if (bdp->b_flags & BND_REFER)
31880Sstevel@tonic-gate 					continue;
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 				clmp = bdp->b_caller;
31910Sstevel@tonic-gate 				if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
31920Sstevel@tonic-gate 					continue;
31930Sstevel@tonic-gate 
31944362Srie 				/* BEGIN CSTYLED */
31950Sstevel@tonic-gate 				if (nl++ == 0) {
31966387Srie 					if (unref)
31970Sstevel@tonic-gate 					    (void) printf(MSG_ORIG(MSG_STR_NL));
31980Sstevel@tonic-gate 					else
31991618Srie 					    DBG_CALL(Dbg_util_nl(lml,
32001618Srie 						DBG_NL_STD));
32010Sstevel@tonic-gate 				}
32020Sstevel@tonic-gate 
32036387Srie 				if (unref)
32040Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_UNREF_FMT),
32050Sstevel@tonic-gate 					NAME(lmp), NAME(clmp));
32060Sstevel@tonic-gate 				else
32071618Srie 				    DBG_CALL(Dbg_unused_unref(lmp, NAME(clmp)));
32084362Srie 				/* END CSTYLED */
32090Sstevel@tonic-gate 			}
32100Sstevel@tonic-gate 		}
32110Sstevel@tonic-gate 
32120Sstevel@tonic-gate 		/*
32130Sstevel@tonic-gate 		 * If tracing unused objects simply display those objects that
32140Sstevel@tonic-gate 		 * haven't been referenced by anyone.
32150Sstevel@tonic-gate 		 */
32160Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_USED)
32170Sstevel@tonic-gate 			continue;
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 		if (nl++ == 0) {
32206387Srie 			if (unref || unuse)
32210Sstevel@tonic-gate 				(void) printf(MSG_ORIG(MSG_STR_NL));
32220Sstevel@tonic-gate 			else
32231618Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
32240Sstevel@tonic-gate 		}
32250Sstevel@tonic-gate 		if (CYCGROUP(lmp)) {
32266387Srie 			if (unref || unuse)
32270Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNCYC_FMT),
32280Sstevel@tonic-gate 				    NAME(lmp), CYCGROUP(lmp));
32290Sstevel@tonic-gate 			else
32301618Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0,
32310Sstevel@tonic-gate 				    CYCGROUP(lmp)));
32320Sstevel@tonic-gate 		} else {
32336387Srie 			if (unref || unuse)
32340Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNUSED_FMT),
32350Sstevel@tonic-gate 				    NAME(lmp));
32360Sstevel@tonic-gate 			else
32371618Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0, 0));
32380Sstevel@tonic-gate 		}
32390Sstevel@tonic-gate 	}
32400Sstevel@tonic-gate 
32411618Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
32420Sstevel@tonic-gate }
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate /*
32450Sstevel@tonic-gate  * Generic cleanup routine called prior to returning control to the user.
32460Sstevel@tonic-gate  * Insures that any ld.so.1 specific file descriptors or temporary mapping are
32470Sstevel@tonic-gate  * released, and any locks dropped.
32480Sstevel@tonic-gate  */
32490Sstevel@tonic-gate void
32506515Sraf leave(Lm_list *lml, int flags)
32510Sstevel@tonic-gate {
32528598SRod.Evans@Sun.COM 	Lm_list		*elml = lml;
32538598SRod.Evans@Sun.COM 	Rt_map		*clmp;
32548598SRod.Evans@Sun.COM 	Aliste		idx;
32552454Srie 
32560Sstevel@tonic-gate 	/*
32572454Srie 	 * Alert the debuggers that the link-maps are consistent.  Note, in the
32582454Srie 	 * case of tearing down a whole link-map list, lml will be null.  In
32592454Srie 	 * this case use the main link-map list to test for a notification.
32600Sstevel@tonic-gate 	 */
32617668SRod.Evans@Sun.COM 	if (elml == NULL)
32622454Srie 		elml = &lml_main;
32632454Srie 	if (elml->lm_flags & LML_FLG_DBNOTIF)
32642454Srie 		rd_event(elml, RD_DLACTIVITY, RT_CONSISTENT);
32650Sstevel@tonic-gate 
32664679Srie 	/*
32674679Srie 	 * Alert any auditors that the link-maps are consistent.
32684679Srie 	 */
32695892Sab196087 	for (APLIST_TRAVERSE(elml->lm_actaudit, idx, clmp)) {
32705892Sab196087 		audit_activity(clmp, LA_ACT_CONSISTENT);
32715892Sab196087 
32725892Sab196087 		aplist_delete(elml->lm_actaudit, &idx);
32734679Srie 	}
32744679Srie 
32750Sstevel@tonic-gate 	if (nu_fd != FD_UNAVAIL) {
32760Sstevel@tonic-gate 		(void) close(nu_fd);
32770Sstevel@tonic-gate 		nu_fd = FD_UNAVAIL;
32780Sstevel@tonic-gate 	}
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 	/*
32810Sstevel@tonic-gate 	 * Reinitialize error message pointer, and any overflow indication.
32820Sstevel@tonic-gate 	 */
32830Sstevel@tonic-gate 	nextptr = errbuf;
32848883SRod.Evans@Sun.COM 	prevptr = NULL;
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 	/*
32878598SRod.Evans@Sun.COM 	 * Defragment any freed memory.
32888598SRod.Evans@Sun.COM 	 */
32898598SRod.Evans@Sun.COM 	if (aplist_nitems(free_alp))
32908598SRod.Evans@Sun.COM 		defrag();
32918598SRod.Evans@Sun.COM 
32928598SRod.Evans@Sun.COM 	/*
32930Sstevel@tonic-gate 	 * Don't drop our lock if we are running on our link-map list as
32940Sstevel@tonic-gate 	 * there's little point in doing so since we are single-threaded.
32950Sstevel@tonic-gate 	 *
32960Sstevel@tonic-gate 	 * LML_FLG_HOLDLOCK is set for:
32978883SRod.Evans@Sun.COM 	 *  -	 The ld.so.1's link-map list.
32988883SRod.Evans@Sun.COM 	 *  -	 The auditor's link-map if the environment is pre-UPM.
32990Sstevel@tonic-gate 	 */
33000Sstevel@tonic-gate 	if (lml && (lml->lm_flags & LML_FLG_HOLDLOCK))
33010Sstevel@tonic-gate 		return;
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 	if (rt_bind_clear(0) & THR_FLG_RTLD) {
33046515Sraf 		if (!thr_flg_nolock)
33056515Sraf 			(void) rt_mutex_unlock(&rtldlock);
33066515Sraf 		(void) rt_bind_clear(THR_FLG_RTLD | thr_flg_nolock | flags);
33070Sstevel@tonic-gate 	}
33080Sstevel@tonic-gate }
33090Sstevel@tonic-gate 
33100Sstevel@tonic-gate int
33115220Srie callable(Rt_map *clmp, Rt_map *dlmp, Grp_hdl *ghp, uint_t slflags)
33120Sstevel@tonic-gate {
33135892Sab196087 	APlist		*calp, *dalp;
33145892Sab196087 	Aliste		idx1, idx2;
33155892Sab196087 	Grp_hdl		*ghp1, *ghp2;
33160Sstevel@tonic-gate 
33170Sstevel@tonic-gate 	/*
33180Sstevel@tonic-gate 	 * An object can always find symbols within itself.
33190Sstevel@tonic-gate 	 */
33200Sstevel@tonic-gate 	if (clmp == dlmp)
33210Sstevel@tonic-gate 		return (1);
33220Sstevel@tonic-gate 
33230Sstevel@tonic-gate 	/*
33245220Srie 	 * The search for a singleton must look in every loaded object.
33255220Srie 	 */
33265220Srie 	if (slflags & LKUP_SINGLETON)
33275220Srie 		return (1);
33285220Srie 
33295220Srie 	/*
33300Sstevel@tonic-gate 	 * Don't allow an object to bind to an object that is being deleted
33310Sstevel@tonic-gate 	 * unless the binder is also being deleted.
33320Sstevel@tonic-gate 	 */
33330Sstevel@tonic-gate 	if ((FLAGS(dlmp) & FLG_RT_DELETE) &&
33340Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
33350Sstevel@tonic-gate 		return (0);
33360Sstevel@tonic-gate 
33370Sstevel@tonic-gate 	/*
33380Sstevel@tonic-gate 	 * An object with world access can always bind to an object with global
33390Sstevel@tonic-gate 	 * visibility.
33400Sstevel@tonic-gate 	 */
33418388SRod.Evans@Sun.COM 	if (((MODE(clmp) & RTLD_WORLD) || (slflags & LKUP_WORLD)) &&
33428388SRod.Evans@Sun.COM 	    (MODE(dlmp) & RTLD_GLOBAL))
33430Sstevel@tonic-gate 		return (1);
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 	/*
33460Sstevel@tonic-gate 	 * An object with local access can only bind to an object that is a
33470Sstevel@tonic-gate 	 * member of the same group.
33480Sstevel@tonic-gate 	 */
33490Sstevel@tonic-gate 	if (((MODE(clmp) & RTLD_GROUP) == 0) ||
33505892Sab196087 	    ((calp = GROUPS(clmp)) == NULL) || ((dalp = GROUPS(dlmp)) == NULL))
33510Sstevel@tonic-gate 		return (0);
33520Sstevel@tonic-gate 
33530Sstevel@tonic-gate 	/*
33540Sstevel@tonic-gate 	 * Traverse the list of groups the caller is a part of.
33550Sstevel@tonic-gate 	 */
33565892Sab196087 	for (APLIST_TRAVERSE(calp, idx1, ghp1)) {
33570Sstevel@tonic-gate 		/*
33580Sstevel@tonic-gate 		 * If we're testing for the ability of two objects to bind to
33590Sstevel@tonic-gate 		 * each other regardless of a specific group, ignore that group.
33600Sstevel@tonic-gate 		 */
33615892Sab196087 		if (ghp && (ghp1 == ghp))
33620Sstevel@tonic-gate 			continue;
33630Sstevel@tonic-gate 
33640Sstevel@tonic-gate 		/*
33650Sstevel@tonic-gate 		 * Traverse the list of groups the destination is a part of.
33660Sstevel@tonic-gate 		 */
33675892Sab196087 		for (APLIST_TRAVERSE(dalp, idx2, ghp2)) {
33684699Srie 			Grp_desc	*gdp;
33695892Sab196087 			Aliste		idx3;
33705892Sab196087 
33715892Sab196087 			if (ghp1 != ghp2)
33724699Srie 				continue;
33734699Srie 
33744699Srie 			/*
33754699Srie 			 * Make sure the relationship between the destination
33764699Srie 			 * and the caller provide symbols for relocation.
33774699Srie 			 * Parents are maintained as callers, but unless the
33784699Srie 			 * destination object was opened with RTLD_PARENT, the
33794699Srie 			 * parent doesn't provide symbols for the destination
33804699Srie 			 * to relocate against.
33814699Srie 			 */
33825892Sab196087 			for (ALIST_TRAVERSE(ghp2->gh_depends, idx3, gdp)) {
33834699Srie 				if (dlmp != gdp->gd_depend)
33844699Srie 					continue;
33854699Srie 
33864699Srie 				if (gdp->gd_flags & GPD_RELOC)
33874699Srie 					return (1);
33884699Srie 			}
33890Sstevel@tonic-gate 		}
33900Sstevel@tonic-gate 	}
33910Sstevel@tonic-gate 	return (0);
33920Sstevel@tonic-gate }
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate /*
33950Sstevel@tonic-gate  * Initialize the environ symbol.  Traditionally this is carried out by the crt
33960Sstevel@tonic-gate  * code prior to jumping to main.  However, init sections get fired before this
33970Sstevel@tonic-gate  * variable is initialized, so ld.so.1 sets this directly from the AUX vector
33980Sstevel@tonic-gate  * information.  In addition, a process may have multiple link-maps (ld.so.1's
33990Sstevel@tonic-gate  * debugging and preloading objects), and link auditing, and each may need an
34000Sstevel@tonic-gate  * environ variable set.
34010Sstevel@tonic-gate  *
34020Sstevel@tonic-gate  * This routine is called after a relocation() pass, and thus provides for:
34030Sstevel@tonic-gate  *
34048883SRod.Evans@Sun.COM  *  -	setting environ on the main link-map after the initial application and
34050Sstevel@tonic-gate  *	its dependencies have been established.  Typically environ lives in the
34060Sstevel@tonic-gate  *	application (provided by its crt), but in older applications it might
34070Sstevel@tonic-gate  *	be in libc.  Who knows what's expected of applications not built on
34080Sstevel@tonic-gate  *	Solaris.
34090Sstevel@tonic-gate  *
34108883SRod.Evans@Sun.COM  *  -	after loading a new shared object.  We can add shared objects to various
34110Sstevel@tonic-gate  *	link-maps, and any link-map dependencies requiring getopt() require
34120Sstevel@tonic-gate  *	their own environ.  In addition, lazy loading might bring in the
34136Srie  *	supplier of environ (libc used to be a lazy loading candidate) after
34146Srie  *	the link-map has been established and other objects are present.
34150Sstevel@tonic-gate  *
34160Sstevel@tonic-gate  * This routine handles all these scenarios, without adding unnecessary overhead
34170Sstevel@tonic-gate  * to ld.so.1.
34180Sstevel@tonic-gate  */
34190Sstevel@tonic-gate void
34200Sstevel@tonic-gate set_environ(Lm_list *lml)
34210Sstevel@tonic-gate {
34225950Srie 	Rt_map		*dlmp;
34235950Srie 	Sym		*sym;
34240Sstevel@tonic-gate 	Slookup		sl;
34250Sstevel@tonic-gate 	uint_t		binfo;
34260Sstevel@tonic-gate 
34275950Srie 	/*
34285950Srie 	 * Initialize the symbol lookup data structure.
34295950Srie 	 */
34305950Srie 	SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_ENVIRON), lml->lm_head, lml->lm_head,
34315950Srie 	    ld_entry_cnt, 0, 0, 0, 0, LKUP_WEAK);
34320Sstevel@tonic-gate 
34336387Srie 	if (sym = LM_LOOKUP_SYM(lml->lm_head)(&sl, &dlmp, &binfo, 0)) {
34346Srie 		lml->lm_environ = (char ***)sym->st_value;
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 		if (!(FLAGS(dlmp) & FLG_RT_FIXED))
34376Srie 			lml->lm_environ =
34386Srie 			    (char ***)((uintptr_t)lml->lm_environ +
34396Srie 			    (uintptr_t)ADDR(dlmp));
34406Srie 		*(lml->lm_environ) = (char **)environ;
34410Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_ENVIRON;
34420Sstevel@tonic-gate 	}
34430Sstevel@tonic-gate }
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate /*
34460Sstevel@tonic-gate  * Determine whether we have a secure executable.  Uid and gid information
34470Sstevel@tonic-gate  * can be passed to us via the aux vector, however if these values are -1
34480Sstevel@tonic-gate  * then use the appropriate system call to obtain them.
34490Sstevel@tonic-gate  *
34508883SRod.Evans@Sun.COM  *  -	If the user is the root they can do anything
34510Sstevel@tonic-gate  *
34528883SRod.Evans@Sun.COM  *  -	If the real and effective uid's don't match, or the real and
34530Sstevel@tonic-gate  *	effective gid's don't match then this is determined to be a `secure'
34540Sstevel@tonic-gate  *	application.
34550Sstevel@tonic-gate  *
34560Sstevel@tonic-gate  * This function is called prior to any dependency processing (see _setup.c).
34570Sstevel@tonic-gate  * Any secure setting will remain in effect for the life of the process.
34580Sstevel@tonic-gate  */
34590Sstevel@tonic-gate void
34600Sstevel@tonic-gate security(uid_t uid, uid_t euid, gid_t gid, gid_t egid, int auxflags)
34610Sstevel@tonic-gate {
34620Sstevel@tonic-gate 	if (auxflags != -1) {
34630Sstevel@tonic-gate 		if ((auxflags & AF_SUN_SETUGID) != 0)
34640Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
34650Sstevel@tonic-gate 		return;
34660Sstevel@tonic-gate 	}
34678598SRod.Evans@Sun.COM 
34684362Srie 	if (uid == (uid_t)-1)
34690Sstevel@tonic-gate 		uid = getuid();
34700Sstevel@tonic-gate 	if (uid) {
34714362Srie 		if (euid == (uid_t)-1)
34720Sstevel@tonic-gate 			euid = geteuid();
34730Sstevel@tonic-gate 		if (uid != euid)
34740Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
34750Sstevel@tonic-gate 		else {
34764362Srie 			if (gid == (gid_t)-1)
34770Sstevel@tonic-gate 				gid = getgid();
34784362Srie 			if (egid == (gid_t)-1)
34790Sstevel@tonic-gate 				egid = getegid();
34800Sstevel@tonic-gate 			if (gid != egid)
34810Sstevel@tonic-gate 				rtld_flags |= RT_FL_SECURE;
34820Sstevel@tonic-gate 		}
34830Sstevel@tonic-gate 	}
34840Sstevel@tonic-gate }
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate /*
34877668SRod.Evans@Sun.COM  * Determine whether ld.so.1 itself is owned by root and has its mode setuid.
34887668SRod.Evans@Sun.COM  */
34897668SRod.Evans@Sun.COM int
34907668SRod.Evans@Sun.COM is_rtld_setuid()
34917668SRod.Evans@Sun.COM {
34928394SAli.Bahrami@Sun.COM 	rtld_stat_t	status;
34937668SRod.Evans@Sun.COM 
34947668SRod.Evans@Sun.COM 	if ((rtld_flags2 & RT_FL2_SETUID) ||
34958394SAli.Bahrami@Sun.COM 	    ((rtld_stat(NAME(lml_rtld.lm_head), &status) == 0) &&
34967668SRod.Evans@Sun.COM 	    (status.st_uid == 0) && (status.st_mode & S_ISUID))) {
34977668SRod.Evans@Sun.COM 		rtld_flags2 |= RT_FL2_SETUID;
34987668SRod.Evans@Sun.COM 		return (1);
34997668SRod.Evans@Sun.COM 	}
35007668SRod.Evans@Sun.COM 	return (0);
35017668SRod.Evans@Sun.COM }
35027668SRod.Evans@Sun.COM 
35037668SRod.Evans@Sun.COM /*
35040Sstevel@tonic-gate  * _REENTRANT code gets errno redefined to a function so provide for return
35050Sstevel@tonic-gate  * of the thread errno if applicable.  This has no meaning in ld.so.1 which
35060Sstevel@tonic-gate  * is basically singled threaded.  Provide the interface for our dependencies.
35070Sstevel@tonic-gate  */
35080Sstevel@tonic-gate #undef errno
35090Sstevel@tonic-gate int *
35100Sstevel@tonic-gate ___errno()
35110Sstevel@tonic-gate {
35120Sstevel@tonic-gate 	extern	int	errno;
35130Sstevel@tonic-gate 
35140Sstevel@tonic-gate 	return (&errno);
35150Sstevel@tonic-gate }
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate /*
35180Sstevel@tonic-gate  * Determine whether a symbol name should be demangled.
35190Sstevel@tonic-gate  */
35200Sstevel@tonic-gate const char *
35210Sstevel@tonic-gate demangle(const char *name)
35220Sstevel@tonic-gate {
35230Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DEMANGLE)
35241618Srie 		return (conv_demangle_name(name));
35250Sstevel@tonic-gate 	else
35260Sstevel@tonic-gate 		return (name);
35270Sstevel@tonic-gate }
35288394SAli.Bahrami@Sun.COM 
35298394SAli.Bahrami@Sun.COM #ifndef _LP64
35308394SAli.Bahrami@Sun.COM /*
35318394SAli.Bahrami@Sun.COM  * Wrappers on stat() and fstat() for 32-bit rtld that uses stat64()
35328394SAli.Bahrami@Sun.COM  * underneath while preserving the object size limits of a non-largefile
35338394SAli.Bahrami@Sun.COM  * enabled 32-bit process. The purpose of this is to prevent large inode
35348394SAli.Bahrami@Sun.COM  * values from causing stat() to fail.
35358394SAli.Bahrami@Sun.COM  */
35368394SAli.Bahrami@Sun.COM inline static int
35378394SAli.Bahrami@Sun.COM rtld_stat_process(int r, struct stat64 *lbuf, rtld_stat_t *restrict buf)
35388394SAli.Bahrami@Sun.COM {
35398394SAli.Bahrami@Sun.COM 	extern int	errno;
35408394SAli.Bahrami@Sun.COM 
35418394SAli.Bahrami@Sun.COM 	/*
35428394SAli.Bahrami@Sun.COM 	 * Although we used a 64-bit capable stat(), the 32-bit rtld
35438394SAli.Bahrami@Sun.COM 	 * can only handle objects < 2GB in size. If this object is
35448394SAli.Bahrami@Sun.COM 	 * too big, turn the success into an overflow error.
35458394SAli.Bahrami@Sun.COM 	 */
35468394SAli.Bahrami@Sun.COM 	if ((lbuf->st_size & 0xffffffff80000000) != 0) {
35478394SAli.Bahrami@Sun.COM 		errno = EOVERFLOW;
35488394SAli.Bahrami@Sun.COM 		return (-1);
35498394SAli.Bahrami@Sun.COM 	}
35508394SAli.Bahrami@Sun.COM 
35518394SAli.Bahrami@Sun.COM 	/*
35528394SAli.Bahrami@Sun.COM 	 * Transfer the information needed by rtld into a rtld_stat_t
35538394SAli.Bahrami@Sun.COM 	 * structure that preserves the non-largile types for everything
35548394SAli.Bahrami@Sun.COM 	 * except inode.
35558394SAli.Bahrami@Sun.COM 	 */
35568394SAli.Bahrami@Sun.COM 	buf->st_dev = lbuf->st_dev;
35578394SAli.Bahrami@Sun.COM 	buf->st_ino = lbuf->st_ino;
35588394SAli.Bahrami@Sun.COM 	buf->st_mode = lbuf->st_mode;
35598394SAli.Bahrami@Sun.COM 	buf->st_uid = lbuf->st_uid;
35608394SAli.Bahrami@Sun.COM 	buf->st_size = (off_t)lbuf->st_size;
35618394SAli.Bahrami@Sun.COM 	buf->st_mtim = lbuf->st_mtim;
35628394SAli.Bahrami@Sun.COM #ifdef sparc
35638394SAli.Bahrami@Sun.COM 	buf->st_blksize = lbuf->st_blksize;
35648394SAli.Bahrami@Sun.COM #endif
35658394SAli.Bahrami@Sun.COM 
35668394SAli.Bahrami@Sun.COM 	return (r);
35678394SAli.Bahrami@Sun.COM }
35688394SAli.Bahrami@Sun.COM 
35698394SAli.Bahrami@Sun.COM int
35708394SAli.Bahrami@Sun.COM rtld_stat(const char *restrict path, rtld_stat_t *restrict buf)
35718394SAli.Bahrami@Sun.COM {
35728394SAli.Bahrami@Sun.COM 	struct stat64	lbuf;
35738394SAli.Bahrami@Sun.COM 	int		r;
35748394SAli.Bahrami@Sun.COM 
35758394SAli.Bahrami@Sun.COM 	r = stat64(path, &lbuf);
35768394SAli.Bahrami@Sun.COM 	if (r != -1)
35778394SAli.Bahrami@Sun.COM 		r = rtld_stat_process(r, &lbuf, buf);
35788394SAli.Bahrami@Sun.COM 	return (r);
35798394SAli.Bahrami@Sun.COM }
35808394SAli.Bahrami@Sun.COM 
35818394SAli.Bahrami@Sun.COM int
35828394SAli.Bahrami@Sun.COM rtld_fstat(int fildes, rtld_stat_t *restrict buf)
35838394SAli.Bahrami@Sun.COM {
35848394SAli.Bahrami@Sun.COM 	struct stat64	lbuf;
35858394SAli.Bahrami@Sun.COM 	int		r;
35868394SAli.Bahrami@Sun.COM 
35878394SAli.Bahrami@Sun.COM 	r = fstat64(fildes, &lbuf);
35888394SAli.Bahrami@Sun.COM 	if (r != -1)
35898394SAli.Bahrami@Sun.COM 		r = rtld_stat_process(r, &lbuf, buf);
35908394SAli.Bahrami@Sun.COM 	return (r);
35918394SAli.Bahrami@Sun.COM }
35928394SAli.Bahrami@Sun.COM #endif
3593