summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Object/WasmObjectFile.cpp
diff options
context:
space:
mode:
authorHeejin Ahn <aheejin@gmail.com>2018-11-14 02:46:21 +0000
committerHeejin Ahn <aheejin@gmail.com>2018-11-14 02:46:21 +0000
commitda419bdb5e3e167ea90c6923660059f35fa17d67 (patch)
tree0264ae56ecdddf59399c2b355e1dee8eeffafb63 /llvm/lib/Object/WasmObjectFile.cpp
parent6a3c279d1cdcd4205a233952b4bacd5941cd355e (diff)
downloadbcm5719-llvm-da419bdb5e3e167ea90c6923660059f35fa17d67.tar.gz
bcm5719-llvm-da419bdb5e3e167ea90c6923660059f35fa17d67.zip
[WebAssembly] Add support for the event section
Summary: This adds support for the 'event section' specified in the exception handling proposal. (This was named 'exception section' first, but later renamed to 'event section' to take possibilities of other kinds of events into consideration. But currently we only store exception info in this section.) The event section is added between the global section and the export section. This is for ease of validation per request of the V8 team. This patch: - Creates the event symbol type, which is a weak symbol - Makes 'throw' instruction take the event symbol '__cpp_exception' - Adds relocation support for events - Adds WasmObjectWriter / WasmObjectFile (Reader) support - Adds obj2yaml / yaml2obj support - Adds '.eventtype' printing support Reviewers: dschuff, sbc100, aardappel Subscribers: jgravelle-google, sunfish, llvm-commits Differential Revision: https://reviews.llvm.org/D54096 llvm-svn: 346825
Diffstat (limited to 'llvm/lib/Object/WasmObjectFile.cpp')
-rw-r--r--llvm/lib/Object/WasmObjectFile.cpp95
1 files changed, 92 insertions, 3 deletions
diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp
index 3bd66f9375f..794f869d475 100644
--- a/llvm/lib/Object/WasmObjectFile.cpp
+++ b/llvm/lib/Object/WasmObjectFile.cpp
@@ -295,6 +295,8 @@ Error WasmObjectFile::parseSection(WasmSection &Sec) {
return parseMemorySection(Ctx);
case wasm::WASM_SEC_GLOBAL:
return parseGlobalSection(Ctx);
+ case wasm::WASM_SEC_EVENT:
+ return parseEventSection(Ctx);
case wasm::WASM_SEC_EXPORT:
return parseExportSection(Ctx);
case wasm::WASM_SEC_START:
@@ -439,19 +441,24 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
std::vector<wasm::WasmImport *> ImportedGlobals;
std::vector<wasm::WasmImport *> ImportedFunctions;
+ std::vector<wasm::WasmImport *> ImportedEvents;
ImportedGlobals.reserve(Imports.size());
ImportedFunctions.reserve(Imports.size());
+ ImportedEvents.reserve(Imports.size());
for (auto &I : Imports) {
if (I.Kind == wasm::WASM_EXTERNAL_FUNCTION)
ImportedFunctions.emplace_back(&I);
else if (I.Kind == wasm::WASM_EXTERNAL_GLOBAL)
ImportedGlobals.emplace_back(&I);
+ else if (I.Kind == wasm::WASM_EXTERNAL_EVENT)
+ ImportedEvents.emplace_back(&I);
}
while (Count--) {
wasm::WasmSymbolInfo Info;
const wasm::WasmSignature *FunctionType = nullptr;
const wasm::WasmGlobalType *GlobalType = nullptr;
+ const wasm::WasmEventType *EventType = nullptr;
Info.Kind = readUint8(Ctx);
Info.Flags = readVaruint32(Ctx);
@@ -532,6 +539,32 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
break;
}
+ case wasm::WASM_SYMBOL_TYPE_EVENT: {
+ Info.ElementIndex = readVaruint32(Ctx);
+ if (!isValidEventIndex(Info.ElementIndex) ||
+ IsDefined != isDefinedEventIndex(Info.ElementIndex))
+ return make_error<GenericBinaryError>("invalid event symbol index",
+ object_error::parse_failed);
+ if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) ==
+ wasm::WASM_SYMBOL_BINDING_WEAK)
+ return make_error<GenericBinaryError>("undefined weak global symbol",
+ object_error::parse_failed);
+ if (IsDefined) {
+ Info.Name = readString(Ctx);
+ unsigned EventIndex = Info.ElementIndex - NumImportedEvents;
+ wasm::WasmEvent &Event = Events[EventIndex];
+ EventType = &Event.Type;
+ if (Event.SymbolName.empty())
+ Event.SymbolName = Info.Name;
+
+ } else {
+ wasm::WasmImport &Import = *ImportedEvents[Info.ElementIndex];
+ EventType = &Import.Event;
+ Info.Name = Import.Field;
+ }
+ break;
+ }
+
default:
return make_error<GenericBinaryError>("Invalid symbol type",
object_error::parse_failed);
@@ -545,7 +578,7 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) {
object_error::parse_failed);
LinkingData.SymbolTable.emplace_back(Info);
Symbols.emplace_back(LinkingData.SymbolTable.back(), FunctionType,
- GlobalType);
+ GlobalType, EventType);
LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n");
}
@@ -635,6 +668,11 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, ReadContext &Ctx) {
return make_error<GenericBinaryError>("Bad relocation global index",
object_error::parse_failed);
break;
+ case wasm::R_WEBASSEMBLY_EVENT_INDEX_LEB:
+ if (!isValidEventSymbol(Reloc.Index))
+ return make_error<GenericBinaryError>("Bad relocation event index",
+ object_error::parse_failed);
+ break;
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_LEB:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
case wasm::R_WEBASSEMBLY_MEMORY_ADDR_I32:
@@ -755,6 +793,11 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) {
return make_error<GenericBinaryError>("Invalid table element type",
object_error::parse_failed);
break;
+ case wasm::WASM_EXTERNAL_EVENT:
+ NumImportedEvents++;
+ Im.Event.Attribute = readVarint32(Ctx);
+ Im.Event.SigIndex = readVarint32(Ctx);
+ break;
default:
return make_error<GenericBinaryError>("Unexpected import kind",
object_error::parse_failed);
@@ -831,6 +874,24 @@ Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) {
return Error::success();
}
+Error WasmObjectFile::parseEventSection(ReadContext &Ctx) {
+ EventSection = Sections.size();
+ uint32_t Count = readVarint32(Ctx);
+ Events.reserve(Count);
+ while (Count--) {
+ wasm::WasmEvent Event;
+ Event.Index = NumImportedEvents + Events.size();
+ Event.Type.Attribute = readVaruint32(Ctx);
+ Event.Type.SigIndex = readVarint32(Ctx);
+ Events.push_back(Event);
+ }
+
+ if (Ctx.Ptr != Ctx.End)
+ return make_error<GenericBinaryError>("Event section ended prematurely",
+ object_error::parse_failed);
+ return Error::success();
+}
+
Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
uint32_t Count = readVaruint32(Ctx);
Exports.reserve(Count);
@@ -850,6 +911,11 @@ Error WasmObjectFile::parseExportSection(ReadContext &Ctx) {
return make_error<GenericBinaryError>("Invalid global export",
object_error::parse_failed);
break;
+ case wasm::WASM_EXTERNAL_EVENT:
+ if (!isValidEventIndex(Ex.Index))
+ return make_error<GenericBinaryError>("Invalid event export",
+ object_error::parse_failed);
+ break;
case wasm::WASM_EXTERNAL_MEMORY:
case wasm::WASM_EXTERNAL_TABLE:
break;
@@ -881,6 +947,14 @@ bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const {
return Index >= NumImportedGlobals && isValidGlobalIndex(Index);
}
+bool WasmObjectFile::isValidEventIndex(uint32_t Index) const {
+ return Index < NumImportedEvents + Events.size();
+}
+
+bool WasmObjectFile::isDefinedEventIndex(uint32_t Index) const {
+ return Index >= NumImportedEvents && isValidEventIndex(Index);
+}
+
bool WasmObjectFile::isValidFunctionSymbol(uint32_t Index) const {
return Index < Symbols.size() && Symbols[Index].isTypeFunction();
}
@@ -889,6 +963,10 @@ bool WasmObjectFile::isValidGlobalSymbol(uint32_t Index) const {
return Index < Symbols.size() && Symbols[Index].isTypeGlobal();
}
+bool WasmObjectFile::isValidEventSymbol(uint32_t Index) const {
+ return Index < Symbols.size() && Symbols[Index].isTypeEvent();
+}
+
bool WasmObjectFile::isValidDataSymbol(uint32_t Index) const {
return Index < Symbols.size() && Symbols[Index].isTypeData();
}
@@ -907,6 +985,11 @@ wasm::WasmGlobal &WasmObjectFile::getDefinedGlobal(uint32_t Index) {
return Globals[Index - NumImportedGlobals];
}
+wasm::WasmEvent &WasmObjectFile::getDefinedEvent(uint32_t Index) {
+ assert(isDefinedEventIndex(Index));
+ return Events[Index - NumImportedEvents];
+}
+
Error WasmObjectFile::parseStartSection(ReadContext &Ctx) {
StartFunction = readVaruint32(Ctx);
if (!isValidFunctionIndex(StartFunction))
@@ -1070,6 +1153,7 @@ uint64_t WasmObjectFile::getWasmSymbolValue(const WasmSymbol &Sym) const {
switch (Sym.Info.Kind) {
case wasm::WASM_SYMBOL_TYPE_FUNCTION:
case wasm::WASM_SYMBOL_TYPE_GLOBAL:
+ case wasm::WASM_SYMBOL_TYPE_EVENT:
return Sym.Info.ElementIndex;
case wasm::WASM_SYMBOL_TYPE_DATA: {
// The value of a data symbol is the segment offset, plus the symbol
@@ -1112,6 +1196,8 @@ WasmObjectFile::getSymbolType(DataRefImpl Symb) const {
return SymbolRef::ST_Data;
case wasm::WASM_SYMBOL_TYPE_SECTION:
return SymbolRef::ST_Debug;
+ case wasm::WASM_SYMBOL_TYPE_EVENT:
+ return SymbolRef::ST_Other;
}
llvm_unreachable("Unknown WasmSymbol::SymbolType");
@@ -1135,10 +1221,12 @@ WasmObjectFile::getSymbolSection(DataRefImpl Symb) const {
case wasm::WASM_SYMBOL_TYPE_DATA:
Ref.d.a = DataSection;
break;
- case wasm::WASM_SYMBOL_TYPE_SECTION: {
+ case wasm::WASM_SYMBOL_TYPE_SECTION:
Ref.d.a = Sym.Info.ElementIndex;
break;
- }
+ case wasm::WASM_SYMBOL_TYPE_EVENT:
+ Ref.d.a = EventSection;
+ break;
default:
llvm_unreachable("Unknown WasmSymbol::SymbolType");
}
@@ -1161,6 +1249,7 @@ std::error_code WasmObjectFile::getSectionName(DataRefImpl Sec,
ECase(TABLE);
ECase(MEMORY);
ECase(GLOBAL);
+ ECase(EVENT);
ECase(EXPORT);
ECase(START);
ECase(ELEM);
OpenPOWER on IntegriCloud