sim: overhaul & unify endian settings management

Message ID 20210616054957.10477-1-vapier@gentoo.org
State New
Headers show
Series
  • sim: overhaul & unify endian settings management
Related show

Commit Message

Simon Marchi via Gdb-patches June 16, 2021, 5:49 a.m.
The m4 macro has 2 args: the "wire" settings (which represents the
hardwired port behavior), and the default settings (which are used
if nothing else is specified).  If none are specified, the arch is
expected to support both, and the value will be probed based on the
user runtime options or the input program.

Only two arches today set the default value (bpf & mips).  We can
probably let this go as it only shows up in one scenario: the sim
is invoked, but with no inputs, and no user endian selection.  This
means bpf will not behave like the other arches: an error is shown
and forces the user to make a choice.  If an input program is used
though, we'll still switch the default to that.  This allows us to
remove the WITH_DEFAULT_TARGET_BYTE_ORDER setting.

For the ports that set a "wire" endian, move it to the runtime init
of the respective sim_open calls.  This allows us to change the
WITH_TARGET_BYTE_ORDER to purely a user-selected configure setting
if they want to force a specific endianness.

With all the endian logic moved to runtime selection, we can move
the configure call up to the common dir so we only process it once
across all ports.

The ppc arch was picking the wire endian based on the target used,
but since we weren't doing that for other biendian arches, we can
let this go too.  We'll rely on the input selecting the endian, or
make the user decide.
---
 sim/Makefile.in                  |  1 +
 sim/aarch64/aclocal.m4           |  1 -
 sim/aarch64/configure            | 59 ++-----------------------
 sim/aarch64/configure.ac         |  2 -
 sim/aclocal.m4                   |  1 +
 sim/arm/aclocal.m4               |  1 -
 sim/arm/configure                | 59 ++-----------------------
 sim/arm/configure.ac             |  2 -
 sim/avr/aclocal.m4               |  1 -
 sim/avr/configure                | 59 ++-----------------------
 sim/avr/configure.ac             |  2 -
 sim/avr/interp.c                 |  1 +
 sim/bfin/aclocal.m4              |  1 -
 sim/bfin/configure               | 56 +-----------------------
 sim/bfin/configure.ac            |  1 -
 sim/bfin/interp.c                |  1 +
 sim/bpf/aclocal.m4               |  1 -
 sim/bpf/configure                | 56 +-----------------------
 sim/bpf/configure.ac             |  1 -
 sim/common/Make-common.in        |  2 -
 sim/common/sim-config.c          |  5 ---
 sim/common/sim-config.h          |  8 ----
 sim/config.h.in                  |  3 ++
 sim/configure                    | 23 ++++++++++
 sim/configure.ac                 |  1 +
 sim/cr16/aclocal.m4              |  1 -
 sim/cr16/configure               | 59 ++-----------------------
 sim/cr16/configure.ac            |  2 -
 sim/cr16/interp.c                |  3 ++
 sim/cris/aclocal.m4              |  1 -
 sim/cris/configure               | 56 +-----------------------
 sim/cris/configure.ac            |  1 -
 sim/cris/sim-if.c                |  3 ++
 sim/d10v/aclocal.m4              |  1 -
 sim/d10v/configure               | 59 ++-----------------------
 sim/d10v/configure.ac            |  2 -
 sim/erc32/configure              |  8 +---
 sim/example-synacor/aclocal.m4   |  1 -
 sim/example-synacor/configure    | 59 ++-----------------------
 sim/example-synacor/configure.ac |  2 -
 sim/example-synacor/interp.c     |  1 +
 sim/frv/aclocal.m4               |  1 -
 sim/frv/configure                | 56 +-----------------------
 sim/frv/configure.ac             |  1 -
 sim/frv/sim-if.c                 |  1 +
 sim/ft32/aclocal.m4              |  1 -
 sim/ft32/configure               | 59 ++-----------------------
 sim/ft32/configure.ac            |  2 -
 sim/ft32/interp.c                |  1 +
 sim/h8300/aclocal.m4             |  1 -
 sim/h8300/compile.c              |  3 ++
 sim/h8300/configure              | 59 ++-----------------------
 sim/h8300/configure.ac           |  2 -
 sim/iq2000/aclocal.m4            |  1 -
 sim/iq2000/configure             | 56 +-----------------------
 sim/iq2000/configure.ac          |  1 -
 sim/iq2000/sim-if.c              |  1 +
 sim/lm32/aclocal.m4              |  1 -
 sim/lm32/configure               | 56 +-----------------------
 sim/lm32/configure.ac            |  1 -
 sim/lm32/sim-if.c                |  1 +
 sim/m32c/configure               |  8 +---
 sim/m32r/aclocal.m4              |  1 -
 sim/m32r/configure               | 56 +-----------------------
 sim/m32r/configure.ac            |  1 -
 sim/m32r/sim-if.c                |  1 +
 sim/m4/sim_ac_option_endian.m4   | 55 ++++++------------------
 sim/m4/sim_ac_output.m4          |  2 -
 sim/m68hc11/aclocal.m4           |  1 -
 sim/m68hc11/configure            | 58 ++-----------------------
 sim/m68hc11/configure.ac         |  1 -
 sim/m68hc11/interp.c             |  3 ++
 sim/mcore/aclocal.m4             |  1 -
 sim/mcore/configure              | 59 ++-----------------------
 sim/mcore/configure.ac           |  2 -
 sim/microblaze/aclocal.m4        |  1 -
 sim/microblaze/configure         | 59 ++-----------------------
 sim/microblaze/configure.ac      |  2 -
 sim/mips/aclocal.m4              |  1 -
 sim/mips/configure               | 74 +-------------------------------
 sim/mips/configure.ac            | 19 --------
 sim/mn10300/aclocal.m4           |  1 -
 sim/mn10300/configure            | 56 +-----------------------
 sim/mn10300/configure.ac         |  1 -
 sim/mn10300/interp.c             |  3 ++
 sim/moxie/aclocal.m4             |  1 -
 sim/moxie/configure              | 59 ++-----------------------
 sim/moxie/configure.ac           |  2 -
 sim/moxie/interp.c               |  3 ++
 sim/msp430/aclocal.m4            |  1 -
 sim/msp430/configure             | 59 ++-----------------------
 sim/msp430/configure.ac          |  2 -
 sim/msp430/msp430-sim.c          |  3 ++
 sim/or1k/aclocal.m4              |  1 -
 sim/or1k/configure               | 56 +-----------------------
 sim/or1k/configure.ac            |  1 -
 sim/or1k/sim-if.c                |  3 ++
 sim/ppc/Makefile.in              |  2 -
 sim/ppc/configure                | 25 -----------
 sim/ppc/configure.ac             | 19 --------
 sim/pru/aclocal.m4               |  1 -
 sim/pru/configure                | 59 ++-----------------------
 sim/pru/configure.ac             |  2 -
 sim/pru/interp.c                 |  1 +
 sim/riscv/aclocal.m4             |  1 -
 sim/riscv/configure              | 57 +-----------------------
 sim/riscv/configure.ac           |  2 -
 sim/riscv/interp.c               |  3 ++
 sim/rl78/configure               |  8 +---
 sim/rx/configure                 |  8 +---
 sim/sh/aclocal.m4                |  1 -
 sim/sh/configure                 | 58 ++-----------------------
 sim/sh/configure.ac              |  1 -
 sim/v850/aclocal.m4              |  1 -
 sim/v850/configure               | 56 +-----------------------
 sim/v850/configure.ac            |  1 -
 sim/v850/interp.c                |  3 ++
 117 files changed, 157 insertions(+), 1721 deletions(-)

-- 
2.31.1

Patch

diff --git a/sim/aarch64/configure.ac b/sim/aarch64/configure.ac
index 7ac149a5f9db..849e957f8277 100644
--- a/sim/aarch64/configure.ac
+++ b/sim/aarch64/configure.ac
@@ -24,6 +24,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN
-
 SIM_AC_OUTPUT
diff --git a/sim/arm/configure.ac b/sim/arm/configure.ac
index ec8a87e7d397..08bf6ff8acbc 100644
--- a/sim/arm/configure.ac
+++ b/sim/arm/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN
-
 SIM_AC_OUTPUT
diff --git a/sim/avr/configure.ac b/sim/avr/configure.ac
index 8cd4d23a479b..08bf6ff8acbc 100644
--- a/sim/avr/configure.ac
+++ b/sim/avr/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
-
 SIM_AC_OUTPUT
diff --git a/sim/avr/interp.c b/sim/avr/interp.c
index 1626fadaa973..2bd72731b555 100644
--- a/sim/avr/interp.c
+++ b/sim/avr/interp.c
@@ -1686,6 +1686,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *cb,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/bfin/configure.ac b/sim/bfin/configure.ac
index ddc7bc6e0399..9b17dc4212fe 100644
--- a/sim/bfin/configure.ac
+++ b/sim/bfin/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
 SIM_AC_OPTION_DEFAULT_MODEL(bf537)
 SIM_AC_OPTION_HARDWARE(\
 	bfin_cec \
diff --git a/sim/bfin/interp.c b/sim/bfin/interp.c
index 10de001d7ce7..216cefe2c21c 100644
--- a/sim/bfin/interp.c
+++ b/sim/bfin/interp.c
@@ -704,6 +704,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/bpf/configure.ac b/sim/bpf/configure.ac
index 4e00c1316090..d2ec00c5a34c 100644
--- a/sim/bpf/configure.ac
+++ b/sim/bpf/configure.ac
@@ -5,7 +5,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 SIM_AC_COMMON
 
 SIM_AC_OPTION_BITSIZE([64])
-SIM_AC_OPTION_ENDIAN([], [LITTLE])
 SIM_AC_OPTION_SCACHE(16384)
 SIM_AC_OPTION_DEFAULT_MODEL([bpf-def])
 SIM_AC_OPTION_WARNINGS(no)
diff --git a/sim/common/Make-common.in b/sim/common/Make-common.in
index 54ac7f40a9c0..5b62ca5a2448 100644
--- a/sim/common/Make-common.in
+++ b/sim/common/Make-common.in
@@ -84,7 +84,6 @@  LDFLAGS = @LDFLAGS@
 
 SIM_BITSIZE = @sim_bitsize@
 SIM_DEFAULT_MODEL = @sim_default_model@
-SIM_ENDIAN = @sim_endian@
 SIM_FLOAT = @sim_float@
 SIM_HW_CFLAGS = @sim_hw_cflags@
 SIM_HW_OBJS = @sim_hw_objs@
@@ -221,7 +220,6 @@  CONFIG_CFLAGS = \
 	-DHAVE_CONFIG_H \
 	$(SIM_DEFAULT_MODEL) \
 	$(SIM_BITSIZE) \
-	$(SIM_ENDIAN) \
 	$(SIM_FLOAT) \
 	$(SIM_HW_CFLAGS) \
 	$(SIM_INLINE) \
diff --git a/sim/common/sim-config.c b/sim/common/sim-config.c
index c9005f449fe6..fc6b09a7238d 100644
--- a/sim/common/sim-config.c
+++ b/sim/common/sim-config.c
@@ -169,8 +169,6 @@  sim_config (SIM_DESC sd)
     current_target_byte_order = prefered_target_byte_order;
   if (current_target_byte_order == BFD_ENDIAN_UNKNOWN)
     current_target_byte_order = WITH_TARGET_BYTE_ORDER;
-  if (current_target_byte_order == BFD_ENDIAN_UNKNOWN)
-    current_target_byte_order = WITH_DEFAULT_TARGET_BYTE_ORDER;
 
   /* verify the target byte order */
   if (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_UNKNOWN)
@@ -302,9 +300,6 @@  sim_config_print (SIM_DESC sd)
   sim_io_printf (sd, "WITH_TARGET_BYTE_ORDER = %s\n",
 		 config_byte_order_to_a (WITH_TARGET_BYTE_ORDER));
 
-  sim_io_printf (sd, "WITH_DEFAULT_TARGET_BYTE_ORDER = %s\n",
-		 config_byte_order_to_a (WITH_DEFAULT_TARGET_BYTE_ORDER));
-
   sim_io_printf (sd, "HOST_BYTE_ORDER = %s\n",
 		 config_byte_order_to_a (HOST_BYTE_ORDER));
 
diff --git a/sim/common/sim-config.h b/sim/common/sim-config.h
index 2b6ed3d0b7d4..5b1713cff706 100644
--- a/sim/common/sim-config.h
+++ b/sim/common/sim-config.h
@@ -50,14 +50,6 @@ 
 
    Possible values are BFD_ENDIAN_UNKNOWN, BFD_ENDIAN_LITTLE, or BFD_ENDIAN_BIG.  */
 
-#ifndef WITH_TARGET_BYTE_ORDER
-#define WITH_TARGET_BYTE_ORDER		BFD_ENDIAN_UNKNOWN
-#endif
-
-#ifndef WITH_DEFAULT_TARGET_BYTE_ORDER
-#define WITH_DEFAULT_TARGET_BYTE_ORDER	BFD_ENDIAN_UNKNOWN
-#endif
-
 extern enum bfd_endian current_target_byte_order;
 #define CURRENT_TARGET_BYTE_ORDER \
   (WITH_TARGET_BYTE_ORDER != BFD_ENDIAN_UNKNOWN \
diff --git a/sim/configure.ac b/sim/configure.ac
index 129e9421ec1d..4ba2921ebaf3 100644
--- a/sim/configure.ac
+++ b/sim/configure.ac
@@ -112,6 +112,7 @@  dnl Eventually all simulators will support these.
 SIM_AC_OPTION_ALIGNMENT
 SIM_AC_OPTION_ASSERT
 SIM_AC_OPTION_DEBUG
+SIM_AC_OPTION_ENDIAN
 SIM_AC_OPTION_ENVIRONMENT
 SIM_AC_OPTION_PROFILE
 SIM_AC_OPTION_STDIO
diff --git a/sim/cr16/configure.ac b/sim/cr16/configure.ac
index 8cd4d23a479b..08bf6ff8acbc 100644
--- a/sim/cr16/configure.ac
+++ b/sim/cr16/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
-
 SIM_AC_OUTPUT
diff --git a/sim/cr16/interp.c b/sim/cr16/interp.c
index 7f6b07fe286a..6e3438c9d809 100644
--- a/sim/cr16/interp.c
+++ b/sim/cr16/interp.c
@@ -398,6 +398,9 @@  sim_open (SIM_OPEN_KIND kind, struct host_callback_struct *cb,
   SIM_DESC sd = sim_state_alloc (kind, cb);
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
diff --git a/sim/cris/configure.ac b/sim/cris/configure.ac
index cc5bb4922f18..48c4bbeabac2 100644
--- a/sim/cris/configure.ac
+++ b/sim/cris/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
 SIM_AC_OPTION_SCACHE(16384)
 SIM_AC_OPTION_WARNINGS(no)
 SIM_AC_OPTION_HARDWARE(rv cris cris_900000xx)
diff --git a/sim/cris/sim-if.c b/sim/cris/sim-if.c
index c6dcc23c938a..c183856fa4c8 100644
--- a/sim/cris/sim-if.c
+++ b/sim/cris/sim-if.c
@@ -654,6 +654,9 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
      standard ;-) that the rest of the elements won't be initialized.  */
   bfd_byte sp_init[4] = {0, 0, 0, 0};
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
diff --git a/sim/d10v/configure.ac b/sim/d10v/configure.ac
index ec8a87e7d397..08bf6ff8acbc 100644
--- a/sim/d10v/configure.ac
+++ b/sim/d10v/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN
-
 SIM_AC_OUTPUT
diff --git a/sim/example-synacor/configure.ac b/sim/example-synacor/configure.ac
index 8cd4d23a479b..08bf6ff8acbc 100644
--- a/sim/example-synacor/configure.ac
+++ b/sim/example-synacor/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
-
 SIM_AC_OUTPUT
diff --git a/sim/example-synacor/interp.c b/sim/example-synacor/interp.c
index 784c9cbc5755..57b1bee7cd9d 100644
--- a/sim/example-synacor/interp.c
+++ b/sim/example-synacor/interp.c
@@ -84,6 +84,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/frv/configure.ac b/sim/frv/configure.ac
index fcc2e8836bb7..39d8de4a5b8e 100644
--- a/sim/frv/configure.ac
+++ b/sim/frv/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(BIG)
 SIM_AC_OPTION_SCACHE(16384)
 SIM_AC_OPTION_DEFAULT_MODEL(fr500)
 SIM_AC_OPTION_WARNINGS(no)
diff --git a/sim/frv/sim-if.c b/sim/frv/sim-if.c
index 0ad5f626e45c..758e17a3fc5a 100644
--- a/sim/frv/sim-if.c
+++ b/sim/frv/sim-if.c
@@ -56,6 +56,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback, bfd *abfd,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_BIG;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/ft32/configure.ac b/sim/ft32/configure.ac
index 8cd4d23a479b..08bf6ff8acbc 100644
--- a/sim/ft32/configure.ac
+++ b/sim/ft32/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
-
 SIM_AC_OUTPUT
diff --git a/sim/ft32/interp.c b/sim/ft32/interp.c
index 8a44a472f605..77ed8f8eb897 100644
--- a/sim/ft32/interp.c
+++ b/sim/ft32/interp.c
@@ -810,6 +810,7 @@  sim_open (SIM_OPEN_KIND kind,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/h8300/compile.c b/sim/h8300/compile.c
index 01e9766790c8..0a36d07837cf 100644
--- a/sim/h8300/compile.c
+++ b/sim/h8300/compile.c
@@ -4715,6 +4715,9 @@  sim_open (SIM_OPEN_KIND kind,
 
   sd = sim_state_alloc_extra (kind, callback, sizeof (struct h8300_sim_state));
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_BIG;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
diff --git a/sim/h8300/configure.ac b/sim/h8300/configure.ac
index 7e2a166307ed..08bf6ff8acbc 100644
--- a/sim/h8300/configure.ac
+++ b/sim/h8300/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(BIG)
-
 SIM_AC_OUTPUT
diff --git a/sim/iq2000/configure.ac b/sim/iq2000/configure.ac
index c61c9480d5e5..4afa1b3501d8 100644
--- a/sim/iq2000/configure.ac
+++ b/sim/iq2000/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(BIG)
 SIM_AC_OPTION_SCACHE(16384)
 SIM_AC_OPTION_DEFAULT_MODEL(iq2000)
 SIM_AC_OPTION_WARNINGS(no)
diff --git a/sim/iq2000/sim-if.c b/sim/iq2000/sim-if.c
index 82ac53f2d0bd..1129cee91457 100644
--- a/sim/iq2000/sim-if.c
+++ b/sim/iq2000/sim-if.c
@@ -62,6 +62,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_BIG;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/lm32/configure.ac b/sim/lm32/configure.ac
index 4a2037468dee..06c655696420 100644
--- a/sim/lm32/configure.ac
+++ b/sim/lm32/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(BIG)
 SIM_AC_OPTION_SCACHE(16384)
 SIM_AC_OPTION_DEFAULT_MODEL(lm32)
 SIM_AC_OPTION_WARNINGS(no)
diff --git a/sim/lm32/sim-if.c b/sim/lm32/sim-if.c
index fba2d638f42b..0142868834d5 100644
--- a/sim/lm32/sim-if.c
+++ b/sim/lm32/sim-if.c
@@ -93,6 +93,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_BIG;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/m32r/configure.ac b/sim/m32r/configure.ac
index a5cea83849e7..9377081e938a 100644
--- a/sim/m32r/configure.ac
+++ b/sim/m32r/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(BIG)
 SIM_AC_OPTION_SCACHE(16384)
 SIM_AC_OPTION_DEFAULT_MODEL(m32r/d)
 SIM_AC_OPTION_WARNINGS(no)
diff --git a/sim/m32r/sim-if.c b/sim/m32r/sim-if.c
index bea6505d7404..3d8db0332fd6 100644
--- a/sim/m32r/sim-if.c
+++ b/sim/m32r/sim-if.c
@@ -56,6 +56,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_BIG;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/m4/sim_ac_option_endian.m4 b/sim/m4/sim_ac_option_endian.m4
index e5a5b61dc178..01467a06c163 100644
--- a/sim/m4/sim_ac_option_endian.m4
+++ b/sim/m4/sim_ac_option_endian.m4
@@ -15,51 +15,20 @@  dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
 dnl
 dnl --enable-sim-endian={yes,no,big,little} is for simulators
 dnl that support both big and little endian targets.
-dnl arg[1] is hardwired target endianness.
-dnl arg[2] is default target endianness.
 AC_DEFUN([SIM_AC_OPTION_ENDIAN],
-[
-wire_endian="[$1]"
-default_endian="[$2]"
+[dnl
+AC_MSG_CHECKING([whether to force sim endianness])
+sim_endian=
 AC_ARG_ENABLE(sim-endian,
 [AS_HELP_STRING([--enable-sim-endian=endian],
 		[Specify target byte endian orientation])],
 [case "${enableval}" in
-  b*|B*) sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_BIG";;
-  l*|L*) sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_LITTLE";;
-  yes)	 if test x"$wire_endian" != x; then
-	   sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_${wire_endian}"
-	 else
-	  if test x"$default_endian" != x; then
-	     sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_${default_endian}"
-	   else
-	     echo "No hard-wired endian for target $target" 1>&6
-	     sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_UNKNOWN"
-	   fi
-	 fi;;
-  no)	 if test x"$default_endian" != x; then
-	   sim_endian="-DWITH_DEFAULT_TARGET_BYTE_ORDER=BFD_ENDIAN_${default_endian}"
-	 else
-	   if test x"$wire_endian" != x; then
-	     sim_endian="-DWITH_DEFAULT_TARGET_BYTE_ORDER=BFD_ENDIAN_${wire_endian}"
-	   else
-	     echo "No default endian for target $target" 1>&6
-	     sim_endian="-DWITH_DEFAULT_TARGET_BYTE_ORDER=BFD_ENDIAN_UNKNOWN"
-	   fi
-	 fi;;
-  *)	 AC_MSG_ERROR("Unknown value $enableval for --enable-sim-endian"); sim_endian="";;
-esac
-if test x"$silent" != x"yes" && test x"$sim_endian" != x""; then
-  echo "Setting endian flags = $sim_endian" 6>&1
-fi],
-[if test x"$default_endian" != x; then
-  sim_endian="-DWITH_DEFAULT_TARGET_BYTE_ORDER=BFD_ENDIAN_${default_endian}"
-else
-  if test x"$wire_endian" != x; then
-    sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_${wire_endian}"
-  else
-    sim_endian=
-  fi
-fi])dnl
-])
-AC_SUBST(sim_endian)
+  b*|B*) sim_endian="BFD_ENDIAN_BIG";;
+  l*|L*) sim_endian="BFD_ENDIAN_LITTLE";;
+  yes | no) ;;
+  *)	 AC_MSG_ERROR("Unknown value $enableval for --enable-sim-endian");;
+esac])dnl
+AC_DEFINE_UNQUOTED([WITH_TARGET_BYTE_ORDER],
+		   [${sim_endian:-BFD_ENDIAN_UNKNOWN}], [Sim endian settings])
+AC_MSG_RESULT([${sim_alignment:-no}])
+])dnl
diff --git a/sim/m4/sim_ac_output.m4 b/sim/m4/sim_ac_output.m4
index b18f2540f70b..db6c3ffbbfe9 100644
--- a/sim/m4/sim_ac_output.m4
+++ b/sim/m4/sim_ac_output.m4
@@ -48,9 +48,7 @@  AC_CONFIG_COMMANDS([stamp-h], [echo > stamp-h])
 dnl These are unfortunate.  They are conditionally called by other sim macros
 dnl but always used by common/Make-common.in.  So we have to subst here even
 dnl when the rest of the code is in the respective macros.
-AC_SUBST(sim_alignment)
 AC_SUBST(sim_bitsize)
-AC_SUBST(sim_endian)
 AC_SUBST(sim_float)
 AC_SUBST(sim_scache)
 AC_SUBST(sim_default_model)
diff --git a/sim/m68hc11/configure.ac b/sim/m68hc11/configure.ac
index b2685040f61e..986d8addfd86 100644
--- a/sim/m68hc11/configure.ac
+++ b/sim/m68hc11/configure.ac
@@ -5,7 +5,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 SIM_AC_COMMON
 
 dnl Options available in this module
-SIM_AC_OPTION_ENDIAN(BIG)
 SIM_AC_OPTION_HARDWARE(\
   m68hc11 m68hc11sio m68hc11eepr m68hc11tim m68hc11spi nvram)
 
diff --git a/sim/m68hc11/interp.c b/sim/m68hc11/interp.c
index d4b2531abea0..7d28f40d71dd 100644
--- a/sim/m68hc11/interp.c
+++ b/sim/m68hc11/interp.c
@@ -406,6 +406,9 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback,
 
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_BIG;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
diff --git a/sim/mcore/configure.ac b/sim/mcore/configure.ac
index ec8a87e7d397..08bf6ff8acbc 100644
--- a/sim/mcore/configure.ac
+++ b/sim/mcore/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN
-
 SIM_AC_OUTPUT
diff --git a/sim/microblaze/configure.ac b/sim/microblaze/configure.ac
index ec8a87e7d397..08bf6ff8acbc 100644
--- a/sim/microblaze/configure.ac
+++ b/sim/microblaze/configure.ac
@@ -4,6 +4,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN
-
 SIM_AC_OUTPUT
diff --git a/sim/mips/configure.ac b/sim/mips/configure.ac
index f0b26b4f0acb..03d8f5169e76 100644
--- a/sim/mips/configure.ac
+++ b/sim/mips/configure.ac
@@ -27,25 +27,6 @@  AC_SUBST(SIM_SUBTARGET)
 
 
 
-#
-# Select the byte order of the target
-#
-mips_endian=
-default_endian=
-case "${target}" in
-  mips64el*-*-*)        mips_endian=LITTLE ;;
-  mips64vr*el-*-*)      default_endian=LITTLE ;;
-  mips64*-*-*)          default_endian=BIG ;;
-  mips16*-*-*)          default_endian=BIG ;;
-  mipsisa32*-*-*)       default_endian=BIG ;;
-  mipsisa64*-*-*)       default_endian=BIG ;;
-  mips*-*-*)            default_endian=BIG ;;
-  *)                    default_endian=BIG ;;
-esac
-SIM_AC_OPTION_ENDIAN($mips_endian,$default_endian)
-
-
-
 #
 # Select the bitsize of the target
 #
diff --git a/sim/mn10300/configure.ac b/sim/mn10300/configure.ac
index 6754136f6d96..1083c062df68 100644
--- a/sim/mn10300/configure.ac
+++ b/sim/mn10300/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
 SIM_AC_OPTION_RESERVED_BITS
 SIM_AC_OPTION_BITSIZE(32,31)
 SIM_AC_OPTION_HARDWARE(mn103cpu mn103int mn103tim mn103ser mn103iop)
diff --git a/sim/mn10300/interp.c b/sim/mn10300/interp.c
index 541d4b14668c..b793d3c019fe 100644
--- a/sim/mn10300/interp.c
+++ b/sim/mn10300/interp.c
@@ -94,6 +94,9 @@  sim_open (SIM_OPEN_KIND kind,
 
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     return 0;
diff --git a/sim/moxie/configure.ac b/sim/moxie/configure.ac
index 75a6f37ed265..c7475b799a6f 100644
--- a/sim/moxie/configure.ac
+++ b/sim/moxie/configure.ac
@@ -6,6 +6,4 @@  SIM_AC_COMMON
 
 AC_CHECK_TOOL(DTC, dtc)
 
-SIM_AC_OPTION_ENDIAN(BIG)
-
 SIM_AC_OUTPUT
diff --git a/sim/moxie/interp.c b/sim/moxie/interp.c
index caf9e43deda5..c70215f8c7a4 100644
--- a/sim/moxie/interp.c
+++ b/sim/moxie/interp.c
@@ -1198,6 +1198,9 @@  sim_open (SIM_OPEN_KIND kind, host_callback *cb,
   SIM_DESC sd = sim_state_alloc (kind, cb);
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_BIG;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
diff --git a/sim/msp430/configure.ac b/sim/msp430/configure.ac
index 9cab54296b62..4f2c0ace70bb 100644
--- a/sim/msp430/configure.ac
+++ b/sim/msp430/configure.ac
@@ -23,6 +23,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
-
 SIM_AC_OUTPUT
diff --git a/sim/msp430/msp430-sim.c b/sim/msp430/msp430-sim.c
index 58a54698993f..ed0393317c1b 100644
--- a/sim/msp430/msp430-sim.c
+++ b/sim/msp430/msp430-sim.c
@@ -112,6 +112,9 @@  sim_open (SIM_OPEN_KIND kind,
 
   /* Initialise the simulator.  */
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
+
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
       sim_state_free (sd);
diff --git a/sim/or1k/configure.ac b/sim/or1k/configure.ac
index e09fee1ac13d..ed8538eb1089 100644
--- a/sim/or1k/configure.ac
+++ b/sim/or1k/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(BIG)
 SIM_AC_OPTION_BITSIZE([32], [31], [32])
 SIM_AC_OPTION_SCACHE(16384)
 SIM_AC_OPTION_DEFAULT_MODEL([or1200])
diff --git a/sim/or1k/sim-if.c b/sim/or1k/sim-if.c
index e6342ee42c0f..06d7dcb61b23 100644
--- a/sim/or1k/sim-if.c
+++ b/sim/or1k/sim-if.c
@@ -160,6 +160,9 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback, struct bfd *abfd,
   char c;
   int i;
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_BIG;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
diff --git a/sim/ppc/configure.ac b/sim/ppc/configure.ac
index 5867f60b6281..9d294ae414aa 100644
--- a/sim/ppc/configure.ac
+++ b/sim/ppc/configure.ac
@@ -93,24 +93,6 @@  if test x"$silent" != x"yes"; then
 fi])dnl
 
 
-AC_ARG_ENABLE(sim-endian,
-[  --enable-sim-endian=endian		Specify target byte endian orientation.],
-[case "${enableval}" in
-  yes)	case "$target" in
-	  *powerpc-*) sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_BIG";;
-	  *powerpcle-*) sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_LITTLE";;
-	  *) echo "Unknown target $target" 1>&6; sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_UNKNOWN";;
-	esac;;
-  no)	 sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_UNKNOWN";;
-  b*|B*) sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_BIG";;
-  l*|L*) sim_endian="-DWITH_TARGET_BYTE_ORDER=BFD_ENDIAN_LITTLE";;
-  *)	 AC_MSG_ERROR("Unknown value $enableval for --enable-sim-endian"); sim_endian="";;
-esac
-if test x"$silent" != x"yes" && test x"$sim_endian" != x""; then
-  echo "Setting endian flags = $sim_endian" 6>&1
-fi],[sim_endian=""])dnl
-
-
 AC_ARG_ENABLE(sim-filter,
 [  --enable-sim-filter=rule		Specify filter rules.],
 [case "${enableval}" in
@@ -758,7 +740,6 @@  AC_SUBST(sim_hw_obj)
 AC_SUBST(sim_pk_src)
 AC_SUBST(sim_pk_obj)
 AC_SUBST(sim_inline)
-AC_SUBST(sim_endian)
 AC_SUBST(sim_xor_endian)
 AC_SUBST(sim_smp)
 AC_SUBST(sim_igen_smp)
diff --git a/sim/pru/configure.ac b/sim/pru/configure.ac
index 1846b26e14b0..8e9577c9ea25 100644
--- a/sim/pru/configure.ac
+++ b/sim/pru/configure.ac
@@ -23,6 +23,4 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
-
 SIM_AC_OUTPUT
diff --git a/sim/pru/interp.c b/sim/pru/interp.c
index 38269df44ce7..09045dc4cb0a 100644
--- a/sim/pru/interp.c
+++ b/sim/pru/interp.c
@@ -747,6 +747,7 @@  sim_open (SIM_OPEN_KIND kind, host_callback *cb,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
diff --git a/sim/riscv/configure.ac b/sim/riscv/configure.ac
index 421b5ba7ef54..e866c8f8f4a0 100644
--- a/sim/riscv/configure.ac
+++ b/sim/riscv/configure.ac
@@ -4,8 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
-
 # Select the default model for the target.
 riscv_model=
 case "${target}" in
diff --git a/sim/riscv/interp.c b/sim/riscv/interp.c
index f3754da1ed8a..8b96677634f9 100644
--- a/sim/riscv/interp.c
+++ b/sim/riscv/interp.c
@@ -62,6 +62,9 @@  sim_open (SIM_OPEN_KIND kind, host_callback *callback,
   SIM_DESC sd = sim_state_alloc_extra (kind, callback,
 				       sizeof (struct riscv_sim_state));
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
diff --git a/sim/sh/configure.ac b/sim/sh/configure.ac
index dd975d3259e6..9bf4a4099ee5 100644
--- a/sim/sh/configure.ac
+++ b/sim/sh/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN
 SIM_AC_OPTION_WARNINGS(no)
 
 SIM_AC_OUTPUT
diff --git a/sim/v850/configure.ac b/sim/v850/configure.ac
index 6d586f194fd0..c3cd64627c0a 100644
--- a/sim/v850/configure.ac
+++ b/sim/v850/configure.ac
@@ -4,7 +4,6 @@  AC_CONFIG_MACRO_DIRS([../m4 ../.. ../../config])
 
 SIM_AC_COMMON
 
-SIM_AC_OPTION_ENDIAN(LITTLE)
 SIM_AC_OPTION_RESERVED_BITS
 SIM_AC_OPTION_BITSIZE(32,31)
 
diff --git a/sim/v850/interp.c b/sim/v850/interp.c
index 79c6e0f7ddcc..92c80a814c69 100644
--- a/sim/v850/interp.c
+++ b/sim/v850/interp.c
@@ -197,6 +197,9 @@  sim_open (SIM_OPEN_KIND    kind,
 
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
 
+  /* Set default options before parsing user options.  */
+  current_target_byte_order = BFD_ENDIAN_LITTLE;
+
   /* The cpu data is kept in a separately allocated chunk of memory.  */
   if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     return 0;