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.4 1994/06/15 22:41:03 rich 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 <err.h> 41 #include <fcntl.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <unistd.h> 46 47 void 48 usage() 49 { 50 extern char *__progname; 51 52 fprintf(stderr, "Usage: %s <filename> ...\n", __progname); 53 exit(1); 54 } 55 56 int 57 main(argc, argv) 58 int argc; 59 char *argv[]; 60 { 61 int rval; 62 int c; 63 64 while ((c = getopt(argc, argv, "")) != EOF) { 65 switch (c) { 66 default: 67 usage(); 68 /*NOTREACHED*/ 69 } 70 } 71 argc -= optind; 72 argv += optind; 73 74 if (argc <= 0) { 75 usage(); 76 /*NOTREACHED*/ 77 } 78 79 /* ld.so magic */ 80 setenv("LD_TRACE_LOADED_OBJECTS", "", 1); 81 82 rval = 0; 83 while (argc--) { 84 int fd; 85 struct exec hdr; 86 int status; 87 88 if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 89 warn("%s", *argv); 90 rval |= 1; 91 argv++; 92 continue; 93 } 94 if (read(fd, &hdr, sizeof hdr) != sizeof hdr 95 || (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC 96 #if 1 /* Compatibility */ 97 || hdr.a_entry < __LDPGSZ 98 #endif 99 ) { 100 101 warnx("%s: not a dynamic executable", *argv); 102 (void)close(fd); 103 rval |= 1; 104 argv++; 105 continue; 106 } 107 (void)close(fd); 108 109 printf("%s:\n", *argv); 110 fflush(stdout); 111 112 switch (fork()) { 113 case -1: 114 err(1, "fork"); 115 break; 116 default: 117 if (wait(&status) <= 0) { 118 warn("wait"); 119 rval |= 1; 120 } else if (WIFSIGNALED(status)) { 121 fprintf(stderr, "%s: signal %d\n", 122 *argv, WTERMSIG(status)); 123 rval |= 1; 124 } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 125 fprintf(stderr, "%s: exit status %d\n", 126 *argv, WEXITSTATUS(status)); 127 rval |= 1; 128 } 129 break; 130 case 0: 131 rval |= execl(*argv, *argv, NULL) != 0; 132 perror(*argv); 133 _exit(1); 134 } 135 argv++; 136 } 137 138 return rval; 139 } 140