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