[PATCH-for-12.0 0/8] linux-user/aarch64: Support both endianness at runtime
This series lets qemu-aarch64 run both little- and big-endian ELF binaries, retaining qemu-aarch64_be as a compatibility alias. The ELF loader selects the target byte order once, before execution. AArch64 CPU, signal, ABI and vDSO handling then use that selection. Fixed-endian targets and system emulation retain their existing accessors. Runtime-endian TargetInfo data is exposed const to readers, allowing target_big_endian() to be inlined without a separate boolean cache. The writable initializer remains outside LTO, preserving the boundary between ELF initialization and subsequent reads. I compared the complete series against its baseline, with normal and LTO builds and LE/BE guests and did not notice significative speed down. The inspiration comes from previous optimization intent from Richard: https://lore.kernel.org/qemu-devel/20260511190634.472174-1-richard.henderson... Philippe Mathieu-Daudé (8): tests/tcg/aarch64_be: Check data byte order in hello linux-user/aarch64: Move ELF/vDSO endian checks to runtime linux-user/aarch64: Move CPU/signal endian checks to runtime target-info: Support runtime-endian Linux-user targets linux-user: Honor runtime byte order in ABI accesses linux-user: Select target byte order from the ELF header meson: Support Linux-user executable aliases linux-user/aarch64: Unify both byte orders under qemu-aarch64 docs/about/deprecated.rst | 13 ++++++++ docs/user/main.rst | 7 ++++ configs/targets/aarch64-linux-user.mak | 2 ++ configs/targets/aarch64_be-linux-user.mak | 10 ------ meson.build | 39 +++++++++++++++++++++-- include/exec/tswap.h | 11 ++++--- include/qemu/target-info-def.h | 14 ++++++++ include/qemu/target-info.h | 20 ++++++++++++ linux-user/aarch64/target_elf.h | 6 +--- linux-user/aarch64/target_syscall.h | 6 +--- linux-user/arm/target_proc.h | 2 +- linux-user/qemu.h | 17 +++++++++- linux-user/syscall_defs.h | 7 ---- target/arm/internals.h | 2 +- linux-user/aarch64/cpu_loop.c | 12 +++---- linux-user/aarch64/elfload.c | 10 +++++- linux-user/aarch64/signal.c | 20 ++++-------- linux-user/elfload.c | 6 +--- linux-user/main.c | 26 +++++++++++++++ linux-user/syscall.c | 6 ++-- target-info-def.c | 19 +++++++++++ target/arm/gdbstub64.c | 4 +-- target/arm/tcg/translate-sve.c | 2 +- tests/tcg/aarch64_be/hello.c | 11 ++++--- .gitlab-ci.d/buildtest.yml | 2 +- .gitlab-ci.d/crossbuild-template.yml | 4 +-- linux-user/aarch64/meson.build | 10 +++--- tests/tcg/aarch64_be/meson.build | 4 +-- 28 files changed, 208 insertions(+), 84 deletions(-) delete mode 100644 configs/targets/aarch64_be-linux-user.mak -- 2.55.0
Load the write length from guest data instead of an immediate, and fail unless the syscall writes exactly twelve bytes. This detects incorrect CPU data byte order rather than merely accepting the ELF header. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- tests/tcg/aarch64_be/hello.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/tcg/aarch64_be/hello.c b/tests/tcg/aarch64_be/hello.c index a9b2ab45de9b..4816635d1704 100644 --- a/tests/tcg/aarch64_be/hello.c +++ b/tests/tcg/aarch64_be/hello.c @@ -14,9 +14,10 @@ int write(int fd, char *buf, int len) register int x2 __asm__("x2") = len; register int x8 __asm__("x8") = __NR_write; - asm volatile("svc #0" : : "r"(x0), "r"(x1), "r"(x2), "r"(x8)); + asm volatile("svc #0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x8) + : "memory"); - return len; + return x0; } void exit(int ret) @@ -30,6 +31,8 @@ void exit(int ret) void _start(void) { - write(1, "Hello World\n", 12); - exit(0); + /* Force a data load to check CPU data byte order. */ + static volatile int len = 12; + + exit(write(1, "Hello World\n", len) != 12); } -- 2.55.0
Use target_big_endian() for the platform name, uname machine, /proc/cpuinfo suffix and vDSO selection. Give the generated vDSOs distinct symbols so both can be included. Preserve the behavior of the existing fixed-endian targets. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- linux-user/aarch64/target_elf.h | 6 +----- linux-user/aarch64/target_syscall.h | 6 +----- linux-user/arm/target_proc.h | 2 +- linux-user/aarch64/elfload.c | 10 +++++++++- linux-user/aarch64/meson.build | 10 ++++------ 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/linux-user/aarch64/target_elf.h b/linux-user/aarch64/target_elf.h index 4cdeb64b0d4e..ebd23cfbceda 100644 --- a/linux-user/aarch64/target_elf.h +++ b/linux-user/aarch64/target_elf.h @@ -27,10 +27,6 @@ typedef struct target_elf_gregset_t { struct target_user_pt_regs pt; } target_elf_gregset_t; -#if TARGET_BIG_ENDIAN -# define VDSO_HEADER "vdso-be.c.inc" -#else -# define VDSO_HEADER "vdso-le.c.inc" -#endif +#define HAVE_VDSO_IMAGE_INFO 1 #endif diff --git a/linux-user/aarch64/target_syscall.h b/linux-user/aarch64/target_syscall.h index bd05f6c7fe5d..f3c570a5b2eb 100644 --- a/linux-user/aarch64/target_syscall.h +++ b/linux-user/aarch64/target_syscall.h @@ -1,11 +1,7 @@ #ifndef AARCH64_TARGET_SYSCALL_H #define AARCH64_TARGET_SYSCALL_H -#if TARGET_BIG_ENDIAN -#define UNAME_MACHINE "aarch64_be" -#else -#define UNAME_MACHINE "aarch64" -#endif +#define UNAME_MACHINE (target_big_endian() ? "aarch64_be" : "aarch64") #define UNAME_MINIMUM_RELEASE "3.8.0" #define TARGET_CLONE_BACKWARDS #define TARGET_MCL_CURRENT 1 diff --git a/linux-user/arm/target_proc.h b/linux-user/arm/target_proc.h index 13c33d44d5ed..d3186ca655cf 100644 --- a/linux-user/arm/target_proc.h +++ b/linux-user/arm/target_proc.h @@ -25,7 +25,7 @@ static int open_cpuinfo(CPUArchState *cpu_env, int fd) #endif arch = 8; - elf_name = "v8" END_SUFFIX; + elf_name = target_big_endian() ? "v8b" : "v8l"; midr_rev = FIELD_EX32(midr, MIDR_EL1, REVISION); midr_part = FIELD_EX32(midr, MIDR_EL1, PARTNUM); midr_var = FIELD_EX32(midr, MIDR_EL1, VARIANT); diff --git a/linux-user/aarch64/elfload.c b/linux-user/aarch64/elfload.c index f87f22f6903a..8e75da826e62 100644 --- a/linux-user/aarch64/elfload.c +++ b/linux-user/aarch64/elfload.c @@ -379,7 +379,15 @@ const char *elf_hwcap2_str(uint32_t bit) const char *get_elf_platform(CPUState *cs) { - return TARGET_BIG_ENDIAN ? "aarch64_be" : "aarch64"; + return target_big_endian() ? "aarch64_be" : "aarch64"; +} + +#include "vdso-be.c.inc" +#include "vdso-le.c.inc" + +const VdsoImageInfo *get_vdso_image_info(uint32_t elf_flags) +{ + return target_big_endian() ? &vdso_be_image_info : &vdso_le_image_info; } bool arch_parse_elf_property(uint32_t pr_type, uint32_t pr_datasz, diff --git a/linux-user/aarch64/meson.build b/linux-user/aarch64/meson.build index f25a67a21ef6..a5f9a47a9a27 100644 --- a/linux-user/aarch64/meson.build +++ b/linux-user/aarch64/meson.build @@ -1,12 +1,10 @@ -# TARGET_BIG_ENDIAN is defined to 'n' for little-endian; which means it -# is always true as far as source_set.apply() is concerned. Always build -# both header files and include the right one via #if. - vdso_be_inc = gen_vdso.process('vdso-be.so', - extra_args: ['-r', '__kernel_rt_sigreturn']) + extra_args: ['-r', '__kernel_rt_sigreturn', + '-p', 'vdso_be']) vdso_le_inc = gen_vdso.process('vdso-le.so', - extra_args: ['-r', '__kernel_rt_sigreturn']) + extra_args: ['-r', '__kernel_rt_sigreturn', + '-p', 'vdso_le']) linux_user_ss.add(when: 'TARGET_AARCH64', if_true: [vdso_be_inc, vdso_le_inc]) -- 2.55.0
Use target_big_endian() for CPU endian initialization, FPSIMD signal-frame ordering and CPU-versus-ABI byte-order checks. Preserve the behavior of the existing fixed-endian targets. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- target/arm/internals.h | 2 +- linux-user/aarch64/cpu_loop.c | 12 ++++++------ linux-user/aarch64/signal.c | 20 ++++++-------------- target/arm/gdbstub64.c | 4 ++-- target/arm/tcg/translate-sve.c | 2 +- 5 files changed, 16 insertions(+), 24 deletions(-) diff --git a/target/arm/internals.h b/target/arm/internals.h index 033d6d66da3a..1381ef9638a3 100644 --- a/target/arm/internals.h +++ b/target/arm/internals.h @@ -588,7 +588,7 @@ static inline bool arm_cpu_data_is_big_endian(CPUARMState *env) #ifdef CONFIG_USER_ONLY static inline bool arm_cpu_bswap_data(CPUARMState *env) { - return TARGET_BIG_ENDIAN ^ arm_cpu_data_is_big_endian(env); + return target_big_endian() ^ arm_cpu_data_is_big_endian(env); } #endif diff --git a/linux-user/aarch64/cpu_loop.c b/linux-user/aarch64/cpu_loop.c index e7f643d69d56..91ebf4e0778f 100644 --- a/linux-user/aarch64/cpu_loop.c +++ b/linux-user/aarch64/cpu_loop.c @@ -242,13 +242,13 @@ void init_main_thread(CPUState *cs, struct image_info *info) env->pc = info->entry & ~0x3ULL; env->xregs[31] = info->start_stack; -#if TARGET_BIG_ENDIAN - env->cp15.sctlr_el[1] |= SCTLR_E0E; - for (int i = 1; i < 4; ++i) { - env->cp15.sctlr_el[i] |= SCTLR_EE; + if (target_big_endian()) { + env->cp15.sctlr_el[1] |= SCTLR_E0E; + for (int i = 1; i < 4; ++i) { + env->cp15.sctlr_el[i] |= SCTLR_EE; + } + arm_rebuild_hflags(env); } - arm_rebuild_hflags(env); -#endif if (cpu_isar_feature(aa64_pauth, cpu)) { qemu_guest_getrandom_nofail(&env->keys, sizeof(env->keys)); diff --git a/linux-user/aarch64/signal.c b/linux-user/aarch64/signal.c index 899fce76436c..f24fcddfec3e 100644 --- a/linux-user/aarch64/signal.c +++ b/linux-user/aarch64/signal.c @@ -208,6 +208,7 @@ static void target_setup_fpsimd_record(struct target_fpsimd_context *fpsimd, CPUARMState *env) { int i; + int big_endian = target_big_endian(); __put_user(TARGET_FPSIMD_MAGIC, &fpsimd->head.magic); __put_user(sizeof(struct target_fpsimd_context), &fpsimd->head.size); @@ -216,13 +217,8 @@ static void target_setup_fpsimd_record(struct target_fpsimd_context *fpsimd, for (i = 0; i < 32; i++) { uint64_t *q = aa64_vfp_qreg(env, i); -#if TARGET_BIG_ENDIAN - __put_user(q[0], &fpsimd->vregs[i * 2 + 1]); - __put_user(q[1], &fpsimd->vregs[i * 2]); -#else - __put_user(q[0], &fpsimd->vregs[i * 2]); - __put_user(q[1], &fpsimd->vregs[i * 2 + 1]); -#endif + __put_user(q[0], &fpsimd->vregs[i * 2 + big_endian]); + __put_user(q[1], &fpsimd->vregs[i * 2 + !big_endian]); } } @@ -402,6 +398,7 @@ static void target_restore_fpsimd_record(CPUARMState *env, { uint32_t fpsr, fpcr; int i; + int big_endian = target_big_endian(); __get_user(fpsr, &fpsimd->fpsr); vfp_set_fpsr(env, fpsr); @@ -410,13 +407,8 @@ static void target_restore_fpsimd_record(CPUARMState *env, for (i = 0; i < 32; i++) { uint64_t *q = aa64_vfp_qreg(env, i); -#if TARGET_BIG_ENDIAN - __get_user(q[0], &fpsimd->vregs[i * 2 + 1]); - __get_user(q[1], &fpsimd->vregs[i * 2]); -#else - __get_user(q[0], &fpsimd->vregs[i * 2]); - __get_user(q[1], &fpsimd->vregs[i * 2 + 1]); -#endif + __get_user(q[0], &fpsimd->vregs[i * 2 + big_endian]); + __get_user(q[1], &fpsimd->vregs[i * 2 + !big_endian]); } } diff --git a/target/arm/gdbstub64.c b/target/arm/gdbstub64.c index 0c3e5b30bd6a..ee6ed8296e33 100644 --- a/target/arm/gdbstub64.c +++ b/target/arm/gdbstub64.c @@ -122,8 +122,8 @@ int aarch64_gdb_set_fpu_reg(CPUState *cs, uint8_t *buf, int reg) * On the wire these are target-endian 128 bit values. * In the CPU state these are host-order uint64_t values * with the least-significant one first. This means they're - * the other way around for target_big_endian() (which is - * only true for us for aarch64_be-linux-user). + * the other way around for target_big_endian(), which reflects + * the executable's byte order in Linux-user emulation. */ if (target_big_endian()) { q[1] = ldq_p(buf); diff --git a/target/arm/tcg/translate-sve.c b/target/arm/tcg/translate-sve.c index 10375577b1e0..6c4cf29238c6 100644 --- a/target/arm/tcg/translate-sve.c +++ b/target/arm/tcg/translate-sve.c @@ -5165,7 +5165,7 @@ void gen_sve_str(DisasContext *s, TCGv_ptr base, int vofs, /* Note that unpredicated load/store of vector/predicate registers * are defined as a stream of bytes, which equates to little-endian * operations on larger quantities. There is no nice way to force - * a little-endian store for aarch64_be-linux-user out of line. + * a little-endian store for big-endian Linux-user guests out of line. * * Attempt to keep code expansion to a minimum by limiting the * amount of unrolling done. -- 2.55.0
Add a write-once ABI byte-order setter for runtime-endian Linux-user targets. Expose const data to readers and inline runtime endian checks, keeping its writable initializer outside LTO. Retain immutable data and compile-time byte swapping for fixed-endian targets. Restrict TARGET_RUNTIME_ENDIAN to Linux-user configurations. Inspired-by: Richard Henderson <richard.henderson@linaro.org> Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- meson.build | 26 ++++++++++++++++++++++++-- include/exec/tswap.h | 11 +++++++---- include/qemu/target-info-def.h | 14 ++++++++++++++ include/qemu/target-info.h | 20 ++++++++++++++++++++ target-info-def.c | 19 +++++++++++++++++++ 5 files changed, 84 insertions(+), 6 deletions(-) diff --git a/meson.build b/meson.build index d7bcb581c181..04cf5d77f188 100644 --- a/meson.build +++ b/meson.build @@ -3342,6 +3342,11 @@ foreach target : target_dirs config_target += keyval.load('configs/targets' / target + '.mak') + if 'TARGET_RUNTIME_ENDIAN' in config_target + assert(target.endswith('linux-user'), + 'Runtime target endianness is only supported for Linux-user') + endif + target_kconfig = [] foreach sym: accelerators if sym == 'CONFIG_TCG' or target in accelerator_targets.get(sym, []) @@ -4400,8 +4405,6 @@ foreach target : target_dirs endif endif - arch_srcs += files('target-info-def.c') - if target_base_arch in target_arch t = target_arch[target_base_arch].apply(config_target, strict: false) arch_srcs += t.sources() @@ -4470,6 +4473,25 @@ foreach target : target_dirs lib_deps += dep.partial_dependency(compile_args: true, includes: true) endforeach + # Keep the writable runtime-endian definition opaque to const readers, + # as for page-vary-common.c above. + target_info = files('target-info-def.c') + if get_option('b_lto') and 'TARGET_RUNTIME_ENDIAN' in config_target + target_info_flags = ['-fno-lto'] + if get_option('cfi') + target_info_flags += '-fno-sanitize=cfi-icall' + endif + target_info = static_library('target-info-' + target, + sources: target_info + genh + config_target_h[target], + dependencies: lib_deps, + include_directories: target_inc, + c_args: c_args + target_info_flags, + build_by_default: false) + objects += target_info.extract_all_objects(recursive: false) + else + arch_srcs += target_info + endif + lib = static_library('qemu-' + target, sources: arch_srcs + genh, dependencies: lib_deps, diff --git a/include/exec/tswap.h b/include/exec/tswap.h index 17ac5444544d..869619bac314 100644 --- a/include/exec/tswap.h +++ b/include/exec/tswap.h @@ -13,14 +13,17 @@ #include "hw/core/cpu.h" /* - * If we're in target-specific code, we can hard-code the swapping - * condition, otherwise we have to do (slower) run-time checks. + * For fixed-endian target-specific code, hard-code the swapping condition. + * Otherwise use run-time checks. */ #ifdef COMPILING_PER_TARGET +#ifndef TARGET_RUNTIME_ENDIAN #define target_needs_bswap() (HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN) -#else +#endif +#endif +#ifndef target_needs_bswap #define target_needs_bswap() (HOST_BIG_ENDIAN != target_big_endian()) -#endif /* COMPILING_PER_TARGET */ +#endif #if defined(CONFIG_USER_ONLY) \ || !defined(TARGET_NOT_USING_LEGACY_NATIVE_ENDIAN_API) diff --git a/include/qemu/target-info-def.h b/include/qemu/target-info-def.h index 8019d2c7196e..34058955edec 100644 --- a/include/qemu/target-info-def.h +++ b/include/qemu/target-info-def.h @@ -32,6 +32,20 @@ typedef struct TargetInfo { bool page_bits_vary; } TargetInfo; +#ifdef COMPILING_PER_TARGET +#ifdef TARGET_RUNTIME_ENDIAN +/* + * Linux-user selects the ELF byte order before creating CPUs or guest threads. + * The data is then immutable; its writable definition must stay outside LTO. + */ +#ifdef IN_TARGET_INFO_DEF +extern TargetInfo target_info_stub; +#else +extern const TargetInfo target_info_stub; +#endif +#endif +#endif + /** * target_info: * diff --git a/include/qemu/target-info.h b/include/qemu/target-info.h index 2cd4cc5671b5..6ed4cfda8d7b 100644 --- a/include/qemu/target-info.h +++ b/include/qemu/target-info.h @@ -9,6 +9,12 @@ #ifndef QEMU_TARGET_INFO_H #define QEMU_TARGET_INFO_H +#ifdef COMPILING_PER_TARGET +#ifdef TARGET_RUNTIME_ENDIAN +#include "qemu/target-info-def.h" +#endif +#endif + /** * target_name: * @@ -40,7 +46,21 @@ const char *target_cpu_type(void); * the target, so please do *not* use this function unless you know very * well what you are doing! */ +#ifdef COMPILING_PER_TARGET +#ifdef TARGET_RUNTIME_ENDIAN +static inline bool target_big_endian(void) +{ + return target_info_stub.endianness == ENDIAN_MODE_BIG; +} + +/* Select the Linux-user ABI byte order once, before CPUs or guest threads. */ +void target_set_big_endian(bool big_endian); +#else bool target_big_endian(void); +#endif +#else +bool target_big_endian(void); +#endif /** * target_heterogeneous: diff --git a/target-info-def.c b/target-info-def.c index bcf9cfe1e437..7580993e0bb3 100644 --- a/target-info-def.c +++ b/target-info-def.c @@ -6,6 +6,8 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ +#define IN_TARGET_INFO_DEF 1 + #include "qemu/osdep.h" #include "qemu/target-info.h" #include "qemu/target-info-def.h" @@ -24,7 +26,12 @@ QEMU_BUILD_BUG_ON(offsetof(ArchCPU, env) != sizeof(CPUState)); QEMU_BUILD_BUG_ON(TARGET_PAGE_BITS < TARGET_PAGE_BITS_MIN); #endif +#ifdef TARGET_RUNTIME_ENDIAN +/* This writable definition must not be compiled with -flto. */ +TargetInfo target_info_stub = { +#else static const TargetInfo target_info_stub = { +#endif .target_name = TARGET_NAME, .target_arch = glue(SYS_EMU_TARGET_, TARGET_ARCH_VARIANT), .long_bits = TARGET_LONG_BITS, @@ -42,3 +49,15 @@ static const TargetInfo target_info_stub = { }; target_info_init(target_info_stub) + +#ifdef TARGET_RUNTIME_ENDIAN +void target_set_big_endian(bool big_endian) +{ + static bool endian_decided; + + assert(!endian_decided); + endian_decided = true; + target_info_stub.endianness = big_endian ? ENDIAN_MODE_BIG + : ENDIAN_MODE_LITTLE; +} +#endif -- 2.55.0
Use the selected ABI byte order for ELF validation and core headers, user-memory accesses, signal masks and /proc/net/route conversion. Retain the fixed-endian paths for targets without runtime selection. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- linux-user/qemu.h | 17 ++++++++++++++++- linux-user/syscall_defs.h | 7 ------- linux-user/elfload.c | 6 +----- linux-user/syscall.c | 6 +++--- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/linux-user/qemu.h b/linux-user/qemu.h index ef7aed2daec6..53567a6c0363 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -254,7 +254,22 @@ static inline bool access_ok(CPUState *cpu, int type, } while (0) -#if TARGET_BIG_ENDIAN +#ifdef TARGET_RUNTIME_ENDIAN +# define __put_user(x, hptr) do { \ + if (target_big_endian()) { \ + __put_user_e(x, hptr, be); \ + } else { \ + __put_user_e(x, hptr, le); \ + } \ +} while (0) +# define __get_user(x, hptr) do { \ + if (target_big_endian()) { \ + __get_user_e(x, hptr, be); \ + } else { \ + __get_user_e(x, hptr, le); \ + } \ +} while (0) +#elif TARGET_BIG_ENDIAN # define __put_user(x, hptr) __put_user_e(x, hptr, be) # define __get_user(x, hptr) __get_user_e(x, hptr, be) #else diff --git a/linux-user/syscall_defs.h b/linux-user/syscall_defs.h index f5831a6baddc..c9a1853c9e90 100644 --- a/linux-user/syscall_defs.h +++ b/linux-user/syscall_defs.h @@ -462,19 +462,12 @@ typedef struct { abi_ulong sig[TARGET_NSIG_WORDS]; } target_sigset_t; -#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN static inline void tswap_sigset(target_sigset_t *d, const target_sigset_t *s) { int i; for(i = 0;i < TARGET_NSIG_WORDS; i++) d->sig[i] = tswapal(s->sig[i]); } -#else -static inline void tswap_sigset(target_sigset_t *d, const target_sigset_t *s) -{ - *d = *s; -} -#endif static inline void target_siginitset(target_sigset_t *d, abi_ulong set) { diff --git a/linux-user/elfload.c b/linux-user/elfload.c index c188fa03c31f..c76afb77bbdc 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -102,11 +102,7 @@ int info_is_fdpic(struct image_info *info) return info->personality == PER_LINUX_FDPIC; } -#if TARGET_BIG_ENDIAN -#define ELF_DATA ELFDATA2MSB -#else -#define ELF_DATA ELFDATA2LSB -#endif +#define ELF_DATA (target_big_endian() ? ELFDATA2MSB : ELFDATA2LSB) #ifdef USE_UID16 typedef abi_ushort target_uid_t; diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 2fc61b3eabcc..9dec129e1d4f 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8778,7 +8778,7 @@ void target_exception_dump(CPUArchState *env, const char *fmt, int code) #include "target_proc.h" -#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \ +#if defined(TARGET_RUNTIME_ENDIAN) || HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN || \ defined(HAVE_ARCH_PROC_CPUINFO) || \ defined(HAVE_ARCH_PROC_HARDWARE) static int is_proc(const char *filename, const char *entry) @@ -8787,7 +8787,7 @@ static int is_proc(const char *filename, const char *entry) } #endif -#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN +#if defined(TARGET_RUNTIME_ENDIAN) || HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN static int open_net_route(CPUArchState *cpu_env, int fd) { FILE *fp; @@ -8850,7 +8850,7 @@ static int maybe_do_fake_open(CPUArchState *cpu_env, int dirfd, { "stat", open_self_stat, is_proc_myself }, { "auxv", open_self_auxv, is_proc_myself }, { "cmdline", open_self_cmdline, is_proc_myself }, -#if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN +#if defined(TARGET_RUNTIME_ENDIAN) || HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN { "/proc/net/route", open_net_route, is_proc }, #endif #if defined(HAVE_ARCH_PROC_CPUINFO) -- 2.55.0
Read EI_DATA from the executable descriptor before selecting the CPU and loading the image. This also covers binfmt open-binary execution and an explicitly selected CPU model. Reject invalid ELF byte-order encodings without using the invoked emulator name to select an ABI. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- linux-user/main.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/linux-user/main.c b/linux-user/main.c index 2f0c0a50c8a4..71718f974f6b 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -22,6 +22,7 @@ #include "qemu/units.h" #include "qemu/accel.h" #include "qemu/version.h" +#include "elf.h" #include <sys/syscall.h> #include <sys/resource.h> #include <sys/shm.h> @@ -769,6 +770,31 @@ int main(int argc, char **argv, char **envp) } } +#ifdef TARGET_RUNTIME_ENDIAN + { + unsigned char ident[EI_NIDENT]; + ssize_t len = pread(execfd, ident, sizeof(ident), 0); + + if (len < 0) { + error_report("Error reading %s: %s", exec_path, strerror(errno)); + exit(EXIT_FAILURE); + } + if (len == sizeof(ident) && memcmp(ident, ELFMAG, SELFMAG) == 0) { + switch (ident[EI_DATA]) { + case ELFDATA2LSB: + target_set_big_endian(false); + break; + case ELFDATA2MSB: + target_set_big_endian(true); + break; + default: + error_report("%s: Invalid ELF byte order", exec_path); + exit(EXIT_FAILURE); + } + } + } +#endif + /* Resolve executable file name to full path name */ /* Keep how we started the program in exec_path, e.g. "./my_program" */ /* Store real path in real_exec_path, e.g. "/usr/local/bin/my_program" */ -- 2.55.0
Accept TARGET_USER_ALIASES in target configurations and create relative symlinks in both the build and install trees. Depend on the canonical emulator instead of linking a second binary. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- meson.build | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index 04cf5d77f188..86b942640abf 100644 --- a/meson.build +++ b/meson.build @@ -3297,7 +3297,7 @@ host_kconfig = \ (hv_balloon ? ['CONFIG_HV_BALLOON_POSSIBLE=y'] : []) + \ (have_rust ? ['CONFIG_HAVE_RUST=y'] : []) -ignored = [ 'TARGET_XML_FILES', 'TARGET_ABI_DIR' ] +ignored = [ 'TARGET_XML_FILES', 'TARGET_ABI_DIR', 'TARGET_USER_ALIASES' ] default_targets = 'CONFIG_DEFAULT_TARGETS' in config_host actual_target_dirs = [] @@ -4580,6 +4580,17 @@ foreach target : target_dirs emulators += {exe['name']: emulator} endif + foreach alias : config_target.get('TARGET_USER_ALIASES', '').split() + alias_name = 'qemu-' + alias + custom_target(alias_name, + output: alias_name, + depends: emulator, + build_by_default: true, + command: [find_program('ln'), '-sf', exe['name'], '@OUTPUT@']) + install_symlink(alias_name, pointing_to: exe['name'], + install_dir: get_option('bindir')) + endforeach + traceable += [{ 'exe': exe['name'], 'probe-prefix': 'qemu.' + target_type + '.' + target_name, -- 2.55.0
Enable runtime byte-order selection for aarch64-linux-user and retain qemu-aarch64_be as a compatibility symlink. Deprecate the symlink since 12.0 and recommend qemu-aarch64 for both ELF byte orders. Remove the separate aarch64_be-linux-user build target and obsolete CI exclusions. Run the existing big-endian hello test through qemu-aarch64 while retaining its big-endian fixture toolchain. Document that executable name compatibility does not preserve the removed target-list spelling. Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- docs/about/deprecated.rst | 13 +++++++++++++ docs/user/main.rst | 7 +++++++ configs/targets/aarch64-linux-user.mak | 2 ++ configs/targets/aarch64_be-linux-user.mak | 10 ---------- .gitlab-ci.d/buildtest.yml | 2 +- .gitlab-ci.d/crossbuild-template.yml | 4 ++-- tests/tcg/aarch64_be/meson.build | 4 ++-- 7 files changed, 27 insertions(+), 15 deletions(-) delete mode 100644 configs/targets/aarch64_be-linux-user.mak diff --git a/docs/about/deprecated.rst b/docs/about/deprecated.rst index 0891bbc44ded..20eb4268f640 100644 --- a/docs/about/deprecated.rst +++ b/docs/about/deprecated.rst @@ -438,6 +438,19 @@ It was implemented as a no-op instruction in TCG up to QEMU 9.0, but only with ``-cpu max`` (which does not guarantee migration compatibility across versions). +linux-user mode executables +--------------------------- + +.. _deprecated-qemu-aarch64-be: + +``qemu-aarch64_be`` executable name (since 12.0) +'''''''''''''''''''''''''''''''''''''''''''''''' + +The ``qemu-aarch64_be`` compatibility symlink is deprecated. +Use ``qemu-aarch64`` instead for both little- and big-endian +Linux ELF executables. The emulator selects the byte order from +the ELF header, regardless of the executable name used to invoke it. + linux-user mode CPUs -------------------- diff --git a/docs/user/main.rst b/docs/user/main.rst index a8ddf9142495..9dcea8dac4d0 100644 --- a/docs/user/main.rst +++ b/docs/user/main.rst @@ -65,6 +65,13 @@ emulator. Linux User space emulator ------------------------- +For AArch64, ``qemu-aarch64`` runs both little- and big-endian Linux ELF +executables, selecting the byte order from the ELF header. ``qemu-aarch64_be`` +is a :ref:`deprecated compatibility symlink <deprecated-qemu-aarch64-be>` +to the same emulator; its name does not override the ELF byte order. +Build both names with ``--target-list=aarch64-linux-user``; the separate +``aarch64_be-linux-user`` build target is no longer supported. + Command line options ~~~~~~~~~~~~~~~~~~~~ diff --git a/configs/targets/aarch64-linux-user.mak b/configs/targets/aarch64-linux-user.mak index ddfacc092ddf..efe372021ce8 100644 --- a/configs/targets/aarch64-linux-user.mak +++ b/configs/targets/aarch64-linux-user.mak @@ -1,5 +1,7 @@ TARGET_ARCH=aarch64 TARGET_BASE_ARCH=arm +TARGET_RUNTIME_ENDIAN=y +TARGET_USER_ALIASES=aarch64_be TARGET_XML_FILES= aarch64-core.xml aarch64-fpu.xml aarch64-pauth.xml aarch64-mte.xml aarch64-sme2.xml TARGET_HAS_BFLT=y CONFIG_SEMIHOSTING=y diff --git a/configs/targets/aarch64_be-linux-user.mak b/configs/targets/aarch64_be-linux-user.mak deleted file mode 100644 index 83209fa2ae54..000000000000 --- a/configs/targets/aarch64_be-linux-user.mak +++ /dev/null @@ -1,10 +0,0 @@ -TARGET_ARCH=aarch64 -TARGET_BASE_ARCH=arm -TARGET_BIG_ENDIAN=y -TARGET_XML_FILES= aarch64-core.xml aarch64-fpu.xml aarch64-pauth.xml aarch64-mte.xml aarch64-sme2.xml -TARGET_HAS_BFLT=y -CONFIG_SEMIHOSTING=y -CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y -TARGET_SYSTBL_ABI=common,64,renameat,rlimit,memfd_secret -TARGET_SYSTBL=syscall_64.tbl -TARGET_LONG_BITS=64 diff --git a/.gitlab-ci.d/buildtest.yml b/.gitlab-ci.d/buildtest.yml index b1e95395d0c3..a603c93cb799 100644 --- a/.gitlab-ci.d/buildtest.yml +++ b/.gitlab-ci.d/buildtest.yml @@ -451,7 +451,7 @@ clang-user: variables: IMAGE: debian-all-test-cross CONFIGURE_ARGS: --cc=clang --cxx=clang++ --disable-system --enable-ubsan - --target-list-exclude=alpha-linux-user,microblazeel-linux-user,aarch64_be-linux-user,i386-linux-user,m68k-linux-user,mipsn32el-linux-user,xtensaeb-linux-user + --target-list-exclude=alpha-linux-user,microblazeel-linux-user,i386-linux-user,m68k-linux-user,mipsn32el-linux-user,xtensaeb-linux-user --extra-cflags=-fno-sanitize-recover=undefined MAKE_CHECK_ARGS: check-unit check-tcg diff --git a/.gitlab-ci.d/crossbuild-template.yml b/.gitlab-ci.d/crossbuild-template.yml index 94423814a751..32c11f4895b5 100644 --- a/.gitlab-ci.d/crossbuild-template.yml +++ b/.gitlab-ci.d/crossbuild-template.yml @@ -70,8 +70,8 @@ - cd build - section_start configure "Running configure" - ../configure --enable-werror --disable-docs $QEMU_CONFIGURE_OPTS - --disable-system --target-list-exclude="aarch64_be-linux-user - alpha-linux-user m68k-linux-user microblazeel-linux-user + --disable-system --target-list-exclude="alpha-linux-user + m68k-linux-user microblazeel-linux-user or1k-linux-user ppc-linux-user sparc-linux-user xtensa-linux-user $CROSS_SKIP_TARGETS" - section_end configure diff --git a/tests/tcg/aarch64_be/meson.build b/tests/tcg/aarch64_be/meson.build index c1873cd5606c..08d29a94e702 100644 --- a/tests/tcg/aarch64_be/meson.build +++ b/tests/tcg/aarch64_be/meson.build @@ -6,7 +6,7 @@ tests += { 'hello.c': {'cflags': ['-mbig-endian', '-ffreestanding', '-nostdlib']}, } -if 'qemu-aarch64_be' in emulators +if 'qemu-aarch64' in emulators tcg_tests += { 'aarch64_be-linux-user': { 'cc': 'aarch64-linux-gnu-gcc', @@ -14,7 +14,7 @@ if 'qemu-aarch64_be' in emulators 'cc_dockerfile': 'debian-all-test-cross', 'folder': 'aarch64_be', 'gdb_arch': 'aarch64_be', - 'qemu': emulators['qemu-aarch64_be'], + 'qemu': emulators['qemu-aarch64'], 'tests': tests, } } -- 2.55.0
On 2026-11-12 15:03, Philippe Mathieu-Daudé wrote:
Enable runtime byte-order selection for aarch64-linux-user and retain qemu-aarch64_be as a compatibility symlink. Deprecate the symlink since 12.0 and recommend qemu-aarch64 for both ELF byte orders. Remove the separate aarch64_be-linux-user build target and obsolete CI exclusions.
Run the existing big-endian hello test through qemu-aarch64 while retaining its big-endian fixture toolchain. Document that executable name compatibility does not preserve the removed target-list spelling.
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> --- docs/about/deprecated.rst | 13 +++++++++++++ docs/user/main.rst | 7 +++++++ configs/targets/aarch64-linux-user.mak | 2 ++ configs/targets/aarch64_be-linux-user.mak | 10 ---------- .gitlab-ci.d/buildtest.yml | 2 +- .gitlab-ci.d/crossbuild-template.yml | 4 ++-- tests/tcg/aarch64_be/meson.build | 4 ++-- 7 files changed, 27 insertions(+), 15 deletions(-) delete mode 100644 configs/targets/aarch64_be-linux-user.mak
diff --git a/docs/about/deprecated.rst b/docs/about/deprecated.rst index 0891bbc44ded..20eb4268f640 100644 --- a/docs/about/deprecated.rst +++ b/docs/about/deprecated.rst @@ -438,6 +438,19 @@ It was implemented as a no-op instruction in TCG up to QEMU 9.0, but only with ``-cpu max`` (which does not guarantee migration compatibility across versions).
+linux-user mode executables +--------------------------- + +.. _deprecated-qemu-aarch64-be: + +``qemu-aarch64_be`` executable name (since 12.0) +'''''''''''''''''''''''''''''''''''''''''''''''' + +The ``qemu-aarch64_be`` compatibility symlink is deprecated. +Use ``qemu-aarch64`` instead for both little- and big-endian +Linux ELF executables. The emulator selects the byte order from +the ELF header, regardless of the executable name used to invoke it. + linux-user mode CPUs --------------------
diff --git a/docs/user/main.rst b/docs/user/main.rst index a8ddf9142495..9dcea8dac4d0 100644 --- a/docs/user/main.rst +++ b/docs/user/main.rst @@ -65,6 +65,13 @@ emulator. Linux User space emulator -------------------------
+For AArch64, ``qemu-aarch64`` runs both little- and big-endian Linux ELF +executables, selecting the byte order from the ELF header. ``qemu-aarch64_be`` +is a :ref:`deprecated compatibility symlink <deprecated-qemu-aarch64-be>` +to the same emulator; its name does not override the ELF byte order. +Build both names with ``--target-list=aarch64-linux-user``; the separate +``aarch64_be-linux-user`` build target is no longer supported. +
Squashing: -- >8 -- diff --git a/configure b/configure index af14a9c23e..821a887a31 100755 --- a/configure +++ b/configure @@ -1107,7 +1107,14 @@ else *" $target "*) ;; *) - error_exit "Unknown target name '$target'" + case "$target" in + aarch64_be-linux-user) + error_exit "Target '$target' is no longer supported; use 'aarch64-linux-user' instead" + ;; + *) + error_exit "Unknown target name '$target'" + ;; + esac ;; esac done ---
participants (1)
-
Philippe Mathieu-Daudé