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