Skip to contentSkip to conversation

Add big-endian build option for riscv

PATCH In review Ben Dooks 3 participants 5 messages Jun 13, 2025 – Jul 3, 2025 RISC-V

lore
Share & tools
lore

Ben Dooks

author
#0 lore raw

Add the option to allow building a RISC-V system big-endian.

This has been tested against our big-endian qemu/cva6 system and is the minimal base to allow a system to run big-endian.

We don't yet address IO endian which has been an interesting issue, as some code seems to assume big-endian means big-endian IO.

author
#1 lore raw

PATCH v2 1/2 riscv: add build support for big-endian

Add support to build code big-endian if the board supports it. Updates the makefile to pass the correct compiler and elf flags.

---
v2:
- fixed issue with big v little endian build (+typos)

---
 arch/riscv/config.mk | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)
arch/riscv/config.mk +14 -4
index 9f16dda92a0..eddd6a3b9a2 100644
@@ -10,19 +10,29 @@
 # Rick Chen, Andes Technology Corporation <[email protected]>
 #
 
-32bit-emul		:= elf32lriscv
-64bit-emul		:= elf64lriscv
+ifdef CONFIG_SYS_BIG_ENDIAN
+small-endian		:= b
+large-endian		:= big
+PLATFORM_CPPFLAGS       += -mbig-endian
+KBUILD_LDFLAGS          += -mbig-endian
+else
+small-endian		:= l
+large-endian		:= little
+endif
+
+32bit-emul		:= elf32$(small-endian)riscv
+64bit-emul		:= elf64$(small-endian)riscv
 
 ifdef CONFIG_32BIT
 KBUILD_LDFLAGS		+= -m $(32bit-emul)
 EFI_LDS			:= elf_riscv32_efi.lds
-PLATFORM_ELFFLAGS	+= -B riscv -O elf32-littleriscv
+PLATFORM_ELFFLAGS	+= -B riscv -O elf32-$(large-endian)riscv
 endif
 
 ifdef CONFIG_64BIT
 KBUILD_LDFLAGS		+= -m $(64bit-emul)
 EFI_LDS			:= elf_riscv64_efi.lds
-PLATFORM_ELFFLAGS	+= -B riscv -O elf64-littleriscv
+PLATFORM_ELFFLAGS	+= -B riscv -O elf64-$(large-endian)riscv
 endif
 
 PLATFORM_CPPFLAGS	+= -ffixed-x3 -fpic
Signature · 1 line
2.37.2.352.g3c44437643
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] riscv: add build support for big-endian

Quoted 5 lines · Ben Dooks

Add support to build code big-endian if the board supports it. Updates the makefile to pass the correct compiler and elf flags.

Signature · 1 line
Tom
author
#3 lore raw

PATCH v2 2/2 riscv: byteorder: add test for big-endian

In reply to Ben Dooks

Test for big-endian either via __RISCVEB__ which migth be rather old, or check the BYTE_ORDER if the compiler defines it (which should be any modern gcc like v12)

---
 arch/riscv/include/asm/byteorder.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/include/asm/byteorder.h +1 -1
index d26ac5688fa..3140c1f585e 100644
@@ -26,7 +26,7 @@
 #  define __SWAB_64_THRU_32__
 #endif
 
-#ifdef __RISCVEB__
+#if  defined(__RISCVEB__) || (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
 #include <linux/byteorder/big_endian.h>
 #else
 #include <linux/byteorder/little_endian.h>
Signature · 1 line
2.37.2.352.g3c44437643
Acked-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] riscv: byteorder: add test for big-endian

Quoted 8 lines · Ben Dooks

Test for big-endian either via __RISCVEB__ which migth be rather old, or check the BYTE_ORDER if the compiler defines it (which should be any modern gcc like v12)

---
 arch/riscv/include/asm/byteorder.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)