diff --git a/lld/wasm/SyntheticSections.h b/lld/wasm/SyntheticSections.h --- a/lld/wasm/SyntheticSections.h +++ b/lld/wasm/SyntheticSections.h @@ -141,17 +141,6 @@ protected: }; -class MemorySection : public SyntheticSection { -public: - MemorySection() : SyntheticSection(llvm::wasm::WASM_SEC_MEMORY) {} - - bool isNeeded() const override { return !config->importMemory; } - void writeBody() override; - - uint32_t numMemoryPages = 0; - uint32_t maxMemoryPages = 0; -}; - class TableSection : public SyntheticSection { public: TableSection() : SyntheticSection(llvm::wasm::WASM_SEC_TABLE) {} @@ -171,27 +160,15 @@ void writeBody() override; }; -class GlobalSection : public SyntheticSection { +class MemorySection : public SyntheticSection { public: - GlobalSection() : SyntheticSection(llvm::wasm::WASM_SEC_GLOBAL) {} - uint32_t numGlobals() const { - assert(isSealed); - return inputGlobals.size() + dataAddressGlobals.size() + - staticGotSymbols.size(); - } - bool isNeeded() const override { return numGlobals() > 0; } - void assignIndexes() override; - void writeBody() override; - void addGlobal(InputGlobal *global); - void addDataAddressGlobal(DefinedData *global); - void addStaticGOTEntry(Symbol *sym); + MemorySection() : SyntheticSection(llvm::wasm::WASM_SEC_MEMORY) {} - std::vector dataAddressGlobals; + bool isNeeded() const override { return !config->importMemory; } + void writeBody() override; -protected: - bool isSealed = false; - std::vector inputGlobals; - std::vector staticGotSymbols; + uint32_t numMemoryPages = 0; + uint32_t maxMemoryPages = 0; }; // The event section contains a list of declared wasm events associated with the @@ -214,6 +191,29 @@ std::vector inputEvents; }; +class GlobalSection : public SyntheticSection { +public: + GlobalSection() : SyntheticSection(llvm::wasm::WASM_SEC_GLOBAL) {} + uint32_t numGlobals() const { + assert(isSealed); + return inputGlobals.size() + dataAddressGlobals.size() + + staticGotSymbols.size(); + } + bool isNeeded() const override { return numGlobals() > 0; } + void assignIndexes() override; + void writeBody() override; + void addGlobal(InputGlobal *global); + void addDataAddressGlobal(DefinedData *global); + void addStaticGOTEntry(Symbol *sym); + + std::vector dataAddressGlobals; + +protected: + bool isSealed = false; + std::vector inputGlobals; + std::vector staticGotSymbols; +}; + class ExportSection : public SyntheticSection { public: ExportSection() : SyntheticSection(llvm::wasm::WASM_SEC_EXPORT) {} diff --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp --- a/lld/wasm/SyntheticSections.cpp +++ b/lld/wasm/SyntheticSections.cpp @@ -240,6 +240,26 @@ writeUleb128(os, maxMemoryPages, "max pages"); } +void EventSection::writeBody() { + raw_ostream &os = bodyOutputStream; + + writeUleb128(os, inputEvents.size(), "event count"); + for (InputEvent *e : inputEvents) { + e->event.Type.SigIndex = out.typeSec->lookupType(e->signature); + writeEvent(os, e->event); + } +} + +void EventSection::addEvent(InputEvent *event) { + if (!event->live) + return; + uint32_t eventIndex = + out.importSec->getNumImportedEvents() + inputEvents.size(); + LLVM_DEBUG(dbgs() << "addEvent: " << eventIndex << "\n"); + event->setEventIndex(eventIndex); + inputEvents.push_back(event); +} + void GlobalSection::assignIndexes() { uint32_t globalIndex = out.importSec->getNumImportedGlobals(); for (InputGlobal *g : inputGlobals) @@ -297,26 +317,6 @@ inputGlobals.push_back(global); } -void EventSection::writeBody() { - raw_ostream &os = bodyOutputStream; - - writeUleb128(os, inputEvents.size(), "event count"); - for (InputEvent *e : inputEvents) { - e->event.Type.SigIndex = out.typeSec->lookupType(e->signature); - writeEvent(os, e->event); - } -} - -void EventSection::addEvent(InputEvent *event) { - if (!event->live) - return; - uint32_t eventIndex = - out.importSec->getNumImportedEvents() + inputEvents.size(); - LLVM_DEBUG(dbgs() << "addEvent: " << eventIndex << "\n"); - event->setEventIndex(eventIndex); - inputEvents.push_back(event); -} - void ExportSection::writeBody() { raw_ostream &os = bodyOutputStream; diff --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp --- a/lld/wasm/Writer.cpp +++ b/lld/wasm/Writer.cpp @@ -336,8 +336,8 @@ addSection(out.functionSec); addSection(out.tableSec); addSection(out.memorySec); - addSection(out.globalSec); addSection(out.eventSec); + addSection(out.globalSec); addSection(out.exportSec); addSection(out.startSec); addSection(out.elemSec); @@ -966,8 +966,8 @@ out.functionSec = make(); out.tableSec = make(); out.memorySec = make(); - out.globalSec = make(); out.eventSec = make(); + out.globalSec = make(); out.exportSec = make(); out.startSec = make(segments.size()); out.elemSec = make(); 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 @@ -242,8 +242,8 @@ Error parseFunctionSection(ReadContext &Ctx); Error parseTableSection(ReadContext &Ctx); Error parseMemorySection(ReadContext &Ctx); - Error parseGlobalSection(ReadContext &Ctx); Error parseEventSection(ReadContext &Ctx); + Error parseGlobalSection(ReadContext &Ctx); Error parseExportSection(ReadContext &Ctx); Error parseStartSection(ReadContext &Ctx); Error parseElemSection(ReadContext &Ctx); @@ -290,8 +290,8 @@ uint32_t NumImportedEvents = 0; uint32_t CodeSection = 0; uint32_t DataSection = 0; - uint32_t GlobalSection = 0; uint32_t EventSection = 0; + uint32_t GlobalSection = 0; }; class WasmSectionOrderChecker { @@ -307,8 +307,8 @@ WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY, - WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EVENT, + WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM, 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 @@ -309,24 +309,24 @@ std::vector Memories; }; -struct GlobalSection : Section { - GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {} +struct EventSection : Section { + EventSection() : Section(wasm::WASM_SEC_EVENT) {} static bool classof(const Section *S) { - return S->Type == wasm::WASM_SEC_GLOBAL; + return S->Type == wasm::WASM_SEC_EVENT; } - std::vector Globals; + std::vector Events; }; -struct EventSection : Section { - EventSection() : Section(wasm::WASM_SEC_EVENT) {} +struct GlobalSection : Section { + GlobalSection() : Section(wasm::WASM_SEC_GLOBAL) {} static bool classof(const Section *S) { - return S->Type == wasm::WASM_SEC_EVENT; + return S->Type == wasm::WASM_SEC_GLOBAL; } - std::vector Events; + std::vector Globals; }; struct ExportSection : Section { 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 @@ -302,10 +302,10 @@ return parseTableSection(Ctx); case wasm::WASM_SEC_MEMORY: return parseMemorySection(Ctx); - case wasm::WASM_SEC_GLOBAL: - return parseGlobalSection(Ctx); case wasm::WASM_SEC_EVENT: return parseEventSection(Ctx); + case wasm::WASM_SEC_GLOBAL: + return parseGlobalSection(Ctx); case wasm::WASM_SEC_EXPORT: return parseExportSection(Ctx); case wasm::WASM_SEC_START: @@ -993,6 +993,24 @@ 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("Event section ended prematurely", + object_error::parse_failed); + return Error::success(); +} + Error WasmObjectFile::parseGlobalSection(ReadContext &Ctx) { GlobalSection = Sections.size(); uint32_t Count = readVaruint32(Ctx); @@ -1012,24 +1030,6 @@ 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("Event section ended prematurely", - object_error::parse_failed); - return Error::success(); -} - Error WasmObjectFile::parseExportSection(ReadContext &Ctx) { uint32_t Count = readVaruint32(Ctx); Exports.reserve(Count); @@ -1622,30 +1622,50 @@ // Represents the edges in a directed graph where any node B reachable from node // A is not allowed to appear before A in the section ordering, but may appear // afterward. -int WasmSectionOrderChecker::DisallowedPredecessors[WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = { - {}, // WASM_SEC_ORDER_NONE - {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT}, // WASM_SEC_ORDER_TYPE, - {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION}, // WASM_SEC_ORDER_IMPORT, - {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE}, // WASM_SEC_ORDER_FUNCTION, - {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY}, // WASM_SEC_ORDER_TABLE, - {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_GLOBAL}, // WASM_SEC_ORDER_MEMORY, - {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EVENT}, // WASM_SEC_ORDER_GLOBAL, - {WASM_SEC_ORDER_EVENT, WASM_SEC_ORDER_EXPORT}, // WASM_SEC_ORDER_EVENT, - {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START}, // WASM_SEC_ORDER_EXPORT, - {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM}, // WASM_SEC_ORDER_START, - {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT}, // WASM_SEC_ORDER_ELEM, - {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE}, // WASM_SEC_ORDER_DATACOUNT, - {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA}, // WASM_SEC_ORDER_CODE, - {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING}, // WASM_SEC_ORDER_DATA, - - // Custom Sections - {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE}, // WASM_SEC_ORDER_DYLINK, - {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME}, // WASM_SEC_ORDER_LINKING, - {}, // WASM_SEC_ORDER_RELOC (can be repeated), - {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS}, // WASM_SEC_ORDER_NAME, - {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES}, // WASM_SEC_ORDER_PRODUCERS, - {WASM_SEC_ORDER_TARGET_FEATURES} // WASM_SEC_ORDER_TARGET_FEATURES -}; +int WasmSectionOrderChecker::DisallowedPredecessors + [WASM_NUM_SEC_ORDERS][WASM_NUM_SEC_ORDERS] = { + // WASM_SEC_ORDER_NONE + {}, + // WASM_SEC_ORDER_TYPE + {WASM_SEC_ORDER_TYPE, WASM_SEC_ORDER_IMPORT}, + // WASM_SEC_ORDER_IMPORT + {WASM_SEC_ORDER_IMPORT, WASM_SEC_ORDER_FUNCTION}, + // WASM_SEC_ORDER_FUNCTION + {WASM_SEC_ORDER_FUNCTION, WASM_SEC_ORDER_TABLE}, + // WASM_SEC_ORDER_TABLE + {WASM_SEC_ORDER_TABLE, WASM_SEC_ORDER_MEMORY}, + // WASM_SEC_ORDER_MEMORY + {WASM_SEC_ORDER_MEMORY, WASM_SEC_ORDER_EVENT}, + // WASM_SEC_ORDER_EVENT + {WASM_SEC_ORDER_EVENT, WASM_SEC_ORDER_GLOBAL}, + // WASM_SEC_ORDER_GLOBAL + {WASM_SEC_ORDER_GLOBAL, WASM_SEC_ORDER_EXPORT}, + // WASM_SEC_ORDER_EXPORT + {WASM_SEC_ORDER_EXPORT, WASM_SEC_ORDER_START}, + // WASM_SEC_ORDER_START + {WASM_SEC_ORDER_START, WASM_SEC_ORDER_ELEM}, + // WASM_SEC_ORDER_ELEM + {WASM_SEC_ORDER_ELEM, WASM_SEC_ORDER_DATACOUNT}, + // WASM_SEC_ORDER_DATACOUNT + {WASM_SEC_ORDER_DATACOUNT, WASM_SEC_ORDER_CODE}, + // WASM_SEC_ORDER_CODE + {WASM_SEC_ORDER_CODE, WASM_SEC_ORDER_DATA}, + // WASM_SEC_ORDER_DATA + {WASM_SEC_ORDER_DATA, WASM_SEC_ORDER_LINKING}, + + // Custom Sections + // WASM_SEC_ORDER_DYLINK + {WASM_SEC_ORDER_DYLINK, WASM_SEC_ORDER_TYPE}, + // WASM_SEC_ORDER_LINKING + {WASM_SEC_ORDER_LINKING, WASM_SEC_ORDER_RELOC, WASM_SEC_ORDER_NAME}, + // WASM_SEC_ORDER_RELOC (can be repeated) + {}, + // WASM_SEC_ORDER_NAME + {WASM_SEC_ORDER_NAME, WASM_SEC_ORDER_PRODUCERS}, + // WASM_SEC_ORDER_PRODUCERS + {WASM_SEC_ORDER_PRODUCERS, WASM_SEC_ORDER_TARGET_FEATURES}, + // WASM_SEC_ORDER_TARGET_FEATURES + {WASM_SEC_ORDER_TARGET_FEATURES}}; bool WasmSectionOrderChecker::isValidSectionOrder(unsigned ID, StringRef CustomSectionName) { 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 @@ -41,8 +41,8 @@ void writeSectionContent(raw_ostream &OS, WasmYAML::FunctionSection &Section); void writeSectionContent(raw_ostream &OS, WasmYAML::TableSection &Section); void writeSectionContent(raw_ostream &OS, WasmYAML::MemorySection &Section); - void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section); void writeSectionContent(raw_ostream &OS, WasmYAML::EventSection &Section); + void writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section); void writeSectionContent(raw_ostream &OS, WasmYAML::ExportSection &Section); void writeSectionContent(raw_ostream &OS, WasmYAML::StartSection &Section); void writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section); @@ -415,6 +415,21 @@ } void WasmWriter::writeSectionContent(raw_ostream &OS, + WasmYAML::EventSection &Section) { + encodeULEB128(Section.Events.size(), OS); + uint32_t ExpectedIndex = NumImportedEvents; + for (auto &Event : Section.Events) { + if (Event.Index != ExpectedIndex) { + reportError("unexpected event index: " + Twine(Event.Index)); + return; + } + ++ExpectedIndex; + encodeULEB128(Event.Attribute, OS); + encodeULEB128(Event.SigIndex, OS); + } +} + +void WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::GlobalSection &Section) { encodeULEB128(Section.Globals.size(), OS); uint32_t ExpectedIndex = NumImportedGlobals; @@ -431,21 +446,6 @@ } void WasmWriter::writeSectionContent(raw_ostream &OS, - WasmYAML::EventSection &Section) { - encodeULEB128(Section.Events.size(), OS); - uint32_t ExpectedIndex = NumImportedEvents; - for (auto &Event : Section.Events) { - if (Event.Index != ExpectedIndex) { - reportError("unexpected event index: " + Twine(Event.Index)); - return; - } - ++ExpectedIndex; - encodeULEB128(Event.Attribute, OS); - encodeULEB128(Event.SigIndex, OS); - } -} - -void WasmWriter::writeSectionContent(raw_ostream &OS, WasmYAML::ElemSection &Section) { encodeULEB128(Section.Segments.size(), OS); for (auto &Segment : Section.Segments) { @@ -571,10 +571,10 @@ writeSectionContent(StringStream, *S); else if (auto S = dyn_cast(Sec.get())) writeSectionContent(StringStream, *S); - else if (auto S = dyn_cast(Sec.get())) - writeSectionContent(StringStream, *S); else if (auto S = dyn_cast(Sec.get())) writeSectionContent(StringStream, *S); + else if (auto S = dyn_cast(Sec.get())) + writeSectionContent(StringStream, *S); else if (auto S = dyn_cast(Sec.get())) writeSectionContent(StringStream, *S); else if (auto S = dyn_cast(Sec.get())) 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 @@ -118,14 +118,14 @@ IO.mapOptional("Memories", Section.Memories); } -static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) { +static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) { commonSectionMapping(IO, Section); - IO.mapOptional("Globals", Section.Globals); + IO.mapOptional("Events", Section.Events); } -static void sectionMapping(IO &IO, WasmYAML::EventSection &Section) { +static void sectionMapping(IO &IO, WasmYAML::GlobalSection &Section) { commonSectionMapping(IO, Section); - IO.mapOptional("Events", Section.Events); + IO.mapOptional("Globals", Section.Globals); } static void sectionMapping(IO &IO, WasmYAML::ExportSection &Section) { @@ -227,16 +227,16 @@ Section.reset(new WasmYAML::MemorySection()); sectionMapping(IO, *cast(Section.get())); break; - case wasm::WASM_SEC_GLOBAL: - if (!IO.outputting()) - Section.reset(new WasmYAML::GlobalSection()); - sectionMapping(IO, *cast(Section.get())); - break; case wasm::WASM_SEC_EVENT: if (!IO.outputting()) Section.reset(new WasmYAML::EventSection()); sectionMapping(IO, *cast(Section.get())); break; + case wasm::WASM_SEC_GLOBAL: + if (!IO.outputting()) + Section.reset(new WasmYAML::GlobalSection()); + sectionMapping(IO, *cast(Section.get())); + break; case wasm::WASM_SEC_EXPORT: if (!IO.outputting()) Section.reset(new WasmYAML::ExportSection()); 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 @@ -262,6 +262,18 @@ S = std::move(MemorySec); break; } + case wasm::WASM_SEC_EVENT: { + auto EventSec = std::make_unique(); + for (auto &Event : Obj.events()) { + WasmYAML::Event E; + E.Index = Event.Index; + E.Attribute = Event.Type.Attribute; + E.SigIndex = Event.Type.SigIndex; + EventSec->Events.push_back(E); + } + S = std::move(EventSec); + break; + } case wasm::WASM_SEC_GLOBAL: { auto GlobalSec = std::make_unique(); for (auto &Global : Obj.globals()) { @@ -275,18 +287,6 @@ S = std::move(GlobalSec); break; } - case wasm::WASM_SEC_EVENT: { - auto EventSec = std::make_unique(); - for (auto &Event : Obj.events()) { - WasmYAML::Event E; - E.Index = Event.Index; - E.Attribute = Event.Type.Attribute; - E.SigIndex = Event.Type.SigIndex; - EventSec->Events.push_back(E); - } - S = std::move(EventSec); - break; - } case wasm::WASM_SEC_START: { auto StartSec = std::make_unique(); StartSec->StartFunction = Obj.startFunction();