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>
51 /* We don't support a.out executables on arm64 and riscv */
52 #if !defined(__aarch64__) && !defined(__riscv__)
54 #define AOUT_SUPPORTED
58 * 32-bit ELF data structures can only be used if the system header[s] declare
59 * them. There is no official macro for determining whether they are declared,
60 * so check for the existence of one of the 32-macros defined in elf(5).
63 #define ELF32_SUPPORTED
66 #define LDD_SETENV(name, value, overwrite) do { \
67 setenv("LD_" name, value, overwrite); \
68 setenv("LD_32_" name, value, overwrite); \
71 #define LDD_UNSETENV(name) do { \
72 unsetenv("LD_" name); \
73 unsetenv("LD_32_" name); \
76 static int is_executable(const char *fname
, int fd
, int *is_shlib
,
78 static void usage(void);
80 #define TYPE_UNKNOWN 0
82 #define TYPE_ELF 2 /* Architecture default */
83 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
84 #define TYPE_ELF32 3 /* Explicit 32 bits on architectures >32 bits */
86 #define _PATH_LDD32 "/usr/bin/ldd32"
89 execldd32(char *file
, char *fmt1
, char *fmt2
, int aflag
, int vflag
)
94 LDD_UNSETENV("TRACE_LOADED_OBJECTS");
97 argv
[i
++] = strdup(_PATH_LDD32
);
99 argv
[i
++] = strdup("-a");
101 argv
[i
++] = strdup("-v");
103 argv
[i
++] = strdup("-f");
104 argv
[i
++] = strdup(fmt1
);
107 argv
[i
++] = strdup("-f");
108 argv
[i
++] = strdup(fmt2
);
110 argv
[i
++] = strdup(file
);
118 execv(_PATH_LDD32
, argv
);
119 warn("%s", _PATH_LDD32
);
123 if (wait(&status
) < 0)
125 else if (WIFSIGNALED(status
))
127 else if (WIFEXITED(status
) && WEXITSTATUS(status
) != 0)
133 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
139 main(int argc
, char *argv
[])
142 int rval
, c
, aflag
, vflag
;
147 while ((c
= getopt(argc
, argv
, "af:v")) != -1) {
155 errx(1, "too many formats");
171 if (vflag
&& fmt1
!= NULL
)
172 errx(1, "-v may not be used with -f");
181 for (c
= 0; c
< argc
; c
++)
183 exit(error_count
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
188 for (; argc
> 0; argc
--, argv
++) {
189 int fd
, status
, is_shlib
, rv
, type
;
191 if ((fd
= open(*argv
, O_RDONLY
, 0)) < 0) {
196 rv
= is_executable(*argv
, fd
, &is_shlib
, &type
);
207 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
209 rval
|= execldd32(*argv
, fmt1
, fmt2
, aflag
, vflag
);
215 * This shouldn't happen unless is_executable()
218 errx(EDOOFUS
, "unknown executable type");
222 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
224 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT1", fmt1
, 1);
226 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT2", fmt2
, 1);
228 LDD_SETENV("TRACE_LOADED_OBJECTS_PROGNAME", *argv
, 1);
230 LDD_SETENV("TRACE_LOADED_OBJECTS_ALL", "1", 1);
231 else if (fmt1
== NULL
&& fmt2
== NULL
)
232 /* Default formats */
233 printf("%s:\n", *argv
);
241 if (wait(&status
) < 0) {
244 } else if (WIFSIGNALED(status
)) {
245 fprintf(stderr
, "%s: signal %d\n", *argv
,
248 } else if (WIFEXITED(status
) &&
249 WEXITSTATUS(status
) != 0) {
250 fprintf(stderr
, "%s: exit status %d\n", *argv
,
251 WEXITSTATUS(status
));
257 execl(*argv
, *argv
, (char *)NULL
);
260 dlopen(*argv
, RTLD_TRACE
);
261 warnx("%s: %s", *argv
, dlerror());
274 fprintf(stderr
, "usage: ldd [-a] [-v] [-f format] program ...\n");
279 is_executable(const char *fname
, int fd
, int *is_shlib
, int *type
)
282 #ifdef AOUT_SUPPORTED
285 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
293 *type
= TYPE_UNKNOWN
;
295 if ((n
= read(fd
, &hdr
, sizeof(hdr
))) == -1) {
296 warn("%s: can't read program header", fname
);
300 #ifdef AOUT_SUPPORTED
301 if ((size_t)n
>= sizeof(hdr
.aout
) && !N_BADMAG(hdr
.aout
)) {
303 if ((N_GETFLAG(hdr
.aout
) & EX_DPMASK
) != EX_DYNAMIC
304 #if 1 /* Compatibility */
305 || hdr
.aout
.a_entry
< __LDPGSZ
308 warnx("%s: not a dynamic executable", fname
);
316 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
317 if ((size_t)n
>= sizeof(hdr
.elf32
) && IS_ELF(hdr
.elf32
) &&
318 hdr
.elf32
.e_ident
[EI_CLASS
] == ELFCLASS32
) {
319 /* Handle 32 bit ELF objects */
326 if (lseek(fd
, hdr
.elf32
.e_phoff
, SEEK_SET
) == -1) {
327 warnx("%s: header too short", fname
);
330 for (i
= 0; i
< hdr
.elf32
.e_phnum
; i
++) {
331 if (read(fd
, &phdr
, hdr
.elf32
.e_phentsize
) !=
333 warnx("%s: can't read program header", fname
);
336 if (phdr
.p_type
== PT_DYNAMIC
) {
343 warnx("%s: not a dynamic ELF executable", fname
);
346 if (hdr
.elf32
.e_type
== ET_DYN
) {
347 if (hdr
.elf32
.e_ident
[EI_OSABI
] == ELFOSABI_FREEBSD
) {
351 warnx("%s: not a FreeBSD ELF shared object", fname
);
359 if ((size_t)n
>= sizeof(hdr
.elf
) && IS_ELF(hdr
.elf
) &&
360 hdr
.elf
.e_ident
[EI_CLASS
] == ELF_TARG_CLASS
) {
361 /* Handle default ELF objects on this architecture */
368 if (lseek(fd
, hdr
.elf
.e_phoff
, SEEK_SET
) == -1) {
369 warnx("%s: header too short", fname
);
372 for (i
= 0; i
< hdr
.elf
.e_phnum
; i
++) {
373 if (read(fd
, &phdr
, hdr
.elf
.e_phentsize
)
375 warnx("%s: can't read program header", fname
);
378 if (phdr
.p_type
== PT_DYNAMIC
) {
385 warnx("%s: not a dynamic ELF executable", fname
);
388 if (hdr
.elf
.e_type
== ET_DYN
) {
389 switch (hdr
.elf
.e_ident
[EI_OSABI
]) {
390 case ELFOSABI_FREEBSD
:
395 if (hdr
.elf
.e_machine
!= EM_ARM
)
397 if (EF_ARM_EABI_VERSION(hdr
.elf
.e_flags
) <
398 EF_ARM_EABI_FREEBSD_MIN
)
404 warnx("%s: not a FreeBSD ELF shared object", fname
);
411 warnx("%s: not a dynamic executable", fname
);