[lld] r287614 - Convert MipsReginfoSection to SyntheticSection.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 21 19:57:08 PST 2016


Author: ruiu
Date: Mon Nov 21 21:57:08 2016
New Revision: 287614

URL: http://llvm.org/viewvc/llvm-project?rev=287614&view=rev
Log:
Convert MipsReginfoSection to SyntheticSection.

Modified:
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/SyntheticSections.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=287614&r1=287613&r2=287614&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Mon Nov 21 21:57:08 2016
@@ -221,30 +221,46 @@ template <class ELFT> void MipsOptionsSe
 
 // MIPS .reginfo section.
 template <class ELFT>
-MipsReginfoSection<ELFT>::MipsReginfoSection()
-    : InputSection<ELFT>(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ArrayRef<uint8_t>(),
-                         ".reginfo") {
-  auto Func = [this](ObjectFile<ELFT> *F, ArrayRef<uint8_t> D) {
-    if (D.size() != sizeof(Elf_Mips_RegInfo)) {
-      error(getFilename(F) + ": invalid size of .reginfo section");
-      return;
+MipsReginfoSection<ELFT>::MipsReginfoSection(Elf_Mips_RegInfo Reginfo)
+    : SyntheticSection<ELFT>(SHF_ALLOC, SHT_MIPS_REGINFO, 4, ".reginfo"),
+      Reginfo(Reginfo) {}
+
+template <class ELFT> void MipsReginfoSection<ELFT>::writeTo(uint8_t *Buf) {
+  if (!Config->Relocatable)
+    Reginfo.ri_gp_value = In<ELFT>::MipsGot->getVA() + MipsGPOffset;
+  memcpy(Buf, &Reginfo, sizeof(Reginfo));
+}
+
+template <class ELFT>
+MipsReginfoSection<ELFT> *MipsReginfoSection<ELFT>::create() {
+  // Section should be alive for O32 and N32 ABIs only.
+  if (ELFT::Is64Bits)
+    return nullptr;
+
+  Elf_Mips_RegInfo Reginfo = {};
+  bool Create = false;
+
+  for (InputSectionBase<ELFT> *Sec : Symtab<ELFT>::X->Sections) {
+    if (!Sec->Live || Sec->Type != SHT_MIPS_REGINFO)
+      continue;
+    Sec->Live = false;
+    Create = true;
+
+    if (Sec->Data.size() != sizeof(Elf_Mips_RegInfo)) {
+      error(getFilename(Sec->getFile()) + ": invalid size of .reginfo section");
+      return nullptr;
     }
-    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(D.data());
+    auto *R = reinterpret_cast<const Elf_Mips_RegInfo *>(Sec->Data.data());
     if (Config->Relocatable && R->ri_gp_value)
-      error(getFilename(F) + ": unsupported non-zero ri_gp_value");
+      error(getFilename(Sec->getFile()) + ": unsupported non-zero ri_gp_value");
+
     Reginfo.ri_gprmask |= R->ri_gprmask;
-    F->MipsGp0 = R->ri_gp_value;
+    Sec->getFile()->MipsGp0 = R->ri_gp_value;
   };
-  iterateSectionContents<ELFT>(SHT_MIPS_REGINFO, Func);
-
-  this->Data = ArrayRef<uint8_t>((const uint8_t *)&Reginfo, sizeof(Reginfo));
-  // Section should be alive for O32 and N32 ABIs only.
-  this->Live = !ELFT::Is64Bits;
-}
 
-template <class ELFT> void MipsReginfoSection<ELFT>::finalize() {
-  if (!Config->Relocatable)
-    Reginfo.ri_gp_value = In<ELFT>::MipsGot->getVA() + MipsGPOffset;
+  if (Create)
+    return make<MipsReginfoSection<ELFT>>(Reginfo);
+  return nullptr;
 }
 
 static ArrayRef<uint8_t> createInterp() {

Modified: lld/trunk/ELF/SyntheticSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.h?rev=287614&r1=287613&r2=287614&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.h (original)
+++ lld/trunk/ELF/SyntheticSections.h Mon Nov 21 21:57:08 2016
@@ -35,19 +35,6 @@ private:
   }
 };
 
-// MIPS .reginfo section.
-template <class ELFT>
-class MipsReginfoSection final : public InputSection<ELFT> {
-  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
-
-public:
-  MipsReginfoSection();
-  void finalize();
-
-private:
-  Elf_Mips_RegInfo Reginfo = {};
-};
-
 template <class ELFT> class SyntheticSection : public InputSection<ELFT> {
   typedef typename ELFT::uint uintX_t;
 
@@ -593,6 +580,22 @@ private:
   Elf_Mips_ABIFlags Flags;
 };
 
+// MIPS .reginfo section.
+template <class ELFT>
+class MipsReginfoSection final : public SyntheticSection<ELFT> {
+  typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
+
+public:
+  static MipsReginfoSection *create();
+
+  MipsReginfoSection(Elf_Mips_RegInfo Reginfo);
+  size_t getSize() const override { return sizeof(Elf_Mips_RegInfo); }
+  void writeTo(uint8_t *Buf) override;
+
+private:
+  Elf_Mips_RegInfo Reginfo;
+};
+
 template <class ELFT> InputSection<ELFT> *createCommonSection();
 template <class ELFT> InputSection<ELFT> *createInterpSection();
 template <class ELFT> MergeInputSection<ELFT> *createCommentSection();

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=287614&r1=287613&r2=287614&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Nov 21 21:57:08 2016
@@ -318,10 +318,9 @@ template <class ELFT> void Writer<ELFT>:
       Symtab<ELFT>::X->Sections.push_back(OptSec);
     }
     // MIPS .reginfo
-    auto *RegSec = make<MipsReginfoSection<ELFT>>();
-    if (RegSec->Live) {
-      In<ELFT>::MipsReginfo = RegSec;
-      Symtab<ELFT>::X->Sections.push_back(RegSec);
+    if (auto *Sec = MipsReginfoSection<ELFT>::create()) {
+      In<ELFT>::MipsReginfo = Sec;
+      Symtab<ELFT>::X->Sections.push_back(Sec);
     }
   }
 
@@ -1500,10 +1499,8 @@ template <class ELFT> void Writer<ELFT>:
 template <class ELFT> void Writer<ELFT>::writeSections() {
   uint8_t *Buf = Buffer->getBufferStart();
 
-  // Finalize MIPS .reginfo and .MIPS.options sections
-  // because they contain offsets to .got and _gp.
-  if (In<ELFT>::MipsReginfo)
-    In<ELFT>::MipsReginfo->finalize();
+  // Finalize MIPS .MIPS.options sections because that contains
+  // offsets to .got and _gp.
   if (In<ELFT>::MipsOptions)
     In<ELFT>::MipsOptions->finalize();
 




More information about the llvm-commits mailing list