wordexp: Fix the usage of the internal _itoa function
[uclibc-ng.git] / utils / ldd.c
blob4c5635ca7156c8ed576a65e0f5c5a13c13e96305
1 /*
2 * A small little ldd implementation for uClibc
4 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
6 * Several functions in this file (specifically, elf_find_section_type(),
7 * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from
8 * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser
9 * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC
10 * (GPL2).
12 * Licensed under GPLv2 or later
15 #include "porting.h"
17 #if defined(__aarch64__)
18 #define MATCH_MACHINE(x) (x == EM_AARCH64)
19 #define ELFCLASSM ELFCLASS64
20 #endif
22 #if defined(__alpha__)
23 #define MATCH_MACHINE(x) (x == EM_ALPHA)
24 #define ELFCLASSM ELFCLASS64
25 #endif
27 #if defined(__arc__)
28 #define MATCH_MACHINE(x) (x == EM_ARCOMPACT)
29 #define ELFCLASSM ELFCLASS32
30 #endif
32 #if defined(__arm__) || defined(__thumb__)
33 #define MATCH_MACHINE(x) (x == EM_ARM)
34 #define ELFCLASSM ELFCLASS32
35 #endif
37 #if defined(__avr32__)
38 #define MATCH_MACHINE(x) (x == EM_AVR32)
39 #define ELFCLASSM ELFCLASS32
40 #endif
42 #if defined(__bfin__)
43 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
44 #define ELFCLASSM ELFCLASS32
45 #endif
47 #if defined(__TMS320C6X__)
48 #define MATCH_MACHINE(x) (x == EM_TI_C6000)
49 #define ELFCLASSM ELFCLASS32
50 #endif
52 #if defined(__cris__)
53 #define MATCH_MACHINE(x) (x == EM_CRIS)
54 #define ELFCLASSM ELFCLASS32
55 #endif
57 #if defined(__csky__)
58 #define MATCH_MACHINE(x) (x == EM_MCORE)
59 #define ELFCLASSM ELFCLASS32
60 #endif
62 #if defined(__frv__)
63 #define MATCH_MACHINE(x) (x == EM_CYGNUS_FRV)
64 #define ELFCLASSM ELFCLASS32
65 #endif
67 #if defined(__hppa__)
68 #define MATCH_MACHINE(x) (x == EM_PARISC)
69 #if defined(__LP64__)
70 #define ELFCLASSM ELFCLASS64
71 #else
72 #define ELFCLASSM ELFCLASS32
73 #endif
74 #endif
76 #if defined(__i386__)
77 #ifndef EM_486
78 #define MATCH_MACHINE(x) (x == EM_386)
79 #else
80 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
81 #endif
82 #define ELFCLASSM ELFCLASS32
83 #endif
85 #if defined(__ia64__)
86 #define MATCH_MACHINE(x) (x == EM_IA_64)
87 #define ELFCLASSM ELFCLASS64
88 #endif
90 #if defined(__mc68000__)
91 #define MATCH_MACHINE(x) (x == EM_68K)
92 #define ELFCLASSM ELFCLASS32
93 #endif
95 #if defined(__metag__)
96 #define MATCH_MACHINE(x) (x == EM_METAG)
97 #define ELFCLASSM ELFCLASS32
98 #endif
100 #if defined(__microblaze__)
101 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE)
102 #define ELFCLASSM ELFCLASS32
103 #endif
105 #if defined(__mips__)
106 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
107 #define ELFCLASSM ELFCLASS32
108 #endif
110 #if defined(__nds32__)
111 #define MATCH_MACHINE(x) (x == EM_NDS32)
112 #define ELFCLASSM ELFCLASS32
113 #endif
115 #if defined(__nios2__)
116 #define MATCH_MACHINE(x) (x == EM_NIOS32)
117 #define ELFCLASSM ELFCLASS32
118 #endif
120 #if defined(__powerpc__)
121 #define MATCH_MACHINE(x) (x == EM_PPC)
122 #define ELFCLASSM ELFCLASS32
123 #endif
125 #if defined(__sh__)
126 #define MATCH_MACHINE(x) (x == EM_SH)
127 #define ELFCLASSM ELFCLASS32
128 #endif
130 #if defined(__sparc__)
131 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
132 #define ELFCLASSM ELFCLASS32
133 #endif
135 #if defined(__x86_64__)
136 #define MATCH_MACHINE(x) (x == EM_X86_64)
137 #define ELFCLASSM ELFCLASS64
138 #endif
140 #if defined(__xtensa__)
141 #define MATCH_MACHINE(x) (x == EM_XTENSA)
142 #define ELFCLASSM ELFCLASS32
143 #endif
145 #ifndef MATCH_MACHINE
146 # ifdef __linux__
147 # include <asm/elf.h>
148 # endif
149 # ifdef ELF_ARCH
150 # define MATCH_MACHINE(x) (x == ELF_ARCH)
151 # endif
152 # ifdef ELF_CLASS
153 # define ELFCLASSM ELF_CLASS
154 # endif
155 #endif
156 #ifndef MATCH_MACHINE
157 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
158 #endif
160 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
161 #define ELFDATAM ELFDATA2LSB
162 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
163 #define ELFDATAM ELFDATA2MSB
164 #endif
166 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
168 struct library {
169 char *name;
170 int resolved;
171 char *path;
172 struct library *next;
174 static struct library *lib_list = NULL;
175 static char not_found[] = "not found";
176 static char *interp_name = NULL;
177 static char *interp_dir = NULL;
178 static int byteswap;
179 static int interpreter_already_found = 0;
181 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
183 if (byteswap) {
184 return (bswap_32(value));
185 } else {
186 return (value);
189 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
191 if (byteswap) {
192 return (bswap_64(value));
193 } else {
194 return (value);
198 #if __WORDSIZE == 64
199 # define byteswap_to_host(x) byteswap64_to_host(x)
200 #else
201 # define byteswap_to_host(x) byteswap32_to_host(x)
202 #endif
204 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
206 int j;
207 ElfW(Shdr) *shdr;
208 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
209 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
210 if (key == byteswap32_to_host(shdr->sh_type)) {
211 return shdr;
214 return NULL;
217 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
219 int j;
220 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
221 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
222 if (type == byteswap32_to_host(phdr->p_type)) {
223 return phdr;
226 return NULL;
229 /* Returns value if return_val==1, ptr otherwise */
230 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
231 ElfW(Ehdr) *ehdr, int return_val)
233 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
234 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
235 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
236 if (key == byteswap_to_host(dynp->d_tag)) {
237 if (return_val == 1)
238 return (void *)byteswap_to_host(dynp->d_un.d_val);
239 else
240 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
243 return NULL;
246 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
248 ElfW(Dyn) *dyns;
250 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
251 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
252 char *strtab;
253 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
254 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
257 return NULL;
260 static int check_elf_header(ElfW(Ehdr) *const ehdr)
262 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
263 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
264 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
265 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
267 return 1;
270 /* Check if the target endianness matches the host's endianness */
271 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
273 /* Be very lazy, and only byteswap the stuff we use */
274 if (byteswap) {
275 ehdr->e_type = bswap_16(ehdr->e_type);
276 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
277 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
278 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
279 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
282 return 0;
285 #ifdef __LDSO_CACHE_SUPPORT__
286 static caddr_t cache_addr = NULL;
287 static size_t cache_size = 0;
289 static int map_cache(void)
291 int fd;
292 struct stat st;
293 header_t *header;
294 libentry_t *libent;
295 int i, strtabsize;
297 if (cache_addr == (caddr_t) - 1)
298 return -1;
299 else if (cache_addr != NULL)
300 return 0;
302 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
303 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
304 cache_addr = (caddr_t) - 1; /* so we won't try again */
305 return -1;
308 cache_size = st.st_size;
309 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
310 close(fd);
311 if (cache_addr == MAP_FAILED) {
312 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
313 return -1;
316 header = (header_t *) cache_addr;
318 if (cache_size < sizeof(header_t)
319 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
320 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
321 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
322 || cache_addr[cache_size - 1] != '\0')
324 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
325 goto fail;
328 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
329 libent = (libentry_t *) & header[1];
331 for (i = 0; i < header->nlibs; i++) {
332 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
333 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
334 goto fail;
338 return 0;
340 fail:
341 munmap(cache_addr, cache_size);
342 cache_addr = (caddr_t) - 1;
343 return -1;
346 static int unmap_cache(void)
348 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
349 return -1;
351 #if 1
352 munmap(cache_addr, cache_size);
353 cache_addr = NULL;
354 #endif
356 return 0;
358 #else
359 static __inline__ void map_cache(void)
362 static __inline__ void unmap_cache(void)
365 #endif
367 /* This function's behavior must exactly match that
368 * in uClibc/ldso/ldso/dl-elf.c */
369 static void search_for_named_library(char *name, char *result,
370 const char *path_list)
372 int i, count = 1;
373 char *path, *path_n;
374 struct stat filestat;
376 /* We need a writable copy of this string */
377 path = strdup(path_list);
378 if (!path) {
379 fprintf(stderr, "%s: Out of memory!\n", __func__);
380 exit(EXIT_FAILURE);
382 /* Eliminate all double //s */
383 path_n = path;
384 while ((path_n = strstr(path_n, "//"))) {
385 i = strlen(path_n);
386 memmove(path_n, path_n + 1, i - 1);
387 *(path_n + i - 1) = '\0';
390 /* Replace colons with zeros in path_list and count them */
391 for (i = strlen(path); i > 0; i--) {
392 if (path[i] == ':') {
393 path[i] = 0;
394 count++;
397 path_n = path;
398 for (i = 0; i < count; i++) {
399 strcpy(result, path_n);
400 strcat(result, "/");
401 strcat(result, name);
402 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
403 free(path);
404 return;
406 path_n += (strlen(path_n) + 1);
408 free(path);
409 *result = '\0';
412 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
413 int is_suid, struct library *lib)
415 char *buf;
416 char *path;
417 struct stat filestat;
419 /* If this is a fully resolved name, our job is easy */
420 if (stat(lib->name, &filestat) == 0) {
421 lib->path = strdup(lib->name);
422 return;
425 /* We need some elbow room here. Make some room... */
426 buf = malloc(1024);
427 if (!buf) {
428 fprintf(stderr, "%s: Out of memory!\n", __func__);
429 exit(EXIT_FAILURE);
432 /* This function must match the behavior of _dl_load_shared_library
433 * in readelflib1.c or things won't work out as expected... */
435 /* The ABI specifies that RPATH is searched first, so do that now. */
436 path = elf_find_rpath(ehdr, dynamic);
437 if (path) {
438 search_for_named_library(lib->name, buf, path);
439 if (*buf != '\0') {
440 lib->path = buf;
441 return;
445 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
446 * Since this app doesn't actually run an executable I will skip
447 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
448 if (is_suid == 1)
449 path = NULL;
450 else
451 path = getenv("LD_LIBRARY_PATH");
452 if (path) {
453 search_for_named_library(lib->name, buf, path);
454 if (*buf != '\0') {
455 lib->path = buf;
456 return;
459 #ifdef __LDSO_CACHE_SUPPORT__
460 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
461 int i;
462 header_t *header = (header_t *) cache_addr;
463 libentry_t *libent = (libentry_t *) & header[1];
464 char *strs = (char *)&libent[header->nlibs];
466 for (i = 0; i < header->nlibs; i++) {
467 if ((libent[i].flags == LIB_ELF ||
468 libent[i].flags == LIB_ELF_LIBC0 ||
469 libent[i].flags == LIB_ELF_LIBC5) &&
470 strcmp(lib->name, strs + libent[i].sooffset) == 0)
472 lib->path = strdup(strs + libent[i].liboffset);
473 return;
477 #endif
479 /* Next look for libraries wherever the shared library
480 * loader was installed -- this is usually where we
481 * should find things... */
482 if (interp_dir) {
483 search_for_named_library(lib->name, buf, interp_dir);
484 if (*buf != '\0') {
485 lib->path = buf;
486 return;
490 /* Lastly, search the standard list of paths for the library.
491 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
492 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
493 #ifndef __LDSO_CACHE_SUPPORT__
494 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
495 #endif
497 search_for_named_library(lib->name, buf, path);
498 if (*buf != '\0') {
499 lib->path = buf;
500 } else {
501 free(buf);
502 lib->path = not_found;
506 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
508 char *tmp, *tmp1, *tmp2;
509 struct library *cur, *newlib = lib_list;
511 if (!s || !strlen(s))
512 return 1;
514 tmp = s;
515 while (*tmp) {
516 if (*tmp == '/')
517 s = tmp + 1;
518 tmp++;
521 /* We add ldso elsewhere */
522 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
523 int len = strlen(interp_dir);
524 if (strcmp(s, interp_name + 1 + len) == 0)
525 return 1;
528 for (cur = lib_list; cur; cur = cur->next) {
529 /* Check if this library is already in the list */
530 tmp1 = tmp2 = cur->name;
531 if (!cur->name)
532 continue;
533 while (*tmp1) {
534 if (*tmp1 == '/')
535 tmp2 = tmp1 + 1;
536 tmp1++;
538 if (strcmp(tmp2, s) == 0) {
539 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
540 return 0;
544 /* Ok, this lib needs to be added to the list */
545 newlib = malloc(sizeof(struct library));
546 if (!newlib)
547 return 1;
548 newlib->name = malloc(strlen(s) + 1);
549 strcpy(newlib->name, s);
550 newlib->resolved = 0;
551 newlib->path = NULL;
552 newlib->next = NULL;
554 /* Now try and locate where this library might be living... */
555 locate_library_file(ehdr, dynamic, is_setuid, newlib);
557 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
558 if (!lib_list) {
559 lib_list = newlib;
560 } else {
561 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
562 cur->next = newlib;
564 return 0;
567 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
569 ElfW(Dyn) *dyns;
571 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
572 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
573 char *strtab;
574 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
575 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
580 #ifdef __LDSO_LDD_SUPPORT__
581 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
583 ElfW(Phdr) *phdr;
585 if (interpreter_already_found == 1)
586 return NULL;
587 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
588 if (phdr) {
589 struct library *cur, *newlib = NULL;
590 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
592 char *tmp, *tmp1;
593 interp_name = strdup(s);
594 interp_dir = strdup(s);
595 tmp = strrchr(interp_dir, '/');
596 if (tmp)
597 *tmp = '\0';
598 else {
599 free(interp_dir);
600 interp_dir = interp_name;
602 tmp1 = tmp = s;
603 while (*tmp) {
604 if (*tmp == '/')
605 tmp1 = tmp + 1;
606 tmp++;
608 for (cur = lib_list; cur; cur = cur->next) {
609 /* Check if this library is already in the list */
610 if (!tmp1 || !cur->name)
611 return NULL;
612 if (strcmp(cur->name, tmp1) == 0) {
613 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
614 newlib = cur;
615 free(newlib->name);
616 if (newlib->path != not_found) {
617 free(newlib->path);
619 newlib->name = NULL;
620 newlib->path = NULL;
621 break;
624 if (newlib == NULL)
625 newlib = malloc(sizeof(struct library));
626 if (!newlib)
627 return NULL;
628 newlib->name = malloc(strlen(s) + 1);
629 strcpy(newlib->name, s);
630 newlib->path = strdup(newlib->name);
631 newlib->resolved = 1;
632 newlib->next = NULL;
634 #if 0
635 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
636 if (!lib_list) {
637 lib_list = newlib;
638 } else {
639 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
640 cur->next = newlib;
642 #endif
643 interpreter_already_found = 1;
644 return newlib;
646 return NULL;
648 #endif /* __LDSO_LDD_SUPPORT__ */
650 /* map the .so, and locate interesting pieces */
652 #warning "There may be two warnings here about vfork() clobbering, ignore them"
654 static int find_dependencies(char *filename)
656 int is_suid = 0;
657 FILE *thefile;
658 struct stat statbuf;
659 ElfW(Ehdr) *ehdr = NULL;
660 ElfW(Shdr) *dynsec = NULL;
661 ElfW(Dyn) *dynamic = NULL;
662 #ifdef __LDSO_LDD_SUPPORT__
663 struct library *interp;
664 #endif
666 if (filename == not_found)
667 return 0;
669 if (!filename) {
670 fprintf(stderr, "No filename specified.\n");
671 return -1;
673 if (!(thefile = fopen(filename, "r"))) {
674 perror(filename);
675 return -1;
677 if (fstat(fileno(thefile), &statbuf) < 0) {
678 perror(filename);
679 fclose(thefile);
680 return -1;
683 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
684 goto foo;
686 if (!S_ISREG(statbuf.st_mode))
687 goto foo;
689 /* mmap the file to make reading stuff from it effortless */
690 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
691 if (ehdr == MAP_FAILED) {
692 fclose(thefile);
693 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
694 return -1;
697 foo:
698 fclose(thefile);
700 /* Check if this looks like a legit ELF file */
701 if (check_elf_header(ehdr)) {
702 fprintf(stderr, "%s: not an ELF file.\n", filename);
703 return -1;
705 /* Check if this is the right kind of ELF file */
706 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
707 fprintf(stderr, "%s: not a dynamic executable\n", filename);
708 return -1;
710 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
711 if (statbuf.st_mode & S_ISUID)
712 is_suid = 1;
713 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
714 is_suid = 1;
715 /* FIXME */
716 if (is_suid)
717 fprintf(stderr, "%s: is setuid\n", filename);
720 interpreter_already_found = 0;
721 #ifdef __LDSO_LDD_SUPPORT__
722 interp = find_elf_interpreter(ehdr);
724 if (interp
725 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
726 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
727 && ehdr->e_ident[EI_DATA] == ELFDATAM
728 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
729 && MATCH_MACHINE(ehdr->e_machine))
731 struct stat st;
732 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
733 pid_t pid;
734 int status;
735 static const char *const environment[] = {
736 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
737 "SHELL=/bin/sh",
738 "LD_TRACE_LOADED_OBJECTS=1",
739 NULL
741 # ifdef __LDSO_STANDALONE_SUPPORT__
742 char * lib_path = getenv("LD_LIBRARY_PATH");
744 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
745 static char *ext_environment[ARRAY_SIZE(environment) + 1];
746 char **envp = (char **) environment;
748 if (lib_path) {
750 * If the LD_LIBRARY_PATH is set, it needs to include it
751 * into the environment for the new process to be spawned
753 char ** eenvp = (char **) ext_environment;
755 /* Copy the N-1 environment's entries */
756 while (*envp)
757 *eenvp++=*envp++;
759 /* Make room for LD_LIBRARY_PATH */
760 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
761 + strlen(lib_path));
762 strcpy(*eenvp, "LD_LIBRARY_PATH=");
763 strcat(*eenvp, lib_path);
764 lib_path = *eenvp;
765 /* ext_environment[size] is already NULL */
767 /* Use the extended environment */
768 envp = ext_environment;
770 if ((pid = vfork()) == 0) {
772 * Force to use the standard dynamic linker in stand-alone mode.
773 * It will fails at runtime if support is not actually available
775 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
776 _exit(0xdead);
778 # else
779 if ((pid = vfork()) == 0) {
780 /* Cool, it looks like we should be able to actually
781 * run this puppy. Do so now... */
782 execle(filename, filename, NULL, environment);
783 _exit(0xdead);
785 # endif
786 /* Wait till it returns */
787 waitpid(pid, &status, 0);
789 # ifdef __LDSO_STANDALONE_SUPPORT__
790 /* Do not leak */
791 free(lib_path);
792 # endif
794 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
795 return 1;
798 /* If the exec failed, we fall through to trying to find
799 * all the needed libraries ourselves by rummaging about
800 * in the ELF headers... */
803 #endif
805 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
806 if (dynsec) {
807 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
808 find_needed_libraries(ehdr, dynamic, is_suid);
811 return 0;
814 int main(int argc, char **argv)
816 int multi = 0;
817 int got_em_all = 1;
818 char *filename = NULL;
819 struct library *cur;
821 if (argc < 2) {
822 fprintf(stderr, "ldd: missing file arguments\n"
823 "Try `ldd --help' for more information.\n");
824 exit(EXIT_FAILURE);
826 if (argc > 2)
827 multi++;
829 while (--argc > 0) {
830 ++argv;
832 if (strcmp(*argv, "--") == 0) {
833 /* Ignore "--" */
834 continue;
837 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
838 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
839 "\t--help\t\tprint this help and exit\n");
840 exit(EXIT_SUCCESS);
843 filename = *argv;
844 if (!filename) {
845 fprintf(stderr, "No filename specified.\n");
846 exit(EXIT_FAILURE);
849 if (multi) {
850 printf("%s:\n", *argv);
853 map_cache();
855 if (find_dependencies(filename) != 0)
856 continue;
858 while (got_em_all) {
859 got_em_all = 0;
860 /* Keep walking the list till everybody is resolved */
861 for (cur = lib_list; cur; cur = cur->next) {
862 if (cur->resolved == 0 && cur->path) {
863 got_em_all = 1;
864 printf("checking sub-depends for '%s'\n", cur->path);
865 find_dependencies(cur->path);
866 cur->resolved = 1;
871 unmap_cache();
873 /* Print the list */
874 got_em_all = 0;
875 for (cur = lib_list; cur; cur = cur->next) {
876 got_em_all = 1;
877 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
879 if (interp_name && interpreter_already_found == 1)
880 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
881 else
882 printf("\tnot a dynamic executable\n");
884 for (cur = lib_list; cur; cur = cur->next) {
885 free(cur->name);
886 cur->name = NULL;
887 if (cur->path && cur->path != not_found) {
888 free(cur->path);
889 cur->path = NULL;
892 lib_list = NULL;
895 return 0;