microblaze: sync sysdep-cancel.h/sydep.h with GNU libc
[uclibc-ng.git] / utils / ldd.c
blobe7430fca511582c871b8909831d63432573e5bcc
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(__alpha__)
19 #define MATCH_MACHINE(x) (x == EM_ALPHA)
20 #define ELFCLASSM ELFCLASS64
21 #endif
23 #if defined(__arc__)
24 #define MATCH_MACHINE(x) (x == EM_ARCOMPACT)
25 #define ELFCLASSM ELFCLASS32
26 #endif
28 #if defined(__arm__) || defined(__thumb__)
29 #define MATCH_MACHINE(x) (x == EM_ARM)
30 #define ELFCLASSM ELFCLASS32
31 #endif
33 #if defined(__avr32__)
34 #define MATCH_MACHINE(x) (x == EM_AVR32)
35 #define ELFCLASSM ELFCLASS32
36 #endif
38 #if defined(__bfin__)
39 #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
40 #define ELFCLASSM ELFCLASS32
41 #endif
43 #if defined(__TMS320C6X__)
44 #define MATCH_MACHINE(x) (x == EM_TI_C6000)
45 #define ELFCLASSM ELFCLASS32
46 #endif
48 #if defined(__cris__)
49 #define MATCH_MACHINE(x) (x == EM_CRIS)
50 #define ELFCLASSM ELFCLASS32
51 #endif
53 #if defined(__frv__)
54 #define MATCH_MACHINE(x) (x == EM_CYGNUS_FRV)
55 #define ELFCLASSM ELFCLASS32
56 #endif
58 #if defined(__hppa__)
59 #define MATCH_MACHINE(x) (x == EM_PARISC)
60 #if defined(__LP64__)
61 #define ELFCLASSM ELFCLASS64
62 #else
63 #define ELFCLASSM ELFCLASS32
64 #endif
65 #endif
67 #if defined(__i386__)
68 #ifndef EM_486
69 #define MATCH_MACHINE(x) (x == EM_386)
70 #else
71 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
72 #endif
73 #define ELFCLASSM ELFCLASS32
74 #endif
76 #if defined(__ia64__)
77 #define MATCH_MACHINE(x) (x == EM_IA_64)
78 #define ELFCLASSM ELFCLASS64
79 #endif
81 #if defined(__mc68000__)
82 #define MATCH_MACHINE(x) (x == EM_68K)
83 #define ELFCLASSM ELFCLASS32
84 #endif
86 #if defined(__metag__)
87 #define MATCH_MACHINE(x) (x == EM_METAG)
88 #define ELFCLASSM ELFCLASS32
89 #endif
91 #if defined(__microblaze__)
92 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE)
93 #define ELFCLASSM ELFCLASS32
94 #endif
96 #if defined(__mips__)
97 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
98 #define ELFCLASSM ELFCLASS32
99 #endif
101 #if defined(__nds32__)
102 #define MATCH_MACHINE(x) (x == EM_NDS32)
103 #define ELFCLASSM ELFCLASS32
104 #endif
106 #if defined(__nios2__)
107 #define MATCH_MACHINE(x) (x == EM_NIOS32)
108 #define ELFCLASSM ELFCLASS32
109 #endif
111 #if defined(__powerpc__)
112 #define MATCH_MACHINE(x) (x == EM_PPC)
113 #define ELFCLASSM ELFCLASS32
114 #endif
116 #if defined(__sh__)
117 #define MATCH_MACHINE(x) (x == EM_SH)
118 #define ELFCLASSM ELFCLASS32
119 #endif
121 #if defined(__sparc__)
122 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
123 #define ELFCLASSM ELFCLASS32
124 #endif
126 #if defined(__x86_64__)
127 #define MATCH_MACHINE(x) (x == EM_X86_64)
128 #define ELFCLASSM ELFCLASS64
129 #endif
131 #if defined(__xtensa__)
132 #define MATCH_MACHINE(x) (x == EM_XTENSA)
133 #define ELFCLASSM ELFCLASS32
134 #endif
136 #ifndef MATCH_MACHINE
137 # ifdef __linux__
138 # include <asm/elf.h>
139 # endif
140 # ifdef ELF_ARCH
141 # define MATCH_MACHINE(x) (x == ELF_ARCH)
142 # endif
143 # ifdef ELF_CLASS
144 # define ELFCLASSM ELF_CLASS
145 # endif
146 #endif
147 #ifndef MATCH_MACHINE
148 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
149 #endif
151 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
152 #define ELFDATAM ELFDATA2LSB
153 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
154 #define ELFDATAM ELFDATA2MSB
155 #endif
157 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
159 struct library {
160 char *name;
161 int resolved;
162 char *path;
163 struct library *next;
165 static struct library *lib_list = NULL;
166 static char not_found[] = "not found";
167 static char *interp_name = NULL;
168 static char *interp_dir = NULL;
169 static int byteswap;
170 static int interpreter_already_found = 0;
172 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
174 if (byteswap) {
175 return (bswap_32(value));
176 } else {
177 return (value);
180 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
182 if (byteswap) {
183 return (bswap_64(value));
184 } else {
185 return (value);
189 #if __WORDSIZE == 64
190 # define byteswap_to_host(x) byteswap64_to_host(x)
191 #else
192 # define byteswap_to_host(x) byteswap32_to_host(x)
193 #endif
195 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
197 int j;
198 ElfW(Shdr) *shdr;
199 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
200 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
201 if (key == byteswap32_to_host(shdr->sh_type)) {
202 return shdr;
205 return NULL;
208 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
210 int j;
211 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
212 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
213 if (type == byteswap32_to_host(phdr->p_type)) {
214 return phdr;
217 return NULL;
220 /* Returns value if return_val==1, ptr otherwise */
221 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
222 ElfW(Ehdr) *ehdr, int return_val)
224 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
225 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
226 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
227 if (key == byteswap_to_host(dynp->d_tag)) {
228 if (return_val == 1)
229 return (void *)byteswap_to_host(dynp->d_un.d_val);
230 else
231 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
234 return NULL;
237 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
239 ElfW(Dyn) *dyns;
241 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
242 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
243 char *strtab;
244 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
245 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
248 return NULL;
251 static int check_elf_header(ElfW(Ehdr) *const ehdr)
253 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
254 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
255 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
256 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
258 return 1;
261 /* Check if the target endianness matches the host's endianness */
262 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
264 /* Be very lazy, and only byteswap the stuff we use */
265 if (byteswap) {
266 ehdr->e_type = bswap_16(ehdr->e_type);
267 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
268 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
269 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
270 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
273 return 0;
276 #ifdef __LDSO_CACHE_SUPPORT__
277 static caddr_t cache_addr = NULL;
278 static size_t cache_size = 0;
280 static int map_cache(void)
282 int fd;
283 struct stat st;
284 header_t *header;
285 libentry_t *libent;
286 int i, strtabsize;
288 if (cache_addr == (caddr_t) - 1)
289 return -1;
290 else if (cache_addr != NULL)
291 return 0;
293 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
294 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
295 cache_addr = (caddr_t) - 1; /* so we won't try again */
296 return -1;
299 cache_size = st.st_size;
300 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
301 close(fd);
302 if (cache_addr == MAP_FAILED) {
303 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
304 return -1;
307 header = (header_t *) cache_addr;
309 if (cache_size < sizeof(header_t)
310 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
311 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
312 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
313 || cache_addr[cache_size - 1] != '\0')
315 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
316 goto fail;
319 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
320 libent = (libentry_t *) & header[1];
322 for (i = 0; i < header->nlibs; i++) {
323 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
324 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
325 goto fail;
329 return 0;
331 fail:
332 munmap(cache_addr, cache_size);
333 cache_addr = (caddr_t) - 1;
334 return -1;
337 static int unmap_cache(void)
339 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
340 return -1;
342 #if 1
343 munmap(cache_addr, cache_size);
344 cache_addr = NULL;
345 #endif
347 return 0;
349 #else
350 static __inline__ void map_cache(void)
353 static __inline__ void unmap_cache(void)
356 #endif
358 /* This function's behavior must exactly match that
359 * in uClibc/ldso/ldso/dl-elf.c */
360 static void search_for_named_library(char *name, char *result,
361 const char *path_list)
363 int i, count = 1;
364 char *path, *path_n;
365 struct stat filestat;
367 /* We need a writable copy of this string */
368 path = strdup(path_list);
369 if (!path) {
370 fprintf(stderr, "%s: Out of memory!\n", __func__);
371 exit(EXIT_FAILURE);
373 /* Eliminate all double //s */
374 path_n = path;
375 while ((path_n = strstr(path_n, "//"))) {
376 i = strlen(path_n);
377 memmove(path_n, path_n + 1, i - 1);
378 *(path_n + i - 1) = '\0';
381 /* Replace colons with zeros in path_list and count them */
382 for (i = strlen(path); i > 0; i--) {
383 if (path[i] == ':') {
384 path[i] = 0;
385 count++;
388 path_n = path;
389 for (i = 0; i < count; i++) {
390 strcpy(result, path_n);
391 strcat(result, "/");
392 strcat(result, name);
393 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
394 free(path);
395 return;
397 path_n += (strlen(path_n) + 1);
399 free(path);
400 *result = '\0';
403 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
404 int is_suid, struct library *lib)
406 char *buf;
407 char *path;
408 struct stat filestat;
410 /* If this is a fully resolved name, our job is easy */
411 if (stat(lib->name, &filestat) == 0) {
412 lib->path = strdup(lib->name);
413 return;
416 /* We need some elbow room here. Make some room... */
417 buf = malloc(1024);
418 if (!buf) {
419 fprintf(stderr, "%s: Out of memory!\n", __func__);
420 exit(EXIT_FAILURE);
423 /* This function must match the behavior of _dl_load_shared_library
424 * in readelflib1.c or things won't work out as expected... */
426 /* The ABI specifies that RPATH is searched first, so do that now. */
427 path = elf_find_rpath(ehdr, dynamic);
428 if (path) {
429 search_for_named_library(lib->name, buf, path);
430 if (*buf != '\0') {
431 lib->path = buf;
432 return;
436 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
437 * Since this app doesn't actually run an executable I will skip
438 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
439 if (is_suid == 1)
440 path = NULL;
441 else
442 path = getenv("LD_LIBRARY_PATH");
443 if (path) {
444 search_for_named_library(lib->name, buf, path);
445 if (*buf != '\0') {
446 lib->path = buf;
447 return;
450 #ifdef __LDSO_CACHE_SUPPORT__
451 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
452 int i;
453 header_t *header = (header_t *) cache_addr;
454 libentry_t *libent = (libentry_t *) & header[1];
455 char *strs = (char *)&libent[header->nlibs];
457 for (i = 0; i < header->nlibs; i++) {
458 if ((libent[i].flags == LIB_ELF ||
459 libent[i].flags == LIB_ELF_LIBC0 ||
460 libent[i].flags == LIB_ELF_LIBC5) &&
461 strcmp(lib->name, strs + libent[i].sooffset) == 0)
463 lib->path = strdup(strs + libent[i].liboffset);
464 return;
468 #endif
470 /* Next look for libraries wherever the shared library
471 * loader was installed -- this is usually where we
472 * should find things... */
473 if (interp_dir) {
474 search_for_named_library(lib->name, buf, interp_dir);
475 if (*buf != '\0') {
476 lib->path = buf;
477 return;
481 /* Lastly, search the standard list of paths for the library.
482 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
483 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
484 #ifndef __LDSO_CACHE_SUPPORT__
485 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
486 #endif
488 search_for_named_library(lib->name, buf, path);
489 if (*buf != '\0') {
490 lib->path = buf;
491 } else {
492 free(buf);
493 lib->path = not_found;
497 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
499 char *tmp, *tmp1, *tmp2;
500 struct library *cur, *newlib = lib_list;
502 if (!s || !strlen(s))
503 return 1;
505 tmp = s;
506 while (*tmp) {
507 if (*tmp == '/')
508 s = tmp + 1;
509 tmp++;
512 /* We add ldso elsewhere */
513 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
514 int len = strlen(interp_dir);
515 if (strcmp(s, interp_name + 1 + len) == 0)
516 return 1;
519 for (cur = lib_list; cur; cur = cur->next) {
520 /* Check if this library is already in the list */
521 tmp1 = tmp2 = cur->name;
522 if (!cur->name)
523 continue;
524 while (*tmp1) {
525 if (*tmp1 == '/')
526 tmp2 = tmp1 + 1;
527 tmp1++;
529 if (strcmp(tmp2, s) == 0) {
530 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
531 return 0;
535 /* Ok, this lib needs to be added to the list */
536 newlib = malloc(sizeof(struct library));
537 if (!newlib)
538 return 1;
539 newlib->name = malloc(strlen(s) + 1);
540 strcpy(newlib->name, s);
541 newlib->resolved = 0;
542 newlib->path = NULL;
543 newlib->next = NULL;
545 /* Now try and locate where this library might be living... */
546 locate_library_file(ehdr, dynamic, is_setuid, newlib);
548 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
549 if (!lib_list) {
550 lib_list = newlib;
551 } else {
552 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
553 cur->next = newlib;
555 return 0;
558 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
560 ElfW(Dyn) *dyns;
562 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
563 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
564 char *strtab;
565 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
566 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
571 #ifdef __LDSO_LDD_SUPPORT__
572 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
574 ElfW(Phdr) *phdr;
576 if (interpreter_already_found == 1)
577 return NULL;
578 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
579 if (phdr) {
580 struct library *cur, *newlib = NULL;
581 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
583 char *tmp, *tmp1;
584 interp_name = strdup(s);
585 interp_dir = strdup(s);
586 tmp = strrchr(interp_dir, '/');
587 if (tmp)
588 *tmp = '\0';
589 else {
590 free(interp_dir);
591 interp_dir = interp_name;
593 tmp1 = tmp = s;
594 while (*tmp) {
595 if (*tmp == '/')
596 tmp1 = tmp + 1;
597 tmp++;
599 for (cur = lib_list; cur; cur = cur->next) {
600 /* Check if this library is already in the list */
601 if (!tmp1 || !cur->name)
602 return NULL;
603 if (strcmp(cur->name, tmp1) == 0) {
604 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
605 newlib = cur;
606 free(newlib->name);
607 if (newlib->path != not_found) {
608 free(newlib->path);
610 newlib->name = NULL;
611 newlib->path = NULL;
612 break;
615 if (newlib == NULL)
616 newlib = malloc(sizeof(struct library));
617 if (!newlib)
618 return NULL;
619 newlib->name = malloc(strlen(s) + 1);
620 strcpy(newlib->name, s);
621 newlib->path = strdup(newlib->name);
622 newlib->resolved = 1;
623 newlib->next = NULL;
625 #if 0
626 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
627 if (!lib_list) {
628 lib_list = newlib;
629 } else {
630 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
631 cur->next = newlib;
633 #endif
634 interpreter_already_found = 1;
635 return newlib;
637 return NULL;
639 #endif /* __LDSO_LDD_SUPPORT__ */
641 /* map the .so, and locate interesting pieces */
643 #warning "There may be two warnings here about vfork() clobbering, ignore them"
645 static int find_dependencies(char *filename)
647 int is_suid = 0;
648 FILE *thefile;
649 struct stat statbuf;
650 ElfW(Ehdr) *ehdr = NULL;
651 ElfW(Shdr) *dynsec = NULL;
652 ElfW(Dyn) *dynamic = NULL;
653 #ifdef __LDSO_LDD_SUPPORT__
654 struct library *interp;
655 #endif
657 if (filename == not_found)
658 return 0;
660 if (!filename) {
661 fprintf(stderr, "No filename specified.\n");
662 return -1;
664 if (!(thefile = fopen(filename, "r"))) {
665 perror(filename);
666 return -1;
668 if (fstat(fileno(thefile), &statbuf) < 0) {
669 perror(filename);
670 fclose(thefile);
671 return -1;
674 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
675 goto foo;
677 if (!S_ISREG(statbuf.st_mode))
678 goto foo;
680 /* mmap the file to make reading stuff from it effortless */
681 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
682 if (ehdr == MAP_FAILED) {
683 fclose(thefile);
684 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
685 return -1;
688 foo:
689 fclose(thefile);
691 /* Check if this looks like a legit ELF file */
692 if (check_elf_header(ehdr)) {
693 fprintf(stderr, "%s: not an ELF file.\n", filename);
694 return -1;
696 /* Check if this is the right kind of ELF file */
697 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
698 fprintf(stderr, "%s: not a dynamic executable\n", filename);
699 return -1;
701 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
702 if (statbuf.st_mode & S_ISUID)
703 is_suid = 1;
704 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
705 is_suid = 1;
706 /* FIXME */
707 if (is_suid)
708 fprintf(stderr, "%s: is setuid\n", filename);
711 interpreter_already_found = 0;
712 #ifdef __LDSO_LDD_SUPPORT__
713 interp = find_elf_interpreter(ehdr);
715 if (interp
716 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
717 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
718 && ehdr->e_ident[EI_DATA] == ELFDATAM
719 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
720 && MATCH_MACHINE(ehdr->e_machine))
722 struct stat st;
723 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
724 pid_t pid;
725 int status;
726 static const char *const environment[] = {
727 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
728 "SHELL=/bin/sh",
729 "LD_TRACE_LOADED_OBJECTS=1",
730 NULL
732 # ifdef __LDSO_STANDALONE_SUPPORT__
733 char * lib_path = getenv("LD_LIBRARY_PATH");
735 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
736 static char *ext_environment[ARRAY_SIZE(environment) + 1];
737 char **envp = (char **) environment;
739 if (lib_path) {
741 * If the LD_LIBRARY_PATH is set, it needs to include it
742 * into the environment for the new process to be spawned
744 char ** eenvp = (char **) ext_environment;
746 /* Copy the N-1 environment's entries */
747 while (*envp)
748 *eenvp++=*envp++;
750 /* Make room for LD_LIBRARY_PATH */
751 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
752 + strlen(lib_path));
753 strcpy(*eenvp, "LD_LIBRARY_PATH=");
754 strcat(*eenvp, lib_path);
755 lib_path = *eenvp;
756 /* ext_environment[size] is already NULL */
758 /* Use the extended environment */
759 envp = ext_environment;
761 if ((pid = vfork()) == 0) {
763 * Force to use the standard dynamic linker in stand-alone mode.
764 * It will fails at runtime if support is not actually available
766 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
767 _exit(0xdead);
769 # else
770 if ((pid = vfork()) == 0) {
771 /* Cool, it looks like we should be able to actually
772 * run this puppy. Do so now... */
773 execle(filename, filename, NULL, environment);
774 _exit(0xdead);
776 # endif
777 /* Wait till it returns */
778 waitpid(pid, &status, 0);
780 # ifdef __LDSO_STANDALONE_SUPPORT__
781 /* Do not leak */
782 free(lib_path);
783 # endif
785 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
786 return 1;
789 /* If the exec failed, we fall through to trying to find
790 * all the needed libraries ourselves by rummaging about
791 * in the ELF headers... */
794 #endif
796 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
797 if (dynsec) {
798 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
799 find_needed_libraries(ehdr, dynamic, is_suid);
802 return 0;
805 int main(int argc, char **argv)
807 int multi = 0;
808 int got_em_all = 1;
809 char *filename = NULL;
810 struct library *cur;
812 if (argc < 2) {
813 fprintf(stderr, "ldd: missing file arguments\n"
814 "Try `ldd --help' for more information.\n");
815 exit(EXIT_FAILURE);
817 if (argc > 2)
818 multi++;
820 while (--argc > 0) {
821 ++argv;
823 if (strcmp(*argv, "--") == 0) {
824 /* Ignore "--" */
825 continue;
828 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
829 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
830 "\t--help\t\tprint this help and exit\n");
831 exit(EXIT_SUCCESS);
834 filename = *argv;
835 if (!filename) {
836 fprintf(stderr, "No filename specified.\n");
837 exit(EXIT_FAILURE);
840 if (multi) {
841 printf("%s:\n", *argv);
844 map_cache();
846 if (find_dependencies(filename) != 0)
847 continue;
849 while (got_em_all) {
850 got_em_all = 0;
851 /* Keep walking the list till everybody is resolved */
852 for (cur = lib_list; cur; cur = cur->next) {
853 if (cur->resolved == 0 && cur->path) {
854 got_em_all = 1;
855 printf("checking sub-depends for '%s'\n", cur->path);
856 find_dependencies(cur->path);
857 cur->resolved = 1;
862 unmap_cache();
864 /* Print the list */
865 got_em_all = 0;
866 for (cur = lib_list; cur; cur = cur->next) {
867 got_em_all = 1;
868 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
870 if (interp_name && interpreter_already_found == 1)
871 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
872 else
873 printf("\tnot a dynamic executable\n");
875 for (cur = lib_list; cur; cur = cur->next) {
876 free(cur->name);
877 cur->name = NULL;
878 if (cur->path && cur->path != not_found) {
879 free(cur->path);
880 cur->path = NULL;
883 lib_list = NULL;
886 return 0;