OSDN Git Service

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