Home | History | Annotate | Line # | Download | only in ldd
ldd_elfxx.c revision 1.5.2.1
      1 /*	$NetBSD: ldd_elfxx.c,v 1.5.2.1 2012/10/30 19:00:19 yamt 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_elfxx.c,v 1.5.2.1 2012/10/30 19:00:19 yamt 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 static void print_needed(Obj_Entry *, const char *, const char *);
     88 static void fmtprint(const char *, Obj_Entry *, const char *, const char *);
     89 
     90 /*
     91  * elfxx_ldd() - bit-size independent ELF ldd implementation.
     92  * returns 0 on success and -1 on failure.
     93  */
     94 int
     95 ELFNAME(ldd)(int fd, char *path, const char *fmt1, const char *fmt2)
     96 {
     97 	struct stat st;
     98 
     99 	if (lseek(fd, 0, SEEK_SET) < 0 ||
    100 	    fstat(fd, &st) < 0) {
    101 		_rtld_error("%s: %s", path, strerror(errno));
    102 		return -1;
    103 	}
    104 
    105 	_rtld_pagesz = sysconf(_SC_PAGESIZE);
    106 
    107 #ifdef RTLD_ARCH_SUBDIR
    108 	_rtld_add_paths(path, &_rtld_default_paths,
    109 	    RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
    110 #endif
    111 	_rtld_add_paths(path, &_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH);
    112 
    113 	_rtld_paths = NULL;
    114 	_rtld_trust = (st.st_mode & (S_ISUID | S_ISGID)) == 0;
    115 	if (_rtld_trust)
    116 		_rtld_add_paths(path, &_rtld_paths, getenv("LD_LIBRARY_PATH"));
    117 
    118 	_rtld_process_hints(path, &_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS);
    119 	_rtld_objmain = _rtld_map_object(xstrdup(path), fd, &st);
    120 	if (_rtld_objmain == NULL)
    121 		return -1;
    122 
    123 	_rtld_objmain->path = xstrdup(path);
    124 	_rtld_digest_dynamic(path, _rtld_objmain);
    125 
    126 	/* Link the main program into the list of objects. */
    127 	*_rtld_objtail = _rtld_objmain;
    128 	_rtld_objtail = &_rtld_objmain->next;
    129 	++_rtld_objmain->refcount;
    130 
    131 	(void) _rtld_load_needed_objects(_rtld_objmain, 0);
    132 
    133 	if (fmt1 == NULL)
    134 		printf("%s:\n", _rtld_objmain->path);
    135 	main_local = path;
    136 	main_progname = _rtld_objmain->path;
    137 	print_needed(_rtld_objmain, fmt1, fmt2);
    138 
    139 	while (_rtld_objlist != NULL) {
    140 		Obj_Entry *obj = _rtld_objlist;
    141 		_rtld_objlist = obj->next;
    142 		while (obj->rpaths != NULL) {
    143 			const Search_Path *rpath = obj->rpaths;
    144 			obj->rpaths = rpath->sp_next;
    145 			xfree(__UNCONST(rpath->sp_path));
    146 			xfree(__UNCONST(rpath));
    147 		}
    148 		while (obj->needed != NULL) {
    149 			const Needed_Entry *needed = obj->needed;
    150 			obj->needed = needed->next;
    151 			xfree(__UNCONST(needed));
    152 		}
    153 		(void) munmap(obj->mapbase, obj->mapsize);
    154 		xfree(obj->path);
    155 		xfree(obj);
    156 	}
    157 
    158 	_rtld_objmain = NULL;
    159 	_rtld_objtail = &_rtld_objlist;
    160 	/* Need to free _rtld_paths? */
    161 
    162 	return 0;
    163 }
    164 
    165 void
    166 fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
    167     const char *fmt2)
    168 {
    169 	const char *libpath = obj ? obj->path : "not found";
    170 	char libnamebuf[200];
    171 	char *libmajor = NULL;
    172 	const char *fmt;
    173 	char *cp;
    174 	int c;
    175 
    176 	if (strncmp(libname, "lib", 3) == 0 &&
    177 	    (cp = strstr(libname, ".so")) != NULL) {
    178 		size_t i = cp - (libname + 3);
    179 
    180 		if (i >= sizeof(libnamebuf))
    181 			i = sizeof(libnamebuf) - 1;
    182 		(void)memcpy(libnamebuf, libname + 3, i);
    183 		libnamebuf[i] = '\0';
    184 		if (cp[3] && isdigit((unsigned char)cp[4]))
    185 			libmajor = &cp[4];
    186 		libname = libnamebuf;
    187 	}
    188 
    189 	if (fmt1 == NULL)
    190 		fmt1 = libmajor != NULL ?
    191 		    "\t-l%o.%m => %p\n" :
    192 		    "\t-l%o => %p\n";
    193 	if (fmt2 == NULL)
    194 		fmt2 = "\t%o => %p\n";
    195 
    196 	fmt = libname == libnamebuf ? fmt1 : fmt2;
    197 	while ((c = *fmt++) != '\0') {
    198 		switch (c) {
    199 		default:
    200 			putchar(c);
    201 			continue;
    202 		case '\\':
    203 			switch (c = *fmt) {
    204 			case '\0':
    205 				continue;
    206 			case 'n':
    207 				putchar('\n');
    208 				break;
    209 			case 't':
    210 				putchar('\t');
    211 				break;
    212 			}
    213 			break;
    214 		case '%':
    215 			switch (c = *fmt) {
    216 			case '\0':
    217 				continue;
    218 			case '%':
    219 			default:
    220 				putchar(c);
    221 				break;
    222 			case 'A':
    223 				printf("%s", main_local);
    224 				break;
    225 			case 'a':
    226 				printf("%s", main_progname);
    227 				break;
    228 			case 'o':
    229 				printf("%s", libname);
    230 				break;
    231 			case 'm':
    232 				printf("%s", libmajor);
    233 				break;
    234 			case 'n':
    235 				/* XXX: not supported for elf */
    236 				break;
    237 			case 'p':
    238 				printf("%s", libpath);
    239 				break;
    240 			case 'x':
    241 				printf("%p", obj ? obj->mapbase : 0);
    242 				break;
    243 			}
    244 			break;
    245 		}
    246 		++fmt;
    247 	}
    248 }
    249 
    250 void
    251 print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
    252 {
    253 	const Needed_Entry *needed;
    254 
    255 	for (needed = obj->needed; needed != NULL; needed = needed->next) {
    256 		const char *libname = obj->strtab + needed->name;
    257 
    258 		if (needed->obj != NULL) {
    259 			if (!needed->obj->printed) {
    260 				fmtprint(libname, needed->obj, fmt1, fmt2);
    261 				needed->obj->printed = 1;
    262 				print_needed(needed->obj, fmt1, fmt2);
    263 			}
    264 		} else {
    265 			fmtprint(libname, needed->obj, fmt1, fmt2);
    266 		}
    267 	}
    268 }
    269