1 /* vi: set sw=4 ts=4: */
3 * A small little ldd implementation for uClibc
5 * Copyright (C) 2000-2004 Erik Andersen <andersee@debian.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 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 #include <sys/types.h>
38 #include <sys/types.h>
50 #if defined(__alpha__)
51 #define MATCH_MACHINE(x) (x == EM_ALPHA)
52 #define ELFCLASSM ELFCLASS64
55 #if defined(__arm__) || defined(__thumb__)
56 #define MATCH_MACHINE(x) (x == EM_ARM)
57 #define ELFCLASSM ELFCLASS32
61 #define MATCH_MACHINE(x) (x == EM_S390)
62 #define ELFCLASSM ELFCLASS32
66 #define MATCH_MACHINE(x) (x == EM_PARISC)
68 #define ELFCLASSM ELFCLASS64
70 #define ELFCLASSM ELFCLASS32
76 #define MATCH_MACHINE(x) (x == EM_386)
78 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
80 #define ELFCLASSM ELFCLASS32
84 #define MATCH_MACHINE(x) (x == EM_IA_64)
85 #define ELFCLASSM ELFCLASS64
88 #if defined(__mc68000__)
89 #define MATCH_MACHINE(x) (x == EM_68K)
90 #define ELFCLASSM ELFCLASS32
94 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
95 #define ELFCLASSM ELFCLASS32
98 #if defined(__powerpc64__)
99 #define MATCH_MACHINE(x) (x == EM_PPC64)
100 #define ELFCLASSM ELFCLASS64
101 #elif defined(__powerpc__)
102 #define MATCH_MACHINE(x) (x == EM_PPC)
103 #define ELFCLASSM ELFCLASS32
107 #define MATCH_MACHINE(x) (x == EM_SH)
108 #define ELFCLASSM ELFCLASS32
111 #if defined(__v850e__)
112 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
113 #define ELFCLASSM ELFCLASS32
116 #if defined(__sparc__)
117 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
118 #define ELFCLASSM ELFCLASS32
121 #if defined(__cris__)
122 #define MATCH_MACHINE(x) (x == EM_CRIS)
123 #define ELFCLASSM ELFCLASS32
126 #if defined(__x86_64__)
127 #define MATCH_MACHINE(x) (x == EM_X86_64)
128 #define ELFCLASSM ELFCLASS64
131 #ifndef MATCH_MACHINE
133 # include <asm/elf.h>
136 # define MATCH_MACHINE(x) (x == ELF_ARCH)
139 # define ELFCLASSM ELF_CLASS
142 #ifndef MATCH_MACHINE
143 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
146 #if __BYTE_ORDER == __LITTLE_ENDIAN
147 #define ELFDATAM ELFDATA2LSB
148 #elif __BYTE_ORDER == __BIG_ENDIAN
149 #define ELFDATAM ELFDATA2MSB
152 #ifndef UCLIBC_RUNTIME_PREFIX
153 # define UCLIBC_RUNTIME_PREFIX "/"
160 struct library *next;
162 struct library *lib_list = NULL;
163 char not_found[] = "not found";
164 char *interp_name = NULL;
165 char *interp_dir = NULL;
167 static int interpreter_already_found=0;
169 inline uint32_t byteswap32_to_host(uint32_t value)
172 return(bswap_32(value));
177 inline uint64_t byteswap64_to_host(uint64_t value)
180 return(bswap_64(value));
185 #if ELFCLASSM == ELFCLASS32
186 # define byteswap_to_host(x) byteswap32_to_host(x)
188 # define byteswap_to_host(x) byteswap64_to_host(x)
191 ElfW(Shdr) * elf_find_section_type( uint32_t key, ElfW(Ehdr) *ehdr)
195 shdr = (ElfW(Shdr) *)(ehdr->e_shoff + (char *)ehdr);
196 for (j = ehdr->e_shnum; --j>=0; ++shdr) {
197 if (key==byteswap32_to_host(shdr->sh_type)) {
204 ElfW(Phdr) * elf_find_phdr_type( uint32_t type, ElfW(Ehdr) *ehdr)
207 ElfW(Phdr) *phdr = (ElfW(Phdr) *)(ehdr->e_phoff + (char *)ehdr);
208 for (j = ehdr->e_phnum; --j>=0; ++phdr) {
209 if (type==byteswap32_to_host(phdr->p_type)) {
216 /* Returns value if return_val==1, ptr otherwise */
217 void * elf_find_dynamic( int64_t const key, ElfW(Dyn) *dynp,
218 ElfW(Ehdr) *ehdr, int return_val)
220 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
221 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
222 for (; DT_NULL!=byteswap_to_host(dynp->d_tag); ++dynp) {
223 if (key == byteswap_to_host(dynp->d_tag)) {
225 return (void *)byteswap_to_host(dynp->d_un.d_val);
227 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
233 static char * elf_find_rpath(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic)
237 for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
238 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
240 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
241 return ((char*)strtab + byteswap_to_host(dyns->d_un.d_val));
247 int check_elf_header(ElfW(Ehdr) *const ehdr)
249 if (! ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 ||
250 ehdr->e_ident[EI_CLASS] != ELFCLASSM ||
251 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
256 /* Check if the target endianness matches the host's endianness */
258 #if __BYTE_ORDER == __LITTLE_ENDIAN
259 if (ehdr->e_ident[5] == ELFDATA2MSB) {
260 /* Ick -- we will have to byte-swap everything */
263 #elif __BYTE_ORDER == __BIG_ENDIAN
264 if (ehdr->e_ident[5] == ELFDATA2LSB) {
265 /* Ick -- we will have to byte-swap everything */
269 #error Unknown host byte order!
272 /* Be vary lazy, and only byteswap the stuff we use */
274 ehdr->e_type = bswap_16(ehdr->e_type);
275 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
276 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
277 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
278 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
284 #ifdef __LDSO_CACHE_SUPPORT__
285 static caddr_t cache_addr = NULL;
286 static size_t cache_size = 0;
296 if (cache_addr == (caddr_t) - 1)
298 else if (cache_addr != NULL)
301 if (stat(LDSO_CACHE, &st)
302 || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
303 dprintf(2, "ldd: can't open cache '%s'\n", LDSO_CACHE);
304 cache_addr = (caddr_t) - 1; /* so we won't try again */
308 cache_size = st.st_size;
309 cache_addr = (caddr_t) mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
311 if (cache_addr == MAP_FAILED) {
312 dprintf(2, "ldd: can't map cache '%s'\n", LDSO_CACHE);
316 header = (header_t *) cache_addr;
318 if (cache_size < sizeof(header_t) ||
319 memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
320 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
322 (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
323 || cache_addr[cache_size - 1] != '\0')
325 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
329 strtabsize = cache_size - sizeof(header_t) -
330 header->nlibs * sizeof(libentry_t);
331 libent = (libentry_t *) & header[1];
333 for (i = 0; i < header->nlibs; i++) {
334 if (libent[i].sooffset >= strtabsize ||
335 libent[i].liboffset >= strtabsize)
337 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
345 munmap(cache_addr, cache_size);
346 cache_addr = (caddr_t) - 1;
350 int unmap_cache(void)
352 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
356 munmap(cache_addr, cache_size);
363 static inline void map_cache(void) { }
364 static inline void unmap_cache(void) { }
367 /* This function's behavior must exactly match that
368 * in uClibc/ldso/ldso/dl-elf.c */
369 static void search_for_named_library(char *name, char *result, const char *path_list)
373 struct stat filestat;
375 /* We need a writable copy of this string */
376 path = strdup(path_list);
378 fprintf(stderr, "Out of memory!\n");
381 /* Eliminate all double //s */
383 while((path_n=strstr(path_n, "//"))) {
385 memmove(path_n, path_n+1, i-1);
386 *(path_n + i - 1)='\0';
389 /* Replace colons with zeros in path_list and count them */
390 for(i=strlen(path); i > 0; i--) {
397 for (i = 0; i < count; i++) {
398 strcpy(result, path_n);
400 strcat(result, name);
401 if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
405 path_n += (strlen(path_n) + 1);
411 void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, struct library *lib)
415 struct stat filestat;
417 /* If this is a fully resolved name, our job is easy */
418 if (stat (lib->name, &filestat) == 0) {
419 lib->path = strdup(lib->name);
423 /* We need some elbow room here. Make some room...*/
426 fprintf(stderr, "Out of memory!\n");
430 /* This function must match the behavior of _dl_load_shared_library
431 * in readelflib1.c or things won't work out as expected... */
433 /* The ABI specifies that RPATH is searched first, so do that now. */
434 path = elf_find_rpath(ehdr, dynamic);
436 search_for_named_library(lib->name, buf, path);
443 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
444 * Since this app doesn't actually run an executable I will skip
445 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
449 path = getenv("LD_LIBRARY_PATH");
451 search_for_named_library(lib->name, buf, path);
458 #ifdef __LDSO_CACHE_SUPPORT__
459 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
461 header_t *header = (header_t *) cache_addr;
462 libentry_t *libent = (libentry_t *) & header[1];
463 char *strs = (char *) &libent[header->nlibs];
465 for (i = 0; i < header->nlibs; i++) {
466 if ((libent[i].flags == LIB_ELF ||
467 libent[i].flags == LIB_ELF_LIBC0 ||
468 libent[i].flags == LIB_ELF_LIBC5) &&
469 strcmp(lib->name, strs + libent[i].sooffset) == 0) {
470 lib->path = strdup(strs + libent[i].liboffset);
478 /* Next look for libraries wherever the shared library
479 * loader was installed -- this is usually where we
480 * should find things... */
482 search_for_named_library(lib->name, buf, interp_dir);
489 /* Lastly, search the standard list of paths for the library.
490 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
491 path = UCLIBC_RUNTIME_PREFIX "lib:"
492 UCLIBC_RUNTIME_PREFIX "usr/lib"
493 #ifndef __LDSO_CACHE_SUPPORT__
494 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
497 search_for_named_library(lib->name, buf, path);
502 lib->path = not_found;
506 static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char *s)
508 char *tmp, *tmp1, *tmp2;
509 struct library *cur, *newlib=lib_list;
511 if (!s || !strlen(s))
521 /* We add ldso elsewhere */
522 if (interpreter_already_found && (tmp=strrchr(interp_name, '/')) != NULL)
524 int len = strlen(interp_dir);
525 if (strcmp(s, interp_name+1+len)==0)
529 for (cur = lib_list; cur; cur=cur->next) {
530 /* Check if this library is already in the list */
531 tmp1 = tmp2 = cur->name;
537 if(strcmp(tmp2, s)==0) {
538 //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
543 /* Ok, this lib needs to be added to the list */
544 newlib = malloc(sizeof(struct library));
547 newlib->name = malloc(strlen(s)+1);
548 strcpy(newlib->name, s);
549 newlib->resolved = 0;
553 /* Now try and locate where this library might be living... */
554 locate_library_file(ehdr, dynamic, is_setuid, newlib);
556 //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
560 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
566 static void find_needed_libraries(ElfW(Ehdr)* ehdr,
567 ElfW(Dyn)* dynamic, int is_setuid)
571 for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
572 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
574 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
575 add_library(ehdr, dynamic, is_setuid,
576 (char*)strtab + byteswap_to_host(dyns->d_un.d_val));
581 static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr)
585 if (interpreter_already_found == 1)
587 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
589 struct library *cur, *newlib=NULL;
590 char *s = (char*)ehdr + byteswap_to_host(phdr->p_offset);
593 interp_name = strdup(s);
594 interp_dir = strdup(s);
595 tmp = strrchr(interp_dir, '/');
600 interp_dir = interp_name;
608 for (cur = lib_list; cur; cur=cur->next) {
609 /* Check if this library is already in the list */
610 if(strcmp(cur->name, tmp1)==0) {
611 //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
614 if (newlib->path != not_found) {
623 newlib = malloc(sizeof(struct library));
626 newlib->name = malloc(strlen(s)+1);
627 strcpy(newlib->name, s);
628 newlib->path = strdup(newlib->name);
629 newlib->resolved = 1;
633 //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
637 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
641 interpreter_already_found = 1;
647 /* map the .so, and locate interesting pieces */
648 #warning "There may be two warnings here about vfork() clobbering, ignore them"
649 int find_dependancies(char* filename)
653 struct library *interp;
655 ElfW(Ehdr) *ehdr = NULL;
656 ElfW(Shdr) *dynsec = NULL;
657 ElfW(Dyn) *dynamic = NULL;
659 if (filename == not_found)
663 fprintf(stderr, "No filename specified.\n");
666 if (!(thefile = fopen(filename, "r"))) {
670 if (fstat(fileno(thefile), &statbuf) < 0) {
676 if ((size_t)statbuf.st_size < sizeof(ElfW(Ehdr)))
679 if (!S_ISREG(statbuf.st_mode))
682 /* mmap the file to make reading stuff from it effortless */
683 ehdr = (ElfW(Ehdr) *)mmap(0, statbuf.st_size,
684 PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
685 if (ehdr == MAP_FAILED) {
687 fprintf(stderr, "Out of memory!\n");
694 /* Check if this looks like a legit ELF file */
695 if (check_elf_header(ehdr)) {
696 fprintf(stderr, "%s: not an ELF file.\n", filename);
699 /* Check if this is the right kind of ELF file */
700 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
701 fprintf(stderr, "%s: not a dynamic executable\n", filename);
704 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
705 if (statbuf.st_mode & S_ISUID)
707 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
711 fprintf(stderr, "%s: is setuid\n", filename);
714 interpreter_already_found = 0;
715 interp = find_elf_interpreter(ehdr);
717 #ifdef __LDSO_LDD_SUPPORT__
719 (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) && \
720 ehdr->e_ident[EI_CLASS] == ELFCLASSM && ehdr->e_ident[EI_DATA] == ELFDATAM && \
721 ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
724 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
727 static const char * const environment[] = {
728 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
730 "LD_TRACE_LOADED_OBJECTS=1",
734 if ((pid = vfork()) == 0) {
735 /* Cool, it looks like we should be able to actually
736 * run this puppy. Do so now... */
737 execle(filename, filename, NULL, environment);
741 /* Wait till it returns */
742 waitpid(pid, &status, 0);
743 if (WIFEXITED(status) && WEXITSTATUS(status)==0) {
747 /* If the exec failed, we fall through to trying to find
748 * all the needed libraries ourselves by rummaging about
749 * in the ELF headers... */
754 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
756 dynamic = (ElfW(Dyn)*)(byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
757 find_needed_libraries(ehdr, dynamic, is_suid);
763 int main( int argc, char** argv)
767 char *filename = NULL;
771 fprintf(stderr, "ldd: missing file arguments\n");
772 fprintf(stderr, "Try `ldd --help' for more information.\n");
781 if(strcmp(*argv, "--")==0) {
786 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
787 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
788 fprintf(stderr, "\t--help\t\tprint this help and exit\n");
794 fprintf(stderr, "No filename specified.\n");
799 printf("%s:\n", *argv);
804 if (find_dependancies(filename)!=0)
809 /* Keep walking the list till everybody is resolved */
810 for (cur = lib_list; cur; cur=cur->next) {
811 if (cur->resolved == 0 && cur->path) {
813 printf("checking sub-depends for '%s'\n", cur->path);
814 find_dependancies(cur->path);
824 for (cur = lib_list; cur; cur=cur->next) {
826 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
828 if (interp_name && interpreter_already_found==1)
829 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
831 printf("\tnot a dynamic executable\n");
833 for (cur = lib_list; cur; cur=cur->next) {
836 if (cur->path && cur->path != not_found) {