diff --git a/lld/test/wasm/emit-relocs-fpic.s b/lld/test/wasm/emit-relocs-fpic.s new file mode 100644 index 000000000000..e1adede20a5f --- /dev/null +++ b/lld/test/wasm/emit-relocs-fpic.s @@ -0,0 +1,20 @@ +# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o %t.o < %s +# RUN: llc --relocation-model=pic -filetype=obj %p/Inputs/ret32.ll -o %t.ret32.o +# RUN: wasm-ld -pie --export-all --no-gc-sections --no-entry --emit-relocs -o %t.wasm %t.o %t.ret32.o +# RUN: obj2yaml %t.wasm | FileCheck %s + +load_hidden_data: + .functype load_hidden_data () -> (i32) + i32.const .L.hidden_data@MBREL + end_function + +.section .rodata.hidden_data,"",@ +.L.hidden_data: + .int8 100 + .size .L.hidden_data, 1 + +# We just want to make sure that processing this relocation doesn't +# cause corrupt output. We get most of the way there, by just checking +# that obj2yaml doesn't fail. Here we just make sure that the relocation +# survived the trip. +# CHECK: R_WASM_MEMORY_ADDR_REL_SLEB diff --git a/lld/test/wasm/lit.local.cfg b/lld/test/wasm/lit.local.cfg index bc76e57c8e87..0b3ba0937d42 100644 --- a/lld/test/wasm/lit.local.cfg +++ b/lld/test/wasm/lit.local.cfg @@ -1,4 +1,4 @@ if 'wasm' not in config.available_features: config.unsupported = True -config.suffixes = ['.test', '.yaml', '.ll'] +config.suffixes = ['.test', '.yaml', '.ll', '.s'] diff --git a/lld/wasm/InputChunks.cpp b/lld/wasm/InputChunks.cpp index 735eb77ae1b6..efb8d3164658 100644 --- a/lld/wasm/InputChunks.cpp +++ b/lld/wasm/InputChunks.cpp @@ -154,15 +154,8 @@ void InputChunk::writeRelocations(raw_ostream &OS) const { 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 (relocTypeHasAddend(Rel.Type)) writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend"); - break; - } } } diff --git a/llvm/include/llvm/BinaryFormat/Wasm.h b/llvm/include/llvm/BinaryFormat/Wasm.h index ee48a187f8e6..0f327c188bf3 100644 --- a/llvm/include/llvm/BinaryFormat/Wasm.h +++ b/llvm/include/llvm/BinaryFormat/Wasm.h @@ -364,6 +364,7 @@ inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) { std::string toString(WasmSymbolType type); std::string relocTypetoString(uint32_t type); +bool relocTypeHasAddend(uint32_t type); } // end namespace wasm } // end namespace llvm diff --git a/llvm/lib/BinaryFormat/Wasm.cpp b/llvm/lib/BinaryFormat/Wasm.cpp index 1d36b2cb6fbd..d46be481edb3 100644 --- a/llvm/lib/BinaryFormat/Wasm.cpp +++ b/llvm/lib/BinaryFormat/Wasm.cpp @@ -35,3 +35,17 @@ std::string llvm::wasm::relocTypetoString(uint32_t Type) { llvm_unreachable("unknown reloc type"); } } + +bool llvm::wasm::relocTypeHasAddend(uint32_t 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; + } +} diff --git a/llvm/lib/MC/WasmObjectWriter.cpp b/llvm/lib/MC/WasmObjectWriter.cpp index dab5bb329bc1..8743eb7ee3c9 100644 --- a/llvm/lib/MC/WasmObjectWriter.cpp +++ b/llvm/lib/MC/WasmObjectWriter.cpp @@ -147,19 +147,7 @@ struct WasmRelocationEntry { : 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::relocTypeHasAddend(Type); } void print(raw_ostream &Out) const { Out << wasm::relocTypetoString(Type) << " Off=" << Offset