From 2ae385e560a62a18942cbc202cd19313c6c59470 Mon Sep 17 00:00:00 2001 From: Derek Schuff Date: Mon, 6 Jun 2022 15:42:24 -0700 Subject: [PATCH] [WebAssembly] Add WASM_SEC_LAST_KNOWN to BinaryFormat section types list [NFC] There are 3 places where we were using WASM_SEC_TAG as the "last" known section type, which requires updating (or leaves a bug) when a new known section type is added. Instead add a "last type" to the enum for this purpose. Differential Revision: https://reviews.llvm.org/D127164 --- lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp | 2 +- llvm/include/llvm/BinaryFormat/Wasm.h | 3 ++- llvm/lib/ObjCopy/wasm/WasmReader.cpp | 9 +++------ llvm/lib/Object/WasmObjectFile.cpp | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp index 58be1d0c7dbe4..905e9637493be 100644 --- a/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp +++ b/lldb/source/Plugins/ObjectFile/wasm/ObjectFileWasm.cpp @@ -192,7 +192,7 @@ bool ObjectFileWasm::DecodeNextSection(lldb::offset_t *offset_ptr) { m_sect_infos.push_back(section_info{*offset_ptr + c.tell(), section_length, section_id, *sect_name}); *offset_ptr += (c.tell() + section_length); - } else if (section_id <= llvm::wasm::WASM_SEC_TAG) { + } else if (section_id <= llvm::wasm::WASM_SEC_LAST_KNOWN) { m_sect_infos.push_back(section_info{*offset_ptr + c.tell(), static_cast(payload_len), section_id, ConstString()}); diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h index 4b6a1d584077b..62a6881ef36a3 100644 --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -252,7 +252,8 @@ enum : unsigned { WASM_SEC_CODE = 10, // Function bodies (code) WASM_SEC_DATA = 11, // Data segments WASM_SEC_DATACOUNT = 12, // Data segment count - WASM_SEC_TAG = 13 // Tag declarations + WASM_SEC_TAG = 13, // Tag declarations + WASM_SEC_LAST_KNOWN = WASM_SEC_TAG, }; // Type immediate encodings used in various contexts. diff --git a/llvm/lib/ObjCopy/wasm/WasmReader.cpp b/llvm/lib/ObjCopy/wasm/WasmReader.cpp index 3725c95626cf9..6e7d8b5591c92 100644 --- a/llvm/lib/ObjCopy/wasm/WasmReader.cpp +++ b/llvm/lib/ObjCopy/wasm/WasmReader.cpp @@ -24,15 +24,12 @@ Expected> Reader::create() const { const WasmSection &WS = WasmObj.getWasmSection(Sec); Obj->Sections.push_back( {static_cast(WS.Type), WS.Name, WS.Content}); - // Give known sections standard names to allow them to be selected. + // Give known sections standard names to allow them to be selected. (Custom + // sections already have their names filled in by the parser). Section &ReaderSec = Obj->Sections.back(); if (ReaderSec.SectionType > WASM_SEC_CUSTOM && - ReaderSec.SectionType <= WASM_SEC_TAG) + ReaderSec.SectionType <= WASM_SEC_LAST_KNOWN) ReaderSec.Name = sectionTypeToString(ReaderSec.SectionType); - // If the section type is CUSTOM, it has a name already. If it's a new type - // of section that we don't explicitly handle here, it will have an empty - // name and objcopy won't be able to select it by name (e.g. for removal - // or dumping) but it will still be valid and able to be copied. } return std::move(Obj); } diff --git a/llvm/lib/Object/WasmObjectFile.cpp b/llvm/lib/Object/WasmObjectFile.cpp index 73c2f868492e2..0e99c21295714 100644 --- a/llvm/lib/Object/WasmObjectFile.cpp +++ b/llvm/lib/Object/WasmObjectFile.cpp @@ -1729,7 +1729,7 @@ Expected WasmObjectFile::getSectionName(DataRefImpl Sec) const { const WasmSection &S = Sections[Sec.d.a]; if (S.Type == wasm::WASM_SEC_CUSTOM) return S.Name; - if (S.Type > wasm::WASM_SEC_TAG) + if (S.Type > wasm::WASM_SEC_LAST_KNOWN) return createStringError(object_error::invalid_section_index, ""); return wasm::sectionTypeToString(S.Type); }