Skip to content

Commit 679a1d1

Browse files
authored
Rollup merge of #85920 - luqmana:wasm-linker-tweaks, r=petrochenkov
Tweak wasm_base target spec to indicate linker is not GNU and update linker inferring logic for wasm-ld. Reported via [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/wasi.20linker.20unknown.20argument.3A.20--as-needed): we try passing `--as-needed` to the linker if it's GNU ld which `wasm-ld` is not. Usually this isn't an issue for wasm as we would use the WasmLd linker driver but because the linker in question (`wasm32-unknown-wasi-wasm-ld`) ended with `-ld` our linker inferring [logic](https://github.com/rust-lang/rust/blob/f64503eb555475d65ae5503ef22439ca5dd394fd/compiler/rustc_codegen_ssa/src/back/link.rs#L957-L1040) used the `GccLinker` implementations. (UPD: The linker inferring logic actually didn't apply in this case because the linker is actually invoked through gcc in the reported issue. But it's still worth updating the logic I think.) This change then has 2 parts: 1. Update wasm_base target spec to indicate `linker_is_gnu: false` plus a few additions of `target.is_like_wasm` to handle flags `wasm-ld` does in fact support. 2. Improve the linker detection logic to properly determine the correct flavor of wasm linker we're using when we can. We need to add the new `target.is_like_wasm` branches to handle the case where the "linker" used could be something like clang which would then under the hood call wasm-ld.
2 parents d69cd46 + f667aca commit 679a1d1

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,8 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
11991199
|| stem.ends_with("-clang")
12001200
{
12011201
LinkerFlavor::Gcc
1202+
} else if stem == "wasm-ld" || stem.ends_with("-wasm-ld") {
1203+
LinkerFlavor::Lld(LldFlavor::Wasm)
12021204
} else if stem == "ld" || stem == "ld.lld" || stem.ends_with("-ld") {
12031205
LinkerFlavor::Ld
12041206
} else if stem == "link" || stem == "lld-link" {

compiler/rustc_codegen_ssa/src/back/linker.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -476,21 +476,23 @@ impl<'a> Linker for GccLinker<'a> {
476476
// eliminate the metadata. If we're building an executable, however,
477477
// --gc-sections drops the size of hello world from 1.8MB to 597K, a 67%
478478
// reduction.
479-
} else if self.sess.target.linker_is_gnu && !keep_metadata {
479+
} else if (self.sess.target.linker_is_gnu || self.sess.target.is_like_wasm)
480+
&& !keep_metadata
481+
{
480482
self.linker_arg("--gc-sections");
481483
}
482484
}
483485

484486
fn no_gc_sections(&mut self) {
485487
if self.sess.target.is_like_osx {
486488
self.linker_arg("-no_dead_strip");
487-
} else if self.sess.target.linker_is_gnu {
489+
} else if self.sess.target.linker_is_gnu || self.sess.target.is_like_wasm {
488490
self.linker_arg("--no-gc-sections");
489491
}
490492
}
491493

492494
fn optimize(&mut self) {
493-
if !self.sess.target.linker_is_gnu {
495+
if !self.sess.target.linker_is_gnu && !self.sess.target.is_like_wasm {
494496
return;
495497
}
496498

compiler/rustc_target/src/spec/wasm_base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ pub fn options() -> TargetOptions {
102102
// we use the LLD shipped with the Rust toolchain by default
103103
linker: Some("rust-lld".to_owned()),
104104
lld_flavor: LldFlavor::Wasm,
105+
linker_is_gnu: false,
105106

106107
pre_link_args,
107108

0 commit comments

Comments
 (0)