xref: /freebsd/usr.bin/ldd/ldd.c (revision d74e86d9e30043893d6b308468008b65640ddcae)
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  *	$Id: ldd.c,v 1.16 1998/08/30 18:30:59 jdp Exp $
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/file.h>
36 #include <sys/time.h>
37 #include <sys/resource.h>
38 #include <sys/wait.h>
39 #include <a.out.h>
40 #include <elf.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 extern void	dump_file __P((const char *));
49 extern int	error_count;
50 
51 void
52 usage()
53 {
54 	fprintf(stderr, "usage: ldd [-v] [-f format] program ...\n");
55 	exit(1);
56 }
57 
58 int
59 main(argc, argv)
60 int	argc;
61 char	*argv[];
62 {
63 	char		*fmt1 = NULL, *fmt2 = NULL;
64 	int		rval;
65 	int		c;
66 	int		vflag = 0;
67 
68 	while ((c = getopt(argc, argv, "vf:")) != EOF) {
69 		switch (c) {
70 		case 'v':
71 			vflag++;
72 			break;
73 		case 'f':
74 			if (fmt1) {
75 				if (fmt2)
76 					errx(1, "Too many formats");
77 				fmt2 = optarg;
78 			} else
79 				fmt1 = optarg;
80 			break;
81 		default:
82 			usage();
83 			/*NOTREACHED*/
84 		}
85 	}
86 	argc -= optind;
87 	argv += optind;
88 
89 	if (vflag && fmt1)
90 		errx(1, "-v may not be used with -f");
91 
92 	if (argc <= 0) {
93 		usage();
94 		/*NOTREACHED*/
95 	}
96 
97 #ifdef __i386__
98 	if (vflag) {
99 		for (c = 0; c < argc; c++)
100 			dump_file(argv[c]);
101 		exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
102 	}
103 #endif
104 
105 	/* ld.so magic */
106 	setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
107 	if (fmt1)
108 		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
109 	if (fmt2)
110 		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
111 
112 	rval = 0;
113 	for ( ;  argc > 0;  argc--, argv++) {
114 		int	fd;
115 		union {
116 			struct exec aout;
117 			Elf_Ehdr elf;
118 		} hdr;
119 		int	n;
120 		int	status;
121 		int	file_ok;
122 
123 		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
124 			warn("%s", *argv);
125 			rval |= 1;
126 			continue;
127 		}
128 		if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
129 			warn("%s: can't read program header", *argv);
130 			(void)close(fd);
131 			rval |= 1;
132 			continue;
133 		}
134 
135 		file_ok = 1;
136 		if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
137 			/* a.out file */
138 			if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
139 #if 1 /* Compatibility */
140 			    || hdr.aout.a_entry < __LDPGSZ
141 #endif
142 				) {
143 				warnx("%s: not a dynamic executable", *argv);
144 				file_ok = 0;
145 			}
146 		} else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
147 			Elf_Ehdr ehdr;
148 			Elf_Phdr phdr;
149 			int dynamic = 0, i;
150 
151 			lseek(fd, 0, SEEK_SET);
152 			if (read(fd, &ehdr, sizeof ehdr) != sizeof ehdr) {
153 				warnx("%s: can't read program header", *argv);
154 				file_ok = 0;
155 			}
156 			lseek(fd, 0, ehdr.e_phoff);
157 			for (i = 0; i < ehdr.e_phnum; i++) {
158 				if (read(fd, &phdr, ehdr.e_phentsize)
159 				   != sizeof phdr) {
160 					warnx("%s: can't read program header",
161 					    *argv);
162 					file_ok = 0;
163 				}
164 				if (phdr.p_type == PT_DYNAMIC)
165 					dynamic = 1;
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 			perror(*argv);
209 			_exit(1);
210 		}
211 	}
212 
213 	return rval;
214 }
215