xref: /illumos-gate/usr/src/cmd/sgs/ldd/common/ldd.c (revision dae2dfb7)
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
20*dae2dfb7Srie  *
217c478bd9Sstevel@tonic-gate  *	Copyright (c) 1988 AT&T
227c478bd9Sstevel@tonic-gate  *	  All Rights Reserved
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  *
25*dae2dfb7Srie  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2602ca3e02Srie  * Use is subject to license terms.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Print the list of shared objects required by a dynamic executable or shared
327c478bd9Sstevel@tonic-gate  * object.
337c478bd9Sstevel@tonic-gate  *
34*dae2dfb7Srie  * usage is: ldd [-d | -r] [-c] [-e envar] [-i] [-f] [-L] [-l] [-p] [-s]
35df4628cbSrie  *		[-U | -u] [-v] [-w] file(s)
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * ldd opens the file and verifies the information in the elf header.
387c478bd9Sstevel@tonic-gate  * If the file is a dynamic executable, we set up some environment variables
397c478bd9Sstevel@tonic-gate  * and exec(2) the file.  If the file is a shared object, we preload the
407c478bd9Sstevel@tonic-gate  * file with a dynamic executable stub. The runtime linker (ld.so.1) actually
417c478bd9Sstevel@tonic-gate  * provides the diagnostic output, according to the environment variables set.
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_[AE].
447c478bd9Sstevel@tonic-gate  * The runtime linker will print the pathnames of all dynamic objects it
457c478bd9Sstevel@tonic-gate  * loads, and then exit.  Note that we distiguish between ELF and AOUT objects
467c478bd9Sstevel@tonic-gate  * when setting this environment variable - AOUT executables cause the mapping
477c478bd9Sstevel@tonic-gate  * of sbcp, the dependencies of which the user isn't interested in.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * If -d or -r is specified, we also set LD_WARN=1; the runtime linker will
507c478bd9Sstevel@tonic-gate  * perform its normal relocations and issue warning messages for unresolved
517c478bd9Sstevel@tonic-gate  * references. It will then exit.
527c478bd9Sstevel@tonic-gate  * If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker
537c478bd9Sstevel@tonic-gate  * will perform all relocations, otherwise (under -d) the runtime linker
547c478bd9Sstevel@tonic-gate  * will not perform PLT (function) type relocations.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * If -c is specified we also set LD_NOCONFIG=1, thus disabling any
577c478bd9Sstevel@tonic-gate  * configuration file use.
587c478bd9Sstevel@tonic-gate  *
597c478bd9Sstevel@tonic-gate  * If -e is specified the associated environment variable is set for the
607c478bd9Sstevel@tonic-gate  * child process that will produce ldd's diagnostics.
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * If -i is specified, we set LD_INIT=1. The order of inititialization
637c478bd9Sstevel@tonic-gate  * sections to be executed is printed. We also set LD_WARN=1.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * If -f is specified, we will run ldd as root on executables that have
667c478bd9Sstevel@tonic-gate  * an unsercure runtime linker that does not live under the "/usr/lib"
677c478bd9Sstevel@tonic-gate  * directory.  By default we will not let this happen.
687c478bd9Sstevel@tonic-gate  *
697c478bd9Sstevel@tonic-gate  * If -l is specified it generates a warning for any auxiliary filter not found.
707c478bd9Sstevel@tonic-gate  * Prior to 2.8 this forced any filters to load (all) their filtees.  This is
717c478bd9Sstevel@tonic-gate  * now the default, however missing auxiliary filters don't generate any error
727c478bd9Sstevel@tonic-gate  * diagniostic.  See also -L.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * If -L is specified we revert to lazy loading, thus any filtee or lazy
757c478bd9Sstevel@tonic-gate  * dependency loading is deferred until relocations cause loading.  Without
767c478bd9Sstevel@tonic-gate  * this option we set LD_LOADFLTR=1, thus forcing any filters to load (all)
777c478bd9Sstevel@tonic-gate  * their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of
787c478bd9Sstevel@tonic-gate  * any lazy loaded dependencies.
797c478bd9Sstevel@tonic-gate  *
807c478bd9Sstevel@tonic-gate  * If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling
817c478bd9Sstevel@tonic-gate  * the runtime linker to indicate the search algorithm used.
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime
847c478bd9Sstevel@tonic-gate  * linker to indicate all object dependencies (not just the first object
857c478bd9Sstevel@tonic-gate  * loaded) together with any versionig requirements.
867c478bd9Sstevel@tonic-gate  *
877c478bd9Sstevel@tonic-gate  * If -U or -u is specified unused dependencies are detected.  -u causes
887c478bd9Sstevel@tonic-gate  * LD_UNUSED=1 to be set, which causes dependencies that are unused within the
897c478bd9Sstevel@tonic-gate  * process to be detected.  -U causes LD_UNREF=1 to be set, which causes
907c478bd9Sstevel@tonic-gate  * unreferenced objects, and unreferenced cyclic dependencies to be detected.
917c478bd9Sstevel@tonic-gate  * These options assert that at least -d is set as relocation references are
927c478bd9Sstevel@tonic-gate  * what determine an objects use.
93df4628cbSrie  *
94df4628cbSrie  * If -w is specified, no unresolved weak references are allowed.  -w causes
95df4628cbSrie  * LD_NOUNRESWEAK=1 to be set.  By default, an unresolved weak reference is
96df4628cbSrie  * allowed, and a "0" is written to the relocation offset.  The -w option
97df4628cbSrie  * disables this default.  Any weak references that can not be resolved result
98*dae2dfb7Srie  * in relocation error messages.  This option has no use without -r or -d.
99*dae2dfb7Srie  *
100*dae2dfb7Srie  * If the -p option is specified, no unresolved PARENT or EXTERN references are
101*dae2dfb7Srie  * allowed.  -p causes LD_NOPAREXT=1 to be set.  By default, PARENT and EXTERN
102*dae2dfb7Srie  * references, which have been explicitly assigned via a mapfile when a shared
103*dae2dfb7Srie  * object was built, imply that a caller will provide the symbols, and hence
104*dae2dfb7Srie  * these are not reported as relocation errors.  Note, the -p option is asserted
105*dae2dfb7Srie  * by default when either the -r or -d options are used to inspect a dynamic
106*dae2dfb7Srie  * executable.  This option has no use with a shared object without -r or -d.
1077c478bd9Sstevel@tonic-gate  */
1087c478bd9Sstevel@tonic-gate #include	<fcntl.h>
1097c478bd9Sstevel@tonic-gate #include	<stdio.h>
1107c478bd9Sstevel@tonic-gate #include	<string.h>
1114899432aSab #include	<_libelf.h>
1127c478bd9Sstevel@tonic-gate #include	<stdlib.h>
1137c478bd9Sstevel@tonic-gate #include	<unistd.h>
1147c478bd9Sstevel@tonic-gate #include	<wait.h>
1157c478bd9Sstevel@tonic-gate #include	<locale.h>
1167c478bd9Sstevel@tonic-gate #include	<errno.h>
1177c478bd9Sstevel@tonic-gate #include	<signal.h>
1187c478bd9Sstevel@tonic-gate #include	"machdep.h"
1197c478bd9Sstevel@tonic-gate #include	"sgs.h"
1207c478bd9Sstevel@tonic-gate #include	"conv.h"
1217c478bd9Sstevel@tonic-gate #include	"a.out.h"
1227c478bd9Sstevel@tonic-gate #include	"msg.h"
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int	elf_check(int, char *, char *, Elf *, int);
1257c478bd9Sstevel@tonic-gate static int	aout_check(int, char *, char *, int, int);
1267c478bd9Sstevel@tonic-gate static int	run(int, char *, char *, const char *, int);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
130df4628cbSrie  * Define all environment variable strings.  The character following the "="
131df4628cbSrie  * will be written to, to disable or enable the associated feature.
1327c478bd9Sstevel@tonic-gate  */
1337c478bd9Sstevel@tonic-gate static char	bind[] =	"LD_BIND_NOW= ",
1347c478bd9Sstevel@tonic-gate 		load_elf[] =	"LD_TRACE_LOADED_OBJECTS_E= ",
1357c478bd9Sstevel@tonic-gate 		load_aout[] =	"LD_TRACE_LOADED_OBJECTS_A= ",
1367c478bd9Sstevel@tonic-gate 		path[] =	"LD_TRACE_SEARCH_PATHS= ",
1377c478bd9Sstevel@tonic-gate 		verb[] =	"LD_VERBOSE= ",
1387c478bd9Sstevel@tonic-gate 		warn[] =	"LD_WARN= ",
1397c478bd9Sstevel@tonic-gate 		conf[] =	"LD_NOCONFIG= ",
1407c478bd9Sstevel@tonic-gate 		fltr[] =	"LD_LOADFLTR= ",
1417c478bd9Sstevel@tonic-gate 		lazy[] =	"LD_NOLAZYLOAD=1",
1427c478bd9Sstevel@tonic-gate 		init[] =	"LD_INIT= ",
1437c478bd9Sstevel@tonic-gate 		uref[] =	"LD_UNREF= ",
144df4628cbSrie 		used[] =	"LD_UNUSED= ",
145*dae2dfb7Srie 		weak[] =	"LD_NOUNRESWEAK= ",
146*dae2dfb7Srie 		nope[] =	"LD_NOPAREXT= ";
1477c478bd9Sstevel@tonic-gate static char	*load;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static const char	*prefile_32, *prefile_64, *prefile;
1507c478bd9Sstevel@tonic-gate static List		eopts = { 0, 0 };
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Append an item to the specified list, and return a pointer to the list
1547c478bd9Sstevel@tonic-gate  * node created.
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate Listnode *
1577c478bd9Sstevel@tonic-gate list_append(List *lst, const void *item)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	Listnode	*lnp;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if ((lnp = malloc(sizeof (Listnode))) == (Listnode *)0)
1627c478bd9Sstevel@tonic-gate 		return (0);
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	lnp->data = (void *)item;
1657c478bd9Sstevel@tonic-gate 	lnp->next = NULL;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	if (lst->head == NULL)
1687c478bd9Sstevel@tonic-gate 		lst->tail = lst->head = lnp;
1697c478bd9Sstevel@tonic-gate 	else {
1707c478bd9Sstevel@tonic-gate 		lst->tail->next = lnp;
1717c478bd9Sstevel@tonic-gate 		lst->tail = lst->tail->next;
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	return (lnp);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate int
1777c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1787c478bd9Sstevel@tonic-gate {
1797c478bd9Sstevel@tonic-gate 	char	*str, *cname = argv[0];
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	Elf	*elf;
1827c478bd9Sstevel@tonic-gate 	int	cflag = 0, dflag = 0, fflag = 0, iflag = 0, Lflag = 0;
1837c478bd9Sstevel@tonic-gate 	int	lflag = 0, rflag = 0, sflag = 0, Uflag = 0, uflag = 0;
184*dae2dfb7Srie 	int	pflag = 0, vflag = 0, wflag = 0, nfile, var, error = 0;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	Listnode	*lnp;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/*
1897c478bd9Sstevel@tonic-gate 	 * Establish locale.
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
1927c478bd9Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * verify command line syntax and process arguments
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	opterr = 0;				/* disable getopt error mesg */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_GETOPT))) != EOF) {
2007c478bd9Sstevel@tonic-gate 		switch (var) {
2017c478bd9Sstevel@tonic-gate 		case 'c' :			/* enable config search */
2027c478bd9Sstevel@tonic-gate 			cflag = 1;
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 		case 'd' :			/* perform data relocations */
2057c478bd9Sstevel@tonic-gate 			dflag = 1;
2067c478bd9Sstevel@tonic-gate 			if (rflag)
2077c478bd9Sstevel@tonic-gate 				error++;
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 		case 'e' :
2107c478bd9Sstevel@tonic-gate 			if (list_append(&eopts, optarg) == 0) {
2117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
2127c478bd9Sstevel@tonic-gate 				    cname);
2137c478bd9Sstevel@tonic-gate 				exit(1);
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 		case 'f' :
2177c478bd9Sstevel@tonic-gate 			fflag = 1;
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		case 'L' :
2207c478bd9Sstevel@tonic-gate 			Lflag = 1;
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		case 'l' :
2237c478bd9Sstevel@tonic-gate 			lflag = 1;
2247c478bd9Sstevel@tonic-gate 			break;
2257c478bd9Sstevel@tonic-gate 		case 'i' :			/* print the order of .init */
2267c478bd9Sstevel@tonic-gate 			iflag = 1;
2277c478bd9Sstevel@tonic-gate 			break;
228*dae2dfb7Srie 		case 'p' :
229*dae2dfb7Srie 			pflag = 1;		/* expose unreferenced */
230*dae2dfb7Srie 			break;			/*	parent or externals */
2317c478bd9Sstevel@tonic-gate 		case 'r' :			/* perform all relocations */
2327c478bd9Sstevel@tonic-gate 			rflag = 1;
2337c478bd9Sstevel@tonic-gate 			if (dflag)
2347c478bd9Sstevel@tonic-gate 				error++;
2357c478bd9Sstevel@tonic-gate 			break;
2367c478bd9Sstevel@tonic-gate 		case 's' :			/* enable search path output */
2377c478bd9Sstevel@tonic-gate 			sflag = 1;
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 		case 'U' :			/* list unreferenced */
2407c478bd9Sstevel@tonic-gate 			Uflag = 1;		/*	dependencies */
2417c478bd9Sstevel@tonic-gate 			if (uflag)
2427c478bd9Sstevel@tonic-gate 				error++;
2437c478bd9Sstevel@tonic-gate 			break;
2447c478bd9Sstevel@tonic-gate 		case 'u' :			/* list unused dependencies */
2457c478bd9Sstevel@tonic-gate 			uflag = 1;
2467c478bd9Sstevel@tonic-gate 			if (Uflag)
2477c478bd9Sstevel@tonic-gate 				error++;
2487c478bd9Sstevel@tonic-gate 			break;
2497c478bd9Sstevel@tonic-gate 		case 'v' :			/* enable verbose output */
2507c478bd9Sstevel@tonic-gate 			vflag = 1;
2517c478bd9Sstevel@tonic-gate 			break;
252*dae2dfb7Srie 		case 'w' :			/* expose unresolved weak */
253df4628cbSrie 			wflag = 1;		/*	references */
254df4628cbSrie 			break;
2557c478bd9Sstevel@tonic-gate 		default :
2567c478bd9Sstevel@tonic-gate 			error++;
2577c478bd9Sstevel@tonic-gate 			break;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 		if (error)
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	if (error) {
2637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), cname);
2647c478bd9Sstevel@tonic-gate 		exit(1);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/*
2687c478bd9Sstevel@tonic-gate 	 * Determine if any of the LD_PRELOAD family is already set in the
2697c478bd9Sstevel@tonic-gate 	 * environment, if so we'll continue to analyze each object with the
2707c478bd9Sstevel@tonic-gate 	 * appropriate setting.
2717c478bd9Sstevel@tonic-gate 	 */
2727c478bd9Sstevel@tonic-gate 	if (((prefile_32 = getenv(MSG_ORIG(MSG_LD_PRELOAD_32))) == NULL) ||
2737c478bd9Sstevel@tonic-gate 	    (*prefile_32 == '\0')) {
2747c478bd9Sstevel@tonic-gate 		prefile_32 = MSG_ORIG(MSG_STR_EMPTY);
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate 	if (((prefile_64 = getenv(MSG_ORIG(MSG_LD_PRELOAD_64))) == NULL) ||
2777c478bd9Sstevel@tonic-gate 	    (*prefile_64 == '\0')) {
2787c478bd9Sstevel@tonic-gate 		prefile_64 = MSG_ORIG(MSG_STR_EMPTY);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	if (((prefile = getenv(MSG_ORIG(MSG_LD_PRELOAD))) == NULL) ||
2817c478bd9Sstevel@tonic-gate 	    (*prefile == '\0')) {
2827c478bd9Sstevel@tonic-gate 		prefile = MSG_ORIG(MSG_STR_EMPTY);
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Determine if any environment requests are for the LD_PRELOAD family,
2877c478bd9Sstevel@tonic-gate 	 * and if so override any environment settings we've established above.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	for (LIST_TRAVERSE(&eopts, lnp, str)) {
2907c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_32),
2917c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_32_SIZE)) == 0) {
2927c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_32_SIZE;
2937c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
2947c478bd9Sstevel@tonic-gate 				prefile_32 = str;
2957c478bd9Sstevel@tonic-gate 			continue;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_64),
2987c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_64_SIZE)) == 0) {
2997c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_64_SIZE;
3007c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
3017c478bd9Sstevel@tonic-gate 				prefile_64 = str;
3027c478bd9Sstevel@tonic-gate 			continue;
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD),
3057c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_SIZE)) == 0) {
3067c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_SIZE;
3077c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
3087c478bd9Sstevel@tonic-gate 				prefile = str;
3097c478bd9Sstevel@tonic-gate 			continue;
3107c478bd9Sstevel@tonic-gate 		}
3117c478bd9Sstevel@tonic-gate 	}
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * Set the appropriate relocation environment variables (Note unsetting
3157c478bd9Sstevel@tonic-gate 	 * the environment variables is done just in case the user already
3167c478bd9Sstevel@tonic-gate 	 * has these in their environment ... sort of thing the test folks
3177c478bd9Sstevel@tonic-gate 	 * would do :-)
3187c478bd9Sstevel@tonic-gate 	 */
319df4628cbSrie 	warn[sizeof (warn) - 2] = (dflag || rflag || Uflag || uflag) ? '1' :
3207c478bd9Sstevel@tonic-gate 	    '\0';
321df4628cbSrie 	bind[sizeof (bind) - 2] = (rflag) ? '1' : '\0';
322df4628cbSrie 	path[sizeof (path) - 2] = (sflag) ? '1' : '\0';
323df4628cbSrie 	verb[sizeof (verb) - 2] = (vflag) ? '1' : '\0';
324df4628cbSrie 	fltr[sizeof (fltr) - 2] = (Lflag) ? '\0' : (lflag) ? '2' : '1';
325df4628cbSrie 	init[sizeof (init) - 2] = (iflag) ? '1' : '\0';
326df4628cbSrie 	conf[sizeof (conf) - 2] = (cflag) ? '1' : '\0';
327df4628cbSrie 	lazy[sizeof (lazy) - 2] = (Lflag) ? '\0' : '1';
328df4628cbSrie 	uref[sizeof (uref) - 2] = (Uflag) ? '1' : '\0';
329df4628cbSrie 	used[sizeof (used) - 2] = (uflag) ? '1' : '\0';
330df4628cbSrie 	weak[sizeof (weak) - 2] = (wflag) ? '1' : '\0';
331*dae2dfb7Srie 	nope[sizeof (nope) - 2] = (pflag) ? '1' : '\0';
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * coordinate libelf's version information
3357c478bd9Sstevel@tonic-gate 	 */
3367c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
3377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_LIBELF), cname,
3387c478bd9Sstevel@tonic-gate 		    EV_CURRENT);
3397c478bd9Sstevel@tonic-gate 		exit(1);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * Loop through remaining arguments.  Note that from here on there
3447c478bd9Sstevel@tonic-gate 	 * are no exit conditions so that we can process a list of files,
3457c478bd9Sstevel@tonic-gate 	 * any error condition is retained for a final exit status.
3467c478bd9Sstevel@tonic-gate 	 */
3477c478bd9Sstevel@tonic-gate 	nfile = argc - optind;
3487c478bd9Sstevel@tonic-gate 	for (; optind < argc; optind++) {
3497c478bd9Sstevel@tonic-gate 		char	*fname = argv[optind];
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		/*
3527c478bd9Sstevel@tonic-gate 		 * Open file (do this before checking access so that we can
3537c478bd9Sstevel@tonic-gate 		 * provide the user with better diagnostics).
3547c478bd9Sstevel@tonic-gate 		 */
3557c478bd9Sstevel@tonic-gate 		if ((var = open(fname, O_RDONLY)) == -1) {
3567c478bd9Sstevel@tonic-gate 			int	err = errno;
3577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), cname,
3587c478bd9Sstevel@tonic-gate 			    fname, strerror(err));
3597c478bd9Sstevel@tonic-gate 			error = 1;
3607c478bd9Sstevel@tonic-gate 			continue;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 		/*
3647c478bd9Sstevel@tonic-gate 		 * Get the files elf descriptor and process it as an elf or
3657c478bd9Sstevel@tonic-gate 		 * a.out (4.x) file.
3667c478bd9Sstevel@tonic-gate 		 */
3677c478bd9Sstevel@tonic-gate 		elf = elf_begin(var, ELF_C_READ, (Elf *)0);
3687c478bd9Sstevel@tonic-gate 		switch (elf_kind(elf)) {
3697c478bd9Sstevel@tonic-gate 		case ELF_K_AR :
3707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO),
3717c478bd9Sstevel@tonic-gate 			    cname, fname);
3727c478bd9Sstevel@tonic-gate 			error = 1;
3737c478bd9Sstevel@tonic-gate 			break;
3747c478bd9Sstevel@tonic-gate 		case ELF_K_COFF:
3757c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN),
3767c478bd9Sstevel@tonic-gate 			    cname, fname);
3777c478bd9Sstevel@tonic-gate 			error = 1;
3787c478bd9Sstevel@tonic-gate 			break;
3797c478bd9Sstevel@tonic-gate 		case ELF_K_ELF:
3807c478bd9Sstevel@tonic-gate 			if (elf_check(nfile, fname, cname, elf, fflag) != NULL)
3817c478bd9Sstevel@tonic-gate 				error = 1;
3827c478bd9Sstevel@tonic-gate 			break;
3837c478bd9Sstevel@tonic-gate 		default:
3847c478bd9Sstevel@tonic-gate 			/*
3857c478bd9Sstevel@tonic-gate 			 * This is either an unknown file or an aout format
3867c478bd9Sstevel@tonic-gate 			 */
3877c478bd9Sstevel@tonic-gate 			if (aout_check(nfile, fname, cname, var, fflag) != NULL)
3887c478bd9Sstevel@tonic-gate 				error = 1;
3897c478bd9Sstevel@tonic-gate 			break;
3907c478bd9Sstevel@tonic-gate 		}
3917c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
3927c478bd9Sstevel@tonic-gate 		(void) close(var);
3937c478bd9Sstevel@tonic-gate 	}
3947c478bd9Sstevel@tonic-gate 	return (error);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate static int
4007c478bd9Sstevel@tonic-gate is_runnable(GElf_Ehdr *ehdr)
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate 	if ((ehdr->e_ident[EI_CLASS] == M_CLASS) &&
4037c478bd9Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == M_DATA))
4047c478bd9Sstevel@tonic-gate 		return (ELFCLASS32);
4057c478bd9Sstevel@tonic-gate 
40602ca3e02Srie #if	defined(__sparc)
4077c478bd9Sstevel@tonic-gate 	if ((ehdr->e_machine == EM_SPARCV9) &&
4087c478bd9Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == M_DATA) &&
4097c478bd9Sstevel@tonic-gate 	    (conv_sys_eclass() == ELFCLASS64))
4107c478bd9Sstevel@tonic-gate 		return (ELFCLASS64);
41102ca3e02Srie #elif	defined(__x86)
4127c478bd9Sstevel@tonic-gate 	if ((ehdr->e_machine == EM_AMD64) &&
4137c478bd9Sstevel@tonic-gate 	    (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) &&
4147c478bd9Sstevel@tonic-gate 	    (conv_sys_eclass() == ELFCLASS64))
4157c478bd9Sstevel@tonic-gate 		return (ELFCLASS64);
4167c478bd9Sstevel@tonic-gate #endif
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	return (ELFCLASSNONE);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static int
4237c478bd9Sstevel@tonic-gate elf_check(int nfile, char *fname, char *cname, Elf *elf, int fflag)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	GElf_Ehdr 	ehdr;
4267c478bd9Sstevel@tonic-gate 	GElf_Phdr 	phdr;
4277c478bd9Sstevel@tonic-gate 	int		dynamic = 0, interp = 0, cnt, class;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	/*
4307c478bd9Sstevel@tonic-gate 	 * verify information in file header
4317c478bd9Sstevel@tonic-gate 	 */
4327c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
4337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR),
434df4628cbSrie 		    cname, fname, elf_errmsg(-1));
4357c478bd9Sstevel@tonic-gate 		return (1);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	/*
4397c478bd9Sstevel@tonic-gate 	 * check class and encoding
4407c478bd9Sstevel@tonic-gate 	 */
4417c478bd9Sstevel@tonic-gate 	if ((class = is_runnable(&ehdr)) == ELFCLASSNONE) {
4427c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_CLASSDATA),
443df4628cbSrie 		    cname, fname);
4447c478bd9Sstevel@tonic-gate 		return (1);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * check type
4497c478bd9Sstevel@tonic-gate 	 */
4507c478bd9Sstevel@tonic-gate 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN) &&
4517c478bd9Sstevel@tonic-gate 	    (ehdr.e_type != ET_REL)) {
4527c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_BADMAGIC),
453df4628cbSrie 		    cname, fname);
4547c478bd9Sstevel@tonic-gate 		return (1);
4557c478bd9Sstevel@tonic-gate 	}
4567c478bd9Sstevel@tonic-gate 	if ((class == ELFCLASS32) && (ehdr.e_machine != M_MACH)) {
4577c478bd9Sstevel@tonic-gate 		if (ehdr.e_machine != M_MACHPLUS) {
4587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHTYPE),
459df4628cbSrie 			    cname, fname);
4607c478bd9Sstevel@tonic-gate 			return (1);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 		if ((ehdr.e_flags & M_FLAGSPLUS) == 0) {
4637c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHFLAGS),
464df4628cbSrie 			    cname, fname);
4657c478bd9Sstevel@tonic-gate 			return (1);
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 	}
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	/*
4707c478bd9Sstevel@tonic-gate 	 * Check that the file is executable.  Dynamic executables must be
4717c478bd9Sstevel@tonic-gate 	 * executable to be exec'ed.  Shared objects need not be executable to
4727c478bd9Sstevel@tonic-gate 	 * be mapped with a dynamic executable, however, by convention they're
4737c478bd9Sstevel@tonic-gate 	 * supposed to be executable.
4747c478bd9Sstevel@tonic-gate 	 */
4757c478bd9Sstevel@tonic-gate 	if (access(fname, X_OK) != 0) {
4767c478bd9Sstevel@tonic-gate 		if (ehdr.e_type == ET_EXEC) {
4777c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_1),
478df4628cbSrie 			    cname, fname);
4797c478bd9Sstevel@tonic-gate 			return (1);
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC_2), cname,
4827c478bd9Sstevel@tonic-gate 		    fname);
4837c478bd9Sstevel@tonic-gate 	}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	/*
4867c478bd9Sstevel@tonic-gate 	 * Determine whether we have a dynamic section or interpretor.
4877c478bd9Sstevel@tonic-gate 	 */
4887c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) {
4897c478bd9Sstevel@tonic-gate 		if (dynamic && interp)
4907c478bd9Sstevel@tonic-gate 			break;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if (gelf_getphdr(elf, cnt, &phdr) == NULL) {
4937c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETPHDR),
494df4628cbSrie 			    cname, fname, elf_errmsg(-1));
4957c478bd9Sstevel@tonic-gate 			return (1);
4967c478bd9Sstevel@tonic-gate 		}
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		if (phdr.p_type == PT_DYNAMIC) {
4997c478bd9Sstevel@tonic-gate 			dynamic = 1;
5007c478bd9Sstevel@tonic-gate 			continue;
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate 		if (phdr.p_type != PT_INTERP)
5047c478bd9Sstevel@tonic-gate 			continue;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		interp = 1;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		/*
5097c478bd9Sstevel@tonic-gate 		 * If fflag is not set, and euid == root, and the interpreter
5107c478bd9Sstevel@tonic-gate 		 * does not live under /lib, /usr/lib or /etc/lib then don't
5117c478bd9Sstevel@tonic-gate 		 * allow ldd to execute the image.  This prevents someone
5127c478bd9Sstevel@tonic-gate 		 * creating a `trojan horse' by substituting their own
5137c478bd9Sstevel@tonic-gate 		 * interpreter that could preform privileged operations
5147c478bd9Sstevel@tonic-gate 		 * when ldd is against it.
5157c478bd9Sstevel@tonic-gate 		 */
5167c478bd9Sstevel@tonic-gate 		if ((fflag == 0) && (geteuid() == 0) &&
5177c478bd9Sstevel@tonic-gate 		    (strcmp(fname, conv_lddstub(class)) != 0)) {
5187c478bd9Sstevel@tonic-gate 			char	*interpreter;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 			/*
5217c478bd9Sstevel@tonic-gate 			 * Does the interpreter live under a trusted directory.
5227c478bd9Sstevel@tonic-gate 			 */
5237c478bd9Sstevel@tonic-gate 			interpreter = elf_getident(elf, 0) + phdr.p_offset;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 			if ((strncmp(interpreter, MSG_ORIG(MSG_PTH_USRLIB),
5267c478bd9Sstevel@tonic-gate 			    MSG_PTH_USRLIB_SIZE) != 0) &&
5277c478bd9Sstevel@tonic-gate 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_LIB),
5287c478bd9Sstevel@tonic-gate 			    MSG_PTH_LIB_SIZE) != 0) &&
5297c478bd9Sstevel@tonic-gate 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_ETCLIB),
5307c478bd9Sstevel@tonic-gate 			    MSG_PTH_ETCLIB_SIZE) != 0)) {
5317c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_USP_ELFINS),
532df4628cbSrie 				    cname, fname, interpreter);
5337c478bd9Sstevel@tonic-gate 				return (1);
5347c478bd9Sstevel@tonic-gate 			}
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	/*
5397c478bd9Sstevel@tonic-gate 	 * Catch the case of a static executable (ie, an ET_EXEC that has a set
5407c478bd9Sstevel@tonic-gate 	 * of program headers but no PT_DYNAMIC).
5417c478bd9Sstevel@tonic-gate 	 */
5427c478bd9Sstevel@tonic-gate 	if (ehdr.e_phnum && !dynamic) {
5437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname,
5447c478bd9Sstevel@tonic-gate 		    fname);
5457c478bd9Sstevel@tonic-gate 		return (1);
5467c478bd9Sstevel@tonic-gate 	}
5477c478bd9Sstevel@tonic-gate 
5484899432aSab 	/*
5494899432aSab 	 * If there is a dynamic section, then check for the DF_1_NOHDR
5504899432aSab 	 * flag, and bail if it is present. Those objects are created using
5514899432aSab 	 * the ?N mapfile option: The ELF header and program headers are
5524899432aSab 	 * not mapped as part of the first segment, and virtual addresses
5534899432aSab 	 * are computed without them. If ldd tries to interpret such
5544899432aSab 	 * a file, it will become confused and generate bad output or
5554899432aSab 	 * crash. Such objects are always special purpose files (like an OS
5564899432aSab 	 * kernel) --- files for which the ldd operation doesn't make sense.
5574899432aSab 	 */
5584899432aSab 	if (dynamic && (_gelf_getdyndtflags_1(elf) & DF_1_NOHDR)) {
5594899432aSab 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOHDR), cname,
5604899432aSab 		    fname);
5614899432aSab 		return (1);
5624899432aSab 	}
5634899432aSab 
5647c478bd9Sstevel@tonic-gate 	load = load_elf;
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	/*
5677c478bd9Sstevel@tonic-gate 	 * Run the required program (shared and relocatable objects require the
5687c478bd9Sstevel@tonic-gate 	 * use of lddstub).
5697c478bd9Sstevel@tonic-gate 	 */
5707c478bd9Sstevel@tonic-gate 	if ((ehdr.e_type == ET_EXEC) && interp)
5717c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, (const char *)fname, class));
5727c478bd9Sstevel@tonic-gate 	else
5737c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, conv_lddstub(class), class));
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate static int
5777c478bd9Sstevel@tonic-gate aout_check(int nfile, char *fname, char *cname, int fd, int fflag)
5787c478bd9Sstevel@tonic-gate {
5797c478bd9Sstevel@tonic-gate 	struct exec	aout;
5807c478bd9Sstevel@tonic-gate 	int		err;
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 	if (lseek(fd, 0, SEEK_SET) != 0) {
5837c478bd9Sstevel@tonic-gate 		err = errno;
5847c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_LSEEK), cname, fname,
5857c478bd9Sstevel@tonic-gate 		    strerror(err));
5867c478bd9Sstevel@tonic-gate 		return (1);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 	if (read(fd, (char *)&aout, sizeof (struct exec)) !=
5897c478bd9Sstevel@tonic-gate 	    sizeof (struct exec)) {
5907c478bd9Sstevel@tonic-gate 		err = errno;
5917c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_READ), cname, fname,
5927c478bd9Sstevel@tonic-gate 		    strerror(err));
5937c478bd9Sstevel@tonic-gate 		return (1);
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 	if (aout.a_machtype != M_SPARC) {
5967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN), cname, fname);
5977c478bd9Sstevel@tonic-gate 		return (1);
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 	if (N_BADMAG(aout) || !aout.a_dynamic) {
6007c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname,
6017c478bd9Sstevel@tonic-gate 		    fname);
6027c478bd9Sstevel@tonic-gate 		return (1);
6037c478bd9Sstevel@tonic-gate 	}
6047c478bd9Sstevel@tonic-gate 	if (!fflag && (geteuid() == 0)) {
6057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_USP_AOUTINS), cname, fname);
6067c478bd9Sstevel@tonic-gate 		return (1);
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/*
6107c478bd9Sstevel@tonic-gate 	 * Run the required program.
6117c478bd9Sstevel@tonic-gate 	 */
6127c478bd9Sstevel@tonic-gate 	if ((aout.a_magic == ZMAGIC) &&
6137c478bd9Sstevel@tonic-gate 	    (aout.a_entry <= sizeof (struct exec))) {
6147c478bd9Sstevel@tonic-gate 		load = load_elf;
6157c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, conv_lddstub(ELFCLASS32),
6167c478bd9Sstevel@tonic-gate 		    ELFCLASS32));
6177c478bd9Sstevel@tonic-gate 	} else {
6187c478bd9Sstevel@tonic-gate 		load = load_aout;
6197c478bd9Sstevel@tonic-gate 		return (run(nfile, cname, fname, (const char *)fname,
6207c478bd9Sstevel@tonic-gate 		    ELFCLASS32));
6217c478bd9Sstevel@tonic-gate 	}
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate /*
6267c478bd9Sstevel@tonic-gate  * Run the required program, setting the preload and trace environment
6277c478bd9Sstevel@tonic-gate  * variables accordingly.
6287c478bd9Sstevel@tonic-gate  */
6297c478bd9Sstevel@tonic-gate static int
6307c478bd9Sstevel@tonic-gate run(int nfile, char *cname, char *fname, const char *ename, int class)
6317c478bd9Sstevel@tonic-gate {
6327c478bd9Sstevel@tonic-gate 	const char	*preload = 0;
6337c478bd9Sstevel@tonic-gate 	int		pid, status;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
6367c478bd9Sstevel@tonic-gate 		int	err = errno;
6377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK), cname,
6387c478bd9Sstevel@tonic-gate 		    strerror(err));
6397c478bd9Sstevel@tonic-gate 		return (1);
6407c478bd9Sstevel@tonic-gate 	}
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	if (pid) {				/* parent */
6437c478bd9Sstevel@tonic-gate 		while (wait(&status) != pid)
6447c478bd9Sstevel@tonic-gate 			;
6457c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(status) && ((WSIGMASK & status) != SIGPIPE)) {
6467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
6477c478bd9Sstevel@tonic-gate 			    fname);
6487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_SIG),
6497c478bd9Sstevel@tonic-gate 			    (WSIGMASK & status), ((status & WCOREFLG) ?
6507c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_SYS_EXEC_CORE) :
6517c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_EMPTY)));
6527c478bd9Sstevel@tonic-gate 			status = 1;
6537c478bd9Sstevel@tonic-gate 		} else if (WHIBYTE(status)) {
6547c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
6557c478bd9Sstevel@tonic-gate 			    fname);
6567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_STAT),
6577c478bd9Sstevel@tonic-gate 			    WHIBYTE(status));
6587c478bd9Sstevel@tonic-gate 			status = 1;
6597c478bd9Sstevel@tonic-gate 		}
6607c478bd9Sstevel@tonic-gate 	} else {				/* child */
6617c478bd9Sstevel@tonic-gate 		Listnode	*lnp;
6627c478bd9Sstevel@tonic-gate 		char		*str;
6637c478bd9Sstevel@tonic-gate 		size_t		size;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		/*
6667c478bd9Sstevel@tonic-gate 		 * When using ldd(1) to analyze a shared object we preload the
6677c478bd9Sstevel@tonic-gate 		 * shared object with lddstub.  Any additional preload
6687c478bd9Sstevel@tonic-gate 		 * requirements are added after the object being analyzed, this
6697c478bd9Sstevel@tonic-gate 		 * allows us to skip the first object but produce diagnostics
6707c478bd9Sstevel@tonic-gate 		 * for each other preloaded object.
6717c478bd9Sstevel@tonic-gate 		 */
6727c478bd9Sstevel@tonic-gate 		if (fname != ename) {
6737c478bd9Sstevel@tonic-gate 			char		*str;
6747c478bd9Sstevel@tonic-gate 			const char	*files = prefile;
6757c478bd9Sstevel@tonic-gate 			const char	*format = MSG_ORIG(MSG_STR_FMT1);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 			for (str = fname; *str; str++)
6787c478bd9Sstevel@tonic-gate 				if (*str == '/') {
6797c478bd9Sstevel@tonic-gate 					format = MSG_ORIG(MSG_STR_FMT2);
6807c478bd9Sstevel@tonic-gate 					break;
6817c478bd9Sstevel@tonic-gate 			}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 			preload = MSG_ORIG(MSG_LD_PRELOAD);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			/*
6867c478bd9Sstevel@tonic-gate 			 * Determine which preload files and preload environment
6877c478bd9Sstevel@tonic-gate 			 * variable to use.
6887c478bd9Sstevel@tonic-gate 			 */
6897c478bd9Sstevel@tonic-gate 			if (class == ELFCLASS64) {
6907c478bd9Sstevel@tonic-gate 				if (prefile_64 != MSG_ORIG(MSG_STR_EMPTY)) {
6917c478bd9Sstevel@tonic-gate 					files = prefile_64;
6927c478bd9Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_64);
6937c478bd9Sstevel@tonic-gate 				}
6947c478bd9Sstevel@tonic-gate 			} else {
6957c478bd9Sstevel@tonic-gate 				if (prefile_32 != MSG_ORIG(MSG_STR_EMPTY)) {
6967c478bd9Sstevel@tonic-gate 					files = prefile_32;
6977c478bd9Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_32);
6987c478bd9Sstevel@tonic-gate 				}
6997c478bd9Sstevel@tonic-gate 			}
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			if ((str = (char *)malloc(strlen(preload) +
7027c478bd9Sstevel@tonic-gate 			    strlen(fname) + strlen(files) + 5)) == 0) {
7037c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
7047c478bd9Sstevel@tonic-gate 				    cname);
7057c478bd9Sstevel@tonic-gate 				exit(1);
7067c478bd9Sstevel@tonic-gate 			}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 			(void) sprintf(str, format, preload, fname, files);
7097c478bd9Sstevel@tonic-gate 			if (putenv(str) != 0) {
7107c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
7117c478bd9Sstevel@tonic-gate 				    cname);
7127c478bd9Sstevel@tonic-gate 				exit(1);
7137c478bd9Sstevel@tonic-gate 			}
714df4628cbSrie 
715df4628cbSrie 			/*
716df4628cbSrie 			 * The pointer "load" has be assigned to load_elf[] or
717df4628cbSrie 			 * load_aout[].  Use the size of load_elf[] as the size
718df4628cbSrie 			 * of load_aout[] is the same.
719df4628cbSrie 			 */
720df4628cbSrie 			load[sizeof (load_elf) - 2] = '2';
7217c478bd9Sstevel@tonic-gate 		} else
722df4628cbSrie 			load[sizeof (load_elf) - 2] = '1';
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		/*
7267c478bd9Sstevel@tonic-gate 		 * Establish new environment variables to affect the child
7277c478bd9Sstevel@tonic-gate 		 * process.
7287c478bd9Sstevel@tonic-gate 		 */
7297c478bd9Sstevel@tonic-gate 		if ((putenv(warn) != 0) || (putenv(bind) != 0) ||
7307c478bd9Sstevel@tonic-gate 		    (putenv(path) != 0) || (putenv(verb) != 0) ||
7317c478bd9Sstevel@tonic-gate 		    (putenv(fltr) != 0) || (putenv(conf) != 0) ||
7327c478bd9Sstevel@tonic-gate 		    (putenv(init) != 0) || (putenv(lazy) != 0) ||
7337c478bd9Sstevel@tonic-gate 		    (putenv(uref) != 0) || (putenv(used) != 0) ||
734*dae2dfb7Srie 		    (putenv(weak) != 0) || (putenv(load) != 0) ||
735*dae2dfb7Srie 		    (putenv(nope) != 0)) {
7367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), cname);
7377c478bd9Sstevel@tonic-gate 			exit(1);
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 		/*
7417c478bd9Sstevel@tonic-gate 		 * Establish explicit environment requires (but don't override
7427c478bd9Sstevel@tonic-gate 		 * any preload request established to process a shared object).
7437c478bd9Sstevel@tonic-gate 		 */
7447c478bd9Sstevel@tonic-gate 		size = 0;
7457c478bd9Sstevel@tonic-gate 		for (LIST_TRAVERSE(&eopts, lnp, str)) {
7467c478bd9Sstevel@tonic-gate 			if (preload) {
7477c478bd9Sstevel@tonic-gate 				if (size == 0)
7487c478bd9Sstevel@tonic-gate 					size = strlen(preload);
7497c478bd9Sstevel@tonic-gate 				if ((strncmp(preload, str, size) == 0) &&
7507c478bd9Sstevel@tonic-gate 				    (str[size] == '=')) {
7517c478bd9Sstevel@tonic-gate 					continue;
7527c478bd9Sstevel@tonic-gate 				}
7537c478bd9Sstevel@tonic-gate 			}
7547c478bd9Sstevel@tonic-gate 			if (putenv(str) != 0) {
7557c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
7567c478bd9Sstevel@tonic-gate 				    cname);
7577c478bd9Sstevel@tonic-gate 				exit(1);
7587c478bd9Sstevel@tonic-gate 			}
7597c478bd9Sstevel@tonic-gate 		}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 		/*
7627c478bd9Sstevel@tonic-gate 		 * Execute the object and let ld.so.1 do the rest.
7637c478bd9Sstevel@tonic-gate 		 */
7647c478bd9Sstevel@tonic-gate 		if (nfile > 1)
7657c478bd9Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_STR_FMT3), fname);
7667c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
7677c478bd9Sstevel@tonic-gate 		if ((execl(ename, ename, (char *)0)) == -1) {
7687c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
7697c478bd9Sstevel@tonic-gate 			    fname);
7707c478bd9Sstevel@tonic-gate 			perror(ename);
7717c478bd9Sstevel@tonic-gate 			_exit(0);
7727c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
7737c478bd9Sstevel@tonic-gate 		}
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 	return (status);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate const char *
7797c478bd9Sstevel@tonic-gate _ldd_msg(Msg mid)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
7827c478bd9Sstevel@tonic-gate }
783