diff --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp --- a/lld/wasm/OutputSections.cpp +++ b/lld/wasm/OutputSections.cpp @@ -33,41 +33,6 @@ } namespace wasm { -static StringRef sectionTypeToString(uint32_t sectionType) { - switch (sectionType) { - case WASM_SEC_CUSTOM: - return "CUSTOM"; - case WASM_SEC_TYPE: - return "TYPE"; - case WASM_SEC_IMPORT: - return "IMPORT"; - case WASM_SEC_FUNCTION: - return "FUNCTION"; - case WASM_SEC_TABLE: - return "TABLE"; - case WASM_SEC_MEMORY: - return "MEMORY"; - case WASM_SEC_GLOBAL: - return "GLOBAL"; - case WASM_SEC_TAG: - return "TAG"; - case WASM_SEC_EXPORT: - return "EXPORT"; - case WASM_SEC_START: - return "START"; - case WASM_SEC_ELEM: - return "ELEM"; - case WASM_SEC_CODE: - return "CODE"; - case WASM_SEC_DATA: - return "DATA"; - case WASM_SEC_DATACOUNT: - return "DATACOUNT"; - default: - fatal("invalid section type"); - } -} - StringRef OutputSection::getSectionName() const { return sectionTypeToString(type); } 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 @@ -470,8 +470,9 @@ return LHS.ElemType == RHS.ElemType && LHS.Limits == RHS.Limits; } -std::string toString(WasmSymbolType type); -std::string relocTypetoString(uint32_t type); +llvm::StringRef toString(WasmSymbolType type); +llvm::StringRef relocTypetoString(uint32_t type); +llvm::StringRef sectionTypeToString(uint32_t type); bool relocTypeHasAddend(uint32_t type); } // end namespace wasm diff --git a/llvm/lib/BinaryFormat/Wasm.cpp b/llvm/lib/BinaryFormat/Wasm.cpp --- a/llvm/lib/BinaryFormat/Wasm.cpp +++ b/llvm/lib/BinaryFormat/Wasm.cpp @@ -8,7 +8,7 @@ #include "llvm/BinaryFormat/Wasm.h" -std::string llvm::wasm::toString(wasm::WasmSymbolType Type) { +llvm::StringRef llvm::wasm::toString(wasm::WasmSymbolType Type) { switch (Type) { case wasm::WASM_SYMBOL_TYPE_FUNCTION: return "WASM_SYMBOL_TYPE_FUNCTION"; @@ -26,7 +26,7 @@ llvm_unreachable("unknown symbol type"); } -std::string llvm::wasm::relocTypetoString(uint32_t Type) { +llvm::StringRef llvm::wasm::relocTypetoString(uint32_t Type) { switch (Type) { #define WASM_RELOC(NAME, VALUE) \ case VALUE: \ @@ -38,6 +38,31 @@ } } +llvm::StringRef llvm::wasm::sectionTypeToString(uint32_t Type) { +#define ECase(X) \ + case wasm::WASM_SEC_##X: \ + return #X; + switch (Type) { + ECase(CUSTOM); + ECase(TYPE); + ECase(IMPORT); + ECase(FUNCTION); + ECase(TABLE); + ECase(MEMORY); + ECase(GLOBAL); + ECase(EXPORT); + ECase(START); + ECase(ELEM); + ECase(CODE); + ECase(DATA); + ECase(DATACOUNT); + ECase(TAG); + default: + llvm_unreachable("unknown section type"); + } +#undef ECase +} + bool llvm::wasm::relocTypeHasAddend(uint32_t Type) { switch (Type) { case R_WASM_MEMORY_ADDR_LEB: 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 @@ -1727,29 +1727,11 @@ Expected WasmObjectFile::getSectionName(DataRefImpl Sec) const { const WasmSection &S = Sections[Sec.d.a]; -#define ECase(X) \ - case wasm::WASM_SEC_##X: \ - return #X; - switch (S.Type) { - ECase(TYPE); - ECase(IMPORT); - ECase(FUNCTION); - ECase(TABLE); - ECase(MEMORY); - ECase(GLOBAL); - ECase(TAG); - ECase(EXPORT); - ECase(START); - ECase(ELEM); - ECase(CODE); - ECase(DATA); - ECase(DATACOUNT); - case wasm::WASM_SEC_CUSTOM: + if (S.Type == wasm::WASM_SEC_CUSTOM) return S.Name; - default: + if (S.Type > wasm::WASM_SEC_TAG) return createStringError(object_error::invalid_section_index, ""); - } -#undef ECase + return wasm::sectionTypeToString(S.Type); } uint64_t WasmObjectFile::getSectionAddress(DataRefImpl Sec) const { return 0; }