[WebAssembly] Move code for copying of data segment relocation. NFC.
authorSam Clegg <sbc@chromium.org>
Sun, 17 Dec 2017 17:52:01 +0000 (17:52 +0000)
committerSam Clegg <sbc@chromium.org>
Sun, 17 Dec 2017 17:52:01 +0000 (17:52 +0000)
This is a preparetory change for function gc which also
requires relocations to be copied in ranges like this.

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

llvm-svn: 320948

lld/wasm/InputFiles.cpp
lld/wasm/Writer.cpp

index 993149532484cbf13d5b62fa3e757dd62d646441..e7463da39db9739106f1c565e385cd5b63e7a2fa 100644 (file)
@@ -139,6 +139,14 @@ InputSegment *ObjFile::getSegment(const WasmSymbol &WasmSym) {
   return nullptr;
 }
 
+static void copyRelocationsRange(std::vector<WasmRelocation> &To,
+                                 ArrayRef<WasmRelocation> From, size_t Start,
+                                 size_t End) {
+  for (const WasmRelocation &R : From)
+    if (R.Offset >= Start && R.Offset < End)
+      To.push_back(R);
+}
+
 void ObjFile::initializeSymbols() {
   Symbols.reserve(WasmObj->getNumberOfSymbols());
 
@@ -156,8 +164,13 @@ void ObjFile::initializeSymbols() {
   FunctionSymbols.resize(FunctionImports + WasmObj->functions().size());
   GlobalSymbols.resize(GlobalImports + WasmObj->globals().size());
 
-  for (const WasmSegment &Seg : WasmObj->dataSegments())
-    Segments.emplace_back(make<InputSegment>(&Seg, this));
+  for (const WasmSegment &S : WasmObj->dataSegments()) {
+    InputSegment *Seg = make<InputSegment>(&S, this);
+    copyRelocationsRange(Seg->Relocations, DataSection->Relocations,
+                         Seg->getInputSectionOffset(),
+                         Seg->getInputSectionOffset() + Seg->getSize());
+    Segments.emplace_back(Seg);
+  }
 
   // Populate `FunctionSymbols` and `GlobalSymbols` based on the WasmSymbols
   // in the object
index 17f0bc46014ca343dbea93d983f66c8db64ffd85..61ac54a3e4b3161ccacf273585fc75d68feb2b53 100644 (file)
@@ -646,12 +646,6 @@ void Writer::createOutputSegments() {
       }
       S->addInputSegment(Segment);
       DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
-      for (const WasmRelocation &R : File->DataSection->Relocations) {
-        if (R.Offset >= Segment->getInputSectionOffset() &&
-            R.Offset < Segment->getInputSectionOffset() + Segment->getSize()) {
-          Segment->Relocations.push_back(R);
-        }
-      }
     }
   }
 }