arc: Merge ARCv2 string routines in generic ARC .S files
[uclibc-ng.git] / utils / ldd.c
blob963dd1a9fee852ef20427e492d39788754d8a2c9
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(__frv__)
58 #define MATCH_MACHINE(x) (x == EM_CYGNUS_FRV)
59 #define ELFCLASSM ELFCLASS32
60 #endif
62 #if defined(__hppa__)
63 #define MATCH_MACHINE(x) (x == EM_PARISC)
64 #if defined(__LP64__)
65 #define ELFCLASSM ELFCLASS64
66 #else
67 #define ELFCLASSM ELFCLASS32
68 #endif
69 #endif
71 #if defined(__i386__)
72 #ifndef EM_486
73 #define MATCH_MACHINE(x) (x == EM_386)
74 #else
75 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
76 #endif
77 #define ELFCLASSM ELFCLASS32
78 #endif
80 #if defined(__ia64__)
81 #define MATCH_MACHINE(x) (x == EM_IA_64)
82 #define ELFCLASSM ELFCLASS64
83 #endif
85 #if defined(__mc68000__)
86 #define MATCH_MACHINE(x) (x == EM_68K)
87 #define ELFCLASSM ELFCLASS32
88 #endif
90 #if defined(__metag__)
91 #define MATCH_MACHINE(x) (x == EM_METAG)
92 #define ELFCLASSM ELFCLASS32
93 #endif
95 #if defined(__microblaze__)
96 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE)
97 #define ELFCLASSM ELFCLASS32
98 #endif
100 #if defined(__mips__)
101 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
102 #define ELFCLASSM ELFCLASS32
103 #endif
105 #if defined(__nds32__)
106 #define MATCH_MACHINE(x) (x == EM_NDS32)
107 #define ELFCLASSM ELFCLASS32
108 #endif
110 #if defined(__nios2__)
111 #define MATCH_MACHINE(x) (x == EM_NIOS32)
112 #define ELFCLASSM ELFCLASS32
113 #endif
115 #if defined(__powerpc__)
116 #define MATCH_MACHINE(x) (x == EM_PPC)
117 #define ELFCLASSM ELFCLASS32
118 #endif
120 #if defined(__sh__)
121 #define MATCH_MACHINE(x) (x == EM_SH)
122 #define ELFCLASSM ELFCLASS32
123 #endif
125 #if defined(__sparc__)
126 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
127 #define ELFCLASSM ELFCLASS32
128 #endif
130 #if defined(__x86_64__)
131 #define MATCH_MACHINE(x) (x == EM_X86_64)
132 #define ELFCLASSM ELFCLASS64
133 #endif
135 #if defined(__xtensa__)
136 #define MATCH_MACHINE(x) (x == EM_XTENSA)
137 #define ELFCLASSM ELFCLASS32
138 #endif
140 #ifndef MATCH_MACHINE
141 # ifdef __linux__
142 # include <asm/elf.h>
143 # endif
144 # ifdef ELF_ARCH
145 # define MATCH_MACHINE(x) (x == ELF_ARCH)
146 # endif
147 # ifdef ELF_CLASS
148 # define ELFCLASSM ELF_CLASS
149 # endif
150 #endif
151 #ifndef MATCH_MACHINE
152 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
153 #endif
155 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
156 #define ELFDATAM ELFDATA2LSB
157 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
158 #define ELFDATAM ELFDATA2MSB
159 #endif
161 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
163 struct library {
164 char *name;
165 int resolved;
166 char *path;
167 struct library *next;
169 static struct library *lib_list = NULL;
170 static char not_found[] = "not found";
171 static char *interp_name = NULL;
172 static char *interp_dir = NULL;
173 static int byteswap;
174 static int interpreter_already_found = 0;
176 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
178 if (byteswap) {
179 return (bswap_32(value));
180 } else {
181 return (value);
184 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
186 if (byteswap) {
187 return (bswap_64(value));
188 } else {
189 return (value);
193 #if __WORDSIZE == 64
194 # define byteswap_to_host(x) byteswap64_to_host(x)
195 #else
196 # define byteswap_to_host(x) byteswap32_to_host(x)
197 #endif
199 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
201 int j;
202 ElfW(Shdr) *shdr;
203 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
204 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
205 if (key == byteswap32_to_host(shdr->sh_type)) {
206 return shdr;
209 return NULL;
212 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
214 int j;
215 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
216 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
217 if (type == byteswap32_to_host(phdr->p_type)) {
218 return phdr;
221 return NULL;
224 /* Returns value if return_val==1, ptr otherwise */
225 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
226 ElfW(Ehdr) *ehdr, int return_val)
228 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
229 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
230 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
231 if (key == byteswap_to_host(dynp->d_tag)) {
232 if (return_val == 1)
233 return (void *)byteswap_to_host(dynp->d_un.d_val);
234 else
235 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
238 return NULL;
241 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
243 ElfW(Dyn) *dyns;
245 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
246 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
247 char *strtab;
248 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
249 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
252 return NULL;
255 static int check_elf_header(ElfW(Ehdr) *const ehdr)
257 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
258 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
259 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
260 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
262 return 1;
265 /* Check if the target endianness matches the host's endianness */
266 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
268 /* Be very lazy, and only byteswap the stuff we use */
269 if (byteswap) {
270 ehdr->e_type = bswap_16(ehdr->e_type);
271 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
272 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
273 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
274 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
277 return 0;
280 #ifdef __LDSO_CACHE_SUPPORT__
281 static caddr_t cache_addr = NULL;
282 static size_t cache_size = 0;
284 static int map_cache(void)
286 int fd;
287 struct stat st;
288 header_t *header;
289 libentry_t *libent;
290 int i, strtabsize;
292 if (cache_addr == (caddr_t) - 1)
293 return -1;
294 else if (cache_addr != NULL)
295 return 0;
297 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
298 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
299 cache_addr = (caddr_t) - 1; /* so we won't try again */
300 return -1;
303 cache_size = st.st_size;
304 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
305 close(fd);
306 if (cache_addr == MAP_FAILED) {
307 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
308 return -1;
311 header = (header_t *) cache_addr;
313 if (cache_size < sizeof(header_t)
314 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
315 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
316 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
317 || cache_addr[cache_size - 1] != '\0')
319 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
320 goto fail;
323 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
324 libent = (libentry_t *) & header[1];
326 for (i = 0; i < header->nlibs; i++) {
327 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
328 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
329 goto fail;
333 return 0;
335 fail:
336 munmap(cache_addr, cache_size);
337 cache_addr = (caddr_t) - 1;
338 return -1;
341 static int unmap_cache(void)
343 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
344 return -1;
346 #if 1
347 munmap(cache_addr, cache_size);
348 cache_addr = NULL;
349 #endif
351 return 0;
353 #else
354 static __inline__ void map_cache(void)
357 static __inline__ void unmap_cache(void)
360 #endif
362 /* This function's behavior must exactly match that
363 * in uClibc/ldso/ldso/dl-elf.c */
364 static void search_for_named_library(char *name, char *result,
365 const char *path_list)
367 int i, count = 1;
368 char *path, *path_n;
369 struct stat filestat;
371 /* We need a writable copy of this string */
372 path = strdup(path_list);
373 if (!path) {
374 fprintf(stderr, "%s: Out of memory!\n", __func__);
375 exit(EXIT_FAILURE);
377 /* Eliminate all double //s */
378 path_n = path;
379 while ((path_n = strstr(path_n, "//"))) {
380 i = strlen(path_n);
381 memmove(path_n, path_n + 1, i - 1);
382 *(path_n + i - 1) = '\0';
385 /* Replace colons with zeros in path_list and count them */
386 for (i = strlen(path); i > 0; i--) {
387 if (path[i] == ':') {
388 path[i] = 0;
389 count++;
392 path_n = path;
393 for (i = 0; i < count; i++) {
394 strcpy(result, path_n);
395 strcat(result, "/");
396 strcat(result, name);
397 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
398 free(path);
399 return;
401 path_n += (strlen(path_n) + 1);
403 free(path);
404 *result = '\0';
407 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
408 int is_suid, struct library *lib)
410 char *buf;
411 char *path;
412 struct stat filestat;
414 /* If this is a fully resolved name, our job is easy */
415 if (stat(lib->name, &filestat) == 0) {
416 lib->path = strdup(lib->name);
417 return;
420 /* We need some elbow room here. Make some room... */
421 buf = malloc(1024);
422 if (!buf) {
423 fprintf(stderr, "%s: Out of memory!\n", __func__);
424 exit(EXIT_FAILURE);
427 /* This function must match the behavior of _dl_load_shared_library
428 * in readelflib1.c or things won't work out as expected... */
430 /* The ABI specifies that RPATH is searched first, so do that now. */
431 path = elf_find_rpath(ehdr, dynamic);
432 if (path) {
433 search_for_named_library(lib->name, buf, path);
434 if (*buf != '\0') {
435 lib->path = buf;
436 return;
440 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
441 * Since this app doesn't actually run an executable I will skip
442 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
443 if (is_suid == 1)
444 path = NULL;
445 else
446 path = getenv("LD_LIBRARY_PATH");
447 if (path) {
448 search_for_named_library(lib->name, buf, path);
449 if (*buf != '\0') {
450 lib->path = buf;
451 return;
454 #ifdef __LDSO_CACHE_SUPPORT__
455 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
456 int i;
457 header_t *header = (header_t *) cache_addr;
458 libentry_t *libent = (libentry_t *) & header[1];
459 char *strs = (char *)&libent[header->nlibs];
461 for (i = 0; i < header->nlibs; i++) {
462 if ((libent[i].flags == LIB_ELF ||
463 libent[i].flags == LIB_ELF_LIBC0 ||
464 libent[i].flags == LIB_ELF_LIBC5) &&
465 strcmp(lib->name, strs + libent[i].sooffset) == 0)
467 lib->path = strdup(strs + libent[i].liboffset);
468 return;
472 #endif
474 /* Next look for libraries wherever the shared library
475 * loader was installed -- this is usually where we
476 * should find things... */
477 if (interp_dir) {
478 search_for_named_library(lib->name, buf, interp_dir);
479 if (*buf != '\0') {
480 lib->path = buf;
481 return;
485 /* Lastly, search the standard list of paths for the library.
486 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
487 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
488 #ifndef __LDSO_CACHE_SUPPORT__
489 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
490 #endif
492 search_for_named_library(lib->name, buf, path);
493 if (*buf != '\0') {
494 lib->path = buf;
495 } else {
496 free(buf);
497 lib->path = not_found;
501 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
503 char *tmp, *tmp1, *tmp2;
504 struct library *cur, *newlib = lib_list;
506 if (!s || !strlen(s))
507 return 1;
509 tmp = s;
510 while (*tmp) {
511 if (*tmp == '/')
512 s = tmp + 1;
513 tmp++;
516 /* We add ldso elsewhere */
517 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
518 int len = strlen(interp_dir);
519 if (strcmp(s, interp_name + 1 + len) == 0)
520 return 1;
523 for (cur = lib_list; cur; cur = cur->next) {
524 /* Check if this library is already in the list */
525 tmp1 = tmp2 = cur->name;
526 if (!cur->name)
527 continue;
528 while (*tmp1) {
529 if (*tmp1 == '/')
530 tmp2 = tmp1 + 1;
531 tmp1++;
533 if (strcmp(tmp2, s) == 0) {
534 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
535 return 0;
539 /* Ok, this lib needs to be added to the list */
540 newlib = malloc(sizeof(struct library));
541 if (!newlib)
542 return 1;
543 newlib->name = malloc(strlen(s) + 1);
544 strcpy(newlib->name, s);
545 newlib->resolved = 0;
546 newlib->path = NULL;
547 newlib->next = NULL;
549 /* Now try and locate where this library might be living... */
550 locate_library_file(ehdr, dynamic, is_setuid, newlib);
552 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
553 if (!lib_list) {
554 lib_list = newlib;
555 } else {
556 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
557 cur->next = newlib;
559 return 0;
562 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
564 ElfW(Dyn) *dyns;
566 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
567 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
568 char *strtab;
569 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
570 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
575 #ifdef __LDSO_LDD_SUPPORT__
576 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
578 ElfW(Phdr) *phdr;
580 if (interpreter_already_found == 1)
581 return NULL;
582 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
583 if (phdr) {
584 struct library *cur, *newlib = NULL;
585 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
587 char *tmp, *tmp1;
588 interp_name = strdup(s);
589 interp_dir = strdup(s);
590 tmp = strrchr(interp_dir, '/');
591 if (tmp)
592 *tmp = '\0';
593 else {
594 free(interp_dir);
595 interp_dir = interp_name;
597 tmp1 = tmp = s;
598 while (*tmp) {
599 if (*tmp == '/')
600 tmp1 = tmp + 1;
601 tmp++;
603 for (cur = lib_list; cur; cur = cur->next) {
604 /* Check if this library is already in the list */
605 if (!tmp1 || !cur->name)
606 return NULL;
607 if (strcmp(cur->name, tmp1) == 0) {
608 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
609 newlib = cur;
610 free(newlib->name);
611 if (newlib->path != not_found) {
612 free(newlib->path);
614 newlib->name = NULL;
615 newlib->path = NULL;
616 break;
619 if (newlib == NULL)
620 newlib = malloc(sizeof(struct library));
621 if (!newlib)
622 return NULL;
623 newlib->name = malloc(strlen(s) + 1);
624 strcpy(newlib->name, s);
625 newlib->path = strdup(newlib->name);
626 newlib->resolved = 1;
627 newlib->next = NULL;
629 #if 0
630 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
631 if (!lib_list) {
632 lib_list = newlib;
633 } else {
634 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
635 cur->next = newlib;
637 #endif
638 interpreter_already_found = 1;
639 return newlib;
641 return NULL;
643 #endif /* __LDSO_LDD_SUPPORT__ */
645 /* map the .so, and locate interesting pieces */
647 #warning "There may be two warnings here about vfork() clobbering, ignore them"
649 static int find_dependencies(char *filename)
651 int is_suid = 0;
652 FILE *thefile;
653 struct stat statbuf;
654 ElfW(Ehdr) *ehdr = NULL;
655 ElfW(Shdr) *dynsec = NULL;
656 ElfW(Dyn) *dynamic = NULL;
657 #ifdef __LDSO_LDD_SUPPORT__
658 struct library *interp;
659 #endif
661 if (filename == not_found)
662 return 0;
664 if (!filename) {
665 fprintf(stderr, "No filename specified.\n");
666 return -1;
668 if (!(thefile = fopen(filename, "r"))) {
669 perror(filename);
670 return -1;
672 if (fstat(fileno(thefile), &statbuf) < 0) {
673 perror(filename);
674 fclose(thefile);
675 return -1;
678 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
679 goto foo;
681 if (!S_ISREG(statbuf.st_mode))
682 goto foo;
684 /* mmap the file to make reading stuff from it effortless */
685 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
686 if (ehdr == MAP_FAILED) {
687 fclose(thefile);
688 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
689 return -1;
692 foo:
693 fclose(thefile);
695 /* Check if this looks like a legit ELF file */
696 if (check_elf_header(ehdr)) {
697 fprintf(stderr, "%s: not an ELF file.\n", filename);
698 return -1;
700 /* Check if this is the right kind of ELF file */
701 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
702 fprintf(stderr, "%s: not a dynamic executable\n", filename);
703 return -1;
705 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
706 if (statbuf.st_mode & S_ISUID)
707 is_suid = 1;
708 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
709 is_suid = 1;
710 /* FIXME */
711 if (is_suid)
712 fprintf(stderr, "%s: is setuid\n", filename);
715 interpreter_already_found = 0;
716 #ifdef __LDSO_LDD_SUPPORT__
717 interp = find_elf_interpreter(ehdr);
719 if (interp
720 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
721 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
722 && ehdr->e_ident[EI_DATA] == ELFDATAM
723 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
724 && MATCH_MACHINE(ehdr->e_machine))
726 struct stat st;
727 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
728 pid_t pid;
729 int status;
730 static const char *const environment[] = {
731 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
732 "SHELL=/bin/sh",
733 "LD_TRACE_LOADED_OBJECTS=1",
734 NULL
736 # ifdef __LDSO_STANDALONE_SUPPORT__
737 char * lib_path = getenv("LD_LIBRARY_PATH");
739 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
740 static char *ext_environment[ARRAY_SIZE(environment) + 1];
741 char **envp = (char **) environment;
743 if (lib_path) {
745 * If the LD_LIBRARY_PATH is set, it needs to include it
746 * into the environment for the new process to be spawned
748 char ** eenvp = (char **) ext_environment;
750 /* Copy the N-1 environment's entries */
751 while (*envp)
752 *eenvp++=*envp++;
754 /* Make room for LD_LIBRARY_PATH */
755 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
756 + strlen(lib_path));
757 strcpy(*eenvp, "LD_LIBRARY_PATH=");
758 strcat(*eenvp, lib_path);
759 lib_path = *eenvp;
760 /* ext_environment[size] is already NULL */
762 /* Use the extended environment */
763 envp = ext_environment;
765 if ((pid = vfork()) == 0) {
767 * Force to use the standard dynamic linker in stand-alone mode.
768 * It will fails at runtime if support is not actually available
770 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
771 _exit(0xdead);
773 # else
774 if ((pid = vfork()) == 0) {
775 /* Cool, it looks like we should be able to actually
776 * run this puppy. Do so now... */
777 execle(filename, filename, NULL, environment);
778 _exit(0xdead);
780 # endif
781 /* Wait till it returns */
782 waitpid(pid, &status, 0);
784 # ifdef __LDSO_STANDALONE_SUPPORT__
785 /* Do not leak */
786 free(lib_path);
787 # endif
789 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
790 return 1;
793 /* If the exec failed, we fall through to trying to find
794 * all the needed libraries ourselves by rummaging about
795 * in the ELF headers... */
798 #endif
800 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
801 if (dynsec) {
802 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
803 find_needed_libraries(ehdr, dynamic, is_suid);
806 return 0;
809 int main(int argc, char **argv)
811 int multi = 0;
812 int got_em_all = 1;
813 char *filename = NULL;
814 struct library *cur;
816 if (argc < 2) {
817 fprintf(stderr, "ldd: missing file arguments\n"
818 "Try `ldd --help' for more information.\n");
819 exit(EXIT_FAILURE);
821 if (argc > 2)
822 multi++;
824 while (--argc > 0) {
825 ++argv;
827 if (strcmp(*argv, "--") == 0) {
828 /* Ignore "--" */
829 continue;
832 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
833 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
834 "\t--help\t\tprint this help and exit\n");
835 exit(EXIT_SUCCESS);
838 filename = *argv;
839 if (!filename) {
840 fprintf(stderr, "No filename specified.\n");
841 exit(EXIT_FAILURE);
844 if (multi) {
845 printf("%s:\n", *argv);
848 map_cache();
850 if (find_dependencies(filename) != 0)
851 continue;
853 while (got_em_all) {
854 got_em_all = 0;
855 /* Keep walking the list till everybody is resolved */
856 for (cur = lib_list; cur; cur = cur->next) {
857 if (cur->resolved == 0 && cur->path) {
858 got_em_all = 1;
859 printf("checking sub-depends for '%s'\n", cur->path);
860 find_dependencies(cur->path);
861 cur->resolved = 1;
866 unmap_cache();
868 /* Print the list */
869 got_em_all = 0;
870 for (cur = lib_list; cur; cur = cur->next) {
871 got_em_all = 1;
872 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
874 if (interp_name && interpreter_already_found == 1)
875 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
876 else
877 printf("\tnot a dynamic executable\n");
879 for (cur = lib_list; cur; cur = cur->next) {
880 free(cur->name);
881 cur->name = NULL;
882 if (cur->path && cur->path != not_found) {
883 free(cur->path);
884 cur->path = NULL;
887 lib_list = NULL;
890 return 0;