OSDN Git Service

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