Merge from vendor branch PKGSRC:
[netbsd-mini2440.git] / usr.bin / ldd / ldd.c
blobc52e213981c350e8e5ba07a281c14108c278ea59
1 /* $NetBSD: ldd.c,v 1.9 2009/08/22 06:52:16 mrg Exp $ */
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright 1996 John D. Polstra.
34 * Copyright 1996 Matt Thomas <matt@3am-software.com>
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by John Polstra.
48 * 4. The name of the author may not be used to endorse or promote products
49 * derived from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 #include <sys/cdefs.h>
64 #ifndef lint
65 __RCSID("$NetBSD: ldd.c,v 1.9 2009/08/22 06:52:16 mrg Exp $");
66 #endif /* not lint */
68 #include <sys/types.h>
69 #include <sys/mman.h>
70 #include <sys/wait.h>
72 #include <dirent.h>
73 #include <err.h>
74 #include <errno.h>
75 #include <fcntl.h>
76 #include <stdarg.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <unistd.h>
81 #include <ctype.h>
83 #include "debug.h"
84 #include "rtld.h"
85 #include "ldd.h"
88 * Data declarations.
90 static char *error_message; /* Message for dlopen(), or NULL */
91 bool _rtld_trust; /* False for setuid and setgid programs */
92 Obj_Entry *_rtld_objlist; /* Head of linked list of shared objects */
93 Obj_Entry **_rtld_objtail = &_rtld_objlist;
94 /* Link field of last object in list */
95 Obj_Entry *_rtld_objmain; /* The main program shared object */
96 unsigned int _rtld_pagesz;
98 Search_Path *_rtld_default_paths;
99 Search_Path *_rtld_paths;
100 Library_Xform *_rtld_xforms;
102 static void usage(void) __dead;
103 char *main_local;
104 char *main_progname;
106 static void
107 usage(void)
109 fprintf(stderr, "Usage: %s [-f <format 1>] [-f <format 2>] <filename>"
110 " ...\n", getprogname());
111 exit(1);
115 main(int argc, char **argv)
117 const char *fmt1 = NULL, *fmt2 = NULL;
118 int c;
120 #ifdef DEBUG
121 debug = 1;
122 #endif
123 while ((c = getopt(argc, argv, "f:o")) != -1) {
124 switch (c) {
125 case 'f':
126 if (fmt1) {
127 if (fmt2)
128 errx(1, "Too many formats");
129 fmt2 = optarg;
130 } else
131 fmt1 = optarg;
132 break;
133 case 'o':
134 if (fmt1 || fmt2)
135 errx(1, "Cannot use -o and -f together");
136 fmt1 = "%a:-l%o.%m => %p\n";
137 break;
138 default:
139 usage();
140 /*NOTREACHED*/
143 argc -= optind;
144 argv += optind;
146 if (argc <= 0) {
147 usage();
148 /*NOTREACHED*/
151 for (; argc != 0; argc--, argv++) {
152 int fd;
154 fd = open(*argv, O_RDONLY);
155 if (fd == -1) {
156 warn("%s", *argv);
157 continue;
159 if (elf_ldd(fd, *argv, fmt1, fmt2) == -1
160 /* Alpha never had 32 bit support. */
161 #if defined(_LP64) && !defined(__alpha__)
162 && elf32_ldd(fd, *argv, fmt1, fmt2) == -1
163 #endif
165 warnx("%s", error_message);
166 close(fd);
169 return 0;
173 * Error reporting function. Use it like printf. If formats the message
174 * into a buffer, and sets things up so that the next call to dlerror()
175 * will return the message.
177 void
178 _rtld_error(const char *fmt, ...)
180 static char buf[512];
181 va_list ap;
182 va_start(ap, fmt);
183 xvsnprintf(buf, sizeof buf, fmt, ap);
184 error_message = buf;
185 va_end(ap);
188 char *
189 dlerror()
191 char *msg = error_message;
192 error_message = NULL;
193 return msg;
196 void
197 fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
198 const char *fmt2)
200 const char *libpath = obj ? obj->path : "not found";
201 char libnamebuf[200];
202 char *libmajor = NULL;
203 const char *fmt;
204 char *cp;
205 int c;
207 if (strncmp(libname, "lib", 3) == 0 &&
208 (cp = strstr(libname, ".so")) != NULL) {
209 int i = cp - (libname + 3);
211 if (i >= sizeof(libnamebuf))
212 i = sizeof(libnamebuf) - 1;
213 (void)memcpy(libnamebuf, libname + 3, i);
214 libnamebuf[i] = '\0';
215 if (cp[3] && isdigit((unsigned char)cp[4]))
216 libmajor = &cp[4];
217 libname = libnamebuf;
220 if (fmt1 == NULL)
221 fmt1 = libmajor != NULL ?
222 "\t-l%o.%m => %p\n" :
223 "\t-l%o => %p\n";
224 if (fmt2 == NULL)
225 fmt2 = "\t%o => %p\n";
227 fmt = libname == libnamebuf ? fmt1 : fmt2;
228 while ((c = *fmt++) != '\0') {
229 switch (c) {
230 default:
231 putchar(c);
232 continue;
233 case '\\':
234 switch (c = *fmt) {
235 case '\0':
236 continue;
237 case 'n':
238 putchar('\n');
239 break;
240 case 't':
241 putchar('\t');
242 break;
244 break;
245 case '%':
246 switch (c = *fmt) {
247 case '\0':
248 continue;
249 case '%':
250 default:
251 putchar(c);
252 break;
253 case 'A':
254 printf("%s", main_local);
255 break;
256 case 'a':
257 printf("%s", main_progname);
258 break;
259 case 'o':
260 printf("%s", libname);
261 break;
262 case 'm':
263 printf("%s", libmajor);
264 break;
265 case 'n':
266 /* XXX: not supported for elf */
267 break;
268 case 'p':
269 printf("%s", libpath);
270 break;
271 case 'x':
272 printf("%p", obj ? obj->mapbase : 0);
273 break;
275 break;
277 ++fmt;
281 void
282 print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
284 const Needed_Entry *needed;
286 for (needed = obj->needed; needed != NULL; needed = needed->next) {
287 const char *libname = obj->strtab + needed->name;
289 if (needed->obj != NULL) {
290 print_needed(needed->obj, fmt1, fmt2);
291 if (!needed->obj->printed) {
292 fmtprint(libname, needed->obj, fmt1, fmt2);
293 needed->obj->printed = 1;
295 } else {
296 fmtprint(libname, needed->obj, fmt1, fmt2);