[WebAssembly] Emit the DataCount section when bulk memory is enabled

Summary:
The DataCount section is necessary for the bulk memory operations
memory.init and data.drop to validate, but it is not recognized by
engines that do not support bulk memory, so emit the section only if
bulk-memory is enabled.

Reviewers: aheejin, dschuff, sbc100

Subscribers: jgravelle-google, sunfish, llvm-commits

Tags: #llvm

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

llvm-svn: 358798
diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 7309bab..6a835ce 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -93,6 +93,7 @@
   void createImportSection();
   void createMemorySection();
   void createElemSection();
+  void createDataCountSection();
   void createCodeSection();
   void createDataSection();
   void createCustomSections();
@@ -414,6 +415,16 @@
   }
 }
 
+void Writer::createDataCountSection() {
+  if (!Segments.size() || !TargetFeatures.count("bulk-memory"))
+    return;
+
+  log("createDataCountSection");
+  SyntheticSection *Section = createSyntheticSection(WASM_SEC_DATACOUNT);
+  raw_ostream &OS = Section->getStream();
+  writeUleb128(OS, Segments.size(), "data count");
+}
+
 void Writer::createCodeSection() {
   if (InputFunctions.empty())
     return;
@@ -865,6 +876,7 @@
   createEventSection();
   createExportSection();
   createElemSection();
+  createDataCountSection();
   createCodeSection();
   createDataSection();
   createCustomSections();