Skip to content

Commit

Permalink
Rollup merge of rust-lang#85274 - luqmana:linker-is-gnu-gc-sections, …
Browse files Browse the repository at this point in the history
…r=petrochenkov

Only pass --[no-]gc-sections if linker is GNU ld.

Fixes a regression from rust-lang#84468 where linking now fails with solaris linkers. LinkerFlavor::Gcc does not always mean GNU ld specifically. And in the case of at least the solaris ld in illumos, that flag is unrecognized and will cause the linking step to fail.

Even though removing the `is_like_solaris` branch from `gc_sections` in rust-lang#84468 made sense as `-z ignore/record` are more analogous to the `--[no-]-as-needed` flags, it inadvertently caused solaris linkers to be passed the `--gc-sections` flag. So let's just change it to be more explicit about when we pass those flags.
  • Loading branch information
jackh726 committed May 19, 2021
2 parents 37ecede + ac5fd90 commit ec0e0d1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
19 changes: 10 additions & 9 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,11 @@ impl<'a> Linker for GccLinker<'a> {
}
}
LinkOutputKind::DynamicPicExe => {
// `-pie` works for both gcc wrapper and ld.
self.cmd.arg("-pie");
// noop on windows w/ gcc & ld, error w/ lld
if !self.sess.target.is_like_windows {
// `-pie` works for both gcc wrapper and ld.
self.cmd.arg("-pie");
}
}
LinkOutputKind::StaticNoPicExe => {
// `-static` works for both gcc wrapper and ld.
Expand Down Expand Up @@ -347,7 +350,7 @@ impl<'a> Linker for GccLinker<'a> {
// has -needed-l{} / -needed_library {}
// but we have no way to detect that here.
self.sess.warn("`as-needed` modifier not implemented yet for ld64");
} else if self.sess.target.linker_is_gnu {
} else if self.sess.target.linker_is_gnu && !self.sess.target.is_like_windows {
self.linker_arg("--no-as-needed");
} else {
self.sess.warn("`as-needed` modifier not supported for current linker");
Expand All @@ -358,7 +361,7 @@ impl<'a> Linker for GccLinker<'a> {
if !as_needed {
if self.sess.target.is_like_osx {
// See above FIXME comment
} else if self.sess.target.linker_is_gnu {
} else if self.sess.target.linker_is_gnu && !self.sess.target.is_like_windows {
self.linker_arg("--as-needed");
}
}
Expand Down Expand Up @@ -469,17 +472,15 @@ impl<'a> Linker for GccLinker<'a> {
// eliminate the metadata. If we're building an executable, however,
// --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
// reduction.
} else if !keep_metadata {
} else if self.sess.target.linker_is_gnu && !keep_metadata {
self.linker_arg("--gc-sections");
}
}

fn no_gc_sections(&mut self) {
if self.sess.target.is_like_osx {
self.linker_arg("-no_dead_strip");
} else if self.sess.target.is_like_solaris {
self.linker_arg("-zrecord");
} else {
} else if self.sess.target.linker_is_gnu {
self.linker_arg("--no-gc-sections");
}
}
Expand Down Expand Up @@ -692,7 +693,7 @@ impl<'a> Linker for GccLinker<'a> {
}

fn add_as_needed(&mut self) {
if self.sess.target.linker_is_gnu {
if self.sess.target.linker_is_gnu && !self.sess.target.is_like_windows {
self.linker_arg("--as-needed");
} else if self.sess.target.is_like_solaris {
// -z ignore is the Solaris equivalent to the GNU ld --as-needed option
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/windows_gnu_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn opts() -> TargetOptions {
// FIXME(#13846) this should be enabled for windows
function_sections: false,
linker: Some("gcc".to_string()),
linker_is_gnu: true,
dynamic_linking: true,
executables: true,
dll_prefix: String::new(),
Expand Down

0 comments on commit ec0e0d1

Please sign in to comment.