[lld] r327468 - [WebAssembly] Fix expected contents of relocations with addends

Sam Clegg via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 13 17:53:34 PDT 2018


Author: sbc
Date: Tue Mar 13 17:53:34 2018
New Revision: 327468

URL: http://llvm.org/viewvc/llvm-project?rev=327468&view=rev
Log:
[WebAssembly] Fix expected contents of relocations with addends

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

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

Modified: lld/trunk/test/wasm/data-layout.ll
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/wasm/data-layout.ll?rev=327468&r1=327467&r2=327468&view=diff
==============================================================================
--- lld/trunk/test/wasm/data-layout.ll (original)
+++ lld/trunk/test/wasm/data-layout.ll Tue Mar 13 17:53:34 2018
@@ -9,6 +9,10 @@ target triple = "wasm32-unknown-unknown-
 @hello_str = external global i8*
 @external_ref = global i8** @hello_str, align 8
 
+%struct.s = type { i32, i32 }
+ at local_struct = hidden global %struct.s zeroinitializer, align 4
+ at 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-
 ; 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-
 ; 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-
 ; 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-
 ; 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

Modified: lld/trunk/wasm/InputChunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputChunks.cpp?rev=327468&r1=327467&r2=327468&view=diff
==============================================================================
--- lld/trunk/wasm/InputChunks.cpp (original)
+++ lld/trunk/wasm/InputChunks.cpp Tue Mar 13 17:53:34 2018
@@ -55,7 +55,8 @@ void InputChunk::writeTo(uint8_t *Buf) c
   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) {

Modified: lld/trunk/wasm/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/wasm/InputFiles.cpp?rev=327468&r1=327467&r2=327468&view=diff
==============================================================================
--- lld/trunk/wasm/InputFiles.cpp (original)
+++ lld/trunk/wasm/InputFiles.cpp Tue Mar 13 17:53:34 2018
@@ -77,7 +77,8 @@ uint32_t ObjFile::calcExpectedValue(cons
     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 Was
   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:




More information about the llvm-commits mailing list