xref: /freebsd/usr.bin/ldd/ldd.c (revision 09e3d49d92388785abc5a92944fa0640e351a5f4)
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  *
3009e3d49dSJordan K. Hubbard  *	$Id: ldd.c,v 1.2 1993/11/09 04:19:27 paul Exp $
31b9ae52e3SPaul Richards  */
32b9ae52e3SPaul Richards 
33b9ae52e3SPaul Richards #include <sys/param.h>
34b9ae52e3SPaul Richards #include <stdio.h>
35b9ae52e3SPaul Richards #include <stdlib.h>
36b9ae52e3SPaul Richards #include <string.h>
37b9ae52e3SPaul Richards #include <errno.h>
38b9ae52e3SPaul Richards #include <sys/types.h>
39b9ae52e3SPaul Richards #include <sys/stat.h>
40b9ae52e3SPaul Richards #include <sys/file.h>
41b9ae52e3SPaul Richards #include <sys/time.h>
42b9ae52e3SPaul Richards #include <sys/resource.h>
43b9ae52e3SPaul Richards #include <fcntl.h>
44b9ae52e3SPaul Richards #include <sys/wait.h>
45b9ae52e3SPaul Richards #include <a.out.h>
46b9ae52e3SPaul Richards 
47b9ae52e3SPaul Richards static char	*progname;
48b9ae52e3SPaul Richards 
49b9ae52e3SPaul Richards void
50b9ae52e3SPaul Richards usage()
51b9ae52e3SPaul Richards {
52b9ae52e3SPaul Richards 	fprintf(stderr, "Usage: %s <filename> ...\n", progname);
53b9ae52e3SPaul Richards }
54b9ae52e3SPaul Richards 
55b9ae52e3SPaul Richards int
56b9ae52e3SPaul Richards main(argc, argv)
57b9ae52e3SPaul Richards int	argc;
58b9ae52e3SPaul Richards char	*argv[];
59b9ae52e3SPaul Richards {
60b9ae52e3SPaul Richards 	int		rval = 0;
61b9ae52e3SPaul Richards 	int		c;
62b9ae52e3SPaul Richards 	extern int	optind;
63b9ae52e3SPaul Richards 
64b9ae52e3SPaul Richards 	if ((progname = strrchr(argv[0], '/')) == NULL)
65b9ae52e3SPaul Richards 		progname = argv[0];
66b9ae52e3SPaul Richards 	else
67b9ae52e3SPaul Richards 		progname++;
68b9ae52e3SPaul Richards 
69b9ae52e3SPaul Richards 	while ((c = getopt(argc, argv, "")) != EOF) {
70b9ae52e3SPaul Richards 		switch (c) {
71b9ae52e3SPaul Richards 		default:
72b9ae52e3SPaul Richards 			usage();
73b9ae52e3SPaul Richards 			exit(1);
74b9ae52e3SPaul Richards 		}
75b9ae52e3SPaul Richards 	}
76b9ae52e3SPaul Richards 	argc -= optind;
77b9ae52e3SPaul Richards 	argv += optind;
78b9ae52e3SPaul Richards 
79b9ae52e3SPaul Richards 	if (argc <= 0) {
80b9ae52e3SPaul Richards 		usage();
81b9ae52e3SPaul Richards 		exit(1);
82b9ae52e3SPaul Richards 	}
83b9ae52e3SPaul Richards 
84b9ae52e3SPaul Richards 	/* ld.so magic */
85b9ae52e3SPaul Richards 	setenv("LD_TRACE_LOADED_OBJECTS", "", 1);
86b9ae52e3SPaul Richards 
87b9ae52e3SPaul Richards 	while (argc--) {
88b9ae52e3SPaul Richards 		int	fd;
89b9ae52e3SPaul Richards 		struct exec hdr;
90b9ae52e3SPaul Richards 		int	status;
91b9ae52e3SPaul Richards 
92b9ae52e3SPaul Richards 		if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
93b9ae52e3SPaul Richards 			perror(*argv);
94b9ae52e3SPaul Richards 			rval |= 1;
95b9ae52e3SPaul Richards 			argv++;
96b9ae52e3SPaul Richards 			continue;
97b9ae52e3SPaul Richards 		}
98b9ae52e3SPaul Richards 		if (read(fd, &hdr, sizeof hdr) != sizeof hdr ||
99b9ae52e3SPaul Richards 					!(N_GETFLAG(hdr) & EX_DYNAMIC)) {
100b9ae52e3SPaul Richards 			fprintf(stderr, "%s: not a dynamic executable\n",
101b9ae52e3SPaul Richards 						*argv);
102b9ae52e3SPaul Richards 			(void)close(fd);
103b9ae52e3SPaul Richards 			rval |= 1;
104b9ae52e3SPaul Richards 			argv++;
105b9ae52e3SPaul Richards 			continue;
106b9ae52e3SPaul Richards 		}
107b9ae52e3SPaul Richards 		(void)close(fd);
108b9ae52e3SPaul Richards 
109b9ae52e3SPaul Richards 		printf("%s:\n", *argv);
11009e3d49dSJordan K. Hubbard 		fflush(stdout);
111b9ae52e3SPaul Richards 
112b9ae52e3SPaul Richards 		switch (fork()) {
113b9ae52e3SPaul Richards 		case -1:
114b9ae52e3SPaul Richards 			perror("fork");
115b9ae52e3SPaul Richards 			exit(1);
116b9ae52e3SPaul Richards 			break;
117b9ae52e3SPaul Richards 		default:
118b9ae52e3SPaul Richards 			if (wait(&status) <= 0)
119b9ae52e3SPaul Richards 				perror("wait");
120b9ae52e3SPaul Richards 
121b9ae52e3SPaul Richards 			if (WIFSIGNALED(status)) {
122b9ae52e3SPaul Richards 				fprintf(stderr, "%s: signal %d\n",
123b9ae52e3SPaul Richards 						*argv, WTERMSIG(status));
124b9ae52e3SPaul Richards 				rval |= 1;
125b9ae52e3SPaul Richards 			} else if (WIFEXITED(status) && WEXITSTATUS(status)) {
126b9ae52e3SPaul Richards 				fprintf(stderr, "%s: exit status %d\n",
127b9ae52e3SPaul Richards 						*argv, WEXITSTATUS(status));
128b9ae52e3SPaul Richards 				rval |= 1;
129b9ae52e3SPaul Richards 			}
130b9ae52e3SPaul Richards 			break;
131b9ae52e3SPaul Richards 		case 0:
132b9ae52e3SPaul Richards 			rval != execl(*argv, *argv, NULL) != 0;
133b9ae52e3SPaul Richards 			perror(*argv);
134b9ae52e3SPaul Richards 			_exit(1);
135b9ae52e3SPaul Richards 		}
136b9ae52e3SPaul Richards 		argv++;
137b9ae52e3SPaul Richards 	}
138b9ae52e3SPaul Richards 
139b9ae52e3SPaul Richards 	return rval;
140b9ae52e3SPaul Richards }
141