ldd.c revision 69827
1/*
2 * Copyright (c) 1993 Paul Kranenburg
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by Paul Kranenburg.
16 * 4. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef lint
32static const char rcsid[] =
33  "$FreeBSD: head/usr.bin/ldd/ldd.c 69827 2000-12-10 20:54:13Z charnier $";
34#endif /* not lint */
35
36#include <sys/wait.h>
37#include <a.out.h>
38#include <elf.h>
39#include <err.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <unistd.h>
44
45extern void	dump_file __P((const char *));
46extern int	error_count;
47
48void
49usage()
50{
51	fprintf(stderr, "usage: ldd [-v] [-f format] program ...\n");
52	exit(1);
53}
54
55int
56main(argc, argv)
57int	argc;
58char	*argv[];
59{
60	char		*fmt1 = NULL, *fmt2 = NULL;
61	int		rval;
62	int		c;
63	int		vflag = 0;
64
65	while ((c = getopt(argc, argv, "vf:")) != -1) {
66		switch (c) {
67		case 'v':
68			vflag++;
69			break;
70		case 'f':
71			if (fmt1) {
72				if (fmt2)
73					errx(1, "too many formats");
74				fmt2 = optarg;
75			} else
76				fmt1 = optarg;
77			break;
78		default:
79			usage();
80			/*NOTREACHED*/
81		}
82	}
83	argc -= optind;
84	argv += optind;
85
86	if (vflag && fmt1)
87		errx(1, "-v may not be used with -f");
88
89	if (argc <= 0) {
90		usage();
91		/*NOTREACHED*/
92	}
93
94#ifdef __i386__
95	if (vflag) {
96		for (c = 0; c < argc; c++)
97			dump_file(argv[c]);
98		exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
99	}
100#endif
101
102	/* ld.so magic */
103	setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
104	if (fmt1)
105		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
106	if (fmt2)
107		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
108
109	rval = 0;
110	for ( ;  argc > 0;  argc--, argv++) {
111		int	fd;
112		union {
113			struct exec aout;
114			Elf_Ehdr elf;
115		} hdr;
116		int	n;
117		int	status;
118		int	file_ok;
119
120		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
121			warn("%s", *argv);
122			rval |= 1;
123			continue;
124		}
125		if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
126			warn("%s: can't read program header", *argv);
127			(void)close(fd);
128			rval |= 1;
129			continue;
130		}
131
132		file_ok = 1;
133		if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
134			/* a.out file */
135			if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
136#if 1 /* Compatibility */
137			    || hdr.aout.a_entry < __LDPGSZ
138#endif
139				) {
140				warnx("%s: not a dynamic executable", *argv);
141				file_ok = 0;
142			}
143		} else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
144			Elf_Ehdr ehdr;
145			Elf_Phdr phdr;
146			int dynamic = 0, i;
147
148			lseek(fd, 0, SEEK_SET);
149			if (read(fd, &ehdr, sizeof ehdr) != sizeof ehdr) {
150				warnx("%s: can't read program header", *argv);
151				file_ok = 0;
152			}
153			lseek(fd, 0, ehdr.e_phoff);
154			for (i = 0; i < ehdr.e_phnum; i++) {
155				if (read(fd, &phdr, ehdr.e_phentsize)
156				   != sizeof phdr) {
157					warnx("%s: can't read program header",
158					    *argv);
159					file_ok = 0;
160				}
161				if (phdr.p_type == PT_DYNAMIC)
162					dynamic = 1;
163			}
164			if (!dynamic) {
165				warnx("%s: not a dynamic executable", *argv);
166				file_ok = 0;
167			}
168		} else {
169			warnx("%s: not a dynamic executable", *argv);
170			file_ok = 0;
171		}
172		(void)close(fd);
173		if (!file_ok) {
174			rval |= 1;
175			continue;
176		}
177
178		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
179		if (fmt1 == NULL && fmt2 == NULL)
180			/* Default formats */
181			printf("%s:\n", *argv);
182
183		fflush(stdout);
184
185		switch (fork()) {
186		case -1:
187			err(1, "fork");
188			break;
189		default:
190			if (wait(&status) <= 0) {
191				warn("wait");
192				rval |= 1;
193			} else if (WIFSIGNALED(status)) {
194				fprintf(stderr, "%s: signal %d\n",
195						*argv, WTERMSIG(status));
196				rval |= 1;
197			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
198				fprintf(stderr, "%s: exit status %d\n",
199						*argv, WEXITSTATUS(status));
200				rval |= 1;
201			}
202			break;
203		case 0:
204			rval |= execl(*argv, *argv, NULL) != 0;
205			warn("%s", *argv);
206			_exit(1);
207		}
208	}
209
210	return rval;
211}
212