Skip to content

Commit 32ddd02

Browse files
committed
Refactor linker argument generation
This commit refactors linker argument generation to leverage a helper function that abstracts away details governing how these arguments are transformed and provided to the linker. This fixes the misuse of the `-exported_symbols_list` when an ld-like linker is used rather than a compiler. A compiler would expect `-Wl,-exported_symbols_list,path` but ld would expect `-exported_symbols_list` and `path` as two seperate arguments. Prior to this change, an ld-like linker was given `-exported_symbols_list,path`.
1 parent 9bbc9cb commit 32ddd02

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+18-30
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,10 @@ impl<'a> GccLinker<'a> {
306306
if let Some(path) = &self.sess.opts.debugging_opts.profile_sample_use {
307307
self.linker_arg(&format!("-plugin-opt=sample-profile={}", path.display()));
308308
};
309-
self.linker_arg(&format!("-plugin-opt={}", opt_level));
310-
self.linker_arg(&format!("-plugin-opt=mcpu={}", self.target_cpu));
309+
self.linker_args(&[
310+
&format!("-plugin-opt={}", opt_level),
311+
&format!("-plugin-opt=mcpu={}", self.target_cpu),
312+
]);
311313
}
312314

313315
fn build_dylib(&mut self, out_filename: &Path) {
@@ -321,10 +323,9 @@ impl<'a> GccLinker<'a> {
321323
// principled solution at some point to force the compiler to pass
322324
// the right `-Wl,-install_name` with an `@rpath` in it.
323325
if self.sess.opts.cg.rpath || self.sess.opts.debugging_opts.osx_rpath_install_name {
324-
self.linker_arg("-install_name");
325-
let mut v = OsString::from("@rpath/");
326-
v.push(out_filename.file_name().unwrap());
327-
self.linker_arg(&v);
326+
let mut rpath = OsString::from("@rpath/");
327+
rpath.push(out_filename.file_name().unwrap());
328+
self.linker_args(&[OsString::from("-install_name"), rpath]);
328329
}
329330
} else {
330331
self.cmd.arg("-shared");
@@ -398,8 +399,7 @@ impl<'a> Linker for GccLinker<'a> {
398399
self.build_dylib(out_filename);
399400
}
400401
LinkOutputKind::WasiReactorExe => {
401-
self.linker_arg("--entry");
402-
self.linker_arg("_initialize");
402+
self.linker_args(&["--entry", "_initialize"]);
403403
}
404404
}
405405
// VxWorks compiler driver introduced `--static-crt` flag specifically for rustc,
@@ -471,8 +471,7 @@ impl<'a> Linker for GccLinker<'a> {
471471
self.cmd.arg(path);
472472
}
473473
fn full_relro(&mut self) {
474-
self.linker_arg("-zrelro");
475-
self.linker_arg("-znow");
474+
self.linker_args(&["-zrelro", "-znow"]);
476475
}
477476
fn partial_relro(&mut self) {
478477
self.linker_arg("-zrelro");
@@ -656,7 +655,6 @@ impl<'a> Linker for GccLinker<'a> {
656655
}
657656

658657
let is_windows = self.sess.target.is_like_windows;
659-
let mut arg = OsString::new();
660658
let path = tmpdir.join(if is_windows { "list.def" } else { "list" });
661659

662660
debug!("EXPORTED SYMBOLS:");
@@ -708,27 +706,18 @@ impl<'a> Linker for GccLinker<'a> {
708706
}
709707

710708
if self.sess.target.is_like_osx {
711-
if !self.is_ld {
712-
arg.push("-Wl,")
713-
}
714-
arg.push("-exported_symbols_list,");
709+
self.linker_args(&[OsString::from("-exported_symbols_list"), path.into()]);
715710
} else if self.sess.target.is_like_solaris {
716-
if !self.is_ld {
717-
arg.push("-Wl,")
718-
}
719-
arg.push("-M,");
711+
self.linker_args(&[OsString::from("-M"), path.into()]);
720712
} else {
721-
if !self.is_ld {
722-
arg.push("-Wl,")
723-
}
724-
// Both LD and LLD accept export list in *.def file form, there are no flags required
725-
if !is_windows {
726-
arg.push("--version-script=")
713+
if is_windows {
714+
self.linker_arg(path);
715+
} else {
716+
let mut arg = OsString::from("--version-script=");
717+
arg.push(path);
718+
self.linker_arg(arg);
727719
}
728720
}
729-
730-
arg.push(&path);
731-
self.cmd.arg(arg);
732721
}
733722

734723
fn subsystem(&mut self, subsystem: &str) {
@@ -786,8 +775,7 @@ impl<'a> Linker for GccLinker<'a> {
786775
self.linker_arg("--as-needed");
787776
} else if self.sess.target.is_like_solaris {
788777
// -z ignore is the Solaris equivalent to the GNU ld --as-needed option
789-
self.linker_arg("-z");
790-
self.linker_arg("ignore");
778+
self.linker_args(&["-z", "ignore"]);
791779
}
792780
}
793781
}

0 commit comments

Comments
 (0)