[WebAssembly] Fix crash when __wasm_call_ctor is GCd in programs containing static init/fini

Subscribers: dschuff, jgravelle-google, aheejin, sunfish, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D58864

llvm-svn: 355263
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 2a9cbe6..879a159 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -1168,6 +1168,9 @@
 // This is then used either when creating the output linking section or to
 // synthesize the "__wasm_call_ctors" function.
 void Writer::calculateInitFunctions() {
+  if (!Config->Relocatable && !WasmSym::CallCtors->isLive())
+    return;
+
   for (ObjFile *File : Symtab->ObjectFiles) {
     const WasmLinkingData &L = File->getWasmObj()->linkingData();
     for (const WasmInitFunc &F : L.InitFunctions) {