Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
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