Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use rustc_span::{Symbol, bug};
use rustc_structures::{CrateType, NativeLibKind};
use rustc_target::spec::crt_objects::CrtObjects;
use rustc_target::spec::{
BinaryFormat, Cc, CfgAbi, Env, LinkOutputKind, LinkSelfContainedComponents,
Arch, BinaryFormat, Cc, CfgAbi, Env, LinkOutputKind, LinkSelfContainedComponents,
LinkSelfContainedDefault, LinkerFeatures, LinkerFlavor, LinkerFlavorCli, Lld, Os, RelocModel,
RelroLevel, SanitizerSet, SplitDebuginfo,
};
Expand Down Expand Up @@ -1336,12 +1336,34 @@ fn link_natively(
if matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, Lld::Yes))
&& unknown_arg_regex.is_match(&out)
&& out.contains("-fuse-ld=lld")
&& cmd.get_args().iter().any(|e| e.to_string_lossy() == "-fuse-ld=lld")
&& cmd.get_args().iter().any(|e| e == "-fuse-ld=lld")
{
info!("linker output: {:?}", out);
info!("The linker driver does not support `-fuse-ld=lld`. Retrying without it.");
for arg in cmd.take_args() {
if arg.to_string_lossy() != "-fuse-ld=lld" {
if arg != "-fuse-ld=lld" {
cmd.arg(arg);
}
}
info!("{cmd:?}");
continue;
}

// Check if linking failed with an error message that the linker doesn't recognise the
// cortex-a53 mitigation. We pass `-mfix-cortex-a53-843419` to CC-based linker drivers,
// which forward `--fix-cortex-a53-843419` to the linker itself, but either component may
// not recognise each flag. Notably this was only added to Clang in version 21.
if matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, _))
&& unknown_arg_regex.is_match(&out)
&& out.contains("fix-cortex-a53-843419")
&& cmd.get_args().iter().any(|e| e == "-mfix-cortex-a53-843419")
{
info!("linker output: {:?}", out);
info!(
"The linker or linker driver do not support `fix-cortex-a53-843419`. Retrying without it."
);
for arg in cmd.take_args() {
if arg != "-mfix-cortex-a53-843419" {
cmd.arg(arg);
}
}
Expand Down Expand Up @@ -2405,6 +2427,16 @@ fn add_post_link_objects(
/// Add arbitrary "pre-link" args defined by the target spec or from command line.
/// FIXME: Determine where exactly these args need to be inserted.
fn add_pre_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
// Emit the CC flag for the A53 mitigation early, as later linker args can override it if
// required with `-mno-fix-cortex-a53-843419`
if sess.target.arch == Arch::AArch64
&& !sess.target.is_like_darwin
&& sess.target.os != Os::Windows
&& matches!(flavor, LinkerFlavor::Gnu(Cc::Yes, ..))
{
cmd.cc_arg("-mfix-cortex-a53-843419");
}

@bjorn3 bjorn3 Oct 26, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be after -Zpre-link-arg?
Edit: I guess not given that the pre_link_args field of the target spec is also before it.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense for both the target specs and user pre-link-args to be able to override this


if let Some(args) = sess.target.pre_link_args.get(&flavor) {
cmd.verbatim_args(args.iter().map(Deref::deref));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::spec::{
Arch, Cc, FramePointer, LinkerFlavor, Lld, SanitizerSet, StackProbeType, Target,
TargetMetadata, TargetOptions, base,
Arch, FramePointer, SanitizerSet, StackProbeType, Target, TargetMetadata, TargetOptions, base,
};

pub(crate) fn target() -> Target {
Expand All @@ -16,11 +15,6 @@ pub(crate) fn target() -> Target {
data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32".into(),
arch: Arch::AArch64,
options: TargetOptions {
// Enable the Cortex-A53 errata 843419 mitigation by default
pre_link_args: TargetOptions::link_args(
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
&["-Wl,--fix-cortex-a53-843419"],
),
features: "+v8a,+outline-atomics".into(),
// the AAPCS64 expects use of non-leaf frame pointers per
// https://github.com/ARM-software/abi-aa/blob/4492d1570eb70c8fd146623e0db65b2d241f12e7/aapcs64/aapcs64.rst#the-frame-pointer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::spec::{
Arch, Cc, FramePointer, LinkerFlavor, Lld, SanitizerSet, StackProbeType, Target,
TargetMetadata, TargetOptions, base,
Arch, FramePointer, SanitizerSet, StackProbeType, Target, TargetMetadata, TargetOptions, base,
};

pub(crate) fn target() -> Target {
Expand Down Expand Up @@ -30,11 +29,6 @@ pub(crate) fn target() -> Target {
data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32".into(),
arch: Arch::AArch64,
options: TargetOptions {
// Enable the Cortex-A53 errata 843419 mitigation by default
pre_link_args: TargetOptions::link_args(
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
&["-Wl,--fix-cortex-a53-843419"],
),
// the AAPCS64 expects use of non-leaf frame pointers per
// https://github.com/ARM-software/abi-aa/blob/4492d1570eb70c8fd146623e0db65b2d241f12e7/aapcs64/aapcs64.rst#the-frame-pointer
// and we tend to encounter interesting bugs in AArch64 unwinding code if we do not
Expand Down
1 change: 1 addition & 0 deletions tests/run-make/a53-mitigation/empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
12 changes: 12 additions & 0 deletions tests/run-make/a53-mitigation/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ only-aarch64-unknown-linux-gnu
//@ needs-target-std

use run_make_support::rustc;

fn main() {
rustc()
.input("empty.rs")
.print("link-args")
.run_unchecked()
.assert_stdout_contains("-mfix-cortex-a53-843419");
}
Loading