diff options
Diffstat (limited to 'llvm/lib/Object/WasmObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/WasmObjectFile.cpp | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 23418a3..107a9ef 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -218,6 +218,7 @@ static wasm::WasmTable readTable(WasmObjectFile::ReadContext &Ctx) { wasm::WasmTable Table; Table.ElemType = readUint8(Ctx); Table.Limits = readLimits(Ctx); + // The caller needs to set Table.Index field for Table return Table; } @@ -499,6 +500,7 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { wasm::WasmSymbolInfo Info; const wasm::WasmSignature *Signature = nullptr; const wasm::WasmGlobalType *GlobalType = nullptr; + uint8_t TableType = 0; const wasm::WasmEventType *EventType = nullptr; Info.Kind = readUint8(Ctx); @@ -567,6 +569,27 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { } break; + case wasm::WASM_SYMBOL_TYPE_TABLE: + Info.ElementIndex = readVaruint32(Ctx); + if (!isValidTableIndex(Info.ElementIndex) || + IsDefined != isDefinedTableIndex(Info.ElementIndex)) + return make_error<GenericBinaryError>("invalid table 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 table symbol", + object_error::parse_failed); + if (IsDefined) { + Info.Name = readString(Ctx); + unsigned TableIndex = Info.ElementIndex - NumImportedTables; + wasm::WasmTable &Table = Tables[TableIndex]; + TableType = Table.ElemType; + } else { + return make_error<GenericBinaryError>("undefined table symbol", + object_error::parse_failed); + } + break; + case wasm::WASM_SYMBOL_TYPE_DATA: Info.Name = readString(Ctx); if (IsDefined) { @@ -644,8 +667,8 @@ Error WasmObjectFile::parseLinkingSectionSymtab(ReadContext &Ctx) { Twine(Info.Name), object_error::parse_failed); LinkingData.SymbolTable.emplace_back(Info); - Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, EventType, - Signature); + Symbols.emplace_back(LinkingData.SymbolTable.back(), GlobalType, TableType, + EventType, Signature); LLVM_DEBUG(dbgs() << "Adding symbol: " << Symbols.back() << "\n"); } @@ -963,12 +986,17 @@ Error WasmObjectFile::parseImportSection(ReadContext &Ctx) { if (Im.Memory.Flags & wasm::WASM_LIMITS_FLAG_IS_64) HasMemory64 = true; break; - case wasm::WASM_EXTERNAL_TABLE: + case wasm::WASM_EXTERNAL_TABLE: { Im.Table = readTable(Ctx); - if (Im.Table.ElemType != wasm::WASM_TYPE_FUNCREF) + Im.Table.Index = NumImportedTables + Tables.size(); + NumImportedTables++; + auto ElemType = Im.Table.ElemType; + if (ElemType != wasm::WASM_TYPE_FUNCREF && + ElemType != wasm::WASM_TYPE_EXTERNREF) return make_error<GenericBinaryError>("Invalid table element type", object_error::parse_failed); break; + } case wasm::WASM_EXTERNAL_EVENT: NumImportedEvents++; Im.Event.Attribute = readVarint32(Ctx); @@ -1008,8 +1036,12 @@ Error WasmObjectFile::parseTableSection(ReadContext &Ctx) { uint32_t Count = readVaruint32(Ctx); Tables.reserve(Count); while (Count--) { - Tables.push_back(readTable(Ctx)); - if (Tables.back().ElemType != wasm::WASM_TYPE_FUNCREF) { + wasm::WasmTable T = readTable(Ctx); + T.Index = NumImportedTables + Tables.size(); + Tables.push_back(T); + auto ElemType = Tables.back().ElemType; + if (ElemType != wasm::WASM_TYPE_FUNCREF && + ElemType != wasm::WASM_TYPE_EXTERNREF) { return make_error<GenericBinaryError>("Invalid table element type", object_error::parse_failed); } @@ -1125,10 +1157,18 @@ bool WasmObjectFile::isValidGlobalIndex(uint32_t Index) const { return Index < NumImportedGlobals + Globals.size(); } +bool WasmObjectFile::isValidTableIndex(uint32_t Index) const { + return Index < NumImportedTables + Tables.size(); +} + bool WasmObjectFile::isDefinedGlobalIndex(uint32_t Index) const { return Index >= NumImportedGlobals && isValidGlobalIndex(Index); } +bool WasmObjectFile::isDefinedTableIndex(uint32_t Index) const { + return Index >= NumImportedTables && isValidTableIndex(Index); +} + bool WasmObjectFile::isValidEventIndex(uint32_t Index) const { return Index < NumImportedEvents + Events.size(); } |