cleanup CALL_MCOUNT / PROF
[uclibc-ng.git] / utils / ldd.c
blob5d2c8f4c7f158e9b3e357769a91fe21cda719182
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(__s390__)
44 #define MATCH_MACHINE(x) (x == EM_S390)
45 #define ELFCLASSM ELFCLASS32
46 #endif
48 #if defined(__hppa__)
49 #define MATCH_MACHINE(x) (x == EM_PARISC)
50 #if defined(__LP64__)
51 #define ELFCLASSM ELFCLASS64
52 #else
53 #define ELFCLASSM ELFCLASS32
54 #endif
55 #endif
57 #if defined(__i386__)
58 #ifndef EM_486
59 #define MATCH_MACHINE(x) (x == EM_386)
60 #else
61 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
62 #endif
63 #define ELFCLASSM ELFCLASS32
64 #endif
66 #if defined(__ia64__)
67 #define MATCH_MACHINE(x) (x == EM_IA_64)
68 #define ELFCLASSM ELFCLASS64
69 #endif
71 #if defined(__mc68000__)
72 #define MATCH_MACHINE(x) (x == EM_68K)
73 #define ELFCLASSM ELFCLASS32
74 #endif
76 #if defined(__metag__)
77 #define MATCH_MACHINE(x) (x == EM_METAG)
78 #define ELFCLASSM ELFCLASS32
79 #endif
81 #if defined(__mips__)
82 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
83 #define ELFCLASSM ELFCLASS32
84 #endif
86 #if defined(__powerpc64__)
87 #define MATCH_MACHINE(x) (x == EM_PPC64)
88 #define ELFCLASSM ELFCLASS64
89 #elif defined(__powerpc__)
90 #define MATCH_MACHINE(x) (x == EM_PPC)
91 #define ELFCLASSM ELFCLASS32
92 #endif
94 #if defined(__sh__)
95 #define MATCH_MACHINE(x) (x == EM_SH)
96 #define ELFCLASSM ELFCLASS32
97 #endif
99 #if defined(__sparc__)
100 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
101 #define ELFCLASSM ELFCLASS32
102 #endif
104 #if defined(__cris__)
105 #define MATCH_MACHINE(x) (x == EM_CRIS)
106 #define ELFCLASSM ELFCLASS32
107 #endif
109 #if defined(__x86_64__)
110 #define MATCH_MACHINE(x) (x == EM_X86_64)
111 #define ELFCLASSM ELFCLASS64
112 #endif
114 #if defined(__microblaze__)
115 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE)
116 #define ELFCLASSM ELFCLASS32
117 #endif
119 #if defined(__xtensa__)
120 #define MATCH_MACHINE(x) (x == EM_XTENSA)
121 #define ELFCLASSM ELFCLASS32
122 #endif
124 #ifndef MATCH_MACHINE
125 # ifdef __linux__
126 # include <asm/elf.h>
127 # endif
128 # ifdef ELF_ARCH
129 # define MATCH_MACHINE(x) (x == ELF_ARCH)
130 # endif
131 # ifdef ELF_CLASS
132 # define ELFCLASSM ELF_CLASS
133 # endif
134 #endif
135 #ifndef MATCH_MACHINE
136 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
137 #endif
139 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
140 #define ELFDATAM ELFDATA2LSB
141 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
142 #define ELFDATAM ELFDATA2MSB
143 #endif
145 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
147 struct library {
148 char *name;
149 int resolved;
150 char *path;
151 struct library *next;
153 static struct library *lib_list = NULL;
154 static char not_found[] = "not found";
155 static char *interp_name = NULL;
156 static char *interp_dir = NULL;
157 static int byteswap;
158 static int interpreter_already_found = 0;
160 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
162 if (byteswap) {
163 return (bswap_32(value));
164 } else {
165 return (value);
168 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
170 if (byteswap) {
171 return (bswap_64(value));
172 } else {
173 return (value);
177 #if __WORDSIZE == 64
178 # define byteswap_to_host(x) byteswap64_to_host(x)
179 #else
180 # define byteswap_to_host(x) byteswap32_to_host(x)
181 #endif
183 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
185 int j;
186 ElfW(Shdr) *shdr;
187 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
188 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
189 if (key == byteswap32_to_host(shdr->sh_type)) {
190 return shdr;
193 return NULL;
196 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
198 int j;
199 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
200 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
201 if (type == byteswap32_to_host(phdr->p_type)) {
202 return phdr;
205 return NULL;
208 /* Returns value if return_val==1, ptr otherwise */
209 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
210 ElfW(Ehdr) *ehdr, int return_val)
212 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
213 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
214 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
215 if (key == byteswap_to_host(dynp->d_tag)) {
216 if (return_val == 1)
217 return (void *)byteswap_to_host(dynp->d_un.d_val);
218 else
219 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
222 return NULL;
225 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
227 ElfW(Dyn) *dyns;
229 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
230 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
231 char *strtab;
232 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
233 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
236 return NULL;
239 static int check_elf_header(ElfW(Ehdr) *const ehdr)
241 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
242 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
243 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
244 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
246 return 1;
249 /* Check if the target endianness matches the host's endianness */
250 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
252 /* Be very lazy, and only byteswap the stuff we use */
253 if (byteswap) {
254 ehdr->e_type = bswap_16(ehdr->e_type);
255 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
256 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
257 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
258 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
261 return 0;
264 #ifdef __LDSO_CACHE_SUPPORT__
265 static caddr_t cache_addr = NULL;
266 static size_t cache_size = 0;
268 static int map_cache(void)
270 int fd;
271 struct stat st;
272 header_t *header;
273 libentry_t *libent;
274 int i, strtabsize;
276 if (cache_addr == (caddr_t) - 1)
277 return -1;
278 else if (cache_addr != NULL)
279 return 0;
281 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
282 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
283 cache_addr = (caddr_t) - 1; /* so we won't try again */
284 return -1;
287 cache_size = st.st_size;
288 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
289 close(fd);
290 if (cache_addr == MAP_FAILED) {
291 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
292 return -1;
295 header = (header_t *) cache_addr;
297 if (cache_size < sizeof(header_t)
298 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
299 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
300 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
301 || cache_addr[cache_size - 1] != '\0')
303 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
304 goto fail;
307 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
308 libent = (libentry_t *) & header[1];
310 for (i = 0; i < header->nlibs; i++) {
311 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
312 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
313 goto fail;
317 return 0;
319 fail:
320 munmap(cache_addr, cache_size);
321 cache_addr = (caddr_t) - 1;
322 return -1;
325 static int unmap_cache(void)
327 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
328 return -1;
330 #if 1
331 munmap(cache_addr, cache_size);
332 cache_addr = NULL;
333 #endif
335 return 0;
337 #else
338 static __inline__ void map_cache(void)
341 static __inline__ void unmap_cache(void)
344 #endif
346 /* This function's behavior must exactly match that
347 * in uClibc/ldso/ldso/dl-elf.c */
348 static void search_for_named_library(char *name, char *result,
349 const char *path_list)
351 int i, count = 1;
352 char *path, *path_n;
353 struct stat filestat;
355 /* We need a writable copy of this string */
356 path = strdup(path_list);
357 if (!path) {
358 fprintf(stderr, "%s: Out of memory!\n", __func__);
359 exit(EXIT_FAILURE);
361 /* Eliminate all double //s */
362 path_n = path;
363 while ((path_n = strstr(path_n, "//"))) {
364 i = strlen(path_n);
365 memmove(path_n, path_n + 1, i - 1);
366 *(path_n + i - 1) = '\0';
369 /* Replace colons with zeros in path_list and count them */
370 for (i = strlen(path); i > 0; i--) {
371 if (path[i] == ':') {
372 path[i] = 0;
373 count++;
376 path_n = path;
377 for (i = 0; i < count; i++) {
378 strcpy(result, path_n);
379 strcat(result, "/");
380 strcat(result, name);
381 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
382 free(path);
383 return;
385 path_n += (strlen(path_n) + 1);
387 free(path);
388 *result = '\0';
391 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
392 int is_suid, struct library *lib)
394 char *buf;
395 char *path;
396 struct stat filestat;
398 /* If this is a fully resolved name, our job is easy */
399 if (stat(lib->name, &filestat) == 0) {
400 lib->path = strdup(lib->name);
401 return;
404 /* We need some elbow room here. Make some room... */
405 buf = malloc(1024);
406 if (!buf) {
407 fprintf(stderr, "%s: Out of memory!\n", __func__);
408 exit(EXIT_FAILURE);
411 /* This function must match the behavior of _dl_load_shared_library
412 * in readelflib1.c or things won't work out as expected... */
414 /* The ABI specifies that RPATH is searched first, so do that now. */
415 path = elf_find_rpath(ehdr, dynamic);
416 if (path) {
417 search_for_named_library(lib->name, buf, path);
418 if (*buf != '\0') {
419 lib->path = buf;
420 return;
424 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
425 * Since this app doesn't actually run an executable I will skip
426 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
427 if (is_suid == 1)
428 path = NULL;
429 else
430 path = getenv("LD_LIBRARY_PATH");
431 if (path) {
432 search_for_named_library(lib->name, buf, path);
433 if (*buf != '\0') {
434 lib->path = buf;
435 return;
438 #ifdef __LDSO_CACHE_SUPPORT__
439 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
440 int i;
441 header_t *header = (header_t *) cache_addr;
442 libentry_t *libent = (libentry_t *) & header[1];
443 char *strs = (char *)&libent[header->nlibs];
445 for (i = 0; i < header->nlibs; i++) {
446 if ((libent[i].flags == LIB_ELF ||
447 libent[i].flags == LIB_ELF_LIBC0 ||
448 libent[i].flags == LIB_ELF_LIBC5) &&
449 strcmp(lib->name, strs + libent[i].sooffset) == 0)
451 lib->path = strdup(strs + libent[i].liboffset);
452 return;
456 #endif
458 /* Next look for libraries wherever the shared library
459 * loader was installed -- this is usually where we
460 * should find things... */
461 if (interp_dir) {
462 search_for_named_library(lib->name, buf, interp_dir);
463 if (*buf != '\0') {
464 lib->path = buf;
465 return;
469 /* Lastly, search the standard list of paths for the library.
470 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
471 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
472 #ifndef __LDSO_CACHE_SUPPORT__
473 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
474 #endif
476 search_for_named_library(lib->name, buf, path);
477 if (*buf != '\0') {
478 lib->path = buf;
479 } else {
480 free(buf);
481 lib->path = not_found;
485 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
487 char *tmp, *tmp1, *tmp2;
488 struct library *cur, *newlib = lib_list;
490 if (!s || !strlen(s))
491 return 1;
493 tmp = s;
494 while (*tmp) {
495 if (*tmp == '/')
496 s = tmp + 1;
497 tmp++;
500 /* We add ldso elsewhere */
501 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
502 int len = strlen(interp_dir);
503 if (strcmp(s, interp_name + 1 + len) == 0)
504 return 1;
507 for (cur = lib_list; cur; cur = cur->next) {
508 /* Check if this library is already in the list */
509 tmp1 = tmp2 = cur->name;
510 if (!cur->name)
511 continue;
512 while (*tmp1) {
513 if (*tmp1 == '/')
514 tmp2 = tmp1 + 1;
515 tmp1++;
517 if (strcmp(tmp2, s) == 0) {
518 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
519 return 0;
523 /* Ok, this lib needs to be added to the list */
524 newlib = malloc(sizeof(struct library));
525 if (!newlib)
526 return 1;
527 newlib->name = malloc(strlen(s) + 1);
528 strcpy(newlib->name, s);
529 newlib->resolved = 0;
530 newlib->path = NULL;
531 newlib->next = NULL;
533 /* Now try and locate where this library might be living... */
534 locate_library_file(ehdr, dynamic, is_setuid, newlib);
536 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
537 if (!lib_list) {
538 lib_list = newlib;
539 } else {
540 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
541 cur->next = newlib;
543 return 0;
546 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
548 ElfW(Dyn) *dyns;
550 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
551 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
552 char *strtab;
553 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
554 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
559 #ifdef __LDSO_LDD_SUPPORT__
560 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
562 ElfW(Phdr) *phdr;
564 if (interpreter_already_found == 1)
565 return NULL;
566 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
567 if (phdr) {
568 struct library *cur, *newlib = NULL;
569 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
571 char *tmp, *tmp1;
572 interp_name = strdup(s);
573 interp_dir = strdup(s);
574 tmp = strrchr(interp_dir, '/');
575 if (tmp)
576 *tmp = '\0';
577 else {
578 free(interp_dir);
579 interp_dir = interp_name;
581 tmp1 = tmp = s;
582 while (*tmp) {
583 if (*tmp == '/')
584 tmp1 = tmp + 1;
585 tmp++;
587 for (cur = lib_list; cur; cur = cur->next) {
588 /* Check if this library is already in the list */
589 if (!tmp1 || !cur->name)
590 return NULL;
591 if (strcmp(cur->name, tmp1) == 0) {
592 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
593 newlib = cur;
594 free(newlib->name);
595 if (newlib->path != not_found) {
596 free(newlib->path);
598 newlib->name = NULL;
599 newlib->path = NULL;
600 break;
603 if (newlib == NULL)
604 newlib = malloc(sizeof(struct library));
605 if (!newlib)
606 return NULL;
607 newlib->name = malloc(strlen(s) + 1);
608 strcpy(newlib->name, s);
609 newlib->path = strdup(newlib->name);
610 newlib->resolved = 1;
611 newlib->next = NULL;
613 #if 0
614 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
615 if (!lib_list) {
616 lib_list = newlib;
617 } else {
618 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
619 cur->next = newlib;
621 #endif
622 interpreter_already_found = 1;
623 return newlib;
625 return NULL;
627 #endif /* __LDSO_LDD_SUPPORT__ */
629 /* map the .so, and locate interesting pieces */
631 #warning "There may be two warnings here about vfork() clobbering, ignore them"
633 static int find_dependencies(char *filename)
635 int is_suid = 0;
636 FILE *thefile;
637 struct stat statbuf;
638 ElfW(Ehdr) *ehdr = NULL;
639 ElfW(Shdr) *dynsec = NULL;
640 ElfW(Dyn) *dynamic = NULL;
641 #ifdef __LDSO_LDD_SUPPORT__
642 struct library *interp;
643 #endif
645 if (filename == not_found)
646 return 0;
648 if (!filename) {
649 fprintf(stderr, "No filename specified.\n");
650 return -1;
652 if (!(thefile = fopen(filename, "r"))) {
653 perror(filename);
654 return -1;
656 if (fstat(fileno(thefile), &statbuf) < 0) {
657 perror(filename);
658 fclose(thefile);
659 return -1;
662 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
663 goto foo;
665 if (!S_ISREG(statbuf.st_mode))
666 goto foo;
668 /* mmap the file to make reading stuff from it effortless */
669 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
670 if (ehdr == MAP_FAILED) {
671 fclose(thefile);
672 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
673 return -1;
676 foo:
677 fclose(thefile);
679 /* Check if this looks like a legit ELF file */
680 if (check_elf_header(ehdr)) {
681 fprintf(stderr, "%s: not an ELF file.\n", filename);
682 return -1;
684 /* Check if this is the right kind of ELF file */
685 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
686 fprintf(stderr, "%s: not a dynamic executable\n", filename);
687 return -1;
689 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
690 if (statbuf.st_mode & S_ISUID)
691 is_suid = 1;
692 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
693 is_suid = 1;
694 /* FIXME */
695 if (is_suid)
696 fprintf(stderr, "%s: is setuid\n", filename);
699 interpreter_already_found = 0;
700 #ifdef __LDSO_LDD_SUPPORT__
701 interp = find_elf_interpreter(ehdr);
703 if (interp
704 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
705 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
706 && ehdr->e_ident[EI_DATA] == ELFDATAM
707 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
708 && MATCH_MACHINE(ehdr->e_machine))
710 struct stat st;
711 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
712 pid_t pid;
713 int status;
714 static const char *const environment[] = {
715 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
716 "SHELL=/bin/sh",
717 "LD_TRACE_LOADED_OBJECTS=1",
718 NULL
720 # ifdef __LDSO_STANDALONE_SUPPORT__
721 char * lib_path = getenv("LD_LIBRARY_PATH");
723 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
724 static char *ext_environment[ARRAY_SIZE(environment) + 1];
725 char **envp = (char **) environment;
727 if (lib_path) {
729 * If the LD_LIBRARY_PATH is set, it needs to include it
730 * into the environment for the new process to be spawned
732 char ** eenvp = (char **) ext_environment;
734 /* Copy the N-1 environment's entries */
735 while (*envp)
736 *eenvp++=*envp++;
738 /* Make room for LD_LIBRARY_PATH */
739 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
740 + strlen(lib_path));
741 strcpy(*eenvp, "LD_LIBRARY_PATH=");
742 strcat(*eenvp, lib_path);
743 lib_path = *eenvp;
744 /* ext_environment[size] is already NULL */
746 /* Use the extended environment */
747 envp = ext_environment;
749 if ((pid = vfork()) == 0) {
751 * Force to use the standard dynamic linker in stand-alone mode.
752 * It will fails at runtime if support is not actually available
754 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
755 _exit(0xdead);
757 # else
758 if ((pid = vfork()) == 0) {
759 /* Cool, it looks like we should be able to actually
760 * run this puppy. Do so now... */
761 execle(filename, filename, NULL, environment);
762 _exit(0xdead);
764 # endif
765 /* Wait till it returns */
766 waitpid(pid, &status, 0);
768 # ifdef __LDSO_STANDALONE_SUPPORT__
769 /* Do not leak */
770 free(lib_path);
771 # endif
773 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
774 return 1;
777 /* If the exec failed, we fall through to trying to find
778 * all the needed libraries ourselves by rummaging about
779 * in the ELF headers... */
782 #endif
784 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
785 if (dynsec) {
786 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
787 find_needed_libraries(ehdr, dynamic, is_suid);
790 return 0;
793 int main(int argc, char **argv)
795 int multi = 0;
796 int got_em_all = 1;
797 char *filename = NULL;
798 struct library *cur;
800 if (argc < 2) {
801 fprintf(stderr, "ldd: missing file arguments\n"
802 "Try `ldd --help' for more information.\n");
803 exit(EXIT_FAILURE);
805 if (argc > 2)
806 multi++;
808 while (--argc > 0) {
809 ++argv;
811 if (strcmp(*argv, "--") == 0) {
812 /* Ignore "--" */
813 continue;
816 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
817 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
818 "\t--help\t\tprint this help and exit\n");
819 exit(EXIT_SUCCESS);
822 filename = *argv;
823 if (!filename) {
824 fprintf(stderr, "No filename specified.\n");
825 exit(EXIT_FAILURE);
828 if (multi) {
829 printf("%s:\n", *argv);
832 map_cache();
834 if (find_dependencies(filename) != 0)
835 continue;
837 while (got_em_all) {
838 got_em_all = 0;
839 /* Keep walking the list till everybody is resolved */
840 for (cur = lib_list; cur; cur = cur->next) {
841 if (cur->resolved == 0 && cur->path) {
842 got_em_all = 1;
843 printf("checking sub-depends for '%s'\n", cur->path);
844 find_dependencies(cur->path);
845 cur->resolved = 1;
850 unmap_cache();
852 /* Print the list */
853 got_em_all = 0;
854 for (cur = lib_list; cur; cur = cur->next) {
855 got_em_all = 1;
856 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
858 if (interp_name && interpreter_already_found == 1)
859 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
860 else
861 printf("\tnot a dynamic executable\n");
863 for (cur = lib_list; cur; cur = cur->next) {
864 free(cur->name);
865 cur->name = NULL;
866 if (cur->path && cur->path != not_found) {
867 free(cur->path);
868 cur->path = NULL;
871 lib_list = NULL;
874 return 0;