1 /* vi: set sw=4 ts=4: */
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
13 * Licensed under GPLv2 or later
24 #include <sys/types.h>
25 #include <sys/types.h>
31 /* makefile will include elf.h for us */
37 #if defined(__alpha__)
38 #define MATCH_MACHINE(x) (x == EM_ALPHA)
39 #define ELFCLASSM ELFCLASS64
42 #if defined(__arm__) || defined(__thumb__)
43 #define MATCH_MACHINE(x) (x == EM_ARM)
44 #define ELFCLASSM ELFCLASS32
48 #define MATCH_MACHINE(x) (x == EM_S390)
49 #define ELFCLASSM ELFCLASS32
53 #define MATCH_MACHINE(x) (x == EM_PARISC)
55 #define ELFCLASSM ELFCLASS64
57 #define ELFCLASSM ELFCLASS32
63 #define MATCH_MACHINE(x) (x == EM_386)
65 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
67 #define ELFCLASSM ELFCLASS32
71 #define MATCH_MACHINE(x) (x == EM_IA_64)
72 #define ELFCLASSM ELFCLASS64
75 #if defined(__mc68000__)
76 #define MATCH_MACHINE(x) (x == EM_68K)
77 #define ELFCLASSM ELFCLASS32
81 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
82 #define ELFCLASSM ELFCLASS32
85 #if defined(__powerpc64__)
86 #define MATCH_MACHINE(x) (x == EM_PPC64)
87 #define ELFCLASSM ELFCLASS64
88 #elif defined(__powerpc__)
89 #define MATCH_MACHINE(x) (x == EM_PPC)
90 #define ELFCLASSM ELFCLASS32
94 #define MATCH_MACHINE(x) (x == EM_SH)
95 #define ELFCLASSM ELFCLASS32
98 #if defined(__v850e__)
99 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
100 #define ELFCLASSM ELFCLASS32
103 #if defined(__sparc__)
104 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
105 #define ELFCLASSM ELFCLASS32
108 #if defined(__cris__)
109 #define MATCH_MACHINE(x) (x == EM_CRIS)
110 #define ELFCLASSM ELFCLASS32
113 #if defined(__x86_64__)
114 #define MATCH_MACHINE(x) (x == EM_X86_64)
115 #define ELFCLASSM ELFCLASS64
118 #ifndef MATCH_MACHINE
120 # include <asm/elf.h>
123 # define MATCH_MACHINE(x) (x == ELF_ARCH)
126 # define ELFCLASSM ELF_CLASS
129 #ifndef MATCH_MACHINE
130 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
133 #if __BYTE_ORDER == __LITTLE_ENDIAN
134 #define ELFDATAM ELFDATA2LSB
135 #elif __BYTE_ORDER == __BIG_ENDIAN
136 #define ELFDATAM ELFDATA2MSB
139 #ifndef UCLIBC_RUNTIME_PREFIX
140 # define UCLIBC_RUNTIME_PREFIX "/"
147 struct library *next;
149 struct library *lib_list = NULL;
150 char not_found[] = "not found";
151 char *interp_name = NULL;
152 char *interp_dir = NULL;
154 static int interpreter_already_found = 0;
156 inline uint32_t byteswap32_to_host(uint32_t value)
159 return (bswap_32(value));
164 inline uint64_t byteswap64_to_host(uint64_t value)
167 return (bswap_64(value));
173 #if ELFCLASSM == ELFCLASS32
174 # define byteswap_to_host(x) byteswap32_to_host(x)
176 # define byteswap_to_host(x) byteswap64_to_host(x)
179 ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr)
183 shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr);
184 for (j = ehdr->e_shnum; --j >= 0; ++shdr) {
185 if (key == byteswap32_to_host(shdr->sh_type)) {
192 ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr)
195 ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr);
196 for (j = ehdr->e_phnum; --j >= 0; ++phdr) {
197 if (type == byteswap32_to_host(phdr->p_type)) {
204 /* Returns value if return_val==1, ptr otherwise */
205 void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp,
206 ElfW(Ehdr) *ehdr, int return_val)
208 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
209 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
210 for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) {
211 if (key == byteswap_to_host(dynp->d_tag)) {
213 return (void *)byteswap_to_host(dynp->d_un.d_val);
215 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr);
221 static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic)
225 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
226 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
228 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
229 return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val));
235 int check_elf_header(ElfW(Ehdr) *const ehdr)
237 if (!ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 ||
238 ehdr->e_ident[EI_CLASS] != ELFCLASSM ||
239 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
244 /* Check if the target endianness matches the host's endianness */
246 #if __BYTE_ORDER == __LITTLE_ENDIAN
247 if (ehdr->e_ident[5] == ELFDATA2MSB) {
248 /* Ick -- we will have to byte-swap everything */
251 #elif __BYTE_ORDER == __BIG_ENDIAN
252 if (ehdr->e_ident[5] == ELFDATA2LSB) {
253 /* Ick -- we will have to byte-swap everything */
257 #error Unknown host byte order!
260 /* Be vary lazy, and only byteswap the stuff we use */
262 ehdr->e_type = bswap_16(ehdr->e_type);
263 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
264 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
265 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
266 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
272 #ifdef __LDSO_CACHE_SUPPORT__
273 static caddr_t cache_addr = NULL;
274 static size_t cache_size = 0;
284 if (cache_addr == (caddr_t) - 1)
286 else if (cache_addr != NULL)
289 if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
290 fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE);
291 cache_addr = (caddr_t) - 1; /* so we won't try again */
295 cache_size = st.st_size;
296 cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
298 if (cache_addr == MAP_FAILED) {
299 fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE);
303 header = (header_t *) cache_addr;
305 if (cache_size < sizeof(header_t)
306 || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
307 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
308 || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
309 || cache_addr[cache_size - 1] != '\0')
311 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
315 strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t);
316 libent = (libentry_t *) & header[1];
318 for (i = 0; i < header->nlibs; i++) {
319 if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) {
320 fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
328 munmap(cache_addr, cache_size);
329 cache_addr = (caddr_t) - 1;
333 int unmap_cache(void)
335 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
339 munmap(cache_addr, cache_size);
346 static inline void map_cache(void)
349 static inline void unmap_cache(void)
354 /* This function's behavior must exactly match that
355 * in uClibc/ldso/ldso/dl-elf.c */
356 static void search_for_named_library(char *name, char *result,
357 const char *path_list)
361 struct stat filestat;
363 /* We need a writable copy of this string */
364 path = strdup(path_list);
366 fprintf(stderr, "Out of memory!\n");
369 /* Eliminate all double //s */
371 while ((path_n = strstr(path_n, "//"))) {
373 memmove(path_n, path_n + 1, i - 1);
374 *(path_n + i - 1) = '\0';
377 /* Replace colons with zeros in path_list and count them */
378 for (i = strlen(path); i > 0; i--) {
379 if (path[i] == ':') {
385 for (i = 0; i < count; i++) {
386 strcpy(result, path_n);
388 strcat(result, name);
389 if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
393 path_n += (strlen(path_n) + 1);
399 void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_suid,
404 struct stat filestat;
406 /* If this is a fully resolved name, our job is easy */
407 if (stat(lib->name, &filestat) == 0) {
408 lib->path = strdup(lib->name);
412 /* We need some elbow room here. Make some room... */
415 fprintf(stderr, "Out of memory!\n");
419 /* This function must match the behavior of _dl_load_shared_library
420 * in readelflib1.c or things won't work out as expected... */
422 /* The ABI specifies that RPATH is searched first, so do that now. */
423 path = elf_find_rpath(ehdr, dynamic);
425 search_for_named_library(lib->name, buf, path);
432 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
433 * Since this app doesn't actually run an executable I will skip
434 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
438 path = getenv("LD_LIBRARY_PATH");
440 search_for_named_library(lib->name, buf, path);
446 #ifdef __LDSO_CACHE_SUPPORT__
447 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
449 header_t *header = (header_t *) cache_addr;
450 libentry_t *libent = (libentry_t *) & header[1];
451 char *strs = (char *)&libent[header->nlibs];
453 for (i = 0; i < header->nlibs; i++) {
454 if ((libent[i].flags == LIB_ELF ||
455 libent[i].flags == LIB_ELF_LIBC0 ||
456 libent[i].flags == LIB_ELF_LIBC5) &&
457 strcmp(lib->name, strs + libent[i].sooffset) == 0)
459 lib->path = strdup(strs + libent[i].liboffset);
466 /* Next look for libraries wherever the shared library
467 * loader was installed -- this is usually where we
468 * should find things... */
470 search_for_named_library(lib->name, buf, interp_dir);
477 /* Lastly, search the standard list of paths for the library.
478 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
479 path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib"
480 #ifndef __LDSO_CACHE_SUPPORT__
481 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
484 search_for_named_library(lib->name, buf, path);
489 lib->path = not_found;
493 static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s)
495 char *tmp, *tmp1, *tmp2;
496 struct library *cur, *newlib = lib_list;
498 if (!s || !strlen(s))
508 /* We add ldso elsewhere */
509 if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) {
510 int len = strlen(interp_dir);
511 if (strcmp(s, interp_name + 1 + len) == 0)
515 for (cur = lib_list; cur; cur = cur->next) {
516 /* Check if this library is already in the list */
517 tmp1 = tmp2 = cur->name;
523 if (strcmp(tmp2, s) == 0) {
524 //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
529 /* Ok, this lib needs to be added to the list */
530 newlib = malloc(sizeof(struct library));
533 newlib->name = malloc(strlen(s) + 1);
534 strcpy(newlib->name, s);
535 newlib->resolved = 0;
539 /* Now try and locate where this library might be living... */
540 locate_library_file(ehdr, dynamic, is_setuid, newlib);
542 //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
546 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
552 static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid)
556 for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) {
557 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
559 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
560 add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val));
565 static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr)
569 if (interpreter_already_found == 1)
571 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
573 struct library *cur, *newlib = NULL;
574 char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset);
577 interp_name = strdup(s);
578 interp_dir = strdup(s);
579 tmp = strrchr(interp_dir, '/');
584 interp_dir = interp_name;
592 for (cur = lib_list; cur; cur = cur->next) {
593 /* Check if this library is already in the list */
594 if (strcmp(cur->name, tmp1) == 0) {
595 //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
598 if (newlib->path != not_found) {
607 newlib = malloc(sizeof(struct library));
610 newlib->name = malloc(strlen(s) + 1);
611 strcpy(newlib->name, s);
612 newlib->path = strdup(newlib->name);
613 newlib->resolved = 1;
617 //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
621 for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */
625 interpreter_already_found = 1;
631 /* map the .so, and locate interesting pieces */
633 #warning "There may be two warnings here about vfork() clobbering, ignore them"
635 int find_dependancies(char *filename)
639 struct library *interp;
641 ElfW(Ehdr) *ehdr = NULL;
642 ElfW(Shdr) *dynsec = NULL;
643 ElfW(Dyn) *dynamic = NULL;
645 if (filename == not_found)
649 fprintf(stderr, "No filename specified.\n");
652 if (!(thefile = fopen(filename, "r"))) {
656 if (fstat(fileno(thefile), &statbuf) < 0) {
662 if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr)))
665 if (!S_ISREG(statbuf.st_mode))
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) {
672 fprintf(stderr, "Out of memory!\n");
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);
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);
689 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
690 if (statbuf.st_mode & S_ISUID)
692 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
696 fprintf(stderr, "%s: is setuid\n", filename);
699 interpreter_already_found = 0;
700 interp = find_elf_interpreter(ehdr);
702 #ifdef __LDSO_LDD_SUPPORT__
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))
711 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
714 static const char *const environment[] = {
715 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
717 "LD_TRACE_LOADED_OBJECTS=1",
721 if ((pid = vfork()) == 0) {
722 /* Cool, it looks like we should be able to actually
723 * run this puppy. Do so now... */
724 execle(filename, filename, NULL, environment);
728 /* Wait till it returns */
729 waitpid(pid, &status, 0);
730 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
734 /* If the exec failed, we fall through to trying to find
735 * all the needed libraries ourselves by rummaging about
736 * in the ELF headers... */
741 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
743 dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
744 find_needed_libraries(ehdr, dynamic, is_suid);
750 int main(int argc, char **argv)
754 char *filename = NULL;
758 fprintf(stderr, "ldd: missing file arguments\n");
759 fprintf(stderr, "Try `ldd --help' for more information.\n");
768 if (strcmp(*argv, "--") == 0) {
773 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
774 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
775 fprintf(stderr, "\t--help\t\tprint this help and exit\n");
781 fprintf(stderr, "No filename specified.\n");
786 printf("%s:\n", *argv);
791 if (find_dependancies(filename) != 0)
796 /* Keep walking the list till everybody is resolved */
797 for (cur = lib_list; cur; cur = cur->next) {
798 if (cur->resolved == 0 && cur->path) {
800 printf("checking sub-depends for '%s'\n", cur->path);
801 find_dependancies(cur->path);
811 for (cur = lib_list; cur; cur = cur->next) {
813 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
815 if (interp_name && interpreter_already_found == 1)
816 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
818 printf("\tnot a dynamic executable\n");
820 for (cur = lib_list; cur; cur = cur->next) {
823 if (cur->path && cur->path != not_found) {