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/param.h>
36 #include <machine/elf.h>
38 #include <arpa/inet.h>
46 #include <rtld_paths.h>
54 * 32-bit ELF data structures can only be used if the system header[s] declare
55 * them. There is no official macro for determining whether they are declared,
56 * so check for the existence of one of the 32-macros defined in elf(5).
59 #define ELF32_SUPPORTED
62 #define LDD_SETENV(name, value, overwrite) do { \
63 setenv("LD_" name, value, overwrite); \
64 setenv("LD_32_" name, value, overwrite); \
67 #define LDD_UNSETENV(name) do { \
68 unsetenv("LD_" name); \
69 unsetenv("LD_32_" name); \
72 static int is_executable(const char *fname, int fd, int *is_shlib,
74 static void usage(void);
76 #define TYPE_UNKNOWN 0
77 #define TYPE_ELF 1 /* Architecture default */
78 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
79 #define TYPE_ELF32 2 /* Explicit 32 bits on architectures >32 bits */
81 #define _PATH_LDD32 "/usr/bin/ldd32"
84 execldd32(char *file, char *fmt1, char *fmt2, int aflag)
89 LDD_UNSETENV("TRACE_LOADED_OBJECTS");
92 argv[i++] = strdup(_PATH_LDD32);
94 argv[i++] = strdup("-a");
96 argv[i++] = strdup("-f");
97 argv[i++] = strdup(fmt1);
100 argv[i++] = strdup("-f");
101 argv[i++] = strdup(fmt2);
103 argv[i++] = strdup(file);
111 execv(_PATH_LDD32, argv);
112 warn("%s", _PATH_LDD32);
116 if (wait(&status) < 0)
118 else if (WIFSIGNALED(status))
120 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
126 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
132 main(int argc, char *argv[])
136 int aflag, c, fd, rval, status, is_shlib, rv, type;
141 while ((c = getopt(argc, argv, "af:")) != -1) {
149 errx(1, "too many formats");
168 for (; argc > 0; argc--, argv++) {
169 if ((fd = open(*argv, O_RDONLY | O_VERIFY, 0)) < 0) {
174 rv = is_executable(*argv, fd, &is_shlib, &type);
184 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
186 rval |= execldd32(*argv, fmt1, fmt2, aflag);
192 * This shouldn't happen unless is_executable()
195 errx(EDOOFUS, "unknown executable type");
199 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
201 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
203 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
205 LDD_SETENV("TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
207 LDD_SETENV("TRACE_LOADED_OBJECTS_ALL", "1", 1);
208 else if (fmt1 == NULL && fmt2 == NULL)
209 /* Default formats */
210 printf("%s:\n", *argv);
218 if (wait(&status) < 0) {
221 } else if (WIFSIGNALED(status)) {
222 fprintf(stderr, "%s: signal %d\n", *argv,
225 } else if (WIFEXITED(status) &&
226 WEXITSTATUS(status) != 0) {
227 fprintf(stderr, "%s: exit status %d\n", *argv,
228 WEXITSTATUS(status));
234 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
235 if (type == TYPE_ELF32)
236 rtld = __PATH_RTLD("32");
239 execl(rtld, rtld, "--",
240 *argv, (char *)NULL);
242 } else if (fmt1 == NULL && fmt2 == NULL && !aflag) {
243 dlopen(*argv, RTLD_TRACE);
244 warnx("%s: %s", *argv, dlerror());
246 execl(rtld, rtld, "-d", "--",
247 *argv, (char *)NULL);
261 "usage: ldd [-a] [-f format [-f format]] program ...\n");
266 has_freebsd_abi_tag(const char *fname, Elf *elf, GElf_Ehdr *ehdr, off_t offset,
270 const Elf_Note *note;
274 size_t namesz, descsz;
277 buf = elf_rawfile(elf, NULL);
279 warnx("%s: %s", fname, elf_errmsg(0));
283 memset(&src, 0, sizeof(src));
284 src.d_buf = buf + offset;
286 src.d_type = ELF_T_NOTE;
287 src.d_version = EV_CURRENT;
289 memset(&dst, 0, sizeof(dst));
290 dst.d_buf = copy = malloc(len);
292 dst.d_type = ELF_T_NOTE;
293 dst.d_version = EV_CURRENT;
295 if (gelf_xlatetom(elf, &dst, &src, ehdr->e_ident[EI_DATA]) == NULL) {
296 warnx("%s: failed to parse notes: %s", fname, elf_errmsg(0));
304 if (len < sizeof(*note))
307 note = (const void *)buf;
308 buf += sizeof(*note);
309 len -= sizeof(*note);
311 namesz = roundup2(note->n_namesz, sizeof(uint32_t));
312 descsz = roundup2(note->n_descsz, sizeof(uint32_t));
313 if (len < namesz + descsz)
317 if (note->n_namesz == sizeof(ELF_NOTE_FREEBSD) &&
318 strncmp(name, ELF_NOTE_FREEBSD, note->n_namesz) == 0 &&
319 note->n_type == NT_FREEBSD_ABI_TAG &&
320 note->n_descsz == sizeof(uint32_t)) {
325 buf += namesz + descsz;
326 len -= namesz + descsz;
330 return (has_abi_tag);
334 is_pie(const char *fname, Elf *elf, GElf_Ehdr *ehdr, off_t offset, size_t len)
344 buf = elf_rawfile(elf, NULL);
346 warnx("%s: %s", fname, elf_errmsg(0));
350 dynsize = gelf_fsize(elf, ELF_T_DYN, 1, EV_CURRENT);
352 warnx("%s: %s", fname, elf_errmsg(0));
355 count = len / dynsize;
357 memset(&src, 0, sizeof(src));
358 src.d_buf = buf + offset;
360 src.d_type = ELF_T_DYN;
361 src.d_version = EV_CURRENT;
363 memset(&dst, 0, sizeof(dst));
364 dst.d_buf = copy = malloc(count * sizeof(*dyn));
365 dst.d_size = count * sizeof(*dyn);
366 dst.d_type = ELF_T_DYN;
367 dst.d_version = EV_CURRENT;
369 if (gelf_xlatetom(elf, &dst, &src, ehdr->e_ident[EI_DATA]) == NULL) {
370 warnx("%s: failed to parse .dynamic: %s", fname, elf_errmsg(0));
377 for (i = 0; i < count; i++) {
378 if (dyn[i].d_tag != DT_FLAGS_1)
381 pie = (dyn[i].d_un.d_val & DF_1_PIE) != 0;
390 is_executable(const char *fname, int fd, int *is_shlib, int *type)
395 bool dynamic, freebsd, pie;
399 *type = TYPE_UNKNOWN;
404 if (elf_version(EV_CURRENT) == EV_NONE) {
405 warnx("unsupported libelf");
408 elf = elf_begin(fd, ELF_C_READ, NULL);
410 warnx("%s: %s", fname, elf_errmsg(0));
413 if (elf_kind(elf) != ELF_K_ELF) {
415 warnx("%s: not a dynamic ELF executable", fname);
418 if (gelf_getehdr(elf, &ehdr) == NULL) {
419 warnx("%s: %s", fname, elf_errmsg(0));
425 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
426 if (gelf_getclass(elf) == ELFCLASS32) {
431 freebsd = ehdr.e_ident[EI_OSABI] == ELFOSABI_FREEBSD;
432 for (i = 0; i < ehdr.e_phnum; i++) {
433 if (gelf_getphdr(elf, i, &phdr) == NULL) {
434 warnx("%s: %s", fname, elf_errmsg(0));
438 switch (phdr.p_type) {
440 if (ehdr.e_ident[EI_OSABI] == ELFOSABI_NONE && !freebsd)
441 freebsd = has_freebsd_abi_tag(fname, elf, &ehdr,
442 phdr.p_offset, phdr.p_filesz);
446 if (ehdr.e_type == ET_DYN)
447 pie = is_pie(fname, elf, &ehdr, phdr.p_offset,
455 warnx("%s: not a dynamic ELF executable", fname);
459 if (ehdr.e_type == ET_DYN && !pie) {
464 warnx("%s: not a FreeBSD ELF shared object", fname);