xref: /onnv-gate/usr/src/cmd/sgs/ldd/common/ldd.c (revision 6223:30d6740da48d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51698Sab196087  * Common Development and Distribution License (the "License").
61698Sab196087  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
206150Srie  *
210Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
220Sstevel@tonic-gate  *	  All Rights Reserved
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  *
256150Srie  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
263731Srie  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Print the list of shared objects required by a dynamic executable or shared
320Sstevel@tonic-gate  * object.
330Sstevel@tonic-gate  *
346150Srie  * usage is: ldd [-d | -r] [-c] [-e envar] [-i] [-f] [-L] [-l] [-p] [-s]
354947Srie  *		[-U | -u] [-v] [-w] file(s)
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  * ldd opens the file and verifies the information in the elf header.
380Sstevel@tonic-gate  * If the file is a dynamic executable, we set up some environment variables
390Sstevel@tonic-gate  * and exec(2) the file.  If the file is a shared object, we preload the
400Sstevel@tonic-gate  * file with a dynamic executable stub. The runtime linker (ld.so.1) actually
410Sstevel@tonic-gate  * provides the diagnostic output, according to the environment variables set.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_[AE].
440Sstevel@tonic-gate  * The runtime linker will print the pathnames of all dynamic objects it
450Sstevel@tonic-gate  * loads, and then exit.  Note that we distiguish between ELF and AOUT objects
460Sstevel@tonic-gate  * when setting this environment variable - AOUT executables cause the mapping
470Sstevel@tonic-gate  * of sbcp, the dependencies of which the user isn't interested in.
480Sstevel@tonic-gate  *
490Sstevel@tonic-gate  * If -d or -r is specified, we also set LD_WARN=1; the runtime linker will
500Sstevel@tonic-gate  * perform its normal relocations and issue warning messages for unresolved
510Sstevel@tonic-gate  * references. It will then exit.
520Sstevel@tonic-gate  * If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker
530Sstevel@tonic-gate  * will perform all relocations, otherwise (under -d) the runtime linker
540Sstevel@tonic-gate  * will not perform PLT (function) type relocations.
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * If -c is specified we also set LD_NOCONFIG=1, thus disabling any
570Sstevel@tonic-gate  * configuration file use.
580Sstevel@tonic-gate  *
590Sstevel@tonic-gate  * If -e is specified the associated environment variable is set for the
600Sstevel@tonic-gate  * child process that will produce ldd's diagnostics.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  * If -i is specified, we set LD_INIT=1. The order of inititialization
630Sstevel@tonic-gate  * sections to be executed is printed. We also set LD_WARN=1.
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * If -f is specified, we will run ldd as root on executables that have
660Sstevel@tonic-gate  * an unsercure runtime linker that does not live under the "/usr/lib"
670Sstevel@tonic-gate  * directory.  By default we will not let this happen.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * If -l is specified it generates a warning for any auxiliary filter not found.
700Sstevel@tonic-gate  * Prior to 2.8 this forced any filters to load (all) their filtees.  This is
710Sstevel@tonic-gate  * now the default, however missing auxiliary filters don't generate any error
720Sstevel@tonic-gate  * diagniostic.  See also -L.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * If -L is specified we revert to lazy loading, thus any filtee or lazy
750Sstevel@tonic-gate  * dependency loading is deferred until relocations cause loading.  Without
760Sstevel@tonic-gate  * this option we set LD_LOADFLTR=1, thus forcing any filters to load (all)
770Sstevel@tonic-gate  * their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of
780Sstevel@tonic-gate  * any lazy loaded dependencies.
790Sstevel@tonic-gate  *
800Sstevel@tonic-gate  * If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling
810Sstevel@tonic-gate  * the runtime linker to indicate the search algorithm used.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime
840Sstevel@tonic-gate  * linker to indicate all object dependencies (not just the first object
850Sstevel@tonic-gate  * loaded) together with any versionig requirements.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * If -U or -u is specified unused dependencies are detected.  -u causes
880Sstevel@tonic-gate  * LD_UNUSED=1 to be set, which causes dependencies that are unused within the
890Sstevel@tonic-gate  * process to be detected.  -U causes LD_UNREF=1 to be set, which causes
900Sstevel@tonic-gate  * unreferenced objects, and unreferenced cyclic dependencies to be detected.
910Sstevel@tonic-gate  * These options assert that at least -d is set as relocation references are
920Sstevel@tonic-gate  * what determine an objects use.
934947Srie  *
944947Srie  * If -w is specified, no unresolved weak references are allowed.  -w causes
954947Srie  * LD_NOUNRESWEAK=1 to be set.  By default, an unresolved weak reference is
964947Srie  * allowed, and a "0" is written to the relocation offset.  The -w option
974947Srie  * disables this default.  Any weak references that can not be resolved result
986150Srie  * in relocation error messages.  This option has no use without -r or -d.
996150Srie  *
1006150Srie  * If the -p option is specified, no unresolved PARENT or EXTERN references are
1016150Srie  * allowed.  -p causes LD_NOPAREXT=1 to be set.  By default, PARENT and EXTERN
1026150Srie  * references, which have been explicitly assigned via a mapfile when a shared
1036150Srie  * object was built, imply that a caller will provide the symbols, and hence
1046150Srie  * these are not reported as relocation errors.  Note, the -p option is asserted
1056150Srie  * by default when either the -r or -d options are used to inspect a dynamic
1066150Srie  * executable.  This option has no use with a shared object without -r or -d.
1070Sstevel@tonic-gate  */
1080Sstevel@tonic-gate #include	<fcntl.h>
1090Sstevel@tonic-gate #include	<stdio.h>
1100Sstevel@tonic-gate #include	<string.h>
1111698Sab196087 #include	<_libelf.h>
1120Sstevel@tonic-gate #include	<stdlib.h>
1130Sstevel@tonic-gate #include	<unistd.h>
1140Sstevel@tonic-gate #include	<wait.h>
1150Sstevel@tonic-gate #include	<locale.h>
1160Sstevel@tonic-gate #include	<errno.h>
1170Sstevel@tonic-gate #include	<signal.h>
1180Sstevel@tonic-gate #include	"machdep.h"
1190Sstevel@tonic-gate #include	"sgs.h"
1200Sstevel@tonic-gate #include	"conv.h"
1210Sstevel@tonic-gate #include	"a.out.h"
1220Sstevel@tonic-gate #include	"msg.h"
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate static int	elf_check(int, char *, char *, Elf *, int);
1250Sstevel@tonic-gate static int	aout_check(int, char *, char *, int, int);
1260Sstevel@tonic-gate static int	run(int, char *, char *, const char *, int);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /*
1304947Srie  * Define all environment variable strings.  The character following the "="
1314947Srie  * will be written to, to disable or enable the associated feature.
1320Sstevel@tonic-gate  */
1330Sstevel@tonic-gate static char	bind[] =	"LD_BIND_NOW= ",
1340Sstevel@tonic-gate 		load_elf[] =	"LD_TRACE_LOADED_OBJECTS_E= ",
1350Sstevel@tonic-gate 		load_aout[] =	"LD_TRACE_LOADED_OBJECTS_A= ",
1360Sstevel@tonic-gate 		path[] =	"LD_TRACE_SEARCH_PATHS= ",
1370Sstevel@tonic-gate 		verb[] =	"LD_VERBOSE= ",
1380Sstevel@tonic-gate 		warn[] =	"LD_WARN= ",
1390Sstevel@tonic-gate 		conf[] =	"LD_NOCONFIG= ",
1400Sstevel@tonic-gate 		fltr[] =	"LD_LOADFLTR= ",
1410Sstevel@tonic-gate 		lazy[] =	"LD_NOLAZYLOAD=1",
1420Sstevel@tonic-gate 		init[] =	"LD_INIT= ",
1430Sstevel@tonic-gate 		uref[] =	"LD_UNREF= ",
1444947Srie 		used[] =	"LD_UNUSED= ",
1456150Srie 		weak[] =	"LD_NOUNRESWEAK= ",
1466150Srie 		nope[] =	"LD_NOPAREXT= ";
1470Sstevel@tonic-gate static char	*load;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate static const char	*prefile_32, *prefile_64, *prefile;
1500Sstevel@tonic-gate static List		eopts = { 0, 0 };
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate  * Append an item to the specified list, and return a pointer to the list
1540Sstevel@tonic-gate  * node created.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate Listnode *
1570Sstevel@tonic-gate list_append(List *lst, const void *item)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	Listnode	*lnp;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	if ((lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
1620Sstevel@tonic-gate 		return (0);
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	lnp->data = (void *)item;
1650Sstevel@tonic-gate 	lnp->next = NULL;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (lst->head == NULL)
1680Sstevel@tonic-gate 		lst->tail = lst->head = lnp;
1690Sstevel@tonic-gate 	else {
1700Sstevel@tonic-gate 		lst->tail->next = lnp;
1710Sstevel@tonic-gate 		lst->tail = lst->tail->next;
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	return (lnp);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate int
177*6223Sab196087 main(int argc, char **argv, char **envp)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	char	*str, *cname = argv[0];
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	Elf	*elf;
1820Sstevel@tonic-gate 	int	cflag = 0, dflag = 0, fflag = 0, iflag = 0, Lflag = 0;
1830Sstevel@tonic-gate 	int	lflag = 0, rflag = 0, sflag = 0, Uflag = 0, uflag = 0;
1846150Srie 	int	pflag = 0, vflag = 0, wflag = 0, nfile, var, error = 0;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	Listnode	*lnp;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/*
189*6223Sab196087 	 * If we're on a 64-bit kernel, try to exec a full 64-bit version of
190*6223Sab196087 	 * the binary.  If successful, conv_check_native() won't return.
191*6223Sab196087 	 *
192*6223Sab196087 	 * This is done to ensure that ldd can handle objects >2GB.
193*6223Sab196087 	 * ldd uses libelf, which is not large file capable. The
194*6223Sab196087 	 * 64-bit ldd can handle any sized object.
195*6223Sab196087 	 */
196*6223Sab196087 	(void) conv_check_native(argv, envp);
197*6223Sab196087 
198*6223Sab196087 	/*
1990Sstevel@tonic-gate 	 * Establish locale.
2000Sstevel@tonic-gate 	 */
2010Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
2020Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 	/*
2050Sstevel@tonic-gate 	 * verify command line syntax and process arguments
2060Sstevel@tonic-gate 	 */
2070Sstevel@tonic-gate 	opterr = 0;				/* disable getopt error mesg */
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_GETOPT))) != EOF) {
2100Sstevel@tonic-gate 		switch (var) {
2110Sstevel@tonic-gate 		case 'c' :			/* enable config search */
2120Sstevel@tonic-gate 			cflag = 1;
2130Sstevel@tonic-gate 			break;
2140Sstevel@tonic-gate 		case 'd' :			/* perform data relocations */
2150Sstevel@tonic-gate 			dflag = 1;
2160Sstevel@tonic-gate 			if (rflag)
2170Sstevel@tonic-gate 				error++;
2180Sstevel@tonic-gate 			break;
2190Sstevel@tonic-gate 		case 'e' :
2200Sstevel@tonic-gate 			if (list_append(&eopts, optarg) == 0) {
2210Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
2220Sstevel@tonic-gate 				    cname);
2230Sstevel@tonic-gate 				exit(1);
2240Sstevel@tonic-gate 			}
2250Sstevel@tonic-gate 			break;
2260Sstevel@tonic-gate 		case 'f' :
2270Sstevel@tonic-gate 			fflag = 1;
2280Sstevel@tonic-gate 			break;
2290Sstevel@tonic-gate 		case 'L' :
2300Sstevel@tonic-gate 			Lflag = 1;
2310Sstevel@tonic-gate 			break;
2320Sstevel@tonic-gate 		case 'l' :
2330Sstevel@tonic-gate 			lflag = 1;
2340Sstevel@tonic-gate 			break;
2350Sstevel@tonic-gate 		case 'i' :			/* print the order of .init */
2360Sstevel@tonic-gate 			iflag = 1;
2370Sstevel@tonic-gate 			break;
2386150Srie 		case 'p' :
2396150Srie 			pflag = 1;		/* expose unreferenced */
2406150Srie 			break;			/*	parent or externals */
2410Sstevel@tonic-gate 		case 'r' :			/* perform all relocations */
2420Sstevel@tonic-gate 			rflag = 1;
2430Sstevel@tonic-gate 			if (dflag)
2440Sstevel@tonic-gate 				error++;
2450Sstevel@tonic-gate 			break;
2460Sstevel@tonic-gate 		case 's' :			/* enable search path output */
2470Sstevel@tonic-gate 			sflag = 1;
2480Sstevel@tonic-gate 			break;
2490Sstevel@tonic-gate 		case 'U' :			/* list unreferenced */
2500Sstevel@tonic-gate 			Uflag = 1;		/*	dependencies */
2510Sstevel@tonic-gate 			if (uflag)
2520Sstevel@tonic-gate 				error++;
2530Sstevel@tonic-gate 			break;
2540Sstevel@tonic-gate 		case 'u' :			/* list unused dependencies */
2550Sstevel@tonic-gate 			uflag = 1;
2560Sstevel@tonic-gate 			if (Uflag)
2570Sstevel@tonic-gate 				error++;
2580Sstevel@tonic-gate 			break;
2590Sstevel@tonic-gate 		case 'v' :			/* enable verbose output */
2600Sstevel@tonic-gate 			vflag = 1;
2610Sstevel@tonic-gate 			break;
2626150Srie 		case 'w' :			/* expose unresolved weak */
2634947Srie 			wflag = 1;		/*	references */
2644947Srie 			break;
2650Sstevel@tonic-gate 		default :
2660Sstevel@tonic-gate 			error++;
2670Sstevel@tonic-gate 			break;
2680Sstevel@tonic-gate 		}
2690Sstevel@tonic-gate 		if (error)
2700Sstevel@tonic-gate 			break;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	if (error) {
2730Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), cname);
2740Sstevel@tonic-gate 		exit(1);
2750Sstevel@tonic-gate 	}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	/*
2780Sstevel@tonic-gate 	 * Determine if any of the LD_PRELOAD family is already set in the
2790Sstevel@tonic-gate 	 * environment, if so we'll continue to analyze each object with the
2800Sstevel@tonic-gate 	 * appropriate setting.
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	if (((prefile_32 = getenv(MSG_ORIG(MSG_LD_PRELOAD_32))) == NULL) ||
2830Sstevel@tonic-gate 	    (*prefile_32 == '\0')) {
2840Sstevel@tonic-gate 		prefile_32 = MSG_ORIG(MSG_STR_EMPTY);
2850Sstevel@tonic-gate 	}
2860Sstevel@tonic-gate 	if (((prefile_64 = getenv(MSG_ORIG(MSG_LD_PRELOAD_64))) == NULL) ||
2870Sstevel@tonic-gate 	    (*prefile_64 == '\0')) {
2880Sstevel@tonic-gate 		prefile_64 = MSG_ORIG(MSG_STR_EMPTY);
2890Sstevel@tonic-gate 	}
2900Sstevel@tonic-gate 	if (((prefile = getenv(MSG_ORIG(MSG_LD_PRELOAD))) == NULL) ||
2910Sstevel@tonic-gate 	    (*prefile == '\0')) {
2920Sstevel@tonic-gate 		prefile = MSG_ORIG(MSG_STR_EMPTY);
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
2960Sstevel@tonic-gate 	 * Determine if any environment requests are for the LD_PRELOAD family,
2970Sstevel@tonic-gate 	 * and if so override any environment settings we've established above.
2980Sstevel@tonic-gate 	 */
2990Sstevel@tonic-gate 	for (LIST_TRAVERSE(&eopts, lnp, str)) {
3000Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_32),
3010Sstevel@tonic-gate 		    MSG_LD_PRELOAD_32_SIZE)) == 0) {
3020Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_32_SIZE;
3030Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
3040Sstevel@tonic-gate 				prefile_32 = str;
3050Sstevel@tonic-gate 			continue;
3060Sstevel@tonic-gate 		}
3070Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_64),
3080Sstevel@tonic-gate 		    MSG_LD_PRELOAD_64_SIZE)) == 0) {
3090Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_64_SIZE;
3100Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
3110Sstevel@tonic-gate 				prefile_64 = str;
3120Sstevel@tonic-gate 			continue;
3130Sstevel@tonic-gate 		}
3140Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD),
3150Sstevel@tonic-gate 		    MSG_LD_PRELOAD_SIZE)) == 0) {
3160Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_SIZE;
3170Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
3180Sstevel@tonic-gate 				prefile = str;
3190Sstevel@tonic-gate 			continue;
3200Sstevel@tonic-gate 		}
3210Sstevel@tonic-gate 	}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 	/*
3240Sstevel@tonic-gate 	 * Set the appropriate relocation environment variables (Note unsetting
3250Sstevel@tonic-gate 	 * the environment variables is done just in case the user already
3260Sstevel@tonic-gate 	 * has these in their environment ... sort of thing the test folks
3270Sstevel@tonic-gate 	 * would do :-)
3280Sstevel@tonic-gate 	 */
3294947Srie 	warn[sizeof (warn) - 2] = (dflag || rflag || Uflag || uflag) ? '1' :
3300Sstevel@tonic-gate 	    '\0';
3314947Srie 	bind[sizeof (bind) - 2] = (rflag) ? '1' : '\0';
3324947Srie 	path[sizeof (path) - 2] = (sflag) ? '1' : '\0';
3334947Srie 	verb[sizeof (verb) - 2] = (vflag) ? '1' : '\0';
3344947Srie 	fltr[sizeof (fltr) - 2] = (Lflag) ? '\0' : (lflag) ? '2' : '1';
3354947Srie 	init[sizeof (init) - 2] = (iflag) ? '1' : '\0';
3364947Srie 	conf[sizeof (conf) - 2] = (cflag) ? '1' : '\0';
3374947Srie 	lazy[sizeof (lazy) - 2] = (Lflag) ? '\0' : '1';
3384947Srie 	uref[sizeof (uref) - 2] = (Uflag) ? '1' : '\0';
3394947Srie 	used[sizeof (used) - 2] = (uflag) ? '1' : '\0';
3404947Srie 	weak[sizeof (weak) - 2] = (wflag) ? '1' : '\0';
3416150Srie 	nope[sizeof (nope) - 2] = (pflag) ? '1' : '\0';
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	/*
3440Sstevel@tonic-gate 	 * coordinate libelf's version information
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
3470Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_LIBELF), cname,
3480Sstevel@tonic-gate 		    EV_CURRENT);
3490Sstevel@tonic-gate 		exit(1);
3500Sstevel@tonic-gate 	}
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	/*
3530Sstevel@tonic-gate 	 * Loop through remaining arguments.  Note that from here on there
3540Sstevel@tonic-gate 	 * are no exit conditions so that we can process a list of files,
3550Sstevel@tonic-gate 	 * any error condition is retained for a final exit status.
3560Sstevel@tonic-gate 	 */
3570Sstevel@tonic-gate 	nfile = argc - optind;
3580Sstevel@tonic-gate 	for (; optind < argc; optind++) {
3590Sstevel@tonic-gate 		char	*fname = argv[optind];
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 		/*
3620Sstevel@tonic-gate 		 * Open file (do this before checking access so that we can
3630Sstevel@tonic-gate 		 * provide the user with better diagnostics).
3640Sstevel@tonic-gate 		 */
3650Sstevel@tonic-gate 		if ((var = open(fname, O_RDONLY)) == -1) {
3660Sstevel@tonic-gate 			int	err = errno;
3670Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), cname,
3680Sstevel@tonic-gate 			    fname, strerror(err));
3690Sstevel@tonic-gate 			error = 1;
3700Sstevel@tonic-gate 			continue;
3710Sstevel@tonic-gate 		}
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 		/*
3740Sstevel@tonic-gate 		 * Get the files elf descriptor and process it as an elf or
3750Sstevel@tonic-gate 		 * a.out (4.x) file.
3760Sstevel@tonic-gate 		 */
3770Sstevel@tonic-gate 		elf = elf_begin(var, ELF_C_READ, (Elf *)0);
3780Sstevel@tonic-gate 		switch (elf_kind(elf)) {
3790Sstevel@tonic-gate 		case ELF_K_AR :
3800Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO),
3810Sstevel@tonic-gate 			    cname, fname);
3820Sstevel@tonic-gate 			error = 1;
3830Sstevel@tonic-gate 			break;
3840Sstevel@tonic-gate 		case ELF_K_COFF:
3850Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN),
3860Sstevel@tonic-gate 			    cname, fname);
3870Sstevel@tonic-gate 			error = 1;
3880Sstevel@tonic-gate 			break;
3890Sstevel@tonic-gate 		case ELF_K_ELF:
3900Sstevel@tonic-gate 			if (elf_check(nfile, fname, cname, elf, fflag) != NULL)
3910Sstevel@tonic-gate 				error = 1;
3920Sstevel@tonic-gate 			break;
3930Sstevel@tonic-gate 		default:
3940Sstevel@tonic-gate 			/*
3950Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
3960Sstevel@tonic-gate 			 */
3970Sstevel@tonic-gate 			if (aout_check(nfile, fname, cname, var, fflag) != NULL)
3980Sstevel@tonic-gate 				error = 1;
3990Sstevel@tonic-gate 			break;
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 		(void) elf_end(elf);
4020Sstevel@tonic-gate 		(void) close(var);
4030Sstevel@tonic-gate 	}
4040Sstevel@tonic-gate 	return (error);
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate static int
4100Sstevel@tonic-gate is_runnable(GElf_Ehdr *ehdr)
4110Sstevel@tonic-gate {
412*6223Sab196087 	if ((ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
4130Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == M_DATA))
4140Sstevel@tonic-gate 		return (ELFCLASS32);
4150Sstevel@tonic-gate 
4163731Srie #if	defined(__sparc)
4170Sstevel@tonic-gate 	if ((ehdr->e_machine == EM_SPARCV9) &&
4180Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == M_DATA) &&
4190Sstevel@tonic-gate 	    (conv_sys_eclass() == ELFCLASS64))
4200Sstevel@tonic-gate 		return (ELFCLASS64);
4213731Srie #elif	defined(__x86)
4220Sstevel@tonic-gate 	if ((ehdr->e_machine == EM_AMD64) &&
4230Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) &&
4240Sstevel@tonic-gate 	    (conv_sys_eclass() == ELFCLASS64))
4250Sstevel@tonic-gate 		return (ELFCLASS64);
4260Sstevel@tonic-gate #endif
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	return (ELFCLASSNONE);
4290Sstevel@tonic-gate }
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate static int
4330Sstevel@tonic-gate elf_check(int nfile, char *fname, char *cname, Elf *elf, int fflag)
4340Sstevel@tonic-gate {
4350Sstevel@tonic-gate 	GElf_Ehdr 	ehdr;
4360Sstevel@tonic-gate 	GElf_Phdr 	phdr;
4370Sstevel@tonic-gate 	int		dynamic = 0, interp = 0, cnt, class;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	/*
4400Sstevel@tonic-gate 	 * verify information in file header
4410Sstevel@tonic-gate 	 */
4420Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
4430Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR),
4444947Srie 		    cname, fname, elf_errmsg(-1));
4450Sstevel@tonic-gate 		return (1);
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	/*
4490Sstevel@tonic-gate 	 * check class and encoding
4500Sstevel@tonic-gate 	 */
4510Sstevel@tonic-gate 	if ((class = is_runnable(&ehdr)) == ELFCLASSNONE) {
4520Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_CLASSDATA),
4534947Srie 		    cname, fname);
4540Sstevel@tonic-gate 		return (1);
4550Sstevel@tonic-gate 	}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	/*
4580Sstevel@tonic-gate 	 * check type
4590Sstevel@tonic-gate 	 */
4600Sstevel@tonic-gate 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN) &&
4610Sstevel@tonic-gate 	    (ehdr.e_type != ET_REL)) {
4620Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_BADMAGIC),
4634947Srie 		    cname, fname);
4640Sstevel@tonic-gate 		return (1);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	if ((class == ELFCLASS32) && (ehdr.e_machine != M_MACH)) {
4670Sstevel@tonic-gate 		if (ehdr.e_machine != M_MACHPLUS) {
4680Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHTYPE),
4694947Srie 			    cname, fname);
4700Sstevel@tonic-gate 			return (1);
4710Sstevel@tonic-gate 		}
4720Sstevel@tonic-gate 		if ((ehdr.e_flags & M_FLAGSPLUS) == 0) {
4730Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHFLAGS),
4744947Srie 			    cname, fname);
4750Sstevel@tonic-gate 			return (1);
4760Sstevel@tonic-gate 		}
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Check that the file is executable.  Dynamic executables must be
4810Sstevel@tonic-gate 	 * executable to be exec'ed.  Shared objects need not be executable to
4820Sstevel@tonic-gate 	 * be mapped with a dynamic executable, however, by convention they're
4830Sstevel@tonic-gate 	 * supposed to be executable.
4840Sstevel@tonic-gate 	 */
4850Sstevel@tonic-gate 	if (access(fname, X_OK) != 0) {
4860Sstevel@tonic-gate 		if (ehdr.e_type == ET_EXEC) {
4870Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_1),
4884947Srie 			    cname, fname);
4890Sstevel@tonic-gate 			return (1);
4900Sstevel@tonic-gate 		}
4910Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_2), cname,
4920Sstevel@tonic-gate 		    fname);
4930Sstevel@tonic-gate 	}
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * Determine whether we have a dynamic section or interpretor.
4970Sstevel@tonic-gate 	 */
4980Sstevel@tonic-gate 	for (cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) {
4990Sstevel@tonic-gate 		if (dynamic && interp)
5000Sstevel@tonic-gate 			break;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 		if (gelf_getphdr(elf, cnt, &phdr) == NULL) {
5030Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETPHDR),
5044947Srie 			    cname, fname, elf_errmsg(-1));
5050Sstevel@tonic-gate 			return (1);
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 		if (phdr.p_type == PT_DYNAMIC) {
5090Sstevel@tonic-gate 			dynamic = 1;
5100Sstevel@tonic-gate 			continue;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		if (phdr.p_type != PT_INTERP)
5140Sstevel@tonic-gate 			continue;
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 		interp = 1;
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 		/*
5190Sstevel@tonic-gate 		 * If fflag is not set, and euid == root, and the interpreter
5200Sstevel@tonic-gate 		 * does not live under /lib, /usr/lib or /etc/lib then don't
5210Sstevel@tonic-gate 		 * allow ldd to execute the image.  This prevents someone
5220Sstevel@tonic-gate 		 * creating a `trojan horse' by substituting their own
5230Sstevel@tonic-gate 		 * interpreter that could preform privileged operations
5240Sstevel@tonic-gate 		 * when ldd is against it.
5250Sstevel@tonic-gate 		 */
5260Sstevel@tonic-gate 		if ((fflag == 0) && (geteuid() == 0) &&
5270Sstevel@tonic-gate 		    (strcmp(fname, conv_lddstub(class)) != 0)) {
5280Sstevel@tonic-gate 			char	*interpreter;
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 			/*
5310Sstevel@tonic-gate 			 * Does the interpreter live under a trusted directory.
5320Sstevel@tonic-gate 			 */
5330Sstevel@tonic-gate 			interpreter = elf_getident(elf, 0) + phdr.p_offset;
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 			if ((strncmp(interpreter, MSG_ORIG(MSG_PTH_USRLIB),
5360Sstevel@tonic-gate 			    MSG_PTH_USRLIB_SIZE) != 0) &&
5370Sstevel@tonic-gate 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_LIB),
5380Sstevel@tonic-gate 			    MSG_PTH_LIB_SIZE) != 0) &&
5390Sstevel@tonic-gate 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_ETCLIB),
5400Sstevel@tonic-gate 			    MSG_PTH_ETCLIB_SIZE) != 0)) {
5410Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_USP_ELFINS),
5424947Srie 				    cname, fname, interpreter);
5430Sstevel@tonic-gate 				return (1);
5440Sstevel@tonic-gate 			}
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	/*
5490Sstevel@tonic-gate 	 * Catch the case of a static executable (ie, an ET_EXEC that has a set
5500Sstevel@tonic-gate 	 * of program headers but no PT_DYNAMIC).
5510Sstevel@tonic-gate 	 */
5520Sstevel@tonic-gate 	if (ehdr.e_phnum && !dynamic) {
5530Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname,
5540Sstevel@tonic-gate 		    fname);
5550Sstevel@tonic-gate 		return (1);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 
5581698Sab196087 	/*
5591698Sab196087 	 * If there is a dynamic section, then check for the DF_1_NOHDR
5601698Sab196087 	 * flag, and bail if it is present. Those objects are created using
5611698Sab196087 	 * the ?N mapfile option: The ELF header and program headers are
5621698Sab196087 	 * not mapped as part of the first segment, and virtual addresses
5631698Sab196087 	 * are computed without them. If ldd tries to interpret such
5641698Sab196087 	 * a file, it will become confused and generate bad output or
5651698Sab196087 	 * crash. Such objects are always special purpose files (like an OS
5661698Sab196087 	 * kernel) --- files for which the ldd operation doesn't make sense.
5671698Sab196087 	 */
5681698Sab196087 	if (dynamic && (_gelf_getdyndtflags_1(elf) & DF_1_NOHDR)) {
5691698Sab196087 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOHDR), cname,
5701698Sab196087 		    fname);
5711698Sab196087 		return (1);
5721698Sab196087 	}
5731698Sab196087 
5740Sstevel@tonic-gate 	load = load_elf;
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 	/*
5770Sstevel@tonic-gate 	 * Run the required program (shared and relocatable objects require the
5780Sstevel@tonic-gate 	 * use of lddstub).
5790Sstevel@tonic-gate 	 */
5800Sstevel@tonic-gate 	if ((ehdr.e_type == ET_EXEC) && interp)
5810Sstevel@tonic-gate 		return (run(nfile, cname, fname, (const char *)fname, class));
5820Sstevel@tonic-gate 	else
5830Sstevel@tonic-gate 		return (run(nfile, cname, fname, conv_lddstub(class), class));
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate static int
5870Sstevel@tonic-gate aout_check(int nfile, char *fname, char *cname, int fd, int fflag)
5880Sstevel@tonic-gate {
589*6223Sab196087 	struct exec32	aout;
5900Sstevel@tonic-gate 	int		err;
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (lseek(fd, 0, SEEK_SET) != 0) {
5930Sstevel@tonic-gate 		err = errno;
5940Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_LSEEK), cname, fname,
5950Sstevel@tonic-gate 		    strerror(err));
5960Sstevel@tonic-gate 		return (1);
5970Sstevel@tonic-gate 	}
598*6223Sab196087 	if (read(fd, (char *)&aout, sizeof (aout)) != sizeof (aout)) {
5990Sstevel@tonic-gate 		err = errno;
6000Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_READ), cname, fname,
6010Sstevel@tonic-gate 		    strerror(err));
6020Sstevel@tonic-gate 		return (1);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 	if (aout.a_machtype != M_SPARC) {
6050Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN), cname, fname);
6060Sstevel@tonic-gate 		return (1);
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 	if (N_BADMAG(aout) || !aout.a_dynamic) {
6090Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname,
6100Sstevel@tonic-gate 		    fname);
6110Sstevel@tonic-gate 		return (1);
6120Sstevel@tonic-gate 	}
6130Sstevel@tonic-gate 	if (!fflag && (geteuid() == 0)) {
6140Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_AOUTINS), cname, fname);
6150Sstevel@tonic-gate 		return (1);
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	/*
6190Sstevel@tonic-gate 	 * Run the required program.
6200Sstevel@tonic-gate 	 */
621*6223Sab196087 	if ((aout.a_magic == ZMAGIC) && (aout.a_entry <= sizeof (aout))) {
6220Sstevel@tonic-gate 		load = load_elf;
6230Sstevel@tonic-gate 		return (run(nfile, cname, fname, conv_lddstub(ELFCLASS32),
6240Sstevel@tonic-gate 		    ELFCLASS32));
6250Sstevel@tonic-gate 	} else {
6260Sstevel@tonic-gate 		load = load_aout;
6270Sstevel@tonic-gate 		return (run(nfile, cname, fname, (const char *)fname,
6280Sstevel@tonic-gate 		    ELFCLASS32));
6290Sstevel@tonic-gate 	}
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate /*
6340Sstevel@tonic-gate  * Run the required program, setting the preload and trace environment
6350Sstevel@tonic-gate  * variables accordingly.
6360Sstevel@tonic-gate  */
6370Sstevel@tonic-gate static int
6380Sstevel@tonic-gate run(int nfile, char *cname, char *fname, const char *ename, int class)
6390Sstevel@tonic-gate {
6400Sstevel@tonic-gate 	const char	*preload = 0;
6410Sstevel@tonic-gate 	int		pid, status;
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
6440Sstevel@tonic-gate 		int	err = errno;
6450Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK), cname,
6460Sstevel@tonic-gate 		    strerror(err));
6470Sstevel@tonic-gate 		return (1);
6480Sstevel@tonic-gate 	}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	if (pid) {				/* parent */
6510Sstevel@tonic-gate 		while (wait(&status) != pid)
6520Sstevel@tonic-gate 			;
6530Sstevel@tonic-gate 		if (WIFSIGNALED(status) && ((WSIGMASK & status) != SIGPIPE)) {
6540Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
6550Sstevel@tonic-gate 			    fname);
6560Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_SIG),
6570Sstevel@tonic-gate 			    (WSIGMASK & status), ((status & WCOREFLG) ?
6580Sstevel@tonic-gate 			    MSG_INTL(MSG_SYS_EXEC_CORE) :
6590Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_EMPTY)));
6600Sstevel@tonic-gate 			status = 1;
6610Sstevel@tonic-gate 		} else if (WHIBYTE(status)) {
6620Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
6630Sstevel@tonic-gate 			    fname);
6640Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_STAT),
6650Sstevel@tonic-gate 			    WHIBYTE(status));
6660Sstevel@tonic-gate 			status = 1;
6670Sstevel@tonic-gate 		}
6680Sstevel@tonic-gate 	} else {				/* child */
6690Sstevel@tonic-gate 		Listnode	*lnp;
6700Sstevel@tonic-gate 		char		*str;
6710Sstevel@tonic-gate 		size_t		size;
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 		/*
6740Sstevel@tonic-gate 		 * When using ldd(1) to analyze a shared object we preload the
6750Sstevel@tonic-gate 		 * shared object with lddstub.  Any additional preload
6760Sstevel@tonic-gate 		 * requirements are added after the object being analyzed, this
6770Sstevel@tonic-gate 		 * allows us to skip the first object but produce diagnostics
6780Sstevel@tonic-gate 		 * for each other preloaded object.
6790Sstevel@tonic-gate 		 */
6800Sstevel@tonic-gate 		if (fname != ename) {
6810Sstevel@tonic-gate 			char		*str;
6820Sstevel@tonic-gate 			const char	*files = prefile;
6830Sstevel@tonic-gate 			const char	*format = MSG_ORIG(MSG_STR_FMT1);
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 			for (str = fname; *str; str++)
6860Sstevel@tonic-gate 				if (*str == '/') {
6870Sstevel@tonic-gate 					format = MSG_ORIG(MSG_STR_FMT2);
6880Sstevel@tonic-gate 					break;
6890Sstevel@tonic-gate 			}
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 			preload = MSG_ORIG(MSG_LD_PRELOAD);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 			/*
6940Sstevel@tonic-gate 			 * Determine which preload files and preload environment
6950Sstevel@tonic-gate 			 * variable to use.
6960Sstevel@tonic-gate 			 */
6970Sstevel@tonic-gate 			if (class == ELFCLASS64) {
6980Sstevel@tonic-gate 				if (prefile_64 != MSG_ORIG(MSG_STR_EMPTY)) {
6990Sstevel@tonic-gate 					files = prefile_64;
7000Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_64);
7010Sstevel@tonic-gate 				}
7020Sstevel@tonic-gate 			} else {
7030Sstevel@tonic-gate 				if (prefile_32 != MSG_ORIG(MSG_STR_EMPTY)) {
7040Sstevel@tonic-gate 					files = prefile_32;
7050Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_32);
7060Sstevel@tonic-gate 				}
7070Sstevel@tonic-gate 			}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 			if ((str = (char *)malloc(strlen(preload) +
7100Sstevel@tonic-gate 			    strlen(fname) + strlen(files) + 5)) == 0) {
7110Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
7120Sstevel@tonic-gate 				    cname);
7130Sstevel@tonic-gate 				exit(1);
7140Sstevel@tonic-gate 			}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 			(void) sprintf(str, format, preload, fname, files);
7170Sstevel@tonic-gate 			if (putenv(str) != 0) {
7180Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
7190Sstevel@tonic-gate 				    cname);
7200Sstevel@tonic-gate 				exit(1);
7210Sstevel@tonic-gate 			}
7224947Srie 
7234947Srie 			/*
7244947Srie 			 * The pointer "load" has be assigned to load_elf[] or
7254947Srie 			 * load_aout[].  Use the size of load_elf[] as the size
7264947Srie 			 * of load_aout[] is the same.
7274947Srie 			 */
7284947Srie 			load[sizeof (load_elf) - 2] = '2';
7290Sstevel@tonic-gate 		} else
7304947Srie 			load[sizeof (load_elf) - 2] = '1';
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 		/*
7340Sstevel@tonic-gate 		 * Establish new environment variables to affect the child
7350Sstevel@tonic-gate 		 * process.
7360Sstevel@tonic-gate 		 */
7370Sstevel@tonic-gate 		if ((putenv(warn) != 0) || (putenv(bind) != 0) ||
7380Sstevel@tonic-gate 		    (putenv(path) != 0) || (putenv(verb) != 0) ||
7390Sstevel@tonic-gate 		    (putenv(fltr) != 0) || (putenv(conf) != 0) ||
7400Sstevel@tonic-gate 		    (putenv(init) != 0) || (putenv(lazy) != 0) ||
7410Sstevel@tonic-gate 		    (putenv(uref) != 0) || (putenv(used) != 0) ||
7426150Srie 		    (putenv(weak) != 0) || (putenv(load) != 0) ||
7436150Srie 		    (putenv(nope) != 0)) {
7440Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), cname);
7450Sstevel@tonic-gate 			exit(1);
7460Sstevel@tonic-gate 		}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 		/*
7490Sstevel@tonic-gate 		 * Establish explicit environment requires (but don't override
7500Sstevel@tonic-gate 		 * any preload request established to process a shared object).
7510Sstevel@tonic-gate 		 */
7520Sstevel@tonic-gate 		size = 0;
7530Sstevel@tonic-gate 		for (LIST_TRAVERSE(&eopts, lnp, str)) {
7540Sstevel@tonic-gate 			if (preload) {
7550Sstevel@tonic-gate 				if (size == 0)
7560Sstevel@tonic-gate 					size = strlen(preload);
7570Sstevel@tonic-gate 				if ((strncmp(preload, str, size) == 0) &&
7580Sstevel@tonic-gate 				    (str[size] == '=')) {
7590Sstevel@tonic-gate 					continue;
7600Sstevel@tonic-gate 				}
7610Sstevel@tonic-gate 			}
7620Sstevel@tonic-gate 			if (putenv(str) != 0) {
7630Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
7640Sstevel@tonic-gate 				    cname);
7650Sstevel@tonic-gate 				exit(1);
7660Sstevel@tonic-gate 			}
7670Sstevel@tonic-gate 		}
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 		/*
7700Sstevel@tonic-gate 		 * Execute the object and let ld.so.1 do the rest.
7710Sstevel@tonic-gate 		 */
7720Sstevel@tonic-gate 		if (nfile > 1)
7730Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_STR_FMT3), fname);
7740Sstevel@tonic-gate 		(void) fflush(stdout);
7750Sstevel@tonic-gate 		if ((execl(ename, ename, (char *)0)) == -1) {
7760Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
7770Sstevel@tonic-gate 			    fname);
7780Sstevel@tonic-gate 			perror(ename);
7790Sstevel@tonic-gate 			_exit(0);
7800Sstevel@tonic-gate 			/* NOTREACHED */
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 	return (status);
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate const char *
7870Sstevel@tonic-gate _ldd_msg(Msg mid)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
7900Sstevel@tonic-gate }
791