[PATCH] D23565: [ELF] Linkerscript: fix relocation offsets

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 16 10:57:46 PDT 2016


evgeny777 removed rL LLVM as the repository for this revision.
evgeny777 updated this revision to Diff 68222.
evgeny777 added a comment.

Jacob, I've added explicit template instantiations for DynamicReloc.
Please check, may be it works for you now.


https://reviews.llvm.org/D23565

Files:
  ELF/LinkerScript.cpp
  ELF/OutputSections.cpp
  ELF/OutputSections.h

Index: ELF/OutputSections.h
===================================================================
--- ELF/OutputSections.h
+++ ELF/OutputSections.h
@@ -237,9 +237,13 @@
         UseSymVA(UseSymVA), Addend(Addend) {}
 
   uintX_t getOffset() const;
+  void setSectionOffset(uintX_t Offset) { OffsetInSec = Offset; }
   uintX_t getAddend() const;
   uint32_t getSymIndex() const;
-  const OutputSectionBase<ELFT> *getOutputSec() const { return OutputSec; }
+  const OutputSectionBase<ELFT> *getOutputSec() const {
+    return OutputSec ? OutputSec : InputSec->OutSec;
+  }
+  const InputSectionBase<ELFT> *getInputSec() const { return InputSec; }
 
   uint32_t Type;
 
@@ -379,9 +383,10 @@
   typename Base::Kind getKind() const override { return Base::Reloc; }
   static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }
 
+  std::vector<DynamicReloc<ELFT>> Relocs;
+
 private:
   bool Sort;
-  std::vector<DynamicReloc<ELFT>> Relocs;
 };
 
 template <class ELFT>
Index: ELF/OutputSections.cpp
===================================================================
--- ELF/OutputSections.cpp
+++ ELF/OutputSections.cpp
@@ -1994,6 +1994,11 @@
 template class BuildIdHexstring<ELF64LE>;
 template class BuildIdHexstring<ELF64BE>;
 
+template class DynamicReloc<ELF32LE>;
+template class DynamicReloc<ELF32BE>;
+template class DynamicReloc<ELF64LE>;
+template class DynamicReloc<ELF64BE>;
+
 template class OutputSectionFactory<ELF32LE>;
 template class OutputSectionFactory<ELF32BE>;
 template class OutputSectionFactory<ELF64LE>;
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -304,6 +304,19 @@
   }
 }
 
+template <class ELFT> static void fixStaticRelocs(InputSection<ELFT> *I) {
+  for (Relocation<ELFT> &R : I->Relocations)
+    R.Offset += I->OutSecOff;
+}
+
+template <class ELFT>
+static void fixDynamicRelocs(OutputSectionBase<ELFT> *OutSec) {
+  for (DynamicReloc<ELFT> &D : Out<ELFT>::RelaDyn->Relocs)
+    if (D.getOutputSec() == OutSec)
+      D.setSectionOffset(D.getOffset() +
+                         cast<InputSection<ELFT>>(D.getInputSec())->OutSecOff);
+}
+
 template <class ELFT> void assignOffsets(OutputSectionBase<ELFT> *Sec) {
   auto *OutSec = dyn_cast<OutputSection<ELFT>>(Sec);
   if (!OutSec) {
@@ -330,12 +343,14 @@
       Off = alignTo(Off, I->Alignment);
       I->OutSecOff = Off;
       Off += I->getSize();
+      fixStaticRelocs(I);
     }
     // Update section size inside for-loop, so that SIZEOF
     // works correctly in the case below:
     // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
     Sec->setSize(Off);
   }
+  fixDynamicRelocs(Sec);
 }
 
 template <class ELFT>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D23565.68222.patch
Type: text/x-patch
Size: 2724 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160816/d0a0ca9a/attachment.bin>


More information about the llvm-commits mailing list