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$");
36 #include <sys/param.h>
39 #include <machine/elf.h>
41 #include <arpa/inet.h>
56 * 32-bit ELF data structures can only be used if the system header[s] declare
57 * them. There is no official macro for determining whether they are declared,
58 * so check for the existence of one of the 32-macros defined in elf(5).
61 #define ELF32_SUPPORTED
64 #define LDD_SETENV(name, value, overwrite) do { \
65 setenv("LD_" name, value, overwrite); \
66 setenv("LD_32_" name, value, overwrite); \
69 #define LDD_UNSETENV(name) do { \
70 unsetenv("LD_" name); \
71 unsetenv("LD_32_" name); \
74 static int is_executable(const char *fname, int fd, int *is_shlib,
76 static void usage(void);
78 #define TYPE_UNKNOWN 0
79 #define TYPE_ELF 1 /* Architecture default */
80 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
81 #define TYPE_ELF32 2 /* Explicit 32 bits on architectures >32 bits */
83 #define _PATH_LDD32 "/usr/bin/ldd32"
86 execldd32(char *file, char *fmt1, char *fmt2, int aflag, int vflag)
91 LDD_UNSETENV("TRACE_LOADED_OBJECTS");
94 argv[i++] = strdup(_PATH_LDD32);
96 argv[i++] = strdup("-a");
98 argv[i++] = strdup("-v");
100 argv[i++] = strdup("-f");
101 argv[i++] = strdup(fmt1);
104 argv[i++] = strdup("-f");
105 argv[i++] = strdup(fmt2);
107 argv[i++] = strdup(file);
115 execv(_PATH_LDD32, argv);
116 warn("%s", _PATH_LDD32);
120 if (wait(&status) < 0)
122 else if (WIFSIGNALED(status))
124 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
130 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
136 main(int argc, char *argv[])
139 int rval, c, aflag, vflag;
144 while ((c = getopt(argc, argv, "af:v")) != -1) {
152 errx(1, "too many formats");
168 if (vflag && fmt1 != NULL)
169 errx(1, "-v may not be used with -f");
177 for (; argc > 0; argc--, argv++) {
178 int fd, status, is_shlib, rv, type;
180 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
185 rv = is_executable(*argv, fd, &is_shlib, &type);
195 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
197 rval |= execldd32(*argv, fmt1, fmt2, aflag, vflag);
203 * This shouldn't happen unless is_executable()
206 errx(EDOOFUS, "unknown executable type");
210 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
212 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
214 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
216 LDD_SETENV("TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
218 LDD_SETENV("TRACE_LOADED_OBJECTS_ALL", "1", 1);
219 else if (fmt1 == NULL && fmt2 == NULL)
220 /* Default formats */
221 printf("%s:\n", *argv);
229 if (wait(&status) < 0) {
232 } else if (WIFSIGNALED(status)) {
233 fprintf(stderr, "%s: signal %d\n", *argv,
236 } else if (WIFEXITED(status) &&
237 WEXITSTATUS(status) != 0) {
238 fprintf(stderr, "%s: exit status %d\n", *argv,
239 WEXITSTATUS(status));
245 execl(*argv, *argv, (char *)NULL);
248 dlopen(*argv, RTLD_TRACE);
249 warnx("%s: %s", *argv, dlerror());
262 fprintf(stderr, "usage: ldd [-a] [-v] [-f format] program ...\n");
267 has_freebsd_abi_tag(const char *fname, Elf *elf, GElf_Ehdr *ehdr, off_t offset,
271 const Elf_Note *note;
275 size_t namesz, descsz;
278 buf = elf_rawfile(elf, NULL);
280 warnx("%s: %s", fname, elf_errmsg(0));
284 memset(&src, 0, sizeof(src));
285 src.d_buf = buf + offset;
287 src.d_type = ELF_T_NOTE;
288 src.d_version = EV_CURRENT;
290 memset(&dst, 0, sizeof(dst));
291 dst.d_buf = copy = malloc(len);
293 dst.d_type = ELF_T_NOTE;
294 dst.d_version = EV_CURRENT;
296 if (gelf_xlatetom(elf, &dst, &src, ehdr->e_ident[EI_DATA]) == NULL) {
297 warnx("%s: failed to parse notes: %s", fname, elf_errmsg(0));
305 if (len < sizeof(*note))
308 note = (const void *)buf;
309 buf += sizeof(*note);
310 len -= sizeof(*note);
312 namesz = roundup2(note->n_namesz, sizeof(uint32_t));
313 descsz = roundup2(note->n_descsz, sizeof(uint32_t));
314 if (len < namesz + descsz)
318 if (note->n_namesz == sizeof(ELF_NOTE_FREEBSD) &&
319 strncmp(name, ELF_NOTE_FREEBSD, note->n_namesz) == 0 &&
320 note->n_type == NT_FREEBSD_ABI_TAG &&
321 note->n_descsz == sizeof(uint32_t)) {
326 buf += namesz + descsz;
327 len -= namesz + descsz;
331 return (has_abi_tag);
335 is_pie(const char *fname, Elf *elf, GElf_Ehdr *ehdr, off_t offset, size_t len)
345 buf = elf_rawfile(elf, NULL);
347 warnx("%s: %s", fname, elf_errmsg(0));
351 dynsize = gelf_fsize(elf, ELF_T_DYN, 1, EV_CURRENT);
353 warnx("%s: %s", fname, elf_errmsg(0));
356 count = len / dynsize;
358 memset(&src, 0, sizeof(src));
359 src.d_buf = buf + offset;
361 src.d_type = ELF_T_DYN;
362 src.d_version = EV_CURRENT;
364 memset(&dst, 0, sizeof(dst));
365 dst.d_buf = copy = malloc(count * sizeof(*dyn));
366 dst.d_size = count * sizeof(*dyn);
367 dst.d_type = ELF_T_DYN;
368 dst.d_version = EV_CURRENT;
370 if (gelf_xlatetom(elf, &dst, &src, ehdr->e_ident[EI_DATA]) == NULL) {
371 warnx("%s: failed to parse .dynamic: %s", fname, elf_errmsg(0));
378 for (i = 0; i < count; i++) {
379 if (dyn[i].d_tag != DT_FLAGS_1)
382 pie = (dyn[i].d_un.d_val & DF_1_PIE) != 0;
391 is_executable(const char *fname, int fd, int *is_shlib, int *type)
396 bool dynamic, freebsd, pie;
400 *type = TYPE_UNKNOWN;
405 if (elf_version(EV_CURRENT) == EV_NONE) {
406 warnx("unsupported libelf");
409 elf = elf_begin(fd, ELF_C_READ, NULL);
411 warnx("%s: %s", fname, elf_errmsg(0));
414 if (elf_kind(elf) != ELF_K_ELF) {
416 warnx("%s: not a dynamic ELF executable", fname);
419 if (gelf_getehdr(elf, &ehdr) == NULL) {
420 warnx("%s: %s", fname, elf_errmsg(0));
426 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
427 if (gelf_getclass(elf) == ELFCLASS32) {
432 freebsd = ehdr.e_ident[EI_OSABI] == ELFOSABI_FREEBSD;
433 for (i = 0; i < ehdr.e_phnum; i++) {
434 if (gelf_getphdr(elf, i, &phdr) == NULL) {
435 warnx("%s: %s", fname, elf_errmsg(0));
439 switch (phdr.p_type) {
441 if (ehdr.e_ident[EI_OSABI] == ELFOSABI_NONE && !freebsd)
442 freebsd = has_freebsd_abi_tag(fname, elf, &ehdr,
443 phdr.p_offset, phdr.p_filesz);
447 if (ehdr.e_type == ET_DYN)
448 pie = is_pie(fname, elf, &ehdr, phdr.p_offset,
456 warnx("%s: not a dynamic ELF executable", fname);
460 if (ehdr.e_type == ET_DYN && !pie) {
465 warnx("%s: not a FreeBSD ELF shared object", fname);