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.15 1998/05/01 08:40:11 dfr 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 48 extern void dump_file __P((const char *)); 49 extern int error_count; 50 51 void 52 usage() 53 { 54 fprintf(stderr, "usage: ldd [-v] [-f format] program ...\n"); 55 exit(1); 56 } 57 58 int 59 main(argc, argv) 60 int argc; 61 char *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 for ( ; argc > 0; argc--, argv++) { 112 int fd; 113 union { 114 struct exec aout; 115 Elf32_Ehdr elf; 116 } hdr; 117 int n; 118 int status; 119 int file_ok; 120 121 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 122 warn("%s", *argv); 123 rval |= 1; 124 continue; 125 } 126 if ((n = read(fd, &hdr, sizeof hdr)) == -1) { 127 warn("%s: can't read program header", *argv); 128 (void)close(fd); 129 rval |= 1; 130 continue; 131 } 132 133 file_ok = 1; 134 if (n >= sizeof hdr.aout && !N_BADMAG(hdr.aout)) { 135 /* a.out file */ 136 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC 137 #if 1 /* Compatibility */ 138 || hdr.aout.a_entry < __LDPGSZ 139 #endif 140 ) { 141 warnx("%s: not a dynamic executable", *argv); 142 file_ok = 0; 143 } 144 } else if (n >= sizeof hdr.elf && IS_ELF(hdr.elf)) { 145 Elf32_Ehdr ehdr; 146 Elf32_Phdr phdr; 147 int dynamic = 0, i; 148 149 lseek(fd, 0, SEEK_SET); 150 if (read(fd, &ehdr, sizeof ehdr) != sizeof ehdr) { 151 warnx("%s: can't read program header", *argv); 152 file_ok = 0; 153 } 154 lseek(fd, 0, ehdr.e_phoff); 155 for (i = 0; i < ehdr.e_phnum; i++) { 156 if (read(fd, &phdr, ehdr.e_phentsize) 157 != sizeof phdr) { 158 warnx("%s: can't read program header", 159 *argv); 160 file_ok = 0; 161 } 162 if (phdr.p_type == PT_DYNAMIC) 163 dynamic = 1; 164 } 165 if (!dynamic) { 166 warnx("%s: not a dynamic executable", *argv); 167 file_ok = 0; 168 } 169 } else { 170 warnx("%s: not a dynamic executable", *argv); 171 file_ok = 0; 172 } 173 (void)close(fd); 174 if (!file_ok) { 175 rval |= 1; 176 continue; 177 } 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 } 210 211 return rval; 212 } 213