From 1827005cfcfff2100535d35c22e0cf7caa469c46 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Wed, 18 Nov 2020 21:38:23 -0800 Subject: [WebAssembly] Add support for named globals in the object format. Differential Revision: https://reviews.llvm.org/D91769 --- llvm/lib/Object/WasmObjectFile.cpp | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'llvm/lib/Object/WasmObjectFile.cpp') diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index d6bb96f..2504fda 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -356,7 +356,8 @@ Error WasmObjectFile::parseDylinkSection(ReadContext &Ctx) { } Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { - llvm::DenseSet Seen; + llvm::DenseSet SeenFunctions; + llvm::DenseSet SeenGlobals; if (FunctionTypes.size() && !SeenCodeSection) { return make_error("Names must come after code section", object_error::parse_failed); @@ -367,20 +368,34 @@ Error WasmObjectFile::parseNameSection(ReadContext &Ctx) { uint32_t Size = readVaruint32(Ctx); const uint8_t *SubSectionEnd = Ctx.Ptr + Size; switch (Type) { - case wasm::WASM_NAMES_FUNCTION: { + case wasm::WASM_NAMES_FUNCTION: + case wasm::WASM_NAMES_GLOBAL: { uint32_t Count = readVaruint32(Ctx); while (Count--) { uint32_t Index = readVaruint32(Ctx); - if (!Seen.insert(Index).second) - return make_error("Function named more than once", - object_error::parse_failed); StringRef Name = readString(Ctx); - if (!isValidFunctionIndex(Index) || Name.empty()) - return make_error("Invalid name entry", - object_error::parse_failed); - DebugNames.push_back(wasm::WasmFunctionName{Index, Name}); - if (isDefinedFunctionIndex(Index)) - getDefinedFunction(Index).DebugName = Name; + if (Type == wasm::WASM_NAMES_FUNCTION) { + if (!SeenFunctions.insert(Index).second) + return make_error( + "Function named more than once", object_error::parse_failed); + if (!isValidFunctionIndex(Index) || Name.empty()) + return make_error("Invalid name entry", + object_error::parse_failed); + + if (isDefinedFunctionIndex(Index)) + getDefinedFunction(Index).DebugName = Name; + } else { + 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); + } + wasm::NameType T = Type == wasm::WASM_NAMES_FUNCTION + ? wasm::NameType::FUNCTION + : wasm::NameType::GLOBAL; + DebugNames.push_back(wasm::WasmDebugName{T, Index, Name}); } break; } -- cgit v1.1