[WebAssembly] Define __heap_base global

This is an immutable exported global representing
the start of the heap area.  It is a page aligned.

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

llvm-svn: 322609
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index ea7ce4b..a1e8ddc 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -572,6 +572,11 @@
     MemoryPtr += Config->ZStackSize;
     Config->StackPointerSymbol->setVirtualAddress(MemoryPtr);
     debugPrint("mem: stack top   = %d\n", MemoryPtr);
+    // Set `__heap_base` to directly follow the end of the stack.  We don't
+    // allocate any heap memory up front, but instead really on the malloc/brk
+    // implementation growing the memory at runtime.
+    Config->HeapBaseSymbol->setVirtualAddress(MemoryPtr);
+    debugPrint("mem: heap base   = %d\n", MemoryPtr);
   }
 
   uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize);
@@ -669,6 +674,11 @@
     Config->StackPointerSymbol->setOutputIndex(GlobalIndex++);
   }
 
+  if (Config->HeapBaseSymbol) {
+    DefinedGlobals.emplace_back(Config->HeapBaseSymbol);
+    Config->HeapBaseSymbol->setOutputIndex(GlobalIndex++);
+  }
+
   if (Config->EmitRelocs)
     DefinedGlobals.reserve(Symtab->getSymbols().size());