about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa/src
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2024-01-18 17:41:18 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2024-01-24 01:51:43 +0300
commitd15db6b26071b4d9550c2237283ba602bb4c1a53 (patch)
tree08a2c652d26531b84543b0b615cbb2dcac473358 /compiler/rustc_codegen_ssa/src
parent859f37ae869bcfe059b1f6ecdf10174d9d87d5b5 (diff)
downloadrust-d15db6b26071b4d9550c2237283ba602bb4c1a53.tar.gz
rust-d15db6b26071b4d9550c2237283ba602bb4c1a53.zip
linker: Merge `link_staticlib_*` and `link_whole_staticlib_*`
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
-rw-r--r--compiler/rustc_codegen_ssa/src/back/link.rs18
-rw-r--r--compiler/rustc_codegen_ssa/src/back/linker.rs198
2 files changed, 86 insertions, 130 deletions
diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs
index 5507f04fea1..e3b4189b3f5 100644
--- a/compiler/rustc_codegen_ssa/src/back/link.rs
+++ b/compiler/rustc_codegen_ssa/src/back/link.rs
@@ -1282,7 +1282,7 @@ fn link_sanitizer_runtime(
     } else {
         let filename = format!("librustc{channel}_rt.{name}.a");
         let path = find_sanitizer_runtime(sess, &filename).join(&filename);
-        linker.link_whole_staticlib_by_path(&path);
+        linker.link_staticlib_by_path(&path, true);
     }
 }
 
@@ -2514,18 +2514,10 @@ fn add_native_libs_from_crate(
                         if let Some(filename) = lib.filename {
                             // If rlib contains native libs as archives, they are unpacked to tmpdir.
                             let path = tmpdir.join(filename.as_str());
-                            if whole_archive {
-                                cmd.link_whole_staticlib_by_path(&path);
-                            } else {
-                                cmd.link_staticlib_by_path(&path);
-                            }
+                            cmd.link_staticlib_by_path(&path, whole_archive);
                         }
                     } else {
-                        if whole_archive {
-                            cmd.link_whole_staticlib_by_name(name, verbatim, search_paths);
-                        } else {
-                            cmd.link_staticlib_by_name(name, verbatim)
-                        }
+                        cmd.link_staticlib_by_name(name, verbatim, whole_archive, search_paths);
                     }
                 }
             }
@@ -2539,7 +2531,7 @@ fn add_native_libs_from_crate(
                 // link kind is unspecified.
                 if !link_output_kind.can_link_dylib() && !sess.target.crt_static_allows_dylibs {
                     if link_static {
-                        cmd.link_staticlib_by_name(name, verbatim)
+                        cmd.link_staticlib_by_name(name, verbatim, false, search_paths);
                     }
                 } else {
                     if link_dynamic {
@@ -2796,7 +2788,7 @@ fn add_static_crate<'a>(
         } else {
             fix_windows_verbatim_for_gcc(path)
         };
-        cmd.link_staticlib_by_path(&rlib_path);
+        cmd.link_staticlib_by_path(&rlib_path, false);
     };
 
     if !are_upstream_rust_objects_already_included(sess)
diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs
index 4b38be6f188..092b66e3868 100644
--- a/compiler/rustc_codegen_ssa/src/back/linker.rs
+++ b/compiler/rustc_codegen_ssa/src/back/linker.rs
@@ -171,15 +171,14 @@ pub trait Linker {
     fn link_framework_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) {
         bug!("framework linked with unsupported linker")
     }
-    fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool);
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         name: &str,
         verbatim: bool,
+        whole_archive: bool,
         search_paths: &SearchPaths,
     );
-    fn link_staticlib_by_path(&mut self, path: &Path);
-    fn link_whole_staticlib_by_path(&mut self, path: &Path);
+    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool);
     fn include_path(&mut self, path: &Path);
     fn framework_path(&mut self, path: &Path);
     fn output_filename(&mut self, path: &Path);
@@ -482,26 +481,17 @@ impl<'a> Linker for GccLinker<'a> {
         self.cmd.arg("-framework").arg(name);
     }
 
-    fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) {
-        self.hint_static();
-        self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" },));
-    }
-
-    // Here we explicitly ask that the entire archive is included into the
-    // result artifact. For more details see #15460, but the gist is that
-    // the linker will strip away any unused objects in the archive if we
-    // don't otherwise explicitly reference them. This can occur for
-    // libraries which are just providing bindings, libraries with generic
-    // functions, etc.
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         name: &str,
         verbatim: bool,
+        whole_archive: bool,
         search_paths: &SearchPaths,
     ) {
         self.hint_static();
-        let target = &self.sess.target;
-        if !target.is_like_osx {
+        if !whole_archive {
+            self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
+        } else if !self.sess.target.is_like_osx {
             self.linker_arg("--whole-archive");
             self.cmd.arg(format!("-l{}{name}", if verbatim && self.is_gnu { ":" } else { "" }));
             self.linker_arg("--no-whole-archive");
@@ -515,14 +505,11 @@ impl<'a> Linker for GccLinker<'a> {
         }
     }
 
-    fn link_staticlib_by_path(&mut self, path: &Path) {
+    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
         self.hint_static();
-        self.cmd.arg(path);
-    }
-
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
-        self.hint_static();
-        if self.sess.target.is_like_osx {
+        if !whole_archive {
+            self.cmd.arg(path);
+        } else if self.sess.target.is_like_osx {
             self.linker_arg("-force_load");
             self.linker_arg(&path);
         } else {
@@ -836,27 +823,28 @@ impl<'a> Linker for MsvcLinker<'a> {
         self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
     }
 
-    fn link_staticlib_by_name(&mut self, name: &str, verbatim: bool) {
-        self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
-    }
-
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         name: &str,
         verbatim: bool,
+        whole_archive: bool,
         _search_paths: &SearchPaths,
     ) {
-        self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
-    }
-
-    fn link_staticlib_by_path(&mut self, path: &Path) {
-        self.cmd.arg(path);
+        if !whole_archive {
+            self.cmd.arg(format!("{}{}", name, if verbatim { "" } else { ".lib" }));
+        } else {
+            self.cmd.arg(format!("/WHOLEARCHIVE:{}{}", name, if verbatim { "" } else { ".lib" }));
+        }
     }
 
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
-        let mut arg = OsString::from("/WHOLEARCHIVE:");
-        arg.push(path);
-        self.cmd.arg(arg);
+    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
+        if !whole_archive {
+            self.cmd.arg(path);
+        } else {
+            let mut arg = OsString::from("/WHOLEARCHIVE:");
+            arg.push(path);
+            self.cmd.arg(arg);
+        }
     }
 
     fn add_object(&mut self, path: &Path) {
@@ -1062,34 +1050,25 @@ impl<'a> Linker for EmLinker<'a> {
 
     fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {}
 
-    fn link_dylib_by_name(&mut self, name: &str, verbatim: bool, _as_needed: bool) {
+    fn link_dylib_by_name(&mut self, name: &str, _verbatim: bool, _as_needed: bool) {
         // Emscripten always links statically
-        self.link_staticlib_by_name(name, verbatim);
-    }
-
-    fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
         self.cmd.arg("-l").arg(name);
     }
 
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         name: &str,
-        verbatim: bool,
+        _verbatim: bool,
+        _whole_archive: bool,
         _search_paths: &SearchPaths,
     ) {
-        // not supported?
-        self.link_staticlib_by_name(name, verbatim);
+        self.cmd.arg("-l").arg(name);
     }
 
-    fn link_staticlib_by_path(&mut self, path: &Path) {
+    fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
         self.add_object(path);
     }
 
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
-        // not supported?
-        self.link_staticlib_by_path(path);
-    }
-
     fn include_path(&mut self, path: &Path) {
         self.cmd.arg("-L").arg(path);
     }
@@ -1255,25 +1234,26 @@ impl<'a> Linker for WasmLd<'a> {
         self.cmd.arg("-l").arg(name);
     }
 
-    fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
-        self.cmd.arg("-l").arg(name);
-    }
-
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         name: &str,
         _verbatim: bool,
+        whole_archive: bool,
         _search_paths: &SearchPaths,
     ) {
-        self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive");
-    }
-
-    fn link_staticlib_by_path(&mut self, path: &Path) {
-        self.cmd.arg(path);
+        if !whole_archive {
+            self.cmd.arg("-l").arg(name);
+        } else {
+            self.cmd.arg("--whole-archive").arg("-l").arg(name).arg("--no-whole-archive");
+        }
     }
 
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
-        self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
+    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
+        if !whole_archive {
+            self.cmd.arg(path);
+        } else {
+            self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
+        }
     }
 
     fn include_path(&mut self, path: &Path) {
@@ -1407,30 +1387,29 @@ impl<'a> Linker for L4Bender<'a> {
         bug!("dylibs are not supported on L4Re");
     }
 
-    fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
-        self.hint_static();
-        self.cmd.arg(format!("-PC{name}"));
-    }
-
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         name: &str,
         _verbatim: bool,
+        whole_archive: bool,
         _search_paths: &SearchPaths,
     ) {
         self.hint_static();
-        self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
-        self.cmd.arg("--no-whole-archive");
-    }
-
-    fn link_staticlib_by_path(&mut self, path: &Path) {
-        self.hint_static();
-        self.cmd.arg(path);
+        if !whole_archive {
+            self.cmd.arg(format!("-PC{name}"));
+        } else {
+            self.cmd.arg("--whole-archive").arg(format!("-l{name}"));
+            self.cmd.arg("--no-whole-archive");
+        }
     }
 
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
+    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
         self.hint_static();
-        self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
+        if !whole_archive {
+            self.cmd.arg(path);
+        } else {
+            self.cmd.arg("--whole-archive").arg(path).arg("--no-whole-archive");
+        }
     }
 
     fn include_path(&mut self, path: &Path) {
@@ -1593,31 +1572,30 @@ impl<'a> Linker for AixLinker<'a> {
         self.cmd.arg(format!("-l{name}"));
     }
 
-    fn link_staticlib_by_name(&mut self, name: &str, _verbatim: bool) {
-        self.hint_static();
-        self.cmd.arg(format!("-l{name}"));
-    }
-
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         name: &str,
         verbatim: bool,
+        whole_archive: bool,
         search_paths: &SearchPaths,
     ) {
         self.hint_static();
-        let lib =
-            find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
-        self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap()));
-    }
-
-    fn link_staticlib_by_path(&mut self, path: &Path) {
-        self.hint_static();
-        self.cmd.arg(path);
+        if !whole_archive {
+            self.cmd.arg(format!("-l{name}"));
+        } else {
+            let lib =
+                find_native_static_library(name, verbatim, search_paths.get(self.sess), self.sess);
+            self.cmd.arg(format!("-bkeepfile:{}", lib.to_str().unwrap()));
+        }
     }
 
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
+    fn link_staticlib_by_path(&mut self, path: &Path, whole_archive: bool) {
         self.hint_static();
-        self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
+        if !whole_archive {
+            self.cmd.arg(path);
+        } else {
+            self.cmd.arg(format!("-bkeepfile:{}", path.to_str().unwrap()));
+        }
     }
 
     fn include_path(&mut self, path: &Path) {
@@ -1810,24 +1788,17 @@ impl<'a> Linker for PtxLinker<'a> {
         panic!("external dylibs not supported")
     }
 
-    fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) {
-        panic!("staticlibs not supported")
-    }
-
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         _name: &str,
         _verbatim: bool,
+        _whole_archive: bool,
         _search_paths: &SearchPaths,
     ) {
         panic!("staticlibs not supported")
     }
 
-    fn link_staticlib_by_path(&mut self, path: &Path) {
-        self.cmd.arg("--rlib").arg(path);
-    }
-
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
+    fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
         self.cmd.arg("--rlib").arg(path);
     }
 
@@ -1904,24 +1875,17 @@ impl<'a> Linker for BpfLinker<'a> {
         panic!("external dylibs not supported")
     }
 
-    fn link_staticlib_by_name(&mut self, _name: &str, _verbatim: bool) {
-        panic!("staticlibs not supported")
-    }
-
-    fn link_whole_staticlib_by_name(
+    fn link_staticlib_by_name(
         &mut self,
         _name: &str,
         _verbatim: bool,
+        _whole_archive: bool,
         _search_paths: &SearchPaths,
     ) {
         panic!("staticlibs not supported")
     }
 
-    fn link_staticlib_by_path(&mut self, path: &Path) {
-        self.cmd.arg(path);
-    }
-
-    fn link_whole_staticlib_by_path(&mut self, path: &Path) {
+    fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) {
         self.cmd.arg(path);
     }