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
39 #include <sys/types.h>
40 #include <sys/types.h>
52 #if defined(__alpha__)
53 #define MATCH_MACHINE(x) (x == EM_ALPHA)
54 #define ELFCLASSM ELFCLASS64
57 #if defined(__arm__) || defined(__thumb__)
58 #define MATCH_MACHINE(x) (x == EM_ARM)
59 #define ELFCLASSM ELFCLASS32
63 #define MATCH_MACHINE(x) (x == EM_S390)
64 #define ELFCLASSM ELFCLASS32
68 #define MATCH_MACHINE(x) (x == EM_PARISC)
70 #define ELFCLASSM ELFCLASS64
72 #define ELFCLASSM ELFCLASS32
78 #define MATCH_MACHINE(x) (x == EM_386)
80 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
82 #define ELFCLASSM ELFCLASS32
86 #define MATCH_MACHINE(x) (x == EM_IA_64)
87 #define ELFCLASSM ELFCLASS64
90 #if defined(__mc68000__)
91 #define MATCH_MACHINE(x) (x == EM_68K)
92 #define ELFCLASSM ELFCLASS32
96 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
97 #define ELFCLASSM ELFCLASS32
100 #if defined(__powerpc64__)
101 #define MATCH_MACHINE(x) (x == EM_PPC64)
102 #define ELFCLASSM ELFCLASS64
103 #elif defined(__powerpc__)
104 #define MATCH_MACHINE(x) (x == EM_PPC)
105 #define ELFCLASSM ELFCLASS32
109 #define MATCH_MACHINE(x) (x == EM_SH)
110 #define ELFCLASSM ELFCLASS32
113 #if defined(__v850e__)
114 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
115 #define ELFCLASSM ELFCLASS32
118 #if defined(__sparc__)
119 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
120 #define ELFCLASSM ELFCLASS32
123 #if defined(__cris__)
124 #define MATCH_MACHINE(x) (x == EM_CRIS)
125 #define ELFCLASSM ELFCLASS32
128 #if defined(__x86_64__)
129 #define MATCH_MACHINE(x) (x == EM_X86_64)
130 #define ELFCLASSM ELFCLASS64
133 #ifndef MATCH_MACHINE
135 # include <asm/elf.h>
138 # define MATCH_MACHINE(x) (x == ELF_ARCH)
141 # define ELFCLASSM ELF_CLASS
144 #ifndef MATCH_MACHINE
145 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
148 #if __BYTE_ORDER == __LITTLE_ENDIAN
149 #define ELFDATAM ELFDATA2LSB
150 #elif __BYTE_ORDER == __BIG_ENDIAN
151 #define ELFDATAM ELFDATA2MSB
158 struct library *next;
160 struct library *lib_list = NULL;
161 char not_found[] = "not found";
162 char *interp_name = NULL;
163 char *interp_dir = NULL;
165 static int interpreter_already_found=0;
167 inline uint32_t byteswap32_to_host(uint32_t value)
170 return(bswap_32(value));
175 inline uint64_t byteswap64_to_host(uint64_t value)
178 return(bswap_64(value));
183 #if ELFCLASSM == ELFCLASS32
184 # define byteswap_to_host(x) byteswap32_to_host(x)
186 # define byteswap_to_host(x) byteswap64_to_host(x)
189 ElfW(Shdr) * elf_find_section_type( int key, ElfW(Ehdr) *ehdr)
193 shdr = (ElfW(Shdr) *)(ehdr->e_shoff + (char *)ehdr);
194 for (j = ehdr->e_shnum; --j>=0; ++shdr) {
195 if (key==byteswap32_to_host(shdr->sh_type)) {
202 ElfW(Phdr) * elf_find_phdr_type( int type, ElfW(Ehdr) *ehdr)
205 ElfW(Phdr) *phdr = (ElfW(Phdr) *)(ehdr->e_phoff + (char *)ehdr);
206 for (j = ehdr->e_phnum; --j>=0; ++phdr) {
207 if (type==byteswap32_to_host(phdr->p_type)) {
214 /* Returns value if return_val==1, ptr otherwise */
215 void * elf_find_dynamic(int const key, ElfW(Dyn) *dynp,
216 ElfW(Ehdr) *ehdr, int return_val)
218 ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
219 unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset);
220 for (; DT_NULL!=byteswap_to_host(dynp->d_tag); ++dynp) {
221 if (key == byteswap_to_host(dynp->d_tag)) {
223 return (void *)byteswap_to_host(dynp->d_un.d_val);
225 return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
231 static char * elf_find_rpath(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic)
235 for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
236 if (DT_RPATH == byteswap_to_host(dyns->d_tag)) {
238 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
239 return ((char*)strtab + byteswap_to_host(dyns->d_un.d_val));
245 int check_elf_header(ElfW(Ehdr) *const ehdr)
247 if (! ehdr || strncmp((char *)ehdr, ELFMAG, SELFMAG) != 0 ||
248 ehdr->e_ident[EI_CLASS] != ELFCLASSM ||
249 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
254 /* Check if the target endianness matches the host's endianness */
256 #if __BYTE_ORDER == __LITTLE_ENDIAN
257 if (ehdr->e_ident[5] == ELFDATA2MSB) {
258 /* Ick -- we will have to byte-swap everything */
261 #elif __BYTE_ORDER == __BIG_ENDIAN
262 if (ehdr->e_ident[5] == ELFDATA2LSB) {
263 /* Ick -- we will have to byte-swap everything */
267 #error Unknown host byte order!
270 /* Be vary lazy, and only byteswap the stuff we use */
272 ehdr->e_type = bswap_16(ehdr->e_type);
273 ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff);
274 ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff);
275 ehdr->e_phnum = bswap_16(ehdr->e_phnum);
276 ehdr->e_shnum = bswap_16(ehdr->e_shnum);
282 #ifdef __LDSO_CACHE_SUPPORT__
283 static caddr_t cache_addr = NULL;
284 static size_t cache_size = 0;
294 if (cache_addr == (caddr_t) - 1)
296 else if (cache_addr != NULL)
299 if (stat(LDSO_CACHE, &st)
300 || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
301 dprintf(2, "ldd: can't open cache '%s'\n", LDSO_CACHE);
302 cache_addr = (caddr_t) - 1; /* so we won't try again */
306 cache_size = st.st_size;
307 cache_addr = (caddr_t) mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
309 if (cache_addr == MAP_FAILED) {
310 dprintf(2, "ldd: can't map cache '%s'\n", LDSO_CACHE);
314 header = (header_t *) cache_addr;
316 if (cache_size < sizeof(header_t) ||
317 memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
318 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
320 (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
321 || cache_addr[cache_size - 1] != '\0')
323 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
327 strtabsize = cache_size - sizeof(header_t) -
328 header->nlibs * sizeof(libentry_t);
329 libent = (libentry_t *) & header[1];
331 for (i = 0; i < header->nlibs; i++) {
332 if (libent[i].sooffset >= strtabsize ||
333 libent[i].liboffset >= strtabsize)
335 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
343 munmap(cache_addr, cache_size);
344 cache_addr = (caddr_t) - 1;
348 int unmap_cache(void)
350 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
354 munmap(cache_addr, cache_size);
361 static inline void map_cache(void) { }
362 static inline void unmap_cache(void) { }
365 /* This function's behavior must exactly match that
366 * in uClibc/ldso/ldso/dl-elf.c */
367 static void search_for_named_library(char *name, char *result, const char *path_list)
371 struct stat filestat;
373 /* We need a writable copy of this string */
374 path = strdup(path_list);
376 fprintf(stderr, "Out of memory!\n");
379 /* Eliminate all double //s */
381 while((path_n=strstr(path_n, "//"))) {
383 memmove(path_n, path_n+1, i-1);
384 *(path_n + i - 1)='\0';
387 /* Replace colons with zeros in path_list and count them */
388 for(i=strlen(path); i > 0; i--) {
395 for (i = 0; i < count; i++) {
396 strcpy(result, path_n);
398 strcat(result, name);
399 if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
403 path_n += (strlen(path_n) + 1);
409 void locate_library_file(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_suid, struct library *lib)
413 struct stat filestat;
415 /* If this is a fully resolved name, our job is easy */
416 if (stat (lib->name, &filestat) == 0) {
417 lib->path = strdup(lib->name);
421 /* We need some elbow room here. Make some room...*/
424 fprintf(stderr, "Out of memory!\n");
428 /* This function must match the behavior of _dl_load_shared_library
429 * in readelflib1.c or things won't work out as expected... */
431 /* The ABI specifies that RPATH is searched first, so do that now. */
432 path = elf_find_rpath(ehdr, dynamic);
434 search_for_named_library(lib->name, buf, path);
441 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
442 * Since this app doesn't actually run an executable I will skip
443 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
447 path = getenv("LD_LIBRARY_PATH");
449 search_for_named_library(lib->name, buf, path);
456 #ifdef __LDSO_CACHE_SUPPORT__
457 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
459 header_t *header = (header_t *) cache_addr;
460 libentry_t *libent = (libentry_t *) & header[1];
461 char *strs = (char *) &libent[header->nlibs];
463 for (i = 0; i < header->nlibs; i++) {
464 if ((libent[i].flags == LIB_ELF ||
465 libent[i].flags == LIB_ELF_LIBC0 ||
466 libent[i].flags == LIB_ELF_LIBC5) &&
467 strcmp(lib->name, strs + libent[i].sooffset) == 0) {
468 lib->path = strdup(strs + libent[i].liboffset);
476 /* Next look for libraries wherever the shared library
477 * loader was installed -- this is usually where we
478 * should find things... */
480 search_for_named_library(lib->name, buf, interp_dir);
487 /* Lastly, search the standard list of paths for the library.
488 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
489 path = UCLIBC_RUNTIME_PREFIX "lib:"
490 UCLIBC_RUNTIME_PREFIX "usr/lib"
491 #ifndef __LDSO_CACHE_SUPPORT__
492 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
495 search_for_named_library(lib->name, buf, path);
500 lib->path = not_found;
504 static int add_library(ElfW(Ehdr)* ehdr, ElfW(Dyn)* dynamic, int is_setuid, char *s)
506 char *tmp, *tmp1, *tmp2;
507 struct library *cur, *newlib=lib_list;
509 if (!s || !strlen(s))
519 /* We add ldso elsewhere */
520 if (interpreter_already_found && (tmp=strrchr(interp_name, '/')) != NULL)
522 int len = strlen(interp_dir);
523 if (strcmp(s, interp_name+1+len)==0)
527 for (cur = lib_list; cur; cur=cur->next) {
528 /* Check if this library is already in the list */
529 tmp1 = tmp2 = cur->name;
535 if(strcmp(tmp2, s)==0) {
536 //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
541 /* Ok, this lib needs to be added to the list */
542 newlib = malloc(sizeof(struct library));
545 newlib->name = malloc(strlen(s)+1);
546 strcpy(newlib->name, s);
547 newlib->resolved = 0;
551 /* Now try and locate where this library might be living... */
552 locate_library_file(ehdr, dynamic, is_setuid, newlib);
554 //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
558 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
564 static void find_needed_libraries(ElfW(Ehdr)* ehdr,
565 ElfW(Dyn)* dynamic, int is_setuid)
569 for (dyns=dynamic; byteswap_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
570 if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) {
572 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
573 add_library(ehdr, dynamic, is_setuid,
574 (char*)strtab + byteswap_to_host(dyns->d_un.d_val));
579 static struct library * find_elf_interpreter(ElfW(Ehdr)* ehdr)
583 if (interpreter_already_found == 1)
585 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
587 struct library *cur, *newlib=NULL;
588 char *s = (char*)ehdr + byteswap_to_host(phdr->p_offset);
591 interp_name = strdup(s);
592 interp_dir = strdup(s);
593 tmp = strrchr(interp_dir, '/');
598 interp_dir = interp_name;
606 for (cur = lib_list; cur; cur=cur->next) {
607 /* Check if this library is already in the list */
608 if(strcmp(cur->name, tmp1)==0) {
609 //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
612 if (newlib->path != not_found) {
621 newlib = malloc(sizeof(struct library));
624 newlib->name = malloc(strlen(s)+1);
625 strcpy(newlib->name, s);
626 newlib->path = strdup(newlib->name);
627 newlib->resolved = 1;
631 //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
635 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
639 interpreter_already_found = 1;
645 /* map the .so, and locate interesting pieces */
646 int find_dependancies(char* filename)
650 struct library *interp;
652 ElfW(Ehdr) *ehdr = NULL;
653 ElfW(Shdr) *dynsec = NULL;
654 ElfW(Dyn) *dynamic = NULL;
656 if (filename == not_found)
660 fprintf(stderr, "No filename specified.\n");
663 if (!(thefile = fopen(filename, "r"))) {
667 if (fstat(fileno(thefile), &statbuf) < 0) {
673 if ((size_t)statbuf.st_size < sizeof(ElfW(Ehdr)))
676 if (!S_ISREG(statbuf.st_mode))
679 /* mmap the file to make reading stuff from it effortless */
680 ehdr = (ElfW(Ehdr) *)mmap(0, statbuf.st_size,
681 PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
682 if (ehdr == MAP_FAILED) {
684 fprintf(stderr, "Out of memory!\n");
691 /* Check if this looks like a legit ELF file */
692 if (check_elf_header(ehdr)) {
693 fprintf(stderr, "%s: not an ELF file.\n", filename);
696 /* Check if this is the right kind of ELF file */
697 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
698 fprintf(stderr, "%s: not a dynamic executable\n", filename);
701 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
702 if (statbuf.st_mode & S_ISUID)
704 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
708 fprintf(stderr, "%s: is setuid\n", filename);
711 interpreter_already_found = 0;
712 interp = find_elf_interpreter(ehdr);
714 #ifdef __LDSO_LDD_SUPPORT__
715 if (interp && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) && ehdr->e_ident[EI_CLASS] == ELFCLASSM &&
716 ehdr->e_ident[EI_DATA] == ELFDATAM
717 && ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
720 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
723 static const char * const environment[] = {
724 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
726 "LD_TRACE_LOADED_OBJECTS=1",
730 if ((pid = fork()) == 0) {
731 /* Cool, it looks like we should be able to actually
732 * run this puppy. Do so now... */
733 execle(filename, filename, NULL, environment);
737 /* Wait till it returns */
738 waitpid(pid, &status, 0);
739 if (WIFEXITED(status) && WEXITSTATUS(status)==0) {
743 /* If the exec failed, we fall through to trying to find
744 * all the needed libraries ourselves by rummaging about
745 * in the ELF headers... */
750 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
752 dynamic = (ElfW(Dyn)*)(byteswap_to_host(dynsec->sh_offset) + (char *)ehdr);
753 find_needed_libraries(ehdr, dynamic, is_suid);
759 int main( int argc, char** argv)
763 char *filename = NULL;
767 fprintf(stderr, "ldd: missing file arguments\n");
768 fprintf(stderr, "Try `ldd --help' for more information.\n");
778 if(strcmp(*argv, "--")==0) {
783 if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) {
784 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
785 fprintf(stderr, "\t--help\t\tprint this help and exit\n");
791 fprintf(stderr, "No filename specified.\n");
796 printf("%s:\n", *argv);
801 if (find_dependancies(filename)!=0)
806 /* Keep walking the list till everybody is resolved */
807 for (cur = lib_list; cur; cur=cur->next) {
808 if (cur->resolved == 0 && cur->path) {
810 //printf("checking sub-depends for '%s\n", cur->path);
811 find_dependancies(cur->path);
821 for (cur = lib_list; cur; cur=cur->next) {
823 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
825 if (interp_name && interpreter_already_found==1)
826 printf("\t%s => %s (0x00000000)\n", interp_name, interp_name);
828 printf("\tnot a dynamic executable\n");
830 for (cur = lib_list; cur; cur=cur->next) {
833 if (cur->path && cur->path != not_found) {