OSDN Git Service

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