Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ced69ff

Browse files
committedFeb 26, 2025
Auto merge of rust-lang#137685 - lqd:nostart-stop-gc, r=<try>
self-contained linker: conservatively default to `-znostart-stop-gc` To help stabilization, this PR disables an LLD optimization with respect to `--gc-sections` and encapsulation symbols: it will reduce the number of crates needing to opt-out of lld due to this bfd / lld difference. For example, all the people using [linkme](https://github.com/dtolnay/linkme), which [doesn't work with lld](dtolnay/linkme#63) or on nightly, need to disable lld. This optimization has [no visible impact](rust-lang#112049 (comment)) on our benchmarks, so we can use it by default and have a safer/more conservative starting point to remove friction during migration. We can them emit an FCW for the cases where lld detects reliance on encapsulation symbols without `-znostart-stop-gc`, and then revert back to lld's default after a while. No one compiling on nightly relies on this difference, obviously, so doing an FCW is not necessary until after lld is used on stable. I've tested that this correctly links on `linkme` examples. I've also quickly tried to crate an rmake test but the setup with encapsulation symbols is annoying to reproduce: a few link section/name attributes is not enough, we also need to collect symbols between the encapsulation symbols, without referencing them in code, for `-znostart-stop-gc` to only impact this... It should of course be doable though, maybe `@Kobzol` will look into it if they have time. r? `@petrochenkov`
2 parents ac91805 + 762fdf6 commit ced69ff

File tree

1 file changed

+26
-0
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+26
-0
lines changed
 

‎compiler/rustc_codegen_ssa/src/back/link.rs

+26
Original file line numberDiff line numberDiff line change
@@ -3434,6 +3434,32 @@ fn add_lld_args(
34343434
// this, `wasm-component-ld`, which is overridden if this option is passed.
34353435
if !sess.target.is_like_wasm {
34363436
cmd.cc_arg("-fuse-ld=lld");
3437+
3438+
// GNU ld and LLD have opposite defaults on some section garbage-collection features. For
3439+
// example, the somewhat popular `linkme` crate and its dependents rely in practice on this
3440+
// difference: when using lld, they need `-z nostart-stop-gc` to prevent encapsulation
3441+
// symbols and sections from being garbage-collected.
3442+
//
3443+
// More information about all this can be found in:
3444+
// - https://maskray.me/blog/2021-01-31-metadata-sections-comdat-and-shf-link-order
3445+
// - https://lld.llvm.org/ELF/start-stop-gc
3446+
//
3447+
// So when using lld, we restore, for now, the traditional behavior to help migration, but
3448+
// will remove it in the future.
3449+
// Since this only disables an optimization, it shouldn't create issues, but is in theory
3450+
// slightly suboptimal. However, it:
3451+
// - doesn't have any visible impact on our benchmarks
3452+
// - reduces the need to disable lld for the crates that depend on this
3453+
//
3454+
// Note that lld can detect some cases where this difference is relied on, and emits a
3455+
// dedicated error to add this link arg. We could make use of this error to emit an FCW. As
3456+
// of writing this, we don't do it, because lld is already enabled by default on nightly
3457+
// without this mitigation: no working project would see the FCW, so we do this to help
3458+
// stabilization.
3459+
//
3460+
// FIXME: emit an FCW if linking fails due its absence, and then remove this link-arg in the
3461+
// future.
3462+
cmd.link_arg("-znostart-stop-gc");
34373463
}
34383464

34393465
if !flavor.is_gnu() {

0 commit comments

Comments
 (0)
Failed to load comments.