Skip to content

Commit 9e20d98

Browse files
authored
Rollup merge of rust-lang#85274 - luqmana:linker-is-gnu-gc-sections, 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.
2 parents cfda7f3 + ac5fd90 commit 9e20d98

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,11 @@ impl<'a> Linker for GccLinker<'a> {
281281
}
282282
}
283283
LinkOutputKind::DynamicPicExe => {
284-
// `-pie` works for both gcc wrapper and ld.
285-
self.cmd.arg("-pie");
284+
// noop on windows w/ gcc & ld, error w/ lld
285+
if !self.sess.target.is_like_windows {
286+
// `-pie` works for both gcc wrapper and ld.
287+
self.cmd.arg("-pie");
288+
}
286289
}
287290
LinkOutputKind::StaticNoPicExe => {
288291
// `-static` works for both gcc wrapper and ld.
@@ -347,7 +350,7 @@ impl<'a> Linker for GccLinker<'a> {
347350
// has -needed-l{} / -needed_library {}
348351
// but we have no way to detect that here.
349352
self.sess.warn("`as-needed` modifier not implemented yet for ld64");
350-
} else if self.sess.target.linker_is_gnu {
353+
} else if self.sess.target.linker_is_gnu && !self.sess.target.is_like_windows {
351354
self.linker_arg("--no-as-needed");
352355
} else {
353356
self.sess.warn("`as-needed` modifier not supported for current linker");
@@ -358,7 +361,7 @@ impl<'a> Linker for GccLinker<'a> {
358361
if !as_needed {
359362
if self.sess.target.is_like_osx {
360363
// See above FIXME comment
361-
} else if self.sess.target.linker_is_gnu {
364+
} else if self.sess.target.linker_is_gnu && !self.sess.target.is_like_windows {
362365
self.linker_arg("--as-needed");
363366
}
364367
}
@@ -469,17 +472,15 @@ impl<'a> Linker for GccLinker<'a> {
469472
// eliminate the metadata. If we're building an executable, however,
470473
// --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
471474
// reduction.
472-
} else if !keep_metadata {
475+
} else if self.sess.target.linker_is_gnu && !keep_metadata {
473476
self.linker_arg("--gc-sections");
474477
}
475478
}
476479

477480
fn no_gc_sections(&mut self) {
478481
if self.sess.target.is_like_osx {
479482
self.linker_arg("-no_dead_strip");
480-
} else if self.sess.target.is_like_solaris {
481-
self.linker_arg("-zrecord");
482-
} else {
483+
} else if self.sess.target.linker_is_gnu {
483484
self.linker_arg("--no-gc-sections");
484485
}
485486
}
@@ -692,7 +693,7 @@ impl<'a> Linker for GccLinker<'a> {
692693
}
693694

694695
fn add_as_needed(&mut self) {
695-
if self.sess.target.linker_is_gnu {
696+
if self.sess.target.linker_is_gnu && !self.sess.target.is_like_windows {
696697
self.linker_arg("--as-needed");
697698
} else if self.sess.target.is_like_solaris {
698699
// -z ignore is the Solaris equivalent to the GNU ld --as-needed option

compiler/rustc_target/src/spec/windows_gnu_base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub fn opts() -> TargetOptions {
6666
// FIXME(#13846) this should be enabled for windows
6767
function_sections: false,
6868
linker: Some("gcc".to_string()),
69+
linker_is_gnu: true,
6970
dynamic_linking: true,
7071
executables: true,
7172
dll_prefix: String::new(),

0 commit comments

Comments
 (0)