1 /**	$MirOS: src/libexec/ld.so/ldd/ldd.c,v 1.3 2006/08/30 03:52:53 tg Exp $ */
2 /*	$OpenBSD: ldd.c,v 1.11 2003/07/10 00:04:28 david Exp $	*/
3 /*
4  * Copyright (c) 2001 Artur Grabowski <art@openbsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <sys/wait.h>
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <dlfcn.h>
36 #include <elf_abi.h>
37 #include <err.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 __RCSID("$MirOS: src/libexec/ld.so/ldd/ldd.c,v 1.3 2006/08/30 03:52:53 tg Exp $");
43 
44 __dead int usage(void);
45 int doit(char *);
46 
47 int
main(int argc,char ** argv)48 main(int argc, char **argv)
49 {
50 	int c, xflag, ret;
51 
52 	xflag = 0;
53 	while ((c = getopt(argc, argv, "x")) != -1) {
54 		switch (c) {
55 		case 'x':
56 			xflag = 1;
57 			break;
58 		default:
59 			usage();
60 			/*NOTREACHED*/
61 		}
62 	}
63 
64 	if (xflag)
65 		errx(1, "-x not yet implemented");
66 
67 	argc -= optind;
68 	argv += optind;
69 
70 	if (argc == 0)
71 		usage();
72 
73 	if (setenv("LD_TRACE_LOADED_OBJECTS", "true", 1) < 0)
74 		err(1, "setenv(LD_TRACE_LOADED_OBJECTS)");
75 
76 	ret = 0;
77 	while (argc--) {
78 		ret |= doit(*argv);
79 		argv++;
80 	}
81 
82 	return ret;
83 }
84 
85 int
usage(void)86 usage(void)
87 {
88 	extern char *__progname;
89 
90 	fprintf(stderr, "Usage: %s [-x] <filename> ...\n", __progname);
91 	exit(1);
92 }
93 
94 
95 int
doit(char * name)96 doit(char *name)
97 {
98 	Elf_Ehdr ehdr;
99 	Elf_Phdr *phdr;
100 	int fd, i, size, status;
101 	char buf[MAXPATHLEN];
102 	void *dlhandle;
103 
104 	if ((fd = open(name, O_RDONLY)) < 0) {
105 		warn("%s", name);
106 		return 1;
107 	}
108 
109 	if (read(fd, &ehdr, sizeof(ehdr)) < 0) {
110 		warn("read(%s)", name);
111 		close(fd);
112 		return 1;
113 	}
114 
115 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) ||
116 	    ehdr.e_machine != ELF_TARG_MACH) {
117 		warnx("%s: not an ELF executable", name);
118 		close(fd);
119 		return 1;
120 	}
121 
122 	if (ehdr.e_type == ET_DYN) {
123 		printf("%s:\n", name);
124 		if (realpath(name, buf) == NULL) {
125 			warn("realpath(%s)", name);
126 			return (1);
127 		}
128 		fflush(stdout);
129 		if ((dlhandle = dlopen(buf, RTLD_TRACE)) == NULL) {
130 			printf("%s\n", dlerror());
131 			return (1);
132 		}
133 		close(fd);
134 		return (0);
135 	}
136 
137 	size = ehdr.e_phnum * sizeof(Elf_Phdr);
138 	if ((phdr = malloc(size)) == NULL)
139 		err(1, "malloc");
140 
141 	if (pread(fd, phdr, size, ehdr.e_phoff) != size) {
142 		warn("read(%s)", name);
143 		close(fd);
144 		free(phdr);
145 		return 1;
146 	}
147 	for (i = 0; i < ehdr.e_phnum; i++)
148 		if (phdr[i].p_type == PT_DYNAMIC)
149 			break;
150 	close(fd);
151 	free(phdr);
152 
153 	if (i == ehdr.e_phnum) {
154 		warnx("%s: not a dynamic executable", name);
155 		return 1;
156 	}
157 
158 	printf("%s:\n", name);
159 	fflush(stdout);
160 	switch (fork()) {
161 	case -1:
162 		err(1, "fork");
163 	case 0:
164 		execl(name, name, (char *)NULL);
165 		perror(name);
166 		_exit(1);
167 	default:
168 		if (wait(&status) < 0) {
169 			warn("wait");
170 			return 1;
171 		}
172 		if (WIFSIGNALED(status)) {
173 			fprintf(stderr, "%s: signal %d\n", name,
174 			    WTERMSIG(status));
175 			return 1;
176 		}
177 		if (WEXITSTATUS(status)) {
178 			fprintf(stderr, "%s: exit status %d\n", name,
179 			    WEXITSTATUS(status));
180 			return 1;
181 		}
182 	}
183 
184 	return 0;
185 }
186