Skip to content

Commit cdaa9e5

Browse files
committed
Move TargetOptions::link_args to MaybeLazy - part 1
Replace all the `pre_link_args: TargetOptions::link_args` to `MaybeLazy::lazy(|| TargetOptions::link_args(...))`
1 parent 5012d3a commit cdaa9e5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+516
-385
lines changed

compiler/rustc_target/src/spec/base/apple/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{borrow::Cow, env};
22

3-
use crate::spec::{add_link_args, add_link_args_iter};
3+
use crate::spec::{add_link_args, add_link_args_iter, MaybeLazy};
44
use crate::spec::{cvs, Cc, DebuginfoKind, FramePointer, LinkArgs, LinkerFlavor, Lld};
55
use crate::spec::{SplitDebuginfo, StackProbeType, StaticCow, Target, TargetOptions};
66

@@ -94,7 +94,7 @@ impl TargetAbi {
9494
}
9595
}
9696

97-
fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
97+
pub fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
9898
let platform_name: StaticCow<str> = match abi {
9999
TargetAbi::Normal => os.into(),
100100
TargetAbi::Simulator => format!("{os}-simulator").into(),
@@ -140,7 +140,12 @@ fn pre_link_args(os: &'static str, arch: Arch, abi: TargetAbi) -> LinkArgs {
140140
args
141141
}
142142

143-
pub fn opts(os: &'static str, arch: Arch, abi: TargetAbi) -> TargetOptions {
143+
pub fn opts(
144+
os: &'static str,
145+
arch: Arch,
146+
abi: TargetAbi,
147+
pre_link_args: MaybeLazy<LinkArgs>,
148+
) -> TargetOptions {
144149
TargetOptions {
145150
abi: abi.target_abi().into(),
146151
os: os.into(),
@@ -151,7 +156,7 @@ pub fn opts(os: &'static str, arch: Arch, abi: TargetAbi) -> TargetOptions {
151156
// macOS has -dead_strip, which doesn't rely on function_sections
152157
function_sections: false,
153158
dynamic_linking: true,
154-
pre_link_args: pre_link_args(os, arch, abi),
159+
pre_link_args,
155160
families: cvs!["unix"],
156161
is_like_osx: true,
157162
// LLVM notes that macOS 10.11+ and iOS 9+ default

compiler/rustc_target/src/spec/base/avr_gnu.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
use crate::spec::{Cc, LinkerFlavor, Lld, RelocModel, Target, TargetOptions};
1+
use crate::spec::{Cc, LinkerFlavor, Lld, MaybeLazy, RelocModel, Target, TargetOptions};
22
use object::elf;
33

44
/// A base target for AVR devices using the GNU toolchain.
55
///
66
/// Requires GNU avr-gcc and avr-binutils on the host system.
7-
/// FIXME: Remove the second parameter when const string concatenation is possible.
8-
pub fn target(target_cpu: &'static str, mmcu: &'static str) -> Target {
7+
pub fn target(target_cpu: &'static str) -> Target {
98
Target {
109
arch: "avr".into(),
1110
metadata: crate::spec::TargetMetadata {
@@ -24,11 +23,12 @@ pub fn target(target_cpu: &'static str, mmcu: &'static str) -> Target {
2423

2524
linker: Some("avr-gcc".into()),
2625
eh_frame_header: false,
27-
pre_link_args: TargetOptions::link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &[mmcu]),
28-
late_link_args: TargetOptions::link_args(
29-
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
30-
&["-lgcc"],
31-
),
26+
pre_link_args: MaybeLazy::lazy(|| {
27+
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-mmcu=atmega328"])
28+
}),
29+
late_link_args: MaybeLazy::lazy(|| {
30+
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-lgcc"])
31+
}),
3232
max_atomic_width: Some(16),
3333
atomic_cas: false,
3434
relocation_model: RelocModel::Static,

compiler/rustc_target/src/spec/base/fuchsia.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::spec::{
2-
crt_objects, cvs, Cc, FramePointer, LinkOutputKind, LinkerFlavor, Lld, TargetOptions,
2+
crt_objects, cvs, Cc, FramePointer, LinkOutputKind, LinkerFlavor, Lld, MaybeLazy, TargetOptions,
33
};
44

55
pub fn opts() -> TargetOptions {
@@ -8,22 +8,24 @@ pub fn opts() -> TargetOptions {
88
// so we only list them for ld/lld.
99
//
1010
// https://github.com/llvm/llvm-project/blob/db9322b2066c55254e7691efeab863f43bfcc084/clang/lib/Driver/ToolChains/Fuchsia.cpp#L31
11-
let pre_link_args = TargetOptions::link_args(
12-
LinkerFlavor::Gnu(Cc::No, Lld::No),
13-
&[
14-
"--build-id",
15-
"--hash-style=gnu",
16-
"-z",
17-
"max-page-size=4096",
18-
"-z",
19-
"now",
20-
"-z",
21-
"rodynamic",
22-
"-z",
23-
"separate-loadable-segments",
24-
"--pack-dyn-relocs=relr",
25-
],
26-
);
11+
let pre_link_args = MaybeLazy::lazy(|| {
12+
TargetOptions::link_args(
13+
LinkerFlavor::Gnu(Cc::No, Lld::No),
14+
&[
15+
"--build-id",
16+
"--hash-style=gnu",
17+
"-z",
18+
"max-page-size=4096",
19+
"-z",
20+
"now",
21+
"-z",
22+
"rodynamic",
23+
"-z",
24+
"separate-loadable-segments",
25+
"--pack-dyn-relocs=relr",
26+
],
27+
)
28+
});
2729

2830
TargetOptions {
2931
os: "fuchsia".into(),

compiler/rustc_target/src/spec/base/illumos.rs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
use crate::spec::{cvs, Cc, FramePointer, LinkerFlavor, TargetOptions};
1+
use crate::spec::{cvs, Cc, FramePointer, LinkerFlavor, MaybeLazy, TargetOptions};
22

33
pub fn opts() -> TargetOptions {
4-
let late_link_args = TargetOptions::link_args(
5-
LinkerFlavor::Unix(Cc::Yes),
6-
&[
7-
// The illumos libc contains a stack unwinding implementation, as
8-
// does libgcc_s. The latter implementation includes several
9-
// additional symbols that are not always in base libc. To force
10-
// the consistent use of just one unwinder, we ensure libc appears
11-
// after libgcc_s in the NEEDED list for the resultant binary by
12-
// ignoring any attempts to add it as a dynamic dependency until the
13-
// very end.
14-
// FIXME: This should be replaced by a more complete and generic
15-
// mechanism for controlling the order of library arguments passed
16-
// to the linker.
17-
"-lc",
18-
// LLVM will insert calls to the stack protector functions
19-
// "__stack_chk_fail" and "__stack_chk_guard" into code in native
20-
// object files. Some platforms include these symbols directly in
21-
// libc, but at least historically these have been provided in
22-
// libssp.so on illumos and Solaris systems.
23-
"-lssp",
24-
],
25-
);
4+
let late_link_args = MaybeLazy::lazy(|| {
5+
TargetOptions::link_args(
6+
LinkerFlavor::Unix(Cc::Yes),
7+
&[
8+
// The illumos libc contains a stack unwinding implementation, as
9+
// does libgcc_s. The latter implementation includes several
10+
// additional symbols that are not always in base libc. To force
11+
// the consistent use of just one unwinder, we ensure libc appears
12+
// after libgcc_s in the NEEDED list for the resultant binary by
13+
// ignoring any attempts to add it as a dynamic dependency until the
14+
// very end.
15+
// FIXME: This should be replaced by a more complete and generic
16+
// mechanism for controlling the order of library arguments passed
17+
// to the linker.
18+
"-lc",
19+
// LLVM will insert calls to the stack protector functions
20+
// "__stack_chk_fail" and "__stack_chk_guard" into code in native
21+
// object files. Some platforms include these symbols directly in
22+
// libc, but at least historically these have been provided in
23+
// libssp.so on illumos and Solaris systems.
24+
"-lssp",
25+
],
26+
)
27+
});
2628

2729
TargetOptions {
2830
os: "illumos".into(),

compiler/rustc_target/src/spec/base/msvc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use crate::spec::{DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
1+
use crate::spec::{DebuginfoKind, LinkerFlavor, Lld, MaybeLazy, SplitDebuginfo, TargetOptions};
22
use std::borrow::Cow;
33

44
pub fn opts() -> TargetOptions {
55
// Suppress the verbose logo and authorship debugging output, which would needlessly
66
// clog any log files.
7-
let pre_link_args = TargetOptions::link_args(LinkerFlavor::Msvc(Lld::No), &["/NOLOGO"]);
7+
let pre_link_args =
8+
MaybeLazy::lazy(|| TargetOptions::link_args(LinkerFlavor::Msvc(Lld::No), &["/NOLOGO"]));
89

910
TargetOptions {
1011
linker_flavor: LinkerFlavor::Msvc(Lld::No),

compiler/rustc_target/src/spec/base/teeos.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
use crate::spec::{add_link_args, Cc, LinkerFlavor, Lld, PanicStrategy, RelroLevel, TargetOptions};
1+
use crate::spec::{
2+
add_link_args, Cc, LinkerFlavor, Lld, MaybeLazy, PanicStrategy, RelroLevel, TargetOptions,
3+
};
24

35
pub fn opts() -> TargetOptions {
4-
let lld_args = &["-zmax-page-size=4096", "-znow", "-ztext", "--execute-only"];
5-
let cc_args = &["-Wl,-zmax-page-size=4096", "-Wl,-znow", "-Wl,-ztext", "-mexecute-only"];
6-
7-
let mut pre_link_args = TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), lld_args);
8-
add_link_args(&mut pre_link_args, LinkerFlavor::Gnu(Cc::Yes, Lld::No), cc_args);
6+
let pre_link_args = MaybeLazy::lazy(|| {
7+
let lld_args = &["-zmax-page-size=4096", "-znow", "-ztext", "--execute-only"];
8+
let cc_args = &["-Wl,-zmax-page-size=4096", "-Wl,-znow", "-Wl,-ztext", "-mexecute-only"];
9+
let mut pre_link_args =
10+
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), lld_args);
11+
add_link_args(&mut pre_link_args, LinkerFlavor::Gnu(Cc::Yes, Lld::No), cc_args);
12+
pre_link_args
13+
});
914

1015
TargetOptions {
1116
os: "teeos".into(),

compiler/rustc_target/src/spec/base/wasm.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::spec::{
2-
add_link_args, cvs, Cc, LinkSelfContainedDefault, LinkerFlavor, PanicStrategy, RelocModel,
3-
TargetOptions, TlsModel,
2+
add_link_args, cvs, Cc, LinkSelfContainedDefault, LinkerFlavor, MaybeLazy, PanicStrategy,
3+
RelocModel, TargetOptions, TlsModel,
44
};
55

66
pub fn options() -> TargetOptions {
@@ -48,8 +48,11 @@ pub fn options() -> TargetOptions {
4848
};
4949
}
5050

51-
let mut pre_link_args = TargetOptions::link_args(LinkerFlavor::WasmLld(Cc::No), args!(""));
52-
add_link_args(&mut pre_link_args, LinkerFlavor::WasmLld(Cc::Yes), args!("-Wl,"));
51+
let pre_link_args = MaybeLazy::lazy(|| {
52+
let mut pre_link_args = TargetOptions::link_args(LinkerFlavor::WasmLld(Cc::No), args!(""));
53+
add_link_args(&mut pre_link_args, LinkerFlavor::WasmLld(Cc::Yes), args!("-Wl,"));
54+
pre_link_args
55+
});
5356

5457
TargetOptions {
5558
is_like_wasm: true,

compiler/rustc_target/src/spec/base/windows_gnu.rs

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,90 @@
1-
use crate::spec::LinkSelfContainedDefault;
21
use crate::spec::{add_link_args, crt_objects};
32
use crate::spec::{cvs, Cc, DebuginfoKind, LinkerFlavor, Lld, SplitDebuginfo, TargetOptions};
3+
use crate::spec::{LinkSelfContainedDefault, MaybeLazy};
44
use std::borrow::Cow;
55

66
pub fn opts() -> TargetOptions {
7-
let mut pre_link_args = TargetOptions::link_args(
8-
LinkerFlavor::Gnu(Cc::No, Lld::No),
9-
&[
10-
// Enable ASLR
11-
"--dynamicbase",
12-
// ASLR will rebase it anyway so leaving that option enabled only leads to confusion
13-
"--disable-auto-image-base",
14-
],
15-
);
16-
add_link_args(
17-
&mut pre_link_args,
18-
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
19-
&[
20-
// Tell GCC to avoid linker plugins, because we are not bundling
21-
// them with Windows installer, and Rust does its own LTO anyways.
22-
"-fno-use-linker-plugin",
23-
"-Wl,--dynamicbase",
24-
"-Wl,--disable-auto-image-base",
25-
],
26-
);
7+
let pre_link_args = MaybeLazy::lazy(|| {
8+
let mut pre_link_args = TargetOptions::link_args(
9+
LinkerFlavor::Gnu(Cc::No, Lld::No),
10+
&[
11+
// Enable ASLR
12+
"--dynamicbase",
13+
// ASLR will rebase it anyway so leaving that option enabled only leads to confusion
14+
"--disable-auto-image-base",
15+
],
16+
);
17+
add_link_args(
18+
&mut pre_link_args,
19+
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
20+
&[
21+
// Tell GCC to avoid linker plugins, because we are not bundling
22+
// them with Windows installer, and Rust does its own LTO anyways.
23+
"-fno-use-linker-plugin",
24+
"-Wl,--dynamicbase",
25+
"-Wl,--disable-auto-image-base",
26+
],
27+
);
28+
pre_link_args
29+
});
2730

28-
// Order of `late_link_args*` was found through trial and error to work with various
29-
// mingw-w64 versions (not tested on the CI). It's expected to change from time to time.
30-
let mingw_libs = &[
31-
"-lmsvcrt",
32-
"-lmingwex",
33-
"-lmingw32",
34-
"-lgcc", // alas, mingw* libraries above depend on libgcc
35-
// mingw's msvcrt is a weird hybrid import library and static library.
36-
// And it seems that the linker fails to use import symbols from msvcrt
37-
// that are required from functions in msvcrt in certain cases. For example
38-
// `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
39-
// The library is purposely listed twice to fix that.
40-
//
41-
// See https://github.com/rust-lang/rust/pull/47483 for some more details.
42-
"-lmsvcrt",
43-
// Math functions missing in MSVCRT (they are present in UCRT) require
44-
// this dependency cycle: `libmingwex.a` -> `libmsvcrt.a` -> `libmingwex.a`.
45-
"-lmingwex",
46-
"-luser32",
47-
"-lkernel32",
48-
];
49-
let mut late_link_args =
50-
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), mingw_libs);
51-
add_link_args(&mut late_link_args, LinkerFlavor::Gnu(Cc::Yes, Lld::No), mingw_libs);
31+
let late_link_args = MaybeLazy::lazy(|| {
32+
// Order of `late_link_args*` was found through trial and error to work with various
33+
// mingw-w64 versions (not tested on the CI). It's expected to change from time to time.
34+
let mingw_libs = &[
35+
"-lmsvcrt",
36+
"-lmingwex",
37+
"-lmingw32",
38+
"-lgcc", // alas, mingw* libraries above depend on libgcc
39+
// mingw's msvcrt is a weird hybrid import library and static library.
40+
// And it seems that the linker fails to use import symbols from msvcrt
41+
// that are required from functions in msvcrt in certain cases. For example
42+
// `_fmode` that is used by an implementation of `__p__fmode` in x86_64.
43+
// The library is purposely listed twice to fix that.
44+
//
45+
// See https://github.com/rust-lang/rust/pull/47483 for some more details.
46+
"-lmsvcrt",
47+
// Math functions missing in MSVCRT (they are present in UCRT) require
48+
// this dependency cycle: `libmingwex.a` -> `libmsvcrt.a` -> `libmingwex.a`.
49+
"-lmingwex",
50+
"-luser32",
51+
"-lkernel32",
52+
];
53+
let mut late_link_args =
54+
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), mingw_libs);
55+
add_link_args(&mut late_link_args, LinkerFlavor::Gnu(Cc::Yes, Lld::No), mingw_libs);
56+
late_link_args
57+
});
5258
// If any of our crates are dynamically linked then we need to use
5359
// the shared libgcc_s-dw2-1.dll. This is required to support
5460
// unwinding across DLL boundaries.
55-
let dynamic_unwind_libs = &["-lgcc_s"];
56-
let mut late_link_args_dynamic =
57-
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), dynamic_unwind_libs);
58-
add_link_args(
59-
&mut late_link_args_dynamic,
60-
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
61-
dynamic_unwind_libs,
62-
);
61+
let late_link_args_dynamic = MaybeLazy::lazy(|| {
62+
let dynamic_unwind_libs = &["-lgcc_s"];
63+
let mut late_link_args_dynamic =
64+
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), dynamic_unwind_libs);
65+
add_link_args(
66+
&mut late_link_args_dynamic,
67+
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
68+
dynamic_unwind_libs,
69+
);
70+
late_link_args_dynamic
71+
});
6372
// If all of our crates are statically linked then we can get away
6473
// with statically linking the libgcc unwinding code. This allows
6574
// binaries to be redistributed without the libgcc_s-dw2-1.dll
6675
// dependency, but unfortunately break unwinding across DLL
6776
// boundaries when unwinding across FFI boundaries.
68-
let static_unwind_libs = &["-lgcc_eh", "-l:libpthread.a"];
69-
let mut late_link_args_static =
70-
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), static_unwind_libs);
71-
add_link_args(
72-
&mut late_link_args_static,
73-
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
74-
static_unwind_libs,
75-
);
77+
let late_link_args_static = MaybeLazy::lazy(|| {
78+
let static_unwind_libs = &["-lgcc_eh", "-l:libpthread.a"];
79+
let mut late_link_args_static =
80+
TargetOptions::link_args(LinkerFlavor::Gnu(Cc::No, Lld::No), static_unwind_libs);
81+
add_link_args(
82+
&mut late_link_args_static,
83+
LinkerFlavor::Gnu(Cc::Yes, Lld::No),
84+
static_unwind_libs,
85+
);
86+
late_link_args_static
87+
});
7688

7789
TargetOptions {
7890
os: "windows".into(),

0 commit comments

Comments
 (0)