ldd.c revision 18598
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.5 1994/12/23 22:31:31 nate 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
47void
48usage()
49{
50	extern char *__progname;
51
52	fprintf(stderr, "Usage: %s <filename> ...\n", __progname);
53	exit(1);
54}
55
56int
57main(argc, argv)
58int	argc;
59char	*argv[];
60{
61	char		*fmt1 = NULL, *fmt2 = NULL;
62	int		rval;
63	int		c;
64
65	while ((c = getopt(argc, argv, "f:")) != EOF) {
66		switch (c) {
67		case 'f':
68			if (fmt1) {
69				if (fmt2)
70					errx(1, "Too many formats");
71				fmt2 = optarg;
72			} else
73				fmt1 = optarg;
74			break;
75		default:
76			usage();
77			/*NOTREACHED*/
78		}
79	}
80	argc -= optind;
81	argv += optind;
82
83	if (argc <= 0) {
84		usage();
85		/*NOTREACHED*/
86	}
87
88	/* ld.so magic */
89	setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
90	if (fmt1)
91		setenv("LD_TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
92	if (fmt2)
93		setenv("LD_TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
94
95	rval = 0;
96	while (argc--) {
97		int	fd;
98		struct exec hdr;
99		int	status;
100
101		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
102			warn("%s", *argv);
103			rval |= 1;
104			argv++;
105			continue;
106		}
107		if (read(fd, &hdr, sizeof hdr) != sizeof hdr
108		    || (N_GETFLAG(hdr) & EX_DPMASK) != EX_DYNAMIC
109#if 1 /* Compatibility */
110		    || hdr.a_entry < __LDPGSZ
111#endif
112		    ) {
113
114			warnx("%s: not a dynamic executable", *argv);
115			(void)close(fd);
116			rval |= 1;
117			argv++;
118			continue;
119		}
120		(void)close(fd);
121
122		setenv("LD_TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
123		if (fmt1 == NULL && fmt2 == NULL)
124			/* Default formats */
125			printf("%s:\n", *argv);
126
127		fflush(stdout);
128
129		switch (fork()) {
130		case -1:
131			err(1, "fork");
132			break;
133		default:
134			if (wait(&status) <= 0) {
135				warn("wait");
136				rval |= 1;
137			} else if (WIFSIGNALED(status)) {
138				fprintf(stderr, "%s: signal %d\n",
139						*argv, WTERMSIG(status));
140				rval |= 1;
141			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
142				fprintf(stderr, "%s: exit status %d\n",
143						*argv, WEXITSTATUS(status));
144				rval |= 1;
145			}
146			break;
147		case 0:
148			rval |= execl(*argv, *argv, NULL) != 0;
149			perror(*argv);
150			_exit(1);
151		}
152		argv++;
153	}
154
155	return rval;
156}
157