Deleted Added
full compact
ldd.c (180235) ldd.c (180236)
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

--- 15 unchanged lines hidden (view full) ---

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
31#include <sys/cdefs.h>
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

--- 15 unchanged lines hidden (view full) ---

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
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/usr.bin/ldd/ldd.c 180235 2008-07-03 22:30:18Z edwin $");
32__FBSDID("$FreeBSD: head/usr.bin/ldd/ldd.c 180236 2008-07-03 22:37:51Z edwin $");
33
34#include <sys/wait.h>
35
36#include <machine/elf.h>
37
38#include <arpa/inet.h>
39
40#include <a.out.h>
41#include <dlfcn.h>
42#include <err.h>
33
34#include <sys/wait.h>
35
36#include <machine/elf.h>
37
38#include <arpa/inet.h>
39
40#include <a.out.h>
41#include <dlfcn.h>
42#include <err.h>
43#include <errno.h>
43#include <fcntl.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <unistd.h>
47
48#include "extern.h"
49
50static int is_executable(const char *fname, int fd, int *is_shlib,
51 int *type);
52static void usage(void);
53
54#define TYPE_UNKNOWN 0
55#define TYPE_AOUT 1
56#define TYPE_ELF 2 /* Architecture default */
44#include <fcntl.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <unistd.h>
48
49#include "extern.h"
50
51static int is_executable(const char *fname, int fd, int *is_shlib,
52 int *type);
53static void usage(void);
54
55#define TYPE_UNKNOWN 0
56#define TYPE_AOUT 1
57#define TYPE_ELF 2 /* Architecture default */
58#if __ELF_WORD_SIZE > 32
59#define TYPE_ELF32 3 /* Explicit 32 bits on architectures >32 bits */
60#endif
57
58#define ENV_OBJECTS 0
59#define ENV_OBJECTS_FMT1 1
60#define ENV_OBJECTS_FMT2 2
61#define ENV_OBJECTS_PROGNAME 3
62#define ENV_OBJECTS_ALL 4
63#define ENV_LAST 5
64
65const char *envdef[ENV_LAST] = {
66 "LD_TRACE_LOADED_OBJECTS",
67 "LD_TRACE_LOADED_OBJECTS_FMT1",
68 "LD_TRACE_LOADED_OBJECTS_FMT2",
69 "LD_TRACE_LOADED_OBJECTS_PROGNAME",
70 "LD_TRACE_LOADED_OBJECTS_ALL",
71};
61
62#define ENV_OBJECTS 0
63#define ENV_OBJECTS_FMT1 1
64#define ENV_OBJECTS_FMT2 2
65#define ENV_OBJECTS_PROGNAME 3
66#define ENV_OBJECTS_ALL 4
67#define ENV_LAST 5
68
69const char *envdef[ENV_LAST] = {
70 "LD_TRACE_LOADED_OBJECTS",
71 "LD_TRACE_LOADED_OBJECTS_FMT1",
72 "LD_TRACE_LOADED_OBJECTS_FMT2",
73 "LD_TRACE_LOADED_OBJECTS_PROGNAME",
74 "LD_TRACE_LOADED_OBJECTS_ALL",
75};
76#if __ELF_WORD_SIZE > 32
77const char *env32[ENV_LAST] = {
78 "LD_32_TRACE_LOADED_OBJECTS",
79 "LD_32_TRACE_LOADED_OBJECTS_FMT1",
80 "LD_32_TRACE_LOADED_OBJECTS_FMT2",
81 "LD_32_TRACE_LOADED_OBJECTS_PROGNAME",
82 "LD_32_TRACE_LOADED_OBJECTS_ALL",
83};
84#endif
72
73int
74main(int argc, char *argv[])
75{
76 char *fmt1, *fmt2;
77 int rval, c, aflag, vflag;
78
79 aflag = vflag = 0;

--- 39 unchanged lines hidden (view full) ---

119 }
120#endif
121
122 rval = 0;
123 for (; argc > 0; argc--, argv++) {
124 int fd, status, is_shlib, rv, type;
125 const char **env;
126
85
86int
87main(int argc, char *argv[])
88{
89 char *fmt1, *fmt2;
90 int rval, c, aflag, vflag;
91
92 aflag = vflag = 0;

--- 39 unchanged lines hidden (view full) ---

132 }
133#endif
134
135 rval = 0;
136 for (; argc > 0; argc--, argv++) {
137 int fd, status, is_shlib, rv, type;
138 const char **env;
139
127 env = envdef; /* Temporary placeholder */
128
129 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
130 warn("%s", *argv);
131 rval |= 1;
132 continue;
133 }
134 rv = is_executable(*argv, fd, &is_shlib, &type);
135 close(fd);
136 if (rv == 0) {
137 rval |= 1;
138 continue;
139 }
140
140 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
141 warn("%s", *argv);
142 rval |= 1;
143 continue;
144 }
145 rv = is_executable(*argv, fd, &is_shlib, &type);
146 close(fd);
147 if (rv == 0) {
148 rval |= 1;
149 continue;
150 }
151
152 switch (type) {
153 case TYPE_ELF:
154 case TYPE_AOUT:
155 env = envdef;
156 break;
157#if __ELF_WORD_SIZE > 32
158 case TYPE_ELF32:
159 env = env32;
160 break;
161#endif
162 case TYPE_UNKNOWN:
163 default:
164 /*
165 * This shouldn't happen unless is_executable()
166 * is broken.
167 */
168 errx(EDOOFUS, "unknown executable type");
169 }
170
141 /* ld.so magic */
142 setenv(env[ENV_OBJECTS], "yes", 1);
143 if (fmt1 != NULL)
144 setenv(env[ENV_OBJECTS_FMT1], fmt1, 1);
145 if (fmt2 != NULL)
146 setenv(env[ENV_OBJECTS_FMT2], fmt2, 1);
147
148 setenv(env[ENV_OBJECTS_PROGNAME], *argv, 1);

--- 45 unchanged lines hidden (view full) ---

194 exit(1);
195}
196
197static int
198is_executable(const char *fname, int fd, int *is_shlib, int *type)
199{
200 union {
201 struct exec aout;
171 /* ld.so magic */
172 setenv(env[ENV_OBJECTS], "yes", 1);
173 if (fmt1 != NULL)
174 setenv(env[ENV_OBJECTS_FMT1], fmt1, 1);
175 if (fmt2 != NULL)
176 setenv(env[ENV_OBJECTS_FMT2], fmt2, 1);
177
178 setenv(env[ENV_OBJECTS_PROGNAME], *argv, 1);

--- 45 unchanged lines hidden (view full) ---

224 exit(1);
225}
226
227static int
228is_executable(const char *fname, int fd, int *is_shlib, int *type)
229{
230 union {
231 struct exec aout;
232 Elf32_Ehdr elf32;
202 Elf_Ehdr elf;
203 } hdr;
204 int n;
205
206 *is_shlib = 0;
207 *type = TYPE_UNKNOWN;
208
209 if ((n = read(fd, &hdr, sizeof(hdr))) == -1) {

--- 10 unchanged lines hidden (view full) ---

220 ) {
221 warnx("%s: not a dynamic executable", fname);
222 return (0);
223 }
224 *type = TYPE_AOUT;
225 return (1);
226 }
227
233 Elf_Ehdr elf;
234 } hdr;
235 int n;
236
237 *is_shlib = 0;
238 *type = TYPE_UNKNOWN;
239
240 if ((n = read(fd, &hdr, sizeof(hdr))) == -1) {

--- 10 unchanged lines hidden (view full) ---

251 ) {
252 warnx("%s: not a dynamic executable", fname);
253 return (0);
254 }
255 *type = TYPE_AOUT;
256 return (1);
257 }
258
259#if __ELF_WORD_SIZE > 32
260 if ((size_t)n >= sizeof(hdr.elf32) && IS_ELF(hdr.elf32) &&
261 hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
262 /* Handle 32 bit ELF objects */
263 Elf32_Phdr phdr;
264 int dynamic, i;
265
266 dynamic = 0;
267 *type = TYPE_ELF32;
268
269 if (lseek(fd, hdr.elf32.e_phoff, SEEK_SET) == -1) {
270 warnx("%s: header too short", fname);
271 return (0);
272 }
273 for (i = 0; i < hdr.elf32.e_phnum; i++) {
274 if (read(fd, &phdr, hdr.elf32.e_phentsize) !=
275 sizeof(phdr)) {
276 warnx("%s: can't read program header", fname);
277 return (0);
278 }
279 if (phdr.p_type == PT_DYNAMIC) {
280 dynamic = 1;
281 break;
282 }
283 }
284
285 if (!dynamic) {
286 warnx("%s: not a dynamic ELF executable", fname);
287 return (0);
288 }
289 if (hdr.elf32.e_type == ET_DYN) {
290 if (hdr.elf32.e_ident[EI_OSABI] & ELFOSABI_FREEBSD) {
291 *is_shlib = 1;
292 return (1);
293 }
294 warnx("%s: not a FreeBSD ELF shared object", fname);
295 return (0);
296 }
297
298 return (1);
299 }
300#endif
301
228 if ((size_t)n >= sizeof(hdr.elf) && IS_ELF(hdr.elf) &&
229 hdr.elf.e_ident[EI_CLASS] == ELF_TARG_CLASS) {
230 /* Handle default ELF objects on this architecture */
231 Elf_Phdr phdr;
232 int dynamic, i;
233
234 dynamic = 0;
235 *type = TYPE_ELF;

--- 36 unchanged lines hidden ---
302 if ((size_t)n >= sizeof(hdr.elf) && IS_ELF(hdr.elf) &&
303 hdr.elf.e_ident[EI_CLASS] == ELF_TARG_CLASS) {
304 /* Handle default ELF objects on this architecture */
305 Elf_Phdr phdr;
306 int dynamic, i;
307
308 dynamic = 0;
309 *type = TYPE_ELF;

--- 36 unchanged lines hidden ---