about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa/src
diff options
context:
space:
mode:
authorMads Marquart <mads@marquart.dk>2024-11-13 00:27:14 +0100
committerMads Marquart <mads@marquart.dk>2024-11-24 01:20:29 +0100
commitcb6f8fa422ce14b82b60af79d0f0d0ad80936ecd (patch)
tree001ba94da42e183051521e19dfa66d1de5bc58d9 /compiler/rustc_codegen_ssa/src
parent45791dde8e0d258d794e561b9dff53f1dbb16377 (diff)
downloadrust-cb6f8fa422ce14b82b60af79d0f0d0ad80936ecd.tar.gz
rust-cb6f8fa422ce14b82b60af79d0f0d0ad80936ecd.zip
Support rpath with -Clinker-flavor=ld
Using `cc_args` panics when using `-Clinker-flavor=ld`, because the
arguments are in a form tailored for `-Clinker-flavor=gcc`.

So instead, we use `link_args` and let that wrap the arguments with the
appropriate `-Wl` or `-Xlinker` when needed.
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
-rw-r--r--compiler/rustc_codegen_ssa/src/back/link.rs4
-rw-r--r--compiler/rustc_codegen_ssa/src/back/linker/tests.rs7
-rw-r--r--compiler/rustc_codegen_ssa/src/back/rpath.rs34
-rw-r--r--compiler/rustc_codegen_ssa/src/back/rpath/tests.rs23
4 files changed, 21 insertions, 47 deletions
diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs
index 5149e3a12f2..ea184bb16aa 100644
--- a/compiler/rustc_codegen_ssa/src/back/link.rs
+++ b/compiler/rustc_codegen_ssa/src/back/link.rs
@@ -1384,7 +1384,7 @@ fn link_sanitizer_runtime(
         let filename = format!("rustc{channel}_rt.{name}");
         let path = find_sanitizer_runtime(sess, &filename);
         let rpath = path.to_str().expect("non-utf8 component in path");
-        linker.cc_args(&["-Wl,-rpath", "-Xlinker", rpath]);
+        linker.link_args(&["-rpath", rpath]);
         linker.link_dylib_by_name(&filename, false, true);
     } else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
         // MSVC provides the `/INFERASANLIBS` argument to automatically find the
@@ -2208,7 +2208,7 @@ fn add_rpath_args(
             is_like_osx: sess.target.is_like_osx,
             linker_is_gnu: sess.target.linker_flavor.is_gnu(),
         };
-        cmd.cc_args(&rpath::get_rpath_flags(&rpath_config));
+        cmd.link_args(&rpath::get_rpath_linker_args(&rpath_config));
     }
 }
 
diff --git a/compiler/rustc_codegen_ssa/src/back/linker/tests.rs b/compiler/rustc_codegen_ssa/src/back/linker/tests.rs
index 293ed6634ae..bf3e8c90200 100644
--- a/compiler/rustc_codegen_ssa/src/back/linker/tests.rs
+++ b/compiler/rustc_codegen_ssa/src/back/linker/tests.rs
@@ -1,6 +1,13 @@
 use super::*;
 
 #[test]
+fn test_rpaths_to_args() {
+    let mut cmd = Command::new("foo");
+    convert_link_args_to_cc_args(&mut cmd, &["-rpath", "path1", "-rpath", "path2"]);
+    assert_eq!(cmd.get_args(), [OsStr::new("-Wl,-rpath,path1,-rpath,path2")]);
+}
+
+#[test]
 fn test_xlinker() {
     let mut cmd = Command::new("foo");
     convert_link_args_to_cc_args(&mut cmd, &[
diff --git a/compiler/rustc_codegen_ssa/src/back/rpath.rs b/compiler/rustc_codegen_ssa/src/back/rpath.rs
index 56a808df6b0..d633cc98ac8 100644
--- a/compiler/rustc_codegen_ssa/src/back/rpath.rs
+++ b/compiler/rustc_codegen_ssa/src/back/rpath.rs
@@ -13,39 +13,27 @@ pub(super) struct RPathConfig<'a> {
     pub linker_is_gnu: bool,
 }
 
-pub(super) fn get_rpath_flags(config: &RPathConfig<'_>) -> Vec<OsString> {
+pub(super) fn get_rpath_linker_args(config: &RPathConfig<'_>) -> Vec<OsString> {
     debug!("preparing the RPATH!");
 
     let rpaths = get_rpaths(config);
-    let mut flags = rpaths_to_flags(rpaths);
+    let mut args = Vec::with_capacity(rpaths.len() * 2); // the minimum needed capacity
+
+    for rpath in rpaths {
+        args.push("-rpath".into());
+        args.push(rpath);
+    }
 
     if config.linker_is_gnu {
         // Use DT_RUNPATH instead of DT_RPATH if available
-        flags.push("-Wl,--enable-new-dtags".into());
+        args.push("--enable-new-dtags".into());
 
         // Set DF_ORIGIN for substitute $ORIGIN
-        flags.push("-Wl,-z,origin".into());
-    }
-
-    flags
-}
-
-fn rpaths_to_flags(rpaths: Vec<OsString>) -> Vec<OsString> {
-    let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
-
-    for rpath in rpaths {
-        if rpath.to_string_lossy().contains(',') {
-            ret.push("-Wl,-rpath".into());
-            ret.push("-Xlinker".into());
-            ret.push(rpath);
-        } else {
-            let mut single_arg = OsString::from("-Wl,-rpath,");
-            single_arg.push(rpath);
-            ret.push(single_arg);
-        }
+        args.push("-z".into());
+        args.push("origin".into());
     }
 
-    ret
+    args
 }
 
 fn get_rpaths(config: &RPathConfig<'_>) -> Vec<OsString> {
diff --git a/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs b/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
index 39034842d10..f1a30105c59 100644
--- a/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
+++ b/compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
@@ -1,13 +1,4 @@
-use std::ffi::OsString;
-use std::path::{Path, PathBuf};
-
-use super::{RPathConfig, get_rpath_relative_to_output, minimize_rpaths, rpaths_to_flags};
-
-#[test]
-fn test_rpaths_to_flags() {
-    let flags = rpaths_to_flags(vec!["path1".into(), "path2".into()]);
-    assert_eq!(flags, ["-Wl,-rpath,path1", "-Wl,-rpath,path2"]);
-}
+use super::*;
 
 #[test]
 fn test_minimize1() {
@@ -69,15 +60,3 @@ fn test_rpath_relative_issue_119571() {
     // Should not panic when lib only contains filename.
     let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
 }
-
-#[test]
-fn test_xlinker() {
-    let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);
-
-    assert_eq!(args, vec![
-        OsString::from("-Wl,-rpath,a/normal/path"),
-        OsString::from("-Wl,-rpath"),
-        OsString::from("-Xlinker"),
-        OsString::from("a,comma,path")
-    ]);
-}