Initial import from FreeBSD RELENG_4:
[games.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
31 #ifndef lint
32 static const char rcsid[] =
33   "$FreeBSD: src/usr.bin/ldd/ldd.c,v 1.18.2.7 2002/02/27 18:35:53 sobomax Exp $";
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 __P((const char *));
47 extern int      error_count;
48
49 void
50 usage()
51 {
52         fprintf(stderr, "usage: ldd [-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             vflag = 0;
65
66         while ((c = getopt(argc, argv, "vf:")) != -1) {
67                 switch (c) {
68                 case 'v':
69                         vflag++;
70                         break;
71                 case 'f':
72                         if (fmt1) {
73                                 if (fmt2)
74                                         errx(1, "too many formats");
75                                 fmt2 = optarg;
76                         } else
77                                 fmt1 = optarg;
78                         break;
79                 default:
80                         usage();
81                         /*NOTREACHED*/
82                 }
83         }
84         argc -= optind;
85         argv += optind;
86
87         if (vflag && fmt1)
88                 errx(1, "-v may not be used with -f");
89
90         if (argc <= 0) {
91                 usage();
92                 /*NOTREACHED*/
93         }
94
95 #ifdef __i386__
96         if (vflag) {
97                 for (c = 0; c < argc; c++)
98                         dump_file(argv[c]);
99                 exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
100         }
101 #endif
102
103         /* ld.so magic */
104         setenv("LD_TRACE_LOADED_OBJECTS", "1", 1);
105         if (fmt1)
106                 setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
107         if (fmt2)
108                 setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
109
110         rval = 0;
111         for ( ;  argc > 0;  argc--, argv++) {
112                 int     fd;
113                 union {
114                         struct exec aout;
115                         Elf_Ehdr elf;
116                 } hdr;
117                 int     n;
118                 int     status;
119                 int     file_ok;
120                 int     is_shlib;
121
122                 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
123                         warn("%s", *argv);
124                         rval |= 1;
125                         continue;
126                 }
127                 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
128                         warn("%s: can't read program header", *argv);
129                         (void)close(fd);
130                         rval |= 1;
131                         continue;
132                 }
133
134                 file_ok = 1;
135                 is_shlib = 0;
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                         if (lseek(fd, 0, SEEK_SET) == -1 ||
152                             read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
153                             lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
154                            ) {
155                                 warnx("%s: can't read program header", *argv);
156                                 file_ok = 0;
157                         } else {
158                                 for (i = 0; i < ehdr.e_phnum; i++) {
159                                         if (read(fd, &phdr, ehdr.e_phentsize)
160                                            != sizeof phdr) {
161                                                 warnx("%s: can't read program header",
162                                                     *argv);
163                                                 file_ok = 0;
164                                                 break;
165                                         }
166                                         if (phdr.p_type == PT_DYNAMIC)
167                                                 dynamic = 1;
168                                 }
169                         }
170                         if (!dynamic) {
171                                 warnx("%s: not a dynamic executable", *argv);
172                                 file_ok = 0;
173                         } else if (hdr.elf.e_type == ET_DYN) {
174                                 if (hdr.elf.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
175                                         is_shlib = 1;
176                                 } else {
177                                         warnx("%s: not a FreeBSD ELF shared "
178                                               "object", *argv);
179                                         file_ok = 0;
180                                 }
181                         }
182                 } else {
183                         warnx("%s: not a dynamic executable", *argv);
184                         file_ok = 0;
185                 }
186                 (void)close(fd);
187                 if (!file_ok) {
188                         rval |= 1;
189                         continue;
190                 }
191
192                 setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
193                 if (fmt1 == NULL && fmt2 == NULL)
194                         /* Default formats */
195                         printf("%s:\n", *argv);
196
197                 fflush(stdout);
198
199                 switch (fork()) {
200                 case -1:
201                         err(1, "fork");
202                         break;
203                 default:
204                         if (wait(&status) <= 0) {
205                                 warn("wait");
206                                 rval |= 1;
207                         } else if (WIFSIGNALED(status)) {
208                                 fprintf(stderr, "%s: signal %d\n",
209                                                 *argv, WTERMSIG(status));
210                                 rval |= 1;
211                         } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
212                                 fprintf(stderr, "%s: exit status %d\n",
213                                                 *argv, WEXITSTATUS(status));
214                                 rval |= 1;
215                         }
216                         break;
217                 case 0:
218                         if (is_shlib == 0) {
219                                 execl(*argv, *argv, (char *)NULL);
220                                 warn("%s", *argv);
221                         } else {
222                                 dlopen(*argv, RTLD_TRACE);
223                                 warnx("%s: %s", *argv, dlerror());
224                         }
225                         _exit(1);
226                 }
227         }
228
229         return rval;
230 }