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>
49 #include <rtld_paths.h>
57 * 32-bit ELF data structures can only be used if the system header[s] declare
58 * them. There is no official macro for determining whether they are declared,
59 * so check for the existence of one of the 32-macros defined in elf(5).
62 #define ELF32_SUPPORTED
65 #define LDD_SETENV(name, value, overwrite) do { \
66 setenv("LD_" name, value, overwrite); \
67 setenv("LD_32_" name, value, overwrite); \
70 #define LDD_UNSETENV(name) do { \
71 unsetenv("LD_" name); \
72 unsetenv("LD_32_" name); \
75 static int is_executable(const char *fname, int fd, int *is_shlib,
77 static void usage(void);
79 #define TYPE_UNKNOWN 0
80 #define TYPE_ELF 1 /* Architecture default */
81 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
82 #define TYPE_ELF32 2 /* Explicit 32 bits on architectures >32 bits */
84 #define _PATH_LDD32 "/usr/bin/ldd32"
87 execldd32(char *file, char *fmt1, char *fmt2, int aflag)
92 LDD_UNSETENV("TRACE_LOADED_OBJECTS");
95 argv[i++] = strdup(_PATH_LDD32);
97 argv[i++] = strdup("-a");
99 argv[i++] = strdup("-f");
100 argv[i++] = strdup(fmt1);
103 argv[i++] = strdup("-f");
104 argv[i++] = strdup(fmt2);
106 argv[i++] = strdup(file);
114 execv(_PATH_LDD32, argv);
115 warn("%s", _PATH_LDD32);
119 if (wait(&status) < 0)
121 else if (WIFSIGNALED(status))
123 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
129 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
135 main(int argc, char *argv[])
139 int aflag, c, fd, rval, status, is_shlib, rv, type;
144 while ((c = getopt(argc, argv, "af:")) != -1) {
152 errx(1, "too many formats");
171 for (; argc > 0; argc--, argv++) {
172 if ((fd = open(*argv, O_RDONLY | O_VERIFY, 0)) < 0) {
177 rv = is_executable(*argv, fd, &is_shlib, &type);
187 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
189 rval |= execldd32(*argv, fmt1, fmt2, aflag);
195 * This shouldn't happen unless is_executable()
198 errx(EDOOFUS, "unknown executable type");
202 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
204 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
206 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
208 LDD_SETENV("TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
210 LDD_SETENV("TRACE_LOADED_OBJECTS_ALL", "1", 1);
211 else if (fmt1 == NULL && fmt2 == NULL)
212 /* Default formats */
213 printf("%s:\n", *argv);
221 if (wait(&status) < 0) {
224 } else if (WIFSIGNALED(status)) {
225 fprintf(stderr, "%s: signal %d\n", *argv,
228 } else if (WIFEXITED(status) &&
229 WEXITSTATUS(status) != 0) {
230 fprintf(stderr, "%s: exit status %d\n", *argv,
231 WEXITSTATUS(status));
237 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
238 if (type == TYPE_ELF32)
239 rtld = _COMPAT32_PATH_RTLD;
242 execl(rtld, rtld, "--",
243 *argv, (char *)NULL);
245 } else if (fmt1 == NULL && fmt2 == NULL && !aflag) {
246 dlopen(*argv, RTLD_TRACE);
247 warnx("%s: %s", *argv, dlerror());
249 execl(rtld, rtld, "-d", "--",
250 *argv, (char *)NULL);
263 fprintf(stderr, "usage: ldd [-a] [-f format] program ...\n");
268 has_freebsd_abi_tag(const char *fname, Elf *elf, GElf_Ehdr *ehdr, off_t offset,
272 const Elf_Note *note;
276 size_t namesz, descsz;
279 buf = elf_rawfile(elf, NULL);
281 warnx("%s: %s", fname, elf_errmsg(0));
285 memset(&src, 0, sizeof(src));
286 src.d_buf = buf + offset;
288 src.d_type = ELF_T_NOTE;
289 src.d_version = EV_CURRENT;
291 memset(&dst, 0, sizeof(dst));
292 dst.d_buf = copy = malloc(len);
294 dst.d_type = ELF_T_NOTE;
295 dst.d_version = EV_CURRENT;
297 if (gelf_xlatetom(elf, &dst, &src, ehdr->e_ident[EI_DATA]) == NULL) {
298 warnx("%s: failed to parse notes: %s", fname, elf_errmsg(0));
306 if (len < sizeof(*note))
309 note = (const void *)buf;
310 buf += sizeof(*note);
311 len -= sizeof(*note);
313 namesz = roundup2(note->n_namesz, sizeof(uint32_t));
314 descsz = roundup2(note->n_descsz, sizeof(uint32_t));
315 if (len < namesz + descsz)
319 if (note->n_namesz == sizeof(ELF_NOTE_FREEBSD) &&
320 strncmp(name, ELF_NOTE_FREEBSD, note->n_namesz) == 0 &&
321 note->n_type == NT_FREEBSD_ABI_TAG &&
322 note->n_descsz == sizeof(uint32_t)) {
327 buf += namesz + descsz;
328 len -= namesz + descsz;
332 return (has_abi_tag);
336 is_pie(const char *fname, Elf *elf, GElf_Ehdr *ehdr, off_t offset, size_t len)
346 buf = elf_rawfile(elf, NULL);
348 warnx("%s: %s", fname, elf_errmsg(0));
352 dynsize = gelf_fsize(elf, ELF_T_DYN, 1, EV_CURRENT);
354 warnx("%s: %s", fname, elf_errmsg(0));
357 count = len / dynsize;
359 memset(&src, 0, sizeof(src));
360 src.d_buf = buf + offset;
362 src.d_type = ELF_T_DYN;
363 src.d_version = EV_CURRENT;
365 memset(&dst, 0, sizeof(dst));
366 dst.d_buf = copy = malloc(count * sizeof(*dyn));
367 dst.d_size = count * sizeof(*dyn);
368 dst.d_type = ELF_T_DYN;
369 dst.d_version = EV_CURRENT;
371 if (gelf_xlatetom(elf, &dst, &src, ehdr->e_ident[EI_DATA]) == NULL) {
372 warnx("%s: failed to parse .dynamic: %s", fname, elf_errmsg(0));
379 for (i = 0; i < count; i++) {
380 if (dyn[i].d_tag != DT_FLAGS_1)
383 pie = (dyn[i].d_un.d_val & DF_1_PIE) != 0;
392 is_executable(const char *fname, int fd, int *is_shlib, int *type)
397 bool dynamic, freebsd, pie;
401 *type = TYPE_UNKNOWN;
406 if (elf_version(EV_CURRENT) == EV_NONE) {
407 warnx("unsupported libelf");
410 elf = elf_begin(fd, ELF_C_READ, NULL);
412 warnx("%s: %s", fname, elf_errmsg(0));
415 if (elf_kind(elf) != ELF_K_ELF) {
417 warnx("%s: not a dynamic ELF executable", fname);
420 if (gelf_getehdr(elf, &ehdr) == NULL) {
421 warnx("%s: %s", fname, elf_errmsg(0));
427 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
428 if (gelf_getclass(elf) == ELFCLASS32) {
433 freebsd = ehdr.e_ident[EI_OSABI] == ELFOSABI_FREEBSD;
434 for (i = 0; i < ehdr.e_phnum; i++) {
435 if (gelf_getphdr(elf, i, &phdr) == NULL) {
436 warnx("%s: %s", fname, elf_errmsg(0));
440 switch (phdr.p_type) {
442 if (ehdr.e_ident[EI_OSABI] == ELFOSABI_NONE && !freebsd)
443 freebsd = has_freebsd_abi_tag(fname, elf, &ehdr,
444 phdr.p_offset, phdr.p_filesz);
448 if (ehdr.e_type == ET_DYN)
449 pie = is_pie(fname, elf, &ehdr, phdr.p_offset,
457 warnx("%s: not a dynamic ELF executable", fname);
461 if (ehdr.e_type == ET_DYN && !pie) {
466 warnx("%s: not a FreeBSD ELF shared object", fname);