Skip to contentSkip to conversation

or1k: Add support for a little-endian target variant

PATCH In review Samuel Holland 3 participants 4 messages Jun 9, 2022 – Jun 10, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 or1k: Add support for a little-endian target variant

While not officially sanctioned by the architecture spec, little-endian or1k processors do exist in the wild, for example the Allwinner AR100. Let's add native support for this, instead of hacks like using objcopy to byteswap ELF file contents.

bfd/
* config.bfd (or1k): Split out or1k_elf32_{be,le}vec. Accept or1kle* target variant.
* configure (or1k): Split out or1k_elf32_{be,le}vec.
* configure.ac (or1k): Likewise.
* elf32-or1k.c: Likewise.
* targets.c: Likewise.

gas/
* tc-or1k.c: Add -EB and -EL options. Support dynamic endianness selection.
* tc-or1k.h: Support dynamic endianness selection.
* configure (or1k): Generalize arch to or1k*.
* configure.ac (or1k): Likewise.
* configure.tgt (or1k): Select default endianness from target.

ld/
* configure.tgt (or1k): Generalize arch to or1k*.
* emulparams/elf32or1k.sh: Split out big/little target variants.
* testsuite/ld-elf/binutils.exp: Generalize arch to or1k*.
* testsuite/ld-ifunc/ifunc.exp: Generalize arch to or1k*.

opcodes/
* or1k-opc.h: Make CGEN_DIS_HASH endianness-agnostic.

---
 bfd/config.bfd                   | 12 +++++----
 bfd/configure                    |  3 ++-
 bfd/configure.ac                 |  3 ++-
 bfd/elf32-or1k.c                 |  5 +++-
 bfd/targets.c                    |  6 +++--
 gas/config/tc-or1k.c             | 42 ++++++++++++++++++++++++++------
 gas/config/tc-or1k.h             |  5 +++-
 gas/configure                    |  2 +-
 gas/configure.ac                 |  2 +-
 gas/configure.tgt                |  7 ++++--
 ld/configure.tgt                 |  4 +--
 ld/emulparams/elf32or1k.sh       |  4 ++-
 ld/testsuite/ld-elf/binutils.exp |  2 +-
 ld/testsuite/ld-ifunc/ifunc.exp  |  2 +-
 opcodes/or1k-opc.h               |  2 +-
 15 files changed, 73 insertions(+), 28 deletions(-)
bfd/config.bfd +7 -5
index cfe58247882..d42fea46077 100644
@@ -208,7 +208,7 @@ mips*)		 targ_archs=bfd_mips_arch ;;
 nds32*)		 targ_archs=bfd_nds32_arch ;;
 nfp)		 targ_archs=bfd_nfp_arch ;;
 nios2*)          targ_archs=bfd_nios2_arch ;;
-or1k*|or1knd*)	 targ_archs=bfd_or1k_arch ;;
+or1k*)		 targ_archs=bfd_or1k_arch ;;
 pdp11*)		 targ_archs=bfd_pdp11_arch ;;
 pj*)		 targ_archs="bfd_pj_arch bfd_i386_arch";;
 powerpc*)	 targ_archs="bfd_rs6000_arch bfd_powerpc_arch" ;;
@@ -1046,12 +1046,14 @@ case "${targ}" in
     targ_selvecs=nios2_elf32_be_vec
     ;;
 
-  or1k-*-elf | or1k-*-linux* | or1k-*-rtems*)
-    targ_defvec=or1k_elf32_vec
+  or1kle*-*-elf | or1kle*-*-linux* | or1kle*-*-rtems*)
+    targ_defvec=or1k_elf32_le_vec
+    targ_selvecs=or1k_elf32_be_vec
     ;;
 
-  or1knd-*-elf | or1knd-*-linux* | or1knd-*-rtems*)
-    targ_defvec=or1k_elf32_vec
+  or1k*-*-elf | or1k*-*-linux* | or1k*-*-rtems*)
+    targ_defvec=or1k_elf32_be_vec
+    targ_selvecs=or1k_elf32_le_vec
     ;;
 
   pdp11-*-*)
bfd/configure +2 -1
index b23c9eebfd7..3f6c5aaabc9 100755
@@ -13515,7 +13515,8 @@ do
     nios2_elf32_le_vec)		 tb="$tb elf32-nios2.lo elf32.lo $elf" ;;
     ns32k_aout_pc532mach_vec)	 tb="$tb pc532-mach.lo aout-ns32k.lo" ;;
     ns32k_aout_pc532nbsd_vec)	 tb="$tb ns32knetbsd.lo aout-ns32k.lo" ;;
-    or1k_elf32_vec)		 tb="$tb elf32-or1k.lo elf32.lo $elf" ;;
+    or1k_elf32_be_vec)		 tb="$tb elf32-or1k.lo elf32.lo $elf" ;;
+    or1k_elf32_le_vec)		 tb="$tb elf32-or1k.lo elf32.lo $elf" ;;
     pdp11_aout_vec)		 tb="$tb pdp11.lo" ;;
     pef_vec)			 tb="$tb pef.lo" ;;
     pef_xlib_vec)		 tb="$tb pef.lo" ;;
bfd/configure.ac +2 -1
index a9078965c40..1998603a414 100644
@@ -594,7 +594,8 @@ do
     nios2_elf32_le_vec)		 tb="$tb elf32-nios2.lo elf32.lo $elf" ;;
     ns32k_aout_pc532mach_vec)	 tb="$tb pc532-mach.lo aout-ns32k.lo" ;;
     ns32k_aout_pc532nbsd_vec)	 tb="$tb ns32knetbsd.lo aout-ns32k.lo" ;;
-    or1k_elf32_vec)		 tb="$tb elf32-or1k.lo elf32.lo $elf" ;;
+    or1k_elf32_be_vec)		 tb="$tb elf32-or1k.lo elf32.lo $elf" ;;
+    or1k_elf32_le_vec)		 tb="$tb elf32-or1k.lo elf32.lo $elf" ;;
     pdp11_aout_vec)		 tb="$tb pdp11.lo" ;;
     pef_vec)			 tb="$tb pef.lo" ;;
     pef_xlib_vec)		 tb="$tb pef.lo" ;;
bfd/elf32-or1k.c +4 -1
index 192e83aa7bc..c080402b3c4 100644
@@ -3356,9 +3356,12 @@ or1k_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
 #define ELF_TARGET_ID			OR1K_ELF_DATA
 #define ELF_MAXPAGESIZE			0x2000
 
-#define TARGET_BIG_SYM			or1k_elf32_vec
+#define TARGET_BIG_SYM			or1k_elf32_be_vec
 #define TARGET_BIG_NAME			"elf32-or1k"
 
+#define TARGET_LITTLE_SYM		or1k_elf32_le_vec
+#define TARGET_LITTLE_NAME		"elf32-or1kle"
+
 #define elf_info_to_howto_rel		NULL
 #define elf_info_to_howto		or1k_info_to_howto_rela
 #define elf_backend_relocate_section	or1k_elf_relocate_section
bfd/targets.c +4 -2
index 18fec45f02a..e9ed5104414 100644
@@ -835,7 +835,8 @@ extern const bfd_target nios2_elf32_be_vec;
 extern const bfd_target nios2_elf32_le_vec;
 extern const bfd_target ns32k_aout_pc532mach_vec;
 extern const bfd_target ns32k_aout_pc532nbsd_vec;
-extern const bfd_target or1k_elf32_vec;
+extern const bfd_target or1k_elf32_be_vec;
+extern const bfd_target or1k_elf32_le_vec;
 extern const bfd_target pdp11_aout_vec;
 extern const bfd_target pef_vec;
 extern const bfd_target pef_xlib_vec;
@@ -1219,7 +1220,8 @@ static const bfd_target * const _bfd_target_vector[] =
 	&ns32k_aout_pc532mach_vec,
 	&ns32k_aout_pc532nbsd_vec,
 
-	&or1k_elf32_vec,
+	&or1k_elf32_be_vec,
+	&or1k_elf32_le_vec,
 
 	&pdp11_aout_vec,
 
gas/config/tc-or1k.c +35 -7

73 lines not on this page · raw message

gas/config/tc-or1k.h +4 -1
index dd4f7c837c7..6d81b01db7a 100644
@@ -26,8 +26,11 @@
 extern unsigned long or1k_machine;
 #define TARGET_MACH (or1k_machine)
 
-#define TARGET_FORMAT           "elf32-or1k"
+#define TARGET_FORMAT           (target_big_endian ? "elf32-or1k" : "elf32-or1kle")
+
+#ifndef TARGET_BYTES_BIG_ENDIAN
 #define TARGET_BYTES_BIG_ENDIAN 1
+#endif
 
 extern const char or1k_comment_chars [];
 #define tc_comment_chars or1k_comment_chars
gas/configure +1 -1
index dc6a6682aa4..9ea95a3396b 100755
@@ -12684,7 +12684,7 @@ esac
 cgen_cpu_prefix=""
 if test $using_cgen = yes ; then
   case ${target_cpu} in
-    or1knd)
+    or1k*)
        cgen_cpu_prefix=or1k ;;
     *) cgen_cpu_prefix=${target_cpu} ;;
   esac
gas/configure.ac +1 -1
index 572e56e281c..03786ed073b 100644
@@ -812,7 +812,7 @@ esac
 cgen_cpu_prefix=""
 if test $using_cgen = yes ; then
   case ${target_cpu} in
-    or1knd)
+    or1k*)
        cgen_cpu_prefix=or1k ;;
     *) cgen_cpu_prefix=${target_cpu} ;;
   esac
gas/configure.tgt +5 -2
index 62f806bdfe8..f4f6e6c216a 100644
@@ -86,7 +86,8 @@ case ${cpu} in
   mt)			cpu_type=mt endian=big ;;
   nds32be)		cpu_type=nds32 endian=big ;;
   nds32le)		cpu_type=nds32 endian=little ;;
-  or1k* | or1knd*)	cpu_type=or1k endian=big ;;
+  or1kle*)		cpu_type=or1k endian=little ;;
+  or1k*)		cpu_type=or1k endian=big ;;
   pjl*)			cpu_type=pj endian=little ;;
   pj*)			cpu_type=pj endian=big ;;
   powerpc*le*)		cpu_type=ppc endian=little ;;
@@ -349,7 +350,9 @@ case ${generic_target} in
   ns32k-pc532-lites*)			fmt=aout em=nbsd532 ;;
   ns32k-*-*n*bsd*)			fmt=aout em=nbsd532 ;;
 
+  or1kle*-*-elf | or1kle*-*-rtems*) 	fmt=elf endian=little ;;
   or1k*-*-elf | or1k*-*-rtems*) 	fmt=elf endian=big ;;
+  or1kle*-*-linux*)			fmt=elf em=linux endian=little ;;
   or1k*-*-linux*)			fmt=elf em=linux endian=big ;;
 
   pj*)					fmt=elf ;;
@@ -447,7 +450,7 @@ esac
 
 case ${cpu_type} in
   aarch64 | alpha | arm | csky | i386 | ia64 | microblaze | mips | ns32k | \
-  or1k | or1knd | pdp11 | ppc | riscv | sh | sparc | z80 | z8k | loongarch)
+  or1k | pdp11 | ppc | riscv | sh | sparc | z80 | z8k | loongarch)
     bfd_gas=yes
     ;;
 esac
ld/configure.tgt +2 -2
index c7acf3f11ff..6ab4dceb6be 100644
@@ -621,10 +621,10 @@ ns32k-pc532-mach* | ns32k-pc532-ux*)  targ_emul=pc532macha
 ns32k-*-*bsd* | ns32k-pc532-lites*) targ_emul=ns32knbsd
 			targ_extra_ofiles=
 			;;
-or1k-*-elf | or1knd-*-elf | or1k-*-rtems* | or1knd-*-rtems*)
+or1k*-*-elf | or1k*-*-rtems*)
 			targ_emul=elf32or1k
 			;;
-or1k-*-linux* | or1knd-*-linux*)	targ_emul=elf32or1k_linux
+or1k*-*-linux*)		targ_emul=elf32or1k_linux
 			;;
 pdp11-*-*)		targ_emul=pdp11
 			targ_extra_ofiles=
ld/emulparams/elf32or1k.sh +3 -1
index 48d2ecfa2b0..ccf940dc1b4 100644
@@ -1,7 +1,9 @@
 SCRIPT_NAME=elf
 MACHINE=
 TEMPLATE_NAME=elf
-OUTPUT_FORMAT="elf32-or1k"
+BIG_OUTPUT_FORMAT="elf32-or1k"
+LITTLE_OUTPUT_FORMAT="elf32-or1kle"
+OUTPUT_FORMAT="$BIG_OUTPUT_FORMAT"
 NOP=0x15000000
 TEXT_START_ADDR=0x0000
 TARGET_PAGE_SIZE=0x2000
ld/testsuite/ld-elf/binutils.exp +1 -1
index ee90009bb9b..fd459a2223d 100644
@@ -94,7 +94,7 @@ proc binutils_test { prog_name ld_options test {test_name ""} {readelf_options "
 			      || [istarget "metag-*-*"] \
 			      || [istarget "mips*-*-*"] \
 			      || [istarget "nios2*-*-*"] \
-			      || [istarget "or1k-*-*"] \
+			      || [istarget "or1k*-*-*"] \
 			      || [istarget "riscv*-*-*"] \
 			      || [istarget "sh*-*-*"] \
 			      || [istarget "x86_64-*-rdos*"])]
ld/testsuite/ld-ifunc/ifunc.exp +1 -1
index 1cd8d388b26..122468477ec 100644
@@ -38,7 +38,7 @@ if { ![is_elf_format] || ![supports_gnu_osabi]
      || [istarget mn10300-*-*]
      || [istarget nds32*-*-*]
      || [istarget nios2-*-*]
-     || [istarget or1k-*-*]
+     || [istarget or1k*-*-*]
      || [istarget score*-*-*]
      || [istarget sh*-*-*]
      || [istarget tic6x-*-*]
opcodes/or1k-opc.h +1 -1
index d0950755250..53a3ca4ee21 100644
@@ -35,7 +35,7 @@ extern "C" {
 #undef  CGEN_DIS_HASH_SIZE
 #define CGEN_DIS_HASH_SIZE 256
 #undef  CGEN_DIS_HASH
-#define CGEN_DIS_HASH(buffer, value) (((unsigned char *) (buffer))[0] >> 2)
+#define CGEN_DIS_HASH(buffer, value) ((value) >> 26)
 
 /* Check applicability of instructions against machines.  */
 #define CGEN_VALIDATE_INSN_SUPPORTED
Signature · 1 line
2.35.1
#1 lore raw
Quoted 4 lines · Samuel Holland

While not officially sanctioned by the architecture spec, little-endian or1k processors do exist in the wild, for example the Allwinner AR100. Let's add native support for this, instead of hacks like using objcopy to byteswap ELF file contents.

Hello,

In general I have no objections to this. If there are processors that hare little endian it makes sense to support it. Do you have any details of how people built for these before? I am curious.

Quoted 43 lines
gas/config/tc-or1k.c +22 -2
index ae4e3452f48..9dc5a46f2e2 100644
@@ -58,8 +58,16 @@ const char FLT_CHARS[]            = "dD";
 #define OR1K_SHORTOPTS "m:"
 const char * md_shortopts = OR1K_SHORTOPTS;
 
+enum
+{
+  OPTION_LITTLE_ENDIAN = OPTION_MD_BASE,
+  OPTION_BIG_ENDIAN
+};
+
 struct option md_longopts[] =
 {
+  {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
+  {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
   {NULL, no_argument, NULL, 0}
 };
 size_t md_longopts_size = sizeof (md_longopts);
@@ -67,14 +75,30 @@ size_t md_longopts_size = sizeof (md_longopts);
 unsigned long or1k_machine = 0; /* default */
 
 int
-md_parse_option (int c ATTRIBUTE_UNUSED, const char * arg ATTRIBUTE_UNUSED)
+md_parse_option (int c, const char * arg ATTRIBUTE_UNUSED)
 {
-  return 0;
+  switch (c)
+    {
+    case OPTION_BIG_ENDIAN:
+      target_big_endian = 1;
+      break;
+    case OPTION_LITTLE_ENDIAN:
+      target_big_endian = 0;
+      break;
+    default:
+      return 0;
+    }
+
+  return 1;
 }

gas/config/tc-or1k.c size_t md_longopts_size = sizeof (md_longopts); · line 102

Quoted 10 lines
gas/config/tc-or1k.c +5 -1
 
 void
-md_show_usage (FILE * stream ATTRIBUTE_UNUSED)
+md_show_usage (FILE * stream)
 {
+  fprintf (stream, _(" OR1K-specific assembler options:\n"));
+  fprintf (stream, _("\
+  --EB			generate code for a big endian machine\n\
+  --EL			generate code for a little endian machine\n"));
 }

Aboce you mention -EB, -EL, here is is --EB, --EL.

Does this setup big endian as the default? We should specify that in the options. i.e. "generate code for a big endian machine, this is the default."

But I am not sure how that defaulting works now. I will try to build this and understand better.

-Stafford

#2 lore raw
Quoted 3 lines · Stafford Horne via Binutils

In general I have no objections to this. If there are processors that hare little endian it makes sense to support it. Do you have any details of how people built for these before? I am curious.

The patch looks fine to me too, except that I suspect the testsuite needs updating for little-endian.

Signature · 2 lines
Alan Modra
Australia Development Lab, IBM
#3 lore raw
In reply to Stafford Horne

Hi Stafford,

Thanks for the review.

Quoted 11 lines · Stafford Horne
Quoted 4 lines · Samuel Holland

While not officially sanctioned by the architecture spec, little-endian or1k processors do exist in the wild, for example the Allwinner AR100. Let's add native support for this, instead of hacks like using objcopy to byteswap ELF file contents.

Hello,

In general I have no objections to this. If there are processors that are little endian it makes sense to support it. Do you have any details of how people built for these before? I am curious.

We post-process the binary with "objcopy --reverse-bytes 4".

https://github.com/crust-firmware/crust/blob/master/Makefile#L193
https://github.com/intelligent-agent/klipper/blob/master/src/ar100/Makefile#L39
https://github.com/nfeske/genode-allwinner/blob/scp/src/scp/Makefile#L22

The processor has both a word-invariant mode (the default), and a byte-invariant mode:

https://linux-sunxi.org/AR100#Byte-invariant_Ranges

With a big-endian toolchain, in the word-invariant mode, all GCC optimizations work fine, but sharing data with other software is painful. Sub-32-bit memory accesses are reversed within each group of 4 bytes, which affects strings and struct layouts.

In the byte-invariant mode, sharing strings and structs causes no problems, but some GCC optimizations like -fstore-merging are broken.

Quoted 44 lines
Quoted 44 lines
gas/config/tc-or1k.c +22 -2
index ae4e3452f48..9dc5a46f2e2 100644
@@ -58,8 +58,16 @@ const char FLT_CHARS[]            = "dD";
 #define OR1K_SHORTOPTS "m:"
 const char * md_shortopts = OR1K_SHORTOPTS;
 
+enum
+{
+  OPTION_LITTLE_ENDIAN = OPTION_MD_BASE,
+  OPTION_BIG_ENDIAN
+};
+
 struct option md_longopts[] =
 {
+  {"EB", no_argument, NULL, OPTION_BIG_ENDIAN},
+  {"EL", no_argument, NULL, OPTION_LITTLE_ENDIAN},
   {NULL, no_argument, NULL, 0}
 };
 size_t md_longopts_size = sizeof (md_longopts);
@@ -67,14 +75,30 @@ size_t md_longopts_size = sizeof (md_longopts);
 unsigned long or1k_machine = 0; /* default */
 
 int
-md_parse_option (int c ATTRIBUTE_UNUSED, const char * arg ATTRIBUTE_UNUSED)
+md_parse_option (int c, const char * arg ATTRIBUTE_UNUSED)
 {
-  return 0;
+  switch (c)
+    {
+    case OPTION_BIG_ENDIAN:
+      target_big_endian = 1;
+      break;
+    case OPTION_LITTLE_ENDIAN:
+      target_big_endian = 0;
+      break;
+    default:
+      return 0;
+    }
+
+  return 1;
 }
 
Quoted 11 lines

gas/config/tc-or1k.c size_t md_longopts_size = sizeof (md_longopts); · line 102

Quoted 9 lines
gas/config/tc-or1k.c +5 -1
 void
-md_show_usage (FILE * stream ATTRIBUTE_UNUSED)
+md_show_usage (FILE * stream)
 {
+  fprintf (stream, _(" OR1K-specific assembler options:\n"));
+  fprintf (stream, _("\
+  --EB			generate code for a big endian machine\n\
+  --EL			generate code for a little endian machine\n"));
 }

Aboce you mention -EB, -EL, here is is --EB, --EL.

They are long options. I will fix the changelog.

Quoted 5 lines

Does this setup big endian as the default? We should specify that in the options. i.e. "generate code for a big endian machine, this is the default."

But I am not sure how that defaulting works now. I will try to build this and understand better.

The default depends on the target. It comes from gas/configure.tgt.

Regards,
Samuel