ldd.c revision 76224
1696Spaul/*
2696Spaul * Copyright (c) 1993 Paul Kranenburg
3696Spaul * All rights reserved.
4696Spaul *
5696Spaul * Redistribution and use in source and binary forms, with or without
6696Spaul * modification, are permitted provided that the following conditions
7696Spaul * are met:
8696Spaul * 1. Redistributions of source code must retain the above copyright
9696Spaul *    notice, this list of conditions and the following disclaimer.
10696Spaul * 2. Redistributions in binary form must reproduce the above copyright
11696Spaul *    notice, this list of conditions and the following disclaimer in the
12696Spaul *    documentation and/or other materials provided with the distribution.
13696Spaul * 3. All advertising materials mentioning features or use of this software
14696Spaul *    must display the following acknowledgement:
15696Spaul *      This product includes software developed by Paul Kranenburg.
16696Spaul * 4. The name of the author may not be used to endorse or promote products
171153Sjkh *    derived from this software without specific prior written permission
18696Spaul *
19696Spaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20696Spaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21696Spaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22696Spaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23696Spaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24696Spaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25696Spaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26696Spaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27696Spaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28696Spaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29696Spaul */
30696Spaul
3169827Scharnier#ifndef lint
3269827Scharnierstatic const char rcsid[] =
3369827Scharnier  "$FreeBSD: head/usr.bin/ldd/ldd.c 76224 2001-05-02 23:56:21Z obrien $";
3469827Scharnier#endif /* not lint */
3569827Scharnier
36696Spaul#include <sys/wait.h>
3776224Sobrien#include <machine/elf.h>
38696Spaul#include <a.out.h>
391741Srich#include <err.h>
401741Srich#include <fcntl.h>
411741Srich#include <stdio.h>
421741Srich#include <stdlib.h>
431741Srich#include <unistd.h>
44696Spaul
4529042Sjdpextern void	dump_file __P((const char *));
4618600Speterextern int	error_count;
4718600Speter
48696Spaulvoid
49696Spaulusage()
50696Spaul{
5120050Sbde	fprintf(stderr, "usage: ldd [-v] [-f format] program ...\n");
521741Srich	exit(1);
53696Spaul}
54696Spaul
55696Spaulint
56696Spaulmain(argc, argv)
57696Spaulint	argc;
58696Spaulchar	*argv[];
59696Spaul{
6018598Speter	char		*fmt1 = NULL, *fmt2 = NULL;
611741Srich	int		rval;
62696Spaul	int		c;
6319253Speter	int		vflag = 0;
64696Spaul
6565428Simp	while ((c = getopt(argc, argv, "vf:")) != -1) {
66696Spaul		switch (c) {
6718600Speter		case 'v':
6818600Speter			vflag++;
6918600Speter			break;
7018598Speter		case 'f':
7118598Speter			if (fmt1) {
7218598Speter				if (fmt2)
7369827Scharnier					errx(1, "too many formats");
7418598Speter				fmt2 = optarg;
7518598Speter			} else
7618598Speter				fmt1 = optarg;
7718598Speter			break;
78696Spaul		default:
79696Spaul			usage();
801741Srich			/*NOTREACHED*/
81696Spaul		}
82696Spaul	}
83696Spaul	argc -= optind;
84696Spaul	argv += optind;
85696Spaul
8618600Speter	if (vflag && fmt1)
8718600Speter		errx(1, "-v may not be used with -f");
8818600Speter
89696Spaul	if (argc <= 0) {
90696Spaul		usage();
911741Srich		/*NOTREACHED*/
92696Spaul	}
93696Spaul
9439354Sdfr#ifdef __i386__
9518600Speter	if (vflag) {
9618600Speter		for (c = 0; c < argc; c++)
9718600Speter			dump_file(argv[c]);
9818600Speter		exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
9918600Speter	}
10039354Sdfr#endif
10118600Speter
102696Spaul	/* ld.so magic */
10321576Sjdp	setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
10418598Speter	if (fmt1)
10518598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
10618598Speter	if (fmt2)
10718598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
108696Spaul
1091741Srich	rval = 0;
11038648Sjdp	for ( ;  argc > 0;  argc--, argv++) {
111696Spaul		int	fd;
11238648Sjdp		union {
11338648Sjdp			struct exec aout;
11439354Sdfr			Elf_Ehdr elf;
11538648Sjdp		} hdr;
11638648Sjdp		int	n;
117696Spaul		int	status;
11838648Sjdp		int	file_ok;
119696Spaul
120696Spaul		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
1211741Srich			warn("%s", *argv);
122696Spaul			rval |= 1;
123696Spaul			continue;
124696Spaul		}
12538648Sjdp		if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
12638648Sjdp			warn("%s: can't read program header", *argv);
127696Spaul			(void)close(fd);
128696Spaul			rval |= 1;
129696Spaul			continue;
130696Spaul		}
13135575Sdfr
13238648Sjdp		file_ok = 1;
13338648Sjdp		if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
13435575Sdfr			/* a.out file */
13538648Sjdp			if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
13635575Sdfr#if 1 /* Compatibility */
13738648Sjdp			    || hdr.aout.a_entry < __LDPGSZ
13835575Sdfr#endif
13935575Sdfr				) {
14035575Sdfr				warnx("%s: not a dynamic executable", *argv);
14138648Sjdp				file_ok = 0;
14235575Sdfr			}
14338648Sjdp		} else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
14439354Sdfr			Elf_Ehdr ehdr;
14539354Sdfr			Elf_Phdr phdr;
14635575Sdfr			int dynamic = 0, i;
14735575Sdfr
14870049Sache			if (lseek(fd, 0, SEEK_SET) == -1 ||
14970049Sache			    read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
15070049Sache			    lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
15170049Sache			   ) {
15235575Sdfr				warnx("%s: can't read program header", *argv);
15338648Sjdp				file_ok = 0;
15470049Sache			} else {
15570049Sache				for (i = 0; i < ehdr.e_phnum; i++) {
15670049Sache					if (read(fd, &phdr, ehdr.e_phentsize)
15770049Sache					   != sizeof phdr) {
15870049Sache						warnx("%s: can't read program header",
15970049Sache						    *argv);
16070049Sache						file_ok = 0;
16170049Sache						break;
16270049Sache					}
16370049Sache					if (phdr.p_type == PT_DYNAMIC)
16470049Sache						dynamic = 1;
16535575Sdfr				}
16635575Sdfr			}
16735575Sdfr			if (!dynamic) {
16835575Sdfr				warnx("%s: not a dynamic executable", *argv);
16938648Sjdp				file_ok = 0;
17035575Sdfr			}
17138648Sjdp		} else {
17238648Sjdp			warnx("%s: not a dynamic executable", *argv);
17338648Sjdp			file_ok = 0;
17435575Sdfr		}
175696Spaul		(void)close(fd);
17638648Sjdp		if (!file_ok) {
17738648Sjdp			rval |= 1;
17838648Sjdp			continue;
17938648Sjdp		}
180696Spaul
18118598Speter		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
18218598Speter		if (fmt1 == NULL && fmt2 == NULL)
18318598Speter			/* Default formats */
18418598Speter			printf("%s:\n", *argv);
18518598Speter
1861153Sjkh		fflush(stdout);
187696Spaul
188696Spaul		switch (fork()) {
189696Spaul		case -1:
1901741Srich			err(1, "fork");
191696Spaul			break;
192696Spaul		default:
1931741Srich			if (wait(&status) <= 0) {
1941741Srich				warn("wait");
1951741Srich				rval |= 1;
1961741Srich			} else if (WIFSIGNALED(status)) {
197696Spaul				fprintf(stderr, "%s: signal %d\n",
198696Spaul						*argv, WTERMSIG(status));
199696Spaul				rval |= 1;
200696Spaul			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
201696Spaul				fprintf(stderr, "%s: exit status %d\n",
202696Spaul						*argv, WEXITSTATUS(status));
203696Spaul				rval |= 1;
204696Spaul			}
205696Spaul			break;
206696Spaul		case 0:
2071741Srich			rval |= execl(*argv, *argv, NULL) != 0;
20869827Scharnier			warn("%s", *argv);
209696Spaul			_exit(1);
210696Spaul		}
211696Spaul	}
212696Spaul
213696Spaul	return rval;
214696Spaul}
215