diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs index 4dcc7cd54477d..f6fcdc90cb61f 100644 --- a/compiler/rustc_codegen_llvm/src/context.rs +++ b/compiler/rustc_codegen_llvm/src/context.rs @@ -3,7 +3,6 @@ use crate::back::write::to_llvm_code_model; use crate::callee::get_fn; use crate::coverageinfo; use crate::debuginfo; -use crate::errors::BranchProtectionRequiresAArch64; use crate::llvm; use crate::llvm_util; use crate::type_::Type; @@ -276,9 +275,7 @@ pub unsafe fn create_module<'ll>( } if let Some(BranchProtection { bti, pac_ret }) = sess.opts.unstable_opts.branch_protection { - if sess.target.arch != "aarch64" { - sess.emit_err(BranchProtectionRequiresAArch64); - } else { + if sess.target.arch == "aarch64" { llvm::LLVMRustAddModuleFlag( llmod, llvm::LLVMModFlagBehavior::Error, @@ -304,6 +301,11 @@ pub unsafe fn create_module<'ll>( "sign-return-address-with-bkey\0".as_ptr().cast(), u32::from(pac_opts.key == PAuthKey::B), ); + } else { + bug!( + "branch-protection used on non-AArch64 target; \ + this should be checked in rustc_session." + ); } } diff --git a/compiler/rustc_codegen_llvm/src/errors.rs b/compiler/rustc_codegen_llvm/src/errors.rs index fddfbb23c67d5..de508f917f33e 100644 --- a/compiler/rustc_codegen_llvm/src/errors.rs +++ b/compiler/rustc_codegen_llvm/src/errors.rs @@ -51,10 +51,6 @@ pub(crate) struct SymbolAlreadyDefined<'a> { pub symbol_name: &'a str, } -#[derive(Diagnostic)] -#[diag(codegen_llvm_branch_protection_requires_aarch64)] -pub(crate) struct BranchProtectionRequiresAArch64; - #[derive(Diagnostic)] #[diag(codegen_llvm_invalid_minimum_alignment)] pub(crate) struct InvalidMinimumAlignment { diff --git a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl index e273476b60bb6..86f71a611ab0c 100644 --- a/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl +++ b/compiler/rustc_error_messages/locales/en-US/codegen_llvm.ftl @@ -17,9 +17,6 @@ codegen_llvm_instrument_coverage_requires_llvm_12 = codegen_llvm_symbol_already_defined = symbol `{$symbol_name}` is already defined -codegen_llvm_branch_protection_requires_aarch64 = - -Zbranch-protection is only supported on aarch64 - codegen_llvm_invalid_minimum_alignment = invalid minimum global alignment: {$err} diff --git a/compiler/rustc_error_messages/locales/en-US/session.ftl b/compiler/rustc_error_messages/locales/en-US/session.ftl index 983eb926213ea..ab9e8b6baae6a 100644 --- a/compiler/rustc_error_messages/locales/en-US/session.ftl +++ b/compiler/rustc_error_messages/locales/en-US/session.ftl @@ -41,6 +41,8 @@ session_unsupported_dwarf_version = requested DWARF version {$dwarf_version} is session_target_stack_protector_not_supported = `-Z stack-protector={$stack_protector}` is not supported for target {$target_triple} and will be ignored +session_branch_protection_requires_aarch64 = `-Zbranch-protection` is only supported on aarch64 + session_split_debuginfo_unstable_platform = `-Csplit-debuginfo={$debuginfo}` is unstable on this platform session_file_is_not_writeable = output file {$file} is not writeable -- check its permissions diff --git a/compiler/rustc_session/src/errors.rs b/compiler/rustc_session/src/errors.rs index 8cb9e1a6f1ae8..fd7dabf75ef20 100644 --- a/compiler/rustc_session/src/errors.rs +++ b/compiler/rustc_session/src/errors.rs @@ -115,6 +115,10 @@ pub struct StackProtectorNotSupportedForTarget<'a> { pub target_triple: &'a TargetTriple, } +#[derive(Diagnostic)] +#[diag(session_branch_protection_requires_aarch64)] +pub(crate) struct BranchProtectionRequiresAArch64; + #[derive(Diagnostic)] #[diag(session_split_debuginfo_unstable_platform)] pub struct SplitDebugInfoUnstablePlatform { diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index e99e460913ef0..ce4a2e3d82d89 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -3,10 +3,10 @@ use crate::code_stats::CodeStats; pub use crate::code_stats::{DataTypeKind, FieldInfo, SizeKind, VariantInfo}; use crate::config::{self, CrateType, InstrumentCoverage, OptLevel, OutputType, SwitchWithOptPath}; use crate::errors::{ - CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, LinkerPluginToWindowsNotSupported, - NotCircumventFeature, ProfileSampleUseFileDoesNotExist, ProfileUseFileDoesNotExist, - SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported, SkippingConstChecks, - SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget, + BranchProtectionRequiresAArch64, CannotEnableCrtStaticLinux, CannotMixAndMatchSanitizers, + LinkerPluginToWindowsNotSupported, NotCircumventFeature, ProfileSampleUseFileDoesNotExist, + ProfileUseFileDoesNotExist, SanitizerCfiEnabled, SanitizerNotSupported, SanitizersNotSupported, + SkippingConstChecks, SplitDebugInfoUnstablePlatform, StackProtectorNotSupportedForTarget, TargetRequiresUnwindTables, UnleashedFeatureHelp, UnstableVirtualFunctionElimination, UnsupportedDwarfVersion, }; @@ -1542,6 +1542,10 @@ fn validate_commandline_args_with_session_available(sess: &Session) { } } + if sess.opts.unstable_opts.branch_protection.is_some() && sess.target.arch != "aarch64" { + sess.emit_err(BranchProtectionRequiresAArch64); + } + if let Some(dwarf_version) = sess.opts.unstable_opts.dwarf_version { if dwarf_version > 5 { sess.emit_err(UnsupportedDwarfVersion { dwarf_version }); diff --git a/src/doc/unstable-book/src/compiler-flags/branch-protection.md b/src/doc/unstable-book/src/compiler-flags/branch-protection.md index 85403748e1dcd..851dbead44b40 100644 --- a/src/doc/unstable-book/src/compiler-flags/branch-protection.md +++ b/src/doc/unstable-book/src/compiler-flags/branch-protection.md @@ -1,7 +1,7 @@ # `branch-protection` This option lets you enable branch authentication instructions on AArch64. -This option is ignored for non-AArch64 architectures. +This option is only accepted when targeting AArch64 architectures. It takes some combination of the following values, separated by a `,`. - `pac-ret` - Enable pointer authentication for non-leaf functions. diff --git a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr index 6bd9c6a0276f8..52a5919026463 100644 --- a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr +++ b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.BADTARGET.stderr @@ -1,4 +1,4 @@ -error: -Zbranch-protection is only supported on aarch64 +error: `-Zbranch-protection` is only supported on aarch64 error: aborting due to previous error diff --git a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs index 4bc4919bc935b..1d7ec5cba17ca 100644 --- a/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs +++ b/src/test/ui/invalid-compile-flags/branch-protection-missing-pac-ret.rs @@ -3,7 +3,7 @@ // [BADFLAGS] check-fail // [BADFLAGS] needs-llvm-components: aarch64 // [BADTARGET] compile-flags: --target=x86_64-unknown-linux-gnu -Zbranch-protection=bti -// [BADTARGET] build-fail +// [BADTARGET] check-fail // [BADTARGET] needs-llvm-components: x86 #![crate_type = "lib"]