From d8572c1a640959f17e66f2bcfecad581fea2e02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Wed, 27 Sep 2023 13:41:34 +0000 Subject: [PATCH] limit `lld` flavors to the llvm backend --- compiler/rustc_codegen_ssa/src/back/link.rs | 25 ++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 05d205d9f2fbd..bb8966dff3676 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -3008,9 +3008,28 @@ fn add_lld_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) { // - if the self-contained linker is enabled on the CLI or by the target spec (or by the // unstable CLI flag that will be removed eventually), // - and if the self-contained linker is not disabled on the CLI. - let self_contained_linker = sess.target.options.link_self_contained.is_linker_enabled() - || sess.opts.cg.link_self_contained.is_linker_enabled() - || unstable_use_lld; + let self_contained_cli = + sess.opts.cg.link_self_contained.is_linker_enabled() || unstable_use_lld; + let self_contained_target = sess.target.options.link_self_contained.is_linker_enabled(); + + // FIXME: in the future, codegen backends may need to have more control over this process: they + // don't always support all the features the linker expects here, and vice versa. For example, + // at the time of writing this, lld expects a newer style of aarch64 TLS relocations that + // cranelift doesn't implement yet. That in turn can impact whether linking would succeed on + // such a target when using the `cg_clif` backend and lld. + // + // Until interactions between backends and linker features are expressible, we limit target + // specs to opt-in to lld only when we're on the llvm backend, where it's expected to work and + // tested on CI. As usual, the CLI still has precedence over this, so that users and developers + // can still override this default when needed (e.g. for tests). + let uses_llvm_backend = + matches!(sess.opts.unstable_opts.codegen_backend.as_deref(), None | Some("llvm")); + if !uses_llvm_backend && !self_contained_cli && sess.opts.cg.linker_flavor.is_none() { + // We bail if we're not using llvm and lld was not explicitly requested on the CLI. + return; + } + + let self_contained_linker = self_contained_cli || self_contained_target; if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() { for path in sess.get_tools_search_paths(false) { cmd.arg({