xref: /illumos-gate/usr/src/cmd/sgs/ldd/common/ldd.c (revision 69112edd)
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
54899432aSab  * Common Development and Distribution License (the "License").
64899432aSab  * 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
20dae2dfb7Srie  *
217c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
227c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  *
25*69112eddSAli Bahrami  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
2602ca3e02Srie  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Print the list of shared objects required by a dynamic executable or shared
317c478bd9Sstevel@tonic-gate  * object.
327c478bd9Sstevel@tonic-gate  *
33dae2dfb7Srie  * usage is: ldd [-d | -r] [-c] [-e envar] [-i] [-f] [-L] [-l] [-p] [-s]
34df4628cbSrie  *		[-U | -u] [-v] [-w] file(s)
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * ldd opens the file and verifies the information in the elf header.
377c478bd9Sstevel@tonic-gate  * If the file is a dynamic executable, we set up some environment variables
387c478bd9Sstevel@tonic-gate  * and exec(2) the file.  If the file is a shared object, we preload the
397c478bd9Sstevel@tonic-gate  * file with a dynamic executable stub. The runtime linker (ld.so.1) actually
407c478bd9Sstevel@tonic-gate  * provides the diagnostic output, according to the environment variables set.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_[AE].
437c478bd9Sstevel@tonic-gate  * The runtime linker will print the pathnames of all dynamic objects it
447c478bd9Sstevel@tonic-gate  * loads, and then exit.  Note that we distiguish between ELF and AOUT objects
457c478bd9Sstevel@tonic-gate  * when setting this environment variable - AOUT executables cause the mapping
467c478bd9Sstevel@tonic-gate  * of sbcp, the dependencies of which the user isn't interested in.
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * If -d or -r is specified, we also set LD_WARN=1; the runtime linker will
497c478bd9Sstevel@tonic-gate  * perform its normal relocations and issue warning messages for unresolved
507c478bd9Sstevel@tonic-gate  * references. It will then exit.
517c478bd9Sstevel@tonic-gate  * If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker
527c478bd9Sstevel@tonic-gate  * will perform all relocations, otherwise (under -d) the runtime linker
537c478bd9Sstevel@tonic-gate  * will not perform PLT (function) type relocations.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * If -c is specified we also set LD_NOCONFIG=1, thus disabling any
567c478bd9Sstevel@tonic-gate  * configuration file use.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * If -e is specified the associated environment variable is set for the
597c478bd9Sstevel@tonic-gate  * child process that will produce ldd's diagnostics.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  * If -i is specified, we set LD_INIT=1. The order of inititialization
627c478bd9Sstevel@tonic-gate  * sections to be executed is printed. We also set LD_WARN=1.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * If -f is specified, we will run ldd as root on executables that have
657c478bd9Sstevel@tonic-gate  * an unsercure runtime linker that does not live under the "/usr/lib"
667c478bd9Sstevel@tonic-gate  * directory.  By default we will not let this happen.
677c478bd9Sstevel@tonic-gate  *
687c478bd9Sstevel@tonic-gate  * If -l is specified it generates a warning for any auxiliary filter not found.
697c478bd9Sstevel@tonic-gate  * Prior to 2.8 this forced any filters to load (all) their filtees.  This is
707c478bd9Sstevel@tonic-gate  * now the default, however missing auxiliary filters don't generate any error
717c478bd9Sstevel@tonic-gate  * diagniostic.  See also -L.
727c478bd9Sstevel@tonic-gate  *
737c478bd9Sstevel@tonic-gate  * If -L is specified we revert to lazy loading, thus any filtee or lazy
747c478bd9Sstevel@tonic-gate  * dependency loading is deferred until relocations cause loading.  Without
757c478bd9Sstevel@tonic-gate  * this option we set LD_LOADFLTR=1, thus forcing any filters to load (all)
767c478bd9Sstevel@tonic-gate  * their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of
777c478bd9Sstevel@tonic-gate  * any lazy loaded dependencies.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling
807c478bd9Sstevel@tonic-gate  * the runtime linker to indicate the search algorithm used.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  * If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime
837c478bd9Sstevel@tonic-gate  * linker to indicate all object dependencies (not just the first object
847c478bd9Sstevel@tonic-gate  * loaded) together with any versionig requirements.
857c478bd9Sstevel@tonic-gate  *
867c478bd9Sstevel@tonic-gate  * If -U or -u is specified unused dependencies are detected.  -u causes
877c478bd9Sstevel@tonic-gate  * LD_UNUSED=1 to be set, which causes dependencies that are unused within the
887c478bd9Sstevel@tonic-gate  * process to be detected.  -U causes LD_UNREF=1 to be set, which causes
897c478bd9Sstevel@tonic-gate  * unreferenced objects, and unreferenced cyclic dependencies to be detected.
907c478bd9Sstevel@tonic-gate  * These options assert that at least -d is set as relocation references are
917c478bd9Sstevel@tonic-gate  * what determine an objects use.
92df4628cbSrie  *
93df4628cbSrie  * If -w is specified, no unresolved weak references are allowed.  -w causes
94df4628cbSrie  * LD_NOUNRESWEAK=1 to be set.  By default, an unresolved weak reference is
95df4628cbSrie  * allowed, and a "0" is written to the relocation offset.  The -w option
96df4628cbSrie  * disables this default.  Any weak references that can not be resolved result
97dae2dfb7Srie  * in relocation error messages.  This option has no use without -r or -d.
98dae2dfb7Srie  *
99dae2dfb7Srie  * If the -p option is specified, no unresolved PARENT or EXTERN references are
100dae2dfb7Srie  * allowed.  -p causes LD_NOPAREXT=1 to be set.  By default, PARENT and EXTERN
101dae2dfb7Srie  * references, which have been explicitly assigned via a mapfile when a shared
102dae2dfb7Srie  * object was built, imply that a caller will provide the symbols, and hence
103dae2dfb7Srie  * these are not reported as relocation errors.  Note, the -p option is asserted
104dae2dfb7Srie  * by default when either the -r or -d options are used to inspect a dynamic
105dae2dfb7Srie  * executable.  This option has no use with a shared object without -r or -d.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate #include	<fcntl.h>
1087c478bd9Sstevel@tonic-gate #include	<stdio.h>
1097c478bd9Sstevel@tonic-gate #include	<string.h>
1104899432aSab #include	<_libelf.h>
1117c478bd9Sstevel@tonic-gate #include	<stdlib.h>
1127c478bd9Sstevel@tonic-gate #include	<unistd.h>
1137c478bd9Sstevel@tonic-gate #include	<wait.h>
1147c478bd9Sstevel@tonic-gate #include	<locale.h>
1157c478bd9Sstevel@tonic-gate #include	<errno.h>
1167c478bd9Sstevel@tonic-gate #include	<signal.h>
1177c478bd9Sstevel@tonic-gate #include	"machdep.h"
1187c478bd9Sstevel@tonic-gate #include	"sgs.h"
1197c478bd9Sstevel@tonic-gate #include	"conv.h"
1207c478bd9Sstevel@tonic-gate #include	"a.out.h"
1217c478bd9Sstevel@tonic-gate #include	"msg.h"
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static int	elf_check(int, char *, char *, Elf *, int);
1247c478bd9Sstevel@tonic-gate static int	aout_check(int, char *, char *, int, int);
1257c478bd9Sstevel@tonic-gate static int	run(int, char *, char *, const char *, int);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
129df4628cbSrie  * Define all environment variable strings.  The character following the "="
130df4628cbSrie  * will be written to, to disable or enable the associated feature.
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate static char	bind[] =	"LD_BIND_NOW= ",
1337c478bd9Sstevel@tonic-gate 		load_elf[] =	"LD_TRACE_LOADED_OBJECTS_E= ",
1347c478bd9Sstevel@tonic-gate 		load_aout[] =	"LD_TRACE_LOADED_OBJECTS_A= ",
1357c478bd9Sstevel@tonic-gate 		path[] =	"LD_TRACE_SEARCH_PATHS= ",
1367c478bd9Sstevel@tonic-gate 		verb[] =	"LD_VERBOSE= ",
1377c478bd9Sstevel@tonic-gate 		warn[] =	"LD_WARN= ",
1387c478bd9Sstevel@tonic-gate 		conf[] =	"LD_NOCONFIG= ",
1397c478bd9Sstevel@tonic-gate 		fltr[] =	"LD_LOADFLTR= ",
1407c478bd9Sstevel@tonic-gate 		lazy[] =	"LD_NOLAZYLOAD=1",
1417c478bd9Sstevel@tonic-gate 		init[] =	"LD_INIT= ",
1427c478bd9Sstevel@tonic-gate 		uref[] =	"LD_UNREF= ",
143df4628cbSrie 		used[] =	"LD_UNUSED= ",
144dae2dfb7Srie 		weak[] =	"LD_NOUNRESWEAK= ",
145dae2dfb7Srie 		nope[] =	"LD_NOPAREXT= ";
1467c478bd9Sstevel@tonic-gate static char	*load;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static const char	*prefile_32, *prefile_64, *prefile;
14957ef7aa9SRod Evans static APlist		*eopts = NULL;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate int
152df14233eSab main(int argc, char **argv, char **envp)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	char	*str, *cname = argv[0];
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	Elf	*elf;
1577c478bd9Sstevel@tonic-gate 	int	cflag = 0, dflag = 0, fflag = 0, iflag = 0, Lflag = 0;
1587c478bd9Sstevel@tonic-gate 	int	lflag = 0, rflag = 0, sflag = 0, Uflag = 0, uflag = 0;
159dae2dfb7Srie 	int	pflag = 0, vflag = 0, wflag = 0, nfile, var, error = 0;
1607c478bd9Sstevel@tonic-gate 
16157ef7aa9SRod Evans 	Aliste	idx;
1627c478bd9Sstevel@tonic-gate 
163df14233eSab 	/*
164df14233eSab 	 * If we're on a 64-bit kernel, try to exec a full 64-bit version of
165df14233eSab 	 * the binary.  If successful, conv_check_native() won't return.
166df14233eSab 	 *
167df14233eSab 	 * This is done to ensure that ldd can handle objects >2GB.
168df14233eSab 	 * ldd uses libelf, which is not large file capable. The
169df14233eSab 	 * 64-bit ldd can handle any sized object.
170df14233eSab 	 */
171df14233eSab 	(void) conv_check_native(argv, envp);
172df14233eSab 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * Establish locale.
1757c478bd9Sstevel@tonic-gate 	 */
1767c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
1777c478bd9Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * verify command line syntax and process arguments
1817c478bd9Sstevel@tonic-gate 	 */
1827c478bd9Sstevel@tonic-gate 	opterr = 0;				/* disable getopt error mesg */
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_GETOPT))) != EOF) {
1857c478bd9Sstevel@tonic-gate 		switch (var) {
1867c478bd9Sstevel@tonic-gate 		case 'c' :			/* enable config search */
1877c478bd9Sstevel@tonic-gate 			cflag = 1;
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 		case 'd' :			/* perform data relocations */
1907c478bd9Sstevel@tonic-gate 			dflag = 1;
1917c478bd9Sstevel@tonic-gate 			if (rflag)
1927c478bd9Sstevel@tonic-gate 				error++;
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		case 'e' :
19557ef7aa9SRod Evans 			if (aplist_append(&eopts, optarg, 10) == NULL) {
1967c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
1977c478bd9Sstevel@tonic-gate 				    cname);
1987c478bd9Sstevel@tonic-gate 				exit(1);
1997c478bd9Sstevel@tonic-gate 			}
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 		case 'f' :
2027c478bd9Sstevel@tonic-gate 			fflag = 1;
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 		case 'L' :
2057c478bd9Sstevel@tonic-gate 			Lflag = 1;
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 		case 'l' :
2087c478bd9Sstevel@tonic-gate 			lflag = 1;
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 		case 'i' :			/* print the order of .init */
2117c478bd9Sstevel@tonic-gate 			iflag = 1;
2127c478bd9Sstevel@tonic-gate 			break;
213dae2dfb7Srie 		case 'p' :
214dae2dfb7Srie 			pflag = 1;		/* expose unreferenced */
215dae2dfb7Srie 			break;			/*	parent or externals */
2167c478bd9Sstevel@tonic-gate 		case 'r' :			/* perform all relocations */
2177c478bd9Sstevel@tonic-gate 			rflag = 1;
2187c478bd9Sstevel@tonic-gate 			if (dflag)
2197c478bd9Sstevel@tonic-gate 				error++;
2207c478bd9Sstevel@tonic-gate 			break;
2217c478bd9Sstevel@tonic-gate 		case 's' :			/* enable search path output */
2227c478bd9Sstevel@tonic-gate 			sflag = 1;
2237c478bd9Sstevel@tonic-gate 			break;
2247c478bd9Sstevel@tonic-gate 		case 'U' :			/* list unreferenced */
2257c478bd9Sstevel@tonic-gate 			Uflag = 1;		/*	dependencies */
2267c478bd9Sstevel@tonic-gate 			if (uflag)
2277c478bd9Sstevel@tonic-gate 				error++;
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		case 'u' :			/* list unused dependencies */
2307c478bd9Sstevel@tonic-gate 			uflag = 1;
2317c478bd9Sstevel@tonic-gate 			if (Uflag)
2327c478bd9Sstevel@tonic-gate 				error++;
2337c478bd9Sstevel@tonic-gate 			break;
2347c478bd9Sstevel@tonic-gate 		case 'v' :			/* enable verbose output */
2357c478bd9Sstevel@tonic-gate 			vflag = 1;
2367c478bd9Sstevel@tonic-gate 			break;
237dae2dfb7Srie 		case 'w' :			/* expose unresolved weak */
238df4628cbSrie 			wflag = 1;		/*	references */
239df4628cbSrie 			break;
2407c478bd9Sstevel@tonic-gate 		default :
2417c478bd9Sstevel@tonic-gate 			error++;
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 		if (error)
2457c478bd9Sstevel@tonic-gate 			break;
2467c478bd9Sstevel@tonic-gate 	}
2477c478bd9Sstevel@tonic-gate 	if (error) {
2487c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), cname);
2497c478bd9Sstevel@tonic-gate 		exit(1);
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	/*
2537c478bd9Sstevel@tonic-gate 	 * Determine if any of the LD_PRELOAD family is already set in the
2547c478bd9Sstevel@tonic-gate 	 * environment, if so we'll continue to analyze each object with the
2557c478bd9Sstevel@tonic-gate 	 * appropriate setting.
2567c478bd9Sstevel@tonic-gate 	 */
2577c478bd9Sstevel@tonic-gate 	if (((prefile_32 = getenv(MSG_ORIG(MSG_LD_PRELOAD_32))) == NULL) ||
2587c478bd9Sstevel@tonic-gate 	    (*prefile_32 == '\0')) {
2597c478bd9Sstevel@tonic-gate 		prefile_32 = MSG_ORIG(MSG_STR_EMPTY);
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	if (((prefile_64 = getenv(MSG_ORIG(MSG_LD_PRELOAD_64))) == NULL) ||
2627c478bd9Sstevel@tonic-gate 	    (*prefile_64 == '\0')) {
2637c478bd9Sstevel@tonic-gate 		prefile_64 = MSG_ORIG(MSG_STR_EMPTY);
2647c478bd9Sstevel@tonic-gate 	}
2657c478bd9Sstevel@tonic-gate 	if (((prefile = getenv(MSG_ORIG(MSG_LD_PRELOAD))) == NULL) ||
2667c478bd9Sstevel@tonic-gate 	    (*prefile == '\0')) {
2677c478bd9Sstevel@tonic-gate 		prefile = MSG_ORIG(MSG_STR_EMPTY);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	/*
2717c478bd9Sstevel@tonic-gate 	 * Determine if any environment requests are for the LD_PRELOAD family,
2727c478bd9Sstevel@tonic-gate 	 * and if so override any environment settings we've established above.
2737c478bd9Sstevel@tonic-gate 	 */
27457ef7aa9SRod Evans 	for (APLIST_TRAVERSE(eopts, idx, str)) {
2757c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_32),
2767c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_32_SIZE)) == 0) {
2777c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_32_SIZE;
2787c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
2797c478bd9Sstevel@tonic-gate 				prefile_32 = str;
2807c478bd9Sstevel@tonic-gate 			continue;
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_64),
2837c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_64_SIZE)) == 0) {
2847c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_64_SIZE;
2857c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
2867c478bd9Sstevel@tonic-gate 				prefile_64 = str;
2877c478bd9Sstevel@tonic-gate 			continue;
2887c478bd9Sstevel@tonic-gate 		}
2897c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD),
2907c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_SIZE)) == 0) {
2917c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_SIZE;
2927c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
2937c478bd9Sstevel@tonic-gate 				prefile = str;
2947c478bd9Sstevel@tonic-gate 			continue;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	/*
2997c478bd9Sstevel@tonic-gate 	 * Set the appropriate relocation environment variables (Note unsetting
3007c478bd9Sstevel@tonic-gate 	 * the environment variables is done just in case the user already
3017c478bd9Sstevel@tonic-gate 	 * has these in their environment ... sort of thing the test folks
3027c478bd9Sstevel@tonic-gate 	 * would do :-)
3037c478bd9Sstevel@tonic-gate 	 */
304df4628cbSrie 	warn[sizeof (warn) - 2] = (dflag || rflag || Uflag || uflag) ? '1' :
3057c478bd9Sstevel@tonic-gate 	    '\0';
306df4628cbSrie 	bind[sizeof (bind) - 2] = (rflag) ? '1' : '\0';
307df4628cbSrie 	path[sizeof (path) - 2] = (sflag) ? '1' : '\0';
308df4628cbSrie 	verb[sizeof (verb) - 2] = (vflag) ? '1' : '\0';
309df4628cbSrie 	fltr[sizeof (fltr) - 2] = (Lflag) ? '\0' : (lflag) ? '2' : '1';
310df4628cbSrie 	init[sizeof (init) - 2] = (iflag) ? '1' : '\0';
311df4628cbSrie 	conf[sizeof (conf) - 2] = (cflag) ? '1' : '\0';
312df4628cbSrie 	lazy[sizeof (lazy) - 2] = (Lflag) ? '\0' : '1';
313df4628cbSrie 	uref[sizeof (uref) - 2] = (Uflag) ? '1' : '\0';
314df4628cbSrie 	used[sizeof (used) - 2] = (uflag) ? '1' : '\0';
315df4628cbSrie 	weak[sizeof (weak) - 2] = (wflag) ? '1' : '\0';
316dae2dfb7Srie 	nope[sizeof (nope) - 2] = (pflag) ? '1' : '\0';
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	/*
3197c478bd9Sstevel@tonic-gate 	 * coordinate libelf's version information
3207c478bd9Sstevel@tonic-gate 	 */
3217c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
3227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_LIBELF), cname,
3237c478bd9Sstevel@tonic-gate 		    EV_CURRENT);
3247c478bd9Sstevel@tonic-gate 		exit(1);
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/*
3287c478bd9Sstevel@tonic-gate 	 * Loop through remaining arguments.  Note that from here on there
3297c478bd9Sstevel@tonic-gate 	 * are no exit conditions so that we can process a list of files,
3307c478bd9Sstevel@tonic-gate 	 * any error condition is retained for a final exit status.
3317c478bd9Sstevel@tonic-gate 	 */
3327c478bd9Sstevel@tonic-gate 	nfile = argc - optind;
3337c478bd9Sstevel@tonic-gate 	for (; optind < argc; optind++) {
3347c478bd9Sstevel@tonic-gate 		char	*fname = argv[optind];
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		/*
3377c478bd9Sstevel@tonic-gate 		 * Open file (do this before checking access so that we can
3387c478bd9Sstevel@tonic-gate 		 * provide the user with better diagnostics).
3397c478bd9Sstevel@tonic-gate 		 */
3407c478bd9Sstevel@tonic-gate 		if ((var = open(fname, O_RDONLY)) == -1) {
3417c478bd9Sstevel@tonic-gate 			int	err = errno;
3427c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), cname,
3437c478bd9Sstevel@tonic-gate 			    fname, strerror(err));
3447c478bd9Sstevel@tonic-gate 			error = 1;
3457c478bd9Sstevel@tonic-gate 			continue;
3467c478bd9Sstevel@tonic-gate 		}
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		/*
3497c478bd9Sstevel@tonic-gate 		 * Get the files elf descriptor and process it as an elf or
3507c478bd9Sstevel@tonic-gate 		 * a.out (4.x) file.
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		elf = elf_begin(var, ELF_C_READ, (Elf *)0);
3537c478bd9Sstevel@tonic-gate 		switch (elf_kind(elf)) {
3547c478bd9Sstevel@tonic-gate 		case ELF_K_AR :
3557c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO),
3567c478bd9Sstevel@tonic-gate 			    cname, fname);
3577c478bd9Sstevel@tonic-gate 			error = 1;
3587c478bd9Sstevel@tonic-gate 			break;
3597c478bd9Sstevel@tonic-gate 		case ELF_K_COFF:
3607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN),
3617c478bd9Sstevel@tonic-gate 			    cname, fname);
3627c478bd9Sstevel@tonic-gate 			error = 1;
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		case ELF_K_ELF:
3657c478bd9Sstevel@tonic-gate 			if (elf_check(nfile, fname, cname, elf, fflag) != NULL)
3667c478bd9Sstevel@tonic-gate 				error = 1;
3677c478bd9Sstevel@tonic-gate 			break;
3687c478bd9Sstevel@tonic-gate 		default:
3697c478bd9Sstevel@tonic-gate 			/*
3707c478bd9Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
3717c478bd9Sstevel@tonic-gate 			 */
3727c478bd9Sstevel@tonic-gate 			if (aout_check(nfile, fname, cname, var, fflag) != NULL)
3737c478bd9Sstevel@tonic-gate 				error = 1;
3747c478bd9Sstevel@tonic-gate 			break;
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
3777c478bd9Sstevel@tonic-gate 		(void) close(var);
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 	return (error);
3807c478bd9Sstevel@tonic-gate }
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate static int
3857c478bd9Sstevel@tonic-gate is_runnable(GElf_Ehdr *ehdr)
3867c478bd9Sstevel@tonic-gate {
387df14233eSab 	if ((ehdr->e_ident[EI_CLASS] == ELFCLASS32) &&
3887c478bd9Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == M_DATA))
3897c478bd9Sstevel@tonic-gate 		return (ELFCLASS32);
3907c478bd9Sstevel@tonic-gate 
39102ca3e02Srie #if	defined(__sparc)
3927c478bd9Sstevel@tonic-gate 	if ((ehdr->e_machine == EM_SPARCV9) &&
3937c478bd9Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == M_DATA) &&
3947c478bd9Sstevel@tonic-gate 	    (conv_sys_eclass() == ELFCLASS64))
3957c478bd9Sstevel@tonic-gate 		return (ELFCLASS64);
39602ca3e02Srie #elif	defined(__x86)
3977c478bd9Sstevel@tonic-gate 	if ((ehdr->e_machine == EM_AMD64) &&
3987c478bd9Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) &&
3997c478bd9Sstevel@tonic-gate 	    (conv_sys_eclass() == ELFCLASS64))
4007c478bd9Sstevel@tonic-gate 		return (ELFCLASS64);
4017c478bd9Sstevel@tonic-gate #endif
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	return (ELFCLASSNONE);
4047c478bd9Sstevel@tonic-gate }
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate static int
4087c478bd9Sstevel@tonic-gate elf_check(int nfile, char *fname, char *cname, Elf *elf, int fflag)
4097c478bd9Sstevel@tonic-gate {
4107c478bd9Sstevel@tonic-gate 	GElf_Ehdr 	ehdr;
4117c478bd9Sstevel@tonic-gate 	GElf_Phdr 	phdr;
4127c478bd9Sstevel@tonic-gate 	int		dynamic = 0, interp = 0, cnt, class;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	/*
4157c478bd9Sstevel@tonic-gate 	 * verify information in file header
4167c478bd9Sstevel@tonic-gate 	 */
4177c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
4187c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR),
419df4628cbSrie 		    cname, fname, elf_errmsg(-1));
4207c478bd9Sstevel@tonic-gate 		return (1);
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/*
4247c478bd9Sstevel@tonic-gate 	 * check class and encoding
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	if ((class = is_runnable(&ehdr)) == ELFCLASSNONE) {
4277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_CLASSDATA),
428df4628cbSrie 		    cname, fname);
4297c478bd9Sstevel@tonic-gate 		return (1);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * check type
4347c478bd9Sstevel@tonic-gate 	 */
4357c478bd9Sstevel@tonic-gate 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN) &&
4367c478bd9Sstevel@tonic-gate 	    (ehdr.e_type != ET_REL)) {
4377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_BADMAGIC),
438df4628cbSrie 		    cname, fname);
4397c478bd9Sstevel@tonic-gate 		return (1);
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 	if ((class == ELFCLASS32) && (ehdr.e_machine != M_MACH)) {
4427c478bd9Sstevel@tonic-gate 		if (ehdr.e_machine != M_MACHPLUS) {
4437c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHTYPE),
444df4628cbSrie 			    cname, fname);
4457c478bd9Sstevel@tonic-gate 			return (1);
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate 		if ((ehdr.e_flags & M_FLAGSPLUS) == 0) {
4487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHFLAGS),
449df4628cbSrie 			    cname, fname);
4507c478bd9Sstevel@tonic-gate 			return (1);
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * Check that the file is executable.  Dynamic executables must be
4567c478bd9Sstevel@tonic-gate 	 * executable to be exec'ed.  Shared objects need not be executable to
4577c478bd9Sstevel@tonic-gate 	 * be mapped with a dynamic executable, however, by convention they're
4587c478bd9Sstevel@tonic-gate 	 * supposed to be executable.
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	if (access(fname, X_OK) != 0) {
4617c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_EXEC) {
4627c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_1),
463df4628cbSrie 			    cname, fname);
4647c478bd9Sstevel@tonic-gate 			return (1);
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_2), cname,
4677c478bd9Sstevel@tonic-gate 		    fname);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * Determine whether we have a dynamic section or interpretor.
4727c478bd9Sstevel@tonic-gate 	 */
4737c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) {
4747c478bd9Sstevel@tonic-gate 		if (dynamic && interp)
4757c478bd9Sstevel@tonic-gate 			break;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		if (gelf_getphdr(elf, cnt, &phdr) == NULL) {
4787c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETPHDR),
479df4628cbSrie 			    cname, fname, elf_errmsg(-1));
4807c478bd9Sstevel@tonic-gate 			return (1);
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 
4837c478bd9Sstevel@tonic-gate 		if (phdr.p_type == PT_DYNAMIC) {
4847c478bd9Sstevel@tonic-gate 			dynamic = 1;
4857c478bd9Sstevel@tonic-gate 			continue;
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		if (phdr.p_type != PT_INTERP)
4897c478bd9Sstevel@tonic-gate 			continue;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		interp = 1;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		/*
4947c478bd9Sstevel@tonic-gate 		 * If fflag is not set, and euid == root, and the interpreter
4957c478bd9Sstevel@tonic-gate 		 * does not live under /lib, /usr/lib or /etc/lib then don't
4967c478bd9Sstevel@tonic-gate 		 * allow ldd to execute the image.  This prevents someone
4977c478bd9Sstevel@tonic-gate 		 * creating a `trojan horse' by substituting their own
4987c478bd9Sstevel@tonic-gate 		 * interpreter that could preform privileged operations
4997c478bd9Sstevel@tonic-gate 		 * when ldd is against it.
5007c478bd9Sstevel@tonic-gate 		 */
5017c478bd9Sstevel@tonic-gate 		if ((fflag == 0) && (geteuid() == 0) &&
5027c478bd9Sstevel@tonic-gate 		    (strcmp(fname, conv_lddstub(class)) != 0)) {
5037c478bd9Sstevel@tonic-gate 			char	*interpreter;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 			/*
5067c478bd9Sstevel@tonic-gate 			 * Does the interpreter live under a trusted directory.
5077c478bd9Sstevel@tonic-gate 			 */
5087c478bd9Sstevel@tonic-gate 			interpreter = elf_getident(elf, 0) + phdr.p_offset;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 			if ((strncmp(interpreter, MSG_ORIG(MSG_PTH_USRLIB),
5117c478bd9Sstevel@tonic-gate 			    MSG_PTH_USRLIB_SIZE) != 0) &&
5127c478bd9Sstevel@tonic-gate 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_LIB),
5137c478bd9Sstevel@tonic-gate 			    MSG_PTH_LIB_SIZE) != 0) &&
5147c478bd9Sstevel@tonic-gate 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_ETCLIB),
5157c478bd9Sstevel@tonic-gate 			    MSG_PTH_ETCLIB_SIZE) != 0)) {
5167c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_USP_ELFINS),
517df4628cbSrie 				    cname, fname, interpreter);
5187c478bd9Sstevel@tonic-gate 				return (1);
5197c478bd9Sstevel@tonic-gate 			}
5207c478bd9Sstevel@tonic-gate 		}
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * Catch the case of a static executable (ie, an ET_EXEC that has a set
5257c478bd9Sstevel@tonic-gate 	 * of program headers but no PT_DYNAMIC).
5267c478bd9Sstevel@tonic-gate 	 */
5277c478bd9Sstevel@tonic-gate 	if (ehdr.e_phnum && !dynamic) {
5287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname,
5297c478bd9Sstevel@tonic-gate 		    fname);
5307c478bd9Sstevel@tonic-gate 		return (1);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5334899432aSab 	/*
5344899432aSab 	 * If there is a dynamic section, then check for the DF_1_NOHDR
535*69112eddSAli Bahrami 	 * flag, and bail if it is present. Such objects are created using
536*69112eddSAli Bahrami 	 * a mapfile option (?N in the version 1 syntax, or HDR_NOALLOC
537*69112eddSAli Bahrami 	 * otherwise). The ELF header and program headers are
5384899432aSab 	 * not mapped as part of the first segment, and virtual addresses
5394899432aSab 	 * are computed without them. If ldd tries to interpret such
5404899432aSab 	 * a file, it will become confused and generate bad output or
5414899432aSab 	 * crash. Such objects are always special purpose files (like an OS
5424899432aSab 	 * kernel) --- files for which the ldd operation doesn't make sense.
5434899432aSab 	 */
5444899432aSab 	if (dynamic && (_gelf_getdyndtflags_1(elf) & DF_1_NOHDR)) {
5454899432aSab 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOHDR), cname,
5464899432aSab 		    fname);
5474899432aSab 		return (1);
5484899432aSab 	}
5494899432aSab 
5507c478bd9Sstevel@tonic-gate 	load = load_elf;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	/*
5537c478bd9Sstevel@tonic-gate 	 * Run the required program (shared and relocatable objects require the
5547c478bd9Sstevel@tonic-gate 	 * use of lddstub).
5557c478bd9Sstevel@tonic-gate 	 */
5567c478bd9Sstevel@tonic-gate 	if ((ehdr.e_type == ET_EXEC) && interp)
5577c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, (const char *)fname, class));
5587c478bd9Sstevel@tonic-gate 	else
5597c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, conv_lddstub(class), class));
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate static int
5637c478bd9Sstevel@tonic-gate aout_check(int nfile, char *fname, char *cname, int fd, int fflag)
5647c478bd9Sstevel@tonic-gate {
565df14233eSab 	struct exec32	aout;
5667c478bd9Sstevel@tonic-gate 	int		err;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (lseek(fd, 0, SEEK_SET) != 0) {
5697c478bd9Sstevel@tonic-gate 		err = errno;
5707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_LSEEK), cname, fname,
5717c478bd9Sstevel@tonic-gate 		    strerror(err));
5727c478bd9Sstevel@tonic-gate 		return (1);
5737c478bd9Sstevel@tonic-gate 	}
574df14233eSab 	if (read(fd, (char *)&aout, sizeof (aout)) != sizeof (aout)) {
5757c478bd9Sstevel@tonic-gate 		err = errno;
5767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_READ), cname, fname,
5777c478bd9Sstevel@tonic-gate 		    strerror(err));
5787c478bd9Sstevel@tonic-gate 		return (1);
5797c478bd9Sstevel@tonic-gate 	}
5807c478bd9Sstevel@tonic-gate 	if (aout.a_machtype != M_SPARC) {
5817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN), cname, fname);
5827c478bd9Sstevel@tonic-gate 		return (1);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	if (N_BADMAG(aout) || !aout.a_dynamic) {
5857c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname,
5867c478bd9Sstevel@tonic-gate 		    fname);
5877c478bd9Sstevel@tonic-gate 		return (1);
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	if (!fflag && (geteuid() == 0)) {
5907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_AOUTINS), cname, fname);
5917c478bd9Sstevel@tonic-gate 		return (1);
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * Run the required program.
5967c478bd9Sstevel@tonic-gate 	 */
597df14233eSab 	if ((aout.a_magic == ZMAGIC) && (aout.a_entry <= sizeof (aout))) {
5987c478bd9Sstevel@tonic-gate 		load = load_elf;
5997c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, conv_lddstub(ELFCLASS32),
6007c478bd9Sstevel@tonic-gate 		    ELFCLASS32));
6017c478bd9Sstevel@tonic-gate 	} else {
6027c478bd9Sstevel@tonic-gate 		load = load_aout;
6037c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, (const char *)fname,
6047c478bd9Sstevel@tonic-gate 		    ELFCLASS32));
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate /*
6107c478bd9Sstevel@tonic-gate  * Run the required program, setting the preload and trace environment
6117c478bd9Sstevel@tonic-gate  * variables accordingly.
6127c478bd9Sstevel@tonic-gate  */
6137c478bd9Sstevel@tonic-gate static int
6147c478bd9Sstevel@tonic-gate run(int nfile, char *cname, char *fname, const char *ename, int class)
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	const char	*preload = 0;
6177c478bd9Sstevel@tonic-gate 	int		pid, status;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
6207c478bd9Sstevel@tonic-gate 		int	err = errno;
6217c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK), cname,
6227c478bd9Sstevel@tonic-gate 		    strerror(err));
6237c478bd9Sstevel@tonic-gate 		return (1);
6247c478bd9Sstevel@tonic-gate 	}
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (pid) {				/* parent */
6277c478bd9Sstevel@tonic-gate 		while (wait(&status) != pid)
6287c478bd9Sstevel@tonic-gate 			;
6297c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(status) && ((WSIGMASK & status) != SIGPIPE)) {
6307c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
6317c478bd9Sstevel@tonic-gate 			    fname);
6327c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_SIG),
6337c478bd9Sstevel@tonic-gate 			    (WSIGMASK & status), ((status & WCOREFLG) ?
6347c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_SYS_EXEC_CORE) :
6357c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_EMPTY)));
6367c478bd9Sstevel@tonic-gate 			status = 1;
6377c478bd9Sstevel@tonic-gate 		} else if (WHIBYTE(status)) {
6387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
6397c478bd9Sstevel@tonic-gate 			    fname);
6407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_STAT),
6417c478bd9Sstevel@tonic-gate 			    WHIBYTE(status));
6427c478bd9Sstevel@tonic-gate 			status = 1;
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 	} else {				/* child */
64557ef7aa9SRod Evans 		Aliste	idx;
64657ef7aa9SRod Evans 		char	*str;
64757ef7aa9SRod Evans 		size_t	size;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		/*
6507c478bd9Sstevel@tonic-gate 		 * When using ldd(1) to analyze a shared object we preload the
6517c478bd9Sstevel@tonic-gate 		 * shared object with lddstub.  Any additional preload
6527c478bd9Sstevel@tonic-gate 		 * requirements are added after the object being analyzed, this
6537c478bd9Sstevel@tonic-gate 		 * allows us to skip the first object but produce diagnostics
6547c478bd9Sstevel@tonic-gate 		 * for each other preloaded object.
6557c478bd9Sstevel@tonic-gate 		 */
6567c478bd9Sstevel@tonic-gate 		if (fname != ename) {
6577c478bd9Sstevel@tonic-gate 			char		*str;
6587c478bd9Sstevel@tonic-gate 			const char	*files = prefile;
6597c478bd9Sstevel@tonic-gate 			const char	*format = MSG_ORIG(MSG_STR_FMT1);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 			for (str = fname; *str; str++)
6627c478bd9Sstevel@tonic-gate 				if (*str == '/') {
6637c478bd9Sstevel@tonic-gate 					format = MSG_ORIG(MSG_STR_FMT2);
6647c478bd9Sstevel@tonic-gate 					break;
6657c478bd9Sstevel@tonic-gate 			}
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 			preload = MSG_ORIG(MSG_LD_PRELOAD);
6687c478bd9Sstevel@tonic-gate 
6697c478bd9Sstevel@tonic-gate 			/*
6707c478bd9Sstevel@tonic-gate 			 * Determine which preload files and preload environment
6717c478bd9Sstevel@tonic-gate 			 * variable to use.
6727c478bd9Sstevel@tonic-gate 			 */
6737c478bd9Sstevel@tonic-gate 			if (class == ELFCLASS64) {
6747c478bd9Sstevel@tonic-gate 				if (prefile_64 != MSG_ORIG(MSG_STR_EMPTY)) {
6757c478bd9Sstevel@tonic-gate 					files = prefile_64;
6767c478bd9Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_64);
6777c478bd9Sstevel@tonic-gate 				}
6787c478bd9Sstevel@tonic-gate 			} else {
6797c478bd9Sstevel@tonic-gate 				if (prefile_32 != MSG_ORIG(MSG_STR_EMPTY)) {
6807c478bd9Sstevel@tonic-gate 					files = prefile_32;
6817c478bd9Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_32);
6827c478bd9Sstevel@tonic-gate 				}
6837c478bd9Sstevel@tonic-gate 			}
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			if ((str = (char *)malloc(strlen(preload) +
6867c478bd9Sstevel@tonic-gate 			    strlen(fname) + strlen(files) + 5)) == 0) {
6877c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
6887c478bd9Sstevel@tonic-gate 				    cname);
6897c478bd9Sstevel@tonic-gate 				exit(1);
6907c478bd9Sstevel@tonic-gate 			}
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 			(void) sprintf(str, format, preload, fname, files);
6937c478bd9Sstevel@tonic-gate 			if (putenv(str) != 0) {
6947c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
6957c478bd9Sstevel@tonic-gate 				    cname);
6967c478bd9Sstevel@tonic-gate 				exit(1);
6977c478bd9Sstevel@tonic-gate 			}
698df4628cbSrie 
699df4628cbSrie 			/*
700df4628cbSrie 			 * The pointer "load" has be assigned to load_elf[] or
701df4628cbSrie 			 * load_aout[].  Use the size of load_elf[] as the size
702df4628cbSrie 			 * of load_aout[] is the same.
703df4628cbSrie 			 */
704df4628cbSrie 			load[sizeof (load_elf) - 2] = '2';
7057c478bd9Sstevel@tonic-gate 		} else
706df4628cbSrie 			load[sizeof (load_elf) - 2] = '1';
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		/*
7107c478bd9Sstevel@tonic-gate 		 * Establish new environment variables to affect the child
7117c478bd9Sstevel@tonic-gate 		 * process.
7127c478bd9Sstevel@tonic-gate 		 */
7137c478bd9Sstevel@tonic-gate 		if ((putenv(warn) != 0) || (putenv(bind) != 0) ||
7147c478bd9Sstevel@tonic-gate 		    (putenv(path) != 0) || (putenv(verb) != 0) ||
7157c478bd9Sstevel@tonic-gate 		    (putenv(fltr) != 0) || (putenv(conf) != 0) ||
7167c478bd9Sstevel@tonic-gate 		    (putenv(init) != 0) || (putenv(lazy) != 0) ||
7177c478bd9Sstevel@tonic-gate 		    (putenv(uref) != 0) || (putenv(used) != 0) ||
718dae2dfb7Srie 		    (putenv(weak) != 0) || (putenv(load) != 0) ||
719dae2dfb7Srie 		    (putenv(nope) != 0)) {
7207c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), cname);
7217c478bd9Sstevel@tonic-gate 			exit(1);
7227c478bd9Sstevel@tonic-gate 		}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 		/*
7257c478bd9Sstevel@tonic-gate 		 * Establish explicit environment requires (but don't override
7267c478bd9Sstevel@tonic-gate 		 * any preload request established to process a shared object).
7277c478bd9Sstevel@tonic-gate 		 */
7287c478bd9Sstevel@tonic-gate 		size = 0;
72957ef7aa9SRod Evans 		for (APLIST_TRAVERSE(eopts, idx, str)) {
7307c478bd9Sstevel@tonic-gate 			if (preload) {
7317c478bd9Sstevel@tonic-gate 				if (size == 0)
7327c478bd9Sstevel@tonic-gate 					size = strlen(preload);
7337c478bd9Sstevel@tonic-gate 				if ((strncmp(preload, str, size) == 0) &&
7347c478bd9Sstevel@tonic-gate 				    (str[size] == '=')) {
7357c478bd9Sstevel@tonic-gate 					continue;
7367c478bd9Sstevel@tonic-gate 				}
7377c478bd9Sstevel@tonic-gate 			}
7387c478bd9Sstevel@tonic-gate 			if (putenv(str) != 0) {
7397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
7407c478bd9Sstevel@tonic-gate 				    cname);
7417c478bd9Sstevel@tonic-gate 				exit(1);
7427c478bd9Sstevel@tonic-gate 			}
7437c478bd9Sstevel@tonic-gate 		}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 		/*
7467c478bd9Sstevel@tonic-gate 		 * Execute the object and let ld.so.1 do the rest.
7477c478bd9Sstevel@tonic-gate 		 */
7487c478bd9Sstevel@tonic-gate 		if (nfile > 1)
7497c478bd9Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_STR_FMT3), fname);
7507c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
7517c478bd9Sstevel@tonic-gate 		if ((execl(ename, ename, (char *)0)) == -1) {
7527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
7537c478bd9Sstevel@tonic-gate 			    fname);
7547c478bd9Sstevel@tonic-gate 			perror(ename);
7557c478bd9Sstevel@tonic-gate 			_exit(0);
7567c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
7577c478bd9Sstevel@tonic-gate 		}
7587c478bd9Sstevel@tonic-gate 	}
7597c478bd9Sstevel@tonic-gate 	return (status);
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate const char *
7637c478bd9Sstevel@tonic-gate _ldd_msg(Msg mid)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
7667c478bd9Sstevel@tonic-gate }
767