Home | History | Annotate | Line # | Download | only in ldd
ldd.c revision 1.19
      1  1.19     joerg /*	$NetBSD: ldd.c,v 1.19 2011/05/24 12:27:29 joerg Exp $	*/
      2   1.3       mrg 
      3   1.3       mrg /*-
      4   1.3       mrg  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
      5   1.3       mrg  * All rights reserved.
      6   1.3       mrg  *
      7   1.3       mrg  * This code is derived from software contributed to The NetBSD Foundation
      8   1.3       mrg  * by Paul Kranenburg.
      9   1.3       mrg  *
     10   1.3       mrg  * Redistribution and use in source and binary forms, with or without
     11   1.3       mrg  * modification, are permitted provided that the following conditions
     12   1.3       mrg  * are met:
     13   1.3       mrg  * 1. Redistributions of source code must retain the above copyright
     14   1.3       mrg  *    notice, this list of conditions and the following disclaimer.
     15   1.3       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.3       mrg  *    notice, this list of conditions and the following disclaimer in the
     17   1.3       mrg  *    documentation and/or other materials provided with the distribution.
     18   1.3       mrg  *
     19   1.3       mrg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.3       mrg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.3       mrg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.3       mrg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.3       mrg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.3       mrg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.3       mrg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.3       mrg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.3       mrg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.3       mrg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.3       mrg  * POSSIBILITY OF SUCH DAMAGE.
     30   1.3       mrg  */
     31   1.1       cgd 
     32   1.1       cgd /*
     33   1.1       cgd  * Copyright 1996 John D. Polstra.
     34   1.1       cgd  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
     35   1.1       cgd  * All rights reserved.
     36   1.1       cgd  *
     37   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     38   1.1       cgd  * modification, are permitted provided that the following conditions
     39   1.1       cgd  * are met:
     40   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     41   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     42   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     44   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     45   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     46   1.1       cgd  *    must display the following acknowledgement:
     47   1.1       cgd  *      This product includes software developed by John Polstra.
     48   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     49   1.1       cgd  *    derived from this software without specific prior written permission.
     50   1.1       cgd  *
     51   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     52   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     55   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     56   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     57   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     58   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     59   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     60   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61   1.1       cgd  */
     62   1.1       cgd 
     63   1.3       mrg #include <sys/cdefs.h>
     64   1.3       mrg #ifndef lint
     65  1.19     joerg __RCSID("$NetBSD: ldd.c,v 1.19 2011/05/24 12:27:29 joerg Exp $");
     66   1.3       mrg #endif /* not lint */
     67   1.3       mrg 
     68   1.3       mrg #include <sys/types.h>
     69   1.3       mrg #include <sys/mman.h>
     70   1.3       mrg #include <sys/wait.h>
     71   1.3       mrg 
     72   1.3       mrg #include <dirent.h>
     73   1.1       cgd #include <err.h>
     74   1.1       cgd #include <errno.h>
     75   1.1       cgd #include <fcntl.h>
     76   1.1       cgd #include <stdarg.h>
     77   1.1       cgd #include <stdio.h>
     78   1.1       cgd #include <stdlib.h>
     79   1.1       cgd #include <string.h>
     80   1.1       cgd #include <unistd.h>
     81   1.3       mrg #include <ctype.h>
     82   1.1       cgd 
     83   1.1       cgd #include "debug.h"
     84   1.1       cgd #include "rtld.h"
     85   1.3       mrg #include "ldd.h"
     86   1.1       cgd 
     87   1.1       cgd /*
     88   1.1       cgd  * Data declarations.
     89   1.1       cgd  */
     90   1.3       mrg static char *error_message;	/* Message for dlopen(), or NULL */
     91   1.1       cgd bool _rtld_trust;		/* False for setuid and setgid programs */
     92   1.1       cgd Obj_Entry *_rtld_objlist;	/* Head of linked list of shared objects */
     93   1.1       cgd Obj_Entry **_rtld_objtail = &_rtld_objlist;
     94   1.1       cgd 				/* Link field of last object in list */
     95  1.15     skrll u_int _rtld_objcount;		/* Number of shared objects */
     96  1.15     skrll u_int _rtld_objloads;		/* Number of objects loaded */
     97  1.15     skrll 
     98   1.1       cgd Obj_Entry *_rtld_objmain;	/* The main program shared object */
     99  1.11     skrll size_t _rtld_pagesz;
    100   1.1       cgd 
    101   1.3       mrg Search_Path *_rtld_default_paths;
    102   1.1       cgd Search_Path *_rtld_paths;
    103   1.3       mrg Library_Xform *_rtld_xforms;
    104   1.1       cgd 
    105   1.3       mrg static void usage(void) __dead;
    106   1.3       mrg char *main_local;
    107   1.3       mrg char *main_progname;
    108   1.3       mrg 
    109   1.3       mrg static void
    110   1.3       mrg usage(void)
    111   1.3       mrg {
    112   1.3       mrg 	fprintf(stderr, "Usage: %s [-f <format 1>] [-f <format 2>] <filename>"
    113   1.3       mrg 		" ...\n", getprogname());
    114   1.3       mrg 	exit(1);
    115   1.3       mrg }
    116   1.1       cgd 
    117   1.1       cgd int
    118   1.3       mrg main(int argc, char **argv)
    119   1.1       cgd {
    120  1.10  dholland 	const char *fmt1 = NULL, *fmt2 = NULL;
    121   1.3       mrg 	int c;
    122   1.3       mrg 
    123   1.1       cgd #ifdef DEBUG
    124   1.3       mrg 	debug = 1;
    125   1.1       cgd #endif
    126  1.10  dholland 	while ((c = getopt(argc, argv, "f:o")) != -1) {
    127   1.3       mrg 		switch (c) {
    128   1.3       mrg 		case 'f':
    129   1.3       mrg 			if (fmt1) {
    130   1.3       mrg 				if (fmt2)
    131   1.3       mrg 					errx(1, "Too many formats");
    132   1.3       mrg 				fmt2 = optarg;
    133   1.3       mrg 			} else
    134   1.3       mrg 				fmt1 = optarg;
    135   1.3       mrg 			break;
    136  1.10  dholland 		case 'o':
    137  1.10  dholland 			if (fmt1 || fmt2)
    138  1.10  dholland 				errx(1, "Cannot use -o and -f together");
    139  1.10  dholland 			fmt1 = "%a:-l%o.%m => %p\n";
    140  1.10  dholland 			break;
    141   1.3       mrg 		default:
    142   1.3       mrg 			usage();
    143   1.3       mrg 			/*NOTREACHED*/
    144   1.3       mrg 		}
    145   1.3       mrg 	}
    146   1.3       mrg 	argc -= optind;
    147   1.3       mrg 	argv += optind;
    148   1.1       cgd 
    149   1.3       mrg 	if (argc <= 0) {
    150   1.3       mrg 		usage();
    151   1.3       mrg 		/*NOTREACHED*/
    152   1.1       cgd 	}
    153   1.1       cgd 
    154   1.5       mrg 	for (; argc != 0; argc--, argv++) {
    155   1.5       mrg 		int fd;
    156   1.5       mrg 
    157   1.5       mrg 		fd = open(*argv, O_RDONLY);
    158   1.5       mrg 		if (fd == -1) {
    159   1.5       mrg 			warn("%s", *argv);
    160   1.5       mrg 			continue;
    161   1.5       mrg 		}
    162   1.8        he 		if (elf_ldd(fd, *argv, fmt1, fmt2) == -1
    163   1.4       mrg 		    /* Alpha never had 32 bit support. */
    164   1.4       mrg #if defined(_LP64) && !defined(__alpha__)
    165   1.8        he 		    && elf32_ldd(fd, *argv, fmt1, fmt2) == -1
    166  1.12       mrg #ifdef __mips__
    167  1.12       mrg 		    && elf32_ldd_compat(fd, *argv, fmt1, fmt2) == -1
    168  1.12       mrg #endif
    169   1.3       mrg #endif
    170   1.8        he 		    )
    171   1.3       mrg 			warnx("%s", error_message);
    172   1.5       mrg 		close(fd);
    173   1.5       mrg 	}
    174   1.1       cgd 
    175   1.3       mrg 	return 0;
    176   1.1       cgd }
    177   1.1       cgd 
    178   1.1       cgd /*
    179   1.1       cgd  * Error reporting function.  Use it like printf.  If formats the message
    180   1.1       cgd  * into a buffer, and sets things up so that the next call to dlerror()
    181   1.1       cgd  * will return the message.
    182   1.1       cgd  */
    183   1.1       cgd void
    184   1.3       mrg _rtld_error(const char *fmt, ...)
    185   1.1       cgd {
    186   1.3       mrg 	static char buf[512];
    187   1.3       mrg 	va_list ap;
    188   1.3       mrg 	va_start(ap, fmt);
    189   1.3       mrg 	xvsnprintf(buf, sizeof buf, fmt, ap);
    190   1.3       mrg 	error_message = buf;
    191   1.3       mrg 	va_end(ap);
    192   1.1       cgd }
    193   1.3       mrg 
    194   1.3       mrg char *
    195   1.3       mrg dlerror()
    196   1.1       cgd {
    197   1.3       mrg 	char *msg = error_message;
    198   1.3       mrg 	error_message = NULL;
    199   1.3       mrg 	return msg;
    200   1.3       mrg }
    201   1.1       cgd 
    202   1.3       mrg void
    203   1.3       mrg fmtprint(const char *libname, Obj_Entry *obj, const char *fmt1,
    204   1.3       mrg     const char *fmt2)
    205   1.3       mrg {
    206   1.3       mrg 	const char *libpath = obj ? obj->path : "not found";
    207   1.1       cgd 	char libnamebuf[200];
    208   1.3       mrg 	char *libmajor = NULL;
    209   1.3       mrg 	const char *fmt;
    210   1.3       mrg 	char *cp;
    211   1.3       mrg 	int c;
    212   1.3       mrg 
    213   1.3       mrg 	if (strncmp(libname, "lib", 3) == 0 &&
    214   1.3       mrg 	    (cp = strstr(libname, ".so")) != NULL) {
    215  1.19     joerg 		size_t i = cp - (libname + 3);
    216   1.3       mrg 
    217   1.3       mrg 		if (i >= sizeof(libnamebuf))
    218   1.3       mrg 			i = sizeof(libnamebuf) - 1;
    219   1.3       mrg 		(void)memcpy(libnamebuf, libname + 3, i);
    220   1.3       mrg 		libnamebuf[i] = '\0';
    221   1.3       mrg 		if (cp[3] && isdigit((unsigned char)cp[4]))
    222   1.3       mrg 			libmajor = &cp[4];
    223   1.3       mrg 		libname = libnamebuf;
    224   1.3       mrg 	}
    225   1.3       mrg 
    226   1.3       mrg 	if (fmt1 == NULL)
    227   1.3       mrg 		fmt1 = libmajor != NULL ?
    228   1.3       mrg 		    "\t-l%o.%m => %p\n" :
    229   1.3       mrg 		    "\t-l%o => %p\n";
    230   1.3       mrg 	if (fmt2 == NULL)
    231   1.3       mrg 		fmt2 = "\t%o => %p\n";
    232   1.3       mrg 
    233   1.3       mrg 	fmt = libname == libnamebuf ? fmt1 : fmt2;
    234   1.3       mrg 	while ((c = *fmt++) != '\0') {
    235   1.3       mrg 		switch (c) {
    236   1.3       mrg 		default:
    237   1.3       mrg 			putchar(c);
    238   1.3       mrg 			continue;
    239   1.3       mrg 		case '\\':
    240   1.3       mrg 			switch (c = *fmt) {
    241   1.3       mrg 			case '\0':
    242   1.3       mrg 				continue;
    243   1.3       mrg 			case 'n':
    244   1.3       mrg 				putchar('\n');
    245   1.3       mrg 				break;
    246   1.3       mrg 			case 't':
    247   1.3       mrg 				putchar('\t');
    248   1.3       mrg 				break;
    249   1.3       mrg 			}
    250   1.3       mrg 			break;
    251   1.3       mrg 		case '%':
    252   1.3       mrg 			switch (c = *fmt) {
    253   1.3       mrg 			case '\0':
    254   1.3       mrg 				continue;
    255   1.3       mrg 			case '%':
    256   1.3       mrg 			default:
    257   1.3       mrg 				putchar(c);
    258   1.3       mrg 				break;
    259   1.3       mrg 			case 'A':
    260   1.3       mrg 				printf("%s", main_local);
    261   1.3       mrg 				break;
    262   1.3       mrg 			case 'a':
    263   1.3       mrg 				printf("%s", main_progname);
    264   1.3       mrg 				break;
    265   1.3       mrg 			case 'o':
    266   1.3       mrg 				printf("%s", libname);
    267   1.3       mrg 				break;
    268   1.3       mrg 			case 'm':
    269   1.3       mrg 				printf("%s", libmajor);
    270   1.3       mrg 				break;
    271   1.3       mrg 			case 'n':
    272   1.3       mrg 				/* XXX: not supported for elf */
    273   1.3       mrg 				break;
    274   1.3       mrg 			case 'p':
    275   1.3       mrg 				printf("%s", libpath);
    276   1.3       mrg 				break;
    277   1.3       mrg 			case 'x':
    278   1.3       mrg 				printf("%p", obj ? obj->mapbase : 0);
    279   1.3       mrg 				break;
    280   1.3       mrg 			}
    281   1.3       mrg 			break;
    282   1.3       mrg 		}
    283   1.3       mrg 		++fmt;
    284   1.1       cgd 	}
    285   1.3       mrg }
    286   1.3       mrg 
    287   1.3       mrg void
    288   1.3       mrg print_needed(Obj_Entry *obj, const char *fmt1, const char *fmt2)
    289   1.3       mrg {
    290   1.3       mrg 	const Needed_Entry *needed;
    291   1.3       mrg 
    292   1.3       mrg 	for (needed = obj->needed; needed != NULL; needed = needed->next) {
    293   1.3       mrg 		const char *libname = obj->strtab + needed->name;
    294   1.3       mrg 
    295   1.3       mrg 		if (needed->obj != NULL) {
    296   1.3       mrg 			if (!needed->obj->printed) {
    297   1.3       mrg 				fmtprint(libname, needed->obj, fmt1, fmt2);
    298   1.3       mrg 				needed->obj->printed = 1;
    299  1.13     skrll 				print_needed(needed->obj, fmt1, fmt2);
    300   1.3       mrg 			}
    301   1.3       mrg 		} else {
    302   1.3       mrg 			fmtprint(libname, needed->obj, fmt1, fmt2);
    303   1.3       mrg 		}
    304   1.1       cgd 	}
    305   1.1       cgd }
    306  1.16     joerg 
    307  1.16     joerg void
    308  1.16     joerg _rtld_die(void)
    309  1.16     joerg {
    310  1.16     joerg 	const char *msg = dlerror();
    311  1.16     joerg 
    312  1.16     joerg 	if (msg == NULL)
    313  1.16     joerg 		msg = "Fatal error";
    314  1.16     joerg 	xerrx(1, "%s", msg);
    315  1.16     joerg }
    316  1.17     joerg 
    317  1.17     joerg void
    318  1.17     joerg _rtld_shared_enter(void)
    319  1.17     joerg {
    320  1.17     joerg }
    321  1.17     joerg 
    322  1.17     joerg void
    323  1.17     joerg _rtld_shared_exit(void)
    324  1.17     joerg {
    325  1.17     joerg }
    326  1.17     joerg 
    327  1.17     joerg void
    328  1.18     joerg _rtld_exclusive_enter(sigset_t *mask)
    329  1.17     joerg {
    330  1.17     joerg }
    331  1.17     joerg 
    332  1.17     joerg void
    333  1.18     joerg _rtld_exclusive_exit(sigset_t *mask)
    334  1.17     joerg {
    335  1.17     joerg }
    336