Rollup merge of #147127 - antoyo:fix/gcc-linker-plugin, r=bjorn3

Add a leading dash to linker plugin arguments in the gcc codegen

Fix rust-lang/rust#130583

r? ``@bjorn3``
This commit is contained in:
Stuart Cook
2025-09-29 11:56:44 +10:00
committed by GitHub
7 changed files with 51 additions and 4 deletions

View File

@@ -165,6 +165,10 @@ impl CodegenBackend for CraneliftCodegenBackend {
"" ""
} }
fn name(&self) -> &'static str {
"cranelift"
}
fn init(&self, sess: &Session) { fn init(&self, sess: &Session) {
use rustc_session::config::{InstrumentCoverage, Lto}; use rustc_session::config::{InstrumentCoverage, Lto};
match sess.lto() { match sess.lto() {

View File

@@ -184,6 +184,10 @@ impl CodegenBackend for GccCodegenBackend {
crate::DEFAULT_LOCALE_RESOURCE crate::DEFAULT_LOCALE_RESOURCE
} }
fn name(&self) -> &'static str {
"gcc"
}
fn init(&self, _sess: &Session) { fn init(&self, _sess: &Session) {
#[cfg(feature = "master")] #[cfg(feature = "master")]
{ {

View File

@@ -232,6 +232,10 @@ impl CodegenBackend for LlvmCodegenBackend {
crate::DEFAULT_LOCALE_RESOURCE crate::DEFAULT_LOCALE_RESOURCE
} }
fn name(&self) -> &'static str {
"llvm"
}
fn init(&self, sess: &Session) { fn init(&self, sess: &Session) {
llvm_util::init(sess); // Make sure llvm is inited llvm_util::init(sess); // Make sure llvm is inited
} }
@@ -350,7 +354,14 @@ impl CodegenBackend for LlvmCodegenBackend {
// Run the linker on any artifacts that resulted from the LLVM run. // Run the linker on any artifacts that resulted from the LLVM run.
// This should produce either a finished executable or library. // This should produce either a finished executable or library.
link_binary(sess, &LlvmArchiveBuilderBuilder, codegen_results, metadata, outputs); link_binary(
sess,
&LlvmArchiveBuilderBuilder,
codegen_results,
metadata,
outputs,
self.name(),
);
} }
} }

View File

@@ -79,6 +79,7 @@ pub fn link_binary(
codegen_results: CodegenResults, codegen_results: CodegenResults,
metadata: EncodedMetadata, metadata: EncodedMetadata,
outputs: &OutputFilenames, outputs: &OutputFilenames,
codegen_backend: &'static str,
) { ) {
let _timer = sess.timer("link_binary"); let _timer = sess.timer("link_binary");
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata); let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
@@ -154,6 +155,7 @@ pub fn link_binary(
&codegen_results, &codegen_results,
&metadata, &metadata,
path.as_ref(), path.as_ref(),
codegen_backend,
); );
} }
} }
@@ -680,6 +682,7 @@ fn link_natively(
codegen_results: &CodegenResults, codegen_results: &CodegenResults,
metadata: &EncodedMetadata, metadata: &EncodedMetadata,
tmpdir: &Path, tmpdir: &Path,
codegen_backend: &'static str,
) { ) {
info!("preparing {:?} to {:?}", crate_type, out_filename); info!("preparing {:?} to {:?}", crate_type, out_filename);
let (linker_path, flavor) = linker_and_flavor(sess); let (linker_path, flavor) = linker_and_flavor(sess);
@@ -705,6 +708,7 @@ fn link_natively(
codegen_results, codegen_results,
metadata, metadata,
self_contained_components, self_contained_components,
codegen_backend,
); );
linker::disable_localization(&mut cmd); linker::disable_localization(&mut cmd);
@@ -2208,6 +2212,7 @@ fn linker_with_args(
codegen_results: &CodegenResults, codegen_results: &CodegenResults,
metadata: &EncodedMetadata, metadata: &EncodedMetadata,
self_contained_components: LinkSelfContainedComponents, self_contained_components: LinkSelfContainedComponents,
codegen_backend: &'static str,
) -> Command { ) -> Command {
let self_contained_crt_objects = self_contained_components.is_crt_objects_enabled(); let self_contained_crt_objects = self_contained_components.is_crt_objects_enabled();
let cmd = &mut *super::linker::get_linker( let cmd = &mut *super::linker::get_linker(
@@ -2216,6 +2221,7 @@ fn linker_with_args(
flavor, flavor,
self_contained_components.are_any_components_enabled(), self_contained_components.are_any_components_enabled(),
&codegen_results.crate_info.target_cpu, &codegen_results.crate_info.target_cpu,
codegen_backend,
); );
let link_output_kind = link_output_kind(sess, crate_type); let link_output_kind = link_output_kind(sess, crate_type);

View File

@@ -52,6 +52,7 @@ pub(crate) fn get_linker<'a>(
flavor: LinkerFlavor, flavor: LinkerFlavor,
self_contained: bool, self_contained: bool,
target_cpu: &'a str, target_cpu: &'a str,
codegen_backend: &'static str,
) -> Box<dyn Linker + 'a> { ) -> Box<dyn Linker + 'a> {
let msvc_tool = find_msvc_tools::find_tool(&sess.target.arch, "link.exe"); let msvc_tool = find_msvc_tools::find_tool(&sess.target.arch, "link.exe");
@@ -154,6 +155,7 @@ pub(crate) fn get_linker<'a>(
is_ld: cc == Cc::No, is_ld: cc == Cc::No,
is_gnu: flavor.is_gnu(), is_gnu: flavor.is_gnu(),
uses_lld: flavor.uses_lld(), uses_lld: flavor.uses_lld(),
codegen_backend,
}) as Box<dyn Linker>, }) as Box<dyn Linker>,
LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>, LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>,
LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>, LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
@@ -367,6 +369,7 @@ struct GccLinker<'a> {
is_ld: bool, is_ld: bool,
is_gnu: bool, is_gnu: bool,
uses_lld: bool, uses_lld: bool,
codegen_backend: &'static str,
} }
impl<'a> GccLinker<'a> { impl<'a> GccLinker<'a> {
@@ -423,9 +426,15 @@ impl<'a> GccLinker<'a> {
if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use { if let Some(path) = &self.sess.opts.unstable_opts.profile_sample_use {
self.link_arg(&format!("-plugin-opt=sample-profile={}", path.display())); self.link_arg(&format!("-plugin-opt=sample-profile={}", path.display()));
}; };
let prefix = if self.codegen_backend == "gcc" {
// The GCC linker plugin requires a leading dash.
"-"
} else {
""
};
self.link_args(&[ self.link_args(&[
&format!("-plugin-opt={opt_level}"), &format!("-plugin-opt={prefix}{opt_level}"),
&format!("-plugin-opt=mcpu={}", self.target_cpu), &format!("-plugin-opt={prefix}mcpu={}", self.target_cpu),
]); ]);
} }

View File

@@ -41,6 +41,8 @@ pub trait CodegenBackend {
/// Called before `init` so that all other functions are able to emit translatable diagnostics. /// Called before `init` so that all other functions are able to emit translatable diagnostics.
fn locale_resource(&self) -> &'static str; fn locale_resource(&self) -> &'static str;
fn name(&self) -> &'static str;
fn init(&self, _sess: &Session) {} fn init(&self, _sess: &Session) {}
fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {} fn print(&self, _req: &PrintRequest, _out: &mut String, _sess: &Session) {}
@@ -96,7 +98,14 @@ pub trait CodegenBackend {
metadata: EncodedMetadata, metadata: EncodedMetadata,
outputs: &OutputFilenames, outputs: &OutputFilenames,
) { ) {
link_binary(sess, &ArArchiveBuilderBuilder, codegen_results, metadata, outputs); link_binary(
sess,
&ArArchiveBuilderBuilder,
codegen_results,
metadata,
outputs,
self.name(),
);
} }
} }

View File

@@ -33,6 +33,10 @@ impl CodegenBackend for TheBackend {
"" ""
} }
fn name(&self) -> &'static str {
"the-backend"
}
fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> { fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box<dyn Any> {
Box::new(CodegenResults { Box::new(CodegenResults {
modules: vec![], modules: vec![],