summaryrefslogtreecommitdiffstats
path: root/llvm/lib/MC/WasmObjectWriter.cpp
diff options
context:
space:
mode:
authorSam Clegg <sbc@chromium.org>2018-01-09 23:43:14 +0000
committerSam Clegg <sbc@chromium.org>2018-01-09 23:43:14 +0000
commitea7caceedcc8d872bc31c141515ef2e3749ef659 (patch)
tree7499d1e4ee969bda41210ab8d2e5d7c339c07b44 /llvm/lib/MC/WasmObjectWriter.cpp
parent29f5f987f1b76b5c43310b2062c9f447667a4f80 (diff)
downloadbcm5719-llvm-ea7caceedcc8d872bc31c141515ef2e3749ef659.tar.gz
bcm5719-llvm-ea7caceedcc8d872bc31c141515ef2e3749ef659.zip
[WebAssembly] Add COMDAT support
This adds COMDAT support to the Wasm object-file format. Spec: https://github.com/WebAssembly/tool-conventions/pull/31 Corresponding LLD change: https://bugs.llvm.org/show_bug.cgi?id=35533, and D40845 Patch by Nicholas Wilson Differential Revision: https://reviews.llvm.org/D40844 llvm-svn: 322135
Diffstat (limited to 'llvm/lib/MC/WasmObjectWriter.cpp')
-rw-r--r--llvm/lib/MC/WasmObjectWriter.cpp51
1 files changed, 46 insertions, 5 deletions
diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp
index 37b12c63be4..e5099a83b6f 100644
--- a/llvm/lib/MC/WasmObjectWriter.cpp
+++ b/llvm/lib/MC/WasmObjectWriter.cpp
@@ -138,6 +138,14 @@ struct WasmGlobal {
uint32_t ImportIndex;
};
+// Information about a single item which is part of a COMDAT. For each data
+// segment or function which is in the COMDAT, there is a corresponding
+// WasmComdatEntry.
+struct WasmComdatEntry {
+ unsigned Kind;
+ uint32_t Index;
+};
+
// Information about a single relocation.
struct WasmRelocationEntry {
uint64_t Offset; // Where is the relocation.
@@ -284,8 +292,9 @@ private:
void writeDataRelocSection();
void writeLinkingMetaDataSection(
ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
- const SmallVector<std::pair<StringRef, uint32_t>, 4> &SymbolFlags,
- const SmallVector<std::pair<uint16_t, uint32_t>, 2> &InitFuncs);
+ ArrayRef<std::pair<StringRef, uint32_t>> SymbolFlags,
+ ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
+ const std::map<StringRef, std::vector<WasmComdatEntry>>& Comdats);
uint32_t getProvisionalValue(const WasmRelocationEntry &RelEntry);
void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
@@ -913,8 +922,9 @@ void WasmObjectWriter::writeDataRelocSection() {
void WasmObjectWriter::writeLinkingMetaDataSection(
ArrayRef<WasmDataSegment> Segments, uint32_t DataSize,
- const SmallVector<std::pair<StringRef, uint32_t>, 4> &SymbolFlags,
- const SmallVector<std::pair<uint16_t, uint32_t>, 2> &InitFuncs) {
+ ArrayRef<std::pair<StringRef, uint32_t>> SymbolFlags,
+ ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
+ const std::map<StringRef, std::vector<WasmComdatEntry>>& Comdats) {
SectionBookkeeping Section;
startSection(Section, wasm::WASM_SEC_CUSTOM, "linking");
SectionBookkeeping SubSection;
@@ -956,6 +966,21 @@ void WasmObjectWriter::writeLinkingMetaDataSection(
endSection(SubSection);
}
+ if (Comdats.size()) {
+ startSection(SubSection, wasm::WASM_COMDAT_INFO);
+ encodeULEB128(Comdats.size(), getStream());
+ for (const auto &C : Comdats) {
+ writeString(C.first);
+ encodeULEB128(0, getStream()); // flags for future use
+ encodeULEB128(C.second.size(), getStream());
+ for (const WasmComdatEntry &Entry : C.second) {
+ encodeULEB128(Entry.Kind, getStream());
+ encodeULEB128(Entry.Index, getStream());
+ }
+ }
+ endSection(SubSection);
+ }
+
endSection(Section);
}
@@ -997,6 +1022,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
SmallVector<WasmExport, 4> Exports;
SmallVector<std::pair<StringRef, uint32_t>, 4> SymbolFlags;
SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
+ std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
unsigned NumFuncImports = 0;
SmallVector<WasmDataSegment, 4> DataSegments;
uint32_t DataSize = 0;
@@ -1143,6 +1169,12 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
Segment.Flags = 0;
DataSize += Segment.Data.size();
Section.setMemoryOffset(Segment.Offset);
+
+ if (const MCSymbolWasm *C = Section.getGroup()) {
+ Comdats[C->getName()].emplace_back(
+ WasmComdatEntry{wasm::WASM_COMDAT_DATA,
+ static_cast<uint32_t>(DataSegments.size()) - 1});
+ }
}
// Handle regular defined and undefined symbols.
@@ -1216,6 +1248,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
// address. For externals these will also be named exports.
Index = NumGlobalImports + Globals.size();
auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
+ assert(DataSection.isWasmData());
WasmGlobal Global;
Global.Type = PtrType;
@@ -1239,8 +1272,16 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
Export.Kind = wasm::WASM_EXTERNAL_GLOBAL;
DEBUG(dbgs() << " -> export " << Exports.size() << "\n");
Exports.push_back(Export);
+
if (!WS.isExternal())
SymbolFlags.emplace_back(WS.getName(), wasm::WASM_SYMBOL_BINDING_LOCAL);
+
+ if (WS.isFunction()) {
+ auto &Section = static_cast<MCSectionWasm &>(WS.getSection(false));
+ if (const MCSymbolWasm *C = Section.getGroup())
+ Comdats[C->getName()].emplace_back(
+ WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
+ }
}
}
@@ -1372,7 +1413,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
writeCodeRelocSection();
writeDataRelocSection();
writeLinkingMetaDataSection(DataSegments, DataSize, SymbolFlags,
- InitFuncs);
+ InitFuncs, Comdats);
// TODO: Translate the .comment section to the output.
// TODO: Translate debug sections to the output.
OpenPOWER on IntegriCloud