109467b48Spatrick //===-- llvm/BinaryFormat/Wasm.cpp -------------------------------*- C++-*-===// 209467b48Spatrick // 309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information. 509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 609467b48Spatrick // 709467b48Spatrick //===----------------------------------------------------------------------===// 809467b48Spatrick 909467b48Spatrick #include "llvm/BinaryFormat/Wasm.h" 1009467b48Spatrick toString(wasm::WasmSymbolType Type)11*d415bd75Srobertllvm::StringRef llvm::wasm::toString(wasm::WasmSymbolType Type) { 1209467b48Spatrick switch (Type) { 1309467b48Spatrick case wasm::WASM_SYMBOL_TYPE_FUNCTION: 1409467b48Spatrick return "WASM_SYMBOL_TYPE_FUNCTION"; 1509467b48Spatrick case wasm::WASM_SYMBOL_TYPE_GLOBAL: 1609467b48Spatrick return "WASM_SYMBOL_TYPE_GLOBAL"; 1773471bf0Spatrick case wasm::WASM_SYMBOL_TYPE_TABLE: 1873471bf0Spatrick return "WASM_SYMBOL_TYPE_TABLE"; 1909467b48Spatrick case wasm::WASM_SYMBOL_TYPE_DATA: 2009467b48Spatrick return "WASM_SYMBOL_TYPE_DATA"; 2109467b48Spatrick case wasm::WASM_SYMBOL_TYPE_SECTION: 2209467b48Spatrick return "WASM_SYMBOL_TYPE_SECTION"; 2373471bf0Spatrick case wasm::WASM_SYMBOL_TYPE_TAG: 2473471bf0Spatrick return "WASM_SYMBOL_TYPE_TAG"; 2509467b48Spatrick } 2609467b48Spatrick llvm_unreachable("unknown symbol type"); 2709467b48Spatrick } 2809467b48Spatrick relocTypetoString(uint32_t Type)29*d415bd75Srobertllvm::StringRef llvm::wasm::relocTypetoString(uint32_t Type) { 3009467b48Spatrick switch (Type) { 3109467b48Spatrick #define WASM_RELOC(NAME, VALUE) \ 3209467b48Spatrick case VALUE: \ 3309467b48Spatrick return #NAME; 3409467b48Spatrick #include "llvm/BinaryFormat/WasmRelocs.def" 3509467b48Spatrick #undef WASM_RELOC 3609467b48Spatrick default: 3709467b48Spatrick llvm_unreachable("unknown reloc type"); 3809467b48Spatrick } 3909467b48Spatrick } 4009467b48Spatrick sectionTypeToString(uint32_t Type)41*d415bd75Srobertllvm::StringRef llvm::wasm::sectionTypeToString(uint32_t Type) { 42*d415bd75Srobert #define ECase(X) \ 43*d415bd75Srobert case wasm::WASM_SEC_##X: \ 44*d415bd75Srobert return #X; 45*d415bd75Srobert switch (Type) { 46*d415bd75Srobert ECase(CUSTOM); 47*d415bd75Srobert ECase(TYPE); 48*d415bd75Srobert ECase(IMPORT); 49*d415bd75Srobert ECase(FUNCTION); 50*d415bd75Srobert ECase(TABLE); 51*d415bd75Srobert ECase(MEMORY); 52*d415bd75Srobert ECase(GLOBAL); 53*d415bd75Srobert ECase(EXPORT); 54*d415bd75Srobert ECase(START); 55*d415bd75Srobert ECase(ELEM); 56*d415bd75Srobert ECase(CODE); 57*d415bd75Srobert ECase(DATA); 58*d415bd75Srobert ECase(DATACOUNT); 59*d415bd75Srobert ECase(TAG); 60*d415bd75Srobert default: 61*d415bd75Srobert llvm_unreachable("unknown section type"); 62*d415bd75Srobert } 63*d415bd75Srobert #undef ECase 64*d415bd75Srobert } 65*d415bd75Srobert relocTypeHasAddend(uint32_t Type)6609467b48Spatrickbool llvm::wasm::relocTypeHasAddend(uint32_t Type) { 6709467b48Spatrick switch (Type) { 6809467b48Spatrick case R_WASM_MEMORY_ADDR_LEB: 69097a140dSpatrick case R_WASM_MEMORY_ADDR_LEB64: 7009467b48Spatrick case R_WASM_MEMORY_ADDR_SLEB: 71097a140dSpatrick case R_WASM_MEMORY_ADDR_SLEB64: 7209467b48Spatrick case R_WASM_MEMORY_ADDR_REL_SLEB: 73097a140dSpatrick case R_WASM_MEMORY_ADDR_REL_SLEB64: 7409467b48Spatrick case R_WASM_MEMORY_ADDR_I32: 75097a140dSpatrick case R_WASM_MEMORY_ADDR_I64: 7673471bf0Spatrick case R_WASM_MEMORY_ADDR_TLS_SLEB: 7773471bf0Spatrick case R_WASM_MEMORY_ADDR_TLS_SLEB64: 7809467b48Spatrick case R_WASM_FUNCTION_OFFSET_I32: 7973471bf0Spatrick case R_WASM_FUNCTION_OFFSET_I64: 8009467b48Spatrick case R_WASM_SECTION_OFFSET_I32: 8173471bf0Spatrick case R_WASM_MEMORY_ADDR_LOCREL_I32: 8209467b48Spatrick return true; 8309467b48Spatrick default: 8409467b48Spatrick return false; 8509467b48Spatrick } 8609467b48Spatrick } 87