diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp --- a/lld/wasm/InputChunks.cpp +++ b/lld/wasm/InputChunks.cpp @@ -154,15 +154,8 @@ writeUleb128(OS, Rel.Offset + Off, "reloc offset"); writeUleb128(OS, File->calcNewIndex(Rel), "reloc index"); - switch (Rel.Type) { - case R_WASM_MEMORY_ADDR_LEB: - case R_WASM_MEMORY_ADDR_SLEB: - case R_WASM_MEMORY_ADDR_I32: - case R_WASM_FUNCTION_OFFSET_I32: - case R_WASM_SECTION_OFFSET_I32: + if (Rel.hasAddend()) writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend"); - break; - } } } 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 @@ -165,6 +165,8 @@ uint32_t Index; // Index into either symbol or type index space. uint64_t Offset; // Offset from the start of the section. int64_t Addend; // A value to add to the symbol. + static bool hasAddend(unsigned Type); + inline bool hasAddend() const { return hasAddend(Type); }; }; struct WasmInitFunc { @@ -322,6 +324,20 @@ #undef WASM_RELOC +inline bool WasmRelocation::hasAddend(unsigned Type) { + switch (Type) { + case R_WASM_MEMORY_ADDR_LEB: + case R_WASM_MEMORY_ADDR_SLEB: + case R_WASM_MEMORY_ADDR_REL_SLEB: + case R_WASM_MEMORY_ADDR_I32: + case R_WASM_FUNCTION_OFFSET_I32: + case R_WASM_SECTION_OFFSET_I32: + return true; + default: + return false; + } +} + // Subset of types that a value can have enum class ValType { I32 = WASM_TYPE_I32, diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -147,19 +147,7 @@ : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type), FixupSection(FixupSection) {} - bool hasAddend() const { - switch (Type) { - case wasm::R_WASM_MEMORY_ADDR_LEB: - case wasm::R_WASM_MEMORY_ADDR_SLEB: - case wasm::R_WASM_MEMORY_ADDR_REL_SLEB: - case wasm::R_WASM_MEMORY_ADDR_I32: - case wasm::R_WASM_FUNCTION_OFFSET_I32: - case wasm::R_WASM_SECTION_OFFSET_I32: - return true; - default: - return false; - } - } + bool hasAddend() const { return wasm::WasmRelocation::hasAddend(Type); } void print(raw_ostream &Out) const { Out << wasm::relocTypetoString(Type) << " Off=" << Offset