xref: /illumos-gate/usr/src/cmd/sgs/ldd/common/ldd.c (revision 9174bfaa)
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  *
255bd55801SAli Bahrami  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Print the list of shared objects required by a dynamic executable or shared
307c478bd9Sstevel@tonic-gate  * object.
317c478bd9Sstevel@tonic-gate  *
3202938ba2SRod Evans  * usage is: ldd [-d | -r] [-c] [-D] [-e envar] [-i] [-f] [-L] [-l] [-p] [-s]
33df4628cbSrie  *		[-U | -u] [-v] [-w] file(s)
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * ldd opens the file and verifies the information in the elf header.
367c478bd9Sstevel@tonic-gate  * If the file is a dynamic executable, we set up some environment variables
377c478bd9Sstevel@tonic-gate  * and exec(2) the file.  If the file is a shared object, we preload the
387c478bd9Sstevel@tonic-gate  * file with a dynamic executable stub. The runtime linker (ld.so.1) actually
397c478bd9Sstevel@tonic-gate  * provides the diagnostic output, according to the environment variables set.
407c478bd9Sstevel@tonic-gate  *
41*9174bfaaSGarrett D'Amore  * If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_E.
427c478bd9Sstevel@tonic-gate  * The runtime linker will print the pathnames of all dynamic objects it
43*9174bfaaSGarrett D'Amore  * loads, and then exit.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * If -d or -r is specified, we also set LD_WARN=1; the runtime linker will
467c478bd9Sstevel@tonic-gate  * perform its normal relocations and issue warning messages for unresolved
477c478bd9Sstevel@tonic-gate  * references. It will then exit.
487c478bd9Sstevel@tonic-gate  * If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker
497c478bd9Sstevel@tonic-gate  * will perform all relocations, otherwise (under -d) the runtime linker
507c478bd9Sstevel@tonic-gate  * will not perform PLT (function) type relocations.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * If -c is specified we also set LD_NOCONFIG=1, thus disabling any
537c478bd9Sstevel@tonic-gate  * configuration file use.
547c478bd9Sstevel@tonic-gate  *
55f441771bSRod Evans  * If -D is specified we skip deferred dependency processing.  By default,
56f441771bSRod Evans  * ldd loads all deferred dependencies.  However, during normal process
57f441771bSRod Evans  * execution, deferred dependencies are only loaded when an explicit binding
58f441771bSRod Evans  * to an individual deferred reference is made.  As no user code is executed
59f441771bSRod Evans  * under ldd, explicit references to deferred symbols can't be triggered.
60f441771bSRod Evans  *
617c478bd9Sstevel@tonic-gate  * If -e is specified the associated environment variable is set for the
627c478bd9Sstevel@tonic-gate  * child process that will produce ldd's diagnostics.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * If -i is specified, we set LD_INIT=1. The order of inititialization
657c478bd9Sstevel@tonic-gate  * sections to be executed is printed. We also set LD_WARN=1.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * If -f is specified, we will run ldd as root on executables that have
687c478bd9Sstevel@tonic-gate  * an unsercure runtime linker that does not live under the "/usr/lib"
697c478bd9Sstevel@tonic-gate  * directory.  By default we will not let this happen.
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * If -l is specified it generates a warning for any auxiliary filter not found.
727c478bd9Sstevel@tonic-gate  * Prior to 2.8 this forced any filters to load (all) their filtees.  This is
737c478bd9Sstevel@tonic-gate  * now the default, however missing auxiliary filters don't generate any error
747c478bd9Sstevel@tonic-gate  * diagniostic.  See also -L.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * If -L is specified we revert to lazy loading, thus any filtee or lazy
777c478bd9Sstevel@tonic-gate  * dependency loading is deferred until relocations cause loading.  Without
787c478bd9Sstevel@tonic-gate  * this option we set LD_LOADFLTR=1, thus forcing any filters to load (all)
797c478bd9Sstevel@tonic-gate  * their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of
807c478bd9Sstevel@tonic-gate  * any lazy loaded dependencies.
817c478bd9Sstevel@tonic-gate  *
827c478bd9Sstevel@tonic-gate  * If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling
837c478bd9Sstevel@tonic-gate  * the runtime linker to indicate the search algorithm used.
847c478bd9Sstevel@tonic-gate  *
857c478bd9Sstevel@tonic-gate  * If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime
867c478bd9Sstevel@tonic-gate  * linker to indicate all object dependencies (not just the first object
87f441771bSRod Evans  * loaded) together with any versioning requirements.
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  * If -U or -u is specified unused dependencies are detected.  -u causes
907c478bd9Sstevel@tonic-gate  * LD_UNUSED=1 to be set, which causes dependencies that are unused within the
917c478bd9Sstevel@tonic-gate  * process to be detected.  -U causes LD_UNREF=1 to be set, which causes
927c478bd9Sstevel@tonic-gate  * unreferenced objects, and unreferenced cyclic dependencies to be detected.
937c478bd9Sstevel@tonic-gate  * These options assert that at least -d is set as relocation references are
947c478bd9Sstevel@tonic-gate  * what determine an objects use.
95df4628cbSrie  *
96df4628cbSrie  * If -w is specified, no unresolved weak references are allowed.  -w causes
97df4628cbSrie  * LD_NOUNRESWEAK=1 to be set.  By default, an unresolved weak reference is
98df4628cbSrie  * allowed, and a "0" is written to the relocation offset.  The -w option
99df4628cbSrie  * disables this default.  Any weak references that can not be resolved result
100dae2dfb7Srie  * in relocation error messages.  This option has no use without -r or -d.
101dae2dfb7Srie  *
102dae2dfb7Srie  * If the -p option is specified, no unresolved PARENT or EXTERN references are
103dae2dfb7Srie  * allowed.  -p causes LD_NOPAREXT=1 to be set.  By default, PARENT and EXTERN
104dae2dfb7Srie  * references, which have been explicitly assigned via a mapfile when a shared
105dae2dfb7Srie  * object was built, imply that a caller will provide the symbols, and hence
106dae2dfb7Srie  * these are not reported as relocation errors.  Note, the -p option is asserted
107dae2dfb7Srie  * by default when either the -r or -d options are used to inspect a dynamic
108dae2dfb7Srie  * executable.  This option has no use with a shared object without -r or -d.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate #include	<fcntl.h>
1117c478bd9Sstevel@tonic-gate #include	<stdio.h>
1127c478bd9Sstevel@tonic-gate #include	<string.h>
1134899432aSab #include	<_libelf.h>
1147c478bd9Sstevel@tonic-gate #include	<stdlib.h>
1157c478bd9Sstevel@tonic-gate #include	<unistd.h>
1167c478bd9Sstevel@tonic-gate #include	<wait.h>
1177c478bd9Sstevel@tonic-gate #include	<locale.h>
1187c478bd9Sstevel@tonic-gate #include	<errno.h>
1197c478bd9Sstevel@tonic-gate #include	<signal.h>
1207c478bd9Sstevel@tonic-gate #include	"machdep.h"
1217c478bd9Sstevel@tonic-gate #include	"sgs.h"
1227c478bd9Sstevel@tonic-gate #include	"conv.h"
1237c478bd9Sstevel@tonic-gate #include	"a.out.h"
1247c478bd9Sstevel@tonic-gate #include	"msg.h"
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int	elf_check(int, char *, char *, Elf *, int);
1277c478bd9Sstevel@tonic-gate static int	run(int, char *, char *, const char *, int);
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*
131df4628cbSrie  * Define all environment variable strings.  The character following the "="
132df4628cbSrie  * will be written to, to disable or enable the associated feature.
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate static char	bind[] =	"LD_BIND_NOW= ",
1357c478bd9Sstevel@tonic-gate 		load_elf[] =	"LD_TRACE_LOADED_OBJECTS_E= ",
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= ",
145dae2dfb7Srie 		weak[] =	"LD_NOUNRESWEAK= ",
146f441771bSRod Evans 		nope[] =	"LD_NOPAREXT= ",
147f441771bSRod Evans 		defr[] =	"LD_DEFERRED= ";
1487c478bd9Sstevel@tonic-gate static char	*load;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate static const char	*prefile_32, *prefile_64, *prefile;
15157ef7aa9SRod Evans static APlist		*eopts = NULL;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv,char ** envp)154df14233eSab main(int argc, char **argv, char **envp)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	char	*str, *cname = argv[0];
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	Elf	*elf;
1597c478bd9Sstevel@tonic-gate 	int	cflag = 0, dflag = 0, fflag = 0, iflag = 0, Lflag = 0;
1607c478bd9Sstevel@tonic-gate 	int	lflag = 0, rflag = 0, sflag = 0, Uflag = 0, uflag = 0;
161f441771bSRod Evans 	int	Dflag = 0, pflag = 0, vflag = 0, wflag = 0;
162f441771bSRod Evans 	int	nfile, var, error = 0;
16357ef7aa9SRod Evans 	Aliste	idx;
1647c478bd9Sstevel@tonic-gate 
165df14233eSab 	/*
166df14233eSab 	 * If we're on a 64-bit kernel, try to exec a full 64-bit version of
167df14233eSab 	 * the binary.  If successful, conv_check_native() won't return.
168df14233eSab 	 *
169df14233eSab 	 * This is done to ensure that ldd can handle objects >2GB.
170df14233eSab 	 * ldd uses libelf, which is not large file capable. The
171df14233eSab 	 * 64-bit ldd can handle any sized object.
172df14233eSab 	 */
173df14233eSab 	(void) conv_check_native(argv, envp);
174df14233eSab 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * Establish locale.
1777c478bd9Sstevel@tonic-gate 	 */
1787c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
1797c478bd9Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	/*
1827c478bd9Sstevel@tonic-gate 	 * verify command line syntax and process arguments
1837c478bd9Sstevel@tonic-gate 	 */
1847c478bd9Sstevel@tonic-gate 	opterr = 0;				/* disable getopt error mesg */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_GETOPT))) != EOF) {
1877c478bd9Sstevel@tonic-gate 		switch (var) {
1887c478bd9Sstevel@tonic-gate 		case 'c' :			/* enable config search */
1897c478bd9Sstevel@tonic-gate 			cflag = 1;
1907c478bd9Sstevel@tonic-gate 			break;
191f441771bSRod Evans 		case 'D' :			/* skip deferred dependencies */
192f441771bSRod Evans 			Dflag = 1;
193f441771bSRod Evans 			break;
1947c478bd9Sstevel@tonic-gate 		case 'd' :			/* perform data relocations */
1957c478bd9Sstevel@tonic-gate 			dflag = 1;
1967c478bd9Sstevel@tonic-gate 			if (rflag)
1977c478bd9Sstevel@tonic-gate 				error++;
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		case 'e' :
20057ef7aa9SRod Evans 			if (aplist_append(&eopts, optarg, 10) == NULL) {
2017c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
2027c478bd9Sstevel@tonic-gate 				    cname);
2037c478bd9Sstevel@tonic-gate 				exit(1);
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 		case 'f' :
2077c478bd9Sstevel@tonic-gate 			fflag = 1;
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 		case 'L' :
2107c478bd9Sstevel@tonic-gate 			Lflag = 1;
2117c478bd9Sstevel@tonic-gate 			break;
2127c478bd9Sstevel@tonic-gate 		case 'l' :
2137c478bd9Sstevel@tonic-gate 			lflag = 1;
2147c478bd9Sstevel@tonic-gate 			break;
2157c478bd9Sstevel@tonic-gate 		case 'i' :			/* print the order of .init */
2167c478bd9Sstevel@tonic-gate 			iflag = 1;
2177c478bd9Sstevel@tonic-gate 			break;
218dae2dfb7Srie 		case 'p' :
219dae2dfb7Srie 			pflag = 1;		/* expose unreferenced */
220dae2dfb7Srie 			break;			/*	parent or externals */
2217c478bd9Sstevel@tonic-gate 		case 'r' :			/* perform all relocations */
2227c478bd9Sstevel@tonic-gate 			rflag = 1;
2237c478bd9Sstevel@tonic-gate 			if (dflag)
2247c478bd9Sstevel@tonic-gate 				error++;
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 		case 's' :			/* enable search path output */
2277c478bd9Sstevel@tonic-gate 			sflag = 1;
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		case 'U' :			/* list unreferenced */
2307c478bd9Sstevel@tonic-gate 			Uflag = 1;		/*	dependencies */
2317c478bd9Sstevel@tonic-gate 			if (uflag)
2327c478bd9Sstevel@tonic-gate 				error++;
2337c478bd9Sstevel@tonic-gate 			break;
2347c478bd9Sstevel@tonic-gate 		case 'u' :			/* list unused dependencies */
2357c478bd9Sstevel@tonic-gate 			uflag = 1;
2367c478bd9Sstevel@tonic-gate 			if (Uflag)
2377c478bd9Sstevel@tonic-gate 				error++;
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 		case 'v' :			/* enable verbose output */
2407c478bd9Sstevel@tonic-gate 			vflag = 1;
2417c478bd9Sstevel@tonic-gate 			break;
242dae2dfb7Srie 		case 'w' :			/* expose unresolved weak */
243df4628cbSrie 			wflag = 1;		/*	references */
244df4628cbSrie 			break;
2457c478bd9Sstevel@tonic-gate 		default :
2467c478bd9Sstevel@tonic-gate 			error++;
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 		if (error)
2507c478bd9Sstevel@tonic-gate 			break;
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if (error) {
2537c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), cname);
2547c478bd9Sstevel@tonic-gate 		exit(1);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * Determine if any of the LD_PRELOAD family is already set in the
2597c478bd9Sstevel@tonic-gate 	 * environment, if so we'll continue to analyze each object with the
2607c478bd9Sstevel@tonic-gate 	 * appropriate setting.
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	if (((prefile_32 = getenv(MSG_ORIG(MSG_LD_PRELOAD_32))) == NULL) ||
2637c478bd9Sstevel@tonic-gate 	    (*prefile_32 == '\0')) {
2647c478bd9Sstevel@tonic-gate 		prefile_32 = MSG_ORIG(MSG_STR_EMPTY);
2657c478bd9Sstevel@tonic-gate 	}
2667c478bd9Sstevel@tonic-gate 	if (((prefile_64 = getenv(MSG_ORIG(MSG_LD_PRELOAD_64))) == NULL) ||
2677c478bd9Sstevel@tonic-gate 	    (*prefile_64 == '\0')) {
2687c478bd9Sstevel@tonic-gate 		prefile_64 = MSG_ORIG(MSG_STR_EMPTY);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	if (((prefile = getenv(MSG_ORIG(MSG_LD_PRELOAD))) == NULL) ||
2717c478bd9Sstevel@tonic-gate 	    (*prefile == '\0')) {
2727c478bd9Sstevel@tonic-gate 		prefile = MSG_ORIG(MSG_STR_EMPTY);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	/*
2767c478bd9Sstevel@tonic-gate 	 * Determine if any environment requests are for the LD_PRELOAD family,
2777c478bd9Sstevel@tonic-gate 	 * and if so override any environment settings we've established above.
2787c478bd9Sstevel@tonic-gate 	 */
27957ef7aa9SRod Evans 	for (APLIST_TRAVERSE(eopts, idx, str)) {
2807c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_32),
2817c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_32_SIZE)) == 0) {
2827c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_32_SIZE;
2837c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
2847c478bd9Sstevel@tonic-gate 				prefile_32 = str;
2857c478bd9Sstevel@tonic-gate 			continue;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_64),
2887c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_64_SIZE)) == 0) {
2897c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_64_SIZE;
2907c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
2917c478bd9Sstevel@tonic-gate 				prefile_64 = str;
2927c478bd9Sstevel@tonic-gate 			continue;
2937c478bd9Sstevel@tonic-gate 		}
2947c478bd9Sstevel@tonic-gate 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD),
2957c478bd9Sstevel@tonic-gate 		    MSG_LD_PRELOAD_SIZE)) == 0) {
2967c478bd9Sstevel@tonic-gate 			str += MSG_LD_PRELOAD_SIZE;
2977c478bd9Sstevel@tonic-gate 			if ((*str++ == '=') && (*str != '\0'))
2987c478bd9Sstevel@tonic-gate 				prefile = str;
2997c478bd9Sstevel@tonic-gate 			continue;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * Set the appropriate relocation environment variables (Note unsetting
3057c478bd9Sstevel@tonic-gate 	 * the environment variables is done just in case the user already
3067c478bd9Sstevel@tonic-gate 	 * has these in their environment ... sort of thing the test folks
3077c478bd9Sstevel@tonic-gate 	 * would do :-)
3087c478bd9Sstevel@tonic-gate 	 */
309df4628cbSrie 	warn[sizeof (warn) - 2] = (dflag || rflag || Uflag || uflag) ? '1' :
3107c478bd9Sstevel@tonic-gate 	    '\0';
311df4628cbSrie 	bind[sizeof (bind) - 2] = (rflag) ? '1' : '\0';
312df4628cbSrie 	path[sizeof (path) - 2] = (sflag) ? '1' : '\0';
313df4628cbSrie 	verb[sizeof (verb) - 2] = (vflag) ? '1' : '\0';
314df4628cbSrie 	fltr[sizeof (fltr) - 2] = (Lflag) ? '\0' : (lflag) ? '2' : '1';
315df4628cbSrie 	init[sizeof (init) - 2] = (iflag) ? '1' : '\0';
316df4628cbSrie 	conf[sizeof (conf) - 2] = (cflag) ? '1' : '\0';
317df4628cbSrie 	lazy[sizeof (lazy) - 2] = (Lflag) ? '\0' : '1';
318df4628cbSrie 	uref[sizeof (uref) - 2] = (Uflag) ? '1' : '\0';
319df4628cbSrie 	used[sizeof (used) - 2] = (uflag) ? '1' : '\0';
320df4628cbSrie 	weak[sizeof (weak) - 2] = (wflag) ? '1' : '\0';
321dae2dfb7Srie 	nope[sizeof (nope) - 2] = (pflag) ? '1' : '\0';
322f441771bSRod Evans 	defr[sizeof (defr) - 2] = (Dflag) ? '\0' : '1';
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/*
3257c478bd9Sstevel@tonic-gate 	 * coordinate libelf's version information
3267c478bd9Sstevel@tonic-gate 	 */
3277c478bd9Sstevel@tonic-gate 	if (elf_version(EV_CURRENT) == EV_NONE) {
3287c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_LIBELF), cname,
3297c478bd9Sstevel@tonic-gate 		    EV_CURRENT);
3307c478bd9Sstevel@tonic-gate 		exit(1);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	/*
3347c478bd9Sstevel@tonic-gate 	 * Loop through remaining arguments.  Note that from here on there
3357c478bd9Sstevel@tonic-gate 	 * are no exit conditions so that we can process a list of files,
3367c478bd9Sstevel@tonic-gate 	 * any error condition is retained for a final exit status.
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	nfile = argc - optind;
3397c478bd9Sstevel@tonic-gate 	for (; optind < argc; optind++) {
3407c478bd9Sstevel@tonic-gate 		char	*fname = argv[optind];
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 		/*
3437c478bd9Sstevel@tonic-gate 		 * Open file (do this before checking access so that we can
3447c478bd9Sstevel@tonic-gate 		 * provide the user with better diagnostics).
3457c478bd9Sstevel@tonic-gate 		 */
3467c478bd9Sstevel@tonic-gate 		if ((var = open(fname, O_RDONLY)) == -1) {
3477c478bd9Sstevel@tonic-gate 			int	err = errno;
3487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), cname,
3497c478bd9Sstevel@tonic-gate 			    fname, strerror(err));
3507c478bd9Sstevel@tonic-gate 			error = 1;
3517c478bd9Sstevel@tonic-gate 			continue;
3527c478bd9Sstevel@tonic-gate 		}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		/*
355*9174bfaaSGarrett D'Amore 		 * Get the files elf descriptor and process it as an elf file.
3567c478bd9Sstevel@tonic-gate 		 */
3577c478bd9Sstevel@tonic-gate 		elf = elf_begin(var, ELF_C_READ, (Elf *)0);
3587c478bd9Sstevel@tonic-gate 		switch (elf_kind(elf)) {
3597c478bd9Sstevel@tonic-gate 		case ELF_K_AR :
3607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO),
3617c478bd9Sstevel@tonic-gate 			    cname, fname);
3627c478bd9Sstevel@tonic-gate 			error = 1;
3637c478bd9Sstevel@tonic-gate 			break;
3647c478bd9Sstevel@tonic-gate 		case ELF_K_ELF:
3651304f648SToomas Soome 			if (elf_check(nfile, fname, cname, elf, fflag) != 0)
3667c478bd9Sstevel@tonic-gate 				error = 1;
3677c478bd9Sstevel@tonic-gate 			break;
368*9174bfaaSGarrett D'Amore 		case ELF_K_COFF:
3697c478bd9Sstevel@tonic-gate 		default:
370*9174bfaaSGarrett D'Amore 			(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN),
371*9174bfaaSGarrett D'Amore 			    cname, fname);
372*9174bfaaSGarrett D'Amore 			error = 1;
3737c478bd9Sstevel@tonic-gate 			break;
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 		(void) elf_end(elf);
3767c478bd9Sstevel@tonic-gate 		(void) close(var);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 	return (error);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate static int
elf_check(int nfile,char * fname,char * cname,Elf * elf,int fflag)3847c478bd9Sstevel@tonic-gate elf_check(int nfile, char *fname, char *cname, Elf *elf, int fflag)
3857c478bd9Sstevel@tonic-gate {
3865bd55801SAli Bahrami 	Conv_inv_buf_t	inv_buf;
3871304f648SToomas Soome 	GElf_Ehdr	ehdr;
3881304f648SToomas Soome 	GElf_Phdr	phdr;
3897c478bd9Sstevel@tonic-gate 	int		dynamic = 0, interp = 0, cnt, class;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * verify information in file header
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	if (gelf_getehdr(elf, &ehdr) == NULL) {
3957c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR),
396df4628cbSrie 		    cname, fname, elf_errmsg(-1));
3977c478bd9Sstevel@tonic-gate 		return (1);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/*
4015bd55801SAli Bahrami 	 * Compatible machine
4027c478bd9Sstevel@tonic-gate 	 */
4035bd55801SAli Bahrami 	if ((ehdr.e_machine != M_MACH_32) && (ehdr.e_machine != M_MACH_64) &&
4045bd55801SAli Bahrami 	    (ehdr.e_machine != M_MACHPLUS)) {
4055bd55801SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHTYPE), cname, fname,
4065bd55801SAli Bahrami 		    conv_ehdr_mach(ehdr.e_machine, 0, &inv_buf));
4077c478bd9Sstevel@tonic-gate 		return (1);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4115bd55801SAli Bahrami 	 * Compatible encoding (byte order)
4127c478bd9Sstevel@tonic-gate 	 */
4135bd55801SAli Bahrami 	if (ehdr.e_ident[EI_DATA] != M_DATA) {
4145bd55801SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_DATA), cname, fname,
4155bd55801SAli Bahrami 		    conv_ehdr_data(ehdr.e_ident[EI_DATA], 0, &inv_buf));
4167c478bd9Sstevel@tonic-gate 		return (1);
4177c478bd9Sstevel@tonic-gate 	}
4185bd55801SAli Bahrami 
4195bd55801SAli Bahrami 	/*
4205bd55801SAli Bahrami 	 * Compatible class
4215bd55801SAli Bahrami 	 */
4225bd55801SAli Bahrami 	switch (class = ehdr.e_ident[EI_CLASS]) {
4235bd55801SAli Bahrami 	case ELFCLASS32:
4245bd55801SAli Bahrami 		/*
4255bd55801SAli Bahrami 		 * If M_MACH is not the same thing as M_MACHPLUS and this
4265bd55801SAli Bahrami 		 * is an M_MACHPLUS object, then the corresponding header
4275bd55801SAli Bahrami 		 * flag must be set.
4285bd55801SAli Bahrami 		 */
4295bd55801SAli Bahrami 		if ((ehdr.e_machine != M_MACH) &&
4305bd55801SAli Bahrami 		    ((ehdr.e_flags & M_FLAGSPLUS) == 0)) {
4315bd55801SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHFLAGS),
432df4628cbSrie 			    cname, fname);
4337c478bd9Sstevel@tonic-gate 			return (1);
4347c478bd9Sstevel@tonic-gate 		}
4355bd55801SAli Bahrami 		break;
4365bd55801SAli Bahrami 	case ELFCLASS64:
4375bd55801SAli Bahrami 		/* Requires 64-bit kernel */
4385bd55801SAli Bahrami 		if (conv_sys_eclass() == ELFCLASS32) {
4395bd55801SAli Bahrami 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_KCLASS32),
4405bd55801SAli Bahrami 			    cname, fname, conv_ehdr_class(class, 0, &inv_buf));
4417c478bd9Sstevel@tonic-gate 			return (1);
4427c478bd9Sstevel@tonic-gate 		}
4435bd55801SAli Bahrami 		break;
4445bd55801SAli Bahrami 	default:
4455bd55801SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_CLASS), cname, fname,
4465bd55801SAli Bahrami 		    conv_ehdr_class(class, 0, &inv_buf));
4475bd55801SAli Bahrami 		return (1);
4485bd55801SAli Bahrami 	}
4495bd55801SAli Bahrami 
4505bd55801SAli Bahrami 	/*
4515bd55801SAli Bahrami 	 * Object type
4525bd55801SAli Bahrami 	 */
4535bd55801SAli Bahrami 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN) &&
4545bd55801SAli Bahrami 	    (ehdr.e_type != ET_REL)) {
4555bd55801SAli Bahrami 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_BADMAGIC),
4565bd55801SAli Bahrami 		    cname, fname);
4575bd55801SAli Bahrami 		return (1);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * Check that the file is executable.  Dynamic executables must be
46201355ae8SRichard Lowe 	 * executable to be exec'ed for ldd(1) to function.
4637c478bd9Sstevel@tonic-gate 	 */
46401355ae8SRichard Lowe 	if ((access(fname, X_OK) != 0) && (ehdr.e_type == ET_EXEC)) {
46501355ae8SRichard Lowe 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC),
46601355ae8SRichard Lowe 		    cname, fname);
46701355ae8SRichard Lowe 		return (1);
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
53569112eddSAli Bahrami 	 * flag, and bail if it is present. Such objects are created using
53669112eddSAli Bahrami 	 * a mapfile option (?N in the version 1 syntax, or HDR_NOALLOC
53769112eddSAli 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 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate  * Run the required program, setting the preload and trace environment
5657c478bd9Sstevel@tonic-gate  * variables accordingly.
5667c478bd9Sstevel@tonic-gate  */
5677c478bd9Sstevel@tonic-gate static int
run(int nfile,char * cname,char * fname,const char * ename,int class)5687c478bd9Sstevel@tonic-gate run(int nfile, char *cname, char *fname, const char *ename, int class)
5697c478bd9Sstevel@tonic-gate {
5707c478bd9Sstevel@tonic-gate 	const char	*preload = 0;
5717c478bd9Sstevel@tonic-gate 	int		pid, status;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == -1) {
5747c478bd9Sstevel@tonic-gate 		int	err = errno;
5757c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK), cname,
5767c478bd9Sstevel@tonic-gate 		    strerror(err));
5777c478bd9Sstevel@tonic-gate 		return (1);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	if (pid) {				/* parent */
5817c478bd9Sstevel@tonic-gate 		while (wait(&status) != pid)
5827c478bd9Sstevel@tonic-gate 			;
5837c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(status) && ((WSIGMASK & status) != SIGPIPE)) {
5847c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
5857c478bd9Sstevel@tonic-gate 			    fname);
5867c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_SIG),
5877c478bd9Sstevel@tonic-gate 			    (WSIGMASK & status), ((status & WCOREFLG) ?
5887c478bd9Sstevel@tonic-gate 			    MSG_INTL(MSG_SYS_EXEC_CORE) :
5897c478bd9Sstevel@tonic-gate 			    MSG_ORIG(MSG_STR_EMPTY)));
5907c478bd9Sstevel@tonic-gate 			status = 1;
5917c478bd9Sstevel@tonic-gate 		} else if (WHIBYTE(status)) {
5927c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
5937c478bd9Sstevel@tonic-gate 			    fname);
5947c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_STAT),
5957c478bd9Sstevel@tonic-gate 			    WHIBYTE(status));
5967c478bd9Sstevel@tonic-gate 			status = 1;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	} else {				/* child */
59957ef7aa9SRod Evans 		Aliste	idx;
60057ef7aa9SRod Evans 		char	*str;
60157ef7aa9SRod Evans 		size_t	size;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		/*
6047c478bd9Sstevel@tonic-gate 		 * When using ldd(1) to analyze a shared object we preload the
6057c478bd9Sstevel@tonic-gate 		 * shared object with lddstub.  Any additional preload
6067c478bd9Sstevel@tonic-gate 		 * requirements are added after the object being analyzed, this
6077c478bd9Sstevel@tonic-gate 		 * allows us to skip the first object but produce diagnostics
6087c478bd9Sstevel@tonic-gate 		 * for each other preloaded object.
6097c478bd9Sstevel@tonic-gate 		 */
6107c478bd9Sstevel@tonic-gate 		if (fname != ename) {
6117c478bd9Sstevel@tonic-gate 			char		*str;
6127c478bd9Sstevel@tonic-gate 			const char	*files = prefile;
6137c478bd9Sstevel@tonic-gate 			const char	*format = MSG_ORIG(MSG_STR_FMT1);
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 			for (str = fname; *str; str++)
6167c478bd9Sstevel@tonic-gate 				if (*str == '/') {
6177c478bd9Sstevel@tonic-gate 					format = MSG_ORIG(MSG_STR_FMT2);
6187c478bd9Sstevel@tonic-gate 					break;
6197c478bd9Sstevel@tonic-gate 			}
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 			preload = MSG_ORIG(MSG_LD_PRELOAD);
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 			/*
6247c478bd9Sstevel@tonic-gate 			 * Determine which preload files and preload environment
6257c478bd9Sstevel@tonic-gate 			 * variable to use.
6267c478bd9Sstevel@tonic-gate 			 */
6277c478bd9Sstevel@tonic-gate 			if (class == ELFCLASS64) {
6287c478bd9Sstevel@tonic-gate 				if (prefile_64 != MSG_ORIG(MSG_STR_EMPTY)) {
6297c478bd9Sstevel@tonic-gate 					files = prefile_64;
6307c478bd9Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_64);
6317c478bd9Sstevel@tonic-gate 				}
6327c478bd9Sstevel@tonic-gate 			} else {
6337c478bd9Sstevel@tonic-gate 				if (prefile_32 != MSG_ORIG(MSG_STR_EMPTY)) {
6347c478bd9Sstevel@tonic-gate 					files = prefile_32;
6357c478bd9Sstevel@tonic-gate 					preload = MSG_ORIG(MSG_LD_PRELOAD_32);
6367c478bd9Sstevel@tonic-gate 				}
6377c478bd9Sstevel@tonic-gate 			}
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 			if ((str = (char *)malloc(strlen(preload) +
6407c478bd9Sstevel@tonic-gate 			    strlen(fname) + strlen(files) + 5)) == 0) {
6417c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
6427c478bd9Sstevel@tonic-gate 				    cname);
6437c478bd9Sstevel@tonic-gate 				exit(1);
6447c478bd9Sstevel@tonic-gate 			}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 			(void) sprintf(str, format, preload, fname, files);
6477c478bd9Sstevel@tonic-gate 			if (putenv(str) != 0) {
6487c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
6497c478bd9Sstevel@tonic-gate 				    cname);
6507c478bd9Sstevel@tonic-gate 				exit(1);
6517c478bd9Sstevel@tonic-gate 			}
652df4628cbSrie 
653df4628cbSrie 			/*
654*9174bfaaSGarrett D'Amore 			 * The pointer "load" has be assigned to load_elf[].
655*9174bfaaSGarrett D'Amore 			 * Use the size of load_elf[].
656df4628cbSrie 			 */
657df4628cbSrie 			load[sizeof (load_elf) - 2] = '2';
6587c478bd9Sstevel@tonic-gate 		} else
659df4628cbSrie 			load[sizeof (load_elf) - 2] = '1';
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * Establish new environment variables to affect the child
6647c478bd9Sstevel@tonic-gate 		 * process.
6657c478bd9Sstevel@tonic-gate 		 */
6667c478bd9Sstevel@tonic-gate 		if ((putenv(warn) != 0) || (putenv(bind) != 0) ||
6677c478bd9Sstevel@tonic-gate 		    (putenv(path) != 0) || (putenv(verb) != 0) ||
6687c478bd9Sstevel@tonic-gate 		    (putenv(fltr) != 0) || (putenv(conf) != 0) ||
6697c478bd9Sstevel@tonic-gate 		    (putenv(init) != 0) || (putenv(lazy) != 0) ||
6707c478bd9Sstevel@tonic-gate 		    (putenv(uref) != 0) || (putenv(used) != 0) ||
671dae2dfb7Srie 		    (putenv(weak) != 0) || (putenv(load) != 0) ||
672f441771bSRod Evans 		    (putenv(nope) != 0) || (putenv(defr) != 0)) {
6737c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), cname);
6747c478bd9Sstevel@tonic-gate 			exit(1);
6757c478bd9Sstevel@tonic-gate 		}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		/*
6787c478bd9Sstevel@tonic-gate 		 * Establish explicit environment requires (but don't override
6797c478bd9Sstevel@tonic-gate 		 * any preload request established to process a shared object).
6807c478bd9Sstevel@tonic-gate 		 */
6817c478bd9Sstevel@tonic-gate 		size = 0;
68257ef7aa9SRod Evans 		for (APLIST_TRAVERSE(eopts, idx, str)) {
6837c478bd9Sstevel@tonic-gate 			if (preload) {
6847c478bd9Sstevel@tonic-gate 				if (size == 0)
6857c478bd9Sstevel@tonic-gate 					size = strlen(preload);
6867c478bd9Sstevel@tonic-gate 				if ((strncmp(preload, str, size) == 0) &&
6877c478bd9Sstevel@tonic-gate 				    (str[size] == '=')) {
6887c478bd9Sstevel@tonic-gate 					continue;
6897c478bd9Sstevel@tonic-gate 				}
6907c478bd9Sstevel@tonic-gate 			}
6917c478bd9Sstevel@tonic-gate 			if (putenv(str) != 0) {
6927c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
6937c478bd9Sstevel@tonic-gate 				    cname);
6947c478bd9Sstevel@tonic-gate 				exit(1);
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		/*
6997c478bd9Sstevel@tonic-gate 		 * Execute the object and let ld.so.1 do the rest.
7007c478bd9Sstevel@tonic-gate 		 */
7017c478bd9Sstevel@tonic-gate 		if (nfile > 1)
7027c478bd9Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_STR_FMT3), fname);
7037c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
7047c478bd9Sstevel@tonic-gate 		if ((execl(ename, ename, (char *)0)) == -1) {
7057c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
7067c478bd9Sstevel@tonic-gate 			    fname);
7077c478bd9Sstevel@tonic-gate 			perror(ename);
7087c478bd9Sstevel@tonic-gate 			_exit(0);
7097c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
7107c478bd9Sstevel@tonic-gate 		}
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 	return (status);
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate const char *
_ldd_msg(Msg mid)7167c478bd9Sstevel@tonic-gate _ldd_msg(Msg mid)
7177c478bd9Sstevel@tonic-gate {
7187c478bd9Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
7197c478bd9Sstevel@tonic-gate }
720