xref: /freebsd/usr.bin/ldd/ldd.c (revision 699e1b82fb90bac1cb5ceec645e962478ebe7e91)
1b9ae52e3SPaul Richards /*
2b9ae52e3SPaul Richards  * Copyright (c) 1993 Paul Kranenburg
3b9ae52e3SPaul Richards  * All rights reserved.
4b9ae52e3SPaul Richards  *
5b9ae52e3SPaul Richards  * Redistribution and use in source and binary forms, with or without
6b9ae52e3SPaul Richards  * modification, are permitted provided that the following conditions
7b9ae52e3SPaul Richards  * are met:
8b9ae52e3SPaul Richards  * 1. Redistributions of source code must retain the above copyright
9b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer.
10b9ae52e3SPaul Richards  * 2. Redistributions in binary form must reproduce the above copyright
11b9ae52e3SPaul Richards  *    notice, this list of conditions and the following disclaimer in the
12b9ae52e3SPaul Richards  *    documentation and/or other materials provided with the distribution.
13b9ae52e3SPaul Richards  * 3. All advertising materials mentioning features or use of this software
14b9ae52e3SPaul Richards  *    must display the following acknowledgement:
15b9ae52e3SPaul Richards  *      This product includes software developed by Paul Kranenburg.
16b9ae52e3SPaul Richards  * 4. The name of the author may not be used to endorse or promote products
1709e3d49dSJordan K. Hubbard  *    derived from this software without specific prior written permission
18b9ae52e3SPaul Richards  *
19b9ae52e3SPaul Richards  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20b9ae52e3SPaul Richards  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21b9ae52e3SPaul Richards  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22b9ae52e3SPaul Richards  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23b9ae52e3SPaul Richards  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24b9ae52e3SPaul Richards  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b9ae52e3SPaul Richards  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b9ae52e3SPaul Richards  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b9ae52e3SPaul Richards  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28b9ae52e3SPaul Richards  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b9ae52e3SPaul Richards  *
30699e1b82SRich Murphey  *	$Id: ldd.c,v 1.3 1994/02/13 20:42:43 jkh Exp $
31b9ae52e3SPaul Richards  */
32b9ae52e3SPaul Richards 
33b9ae52e3SPaul Richards #include <sys/types.h>
34b9ae52e3SPaul Richards #include <sys/stat.h>
35b9ae52e3SPaul Richards #include <sys/file.h>
36b9ae52e3SPaul Richards #include <sys/time.h>
37b9ae52e3SPaul Richards #include <sys/resource.h>
38b9ae52e3SPaul Richards #include <sys/wait.h>
39b9ae52e3SPaul Richards #include <a.out.h>
40699e1b82SRich Murphey #include <err.h>
41699e1b82SRich Murphey #include <fcntl.h>
42699e1b82SRich Murphey #include <stdio.h>
43699e1b82SRich Murphey #include <stdlib.h>
44699e1b82SRich Murphey #include <string.h>
45699e1b82SRich Murphey #include <unistd.h>
46b9ae52e3SPaul Richards 
47b9ae52e3SPaul Richards void
48b9ae52e3SPaul Richards usage()
49b9ae52e3SPaul Richards {
50699e1b82SRich Murphey 	extern char *__progname;
51699e1b82SRich Murphey 
52699e1b82SRich Murphey 	fprintf(stderr, "Usage: %s <filename> ...\n", __progname);
53699e1b82SRich Murphey 	exit(1);
54b9ae52e3SPaul Richards }
55b9ae52e3SPaul Richards 
56b9ae52e3SPaul Richards int
57b9ae52e3SPaul Richards main(argc, argv)
58b9ae52e3SPaul Richards int	argc;
59b9ae52e3SPaul Richards char	*argv[];
60b9ae52e3SPaul Richards {
61699e1b82SRich Murphey 	int		rval;
62b9ae52e3SPaul Richards 	int		c;
63b9ae52e3SPaul Richards 
64b9ae52e3SPaul Richards 	while ((c = getopt(argc, argv, "")) != EOF) {
65b9ae52e3SPaul Richards 		switch (c) {
66b9ae52e3SPaul Richards 		default:
67b9ae52e3SPaul Richards 			usage();
68699e1b82SRich Murphey 			/*NOTREACHED*/
69b9ae52e3SPaul Richards 		}
70b9ae52e3SPaul Richards 	}
71b9ae52e3SPaul Richards 	argc -= optind;
72b9ae52e3SPaul Richards 	argv += optind;
73b9ae52e3SPaul Richards 
74b9ae52e3SPaul Richards 	if (argc <= 0) {
75b9ae52e3SPaul Richards 		usage();
76699e1b82SRich Murphey 		/*NOTREACHED*/
77b9ae52e3SPaul Richards 	}
78b9ae52e3SPaul Richards 
79b9ae52e3SPaul Richards 	/* ld.so magic */
80b9ae52e3SPaul Richards 	setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
81b9ae52e3SPaul Richards 
82699e1b82SRich Murphey 	rval = 0;
83b9ae52e3SPaul Richards 	while (argc--) {
84b9ae52e3SPaul Richards 		int	fd;
85b9ae52e3SPaul Richards 		struct exec hdr;
86b9ae52e3SPaul Richards 		int	status;
87b9ae52e3SPaul Richards 
88b9ae52e3SPaul Richards 		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
89699e1b82SRich Murphey 			warn("%s", *argv);
90b9ae52e3SPaul Richards 			rval |= 1;
91b9ae52e3SPaul Richards 			argv++;
92b9ae52e3SPaul Richards 			continue;
93b9ae52e3SPaul Richards 		}
94b9ae52e3SPaul Richards 		if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
95699e1b82SRich Murphey 		    !(N_GETFLAG(hdr) & EX_DYNAMIC) ||
96699e1b82SRich Murphey 		    hdr.a_entry < __LDPGSZ) {
97699e1b82SRich Murphey 
98699e1b82SRich Murphey 			warnx("%s: not a dynamic executable", *argv);
99b9ae52e3SPaul Richards 			(void)close(fd);
100b9ae52e3SPaul Richards 			rval |= 1;
101b9ae52e3SPaul Richards 			argv++;
102b9ae52e3SPaul Richards 			continue;
103b9ae52e3SPaul Richards 		}
104b9ae52e3SPaul Richards 		(void)close(fd);
105b9ae52e3SPaul Richards 
106b9ae52e3SPaul Richards 		printf("%s:\n", *argv);
10709e3d49dSJordan K. Hubbard 		fflush(stdout);
108b9ae52e3SPaul Richards 
109b9ae52e3SPaul Richards 		switch (fork()) {
110b9ae52e3SPaul Richards 		case -1:
111699e1b82SRich Murphey 			err(1, "fork");
112b9ae52e3SPaul Richards 			break;
113b9ae52e3SPaul Richards 		default:
114699e1b82SRich Murphey 			if (wait(&status) <= 0) {
115699e1b82SRich Murphey 				warn("wait");
116699e1b82SRich Murphey 				rval |= 1;
117699e1b82SRich Murphey 			} else if (WIFSIGNALED(status)) {
118b9ae52e3SPaul Richards 				fprintf(stderr, "%s: signal %d\n",
119b9ae52e3SPaul Richards 						*argv, WTERMSIG(status));
120b9ae52e3SPaul Richards 				rval |= 1;
121b9ae52e3SPaul Richards 			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
122b9ae52e3SPaul Richards 				fprintf(stderr, "%s: exit status %d\n",
123b9ae52e3SPaul Richards 						*argv, WEXITSTATUS(status));
124b9ae52e3SPaul Richards 				rval |= 1;
125b9ae52e3SPaul Richards 			}
126b9ae52e3SPaul Richards 			break;
127b9ae52e3SPaul Richards 		case 0:
128699e1b82SRich Murphey 			rval |= execl(*argv, *argv, NULL) != 0;
129b9ae52e3SPaul Richards 			perror(*argv);
130b9ae52e3SPaul Richards 			_exit(1);
131b9ae52e3SPaul Richards 		}
132b9ae52e3SPaul Richards 		argv++;
133b9ae52e3SPaul Richards 	}
134b9ae52e3SPaul Richards 
135b9ae52e3SPaul Richards 	return rval;
136b9ae52e3SPaul Richards }
137