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
40 #include <sys/types.h>
41 #include <sys/types.h>
53 #if defined(__alpha__)
54 #define MATCH_MACHINE(x) (x == EM_ALPHA)
55 #define ELFCLASSM ELFCLASS64
58 #if defined(__arm__) || defined(__thumb__)
59 #define MATCH_MACHINE(x) (x == EM_ARM)
60 #define ELFCLASSM ELFCLASS32
64 #define MATCH_MACHINE(x) (x == EM_S390)
65 #define ELFCLASSM ELFCLASS32
69 #define MATCH_MACHINE(x) (x == EM_PARISC)
71 #define ELFCLASSM ELFCLASS64
73 #define ELFCLASSM ELFCLASS32
79 #define MATCH_MACHINE(x) (x == EM_386)
81 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
83 #define ELFCLASSM ELFCLASS32
87 #define MATCH_MACHINE(x) (x == EM_IA_64)
88 #define ELFCLASSM ELFCLASS64
91 #if defined(__mc68000__)
92 #define MATCH_MACHINE(x) (x == EM_68K)
93 #define ELFCLASSM ELFCLASS32
97 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
98 #define ELFCLASSM ELFCLASS32
101 #if defined(__powerpc64__)
102 #define MATCH_MACHINE(x) (x == EM_PPC64)
103 #define ELFCLASSM ELFCLASS64
104 #elif defined(__powerpc__)
105 #define MATCH_MACHINE(x) (x == EM_PPC)
106 #define ELFCLASSM ELFCLASS32
110 #define MATCH_MACHINE(x) (x == EM_SH)
111 #define ELFCLASSM ELFCLASS32
114 #if defined(__v850e__)
115 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
116 #define ELFCLASSM ELFCLASS32
119 #if defined(__sparc__)
120 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
121 #define ELFCLASSM ELFCLASS32
124 #if defined(__cris__)
125 #define MATCH_MACHINE(x) (x == EM_CRIS)
126 #define ELFCLASSM ELFCLASS32
129 #if defined(__x86_64__)
130 #define MATCH_MACHINE(x) (x == EM_X86_64)
131 #define ELFCLASSM ELFCLASS64
134 #ifndef MATCH_MACHINE
136 # include <asm/elf.h>
139 # define MATCH_MACHINE(x) (x == ELF_ARCH)
142 # define ELFCLASSM ELF_CLASS
145 #ifndef MATCH_MACHINE
146 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
149 #if __BYTE_ORDER == __LITTLE_ENDIAN
150 #define ELFDATAM ELFDATA2LSB
151 #elif __BYTE_ORDER == __BIG_ENDIAN
152 #define ELFDATAM ELFDATA2MSB
155 #ifndef UCLIBC_RUNTIME_PREFIX
156 # define UCLIBC_RUNTIME_PREFIX "/"
163 struct library *next;
165 struct library *lib_list = NULL;
166 char not_found[] = "not found";
167 char *interp_name = NULL;
168 char *interp_dir = NULL;
170 static int interpreter_already_found=0;
172 inline uint32_t byteswap32_to_host(uint32_t value)
175 return(bswap_32(value));
180 inline uint64_t byteswap64_to_host(uint64_t value)
183 return(bswap_64(value));
188 #if ELFCLASSM == ELFCLASS32
189 # define byteswap_to_host(x) byteswap32_to_host(x)
191 # define byteswap_to_host(x) byteswap64_to_host(x)
194 ElfW(Shdr) * elf_find_section_type( uint32_t key, ElfW(Ehdr) *ehdr)
198 shdr = (ElfW(Shdr) *)(ehdr->e_shoff + (char *)ehdr);
199 for (j = ehdr->e_shnum; --j>=0; ++shdr) {
200 if (key==byteswap32_to_host(shdr->sh_type)) {
207 ElfW(Phdr) * elf_find_phdr_type( uint32_t type, ElfW(Ehdr) *ehdr)
210 ElfW(Phdr) *phdr = (ElfW(Phdr) *)(ehdr->e_phoff + (char *)ehdr);
211 for (j = ehdr->e_phnum; --j>=0; ++phdr) {
212 if (type==byteswap32_to_host(phdr->p_type)) {
219 /* Returns value if return_val==1, ptr otherwise */
220 void * elf_find_dynamic(int const key, ElfW(Dyn) *dynp,
221 ElfW(Ehdr) *ehdr, int return_val)
223 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
224 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
225 for (; DT_NULL!=byteswap_to_host(dynp->d_tag); ++dynp) {
226 if (key == byteswap_to_host(dynp->d_tag)) {
228 return (void *)byteswap_to_host(dynp->d_un.d_val);
230 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
236 static char * elf_find_rpath(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic)
240 for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
241 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
243 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
244 return ((char*)strtab + byteswap_to_host(dyns->d_un.d_val));
250 int check_elf_header(ElfW(Ehdr) *const ehdr)
252 if (! ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 ||
253 ehdr->e_ident[EI_CLASS] != ELFCLASSM ||
254 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
259 /* Check if the target endianness matches the host's endianness */
261 #if __BYTE_ORDER == __LITTLE_ENDIAN
262 if (ehdr->e_ident[5] == ELFDATA2MSB) {
263 /* Ick -- we will have to byte-swap everything */
266 #elif __BYTE_ORDER == __BIG_ENDIAN
267 if (ehdr->e_ident[5] == ELFDATA2LSB) {
268 /* Ick -- we will have to byte-swap everything */
272 #error Unknown host byte order!
275 /* Be vary lazy, and only byteswap the stuff we use */
277 ehdr->e_type = bswap_16(ehdr->e_type);
278 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
279 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
280 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
281 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
287 #ifdef __LDSO_CACHE_SUPPORT__
288 static caddr_t cache_addr = NULL;
289 static size_t cache_size = 0;
299 if (cache_addr == (caddr_t) - 1)
301 else if (cache_addr != NULL)
304 if (stat(LDSO_CACHE, &st)
305 || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
306 dprintf(2, "ldd: can't open cache '%s'\n", LDSO_CACHE);
307 cache_addr = (caddr_t) - 1; /* so we won't try again */
311 cache_size = st.st_size;
312 cache_addr = (caddr_t) mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
314 if (cache_addr == MAP_FAILED) {
315 dprintf(2, "ldd: can't map cache '%s'\n", LDSO_CACHE);
319 header = (header_t *) cache_addr;
321 if (cache_size < sizeof(header_t) ||
322 memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
323 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
325 (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
326 || cache_addr[cache_size - 1] != '\0')
328 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
332 strtabsize = cache_size - sizeof(header_t) -
333 header->nlibs * sizeof(libentry_t);
334 libent = (libentry_t *) & header[1];
336 for (i = 0; i < header->nlibs; i++) {
337 if (libent[i].sooffset >= strtabsize ||
338 libent[i].liboffset >= strtabsize)
340 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
348 munmap(cache_addr, cache_size);
349 cache_addr = (caddr_t) - 1;
353 int unmap_cache(void)
355 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
359 munmap(cache_addr, cache_size);
366 static inline void map_cache(void) { }
367 static inline void unmap_cache(void) { }
370 /* This function's behavior must exactly match that
371 * in uClibc/ldso/ldso/dl-elf.c */
372 static void search_for_named_library(char *name, char *result, const char *path_list)
376 struct stat filestat;
378 /* We need a writable copy of this string */
379 path = strdup(path_list);
381 fprintf(stderr, "Out of memory!\n");
384 /* Eliminate all double //s */
386 while((path_n=strstr(path_n, "//"))) {
388 memmove(path_n, path_n+1, i-1);
389 *(path_n + i - 1)='\0';
392 /* Replace colons with zeros in path_list and count them */
393 for(i=strlen(path); i > 0; i--) {
400 for (i = 0; i < count; i++) {
401 strcpy(result, path_n);
403 strcat(result, name);
404 if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
408 path_n += (strlen(path_n) + 1);
414 void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, struct library *lib)
418 struct stat filestat;
420 /* If this is a fully resolved name, our job is easy */
421 if (stat (lib->name, &filestat) == 0) {
422 lib->path = strdup(lib->name);
426 /* We need some elbow room here. Make some room...*/
429 fprintf(stderr, "Out of memory!\n");
433 /* This function must match the behavior of _dl_load_shared_library
434 * in readelflib1.c or things won't work out as expected... */
436 /* The ABI specifies that RPATH is searched first, so do that now. */
437 path = elf_find_rpath(ehdr, dynamic);
439 search_for_named_library(lib->name, buf, path);
446 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
447 * Since this app doesn't actually run an executable I will skip
448 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
452 path = getenv("LD_LIBRARY_PATH");
454 search_for_named_library(lib->name, buf, path);
461 #ifdef __LDSO_CACHE_SUPPORT__
462 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
464 header_t *header = (header_t *) cache_addr;
465 libentry_t *libent = (libentry_t *) & header[1];
466 char *strs = (char *) &libent[header->nlibs];
468 for (i = 0; i < header->nlibs; i++) {
469 if ((libent[i].flags == LIB_ELF ||
470 libent[i].flags == LIB_ELF_LIBC0 ||
471 libent[i].flags == LIB_ELF_LIBC5) &&
472 strcmp(lib->name, strs + libent[i].sooffset) == 0) {
473 lib->path = strdup(strs + libent[i].liboffset);
481 /* Next look for libraries wherever the shared library
482 * loader was installed -- this is usually where we
483 * should find things... */
485 search_for_named_library(lib->name, buf, interp_dir);
492 /* Lastly, search the standard list of paths for the library.
493 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
494 path = UCLIBC_RUNTIME_PREFIX "lib:"
495 UCLIBC_RUNTIME_PREFIX "usr/lib"
496 #ifndef __LDSO_CACHE_SUPPORT__
497 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
500 search_for_named_library(lib->name, buf, path);
505 lib->path = not_found;
509 static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char *s)
511 char *tmp, *tmp1, *tmp2;
512 struct library *cur, *newlib=lib_list;
514 if (!s || !strlen(s))
524 /* We add ldso elsewhere */
525 if (interpreter_already_found && (tmp=strrchr(interp_name, '/')) != NULL)
527 int len = strlen(interp_dir);
528 if (strcmp(s, interp_name+1+len)==0)
532 for (cur = lib_list; cur; cur=cur->next) {
533 /* Check if this library is already in the list */
534 tmp1 = tmp2 = cur->name;
540 if(strcmp(tmp2, s)==0) {
541 //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
546 /* Ok, this lib needs to be added to the list */
547 newlib = malloc(sizeof(struct library));
550 newlib->name = malloc(strlen(s)+1);
551 strcpy(newlib->name, s);
552 newlib->resolved = 0;
556 /* Now try and locate where this library might be living... */
557 locate_library_file(ehdr, dynamic, is_setuid, newlib);
559 //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
563 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
569 static void find_needed_libraries(ElfW(Ehdr)* ehdr,
570 ElfW(Dyn)* dynamic, int is_setuid)
574 for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
575 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
577 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
578 add_library(ehdr, dynamic, is_setuid,
579 (char*)strtab + byteswap_to_host(dyns->d_un.d_val));
584 static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr)
588 if (interpreter_already_found == 1)
590 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
592 struct library *cur, *newlib=NULL;
593 char *s = (char*)ehdr + byteswap_to_host(phdr->p_offset);
596 interp_name = strdup(s);
597 interp_dir = strdup(s);
598 tmp = strrchr(interp_dir, '/');
603 interp_dir = interp_name;
611 for (cur = lib_list; cur; cur=cur->next) {
612 /* Check if this library is already in the list */
613 if(strcmp(cur->name, tmp1)==0) {
614 //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
617 if (newlib->path != not_found) {
626 newlib = malloc(sizeof(struct library));
629 newlib->name = malloc(strlen(s)+1);
630 strcpy(newlib->name, s);
631 newlib->path = strdup(newlib->name);
632 newlib->resolved = 1;
636 //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
640 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
644 interpreter_already_found = 1;
650 /* map the .so, and locate interesting pieces */
651 int find_dependancies(char* filename)
655 struct library *interp;
657 ElfW(Ehdr) *ehdr = NULL;
658 ElfW(Shdr) *dynsec = NULL;
659 ElfW(Dyn) *dynamic = NULL;
661 if (filename == not_found)
665 fprintf(stderr, "No filename specified.\n");
668 if (!(thefile = fopen(filename, "r"))) {
672 if (fstat(fileno(thefile), &statbuf) < 0) {
678 if ((size_t)statbuf.st_size < sizeof(ElfW(Ehdr)))
681 if (!S_ISREG(statbuf.st_mode))
684 /* mmap the file to make reading stuff from it effortless */
685 ehdr = (ElfW(Ehdr) *)mmap(0, statbuf.st_size,
686 PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
687 if (ehdr == MAP_FAILED) {
689 fprintf(stderr, "Out of memory!\n");
696 /* Check if this looks like a legit ELF file */
697 if (check_elf_header(ehdr)) {
698 fprintf(stderr, "%s: not an ELF file.\n", filename);
701 /* Check if this is the right kind of ELF file */
702 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
703 fprintf(stderr, "%s: not a dynamic executable\n", filename);
706 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
707 if (statbuf.st_mode & S_ISUID)
709 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
713 fprintf(stderr, "%s: is setuid\n", filename);
716 interpreter_already_found = 0;
717 interp = find_elf_interpreter(ehdr);
719 #ifdef __LDSO_LDD_SUPPORT__
721 (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) && \
722 ehdr->e_ident[EI_CLASS] == ELFCLASSM && ehdr->e_ident[EI_DATA] == ELFDATAM && \
723 ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
726 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
729 static const char * const environment[] = {
730 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
732 "LD_TRACE_LOADED_OBJECTS=1",
736 if ((pid = vfork()) == 0) {
737 /* Cool, it looks like we should be able to actually
738 * run this puppy. Do so now... */
739 execle(filename, filename, NULL, environment);
743 /* Wait till it returns */
744 waitpid(pid, &status, 0);
745 if (WIFEXITED(status) && WEXITSTATUS(status)==0) {
749 /* If the exec failed, we fall through to trying to find
750 * all the needed libraries ourselves by rummaging about
751 * in the ELF headers... */
756 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
758 dynamic = (ElfW(Dyn)*)(byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
759 find_needed_libraries(ehdr, dynamic, is_suid);
765 int main( int argc, char** argv)
769 char *filename = NULL;
773 fprintf(stderr, "ldd: missing file arguments\n");
774 fprintf(stderr, "Try `ldd --help' for more information.\n");
783 if(strcmp(*argv, "--")==0) {
788 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
789 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
790 fprintf(stderr, "\t--help\t\tprint this help and exit\n");
796 fprintf(stderr, "No filename specified.\n");
801 printf("%s:\n", *argv);
806 if (find_dependancies(filename)!=0)
811 /* Keep walking the list till everybody is resolved */
812 for (cur = lib_list; cur; cur=cur->next) {
813 if (cur->resolved == 0 && cur->path) {
815 printf("checking sub-depends for '%s'\n", cur->path);
816 find_dependancies(cur->path);
826 for (cur = lib_list; cur; cur=cur->next) {
828 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
830 if (interp_name && interpreter_already_found==1)
831 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
833 printf("\tnot a dynamic executable\n");
835 for (cur = lib_list; cur; cur=cur->next) {
838 if (cur->path && cur->path != not_found) {