Merge branch 'vendor/OPENSSL'
[dragonfly.git] / usr.bin / ldd / ldd.c
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  */
32
33 #include <sys/param.h>
34 #include <sys/wait.h>
35
36 #include <machine/elf.h>
37 #include <a.out.h>
38 #include <dlfcn.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 extern void     dump_file(const char *);
46 extern int      error_count;
47
48 void
49 usage(void)
50 {
51         fprintf(stderr, "usage: ldd [-av] [-f format] program ...\n");
52         exit(1);
53 }
54
55 int
56 main(int argc, char **argv)
57 {
58         char            *fmt1 = NULL, *fmt2 = NULL;
59         int             rval;
60         int             c;
61         int             aflag;
62
63         aflag = 0;
64         while ((c = getopt(argc, argv, "af:v")) != -1) {
65                 switch (c) {
66                 case 'a':
67                         aflag++;
68                         break;
69                 case 'f':
70                         if (fmt1) {
71                                 if (fmt2)
72                                         errx(1, "too many formats");
73                                 fmt2 = optarg;
74                         } else
75                                 fmt1 = optarg;
76                         break;
77                 case 'v':
78                         break;
79                 default:
80                         usage();
81                         /* NOTREACHED */
82                 }
83         }
84         argc -= optind;
85         argv += optind;
86
87         if (argc <= 0) {
88                 usage();
89                 /* NOTREACHED */
90         }
91
92         /* ld.so magic */
93         if (setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1) == -1)
94                 err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS=1");
95         if (fmt1) {
96                 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1) == -1)
97                         err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS_FMT1=%s", fmt1);
98         }
99         if (fmt2) {
100                 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1) == -1)
101                         err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS_FMT2=%s", fmt2);
102         }
103
104         rval = 0;
105         for ( ;  argc > 0;  argc--, argv++) {
106                 int     fd;
107                 union {
108                         struct exec aout;
109                         Elf_Ehdr elf;
110                 } hdr;
111                 int     n;
112                 int     status;
113                 int     file_ok;
114                 int     is_shlib;
115
116                 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
117                         warn("%s", *argv);
118                         rval |= 1;
119                         continue;
120                 }
121                 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
122                         warn("%s: can't read program header", *argv);
123                         (void)close(fd);
124                         rval |= 1;
125                         continue;
126                 }
127
128                 file_ok = 1;
129                 is_shlib = 0;
130                 if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) {
131                         /* a.out file */
132                         if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
133 #if 1 /* Compatibility */
134                             || hdr.aout.a_entry < __LDPGSZ
135 #endif
136                                 ) {
137                                 warnx("%s: not a dynamic executable", *argv);
138                                 file_ok = 0;
139                         }
140                 } else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
141                         Elf_Ehdr ehdr;
142                         Elf_Phdr phdr;
143                         int dynamic = 0, i;
144
145                         if (lseek(fd, 0, SEEK_SET) == -1 ||
146                             read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
147                             lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
148                            ) {
149                                 warnx("%s: can't read program header", *argv);
150                                 file_ok = 0;
151                         } else {
152                                 for (i = 0; i < ehdr.e_phnum; i++) {
153                                         if (read(fd, &phdr, ehdr.e_phentsize)
154                                            != sizeof phdr) {
155                                                 warnx("%s: can't read program header",
156                                                     *argv);
157                                                 file_ok = 0;
158                                                 break;
159                                         }
160                                         if (phdr.p_type == PT_DYNAMIC)
161                                                 dynamic = 1;
162                                 }
163                         }
164                         if (!dynamic) {
165                                 warnx("%s: not a dynamic executable", *argv);
166                                 file_ok = 0;
167                         } else if (hdr.elf.e_type == ET_DYN) {
168                                 is_shlib = 1;
169                         }
170                 } else {
171                         warnx("%s: not a dynamic executable", *argv);
172                         file_ok = 0;
173                 }
174                 (void)close(fd);
175                 if (!file_ok) {
176                         rval |= 1;
177                         continue;
178                 }
179
180                 if (setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1) == -1)
181                         err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS_PROGNAME=%s", *argv);
182                 if (aflag) setenv("LD_TRACE_LOADED_OBJECTS_ALL", "1", 1);
183                 else if (fmt1 == NULL && fmt2 == NULL)
184                         /* Default formats */
185                         printf("%s:\n", *argv);
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                         if (is_shlib == 0) {
208                                 execl(*argv, *argv, NULL);
209                                 warn("%s", *argv);
210                         } else {
211                                 dlopen(*argv, RTLD_TRACE);
212                                 warnx("%s: %s", *argv, dlerror());
213                         }
214                         _exit(1);
215                 }
216         }
217
218         return rval;
219 }