[WebAssembly] Fix expected contents of relocations with addends
authorSam Clegg <sbc@chromium.org>
Wed, 14 Mar 2018 00:53:34 +0000 (00:53 +0000)
committerSam Clegg <sbc@chromium.org>
Wed, 14 Mar 2018 00:53:34 +0000 (00:53 +0000)
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

lld/test/wasm/data-layout.ll
lld/wasm/InputChunks.cpp
lld/wasm/InputFiles.cpp

index e1e64b3..c4f6924 100644 (file)
@@ -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
 
@@ -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
@@ -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
@@ -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
@@ -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
index 748073e..ef3edb8 100644 (file)
@@ -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) {
index 0ffaae9..52be12a 100644 (file)
@@ -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;
@@ -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: