From 3fe1d7f78913231cdb7a8748149f7b2e67b814bd Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Thu, 13 May 2021 16:55:33 -0700 Subject: [PATCH 1/5] Only pass --[no-]gc-sections if linker is GNU ld. 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. --- compiler/rustc_codegen_ssa/src/back/linker.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 93059b2323da8..74629fcd118c3 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -469,7 +469,7 @@ 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"); } } @@ -477,9 +477,7 @@ impl<'a> Linker for GccLinker<'a> { 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"); } } From 45225d24bff4bec2eec55f89695876454daa23e2 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Sat, 15 May 2021 22:09:34 -0700 Subject: [PATCH 2/5] Windows mingw targets use gcc as the linker so the target spec should also indicate linker_is_gnu. --- compiler/rustc_target/src/spec/windows_gnu_base.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/rustc_target/src/spec/windows_gnu_base.rs b/compiler/rustc_target/src/spec/windows_gnu_base.rs index 35a52896f6fa6..5808391ee065d 100644 --- a/compiler/rustc_target/src/spec/windows_gnu_base.rs +++ b/compiler/rustc_target/src/spec/windows_gnu_base.rs @@ -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(), From a862b1f6cc904789f046dbef88c944f4c723970c Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Mon, 17 May 2021 16:14:13 -0700 Subject: [PATCH 3/5] Adjust linker_is_gnu branches for cases that don't work on windows. --- compiler/rustc_codegen_ssa/src/back/linker.rs | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 74629fcd118c3..dfcb3fe94f630 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -276,18 +276,27 @@ impl<'a> Linker for GccLinker<'a> { fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) { match output_kind { LinkOutputKind::DynamicNoPicExe => { - if !self.is_ld && self.sess.target.linker_is_gnu { + if !self.is_ld + && self.sess.target.linker_is_gnu + && !self.sess.target.is_like_windows + { self.cmd.arg("-no-pie"); } } 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. self.cmd.arg("-static"); - if !self.is_ld && self.sess.target.linker_is_gnu { + if !self.is_ld + && self.sess.target.linker_is_gnu + && !self.sess.target.is_like_windows + { self.cmd.arg("-no-pie"); } } @@ -347,7 +356,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"); @@ -358,7 +367,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"); } } @@ -690,7 +699,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 From e0d58725037b7a414d1ed60225207a9c708ac313 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 18 May 2021 02:42:29 -0700 Subject: [PATCH 4/5] Undo unnecessary changes. --- compiler/rustc_codegen_ssa/src/back/linker.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index dfcb3fe94f630..2e909c29f557e 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -276,27 +276,18 @@ impl<'a> Linker for GccLinker<'a> { fn set_output_kind(&mut self, output_kind: LinkOutputKind, out_filename: &Path) { match output_kind { LinkOutputKind::DynamicNoPicExe => { - if !self.is_ld - && self.sess.target.linker_is_gnu - && !self.sess.target.is_like_windows - { + if !self.is_ld && self.sess.target.linker_is_gnu { self.cmd.arg("-no-pie"); } } LinkOutputKind::DynamicPicExe => { - // 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"); - } + // `-pie` works for both gcc wrapper and ld. + self.cmd.arg("-pie"); } LinkOutputKind::StaticNoPicExe => { // `-static` works for both gcc wrapper and ld. self.cmd.arg("-static"); - if !self.is_ld - && self.sess.target.linker_is_gnu - && !self.sess.target.is_like_windows - { + if !self.is_ld && self.sess.target.linker_is_gnu { self.cmd.arg("-no-pie"); } } From ac5fd90d822304a6b6e059d84a3c1e4103e24200 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 18 May 2021 03:57:53 -0700 Subject: [PATCH 5/5] Don't pass -pie to linker on windows targets. --- compiler/rustc_codegen_ssa/src/back/linker.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index 2e909c29f557e..bab38789a9f4c 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -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.