test: add memmove test
[uclibc-ng.git] / utils / ldd.c
blob7fd4f7bb0aed72cd617ba173cccbeb4451f21302
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(__s390__)
39 #define MATCH_MACHINE(x) (x == EM_S390)
40 #define ELFCLASSM ELFCLASS32
41 #endif
43 #if defined(__hppa__)
44 #define MATCH_MACHINE(x) (x == EM_PARISC)
45 #if defined(__LP64__)
46 #define ELFCLASSM ELFCLASS64
47 #else
48 #define ELFCLASSM ELFCLASS32
49 #endif
50 #endif
52 #if defined(__i386__)
53 #ifndef EM_486
54 #define MATCH_MACHINE(x) (x == EM_386)
55 #else
56 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
57 #endif
58 #define ELFCLASSM ELFCLASS32
59 #endif
61 #if defined(__ia64__)
62 #define MATCH_MACHINE(x) (x == EM_IA_64)
63 #define ELFCLASSM ELFCLASS64
64 #endif
66 #if defined(__mc68000__)
67 #define MATCH_MACHINE(x) (x == EM_68K)
68 #define ELFCLASSM ELFCLASS32
69 #endif
71 #if defined(__metag__)
72 #define MATCH_MACHINE(x) (x == EM_METAG)
73 #define ELFCLASSM ELFCLASS32
74 #endif
76 #if defined(__mips__)
77 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
78 #define ELFCLASSM ELFCLASS32
79 #endif
81 #if defined(__powerpc64__)
82 #define MATCH_MACHINE(x) (x == EM_PPC64)
83 #define ELFCLASSM ELFCLASS64
84 #elif defined(__powerpc__)
85 #define MATCH_MACHINE(x) (x == EM_PPC)
86 #define ELFCLASSM ELFCLASS32
87 #endif
89 #if defined(__sh__)
90 #define MATCH_MACHINE(x) (x == EM_SH)
91 #define ELFCLASSM ELFCLASS32
92 #endif
94 #if defined(__sparc__)
95 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
96 #define ELFCLASSM ELFCLASS32
97 #endif
99 #if defined(__cris__)
100 #define MATCH_MACHINE(x) (x == EM_CRIS)
101 #define ELFCLASSM ELFCLASS32
102 #endif
104 #if defined(__x86_64__)
105 #define MATCH_MACHINE(x) (x == EM_X86_64)
106 #define ELFCLASSM ELFCLASS64
107 #endif
109 #if defined(__microblaze__)
110 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE_OLD)
111 #define ELFCLASSM ELFCLASS32
112 #endif
114 #if defined(__xtensa__)
115 #define MATCH_MACHINE(x) (x == EM_XTENSA)
116 #define ELFCLASSM ELFCLASS32
117 #endif
119 #ifndef MATCH_MACHINE
120 # ifdef __linux__
121 # include <asm/elf.h>
122 # endif
123 # ifdef ELF_ARCH
124 # define MATCH_MACHINE(x) (x == ELF_ARCH)
125 # endif
126 # ifdef ELF_CLASS
127 # define ELFCLASSM ELF_CLASS
128 # endif
129 #endif
130 #ifndef MATCH_MACHINE
131 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
132 #endif
134 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
135 #define ELFDATAM ELFDATA2LSB
136 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
137 #define ELFDATAM ELFDATA2MSB
138 #endif
140 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
142 struct library {
143 char *name;
144 int resolved;
145 char *path;
146 struct library *next;
148 static struct library *lib_list = NULL;
149 static char not_found[] = "not found";
150 static char *interp_name = NULL;
151 static char *interp_dir = NULL;
152 static int byteswap;
153 static int interpreter_already_found = 0;
155 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
157 if (byteswap) {
158 return (bswap_32(value));
159 } else {
160 return (value);
163 static __inline__ uint64_t byteswap64_to_host(uint64_t value)
165 if (byteswap) {
166 return (bswap_64(value));
167 } else {
168 return (value);
172 #if __WORDSIZE == 64
173 # define byteswap_to_host(x) byteswap64_to_host(x)
174 #else
175 # define byteswap_to_host(x) byteswap32_to_host(x)
176 #endif
178 static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
180 int j;
181 ElfW(Shdr) *shdr;
182 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
183 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
184 if (key == byteswap32_to_host(shdr->sh_type)) {
185 return shdr;
188 return NULL;
191 static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
193 int j;
194 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
195 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
196 if (type == byteswap32_to_host(phdr->p_type)) {
197 return phdr;
200 return NULL;
203 /* Returns value if return_val==1, ptr otherwise */
204 static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
205 ElfW(Ehdr) *ehdr, int return_val)
207 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
208 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
209 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
210 if (key == byteswap_to_host(dynp->d_tag)) {
211 if (return_val == 1)
212 return (void *)byteswap_to_host(dynp->d_un.d_val);
213 else
214 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
217 return NULL;
220 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
222 ElfW(Dyn) *dyns;
224 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
225 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
226 char *strtab;
227 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
228 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
231 return NULL;
234 static int check_elf_header(ElfW(Ehdr) *const ehdr)
236 if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32
237 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
238 || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5)
239 || ehdr->e_ident[EI_VERSION] != EV_CURRENT
241 return 1;
244 /* Check if the target endianness matches the host's endianness */
245 byteswap = !(ehdr->e_ident[5] == ELFDATAM);
247 /* Be very lazy, and only byteswap the stuff we use */
248 if (byteswap) {
249 ehdr->e_type = bswap_16(ehdr->e_type);
250 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
251 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
252 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
253 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
256 return 0;
259 #ifdef __LDSO_CACHE_SUPPORT__
260 static caddr_t cache_addr = NULL;
261 static size_t cache_size = 0;
263 static int map_cache(void)
265 int fd;
266 struct stat st;
267 header_t *header;
268 libentry_t *libent;
269 int i, strtabsize;
271 if (cache_addr == (caddr_t) - 1)
272 return -1;
273 else if (cache_addr != NULL)
274 return 0;
276 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) {
277 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
278 cache_addr = (caddr_t) - 1; /* so we won't try again */
279 return -1;
282 cache_size = st.st_size;
283 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
284 close(fd);
285 if (cache_addr == MAP_FAILED) {
286 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
287 return -1;
290 header = (header_t *) cache_addr;
292 if (cache_size < sizeof(header_t)
293 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
294 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
295 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
296 || cache_addr[cache_size - 1] != '\0')
298 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
299 goto fail;
302 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
303 libent = (libentry_t *) & header[1];
305 for (i = 0; i < header->nlibs; i++) {
306 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
307 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
308 goto fail;
312 return 0;
314 fail:
315 munmap(cache_addr, cache_size);
316 cache_addr = (caddr_t) - 1;
317 return -1;
320 static int unmap_cache(void)
322 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
323 return -1;
325 #if 1
326 munmap(cache_addr, cache_size);
327 cache_addr = NULL;
328 #endif
330 return 0;
332 #else
333 static __inline__ void map_cache(void)
336 static __inline__ void unmap_cache(void)
339 #endif
341 /* This function's behavior must exactly match that
342 * in uClibc/ldso/ldso/dl-elf.c */
343 static void search_for_named_library(char *name, char *result,
344 const char *path_list)
346 int i, count = 1;
347 char *path, *path_n;
348 struct stat filestat;
350 /* We need a writable copy of this string */
351 path = strdup(path_list);
352 if (!path) {
353 fprintf(stderr, "%s: Out of memory!\n", __func__);
354 exit(EXIT_FAILURE);
356 /* Eliminate all double //s */
357 path_n = path;
358 while ((path_n = strstr(path_n, "//"))) {
359 i = strlen(path_n);
360 memmove(path_n, path_n + 1, i - 1);
361 *(path_n + i - 1) = '\0';
364 /* Replace colons with zeros in path_list and count them */
365 for (i = strlen(path); i > 0; i--) {
366 if (path[i] == ':') {
367 path[i] = 0;
368 count++;
371 path_n = path;
372 for (i = 0; i < count; i++) {
373 strcpy(result, path_n);
374 strcat(result, "/");
375 strcat(result, name);
376 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
377 free(path);
378 return;
380 path_n += (strlen(path_n) + 1);
382 free(path);
383 *result = '\0';
386 static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic,
387 int is_suid, struct library *lib)
389 char *buf;
390 char *path;
391 struct stat filestat;
393 /* If this is a fully resolved name, our job is easy */
394 if (stat(lib->name, &filestat) == 0) {
395 lib->path = strdup(lib->name);
396 return;
399 /* We need some elbow room here. Make some room... */
400 buf = malloc(1024);
401 if (!buf) {
402 fprintf(stderr, "%s: Out of memory!\n", __func__);
403 exit(EXIT_FAILURE);
406 /* This function must match the behavior of _dl_load_shared_library
407 * in readelflib1.c or things won't work out as expected... */
409 /* The ABI specifies that RPATH is searched first, so do that now. */
410 path = elf_find_rpath(ehdr, dynamic);
411 if (path) {
412 search_for_named_library(lib->name, buf, path);
413 if (*buf != '\0') {
414 lib->path = buf;
415 return;
419 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
420 * Since this app doesn't actually run an executable I will skip
421 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
422 if (is_suid == 1)
423 path = NULL;
424 else
425 path = getenv("LD_LIBRARY_PATH");
426 if (path) {
427 search_for_named_library(lib->name, buf, path);
428 if (*buf != '\0') {
429 lib->path = buf;
430 return;
433 #ifdef __LDSO_CACHE_SUPPORT__
434 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
435 int i;
436 header_t *header = (header_t *) cache_addr;
437 libentry_t *libent = (libentry_t *) & header[1];
438 char *strs = (char *)&libent[header->nlibs];
440 for (i = 0; i < header->nlibs; i++) {
441 if ((libent[i].flags == LIB_ELF ||
442 libent[i].flags == LIB_ELF_LIBC0 ||
443 libent[i].flags == LIB_ELF_LIBC5) &&
444 strcmp(lib->name, strs + libent[i].sooffset) == 0)
446 lib->path = strdup(strs + libent[i].liboffset);
447 return;
451 #endif
453 /* Next look for libraries wherever the shared library
454 * loader was installed -- this is usually where we
455 * should find things... */
456 if (interp_dir) {
457 search_for_named_library(lib->name, buf, interp_dir);
458 if (*buf != '\0') {
459 lib->path = buf;
460 return;
464 /* Lastly, search the standard list of paths for the library.
465 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
466 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
467 #ifndef __LDSO_CACHE_SUPPORT__
468 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
469 #endif
471 search_for_named_library(lib->name, buf, path);
472 if (*buf != '\0') {
473 lib->path = buf;
474 } else {
475 free(buf);
476 lib->path = not_found;
480 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
482 char *tmp, *tmp1, *tmp2;
483 struct library *cur, *newlib = lib_list;
485 if (!s || !strlen(s))
486 return 1;
488 tmp = s;
489 while (*tmp) {
490 if (*tmp == '/')
491 s = tmp + 1;
492 tmp++;
495 /* We add ldso elsewhere */
496 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
497 int len = strlen(interp_dir);
498 if (strcmp(s, interp_name + 1 + len) == 0)
499 return 1;
502 for (cur = lib_list; cur; cur = cur->next) {
503 /* Check if this library is already in the list */
504 tmp1 = tmp2 = cur->name;
505 if (!cur->name)
506 continue;
507 while (*tmp1) {
508 if (*tmp1 == '/')
509 tmp2 = tmp1 + 1;
510 tmp1++;
512 if (strcmp(tmp2, s) == 0) {
513 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
514 return 0;
518 /* Ok, this lib needs to be added to the list */
519 newlib = malloc(sizeof(struct library));
520 if (!newlib)
521 return 1;
522 newlib->name = malloc(strlen(s) + 1);
523 strcpy(newlib->name, s);
524 newlib->resolved = 0;
525 newlib->path = NULL;
526 newlib->next = NULL;
528 /* Now try and locate where this library might be living... */
529 locate_library_file(ehdr, dynamic, is_setuid, newlib);
531 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
532 if (!lib_list) {
533 lib_list = newlib;
534 } else {
535 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
536 cur->next = newlib;
538 return 0;
541 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
543 ElfW(Dyn) *dyns;
545 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
546 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
547 char *strtab;
548 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
549 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
554 #ifdef __LDSO_LDD_SUPPORT__
555 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
557 ElfW(Phdr) *phdr;
559 if (interpreter_already_found == 1)
560 return NULL;
561 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
562 if (phdr) {
563 struct library *cur, *newlib = NULL;
564 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
566 char *tmp, *tmp1;
567 interp_name = strdup(s);
568 interp_dir = strdup(s);
569 tmp = strrchr(interp_dir, '/');
570 if (tmp)
571 *tmp = '\0';
572 else {
573 free(interp_dir);
574 interp_dir = interp_name;
576 tmp1 = tmp = s;
577 while (*tmp) {
578 if (*tmp == '/')
579 tmp1 = tmp + 1;
580 tmp++;
582 for (cur = lib_list; cur; cur = cur->next) {
583 /* Check if this library is already in the list */
584 if (!tmp1 || !cur->name)
585 return NULL;
586 if (strcmp(cur->name, tmp1) == 0) {
587 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
588 newlib = cur;
589 free(newlib->name);
590 if (newlib->path != not_found) {
591 free(newlib->path);
593 newlib->name = NULL;
594 newlib->path = NULL;
595 break;
598 if (newlib == NULL)
599 newlib = malloc(sizeof(struct library));
600 if (!newlib)
601 return NULL;
602 newlib->name = malloc(strlen(s) + 1);
603 strcpy(newlib->name, s);
604 newlib->path = strdup(newlib->name);
605 newlib->resolved = 1;
606 newlib->next = NULL;
608 #if 0
609 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
610 if (!lib_list) {
611 lib_list = newlib;
612 } else {
613 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
614 cur->next = newlib;
616 #endif
617 interpreter_already_found = 1;
618 return newlib;
620 return NULL;
622 #endif /* __LDSO_LDD_SUPPORT__ */
624 /* map the .so, and locate interesting pieces */
626 #warning "There may be two warnings here about vfork() clobbering, ignore them"
628 static int find_dependencies(char *filename)
630 int is_suid = 0;
631 FILE *thefile;
632 struct stat statbuf;
633 ElfW(Ehdr) *ehdr = NULL;
634 ElfW(Shdr) *dynsec = NULL;
635 ElfW(Dyn) *dynamic = NULL;
636 #ifdef __LDSO_LDD_SUPPORT__
637 struct library *interp;
638 #endif
640 if (filename == not_found)
641 return 0;
643 if (!filename) {
644 fprintf(stderr, "No filename specified.\n");
645 return -1;
647 if (!(thefile = fopen(filename, "r"))) {
648 perror(filename);
649 return -1;
651 if (fstat(fileno(thefile), &statbuf) < 0) {
652 perror(filename);
653 fclose(thefile);
654 return -1;
657 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
658 goto foo;
660 if (!S_ISREG(statbuf.st_mode))
661 goto foo;
663 /* mmap the file to make reading stuff from it effortless */
664 ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
665 if (ehdr == MAP_FAILED) {
666 fclose(thefile);
667 fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno));
668 return -1;
671 foo:
672 fclose(thefile);
674 /* Check if this looks like a legit ELF file */
675 if (check_elf_header(ehdr)) {
676 fprintf(stderr, "%s: not an ELF file.\n", filename);
677 return -1;
679 /* Check if this is the right kind of ELF file */
680 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
681 fprintf(stderr, "%s: not a dynamic executable\n", filename);
682 return -1;
684 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
685 if (statbuf.st_mode & S_ISUID)
686 is_suid = 1;
687 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
688 is_suid = 1;
689 /* FIXME */
690 if (is_suid)
691 fprintf(stderr, "%s: is setuid\n", filename);
694 interpreter_already_found = 0;
695 #ifdef __LDSO_LDD_SUPPORT__
696 interp = find_elf_interpreter(ehdr);
698 if (interp
699 && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN)
700 && ehdr->e_ident[EI_CLASS] == ELFCLASSM
701 && ehdr->e_ident[EI_DATA] == ELFDATAM
702 && ehdr->e_ident[EI_VERSION] == EV_CURRENT
703 && MATCH_MACHINE(ehdr->e_machine))
705 struct stat st;
706 if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) {
707 pid_t pid;
708 int status;
709 static const char *const environment[] = {
710 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
711 "SHELL=/bin/sh",
712 "LD_TRACE_LOADED_OBJECTS=1",
713 NULL
715 # ifdef __LDSO_STANDALONE_SUPPORT__
716 char * lib_path = getenv("LD_LIBRARY_PATH");
718 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
719 static char *ext_environment[ARRAY_SIZE(environment) + 1];
720 char **envp = (char **) environment;
722 if (lib_path) {
724 * If the LD_LIBRARY_PATH is set, it needs to include it
725 * into the environment for the new process to be spawned
727 char ** eenvp = (char **) ext_environment;
729 /* Copy the N-1 environment's entries */
730 while (*envp)
731 *eenvp++=*envp++;
733 /* Make room for LD_LIBRARY_PATH */
734 *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=")
735 + strlen(lib_path));
736 strcpy(*eenvp, "LD_LIBRARY_PATH=");
737 strcat(*eenvp, lib_path);
738 lib_path = *eenvp;
739 /* ext_environment[size] is already NULL */
741 /* Use the extended environment */
742 envp = ext_environment;
744 if ((pid = vfork()) == 0) {
746 * Force to use the standard dynamic linker in stand-alone mode.
747 * It will fails at runtime if support is not actually available
749 execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp);
750 _exit(0xdead);
752 # else
753 if ((pid = vfork()) == 0) {
754 /* Cool, it looks like we should be able to actually
755 * run this puppy. Do so now... */
756 execle(filename, filename, NULL, environment);
757 _exit(0xdead);
759 # endif
760 /* Wait till it returns */
761 waitpid(pid, &status, 0);
763 # ifdef __LDSO_STANDALONE_SUPPORT__
764 /* Do not leak */
765 free(lib_path);
766 # endif
768 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
769 return 1;
772 /* If the exec failed, we fall through to trying to find
773 * all the needed libraries ourselves by rummaging about
774 * in the ELF headers... */
777 #endif
779 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
780 if (dynsec) {
781 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
782 find_needed_libraries(ehdr, dynamic, is_suid);
785 return 0;
788 int main(int argc, char **argv)
790 int multi = 0;
791 int got_em_all = 1;
792 char *filename = NULL;
793 struct library *cur;
795 if (argc < 2) {
796 fprintf(stderr, "ldd: missing file arguments\n"
797 "Try `ldd --help' for more information.\n");
798 exit(EXIT_FAILURE);
800 if (argc > 2)
801 multi++;
803 while (--argc > 0) {
804 ++argv;
806 if (strcmp(*argv, "--") == 0) {
807 /* Ignore "--" */
808 continue;
811 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
812 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n"
813 "\t--help\t\tprint this help and exit\n");
814 exit(EXIT_SUCCESS);
817 filename = *argv;
818 if (!filename) {
819 fprintf(stderr, "No filename specified.\n");
820 exit(EXIT_FAILURE);
823 if (multi) {
824 printf("%s:\n", *argv);
827 map_cache();
829 if (find_dependencies(filename) != 0)
830 continue;
832 while (got_em_all) {
833 got_em_all = 0;
834 /* Keep walking the list till everybody is resolved */
835 for (cur = lib_list; cur; cur = cur->next) {
836 if (cur->resolved == 0 && cur->path) {
837 got_em_all = 1;
838 printf("checking sub-depends for '%s'\n", cur->path);
839 find_dependencies(cur->path);
840 cur->resolved = 1;
845 unmap_cache();
847 /* Print the list */
848 got_em_all = 0;
849 for (cur = lib_list; cur; cur = cur->next) {
850 got_em_all = 1;
851 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
853 if (interp_name && interpreter_already_found == 1)
854 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
855 else
856 printf("\tnot a dynamic executable\n");
858 for (cur = lib_list; cur; cur = cur->next) {
859 free(cur->name);
860 cur->name = NULL;
861 if (cur->path && cur->path != not_found) {
862 free(cur->path);
863 cur->path = NULL;
866 lib_list = NULL;
869 return 0;