[WebAssembly] Create synthetic __wasm_call_ctors function

This change create a new synthetic function in the final
output binary which calls the static constructors in sequence.

See: https://github.com/WebAssembly/tool-conventions/issues/25

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

llvm-svn: 322388
diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp
index 286afcd..ff53eff 100644
--- a/lld/wasm/InputChunks.cpp
+++ b/lld/wasm/InputChunks.cpp
@@ -30,6 +30,8 @@
 }
 
 void InputChunk::copyRelocations(const WasmSection &Section) {
+  if (Section.Relocations.empty())
+    return;
   size_t Start = getInputSectionOffset();
   size_t Size = getSize();
   for (const WasmRelocation &R : Section.Relocations)
@@ -92,8 +94,10 @@
 // output section.  Calculates the updated index and offset for each relocation
 // as well as the value to write out in the final binary.
 void InputChunk::calcRelocations() {
+  if (Relocations.empty())
+    return;
   int32_t Off = getOutputOffset() - getInputSectionOffset();
-  DEBUG(dbgs() << "calcRelocations: " << File.getName()
+  DEBUG(dbgs() << "calcRelocations: " << File->getName()
                << " offset=" << Twine(Off) << "\n");
   for (const WasmRelocation &Reloc : Relocations) {
     OutputRelocation NewReloc;
@@ -105,19 +109,25 @@
                  << " newOffset=" << NewReloc.Reloc.Offset << "\n");
 
     if (Config->EmitRelocs)
-      NewReloc.NewIndex = File.calcNewIndex(Reloc);
+      NewReloc.NewIndex = File->calcNewIndex(Reloc);
 
     switch (Reloc.Type) {
     case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
     case R_WEBASSEMBLY_MEMORY_ADDR_I32:
     case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
-      NewReloc.Value = File.getRelocatedAddress(Reloc.Index) + Reloc.Addend;
+      NewReloc.Value = File->getRelocatedAddress(Reloc.Index) + Reloc.Addend;
       break;
     default:
-      NewReloc.Value = File.calcNewIndex(Reloc);
+      NewReloc.Value = File->calcNewIndex(Reloc);
       break;
     }
 
     OutRelocations.emplace_back(NewReloc);
   }
 }
+
+void InputFunction::setOutputIndex(uint32_t Index) {
+  DEBUG(dbgs() << "InputFunction::setOutputIndex: " << Index << "\n");
+  assert(!hasOutputIndex());
+  OutputIndex = Index;
+};