diff --git a/lld/test/wasm/multi-table.s b/lld/test/wasm/multi-table.s new file mode 100644 --- /dev/null +++ b/lld/test/wasm/multi-table.s @@ -0,0 +1,146 @@ +# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown -o %t.a1.o %s +# RUN: llvm-mc -filetype=obj -triple=wasm32-unknown-unknown %p/Inputs/call-indirect.s -o %t.a2.o +# RUN: wasm-ld --allow-undefined --export-dynamic --no-entry -o %t.wasm %t.a1.o %t.a2.o +# RUN: obj2yaml %t.wasm | FileCheck %s + + .globl table_a + .tabletype table_a, funcref + + .globl table_b +table_b: + .tabletype table_b, funcref + + .globl table_c +table_c: + .tabletype table_c, externref + + .globl call_indirect_explicit_tables +call_indirect_explicit_tables: + .functype call_indirect_explicit_tables () -> () + i32.const 0 + call_indirect () -> (), table_a + i32.const 0 + call_indirect () -> (), table_b + end_function + +# CHECK: --- !WASM +# CHECK-NEXT: FileHeader: +# CHECK-NEXT: Version: 0x1 +# CHECK-NEXT: Sections: +# CHECK-NEXT: - Type: TYPE +# CHECK-NEXT: Signatures: +# CHECK-NEXT: - Index: 0 +# CHECK-NEXT: ParamTypes: [] +# CHECK-NEXT: ReturnTypes: [] +# CHECK-NEXT: - Index: 1 +# CHECK-NEXT: ParamTypes: [] +# CHECK-NEXT: ReturnTypes: +# CHECK-NEXT: - I64 +# CHECK-NEXT: - Index: 2 +# CHECK-NEXT: ParamTypes: [] +# CHECK-NEXT: ReturnTypes: +# CHECK-NEXT: - I32 +# CHECK-NEXT: - Type: IMPORT +# CHECK-NEXT: Imports: +# CHECK-NEXT: - Module: env +# CHECK-NEXT: Field: table_a +# CHECK-NEXT: Kind: TABLE +# CHECK-NEXT: Table: +# CHECK-NEXT: Index: 0 +# CHECK-NEXT: ElemType: FUNCREF +# CHECK-NEXT: Limits: +# CHECK-NEXT: Initial: 0x0 +# CHECK-NEXT: - Module: env +# CHECK-NEXT: Field: foo +# CHECK-NEXT: Kind: FUNCTION +# CHECK-NEXT: SigIndex: 2 +# CHECK-NEXT: - Type: FUNCTION +# CHECK-NEXT: FunctionTypes: [ 0, 1, 0 ] +# CHECK-NEXT: - Type: TABLE +# CHECK-NEXT: Tables: +# CHECK-NEXT: - Index: 1 +# CHECK-NEXT: ElemType: FUNCREF +# CHECK-NEXT: Limits: +# CHECK-NEXT: Initial: 0x0 +# CHECK-NEXT: - Index: 2 +# CHECK-NEXT: ElemType: EXTERNREF +# CHECK-NEXT: Limits: +# CHECK-NEXT: Initial: 0x0 +# CHECK-NEXT: - Index: 3 +# CHECK-NEXT: ElemType: FUNCREF +# CHECK-NEXT: Limits: +# CHECK-NEXT: Flags: [ HAS_MAX ] +# CHECK-NEXT: Initial: 0x3 +# CHECK-NEXT: Maximum: 0x3 +# CHECK-NEXT: - Type: MEMORY +# CHECK-NEXT: Memories: +# CHECK-NEXT: - Initial: 0x2 +# CHECK-NEXT: - Type: GLOBAL +# CHECK-NEXT: Globals: +# CHECK-NEXT: - Index: 0 +# CHECK-NEXT: Type: I32 +# CHECK-NEXT: Mutable: true +# CHECK-NEXT: InitExpr: +# CHECK-NEXT: Opcode: I32_CONST +# CHECK-NEXT: Value: 66576 +# CHECK-NEXT: - Type: EXPORT +# CHECK-NEXT: Exports: +# CHECK-NEXT: - Name: memory +# CHECK-NEXT: Kind: MEMORY +# CHECK-NEXT: Index: 0 +# CHECK-NEXT: - Name: table_b +# CHECK-NEXT: Kind: TABLE +# CHECK-NEXT: Index: 1 +# CHECK-NEXT: - Name: table_c +# CHECK-NEXT: Kind: TABLE +# CHECK-NEXT: Index: 2 +# CHECK-NEXT: - Name: call_indirect_explicit_tables +# CHECK-NEXT: Kind: FUNCTION +# CHECK-NEXT: Index: 1 +# CHECK-NEXT: - Name: bar +# CHECK-NEXT: Kind: FUNCTION +# CHECK-NEXT: Index: 2 +# CHECK-NEXT: - Name: call_bar_indirect +# CHECK-NEXT: Kind: FUNCTION +# CHECK-NEXT: Index: 3 +# CHECK-NEXT: - Type: ELEM +# CHECK-NEXT: Segments: +# CHECK-NEXT: - Flags: 2 +# CHECK-NEXT: TableNumber: 3 +# CHECK-NEXT: Offset: +# CHECK-NEXT: Opcode: I32_CONST +# CHECK-NEXT: Value: 1 +# CHECK-NEXT: Functions: [ 2, 0 ] +# CHECK-NEXT: - Type: CODE +# CHECK-NEXT: Functions: +# CHECK-NEXT: - Index: 1 +# CHECK-NEXT: Locals: [] +# CHECK-NEXT: Body: 41001180808080008080808000410011808080800081808080000B +# CHECK-NEXT: - Index: 2 +# CHECK-NEXT: Locals: [] +# CHECK-NEXT: Body: 42010B +# CHECK-NEXT: - Index: 3 +# CHECK-NEXT: Locals: [] +# CHECK-NEXT: Body: 41002802808880800011818080800083808080001A41002802848880800011828080800083808080001A0B +# CHECK-NEXT: - Type: DATA +# CHECK-NEXT: Segments: +# CHECK-NEXT: - SectionOffset: 7 +# CHECK-NEXT: InitFlags: 0 +# CHECK-NEXT: Offset: +# CHECK-NEXT: Opcode: I32_CONST +# CHECK-NEXT: Value: 1024 +# CHECK-NEXT: Content: '0100000002000000' +# CHECK-NEXT: - Type: CUSTOM +# CHECK-NEXT: Name: name +# CHECK-NEXT: FunctionNames: +# CHECK-NEXT: - Index: 0 +# CHECK-NEXT: Name: foo +# CHECK-NEXT: - Index: 1 +# CHECK-NEXT: Name: call_indirect_explicit_tables +# CHECK-NEXT: - Index: 2 +# CHECK-NEXT: Name: bar +# CHECK-NEXT: - Index: 3 +# CHECK-NEXT: Name: call_bar_indirect +# CHECK-NEXT: GlobalNames: +# CHECK-NEXT: - Index: 0 +# CHECK-NEXT: Name: __stack_pointer diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -393,8 +393,16 @@ void ElemSection::writeBody() { raw_ostream &os = bodyOutputStream; + assert(WasmSym::indirectFunctionTable); writeUleb128(os, 1, "segment count"); - writeUleb128(os, 0, "table index"); + uint32_t TableNumber = WasmSym::indirectFunctionTable->getTableNumber(); + uint32_t Flags = 0; + if (TableNumber) + Flags |= WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER; + writeUleb128(os, Flags, "elem segment flags"); + if (Flags & WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER) + writeUleb128(os, TableNumber, "table number"); + WasmInitExpr initExpr; if (config->isPic) { initExpr.Opcode = WASM_OPCODE_GLOBAL_GET; @@ -404,8 +412,15 @@ initExpr.Value.Int32 = config->tableBase; } writeInitExpr(os, initExpr); - writeUleb128(os, indirectFunctions.size(), "elem count"); + if (Flags & WASM_ELEM_SEGMENT_HAS_ELEM_KIND) { + // We only write active function table initializers, for which the elem kind + // is specified to be written as 0x00 and interpreted to mean "funcref". + const uint8_t ElemKind = 0; + writeU8(os, ElemKind, "elem kind"); + } + + writeUleb128(os, indirectFunctions.size(), "elem count"); uint32_t tableIndex = config->tableBase; for (const FunctionSymbol *sym : indirectFunctions) { assert(sym->getTableIndex() == tableIndex); diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -157,7 +157,9 @@ }; struct WasmElemSegment { - uint32_t TableIndex; + uint32_t Flags; + uint32_t TableNumber; + uint8_t ElemKind; WasmInitExpr Offset; std::vector Functions; }; @@ -305,6 +307,13 @@ WASM_DATA_SEGMENT_HAS_MEMINDEX = 0x02, }; +enum : unsigned { + WASM_ELEM_SEGMENT_IS_PASSIVE = 0x01, + WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER = 0x02, + WASM_ELEM_SEGMENT_HAS_INIT_EXPRS = 0x04, + WASM_ELEM_SEGMENT_HAS_ELEM_KIND = ~WASM_ELEM_SEGMENT_HAS_INIT_EXPRS +}; + // Feature policy prefixes used in the custom "target_features" section enum : uint8_t { WASM_FEATURE_PREFIX_USED = '+', diff --git a/llvm/include/llvm/Object/Wasm.h b/llvm/include/llvm/Object/Wasm.h --- a/llvm/include/llvm/Object/Wasm.h +++ b/llvm/include/llvm/Object/Wasm.h @@ -219,9 +219,9 @@ bool isValidFunctionIndex(uint32_t Index) const; bool isDefinedFunctionIndex(uint32_t Index) const; bool isValidGlobalIndex(uint32_t Index) const; - bool isValidTableIndex(uint32_t Index) const; + bool isValidTableNumber(uint32_t Index) const; bool isDefinedGlobalIndex(uint32_t Index) const; - bool isDefinedTableIndex(uint32_t Index) const; + bool isDefinedTableNumber(uint32_t Index) const; bool isValidEventIndex(uint32_t Index) const; bool isDefinedEventIndex(uint32_t Index) const; bool isValidFunctionSymbol(uint32_t Index) const; diff --git a/llvm/include/llvm/ObjectYAML/WasmYAML.h b/llvm/include/llvm/ObjectYAML/WasmYAML.h --- a/llvm/include/llvm/ObjectYAML/WasmYAML.h +++ b/llvm/include/llvm/ObjectYAML/WasmYAML.h @@ -63,7 +63,9 @@ }; struct ElemSegment { - uint32_t TableIndex; + uint32_t Flags; + uint32_t TableNumber; + ValueType ElemKind; wasm::WasmInitExpr Offset; std::vector Functions; }; diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -311,7 +311,8 @@ uint32_t NumElements); void writeFunctionSection(ArrayRef Functions); void writeExportSection(ArrayRef Exports); - void writeElemSection(ArrayRef TableElems); + void writeElemSection(const MCSymbolWasm *IndirectFunctionTable, + ArrayRef TableElems); void writeDataCountSection(); uint32_t writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout, ArrayRef Functions); @@ -898,7 +899,9 @@ endSection(Section); } -void WasmObjectWriter::writeElemSection(ArrayRef TableElems) { +void WasmObjectWriter::writeElemSection( + const MCSymbolWasm *IndirectFunctionTable, ArrayRef TableElems) { + assert(IndirectFunctionTable || TableElems.empty()); if (TableElems.empty()) return; @@ -906,13 +909,28 @@ startSection(Section, wasm::WASM_SEC_ELEM); encodeULEB128(1, W->OS); // number of "segments" - encodeULEB128(0, W->OS); // the table index + + assert(WasmIndices.count(cast(IndirectFunctionTable))); + uint32_t TableNumber = WasmIndices.find(IndirectFunctionTable)->second; + uint32_t Flags = 0; + if (TableNumber) + Flags |= wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER; + encodeULEB128(Flags, W->OS); + if (Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER) + encodeULEB128(TableNumber, W->OS); // the table number // init expr for starting offset W->OS << char(wasm::WASM_OPCODE_I32_CONST); encodeSLEB128(InitialTableOffset, W->OS); W->OS << char(wasm::WASM_OPCODE_END); + if (Flags & wasm::WASM_ELEM_SEGMENT_HAS_ELEM_KIND) { + // We only write active function table initializers, for which the elem kind + // is specified to be written as 0x00 and interpreted to mean "funcref". + const uint8_t ElemKind = 0; + W->OS << ElemKind; + } + encodeULEB128(TableElems.size(), W->OS); for (uint32_t Elem : TableElems) encodeULEB128(Elem, W->OS); @@ -1795,7 +1813,10 @@ writeEventSection(Events); writeGlobalSection(Globals); writeExportSection(Exports); - writeElemSection(TableElems); + auto &Ctx = Asm.getContext(); + auto *IndirectFunctionTable = Ctx.lookupSymbol("__indirect_function_table"); + writeElemSection(cast_or_null(IndirectFunctionTable), + TableElems); writeDataCountSection(); CodeSectionIndex = writeCodeSection(Asm, Layout, Functions); diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -588,8 +588,8 @@ case wasm::WASM_SYMBOL_TYPE_TABLE: Info.ElementIndex = readVaruint32(Ctx); - if (!isValidTableIndex(Info.ElementIndex) || - IsDefined != isDefinedTableIndex(Info.ElementIndex)) + if (!isValidTableNumber(Info.ElementIndex) || + IsDefined != isDefinedTableNumber(Info.ElementIndex)) return make_error("invalid table symbol index", object_error::parse_failed); if (!IsDefined && (Info.Flags & wasm::WASM_SYMBOL_BINDING_MASK) == @@ -598,8 +598,8 @@ object_error::parse_failed); if (IsDefined) { Info.Name = readString(Ctx); - unsigned TableIndex = Info.ElementIndex - NumImportedTables; - wasm::WasmTable &Table = Tables[TableIndex]; + unsigned TableNumber = Info.ElementIndex - NumImportedTables; + wasm::WasmTable &Table = Tables[TableNumber]; TableType = &Table.Type; if (Table.SymbolName.empty()) Table.SymbolName = Info.Name; @@ -1199,7 +1199,7 @@ return Index < NumImportedGlobals + Globals.size(); } -bool WasmObjectFile::isValidTableIndex(uint32_t Index) const { +bool WasmObjectFile::isValidTableNumber(uint32_t Index) const { return Index < NumImportedTables + Tables.size(); } @@ -1207,8 +1207,8 @@ return Index >= NumImportedGlobals && isValidGlobalIndex(Index); } -bool WasmObjectFile::isDefinedTableIndex(uint32_t Index) const { - return Index >= NumImportedTables && isValidTableIndex(Index); +bool WasmObjectFile::isDefinedTableNumber(uint32_t Index) const { + return Index >= NumImportedTables && isValidTableNumber(Index); } bool WasmObjectFile::isValidEventIndex(uint32_t Index) const { @@ -1319,13 +1319,54 @@ ElemSegments.reserve(Count); while (Count--) { wasm::WasmElemSegment Segment; - Segment.TableIndex = readVaruint32(Ctx); - if (Segment.TableIndex != 0) { - return make_error("Invalid TableIndex", + Segment.Flags = readVaruint32(Ctx); + + uint32_t SupportedFlags = wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER | + wasm::WASM_ELEM_SEGMENT_IS_PASSIVE | + wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS; + if (Segment.Flags & ~SupportedFlags) + return make_error( + "Unsupported flags for element segment", object_error::parse_failed); + + if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER) + Segment.TableNumber = readVaruint32(Ctx); + else + Segment.TableNumber = 0; + if (!isValidTableNumber(Segment.TableNumber)) + return make_error("Invalid TableNumber", object_error::parse_failed); + + if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) { + Segment.Offset.Opcode = wasm::WASM_OPCODE_I32_CONST; + Segment.Offset.Value.Int32 = 0; + } else { + if (Error Err = readInitExpr(Segment.Offset, Ctx)) + return Err; } - if (Error Err = readInitExpr(Segment.Offset, Ctx)) - return Err; + + if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_ELEM_KIND) { + Segment.ElemKind = readUint8(Ctx); + if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) { + if (Segment.ElemKind != uint8_t(wasm::ValType::FUNCREF) && + Segment.ElemKind != uint8_t(wasm::ValType::EXTERNREF)) { + return make_error("Invalid reference type", + object_error::parse_failed); + } + } else { + if (Segment.ElemKind != 0) + return make_error("Invalid elemtype", + object_error::parse_failed); + Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF); + } + } else { + Segment.ElemKind = uint8_t(wasm::ValType::FUNCREF); + } + + if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_INIT_EXPRS) + return make_error( + "Elem segment init expressions not yet implemented", + object_error::parse_failed); + uint32_t NumElems = readVaruint32(Ctx); while (NumElems--) { Segment.Functions.push_back(readVaruint32(Ctx)); diff --git a/llvm/lib/ObjectYAML/WasmEmitter.cpp b/llvm/lib/ObjectYAML/WasmEmitter.cpp --- a/llvm/lib/ObjectYAML/WasmEmitter.cpp +++ b/llvm/lib/ObjectYAML/WasmEmitter.cpp @@ -471,9 +471,24 @@ WasmYAML::ElemSection &Section) { encodeULEB128(Section.Segments.size(), OS); for (auto &Segment : Section.Segments) { - encodeULEB128(Segment.TableIndex, OS); + encodeULEB128(Segment.Flags, OS); + if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER) + encodeULEB128(Segment.TableNumber, OS); + writeInitExpr(OS, Segment.Offset); + if (Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_ELEM_KIND) { + // We only support active function table initializers, for which the elem + // kind is specified to be written as 0x00 and interpreted to mean + // "funcref". + if (Segment.ElemKind != uint32_t(wasm::ValType::FUNCREF)) { + reportError("unexpected elemkind: " + Twine(Segment.ElemKind)); + return; + } + const uint8_t ElemKind = 0; + writeUint8(OS, ElemKind); + } + encodeULEB128(Segment.Functions.size(), OS); for (auto &Function : Segment.Functions) encodeULEB128(Function, OS); diff --git a/llvm/lib/ObjectYAML/WasmYAML.cpp b/llvm/lib/ObjectYAML/WasmYAML.cpp --- a/llvm/lib/ObjectYAML/WasmYAML.cpp +++ b/llvm/lib/ObjectYAML/WasmYAML.cpp @@ -373,6 +373,13 @@ void MappingTraits::mapping( IO &IO, WasmYAML::ElemSegment &Segment) { + if (!IO.outputting() || Segment.Flags) + IO.mapOptional("Flags", Segment.Flags); + if (!IO.outputting() || + Segment.Flags & wasm::WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER) + IO.mapOptional("TableNumber", Segment.TableNumber); + if (!IO.outputting() || Segment.Flags & wasm::WASM_ELEM_SEGMENT_IS_PASSIVE) + IO.mapOptional("ElemKind", Segment.ElemKind); IO.mapRequired("Offset", Segment.Offset); IO.mapRequired("Functions", Segment.Functions); } diff --git a/llvm/tools/obj2yaml/wasm2yaml.cpp b/llvm/tools/obj2yaml/wasm2yaml.cpp --- a/llvm/tools/obj2yaml/wasm2yaml.cpp +++ b/llvm/tools/obj2yaml/wasm2yaml.cpp @@ -317,7 +317,9 @@ auto ElemSec = std::make_unique(); for (auto &Segment : Obj.elements()) { WasmYAML::ElemSegment Seg; - Seg.TableIndex = Segment.TableIndex; + Seg.Flags = Segment.Flags; + Seg.TableNumber = Segment.TableNumber; + Seg.ElemKind = Segment.ElemKind; Seg.Offset = Segment.Offset; for (auto &Func : Segment.Functions) { Seg.Functions.push_back(Func);