Skip to content

Commit 2fa2746

Browse files
committed
Fix linking for wasm with crate metadata included
On wasm the crate metadata ends up in a custom section. It is not possible to refer to create symbols inside a custom section, so attempting to export it during linking will result in a linker error. This keeps the target spec flag to deny compiling rust dylibs for wasm, but may theoretically allow compiling wasm rust dylibs with a custom target spec. It will help with wasm proc-macros. And in the future we can try to flip the only-cdylib option to false on wasm.
1 parent 942ac9c commit 2fa2746

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1817,7 +1817,13 @@ pub(crate) fn exported_symbols(
18171817
exported_symbols_for_non_proc_macro(tcx, crate_type)
18181818
};
18191819

1820-
if crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro {
1820+
// Preserve the metadata symbol to ensure the metadata section doesn't get removed by the
1821+
// linker. On wasm however the metadata is put in a custom section, to which symbols can't
1822+
// refer, so there is no metadata symbol there. Luckily custom sections are always preserved by
1823+
// the linker.
1824+
if (crate_type == CrateType::Dylib || crate_type == CrateType::ProcMacro)
1825+
&& !tcx.sess.target.is_like_wasm
1826+
{
18211827
let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx);
18221828
symbols.push((metadata_symbol_name, SymbolExportKind::Data));
18231829
}

0 commit comments

Comments
 (0)