[lld] r244042 - For now we only have on Chunk type. Simplify.

Rafael Espindola rafael.espindola at gmail.com
Wed Aug 5 06:55:35 PDT 2015


Author: rafael
Date: Wed Aug  5 08:55:34 2015
New Revision: 244042

URL: http://llvm.org/viewvc/llvm-project?rev=244042&view=rev
Log:
For now we only have on Chunk type. Simplify.

The others we have in sight are
* common symbols.
* entries in SHF_MERGE sections.

They will have a substantially different treatment. It is not clear if it is
worth it putting them all in a single list just to dispatch based on the kind on
the other side.

I hope to implement common symbols soon, and then we will be in a position
to have a concrete discussion. For now this is simpler for the the implemented
features.

Modified:
    lld/trunk/ELF/Chunks.cpp
    lld/trunk/ELF/Chunks.h
    lld/trunk/ELF/InputFiles.h
    lld/trunk/ELF/Writer.cpp
    lld/trunk/ELF/Writer.h

Modified: lld/trunk/ELF/Chunks.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Chunks.cpp?rev=244042&r1=244041&r2=244042&view=diff
==============================================================================
--- lld/trunk/ELF/Chunks.cpp (original)
+++ lld/trunk/ELF/Chunks.cpp Wed Aug  5 08:55:34 2015
@@ -19,7 +19,7 @@ using namespace lld::elf2;
 template <class ELFT>
 SectionChunk<ELFT>::SectionChunk(object::ELFFile<ELFT> *Obj,
                                  const Elf_Shdr *Header)
-    : Chunk(SectionKind), Obj(Obj), Header(Header) {
+    : Obj(Obj), Header(Header) {
   // Initialize SectionName.
   ErrorOr<StringRef> Name = Obj->getSectionName(Header);
   error(Name);

Modified: lld/trunk/ELF/Chunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Chunks.h?rev=244042&r1=244041&r2=244042&view=diff
==============================================================================
--- lld/trunk/ELF/Chunks.h (original)
+++ lld/trunk/ELF/Chunks.h Wed Aug  5 08:55:34 2015
@@ -26,8 +26,6 @@ class OutputSection;
 // doesn't even have actual data (if common or bss).
 class Chunk {
 public:
-  enum Kind { SectionKind, OtherKind };
-  Kind kind() const { return ChunkKind; }
   virtual ~Chunk() = default;
 
   // Returns the size of this chunk (even if this is a common or BSS.)
@@ -56,9 +54,6 @@ public:
   OutputSection *getOutputSection() { return Out; }
 
 protected:
-  Chunk(Kind K = OtherKind) : ChunkKind(K) {}
-  const Kind ChunkKind;
-
   // The VA of this chunk in the output. The writer sets a value.
   uint64_t VA = 0;
 
@@ -85,10 +80,6 @@ public:
   StringRef getSectionName() const override { return SectionName; }
   const Elf_Shdr *getSectionHdr() const { return Header; }
 
-  static bool classof(const Chunk *C) {
-    return C->kind() == SectionKind;
-  }
-
 private:
   // A file this chunk was created from.
   llvm::object::ELFFile<ELFT> *Obj;

Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=244042&r1=244041&r2=244042&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Wed Aug  5 08:55:34 2015
@@ -10,13 +10,13 @@
 #ifndef LLD_ELF_INPUT_FILES_H
 #define LLD_ELF_INPUT_FILES_H
 
+#include "Chunks.h"
 #include "lld/Core/LLVM.h"
 #include "llvm/Object/ELF.h"
 
 namespace lld {
 namespace elf2 {
 class SymbolBody;
-class Chunk;
 
 // The root class of input files.
 class InputFile {
@@ -50,16 +50,11 @@ public:
     return K >= Object32LEKind && K <= Object64BEKind;
   }
 
-  ArrayRef<Chunk *> getChunks() { return Chunks; }
   ArrayRef<SymbolBody *> getSymbols() override { return SymbolBodies; }
 
   virtual bool isCompatibleWith(const ObjectFileBase &Other) const = 0;
 
 protected:
-  // List of all chunks defined by this file. This includes both section
-  // chunks and non-section chunks for common symbols.
-  std::vector<Chunk *> Chunks;
-
   // List of all symbols referenced or defined by this file.
   std::vector<SymbolBody *> SymbolBodies;
 
@@ -93,6 +88,8 @@ public:
   // Returns the underying ELF file.
   llvm::object::ELFFile<ELFT> *getObj() const { return ELFObj.get(); }
 
+  ArrayRef<SectionChunk<ELFT> *> getChunks() { return Chunks; }
+
 private:
   void initializeChunks();
   void initializeSymbols();
@@ -100,6 +97,9 @@ private:
   SymbolBody *createSymbolBody(StringRef StringTable, const Elf_Sym *Sym);
 
   std::unique_ptr<llvm::object::ELFFile<ELFT>> ELFObj;
+
+  // List of all chunks defined by this file.
+  std::vector<SectionChunk<ELFT> *> Chunks;
 };
 
 } // namespace elf2

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=244042&r1=244041&r2=244042&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Aug  5 08:55:34 2015
@@ -51,7 +51,7 @@ void OutputSection::setFileOffset(uint64
 }
 
 template <class ELFT>
-void OutputSection::addChunk(Chunk *C) {
+void OutputSection::addSectionChunk(SectionChunk<ELFT> *C) {
   Chunks.push_back(C);
   C->setOutputSection(this);
   uint64_t Off = Header.sh_size;
@@ -60,10 +60,8 @@ void OutputSection::addChunk(Chunk *C) {
   C->setFileOff(Off);
   Off += C->getSize();
   Header.sh_size = Off;
-  if (auto SC = dyn_cast<SectionChunk<ELFT>>(C)) {
-    Header.sh_type = SC->getSectionHdr()->sh_type;
-    Header.sh_flags |= SC->getSectionHdr()->sh_flags;
-  }
+  Header.sh_type = C->getSectionHdr()->sh_type;
+  Header.sh_flags |= C->getSectionHdr()->sh_flags;
 }
 
 template <class ELFT>
@@ -83,14 +81,15 @@ void OutputSection::writeHeaderTo(Elf_Sh
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::createSections() {
   SmallDenseMap<StringRef, OutputSection *> Map;
-  for (std::unique_ptr<ObjectFileBase> &File : Symtab->ObjectFiles) {
-    for (Chunk *C : File->getChunks()) {
+  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()];
       if (!Sec) {
         Sec = new (CAlloc.Allocate()) OutputSection(C->getSectionName());
         OutputSections.push_back(Sec);
       }
-      Sec->addChunk<ELFT>(C);
+      Sec->addSectionChunk<ELFT>(C);
     }
   }
 }
@@ -177,9 +176,9 @@ template class Writer<ELF32BE>;
 template class Writer<ELF64LE>;
 template class Writer<ELF64BE>;
 
-template void OutputSection::addChunk<ELF32LE>(Chunk *);
-template void OutputSection::addChunk<ELF32BE>(Chunk *);
-template void OutputSection::addChunk<ELF64LE>(Chunk *);
-template void OutputSection::addChunk<ELF64BE>(Chunk *);
+template void OutputSection::addSectionChunk<ELF32LE>(SectionChunk<ELF32LE> *);
+template void OutputSection::addSectionChunk<ELF32BE>(SectionChunk<ELF32BE> *);
+template void OutputSection::addSectionChunk<ELF64LE>(SectionChunk<ELF64LE> *);
+template void OutputSection::addSectionChunk<ELF64BE>(SectionChunk<ELF64BE> *);
 }
 }

Modified: lld/trunk/ELF/Writer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.h?rev=244042&r1=244041&r2=244042&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.h (original)
+++ lld/trunk/ELF/Writer.h Wed Aug  5 08:55:34 2015
@@ -10,6 +10,7 @@
 #ifndef LLD_ELF_WRITER_H
 #define LLD_ELF_WRITER_H
 
+#include "Chunks.h"
 #include "SymbolTable.h"
 #include "llvm/Support/FileOutputBuffer.h"
 
@@ -25,7 +26,7 @@ public:
   OutputSection(StringRef Name) : Name(Name), Header({}) {}
   void setVA(uint64_t);
   void setFileOffset(uint64_t);
-  template <class ELFT> void addChunk(Chunk *C);
+  template <class ELFT> void addSectionChunk(SectionChunk<ELFT> *C);
   std::vector<Chunk *> &getChunks() { return Chunks; }
   template <class ELFT>
   void writeHeaderTo(llvm::object::Elf_Shdr_Impl<ELFT> *SHdr);




More information about the llvm-commits mailing list