riscv: tools: Fix prelink-riscv to work on big endian hosts
PATCH 1/1 riscv: tools: Fix prelink-riscv to work on big endian hosts
All ELF fields whose values are inspected by the code are converted to CPU byteorder first. Values which are copied verbatim (relocation fixups) are not swapped to CPU byteorder and back as it is not needed.
Note that it is still assumed that the RISC-V ELF is little endian. In order to support big endian RISC-V targets as well, the calls to le*_to_cpu need to be replaced with macros target*_to_cpu defined by prelink-riscv.c, and prelink-riscv.inc included four times (32le, 64le, 32be, 32be) instead of two.
--- tools/prelink-riscv.c | 5 +---- tools/prelink-riscv.inc | 41 +++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 24 deletions(-)
tools/prelink-riscv.c +1 -4
@@ -8,10 +8,6 @@* without fixup. Both RV32 and RV64 are supported. */ -#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ -#error "Only little-endian host is supported" -#endif -#include <errno.h> #include <stdbool.h> #include <stdint.h>
@@ -25,6 +21,7 @@#include <sys/stat.h> #include <sys/types.h> #include <unistd.h> +#include <compiler.h> #ifndef EM_RISCV #define EM_RISCV 243
tools/prelink-riscv.inc +21 -20
81 lines not on this page · raw message
Signature · 1 line
2.21.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/1 riscv: tools: Fix prelink-riscv to work on big endian hosts
Hi Marcus
Quoted 135 lines
From: Marcus Comstedt [mailto:marcus at mc.pp.se]
Sent: Monday, July 15, 2019 8:36 PM
To: u-boot at lists.denx.de
Cc: Marcus Comstedt; Rick Jian-Zhi Chen(陳建志)
Subject: [PATCH] riscv: tools: Fix prelink-riscv to work on big endian hostsAll ELF fields whose values are inspected by the code are converted to CPU byteorder first. Values which are copied verbatim (relocation fixups) are not swapped to CPU byteorder and back as it is not needed.
Note that it is still assumed that the RISC-V ELF is little endian. In order to support big endian RISC-V targets as well, the calls to le*_to_cpu need to be replaced with macros target*_to_cpu defined by
prelink-riscv.c, and prelink-riscv.inc included four times (32le, 64le, 32be, 32be) instead of two.Signed-off-by Marcus Comstedt <[email protected]>Cc Rick Chen <[email protected]>--- tools/prelink-riscv.c | 5 +---- tools/prelink-riscv.inc | 41 +++++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 24 deletions(-)tools/prelink-riscv.c index renamed from tools/prelink-riscv.c +0
-0
52eb78e9d0..a900a1497a100644tools/prelink-riscv.c +0
-1@@ -8,10 +8,6 @@* without fixup. Both RV32 and RV64 are supported. */ -#if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ -#error "Onlylittle-endian host is supported"
tools/prelink-riscv.c +1
-2-#endif -#include <errno.h> #include <stdbool.h> #include <stdint.h> @@ -25,6 +21,7 @@#include <sys/stat.h> #include <sys/types.h> #include <unistd.h> +#include <compiler.h> #ifndef EM_RISCV #define EM_RISCV 243tools/prelink-riscv.inc index renamed from tools/prelink-riscv.inc +0
-0
d49258707d..e451159c03100644tools/prelink-riscv.inc +16
-14@@ -23,14 +23,15 @@ #define Elf_Addr CONCAT3(Elf, PRELINK_INC_BITS, _Addr) #define ELF_R_TYPE CONCAT3(ELF, PRELINK_INC_BITS, _R_TYPE) #define ELF_R_SYM CONCAT3(ELF, PRELINK_INC_BITS, _R_SYM) +#define lenn_to_cpu CONCAT3(le, PRELINK_INC_BITS, _to_cpu) static void* get_offset_nn (void* data, Elf_Phdr* phdrs, size_t phnum, Elf_Addr addr) { Elf_Phdr *p; for (p = phdrs; p < phdrs + phnum; ++p) - if (p->p_vaddr <= addr && p->p_vaddr + p->p_memsz > addr) - return data + p->p_offset + (addr - p->p_vaddr); + if (lenn_to_cpu(p->p_vaddr) <= addr && lenn_to_cpu(p->p_vaddr) + lenn_to_cpu(p->p_memsz) > addr) + return data + lenn_to_cpu(p->p_offset) + (addr - +lenn_to_cpu(p->p_vaddr)); return NULL; }@@ -42,15 +43,15 @@ static void prelink_nn(void *data) Elf_Dyn *dyn; Elf_Rela *r; - if (ehdr->e_machine != EM_RISCV) + if (le16_to_cpu(ehdr->e_machine) != EM_RISCV) die("Machine type is not RISC-V"); - Elf_Phdr *phdrs = data + ehdr->e_phoff; + Elf_Phdr *phdrs = data + lenn_to_cpu(ehdr->e_phoff); Elf_Dyn *dyns = NULL; - for (p = phdrs; p < phdrs + ehdr->e_phnum; ++p) { - if (p->p_type == PT_DYNAMIC) { - dyns = data + p->p_offset; + for (p = phdrs; p < phdrs + le16_to_cpu(ehdr->e_phnum); ++p) { + if (le32_to_cpu(p->p_type) == PT_DYNAMIC) { + dyns = data + lenn_to_cpu(p->p_offset); break; } }@@ -62,14 +63,14 @@ static void prelink_nn(void *data) size_t rela_count = 0; Elf_Sym *dynsym = NULL; for (dyn = dyns;; ++dyn) { - if (dyn->d_tag == DT_NULL) + if (lenn_to_cpu(dyn->d_tag) == DT_NULL) break; - else if (dyn->d_tag == DT_RELA) - rela_dyn = get_offset_nn(data, phdrs, ehdr->e_phnum, + dyn->d_un.d_ptr); - else if (dyn->d_tag == DT_RELASZ) - rela_count = dyn->d_un.d_val / sizeof(Elf_Rela); - else if (dyn->d_tag == DT_SYMTAB) - dynsym = get_offset_nn(data, phdrs, ehdr->e_phnum, + dyn->d_un.d_ptr); + else if (lenn_to_cpu(dyn->d_tag) == DT_RELA) + rela_dyn = get_offset_nn(data, phdrs, le16_to_cpu(ehdr->e_phnum), + lenn_to_cpu(dyn->d_un.d_ptr)); + else if (lenn_to_cpu(dyn->d_tag) == DT_RELASZ) + rela_count = lenn_to_cpu(dyn->d_un.d_val) / sizeof(Elf_Rela); + else if (lenn_to_cpu(dyn->d_tag) == DT_SYMTAB) + dynsym = get_offset_nn(data, phdrs,
le16_to_cpu(ehdr->e_phnum), +tools/prelink-riscv.inc +4
-4+lenn_to_cpu(dyn->d_un.d_ptr)); }@@ -80,17 +81,17 @@ static void prelink_nn(void *data) die("No .dynsym found"); for (r = rela_dyn; r < rela_dyn + rela_count; ++r) { - void* buf = get_offset_nn(data, phdrs, ehdr->e_phnum, r->r_offset); + void* buf = get_offset_nn(data, phdrs, le16_to_cpu(ehdr->e_phnum), +lenn_to_cpu(r->r_offset)); if (buf == NULL) continue; - if (ELF_R_TYPE(r->r_info) == R_RISCV_RELATIVE) + if (ELF_R_TYPE(lenn_to_cpu(r->r_info)) == R_RISCV_RELATIVE) *((uintnn_t*) buf) = r->r_addend; - else if (ELF_R_TYPE(r->r_info) == R_RISCV_32) - *((uint32_t*) buf) = dynsym[ELF_R_SYM(r->r_info)].st_value;
tools/prelink-riscv.inc static void prelink_nn(void *data) · line 96
Quoted 10 lines
tools/prelink-riscv.inc +5
-2- else if (ELF_R_TYPE(r->r_info) == R_RISCV_64) - *((uint64_t*) buf) = dynsym[ELF_R_SYM(r->r_info)].st_value; + else if (ELF_R_TYPE(lenn_to_cpu(r->r_info)) == R_RISCV_32) + *((uint32_t*) buf) = dynsym[ELF_R_SYM(lenn_to_cpu(r->r_info))].st_value; + else if (ELF_R_TYPE(lenn_to_cpu(r->r_info)) == R_RISCV_64) + *((uint64_t*) buf) = +dynsym[ELF_R_SYM(lenn_to_cpu(r->r_info))].st_value; } }
Nitpick: #undef lenn_to_cpu at the end of prelink-riscv.inc.
Other than that,
Quoted 3 lines
--
2.21.0
PATCH 1/1 riscv: tools: Fix prelink-riscv to work on big endian hosts
Hi Rick,
Quoted 4 lines · Rick Chen
Nitpick: #undef
lenn_to_cpuat the end of prelink-riscv.inc.Other than that,
Reviewed-by Rick Chen <[email protected]>
Thanks for reviewing!
Should I post a new version of the patch which adds the missing #undef?
In the meantime I have also implemented support for big endian ELFs if there is interest. :-D
// Marcus