Skip to content

Commit bab6911

Browse files
committed
Tell the linker when we want to link a static executable
If the C runtime is linked statically, explicitly tell the linker that the executable should be static.
1 parent 054f310 commit bab6911

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/librustc_trans/back/link.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,13 @@ fn link_args(cmd: &mut Linker,
966966
add_upstream_rust_crates(cmd, sess, crate_type, tmpdir);
967967
add_upstream_native_libraries(cmd, sess, crate_type);
968968

969-
// # Telling the linker what we're doing
970-
969+
// Tell the linker what we're doing.
971970
if crate_type != config::CrateTypeExecutable {
972971
cmd.build_dylib(out_filename);
973972
}
973+
if crate_type == config::CrateTypeExecutable && sess.crt_static() {
974+
cmd.build_static_executable();
975+
}
974976

975977
// FIXME (#2397): At some point we want to rpath our guesses as to
976978
// where extern libraries might live, based on the

src/librustc_trans/back/linker.rs

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub trait Linker {
110110
fn debuginfo(&mut self);
111111
fn no_default_libraries(&mut self);
112112
fn build_dylib(&mut self, out_filename: &Path);
113+
fn build_static_executable(&mut self);
113114
fn args(&mut self, args: &[String]);
114115
fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
115116
fn subsystem(&mut self, subsystem: &str);
@@ -179,6 +180,7 @@ impl<'a> Linker for GccLinker<'a> {
179180
fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
180181
fn partial_relro(&mut self) { self.linker_arg("-z,relro"); }
181182
fn full_relro(&mut self) { self.linker_arg("-z,relro,-z,now"); }
183+
fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
182184
fn args(&mut self, args: &[String]) { self.cmd.args(args); }
183185

184186
fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
@@ -396,6 +398,10 @@ impl<'a> Linker for MsvcLinker<'a> {
396398
self.cmd.arg(arg);
397399
}
398400

401+
fn build_static_executable(&mut self) {
402+
// noop
403+
}
404+
399405
fn gc_sections(&mut self, _keep_metadata: bool) {
400406
// MSVC's ICF (Identical COMDAT Folding) link optimization is
401407
// slow for Rust and thus we disable it by default when not in
@@ -683,6 +689,10 @@ impl<'a> Linker for EmLinker<'a> {
683689
bug!("building dynamic library is unsupported on Emscripten")
684690
}
685691

692+
fn build_static_executable(&mut self) {
693+
// noop
694+
}
695+
686696
fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
687697
let symbols = &self.info.exports[&crate_type];
688698

0 commit comments

Comments
 (0)