On 7/8/25 11:19, Philippe Mathieu-Daudé wrote:
Check endianness at runtime to remove the target-specific
TARGET_BIG_ENDIAN definition.

Signed-off-by: Philippe Mathieu-Daudé <phi...@linaro.org>
---
  include/gdbstub/helpers.h | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/include/gdbstub/helpers.h b/include/gdbstub/helpers.h
index 6f7cc48adcb..1411b136d5c 100644
--- a/include/gdbstub/helpers.h
+++ b/include/gdbstub/helpers.h
@@ -16,6 +16,7 @@
  #error "gdbstub helpers should only be included by target specific code"
  #endif
+#include "qemu/target-info.h"
  #include "exec/tswap.h"
  #include "cpu-param.h"
@@ -55,18 +56,14 @@ static inline int gdb_get_reg64(GByteArray *buf, uint64_t val)
  static inline int gdb_get_reg128(GByteArray *buf, uint64_t val_hi,
                                   uint64_t val_lo)
  {
+    bool be = target_big_endian();
      uint64_t to_quad;
-#if TARGET_BIG_ENDIAN
-    to_quad = tswap64(val_hi);
+
+    to_quad = tswap64(be ? val_hi : val_lo);
      g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-    to_quad = tswap64(val_lo);
+    to_quad = tswap64(be ? val_lo : val_hi);
      g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-#else
-    to_quad = tswap64(val_lo);
-    g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-    to_quad = tswap64(val_hi);
-    g_byte_array_append(buf, (uint8_t *) &to_quad, 8);
-#endif

Don't split the endianness check between here and also within tswap64.

This would be much better as

    uint64_t tmp[2];
    if (target_big_endian()) {
        tmp[0] = cpu_to_be64(val_hi);
        tmp[1] = cpu_to_be64(val_lo);
    } else {
        tmp[0] = cpu_to_le64(val_lo);
        tmp[1] = cpu_to_le64(val_hi);
    }
    g_byte_array_append(buf, (uint8_t *)&tmp, 16);


r~

Reply via email to