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