Skip to content

Commit 6615ee8

Browse files
committed
linker: Use --as-needed by default when linker supports it
1 parent afaf33d commit 6615ee8

21 files changed

+30
-94
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1651,6 +1651,12 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
16511651
cmd.add_eh_frame_header();
16521652
}
16531653

1654+
// NO-OPT-OUT, OBJECT-FILES-NO
1655+
// Avoid linking to dynamic libraries unless they satisfy some undefined symbols
1656+
// at the point at which they are specified on the command line.
1657+
// Must be passed before any dynamic libraries.
1658+
cmd.add_as_needed();
1659+
16541660
// NO-OPT-OUT, OBJECT-FILES-NO
16551661
if crt_objects_fallback {
16561662
cmd.no_crt_objects();

compiler/rustc_codegen_ssa/src/back/linker.rs

+7
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub trait Linker {
130130
fn group_end(&mut self);
131131
fn linker_plugin_lto(&mut self);
132132
fn add_eh_frame_header(&mut self) {}
133+
fn add_as_needed(&mut self) {}
133134
fn finalize(&mut self);
134135
}
135136

@@ -641,6 +642,12 @@ impl<'a> Linker for GccLinker<'a> {
641642
fn add_eh_frame_header(&mut self) {
642643
self.linker_arg("--eh-frame-hdr");
643644
}
645+
646+
fn add_as_needed(&mut self) {
647+
if self.sess.target.linker_is_gnu {
648+
self.linker_arg("--as-needed");
649+
}
650+
}
644651
}
645652

646653
pub struct MsvcLinker<'a> {

compiler/rustc_target/src/spec/avr_gnu_base.rs

+3-16
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,9 @@ pub fn target(target_cpu: String) -> Target {
2121
has_rpath: false,
2222
position_independent_executables: false,
2323
eh_frame_header: false,
24-
pre_link_args: vec![(
25-
LinkerFlavor::Gcc,
26-
vec![
27-
format!("-mmcu={}", target_cpu),
28-
// We want to be able to strip as much executable code as possible
29-
// from the linker command line, and this flag indicates to the
30-
// linker that it can avoid linking in dynamic libraries that don't
31-
// actually satisfy any symbols up to that point (as with many other
32-
// resolutions the linker does). This option only applies to all
33-
// following libraries so we're sure to pass it as one of the first
34-
// arguments.
35-
"-Wl,--as-needed".to_string(),
36-
],
37-
)]
38-
.into_iter()
39-
.collect(),
24+
pre_link_args: vec![(LinkerFlavor::Gcc, vec![format!("-mmcu={}", target_cpu)])]
25+
.into_iter()
26+
.collect(),
4027
late_link_args: vec![(LinkerFlavor::Gcc, vec!["-lgcc".to_owned()])]
4128
.into_iter()
4229
.collect(),

compiler/rustc_target/src/spec/dragonfly_base.rs

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ pub fn opts() -> TargetOptions {
55
args.insert(
66
LinkerFlavor::Gcc,
77
vec![
8-
// GNU-style linkers will use this to omit linking to libraries
9-
// which don't actually fulfill any relocations, but only for
10-
// libraries which follow this flag. Thus, use it before
11-
// specifying libraries to link to.
12-
"-Wl,--as-needed".to_string(),
138
// Always enable NX protection when it is available
149
"-Wl,-z,noexecstack".to_string(),
1510
],

compiler/rustc_target/src/spec/freebsd_base.rs

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ pub fn opts() -> TargetOptions {
55
args.insert(
66
LinkerFlavor::Gcc,
77
vec![
8-
// GNU-style linkers will use this to omit linking to libraries
9-
// which don't actually fulfill any relocations, but only for
10-
// libraries which follow this flag. Thus, use it before
11-
// specifying libraries to link to.
12-
"-Wl,--as-needed".to_string(),
138
// Always enable NX protection when it is available
149
"-Wl,-z,noexecstack".to_string(),
1510
],

compiler/rustc_target/src/spec/i686_unknown_netbsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub fn target() -> Target {
44
let mut base = super::netbsd_base::opts();
55
base.cpu = "pentium4".to_string();
66
base.max_atomic_width = Some(64);
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m32".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".to_string());
88
base.stack_probes = StackProbeType::InlineOrCall { min_llvm_version_for_inline: (11, 0, 1) };
99

1010
Target {

compiler/rustc_target/src/spec/i686_wrs_vxworks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub fn target() -> Target {
44
let mut base = super::vxworks_base::opts();
55
base.cpu = "pentium4".to_string();
66
base.max_atomic_width = Some(64);
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m32".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".to_string());
88
base.stack_probes = StackProbeType::InlineOrCall { min_llvm_version_for_inline: (11, 0, 1) };
99

1010
Target {

compiler/rustc_target/src/spec/linux_base.rs

-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ pub fn opts() -> TargetOptions {
55
args.insert(
66
LinkerFlavor::Gcc,
77
vec![
8-
// We want to be able to strip as much executable code as possible
9-
// from the linker command line, and this flag indicates to the
10-
// linker that it can avoid linking in dynamic libraries that don't
11-
// actually satisfy any symbols up to that point (as with many other
12-
// resolutions the linker does). This option only applies to all
13-
// following libraries so we're sure to pass it as one of the first
14-
// arguments.
15-
"-Wl,--as-needed".to_string(),
168
// Always enable NX protection when it is available
179
"-Wl,-z,noexecstack".to_string(),
1810
],

compiler/rustc_target/src/spec/linux_kernel_base.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use crate::spec::{
44

55
pub fn opts() -> TargetOptions {
66
let mut pre_link_args = LinkArgs::new();
7-
pre_link_args.insert(
8-
LinkerFlavor::Gcc,
9-
vec!["-Wl,--as-needed".to_string(), "-Wl,-z,noexecstack".to_string()],
10-
);
7+
pre_link_args.insert(LinkerFlavor::Gcc, vec!["-Wl,-z,noexecstack".to_string()]);
118

129
TargetOptions {
1310
env: "gnu".to_string(),

compiler/rustc_target/src/spec/netbsd_base.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, RelroLevel, TargetOptions};
1+
use crate::spec::{RelroLevel, TargetOptions};
22

33
pub fn opts() -> TargetOptions {
4-
let mut args = LinkArgs::new();
5-
args.insert(
6-
LinkerFlavor::Gcc,
7-
vec![
8-
// GNU-style linkers will use this to omit linking to libraries
9-
// which don't actually fulfill any relocations, but only for
10-
// libraries which follow this flag. Thus, use it before
11-
// specifying libraries to link to.
12-
"-Wl,--as-needed".to_string(),
13-
],
14-
);
15-
164
TargetOptions {
175
os: "netbsd".to_string(),
186
dynamic_linking: true,
@@ -21,7 +9,6 @@ pub fn opts() -> TargetOptions {
219
linker_is_gnu: true,
2210
no_default_libraries: false,
2311
has_rpath: true,
24-
pre_link_args: args,
2512
position_independent_executables: true,
2613
relro_level: RelroLevel::Full,
2714
use_ctors_section: true,

compiler/rustc_target/src/spec/openbsd_base.rs

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ pub fn opts() -> TargetOptions {
55
args.insert(
66
LinkerFlavor::Gcc,
77
vec![
8-
// GNU-style linkers will use this to omit linking to libraries
9-
// which don't actually fulfill any relocations, but only for
10-
// libraries which follow this flag. Thus, use it before
11-
// specifying libraries to link to.
12-
"-Wl,--as-needed".to_string(),
138
// Always enable NX protection when it is available
149
"-Wl,-z,noexecstack".to_string(),
1510
],

compiler/rustc_target/src/spec/powerpc64_wrs_vxworks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::spec::{LinkerFlavor, Target, TargetOptions};
44
pub fn target() -> Target {
55
let mut base = super::vxworks_base::opts();
66
base.cpu = "ppc64".to_string();
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m64".to_string());
88
base.max_atomic_width = Some(64);
99

1010
Target {

compiler/rustc_target/src/spec/powerpc_unknown_netbsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::spec::{LinkerFlavor, Target, TargetOptions};
33

44
pub fn target() -> Target {
55
let mut base = super::netbsd_base::opts();
6-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m32".to_string());
6+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".to_string());
77
base.max_atomic_width = Some(32);
88

99
Target {

compiler/rustc_target/src/spec/powerpc_wrs_vxworks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::spec::{LinkerFlavor, Target, TargetOptions};
33

44
pub fn target() -> Target {
55
let mut base = super::vxworks_base::opts();
6-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m32".to_string());
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("--secure-plt".to_string());
6+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m32".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("--secure-plt".to_string());
88
base.max_atomic_width = Some(32);
99

1010
Target {

compiler/rustc_target/src/spec/powerpc_wrs_vxworks_spe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::spec::{LinkerFlavor, Target, TargetOptions};
33

44
pub fn target() -> Target {
55
let mut base = super::vxworks_base::opts();
6-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-mspe".to_string());
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("--secure-plt".to_string());
6+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-mspe".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("--secure-plt".to_string());
88
base.max_atomic_width = Some(32);
99

1010
Target {

compiler/rustc_target/src/spec/redox_base.rs

-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ pub fn opts() -> TargetOptions {
55
args.insert(
66
LinkerFlavor::Gcc,
77
vec![
8-
// We want to be able to strip as much executable code as possible
9-
// from the linker command line, and this flag indicates to the
10-
// linker that it can avoid linking in dynamic libraries that don't
11-
// actually satisfy any symbols up to that point (as with many other
12-
// resolutions the linker does). This option only applies to all
13-
// following libraries so we're sure to pass it as one of the first
14-
// arguments.
15-
"-Wl,--as-needed".to_string(),
168
// Always enable NX protection when it is available
179
"-Wl,-z,noexecstack".to_string(),
1810
],

compiler/rustc_target/src/spec/sparc64_unknown_netbsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::spec::{LinkerFlavor, Target, TargetOptions};
44
pub fn target() -> Target {
55
let mut base = super::netbsd_base::opts();
66
base.cpu = "v9".to_string();
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m64".to_string());
88
base.max_atomic_width = Some(64);
99

1010
Target {

compiler/rustc_target/src/spec/vxworks_base.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
1-
use crate::spec::{LinkArgs, LinkerFlavor, TargetOptions};
1+
use crate::spec::TargetOptions;
22

33
pub fn opts() -> TargetOptions {
4-
let mut args = LinkArgs::new();
5-
args.insert(
6-
LinkerFlavor::Gcc,
7-
vec![
8-
// We want to be able to strip as much executable code as possible
9-
// from the linker command line, and this flag indicates to the
10-
// linker that it can avoid linking in dynamic libraries that don't
11-
// actually satisfy any symbols up to that point (as with many other
12-
// resolutions the linker does). This option only applies to all
13-
// following libraries so we're sure to pass it as one of the first
14-
// arguments.
15-
"-Wl,--as-needed".to_string(),
16-
],
17-
);
18-
194
TargetOptions {
205
os: "vxworks".to_string(),
216
env: "gnu".to_string(),
@@ -27,7 +12,6 @@ pub fn opts() -> TargetOptions {
2712
os_family: Some("unix".to_string()),
2813
linker_is_gnu: true,
2914
has_rpath: true,
30-
pre_link_args: args,
3115
position_independent_executables: false,
3216
has_elf_tls: true,
3317
crt_static_default: true,

compiler/rustc_target/src/spec/x86_64_fortanix_unknown_sgx.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use super::{LinkerFlavor, LldFlavor, PanicStrategy, Target, TargetOptions};
44

55
pub fn target() -> Target {
66
const PRE_LINK_ARGS: &[&str] = &[
7-
"--as-needed",
87
"-z",
98
"noexecstack",
109
"-e",

compiler/rustc_target/src/spec/x86_64_unknown_netbsd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub fn target() -> Target {
44
let mut base = super::netbsd_base::opts();
55
base.cpu = "x86-64".to_string();
66
base.max_atomic_width = Some(64);
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m64".to_string());
88
base.stack_probes = StackProbeType::InlineOrCall { min_llvm_version_for_inline: (11, 0, 1) };
99

1010
Target {

compiler/rustc_target/src/spec/x86_64_wrs_vxworks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub fn target() -> Target {
44
let mut base = super::vxworks_base::opts();
55
base.cpu = "x86-64".to_string();
66
base.max_atomic_width = Some(64);
7-
base.pre_link_args.get_mut(&LinkerFlavor::Gcc).unwrap().push("-m64".to_string());
7+
base.pre_link_args.entry(LinkerFlavor::Gcc).or_default().push("-m64".to_string());
88
base.stack_probes = StackProbeType::InlineOrCall { min_llvm_version_for_inline: (11, 0, 1) };
99
base.disable_redzone = true;
1010

0 commit comments

Comments
 (0)