2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1993 Paul Kranenburg
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Paul Kranenburg.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
38 #include <machine/elf.h>
40 #include <arpa/inet.h>
53 /* We don't support a.out executables on arm64 and riscv */
54 #if !defined(__aarch64__) && !defined(__riscv)
56 #define AOUT_SUPPORTED
60 * 32-bit ELF data structures can only be used if the system header[s] declare
61 * them. There is no official macro for determining whether they are declared,
62 * so check for the existence of one of the 32-macros defined in elf(5).
65 #define ELF32_SUPPORTED
68 #define LDD_SETENV(name, value, overwrite) do { \
69 setenv("LD_" name, value, overwrite); \
70 setenv("LD_32_" name, value, overwrite); \
73 #define LDD_UNSETENV(name) do { \
74 unsetenv("LD_" name); \
75 unsetenv("LD_32_" name); \
78 static int is_executable(const char *fname, int fd, int *is_shlib,
80 static void usage(void);
82 #define TYPE_UNKNOWN 0
84 #define TYPE_ELF 2 /* Architecture default */
85 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
86 #define TYPE_ELF32 3 /* Explicit 32 bits on architectures >32 bits */
88 #define _PATH_LDD32 "/usr/bin/ldd32"
91 execldd32(char *file, char *fmt1, char *fmt2, int aflag, int vflag)
96 LDD_UNSETENV("TRACE_LOADED_OBJECTS");
99 argv[i++] = strdup(_PATH_LDD32);
101 argv[i++] = strdup("-a");
103 argv[i++] = strdup("-v");
105 argv[i++] = strdup("-f");
106 argv[i++] = strdup(fmt1);
109 argv[i++] = strdup("-f");
110 argv[i++] = strdup(fmt2);
112 argv[i++] = strdup(file);
120 execv(_PATH_LDD32, argv);
121 warn("%s", _PATH_LDD32);
125 if (wait(&status) < 0)
127 else if (WIFSIGNALED(status))
129 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
135 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
141 main(int argc, char *argv[])
144 int rval, c, aflag, vflag;
149 while ((c = getopt(argc, argv, "af:v")) != -1) {
157 errx(1, "too many formats");
173 if (vflag && fmt1 != NULL)
174 errx(1, "-v may not be used with -f");
183 for (c = 0; c < argc; c++)
185 exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
190 for (; argc > 0; argc--, argv++) {
191 int fd, status, is_shlib, rv, type;
193 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
198 rv = is_executable(*argv, fd, &is_shlib, &type);
209 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
211 rval |= execldd32(*argv, fmt1, fmt2, aflag, vflag);
217 * This shouldn't happen unless is_executable()
220 errx(EDOOFUS, "unknown executable type");
224 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
226 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
228 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
230 LDD_SETENV("TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
232 LDD_SETENV("TRACE_LOADED_OBJECTS_ALL", "1", 1);
233 else if (fmt1 == NULL && fmt2 == NULL)
234 /* Default formats */
235 printf("%s:\n", *argv);
243 if (wait(&status) < 0) {
246 } else if (WIFSIGNALED(status)) {
247 fprintf(stderr, "%s: signal %d\n", *argv,
250 } else if (WIFEXITED(status) &&
251 WEXITSTATUS(status) != 0) {
252 fprintf(stderr, "%s: exit status %d\n", *argv,
253 WEXITSTATUS(status));
259 execl(*argv, *argv, (char *)NULL);
262 dlopen(*argv, RTLD_TRACE);
263 warnx("%s: %s", *argv, dlerror());
276 fprintf(stderr, "usage: ldd [-a] [-v] [-f format] program ...\n");
281 is_executable(const char *fname, int fd, int *is_shlib, int *type)
284 #ifdef AOUT_SUPPORTED
287 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
295 *type = TYPE_UNKNOWN;
297 if ((n = read(fd, &hdr, sizeof(hdr))) == -1) {
298 warn("%s: can't read program header", fname);
302 #ifdef AOUT_SUPPORTED
303 if ((size_t)n >= sizeof(hdr.aout) && !N_BADMAG(hdr.aout)) {
305 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
306 #if 1 /* Compatibility */
307 || hdr.aout.a_entry < __LDPGSZ
310 warnx("%s: not a dynamic executable", fname);
314 warnx("%s: aout support is deprecated", fname);
319 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
320 if ((size_t)n >= sizeof(hdr.elf32) && IS_ELF(hdr.elf32) &&
321 hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
322 /* Handle 32 bit ELF objects */
329 if (lseek(fd, hdr.elf32.e_phoff, SEEK_SET) == -1) {
330 warnx("%s: header too short", fname);
333 for (i = 0; i < hdr.elf32.e_phnum; i++) {
334 if (read(fd, &phdr, hdr.elf32.e_phentsize) !=
336 warnx("%s: can't read program header", fname);
339 if (phdr.p_type == PT_DYNAMIC) {
346 warnx("%s: not a dynamic ELF executable", fname);
349 if (hdr.elf32.e_type == ET_DYN) {
350 if (hdr.elf32.e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
354 warnx("%s: not a FreeBSD ELF shared object", fname);
362 if ((size_t)n >= sizeof(hdr.elf) && IS_ELF(hdr.elf) &&
363 hdr.elf.e_ident[EI_CLASS] == ELF_TARG_CLASS) {
364 /* Handle default ELF objects on this architecture */
371 if (lseek(fd, hdr.elf.e_phoff, SEEK_SET) == -1) {
372 warnx("%s: header too short", fname);
375 for (i = 0; i < hdr.elf.e_phnum; i++) {
376 if (read(fd, &phdr, hdr.elf.e_phentsize)
378 warnx("%s: can't read program header", fname);
381 if (phdr.p_type == PT_DYNAMIC) {
388 warnx("%s: not a dynamic ELF executable", fname);
391 if (hdr.elf.e_type == ET_DYN) {
392 switch (hdr.elf.e_ident[EI_OSABI]) {
393 case ELFOSABI_FREEBSD:
398 if (hdr.elf.e_machine != EM_ARM)
400 if (EF_ARM_EABI_VERSION(hdr.elf.e_flags) <
401 EF_ARM_EABI_FREEBSD_MIN)
407 warnx("%s: not a FreeBSD ELF shared object", fname);
414 warnx("%s: not a dynamic executable", fname);