OSDN Git Service

simple optimizations and style fixes in dynamic loading
[uclinux-h8/uClibc.git] / utils / ldd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * A small little ldd implementation for uClibc
4  *
5  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
6  *
7  * Several functions in this file (specifically, elf_find_section_type(),
8  * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
9  * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
10  * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
11  * (GPL2).
12  *
13  * Licensed under GPLv2 or later
14  */
15
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <fcntl.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <stdint.h>
22 #include <sys/mman.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26
27 #include "bswap.h"
28 #include "link.h"
29 #include "dl-defs.h"
30 /* makefile will include elf.h for us */
31
32 #ifdef DMALLOC
33 #include <dmalloc.h>
34 #endif
35
36 #if defined(__alpha__)
37 #define MATCH_MACHINE(x) (x == EM_ALPHA)
38 #define ELFCLASSM       ELFCLASS64
39 #endif
40
41 #if defined(__arm__) || defined(__thumb__)
42 #define MATCH_MACHINE(x) (x == EM_ARM)
43 #define ELFCLASSM       ELFCLASS32
44 #endif
45
46 #if defined(__avr32__)
47 #define MATCH_MACHINE(x) (x == EM_AVR32)
48 #define ELFCLASSM      ELFCLASS32
49 #endif
50
51 #if defined(__s390__)
52 #define MATCH_MACHINE(x) (x == EM_S390)
53 #define ELFCLASSM       ELFCLASS32
54 #endif
55
56 #if defined(__hppa__)
57 #define MATCH_MACHINE(x) (x == EM_PARISC)
58 #if defined(__LP64__)
59 #define ELFCLASSM               ELFCLASS64
60 #else
61 #define ELFCLASSM               ELFCLASS32
62 #endif
63 #endif
64
65 #if defined(__i386__)
66 #ifndef EM_486
67 #define MATCH_MACHINE(x) (x == EM_386)
68 #else
69 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
70 #endif
71 #define ELFCLASSM       ELFCLASS32
72 #endif
73
74 #if defined(__ia64__)
75 #define MATCH_MACHINE(x) (x == EM_IA_64)
76 #define ELFCLASSM       ELFCLASS64
77 #endif
78
79 #if defined(__mc68000__)
80 #define MATCH_MACHINE(x) (x == EM_68K)
81 #define ELFCLASSM       ELFCLASS32
82 #endif
83
84 #if defined(__mips__)
85 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
86 #define ELFCLASSM       ELFCLASS32
87 #endif
88
89 #if defined(__powerpc64__)
90 #define MATCH_MACHINE(x) (x == EM_PPC64)
91 #define ELFCLASSM       ELFCLASS64
92 #elif defined(__powerpc__)
93 #define MATCH_MACHINE(x) (x == EM_PPC)
94 #define ELFCLASSM       ELFCLASS32
95 #endif
96
97 #if defined(__sh__)
98 #define MATCH_MACHINE(x) (x == EM_SH)
99 #define ELFCLASSM       ELFCLASS32
100 #endif
101
102 #if defined(__v850e__)
103 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
104 #define ELFCLASSM       ELFCLASS32
105 #endif
106
107 #if defined(__sparc__)
108 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
109 #define ELFCLASSM    ELFCLASS32
110 #endif
111
112 #if defined(__cris__)
113 #define MATCH_MACHINE(x) (x == EM_CRIS)
114 #define ELFCLASSM       ELFCLASS32
115 #endif
116
117 #if defined(__x86_64__)
118 #define MATCH_MACHINE(x) (x == EM_X86_64)
119 #define ELFCLASSM       ELFCLASS64
120 #endif
121
122 #ifndef MATCH_MACHINE
123 # ifdef __linux__
124 #  include <asm/elf.h>
125 # endif
126 # ifdef ELF_ARCH
127 #  define MATCH_MACHINE(x) (x == ELF_ARCH)
128 # endif
129 # ifdef ELF_CLASS
130 #  define ELFCLASSM ELF_CLASS
131 # endif
132 #endif
133 #ifndef MATCH_MACHINE
134 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
135 #endif
136
137 #if __BYTE_ORDER == __LITTLE_ENDIAN
138 #define ELFDATAM        ELFDATA2LSB
139 #elif __BYTE_ORDER == __BIG_ENDIAN
140 #define ELFDATAM        ELFDATA2MSB
141 #endif
142
143 #ifndef UCLIBC_RUNTIME_PREFIX
144 # define UCLIBC_RUNTIME_PREFIX "/"
145 #endif
146
147 struct library {
148         char *name;
149         int resolved;
150         char *path;
151         struct library *next;
152 };
153 static struct library *lib_list = NULL;
154 static char not_found[] = "not found";
155 static char *interp_name = NULL;
156 static char *interp_dir = NULL;
157 static int byteswap;
158 static int interpreter_already_found = 0;
159
160 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
161 {
162         if (byteswap == 1) {
163                 return (bswap_32(value));
164         } else {
165                 return (value);
166         }
167 }
168 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
169 {
170         if (byteswap == 1) {
171                 return (bswap_64(value));
172         } else {
173                 return (value);
174         }
175 }
176
177 #if ELFCLASSM == ELFCLASS32
178 # define byteswap_to_host(x) byteswap32_to_host(x)
179 #else
180 # define byteswap_to_host(x) byteswap64_to_host(x)
181 #endif
182
183 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
184 {
185         int j;
186         ElfW(Shdr) *shdr;
187         shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
188         for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
189                 if (key == byteswap32_to_host(shdr->sh_type)) {
190                         return shdr;
191                 }
192         }
193         return NULL;
194 }
195
196 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
197 {
198         int j;
199         ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
200         for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
201                 if (type == byteswap32_to_host(phdr->p_type)) {
202                         return phdr;
203                 }
204         }
205         return NULL;
206 }
207
208 /* Returns value if return_val==1, ptr otherwise */
209 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
210                        ElfW(Ehdr) *ehdr, int return_val)
211 {
212         ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
213         unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
214         for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
215                 if (key == byteswap_to_host(dynp->d_tag)) {
216                         if (return_val == 1)
217                                 return (void *)byteswap_to_host(dynp->d_un.d_val);
218                         else
219                                 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
220                 }
221         }
222         return NULL;
223 }
224
225 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
226 {
227         ElfW(Dyn) *dyns;
228
229         for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
230                 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
231                         char *strtab;
232                         strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
233                         return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
234                 }
235         }
236         return NULL;
237 }
238
239 int check_elf_header(ElfW(Ehdr) *const ehdr)
240 {
241         if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
242          || ehdr->e_ident[EI_CLASS] != ELFCLASSM
243          || ehdr->e_ident[EI_VERSION] != EV_CURRENT
244         ) {
245                 return 1;
246         }
247
248         /* Check if the target endianness matches the host's endianness */
249         byteswap = 0;
250 #if __BYTE_ORDER == __LITTLE_ENDIAN
251         if (ehdr->e_ident[5] == ELFDATA2MSB)
252                 byteswap = 1;
253 #elif __BYTE_ORDER == __BIG_ENDIAN
254         if (ehdr->e_ident[5] == ELFDATA2LSB)
255                 byteswap = 1;
256 #else
257 #error Unknown host byte order!
258 #endif
259
260         /* Be very lazy, and only byteswap the stuff we use */
261         if (byteswap) {
262                 ehdr->e_type = bswap_16(ehdr->e_type);
263                 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
264                 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
265                 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
266                 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
267         }
268
269         return 0;
270 }
271
272 #ifdef __LDSO_CACHE_SUPPORT__
273 static caddr_t cache_addr = NULL;
274 static size_t cache_size = 0;
275
276 int map_cache(void)
277 {
278         int fd;
279         struct stat st;
280         header_t *header;
281         libentry_t *libent;
282         int i, strtabsize;
283
284         if (cache_addr == (caddr_t) - 1)
285                 return -1;
286         else if (cache_addr != NULL)
287                 return 0;
288
289         if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
290                 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
291                 cache_addr = (caddr_t) - 1;     /* so we won't try again */
292                 return -1;
293         }
294
295         cache_size = st.st_size;
296         cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
297         close(fd);
298         if (cache_addr == MAP_FAILED) {
299                 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
300                 return -1;
301         }
302
303         header = (header_t *) cache_addr;
304
305         if (cache_size < sizeof(header_t)
306             || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
307             || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
308             || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
309             || cache_addr[cache_size - 1] != '\0')
310         {
311                 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
312                 goto fail;
313         }
314
315         strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
316         libent = (libentry_t *) & header[1];
317
318         for (i = 0; i < header->nlibs; i++) {
319                 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
320                         fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
321                         goto fail;
322                 }
323         }
324
325         return 0;
326
327 fail:
328         munmap(cache_addr, cache_size);
329         cache_addr = (caddr_t) - 1;
330         return -1;
331 }
332
333 int unmap_cache(void)
334 {
335         if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
336                 return -1;
337
338 #if 1
339         munmap(cache_addr, cache_size);
340         cache_addr = NULL;
341 #endif
342
343         return 0;
344 }
345 #else
346 static __inline__ void map_cache(void)
347 {
348 }
349 static __inline__ void unmap_cache(void)
350 {
351 }
352 #endif
353
354 /* This function's behavior must exactly match that
355  * in uClibc/ldso/ldso/dl-elf.c */
356 static void search_for_named_library(char *name, char *result,
357                                      const char *path_list)
358 {
359         int i, count = 1;
360         char *path, *path_n;
361         struct stat filestat;
362
363         /* We need a writable copy of this string */
364         path = strdup(path_list);
365         if (!path) {
366                 fprintf(stderr, "Out of memory!\n");
367                 exit(EXIT_FAILURE);
368         }
369         /* Eliminate all double //s */
370         path_n = path;
371         while ((path_n = strstr(path_n, "//"))) {
372                 i = strlen(path_n);
373                 memmove(path_n, path_n + 1, i - 1);
374                 *(path_n + i - 1) = '\0';
375         }
376
377         /* Replace colons with zeros in path_list and count them */
378         for (i = strlen(path); i > 0; i--) {
379                 if (path[i] == ':') {
380                         path[i] = 0;
381                         count++;
382                 }
383         }
384         path_n = path;
385         for (i = 0; i < count; i++) {
386                 strcpy(result, path_n);
387                 strcat(result, "/");
388                 strcat(result, name);
389                 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
390                         free(path);
391                         return;
392                 }
393                 path_n += (strlen(path_n) + 1);
394         }
395         free(path);
396         *result = '\0';
397 }
398
399 void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_suid,
400                          struct library *lib)
401 {
402         char *buf;
403         char *path;
404         struct stat filestat;
405
406         /* If this is a fully resolved name, our job is easy */
407         if (stat(lib->name, &filestat) == 0) {
408                 lib->path = strdup(lib->name);
409                 return;
410         }
411
412         /* We need some elbow room here.  Make some room... */
413         buf = malloc(1024);
414         if (!buf) {
415                 fprintf(stderr, "Out of memory!\n");
416                 exit(EXIT_FAILURE);
417         }
418
419         /* This function must match the behavior of _dl_load_shared_library
420          * in readelflib1.c or things won't work out as expected... */
421
422         /* The ABI specifies that RPATH is searched first, so do that now.  */
423         path = elf_find_rpath(ehdr, dynamic);
424         if (path) {
425                 search_for_named_library(lib->name, buf, path);
426                 if (*buf != '\0') {
427                         lib->path = buf;
428                         return;
429                 }
430         }
431
432         /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
433          * Since this app doesn't actually run an executable I will skip
434          * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
435         if (is_suid == 1)
436                 path = NULL;
437         else
438                 path = getenv("LD_LIBRARY_PATH");
439         if (path) {
440                 search_for_named_library(lib->name, buf, path);
441                 if (*buf != '\0') {
442                         lib->path = buf;
443                         return;
444                 }
445         }
446 #ifdef __LDSO_CACHE_SUPPORT__
447         if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
448                 int i;
449                 header_t *header = (header_t *) cache_addr;
450                 libentry_t *libent = (libentry_t *) & header[1];
451                 char *strs = (char *)&libent[header->nlibs];
452
453                 for (i = 0; i < header->nlibs; i++) {
454                         if ((libent[i].flags == LIB_ELF ||
455                              libent[i].flags == LIB_ELF_LIBC0 ||
456                              libent[i].flags == LIB_ELF_LIBC5) &&
457                             strcmp(lib->name, strs + libent[i].sooffset) == 0)
458                         {
459                                 lib->path = strdup(strs + libent[i].liboffset);
460                                 return;
461                         }
462                 }
463         }
464 #endif
465
466         /* Next look for libraries wherever the shared library
467          * loader was installed -- this is usually where we
468          * should find things... */
469         if (interp_dir) {
470                 search_for_named_library(lib->name, buf, interp_dir);
471                 if (*buf != '\0') {
472                         lib->path = buf;
473                         return;
474                 }
475         }
476
477         /* Lastly, search the standard list of paths for the library.
478            This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
479         path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
480 #ifndef __LDSO_CACHE_SUPPORT__
481             ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
482 #endif
483             ;
484         search_for_named_library(lib->name, buf, path);
485         if (*buf != '\0') {
486                 lib->path = buf;
487         } else {
488                 free(buf);
489                 lib->path = not_found;
490         }
491 }
492
493 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
494 {
495         char *tmp, *tmp1, *tmp2;
496         struct library *cur, *newlib = lib_list;
497
498         if (!s || !strlen(s))
499                 return 1;
500
501         tmp = s;
502         while (*tmp) {
503                 if (*tmp == '/')
504                         s = tmp + 1;
505                 tmp++;
506         }
507
508         /* We add ldso elsewhere */
509         if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
510                 int len = strlen(interp_dir);
511                 if (strcmp(s, interp_name + 1 + len) == 0)
512                         return 1;
513         }
514
515         for (cur = lib_list; cur; cur = cur->next) {
516                 /* Check if this library is already in the list */
517                 tmp1 = tmp2 = cur->name;
518                 while (*tmp1) {
519                         if (*tmp1 == '/')
520                                 tmp2 = tmp1 + 1;
521                         tmp1++;
522                 }
523                 if (strcmp(tmp2, s) == 0) {
524                         /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
525                         return 0;
526                 }
527         }
528
529         /* Ok, this lib needs to be added to the list */
530         newlib = malloc(sizeof(struct library));
531         if (!newlib)
532                 return 1;
533         newlib->name = malloc(strlen(s) + 1);
534         strcpy(newlib->name, s);
535         newlib->resolved = 0;
536         newlib->path = NULL;
537         newlib->next = NULL;
538
539         /* Now try and locate where this library might be living... */
540         locate_library_file(ehdr, dynamic, is_setuid, newlib);
541
542         /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
543         if (!lib_list) {
544                 lib_list = newlib;
545         } else {
546                 for (cur = lib_list; cur->next; cur = cur->next) ;      /* nothing */
547                 cur->next = newlib;
548         }
549         return 0;
550 }
551
552 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
553 {
554         ElfW(Dyn) *dyns;
555
556         for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
557                 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
558                         char *strtab;
559                         strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
560                         add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
561                 }
562         }
563 }
564
565 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
566 {
567         ElfW(Phdr) *phdr;
568
569         if (interpreter_already_found == 1)
570                 return NULL;
571         phdr = elf_find_phdr_type(PT_INTERP, ehdr);
572         if (phdr) {
573                 struct library *cur, *newlib = NULL;
574                 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
575
576                 char *tmp, *tmp1;
577                 interp_name = strdup(s);
578                 interp_dir = strdup(s);
579                 tmp = strrchr(interp_dir, '/');
580                 if (*tmp)
581                         *tmp = '\0';
582                 else {
583                         free(interp_dir);
584                         interp_dir = interp_name;
585                 }
586                 tmp1 = tmp = s;
587                 while (*tmp) {
588                         if (*tmp == '/')
589                                 tmp1 = tmp + 1;
590                         tmp++;
591                 }
592                 for (cur = lib_list; cur; cur = cur->next) {
593                         /* Check if this library is already in the list */
594                         if (strcmp(cur->name, tmp1) == 0) {
595                                 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
596                                 newlib = cur;
597                                 free(newlib->name);
598                                 if (newlib->path != not_found) {
599                                         free(newlib->path);
600                                 }
601                                 newlib->name = NULL;
602                                 newlib->path = NULL;
603                                 return NULL;
604                         }
605                 }
606                 if (newlib == NULL)
607                         newlib = malloc(sizeof(struct library));
608                 if (!newlib)
609                         return NULL;
610                 newlib->name = malloc(strlen(s) + 1);
611                 strcpy(newlib->name, s);
612                 newlib->path = strdup(newlib->name);
613                 newlib->resolved = 1;
614                 newlib->next = NULL;
615
616 #if 0
617                 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
618                 if (!lib_list) {
619                         lib_list = newlib;
620                 } else {
621                         for (cur = lib_list; cur->next; cur = cur->next) ;      /* nothing */
622                         cur->next = newlib;
623                 }
624 #endif
625                 interpreter_already_found = 1;
626                 return newlib;
627         }
628         return NULL;
629 }
630
631 /* map the .so, and locate interesting pieces */
632 /*
633 #warning "There may be two warnings here about vfork() clobbering, ignore them"
634 */
635 int find_dependancies(char *filename)
636 {
637         int is_suid = 0;
638         FILE *thefile;
639         struct library *interp;
640         struct stat statbuf;
641         ElfW(Ehdr) *ehdr = NULL;
642         ElfW(Shdr) *dynsec = NULL;
643         ElfW(Dyn) *dynamic = NULL;
644
645         if (filename == not_found)
646                 return 0;
647
648         if (!filename) {
649                 fprintf(stderr, "No filename specified.\n");
650                 return -1;
651         }
652         if (!(thefile = fopen(filename, "r"))) {
653                 perror(filename);
654                 return -1;
655         }
656         if (fstat(fileno(thefile), &statbuf) < 0) {
657                 perror(filename);
658                 fclose(thefile);
659                 return -1;
660         }
661
662         if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
663                 goto foo;
664
665         if (!S_ISREG(statbuf.st_mode))
666                 goto foo;
667
668         /* mmap the file to make reading stuff from it effortless */
669         ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
670         if (ehdr == MAP_FAILED) {
671                 fclose(thefile);
672                 fprintf(stderr, "Out of memory!\n");
673                 return -1;
674         }
675
676 foo:
677         fclose(thefile);
678
679         /* Check if this looks like a legit ELF file */
680         if (check_elf_header(ehdr)) {
681                 fprintf(stderr, "%s: not an ELF file.\n", filename);
682                 return -1;
683         }
684         /* Check if this is the right kind of ELF file */
685         if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
686                 fprintf(stderr, "%s: not a dynamic executable\n", filename);
687                 return -1;
688         }
689         if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
690                 if (statbuf.st_mode & S_ISUID)
691                         is_suid = 1;
692                 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
693                         is_suid = 1;
694                 /* FIXME */
695                 if (is_suid)
696                         fprintf(stderr, "%s: is setuid\n", filename);
697         }
698
699         interpreter_already_found = 0;
700         interp = find_elf_interpreter(ehdr);
701
702 #ifdef __LDSO_LDD_SUPPORT__
703         if (interp
704             && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
705             && ehdr->e_ident[EI_CLASS] == ELFCLASSM
706             && ehdr->e_ident[EI_DATA] == ELFDATAM
707             && ehdr->e_ident[EI_VERSION] == EV_CURRENT
708             && MATCH_MACHINE(ehdr->e_machine))
709         {
710                 struct stat statbuf;
711                 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
712                         pid_t pid;
713                         int status;
714                         static const char *const environment[] = {
715                                 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
716                                 "SHELL=/bin/sh",
717                                 "LD_TRACE_LOADED_OBJECTS=1",
718                                 NULL
719                         };
720
721                         if ((pid = vfork()) == 0) {
722                                 /* Cool, it looks like we should be able to actually
723                                  * run this puppy.  Do so now... */
724                                 execle(filename, filename, NULL, environment);
725                                 _exit(0xdead);
726                         }
727
728                         /* Wait till it returns */
729                         waitpid(pid, &status, 0);
730                         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
731                                 return 1;
732                         }
733
734                         /* If the exec failed, we fall through to trying to find
735                          * all the needed libraries ourselves by rummaging about
736                          * in the ELF headers... */
737                 }
738         }
739 #endif
740
741         dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
742         if (dynsec) {
743                 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
744                 find_needed_libraries(ehdr, dynamic, is_suid);
745         }
746
747         return 0;
748 }
749
750 int main(int argc, char **argv)
751 {
752         int multi = 0;
753         int got_em_all = 1;
754         char *filename = NULL;
755         struct library *cur;
756
757         if (argc < 2) {
758                 fprintf(stderr, "ldd: missing file arguments\n"
759                                 "Try `ldd --help' for more information.\n");
760                 exit(EXIT_FAILURE);
761         }
762         if (argc > 2)
763                 multi++;
764
765         while (--argc > 0) {
766                 ++argv;
767
768                 if (strcmp(*argv, "--") == 0) {
769                         /* Ignore "--" */
770                         continue;
771                 }
772
773                 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
774                         fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
775                                         "\t--help\t\tprint this help and exit\n");
776                         exit(EXIT_SUCCESS);
777                 }
778
779                 filename = *argv;
780                 if (!filename) {
781                         fprintf(stderr, "No filename specified.\n");
782                         exit(EXIT_FAILURE);
783                 }
784
785                 if (multi) {
786                         printf("%s:\n", *argv);
787                 }
788
789                 map_cache();
790
791                 if (find_dependancies(filename) != 0)
792                         continue;
793
794                 while (got_em_all) {
795                         got_em_all = 0;
796                         /* Keep walking the list till everybody is resolved */
797                         for (cur = lib_list; cur; cur = cur->next) {
798                                 if (cur->resolved == 0 && cur->path) {
799                                         got_em_all = 1;
800                                         printf("checking sub-depends for '%s'\n", cur->path);
801                                         find_dependancies(cur->path);
802                                         cur->resolved = 1;
803                                 }
804                         }
805                 }
806
807                 unmap_cache();
808
809                 /* Print the list */
810                 got_em_all = 0;
811                 for (cur = lib_list; cur; cur = cur->next) {
812                         got_em_all = 1;
813                         printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
814                 }
815                 if (interp_name && interpreter_already_found == 1)
816                         printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
817                 else
818                         printf("\tnot a dynamic executable\n");
819
820                 for (cur = lib_list; cur; cur = cur->next) {
821                         free(cur->name);
822                         cur->name = NULL;
823                         if (cur->path && cur->path != not_found) {
824                                 free(cur->path);
825                                 cur->path = NULL;
826                         }
827                 }
828                 lib_list = NULL;
829         }
830
831         return 0;
832 }