OSDN Git Service

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