OSDN Git Service

cleanup style; no functional changes
[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/types.h>
26 #include <sys/wait.h>
27
28 #include "bswap.h"
29 #include "link.h"
30 #include "dl-defs.h"
31 /* makefile will include elf.h for us */
32
33 #ifdef DMALLOC
34 #include <dmalloc.h>
35 #endif
36
37 #if defined(__alpha__)
38 #define MATCH_MACHINE(x) (x == EM_ALPHA)
39 #define ELFCLASSM       ELFCLASS64
40 #endif
41
42 #if defined(__arm__) || defined(__thumb__)
43 #define MATCH_MACHINE(x) (x == EM_ARM)
44 #define ELFCLASSM       ELFCLASS32
45 #endif
46
47 #if defined(__s390__)
48 #define MATCH_MACHINE(x) (x == EM_S390)
49 #define ELFCLASSM       ELFCLASS32
50 #endif
51
52 #if defined(__hppa__)
53 #define MATCH_MACHINE(x) (x == EM_PARISC)
54 #if defined(__LP64__)
55 #define ELFCLASSM               ELFCLASS64
56 #else
57 #define ELFCLASSM               ELFCLASS32
58 #endif
59 #endif
60
61 #if defined(__i386__)
62 #ifndef EM_486
63 #define MATCH_MACHINE(x) (x == EM_386)
64 #else
65 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
66 #endif
67 #define ELFCLASSM       ELFCLASS32
68 #endif
69
70 #if defined(__ia64__)
71 #define MATCH_MACHINE(x) (x == EM_IA_64)
72 #define ELFCLASSM       ELFCLASS64
73 #endif
74
75 #if defined(__mc68000__)
76 #define MATCH_MACHINE(x) (x == EM_68K)
77 #define ELFCLASSM       ELFCLASS32
78 #endif
79
80 #if defined(__mips__)
81 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
82 #define ELFCLASSM       ELFCLASS32
83 #endif
84
85 #if defined(__powerpc64__)
86 #define MATCH_MACHINE(x) (x == EM_PPC64)
87 #define ELFCLASSM       ELFCLASS64
88 #elif defined(__powerpc__)
89 #define MATCH_MACHINE(x) (x == EM_PPC)
90 #define ELFCLASSM       ELFCLASS32
91 #endif
92
93 #if defined(__sh__)
94 #define MATCH_MACHINE(x) (x == EM_SH)
95 #define ELFCLASSM       ELFCLASS32
96 #endif
97
98 #if defined(__v850e__)
99 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
100 #define ELFCLASSM       ELFCLASS32
101 #endif
102
103 #if defined(__sparc__)
104 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
105 #define ELFCLASSM    ELFCLASS32
106 #endif
107
108 #if defined(__cris__)
109 #define MATCH_MACHINE(x) (x == EM_CRIS)
110 #define ELFCLASSM       ELFCLASS32
111 #endif
112
113 #if defined(__x86_64__)
114 #define MATCH_MACHINE(x) (x == EM_X86_64)
115 #define ELFCLASSM       ELFCLASS64
116 #endif
117
118 #ifndef MATCH_MACHINE
119 # ifdef __linux__
120 #  include <asm/elf.h>
121 # endif
122 # ifdef ELF_ARCH
123 #  define MATCH_MACHINE(x) (x == ELF_ARCH)
124 # endif
125 # ifdef ELF_CLASS
126 #  define ELFCLASSM ELF_CLASS
127 # endif
128 #endif
129 #ifndef MATCH_MACHINE
130 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
131 #endif
132
133 #if __BYTE_ORDER == __LITTLE_ENDIAN
134 #define ELFDATAM        ELFDATA2LSB
135 #elif __BYTE_ORDER == __BIG_ENDIAN
136 #define ELFDATAM        ELFDATA2MSB
137 #endif
138
139 #ifndef UCLIBC_RUNTIME_PREFIX
140 # define UCLIBC_RUNTIME_PREFIX "/"
141 #endif
142
143 struct library {
144         char *name;
145         int resolved;
146         char *path;
147         struct library *next;
148 };
149 struct library *lib_list = NULL;
150 char not_found[] = "not found";
151 char *interp_name = NULL;
152 char *interp_dir = NULL;
153 int byteswap;
154 static int interpreter_already_found = 0;
155
156 inline uint32_t byteswap32_to_host(uint32_t value)
157 {
158         if (byteswap == 1) {
159                 return (bswap_32(value));
160         } else {
161                 return (value);
162         }
163 }
164 inline uint64_t byteswap64_to_host(uint64_t value)
165 {
166         if (byteswap == 1) {
167                 return (bswap_64(value));
168         } else {
169                 return (value);
170         }
171 }
172
173 #if ELFCLASSM == ELFCLASS32
174 # define byteswap_to_host(x) byteswap32_to_host(x)
175 #else
176 # define byteswap_to_host(x) byteswap64_to_host(x)
177 #endif
178
179 ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
180 {
181         int j;
182         ElfW(Shdr) *shdr;
183         shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
184         for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
185                 if (key == byteswap32_to_host(shdr->sh_type)) {
186                         return shdr;
187                 }
188         }
189         return NULL;
190 }
191
192 ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
193 {
194         int j;
195         ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
196         for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
197                 if (type == byteswap32_to_host(phdr->p_type)) {
198                         return phdr;
199                 }
200         }
201         return NULL;
202 }
203
204 /* Returns value if return_val==1, ptr otherwise */
205 void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
206                        ElfW(Ehdr) *ehdr, int return_val)
207 {
208         ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
209         unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
210         for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
211                 if (key == byteswap_to_host(dynp->d_tag)) {
212                         if (return_val == 1)
213                                 return (void *)byteswap_to_host(dynp->d_un.d_val);
214                         else
215                                 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
216                 }
217         }
218         return NULL;
219 }
220
221 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
222 {
223         ElfW(Dyn) *dyns;
224
225         for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
226                 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
227                         char *strtab;
228                         strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
229                         return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
230                 }
231         }
232         return NULL;
233 }
234
235 int check_elf_header(ElfW(Ehdr) *const ehdr)
236 {
237         if (!ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 ||
238             ehdr->e_ident[EI_CLASS] != ELFCLASSM ||
239             ehdr->e_ident[EI_VERSION] != EV_CURRENT)
240         {
241                 return 1;
242         }
243
244         /* Check if the target endianness matches the host's endianness */
245         byteswap = 0;
246 #if __BYTE_ORDER == __LITTLE_ENDIAN
247         if (ehdr->e_ident[5] == ELFDATA2MSB) {
248                 /* Ick -- we will have to byte-swap everything */
249                 byteswap = 1;
250         }
251 #elif __BYTE_ORDER == __BIG_ENDIAN
252         if (ehdr->e_ident[5] == ELFDATA2LSB) {
253                 /* Ick -- we will have to byte-swap everything */
254                 byteswap = 1;
255         }
256 #else
257 #error Unknown host byte order!
258 #endif
259
260         /* Be vary lazy, and only byteswap the stuff we use */
261         if (byteswap == 1) {
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                 fprintf(stderr, "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                         fprintf(stderr, "\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 }