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
18 #if defined(__alpha__)
19 #define MATCH_MACHINE(x) (x == EM_ALPHA)
20 #define ELFCLASSM ELFCLASS64
24 #define MATCH_MACHINE(x) (x == EM_ARCOMPACT)
25 #define ELFCLASSM ELFCLASS32
28 #if defined(__arm__) || defined(__thumb__)
29 #define MATCH_MACHINE(x) (x == EM_ARM)
30 #define ELFCLASSM ELFCLASS32
33 #if defined(__avr32__)
34 #define MATCH_MACHINE(x) (x == EM_AVR32)
35 #define ELFCLASSM ELFCLASS32
39 #define MATCH_MACHINE(x) (x == EM_S390)
40 #define ELFCLASSM ELFCLASS32
44 #define MATCH_MACHINE(x) (x == EM_PARISC)
46 #define ELFCLASSM ELFCLASS64
48 #define ELFCLASSM ELFCLASS32
54 #define MATCH_MACHINE(x) (x == EM_386)
56 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
58 #define ELFCLASSM ELFCLASS32
62 #define MATCH_MACHINE(x) (x == EM_IA_64)
63 #define ELFCLASSM ELFCLASS64
66 #if defined(__mc68000__)
67 #define MATCH_MACHINE(x) (x == EM_68K)
68 #define ELFCLASSM ELFCLASS32
71 #if defined(__metag__)
72 #define MATCH_MACHINE(x) (x == EM_METAG)
73 #define ELFCLASSM ELFCLASS32
77 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
78 #define ELFCLASSM ELFCLASS32
81 #if defined(__powerpc64__)
82 #define MATCH_MACHINE(x) (x == EM_PPC64)
83 #define ELFCLASSM ELFCLASS64
84 #elif defined(__powerpc__)
85 #define MATCH_MACHINE(x) (x == EM_PPC)
86 #define ELFCLASSM ELFCLASS32
90 #define MATCH_MACHINE(x) (x == EM_SH)
91 #define ELFCLASSM ELFCLASS32
94 #if defined(__sparc__)
95 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
96 #define ELFCLASSM ELFCLASS32
100 #define MATCH_MACHINE(x) (x == EM_CRIS)
101 #define ELFCLASSM ELFCLASS32
104 #if defined(__x86_64__)
105 #define MATCH_MACHINE(x) (x == EM_X86_64)
106 #define ELFCLASSM ELFCLASS64
109 #if defined(__microblaze__)
110 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE_OLD)
111 #define ELFCLASSM ELFCLASS32
114 #if defined(__xtensa__)
115 #define MATCH_MACHINE(x) (x == EM_XTENSA)
116 #define ELFCLASSM ELFCLASS32
119 #ifndef MATCH_MACHINE
121 # include <asm/elf.h>
124 # define MATCH_MACHINE(x) (x == ELF_ARCH)
127 # define ELFCLASSM ELF_CLASS
130 #ifndef MATCH_MACHINE
131 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
134 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
135 #define ELFDATAM ELFDATA2LSB
136 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
137 #define ELFDATAM ELFDATA2MSB
140 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
146 struct library
*next
;
148 static struct library
*lib_list
= NULL
;
149 static char not_found
[] = "not found";
150 static char *interp_name
= NULL
;
151 static char *interp_dir
= NULL
;
153 static int interpreter_already_found
= 0;
155 static __inline__
uint32_t byteswap32_to_host(uint32_t value
)
158 return (bswap_32(value
));
163 static __inline__
uint64_t byteswap64_to_host(uint64_t value
)
166 return (bswap_64(value
));
173 # define byteswap_to_host(x) byteswap64_to_host(x)
175 # define byteswap_to_host(x) byteswap32_to_host(x)
178 static ElfW(Shdr
) *elf_find_section_type(uint32_t key
, ElfW(Ehdr
) *ehdr
)
182 shdr
= (ElfW(Shdr
) *) (ehdr
->e_shoff
+ (char *)ehdr
);
183 for (j
= ehdr
->e_shnum
; --j
>= 0; ++shdr
) {
184 if (key
== byteswap32_to_host(shdr
->sh_type
)) {
191 static ElfW(Phdr
) *elf_find_phdr_type(uint32_t type
, ElfW(Ehdr
) *ehdr
)
194 ElfW(Phdr
) *phdr
= (ElfW(Phdr
) *) (ehdr
->e_phoff
+ (char *)ehdr
);
195 for (j
= ehdr
->e_phnum
; --j
>= 0; ++phdr
) {
196 if (type
== byteswap32_to_host(phdr
->p_type
)) {
203 /* Returns value if return_val==1, ptr otherwise */
204 static void *elf_find_dynamic(int64_t const key
, ElfW(Dyn
) *dynp
,
205 ElfW(Ehdr
) *ehdr
, int return_val
)
207 ElfW(Phdr
) *pt_text
= elf_find_phdr_type(PT_LOAD
, ehdr
);
208 unsigned tx_reloc
= byteswap_to_host(pt_text
->p_vaddr
) - byteswap_to_host(pt_text
->p_offset
);
209 for (; DT_NULL
!= byteswap_to_host(dynp
->d_tag
); ++dynp
) {
210 if (key
== byteswap_to_host(dynp
->d_tag
)) {
212 return (void *)byteswap_to_host(dynp
->d_un
.d_val
);
214 return (void *)(byteswap_to_host(dynp
->d_un
.d_val
) - tx_reloc
+ (char *)ehdr
);
220 static char *elf_find_rpath(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
)
224 for (dyns
= dynamic
; byteswap_to_host(dyns
->d_tag
) != DT_NULL
; ++dyns
) {
225 if (DT_RPATH
== byteswap_to_host(dyns
->d_tag
)) {
227 strtab
= (char *)elf_find_dynamic(DT_STRTAB
, dynamic
, ehdr
, 0);
228 return ((char *)strtab
+ byteswap_to_host(dyns
->d_un
.d_val
));
234 static int check_elf_header(ElfW(Ehdr
) *const ehdr
)
236 if (!ehdr
|| *(uint32_t*)ehdr
!= ELFMAG_U32
237 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
238 || ehdr
->e_ident
[EI_CLASS
] != (__WORDSIZE
>> 5)
239 || ehdr
->e_ident
[EI_VERSION
] != EV_CURRENT
244 /* Check if the target endianness matches the host's endianness */
245 byteswap
= !(ehdr
->e_ident
[5] == ELFDATAM
);
247 /* Be very lazy, and only byteswap the stuff we use */
249 ehdr
->e_type
= bswap_16(ehdr
->e_type
);
250 ehdr
->e_phoff
= byteswap_to_host(ehdr
->e_phoff
);
251 ehdr
->e_shoff
= byteswap_to_host(ehdr
->e_shoff
);
252 ehdr
->e_phnum
= bswap_16(ehdr
->e_phnum
);
253 ehdr
->e_shnum
= bswap_16(ehdr
->e_shnum
);
259 #ifdef __LDSO_CACHE_SUPPORT__
260 static caddr_t cache_addr
= NULL
;
261 static size_t cache_size
= 0;
263 static int map_cache(void)
271 if (cache_addr
== (caddr_t
) - 1)
273 else if (cache_addr
!= NULL
)
276 if (stat(LDSO_CACHE
, &st
) || (fd
= open(LDSO_CACHE
, O_RDONLY
)) < 0) {
277 fprintf(stderr
, "ldd: can't open cache '%s'\n", LDSO_CACHE
);
278 cache_addr
= (caddr_t
) - 1; /* so we won't try again */
282 cache_size
= st
.st_size
;
283 cache_addr
= mmap(0, cache_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
285 if (cache_addr
== MAP_FAILED
) {
286 fprintf(stderr
, "ldd: can't map cache '%s'\n", LDSO_CACHE
);
290 header
= (header_t
*) cache_addr
;
292 if (cache_size
< sizeof(header_t
)
293 || memcmp(header
->magic
, LDSO_CACHE_MAGIC
, LDSO_CACHE_MAGIC_LEN
)
294 || memcmp(header
->version
, LDSO_CACHE_VER
, LDSO_CACHE_VER_LEN
)
295 || cache_size
< (sizeof(header_t
) + header
->nlibs
* sizeof(libentry_t
))
296 || cache_addr
[cache_size
- 1] != '\0')
298 fprintf(stderr
, "ldd: cache '%s' is corrupt\n", LDSO_CACHE
);
302 strtabsize
= cache_size
- sizeof(header_t
) - header
->nlibs
* sizeof(libentry_t
);
303 libent
= (libentry_t
*) & header
[1];
305 for (i
= 0; i
< header
->nlibs
; i
++) {
306 if (libent
[i
].sooffset
>= strtabsize
|| libent
[i
].liboffset
>= strtabsize
) {
307 fprintf(stderr
, "ldd: cache '%s' is corrupt\n", LDSO_CACHE
);
315 munmap(cache_addr
, cache_size
);
316 cache_addr
= (caddr_t
) - 1;
320 static int unmap_cache(void)
322 if (cache_addr
== NULL
|| cache_addr
== (caddr_t
) - 1)
326 munmap(cache_addr
, cache_size
);
333 static __inline__
void map_cache(void)
336 static __inline__
void unmap_cache(void)
341 /* This function's behavior must exactly match that
342 * in uClibc/ldso/ldso/dl-elf.c */
343 static void search_for_named_library(char *name
, char *result
,
344 const char *path_list
)
348 struct stat filestat
;
350 /* We need a writable copy of this string */
351 path
= strdup(path_list
);
353 fprintf(stderr
, "%s: Out of memory!\n", __func__
);
356 /* Eliminate all double //s */
358 while ((path_n
= strstr(path_n
, "//"))) {
360 memmove(path_n
, path_n
+ 1, i
- 1);
361 *(path_n
+ i
- 1) = '\0';
364 /* Replace colons with zeros in path_list and count them */
365 for (i
= strlen(path
); i
> 0; i
--) {
366 if (path
[i
] == ':') {
372 for (i
= 0; i
< count
; i
++) {
373 strcpy(result
, path_n
);
375 strcat(result
, name
);
376 if (stat(result
, &filestat
) == 0 && filestat
.st_mode
& S_IRUSR
) {
380 path_n
+= (strlen(path_n
) + 1);
386 static void locate_library_file(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
,
387 int is_suid
, struct library
*lib
)
391 struct stat filestat
;
393 /* If this is a fully resolved name, our job is easy */
394 if (stat(lib
->name
, &filestat
) == 0) {
395 lib
->path
= strdup(lib
->name
);
399 /* We need some elbow room here. Make some room... */
402 fprintf(stderr
, "%s: Out of memory!\n", __func__
);
406 /* This function must match the behavior of _dl_load_shared_library
407 * in readelflib1.c or things won't work out as expected... */
409 /* The ABI specifies that RPATH is searched first, so do that now. */
410 path
= elf_find_rpath(ehdr
, dynamic
);
412 search_for_named_library(lib
->name
, buf
, path
);
419 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
420 * Since this app doesn't actually run an executable I will skip
421 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
425 path
= getenv("LD_LIBRARY_PATH");
427 search_for_named_library(lib
->name
, buf
, path
);
433 #ifdef __LDSO_CACHE_SUPPORT__
434 if (cache_addr
!= NULL
&& cache_addr
!= (caddr_t
) - 1) {
436 header_t
*header
= (header_t
*) cache_addr
;
437 libentry_t
*libent
= (libentry_t
*) & header
[1];
438 char *strs
= (char *)&libent
[header
->nlibs
];
440 for (i
= 0; i
< header
->nlibs
; i
++) {
441 if ((libent
[i
].flags
== LIB_ELF
||
442 libent
[i
].flags
== LIB_ELF_LIBC0
||
443 libent
[i
].flags
== LIB_ELF_LIBC5
) &&
444 strcmp(lib
->name
, strs
+ libent
[i
].sooffset
) == 0)
446 lib
->path
= strdup(strs
+ libent
[i
].liboffset
);
453 /* Next look for libraries wherever the shared library
454 * loader was installed -- this is usually where we
455 * should find things... */
457 search_for_named_library(lib
->name
, buf
, interp_dir
);
464 /* Lastly, search the standard list of paths for the library.
465 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
466 path
= UCLIBC_RUNTIME_PREFIX
"lib:" UCLIBC_RUNTIME_PREFIX
"usr/lib"
467 #ifndef __LDSO_CACHE_SUPPORT__
468 ":" UCLIBC_RUNTIME_PREFIX
"usr/X11R6/lib"
471 search_for_named_library(lib
->name
, buf
, path
);
476 lib
->path
= not_found
;
480 static int add_library(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
, int is_setuid
, char *s
)
482 char *tmp
, *tmp1
, *tmp2
;
483 struct library
*cur
, *newlib
= lib_list
;
485 if (!s
|| !strlen(s
))
495 /* We add ldso elsewhere */
496 if (interpreter_already_found
&& (tmp
= strrchr(interp_name
, '/')) != NULL
) {
497 int len
= strlen(interp_dir
);
498 if (strcmp(s
, interp_name
+ 1 + len
) == 0)
502 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
503 /* Check if this library is already in the list */
504 tmp1
= tmp2
= cur
->name
;
512 if (strcmp(tmp2
, s
) == 0) {
513 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
518 /* Ok, this lib needs to be added to the list */
519 newlib
= malloc(sizeof(struct library
));
522 newlib
->name
= malloc(strlen(s
) + 1);
523 strcpy(newlib
->name
, s
);
524 newlib
->resolved
= 0;
528 /* Now try and locate where this library might be living... */
529 locate_library_file(ehdr
, dynamic
, is_setuid
, newlib
);
531 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
535 for (cur
= lib_list
; cur
->next
; cur
= cur
->next
) ; /* nothing */
541 static void find_needed_libraries(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
, int is_setuid
)
545 for (dyns
= dynamic
; byteswap_to_host(dyns
->d_tag
) != DT_NULL
; ++dyns
) {
546 if (DT_NEEDED
== byteswap_to_host(dyns
->d_tag
)) {
548 strtab
= (char *)elf_find_dynamic(DT_STRTAB
, dynamic
, ehdr
, 0);
549 add_library(ehdr
, dynamic
, is_setuid
, (char *)strtab
+ byteswap_to_host(dyns
->d_un
.d_val
));
554 #ifdef __LDSO_LDD_SUPPORT__
555 static struct library
*find_elf_interpreter(ElfW(Ehdr
) *ehdr
)
559 if (interpreter_already_found
== 1)
561 phdr
= elf_find_phdr_type(PT_INTERP
, ehdr
);
563 struct library
*cur
, *newlib
= NULL
;
564 char *s
= (char *)ehdr
+ byteswap_to_host(phdr
->p_offset
);
567 interp_name
= strdup(s
);
568 interp_dir
= strdup(s
);
569 tmp
= strrchr(interp_dir
, '/');
574 interp_dir
= interp_name
;
582 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
583 /* Check if this library is already in the list */
584 if (!tmp1
|| !cur
->name
)
586 if (strcmp(cur
->name
, tmp1
) == 0) {
587 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
590 if (newlib
->path
!= not_found
) {
599 newlib
= malloc(sizeof(struct library
));
602 newlib
->name
= malloc(strlen(s
) + 1);
603 strcpy(newlib
->name
, s
);
604 newlib
->path
= strdup(newlib
->name
);
605 newlib
->resolved
= 1;
609 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
613 for (cur
= lib_list
; cur
->next
; cur
= cur
->next
) ; /* nothing */
617 interpreter_already_found
= 1;
622 #endif /* __LDSO_LDD_SUPPORT__ */
624 /* map the .so, and locate interesting pieces */
626 #warning "There may be two warnings here about vfork() clobbering, ignore them"
628 static int find_dependencies(char *filename
)
633 ElfW(Ehdr
) *ehdr
= NULL
;
634 ElfW(Shdr
) *dynsec
= NULL
;
635 ElfW(Dyn
) *dynamic
= NULL
;
636 #ifdef __LDSO_LDD_SUPPORT__
637 struct library
*interp
;
640 if (filename
== not_found
)
644 fprintf(stderr
, "No filename specified.\n");
647 if (!(thefile
= fopen(filename
, "r"))) {
651 if (fstat(fileno(thefile
), &statbuf
) < 0) {
657 if ((size_t) statbuf
.st_size
< sizeof(ElfW(Ehdr
)))
660 if (!S_ISREG(statbuf
.st_mode
))
663 /* mmap the file to make reading stuff from it effortless */
664 ehdr
= mmap(0, statbuf
.st_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fileno(thefile
), 0);
665 if (ehdr
== MAP_FAILED
) {
667 fprintf(stderr
, "mmap(%s) failed: %s\n", filename
, strerror(errno
));
674 /* Check if this looks like a legit ELF file */
675 if (check_elf_header(ehdr
)) {
676 fprintf(stderr
, "%s: not an ELF file.\n", filename
);
679 /* Check if this is the right kind of ELF file */
680 if (ehdr
->e_type
!= ET_EXEC
&& ehdr
->e_type
!= ET_DYN
) {
681 fprintf(stderr
, "%s: not a dynamic executable\n", filename
);
684 if (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
) {
685 if (statbuf
.st_mode
& S_ISUID
)
687 if ((statbuf
.st_mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
))
691 fprintf(stderr
, "%s: is setuid\n", filename
);
694 interpreter_already_found
= 0;
695 #ifdef __LDSO_LDD_SUPPORT__
696 interp
= find_elf_interpreter(ehdr
);
699 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
)
700 && ehdr
->e_ident
[EI_CLASS
] == ELFCLASSM
701 && ehdr
->e_ident
[EI_DATA
] == ELFDATAM
702 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
703 && MATCH_MACHINE(ehdr
->e_machine
))
706 if (stat(interp
->path
, &st
) == 0 && S_ISREG(st
.st_mode
)) {
709 static const char *const environment
[] = {
710 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
712 "LD_TRACE_LOADED_OBJECTS=1",
715 # ifdef __LDSO_STANDALONE_SUPPORT__
716 char * lib_path
= getenv("LD_LIBRARY_PATH");
718 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
719 static char *ext_environment
[ARRAY_SIZE(environment
) + 1];
720 char **envp
= (char **) environment
;
724 * If the LD_LIBRARY_PATH is set, it needs to include it
725 * into the environment for the new process to be spawned
727 char ** eenvp
= (char **) ext_environment
;
729 /* Copy the N-1 environment's entries */
733 /* Make room for LD_LIBRARY_PATH */
734 *eenvp
= (char *) malloc(sizeof("LD_LIBRARY_PATH=")
736 strcpy(*eenvp
, "LD_LIBRARY_PATH=");
737 strcat(*eenvp
, lib_path
);
739 /* ext_environment[size] is already NULL */
741 /* Use the extended environment */
742 envp
= ext_environment
;
744 if ((pid
= vfork()) == 0) {
746 * Force to use the standard dynamic linker in stand-alone mode.
747 * It will fails at runtime if support is not actually available
749 execle(TRUSTED_LDSO
, TRUSTED_LDSO
, filename
, NULL
, envp
);
753 if ((pid
= vfork()) == 0) {
754 /* Cool, it looks like we should be able to actually
755 * run this puppy. Do so now... */
756 execle(filename
, filename
, NULL
, environment
);
760 /* Wait till it returns */
761 waitpid(pid
, &status
, 0);
763 # ifdef __LDSO_STANDALONE_SUPPORT__
768 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
772 /* If the exec failed, we fall through to trying to find
773 * all the needed libraries ourselves by rummaging about
774 * in the ELF headers... */
779 dynsec
= elf_find_section_type(SHT_DYNAMIC
, ehdr
);
781 dynamic
= (ElfW(Dyn
) *) (byteswap_to_host(dynsec
->sh_offset
) + (char *)ehdr
);
782 find_needed_libraries(ehdr
, dynamic
, is_suid
);
788 int main(int argc
, char **argv
)
792 char *filename
= NULL
;
796 fprintf(stderr
, "ldd: missing file arguments\n"
797 "Try `ldd --help' for more information.\n");
806 if (strcmp(*argv
, "--") == 0) {
811 if (strcmp(*argv
, "--help") == 0 || strcmp(*argv
, "-h") == 0) {
812 fprintf(stderr
, "Usage: ldd [OPTION]... FILE...\n"
813 "\t--help\t\tprint this help and exit\n");
819 fprintf(stderr
, "No filename specified.\n");
824 printf("%s:\n", *argv
);
829 if (find_dependencies(filename
) != 0)
834 /* Keep walking the list till everybody is resolved */
835 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
836 if (cur
->resolved
== 0 && cur
->path
) {
838 printf("checking sub-depends for '%s'\n", cur
->path
);
839 find_dependencies(cur
->path
);
849 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
851 printf("\t%s => %s (0x00000000)\n", cur
->name
, cur
->path
);
853 if (interp_name
&& interpreter_already_found
== 1)
854 printf("\t%s => %s (0x00000000)\n", interp_name
, interp_name
);
856 printf("\tnot a dynamic executable\n");
858 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
861 if (cur
->path
&& cur
->path
!= not_found
) {