From 7a77108809da136b52c558ad32803896df3b1242 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 3 May 2024 15:57:31 -0700 Subject: [PATCH 1/3] rustc: Change LLVM target for the wasm32-wasip2 Rust target This commit changes the LLVM target of for the Rust `wasm32-wasip2` target to `wasm32-wasip2` as well. LLVM does a bit of detection on the target string to know when to call `wasm-component-ld` vs `wasm-ld` so otherwise clang is invoking the wrong linker. --- compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs b/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs index 5ff3f07daae84..a4346cd5a8722 100644 --- a/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs +++ b/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs @@ -55,7 +55,7 @@ pub fn target() -> Target { options.entry_name = "__main_void".into(); Target { - llvm_target: "wasm32-unknown-unknown".into(), + llvm_target: "wasm32-wasip2".into(), metadata: crate::spec::TargetMetadata { description: None, tier: None, From 400e75494ab345e95125da2dd65433bacec2775a Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 3 May 2024 19:38:04 -0700 Subject: [PATCH 2/3] rustc: Don't pass `-fuse-ld=lld` on wasm targets This argument isn't necessary for WebAssembly targets since `wasm-ld` is the only linker for the targets. Passing it otherwise interferes with Clang's linker selection on `wasm32-wasip2` so avoid it altogether. --- compiler/rustc_codegen_ssa/src/back/link.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 1f691d14c5320..6939674ce9dd7 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -3127,7 +3127,13 @@ fn add_lld_args( // 2. Implement the "linker flavor" part of this feature by asking `cc` to use some kind of // `lld` as the linker. - cmd.arg("-fuse-ld=lld"); + // + // Note that wasm targets skip this step since the only option there anyway + // is to use LLD but the `wasm32-wasip2` target relies on a wrapper around + // this, `wasm-component-ld`, which is overridden if this option is passed. + if !sess.target.is_like_wasm { + cmd.arg("-fuse-ld=lld"); + } if !flavor.is_gnu() { // Tell clang to use a non-default LLD flavor. From 38b2bd782b251b4d729ba8fd9daadc774f9b4008 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 7 May 2024 13:23:46 -0700 Subject: [PATCH 3/3] rustc: Change wasm32-wasip2 to PIC-by-default This commit changes the new `wasm32-wasip2` target to being PIC by default rather than the previous non-PIC by default. This change is intended to make it easier for the standard library to be used in a shared object in its precompiled form. This comes with a hypothetical modest slowdown but it's expected that this is quite minor in most use cases or otherwise wasm compilers and/or optimizing runtimes can elide the cost. --- compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs b/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs index a4346cd5a8722..1259969ac3655 100644 --- a/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs +++ b/compiler/rustc_target/src/spec/targets/wasm32_wasip2.rs @@ -18,7 +18,7 @@ use crate::spec::crt_objects; use crate::spec::LinkSelfContainedDefault; -use crate::spec::{base, Target}; +use crate::spec::{base, RelocModel, Target}; pub fn target() -> Target { let mut options = base::wasm::options(); @@ -54,6 +54,11 @@ pub fn target() -> Target { // signatures. options.entry_name = "__main_void".into(); + // Default to PIC unlike base wasm. This makes precompiled objects such as + // the standard library more suitable to be used with shared libaries a la + // emscripten's dynamic linking convention. + options.relocation_model = RelocModel::Pic; + Target { llvm_target: "wasm32-wasip2".into(), metadata: crate::spec::TargetMetadata {