[llvm] r236189 - Store relocations in a map from MCSectionELF.

Rafael Espindola rafael.espindola at gmail.com
Wed Apr 29 17:45:47 PDT 2015


Author: rafael
Date: Wed Apr 29 19:45:46 2015
New Revision: 236189

URL: http://llvm.org/viewvc/llvm-project?rev=236189&view=rev
Log:
Store relocations in a map from MCSectionELF.

Saves finding the MCSectionData just to do a map lookup.

Modified:
    llvm/trunk/lib/MC/ELFObjectWriter.cpp

Modified: llvm/trunk/lib/MC/ELFObjectWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/ELFObjectWriter.cpp?rev=236189&r1=236188&r2=236189&view=diff
==============================================================================
--- llvm/trunk/lib/MC/ELFObjectWriter.cpp (original)
+++ llvm/trunk/lib/MC/ELFObjectWriter.cpp Wed Apr 29 19:45:46 2015
@@ -111,8 +111,8 @@ class ELFObjectWriter : public MCObjectW
     SmallPtrSet<const MCSymbol *, 16> WeakrefUsedInReloc;
     DenseMap<const MCSymbol *, const MCSymbol *> Renames;
 
-    llvm::DenseMap<const MCSectionData *, std::vector<ELFRelocationEntry>>
-    Relocations;
+    llvm::DenseMap<const MCSectionELF *, std::vector<ELFRelocationEntry>>
+        Relocations;
     StringTableBuilder ShStrTabBuilder;
 
     /// @}
@@ -234,7 +234,7 @@ class ELFObjectWriter : public MCObjectW
                          SectionIndexMapTy &SectionIndexMap,
                          const RevGroupMapTy &RevGroupMap);
 
-    void createRelocationSection(MCAssembler &Asm, const MCSectionData &SD);
+    void createRelocationSection(MCAssembler &Asm, const MCSectionELF &Sec);
 
     void CompressDebugSections(MCAssembler &Asm, MCAsmLayout &Layout);
 
@@ -264,7 +264,7 @@ class ELFObjectWriter : public MCObjectW
                           uint64_t Size, uint32_t Link, uint32_t Info,
                           uint64_t Alignment, uint64_t EntrySize);
 
-    void writeRelocations(const MCAssembler &Asm, const MCSectionData &SD);
+    void writeRelocations(const MCAssembler &Asm, const MCSectionELF &Sec);
 
     bool
     IsSymbolRefDifferenceFullyResolvedImpl(const MCAssembler &Asm,
@@ -792,7 +792,9 @@ void ELFObjectWriter::RecordRelocation(M
                                        const MCFragment *Fragment,
                                        const MCFixup &Fixup, MCValue Target,
                                        bool &IsPCRel, uint64_t &FixedValue) {
-  const MCSectionData *FixupSection = Fragment->getParent();
+  const MCSectionData *FixupSectionD = Fragment->getParent();
+  const MCSectionELF &FixupSection =
+      cast<MCSectionELF>(FixupSectionD->getSection());
   uint64_t C = Target.getConstant();
   uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
 
@@ -822,7 +824,7 @@ void ELFObjectWriter::RecordRelocation(M
 
     assert(!SymB.isAbsolute() && "Should have been folded");
     const MCSection &SecB = SymB.getSection();
-    if (&SecB != &FixupSection->getSection())
+    if (&SecB != &FixupSection)
       Asm.getContext().FatalError(
           Fixup.getLoc(), "Cannot represent a difference across sections");
 
@@ -869,7 +871,7 @@ void ELFObjectWriter::RecordRelocation(M
         ELFSec ? Asm.getContext().getOrCreateSectionSymbol(*ELFSec)
                : nullptr;
     ELFRelocationEntry Rec(FixupOffset, SectionSymbol, Type, Addend);
-    Relocations[FixupSection].push_back(Rec);
+    Relocations[&FixupSection].push_back(Rec);
     return;
   }
 
@@ -883,7 +885,7 @@ void ELFObjectWriter::RecordRelocation(M
       UsedInReloc.insert(SymA);
   }
   ELFRelocationEntry Rec(FixupOffset, SymA, Type, Addend);
-  Relocations[FixupSection].push_back(Rec);
+  Relocations[&FixupSection].push_back(Rec);
   return;
 }
 
@@ -973,7 +975,7 @@ void ELFObjectWriter::computeIndexMap(
     unsigned Index = Sections.size();
     SectionIndexMap[&Section] = Index;
     maybeAddToGroup(Asm, Sections, RevGroupMap, Section, Index);
-    createRelocationSection(Asm, SD);
+    createRelocationSection(Asm, Section);
   }
 }
 
@@ -1123,15 +1125,12 @@ void ELFObjectWriter::computeSymbolTable
 }
 
 void ELFObjectWriter::createRelocationSection(MCAssembler &Asm,
-                                              const MCSectionData &SD) {
-  if (Relocations[&SD].empty())
+                                              const MCSectionELF &Sec) {
+  if (Relocations[&Sec].empty())
     return;
 
   MCContext &Ctx = Asm.getContext();
-  const MCSectionELF &Section =
-      static_cast<const MCSectionELF &>(SD.getSection());
-
-  const StringRef SectionName = Section.getSectionName();
+  const StringRef SectionName = Sec.getSectionName();
   std::string RelaSectionName = hasRelocationAddend() ? ".rela" : ".rel";
   RelaSectionName += SectionName;
 
@@ -1142,12 +1141,12 @@ void ELFObjectWriter::createRelocationSe
     EntrySize = is64Bit() ? sizeof(ELF::Elf64_Rel) : sizeof(ELF::Elf32_Rel);
 
   unsigned Flags = 0;
-  if (Section.getFlags() & ELF::SHF_GROUP)
+  if (Sec.getFlags() & ELF::SHF_GROUP)
     Flags = ELF::SHF_GROUP;
 
   const MCSectionELF *RelaSection = Ctx.createELFRelSection(
       RelaSectionName, hasRelocationAddend() ? ELF::SHT_RELA : ELF::SHT_REL,
-      Flags, EntrySize, Section.getGroup(), &Section);
+      Flags, EntrySize, Sec.getGroup(), &Sec);
   MCSectionData &RelSD = Asm.getOrCreateSectionData(*RelaSection);
   RelSD.setAlignment(is64Bit() ? 8 : 4);
 }
@@ -1316,8 +1315,8 @@ void ELFObjectWriter::WriteSecHdrEntry(u
 }
 
 void ELFObjectWriter::writeRelocations(const MCAssembler &Asm,
-                                       const MCSectionData &SD) {
-  std::vector<ELFRelocationEntry> &Relocs = Relocations[&SD];
+                                       const MCSectionELF &Sec) {
+  std::vector<ELFRelocationEntry> &Relocs = Relocations[&Sec];
 
   // Sort the relocation entries. Most targets just sort by Offset, but some
   // (e.g., MIPS) have additional constraints.
@@ -1552,13 +1551,10 @@ void ELFObjectWriter::WriteObject(MCAsse
     uint64_t SecStart = OS.tell();
 
     unsigned Type = Sec.getType();
-    if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA) {
+    if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
       writeDataSectionData(Asm, Layout, SD);
-    } else {
-      const MCSectionELF &RelocatedSec = *Sec.getAssociatedSection();
-      MCSectionData &RelocatedSecD = Asm.getOrCreateSectionData(RelocatedSec);
-      writeRelocations(Asm, RelocatedSecD);
-    }
+    else
+      writeRelocations(Asm, *Sec.getAssociatedSection());
 
     uint64_t SecEnd = OS.tell();
     SectionOffsets.push_back(std::make_pair(SecStart, SecEnd));





More information about the llvm-commits mailing list