Skip to content

Commit 5aa7fb7

Browse files
committed
Add a leading dash to linker plugin arguments in the gcc codegen
1 parent a00a515 commit 5aa7fb7

File tree

7 files changed

+51
-4
lines changed

7 files changed

+51
-4
lines changed

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
165165
""
166166
}
167167

168+
fn name(&self) -> &'static str {
169+
"cranelift"
170+
}
171+
168172
fn init(&self, sess: &Session) {
169173
use rustc_session::config::{InstrumentCoverage, Lto};
170174
match sess.lto() {

compiler/rustc_codegen_gcc/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ impl CodegenBackend for GccCodegenBackend {
184184
crate::DEFAULT_LOCALE_RESOURCE
185185
}
186186

187+
fn name(&self) -> &'static str {
188+
"gcc"
189+
}
190+
187191
fn init(&self, _sess: &Session) {
188192
#[cfg(feature = "master")]
189193
{

compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ impl CodegenBackend for LlvmCodegenBackend {
231231
crate::DEFAULT_LOCALE_RESOURCE
232232
}
233233

234+
fn name(&self) -> &'static str {
235+
"llvm"
236+
}
237+
234238
fn init(&self, sess: &Session) {
235239
llvm_util::init(sess); // Make sure llvm is inited
236240
}
@@ -349,7 +353,14 @@ impl CodegenBackend for LlvmCodegenBackend {
349353

350354
// Run the linker on any artifacts that resulted from the LLVM run.
351355
// This should produce either a finished executable or library.
352-
link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, metadata, outputs);
356+
link_binary(
357+
sess,
358+
&LlvmArchiveBuilderBuilder,
359+
codegen_results,
360+
metadata,
361+
outputs,
362+
self.name(),
363+
);
353364
}
354365
}
355366

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ pub fn link_binary(
7979
codegen_results: CodegenResults,
8080
metadata: EncodedMetadata,
8181
outputs: &OutputFilenames,
82+
codegen_backend: &'static str,
8283
) {
8384
let _timer = sess.timer("link_binary");
8485
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
@@ -154,6 +155,7 @@ pub fn link_binary(
154155
&codegen_results,
155156
&metadata,
156157
path.as_ref(),
158+
codegen_backend,
157159
);
158160
}
159161
}
@@ -680,6 +682,7 @@ fn link_natively(
680682
codegen_results: &CodegenResults,
681683
metadata: &EncodedMetadata,
682684
tmpdir: &Path,
685+
codegen_backend: &'static str,
683686
) {
684687
info!("preparing {:?} to {:?}", crate_type, out_filename);
685688
let (linker_path, flavor) = linker_and_flavor(sess);
@@ -705,6 +708,7 @@ fn link_natively(
705708
codegen_results,
706709
metadata,
707710
self_contained_components,
711+
codegen_backend,
708712
);
709713

710714
linker::disable_localization(&mut cmd);
@@ -2208,6 +2212,7 @@ fn linker_with_args(
22082212
codegen_results: &CodegenResults,
22092213
metadata: &EncodedMetadata,
22102214
self_contained_components: LinkSelfContainedComponents,
2215+
codegen_backend: &'static str,
22112216
) -> Command {
22122217
let self_contained_crt_objects = self_contained_components.is_crt_objects_enabled();
22132218
let cmd = &mut *super::linker::get_linker(
@@ -2216,6 +2221,7 @@ fn linker_with_args(
22162221
flavor,
22172222
self_contained_components.are_any_components_enabled(),
22182223
&codegen_results.crate_info.target_cpu,
2224+
codegen_backend,
22192225
);
22202226
let link_output_kind = link_output_kind(sess, crate_type);
22212227

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub(crate) fn get_linker<'a>(
5252
flavor: LinkerFlavor,
5353
self_contained: bool,
5454
target_cpu: &'a str,
55+
codegen_backend: &'static str,
5556
) -> Box<dyn Linker + 'a> {
5657
let msvc_tool = find_msvc_tools::find_tool(&sess.target.arch, "link.exe");
5758

@@ -154,6 +155,7 @@ pub(crate) fn get_linker<'a>(
154155
is_ld: cc == Cc::No,
155156
is_gnu: flavor.is_gnu(),
156157
uses_lld: flavor.uses_lld(),
158+
codegen_backend,
157159
}) as Box<dyn Linker>,
158160
LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>,
159161
LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
@@ -367,6 +369,7 @@ struct GccLinker<'a> {
367369
is_ld: bool,
368370
is_gnu: bool,
369371
uses_lld: bool,
372+
codegen_backend: &'static str,
370373
}
371374

372375
impl<'a> GccLinker<'a> {
@@ -423,9 +426,15 @@ impl<'a> GccLinker<'a> {
423426
if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
424427
self.link_arg(&format!("-plugin-opt=sample-profile={}", path.display()));
425428
};
429+
let prefix = if self.codegen_backend == "gcc" {
430+
// The GCC linker plugin requires a leading dash.
431+
"-"
432+
} else {
433+
""
434+
};
426435
self.link_args(&[
427-
&format!("-plugin-opt={opt_level}"),
428-
&format!("-plugin-opt=mcpu={}", self.target_cpu),
436+
&format!("-plugin-opt={}{opt_level}", prefix),
437+
&format!("-plugin-opt={}mcpu={}", prefix, self.target_cpu),
429438
]);
430439
}
431440

compiler/rustc_codegen_ssa/src/traits/backend.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ pub trait CodegenBackend {
4343

4444
fn init(&self, _sess: &Session) {}
4545

46+
fn name(&self) -> &'static str;
47+
4648
fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
4749

4850
/// Collect target-specific options that should be set in `cfg(...)`, including
@@ -96,7 +98,14 @@ pub trait CodegenBackend {
9698
metadata: EncodedMetadata,
9799
outputs: &OutputFilenames,
98100
) {
99-
link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, metadata, outputs);
101+
link_binary(
102+
sess,
103+
&ArArchiveBuilderBuilder,
104+
codegen_results,
105+
metadata,
106+
outputs,
107+
self.name(),
108+
);
100109
}
101110
}
102111

tests/ui-fulldeps/codegen-backend/auxiliary/the_backend.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ impl CodegenBackend for TheBackend {
3333
""
3434
}
3535

36+
fn name(&self) -> &'static str {
37+
"the-backend"
38+
}
39+
3640
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
3741
Box::new(CodegenResults {
3842
modules: vec![],

0 commit comments

Comments
 (0)