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