-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Link with ld.gold by default #29974
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
Merged
Merged
Link with ld.gold by default #29974
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
use std::env; | ||
use std::ffi::OsString; | ||
use std::fs::{self, File}; | ||
use std::io::{self, BufWriter}; | ||
|
@@ -56,6 +57,7 @@ pub trait Linker { | |
fn no_whole_archives(&mut self); | ||
fn export_symbols(&mut self, sess: &Session, trans: &CrateTranslation, | ||
tmpdir: &Path); | ||
fn try_gold_linker(&mut self); | ||
} | ||
|
||
pub struct GnuLinker<'a> { | ||
|
@@ -199,6 +201,53 @@ impl<'a> Linker for GnuLinker<'a> { | |
fn export_symbols(&mut self, _: &Session, _: &CrateTranslation, _: &Path) { | ||
// noop, visibility in object files takes care of this | ||
} | ||
|
||
fn try_gold_linker(&mut self) { | ||
// Only use gold under specific conditions that we know work | ||
|
||
let gold_exists = match env::var_os("PATH") { | ||
Some(ref env_path) => { | ||
env::split_paths(env_path).any(|mut p| { | ||
p.push("ld.gold"); | ||
p.exists() | ||
}) | ||
} | ||
None => false | ||
}; | ||
let host_is_linux = cfg!(target_os = "linux"); | ||
// Defensively prevent trying to use gold for bogus cross-targets. | ||
let target_is_host_compatible = { | ||
let host_os_is_target_os = self.sess.target.target.target_os == env::consts::OS; | ||
let host_arch_is_target_arch = self.sess.target.target.arch == env::consts::ARCH; | ||
// Support x86_64->i686 and reverse | ||
let host_and_target_are_x86ish = | ||
(self.sess.target.target.arch == "x86" || | ||
self.sess.target.target.arch == "x86_64") && | ||
(env::consts::ARCH == "x86" || | ||
env::consts::ARCH == "x86_64"); | ||
host_os_is_target_os && (host_arch_is_target_arch || host_and_target_are_x86ish) | ||
}; | ||
// We have strong confidence that x86 works, but not much | ||
// visibility into other architectures. | ||
let target_works_with_gold = | ||
self.sess.target.target.arch == "x86" || | ||
self.sess.target.target.arch == "x86_64"; | ||
let opt_out = self.sess.opts.cg.disable_gold; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d rather to just have a regular if here: |
||
|
||
let can_use_gold = | ||
gold_exists && | ||
host_is_linux && | ||
target_is_host_compatible && | ||
target_works_with_gold && | ||
!opt_out; | ||
|
||
if can_use_gold { | ||
info!("linking with ld.gold"); | ||
self.cmd.arg("-fuse-ld=gold"); | ||
} else { | ||
info!("linking with ld"); | ||
} | ||
} | ||
} | ||
|
||
pub struct MsvcLinker<'a> { | ||
|
@@ -358,4 +407,6 @@ impl<'a> Linker for MsvcLinker<'a> { | |
arg.push(path); | ||
self.cmd.arg(&arg); | ||
} | ||
|
||
fn try_gold_linker(&mut self) {} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is… a pretty confusing option (both its description and behaviour).
We already have
-C linker
which possibly swaps outgcc
for something else and now we pass a special flag into linker assuming it isgcc
-compatible (in absence of this option) without checking if specified linker is indeed compatible. That might make-C linker
harder to use.Maybe it would be better to have
-C linker=gcc
which overrides any default/dubious flags we might add?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can tell that is an existing problem. Even if you override the linker, rustc assumes it takes either
gcc
orlink.exe
style arguments, depending on platform. IOW I tihnk-C linker
is only good for specifying a different path to the expected linker, not for using a completely different 'style' of linker.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not super worried about random codegen options being deprecated at some point, an option like this is pretty flavorful and likely to only be a one-time use case (if ever), so if we in the future just hide this and make it ignored by default then it's probably not too bad.
It may be the case that
gcc -fuse-ld=gold -fuse-ld=not-gold
(or some similar incantation) also works, so we could just be sure to allow that pattern, but I don't know if it exists. Either way this seems fine to me for now.