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_BLACKFIN)
40 #define ELFCLASSM ELFCLASS32
43 #if defined(__TMS320C6X__)
44 #define MATCH_MACHINE(x) (x == EM_TI_C6000)
45 #define ELFCLASSM ELFCLASS32
49 #define MATCH_MACHINE(x) (x == EM_CRIS)
50 #define ELFCLASSM ELFCLASS32
54 #define MATCH_MACHINE(x) (x == EM_CYGNUS_FRV)
55 #define ELFCLASSM ELFCLASS32
59 #define MATCH_MACHINE(x) (x == EM_PARISC)
61 #define ELFCLASSM ELFCLASS64
63 #define ELFCLASSM ELFCLASS32
69 #define MATCH_MACHINE(x) (x == EM_386)
71 #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
73 #define ELFCLASSM ELFCLASS32
77 #define MATCH_MACHINE(x) (x == EM_IA_64)
78 #define ELFCLASSM ELFCLASS64
81 #if defined(__mc68000__)
82 #define MATCH_MACHINE(x) (x == EM_68K)
83 #define ELFCLASSM ELFCLASS32
86 #if defined(__metag__)
87 #define MATCH_MACHINE(x) (x == EM_METAG)
88 #define ELFCLASSM ELFCLASS32
91 #if defined(__microblaze__)
92 #define MATCH_MACHINE(x) (x == EM_MICROBLAZE)
93 #define ELFCLASSM ELFCLASS32
97 #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
98 #define ELFCLASSM ELFCLASS32
101 #if defined(__nds32__)
102 #define MATCH_MACHINE(x) (x == EM_NDS32)
103 #define ELFCLASSM ELFCLASS32
106 #if defined(__nios2__)
107 #define MATCH_MACHINE(x) (x == EM_NIOS32)
108 #define ELFCLASSM ELFCLASS32
111 #if defined(__powerpc__)
112 #define MATCH_MACHINE(x) (x == EM_PPC)
113 #define ELFCLASSM ELFCLASS32
117 #define MATCH_MACHINE(x) (x == EM_SH)
118 #define ELFCLASSM ELFCLASS32
121 #if defined(__sparc__)
122 #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS)
123 #define ELFCLASSM ELFCLASS32
126 #if defined(__x86_64__)
127 #define MATCH_MACHINE(x) (x == EM_X86_64)
128 #define ELFCLASSM ELFCLASS64
131 #if defined(__xtensa__)
132 #define MATCH_MACHINE(x) (x == EM_XTENSA)
133 #define ELFCLASSM ELFCLASS32
136 #ifndef MATCH_MACHINE
138 # include <asm/elf.h>
141 # define MATCH_MACHINE(x) (x == ELF_ARCH)
144 # define ELFCLASSM ELF_CLASS
147 #ifndef MATCH_MACHINE
148 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
151 #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
152 #define ELFDATAM ELFDATA2LSB
153 #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
154 #define ELFDATAM ELFDATA2MSB
157 #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO
163 struct library
*next
;
165 static struct library
*lib_list
= NULL
;
166 static char not_found
[] = "not found";
167 static char *interp_name
= NULL
;
168 static char *interp_dir
= NULL
;
170 static int interpreter_already_found
= 0;
172 static __inline__
uint32_t byteswap32_to_host(uint32_t value
)
175 return (bswap_32(value
));
180 static __inline__
uint64_t byteswap64_to_host(uint64_t value
)
183 return (bswap_64(value
));
190 # define byteswap_to_host(x) byteswap64_to_host(x)
192 # define byteswap_to_host(x) byteswap32_to_host(x)
195 static ElfW(Shdr
) *elf_find_section_type(uint32_t key
, ElfW(Ehdr
) *ehdr
)
199 shdr
= (ElfW(Shdr
) *) (ehdr
->e_shoff
+ (char *)ehdr
);
200 for (j
= ehdr
->e_shnum
; --j
>= 0; ++shdr
) {
201 if (key
== byteswap32_to_host(shdr
->sh_type
)) {
208 static ElfW(Phdr
) *elf_find_phdr_type(uint32_t type
, ElfW(Ehdr
) *ehdr
)
211 ElfW(Phdr
) *phdr
= (ElfW(Phdr
) *) (ehdr
->e_phoff
+ (char *)ehdr
);
212 for (j
= ehdr
->e_phnum
; --j
>= 0; ++phdr
) {
213 if (type
== byteswap32_to_host(phdr
->p_type
)) {
220 /* Returns value if return_val==1, ptr otherwise */
221 static void *elf_find_dynamic(int64_t const key
, ElfW(Dyn
) *dynp
,
222 ElfW(Ehdr
) *ehdr
, int return_val
)
224 ElfW(Phdr
) *pt_text
= elf_find_phdr_type(PT_LOAD
, ehdr
);
225 unsigned tx_reloc
= byteswap_to_host(pt_text
->p_vaddr
) - byteswap_to_host(pt_text
->p_offset
);
226 for (; DT_NULL
!= byteswap_to_host(dynp
->d_tag
); ++dynp
) {
227 if (key
== byteswap_to_host(dynp
->d_tag
)) {
229 return (void *)byteswap_to_host(dynp
->d_un
.d_val
);
231 return (void *)(byteswap_to_host(dynp
->d_un
.d_val
) - tx_reloc
+ (char *)ehdr
);
237 static char *elf_find_rpath(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
)
241 for (dyns
= dynamic
; byteswap_to_host(dyns
->d_tag
) != DT_NULL
; ++dyns
) {
242 if (DT_RPATH
== byteswap_to_host(dyns
->d_tag
)) {
244 strtab
= (char *)elf_find_dynamic(DT_STRTAB
, dynamic
, ehdr
, 0);
245 return ((char *)strtab
+ byteswap_to_host(dyns
->d_un
.d_val
));
251 static int check_elf_header(ElfW(Ehdr
) *const ehdr
)
253 if (!ehdr
|| *(uint32_t*)ehdr
!= ELFMAG_U32
254 /* Use __WORDSIZE, not ELFCLASSM which depends on the host */
255 || ehdr
->e_ident
[EI_CLASS
] != (__WORDSIZE
>> 5)
256 || ehdr
->e_ident
[EI_VERSION
] != EV_CURRENT
261 /* Check if the target endianness matches the host's endianness */
262 byteswap
= !(ehdr
->e_ident
[5] == ELFDATAM
);
264 /* Be very lazy, and only byteswap the stuff we use */
266 ehdr
->e_type
= bswap_16(ehdr
->e_type
);
267 ehdr
->e_phoff
= byteswap_to_host(ehdr
->e_phoff
);
268 ehdr
->e_shoff
= byteswap_to_host(ehdr
->e_shoff
);
269 ehdr
->e_phnum
= bswap_16(ehdr
->e_phnum
);
270 ehdr
->e_shnum
= bswap_16(ehdr
->e_shnum
);
276 #ifdef __LDSO_CACHE_SUPPORT__
277 static caddr_t cache_addr
= NULL
;
278 static size_t cache_size
= 0;
280 static int map_cache(void)
288 if (cache_addr
== (caddr_t
) - 1)
290 else if (cache_addr
!= NULL
)
293 if (stat(LDSO_CACHE
, &st
) || (fd
= open(LDSO_CACHE
, O_RDONLY
)) < 0) {
294 fprintf(stderr
, "ldd: can't open cache '%s'\n", LDSO_CACHE
);
295 cache_addr
= (caddr_t
) - 1; /* so we won't try again */
299 cache_size
= st
.st_size
;
300 cache_addr
= mmap(0, cache_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
302 if (cache_addr
== MAP_FAILED
) {
303 fprintf(stderr
, "ldd: can't map cache '%s'\n", LDSO_CACHE
);
307 header
= (header_t
*) cache_addr
;
309 if (cache_size
< sizeof(header_t
)
310 || memcmp(header
->magic
, LDSO_CACHE_MAGIC
, LDSO_CACHE_MAGIC_LEN
)
311 || memcmp(header
->version
, LDSO_CACHE_VER
, LDSO_CACHE_VER_LEN
)
312 || cache_size
< (sizeof(header_t
) + header
->nlibs
* sizeof(libentry_t
))
313 || cache_addr
[cache_size
- 1] != '\0')
315 fprintf(stderr
, "ldd: cache '%s' is corrupt\n", LDSO_CACHE
);
319 strtabsize
= cache_size
- sizeof(header_t
) - header
->nlibs
* sizeof(libentry_t
);
320 libent
= (libentry_t
*) & header
[1];
322 for (i
= 0; i
< header
->nlibs
; i
++) {
323 if (libent
[i
].sooffset
>= strtabsize
|| libent
[i
].liboffset
>= strtabsize
) {
324 fprintf(stderr
, "ldd: cache '%s' is corrupt\n", LDSO_CACHE
);
332 munmap(cache_addr
, cache_size
);
333 cache_addr
= (caddr_t
) - 1;
337 static int unmap_cache(void)
339 if (cache_addr
== NULL
|| cache_addr
== (caddr_t
) - 1)
343 munmap(cache_addr
, cache_size
);
350 static __inline__
void map_cache(void)
353 static __inline__
void unmap_cache(void)
358 /* This function's behavior must exactly match that
359 * in uClibc/ldso/ldso/dl-elf.c */
360 static void search_for_named_library(char *name
, char *result
,
361 const char *path_list
)
365 struct stat filestat
;
367 /* We need a writable copy of this string */
368 path
= strdup(path_list
);
370 fprintf(stderr
, "%s: Out of memory!\n", __func__
);
373 /* Eliminate all double //s */
375 while ((path_n
= strstr(path_n
, "//"))) {
377 memmove(path_n
, path_n
+ 1, i
- 1);
378 *(path_n
+ i
- 1) = '\0';
381 /* Replace colons with zeros in path_list and count them */
382 for (i
= strlen(path
); i
> 0; i
--) {
383 if (path
[i
] == ':') {
389 for (i
= 0; i
< count
; i
++) {
390 strcpy(result
, path_n
);
392 strcat(result
, name
);
393 if (stat(result
, &filestat
) == 0 && filestat
.st_mode
& S_IRUSR
) {
397 path_n
+= (strlen(path_n
) + 1);
403 static void locate_library_file(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
,
404 int is_suid
, struct library
*lib
)
408 struct stat filestat
;
410 /* If this is a fully resolved name, our job is easy */
411 if (stat(lib
->name
, &filestat
) == 0) {
412 lib
->path
= strdup(lib
->name
);
416 /* We need some elbow room here. Make some room... */
419 fprintf(stderr
, "%s: Out of memory!\n", __func__
);
423 /* This function must match the behavior of _dl_load_shared_library
424 * in readelflib1.c or things won't work out as expected... */
426 /* The ABI specifies that RPATH is searched first, so do that now. */
427 path
= elf_find_rpath(ehdr
, dynamic
);
429 search_for_named_library(lib
->name
, buf
, path
);
436 /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed.
437 * Since this app doesn't actually run an executable I will skip
438 * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */
442 path
= getenv("LD_LIBRARY_PATH");
444 search_for_named_library(lib
->name
, buf
, path
);
450 #ifdef __LDSO_CACHE_SUPPORT__
451 if (cache_addr
!= NULL
&& cache_addr
!= (caddr_t
) - 1) {
453 header_t
*header
= (header_t
*) cache_addr
;
454 libentry_t
*libent
= (libentry_t
*) & header
[1];
455 char *strs
= (char *)&libent
[header
->nlibs
];
457 for (i
= 0; i
< header
->nlibs
; i
++) {
458 if ((libent
[i
].flags
== LIB_ELF
||
459 libent
[i
].flags
== LIB_ELF_LIBC0
||
460 libent
[i
].flags
== LIB_ELF_LIBC5
) &&
461 strcmp(lib
->name
, strs
+ libent
[i
].sooffset
) == 0)
463 lib
->path
= strdup(strs
+ libent
[i
].liboffset
);
470 /* Next look for libraries wherever the shared library
471 * loader was installed -- this is usually where we
472 * should find things... */
474 search_for_named_library(lib
->name
, buf
, interp_dir
);
481 /* Lastly, search the standard list of paths for the library.
482 This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */
483 path
= UCLIBC_RUNTIME_PREFIX
"lib:" UCLIBC_RUNTIME_PREFIX
"usr/lib"
484 #ifndef __LDSO_CACHE_SUPPORT__
485 ":" UCLIBC_RUNTIME_PREFIX
"usr/X11R6/lib"
488 search_for_named_library(lib
->name
, buf
, path
);
493 lib
->path
= not_found
;
497 static int add_library(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
, int is_setuid
, char *s
)
499 char *tmp
, *tmp1
, *tmp2
;
500 struct library
*cur
, *newlib
= lib_list
;
502 if (!s
|| !strlen(s
))
512 /* We add ldso elsewhere */
513 if (interpreter_already_found
&& (tmp
= strrchr(interp_name
, '/')) != NULL
) {
514 int len
= strlen(interp_dir
);
515 if (strcmp(s
, interp_name
+ 1 + len
) == 0)
519 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
520 /* Check if this library is already in the list */
521 tmp1
= tmp2
= cur
->name
;
529 if (strcmp(tmp2
, s
) == 0) {
530 /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */
535 /* Ok, this lib needs to be added to the list */
536 newlib
= malloc(sizeof(struct library
));
539 newlib
->name
= malloc(strlen(s
) + 1);
540 strcpy(newlib
->name
, s
);
541 newlib
->resolved
= 0;
545 /* Now try and locate where this library might be living... */
546 locate_library_file(ehdr
, dynamic
, is_setuid
, newlib
);
548 /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */
552 for (cur
= lib_list
; cur
->next
; cur
= cur
->next
) ; /* nothing */
558 static void find_needed_libraries(ElfW(Ehdr
) *ehdr
, ElfW(Dyn
) *dynamic
, int is_setuid
)
562 for (dyns
= dynamic
; byteswap_to_host(dyns
->d_tag
) != DT_NULL
; ++dyns
) {
563 if (DT_NEEDED
== byteswap_to_host(dyns
->d_tag
)) {
565 strtab
= (char *)elf_find_dynamic(DT_STRTAB
, dynamic
, ehdr
, 0);
566 add_library(ehdr
, dynamic
, is_setuid
, (char *)strtab
+ byteswap_to_host(dyns
->d_un
.d_val
));
571 #ifdef __LDSO_LDD_SUPPORT__
572 static struct library
*find_elf_interpreter(ElfW(Ehdr
) *ehdr
)
576 if (interpreter_already_found
== 1)
578 phdr
= elf_find_phdr_type(PT_INTERP
, ehdr
);
580 struct library
*cur
, *newlib
= NULL
;
581 char *s
= (char *)ehdr
+ byteswap_to_host(phdr
->p_offset
);
584 interp_name
= strdup(s
);
585 interp_dir
= strdup(s
);
586 tmp
= strrchr(interp_dir
, '/');
591 interp_dir
= interp_name
;
599 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
600 /* Check if this library is already in the list */
601 if (!tmp1
|| !cur
->name
)
603 if (strcmp(cur
->name
, tmp1
) == 0) {
604 /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */
607 if (newlib
->path
!= not_found
) {
616 newlib
= malloc(sizeof(struct library
));
619 newlib
->name
= malloc(strlen(s
) + 1);
620 strcpy(newlib
->name
, s
);
621 newlib
->path
= strdup(newlib
->name
);
622 newlib
->resolved
= 1;
626 /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */
630 for (cur
= lib_list
; cur
->next
; cur
= cur
->next
) ; /* nothing */
634 interpreter_already_found
= 1;
639 #endif /* __LDSO_LDD_SUPPORT__ */
641 /* map the .so, and locate interesting pieces */
643 #warning "There may be two warnings here about vfork() clobbering, ignore them"
645 static int find_dependencies(char *filename
)
650 ElfW(Ehdr
) *ehdr
= NULL
;
651 ElfW(Shdr
) *dynsec
= NULL
;
652 ElfW(Dyn
) *dynamic
= NULL
;
653 #ifdef __LDSO_LDD_SUPPORT__
654 struct library
*interp
;
657 if (filename
== not_found
)
661 fprintf(stderr
, "No filename specified.\n");
664 if (!(thefile
= fopen(filename
, "r"))) {
668 if (fstat(fileno(thefile
), &statbuf
) < 0) {
674 if ((size_t) statbuf
.st_size
< sizeof(ElfW(Ehdr
)))
677 if (!S_ISREG(statbuf
.st_mode
))
680 /* mmap the file to make reading stuff from it effortless */
681 ehdr
= mmap(0, statbuf
.st_size
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fileno(thefile
), 0);
682 if (ehdr
== MAP_FAILED
) {
684 fprintf(stderr
, "mmap(%s) failed: %s\n", filename
, strerror(errno
));
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 #ifdef __LDSO_LDD_SUPPORT__
713 interp
= find_elf_interpreter(ehdr
);
716 && (ehdr
->e_type
== ET_EXEC
|| ehdr
->e_type
== ET_DYN
)
717 && ehdr
->e_ident
[EI_CLASS
] == ELFCLASSM
718 && ehdr
->e_ident
[EI_DATA
] == ELFDATAM
719 && ehdr
->e_ident
[EI_VERSION
] == EV_CURRENT
720 && MATCH_MACHINE(ehdr
->e_machine
))
723 if (stat(interp
->path
, &st
) == 0 && S_ISREG(st
.st_mode
)) {
726 static const char *const environment
[] = {
727 "PATH=/usr/bin:/bin:/usr/sbin:/sbin",
729 "LD_TRACE_LOADED_OBJECTS=1",
732 # ifdef __LDSO_STANDALONE_SUPPORT__
733 char * lib_path
= getenv("LD_LIBRARY_PATH");
735 /* The 'extended' environment inclusing the LD_LIBRARY_PATH */
736 static char *ext_environment
[ARRAY_SIZE(environment
) + 1];
737 char **envp
= (char **) environment
;
741 * If the LD_LIBRARY_PATH is set, it needs to include it
742 * into the environment for the new process to be spawned
744 char ** eenvp
= (char **) ext_environment
;
746 /* Copy the N-1 environment's entries */
750 /* Make room for LD_LIBRARY_PATH */
751 *eenvp
= (char *) malloc(sizeof("LD_LIBRARY_PATH=")
753 strcpy(*eenvp
, "LD_LIBRARY_PATH=");
754 strcat(*eenvp
, lib_path
);
756 /* ext_environment[size] is already NULL */
758 /* Use the extended environment */
759 envp
= ext_environment
;
761 if ((pid
= vfork()) == 0) {
763 * Force to use the standard dynamic linker in stand-alone mode.
764 * It will fails at runtime if support is not actually available
766 execle(TRUSTED_LDSO
, TRUSTED_LDSO
, filename
, NULL
, envp
);
770 if ((pid
= vfork()) == 0) {
771 /* Cool, it looks like we should be able to actually
772 * run this puppy. Do so now... */
773 execle(filename
, filename
, NULL
, environment
);
777 /* Wait till it returns */
778 waitpid(pid
, &status
, 0);
780 # ifdef __LDSO_STANDALONE_SUPPORT__
785 if (WIFEXITED(status
) && WEXITSTATUS(status
) == 0) {
789 /* If the exec failed, we fall through to trying to find
790 * all the needed libraries ourselves by rummaging about
791 * in the ELF headers... */
796 dynsec
= elf_find_section_type(SHT_DYNAMIC
, ehdr
);
798 dynamic
= (ElfW(Dyn
) *) (byteswap_to_host(dynsec
->sh_offset
) + (char *)ehdr
);
799 find_needed_libraries(ehdr
, dynamic
, is_suid
);
805 int main(int argc
, char **argv
)
809 char *filename
= NULL
;
813 fprintf(stderr
, "ldd: missing file arguments\n"
814 "Try `ldd --help' for more information.\n");
823 if (strcmp(*argv
, "--") == 0) {
828 if (strcmp(*argv
, "--help") == 0 || strcmp(*argv
, "-h") == 0) {
829 fprintf(stderr
, "Usage: ldd [OPTION]... FILE...\n"
830 "\t--help\t\tprint this help and exit\n");
836 fprintf(stderr
, "No filename specified.\n");
841 printf("%s:\n", *argv
);
846 if (find_dependencies(filename
) != 0)
851 /* Keep walking the list till everybody is resolved */
852 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
853 if (cur
->resolved
== 0 && cur
->path
) {
855 printf("checking sub-depends for '%s'\n", cur
->path
);
856 find_dependencies(cur
->path
);
866 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
868 printf("\t%s => %s (0x00000000)\n", cur
->name
, cur
->path
);
870 if (interp_name
&& interpreter_already_found
== 1)
871 printf("\t%s => %s (0x00000000)\n", interp_name
, interp_name
);
873 printf("\tnot a dynamic executable\n");
875 for (cur
= lib_list
; cur
; cur
= cur
->next
) {
878 if (cur
->path
&& cur
->path
!= not_found
) {