Skip to content

Commit

Permalink
[WebAssembly] Fix expected contents of relocations with addends
Browse files Browse the repository at this point in the history
This fixes issues found on the wasm waterfall related to relocations
with addends. Undefined symbols, even those with addends should
always have a provisional value of zero. At least this is what llvm
emits (and I believe this is true for ELF too).

Differential Revision: https://reviews.llvm.org/D44451

llvm-svn: 327468
  • Loading branch information
sbc100 committed Mar 14, 2018
1 parent 41e5ac4 commit 37fbfc6
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
43 changes: 35 additions & 8 deletions lld/test/wasm/data-layout.ll
Expand Up @@ -9,6 +9,10 @@ target triple = "wasm32-unknown-unknown-wasm"
@hello_str = external global i8*
@external_ref = global i8** @hello_str, align 8

%struct.s = type { i32, i32 }
@local_struct = hidden global %struct.s zeroinitializer, align 4
@local_struct_internal_ptr = hidden local_unnamed_addr global i32* getelementptr inbounds (%struct.s, %struct.s* @local_struct, i32 0, i32 1), align 4

; RUN: wasm-ld -no-gc-sections --check-signatures --allow-undefined -o %t.wasm %t.o %t.hello.o
; RUN: obj2yaml %t.wasm | FileCheck %s

Expand All @@ -34,13 +38,20 @@ target triple = "wasm32-unknown-unknown-wasm"
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 1024
; CHECK-NEXT: Content: 0100000000000000000000000000000003000000000000001C040000
; CHECK-NEXT: - SectionOffset: 41
; CHECK-NEXT: Content: '0100000000000000000000000000000003000000000000002804000024040000'
; CHECK-NEXT: - SectionOffset: 45
; CHECK-NEXT: MemoryIndex: 0
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 1052
; CHECK-NEXT: Value: 1056
; CHECK-NEXT: Content: '0000000000000000'
; CHECK-NEXT: - SectionOffset: 59
; CHECK-NEXT: MemoryIndex: 0
; CHECK-NEXT: Offset:
; CHECK-NEXT: Opcode: I32_CONST
; CHECK-NEXT: Value: 1064
; CHECK-NEXT: Content: 68656C6C6F0A00
; CHECK-NEXT: - Type: CUSTOM


; RUN: wasm-ld --check-signatures --relocatable -o %t_reloc.wasm %t.o %t.hello.o
Expand All @@ -49,8 +60,12 @@ target triple = "wasm32-unknown-unknown-wasm"
; RELOC: - Type: DATA
; RELOC-NEXT: Relocations:
; RELOC-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_I32
; RELOC-NEXT: Index: 4
; RELOC-NEXT: Index: 6
; RELOC-NEXT: Offset: 0x00000018
; RELOC-NEXT: - Type: R_WEBASSEMBLY_MEMORY_ADDR_I32
; RELOC-NEXT: Index: 3
; RELOC-NEXT: Offset: 0x0000002E
; RELOC-NEXT: Addend: 4
; RELOC-NEXT: Segments:
; RELOC-NEXT: - SectionOffset: 6
; RELOC-NEXT: MemoryIndex: 0
Expand All @@ -69,12 +84,24 @@ target triple = "wasm32-unknown-unknown-wasm"
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 24
; RELOC-NEXT: Content: 1C000000
; RELOC-NEXT: Content: '28000000'
; RELOC-NEXT: - SectionOffset: 33
; RELOC-NEXT: MemoryIndex: 0
; RELOC-NEXT: Offset:
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 28
; RELOC-NEXT: Content: '0000000000000000'
; RELOC-NEXT: - SectionOffset: 46
; RELOC-NEXT: MemoryIndex: 0
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 36
; RELOC-NEXT: Content: '20000000'
; RELOC-NEXT: - SectionOffset: 55
; RELOC-NEXT: MemoryIndex: 0
; RELOC-NEXT: Offset:
; RELOC-NEXT: Opcode: I32_CONST
; RELOC-NEXT: Value: 40
; RELOC-NEXT: Content: 68656C6C6F0A00

; RELOC: - Type: CUSTOM
Expand All @@ -98,9 +125,9 @@ target triple = "wasm32-unknown-unknown-wasm"
; RELOC-NEXT: Flags: [ ]
; RELOC-NEXT: Segment: 2
; RELOC-NEXT: Size: 4
; RELOC: - Index: 4
; RELOC: - Index: 6
; RELOC-NEXT: Kind: DATA
; RELOC-NEXT: Name: hello_str
; RELOC-NEXT: Flags: [ ]
; RELOC-NEXT: Segment: 3
; RELOC-NEXT: Segment: 5
; RELOC-NEXT: Size: 7
3 changes: 2 additions & 1 deletion lld/wasm/InputChunks.cpp
Expand Up @@ -55,7 +55,8 @@ void InputChunk::writeTo(uint8_t *Buf) const {
if (Relocations.empty())
return;

DEBUG(dbgs() << "applying relocations: count=" << Relocations.size() << "\n");
DEBUG(dbgs() << "applying relocations: " << getName()
<< " count=" << Relocations.size() << "\n");
int32_t Off = OutputOffset - getInputSectionOffset();

for (const WasmRelocation &Rel : Relocations) {
Expand Down
5 changes: 3 additions & 2 deletions lld/wasm/InputFiles.cpp
Expand Up @@ -77,7 +77,8 @@ uint32_t ObjFile::calcExpectedValue(const WasmRelocation &Reloc) const {
if (Sym.isUndefined())
return 0;
const WasmSegment& Segment = WasmObj->dataSegments()[Sym.Info.DataRef.Segment];
return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset;
return Segment.Data.Offset.Value.Int32 + Sym.Info.DataRef.Offset +
Reloc.Addend;
}
case R_WEBASSEMBLY_TYPE_INDEX_LEB:
return Reloc.Index;
Expand All @@ -102,7 +103,7 @@ uint32_t ObjFile::calcNewValue(const WasmRelocation &Reloc) const {
case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
if (auto *Sym = dyn_cast<DefinedData>(getDataSymbol(Reloc.Index)))
return Sym->getVirtualAddress() + Reloc.Addend;
return Reloc.Addend;
return 0;
case R_WEBASSEMBLY_TYPE_INDEX_LEB:
return TypeMap[Reloc.Index];
case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
Expand Down

0 comments on commit 37fbfc6

Please sign in to comment.