ldd.c revision 1.2.12.4 1 /* $NetBSD: ldd.c,v 1.2.12.4 2012/03/17 18:28:31 bouyer Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
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.
18 *
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.
30 */
31
32 /*
33 * Copyright 1996 John D. Polstra.
34 * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
35 * All rights reserved.
36 *
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.
50 *
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.
61 */
62
63 #include <sys/cdefs.h>
64 #ifndef lint
65 __RCSID("$NetBSD: ldd.c,v 1.2.12.4 2012/03/17 18:28:31 bouyer Exp $");
66 #endif /* not lint */
67
68 #include <sys/types.h>
69 #include <sys/mman.h>
70 #include <sys/wait.h>
71
72 #include <a.out.h>
73 #include <dirent.h>
74 #include <err.h>
75 #include <errno.h>
76 #include <fcntl.h>
77 #include <stdarg.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82 #include <ctype.h>
83
84 #include "debug.h"
85 #include "rtld.h"
86 #include "ldd.h"
87
88 /*
89 * Data declarations.
90 */
91 static char *error_message; /* Message for dlopen(), or NULL */
92 bool _rtld_trust; /* False for setuid and setgid programs */
93 Obj_Entry *_rtld_objlist; /* Head of linked list of shared objects */
94 Obj_Entry **_rtld_objtail = &_rtld_objlist;
95 /* Link field of last object in list */
96 u_int _rtld_objcount; /* Number of shared objects */
97 u_int _rtld_objloads; /* Number of objects loaded */
98
99 Obj_Entry *_rtld_objmain; /* The main program shared object */
100 int _rtld_pagesz;
101
102 Search_Path *_rtld_default_paths;
103 Search_Path *_rtld_paths;
104 Library_Xform *_rtld_xforms;
105
106 static void usage(void) __dead;
107 char *main_local;
108 char *main_progname;
109
110 static void
111 usage(void)
112 {
113 fprintf(stderr, "Usage: %s [-f <format 1>] [-f <format 2>] <filename>"
114 " ...\n", getprogname());
115 exit(1);
116 }
117
118 int
119 main(int argc, char **argv)
120 {
121 char *fmt1 = NULL, *fmt2 = NULL;
122 int c;
123
124 #ifdef DEBUG
125 debug = 1;
126 #endif
127 while ((c = getopt(argc, argv, "f:")) != -1) {
128 switch (c) {
129 case 'f':
130 if (fmt1) {
131 if (fmt2)
132 errx(1, "Too many formats");
133 fmt2 = optarg;
134 } else
135 fmt1 = optarg;
136 break;
137 default:
138 usage();
139 /*NOTREACHED*/
140 }
141 }
142 argc -= optind;
143 argv += optind;
144
145 if (argc <= 0) {
146 usage();
147 /*NOTREACHED*/
148 }
149
150 for (; argc != 0; argc--, argv++) {
151 int fd;
152
153 fd = open(*argv, O_RDONLY);
154 if (fd == -1) {
155 warn("%s", *argv);
156 continue;
157 }
158 if (elf_ldd(fd, *argv, fmt1, fmt2) == -1 &&
159 /* Alpha never had 32 bit support. */
160 #if defined(_LP64) && !defined(__alpha__)
161 elf32_ldd(fd, *argv, fmt1, fmt2) == -1 &&
162 #endif
163 aout_ldd(fd, *argv, fmt1, fmt2) == -1)
164 warnx("%s", error_message);
165 close(fd);
166 }
167
168 return 0;
169 }
170
171 /*
172 * Error reporting function. Use it like printf. If formats the message
173 * into a buffer, and sets things up so that the next call to dlerror()
174 * will return the message.
175 */
176 void
177 _rtld_error(const char *fmt, ...)
178 {
179 static char buf[512];
180 va_list ap;
181 va_start(ap, fmt);
182 xvsnprintf(buf, sizeof buf, fmt, ap);
183 error_message = buf;
184 va_end(ap);
185 }
186
187 char *
188 dlerror()
189 {
190 char *msg = error_message;
191 error_message = NULL;
192 return msg;
193 }
194
195 void
196 fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
197 const char *fmt2)
198 {
199 const char *libpath = obj ? obj->path : "not found";
200 char libnamebuf[200];
201 char *libmajor = NULL;
202 const char *fmt;
203 char *cp;
204 int c;
205
206 if (strncmp(libname, "lib", 3) == 0 &&
207 (cp = strstr(libname, ".so")) != NULL) {
208 int i = cp - (libname + 3);
209
210 if (i >= sizeof(libnamebuf))
211 i = sizeof(libnamebuf) - 1;
212 (void)memcpy(libnamebuf, libname + 3, i);
213 libnamebuf[i] = '\0';
214 if (cp[3] && isdigit((unsigned char)cp[4]))
215 libmajor = &cp[4];
216 libname = libnamebuf;
217 }
218
219 if (fmt1 == NULL)
220 fmt1 = libmajor != NULL ?
221 "\t-l%o.%m => %p\n" :
222 "\t-l%o => %p\n";
223 if (fmt2 == NULL)
224 fmt2 = "\t%o => %p\n";
225
226 fmt = libname == libnamebuf ? fmt1 : fmt2;
227 while ((c = *fmt++) != '\0') {
228 switch (c) {
229 default:
230 putchar(c);
231 continue;
232 case '\\':
233 switch (c = *fmt) {
234 case '\0':
235 continue;
236 case 'n':
237 putchar('\n');
238 break;
239 case 't':
240 putchar('\t');
241 break;
242 }
243 break;
244 case '%':
245 switch (c = *fmt) {
246 case '\0':
247 continue;
248 case '%':
249 default:
250 putchar(c);
251 break;
252 case 'A':
253 printf("%s", main_local);
254 break;
255 case 'a':
256 printf("%s", main_progname);
257 break;
258 case 'o':
259 printf("%s", libname);
260 break;
261 case 'm':
262 printf("%s", libmajor);
263 break;
264 case 'n':
265 /* XXX: not supported for elf */
266 break;
267 case 'p':
268 printf("%s", libpath);
269 break;
270 case 'x':
271 printf("%p", obj ? obj->mapbase : 0);
272 break;
273 }
274 break;
275 }
276 ++fmt;
277 }
278 }
279
280 void
281 print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
282 {
283 const Needed_Entry *needed;
284
285 for (needed = obj->needed; needed != NULL; needed = needed->next) {
286 const char *libname = obj->strtab + needed->name;
287
288 if (needed->obj != NULL) {
289 if (!needed->obj->printed) {
290 fmtprint(libname, needed->obj, fmt1, fmt2);
291 needed->obj->printed = 1;
292 print_needed(needed->obj, fmt1, fmt2);
293 }
294 } else {
295 fmtprint(libname, needed->obj, fmt1, fmt2);
296 }
297 }
298 }
299