Bring in an errno.9 manual page (based on NetBSD's).
[dragonfly.git] / usr.bin / ldd / ldd.c
blob15609cc1b59fd1eba7021c760f1cf7947d099e9b
1 /*
2 * Copyright (c) 1993 Paul Kranenburg
3 * All rights reserved.
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
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.
30 * $FreeBSD: src/usr.bin/ldd/ldd.c,v 1.18.2.7 2002/02/27 18:35:53 sobomax Exp $
33 #include <sys/param.h>
34 #include <sys/wait.h>
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>
45 static void usage(void);
47 static void
48 usage(void)
50 fprintf(stderr, "usage: ldd [-av] [-f format] program ...\n");
51 exit(1);
54 int
55 main(int argc, char **argv)
57 char *fmt1 = NULL, *fmt2 = NULL;
58 int rval;
59 int c;
60 int aflag;
62 aflag = 0;
63 while ((c = getopt(argc, argv, "af:v")) != -1) {
64 switch (c) {
65 case 'a':
66 aflag++;
67 break;
68 case 'f':
69 if (fmt1) {
70 if (fmt2)
71 errx(1, "too many formats");
72 fmt2 = optarg;
73 } else
74 fmt1 = optarg;
75 break;
76 case 'v':
77 break;
78 default:
79 usage();
80 /* NOTREACHED */
83 argc -= optind;
84 argv += optind;
86 if (argc <= 0) {
87 usage();
88 /* NOTREACHED */
91 /* ld.so magic */
92 if (setenv("LD_TRACE_LOADED_OBJECTS", "yes", 1) == -1)
93 err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS=1");
94 if (fmt1) {
95 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1) == -1)
96 err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS_FMT1=%s", fmt1);
98 if (fmt2) {
99 if (setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1) == -1)
100 err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS_FMT2=%s", fmt2);
103 rval = 0;
104 for ( ; argc > 0; argc--, argv++) {
105 int fd;
106 union {
107 Elf_Ehdr elf;
108 } hdr;
109 ssize_t n;
110 int status;
111 int file_ok;
112 int is_shlib;
114 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
115 warn("%s", *argv);
116 rval |= 1;
117 continue;
119 if ((n = read(fd, &hdr, sizeof hdr)) == -1) {
120 warn("%s: can't read program header", *argv);
121 close(fd);
122 rval |= 1;
123 continue;
126 file_ok = 1;
127 is_shlib = 0;
128 if ((size_t)n >= sizeof hdr.elf && IS_ELF(hdr.elf)) {
129 Elf_Ehdr ehdr;
130 Elf_Phdr phdr;
131 int dynamic = 0, i;
133 if (lseek(fd, 0, SEEK_SET) == -1 ||
134 read(fd, &ehdr, sizeof ehdr) != sizeof ehdr ||
135 lseek(fd, ehdr.e_phoff, SEEK_SET) == -1
137 warnx("%s: can't read program header", *argv);
138 file_ok = 0;
139 } else {
140 for (i = 0; i < ehdr.e_phnum; i++) {
141 if (read(fd, &phdr, ehdr.e_phentsize)
142 != sizeof phdr) {
143 warnx("%s: can't read program header",
144 *argv);
145 file_ok = 0;
146 break;
148 if (phdr.p_type == PT_DYNAMIC)
149 dynamic = 1;
152 if (!dynamic) {
153 warnx("%s: not a dynamic executable", *argv);
154 file_ok = 0;
155 } else if (hdr.elf.e_type == ET_DYN) {
156 is_shlib = 1;
158 } else {
159 warnx("%s: not a dynamic executable", *argv);
160 file_ok = 0;
162 close(fd);
163 if (!file_ok) {
164 rval |= 1;
165 continue;
168 if (setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1) == -1)
169 err(1, "setenv: cannot set LD_TRACE_LOADED_OBJECTS_PROGNAME=%s", *argv);
170 if (aflag) setenv("LD_TRACE_LOADED_OBJECTS_ALL", "1", 1);
171 else if (fmt1 == NULL && fmt2 == NULL)
172 /* Default formats */
173 printf("%s:\n", *argv);
174 fflush(stdout);
176 switch (fork()) {
177 case -1:
178 err(1, "fork");
179 break;
180 default:
181 if (wait(&status) <= 0) {
182 warn("wait");
183 rval |= 1;
184 } else if (WIFSIGNALED(status)) {
185 fprintf(stderr, "%s: signal %d\n",
186 *argv, WTERMSIG(status));
187 rval |= 1;
188 } else if (WIFEXITED(status) && WEXITSTATUS(status)) {
189 fprintf(stderr, "%s: exit status %d\n",
190 *argv, WEXITSTATUS(status));
191 rval |= 1;
193 break;
194 case 0:
195 if (is_shlib == 0) {
196 execl(*argv, *argv, NULL);
197 warn("%s", *argv);
198 } else {
199 dlopen(*argv, RTLD_TRACE);
200 warnx("%s: %s", *argv, dlerror());
202 _exit(1);
206 return rval;