From a76c3fc7122954842e2512afad81ebb5894c5572 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Thu, 29 Sep 2022 16:56:34 -0700 Subject: [PATCH] Add logic to build.rs to autodetect whether we need to use thumb-mode. `#[cfg(target_feature = "thumb-mode")]` isn't available on stable, so use a build.rs script to detect whether we can use `r7` in inline asm on ARM targets. --- build.rs | 10 ++++++++++ src/backend/linux_raw/arch/inline/mod.rs | 10 ++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/build.rs b/build.rs index aac91e5e5..56c6b1557 100644 --- a/build.rs +++ b/build.rs @@ -101,6 +101,11 @@ fn main() { } } + // Detect whether the compiler requires us to use thumb mode on ARM. + if arch == "arm" && use_thumb_mode() { + use_feature("thumb_mode"); + } + println!("cargo:rerun-if-env-changed=CARGO_CFG_RUSTIX_USE_EXPERIMENTAL_ASM"); } @@ -151,6 +156,11 @@ fn link_in_librustix_outline(arch: &str, asm_name: &str) { } } +fn use_thumb_mode() -> bool { + // In thumb mode, r7 is reserved. + !can_compile("pub unsafe fn f() { core::arch::asm!(\"udf #16\", in(\"r7\") 0); }") +} + fn use_feature_or_nothing(feature: &str) { if has_feature(feature) { use_feature(feature); diff --git a/src/backend/linux_raw/arch/inline/mod.rs b/src/backend/linux_raw/arch/inline/mod.rs index 6ab1507da..524c449d9 100644 --- a/src/backend/linux_raw/arch/inline/mod.rs +++ b/src/backend/linux_raw/arch/inline/mod.rs @@ -5,14 +5,8 @@ //! conventions are otherwise the compiler's job. But for now, use inline asm. #[cfg_attr(target_arch = "aarch64", path = "aarch64.rs")] -#[cfg_attr( - all(target_arch = "arm", not(target_feature = "thumb-mode")), - path = "arm.rs" -)] -#[cfg_attr( - all(target_arch = "arm", target_feature = "thumb-mode"), - path = "thumb.rs" -)] +#[cfg_attr(all(target_arch = "arm", not(thumb_mode)), path = "arm.rs")] +#[cfg_attr(all(target_arch = "arm", thumb_mode), path = "thumb.rs")] #[cfg_attr(target_arch = "mips", path = "mips.rs")] #[cfg_attr(target_arch = "mips64", path = "mips64.rs")] #[cfg_attr(target_arch = "powerpc64", path = "powerpc64.rs")]