ldd.c revision 18600
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 *
3018600Speter *	$Id: ldd.c,v 1.6 1996/10/01 01:34:32 peter 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
4718600Speterextern void	dump_filename __P((const char *));
4818600Speterextern int	error_count;
4918600Speter
50696Spaulvoid
51696Spaulusage()
52696Spaul{
531741Srich	extern char *__progname;
541741Srich
551741Srich	fprintf(stderr, "Usage: %s <filename> ...\n", __progname);
561741Srich	exit(1);
57696Spaul}
58696Spaul
59696Spaulint
60696Spaulmain(argc, argv)
61696Spaulint	argc;
62696Spaulchar	*argv[];
63696Spaul{
6418598Speter	char		*fmt1 = NULL, *fmt2 = NULL;
651741Srich	int		rval;
66696Spaul	int		c;
6718600Speter	int		vflag;
68696Spaul
6918600Speter	while ((c = getopt(argc, argv, "vf:")) != EOF) {
70696Spaul		switch (c) {
7118600Speter		case 'v':
7218600Speter			vflag++;
7318600Speter			break;
7418598Speter		case 'f':
7518598Speter			if (fmt1) {
7618598Speter				if (fmt2)
7718598Speter					errx(1, "Too many formats");
7818598Speter				fmt2 = optarg;
7918598Speter			} else
8018598Speter				fmt1 = optarg;
8118598Speter			break;
82696Spaul		default:
83696Spaul			usage();
841741Srich			/*NOTREACHED*/
85696Spaul		}
86696Spaul	}
87696Spaul	argc -= optind;
88696Spaul	argv += optind;
89696Spaul
9018600Speter	if (vflag && fmt1)
9118600Speter		errx(1, "-v may not be used with -f");
9218600Speter
93696Spaul	if (argc <= 0) {
94696Spaul		usage();
951741Srich		/*NOTREACHED*/
96696Spaul	}
97696Spaul
9818600Speter	if (vflag) {
9918600Speter		for (c = 0; c < argc; c++)
10018600Speter			dump_file(argv[c]);
10118600Speter		exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
10218600Speter	}
10318600Speter
104696Spaul	/* ld.so magic */
105696Spaul	setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
10618598Speter	if (fmt1)
10718598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
10818598Speter	if (fmt2)
10918598Speter		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
110696Spaul
1111741Srich	rval = 0;
112696Spaul	while (argc--) {
113696Spaul		int	fd;
114696Spaul		struct exec hdr;
115696Spaul		int	status;
116696Spaul
117696Spaul		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
1181741Srich			warn("%s", *argv);
119696Spaul			rval |= 1;
120696Spaul			argv++;
121696Spaul			continue;
122696Spaul		}
1235205Snate		if (read(fd, &hdr, sizeof hdr) != sizeof hdr
1245205Snate		    || (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC
1255205Snate#if 1 /* Compatibility */
1265205Snate		    || hdr.a_entry < __LDPGSZ
1275205Snate#endif
1285205Snate		    ) {
1291741Srich
1301741Srich			warnx("%s: not a dynamic executable", *argv);
131696Spaul			(void)close(fd);
132696Spaul			rval |= 1;
133696Spaul			argv++;
134696Spaul			continue;
135696Spaul		}
136696Spaul		(void)close(fd);
137696Spaul
13818598Speter		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
13918598Speter		if (fmt1 == NULL && fmt2 == NULL)
14018598Speter			/* Default formats */
14118598Speter			printf("%s:\n", *argv);
14218598Speter
1431153Sjkh		fflush(stdout);
144696Spaul
145696Spaul		switch (fork()) {
146696Spaul		case -1:
1471741Srich			err(1, "fork");
148696Spaul			break;
149696Spaul		default:
1501741Srich			if (wait(&status) <= 0) {
1511741Srich				warn("wait");
1521741Srich				rval |= 1;
1531741Srich			} else if (WIFSIGNALED(status)) {
154696Spaul				fprintf(stderr, "%s: signal %d\n",
155696Spaul						*argv, WTERMSIG(status));
156696Spaul				rval |= 1;
157696Spaul			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
158696Spaul				fprintf(stderr, "%s: exit status %d\n",
159696Spaul						*argv, WEXITSTATUS(status));
160696Spaul				rval |= 1;
161696Spaul			}
162696Spaul			break;
163696Spaul		case 0:
1641741Srich			rval |= execl(*argv, *argv, NULL) != 0;
165696Spaul			perror(*argv);
166696Spaul			_exit(1);
167696Spaul		}
168696Spaul		argv++;
169696Spaul	}
170696Spaul
171696Spaul	return rval;
172696Spaul}
173