[WebAssembly] Allow each data segment to specify its own alignment

Also, add a flags field as we will almost certainly
be needing that soon too.

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

llvm-svn: 314534
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index d7a9895..af5c1a7 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -104,6 +104,8 @@
   MCSectionWasm *Section;
   StringRef Name;
   uint32_t Offset;
+  uint32_t Alignment;
+  uint32_t Flags;
   SmallVector<char, 4> Data;
 };
 
@@ -282,7 +284,6 @@
   void writeDataRelocSection();
   void writeLinkingMetaDataSection(
       ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
-      uint32_t DataAlignment,
       SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
       bool HasStackPointer, uint32_t StackPointerGlobal);
 
@@ -499,11 +500,11 @@
 }
 
 static void addData(SmallVectorImpl<char> &DataBytes,
-                    MCSectionWasm &DataSection, uint32_t &DataAlignment) {
-  DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
-  DataAlignment = std::max(DataAlignment, DataSection.getAlignment());
+                    MCSectionWasm &DataSection) {
   DEBUG(errs() << "addData: " << DataSection.getSectionName() << "\n");
 
+  DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
+
   for (const MCFragment &Frag : DataSection) {
     if (Frag.hasInstructions())
       report_fatal_error("only data supported in data sections");
@@ -914,7 +915,6 @@
 
 void WasmObjectWriter::writeLinkingMetaDataSection(
     ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
-    uint32_t DataAlignment,
     SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags,
     bool HasStackPointer, uint32_t StackPointerGlobal) {
   SectionBookkeeping Section;
@@ -941,17 +941,16 @@
     startSection(SubSection, wasm::WASM_DATA_SIZE);
     encodeULEB128(DataSize, getStream());
     endSection(SubSection);
-
-    startSection(SubSection, wasm::WASM_DATA_ALIGNMENT);
-    encodeULEB128(DataAlignment, getStream());
-    endSection(SubSection);
   }
 
   if (Segments.size()) {
-    startSection(SubSection, wasm::WASM_SEGMENT_NAMES);
+    startSection(SubSection, wasm::WASM_SEGMENT_INFO);
     encodeULEB128(Segments.size(), getStream());
-    for (const WasmDataSegment &Segment : Segments)
+    for (const WasmDataSegment &Segment : Segments) {
       writeString(Segment.Name);
+      encodeULEB128(Segment.Alignment, getStream());
+      encodeULEB128(Segment.Flags, getStream());
+    }
     endSection(SubSection);
   }
 
@@ -998,7 +997,6 @@
   SmallPtrSet<const MCSymbolWasm *, 4> IsAddressTaken;
   unsigned NumFuncImports = 0;
   SmallVector<WasmDataSegment, 4> DataSegments;
-  uint32_t DataAlignment = 1;
   uint32_t StackPointerGlobal = 0;
   uint32_t DataSize = 0;
   bool HasStackPointer = false;
@@ -1144,7 +1142,9 @@
     Segment.Name = Section.getSectionName();
     Segment.Offset = DataSize;
     Segment.Section = &Section;
-    addData(Segment.Data, Section, DataAlignment);
+    addData(Segment.Data, Section);
+    Segment.Alignment = Section.getAlignment();
+    Segment.Flags = 0;
     DataSize += Segment.Data.size();
     Section.setMemoryOffset(Segment.Offset);
   }
@@ -1308,8 +1308,8 @@
   writeNameSection(Functions, Imports, NumFuncImports);
   writeCodeRelocSection();
   writeDataRelocSection();
-  writeLinkingMetaDataSection(DataSegments, DataSize, DataAlignment,
-                              SymbolFlags, HasStackPointer, StackPointerGlobal);
+  writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
+                              HasStackPointer, StackPointerGlobal);
 
   // TODO: Translate the .comment section to the output.
   // TODO: Translate debug sections to the output.