xref: /freebsd/usr.bin/ldd/ldd.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
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
32 static const char rcsid[] =
33   "$FreeBSD$";
34 #endif /* not lint */
35 
36 #include <sys/wait.h>
37 #include <machine/elf.h>
38 #include <a.out.h>
39 #include <dlfcn.h>
40 #include <err.h>
41 #include <fcntl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 
46 extern void	dump_file(const char *);
47 extern int	error_count;
48 
49 void
50 usage()
51 {
52 	fprintf(stderr, "usage: ldd [-a] [-v] [-f format] program ...\n");
53 	exit(1);
54 }
55 
56 int
57 main(argc, argv)
58 int	argc;
59 char	*argv[];
60 {
61 	char		*fmt1 = NULL, *fmt2 = NULL;
62 	int		rval;
63 	int		c;
64 	int		aflag, vflag;
65 
66 	aflag = vflag = 0;
67 
68 	while ((c = getopt(argc, argv, "avf:")) != -1) {
69 		switch (c) {
70 		case 'a':
71 			aflag++;
72 			break;
73 		case 'v':
74 			vflag++;
75 			break;
76 		case 'f':
77 			if (fmt1) {
78 				if (fmt2)
79 					errx(1, "too many formats");
80 				fmt2 = optarg;
81 			} else
82 				fmt1 = optarg;
83 			break;
84 		default:
85 			usage();
86 			/*NOTREACHED*/
87 		}
88 	}
89 	argc -= optind;
90 	argv += optind;
91 
92 	if (vflag && fmt1)
93 		errx(1, "-v may not be used with -f");
94 
95 	if (argc <= 0) {
96 		usage();
97 		/*NOTREACHED*/
98 	}
99 
100 #ifdef __i386__
101 	if (vflag) {
102 		for (c = 0; c < argc; c++)
103 			dump_file(argv[c]);
104 		exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
105 	}
106 #endif
107 
108 	/* ld.so magic */
109 	setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1);
110 	if (fmt1)
111 		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
112 	if (fmt2)
113 		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
114 
115 	rval = 0;
116 	for ( ;  argc > 0;  argc--, argv++) {
117 		int	fd;
118 		union {
119 			struct exec aout;
120 			Elf_Ehdr elf;
121 		} hdr;
122 		int	n;
123 		int	status;
124 		int	file_ok;
125 		int	is_shlib;
126 
127 		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
128 			warn("%s", *argv);
129 			rval |= 1;
130 			continue;
131 		}
132 		if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
133 			warn("%s: can't read program header", *argv);
134 			(void)close(fd);
135 			rval |= 1;
136 			continue;
137 		}
138 
139 		file_ok = 1;
140 		is_shlib = 0;
141 		if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
142 			/* a.out file */
143 			if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
144 #if 1 /* Compatibility */
145 			    || hdr.aout.a_entry < __LDPGSZ
146 #endif
147 				) {
148 				warnx("%s: not a dynamic executable", *argv);
149 				file_ok = 0;
150 			}
151 		} else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
152 			Elf_Ehdr ehdr;
153 			Elf_Phdr phdr;
154 			int dynamic = 0, i;
155 
156 			if (lseek(fd, 0, SEEK_SET) == -1 ||
157 			    read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
158 			    lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
159 			   ) {
160 				warnx("%s: can't read program header", *argv);
161 				file_ok = 0;
162 			} else {
163 				for (i = 0; i < ehdr.e_phnum; i++) {
164 					if (read(fd, &phdr, ehdr.e_phentsize)
165 					   != sizeof phdr) {
166 						warnx("%s: can't read program header",
167 						    *argv);
168 						file_ok = 0;
169 						break;
170 					}
171 					if (phdr.p_type == PT_DYNAMIC)
172 						dynamic = 1;
173 				}
174 			}
175 			if (!dynamic) {
176 				warnx("%s: not a dynamic executable", *argv);
177 				file_ok = 0;
178 			} else if (hdr.elf.e_type == ET_DYN) {
179 				if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
180 					is_shlib = 1;
181 				} else {
182 					warnx("%s: not a FreeBSD ELF shared "
183 					      "object", *argv);
184 					file_ok = 0;
185 				}
186 			}
187 		} else {
188 			warnx("%s: not a dynamic executable", *argv);
189 			file_ok = 0;
190 		}
191 		(void)close(fd);
192 		if (!file_ok) {
193 			rval |= 1;
194 			continue;
195 		}
196 
197 		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
198 		if (aflag) setenv("LD_TRACE_LOADED_OBJECTS_ALL", "1", 1);
199 		else if (fmt1 == NULL && fmt2 == NULL)
200 			/* Default formats */
201 			printf("%s:\n", *argv);
202 
203 		fflush(stdout);
204 
205 		switch (fork()) {
206 		case -1:
207 			err(1, "fork");
208 			break;
209 		default:
210 			if (wait(&status) <= 0) {
211 				warn("wait");
212 				rval |= 1;
213 			} else if (WIFSIGNALED(status)) {
214 				fprintf(stderr, "%s: signal %d\n",
215 						*argv, WTERMSIG(status));
216 				rval |= 1;
217 			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
218 				fprintf(stderr, "%s: exit status %d\n",
219 						*argv, WEXITSTATUS(status));
220 				rval |= 1;
221 			}
222 			break;
223 		case 0:
224 			if (is_shlib == 0) {
225 				execl(*argv, *argv, (char *)NULL);
226 				warn("%s", *argv);
227 			} else {
228 				dlopen(*argv, RTLD_TRACE);
229 				warnx("%s: %s", *argv, dlerror());
230 			}
231 			_exit(1);
232 		}
233 	}
234 
235 	return rval;
236 }
237