xref: /illumos-gate/usr/src/cmd/sgs/ldd/common/ldd.c (revision 9174bfaa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  *	Copyright (c) 1988 AT&T
22  *	  All Rights Reserved
23  *
24  *
25  * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
26  */
27 
28 /*
29  * Print the list of shared objects required by a dynamic executable or shared
30  * object.
31  *
32  * usage is: ldd [-d | -r] [-c] [-D] [-e envar] [-i] [-f] [-L] [-l] [-p] [-s]
33  *		[-U | -u] [-v] [-w] file(s)
34  *
35  * ldd opens the file and verifies the information in the elf header.
36  * If the file is a dynamic executable, we set up some environment variables
37  * and exec(2) the file.  If the file is a shared object, we preload the
38  * file with a dynamic executable stub. The runtime linker (ld.so.1) actually
39  * provides the diagnostic output, according to the environment variables set.
40  *
41  * If neither -d nor -r is specified, we set only LD_TRACE_LOADED_OBJECTS_E.
42  * The runtime linker will print the pathnames of all dynamic objects it
43  * loads, and then exit.
44  *
45  * If -d or -r is specified, we also set LD_WARN=1; the runtime linker will
46  * perform its normal relocations and issue warning messages for unresolved
47  * references. It will then exit.
48  * If -r is specified, we set LD_BIND_NOW=1, so that the runtime linker
49  * will perform all relocations, otherwise (under -d) the runtime linker
50  * will not perform PLT (function) type relocations.
51  *
52  * If -c is specified we also set LD_NOCONFIG=1, thus disabling any
53  * configuration file use.
54  *
55  * If -D is specified we skip deferred dependency processing.  By default,
56  * ldd loads all deferred dependencies.  However, during normal process
57  * execution, deferred dependencies are only loaded when an explicit binding
58  * to an individual deferred reference is made.  As no user code is executed
59  * under ldd, explicit references to deferred symbols can't be triggered.
60  *
61  * If -e is specified the associated environment variable is set for the
62  * child process that will produce ldd's diagnostics.
63  *
64  * If -i is specified, we set LD_INIT=1. The order of inititialization
65  * sections to be executed is printed. We also set LD_WARN=1.
66  *
67  * If -f is specified, we will run ldd as root on executables that have
68  * an unsercure runtime linker that does not live under the "/usr/lib"
69  * directory.  By default we will not let this happen.
70  *
71  * If -l is specified it generates a warning for any auxiliary filter not found.
72  * Prior to 2.8 this forced any filters to load (all) their filtees.  This is
73  * now the default, however missing auxiliary filters don't generate any error
74  * diagniostic.  See also -L.
75  *
76  * If -L is specified we revert to lazy loading, thus any filtee or lazy
77  * dependency loading is deferred until relocations cause loading.  Without
78  * this option we set LD_LOADFLTR=1, thus forcing any filters to load (all)
79  * their filtees, and LD_NOLAZYLOAD=1 thus forcing immediate processing of
80  * any lazy loaded dependencies.
81  *
82  * If -s is specified we also set LD_TRACE_SEARCH_PATH=1, thus enabling
83  * the runtime linker to indicate the search algorithm used.
84  *
85  * If -v is specified we also set LD_VERBOSE=1, thus enabling the runtime
86  * linker to indicate all object dependencies (not just the first object
87  * loaded) together with any versioning requirements.
88  *
89  * If -U or -u is specified unused dependencies are detected.  -u causes
90  * LD_UNUSED=1 to be set, which causes dependencies that are unused within the
91  * process to be detected.  -U causes LD_UNREF=1 to be set, which causes
92  * unreferenced objects, and unreferenced cyclic dependencies to be detected.
93  * These options assert that at least -d is set as relocation references are
94  * what determine an objects use.
95  *
96  * If -w is specified, no unresolved weak references are allowed.  -w causes
97  * LD_NOUNRESWEAK=1 to be set.  By default, an unresolved weak reference is
98  * allowed, and a "0" is written to the relocation offset.  The -w option
99  * disables this default.  Any weak references that can not be resolved result
100  * in relocation error messages.  This option has no use without -r or -d.
101  *
102  * If the -p option is specified, no unresolved PARENT or EXTERN references are
103  * allowed.  -p causes LD_NOPAREXT=1 to be set.  By default, PARENT and EXTERN
104  * references, which have been explicitly assigned via a mapfile when a shared
105  * object was built, imply that a caller will provide the symbols, and hence
106  * these are not reported as relocation errors.  Note, the -p option is asserted
107  * by default when either the -r or -d options are used to inspect a dynamic
108  * executable.  This option has no use with a shared object without -r or -d.
109  */
110 #include	<fcntl.h>
111 #include	<stdio.h>
112 #include	<string.h>
113 #include	<_libelf.h>
114 #include	<stdlib.h>
115 #include	<unistd.h>
116 #include	<wait.h>
117 #include	<locale.h>
118 #include	<errno.h>
119 #include	<signal.h>
120 #include	"machdep.h"
121 #include	"sgs.h"
122 #include	"conv.h"
123 #include	"a.out.h"
124 #include	"msg.h"
125 
126 static int	elf_check(int, char *, char *, Elf *, int);
127 static int	run(int, char *, char *, const char *, int);
128 
129 
130 /*
131  * Define all environment variable strings.  The character following the "="
132  * will be written to, to disable or enable the associated feature.
133  */
134 static char	bind[] =	"LD_BIND_NOW= ",
135 		load_elf[] =	"LD_TRACE_LOADED_OBJECTS_E= ",
136 		path[] =	"LD_TRACE_SEARCH_PATHS= ",
137 		verb[] =	"LD_VERBOSE= ",
138 		warn[] =	"LD_WARN= ",
139 		conf[] =	"LD_NOCONFIG= ",
140 		fltr[] =	"LD_LOADFLTR= ",
141 		lazy[] =	"LD_NOLAZYLOAD=1",
142 		init[] =	"LD_INIT= ",
143 		uref[] =	"LD_UNREF= ",
144 		used[] =	"LD_UNUSED= ",
145 		weak[] =	"LD_NOUNRESWEAK= ",
146 		nope[] =	"LD_NOPAREXT= ",
147 		defr[] =	"LD_DEFERRED= ";
148 static char	*load;
149 
150 static const char	*prefile_32, *prefile_64, *prefile;
151 static APlist		*eopts = NULL;
152 
153 int
main(int argc,char ** argv,char ** envp)154 main(int argc, char **argv, char **envp)
155 {
156 	char	*str, *cname = argv[0];
157 
158 	Elf	*elf;
159 	int	cflag = 0, dflag = 0, fflag = 0, iflag = 0, Lflag = 0;
160 	int	lflag = 0, rflag = 0, sflag = 0, Uflag = 0, uflag = 0;
161 	int	Dflag = 0, pflag = 0, vflag = 0, wflag = 0;
162 	int	nfile, var, error = 0;
163 	Aliste	idx;
164 
165 	/*
166 	 * If we're on a 64-bit kernel, try to exec a full 64-bit version of
167 	 * the binary.  If successful, conv_check_native() won't return.
168 	 *
169 	 * This is done to ensure that ldd can handle objects >2GB.
170 	 * ldd uses libelf, which is not large file capable. The
171 	 * 64-bit ldd can handle any sized object.
172 	 */
173 	(void) conv_check_native(argv, envp);
174 
175 	/*
176 	 * Establish locale.
177 	 */
178 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
179 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
180 
181 	/*
182 	 * verify command line syntax and process arguments
183 	 */
184 	opterr = 0;				/* disable getopt error mesg */
185 
186 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_GETOPT))) != EOF) {
187 		switch (var) {
188 		case 'c' :			/* enable config search */
189 			cflag = 1;
190 			break;
191 		case 'D' :			/* skip deferred dependencies */
192 			Dflag = 1;
193 			break;
194 		case 'd' :			/* perform data relocations */
195 			dflag = 1;
196 			if (rflag)
197 				error++;
198 			break;
199 		case 'e' :
200 			if (aplist_append(&eopts, optarg, 10) == NULL) {
201 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
202 				    cname);
203 				exit(1);
204 			}
205 			break;
206 		case 'f' :
207 			fflag = 1;
208 			break;
209 		case 'L' :
210 			Lflag = 1;
211 			break;
212 		case 'l' :
213 			lflag = 1;
214 			break;
215 		case 'i' :			/* print the order of .init */
216 			iflag = 1;
217 			break;
218 		case 'p' :
219 			pflag = 1;		/* expose unreferenced */
220 			break;			/*	parent or externals */
221 		case 'r' :			/* perform all relocations */
222 			rflag = 1;
223 			if (dflag)
224 				error++;
225 			break;
226 		case 's' :			/* enable search path output */
227 			sflag = 1;
228 			break;
229 		case 'U' :			/* list unreferenced */
230 			Uflag = 1;		/*	dependencies */
231 			if (uflag)
232 				error++;
233 			break;
234 		case 'u' :			/* list unused dependencies */
235 			uflag = 1;
236 			if (Uflag)
237 				error++;
238 			break;
239 		case 'v' :			/* enable verbose output */
240 			vflag = 1;
241 			break;
242 		case 'w' :			/* expose unresolved weak */
243 			wflag = 1;		/*	references */
244 			break;
245 		default :
246 			error++;
247 			break;
248 		}
249 		if (error)
250 			break;
251 	}
252 	if (error) {
253 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), cname);
254 		exit(1);
255 	}
256 
257 	/*
258 	 * Determine if any of the LD_PRELOAD family is already set in the
259 	 * environment, if so we'll continue to analyze each object with the
260 	 * appropriate setting.
261 	 */
262 	if (((prefile_32 = getenv(MSG_ORIG(MSG_LD_PRELOAD_32))) == NULL) ||
263 	    (*prefile_32 == '\0')) {
264 		prefile_32 = MSG_ORIG(MSG_STR_EMPTY);
265 	}
266 	if (((prefile_64 = getenv(MSG_ORIG(MSG_LD_PRELOAD_64))) == NULL) ||
267 	    (*prefile_64 == '\0')) {
268 		prefile_64 = MSG_ORIG(MSG_STR_EMPTY);
269 	}
270 	if (((prefile = getenv(MSG_ORIG(MSG_LD_PRELOAD))) == NULL) ||
271 	    (*prefile == '\0')) {
272 		prefile = MSG_ORIG(MSG_STR_EMPTY);
273 	}
274 
275 	/*
276 	 * Determine if any environment requests are for the LD_PRELOAD family,
277 	 * and if so override any environment settings we've established above.
278 	 */
279 	for (APLIST_TRAVERSE(eopts, idx, str)) {
280 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_32),
281 		    MSG_LD_PRELOAD_32_SIZE)) == 0) {
282 			str += MSG_LD_PRELOAD_32_SIZE;
283 			if ((*str++ == '=') && (*str != '\0'))
284 				prefile_32 = str;
285 			continue;
286 		}
287 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD_64),
288 		    MSG_LD_PRELOAD_64_SIZE)) == 0) {
289 			str += MSG_LD_PRELOAD_64_SIZE;
290 			if ((*str++ == '=') && (*str != '\0'))
291 				prefile_64 = str;
292 			continue;
293 		}
294 		if ((strncmp(str, MSG_ORIG(MSG_LD_PRELOAD),
295 		    MSG_LD_PRELOAD_SIZE)) == 0) {
296 			str += MSG_LD_PRELOAD_SIZE;
297 			if ((*str++ == '=') && (*str != '\0'))
298 				prefile = str;
299 			continue;
300 		}
301 	}
302 
303 	/*
304 	 * Set the appropriate relocation environment variables (Note unsetting
305 	 * the environment variables is done just in case the user already
306 	 * has these in their environment ... sort of thing the test folks
307 	 * would do :-)
308 	 */
309 	warn[sizeof (warn) - 2] = (dflag || rflag || Uflag || uflag) ? '1' :
310 	    '\0';
311 	bind[sizeof (bind) - 2] = (rflag) ? '1' : '\0';
312 	path[sizeof (path) - 2] = (sflag) ? '1' : '\0';
313 	verb[sizeof (verb) - 2] = (vflag) ? '1' : '\0';
314 	fltr[sizeof (fltr) - 2] = (Lflag) ? '\0' : (lflag) ? '2' : '1';
315 	init[sizeof (init) - 2] = (iflag) ? '1' : '\0';
316 	conf[sizeof (conf) - 2] = (cflag) ? '1' : '\0';
317 	lazy[sizeof (lazy) - 2] = (Lflag) ? '\0' : '1';
318 	uref[sizeof (uref) - 2] = (Uflag) ? '1' : '\0';
319 	used[sizeof (used) - 2] = (uflag) ? '1' : '\0';
320 	weak[sizeof (weak) - 2] = (wflag) ? '1' : '\0';
321 	nope[sizeof (nope) - 2] = (pflag) ? '1' : '\0';
322 	defr[sizeof (defr) - 2] = (Dflag) ? '\0' : '1';
323 
324 	/*
325 	 * coordinate libelf's version information
326 	 */
327 	if (elf_version(EV_CURRENT) == EV_NONE) {
328 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_LIBELF), cname,
329 		    EV_CURRENT);
330 		exit(1);
331 	}
332 
333 	/*
334 	 * Loop through remaining arguments.  Note that from here on there
335 	 * are no exit conditions so that we can process a list of files,
336 	 * any error condition is retained for a final exit status.
337 	 */
338 	nfile = argc - optind;
339 	for (; optind < argc; optind++) {
340 		char	*fname = argv[optind];
341 
342 		/*
343 		 * Open file (do this before checking access so that we can
344 		 * provide the user with better diagnostics).
345 		 */
346 		if ((var = open(fname, O_RDONLY)) == -1) {
347 			int	err = errno;
348 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_OPEN), cname,
349 			    fname, strerror(err));
350 			error = 1;
351 			continue;
352 		}
353 
354 		/*
355 		 * Get the files elf descriptor and process it as an elf file.
356 		 */
357 		elf = elf_begin(var, ELF_C_READ, (Elf *)0);
358 		switch (elf_kind(elf)) {
359 		case ELF_K_AR :
360 			(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO),
361 			    cname, fname);
362 			error = 1;
363 			break;
364 		case ELF_K_ELF:
365 			if (elf_check(nfile, fname, cname, elf, fflag) != 0)
366 				error = 1;
367 			break;
368 		case ELF_K_COFF:
369 		default:
370 			(void) fprintf(stderr, MSG_INTL(MSG_USP_UNKNOWN),
371 			    cname, fname);
372 			error = 1;
373 			break;
374 		}
375 		(void) elf_end(elf);
376 		(void) close(var);
377 	}
378 	return (error);
379 }
380 
381 
382 
383 static int
elf_check(int nfile,char * fname,char * cname,Elf * elf,int fflag)384 elf_check(int nfile, char *fname, char *cname, Elf *elf, int fflag)
385 {
386 	Conv_inv_buf_t	inv_buf;
387 	GElf_Ehdr	ehdr;
388 	GElf_Phdr	phdr;
389 	int		dynamic = 0, interp = 0, cnt, class;
390 
391 	/*
392 	 * verify information in file header
393 	 */
394 	if (gelf_getehdr(elf, &ehdr) == NULL) {
395 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETEHDR),
396 		    cname, fname, elf_errmsg(-1));
397 		return (1);
398 	}
399 
400 	/*
401 	 * Compatible machine
402 	 */
403 	if ((ehdr.e_machine != M_MACH_32) && (ehdr.e_machine != M_MACH_64) &&
404 	    (ehdr.e_machine != M_MACHPLUS)) {
405 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHTYPE), cname, fname,
406 		    conv_ehdr_mach(ehdr.e_machine, 0, &inv_buf));
407 		return (1);
408 	}
409 
410 	/*
411 	 * Compatible encoding (byte order)
412 	 */
413 	if (ehdr.e_ident[EI_DATA] != M_DATA) {
414 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_DATA), cname, fname,
415 		    conv_ehdr_data(ehdr.e_ident[EI_DATA], 0, &inv_buf));
416 		return (1);
417 	}
418 
419 	/*
420 	 * Compatible class
421 	 */
422 	switch (class = ehdr.e_ident[EI_CLASS]) {
423 	case ELFCLASS32:
424 		/*
425 		 * If M_MACH is not the same thing as M_MACHPLUS and this
426 		 * is an M_MACHPLUS object, then the corresponding header
427 		 * flag must be set.
428 		 */
429 		if ((ehdr.e_machine != M_MACH) &&
430 		    ((ehdr.e_flags & M_FLAGSPLUS) == 0)) {
431 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_MACHFLAGS),
432 			    cname, fname);
433 			return (1);
434 		}
435 		break;
436 	case ELFCLASS64:
437 		/* Requires 64-bit kernel */
438 		if (conv_sys_eclass() == ELFCLASS32) {
439 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_KCLASS32),
440 			    cname, fname, conv_ehdr_class(class, 0, &inv_buf));
441 			return (1);
442 		}
443 		break;
444 	default:
445 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_CLASS), cname, fname,
446 		    conv_ehdr_class(class, 0, &inv_buf));
447 		return (1);
448 	}
449 
450 	/*
451 	 * Object type
452 	 */
453 	if ((ehdr.e_type != ET_EXEC) && (ehdr.e_type != ET_DYN) &&
454 	    (ehdr.e_type != ET_REL)) {
455 		(void) fprintf(stderr, MSG_INTL(MSG_ELF_BADMAGIC),
456 		    cname, fname);
457 		return (1);
458 	}
459 
460 	/*
461 	 * Check that the file is executable.  Dynamic executables must be
462 	 * executable to be exec'ed for ldd(1) to function.
463 	 */
464 	if ((access(fname, X_OK) != 0) && (ehdr.e_type == ET_EXEC)) {
465 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOTEXEC),
466 		    cname, fname);
467 		return (1);
468 	}
469 
470 	/*
471 	 * Determine whether we have a dynamic section or interpretor.
472 	 */
473 	for (cnt = 0; cnt < (int)ehdr.e_phnum; cnt++) {
474 		if (dynamic && interp)
475 			break;
476 
477 		if (gelf_getphdr(elf, cnt, &phdr) == NULL) {
478 			(void) fprintf(stderr, MSG_INTL(MSG_ELF_GETPHDR),
479 			    cname, fname, elf_errmsg(-1));
480 			return (1);
481 		}
482 
483 		if (phdr.p_type == PT_DYNAMIC) {
484 			dynamic = 1;
485 			continue;
486 		}
487 
488 		if (phdr.p_type != PT_INTERP)
489 			continue;
490 
491 		interp = 1;
492 
493 		/*
494 		 * If fflag is not set, and euid == root, and the interpreter
495 		 * does not live under /lib, /usr/lib or /etc/lib then don't
496 		 * allow ldd to execute the image.  This prevents someone
497 		 * creating a `trojan horse' by substituting their own
498 		 * interpreter that could preform privileged operations
499 		 * when ldd is against it.
500 		 */
501 		if ((fflag == 0) && (geteuid() == 0) &&
502 		    (strcmp(fname, conv_lddstub(class)) != 0)) {
503 			char	*interpreter;
504 
505 			/*
506 			 * Does the interpreter live under a trusted directory.
507 			 */
508 			interpreter = elf_getident(elf, 0) + phdr.p_offset;
509 
510 			if ((strncmp(interpreter, MSG_ORIG(MSG_PTH_USRLIB),
511 			    MSG_PTH_USRLIB_SIZE) != 0) &&
512 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_LIB),
513 			    MSG_PTH_LIB_SIZE) != 0) &&
514 			    (strncmp(interpreter, MSG_ORIG(MSG_PTH_ETCLIB),
515 			    MSG_PTH_ETCLIB_SIZE) != 0)) {
516 				(void) fprintf(stderr, MSG_INTL(MSG_USP_ELFINS),
517 				    cname, fname, interpreter);
518 				return (1);
519 			}
520 		}
521 	}
522 
523 	/*
524 	 * Catch the case of a static executable (ie, an ET_EXEC that has a set
525 	 * of program headers but no PT_DYNAMIC).
526 	 */
527 	if (ehdr.e_phnum && !dynamic) {
528 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NODYNORSO), cname,
529 		    fname);
530 		return (1);
531 	}
532 
533 	/*
534 	 * If there is a dynamic section, then check for the DF_1_NOHDR
535 	 * flag, and bail if it is present. Such objects are created using
536 	 * a mapfile option (?N in the version 1 syntax, or HDR_NOALLOC
537 	 * otherwise). The ELF header and program headers are
538 	 * not mapped as part of the first segment, and virtual addresses
539 	 * are computed without them. If ldd tries to interpret such
540 	 * a file, it will become confused and generate bad output or
541 	 * crash. Such objects are always special purpose files (like an OS
542 	 * kernel) --- files for which the ldd operation doesn't make sense.
543 	 */
544 	if (dynamic && (_gelf_getdyndtflags_1(elf) & DF_1_NOHDR)) {
545 		(void) fprintf(stderr, MSG_INTL(MSG_USP_NOHDR), cname,
546 		    fname);
547 		return (1);
548 	}
549 
550 	load = load_elf;
551 
552 	/*
553 	 * Run the required program (shared and relocatable objects require the
554 	 * use of lddstub).
555 	 */
556 	if ((ehdr.e_type == ET_EXEC) && interp)
557 		return (run(nfile, cname, fname, (const char *)fname, class));
558 	else
559 		return (run(nfile, cname, fname, conv_lddstub(class), class));
560 }
561 
562 
563 /*
564  * Run the required program, setting the preload and trace environment
565  * variables accordingly.
566  */
567 static int
run(int nfile,char * cname,char * fname,const char * ename,int class)568 run(int nfile, char *cname, char *fname, const char *ename, int class)
569 {
570 	const char	*preload = 0;
571 	int		pid, status;
572 
573 	if ((pid = fork()) == -1) {
574 		int	err = errno;
575 		(void) fprintf(stderr, MSG_INTL(MSG_SYS_FORK), cname,
576 		    strerror(err));
577 		return (1);
578 	}
579 
580 	if (pid) {				/* parent */
581 		while (wait(&status) != pid)
582 			;
583 		if (WIFSIGNALED(status) && ((WSIGMASK & status) != SIGPIPE)) {
584 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
585 			    fname);
586 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_SIG),
587 			    (WSIGMASK & status), ((status & WCOREFLG) ?
588 			    MSG_INTL(MSG_SYS_EXEC_CORE) :
589 			    MSG_ORIG(MSG_STR_EMPTY)));
590 			status = 1;
591 		} else if (WHIBYTE(status)) {
592 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
593 			    fname);
594 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC_STAT),
595 			    WHIBYTE(status));
596 			status = 1;
597 		}
598 	} else {				/* child */
599 		Aliste	idx;
600 		char	*str;
601 		size_t	size;
602 
603 		/*
604 		 * When using ldd(1) to analyze a shared object we preload the
605 		 * shared object with lddstub.  Any additional preload
606 		 * requirements are added after the object being analyzed, this
607 		 * allows us to skip the first object but produce diagnostics
608 		 * for each other preloaded object.
609 		 */
610 		if (fname != ename) {
611 			char		*str;
612 			const char	*files = prefile;
613 			const char	*format = MSG_ORIG(MSG_STR_FMT1);
614 
615 			for (str = fname; *str; str++)
616 				if (*str == '/') {
617 					format = MSG_ORIG(MSG_STR_FMT2);
618 					break;
619 			}
620 
621 			preload = MSG_ORIG(MSG_LD_PRELOAD);
622 
623 			/*
624 			 * Determine which preload files and preload environment
625 			 * variable to use.
626 			 */
627 			if (class == ELFCLASS64) {
628 				if (prefile_64 != MSG_ORIG(MSG_STR_EMPTY)) {
629 					files = prefile_64;
630 					preload = MSG_ORIG(MSG_LD_PRELOAD_64);
631 				}
632 			} else {
633 				if (prefile_32 != MSG_ORIG(MSG_STR_EMPTY)) {
634 					files = prefile_32;
635 					preload = MSG_ORIG(MSG_LD_PRELOAD_32);
636 				}
637 			}
638 
639 			if ((str = (char *)malloc(strlen(preload) +
640 			    strlen(fname) + strlen(files) + 5)) == 0) {
641 				(void) fprintf(stderr, MSG_INTL(MSG_SYS_MALLOC),
642 				    cname);
643 				exit(1);
644 			}
645 
646 			(void) sprintf(str, format, preload, fname, files);
647 			if (putenv(str) != 0) {
648 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
649 				    cname);
650 				exit(1);
651 			}
652 
653 			/*
654 			 * The pointer "load" has be assigned to load_elf[].
655 			 * Use the size of load_elf[].
656 			 */
657 			load[sizeof (load_elf) - 2] = '2';
658 		} else
659 			load[sizeof (load_elf) - 2] = '1';
660 
661 
662 		/*
663 		 * Establish new environment variables to affect the child
664 		 * process.
665 		 */
666 		if ((putenv(warn) != 0) || (putenv(bind) != 0) ||
667 		    (putenv(path) != 0) || (putenv(verb) != 0) ||
668 		    (putenv(fltr) != 0) || (putenv(conf) != 0) ||
669 		    (putenv(init) != 0) || (putenv(lazy) != 0) ||
670 		    (putenv(uref) != 0) || (putenv(used) != 0) ||
671 		    (putenv(weak) != 0) || (putenv(load) != 0) ||
672 		    (putenv(nope) != 0) || (putenv(defr) != 0)) {
673 			(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED), cname);
674 			exit(1);
675 		}
676 
677 		/*
678 		 * Establish explicit environment requires (but don't override
679 		 * any preload request established to process a shared object).
680 		 */
681 		size = 0;
682 		for (APLIST_TRAVERSE(eopts, idx, str)) {
683 			if (preload) {
684 				if (size == 0)
685 					size = strlen(preload);
686 				if ((strncmp(preload, str, size) == 0) &&
687 				    (str[size] == '=')) {
688 					continue;
689 				}
690 			}
691 			if (putenv(str) != 0) {
692 				(void) fprintf(stderr, MSG_INTL(MSG_ENV_FAILED),
693 				    cname);
694 				exit(1);
695 			}
696 		}
697 
698 		/*
699 		 * Execute the object and let ld.so.1 do the rest.
700 		 */
701 		if (nfile > 1)
702 			(void) printf(MSG_ORIG(MSG_STR_FMT3), fname);
703 		(void) fflush(stdout);
704 		if ((execl(ename, ename, (char *)0)) == -1) {
705 			(void) fprintf(stderr, MSG_INTL(MSG_SYS_EXEC), cname,
706 			    fname);
707 			perror(ename);
708 			_exit(0);
709 			/* NOTREACHED */
710 		}
711 	}
712 	return (status);
713 }
714 
715 const char *
_ldd_msg(Msg mid)716 _ldd_msg(Msg mid)
717 {
718 	return (gettext(MSG_ORIG(mid)));
719 }
720