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