ldd.c revision 35575
1/* 2 * Copyright (c) 1993 Paul Kranenburg 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Paul Kranenburg. 16 * 4. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * 30 * $Id: ldd.c,v 1.14 1997/09/02 21:54:39 jdp Exp $ 31 */ 32 33#include <sys/types.h> 34#include <sys/stat.h> 35#include <sys/file.h> 36#include <sys/time.h> 37#include <sys/resource.h> 38#include <sys/wait.h> 39#include <a.out.h> 40#include <elf.h> 41#include <err.h> 42#include <fcntl.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <unistd.h> 47 48extern void dump_file __P((const char *)); 49extern int error_count; 50 51void 52usage() 53{ 54 fprintf(stderr, "usage: ldd [-v] [-f format] program ...\n"); 55 exit(1); 56} 57 58int 59main(argc, argv) 60int argc; 61char *argv[]; 62{ 63 char *fmt1 = NULL, *fmt2 = NULL; 64 int rval; 65 int c; 66 int vflag = 0; 67 68 while ((c = getopt(argc, argv, "vf:")) != EOF) { 69 switch (c) { 70 case 'v': 71 vflag++; 72 break; 73 case 'f': 74 if (fmt1) { 75 if (fmt2) 76 errx(1, "Too many formats"); 77 fmt2 = optarg; 78 } else 79 fmt1 = optarg; 80 break; 81 default: 82 usage(); 83 /*NOTREACHED*/ 84 } 85 } 86 argc -= optind; 87 argv += optind; 88 89 if (vflag && fmt1) 90 errx(1, "-v may not be used with -f"); 91 92 if (argc <= 0) { 93 usage(); 94 /*NOTREACHED*/ 95 } 96 97 if (vflag) { 98 for (c = 0; c < argc; c++) 99 dump_file(argv[c]); 100 exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE); 101 } 102 103 /* ld.so magic */ 104 setenv("LD_TRACE_LOADED_OBJECTS", "1", 1); 105 if (fmt1) 106 setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1); 107 if (fmt2) 108 setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1); 109 110 rval = 0; 111 while (argc--) { 112 int fd; 113 struct exec hdr; 114 int status; 115 116 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 117 warn("%s", *argv); 118 rval |= 1; 119 argv++; 120 continue; 121 } 122 if (read(fd, &hdr, sizeof hdr) != sizeof hdr) { 123 warnx("%s: can't read program header", *argv); 124 (void)close(fd); 125 rval |= 1; 126 argv++; 127 continue; 128 } 129 130 if (!N_BADMAG(hdr)) { 131 /* a.out file */ 132 if ((N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC 133#if 1 /* Compatibility */ 134 || hdr.a_entry < __LDPGSZ 135#endif 136 ) { 137 warnx("%s: not a dynamic executable", *argv); 138 (void)close(fd); 139 rval |= 1; 140 argv++; 141 continue; 142 } 143 } else if (IS_ELF(*(Elf32_Ehdr*)&hdr)) { 144 Elf32_Ehdr ehdr; 145 Elf32_Phdr phdr; 146 int dynamic = 0, i; 147 148 lseek(fd, 0, SEEK_SET); 149 if (read(fd, &ehdr, sizeof ehdr) != sizeof ehdr) { 150 warnx("%s: can't read program header", *argv); 151 (void)close(fd); 152 rval |= 1; 153 argv++; 154 continue; 155 } 156 lseek(fd, 0, ehdr.e_phoff); 157 for (i = 0; i < ehdr.e_phnum; i++) { 158 if (read(fd, &phdr, ehdr.e_phentsize) 159 != sizeof phdr) { 160 warnx("%s: can't read program header", *argv); 161 (void)close(fd); 162 rval |= 1; 163 argv++; 164 continue; 165 } 166 if (phdr.p_type == PT_DYNAMIC) 167 dynamic = 1; 168 } 169 if (!dynamic) { 170 warnx("%s: not a dynamic executable", *argv); 171 (void)close(fd); 172 rval |= 1; 173 argv++; 174 continue; 175 } 176 } 177 (void)close(fd); 178 179 setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1); 180 if (fmt1 == NULL && fmt2 == NULL) 181 /* Default formats */ 182 printf("%s:\n", *argv); 183 184 fflush(stdout); 185 186 switch (fork()) { 187 case -1: 188 err(1, "fork"); 189 break; 190 default: 191 if (wait(&status) <= 0) { 192 warn("wait"); 193 rval |= 1; 194 } else if (WIFSIGNALED(status)) { 195 fprintf(stderr, "%s: signal %d\n", 196 *argv, WTERMSIG(status)); 197 rval |= 1; 198 } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 199 fprintf(stderr, "%s: exit status %d\n", 200 *argv, WEXITSTATUS(status)); 201 rval |= 1; 202 } 203 break; 204 case 0: 205 rval |= execl(*argv, *argv, NULL) != 0; 206 perror(*argv); 207 _exit(1); 208 } 209 argv++; 210 } 211 212 return rval; 213} 214