]> git.lizzy.rs Git - rust.git/commitdiff
Tell the linker when we want to link a static executable
authorSamuel Holland <samuel@sholland.org>
Tue, 22 Aug 2017 21:24:29 +0000 (16:24 -0500)
committerSamuel Holland <samuel@sholland.org>
Tue, 22 Aug 2017 21:24:29 +0000 (16:24 -0500)
If the C runtime is linked statically, explicitly tell the linker that
the executable should be static.

src/librustc_trans/back/link.rs
src/librustc_trans/back/linker.rs

index fb485e71d946918dbb2971cb4a875d6b04a24416..4e211d83cff3e79551168f2aac1d2dce38d30daf 100644 (file)
@@ -966,11 +966,13 @@ fn link_args(cmd: &mut Linker,
     add_upstream_rust_crates(cmd, sess, crate_type, tmpdir);
     add_upstream_native_libraries(cmd, sess, crate_type);
 
-    // # Telling the linker what we're doing
-
+    // Tell the linker what we're doing.
     if crate_type != config::CrateTypeExecutable {
         cmd.build_dylib(out_filename);
     }
+    if crate_type == config::CrateTypeExecutable && sess.crt_static() {
+        cmd.build_static_executable();
+    }
 
     // FIXME (#2397): At some point we want to rpath our guesses as to
     // where extern libraries might live, based on the
index ab401465b560b80b09ee460b9ad0d99cb99bbf9c..9b0a5e3f4a5b11603119d84d516d147e5cd843d0 100644 (file)
@@ -110,6 +110,7 @@ pub trait Linker {
     fn debuginfo(&mut self);
     fn no_default_libraries(&mut self);
     fn build_dylib(&mut self, out_filename: &Path);
+    fn build_static_executable(&mut self);
     fn args(&mut self, args: &[String]);
     fn export_symbols(&mut self, tmpdir: &Path, crate_type: CrateType);
     fn subsystem(&mut self, subsystem: &str);
@@ -179,6 +180,7 @@ impl<'a> Linker for GccLinker<'a> {
     fn position_independent_executable(&mut self) { self.cmd.arg("-pie"); }
     fn partial_relro(&mut self) { self.linker_arg("-z,relro"); }
     fn full_relro(&mut self) { self.linker_arg("-z,relro,-z,now"); }
+    fn build_static_executable(&mut self) { self.cmd.arg("-static"); }
     fn args(&mut self, args: &[String]) { self.cmd.args(args); }
 
     fn link_rust_dylib(&mut self, lib: &str, _path: &Path) {
@@ -396,6 +398,10 @@ fn build_dylib(&mut self, out_filename: &Path) {
         self.cmd.arg(arg);
     }
 
+    fn build_static_executable(&mut self) {
+        // noop
+    }
+
     fn gc_sections(&mut self, _keep_metadata: bool) {
         // MSVC's ICF (Identical COMDAT Folding) link optimization is
         // slow for Rust and thus we disable it by default when not in
@@ -683,6 +689,10 @@ fn build_dylib(&mut self, _out_filename: &Path) {
         bug!("building dynamic library is unsupported on Emscripten")
     }
 
+    fn build_static_executable(&mut self) {
+        // noop
+    }
+
     fn export_symbols(&mut self, _tmpdir: &Path, crate_type: CrateType) {
         let symbols = &self.info.exports[&crate_type];