Skip to content

Commit 9bbc9cb

Browse files
committed
Add a helper method for linker arguments
Linker arguments must transformed when Rust is interacting with the linker through a compiler. This commit introduces a helper function that abstracts away details of this transformation.
1 parent 43192ca commit 9bbc9cb

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+28-11
Original file line numberDiff line numberDiff line change
@@ -219,19 +219,36 @@ pub struct GccLinker<'a> {
219219
}
220220

221221
impl<'a> GccLinker<'a> {
222-
/// Argument that must be passed *directly* to the linker
222+
/// Passes an argument directly to the linker.
223223
///
224-
/// These arguments need to be prepended with `-Wl`, when a GCC-style linker is used.
225-
fn linker_arg<S>(&mut self, arg: S) -> &mut Self
226-
where
227-
S: AsRef<OsStr>,
228-
{
229-
if !self.is_ld {
230-
let mut os = OsString::from("-Wl,");
231-
os.push(arg.as_ref());
232-
self.cmd.arg(os);
224+
/// When the linker is not ld-like such as when using a compiler as a linker, the argument is
225+
/// prepended by `-Wl,`.
226+
fn linker_arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
227+
self.linker_args(&[arg]);
228+
self
229+
}
230+
231+
/// Passes a series of arguments directly to the linker.
232+
///
233+
/// When the linker is ld-like, the arguments are simply appended to the command. When the
234+
/// linker is not ld-like such as when using a compiler as a linker, the arguments are joined by
235+
/// commas to form an argument that is then prepended with `-Wl`. In this situation, only a
236+
/// single argument is appended to the command to ensure that the order of the arguments is
237+
/// preserved by the compiler.
238+
fn linker_args(&mut self, args: &[impl AsRef<OsStr>]) -> &mut Self {
239+
if self.is_ld {
240+
args.into_iter().for_each(|a| {
241+
self.cmd.arg(a);
242+
});
233243
} else {
234-
self.cmd.arg(arg);
244+
if !args.is_empty() {
245+
let mut s = OsString::from("-Wl");
246+
for a in args {
247+
s.push(",");
248+
s.push(a);
249+
}
250+
self.cmd.arg(s);
251+
}
235252
}
236253
self
237254
}

0 commit comments

Comments
 (0)