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
38 #include <sys/types.h>
39 #include <sys/types.h>
54 #if defined(__arm__) || defined(__thumb__)
55 #define MATCH_MACHINE(x) (x == EM_ARM)
56 #define ELFCLASSM ELFCLASS32
60 #define MATCH_MACHINE(x) (x == EM_S390)
61 #define ELFCLASSM ELFCLASS32
66 #define MATCH_MACHINE(x) (x == EM_386)
68 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
70 #define ELFCLASSM ELFCLASS32
73 #if defined(__mc68000__)
74 #define MATCH_MACHINE(x) (x == EM_68K)
75 #define ELFCLASSM ELFCLASS32
79 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
80 #define ELFCLASSM ELFCLASS32
83 #if defined(__powerpc__)
84 #define MATCH_MACHINE(x) (x == EM_PPC)
85 #define ELFCLASSM ELFCLASS32
89 #define MATCH_MACHINE(x) (x == EM_SH)
90 #define ELFCLASSM ELFCLASS32
93 #if defined (__v850e__)
94 #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
95 #define ELFCLASSM ELFCLASS32
98 #if defined (__sparc__)
99 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
100 #define ELFCLASSM ELFCLASS32
103 #if defined(__cris__)
104 #define MATCH_MACHINE(x) (x == EM_CRIS)
105 #define ELFCLASSM ELFCLASS32
108 #ifndef MATCH_MACHINE
109 #warning "You really should add a MATCH_MACHINE() macro for your architecture"
112 #if __BYTE_ORDER == __LITTLE_ENDIAN
113 #define ELFDATAM ELFDATA2LSB
114 #elif __BYTE_ORDER == __BIG_ENDIAN
115 #define ELFDATAM ELFDATA2MSB
122 struct library *next;
124 struct library *lib_list = NULL;
125 char not_found[] = "not found";
127 char *interp_dir = NULL;
129 static int interpreter_already_found=0;
131 inline uint32_t byteswap32_to_host(uint32_t value)
134 return(bswap_32(value));
140 Elf32_Shdr * elf_find_section_type( int key, Elf32_Ehdr *ehdr)
144 shdr = (Elf32_Shdr *)(ehdr->e_shoff + (char *)ehdr);
145 for (j = ehdr->e_shnum; --j>=0; ++shdr) {
146 if (key==(int)byteswap32_to_host(shdr->sh_type)) {
153 Elf32_Phdr * elf_find_phdr_type( int type, Elf32_Ehdr *ehdr)
156 Elf32_Phdr *phdr = (Elf32_Phdr *)(ehdr->e_phoff + (char *)ehdr);
157 for (j = ehdr->e_phnum; --j>=0; ++phdr) {
158 if (type==(int)byteswap32_to_host(phdr->p_type)) {
165 /* Returns value if return_val==1, ptr otherwise */
166 void * elf_find_dynamic(int const key, Elf32_Dyn *dynp,
167 Elf32_Ehdr *ehdr, int return_val)
169 Elf32_Phdr *pt_text = elf_find_phdr_type(PT_LOAD, ehdr);
170 unsigned tx_reloc = byteswap32_to_host(pt_text->p_vaddr) - byteswap32_to_host(pt_text->p_offset);
171 for (; DT_NULL!=byteswap32_to_host(dynp->d_tag); ++dynp) {
172 if (key == (int)byteswap32_to_host(dynp->d_tag)) {
174 return (void *)(intptr_t)byteswap32_to_host(dynp->d_un.d_val);
176 return (void *)(byteswap32_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr );
182 static char * elf_find_rpath(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic)
186 for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
187 if (DT_RPATH == byteswap32_to_host(dyns->d_tag)) {
189 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
190 return ((char*)strtab + byteswap32_to_host(dyns->d_un.d_val));
196 int check_elf_header(Elf32_Ehdr *const ehdr)
198 if (! ehdr || strncmp((void *)ehdr, ELFMAG, SELFMAG) != 0 ||
199 ehdr->e_ident[EI_CLASS] != ELFCLASS32 ||
200 ehdr->e_ident[EI_VERSION] != EV_CURRENT)
205 /* Check if the target endianness matches the host's endianness */
207 #if __BYTE_ORDER == __LITTLE_ENDIAN
208 if (ehdr->e_ident[5] == ELFDATA2MSB) {
209 /* Ick -- we will have to byte-swap everything */
212 #elif __BYTE_ORDER == __BIG_ENDIAN
213 if (ehdr->e_ident[5] == ELFDATA2LSB) {
214 /* Ick -- we will have to byte-swap everything */
218 #error Unknown host byte order!
221 /* Be vary lazy, and only byteswap the stuff we use */
223 ehdr->e_type=bswap_16(ehdr->e_type);
224 ehdr->e_phoff=bswap_32(ehdr->e_phoff);
225 ehdr->e_shoff=bswap_32(ehdr->e_shoff);
226 ehdr->e_phnum=bswap_16(ehdr->e_phnum);
227 ehdr->e_shnum=bswap_16(ehdr->e_shnum);
233 #ifdef __LDSO_CACHE_SUPPORT__
234 static caddr_t cache_addr = NULL;
235 static size_t cache_size = 0;
245 if (cache_addr == (caddr_t) - 1)
247 else if (cache_addr != NULL)
250 if (stat(LDSO_CACHE, &st)
251 || (fd = open(LDSO_CACHE, O_RDONLY, 0)) < 0) {
252 dprintf(2, "ldd: can't open cache '%s'\n", LDSO_CACHE);
253 cache_addr = (caddr_t) - 1; /* so we won't try again */
257 cache_size = st.st_size;
258 cache_addr = (caddr_t) mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0);
260 if (cache_addr == MAP_FAILED) {
261 dprintf(2, "ldd: can't map cache '%s'\n", LDSO_CACHE);
265 header = (header_t *) cache_addr;
267 if (cache_size < sizeof(header_t) ||
268 memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
269 || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
271 (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
272 || cache_addr[cache_size - 1] != '\0')
274 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
278 strtabsize = cache_size - sizeof(header_t) -
279 header->nlibs * sizeof(libentry_t);
280 libent = (libentry_t *) & header[1];
282 for (i = 0; i < header->nlibs; i++) {
283 if (libent[i].sooffset >= strtabsize ||
284 libent[i].liboffset >= strtabsize)
286 dprintf(2, "ldd: cache '%s' is corrupt\n", LDSO_CACHE);
294 munmap(cache_addr, cache_size);
295 cache_addr = (caddr_t) - 1;
299 int unmap_cache(void)
301 if (cache_addr == NULL || cache_addr == (caddr_t) - 1)
305 munmap(cache_addr, cache_size);
312 static inline void map_cache(void) { }
313 static inline void unmap_cache(void) { }
316 /* This function's behavior must exactly match that
317 * in uClibc/ldso/ldso/dl-elf.c */
318 static void search_for_named_library(char *name, char *result, const char *path_list)
322 struct stat filestat;
324 /* We need a writable copy of this string */
325 path = strdup(path_list);
327 fprintf(stderr, "Out of memory!\n");
330 /* Eliminate all double //s */
332 while((path_n=strstr(path_n, "//"))) {
334 memmove(path_n, path_n+1, i-1);
335 *(path_n + i - 1)='\0';
338 /* Replace colons with zeros in path_list and count them */
339 for(i=strlen(path); i > 0; i--) {
346 for (i = 0; i < count; i++) {
347 strcpy(result, path_n);
349 strcat(result, name);
350 if (stat (result, &filestat) == 0 && filestat.st_mode & S_IRUSR) {
354 path_n += (strlen(path_n) + 1);
360 void locate_library_file(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_suid, struct library *lib)
364 struct stat filestat;
366 /* If this is a fully resolved name, our job is easy */
367 if (stat (lib->name, &filestat) == 0) {
368 lib->path = strdup(lib->name);
372 /* We need some elbow room here. Make some room...*/
375 fprintf(stderr, "Out of memory!\n");
379 /* This function must match the behavior of _dl_load_shared_library
380 * in readelflib1.c or things won't work out as expected... */
382 /* The ABI specifies that RPATH is searched first, so do that now. */
383 path = elf_find_rpath(ehdr, dynamic);
385 search_for_named_library(lib->name, buf, path);
392 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
393 * Since this app doesn't actually run an executable I will skip
394 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
398 path = getenv("LD_LIBRARY_PATH");
400 search_for_named_library(lib->name, buf, path);
407 #ifdef __LDSO_CACHE_SUPPORT__
408 if (cache_addr != NULL && cache_addr != (caddr_t) - 1) {
410 header_t *header = (header_t *) cache_addr;
411 libentry_t *libent = (libentry_t *) & header[1];
412 char *strs = (char *) &libent[header->nlibs];
414 for (i = 0; i < header->nlibs; i++) {
415 if ((libent[i].flags == LIB_ELF ||
416 libent[i].flags == LIB_ELF_LIBC0 ||
417 libent[i].flags == LIB_ELF_LIBC5) &&
418 strcmp(lib->name, strs + libent[i].sooffset) == 0) {
419 lib->path = strdup(strs + libent[i].liboffset);
427 /* Next look for libraries wherever the shared library
428 * loader was installed -- this is usually where we
429 * should find things... */
431 search_for_named_library(lib->name, buf, interp_dir);
438 /* Lastly, search the standard list of paths for the library.
439 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
440 path = UCLIBC_RUNTIME_PREFIX "lib:"
441 UCLIBC_RUNTIME_PREFIX "usr/lib"
442 #ifndef __LDSO_CACHE_SUPPORT__
443 ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
446 search_for_named_library(lib->name, buf, path);
451 lib->path = not_found;
455 static int add_library(Elf32_Ehdr* ehdr, Elf32_Dyn* dynamic, int is_setuid, char *s)
457 char *tmp, *tmp1, *tmp2;
458 struct library *cur, *newlib=lib_list;
460 if (!s || !strlen(s))
470 /* We add ldso elsewhere */
471 if (interpreter_already_found && (tmp=strrchr(interp, '/')) != NULL)
473 int len = strlen(interp_dir);
474 if (strcmp(s, interp+1+len)==0)
478 for (cur = lib_list; cur; cur=cur->next) {
479 /* Check if this library is already in the list */
480 tmp1 = tmp2 = cur->name;
486 if(strcmp(tmp2, s)==0) {
487 //printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name);
492 /* Ok, this lib needs to be added to the list */
493 newlib = malloc(sizeof(struct library));
496 newlib->name = malloc(strlen(s)+1);
497 strcpy(newlib->name, s);
498 newlib->resolved = 0;
502 /* Now try and locate where this library might be living... */
503 locate_library_file(ehdr, dynamic, is_setuid, newlib);
505 //printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path);
509 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
515 static void find_needed_libraries(Elf32_Ehdr* ehdr,
516 Elf32_Dyn* dynamic, int is_setuid)
520 for (dyns=dynamic; byteswap32_to_host(dyns->d_tag)!=DT_NULL; ++dyns) {
521 if (DT_NEEDED == byteswap32_to_host(dyns->d_tag)) {
523 strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0);
524 add_library(ehdr, dynamic, is_setuid,
525 (char*)strtab + byteswap32_to_host(dyns->d_un.d_val));
530 static struct library * find_elf_interpreter(Elf32_Ehdr* ehdr)
534 if (interpreter_already_found==1)
536 phdr = elf_find_phdr_type(PT_INTERP, ehdr);
538 struct library *cur, *newlib=NULL;
539 char *s = (char*)ehdr + byteswap32_to_host(phdr->p_offset);
543 interp_dir = strdup(s);
544 tmp = strrchr(interp_dir, '/');
557 for (cur = lib_list; cur; cur=cur->next) {
558 /* Check if this library is already in the list */
559 if(strcmp(cur->name, tmp1)==0) {
560 //printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name);
563 if (newlib->path != not_found) {
572 newlib = malloc(sizeof(struct library));
575 newlib->name = malloc(strlen(s)+1);
576 strcpy(newlib->name, s);
577 newlib->path = strdup(newlib->name);
578 newlib->resolved = 1;
582 //printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path);
586 for (cur = lib_list; cur->next; cur=cur->next); /* nothing */
590 interpreter_already_found=1;
596 /* map the .so, and locate interesting pieces */
597 int find_dependancies(char* filename)
602 Elf32_Ehdr *ehdr = NULL;
603 Elf32_Shdr *dynsec = NULL;
604 Elf32_Dyn *dynamic = NULL;
605 struct library *interp;
607 if (filename == not_found)
611 fprintf(stderr, "No filename specified.\n");
614 if (!(thefile = fopen(filename, "r"))) {
618 if (fstat(fileno(thefile), &statbuf) < 0) {
624 if ((size_t)statbuf.st_size < sizeof(Elf32_Ehdr))
627 if (!S_ISREG(statbuf.st_mode))
630 /* mmap the file to make reading stuff from it effortless */
631 ehdr = (Elf32_Ehdr *)mmap(0, statbuf.st_size,
632 PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0);
633 if (ehdr == MAP_FAILED) {
635 fprintf(stderr, "Out of memory!\n");
642 /* Check if this looks like a legit ELF file */
643 if (check_elf_header(ehdr)) {
644 fprintf(stderr, "%s: not an ELF file.\n", filename);
647 /* Check if this is the right kind of ELF file */
648 if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) {
649 fprintf(stderr, "%s: not a dynamic executable\n", filename);
652 if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) {
653 if (statbuf.st_mode & S_ISUID)
655 if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
659 fprintf(stderr, "%s: is setuid\n", filename);
662 interpreter_already_found=0;
663 interp = find_elf_interpreter(ehdr);
665 #ifdef __LDSO_LDD_SUPPORT__
666 if (interp && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) && ehdr->e_ident[EI_CLASS] == ELFCLASSM &&
667 ehdr->e_ident[EI_DATA] == ELFDATAM
668 && ehdr->e_ident[EI_VERSION] == EV_CURRENT && MATCH_MACHINE(ehdr->e_machine))
671 if (stat(interp->path, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) {
674 static const char * const environment[] = {
675 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
677 "LD_TRACE_LOADED_OBJECTS=1",
681 if ((pid = fork()) == 0) {
682 /* Cool, it looks like we should be able to actually
683 * run this puppy. Do so now... */
684 execle(filename, filename, NULL, environment);
688 /* Wait till it returns */
689 waitpid(pid, &status, 0);
690 if (WIFEXITED(status) && WEXITSTATUS(status)==0) {
694 /* If the exec failed, we fall through to trying to find
695 * all the needed libraries ourselves by rummaging about
696 * in the ELF headers... */
701 dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr);
703 dynamic = (Elf32_Dyn*)(byteswap32_to_host(dynsec->sh_offset) + (intptr_t)ehdr);
704 find_needed_libraries(ehdr, dynamic, is_suid);
710 int main( int argc, char** argv)
714 char *filename = NULL;
718 fprintf(stderr, "ldd: missing file arguments\n");
719 fprintf(stderr, "Try `ldd --help' for more information.\n");
729 if(strcmp(*argv, "--")==0) {
734 if(strcmp(*argv, "--help")==0) {
735 fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n");
736 fprintf(stderr, "\t--help\t\tprint this help and exit\n");
742 fprintf(stderr, "No filename specified.\n");
747 printf("%s:\n", *argv);
752 if (find_dependancies(filename)!=0)
757 /* Keep walking the list till everybody is resolved */
758 for (cur = lib_list; cur; cur=cur->next) {
759 if (cur->resolved == 0 && cur->path) {
761 //printf("checking sub-depends for '%s\n", cur->path);
762 find_dependancies(cur->path);
772 for (cur = lib_list; cur; cur=cur->next) {
774 printf("\t%s => %s (0x00000000)\n", cur->name, cur->path);
776 if (interp && interpreter_already_found==1)
777 printf("\t%s => %s (0x00000000)\n", interp, interp);
779 printf("\tnot a dynamic executable\n");
781 for (cur = lib_list; cur; cur=cur->next) {
784 if (cur->path && cur->path != not_found) {