From 9da4fea9ba840e1e4d15ed096258f6bf63ec9e89 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 21 Jun 2022 14:07:21 -0700 Subject: [PATCH 1/2] Implement set_output_kind for emscripten linker --- compiler/rustc_codegen_ssa/src/back/linker.rs | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index b5b63942e2c6e..965a42ed46722 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -1041,7 +1041,38 @@ impl<'a> Linker for EmLinker<'a> { &mut self.cmd } - fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {} + fn set_output_kind(&mut self, output_kind: LinkOutputKind, _out_filename: &Path) { + match output_kind { + LinkOutputKind::DynamicNoPicExe | LinkOutputKind::DynamicPicExe => { + // "-sMAIN_MODULE=1" breaks + // https://github.com/rust-lang/rust/issues/92738 + self.cmd.arg("-sMAIN_MODULE=2"); + warn!( + "Building dynamic executable with -sMAIN_MODULE=2. \ + Dead code elimination may break things. \ + See https://emscripten.org/docs/compiling/Dynamic-Linking.html?highlight=main_module#code-size \ + " + ); + } + LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => { + // -sSIDE_MODULE=1 breaks + // https://github.com/rust-lang/rust/issues/92738 + // In any case, -sSIDE_MODULE=2 is better because Rust is good at + // calculating exports. + self.cmd.arg("-sSIDE_MODULE=2"); + // Without -sWASM_BIGINT there are issues with dynamic Rust libraries. There + // are no plans to fix this in Emscripten AFAIK. See + // https://github.com/emscripten-core/emscripten/pull/16693 This could also + // be fixed with panic=abort. + self.cmd.arg("-sWASM_BIGINT"); + } + // -fno-pie is the default on Emscripten. + LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {} + LinkOutputKind::WasiReactorExe => { + unreachable!(); + } + } + } fn include_path(&mut self, path: &Path) { self.cmd.arg("-L").arg(path); From b0da01fb0f15141f3e49167b216bae4e04e448a7 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Thu, 14 Jul 2022 16:36:33 +0300 Subject: [PATCH 2/2] Set crt_static_respected to true for the Emscripten target --- compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs b/compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs index 1b94c59b55f09..146635beee475 100644 --- a/compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs +++ b/compiler/rustc_target/src/spec/wasm32_unknown_emscripten.rs @@ -27,6 +27,8 @@ pub fn target() -> Target { exe_suffix: ".js".into(), linker: None, relocation_model: RelocModel::Pic, + crt_static_respected: true, + crt_static_default: true, panic_strategy: PanicStrategy::Unwind, no_default_libraries: false, post_link_args,