Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,9 @@ impl<'a> Linker for EmLinker<'a> {
}
LinkOutputKind::DynamicDylib | LinkOutputKind::StaticDylib => {
self.cmd.arg("-sSIDE_MODULE=2");
// Shared libraries have no `main`, so emcc must not link in the
// standalone-wasm entry shim that expects one. Mirrors `WasmLd`.
self.link_arg("--no-entry");
}
// -fno-pie is the default on Emscripten.
LinkOutputKind::StaticNoPicExe | LinkOutputKind::StaticPicExe => {}
Expand Down
3 changes: 3 additions & 0 deletions tests/run-make/wasm-emscripten-cdylib/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("{}", 42);
}
10 changes: 10 additions & 0 deletions tests/run-make/wasm-emscripten-cdylib/rmake.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
//! Check that cdylib crate type is supported for the wasm32-unknown-emscripten
//! target and produces a valid Emscripten dynamic library.
//!
//! A cdylib has no `main`, so rustc must link it as entryless (`--no-entry`),
//! otherwise emcc pulls in the standalone-wasm entry shim and `wasm-ld` errors
//! with `undefined symbol: main`. Executables must keep their entry, so this
//! also checks that a `bin` crate with `main` still links.

//@ only-wasm32-unknown-emscripten

use run_make_support::{bare_rustc, rfs, wasmparser};

fn main() {
// A cdylib must link without `--no-entry` being supplied by the user.
bare_rustc().input("foo.rs").target("wasm32-unknown-emscripten").crate_type("cdylib").run();

@bjorn3 bjorn3 Jun 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this work before?
Edit: I think -sSIDE_MODULE=2 already implies --no-entry.

View changes since the review


// Verify the output is a valid wasm file with a dylink.0 section
Expand All @@ -22,4 +28,8 @@ fn main() {
}

assert!(has_dylink, "expected dylink.0 section in emscripten cdylib output");

// An executable has a `main`, so `--no-entry` must NOT be applied: it must
// still link with its entry preserved.
bare_rustc().input("main.rs").target("wasm32-unknown-emscripten").crate_type("bin").run();

@bjorn3 bjorn3 Jun 17, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If --no-entry is passed here, the test would still pass. I think --no-entry not being passed would already be covered by all run-pass tests that run on emscripten anyway.

View changes since the review

}
Loading