depend on __UCLIBC_HAVE_STATX__
[uclibc-ng.git] / utils / ldd.c
blob46cd971704335d1333c17c488d7c349839734c01
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(__kvx__)
18 #define MATCH_MACHINE(x) (x == EM_KVX)
19 #define ELFCLASSM ELFCLASS64
20 #endif
22 #if defined(__aarch64__)
23 #define MATCH_MACHINE(x) (x == EM_AARCH64)
24 #define ELFCLASSM ELFCLASS64
25 #endif
27 #if defined(__alpha__)
28 #define MATCH_MACHINE(x) (x == EM_ALPHA)
29 #define ELFCLASSM ELFCLASS64
30 #endif
32 #if defined(__arc__)
33 #define MATCH_MACHINE(x) (x == EM_ARCOMPACT || x == EM_ARCV2)
34 #define ELFCLASSM ELFCLASS32
35 #endif
37 #if defined(__ARC64_ARCH32__)
38 #define MATCH_MACHINE(x) (x == EM_ARCV3_32)
39 #define ELFCLASSM ELFCLASS32
40 #endif
42 #if defined(__arm__) || defined(__thumb__)
43 #define MATCH_MACHINE(x) (x == EM_ARM)
44 #define ELFCLASSM ELFCLASS32
45 #endif
47 #if defined(__avr32__)
48 #define MATCH_MACHINE(x) (x == EM_AVR32)
49 #define ELFCLASSM ELFCLASS32
50 #endif
52 #if defined(__bfin__)
53 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
54 #define ELFCLASSM ELFCLASS32
55 #endif
57 #if defined(__TMS320C6X__)
58 #define MATCH_MACHINE(x) (x == EM_TI_C6000)
59 #define ELFCLASSM ELFCLASS32
60 #endif
62 #if defined(__cris__)
63 #define MATCH_MACHINE(x) (x == EM_CRIS)
64 #define ELFCLASSM ELFCLASS32
65 #endif
67 #if defined(__csky__)
68 #define MATCH_MACHINE(x) (x == EM_MCORE)
69 #define ELFCLASSM ELFCLASS32
70 #endif
72 #if defined(__frv__)
73 #define MATCH_MACHINE(x) (x == EM_CYGNUS_FRV)
74 #define ELFCLASSM ELFCLASS32
75 #endif
77 #if defined(__hppa__)
78 #define MATCH_MACHINE(x) (x == EM_PARISC)
79 #if defined(__LP64__)
80 #define ELFCLASSM ELFCLASS64
81 #else
82 #define ELFCLASSM ELFCLASS32
83 #endif
84 #endif
86 #if defined(__i386__)
87 #ifndef EM_486
88 #define MATCH_MACHINE(x) (x == EM_386)
89 #else
90 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
91 #endif
92 #define ELFCLASSM ELFCLASS32
93 #endif
95 #if defined(__ia64__)
96 #define MATCH_MACHINE(x) (x == EM_IA_64)
97 #define ELFCLASSM ELFCLASS64
98 #endif
100 #if defined(__mc68000__)
101 #define MATCH_MACHINE(x) (x == EM_68K)
102 #define ELFCLASSM ELFCLASS32
103 #endif
105 #if defined(__metag__)
106 #define MATCH_MACHINE(x) (x == EM_METAG)
107 #define ELFCLASSM ELFCLASS32
108 #endif
110 #if defined(__microblaze__)
111 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE)
112 #define ELFCLASSM ELFCLASS32
113 #endif
115 #if defined(__mips__)
116 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
117 #define ELFCLASSM ELFCLASS32
118 #endif
120 #if defined(__nds32__)
121 #define MATCH_MACHINE(x) (x == EM_NDS32)
122 #define ELFCLASSM ELFCLASS32
123 #endif
125 #if defined(__nios2__)
126 #define MATCH_MACHINE(x) (x == EM_NIOS32)
127 #define ELFCLASSM ELFCLASS32
128 #endif
130 #if defined(__powerpc__)
131 #define MATCH_MACHINE(x) (x == EM_PPC)
132 #define ELFCLASSM ELFCLASS32
133 #endif
135 #if defined(__riscv)
136 #define MATCH_MACHINE(x) (x == EM_RISCV)
137 #define ELFCLASSM ELFCLASS64
138 #endif
140 #if defined(__sh__)
141 #define MATCH_MACHINE(x) (x == EM_SH)
142 #define ELFCLASSM ELFCLASS32
143 #endif
145 #if defined(__sparc__)
146 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
147 #define ELFCLASSM ELFCLASS32
148 #endif
150 #if defined(__x86_64__)
151 #define MATCH_MACHINE(x) (x == EM_X86_64)
152 #define ELFCLASSM ELFCLASS64
153 #endif
155 #if defined(__xtensa__)
156 #define MATCH_MACHINE(x) (x == EM_XTENSA)
157 #define ELFCLASSM ELFCLASS32
158 #endif
160 #ifndef MATCH_MACHINE
161 # ifdef __linux__
162 # include <asm/elf.h>
163 # endif
164 # ifdef ELF_ARCH
165 # define MATCH_MACHINE(x) (x == ELF_ARCH)
166 # endif
167 # ifdef ELF_CLASS
168 # define ELFCLASSM ELF_CLASS
169 # endif
170 #endif
171 #ifndef MATCH_MACHINE
172 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
173 #endif
175 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
176 #define ELFDATAM ELFDATA2LSB
177 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
178 #define ELFDATAM ELFDATA2MSB
179 #endif
181 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
183 struct library {
184 char *name;
185 int resolved;
186 char *path;
187 struct library *next;
189 static struct library *lib_list = NULL;
190 static char not_found[] = "not found";
191 static char *interp_name = NULL;
192 static char *interp_dir = NULL;
193 static int byteswap;
194 static int interpreter_already_found = 0;
196 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
198 if (byteswap) {
199 return (bswap_32(value));
200 } else {
201 return (value);
204 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
206 if (byteswap) {
207 return (bswap_64(value));
208 } else {
209 return (value);
213 #if __WORDSIZE == 64
214 # define byteswap_to_host(x) byteswap64_to_host(x)
215 #else
216 # define byteswap_to_host(x) byteswap32_to_host(x)
217 #endif
219 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
221 int j;
222 ElfW(Shdr) *shdr;
223 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
224 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
225 if (key == byteswap32_to_host(shdr->sh_type)) {
226 return shdr;
229 return NULL;
232 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
234 int j;
235 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
236 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
237 if (type == byteswap32_to_host(phdr->p_type)) {
238 return phdr;
241 return NULL;
244 /* Returns value if return_val==1, ptr otherwise */
245 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
246 ElfW(Ehdr) *ehdr, int return_val)
248 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
249 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
250 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
251 if (key == byteswap_to_host(dynp->d_tag)) {
252 if (return_val == 1)
253 return (void *)byteswap_to_host(dynp->d_un.d_val);
254 else
255 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
258 return NULL;
261 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
263 ElfW(Dyn) *dyns;
265 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
266 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
267 char *strtab;
268 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
269 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
272 return NULL;
275 static int check_elf_header(ElfW(Ehdr) *const ehdr)
277 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
278 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
279 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
280 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
282 return 1;
285 /* Check if the target endianness matches the host's endianness */
286 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
288 /* Be very lazy, and only byteswap the stuff we use */
289 if (byteswap) {
290 ehdr->e_type = bswap_16(ehdr->e_type);
291 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
292 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
293 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
294 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
297 return 0;
300 #ifdef __LDSO_CACHE_SUPPORT__
301 static caddr_t cache_addr = NULL;
302 static size_t cache_size = 0;
304 static int map_cache(void)
306 int fd;
307 struct stat st;
308 header_t *header;
309 libentry_t *libent;
310 int i, strtabsize;
312 if (cache_addr == (caddr_t) - 1)
313 return -1;
314 else if (cache_addr != NULL)
315 return 0;
317 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
318 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
319 cache_addr = (caddr_t) - 1; /* so we won't try again */
320 return -1;
323 cache_size = st.st_size;
324 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
325 close(fd);
326 if (cache_addr == MAP_FAILED) {
327 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
328 return -1;
331 header = (header_t *) cache_addr;
333 if (cache_size < sizeof(header_t)
334 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
335 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
336 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
337 || cache_addr[cache_size - 1] != '\0')
339 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
340 goto fail;
343 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
344 libent = (libentry_t *) & header[1];
346 for (i = 0; i < header->nlibs; i++) {
347 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
348 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
349 goto fail;
353 return 0;
355 fail:
356 munmap(cache_addr, cache_size);
357 cache_addr = (caddr_t) - 1;
358 return -1;
361 static int unmap_cache(void)
363 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
364 return -1;
366 #if 1
367 munmap(cache_addr, cache_size);
368 cache_addr = NULL;
369 #endif
371 return 0;
373 #else
374 static __inline__ void map_cache(void)
377 static __inline__ void unmap_cache(void)
380 #endif
382 /* This function's behavior must exactly match that
383 * in uClibc/ldso/ldso/dl-elf.c */
384 static void search_for_named_library(char *name, char *result,
385 const char *path_list)
387 int i, count = 1;
388 char *path, *path_n;
389 struct stat filestat;
391 /* We need a writable copy of this string */
392 path = strdup(path_list);
393 if (!path) {
394 fprintf(stderr, "%s: Out of memory!\n", __func__);
395 exit(EXIT_FAILURE);
397 /* Eliminate all double //s */
398 path_n = path;
399 while ((path_n = strstr(path_n, "//"))) {
400 i = strlen(path_n);
401 memmove(path_n, path_n + 1, i - 1);
402 *(path_n + i - 1) = '\0';
405 /* Replace colons with zeros in path_list and count them */
406 for (i = strlen(path); i > 0; i--) {
407 if (path[i] == ':') {
408 path[i] = 0;
409 count++;
412 path_n = path;
413 for (i = 0; i < count; i++) {
414 strcpy(result, path_n);
415 strcat(result, "/");
416 strcat(result, name);
417 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
418 free(path);
419 return;
421 path_n += (strlen(path_n) + 1);
423 free(path);
424 *result = '\0';
427 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
428 int is_suid, struct library *lib)
430 char *buf;
431 char *path;
432 struct stat filestat;
434 /* If this is a fully resolved name, our job is easy */
435 if (stat(lib->name, &filestat) == 0) {
436 lib->path = strdup(lib->name);
437 return;
440 /* We need some elbow room here. Make some room... */
441 buf = malloc(1024);
442 if (!buf) {
443 fprintf(stderr, "%s: Out of memory!\n", __func__);
444 exit(EXIT_FAILURE);
447 /* This function must match the behavior of _dl_load_shared_library
448 * in readelflib1.c or things won't work out as expected... */
450 /* The ABI specifies that RPATH is searched first, so do that now. */
451 path = elf_find_rpath(ehdr, dynamic);
452 if (path) {
453 search_for_named_library(lib->name, buf, path);
454 if (*buf != '\0') {
455 lib->path = buf;
456 return;
460 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
461 * Since this app doesn't actually run an executable I will skip
462 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
463 if (is_suid == 1)
464 path = NULL;
465 else
466 path = getenv("LD_LIBRARY_PATH");
467 if (path) {
468 search_for_named_library(lib->name, buf, path);
469 if (*buf != '\0') {
470 lib->path = buf;
471 return;
474 #ifdef __LDSO_CACHE_SUPPORT__
475 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
476 int i;
477 header_t *header = (header_t *) cache_addr;
478 libentry_t *libent = (libentry_t *) & header[1];
479 char *strs = (char *)&libent[header->nlibs];
481 for (i = 0; i < header->nlibs; i++) {
482 if ((libent[i].flags == LIB_ELF ||
483 libent[i].flags == LIB_ELF_LIBC0 ||
484 libent[i].flags == LIB_ELF_LIBC5) &&
485 strcmp(lib->name, strs + libent[i].sooffset) == 0)
487 lib->path = strdup(strs + libent[i].liboffset);
488 return;
492 #endif
494 /* Next look for libraries wherever the shared library
495 * loader was installed -- this is usually where we
496 * should find things... */
497 if (interp_dir) {
498 search_for_named_library(lib->name, buf, interp_dir);
499 if (*buf != '\0') {
500 lib->path = buf;
501 return;
505 /* Lastly, search the standard list of paths for the library.
506 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
507 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
508 #ifndef __LDSO_CACHE_SUPPORT__
509 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
510 #endif
512 search_for_named_library(lib->name, buf, path);
513 if (*buf != '\0') {
514 lib->path = buf;
515 } else {
516 free(buf);
517 lib->path = not_found;
521 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
523 char *tmp, *tmp1, *tmp2;
524 struct library *cur, *newlib = lib_list;
526 if (!s || !strlen(s))
527 return 1;
529 tmp = s;
530 while (*tmp) {
531 if (*tmp == '/')
532 s = tmp + 1;
533 tmp++;
536 /* We add ldso elsewhere */
537 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
538 int len = strlen(interp_dir);
539 if (strcmp(s, interp_name + 1 + len) == 0)
540 return 1;
543 for (cur = lib_list; cur; cur = cur->next) {
544 /* Check if this library is already in the list */
545 tmp1 = tmp2 = cur->name;
546 if (!cur->name)
547 continue;
548 while (*tmp1) {
549 if (*tmp1 == '/')
550 tmp2 = tmp1 + 1;
551 tmp1++;
553 if (strcmp(tmp2, s) == 0) {
554 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
555 return 0;
559 /* Ok, this lib needs to be added to the list */
560 newlib = malloc(sizeof(struct library));
561 if (!newlib)
562 return 1;
563 newlib->name = malloc(strlen(s) + 1);
564 strcpy(newlib->name, s);
565 newlib->resolved = 0;
566 newlib->path = NULL;
567 newlib->next = NULL;
569 /* Now try and locate where this library might be living... */
570 locate_library_file(ehdr, dynamic, is_setuid, newlib);
572 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
573 if (!lib_list) {
574 lib_list = newlib;
575 } else {
576 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
577 cur->next = newlib;
579 return 0;
582 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
584 ElfW(Dyn) *dyns;
586 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
587 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
588 char *strtab;
589 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
590 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
595 #ifdef __LDSO_LDD_SUPPORT__
596 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
598 ElfW(Phdr) *phdr;
600 if (interpreter_already_found == 1)
601 return NULL;
602 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
603 if (phdr) {
604 struct library *cur, *newlib = NULL;
605 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
607 char *tmp, *tmp1;
608 interp_name = strdup(s);
609 interp_dir = strdup(s);
610 tmp = strrchr(interp_dir, '/');
611 if (tmp)
612 *tmp = '\0';
613 else {
614 free(interp_dir);
615 interp_dir = interp_name;
617 tmp1 = tmp = s;
618 while (*tmp) {
619 if (*tmp == '/')
620 tmp1 = tmp + 1;
621 tmp++;
623 for (cur = lib_list; cur; cur = cur->next) {
624 /* Check if this library is already in the list */
625 if (!tmp1 || !cur->name)
626 return NULL;
627 if (strcmp(cur->name, tmp1) == 0) {
628 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
629 newlib = cur;
630 free(newlib->name);
631 if (newlib->path != not_found) {
632 free(newlib->path);
634 newlib->name = NULL;
635 newlib->path = NULL;
636 break;
639 if (newlib == NULL)
640 newlib = malloc(sizeof(struct library));
641 if (!newlib)
642 return NULL;
643 newlib->name = malloc(strlen(s) + 1);
644 strcpy(newlib->name, s);
645 newlib->path = strdup(newlib->name);
646 newlib->resolved = 1;
647 newlib->next = NULL;
649 #if 0
650 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
651 if (!lib_list) {
652 lib_list = newlib;
653 } else {
654 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
655 cur->next = newlib;
657 #endif
658 interpreter_already_found = 1;
659 return newlib;
661 return NULL;
663 #endif /* __LDSO_LDD_SUPPORT__ */
665 /* map the .so, and locate interesting pieces */
667 #warning "There may be two warnings here about vfork() clobbering, ignore them"
669 static int find_dependencies(char *filename)
671 int is_suid = 0;
672 FILE *thefile;
673 struct stat statbuf;
674 ElfW(Ehdr) *ehdr = NULL;
675 ElfW(Shdr) *dynsec = NULL;
676 ElfW(Dyn) *dynamic = NULL;
677 #ifdef __LDSO_LDD_SUPPORT__
678 struct library *interp;
679 #endif
681 if (filename == not_found)
682 return 0;
684 if (!filename) {
685 fprintf(stderr, "No filename specified.\n");
686 return -1;
688 if (!(thefile = fopen(filename, "r"))) {
689 perror(filename);
690 return -1;
692 if (fstat(fileno(thefile), &statbuf) < 0) {
693 perror(filename);
694 fclose(thefile);
695 return -1;
698 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
699 goto foo;
701 if (!S_ISREG(statbuf.st_mode))
702 goto foo;
704 /* mmap the file to make reading stuff from it effortless */
705 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
706 if (ehdr == MAP_FAILED) {
707 fclose(thefile);
708 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
709 return -1;
712 foo:
713 fclose(thefile);
715 /* Check if this looks like a legit ELF file */
716 if (check_elf_header(ehdr)) {
717 fprintf(stderr, "%s: not an ELF file.\n", filename);
718 return -1;
720 /* Check if this is the right kind of ELF file */
721 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
722 fprintf(stderr, "%s: not a dynamic executable\n", filename);
723 return -1;
725 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
726 if (statbuf.st_mode & S_ISUID)
727 is_suid = 1;
728 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
729 is_suid = 1;
730 /* FIXME */
731 if (is_suid)
732 fprintf(stderr, "%s: is setuid\n", filename);
735 interpreter_already_found = 0;
736 #ifdef __LDSO_LDD_SUPPORT__
737 interp = find_elf_interpreter(ehdr);
739 if (interp
740 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
741 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
742 && ehdr->e_ident[EI_DATA] == ELFDATAM
743 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
744 && MATCH_MACHINE(ehdr->e_machine))
746 struct stat st;
747 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
748 pid_t pid;
749 int status;
750 static const char *const environment[] = {
751 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
752 "SHELL=/bin/sh",
753 "LD_TRACE_LOADED_OBJECTS=1",
754 NULL
756 # ifdef __LDSO_STANDALONE_SUPPORT__
757 char * lib_path = getenv("LD_LIBRARY_PATH");
759 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
760 static char *ext_environment[ARRAY_SIZE(environment) + 1];
761 char **envp = (char **) environment;
763 if (lib_path) {
765 * If the LD_LIBRARY_PATH is set, it needs to include it
766 * into the environment for the new process to be spawned
768 char ** eenvp = (char **) ext_environment;
770 /* Copy the N-1 environment's entries */
771 while (*envp)
772 *eenvp++=*envp++;
774 /* Make room for LD_LIBRARY_PATH */
775 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
776 + strlen(lib_path));
777 strcpy(*eenvp, "LD_LIBRARY_PATH=");
778 strcat(*eenvp, lib_path);
779 lib_path = *eenvp;
780 /* ext_environment[size] is already NULL */
782 /* Use the extended environment */
783 envp = ext_environment;
785 if ((pid = vfork()) == 0) {
787 * Force to use the standard dynamic linker in stand-alone mode.
788 * It will fails at runtime if support is not actually available
790 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
791 _exit(0xdead);
793 # else
794 if ((pid = vfork()) == 0) {
795 /* Cool, it looks like we should be able to actually
796 * run this puppy. Do so now... */
797 execle(filename, filename, NULL, environment);
798 _exit(0xdead);
800 # endif
801 /* Wait till it returns */
802 waitpid(pid, &status, 0);
804 # ifdef __LDSO_STANDALONE_SUPPORT__
805 /* Do not leak */
806 free(lib_path);
807 # endif
809 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
810 return 1;
813 /* If the exec failed, we fall through to trying to find
814 * all the needed libraries ourselves by rummaging about
815 * in the ELF headers... */
818 #endif
820 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
821 if (dynsec) {
822 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
823 find_needed_libraries(ehdr, dynamic, is_suid);
826 return 0;
829 int main(int argc, char **argv)
831 int multi = 0;
832 int got_em_all = 1;
833 char *filename = NULL;
834 struct library *cur;
836 if (argc < 2) {
837 fprintf(stderr, "ldd: missing file arguments\n"
838 "Try `ldd --help' for more information.\n");
839 exit(EXIT_FAILURE);
841 if (argc > 2)
842 multi++;
844 while (--argc > 0) {
845 ++argv;
847 if (strcmp(*argv, "--") == 0) {
848 /* Ignore "--" */
849 continue;
852 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
853 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
854 "\t--help\t\tprint this help and exit\n");
855 exit(EXIT_SUCCESS);
858 filename = *argv;
859 if (!filename) {
860 fprintf(stderr, "No filename specified.\n");
861 exit(EXIT_FAILURE);
864 if (multi) {
865 printf("%s:\n", *argv);
868 map_cache();
870 if (find_dependencies(filename) != 0)
871 continue;
873 while (got_em_all) {
874 got_em_all = 0;
875 /* Keep walking the list till everybody is resolved */
876 for (cur = lib_list; cur; cur = cur->next) {
877 if (cur->resolved == 0 && cur->path) {
878 got_em_all = 1;
879 printf("checking sub-depends for '%s'\n", cur->path);
880 find_dependencies(cur->path);
881 cur->resolved = 1;
886 unmap_cache();
888 /* Print the list */
889 got_em_all = 0;
890 for (cur = lib_list; cur; cur = cur->next) {
891 got_em_all = 1;
892 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
894 if (interp_name && interpreter_already_found == 1)
895 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
896 else
897 printf("\tnot a dynamic executable\n");
899 for (cur = lib_list; cur; cur = cur->next) {
900 free(cur->name);
901 cur->name = NULL;
902 if (cur->path && cur->path != not_found) {
903 free(cur->path);
904 cur->path = NULL;
907 lib_list = NULL;
910 return 0;