[WebAssembly] MC: Don't allow zero sized data segments
This ensures that each segment has a unique address.
Without this, consecutive zero sized symbols would
end up with the same address and the linker cannot
map symbols to unique data segments.
Differential Revision: https://reviews.llvm.org/D39107
llvm-svn: 316717
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 44f2ba6..2297084 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -510,6 +510,7 @@
DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
+ size_t LastFragmentSize = 0;
for (const MCFragment &Frag : DataSection) {
if (Frag.hasInstructions())
report_fatal_error("only data supported in data sections");
@@ -531,9 +532,16 @@
const SmallVectorImpl<char> &Contents = DataFrag.getContents();
DataBytes.insert(DataBytes.end(), Contents.begin(), Contents.end());
+ LastFragmentSize = Contents.size();
}
}
+ // Don't allow empty segments, or segments that end with zero-sized
+ // fragment, otherwise the linker cannot map symbols to a unique
+ // data segment. This can be triggered by zero-sized structs
+ // See: test/MC/WebAssembly/bss.ll
+ if (LastFragmentSize == 0)
+ DataBytes.resize(DataBytes.size() + 1);
DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
}