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