[WebAssembly] Fix list of relocations with addends in lld

Summary:
The list of relocations with addend in lld was missing `R_WASM_MEMORY_ADDR_REL_SLEB`,
causing `wasm-ld` to generate corrupted output. This fixes that problem and while
we're at it pulls the list of such relocations into the Wasm.h header, to avoid
duplicating it in multiple places.

Reviewers: sbc100
Differential Revision: https://reviews.llvm.org/D63696

llvm-svn: 364367
This commit is contained in:
Keno Fischer
2019-06-26 00:52:42 +00:00
parent 5242fbde5a
commit cadcb9eb61
6 changed files with 38 additions and 22 deletions

View File

@@ -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

View File

@@ -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']

View File

@@ -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;
}
}
}

View File

@@ -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

View File

@@ -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;
}
}

View File

@@ -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