Skip to content

Commit bb24a92

Browse files
committed
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.
1 parent 970cb5b commit bb24a92

File tree

4 files changed

+20
-44
lines changed

4 files changed

+20
-44
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ fn link_sanitizer_runtime(
13681368
let filename = format!("rustc{channel}_rt.{name}");
13691369
let path = find_sanitizer_runtime(sess, &filename);
13701370
let rpath = path.to_str().expect("non-utf8 component in path");
1371-
linker.cc_args(&["-Wl,-rpath", "-Xlinker", rpath]);
1371+
linker.link_args(&["-rpath", rpath]);
13721372
linker.link_dylib_by_name(&filename, false, true);
13731373
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
13741374
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
@@ -2192,7 +2192,7 @@ fn add_rpath_args(
21922192
is_like_osx: sess.target.is_like_osx,
21932193
linker_is_gnu: sess.target.linker_flavor.is_gnu(),
21942194
};
2195-
cmd.cc_args(&rpath::get_rpath_flags(&rpath_config));
2195+
cmd.link_args(&rpath::get_rpath_linker_flags(&rpath_config));
21962196
}
21972197
}
21982198

compiler/rustc_codegen_ssa/src/back/linker/tests.rs

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ use std::ffi::OsStr;
33
use super::convert_link_args_to_cc_args;
44
use crate::back::command::Command;
55

6+
#[test]
7+
fn test_rpaths_to_flags() {
8+
let mut cmd = Command::new("foo");
9+
convert_link_args_to_cc_args(&mut cmd, &["-rpath", "path1", "-rpath", "path2"]);
10+
assert_eq!(cmd.get_args(), [OsStr::new("-Wl,-rpath,path1,-rpath,path2")]);
11+
}
12+
613
#[test]
714
fn test_xlinker() {
815
let mut cmd = Command::new("foo");

compiler/rustc_codegen_ssa/src/back/rpath.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,29 @@ pub(super) struct RPathConfig<'a> {
1313
pub linker_is_gnu: bool,
1414
}
1515

16-
pub(super) fn get_rpath_flags(config: &RPathConfig<'_>) -> Vec<OsString> {
16+
pub(super) fn get_rpath_linker_flags(config: &RPathConfig<'_>) -> Vec<OsString> {
1717
debug!("preparing the RPATH!");
1818

1919
let rpaths = get_rpaths(config);
20-
let mut flags = rpaths_to_flags(rpaths);
20+
let mut flags = Vec::with_capacity(rpaths.len() * 2); // the minimum needed capacity
21+
22+
for rpath in rpaths {
23+
flags.push("-rpath".into());
24+
flags.push(rpath);
25+
}
2126

2227
if config.linker_is_gnu {
2328
// Use DT_RUNPATH instead of DT_RPATH if available
24-
flags.push("-Wl,--enable-new-dtags".into());
29+
flags.push("--enable-new-dtags".into());
2530

2631
// Set DF_ORIGIN for substitute $ORIGIN
27-
flags.push("-Wl,-z,origin".into());
32+
flags.push("-z".into());
33+
flags.push("origin".into());
2834
}
2935

3036
flags
3137
}
3238

33-
fn rpaths_to_flags(rpaths: Vec<OsString>) -> Vec<OsString> {
34-
let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity
35-
36-
for rpath in rpaths {
37-
if rpath.to_string_lossy().contains(',') {
38-
ret.push("-Wl,-rpath".into());
39-
ret.push("-Xlinker".into());
40-
ret.push(rpath);
41-
} else {
42-
let mut single_arg = OsString::from("-Wl,-rpath,");
43-
single_arg.push(rpath);
44-
ret.push(single_arg);
45-
}
46-
}
47-
48-
ret
49-
}
50-
5139
fn get_rpaths(config: &RPathConfig<'_>) -> Vec<OsString> {
5240
debug!("output: {:?}", config.out_filename.display());
5341
debug!("libs:");

compiler/rustc_codegen_ssa/src/back/rpath/tests.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
use std::ffi::OsString;
21
use std::path::{Path, PathBuf};
32

4-
use super::{RPathConfig, get_rpath_relative_to_output, minimize_rpaths, rpaths_to_flags};
5-
6-
#[test]
7-
fn test_rpaths_to_flags() {
8-
let flags = rpaths_to_flags(vec!["path1".into(), "path2".into()]);
9-
assert_eq!(flags, ["-Wl,-rpath,path1", "-Wl,-rpath,path2"]);
10-
}
3+
use super::{RPathConfig, get_rpath_relative_to_output, minimize_rpaths};
114

125
#[test]
136
fn test_minimize1() {
@@ -69,15 +62,3 @@ fn test_rpath_relative_issue_119571() {
6962
// Should not panic when lib only contains filename.
7063
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
7164
}
72-
73-
#[test]
74-
fn test_xlinker() {
75-
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);
76-
77-
assert_eq!(args, vec![
78-
OsString::from("-Wl,-rpath,a/normal/path"),
79-
OsString::from("-Wl,-rpath"),
80-
OsString::from("-Xlinker"),
81-
OsString::from("a,comma,path")
82-
]);
83-
}

0 commit comments

Comments
 (0)