ldd.c revision 76224
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 76224 2001-05-02 23:56:21Z obrien $";
34#endif /* not lint */
35
36#include <sys/wait.h>
37#include <machine/elf.h>
38#include <a.out.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			if (lseek(fd, 0, SEEK_SET) == -1 ||
149			    read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
150			    lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
151			   ) {
152				warnx("%s: can't read program header", *argv);
153				file_ok = 0;
154			} else {
155				for (i = 0; i < ehdr.e_phnum; i++) {
156					if (read(fd, &phdr, ehdr.e_phentsize)
157					   != sizeof phdr) {
158						warnx("%s: can't read program header",
159						    *argv);
160						file_ok = 0;
161						break;
162					}
163					if (phdr.p_type == PT_DYNAMIC)
164						dynamic = 1;
165				}
166			}
167			if (!dynamic) {
168				warnx("%s: not a dynamic executable", *argv);
169				file_ok = 0;
170			}
171		} else {
172			warnx("%s: not a dynamic executable", *argv);
173			file_ok = 0;
174		}
175		(void)close(fd);
176		if (!file_ok) {
177			rval |= 1;
178			continue;
179		}
180
181		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
182		if (fmt1 == NULL && fmt2 == NULL)
183			/* Default formats */
184			printf("%s:\n", *argv);
185
186		fflush(stdout);
187
188		switch (fork()) {
189		case -1:
190			err(1, "fork");
191			break;
192		default:
193			if (wait(&status) <= 0) {
194				warn("wait");
195				rval |= 1;
196			} else if (WIFSIGNALED(status)) {
197				fprintf(stderr, "%s: signal %d\n",
198						*argv, WTERMSIG(status));
199				rval |= 1;
200			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
201				fprintf(stderr, "%s: exit status %d\n",
202						*argv, WEXITSTATUS(status));
203				rval |= 1;
204			}
205			break;
206		case 0:
207			rval |= execl(*argv, *argv, NULL) != 0;
208			warn("%s", *argv);
209			_exit(1);
210		}
211	}
212
213	return rval;
214}
215