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