From 3aabb58d126445092dca953223c1730d975491dc Mon Sep 17 00:00:00 2001 From: Waldemar Brodkorb Date: Sun, 13 Dec 2015 23:52:24 +0100 Subject: utlis/ldd: Fix host ldd in case of target wordsize differs from host one improved solution from http://freetz.org/ticket/842 Signed-off-by: Leonid Lisovskiy Signed-off-by: Waldemar Brodkorb --- utils/Makefile.in | 1 + utils/ldd.c | 13 +++++++++---- utils/porting.h | 10 ++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) (limited to 'utils') diff --git a/utils/Makefile.in b/utils/Makefile.in index d7252824c..a601721e5 100644 --- a/utils/Makefile.in +++ b/utils/Makefile.in @@ -14,6 +14,7 @@ CFLAGS-utils := \ -I$(top_srcdir)ldso/include \ -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \ -DUCLIBC_LDSO=\"$(UCLIBC_LDSO)\" \ + -DARCH_NATIVE_BIT=$(ARCH_NATIVE_BIT) \ -I$(top_srcdir)/$(KERNEL_HEADERS) \ -DNOT_IN_libc \ -B$(top_builddir)lib \ diff --git a/utils/ldd.c b/utils/ldd.c index a12acd253..7e1c314b0 100644 --- a/utils/ldd.c +++ b/utils/ldd.c @@ -174,10 +174,10 @@ static __inline__ uint64_t byteswap64_to_host(uint64_t value) } } -#if ELFCLASSM == ELFCLASS32 -# define byteswap_to_host(x) byteswap32_to_host(x) -#else +#if __WORDSIZE == 64 # define byteswap_to_host(x) byteswap64_to_host(x) +#else +# define byteswap_to_host(x) byteswap32_to_host(x) #endif static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr) @@ -239,7 +239,8 @@ static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic) static int check_elf_header(ElfW(Ehdr) *const ehdr) { if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32 - || ehdr->e_ident[EI_CLASS] != ELFCLASSM + /* Use __WORDSIZE, not ELFCLASSM which depends on the host */ + || ehdr->e_ident[EI_CLASS] != (__WORDSIZE >> 5) || ehdr->e_ident[EI_VERSION] != EV_CURRENT ) { return 1; @@ -506,6 +507,8 @@ static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char for (cur = lib_list; cur; cur = cur->next) { /* Check if this library is already in the list */ tmp1 = tmp2 = cur->name; + if (!cur->name) + continue; while (*tmp1) { if (*tmp1 == '/') tmp2 = tmp1 + 1; @@ -583,6 +586,8 @@ static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr) } for (cur = lib_list; cur; cur = cur->next) { /* Check if this library is already in the list */ + if (!tmp1 || !cur->name) + return NULL; if (strcmp(cur->name, tmp1) == 0) { /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */ newlib = cur; diff --git a/utils/porting.h b/utils/porting.h index f1fdc70aa..f83074111 100644 --- a/utils/porting.h +++ b/utils/porting.h @@ -41,6 +41,16 @@ #include "dl-defs.h" #endif +/* __WORDSIZE ist used for __ELF_NATIVE_CLASS, which is used for ElfW(). + We want to provide the wordsize of the target, not of the host, when + compiling readelf.host + */ +#include +#ifdef ARCH_NATIVE_BIT +#undef __WORDSIZE +#define __WORDSIZE ARCH_NATIVE_BIT +#endif + #ifdef DMALLOC #include #endif -- cgit v1.2.3