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