Skip to content

Commit b5cc5d7

Browse files
committed
Link with ld.gold by default
To disable, pass `-C disable-gold`
1 parent 685e098 commit b5cc5d7

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/librustc/session/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,8 @@ options! {CodegenOptions, CodegenSetter, basic_codegen_options,
515515
"explicitly enable the cfg(debug_assertions) directive"),
516516
inline_threshold: Option<usize> = (None, parse_opt_uint,
517517
"set the inlining threshold for"),
518+
disable_gold: bool = (false, parse_bool,
519+
"disable use of the ld.gold linker"),
518520
}
519521

520522

src/librustc_trans/back/link.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,10 @@ fn link_args(cmd: &mut Linker,
10601060
cmd.args(&rpath::get_rpath_flags(&mut rpath_config));
10611061
}
10621062

1063+
// Use the gold linker if possible instead of ld. It is much
1064+
// faster.
1065+
cmd.try_gold_linker();
1066+
10631067
// Finally add all the linker arguments provided on the command line along
10641068
// with any #[link_args] attributes found inside the crate
10651069
if let Some(ref args) = sess.opts.cg.link_args {

src/librustc_trans/back/linker.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub trait Linker {
5656
fn no_whole_archives(&mut self);
5757
fn export_symbols(&mut self, sess: &Session, trans: &CrateTranslation,
5858
tmpdir: &Path);
59+
fn try_gold_linker(&mut self);
5960
}
6061

6162
pub struct GnuLinker<'a> {
@@ -199,6 +200,24 @@ impl<'a> Linker for GnuLinker<'a> {
199200
fn export_symbols(&mut self, _: &Session, _: &CrateTranslation, _: &Path) {
200201
// noop, visibility in object files takes care of this
201202
}
203+
204+
fn try_gold_linker(&mut self) {
205+
let gold_exists = Path::new("/usr/bin/ld.gold").exists();
206+
let opt_out = self.sess.opts.cg.disable_gold;
207+
208+
match (gold_exists, opt_out) {
209+
(true, false) => {
210+
info!("linking with ld.gold");
211+
self.cmd.arg("-fuse-ld=gold");
212+
}
213+
(false, _) => {
214+
info!("ld.gold not found. linking with ld");
215+
}
216+
(_, true) => {
217+
info!("opting out of ld.gold");
218+
}
219+
}
220+
}
202221
}
203222

204223
pub struct MsvcLinker<'a> {
@@ -358,4 +377,6 @@ impl<'a> Linker for MsvcLinker<'a> {
358377
arg.push(path);
359378
self.cmd.arg(&arg);
360379
}
380+
381+
fn try_gold_linker(&mut self) {}
361382
}

0 commit comments

Comments
 (0)