2 * Copyright (c) 1993 Paul Kranenburg
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Paul Kranenburg.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include <machine/elf.h>
38 #include <arpa/inet.h>
59 * 32-bit ELF data structures can only be used if the system header[s] declare
60 * them. There is no official macro for determining whether they are declared,
61 * so check for the existence of one of the 32-macros defined in elf(5).
64 #define ELF32_SUPPORTED
67 static int is_executable(const char *fname, int fd, int *is_shlib,
69 static void usage(void);
71 #define TYPE_UNKNOWN 0
73 #define TYPE_ELF 2 /* Architecture default */
74 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
75 #define TYPE_ELF32 3 /* Explicit 32 bits on architectures >32 bits */
77 #define _PATH_LDD32 "/usr/bin/ldd32"
80 execldd32(char *file, char *fmt1, char *fmt2, int aflag, int vflag)
85 unsetenv(LD_ "TRACE_LOADED_OBJECTS");
88 argv[i++] = strdup(_PATH_LDD32);
90 argv[i++] = strdup("-a");
92 argv[i++] = strdup("-v");
94 argv[i++] = strdup("-f");
95 argv[i++] = strdup(fmt1);
98 argv[i++] = strdup("-f");
99 argv[i++] = strdup(fmt2);
101 argv[i++] = strdup(file);
109 execv(_PATH_LDD32, argv);
110 warn("%s", _PATH_LDD32);
114 if (wait(&status) < 0)
116 else if (WIFSIGNALED(status))
118 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
124 setenv(LD_ "TRACE_LOADED_OBJECTS", "yes", 1);
130 main(int argc, char *argv[])
133 int rval, c, aflag, vflag;
138 while ((c = getopt(argc, argv, "af:v")) != -1) {
146 errx(1, "too many formats");
162 if (vflag && fmt1 != NULL)
163 errx(1, "-v may not be used with -f");
172 for (c = 0; c < argc; c++)
174 exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
179 for (; argc > 0; argc--, argv++) {
180 int fd, status, is_shlib, rv, type;
182 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
187 rv = is_executable(*argv, fd, &is_shlib, &type);
198 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
200 rval |= execldd32(*argv, fmt1, fmt2, aflag, vflag);
206 * This shouldn't happen unless is_executable()
209 errx(EDOOFUS, "unknown executable type");
213 setenv(LD_ "TRACE_LOADED_OBJECTS", "yes", 1);
215 setenv(LD_ "TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
217 setenv(LD_ "TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
219 setenv(LD_ "TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
221 setenv(LD_ "TRACE_LOADED_OBJECTS_ALL", "1", 1);
222 else if (fmt1 == NULL && fmt2 == NULL)
223 /* Default formats */
224 printf("%s:\n", *argv);
232 if (wait(&status) < 0) {
235 } else if (WIFSIGNALED(status)) {
236 fprintf(stderr, "%s: signal %d\n", *argv,
239 } else if (WIFEXITED(status) &&
240 WEXITSTATUS(status) != 0) {
241 fprintf(stderr, "%s: exit status %d\n", *argv,
242 WEXITSTATUS(status));
248 execl(*argv, *argv, (char *)NULL);
251 dlopen(*argv, RTLD_TRACE);
252 warnx("%s: %s", *argv, dlerror());
265 fprintf(stderr, "usage: ldd [-a] [-v] [-f format] program ...\n");
270 is_executable(const char *fname, int fd, int *is_shlib, int *type)
274 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
282 *type = TYPE_UNKNOWN;
284 if ((n = read(fd, &hdr, sizeof(hdr))) == -1) {
285 warn("%s: can't read program header", fname);
289 if ((size_t)n >= sizeof(hdr.aout) && !N_BADMAG(hdr.aout)) {
291 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
292 #if 1 /* Compatibility */
293 || hdr.aout.a_entry < __LDPGSZ
296 warnx("%s: not a dynamic executable", fname);
303 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
304 if ((size_t)n >= sizeof(hdr.elf32) && IS_ELF(hdr.elf32) &&
305 hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
306 /* Handle 32 bit ELF objects */
313 if (lseek(fd, hdr.elf32.e_phoff, SEEK_SET) == -1) {
314 warnx("%s: header too short", fname);
317 for (i = 0; i < hdr.elf32.e_phnum; i++) {
318 if (read(fd, &phdr, hdr.elf32.e_phentsize) !=
320 warnx("%s: can't read program header", fname);
323 if (phdr.p_type == PT_DYNAMIC) {
330 warnx("%s: not a dynamic ELF executable", fname);
333 if (hdr.elf32.e_type == ET_DYN) {
334 if (hdr.elf32.e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
338 warnx("%s: not a FreeBSD ELF shared object", fname);
346 if ((size_t)n >= sizeof(hdr.elf) && IS_ELF(hdr.elf) &&
347 hdr.elf.e_ident[EI_CLASS] == ELF_TARG_CLASS) {
348 /* Handle default ELF objects on this architecture */
355 if (lseek(fd, hdr.elf.e_phoff, SEEK_SET) == -1) {
356 warnx("%s: header too short", fname);
359 for (i = 0; i < hdr.elf.e_phnum; i++) {
360 if (read(fd, &phdr, hdr.elf.e_phentsize)
362 warnx("%s: can't read program header", fname);
365 if (phdr.p_type == PT_DYNAMIC) {
372 warnx("%s: not a dynamic ELF executable", fname);
375 if (hdr.elf.e_type == ET_DYN) {
376 if (hdr.elf.e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
380 warnx("%s: not a FreeBSD ELF shared object", fname);
387 warnx("%s: not a dynamic executable", fname);