xref: /illumos-gate/usr/src/cmd/sgs/rtld/common/util.c (revision 7247f888)
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 /*
237c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
247c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
257c478bd9Sstevel@tonic-gate  *
269a411307Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Utility routines for run-time linker.  some are duplicated here from libc
337c478bd9Sstevel@tonic-gate  * (with different names) to avoid name space collisions.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate #include	"_synonyms.h"
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>
487c478bd9Sstevel@tonic-gate #include	<sys/auxv.h>
495aefb655Srie #include	<debug.h>
505aefb655Srie #include	<conv.h>
517c478bd9Sstevel@tonic-gate #include	"_rtld.h"
527c478bd9Sstevel@tonic-gate #include	"_audit.h"
5310a4fa49Srie #include	"_elf.h"
547c478bd9Sstevel@tonic-gate #include	"msg.h"
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static int ld_flags_env(const char *, Word *, Word *, uint_t, int);
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * All error messages go through eprintf().  During process initialization these
607c478bd9Sstevel@tonic-gate  * messages should be directed to the standard error, however once control has
617c478bd9Sstevel@tonic-gate  * been passed to the applications code these messages should be stored in an
627c478bd9Sstevel@tonic-gate  * internal buffer for use with dlerror().  Note, fatal error conditions that
637c478bd9Sstevel@tonic-gate  * may occur while running the application will still cause a standard error
647c478bd9Sstevel@tonic-gate  * message, see rtldexit() in this file for details.
657c478bd9Sstevel@tonic-gate  * The `application' flag serves to indicate the transition between process
667c478bd9Sstevel@tonic-gate  * initialization and when the applications code is running.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate  * Null function used as place where a debugger can set a breakpoint.
717c478bd9Sstevel@tonic-gate  */
727c478bd9Sstevel@tonic-gate void
735aefb655Srie rtld_db_dlactivity(Lm_list *lml)
747c478bd9Sstevel@tonic-gate {
755aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
765aefb655Srie 	    r_debug.rtd_rdebug.r_state));
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * Null function used as place where debugger can set a pre .init
817c478bd9Sstevel@tonic-gate  * processing breakpoint.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate void
845aefb655Srie rtld_db_preinit(Lm_list *lml)
857c478bd9Sstevel@tonic-gate {
865aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
875aefb655Srie 	    r_debug.rtd_rdebug.r_state));
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * Null function used as place where debugger can set a post .init
927c478bd9Sstevel@tonic-gate  * processing breakpoint.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate void
955aefb655Srie rtld_db_postinit(Lm_list *lml)
967c478bd9Sstevel@tonic-gate {
975aefb655Srie 	DBG_CALL(Dbg_util_dbnotify(lml, r_debug.rtd_rdebug.r_rdevent,
985aefb655Srie 	    r_debug.rtd_rdebug.r_state));
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate /*
1027c478bd9Sstevel@tonic-gate  * Debugger Event Notification
1037c478bd9Sstevel@tonic-gate  *
1047c478bd9Sstevel@tonic-gate  * This function centralizes all debugger event notification (ala rtld_db).
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  * There's a simple intent, focused on insuring the primary link-map control
1077c478bd9Sstevel@tonic-gate  * list (or each link-map list) is consistent, and the indication that objects
1087c478bd9Sstevel@tonic-gate  * have been added or deleted from this list.  Although an RD_ADD and RD_DELETE
1097c478bd9Sstevel@tonic-gate  * event are posted for each of these, most debuggers don't care, as their
1107c478bd9Sstevel@tonic-gate  * view is that these events simply convey an "inconsistent" state.
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * We also don't want to trigger multiple RD_ADD/RD_DELETE events any time we
1137c478bd9Sstevel@tonic-gate  * enter ld.so.1.
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * With auditors, we may be in the process of relocating a collection of
1167c478bd9Sstevel@tonic-gate  * objects, and will leave() ld.so.1 to call the auditor.  At this point we
1177c478bd9Sstevel@tonic-gate  * must indicate an RD_CONSISTENT event, but librtld_db will not report an
1187c478bd9Sstevel@tonic-gate  * object to the debuggers until relocation processing has been completed on it.
1197c478bd9Sstevel@tonic-gate  * To allow for the collection of these objects that are pending relocation, an
1207c478bd9Sstevel@tonic-gate  * RD_ADD event is set after completing a series of relocations on the primary
1217c478bd9Sstevel@tonic-gate  * link-map control list.
1227c478bd9Sstevel@tonic-gate  *
1237c478bd9Sstevel@tonic-gate  * Set an RD_ADD/RD_DELETE event and indicate that an RD_CONSISTENT event is
1247c478bd9Sstevel@tonic-gate  * required later (LML_FLG_DBNOTIF):
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  *  i	the first time we add or delete an object to the primary link-map
1277c478bd9Sstevel@tonic-gate  *	control list.
1287c478bd9Sstevel@tonic-gate  *  ii	the first time we move a secondary link-map control list to the primary
1297c478bd9Sstevel@tonic-gate  *	link-map control list (effectively, this is like adding a group of
1307c478bd9Sstevel@tonic-gate  *	objects to the primary link-map control list).
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * Set an RD_CONSISTENT event when it is required (LML_FLG_DBNOTIF is set) and
1337c478bd9Sstevel@tonic-gate  *
1347c478bd9Sstevel@tonic-gate  *  i	each time we leave the runtime linker.
1357c478bd9Sstevel@tonic-gate  */
1367c478bd9Sstevel@tonic-gate void
1377c478bd9Sstevel@tonic-gate rd_event(Lm_list *lml, rd_event_e event, r_state_e state)
1387c478bd9Sstevel@tonic-gate {
13910a4fa49Srie 	void	(*fptr)(Lm_list *);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	switch (event) {
1427c478bd9Sstevel@tonic-gate 	case RD_PREINIT:
1437c478bd9Sstevel@tonic-gate 		fptr = rtld_db_preinit;
1447c478bd9Sstevel@tonic-gate 		break;
1457c478bd9Sstevel@tonic-gate 	case RD_POSTINIT:
1467c478bd9Sstevel@tonic-gate 		fptr = rtld_db_postinit;
1477c478bd9Sstevel@tonic-gate 		break;
1487c478bd9Sstevel@tonic-gate 	case RD_DLACTIVITY:
1497c478bd9Sstevel@tonic-gate 		switch (state) {
1507c478bd9Sstevel@tonic-gate 		case RT_CONSISTENT:
1517c478bd9Sstevel@tonic-gate 			lml->lm_flags &= ~LML_FLG_DBNOTIF;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 			/*
1547c478bd9Sstevel@tonic-gate 			 * Do we need to send a notification?
1557c478bd9Sstevel@tonic-gate 			 */
1567c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_DBNOTIF) == 0)
1577c478bd9Sstevel@tonic-gate 				return;
1587c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_DBNOTIF;
1597c478bd9Sstevel@tonic-gate 			break;
1607c478bd9Sstevel@tonic-gate 		case RT_ADD:
1617c478bd9Sstevel@tonic-gate 		case RT_DELETE:
1627c478bd9Sstevel@tonic-gate 			lml->lm_flags |= LML_FLG_DBNOTIF;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 			/*
1657c478bd9Sstevel@tonic-gate 			 * If we are already in an inconsistent state, no
1667c478bd9Sstevel@tonic-gate 			 * notification is required.
1677c478bd9Sstevel@tonic-gate 			 */
1687c478bd9Sstevel@tonic-gate 			if (rtld_flags & RT_FL_DBNOTIF)
1697c478bd9Sstevel@tonic-gate 				return;
1707c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_DBNOTIF;
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 		};
1737c478bd9Sstevel@tonic-gate 		fptr = rtld_db_dlactivity;
1747c478bd9Sstevel@tonic-gate 		break;
1757c478bd9Sstevel@tonic-gate 	default:
1767c478bd9Sstevel@tonic-gate 		/*
1777c478bd9Sstevel@tonic-gate 		 * RD_NONE - do nothing
1787c478bd9Sstevel@tonic-gate 		 */
1797c478bd9Sstevel@tonic-gate 		break;
1807c478bd9Sstevel@tonic-gate 	};
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	/*
1837c478bd9Sstevel@tonic-gate 	 * Set event state and call 'notification' function.
1847c478bd9Sstevel@tonic-gate 	 *
1857c478bd9Sstevel@tonic-gate 	 * The debugging clients have previously been told about these
1867c478bd9Sstevel@tonic-gate 	 * notification functions and have set breakpoints on them if they
1877c478bd9Sstevel@tonic-gate 	 * are interested in the notification.
1887c478bd9Sstevel@tonic-gate 	 */
1897c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_state = state;
1907c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = event;
1915aefb655Srie 	fptr(lml);
1927c478bd9Sstevel@tonic-gate 	r_debug.rtd_rdebug.r_rdevent = RD_NONE;
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
19502ca3e02Srie #if	defined(__sparc) || defined(__x86)
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * Stack Cleanup.
1987c478bd9Sstevel@tonic-gate  *
1997c478bd9Sstevel@tonic-gate  * This function is invoked to 'remove' arguments that were passed in on the
2007c478bd9Sstevel@tonic-gate  * stack.  This is most likely if ld.so.1 was invoked directly.  In that case
2017c478bd9Sstevel@tonic-gate  * we want to remove ld.so.1 as well as it's arguments from the argv[] array.
2027c478bd9Sstevel@tonic-gate  * Which means we then need to slide everything above it on the stack down
2037c478bd9Sstevel@tonic-gate  * accordingly.
2047c478bd9Sstevel@tonic-gate  *
20502ca3e02Srie  * While the stack layout is platform specific - it just so happens that __x86,
20602ca3e02Srie  * and __sparc platforms share the following initial stack layout.
2077c478bd9Sstevel@tonic-gate  *
2087c478bd9Sstevel@tonic-gate  *	!_______________________!  high addresses
2097c478bd9Sstevel@tonic-gate  *	!			!
2107c478bd9Sstevel@tonic-gate  *	!	Information	!
2117c478bd9Sstevel@tonic-gate  *	!	Block		!
2127c478bd9Sstevel@tonic-gate  *	!	(size varies)	!
2137c478bd9Sstevel@tonic-gate  *	!_______________________!
2147c478bd9Sstevel@tonic-gate  *	!	0 word		!
2157c478bd9Sstevel@tonic-gate  *	!_______________________!
2167c478bd9Sstevel@tonic-gate  *	!	Auxiliary	!
2177c478bd9Sstevel@tonic-gate  *	!	vector		!
2187c478bd9Sstevel@tonic-gate  *	!	2 word entries	!
2197c478bd9Sstevel@tonic-gate  *	!			!
2207c478bd9Sstevel@tonic-gate  *	!_______________________!
2217c478bd9Sstevel@tonic-gate  *	!	0 word		!
2227c478bd9Sstevel@tonic-gate  *	!_______________________!
2237c478bd9Sstevel@tonic-gate  *	!	Environment	!
2247c478bd9Sstevel@tonic-gate  *	!	pointers	!
2257c478bd9Sstevel@tonic-gate  *	!	...		!
2267c478bd9Sstevel@tonic-gate  *	!	(one word each)	!
2277c478bd9Sstevel@tonic-gate  *	!_______________________!
2287c478bd9Sstevel@tonic-gate  *	!	0 word		!
2297c478bd9Sstevel@tonic-gate  *	!_______________________!
2307c478bd9Sstevel@tonic-gate  *	!	Argument	! low addresses
2317c478bd9Sstevel@tonic-gate  *	!	pointers	!
2327c478bd9Sstevel@tonic-gate  *	!	Argc words	!
2337c478bd9Sstevel@tonic-gate  *	!_______________________!
2347c478bd9Sstevel@tonic-gate  *	!			!
2357c478bd9Sstevel@tonic-gate  *	!	Argc		!
2367c478bd9Sstevel@tonic-gate  *	!_______________________!
2377c478bd9Sstevel@tonic-gate  *	!	...		!
2387c478bd9Sstevel@tonic-gate  *
2397c478bd9Sstevel@tonic-gate  */
2407c478bd9Sstevel@tonic-gate static void
24141072f3cSrie stack_cleanup(char **argv, char ***envp, auxv_t **auxv, int rmcnt)
2427c478bd9Sstevel@tonic-gate {
24341072f3cSrie 	int		ndx;
2447c478bd9Sstevel@tonic-gate 	long		*argc;
24541072f3cSrie 	char		**oargv, **nargv;
24641072f3cSrie 	char		**oenvp, **nenvp;
24741072f3cSrie 	auxv_t		*oauxv, *nauxv;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	/*
25041072f3cSrie 	 * Slide ARGV[] and update argc.  The argv pointer remains the same,
25141072f3cSrie 	 * however slide the applications arguments over the arguments to
25241072f3cSrie 	 * ld.so.1.
2537c478bd9Sstevel@tonic-gate 	 */
25441072f3cSrie 	nargv = &argv[0];
25541072f3cSrie 	oargv = &argv[rmcnt];
25641072f3cSrie 
25741072f3cSrie 	for (ndx = 0; oargv[ndx]; ndx++)
25841072f3cSrie 		nargv[ndx] = oargv[ndx];
25941072f3cSrie 	nargv[ndx] = oargv[ndx];
26041072f3cSrie 
26141072f3cSrie 	argc = (long *)((uintptr_t)argv - sizeof (long *));
2627c478bd9Sstevel@tonic-gate 	*argc -= rmcnt;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
26541072f3cSrie 	 * Slide ENVP[], and update the environment array pointer.
2667c478bd9Sstevel@tonic-gate 	 */
26741072f3cSrie 	ndx++;
26841072f3cSrie 	nenvp = &nargv[ndx];
26941072f3cSrie 	oenvp = &oargv[ndx];
27041072f3cSrie 	*envp = nenvp;
27141072f3cSrie 
27241072f3cSrie 	for (ndx = 0; oenvp[ndx]; ndx++)
27341072f3cSrie 		nenvp[ndx] = oenvp[ndx];
27441072f3cSrie 	nenvp[ndx] = oenvp[ndx];
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/*
27741072f3cSrie 	 * Slide AUXV[], and update the aux vector pointer.
2787c478bd9Sstevel@tonic-gate 	 */
27941072f3cSrie 	ndx++;
28041072f3cSrie 	nauxv = (auxv_t *)&nenvp[ndx];
28141072f3cSrie 	oauxv = (auxv_t *)&oenvp[ndx];
28241072f3cSrie 	*auxv = nauxv;
28341072f3cSrie 
28441072f3cSrie 	for (ndx = 0; (oauxv[ndx].a_type != AT_NULL); ndx++)
28541072f3cSrie 		nauxv[ndx] = oauxv[ndx];
28641072f3cSrie 	nauxv[ndx] = oauxv[ndx];
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate #else
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Verify that the above routine is appropriate for any new platforms.
2917c478bd9Sstevel@tonic-gate  */
2927c478bd9Sstevel@tonic-gate #error	unsupported architecture!
2937c478bd9Sstevel@tonic-gate #endif
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate /*
29641072f3cSrie  * The only command line argument recognized is -e, followed by a runtime
29741072f3cSrie  * linker environment variable.
2987c478bd9Sstevel@tonic-gate  */
2997c478bd9Sstevel@tonic-gate int
30041072f3cSrie rtld_getopt(char **argv, char ***envp, auxv_t **auxv, Word *lmflags,
30141072f3cSrie     Word *lmtflags, int aout)
3027c478bd9Sstevel@tonic-gate {
30341072f3cSrie 	int	ndx;
3047c478bd9Sstevel@tonic-gate 
30541072f3cSrie 	for (ndx = 1; argv[ndx]; ndx++) {
3067c478bd9Sstevel@tonic-gate 		char	*str;
3077c478bd9Sstevel@tonic-gate 
30841072f3cSrie 		if (argv[ndx][0] != '-')
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 
31141072f3cSrie 		if (argv[ndx][1] == '\0') {
31241072f3cSrie 			ndx++;
3137c478bd9Sstevel@tonic-gate 			break;
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 
31641072f3cSrie 		if (argv[ndx][1] != 'e')
31741072f3cSrie 			return (1);
3187c478bd9Sstevel@tonic-gate 
31941072f3cSrie 		if (argv[ndx][2] == '\0') {
32041072f3cSrie 			ndx++;
32141072f3cSrie 			if (argv[ndx] == NULL)
32241072f3cSrie 				return (1);
32341072f3cSrie 			str = argv[ndx];
3247c478bd9Sstevel@tonic-gate 		} else
32541072f3cSrie 			str = &argv[ndx][2];
3267c478bd9Sstevel@tonic-gate 
32741072f3cSrie 		/*
32841072f3cSrie 		 * If the environment variable starts with LD_, strip the LD_.
32941072f3cSrie 		 * Otherwise, take things as is.
33041072f3cSrie 		 */
33141072f3cSrie 		if ((str[0] == 'L') && (str[1] == 'D') && (str[2] == '_') &&
33241072f3cSrie 		    (str[3] != '\0'))
33341072f3cSrie 			str += 3;
3347c478bd9Sstevel@tonic-gate 		if (ld_flags_env(str, lmflags, lmtflags, 0, aout) == 1)
3357c478bd9Sstevel@tonic-gate 			return (1);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
33841072f3cSrie 	/*
33941072f3cSrie 	 * Make sure an object file has been specified.
34041072f3cSrie 	 */
34141072f3cSrie 	if (argv[ndx] == 0)
3427c478bd9Sstevel@tonic-gate 		return (1);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * Having gotten the arguments, clean ourselves off of the stack.
3467c478bd9Sstevel@tonic-gate 	 */
34741072f3cSrie 	stack_cleanup(argv, envp, auxv, ndx);
3487c478bd9Sstevel@tonic-gate 	return (0);
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * Compare function for FullpathNode AVL tree.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate static int
3557c478bd9Sstevel@tonic-gate fpavl_compare(const void * n1, const void * n2)
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 	uint_t		hash1, hash2;
3587c478bd9Sstevel@tonic-gate 	const char	*st1, *st2;
3597c478bd9Sstevel@tonic-gate 	int		rc;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	hash1 = ((FullpathNode *)n1)->fpn_hash;
3627c478bd9Sstevel@tonic-gate 	hash2 = ((FullpathNode *)n2)->fpn_hash;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if (hash1 > hash2)
3657c478bd9Sstevel@tonic-gate 		return (1);
3667c478bd9Sstevel@tonic-gate 	if (hash1 < hash2)
3677c478bd9Sstevel@tonic-gate 		return (-1);
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	st1 = ((FullpathNode *)n1)->fpn_name;
3707c478bd9Sstevel@tonic-gate 	st2 = ((FullpathNode *)n2)->fpn_name;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	rc = strcmp(st1, st2);
3737c478bd9Sstevel@tonic-gate 	if (rc > 0)
3747c478bd9Sstevel@tonic-gate 		return (1);
3757c478bd9Sstevel@tonic-gate 	if (rc < 0)
3767c478bd9Sstevel@tonic-gate 		return (-1);
3777c478bd9Sstevel@tonic-gate 	return (0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*
38224a6229eSrie  * Determine if a given pathname has already been loaded in the AVL tree.
3837c478bd9Sstevel@tonic-gate  * If the pathname does not exist in the AVL tree, the next insertion point
3847c478bd9Sstevel@tonic-gate  * is deposited in "where".  This value can be used by fpavl_insert() to
3857c478bd9Sstevel@tonic-gate  * expedite the insertion.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate Rt_map *
3887c478bd9Sstevel@tonic-gate fpavl_loaded(Lm_list *lml, const char *name, avl_index_t *where)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	FullpathNode	fpn, *fpnp;
3917c478bd9Sstevel@tonic-gate 	avl_tree_t	*avlt;
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * Create the avl tree if required.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	if ((avlt = lml->lm_fpavl) == NULL) {
3977c478bd9Sstevel@tonic-gate 		if ((avlt = calloc(sizeof (avl_tree_t), 1)) == 0)
3987c478bd9Sstevel@tonic-gate 			return (0);
3997c478bd9Sstevel@tonic-gate 		avl_create(avlt, fpavl_compare, sizeof (FullpathNode),
4007c478bd9Sstevel@tonic-gate 		    SGSOFFSETOF(FullpathNode, fpn_avl));
4017c478bd9Sstevel@tonic-gate 		lml->lm_fpavl = avlt;
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	fpn.fpn_name = name;
4057c478bd9Sstevel@tonic-gate 	fpn.fpn_hash = sgs_str_hash(name);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if ((fpnp = avl_find(lml->lm_fpavl, &fpn, where)) == NULL)
4087c478bd9Sstevel@tonic-gate 		return (NULL);
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	return (fpnp->fpn_lmp);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
41524a6229eSrie  * Insert a name into the FullpathNode AVL tree for the link-map list.  The
41624a6229eSrie  * objects NAME() is the path that would have originally been searched for, and
41724a6229eSrie  * is therefore the name to associate with any "where" value.  If the object has
4187c478bd9Sstevel@tonic-gate  * a different PATHNAME(), perhaps because it has resolved to a different file
4197c478bd9Sstevel@tonic-gate  * (see fullpath), then this name is recorded also.  See load_file().
4207c478bd9Sstevel@tonic-gate  */
4217c478bd9Sstevel@tonic-gate int
4227c478bd9Sstevel@tonic-gate fpavl_insert(Lm_list *lml, Rt_map *lmp, const char *name, avl_index_t where)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	FullpathNode	*fpnp;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 	if (where == 0) {
4277c478bd9Sstevel@tonic-gate 		/* LINTED */
4287c478bd9Sstevel@tonic-gate 		Rt_map	*_lmp = fpavl_loaded(lml, name, &where);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		/*
4317c478bd9Sstevel@tonic-gate 		 * We better not get a hit now, we do not want duplicates in
4327c478bd9Sstevel@tonic-gate 		 * the tree.
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate 		ASSERT(_lmp == 0);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/*
4387c478bd9Sstevel@tonic-gate 	 * Insert new node in tree
4397c478bd9Sstevel@tonic-gate 	 */
4407c478bd9Sstevel@tonic-gate 	if ((fpnp = calloc(sizeof (FullpathNode), 1)) == 0)
4417c478bd9Sstevel@tonic-gate 		return (0);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	fpnp->fpn_name = name;
4447c478bd9Sstevel@tonic-gate 	fpnp->fpn_hash = sgs_str_hash(name);
4457c478bd9Sstevel@tonic-gate 	fpnp->fpn_lmp = lmp;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (alist_append(&FPNODE(lmp), &fpnp, sizeof (FullpathNode *),
4487c478bd9Sstevel@tonic-gate 	    AL_CNT_FPNODE) == 0) {
4497c478bd9Sstevel@tonic-gate 		free(fpnp);
4507c478bd9Sstevel@tonic-gate 		return (0);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	ASSERT(lml->lm_fpavl != NULL);
4547c478bd9Sstevel@tonic-gate 	avl_insert(lml->lm_fpavl, fpnp, where);
4557c478bd9Sstevel@tonic-gate 	return (1);
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /*
45924a6229eSrie  * Remove an object from the Fullpath AVL tree.  Note, this is called *before*
4607c478bd9Sstevel@tonic-gate  * the objects link-map is torn down (remove_so), which is where any NAME() and
4617c478bd9Sstevel@tonic-gate  * PATHNAME() strings will be deallocated.
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate void
4647c478bd9Sstevel@tonic-gate fpavl_remove(Rt_map *lmp)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	FullpathNode	**fpnpp;
4677c478bd9Sstevel@tonic-gate 	Aliste		off;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(FPNODE(lmp), off, fpnpp)) {
4707c478bd9Sstevel@tonic-gate 		FullpathNode	*fpnp = *fpnpp;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		avl_remove(LIST(lmp)->lm_fpavl, fpnp);
4737c478bd9Sstevel@tonic-gate 		free(fpnp);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 	free(FPNODE(lmp));
4767c478bd9Sstevel@tonic-gate 	FPNODE(lmp) = 0;
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Prior to calling an object, either via a .plt or through dlsym(), make sure
4827c478bd9Sstevel@tonic-gate  * its .init has fired.  Through topological sorting, ld.so.1 attempts to fire
4837c478bd9Sstevel@tonic-gate  * init's in the correct order, however, this order is typically based on needed
4847c478bd9Sstevel@tonic-gate  * dependencies and non-lazy relocation bindings.  Lazy relocations (.plts) can
4857c478bd9Sstevel@tonic-gate  * still occur and result in bindings that were not captured during topological
4867c478bd9Sstevel@tonic-gate  * sorting.  This routine compensates for this lack of binding information, and
4877c478bd9Sstevel@tonic-gate  * provides for dynamic .init firing.
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate void
4907c478bd9Sstevel@tonic-gate is_dep_init(Rt_map * dlmp, Rt_map * clmp)
4917c478bd9Sstevel@tonic-gate {
4927c478bd9Sstevel@tonic-gate 	Rt_map **	tobj;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/*
4957c478bd9Sstevel@tonic-gate 	 * If the caller is an auditor, and the destination isn't, then don't
4967c478bd9Sstevel@tonic-gate 	 * run any .inits (see comments in load_completion()).
4977c478bd9Sstevel@tonic-gate 	 */
4987c478bd9Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
4997c478bd9Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
5007c478bd9Sstevel@tonic-gate 		return;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if ((dlmp == clmp) || (rtld_flags & (RT_FL_BREADTH | RT_FL_INITFIRST)))
5037c478bd9Sstevel@tonic-gate 		return;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITDONE)) ==
5067c478bd9Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITDONE))
5077c478bd9Sstevel@tonic-gate 		return;
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & (FLG_RT_RELOCED | FLG_RT_INITCALL)) ==
5107c478bd9Sstevel@tonic-gate 	    (FLG_RT_RELOCED | FLG_RT_INITCALL)) {
5115aefb655Srie 		DBG_CALL(Dbg_util_no_init(dlmp));
5127c478bd9Sstevel@tonic-gate 		return;
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if ((tobj = calloc(2, sizeof (Rt_map *))) != NULL) {
5167c478bd9Sstevel@tonic-gate 		tobj[0] = dlmp;
5177c478bd9Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_DYN);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * In a threaded environment insure the thread responsible for loading an object
5237c478bd9Sstevel@tonic-gate  * has completed .init processing for that object before any new thread is
5247c478bd9Sstevel@tonic-gate  * allowed to access the object.  This check is only valid with libthread
5257c478bd9Sstevel@tonic-gate  * TI_VERSION 2, where ld.so.1 implements locking through low level mutexes.
5267c478bd9Sstevel@tonic-gate  *
5277c478bd9Sstevel@tonic-gate  * When a new link-map is created, the thread that causes it to be loaded is
5287c478bd9Sstevel@tonic-gate  * identified by THREADID(dlmp).  Compare this with the current thread to
5297c478bd9Sstevel@tonic-gate  * determine if it must be blocked.
5307c478bd9Sstevel@tonic-gate  *
5317c478bd9Sstevel@tonic-gate  * NOTE, there are a number of instances (typically only for .plt processing)
5327c478bd9Sstevel@tonic-gate  * where we must skip this test:
5337c478bd9Sstevel@tonic-gate  *
5347c478bd9Sstevel@tonic-gate  *   .	any thread id of 0 - threads that call thr_exit() may be in this state
5357c478bd9Sstevel@tonic-gate  *	thus we can't deduce what tid they used to be.  Also some of the
5367c478bd9Sstevel@tonic-gate  *	lib/libthread worker threads have this id and must bind (to themselves
5377c478bd9Sstevel@tonic-gate  *	or libc) for libthread to function.
5387c478bd9Sstevel@tonic-gate  *
5397c478bd9Sstevel@tonic-gate  *   .	libthread itself binds to libc, and as libthread is INITFIRST
5407c478bd9Sstevel@tonic-gate  *	libc's .init can't have fired yet.  Luckly libc's .init is not required
5417c478bd9Sstevel@tonic-gate  *	by libthreads binding.
5427c478bd9Sstevel@tonic-gate  *
5437c478bd9Sstevel@tonic-gate  *   .	if the caller is an auditor, and the destination isn't, then don't
5447c478bd9Sstevel@tonic-gate  *	block (see comments in load_completion()).
5457c478bd9Sstevel@tonic-gate  */
5467c478bd9Sstevel@tonic-gate void
5477c478bd9Sstevel@tonic-gate is_dep_ready(Rt_map * dlmp, Rt_map * clmp, int what)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	thread_t	tid;
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	if ((LIST(clmp)->lm_flags & LML_FLG_NOAUDIT) &&
5527c478bd9Sstevel@tonic-gate 	    (LIST(clmp) != LIST(dlmp)))
5537c478bd9Sstevel@tonic-gate 		return;
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_CONCUR) &&
5567c478bd9Sstevel@tonic-gate 	    ((FLAGS(dlmp) & FLG_RT_INITDONE) == 0) &&
5577c478bd9Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_INITFRST) == 0) &&
5587c478bd9Sstevel@tonic-gate 	    ((tid = rt_thr_self()) != 0) && (THREADID(dlmp) != tid)) {
5597c478bd9Sstevel@tonic-gate 		while ((FLAGS(dlmp) & FLG_RT_INITDONE) == 0) {
5607c478bd9Sstevel@tonic-gate 			FLAGS1(dlmp) |= FL1_RT_INITWAIT;
5615aefb655Srie 			DBG_CALL(Dbg_util_wait(clmp, dlmp, what));
5627c478bd9Sstevel@tonic-gate 			(void) rt_cond_wait(CONDVAR(dlmp), &rtldlock);
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate }
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate /*
5687c478bd9Sstevel@tonic-gate  * Execute .{preinit|init|fini}array sections
5697c478bd9Sstevel@tonic-gate  */
5707c478bd9Sstevel@tonic-gate void
5715aefb655Srie call_array(Addr *array, uint_t arraysz, Rt_map *lmp, Word shtype)
5727c478bd9Sstevel@tonic-gate {
5735aefb655Srie 	int	start, stop, incr, ndx;
5747c478bd9Sstevel@tonic-gate 	uint_t	arraycnt = (uint_t)(arraysz / sizeof (Addr));
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	if (array == NULL)
5777c478bd9Sstevel@tonic-gate 		return;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * initarray & preinitarray are walked from beginning to end - while
5817c478bd9Sstevel@tonic-gate 	 * finiarray is walked from end to beginning.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	if (shtype == SHT_FINI_ARRAY) {
5847c478bd9Sstevel@tonic-gate 		start = arraycnt - 1;
5857c478bd9Sstevel@tonic-gate 		stop = incr = -1;
5867c478bd9Sstevel@tonic-gate 	} else {
5877c478bd9Sstevel@tonic-gate 		start = 0;
5887c478bd9Sstevel@tonic-gate 		stop = arraycnt;
5897c478bd9Sstevel@tonic-gate 		incr = 1;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	/*
5937c478bd9Sstevel@tonic-gate 	 * Call the .*array[] entries
5947c478bd9Sstevel@tonic-gate 	 */
5955aefb655Srie 	for (ndx = start; ndx != stop; ndx += incr) {
59610a4fa49Srie 		void (*fptr)(void) = (void(*)())array[ndx];
5977c478bd9Sstevel@tonic-gate 
5985aefb655Srie 		DBG_CALL(Dbg_util_call_array(lmp, (void *)fptr, ndx, shtype));
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 		leave(LIST(lmp));
6017c478bd9Sstevel@tonic-gate 		(*fptr)();
6027c478bd9Sstevel@tonic-gate 		(void) enter();
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate }
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Execute any .init sections.  These are passed to us in an lmp array which
6097c478bd9Sstevel@tonic-gate  * (by default) will have been sorted.
6107c478bd9Sstevel@tonic-gate  */
6117c478bd9Sstevel@tonic-gate void
6127c478bd9Sstevel@tonic-gate call_init(Rt_map ** tobj, int flag)
6137c478bd9Sstevel@tonic-gate {
6147c478bd9Sstevel@tonic-gate 	Rt_map **	_tobj, ** _nobj;
6157c478bd9Sstevel@tonic-gate 	static List	pending = { NULL, NULL };
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 	/*
6187c478bd9Sstevel@tonic-gate 	 * If we're in the middle of an INITFIRST, this must complete before
6197c478bd9Sstevel@tonic-gate 	 * any new init's are fired.  In this case add the object list to the
6207c478bd9Sstevel@tonic-gate 	 * pending queue and return.  We'll pick up the queue after any
6217c478bd9Sstevel@tonic-gate 	 * INITFIRST objects have their init's fired.
6227c478bd9Sstevel@tonic-gate 	 */
6237c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_INITFIRST) {
6247c478bd9Sstevel@tonic-gate 		(void) list_append(&pending, tobj);
6257c478bd9Sstevel@tonic-gate 		return;
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * Traverse the tobj array firing each objects init.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	for (_tobj = _nobj = tobj, _nobj++; *_tobj != NULL; _tobj++, _nobj++) {
6327c478bd9Sstevel@tonic-gate 		Rt_map *	lmp = *_tobj;
633dffec89cSrie 		void (*		iptr)() = INIT(lmp);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITCALL)
6367c478bd9Sstevel@tonic-gate 			continue;
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITCALL;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		/*
6417c478bd9Sstevel@tonic-gate 		 * Establish an initfirst state if necessary - no other inits
642dffec89cSrie 		 * will be fired (because of additional relocation bindings)
643dffec89cSrie 		 * when in this state.
6447c478bd9Sstevel@tonic-gate 		 */
6457c478bd9Sstevel@tonic-gate 		if (FLAGS(lmp) & FLG_RT_INITFRST)
6467c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_INITFIRST;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr) {
6497c478bd9Sstevel@tonic-gate 			Aliste		off;
6507c478bd9Sstevel@tonic-gate 			Bnd_desc **	bdpp;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 			/*
6537c478bd9Sstevel@tonic-gate 			 * Make sure that all dependencies that have been
6547c478bd9Sstevel@tonic-gate 			 * relocated to are initialized before this objects
6557c478bd9Sstevel@tonic-gate 			 * .init is executed.  This insures that a dependency
6567c478bd9Sstevel@tonic-gate 			 * on an external item that must first be initialized
6577c478bd9Sstevel@tonic-gate 			 * by its associated object is satisfied.
6587c478bd9Sstevel@tonic-gate 			 */
6597c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(DEPENDS(lmp), off, bdpp)) {
6607c478bd9Sstevel@tonic-gate 				Bnd_desc *	bdp = *bdpp;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 				if ((bdp->b_flags & BND_REFER) == 0)
6637c478bd9Sstevel@tonic-gate 					continue;
6647c478bd9Sstevel@tonic-gate 				is_dep_ready(bdp->b_depend, lmp, DBG_WAIT_INIT);
6657c478bd9Sstevel@tonic-gate 			}
6665aefb655Srie 			DBG_CALL(Dbg_util_call_init(lmp, flag));
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 		if (iptr) {
6707c478bd9Sstevel@tonic-gate 			leave(LIST(lmp));
6717c478bd9Sstevel@tonic-gate 			(*iptr)();
6727c478bd9Sstevel@tonic-gate 			(void) enter();
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate 		call_array(INITARRAY(lmp), INITARRAYSZ(lmp), lmp,
6767c478bd9Sstevel@tonic-gate 		    SHT_INIT_ARRAY);
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		if (INITARRAY(lmp) || iptr)
6795aefb655Srie 			DBG_CALL(Dbg_util_call_init(lmp, DBG_INIT_DONE));
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 		/*
6827c478bd9Sstevel@tonic-gate 		 * Set the initdone flag regardless of whether this object
6837c478bd9Sstevel@tonic-gate 		 * actually contains an .init section.  This flag prevents us
6847c478bd9Sstevel@tonic-gate 		 * from processing this section again for an .init and also
6857c478bd9Sstevel@tonic-gate 		 * signifies that a .fini must be called should it exist.
6867c478bd9Sstevel@tonic-gate 		 * Clear the sort field for use in later .fini processing.
6877c478bd9Sstevel@tonic-gate 		 */
6887c478bd9Sstevel@tonic-gate 		FLAGS(lmp) |= FLG_RT_INITDONE;
689dffec89cSrie 		SORTVAL(lmp) = -1;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 		/*
6927c478bd9Sstevel@tonic-gate 		 * Wake anyone up who might be waiting on this .init.
6937c478bd9Sstevel@tonic-gate 		 */
6947c478bd9Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_INITWAIT) {
6955aefb655Srie 			DBG_CALL(Dbg_util_broadcast(lmp));
6967c478bd9Sstevel@tonic-gate 			(void) rt_cond_broadcast(CONDVAR(lmp));
6977c478bd9Sstevel@tonic-gate 			FLAGS1(lmp) &= ~FL1_RT_INITWAIT;
6987c478bd9Sstevel@tonic-gate 		}
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 		/*
7017c478bd9Sstevel@tonic-gate 		 * If we're firing an INITFIRST object, and other objects must
7027c478bd9Sstevel@tonic-gate 		 * be fired which are not INITFIRST, make sure we grab any
7037c478bd9Sstevel@tonic-gate 		 * pending objects that might have been delayed as this
7047c478bd9Sstevel@tonic-gate 		 * INITFIRST was processed.
7057c478bd9Sstevel@tonic-gate 		 */
7067c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_INITFIRST) &&
7077c478bd9Sstevel@tonic-gate 		    ((*_nobj == NULL) || !(FLAGS(*_nobj) & FLG_RT_INITFRST))) {
7087c478bd9Sstevel@tonic-gate 			Listnode *	lnp;
7097c478bd9Sstevel@tonic-gate 			Rt_map **	pobj;
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 			rtld_flags &= ~RT_FL_INITFIRST;
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 			while ((lnp = pending.head) != NULL) {
7147c478bd9Sstevel@tonic-gate 				if ((pending.head = lnp->next) == NULL)
7157c478bd9Sstevel@tonic-gate 					pending.tail = NULL;
7167c478bd9Sstevel@tonic-gate 				pobj = lnp->data;
7177c478bd9Sstevel@tonic-gate 				free(lnp);
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 				call_init(pobj, DBG_INIT_PEND);
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 	free(tobj);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate  * Function called by atexit(3C).  Calls all .fini sections related with the
7287c478bd9Sstevel@tonic-gate  * mains dependent shared libraries in the order in which the shared libraries
7297c478bd9Sstevel@tonic-gate  * have been loaded.  Skip any .fini defined in the main executable, as this
7307c478bd9Sstevel@tonic-gate  * will be called by crt0 (main was never marked as initdone).
7317c478bd9Sstevel@tonic-gate  */
7327c478bd9Sstevel@tonic-gate void
7337c478bd9Sstevel@tonic-gate call_fini(Lm_list * lml, Rt_map ** tobj)
7347c478bd9Sstevel@tonic-gate {
735dffec89cSrie 	Rt_map **_tobj;
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	for (_tobj = tobj; *_tobj != NULL; _tobj++) {
7387c478bd9Sstevel@tonic-gate 		Rt_map *	clmp, * lmp = *_tobj;
7397c478bd9Sstevel@tonic-gate 		Aliste		off;
7407c478bd9Sstevel@tonic-gate 		Bnd_desc **	bdpp;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * If concurrency checking isn't enabled only fire .fini if
7447c478bd9Sstevel@tonic-gate 		 * .init has completed.  We collect all .fini sections of
7457c478bd9Sstevel@tonic-gate 		 * objects that had their .init collected, but that doesn't
7467c478bd9Sstevel@tonic-gate 		 * mean at the time that the .init had completed.
7477c478bd9Sstevel@tonic-gate 		 */
7487c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_CONCUR) ||
7497c478bd9Sstevel@tonic-gate 		    (FLAGS(lmp) & FLG_RT_INITDONE)) {
75010a4fa49Srie 			void	(*fptr)(void) = FINI(lmp);
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 			if (FINIARRAY(lmp) || fptr) {
7537c478bd9Sstevel@tonic-gate 				/*
7547c478bd9Sstevel@tonic-gate 				 * If concurrency checking is enabled make sure
7557c478bd9Sstevel@tonic-gate 				 * this object's .init is completed before
7567c478bd9Sstevel@tonic-gate 				 * calling any .fini.
7577c478bd9Sstevel@tonic-gate 				 */
7587c478bd9Sstevel@tonic-gate 				is_dep_ready(lmp, lmp, DBG_WAIT_FINI);
7595aefb655Srie 				DBG_CALL(Dbg_util_call_fini(lmp));
7607c478bd9Sstevel@tonic-gate 			}
7617c478bd9Sstevel@tonic-gate 
7625aefb655Srie 			call_array(FINIARRAY(lmp), FINIARRAYSZ(lmp), lmp,
7635aefb655Srie 			    SHT_FINI_ARRAY);
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 			if (fptr) {
7667c478bd9Sstevel@tonic-gate 				leave(LIST(lmp));
7677c478bd9Sstevel@tonic-gate 				(*fptr)();
7687c478bd9Sstevel@tonic-gate 				(void) enter();
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 
772dffec89cSrie 		/*
773dffec89cSrie 		 * Skip main, this is explicitly called last in atexit_fini().
774dffec89cSrie 		 */
775dffec89cSrie 		if (FLAGS(lmp) & FLG_RT_ISMAIN)
776dffec89cSrie 			continue;
777dffec89cSrie 
7787c478bd9Sstevel@tonic-gate 		/*
7797c478bd9Sstevel@tonic-gate 		 * Audit `close' operations at this point.  The library has
7807c478bd9Sstevel@tonic-gate 		 * exercised its last instructions (regardless of whether it
7817c478bd9Sstevel@tonic-gate 		 * will be unmapped or not).
7827c478bd9Sstevel@tonic-gate 		 *
7837c478bd9Sstevel@tonic-gate 		 * First call any global auditing.
7847c478bd9Sstevel@tonic-gate 		 */
7857c478bd9Sstevel@tonic-gate 		if (lml->lm_tflags & LML_TFLG_AUD_OBJCLOSE)
7867c478bd9Sstevel@tonic-gate 			_audit_objclose(&(auditors->ad_list), lmp);
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 		/*
7897c478bd9Sstevel@tonic-gate 		 * Finally determine whether this object has local auditing
7907c478bd9Sstevel@tonic-gate 		 * requirements by inspecting itself and then its dependencies.
7917c478bd9Sstevel@tonic-gate 		 */
7927c478bd9Sstevel@tonic-gate 		if ((lml->lm_flags & LML_FLG_LOCAUDIT) == 0)
7937c478bd9Sstevel@tonic-gate 			continue;
7947c478bd9Sstevel@tonic-gate 
7957c478bd9Sstevel@tonic-gate 		if (FLAGS1(lmp) & LML_TFLG_AUD_OBJCLOSE)
7967c478bd9Sstevel@tonic-gate 			_audit_objclose(&(AUDITORS(lmp)->ad_list), lmp);
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(CALLERS(lmp), off, bdpp)) {
7997c478bd9Sstevel@tonic-gate 			Bnd_desc *	bdp = *bdpp;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 			clmp = bdp->b_caller;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 			if (FLAGS1(clmp) & LML_TFLG_AUD_OBJCLOSE) {
804a953e2b1Srie 				_audit_objclose(&(AUDITORS(clmp)->ad_list),
805a953e2b1Srie 				    lmp);
806a953e2b1Srie 				break;
8077c478bd9Sstevel@tonic-gate 			}
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 	}
8105aefb655Srie 	DBG_CALL(Dbg_bind_plt_summary(lml, M_MACH, pltcnt21d, pltcnt24d,
8115aefb655Srie 	    pltcntu32, pltcntu44, pltcntfull, pltcntfar));
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	free(tobj);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate void
8177c478bd9Sstevel@tonic-gate atexit_fini()
8187c478bd9Sstevel@tonic-gate {
8197c478bd9Sstevel@tonic-gate 	Rt_map **	tobj, * lmp;
8207c478bd9Sstevel@tonic-gate 	Lm_list *	lml;
8217c478bd9Sstevel@tonic-gate 	Listnode *	lnp;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	(void) enter();
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	rtld_flags |= RT_FL_ATEXIT;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	lml = &lml_main;
828dffec89cSrie 	lml->lm_flags |= LML_FLG_ATEXIT;
829883c6d49Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
8307c478bd9Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	/*
8337c478bd9Sstevel@tonic-gate 	 * Display any objects that haven't been referenced so far.
8347c478bd9Sstevel@tonic-gate 	 */
8357c478bd9Sstevel@tonic-gate 	unused(lml);
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	/*
8387c478bd9Sstevel@tonic-gate 	 * Reverse topologically sort the main link-map for .fini execution.
8397c478bd9Sstevel@tonic-gate 	 */
8407c478bd9Sstevel@tonic-gate 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
8417c478bd9Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
8427c478bd9Sstevel@tonic-gate 		call_fini(lml, tobj);
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate 	/*
845dffec89cSrie 	 * Add an explicit close to main and ld.so.1.  Although main's .fini is
846dffec89cSrie 	 * collected in call_fini() to provide for FINITARRAY processing, its
847dffec89cSrie 	 * audit_objclose is explicitly skipped.  This provides for it to be
848dffec89cSrie 	 * called last, here.  This is the reverse of the explicit calls to
849dffec89cSrie 	 * audit_objopen() made in setup().
8507c478bd9Sstevel@tonic-gate 	 */
8517c478bd9Sstevel@tonic-gate 	if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_MASK) {
8527c478bd9Sstevel@tonic-gate 		audit_objclose(lmp, (Rt_map *)lml_rtld.lm_head);
853dffec89cSrie 		audit_objclose(lmp, lmp);
8547c478bd9Sstevel@tonic-gate 	}
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	/*
8577c478bd9Sstevel@tonic-gate 	 * Now that all .fini code has been run, see what unreferenced objects
8587c478bd9Sstevel@tonic-gate 	 * remain.  Any difference between this and the above unused() would
8597c478bd9Sstevel@tonic-gate 	 * indicate an object is only being used for .fini processing, which
8607c478bd9Sstevel@tonic-gate 	 * might be fine, but might also indicate an overhead whose removal
8617c478bd9Sstevel@tonic-gate 	 * would be worth considering.
8627c478bd9Sstevel@tonic-gate 	 */
8637c478bd9Sstevel@tonic-gate 	unused(lml);
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	/*
8667c478bd9Sstevel@tonic-gate 	 * Traverse any alternative link-map lists.
8677c478bd9Sstevel@tonic-gate 	 */
8687c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&dynlm_list, lnp, lml)) {
86910a4fa49Srie 		/*
87010a4fa49Srie 		 * Ignore the base-link-map list, which has already been
87110a4fa49Srie 		 * processed, and the runtime linkers link-map list, which is
87210a4fa49Srie 		 * typically processed last.
87310a4fa49Srie 		 */
8747c478bd9Sstevel@tonic-gate 		if (lml->lm_flags & (LML_FLG_BASELM | LML_FLG_RTLDLM))
8757c478bd9Sstevel@tonic-gate 			continue;
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 		if ((lmp = (Rt_map *)lml->lm_head) == 0)
8787c478bd9Sstevel@tonic-gate 			continue;
8797c478bd9Sstevel@tonic-gate 
880dffec89cSrie 		lml->lm_flags |= LML_FLG_ATEXIT;
881883c6d49Srie 		lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
882dffec89cSrie 
8837c478bd9Sstevel@tonic-gate 		/*
8847c478bd9Sstevel@tonic-gate 		 * Reverse topologically sort the link-map for .fini execution.
8857c478bd9Sstevel@tonic-gate 		 */
8867c478bd9Sstevel@tonic-gate 		if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
8877c478bd9Sstevel@tonic-gate 		    (tobj != (Rt_map **)S_ERROR))
8887c478bd9Sstevel@tonic-gate 			call_fini(lml, tobj);
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 		unused(lml);
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * Finally reverse topologically sort the runtime linkers link-map for
8957c478bd9Sstevel@tonic-gate 	 * .fini execution.
8967c478bd9Sstevel@tonic-gate 	 */
8977c478bd9Sstevel@tonic-gate 	lml = &lml_rtld;
898dffec89cSrie 	lml->lm_flags |= LML_FLG_ATEXIT;
899883c6d49Srie 	lml->lm_flags &= ~LML_FLG_INTRPOSETSORT;
9007c478bd9Sstevel@tonic-gate 	lmp = (Rt_map *)lml->lm_head;
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate 	if (((tobj = tsort(lmp, lml->lm_obj, RT_SORT_FWD)) != 0) &&
9037c478bd9Sstevel@tonic-gate 	    (tobj != (Rt_map **)S_ERROR))
9047c478bd9Sstevel@tonic-gate 		call_fini(lml, tobj);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	leave(&lml_main);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate /*
9117c478bd9Sstevel@tonic-gate  * This routine is called to complete any runtime linker activity which may have
9127c478bd9Sstevel@tonic-gate  * resulted in objects being loaded.  This is called from all user entry points
9137c478bd9Sstevel@tonic-gate  * and from any internal dl*() requests.
9147c478bd9Sstevel@tonic-gate  */
9157c478bd9Sstevel@tonic-gate void
916*7247f888Srie load_completion(Rt_map *nlmp)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	Rt_map	**tobj = 0;
919*7247f888Srie 	Lm_list	*nlml;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	/*
9227c478bd9Sstevel@tonic-gate 	 * Establish any .init processing.  Note, in a world of lazy loading,
9237c478bd9Sstevel@tonic-gate 	 * objects may have been loaded regardless of whether the users request
9247c478bd9Sstevel@tonic-gate 	 * was fulfilled (i.e., a dlsym() request may have failed to find a
9257c478bd9Sstevel@tonic-gate 	 * symbol but objects might have been loaded during its search).  Thus,
9267c478bd9Sstevel@tonic-gate 	 * any tsorting starts from the nlmp (new link-maps) pointer and not
9277c478bd9Sstevel@tonic-gate 	 * necessarily from the link-map that may have satisfied the request.
9287c478bd9Sstevel@tonic-gate 	 *
92910a4fa49Srie 	 * Note, the primary link-map has an initialization phase where dynamic
93010a4fa49Srie 	 * .init firing is suppressed.  This provides for a simple and clean
93110a4fa49Srie 	 * handshake with the primary link-maps libc, which is important for
93210a4fa49Srie 	 * establishing uberdata.  In addition, auditors often obtain handles
93310a4fa49Srie 	 * to primary link-map objects as the objects are loaded, so as to
93410a4fa49Srie 	 * inspect the link-map for symbols.  This inspection is allowed without
93510a4fa49Srie 	 * running any code on the primary link-map, as running this code may
93610a4fa49Srie 	 * reenter the auditor, who may not yet have finished its own
9377c478bd9Sstevel@tonic-gate 	 * initialization.
9387c478bd9Sstevel@tonic-gate 	 */
93910a4fa49Srie 	if (nlmp)
94010a4fa49Srie 		nlml = LIST(nlmp);
94110a4fa49Srie 
94210a4fa49Srie 	if (nlmp && nlml->lm_init &&
94310a4fa49Srie 	    ((nlml != &lml_main) || (rtld_flags2 & RT_FL2_PLMSETUP))) {
944dffec89cSrie 		if ((tobj = tsort(nlmp, LIST(nlmp)->lm_init,
945dffec89cSrie 		    RT_SORT_REV)) == (Rt_map **)S_ERROR)
9467c478bd9Sstevel@tonic-gate 			tobj = 0;
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
94910a4fa49Srie 	/*
95010a4fa49Srie 	 * Make sure any alternative link-map retrieves any external interfaces
95110a4fa49Srie 	 * and initializes threads.
95210a4fa49Srie 	 */
95310a4fa49Srie 	if (nlmp && (nlml != &lml_main)) {
95410a4fa49Srie 		(void) rt_get_extern(nlml, nlmp);
95510a4fa49Srie 		rt_thr_init(nlml);
95610a4fa49Srie 	}
95710a4fa49Srie 
95810a4fa49Srie 	/*
95910a4fa49Srie 	 * Traverse the list of new link-maps and register any dynamic TLS.
96010a4fa49Srie 	 * This storage is established for any objects not on the primary
96110a4fa49Srie 	 * link-map, and for any objects added to the primary link-map after
96210a4fa49Srie 	 * static TLS has been registered.
96310a4fa49Srie 	 */
96410a4fa49Srie 	if (nlmp && nlml->lm_tls &&
96510a4fa49Srie 	    ((nlml != &lml_main) || (rtld_flags2 & RT_FL2_PLMSETUP))) {
96610a4fa49Srie 		Rt_map	*lmp;
96710a4fa49Srie 
96810a4fa49Srie 		for (lmp = nlmp; lmp; lmp = (Rt_map *)NEXT(lmp)) {
96910a4fa49Srie 			if (PTTLS(lmp) && PTTLS(lmp)->p_memsz)
97010a4fa49Srie 				tls_modaddrem(lmp, TM_FLG_MODADD);
97110a4fa49Srie 		}
97210a4fa49Srie 		nlml->lm_tls = 0;
97310a4fa49Srie 	}
97410a4fa49Srie 
9757c478bd9Sstevel@tonic-gate 	/*
9767c478bd9Sstevel@tonic-gate 	 * Fire any .init's.
9777c478bd9Sstevel@tonic-gate 	 */
9787c478bd9Sstevel@tonic-gate 	if (tobj)
9797c478bd9Sstevel@tonic-gate 		call_init(tobj, DBG_INIT_SORT);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate /*
9837c478bd9Sstevel@tonic-gate  * Append an item to the specified list, and return a pointer to the list
9847c478bd9Sstevel@tonic-gate  * node created.
9857c478bd9Sstevel@tonic-gate  */
9867c478bd9Sstevel@tonic-gate Listnode *
9877c478bd9Sstevel@tonic-gate list_append(List *lst, const void *item)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	Listnode *	_lnp;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == 0)
9927c478bd9Sstevel@tonic-gate 		return (0);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	_lnp->data = (void *)item;
9957c478bd9Sstevel@tonic-gate 	_lnp->next = NULL;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (lst->head == NULL)
9987c478bd9Sstevel@tonic-gate 		lst->tail = lst->head = _lnp;
9997c478bd9Sstevel@tonic-gate 	else {
10007c478bd9Sstevel@tonic-gate 		lst->tail->next = _lnp;
10017c478bd9Sstevel@tonic-gate 		lst->tail = lst->tail->next;
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	return (_lnp);
10047c478bd9Sstevel@tonic-gate }
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate /*
10087c478bd9Sstevel@tonic-gate  * Add an item after specified listnode, and return a pointer to the list
10097c478bd9Sstevel@tonic-gate  * node created.
10107c478bd9Sstevel@tonic-gate  */
10117c478bd9Sstevel@tonic-gate Listnode *
10127c478bd9Sstevel@tonic-gate list_insert(List *lst, const void *item, Listnode *lnp)
10137c478bd9Sstevel@tonic-gate {
10147c478bd9Sstevel@tonic-gate 	Listnode *	_lnp;
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
10177c478bd9Sstevel@tonic-gate 		return (0);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	_lnp->data = (void *)item;
10207c478bd9Sstevel@tonic-gate 	_lnp->next = lnp->next;
10217c478bd9Sstevel@tonic-gate 	if (_lnp->next == NULL)
10227c478bd9Sstevel@tonic-gate 		lst->tail = _lnp;
10237c478bd9Sstevel@tonic-gate 	lnp->next = _lnp;
10247c478bd9Sstevel@tonic-gate 	return (_lnp);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate /*
10287c478bd9Sstevel@tonic-gate  * Prepend an item to the specified list, and return a pointer to the
10297c478bd9Sstevel@tonic-gate  * list node created.
10307c478bd9Sstevel@tonic-gate  */
10317c478bd9Sstevel@tonic-gate Listnode *
10327c478bd9Sstevel@tonic-gate list_prepend(List * lst, const void * item)
10337c478bd9Sstevel@tonic-gate {
10347c478bd9Sstevel@tonic-gate 	Listnode *	_lnp;
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	if ((_lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
10377c478bd9Sstevel@tonic-gate 		return (0);
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	_lnp->data = (void *)item;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	if (lst->head == NULL) {
10427c478bd9Sstevel@tonic-gate 		_lnp->next = NULL;
10437c478bd9Sstevel@tonic-gate 		lst->tail = lst->head = _lnp;
10447c478bd9Sstevel@tonic-gate 	} else {
10457c478bd9Sstevel@tonic-gate 		_lnp->next = lst->head;
10467c478bd9Sstevel@tonic-gate 		lst->head = _lnp;
10477c478bd9Sstevel@tonic-gate 	}
10487c478bd9Sstevel@tonic-gate 	return (_lnp);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate /*
10537c478bd9Sstevel@tonic-gate  * Delete a 'listnode' from a list.
10547c478bd9Sstevel@tonic-gate  */
10557c478bd9Sstevel@tonic-gate void
105602ca3e02Srie list_delete(List *lst, void *item)
10577c478bd9Sstevel@tonic-gate {
105802ca3e02Srie 	Listnode	*clnp, *plnp;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 	for (plnp = NULL, clnp = lst->head; clnp; clnp = clnp->next) {
10617c478bd9Sstevel@tonic-gate 		if (item == clnp->data)
10627c478bd9Sstevel@tonic-gate 			break;
10637c478bd9Sstevel@tonic-gate 		plnp = clnp;
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (clnp == 0)
10677c478bd9Sstevel@tonic-gate 		return;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (lst->head == clnp)
10707c478bd9Sstevel@tonic-gate 		lst->head = clnp->next;
10717c478bd9Sstevel@tonic-gate 	if (lst->tail == clnp)
10727c478bd9Sstevel@tonic-gate 		lst->tail = plnp;
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	if (plnp)
10757c478bd9Sstevel@tonic-gate 		plnp->next = clnp->next;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	free(clnp);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate /*
10817c478bd9Sstevel@tonic-gate  * Append an item to the specified link map control list.
10827c478bd9Sstevel@tonic-gate  */
10837c478bd9Sstevel@tonic-gate void
10847c478bd9Sstevel@tonic-gate lm_append(Lm_list *lml, Aliste lmco, Rt_map *lmp)
10857c478bd9Sstevel@tonic-gate {
10867c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
10877c478bd9Sstevel@tonic-gate 	int	add = 1;
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/*
10907c478bd9Sstevel@tonic-gate 	 * Indicate that this link-map list has a new object.
10917c478bd9Sstevel@tonic-gate 	 */
10927c478bd9Sstevel@tonic-gate 	(lml->lm_obj)++;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 	/*
109512b8e62eSrie 	 * If we're about to add a new object to the main link-map control list,
109612b8e62eSrie 	 * alert the debuggers that we are about to mess with this list.
109712b8e62eSrie 	 * Additions of individual objects to the main link-map control list
109812b8e62eSrie 	 * occur during initial setup as the applications immediate dependencies
109912b8e62eSrie 	 * are loaded.  Individual objects are also loaded on the main link-map
110012b8e62eSrie 	 * control list of new alternative link-map control lists.
11017c478bd9Sstevel@tonic-gate 	 */
11027c478bd9Sstevel@tonic-gate 	if ((lmco == ALO_DATA) && ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
110312b8e62eSrie 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	/* LINTED */
11067c478bd9Sstevel@tonic-gate 	lmc = (Lm_cntl *)((char *)lml->lm_lists + lmco);
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	/*
11097c478bd9Sstevel@tonic-gate 	 * A link-map list header points to one of more link-map control lists
11107c478bd9Sstevel@tonic-gate 	 * (see include/rtld.h).  The initial list, pointed to by lm_cntl, is
11117c478bd9Sstevel@tonic-gate 	 * the list of relocated objects.  Other lists maintain objects that
11127c478bd9Sstevel@tonic-gate 	 * are still being analyzed or relocated.  This list provides the core
11137c478bd9Sstevel@tonic-gate 	 * link-map list information used by all ld.so.1 routines.
11147c478bd9Sstevel@tonic-gate 	 */
11157c478bd9Sstevel@tonic-gate 	if (lmc->lc_head == NULL) {
11167c478bd9Sstevel@tonic-gate 		/*
11177c478bd9Sstevel@tonic-gate 		 * If this is the first link-map for the given control list,
11187c478bd9Sstevel@tonic-gate 		 * initialize the list.
11197c478bd9Sstevel@tonic-gate 		 */
11207c478bd9Sstevel@tonic-gate 		lmc->lc_head = lmc->lc_tail = lmp;
11217c478bd9Sstevel@tonic-gate 		add = 0;
11227c478bd9Sstevel@tonic-gate 
11239a411307Srie 	} else if (FLAGS(lmp) & FLG_RT_OBJINTPO) {
11247c478bd9Sstevel@tonic-gate 		Rt_map	*tlmp;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 		/*
11277c478bd9Sstevel@tonic-gate 		 * If this is an interposer then append the link-map following
11287c478bd9Sstevel@tonic-gate 		 * any other interposers (these are objects that have been
11297c478bd9Sstevel@tonic-gate 		 * previously preloaded, or were identified with -z interpose).
11307c478bd9Sstevel@tonic-gate 		 * Interposers can only be inserted on the first link-map
11317c478bd9Sstevel@tonic-gate 		 * control list, as once relocation has started, interposition
11327c478bd9Sstevel@tonic-gate 		 * from new interposers can't be guaranteed.
11337c478bd9Sstevel@tonic-gate 		 *
11347c478bd9Sstevel@tonic-gate 		 * NOTE: We do not interpose on the head of a list.  This model
11357c478bd9Sstevel@tonic-gate 		 * evolved because dynamic executables have already been fully
11367c478bd9Sstevel@tonic-gate 		 * relocated within themselves and thus can't be interposed on.
11377c478bd9Sstevel@tonic-gate 		 * Nowadays it's possible to have shared objects at the head of
11387c478bd9Sstevel@tonic-gate 		 * a list, which conceptually means they could be interposed on.
11397c478bd9Sstevel@tonic-gate 		 * But, shared objects can be created via dldump() and may only
11407c478bd9Sstevel@tonic-gate 		 * be partially relocated (just relatives), in which case they
11417c478bd9Sstevel@tonic-gate 		 * are interposable, but are marked as fixed (ET_EXEC).
11427c478bd9Sstevel@tonic-gate 		 *
11437c478bd9Sstevel@tonic-gate 		 * Thus we really don't have a clear method of deciding when the
11447c478bd9Sstevel@tonic-gate 		 * head of a link-map is interposable.  So, to be consistent,
11457c478bd9Sstevel@tonic-gate 		 * for now only add interposers after the link-map lists head
11467c478bd9Sstevel@tonic-gate 		 * object.
11477c478bd9Sstevel@tonic-gate 		 */
11487c478bd9Sstevel@tonic-gate 		for (tlmp = (Rt_map *)NEXT(lmc->lc_head); tlmp;
11497c478bd9Sstevel@tonic-gate 		    tlmp = (Rt_map *)NEXT(tlmp)) {
11507c478bd9Sstevel@tonic-gate 
11519a411307Srie 			if (FLAGS(tlmp) & FLG_RT_OBJINTPO)
11527c478bd9Sstevel@tonic-gate 				continue;
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 			/*
11557c478bd9Sstevel@tonic-gate 			 * Insert the new link-map before this non-interposer,
11567c478bd9Sstevel@tonic-gate 			 * and indicate an interposer is found.
11577c478bd9Sstevel@tonic-gate 			 */
11587c478bd9Sstevel@tonic-gate 			NEXT((Rt_map *)PREV(tlmp)) = (Link_map *)lmp;
11597c478bd9Sstevel@tonic-gate 			PREV(lmp) = PREV(tlmp);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 			NEXT(lmp) = (Link_map *)tlmp;
11627c478bd9Sstevel@tonic-gate 			PREV(tlmp) = (Link_map *)lmp;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 			lmc->lc_flags |= LMC_FLG_REANALYZE;
11657c478bd9Sstevel@tonic-gate 			add = 0;
11667c478bd9Sstevel@tonic-gate 			break;
11677c478bd9Sstevel@tonic-gate 		}
11687c478bd9Sstevel@tonic-gate 	}
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	/*
11717c478bd9Sstevel@tonic-gate 	 * Fall through to appending the new link map to the tail of the list.
11727c478bd9Sstevel@tonic-gate 	 * If we're processing the initial objects of this link-map list, add
11737c478bd9Sstevel@tonic-gate 	 * them to the backward compatibility list.
11747c478bd9Sstevel@tonic-gate 	 */
11757c478bd9Sstevel@tonic-gate 	if (add) {
11767c478bd9Sstevel@tonic-gate 		NEXT(lmc->lc_tail) = (Link_map *)lmp;
11777c478bd9Sstevel@tonic-gate 		PREV(lmp) = (Link_map *)lmc->lc_tail;
11787c478bd9Sstevel@tonic-gate 		lmc->lc_tail = lmp;
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	/*
11827c478bd9Sstevel@tonic-gate 	 * Having added this link-map to a control list, indicate which control
11837c478bd9Sstevel@tonic-gate 	 * list the link-map belongs to.  Note, control list information is
11847c478bd9Sstevel@tonic-gate 	 * always maintained as an offset, as the Alist can be reallocated.
11857c478bd9Sstevel@tonic-gate 	 */
11867c478bd9Sstevel@tonic-gate 	CNTL(lmp) = lmco;
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 	/*
11897c478bd9Sstevel@tonic-gate 	 * Indicate if an interposer is found.  Note that the first object on a
11907c478bd9Sstevel@tonic-gate 	 * link-map can be explicitly defined as an interposer so that it can
11917c478bd9Sstevel@tonic-gate 	 * provide interposition over direct binding requests.
11927c478bd9Sstevel@tonic-gate 	 */
11939a411307Srie 	if (FLAGS(lmp) & MSK_RT_INTPOSE)
11947c478bd9Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_INTRPOSE;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	/*
11977c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
11987c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
11997c478bd9Sstevel@tonic-gate 	 */
12007c478bd9Sstevel@tonic-gate 	if (lmco == ALO_DATA) {
12017c478bd9Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12027c478bd9Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * Delete an item from the specified link map control list.
12087c478bd9Sstevel@tonic-gate  */
12097c478bd9Sstevel@tonic-gate void
12107c478bd9Sstevel@tonic-gate lm_delete(Lm_list *lml, Rt_map *lmp)
12117c478bd9Sstevel@tonic-gate {
12127c478bd9Sstevel@tonic-gate 	Lm_cntl	*lmc;
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	/*
12157c478bd9Sstevel@tonic-gate 	 * If the control list pointer hasn't been initialized, this object
12167c478bd9Sstevel@tonic-gate 	 * never got added to a link-map list.
12177c478bd9Sstevel@tonic-gate 	 */
12187c478bd9Sstevel@tonic-gate 	if (CNTL(lmp) == 0)
12197c478bd9Sstevel@tonic-gate 		return;
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	/*
122212b8e62eSrie 	 * If we're about to delete an object from the main link-map control
122312b8e62eSrie 	 * list, alert the debuggers that we are about to mess with this list.
12247c478bd9Sstevel@tonic-gate 	 */
12257c478bd9Sstevel@tonic-gate 	if ((CNTL(lmp) == ALO_DATA) && ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
12267c478bd9Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_DELETE);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	/* LINTED */
12297c478bd9Sstevel@tonic-gate 	lmc = (Lm_cntl *)((char *)lml->lm_lists + CNTL(lmp));
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	if (lmc->lc_head == lmp)
12327c478bd9Sstevel@tonic-gate 		lmc->lc_head = (Rt_map *)NEXT(lmp);
12337c478bd9Sstevel@tonic-gate 	else
12347c478bd9Sstevel@tonic-gate 		NEXT((Rt_map *)PREV(lmp)) = (void *)NEXT(lmp);
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	if (lmc->lc_tail == lmp)
12377c478bd9Sstevel@tonic-gate 		lmc->lc_tail = (Rt_map *)PREV(lmp);
12387c478bd9Sstevel@tonic-gate 	else
12397c478bd9Sstevel@tonic-gate 		PREV((Rt_map *)NEXT(lmp)) = PREV(lmp);
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	/*
12427c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
12437c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
12447c478bd9Sstevel@tonic-gate 	 */
12457c478bd9Sstevel@tonic-gate 	if (lmc == (Lm_cntl *)&(lml->lm_lists->al_data)) {
12467c478bd9Sstevel@tonic-gate 		lml->lm_head = lmc->lc_head;
12477c478bd9Sstevel@tonic-gate 		lml->lm_tail = lmc->lc_tail;
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * Indicate we have one less object on this control list.
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	(lml->lm_obj)--;
12547c478bd9Sstevel@tonic-gate }
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate /*
12577c478bd9Sstevel@tonic-gate  * Move a link-map control list to another.  Objects that are being relocated
12587c478bd9Sstevel@tonic-gate  * are maintained on secondary control lists.  Once their relocation is
12597c478bd9Sstevel@tonic-gate  * complete, the entire list is appended to the previous control list, as this
12607c478bd9Sstevel@tonic-gate  * list must have been the trigger for generating the new control list.
12617c478bd9Sstevel@tonic-gate  */
12627c478bd9Sstevel@tonic-gate void
12637c478bd9Sstevel@tonic-gate lm_move(Lm_list *lml, Aliste nlmco, Aliste plmco, Lm_cntl *nlmc, Lm_cntl *plmc)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate 	Rt_map	*lmp;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	/*
126812b8e62eSrie 	 * If we're about to add a new family of objects to the main link-map
126912b8e62eSrie 	 * control list, alert the debuggers that we are about to mess with this
127012b8e62eSrie 	 * list.  Additions of object families to the main link-map control
127112b8e62eSrie 	 * list occur during lazy loading, filtering and dlopen().
12727c478bd9Sstevel@tonic-gate 	 */
12737c478bd9Sstevel@tonic-gate 	if ((plmco == ALO_DATA) && ((lml->lm_flags & LML_FLG_DBNOTIF) == 0))
12747c478bd9Sstevel@tonic-gate 		rd_event(lml, RD_DLACTIVITY, RT_ADD);
12757c478bd9Sstevel@tonic-gate 
127612b8e62eSrie 	DBG_CALL(Dbg_file_cntl(lml, nlmco, plmco));
127712b8e62eSrie 
12787c478bd9Sstevel@tonic-gate 	/*
12797c478bd9Sstevel@tonic-gate 	 * Indicate each new link-map has been moved to the previous link-map
12807c478bd9Sstevel@tonic-gate 	 * control list.
12817c478bd9Sstevel@tonic-gate 	 */
12827c478bd9Sstevel@tonic-gate 	for (lmp = nlmc->lc_head; lmp; lmp = (Rt_map *)NEXT(lmp))
12837c478bd9Sstevel@tonic-gate 		CNTL(lmp) = plmco;
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 	/*
12867c478bd9Sstevel@tonic-gate 	 * Move the new link-map control list, to the callers link-map control
12877c478bd9Sstevel@tonic-gate 	 * list.
12887c478bd9Sstevel@tonic-gate 	 */
12897c478bd9Sstevel@tonic-gate 	if (plmc->lc_head == 0) {
12907c478bd9Sstevel@tonic-gate 		plmc->lc_head = nlmc->lc_head;
12917c478bd9Sstevel@tonic-gate 		PREV(nlmc->lc_head) = 0;
12927c478bd9Sstevel@tonic-gate 	} else {
12937c478bd9Sstevel@tonic-gate 		NEXT(plmc->lc_tail) = (Link_map *)nlmc->lc_head;
12947c478bd9Sstevel@tonic-gate 		PREV(nlmc->lc_head) = (Link_map *)plmc->lc_tail;
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	plmc->lc_tail = nlmc->lc_tail;
12987c478bd9Sstevel@tonic-gate 	nlmc->lc_head = nlmc->lc_tail = 0;
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	/*
13017c478bd9Sstevel@tonic-gate 	 * For backward compatibility with debuggers, the link-map list contains
13027c478bd9Sstevel@tonic-gate 	 * pointers to the main control list.
13037c478bd9Sstevel@tonic-gate 	 */
13047c478bd9Sstevel@tonic-gate 	if (plmco == ALO_DATA) {
13057c478bd9Sstevel@tonic-gate 		lml->lm_head = plmc->lc_head;
13067c478bd9Sstevel@tonic-gate 		lml->lm_tail = plmc->lc_tail;
13077c478bd9Sstevel@tonic-gate 	}
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate /*
13117c478bd9Sstevel@tonic-gate  * Environment variables can have a variety of defined permutations, and thus
13127c478bd9Sstevel@tonic-gate  * the following infrastructure exists to allow this variety and to select the
13137c478bd9Sstevel@tonic-gate  * required definition.
13147c478bd9Sstevel@tonic-gate  *
13157c478bd9Sstevel@tonic-gate  * Environment variables can be defined as 32- or 64-bit specific, and if so
13167c478bd9Sstevel@tonic-gate  * they will take precedence over any instruction set neutral form.  Typically
13177c478bd9Sstevel@tonic-gate  * this is only useful when the environment value is an informational string.
13187c478bd9Sstevel@tonic-gate  *
13197c478bd9Sstevel@tonic-gate  * Environment variables may be obtained from the standard user environment or
13207c478bd9Sstevel@tonic-gate  * from a configuration file.  The latter provides a fallback if no user
13217c478bd9Sstevel@tonic-gate  * environment setting is found, and can take two forms:
13227c478bd9Sstevel@tonic-gate  *
13237c478bd9Sstevel@tonic-gate  *  .	a replaceable definition - this will be used if no user environment
13247c478bd9Sstevel@tonic-gate  *	setting has been seen, or
13257c478bd9Sstevel@tonic-gate  *
13267c478bd9Sstevel@tonic-gate  *  .	an permanent definition - this will be used no matter what user
13277c478bd9Sstevel@tonic-gate  *	environment setting is seen.  In the case of list variables it will be
13287c478bd9Sstevel@tonic-gate  *	appended to any process environment setting seen.
13297c478bd9Sstevel@tonic-gate  *
13307c478bd9Sstevel@tonic-gate  * Environment variables can be defined without a value (ie. LD_XXXX=) so as to
13317c478bd9Sstevel@tonic-gate  * override any replaceable environment variables from a configuration file.
13327c478bd9Sstevel@tonic-gate  */
13337c478bd9Sstevel@tonic-gate static	u_longlong_t		rplgen;		/* replaceable generic */
13347c478bd9Sstevel@tonic-gate 						/*	variables */
13357c478bd9Sstevel@tonic-gate static	u_longlong_t		rplisa;		/* replaceable ISA specific */
13367c478bd9Sstevel@tonic-gate 						/*	variables */
13377c478bd9Sstevel@tonic-gate static	u_longlong_t		prmgen;		/* permanent generic */
13387c478bd9Sstevel@tonic-gate 						/*	variables */
13397c478bd9Sstevel@tonic-gate static	u_longlong_t		prmisa;		/* permanent ISA specific */
13407c478bd9Sstevel@tonic-gate 						/*	variables */
13417c478bd9Sstevel@tonic-gate 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * Classify an environment variables type.
13447c478bd9Sstevel@tonic-gate  */
13457c478bd9Sstevel@tonic-gate #define	ENV_TYP_IGNORE		0x1		/* ignore - variable is for */
13467c478bd9Sstevel@tonic-gate 						/*	the wrong ISA */
13477c478bd9Sstevel@tonic-gate #define	ENV_TYP_ISA		0x2		/* variable is ISA specific */
13487c478bd9Sstevel@tonic-gate #define	ENV_TYP_CONFIG		0x4		/* variable obtained from a */
13497c478bd9Sstevel@tonic-gate 						/*	config file */
13507c478bd9Sstevel@tonic-gate #define	ENV_TYP_PERMANT		0x8		/* variable is permanent */
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate /*
13537c478bd9Sstevel@tonic-gate  * Identify all environment variables.
13547c478bd9Sstevel@tonic-gate  */
13557c478bd9Sstevel@tonic-gate #define	ENV_FLG_AUDIT		0x0000000001ULL
13567c478bd9Sstevel@tonic-gate #define	ENV_FLG_AUDIT_ARGS	0x0000000002ULL
13577c478bd9Sstevel@tonic-gate #define	ENV_FLG_BIND_NOW	0x0000000004ULL
13587c478bd9Sstevel@tonic-gate #define	ENV_FLG_BIND_NOT	0x0000000008ULL
13597c478bd9Sstevel@tonic-gate #define	ENV_FLG_BINDINGS	0x0000000010ULL
13607c478bd9Sstevel@tonic-gate #define	ENV_FLG_CONCURRENCY	0x0000000020ULL
13617c478bd9Sstevel@tonic-gate #define	ENV_FLG_CONFGEN		0x0000000040ULL
13627c478bd9Sstevel@tonic-gate #define	ENV_FLG_CONFIG		0x0000000080ULL
13637c478bd9Sstevel@tonic-gate #define	ENV_FLG_DEBUG		0x0000000100ULL
13647c478bd9Sstevel@tonic-gate #define	ENV_FLG_DEBUG_OUTPUT	0x0000000200ULL
13657c478bd9Sstevel@tonic-gate #define	ENV_FLG_DEMANGLE	0x0000000400ULL
13667c478bd9Sstevel@tonic-gate #define	ENV_FLG_FLAGS		0x0000000800ULL
13677c478bd9Sstevel@tonic-gate #define	ENV_FLG_INIT		0x0000001000ULL
13687c478bd9Sstevel@tonic-gate #define	ENV_FLG_LIBPATH		0x0000002000ULL
13697c478bd9Sstevel@tonic-gate #define	ENV_FLG_LOADAVAIL	0x0000004000ULL
13707c478bd9Sstevel@tonic-gate #define	ENV_FLG_LOADFLTR	0x0000008000ULL
13717c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOAUDIT		0x0000010000ULL
13727c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOAUXFLTR	0x0000020000ULL
13737c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOBAPLT		0x0000040000ULL
13747c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOCONFIG	0x0000080000ULL
13757c478bd9Sstevel@tonic-gate #define	ENV_FLG_NODIRCONFIG	0x0000100000ULL
13767c478bd9Sstevel@tonic-gate #define	ENV_FLG_NODIRECT	0x0000200000ULL
13777c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOENVCONFIG	0x0000400000ULL
13787c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOLAZY		0x0000800000ULL
13797c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOOBJALTER	0x0001000000ULL
13807c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOVERSION	0x0002000000ULL
13817c478bd9Sstevel@tonic-gate #define	ENV_FLG_PRELOAD		0x0004000000ULL
13827c478bd9Sstevel@tonic-gate #define	ENV_FLG_PROFILE		0x0008000000ULL
13837c478bd9Sstevel@tonic-gate #define	ENV_FLG_PROFILE_OUTPUT	0x0010000000ULL
13847c478bd9Sstevel@tonic-gate #define	ENV_FLG_SIGNAL		0x0020000000ULL
13857c478bd9Sstevel@tonic-gate #define	ENV_FLG_TRACE_OBJS	0x0040000000ULL
13867c478bd9Sstevel@tonic-gate #define	ENV_FLG_TRACE_PTHS	0x0080000000ULL
13877c478bd9Sstevel@tonic-gate #define	ENV_FLG_UNREF		0x0100000000ULL
13887c478bd9Sstevel@tonic-gate #define	ENV_FLG_UNUSED		0x0200000000ULL
13897c478bd9Sstevel@tonic-gate #define	ENV_FLG_VERBOSE		0x0400000000ULL
13907c478bd9Sstevel@tonic-gate #define	ENV_FLG_WARN		0x0800000000ULL
13917c478bd9Sstevel@tonic-gate #define	ENV_FLG_NOFLTCONFIG	0x1000000000ULL
1392dffec89cSrie #define	ENV_FLG_BIND_LAZY	0x2000000000ULL
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
13957c478bd9Sstevel@tonic-gate #define	ENV_FLG_FIX_1		0x8000000000ULL
13967c478bd9Sstevel@tonic-gate #endif
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate #define	SEL_REPLACE		0x0001
13997c478bd9Sstevel@tonic-gate #define	SEL_PERMANT		0x0002
14007c478bd9Sstevel@tonic-gate #define	SEL_ACT_RT		0x0100	/* setting rtld_flags */
14017c478bd9Sstevel@tonic-gate #define	SEL_ACT_RT2		0x0200	/* setting rtld_flags2 */
14027c478bd9Sstevel@tonic-gate #define	SEL_ACT_STR		0x0400	/* setting string value */
14037c478bd9Sstevel@tonic-gate #define	SEL_ACT_LML		0x0800	/* setting lml_flags */
14047c478bd9Sstevel@tonic-gate #define	SEL_ACT_LMLT		0x1000	/* setting lml_tflags */
14057c478bd9Sstevel@tonic-gate #define	SEL_ACT_SPEC_1		0x2000	/* For FLG_{FLAGS, LIBPATH} */
14067c478bd9Sstevel@tonic-gate #define	SEL_ACT_SPEC_2		0x4000	/* need special handling */
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate /*
14097c478bd9Sstevel@tonic-gate  * Pattern match an LD_XXXX environment variable.  s1 points to the XXXX part
14107c478bd9Sstevel@tonic-gate  * and len specifies its length (comparing a strings length before the string
14117c478bd9Sstevel@tonic-gate  * itself speed things up).  s2 points to the token itself which has already
14127c478bd9Sstevel@tonic-gate  * had any leading white-space removed.
14137c478bd9Sstevel@tonic-gate  */
14147c478bd9Sstevel@tonic-gate static void
14157c478bd9Sstevel@tonic-gate ld_generic_env(const char *s1, size_t len, const char *s2, Word *lmflags,
14167c478bd9Sstevel@tonic-gate     Word *lmtflags, uint_t env_flags, int aout)
14177c478bd9Sstevel@tonic-gate {
14187c478bd9Sstevel@tonic-gate 	u_longlong_t	variable = 0;
141910a4fa49Srie 	ushort_t	select = 0;
142010a4fa49Srie 	const char	**str;
142110a4fa49Srie 	Word		val = 0;
14227c478bd9Sstevel@tonic-gate 
14237c478bd9Sstevel@tonic-gate 	/*
14247c478bd9Sstevel@tonic-gate 	 * Determine whether we're dealing with a replaceable or permanent
14257c478bd9Sstevel@tonic-gate 	 * string.
14267c478bd9Sstevel@tonic-gate 	 */
14277c478bd9Sstevel@tonic-gate 	if (env_flags & ENV_TYP_PERMANT) {
14287c478bd9Sstevel@tonic-gate 		/*
14297c478bd9Sstevel@tonic-gate 		 * If the string is from a configuration file and defined as
14307c478bd9Sstevel@tonic-gate 		 * permanent, assign it as permanent.
14317c478bd9Sstevel@tonic-gate 		 */
14327c478bd9Sstevel@tonic-gate 		select |= SEL_PERMANT;
14337c478bd9Sstevel@tonic-gate 	} else
14347c478bd9Sstevel@tonic-gate 		select |= SEL_REPLACE;
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 	/*
14377c478bd9Sstevel@tonic-gate 	 * Parse the variable given.
14387c478bd9Sstevel@tonic-gate 	 *
14397c478bd9Sstevel@tonic-gate 	 * The LD_AUDIT family.
14407c478bd9Sstevel@tonic-gate 	 */
14417c478bd9Sstevel@tonic-gate 	if (*s1 == 'A') {
14427c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_AUDIT_SIZE) && (strncmp(s1,
14437c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_AUDIT), MSG_LD_AUDIT_SIZE) == 0)) {
14447c478bd9Sstevel@tonic-gate 			/*
14457c478bd9Sstevel@tonic-gate 			 * Replaceable and permanent audit objects can exist.
14467c478bd9Sstevel@tonic-gate 			 */
14477c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1448fa034d7fSseizo 			if (select & SEL_REPLACE)
1449fa034d7fSseizo 				str = &rpl_audit;
1450fa034d7fSseizo 			else {
1451fa034d7fSseizo 				str = &prm_audit;
1452fa034d7fSseizo 				rpl_audit = 0;
1453fa034d7fSseizo 			}
14547c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT;
14557c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_AUDIT_ARGS_SIZE) &&
14567c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_AUDIT_ARGS),
14577c478bd9Sstevel@tonic-gate 		    MSG_LD_AUDIT_ARGS_SIZE) == 0)) {
14587c478bd9Sstevel@tonic-gate 			/*
14597c478bd9Sstevel@tonic-gate 			 * A specialized variable for plt_exit() use, not
14607c478bd9Sstevel@tonic-gate 			 * documented for general use.
14617c478bd9Sstevel@tonic-gate 			 */
14627c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
14637c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_AUDIT_ARGS;
14647c478bd9Sstevel@tonic-gate 		}
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * The LD_BIND family and LD_BREADTH (historic).
14687c478bd9Sstevel@tonic-gate 	 */
14697c478bd9Sstevel@tonic-gate 	else if (*s1 == 'B') {
1470dffec89cSrie 		if ((len == MSG_LD_BIND_LAZY_SIZE) && (strncmp(s1,
1471dffec89cSrie 		    MSG_ORIG(MSG_LD_BIND_LAZY),
1472dffec89cSrie 		    MSG_LD_BIND_LAZY_SIZE) == 0)) {
1473dffec89cSrie 			select |= SEL_ACT_RT2;
1474dffec89cSrie 			val = RT_FL2_BINDLAZY;
1475dffec89cSrie 			variable = ENV_FLG_BIND_LAZY;
1476dffec89cSrie 		} else if ((len == MSG_LD_BIND_NOW_SIZE) && (strncmp(s1,
14777c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOW), MSG_LD_BIND_NOW_SIZE) == 0)) {
14787c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
14797c478bd9Sstevel@tonic-gate 			val = RT_FL2_BINDNOW;
14807c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOW;
14817c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BIND_NOT_SIZE) && (strncmp(s1,
14827c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BIND_NOT), MSG_LD_BIND_NOT_SIZE) == 0)) {
14837c478bd9Sstevel@tonic-gate 			/*
14847c478bd9Sstevel@tonic-gate 			 * Another trick, enabled to help debug AOUT
14857c478bd9Sstevel@tonic-gate 			 * applications under BCP, but not documented for
14867c478bd9Sstevel@tonic-gate 			 * general use.
14877c478bd9Sstevel@tonic-gate 			 */
14887c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
14897c478bd9Sstevel@tonic-gate 			val = RT_FL_NOBIND;
14907c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BIND_NOT;
14917c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BINDINGS_SIZE) && (strncmp(s1,
14927c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BINDINGS), MSG_LD_BINDINGS_SIZE) == 0)) {
14937c478bd9Sstevel@tonic-gate 			/*
14947c478bd9Sstevel@tonic-gate 			 * This variable is simply for backward compatibility.
14957c478bd9Sstevel@tonic-gate 			 * If this and LD_DEBUG are both specified, only one of
14967c478bd9Sstevel@tonic-gate 			 * the strings is going to get processed.
14977c478bd9Sstevel@tonic-gate 			 */
14987c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
14997c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_BINDINGS;
15007c478bd9Sstevel@tonic-gate #ifndef LD_BREADTH_DISABLED
15017c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_BREADTH_SIZE) && (strncmp(s1,
15027c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_BREADTH), MSG_LD_BREADTH_SIZE) == 0)) {
15037c478bd9Sstevel@tonic-gate 			/*
15047c478bd9Sstevel@tonic-gate 			 * Besides some old patches this is no longer available.
15057c478bd9Sstevel@tonic-gate 			 */
15067c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_BREADTH;
15077c478bd9Sstevel@tonic-gate 			return;
15087c478bd9Sstevel@tonic-gate #endif
15097c478bd9Sstevel@tonic-gate 		}
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 	/*
15127c478bd9Sstevel@tonic-gate 	 * LD_CONCURRENCY and LD_CONFIG family.
15137c478bd9Sstevel@tonic-gate 	 */
15147c478bd9Sstevel@tonic-gate 	else if (*s1 == 'C') {
15157c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_CONCURRENCY_SIZE) && (strncmp(s1,
15167c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONCURRENCY),
15177c478bd9Sstevel@tonic-gate 		    MSG_LD_CONCURRENCY_SIZE) == 0)) {
15187c478bd9Sstevel@tonic-gate 			/*
15197c478bd9Sstevel@tonic-gate 			 * Waiting in the wings, as concurrency checking isn't
15207c478bd9Sstevel@tonic-gate 			 * yet enabled.
15217c478bd9Sstevel@tonic-gate 			 */
15227c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15237c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONCURRENCY;
15247c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFGEN_SIZE) && (strncmp(s1,
15257c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFGEN), MSG_LD_CONFGEN_SIZE) == 0)) {
15267c478bd9Sstevel@tonic-gate 			/*
15277c478bd9Sstevel@tonic-gate 			 * Set by crle(1) to indicate it's building a
15287c478bd9Sstevel@tonic-gate 			 * configuration file, not documented for general use.
15297c478bd9Sstevel@tonic-gate 			 */
15307c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
15317c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONFGEN;
15327c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_CONFIG_SIZE) && (strncmp(s1,
15337c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_CONFIG), MSG_LD_CONFIG_SIZE) == 0)) {
15347c478bd9Sstevel@tonic-gate 			/*
15357c478bd9Sstevel@tonic-gate 			 * Secure applications must use a default configuration
15367c478bd9Sstevel@tonic-gate 			 * file.  A setting from a configuration file doesn't
15377c478bd9Sstevel@tonic-gate 			 * make sense (given we must be reading a configuration
15387c478bd9Sstevel@tonic-gate 			 * file to have gotten this).
15397c478bd9Sstevel@tonic-gate 			 */
15407c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_SECURE) ||
15417c478bd9Sstevel@tonic-gate 			    (env_flags & ENV_TYP_CONFIG))
15427c478bd9Sstevel@tonic-gate 				return;
15437c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
15447c478bd9Sstevel@tonic-gate 			str = &config->c_name;
15457c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_CONFIG;
15467c478bd9Sstevel@tonic-gate 		}
15477c478bd9Sstevel@tonic-gate 	}
15487c478bd9Sstevel@tonic-gate 	/*
15497c478bd9Sstevel@tonic-gate 	 * The LD_DEBUG family and LD_DEMANGLE.
15507c478bd9Sstevel@tonic-gate 	 */
15517c478bd9Sstevel@tonic-gate 	else if (*s1 == 'D') {
15527c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_DEBUG_SIZE) && (strncmp(s1,
15537c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG), MSG_LD_DEBUG_SIZE) == 0)) {
15547c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1555fa034d7fSseizo 			if (select & SEL_REPLACE)
1556fa034d7fSseizo 				str = &rpl_debug;
1557fa034d7fSseizo 			else {
1558fa034d7fSseizo 				str = &prm_debug;
1559fa034d7fSseizo 				rpl_debug = 0;
1560fa034d7fSseizo 			}
15617c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG;
15627c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEBUG_OUTPUT_SIZE) && (strncmp(s1,
15637c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEBUG_OUTPUT),
15647c478bd9Sstevel@tonic-gate 		    MSG_LD_DEBUG_OUTPUT_SIZE) == 0)) {
15657c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
15667c478bd9Sstevel@tonic-gate 			str = &dbg_file;
15677c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEBUG_OUTPUT;
15687c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_DEMANGLE_SIZE) && (strncmp(s1,
15697c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_DEMANGLE), MSG_LD_DEMANGLE_SIZE) == 0)) {
15707c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
15717c478bd9Sstevel@tonic-gate 			val = RT_FL_DEMANGLE;
15727c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_DEMANGLE;
15737c478bd9Sstevel@tonic-gate 		}
15747c478bd9Sstevel@tonic-gate 	}
15757c478bd9Sstevel@tonic-gate 	/*
15767c478bd9Sstevel@tonic-gate 	 * LD_FLAGS - collect the best variable definition.  On completion of
15777c478bd9Sstevel@tonic-gate 	 * environment variable processing pass the result to ld_flags_env()
15787c478bd9Sstevel@tonic-gate 	 * where they'll be decomposed and passed back to this routine.
15797c478bd9Sstevel@tonic-gate 	 */
15807c478bd9Sstevel@tonic-gate 	else if (*s1 == 'F') {
15817c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_FLAGS_SIZE) && (strncmp(s1,
15827c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FLAGS), MSG_LD_FLAGS_SIZE) == 0)) {
15837c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1584fa034d7fSseizo 			if (select & SEL_REPLACE)
1585fa034d7fSseizo 				str = &rpl_ldflags;
1586fa034d7fSseizo 			else {
1587fa034d7fSseizo 				str = &prm_ldflags;
1588fa034d7fSseizo 				rpl_ldflags = 0;
1589fa034d7fSseizo 			}
15907c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_FLAGS;
15917c478bd9Sstevel@tonic-gate 		}
15927c478bd9Sstevel@tonic-gate 	}
15937c478bd9Sstevel@tonic-gate 	/*
15947c478bd9Sstevel@tonic-gate 	 * LD_INIT (internal, used by ldd(1)).
15957c478bd9Sstevel@tonic-gate 	 */
15967c478bd9Sstevel@tonic-gate 	else if (*s1 == 'I') {
15977c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_INIT_SIZE) && (strncmp(s1,
15987c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_INIT), MSG_LD_INIT_SIZE) == 0)) {
15997c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16007c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_INIT;
16017c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_INIT;
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 	}
16047c478bd9Sstevel@tonic-gate 	/*
16057c478bd9Sstevel@tonic-gate 	 * The LD_LIBRARY_PATH and LD_LOAD families.
16067c478bd9Sstevel@tonic-gate 	 */
16077c478bd9Sstevel@tonic-gate 	else if (*s1 == 'L') {
16087c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_LIBPATH_SIZE) && (strncmp(s1,
16097c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LIBPATH), MSG_LD_LIBPATH_SIZE) == 0)) {
16107c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_1;
1611fa034d7fSseizo 			if (select & SEL_REPLACE)
1612fa034d7fSseizo 				str = &rpl_libpath;
1613fa034d7fSseizo 			else {
1614fa034d7fSseizo 				str = &prm_libpath;
1615fa034d7fSseizo 				rpl_libpath = 0;
1616fa034d7fSseizo 			}
16177c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LIBPATH;
16187c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADAVAIL_SIZE) && (strncmp(s1,
16197c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADAVAIL), MSG_LD_LOADAVAIL_SIZE) == 0)) {
16207c478bd9Sstevel@tonic-gate 			/*
16217c478bd9Sstevel@tonic-gate 			 * Internal use by crle(1), not documented for general
16227c478bd9Sstevel@tonic-gate 			 * use.
16237c478bd9Sstevel@tonic-gate 			 */
16247c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
16257c478bd9Sstevel@tonic-gate 			val = LML_FLG_LOADAVAIL;
16267c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LOADAVAIL;
16277c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_LOADFLTR_SIZE) && (strncmp(s1,
16287c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_LOADFLTR), MSG_LD_LOADFLTR_SIZE) == 0)) {
16297c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
16307c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_LOADFLTR;
16317c478bd9Sstevel@tonic-gate 		}
16327c478bd9Sstevel@tonic-gate 	}
16337c478bd9Sstevel@tonic-gate 	/*
16347c478bd9Sstevel@tonic-gate 	 * The LD_NO family.
16357c478bd9Sstevel@tonic-gate 	 */
16367c478bd9Sstevel@tonic-gate 	else if (*s1 == 'N') {
16377c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_NOAUDIT_SIZE) && (strncmp(s1,
16387c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUDIT), MSG_LD_NOAUDIT_SIZE) == 0)) {
16397c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16407c478bd9Sstevel@tonic-gate 			val = RT_FL_NOAUDIT;
16417c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOAUDIT;
16427c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOAUXFLTR_SIZE) && (strncmp(s1,
16437c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOAUXFLTR), MSG_LD_NOAUXFLTR_SIZE) == 0)) {
16447c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16457c478bd9Sstevel@tonic-gate 			val = RT_FL_NOAUXFLTR;
16467c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOAUXFLTR;
16477c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOBAPLT_SIZE) && (strncmp(s1,
16487c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOBAPLT), MSG_LD_NOBAPLT_SIZE) == 0)) {
16497c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16507c478bd9Sstevel@tonic-gate 			val = RT_FL_NOBAPLT;
16517c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOBAPLT;
16527c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOCONFIG_SIZE) && (strncmp(s1,
16537c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOCONFIG), MSG_LD_NOCONFIG_SIZE) == 0)) {
16547c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16557c478bd9Sstevel@tonic-gate 			val = RT_FL_NOCFG;
16567c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOCONFIG;
16577c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRCONFIG_SIZE) && (strncmp(s1,
16587c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRCONFIG),
16597c478bd9Sstevel@tonic-gate 		    MSG_LD_NODIRCONFIG_SIZE) == 0)) {
16607c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16617c478bd9Sstevel@tonic-gate 			val = RT_FL_NODIRCFG;
16627c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NODIRCONFIG;
16637c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NODIRECT_SIZE) && (strncmp(s1,
16647c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NODIRECT), MSG_LD_NODIRECT_SIZE) == 0)) {
16657c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
16667c478bd9Sstevel@tonic-gate 			val = LML_TFLG_NODIRECT;
16677c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NODIRECT;
16687c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOENVCONFIG_SIZE) && (strncmp(s1,
16697c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOENVCONFIG),
16707c478bd9Sstevel@tonic-gate 		    MSG_LD_NOENVCONFIG_SIZE) == 0)) {
16717c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16727c478bd9Sstevel@tonic-gate 			val = RT_FL_NOENVCFG;
16737c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOENVCONFIG;
16747c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOFLTCONFIG_SIZE) && (strncmp(s1,
16757c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOFLTCONFIG),
16767c478bd9Sstevel@tonic-gate 		    MSG_LD_NOFLTCONFIG_SIZE) == 0)) {
16777c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT2;
16787c478bd9Sstevel@tonic-gate 			val = RT_FL2_NOFLTCFG;
16797c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOFLTCONFIG;
16807c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOLAZY_SIZE) && (strncmp(s1,
16817c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOLAZY), MSG_LD_NOLAZY_SIZE) == 0)) {
16827c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LMLT;
16837c478bd9Sstevel@tonic-gate 			val = LML_TFLG_NOLAZYLD;
16847c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOLAZY;
16857c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOOBJALTER_SIZE) && (strncmp(s1,
16867c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOOBJALTER),
16877c478bd9Sstevel@tonic-gate 		    MSG_LD_NOOBJALTER_SIZE) == 0)) {
16887c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16897c478bd9Sstevel@tonic-gate 			val = RT_FL_NOOBJALT;
16907c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOOBJALTER;
16917c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_NOVERSION_SIZE) && (strncmp(s1,
16927c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_NOVERSION), MSG_LD_NOVERSION_SIZE) == 0)) {
16937c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
16947c478bd9Sstevel@tonic-gate 			val = RT_FL_NOVERSION;
16957c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_NOVERSION;
16967c478bd9Sstevel@tonic-gate 		}
16977c478bd9Sstevel@tonic-gate 	}
16987c478bd9Sstevel@tonic-gate 	/*
16997c478bd9Sstevel@tonic-gate 	 * LD_ORIGIN.
17007c478bd9Sstevel@tonic-gate 	 */
17017c478bd9Sstevel@tonic-gate 	else if (*s1 == 'O') {
17027c478bd9Sstevel@tonic-gate #ifndef	EXPAND_RELATIVE
17037c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_ORIGIN_SIZE) && (strncmp(s1,
17047c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_ORIGIN), MSG_LD_ORIGIN_SIZE) == 0)) {
17057c478bd9Sstevel@tonic-gate 			/*
17067c478bd9Sstevel@tonic-gate 			 * Besides some old patches this is no longer required.
17077c478bd9Sstevel@tonic-gate 			 */
17087c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_RELATIVE;
17097c478bd9Sstevel@tonic-gate 		}
17107c478bd9Sstevel@tonic-gate #endif
17117c478bd9Sstevel@tonic-gate 		return;
17127c478bd9Sstevel@tonic-gate 	}
17137c478bd9Sstevel@tonic-gate 	/*
17147c478bd9Sstevel@tonic-gate 	 * LD_PRELOAD and LD_PROFILE family.
17157c478bd9Sstevel@tonic-gate 	 */
17167c478bd9Sstevel@tonic-gate 	else if (*s1 == 'P') {
17177c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_PRELOAD_SIZE) && (strncmp(s1,
17187c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PRELOAD), MSG_LD_PRELOAD_SIZE) == 0)) {
17197c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
1720fa034d7fSseizo 			if (select & SEL_REPLACE)
1721fa034d7fSseizo 				str = &rpl_preload;
1722fa034d7fSseizo 			else  {
1723fa034d7fSseizo 				str = &prm_preload;
1724fa034d7fSseizo 				rpl_preload = 0;
1725fa034d7fSseizo 			}
17267c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PRELOAD;
17277c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_SIZE) && (strncmp(s1,
17287c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE), MSG_LD_PROFILE_SIZE) == 0)) {
17297c478bd9Sstevel@tonic-gate 			/*
17307c478bd9Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
17317c478bd9Sstevel@tonic-gate 			 */
17327c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
17337c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE;
17347c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_PROFILE_OUTPUT_SIZE) && (strncmp(s1,
17357c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_PROFILE_OUTPUT),
17367c478bd9Sstevel@tonic-gate 		    MSG_LD_PROFILE_OUTPUT_SIZE) == 0)) {
17377c478bd9Sstevel@tonic-gate 			/*
17387c478bd9Sstevel@tonic-gate 			 * Only one user library can be profiled at a time.
17397c478bd9Sstevel@tonic-gate 			 */
17407c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_STR;
17417c478bd9Sstevel@tonic-gate 			str = &profile_out;
17427c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_PROFILE_OUTPUT;
17437c478bd9Sstevel@tonic-gate 		}
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 	/*
17467c478bd9Sstevel@tonic-gate 	 * LD_SIGNAL.
17477c478bd9Sstevel@tonic-gate 	 */
17487c478bd9Sstevel@tonic-gate 	else if (*s1 == 'S') {
17497c478bd9Sstevel@tonic-gate 		if (rtld_flags & RT_FL_SECURE)
17507c478bd9Sstevel@tonic-gate 			return;
17517c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_SIGNAL_SIZE) &&
17527c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_SIGNAL),
17537c478bd9Sstevel@tonic-gate 		    MSG_LD_SIGNAL_SIZE) == 0)) {
17547c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
17557c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_SIGNAL;
17567c478bd9Sstevel@tonic-gate 		}
17577c478bd9Sstevel@tonic-gate 	}
17587c478bd9Sstevel@tonic-gate 	/*
17591d1fba8aSrie 	 * The LD_TRACE family (internal, used by ldd(1)).  This definition is
17601d1fba8aSrie 	 * the key to enabling all other ldd(1) specific environment variables.
17611d1fba8aSrie 	 * In case an auditor is called, which in turn might exec(2) a
17621d1fba8aSrie 	 * subprocess, this variable is disabled, so that any subprocess
17631d1fba8aSrie 	 * escapes ldd(1) processing.
17647c478bd9Sstevel@tonic-gate 	 */
17657c478bd9Sstevel@tonic-gate 	else if (*s1 == 'T') {
17667c478bd9Sstevel@tonic-gate 		if (((len == MSG_LD_TRACE_OBJS_SIZE) &&
17677c478bd9Sstevel@tonic-gate 		    (strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS),
17687c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_SIZE) == 0)) ||
17697c478bd9Sstevel@tonic-gate 		    ((len == MSG_LD_TRACE_OBJS_E_SIZE) &&
17707c478bd9Sstevel@tonic-gate 		    (((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_E),
17717c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_E_SIZE) == 0) && !aout) ||
17727c478bd9Sstevel@tonic-gate 		    ((strncmp(s1, MSG_ORIG(MSG_LD_TRACE_OBJS_A),
17737c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_OBJS_A_SIZE) == 0) && aout)))) {
17741d1fba8aSrie 			char	*s0 = (char *)s1;
17751d1fba8aSrie 
17767c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_SPEC_2;
17777c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_OBJS;
17781d1fba8aSrie 
177902ca3e02Srie #if	defined(__sparc) || defined(__x86)
17801d1fba8aSrie 			/*
17811d1fba8aSrie 			 * The simplest way to "disable" this variable is to
17821d1fba8aSrie 			 * truncate this string to "LD_'\0'". This string is
17831d1fba8aSrie 			 * ignored by any ld.so.1 environment processing.
17841d1fba8aSrie 			 * Use of such interfaces as unsetenv(3c) are overkill,
17851d1fba8aSrie 			 * and would drag too much libc implementation detail
17861d1fba8aSrie 			 * into ld.so.1.
17871d1fba8aSrie 			 */
1788a953e2b1Srie 			*s0 = '\0';
17891d1fba8aSrie #else
17901d1fba8aSrie /*
17911d1fba8aSrie  * Verify that the above write is appropriate for any new platforms.
17921d1fba8aSrie  */
17931d1fba8aSrie #error	unsupported architecture!
17941d1fba8aSrie #endif
17957c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_TRACE_PTHS_SIZE) && (strncmp(s1,
17967c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_TRACE_PTHS),
17977c478bd9Sstevel@tonic-gate 		    MSG_LD_TRACE_PTHS_SIZE) == 0)) {
17987c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
17997c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_SEARCH;
18007c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_TRACE_PTHS;
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 	}
18037c478bd9Sstevel@tonic-gate 	/*
18047c478bd9Sstevel@tonic-gate 	 * LD_UNREF and LD_UNUSED (internal, used by ldd(1)).
18057c478bd9Sstevel@tonic-gate 	 */
18067c478bd9Sstevel@tonic-gate 	else if (*s1 == 'U') {
18077c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_UNREF_SIZE) && (strncmp(s1,
18087c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNREF), MSG_LD_UNREF_SIZE) == 0)) {
18097c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18107c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_UNREF;
18117c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_UNREF;
18127c478bd9Sstevel@tonic-gate 		} else if ((len == MSG_LD_UNUSED_SIZE) && (strncmp(s1,
18137c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_UNUSED), MSG_LD_UNUSED_SIZE) == 0)) {
18147c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18157c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_UNUSED;
18167c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_UNUSED;
18177c478bd9Sstevel@tonic-gate 		}
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 	/*
18207c478bd9Sstevel@tonic-gate 	 * LD_VERBOSE (internal, used by ldd(1)).
18217c478bd9Sstevel@tonic-gate 	 */
18227c478bd9Sstevel@tonic-gate 	else if (*s1 == 'V') {
18237c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_VERBOSE_SIZE) && (strncmp(s1,
18247c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_VERBOSE), MSG_LD_VERBOSE_SIZE) == 0)) {
18257c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18267c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_VERBOSE;
18277c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_VERBOSE;
18287c478bd9Sstevel@tonic-gate 		}
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 	/*
18317c478bd9Sstevel@tonic-gate 	 * LD_WARN (internal, used by ldd(1)).
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate 	else if (*s1 == 'W') {
18347c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_WARN_SIZE) && (strncmp(s1,
18357c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_WARN), MSG_LD_WARN_SIZE) == 0)) {
18367c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_LML;
18377c478bd9Sstevel@tonic-gate 			val = LML_FLG_TRC_WARN;
18387c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_WARN;
18397c478bd9Sstevel@tonic-gate 		}
18407c478bd9Sstevel@tonic-gate #ifdef	SIEBEL_DISABLE
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate 	/*
18437c478bd9Sstevel@tonic-gate 	 * LD__FIX__ (undocumented, enable future technology that can't be
18447c478bd9Sstevel@tonic-gate 	 * delivered in a patch release).
18457c478bd9Sstevel@tonic-gate 	 */
18467c478bd9Sstevel@tonic-gate 	else if (*s1 == '_') {
18477c478bd9Sstevel@tonic-gate 		if ((len == MSG_LD_FIX_1_SIZE) && (strncmp(s1,
18487c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_LD_FIX_1), MSG_LD_FIX_1_SIZE) == 0)) {
18497c478bd9Sstevel@tonic-gate 			select |= SEL_ACT_RT;
18507c478bd9Sstevel@tonic-gate 			val = RT_FL_DISFIX_1;
18517c478bd9Sstevel@tonic-gate 			variable = ENV_FLG_FIX_1;
18527c478bd9Sstevel@tonic-gate 		}
18537c478bd9Sstevel@tonic-gate #endif
18547c478bd9Sstevel@tonic-gate 	}
18557c478bd9Sstevel@tonic-gate 	if (variable == 0)
18567c478bd9Sstevel@tonic-gate 		return;
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 	/*
18597c478bd9Sstevel@tonic-gate 	 * If the variable is already processed with ISA specific variable,
18607c478bd9Sstevel@tonic-gate 	 * no further processing needed.
18617c478bd9Sstevel@tonic-gate 	 */
18627c478bd9Sstevel@tonic-gate 	if (((select & SEL_REPLACE) && (rplisa & variable)) ||
18637c478bd9Sstevel@tonic-gate 	    ((select & SEL_PERMANT) && (prmisa & variable)))
18647c478bd9Sstevel@tonic-gate 		return;
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 	/*
1867fa034d7fSseizo 	 * Now mark the appropriate variables.
1868fa034d7fSseizo 	 * If the replaceable variable is already set, then the
1869fa034d7fSseizo 	 * process environment variable must be set. Any replaceable
1870fa034d7fSseizo 	 * variable specified in a configuration file can be ignored.
18717c478bd9Sstevel@tonic-gate 	 */
18727c478bd9Sstevel@tonic-gate 	if (env_flags & ENV_TYP_ISA) {
18737c478bd9Sstevel@tonic-gate 		/*
18747c478bd9Sstevel@tonic-gate 		 * This is ISA setting. We do the setting
18757c478bd9Sstevel@tonic-gate 		 * even if s2 is NULL.
18767c478bd9Sstevel@tonic-gate 		 * If s2 is NULL, we might need to undo
18777c478bd9Sstevel@tonic-gate 		 * the setting.
18787c478bd9Sstevel@tonic-gate 		 */
18797c478bd9Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1880fa034d7fSseizo 			if (rplisa & variable)
1881fa034d7fSseizo 				return;
18827c478bd9Sstevel@tonic-gate 			rplisa |= variable;
18837c478bd9Sstevel@tonic-gate 		} else {
18847c478bd9Sstevel@tonic-gate 			prmisa |= variable;
18857c478bd9Sstevel@tonic-gate 		}
18867c478bd9Sstevel@tonic-gate 	} else if (s2) {
18877c478bd9Sstevel@tonic-gate 		/*
18887c478bd9Sstevel@tonic-gate 		 * This is non0-ISA setting
18897c478bd9Sstevel@tonic-gate 		 */
18907c478bd9Sstevel@tonic-gate 		if (select & SEL_REPLACE) {
1891fa034d7fSseizo 			if (rplgen & variable)
1892fa034d7fSseizo 				return;
18937c478bd9Sstevel@tonic-gate 			rplgen |= variable;
18947c478bd9Sstevel@tonic-gate 		} else
18957c478bd9Sstevel@tonic-gate 			prmgen |= variable;
18967c478bd9Sstevel@tonic-gate 	} else
18977c478bd9Sstevel@tonic-gate 		/*
18987c478bd9Sstevel@tonic-gate 		 * This is non-ISA setting which
18997c478bd9Sstevel@tonic-gate 		 * can be ignored.
19007c478bd9Sstevel@tonic-gate 		 */
19017c478bd9Sstevel@tonic-gate 		return;
19027c478bd9Sstevel@tonic-gate 
19037c478bd9Sstevel@tonic-gate 	/*
19047c478bd9Sstevel@tonic-gate 	 * Now perform the setting.
19057c478bd9Sstevel@tonic-gate 	 */
19067c478bd9Sstevel@tonic-gate 	if (select & SEL_ACT_RT) {
19077c478bd9Sstevel@tonic-gate 		if (s2)
19087c478bd9Sstevel@tonic-gate 			rtld_flags |= val;
19097c478bd9Sstevel@tonic-gate 		else
19107c478bd9Sstevel@tonic-gate 			rtld_flags &= ~val;
19117c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_RT2) {
19127c478bd9Sstevel@tonic-gate 		if (s2)
19137c478bd9Sstevel@tonic-gate 			rtld_flags2 |= val;
19147c478bd9Sstevel@tonic-gate 		else
19157c478bd9Sstevel@tonic-gate 			rtld_flags2 &= ~val;
19167c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_STR)
19177c478bd9Sstevel@tonic-gate 		*str = s2;
19187c478bd9Sstevel@tonic-gate 	else if (select & SEL_ACT_LML) {
19197c478bd9Sstevel@tonic-gate 		if (s2)
19207c478bd9Sstevel@tonic-gate 			*lmflags |= val;
19217c478bd9Sstevel@tonic-gate 		else
19227c478bd9Sstevel@tonic-gate 			*lmflags &= ~val;
19237c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_LMLT) {
19247c478bd9Sstevel@tonic-gate 		if (s2)
19257c478bd9Sstevel@tonic-gate 			*lmtflags |= val;
19267c478bd9Sstevel@tonic-gate 		else
19277c478bd9Sstevel@tonic-gate 			*lmtflags &= ~val;
19287c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_1) {
19297c478bd9Sstevel@tonic-gate 		/*
19307c478bd9Sstevel@tonic-gate 		 * variable is either ENV_FLG_FLAGS or ENV_FLG_LIBPATH
19317c478bd9Sstevel@tonic-gate 		 */
19327c478bd9Sstevel@tonic-gate 		*str = s2;
19337c478bd9Sstevel@tonic-gate 		if ((select & SEL_REPLACE) && (env_flags & ENV_TYP_CONFIG)) {
19347c478bd9Sstevel@tonic-gate 			if (s2) {
19357c478bd9Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
19367c478bd9Sstevel@tonic-gate 					env_info |= ENV_INF_FLAGCFG;
19377c478bd9Sstevel@tonic-gate 				else
19387c478bd9Sstevel@tonic-gate 					env_info |= ENV_INF_PATHCFG;
19397c478bd9Sstevel@tonic-gate 			} else {
19407c478bd9Sstevel@tonic-gate 				if (variable == ENV_FLG_FLAGS)
19417c478bd9Sstevel@tonic-gate 					env_info &= ~ENV_INF_FLAGCFG;
19427c478bd9Sstevel@tonic-gate 				else
19437c478bd9Sstevel@tonic-gate 					env_info &= ~ENV_INF_PATHCFG;
19447c478bd9Sstevel@tonic-gate 			}
19457c478bd9Sstevel@tonic-gate 		}
19467c478bd9Sstevel@tonic-gate 	} else if (select & SEL_ACT_SPEC_2) {
19477c478bd9Sstevel@tonic-gate 		/*
19487c478bd9Sstevel@tonic-gate 		 * variables can be: ENV_FLG_
19497c478bd9Sstevel@tonic-gate 		 * 	AUDIT_ARGS, BINDING, CONCURRENCY, CONFGEN,
19507c478bd9Sstevel@tonic-gate 		 *	LOADFLTR, PROFILE, SIGNAL, TRACE_OBJS
19517c478bd9Sstevel@tonic-gate 		 */
19527c478bd9Sstevel@tonic-gate 		if (variable == ENV_FLG_AUDIT_ARGS) {
19537c478bd9Sstevel@tonic-gate 			if (s2) {
19547c478bd9Sstevel@tonic-gate 				audit_argcnt = atoi(s2);
19557c478bd9Sstevel@tonic-gate 				audit_argcnt += audit_argcnt % 2;
19567c478bd9Sstevel@tonic-gate 			} else
19577c478bd9Sstevel@tonic-gate 				audit_argcnt = 0;
19587c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_BINDINGS) {
19597c478bd9Sstevel@tonic-gate 			if (s2)
19607c478bd9Sstevel@tonic-gate 				rpl_debug = MSG_ORIG(MSG_TKN_BINDINGS);
19617c478bd9Sstevel@tonic-gate 			else
19627c478bd9Sstevel@tonic-gate 				rpl_debug = 0;
19637c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_CONCURRENCY) {
19647c478bd9Sstevel@tonic-gate 			if (s2)
19657c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_NOCONCUR;
19667c478bd9Sstevel@tonic-gate 			else
19677c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_NOCONCUR;
19687c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_CONFGEN) {
19697c478bd9Sstevel@tonic-gate 			if (s2) {
19707c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_CONFGEN;
19717c478bd9Sstevel@tonic-gate 				*lmflags |= LML_FLG_IGNRELERR;
19727c478bd9Sstevel@tonic-gate 			} else {
19737c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_CONFGEN;
19747c478bd9Sstevel@tonic-gate 				*lmflags &= ~LML_FLG_IGNRELERR;
19757c478bd9Sstevel@tonic-gate 			}
19767c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_LOADFLTR) {
19777c478bd9Sstevel@tonic-gate 			if (s2) {
19787c478bd9Sstevel@tonic-gate 				*lmtflags |= LML_TFLG_LOADFLTR;
19797c478bd9Sstevel@tonic-gate 				if (*s2 == '2')
19807c478bd9Sstevel@tonic-gate 					rtld_flags |= RT_FL_WARNFLTR;
19817c478bd9Sstevel@tonic-gate 			} else {
19827c478bd9Sstevel@tonic-gate 				*lmtflags &= ~LML_TFLG_LOADFLTR;
19837c478bd9Sstevel@tonic-gate 				rtld_flags &= ~RT_FL_WARNFLTR;
19847c478bd9Sstevel@tonic-gate 			}
19857c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_PROFILE) {
19867c478bd9Sstevel@tonic-gate 			profile_name = s2;
19877c478bd9Sstevel@tonic-gate 			if (s2) {
19887c478bd9Sstevel@tonic-gate 				if (strcmp(s2, MSG_ORIG(MSG_FIL_RTLD)) == 0) {
19897c478bd9Sstevel@tonic-gate 					return;
19907c478bd9Sstevel@tonic-gate 				}
1991a953e2b1Srie 				/* BEGIN CSTYLED */
19927c478bd9Sstevel@tonic-gate 				if (rtld_flags & RT_FL_SECURE) {
19937c478bd9Sstevel@tonic-gate 					profile_lib =
19947c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
19957c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE_64);
19967c478bd9Sstevel@tonic-gate #else
19977c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROFSE);
19987c478bd9Sstevel@tonic-gate #endif
19997c478bd9Sstevel@tonic-gate 				} else {
20007c478bd9Sstevel@tonic-gate 					profile_lib =
20017c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
20027c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF_64);
20037c478bd9Sstevel@tonic-gate #else
20047c478bd9Sstevel@tonic-gate 					    MSG_ORIG(MSG_PTH_LDPROF);
20057c478bd9Sstevel@tonic-gate #endif
20067c478bd9Sstevel@tonic-gate 				}
2007a953e2b1Srie 				/* END CSTYLED */
20087c478bd9Sstevel@tonic-gate 			} else
20097c478bd9Sstevel@tonic-gate 				profile_lib = 0;
20107c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_SIGNAL) {
20117c478bd9Sstevel@tonic-gate 			killsig = s2 ? atoi(s2) : SIGKILL;
20127c478bd9Sstevel@tonic-gate 		} else if (variable == ENV_FLG_TRACE_OBJS) {
20137c478bd9Sstevel@tonic-gate 			if (s2) {
20147c478bd9Sstevel@tonic-gate 				*lmflags |= LML_FLG_TRC_ENABLE;
20157c478bd9Sstevel@tonic-gate 				if (*s2 == '2')
20167c478bd9Sstevel@tonic-gate 					*lmflags |= LML_FLG_TRC_LDDSTUB;
20177c478bd9Sstevel@tonic-gate 			} else
20187c478bd9Sstevel@tonic-gate 				*lmflags &=
2019a953e2b1Srie 				    ~(LML_FLG_TRC_ENABLE|LML_FLG_TRC_LDDSTUB);
20207c478bd9Sstevel@tonic-gate 		}
20217c478bd9Sstevel@tonic-gate 	}
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate /*
20257c478bd9Sstevel@tonic-gate  * Determine whether we have an architecture specific environment variable.
20267c478bd9Sstevel@tonic-gate  * If we do, and we're the wrong architecture, it'll just get ignored.
20277c478bd9Sstevel@tonic-gate  * Otherwise the variable is processed in it's architecture neutral form.
20287c478bd9Sstevel@tonic-gate  */
20297c478bd9Sstevel@tonic-gate static int
20307c478bd9Sstevel@tonic-gate ld_arch_env(const char *s1, size_t *len)
20317c478bd9Sstevel@tonic-gate {
20327c478bd9Sstevel@tonic-gate 	size_t	_len = *len - 3;
20337c478bd9Sstevel@tonic-gate 
20347c478bd9Sstevel@tonic-gate 	if (s1[_len++] == '_') {
20357c478bd9Sstevel@tonic-gate 		if ((s1[_len] == '3') && (s1[_len + 1] == '2')) {
20367c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
20377c478bd9Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
20387c478bd9Sstevel@tonic-gate #else
20397c478bd9Sstevel@tonic-gate 			*len = *len - 3;
20407c478bd9Sstevel@tonic-gate 			return (ENV_TYP_ISA);
20417c478bd9Sstevel@tonic-gate #endif
20427c478bd9Sstevel@tonic-gate 		}
20437c478bd9Sstevel@tonic-gate 		if ((s1[_len] == '6') && (s1[_len + 1] == '4')) {
20447c478bd9Sstevel@tonic-gate #if	defined(_ELF64)
20457c478bd9Sstevel@tonic-gate 			*len = *len - 3;
20467c478bd9Sstevel@tonic-gate 			return (ENV_TYP_ISA);
20477c478bd9Sstevel@tonic-gate #else
20487c478bd9Sstevel@tonic-gate 			return (ENV_TYP_IGNORE);
20497c478bd9Sstevel@tonic-gate #endif
20507c478bd9Sstevel@tonic-gate 		}
20517c478bd9Sstevel@tonic-gate 	}
20527c478bd9Sstevel@tonic-gate 	return (0);
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate /*
20577c478bd9Sstevel@tonic-gate  * Process an LD_FLAGS environment variable.  The value can be a comma
20587c478bd9Sstevel@tonic-gate  * separated set of tokens, which are sent (in upper case) into the generic
20597c478bd9Sstevel@tonic-gate  * LD_XXXX environment variable engine.  For example:
20607c478bd9Sstevel@tonic-gate  *
20617c478bd9Sstevel@tonic-gate  *	LD_FLAGS=bind_now		->	LD_BIND_NOW=1
20627c478bd9Sstevel@tonic-gate  *	LD_FLAGS=library_path=/foo:.	->	LD_LIBRARY_PATH=/foo:.
20637c478bd9Sstevel@tonic-gate  *	LD_FLAGS=debug=files:detail	->	LD_DEBUG=files:detail
20647c478bd9Sstevel@tonic-gate  * or
20657c478bd9Sstevel@tonic-gate  *	LD_FLAGS=bind_now,library_path=/foo:.,debug=files:detail
20667c478bd9Sstevel@tonic-gate  */
20677c478bd9Sstevel@tonic-gate static int
20687c478bd9Sstevel@tonic-gate ld_flags_env(const char *str, Word *lmflags, Word *lmtflags,
20697c478bd9Sstevel@tonic-gate     uint_t env_flags, int aout)
20707c478bd9Sstevel@tonic-gate {
20717c478bd9Sstevel@tonic-gate 	char	*nstr, *sstr, *estr = 0;
20727c478bd9Sstevel@tonic-gate 	size_t	nlen, len;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	if (str == 0)
20757c478bd9Sstevel@tonic-gate 		return (0);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 	/*
20787c478bd9Sstevel@tonic-gate 	 * Create a new string as we're going to transform the token(s) into
20797c478bd9Sstevel@tonic-gate 	 * uppercase and separate tokens with nulls.
20807c478bd9Sstevel@tonic-gate 	 */
20817c478bd9Sstevel@tonic-gate 	len = strlen(str);
20827c478bd9Sstevel@tonic-gate 	if ((nstr = malloc(len + 1)) == 0)
20837c478bd9Sstevel@tonic-gate 		return (1);
20847c478bd9Sstevel@tonic-gate 	(void) strcpy(nstr, str);
20857c478bd9Sstevel@tonic-gate 
20867c478bd9Sstevel@tonic-gate 	for (sstr = nstr; sstr; sstr++, len--) {
20877c478bd9Sstevel@tonic-gate 		int	flags;
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 		if ((*sstr != '\0') && (*sstr != ',')) {
20907c478bd9Sstevel@tonic-gate 			if (estr == 0) {
20917c478bd9Sstevel@tonic-gate 				if (*sstr == '=')
20927c478bd9Sstevel@tonic-gate 					estr = sstr;
20937c478bd9Sstevel@tonic-gate 				else {
20947c478bd9Sstevel@tonic-gate 					/*
20957c478bd9Sstevel@tonic-gate 					 * Translate token to uppercase.  Don't
20967c478bd9Sstevel@tonic-gate 					 * use toupper(3C) as including this
20977c478bd9Sstevel@tonic-gate 					 * code doubles the size of ld.so.1.
20987c478bd9Sstevel@tonic-gate 					 */
20997c478bd9Sstevel@tonic-gate 					if ((*sstr >= 'a') && (*sstr <= 'z'))
21007c478bd9Sstevel@tonic-gate 						*sstr = *sstr - ('a' - 'A');
21017c478bd9Sstevel@tonic-gate 				}
21027c478bd9Sstevel@tonic-gate 			}
21037c478bd9Sstevel@tonic-gate 			continue;
21047c478bd9Sstevel@tonic-gate 		}
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate 		*sstr = '\0';
21077c478bd9Sstevel@tonic-gate 		if (estr) {
21087c478bd9Sstevel@tonic-gate 			nlen = estr - nstr;
21097c478bd9Sstevel@tonic-gate 			if ((*++estr == '\0') || (*estr == ','))
21107c478bd9Sstevel@tonic-gate 				estr = 0;
21117c478bd9Sstevel@tonic-gate 		} else
21127c478bd9Sstevel@tonic-gate 			nlen = sstr - nstr;
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate 		/*
21157c478bd9Sstevel@tonic-gate 		 * Fabricate a boolean definition for any unqualified variable.
21167c478bd9Sstevel@tonic-gate 		 * Thus LD_FLAGS=bind_now is represented as BIND_NOW=(null).
21177c478bd9Sstevel@tonic-gate 		 * The value is sufficient to assert any boolean variables, plus
21187c478bd9Sstevel@tonic-gate 		 * the term "(null)" is specifically chosen in case someone
21197c478bd9Sstevel@tonic-gate 		 * mistakenly supplies something like LD_FLAGS=library_path.
21207c478bd9Sstevel@tonic-gate 		 */
21217c478bd9Sstevel@tonic-gate 		if (estr == 0)
21227c478bd9Sstevel@tonic-gate 			estr = (char *)MSG_INTL(MSG_STR_NULL);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 		/*
21257c478bd9Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
21267c478bd9Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
21277c478bd9Sstevel@tonic-gate 		 * neutral portion of the string.
21287c478bd9Sstevel@tonic-gate 		 */
21297c478bd9Sstevel@tonic-gate 		if ((flags = ld_arch_env(nstr, &nlen)) != ENV_TYP_IGNORE) {
21307c478bd9Sstevel@tonic-gate 			ld_generic_env(nstr, nlen, estr, lmflags,
21317c478bd9Sstevel@tonic-gate 			    lmtflags, (env_flags | flags), aout);
21327c478bd9Sstevel@tonic-gate 		}
21337c478bd9Sstevel@tonic-gate 		if (len == 0)
21347c478bd9Sstevel@tonic-gate 			return (0);
21357c478bd9Sstevel@tonic-gate 
21367c478bd9Sstevel@tonic-gate 		nstr = sstr + 1;
21377c478bd9Sstevel@tonic-gate 		estr = 0;
21387c478bd9Sstevel@tonic-gate 	}
21397c478bd9Sstevel@tonic-gate 	return (0);
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate /*
21447c478bd9Sstevel@tonic-gate  * Process a single environment string.  Only strings starting with `LD_' are
21457c478bd9Sstevel@tonic-gate  * reserved for our use.  By convention, all strings should be of the form
21467c478bd9Sstevel@tonic-gate  * `LD_XXXX=', if the string is followed by a non-null value the appropriate
21477c478bd9Sstevel@tonic-gate  * functionality is enabled.  Also pick off applicable locale variables.
21487c478bd9Sstevel@tonic-gate  */
21497c478bd9Sstevel@tonic-gate #define	LOC_LANG	1
21507c478bd9Sstevel@tonic-gate #define	LOC_MESG	2
21517c478bd9Sstevel@tonic-gate #define	LOC_ALL		3
21527c478bd9Sstevel@tonic-gate 
21537c478bd9Sstevel@tonic-gate static void
21547c478bd9Sstevel@tonic-gate ld_str_env(const char *s1, Word *lmflags, Word *lmtflags, uint_t env_flags,
21557c478bd9Sstevel@tonic-gate     int aout)
21567c478bd9Sstevel@tonic-gate {
21577c478bd9Sstevel@tonic-gate 	const char	*s2;
215810a4fa49Srie 	static		size_t	loc = 0;
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 	if (*s1++ != 'L')
21617c478bd9Sstevel@tonic-gate 		return;
21627c478bd9Sstevel@tonic-gate 
21637c478bd9Sstevel@tonic-gate 	/*
21647c478bd9Sstevel@tonic-gate 	 * See if we have any locale environment settings.  These environment
21657c478bd9Sstevel@tonic-gate 	 * variables have a precedence, LC_ALL is higher than LC_MESSAGES which
21667c478bd9Sstevel@tonic-gate 	 * is higher than LANG.
21677c478bd9Sstevel@tonic-gate 	 */
21687c478bd9Sstevel@tonic-gate 	s2 = s1;
21697c478bd9Sstevel@tonic-gate 	if ((*s2++ == 'C') && (*s2++ == '_') && (*s2 != '\0')) {
21707c478bd9Sstevel@tonic-gate 		if (strncmp(s2, MSG_ORIG(MSG_LC_ALL), MSG_LC_ALL_SIZE) == 0) {
21717c478bd9Sstevel@tonic-gate 			s2 += MSG_LC_ALL_SIZE;
21727c478bd9Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_ALL)) {
217310a4fa49Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
21747c478bd9Sstevel@tonic-gate 				loc = LOC_ALL;
21757c478bd9Sstevel@tonic-gate 			}
21767c478bd9Sstevel@tonic-gate 		} else if (strncmp(s2, MSG_ORIG(MSG_LC_MESSAGES),
21777c478bd9Sstevel@tonic-gate 		    MSG_LC_MESSAGES_SIZE) == 0) {
21787c478bd9Sstevel@tonic-gate 			s2 += MSG_LC_MESSAGES_SIZE;
21797c478bd9Sstevel@tonic-gate 			if ((*s2 != '\0') && (loc < LOC_MESG)) {
218010a4fa49Srie 				glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
21817c478bd9Sstevel@tonic-gate 				loc = LOC_MESG;
21827c478bd9Sstevel@tonic-gate 			}
21837c478bd9Sstevel@tonic-gate 		}
21847c478bd9Sstevel@tonic-gate 		return;
21857c478bd9Sstevel@tonic-gate 	}
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 	s2 = s1;
21887c478bd9Sstevel@tonic-gate 	if ((*s2++ == 'A') && (*s2++ == 'N') && (*s2++ == 'G') &&
21897c478bd9Sstevel@tonic-gate 	    (*s2++ == '=') && (*s2 != '\0') && (loc < LOC_LANG)) {
219010a4fa49Srie 		glcs[CI_LCMESSAGES].lc_un.lc_ptr = (char *)s2;
21917c478bd9Sstevel@tonic-gate 		loc = LOC_LANG;
21927c478bd9Sstevel@tonic-gate 		return;
21937c478bd9Sstevel@tonic-gate 	}
21947c478bd9Sstevel@tonic-gate 
21957c478bd9Sstevel@tonic-gate 	/*
21967c478bd9Sstevel@tonic-gate 	 * Pick off any LD_XXXX environment variables.
21977c478bd9Sstevel@tonic-gate 	 */
21987c478bd9Sstevel@tonic-gate 	if ((*s1++ == 'D') && (*s1++ == '_') && (*s1 != '\0')) {
21997c478bd9Sstevel@tonic-gate 		size_t	len;
22007c478bd9Sstevel@tonic-gate 		int	flags;
22017c478bd9Sstevel@tonic-gate 
22029acbbeafSnn 		/*
22039acbbeafSnn 		 * In a branded process we must ignore all LD_XXXX env vars
22049acbbeafSnn 		 * because they are intended for the brand's linker.
22059acbbeafSnn 		 * To affect the Solaris linker, use LD_BRAND_XXXX instead.
22069acbbeafSnn 		 */
22079acbbeafSnn 		if (rtld_flags2 & RT_FL2_BRANDED) {
22089acbbeafSnn 			if (strncmp(s1, MSG_ORIG(MSG_LD_BRAND_PREFIX),
22099acbbeafSnn 			    MSG_LD_BRAND_PREFIX_SIZE) != 0)
22109acbbeafSnn 				return;
22119acbbeafSnn 			s1 += MSG_LD_BRAND_PREFIX_SIZE;
22129acbbeafSnn 		}
22139acbbeafSnn 
22147c478bd9Sstevel@tonic-gate 		/*
22157c478bd9Sstevel@tonic-gate 		 * Environment variables with no value (ie. LD_XXXX=) typically
22167c478bd9Sstevel@tonic-gate 		 * have no impact, however if environment variables are defined
22177c478bd9Sstevel@tonic-gate 		 * within a configuration file, these null user settings can be
22187c478bd9Sstevel@tonic-gate 		 * used to disable any configuration replaceable definitions.
22197c478bd9Sstevel@tonic-gate 		 */
22207c478bd9Sstevel@tonic-gate 		if ((s2 = strchr(s1, '=')) == 0) {
22217c478bd9Sstevel@tonic-gate 			len = strlen(s1);
22227c478bd9Sstevel@tonic-gate 			s2 = 0;
22237c478bd9Sstevel@tonic-gate 		} else if (*++s2 == '\0') {
22247c478bd9Sstevel@tonic-gate 			len = strlen(s1) - 1;
22257c478bd9Sstevel@tonic-gate 			s2 = 0;
22267c478bd9Sstevel@tonic-gate 		} else {
22277c478bd9Sstevel@tonic-gate 			len = s2 - s1 - 1;
22287c478bd9Sstevel@tonic-gate 			while (isspace(*s2))
22297c478bd9Sstevel@tonic-gate 				s2++;
22307c478bd9Sstevel@tonic-gate 		}
22317c478bd9Sstevel@tonic-gate 
22327c478bd9Sstevel@tonic-gate 		/*
22337c478bd9Sstevel@tonic-gate 		 * Determine whether the environment variable is 32- or 64-bit
22347c478bd9Sstevel@tonic-gate 		 * specific.  The length, len, will reflect the architecture
22357c478bd9Sstevel@tonic-gate 		 * neutral portion of the string.
22367c478bd9Sstevel@tonic-gate 		 */
22377c478bd9Sstevel@tonic-gate 		if ((flags = ld_arch_env(s1, &len)) == ENV_TYP_IGNORE)
22387c478bd9Sstevel@tonic-gate 			return;
22397c478bd9Sstevel@tonic-gate 		env_flags |= flags;
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate 		ld_generic_env(s1, len, s2, lmflags, lmtflags, env_flags, aout);
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate }
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate /*
22467c478bd9Sstevel@tonic-gate  * Internal getenv routine.  Called immediately after ld.so.1 initializes
22477c478bd9Sstevel@tonic-gate  * itself.
22487c478bd9Sstevel@tonic-gate  */
22497c478bd9Sstevel@tonic-gate int
22507c478bd9Sstevel@tonic-gate readenv_user(const char ** envp, Word *lmflags, Word *lmtflags, int aout)
22517c478bd9Sstevel@tonic-gate {
225210a4fa49Srie 	char	*locale;
225310a4fa49Srie 
22547c478bd9Sstevel@tonic-gate 	if (envp == (const char **)0)
22557c478bd9Sstevel@tonic-gate 		return (0);
22567c478bd9Sstevel@tonic-gate 
22577c478bd9Sstevel@tonic-gate 	while (*envp != (const char *)0)
22587c478bd9Sstevel@tonic-gate 		ld_str_env(*envp++, lmflags, lmtflags, 0, aout);
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	/*
22617c478bd9Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
22627c478bd9Sstevel@tonic-gate 	 * these strings.
22637c478bd9Sstevel@tonic-gate 	 */
22647c478bd9Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
22657c478bd9Sstevel@tonic-gate 		return (1);
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	/*
22687c478bd9Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
22697c478bd9Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
22707c478bd9Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
22717c478bd9Sstevel@tonic-gate 	 */
22727c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
22737c478bd9Sstevel@tonic-gate 		rpl_audit = profile_lib = profile_name = 0;
22747c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
22757c478bd9Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
22767c478bd9Sstevel@tonic-gate 
2277dffec89cSrie 	/*
2278dffec89cSrie 	 * If both LD_BIND_NOW and LD_BIND_LAZY are specified, the former wins.
2279dffec89cSrie 	 */
2280dffec89cSrie 	if ((rtld_flags2 & (RT_FL2_BINDNOW | RT_FL2_BINDLAZY)) ==
2281dffec89cSrie 	    (RT_FL2_BINDNOW | RT_FL2_BINDLAZY))
2282dffec89cSrie 		rtld_flags2 &= ~RT_FL2_BINDLAZY;
2283dffec89cSrie 
22847c478bd9Sstevel@tonic-gate 	/*
22857c478bd9Sstevel@tonic-gate 	 * If we have a locale setting make sure its worth processing further.
22861d6b7ad8Srie 	 * C and POSIX locales don't need any processing.  In addition, to
22871d6b7ad8Srie 	 * ensure no one escapes the /usr/lib/locale hierarchy, don't allow
22881d6b7ad8Srie 	 * the locale to contain a segment that leads upward in the file system
22891d6b7ad8Srie 	 * hierarchy (i.e. no '..' segments).   Given that we'll be confined to
22901d6b7ad8Srie 	 * the /usr/lib/locale hierarchy, there is no need to extensively
22911d6b7ad8Srie 	 * validate the mode or ownership of any message file (as libc's
22921d6b7ad8Srie 	 * generic handling of message files does).  Duplicate the string so
22931d6b7ad8Srie 	 * that new locale setting can generically cleanup any previous locales.
22947c478bd9Sstevel@tonic-gate 	 */
229510a4fa49Srie 	if ((locale = glcs[CI_LCMESSAGES].lc_un.lc_ptr) != 0) {
22967c478bd9Sstevel@tonic-gate 		if (((*locale == 'C') && (*(locale + 1) == '\0')) ||
22971d6b7ad8Srie 		    (strcmp(locale, MSG_ORIG(MSG_TKN_POSIX)) == 0) ||
22981d6b7ad8Srie 		    (strstr(locale, MSG_ORIG(MSG_TKN_DOTDOT)) != NULL))
229910a4fa49Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = 0;
23007c478bd9Sstevel@tonic-gate 		else
230110a4fa49Srie 			glcs[CI_LCMESSAGES].lc_un.lc_ptr = strdup(locale);
23027c478bd9Sstevel@tonic-gate 	}
23037c478bd9Sstevel@tonic-gate 	return (0);
23047c478bd9Sstevel@tonic-gate }
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate /*
23077c478bd9Sstevel@tonic-gate  * Configuration environment processing.  Called after the a.out has been
23087c478bd9Sstevel@tonic-gate  * processed (as the a.out can specify its own configuration file).
23097c478bd9Sstevel@tonic-gate  */
23107c478bd9Sstevel@tonic-gate int
23117c478bd9Sstevel@tonic-gate readenv_config(Rtc_env * envtbl, Addr addr, int aout)
23127c478bd9Sstevel@tonic-gate {
23137c478bd9Sstevel@tonic-gate 	Word *	lmflags = &(lml_main.lm_flags);
23147c478bd9Sstevel@tonic-gate 	Word *	lmtflags = &(lml_main.lm_tflags);
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	if (envtbl == (Rtc_env *)0)
23177c478bd9Sstevel@tonic-gate 		return (0);
23187c478bd9Sstevel@tonic-gate 
23197c478bd9Sstevel@tonic-gate 	while (envtbl->env_str) {
23207c478bd9Sstevel@tonic-gate 		uint_t	env_flags = ENV_TYP_CONFIG;
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 		if (envtbl->env_flags & RTC_ENV_PERMANT)
23237c478bd9Sstevel@tonic-gate 			env_flags |= ENV_TYP_PERMANT;
23247c478bd9Sstevel@tonic-gate 
23257c478bd9Sstevel@tonic-gate 		ld_str_env((const char *)(envtbl->env_str + addr),
23267c478bd9Sstevel@tonic-gate 		    lmflags, lmtflags, env_flags, 0);
23277c478bd9Sstevel@tonic-gate 		envtbl++;
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 	/*
23317c478bd9Sstevel@tonic-gate 	 * Having collected the best representation of any LD_FLAGS, process
23327c478bd9Sstevel@tonic-gate 	 * these strings.
23337c478bd9Sstevel@tonic-gate 	 */
23347c478bd9Sstevel@tonic-gate 	if (ld_flags_env(rpl_ldflags, lmflags, lmtflags, 0, aout) == 1)
23357c478bd9Sstevel@tonic-gate 		return (1);
23367c478bd9Sstevel@tonic-gate 	if (ld_flags_env(prm_ldflags, lmflags, lmtflags, ENV_TYP_CONFIG,
23377c478bd9Sstevel@tonic-gate 	    aout) == 1)
23387c478bd9Sstevel@tonic-gate 		return (1);
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	/*
23417c478bd9Sstevel@tonic-gate 	 * Don't allow environment controlled auditing when tracing or if
23427c478bd9Sstevel@tonic-gate 	 * explicitly disabled.  Trigger all tracing modes from
23437c478bd9Sstevel@tonic-gate 	 * LML_FLG_TRC_ENABLE.
23447c478bd9Sstevel@tonic-gate 	 */
23457c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) || (rtld_flags & RT_FL_NOAUDIT))
23467c478bd9Sstevel@tonic-gate 		prm_audit = profile_lib = profile_name = 0;
23477c478bd9Sstevel@tonic-gate 	if ((*lmflags & LML_FLG_TRC_ENABLE) == 0)
23487c478bd9Sstevel@tonic-gate 		*lmflags &= ~LML_MSK_TRC;
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	return (0);
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate int
23547c478bd9Sstevel@tonic-gate dowrite(Prfbuf * prf)
23557c478bd9Sstevel@tonic-gate {
23567c478bd9Sstevel@tonic-gate 	/*
23577c478bd9Sstevel@tonic-gate 	 * We do not have a valid file descriptor, so we are unable
23587c478bd9Sstevel@tonic-gate 	 * to flush the buffer.
23597c478bd9Sstevel@tonic-gate 	 */
23607c478bd9Sstevel@tonic-gate 	if (prf->pr_fd == -1)
23617c478bd9Sstevel@tonic-gate 		return (0);
23627c478bd9Sstevel@tonic-gate 	(void) write(prf->pr_fd, prf->pr_buf, prf->pr_cur - prf->pr_buf);
23637c478bd9Sstevel@tonic-gate 	prf->pr_cur = prf->pr_buf;
23647c478bd9Sstevel@tonic-gate 	return (1);
23657c478bd9Sstevel@tonic-gate }
23667c478bd9Sstevel@tonic-gate 
23677c478bd9Sstevel@tonic-gate /*
23687c478bd9Sstevel@tonic-gate  * Simplified printing.  The following conversion specifications are supported:
23697c478bd9Sstevel@tonic-gate  *
23707c478bd9Sstevel@tonic-gate  *	% [#] [-] [min field width] [. precision] s|d|x|c
23717c478bd9Sstevel@tonic-gate  *
23727c478bd9Sstevel@tonic-gate  *
23737c478bd9Sstevel@tonic-gate  * dorprf takes the output buffer in the form of Prfbuf which permits
23747c478bd9Sstevel@tonic-gate  * the verification of the output buffer size and the concatenation
23757c478bd9Sstevel@tonic-gate  * of data to an already existing output buffer.  The Prfbuf
23767c478bd9Sstevel@tonic-gate  * structure contains the following:
23777c478bd9Sstevel@tonic-gate  *
23787c478bd9Sstevel@tonic-gate  *  pr_buf	pointer to the beginning of the output buffer.
23797c478bd9Sstevel@tonic-gate  *  pr_cur	pointer to the next available byte in the output buffer.  By
23807c478bd9Sstevel@tonic-gate  *		setting pr_cur ahead of pr_buf you can append to an already
23817c478bd9Sstevel@tonic-gate  *		existing buffer.
23827c478bd9Sstevel@tonic-gate  *  pr_len	the size of the output buffer.  By setting pr_len to '0' you
23837c478bd9Sstevel@tonic-gate  *		disable protection from overflows in the output buffer.
23847c478bd9Sstevel@tonic-gate  *  pr_fd	a pointer to the file-descriptor the buffer will eventually be
23857c478bd9Sstevel@tonic-gate  *		output to.  If pr_fd is set to '-1' then it's assumed there is
23861d6b7ad8Srie  *		no output buffer, and doprf() will return with an error to
23871d6b7ad8Srie  *		indicate an output buffer overflow.  If pr_fd is > -1 then when
23881d6b7ad8Srie  *		the output buffer is filled it will be flushed to pr_fd and will
23891d6b7ad8Srie  *		then be	available for additional data.
23907c478bd9Sstevel@tonic-gate  */
23917c478bd9Sstevel@tonic-gate #define	FLG_UT_MINUS	0x0001	/* - */
23927c478bd9Sstevel@tonic-gate #define	FLG_UT_SHARP	0x0002	/* # */
23937c478bd9Sstevel@tonic-gate #define	FLG_UT_DOTSEEN	0x0008	/* dot appeared in format spec */
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate /*
239610a4fa49Srie  * This macro is for use from within doprf only.  It is to be used for checking
239710a4fa49Srie  * the output buffer size and placing characters into the buffer.
23987c478bd9Sstevel@tonic-gate  */
23997c478bd9Sstevel@tonic-gate #define	PUTC(c) \
24007c478bd9Sstevel@tonic-gate 	{ \
240110a4fa49Srie 		char tmpc; \
24027c478bd9Sstevel@tonic-gate 		\
24037c478bd9Sstevel@tonic-gate 		tmpc = (c); \
240410a4fa49Srie 		if (bufsiz && (bp >= bufend)) { \
24057c478bd9Sstevel@tonic-gate 			prf->pr_cur = bp; \
24067c478bd9Sstevel@tonic-gate 			if (dowrite(prf) == 0) \
24077c478bd9Sstevel@tonic-gate 				return (0); \
24087c478bd9Sstevel@tonic-gate 			bp = prf->pr_cur; \
24097c478bd9Sstevel@tonic-gate 		} \
24107c478bd9Sstevel@tonic-gate 		*bp++ = tmpc; \
24117c478bd9Sstevel@tonic-gate 	}
24127c478bd9Sstevel@tonic-gate 
24131d6b7ad8Srie /*
24141d6b7ad8Srie  * Define a local buffer size for building a numeric value - large enough to
24151d6b7ad8Srie  * hold a 64-bit value.
24161d6b7ad8Srie  */
2417051d39bbSrie #define	NUM_SIZE	22
24181d6b7ad8Srie 
24197c478bd9Sstevel@tonic-gate size_t
24207c478bd9Sstevel@tonic-gate doprf(const char *format, va_list args, Prfbuf *prf)
24217c478bd9Sstevel@tonic-gate {
24227c478bd9Sstevel@tonic-gate 	char	c;
24237c478bd9Sstevel@tonic-gate 	char	*bp = prf->pr_cur;
24247c478bd9Sstevel@tonic-gate 	char	*bufend = prf->pr_buf + prf->pr_len;
24257c478bd9Sstevel@tonic-gate 	size_t	bufsiz = prf->pr_len;
24267c478bd9Sstevel@tonic-gate 
24277c478bd9Sstevel@tonic-gate 	while ((c = *format++) != '\0') {
24287c478bd9Sstevel@tonic-gate 		if (c != '%') {
24297c478bd9Sstevel@tonic-gate 			PUTC(c);
24307c478bd9Sstevel@tonic-gate 		} else {
24317c478bd9Sstevel@tonic-gate 			int	base = 0, flag = 0, width = 0, prec = 0;
24327c478bd9Sstevel@tonic-gate 			size_t	_i;
24337c478bd9Sstevel@tonic-gate 			int	_c, _n;
24347c478bd9Sstevel@tonic-gate 			char	*_s;
24357c478bd9Sstevel@tonic-gate 			int	ls = 0;
24367c478bd9Sstevel@tonic-gate again:
24377c478bd9Sstevel@tonic-gate 			c = *format++;
24387c478bd9Sstevel@tonic-gate 			switch (c) {
24397c478bd9Sstevel@tonic-gate 			case '-':
24407c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_MINUS;
24417c478bd9Sstevel@tonic-gate 				goto again;
24427c478bd9Sstevel@tonic-gate 			case '#':
24437c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_SHARP;
24447c478bd9Sstevel@tonic-gate 				goto again;
24457c478bd9Sstevel@tonic-gate 			case '.':
24467c478bd9Sstevel@tonic-gate 				flag |= FLG_UT_DOTSEEN;
24477c478bd9Sstevel@tonic-gate 				goto again;
24487c478bd9Sstevel@tonic-gate 			case '0':
24497c478bd9Sstevel@tonic-gate 			case '1':
24507c478bd9Sstevel@tonic-gate 			case '2':
24517c478bd9Sstevel@tonic-gate 			case '3':
24527c478bd9Sstevel@tonic-gate 			case '4':
24537c478bd9Sstevel@tonic-gate 			case '5':
24547c478bd9Sstevel@tonic-gate 			case '6':
24557c478bd9Sstevel@tonic-gate 			case '7':
24567c478bd9Sstevel@tonic-gate 			case '8':
24577c478bd9Sstevel@tonic-gate 			case '9':
24587c478bd9Sstevel@tonic-gate 				if (flag & FLG_UT_DOTSEEN)
24597c478bd9Sstevel@tonic-gate 					prec = (prec * 10) + c - '0';
24607c478bd9Sstevel@tonic-gate 				else
24617c478bd9Sstevel@tonic-gate 					width = (width * 10) + c - '0';
24627c478bd9Sstevel@tonic-gate 				goto again;
24637c478bd9Sstevel@tonic-gate 			case 'x':
24647c478bd9Sstevel@tonic-gate 			case 'X':
24657c478bd9Sstevel@tonic-gate 				base = 16;
24667c478bd9Sstevel@tonic-gate 				break;
24677c478bd9Sstevel@tonic-gate 			case 'd':
24687c478bd9Sstevel@tonic-gate 			case 'D':
24697c478bd9Sstevel@tonic-gate 			case 'u':
24707c478bd9Sstevel@tonic-gate 				base = 10;
24717c478bd9Sstevel@tonic-gate 				flag &= ~FLG_UT_SHARP;
24727c478bd9Sstevel@tonic-gate 				break;
24737c478bd9Sstevel@tonic-gate 			case 'l':
24747c478bd9Sstevel@tonic-gate 				base = 10;
24757c478bd9Sstevel@tonic-gate 				ls++; /* number of l's (long or long long) */
24767c478bd9Sstevel@tonic-gate 				if ((*format == 'l') ||
24777c478bd9Sstevel@tonic-gate 				    (*format == 'd') || (*format == 'D') ||
24787c478bd9Sstevel@tonic-gate 				    (*format == 'x') || (*format == 'X') ||
24797c478bd9Sstevel@tonic-gate 				    (*format == 'o') || (*format == 'O'))
24807c478bd9Sstevel@tonic-gate 					goto again;
24817c478bd9Sstevel@tonic-gate 				break;
24827c478bd9Sstevel@tonic-gate 			case 'o':
24837c478bd9Sstevel@tonic-gate 			case 'O':
24847c478bd9Sstevel@tonic-gate 				base = 8;
24857c478bd9Sstevel@tonic-gate 				break;
24867c478bd9Sstevel@tonic-gate 			case 'c':
24877c478bd9Sstevel@tonic-gate 				_c = va_arg(args, int);
24887c478bd9Sstevel@tonic-gate 
24897c478bd9Sstevel@tonic-gate 				for (_i = 24; _i > 0; _i -= 8) {
24907c478bd9Sstevel@tonic-gate 					if ((c = ((_c >> _i) & 0x7f)) != 0) {
24917c478bd9Sstevel@tonic-gate 						PUTC(c);
24927c478bd9Sstevel@tonic-gate 					}
24937c478bd9Sstevel@tonic-gate 				}
24947c478bd9Sstevel@tonic-gate 				if ((c = ((_c >> _i) & 0x7f)) != 0) {
24957c478bd9Sstevel@tonic-gate 					PUTC(c);
24967c478bd9Sstevel@tonic-gate 				}
24977c478bd9Sstevel@tonic-gate 				break;
24987c478bd9Sstevel@tonic-gate 			case 's':
24997c478bd9Sstevel@tonic-gate 				_s = va_arg(args, char *);
25007c478bd9Sstevel@tonic-gate 				_i = strlen(_s);
25017c478bd9Sstevel@tonic-gate 				/* LINTED */
25027c478bd9Sstevel@tonic-gate 				_n = (int)(width - _i);
25037c478bd9Sstevel@tonic-gate 				if (!prec)
25047c478bd9Sstevel@tonic-gate 					/* LINTED */
25057c478bd9Sstevel@tonic-gate 					prec = (int)_i;
25067c478bd9Sstevel@tonic-gate 
25077c478bd9Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
25087c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
25097c478bd9Sstevel@tonic-gate 						PUTC(' ');
25107c478bd9Sstevel@tonic-gate 				}
25117c478bd9Sstevel@tonic-gate 				while (((c = *_s++) != 0) && prec--) {
25127c478bd9Sstevel@tonic-gate 					PUTC(c);
25137c478bd9Sstevel@tonic-gate 				}
25147c478bd9Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
25157c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
25167c478bd9Sstevel@tonic-gate 						PUTC(' ');
25177c478bd9Sstevel@tonic-gate 				}
25187c478bd9Sstevel@tonic-gate 				break;
25197c478bd9Sstevel@tonic-gate 			case '%':
25207c478bd9Sstevel@tonic-gate 				PUTC('%');
25217c478bd9Sstevel@tonic-gate 				break;
25227c478bd9Sstevel@tonic-gate 			default:
25237c478bd9Sstevel@tonic-gate 				break;
25247c478bd9Sstevel@tonic-gate 			}
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 			/*
25277c478bd9Sstevel@tonic-gate 			 * Numeric processing
25287c478bd9Sstevel@tonic-gate 			 */
25297c478bd9Sstevel@tonic-gate 			if (base) {
25301d6b7ad8Srie 				char		local[NUM_SIZE];
25311d6b7ad8Srie 				size_t		ssize = 0, psize = 0;
25327c478bd9Sstevel@tonic-gate 				const char	*string =
2533a953e2b1Srie 				    MSG_ORIG(MSG_STR_HEXNUM);
25347c478bd9Sstevel@tonic-gate 				const char	*prefix =
2535a953e2b1Srie 				    MSG_ORIG(MSG_STR_EMPTY);
25367c478bd9Sstevel@tonic-gate 				u_longlong_t	num;
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 				switch (ls) {
25397c478bd9Sstevel@tonic-gate 				case 0:	/* int */
25407c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)
25417c478bd9Sstevel@tonic-gate 					    va_arg(args, uint_t);
25427c478bd9Sstevel@tonic-gate 					break;
25437c478bd9Sstevel@tonic-gate 				case 1:	/* long */
25447c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)
25457c478bd9Sstevel@tonic-gate 					    va_arg(args, ulong_t);
25467c478bd9Sstevel@tonic-gate 					break;
25477c478bd9Sstevel@tonic-gate 				case 2:	/* long long */
25487c478bd9Sstevel@tonic-gate 					num = va_arg(args, u_longlong_t);
25497c478bd9Sstevel@tonic-gate 					break;
25507c478bd9Sstevel@tonic-gate 				}
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 				if (flag & FLG_UT_SHARP) {
25537c478bd9Sstevel@tonic-gate 					if (base == 16) {
25547c478bd9Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_HEX);
25557c478bd9Sstevel@tonic-gate 						psize = 2;
25567c478bd9Sstevel@tonic-gate 					} else {
25577c478bd9Sstevel@tonic-gate 						prefix = MSG_ORIG(MSG_STR_ZERO);
25587c478bd9Sstevel@tonic-gate 						psize = 1;
25597c478bd9Sstevel@tonic-gate 					}
25607c478bd9Sstevel@tonic-gate 				}
25617c478bd9Sstevel@tonic-gate 				if ((base == 10) && (long)num < 0) {
25627c478bd9Sstevel@tonic-gate 					prefix = MSG_ORIG(MSG_STR_NEGATE);
25637c478bd9Sstevel@tonic-gate 					psize = MSG_STR_NEGATE_SIZE;
25647c478bd9Sstevel@tonic-gate 					num = (u_longlong_t)(-(longlong_t)num);
25657c478bd9Sstevel@tonic-gate 				}
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 				/*
25687c478bd9Sstevel@tonic-gate 				 * Convert the numeric value into a local
25697c478bd9Sstevel@tonic-gate 				 * string (stored in reverse order).
25707c478bd9Sstevel@tonic-gate 				 */
25717c478bd9Sstevel@tonic-gate 				_s = local;
25727c478bd9Sstevel@tonic-gate 				do {
25737c478bd9Sstevel@tonic-gate 					*_s++ = string[num % base];
25747c478bd9Sstevel@tonic-gate 					num /= base;
25757c478bd9Sstevel@tonic-gate 					ssize++;
25767c478bd9Sstevel@tonic-gate 				} while (num);
25777c478bd9Sstevel@tonic-gate 
25781d6b7ad8Srie 				ASSERT(ssize < sizeof (local));
25791d6b7ad8Srie 
25807c478bd9Sstevel@tonic-gate 				/*
25817c478bd9Sstevel@tonic-gate 				 * Provide any precision or width padding.
25827c478bd9Sstevel@tonic-gate 				 */
25837c478bd9Sstevel@tonic-gate 				if (prec) {
25847c478bd9Sstevel@tonic-gate 					/* LINTED */
25857c478bd9Sstevel@tonic-gate 					_n = (int)(prec - ssize);
25861d6b7ad8Srie 					while ((_n-- > 0) &&
25871d6b7ad8Srie 					    (ssize < sizeof (local))) {
25887c478bd9Sstevel@tonic-gate 						*_s++ = '0';
25897c478bd9Sstevel@tonic-gate 						ssize++;
25907c478bd9Sstevel@tonic-gate 					}
25917c478bd9Sstevel@tonic-gate 				}
25927c478bd9Sstevel@tonic-gate 				if (width && !(flag & FLG_UT_MINUS)) {
25937c478bd9Sstevel@tonic-gate 					/* LINTED */
25947c478bd9Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
25957c478bd9Sstevel@tonic-gate 					while (_n-- > 0) {
25967c478bd9Sstevel@tonic-gate 						PUTC(' ');
25977c478bd9Sstevel@tonic-gate 					}
25987c478bd9Sstevel@tonic-gate 				}
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate 				/*
26017c478bd9Sstevel@tonic-gate 				 * Print any prefix and the numeric string
26027c478bd9Sstevel@tonic-gate 				 */
26037c478bd9Sstevel@tonic-gate 				while (*prefix)
26047c478bd9Sstevel@tonic-gate 					PUTC(*prefix++);
26057c478bd9Sstevel@tonic-gate 				do {
26067c478bd9Sstevel@tonic-gate 					PUTC(*--_s);
26077c478bd9Sstevel@tonic-gate 				} while (_s > local);
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 				/*
26107c478bd9Sstevel@tonic-gate 				 * Provide any width padding.
26117c478bd9Sstevel@tonic-gate 				 */
26127c478bd9Sstevel@tonic-gate 				if (width && (flag & FLG_UT_MINUS)) {
26137c478bd9Sstevel@tonic-gate 					/* LINTED */
26147c478bd9Sstevel@tonic-gate 					_n = (int)(width - ssize - psize);
26157c478bd9Sstevel@tonic-gate 					while (_n-- > 0)
26167c478bd9Sstevel@tonic-gate 						PUTC(' ');
26177c478bd9Sstevel@tonic-gate 				}
26187c478bd9Sstevel@tonic-gate 			}
26197c478bd9Sstevel@tonic-gate 		}
26207c478bd9Sstevel@tonic-gate 	}
262110a4fa49Srie 
26227c478bd9Sstevel@tonic-gate 	PUTC('\0');
26237c478bd9Sstevel@tonic-gate 	prf->pr_cur = bp;
26247c478bd9Sstevel@tonic-gate 	return (1);
26257c478bd9Sstevel@tonic-gate }
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate static int
26287c478bd9Sstevel@tonic-gate doprintf(const char *format, va_list args, Prfbuf *prf)
26297c478bd9Sstevel@tonic-gate {
26307c478bd9Sstevel@tonic-gate 	char	*ocur = prf->pr_cur;
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	if (doprf(format, args, prf) == 0)
26337c478bd9Sstevel@tonic-gate 		return (0);
26347c478bd9Sstevel@tonic-gate 	/* LINTED */
26357c478bd9Sstevel@tonic-gate 	return ((int)(prf->pr_cur - ocur));
26367c478bd9Sstevel@tonic-gate }
26377c478bd9Sstevel@tonic-gate 
26387c478bd9Sstevel@tonic-gate /* VARARGS2 */
26397c478bd9Sstevel@tonic-gate int
26407c478bd9Sstevel@tonic-gate sprintf(char *buf, const char *format, ...)
26417c478bd9Sstevel@tonic-gate {
26427c478bd9Sstevel@tonic-gate 	va_list	args;
26437c478bd9Sstevel@tonic-gate 	int	len;
26447c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
26457c478bd9Sstevel@tonic-gate 
26467c478bd9Sstevel@tonic-gate 	va_start(args, format);
26477c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
26487c478bd9Sstevel@tonic-gate 	prf.pr_len = 0;
26497c478bd9Sstevel@tonic-gate 	prf.pr_fd = -1;
26507c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
26517c478bd9Sstevel@tonic-gate 	va_end(args);
26527c478bd9Sstevel@tonic-gate 
26537c478bd9Sstevel@tonic-gate 	/*
26547c478bd9Sstevel@tonic-gate 	 * sprintf() return value excludes the terminating null byte.
26557c478bd9Sstevel@tonic-gate 	 */
26567c478bd9Sstevel@tonic-gate 	return (len - 1);
26577c478bd9Sstevel@tonic-gate }
26587c478bd9Sstevel@tonic-gate 
26597c478bd9Sstevel@tonic-gate /* VARARGS3 */
26607c478bd9Sstevel@tonic-gate int
26617c478bd9Sstevel@tonic-gate snprintf(char *buf, size_t n, const char *format, ...)
26627c478bd9Sstevel@tonic-gate {
26637c478bd9Sstevel@tonic-gate 	va_list	args;
26647c478bd9Sstevel@tonic-gate 	int	len;
26657c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
26667c478bd9Sstevel@tonic-gate 
26677c478bd9Sstevel@tonic-gate 	va_start(args, format);
26687c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buf;
26697c478bd9Sstevel@tonic-gate 	prf.pr_len = n;
26707c478bd9Sstevel@tonic-gate 	prf.pr_fd = -1;
26717c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, &prf);
26727c478bd9Sstevel@tonic-gate 	va_end(args);
26737c478bd9Sstevel@tonic-gate 
26747c478bd9Sstevel@tonic-gate 	return (len);
26757c478bd9Sstevel@tonic-gate }
26767c478bd9Sstevel@tonic-gate 
26777c478bd9Sstevel@tonic-gate /* VARARGS2 */
26787c478bd9Sstevel@tonic-gate int
26797c478bd9Sstevel@tonic-gate bufprint(Prfbuf *prf, const char *format, ...)
26807c478bd9Sstevel@tonic-gate {
26817c478bd9Sstevel@tonic-gate 	va_list	args;
26827c478bd9Sstevel@tonic-gate 	int	len;
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	va_start(args, format);
26857c478bd9Sstevel@tonic-gate 	len = doprintf(format, args, prf);
26867c478bd9Sstevel@tonic-gate 	va_end(args);
26877c478bd9Sstevel@tonic-gate 
26887c478bd9Sstevel@tonic-gate 	return (len);
26897c478bd9Sstevel@tonic-gate }
26907c478bd9Sstevel@tonic-gate 
26917c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
26927c478bd9Sstevel@tonic-gate int
26937c478bd9Sstevel@tonic-gate printf(const char *format, ...)
26947c478bd9Sstevel@tonic-gate {
26957c478bd9Sstevel@tonic-gate 	va_list	args;
26967c478bd9Sstevel@tonic-gate 	char 	buffer[ERRSIZE];
26977c478bd9Sstevel@tonic-gate 	Prfbuf	prf;
26987c478bd9Sstevel@tonic-gate 
26997c478bd9Sstevel@tonic-gate 	va_start(args, format);
27007c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = buffer;
27017c478bd9Sstevel@tonic-gate 	prf.pr_len = ERRSIZE;
27027c478bd9Sstevel@tonic-gate 	prf.pr_fd = 1;
27037c478bd9Sstevel@tonic-gate 	(void) doprf(format, args, &prf);
27047c478bd9Sstevel@tonic-gate 	va_end(args);
27057c478bd9Sstevel@tonic-gate 	/*
27067c478bd9Sstevel@tonic-gate 	 * Trim trailing '\0' form buffer
27077c478bd9Sstevel@tonic-gate 	 */
27087c478bd9Sstevel@tonic-gate 	prf.pr_cur--;
27097c478bd9Sstevel@tonic-gate 	return (dowrite(&prf));
27107c478bd9Sstevel@tonic-gate }
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate static char	errbuf[ERRSIZE], *nextptr = errbuf, *prevptr = 0;
27137c478bd9Sstevel@tonic-gate 
27145aefb655Srie /*PRINTFLIKE3*/
27157c478bd9Sstevel@tonic-gate void
27165aefb655Srie eprintf(Lm_list *lml, Error error, const char *format, ...)
27177c478bd9Sstevel@tonic-gate {
27187c478bd9Sstevel@tonic-gate 	va_list		args;
27197c478bd9Sstevel@tonic-gate 	int		overflow = 0;
27207c478bd9Sstevel@tonic-gate 	static int	lock = 0;
27217c478bd9Sstevel@tonic-gate 	Prfbuf		prf;
27227c478bd9Sstevel@tonic-gate 
27237c478bd9Sstevel@tonic-gate 	if (lock || (nextptr == (errbuf + ERRSIZE)))
27247c478bd9Sstevel@tonic-gate 		return;
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * Note: this lock is here to prevent the same thread from recursively
27287c478bd9Sstevel@tonic-gate 	 * entering itself during a eprintf.  ie: during eprintf malloc() fails
27297c478bd9Sstevel@tonic-gate 	 * and we try and call eprintf ... and then malloc() fails ....
27307c478bd9Sstevel@tonic-gate 	 */
27317c478bd9Sstevel@tonic-gate 	lock = 1;
27327c478bd9Sstevel@tonic-gate 
27337c478bd9Sstevel@tonic-gate 	/*
27347c478bd9Sstevel@tonic-gate 	 * If we have completed startup initialization, all error messages
27357c478bd9Sstevel@tonic-gate 	 * must be saved.  These are reported through dlerror().  If we're
27367c478bd9Sstevel@tonic-gate 	 * still in the initialization stage, output the error directly and
27377c478bd9Sstevel@tonic-gate 	 * add a newline.
27387c478bd9Sstevel@tonic-gate 	 */
27397c478bd9Sstevel@tonic-gate 	va_start(args, format);
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 	prf.pr_buf = prf.pr_cur = nextptr;
27427c478bd9Sstevel@tonic-gate 	prf.pr_len = ERRSIZE - (nextptr - errbuf);
27437c478bd9Sstevel@tonic-gate 
27447c478bd9Sstevel@tonic-gate 	if (!(rtld_flags & RT_FL_APPLIC))
27457c478bd9Sstevel@tonic-gate 		prf.pr_fd = 2;
27467c478bd9Sstevel@tonic-gate 	else
27477c478bd9Sstevel@tonic-gate 		prf.pr_fd = -1;
27487c478bd9Sstevel@tonic-gate 
27497c478bd9Sstevel@tonic-gate 	if (error > ERR_NONE) {
27507c478bd9Sstevel@tonic-gate 		if ((error == ERR_FATAL) && (rtld_flags2 & RT_FL2_FTL2WARN))
27517c478bd9Sstevel@tonic-gate 			error = ERR_WARNING;
27527c478bd9Sstevel@tonic-gate 		if (error == ERR_WARNING) {
27537c478bd9Sstevel@tonic-gate 			if (err_strs[ERR_WARNING] == 0)
2754a953e2b1Srie 				err_strs[ERR_WARNING] =
2755a953e2b1Srie 				    MSG_INTL(MSG_ERR_WARNING);
27567c478bd9Sstevel@tonic-gate 		} else if (error == ERR_FATAL) {
27577c478bd9Sstevel@tonic-gate 			if (err_strs[ERR_FATAL] == 0)
2758a953e2b1Srie 				err_strs[ERR_FATAL] = MSG_INTL(MSG_ERR_FATAL);
27597c478bd9Sstevel@tonic-gate 		} else if (error == ERR_ELF) {
27607c478bd9Sstevel@tonic-gate 			if (err_strs[ERR_ELF] == 0)
2761a953e2b1Srie 				err_strs[ERR_ELF] = MSG_INTL(MSG_ERR_ELF);
27627c478bd9Sstevel@tonic-gate 		}
276341072f3cSrie 		if (procname) {
276441072f3cSrie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR1),
276541072f3cSrie 			    rtldname, procname, err_strs[error]) == 0)
276641072f3cSrie 				overflow = 1;
27677c478bd9Sstevel@tonic-gate 		} else {
276841072f3cSrie 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
276941072f3cSrie 			    rtldname, err_strs[error]) == 0)
277041072f3cSrie 				overflow = 1;
277141072f3cSrie 		}
277241072f3cSrie 		if (overflow == 0) {
27737c478bd9Sstevel@tonic-gate 			/*
27747c478bd9Sstevel@tonic-gate 			 * Remove the terminating '\0'.
27757c478bd9Sstevel@tonic-gate 			 */
27767c478bd9Sstevel@tonic-gate 			prf.pr_cur--;
27777c478bd9Sstevel@tonic-gate 		}
27787c478bd9Sstevel@tonic-gate 	}
27797c478bd9Sstevel@tonic-gate 
27807c478bd9Sstevel@tonic-gate 	if ((overflow == 0) && doprf(format, args, &prf) == 0)
27817c478bd9Sstevel@tonic-gate 		overflow = 1;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 	/*
27847c478bd9Sstevel@tonic-gate 	 * If this is an ELF error, it will have been generated by a support
27857c478bd9Sstevel@tonic-gate 	 * object that has a dependency on libelf.  ld.so.1 doesn't generate any
27867c478bd9Sstevel@tonic-gate 	 * ELF error messages as it doesn't interact with libelf.  Determine the
27877c478bd9Sstevel@tonic-gate 	 * ELF error string.
27887c478bd9Sstevel@tonic-gate 	 */
27897c478bd9Sstevel@tonic-gate 	if ((overflow == 0) && (error == ERR_ELF)) {
27907c478bd9Sstevel@tonic-gate 		static int		(*elfeno)() = 0;
27917c478bd9Sstevel@tonic-gate 		static const char	*(*elfemg)();
27927c478bd9Sstevel@tonic-gate 		const char		*emsg;
27937c478bd9Sstevel@tonic-gate 		Rt_map			*dlmp, *lmp = lml_rtld.lm_head;
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 		if (NEXT(lmp) && (elfeno == 0)) {
27967c478bd9Sstevel@tonic-gate 			if (((elfemg = (const char *(*)())dlsym_intn(RTLD_NEXT,
27977c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_ELFERRMSG), lmp, &dlmp)) == 0) ||
27987c478bd9Sstevel@tonic-gate 			    ((elfeno = (int (*)())dlsym_intn(RTLD_NEXT,
27997c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_SYM_ELFERRNO), lmp, &dlmp)) == 0))
28007c478bd9Sstevel@tonic-gate 				elfeno = 0;
28017c478bd9Sstevel@tonic-gate 		}
28027c478bd9Sstevel@tonic-gate 
28037c478bd9Sstevel@tonic-gate 		/*
28047c478bd9Sstevel@tonic-gate 		 * Lookup the message; equivalent to elf_errmsg(elf_errno()).
28057c478bd9Sstevel@tonic-gate 		 */
28067c478bd9Sstevel@tonic-gate 		if (elfeno && ((emsg = (* elfemg)((* elfeno)())) != 0)) {
28077c478bd9Sstevel@tonic-gate 			prf.pr_cur--;
28087c478bd9Sstevel@tonic-gate 			if (bufprint(&prf, MSG_ORIG(MSG_STR_EMSGFOR2),
28097c478bd9Sstevel@tonic-gate 			    emsg) == 0)
28107c478bd9Sstevel@tonic-gate 				overflow = 1;
28117c478bd9Sstevel@tonic-gate 		}
28127c478bd9Sstevel@tonic-gate 	}
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate 	/*
28157c478bd9Sstevel@tonic-gate 	 * Push out any message that's been built.  Note, in the case of an
28167c478bd9Sstevel@tonic-gate 	 * overflow condition, this message may be incomplete, in which case
28177c478bd9Sstevel@tonic-gate 	 * make sure any partial string is null terminated.
28187c478bd9Sstevel@tonic-gate 	 */
28197c478bd9Sstevel@tonic-gate 	if (overflow)
28207c478bd9Sstevel@tonic-gate 		*(prf.pr_cur) = '\0';
28217c478bd9Sstevel@tonic-gate 	if ((rtld_flags & (RT_FL_APPLIC | RT_FL_SILENCERR)) == 0) {
28227c478bd9Sstevel@tonic-gate 		*(prf.pr_cur - 1) = '\n';
28237c478bd9Sstevel@tonic-gate 		(void) dowrite(&prf);
28247c478bd9Sstevel@tonic-gate 	}
28257c478bd9Sstevel@tonic-gate 
28265aefb655Srie 	DBG_CALL(Dbg_util_str(lml, nextptr));
28277c478bd9Sstevel@tonic-gate 	va_end(args);
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 	/*
28307c478bd9Sstevel@tonic-gate 	 * Determine if there was insufficient space left in the buffer to
28317c478bd9Sstevel@tonic-gate 	 * complete the message.  If so, we'll have printed out as much as had
28327c478bd9Sstevel@tonic-gate 	 * been processed if we're not yet executing the application.
28337c478bd9Sstevel@tonic-gate 	 * Otherwise, there will be some debugging diagnostic indicating
28347c478bd9Sstevel@tonic-gate 	 * as much of the error message as possible.  Write out a final buffer
28357c478bd9Sstevel@tonic-gate 	 * overflow diagnostic - unlocalized, so we don't chance more errors.
28367c478bd9Sstevel@tonic-gate 	 */
28377c478bd9Sstevel@tonic-gate 	if (overflow) {
28387c478bd9Sstevel@tonic-gate 		char	*str = (char *)MSG_INTL(MSG_EMG_BUFOVRFLW);
28397c478bd9Sstevel@tonic-gate 
28407c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_SILENCERR) == 0) {
28417c478bd9Sstevel@tonic-gate 			lasterr = str;
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 			if ((rtld_flags & RT_FL_APPLIC) == 0) {
28447c478bd9Sstevel@tonic-gate 				(void) write(2, str, strlen(str));
28457c478bd9Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
28467c478bd9Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
28477c478bd9Sstevel@tonic-gate 			}
28487c478bd9Sstevel@tonic-gate 		}
28495aefb655Srie 		DBG_CALL(Dbg_util_str(lml, str));
28507c478bd9Sstevel@tonic-gate 
28517c478bd9Sstevel@tonic-gate 		lock = 0;
28527c478bd9Sstevel@tonic-gate 		nextptr = errbuf + ERRSIZE;
28537c478bd9Sstevel@tonic-gate 		return;
28547c478bd9Sstevel@tonic-gate 	}
28557c478bd9Sstevel@tonic-gate 
28567c478bd9Sstevel@tonic-gate 	/*
28577c478bd9Sstevel@tonic-gate 	 * If the application has started, then error messages are being saved
28587c478bd9Sstevel@tonic-gate 	 * for retrieval by dlerror(), or possible flushing from rtldexit() in
28597c478bd9Sstevel@tonic-gate 	 * the case of a fatal error.  In this case, establish the next error
28607c478bd9Sstevel@tonic-gate 	 * pointer.  If we haven't started the application, the whole message
28617c478bd9Sstevel@tonic-gate 	 * buffer can be reused.
28627c478bd9Sstevel@tonic-gate 	 */
28637c478bd9Sstevel@tonic-gate 	if ((rtld_flags & RT_FL_SILENCERR) == 0) {
28647c478bd9Sstevel@tonic-gate 		lasterr = nextptr;
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 		/*
28677c478bd9Sstevel@tonic-gate 		 * Note, should we encounter an error such as ENOMEM, there may
28687c478bd9Sstevel@tonic-gate 		 * be a number of the same error messages (ie. an operation
28697c478bd9Sstevel@tonic-gate 		 * fails with ENOMEM, and then the attempts to construct the
28707c478bd9Sstevel@tonic-gate 		 * error message itself, which incurs additional ENOMEM errors).
28717c478bd9Sstevel@tonic-gate 		 * Compare any previous error message with the one we've just
28727c478bd9Sstevel@tonic-gate 		 * created to prevent any duplication clutter.
28737c478bd9Sstevel@tonic-gate 		 */
28747c478bd9Sstevel@tonic-gate 		if ((rtld_flags & RT_FL_APPLIC) &&
28757c478bd9Sstevel@tonic-gate 		    ((prevptr == 0) || (strcmp(prevptr, nextptr) != 0))) {
28767c478bd9Sstevel@tonic-gate 			prevptr = nextptr;
28777c478bd9Sstevel@tonic-gate 			nextptr = prf.pr_cur;
28787c478bd9Sstevel@tonic-gate 			*nextptr = '\0';
28797c478bd9Sstevel@tonic-gate 		}
28807c478bd9Sstevel@tonic-gate 	}
28817c478bd9Sstevel@tonic-gate 	lock = 0;
28827c478bd9Sstevel@tonic-gate }
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate #if	DEBUG
28867c478bd9Sstevel@tonic-gate /*
28877c478bd9Sstevel@tonic-gate  * Provide assfail() for ASSERT() statements,
28887c478bd9Sstevel@tonic-gate  * see <sys/debug.h> for further details.
28897c478bd9Sstevel@tonic-gate  */
28907c478bd9Sstevel@tonic-gate int
28917c478bd9Sstevel@tonic-gate assfail(const char *a, const char *f, int l)
28927c478bd9Sstevel@tonic-gate {
28937c478bd9Sstevel@tonic-gate 	(void) printf("assertion failed: %s, file: %s, line: %d\n", a, f, l);
28947c478bd9Sstevel@tonic-gate 	(void) _lwp_kill(_lwp_self(), SIGABRT);
28957c478bd9Sstevel@tonic-gate 	return (0);
28967c478bd9Sstevel@tonic-gate }
28977c478bd9Sstevel@tonic-gate #endif
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate /*
29007c478bd9Sstevel@tonic-gate  * Exit.  If we arrive here with a non zero status it's because of a fatal
29017c478bd9Sstevel@tonic-gate  * error condition (most commonly a relocation error).  If the application has
29027c478bd9Sstevel@tonic-gate  * already had control, then the actual fatal error message will have been
29037c478bd9Sstevel@tonic-gate  * recorded in the dlerror() message buffer.  Print the message before really
29047c478bd9Sstevel@tonic-gate  * exiting.
29057c478bd9Sstevel@tonic-gate  */
29067c478bd9Sstevel@tonic-gate void
29077c478bd9Sstevel@tonic-gate rtldexit(Lm_list * lml, int status)
29087c478bd9Sstevel@tonic-gate {
29097c478bd9Sstevel@tonic-gate 	if (status) {
29107c478bd9Sstevel@tonic-gate 		if (rtld_flags & RT_FL_APPLIC) {
29117c478bd9Sstevel@tonic-gate 			/*
29127c478bd9Sstevel@tonic-gate 			 * If the error buffer has been used, write out all
29137c478bd9Sstevel@tonic-gate 			 * pending messages - lasterr is simply a pointer to
29147c478bd9Sstevel@tonic-gate 			 * the last message in this buffer.  However, if the
29157c478bd9Sstevel@tonic-gate 			 * buffer couldn't be created at all, lasterr points
29167c478bd9Sstevel@tonic-gate 			 * to a constant error message string.
29177c478bd9Sstevel@tonic-gate 			 */
29187c478bd9Sstevel@tonic-gate 			if (*errbuf) {
29197c478bd9Sstevel@tonic-gate 				char	*errptr = errbuf;
29207c478bd9Sstevel@tonic-gate 				char	*errend = errbuf + ERRSIZE;
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 				while ((errptr < errend) && *errptr) {
29237c478bd9Sstevel@tonic-gate 					size_t	size = strlen(errptr);
29247c478bd9Sstevel@tonic-gate 					(void) write(2, errptr, size);
29257c478bd9Sstevel@tonic-gate 					(void) write(2, MSG_ORIG(MSG_STR_NL),
29267c478bd9Sstevel@tonic-gate 					    MSG_STR_NL_SIZE);
29277c478bd9Sstevel@tonic-gate 					errptr += (size + 1);
29287c478bd9Sstevel@tonic-gate 				}
29297c478bd9Sstevel@tonic-gate 			}
29307c478bd9Sstevel@tonic-gate 			if (lasterr && ((lasterr < errbuf) ||
29317c478bd9Sstevel@tonic-gate 			    (lasterr > (errbuf + ERRSIZE)))) {
29327c478bd9Sstevel@tonic-gate 				(void) write(2, lasterr, strlen(lasterr));
29337c478bd9Sstevel@tonic-gate 				(void) write(2, MSG_ORIG(MSG_STR_NL),
29347c478bd9Sstevel@tonic-gate 				    MSG_STR_NL_SIZE);
29357c478bd9Sstevel@tonic-gate 			}
29367c478bd9Sstevel@tonic-gate 		}
29377c478bd9Sstevel@tonic-gate 		leave(lml);
29387c478bd9Sstevel@tonic-gate 		(void) _lwp_kill(_lwp_self(), killsig);
29397c478bd9Sstevel@tonic-gate 	}
29407c478bd9Sstevel@tonic-gate 	_exit(status);
29417c478bd9Sstevel@tonic-gate }
29427c478bd9Sstevel@tonic-gate 
29437c478bd9Sstevel@tonic-gate /*
29447c478bd9Sstevel@tonic-gate  * Routines to co-ordinate the opening of /dev/zero and /proc.
29457c478bd9Sstevel@tonic-gate  * dz_fd is exported for possible use by libld.so, and to insure it gets
29467c478bd9Sstevel@tonic-gate  * closed on leaving ld.so.1.
29477c478bd9Sstevel@tonic-gate  */
29487c478bd9Sstevel@tonic-gate int	dz_fd = FD_UNAVAIL;
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate void
29517c478bd9Sstevel@tonic-gate dz_init(int fd)
29527c478bd9Sstevel@tonic-gate {
29537c478bd9Sstevel@tonic-gate 	dz_fd = fd;
29547c478bd9Sstevel@tonic-gate }
29557c478bd9Sstevel@tonic-gate 
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate /*
29587c478bd9Sstevel@tonic-gate  * mmap() a page from MAP_ANON
29597c478bd9Sstevel@tonic-gate  *
29607c478bd9Sstevel@tonic-gate  * Note: MAP_ANON is only on Solaris8++, we use this routine to
29617c478bd9Sstevel@tonic-gate  *       not only mmap(MAP_ANON) but to also probe if it is available
29627c478bd9Sstevel@tonic-gate  *	 on the current OS.
29637c478bd9Sstevel@tonic-gate  */
29647c478bd9Sstevel@tonic-gate Am_ret
29655aefb655Srie anon_map(Lm_list *lml, caddr_t *addr, size_t len, int prot, int flags)
29667c478bd9Sstevel@tonic-gate {
29677c478bd9Sstevel@tonic-gate #if defined(MAP_ANON)
29687c478bd9Sstevel@tonic-gate 	static int	noanon = 0;
29697c478bd9Sstevel@tonic-gate 	caddr_t		va;
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	if (noanon == 0) {
29727c478bd9Sstevel@tonic-gate 		if ((va = (caddr_t)mmap(*addr, len, prot,
29737c478bd9Sstevel@tonic-gate 		    (flags | MAP_ANON), -1, 0)) != MAP_FAILED) {
29747c478bd9Sstevel@tonic-gate 			*addr = va;
29757c478bd9Sstevel@tonic-gate 			return (AM_OK);
29767c478bd9Sstevel@tonic-gate 		}
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 		if ((errno != EBADF) && (errno != EINVAL)) {
29797c478bd9Sstevel@tonic-gate 			int	err = errno;
29805aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAPANON),
29817c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
29827c478bd9Sstevel@tonic-gate 			return (AM_ERROR);
29837c478bd9Sstevel@tonic-gate 		} else
29847c478bd9Sstevel@tonic-gate 			noanon = 1;
29857c478bd9Sstevel@tonic-gate 	}
29867c478bd9Sstevel@tonic-gate #endif
29877c478bd9Sstevel@tonic-gate 	return (AM_NOSUP);
29887c478bd9Sstevel@tonic-gate }
29897c478bd9Sstevel@tonic-gate 
29907c478bd9Sstevel@tonic-gate /*
29917c478bd9Sstevel@tonic-gate  * Map anonymous memory from /dev/zero, or via MAP_ANON.
29927c478bd9Sstevel@tonic-gate  *
29937c478bd9Sstevel@tonic-gate  * (MAP_ANON only appears on Solaris 8, so we need fall-back
29947c478bd9Sstevel@tonic-gate  * behavior for older systems.)
29957c478bd9Sstevel@tonic-gate  */
29967c478bd9Sstevel@tonic-gate caddr_t
29975aefb655Srie dz_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
29987c478bd9Sstevel@tonic-gate {
29997c478bd9Sstevel@tonic-gate 	caddr_t	va;
30007c478bd9Sstevel@tonic-gate 	int	err;
30017c478bd9Sstevel@tonic-gate 	Am_ret	amret;
30027c478bd9Sstevel@tonic-gate 
30035aefb655Srie 	amret = anon_map(lml, &addr, len, prot, flags);
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate 	if (amret == AM_OK)
30067c478bd9Sstevel@tonic-gate 		return (addr);
30077c478bd9Sstevel@tonic-gate 	if (amret == AM_ERROR)
30087c478bd9Sstevel@tonic-gate 		return (MAP_FAILED);
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	/* amret == AM_NOSUP -> fallback to a devzero mmaping */
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 	if (dz_fd == FD_UNAVAIL) {
30137c478bd9Sstevel@tonic-gate 		if ((dz_fd = open(MSG_ORIG(MSG_PTH_DEVZERO),
30147c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
30157c478bd9Sstevel@tonic-gate 			err = errno;
30165aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
30177c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
30187c478bd9Sstevel@tonic-gate 			return (MAP_FAILED);
30197c478bd9Sstevel@tonic-gate 		}
30207c478bd9Sstevel@tonic-gate 	}
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate 	if ((va = mmap(addr, len, prot, flags, dz_fd, 0)) == MAP_FAILED) {
30237c478bd9Sstevel@tonic-gate 		err = errno;
30245aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
30257c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVZERO), strerror(err));
30267c478bd9Sstevel@tonic-gate 	}
30277c478bd9Sstevel@tonic-gate 	return (va);
30287c478bd9Sstevel@tonic-gate }
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate static int	pr_fd = FD_UNAVAIL;
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate int
30335aefb655Srie pr_open(Lm_list *lml)
30347c478bd9Sstevel@tonic-gate {
30357c478bd9Sstevel@tonic-gate 	char	proc[16];
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	if (pr_fd == FD_UNAVAIL) {
30387c478bd9Sstevel@tonic-gate 		(void) snprintf(proc, 16, MSG_ORIG(MSG_FMT_PROC),
3039a953e2b1Srie 		    (int)getpid());
30407c478bd9Sstevel@tonic-gate 		if ((pr_fd = open(proc, O_RDONLY)) == FD_UNAVAIL) {
30417c478bd9Sstevel@tonic-gate 			int	err = errno;
30427c478bd9Sstevel@tonic-gate 
30435aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN), proc,
30447c478bd9Sstevel@tonic-gate 			    strerror(err));
30457c478bd9Sstevel@tonic-gate 		}
30467c478bd9Sstevel@tonic-gate 	}
30477c478bd9Sstevel@tonic-gate 	return (pr_fd);
30487c478bd9Sstevel@tonic-gate }
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate static int	nu_fd = FD_UNAVAIL;
30517c478bd9Sstevel@tonic-gate 
30527c478bd9Sstevel@tonic-gate caddr_t
30535aefb655Srie nu_map(Lm_list *lml, caddr_t addr, size_t len, int prot, int flags)
30547c478bd9Sstevel@tonic-gate {
30557c478bd9Sstevel@tonic-gate 	caddr_t	va;
30567c478bd9Sstevel@tonic-gate 	int	err;
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	if (nu_fd == FD_UNAVAIL) {
30597c478bd9Sstevel@tonic-gate 		if ((nu_fd = open(MSG_ORIG(MSG_PTH_DEVNULL),
30607c478bd9Sstevel@tonic-gate 		    O_RDONLY)) == FD_UNAVAIL) {
30617c478bd9Sstevel@tonic-gate 			err = errno;
30625aefb655Srie 			eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_OPEN),
30637c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
30647c478bd9Sstevel@tonic-gate 			return (MAP_FAILED);
30657c478bd9Sstevel@tonic-gate 		}
30667c478bd9Sstevel@tonic-gate 	}
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 	if ((va = (caddr_t)mmap(addr, len, prot, flags, nu_fd, 0)) ==
30697c478bd9Sstevel@tonic-gate 	    MAP_FAILED) {
30707c478bd9Sstevel@tonic-gate 		err = errno;
30715aefb655Srie 		eprintf(lml, ERR_FATAL, MSG_INTL(MSG_SYS_MMAP),
30727c478bd9Sstevel@tonic-gate 		    MSG_ORIG(MSG_PTH_DEVNULL), strerror(err));
30737c478bd9Sstevel@tonic-gate 	}
30747c478bd9Sstevel@tonic-gate 	return (va);
30757c478bd9Sstevel@tonic-gate }
30767c478bd9Sstevel@tonic-gate 
30777c478bd9Sstevel@tonic-gate /*
30787c478bd9Sstevel@tonic-gate  * Generic entry point from user code - simply grabs a lock.
30797c478bd9Sstevel@tonic-gate  */
30807c478bd9Sstevel@tonic-gate int
30817c478bd9Sstevel@tonic-gate enter(void)
30827c478bd9Sstevel@tonic-gate {
30837c478bd9Sstevel@tonic-gate 	if (rt_bind_guard(THR_FLG_RTLD)) {
30847c478bd9Sstevel@tonic-gate 		(void) rt_mutex_lock(&rtldlock);
30857c478bd9Sstevel@tonic-gate 		return (1);
30867c478bd9Sstevel@tonic-gate 	}
30877c478bd9Sstevel@tonic-gate 	return (0);
30887c478bd9Sstevel@tonic-gate }
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate /*
30917c478bd9Sstevel@tonic-gate  * Generate diagnostics as to whether an object has been used.  A symbolic
30927c478bd9Sstevel@tonic-gate  * reference that gets bound to an object marks it as used.  Dependencies that
30937c478bd9Sstevel@tonic-gate  * are unused when RTLD_NOW is in effect should be removed from future builds
30947c478bd9Sstevel@tonic-gate  * of an object.  Dependencies that are unused without RTLD_NOW in effect are
30957c478bd9Sstevel@tonic-gate  * candidates for lazy-loading.
30967c478bd9Sstevel@tonic-gate  * Unreferenced objects identify objects that are defined as dependencies but
30977c478bd9Sstevel@tonic-gate  * are unreferenced by the caller (they may however be referenced by other
30987c478bd9Sstevel@tonic-gate  * objects within the process, and therefore don't qualify as completely unused.
30997c478bd9Sstevel@tonic-gate  */
31007c478bd9Sstevel@tonic-gate void
31017c478bd9Sstevel@tonic-gate unused(Lm_list *lml)
31027c478bd9Sstevel@tonic-gate {
31037c478bd9Sstevel@tonic-gate 	Rt_map		*lmp;
31047c478bd9Sstevel@tonic-gate 	int		nl = 0;
31057c478bd9Sstevel@tonic-gate 	Word		tracing;
31067c478bd9Sstevel@tonic-gate 
31077c478bd9Sstevel@tonic-gate 	/*
31087c478bd9Sstevel@tonic-gate 	 * If we're not tracing unused references or dependencies, or debugging
31097c478bd9Sstevel@tonic-gate 	 * there's nothing to do.
31107c478bd9Sstevel@tonic-gate 	 */
31117c478bd9Sstevel@tonic-gate 	tracing = lml->lm_flags & (LML_FLG_TRC_UNREF | LML_FLG_TRC_UNUSED);
31127c478bd9Sstevel@tonic-gate 
31135aefb655Srie 	if ((tracing == 0) && (DBG_ENABLED == 0))
31147c478bd9Sstevel@tonic-gate 		return;
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 	/*
31177c478bd9Sstevel@tonic-gate 	 * Traverse the link-maps looking for unreferenced or unused
31187c478bd9Sstevel@tonic-gate 	 * dependencies.  Ignore the first object on a link-map list, as this
31197c478bd9Sstevel@tonic-gate 	 * is effectively always used.
31207c478bd9Sstevel@tonic-gate 	 */
31217c478bd9Sstevel@tonic-gate 	for (lmp = (Rt_map *)NEXT(lml->lm_head); lmp;
31227c478bd9Sstevel@tonic-gate 	    lmp = (Rt_map *)NEXT(lmp)) {
31237c478bd9Sstevel@tonic-gate 		/*
31247c478bd9Sstevel@tonic-gate 		 * If tracing unreferenced objects, or under debugging,
31257c478bd9Sstevel@tonic-gate 		 * determine whether any of this objects callers haven't
31267c478bd9Sstevel@tonic-gate 		 * referenced it.
31277c478bd9Sstevel@tonic-gate 		 */
31285aefb655Srie 		if ((tracing & LML_FLG_TRC_UNREF) || DBG_ENABLED) {
31297c478bd9Sstevel@tonic-gate 			Bnd_desc **	bdpp;
31307c478bd9Sstevel@tonic-gate 			Aliste		off;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 			for (ALIST_TRAVERSE(CALLERS(lmp), off, bdpp)) {
31337c478bd9Sstevel@tonic-gate 				Bnd_desc *	bdp = *bdpp;
31347c478bd9Sstevel@tonic-gate 				Rt_map *	clmp;
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 				if (bdp->b_flags & BND_REFER)
31377c478bd9Sstevel@tonic-gate 					continue;
31387c478bd9Sstevel@tonic-gate 
31397c478bd9Sstevel@tonic-gate 				clmp = bdp->b_caller;
31407c478bd9Sstevel@tonic-gate 				if (FLAGS1(clmp) & FL1_RT_LDDSTUB)
31417c478bd9Sstevel@tonic-gate 					continue;
31427c478bd9Sstevel@tonic-gate 
3143a953e2b1Srie 				/* BEGIN CSTYLED */
31447c478bd9Sstevel@tonic-gate 				if (nl++ == 0) {
31457c478bd9Sstevel@tonic-gate 					if (tracing & LML_FLG_TRC_UNREF)
31467c478bd9Sstevel@tonic-gate 					    (void) printf(MSG_ORIG(MSG_STR_NL));
31477c478bd9Sstevel@tonic-gate 					else
31485aefb655Srie 					    DBG_CALL(Dbg_util_nl(lml,
31495aefb655Srie 						DBG_NL_STD));
31507c478bd9Sstevel@tonic-gate 				}
31517c478bd9Sstevel@tonic-gate 
31527c478bd9Sstevel@tonic-gate 				if (tracing & LML_FLG_TRC_UNREF)
31537c478bd9Sstevel@tonic-gate 				    (void) printf(MSG_INTL(MSG_LDD_UNREF_FMT),
31547c478bd9Sstevel@tonic-gate 					NAME(lmp), NAME(clmp));
31557c478bd9Sstevel@tonic-gate 				else
31565aefb655Srie 				    DBG_CALL(Dbg_unused_unref(lmp, NAME(clmp)));
3157a953e2b1Srie 				/* END CSTYLED */
31587c478bd9Sstevel@tonic-gate 			}
31597c478bd9Sstevel@tonic-gate 		}
31607c478bd9Sstevel@tonic-gate 
31617c478bd9Sstevel@tonic-gate 		/*
31627c478bd9Sstevel@tonic-gate 		 * If tracing unused objects simply display those objects that
31637c478bd9Sstevel@tonic-gate 		 * haven't been referenced by anyone.
31647c478bd9Sstevel@tonic-gate 		 */
31657c478bd9Sstevel@tonic-gate 		if (FLAGS1(lmp) & FL1_RT_USED)
31667c478bd9Sstevel@tonic-gate 			continue;
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 		if (nl++ == 0) {
31697c478bd9Sstevel@tonic-gate 			if (tracing)
31707c478bd9Sstevel@tonic-gate 				(void) printf(MSG_ORIG(MSG_STR_NL));
31717c478bd9Sstevel@tonic-gate 			else
31725aefb655Srie 				DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
31737c478bd9Sstevel@tonic-gate 		}
31747c478bd9Sstevel@tonic-gate 		if (CYCGROUP(lmp)) {
31757c478bd9Sstevel@tonic-gate 			if (tracing)
31767c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNCYC_FMT),
31777c478bd9Sstevel@tonic-gate 				    NAME(lmp), CYCGROUP(lmp));
31787c478bd9Sstevel@tonic-gate 			else
31795aefb655Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0,
31807c478bd9Sstevel@tonic-gate 				    CYCGROUP(lmp)));
31817c478bd9Sstevel@tonic-gate 		} else {
31827c478bd9Sstevel@tonic-gate 			if (tracing)
31837c478bd9Sstevel@tonic-gate 				(void) printf(MSG_INTL(MSG_LDD_UNUSED_FMT),
31847c478bd9Sstevel@tonic-gate 				    NAME(lmp));
31857c478bd9Sstevel@tonic-gate 			else
31865aefb655Srie 				DBG_CALL(Dbg_unused_file(lml, NAME(lmp), 0, 0));
31877c478bd9Sstevel@tonic-gate 		}
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate 
31905aefb655Srie 	DBG_CALL(Dbg_util_nl(lml, DBG_NL_STD));
31917c478bd9Sstevel@tonic-gate }
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate /*
31947c478bd9Sstevel@tonic-gate  * Initialization routine for the Fmap structure.  If the fmap structure is
31957c478bd9Sstevel@tonic-gate  * already in use, any mapping is released.  The structure is then initialized
31967c478bd9Sstevel@tonic-gate  * in preparation for further use.
31977c478bd9Sstevel@tonic-gate  */
31987c478bd9Sstevel@tonic-gate void
31997c478bd9Sstevel@tonic-gate fmap_setup()
32007c478bd9Sstevel@tonic-gate {
32017c478bd9Sstevel@tonic-gate #if defined(MAP_ALIGN)
32027c478bd9Sstevel@tonic-gate 	/*
32037c478bd9Sstevel@tonic-gate 	 * If MAP_ALIGN is set, the fm_addr has been seeded with an alignment
32047c478bd9Sstevel@tonic-gate 	 * value.  Otherwise, if fm_addr is non-null it indicates a mapping that
32057c478bd9Sstevel@tonic-gate 	 * should now be freed.
32067c478bd9Sstevel@tonic-gate 	 */
32077c478bd9Sstevel@tonic-gate 	if (fmap->fm_maddr && ((fmap->fm_mflags & MAP_ALIGN) == 0))
32087c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize);
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	/*
32117c478bd9Sstevel@tonic-gate 	 * Providing we haven't determined that this system doesn't support
32127c478bd9Sstevel@tonic-gate 	 * MAP_ALIGN, initialize the mapping address with the default segment
32137c478bd9Sstevel@tonic-gate 	 * alignment.
32147c478bd9Sstevel@tonic-gate 	 */
32157c478bd9Sstevel@tonic-gate 	if ((rtld_flags2 & RT_FL2_NOMALIGN) == 0) {
32167c478bd9Sstevel@tonic-gate 		fmap->fm_maddr = (char *)M_SEGM_ALIGN;
32177c478bd9Sstevel@tonic-gate 		fmap->fm_mflags = MAP_PRIVATE | MAP_ALIGN;
32187c478bd9Sstevel@tonic-gate 	} else {
32197c478bd9Sstevel@tonic-gate 		fmap->fm_maddr = 0;
32207c478bd9Sstevel@tonic-gate 		fmap->fm_mflags = MAP_PRIVATE;
32217c478bd9Sstevel@tonic-gate 	}
32227c478bd9Sstevel@tonic-gate #else
32237c478bd9Sstevel@tonic-gate 	if (fmap->fm_maddr)
32247c478bd9Sstevel@tonic-gate 		(void) munmap((caddr_t)fmap->fm_maddr, fmap->fm_msize);
3225fb1354edSrie 
32267c478bd9Sstevel@tonic-gate 	fmap->fm_maddr = 0;
32277c478bd9Sstevel@tonic-gate 	fmap->fm_mflags = MAP_PRIVATE;
32287c478bd9Sstevel@tonic-gate #endif
32297c478bd9Sstevel@tonic-gate 
3230a953e2b1Srie 	fmap->fm_msize = FMAP_SIZE;
32317c478bd9Sstevel@tonic-gate 	fmap->fm_hwptr = 0;
32327c478bd9Sstevel@tonic-gate }
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate /*
32357c478bd9Sstevel@tonic-gate  * Generic cleanup routine called prior to returning control to the user.
32367c478bd9Sstevel@tonic-gate  * Insures that any ld.so.1 specific file descriptors or temporary mapping are
32377c478bd9Sstevel@tonic-gate  * released, and any locks dropped.
32387c478bd9Sstevel@tonic-gate  */
32397c478bd9Sstevel@tonic-gate void
324012b8e62eSrie leave(Lm_list *lml)
32417c478bd9Sstevel@tonic-gate {
324212b8e62eSrie 	Lm_list	*elml = lml;
3243*7247f888Srie 	Rt_map	**clmpp;
3244*7247f888Srie 	Aliste	off;
324512b8e62eSrie 
32467c478bd9Sstevel@tonic-gate 	/*
324712b8e62eSrie 	 * Alert the debuggers that the link-maps are consistent.  Note, in the
324812b8e62eSrie 	 * case of tearing down a whole link-map list, lml will be null.  In
324912b8e62eSrie 	 * this case use the main link-map list to test for a notification.
32507c478bd9Sstevel@tonic-gate 	 */
325112b8e62eSrie 	if (elml == 0)
325212b8e62eSrie 		elml = &lml_main;
325312b8e62eSrie 	if (elml->lm_flags & LML_FLG_DBNOTIF)
325412b8e62eSrie 		rd_event(elml, RD_DLACTIVITY, RT_CONSISTENT);
32557c478bd9Sstevel@tonic-gate 
3256*7247f888Srie 	/*
3257*7247f888Srie 	 * Alert any auditors that the link-maps are consistent.
3258*7247f888Srie 	 */
3259*7247f888Srie 	for (ALIST_TRAVERSE(elml->lm_actaudit, off, clmpp)) {
3260*7247f888Srie 		audit_activity(*clmpp, LA_ACT_CONSISTENT);
3261*7247f888Srie 
3262*7247f888Srie 		(void) alist_delete(elml->lm_actaudit, 0, &off);
3263*7247f888Srie 	}
3264*7247f888Srie 
32657c478bd9Sstevel@tonic-gate 	if (dz_fd != FD_UNAVAIL) {
32667c478bd9Sstevel@tonic-gate 		(void) close(dz_fd);
32677c478bd9Sstevel@tonic-gate 		dz_fd = FD_UNAVAIL;
32687c478bd9Sstevel@tonic-gate 	}
32697c478bd9Sstevel@tonic-gate 
32707c478bd9Sstevel@tonic-gate 	if (pr_fd != FD_UNAVAIL) {
32717c478bd9Sstevel@tonic-gate 		(void) close(pr_fd);
32727c478bd9Sstevel@tonic-gate 		pr_fd = FD_UNAVAIL;
32737c478bd9Sstevel@tonic-gate 	}
32747c478bd9Sstevel@tonic-gate 
32757c478bd9Sstevel@tonic-gate 	if (nu_fd != FD_UNAVAIL) {
32767c478bd9Sstevel@tonic-gate 		(void) close(nu_fd);
32777c478bd9Sstevel@tonic-gate 		nu_fd = FD_UNAVAIL;
32787c478bd9Sstevel@tonic-gate 	}
32797c478bd9Sstevel@tonic-gate 
32807c478bd9Sstevel@tonic-gate 	fmap_setup();
32817c478bd9Sstevel@tonic-gate 
32827c478bd9Sstevel@tonic-gate 	/*
32837c478bd9Sstevel@tonic-gate 	 * Reinitialize error message pointer, and any overflow indication.
32847c478bd9Sstevel@tonic-gate 	 */
32857c478bd9Sstevel@tonic-gate 	nextptr = errbuf;
32867c478bd9Sstevel@tonic-gate 	prevptr = 0;
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate 	/*
32897c478bd9Sstevel@tonic-gate 	 * Don't drop our lock if we are running on our link-map list as
32907c478bd9Sstevel@tonic-gate 	 * there's little point in doing so since we are single-threaded.
32917c478bd9Sstevel@tonic-gate 	 *
32927c478bd9Sstevel@tonic-gate 	 * LML_FLG_HOLDLOCK is set for:
32937c478bd9Sstevel@tonic-gate 	 *	*) The ld.so.1's link-map list.
32947c478bd9Sstevel@tonic-gate 	 *	*) The auditor's link-map if the environment is
32957c478bd9Sstevel@tonic-gate 	 *	   libc/libthread un-unified.
32967c478bd9Sstevel@tonic-gate 	 */
32977c478bd9Sstevel@tonic-gate 	if (lml && (lml->lm_flags & LML_FLG_HOLDLOCK))
32987c478bd9Sstevel@tonic-gate 		return;
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	if (rt_bind_clear(0) & THR_FLG_RTLD) {
33017c478bd9Sstevel@tonic-gate 		(void) rt_mutex_unlock(&rtldlock);
33027c478bd9Sstevel@tonic-gate 		(void) rt_bind_clear(THR_FLG_RTLD);
33037c478bd9Sstevel@tonic-gate 	}
33047c478bd9Sstevel@tonic-gate }
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate int
33077c478bd9Sstevel@tonic-gate callable(Rt_map * clmp, Rt_map * dlmp, Grp_hdl * ghp)
33087c478bd9Sstevel@tonic-gate {
33097c478bd9Sstevel@tonic-gate 	Alist *		calp, * dalp;
33107c478bd9Sstevel@tonic-gate 	Aliste		cnt1, cnt2;
33117c478bd9Sstevel@tonic-gate 	Grp_hdl **	ghpp1, ** ghpp2;
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	/*
33147c478bd9Sstevel@tonic-gate 	 * An object can always find symbols within itself.
33157c478bd9Sstevel@tonic-gate 	 */
33167c478bd9Sstevel@tonic-gate 	if (clmp == dlmp)
33177c478bd9Sstevel@tonic-gate 		return (1);
33187c478bd9Sstevel@tonic-gate 
33197c478bd9Sstevel@tonic-gate 	/*
33207c478bd9Sstevel@tonic-gate 	 * Don't allow an object to bind to an object that is being deleted
33217c478bd9Sstevel@tonic-gate 	 * unless the binder is also being deleted.
33227c478bd9Sstevel@tonic-gate 	 */
33237c478bd9Sstevel@tonic-gate 	if ((FLAGS(dlmp) & FLG_RT_DELETE) &&
33247c478bd9Sstevel@tonic-gate 	    ((FLAGS(clmp) & FLG_RT_DELETE) == 0))
33257c478bd9Sstevel@tonic-gate 		return (0);
33267c478bd9Sstevel@tonic-gate 
33277c478bd9Sstevel@tonic-gate 	/*
33287c478bd9Sstevel@tonic-gate 	 * An object with world access can always bind to an object with global
33297c478bd9Sstevel@tonic-gate 	 * visibility.
33307c478bd9Sstevel@tonic-gate 	 */
33317c478bd9Sstevel@tonic-gate 	if ((MODE(clmp) & RTLD_WORLD) && (MODE(dlmp) & RTLD_GLOBAL))
33327c478bd9Sstevel@tonic-gate 		return (1);
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	/*
33357c478bd9Sstevel@tonic-gate 	 * An object with local access can only bind to an object that is a
33367c478bd9Sstevel@tonic-gate 	 * member of the same group.
33377c478bd9Sstevel@tonic-gate 	 */
33387c478bd9Sstevel@tonic-gate 	if (((MODE(clmp) & RTLD_GROUP) == 0) ||
33397c478bd9Sstevel@tonic-gate 	    ((calp = GROUPS(clmp)) == 0) || ((dalp = GROUPS(dlmp)) == 0))
33407c478bd9Sstevel@tonic-gate 		return (0);
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 	/*
33437c478bd9Sstevel@tonic-gate 	 * Traverse the list of groups the caller is a part of.
33447c478bd9Sstevel@tonic-gate 	 */
33457c478bd9Sstevel@tonic-gate 	for (ALIST_TRAVERSE(calp, cnt1, ghpp1)) {
33467c478bd9Sstevel@tonic-gate 		/*
33477c478bd9Sstevel@tonic-gate 		 * If we're testing for the ability of two objects to bind to
33487c478bd9Sstevel@tonic-gate 		 * each other regardless of a specific group, ignore that group.
33497c478bd9Sstevel@tonic-gate 		 */
33507c478bd9Sstevel@tonic-gate 		if (ghp && (*ghpp1 == ghp))
33517c478bd9Sstevel@tonic-gate 			continue;
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 		/*
33547c478bd9Sstevel@tonic-gate 		 * Traverse the list of groups the destination is a part of.
33557c478bd9Sstevel@tonic-gate 		 */
33567c478bd9Sstevel@tonic-gate 		for (ALIST_TRAVERSE(dalp, cnt2, ghpp2)) {
33577c478bd9Sstevel@tonic-gate 			if (*ghpp1 == *ghpp2)
33587c478bd9Sstevel@tonic-gate 				return (1);
33597c478bd9Sstevel@tonic-gate 		}
33607c478bd9Sstevel@tonic-gate 	}
33617c478bd9Sstevel@tonic-gate 	return (0);
33627c478bd9Sstevel@tonic-gate }
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate /*
33657c478bd9Sstevel@tonic-gate  * Initialize the environ symbol.  Traditionally this is carried out by the crt
33667c478bd9Sstevel@tonic-gate  * code prior to jumping to main.  However, init sections get fired before this
33677c478bd9Sstevel@tonic-gate  * variable is initialized, so ld.so.1 sets this directly from the AUX vector
33687c478bd9Sstevel@tonic-gate  * information.  In addition, a process may have multiple link-maps (ld.so.1's
33697c478bd9Sstevel@tonic-gate  * debugging and preloading objects), and link auditing, and each may need an
33707c478bd9Sstevel@tonic-gate  * environ variable set.
33717c478bd9Sstevel@tonic-gate  *
33727c478bd9Sstevel@tonic-gate  * This routine is called after a relocation() pass, and thus provides for:
33737c478bd9Sstevel@tonic-gate  *
33747c478bd9Sstevel@tonic-gate  *  o	setting environ on the main link-map after the initial application and
33757c478bd9Sstevel@tonic-gate  *	its dependencies have been established.  Typically environ lives in the
33767c478bd9Sstevel@tonic-gate  *	application (provided by its crt), but in older applications it might
33777c478bd9Sstevel@tonic-gate  *	be in libc.  Who knows what's expected of applications not built on
33787c478bd9Sstevel@tonic-gate  *	Solaris.
33797c478bd9Sstevel@tonic-gate  *
33807c478bd9Sstevel@tonic-gate  *  o	after loading a new shared object.  We can add shared objects to various
33817c478bd9Sstevel@tonic-gate  *	link-maps, and any link-map dependencies requiring getopt() require
33827c478bd9Sstevel@tonic-gate  *	their own environ.  In addition, lazy loading might bring in the
338341072f3cSrie  *	supplier of environ (libc used to be a lazy loading candidate) after
338441072f3cSrie  *	the link-map has been established and other objects are present.
33857c478bd9Sstevel@tonic-gate  *
33867c478bd9Sstevel@tonic-gate  * This routine handles all these scenarios, without adding unnecessary overhead
33877c478bd9Sstevel@tonic-gate  * to ld.so.1.
33887c478bd9Sstevel@tonic-gate  */
33897c478bd9Sstevel@tonic-gate void
33907c478bd9Sstevel@tonic-gate set_environ(Lm_list *lml)
33917c478bd9Sstevel@tonic-gate {
33927c478bd9Sstevel@tonic-gate 	Rt_map *	dlmp;
33937c478bd9Sstevel@tonic-gate 	Sym *		sym;
33947c478bd9Sstevel@tonic-gate 	Slookup		sl;
33957c478bd9Sstevel@tonic-gate 	uint_t		binfo;
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	sl.sl_name = MSG_ORIG(MSG_SYM_ENVIRON);
33987c478bd9Sstevel@tonic-gate 	sl.sl_cmap = lml->lm_head;
33997c478bd9Sstevel@tonic-gate 	sl.sl_imap = lml->lm_head;
34007c478bd9Sstevel@tonic-gate 	sl.sl_hash = 0;
34017c478bd9Sstevel@tonic-gate 	sl.sl_rsymndx = 0;
34027c478bd9Sstevel@tonic-gate 	sl.sl_flags = LKUP_WEAK;
34037c478bd9Sstevel@tonic-gate 
34047c478bd9Sstevel@tonic-gate 	if (sym = LM_LOOKUP_SYM(lml->lm_head)(&sl, &dlmp, &binfo)) {
340541072f3cSrie 		lml->lm_environ = (char ***)sym->st_value;
34067c478bd9Sstevel@tonic-gate 
34077c478bd9Sstevel@tonic-gate 		if (!(FLAGS(dlmp) & FLG_RT_FIXED))
340841072f3cSrie 			lml->lm_environ =
340941072f3cSrie 			    (char ***)((uintptr_t)lml->lm_environ +
341041072f3cSrie 			    (uintptr_t)ADDR(dlmp));
341141072f3cSrie 		*(lml->lm_environ) = (char **)environ;
34127c478bd9Sstevel@tonic-gate 		lml->lm_flags |= LML_FLG_ENVIRON;
34137c478bd9Sstevel@tonic-gate 	}
34147c478bd9Sstevel@tonic-gate }
34157c478bd9Sstevel@tonic-gate 
34167c478bd9Sstevel@tonic-gate /*
34177c478bd9Sstevel@tonic-gate  * Determine whether we have a secure executable.  Uid and gid information
34187c478bd9Sstevel@tonic-gate  * can be passed to us via the aux vector, however if these values are -1
34197c478bd9Sstevel@tonic-gate  * then use the appropriate system call to obtain them.
34207c478bd9Sstevel@tonic-gate  *
34217c478bd9Sstevel@tonic-gate  *  o	If the user is the root they can do anything
34227c478bd9Sstevel@tonic-gate  *
34237c478bd9Sstevel@tonic-gate  *  o	If the real and effective uid's don't match, or the real and
34247c478bd9Sstevel@tonic-gate  *	effective gid's don't match then this is determined to be a `secure'
34257c478bd9Sstevel@tonic-gate  *	application.
34267c478bd9Sstevel@tonic-gate  *
34277c478bd9Sstevel@tonic-gate  * This function is called prior to any dependency processing (see _setup.c).
34287c478bd9Sstevel@tonic-gate  * Any secure setting will remain in effect for the life of the process.
34297c478bd9Sstevel@tonic-gate  */
34307c478bd9Sstevel@tonic-gate void
34317c478bd9Sstevel@tonic-gate security(uid_t uid, uid_t euid, gid_t gid, gid_t egid, int auxflags)
34327c478bd9Sstevel@tonic-gate {
34337c478bd9Sstevel@tonic-gate #ifdef AT_SUN_AUXFLAGS
34347c478bd9Sstevel@tonic-gate 	if (auxflags != -1) {
34357c478bd9Sstevel@tonic-gate 		if ((auxflags & AF_SUN_SETUGID) != 0)
34367c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
34377c478bd9Sstevel@tonic-gate 		return;
34387c478bd9Sstevel@tonic-gate 	}
34397c478bd9Sstevel@tonic-gate #endif
3440a953e2b1Srie 	if (uid == (uid_t)-1)
34417c478bd9Sstevel@tonic-gate 		uid = getuid();
34427c478bd9Sstevel@tonic-gate 	if (uid) {
3443a953e2b1Srie 		if (euid == (uid_t)-1)
34447c478bd9Sstevel@tonic-gate 			euid = geteuid();
34457c478bd9Sstevel@tonic-gate 		if (uid != euid)
34467c478bd9Sstevel@tonic-gate 			rtld_flags |= RT_FL_SECURE;
34477c478bd9Sstevel@tonic-gate 		else {
3448a953e2b1Srie 			if (gid == (gid_t)-1)
34497c478bd9Sstevel@tonic-gate 				gid = getgid();
3450a953e2b1Srie 			if (egid == (gid_t)-1)
34517c478bd9Sstevel@tonic-gate 				egid = getegid();
34527c478bd9Sstevel@tonic-gate 			if (gid != egid)
34537c478bd9Sstevel@tonic-gate 				rtld_flags |= RT_FL_SECURE;
34547c478bd9Sstevel@tonic-gate 		}
34557c478bd9Sstevel@tonic-gate 	}
34567c478bd9Sstevel@tonic-gate }
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate /*
34597c478bd9Sstevel@tonic-gate  * _REENTRANT code gets errno redefined to a function so provide for return
34607c478bd9Sstevel@tonic-gate  * of the thread errno if applicable.  This has no meaning in ld.so.1 which
34617c478bd9Sstevel@tonic-gate  * is basically singled threaded.  Provide the interface for our dependencies.
34627c478bd9Sstevel@tonic-gate  */
34637c478bd9Sstevel@tonic-gate #undef errno
34647c478bd9Sstevel@tonic-gate #pragma weak _private___errno = ___errno
34657c478bd9Sstevel@tonic-gate int *
34667c478bd9Sstevel@tonic-gate ___errno()
34677c478bd9Sstevel@tonic-gate {
34687c478bd9Sstevel@tonic-gate 	extern	int	errno;
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	return (&errno);
34717c478bd9Sstevel@tonic-gate }
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate /*
34747c478bd9Sstevel@tonic-gate  * The interface with the c library which is supplied through libdl.so.1.
34757c478bd9Sstevel@tonic-gate  * A non-null argument allows a function pointer array to be passed to us which
34767c478bd9Sstevel@tonic-gate  * is used to re-initialize the linker libc table.
34777c478bd9Sstevel@tonic-gate  */
34787c478bd9Sstevel@tonic-gate void
34797c478bd9Sstevel@tonic-gate _ld_libc(void * ptr)
34807c478bd9Sstevel@tonic-gate {
34817c478bd9Sstevel@tonic-gate 	get_lcinterface(_caller(caller(), CL_EXECDEF), (Lc_interface *)ptr);
34827c478bd9Sstevel@tonic-gate }
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate /*
34857c478bd9Sstevel@tonic-gate  * Determine whether a symbol name should be demangled.
34867c478bd9Sstevel@tonic-gate  */
34877c478bd9Sstevel@tonic-gate const char *
34887c478bd9Sstevel@tonic-gate demangle(const char *name)
34897c478bd9Sstevel@tonic-gate {
34907c478bd9Sstevel@tonic-gate 	if (rtld_flags & RT_FL_DEMANGLE)
34915aefb655Srie 		return (conv_demangle_name(name));
34927c478bd9Sstevel@tonic-gate 	else
34937c478bd9Sstevel@tonic-gate 		return (name);
34947c478bd9Sstevel@tonic-gate }
3495