ldd.c revision 18598
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
171153Sjkh *    derived from this software without 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 *
3018598Speter *	$Id: ldd.c,v 1.5 1994/12/23 22:31:31 nate Exp $
31696Spaul */
32696Spaul
33696Spaul#include <sys/types.h>
34696Spaul#include <sys/stat.h>
35696Spaul#include <sys/file.h>
36696Spaul#include <sys/time.h>
37696Spaul#include <sys/resource.h>
38696Spaul#include <sys/wait.h>
39696Spaul#include <a.out.h>
401741Srich#include <err.h>
411741Srich#include <fcntl.h>
421741Srich#include <stdio.h>
431741Srich#include <stdlib.h>
441741Srich#include <string.h>
451741Srich#include <unistd.h>
46696Spaul
47696Spaulvoid
48696Spaulusage()
49696Spaul{
501741Srich	extern char *__progname;
511741Srich
521741Srich	fprintf(stderr, "Usage: %s <filename> ...\n", __progname);
531741Srich	exit(1);
54696Spaul}
55696Spaul
56696Spaulint
57696Spaulmain(argc, argv)
58696Spaulint	argc;
59696Spaulchar	*argv[];
60696Spaul{
6118598Speter	char		*fmt1 = NULL, *fmt2 = NULL;
621741Srich	int		rval;
63696Spaul	int		c;
64696Spaul
6518598Speter	while ((c = getopt(argc, argv, "f:")) != EOF) {
66696Spaul		switch (c) {
6718598Speter		case 'f':
6818598Speter			if (fmt1) {
6918598Speter				if (fmt2)
7018598Speter					errx(1, "Too many formats");
7118598Speter				fmt2 = optarg;
7218598Speter			} else
7318598Speter				fmt1 = optarg;
7418598Speter			break;
75696Spaul		default:
76696Spaul			usage();
771741Srich			/*NOTREACHED*/
78696Spaul		}
79696Spaul	}
80696Spaul	argc -= optind;
81696Spaul	argv += optind;
82696Spaul
83696Spaul	if (argc <= 0) {
84696Spaul		usage();
851741Srich		/*NOTREACHED*/
86696Spaul	}
87696Spaul
88696Spaul	/* ld.so magic */
89696Spaul	setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
9018598Speter	if (fmt1)
9118598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
9218598Speter	if (fmt2)
9318598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
94696Spaul
951741Srich	rval = 0;
96696Spaul	while (argc--) {
97696Spaul		int	fd;
98696Spaul		struct exec hdr;
99696Spaul		int	status;
100696Spaul
101696Spaul		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
1021741Srich			warn("%s", *argv);
103696Spaul			rval |= 1;
104696Spaul			argv++;
105696Spaul			continue;
106696Spaul		}
1075205Snate		if (read(fd, &hdr, sizeof hdr) != sizeof hdr
1085205Snate		    || (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC
1095205Snate#if 1 /* Compatibility */
1105205Snate		    || hdr.a_entry < __LDPGSZ
1115205Snate#endif
1125205Snate		    ) {
1131741Srich
1141741Srich			warnx("%s: not a dynamic executable", *argv);
115696Spaul			(void)close(fd);
116696Spaul			rval |= 1;
117696Spaul			argv++;
118696Spaul			continue;
119696Spaul		}
120696Spaul		(void)close(fd);
121696Spaul
12218598Speter		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
12318598Speter		if (fmt1 == NULL && fmt2 == NULL)
12418598Speter			/* Default formats */
12518598Speter			printf("%s:\n", *argv);
12618598Speter
1271153Sjkh		fflush(stdout);
128696Spaul
129696Spaul		switch (fork()) {
130696Spaul		case -1:
1311741Srich			err(1, "fork");
132696Spaul			break;
133696Spaul		default:
1341741Srich			if (wait(&status) <= 0) {
1351741Srich				warn("wait");
1361741Srich				rval |= 1;
1371741Srich			} else if (WIFSIGNALED(status)) {
138696Spaul				fprintf(stderr, "%s: signal %d\n",
139696Spaul						*argv, WTERMSIG(status));
140696Spaul				rval |= 1;
141696Spaul			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
142696Spaul				fprintf(stderr, "%s: exit status %d\n",
143696Spaul						*argv, WEXITSTATUS(status));
144696Spaul				rval |= 1;
145696Spaul			}
146696Spaul			break;
147696Spaul		case 0:
1481741Srich			rval |= execl(*argv, *argv, NULL) != 0;
149696Spaul			perror(*argv);
150696Spaul			_exit(1);
151696Spaul		}
152696Spaul		argv++;
153696Spaul	}
154696Spaul
155696Spaul	return rval;
156696Spaul}
157