Now that the ELFFile constructor does nothing, create it when needed.
authorRafael Espindola <rafael.espindola@gmail.com>
Thu, 3 Nov 2016 20:44:50 +0000 (20:44 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Thu, 3 Nov 2016 20:44:50 +0000 (20:44 +0000)
This avoids duplicating the buffer in InputFile.

llvm-svn: 285965

lld/ELF/ICF.cpp
lld/ELF/InputFiles.cpp
lld/ELF/InputFiles.h
lld/ELF/InputSection.cpp
lld/ELF/MarkLive.cpp
lld/ELF/OutputSections.cpp
lld/ELF/Relocations.cpp

index eff3852d610f01075a302fb93bc0fc0b5696f487..94fbf977320dfa0f6c9734b3d9dfb0a613f7a5eb 100644 (file)
@@ -219,8 +219,8 @@ bool ICF<ELFT>::equalsConstant(const InputSection<ELFT> *A,
   for (size_t I = 0, E = A->RelocSections.size(); I != E; ++I) {
     const Elf_Shdr *RA = A->RelocSections[I];
     const Elf_Shdr *RB = B->RelocSections[I];
-    ELFFile<ELFT> &FileA = A->File->getObj();
-    ELFFile<ELFT> &FileB = B->File->getObj();
+    ELFFile<ELFT> FileA = A->File->getObj();
+    ELFFile<ELFT> FileB = B->File->getObj();
     if (RA->sh_type == SHT_RELA) {
       if (!relocationEq(check(FileA.relas(RA)), check(FileB.relas(RB))))
         return false;
@@ -272,8 +272,8 @@ bool ICF<ELFT>::equalsVariable(const InputSection<ELFT> *A,
   for (size_t I = 0, E = A->RelocSections.size(); I != E; ++I) {
     const Elf_Shdr *RA = A->RelocSections[I];
     const Elf_Shdr *RB = B->RelocSections[I];
-    ELFFile<ELFT> &FileA = A->File->getObj();
-    ELFFile<ELFT> &FileB = B->File->getObj();
+    ELFFile<ELFT> FileA = A->File->getObj();
+    ELFFile<ELFT> FileB = B->File->getObj();
     if (RA->sh_type == SHT_RELA) {
       if (!variableEq(A, B, check(FileA.relas(RA)), check(FileB.relas(RB))))
         return false;
index f1fd58ddaf50cb7b4e8c8c37b59e612fabe40c98..a56361c7fd7b2bd54ceedf18718664265334eb1d 100644 (file)
@@ -110,11 +110,10 @@ template <class ELFT> static ELFKind getELFKind() {
 }
 
 template <class ELFT>
-ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB)
-    : InputFile(K, MB), ELFObj(MB.getBuffer()) {
+ELFFileBase<ELFT>::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) {
   EKind = getELFKind<ELFT>();
-  EMachine = ELFObj.getHeader()->e_machine;
-  OSABI = ELFObj.getHeader()->e_ident[llvm::ELF::EI_OSABI];
+  EMachine = getObj().getHeader()->e_machine;
+  OSABI = getObj().getHeader()->e_ident[llvm::ELF::EI_OSABI];
 }
 
 template <class ELFT>
@@ -126,18 +125,18 @@ typename ELFT::SymRange ELFFileBase<ELFT>::getElfSymbols(bool OnlyGlobals) {
 
 template <class ELFT>
 uint32_t ELFFileBase<ELFT>::getSectionIndex(const Elf_Sym &Sym) const {
-  return check(ELFObj.getSectionIndex(&Sym, Symbols, SymtabSHNDX));
+  return check(getObj().getSectionIndex(&Sym, Symbols, SymtabSHNDX));
 }
 
 template <class ELFT>
 void ELFFileBase<ELFT>::initSymtab(ArrayRef<Elf_Shdr> Sections,
                                    const Elf_Shdr *Symtab) {
   FirstNonLocal = Symtab->sh_info;
-  Symbols = check(ELFObj.symbols(Symtab));
+  Symbols = check(getObj().symbols(Symtab));
   if (FirstNonLocal == 0 || FirstNonLocal > Symbols.size())
     fatal(getFilename(this) + ": invalid sh_info in symbol table");
 
-  StringTable = check(ELFObj.getStringTableForSymtab(*Symtab, Sections));
+  StringTable = check(getObj().getStringTableForSymtab(*Symtab, Sections));
 }
 
 template <class ELFT>
@@ -174,7 +173,7 @@ template <class ELFT> uint32_t elf::ObjectFile<ELFT>::getMipsGp0() const {
 template <class ELFT>
 void elf::ObjectFile<ELFT>::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
   // Read section and symbol tables.
-  ArrayRef<Elf_Shdr> ObjSections = check(this->ELFObj.sections());
+  ArrayRef<Elf_Shdr> ObjSections = check(this->getObj().sections());
   initializeSections(ComdatGroups, ObjSections);
   initializeSymbols(ObjSections);
 }
@@ -197,7 +196,7 @@ elf::ObjectFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections,
 template <class ELFT>
 ArrayRef<typename elf::ObjectFile<ELFT>::Elf_Word>
 elf::ObjectFile<ELFT>::getShtGroupEntries(const Elf_Shdr &Sec) {
-  const ELFFile<ELFT> &Obj = this->ELFObj;
+  const ELFFile<ELFT> &Obj = this->getObj();
   ArrayRef<Elf_Word> Entries =
       check(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec));
   if (Entries.empty() || Entries[0] != GRP_COMDAT)
@@ -262,7 +261,7 @@ template <class ELFT>
 void elf::ObjectFile<ELFT>::initializeSections(
     DenseSet<CachedHashStringRef> &ComdatGroups,
     ArrayRef<Elf_Shdr> ObjSections) {
-  const ELFFile<ELFT> &Obj = this->ELFObj;
+  const ELFFile<ELFT> &Obj = this->getObj();
   uint64_t Size = ObjSections.size();
   Sections.resize(Size);
   unsigned I = -1;
@@ -344,7 +343,8 @@ template <class ELFT>
 InputSectionBase<ELFT> *
 elf::ObjectFile<ELFT>::createInputSection(const Elf_Shdr &Sec,
                                           StringRef SectionStringTable) {
-  StringRef Name = check(this->ELFObj.getSectionName(&Sec, SectionStringTable));
+  StringRef Name =
+      check(this->getObj().getSectionName(&Sec, SectionStringTable));
 
   switch (Sec.sh_type) {
   case SHT_ARM_ATTRIBUTES:
@@ -540,7 +540,8 @@ SharedFile<ELFT>::SharedFile(MemoryBufferRef M)
 template <class ELFT>
 const typename ELFT::Shdr *
 SharedFile<ELFT>::getSection(const Elf_Sym &Sym) const {
-  return check(this->ELFObj.getSection(&Sym, this->Symbols, this->SymtabSHNDX));
+  return check(
+      this->getObj().getSection(&Sym, this->Symbols, this->SymtabSHNDX));
 }
 
 // Partially parse the shared object file so that we can call
@@ -550,7 +551,7 @@ template <class ELFT> void SharedFile<ELFT>::parseSoName() {
   typedef typename ELFT::uint uintX_t;
   const Elf_Shdr *DynamicSec = nullptr;
 
-  const ELFFile<ELFT> Obj = this->ELFObj;
+  const ELFFile<ELFT> Obj = this->getObj();
   ArrayRef<Elf_Shdr> Sections = check(Obj.sections());
   for (const Elf_Shdr &Sec : Sections) {
     switch (Sec.sh_type) {
@@ -614,8 +615,8 @@ SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
     return Verdefs;
 
   // The location of the first global versym entry.
-  Versym = reinterpret_cast<const Elf_Versym *>(this->ELFObj.base() +
-                                                VersymSec->sh_offset) +
+  const char *Base = this->MB.getBuffer().data();
+  Versym = reinterpret_cast<const Elf_Versym *>(Base + VersymSec->sh_offset) +
            this->FirstNonLocal;
 
   // We cannot determine the largest verdef identifier without inspecting
@@ -627,7 +628,7 @@ SharedFile<ELFT>::parseVerdefs(const Elf_Versym *&Versym) {
 
   // Build the Verdefs array by following the chain of Elf_Verdef objects
   // from the start of the .gnu.version_d section.
-  const uint8_t *Verdef = this->ELFObj.base() + VerdefSec->sh_offset;
+  const char *Verdef = Base + VerdefSec->sh_offset;
   for (unsigned I = 0; I != VerdefCount; ++I) {
     auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
     Verdef += CurVerdef->vd_next;
index 47c249d0dc2abd7ab43288e2a72a8779e177ca36..b45643d8d369ccb24fad39765f89bcdbd171043e 100644 (file)
@@ -100,8 +100,9 @@ public:
     return K == ObjectKind || K == SharedKind;
   }
 
-  const llvm::object::ELFFile<ELFT> &getObj() const { return ELFObj; }
-  llvm::object::ELFFile<ELFT> &getObj() { return ELFObj; }
+  llvm::object::ELFFile<ELFT> getObj() const {
+    return llvm::object::ELFFile<ELFT>(MB.getBuffer());
+  }
 
   StringRef getStringTable() const { return StringTable; }
 
@@ -110,7 +111,6 @@ public:
   Elf_Sym_Range getElfSymbols(bool OnlyGlobals);
 
 protected:
-  llvm::object::ELFFile<ELFT> ELFObj;
   ArrayRef<Elf_Sym> Symbols;
   uint32_t FirstNonLocal = 0;
   ArrayRef<Elf_Word> SymtabSHNDX;
index 084707b73403f2525aad585168dd16b2734af68c..efff4b52973218c17b9884a098dffc9fb354eba1 100644 (file)
@@ -563,7 +563,7 @@ template <class ELFT> void EhInputSection<ELFT>::split() {
     return;
 
   if (RelocSection) {
-    ELFFile<ELFT> &Obj = this->File->getObj();
+    ELFFile<ELFT> Obj = this->File->getObj();
     if (RelocSection->sh_type == SHT_RELA)
       split(check(Obj.relas(RelocSection)));
     else
index 7d63bf3382057128fe19af357e5cf692fe843025..b7efe48ed183d9637f668b1214d442d7f977851d 100644 (file)
@@ -80,7 +80,7 @@ static ResolvedReloc<ELFT> resolveReloc(InputSectionBase<ELFT> &Sec,
 template <class ELFT>
 static void forEachSuccessor(InputSection<ELFT> &Sec,
                              std::function<void(ResolvedReloc<ELFT>)> Fn) {
-  ELFFile<ELFT> &Obj = Sec.getFile()->getObj();
+  ELFFile<ELFT> Obj = Sec.getFile()->getObj();
   for (const typename ELFT::Shdr *RelSec : Sec.RelocSections) {
     if (RelSec->sh_type == SHT_RELA) {
       for (const typename ELFT::Rela &Rel : check(Obj.relas(RelSec)))
@@ -153,7 +153,7 @@ scanEhFrameSection(EhInputSection<ELFT> &EH,
   // .eh_frame keep other section alive and some don't.
   EH.split();
 
-  ELFFile<ELFT> &EObj = EH.getFile()->getObj();
+  ELFFile<ELFT> EObj = EH.getFile()->getObj();
   if (EH.RelocSection->sh_type == SHT_RELA)
     scanEhFrameSection(EH, check(EObj.relas(EH.RelocSection)), Enqueue);
   else
index 44ea63ae7bfd5a7ca680829e4a776cf570ce922a..9179d197e7fa0e41ad31ab8ed4be9dbe739961f5 100644 (file)
@@ -1177,7 +1177,7 @@ void EhOutputSection<ELFT>::addSection(InputSectionBase<ELFT> *C) {
     return;
 
   if (const Elf_Shdr *RelSec = Sec->RelocSection) {
-    ELFFile<ELFT> &Obj = Sec->getFile()->getObj();
+    ELFFile<ELFT> Obj = Sec->getFile()->getObj();
     if (RelSec->sh_type == SHT_RELA)
       addSectionAux(Sec, check(Obj.relas(RelSec)));
     else
index 99f7ac1fdfc94c4b2dcb1b2908f38c1c6d7938e9..23d929fc3e8474fea53bbe3f6c60ffcbfbd52201 100644 (file)
@@ -776,7 +776,7 @@ static void scanRelocs(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
 template <class ELFT>
 void scanRelocations(InputSectionBase<ELFT> &S,
                      const typename ELFT::Shdr &RelSec) {
-  ELFFile<ELFT> &EObj = S.getFile()->getObj();
+  ELFFile<ELFT> EObj = S.getFile()->getObj();
   if (RelSec.sh_type == SHT_RELA)
     scanRelocs(S, check(EObj.relas(&RelSec)));
   else
@@ -806,7 +806,7 @@ static void createThunks(InputSectionBase<ELFT> &C, ArrayRef<RelTy> Rels) {
 template <class ELFT>
 void createThunks(InputSectionBase<ELFT> &S,
                   const typename ELFT::Shdr &RelSec) {
-  ELFFile<ELFT> &EObj = S.getFile()->getObj();
+  ELFFile<ELFT> EObj = S.getFile()->getObj();
   if (RelSec.sh_type == SHT_RELA)
     createThunks(S, check(EObj.relas(&RelSec)));
   else