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>
53 * 32-bit ELF data structures can only be used if the system header[s] declare
54 * them. There is no official macro for determining whether they are declared,
55 * so check for the existence of one of the 32-macros defined in elf(5).
58 #define ELF32_SUPPORTED
61 #define LDD_SETENV(name, value, overwrite) do { \
62 setenv("LD_" name, value, overwrite); \
63 setenv("LD_32_" name, value, overwrite); \
66 #define LDD_UNSETENV(name) do { \
67 unsetenv("LD_" name); \
68 unsetenv("LD_32_" name); \
71 static int is_executable(const char *fname, int fd, int *is_shlib,
73 static void usage(void);
75 #define TYPE_UNKNOWN 0
77 #define TYPE_ELF 2 /* Architecture default */
78 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
79 #define TYPE_ELF32 3 /* Explicit 32 bits on architectures >32 bits */
81 #define _PATH_LDD32 "/usr/bin/ldd32"
84 execldd32(char *file, char *fmt1, char *fmt2, int aflag, int vflag)
89 LDD_UNSETENV("TRACE_LOADED_OBJECTS");
92 argv[i++] = strdup(_PATH_LDD32);
94 argv[i++] = strdup("-a");
96 argv[i++] = strdup("-v");
98 argv[i++] = strdup("-f");
99 argv[i++] = strdup(fmt1);
102 argv[i++] = strdup("-f");
103 argv[i++] = strdup(fmt2);
105 argv[i++] = strdup(file);
113 execv(_PATH_LDD32, argv);
114 warn("%s", _PATH_LDD32);
118 if (wait(&status) < 0)
120 else if (WIFSIGNALED(status))
122 else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
128 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
134 main(int argc, char *argv[])
137 int rval, c, aflag, vflag;
142 while ((c = getopt(argc, argv, "af:v")) != -1) {
150 errx(1, "too many formats");
166 if (vflag && fmt1 != NULL)
167 errx(1, "-v may not be used with -f");
176 for (c = 0; c < argc; c++)
178 exit(error_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
183 for (; argc > 0; argc--, argv++) {
184 int fd, status, is_shlib, rv, type;
186 if ((fd = open(*argv, O_RDONLY, 0)) < 0) {
191 rv = is_executable(*argv, fd, &is_shlib, &type);
202 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
204 rval |= execldd32(*argv, fmt1, fmt2, aflag, vflag);
210 * This shouldn't happen unless is_executable()
213 errx(EDOOFUS, "unknown executable type");
217 LDD_SETENV("TRACE_LOADED_OBJECTS", "yes", 1);
219 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT1", fmt1, 1);
221 LDD_SETENV("TRACE_LOADED_OBJECTS_FMT2", fmt2, 1);
223 LDD_SETENV("TRACE_LOADED_OBJECTS_PROGNAME", *argv, 1);
225 LDD_SETENV("TRACE_LOADED_OBJECTS_ALL", "1", 1);
226 else if (fmt1 == NULL && fmt2 == NULL)
227 /* Default formats */
228 printf("%s:\n", *argv);
236 if (wait(&status) < 0) {
239 } else if (WIFSIGNALED(status)) {
240 fprintf(stderr, "%s: signal %d\n", *argv,
243 } else if (WIFEXITED(status) &&
244 WEXITSTATUS(status) != 0) {
245 fprintf(stderr, "%s: exit status %d\n", *argv,
246 WEXITSTATUS(status));
252 execl(*argv, *argv, (char *)NULL);
255 dlopen(*argv, RTLD_TRACE);
256 warnx("%s: %s", *argv, dlerror());
269 fprintf(stderr, "usage: ldd [-a] [-v] [-f format] program ...\n");
274 is_executable(const char *fname, int fd, int *is_shlib, int *type)
278 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
286 *type = TYPE_UNKNOWN;
288 if ((n = read(fd, &hdr, sizeof(hdr))) == -1) {
289 warn("%s: can't read program header", fname);
293 if ((size_t)n >= sizeof(hdr.aout) && !N_BADMAG(hdr.aout)) {
295 if ((N_GETFLAG(hdr.aout) & EX_DPMASK) != EX_DYNAMIC
296 #if 1 /* Compatibility */
297 || hdr.aout.a_entry < __LDPGSZ
300 warnx("%s: not a dynamic executable", fname);
307 #if __ELF_WORD_SIZE > 32 && defined(ELF32_SUPPORTED)
308 if ((size_t)n >= sizeof(hdr.elf32) && IS_ELF(hdr.elf32) &&
309 hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) {
310 /* Handle 32 bit ELF objects */
317 if (lseek(fd, hdr.elf32.e_phoff, SEEK_SET) == -1) {
318 warnx("%s: header too short", fname);
321 for (i = 0; i < hdr.elf32.e_phnum; i++) {
322 if (read(fd, &phdr, hdr.elf32.e_phentsize) !=
324 warnx("%s: can't read program header", fname);
327 if (phdr.p_type == PT_DYNAMIC) {
334 warnx("%s: not a dynamic ELF executable", fname);
337 if (hdr.elf32.e_type == ET_DYN) {
338 if (hdr.elf32.e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
342 warnx("%s: not a FreeBSD ELF shared object", fname);
350 if ((size_t)n >= sizeof(hdr.elf) && IS_ELF(hdr.elf) &&
351 hdr.elf.e_ident[EI_CLASS] == ELF_TARG_CLASS) {
352 /* Handle default ELF objects on this architecture */
359 if (lseek(fd, hdr.elf.e_phoff, SEEK_SET) == -1) {
360 warnx("%s: header too short", fname);
363 for (i = 0; i < hdr.elf.e_phnum; i++) {
364 if (read(fd, &phdr, hdr.elf.e_phentsize)
366 warnx("%s: can't read program header", fname);
369 if (phdr.p_type == PT_DYNAMIC) {
376 warnx("%s: not a dynamic ELF executable", fname);
379 if (hdr.elf.e_type == ET_DYN) {
380 if (hdr.elf.e_ident[EI_OSABI] == ELFOSABI_FREEBSD) {
384 warnx("%s: not a FreeBSD ELF shared object", fname);
391 warnx("%s: not a dynamic executable", fname);