xref: /netbsd-src/usr.bin/ldd/ldd.c (revision de4fa6c51a9708fc05f88b618fa6fad87c9508ec)
1 /*	$NetBSD: ldd.c,v 1.9 2009/08/22 06:52:16 mrg 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@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.9 2009/08/22 06:52:16 mrg Exp $");
66 #endif /* not lint */
67 
68 #include <sys/types.h>
69 #include <sys/mman.h>
70 #include <sys/wait.h>
71 
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>
82 
83 #include "debug.h"
84 #include "rtld.h"
85 #include "ldd.h"
86 
87 /*
88  * Data declarations.
89  */
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;
97 
98 Search_Path *_rtld_default_paths;
99 Search_Path *_rtld_paths;
100 Library_Xform *_rtld_xforms;
101 
102 static void usage(void) __dead;
103 char *main_local;
104 char *main_progname;
105 
106 static void
107 usage(void)
108 {
109 	fprintf(stderr, "Usage: %s [-f <format 1>] [-f <format 2>] <filename>"
110 		" ...\n", getprogname());
111 	exit(1);
112 }
113 
114 int
115 main(int argc, char **argv)
116 {
117 	char *fmt1 = NULL, *fmt2 = NULL;
118 	int c;
119 
120 #ifdef DEBUG
121 	debug = 1;
122 #endif
123 	while ((c = getopt(argc, argv, "f:")) != -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 		default:
134 			usage();
135 			/*NOTREACHED*/
136 		}
137 	}
138 	argc -= optind;
139 	argv += optind;
140 
141 	if (argc <= 0) {
142 		usage();
143 		/*NOTREACHED*/
144 	}
145 
146 	for (; argc != 0; argc--, argv++) {
147 		int fd;
148 
149 		fd = open(*argv, O_RDONLY);
150 		if (fd == -1) {
151 			warn("%s", *argv);
152 			continue;
153 		}
154 		if (elf_ldd(fd, *argv, fmt1, fmt2) == -1
155 		    /* Alpha never had 32 bit support. */
156 #if defined(_LP64) && !defined(__alpha__)
157 		    && elf32_ldd(fd, *argv, fmt1, fmt2) == -1
158 #endif
159 		    )
160 			warnx("%s", error_message);
161 		close(fd);
162 	}
163 
164 	return 0;
165 }
166 
167 /*
168  * Error reporting function.  Use it like printf.  If formats the message
169  * into a buffer, and sets things up so that the next call to dlerror()
170  * will return the message.
171  */
172 void
173 _rtld_error(const char *fmt, ...)
174 {
175 	static char buf[512];
176 	va_list ap;
177 	va_start(ap, fmt);
178 	xvsnprintf(buf, sizeof buf, fmt, ap);
179 	error_message = buf;
180 	va_end(ap);
181 }
182 
183 char *
184 dlerror()
185 {
186 	char *msg = error_message;
187 	error_message = NULL;
188 	return msg;
189 }
190 
191 void
192 fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
193     const char *fmt2)
194 {
195 	const char *libpath = obj ? obj->path : "not found";
196 	char libnamebuf[200];
197 	char *libmajor = NULL;
198 	const char *fmt;
199 	char *cp;
200 	int c;
201 
202 	if (strncmp(libname, "lib", 3) == 0 &&
203 	    (cp = strstr(libname, ".so")) != NULL) {
204 		int i = cp - (libname + 3);
205 
206 		if (i >= sizeof(libnamebuf))
207 			i = sizeof(libnamebuf) - 1;
208 		(void)memcpy(libnamebuf, libname + 3, i);
209 		libnamebuf[i] = '\0';
210 		if (cp[3] && isdigit((unsigned char)cp[4]))
211 			libmajor = &cp[4];
212 		libname = libnamebuf;
213 	}
214 
215 	if (fmt1 == NULL)
216 		fmt1 = libmajor != NULL ?
217 		    "\t-l%o.%m => %p\n" :
218 		    "\t-l%o => %p\n";
219 	if (fmt2 == NULL)
220 		fmt2 = "\t%o => %p\n";
221 
222 	fmt = libname == libnamebuf ? fmt1 : fmt2;
223 	while ((c = *fmt++) != '\0') {
224 		switch (c) {
225 		default:
226 			putchar(c);
227 			continue;
228 		case '\\':
229 			switch (c = *fmt) {
230 			case '\0':
231 				continue;
232 			case 'n':
233 				putchar('\n');
234 				break;
235 			case 't':
236 				putchar('\t');
237 				break;
238 			}
239 			break;
240 		case '%':
241 			switch (c = *fmt) {
242 			case '\0':
243 				continue;
244 			case '%':
245 			default:
246 				putchar(c);
247 				break;
248 			case 'A':
249 				printf("%s", main_local);
250 				break;
251 			case 'a':
252 				printf("%s", main_progname);
253 				break;
254 			case 'o':
255 				printf("%s", libname);
256 				break;
257 			case 'm':
258 				printf("%s", libmajor);
259 				break;
260 			case 'n':
261 				/* XXX: not supported for elf */
262 				break;
263 			case 'p':
264 				printf("%s", libpath);
265 				break;
266 			case 'x':
267 				printf("%p", obj ? obj->mapbase : 0);
268 				break;
269 			}
270 			break;
271 		}
272 		++fmt;
273 	}
274 }
275 
276 void
277 print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
278 {
279 	const Needed_Entry *needed;
280 
281 	for (needed = obj->needed; needed != NULL; needed = needed->next) {
282 		const char *libname = obj->strtab + needed->name;
283 
284 		if (needed->obj != NULL) {
285 			print_needed(needed->obj, fmt1, fmt2);
286 			if (!needed->obj->printed) {
287 				fmtprint(libname, needed->obj, fmt1, fmt2);
288 				needed->obj->printed = 1;
289 			}
290 		} else {
291 			fmtprint(libname, needed->obj, fmt1, fmt2);
292 		}
293 	}
294 }
295