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