Skip to content

Commit cadcb9e

Browse files
committed
[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
1 parent 5242fbd commit cadcb9e

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

lld/test/wasm/emit-relocs-fpic.s

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# RUN: llvm-mc -triple=wasm32-unknown-unknown -filetype=obj -o %t.o < %s
2+
# RUN: llc --relocation-model=pic -filetype=obj %p/Inputs/ret32.ll -o %t.ret32.o
3+
# RUN: wasm-ld -pie --export-all --no-gc-sections --no-entry --emit-relocs -o %t.wasm %t.o %t.ret32.o
4+
# RUN: obj2yaml %t.wasm | FileCheck %s
5+
6+
load_hidden_data:
7+
.functype load_hidden_data () -> (i32)
8+
i32.const .L.hidden_data@MBREL
9+
end_function
10+
11+
.section .rodata.hidden_data,"",@
12+
.L.hidden_data:
13+
.int8 100
14+
.size .L.hidden_data, 1
15+
16+
# We just want to make sure that processing this relocation doesn't
17+
# cause corrupt output. We get most of the way there, by just checking
18+
# that obj2yaml doesn't fail. Here we just make sure that the relocation
19+
# survived the trip.
20+
# CHECK: R_WASM_MEMORY_ADDR_REL_SLEB

lld/test/wasm/lit.local.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
if 'wasm' not in config.available_features:
22
config.unsupported = True
33

4-
config.suffixes = ['.test', '.yaml', '.ll']
4+
config.suffixes = ['.test', '.yaml', '.ll', '.s']

lld/wasm/InputChunks.cpp

+1-8
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,8 @@ void InputChunk::writeRelocations(raw_ostream &OS) const {
154154
writeUleb128(OS, Rel.Offset + Off, "reloc offset");
155155
writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
156156

157-
switch (Rel.Type) {
158-
case R_WASM_MEMORY_ADDR_LEB:
159-
case R_WASM_MEMORY_ADDR_SLEB:
160-
case R_WASM_MEMORY_ADDR_I32:
161-
case R_WASM_FUNCTION_OFFSET_I32:
162-
case R_WASM_SECTION_OFFSET_I32:
157+
if (relocTypeHasAddend(Rel.Type))
163158
writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
164-
break;
165-
}
166159
}
167160
}
168161

llvm/include/llvm/BinaryFormat/Wasm.h

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ inline bool operator!=(const WasmGlobalType &LHS, const WasmGlobalType &RHS) {
364364

365365
std::string toString(WasmSymbolType type);
366366
std::string relocTypetoString(uint32_t type);
367+
bool relocTypeHasAddend(uint32_t type);
367368

368369
} // end namespace wasm
369370
} // end namespace llvm

llvm/lib/BinaryFormat/Wasm.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,17 @@ std::string llvm::wasm::relocTypetoString(uint32_t Type) {
3535
llvm_unreachable("unknown reloc type");
3636
}
3737
}
38+
39+
bool llvm::wasm::relocTypeHasAddend(uint32_t Type) {
40+
switch (Type) {
41+
case R_WASM_MEMORY_ADDR_LEB:
42+
case R_WASM_MEMORY_ADDR_SLEB:
43+
case R_WASM_MEMORY_ADDR_REL_SLEB:
44+
case R_WASM_MEMORY_ADDR_I32:
45+
case R_WASM_FUNCTION_OFFSET_I32:
46+
case R_WASM_SECTION_OFFSET_I32:
47+
return true;
48+
default:
49+
return false;
50+
}
51+
}

llvm/lib/MC/WasmObjectWriter.cpp

+1-13
Original file line numberDiff line numberDiff line change
@@ -147,19 +147,7 @@ struct WasmRelocationEntry {
147147
: Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
148148
FixupSection(FixupSection) {}
149149

150-
bool hasAddend() const {
151-
switch (Type) {
152-
case wasm::R_WASM_MEMORY_ADDR_LEB:
153-
case wasm::R_WASM_MEMORY_ADDR_SLEB:
154-
case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
155-
case wasm::R_WASM_MEMORY_ADDR_I32:
156-
case wasm::R_WASM_FUNCTION_OFFSET_I32:
157-
case wasm::R_WASM_SECTION_OFFSET_I32:
158-
return true;
159-
default:
160-
return false;
161-
}
162-
}
150+
bool hasAddend() const { return wasm::relocTypeHasAddend(Type); }
163151

164152
void print(raw_ostream &Out) const {
165153
Out << wasm::relocTypetoString(Type) << " Off=" << Offset

0 commit comments

Comments
 (0)