From e4419aa79a3c999e85610616d6e85847b08f7e19 Mon Sep 17 00:00:00 2001 From: Marcus Comstedt Date: Sat, 10 Oct 2020 13:03:43 +0200 Subject: [PATCH] Fix new ELF checks on big endian hosts (#567) The new macros IS_ELF_... introduced in 80b5b2f5 were not endian safe. --- fesvr/elf.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fesvr/elf.h b/fesvr/elf.h index a2138327..7b38bf11 100644 --- a/fesvr/elf.h +++ b/fesvr/elf.h @@ -14,14 +14,16 @@ ((hdr).e_ident[0] == 0x7f && (hdr).e_ident[1] == 'E' && \ (hdr).e_ident[2] == 'L' && (hdr).e_ident[3] == 'F') +#define ELF_SWAP(hdr, val) (IS_ELFLE(hdr)? from_le((val)) : from_be((val))) + #define IS_ELF32(hdr) (IS_ELF(hdr) && (hdr).e_ident[4] == 1) #define IS_ELF64(hdr) (IS_ELF(hdr) && (hdr).e_ident[4] == 2) #define IS_ELFLE(hdr) (IS_ELF(hdr) && (hdr).e_ident[5] == 1) #define IS_ELFBE(hdr) (IS_ELF(hdr) && (hdr).e_ident[5] == 2) -#define IS_ELF_EXEC(hdr) (IS_ELF(hdr) && (hdr).e_type == ET_EXEC) -#define IS_ELF_RISCV(hdr) (IS_ELF(hdr) && (hdr).e_machine == EM_RISCV) -#define IS_ELF_EM_NONE(hdr) (IS_ELF(hdr) && (hdr).e_machine == EM_NONE) -#define IS_ELF_VCURRENT(hdr) (IS_ELF(hdr) && (hdr).e_version == EV_CURRENT) +#define IS_ELF_EXEC(hdr) (IS_ELF(hdr) && ELF_SWAP((hdr), (hdr).e_type) == ET_EXEC) +#define IS_ELF_RISCV(hdr) (IS_ELF(hdr) && ELF_SWAP((hdr), (hdr).e_machine) == EM_RISCV) +#define IS_ELF_EM_NONE(hdr) (IS_ELF(hdr) && ELF_SWAP((hdr), (hdr).e_machine) == EM_NONE) +#define IS_ELF_VCURRENT(hdr) (IS_ELF(hdr) && ELF_SWAP((hdr), (hdr).e_version) == EV_CURRENT) #define PT_LOAD 1