add experimental aarch64 support
[uclibc-ng.git] / utils / ldd.c
blob37a069b4f6b6c36d0c1722c73582893edd82e535
1 /* vi: set sw=4 ts=4: */
2 /*
3 * A small little ldd implementation for uClibc
5 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
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).
13 * Licensed under GPLv2 or later
16 #include "porting.h"
18 #if defined(__aarch64__)
19 #define MATCH_MACHINE(x) (x == EM_AARCH64)
20 #define ELFCLASSM ELFCLASS64
21 #endif
23 #if defined(__alpha__)
24 #define MATCH_MACHINE(x) (x == EM_ALPHA)
25 #define ELFCLASSM ELFCLASS64
26 #endif
28 #if defined(__arc__)
29 #define MATCH_MACHINE(x) (x == EM_ARCOMPACT)
30 #define ELFCLASSM ELFCLASS32
31 #endif
33 #if defined(__arm__) || defined(__thumb__)
34 #define MATCH_MACHINE(x) (x == EM_ARM)
35 #define ELFCLASSM ELFCLASS32
36 #endif
38 #if defined(__avr32__)
39 #define MATCH_MACHINE(x) (x == EM_AVR32)
40 #define ELFCLASSM ELFCLASS32
41 #endif
43 #if defined(__bfin__)
44 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
45 #define ELFCLASSM ELFCLASS32
46 #endif
48 #if defined(__TMS320C6X__)
49 #define MATCH_MACHINE(x) (x == EM_TI_C6000)
50 #define ELFCLASSM ELFCLASS32
51 #endif
53 #if defined(__cris__)
54 #define MATCH_MACHINE(x) (x == EM_CRIS)
55 #define ELFCLASSM ELFCLASS32
56 #endif
58 #if defined(__frv__)
59 #define MATCH_MACHINE(x) (x == EM_CYGNUS_FRV)
60 #define ELFCLASSM ELFCLASS32
61 #endif
63 #if defined(__hppa__)
64 #define MATCH_MACHINE(x) (x == EM_PARISC)
65 #if defined(__LP64__)
66 #define ELFCLASSM ELFCLASS64
67 #else
68 #define ELFCLASSM ELFCLASS32
69 #endif
70 #endif
72 #if defined(__i386__)
73 #ifndef EM_486
74 #define MATCH_MACHINE(x) (x == EM_386)
75 #else
76 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
77 #endif
78 #define ELFCLASSM ELFCLASS32
79 #endif
81 #if defined(__ia64__)
82 #define MATCH_MACHINE(x) (x == EM_IA_64)
83 #define ELFCLASSM ELFCLASS64
84 #endif
86 #if defined(__mc68000__)
87 #define MATCH_MACHINE(x) (x == EM_68K)
88 #define ELFCLASSM ELFCLASS32
89 #endif
91 #if defined(__metag__)
92 #define MATCH_MACHINE(x) (x == EM_METAG)
93 #define ELFCLASSM ELFCLASS32
94 #endif
96 #if defined(__microblaze__)
97 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE)
98 #define ELFCLASSM ELFCLASS32
99 #endif
101 #if defined(__mips__)
102 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
103 #define ELFCLASSM ELFCLASS32
104 #endif
106 #if defined(__nds32__)
107 #define MATCH_MACHINE(x) (x == EM_NDS32)
108 #define ELFCLASSM ELFCLASS32
109 #endif
111 #if defined(__nios2__)
112 #define MATCH_MACHINE(x) (x == EM_NIOS32)
113 #define ELFCLASSM ELFCLASS32
114 #endif
116 #if defined(__powerpc__)
117 #define MATCH_MACHINE(x) (x == EM_PPC)
118 #define ELFCLASSM ELFCLASS32
119 #endif
121 #if defined(__sh__)
122 #define MATCH_MACHINE(x) (x == EM_SH)
123 #define ELFCLASSM ELFCLASS32
124 #endif
126 #if defined(__sparc__)
127 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
128 #define ELFCLASSM ELFCLASS32
129 #endif
131 #if defined(__x86_64__)
132 #define MATCH_MACHINE(x) (x == EM_X86_64)
133 #define ELFCLASSM ELFCLASS64
134 #endif
136 #if defined(__xtensa__)
137 #define MATCH_MACHINE(x) (x == EM_XTENSA)
138 #define ELFCLASSM ELFCLASS32
139 #endif
141 #ifndef MATCH_MACHINE
142 # ifdef __linux__
143 # include <asm/elf.h>
144 # endif
145 # ifdef ELF_ARCH
146 # define MATCH_MACHINE(x) (x == ELF_ARCH)
147 # endif
148 # ifdef ELF_CLASS
149 # define ELFCLASSM ELF_CLASS
150 # endif
151 #endif
152 #ifndef MATCH_MACHINE
153 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
154 #endif
156 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
157 #define ELFDATAM ELFDATA2LSB
158 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
159 #define ELFDATAM ELFDATA2MSB
160 #endif
162 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
164 struct library {
165 char *name;
166 int resolved;
167 char *path;
168 struct library *next;
170 static struct library *lib_list = NULL;
171 static char not_found[] = "not found";
172 static char *interp_name = NULL;
173 static char *interp_dir = NULL;
174 static int byteswap;
175 static int interpreter_already_found = 0;
177 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
179 if (byteswap) {
180 return (bswap_32(value));
181 } else {
182 return (value);
185 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
187 if (byteswap) {
188 return (bswap_64(value));
189 } else {
190 return (value);
194 #if __WORDSIZE == 64
195 # define byteswap_to_host(x) byteswap64_to_host(x)
196 #else
197 # define byteswap_to_host(x) byteswap32_to_host(x)
198 #endif
200 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
202 int j;
203 ElfW(Shdr) *shdr;
204 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
205 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
206 if (key == byteswap32_to_host(shdr->sh_type)) {
207 return shdr;
210 return NULL;
213 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
215 int j;
216 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
217 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
218 if (type == byteswap32_to_host(phdr->p_type)) {
219 return phdr;
222 return NULL;
225 /* Returns value if return_val==1, ptr otherwise */
226 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
227 ElfW(Ehdr) *ehdr, int return_val)
229 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
230 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
231 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
232 if (key == byteswap_to_host(dynp->d_tag)) {
233 if (return_val == 1)
234 return (void *)byteswap_to_host(dynp->d_un.d_val);
235 else
236 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
239 return NULL;
242 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
244 ElfW(Dyn) *dyns;
246 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
247 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
248 char *strtab;
249 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
250 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
253 return NULL;
256 static int check_elf_header(ElfW(Ehdr) *const ehdr)
258 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
259 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
260 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
261 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
263 return 1;
266 /* Check if the target endianness matches the host's endianness */
267 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
269 /* Be very lazy, and only byteswap the stuff we use */
270 if (byteswap) {
271 ehdr->e_type = bswap_16(ehdr->e_type);
272 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
273 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
274 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
275 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
278 return 0;
281 #ifdef __LDSO_CACHE_SUPPORT__
282 static caddr_t cache_addr = NULL;
283 static size_t cache_size = 0;
285 static int map_cache(void)
287 int fd;
288 struct stat st;
289 header_t *header;
290 libentry_t *libent;
291 int i, strtabsize;
293 if (cache_addr == (caddr_t) - 1)
294 return -1;
295 else if (cache_addr != NULL)
296 return 0;
298 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
299 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
300 cache_addr = (caddr_t) - 1; /* so we won't try again */
301 return -1;
304 cache_size = st.st_size;
305 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
306 close(fd);
307 if (cache_addr == MAP_FAILED) {
308 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
309 return -1;
312 header = (header_t *) cache_addr;
314 if (cache_size < sizeof(header_t)
315 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
316 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
317 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
318 || cache_addr[cache_size - 1] != '\0')
320 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
321 goto fail;
324 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
325 libent = (libentry_t *) & header[1];
327 for (i = 0; i < header->nlibs; i++) {
328 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
329 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
330 goto fail;
334 return 0;
336 fail:
337 munmap(cache_addr, cache_size);
338 cache_addr = (caddr_t) - 1;
339 return -1;
342 static int unmap_cache(void)
344 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
345 return -1;
347 #if 1
348 munmap(cache_addr, cache_size);
349 cache_addr = NULL;
350 #endif
352 return 0;
354 #else
355 static __inline__ void map_cache(void)
358 static __inline__ void unmap_cache(void)
361 #endif
363 /* This function's behavior must exactly match that
364 * in uClibc/ldso/ldso/dl-elf.c */
365 static void search_for_named_library(char *name, char *result,
366 const char *path_list)
368 int i, count = 1;
369 char *path, *path_n;
370 struct stat filestat;
372 /* We need a writable copy of this string */
373 path = strdup(path_list);
374 if (!path) {
375 fprintf(stderr, "%s: Out of memory!\n", __func__);
376 exit(EXIT_FAILURE);
378 /* Eliminate all double //s */
379 path_n = path;
380 while ((path_n = strstr(path_n, "//"))) {
381 i = strlen(path_n);
382 memmove(path_n, path_n + 1, i - 1);
383 *(path_n + i - 1) = '\0';
386 /* Replace colons with zeros in path_list and count them */
387 for (i = strlen(path); i > 0; i--) {
388 if (path[i] == ':') {
389 path[i] = 0;
390 count++;
393 path_n = path;
394 for (i = 0; i < count; i++) {
395 strcpy(result, path_n);
396 strcat(result, "/");
397 strcat(result, name);
398 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
399 free(path);
400 return;
402 path_n += (strlen(path_n) + 1);
404 free(path);
405 *result = '\0';
408 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
409 int is_suid, struct library *lib)
411 char *buf;
412 char *path;
413 struct stat filestat;
415 /* If this is a fully resolved name, our job is easy */
416 if (stat(lib->name, &filestat) == 0) {
417 lib->path = strdup(lib->name);
418 return;
421 /* We need some elbow room here. Make some room... */
422 buf = malloc(1024);
423 if (!buf) {
424 fprintf(stderr, "%s: Out of memory!\n", __func__);
425 exit(EXIT_FAILURE);
428 /* This function must match the behavior of _dl_load_shared_library
429 * in readelflib1.c or things won't work out as expected... */
431 /* The ABI specifies that RPATH is searched first, so do that now. */
432 path = elf_find_rpath(ehdr, dynamic);
433 if (path) {
434 search_for_named_library(lib->name, buf, path);
435 if (*buf != '\0') {
436 lib->path = buf;
437 return;
441 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
442 * Since this app doesn't actually run an executable I will skip
443 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
444 if (is_suid == 1)
445 path = NULL;
446 else
447 path = getenv("LD_LIBRARY_PATH");
448 if (path) {
449 search_for_named_library(lib->name, buf, path);
450 if (*buf != '\0') {
451 lib->path = buf;
452 return;
455 #ifdef __LDSO_CACHE_SUPPORT__
456 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
457 int i;
458 header_t *header = (header_t *) cache_addr;
459 libentry_t *libent = (libentry_t *) & header[1];
460 char *strs = (char *)&libent[header->nlibs];
462 for (i = 0; i < header->nlibs; i++) {
463 if ((libent[i].flags == LIB_ELF ||
464 libent[i].flags == LIB_ELF_LIBC0 ||
465 libent[i].flags == LIB_ELF_LIBC5) &&
466 strcmp(lib->name, strs + libent[i].sooffset) == 0)
468 lib->path = strdup(strs + libent[i].liboffset);
469 return;
473 #endif
475 /* Next look for libraries wherever the shared library
476 * loader was installed -- this is usually where we
477 * should find things... */
478 if (interp_dir) {
479 search_for_named_library(lib->name, buf, interp_dir);
480 if (*buf != '\0') {
481 lib->path = buf;
482 return;
486 /* Lastly, search the standard list of paths for the library.
487 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
488 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
489 #ifndef __LDSO_CACHE_SUPPORT__
490 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
491 #endif
493 search_for_named_library(lib->name, buf, path);
494 if (*buf != '\0') {
495 lib->path = buf;
496 } else {
497 free(buf);
498 lib->path = not_found;
502 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
504 char *tmp, *tmp1, *tmp2;
505 struct library *cur, *newlib = lib_list;
507 if (!s || !strlen(s))
508 return 1;
510 tmp = s;
511 while (*tmp) {
512 if (*tmp == '/')
513 s = tmp + 1;
514 tmp++;
517 /* We add ldso elsewhere */
518 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
519 int len = strlen(interp_dir);
520 if (strcmp(s, interp_name + 1 + len) == 0)
521 return 1;
524 for (cur = lib_list; cur; cur = cur->next) {
525 /* Check if this library is already in the list */
526 tmp1 = tmp2 = cur->name;
527 if (!cur->name)
528 continue;
529 while (*tmp1) {
530 if (*tmp1 == '/')
531 tmp2 = tmp1 + 1;
532 tmp1++;
534 if (strcmp(tmp2, s) == 0) {
535 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
536 return 0;
540 /* Ok, this lib needs to be added to the list */
541 newlib = malloc(sizeof(struct library));
542 if (!newlib)
543 return 1;
544 newlib->name = malloc(strlen(s) + 1);
545 strcpy(newlib->name, s);
546 newlib->resolved = 0;
547 newlib->path = NULL;
548 newlib->next = NULL;
550 /* Now try and locate where this library might be living... */
551 locate_library_file(ehdr, dynamic, is_setuid, newlib);
553 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
554 if (!lib_list) {
555 lib_list = newlib;
556 } else {
557 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
558 cur->next = newlib;
560 return 0;
563 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
565 ElfW(Dyn) *dyns;
567 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
568 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
569 char *strtab;
570 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
571 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
576 #ifdef __LDSO_LDD_SUPPORT__
577 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
579 ElfW(Phdr) *phdr;
581 if (interpreter_already_found == 1)
582 return NULL;
583 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
584 if (phdr) {
585 struct library *cur, *newlib = NULL;
586 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
588 char *tmp, *tmp1;
589 interp_name = strdup(s);
590 interp_dir = strdup(s);
591 tmp = strrchr(interp_dir, '/');
592 if (tmp)
593 *tmp = '\0';
594 else {
595 free(interp_dir);
596 interp_dir = interp_name;
598 tmp1 = tmp = s;
599 while (*tmp) {
600 if (*tmp == '/')
601 tmp1 = tmp + 1;
602 tmp++;
604 for (cur = lib_list; cur; cur = cur->next) {
605 /* Check if this library is already in the list */
606 if (!tmp1 || !cur->name)
607 return NULL;
608 if (strcmp(cur->name, tmp1) == 0) {
609 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
610 newlib = cur;
611 free(newlib->name);
612 if (newlib->path != not_found) {
613 free(newlib->path);
615 newlib->name = NULL;
616 newlib->path = NULL;
617 break;
620 if (newlib == NULL)
621 newlib = malloc(sizeof(struct library));
622 if (!newlib)
623 return NULL;
624 newlib->name = malloc(strlen(s) + 1);
625 strcpy(newlib->name, s);
626 newlib->path = strdup(newlib->name);
627 newlib->resolved = 1;
628 newlib->next = NULL;
630 #if 0
631 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
632 if (!lib_list) {
633 lib_list = newlib;
634 } else {
635 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
636 cur->next = newlib;
638 #endif
639 interpreter_already_found = 1;
640 return newlib;
642 return NULL;
644 #endif /* __LDSO_LDD_SUPPORT__ */
646 /* map the .so, and locate interesting pieces */
648 #warning "There may be two warnings here about vfork() clobbering, ignore them"
650 static int find_dependencies(char *filename)
652 int is_suid = 0;
653 FILE *thefile;
654 struct stat statbuf;
655 ElfW(Ehdr) *ehdr = NULL;
656 ElfW(Shdr) *dynsec = NULL;
657 ElfW(Dyn) *dynamic = NULL;
658 #ifdef __LDSO_LDD_SUPPORT__
659 struct library *interp;
660 #endif
662 if (filename == not_found)
663 return 0;
665 if (!filename) {
666 fprintf(stderr, "No filename specified.\n");
667 return -1;
669 if (!(thefile = fopen(filename, "r"))) {
670 perror(filename);
671 return -1;
673 if (fstat(fileno(thefile), &statbuf) < 0) {
674 perror(filename);
675 fclose(thefile);
676 return -1;
679 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
680 goto foo;
682 if (!S_ISREG(statbuf.st_mode))
683 goto foo;
685 /* mmap the file to make reading stuff from it effortless */
686 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
687 if (ehdr == MAP_FAILED) {
688 fclose(thefile);
689 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
690 return -1;
693 foo:
694 fclose(thefile);
696 /* Check if this looks like a legit ELF file */
697 if (check_elf_header(ehdr)) {
698 fprintf(stderr, "%s: not an ELF file.\n", filename);
699 return -1;
701 /* Check if this is the right kind of ELF file */
702 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
703 fprintf(stderr, "%s: not a dynamic executable\n", filename);
704 return -1;
706 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
707 if (statbuf.st_mode & S_ISUID)
708 is_suid = 1;
709 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
710 is_suid = 1;
711 /* FIXME */
712 if (is_suid)
713 fprintf(stderr, "%s: is setuid\n", filename);
716 interpreter_already_found = 0;
717 #ifdef __LDSO_LDD_SUPPORT__
718 interp = find_elf_interpreter(ehdr);
720 if (interp
721 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
722 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
723 && ehdr->e_ident[EI_DATA] == ELFDATAM
724 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
725 && MATCH_MACHINE(ehdr->e_machine))
727 struct stat st;
728 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
729 pid_t pid;
730 int status;
731 static const char *const environment[] = {
732 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
733 "SHELL=/bin/sh",
734 "LD_TRACE_LOADED_OBJECTS=1",
735 NULL
737 # ifdef __LDSO_STANDALONE_SUPPORT__
738 char * lib_path = getenv("LD_LIBRARY_PATH");
740 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
741 static char *ext_environment[ARRAY_SIZE(environment) + 1];
742 char **envp = (char **) environment;
744 if (lib_path) {
746 * If the LD_LIBRARY_PATH is set, it needs to include it
747 * into the environment for the new process to be spawned
749 char ** eenvp = (char **) ext_environment;
751 /* Copy the N-1 environment's entries */
752 while (*envp)
753 *eenvp++=*envp++;
755 /* Make room for LD_LIBRARY_PATH */
756 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
757 + strlen(lib_path));
758 strcpy(*eenvp, "LD_LIBRARY_PATH=");
759 strcat(*eenvp, lib_path);
760 lib_path = *eenvp;
761 /* ext_environment[size] is already NULL */
763 /* Use the extended environment */
764 envp = ext_environment;
766 if ((pid = vfork()) == 0) {
768 * Force to use the standard dynamic linker in stand-alone mode.
769 * It will fails at runtime if support is not actually available
771 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
772 _exit(0xdead);
774 # else
775 if ((pid = vfork()) == 0) {
776 /* Cool, it looks like we should be able to actually
777 * run this puppy. Do so now... */
778 execle(filename, filename, NULL, environment);
779 _exit(0xdead);
781 # endif
782 /* Wait till it returns */
783 waitpid(pid, &status, 0);
785 # ifdef __LDSO_STANDALONE_SUPPORT__
786 /* Do not leak */
787 free(lib_path);
788 # endif
790 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
791 return 1;
794 /* If the exec failed, we fall through to trying to find
795 * all the needed libraries ourselves by rummaging about
796 * in the ELF headers... */
799 #endif
801 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
802 if (dynsec) {
803 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
804 find_needed_libraries(ehdr, dynamic, is_suid);
807 return 0;
810 int main(int argc, char **argv)
812 int multi = 0;
813 int got_em_all = 1;
814 char *filename = NULL;
815 struct library *cur;
817 if (argc < 2) {
818 fprintf(stderr, "ldd: missing file arguments\n"
819 "Try `ldd --help' for more information.\n");
820 exit(EXIT_FAILURE);
822 if (argc > 2)
823 multi++;
825 while (--argc > 0) {
826 ++argv;
828 if (strcmp(*argv, "--") == 0) {
829 /* Ignore "--" */
830 continue;
833 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
834 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
835 "\t--help\t\tprint this help and exit\n");
836 exit(EXIT_SUCCESS);
839 filename = *argv;
840 if (!filename) {
841 fprintf(stderr, "No filename specified.\n");
842 exit(EXIT_FAILURE);
845 if (multi) {
846 printf("%s:\n", *argv);
849 map_cache();
851 if (find_dependencies(filename) != 0)
852 continue;
854 while (got_em_all) {
855 got_em_all = 0;
856 /* Keep walking the list till everybody is resolved */
857 for (cur = lib_list; cur; cur = cur->next) {
858 if (cur->resolved == 0 && cur->path) {
859 got_em_all = 1;
860 printf("checking sub-depends for '%s'\n", cur->path);
861 find_dependencies(cur->path);
862 cur->resolved = 1;
867 unmap_cache();
869 /* Print the list */
870 got_em_all = 0;
871 for (cur = lib_list; cur; cur = cur->next) {
872 got_em_all = 1;
873 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
875 if (interp_name && interpreter_already_found == 1)
876 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
877 else
878 printf("\tnot a dynamic executable\n");
880 for (cur = lib_list; cur; cur = cur->next) {
881 free(cur->name);
882 cur->name = NULL;
883 if (cur->path && cur->path != not_found) {
884 free(cur->path);
885 cur->path = NULL;
888 lib_list = NULL;
891 return 0;