Skip to content

Commit

Permalink
Auto merge of #98212 - petrochenkov:addlinkargs, r=lqd
Browse files Browse the repository at this point in the history
rustc_target: Add convenience functions for adding linker arguments

They ensure that lld and non-lld linker flavors get the same set of arguments.

The second commit also adds some tests checking for linker argument inconsistencies, and tweaks some arguments to fix those inconsistencies.
  • Loading branch information
bors committed Jun 27, 2022
2 parents 3b0d481 + 456f65e commit 221bdb6
Show file tree
Hide file tree
Showing 76 changed files with 459 additions and 443 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_target/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! LLVM.

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(assert_matches)]
#![feature(associated_type_bounds)]
#![feature(exhaustive_patterns)]
#![feature(let_else)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/aarch64_apple_darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn target() -> Target {
// FIXME: The leak sanitizer currently fails the tests, see #88132.
base.supported_sanitizers = SanitizerSet::ADDRESS | SanitizerSet::CFI | SanitizerSet::THREAD;

base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-arch".into(), "arm64".into()]);
base.add_pre_link_args(LinkerFlavor::Gcc, &["-arch", "arm64"]);
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());

// Clang automatically chooses a more specific target based on
Expand Down
11 changes: 2 additions & 9 deletions compiler/rustc_target/src/spec/aarch64_unknown_uefi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
// uefi-base module for generic UEFI options.

use super::uefi_msvc_base;
use crate::spec::{LinkerFlavor, LldFlavor, Target};
use crate::spec::{LinkerFlavor, Target};

pub fn target() -> Target {
let mut base = uefi_msvc_base::opts();

base.max_atomic_width = Some(64);

let pre_link_args_msvc = vec!["/machine:arm64".into()];

base.pre_link_args.get_mut(&LinkerFlavor::Msvc).unwrap().extend(pre_link_args_msvc.clone());
base.pre_link_args
.get_mut(&LinkerFlavor::Lld(LldFlavor::Link))
.unwrap()
.extend(pre_link_args_msvc);
base.add_pre_link_args(LinkerFlavor::Msvc, &["/machine:arm64"]);

Target {
llvm_target: "aarch64-unknown-windows".into(),
Expand Down
12 changes: 3 additions & 9 deletions compiler/rustc_target/src/spec/armv6k_nintendo_3ds.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
use crate::spec::{cvs, LinkArgs, LinkerFlavor, RelocModel, Target, TargetOptions};
use crate::spec::{cvs, LinkerFlavor, RelocModel, Target, TargetOptions};

/// A base target for Nintendo 3DS devices using the devkitARM toolchain.
///
/// Requires the devkitARM toolchain for 3DS targets on the host system.

pub fn target() -> Target {
let mut pre_link_args = LinkArgs::new();
pre_link_args.insert(
let pre_link_args = TargetOptions::link_args(
LinkerFlavor::Gcc,
vec![
"-specs=3dsx.specs".into(),
"-mtune=mpcore".into(),
"-mfloat-abi=hard".into(),
"-mtp=soft".into(),
],
&["-specs=3dsx.specs", "-mtune=mpcore", "-mfloat-abi=hard", "-mtp=soft"],
);

Target {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/armv7_linux_androideabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::spec::{LinkerFlavor, SanitizerSet, Target, TargetOptions};

pub fn target() -> Target {
let mut base = super::android_base::opts();
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-march=armv7-a".into());
base.add_pre_link_args(LinkerFlavor::Gcc, &["-march=armv7-a"]);
Target {
llvm_target: "armv7-none-linux-android".into(),
pointer_width: 32,
Expand Down
6 changes: 1 addition & 5 deletions compiler/rustc_target/src/spec/asmjs_unknown_emscripten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ use super::{wasm32_unknown_emscripten, LinkerFlavor, Target};

pub fn target() -> Target {
let mut target = wasm32_unknown_emscripten::target();
target.post_link_args.entry(LinkerFlavor::Em).or_default().extend(vec![
"-sWASM=0".into(),
"--memory-init-file".into(),
"0".into(),
]);
target.add_post_link_args(LinkerFlavor::Em, &["-sWASM=0", "--memory-init-file", "0"]);
target
}
9 changes: 4 additions & 5 deletions compiler/rustc_target/src/spec/avr_gnu_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::spec::{LinkerFlavor, Target, TargetOptions};
/// A base target for AVR devices using the GNU toolchain.
///
/// Requires GNU avr-gcc and avr-binutils on the host system.
pub fn target(target_cpu: &'static str) -> Target {
/// FIXME: Remove the second parameter when const string concatenation is possible.
pub fn target(target_cpu: &'static str, mmcu: &'static str) -> Target {
Target {
arch: "avr".into(),
data_layout: "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8".into(),
Expand All @@ -17,10 +18,8 @@ pub fn target(target_cpu: &'static str) -> Target {
linker: Some("avr-gcc".into()),
executables: true,
eh_frame_header: false,
pre_link_args: [(LinkerFlavor::Gcc, vec![format!("-mmcu={}", target_cpu).into()])]
.into_iter()
.collect(),
late_link_args: [(LinkerFlavor::Gcc, vec!["-lgcc".into()])].into_iter().collect(),
pre_link_args: TargetOptions::link_args(LinkerFlavor::Gcc, &[mmcu]),
late_link_args: TargetOptions::link_args(LinkerFlavor::Gcc, &["-lgcc"]),
max_atomic_width: Some(0),
atomic_cas: false,
..TargetOptions::default()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::spec::Target;

pub fn target() -> Target {
super::avr_gnu_base::target("atmega328")
super::avr_gnu_base::target("atmega328", "-mmcu=atmega328")
}
33 changes: 15 additions & 18 deletions compiler/rustc_target/src/spec/fuchsia_base.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use crate::spec::{
crt_objects, cvs, LinkArgs, LinkOutputKind, LinkerFlavor, LldFlavor, TargetOptions,
};
use crate::spec::{crt_objects, cvs, LinkOutputKind, LinkerFlavor, LldFlavor, TargetOptions};

pub fn opts() -> TargetOptions {
let mut pre_link_args = LinkArgs::new();
pre_link_args.insert(
LinkerFlavor::Lld(LldFlavor::Ld),
vec![
"--build-id".into(),
"--hash-style=gnu".into(),
"-z".into(),
"max-page-size=4096".into(),
"-z".into(),
"now".into(),
"-z".into(),
"rodynamic".into(),
"-z".into(),
"separate-loadable-segments".into(),
"--pack-dyn-relocs=relr".into(),
let pre_link_args = TargetOptions::link_args(
LinkerFlavor::Ld,
&[
"--build-id",
"--hash-style=gnu",
"-z",
"max-page-size=4096",
"-z",
"now",
"-z",
"rodynamic",
"-z",
"separate-loadable-segments",
"--pack-dyn-relocs=relr",
],
);

Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_target/src/spec/hermit_base.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, PanicStrategy, TargetOptions, TlsModel};
use crate::spec::{LinkerFlavor, LldFlavor, PanicStrategy, TargetOptions, TlsModel};

pub fn opts() -> TargetOptions {
let mut pre_link_args = LinkArgs::new();
pre_link_args.insert(
LinkerFlavor::Lld(LldFlavor::Ld),
vec!["--build-id".into(), "--hash-style=gnu".into(), "--Bstatic".into()],
let pre_link_args = TargetOptions::link_args(
LinkerFlavor::Ld,
&["--build-id", "--hash-style=gnu", "--Bstatic"],
);

TargetOptions {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/i686_apple_darwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let mut base = super::apple_base::opts("macos");
base.cpu = "yonah".into();
base.max_atomic_width = Some(64);
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".into()]);
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32"]);
base.link_env_remove.to_mut().extend(super::apple_base::macos_link_env_remove());
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_target/src/spec/i686_pc_windows_gnu.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use crate::spec::{FramePointer, LinkerFlavor, LldFlavor, Target};
use crate::spec::{FramePointer, LinkerFlavor, Target};

pub fn target() -> Target {
let mut base = super::windows_gnu_base::opts();
base.cpu = "pentium4".into();
base.pre_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".into(), "i386pe".into()]);
base.max_atomic_width = Some(64);
base.frame_pointer = FramePointer::Always; // Required for backtraces
base.linker = Some("i686-w64-mingw32-gcc".into());

// Mark all dynamic libraries and executables as compatible with the larger 4GiB address
// space available to x86 Windows binaries on x86_64.
base.pre_link_args
.entry(LinkerFlavor::Gcc)
.or_default()
.push("-Wl,--large-address-aware".into());
base.add_pre_link_args(LinkerFlavor::Ld, &["-m", "i386pe", "--large-address-aware"]);
base.add_pre_link_args(LinkerFlavor::Gcc, &["-Wl,--large-address-aware"]);

Target {
llvm_target: "i686-pc-windows-gnu".into(),
Expand Down
28 changes: 13 additions & 15 deletions compiler/rustc_target/src/spec/i686_pc_windows_msvc.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use crate::spec::{LinkerFlavor, LldFlavor, Target};
use crate::spec::{LinkerFlavor, Target};

pub fn target() -> Target {
let mut base = super::windows_msvc_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);

let pre_link_args_msvc = vec![
// Mark all dynamic libraries and executables as compatible with the larger 4GiB address
// space available to x86 Windows binaries on x86_64.
"/LARGEADDRESSAWARE".into(),
// Ensure the linker will only produce an image if it can also produce a table of
// the image's safe exception handlers.
// https://docs.microsoft.com/en-us/cpp/build/reference/safeseh-image-has-safe-exception-handlers
"/SAFESEH".into(),
];
base.pre_link_args.entry(LinkerFlavor::Msvc).or_default().extend(pre_link_args_msvc.clone());
base.pre_link_args
.entry(LinkerFlavor::Lld(LldFlavor::Link))
.or_default()
.extend(pre_link_args_msvc);
base.add_pre_link_args(
LinkerFlavor::Msvc,
&[
// Mark all dynamic libraries and executables as compatible with the larger 4GiB address
// space available to x86 Windows binaries on x86_64.
"/LARGEADDRESSAWARE",
// Ensure the linker will only produce an image if it can also produce a table of
// the image's safe exception handlers.
// https://docs.microsoft.com/en-us/cpp/build/reference/safeseh-image-has-safe-exception-handlers
"/SAFESEH",
],
);
// Workaround for #95429
base.has_thread_local = false;

Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_target/src/spec/i686_unknown_freebsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ pub fn target() -> Target {
let mut base = super::freebsd_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);
let pre_link_args = base.pre_link_args.entry(LinkerFlavor::Gcc).or_default();
pre_link_args.push("-m32".into());
pre_link_args.push("-Wl,-znotext".into());
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32", "-Wl,-znotext"]);
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/i686_unknown_haiku.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let mut base = super::haiku_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);
base.pre_link_args.insert(LinkerFlavor::Gcc, vec!["-m32".into()]);
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32"]);
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/i686_unknown_linux_gnu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let mut base = super::linux_gnu_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".into());
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32"]);
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_target/src/spec/i686_unknown_linux_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub fn target() -> Target {
let mut base = super::linux_musl_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".into());
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-Wl,-melf_i386".into());
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32", "-Wl,-melf_i386"]);
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/i686_unknown_netbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let mut base = super::netbsd_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".into());
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32"]);
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;

Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_target/src/spec/i686_unknown_openbsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ pub fn target() -> Target {
let mut base = super::openbsd_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".into());
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-fuse-ld=lld".into());
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32", "-fuse-ld=lld"]);
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;

Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_target/src/spec/i686_uwp_windows_gnu.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use crate::spec::{FramePointer, LinkerFlavor, LldFlavor, Target};
use crate::spec::{FramePointer, LinkerFlavor, Target};

pub fn target() -> Target {
let mut base = super::windows_uwp_gnu_base::opts();
base.cpu = "pentium4".into();
base.pre_link_args.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["-m".into(), "i386pe".into()]);
base.max_atomic_width = Some(64);
base.frame_pointer = FramePointer::Always; // Required for backtraces

// Mark all dynamic libraries and executables as compatible with the larger 4GiB address
// space available to x86 Windows binaries on x86_64.
base.pre_link_args
.entry(LinkerFlavor::Gcc)
.or_default()
.push("-Wl,--large-address-aware".into());
base.add_pre_link_args(LinkerFlavor::Ld, &["-m", "i386pe", "--large-address-aware"]);
base.add_pre_link_args(LinkerFlavor::Gcc, &["-Wl,--large-address-aware"]);

Target {
llvm_target: "i686-pc-windows-gnu".into(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/i686_wrs_vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn target() -> Target {
let mut base = super::vxworks_base::opts();
base.cpu = "pentium4".into();
base.max_atomic_width = Some(64);
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".into());
base.add_pre_link_args(LinkerFlavor::Gcc, &["-m32"]);
// don't use probe-stack=inline-asm until rust#83139 and rust#84667 are resolved
base.stack_probes = StackProbeType::Call;

Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_target/src/spec/illumos_base.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::spec::{cvs, FramePointer, LinkArgs, LinkerFlavor, TargetOptions};
use crate::spec::{cvs, FramePointer, LinkerFlavor, TargetOptions};

pub fn opts() -> TargetOptions {
let mut late_link_args = LinkArgs::new();
late_link_args.insert(
let late_link_args = TargetOptions::link_args(
LinkerFlavor::Gcc,
vec![
&[
// The illumos libc contains a stack unwinding implementation, as
// does libgcc_s. The latter implementation includes several
// additional symbols that are not always in base libc. To force
Expand All @@ -15,13 +14,13 @@ pub fn opts() -> TargetOptions {
// FIXME: This should be replaced by a more complete and generic
// mechanism for controlling the order of library arguments passed
// to the linker.
"-lc".into(),
"-lc",
// LLVM will insert calls to the stack protector functions
// "__stack_chk_fail" and "__stack_chk_guard" into code in native
// object files. Some platforms include these symbols directly in
// libc, but at least historically these have been provided in
// libssp.so on illumos and Solaris systems.
"-lssp".into(),
"-lssp",
],
);

Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_target/src/spec/mipsel_sony_psp.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use crate::spec::{cvs, Target, TargetOptions};
use crate::spec::{LinkArgs, LinkerFlavor, LldFlavor, RelocModel};
use crate::spec::{LinkerFlavor, LldFlavor, RelocModel};

// The PSP has custom linker requirements.
const LINKER_SCRIPT: &str = include_str!("./mipsel_sony_psp_linker_script.ld");

pub fn target() -> Target {
let mut pre_link_args = LinkArgs::new();
pre_link_args
.insert(LinkerFlavor::Lld(LldFlavor::Ld), vec!["--emit-relocs".into(), "--nmagic".into()]);
let pre_link_args = TargetOptions::link_args(LinkerFlavor::Ld, &["--emit-relocs", "--nmagic"]);

Target {
llvm_target: "mipsel-sony-psp".into(),
Expand Down
Loading

0 comments on commit 221bdb6

Please sign in to comment.