[lld] r244700 - Template ELF's OutputSection.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 16:34:30 PDT 2015


Author: rafael
Date: Tue Aug 11 18:34:29 2015
New Revision: 244700

URL: http://llvm.org/viewvc/llvm-project?rev=244700&view=rev
Log:
Template ELF's OutputSection.

This removes what I think is the last hard coded ELF64 structure.

Modified:
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=244700&r1=244699&r2=244700&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Aug 11 18:34:29 2015
@@ -30,22 +30,24 @@ namespace {
 // Chunks cannot belong to more than one OutputSections. The writer
 // creates multiple OutputSections and assign them unique,
 // non-overlapping file offsets and VAs.
-class OutputSection {
+template <class ELFT> class OutputSection {
 public:
+  typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
+  typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
+
   OutputSection(StringRef Name) : Name(Name), Header({}) {}
-  void setVA(uint64_t);
-  void setFileOffset(uint64_t);
-  template <class ELFT> void addSectionChunk(SectionChunk<ELFT> *C);
+  void setVA(uintX_t);
+  void setFileOffset(uintX_t);
+  void addSectionChunk(SectionChunk<ELFT> *C);
   std::vector<Chunk *> &getChunks() { return Chunks; }
-  template <class ELFT>
-  void writeHeaderTo(llvm::object::Elf_Shdr_Impl<ELFT> *SHdr);
+  void writeHeaderTo(Elf_Shdr *SHdr);
 
   // Returns the size of the section in the output file.
-  uint64_t getSize() { return Header.sh_size; }
+  uintX_t getSize() { return Header.sh_size; }
 
 private:
   StringRef Name;
-  llvm::ELF::Elf64_Shdr Header;
+  Elf_Shdr Header;
   std::vector<Chunk *> Chunks;
 };
 
@@ -65,8 +67,8 @@ private:
 
   SymbolTable *Symtab;
   std::unique_ptr<llvm::FileOutputBuffer> Buffer;
-  llvm::SpecificBumpPtrAllocator<OutputSection> CAlloc;
-  std::vector<OutputSection *> OutputSections;
+  llvm::SpecificBumpPtrAllocator<OutputSection<ELFT>> CAlloc;
+  std::vector<OutputSection<ELFT> *> OutputSections;
 
   uintX_t FileSize;
   uintX_t SizeOfHeaders;
@@ -100,13 +102,13 @@ template <class ELFT> void Writer<ELFT>:
   error(Buffer->commit());
 }
 
-void OutputSection::setVA(uint64_t VA) {
+template <class ELFT> void OutputSection<ELFT>::setVA(uintX_t VA) {
   Header.sh_addr = VA;
   for (Chunk *C : Chunks)
     C->setVA(C->getVA() + VA);
 }
 
-void OutputSection::setFileOffset(uint64_t Off) {
+template <class ELFT> void OutputSection<ELFT>::setFileOffset(uintX_t Off) {
   if (Header.sh_size == 0)
     return;
   Header.sh_offset = Off;
@@ -115,9 +117,7 @@ void OutputSection::setFileOffset(uint64
 }
 
 template <class ELFT>
-void OutputSection::addSectionChunk(SectionChunk<ELFT> *C) {
-  typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
-
+void OutputSection<ELFT>::addSectionChunk(SectionChunk<ELFT> *C) {
   Chunks.push_back(C);
   uintX_t Off = Header.sh_size;
   Off = RoundUpToAlignment(Off, C->getAlign());
@@ -129,32 +129,22 @@ void OutputSection::addSectionChunk(Sect
   Header.sh_flags |= C->getSectionHdr()->sh_flags;
 }
 
-template <class ELFT>
-void OutputSection::writeHeaderTo(Elf_Shdr_Impl<ELFT> *SHdr) {
-  SHdr->sh_name = Header.sh_name;
-  SHdr->sh_type = Header.sh_type;
-  SHdr->sh_flags = Header.sh_flags;
-  SHdr->sh_addr = Header.sh_addr;
-  SHdr->sh_offset = Header.sh_offset;
-  SHdr->sh_size = Header.sh_size;
-  SHdr->sh_link = Header.sh_link;
-  SHdr->sh_info = Header.sh_info;
-  SHdr->sh_addralign = Header.sh_addralign;
-  SHdr->sh_entsize = Header.sh_entsize;
+template <class ELFT> void OutputSection<ELFT>::writeHeaderTo(Elf_Shdr *SHdr) {
+  *SHdr = Header;
 }
 
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::createSections() {
-  SmallDenseMap<StringRef, OutputSection *> Map;
+  SmallDenseMap<StringRef, OutputSection<ELFT> *> Map;
   for (std::unique_ptr<ObjectFileBase> &FileB : Symtab->ObjectFiles) {
     auto &File = cast<ObjectFile<ELFT>>(*FileB);
     for (SectionChunk<ELFT> *C : File.getChunks()) {
-      OutputSection *&Sec = Map[C->getSectionName()];
+      OutputSection<ELFT> *&Sec = Map[C->getSectionName()];
       if (!Sec) {
-        Sec = new (CAlloc.Allocate()) OutputSection(C->getSectionName());
+        Sec = new (CAlloc.Allocate()) OutputSection<ELFT>(C->getSectionName());
         OutputSections.push_back(Sec);
       }
-      Sec->addSectionChunk<ELFT>(C);
+      Sec->addSectionChunk(C);
     }
   }
 }
@@ -165,7 +155,7 @@ template <class ELFT> void Writer<ELFT>:
   SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>), PageSize);
   uintX_t VA = 0x1000; // The first page is kept unmapped.
   uintX_t FileOff = SizeOfHeaders;
-  for (OutputSection *Sec : OutputSections) {
+  for (OutputSection<ELFT> *Sec : OutputSections) {
     Sec->setVA(VA);
     Sec->setFileOffset(FileOff);
     VA += RoundUpToAlignment(Sec->getSize(), PageSize);
@@ -218,8 +208,8 @@ template <class ELFT> void Writer<ELFT>:
   auto SHdrs = reinterpret_cast<Elf_Shdr_Impl<ELFT> *>(Buf + EHdr->e_shoff);
   // First entry is null.
   ++SHdrs;
-  for (OutputSection *Sec : OutputSections)
-    Sec->writeHeaderTo<ELFT>(SHdrs++);
+  for (OutputSection<ELFT> *Sec : OutputSections)
+    Sec->writeHeaderTo(SHdrs++);
 }
 
 template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
@@ -231,7 +221,7 @@ template <class ELFT> void Writer<ELFT>:
 // Write section contents to a mmap'ed file.
 template <class ELFT> void Writer<ELFT>::writeSections() {
   uint8_t *Buf = Buffer->getBufferStart();
-  for (OutputSection *Sec : OutputSections) {
+  for (OutputSection<ELFT> *Sec : OutputSections) {
     for (Chunk *C : Sec->getChunks())
       C->writeTo(Buf);
   }




More information about the llvm-commits mailing list