[PATCH] D102271: [lld][WebAssembly] Fix for string merging + negative addends

Sam Clegg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 11 12:19:17 PDT 2021


sbc100 created this revision.
Herald added subscribers: wingo, ecnelises, sunfish, jgravelle-google, dschuff.
sbc100 requested review of this revision.
Herald added subscribers: llvm-commits, aheejin.
Herald added a project: LLVM.

Don't include the relocation addend when calculating the
virtual address of a symbol.  Instead just pass the symbol's
offset and add the addend afterwards.

Without this fix we hit the `offset is outside the section`
error in MergeInputSegment::getSegmentPiece.

This fixes a real world error we were are seeing in emscripten.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102271

Files:
  lld/test/wasm/merge-string.s
  lld/wasm/InputFiles.cpp
  lld/wasm/Symbols.cpp
  lld/wasm/Symbols.h


Index: lld/wasm/Symbols.h
===================================================================
--- lld/wasm/Symbols.h
+++ lld/wasm/Symbols.h
@@ -290,7 +290,7 @@
   static bool classof(const Symbol *s) { return s->kind() == DefinedDataKind; }
 
   // Returns the output virtual address of a defined data symbol.
-  uint64_t getVA(uint64_t addend = 0) const;
+  uint64_t getVA() const;
   void setVA(uint64_t va);
 
   // Returns the offset of a defined data symbol within its OutputSegment.
Index: lld/wasm/Symbols.cpp
===================================================================
--- lld/wasm/Symbols.cpp
+++ lld/wasm/Symbols.cpp
@@ -286,10 +286,9 @@
                      function ? &function->signature : nullptr),
       function(function) {}
 
-uint64_t DefinedData::getVA(uint64_t addend) const {
-  LLVM_DEBUG(dbgs() << "getVA: " << getName() << "\n");
+uint64_t DefinedData::getVA() const {
   if (segment)
-    return segment->getVA(value + addend);
+    return segment->getVA(value);
   return value;
 }
 
Index: lld/wasm/InputFiles.cpp
===================================================================
--- lld/wasm/InputFiles.cpp
+++ lld/wasm/InputFiles.cpp
@@ -185,7 +185,7 @@
     if (D->segment && D->segment->outputSeg->isTLS())
       return D->getOutputSegmentOffset() + reloc.Addend;
 
-    uint64_t value = D->getVA(reloc.Addend);
+    uint64_t value = D->getVA() + reloc.Addend;
     if (reloc.Type == R_WASM_MEMORY_ADDR_LOCREL_I32) {
       const auto *segment = cast<InputSegment>(chunk);
       uint64_t p = segment->outputSeg->startVA + segment->outputSegmentOffset +
Index: lld/test/wasm/merge-string.s
===================================================================
--- lld/test/wasm/merge-string.s
+++ lld/test/wasm/merge-string.s
@@ -14,6 +14,11 @@
         .asciz "bc"
         .size bar, 4
 
+        .section .rodata_relocs,"",@
+negative_addend:
+        .int32 foo-10
+        .size negative_addend, 4
+
 .globl foo
 .globl bar
 .export_name    foo, foo


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102271.344533.patch
Type: text/x-patch
Size: 1997 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210511/fafc0b40/attachment.bin>


More information about the llvm-commits mailing list