riscv64: add shared library support
[uclibc-ng.git] / utils / ldd.c
blob8852be39944e335aa04418fcf6bb982a983c5d1e
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(__riscv)
126 #define MATCH_MACHINE(x) (x == EM_RISCV)
127 #define ELFCLASSM ELFCLASS64
128 #endif
130 #if defined(__sh__)
131 #define MATCH_MACHINE(x) (x == EM_SH)
132 #define ELFCLASSM ELFCLASS32
133 #endif
135 #if defined(__sparc__)
136 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
137 #define ELFCLASSM ELFCLASS32
138 #endif
140 #if defined(__x86_64__)
141 #define MATCH_MACHINE(x) (x == EM_X86_64)
142 #define ELFCLASSM ELFCLASS64
143 #endif
145 #if defined(__xtensa__)
146 #define MATCH_MACHINE(x) (x == EM_XTENSA)
147 #define ELFCLASSM ELFCLASS32
148 #endif
150 #ifndef MATCH_MACHINE
151 # ifdef __linux__
152 # include <asm/elf.h>
153 # endif
154 # ifdef ELF_ARCH
155 # define MATCH_MACHINE(x) (x == ELF_ARCH)
156 # endif
157 # ifdef ELF_CLASS
158 # define ELFCLASSM ELF_CLASS
159 # endif
160 #endif
161 #ifndef MATCH_MACHINE
162 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
163 #endif
165 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
166 #define ELFDATAM ELFDATA2LSB
167 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
168 #define ELFDATAM ELFDATA2MSB
169 #endif
171 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
173 struct library {
174 char *name;
175 int resolved;
176 char *path;
177 struct library *next;
179 static struct library *lib_list = NULL;
180 static char not_found[] = "not found";
181 static char *interp_name = NULL;
182 static char *interp_dir = NULL;
183 static int byteswap;
184 static int interpreter_already_found = 0;
186 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
188 if (byteswap) {
189 return (bswap_32(value));
190 } else {
191 return (value);
194 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
196 if (byteswap) {
197 return (bswap_64(value));
198 } else {
199 return (value);
203 #if __WORDSIZE == 64
204 # define byteswap_to_host(x) byteswap64_to_host(x)
205 #else
206 # define byteswap_to_host(x) byteswap32_to_host(x)
207 #endif
209 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
211 int j;
212 ElfW(Shdr) *shdr;
213 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
214 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
215 if (key == byteswap32_to_host(shdr->sh_type)) {
216 return shdr;
219 return NULL;
222 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
224 int j;
225 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
226 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
227 if (type == byteswap32_to_host(phdr->p_type)) {
228 return phdr;
231 return NULL;
234 /* Returns value if return_val==1, ptr otherwise */
235 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
236 ElfW(Ehdr) *ehdr, int return_val)
238 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
239 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
240 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
241 if (key == byteswap_to_host(dynp->d_tag)) {
242 if (return_val == 1)
243 return (void *)byteswap_to_host(dynp->d_un.d_val);
244 else
245 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
248 return NULL;
251 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
253 ElfW(Dyn) *dyns;
255 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
256 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
257 char *strtab;
258 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
259 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
262 return NULL;
265 static int check_elf_header(ElfW(Ehdr) *const ehdr)
267 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
268 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
269 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
270 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
272 return 1;
275 /* Check if the target endianness matches the host's endianness */
276 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
278 /* Be very lazy, and only byteswap the stuff we use */
279 if (byteswap) {
280 ehdr->e_type = bswap_16(ehdr->e_type);
281 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
282 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
283 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
284 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
287 return 0;
290 #ifdef __LDSO_CACHE_SUPPORT__
291 static caddr_t cache_addr = NULL;
292 static size_t cache_size = 0;
294 static int map_cache(void)
296 int fd;
297 struct stat st;
298 header_t *header;
299 libentry_t *libent;
300 int i, strtabsize;
302 if (cache_addr == (caddr_t) - 1)
303 return -1;
304 else if (cache_addr != NULL)
305 return 0;
307 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
308 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
309 cache_addr = (caddr_t) - 1; /* so we won't try again */
310 return -1;
313 cache_size = st.st_size;
314 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
315 close(fd);
316 if (cache_addr == MAP_FAILED) {
317 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
318 return -1;
321 header = (header_t *) cache_addr;
323 if (cache_size < sizeof(header_t)
324 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
325 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
326 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
327 || cache_addr[cache_size - 1] != '\0')
329 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
330 goto fail;
333 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
334 libent = (libentry_t *) & header[1];
336 for (i = 0; i < header->nlibs; i++) {
337 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
338 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
339 goto fail;
343 return 0;
345 fail:
346 munmap(cache_addr, cache_size);
347 cache_addr = (caddr_t) - 1;
348 return -1;
351 static int unmap_cache(void)
353 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
354 return -1;
356 #if 1
357 munmap(cache_addr, cache_size);
358 cache_addr = NULL;
359 #endif
361 return 0;
363 #else
364 static __inline__ void map_cache(void)
367 static __inline__ void unmap_cache(void)
370 #endif
372 /* This function's behavior must exactly match that
373 * in uClibc/ldso/ldso/dl-elf.c */
374 static void search_for_named_library(char *name, char *result,
375 const char *path_list)
377 int i, count = 1;
378 char *path, *path_n;
379 struct stat filestat;
381 /* We need a writable copy of this string */
382 path = strdup(path_list);
383 if (!path) {
384 fprintf(stderr, "%s: Out of memory!\n", __func__);
385 exit(EXIT_FAILURE);
387 /* Eliminate all double //s */
388 path_n = path;
389 while ((path_n = strstr(path_n, "//"))) {
390 i = strlen(path_n);
391 memmove(path_n, path_n + 1, i - 1);
392 *(path_n + i - 1) = '\0';
395 /* Replace colons with zeros in path_list and count them */
396 for (i = strlen(path); i > 0; i--) {
397 if (path[i] == ':') {
398 path[i] = 0;
399 count++;
402 path_n = path;
403 for (i = 0; i < count; i++) {
404 strcpy(result, path_n);
405 strcat(result, "/");
406 strcat(result, name);
407 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
408 free(path);
409 return;
411 path_n += (strlen(path_n) + 1);
413 free(path);
414 *result = '\0';
417 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
418 int is_suid, struct library *lib)
420 char *buf;
421 char *path;
422 struct stat filestat;
424 /* If this is a fully resolved name, our job is easy */
425 if (stat(lib->name, &filestat) == 0) {
426 lib->path = strdup(lib->name);
427 return;
430 /* We need some elbow room here. Make some room... */
431 buf = malloc(1024);
432 if (!buf) {
433 fprintf(stderr, "%s: Out of memory!\n", __func__);
434 exit(EXIT_FAILURE);
437 /* This function must match the behavior of _dl_load_shared_library
438 * in readelflib1.c or things won't work out as expected... */
440 /* The ABI specifies that RPATH is searched first, so do that now. */
441 path = elf_find_rpath(ehdr, dynamic);
442 if (path) {
443 search_for_named_library(lib->name, buf, path);
444 if (*buf != '\0') {
445 lib->path = buf;
446 return;
450 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
451 * Since this app doesn't actually run an executable I will skip
452 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
453 if (is_suid == 1)
454 path = NULL;
455 else
456 path = getenv("LD_LIBRARY_PATH");
457 if (path) {
458 search_for_named_library(lib->name, buf, path);
459 if (*buf != '\0') {
460 lib->path = buf;
461 return;
464 #ifdef __LDSO_CACHE_SUPPORT__
465 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
466 int i;
467 header_t *header = (header_t *) cache_addr;
468 libentry_t *libent = (libentry_t *) & header[1];
469 char *strs = (char *)&libent[header->nlibs];
471 for (i = 0; i < header->nlibs; i++) {
472 if ((libent[i].flags == LIB_ELF ||
473 libent[i].flags == LIB_ELF_LIBC0 ||
474 libent[i].flags == LIB_ELF_LIBC5) &&
475 strcmp(lib->name, strs + libent[i].sooffset) == 0)
477 lib->path = strdup(strs + libent[i].liboffset);
478 return;
482 #endif
484 /* Next look for libraries wherever the shared library
485 * loader was installed -- this is usually where we
486 * should find things... */
487 if (interp_dir) {
488 search_for_named_library(lib->name, buf, interp_dir);
489 if (*buf != '\0') {
490 lib->path = buf;
491 return;
495 /* Lastly, search the standard list of paths for the library.
496 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
497 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
498 #ifndef __LDSO_CACHE_SUPPORT__
499 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
500 #endif
502 search_for_named_library(lib->name, buf, path);
503 if (*buf != '\0') {
504 lib->path = buf;
505 } else {
506 free(buf);
507 lib->path = not_found;
511 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
513 char *tmp, *tmp1, *tmp2;
514 struct library *cur, *newlib = lib_list;
516 if (!s || !strlen(s))
517 return 1;
519 tmp = s;
520 while (*tmp) {
521 if (*tmp == '/')
522 s = tmp + 1;
523 tmp++;
526 /* We add ldso elsewhere */
527 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
528 int len = strlen(interp_dir);
529 if (strcmp(s, interp_name + 1 + len) == 0)
530 return 1;
533 for (cur = lib_list; cur; cur = cur->next) {
534 /* Check if this library is already in the list */
535 tmp1 = tmp2 = cur->name;
536 if (!cur->name)
537 continue;
538 while (*tmp1) {
539 if (*tmp1 == '/')
540 tmp2 = tmp1 + 1;
541 tmp1++;
543 if (strcmp(tmp2, s) == 0) {
544 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
545 return 0;
549 /* Ok, this lib needs to be added to the list */
550 newlib = malloc(sizeof(struct library));
551 if (!newlib)
552 return 1;
553 newlib->name = malloc(strlen(s) + 1);
554 strcpy(newlib->name, s);
555 newlib->resolved = 0;
556 newlib->path = NULL;
557 newlib->next = NULL;
559 /* Now try and locate where this library might be living... */
560 locate_library_file(ehdr, dynamic, is_setuid, newlib);
562 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
563 if (!lib_list) {
564 lib_list = newlib;
565 } else {
566 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
567 cur->next = newlib;
569 return 0;
572 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
574 ElfW(Dyn) *dyns;
576 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
577 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
578 char *strtab;
579 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
580 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
585 #ifdef __LDSO_LDD_SUPPORT__
586 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
588 ElfW(Phdr) *phdr;
590 if (interpreter_already_found == 1)
591 return NULL;
592 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
593 if (phdr) {
594 struct library *cur, *newlib = NULL;
595 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
597 char *tmp, *tmp1;
598 interp_name = strdup(s);
599 interp_dir = strdup(s);
600 tmp = strrchr(interp_dir, '/');
601 if (tmp)
602 *tmp = '\0';
603 else {
604 free(interp_dir);
605 interp_dir = interp_name;
607 tmp1 = tmp = s;
608 while (*tmp) {
609 if (*tmp == '/')
610 tmp1 = tmp + 1;
611 tmp++;
613 for (cur = lib_list; cur; cur = cur->next) {
614 /* Check if this library is already in the list */
615 if (!tmp1 || !cur->name)
616 return NULL;
617 if (strcmp(cur->name, tmp1) == 0) {
618 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
619 newlib = cur;
620 free(newlib->name);
621 if (newlib->path != not_found) {
622 free(newlib->path);
624 newlib->name = NULL;
625 newlib->path = NULL;
626 break;
629 if (newlib == NULL)
630 newlib = malloc(sizeof(struct library));
631 if (!newlib)
632 return NULL;
633 newlib->name = malloc(strlen(s) + 1);
634 strcpy(newlib->name, s);
635 newlib->path = strdup(newlib->name);
636 newlib->resolved = 1;
637 newlib->next = NULL;
639 #if 0
640 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
641 if (!lib_list) {
642 lib_list = newlib;
643 } else {
644 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
645 cur->next = newlib;
647 #endif
648 interpreter_already_found = 1;
649 return newlib;
651 return NULL;
653 #endif /* __LDSO_LDD_SUPPORT__ */
655 /* map the .so, and locate interesting pieces */
657 #warning "There may be two warnings here about vfork() clobbering, ignore them"
659 static int find_dependencies(char *filename)
661 int is_suid = 0;
662 FILE *thefile;
663 struct stat statbuf;
664 ElfW(Ehdr) *ehdr = NULL;
665 ElfW(Shdr) *dynsec = NULL;
666 ElfW(Dyn) *dynamic = NULL;
667 #ifdef __LDSO_LDD_SUPPORT__
668 struct library *interp;
669 #endif
671 if (filename == not_found)
672 return 0;
674 if (!filename) {
675 fprintf(stderr, "No filename specified.\n");
676 return -1;
678 if (!(thefile = fopen(filename, "r"))) {
679 perror(filename);
680 return -1;
682 if (fstat(fileno(thefile), &statbuf) < 0) {
683 perror(filename);
684 fclose(thefile);
685 return -1;
688 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
689 goto foo;
691 if (!S_ISREG(statbuf.st_mode))
692 goto foo;
694 /* mmap the file to make reading stuff from it effortless */
695 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
696 if (ehdr == MAP_FAILED) {
697 fclose(thefile);
698 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
699 return -1;
702 foo:
703 fclose(thefile);
705 /* Check if this looks like a legit ELF file */
706 if (check_elf_header(ehdr)) {
707 fprintf(stderr, "%s: not an ELF file.\n", filename);
708 return -1;
710 /* Check if this is the right kind of ELF file */
711 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
712 fprintf(stderr, "%s: not a dynamic executable\n", filename);
713 return -1;
715 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
716 if (statbuf.st_mode & S_ISUID)
717 is_suid = 1;
718 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
719 is_suid = 1;
720 /* FIXME */
721 if (is_suid)
722 fprintf(stderr, "%s: is setuid\n", filename);
725 interpreter_already_found = 0;
726 #ifdef __LDSO_LDD_SUPPORT__
727 interp = find_elf_interpreter(ehdr);
729 if (interp
730 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
731 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
732 && ehdr->e_ident[EI_DATA] == ELFDATAM
733 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
734 && MATCH_MACHINE(ehdr->e_machine))
736 struct stat st;
737 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
738 pid_t pid;
739 int status;
740 static const char *const environment[] = {
741 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
742 "SHELL=/bin/sh",
743 "LD_TRACE_LOADED_OBJECTS=1",
744 NULL
746 # ifdef __LDSO_STANDALONE_SUPPORT__
747 char * lib_path = getenv("LD_LIBRARY_PATH");
749 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
750 static char *ext_environment[ARRAY_SIZE(environment) + 1];
751 char **envp = (char **) environment;
753 if (lib_path) {
755 * If the LD_LIBRARY_PATH is set, it needs to include it
756 * into the environment for the new process to be spawned
758 char ** eenvp = (char **) ext_environment;
760 /* Copy the N-1 environment's entries */
761 while (*envp)
762 *eenvp++=*envp++;
764 /* Make room for LD_LIBRARY_PATH */
765 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
766 + strlen(lib_path));
767 strcpy(*eenvp, "LD_LIBRARY_PATH=");
768 strcat(*eenvp, lib_path);
769 lib_path = *eenvp;
770 /* ext_environment[size] is already NULL */
772 /* Use the extended environment */
773 envp = ext_environment;
775 if ((pid = vfork()) == 0) {
777 * Force to use the standard dynamic linker in stand-alone mode.
778 * It will fails at runtime if support is not actually available
780 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
781 _exit(0xdead);
783 # else
784 if ((pid = vfork()) == 0) {
785 /* Cool, it looks like we should be able to actually
786 * run this puppy. Do so now... */
787 execle(filename, filename, NULL, environment);
788 _exit(0xdead);
790 # endif
791 /* Wait till it returns */
792 waitpid(pid, &status, 0);
794 # ifdef __LDSO_STANDALONE_SUPPORT__
795 /* Do not leak */
796 free(lib_path);
797 # endif
799 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
800 return 1;
803 /* If the exec failed, we fall through to trying to find
804 * all the needed libraries ourselves by rummaging about
805 * in the ELF headers... */
808 #endif
810 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
811 if (dynsec) {
812 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
813 find_needed_libraries(ehdr, dynamic, is_suid);
816 return 0;
819 int main(int argc, char **argv)
821 int multi = 0;
822 int got_em_all = 1;
823 char *filename = NULL;
824 struct library *cur;
826 if (argc < 2) {
827 fprintf(stderr, "ldd: missing file arguments\n"
828 "Try `ldd --help' for more information.\n");
829 exit(EXIT_FAILURE);
831 if (argc > 2)
832 multi++;
834 while (--argc > 0) {
835 ++argv;
837 if (strcmp(*argv, "--") == 0) {
838 /* Ignore "--" */
839 continue;
842 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
843 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
844 "\t--help\t\tprint this help and exit\n");
845 exit(EXIT_SUCCESS);
848 filename = *argv;
849 if (!filename) {
850 fprintf(stderr, "No filename specified.\n");
851 exit(EXIT_FAILURE);
854 if (multi) {
855 printf("%s:\n", *argv);
858 map_cache();
860 if (find_dependencies(filename) != 0)
861 continue;
863 while (got_em_all) {
864 got_em_all = 0;
865 /* Keep walking the list till everybody is resolved */
866 for (cur = lib_list; cur; cur = cur->next) {
867 if (cur->resolved == 0 && cur->path) {
868 got_em_all = 1;
869 printf("checking sub-depends for '%s'\n", cur->path);
870 find_dependencies(cur->path);
871 cur->resolved = 1;
876 unmap_cache();
878 /* Print the list */
879 got_em_all = 0;
880 for (cur = lib_list; cur; cur = cur->next) {
881 got_em_all = 1;
882 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
884 if (interp_name && interpreter_already_found == 1)
885 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
886 else
887 printf("\tnot a dynamic executable\n");
889 for (cur = lib_list; cur; cur = cur->next) {
890 free(cur->name);
891 cur->name = NULL;
892 if (cur->path && cur->path != not_found) {
893 free(cur->path);
894 cur->path = NULL;
897 lib_list = NULL;
900 return 0;