xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/util.c (revision 3dbfc803)
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 
227c478bd9Sstevel@tonic-gate /*
23cce0e03bSab  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267257d1b4Sraf 
277257d1b4Sraf /*
287257d1b4Sraf  *	Copyright (c) 1988 AT&T
297257d1b4Sraf  *	  All Rights Reserved
307257d1b4Sraf  */
317257d1b4Sraf 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * Utility routines for run-time linker.  some are duplicated here from libc
347c478bd9Sstevel@tonic-gate  * (with different names) to avoid name space collisions.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate #include	<stdio.h>
377c478bd9Sstevel@tonic-gate #include	<sys/types.h>
387c478bd9Sstevel@tonic-gate #include	<sys/mman.h>
397c478bd9Sstevel@tonic-gate #include	<sys/lwp.h>
407c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
417c478bd9Sstevel@tonic-gate #include	<stdarg.h>
427c478bd9Sstevel@tonic-gate #include	<fcntl.h>
437c478bd9Sstevel@tonic-gate #include	<string.h>
447c478bd9Sstevel@tonic-gate #include	<ctype.h>
457c478bd9Sstevel@tonic-gate #include	<dlfcn.h>
467c478bd9Sstevel@tonic-gate #include	<unistd.h>
477c478bd9Sstevel@tonic-gate #include	<stdlib.h>
48*3dbfc803SRod Evans #include	<limits.h>
497c478bd9Sstevel@tonic-gate #include	<sys/auxv.h>
505aefb655Srie #include	<debug.h>
515aefb655Srie #include	<conv.h>
527c478bd9Sstevel@tonic-gate #include	"_rtld.h"
537c478bd9Sstevel@tonic-gate #include	"_audit.h"
5410a4fa49Srie #include	"_elf.h"
557c478bd9Sstevel@tonic-gate #include	"msg.h"
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static int ld_flags_env(const char *, Word *, Word *, uint_t, int);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * All error messages go through eprintf().  During process initialization these
617c478bd9Sstevel@tonic-gate  * messages should be directed to the standard error, however once control has
627c478bd9Sstevel@tonic-gate  * been passed to the applications code these messages should be stored in an
637c478bd9Sstevel@tonic-gate  * internal buffer for use with dlerror().  Note, fatal error conditions that
647c478bd9Sstevel@tonic-gate  * may occur while running the application will still cause a standard error
657c478bd9Sstevel@tonic-gate  * message, see rtldexit() in this file for details.
667c478bd9Sstevel@tonic-gate  * The `application' flag serves to indicate the transition between process
677c478bd9Sstevel@tonic-gate  * initialization and when the applications code is running.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Null function used as place where a debugger can set a breakpoint.
727c478bd9Sstevel@tonic-gate  */
737c478bd9Sstevel@tonic-gate void
745aefb655Srie rtld_db_dlactivity(Lm_list *lml)
757c478bd9Sstevel@tonic-gate {
765aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
775aefb655Srie 	    r_debug.rtd_rdebug.r_state));
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Null function used as place where debugger can set a pre .init
827c478bd9Sstevel@tonic-gate  * processing breakpoint.
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate void
855aefb655Srie rtld_db_preinit(Lm_list *lml)
867c478bd9Sstevel@tonic-gate {
875aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
885aefb655Srie 	    r_debug.rtd_rdebug.r_state));
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate /*
927c478bd9Sstevel@tonic-gate  * Null function used as place where debugger can set a post .init
937c478bd9Sstevel@tonic-gate  * processing breakpoint.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate void
965aefb655Srie rtld_db_postinit(Lm_list *lml)
977c478bd9Sstevel@tonic-gate {
985aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
995aefb655Srie 	    r_debug.rtd_rdebug.r_state));
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate  * Debugger Event Notification
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * This function centralizes all debugger event notification (ala rtld_db).
1067c478bd9Sstevel@tonic-gate  *
1077c478bd9Sstevel@tonic-gate  * There's a simple intent, focused on insuring the primary link-map control
1087c478bd9Sstevel@tonic-gate  * list (or each link-map list) is consistent, and the indication that objects
1097c478bd9Sstevel@tonic-gate  * have been added or deleted from this list.  Although an RD_ADD and RD_DELETE
1107c478bd9Sstevel@tonic-gate  * event are posted for each of these, most debuggers don't care, as their
1117c478bd9Sstevel@tonic-gate  * view is that these events simply convey an "inconsistent" state.
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * We also don't want to trigger multiple RD_ADD/RD_DELETE events any time we
1147c478bd9Sstevel@tonic-gate  * enter ld.so.1.
1157c478bd9Sstevel@tonic-gate  *
1167c478bd9Sstevel@tonic-gate  * With auditors, we may be in the process of relocating a collection of
1177c478bd9Sstevel@tonic-gate  * objects, and will leave() ld.so.1 to call the auditor.  At this point we
1187c478bd9Sstevel@tonic-gate  * must indicate an RD_CONSISTENT event, but librtld_db will not report an
1197c478bd9Sstevel@tonic-gate  * object to the debuggers until relocation processing has been completed on it.
1207c478bd9Sstevel@tonic-gate  * To allow for the collection of these objects that are pending relocation, an
1217c478bd9Sstevel@tonic-gate  * RD_ADD event is set after completing a series of relocations on the primary
1227c478bd9Sstevel@tonic-gate  * link-map control list.
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * Set an RD_ADD/RD_DELETE event and indicate that an RD_CONSISTENT event is
1257c478bd9Sstevel@tonic-gate  * required later (LML_FLG_DBNOTIF):
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  *  i	the first time we add or delete an object to the primary link-map
1287c478bd9Sstevel@tonic-gate  *	control list.
1297c478bd9Sstevel@tonic-gate  *  ii	the first time we move a secondary link-map control list to the primary
1307c478bd9Sstevel@tonic-gate  *	link-map control list (effectively, this is like adding a group of
1317c478bd9Sstevel@tonic-gate  *	objects to the primary link-map control list).
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  * Set an RD_CONSISTENT event when it is required (LML_FLG_DBNOTIF is set) and
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  *  i	each time we leave the runtime linker.
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate void
1387c478bd9Sstevel@tonic-gate rd_event(Lm_list *lml, rd_event_e event, r_state_e state)
1397c478bd9Sstevel@tonic-gate {
14010a4fa49Srie 	void	(*fptr)(Lm_list *);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	switch (event) {
1437c478bd9Sstevel@tonic-gate 	case RD_PREINIT:
1447c478bd9Sstevel@tonic-gate 		fptr = rtld_db_preinit;
1457c478bd9Sstevel@tonic-gate 		break;
1467c478bd9Sstevel@tonic-gate 	case RD_POSTINIT:
1477c478bd9Sstevel@tonic-gate 		fptr = rtld_db_postinit;
1487c478bd9Sstevel@tonic-gate 		break;
1497c478bd9Sstevel@tonic-gate 	case RD_DLACTIVITY:
1507c478bd9Sstevel@tonic-gate 		switch (state) {
1517c478bd9Sstevel@tonic-gate 		case RT_CONSISTENT:
1527c478bd9Sstevel@tonic-gate 			lml->lm_flags &= ~LML_FLG_DBNOTIF;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 			/*
1557c478bd9Sstevel@tonic-gate 			 * Do we need to send a notification?
1567c478bd9Sstevel@tonic-gate 			 */
1577c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_DBNOTIF) == 0)
1587c478bd9Sstevel@tonic-gate 				return;
1597c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_DBNOTIF;
1607c478bd9Sstevel@tonic-gate 			break;
1617c478bd9Sstevel@tonic-gate 		case RT_ADD:
1627c478bd9Sstevel@tonic-gate 		case RT_DELETE:
1637c478bd9Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_DBNOTIF;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 			/*
1667c478bd9Sstevel@tonic-gate 			 * If we are already in an inconsistent state, no
1677c478bd9Sstevel@tonic-gate 			 * notification is required.
1687c478bd9Sstevel@tonic-gate 			 */
1697c478bd9Sstevel@tonic-gate 			if (rtld_flags & RT_FL_DBNOTIF)
1707c478bd9Sstevel@tonic-gate 				return;
1717c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_DBNOTIF;
1727c478bd9Sstevel@tonic-gate 			break;
1737c478bd9Sstevel@tonic-gate 		};
1747c478bd9Sstevel@tonic-gate 		fptr = rtld_db_dlactivity;
1757c478bd9Sstevel@tonic-gate 		break;
1767c478bd9Sstevel@tonic-gate 	default:
1777c478bd9Sstevel@tonic-gate 		/*
1787c478bd9Sstevel@tonic-gate 		 * RD_NONE - do nothing
1797c478bd9Sstevel@tonic-gate 		 */
1807c478bd9Sstevel@tonic-gate 		break;
1817c478bd9Sstevel@tonic-gate 	};
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Set event state and call 'notification' function.
1857c478bd9Sstevel@tonic-gate 	 *
1867c478bd9Sstevel@tonic-gate 	 * The debugging clients have previously been told about these
1877c478bd9Sstevel@tonic-gate 	 * notification functions and have set breakpoints on them if they
1887c478bd9Sstevel@tonic-gate 	 * are interested in the notification.
1897c478bd9Sstevel@tonic-gate 	 */
1907c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_state = state;
1917c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = event;
1925aefb655Srie 	fptr(lml);
1937c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = RD_NONE;
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
19602ca3e02Srie #if	defined(__sparc) || defined(__x86)
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * Stack Cleanup.
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * This function is invoked to 'remove' arguments that were passed in on the
2017c478bd9Sstevel@tonic-gate  * stack.  This is most likely if ld.so.1 was invoked directly.  In that case
2027c478bd9Sstevel@tonic-gate  * we want to remove ld.so.1 as well as it's arguments from the argv[] array.
2037c478bd9Sstevel@tonic-gate  * Which means we then need to slide everything above it on the stack down
2047c478bd9Sstevel@tonic-gate  * accordingly.
2057c478bd9Sstevel@tonic-gate  *
20602ca3e02Srie  * While the stack layout is platform specific - it just so happens that __x86,
20702ca3e02Srie  * and __sparc platforms share the following initial stack layout.
2087c478bd9Sstevel@tonic-gate  *
2097c478bd9Sstevel@tonic-gate  *	!_______________________!  high addresses
2107c478bd9Sstevel@tonic-gate  *	!			!
2117c478bd9Sstevel@tonic-gate  *	!	Information	!
2127c478bd9Sstevel@tonic-gate  *	!	Block		!
2137c478bd9Sstevel@tonic-gate  *	!	(size varies)	!
2147c478bd9Sstevel@tonic-gate  *	!_______________________!
2157c478bd9Sstevel@tonic-gate  *	!	0 word		!
2167c478bd9Sstevel@tonic-gate  *	!_______________________!
2177c478bd9Sstevel@tonic-gate  *	!	Auxiliary	!
2187c478bd9Sstevel@tonic-gate  *	!	vector		!
2197c478bd9Sstevel@tonic-gate  *	!	2 word entries	!
2207c478bd9Sstevel@tonic-gate  *	!			!
2217c478bd9Sstevel@tonic-gate  *	!_______________________!
2227c478bd9Sstevel@tonic-gate  *	!	0 word		!
2237c478bd9Sstevel@tonic-gate  *	!_______________________!
2247c478bd9Sstevel@tonic-gate  *	!	Environment	!
2257c478bd9Sstevel@tonic-gate  *	!	pointers	!
2267c478bd9Sstevel@tonic-gate  *	!	...		!
2277c478bd9Sstevel@tonic-gate  *	!	(one word each)	!
2287c478bd9Sstevel@tonic-gate  *	!_______________________!
2297c478bd9Sstevel@tonic-gate  *	!	0 word		!
2307c478bd9Sstevel@tonic-gate  *	!_______________________!
2317c478bd9Sstevel@tonic-gate  *	!	Argument	! low addresses
2327c478bd9Sstevel@tonic-gate  *	!	pointers	!
2337c478bd9Sstevel@tonic-gate  *	!	Argc words	!
2347c478bd9Sstevel@tonic-gate  *	!_______________________!
2357c478bd9Sstevel@tonic-gate  *	!			!
2367c478bd9Sstevel@tonic-gate  *	!	Argc		!
2377c478bd9Sstevel@tonic-gate  *	!_______________________!
2387c478bd9Sstevel@tonic-gate  *	!	...		!
2397c478bd9Sstevel@tonic-gate  *
2407c478bd9Sstevel@tonic-gate  */
2417c478bd9Sstevel@tonic-gate static void
24241072f3cSrie stack_cleanup(char **argv, char ***envp, auxv_t **auxv, int rmcnt)
2437c478bd9Sstevel@tonic-gate {
24441072f3cSrie 	int		ndx;
2457c478bd9Sstevel@tonic-gate 	long		*argc;
24641072f3cSrie 	char		**oargv, **nargv;
24741072f3cSrie 	char		**oenvp, **nenvp;
24841072f3cSrie 	auxv_t		*oauxv, *nauxv;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
25141072f3cSrie 	 * Slide ARGV[] and update argc.  The argv pointer remains the same,
25241072f3cSrie 	 * however slide the applications arguments over the arguments to
25341072f3cSrie 	 * ld.so.1.
2547c478bd9Sstevel@tonic-gate 	 */
25541072f3cSrie 	nargv = &argv[0];
25641072f3cSrie 	oargv = &argv[rmcnt];
25741072f3cSrie 
25841072f3cSrie 	for (ndx = 0; oargv[ndx]; ndx++)
25941072f3cSrie 		nargv[ndx] = oargv[ndx];
26041072f3cSrie 	nargv[ndx] = oargv[ndx];
26141072f3cSrie 
26241072f3cSrie 	argc = (long *)((uintptr_t)argv - sizeof (long *));
2637c478bd9Sstevel@tonic-gate 	*argc -= rmcnt;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
26641072f3cSrie 	 * Slide ENVP[], and update the environment array pointer.
2677c478bd9Sstevel@tonic-gate 	 */
26841072f3cSrie 	ndx++;
26941072f3cSrie 	nenvp = &nargv[ndx];
27041072f3cSrie 	oenvp = &oargv[ndx];
27141072f3cSrie 	*envp = nenvp;
27241072f3cSrie 
27341072f3cSrie 	for (ndx = 0; oenvp[ndx]; ndx++)
27441072f3cSrie 		nenvp[ndx] = oenvp[ndx];
27541072f3cSrie 	nenvp[ndx] = oenvp[ndx];
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
27841072f3cSrie 	 * Slide AUXV[], and update the aux vector pointer.
2797c478bd9Sstevel@tonic-gate 	 */
28041072f3cSrie 	ndx++;
28141072f3cSrie 	nauxv = (auxv_t *)&nenvp[ndx];
28241072f3cSrie 	oauxv = (auxv_t *)&oenvp[ndx];
28341072f3cSrie 	*auxv = nauxv;
28441072f3cSrie 
28541072f3cSrie 	for (ndx = 0; (oauxv[ndx].a_type != AT_NULL); ndx++)
28641072f3cSrie 		nauxv[ndx] = oauxv[ndx];
28741072f3cSrie 	nauxv[ndx] = oauxv[ndx];
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate #else
2907c478bd9Sstevel@tonic-gate /*
2917c478bd9Sstevel@tonic-gate  * Verify that the above routine is appropriate for any new platforms.
2927c478bd9Sstevel@tonic-gate  */
2937c478bd9Sstevel@tonic-gate #error	unsupported architecture!
2947c478bd9Sstevel@tonic-gate #endif
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
29741072f3cSrie  * The only command line argument recognized is -e, followed by a runtime
29841072f3cSrie  * linker environment variable.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate int
30141072f3cSrie rtld_getopt(char **argv, char ***envp, auxv_t **auxv, Word *lmflags,
30241072f3cSrie     Word *lmtflags, int aout)
3037c478bd9Sstevel@tonic-gate {
30441072f3cSrie 	int	ndx;
3057c478bd9Sstevel@tonic-gate 
30641072f3cSrie 	for (ndx = 1; argv[ndx]; ndx++) {
3077c478bd9Sstevel@tonic-gate 		char	*str;
3087c478bd9Sstevel@tonic-gate 
30941072f3cSrie 		if (argv[ndx][0] != '-')
3107c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 
31241072f3cSrie 		if (argv[ndx][1] == '\0') {
31341072f3cSrie 			ndx++;
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 		}
3167c478bd9Sstevel@tonic-gate 
31741072f3cSrie 		if (argv[ndx][1] != 'e')
31841072f3cSrie 			return (1);
3197c478bd9Sstevel@tonic-gate 
32041072f3cSrie 		if (argv[ndx][2] == '\0') {
32141072f3cSrie 			ndx++;
32241072f3cSrie 			if (argv[ndx] == NULL)
32341072f3cSrie 				return (1);
32441072f3cSrie 			str = argv[ndx];
3257c478bd9Sstevel@tonic-gate 		} else
32641072f3cSrie 			str = &argv[ndx][2];
3277c478bd9Sstevel@tonic-gate 
32841072f3cSrie 		/*
32941072f3cSrie 		 * If the environment variable starts with LD_, strip the LD_.
33041072f3cSrie 		 * Otherwise, take things as is.
33141072f3cSrie 		 */
33241072f3cSrie 		if ((str[0] == 'L') && (str[1] == 'D') && (str[2] == '_') &&
33341072f3cSrie 		    (str[3] != '\0'))
33441072f3cSrie 			str += 3;
3357c478bd9Sstevel@tonic-gate 		if (ld_flags_env(str, lmflags, lmtflags, 0, aout) == 1)
3367c478bd9Sstevel@tonic-gate 			return (1);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
33941072f3cSrie 	/*
34041072f3cSrie 	 * Make sure an object file has been specified.
34141072f3cSrie 	 */
34241072f3cSrie 	if (argv[ndx] == 0)
3437c478bd9Sstevel@tonic-gate 		return (1);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	/*
3467c478bd9Sstevel@tonic-gate 	 * Having gotten the arguments, clean ourselves off of the stack.
3477c478bd9Sstevel@tonic-gate 	 */
34841072f3cSrie 	stack_cleanup(argv, envp, auxv, ndx);
3497c478bd9Sstevel@tonic-gate 	return (0);
3507c478bd9Sstevel@tonic-gate }
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate /*
3539aa23310Srie  * Compare function for PathNode AVL tree.
3547c478bd9Sstevel@tonic-gate  */
3557c478bd9Sstevel@tonic-gate static int
3569aa23310Srie pnavl_compare(const void *n1, const void *n2)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	uint_t		hash1, hash2;
3597c478bd9Sstevel@tonic-gate 	const char	*st1, *st2;
3607c478bd9Sstevel@tonic-gate 	int		rc;
3617c478bd9Sstevel@tonic-gate 
3629aa23310Srie 	hash1 = ((PathNode *)n1)->pn_hash;
3639aa23310Srie 	hash2 = ((PathNode *)n2)->pn_hash;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	if (hash1 > hash2)
3667c478bd9Sstevel@tonic-gate 		return (1);
3677c478bd9Sstevel@tonic-gate 	if (hash1 < hash2)
3687c478bd9Sstevel@tonic-gate 		return (-1);
3697c478bd9Sstevel@tonic-gate 
3709aa23310Srie 	st1 = ((PathNode *)n1)->pn_name;
3719aa23310Srie 	st2 = ((PathNode *)n2)->pn_name;
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	rc = strcmp(st1, st2);
3747c478bd9Sstevel@tonic-gate 	if (rc > 0)
3757c478bd9Sstevel@tonic-gate 		return (1);
3767c478bd9Sstevel@tonic-gate 	if (rc < 0)
3777c478bd9Sstevel@tonic-gate 		return (-1);
3787c478bd9Sstevel@tonic-gate 	return (0);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
381*3dbfc803SRod Evans /*
382*3dbfc803SRod Evans  * Create an AVL tree.
383*3dbfc803SRod Evans  */
384*3dbfc803SRod Evans static avl_tree_t *
385*3dbfc803SRod Evans pnavl_create(size_t size)
386*3dbfc803SRod Evans {
387*3dbfc803SRod Evans 	avl_tree_t	*avlt;
388*3dbfc803SRod Evans 
389*3dbfc803SRod Evans 	if ((avlt = malloc(sizeof (avl_tree_t))) == NULL)
390*3dbfc803SRod Evans 		return (NULL);
391*3dbfc803SRod Evans 	avl_create(avlt, pnavl_compare, size, SGSOFFSETOF(PathNode, pn_avl));
392*3dbfc803SRod Evans 	return (avlt);
393*3dbfc803SRod Evans }
394*3dbfc803SRod Evans 
3957c478bd9Sstevel@tonic-gate /*
3969aa23310Srie  * Determine if a pathname has already been recorded on the full path name
3979aa23310Srie  * AVL tree.  This tree maintains a node for each path name that ld.so.1 has
3989aa23310Srie  * successfully loaded.  If the path name does not exist in this AVL tree, then
3999aa23310Srie  * the next insertion point is deposited in "where".  This value can be used by
4009aa23310Srie  * fpavl_insert() to expedite the insertion.
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate Rt_map *
4039aa23310Srie fpavl_recorded(Lm_list *lml, const char *name, avl_index_t *where)
4047c478bd9Sstevel@tonic-gate {
4059aa23310Srie 	FullPathNode	fpn, *fpnp;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/*
4087c478bd9Sstevel@tonic-gate 	 * Create the avl tree if required.
4097c478bd9Sstevel@tonic-gate 	 */
410*3dbfc803SRod Evans 	if ((lml->lm_fpavl == NULL) &&
411*3dbfc803SRod Evans 	    ((lml->lm_fpavl = pnavl_create(sizeof (FullPathNode))) == NULL))
412*3dbfc803SRod Evans 		return (NULL);
4137c478bd9Sstevel@tonic-gate 
4149aa23310Srie 	fpn.fpn_node.pn_name = name;
4159aa23310Srie 	fpn.fpn_node.pn_hash = sgs_str_hash(name);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if ((fpnp = avl_find(lml->lm_fpavl, &fpn, where)) == NULL)
4187c478bd9Sstevel@tonic-gate 		return (NULL);
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	return (fpnp->fpn_lmp);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4249aa23310Srie  * Insert a name into the FullPathNode AVL tree for the link-map list.  The
42524a6229eSrie  * objects NAME() is the path that would have originally been searched for, and
42624a6229eSrie  * is therefore the name to associate with any "where" value.  If the object has
4277c478bd9Sstevel@tonic-gate  * a different PATHNAME(), perhaps because it has resolved to a different file
4289aa23310Srie  * (see fullpath()), then this name will be recorded as a separate FullPathNode
4299aa23310Srie  * (see load_file()).
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate int
4327c478bd9Sstevel@tonic-gate fpavl_insert(Lm_list *lml, Rt_map *lmp, const char *name, avl_index_t where)
4337c478bd9Sstevel@tonic-gate {
4349aa23310Srie 	FullPathNode	*fpnp;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	if (where == 0) {
4377c478bd9Sstevel@tonic-gate 		/* LINTED */
4389aa23310Srie 		Rt_map	*_lmp = fpavl_recorded(lml, name, &where);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 		/*
4417c478bd9Sstevel@tonic-gate 		 * We better not get a hit now, we do not want duplicates in
4427c478bd9Sstevel@tonic-gate 		 * the tree.
4437c478bd9Sstevel@tonic-gate 		 */
4447c478bd9Sstevel@tonic-gate 		ASSERT(_lmp == 0);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4489aa23310Srie 	 * Insert new node in tree.
4497c478bd9Sstevel@tonic-gate 	 */
450*3dbfc803SRod Evans 	if ((fpnp = calloc(sizeof (FullPathNode), 1)) == NULL)
4517c478bd9Sstevel@tonic-gate 		return (0);
4527c478bd9Sstevel@tonic-gate 
4539aa23310Srie 	fpnp->fpn_node.pn_name = name;
4549aa23310Srie 	fpnp->fpn_node.pn_hash = sgs_str_hash(name);
4557c478bd9Sstevel@tonic-gate 	fpnp->fpn_lmp = lmp;
4567c478bd9Sstevel@tonic-gate 
457cce0e03bSab 	if (aplist_append(&FPNODE(lmp), fpnp, AL_CNT_FPNODE) == NULL) {
4587c478bd9Sstevel@tonic-gate 		free(fpnp);
4597c478bd9Sstevel@tonic-gate 		return (0);
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	ASSERT(lml->lm_fpavl != NULL);
4637c478bd9Sstevel@tonic-gate 	avl_insert(lml->lm_fpavl, fpnp, where);
4647c478bd9Sstevel@tonic-gate 	return (1);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4689aa23310Srie  * Remove an object from the FullPath AVL tree.  Note, this is called *before*
4697c478bd9Sstevel@tonic-gate  * the objects link-map is torn down (remove_so), which is where any NAME() and
4707c478bd9Sstevel@tonic-gate  * PATHNAME() strings will be deallocated.
4717c478bd9Sstevel@tonic-gate  */
4727c478bd9Sstevel@tonic-gate void
4737c478bd9Sstevel@tonic-gate fpavl_remove(Rt_map *lmp)
4747c478bd9Sstevel@tonic-gate {
4759aa23310Srie 	FullPathNode	*fpnp;
476cce0e03bSab 	Aliste		idx;
4777c478bd9Sstevel@tonic-gate 
478cce0e03bSab 	for (APLIST_TRAVERSE(FPNODE(lmp), idx, fpnp)) {
4797c478bd9Sstevel@tonic-gate 		avl_remove(LIST(lmp)->lm_fpavl, fpnp);
4807c478bd9Sstevel@tonic-gate 		free(fpnp);
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	free(FPNODE(lmp));
483cce0e03bSab 	FPNODE(lmp) = NULL;
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4869aa23310Srie /*
4879aa23310Srie  * Determine if a pathname has already been recorded on the not-found AVL tree.
4889aa23310Srie  * This tree maintains a node for each path name that ld.so.1 has explicitly
4899aa23310Srie  * inspected, but has failed to load during a single ld.so.1 operation.  If the
4909aa23310Srie  * path name does not exist in this AVL tree, then the next insertion point is
4919aa23310Srie  * deposited in "where".  This value can be used by nfavl_insert() to expedite
4929aa23310Srie  * the insertion.
4939aa23310Srie  */
4949aa23310Srie int
4959aa23310Srie nfavl_recorded(const char *name, avl_index_t *where)
4969aa23310Srie {
4979aa23310Srie 	PathNode	pn;
4989aa23310Srie 
4999aa23310Srie 	/*
5009aa23310Srie 	 * Create the avl tree if required.
5019aa23310Srie 	 */
502*3dbfc803SRod Evans 	if ((nfavl == NULL) &&
503*3dbfc803SRod Evans 	    ((nfavl = pnavl_create(sizeof (PathNode))) == NULL))
504*3dbfc803SRod Evans 		return (0);
5059aa23310Srie 
5069aa23310Srie 	pn.pn_name = name;
5079aa23310Srie 	pn.pn_hash = sgs_str_hash(name);
5089aa23310Srie 
509*3dbfc803SRod Evans 	if (avl_find(nfavl, &pn, where) == NULL)
5109aa23310Srie 		return (0);
5119aa23310Srie 
5129aa23310Srie 	return (1);
5139aa23310Srie }
5149aa23310Srie 
5159aa23310Srie /*
5169aa23310Srie  * Insert a name into the not-found AVL tree.
5179aa23310Srie  */
5189aa23310Srie void
5199aa23310Srie nfavl_insert(const char *name, avl_index_t where)
5209aa23310Srie {
5219aa23310Srie 	PathNode	*pnp;
5229aa23310Srie 
5239aa23310Srie 	if (where == 0) {
5249aa23310Srie 		/* LINTED */
5259aa23310Srie 		int	in_nfavl = nfavl_recorded(name, &where);
5269aa23310Srie 
5279aa23310Srie 		/*
5289aa23310Srie 		 * We better not get a hit now, we do not want duplicates in
5299aa23310Srie 		 * the tree.
5309aa23310Srie 		 */
5319aa23310Srie 		ASSERT(in_nfavl == 0);
5329aa23310Srie 	}
5339aa23310Srie 
5349aa23310Srie 	/*
5359aa23310Srie 	 * Insert new node in tree.
5369aa23310Srie 	 */
5379aa23310Srie 	if ((pnp = calloc(sizeof (PathNode), 1)) != 0) {
5389aa23310Srie 		pnp->pn_name = name;
5399aa23310Srie 		pnp->pn_hash = sgs_str_hash(name);
5409aa23310Srie 		avl_insert(nfavl, pnp, where);
5419aa23310Srie 	}
5429aa23310Srie }
5437c478bd9Sstevel@tonic-gate 
544*3dbfc803SRod Evans static avl_tree_t	*spavl = NULL;
545*3dbfc803SRod Evans 
546*3dbfc803SRod Evans /*
547*3dbfc803SRod Evans  * Search for a path name within the secure path AVL tree.  This tree is used
548*3dbfc803SRod Evans  * to maintain a list of directories in which the dependencies of a secure
549*3dbfc803SRod Evans  * process have been found.  This list provides a fall-back in the case that a
550*3dbfc803SRod Evans  * $ORIGIN expansion is deemed insecure, when the expansion results in a path
551*3dbfc803SRod Evans  * name that has already provided dependencies.
552*3dbfc803SRod Evans  */
553*3dbfc803SRod Evans int
554*3dbfc803SRod Evans spavl_recorded(const char *name, avl_index_t *where)
555*3dbfc803SRod Evans {
556*3dbfc803SRod Evans 	PathNode	pn;
557*3dbfc803SRod Evans 
558*3dbfc803SRod Evans 	/*
559*3dbfc803SRod Evans 	 * Create the avl tree if required.
560*3dbfc803SRod Evans 	 */
561*3dbfc803SRod Evans 	if ((spavl == NULL) &&
562*3dbfc803SRod Evans 	    ((spavl = pnavl_create(sizeof (PathNode))) == NULL))
563*3dbfc803SRod Evans 		return (0);
564*3dbfc803SRod Evans 
565*3dbfc803SRod Evans 	pn.pn_name = name;
566*3dbfc803SRod Evans 	pn.pn_hash = sgs_str_hash(name);
567*3dbfc803SRod Evans 
568*3dbfc803SRod Evans 	if (avl_find(spavl, &pn, where) == NULL)
569*3dbfc803SRod Evans 		return (0);
570*3dbfc803SRod Evans 
571*3dbfc803SRod Evans 	return (1);
572*3dbfc803SRod Evans }
573*3dbfc803SRod Evans 
574*3dbfc803SRod Evans /*
575*3dbfc803SRod Evans  * Insert the directory name, of a full path name,  into the secure path AVL
576*3dbfc803SRod Evans  * tree.
577*3dbfc803SRod Evans  */
578*3dbfc803SRod Evans void
579*3dbfc803SRod Evans spavl_insert(const char *name)
580*3dbfc803SRod Evans {
581*3dbfc803SRod Evans 	char		buffer[PATH_MAX], *str;
582*3dbfc803SRod Evans 	size_t		size;
583*3dbfc803SRod Evans 	avl_index_t	where;
584*3dbfc803SRod Evans 	PathNode	*pnp;
585*3dbfc803SRod Evans 
586*3dbfc803SRod Evans 	/*
587*3dbfc803SRod Evans 	 * Separate the directory name from the path name.
588*3dbfc803SRod Evans 	 */
589*3dbfc803SRod Evans 	if ((str = strrchr(name, '/')) == name)
590*3dbfc803SRod Evans 		size = 1;
591*3dbfc803SRod Evans 	else
592*3dbfc803SRod Evans 		size = str - name;
593*3dbfc803SRod Evans 
594*3dbfc803SRod Evans 	(void) strncpy(buffer, name, size);
595*3dbfc803SRod Evans 	buffer[size] = '\0';
596*3dbfc803SRod Evans 
597*3dbfc803SRod Evans 	/*
598*3dbfc803SRod Evans 	 * Determine whether this directory name is already recorded, or if
599*3dbfc803SRod Evans 	 * not, 'where" will provide the insertion point for the new string.
600*3dbfc803SRod Evans 	 */
601*3dbfc803SRod Evans 	if (spavl_recorded(buffer, &where))
602*3dbfc803SRod Evans 		return;
603*3dbfc803SRod Evans 
604*3dbfc803SRod Evans 	/*
605*3dbfc803SRod Evans 	 * Insert new node in tree.
606*3dbfc803SRod Evans 	 */
607*3dbfc803SRod Evans 	if ((pnp = calloc(sizeof (PathNode), 1)) != 0) {
608*3dbfc803SRod Evans 		pnp->pn_name = strdup(buffer);
609*3dbfc803SRod Evans 		pnp->pn_hash = sgs_str_hash(buffer);
610*3dbfc803SRod Evans 		avl_insert(spavl, pnp, where);
611*3dbfc803SRod Evans 	}
612*3dbfc803SRod Evans }
613*3dbfc803SRod Evans 
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate  * Prior to calling an object, either via a .plt or through dlsym(), make sure
6167c478bd9Sstevel@tonic-gate  * its .init has fired.  Through topological sorting, ld.so.1 attempts to fire
6177c478bd9Sstevel@tonic-gate  * init's in the correct order, however, this order is typically based on needed
6187c478bd9Sstevel@tonic-gate  * dependencies and non-lazy relocation bindings.  Lazy relocations (.plts) can
6197c478bd9Sstevel@tonic-gate  * still occur and result in bindings that were not captured during topological
6207c478bd9Sstevel@tonic-gate  * sorting.  This routine compensates for this lack of binding information, and
6217c478bd9Sstevel@tonic-gate  * provides for dynamic .init firing.
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate void
6249aa23310Srie is_dep_init(Rt_map *dlmp, Rt_map *clmp)
6257c478bd9Sstevel@tonic-gate {
6269aa23310Srie 	Rt_map	**tobj;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * If the caller is an auditor, and the destination isn't, then don't
6307c478bd9Sstevel@tonic-gate 	 * run any .inits (see comments in load_completion()).
6317c478bd9Sstevel@tonic-gate 	 */
6327c478bd9Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
6337c478bd9Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
6347c478bd9Sstevel@tonic-gate 		return;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	if ((dlmp == clmp) || (rtld_flags & (RT_FL_BREADTH | RT_FL_INITFIRST)))
6377c478bd9Sstevel@tonic-gate 		return;
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITDONE)) ==
6407c478bd9Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITDONE))
6417c478bd9Sstevel@tonic-gate 		return;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITCALL)) ==
6447c478bd9Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITCALL)) {
6455aefb655Srie 		DBG_CALL(Dbg_util_no_init(dlmp));
6467c478bd9Sstevel@tonic-gate 		return;
6477c478bd9Sstevel@tonic-gate 	}
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if ((tobj = calloc(2, sizeof (Rt_map *))) != NULL) {
6507c478bd9Sstevel@tonic-gate 		tobj[0] = dlmp;
6517c478bd9Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_DYN);
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate /*
6567c478bd9Sstevel@tonic-gate  * In a threaded environment insure the thread responsible for loading an object
6577c478bd9Sstevel@tonic-gate  * has completed .init processing for that object before any new thread is
6587c478bd9Sstevel@tonic-gate  * allowed to access the object.  This check is only valid with libthread
6597c478bd9Sstevel@tonic-gate  * TI_VERSION 2, where ld.so.1 implements locking through low level mutexes.
6607c478bd9Sstevel@tonic-gate  *
6617c478bd9Sstevel@tonic-gate  * When a new link-map is created, the thread that causes it to be loaded is
6627c478bd9Sstevel@tonic-gate  * identified by THREADID(dlmp).  Compare this with the current thread to
6637c478bd9Sstevel@tonic-gate  * determine if it must be blocked.
6647c478bd9Sstevel@tonic-gate  *
6657c478bd9Sstevel@tonic-gate  * NOTE, there are a number of instances (typically only for .plt processing)
6667c478bd9Sstevel@tonic-gate  * where we must skip this test:
6677c478bd9Sstevel@tonic-gate  *
6687c478bd9Sstevel@tonic-gate  *   .	any thread id of 0 - threads that call thr_exit() may be in this state
6697c478bd9Sstevel@tonic-gate  *	thus we can't deduce what tid they used to be.  Also some of the
6707c478bd9Sstevel@tonic-gate  *	lib/libthread worker threads have this id and must bind (to themselves
6717c478bd9Sstevel@tonic-gate  *	or libc) for libthread to function.
6727c478bd9Sstevel@tonic-gate  *
6737c478bd9Sstevel@tonic-gate  *   .	libthread itself binds to libc, and as libthread is INITFIRST
6747c478bd9Sstevel@tonic-gate  *	libc's .init can't have fired yet.  Luckly libc's .init is not required
6757c478bd9Sstevel@tonic-gate  *	by libthreads binding.
6767c478bd9Sstevel@tonic-gate  *
6777c478bd9Sstevel@tonic-gate  *   .	if the caller is an auditor, and the destination isn't, then don't
6787c478bd9Sstevel@tonic-gate  *	block (see comments in load_completion()).
6797c478bd9Sstevel@tonic-gate  */
6807c478bd9Sstevel@tonic-gate void
681466e2a62Srie /* ARGSUSED2 */
6829aa23310Srie is_dep_ready(Rt_map *dlmp, Rt_map *clmp, int what)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	thread_t	tid;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
6877c478bd9Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
6887c478bd9Sstevel@tonic-gate 		return;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_CONCUR) &&
6917c478bd9Sstevel@tonic-gate 	    ((FLAGS(dlmp) & FLG_RT_INITDONE) == 0) &&
6927c478bd9Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_INITFRST) == 0) &&
6937c478bd9Sstevel@tonic-gate 	    ((tid = rt_thr_self()) != 0) && (THREADID(dlmp) != tid)) {
6948cd45542Sraf 		rtldexit(LIST(dlmp), 1);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate /*
6997c478bd9Sstevel@tonic-gate  * Execute .{preinit|init|fini}array sections
7007c478bd9Sstevel@tonic-gate  */
7017c478bd9Sstevel@tonic-gate void
7025aefb655Srie call_array(Addr *array, uint_t arraysz, Rt_map *lmp, Word shtype)
7037c478bd9Sstevel@tonic-gate {
7045aefb655Srie 	int	start, stop, incr, ndx;
7057c478bd9Sstevel@tonic-gate 	uint_t	arraycnt = (uint_t)(arraysz / sizeof (Addr));
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if (array == NULL)
7087c478bd9Sstevel@tonic-gate 		return;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	/*
7117c478bd9Sstevel@tonic-gate 	 * initarray & preinitarray are walked from beginning to end - while
7127c478bd9Sstevel@tonic-gate 	 * finiarray is walked from end to beginning.
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	if (shtype == SHT_FINI_ARRAY) {
7157c478bd9Sstevel@tonic-gate 		start = arraycnt - 1;
7167c478bd9Sstevel@tonic-gate 		stop = incr = -1;
7177c478bd9Sstevel@tonic-gate 	} else {
7187c478bd9Sstevel@tonic-gate 		start = 0;
7197c478bd9Sstevel@tonic-gate 		stop = arraycnt;
7207c478bd9Sstevel@tonic-gate 		incr = 1;
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	/*
7247c478bd9Sstevel@tonic-gate 	 * Call the .*array[] entries
7257c478bd9Sstevel@tonic-gate 	 */
7265aefb655Srie 	for (ndx = start; ndx != stop; ndx += incr) {
72710a4fa49Srie 		void (*fptr)(void) = (void(*)())array[ndx];
7287c478bd9Sstevel@tonic-gate 
7295aefb655Srie 		DBG_CALL(Dbg_util_call_array(lmp, (void *)fptr, ndx, shtype));
7307c478bd9Sstevel@tonic-gate 
731eccf73c8Srie 		leave(LIST(lmp), 0);
7327c478bd9Sstevel@tonic-gate 		(*fptr)();
733eccf73c8Srie 		(void) enter(0);
7347c478bd9Sstevel@tonic-gate 	}
7357c478bd9Sstevel@tonic-gate }
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate /*
7397c478bd9Sstevel@tonic-gate  * Execute any .init sections.  These are passed to us in an lmp array which
7407c478bd9Sstevel@tonic-gate  * (by default) will have been sorted.
7417c478bd9Sstevel@tonic-gate  */
7427c478bd9Sstevel@tonic-gate void
7439aa23310Srie call_init(Rt_map **tobj, int flag)
7447c478bd9Sstevel@tonic-gate {
7459aa23310Srie 	Rt_map		**_tobj, **_nobj;
7467c478bd9Sstevel@tonic-gate 	static List	pending = { NULL, NULL };
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * If we're in the middle of an INITFIRST, this must complete before
7507c478bd9Sstevel@tonic-gate 	 * any new init's are fired.  In this case add the object list to the
7517c478bd9Sstevel@tonic-gate 	 * pending queue and return.  We'll pick up the queue after any
7527c478bd9Sstevel@tonic-gate 	 * INITFIRST objects have their init's fired.
7537c478bd9Sstevel@tonic-gate 	 */
7547c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_INITFIRST) {
7557c478bd9Sstevel@tonic-gate 		(void) list_append(&pending, tobj);
7567c478bd9Sstevel@tonic-gate 		return;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	/*
7607c478bd9Sstevel@tonic-gate 	 * Traverse the tobj array firing each objects init.
7617c478bd9Sstevel@tonic-gate 	 */
7627c478bd9Sstevel@tonic-gate 	for (_tobj = _nobj = tobj, _nobj++; *_tobj != NULL; _tobj++, _nobj++) {
7639aa23310Srie 		Rt_map	*lmp = *_tobj;
7649aa23310Srie 		void	(*iptr)() = INIT(lmp);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITCALL)
7677c478bd9Sstevel@tonic-gate 			continue;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITCALL;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		/*
7727c478bd9Sstevel@tonic-gate 		 * Establish an initfirst state if necessary - no other inits
773dffec89cSrie 		 * will be fired (because of additional relocation bindings)
774dffec89cSrie 		 * when in this state.
7757c478bd9Sstevel@tonic-gate 		 */
7767c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITFRST)
7777c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_INITFIRST;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr) {
780cce0e03bSab 			Aliste		idx;
781cce0e03bSab 			Bnd_desc 	*bdp;
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 			/*
7847c478bd9Sstevel@tonic-gate 			 * Make sure that all dependencies that have been
7857c478bd9Sstevel@tonic-gate 			 * relocated to are initialized before this objects
7867c478bd9Sstevel@tonic-gate 			 * .init is executed.  This insures that a dependency
7877c478bd9Sstevel@tonic-gate 			 * on an external item that must first be initialized
7887c478bd9Sstevel@tonic-gate 			 * by its associated object is satisfied.
7897c478bd9Sstevel@tonic-gate 			 */
790cce0e03bSab 			for (APLIST_TRAVERSE(DEPENDS(lmp), idx, bdp)) {
7917c478bd9Sstevel@tonic-gate 				if ((bdp->b_flags & BND_REFER) == 0)
7927c478bd9Sstevel@tonic-gate 					continue;
7937c478bd9Sstevel@tonic-gate 				is_dep_ready(bdp->b_depend, lmp, DBG_WAIT_INIT);
7947c478bd9Sstevel@tonic-gate 			}
7955aefb655Srie 			DBG_CALL(Dbg_util_call_init(lmp, flag));
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		if (iptr) {
799eccf73c8Srie 			leave(LIST(lmp), 0);
8007c478bd9Sstevel@tonic-gate 			(*iptr)();
801eccf73c8Srie 			(void) enter(0);
8027c478bd9Sstevel@tonic-gate 		}
8037c478bd9Sstevel@tonic-gate 
8047c478bd9Sstevel@tonic-gate 		call_array(INITARRAY(lmp), INITARRAYSZ(lmp), lmp,
8057c478bd9Sstevel@tonic-gate 		    SHT_INIT_ARRAY);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr)
8085aefb655Srie 			DBG_CALL(Dbg_util_call_init(lmp, DBG_INIT_DONE));
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		/*
8117c478bd9Sstevel@tonic-gate 		 * Set the initdone flag regardless of whether this object
8127c478bd9Sstevel@tonic-gate 		 * actually contains an .init section.  This flag prevents us
8137c478bd9Sstevel@tonic-gate 		 * from processing this section again for an .init and also
8147c478bd9Sstevel@tonic-gate 		 * signifies that a .fini must be called should it exist.
8157c478bd9Sstevel@tonic-gate 		 * Clear the sort field for use in later .fini processing.
8167c478bd9Sstevel@tonic-gate 		 */
8177c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITDONE;
818dffec89cSrie 		SORTVAL(lmp) = -1;
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 		/*
8217c478bd9Sstevel@tonic-gate 		 * Wake anyone up who might be waiting on this .init.
8227c478bd9Sstevel@tonic-gate 		 */
8237c478bd9Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_INITWAIT) {
8245aefb655Srie 			DBG_CALL(Dbg_util_broadcast(lmp));
8257c478bd9Sstevel@tonic-gate 			(void) rt_cond_broadcast(CONDVAR(lmp));
8267c478bd9Sstevel@tonic-gate 			FLAGS1(lmp) &= ~FL1_RT_INITWAIT;
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		/*
8307c478bd9Sstevel@tonic-gate 		 * If we're firing an INITFIRST object, and other objects must
8317c478bd9Sstevel@tonic-gate 		 * be fired which are not INITFIRST, make sure we grab any
8327c478bd9Sstevel@tonic-gate 		 * pending objects that might have been delayed as this
8337c478bd9Sstevel@tonic-gate 		 * INITFIRST was processed.
8347c478bd9Sstevel@tonic-gate 		 */
8357c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_INITFIRST) &&
8367c478bd9Sstevel@tonic-gate 		    ((*_nobj == NULL) || !(FLAGS(*_nobj) & FLG_RT_INITFRST))) {
8379aa23310Srie 			Listnode	*lnp;
8389aa23310Srie 			Rt_map		**pobj;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_INITFIRST;
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 			while ((lnp = pending.head) != NULL) {
8437c478bd9Sstevel@tonic-gate 				if ((pending.head = lnp->next) == NULL)
8447c478bd9Sstevel@tonic-gate 					pending.tail = NULL;
8457c478bd9Sstevel@tonic-gate 				pobj = lnp->data;
8467c478bd9Sstevel@tonic-gate 				free(lnp);
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate 				call_init(pobj, DBG_INIT_PEND);
8497c478bd9Sstevel@tonic-gate 			}
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 	free(tobj);
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate /*
8567c478bd9Sstevel@tonic-gate  * Function called by atexit(3C).  Calls all .fini sections related with the
8577c478bd9Sstevel@tonic-gate  * mains dependent shared libraries in the order in which the shared libraries
8587c478bd9Sstevel@tonic-gate  * have been loaded.  Skip any .fini defined in the main executable, as this
8597c478bd9Sstevel@tonic-gate  * will be called by crt0 (main was never marked as initdone).
8607c478bd9Sstevel@tonic-gate  */
8617c478bd9Sstevel@tonic-gate void
8627c478bd9Sstevel@tonic-gate call_fini(Lm_list * lml, Rt_map ** tobj)
8637c478bd9Sstevel@tonic-gate {
864dffec89cSrie 	Rt_map **_tobj;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	for (_tobj = tobj; *_tobj != NULL; _tobj++) {
867cce0e03bSab 		Rt_map		*clmp, * lmp = *_tobj;
868cce0e03bSab 		Aliste		idx;
869cce0e03bSab 		Bnd_desc	*bdp;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 		/*
8727c478bd9Sstevel@tonic-gate 		 * If concurrency checking isn't enabled only fire .fini if
8737c478bd9Sstevel@tonic-gate 		 * .init has completed.  We collect all .fini sections of
8747c478bd9Sstevel@tonic-gate 		 * objects that had their .init collected, but that doesn't
8757c478bd9Sstevel@tonic-gate 		 * mean at the time that the .init had completed.
8767c478bd9Sstevel@tonic-gate 		 */
8777c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_CONCUR) ||
8787c478bd9Sstevel@tonic-gate 		    (FLAGS(lmp) & FLG_RT_INITDONE)) {
87910a4fa49Srie 			void	(*fptr)(void) = FINI(lmp);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 			if (FINIARRAY(lmp) || fptr) {
8827c478bd9Sstevel@tonic-gate 				/*
8837c478bd9Sstevel@tonic-gate 				 * If concurrency checking is enabled make sure
8847c478bd9Sstevel@tonic-gate 				 * this object's .init is completed before
8857c478bd9Sstevel@tonic-gate 				 * calling any .fini.
8867c478bd9Sstevel@tonic-gate 				 */
8877c478bd9Sstevel@tonic-gate 				is_dep_ready(lmp, lmp, DBG_WAIT_FINI);
8885aefb655Srie 				DBG_CALL(Dbg_util_call_fini(lmp));
8897c478bd9Sstevel@tonic-gate 			}
8907c478bd9Sstevel@tonic-gate 
8915aefb655Srie 			call_array(FINIARRAY(lmp), FINIARRAYSZ(lmp), lmp,
8925aefb655Srie 			    SHT_FINI_ARRAY);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 			if (fptr) {
895eccf73c8Srie 				leave(LIST(lmp), 0);
8967c478bd9Sstevel@tonic-gate 				(*fptr)();
897eccf73c8Srie 				(void) enter(0);
8987c478bd9Sstevel@tonic-gate 			}
8997c478bd9Sstevel@tonic-gate 		}
9007c478bd9Sstevel@tonic-gate 
901dffec89cSrie 		/*
902dffec89cSrie 		 * Skip main, this is explicitly called last in atexit_fini().
903dffec89cSrie 		 */
904dffec89cSrie 		if (FLAGS(lmp) & FLG_RT_ISMAIN)
905dffec89cSrie 			continue;
906dffec89cSrie 
9077c478bd9Sstevel@tonic-gate 		/*
9087c478bd9Sstevel@tonic-gate 		 * Audit `close' operations at this point.  The library has
9097c478bd9Sstevel@tonic-gate 		 * exercised its last instructions (regardless of whether it
9107c478bd9Sstevel@tonic-gate 		 * will be unmapped or not).
9117c478bd9Sstevel@tonic-gate 		 *
9127c478bd9Sstevel@tonic-gate 		 * First call any global auditing.
9137c478bd9Sstevel@tonic-gate 		 */
9147c478bd9Sstevel@tonic-gate 		if (lml->lm_tflags & LML_TFLG_AUD_OBJCLOSE)
9157c478bd9Sstevel@tonic-gate 			_audit_objclose(&(auditors->ad_list), lmp);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		/*
9187c478bd9Sstevel@tonic-gate 		 * Finally determine whether this object has local auditing
9197c478bd9Sstevel@tonic-gate 		 * requirements by inspecting itself and then its dependencies.
9207c478bd9Sstevel@tonic-gate 		 */
9217c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_LOCAUDIT) == 0)
9227c478bd9Sstevel@tonic-gate 			continue;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 		if (FLAGS1(lmp) & LML_TFLG_AUD_OBJCLOSE)
9257c478bd9Sstevel@tonic-gate 			_audit_objclose(&(AUDITORS(lmp)->ad_list), lmp);
9267c478bd9Sstevel@tonic-gate 
927cce0e03bSab 		for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
9287c478bd9Sstevel@tonic-gate 			clmp = bdp->b_caller;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 			if (FLAGS1(clmp) & LML_TFLG_AUD_OBJCLOSE) {
931a953e2b1Srie 				_audit_objclose(&(AUDITORS(clmp)->ad_list),
932a953e2b1Srie 				    lmp);
933a953e2b1Srie 				break;
9347c478bd9Sstevel@tonic-gate 			}
9357c478bd9Sstevel@tonic-gate 		}
9367c478bd9Sstevel@tonic-gate 	}
9375aefb655Srie 	DBG_CALL(Dbg_bind_plt_summary(lml, M_MACH, pltcnt21d, pltcnt24d,
9385aefb655Srie 	    pltcntu32, pltcntu44, pltcntfull, pltcntfar));
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	free(tobj);
9417c478bd9Sstevel@tonic-gate }
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate void
9447c478bd9Sstevel@tonic-gate atexit_fini()
9457c478bd9Sstevel@tonic-gate {
9469aa23310Srie 	Rt_map		**tobj, *lmp;
9479aa23310Srie 	Lm_list		*lml;
9489aa23310Srie 	Listnode	*lnp;
9497c478bd9Sstevel@tonic-gate 
9508cd45542Sraf 	(void) enter(0);
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	rtld_flags |= RT_FL_ATEXIT;
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	lml = &lml_main;
955dffec89cSrie 	lml->lm_flags |= LML_FLG_ATEXIT;
956883c6d49Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
9577c478bd9Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	/*
9607c478bd9Sstevel@tonic-gate 	 * Reverse topologically sort the main link-map for .fini execution.
9617c478bd9Sstevel@tonic-gate 	 */
9627c478bd9Sstevel@tonic-gate 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
9637c478bd9Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
9647c478bd9Sstevel@tonic-gate 		call_fini(lml, tobj);
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/*
967dffec89cSrie 	 * Add an explicit close to main and ld.so.1.  Although main's .fini is
968dffec89cSrie 	 * collected in call_fini() to provide for FINITARRAY processing, its
969dffec89cSrie 	 * audit_objclose is explicitly skipped.  This provides for it to be
970dffec89cSrie 	 * called last, here.  This is the reverse of the explicit calls to
971dffec89cSrie 	 * audit_objopen() made in setup().
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 	if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_MASK) {
9747c478bd9Sstevel@tonic-gate 		audit_objclose(lmp, (Rt_map *)lml_rtld.lm_head);
975dffec89cSrie 		audit_objclose(lmp, lmp);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate 	/*
9797c478bd9Sstevel@tonic-gate 	 * Now that all .fini code has been run, see what unreferenced objects
9809aa23310Srie 	 * remain.
9817c478bd9Sstevel@tonic-gate 	 */
9827c478bd9Sstevel@tonic-gate 	unused(lml);
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 	/*
9857c478bd9Sstevel@tonic-gate 	 * Traverse any alternative link-map lists.
9867c478bd9Sstevel@tonic-gate 	 */
9877c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
98810a4fa49Srie 		/*
98910a4fa49Srie 		 * Ignore the base-link-map list, which has already been
99010a4fa49Srie 		 * processed, and the runtime linkers link-map list, which is
99110a4fa49Srie 		 * typically processed last.
99210a4fa49Srie 		 */
9937c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM))
9947c478bd9Sstevel@tonic-gate 			continue;
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 		if ((lmp = (Rt_map *)lml->lm_head) == 0)
9977c478bd9Sstevel@tonic-gate 			continue;
9987c478bd9Sstevel@tonic-gate 
999dffec89cSrie 		lml->lm_flags |= LML_FLG_ATEXIT;
1000883c6d49Srie 		lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
1001dffec89cSrie 
10027c478bd9Sstevel@tonic-gate 		/*
10037c478bd9Sstevel@tonic-gate 		 * Reverse topologically sort the link-map for .fini execution.
10047c478bd9Sstevel@tonic-gate 		 */
10057c478bd9Sstevel@tonic-gate 		if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
10067c478bd9Sstevel@tonic-gate 		    (tobj != (Rt_map **)S_ERROR))
10077c478bd9Sstevel@tonic-gate 			call_fini(lml, tobj);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		unused(lml);
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 	/*
10137c478bd9Sstevel@tonic-gate 	 * Finally reverse topologically sort the runtime linkers link-map for
10147c478bd9Sstevel@tonic-gate 	 * .fini execution.
10157c478bd9Sstevel@tonic-gate 	 */
10167c478bd9Sstevel@tonic-gate 	lml = &lml_rtld;
1017dffec89cSrie 	lml->lm_flags |= LML_FLG_ATEXIT;
1018883c6d49Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
10197c478bd9Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
10227c478bd9Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
10237c478bd9Sstevel@tonic-gate 		call_fini(lml, tobj);
10247c478bd9Sstevel@tonic-gate 
10258cd45542Sraf 	leave(&lml_main, 0);
10267c478bd9Sstevel@tonic-gate }
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate /*
10307c478bd9Sstevel@tonic-gate  * This routine is called to complete any runtime linker activity which may have
10317c478bd9Sstevel@tonic-gate  * resulted in objects being loaded.  This is called from all user entry points
10327c478bd9Sstevel@tonic-gate  * and from any internal dl*() requests.
10337c478bd9Sstevel@tonic-gate  */
10347c478bd9Sstevel@tonic-gate void
10357247f888Srie load_completion(Rt_map *nlmp)
10367c478bd9Sstevel@tonic-gate {
10377c478bd9Sstevel@tonic-gate 	Rt_map	**tobj = 0;
10387247f888Srie 	Lm_list	*nlml;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/*
10417c478bd9Sstevel@tonic-gate 	 * Establish any .init processing.  Note, in a world of lazy loading,
10427c478bd9Sstevel@tonic-gate 	 * objects may have been loaded regardless of whether the users request
10437c478bd9Sstevel@tonic-gate 	 * was fulfilled (i.e., a dlsym() request may have failed to find a
10447c478bd9Sstevel@tonic-gate 	 * symbol but objects might have been loaded during its search).  Thus,
10457c478bd9Sstevel@tonic-gate 	 * any tsorting starts from the nlmp (new link-maps) pointer and not
10467c478bd9Sstevel@tonic-gate 	 * necessarily from the link-map that may have satisfied the request.
10477c478bd9Sstevel@tonic-gate 	 *
104810a4fa49Srie 	 * Note, the primary link-map has an initialization phase where dynamic
104910a4fa49Srie 	 * .init firing is suppressed.  This provides for a simple and clean
105010a4fa49Srie 	 * handshake with the primary link-maps libc, which is important for
105110a4fa49Srie 	 * establishing uberdata.  In addition, auditors often obtain handles
105210a4fa49Srie 	 * to primary link-map objects as the objects are loaded, so as to
105310a4fa49Srie 	 * inspect the link-map for symbols.  This inspection is allowed without
105410a4fa49Srie 	 * running any code on the primary link-map, as running this code may
105510a4fa49Srie 	 * reenter the auditor, who may not yet have finished its own
10567c478bd9Sstevel@tonic-gate 	 * initialization.
10577c478bd9Sstevel@tonic-gate 	 */
105810a4fa49Srie 	if (nlmp)
105910a4fa49Srie 		nlml = LIST(nlmp);
106010a4fa49Srie 
1061b71d513aSedp 	if (nlmp && nlml->lm_init && ((nlml != &lml_main) ||
1062b71d513aSedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
1063dffec89cSrie 		if ((tobj = tsort(nlmp, LIST(nlmp)->lm_init,
1064dffec89cSrie 		    RT_SORT_REV)) == (Rt_map **)S_ERROR)
10657c478bd9Sstevel@tonic-gate 			tobj = 0;
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 
106810a4fa49Srie 	/*
106910a4fa49Srie 	 * Make sure any alternative link-map retrieves any external interfaces
107010a4fa49Srie 	 * and initializes threads.
107110a4fa49Srie 	 */
107210a4fa49Srie 	if (nlmp && (nlml != &lml_main)) {
107310a4fa49Srie 		(void) rt_get_extern(nlml, nlmp);
107410a4fa49Srie 		rt_thr_init(nlml);
107510a4fa49Srie 	}
107610a4fa49Srie 
107710a4fa49Srie 	/*
107810a4fa49Srie 	 * Traverse the list of new link-maps and register any dynamic TLS.
107910a4fa49Srie 	 * This storage is established for any objects not on the primary
108010a4fa49Srie 	 * link-map, and for any objects added to the primary link-map after
108110a4fa49Srie 	 * static TLS has been registered.
108210a4fa49Srie 	 */
1083b71d513aSedp 	if (nlmp && nlml->lm_tls && ((nlml != &lml_main) ||
1084b71d513aSedp 	    (rtld_flags2 & (RT_FL2_PLMSETUP | RT_FL2_NOPLM)))) {
108510a4fa49Srie 		Rt_map	*lmp;
108610a4fa49Srie 
108710a4fa49Srie 		for (lmp = nlmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
108810a4fa49Srie 			if (PTTLS(lmp) && PTTLS(lmp)->p_memsz)
108910a4fa49Srie 				tls_modaddrem(lmp, TM_FLG_MODADD);
109010a4fa49Srie 		}
109110a4fa49Srie 		nlml->lm_tls = 0;
109210a4fa49Srie 	}
109310a4fa49Srie 
10947c478bd9Sstevel@tonic-gate 	/*
10957c478bd9Sstevel@tonic-gate 	 * Fire any .init's.
10967c478bd9Sstevel@tonic-gate 	 */
10977c478bd9Sstevel@tonic-gate 	if (tobj)
10987c478bd9Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_SORT);
10997c478bd9Sstevel@tonic-gate }
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate /*
11027c478bd9Sstevel@tonic-gate  * Append an item to the specified list, and return a pointer to the list
11037c478bd9Sstevel@tonic-gate  * node created.
11047c478bd9Sstevel@tonic-gate  */
11057c478bd9Sstevel@tonic-gate Listnode *
11067c478bd9Sstevel@tonic-gate list_append(List *lst, const void *item)
11077c478bd9Sstevel@tonic-gate {
11089aa23310Srie 	Listnode	*_lnp;
11097c478bd9Sstevel@tonic-gate 
1110*3dbfc803SRod Evans 	if ((_lnp = malloc(sizeof (Listnode))) == NULL)
1111*3dbfc803SRod Evans 		return (NULL);
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	_lnp->data = (void *)item;
11147c478bd9Sstevel@tonic-gate 	_lnp->next = NULL;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	if (lst->head == NULL)
11177c478bd9Sstevel@tonic-gate 		lst->tail = lst->head = _lnp;
11187c478bd9Sstevel@tonic-gate 	else {
11197c478bd9Sstevel@tonic-gate 		lst->tail->next = _lnp;
11207c478bd9Sstevel@tonic-gate 		lst->tail = lst->tail->next;
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 	return (_lnp);
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate /*
11277c478bd9Sstevel@tonic-gate  * Add an item after specified listnode, and return a pointer to the list
11287c478bd9Sstevel@tonic-gate  * node created.
11297c478bd9Sstevel@tonic-gate  */
11307c478bd9Sstevel@tonic-gate Listnode *
11317c478bd9Sstevel@tonic-gate list_insert(List *lst, const void *item, Listnode *lnp)
11327c478bd9Sstevel@tonic-gate {
11339aa23310Srie 	Listnode	*_lnp;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
11367c478bd9Sstevel@tonic-gate 		return (0);
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 	_lnp->data = (void *)item;
11397c478bd9Sstevel@tonic-gate 	_lnp->next = lnp->next;
11407c478bd9Sstevel@tonic-gate 	if (_lnp->next == NULL)
11417c478bd9Sstevel@tonic-gate 		lst->tail = _lnp;
11427c478bd9Sstevel@tonic-gate 	lnp->next = _lnp;
11437c478bd9Sstevel@tonic-gate 	return (_lnp);
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate /*
11477c478bd9Sstevel@tonic-gate  * Prepend an item to the specified list, and return a pointer to the
11487c478bd9Sstevel@tonic-gate  * list node created.
11497c478bd9Sstevel@tonic-gate  */
11507c478bd9Sstevel@tonic-gate Listnode *
11517c478bd9Sstevel@tonic-gate list_prepend(List * lst, const void * item)
11527c478bd9Sstevel@tonic-gate {
11539aa23310Srie 	Listnode	*_lnp;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
11567c478bd9Sstevel@tonic-gate 		return (0);
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	_lnp->data = (void *)item;
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	if (lst->head == NULL) {
11617c478bd9Sstevel@tonic-gate 		_lnp->next = NULL;
11627c478bd9Sstevel@tonic-gate 		lst->tail = lst->head = _lnp;
11637c478bd9Sstevel@tonic-gate 	} else {
11647c478bd9Sstevel@tonic-gate 		_lnp->next = lst->head;
11657c478bd9Sstevel@tonic-gate 		lst->head = _lnp;
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	return (_lnp);
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate /*
11727c478bd9Sstevel@tonic-gate  * Delete a 'listnode' from a list.
11737c478bd9Sstevel@tonic-gate  */
11747c478bd9Sstevel@tonic-gate void
117502ca3e02Srie list_delete(List *lst, void *item)
11767c478bd9Sstevel@tonic-gate {
117702ca3e02Srie 	Listnode	*clnp, *plnp;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	for (plnp = NULL, clnp = lst->head; clnp; clnp = clnp->next) {
11807c478bd9Sstevel@tonic-gate 		if (item == clnp->data)
11817c478bd9Sstevel@tonic-gate 			break;
11827c478bd9Sstevel@tonic-gate 		plnp = clnp;
11837c478bd9Sstevel@tonic-gate 	}
11847c478bd9Sstevel@tonic-gate 
1185*3dbfc803SRod Evans 	if (clnp == NULL)
11867c478bd9Sstevel@tonic-gate 		return;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	if (lst->head == clnp)
11897c478bd9Sstevel@tonic-gate 		lst->head = clnp->next;
11907c478bd9Sstevel@tonic-gate 	if (lst->tail == clnp)
11917c478bd9Sstevel@tonic-gate 		lst->tail = plnp;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	if (plnp)
11947c478bd9Sstevel@tonic-gate 		plnp->next = clnp->next;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	free(clnp);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate /*
12007c478bd9Sstevel@tonic-gate  * Append an item to the specified link map control list.
12017c478bd9Sstevel@tonic-gate  */
12027c478bd9Sstevel@tonic-gate void
12037c478bd9Sstevel@tonic-gate lm_append(Lm_list *lml, Aliste lmco, Rt_map *lmp)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
12067c478bd9Sstevel@tonic-gate 	int	add = 1;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	/*
12097c478bd9Sstevel@tonic-gate 	 * Indicate that this link-map list has a new object.
12107c478bd9Sstevel@tonic-gate 	 */
12117c478bd9Sstevel@tonic-gate 	(lml->lm_obj)++;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/*
121412b8e62eSrie 	 * If we're about to add a new object to the main link-map control list,
121512b8e62eSrie 	 * alert the debuggers that we are about to mess with this list.
121612b8e62eSrie 	 * Additions of individual objects to the main link-map control list
121712b8e62eSrie 	 * occur during initial setup as the applications immediate dependencies
121812b8e62eSrie 	 * are loaded.  Individual objects are also loaded on the main link-map
121912b8e62eSrie 	 * control list of new alternative link-map control lists.
12207c478bd9Sstevel@tonic-gate 	 */
1221cce0e03bSab 	if ((lmco == ALIST_OFF_DATA) &&
1222cce0e03bSab 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
122312b8e62eSrie 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	/* LINTED */
1226cce0e03bSab 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, lmco);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	/*
12297c478bd9Sstevel@tonic-gate 	 * A link-map list header points to one of more link-map control lists
12307c478bd9Sstevel@tonic-gate 	 * (see include/rtld.h).  The initial list, pointed to by lm_cntl, is
12317c478bd9Sstevel@tonic-gate 	 * the list of relocated objects.  Other lists maintain objects that
12327c478bd9Sstevel@tonic-gate 	 * are still being analyzed or relocated.  This list provides the core
12337c478bd9Sstevel@tonic-gate 	 * link-map list information used by all ld.so.1 routines.
12347c478bd9Sstevel@tonic-gate 	 */
12357c478bd9Sstevel@tonic-gate 	if (lmc->lc_head == NULL) {
12367c478bd9Sstevel@tonic-gate 		/*
12377c478bd9Sstevel@tonic-gate 		 * If this is the first link-map for the given control list,
12387c478bd9Sstevel@tonic-gate 		 * initialize the list.
12397c478bd9Sstevel@tonic-gate 		 */
12407c478bd9Sstevel@tonic-gate 		lmc->lc_head = lmc->lc_tail = lmp;
12417c478bd9Sstevel@tonic-gate 		add = 0;
12427c478bd9Sstevel@tonic-gate 
12439a411307Srie 	} else if (FLAGS(lmp) & FLG_RT_OBJINTPO) {
12447c478bd9Sstevel@tonic-gate 		Rt_map	*tlmp;
12457c478bd9Sstevel@tonic-gate 
12467c478bd9Sstevel@tonic-gate 		/*
12477c478bd9Sstevel@tonic-gate 		 * If this is an interposer then append the link-map following
12487c478bd9Sstevel@tonic-gate 		 * any other interposers (these are objects that have been
12497c478bd9Sstevel@tonic-gate 		 * previously preloaded, or were identified with -z interpose).
12507c478bd9Sstevel@tonic-gate 		 * Interposers can only be inserted on the first link-map
12517c478bd9Sstevel@tonic-gate 		 * control list, as once relocation has started, interposition
12527c478bd9Sstevel@tonic-gate 		 * from new interposers can't be guaranteed.
12537c478bd9Sstevel@tonic-gate 		 *
12547c478bd9Sstevel@tonic-gate 		 * NOTE: We do not interpose on the head of a list.  This model
12557c478bd9Sstevel@tonic-gate 		 * evolved because dynamic executables have already been fully
12567c478bd9Sstevel@tonic-gate 		 * relocated within themselves and thus can't be interposed on.
12577c478bd9Sstevel@tonic-gate 		 * Nowadays it's possible to have shared objects at the head of
12587c478bd9Sstevel@tonic-gate 		 * a list, which conceptually means they could be interposed on.
12597c478bd9Sstevel@tonic-gate 		 * But, shared objects can be created via dldump() and may only
12607c478bd9Sstevel@tonic-gate 		 * be partially relocated (just relatives), in which case they
12617c478bd9Sstevel@tonic-gate 		 * are interposable, but are marked as fixed (ET_EXEC).
12627c478bd9Sstevel@tonic-gate 		 *
12637c478bd9Sstevel@tonic-gate 		 * Thus we really don't have a clear method of deciding when the
12647c478bd9Sstevel@tonic-gate 		 * head of a link-map is interposable.  So, to be consistent,
12657c478bd9Sstevel@tonic-gate 		 * for now only add interposers after the link-map lists head
12667c478bd9Sstevel@tonic-gate 		 * object.
12677c478bd9Sstevel@tonic-gate 		 */
12687c478bd9Sstevel@tonic-gate 		for (tlmp = (Rt_map *)NEXT(lmc->lc_head); tlmp;
12697c478bd9Sstevel@tonic-gate 		    tlmp = (Rt_map *)NEXT(tlmp)) {
12707c478bd9Sstevel@tonic-gate 
12719a411307Srie 			if (FLAGS(tlmp) & FLG_RT_OBJINTPO)
12727c478bd9Sstevel@tonic-gate 				continue;
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 			/*
12757c478bd9Sstevel@tonic-gate 			 * Insert the new link-map before this non-interposer,
12767c478bd9Sstevel@tonic-gate 			 * and indicate an interposer is found.
12777c478bd9Sstevel@tonic-gate 			 */
12787c478bd9Sstevel@tonic-gate 			NEXT((Rt_map *)PREV(tlmp)) = (Link_map *)lmp;
12797c478bd9Sstevel@tonic-gate 			PREV(lmp) = PREV(tlmp);
12807c478bd9Sstevel@tonic-gate 
12817c478bd9Sstevel@tonic-gate 			NEXT(lmp) = (Link_map *)tlmp;
12827c478bd9Sstevel@tonic-gate 			PREV(tlmp) = (Link_map *)lmp;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 			lmc->lc_flags |= LMC_FLG_REANALYZE;
12857c478bd9Sstevel@tonic-gate 			add = 0;
12867c478bd9Sstevel@tonic-gate 			break;
12877c478bd9Sstevel@tonic-gate 		}
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 	/*
12917c478bd9Sstevel@tonic-gate 	 * Fall through to appending the new link map to the tail of the list.
12927c478bd9Sstevel@tonic-gate 	 * If we're processing the initial objects of this link-map list, add
12937c478bd9Sstevel@tonic-gate 	 * them to the backward compatibility list.
12947c478bd9Sstevel@tonic-gate 	 */
12957c478bd9Sstevel@tonic-gate 	if (add) {
12967c478bd9Sstevel@tonic-gate 		NEXT(lmc->lc_tail) = (Link_map *)lmp;
12977c478bd9Sstevel@tonic-gate 		PREV(lmp) = (Link_map *)lmc->lc_tail;
12987c478bd9Sstevel@tonic-gate 		lmc->lc_tail = lmp;
12997c478bd9Sstevel@tonic-gate 	}
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	/*
13027c478bd9Sstevel@tonic-gate 	 * Having added this link-map to a control list, indicate which control
13037c478bd9Sstevel@tonic-gate 	 * list the link-map belongs to.  Note, control list information is
13047c478bd9Sstevel@tonic-gate 	 * always maintained as an offset, as the Alist can be reallocated.
13057c478bd9Sstevel@tonic-gate 	 */
13067c478bd9Sstevel@tonic-gate 	CNTL(lmp) = lmco;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	/*
13097c478bd9Sstevel@tonic-gate 	 * Indicate if an interposer is found.  Note that the first object on a
13107c478bd9Sstevel@tonic-gate 	 * link-map can be explicitly defined as an interposer so that it can
13117c478bd9Sstevel@tonic-gate 	 * provide interposition over direct binding requests.
13127c478bd9Sstevel@tonic-gate 	 */
13139a411307Srie 	if (FLAGS(lmp) & MSK_RT_INTPOSE)
13147c478bd9Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_INTRPOSE;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	/*
13177c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
13187c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
13197c478bd9Sstevel@tonic-gate 	 */
1320cce0e03bSab 	if (lmco == ALIST_OFF_DATA) {
13217c478bd9Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
13227c478bd9Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate }
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate /*
13277c478bd9Sstevel@tonic-gate  * Delete an item from the specified link map control list.
13287c478bd9Sstevel@tonic-gate  */
13297c478bd9Sstevel@tonic-gate void
13307c478bd9Sstevel@tonic-gate lm_delete(Lm_list *lml, Rt_map *lmp)
13317c478bd9Sstevel@tonic-gate {
13327c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate 	/*
13357c478bd9Sstevel@tonic-gate 	 * If the control list pointer hasn't been initialized, this object
13367c478bd9Sstevel@tonic-gate 	 * never got added to a link-map list.
13377c478bd9Sstevel@tonic-gate 	 */
13387c478bd9Sstevel@tonic-gate 	if (CNTL(lmp) == 0)
13397c478bd9Sstevel@tonic-gate 		return;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	/*
134212b8e62eSrie 	 * If we're about to delete an object from the main link-map control
134312b8e62eSrie 	 * list, alert the debuggers that we are about to mess with this list.
13447c478bd9Sstevel@tonic-gate 	 */
1345cce0e03bSab 	if ((CNTL(lmp) == ALIST_OFF_DATA) &&
1346cce0e03bSab 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
13477c478bd9Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_DELETE);
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate 	/* LINTED */
1350cce0e03bSab 	lmc = (Lm_cntl *)alist_item_by_offset(lml->lm_lists, CNTL(lmp));
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate 	if (lmc->lc_head == lmp)
13537c478bd9Sstevel@tonic-gate 		lmc->lc_head = (Rt_map *)NEXT(lmp);
13547c478bd9Sstevel@tonic-gate 	else
13557c478bd9Sstevel@tonic-gate 		NEXT((Rt_map *)PREV(lmp)) = (void *)NEXT(lmp);
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	if (lmc->lc_tail == lmp)
13587c478bd9Sstevel@tonic-gate 		lmc->lc_tail = (Rt_map *)PREV(lmp);
13597c478bd9Sstevel@tonic-gate 	else
13607c478bd9Sstevel@tonic-gate 		PREV((Rt_map *)NEXT(lmp)) = PREV(lmp);
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate 	/*
13637c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
13647c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
13657c478bd9Sstevel@tonic-gate 	 */
1366cce0e03bSab 	if (lmc == (Lm_cntl *)&lml->lm_lists->al_data) {
13677c478bd9Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
13687c478bd9Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/*
13727c478bd9Sstevel@tonic-gate 	 * Indicate we have one less object on this control list.
13737c478bd9Sstevel@tonic-gate 	 */
13747c478bd9Sstevel@tonic-gate 	(lml->lm_obj)--;
13757c478bd9Sstevel@tonic-gate }
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate /*
13787c478bd9Sstevel@tonic-gate  * Move a link-map control list to another.  Objects that are being relocated
13797c478bd9Sstevel@tonic-gate  * are maintained on secondary control lists.  Once their relocation is
13807c478bd9Sstevel@tonic-gate  * complete, the entire list is appended to the previous control list, as this
13817c478bd9Sstevel@tonic-gate  * list must have been the trigger for generating the new control list.
13827c478bd9Sstevel@tonic-gate  */
13837c478bd9Sstevel@tonic-gate void
13847c478bd9Sstevel@tonic-gate lm_move(Lm_list *lml, Aliste nlmco, Aliste plmco, Lm_cntl *nlmc, Lm_cntl *plmc)
13857c478bd9Sstevel@tonic-gate {
13867c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	/*
138912b8e62eSrie 	 * If we're about to add a new family of objects to the main link-map
139012b8e62eSrie 	 * control list, alert the debuggers that we are about to mess with this
139112b8e62eSrie 	 * list.  Additions of object families to the main link-map control
139212b8e62eSrie 	 * list occur during lazy loading, filtering and dlopen().
13937c478bd9Sstevel@tonic-gate 	 */
1394cce0e03bSab 	if ((plmco == ALIST_OFF_DATA) &&
1395cce0e03bSab 	    ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
13967c478bd9Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
13977c478bd9Sstevel@tonic-gate 
139812b8e62eSrie 	DBG_CALL(Dbg_file_cntl(lml, nlmco, plmco));
139912b8e62eSrie 
14007c478bd9Sstevel@tonic-gate 	/*
14017c478bd9Sstevel@tonic-gate 	 * Indicate each new link-map has been moved to the previous link-map
14027c478bd9Sstevel@tonic-gate 	 * control list.
14037c478bd9Sstevel@tonic-gate 	 */
14047c478bd9Sstevel@tonic-gate 	for (lmp = nlmc->lc_head; lmp; lmp = (Rt_map *)NEXT(lmp))
14057c478bd9Sstevel@tonic-gate 		CNTL(lmp) = plmco;
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	/*
14087c478bd9Sstevel@tonic-gate 	 * Move the new link-map control list, to the callers link-map control
14097c478bd9Sstevel@tonic-gate 	 * list.
14107c478bd9Sstevel@tonic-gate 	 */
1411*3dbfc803SRod Evans 	if (plmc->lc_head == NULL) {
14127c478bd9Sstevel@tonic-gate 		plmc->lc_head = nlmc->lc_head;
1413*3dbfc803SRod Evans 		PREV(nlmc->lc_head) = NULL;
14147c478bd9Sstevel@tonic-gate 	} else {
14157c478bd9Sstevel@tonic-gate 		NEXT(plmc->lc_tail) = (Link_map *)nlmc->lc_head;
14167c478bd9Sstevel@tonic-gate 		PREV(nlmc->lc_head) = (Link_map *)plmc->lc_tail;
14177c478bd9Sstevel@tonic-gate 	}
14187c478bd9Sstevel@tonic-gate 
14197c478bd9Sstevel@tonic-gate 	plmc->lc_tail = nlmc->lc_tail;
1420*3dbfc803SRod Evans 	nlmc->lc_head = nlmc->lc_tail = NULL;
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	/*
14237c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
14247c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
14257c478bd9Sstevel@tonic-gate 	 */
1426cce0e03bSab 	if (plmco == ALIST_OFF_DATA) {
14277c478bd9Sstevel@tonic-gate 		lml->lm_head = plmc->lc_head;
14287c478bd9Sstevel@tonic-gate 		lml->lm_tail = plmc->lc_tail;
14297c478bd9Sstevel@tonic-gate 	}
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate /*
14337c478bd9Sstevel@tonic-gate  * Environment variables can have a variety of defined permutations, and thus
14347c478bd9Sstevel@tonic-gate  * the following infrastructure exists to allow this variety and to select the
14357c478bd9Sstevel@tonic-gate  * required definition.
14367c478bd9Sstevel@tonic-gate  *
14377c478bd9Sstevel@tonic-gate  * Environment variables can be defined as 32- or 64-bit specific, and if so
14387c478bd9Sstevel@tonic-gate  * they will take precedence over any instruction set neutral form.  Typically
14397c478bd9Sstevel@tonic-gate  * this is only useful when the environment value is an informational string.
14407c478bd9Sstevel@tonic-gate  *
14417c478bd9Sstevel@tonic-gate  * Environment variables may be obtained from the standard user environment or
14427c478bd9Sstevel@tonic-gate  * from a configuration file.  The latter provides a fallback if no user
14437c478bd9Sstevel@tonic-gate  * environment setting is found, and can take two forms:
14447c478bd9Sstevel@tonic-gate  *
14457c478bd9Sstevel@tonic-gate  *  .	a replaceable definition - this will be used if no user environment
14467c478bd9Sstevel@tonic-gate  *	setting has been seen, or
14477c478bd9Sstevel@tonic-gate  *
14487c478bd9Sstevel@tonic-gate  *  .	an permanent definition - this will be used no matter what user
14497c478bd9Sstevel@tonic-gate  *	environment setting is seen.  In the case of list variables it will be
14507c478bd9Sstevel@tonic-gate  *	appended to any process environment setting seen.
14517c478bd9Sstevel@tonic-gate  *
14527c478bd9Sstevel@tonic-gate  * Environment variables can be defined without a value (ie. LD_XXXX=) so as to
14537c478bd9Sstevel@tonic-gate  * override any replaceable environment variables from a configuration file.
14547c478bd9Sstevel@tonic-gate  */
14557c478bd9Sstevel@tonic-gate static	u_longlong_t		rplgen;		/* replaceable generic */
14567c478bd9Sstevel@tonic-gate 						/*	variables */
14577c478bd9Sstevel@tonic-gate static	u_longlong_t		rplisa;		/* replaceable ISA specific */
14587c478bd9Sstevel@tonic-gate 						/*	variables */
14597c478bd9Sstevel@tonic-gate static	u_longlong_t		prmgen;		/* permanent generic */
14607c478bd9Sstevel@tonic-gate 						/*	variables */
14617c478bd9Sstevel@tonic-gate static	u_longlong_t		prmisa;		/* permanent ISA specific */
14627c478bd9Sstevel@tonic-gate 						/*	variables */
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate /*
14657c478bd9Sstevel@tonic-gate  * Classify an environment variables type.
14667c478bd9Sstevel@tonic-gate  */
14677c478bd9Sstevel@tonic-gate #define	ENV_TYP_IGNORE		0x1		/* ignore - variable is for */
14687c478bd9Sstevel@tonic-gate 						/*	the wrong ISA */
14697c478bd9Sstevel@tonic-gate #define	ENV_TYP_ISA		0x2		/* variable is ISA specific */
14707c478bd9Sstevel@tonic-gate #define	ENV_TYP_CONFIG		0x4		/* variable obtained from a */
14717c478bd9Sstevel@tonic-gate 						/*	config file */
14727c478bd9Sstevel@tonic-gate #define	ENV_TYP_PERMANT		0x8		/* variable is permanent */
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate /*
14757c478bd9Sstevel@tonic-gate  * Identify all environment variables.
14767c478bd9Sstevel@tonic-gate  */
14777c478bd9Sstevel@tonic-gate #define	ENV_FLG_AUDIT		0x0000000001ULL
14787c478bd9Sstevel@tonic-gate #define	ENV_FLG_AUDIT_ARGS	0x0000000002ULL
14797c478bd9Sstevel@tonic-gate #define	ENV_FLG_BIND_NOW	0x0000000004ULL
14807c478bd9Sstevel@tonic-gate #define	ENV_FLG_BIND_NOT	0x0000000008ULL
14817c478bd9Sstevel@tonic-gate #define	ENV_FLG_BINDINGS	0x0000000010ULL
14827c478bd9Sstevel@tonic-gate #define	ENV_FLG_CONCURRENCY	0x0000000020ULL
14837c478bd9Sstevel@tonic-gate #define	ENV_FLG_CONFGEN		0x0000000040ULL
14847c478bd9Sstevel@tonic-gate #define	ENV_FLG_CONFIG		0x0000000080ULL
14857c478bd9Sstevel@tonic-gate #define	ENV_FLG_DEBUG		0x0000000100ULL
14867c478bd9Sstevel@tonic-gate #define	ENV_FLG_DEBUG_OUTPUT	0x0000000200ULL
14877c478bd9Sstevel@tonic-gate #define	ENV_FLG_DEMANGLE	0x0000000400ULL
14887c478bd9Sstevel@tonic-gate #define	ENV_FLG_FLAGS		0x0000000800ULL
14897c478bd9Sstevel@tonic-gate #define	ENV_FLG_INIT		0x0000001000ULL
14907c478bd9Sstevel@tonic-gate #define	ENV_FLG_LIBPATH		0x0000002000ULL
14917c478bd9Sstevel@tonic-gate #define	ENV_FLG_LOADAVAIL	0x0000004000ULL
14927c478bd9Sstevel@tonic-gate #define	ENV_FLG_LOADFLTR	0x0000008000ULL
14937c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOAUDIT		0x0000010000ULL
14947c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOAUXFLTR	0x0000020000ULL
14957c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOBAPLT		0x0000040000ULL
14967c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOCONFIG	0x0000080000ULL
14977c478bd9Sstevel@tonic-gate #define	ENV_FLG_NODIRCONFIG	0x0000100000ULL
14987c478bd9Sstevel@tonic-gate #define	ENV_FLG_NODIRECT	0x0000200000ULL
14997c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOENVCONFIG	0x0000400000ULL
15007c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOLAZY		0x0000800000ULL
15017c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOOBJALTER	0x0001000000ULL
15027c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOVERSION	0x0002000000ULL
15037c478bd9Sstevel@tonic-gate #define	ENV_FLG_PRELOAD		0x0004000000ULL
15047c478bd9Sstevel@tonic-gate #define	ENV_FLG_PROFILE		0x0008000000ULL
15057c478bd9Sstevel@tonic-gate #define	ENV_FLG_PROFILE_OUTPUT	0x0010000000ULL
15067c478bd9Sstevel@tonic-gate #define	ENV_FLG_SIGNAL		0x0020000000ULL
15077c478bd9Sstevel@tonic-gate #define	ENV_FLG_TRACE_OBJS	0x0040000000ULL
15087c478bd9Sstevel@tonic-gate #define	ENV_FLG_TRACE_PTHS	0x0080000000ULL
15097c478bd9Sstevel@tonic-gate #define	ENV_FLG_UNREF		0x0100000000ULL
15107c478bd9Sstevel@tonic-gate #define	ENV_FLG_UNUSED		0x0200000000ULL
15117c478bd9Sstevel@tonic-gate #define	ENV_FLG_VERBOSE		0x0400000000ULL
15127c478bd9Sstevel@tonic-gate #define	ENV_FLG_WARN		0x0800000000ULL
15137c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOFLTCONFIG	0x1000000000ULL
1514dffec89cSrie #define	ENV_FLG_BIND_LAZY	0x2000000000ULL
1515df4628cbSrie #define	ENV_FLG_NOUNRESWEAK	0x4000000000ULL
1516dae2dfb7Srie #define	ENV_FLG_NOPAREXT	0x8000000000ULL
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
15197c478bd9Sstevel@tonic-gate #define	ENV_FLG_FIX_1		0x8000000000ULL
15207c478bd9Sstevel@tonic-gate #endif
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate #define	SEL_REPLACE		0x0001
15237c478bd9Sstevel@tonic-gate #define	SEL_PERMANT		0x0002
15247c478bd9Sstevel@tonic-gate #define	SEL_ACT_RT		0x0100	/* setting rtld_flags */
15257c478bd9Sstevel@tonic-gate #define	SEL_ACT_RT2		0x0200	/* setting rtld_flags2 */
15267c478bd9Sstevel@tonic-gate #define	SEL_ACT_STR		0x0400	/* setting string value */
15277c478bd9Sstevel@tonic-gate #define	SEL_ACT_LML		0x0800	/* setting lml_flags */
15287c478bd9Sstevel@tonic-gate #define	SEL_ACT_LMLT		0x1000	/* setting lml_tflags */
15297c478bd9Sstevel@tonic-gate #define	SEL_ACT_SPEC_1		0x2000	/* For FLG_{FLAGS, LIBPATH} */
15307c478bd9Sstevel@tonic-gate #define	SEL_ACT_SPEC_2		0x4000	/* need special handling */
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate /*
15337c478bd9Sstevel@tonic-gate  * Pattern match an LD_XXXX environment variable.  s1 points to the XXXX part
15347c478bd9Sstevel@tonic-gate  * and len specifies its length (comparing a strings length before the string
15357c478bd9Sstevel@tonic-gate  * itself speed things up).  s2 points to the token itself which has already
15367c478bd9Sstevel@tonic-gate  * had any leading white-space removed.
15377c478bd9Sstevel@tonic-gate  */
15387c478bd9Sstevel@tonic-gate static void
15397c478bd9Sstevel@tonic-gate ld_generic_env(const char *s1, size_t len, const char *s2, Word *lmflags,
15407c478bd9Sstevel@tonic-gate     Word *lmtflags, uint_t env_flags, int aout)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	u_longlong_t	variable = 0;
154310a4fa49Srie 	ushort_t	select = 0;
154410a4fa49Srie 	const char	**str;
154510a4fa49Srie 	Word		val = 0;
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * Determine whether we're dealing with a replaceable or permanent
15497c478bd9Sstevel@tonic-gate 	 * string.
15507c478bd9Sstevel@tonic-gate 	 */
15517c478bd9Sstevel@tonic-gate 	if (env_flags & ENV_TYP_PERMANT) {
15527c478bd9Sstevel@tonic-gate 		/*
15537c478bd9Sstevel@tonic-gate 		 * If the string is from a configuration file and defined as
15547c478bd9Sstevel@tonic-gate 		 * permanent, assign it as permanent.
15557c478bd9Sstevel@tonic-gate 		 */
15567c478bd9Sstevel@tonic-gate 		select |= SEL_PERMANT;
15577c478bd9Sstevel@tonic-gate 	} else
15587c478bd9Sstevel@tonic-gate 		select |= SEL_REPLACE;
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 	/*
15617c478bd9Sstevel@tonic-gate 	 * Parse the variable given.
15627c478bd9Sstevel@tonic-gate 	 *
15637c478bd9Sstevel@tonic-gate 	 * The LD_AUDIT family.
15647c478bd9Sstevel@tonic-gate 	 */
15657c478bd9Sstevel@tonic-gate 	if (*s1 == 'A') {
15667c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_AUDIT_SIZE) && (strncmp(s1,
15677c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_AUDIT), MSG_LD_AUDIT_SIZE) == 0)) {
15687c478bd9Sstevel@tonic-gate 			/*
15697c478bd9Sstevel@tonic-gate 			 * Replaceable and permanent audit objects can exist.
15707c478bd9Sstevel@tonic-gate 			 */
15717c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1572fa034d7fSseizo 			if (select & SEL_REPLACE)
1573fa034d7fSseizo 				str = &rpl_audit;
1574fa034d7fSseizo 			else {
1575fa034d7fSseizo 				str = &prm_audit;
1576fa034d7fSseizo 				rpl_audit = 0;
1577fa034d7fSseizo 			}
15787c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT;
15797c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_AUDIT_ARGS_SIZE) &&
15807c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_AUDIT_ARGS),
15817c478bd9Sstevel@tonic-gate 		    MSG_LD_AUDIT_ARGS_SIZE) == 0)) {
15827c478bd9Sstevel@tonic-gate 			/*
15837c478bd9Sstevel@tonic-gate 			 * A specialized variable for plt_exit() use, not
15847c478bd9Sstevel@tonic-gate 			 * documented for general use.
15857c478bd9Sstevel@tonic-gate 			 */
15867c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15877c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT_ARGS;
15887c478bd9Sstevel@tonic-gate 		}
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * The LD_BIND family and LD_BREADTH (historic).
15927c478bd9Sstevel@tonic-gate 	 */
15937c478bd9Sstevel@tonic-gate 	else if (*s1 == 'B') {
1594dffec89cSrie 		if ((len == MSG_LD_BIND_LAZY_SIZE) && (strncmp(s1,
1595dffec89cSrie 		    MSG_ORIG(MSG_LD_BIND_LAZY),
1596dffec89cSrie 		    MSG_LD_BIND_LAZY_SIZE) == 0)) {
1597dffec89cSrie 			select |= SEL_ACT_RT2;
1598dffec89cSrie 			val = RT_FL2_BINDLAZY;
1599dffec89cSrie 			variable = ENV_FLG_BIND_LAZY;
1600dffec89cSrie 		} else if ((len == MSG_LD_BIND_NOW_SIZE) && (strncmp(s1,
16017c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOW), MSG_LD_BIND_NOW_SIZE) == 0)) {
16027c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
16037c478bd9Sstevel@tonic-gate 			val = RT_FL2_BINDNOW;
16047c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOW;
16057c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BIND_NOT_SIZE) && (strncmp(s1,
16067c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOT), MSG_LD_BIND_NOT_SIZE) == 0)) {
16077c478bd9Sstevel@tonic-gate 			/*
16087c478bd9Sstevel@tonic-gate 			 * Another trick, enabled to help debug AOUT
16097c478bd9Sstevel@tonic-gate 			 * applications under BCP, but not documented for
16107c478bd9Sstevel@tonic-gate 			 * general use.
16117c478bd9Sstevel@tonic-gate 			 */
16127c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16137c478bd9Sstevel@tonic-gate 			val = RT_FL_NOBIND;
16147c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOT;
16157c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BINDINGS_SIZE) && (strncmp(s1,
16167c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BINDINGS), MSG_LD_BINDINGS_SIZE) == 0)) {
16177c478bd9Sstevel@tonic-gate 			/*
16187c478bd9Sstevel@tonic-gate 			 * This variable is simply for backward compatibility.
16197c478bd9Sstevel@tonic-gate 			 * If this and LD_DEBUG are both specified, only one of
16207c478bd9Sstevel@tonic-gate 			 * the strings is going to get processed.
16217c478bd9Sstevel@tonic-gate 			 */
16227c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
16237c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BINDINGS;
16247c478bd9Sstevel@tonic-gate #ifndef LD_BREADTH_DISABLED
16257c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BREADTH_SIZE) && (strncmp(s1,
16267c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BREADTH), MSG_LD_BREADTH_SIZE) == 0)) {
16277c478bd9Sstevel@tonic-gate 			/*
16287c478bd9Sstevel@tonic-gate 			 * Besides some old patches this is no longer available.
16297c478bd9Sstevel@tonic-gate 			 */
16307c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_BREADTH;
16317c478bd9Sstevel@tonic-gate 			return;
16327c478bd9Sstevel@tonic-gate #endif
16337c478bd9Sstevel@tonic-gate 		}
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate 	/*
16367c478bd9Sstevel@tonic-gate 	 * LD_CONCURRENCY and LD_CONFIG family.
16377c478bd9Sstevel@tonic-gate 	 */
16387c478bd9Sstevel@tonic-gate 	else if (*s1 == 'C') {
16397c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_CONCURRENCY_SIZE) && (strncmp(s1,
16407c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONCURRENCY),
16417c478bd9Sstevel@tonic-gate 		    MSG_LD_CONCURRENCY_SIZE) == 0)) {
16427c478bd9Sstevel@tonic-gate 			/*
16437c478bd9Sstevel@tonic-gate 			 * Waiting in the wings, as concurrency checking isn't
16447c478bd9Sstevel@tonic-gate 			 * yet enabled.
16457c478bd9Sstevel@tonic-gate 			 */
16467c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
16477c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONCURRENCY;
16487c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFGEN_SIZE) && (strncmp(s1,
16497c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFGEN), MSG_LD_CONFGEN_SIZE) == 0)) {
16507c478bd9Sstevel@tonic-gate 			/*
16517c478bd9Sstevel@tonic-gate 			 * Set by crle(1) to indicate it's building a
16527c478bd9Sstevel@tonic-gate 			 * configuration file, not documented for general use.
16537c478bd9Sstevel@tonic-gate 			 */
16547c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
16557c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONFGEN;
16567c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFIG_SIZE) && (strncmp(s1,
16577c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFIG), MSG_LD_CONFIG_SIZE) == 0)) {
16587c478bd9Sstevel@tonic-gate 			/*
16597c478bd9Sstevel@tonic-gate 			 * Secure applications must use a default configuration
16607c478bd9Sstevel@tonic-gate 			 * file.  A setting from a configuration file doesn't
16617c478bd9Sstevel@tonic-gate 			 * make sense (given we must be reading a configuration
16627c478bd9Sstevel@tonic-gate 			 * file to have gotten this).
16637c478bd9Sstevel@tonic-gate 			 */
16647c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_SECURE) ||
16657c478bd9Sstevel@tonic-gate 			    (env_flags & ENV_TYP_CONFIG))
16667c478bd9Sstevel@tonic-gate 				return;
16677c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
16687c478bd9Sstevel@tonic-gate 			str = &config->c_name;
16697c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONFIG;
16707c478bd9Sstevel@tonic-gate 		}
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 	/*
16737c478bd9Sstevel@tonic-gate 	 * The LD_DEBUG family and LD_DEMANGLE.
16747c478bd9Sstevel@tonic-gate 	 */
16757c478bd9Sstevel@tonic-gate 	else if (*s1 == 'D') {
16767c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_DEBUG_SIZE) && (strncmp(s1,
16777c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG), MSG_LD_DEBUG_SIZE) == 0)) {
16787c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1679fa034d7fSseizo 			if (select & SEL_REPLACE)
1680fa034d7fSseizo 				str = &rpl_debug;
1681fa034d7fSseizo 			else {
1682fa034d7fSseizo 				str = &prm_debug;
1683fa034d7fSseizo 				rpl_debug = 0;
1684fa034d7fSseizo 			}
16857c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG;
16867c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEBUG_OUTPUT_SIZE) && (strncmp(s1,
16877c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG_OUTPUT),
16887c478bd9Sstevel@tonic-gate 		    MSG_LD_DEBUG_OUTPUT_SIZE) == 0)) {
16897c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
16907c478bd9Sstevel@tonic-gate 			str = &dbg_file;
16917c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG_OUTPUT;
16927c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEMANGLE_SIZE) && (strncmp(s1,
16937c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEMANGLE), MSG_LD_DEMANGLE_SIZE) == 0)) {
16947c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16957c478bd9Sstevel@tonic-gate 			val = RT_FL_DEMANGLE;
16967c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEMANGLE;
16977c478bd9Sstevel@tonic-gate 		}
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 	/*
17007c478bd9Sstevel@tonic-gate 	 * LD_FLAGS - collect the best variable definition.  On completion of
17017c478bd9Sstevel@tonic-gate 	 * environment variable processing pass the result to ld_flags_env()
17027c478bd9Sstevel@tonic-gate 	 * where they'll be decomposed and passed back to this routine.
17037c478bd9Sstevel@tonic-gate 	 */
17047c478bd9Sstevel@tonic-gate 	else if (*s1 == 'F') {
17057c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_FLAGS_SIZE) && (strncmp(s1,
17067c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FLAGS), MSG_LD_FLAGS_SIZE) == 0)) {
17077c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1708fa034d7fSseizo 			if (select & SEL_REPLACE)
1709fa034d7fSseizo 				str = &rpl_ldflags;
1710fa034d7fSseizo 			else {
1711fa034d7fSseizo 				str = &prm_ldflags;
1712fa034d7fSseizo 				rpl_ldflags = 0;
1713fa034d7fSseizo 			}
17147c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_FLAGS;
17157c478bd9Sstevel@tonic-gate 		}
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate 	/*
17187c478bd9Sstevel@tonic-gate 	 * LD_INIT (internal, used by ldd(1)).
17197c478bd9Sstevel@tonic-gate 	 */
17207c478bd9Sstevel@tonic-gate 	else if (*s1 == 'I') {
17217c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_INIT_SIZE) && (strncmp(s1,
17227c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_INIT), MSG_LD_INIT_SIZE) == 0)) {
17237c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
17247c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_INIT;
17257c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_INIT;
17267c478bd9Sstevel@tonic-gate 		}
17277c478bd9Sstevel@tonic-gate 	}
17287c478bd9Sstevel@tonic-gate 	/*
17297c478bd9Sstevel@tonic-gate 	 * The LD_LIBRARY_PATH and LD_LOAD families.
17307c478bd9Sstevel@tonic-gate 	 */
17317c478bd9Sstevel@tonic-gate 	else if (*s1 == 'L') {
17327c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_LIBPATH_SIZE) && (strncmp(s1,
17337c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LIBPATH), MSG_LD_LIBPATH_SIZE) == 0)) {
17347c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1735fa034d7fSseizo 			if (select & SEL_REPLACE)
1736fa034d7fSseizo 				str = &rpl_libpath;
1737fa034d7fSseizo 			else {
1738fa034d7fSseizo 				str = &prm_libpath;
1739fa034d7fSseizo 				rpl_libpath = 0;
1740fa034d7fSseizo 			}
17417c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LIBPATH;
17427c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADAVAIL_SIZE) && (strncmp(s1,
17437c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADAVAIL), MSG_LD_LOADAVAIL_SIZE) == 0)) {
17447c478bd9Sstevel@tonic-gate 			/*
17457c478bd9Sstevel@tonic-gate 			 * Internal use by crle(1), not documented for general
17467c478bd9Sstevel@tonic-gate 			 * use.
17477c478bd9Sstevel@tonic-gate 			 */
17487c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
17497c478bd9Sstevel@tonic-gate 			val = LML_FLG_LOADAVAIL;
17507c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LOADAVAIL;
17517c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADFLTR_SIZE) && (strncmp(s1,
17527c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADFLTR), MSG_LD_LOADFLTR_SIZE) == 0)) {
17537c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
17547c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LOADFLTR;
17557c478bd9Sstevel@tonic-gate 		}
17567c478bd9Sstevel@tonic-gate 	}
17577c478bd9Sstevel@tonic-gate 	/*
17587c478bd9Sstevel@tonic-gate 	 * The LD_NO family.
17597c478bd9Sstevel@tonic-gate 	 */
17607c478bd9Sstevel@tonic-gate 	else if (*s1 == 'N') {
17617c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_NOAUDIT_SIZE) && (strncmp(s1,
17627c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUDIT), MSG_LD_NOAUDIT_SIZE) == 0)) {
17637c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17647c478bd9Sstevel@tonic-gate 			val = RT_FL_NOAUDIT;
17657c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOAUDIT;
17667c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOAUXFLTR_SIZE) && (strncmp(s1,
17677c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUXFLTR), MSG_LD_NOAUXFLTR_SIZE) == 0)) {
17687c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17697c478bd9Sstevel@tonic-gate 			val = RT_FL_NOAUXFLTR;
17707c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOAUXFLTR;
17717c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOBAPLT_SIZE) && (strncmp(s1,
17727c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOBAPLT), MSG_LD_NOBAPLT_SIZE) == 0)) {
17737c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17747c478bd9Sstevel@tonic-gate 			val = RT_FL_NOBAPLT;
17757c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOBAPLT;
17767c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOCONFIG_SIZE) && (strncmp(s1,
17777c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOCONFIG), MSG_LD_NOCONFIG_SIZE) == 0)) {
17787c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17797c478bd9Sstevel@tonic-gate 			val = RT_FL_NOCFG;
17807c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOCONFIG;
17817c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRCONFIG_SIZE) && (strncmp(s1,
17827c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRCONFIG),
17837c478bd9Sstevel@tonic-gate 		    MSG_LD_NODIRCONFIG_SIZE) == 0)) {
17847c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17857c478bd9Sstevel@tonic-gate 			val = RT_FL_NODIRCFG;
17867c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NODIRCONFIG;
17877c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRECT_SIZE) && (strncmp(s1,
17887c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRECT), MSG_LD_NODIRECT_SIZE) == 0)) {
17897c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
17907c478bd9Sstevel@tonic-gate 			val = LML_TFLG_NODIRECT;
17917c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NODIRECT;
17927c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOENVCONFIG_SIZE) && (strncmp(s1,
17937c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOENVCONFIG),
17947c478bd9Sstevel@tonic-gate 		    MSG_LD_NOENVCONFIG_SIZE) == 0)) {
17957c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
17967c478bd9Sstevel@tonic-gate 			val = RT_FL_NOENVCFG;
17977c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOENVCONFIG;
17987c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOFLTCONFIG_SIZE) && (strncmp(s1,
17997c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOFLTCONFIG),
18007c478bd9Sstevel@tonic-gate 		    MSG_LD_NOFLTCONFIG_SIZE) == 0)) {
18017c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
18027c478bd9Sstevel@tonic-gate 			val = RT_FL2_NOFLTCFG;
18037c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOFLTCONFIG;
18047c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOLAZY_SIZE) && (strncmp(s1,
18057c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOLAZY), MSG_LD_NOLAZY_SIZE) == 0)) {
18067c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
18077c478bd9Sstevel@tonic-gate 			val = LML_TFLG_NOLAZYLD;
18087c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOLAZY;
18097c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOOBJALTER_SIZE) && (strncmp(s1,
18107c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOOBJALTER),
18117c478bd9Sstevel@tonic-gate 		    MSG_LD_NOOBJALTER_SIZE) == 0)) {
18127c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
18137c478bd9Sstevel@tonic-gate 			val = RT_FL_NOOBJALT;
18147c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOOBJALTER;
18157c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOVERSION_SIZE) && (strncmp(s1,
18167c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOVERSION), MSG_LD_NOVERSION_SIZE) == 0)) {
18177c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
18187c478bd9Sstevel@tonic-gate 			val = RT_FL_NOVERSION;
18197c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOVERSION;
1820df4628cbSrie 		} else if ((len == MSG_LD_NOUNRESWEAK_SIZE) && (strncmp(s1,
1821df4628cbSrie 		    MSG_ORIG(MSG_LD_NOUNRESWEAK),
1822df4628cbSrie 		    MSG_LD_NOUNRESWEAK_SIZE) == 0)) {
1823df4628cbSrie 			/*
1824df4628cbSrie 			 * LD_NOUNRESWEAK (internal, used by ldd(1)).
1825df4628cbSrie 			 */
1826df4628cbSrie 			select |= SEL_ACT_LML;
1827df4628cbSrie 			val = LML_FLG_TRC_NOUNRESWEAK;
1828df4628cbSrie 			variable = ENV_FLG_NOUNRESWEAK;
1829dae2dfb7Srie 		} else if ((len == MSG_LD_NOPAREXT_SIZE) && (strncmp(s1,
1830dae2dfb7Srie 		    MSG_ORIG(MSG_LD_NOPAREXT), MSG_LD_NOPAREXT_SIZE) == 0)) {
1831dae2dfb7Srie 			select |= SEL_ACT_LML;
1832dae2dfb7Srie 			val = LML_FLG_TRC_NOPAREXT;
1833dae2dfb7Srie 			variable = ENV_FLG_NOPAREXT;
18347c478bd9Sstevel@tonic-gate 		}
18357c478bd9Sstevel@tonic-gate 	}
18367c478bd9Sstevel@tonic-gate 	/*
18377c478bd9Sstevel@tonic-gate 	 * LD_ORIGIN.
18387c478bd9Sstevel@tonic-gate 	 */
18397c478bd9Sstevel@tonic-gate 	else if (*s1 == 'O') {
18407c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
18417c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_ORIGIN_SIZE) && (strncmp(s1,
18427c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_ORIGIN), MSG_LD_ORIGIN_SIZE) == 0)) {
18437c478bd9Sstevel@tonic-gate 			/*
18447c478bd9Sstevel@tonic-gate 			 * Besides some old patches this is no longer required.
18457c478bd9Sstevel@tonic-gate 			 */
18467c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_RELATIVE;
18477c478bd9Sstevel@tonic-gate 		}
18487c478bd9Sstevel@tonic-gate #endif
18497c478bd9Sstevel@tonic-gate 		return;
18507c478bd9Sstevel@tonic-gate 	}
18517c478bd9Sstevel@tonic-gate 	/*
18527c478bd9Sstevel@tonic-gate 	 * LD_PRELOAD and LD_PROFILE family.
18537c478bd9Sstevel@tonic-gate 	 */
18547c478bd9Sstevel@tonic-gate 	else if (*s1 == 'P') {
18557c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_PRELOAD_SIZE) && (strncmp(s1,
18567c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PRELOAD), MSG_LD_PRELOAD_SIZE) == 0)) {
18577c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1858fa034d7fSseizo 			if (select & SEL_REPLACE)
1859fa034d7fSseizo 				str = &rpl_preload;
1860fa034d7fSseizo 			else  {
1861fa034d7fSseizo 				str = &prm_preload;
1862fa034d7fSseizo 				rpl_preload = 0;
1863fa034d7fSseizo 			}
18647c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PRELOAD;
18657c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_SIZE) && (strncmp(s1,
18667c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE), MSG_LD_PROFILE_SIZE) == 0)) {
18677c478bd9Sstevel@tonic-gate 			/*
18687c478bd9Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
18697c478bd9Sstevel@tonic-gate 			 */
18707c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18717c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE;
18727c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_OUTPUT_SIZE) && (strncmp(s1,
18737c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE_OUTPUT),
18747c478bd9Sstevel@tonic-gate 		    MSG_LD_PROFILE_OUTPUT_SIZE) == 0)) {
18757c478bd9Sstevel@tonic-gate 			/*
18767c478bd9Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
18777c478bd9Sstevel@tonic-gate 			 */
18787c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
18797c478bd9Sstevel@tonic-gate 			str = &profile_out;
18807c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE_OUTPUT;
18817c478bd9Sstevel@tonic-gate 		}
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 	/*
18847c478bd9Sstevel@tonic-gate 	 * LD_SIGNAL.
18857c478bd9Sstevel@tonic-gate 	 */
18867c478bd9Sstevel@tonic-gate 	else if (*s1 == 'S') {
18877c478bd9Sstevel@tonic-gate 		if (rtld_flags & RT_FL_SECURE)
18887c478bd9Sstevel@tonic-gate 			return;
18897c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_SIGNAL_SIZE) &&
18907c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_SIGNAL),
18917c478bd9Sstevel@tonic-gate 		    MSG_LD_SIGNAL_SIZE) == 0)) {
18927c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
18937c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_SIGNAL;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 	}
18967c478bd9Sstevel@tonic-gate 	/*
18971d1fba8aSrie 	 * The LD_TRACE family (internal, used by ldd(1)).  This definition is
18981d1fba8aSrie 	 * the key to enabling all other ldd(1) specific environment variables.
18991d1fba8aSrie 	 * In case an auditor is called, which in turn might exec(2) a
19001d1fba8aSrie 	 * subprocess, this variable is disabled, so that any subprocess
19011d1fba8aSrie 	 * escapes ldd(1) processing.
19027c478bd9Sstevel@tonic-gate 	 */
19037c478bd9Sstevel@tonic-gate 	else if (*s1 == 'T') {
19047c478bd9Sstevel@tonic-gate 		if (((len == MSG_LD_TRACE_OBJS_SIZE) &&
19057c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS),
19067c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_SIZE) == 0)) ||
19077c478bd9Sstevel@tonic-gate 		    ((len == MSG_LD_TRACE_OBJS_E_SIZE) &&
19087c478bd9Sstevel@tonic-gate 		    (((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_E),
19097c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_E_SIZE) == 0) && !aout) ||
19107c478bd9Sstevel@tonic-gate 		    ((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_A),
19117c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_A_SIZE) == 0) && aout)))) {
19121d1fba8aSrie 			char	*s0 = (char *)s1;
19131d1fba8aSrie 
19147c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
19157c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_OBJS;
19161d1fba8aSrie 
191702ca3e02Srie #if	defined(__sparc) || defined(__x86)
19181d1fba8aSrie 			/*
19191d1fba8aSrie 			 * The simplest way to "disable" this variable is to
19201d1fba8aSrie 			 * truncate this string to "LD_'\0'". This string is
19211d1fba8aSrie 			 * ignored by any ld.so.1 environment processing.
19221d1fba8aSrie 			 * Use of such interfaces as unsetenv(3c) are overkill,
19231d1fba8aSrie 			 * and would drag too much libc implementation detail
19241d1fba8aSrie 			 * into ld.so.1.
19251d1fba8aSrie 			 */
1926a953e2b1Srie 			*s0 = '\0';
19271d1fba8aSrie #else
19281d1fba8aSrie /*
19291d1fba8aSrie  * Verify that the above write is appropriate for any new platforms.
19301d1fba8aSrie  */
19311d1fba8aSrie #error	unsupported architecture!
19321d1fba8aSrie #endif
19337c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_TRACE_PTHS_SIZE) && (strncmp(s1,
19347c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_TRACE_PTHS),
19357c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_PTHS_SIZE) == 0)) {
19367c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19377c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_SEARCH;
19387c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_PTHS;
19397c478bd9Sstevel@tonic-gate 		}
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 	/*
19427c478bd9Sstevel@tonic-gate 	 * LD_UNREF and LD_UNUSED (internal, used by ldd(1)).
19437c478bd9Sstevel@tonic-gate 	 */
19447c478bd9Sstevel@tonic-gate 	else if (*s1 == 'U') {
19457c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_UNREF_SIZE) && (strncmp(s1,
19467c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNREF), MSG_LD_UNREF_SIZE) == 0)) {
19477c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19487c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_UNREF;
19497c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_UNREF;
19507c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_UNUSED_SIZE) && (strncmp(s1,
19517c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNUSED), MSG_LD_UNUSED_SIZE) == 0)) {
19527c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19537c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_UNUSED;
19547c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_UNUSED;
19557c478bd9Sstevel@tonic-gate 		}
19567c478bd9Sstevel@tonic-gate 	}
19577c478bd9Sstevel@tonic-gate 	/*
19587c478bd9Sstevel@tonic-gate 	 * LD_VERBOSE (internal, used by ldd(1)).
19597c478bd9Sstevel@tonic-gate 	 */
19607c478bd9Sstevel@tonic-gate 	else if (*s1 == 'V') {
19617c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_VERBOSE_SIZE) && (strncmp(s1,
19627c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_VERBOSE), MSG_LD_VERBOSE_SIZE) == 0)) {
19637c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19647c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_VERBOSE;
19657c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_VERBOSE;
19667c478bd9Sstevel@tonic-gate 		}
19677c478bd9Sstevel@tonic-gate 	}
19687c478bd9Sstevel@tonic-gate 	/*
19697c478bd9Sstevel@tonic-gate 	 * LD_WARN (internal, used by ldd(1)).
19707c478bd9Sstevel@tonic-gate 	 */
19717c478bd9Sstevel@tonic-gate 	else if (*s1 == 'W') {
19727c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_WARN_SIZE) && (strncmp(s1,
19737c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_WARN), MSG_LD_WARN_SIZE) == 0)) {
19747c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
19757c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_WARN;
19767c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_WARN;
19777c478bd9Sstevel@tonic-gate 		}
19787c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
19797c478bd9Sstevel@tonic-gate 	}
19807c478bd9Sstevel@tonic-gate 	/*
19817c478bd9Sstevel@tonic-gate 	 * LD__FIX__ (undocumented, enable future technology that can't be
19827c478bd9Sstevel@tonic-gate 	 * delivered in a patch release).
19837c478bd9Sstevel@tonic-gate 	 */
19847c478bd9Sstevel@tonic-gate 	else if (*s1 == '_') {
19857c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_FIX_1_SIZE) && (strncmp(s1,
19867c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FIX_1), MSG_LD_FIX_1_SIZE) == 0)) {
19877c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
19887c478bd9Sstevel@tonic-gate 			val = RT_FL_DISFIX_1;
19897c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_FIX_1;
19907c478bd9Sstevel@tonic-gate 		}
19917c478bd9Sstevel@tonic-gate #endif
19927c478bd9Sstevel@tonic-gate 	}
19937c478bd9Sstevel@tonic-gate 	if (variable == 0)
19947c478bd9Sstevel@tonic-gate 		return;
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate 	/*
19977c478bd9Sstevel@tonic-gate 	 * If the variable is already processed with ISA specific variable,
19987c478bd9Sstevel@tonic-gate 	 * no further processing needed.
19997c478bd9Sstevel@tonic-gate 	 */
20007c478bd9Sstevel@tonic-gate 	if (((select & SEL_REPLACE) && (rplisa & variable)) ||
20017c478bd9Sstevel@tonic-gate 	    ((select & SEL_PERMANT) && (prmisa & variable)))
20027c478bd9Sstevel@tonic-gate 		return;
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate 	/*
2005fa034d7fSseizo 	 * Now mark the appropriate variables.
2006fa034d7fSseizo 	 * If the replaceable variable is already set, then the
2007fa034d7fSseizo 	 * process environment variable must be set. Any replaceable
2008fa034d7fSseizo 	 * variable specified in a configuration file can be ignored.
20097c478bd9Sstevel@tonic-gate 	 */
20107c478bd9Sstevel@tonic-gate 	if (env_flags & ENV_TYP_ISA) {
20117c478bd9Sstevel@tonic-gate 		/*
20127c478bd9Sstevel@tonic-gate 		 * This is ISA setting. We do the setting
20137c478bd9Sstevel@tonic-gate 		 * even if s2 is NULL.
20147c478bd9Sstevel@tonic-gate 		 * If s2 is NULL, we might need to undo
20157c478bd9Sstevel@tonic-gate 		 * the setting.
20167c478bd9Sstevel@tonic-gate 		 */
20177c478bd9Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
2018fa034d7fSseizo 			if (rplisa & variable)
2019fa034d7fSseizo 				return;
20207c478bd9Sstevel@tonic-gate 			rplisa |= variable;
20217c478bd9Sstevel@tonic-gate 		} else {
20227c478bd9Sstevel@tonic-gate 			prmisa |= variable;
20237c478bd9Sstevel@tonic-gate 		}
20247c478bd9Sstevel@tonic-gate 	} else if (s2) {
20257c478bd9Sstevel@tonic-gate 		/*
20267c478bd9Sstevel@tonic-gate 		 * This is non0-ISA setting
20277c478bd9Sstevel@tonic-gate 		 */
20287c478bd9Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
2029fa034d7fSseizo 			if (rplgen & variable)
2030fa034d7fSseizo 				return;
20317c478bd9Sstevel@tonic-gate 			rplgen |= variable;
20327c478bd9Sstevel@tonic-gate 		} else
20337c478bd9Sstevel@tonic-gate 			prmgen |= variable;
20347c478bd9Sstevel@tonic-gate 	} else
20357c478bd9Sstevel@tonic-gate 		/*
20367c478bd9Sstevel@tonic-gate 		 * This is non-ISA setting which
20377c478bd9Sstevel@tonic-gate 		 * can be ignored.
20387c478bd9Sstevel@tonic-gate 		 */
20397c478bd9Sstevel@tonic-gate 		return;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 	/*
20427c478bd9Sstevel@tonic-gate 	 * Now perform the setting.
20437c478bd9Sstevel@tonic-gate 	 */
20447c478bd9Sstevel@tonic-gate 	if (select & SEL_ACT_RT) {
20457c478bd9Sstevel@tonic-gate 		if (s2)
20467c478bd9Sstevel@tonic-gate 			rtld_flags |= val;
20477c478bd9Sstevel@tonic-gate 		else
20487c478bd9Sstevel@tonic-gate 			rtld_flags &= ~val;
20497c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_RT2) {
20507c478bd9Sstevel@tonic-gate 		if (s2)
20517c478bd9Sstevel@tonic-gate 			rtld_flags2 |= val;
20527c478bd9Sstevel@tonic-gate 		else
20537c478bd9Sstevel@tonic-gate 			rtld_flags2 &= ~val;
20547c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_STR)
20557c478bd9Sstevel@tonic-gate 		*str = s2;
20567c478bd9Sstevel@tonic-gate 	else if (select & SEL_ACT_LML) {
20577c478bd9Sstevel@tonic-gate 		if (s2)
20587c478bd9Sstevel@tonic-gate 			*lmflags |= val;
20597c478bd9Sstevel@tonic-gate 		else
20607c478bd9Sstevel@tonic-gate 			*lmflags &= ~val;
20617c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_LMLT) {
20627c478bd9Sstevel@tonic-gate 		if (s2)
20637c478bd9Sstevel@tonic-gate 			*lmtflags |= val;
20647c478bd9Sstevel@tonic-gate 		else
20657c478bd9Sstevel@tonic-gate 			*lmtflags &= ~val;
20667c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_1) {
20677c478bd9Sstevel@tonic-gate 		/*
20687c478bd9Sstevel@tonic-gate 		 * variable is either ENV_FLG_FLAGS or ENV_FLG_LIBPATH
20697c478bd9Sstevel@tonic-gate 		 */
20707c478bd9Sstevel@tonic-gate 		*str = s2;
20717c478bd9Sstevel@tonic-gate 		if ((select & SEL_REPLACE) && (env_flags & ENV_TYP_CONFIG)) {
20727c478bd9Sstevel@tonic-gate 			if (s2) {
20737c478bd9Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
20747c478bd9Sstevel@tonic-gate 					env_info |= ENV_INF_FLAGCFG;
20757c478bd9Sstevel@tonic-gate 				else
20767c478bd9Sstevel@tonic-gate 					env_info |= ENV_INF_PATHCFG;
20777c478bd9Sstevel@tonic-gate 			} else {
20787c478bd9Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
20797c478bd9Sstevel@tonic-gate 					env_info &= ~ENV_INF_FLAGCFG;
20807c478bd9Sstevel@tonic-gate 				else
20817c478bd9Sstevel@tonic-gate 					env_info &= ~ENV_INF_PATHCFG;
20827c478bd9Sstevel@tonic-gate 			}
20837c478bd9Sstevel@tonic-gate 		}
20847c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_2) {
20857c478bd9Sstevel@tonic-gate 		/*
20867c478bd9Sstevel@tonic-gate 		 * variables can be: ENV_FLG_
20877c478bd9Sstevel@tonic-gate 		 * 	AUDIT_ARGS, BINDING, CONCURRENCY, CONFGEN,
20887c478bd9Sstevel@tonic-gate 		 *	LOADFLTR, PROFILE, SIGNAL, TRACE_OBJS
20897c478bd9Sstevel@tonic-gate 		 */
20907c478bd9Sstevel@tonic-gate 		if (variable == ENV_FLG_AUDIT_ARGS) {
20917c478bd9Sstevel@tonic-gate 			if (s2) {
20927c478bd9Sstevel@tonic-gate 				audit_argcnt = atoi(s2);
20937c478bd9Sstevel@tonic-gate 				audit_argcnt += audit_argcnt % 2;
20947c478bd9Sstevel@tonic-gate 			} else
20957c478bd9Sstevel@tonic-gate 				audit_argcnt = 0;
20967c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_BINDINGS) {
20977c478bd9Sstevel@tonic-gate 			if (s2)
20987c478bd9Sstevel@tonic-gate 				rpl_debug = MSG_ORIG(MSG_TKN_BINDINGS);
20997c478bd9Sstevel@tonic-gate 			else
21007c478bd9Sstevel@tonic-gate 				rpl_debug = 0;
21017c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_CONCURRENCY) {
21027c478bd9Sstevel@tonic-gate 			if (s2)
21037c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_NOCONCUR;
21047c478bd9Sstevel@tonic-gate 			else
21057c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_NOCONCUR;
21067c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_CONFGEN) {
21077c478bd9Sstevel@tonic-gate 			if (s2) {
21087c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_CONFGEN;
21097c478bd9Sstevel@tonic-gate 				*lmflags |= LML_FLG_IGNRELERR;
21107c478bd9Sstevel@tonic-gate 			} else {
21117c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_CONFGEN;
21127c478bd9Sstevel@tonic-gate 				*lmflags &= ~LML_FLG_IGNRELERR;
21137c478bd9Sstevel@tonic-gate 			}
21147c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_LOADFLTR) {
21157c478bd9Sstevel@tonic-gate 			if (s2) {
21167c478bd9Sstevel@tonic-gate 				*lmtflags |= LML_TFLG_LOADFLTR;
21177c478bd9Sstevel@tonic-gate 				if (*s2 == '2')
21187c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_WARNFLTR;
21197c478bd9Sstevel@tonic-gate 			} else {
21207c478bd9Sstevel@tonic-gate 				*lmtflags &= ~LML_TFLG_LOADFLTR;
21217c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_WARNFLTR;
21227c478bd9Sstevel@tonic-gate 			}
21237c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_PROFILE) {
21247c478bd9Sstevel@tonic-gate 			profile_name = s2;
21257c478bd9Sstevel@tonic-gate 			if (s2) {
21267c478bd9Sstevel@tonic-gate 				if (strcmp(s2, MSG_ORIG(MSG_FIL_RTLD)) == 0) {
21277c478bd9Sstevel@tonic-gate 					return;
21287c478bd9Sstevel@tonic-gate 				}
2129a953e2b1Srie 				/* BEGIN CSTYLED */
21307c478bd9Sstevel@tonic-gate 				if (rtld_flags & RT_FL_SECURE) {
21317c478bd9Sstevel@tonic-gate 					profile_lib =
21327c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
21337c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE_64);
21347c478bd9Sstevel@tonic-gate #else
21357c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE);
21367c478bd9Sstevel@tonic-gate #endif
21377c478bd9Sstevel@tonic-gate 				} else {
21387c478bd9Sstevel@tonic-gate 					profile_lib =
21397c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
21407c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF_64);
21417c478bd9Sstevel@tonic-gate #else
21427c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF);
21437c478bd9Sstevel@tonic-gate #endif
21447c478bd9Sstevel@tonic-gate 				}
2145a953e2b1Srie 				/* END CSTYLED */
21467c478bd9Sstevel@tonic-gate 			} else
21477c478bd9Sstevel@tonic-gate 				profile_lib = 0;
21487c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_SIGNAL) {
21497c478bd9Sstevel@tonic-gate 			killsig = s2 ? atoi(s2) : SIGKILL;
21507c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_TRACE_OBJS) {
21517c478bd9Sstevel@tonic-gate 			if (s2) {
21527c478bd9Sstevel@tonic-gate 				*lmflags |= LML_FLG_TRC_ENABLE;
21537c478bd9Sstevel@tonic-gate 				if (*s2 == '2')
21547c478bd9Sstevel@tonic-gate 					*lmflags |= LML_FLG_TRC_LDDSTUB;
21557c478bd9Sstevel@tonic-gate 			} else
21567c478bd9Sstevel@tonic-gate 				*lmflags &=
2157a953e2b1Srie 				    ~(LML_FLG_TRC_ENABLE|LML_FLG_TRC_LDDSTUB);
21587c478bd9Sstevel@tonic-gate 		}
21597c478bd9Sstevel@tonic-gate 	}
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate 
21627c478bd9Sstevel@tonic-gate /*
21637c478bd9Sstevel@tonic-gate  * Determine whether we have an architecture specific environment variable.
21647c478bd9Sstevel@tonic-gate  * If we do, and we're the wrong architecture, it'll just get ignored.
21657c478bd9Sstevel@tonic-gate  * Otherwise the variable is processed in it's architecture neutral form.
21667c478bd9Sstevel@tonic-gate  */
21677c478bd9Sstevel@tonic-gate static int
21687c478bd9Sstevel@tonic-gate ld_arch_env(const char *s1, size_t *len)
21697c478bd9Sstevel@tonic-gate {
21707c478bd9Sstevel@tonic-gate 	size_t	_len = *len - 3;
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	if (s1[_len++] == '_') {
21737c478bd9Sstevel@tonic-gate 		if ((s1[_len] == '3') && (s1[_len + 1] == '2')) {
21747c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
21757c478bd9Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
21767c478bd9Sstevel@tonic-gate #else
21777c478bd9Sstevel@tonic-gate 			*len = *len - 3;
21787c478bd9Sstevel@tonic-gate 			return (ENV_TYP_ISA);
21797c478bd9Sstevel@tonic-gate #endif
21807c478bd9Sstevel@tonic-gate 		}
21817c478bd9Sstevel@tonic-gate 		if ((s1[_len] == '6') && (s1[_len + 1] == '4')) {
21827c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
21837c478bd9Sstevel@tonic-gate 			*len = *len - 3;
21847c478bd9Sstevel@tonic-gate 			return (ENV_TYP_ISA);
21857c478bd9Sstevel@tonic-gate #else
21867c478bd9Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
21877c478bd9Sstevel@tonic-gate #endif
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 	}
21907c478bd9Sstevel@tonic-gate 	return (0);
21917c478bd9Sstevel@tonic-gate }
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate /*
21957c478bd9Sstevel@tonic-gate  * Process an LD_FLAGS environment variable.  The value can be a comma
21967c478bd9Sstevel@tonic-gate  * separated set of tokens, which are sent (in upper case) into the generic
21977c478bd9Sstevel@tonic-gate  * LD_XXXX environment variable engine.  For example:
21987c478bd9Sstevel@tonic-gate  *
21997c478bd9Sstevel@tonic-gate  *	LD_FLAGS=bind_now		->	LD_BIND_NOW=1
22007c478bd9Sstevel@tonic-gate  *	LD_FLAGS=library_path=/foo:.	->	LD_LIBRARY_PATH=/foo:.
22017c478bd9Sstevel@tonic-gate  *	LD_FLAGS=debug=files:detail	->	LD_DEBUG=files:detail
22027c478bd9Sstevel@tonic-gate  * or
22037c478bd9Sstevel@tonic-gate  *	LD_FLAGS=bind_now,library_path=/foo:.,debug=files:detail
22047c478bd9Sstevel@tonic-gate  */
22057c478bd9Sstevel@tonic-gate static int
22067c478bd9Sstevel@tonic-gate ld_flags_env(const char *str, Word *lmflags, Word *lmtflags,
22077c478bd9Sstevel@tonic-gate     uint_t env_flags, int aout)
22087c478bd9Sstevel@tonic-gate {
2209*3dbfc803SRod Evans 	char	*nstr, *sstr, *estr = NULL;
22107c478bd9Sstevel@tonic-gate 	size_t	nlen, len;
22117c478bd9Sstevel@tonic-gate 
2212*3dbfc803SRod Evans 	if (str == NULL)
22137c478bd9Sstevel@tonic-gate 		return (0);
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	/*
22167c478bd9Sstevel@tonic-gate 	 * Create a new string as we're going to transform the token(s) into
22177c478bd9Sstevel@tonic-gate 	 * uppercase and separate tokens with nulls.
22187c478bd9Sstevel@tonic-gate 	 */
22197c478bd9Sstevel@tonic-gate 	len = strlen(str);
2220*3dbfc803SRod Evans 	if ((nstr = malloc(len + 1)) == NULL)
22217c478bd9Sstevel@tonic-gate 		return (1);
22227c478bd9Sstevel@tonic-gate 	(void) strcpy(nstr, str);
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	for (sstr = nstr; sstr; sstr++, len--) {
22257c478bd9Sstevel@tonic-gate 		int	flags;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 		if ((*sstr != '\0') && (*sstr != ',')) {
2228*3dbfc803SRod Evans 			if (estr == NULL) {
22297c478bd9Sstevel@tonic-gate 				if (*sstr == '=')
22307c478bd9Sstevel@tonic-gate 					estr = sstr;
22317c478bd9Sstevel@tonic-gate 				else {
22327c478bd9Sstevel@tonic-gate 					/*
22337c478bd9Sstevel@tonic-gate 					 * Translate token to uppercase.  Don't
22347c478bd9Sstevel@tonic-gate 					 * use toupper(3C) as including this
22357c478bd9Sstevel@tonic-gate 					 * code doubles the size of ld.so.1.
22367c478bd9Sstevel@tonic-gate 					 */
22377c478bd9Sstevel@tonic-gate 					if ((*sstr >= 'a') && (*sstr <= 'z'))
22387c478bd9Sstevel@tonic-gate 						*sstr = *sstr - ('a' - 'A');
22397c478bd9Sstevel@tonic-gate 				}
22407c478bd9Sstevel@tonic-gate 			}
22417c478bd9Sstevel@tonic-gate 			continue;
22427c478bd9Sstevel@tonic-gate 		}
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate 		*sstr = '\0';
22457c478bd9Sstevel@tonic-gate 		if (estr) {
22467c478bd9Sstevel@tonic-gate 			nlen = estr - nstr;
22477c478bd9Sstevel@tonic-gate 			if ((*++estr == '\0') || (*estr == ','))
22487c478bd9Sstevel@tonic-gate 				estr = 0;
22497c478bd9Sstevel@tonic-gate 		} else
22507c478bd9Sstevel@tonic-gate 			nlen = sstr - nstr;
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 		/*
22537c478bd9Sstevel@tonic-gate 		 * Fabricate a boolean definition for any unqualified variable.
22547c478bd9Sstevel@tonic-gate 		 * Thus LD_FLAGS=bind_now is represented as BIND_NOW=(null).
22557c478bd9Sstevel@tonic-gate 		 * The value is sufficient to assert any boolean variables, plus
22567c478bd9Sstevel@tonic-gate 		 * the term "(null)" is specifically chosen in case someone
22577c478bd9Sstevel@tonic-gate 		 * mistakenly supplies something like LD_FLAGS=library_path.
22587c478bd9Sstevel@tonic-gate 		 */
2259*3dbfc803SRod Evans 		if (estr == NULL)
22607c478bd9Sstevel@tonic-gate 			estr = (char *)MSG_INTL(MSG_STR_NULL);
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 		/*
22637c478bd9Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
22647c478bd9Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
22657c478bd9Sstevel@tonic-gate 		 * neutral portion of the string.
22667c478bd9Sstevel@tonic-gate 		 */
22677c478bd9Sstevel@tonic-gate 		if ((flags = ld_arch_env(nstr, &nlen)) != ENV_TYP_IGNORE) {
22687c478bd9Sstevel@tonic-gate 			ld_generic_env(nstr, nlen, estr, lmflags,
22697c478bd9Sstevel@tonic-gate 			    lmtflags, (env_flags | flags), aout);
22707c478bd9Sstevel@tonic-gate 		}
22717c478bd9Sstevel@tonic-gate 		if (len == 0)
22727c478bd9Sstevel@tonic-gate 			return (0);
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 		nstr = sstr + 1;
22757c478bd9Sstevel@tonic-gate 		estr = 0;
22767c478bd9Sstevel@tonic-gate 	}
22777c478bd9Sstevel@tonic-gate 	return (0);
22787c478bd9Sstevel@tonic-gate }
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate /*
22827c478bd9Sstevel@tonic-gate  * Process a single environment string.  Only strings starting with `LD_' are
22837c478bd9Sstevel@tonic-gate  * reserved for our use.  By convention, all strings should be of the form
22847c478bd9Sstevel@tonic-gate  * `LD_XXXX=', if the string is followed by a non-null value the appropriate
22857c478bd9Sstevel@tonic-gate  * functionality is enabled.  Also pick off applicable locale variables.
22867c478bd9Sstevel@tonic-gate  */
22877c478bd9Sstevel@tonic-gate #define	LOC_LANG	1
22887c478bd9Sstevel@tonic-gate #define	LOC_MESG	2
22897c478bd9Sstevel@tonic-gate #define	LOC_ALL		3
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate static void
22927c478bd9Sstevel@tonic-gate ld_str_env(const char *s1, Word *lmflags, Word *lmtflags, uint_t env_flags,
22937c478bd9Sstevel@tonic-gate     int aout)
22947c478bd9Sstevel@tonic-gate {
22957c478bd9Sstevel@tonic-gate 	const char	*s2;
229610a4fa49Srie 	static		size_t	loc = 0;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	if (*s1++ != 'L')
22997c478bd9Sstevel@tonic-gate 		return;
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate 	/*
23027c478bd9Sstevel@tonic-gate 	 * See if we have any locale environment settings.  These environment
23037c478bd9Sstevel@tonic-gate 	 * variables have a precedence, LC_ALL is higher than LC_MESSAGES which
23047c478bd9Sstevel@tonic-gate 	 * is higher than LANG.
23057c478bd9Sstevel@tonic-gate 	 */
23067c478bd9Sstevel@tonic-gate 	s2 = s1;
23077c478bd9Sstevel@tonic-gate 	if ((*s2++ == 'C') && (*s2++ == '_') && (*s2 != '\0')) {
23087c478bd9Sstevel@tonic-gate 		if (strncmp(s2, MSG_ORIG(MSG_LC_ALL), MSG_LC_ALL_SIZE) == 0) {
23097c478bd9Sstevel@tonic-gate 			s2 += MSG_LC_ALL_SIZE;
23107c478bd9Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_ALL)) {
231110a4fa49Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
23127c478bd9Sstevel@tonic-gate 				loc = LOC_ALL;
23137c478bd9Sstevel@tonic-gate 			}
23147c478bd9Sstevel@tonic-gate 		} else if (strncmp(s2, MSG_ORIG(MSG_LC_MESSAGES),
23157c478bd9Sstevel@tonic-gate 		    MSG_LC_MESSAGES_SIZE) == 0) {
23167c478bd9Sstevel@tonic-gate 			s2 += MSG_LC_MESSAGES_SIZE;
23177c478bd9Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_MESG)) {
231810a4fa49Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
23197c478bd9Sstevel@tonic-gate 				loc = LOC_MESG;
23207c478bd9Sstevel@tonic-gate 			}
23217c478bd9Sstevel@tonic-gate 		}
23227c478bd9Sstevel@tonic-gate 		return;
23237c478bd9Sstevel@tonic-gate 	}
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 	s2 = s1;
23267c478bd9Sstevel@tonic-gate 	if ((*s2++ == 'A') && (*s2++ == 'N') && (*s2++ == 'G') &&
23277c478bd9Sstevel@tonic-gate 	    (*s2++ == '=') && (*s2 != '\0') && (loc < LOC_LANG)) {
232810a4fa49Srie 		glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
23297c478bd9Sstevel@tonic-gate 		loc = LOC_LANG;
23307c478bd9Sstevel@tonic-gate 		return;
23317c478bd9Sstevel@tonic-gate 	}
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate 	/*
23347c478bd9Sstevel@tonic-gate 	 * Pick off any LD_XXXX environment variables.
23357c478bd9Sstevel@tonic-gate 	 */
23367c478bd9Sstevel@tonic-gate 	if ((*s1++ == 'D') && (*s1++ == '_') && (*s1 != '\0')) {
23377c478bd9Sstevel@tonic-gate 		size_t	len;
23387c478bd9Sstevel@tonic-gate 		int	flags;
23397c478bd9Sstevel@tonic-gate 
23409acbbeafSnn 		/*
23419acbbeafSnn 		 * In a branded process we must ignore all LD_XXXX env vars
23429acbbeafSnn 		 * because they are intended for the brand's linker.
23439acbbeafSnn 		 * To affect the Solaris linker, use LD_BRAND_XXXX instead.
23449acbbeafSnn 		 */
23459acbbeafSnn 		if (rtld_flags2 & RT_FL2_BRANDED) {
23469acbbeafSnn 			if (strncmp(s1, MSG_ORIG(MSG_LD_BRAND_PREFIX),
23479acbbeafSnn 			    MSG_LD_BRAND_PREFIX_SIZE) != 0)
23489acbbeafSnn 				return;
23499acbbeafSnn 			s1 += MSG_LD_BRAND_PREFIX_SIZE;
23509acbbeafSnn 		}
23519acbbeafSnn 
23527c478bd9Sstevel@tonic-gate 		/*
23537c478bd9Sstevel@tonic-gate 		 * Environment variables with no value (ie. LD_XXXX=) typically
23547c478bd9Sstevel@tonic-gate 		 * have no impact, however if environment variables are defined
23557c478bd9Sstevel@tonic-gate 		 * within a configuration file, these null user settings can be
23567c478bd9Sstevel@tonic-gate 		 * used to disable any configuration replaceable definitions.
23577c478bd9Sstevel@tonic-gate 		 */
2358*3dbfc803SRod Evans 		if ((s2 = strchr(s1, '=')) == NULL) {
23597c478bd9Sstevel@tonic-gate 			len = strlen(s1);
23607c478bd9Sstevel@tonic-gate 			s2 = 0;
23617c478bd9Sstevel@tonic-gate 		} else if (*++s2 == '\0') {
23627c478bd9Sstevel@tonic-gate 			len = strlen(s1) - 1;
23637c478bd9Sstevel@tonic-gate 			s2 = 0;
23647c478bd9Sstevel@tonic-gate 		} else {
23657c478bd9Sstevel@tonic-gate 			len = s2 - s1 - 1;
23667c478bd9Sstevel@tonic-gate 			while (isspace(*s2))
23677c478bd9Sstevel@tonic-gate 				s2++;
23687c478bd9Sstevel@tonic-gate 		}
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 		/*
23717c478bd9Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
23727c478bd9Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
23737c478bd9Sstevel@tonic-gate 		 * neutral portion of the string.
23747c478bd9Sstevel@tonic-gate 		 */
23757c478bd9Sstevel@tonic-gate 		if ((flags = ld_arch_env(s1, &len)) == ENV_TYP_IGNORE)
23767c478bd9Sstevel@tonic-gate 			return;
23777c478bd9Sstevel@tonic-gate 		env_flags |= flags;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 		ld_generic_env(s1, len, s2, lmflags, lmtflags, env_flags, aout);
23807c478bd9Sstevel@tonic-gate 	}
23817c478bd9Sstevel@tonic-gate }
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate /*
23847c478bd9Sstevel@tonic-gate  * Internal getenv routine.  Called immediately after ld.so.1 initializes
23857c478bd9Sstevel@tonic-gate  * itself.
23867c478bd9Sstevel@tonic-gate  */
23877c478bd9Sstevel@tonic-gate int
23887c478bd9Sstevel@tonic-gate readenv_user(const char ** envp, Word *lmflags, Word *lmtflags, int aout)
23897c478bd9Sstevel@tonic-gate {
239010a4fa49Srie 	char	*locale;
239110a4fa49Srie 
23927c478bd9Sstevel@tonic-gate 	if (envp == (const char **)0)
23937c478bd9Sstevel@tonic-gate 		return (0);
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	while (*envp != (const char *)0)
23967c478bd9Sstevel@tonic-gate 		ld_str_env(*envp++, lmflags, lmtflags, 0, aout);
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 	/*
23997c478bd9Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
24007c478bd9Sstevel@tonic-gate 	 * these strings.
24017c478bd9Sstevel@tonic-gate 	 */
24027c478bd9Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
24037c478bd9Sstevel@tonic-gate 		return (1);
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	/*
24067c478bd9Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
24077c478bd9Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
24087c478bd9Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
24097c478bd9Sstevel@tonic-gate 	 */
24107c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
24117c478bd9Sstevel@tonic-gate 		rpl_audit = profile_lib = profile_name = 0;
24127c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
24137c478bd9Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
24147c478bd9Sstevel@tonic-gate 
2415dffec89cSrie 	/*
2416dffec89cSrie 	 * If both LD_BIND_NOW and LD_BIND_LAZY are specified, the former wins.
2417dffec89cSrie 	 */
2418dffec89cSrie 	if ((rtld_flags2 & (RT_FL2_BINDNOW | RT_FL2_BINDLAZY)) ==
2419dffec89cSrie 	    (RT_FL2_BINDNOW | RT_FL2_BINDLAZY))
2420dffec89cSrie 		rtld_flags2 &= ~RT_FL2_BINDLAZY;
2421dffec89cSrie 
2422dae2dfb7Srie 	/*
2423dae2dfb7Srie 	 * When using ldd(1) -r or -d against an executable, assert -p.
2424dae2dfb7Srie 	 */
2425dae2dfb7Srie 	if ((*lmflags &
2426dae2dfb7Srie 	    (LML_FLG_TRC_WARN | LML_FLG_TRC_LDDSTUB)) == LML_FLG_TRC_WARN)
2427dae2dfb7Srie 		*lmflags |= LML_FLG_TRC_NOPAREXT;
2428dae2dfb7Srie 
24297c478bd9Sstevel@tonic-gate 	/*
24307c478bd9Sstevel@tonic-gate 	 * If we have a locale setting make sure its worth processing further.
24311d6b7ad8Srie 	 * C and POSIX locales don't need any processing.  In addition, to
24321d6b7ad8Srie 	 * ensure no one escapes the /usr/lib/locale hierarchy, don't allow
24331d6b7ad8Srie 	 * the locale to contain a segment that leads upward in the file system
24341d6b7ad8Srie 	 * hierarchy (i.e. no '..' segments).   Given that we'll be confined to
24351d6b7ad8Srie 	 * the /usr/lib/locale hierarchy, there is no need to extensively
24361d6b7ad8Srie 	 * validate the mode or ownership of any message file (as libc's
24371d6b7ad8Srie 	 * generic handling of message files does).  Duplicate the string so
24381d6b7ad8Srie 	 * that new locale setting can generically cleanup any previous locales.
24397c478bd9Sstevel@tonic-gate 	 */
244010a4fa49Srie 	if ((locale = glcs[CI_LCMESSAGES].lc_un.lc_ptr) != 0) {
24417c478bd9Sstevel@tonic-gate 		if (((*locale == 'C') && (*(locale + 1) == '\0')) ||
24421d6b7ad8Srie 		    (strcmp(locale, MSG_ORIG(MSG_TKN_POSIX)) == 0) ||
24431d6b7ad8Srie 		    (strstr(locale, MSG_ORIG(MSG_TKN_DOTDOT)) != NULL))
244410a4fa49Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = 0;
24457c478bd9Sstevel@tonic-gate 		else
244610a4fa49Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = strdup(locale);
24477c478bd9Sstevel@tonic-gate 	}
24487c478bd9Sstevel@tonic-gate 	return (0);
24497c478bd9Sstevel@tonic-gate }
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate /*
24527c478bd9Sstevel@tonic-gate  * Configuration environment processing.  Called after the a.out has been
24537c478bd9Sstevel@tonic-gate  * processed (as the a.out can specify its own configuration file).
24547c478bd9Sstevel@tonic-gate  */
24557c478bd9Sstevel@tonic-gate int
24567c478bd9Sstevel@tonic-gate readenv_config(Rtc_env * envtbl, Addr addr, int aout)
24577c478bd9Sstevel@tonic-gate {
24589aa23310Srie 	Word	*lmflags = &(lml_main.lm_flags);
24599aa23310Srie 	Word	*lmtflags = &(lml_main.lm_tflags);
24607c478bd9Sstevel@tonic-gate 
24617c478bd9Sstevel@tonic-gate 	if (envtbl == (Rtc_env *)0)
24627c478bd9Sstevel@tonic-gate 		return (0);
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	while (envtbl->env_str) {
24657c478bd9Sstevel@tonic-gate 		uint_t	env_flags = ENV_TYP_CONFIG;
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 		if (envtbl->env_flags & RTC_ENV_PERMANT)
24687c478bd9Sstevel@tonic-gate 			env_flags |= ENV_TYP_PERMANT;
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 		ld_str_env((const char *)(envtbl->env_str + addr),
24717c478bd9Sstevel@tonic-gate 		    lmflags, lmtflags, env_flags, 0);
24727c478bd9Sstevel@tonic-gate 		envtbl++;
24737c478bd9Sstevel@tonic-gate 	}
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 	/*
24767c478bd9Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
24777c478bd9Sstevel@tonic-gate 	 * these strings.
24787c478bd9Sstevel@tonic-gate 	 */
24797c478bd9Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
24807c478bd9Sstevel@tonic-gate 		return (1);
24817c478bd9Sstevel@tonic-gate 	if (ld_flags_env(prm_ldflags, lmflags, lmtflags, ENV_TYP_CONFIG,
24827c478bd9Sstevel@tonic-gate 	    aout) == 1)
24837c478bd9Sstevel@tonic-gate 		return (1);
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 	/*
24867c478bd9Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
24877c478bd9Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
24887c478bd9Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
24897c478bd9Sstevel@tonic-gate 	 */
24907c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
24917c478bd9Sstevel@tonic-gate 		prm_audit = profile_lib = profile_name = 0;
24927c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
24937c478bd9Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
24947c478bd9Sstevel@tonic-gate 
24957c478bd9Sstevel@tonic-gate 	return (0);
24967c478bd9Sstevel@tonic-gate }
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate int
24997c478bd9Sstevel@tonic-gate dowrite(Prfbuf * prf)
25007c478bd9Sstevel@tonic-gate {
25017c478bd9Sstevel@tonic-gate 	/*
25027c478bd9Sstevel@tonic-gate 	 * We do not have a valid file descriptor, so we are unable
25037c478bd9Sstevel@tonic-gate 	 * to flush the buffer.
25047c478bd9Sstevel@tonic-gate 	 */
25057c478bd9Sstevel@tonic-gate 	if (prf->pr_fd == -1)
25067c478bd9Sstevel@tonic-gate 		return (0);
25077c478bd9Sstevel@tonic-gate 	(void) write(prf->pr_fd, prf->pr_buf, prf->pr_cur - prf->pr_buf);
25087c478bd9Sstevel@tonic-gate 	prf->pr_cur = prf->pr_buf;
25097c478bd9Sstevel@tonic-gate 	return (1);
25107c478bd9Sstevel@tonic-gate }
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate /*
25137c478bd9Sstevel@tonic-gate  * Simplified printing.  The following conversion specifications are supported:
25147c478bd9Sstevel@tonic-gate  *
25157c478bd9Sstevel@tonic-gate  *	% [#] [-] [min field width] [. precision] s|d|x|c
25167c478bd9Sstevel@tonic-gate  *
25177c478bd9Sstevel@tonic-gate  *
25187c478bd9Sstevel@tonic-gate  * dorprf takes the output buffer in the form of Prfbuf which permits
25197c478bd9Sstevel@tonic-gate  * the verification of the output buffer size and the concatenation
25207c478bd9Sstevel@tonic-gate  * of data to an already existing output buffer.  The Prfbuf
25217c478bd9Sstevel@tonic-gate  * structure contains the following:
25227c478bd9Sstevel@tonic-gate  *
25237c478bd9Sstevel@tonic-gate  *  pr_buf	pointer to the beginning of the output buffer.
25247c478bd9Sstevel@tonic-gate  *  pr_cur	pointer to the next available byte in the output buffer.  By
25257c478bd9Sstevel@tonic-gate  *		setting pr_cur ahead of pr_buf you can append to an already
25267c478bd9Sstevel@tonic-gate  *		existing buffer.
25277c478bd9Sstevel@tonic-gate  *  pr_len	the size of the output buffer.  By setting pr_len to '0' you
25287c478bd9Sstevel@tonic-gate  *		disable protection from overflows in the output buffer.
25297c478bd9Sstevel@tonic-gate  *  pr_fd	a pointer to the file-descriptor the buffer will eventually be
25307c478bd9Sstevel@tonic-gate  *		output to.  If pr_fd is set to '-1' then it's assumed there is
25311d6b7ad8Srie  *		no output buffer, and doprf() will return with an error to
25321d6b7ad8Srie  *		indicate an output buffer overflow.  If pr_fd is > -1 then when
25331d6b7ad8Srie  *		the output buffer is filled it will be flushed to pr_fd and will
25341d6b7ad8Srie  *		then be	available for additional data.
25357c478bd9Sstevel@tonic-gate  */
25367c478bd9Sstevel@tonic-gate #define	FLG_UT_MINUS	0x0001	/* - */
25377c478bd9Sstevel@tonic-gate #define	FLG_UT_SHARP	0x0002	/* # */
25387c478bd9Sstevel@tonic-gate #define	FLG_UT_DOTSEEN	0x0008	/* dot appeared in format spec */
25397c478bd9Sstevel@tonic-gate 
25407c478bd9Sstevel@tonic-gate /*
254110a4fa49Srie  * This macro is for use from within doprf only.  It is to be used for checking
254210a4fa49Srie  * the output buffer size and placing characters into the buffer.
25437c478bd9Sstevel@tonic-gate  */
25447c478bd9Sstevel@tonic-gate #define	PUTC(c) \
25457c478bd9Sstevel@tonic-gate 	{ \
254610a4fa49Srie 		char tmpc; \
25477c478bd9Sstevel@tonic-gate 		\
25487c478bd9Sstevel@tonic-gate 		tmpc = (c); \
254910a4fa49Srie 		if (bufsiz && (bp >= bufend)) { \
25507c478bd9Sstevel@tonic-gate 			prf->pr_cur = bp; \
25517c478bd9Sstevel@tonic-gate 			if (dowrite(prf) == 0) \
25527c478bd9Sstevel@tonic-gate 				return (0); \
25537c478bd9Sstevel@tonic-gate 			bp = prf->pr_cur; \
25547c478bd9Sstevel@tonic-gate 		} \
25557c478bd9Sstevel@tonic-gate 		*bp++ = tmpc; \
25567c478bd9Sstevel@tonic-gate 	}
25577c478bd9Sstevel@tonic-gate 
25581d6b7ad8Srie /*
25591d6b7ad8Srie  * Define a local buffer size for building a numeric value - large enough to
25601d6b7ad8Srie  * hold a 64-bit value.
25611d6b7ad8Srie  */
2562051d39bbSrie #define	NUM_SIZE	22
25631d6b7ad8Srie 
25647c478bd9Sstevel@tonic-gate size_t
25657c478bd9Sstevel@tonic-gate doprf(const char *format, va_list args, Prfbuf *prf)
25667c478bd9Sstevel@tonic-gate {
25677c478bd9Sstevel@tonic-gate 	char	c;
25687c478bd9Sstevel@tonic-gate 	char	*bp = prf->pr_cur;
25697c478bd9Sstevel@tonic-gate 	char	*bufend = prf->pr_buf + prf->pr_len;
25707c478bd9Sstevel@tonic-gate 	size_t	bufsiz = prf->pr_len;
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 	while ((c = *format++) != '\0') {
25737c478bd9Sstevel@tonic-gate 		if (c != '%') {
25747c478bd9Sstevel@tonic-gate 			PUTC(c);
25757c478bd9Sstevel@tonic-gate 		} else {
25767c478bd9Sstevel@tonic-gate 			int	base = 0, flag = 0, width = 0, prec = 0;
25777c478bd9Sstevel@tonic-gate 			size_t	_i;
25787c478bd9Sstevel@tonic-gate 			int	_c, _n;
25797c478bd9Sstevel@tonic-gate 			char	*_s;
25807c478bd9Sstevel@tonic-gate 			int	ls = 0;
25817c478bd9Sstevel@tonic-gate again:
25827c478bd9Sstevel@tonic-gate 			c = *format++;
25837c478bd9Sstevel@tonic-gate 			switch (c) {
25847c478bd9Sstevel@tonic-gate 			case '-':
25857c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_MINUS;
25867c478bd9Sstevel@tonic-gate 				goto again;
25877c478bd9Sstevel@tonic-gate 			case '#':
25887c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_SHARP;
25897c478bd9Sstevel@tonic-gate 				goto again;
25907c478bd9Sstevel@tonic-gate 			case '.':
25917c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_DOTSEEN;
25927c478bd9Sstevel@tonic-gate 				goto again;
25937c478bd9Sstevel@tonic-gate 			case '0':
25947c478bd9Sstevel@tonic-gate 			case '1':
25957c478bd9Sstevel@tonic-gate 			case '2':
25967c478bd9Sstevel@tonic-gate 			case '3':
25977c478bd9Sstevel@tonic-gate 			case '4':
25987c478bd9Sstevel@tonic-gate 			case '5':
25997c478bd9Sstevel@tonic-gate 			case '6':
26007c478bd9Sstevel@tonic-gate 			case '7':
26017c478bd9Sstevel@tonic-gate 			case '8':
26027c478bd9Sstevel@tonic-gate 			case '9':
26037c478bd9Sstevel@tonic-gate 				if (flag & FLG_UT_DOTSEEN)
26047c478bd9Sstevel@tonic-gate 					prec = (prec * 10) + c - '0';
26057c478bd9Sstevel@tonic-gate 				else
26067c478bd9Sstevel@tonic-gate 					width = (width * 10) + c - '0';
26077c478bd9Sstevel@tonic-gate 				goto again;
26087c478bd9Sstevel@tonic-gate 			case 'x':
26097c478bd9Sstevel@tonic-gate 			case 'X':
26107c478bd9Sstevel@tonic-gate 				base = 16;
26117c478bd9Sstevel@tonic-gate 				break;
26127c478bd9Sstevel@tonic-gate 			case 'd':
26137c478bd9Sstevel@tonic-gate 			case 'D':
26147c478bd9Sstevel@tonic-gate 			case 'u':
26157c478bd9Sstevel@tonic-gate 				base = 10;
26167c478bd9Sstevel@tonic-gate 				flag &= ~FLG_UT_SHARP;
26177c478bd9Sstevel@tonic-gate 				break;
26187c478bd9Sstevel@tonic-gate 			case 'l':
26197c478bd9Sstevel@tonic-gate 				base = 10;
26207c478bd9Sstevel@tonic-gate 				ls++; /* number of l's (long or long long) */
26217c478bd9Sstevel@tonic-gate 				if ((*format == 'l') ||
26227c478bd9Sstevel@tonic-gate 				    (*format == 'd') || (*format == 'D') ||
26237c478bd9Sstevel@tonic-gate 				    (*format == 'x') || (*format == 'X') ||
26247c478bd9Sstevel@tonic-gate 				    (*format == 'o') || (*format == 'O'))
26257c478bd9Sstevel@tonic-gate 					goto again;
26267c478bd9Sstevel@tonic-gate 				break;
26277c478bd9Sstevel@tonic-gate 			case 'o':
26287c478bd9Sstevel@tonic-gate 			case 'O':
26297c478bd9Sstevel@tonic-gate 				base = 8;
26307c478bd9Sstevel@tonic-gate 				break;
26317c478bd9Sstevel@tonic-gate 			case 'c':
26327c478bd9Sstevel@tonic-gate 				_c = va_arg(args, int);
26337c478bd9Sstevel@tonic-gate 
26347c478bd9Sstevel@tonic-gate 				for (_i = 24; _i > 0; _i -= 8) {
26357c478bd9Sstevel@tonic-gate 					if ((c = ((_c >> _i) & 0x7f)) != 0) {
26367c478bd9Sstevel@tonic-gate 						PUTC(c);
26377c478bd9Sstevel@tonic-gate 					}
26387c478bd9Sstevel@tonic-gate 				}
26397c478bd9Sstevel@tonic-gate 				if ((c = ((_c >> _i) & 0x7f)) != 0) {
26407c478bd9Sstevel@tonic-gate 					PUTC(c);
26417c478bd9Sstevel@tonic-gate 				}
26427c478bd9Sstevel@tonic-gate 				break;
26437c478bd9Sstevel@tonic-gate 			case 's':
26447c478bd9Sstevel@tonic-gate 				_s = va_arg(args, char *);
26457c478bd9Sstevel@tonic-gate 				_i = strlen(_s);
26467c478bd9Sstevel@tonic-gate 				/* LINTED */
26477c478bd9Sstevel@tonic-gate 				_n = (int)(width - _i);
26487c478bd9Sstevel@tonic-gate 				if (!prec)
26497c478bd9Sstevel@tonic-gate 					/* LINTED */
26507c478bd9Sstevel@tonic-gate 					prec = (int)_i;
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
26537c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
26547c478bd9Sstevel@tonic-gate 						PUTC(' ');
26557c478bd9Sstevel@tonic-gate 				}
26567c478bd9Sstevel@tonic-gate 				while (((c = *_s++) != 0) && prec--) {
26577c478bd9Sstevel@tonic-gate 					PUTC(c);
26587c478bd9Sstevel@tonic-gate 				}
26597c478bd9Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
26607c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
26617c478bd9Sstevel@tonic-gate 						PUTC(' ');
26627c478bd9Sstevel@tonic-gate 				}
26637c478bd9Sstevel@tonic-gate 				break;
26647c478bd9Sstevel@tonic-gate 			case '%':
26657c478bd9Sstevel@tonic-gate 				PUTC('%');
26667c478bd9Sstevel@tonic-gate 				break;
26677c478bd9Sstevel@tonic-gate 			default:
26687c478bd9Sstevel@tonic-gate 				break;
26697c478bd9Sstevel@tonic-gate 			}
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate 			/*
26727c478bd9Sstevel@tonic-gate 			 * Numeric processing
26737c478bd9Sstevel@tonic-gate 			 */
26747c478bd9Sstevel@tonic-gate 			if (base) {
26751d6b7ad8Srie 				char		local[NUM_SIZE];
26761d6b7ad8Srie 				size_t		ssize = 0, psize = 0;
26777c478bd9Sstevel@tonic-gate 				const char	*string =
2678a953e2b1Srie 				    MSG_ORIG(MSG_STR_HEXNUM);
26797c478bd9Sstevel@tonic-gate 				const char	*prefix =
2680a953e2b1Srie 				    MSG_ORIG(MSG_STR_EMPTY);
26817c478bd9Sstevel@tonic-gate 				u_longlong_t	num;
26827c478bd9Sstevel@tonic-gate 
26837c478bd9Sstevel@tonic-gate 				switch (ls) {
26847c478bd9Sstevel@tonic-gate 				case 0:	/* int */
26857c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)
26867c478bd9Sstevel@tonic-gate 					    va_arg(args, uint_t);
26877c478bd9Sstevel@tonic-gate 					break;
26887c478bd9Sstevel@tonic-gate 				case 1:	/* long */
26897c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)
26907c478bd9Sstevel@tonic-gate 					    va_arg(args, ulong_t);
26917c478bd9Sstevel@tonic-gate 					break;
26927c478bd9Sstevel@tonic-gate 				case 2:	/* long long */
26937c478bd9Sstevel@tonic-gate 					num = va_arg(args, u_longlong_t);
26947c478bd9Sstevel@tonic-gate 					break;
26957c478bd9Sstevel@tonic-gate 				}
26967c478bd9Sstevel@tonic-gate 
26977c478bd9Sstevel@tonic-gate 				if (flag & FLG_UT_SHARP) {
26987c478bd9Sstevel@tonic-gate 					if (base == 16) {
26997c478bd9Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_HEX);
27007c478bd9Sstevel@tonic-gate 						psize = 2;
27017c478bd9Sstevel@tonic-gate 					} else {
27027c478bd9Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_ZERO);
27037c478bd9Sstevel@tonic-gate 						psize = 1;
27047c478bd9Sstevel@tonic-gate 					}
27057c478bd9Sstevel@tonic-gate 				}
27067c478bd9Sstevel@tonic-gate 				if ((base == 10) && (long)num < 0) {
27077c478bd9Sstevel@tonic-gate 					prefix = MSG_ORIG(MSG_STR_NEGATE);
27087c478bd9Sstevel@tonic-gate 					psize = MSG_STR_NEGATE_SIZE;
27097c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)(-(longlong_t)num);
27107c478bd9Sstevel@tonic-gate 				}
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 				/*
27137c478bd9Sstevel@tonic-gate 				 * Convert the numeric value into a local
27147c478bd9Sstevel@tonic-gate 				 * string (stored in reverse order).
27157c478bd9Sstevel@tonic-gate 				 */
27167c478bd9Sstevel@tonic-gate 				_s = local;
27177c478bd9Sstevel@tonic-gate 				do {
27187c478bd9Sstevel@tonic-gate 					*_s++ = string[num % base];
27197c478bd9Sstevel@tonic-gate 					num /= base;
27207c478bd9Sstevel@tonic-gate 					ssize++;
27217c478bd9Sstevel@tonic-gate 				} while (num);
27227c478bd9Sstevel@tonic-gate 
27231d6b7ad8Srie 				ASSERT(ssize < sizeof (local));
27241d6b7ad8Srie 
27257c478bd9Sstevel@tonic-gate 				/*
27267c478bd9Sstevel@tonic-gate 				 * Provide any precision or width padding.
27277c478bd9Sstevel@tonic-gate 				 */
27287c478bd9Sstevel@tonic-gate 				if (prec) {
27297c478bd9Sstevel@tonic-gate 					/* LINTED */
27307c478bd9Sstevel@tonic-gate 					_n = (int)(prec - ssize);
27311d6b7ad8Srie 					while ((_n-- > 0) &&
27321d6b7ad8Srie 					    (ssize < sizeof (local))) {
27337c478bd9Sstevel@tonic-gate 						*_s++ = '0';
27347c478bd9Sstevel@tonic-gate 						ssize++;
27357c478bd9Sstevel@tonic-gate 					}
27367c478bd9Sstevel@tonic-gate 				}
27377c478bd9Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
27387c478bd9Sstevel@tonic-gate 					/* LINTED */
27397c478bd9Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
27407c478bd9Sstevel@tonic-gate 					while (_n-- > 0) {
27417c478bd9Sstevel@tonic-gate 						PUTC(' ');
27427c478bd9Sstevel@tonic-gate 					}
27437c478bd9Sstevel@tonic-gate 				}
27447c478bd9Sstevel@tonic-gate 
27457c478bd9Sstevel@tonic-gate 				/*
27467c478bd9Sstevel@tonic-gate 				 * Print any prefix and the numeric string
27477c478bd9Sstevel@tonic-gate 				 */
27487c478bd9Sstevel@tonic-gate 				while (*prefix)
27497c478bd9Sstevel@tonic-gate 					PUTC(*prefix++);
27507c478bd9Sstevel@tonic-gate 				do {
27517c478bd9Sstevel@tonic-gate 					PUTC(*--_s);
27527c478bd9Sstevel@tonic-gate 				} while (_s > local);
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 				/*
27557c478bd9Sstevel@tonic-gate 				 * Provide any width padding.
27567c478bd9Sstevel@tonic-gate 				 */
27577c478bd9Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
27587c478bd9Sstevel@tonic-gate 					/* LINTED */
27597c478bd9Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
27607c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
27617c478bd9Sstevel@tonic-gate 						PUTC(' ');
27627c478bd9Sstevel@tonic-gate 				}
27637c478bd9Sstevel@tonic-gate 			}
27647c478bd9Sstevel@tonic-gate 		}
27657c478bd9Sstevel@tonic-gate 	}
276610a4fa49Srie 
27677c478bd9Sstevel@tonic-gate 	PUTC('\0');
27687c478bd9Sstevel@tonic-gate 	prf->pr_cur = bp;
27697c478bd9Sstevel@tonic-gate 	return (1);
27707c478bd9Sstevel@tonic-gate }
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate static int
27737c478bd9Sstevel@tonic-gate doprintf(const char *format, va_list args, Prfbuf *prf)
27747c478bd9Sstevel@tonic-gate {
27757c478bd9Sstevel@tonic-gate 	char	*ocur = prf->pr_cur;
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate 	if (doprf(format, args, prf) == 0)
27787c478bd9Sstevel@tonic-gate 		return (0);
27797c478bd9Sstevel@tonic-gate 	/* LINTED */
27807c478bd9Sstevel@tonic-gate 	return ((int)(prf->pr_cur - ocur));
27817c478bd9Sstevel@tonic-gate }
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate /* VARARGS2 */
27847c478bd9Sstevel@tonic-gate int
27857c478bd9Sstevel@tonic-gate sprintf(char *buf, const char *format, ...)
27867c478bd9Sstevel@tonic-gate {
27877c478bd9Sstevel@tonic-gate 	va_list	args;
27887c478bd9Sstevel@tonic-gate 	int	len;
27897c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	va_start(args, format);
27927c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
27937c478bd9Sstevel@tonic-gate 	prf.pr_len = 0;
27947c478bd9Sstevel@tonic-gate 	prf.pr_fd = -1;
27957c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
27967c478bd9Sstevel@tonic-gate 	va_end(args);
27977c478bd9Sstevel@tonic-gate 
27987c478bd9Sstevel@tonic-gate 	/*
27997c478bd9Sstevel@tonic-gate 	 * sprintf() return value excludes the terminating null byte.
28007c478bd9Sstevel@tonic-gate 	 */
28017c478bd9Sstevel@tonic-gate 	return (len - 1);
28027c478bd9Sstevel@tonic-gate }
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate /* VARARGS3 */
28057c478bd9Sstevel@tonic-gate int
28067c478bd9Sstevel@tonic-gate snprintf(char *buf, size_t n, const char *format, ...)
28077c478bd9Sstevel@tonic-gate {
28087c478bd9Sstevel@tonic-gate 	va_list	args;
28097c478bd9Sstevel@tonic-gate 	int	len;
28107c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	va_start(args, format);
28137c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
28147c478bd9Sstevel@tonic-gate 	prf.pr_len = n;
28157c478bd9Sstevel@tonic-gate 	prf.pr_fd = -1;
28167c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
28177c478bd9Sstevel@tonic-gate 	va_end(args);
28187c478bd9Sstevel@tonic-gate 
28197c478bd9Sstevel@tonic-gate 	return (len);
28207c478bd9Sstevel@tonic-gate }
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate /* VARARGS2 */
28237c478bd9Sstevel@tonic-gate int
28247c478bd9Sstevel@tonic-gate bufprint(Prfbuf *prf, const char *format, ...)
28257c478bd9Sstevel@tonic-gate {
28267c478bd9Sstevel@tonic-gate 	va_list	args;
28277c478bd9Sstevel@tonic-gate 	int	len;
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	va_start(args, format);
28307c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, prf);
28317c478bd9Sstevel@tonic-gate 	va_end(args);
28327c478bd9Sstevel@tonic-gate 
28337c478bd9Sstevel@tonic-gate 	return (len);
28347c478bd9Sstevel@tonic-gate }
28357c478bd9Sstevel@tonic-gate 
28367c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
28377c478bd9Sstevel@tonic-gate int
28387c478bd9Sstevel@tonic-gate printf(const char *format, ...)
28397c478bd9Sstevel@tonic-gate {
28407c478bd9Sstevel@tonic-gate 	va_list	args;
28417c478bd9Sstevel@tonic-gate 	char 	buffer[ERRSIZE];
28427c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
28437c478bd9Sstevel@tonic-gate 
28447c478bd9Sstevel@tonic-gate 	va_start(args, format);
28457c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buffer;
28467c478bd9Sstevel@tonic-gate 	prf.pr_len = ERRSIZE;
28477c478bd9Sstevel@tonic-gate 	prf.pr_fd = 1;
28487c478bd9Sstevel@tonic-gate 	(void) doprf(format, args, &prf);
28497c478bd9Sstevel@tonic-gate 	va_end(args);
28507c478bd9Sstevel@tonic-gate 	/*
28517c478bd9Sstevel@tonic-gate 	 * Trim trailing '\0' form buffer
28527c478bd9Sstevel@tonic-gate 	 */
28537c478bd9Sstevel@tonic-gate 	prf.pr_cur--;
28547c478bd9Sstevel@tonic-gate 	return (dowrite(&prf));
28557c478bd9Sstevel@tonic-gate }
28567c478bd9Sstevel@tonic-gate 
28577c478bd9Sstevel@tonic-gate static char	errbuf[ERRSIZE], *nextptr = errbuf, *prevptr = 0;
28587c478bd9Sstevel@tonic-gate 
28595aefb655Srie /*PRINTFLIKE3*/
28607c478bd9Sstevel@tonic-gate void
28615aefb655Srie eprintf(Lm_list *lml, Error error, const char *format, ...)
28627c478bd9Sstevel@tonic-gate {
28637c478bd9Sstevel@tonic-gate 	va_list		args;
28647c478bd9Sstevel@tonic-gate 	int		overflow = 0;
28657c478bd9Sstevel@tonic-gate 	static int	lock = 0;
28667c478bd9Sstevel@tonic-gate 	Prfbuf		prf;
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 	if (lock || (nextptr == (errbuf + ERRSIZE)))
28697c478bd9Sstevel@tonic-gate 		return;
28707c478bd9Sstevel@tonic-gate 
28717c478bd9Sstevel@tonic-gate 	/*
28727c478bd9Sstevel@tonic-gate 	 * Note: this lock is here to prevent the same thread from recursively
28737c478bd9Sstevel@tonic-gate 	 * entering itself during a eprintf.  ie: during eprintf malloc() fails
28747c478bd9Sstevel@tonic-gate 	 * and we try and call eprintf ... and then malloc() fails ....
28757c478bd9Sstevel@tonic-gate 	 */
28767c478bd9Sstevel@tonic-gate 	lock = 1;
28777c478bd9Sstevel@tonic-gate 
28787c478bd9Sstevel@tonic-gate 	/*
28797c478bd9Sstevel@tonic-gate 	 * If we have completed startup initialization, all error messages
28807c478bd9Sstevel@tonic-gate 	 * must be saved.  These are reported through dlerror().  If we're
28817c478bd9Sstevel@tonic-gate 	 * still in the initialization stage, output the error directly and
28827c478bd9Sstevel@tonic-gate 	 * add a newline.
28837c478bd9Sstevel@tonic-gate 	 */
28847c478bd9Sstevel@tonic-gate 	va_start(args, format);
28857c478bd9Sstevel@tonic-gate 
28867c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = nextptr;
28877c478bd9Sstevel@tonic-gate 	prf.pr_len = ERRSIZE - (nextptr - errbuf);
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_APPLIC))
28907c478bd9Sstevel@tonic-gate 		prf.pr_fd = 2;
28917c478bd9Sstevel@tonic-gate 	else
28927c478bd9Sstevel@tonic-gate 		prf.pr_fd = -1;
28937c478bd9Sstevel@tonic-gate 
28947c478bd9Sstevel@tonic-gate 	if (error > ERR_NONE) {
28957c478bd9Sstevel@tonic-gate 		if ((error == ERR_FATAL) && (rtld_flags2 & RT_FL2_FTL2WARN))
28967c478bd9Sstevel@tonic-gate 			error = ERR_WARNING;
28977c478bd9Sstevel@tonic-gate 		if (error == ERR_WARNING) {
28987c478bd9Sstevel@tonic-gate 			if (err_strs[ERR_WARNING] == 0)
2899a953e2b1Srie 				err_strs[ERR_WARNING] =
2900a953e2b1Srie 				    MSG_INTL(MSG_ERR_WARNING);
29017c478bd9Sstevel@tonic-gate 		} else if (error == ERR_FATAL) {
29027c478bd9Sstevel@tonic-gate 			if (err_strs[ERR_FATAL] == 0)
2903a953e2b1Srie 				err_strs[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
29047c478bd9Sstevel@tonic-gate 		} else if (error == ERR_ELF) {
29057c478bd9Sstevel@tonic-gate 			if (err_strs[ERR_ELF] == 0)
2906a953e2b1Srie 				err_strs[ERR_ELF] = MSG_INTL(MSG_ERR_ELF);
29077c478bd9Sstevel@tonic-gate 		}
290841072f3cSrie 		if (procname) {
290941072f3cSrie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR1),
291041072f3cSrie 			    rtldname, procname, err_strs[error]) == 0)
291141072f3cSrie 				overflow = 1;
29127c478bd9Sstevel@tonic-gate 		} else {
291341072f3cSrie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
291441072f3cSrie 			    rtldname, err_strs[error]) == 0)
291541072f3cSrie 				overflow = 1;
291641072f3cSrie 		}
291741072f3cSrie 		if (overflow == 0) {
29187c478bd9Sstevel@tonic-gate 			/*
29197c478bd9Sstevel@tonic-gate 			 * Remove the terminating '\0'.
29207c478bd9Sstevel@tonic-gate 			 */
29217c478bd9Sstevel@tonic-gate 			prf.pr_cur--;
29227c478bd9Sstevel@tonic-gate 		}
29237c478bd9Sstevel@tonic-gate 	}
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 	if ((overflow == 0) && doprf(format, args, &prf) == 0)
29267c478bd9Sstevel@tonic-gate 		overflow = 1;
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	/*
29297c478bd9Sstevel@tonic-gate 	 * If this is an ELF error, it will have been generated by a support
29307c478bd9Sstevel@tonic-gate 	 * object that has a dependency on libelf.  ld.so.1 doesn't generate any
29317c478bd9Sstevel@tonic-gate 	 * ELF error messages as it doesn't interact with libelf.  Determine the
29327c478bd9Sstevel@tonic-gate 	 * ELF error string.
29337c478bd9Sstevel@tonic-gate 	 */
29347c478bd9Sstevel@tonic-gate 	if ((overflow == 0) && (error == ERR_ELF)) {
29357c478bd9Sstevel@tonic-gate 		static int		(*elfeno)() = 0;
29367c478bd9Sstevel@tonic-gate 		static const char	*(*elfemg)();
29377c478bd9Sstevel@tonic-gate 		const char		*emsg;
29387c478bd9Sstevel@tonic-gate 		Rt_map			*dlmp, *lmp = lml_rtld.lm_head;
29397c478bd9Sstevel@tonic-gate 
29407c478bd9Sstevel@tonic-gate 		if (NEXT(lmp) && (elfeno == 0)) {
29417c478bd9Sstevel@tonic-gate 			if (((elfemg = (const char *(*)())dlsym_intn(RTLD_NEXT,
29427c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_ELFERRMSG), lmp, &dlmp)) == 0) ||
29437c478bd9Sstevel@tonic-gate 			    ((elfeno = (int (*)())dlsym_intn(RTLD_NEXT,
29447c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_ELFERRNO), lmp, &dlmp)) == 0))
29457c478bd9Sstevel@tonic-gate 				elfeno = 0;
29467c478bd9Sstevel@tonic-gate 		}
29477c478bd9Sstevel@tonic-gate 
29487c478bd9Sstevel@tonic-gate 		/*
29497c478bd9Sstevel@tonic-gate 		 * Lookup the message; equivalent to elf_errmsg(elf_errno()).
29507c478bd9Sstevel@tonic-gate 		 */
29517c478bd9Sstevel@tonic-gate 		if (elfeno && ((emsg = (* elfemg)((* elfeno)())) != 0)) {
29527c478bd9Sstevel@tonic-gate 			prf.pr_cur--;
29537c478bd9Sstevel@tonic-gate 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
29547c478bd9Sstevel@tonic-gate 			    emsg) == 0)
29557c478bd9Sstevel@tonic-gate 				overflow = 1;
29567c478bd9Sstevel@tonic-gate 		}
29577c478bd9Sstevel@tonic-gate 	}
29587c478bd9Sstevel@tonic-gate 
29597c478bd9Sstevel@tonic-gate 	/*
29607c478bd9Sstevel@tonic-gate 	 * Push out any message that's been built.  Note, in the case of an
29617c478bd9Sstevel@tonic-gate 	 * overflow condition, this message may be incomplete, in which case
29627c478bd9Sstevel@tonic-gate 	 * make sure any partial string is null terminated.
29637c478bd9Sstevel@tonic-gate 	 */
29647c478bd9Sstevel@tonic-gate 	if (overflow)
29657c478bd9Sstevel@tonic-gate 		*(prf.pr_cur) = '\0';
29667c478bd9Sstevel@tonic-gate 	if ((rtld_flags & (RT_FL_APPLIC | RT_FL_SILENCERR)) == 0) {
29677c478bd9Sstevel@tonic-gate 		*(prf.pr_cur - 1) = '\n';
29687c478bd9Sstevel@tonic-gate 		(void) dowrite(&prf);
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate 
29715aefb655Srie 	DBG_CALL(Dbg_util_str(lml, nextptr));
29727c478bd9Sstevel@tonic-gate 	va_end(args);
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	/*
29757c478bd9Sstevel@tonic-gate 	 * Determine if there was insufficient space left in the buffer to
29767c478bd9Sstevel@tonic-gate 	 * complete the message.  If so, we'll have printed out as much as had
29777c478bd9Sstevel@tonic-gate 	 * been processed if we're not yet executing the application.
29787c478bd9Sstevel@tonic-gate 	 * Otherwise, there will be some debugging diagnostic indicating
29797c478bd9Sstevel@tonic-gate 	 * as much of the error message as possible.  Write out a final buffer
29807c478bd9Sstevel@tonic-gate 	 * overflow diagnostic - unlocalized, so we don't chance more errors.
29817c478bd9Sstevel@tonic-gate 	 */
29827c478bd9Sstevel@tonic-gate 	if (overflow) {
29837c478bd9Sstevel@tonic-gate 		char	*str = (char *)MSG_INTL(MSG_EMG_BUFOVRFLW);
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) == 0) {
29867c478bd9Sstevel@tonic-gate 			lasterr = str;
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_APPLIC) == 0) {
29897c478bd9Sstevel@tonic-gate 				(void) write(2, str, strlen(str));
29907c478bd9Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
29917c478bd9Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
29927c478bd9Sstevel@tonic-gate 			}
29937c478bd9Sstevel@tonic-gate 		}
29945aefb655Srie 		DBG_CALL(Dbg_util_str(lml, str));
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 		lock = 0;
29977c478bd9Sstevel@tonic-gate 		nextptr = errbuf + ERRSIZE;
29987c478bd9Sstevel@tonic-gate 		return;
29997c478bd9Sstevel@tonic-gate 	}
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	/*
30027c478bd9Sstevel@tonic-gate 	 * If the application has started, then error messages are being saved
30037c478bd9Sstevel@tonic-gate 	 * for retrieval by dlerror(), or possible flushing from rtldexit() in
30047c478bd9Sstevel@tonic-gate 	 * the case of a fatal error.  In this case, establish the next error
30057c478bd9Sstevel@tonic-gate 	 * pointer.  If we haven't started the application, the whole message
30067c478bd9Sstevel@tonic-gate 	 * buffer can be reused.
30077c478bd9Sstevel@tonic-gate 	 */
30087c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SILENCERR) == 0) {
30097c478bd9Sstevel@tonic-gate 		lasterr = nextptr;
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 		/*
30127c478bd9Sstevel@tonic-gate 		 * Note, should we encounter an error such as ENOMEM, there may
30137c478bd9Sstevel@tonic-gate 		 * be a number of the same error messages (ie. an operation
30147c478bd9Sstevel@tonic-gate 		 * fails with ENOMEM, and then the attempts to construct the
30157c478bd9Sstevel@tonic-gate 		 * error message itself, which incurs additional ENOMEM errors).
30167c478bd9Sstevel@tonic-gate 		 * Compare any previous error message with the one we've just
30177c478bd9Sstevel@tonic-gate 		 * created to prevent any duplication clutter.
30187c478bd9Sstevel@tonic-gate 		 */
30197c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) &&
30207c478bd9Sstevel@tonic-gate 		    ((prevptr == 0) || (strcmp(prevptr, nextptr) != 0))) {
30217c478bd9Sstevel@tonic-gate 			prevptr = nextptr;
30227c478bd9Sstevel@tonic-gate 			nextptr = prf.pr_cur;
30237c478bd9Sstevel@tonic-gate 			*nextptr = '\0';
30247c478bd9Sstevel@tonic-gate 		}
30257c478bd9Sstevel@tonic-gate 	}
30267c478bd9Sstevel@tonic-gate 	lock = 0;
30277c478bd9Sstevel@tonic-gate }
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate #if	DEBUG
30317c478bd9Sstevel@tonic-gate /*
30327c478bd9Sstevel@tonic-gate  * Provide assfail() for ASSERT() statements,
30337c478bd9Sstevel@tonic-gate  * see <sys/debug.h> for further details.
30347c478bd9Sstevel@tonic-gate  */
30357c478bd9Sstevel@tonic-gate int
30367c478bd9Sstevel@tonic-gate assfail(const char *a, const char *f, int l)
30377c478bd9Sstevel@tonic-gate {
30387c478bd9Sstevel@tonic-gate 	(void) printf("assertion failed: %s, file: %s, line: %d\n", a, f, l);
30397c478bd9Sstevel@tonic-gate 	(void) _lwp_kill(_lwp_self(), SIGABRT);
30407c478bd9Sstevel@tonic-gate 	return (0);
30417c478bd9Sstevel@tonic-gate }
30427c478bd9Sstevel@tonic-gate #endif
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate /*
30457c478bd9Sstevel@tonic-gate  * Exit.  If we arrive here with a non zero status it's because of a fatal
30467c478bd9Sstevel@tonic-gate  * error condition (most commonly a relocation error).  If the application has
30477c478bd9Sstevel@tonic-gate  * already had control, then the actual fatal error message will have been
30487c478bd9Sstevel@tonic-gate  * recorded in the dlerror() message buffer.  Print the message before really
30497c478bd9Sstevel@tonic-gate  * exiting.
30507c478bd9Sstevel@tonic-gate  */
30517c478bd9Sstevel@tonic-gate void
30527c478bd9Sstevel@tonic-gate rtldexit(Lm_list * lml, int status)
30537c478bd9Sstevel@tonic-gate {
30547c478bd9Sstevel@tonic-gate 	if (status) {
30557c478bd9Sstevel@tonic-gate 		if (rtld_flags & RT_FL_APPLIC) {
30567c478bd9Sstevel@tonic-gate 			/*
30577c478bd9Sstevel@tonic-gate 			 * If the error buffer has been used, write out all
30587c478bd9Sstevel@tonic-gate 			 * pending messages - lasterr is simply a pointer to
30597c478bd9Sstevel@tonic-gate 			 * the last message in this buffer.  However, if the
30607c478bd9Sstevel@tonic-gate 			 * buffer couldn't be created at all, lasterr points
30617c478bd9Sstevel@tonic-gate 			 * to a constant error message string.
30627c478bd9Sstevel@tonic-gate 			 */
30637c478bd9Sstevel@tonic-gate 			if (*errbuf) {
30647c478bd9Sstevel@tonic-gate 				char	*errptr = errbuf;
30657c478bd9Sstevel@tonic-gate 				char	*errend = errbuf + ERRSIZE;
30667c478bd9Sstevel@tonic-gate 
30677c478bd9Sstevel@tonic-gate 				while ((errptr < errend) && *errptr) {
30687c478bd9Sstevel@tonic-gate 					size_t	size = strlen(errptr);
30697c478bd9Sstevel@tonic-gate 					(void) write(2, errptr, size);
30707c478bd9Sstevel@tonic-gate 					(void) write(2, MSG_ORIG(MSG_STR_NL),
30717c478bd9Sstevel@tonic-gate 					    MSG_STR_NL_SIZE);
30727c478bd9Sstevel@tonic-gate 					errptr += (size + 1);
30737c478bd9Sstevel@tonic-gate 				}
30747c478bd9Sstevel@tonic-gate 			}
30757c478bd9Sstevel@tonic-gate 			if (lasterr && ((lasterr < errbuf) ||
30767c478bd9Sstevel@tonic-gate 			    (lasterr > (errbuf + ERRSIZE)))) {
30777c478bd9Sstevel@tonic-gate 				(void) write(2, lasterr, strlen(lasterr));
30787c478bd9Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
30797c478bd9Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
30807c478bd9Sstevel@tonic-gate 			}
30817c478bd9Sstevel@tonic-gate 		}
30828cd45542Sraf 		leave(lml, 0);
30837c478bd9Sstevel@tonic-gate 		(void) _lwp_kill(_lwp_self(), killsig);
30847c478bd9Sstevel@tonic-gate 	}
30857c478bd9Sstevel@tonic-gate 	_exit(status);
30867c478bd9Sstevel@tonic-gate }
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate /*
30897c478bd9Sstevel@tonic-gate  * Routines to co-ordinate the opening of /dev/zero and /proc.
30907c478bd9Sstevel@tonic-gate  * dz_fd is exported for possible use by libld.so, and to insure it gets
30917c478bd9Sstevel@tonic-gate  * closed on leaving ld.so.1.
30927c478bd9Sstevel@tonic-gate  */
30937c478bd9Sstevel@tonic-gate int	dz_fd = FD_UNAVAIL;
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate void
30967c478bd9Sstevel@tonic-gate dz_init(int fd)
30977c478bd9Sstevel@tonic-gate {
30987c478bd9Sstevel@tonic-gate 	dz_fd = fd;
30997c478bd9Sstevel@tonic-gate }
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate /*
31037c478bd9Sstevel@tonic-gate  * mmap() a page from MAP_ANON
31047c478bd9Sstevel@tonic-gate  *
31057c478bd9Sstevel@tonic-gate  * Note: MAP_ANON is only on Solaris8++, we use this routine to
31067c478bd9Sstevel@tonic-gate  *       not only mmap(MAP_ANON) but to also probe if it is available
31077c478bd9Sstevel@tonic-gate  *	 on the current OS.
31087c478bd9Sstevel@tonic-gate  */
31097c478bd9Sstevel@tonic-gate Am_ret
31105aefb655Srie anon_map(Lm_list *lml, caddr_t *addr, size_t len, int prot, int flags)
31117c478bd9Sstevel@tonic-gate {
31127c478bd9Sstevel@tonic-gate #if defined(MAP_ANON)
31137c478bd9Sstevel@tonic-gate 	static int	noanon = 0;
31147c478bd9Sstevel@tonic-gate 	caddr_t		va;
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	if (noanon == 0) {
31177c478bd9Sstevel@tonic-gate 		if ((va = (caddr_t)mmap(*addr, len, prot,
31187c478bd9Sstevel@tonic-gate 		    (flags | MAP_ANON), -1, 0)) != MAP_FAILED) {
31197c478bd9Sstevel@tonic-gate 			*addr = va;
31207c478bd9Sstevel@tonic-gate 			return (AM_OK);
31217c478bd9Sstevel@tonic-gate 		}
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 		if ((errno != EBADF) && (errno != EINVAL)) {
31247c478bd9Sstevel@tonic-gate 			int	err = errno;
31255aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON),
31267c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
31277c478bd9Sstevel@tonic-gate 			return (AM_ERROR);
31287c478bd9Sstevel@tonic-gate 		} else
31297c478bd9Sstevel@tonic-gate 			noanon = 1;
31307c478bd9Sstevel@tonic-gate 	}
31317c478bd9Sstevel@tonic-gate #endif
31327c478bd9Sstevel@tonic-gate 	return (AM_NOSUP);
31337c478bd9Sstevel@tonic-gate }
31347c478bd9Sstevel@tonic-gate 
31357c478bd9Sstevel@tonic-gate /*
31367c478bd9Sstevel@tonic-gate  * Map anonymous memory from /dev/zero, or via MAP_ANON.
31377c478bd9Sstevel@tonic-gate  *
31387c478bd9Sstevel@tonic-gate  * (MAP_ANON only appears on Solaris 8, so we need fall-back
31397c478bd9Sstevel@tonic-gate  * behavior for older systems.)
31407c478bd9Sstevel@tonic-gate  */
31417c478bd9Sstevel@tonic-gate caddr_t
31425aefb655Srie dz_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
31437c478bd9Sstevel@tonic-gate {
31447c478bd9Sstevel@tonic-gate 	caddr_t	va;
31457c478bd9Sstevel@tonic-gate 	int	err;
31467c478bd9Sstevel@tonic-gate 	Am_ret	amret;
31477c478bd9Sstevel@tonic-gate 
31485aefb655Srie 	amret = anon_map(lml, &addr, len, prot, flags);
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	if (amret == AM_OK)
31517c478bd9Sstevel@tonic-gate 		return (addr);
31527c478bd9Sstevel@tonic-gate 	if (amret == AM_ERROR)
31537c478bd9Sstevel@tonic-gate 		return (MAP_FAILED);
31547c478bd9Sstevel@tonic-gate 
31557c478bd9Sstevel@tonic-gate 	/* amret == AM_NOSUP -> fallback to a devzero mmaping */
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 	if (dz_fd == FD_UNAVAIL) {
31587c478bd9Sstevel@tonic-gate 		if ((dz_fd = open(MSG_ORIG(MSG_PTH_DEVZERO),
31597c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
31607c478bd9Sstevel@tonic-gate 			err = errno;
31615aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
31627c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
31637c478bd9Sstevel@tonic-gate 			return (MAP_FAILED);
31647c478bd9Sstevel@tonic-gate 		}
31657c478bd9Sstevel@tonic-gate 	}
31667c478bd9Sstevel@tonic-gate 
31677c478bd9Sstevel@tonic-gate 	if ((va = mmap(addr, len, prot, flags, dz_fd, 0)) == MAP_FAILED) {
31687c478bd9Sstevel@tonic-gate 		err = errno;
31695aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
31707c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
31717c478bd9Sstevel@tonic-gate 	}
31727c478bd9Sstevel@tonic-gate 	return (va);
31737c478bd9Sstevel@tonic-gate }
31747c478bd9Sstevel@tonic-gate 
31757c478bd9Sstevel@tonic-gate static int	pr_fd = FD_UNAVAIL;
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate int
31785aefb655Srie pr_open(Lm_list *lml)
31797c478bd9Sstevel@tonic-gate {
31807c478bd9Sstevel@tonic-gate 	char	proc[16];
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate 	if (pr_fd == FD_UNAVAIL) {
31837c478bd9Sstevel@tonic-gate 		(void) snprintf(proc, 16, MSG_ORIG(MSG_FMT_PROC),
3184a953e2b1Srie 		    (int)getpid());
31857c478bd9Sstevel@tonic-gate 		if ((pr_fd = open(proc, O_RDONLY)) == FD_UNAVAIL) {
31867c478bd9Sstevel@tonic-gate 			int	err = errno;
31877c478bd9Sstevel@tonic-gate 
31885aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), proc,
31897c478bd9Sstevel@tonic-gate 			    strerror(err));
31907c478bd9Sstevel@tonic-gate 		}
31917c478bd9Sstevel@tonic-gate 	}
31927c478bd9Sstevel@tonic-gate 	return (pr_fd);
31937c478bd9Sstevel@tonic-gate }
31947c478bd9Sstevel@tonic-gate 
31957c478bd9Sstevel@tonic-gate static int	nu_fd = FD_UNAVAIL;
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate caddr_t
31985aefb655Srie nu_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
31997c478bd9Sstevel@tonic-gate {
32007c478bd9Sstevel@tonic-gate 	caddr_t	va;
32017c478bd9Sstevel@tonic-gate 	int	err;
32027c478bd9Sstevel@tonic-gate 
32037c478bd9Sstevel@tonic-gate 	if (nu_fd == FD_UNAVAIL) {
32047c478bd9Sstevel@tonic-gate 		if ((nu_fd = open(MSG_ORIG(MSG_PTH_DEVNULL),
32057c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
32067c478bd9Sstevel@tonic-gate 			err = errno;
32075aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
32087c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
32097c478bd9Sstevel@tonic-gate 			return (MAP_FAILED);
32107c478bd9Sstevel@tonic-gate 		}
32117c478bd9Sstevel@tonic-gate 	}
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 	if ((va = (caddr_t)mmap(addr, len, prot, flags, nu_fd, 0)) ==
32147c478bd9Sstevel@tonic-gate 	    MAP_FAILED) {
32157c478bd9Sstevel@tonic-gate 		err = errno;
32165aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
32177c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
32187c478bd9Sstevel@tonic-gate 	}
32197c478bd9Sstevel@tonic-gate 	return (va);
32207c478bd9Sstevel@tonic-gate }
32217c478bd9Sstevel@tonic-gate 
32227c478bd9Sstevel@tonic-gate /*
322375e7992aSrie  * Generic entry point from user code - simply grabs a lock, and bumps the
322475e7992aSrie  * entrance count.
32257c478bd9Sstevel@tonic-gate  */
32267c478bd9Sstevel@tonic-gate int
32278cd45542Sraf enter(int flags)
32287c478bd9Sstevel@tonic-gate {
32298cd45542Sraf 	if (rt_bind_guard(THR_FLG_RTLD | thr_flg_nolock | flags)) {
32308cd45542Sraf 		if (!thr_flg_nolock)
32318cd45542Sraf 			(void) rt_mutex_lock(&rtldlock);
32329aa23310Srie 		if (rtld_flags & RT_FL_OPERATION)
32339aa23310Srie 			ld_entry_cnt++;
32347c478bd9Sstevel@tonic-gate 		return (1);
32357c478bd9Sstevel@tonic-gate 	}
32367c478bd9Sstevel@tonic-gate 	return (0);
32377c478bd9Sstevel@tonic-gate }
32387c478bd9Sstevel@tonic-gate 
32399aa23310Srie /*
32409aa23310Srie  * Determine whether a search path has been used.
32419aa23310Srie  */
32429aa23310Srie static void
32439aa23310Srie is_path_used(Lm_list *lml, Word unref, int *nl, Pnode *pnp, const char *obj)
32449aa23310Srie {
32459aa23310Srie 	for (; pnp; pnp = pnp->p_next) {
32469aa23310Srie 		const char	*fmt, *name;
32479aa23310Srie 
32489aa23310Srie 		if ((pnp->p_len == 0) || (pnp->p_orig & PN_FLG_USED))
32499aa23310Srie 			continue;
32509aa23310Srie 
32519aa23310Srie 		/*
32529aa23310Srie 		 * If this pathname originated from an expanded token, use the
32539aa23310Srie 		 * original for any diagnostic output.
32549aa23310Srie 		 */
3255*3dbfc803SRod Evans 		if ((name = pnp->p_oname) == NULL)
32569aa23310Srie 			name = pnp->p_name;
32579aa23310Srie 
32589aa23310Srie 		if (unref == 0) {
32599aa23310Srie 			if ((*nl)++ == 0)
32609aa23310Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
32619aa23310Srie 			DBG_CALL(Dbg_unused_path(lml, name, pnp->p_orig,
32629aa23310Srie 			    (pnp->p_orig & PN_FLG_DUPLICAT), obj));
32639aa23310Srie 			continue;
32649aa23310Srie 		}
32659aa23310Srie 
32669aa23310Srie 		if (pnp->p_orig & LA_SER_LIBPATH) {
32679aa23310Srie 			if (pnp->p_orig & LA_SER_CONFIG) {
32689aa23310Srie 				if (pnp->p_orig & PN_FLG_DUPLICAT)
32699aa23310Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATHC);
32709aa23310Srie 				else
32719aa23310Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATHC);
32729aa23310Srie 			} else {
32739aa23310Srie 				if (pnp->p_orig & PN_FLG_DUPLICAT)
32749aa23310Srie 					fmt = MSG_INTL(MSG_DUP_LDLIBPATH);
32759aa23310Srie 				else
32769aa23310Srie 					fmt = MSG_INTL(MSG_USD_LDLIBPATH);
32779aa23310Srie 			}
32789aa23310Srie 		} else if (pnp->p_orig & LA_SER_RUNPATH) {
32799aa23310Srie 			fmt = MSG_INTL(MSG_USD_RUNPATH);
32809aa23310Srie 		} else
32819aa23310Srie 			continue;
32829aa23310Srie 
32839aa23310Srie 		if ((*nl)++ == 0)
32849aa23310Srie 			(void) printf(MSG_ORIG(MSG_STR_NL));
32859aa23310Srie 		(void) printf(fmt, name, obj);
32869aa23310Srie 	}
32879aa23310Srie }
32889aa23310Srie 
32897c478bd9Sstevel@tonic-gate /*
32907c478bd9Sstevel@tonic-gate  * Generate diagnostics as to whether an object has been used.  A symbolic
32917c478bd9Sstevel@tonic-gate  * reference that gets bound to an object marks it as used.  Dependencies that
32927c478bd9Sstevel@tonic-gate  * are unused when RTLD_NOW is in effect should be removed from future builds
32937c478bd9Sstevel@tonic-gate  * of an object.  Dependencies that are unused without RTLD_NOW in effect are
32947c478bd9Sstevel@tonic-gate  * candidates for lazy-loading.
32959aa23310Srie  *
32967c478bd9Sstevel@tonic-gate  * Unreferenced objects identify objects that are defined as dependencies but
32979aa23310Srie  * are unreferenced by the caller.  These unreferenced objects may however be
32989aa23310Srie  * referenced by other objects within the process, and therefore don't qualify
32999aa23310Srie  * as completely unused.  They are still an unnecessary overhead.
33009aa23310Srie  *
33019aa23310Srie  * Unreferenced runpaths are also captured under ldd -U, or "unused,detail"
33029aa23310Srie  * debugging.
33037c478bd9Sstevel@tonic-gate  */
33047c478bd9Sstevel@tonic-gate void
33057c478bd9Sstevel@tonic-gate unused(Lm_list *lml)
33067c478bd9Sstevel@tonic-gate {
33077c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
33087c478bd9Sstevel@tonic-gate 	int		nl = 0;
33099aa23310Srie 	Word		unref, unuse;
33107c478bd9Sstevel@tonic-gate 
33117c478bd9Sstevel@tonic-gate 	/*
33127c478bd9Sstevel@tonic-gate 	 * If we're not tracing unused references or dependencies, or debugging
33137c478bd9Sstevel@tonic-gate 	 * there's nothing to do.
33147c478bd9Sstevel@tonic-gate 	 */
33159aa23310Srie 	unref = lml->lm_flags & LML_FLG_TRC_UNREF;
33169aa23310Srie 	unuse = lml->lm_flags & LML_FLG_TRC_UNUSED;
33177c478bd9Sstevel@tonic-gate 
33189aa23310Srie 	if ((unref == 0) && (unuse == 0) && (DBG_ENABLED == 0))
33197c478bd9Sstevel@tonic-gate 		return;
33207c478bd9Sstevel@tonic-gate 
33219aa23310Srie 	/*
33229aa23310Srie 	 * Detect unused global search paths.
33239aa23310Srie 	 */
33249aa23310Srie 	if (rpl_libdirs)
33259aa23310Srie 		is_path_used(lml, unref, &nl, rpl_libdirs, config->c_name);
33269aa23310Srie 	if (prm_libdirs)
33279aa23310Srie 		is_path_used(lml, unref, &nl, prm_libdirs, config->c_name);
33289aa23310Srie 
33299aa23310Srie 	nl = 0;
33309aa23310Srie 	lmp = lml->lm_head;
33319aa23310Srie 	if (RLIST(lmp))
33329aa23310Srie 		is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
33339aa23310Srie 
33347c478bd9Sstevel@tonic-gate 	/*
33357c478bd9Sstevel@tonic-gate 	 * Traverse the link-maps looking for unreferenced or unused
33367c478bd9Sstevel@tonic-gate 	 * dependencies.  Ignore the first object on a link-map list, as this
33379aa23310Srie 	 * is always used.
33387c478bd9Sstevel@tonic-gate 	 */
33399aa23310Srie 	nl = 0;
33409aa23310Srie 	for (lmp = (Rt_map *)NEXT(lmp); lmp; lmp = (Rt_map *)NEXT(lmp)) {
33419aa23310Srie 		/*
33429aa23310Srie 		 * Determine if this object contains any runpaths that have
33439aa23310Srie 		 * not been used.
33449aa23310Srie 		 */
33459aa23310Srie 		if (RLIST(lmp))
33469aa23310Srie 			is_path_used(lml, unref, &nl, RLIST(lmp), NAME(lmp));
33479aa23310Srie 
33487c478bd9Sstevel@tonic-gate 		/*
33497c478bd9Sstevel@tonic-gate 		 * If tracing unreferenced objects, or under debugging,
33507c478bd9Sstevel@tonic-gate 		 * determine whether any of this objects callers haven't
33517c478bd9Sstevel@tonic-gate 		 * referenced it.
33527c478bd9Sstevel@tonic-gate 		 */
33539aa23310Srie 		if (unref || DBG_ENABLED) {
3354cce0e03bSab 			Bnd_desc	*bdp;
3355cce0e03bSab 			Aliste		idx;
33567c478bd9Sstevel@tonic-gate 
3357cce0e03bSab 			for (APLIST_TRAVERSE(CALLERS(lmp), idx, bdp)) {
33589aa23310Srie 				Rt_map	*clmp;
33597c478bd9Sstevel@tonic-gate 
33607c478bd9Sstevel@tonic-gate 				if (bdp->b_flags & BND_REFER)
33617c478bd9Sstevel@tonic-gate 					continue;
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 				clmp = bdp->b_caller;
33647c478bd9Sstevel@tonic-gate 				if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
33657c478bd9Sstevel@tonic-gate 					continue;
33667c478bd9Sstevel@tonic-gate 
3367a953e2b1Srie 				/* BEGIN CSTYLED */
33687c478bd9Sstevel@tonic-gate 				if (nl++ == 0) {
33699aa23310Srie 					if (unref)
33707c478bd9Sstevel@tonic-gate 					    (void) printf(MSG_ORIG(MSG_STR_NL));
33717c478bd9Sstevel@tonic-gate 					else
33725aefb655Srie 					    DBG_CALL(Dbg_util_nl(lml,
33735aefb655Srie 						DBG_NL_STD));
33747c478bd9Sstevel@tonic-gate 				}
33757c478bd9Sstevel@tonic-gate 
33769aa23310Srie 				if (unref)
33777c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_UNREF_FMT),
33787c478bd9Sstevel@tonic-gate 					NAME(lmp), NAME(clmp));
33797c478bd9Sstevel@tonic-gate 				else
33805aefb655Srie 				    DBG_CALL(Dbg_unused_unref(lmp, NAME(clmp)));
3381a953e2b1Srie 				/* END CSTYLED */
33827c478bd9Sstevel@tonic-gate 			}
33837c478bd9Sstevel@tonic-gate 		}
33847c478bd9Sstevel@tonic-gate 
33857c478bd9Sstevel@tonic-gate 		/*
33867c478bd9Sstevel@tonic-gate 		 * If tracing unused objects simply display those objects that
33877c478bd9Sstevel@tonic-gate 		 * haven't been referenced by anyone.
33887c478bd9Sstevel@tonic-gate 		 */
33897c478bd9Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_USED)
33907c478bd9Sstevel@tonic-gate 			continue;
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 		if (nl++ == 0) {
33939aa23310Srie 			if (unref || unuse)
33947c478bd9Sstevel@tonic-gate 				(void) printf(MSG_ORIG(MSG_STR_NL));
33957c478bd9Sstevel@tonic-gate 			else
33965aefb655Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
33977c478bd9Sstevel@tonic-gate 		}
33987c478bd9Sstevel@tonic-gate 		if (CYCGROUP(lmp)) {
33999aa23310Srie 			if (unref || unuse)
34007c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNCYC_FMT),
34017c478bd9Sstevel@tonic-gate 				    NAME(lmp), CYCGROUP(lmp));
34027c478bd9Sstevel@tonic-gate 			else
34035aefb655Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0,
34047c478bd9Sstevel@tonic-gate 				    CYCGROUP(lmp)));
34057c478bd9Sstevel@tonic-gate 		} else {
34069aa23310Srie 			if (unref || unuse)
34077c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNUSED_FMT),
34087c478bd9Sstevel@tonic-gate 				    NAME(lmp));
34097c478bd9Sstevel@tonic-gate 			else
34105aefb655Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0, 0));
34117c478bd9Sstevel@tonic-gate 		}
34127c478bd9Sstevel@tonic-gate 	}
34137c478bd9Sstevel@tonic-gate 
34145aefb655Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
34157c478bd9Sstevel@tonic-gate }
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate /*
34187c478bd9Sstevel@tonic-gate  * Initialization routine for the Fmap structure.  If the fmap structure is
34197c478bd9Sstevel@tonic-gate  * already in use, any mapping is released.  The structure is then initialized
34207c478bd9Sstevel@tonic-gate  * in preparation for further use.
34217c478bd9Sstevel@tonic-gate  */
34227c478bd9Sstevel@tonic-gate void
34237c478bd9Sstevel@tonic-gate fmap_setup()
34247c478bd9Sstevel@tonic-gate {
34257c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
34267c478bd9Sstevel@tonic-gate 	/*
34277c478bd9Sstevel@tonic-gate 	 * If MAP_ALIGN is set, the fm_addr has been seeded with an alignment
34287c478bd9Sstevel@tonic-gate 	 * value.  Otherwise, if fm_addr is non-null it indicates a mapping that
34297c478bd9Sstevel@tonic-gate 	 * should now be freed.
34307c478bd9Sstevel@tonic-gate 	 */
34317c478bd9Sstevel@tonic-gate 	if (fmap->fm_maddr && ((fmap->fm_mflags & MAP_ALIGN) == 0))
34327c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize);
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	/*
34357c478bd9Sstevel@tonic-gate 	 * Providing we haven't determined that this system doesn't support
34367c478bd9Sstevel@tonic-gate 	 * MAP_ALIGN, initialize the mapping address with the default segment
34377c478bd9Sstevel@tonic-gate 	 * alignment.
34387c478bd9Sstevel@tonic-gate 	 */
34397c478bd9Sstevel@tonic-gate 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
34407c478bd9Sstevel@tonic-gate 		fmap->fm_maddr = (char *)M_SEGM_ALIGN;
34417c478bd9Sstevel@tonic-gate 		fmap->fm_mflags = MAP_PRIVATE | MAP_ALIGN;
34427c478bd9Sstevel@tonic-gate 	} else {
34437c478bd9Sstevel@tonic-gate 		fmap->fm_maddr = 0;
34447c478bd9Sstevel@tonic-gate 		fmap->fm_mflags = MAP_PRIVATE;
34457c478bd9Sstevel@tonic-gate 	}
34467c478bd9Sstevel@tonic-gate #else
34477c478bd9Sstevel@tonic-gate 	if (fmap->fm_maddr)
34487c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize);
3449fb1354edSrie 
34507c478bd9Sstevel@tonic-gate 	fmap->fm_maddr = 0;
34517c478bd9Sstevel@tonic-gate 	fmap->fm_mflags = MAP_PRIVATE;
34527c478bd9Sstevel@tonic-gate #endif
34537c478bd9Sstevel@tonic-gate 
3454a953e2b1Srie 	fmap->fm_msize = FMAP_SIZE;
34557c478bd9Sstevel@tonic-gate 	fmap->fm_hwptr = 0;
34567c478bd9Sstevel@tonic-gate }
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate /*
34597c478bd9Sstevel@tonic-gate  * Generic cleanup routine called prior to returning control to the user.
34607c478bd9Sstevel@tonic-gate  * Insures that any ld.so.1 specific file descriptors or temporary mapping are
34617c478bd9Sstevel@tonic-gate  * released, and any locks dropped.
34627c478bd9Sstevel@tonic-gate  */
34637c478bd9Sstevel@tonic-gate void
34648cd45542Sraf leave(Lm_list *lml, int flags)
34657c478bd9Sstevel@tonic-gate {
346612b8e62eSrie 	Lm_list	*elml = lml;
3467cce0e03bSab 	Rt_map	*clmp;
3468cce0e03bSab 	Aliste	idx;
346912b8e62eSrie 
34707c478bd9Sstevel@tonic-gate 	/*
347112b8e62eSrie 	 * Alert the debuggers that the link-maps are consistent.  Note, in the
347212b8e62eSrie 	 * case of tearing down a whole link-map list, lml will be null.  In
347312b8e62eSrie 	 * this case use the main link-map list to test for a notification.
34747c478bd9Sstevel@tonic-gate 	 */
3475*3dbfc803SRod Evans 	if (elml == NULL)
347612b8e62eSrie 		elml = &lml_main;
347712b8e62eSrie 	if (elml->lm_flags & LML_FLG_DBNOTIF)
347812b8e62eSrie 		rd_event(elml, RD_DLACTIVITY, RT_CONSISTENT);
34797c478bd9Sstevel@tonic-gate 
34807247f888Srie 	/*
34817247f888Srie 	 * Alert any auditors that the link-maps are consistent.
34827247f888Srie 	 */
3483cce0e03bSab 	for (APLIST_TRAVERSE(elml->lm_actaudit, idx, clmp)) {
3484cce0e03bSab 		audit_activity(clmp, LA_ACT_CONSISTENT);
34857247f888Srie 
3486cce0e03bSab 		aplist_delete(elml->lm_actaudit, &idx);
34877247f888Srie 	}
34887247f888Srie 
34897c478bd9Sstevel@tonic-gate 	if (dz_fd != FD_UNAVAIL) {
34907c478bd9Sstevel@tonic-gate 		(void) close(dz_fd);
34917c478bd9Sstevel@tonic-gate 		dz_fd = FD_UNAVAIL;
34927c478bd9Sstevel@tonic-gate 	}
34937c478bd9Sstevel@tonic-gate 
34947c478bd9Sstevel@tonic-gate 	if (pr_fd != FD_UNAVAIL) {
34957c478bd9Sstevel@tonic-gate 		(void) close(pr_fd);
34967c478bd9Sstevel@tonic-gate 		pr_fd = FD_UNAVAIL;
34977c478bd9Sstevel@tonic-gate 	}
34987c478bd9Sstevel@tonic-gate 
34997c478bd9Sstevel@tonic-gate 	if (nu_fd != FD_UNAVAIL) {
35007c478bd9Sstevel@tonic-gate 		(void) close(nu_fd);
35017c478bd9Sstevel@tonic-gate 		nu_fd = FD_UNAVAIL;
35027c478bd9Sstevel@tonic-gate 	}
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 	fmap_setup();
35057c478bd9Sstevel@tonic-gate 
35067c478bd9Sstevel@tonic-gate 	/*
35077c478bd9Sstevel@tonic-gate 	 * Reinitialize error message pointer, and any overflow indication.
35087c478bd9Sstevel@tonic-gate 	 */
35097c478bd9Sstevel@tonic-gate 	nextptr = errbuf;
35107c478bd9Sstevel@tonic-gate 	prevptr = 0;
35117c478bd9Sstevel@tonic-gate 
35127c478bd9Sstevel@tonic-gate 	/*
35137c478bd9Sstevel@tonic-gate 	 * Don't drop our lock if we are running on our link-map list as
35147c478bd9Sstevel@tonic-gate 	 * there's little point in doing so since we are single-threaded.
35157c478bd9Sstevel@tonic-gate 	 *
35167c478bd9Sstevel@tonic-gate 	 * LML_FLG_HOLDLOCK is set for:
35177c478bd9Sstevel@tonic-gate 	 *	*) The ld.so.1's link-map list.
35187c478bd9Sstevel@tonic-gate 	 *	*) The auditor's link-map if the environment is
35197c478bd9Sstevel@tonic-gate 	 *	   libc/libthread un-unified.
35207c478bd9Sstevel@tonic-gate 	 */
35217c478bd9Sstevel@tonic-gate 	if (lml && (lml->lm_flags & LML_FLG_HOLDLOCK))
35227c478bd9Sstevel@tonic-gate 		return;
35237c478bd9Sstevel@tonic-gate 
35247c478bd9Sstevel@tonic-gate 	if (rt_bind_clear(0) & THR_FLG_RTLD) {
35258cd45542Sraf 		if (!thr_flg_nolock)
35268cd45542Sraf 			(void) rt_mutex_unlock(&rtldlock);
35278cd45542Sraf 		(void) rt_bind_clear(THR_FLG_RTLD | thr_flg_nolock | flags);
35287c478bd9Sstevel@tonic-gate 	}
35297c478bd9Sstevel@tonic-gate }
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate int
353260758829Srie callable(Rt_map *clmp, Rt_map *dlmp, Grp_hdl *ghp, uint_t slflags)
35337c478bd9Sstevel@tonic-gate {
3534cce0e03bSab 	APlist		*calp, *dalp;
3535cce0e03bSab 	Aliste		idx1, idx2;
3536cce0e03bSab 	Grp_hdl		*ghp1, *ghp2;
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate 	/*
35397c478bd9Sstevel@tonic-gate 	 * An object can always find symbols within itself.
35407c478bd9Sstevel@tonic-gate 	 */
35417c478bd9Sstevel@tonic-gate 	if (clmp == dlmp)
35427c478bd9Sstevel@tonic-gate 		return (1);
35437c478bd9Sstevel@tonic-gate 
354460758829Srie 	/*
354560758829Srie 	 * The search for a singleton must look in every loaded object.
354660758829Srie 	 */
354760758829Srie 	if (slflags & LKUP_SINGLETON)
354860758829Srie 		return (1);
354960758829Srie 
35507c478bd9Sstevel@tonic-gate 	/*
35517c478bd9Sstevel@tonic-gate 	 * Don't allow an object to bind to an object that is being deleted
35527c478bd9Sstevel@tonic-gate 	 * unless the binder is also being deleted.
35537c478bd9Sstevel@tonic-gate 	 */
35547c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & FLG_RT_DELETE) &&
35557c478bd9Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
35567c478bd9Sstevel@tonic-gate 		return (0);
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	/*
35597c478bd9Sstevel@tonic-gate 	 * An object with world access can always bind to an object with global
35607c478bd9Sstevel@tonic-gate 	 * visibility.
35617c478bd9Sstevel@tonic-gate 	 */
35627c478bd9Sstevel@tonic-gate 	if ((MODE(clmp) & RTLD_WORLD) && (MODE(dlmp) & RTLD_GLOBAL))
35637c478bd9Sstevel@tonic-gate 		return (1);
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate 	/*
35667c478bd9Sstevel@tonic-gate 	 * An object with local access can only bind to an object that is a
35677c478bd9Sstevel@tonic-gate 	 * member of the same group.
35687c478bd9Sstevel@tonic-gate 	 */
35697c478bd9Sstevel@tonic-gate 	if (((MODE(clmp) & RTLD_GROUP) == 0) ||
3570cce0e03bSab 	    ((calp = GROUPS(clmp)) == NULL) || ((dalp = GROUPS(dlmp)) == NULL))
35717c478bd9Sstevel@tonic-gate 		return (0);
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 	/*
35747c478bd9Sstevel@tonic-gate 	 * Traverse the list of groups the caller is a part of.
35757c478bd9Sstevel@tonic-gate 	 */
3576cce0e03bSab 	for (APLIST_TRAVERSE(calp, idx1, ghp1)) {
35777c478bd9Sstevel@tonic-gate 		/*
35787c478bd9Sstevel@tonic-gate 		 * If we're testing for the ability of two objects to bind to
35797c478bd9Sstevel@tonic-gate 		 * each other regardless of a specific group, ignore that group.
35807c478bd9Sstevel@tonic-gate 		 */
3581cce0e03bSab 		if (ghp && (ghp1 == ghp))
35827c478bd9Sstevel@tonic-gate 			continue;
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate 		/*
35857c478bd9Sstevel@tonic-gate 		 * Traverse the list of groups the destination is a part of.
35867c478bd9Sstevel@tonic-gate 		 */
3587cce0e03bSab 		for (APLIST_TRAVERSE(dalp, idx2, ghp2)) {
3588efb9e8b8Srie 			Grp_desc	*gdp;
3589cce0e03bSab 			Aliste		idx3;
3590efb9e8b8Srie 
3591cce0e03bSab 			if (ghp1 != ghp2)
3592efb9e8b8Srie 				continue;
3593efb9e8b8Srie 
3594efb9e8b8Srie 			/*
3595efb9e8b8Srie 			 * Make sure the relationship between the destination
3596efb9e8b8Srie 			 * and the caller provide symbols for relocation.
3597efb9e8b8Srie 			 * Parents are maintained as callers, but unless the
3598efb9e8b8Srie 			 * destination object was opened with RTLD_PARENT, the
3599efb9e8b8Srie 			 * parent doesn't provide symbols for the destination
3600efb9e8b8Srie 			 * to relocate against.
3601efb9e8b8Srie 			 */
3602cce0e03bSab 			for (ALIST_TRAVERSE(ghp2->gh_depends, idx3, gdp)) {
3603efb9e8b8Srie 				if (dlmp != gdp->gd_depend)
3604efb9e8b8Srie 					continue;
3605efb9e8b8Srie 
3606efb9e8b8Srie 				if (gdp->gd_flags & GPD_RELOC)
3607efb9e8b8Srie 					return (1);
3608efb9e8b8Srie 			}
36097c478bd9Sstevel@tonic-gate 		}
36107c478bd9Sstevel@tonic-gate 	}
36117c478bd9Sstevel@tonic-gate 	return (0);
36127c478bd9Sstevel@tonic-gate }
36137c478bd9Sstevel@tonic-gate 
36147c478bd9Sstevel@tonic-gate /*
36157c478bd9Sstevel@tonic-gate  * Initialize the environ symbol.  Traditionally this is carried out by the crt
36167c478bd9Sstevel@tonic-gate  * code prior to jumping to main.  However, init sections get fired before this
36177c478bd9Sstevel@tonic-gate  * variable is initialized, so ld.so.1 sets this directly from the AUX vector
36187c478bd9Sstevel@tonic-gate  * information.  In addition, a process may have multiple link-maps (ld.so.1's
36197c478bd9Sstevel@tonic-gate  * debugging and preloading objects), and link auditing, and each may need an
36207c478bd9Sstevel@tonic-gate  * environ variable set.
36217c478bd9Sstevel@tonic-gate  *
36227c478bd9Sstevel@tonic-gate  * This routine is called after a relocation() pass, and thus provides for:
36237c478bd9Sstevel@tonic-gate  *
36247c478bd9Sstevel@tonic-gate  *  o	setting environ on the main link-map after the initial application and
36257c478bd9Sstevel@tonic-gate  *	its dependencies have been established.  Typically environ lives in the
36267c478bd9Sstevel@tonic-gate  *	application (provided by its crt), but in older applications it might
36277c478bd9Sstevel@tonic-gate  *	be in libc.  Who knows what's expected of applications not built on
36287c478bd9Sstevel@tonic-gate  *	Solaris.
36297c478bd9Sstevel@tonic-gate  *
36307c478bd9Sstevel@tonic-gate  *  o	after loading a new shared object.  We can add shared objects to various
36317c478bd9Sstevel@tonic-gate  *	link-maps, and any link-map dependencies requiring getopt() require
36327c478bd9Sstevel@tonic-gate  *	their own environ.  In addition, lazy loading might bring in the
363341072f3cSrie  *	supplier of environ (libc used to be a lazy loading candidate) after
363441072f3cSrie  *	the link-map has been established and other objects are present.
36357c478bd9Sstevel@tonic-gate  *
36367c478bd9Sstevel@tonic-gate  * This routine handles all these scenarios, without adding unnecessary overhead
36377c478bd9Sstevel@tonic-gate  * to ld.so.1.
36387c478bd9Sstevel@tonic-gate  */
36397c478bd9Sstevel@tonic-gate void
36407c478bd9Sstevel@tonic-gate set_environ(Lm_list *lml)
36417c478bd9Sstevel@tonic-gate {
364275e7992aSrie 	Rt_map		*dlmp;
364375e7992aSrie 	Sym		*sym;
36447c478bd9Sstevel@tonic-gate 	Slookup		sl;
36457c478bd9Sstevel@tonic-gate 	uint_t		binfo;
36467c478bd9Sstevel@tonic-gate 
364775e7992aSrie 	/*
364875e7992aSrie 	 * Initialize the symbol lookup data structure.
364975e7992aSrie 	 */
365075e7992aSrie 	SLOOKUP_INIT(sl, MSG_ORIG(MSG_SYM_ENVIRON), lml->lm_head, lml->lm_head,
365175e7992aSrie 	    ld_entry_cnt, 0, 0, 0, 0, LKUP_WEAK);
36527c478bd9Sstevel@tonic-gate 
36539aa23310Srie 	if (sym = LM_LOOKUP_SYM(lml->lm_head)(&sl, &dlmp, &binfo, 0)) {
365441072f3cSrie 		lml->lm_environ = (char ***)sym->st_value;
36557c478bd9Sstevel@tonic-gate 
36567c478bd9Sstevel@tonic-gate 		if (!(FLAGS(dlmp) & FLG_RT_FIXED))
365741072f3cSrie 			lml->lm_environ =
365841072f3cSrie 			    (char ***)((uintptr_t)lml->lm_environ +
365941072f3cSrie 			    (uintptr_t)ADDR(dlmp));
366041072f3cSrie 		*(lml->lm_environ) = (char **)environ;
36617c478bd9Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_ENVIRON;
36627c478bd9Sstevel@tonic-gate 	}
36637c478bd9Sstevel@tonic-gate }
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate /*
36667c478bd9Sstevel@tonic-gate  * Determine whether we have a secure executable.  Uid and gid information
36677c478bd9Sstevel@tonic-gate  * can be passed to us via the aux vector, however if these values are -1
36687c478bd9Sstevel@tonic-gate  * then use the appropriate system call to obtain them.
36697c478bd9Sstevel@tonic-gate  *
36707c478bd9Sstevel@tonic-gate  *  o	If the user is the root they can do anything
36717c478bd9Sstevel@tonic-gate  *
36727c478bd9Sstevel@tonic-gate  *  o	If the real and effective uid's don't match, or the real and
36737c478bd9Sstevel@tonic-gate  *	effective gid's don't match then this is determined to be a `secure'
36747c478bd9Sstevel@tonic-gate  *	application.
36757c478bd9Sstevel@tonic-gate  *
36767c478bd9Sstevel@tonic-gate  * This function is called prior to any dependency processing (see _setup.c).
36777c478bd9Sstevel@tonic-gate  * Any secure setting will remain in effect for the life of the process.
36787c478bd9Sstevel@tonic-gate  */
36797c478bd9Sstevel@tonic-gate void
36807c478bd9Sstevel@tonic-gate security(uid_t uid, uid_t euid, gid_t gid, gid_t egid, int auxflags)
36817c478bd9Sstevel@tonic-gate {
36827c478bd9Sstevel@tonic-gate #ifdef AT_SUN_AUXFLAGS
36837c478bd9Sstevel@tonic-gate 	if (auxflags != -1) {
36847c478bd9Sstevel@tonic-gate 		if ((auxflags & AF_SUN_SETUGID) != 0)
36857c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
36867c478bd9Sstevel@tonic-gate 		return;
36877c478bd9Sstevel@tonic-gate 	}
36887c478bd9Sstevel@tonic-gate #endif
3689a953e2b1Srie 	if (uid == (uid_t)-1)
36907c478bd9Sstevel@tonic-gate 		uid = getuid();
36917c478bd9Sstevel@tonic-gate 	if (uid) {
3692a953e2b1Srie 		if (euid == (uid_t)-1)
36937c478bd9Sstevel@tonic-gate 			euid = geteuid();
36947c478bd9Sstevel@tonic-gate 		if (uid != euid)
36957c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
36967c478bd9Sstevel@tonic-gate 		else {
3697a953e2b1Srie 			if (gid == (gid_t)-1)
36987c478bd9Sstevel@tonic-gate 				gid = getgid();
3699a953e2b1Srie 			if (egid == (gid_t)-1)
37007c478bd9Sstevel@tonic-gate 				egid = getegid();
37017c478bd9Sstevel@tonic-gate 			if (gid != egid)
37027c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_SECURE;
37037c478bd9Sstevel@tonic-gate 		}
37047c478bd9Sstevel@tonic-gate 	}
37057c478bd9Sstevel@tonic-gate }
37067c478bd9Sstevel@tonic-gate 
3707*3dbfc803SRod Evans /*
3708*3dbfc803SRod Evans  * Determine whether ld.so.1 itself is owned by root and has its mode setuid.
3709*3dbfc803SRod Evans  */
3710*3dbfc803SRod Evans int
3711*3dbfc803SRod Evans is_rtld_setuid()
3712*3dbfc803SRod Evans {
3713*3dbfc803SRod Evans 	struct stat	status;
3714*3dbfc803SRod Evans 
3715*3dbfc803SRod Evans 	if ((rtld_flags2 & RT_FL2_SETUID) ||
3716*3dbfc803SRod Evans 	    ((stat(NAME(lml_rtld.lm_head), &status) == 0) &&
3717*3dbfc803SRod Evans 	    (status.st_uid == 0) && (status.st_mode & S_ISUID))) {
3718*3dbfc803SRod Evans 		rtld_flags2 |= RT_FL2_SETUID;
3719*3dbfc803SRod Evans 		return (1);
3720*3dbfc803SRod Evans 	}
3721*3dbfc803SRod Evans 	return (0);
3722*3dbfc803SRod Evans }
3723*3dbfc803SRod Evans 
37247c478bd9Sstevel@tonic-gate /*
37257c478bd9Sstevel@tonic-gate  * _REENTRANT code gets errno redefined to a function so provide for return
37267c478bd9Sstevel@tonic-gate  * of the thread errno if applicable.  This has no meaning in ld.so.1 which
37277c478bd9Sstevel@tonic-gate  * is basically singled threaded.  Provide the interface for our dependencies.
37287c478bd9Sstevel@tonic-gate  */
37297c478bd9Sstevel@tonic-gate #undef errno
37307c478bd9Sstevel@tonic-gate int *
37317c478bd9Sstevel@tonic-gate ___errno()
37327c478bd9Sstevel@tonic-gate {
37337c478bd9Sstevel@tonic-gate 	extern	int	errno;
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 	return (&errno);
37367c478bd9Sstevel@tonic-gate }
37377c478bd9Sstevel@tonic-gate 
37387c478bd9Sstevel@tonic-gate /*
37397c478bd9Sstevel@tonic-gate  * The interface with the c library which is supplied through libdl.so.1.
37407c478bd9Sstevel@tonic-gate  * A non-null argument allows a function pointer array to be passed to us which
37417c478bd9Sstevel@tonic-gate  * is used to re-initialize the linker libc table.
37427c478bd9Sstevel@tonic-gate  */
37437c478bd9Sstevel@tonic-gate void
37447c478bd9Sstevel@tonic-gate _ld_libc(void * ptr)
37457c478bd9Sstevel@tonic-gate {
37467c478bd9Sstevel@tonic-gate 	get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr);
37477c478bd9Sstevel@tonic-gate }
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate /*
37507c478bd9Sstevel@tonic-gate  * Determine whether a symbol name should be demangled.
37517c478bd9Sstevel@tonic-gate  */
37527c478bd9Sstevel@tonic-gate const char *
37537c478bd9Sstevel@tonic-gate demangle(const char *name)
37547c478bd9Sstevel@tonic-gate {
37557c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DEMANGLE)
37565aefb655Srie 		return (conv_demangle_name(name));
37577c478bd9Sstevel@tonic-gate 	else
37587c478bd9Sstevel@tonic-gate 		return (name);
37597c478bd9Sstevel@tonic-gate }
3760