Home | History | Annotate | Line # | Download | only in ldd
ldd.c revision 1.27
      1 /*	$NetBSD: ldd.c,v 1.27 2022/10/18 19:04:57 mrg 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.c,v 1.27 2022/10/18 19:04:57 mrg 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 /*
     93  * This may be ELF64 or ELF32 but since they are used opaquely it doesn't
     94  * really matter.
     95  */
     96 Obj_Entry *_rtld_objlist;	/* Head of linked list of shared objects */
     97 Obj_Entry **_rtld_objtail = &_rtld_objlist;
     98 				/* Link field of last object in list */
     99 u_int _rtld_objcount;		/* Number of shared objects */
    100 u_int _rtld_objloads;		/* Number of objects loaded */
    101 
    102 Obj_Entry *_rtld_objmain;	/* The main program shared object */
    103 size_t _rtld_pagesz;
    104 
    105 Search_Path *_rtld_default_paths;
    106 Search_Path *_rtld_paths;
    107 Library_Xform *_rtld_xforms;
    108 
    109 static void usage(void) __dead;
    110 char *main_local;
    111 char *main_progname;
    112 
    113 static void
    114 usage(void)
    115 {
    116 	fprintf(stderr, "Usage: %s [-f <format 1>] [-f <format 2>] <filename>"
    117 		" ...\n", getprogname());
    118 	exit(1);
    119 }
    120 
    121 int
    122 main(int argc, char **argv)
    123 {
    124 	const char *fmt1 = NULL, *fmt2 = NULL;
    125 	int c, exit_status = EXIT_SUCCESS;
    126 	char cwd[MAXPATHLEN], path[MAXPATHLEN];
    127 	bool verbose = false, failed = false;
    128 
    129 #ifdef DEBUG
    130 	debug = 1;
    131 #endif
    132 	while ((c = getopt(argc, argv, "f:ov")) != -1) {
    133 		switch (c) {
    134 		case 'f':
    135 			if (fmt1) {
    136 				if (fmt2)
    137 					errx(1, "Too many formats");
    138 				fmt2 = optarg;
    139 			} else
    140 				fmt1 = optarg;
    141 			break;
    142 		case 'o':
    143 			if (fmt1 || fmt2)
    144 				errx(1, "Cannot use -o and -f together");
    145 			fmt1 = "%a:-l%o.%m => %p\n";
    146 			break;
    147 		case 'v':
    148 			verbose = true;
    149 			break;
    150 		default:
    151 			usage();
    152 			/*NOTREACHED*/
    153 		}
    154 	}
    155 	argc -= optind;
    156 	argv += optind;
    157 
    158 	if (argc <= 0) {
    159 		usage();
    160 		/*NOTREACHED*/
    161 	}
    162 	if (getcwd(cwd, sizeof(cwd)) == NULL)
    163 		err(EXIT_FAILURE, "Can't get working directory");
    164 
    165 	for (; argc != 0; argc--, argv++) {
    166 		int fd;
    167 
    168 		if (**argv != '/') {
    169 			strcpy(path, cwd);
    170 			strlcat(path, "/", sizeof(path));
    171 			strlcat(path, *argv, sizeof(path));
    172 		} else {
    173 			strlcpy(path, *argv, sizeof(path));
    174 		}
    175 		fd = open(*argv, O_RDONLY);
    176 		if (fd == -1) {
    177 			exit_status = EXIT_FAILURE;
    178 			warn("%s", *argv);
    179 			continue;
    180 		}
    181 		if (elf_ldd(fd, *argv, path, fmt1, fmt2) == -1) {
    182 			if (verbose)
    183 				warnx("%s", error_message);
    184 			failed = true;
    185 		}
    186 		/* Alpha never had 32 bit support. */
    187 #if (defined(_LP64) && !defined(ELF64_ONLY)) || defined(MIPS_N32)
    188 		if (failed && elf32_ldd(fd, *argv, path, fmt1, fmt2) == -1) {
    189 			if (verbose)
    190 				warnx("%s", error_message);
    191 			failed = true;
    192 		}
    193 #if defined(__mips__) && 0 /* XXX this is still hosed for some reason */
    194 		if (failed &&
    195 		    elf32_ldd_compat(fd, *argv, path, fmt1, fmt2) == -1) {
    196 			if (verbose)
    197 				warnx("%s", error_message);
    198 			failed = true;
    199 		}
    200 #endif
    201 #endif
    202 
    203 		if (failed) {
    204 			exit_status = EXIT_FAILURE;
    205 			if (!verbose)
    206 				warnx("%s", error_message);
    207 		}
    208 		close(fd);
    209 	}
    210 
    211 	return exit_status;
    212 }
    213 
    214 /*
    215  * Error reporting function.  Use it like printf.  If formats the message
    216  * into a buffer, and sets things up so that the next call to dlerror()
    217  * will return the message.
    218  */
    219 void
    220 _rtld_error(const char *fmt, ...)
    221 {
    222 	static char buf[512];
    223 	va_list ap;
    224 	va_start(ap, fmt);
    225 	xvsnprintf(buf, sizeof buf, fmt, ap);
    226 	error_message = buf;
    227 	va_end(ap);
    228 }
    229 
    230 char *
    231 dlerror()
    232 {
    233 	char *msg = error_message;
    234 	error_message = NULL;
    235 	return msg;
    236 }
    237 
    238 void
    239 _rtld_die(void)
    240 {
    241 	const char *msg = dlerror();
    242 
    243 	if (msg == NULL)
    244 		msg = "Fatal error";
    245 	xerrx(1, "%s", msg);
    246 }
    247 
    248 void
    249 _rtld_shared_enter(void)
    250 {
    251 }
    252 
    253 void
    254 _rtld_shared_exit(void)
    255 {
    256 }
    257 
    258 void
    259 _rtld_exclusive_enter(sigset_t *mask)
    260 {
    261 }
    262 
    263 void
    264 _rtld_exclusive_exit(sigset_t *mask)
    265 {
    266 }
    267