ldd.c revision 696
1696Spaul/* 2696Spaul * Copyright (c) 1993 Paul Kranenburg 3696Spaul * All rights reserved. 4696Spaul * 5696Spaul * Redistribution and use in source and binary forms, with or without 6696Spaul * modification, are permitted provided that the following conditions 7696Spaul * are met: 8696Spaul * 1. Redistributions of source code must retain the above copyright 9696Spaul * notice, this list of conditions and the following disclaimer. 10696Spaul * 2. Redistributions in binary form must reproduce the above copyright 11696Spaul * notice, this list of conditions and the following disclaimer in the 12696Spaul * documentation and/or other materials provided with the distribution. 13696Spaul * 3. All advertising materials mentioning features or use of this software 14696Spaul * must display the following acknowledgement: 15696Spaul * This product includes software developed by Paul Kranenburg. 16696Spaul * 4. The name of the author may not be used to endorse or promote products 17696Spaul * derived from this software withough specific prior written permission 18696Spaul * 19696Spaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20696Spaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21696Spaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22696Spaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23696Spaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24696Spaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25696Spaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26696Spaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27696Spaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28696Spaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29696Spaul * 30696Spaul * $Id: ldd.c,v 1.3 1993/10/31 14:54:29 pk Exp $ 31696Spaul */ 32696Spaul 33696Spaul#include <sys/param.h> 34696Spaul#include <stdio.h> 35696Spaul#include <stdlib.h> 36696Spaul#include <string.h> 37696Spaul#include <errno.h> 38696Spaul#include <sys/types.h> 39696Spaul#include <sys/stat.h> 40696Spaul#include <sys/file.h> 41696Spaul#include <sys/time.h> 42696Spaul#include <sys/resource.h> 43696Spaul#include <fcntl.h> 44696Spaul#include <sys/wait.h> 45696Spaul#include <a.out.h> 46696Spaul 47696Spaulstatic char *progname; 48696Spaul 49696Spaulvoid 50696Spaulusage() 51696Spaul{ 52696Spaul fprintf(stderr, "Usage: %s <filename> ...\n", progname); 53696Spaul} 54696Spaul 55696Spaulint 56696Spaulmain(argc, argv) 57696Spaulint argc; 58696Spaulchar *argv[]; 59696Spaul{ 60696Spaul int rval = 0; 61696Spaul int c; 62696Spaul extern int optind; 63696Spaul 64696Spaul if ((progname = strrchr(argv[0], '/')) == NULL) 65696Spaul progname = argv[0]; 66696Spaul else 67696Spaul progname++; 68696Spaul 69696Spaul while ((c = getopt(argc, argv, "")) != EOF) { 70696Spaul switch (c) { 71696Spaul default: 72696Spaul usage(); 73696Spaul exit(1); 74696Spaul } 75696Spaul } 76696Spaul argc -= optind; 77696Spaul argv += optind; 78696Spaul 79696Spaul if (argc <= 0) { 80696Spaul usage(); 81696Spaul exit(1); 82696Spaul } 83696Spaul 84696Spaul /* ld.so magic */ 85696Spaul setenv("LD_TRACE_LOADED_OBJECTS", "", 1); 86696Spaul 87696Spaul while (argc--) { 88696Spaul int fd; 89696Spaul struct exec hdr; 90696Spaul int status; 91696Spaul 92696Spaul if ((fd = open(*argv, O_RDONLY, 0)) < 0) { 93696Spaul perror(*argv); 94696Spaul rval |= 1; 95696Spaul argv++; 96696Spaul continue; 97696Spaul } 98696Spaul if (read(fd, &hdr, sizeof hdr) != sizeof hdr || 99696Spaul !(N_GETFLAG(hdr) & EX_DYNAMIC)) { 100696Spaul fprintf(stderr, "%s: not a dynamic executable\n", 101696Spaul *argv); 102696Spaul (void)close(fd); 103696Spaul rval |= 1; 104696Spaul argv++; 105696Spaul continue; 106696Spaul } 107696Spaul (void)close(fd); 108696Spaul 109696Spaul printf("%s:\n", *argv); 110696Spaul 111696Spaul switch (fork()) { 112696Spaul case -1: 113696Spaul perror("fork"); 114696Spaul exit(1); 115696Spaul break; 116696Spaul default: 117696Spaul if (wait(&status) <= 0) 118696Spaul perror("wait"); 119696Spaul 120696Spaul if (WIFSIGNALED(status)) { 121696Spaul fprintf(stderr, "%s: signal %d\n", 122696Spaul *argv, WTERMSIG(status)); 123696Spaul rval |= 1; 124696Spaul } else if (WIFEXITED(status) && WEXITSTATUS(status)) { 125696Spaul fprintf(stderr, "%s: exit status %d\n", 126696Spaul *argv, WEXITSTATUS(status)); 127696Spaul rval |= 1; 128696Spaul } 129696Spaul break; 130696Spaul case 0: 131696Spaul rval != execl(*argv, *argv, NULL) != 0; 132696Spaul perror(*argv); 133696Spaul _exit(1); 134696Spaul } 135696Spaul argv++; 136696Spaul } 137696Spaul 138696Spaul return rval; 139696Spaul} 140