diff --git a/include/llvm/BinaryFormat/Wasm.h b/include/llvm/BinaryFormat/Wasm.h index 760ddf5a236..371e9add880 100644 --- a/include/llvm/BinaryFormat/Wasm.h +++ b/include/llvm/BinaryFormat/Wasm.h @@ -202,7 +202,8 @@ struct WasmSymbolInfo { enum class NameType { FUNCTION, - GLOBAL + GLOBAL, + DATA_SEGMENT, }; struct WasmDebugName { @@ -313,9 +314,10 @@ enum : uint8_t { // Kind codes used in the custom "name" section enum : unsigned { - WASM_NAMES_FUNCTION = 0x1, - WASM_NAMES_LOCAL = 0x2, - WASM_NAMES_GLOBAL = 0x7, + WASM_NAMES_FUNCTION = 1, + WASM_NAMES_LOCAL = 2, + WASM_NAMES_GLOBAL = 7, + WASM_NAMES_DATA_SEGMENT = 9, }; // Kind codes used in the custom "linking" section diff --git a/include/llvm/ObjectYAML/WasmYAML.h b/include/llvm/ObjectYAML/WasmYAML.h index 28cd56061a3..80f1b400620 100644 --- a/include/llvm/ObjectYAML/WasmYAML.h +++ b/include/llvm/ObjectYAML/WasmYAML.h @@ -222,6 +222,7 @@ struct NameSection : CustomSection { std::vector FunctionNames; std::vector GlobalNames; + std::vector DataSegmentNames; }; struct LinkingSection : CustomSection { diff --git a/lib/Object/WasmObjectFile.cpp b/lib/Object/WasmObjectFile.cpp index 7c8abcbd76f..c9b13e4afb4 100644 --- a/lib/Object/WasmObjectFile.cpp +++ b/lib/Object/WasmObjectFile.cpp @@ -357,6 +357,7 @@ Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { llvm::DenseSet SeenFunctions; llvm::DenseSet SeenGlobals; + llvm::DenseSet SeenSegments; if (FunctionTypes.size() && !SeenCodeSection) { return make_error("Names must come after code section", object_error::parse_failed); @@ -368,11 +369,13 @@ Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { const uint8_t *SubSectionEnd = Ctx.Ptr + Size; switch (Type) { case wasm::WASM_NAMES_FUNCTION: - case wasm::WASM_NAMES_GLOBAL: { + case wasm::WASM_NAMES_GLOBAL: + case wasm::WASM_NAMES_DATA_SEGMENT: { uint32_t Count = readVaruint32(Ctx); while (Count--) { uint32_t Index = readVaruint32(Ctx); StringRef Name = readString(Ctx); + wasm::NameType nameType = wasm::NameType::FUNCTION; if (Type == wasm::WASM_NAMES_FUNCTION) { if (!SeenFunctions.insert(Index).second) return make_error( @@ -383,18 +386,24 @@ Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { if (isDefinedFunctionIndex(Index)) getDefinedFunction(Index).DebugName = Name; - } else { + } else if (Type == wasm::WASM_NAMES_GLOBAL) { + nameType = wasm::NameType::GLOBAL; if (!SeenGlobals.insert(Index).second) return make_error("Global named more than once", object_error::parse_failed); if (!isValidGlobalIndex(Index) || Name.empty()) return make_error("Invalid name entry", object_error::parse_failed); + } else { + nameType = wasm::NameType::DATA_SEGMENT; + if (!SeenSegments.insert(Index).second) + return make_error( + "Segment named more than once", object_error::parse_failed); + if (Index > DataSegments.size()) + return make_error("Invalid named data segment", + object_error::parse_failed); } - wasm::NameType T = Type == wasm::WASM_NAMES_FUNCTION - ? wasm::NameType::FUNCTION - : wasm::NameType::GLOBAL; - DebugNames.push_back(wasm::WasmDebugName{T, Index, Name}); + DebugNames.push_back(wasm::WasmDebugName{nameType, Index, Name}); } break; } diff --git a/lib/ObjectYAML/WasmEmitter.cpp b/lib/ObjectYAML/WasmEmitter.cpp index 64498c82232..d9f820baaaa 100644 --- a/lib/ObjectYAML/WasmEmitter.cpp +++ b/lib/ObjectYAML/WasmEmitter.cpp @@ -281,6 +281,19 @@ void WasmWriter::writeSectionContent(raw_ostream &OS, writeStringRef(NameEntry.Name, SubSection.getStream()); } + SubSection.done(); + } + if (Section.DataSegmentNames.size()) { + writeUint8(OS, wasm::WASM_NAMES_DATA_SEGMENT); + + SubSectionWriter SubSection(OS); + + encodeULEB128(Section.DataSegmentNames.size(), SubSection.getStream()); + for (const WasmYAML::NameEntry &NameEntry : Section.DataSegmentNames) { + encodeULEB128(NameEntry.Index, SubSection.getStream()); + writeStringRef(NameEntry.Name, SubSection.getStream()); + } + SubSection.done(); } } diff --git a/lib/ObjectYAML/WasmYAML.cpp b/lib/ObjectYAML/WasmYAML.cpp index a6ad5c3e0b8..69c4fd6cf48 100644 --- a/lib/ObjectYAML/WasmYAML.cpp +++ b/lib/ObjectYAML/WasmYAML.cpp @@ -62,6 +62,7 @@ static void sectionMapping(IO &IO, WasmYAML::NameSection &Section) { IO.mapRequired("Name", Section.Name); IO.mapOptional("FunctionNames", Section.FunctionNames); IO.mapOptional("GlobalNames", Section.GlobalNames); + IO.mapOptional("DataSegmentNames", Section.DataSegmentNames); } static void sectionMapping(IO &IO, WasmYAML::LinkingSection &Section) { diff --git a/tools/obj2yaml/wasm2yaml.cpp b/tools/obj2yaml/wasm2yaml.cpp index 91855c30653..205ec1e0163 100644 --- a/tools/obj2yaml/wasm2yaml.cpp +++ b/tools/obj2yaml/wasm2yaml.cpp @@ -70,9 +70,11 @@ WasmDumper::dumpCustomSection(const WasmSection &WasmSec) { NameEntry.Index = Name.Index; if (Name.Type == llvm::wasm::NameType::FUNCTION) { NameSec->FunctionNames.push_back(NameEntry); - } else { - assert(Name.Type == llvm::wasm::NameType::GLOBAL); + } else if (Name.Type == llvm::wasm::NameType::GLOBAL) { NameSec->GlobalNames.push_back(NameEntry); + } else { + assert(Name.Type == llvm::wasm::NameType::DATA_SEGMENT); + NameSec->DataSegmentNames.push_back(NameEntry); } } CustomSec = std::move(NameSec);