1
Fork 0

Auto merge of #83587 - petrochenkov:asneeded, r=nagisa

linker: Use `--as-needed` by default when linker supports it

Do it in a centralized way in `link.rs` instead of individual target specs.
Majority of relevant target specs were already passing it.
This commit is contained in:
bors 2021-03-28 01:00:25 +00:00
commit 3bfc85149e
21 changed files with 30 additions and 94 deletions

View file

@ -1651,6 +1651,12 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
cmd.add_eh_frame_header();
}
// NO-OPT-OUT, OBJECT-FILES-NO
// Avoid linking to dynamic libraries unless they satisfy some undefined symbols
// at the point at which they are specified on the command line.
// Must be passed before any dynamic libraries.
cmd.add_as_needed();
// NO-OPT-OUT, OBJECT-FILES-NO
if crt_objects_fallback {
cmd.no_crt_objects();

View file

@ -130,6 +130,7 @@ pub trait Linker {
fn group_end(&mut self);
fn linker_plugin_lto(&mut self);
fn add_eh_frame_header(&mut self) {}
fn add_as_needed(&mut self) {}
fn finalize(&mut self);
}
@ -641,6 +642,12 @@ impl<'a> Linker for GccLinker<'a> {
fn add_eh_frame_header(&mut self) {
self.linker_arg("--eh-frame-hdr");
}
fn add_as_needed(&mut self) {
if self.sess.target.linker_is_gnu {
self.linker_arg("--as-needed");
}
}
}
pub struct MsvcLinker<'a> {