@@ -3008,9 +3008,28 @@ fn add_lld_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor) {
30083008 // - if the self-contained linker is enabled on the CLI or by the target spec (or by the
30093009 // unstable CLI flag that will be removed eventually),
30103010 // - and if the self-contained linker is not disabled on the CLI.
3011- let self_contained_linker = sess. target . options . link_self_contained . is_linker_enabled ( )
3012- || sess. opts . cg . link_self_contained . is_linker_enabled ( )
3013- || unstable_use_lld;
3011+ let self_contained_cli =
3012+ sess. opts . cg . link_self_contained . is_linker_enabled ( ) || unstable_use_lld;
3013+ let self_contained_target = sess. target . options . link_self_contained . is_linker_enabled ( ) ;
3014+
3015+ // FIXME: in the future, codegen backends may need to have more control over this process: they
3016+ // don't always support all the features the linker expects here, and vice versa. For example,
3017+ // at the time of writing this, lld expects a newer style of aarch64 TLS relocations that
3018+ // cranelift doesn't implement yet. That in turn can impact whether linking would succeed on
3019+ // such a target when using the `cg_clif` backend and lld.
3020+ //
3021+ // Until interactions between backends and linker features are expressible, we limit target
3022+ // specs to opt-in to lld only when we're on the llvm backend, where it's expected to work and
3023+ // tested on CI. As usual, the CLI still has precedence over this, so that users and developers
3024+ // can still override this default when needed (e.g. for tests).
3025+ let uses_llvm_backend =
3026+ matches ! ( sess. opts. unstable_opts. codegen_backend. as_deref( ) , None | Some ( "llvm" ) ) ;
3027+ if !uses_llvm_backend && !self_contained_cli && sess. opts . cg . linker_flavor . is_none ( ) {
3028+ // We bail if we're not using llvm and lld was not explicitly requested on the CLI.
3029+ return ;
3030+ }
3031+
3032+ let self_contained_linker = self_contained_cli || self_contained_target;
30143033 if self_contained_linker && !sess. opts . cg . link_self_contained . is_linker_disabled ( ) {
30153034 for path in sess. get_tools_search_paths ( false ) {
30163035 cmd. arg ( {
0 commit comments