xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/util.c (revision f441771b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55aefb655Srie  * Common Development and Distribution License (the "License").
65aefb655Srie  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21fb1354edSrie 
227257d1b4Sraf /*
237257d1b4Sraf  *	Copyright (c) 1988 AT&T
247257d1b4Sraf  *	  All Rights Reserved
25*f441771bSRod Evans  *
26*f441771bSRod Evans  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
277257d1b4Sraf  */
287257d1b4Sraf 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Utility routines for run-time linker.  some are duplicated here from libc
317c478bd9Sstevel@tonic-gate  * (with different names) to avoid name space collisions.
327c478bd9Sstevel@tonic-gate  */
3308278a5eSRod Evans #include	<sys/systeminfo.h>
347c478bd9Sstevel@tonic-gate #include	<stdio.h>
352017c965SRod Evans #include	<sys/time.h>
367c478bd9Sstevel@tonic-gate #include	<sys/types.h>
377c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
387c478bd9Sstevel@tonic-gate #include	<sys/lwp.h>
397c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
407c478bd9Sstevel@tonic-gate #include	<stdarg.h>
417c478bd9Sstevel@tonic-gate #include	<fcntl.h>
427c478bd9Sstevel@tonic-gate #include	<string.h>
437c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
447c478bd9Sstevel@tonic-gate #include	<unistd.h>
457c478bd9Sstevel@tonic-gate #include	<stdlib.h>
467c478bd9Sstevel@tonic-gate #include	<sys/auxv.h>
4756deab07SRod Evans #include	<limits.h>
485aefb655Srie #include	<debug.h>
495aefb655Srie #include	<conv.h>
507c478bd9Sstevel@tonic-gate #include	"_rtld.h"
517c478bd9Sstevel@tonic-gate #include	"_audit.h"
5210a4fa49Srie #include	"_elf.h"
537c478bd9Sstevel@tonic-gate #include	"msg.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int ld_flags_env(const char *, Word *, Word *, uint_t, int);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Null function used as place where a debugger can set a breakpoint.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate void
615aefb655Srie rtld_db_dlactivity(Lm_list *lml)
627c478bd9Sstevel@tonic-gate {
635aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
645aefb655Srie 	    r_debug.rtd_rdebug.r_state));
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Null function used as place where debugger can set a pre .init
697c478bd9Sstevel@tonic-gate  * processing breakpoint.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate void
725aefb655Srie rtld_db_preinit(Lm_list *lml)
737c478bd9Sstevel@tonic-gate {
745aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
755aefb655Srie 	    r_debug.rtd_rdebug.r_state));
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Null function used as place where debugger can set a post .init
807c478bd9Sstevel@tonic-gate  * processing breakpoint.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate void
835aefb655Srie rtld_db_postinit(Lm_list *lml)
847c478bd9Sstevel@tonic-gate {
855aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
865aefb655Srie 	    r_debug.rtd_rdebug.r_state));
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * Debugger Event Notification
917c478bd9Sstevel@tonic-gate  *
927c478bd9Sstevel@tonic-gate  * This function centralizes all debugger event notification (ala rtld_db).
937c478bd9Sstevel@tonic-gate  *
947c478bd9Sstevel@tonic-gate  * There's a simple intent, focused on insuring the primary link-map control
957c478bd9Sstevel@tonic-gate  * list (or each link-map list) is consistent, and the indication that objects
967c478bd9Sstevel@tonic-gate  * have been added or deleted from this list.  Although an RD_ADD and RD_DELETE
977c478bd9Sstevel@tonic-gate  * event are posted for each of these, most debuggers don't care, as their
987c478bd9Sstevel@tonic-gate  * view is that these events simply convey an "inconsistent" state.
997c478bd9Sstevel@tonic-gate  *
1007c478bd9Sstevel@tonic-gate  * We also don't want to trigger multiple RD_ADD/RD_DELETE events any time we
1017c478bd9Sstevel@tonic-gate  * enter ld.so.1.
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * With auditors, we may be in the process of relocating a collection of
1047c478bd9Sstevel@tonic-gate  * objects, and will leave() ld.so.1 to call the auditor.  At this point we
1057c478bd9Sstevel@tonic-gate  * must indicate an RD_CONSISTENT event, but librtld_db will not report an
1067c478bd9Sstevel@tonic-gate  * object to the debuggers until relocation processing has been completed on it.
1077c478bd9Sstevel@tonic-gate  * To allow for the collection of these objects that are pending relocation, an
1087c478bd9Sstevel@tonic-gate  * RD_ADD event is set after completing a series of relocations on the primary
1097c478bd9Sstevel@tonic-gate  * link-map control list.
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * Set an RD_ADD/RD_DELETE event and indicate that an RD_CONSISTENT event is
1127c478bd9Sstevel@tonic-gate  * required later (LML_FLG_DBNOTIF):
1137c478bd9Sstevel@tonic-gate  *
1147c478bd9Sstevel@tonic-gate  *  i	the first time we add or delete an object to the primary link-map
1157c478bd9Sstevel@tonic-gate  *	control list.
1167c478bd9Sstevel@tonic-gate  *  ii	the first time we move a secondary link-map control list to the primary
1177c478bd9Sstevel@tonic-gate  *	link-map control list (effectively, this is like adding a group of
1187c478bd9Sstevel@tonic-gate  *	objects to the primary link-map control list).
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * Set an RD_CONSISTENT event when it is required (LML_FLG_DBNOTIF is set) and
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  *  i	each time we leave the runtime linker.
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate void
1257c478bd9Sstevel@tonic-gate rd_event(Lm_list *lml, rd_event_e event, r_state_e state)
1267c478bd9Sstevel@tonic-gate {
12710a4fa49Srie 	void	(*fptr)(Lm_list *);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	switch (event) {
1307c478bd9Sstevel@tonic-gate 	case RD_PREINIT:
1317c478bd9Sstevel@tonic-gate 		fptr = rtld_db_preinit;
1327c478bd9Sstevel@tonic-gate 		break;
1337c478bd9Sstevel@tonic-gate 	case RD_POSTINIT:
1347c478bd9Sstevel@tonic-gate 		fptr = rtld_db_postinit;
1357c478bd9Sstevel@tonic-gate 		break;
1367c478bd9Sstevel@tonic-gate 	case RD_DLACTIVITY:
1377c478bd9Sstevel@tonic-gate 		switch (state) {
1387c478bd9Sstevel@tonic-gate 		case RT_CONSISTENT:
1397c478bd9Sstevel@tonic-gate 			lml->lm_flags &= ~LML_FLG_DBNOTIF;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 			/*
1427c478bd9Sstevel@tonic-gate 			 * Do we need to send a notification?
1437c478bd9Sstevel@tonic-gate 			 */
1447c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_DBNOTIF) == 0)
1457c478bd9Sstevel@tonic-gate 				return;
1467c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_DBNOTIF;
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 		case RT_ADD:
1497c478bd9Sstevel@tonic-gate 		case RT_DELETE:
1507c478bd9Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_DBNOTIF;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 			/*
1537c478bd9Sstevel@tonic-gate 			 * If we are already in an inconsistent state, no
1547c478bd9Sstevel@tonic-gate 			 * notification is required.
1557c478bd9Sstevel@tonic-gate 			 */
1567c478bd9Sstevel@tonic-gate 			if (rtld_flags & RT_FL_DBNOTIF)
1577c478bd9Sstevel@tonic-gate 				return;
1587c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_DBNOTIF;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		};
1617c478bd9Sstevel@tonic-gate 		fptr = rtld_db_dlactivity;
1627c478bd9Sstevel@tonic-gate 		break;
1637c478bd9Sstevel@tonic-gate 	default:
1647c478bd9Sstevel@tonic-gate 		/*
1657c478bd9Sstevel@tonic-gate 		 * RD_NONE - do nothing
1667c478bd9Sstevel@tonic-gate 		 */
1677c478bd9Sstevel@tonic-gate 		break;
1687c478bd9Sstevel@tonic-gate 	};
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	/*
1717c478bd9Sstevel@tonic-gate 	 * Set event state and call 'notification' function.
1727c478bd9Sstevel@tonic-gate 	 *
1737c478bd9Sstevel@tonic-gate 	 * The debugging clients have previously been told about these
1747c478bd9Sstevel@tonic-gate 	 * notification functions and have set breakpoints on them if they
1757c478bd9Sstevel@tonic-gate 	 * are interested in the notification.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_state = state;
1787c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = event;
1795aefb655Srie 	fptr(lml);
1807c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = RD_NONE;
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
18302ca3e02Srie #if	defined(__sparc) || defined(__x86)
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Stack Cleanup.
1867c478bd9Sstevel@tonic-gate  *
1877c478bd9Sstevel@tonic-gate  * This function is invoked to 'remove' arguments that were passed in on the
1887c478bd9Sstevel@tonic-gate  * stack.  This is most likely if ld.so.1 was invoked directly.  In that case
1897c478bd9Sstevel@tonic-gate  * we want to remove ld.so.1 as well as it's arguments from the argv[] array.
1907c478bd9Sstevel@tonic-gate  * Which means we then need to slide everything above it on the stack down
1917c478bd9Sstevel@tonic-gate  * accordingly.
1927c478bd9Sstevel@tonic-gate  *
19302ca3e02Srie  * While the stack layout is platform specific - it just so happens that __x86,
19402ca3e02Srie  * and __sparc platforms share the following initial stack layout.
1957c478bd9Sstevel@tonic-gate  *
1967c478bd9Sstevel@tonic-gate  *	!_______________________!  high addresses
1977c478bd9Sstevel@tonic-gate  *	!			!
1987c478bd9Sstevel@tonic-gate  *	!	Information	!
1997c478bd9Sstevel@tonic-gate  *	!	Block		!
2007c478bd9Sstevel@tonic-gate  *	!	(size varies)	!
2017c478bd9Sstevel@tonic-gate  *	!_______________________!
2027c478bd9Sstevel@tonic-gate  *	!	0 word		!
2037c478bd9Sstevel@tonic-gate  *	!_______________________!
2047c478bd9Sstevel@tonic-gate  *	!	Auxiliary	!
2057c478bd9Sstevel@tonic-gate  *	!	vector		!
2067c478bd9Sstevel@tonic-gate  *	!	2 word entries	!
2077c478bd9Sstevel@tonic-gate  *	!			!
2087c478bd9Sstevel@tonic-gate  *	!_______________________!
2097c478bd9Sstevel@tonic-gate  *	!	0 word		!
2107c478bd9Sstevel@tonic-gate  *	!_______________________!
2117c478bd9Sstevel@tonic-gate  *	!	Environment	!
2127c478bd9Sstevel@tonic-gate  *	!	pointers	!
2137c478bd9Sstevel@tonic-gate  *	!	...		!
2147c478bd9Sstevel@tonic-gate  *	!	(one word each)	!
2157c478bd9Sstevel@tonic-gate  *	!_______________________!
2167c478bd9Sstevel@tonic-gate  *	!	0 word		!
2177c478bd9Sstevel@tonic-gate  *	!_______________________!
2187c478bd9Sstevel@tonic-gate  *	!	Argument	! low addresses
2197c478bd9Sstevel@tonic-gate  *	!	pointers	!
2207c478bd9Sstevel@tonic-gate  *	!	Argc words	!
2217c478bd9Sstevel@tonic-gate  *	!_______________________!
2227c478bd9Sstevel@tonic-gate  *	!			!
2237c478bd9Sstevel@tonic-gate  *	!	Argc		!
2247c478bd9Sstevel@tonic-gate  *	!_______________________!
2257c478bd9Sstevel@tonic-gate  *	!	...		!
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate static void
22941072f3cSrie stack_cleanup(char **argv, char ***envp, auxv_t **auxv, int rmcnt)
2307c478bd9Sstevel@tonic-gate {
23141072f3cSrie 	int		ndx;
2327c478bd9Sstevel@tonic-gate 	long		*argc;
23341072f3cSrie 	char		**oargv, **nargv;
23441072f3cSrie 	char		**oenvp, **nenvp;
23541072f3cSrie 	auxv_t		*oauxv, *nauxv;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	/*
23841072f3cSrie 	 * Slide ARGV[] and update argc.  The argv pointer remains the same,
23941072f3cSrie 	 * however slide the applications arguments over the arguments to
24041072f3cSrie 	 * ld.so.1.
2417c478bd9Sstevel@tonic-gate 	 */
24241072f3cSrie 	nargv = &argv[0];
24341072f3cSrie 	oargv = &argv[rmcnt];
24441072f3cSrie 
24541072f3cSrie 	for (ndx = 0; oargv[ndx]; ndx++)
24641072f3cSrie 		nargv[ndx] = oargv[ndx];
24741072f3cSrie 	nargv[ndx] = oargv[ndx];
24841072f3cSrie 
24941072f3cSrie 	argc = (long *)((uintptr_t)argv - sizeof (long *));
2507c478bd9Sstevel@tonic-gate 	*argc -= rmcnt;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
25341072f3cSrie 	 * Slide ENVP[], and update the environment array pointer.
2547c478bd9Sstevel@tonic-gate 	 */
25541072f3cSrie 	ndx++;
25641072f3cSrie 	nenvp = &nargv[ndx];
25741072f3cSrie 	oenvp = &oargv[ndx];
25841072f3cSrie 	*envp = nenvp;
25941072f3cSrie 
26041072f3cSrie 	for (ndx = 0; oenvp[ndx]; ndx++)
26141072f3cSrie 		nenvp[ndx] = oenvp[ndx];
26241072f3cSrie 	nenvp[ndx] = oenvp[ndx];
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
26541072f3cSrie 	 * Slide AUXV[], and update the aux vector pointer.
2667c478bd9Sstevel@tonic-gate 	 */
26741072f3cSrie 	ndx++;
26841072f3cSrie 	nauxv = (auxv_t *)&nenvp[ndx];
26941072f3cSrie 	oauxv = (auxv_t *)&oenvp[ndx];
27041072f3cSrie 	*auxv = nauxv;
27141072f3cSrie 
27241072f3cSrie 	for (ndx = 0; (oauxv[ndx].a_type != AT_NULL); ndx++)
27341072f3cSrie 		nauxv[ndx] = oauxv[ndx];
27441072f3cSrie 	nauxv[ndx] = oauxv[ndx];
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate #else
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Verify that the above routine is appropriate for any new platforms.
2797c478bd9Sstevel@tonic-gate  */
2807c478bd9Sstevel@tonic-gate #error	unsupported architecture!
2817c478bd9Sstevel@tonic-gate #endif
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
28441072f3cSrie  * The only command line argument recognized is -e, followed by a runtime
28541072f3cSrie  * linker environment variable.
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate int
28841072f3cSrie rtld_getopt(char **argv, char ***envp, auxv_t **auxv, Word *lmflags,
28941072f3cSrie     Word *lmtflags, int aout)
2907c478bd9Sstevel@tonic-gate {
29141072f3cSrie 	int	ndx;
2927c478bd9Sstevel@tonic-gate 
29341072f3cSrie 	for (ndx = 1; argv[ndx]; ndx++) {
2947c478bd9Sstevel@tonic-gate 		char	*str;
2957c478bd9Sstevel@tonic-gate 
29641072f3cSrie 		if (argv[ndx][0] != '-')
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 
29941072f3cSrie 		if (argv[ndx][1] == '\0') {
30041072f3cSrie 			ndx++;
3017c478bd9Sstevel@tonic-gate 			break;
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 
30441072f3cSrie 		if (argv[ndx][1] != 'e')
30541072f3cSrie 			return (1);
3067c478bd9Sstevel@tonic-gate 
30741072f3cSrie 		if (argv[ndx][2] == '\0') {
30841072f3cSrie 			ndx++;
30941072f3cSrie 			if (argv[ndx] == NULL)
31041072f3cSrie 				return (1);
31141072f3cSrie 			str = argv[ndx];
3127c478bd9Sstevel@tonic-gate 		} else
31341072f3cSrie 			str = &argv[ndx][2];
3147c478bd9Sstevel@tonic-gate 
31541072f3cSrie 		/*
31641072f3cSrie 		 * If the environment variable starts with LD_, strip the LD_.
31741072f3cSrie 		 * Otherwise, take things as is.
31841072f3cSrie 		 */
31941072f3cSrie 		if ((str[0] == 'L') && (str[1] == 'D') && (str[2] == '_') &&
32041072f3cSrie 		    (str[3] != '\0'))
32141072f3cSrie 			str += 3;
3227c478bd9Sstevel@tonic-gate 		if (ld_flags_env(str, lmflags, lmtflags, 0, aout) == 1)
3237c478bd9Sstevel@tonic-gate 			return (1);
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
32641072f3cSrie 	/*
32741072f3cSrie 	 * Make sure an object file has been specified.
32841072f3cSrie 	 */
32943d7826aSRod Evans 	if (argv[ndx] == NULL)
3307c478bd9Sstevel@tonic-gate 		return (1);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	/*
3337c478bd9Sstevel@tonic-gate 	 * Having gotten the arguments, clean ourselves off of the stack.
3347c478bd9Sstevel@tonic-gate 	 */
33541072f3cSrie 	stack_cleanup(argv, envp, auxv, ndx);
3367c478bd9Sstevel@tonic-gate 	return (0);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate /*
3409aa23310Srie  * Compare function for PathNode AVL tree.
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate static int
3439aa23310Srie pnavl_compare(const void *n1, const void *n2)
3447c478bd9Sstevel@tonic-gate {
3457c478bd9Sstevel@tonic-gate 	uint_t		hash1, hash2;
3467c478bd9Sstevel@tonic-gate 	const char	*st1, *st2;
3477c478bd9Sstevel@tonic-gate 	int		rc;
3487c478bd9Sstevel@tonic-gate 
3499aa23310Srie 	hash1 = ((PathNode *)n1)->pn_hash;
3509aa23310Srie 	hash2 = ((PathNode *)n2)->pn_hash;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (hash1 > hash2)
3537c478bd9Sstevel@tonic-gate 		return (1);
3547c478bd9Sstevel@tonic-gate 	if (hash1 < hash2)
3557c478bd9Sstevel@tonic-gate 		return (-1);
3567c478bd9Sstevel@tonic-gate 
3579aa23310Srie 	st1 = ((PathNode *)n1)->pn_name;
3589aa23310Srie 	st2 = ((PathNode *)n2)->pn_name;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	rc = strcmp(st1, st2);
3617c478bd9Sstevel@tonic-gate 	if (rc > 0)
3627c478bd9Sstevel@tonic-gate 		return (1);
3637c478bd9Sstevel@tonic-gate 	if (rc < 0)
3647c478bd9Sstevel@tonic-gate 		return (-1);
3657c478bd9Sstevel@tonic-gate 	return (0);
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate 
3683dbfc803SRod Evans /*
3693dbfc803SRod Evans  * Create an AVL tree.
3703dbfc803SRod Evans  */
3713dbfc803SRod Evans static avl_tree_t *
3723dbfc803SRod Evans pnavl_create(size_t size)
3733dbfc803SRod Evans {
3743dbfc803SRod Evans 	avl_tree_t	*avlt;
3753dbfc803SRod Evans 
3763dbfc803SRod Evans 	if ((avlt = malloc(sizeof (avl_tree_t))) == NULL)
3773dbfc803SRod Evans 		return (NULL);
3783dbfc803SRod Evans 	avl_create(avlt, pnavl_compare, size, SGSOFFSETOF(PathNode, pn_avl));
3793dbfc803SRod Evans 	return (avlt);
3803dbfc803SRod Evans }
3813dbfc803SRod Evans 
38208278a5eSRod Evans /*
38308278a5eSRod Evans  * Determine whether a PathNode is recorded.
38408278a5eSRod Evans  */
38508278a5eSRod Evans int
38608278a5eSRod Evans pnavl_recorded(avl_tree_t **pnavl, const char *name, uint_t hash,
38708278a5eSRod Evans     avl_index_t *where)
38808278a5eSRod Evans {
38908278a5eSRod Evans 	PathNode	pn;
39008278a5eSRod Evans 
39108278a5eSRod Evans 	/*
39208278a5eSRod Evans 	 * Create the avl tree if required.
39308278a5eSRod Evans 	 */
39408278a5eSRod Evans 	if ((*pnavl == NULL) &&
39508278a5eSRod Evans 	    ((*pnavl = pnavl_create(sizeof (PathNode))) == NULL))
39608278a5eSRod Evans 		return (0);
39708278a5eSRod Evans 
39808278a5eSRod Evans 	pn.pn_name = name;
39908278a5eSRod Evans 	if ((pn.pn_hash = hash) == 0)
40008278a5eSRod Evans 		pn.pn_hash = sgs_str_hash(name);
40108278a5eSRod Evans 
40208278a5eSRod Evans 	if (avl_find(*pnavl, &pn, where) == NULL)
40308278a5eSRod Evans 		return (0);
40408278a5eSRod Evans 
40508278a5eSRod Evans 	return (1);
40608278a5eSRod Evans }
40708278a5eSRod Evans 
4087c478bd9Sstevel@tonic-gate /*
4099aa23310Srie  * Determine if a pathname has already been recorded on the full path name
4109aa23310Srie  * AVL tree.  This tree maintains a node for each path name that ld.so.1 has
4119aa23310Srie  * successfully loaded.  If the path name does not exist in this AVL tree, then
4129aa23310Srie  * the next insertion point is deposited in "where".  This value can be used by
4139aa23310Srie  * fpavl_insert() to expedite the insertion.
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate Rt_map *
41656deab07SRod Evans fpavl_recorded(Lm_list *lml, const char *name, uint_t hash, avl_index_t *where)
4177c478bd9Sstevel@tonic-gate {
4189aa23310Srie 	FullPathNode	fpn, *fpnp;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	/*
4217c478bd9Sstevel@tonic-gate 	 * Create the avl tree if required.
4227c478bd9Sstevel@tonic-gate 	 */
4233dbfc803SRod Evans 	if ((lml->lm_fpavl == NULL) &&
4243dbfc803SRod Evans 	    ((lml->lm_fpavl = pnavl_create(sizeof (FullPathNode))) == NULL))
4253dbfc803SRod Evans 		return (NULL);
4267c478bd9Sstevel@tonic-gate 
4279aa23310Srie 	fpn.fpn_node.pn_name = name;
42856deab07SRod Evans 	if ((fpn.fpn_node.pn_hash = hash) == 0)
42956deab07SRod Evans 		fpn.fpn_node.pn_hash = sgs_str_hash(name);
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	if ((fpnp = avl_find(lml->lm_fpavl, &fpn, where)) == NULL)
4327c478bd9Sstevel@tonic-gate 		return (NULL);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	return (fpnp->fpn_lmp);
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate /*
4389aa23310Srie  * Insert a name into the FullPathNode AVL tree for the link-map list.  The
43924a6229eSrie  * objects NAME() is the path that would have originally been searched for, and
44024a6229eSrie  * is therefore the name to associate with any "where" value.  If the object has
4417c478bd9Sstevel@tonic-gate  * a different PATHNAME(), perhaps because it has resolved to a different file
4429aa23310Srie  * (see fullpath()), then this name will be recorded as a separate FullPathNode
4439aa23310Srie  * (see load_file()).
4447c478bd9Sstevel@tonic-gate  */
4457c478bd9Sstevel@tonic-gate int
4467c478bd9Sstevel@tonic-gate fpavl_insert(Lm_list *lml, Rt_map *lmp, const char *name, avl_index_t where)
4477c478bd9Sstevel@tonic-gate {
4489aa23310Srie 	FullPathNode	*fpnp;
44956deab07SRod Evans 	uint_t		hash = sgs_str_hash(name);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (where == 0) {
4527c478bd9Sstevel@tonic-gate 		/* LINTED */
45356deab07SRod Evans 		Rt_map	*_lmp = fpavl_recorded(lml, name, hash, &where);
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		/*
4567c478bd9Sstevel@tonic-gate 		 * We better not get a hit now, we do not want duplicates in
4577c478bd9Sstevel@tonic-gate 		 * the tree.
4587c478bd9Sstevel@tonic-gate 		 */
45943d7826aSRod Evans 		ASSERT(_lmp == NULL);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4639aa23310Srie 	 * Insert new node in tree.
4647c478bd9Sstevel@tonic-gate 	 */
4653dbfc803SRod Evans 	if ((fpnp = calloc(sizeof (FullPathNode), 1)) == NULL)
4667c478bd9Sstevel@tonic-gate 		return (0);
4677c478bd9Sstevel@tonic-gate 
4689aa23310Srie 	fpnp->fpn_node.pn_name = name;
46956deab07SRod Evans 	fpnp->fpn_node.pn_hash = hash;
4707c478bd9Sstevel@tonic-gate 	fpnp->fpn_lmp = lmp;
4717c478bd9Sstevel@tonic-gate 
472cce0e03bSab 	if (aplist_append(&FPNODE(lmp), fpnp, AL_CNT_FPNODE) == NULL) {
4737c478bd9Sstevel@tonic-gate 		free(fpnp);
4747c478bd9Sstevel@tonic-gate 		return (0);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	ASSERT(lml->lm_fpavl != NULL);
4787c478bd9Sstevel@tonic-gate 	avl_insert(lml->lm_fpavl, fpnp, where);
4797c478bd9Sstevel@tonic-gate 	return (1);
4807c478bd9Sstevel@tonic-gate }
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
48356deab07SRod Evans  * Remove an object from the FullPathNode AVL tree.
4847c478bd9Sstevel@tonic-gate  */
4857c478bd9Sstevel@tonic-gate void
4867c478bd9Sstevel@tonic-gate fpavl_remove(Rt_map *lmp)
4877c478bd9Sstevel@tonic-gate {
4889aa23310Srie 	FullPathNode	*fpnp;
489cce0e03bSab 	Aliste		idx;
4907c478bd9Sstevel@tonic-gate 
491cce0e03bSab 	for (APLIST_TRAVERSE(FPNODE(lmp), idx, fpnp)) {
4927c478bd9Sstevel@tonic-gate 		avl_remove(LIST(lmp)->lm_fpavl, fpnp);
4937c478bd9Sstevel@tonic-gate 		free(fpnp);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	free(FPNODE(lmp));
496cce0e03bSab 	FPNODE(lmp) = NULL;
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
4999aa23310Srie /*
50008278a5eSRod Evans  * Insert a path name into the not-found AVL tree.
50108278a5eSRod Evans  *
5029aa23310Srie  * This tree maintains a node for each path name that ld.so.1 has explicitly
5039aa23310Srie  * inspected, but has failed to load during a single ld.so.1 operation.  If the
5049aa23310Srie  * path name does not exist in this AVL tree, then the next insertion point is
5059aa23310Srie  * deposited in "where".  This value can be used by nfavl_insert() to expedite
5069aa23310Srie  * the insertion.
5079aa23310Srie  */
5089aa23310Srie void
5099aa23310Srie nfavl_insert(const char *name, avl_index_t where)
5109aa23310Srie {
5119aa23310Srie 	PathNode	*pnp;
51256deab07SRod Evans 	uint_t		hash = sgs_str_hash(name);
5139aa23310Srie 
5149aa23310Srie 	if (where == 0) {
5159aa23310Srie 		/* LINTED */
51608278a5eSRod Evans 		int	in_nfavl = pnavl_recorded(&nfavl, name, hash, &where);
5179aa23310Srie 
5189aa23310Srie 		/*
5199aa23310Srie 		 * We better not get a hit now, we do not want duplicates in
5209aa23310Srie 		 * the tree.
5219aa23310Srie 		 */
5229aa23310Srie 		ASSERT(in_nfavl == 0);
5239aa23310Srie 	}
5249aa23310Srie 
5259aa23310Srie 	/*
5269aa23310Srie 	 * Insert new node in tree.
5279aa23310Srie 	 */
52856deab07SRod Evans 	if ((pnp = calloc(sizeof (PathNode), 1)) != NULL) {
5299aa23310Srie 		pnp->pn_name = name;
53056deab07SRod Evans 		pnp->pn_hash = hash;
5319aa23310Srie 		avl_insert(nfavl, pnp, where);
5329aa23310Srie 	}
5339aa23310Srie }
5347c478bd9Sstevel@tonic-gate 
5353dbfc803SRod Evans /*
53608278a5eSRod Evans  * Insert the directory name, of a full path name, into the secure path AVL
5373dbfc803SRod Evans  * tree.
53808278a5eSRod Evans  *
53908278a5eSRod Evans  * This tree is used to maintain a list of directories in which the dependencies
54008278a5eSRod Evans  * of a secure process have been found.  This list provides a fall-back in the
54108278a5eSRod Evans  * case that a $ORIGIN expansion is deemed insecure, when the expansion results
54208278a5eSRod Evans  * in a path name that has already provided dependencies.
5433dbfc803SRod Evans  */
5443dbfc803SRod Evans void
5453dbfc803SRod Evans spavl_insert(const char *name)
5463dbfc803SRod Evans {
5473dbfc803SRod Evans 	char		buffer[PATH_MAX], *str;
5483dbfc803SRod Evans 	size_t		size;
5493dbfc803SRod Evans 	avl_index_t	where;
5503dbfc803SRod Evans 	PathNode	*pnp;
55108278a5eSRod Evans 	uint_t		hash;
5523dbfc803SRod Evans 
5533dbfc803SRod Evans 	/*
5543dbfc803SRod Evans 	 * Separate the directory name from the path name.
5553dbfc803SRod Evans 	 */
5563dbfc803SRod Evans 	if ((str = strrchr(name, '/')) == name)
5573dbfc803SRod Evans 		size = 1;
5583dbfc803SRod Evans 	else
5593dbfc803SRod Evans 		size = str - name;
5603dbfc803SRod Evans 
5613dbfc803SRod Evans 	(void) strncpy(buffer, name, size);
5623dbfc803SRod Evans 	buffer[size] = '\0';
56308278a5eSRod Evans 	hash = sgs_str_hash(buffer);
5643dbfc803SRod Evans 
5653dbfc803SRod Evans 	/*
5663dbfc803SRod Evans 	 * Determine whether this directory name is already recorded, or if
5673dbfc803SRod Evans 	 * not, 'where" will provide the insertion point for the new string.
5683dbfc803SRod Evans 	 */
56908278a5eSRod Evans 	if (pnavl_recorded(&spavl, buffer, hash, &where))
5703dbfc803SRod Evans 		return;
5713dbfc803SRod Evans 
5723dbfc803SRod Evans 	/*
5733dbfc803SRod Evans 	 * Insert new node in tree.
5743dbfc803SRod Evans 	 */
57543d7826aSRod Evans 	if ((pnp = calloc(sizeof (PathNode), 1)) != NULL) {
5763dbfc803SRod Evans 		pnp->pn_name = strdup(buffer);
57708278a5eSRod Evans 		pnp->pn_hash = hash;
5783dbfc803SRod Evans 		avl_insert(spavl, pnp, where);
5793dbfc803SRod Evans 	}
5803dbfc803SRod Evans }
5813dbfc803SRod Evans 
58256deab07SRod Evans /*
58356deab07SRod Evans  * Inspect the generic string AVL tree for the given string.  If the string is
58456deab07SRod Evans  * not present, duplicate it, and insert the string in the AVL tree.  Return the
58556deab07SRod Evans  * duplicated string to the caller.
58656deab07SRod Evans  *
58756deab07SRod Evans  * These strings are maintained for the life of ld.so.1 and represent path
58856deab07SRod Evans  * names, file names, and search paths.  All other AVL trees that maintain
58956deab07SRod Evans  * FullPathNode and not-found path names use the same string pointer
59056deab07SRod Evans  * established for this string.
59156deab07SRod Evans  */
59256deab07SRod Evans static avl_tree_t	*stravl = NULL;
59356deab07SRod Evans static char		*strbuf = NULL;
59456deab07SRod Evans static PathNode		*pnbuf = NULL;
59556deab07SRod Evans static size_t		strsize = 0, pnsize = 0;
59656deab07SRod Evans 
59756deab07SRod Evans const char *
59856deab07SRod Evans stravl_insert(const char *name, uint_t hash, size_t nsize, int substr)
59956deab07SRod Evans {
60056deab07SRod Evans 	char		str[PATH_MAX];
60156deab07SRod Evans 	PathNode	*pnp;
60256deab07SRod Evans 	avl_index_t	where;
60356deab07SRod Evans 
60456deab07SRod Evans 	/*
60556deab07SRod Evans 	 * Create the avl tree if required.
60656deab07SRod Evans 	 */
60756deab07SRod Evans 	if ((stravl == NULL) &&
60856deab07SRod Evans 	    ((stravl = pnavl_create(sizeof (PathNode))) == NULL))
60956deab07SRod Evans 		return (NULL);
61056deab07SRod Evans 
61156deab07SRod Evans 	/*
61256deab07SRod Evans 	 * Determine the string size if not provided by the caller.
61356deab07SRod Evans 	 */
61456deab07SRod Evans 	if (nsize == 0)
61556deab07SRod Evans 		nsize = strlen(name) + 1;
61656deab07SRod Evans 	else if (substr) {
61756deab07SRod Evans 		/*
61856deab07SRod Evans 		 * The string passed to us may be a multiple path string for
61956deab07SRod Evans 		 * which we only need the first component.  Using the provided
62056deab07SRod Evans 		 * size, strip out the required string.
62156deab07SRod Evans 		 */
62256deab07SRod Evans 		(void) strncpy(str, name, nsize);
62356deab07SRod Evans 		str[nsize - 1] = '\0';
62456deab07SRod Evans 		name = str;
62556deab07SRod Evans 	}
62656deab07SRod Evans 
62756deab07SRod Evans 	/*
62856deab07SRod Evans 	 * Allocate a PathNode buffer if one doesn't exist, or any existing
62956deab07SRod Evans 	 * buffer has been used up.
63056deab07SRod Evans 	 */
63156deab07SRod Evans 	if ((pnbuf == NULL) || (sizeof (PathNode) > pnsize)) {
63256deab07SRod Evans 		pnsize = syspagsz;
63356deab07SRod Evans 		if ((pnbuf = dz_map(0, 0, pnsize, (PROT_READ | PROT_WRITE),
63456deab07SRod Evans 		    MAP_PRIVATE)) == MAP_FAILED)
63556deab07SRod Evans 			return (NULL);
63656deab07SRod Evans 	}
63756deab07SRod Evans 	/*
63856deab07SRod Evans 	 * Determine whether this string already exists.
63956deab07SRod Evans 	 */
64056deab07SRod Evans 	pnbuf->pn_name = name;
64156deab07SRod Evans 	if ((pnbuf->pn_hash = hash) == 0)
64256deab07SRod Evans 		pnbuf->pn_hash = sgs_str_hash(name);
64356deab07SRod Evans 
64456deab07SRod Evans 	if ((pnp = avl_find(stravl, pnbuf, &where)) != NULL)
64556deab07SRod Evans 		return (pnp->pn_name);
64656deab07SRod Evans 
64756deab07SRod Evans 	/*
64856deab07SRod Evans 	 * Allocate a string buffer if one does not exist, or if there is
64956deab07SRod Evans 	 * insufficient space for the new string in any existing buffer.
65056deab07SRod Evans 	 */
65156deab07SRod Evans 	if ((strbuf == NULL) || (nsize > strsize)) {
65256deab07SRod Evans 		strsize = S_ROUND(nsize, syspagsz);
65356deab07SRod Evans 
65456deab07SRod Evans 		if ((strbuf = dz_map(0, 0, strsize, (PROT_READ | PROT_WRITE),
65556deab07SRod Evans 		    MAP_PRIVATE)) == MAP_FAILED)
65656deab07SRod Evans 			return (NULL);
65756deab07SRod Evans 	}
65856deab07SRod Evans 
65956deab07SRod Evans 	(void) memcpy(strbuf, name, nsize);
66056deab07SRod Evans 	pnp = pnbuf;
66156deab07SRod Evans 	pnp->pn_name = strbuf;
66256deab07SRod Evans 	avl_insert(stravl, pnp, where);
66356deab07SRod Evans 
66456deab07SRod Evans 	strbuf += nsize;
66556deab07SRod Evans 	strsize -= nsize;
66656deab07SRod Evans 	pnbuf++;
66756deab07SRod Evans 	pnsize -= sizeof (PathNode);
66856deab07SRod Evans 	return (pnp->pn_name);
66956deab07SRod Evans }
67056deab07SRod Evans 
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate  * Prior to calling an object, either via a .plt or through dlsym(), make sure
6737c478bd9Sstevel@tonic-gate  * its .init has fired.  Through topological sorting, ld.so.1 attempts to fire
6747c478bd9Sstevel@tonic-gate  * init's in the correct order, however, this order is typically based on needed
6757c478bd9Sstevel@tonic-gate  * dependencies and non-lazy relocation bindings.  Lazy relocations (.plts) can
6767c478bd9Sstevel@tonic-gate  * still occur and result in bindings that were not captured during topological
6777c478bd9Sstevel@tonic-gate  * sorting.  This routine compensates for this lack of binding information, and
6787c478bd9Sstevel@tonic-gate  * provides for dynamic .init firing.
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate void
6819aa23310Srie is_dep_init(Rt_map *dlmp, Rt_map *clmp)
6827c478bd9Sstevel@tonic-gate {
6839aa23310Srie 	Rt_map	**tobj;
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 	/*
6867c478bd9Sstevel@tonic-gate 	 * If the caller is an auditor, and the destination isn't, then don't
6877c478bd9Sstevel@tonic-gate 	 * run any .inits (see comments in load_completion()).
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
6907c478bd9Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
6917c478bd9Sstevel@tonic-gate 		return;
6927c478bd9Sstevel@tonic-gate 
69356deab07SRod Evans 	if ((dlmp == clmp) || (rtld_flags & RT_FL_INITFIRST))
6947c478bd9Sstevel@tonic-gate 		return;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITDONE)) ==
6977c478bd9Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITDONE))
6987c478bd9Sstevel@tonic-gate 		return;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITCALL)) ==
7017c478bd9Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITCALL)) {
7025aefb655Srie 		DBG_CALL(Dbg_util_no_init(dlmp));
7037c478bd9Sstevel@tonic-gate 		return;
7047c478bd9Sstevel@tonic-gate 	}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	if ((tobj = calloc(2, sizeof (Rt_map *))) != NULL) {
7077c478bd9Sstevel@tonic-gate 		tobj[0] = dlmp;
7087c478bd9Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_DYN);
7097c478bd9Sstevel@tonic-gate 	}
7107c478bd9Sstevel@tonic-gate }
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate /*
7137c478bd9Sstevel@tonic-gate  * Execute .{preinit|init|fini}array sections
7147c478bd9Sstevel@tonic-gate  */
7157c478bd9Sstevel@tonic-gate void
7165aefb655Srie call_array(Addr *array, uint_t arraysz, Rt_map *lmp, Word shtype)
7177c478bd9Sstevel@tonic-gate {
7185aefb655Srie 	int	start, stop, incr, ndx;
7197c478bd9Sstevel@tonic-gate 	uint_t	arraycnt = (uint_t)(arraysz / sizeof (Addr));
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (array == NULL)
7227c478bd9Sstevel@tonic-gate 		return;
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	/*
7257c478bd9Sstevel@tonic-gate 	 * initarray & preinitarray are walked from beginning to end - while
7267c478bd9Sstevel@tonic-gate 	 * finiarray is walked from end to beginning.
7277c478bd9Sstevel@tonic-gate 	 */
7287c478bd9Sstevel@tonic-gate 	if (shtype == SHT_FINI_ARRAY) {
7297c478bd9Sstevel@tonic-gate 		start = arraycnt - 1;
7307c478bd9Sstevel@tonic-gate 		stop = incr = -1;
7317c478bd9Sstevel@tonic-gate 	} else {
7327c478bd9Sstevel@tonic-gate 		start = 0;
7337c478bd9Sstevel@tonic-gate 		stop = arraycnt;
7347c478bd9Sstevel@tonic-gate 		incr = 1;
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/*
7387c478bd9Sstevel@tonic-gate 	 * Call the .*array[] entries
7397c478bd9Sstevel@tonic-gate 	 */
7405aefb655Srie 	for (ndx = start; ndx != stop; ndx += incr) {
74110a4fa49Srie 		void (*fptr)(void) = (void(*)())array[ndx];
7427c478bd9Sstevel@tonic-gate 
7435aefb655Srie 		DBG_CALL(Dbg_util_call_array(lmp, (void *)fptr, ndx, shtype));
7447c478bd9Sstevel@tonic-gate 
745eccf73c8Srie 		leave(LIST(lmp), 0);
7467c478bd9Sstevel@tonic-gate 		(*fptr)();
747eccf73c8Srie 		(void) enter(0);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * Execute any .init sections.  These are passed to us in an lmp array which
7547c478bd9Sstevel@tonic-gate  * (by default) will have been sorted.
7557c478bd9Sstevel@tonic-gate  */
7567c478bd9Sstevel@tonic-gate void
7579aa23310Srie call_init(Rt_map **tobj, int flag)
7587c478bd9Sstevel@tonic-gate {
7599aa23310Srie 	Rt_map		**_tobj, **_nobj;
76057ef7aa9SRod Evans 	static APlist	*pending = NULL;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/*
7637c478bd9Sstevel@tonic-gate 	 * If we're in the middle of an INITFIRST, this must complete before
7647c478bd9Sstevel@tonic-gate 	 * any new init's are fired.  In this case add the object list to the
7657c478bd9Sstevel@tonic-gate 	 * pending queue and return.  We'll pick up the queue after any
7667c478bd9Sstevel@tonic-gate 	 * INITFIRST objects have their init's fired.
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_INITFIRST) {
76957ef7aa9SRod Evans 		(void) aplist_append(&pending, tobj, AL_CNT_PENDING);
7707c478bd9Sstevel@tonic-gate 		return;
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	/*
7747c478bd9Sstevel@tonic-gate 	 * Traverse the tobj array firing each objects init.
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	for (_tobj = _nobj = tobj, _nobj++; *_tobj != NULL; _tobj++, _nobj++) {
7779aa23310Srie 		Rt_map	*lmp = *_tobj;
7789aa23310Srie 		void	(*iptr)() = INIT(lmp);
779dde769a2SRod Evans 		uint_t	rtldflags;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITCALL)
7827c478bd9Sstevel@tonic-gate 			continue;
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITCALL;
7857c478bd9Sstevel@tonic-gate 
786dde769a2SRod Evans 		/*
787dde769a2SRod Evans 		 * It is possible, that during the initial handshake with libc,
788dde769a2SRod Evans 		 * an interposition object has resolved a symbol binding, and
789dde769a2SRod Evans 		 * that this objects .init must be fired.  As we're about to
790dde769a2SRod Evans 		 * run user code, make sure any dynamic linking errors remain
791dde769a2SRod Evans 		 * internal (ie., only obtainable from dlerror()), and are not
792dde769a2SRod Evans 		 * flushed to stderr.
793dde769a2SRod Evans 		 */
794dde769a2SRod Evans 		rtldflags = (rtld_flags & RT_FL_APPLIC) ? 0 : RT_FL_APPLIC;
795dde769a2SRod Evans 		rtld_flags |= rtldflags;
796dde769a2SRod Evans 
7977c478bd9Sstevel@tonic-gate 		/*
7987c478bd9Sstevel@tonic-gate 		 * Establish an initfirst state if necessary - no other inits
799dffec89cSrie 		 * will be fired (because of additional relocation bindings)
800dffec89cSrie 		 * when in this state.
8017c478bd9Sstevel@tonic-gate 		 */
8027c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITFRST)
8037c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_INITFIRST;
8047c478bd9Sstevel@tonic-gate 
80556deab07SRod Evans 		if (INITARRAY(lmp) || iptr)
8065aefb655Srie 			DBG_CALL(Dbg_util_call_init(lmp, flag));
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 		if (iptr) {
809eccf73c8Srie 			leave(LIST(lmp), 0);
8107c478bd9Sstevel@tonic-gate 			(*iptr)();
811eccf73c8Srie 			(void) enter(0);
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		call_array(INITARRAY(lmp), INITARRAYSZ(lmp), lmp,
8157c478bd9Sstevel@tonic-gate 		    SHT_INIT_ARRAY);
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr)
8185aefb655Srie 			DBG_CALL(Dbg_util_call_init(lmp, DBG_INIT_DONE));
8197c478bd9Sstevel@tonic-gate 
820dde769a2SRod Evans 		/*
821dde769a2SRod Evans 		 * Return to a non-application setting if necessary.
822dde769a2SRod Evans 		 */
823dde769a2SRod Evans 		rtld_flags &= ~rtldflags;
824dde769a2SRod Evans 
8257c478bd9Sstevel@tonic-gate 		/*
8267c478bd9Sstevel@tonic-gate 		 * Set the initdone flag regardless of whether this object
8277c478bd9Sstevel@tonic-gate 		 * actually contains an .init section.  This flag prevents us
8287c478bd9Sstevel@tonic-gate 		 * from processing this section again for an .init and also
8297c478bd9Sstevel@tonic-gate 		 * signifies that a .fini must be called should it exist.
8307c478bd9Sstevel@tonic-gate 		 * Clear the sort field for use in later .fini processing.
8317c478bd9Sstevel@tonic-gate 		 */
8327c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITDONE;
833dffec89cSrie 		SORTVAL(lmp) = -1;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		/*
8367c478bd9Sstevel@tonic-gate 		 * If we're firing an INITFIRST object, and other objects must
8377c478bd9Sstevel@tonic-gate 		 * be fired which are not INITFIRST, make sure we grab any
8387c478bd9Sstevel@tonic-gate 		 * pending objects that might have been delayed as this
8397c478bd9Sstevel@tonic-gate 		 * INITFIRST was processed.
8407c478bd9Sstevel@tonic-gate 		 */
8417c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_INITFIRST) &&
8427c478bd9Sstevel@tonic-gate 		    ((*_nobj == NULL) || !(FLAGS(*_nobj) & FLG_RT_INITFRST))) {
84357ef7aa9SRod Evans 			Aliste	idx;
84457ef7aa9SRod Evans 			Rt_map	**pobj;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_INITFIRST;
8477c478bd9Sstevel@tonic-gate 
84857ef7aa9SRod Evans 			for (APLIST_TRAVERSE(pending, idx, pobj)) {
84957ef7aa9SRod Evans 				aplist_delete(pending, &idx);
8507c478bd9Sstevel@tonic-gate 				call_init(pobj, DBG_INIT_PEND);
8517c478bd9Sstevel@tonic-gate 			}
8527c478bd9Sstevel@tonic-gate 		}
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	free(tobj);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate  * Function called by atexit(3C).  Calls all .fini sections related with the
8597c478bd9Sstevel@tonic-gate  * mains dependent shared libraries in the order in which the shared libraries
8607c478bd9Sstevel@tonic-gate  * have been loaded.  Skip any .fini defined in the main executable, as this
8617c478bd9Sstevel@tonic-gate  * will be called by crt0 (main was never marked as initdone).
8627c478bd9Sstevel@tonic-gate  */
8637c478bd9Sstevel@tonic-gate void
8647c478bd9Sstevel@tonic-gate call_fini(Lm_list * lml, Rt_map ** tobj)
8657c478bd9Sstevel@tonic-gate {
866dffec89cSrie 	Rt_map **_tobj;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	for (_tobj = tobj; *_tobj != NULL; _tobj++) {
869cce0e03bSab 		Rt_map		*clmp, * lmp = *_tobj;
870cce0e03bSab 		Aliste		idx;
871cce0e03bSab 		Bnd_desc	*bdp;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		/*
87456deab07SRod Evans 		 * Only fire a .fini if the objects corresponding .init has
87556deab07SRod Evans 		 * completed.  We collect all .fini sections of objects that
87656deab07SRod Evans 		 * had their .init collected, but that doesn't mean that at
87756deab07SRod Evans 		 * the time of collection, that the .init had completed.
8787c478bd9Sstevel@tonic-gate 		 */
87956deab07SRod Evans 		if (FLAGS(lmp) & FLG_RT_INITDONE) {
88010a4fa49Srie 			void	(*fptr)(void) = FINI(lmp);
8817c478bd9Sstevel@tonic-gate 
88256deab07SRod Evans 			if (FINIARRAY(lmp) || fptr)
8835aefb655Srie 				DBG_CALL(Dbg_util_call_fini(lmp));
8847c478bd9Sstevel@tonic-gate 
8855aefb655Srie 			call_array(FINIARRAY(lmp), FINIARRAYSZ(lmp), lmp,
8865aefb655Srie 			    SHT_FINI_ARRAY);
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 			if (fptr) {
889eccf73c8Srie 				leave(LIST(lmp), 0);
8907c478bd9Sstevel@tonic-gate 				(*fptr)();
891eccf73c8Srie 				(void) enter(0);
8927c478bd9Sstevel@tonic-gate 			}
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 
895dffec89cSrie 		/*
896dffec89cSrie 		 * Skip main, this is explicitly called last in atexit_fini().
897dffec89cSrie 		 */
898dffec89cSrie 		if (FLAGS(lmp) & FLG_RT_ISMAIN)
899dffec89cSrie 			continue;
900dffec89cSrie 
9017c478bd9Sstevel@tonic-gate 		/*
9027c478bd9Sstevel@tonic-gate 		 * Audit `close' operations at this point.  The library has
9037c478bd9Sstevel@tonic-gate 		 * exercised its last instructions (regardless of whether it
9047c478bd9Sstevel@tonic-gate 		 * will be unmapped or not).
9057c478bd9Sstevel@tonic-gate 		 *
9067c478bd9Sstevel@tonic-gate 		 * First call any global auditing.
9077c478bd9Sstevel@tonic-gate 		 */
9087c478bd9Sstevel@tonic-gate 		if (lml->lm_tflags & LML_TFLG_AUD_OBJCLOSE)
90957ef7aa9SRod Evans 			_audit_objclose(auditors->ad_list, lmp);
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		/*
9127c478bd9Sstevel@tonic-gate 		 * Finally determine whether this object has local auditing
9137c478bd9Sstevel@tonic-gate 		 * requirements by inspecting itself and then its dependencies.
9147c478bd9Sstevel@tonic-gate 		 */
9157c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_LOCAUDIT) == 0)
9167c478bd9Sstevel@tonic-gate 			continue;
9177c478bd9Sstevel@tonic-gate 
91856deab07SRod Evans 		if (AFLAGS(lmp) & LML_TFLG_AUD_OBJCLOSE)
91957ef7aa9SRod Evans 			_audit_objclose(AUDITORS(lmp)->ad_list, lmp);
9207c478bd9Sstevel@tonic-gate 
921cce0e03bSab 		for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
9227c478bd9Sstevel@tonic-gate 			clmp = bdp->b_caller;
9237c478bd9Sstevel@tonic-gate 
92456deab07SRod Evans 			if (AFLAGS(clmp) & LML_TFLG_AUD_OBJCLOSE) {
92557ef7aa9SRod Evans 				_audit_objclose(AUDITORS(clmp)->ad_list, lmp);
926a953e2b1Srie 				break;
9277c478bd9Sstevel@tonic-gate 			}
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 	}
9305aefb655Srie 	DBG_CALL(Dbg_bind_plt_summary(lml, M_MACH, pltcnt21d, pltcnt24d,
9315aefb655Srie 	    pltcntu32, pltcntu44, pltcntfull, pltcntfar));
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	free(tobj);
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate void
9377c478bd9Sstevel@tonic-gate atexit_fini()
9387c478bd9Sstevel@tonic-gate {
93957ef7aa9SRod Evans 	Rt_map	**tobj, *lmp;
94057ef7aa9SRod Evans 	Lm_list	*lml;
94157ef7aa9SRod Evans 	Aliste	idx;
9427c478bd9Sstevel@tonic-gate 
9438cd45542Sraf 	(void) enter(0);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	rtld_flags |= RT_FL_ATEXIT;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	lml = &lml_main;
948dffec89cSrie 	lml->lm_flags |= LML_FLG_ATEXIT;
949883c6d49Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
9507c478bd9Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	/*
9537c478bd9Sstevel@tonic-gate 	 * Reverse topologically sort the main link-map for .fini execution.
9547c478bd9Sstevel@tonic-gate 	 */
95543d7826aSRod Evans 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != NULL) &&
9567c478bd9Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
9577c478bd9Sstevel@tonic-gate 		call_fini(lml, tobj);
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	/*
960dffec89cSrie 	 * Add an explicit close to main and ld.so.1.  Although main's .fini is
961dffec89cSrie 	 * collected in call_fini() to provide for FINITARRAY processing, its
962dffec89cSrie 	 * audit_objclose is explicitly skipped.  This provides for it to be
963dffec89cSrie 	 * called last, here.  This is the reverse of the explicit calls to
964dffec89cSrie 	 * audit_objopen() made in setup().
9657c478bd9Sstevel@tonic-gate 	 */
96656deab07SRod Evans 	if ((lml->lm_tflags | AFLAGS(lmp)) & LML_TFLG_AUD_MASK) {
9677c478bd9Sstevel@tonic-gate 		audit_objclose(lmp, (Rt_map *)lml_rtld.lm_head);
968dffec89cSrie 		audit_objclose(lmp, lmp);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 	/*
9727c478bd9Sstevel@tonic-gate 	 * Now that all .fini code has been run, see what unreferenced objects
9739aa23310Srie 	 * remain.
9747c478bd9Sstevel@tonic-gate 	 */
9757c478bd9Sstevel@tonic-gate 	unused(lml);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	/*
9787c478bd9Sstevel@tonic-gate 	 * Traverse any alternative link-map lists.
9797c478bd9Sstevel@tonic-gate 	 */
98057ef7aa9SRod Evans 	for (APLIST_TRAVERSE(dynlm_list, idx, lml)) {
98110a4fa49Srie 		/*
98210a4fa49Srie 		 * Ignore the base-link-map list, which has already been
98310a4fa49Srie 		 * processed, and the runtime linkers link-map list, which is
98410a4fa49Srie 		 * typically processed last.
98510a4fa49Srie 		 */
9867c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM))
9877c478bd9Sstevel@tonic-gate 			continue;
9887c478bd9Sstevel@tonic-gate 
98943d7826aSRod Evans 		if ((lmp = (Rt_map *)lml->lm_head) == NULL)
9907c478bd9Sstevel@tonic-gate 			continue;
9917c478bd9Sstevel@tonic-gate 
992dffec89cSrie 		lml->lm_flags |= LML_FLG_ATEXIT;
993883c6d49Srie 		lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
994dffec89cSrie 
9957c478bd9Sstevel@tonic-gate 		/*
9967c478bd9Sstevel@tonic-gate 		 * Reverse topologically sort the link-map for .fini execution.
9977c478bd9Sstevel@tonic-gate 		 */
99843d7826aSRod Evans 		if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != NULL) &&
9997c478bd9Sstevel@tonic-gate 		    (tobj != (Rt_map **)S_ERROR))
10007c478bd9Sstevel@tonic-gate 			call_fini(lml, tobj);
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		unused(lml);
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Finally reverse topologically sort the runtime linkers link-map for
10077c478bd9Sstevel@tonic-gate 	 * .fini execution.
10087c478bd9Sstevel@tonic-gate 	 */
10097c478bd9Sstevel@tonic-gate 	lml = &lml_rtld;
1010dffec89cSrie 	lml->lm_flags |= LML_FLG_ATEXIT;
1011883c6d49Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
10127c478bd9Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
10137c478bd9Sstevel@tonic-gate 
101443d7826aSRod Evans 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != NULL) &&
10157c478bd9Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
10167c478bd9Sstevel@tonic-gate 		call_fini(lml, tobj);
10177c478bd9Sstevel@tonic-gate 
10188cd45542Sraf 	leave(&lml_main, 0);
10197c478bd9Sstevel@tonic-gate }
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate /*
10237c478bd9Sstevel@tonic-gate  * This routine is called to complete any runtime linker activity which may have
10247c478bd9Sstevel@tonic-gate  * resulted in objects being loaded.  This is called from all user entry points
10257c478bd9Sstevel@tonic-gate  * and from any internal dl*() requests.
10267c478bd9Sstevel@tonic-gate  */
10277c478bd9Sstevel@tonic-gate void
10287247f888Srie load_completion(Rt_map *nlmp)
10297c478bd9Sstevel@tonic-gate {
103043d7826aSRod Evans 	Rt_map	**tobj = NULL;
10317247f888Srie 	Lm_list	*nlml;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	/*
10347c478bd9Sstevel@tonic-gate 	 * Establish any .init processing.  Note, in a world of lazy loading,
10357c478bd9Sstevel@tonic-gate 	 * objects may have been loaded regardless of whether the users request
10367c478bd9Sstevel@tonic-gate 	 * was fulfilled (i.e., a dlsym() request may have failed to find a
10377c478bd9Sstevel@tonic-gate 	 * symbol but objects might have been loaded during its search).  Thus,
10387c478bd9Sstevel@tonic-gate 	 * any tsorting starts from the nlmp (new link-maps) pointer and not
10397c478bd9Sstevel@tonic-gate 	 * necessarily from the link-map that may have satisfied the request.
10407c478bd9Sstevel@tonic-gate 	 *
104110a4fa49Srie 	 * Note, the primary link-map has an initialization phase where dynamic
104210a4fa49Srie 	 * .init firing is suppressed.  This provides for a simple and clean
104310a4fa49Srie 	 * handshake with the primary link-maps libc, which is important for
104410a4fa49Srie 	 * establishing uberdata.  In addition, auditors often obtain handles
104510a4fa49Srie 	 * to primary link-map objects as the objects are loaded, so as to
104610a4fa49Srie 	 * inspect the link-map for symbols.  This inspection is allowed without
104710a4fa49Srie 	 * running any code on the primary link-map, as running this code may
104810a4fa49Srie 	 * reenter the auditor, who may not yet have finished its own
10497c478bd9Sstevel@tonic-gate 	 * initialization.
10507c478bd9Sstevel@tonic-gate 	 */
105110a4fa49Srie 	if (nlmp)
105210a4fa49Srie 		nlml = LIST(nlmp);
105310a4fa49Srie 
1054b71d513aSedp 	if (nlmp && nlml->lm_init && ((nlml != &lml_main) ||
1055b71d513aSedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
105656deab07SRod Evans 		if ((tobj = tsort(nlmp, nlml->lm_init,
1057dffec89cSrie 		    RT_SORT_REV)) == (Rt_map **)S_ERROR)
105843d7826aSRod Evans 			tobj = NULL;
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
106110a4fa49Srie 	/*
106210a4fa49Srie 	 * Make sure any alternative link-map retrieves any external interfaces
106310a4fa49Srie 	 * and initializes threads.
106410a4fa49Srie 	 */
106510a4fa49Srie 	if (nlmp && (nlml != &lml_main)) {
106610a4fa49Srie 		(void) rt_get_extern(nlml, nlmp);
106710a4fa49Srie 		rt_thr_init(nlml);
106810a4fa49Srie 	}
106910a4fa49Srie 
107010a4fa49Srie 	/*
107110a4fa49Srie 	 * Traverse the list of new link-maps and register any dynamic TLS.
107210a4fa49Srie 	 * This storage is established for any objects not on the primary
107310a4fa49Srie 	 * link-map, and for any objects added to the primary link-map after
107410a4fa49Srie 	 * static TLS has been registered.
107510a4fa49Srie 	 */
1076b71d513aSedp 	if (nlmp && nlml->lm_tls && ((nlml != &lml_main) ||
1077b71d513aSedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
107810a4fa49Srie 		Rt_map	*lmp;
107910a4fa49Srie 
1080cb511613SAli Bahrami 		for (lmp = nlmp; lmp; lmp = NEXT_RT_MAP(lmp)) {
108110a4fa49Srie 			if (PTTLS(lmp) && PTTLS(lmp)->p_memsz)
108210a4fa49Srie 				tls_modaddrem(lmp, TM_FLG_MODADD);
108310a4fa49Srie 		}
108410a4fa49Srie 		nlml->lm_tls = 0;
108510a4fa49Srie 	}
108610a4fa49Srie 
10877c478bd9Sstevel@tonic-gate 	/*
10887c478bd9Sstevel@tonic-gate 	 * Fire any .init's.
10897c478bd9Sstevel@tonic-gate 	 */
10907c478bd9Sstevel@tonic-gate 	if (tobj)
10917c478bd9Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_SORT);
10927c478bd9Sstevel@tonic-gate }
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate /*
10957c478bd9Sstevel@tonic-gate  * Append an item to the specified link map control list.
10967c478bd9Sstevel@tonic-gate  */
10977c478bd9Sstevel@tonic-gate void
10987c478bd9Sstevel@tonic-gate lm_append(Lm_list *lml, Aliste lmco, Rt_map *lmp)
10997c478bd9Sstevel@tonic-gate {
11007c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
11017c478bd9Sstevel@tonic-gate 	int	add = 1;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	/*
11047c478bd9Sstevel@tonic-gate 	 * Indicate that this link-map list has a new object.
11057c478bd9Sstevel@tonic-gate 	 */
11067c478bd9Sstevel@tonic-gate 	(lml->lm_obj)++;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/*
110912b8e62eSrie 	 * If we're about to add a new object to the main link-map control list,
111012b8e62eSrie 	 * alert the debuggers that we are about to mess with this list.
111112b8e62eSrie 	 * Additions of individual objects to the main link-map control list
111212b8e62eSrie 	 * occur during initial setup as the applications immediate dependencies
111312b8e62eSrie 	 * are loaded.  Individual objects are also loaded on the main link-map
111412b8e62eSrie 	 * control list of new alternative link-map control lists.
11157c478bd9Sstevel@tonic-gate 	 */
1116cce0e03bSab 	if ((lmco == ALIST_OFF_DATA) &&
1117cce0e03bSab 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
111812b8e62eSrie 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	/* LINTED */
1121cce0e03bSab 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, lmco);
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	/*
11247c478bd9Sstevel@tonic-gate 	 * A link-map list header points to one of more link-map control lists
11257c478bd9Sstevel@tonic-gate 	 * (see include/rtld.h).  The initial list, pointed to by lm_cntl, is
11267c478bd9Sstevel@tonic-gate 	 * the list of relocated objects.  Other lists maintain objects that
11277c478bd9Sstevel@tonic-gate 	 * are still being analyzed or relocated.  This list provides the core
11287c478bd9Sstevel@tonic-gate 	 * link-map list information used by all ld.so.1 routines.
11297c478bd9Sstevel@tonic-gate 	 */
11307c478bd9Sstevel@tonic-gate 	if (lmc->lc_head == NULL) {
11317c478bd9Sstevel@tonic-gate 		/*
11327c478bd9Sstevel@tonic-gate 		 * If this is the first link-map for the given control list,
11337c478bd9Sstevel@tonic-gate 		 * initialize the list.
11347c478bd9Sstevel@tonic-gate 		 */
11357c478bd9Sstevel@tonic-gate 		lmc->lc_head = lmc->lc_tail = lmp;
11367c478bd9Sstevel@tonic-gate 		add = 0;
11377c478bd9Sstevel@tonic-gate 
11389a411307Srie 	} else if (FLAGS(lmp) & FLG_RT_OBJINTPO) {
11397c478bd9Sstevel@tonic-gate 		Rt_map	*tlmp;
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		/*
11427c478bd9Sstevel@tonic-gate 		 * If this is an interposer then append the link-map following
11437c478bd9Sstevel@tonic-gate 		 * any other interposers (these are objects that have been
11447c478bd9Sstevel@tonic-gate 		 * previously preloaded, or were identified with -z interpose).
11457c478bd9Sstevel@tonic-gate 		 * Interposers can only be inserted on the first link-map
11467c478bd9Sstevel@tonic-gate 		 * control list, as once relocation has started, interposition
11477c478bd9Sstevel@tonic-gate 		 * from new interposers can't be guaranteed.
11487c478bd9Sstevel@tonic-gate 		 *
11497c478bd9Sstevel@tonic-gate 		 * NOTE: We do not interpose on the head of a list.  This model
11507c478bd9Sstevel@tonic-gate 		 * evolved because dynamic executables have already been fully
11517c478bd9Sstevel@tonic-gate 		 * relocated within themselves and thus can't be interposed on.
11527c478bd9Sstevel@tonic-gate 		 * Nowadays it's possible to have shared objects at the head of
11537c478bd9Sstevel@tonic-gate 		 * a list, which conceptually means they could be interposed on.
11547c478bd9Sstevel@tonic-gate 		 * But, shared objects can be created via dldump() and may only
11557c478bd9Sstevel@tonic-gate 		 * be partially relocated (just relatives), in which case they
11567c478bd9Sstevel@tonic-gate 		 * are interposable, but are marked as fixed (ET_EXEC).
11577c478bd9Sstevel@tonic-gate 		 *
11587c478bd9Sstevel@tonic-gate 		 * Thus we really don't have a clear method of deciding when the
11597c478bd9Sstevel@tonic-gate 		 * head of a link-map is interposable.  So, to be consistent,
11607c478bd9Sstevel@tonic-gate 		 * for now only add interposers after the link-map lists head
11617c478bd9Sstevel@tonic-gate 		 * object.
11627c478bd9Sstevel@tonic-gate 		 */
1163cb511613SAli Bahrami 		for (tlmp = NEXT_RT_MAP(lmc->lc_head); tlmp;
1164cb511613SAli Bahrami 		    tlmp = NEXT_RT_MAP(tlmp)) {
11657c478bd9Sstevel@tonic-gate 
11669a411307Srie 			if (FLAGS(tlmp) & FLG_RT_OBJINTPO)
11677c478bd9Sstevel@tonic-gate 				continue;
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 			/*
11707c478bd9Sstevel@tonic-gate 			 * Insert the new link-map before this non-interposer,
11717c478bd9Sstevel@tonic-gate 			 * and indicate an interposer is found.
11727c478bd9Sstevel@tonic-gate 			 */
1173cb511613SAli Bahrami 			NEXT(PREV_RT_MAP(tlmp)) = (Link_map *)lmp;
11747c478bd9Sstevel@tonic-gate 			PREV(lmp) = PREV(tlmp);
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 			NEXT(lmp) = (Link_map *)tlmp;
11777c478bd9Sstevel@tonic-gate 			PREV(tlmp) = (Link_map *)lmp;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 			lmc->lc_flags |= LMC_FLG_REANALYZE;
11807c478bd9Sstevel@tonic-gate 			add = 0;
11817c478bd9Sstevel@tonic-gate 			break;
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 	/*
11867c478bd9Sstevel@tonic-gate 	 * Fall through to appending the new link map to the tail of the list.
11877c478bd9Sstevel@tonic-gate 	 * If we're processing the initial objects of this link-map list, add
11887c478bd9Sstevel@tonic-gate 	 * them to the backward compatibility list.
11897c478bd9Sstevel@tonic-gate 	 */
11907c478bd9Sstevel@tonic-gate 	if (add) {
11917c478bd9Sstevel@tonic-gate 		NEXT(lmc->lc_tail) = (Link_map *)lmp;
11927c478bd9Sstevel@tonic-gate 		PREV(lmp) = (Link_map *)lmc->lc_tail;
11937c478bd9Sstevel@tonic-gate 		lmc->lc_tail = lmp;
11947c478bd9Sstevel@tonic-gate 	}
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * Having added this link-map to a control list, indicate which control
11987c478bd9Sstevel@tonic-gate 	 * list the link-map belongs to.  Note, control list information is
11997c478bd9Sstevel@tonic-gate 	 * always maintained as an offset, as the Alist can be reallocated.
12007c478bd9Sstevel@tonic-gate 	 */
12017c478bd9Sstevel@tonic-gate 	CNTL(lmp) = lmco;
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate 	/*
12047c478bd9Sstevel@tonic-gate 	 * Indicate if an interposer is found.  Note that the first object on a
12057c478bd9Sstevel@tonic-gate 	 * link-map can be explicitly defined as an interposer so that it can
12067c478bd9Sstevel@tonic-gate 	 * provide interposition over direct binding requests.
12077c478bd9Sstevel@tonic-gate 	 */
12089a411307Srie 	if (FLAGS(lmp) & MSK_RT_INTPOSE)
12097c478bd9Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_INTRPOSE;
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate 	/*
12127c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
12137c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
12147c478bd9Sstevel@tonic-gate 	 */
1215cce0e03bSab 	if (lmco == ALIST_OFF_DATA) {
12167c478bd9Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12177c478bd9Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12187c478bd9Sstevel@tonic-gate 	}
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate /*
12227c478bd9Sstevel@tonic-gate  * Delete an item from the specified link map control list.
12237c478bd9Sstevel@tonic-gate  */
12247c478bd9Sstevel@tonic-gate void
12257c478bd9Sstevel@tonic-gate lm_delete(Lm_list *lml, Rt_map *lmp)
12267c478bd9Sstevel@tonic-gate {
12277c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
12287c478bd9Sstevel@tonic-gate 
12297c478bd9Sstevel@tonic-gate 	/*
12307c478bd9Sstevel@tonic-gate 	 * If the control list pointer hasn't been initialized, this object
12317c478bd9Sstevel@tonic-gate 	 * never got added to a link-map list.
12327c478bd9Sstevel@tonic-gate 	 */
12337c478bd9Sstevel@tonic-gate 	if (CNTL(lmp) == 0)
12347c478bd9Sstevel@tonic-gate 		return;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
123712b8e62eSrie 	 * If we're about to delete an object from the main link-map control
123812b8e62eSrie 	 * list, alert the debuggers that we are about to mess with this list.
12397c478bd9Sstevel@tonic-gate 	 */
1240cce0e03bSab 	if ((CNTL(lmp) == ALIST_OFF_DATA) &&
1241cce0e03bSab 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
12427c478bd9Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_DELETE);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	/* LINTED */
1245cce0e03bSab 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(lmp));
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	if (lmc->lc_head == lmp)
1248cb511613SAli Bahrami 		lmc->lc_head = NEXT_RT_MAP(lmp);
12497c478bd9Sstevel@tonic-gate 	else
1250cb511613SAli Bahrami 		NEXT(PREV_RT_MAP(lmp)) = (void *)NEXT(lmp);
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (lmc->lc_tail == lmp)
1253cb511613SAli Bahrami 		lmc->lc_tail = PREV_RT_MAP(lmp);
12547c478bd9Sstevel@tonic-gate 	else
1255cb511613SAli Bahrami 		PREV(NEXT_RT_MAP(lmp)) = PREV(lmp);
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 	/*
12587c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
12597c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
12607c478bd9Sstevel@tonic-gate 	 */
1261cce0e03bSab 	if (lmc == (Lm_cntl *)&lml->lm_lists->al_data) {
12627c478bd9Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12637c478bd9Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	/*
12677c478bd9Sstevel@tonic-gate 	 * Indicate we have one less object on this control list.
12687c478bd9Sstevel@tonic-gate 	 */
12697c478bd9Sstevel@tonic-gate 	(lml->lm_obj)--;
12707c478bd9Sstevel@tonic-gate }
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate /*
12737c478bd9Sstevel@tonic-gate  * Move a link-map control list to another.  Objects that are being relocated
12747c478bd9Sstevel@tonic-gate  * are maintained on secondary control lists.  Once their relocation is
12757c478bd9Sstevel@tonic-gate  * complete, the entire list is appended to the previous control list, as this
12767c478bd9Sstevel@tonic-gate  * list must have been the trigger for generating the new control list.
12777c478bd9Sstevel@tonic-gate  */
12787c478bd9Sstevel@tonic-gate void
12797c478bd9Sstevel@tonic-gate lm_move(Lm_list *lml, Aliste nlmco, Aliste plmco, Lm_cntl *nlmc, Lm_cntl *plmc)
12807c478bd9Sstevel@tonic-gate {
12817c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate 	/*
128412b8e62eSrie 	 * If we're about to add a new family of objects to the main link-map
128512b8e62eSrie 	 * control list, alert the debuggers that we are about to mess with this
128612b8e62eSrie 	 * list.  Additions of object families to the main link-map control
128712b8e62eSrie 	 * list occur during lazy loading, filtering and dlopen().
12887c478bd9Sstevel@tonic-gate 	 */
1289cce0e03bSab 	if ((plmco == ALIST_OFF_DATA) &&
1290cce0e03bSab 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
12917c478bd9Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
12927c478bd9Sstevel@tonic-gate 
129312b8e62eSrie 	DBG_CALL(Dbg_file_cntl(lml, nlmco, plmco));
129412b8e62eSrie 
12957c478bd9Sstevel@tonic-gate 	/*
12967c478bd9Sstevel@tonic-gate 	 * Indicate each new link-map has been moved to the previous link-map
12977c478bd9Sstevel@tonic-gate 	 * control list.
12987c478bd9Sstevel@tonic-gate 	 */
129956deab07SRod Evans 	for (lmp = nlmc->lc_head; lmp; lmp = NEXT_RT_MAP(lmp)) {
13007c478bd9Sstevel@tonic-gate 		CNTL(lmp) = plmco;
13017c478bd9Sstevel@tonic-gate 
130256deab07SRod Evans 		/*
130356deab07SRod Evans 		 * If these objects are being added to the main link-map
130456deab07SRod Evans 		 * control list, indicate that there are init's available
130556deab07SRod Evans 		 * for harvesting.
130656deab07SRod Evans 		 */
130756deab07SRod Evans 		if (plmco == ALIST_OFF_DATA) {
130856deab07SRod Evans 			lml->lm_init++;
130956deab07SRod Evans 			lml->lm_flags |= LML_FLG_OBJADDED;
131056deab07SRod Evans 		}
131156deab07SRod Evans 	}
131256deab07SRod Evans 
13137c478bd9Sstevel@tonic-gate 	/*
13147c478bd9Sstevel@tonic-gate 	 * Move the new link-map control list, to the callers link-map control
13157c478bd9Sstevel@tonic-gate 	 * list.
13167c478bd9Sstevel@tonic-gate 	 */
13173dbfc803SRod Evans 	if (plmc->lc_head == NULL) {
13187c478bd9Sstevel@tonic-gate 		plmc->lc_head = nlmc->lc_head;
13193dbfc803SRod Evans 		PREV(nlmc->lc_head) = NULL;
13207c478bd9Sstevel@tonic-gate 	} else {
13217c478bd9Sstevel@tonic-gate 		NEXT(plmc->lc_tail) = (Link_map *)nlmc->lc_head;
13227c478bd9Sstevel@tonic-gate 		PREV(nlmc->lc_head) = (Link_map *)plmc->lc_tail;
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	plmc->lc_tail = nlmc->lc_tail;
13263dbfc803SRod Evans 	nlmc->lc_head = nlmc->lc_tail = NULL;
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate 	/*
13297c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
13307c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
13317c478bd9Sstevel@tonic-gate 	 */
1332cce0e03bSab 	if (plmco == ALIST_OFF_DATA) {
13337c478bd9Sstevel@tonic-gate 		lml->lm_head = plmc->lc_head;
13347c478bd9Sstevel@tonic-gate 		lml->lm_tail = plmc->lc_tail;
13357c478bd9Sstevel@tonic-gate 	}
13367c478bd9Sstevel@tonic-gate }
13377c478bd9Sstevel@tonic-gate 
1338dde769a2SRod Evans /*
1339dde769a2SRod Evans  * Create, or assign a link-map control list.  Each link-map list contains a
1340dde769a2SRod Evans  * main control list, which has an Alist offset of ALIST_OFF_DATA (see the
1341dde769a2SRod Evans  * description in include/rtld.h).  During the initial construction of a
1342dde769a2SRod Evans  * process, objects are added to this main control list.  This control list is
1343dde769a2SRod Evans  * never deleted, unless an alternate link-map list has been requested (say for
1344dde769a2SRod Evans  * auditors), and the associated objects could not be loaded or relocated.
1345dde769a2SRod Evans  *
1346dde769a2SRod Evans  * Once relocation has started, any lazy loadable objects, or filtees, are
1347dde769a2SRod Evans  * processed on a new, temporary control list.  Only when these objects have
1348dde769a2SRod Evans  * been fully relocated, are they moved to the main link-map control list.
1349dde769a2SRod Evans  * Once the objects are moved, this temporary control list is deleted (see
1350dde769a2SRod Evans  * remove_cntl()).
1351dde769a2SRod Evans  *
1352dde769a2SRod Evans  * A dlopen() always requires a new temporary link-map control list.
1353dde769a2SRod Evans  * Typically, a dlopen() occurs on a link-map list that had already started
1354dde769a2SRod Evans  * relocation, however, auditors can dlopen() objects on the main link-map
1355dde769a2SRod Evans  * list while under initial construction, before any relocation has begun.
1356dde769a2SRod Evans  * Hence, dlopen() requests are explicitly flagged.
1357dde769a2SRod Evans  */
1358dde769a2SRod Evans Aliste
1359dde769a2SRod Evans create_cntl(Lm_list *lml, int dlopen)
1360dde769a2SRod Evans {
1361dde769a2SRod Evans 	/*
1362dde769a2SRod Evans 	 * If the head link-map object has already been relocated, create a
1363dde769a2SRod Evans 	 * new, temporary, control list.
1364dde769a2SRod Evans 	 */
1365dde769a2SRod Evans 	if (dlopen || (lml->lm_head == NULL) ||
1366dde769a2SRod Evans 	    (FLAGS(lml->lm_head) & FLG_RT_RELOCED)) {
1367dde769a2SRod Evans 		Lm_cntl *lmc;
1368dde769a2SRod Evans 
1369dde769a2SRod Evans 		if ((lmc = alist_append(&lml->lm_lists, NULL, sizeof (Lm_cntl),
1370dde769a2SRod Evans 		    AL_CNT_LMLISTS)) == NULL)
1371dde769a2SRod Evans 			return (NULL);
1372dde769a2SRod Evans 
1373dde769a2SRod Evans 		return ((Aliste)((char *)lmc - (char *)lml->lm_lists));
1374dde769a2SRod Evans 	}
1375dde769a2SRod Evans 
1376dde769a2SRod Evans 	return (ALIST_OFF_DATA);
1377dde769a2SRod Evans }
1378dde769a2SRod Evans 
13797c478bd9Sstevel@tonic-gate /*
13807c478bd9Sstevel@tonic-gate  * Environment variables can have a variety of defined permutations, and thus
13817c478bd9Sstevel@tonic-gate  * the following infrastructure exists to allow this variety and to select the
13827c478bd9Sstevel@tonic-gate  * required definition.
13837c478bd9Sstevel@tonic-gate  *
13847c478bd9Sstevel@tonic-gate  * Environment variables can be defined as 32- or 64-bit specific, and if so
13857c478bd9Sstevel@tonic-gate  * they will take precedence over any instruction set neutral form.  Typically
13867c478bd9Sstevel@tonic-gate  * this is only useful when the environment value is an informational string.
13877c478bd9Sstevel@tonic-gate  *
13887c478bd9Sstevel@tonic-gate  * Environment variables may be obtained from the standard user environment or
13897c478bd9Sstevel@tonic-gate  * from a configuration file.  The latter provides a fallback if no user
13907c478bd9Sstevel@tonic-gate  * environment setting is found, and can take two forms:
13917c478bd9Sstevel@tonic-gate  *
139243d7826aSRod Evans  *  -	a replaceable definition - this will be used if no user environment
13937c478bd9Sstevel@tonic-gate  *	setting has been seen, or
13947c478bd9Sstevel@tonic-gate  *
139543d7826aSRod Evans  *  -	an permanent definition - this will be used no matter what user
13967c478bd9Sstevel@tonic-gate  *	environment setting is seen.  In the case of list variables it will be
13977c478bd9Sstevel@tonic-gate  *	appended to any process environment setting seen.
13987c478bd9Sstevel@tonic-gate  *
13997c478bd9Sstevel@tonic-gate  * Environment variables can be defined without a value (ie. LD_XXXX=) so as to
14007c478bd9Sstevel@tonic-gate  * override any replaceable environment variables from a configuration file.
14017c478bd9Sstevel@tonic-gate  */
14027c478bd9Sstevel@tonic-gate static	u_longlong_t		rplgen;		/* replaceable generic */
14037c478bd9Sstevel@tonic-gate 						/*	variables */
14047c478bd9Sstevel@tonic-gate static	u_longlong_t		rplisa;		/* replaceable ISA specific */
14057c478bd9Sstevel@tonic-gate 						/*	variables */
14067c478bd9Sstevel@tonic-gate static	u_longlong_t		prmgen;		/* permanent generic */
14077c478bd9Sstevel@tonic-gate 						/*	variables */
14087c478bd9Sstevel@tonic-gate static	u_longlong_t		prmisa;		/* permanent ISA specific */
14097c478bd9Sstevel@tonic-gate 						/*	variables */
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate /*
14127c478bd9Sstevel@tonic-gate  * Classify an environment variables type.
14137c478bd9Sstevel@tonic-gate  */
14147c478bd9Sstevel@tonic-gate #define	ENV_TYP_IGNORE		0x1		/* ignore - variable is for */
14157c478bd9Sstevel@tonic-gate 						/*	the wrong ISA */
14167c478bd9Sstevel@tonic-gate #define	ENV_TYP_ISA		0x2		/* variable is ISA specific */
14177c478bd9Sstevel@tonic-gate #define	ENV_TYP_CONFIG		0x4		/* variable obtained from a */
14187c478bd9Sstevel@tonic-gate 						/*	config file */
14197c478bd9Sstevel@tonic-gate #define	ENV_TYP_PERMANT		0x8		/* variable is permanent */
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate /*
14227c478bd9Sstevel@tonic-gate  * Identify all environment variables.
14237c478bd9Sstevel@tonic-gate  */
142408278a5eSRod Evans #define	ENV_FLG_AUDIT		0x0000000000001ULL
142508278a5eSRod Evans #define	ENV_FLG_AUDIT_ARGS	0x0000000000002ULL
142608278a5eSRod Evans #define	ENV_FLG_BIND_NOW	0x0000000000004ULL
142708278a5eSRod Evans #define	ENV_FLG_BIND_NOT	0x0000000000008ULL
142808278a5eSRod Evans #define	ENV_FLG_BINDINGS	0x0000000000010ULL
142908278a5eSRod Evans #define	ENV_FLG_CONFGEN		0x0000000000020ULL
143008278a5eSRod Evans #define	ENV_FLG_CONFIG		0x0000000000040ULL
143108278a5eSRod Evans #define	ENV_FLG_DEBUG		0x0000000000080ULL
143208278a5eSRod Evans #define	ENV_FLG_DEBUG_OUTPUT	0x0000000000100ULL
143308278a5eSRod Evans #define	ENV_FLG_DEMANGLE	0x0000000000200ULL
143408278a5eSRod Evans #define	ENV_FLG_FLAGS		0x0000000000400ULL
143508278a5eSRod Evans #define	ENV_FLG_INIT		0x0000000000800ULL
143608278a5eSRod Evans #define	ENV_FLG_LIBPATH		0x0000000001000ULL
143708278a5eSRod Evans #define	ENV_FLG_LOADAVAIL	0x0000000002000ULL
143808278a5eSRod Evans #define	ENV_FLG_LOADFLTR	0x0000000004000ULL
143908278a5eSRod Evans #define	ENV_FLG_NOAUDIT		0x0000000008000ULL
144008278a5eSRod Evans #define	ENV_FLG_NOAUXFLTR	0x0000000010000ULL
144108278a5eSRod Evans #define	ENV_FLG_NOBAPLT		0x0000000020000ULL
144208278a5eSRod Evans #define	ENV_FLG_NOCONFIG	0x0000000040000ULL
144308278a5eSRod Evans #define	ENV_FLG_NODIRCONFIG	0x0000000080000ULL
144408278a5eSRod Evans #define	ENV_FLG_NODIRECT	0x0000000100000ULL
144508278a5eSRod Evans #define	ENV_FLG_NOENVCONFIG	0x0000000200000ULL
144608278a5eSRod Evans #define	ENV_FLG_NOLAZY		0x0000000400000ULL
144708278a5eSRod Evans #define	ENV_FLG_NOOBJALTER	0x0000000800000ULL
144808278a5eSRod Evans #define	ENV_FLG_NOVERSION	0x0000001000000ULL
144908278a5eSRod Evans #define	ENV_FLG_PRELOAD		0x0000002000000ULL
145008278a5eSRod Evans #define	ENV_FLG_PROFILE		0x0000004000000ULL
145108278a5eSRod Evans #define	ENV_FLG_PROFILE_OUTPUT	0x0000008000000ULL
145208278a5eSRod Evans #define	ENV_FLG_SIGNAL		0x0000010000000ULL
145308278a5eSRod Evans #define	ENV_FLG_TRACE_OBJS	0x0000020000000ULL
145408278a5eSRod Evans #define	ENV_FLG_TRACE_PTHS	0x0000040000000ULL
145508278a5eSRod Evans #define	ENV_FLG_UNREF		0x0000080000000ULL
145608278a5eSRod Evans #define	ENV_FLG_UNUSED		0x0000100000000ULL
145708278a5eSRod Evans #define	ENV_FLG_VERBOSE		0x0000200000000ULL
145808278a5eSRod Evans #define	ENV_FLG_WARN		0x0000400000000ULL
145908278a5eSRod Evans #define	ENV_FLG_NOFLTCONFIG	0x0000800000000ULL
146008278a5eSRod Evans #define	ENV_FLG_BIND_LAZY	0x0001000000000ULL
146108278a5eSRod Evans #define	ENV_FLG_NOUNRESWEAK	0x0002000000000ULL
146208278a5eSRod Evans #define	ENV_FLG_NOPAREXT	0x0004000000000ULL
146308278a5eSRod Evans #define	ENV_FLG_HWCAP		0x0008000000000ULL
146408278a5eSRod Evans #define	ENV_FLG_SFCAP		0x0010000000000ULL
146508278a5eSRod Evans #define	ENV_FLG_MACHCAP		0x0020000000000ULL
146608278a5eSRod Evans #define	ENV_FLG_PLATCAP		0x0040000000000ULL
146708278a5eSRod Evans #define	ENV_FLG_CAP_FILES	0x0080000000000ULL
1468*f441771bSRod Evans #define	ENV_FLG_DEFERRED	0x0100000000000ULL
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate #define	SEL_REPLACE		0x0001
14717c478bd9Sstevel@tonic-gate #define	SEL_PERMANT		0x0002
14727c478bd9Sstevel@tonic-gate #define	SEL_ACT_RT		0x0100	/* setting rtld_flags */
14737c478bd9Sstevel@tonic-gate #define	SEL_ACT_RT2		0x0200	/* setting rtld_flags2 */
14747c478bd9Sstevel@tonic-gate #define	SEL_ACT_STR		0x0400	/* setting string value */
14757c478bd9Sstevel@tonic-gate #define	SEL_ACT_LML		0x0800	/* setting lml_flags */
14767c478bd9Sstevel@tonic-gate #define	SEL_ACT_LMLT		0x1000	/* setting lml_tflags */
1477635216b6SRod Evans #define	SEL_ACT_SPEC_1		0x2000	/* for FLG_{FLAGS, LIBPATH} */
14787c478bd9Sstevel@tonic-gate #define	SEL_ACT_SPEC_2		0x4000	/* need special handling */
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate /*
14817c478bd9Sstevel@tonic-gate  * Pattern match an LD_XXXX environment variable.  s1 points to the XXXX part
14827c478bd9Sstevel@tonic-gate  * and len specifies its length (comparing a strings length before the string
14837c478bd9Sstevel@tonic-gate  * itself speed things up).  s2 points to the token itself which has already
14847c478bd9Sstevel@tonic-gate  * had any leading white-space removed.
14857c478bd9Sstevel@tonic-gate  */
14867c478bd9Sstevel@tonic-gate static void
14877c478bd9Sstevel@tonic-gate ld_generic_env(const char *s1, size_t len, const char *s2, Word *lmflags,
14887c478bd9Sstevel@tonic-gate     Word *lmtflags, uint_t env_flags, int aout)
14897c478bd9Sstevel@tonic-gate {
14907c478bd9Sstevel@tonic-gate 	u_longlong_t	variable = 0;
149110a4fa49Srie 	ushort_t	select = 0;
149210a4fa49Srie 	const char	**str;
149310a4fa49Srie 	Word		val = 0;
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	/*
14967c478bd9Sstevel@tonic-gate 	 * Determine whether we're dealing with a replaceable or permanent
14977c478bd9Sstevel@tonic-gate 	 * string.
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	if (env_flags & ENV_TYP_PERMANT) {
15007c478bd9Sstevel@tonic-gate 		/*
15017c478bd9Sstevel@tonic-gate 		 * If the string is from a configuration file and defined as
15027c478bd9Sstevel@tonic-gate 		 * permanent, assign it as permanent.
15037c478bd9Sstevel@tonic-gate 		 */
15047c478bd9Sstevel@tonic-gate 		select |= SEL_PERMANT;
15057c478bd9Sstevel@tonic-gate 	} else
15067c478bd9Sstevel@tonic-gate 		select |= SEL_REPLACE;
15077c478bd9Sstevel@tonic-gate 
15087c478bd9Sstevel@tonic-gate 	/*
15097c478bd9Sstevel@tonic-gate 	 * Parse the variable given.
15107c478bd9Sstevel@tonic-gate 	 *
15117c478bd9Sstevel@tonic-gate 	 * The LD_AUDIT family.
15127c478bd9Sstevel@tonic-gate 	 */
15137c478bd9Sstevel@tonic-gate 	if (*s1 == 'A') {
15147c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_AUDIT_SIZE) && (strncmp(s1,
15157c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_AUDIT), MSG_LD_AUDIT_SIZE) == 0)) {
15167c478bd9Sstevel@tonic-gate 			/*
15177c478bd9Sstevel@tonic-gate 			 * Replaceable and permanent audit objects can exist.
15187c478bd9Sstevel@tonic-gate 			 */
15197c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1520dde769a2SRod Evans 			str = (select & SEL_REPLACE) ? &rpl_audit : &prm_audit;
15217c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT;
15227c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_AUDIT_ARGS_SIZE) &&
15237c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_AUDIT_ARGS),
15247c478bd9Sstevel@tonic-gate 		    MSG_LD_AUDIT_ARGS_SIZE) == 0)) {
15257c478bd9Sstevel@tonic-gate 			/*
15267c478bd9Sstevel@tonic-gate 			 * A specialized variable for plt_exit() use, not
15277c478bd9Sstevel@tonic-gate 			 * documented for general use.
15287c478bd9Sstevel@tonic-gate 			 */
15297c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15307c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT_ARGS;
15317c478bd9Sstevel@tonic-gate 		}
15327c478bd9Sstevel@tonic-gate 	}
15337c478bd9Sstevel@tonic-gate 	/*
153456deab07SRod Evans 	 * The LD_BIND family.
15357c478bd9Sstevel@tonic-gate 	 */
15367c478bd9Sstevel@tonic-gate 	else if (*s1 == 'B') {
1537dffec89cSrie 		if ((len == MSG_LD_BIND_LAZY_SIZE) && (strncmp(s1,
1538dffec89cSrie 		    MSG_ORIG(MSG_LD_BIND_LAZY),
1539dffec89cSrie 		    MSG_LD_BIND_LAZY_SIZE) == 0)) {
1540dffec89cSrie 			select |= SEL_ACT_RT2;
1541dffec89cSrie 			val = RT_FL2_BINDLAZY;
1542dffec89cSrie 			variable = ENV_FLG_BIND_LAZY;
1543dffec89cSrie 		} else if ((len == MSG_LD_BIND_NOW_SIZE) && (strncmp(s1,
15447c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOW), MSG_LD_BIND_NOW_SIZE) == 0)) {
15457c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
15467c478bd9Sstevel@tonic-gate 			val = RT_FL2_BINDNOW;
15477c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOW;
15487c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BIND_NOT_SIZE) && (strncmp(s1,
15497c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOT), MSG_LD_BIND_NOT_SIZE) == 0)) {
15507c478bd9Sstevel@tonic-gate 			/*
15517c478bd9Sstevel@tonic-gate 			 * Another trick, enabled to help debug AOUT
15527c478bd9Sstevel@tonic-gate 			 * applications under BCP, but not documented for
15537c478bd9Sstevel@tonic-gate 			 * general use.
15547c478bd9Sstevel@tonic-gate 			 */
15557c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
15567c478bd9Sstevel@tonic-gate 			val = RT_FL_NOBIND;
15577c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOT;
15587c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BINDINGS_SIZE) && (strncmp(s1,
15597c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BINDINGS), MSG_LD_BINDINGS_SIZE) == 0)) {
15607c478bd9Sstevel@tonic-gate 			/*
15617c478bd9Sstevel@tonic-gate 			 * This variable is simply for backward compatibility.
15627c478bd9Sstevel@tonic-gate 			 * If this and LD_DEBUG are both specified, only one of
15637c478bd9Sstevel@tonic-gate 			 * the strings is going to get processed.
15647c478bd9Sstevel@tonic-gate 			 */
15657c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15667c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BINDINGS;
15677c478bd9Sstevel@tonic-gate 		}
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 	/*
157008278a5eSRod Evans 	 * LD_CAP_FILES and LD_CONFIG family.
15717c478bd9Sstevel@tonic-gate 	 */
15727c478bd9Sstevel@tonic-gate 	else if (*s1 == 'C') {
157308278a5eSRod Evans 		if ((len == MSG_LD_CAP_FILES_SIZE) && (strncmp(s1,
157408278a5eSRod Evans 		    MSG_ORIG(MSG_LD_CAP_FILES), MSG_LD_CAP_FILES_SIZE) == 0)) {
157508278a5eSRod Evans 			select |= SEL_ACT_STR;
157608278a5eSRod Evans 			str = (select & SEL_REPLACE) ?
157708278a5eSRod Evans 			    &rpl_cap_files : &prm_cap_files;
157808278a5eSRod Evans 			variable = ENV_FLG_CAP_FILES;
157908278a5eSRod Evans 		} else if ((len == MSG_LD_CONFGEN_SIZE) && (strncmp(s1,
15807c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFGEN), MSG_LD_CONFGEN_SIZE) == 0)) {
15817c478bd9Sstevel@tonic-gate 			/*
15827c478bd9Sstevel@tonic-gate 			 * Set by crle(1) to indicate it's building a
15837c478bd9Sstevel@tonic-gate 			 * configuration file, not documented for general use.
15847c478bd9Sstevel@tonic-gate 			 */
15857c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15867c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONFGEN;
15877c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFIG_SIZE) && (strncmp(s1,
15887c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFIG), MSG_LD_CONFIG_SIZE) == 0)) {
15897c478bd9Sstevel@tonic-gate 			/*
15907c478bd9Sstevel@tonic-gate 			 * Secure applications must use a default configuration
15917c478bd9Sstevel@tonic-gate 			 * file.  A setting from a configuration file doesn't
15927c478bd9Sstevel@tonic-gate 			 * make sense (given we must be reading a configuration
15937c478bd9Sstevel@tonic-gate 			 * file to have gotten this).
15947c478bd9Sstevel@tonic-gate 			 */
15957c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_SECURE) ||
15967c478bd9Sstevel@tonic-gate 			    (env_flags & ENV_TYP_CONFIG))
15977c478bd9Sstevel@tonic-gate 				return;
15987c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
15997c478bd9Sstevel@tonic-gate 			str = &config->c_name;
16007c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONFIG;
16017c478bd9Sstevel@tonic-gate 		}
16027c478bd9Sstevel@tonic-gate 	}
16037c478bd9Sstevel@tonic-gate 	/*
1604*f441771bSRod Evans 	 * The LD_DEBUG family, LD_DEFERRED (internal, used by ldd(1)), and
1605*f441771bSRod Evans 	 * LD_DEMANGLE.
16067c478bd9Sstevel@tonic-gate 	 */
16077c478bd9Sstevel@tonic-gate 	else if (*s1 == 'D') {
16087c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_DEBUG_SIZE) && (strncmp(s1,
16097c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG), MSG_LD_DEBUG_SIZE) == 0)) {
16107c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1611dde769a2SRod Evans 			str = (select & SEL_REPLACE) ? &rpl_debug : &prm_debug;
16127c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG;
16137c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEBUG_OUTPUT_SIZE) && (strncmp(s1,
16147c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG_OUTPUT),
16157c478bd9Sstevel@tonic-gate 		    MSG_LD_DEBUG_OUTPUT_SIZE) == 0)) {
16167c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
16177c478bd9Sstevel@tonic-gate 			str = &dbg_file;
16187c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG_OUTPUT;
1619*f441771bSRod Evans 		} else if ((len == MSG_LD_DEFERRED_SIZE) && (strncmp(s1,
1620*f441771bSRod Evans 		    MSG_ORIG(MSG_LD_DEFERRED), MSG_LD_DEFERRED_SIZE) == 0)) {
1621*f441771bSRod Evans 			select |= SEL_ACT_RT;
1622*f441771bSRod Evans 			val = RT_FL_DEFERRED;
1623*f441771bSRod Evans 			variable = ENV_FLG_DEFERRED;
16247c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEMANGLE_SIZE) && (strncmp(s1,
16257c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEMANGLE), MSG_LD_DEMANGLE_SIZE) == 0)) {
16267c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16277c478bd9Sstevel@tonic-gate 			val = RT_FL_DEMANGLE;
16287c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEMANGLE;
16297c478bd9Sstevel@tonic-gate 		}
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 	/*
16327c478bd9Sstevel@tonic-gate 	 * LD_FLAGS - collect the best variable definition.  On completion of
16337c478bd9Sstevel@tonic-gate 	 * environment variable processing pass the result to ld_flags_env()
16347c478bd9Sstevel@tonic-gate 	 * where they'll be decomposed and passed back to this routine.
16357c478bd9Sstevel@tonic-gate 	 */
16367c478bd9Sstevel@tonic-gate 	else if (*s1 == 'F') {
16377c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_FLAGS_SIZE) && (strncmp(s1,
16387c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FLAGS), MSG_LD_FLAGS_SIZE) == 0)) {
16397c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1640dde769a2SRod Evans 			str = (select & SEL_REPLACE) ? &rpl_ldflags :
1641dde769a2SRod Evans 			    &prm_ldflags;
16427c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_FLAGS;
16437c478bd9Sstevel@tonic-gate 		}
16447c478bd9Sstevel@tonic-gate 	}
164508278a5eSRod Evans 	/*
164608278a5eSRod Evans 	 * LD_HWCAP.
164708278a5eSRod Evans 	 */
164808278a5eSRod Evans 	else if (*s1 == 'H') {
164908278a5eSRod Evans 		if ((len == MSG_LD_HWCAP_SIZE) && (strncmp(s1,
165008278a5eSRod Evans 		    MSG_ORIG(MSG_LD_HWCAP), MSG_LD_HWCAP_SIZE) == 0)) {
165108278a5eSRod Evans 			select |= SEL_ACT_STR;
165208278a5eSRod Evans 			str = (select & SEL_REPLACE) ?
165308278a5eSRod Evans 			    &rpl_hwcap : &prm_hwcap;
165408278a5eSRod Evans 			variable = ENV_FLG_HWCAP;
165508278a5eSRod Evans 		}
165608278a5eSRod Evans 	}
16577c478bd9Sstevel@tonic-gate 	/*
16587c478bd9Sstevel@tonic-gate 	 * LD_INIT (internal, used by ldd(1)).
16597c478bd9Sstevel@tonic-gate 	 */
16607c478bd9Sstevel@tonic-gate 	else if (*s1 == 'I') {
16617c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_INIT_SIZE) && (strncmp(s1,
16627c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_INIT), MSG_LD_INIT_SIZE) == 0)) {
16637c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16647c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_INIT;
16657c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_INIT;
16667c478bd9Sstevel@tonic-gate 		}
16677c478bd9Sstevel@tonic-gate 	}
16687c478bd9Sstevel@tonic-gate 	/*
16697c478bd9Sstevel@tonic-gate 	 * The LD_LIBRARY_PATH and LD_LOAD families.
16707c478bd9Sstevel@tonic-gate 	 */
16717c478bd9Sstevel@tonic-gate 	else if (*s1 == 'L') {
16727c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_LIBPATH_SIZE) && (strncmp(s1,
16737c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LIBPATH), MSG_LD_LIBPATH_SIZE) == 0)) {
16747c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1675dde769a2SRod Evans 			str = (select & SEL_REPLACE) ? &rpl_libpath :
1676dde769a2SRod Evans 			    &prm_libpath;
16777c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LIBPATH;
16787c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADAVAIL_SIZE) && (strncmp(s1,
16797c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADAVAIL), MSG_LD_LOADAVAIL_SIZE) == 0)) {
16807c478bd9Sstevel@tonic-gate 			/*
16817c478bd9Sstevel@tonic-gate 			 * Internal use by crle(1), not documented for general
16827c478bd9Sstevel@tonic-gate 			 * use.
16837c478bd9Sstevel@tonic-gate 			 */
16847c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16857c478bd9Sstevel@tonic-gate 			val = LML_FLG_LOADAVAIL;
16867c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LOADAVAIL;
16877c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADFLTR_SIZE) && (strncmp(s1,
16887c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADFLTR), MSG_LD_LOADFLTR_SIZE) == 0)) {
16897c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
16907c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LOADFLTR;
16917c478bd9Sstevel@tonic-gate 		}
16927c478bd9Sstevel@tonic-gate 	}
169308278a5eSRod Evans 	/*
169408278a5eSRod Evans 	 * LD_MACHCAP.
169508278a5eSRod Evans 	 */
169608278a5eSRod Evans 	else if (*s1 == 'M') {
169708278a5eSRod Evans 		if ((len == MSG_LD_MACHCAP_SIZE) && (strncmp(s1,
169808278a5eSRod Evans 		    MSG_ORIG(MSG_LD_MACHCAP), MSG_LD_MACHCAP_SIZE) == 0)) {
169908278a5eSRod Evans 			select |= SEL_ACT_STR;
170008278a5eSRod Evans 			str = (select & SEL_REPLACE) ?
170108278a5eSRod Evans 			    &rpl_machcap : &prm_machcap;
170208278a5eSRod Evans 			variable = ENV_FLG_MACHCAP;
170308278a5eSRod Evans 		}
170408278a5eSRod Evans 	}
17057c478bd9Sstevel@tonic-gate 	/*
17067c478bd9Sstevel@tonic-gate 	 * The LD_NO family.
17077c478bd9Sstevel@tonic-gate 	 */
17087c478bd9Sstevel@tonic-gate 	else if (*s1 == 'N') {
17097c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_NOAUDIT_SIZE) && (strncmp(s1,
17107c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUDIT), MSG_LD_NOAUDIT_SIZE) == 0)) {
17117c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17127c478bd9Sstevel@tonic-gate 			val = RT_FL_NOAUDIT;
17137c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOAUDIT;
17147c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOAUXFLTR_SIZE) && (strncmp(s1,
17157c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUXFLTR), MSG_LD_NOAUXFLTR_SIZE) == 0)) {
17167c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17177c478bd9Sstevel@tonic-gate 			val = RT_FL_NOAUXFLTR;
17187c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOAUXFLTR;
17197c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOBAPLT_SIZE) && (strncmp(s1,
17207c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOBAPLT), MSG_LD_NOBAPLT_SIZE) == 0)) {
17217c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17227c478bd9Sstevel@tonic-gate 			val = RT_FL_NOBAPLT;
17237c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOBAPLT;
17247c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOCONFIG_SIZE) && (strncmp(s1,
17257c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOCONFIG), MSG_LD_NOCONFIG_SIZE) == 0)) {
17267c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17277c478bd9Sstevel@tonic-gate 			val = RT_FL_NOCFG;
17287c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOCONFIG;
17297c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRCONFIG_SIZE) && (strncmp(s1,
17307c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRCONFIG),
17317c478bd9Sstevel@tonic-gate 		    MSG_LD_NODIRCONFIG_SIZE) == 0)) {
17327c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17337c478bd9Sstevel@tonic-gate 			val = RT_FL_NODIRCFG;
17347c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NODIRCONFIG;
17357c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRECT_SIZE) && (strncmp(s1,
17367c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRECT), MSG_LD_NODIRECT_SIZE) == 0)) {
17377c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
17387c478bd9Sstevel@tonic-gate 			val = LML_TFLG_NODIRECT;
17397c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NODIRECT;
17407c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOENVCONFIG_SIZE) && (strncmp(s1,
17417c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOENVCONFIG),
17427c478bd9Sstevel@tonic-gate 		    MSG_LD_NOENVCONFIG_SIZE) == 0)) {
17437c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17447c478bd9Sstevel@tonic-gate 			val = RT_FL_NOENVCFG;
17457c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOENVCONFIG;
17467c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOFLTCONFIG_SIZE) && (strncmp(s1,
17477c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOFLTCONFIG),
17487c478bd9Sstevel@tonic-gate 		    MSG_LD_NOFLTCONFIG_SIZE) == 0)) {
17497c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
17507c478bd9Sstevel@tonic-gate 			val = RT_FL2_NOFLTCFG;
17517c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOFLTCONFIG;
17527c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOLAZY_SIZE) && (strncmp(s1,
17537c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOLAZY), MSG_LD_NOLAZY_SIZE) == 0)) {
17547c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
17557c478bd9Sstevel@tonic-gate 			val = LML_TFLG_NOLAZYLD;
17567c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOLAZY;
17577c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOOBJALTER_SIZE) && (strncmp(s1,
17587c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOOBJALTER),
17597c478bd9Sstevel@tonic-gate 		    MSG_LD_NOOBJALTER_SIZE) == 0)) {
17607c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17617c478bd9Sstevel@tonic-gate 			val = RT_FL_NOOBJALT;
17627c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOOBJALTER;
17637c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOVERSION_SIZE) && (strncmp(s1,
17647c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOVERSION), MSG_LD_NOVERSION_SIZE) == 0)) {
17657c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17667c478bd9Sstevel@tonic-gate 			val = RT_FL_NOVERSION;
17677c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOVERSION;
1768df4628cbSrie 		} else if ((len == MSG_LD_NOUNRESWEAK_SIZE) && (strncmp(s1,
1769df4628cbSrie 		    MSG_ORIG(MSG_LD_NOUNRESWEAK),
1770df4628cbSrie 		    MSG_LD_NOUNRESWEAK_SIZE) == 0)) {
1771df4628cbSrie 			/*
1772df4628cbSrie 			 * LD_NOUNRESWEAK (internal, used by ldd(1)).
1773df4628cbSrie 			 */
1774df4628cbSrie 			select |= SEL_ACT_LML;
1775df4628cbSrie 			val = LML_FLG_TRC_NOUNRESWEAK;
1776df4628cbSrie 			variable = ENV_FLG_NOUNRESWEAK;
1777dae2dfb7Srie 		} else if ((len == MSG_LD_NOPAREXT_SIZE) && (strncmp(s1,
1778dae2dfb7Srie 		    MSG_ORIG(MSG_LD_NOPAREXT), MSG_LD_NOPAREXT_SIZE) == 0)) {
1779dae2dfb7Srie 			select |= SEL_ACT_LML;
1780dae2dfb7Srie 			val = LML_FLG_TRC_NOPAREXT;
1781dae2dfb7Srie 			variable = ENV_FLG_NOPAREXT;
17827c478bd9Sstevel@tonic-gate 		}
17837c478bd9Sstevel@tonic-gate 	}
17847c478bd9Sstevel@tonic-gate 	/*
178508278a5eSRod Evans 	 * LD_PLATCAP, LD_PRELOAD and LD_PROFILE family.
17867c478bd9Sstevel@tonic-gate 	 */
17877c478bd9Sstevel@tonic-gate 	else if (*s1 == 'P') {
178808278a5eSRod Evans 		if ((len == MSG_LD_PLATCAP_SIZE) && (strncmp(s1,
178908278a5eSRod Evans 		    MSG_ORIG(MSG_LD_PLATCAP), MSG_LD_PLATCAP_SIZE) == 0)) {
179008278a5eSRod Evans 			select |= SEL_ACT_STR;
179108278a5eSRod Evans 			str = (select & SEL_REPLACE) ?
179208278a5eSRod Evans 			    &rpl_platcap : &prm_platcap;
179308278a5eSRod Evans 			variable = ENV_FLG_PLATCAP;
179408278a5eSRod Evans 		} else if ((len == MSG_LD_PRELOAD_SIZE) && (strncmp(s1,
17957c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PRELOAD), MSG_LD_PRELOAD_SIZE) == 0)) {
17967c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1797dde769a2SRod Evans 			str = (select & SEL_REPLACE) ? &rpl_preload :
1798dde769a2SRod Evans 			    &prm_preload;
17997c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PRELOAD;
18007c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_SIZE) && (strncmp(s1,
18017c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE), MSG_LD_PROFILE_SIZE) == 0)) {
18027c478bd9Sstevel@tonic-gate 			/*
18037c478bd9Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
18047c478bd9Sstevel@tonic-gate 			 */
18057c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18067c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE;
18077c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_OUTPUT_SIZE) && (strncmp(s1,
18087c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE_OUTPUT),
18097c478bd9Sstevel@tonic-gate 		    MSG_LD_PROFILE_OUTPUT_SIZE) == 0)) {
18107c478bd9Sstevel@tonic-gate 			/*
18117c478bd9Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
18127c478bd9Sstevel@tonic-gate 			 */
18137c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
18147c478bd9Sstevel@tonic-gate 			str = &profile_out;
18157c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE_OUTPUT;
18167c478bd9Sstevel@tonic-gate 		}
18177c478bd9Sstevel@tonic-gate 	}
18187c478bd9Sstevel@tonic-gate 	/*
181908278a5eSRod Evans 	 * LD_SFCAP and LD_SIGNAL.
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	else if (*s1 == 'S') {
182208278a5eSRod Evans 		if ((len == MSG_LD_SFCAP_SIZE) && (strncmp(s1,
182308278a5eSRod Evans 		    MSG_ORIG(MSG_LD_SFCAP), MSG_LD_SFCAP_SIZE) == 0)) {
182408278a5eSRod Evans 			select |= SEL_ACT_STR;
182508278a5eSRod Evans 			str = (select & SEL_REPLACE) ?
182608278a5eSRod Evans 			    &rpl_sfcap : &prm_sfcap;
182708278a5eSRod Evans 			variable = ENV_FLG_SFCAP;
182808278a5eSRod Evans 		} else if ((len == MSG_LD_SIGNAL_SIZE) &&
18297c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_SIGNAL),
183008278a5eSRod Evans 		    MSG_LD_SIGNAL_SIZE) == 0) &&
183108278a5eSRod Evans 		    ((rtld_flags & RT_FL_SECURE) == 0)) {
18327c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18337c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_SIGNAL;
18347c478bd9Sstevel@tonic-gate 		}
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 	/*
18371d1fba8aSrie 	 * The LD_TRACE family (internal, used by ldd(1)).  This definition is
18381d1fba8aSrie 	 * the key to enabling all other ldd(1) specific environment variables.
18391d1fba8aSrie 	 * In case an auditor is called, which in turn might exec(2) a
18401d1fba8aSrie 	 * subprocess, this variable is disabled, so that any subprocess
18411d1fba8aSrie 	 * escapes ldd(1) processing.
18427c478bd9Sstevel@tonic-gate 	 */
18437c478bd9Sstevel@tonic-gate 	else if (*s1 == 'T') {
18447c478bd9Sstevel@tonic-gate 		if (((len == MSG_LD_TRACE_OBJS_SIZE) &&
18457c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS),
18467c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_SIZE) == 0)) ||
18477c478bd9Sstevel@tonic-gate 		    ((len == MSG_LD_TRACE_OBJS_E_SIZE) &&
18487c478bd9Sstevel@tonic-gate 		    (((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_E),
18497c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_E_SIZE) == 0) && !aout) ||
18507c478bd9Sstevel@tonic-gate 		    ((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_A),
18517c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_A_SIZE) == 0) && aout)))) {
18521d1fba8aSrie 			char	*s0 = (char *)s1;
18531d1fba8aSrie 
18547c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18557c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_OBJS;
18561d1fba8aSrie 
185702ca3e02Srie #if	defined(__sparc) || defined(__x86)
18581d1fba8aSrie 			/*
18591d1fba8aSrie 			 * The simplest way to "disable" this variable is to
18601d1fba8aSrie 			 * truncate this string to "LD_'\0'". This string is
18611d1fba8aSrie 			 * ignored by any ld.so.1 environment processing.
18621d1fba8aSrie 			 * Use of such interfaces as unsetenv(3c) are overkill,
18631d1fba8aSrie 			 * and would drag too much libc implementation detail
18641d1fba8aSrie 			 * into ld.so.1.
18651d1fba8aSrie 			 */
1866a953e2b1Srie 			*s0 = '\0';
18671d1fba8aSrie #else
18681d1fba8aSrie /*
18691d1fba8aSrie  * Verify that the above write is appropriate for any new platforms.
18701d1fba8aSrie  */
18711d1fba8aSrie #error	unsupported architecture!
18721d1fba8aSrie #endif
18737c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_TRACE_PTHS_SIZE) && (strncmp(s1,
18747c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_TRACE_PTHS),
18757c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_PTHS_SIZE) == 0)) {
18767c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18777c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_SEARCH;
18787c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_PTHS;
18797c478bd9Sstevel@tonic-gate 		}
18807c478bd9Sstevel@tonic-gate 	}
18817c478bd9Sstevel@tonic-gate 	/*
18827c478bd9Sstevel@tonic-gate 	 * LD_UNREF and LD_UNUSED (internal, used by ldd(1)).
18837c478bd9Sstevel@tonic-gate 	 */
18847c478bd9Sstevel@tonic-gate 	else if (*s1 == 'U') {
18857c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_UNREF_SIZE) && (strncmp(s1,
18867c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNREF), MSG_LD_UNREF_SIZE) == 0)) {
18877c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18887c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_UNREF;
18897c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_UNREF;
18907c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_UNUSED_SIZE) && (strncmp(s1,
18917c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNUSED), MSG_LD_UNUSED_SIZE) == 0)) {
18927c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18937c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_UNUSED;
18947c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_UNUSED;
18957c478bd9Sstevel@tonic-gate 		}
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate 	/*
18987c478bd9Sstevel@tonic-gate 	 * LD_VERBOSE (internal, used by ldd(1)).
18997c478bd9Sstevel@tonic-gate 	 */
19007c478bd9Sstevel@tonic-gate 	else if (*s1 == 'V') {
19017c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_VERBOSE_SIZE) && (strncmp(s1,
19027c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_VERBOSE), MSG_LD_VERBOSE_SIZE) == 0)) {
19037c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19047c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_VERBOSE;
19057c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_VERBOSE;
19067c478bd9Sstevel@tonic-gate 		}
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 	/*
19097c478bd9Sstevel@tonic-gate 	 * LD_WARN (internal, used by ldd(1)).
19107c478bd9Sstevel@tonic-gate 	 */
19117c478bd9Sstevel@tonic-gate 	else if (*s1 == 'W') {
19127c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_WARN_SIZE) && (strncmp(s1,
19137c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_WARN), MSG_LD_WARN_SIZE) == 0)) {
19147c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19157c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_WARN;
19167c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_WARN;
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 	}
191956deab07SRod Evans 
19207c478bd9Sstevel@tonic-gate 	if (variable == 0)
19217c478bd9Sstevel@tonic-gate 		return;
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	/*
1924635216b6SRod Evans 	 * If the variable is already processed with and ISA specific variable,
1925635216b6SRod Evans 	 * no further processing is needed.
19267c478bd9Sstevel@tonic-gate 	 */
19277c478bd9Sstevel@tonic-gate 	if (((select & SEL_REPLACE) && (rplisa & variable)) ||
19287c478bd9Sstevel@tonic-gate 	    ((select & SEL_PERMANT) && (prmisa & variable)))
19297c478bd9Sstevel@tonic-gate 		return;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	/*
1932635216b6SRod Evans 	 * Mark the appropriate variables.
19337c478bd9Sstevel@tonic-gate 	 */
19347c478bd9Sstevel@tonic-gate 	if (env_flags & ENV_TYP_ISA) {
19357c478bd9Sstevel@tonic-gate 		/*
1936635216b6SRod Evans 		 * This is an ISA setting.
19377c478bd9Sstevel@tonic-gate 		 */
19387c478bd9Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1939fa034d7fSseizo 			if (rplisa & variable)
1940fa034d7fSseizo 				return;
19417c478bd9Sstevel@tonic-gate 			rplisa |= variable;
19427c478bd9Sstevel@tonic-gate 		} else {
19437c478bd9Sstevel@tonic-gate 			prmisa |= variable;
19447c478bd9Sstevel@tonic-gate 		}
1945635216b6SRod Evans 	} else {
19467c478bd9Sstevel@tonic-gate 		/*
1947dde769a2SRod Evans 		 * This is a non-ISA setting.
19487c478bd9Sstevel@tonic-gate 		 */
19497c478bd9Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1950fa034d7fSseizo 			if (rplgen & variable)
1951fa034d7fSseizo 				return;
19527c478bd9Sstevel@tonic-gate 			rplgen |= variable;
19537c478bd9Sstevel@tonic-gate 		} else
19547c478bd9Sstevel@tonic-gate 			prmgen |= variable;
1955635216b6SRod Evans 	}
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	/*
19587c478bd9Sstevel@tonic-gate 	 * Now perform the setting.
19597c478bd9Sstevel@tonic-gate 	 */
19607c478bd9Sstevel@tonic-gate 	if (select & SEL_ACT_RT) {
19617c478bd9Sstevel@tonic-gate 		if (s2)
19627c478bd9Sstevel@tonic-gate 			rtld_flags |= val;
19637c478bd9Sstevel@tonic-gate 		else
19647c478bd9Sstevel@tonic-gate 			rtld_flags &= ~val;
19657c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_RT2) {
19667c478bd9Sstevel@tonic-gate 		if (s2)
19677c478bd9Sstevel@tonic-gate 			rtld_flags2 |= val;
19687c478bd9Sstevel@tonic-gate 		else
19697c478bd9Sstevel@tonic-gate 			rtld_flags2 &= ~val;
1970635216b6SRod Evans 	} else if (select & SEL_ACT_STR) {
19717c478bd9Sstevel@tonic-gate 		*str = s2;
1972635216b6SRod Evans 	} else if (select & SEL_ACT_LML) {
19737c478bd9Sstevel@tonic-gate 		if (s2)
19747c478bd9Sstevel@tonic-gate 			*lmflags |= val;
19757c478bd9Sstevel@tonic-gate 		else
19767c478bd9Sstevel@tonic-gate 			*lmflags &= ~val;
19777c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_LMLT) {
19787c478bd9Sstevel@tonic-gate 		if (s2)
19797c478bd9Sstevel@tonic-gate 			*lmtflags |= val;
19807c478bd9Sstevel@tonic-gate 		else
19817c478bd9Sstevel@tonic-gate 			*lmtflags &= ~val;
19827c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_1) {
19837c478bd9Sstevel@tonic-gate 		/*
19847c478bd9Sstevel@tonic-gate 		 * variable is either ENV_FLG_FLAGS or ENV_FLG_LIBPATH
19857c478bd9Sstevel@tonic-gate 		 */
19867c478bd9Sstevel@tonic-gate 		*str = s2;
19877c478bd9Sstevel@tonic-gate 		if ((select & SEL_REPLACE) && (env_flags & ENV_TYP_CONFIG)) {
19887c478bd9Sstevel@tonic-gate 			if (s2) {
19897c478bd9Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
19907c478bd9Sstevel@tonic-gate 					env_info |= ENV_INF_FLAGCFG;
19917c478bd9Sstevel@tonic-gate 				else
19927c478bd9Sstevel@tonic-gate 					env_info |= ENV_INF_PATHCFG;
19937c478bd9Sstevel@tonic-gate 			} else {
19947c478bd9Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
19957c478bd9Sstevel@tonic-gate 					env_info &= ~ENV_INF_FLAGCFG;
19967c478bd9Sstevel@tonic-gate 				else
19977c478bd9Sstevel@tonic-gate 					env_info &= ~ENV_INF_PATHCFG;
19987c478bd9Sstevel@tonic-gate 			}
19997c478bd9Sstevel@tonic-gate 		}
20007c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_2) {
20017c478bd9Sstevel@tonic-gate 		/*
20027c478bd9Sstevel@tonic-gate 		 * variables can be: ENV_FLG_
2003635216b6SRod Evans 		 * 	AUDIT_ARGS, BINDING, CONFGEN, LOADFLTR, PROFILE,
2004635216b6SRod Evans 		 *	SIGNAL, TRACE_OBJS
20057c478bd9Sstevel@tonic-gate 		 */
2006635216b6SRod Evans 		switch (variable) {
2007635216b6SRod Evans 		case ENV_FLG_AUDIT_ARGS:
20087c478bd9Sstevel@tonic-gate 			if (s2) {
20097c478bd9Sstevel@tonic-gate 				audit_argcnt = atoi(s2);
20107c478bd9Sstevel@tonic-gate 				audit_argcnt += audit_argcnt % 2;
20117c478bd9Sstevel@tonic-gate 			} else
20127c478bd9Sstevel@tonic-gate 				audit_argcnt = 0;
2013635216b6SRod Evans 			break;
2014635216b6SRod Evans 		case ENV_FLG_BINDINGS:
20157c478bd9Sstevel@tonic-gate 			if (s2)
20167c478bd9Sstevel@tonic-gate 				rpl_debug = MSG_ORIG(MSG_TKN_BINDINGS);
20177c478bd9Sstevel@tonic-gate 			else
201843d7826aSRod Evans 				rpl_debug = NULL;
2019635216b6SRod Evans 			break;
2020635216b6SRod Evans 		case ENV_FLG_CONFGEN:
20217c478bd9Sstevel@tonic-gate 			if (s2) {
20227c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_CONFGEN;
20237c478bd9Sstevel@tonic-gate 				*lmflags |= LML_FLG_IGNRELERR;
20247c478bd9Sstevel@tonic-gate 			} else {
20257c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_CONFGEN;
20267c478bd9Sstevel@tonic-gate 				*lmflags &= ~LML_FLG_IGNRELERR;
20277c478bd9Sstevel@tonic-gate 			}
2028635216b6SRod Evans 			break;
2029635216b6SRod Evans 		case ENV_FLG_LOADFLTR:
20307c478bd9Sstevel@tonic-gate 			if (s2) {
20317c478bd9Sstevel@tonic-gate 				*lmtflags |= LML_TFLG_LOADFLTR;
20327c478bd9Sstevel@tonic-gate 				if (*s2 == '2')
20337c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_WARNFLTR;
20347c478bd9Sstevel@tonic-gate 			} else {
20357c478bd9Sstevel@tonic-gate 				*lmtflags &= ~LML_TFLG_LOADFLTR;
20367c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_WARNFLTR;
20377c478bd9Sstevel@tonic-gate 			}
2038635216b6SRod Evans 			break;
2039635216b6SRod Evans 		case ENV_FLG_PROFILE:
20407c478bd9Sstevel@tonic-gate 			profile_name = s2;
20417c478bd9Sstevel@tonic-gate 			if (s2) {
20427c478bd9Sstevel@tonic-gate 				if (strcmp(s2, MSG_ORIG(MSG_FIL_RTLD)) == 0) {
20437c478bd9Sstevel@tonic-gate 					return;
20447c478bd9Sstevel@tonic-gate 				}
2045a953e2b1Srie 				/* BEGIN CSTYLED */
20467c478bd9Sstevel@tonic-gate 				if (rtld_flags & RT_FL_SECURE) {
20477c478bd9Sstevel@tonic-gate 					profile_lib =
20487c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
20497c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE_64);
20507c478bd9Sstevel@tonic-gate #else
20517c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE);
20527c478bd9Sstevel@tonic-gate #endif
20537c478bd9Sstevel@tonic-gate 				} else {
20547c478bd9Sstevel@tonic-gate 					profile_lib =
20557c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
20567c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF_64);
20577c478bd9Sstevel@tonic-gate #else
20587c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF);
20597c478bd9Sstevel@tonic-gate #endif
20607c478bd9Sstevel@tonic-gate 				}
2061a953e2b1Srie 				/* END CSTYLED */
20627c478bd9Sstevel@tonic-gate 			} else
206356deab07SRod Evans 				profile_lib = NULL;
2064635216b6SRod Evans 			break;
2065635216b6SRod Evans 		case ENV_FLG_SIGNAL:
20667c478bd9Sstevel@tonic-gate 			killsig = s2 ? atoi(s2) : SIGKILL;
2067635216b6SRod Evans 			break;
2068635216b6SRod Evans 		case ENV_FLG_TRACE_OBJS:
20697c478bd9Sstevel@tonic-gate 			if (s2) {
20707c478bd9Sstevel@tonic-gate 				*lmflags |= LML_FLG_TRC_ENABLE;
20717c478bd9Sstevel@tonic-gate 				if (*s2 == '2')
20727c478bd9Sstevel@tonic-gate 					*lmflags |= LML_FLG_TRC_LDDSTUB;
20737c478bd9Sstevel@tonic-gate 			} else
20747c478bd9Sstevel@tonic-gate 				*lmflags &=
2075dde769a2SRod Evans 				    ~(LML_FLG_TRC_ENABLE | LML_FLG_TRC_LDDSTUB);
2076635216b6SRod Evans 			break;
20777c478bd9Sstevel@tonic-gate 		}
20787c478bd9Sstevel@tonic-gate 	}
20797c478bd9Sstevel@tonic-gate }
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate /*
20827c478bd9Sstevel@tonic-gate  * Determine whether we have an architecture specific environment variable.
20837c478bd9Sstevel@tonic-gate  * If we do, and we're the wrong architecture, it'll just get ignored.
20847c478bd9Sstevel@tonic-gate  * Otherwise the variable is processed in it's architecture neutral form.
20857c478bd9Sstevel@tonic-gate  */
20867c478bd9Sstevel@tonic-gate static int
20877c478bd9Sstevel@tonic-gate ld_arch_env(const char *s1, size_t *len)
20887c478bd9Sstevel@tonic-gate {
20897c478bd9Sstevel@tonic-gate 	size_t	_len = *len - 3;
20907c478bd9Sstevel@tonic-gate 
20917c478bd9Sstevel@tonic-gate 	if (s1[_len++] == '_') {
20927c478bd9Sstevel@tonic-gate 		if ((s1[_len] == '3') && (s1[_len + 1] == '2')) {
20937c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
20947c478bd9Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
20957c478bd9Sstevel@tonic-gate #else
20967c478bd9Sstevel@tonic-gate 			*len = *len - 3;
20977c478bd9Sstevel@tonic-gate 			return (ENV_TYP_ISA);
20987c478bd9Sstevel@tonic-gate #endif
20997c478bd9Sstevel@tonic-gate 		}
21007c478bd9Sstevel@tonic-gate 		if ((s1[_len] == '6') && (s1[_len + 1] == '4')) {
21017c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
21027c478bd9Sstevel@tonic-gate 			*len = *len - 3;
21037c478bd9Sstevel@tonic-gate 			return (ENV_TYP_ISA);
21047c478bd9Sstevel@tonic-gate #else
21057c478bd9Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
21067c478bd9Sstevel@tonic-gate #endif
21077c478bd9Sstevel@tonic-gate 		}
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	return (0);
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate /*
21147c478bd9Sstevel@tonic-gate  * Process an LD_FLAGS environment variable.  The value can be a comma
21157c478bd9Sstevel@tonic-gate  * separated set of tokens, which are sent (in upper case) into the generic
21167c478bd9Sstevel@tonic-gate  * LD_XXXX environment variable engine.  For example:
21177c478bd9Sstevel@tonic-gate  *
21187c478bd9Sstevel@tonic-gate  *	LD_FLAGS=bind_now		->	LD_BIND_NOW=1
21197c478bd9Sstevel@tonic-gate  *	LD_FLAGS=library_path=/foo:.	->	LD_LIBRARY_PATH=/foo:.
21207c478bd9Sstevel@tonic-gate  *	LD_FLAGS=debug=files:detail	->	LD_DEBUG=files:detail
21217c478bd9Sstevel@tonic-gate  * or
21227c478bd9Sstevel@tonic-gate  *	LD_FLAGS=bind_now,library_path=/foo:.,debug=files:detail
21237c478bd9Sstevel@tonic-gate  */
21247c478bd9Sstevel@tonic-gate static int
21257c478bd9Sstevel@tonic-gate ld_flags_env(const char *str, Word *lmflags, Word *lmtflags,
21267c478bd9Sstevel@tonic-gate     uint_t env_flags, int aout)
21277c478bd9Sstevel@tonic-gate {
21283dbfc803SRod Evans 	char	*nstr, *sstr, *estr = NULL;
21297c478bd9Sstevel@tonic-gate 	size_t	nlen, len;
21307c478bd9Sstevel@tonic-gate 
21313dbfc803SRod Evans 	if (str == NULL)
21327c478bd9Sstevel@tonic-gate 		return (0);
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 	/*
21357c478bd9Sstevel@tonic-gate 	 * Create a new string as we're going to transform the token(s) into
21367c478bd9Sstevel@tonic-gate 	 * uppercase and separate tokens with nulls.
21377c478bd9Sstevel@tonic-gate 	 */
21387c478bd9Sstevel@tonic-gate 	len = strlen(str);
21393dbfc803SRod Evans 	if ((nstr = malloc(len + 1)) == NULL)
21407c478bd9Sstevel@tonic-gate 		return (1);
21417c478bd9Sstevel@tonic-gate 	(void) strcpy(nstr, str);
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	for (sstr = nstr; sstr; sstr++, len--) {
21447c478bd9Sstevel@tonic-gate 		int	flags;
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 		if ((*sstr != '\0') && (*sstr != ',')) {
21473dbfc803SRod Evans 			if (estr == NULL) {
21487c478bd9Sstevel@tonic-gate 				if (*sstr == '=')
21497c478bd9Sstevel@tonic-gate 					estr = sstr;
21507c478bd9Sstevel@tonic-gate 				else {
21517c478bd9Sstevel@tonic-gate 					/*
21527c478bd9Sstevel@tonic-gate 					 * Translate token to uppercase.  Don't
21537c478bd9Sstevel@tonic-gate 					 * use toupper(3C) as including this
21547c478bd9Sstevel@tonic-gate 					 * code doubles the size of ld.so.1.
21557c478bd9Sstevel@tonic-gate 					 */
21567c478bd9Sstevel@tonic-gate 					if ((*sstr >= 'a') && (*sstr <= 'z'))
21577c478bd9Sstevel@tonic-gate 						*sstr = *sstr - ('a' - 'A');
21587c478bd9Sstevel@tonic-gate 				}
21597c478bd9Sstevel@tonic-gate 			}
21607c478bd9Sstevel@tonic-gate 			continue;
21617c478bd9Sstevel@tonic-gate 		}
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 		*sstr = '\0';
21647c478bd9Sstevel@tonic-gate 		if (estr) {
21657c478bd9Sstevel@tonic-gate 			nlen = estr - nstr;
21667c478bd9Sstevel@tonic-gate 			if ((*++estr == '\0') || (*estr == ','))
216743d7826aSRod Evans 				estr = NULL;
21687c478bd9Sstevel@tonic-gate 		} else
21697c478bd9Sstevel@tonic-gate 			nlen = sstr - nstr;
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate 		/*
21727c478bd9Sstevel@tonic-gate 		 * Fabricate a boolean definition for any unqualified variable.
21737c478bd9Sstevel@tonic-gate 		 * Thus LD_FLAGS=bind_now is represented as BIND_NOW=(null).
21747c478bd9Sstevel@tonic-gate 		 * The value is sufficient to assert any boolean variables, plus
21757c478bd9Sstevel@tonic-gate 		 * the term "(null)" is specifically chosen in case someone
21767c478bd9Sstevel@tonic-gate 		 * mistakenly supplies something like LD_FLAGS=library_path.
21777c478bd9Sstevel@tonic-gate 		 */
21783dbfc803SRod Evans 		if (estr == NULL)
21797c478bd9Sstevel@tonic-gate 			estr = (char *)MSG_INTL(MSG_STR_NULL);
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate 		/*
21827c478bd9Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
21837c478bd9Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
21847c478bd9Sstevel@tonic-gate 		 * neutral portion of the string.
21857c478bd9Sstevel@tonic-gate 		 */
21867c478bd9Sstevel@tonic-gate 		if ((flags = ld_arch_env(nstr, &nlen)) != ENV_TYP_IGNORE) {
21877c478bd9Sstevel@tonic-gate 			ld_generic_env(nstr, nlen, estr, lmflags,
21887c478bd9Sstevel@tonic-gate 			    lmtflags, (env_flags | flags), aout);
21897c478bd9Sstevel@tonic-gate 		}
21907c478bd9Sstevel@tonic-gate 		if (len == 0)
21917c478bd9Sstevel@tonic-gate 			return (0);
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 		nstr = sstr + 1;
219443d7826aSRod Evans 		estr = NULL;
21957c478bd9Sstevel@tonic-gate 	}
21967c478bd9Sstevel@tonic-gate 	return (0);
21977c478bd9Sstevel@tonic-gate }
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate /*
22017c478bd9Sstevel@tonic-gate  * Process a single environment string.  Only strings starting with `LD_' are
22027c478bd9Sstevel@tonic-gate  * reserved for our use.  By convention, all strings should be of the form
22037c478bd9Sstevel@tonic-gate  * `LD_XXXX=', if the string is followed by a non-null value the appropriate
22047c478bd9Sstevel@tonic-gate  * functionality is enabled.  Also pick off applicable locale variables.
22057c478bd9Sstevel@tonic-gate  */
22067c478bd9Sstevel@tonic-gate #define	LOC_LANG	1
22077c478bd9Sstevel@tonic-gate #define	LOC_MESG	2
22087c478bd9Sstevel@tonic-gate #define	LOC_ALL		3
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate static void
22117c478bd9Sstevel@tonic-gate ld_str_env(const char *s1, Word *lmflags, Word *lmtflags, uint_t env_flags,
22127c478bd9Sstevel@tonic-gate     int aout)
22137c478bd9Sstevel@tonic-gate {
22147c478bd9Sstevel@tonic-gate 	const char	*s2;
221510a4fa49Srie 	static		size_t	loc = 0;
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	if (*s1++ != 'L')
22187c478bd9Sstevel@tonic-gate 		return;
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	/*
22217c478bd9Sstevel@tonic-gate 	 * See if we have any locale environment settings.  These environment
22227c478bd9Sstevel@tonic-gate 	 * variables have a precedence, LC_ALL is higher than LC_MESSAGES which
22237c478bd9Sstevel@tonic-gate 	 * is higher than LANG.
22247c478bd9Sstevel@tonic-gate 	 */
22257c478bd9Sstevel@tonic-gate 	s2 = s1;
22267c478bd9Sstevel@tonic-gate 	if ((*s2++ == 'C') && (*s2++ == '_') && (*s2 != '\0')) {
22277c478bd9Sstevel@tonic-gate 		if (strncmp(s2, MSG_ORIG(MSG_LC_ALL), MSG_LC_ALL_SIZE) == 0) {
22287c478bd9Sstevel@tonic-gate 			s2 += MSG_LC_ALL_SIZE;
22297c478bd9Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_ALL)) {
223010a4fa49Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22317c478bd9Sstevel@tonic-gate 				loc = LOC_ALL;
22327c478bd9Sstevel@tonic-gate 			}
22337c478bd9Sstevel@tonic-gate 		} else if (strncmp(s2, MSG_ORIG(MSG_LC_MESSAGES),
22347c478bd9Sstevel@tonic-gate 		    MSG_LC_MESSAGES_SIZE) == 0) {
22357c478bd9Sstevel@tonic-gate 			s2 += MSG_LC_MESSAGES_SIZE;
22367c478bd9Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_MESG)) {
223710a4fa49Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22387c478bd9Sstevel@tonic-gate 				loc = LOC_MESG;
22397c478bd9Sstevel@tonic-gate 			}
22407c478bd9Sstevel@tonic-gate 		}
22417c478bd9Sstevel@tonic-gate 		return;
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 	s2 = s1;
22457c478bd9Sstevel@tonic-gate 	if ((*s2++ == 'A') && (*s2++ == 'N') && (*s2++ == 'G') &&
22467c478bd9Sstevel@tonic-gate 	    (*s2++ == '=') && (*s2 != '\0') && (loc < LOC_LANG)) {
224710a4fa49Srie 		glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
22487c478bd9Sstevel@tonic-gate 		loc = LOC_LANG;
22497c478bd9Sstevel@tonic-gate 		return;
22507c478bd9Sstevel@tonic-gate 	}
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 	/*
22537c478bd9Sstevel@tonic-gate 	 * Pick off any LD_XXXX environment variables.
22547c478bd9Sstevel@tonic-gate 	 */
22557c478bd9Sstevel@tonic-gate 	if ((*s1++ == 'D') && (*s1++ == '_') && (*s1 != '\0')) {
22567c478bd9Sstevel@tonic-gate 		size_t	len;
22577c478bd9Sstevel@tonic-gate 		int	flags;
22587c478bd9Sstevel@tonic-gate 
22599acbbeafSnn 		/*
22609acbbeafSnn 		 * In a branded process we must ignore all LD_XXXX env vars
22619acbbeafSnn 		 * because they are intended for the brand's linker.
22629acbbeafSnn 		 * To affect the Solaris linker, use LD_BRAND_XXXX instead.
22639acbbeafSnn 		 */
22649acbbeafSnn 		if (rtld_flags2 & RT_FL2_BRANDED) {
22659acbbeafSnn 			if (strncmp(s1, MSG_ORIG(MSG_LD_BRAND_PREFIX),
22669acbbeafSnn 			    MSG_LD_BRAND_PREFIX_SIZE) != 0)
22679acbbeafSnn 				return;
22689acbbeafSnn 			s1 += MSG_LD_BRAND_PREFIX_SIZE;
22699acbbeafSnn 		}
22709acbbeafSnn 
22717c478bd9Sstevel@tonic-gate 		/*
22727c478bd9Sstevel@tonic-gate 		 * Environment variables with no value (ie. LD_XXXX=) typically
22737c478bd9Sstevel@tonic-gate 		 * have no impact, however if environment variables are defined
22747c478bd9Sstevel@tonic-gate 		 * within a configuration file, these null user settings can be
22757c478bd9Sstevel@tonic-gate 		 * used to disable any configuration replaceable definitions.
22767c478bd9Sstevel@tonic-gate 		 */
22773dbfc803SRod Evans 		if ((s2 = strchr(s1, '=')) == NULL) {
22787c478bd9Sstevel@tonic-gate 			len = strlen(s1);
227943d7826aSRod Evans 			s2 = NULL;
22807c478bd9Sstevel@tonic-gate 		} else if (*++s2 == '\0') {
22817c478bd9Sstevel@tonic-gate 			len = strlen(s1) - 1;
228243d7826aSRod Evans 			s2 = NULL;
22837c478bd9Sstevel@tonic-gate 		} else {
22847c478bd9Sstevel@tonic-gate 			len = s2 - s1 - 1;
2285e23c41c9SAli Bahrami 			while (conv_strproc_isspace(*s2))
22867c478bd9Sstevel@tonic-gate 				s2++;
22877c478bd9Sstevel@tonic-gate 		}
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate 		/*
22907c478bd9Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
22917c478bd9Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
22927c478bd9Sstevel@tonic-gate 		 * neutral portion of the string.
22937c478bd9Sstevel@tonic-gate 		 */
22947c478bd9Sstevel@tonic-gate 		if ((flags = ld_arch_env(s1, &len)) == ENV_TYP_IGNORE)
22957c478bd9Sstevel@tonic-gate 			return;
22967c478bd9Sstevel@tonic-gate 		env_flags |= flags;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 		ld_generic_env(s1, len, s2, lmflags, lmtflags, env_flags, aout);
22997c478bd9Sstevel@tonic-gate 	}
23007c478bd9Sstevel@tonic-gate }
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate /*
23037c478bd9Sstevel@tonic-gate  * Internal getenv routine.  Called immediately after ld.so.1 initializes
23047c478bd9Sstevel@tonic-gate  * itself.
23057c478bd9Sstevel@tonic-gate  */
23067c478bd9Sstevel@tonic-gate int
230756deab07SRod Evans readenv_user(const char **envp, Word *lmflags, Word *lmtflags, int aout)
23087c478bd9Sstevel@tonic-gate {
230910a4fa49Srie 	char	*locale;
231010a4fa49Srie 
231156deab07SRod Evans 	if (envp == NULL)
23127c478bd9Sstevel@tonic-gate 		return (0);
23137c478bd9Sstevel@tonic-gate 
231456deab07SRod Evans 	while (*envp != NULL)
23157c478bd9Sstevel@tonic-gate 		ld_str_env(*envp++, lmflags, lmtflags, 0, aout);
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	/*
23187c478bd9Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
23197c478bd9Sstevel@tonic-gate 	 * these strings.
23207c478bd9Sstevel@tonic-gate 	 */
23217c478bd9Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
23227c478bd9Sstevel@tonic-gate 		return (1);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	/*
23257c478bd9Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
23267c478bd9Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
23277c478bd9Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
23287c478bd9Sstevel@tonic-gate 	 */
23297c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
233056deab07SRod Evans 		rpl_audit = profile_lib = profile_name = NULL;
23317c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
23327c478bd9Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
23337c478bd9Sstevel@tonic-gate 
2334dffec89cSrie 	/*
2335dffec89cSrie 	 * If both LD_BIND_NOW and LD_BIND_LAZY are specified, the former wins.
2336dffec89cSrie 	 */
2337dffec89cSrie 	if ((rtld_flags2 & (RT_FL2_BINDNOW | RT_FL2_BINDLAZY)) ==
2338dffec89cSrie 	    (RT_FL2_BINDNOW | RT_FL2_BINDLAZY))
2339dffec89cSrie 		rtld_flags2 &= ~RT_FL2_BINDLAZY;
2340dffec89cSrie 
2341dae2dfb7Srie 	/*
2342dae2dfb7Srie 	 * When using ldd(1) -r or -d against an executable, assert -p.
2343dae2dfb7Srie 	 */
2344dae2dfb7Srie 	if ((*lmflags &
2345dae2dfb7Srie 	    (LML_FLG_TRC_WARN | LML_FLG_TRC_LDDSTUB)) == LML_FLG_TRC_WARN)
2346dae2dfb7Srie 		*lmflags |= LML_FLG_TRC_NOPAREXT;
2347dae2dfb7Srie 
23487c478bd9Sstevel@tonic-gate 	/*
23497c478bd9Sstevel@tonic-gate 	 * If we have a locale setting make sure its worth processing further.
23501d6b7ad8Srie 	 * C and POSIX locales don't need any processing.  In addition, to
23511d6b7ad8Srie 	 * ensure no one escapes the /usr/lib/locale hierarchy, don't allow
23521d6b7ad8Srie 	 * the locale to contain a segment that leads upward in the file system
23531d6b7ad8Srie 	 * hierarchy (i.e. no '..' segments).   Given that we'll be confined to
23541d6b7ad8Srie 	 * the /usr/lib/locale hierarchy, there is no need to extensively
23551d6b7ad8Srie 	 * validate the mode or ownership of any message file (as libc's
23561d6b7ad8Srie 	 * generic handling of message files does).  Duplicate the string so
23571d6b7ad8Srie 	 * that new locale setting can generically cleanup any previous locales.
23587c478bd9Sstevel@tonic-gate 	 */
235943d7826aSRod Evans 	if ((locale = glcs[CI_LCMESSAGES].lc_un.lc_ptr) != NULL) {
23607c478bd9Sstevel@tonic-gate 		if (((*locale == 'C') && (*(locale + 1) == '\0')) ||
23611d6b7ad8Srie 		    (strcmp(locale, MSG_ORIG(MSG_TKN_POSIX)) == 0) ||
23621d6b7ad8Srie 		    (strstr(locale, MSG_ORIG(MSG_TKN_DOTDOT)) != NULL))
236343d7826aSRod Evans 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = NULL;
23647c478bd9Sstevel@tonic-gate 		else
236510a4fa49Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = strdup(locale);
23667c478bd9Sstevel@tonic-gate 	}
23677c478bd9Sstevel@tonic-gate 	return (0);
23687c478bd9Sstevel@tonic-gate }
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate /*
23717c478bd9Sstevel@tonic-gate  * Configuration environment processing.  Called after the a.out has been
23727c478bd9Sstevel@tonic-gate  * processed (as the a.out can specify its own configuration file).
23737c478bd9Sstevel@tonic-gate  */
23747c478bd9Sstevel@tonic-gate int
23757c478bd9Sstevel@tonic-gate readenv_config(Rtc_env * envtbl, Addr addr, int aout)
23767c478bd9Sstevel@tonic-gate {
23779aa23310Srie 	Word	*lmflags = &(lml_main.lm_flags);
23789aa23310Srie 	Word	*lmtflags = &(lml_main.lm_tflags);
23797c478bd9Sstevel@tonic-gate 
238056deab07SRod Evans 	if (envtbl == NULL)
23817c478bd9Sstevel@tonic-gate 		return (0);
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate 	while (envtbl->env_str) {
23847c478bd9Sstevel@tonic-gate 		uint_t	env_flags = ENV_TYP_CONFIG;
23857c478bd9Sstevel@tonic-gate 
23867c478bd9Sstevel@tonic-gate 		if (envtbl->env_flags & RTC_ENV_PERMANT)
23877c478bd9Sstevel@tonic-gate 			env_flags |= ENV_TYP_PERMANT;
23887c478bd9Sstevel@tonic-gate 
23897c478bd9Sstevel@tonic-gate 		ld_str_env((const char *)(envtbl->env_str + addr),
23907c478bd9Sstevel@tonic-gate 		    lmflags, lmtflags, env_flags, 0);
23917c478bd9Sstevel@tonic-gate 		envtbl++;
23927c478bd9Sstevel@tonic-gate 	}
23937c478bd9Sstevel@tonic-gate 
23947c478bd9Sstevel@tonic-gate 	/*
23957c478bd9Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
23967c478bd9Sstevel@tonic-gate 	 * these strings.
23977c478bd9Sstevel@tonic-gate 	 */
23987c478bd9Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
23997c478bd9Sstevel@tonic-gate 		return (1);
24007c478bd9Sstevel@tonic-gate 	if (ld_flags_env(prm_ldflags, lmflags, lmtflags, ENV_TYP_CONFIG,
24017c478bd9Sstevel@tonic-gate 	    aout) == 1)
24027c478bd9Sstevel@tonic-gate 		return (1);
24037c478bd9Sstevel@tonic-gate 
24047c478bd9Sstevel@tonic-gate 	/*
24057c478bd9Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
24067c478bd9Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
24077c478bd9Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
24087c478bd9Sstevel@tonic-gate 	 */
24097c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
241056deab07SRod Evans 		prm_audit = profile_lib = profile_name = NULL;
24117c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
24127c478bd9Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
24137c478bd9Sstevel@tonic-gate 
24147c478bd9Sstevel@tonic-gate 	return (0);
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate int
24187c478bd9Sstevel@tonic-gate dowrite(Prfbuf * prf)
24197c478bd9Sstevel@tonic-gate {
24207c478bd9Sstevel@tonic-gate 	/*
24217c478bd9Sstevel@tonic-gate 	 * We do not have a valid file descriptor, so we are unable
24227c478bd9Sstevel@tonic-gate 	 * to flush the buffer.
24237c478bd9Sstevel@tonic-gate 	 */
24247c478bd9Sstevel@tonic-gate 	if (prf->pr_fd == -1)
24257c478bd9Sstevel@tonic-gate 		return (0);
24267c478bd9Sstevel@tonic-gate 	(void) write(prf->pr_fd, prf->pr_buf, prf->pr_cur - prf->pr_buf);
24277c478bd9Sstevel@tonic-gate 	prf->pr_cur = prf->pr_buf;
24287c478bd9Sstevel@tonic-gate 	return (1);
24297c478bd9Sstevel@tonic-gate }
24307c478bd9Sstevel@tonic-gate 
24317c478bd9Sstevel@tonic-gate /*
24327c478bd9Sstevel@tonic-gate  * Simplified printing.  The following conversion specifications are supported:
24337c478bd9Sstevel@tonic-gate  *
24347c478bd9Sstevel@tonic-gate  *	% [#] [-] [min field width] [. precision] s|d|x|c
24357c478bd9Sstevel@tonic-gate  *
24367c478bd9Sstevel@tonic-gate  *
24377c478bd9Sstevel@tonic-gate  * dorprf takes the output buffer in the form of Prfbuf which permits
24387c478bd9Sstevel@tonic-gate  * the verification of the output buffer size and the concatenation
24397c478bd9Sstevel@tonic-gate  * of data to an already existing output buffer.  The Prfbuf
24407c478bd9Sstevel@tonic-gate  * structure contains the following:
24417c478bd9Sstevel@tonic-gate  *
24427c478bd9Sstevel@tonic-gate  *  pr_buf	pointer to the beginning of the output buffer.
24437c478bd9Sstevel@tonic-gate  *  pr_cur	pointer to the next available byte in the output buffer.  By
24447c478bd9Sstevel@tonic-gate  *		setting pr_cur ahead of pr_buf you can append to an already
24457c478bd9Sstevel@tonic-gate  *		existing buffer.
24467c478bd9Sstevel@tonic-gate  *  pr_len	the size of the output buffer.  By setting pr_len to '0' you
24477c478bd9Sstevel@tonic-gate  *		disable protection from overflows in the output buffer.
24487c478bd9Sstevel@tonic-gate  *  pr_fd	a pointer to the file-descriptor the buffer will eventually be
24497c478bd9Sstevel@tonic-gate  *		output to.  If pr_fd is set to '-1' then it's assumed there is
24501d6b7ad8Srie  *		no output buffer, and doprf() will return with an error to
24511d6b7ad8Srie  *		indicate an output buffer overflow.  If pr_fd is > -1 then when
24521d6b7ad8Srie  *		the output buffer is filled it will be flushed to pr_fd and will
24531d6b7ad8Srie  *		then be	available for additional data.
24547c478bd9Sstevel@tonic-gate  */
24557c478bd9Sstevel@tonic-gate #define	FLG_UT_MINUS	0x0001	/* - */
24567c478bd9Sstevel@tonic-gate #define	FLG_UT_SHARP	0x0002	/* # */
24577c478bd9Sstevel@tonic-gate #define	FLG_UT_DOTSEEN	0x0008	/* dot appeared in format spec */
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate /*
246010a4fa49Srie  * This macro is for use from within doprf only.  It is to be used for checking
246110a4fa49Srie  * the output buffer size and placing characters into the buffer.
24627c478bd9Sstevel@tonic-gate  */
24637c478bd9Sstevel@tonic-gate #define	PUTC(c) \
24647c478bd9Sstevel@tonic-gate 	{ \
246510a4fa49Srie 		char tmpc; \
24667c478bd9Sstevel@tonic-gate 		\
24677c478bd9Sstevel@tonic-gate 		tmpc = (c); \
246810a4fa49Srie 		if (bufsiz && (bp >= bufend)) { \
24697c478bd9Sstevel@tonic-gate 			prf->pr_cur = bp; \
24707c478bd9Sstevel@tonic-gate 			if (dowrite(prf) == 0) \
24717c478bd9Sstevel@tonic-gate 				return (0); \
24727c478bd9Sstevel@tonic-gate 			bp = prf->pr_cur; \
24737c478bd9Sstevel@tonic-gate 		} \
24747c478bd9Sstevel@tonic-gate 		*bp++ = tmpc; \
24757c478bd9Sstevel@tonic-gate 	}
24767c478bd9Sstevel@tonic-gate 
24771d6b7ad8Srie /*
24781d6b7ad8Srie  * Define a local buffer size for building a numeric value - large enough to
24791d6b7ad8Srie  * hold a 64-bit value.
24801d6b7ad8Srie  */
2481051d39bbSrie #define	NUM_SIZE	22
24821d6b7ad8Srie 
24837c478bd9Sstevel@tonic-gate size_t
24847c478bd9Sstevel@tonic-gate doprf(const char *format, va_list args, Prfbuf *prf)
24857c478bd9Sstevel@tonic-gate {
24867c478bd9Sstevel@tonic-gate 	char	c;
24877c478bd9Sstevel@tonic-gate 	char	*bp = prf->pr_cur;
24887c478bd9Sstevel@tonic-gate 	char	*bufend = prf->pr_buf + prf->pr_len;
24897c478bd9Sstevel@tonic-gate 	size_t	bufsiz = prf->pr_len;
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	while ((c = *format++) != '\0') {
24927c478bd9Sstevel@tonic-gate 		if (c != '%') {
24937c478bd9Sstevel@tonic-gate 			PUTC(c);
24947c478bd9Sstevel@tonic-gate 		} else {
24957c478bd9Sstevel@tonic-gate 			int	base = 0, flag = 0, width = 0, prec = 0;
24967c478bd9Sstevel@tonic-gate 			size_t	_i;
24977c478bd9Sstevel@tonic-gate 			int	_c, _n;
24987c478bd9Sstevel@tonic-gate 			char	*_s;
24997c478bd9Sstevel@tonic-gate 			int	ls = 0;
25007c478bd9Sstevel@tonic-gate again:
25017c478bd9Sstevel@tonic-gate 			c = *format++;
25027c478bd9Sstevel@tonic-gate 			switch (c) {
25037c478bd9Sstevel@tonic-gate 			case '-':
25047c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_MINUS;
25057c478bd9Sstevel@tonic-gate 				goto again;
25067c478bd9Sstevel@tonic-gate 			case '#':
25077c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_SHARP;
25087c478bd9Sstevel@tonic-gate 				goto again;
25097c478bd9Sstevel@tonic-gate 			case '.':
25107c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_DOTSEEN;
25117c478bd9Sstevel@tonic-gate 				goto again;
25127c478bd9Sstevel@tonic-gate 			case '0':
25137c478bd9Sstevel@tonic-gate 			case '1':
25147c478bd9Sstevel@tonic-gate 			case '2':
25157c478bd9Sstevel@tonic-gate 			case '3':
25167c478bd9Sstevel@tonic-gate 			case '4':
25177c478bd9Sstevel@tonic-gate 			case '5':
25187c478bd9Sstevel@tonic-gate 			case '6':
25197c478bd9Sstevel@tonic-gate 			case '7':
25207c478bd9Sstevel@tonic-gate 			case '8':
25217c478bd9Sstevel@tonic-gate 			case '9':
25227c478bd9Sstevel@tonic-gate 				if (flag & FLG_UT_DOTSEEN)
25237c478bd9Sstevel@tonic-gate 					prec = (prec * 10) + c - '0';
25247c478bd9Sstevel@tonic-gate 				else
25257c478bd9Sstevel@tonic-gate 					width = (width * 10) + c - '0';
25267c478bd9Sstevel@tonic-gate 				goto again;
25277c478bd9Sstevel@tonic-gate 			case 'x':
25287c478bd9Sstevel@tonic-gate 			case 'X':
25297c478bd9Sstevel@tonic-gate 				base = 16;
25307c478bd9Sstevel@tonic-gate 				break;
25317c478bd9Sstevel@tonic-gate 			case 'd':
25327c478bd9Sstevel@tonic-gate 			case 'D':
25337c478bd9Sstevel@tonic-gate 			case 'u':
25347c478bd9Sstevel@tonic-gate 				base = 10;
25357c478bd9Sstevel@tonic-gate 				flag &= ~FLG_UT_SHARP;
25367c478bd9Sstevel@tonic-gate 				break;
25377c478bd9Sstevel@tonic-gate 			case 'l':
25387c478bd9Sstevel@tonic-gate 				base = 10;
25397c478bd9Sstevel@tonic-gate 				ls++; /* number of l's (long or long long) */
25407c478bd9Sstevel@tonic-gate 				if ((*format == 'l') ||
25417c478bd9Sstevel@tonic-gate 				    (*format == 'd') || (*format == 'D') ||
25427c478bd9Sstevel@tonic-gate 				    (*format == 'x') || (*format == 'X') ||
254320272c2eSAli Bahrami 				    (*format == 'o') || (*format == 'O') ||
254420272c2eSAli Bahrami 				    (*format == 'u') || (*format == 'U'))
25457c478bd9Sstevel@tonic-gate 					goto again;
25467c478bd9Sstevel@tonic-gate 				break;
25477c478bd9Sstevel@tonic-gate 			case 'o':
25487c478bd9Sstevel@tonic-gate 			case 'O':
25497c478bd9Sstevel@tonic-gate 				base = 8;
25507c478bd9Sstevel@tonic-gate 				break;
25517c478bd9Sstevel@tonic-gate 			case 'c':
25527c478bd9Sstevel@tonic-gate 				_c = va_arg(args, int);
25537c478bd9Sstevel@tonic-gate 
25547c478bd9Sstevel@tonic-gate 				for (_i = 24; _i > 0; _i -= 8) {
25557c478bd9Sstevel@tonic-gate 					if ((c = ((_c >> _i) & 0x7f)) != 0) {
25567c478bd9Sstevel@tonic-gate 						PUTC(c);
25577c478bd9Sstevel@tonic-gate 					}
25587c478bd9Sstevel@tonic-gate 				}
25597c478bd9Sstevel@tonic-gate 				if ((c = ((_c >> _i) & 0x7f)) != 0) {
25607c478bd9Sstevel@tonic-gate 					PUTC(c);
25617c478bd9Sstevel@tonic-gate 				}
25627c478bd9Sstevel@tonic-gate 				break;
25637c478bd9Sstevel@tonic-gate 			case 's':
25647c478bd9Sstevel@tonic-gate 				_s = va_arg(args, char *);
25657c478bd9Sstevel@tonic-gate 				_i = strlen(_s);
25667c478bd9Sstevel@tonic-gate 				/* LINTED */
25677c478bd9Sstevel@tonic-gate 				_n = (int)(width - _i);
25687c478bd9Sstevel@tonic-gate 				if (!prec)
25697c478bd9Sstevel@tonic-gate 					/* LINTED */
25707c478bd9Sstevel@tonic-gate 					prec = (int)_i;
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
25737c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
25747c478bd9Sstevel@tonic-gate 						PUTC(' ');
25757c478bd9Sstevel@tonic-gate 				}
25767c478bd9Sstevel@tonic-gate 				while (((c = *_s++) != 0) && prec--) {
25777c478bd9Sstevel@tonic-gate 					PUTC(c);
25787c478bd9Sstevel@tonic-gate 				}
25797c478bd9Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
25807c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
25817c478bd9Sstevel@tonic-gate 						PUTC(' ');
25827c478bd9Sstevel@tonic-gate 				}
25837c478bd9Sstevel@tonic-gate 				break;
25847c478bd9Sstevel@tonic-gate 			case '%':
25857c478bd9Sstevel@tonic-gate 				PUTC('%');
25867c478bd9Sstevel@tonic-gate 				break;
25877c478bd9Sstevel@tonic-gate 			default:
25887c478bd9Sstevel@tonic-gate 				break;
25897c478bd9Sstevel@tonic-gate 			}
25907c478bd9Sstevel@tonic-gate 
25917c478bd9Sstevel@tonic-gate 			/*
25927c478bd9Sstevel@tonic-gate 			 * Numeric processing
25937c478bd9Sstevel@tonic-gate 			 */
25947c478bd9Sstevel@tonic-gate 			if (base) {
25951d6b7ad8Srie 				char		local[NUM_SIZE];
25961d6b7ad8Srie 				size_t		ssize = 0, psize = 0;
25977c478bd9Sstevel@tonic-gate 				const char	*string =
2598a953e2b1Srie 				    MSG_ORIG(MSG_STR_HEXNUM);
25997c478bd9Sstevel@tonic-gate 				const char	*prefix =
2600a953e2b1Srie 				    MSG_ORIG(MSG_STR_EMPTY);
26017c478bd9Sstevel@tonic-gate 				u_longlong_t	num;
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 				switch (ls) {
26047c478bd9Sstevel@tonic-gate 				case 0:	/* int */
26057c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)
26067c478bd9Sstevel@tonic-gate 					    va_arg(args, uint_t);
26077c478bd9Sstevel@tonic-gate 					break;
26087c478bd9Sstevel@tonic-gate 				case 1:	/* long */
26097c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)
26107c478bd9Sstevel@tonic-gate 					    va_arg(args, ulong_t);
26117c478bd9Sstevel@tonic-gate 					break;
26127c478bd9Sstevel@tonic-gate 				case 2:	/* long long */
26137c478bd9Sstevel@tonic-gate 					num = va_arg(args, u_longlong_t);
26147c478bd9Sstevel@tonic-gate 					break;
26157c478bd9Sstevel@tonic-gate 				}
26167c478bd9Sstevel@tonic-gate 
26177c478bd9Sstevel@tonic-gate 				if (flag & FLG_UT_SHARP) {
26187c478bd9Sstevel@tonic-gate 					if (base == 16) {
26197c478bd9Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_HEX);
26207c478bd9Sstevel@tonic-gate 						psize = 2;
26217c478bd9Sstevel@tonic-gate 					} else {
26227c478bd9Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_ZERO);
26237c478bd9Sstevel@tonic-gate 						psize = 1;
26247c478bd9Sstevel@tonic-gate 					}
26257c478bd9Sstevel@tonic-gate 				}
26267c478bd9Sstevel@tonic-gate 				if ((base == 10) && (long)num < 0) {
26277c478bd9Sstevel@tonic-gate 					prefix = MSG_ORIG(MSG_STR_NEGATE);
26287c478bd9Sstevel@tonic-gate 					psize = MSG_STR_NEGATE_SIZE;
26297c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)(-(longlong_t)num);
26307c478bd9Sstevel@tonic-gate 				}
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 				/*
26337c478bd9Sstevel@tonic-gate 				 * Convert the numeric value into a local
26347c478bd9Sstevel@tonic-gate 				 * string (stored in reverse order).
26357c478bd9Sstevel@tonic-gate 				 */
26367c478bd9Sstevel@tonic-gate 				_s = local;
26377c478bd9Sstevel@tonic-gate 				do {
26387c478bd9Sstevel@tonic-gate 					*_s++ = string[num % base];
26397c478bd9Sstevel@tonic-gate 					num /= base;
26407c478bd9Sstevel@tonic-gate 					ssize++;
26417c478bd9Sstevel@tonic-gate 				} while (num);
26427c478bd9Sstevel@tonic-gate 
26431d6b7ad8Srie 				ASSERT(ssize < sizeof (local));
26441d6b7ad8Srie 
26457c478bd9Sstevel@tonic-gate 				/*
26467c478bd9Sstevel@tonic-gate 				 * Provide any precision or width padding.
26477c478bd9Sstevel@tonic-gate 				 */
26487c478bd9Sstevel@tonic-gate 				if (prec) {
26497c478bd9Sstevel@tonic-gate 					/* LINTED */
26507c478bd9Sstevel@tonic-gate 					_n = (int)(prec - ssize);
26511d6b7ad8Srie 					while ((_n-- > 0) &&
26521d6b7ad8Srie 					    (ssize < sizeof (local))) {
26537c478bd9Sstevel@tonic-gate 						*_s++ = '0';
26547c478bd9Sstevel@tonic-gate 						ssize++;
26557c478bd9Sstevel@tonic-gate 					}
26567c478bd9Sstevel@tonic-gate 				}
26577c478bd9Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
26587c478bd9Sstevel@tonic-gate 					/* LINTED */
26597c478bd9Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
26607c478bd9Sstevel@tonic-gate 					while (_n-- > 0) {
26617c478bd9Sstevel@tonic-gate 						PUTC(' ');
26627c478bd9Sstevel@tonic-gate 					}
26637c478bd9Sstevel@tonic-gate 				}
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 				/*
26667c478bd9Sstevel@tonic-gate 				 * Print any prefix and the numeric string
26677c478bd9Sstevel@tonic-gate 				 */
26687c478bd9Sstevel@tonic-gate 				while (*prefix)
26697c478bd9Sstevel@tonic-gate 					PUTC(*prefix++);
26707c478bd9Sstevel@tonic-gate 				do {
26717c478bd9Sstevel@tonic-gate 					PUTC(*--_s);
26727c478bd9Sstevel@tonic-gate 				} while (_s > local);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 				/*
26757c478bd9Sstevel@tonic-gate 				 * Provide any width padding.
26767c478bd9Sstevel@tonic-gate 				 */
26777c478bd9Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
26787c478bd9Sstevel@tonic-gate 					/* LINTED */
26797c478bd9Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
26807c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
26817c478bd9Sstevel@tonic-gate 						PUTC(' ');
26827c478bd9Sstevel@tonic-gate 				}
26837c478bd9Sstevel@tonic-gate 			}
26847c478bd9Sstevel@tonic-gate 		}
26857c478bd9Sstevel@tonic-gate 	}
268610a4fa49Srie 
26877c478bd9Sstevel@tonic-gate 	PUTC('\0');
26887c478bd9Sstevel@tonic-gate 	prf->pr_cur = bp;
26897c478bd9Sstevel@tonic-gate 	return (1);
26907c478bd9Sstevel@tonic-gate }
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate static int
26937c478bd9Sstevel@tonic-gate doprintf(const char *format, va_list args, Prfbuf *prf)
26947c478bd9Sstevel@tonic-gate {
26957c478bd9Sstevel@tonic-gate 	char	*ocur = prf->pr_cur;
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 	if (doprf(format, args, prf) == 0)
26987c478bd9Sstevel@tonic-gate 		return (0);
26997c478bd9Sstevel@tonic-gate 	/* LINTED */
27007c478bd9Sstevel@tonic-gate 	return ((int)(prf->pr_cur - ocur));
27017c478bd9Sstevel@tonic-gate }
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate /* VARARGS2 */
27047c478bd9Sstevel@tonic-gate int
27057c478bd9Sstevel@tonic-gate sprintf(char *buf, const char *format, ...)
27067c478bd9Sstevel@tonic-gate {
27077c478bd9Sstevel@tonic-gate 	va_list	args;
27087c478bd9Sstevel@tonic-gate 	int	len;
27097c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 	va_start(args, format);
27127c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
27137c478bd9Sstevel@tonic-gate 	prf.pr_len = 0;
27147c478bd9Sstevel@tonic-gate 	prf.pr_fd = -1;
27157c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
27167c478bd9Sstevel@tonic-gate 	va_end(args);
27177c478bd9Sstevel@tonic-gate 
27187c478bd9Sstevel@tonic-gate 	/*
27197c478bd9Sstevel@tonic-gate 	 * sprintf() return value excludes the terminating null byte.
27207c478bd9Sstevel@tonic-gate 	 */
27217c478bd9Sstevel@tonic-gate 	return (len - 1);
27227c478bd9Sstevel@tonic-gate }
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate /* VARARGS3 */
27257c478bd9Sstevel@tonic-gate int
27267c478bd9Sstevel@tonic-gate snprintf(char *buf, size_t n, const char *format, ...)
27277c478bd9Sstevel@tonic-gate {
27287c478bd9Sstevel@tonic-gate 	va_list	args;
27297c478bd9Sstevel@tonic-gate 	int	len;
27307c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
27317c478bd9Sstevel@tonic-gate 
27327c478bd9Sstevel@tonic-gate 	va_start(args, format);
27337c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
27347c478bd9Sstevel@tonic-gate 	prf.pr_len = n;
27357c478bd9Sstevel@tonic-gate 	prf.pr_fd = -1;
27367c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
27377c478bd9Sstevel@tonic-gate 	va_end(args);
27387c478bd9Sstevel@tonic-gate 
27397c478bd9Sstevel@tonic-gate 	return (len);
27407c478bd9Sstevel@tonic-gate }
27417c478bd9Sstevel@tonic-gate 
27427c478bd9Sstevel@tonic-gate /* VARARGS2 */
27437c478bd9Sstevel@tonic-gate int
27447c478bd9Sstevel@tonic-gate bufprint(Prfbuf *prf, const char *format, ...)
27457c478bd9Sstevel@tonic-gate {
27467c478bd9Sstevel@tonic-gate 	va_list	args;
27477c478bd9Sstevel@tonic-gate 	int	len;
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 	va_start(args, format);
27507c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, prf);
27517c478bd9Sstevel@tonic-gate 	va_end(args);
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 	return (len);
27547c478bd9Sstevel@tonic-gate }
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
27577c478bd9Sstevel@tonic-gate int
27587c478bd9Sstevel@tonic-gate printf(const char *format, ...)
27597c478bd9Sstevel@tonic-gate {
27607c478bd9Sstevel@tonic-gate 	va_list	args;
27617c478bd9Sstevel@tonic-gate 	char 	buffer[ERRSIZE];
27627c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	va_start(args, format);
27657c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buffer;
27667c478bd9Sstevel@tonic-gate 	prf.pr_len = ERRSIZE;
27677c478bd9Sstevel@tonic-gate 	prf.pr_fd = 1;
27687c478bd9Sstevel@tonic-gate 	(void) doprf(format, args, &prf);
27697c478bd9Sstevel@tonic-gate 	va_end(args);
27707c478bd9Sstevel@tonic-gate 	/*
27717c478bd9Sstevel@tonic-gate 	 * Trim trailing '\0' form buffer
27727c478bd9Sstevel@tonic-gate 	 */
27737c478bd9Sstevel@tonic-gate 	prf.pr_cur--;
27747c478bd9Sstevel@tonic-gate 	return (dowrite(&prf));
27757c478bd9Sstevel@tonic-gate }
27767c478bd9Sstevel@tonic-gate 
277743d7826aSRod Evans static char	errbuf[ERRSIZE], *nextptr = errbuf, *prevptr = NULL;
27787c478bd9Sstevel@tonic-gate 
277956deab07SRod Evans /*
278056deab07SRod Evans  * All error messages go through eprintf().  During process initialization,
278156deab07SRod Evans  * these messages are directed to the standard error, however once control has
278256deab07SRod Evans  * been passed to the applications code these messages are stored in an internal
278356deab07SRod Evans  * buffer for use with dlerror().  Note, fatal error conditions that may occur
278456deab07SRod Evans  * while running the application will still cause a standard error message, see
278556deab07SRod Evans  * rtldexit() in this file for details.
278656deab07SRod Evans  * The RT_FL_APPLIC flag serves to indicate the transition between process
278756deab07SRod Evans  * initialization and when the applications code is running.
278856deab07SRod Evans  */
27895aefb655Srie /*PRINTFLIKE3*/
27907c478bd9Sstevel@tonic-gate void
27915aefb655Srie eprintf(Lm_list *lml, Error error, const char *format, ...)
27927c478bd9Sstevel@tonic-gate {
27937c478bd9Sstevel@tonic-gate 	va_list		args;
27947c478bd9Sstevel@tonic-gate 	int		overflow = 0;
27957c478bd9Sstevel@tonic-gate 	static int	lock = 0;
27967c478bd9Sstevel@tonic-gate 	Prfbuf		prf;
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	if (lock || (nextptr == (errbuf + ERRSIZE)))
27997c478bd9Sstevel@tonic-gate 		return;
28007c478bd9Sstevel@tonic-gate 
28017c478bd9Sstevel@tonic-gate 	/*
28027c478bd9Sstevel@tonic-gate 	 * Note: this lock is here to prevent the same thread from recursively
28037c478bd9Sstevel@tonic-gate 	 * entering itself during a eprintf.  ie: during eprintf malloc() fails
28047c478bd9Sstevel@tonic-gate 	 * and we try and call eprintf ... and then malloc() fails ....
28057c478bd9Sstevel@tonic-gate 	 */
28067c478bd9Sstevel@tonic-gate 	lock = 1;
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	/*
28097c478bd9Sstevel@tonic-gate 	 * If we have completed startup initialization, all error messages
28107c478bd9Sstevel@tonic-gate 	 * must be saved.  These are reported through dlerror().  If we're
28117c478bd9Sstevel@tonic-gate 	 * still in the initialization stage, output the error directly and
28127c478bd9Sstevel@tonic-gate 	 * add a newline.
28137c478bd9Sstevel@tonic-gate 	 */
28147c478bd9Sstevel@tonic-gate 	va_start(args, format);
28157c478bd9Sstevel@tonic-gate 
28167c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = nextptr;
28177c478bd9Sstevel@tonic-gate 	prf.pr_len = ERRSIZE - (nextptr - errbuf);
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_APPLIC))
28207c478bd9Sstevel@tonic-gate 		prf.pr_fd = 2;
28217c478bd9Sstevel@tonic-gate 	else
28227c478bd9Sstevel@tonic-gate 		prf.pr_fd = -1;
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	if (error > ERR_NONE) {
28257c478bd9Sstevel@tonic-gate 		if ((error == ERR_FATAL) && (rtld_flags2 & RT_FL2_FTL2WARN))
28267c478bd9Sstevel@tonic-gate 			error = ERR_WARNING;
28277c478bd9Sstevel@tonic-gate 		if (error == ERR_WARNING) {
282843d7826aSRod Evans 			if (err_strs[ERR_WARNING] == NULL)
2829a953e2b1Srie 				err_strs[ERR_WARNING] =
2830a953e2b1Srie 				    MSG_INTL(MSG_ERR_WARNING);
28317c478bd9Sstevel@tonic-gate 		} else if (error == ERR_FATAL) {
283243d7826aSRod Evans 			if (err_strs[ERR_FATAL] == NULL)
2833a953e2b1Srie 				err_strs[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
28347c478bd9Sstevel@tonic-gate 		} else if (error == ERR_ELF) {
283543d7826aSRod Evans 			if (err_strs[ERR_ELF] == NULL)
2836a953e2b1Srie 				err_strs[ERR_ELF] = MSG_INTL(MSG_ERR_ELF);
28377c478bd9Sstevel@tonic-gate 		}
283841072f3cSrie 		if (procname) {
283941072f3cSrie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR1),
284041072f3cSrie 			    rtldname, procname, err_strs[error]) == 0)
284141072f3cSrie 				overflow = 1;
28427c478bd9Sstevel@tonic-gate 		} else {
284341072f3cSrie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
284441072f3cSrie 			    rtldname, err_strs[error]) == 0)
284541072f3cSrie 				overflow = 1;
284641072f3cSrie 		}
284741072f3cSrie 		if (overflow == 0) {
28487c478bd9Sstevel@tonic-gate 			/*
28497c478bd9Sstevel@tonic-gate 			 * Remove the terminating '\0'.
28507c478bd9Sstevel@tonic-gate 			 */
28517c478bd9Sstevel@tonic-gate 			prf.pr_cur--;
28527c478bd9Sstevel@tonic-gate 		}
28537c478bd9Sstevel@tonic-gate 	}
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 	if ((overflow == 0) && doprf(format, args, &prf) == 0)
28567c478bd9Sstevel@tonic-gate 		overflow = 1;
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	/*
28597c478bd9Sstevel@tonic-gate 	 * If this is an ELF error, it will have been generated by a support
28607c478bd9Sstevel@tonic-gate 	 * object that has a dependency on libelf.  ld.so.1 doesn't generate any
28617c478bd9Sstevel@tonic-gate 	 * ELF error messages as it doesn't interact with libelf.  Determine the
28627c478bd9Sstevel@tonic-gate 	 * ELF error string.
28637c478bd9Sstevel@tonic-gate 	 */
28647c478bd9Sstevel@tonic-gate 	if ((overflow == 0) && (error == ERR_ELF)) {
28657c478bd9Sstevel@tonic-gate 		static int		(*elfeno)() = 0;
28667c478bd9Sstevel@tonic-gate 		static const char	*(*elfemg)();
28677c478bd9Sstevel@tonic-gate 		const char		*emsg;
28687c478bd9Sstevel@tonic-gate 		Rt_map			*dlmp, *lmp = lml_rtld.lm_head;
28697c478bd9Sstevel@tonic-gate 
28707c478bd9Sstevel@tonic-gate 		if (NEXT(lmp) && (elfeno == 0)) {
28717c478bd9Sstevel@tonic-gate 			if (((elfemg = (const char *(*)())dlsym_intn(RTLD_NEXT,
287243d7826aSRod Evans 			    MSG_ORIG(MSG_SYM_ELFERRMSG),
287343d7826aSRod Evans 			    lmp, &dlmp)) == NULL) ||
28747c478bd9Sstevel@tonic-gate 			    ((elfeno = (int (*)())dlsym_intn(RTLD_NEXT,
287543d7826aSRod Evans 			    MSG_ORIG(MSG_SYM_ELFERRNO), lmp, &dlmp)) == NULL))
28767c478bd9Sstevel@tonic-gate 				elfeno = 0;
28777c478bd9Sstevel@tonic-gate 		}
28787c478bd9Sstevel@tonic-gate 
28797c478bd9Sstevel@tonic-gate 		/*
28807c478bd9Sstevel@tonic-gate 		 * Lookup the message; equivalent to elf_errmsg(elf_errno()).
28817c478bd9Sstevel@tonic-gate 		 */
288243d7826aSRod Evans 		if (elfeno && ((emsg = (* elfemg)((* elfeno)())) != NULL)) {
28837c478bd9Sstevel@tonic-gate 			prf.pr_cur--;
28847c478bd9Sstevel@tonic-gate 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
28857c478bd9Sstevel@tonic-gate 			    emsg) == 0)
28867c478bd9Sstevel@tonic-gate 				overflow = 1;
28877c478bd9Sstevel@tonic-gate 		}
28887c478bd9Sstevel@tonic-gate 	}
28897c478bd9Sstevel@tonic-gate 
28907c478bd9Sstevel@tonic-gate 	/*
28917c478bd9Sstevel@tonic-gate 	 * Push out any message that's been built.  Note, in the case of an
28927c478bd9Sstevel@tonic-gate 	 * overflow condition, this message may be incomplete, in which case
28937c478bd9Sstevel@tonic-gate 	 * make sure any partial string is null terminated.
28947c478bd9Sstevel@tonic-gate 	 */
28957c478bd9Sstevel@tonic-gate 	if ((rtld_flags & (RT_FL_APPLIC | RT_FL_SILENCERR)) == 0) {
28967c478bd9Sstevel@tonic-gate 		*(prf.pr_cur - 1) = '\n';
28977c478bd9Sstevel@tonic-gate 		(void) dowrite(&prf);
28987c478bd9Sstevel@tonic-gate 	}
289956deab07SRod Evans 	if (overflow)
290056deab07SRod Evans 		*(prf.pr_cur - 1) = '\0';
29017c478bd9Sstevel@tonic-gate 
29025aefb655Srie 	DBG_CALL(Dbg_util_str(lml, nextptr));
29037c478bd9Sstevel@tonic-gate 	va_end(args);
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	/*
29067c478bd9Sstevel@tonic-gate 	 * Determine if there was insufficient space left in the buffer to
29077c478bd9Sstevel@tonic-gate 	 * complete the message.  If so, we'll have printed out as much as had
29087c478bd9Sstevel@tonic-gate 	 * been processed if we're not yet executing the application.
29097c478bd9Sstevel@tonic-gate 	 * Otherwise, there will be some debugging diagnostic indicating
29107c478bd9Sstevel@tonic-gate 	 * as much of the error message as possible.  Write out a final buffer
29117c478bd9Sstevel@tonic-gate 	 * overflow diagnostic - unlocalized, so we don't chance more errors.
29127c478bd9Sstevel@tonic-gate 	 */
29137c478bd9Sstevel@tonic-gate 	if (overflow) {
29147c478bd9Sstevel@tonic-gate 		char	*str = (char *)MSG_INTL(MSG_EMG_BUFOVRFLW);
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) == 0) {
29177c478bd9Sstevel@tonic-gate 			lasterr = str;
29187c478bd9Sstevel@tonic-gate 
29197c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_APPLIC) == 0) {
29207c478bd9Sstevel@tonic-gate 				(void) write(2, str, strlen(str));
29217c478bd9Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
29227c478bd9Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
29237c478bd9Sstevel@tonic-gate 			}
29247c478bd9Sstevel@tonic-gate 		}
29255aefb655Srie 		DBG_CALL(Dbg_util_str(lml, str));
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 		lock = 0;
29287c478bd9Sstevel@tonic-gate 		nextptr = errbuf + ERRSIZE;
29297c478bd9Sstevel@tonic-gate 		return;
29307c478bd9Sstevel@tonic-gate 	}
29317c478bd9Sstevel@tonic-gate 
29327c478bd9Sstevel@tonic-gate 	/*
29337c478bd9Sstevel@tonic-gate 	 * If the application has started, then error messages are being saved
29347c478bd9Sstevel@tonic-gate 	 * for retrieval by dlerror(), or possible flushing from rtldexit() in
29357c478bd9Sstevel@tonic-gate 	 * the case of a fatal error.  In this case, establish the next error
29367c478bd9Sstevel@tonic-gate 	 * pointer.  If we haven't started the application, the whole message
29377c478bd9Sstevel@tonic-gate 	 * buffer can be reused.
29387c478bd9Sstevel@tonic-gate 	 */
29397c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SILENCERR) == 0) {
29407c478bd9Sstevel@tonic-gate 		lasterr = nextptr;
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 		/*
29437c478bd9Sstevel@tonic-gate 		 * Note, should we encounter an error such as ENOMEM, there may
29447c478bd9Sstevel@tonic-gate 		 * be a number of the same error messages (ie. an operation
29457c478bd9Sstevel@tonic-gate 		 * fails with ENOMEM, and then the attempts to construct the
29467c478bd9Sstevel@tonic-gate 		 * error message itself, which incurs additional ENOMEM errors).
29477c478bd9Sstevel@tonic-gate 		 * Compare any previous error message with the one we've just
29487c478bd9Sstevel@tonic-gate 		 * created to prevent any duplication clutter.
29497c478bd9Sstevel@tonic-gate 		 */
29507c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) &&
295143d7826aSRod Evans 		    ((prevptr == NULL) || (strcmp(prevptr, nextptr) != 0))) {
29527c478bd9Sstevel@tonic-gate 			prevptr = nextptr;
29537c478bd9Sstevel@tonic-gate 			nextptr = prf.pr_cur;
29547c478bd9Sstevel@tonic-gate 			*nextptr = '\0';
29557c478bd9Sstevel@tonic-gate 		}
29567c478bd9Sstevel@tonic-gate 	}
29577c478bd9Sstevel@tonic-gate 	lock = 0;
29587c478bd9Sstevel@tonic-gate }
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate #if	DEBUG
29627c478bd9Sstevel@tonic-gate /*
296356deab07SRod Evans  * Provide assfail() for ASSERT() statements.  See <sys/debug.h> for further
296456deab07SRod Evans  * details.
29657c478bd9Sstevel@tonic-gate  */
29667c478bd9Sstevel@tonic-gate int
29677c478bd9Sstevel@tonic-gate assfail(const char *a, const char *f, int l)
29687c478bd9Sstevel@tonic-gate {
29697c478bd9Sstevel@tonic-gate 	(void) printf("assertion failed: %s, file: %s, line: %d\n", a, f, l);
29707c478bd9Sstevel@tonic-gate 	(void) _lwp_kill(_lwp_self(), SIGABRT);
29717c478bd9Sstevel@tonic-gate 	return (0);
29727c478bd9Sstevel@tonic-gate }
29737c478bd9Sstevel@tonic-gate #endif
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate /*
29767c478bd9Sstevel@tonic-gate  * Exit.  If we arrive here with a non zero status it's because of a fatal
29777c478bd9Sstevel@tonic-gate  * error condition (most commonly a relocation error).  If the application has
29787c478bd9Sstevel@tonic-gate  * already had control, then the actual fatal error message will have been
29797c478bd9Sstevel@tonic-gate  * recorded in the dlerror() message buffer.  Print the message before really
29807c478bd9Sstevel@tonic-gate  * exiting.
29817c478bd9Sstevel@tonic-gate  */
29827c478bd9Sstevel@tonic-gate void
29837c478bd9Sstevel@tonic-gate rtldexit(Lm_list * lml, int status)
29847c478bd9Sstevel@tonic-gate {
29857c478bd9Sstevel@tonic-gate 	if (status) {
29867c478bd9Sstevel@tonic-gate 		if (rtld_flags & RT_FL_APPLIC) {
29877c478bd9Sstevel@tonic-gate 			/*
29887c478bd9Sstevel@tonic-gate 			 * If the error buffer has been used, write out all
29897c478bd9Sstevel@tonic-gate 			 * pending messages - lasterr is simply a pointer to
29907c478bd9Sstevel@tonic-gate 			 * the last message in this buffer.  However, if the
29917c478bd9Sstevel@tonic-gate 			 * buffer couldn't be created at all, lasterr points
29927c478bd9Sstevel@tonic-gate 			 * to a constant error message string.
29937c478bd9Sstevel@tonic-gate 			 */
29947c478bd9Sstevel@tonic-gate 			if (*errbuf) {
29957c478bd9Sstevel@tonic-gate 				char	*errptr = errbuf;
29967c478bd9Sstevel@tonic-gate 				char	*errend = errbuf + ERRSIZE;
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 				while ((errptr < errend) && *errptr) {
29997c478bd9Sstevel@tonic-gate 					size_t	size = strlen(errptr);
30007c478bd9Sstevel@tonic-gate 					(void) write(2, errptr, size);
30017c478bd9Sstevel@tonic-gate 					(void) write(2, MSG_ORIG(MSG_STR_NL),
30027c478bd9Sstevel@tonic-gate 					    MSG_STR_NL_SIZE);
30037c478bd9Sstevel@tonic-gate 					errptr += (size + 1);
30047c478bd9Sstevel@tonic-gate 				}
30057c478bd9Sstevel@tonic-gate 			}
30067c478bd9Sstevel@tonic-gate 			if (lasterr && ((lasterr < errbuf) ||
30077c478bd9Sstevel@tonic-gate 			    (lasterr > (errbuf + ERRSIZE)))) {
30087c478bd9Sstevel@tonic-gate 				(void) write(2, lasterr, strlen(lasterr));
30097c478bd9Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
30107c478bd9Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
30117c478bd9Sstevel@tonic-gate 			}
30127c478bd9Sstevel@tonic-gate 		}
30138cd45542Sraf 		leave(lml, 0);
30147c478bd9Sstevel@tonic-gate 		(void) _lwp_kill(_lwp_self(), killsig);
30157c478bd9Sstevel@tonic-gate 	}
30167c478bd9Sstevel@tonic-gate 	_exit(status);
30177c478bd9Sstevel@tonic-gate }
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate /*
302056deab07SRod Evans  * Map anonymous memory via MAP_ANON (added in Solaris 8).
30217c478bd9Sstevel@tonic-gate  */
302256deab07SRod Evans void *
30235aefb655Srie dz_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
30247c478bd9Sstevel@tonic-gate {
30257c478bd9Sstevel@tonic-gate 	caddr_t	va;
30267c478bd9Sstevel@tonic-gate 
302756deab07SRod Evans 	if ((va = (caddr_t)mmap(addr, len, prot,
302856deab07SRod Evans 	    (flags | MAP_ANON), -1, 0)) == MAP_FAILED) {
302956deab07SRod Evans 		int	err = errno;
303056deab07SRod Evans 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON),
303156deab07SRod Evans 		    strerror(err));
30327c478bd9Sstevel@tonic-gate 		return (MAP_FAILED);
30337c478bd9Sstevel@tonic-gate 	}
30347c478bd9Sstevel@tonic-gate 	return (va);
30357c478bd9Sstevel@tonic-gate }
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate static int	nu_fd = FD_UNAVAIL;
30387c478bd9Sstevel@tonic-gate 
303956deab07SRod Evans void *
30405aefb655Srie nu_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
30417c478bd9Sstevel@tonic-gate {
30427c478bd9Sstevel@tonic-gate 	caddr_t	va;
30437c478bd9Sstevel@tonic-gate 	int	err;
30447c478bd9Sstevel@tonic-gate 
30457c478bd9Sstevel@tonic-gate 	if (nu_fd == FD_UNAVAIL) {
30467c478bd9Sstevel@tonic-gate 		if ((nu_fd = open(MSG_ORIG(MSG_PTH_DEVNULL),
30477c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
30487c478bd9Sstevel@tonic-gate 			err = errno;
30495aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
30507c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
30517c478bd9Sstevel@tonic-gate 			return (MAP_FAILED);
30527c478bd9Sstevel@tonic-gate 		}
30537c478bd9Sstevel@tonic-gate 	}
30547c478bd9Sstevel@tonic-gate 
30557c478bd9Sstevel@tonic-gate 	if ((va = (caddr_t)mmap(addr, len, prot, flags, nu_fd, 0)) ==
30567c478bd9Sstevel@tonic-gate 	    MAP_FAILED) {
30577c478bd9Sstevel@tonic-gate 		err = errno;
30585aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
30597c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
30607c478bd9Sstevel@tonic-gate 	}
30617c478bd9Sstevel@tonic-gate 	return (va);
30627c478bd9Sstevel@tonic-gate }
30637c478bd9Sstevel@tonic-gate 
30647c478bd9Sstevel@tonic-gate /*
306575e7992aSrie  * Generic entry point from user code - simply grabs a lock, and bumps the
306675e7992aSrie  * entrance count.
30677c478bd9Sstevel@tonic-gate  */
30687c478bd9Sstevel@tonic-gate int
30698cd45542Sraf enter(int flags)
30707c478bd9Sstevel@tonic-gate {
30718cd45542Sraf 	if (rt_bind_guard(THR_FLG_RTLD | thr_flg_nolock | flags)) {
30728cd45542Sraf 		if (!thr_flg_nolock)
30738cd45542Sraf 			(void) rt_mutex_lock(&rtldlock);
30742017c965SRod Evans 		if (rtld_flags & RT_FL_OPERATION) {
30759aa23310Srie 			ld_entry_cnt++;
30762017c965SRod Evans 
30772017c965SRod Evans 			/*
30782017c965SRod Evans 			 * Reset the diagnostic time information for each new
30792017c965SRod Evans 			 * "operation".  Thus timing diagnostics are relative
30802017c965SRod Evans 			 * to entering ld.so.1.
30812017c965SRod Evans 			 */
30822017c965SRod Evans 			if (DBG_ISTIME() &&
30832017c965SRod Evans 			    (gettimeofday(&DBG_TOTALTIME, NULL) == 0)) {
30842017c965SRod Evans 				DBG_DELTATIME = DBG_TOTALTIME;
30852017c965SRod Evans 				DBG_ONRESET();
30862017c965SRod Evans 			}
30872017c965SRod Evans 		}
30887c478bd9Sstevel@tonic-gate 		return (1);
30897c478bd9Sstevel@tonic-gate 	}
30907c478bd9Sstevel@tonic-gate 	return (0);
30917c478bd9Sstevel@tonic-gate }
30927c478bd9Sstevel@tonic-gate 
30939aa23310Srie /*
30949aa23310Srie  * Determine whether a search path has been used.
30959aa23310Srie  */
30969aa23310Srie static void
309756deab07SRod Evans is_path_used(Lm_list *lml, Word unref, int *nl, Alist *alp, const char *obj)
30989aa23310Srie {
309956deab07SRod Evans 	Pdesc	*pdp;
310056deab07SRod Evans 	Aliste	idx;
310156deab07SRod Evans 
310256deab07SRod Evans 	for (ALIST_TRAVERSE(alp, idx, pdp)) {
31039aa23310Srie 		const char	*fmt, *name;
31049aa23310Srie 
310556deab07SRod Evans 		if ((pdp->pd_plen == 0) || (pdp->pd_flags & PD_FLG_USED))
31069aa23310Srie 			continue;
31079aa23310Srie 
31089aa23310Srie 		/*
31099aa23310Srie 		 * If this pathname originated from an expanded token, use the
31109aa23310Srie 		 * original for any diagnostic output.
31119aa23310Srie 		 */
311256deab07SRod Evans 		if ((name = pdp->pd_oname) == NULL)
311356deab07SRod Evans 			name = pdp->pd_pname;
31149aa23310Srie 
31159aa23310Srie 		if (unref == 0) {
31169aa23310Srie 			if ((*nl)++ == 0)
31179aa23310Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
311856deab07SRod Evans 			DBG_CALL(Dbg_unused_path(lml, name, pdp->pd_flags,
311956deab07SRod Evans 			    (pdp->pd_flags & PD_FLG_DUPLICAT), obj));
31209aa23310Srie 			continue;
31219aa23310Srie 		}
31229aa23310Srie 
312356deab07SRod Evans 		if (pdp->pd_flags & LA_SER_LIBPATH) {
312456deab07SRod Evans 			if (pdp->pd_flags & LA_SER_CONFIG) {
312556deab07SRod Evans 				if (pdp->pd_flags & PD_FLG_DUPLICAT)
31269aa23310Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATHC);
31279aa23310Srie 				else
31289aa23310Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATHC);
31299aa23310Srie 			} else {
313056deab07SRod Evans 				if (pdp->pd_flags & PD_FLG_DUPLICAT)
31319aa23310Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATH);
31329aa23310Srie 				else
31339aa23310Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATH);
31349aa23310Srie 			}
313556deab07SRod Evans 		} else if (pdp->pd_flags & LA_SER_RUNPATH) {
31369aa23310Srie 			fmt = MSG_INTL(MSG_USD_RUNPATH);
31379aa23310Srie 		} else
31389aa23310Srie 			continue;
31399aa23310Srie 
31409aa23310Srie 		if ((*nl)++ == 0)
31419aa23310Srie 			(void) printf(MSG_ORIG(MSG_STR_NL));
31429aa23310Srie 		(void) printf(fmt, name, obj);
31439aa23310Srie 	}
31449aa23310Srie }
31459aa23310Srie 
31467c478bd9Sstevel@tonic-gate /*
31477c478bd9Sstevel@tonic-gate  * Generate diagnostics as to whether an object has been used.  A symbolic
31487c478bd9Sstevel@tonic-gate  * reference that gets bound to an object marks it as used.  Dependencies that
31497c478bd9Sstevel@tonic-gate  * are unused when RTLD_NOW is in effect should be removed from future builds
31507c478bd9Sstevel@tonic-gate  * of an object.  Dependencies that are unused without RTLD_NOW in effect are
31517c478bd9Sstevel@tonic-gate  * candidates for lazy-loading.
31529aa23310Srie  *
31537c478bd9Sstevel@tonic-gate  * Unreferenced objects identify objects that are defined as dependencies but
31549aa23310Srie  * are unreferenced by the caller.  These unreferenced objects may however be
31559aa23310Srie  * referenced by other objects within the process, and therefore don't qualify
31569aa23310Srie  * as completely unused.  They are still an unnecessary overhead.
31579aa23310Srie  *
31589aa23310Srie  * Unreferenced runpaths are also captured under ldd -U, or "unused,detail"
31599aa23310Srie  * debugging.
31607c478bd9Sstevel@tonic-gate  */
31617c478bd9Sstevel@tonic-gate void
31627c478bd9Sstevel@tonic-gate unused(Lm_list *lml)
31637c478bd9Sstevel@tonic-gate {
31647c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
31657c478bd9Sstevel@tonic-gate 	int		nl = 0;
31669aa23310Srie 	Word		unref, unuse;
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	/*
31697c478bd9Sstevel@tonic-gate 	 * If we're not tracing unused references or dependencies, or debugging
31707c478bd9Sstevel@tonic-gate 	 * there's nothing to do.
31717c478bd9Sstevel@tonic-gate 	 */
31729aa23310Srie 	unref = lml->lm_flags & LML_FLG_TRC_UNREF;
31739aa23310Srie 	unuse = lml->lm_flags & LML_FLG_TRC_UNUSED;
31747c478bd9Sstevel@tonic-gate 
31759aa23310Srie 	if ((unref == 0) && (unuse == 0) && (DBG_ENABLED == 0))
31767c478bd9Sstevel@tonic-gate 		return;
31777c478bd9Sstevel@tonic-gate 
31789aa23310Srie 	/*
31799aa23310Srie 	 * Detect unused global search paths.
31809aa23310Srie 	 */
31819aa23310Srie 	if (rpl_libdirs)
31829aa23310Srie 		is_path_used(lml, unref, &nl, rpl_libdirs, config->c_name);
31839aa23310Srie 	if (prm_libdirs)
31849aa23310Srie 		is_path_used(lml, unref, &nl, prm_libdirs, config->c_name);
31859aa23310Srie 
31869aa23310Srie 	nl = 0;
31879aa23310Srie 	lmp = lml->lm_head;
31889aa23310Srie 	if (RLIST(lmp))
31899aa23310Srie 		is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
31909aa23310Srie 
31917c478bd9Sstevel@tonic-gate 	/*
31927c478bd9Sstevel@tonic-gate 	 * Traverse the link-maps looking for unreferenced or unused
31937c478bd9Sstevel@tonic-gate 	 * dependencies.  Ignore the first object on a link-map list, as this
31949aa23310Srie 	 * is always used.
31957c478bd9Sstevel@tonic-gate 	 */
31969aa23310Srie 	nl = 0;
3197cb511613SAli Bahrami 	for (lmp = NEXT_RT_MAP(lmp); lmp; lmp = NEXT_RT_MAP(lmp)) {
31989aa23310Srie 		/*
31999aa23310Srie 		 * Determine if this object contains any runpaths that have
32009aa23310Srie 		 * not been used.
32019aa23310Srie 		 */
32029aa23310Srie 		if (RLIST(lmp))
32039aa23310Srie 			is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
32049aa23310Srie 
32057c478bd9Sstevel@tonic-gate 		/*
32067c478bd9Sstevel@tonic-gate 		 * If tracing unreferenced objects, or under debugging,
32077c478bd9Sstevel@tonic-gate 		 * determine whether any of this objects callers haven't
32087c478bd9Sstevel@tonic-gate 		 * referenced it.
32097c478bd9Sstevel@tonic-gate 		 */
32109aa23310Srie 		if (unref || DBG_ENABLED) {
3211cce0e03bSab 			Bnd_desc	*bdp;
3212cce0e03bSab 			Aliste		idx;
32137c478bd9Sstevel@tonic-gate 
3214cce0e03bSab 			for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
32159aa23310Srie 				Rt_map	*clmp;
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 				if (bdp->b_flags & BND_REFER)
32187c478bd9Sstevel@tonic-gate 					continue;
32197c478bd9Sstevel@tonic-gate 
32207c478bd9Sstevel@tonic-gate 				clmp = bdp->b_caller;
32217c478bd9Sstevel@tonic-gate 				if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
32227c478bd9Sstevel@tonic-gate 					continue;
32237c478bd9Sstevel@tonic-gate 
3224a953e2b1Srie 				/* BEGIN CSTYLED */
32257c478bd9Sstevel@tonic-gate 				if (nl++ == 0) {
32269aa23310Srie 					if (unref)
32277c478bd9Sstevel@tonic-gate 					    (void) printf(MSG_ORIG(MSG_STR_NL));
32287c478bd9Sstevel@tonic-gate 					else
32295aefb655Srie 					    DBG_CALL(Dbg_util_nl(lml,
32305aefb655Srie 						DBG_NL_STD));
32317c478bd9Sstevel@tonic-gate 				}
32327c478bd9Sstevel@tonic-gate 
32339aa23310Srie 				if (unref)
32347c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_UNREF_FMT),
32357c478bd9Sstevel@tonic-gate 					NAME(lmp), NAME(clmp));
32367c478bd9Sstevel@tonic-gate 				else
32375aefb655Srie 				    DBG_CALL(Dbg_unused_unref(lmp, NAME(clmp)));
3238a953e2b1Srie 				/* END CSTYLED */
32397c478bd9Sstevel@tonic-gate 			}
32407c478bd9Sstevel@tonic-gate 		}
32417c478bd9Sstevel@tonic-gate 
32427c478bd9Sstevel@tonic-gate 		/*
32437c478bd9Sstevel@tonic-gate 		 * If tracing unused objects simply display those objects that
32447c478bd9Sstevel@tonic-gate 		 * haven't been referenced by anyone.
32457c478bd9Sstevel@tonic-gate 		 */
32467c478bd9Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_USED)
32477c478bd9Sstevel@tonic-gate 			continue;
32487c478bd9Sstevel@tonic-gate 
32497c478bd9Sstevel@tonic-gate 		if (nl++ == 0) {
32509aa23310Srie 			if (unref || unuse)
32517c478bd9Sstevel@tonic-gate 				(void) printf(MSG_ORIG(MSG_STR_NL));
32527c478bd9Sstevel@tonic-gate 			else
32535aefb655Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
32547c478bd9Sstevel@tonic-gate 		}
32557c478bd9Sstevel@tonic-gate 		if (CYCGROUP(lmp)) {
32569aa23310Srie 			if (unref || unuse)
32577c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNCYC_FMT),
32587c478bd9Sstevel@tonic-gate 				    NAME(lmp), CYCGROUP(lmp));
32597c478bd9Sstevel@tonic-gate 			else
32605aefb655Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0,
32617c478bd9Sstevel@tonic-gate 				    CYCGROUP(lmp)));
32627c478bd9Sstevel@tonic-gate 		} else {
32639aa23310Srie 			if (unref || unuse)
32647c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNUSED_FMT),
32657c478bd9Sstevel@tonic-gate 				    NAME(lmp));
32667c478bd9Sstevel@tonic-gate 			else
32675aefb655Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0, 0));
32687c478bd9Sstevel@tonic-gate 		}
32697c478bd9Sstevel@tonic-gate 	}
32707c478bd9Sstevel@tonic-gate 
32715aefb655Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
32727c478bd9Sstevel@tonic-gate }
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate /*
32757c478bd9Sstevel@tonic-gate  * Generic cleanup routine called prior to returning control to the user.
32767c478bd9Sstevel@tonic-gate  * Insures that any ld.so.1 specific file descriptors or temporary mapping are
32777c478bd9Sstevel@tonic-gate  * released, and any locks dropped.
32787c478bd9Sstevel@tonic-gate  */
32797c478bd9Sstevel@tonic-gate void
32808cd45542Sraf leave(Lm_list *lml, int flags)
32817c478bd9Sstevel@tonic-gate {
328256deab07SRod Evans 	Lm_list		*elml = lml;
328356deab07SRod Evans 	Rt_map		*clmp;
328456deab07SRod Evans 	Aliste		idx;
328512b8e62eSrie 
32867c478bd9Sstevel@tonic-gate 	/*
328712b8e62eSrie 	 * Alert the debuggers that the link-maps are consistent.  Note, in the
328812b8e62eSrie 	 * case of tearing down a whole link-map list, lml will be null.  In
328912b8e62eSrie 	 * this case use the main link-map list to test for a notification.
32907c478bd9Sstevel@tonic-gate 	 */
32913dbfc803SRod Evans 	if (elml == NULL)
329212b8e62eSrie 		elml = &lml_main;
329312b8e62eSrie 	if (elml->lm_flags & LML_FLG_DBNOTIF)
329412b8e62eSrie 		rd_event(elml, RD_DLACTIVITY, RT_CONSISTENT);
32957c478bd9Sstevel@tonic-gate 
32967247f888Srie 	/*
32977247f888Srie 	 * Alert any auditors that the link-maps are consistent.
32987247f888Srie 	 */
3299cce0e03bSab 	for (APLIST_TRAVERSE(elml->lm_actaudit, idx, clmp)) {
3300cce0e03bSab 		audit_activity(clmp, LA_ACT_CONSISTENT);
33017247f888Srie 
3302cce0e03bSab 		aplist_delete(elml->lm_actaudit, &idx);
33037247f888Srie 	}
33047247f888Srie 
33057c478bd9Sstevel@tonic-gate 	if (nu_fd != FD_UNAVAIL) {
33067c478bd9Sstevel@tonic-gate 		(void) close(nu_fd);
33077c478bd9Sstevel@tonic-gate 		nu_fd = FD_UNAVAIL;
33087c478bd9Sstevel@tonic-gate 	}
33097c478bd9Sstevel@tonic-gate 
33107c478bd9Sstevel@tonic-gate 	/*
33117c478bd9Sstevel@tonic-gate 	 * Reinitialize error message pointer, and any overflow indication.
33127c478bd9Sstevel@tonic-gate 	 */
33137c478bd9Sstevel@tonic-gate 	nextptr = errbuf;
331443d7826aSRod Evans 	prevptr = NULL;
33157c478bd9Sstevel@tonic-gate 
331656deab07SRod Evans 	/*
331756deab07SRod Evans 	 * Defragment any freed memory.
331856deab07SRod Evans 	 */
331956deab07SRod Evans 	if (aplist_nitems(free_alp))
332056deab07SRod Evans 		defrag();
332156deab07SRod Evans 
33227c478bd9Sstevel@tonic-gate 	/*
33237c478bd9Sstevel@tonic-gate 	 * Don't drop our lock if we are running on our link-map list as
33247c478bd9Sstevel@tonic-gate 	 * there's little point in doing so since we are single-threaded.
33257c478bd9Sstevel@tonic-gate 	 *
33267c478bd9Sstevel@tonic-gate 	 * LML_FLG_HOLDLOCK is set for:
332743d7826aSRod Evans 	 *  -	 The ld.so.1's link-map list.
332843d7826aSRod Evans 	 *  -	 The auditor's link-map if the environment is pre-UPM.
33297c478bd9Sstevel@tonic-gate 	 */
33307c478bd9Sstevel@tonic-gate 	if (lml && (lml->lm_flags & LML_FLG_HOLDLOCK))
33317c478bd9Sstevel@tonic-gate 		return;
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	if (rt_bind_clear(0) & THR_FLG_RTLD) {
33348cd45542Sraf 		if (!thr_flg_nolock)
33358cd45542Sraf 			(void) rt_mutex_unlock(&rtldlock);
33368cd45542Sraf 		(void) rt_bind_clear(THR_FLG_RTLD | thr_flg_nolock | flags);
33377c478bd9Sstevel@tonic-gate 	}
33387c478bd9Sstevel@tonic-gate }
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate int
334160758829Srie callable(Rt_map *clmp, Rt_map *dlmp, Grp_hdl *ghp, uint_t slflags)
33427c478bd9Sstevel@tonic-gate {
3343cce0e03bSab 	APlist		*calp, *dalp;
3344cce0e03bSab 	Aliste		idx1, idx2;
3345cce0e03bSab 	Grp_hdl		*ghp1, *ghp2;
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 	/*
33487c478bd9Sstevel@tonic-gate 	 * An object can always find symbols within itself.
33497c478bd9Sstevel@tonic-gate 	 */
33507c478bd9Sstevel@tonic-gate 	if (clmp == dlmp)
33517c478bd9Sstevel@tonic-gate 		return (1);
33527c478bd9Sstevel@tonic-gate 
335360758829Srie 	/*
335460758829Srie 	 * The search for a singleton must look in every loaded object.
335560758829Srie 	 */
335660758829Srie 	if (slflags & LKUP_SINGLETON)
335760758829Srie 		return (1);
335860758829Srie 
33597c478bd9Sstevel@tonic-gate 	/*
33607c478bd9Sstevel@tonic-gate 	 * Don't allow an object to bind to an object that is being deleted
33617c478bd9Sstevel@tonic-gate 	 * unless the binder is also being deleted.
33627c478bd9Sstevel@tonic-gate 	 */
33637c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & FLG_RT_DELETE) &&
33647c478bd9Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
33657c478bd9Sstevel@tonic-gate 		return (0);
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	/*
33687c478bd9Sstevel@tonic-gate 	 * An object with world access can always bind to an object with global
33697c478bd9Sstevel@tonic-gate 	 * visibility.
33707c478bd9Sstevel@tonic-gate 	 */
337137ffaf83SRod Evans 	if (((MODE(clmp) & RTLD_WORLD) || (slflags & LKUP_WORLD)) &&
337237ffaf83SRod Evans 	    (MODE(dlmp) & RTLD_GLOBAL))
33737c478bd9Sstevel@tonic-gate 		return (1);
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	/*
33767c478bd9Sstevel@tonic-gate 	 * An object with local access can only bind to an object that is a
33777c478bd9Sstevel@tonic-gate 	 * member of the same group.
33787c478bd9Sstevel@tonic-gate 	 */
33797c478bd9Sstevel@tonic-gate 	if (((MODE(clmp) & RTLD_GROUP) == 0) ||
3380cce0e03bSab 	    ((calp = GROUPS(clmp)) == NULL) || ((dalp = GROUPS(dlmp)) == NULL))
33817c478bd9Sstevel@tonic-gate 		return (0);
33827c478bd9Sstevel@tonic-gate 
33837c478bd9Sstevel@tonic-gate 	/*
33847c478bd9Sstevel@tonic-gate 	 * Traverse the list of groups the caller is a part of.
33857c478bd9Sstevel@tonic-gate 	 */
3386cce0e03bSab 	for (APLIST_TRAVERSE(calp, idx1, ghp1)) {
33877c478bd9Sstevel@tonic-gate 		/*
33887c478bd9Sstevel@tonic-gate 		 * If we're testing for the ability of two objects to bind to
33897c478bd9Sstevel@tonic-gate 		 * each other regardless of a specific group, ignore that group.
33907c478bd9Sstevel@tonic-gate 		 */
3391cce0e03bSab 		if (ghp && (ghp1 == ghp))
33927c478bd9Sstevel@tonic-gate 			continue;
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 		/*
33957c478bd9Sstevel@tonic-gate 		 * Traverse the list of groups the destination is a part of.
33967c478bd9Sstevel@tonic-gate 		 */
3397cce0e03bSab 		for (APLIST_TRAVERSE(dalp, idx2, ghp2)) {
3398efb9e8b8Srie 			Grp_desc	*gdp;
3399cce0e03bSab 			Aliste		idx3;
3400efb9e8b8Srie 
3401cce0e03bSab 			if (ghp1 != ghp2)
3402efb9e8b8Srie 				continue;
3403efb9e8b8Srie 
3404efb9e8b8Srie 			/*
3405efb9e8b8Srie 			 * Make sure the relationship between the destination
3406efb9e8b8Srie 			 * and the caller provide symbols for relocation.
3407efb9e8b8Srie 			 * Parents are maintained as callers, but unless the
3408efb9e8b8Srie 			 * destination object was opened with RTLD_PARENT, the
3409efb9e8b8Srie 			 * parent doesn't provide symbols for the destination
3410efb9e8b8Srie 			 * to relocate against.
3411efb9e8b8Srie 			 */
3412cce0e03bSab 			for (ALIST_TRAVERSE(ghp2->gh_depends, idx3, gdp)) {
3413efb9e8b8Srie 				if (dlmp != gdp->gd_depend)
3414efb9e8b8Srie 					continue;
3415efb9e8b8Srie 
3416efb9e8b8Srie 				if (gdp->gd_flags & GPD_RELOC)
3417efb9e8b8Srie 					return (1);
3418efb9e8b8Srie 			}
34197c478bd9Sstevel@tonic-gate 		}
34207c478bd9Sstevel@tonic-gate 	}
34217c478bd9Sstevel@tonic-gate 	return (0);
34227c478bd9Sstevel@tonic-gate }
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate /*
34257c478bd9Sstevel@tonic-gate  * Initialize the environ symbol.  Traditionally this is carried out by the crt
34267c478bd9Sstevel@tonic-gate  * code prior to jumping to main.  However, init sections get fired before this
34277c478bd9Sstevel@tonic-gate  * variable is initialized, so ld.so.1 sets this directly from the AUX vector
34287c478bd9Sstevel@tonic-gate  * information.  In addition, a process may have multiple link-maps (ld.so.1's
34297c478bd9Sstevel@tonic-gate  * debugging and preloading objects), and link auditing, and each may need an
34307c478bd9Sstevel@tonic-gate  * environ variable set.
34317c478bd9Sstevel@tonic-gate  *
34327c478bd9Sstevel@tonic-gate  * This routine is called after a relocation() pass, and thus provides for:
34337c478bd9Sstevel@tonic-gate  *
343443d7826aSRod Evans  *  -	setting environ on the main link-map after the initial application and
34357c478bd9Sstevel@tonic-gate  *	its dependencies have been established.  Typically environ lives in the
34367c478bd9Sstevel@tonic-gate  *	application (provided by its crt), but in older applications it might
34377c478bd9Sstevel@tonic-gate  *	be in libc.  Who knows what's expected of applications not built on
34387c478bd9Sstevel@tonic-gate  *	Solaris.
34397c478bd9Sstevel@tonic-gate  *
344043d7826aSRod Evans  *  -	after loading a new shared object.  We can add shared objects to various
34417c478bd9Sstevel@tonic-gate  *	link-maps, and any link-map dependencies requiring getopt() require
34427c478bd9Sstevel@tonic-gate  *	their own environ.  In addition, lazy loading might bring in the
344341072f3cSrie  *	supplier of environ (libc used to be a lazy loading candidate) after
344441072f3cSrie  *	the link-map has been established and other objects are present.
34457c478bd9Sstevel@tonic-gate  *
34467c478bd9Sstevel@tonic-gate  * This routine handles all these scenarios, without adding unnecessary overhead
34477c478bd9Sstevel@tonic-gate  * to ld.so.1.
34487c478bd9Sstevel@tonic-gate  */
34497c478bd9Sstevel@tonic-gate void
34507c478bd9Sstevel@tonic-gate set_environ(Lm_list *lml)
34517c478bd9Sstevel@tonic-gate {
34527c478bd9Sstevel@tonic-gate 	Slookup		sl;
345308278a5eSRod Evans 	Sresult		sr;
34547c478bd9Sstevel@tonic-gate 	uint_t		binfo;
34557c478bd9Sstevel@tonic-gate 
345675e7992aSrie 	/*
345708278a5eSRod Evans 	 * Initialize the symbol lookup, and symbol result, data structures.
345875e7992aSrie 	 */
345975e7992aSrie 	SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_ENVIRON), lml->lm_head, lml->lm_head,
346075e7992aSrie 	    ld_entry_cnt, 0, 0, 0, 0, LKUP_WEAK);
346108278a5eSRod Evans 	SRESULT_INIT(sr, MSG_ORIG(MSG_SYM_ENVIRON));
346208278a5eSRod Evans 
346308278a5eSRod Evans 	if (LM_LOOKUP_SYM(lml->lm_head)(&sl, &sr, &binfo, 0)) {
346408278a5eSRod Evans 		Rt_map	*dlmp = sr.sr_dmap;
34657c478bd9Sstevel@tonic-gate 
346608278a5eSRod Evans 		lml->lm_environ = (char ***)sr.sr_sym->st_value;
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 		if (!(FLAGS(dlmp) & FLG_RT_FIXED))
346941072f3cSrie 			lml->lm_environ =
347041072f3cSrie 			    (char ***)((uintptr_t)lml->lm_environ +
347141072f3cSrie 			    (uintptr_t)ADDR(dlmp));
347241072f3cSrie 		*(lml->lm_environ) = (char **)environ;
34737c478bd9Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_ENVIRON;
34747c478bd9Sstevel@tonic-gate 	}
34757c478bd9Sstevel@tonic-gate }
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate /*
34787c478bd9Sstevel@tonic-gate  * Determine whether we have a secure executable.  Uid and gid information
34797c478bd9Sstevel@tonic-gate  * can be passed to us via the aux vector, however if these values are -1
34807c478bd9Sstevel@tonic-gate  * then use the appropriate system call to obtain them.
34817c478bd9Sstevel@tonic-gate  *
348243d7826aSRod Evans  *  -	If the user is the root they can do anything
34837c478bd9Sstevel@tonic-gate  *
348443d7826aSRod Evans  *  -	If the real and effective uid's don't match, or the real and
34857c478bd9Sstevel@tonic-gate  *	effective gid's don't match then this is determined to be a `secure'
34867c478bd9Sstevel@tonic-gate  *	application.
34877c478bd9Sstevel@tonic-gate  *
34887c478bd9Sstevel@tonic-gate  * This function is called prior to any dependency processing (see _setup.c).
34897c478bd9Sstevel@tonic-gate  * Any secure setting will remain in effect for the life of the process.
34907c478bd9Sstevel@tonic-gate  */
34917c478bd9Sstevel@tonic-gate void
34927c478bd9Sstevel@tonic-gate security(uid_t uid, uid_t euid, gid_t gid, gid_t egid, int auxflags)
34937c478bd9Sstevel@tonic-gate {
34947c478bd9Sstevel@tonic-gate 	if (auxflags != -1) {
34957c478bd9Sstevel@tonic-gate 		if ((auxflags & AF_SUN_SETUGID) != 0)
34967c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
34977c478bd9Sstevel@tonic-gate 		return;
34987c478bd9Sstevel@tonic-gate 	}
349956deab07SRod Evans 
3500a953e2b1Srie 	if (uid == (uid_t)-1)
35017c478bd9Sstevel@tonic-gate 		uid = getuid();
35027c478bd9Sstevel@tonic-gate 	if (uid) {
3503a953e2b1Srie 		if (euid == (uid_t)-1)
35047c478bd9Sstevel@tonic-gate 			euid = geteuid();
35057c478bd9Sstevel@tonic-gate 		if (uid != euid)
35067c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
35077c478bd9Sstevel@tonic-gate 		else {
3508a953e2b1Srie 			if (gid == (gid_t)-1)
35097c478bd9Sstevel@tonic-gate 				gid = getgid();
3510a953e2b1Srie 			if (egid == (gid_t)-1)
35117c478bd9Sstevel@tonic-gate 				egid = getegid();
35127c478bd9Sstevel@tonic-gate 			if (gid != egid)
35137c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_SECURE;
35147c478bd9Sstevel@tonic-gate 		}
35157c478bd9Sstevel@tonic-gate 	}
35167c478bd9Sstevel@tonic-gate }
35177c478bd9Sstevel@tonic-gate 
35183dbfc803SRod Evans /*
35193dbfc803SRod Evans  * Determine whether ld.so.1 itself is owned by root and has its mode setuid.
35203dbfc803SRod Evans  */
35213dbfc803SRod Evans int
35223dbfc803SRod Evans is_rtld_setuid()
35233dbfc803SRod Evans {
3524cb511613SAli Bahrami 	rtld_stat_t	status;
35253dbfc803SRod Evans 
35263dbfc803SRod Evans 	if ((rtld_flags2 & RT_FL2_SETUID) ||
3527cb511613SAli Bahrami 	    ((rtld_stat(NAME(lml_rtld.lm_head), &status) == 0) &&
35283dbfc803SRod Evans 	    (status.st_uid == 0) && (status.st_mode & S_ISUID))) {
35293dbfc803SRod Evans 		rtld_flags2 |= RT_FL2_SETUID;
35303dbfc803SRod Evans 		return (1);
35313dbfc803SRod Evans 	}
35323dbfc803SRod Evans 	return (0);
35333dbfc803SRod Evans }
35343dbfc803SRod Evans 
353508278a5eSRod Evans /*
353608278a5eSRod Evans  * Determine that systems platform name.  Normally, this name is provided from
353708278a5eSRod Evans  * the AT_SUN_PLATFORM aux vector from the kernel.  This routine provides a
353808278a5eSRod Evans  * fall back.
353908278a5eSRod Evans  */
354008278a5eSRod Evans void
354108278a5eSRod Evans platform_name(Syscapset *scapset)
354208278a5eSRod Evans {
354308278a5eSRod Evans 	char	info[SYS_NMLN];
354408278a5eSRod Evans 	size_t	size;
354508278a5eSRod Evans 
354608278a5eSRod Evans 	if ((scapset->sc_platsz = size =
354708278a5eSRod Evans 	    sysinfo(SI_PLATFORM, info, SYS_NMLN)) == (size_t)-1)
354808278a5eSRod Evans 		return;
354908278a5eSRod Evans 
355008278a5eSRod Evans 	if ((scapset->sc_plat = malloc(size)) == NULL) {
355108278a5eSRod Evans 		scapset->sc_platsz = (size_t)-1;
355208278a5eSRod Evans 		return;
355308278a5eSRod Evans 	}
355408278a5eSRod Evans 	(void) strcpy(scapset->sc_plat, info);
355508278a5eSRod Evans }
355608278a5eSRod Evans 
355708278a5eSRod Evans /*
355808278a5eSRod Evans  * Determine that systems machine name.  Normally, this name is provided from
355908278a5eSRod Evans  * the AT_SUN_MACHINE aux vector from the kernel.  This routine provides a
356008278a5eSRod Evans  * fall back.
356108278a5eSRod Evans  */
356208278a5eSRod Evans void
356308278a5eSRod Evans machine_name(Syscapset *scapset)
356408278a5eSRod Evans {
356508278a5eSRod Evans 	char	info[SYS_NMLN];
356608278a5eSRod Evans 	size_t	size;
356708278a5eSRod Evans 
356808278a5eSRod Evans 	if ((scapset->sc_machsz = size =
356908278a5eSRod Evans 	    sysinfo(SI_MACHINE, info, SYS_NMLN)) == (size_t)-1)
357008278a5eSRod Evans 		return;
357108278a5eSRod Evans 
357208278a5eSRod Evans 	if ((scapset->sc_mach = malloc(size)) == NULL) {
357308278a5eSRod Evans 		scapset->sc_machsz = (size_t)-1;
357408278a5eSRod Evans 		return;
357508278a5eSRod Evans 	}
357608278a5eSRod Evans 	(void) strcpy(scapset->sc_mach, info);
357708278a5eSRod Evans }
357808278a5eSRod Evans 
35797c478bd9Sstevel@tonic-gate /*
35807c478bd9Sstevel@tonic-gate  * _REENTRANT code gets errno redefined to a function so provide for return
35817c478bd9Sstevel@tonic-gate  * of the thread errno if applicable.  This has no meaning in ld.so.1 which
35827c478bd9Sstevel@tonic-gate  * is basically singled threaded.  Provide the interface for our dependencies.
35837c478bd9Sstevel@tonic-gate  */
35847c478bd9Sstevel@tonic-gate #undef errno
35857c478bd9Sstevel@tonic-gate int *
35867c478bd9Sstevel@tonic-gate ___errno()
35877c478bd9Sstevel@tonic-gate {
35887c478bd9Sstevel@tonic-gate 	extern	int	errno;
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	return (&errno);
35917c478bd9Sstevel@tonic-gate }
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate /*
35947c478bd9Sstevel@tonic-gate  * Determine whether a symbol name should be demangled.
35957c478bd9Sstevel@tonic-gate  */
35967c478bd9Sstevel@tonic-gate const char *
35977c478bd9Sstevel@tonic-gate demangle(const char *name)
35987c478bd9Sstevel@tonic-gate {
35997c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DEMANGLE)
36005aefb655Srie 		return (conv_demangle_name(name));
36017c478bd9Sstevel@tonic-gate 	else
36027c478bd9Sstevel@tonic-gate 		return (name);
36037c478bd9Sstevel@tonic-gate }
3604cb511613SAli Bahrami 
3605cb511613SAli Bahrami #ifndef _LP64
3606cb511613SAli Bahrami /*
3607cb511613SAli Bahrami  * Wrappers on stat() and fstat() for 32-bit rtld that uses stat64()
3608cb511613SAli Bahrami  * underneath while preserving the object size limits of a non-largefile
3609cb511613SAli Bahrami  * enabled 32-bit process. The purpose of this is to prevent large inode
3610cb511613SAli Bahrami  * values from causing stat() to fail.
3611cb511613SAli Bahrami  */
3612cb511613SAli Bahrami inline static int
3613cb511613SAli Bahrami rtld_stat_process(int r, struct stat64 *lbuf, rtld_stat_t *restrict buf)
3614cb511613SAli Bahrami {
3615cb511613SAli Bahrami 	extern int	errno;
3616cb511613SAli Bahrami 
3617cb511613SAli Bahrami 	/*
3618cb511613SAli Bahrami 	 * Although we used a 64-bit capable stat(), the 32-bit rtld
3619cb511613SAli Bahrami 	 * can only handle objects < 2GB in size. If this object is
3620cb511613SAli Bahrami 	 * too big, turn the success into an overflow error.
3621cb511613SAli Bahrami 	 */
3622cb511613SAli Bahrami 	if ((lbuf->st_size & 0xffffffff80000000) != 0) {
3623cb511613SAli Bahrami 		errno = EOVERFLOW;
3624cb511613SAli Bahrami 		return (-1);
3625cb511613SAli Bahrami 	}
3626cb511613SAli Bahrami 
3627cb511613SAli Bahrami 	/*
3628cb511613SAli Bahrami 	 * Transfer the information needed by rtld into a rtld_stat_t
3629cb511613SAli Bahrami 	 * structure that preserves the non-largile types for everything
3630cb511613SAli Bahrami 	 * except inode.
3631cb511613SAli Bahrami 	 */
3632cb511613SAli Bahrami 	buf->st_dev = lbuf->st_dev;
3633cb511613SAli Bahrami 	buf->st_ino = lbuf->st_ino;
3634cb511613SAli Bahrami 	buf->st_mode = lbuf->st_mode;
3635cb511613SAli Bahrami 	buf->st_uid = lbuf->st_uid;
3636cb511613SAli Bahrami 	buf->st_size = (off_t)lbuf->st_size;
3637cb511613SAli Bahrami 	buf->st_mtim = lbuf->st_mtim;
3638cb511613SAli Bahrami #ifdef sparc
3639cb511613SAli Bahrami 	buf->st_blksize = lbuf->st_blksize;
3640cb511613SAli Bahrami #endif
3641cb511613SAli Bahrami 
3642cb511613SAli Bahrami 	return (r);
3643cb511613SAli Bahrami }
3644cb511613SAli Bahrami 
3645cb511613SAli Bahrami int
3646cb511613SAli Bahrami rtld_stat(const char *restrict path, rtld_stat_t *restrict buf)
3647cb511613SAli Bahrami {
3648cb511613SAli Bahrami 	struct stat64	lbuf;
3649cb511613SAli Bahrami 	int		r;
3650cb511613SAli Bahrami 
3651cb511613SAli Bahrami 	r = stat64(path, &lbuf);
3652cb511613SAli Bahrami 	if (r != -1)
3653cb511613SAli Bahrami 		r = rtld_stat_process(r, &lbuf, buf);
3654cb511613SAli Bahrami 	return (r);
3655cb511613SAli Bahrami }
3656cb511613SAli Bahrami 
3657cb511613SAli Bahrami int
3658cb511613SAli Bahrami rtld_fstat(int fildes, rtld_stat_t *restrict buf)
3659cb511613SAli Bahrami {
3660cb511613SAli Bahrami 	struct stat64	lbuf;
3661cb511613SAli Bahrami 	int		r;
3662cb511613SAli Bahrami 
3663cb511613SAli Bahrami 	r = fstat64(fildes, &lbuf);
3664cb511613SAli Bahrami 	if (r != -1)
3665cb511613SAli Bahrami 		r = rtld_stat_process(r, &lbuf, buf);
3666cb511613SAli Bahrami 	return (r);
3667cb511613SAli Bahrami }
3668cb511613SAli Bahrami #endif
3669