Home | History | Annotate | Line # | Download | only in ldd
ldd_elfxx.c revision 1.5.2.1
      1  1.5.2.1      yamt /*	$NetBSD: ldd_elfxx.c,v 1.5.2.1 2012/10/30 19:00:19 yamt Exp $	*/
      2      1.1       mrg 
      3      1.1       mrg /*-
      4      1.1       mrg  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5      1.1       mrg  * All rights reserved.
      6      1.1       mrg  *
      7      1.1       mrg  * This code is derived from software contributed to The NetBSD Foundation
      8      1.1       mrg  * by Paul Kranenburg.
      9      1.1       mrg  *
     10      1.1       mrg  * Redistribution and use in source and binary forms, with or without
     11      1.1       mrg  * modification, are permitted provided that the following conditions
     12      1.1       mrg  * are met:
     13      1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     14      1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     15      1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     17      1.1       mrg  *    documentation and/or other materials provided with the distribution.
     18      1.1       mrg  *
     19      1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.1       mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.1       mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.1       mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.1       mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.1       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.1       mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.1       mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.1       mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.1       mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.1       mrg  * POSSIBILITY OF SUCH DAMAGE.
     30      1.1       mrg  */
     31      1.1       mrg 
     32      1.1       mrg /*
     33      1.1       mrg  * Copyright 1996 John D. Polstra.
     34      1.1       mrg  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
     35      1.1       mrg  * All rights reserved.
     36      1.1       mrg  *
     37      1.1       mrg  * Redistribution and use in source and binary forms, with or without
     38      1.1       mrg  * modification, are permitted provided that the following conditions
     39      1.1       mrg  * are met:
     40      1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     41      1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     42      1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     43      1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     44      1.1       mrg  *    documentation and/or other materials provided with the distribution.
     45      1.1       mrg  * 3. All advertising materials mentioning features or use of this software
     46      1.1       mrg  *    must display the following acknowledgement:
     47      1.1       mrg  *      This product includes software developed by John Polstra.
     48      1.1       mrg  * 4. The name of the author may not be used to endorse or promote products
     49      1.1       mrg  *    derived from this software without specific prior written permission.
     50      1.1       mrg  *
     51      1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52      1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53      1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54      1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55      1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56      1.1       mrg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57      1.1       mrg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58      1.1       mrg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59      1.1       mrg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60      1.1       mrg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61      1.1       mrg  */
     62      1.1       mrg 
     63      1.1       mrg #include <sys/cdefs.h>
     64      1.1       mrg #ifndef lint
     65  1.5.2.1      yamt __RCSID("$NetBSD: ldd_elfxx.c,v 1.5.2.1 2012/10/30 19:00:19 yamt Exp $");
     66      1.1       mrg #endif /* not lint */
     67      1.1       mrg 
     68      1.1       mrg #include <sys/types.h>
     69      1.1       mrg #include <sys/mman.h>
     70      1.1       mrg #include <sys/wait.h>
     71      1.1       mrg 
     72      1.1       mrg #include <dirent.h>
     73      1.1       mrg #include <err.h>
     74      1.1       mrg #include <errno.h>
     75      1.1       mrg #include <fcntl.h>
     76      1.1       mrg #include <stdarg.h>
     77      1.1       mrg #include <stdio.h>
     78      1.1       mrg #include <stdlib.h>
     79      1.1       mrg #include <string.h>
     80      1.1       mrg #include <unistd.h>
     81      1.1       mrg #include <ctype.h>
     82      1.1       mrg 
     83      1.1       mrg #include "debug.h"
     84      1.1       mrg #include "rtld.h"
     85      1.1       mrg #include "ldd.h"
     86      1.1       mrg 
     87  1.5.2.1      yamt static void print_needed(Obj_Entry *, const char *, const char *);
     88  1.5.2.1      yamt static void fmtprint(const char *, Obj_Entry *, const char *, const char *);
     89  1.5.2.1      yamt 
     90      1.1       mrg /*
     91      1.5       wiz  * elfxx_ldd() - bit-size independent ELF ldd implementation.
     92      1.1       mrg  * returns 0 on success and -1 on failure.
     93      1.1       mrg  */
     94      1.1       mrg int
     95      1.4  dholland ELFNAME(ldd)(int fd, char *path, const char *fmt1, const char *fmt2)
     96      1.1       mrg {
     97      1.1       mrg 	struct stat st;
     98      1.1       mrg 
     99      1.2       mrg 	if (lseek(fd, 0, SEEK_SET) < 0 ||
    100      1.2       mrg 	    fstat(fd, &st) < 0) {
    101      1.2       mrg 		_rtld_error("%s: %s", path, strerror(errno));
    102      1.1       mrg 		return -1;
    103      1.1       mrg 	}
    104      1.1       mrg 
    105      1.1       mrg 	_rtld_pagesz = sysconf(_SC_PAGESIZE);
    106      1.1       mrg 
    107      1.1       mrg #ifdef RTLD_ARCH_SUBDIR
    108      1.1       mrg 	_rtld_add_paths(path, &_rtld_default_paths,
    109      1.1       mrg 	    RTLD_DEFAULT_LIBRARY_PATH "/" RTLD_ARCH_SUBDIR);
    110      1.1       mrg #endif
    111      1.1       mrg 	_rtld_add_paths(path, &_rtld_default_paths, RTLD_DEFAULT_LIBRARY_PATH);
    112      1.1       mrg 
    113      1.1       mrg 	_rtld_paths = NULL;
    114      1.1       mrg 	_rtld_trust = (st.st_mode & (S_ISUID | S_ISGID)) == 0;
    115      1.1       mrg 	if (_rtld_trust)
    116      1.1       mrg 		_rtld_add_paths(path, &_rtld_paths, getenv("LD_LIBRARY_PATH"));
    117      1.1       mrg 
    118      1.1       mrg 	_rtld_process_hints(path, &_rtld_paths, &_rtld_xforms, _PATH_LD_HINTS);
    119      1.1       mrg 	_rtld_objmain = _rtld_map_object(xstrdup(path), fd, &st);
    120      1.2       mrg 	if (_rtld_objmain == NULL)
    121      1.1       mrg 		return -1;
    122      1.1       mrg 
    123      1.1       mrg 	_rtld_objmain->path = xstrdup(path);
    124      1.1       mrg 	_rtld_digest_dynamic(path, _rtld_objmain);
    125      1.1       mrg 
    126      1.1       mrg 	/* Link the main program into the list of objects. */
    127      1.1       mrg 	*_rtld_objtail = _rtld_objmain;
    128      1.1       mrg 	_rtld_objtail = &_rtld_objmain->next;
    129      1.1       mrg 	++_rtld_objmain->refcount;
    130      1.1       mrg 
    131      1.1       mrg 	(void) _rtld_load_needed_objects(_rtld_objmain, 0);
    132      1.1       mrg 
    133      1.1       mrg 	if (fmt1 == NULL)
    134      1.1       mrg 		printf("%s:\n", _rtld_objmain->path);
    135      1.1       mrg 	main_local = path;
    136      1.1       mrg 	main_progname = _rtld_objmain->path;
    137      1.1       mrg 	print_needed(_rtld_objmain, fmt1, fmt2);
    138      1.1       mrg 
    139      1.1       mrg 	while (_rtld_objlist != NULL) {
    140      1.1       mrg 		Obj_Entry *obj = _rtld_objlist;
    141      1.1       mrg 		_rtld_objlist = obj->next;
    142      1.1       mrg 		while (obj->rpaths != NULL) {
    143      1.1       mrg 			const Search_Path *rpath = obj->rpaths;
    144      1.1       mrg 			obj->rpaths = rpath->sp_next;
    145      1.1       mrg 			xfree(__UNCONST(rpath->sp_path));
    146      1.1       mrg 			xfree(__UNCONST(rpath));
    147      1.1       mrg 		}
    148      1.1       mrg 		while (obj->needed != NULL) {
    149      1.1       mrg 			const Needed_Entry *needed = obj->needed;
    150      1.1       mrg 			obj->needed = needed->next;
    151      1.1       mrg 			xfree(__UNCONST(needed));
    152      1.1       mrg 		}
    153      1.1       mrg 		(void) munmap(obj->mapbase, obj->mapsize);
    154      1.1       mrg 		xfree(obj->path);
    155      1.1       mrg 		xfree(obj);
    156      1.1       mrg 	}
    157      1.1       mrg 
    158      1.1       mrg 	_rtld_objmain = NULL;
    159      1.1       mrg 	_rtld_objtail = &_rtld_objlist;
    160      1.1       mrg 	/* Need to free _rtld_paths? */
    161      1.1       mrg 
    162      1.1       mrg 	return 0;
    163      1.1       mrg }
    164  1.5.2.1      yamt 
    165  1.5.2.1      yamt void
    166  1.5.2.1      yamt fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
    167  1.5.2.1      yamt     const char *fmt2)
    168  1.5.2.1      yamt {
    169  1.5.2.1      yamt 	const char *libpath = obj ? obj->path : "not found";
    170  1.5.2.1      yamt 	char libnamebuf[200];
    171  1.5.2.1      yamt 	char *libmajor = NULL;
    172  1.5.2.1      yamt 	const char *fmt;
    173  1.5.2.1      yamt 	char *cp;
    174  1.5.2.1      yamt 	int c;
    175  1.5.2.1      yamt 
    176  1.5.2.1      yamt 	if (strncmp(libname, "lib", 3) == 0 &&
    177  1.5.2.1      yamt 	    (cp = strstr(libname, ".so")) != NULL) {
    178  1.5.2.1      yamt 		size_t i = cp - (libname + 3);
    179  1.5.2.1      yamt 
    180  1.5.2.1      yamt 		if (i >= sizeof(libnamebuf))
    181  1.5.2.1      yamt 			i = sizeof(libnamebuf) - 1;
    182  1.5.2.1      yamt 		(void)memcpy(libnamebuf, libname + 3, i);
    183  1.5.2.1      yamt 		libnamebuf[i] = '\0';
    184  1.5.2.1      yamt 		if (cp[3] && isdigit((unsigned char)cp[4]))
    185  1.5.2.1      yamt 			libmajor = &cp[4];
    186  1.5.2.1      yamt 		libname = libnamebuf;
    187  1.5.2.1      yamt 	}
    188  1.5.2.1      yamt 
    189  1.5.2.1      yamt 	if (fmt1 == NULL)
    190  1.5.2.1      yamt 		fmt1 = libmajor != NULL ?
    191  1.5.2.1      yamt 		    "\t-l%o.%m => %p\n" :
    192  1.5.2.1      yamt 		    "\t-l%o => %p\n";
    193  1.5.2.1      yamt 	if (fmt2 == NULL)
    194  1.5.2.1      yamt 		fmt2 = "\t%o => %p\n";
    195  1.5.2.1      yamt 
    196  1.5.2.1      yamt 	fmt = libname == libnamebuf ? fmt1 : fmt2;
    197  1.5.2.1      yamt 	while ((c = *fmt++) != '\0') {
    198  1.5.2.1      yamt 		switch (c) {
    199  1.5.2.1      yamt 		default:
    200  1.5.2.1      yamt 			putchar(c);
    201  1.5.2.1      yamt 			continue;
    202  1.5.2.1      yamt 		case '\\':
    203  1.5.2.1      yamt 			switch (c = *fmt) {
    204  1.5.2.1      yamt 			case '\0':
    205  1.5.2.1      yamt 				continue;
    206  1.5.2.1      yamt 			case 'n':
    207  1.5.2.1      yamt 				putchar('\n');
    208  1.5.2.1      yamt 				break;
    209  1.5.2.1      yamt 			case 't':
    210  1.5.2.1      yamt 				putchar('\t');
    211  1.5.2.1      yamt 				break;
    212  1.5.2.1      yamt 			}
    213  1.5.2.1      yamt 			break;
    214  1.5.2.1      yamt 		case '%':
    215  1.5.2.1      yamt 			switch (c = *fmt) {
    216  1.5.2.1      yamt 			case '\0':
    217  1.5.2.1      yamt 				continue;
    218  1.5.2.1      yamt 			case '%':
    219  1.5.2.1      yamt 			default:
    220  1.5.2.1      yamt 				putchar(c);
    221  1.5.2.1      yamt 				break;
    222  1.5.2.1      yamt 			case 'A':
    223  1.5.2.1      yamt 				printf("%s", main_local);
    224  1.5.2.1      yamt 				break;
    225  1.5.2.1      yamt 			case 'a':
    226  1.5.2.1      yamt 				printf("%s", main_progname);
    227  1.5.2.1      yamt 				break;
    228  1.5.2.1      yamt 			case 'o':
    229  1.5.2.1      yamt 				printf("%s", libname);
    230  1.5.2.1      yamt 				break;
    231  1.5.2.1      yamt 			case 'm':
    232  1.5.2.1      yamt 				printf("%s", libmajor);
    233  1.5.2.1      yamt 				break;
    234  1.5.2.1      yamt 			case 'n':
    235  1.5.2.1      yamt 				/* XXX: not supported for elf */
    236  1.5.2.1      yamt 				break;
    237  1.5.2.1      yamt 			case 'p':
    238  1.5.2.1      yamt 				printf("%s", libpath);
    239  1.5.2.1      yamt 				break;
    240  1.5.2.1      yamt 			case 'x':
    241  1.5.2.1      yamt 				printf("%p", obj ? obj->mapbase : 0);
    242  1.5.2.1      yamt 				break;
    243  1.5.2.1      yamt 			}
    244  1.5.2.1      yamt 			break;
    245  1.5.2.1      yamt 		}
    246  1.5.2.1      yamt 		++fmt;
    247  1.5.2.1      yamt 	}
    248  1.5.2.1      yamt }
    249  1.5.2.1      yamt 
    250  1.5.2.1      yamt void
    251  1.5.2.1      yamt print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
    252  1.5.2.1      yamt {
    253  1.5.2.1      yamt 	const Needed_Entry *needed;
    254  1.5.2.1      yamt 
    255  1.5.2.1      yamt 	for (needed = obj->needed; needed != NULL; needed = needed->next) {
    256  1.5.2.1      yamt 		const char *libname = obj->strtab + needed->name;
    257  1.5.2.1      yamt 
    258  1.5.2.1      yamt 		if (needed->obj != NULL) {
    259  1.5.2.1      yamt 			if (!needed->obj->printed) {
    260  1.5.2.1      yamt 				fmtprint(libname, needed->obj, fmt1, fmt2);
    261  1.5.2.1      yamt 				needed->obj->printed = 1;
    262  1.5.2.1      yamt 				print_needed(needed->obj, fmt1, fmt2);
    263  1.5.2.1      yamt 			}
    264  1.5.2.1      yamt 		} else {
    265  1.5.2.1      yamt 			fmtprint(libname, needed->obj, fmt1, fmt2);
    266  1.5.2.1      yamt 		}
    267  1.5.2.1      yamt 	}
    268  1.5.2.1      yamt }
    269