[lld] r222311 - [ELF] Create input and output section names

Shankar Easwaran shankare at codeaurora.org
Tue Nov 18 19:51:46 PST 2014


Author: shankare
Date: Tue Nov 18 21:51:45 2014
New Revision: 222311

URL: http://llvm.org/viewvc/llvm-project?rev=222311&view=rev
Log:
[ELF] Create input and output section names

No change in functionality.

Modified:
    lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h
    lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h
    lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h
    lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h
    lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h
    lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h

Modified: lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h?rev=222311&r1=222310&r2=222311&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/DefaultLayout.h Tue Nov 18 21:51:45 2014
@@ -175,8 +175,14 @@ public:
   SectionOrder getSectionOrder(StringRef name, int32_t contentType,
                                int32_t contentPermissions) override;
 
-  /// \brief This maps the input sections to the output section names
-  virtual StringRef getSectionName(const DefinedAtom *da) const;
+  /// \brief Return the name of the input section by decoding the input
+  /// sectionChoice.
+  virtual StringRef getInputSectionName(const DefinedAtom *da) const;
+
+  /// \brief Return the name of the output section from the input section and
+  /// path.
+  virtual StringRef getOutputSectionName(StringRef inputSectionName,
+                                         StringRef path) const;
 
   /// \brief Gets or creates a section.
   AtomSection<ELFT> *
@@ -402,7 +408,8 @@ Layout::SectionOrder DefaultLayout<ELFT>
 
 /// \brief This maps the input sections to the output section names
 template <class ELFT>
-StringRef DefaultLayout<ELFT>::getSectionName(const DefinedAtom *da) const {
+StringRef
+DefaultLayout<ELFT>::getInputSectionName(const DefinedAtom *da) const {
   if (da->sectionChoice() == DefinedAtom::sectionBasedOnContent) {
     switch (da->contentType()) {
     case DefinedAtom::typeCode:
@@ -421,7 +428,15 @@ StringRef DefaultLayout<ELFT>::getSectio
       break;
     }
   }
-  return llvm::StringSwitch<StringRef>(da->customSectionName())
+  return da->customSectionName();
+}
+
+/// \brief This maps the input sections to the output section names
+template <class ELFT>
+StringRef
+DefaultLayout<ELFT>::getOutputSectionName(StringRef inputSectionName,
+                                          StringRef /* path */) const {
+  return llvm::StringSwitch<StringRef>(inputSectionName)
       .StartsWith(".text", ".text")
       .StartsWith(".rodata", ".rodata")
       .StartsWith(".gcc_except_table", ".gcc_except_table")
@@ -432,7 +447,7 @@ StringRef DefaultLayout<ELFT>::getSectio
       .StartsWith(".tbss", ".tbss")
       .StartsWith(".init_array", ".init_array")
       .StartsWith(".fini_array", ".fini_array")
-      .Default(da->customSectionName());
+      .Default(inputSectionName);
 }
 
 /// \brief Gets the segment for a output section
@@ -547,6 +562,7 @@ DefaultLayout<ELFT>::getSection(StringRe
       getSectionOrder(sectionName, contentType, permissions);
   AtomSection<ELFT> *newSec =
       createSection(sectionName, contentType, permissions, sectionOrder);
+  newSec->setOutputSectionName(getOutputSectionName(sectionName, path));
   newSec->setOrder(sectionOrder);
   _sections.push_back(newSec);
   _sectionMap.insert(std::make_pair(sectionKey, newSec));
@@ -565,7 +581,7 @@ ErrorOr<const lld::AtomLayout &> Default
         definedAtom->permissions();
     const DefinedAtom::ContentType contentType = definedAtom->contentType();
 
-    StringRef sectionName = getSectionName(definedAtom);
+    StringRef sectionName = getInputSectionName(definedAtom);
     AtomSection<ELFT> *section = getSection(
         sectionName, contentType, permissions, definedAtom->file().path());
 
@@ -612,15 +628,18 @@ template <class ELFT> void DefaultLayout
   OutputSection<ELFT> *outputSection;
 
   for (auto &si : _sections) {
+    Section<ELFT> *section = dyn_cast<Section<ELFT>>(si);
+    if (!section)
+      continue;
     const std::pair<StringRef, OutputSection<ELFT> *> currentOutputSection(
-        si->name(), nullptr);
+        section->outputSectionName(), nullptr);
     std::pair<typename OutputSectionMapT::iterator, bool> outputSectionInsert(
         _outputSectionMap.insert(currentOutputSection));
     if (!outputSectionInsert.second) {
       outputSection = outputSectionInsert.first->second;
     } else {
       outputSection = new (_allocator.Allocate<OutputSection<ELFT>>())
-          OutputSection<ELFT>(si->name());
+          OutputSection<ELFT>(section->outputSectionName());
       _outputSections.push_back(outputSection);
       outputSectionInsert.first->second = outputSection;
     }

Modified: lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h?rev=222311&r1=222310&r2=222311&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonTargetHandler.h Tue Nov 18 21:51:45 2014
@@ -46,8 +46,8 @@ public:
                                                           contentPermissions);
   }
 
-  /// \brief This maps the input sections to the output section names
-  virtual StringRef getSectionName(const DefinedAtom *da) const {
+  /// \brief Return the appropriate input section name.
+  virtual StringRef getInputSectionName(const DefinedAtom *da) const {
     switch (da->contentType()) {
     case DefinedAtom::typeDataFast:
     case DefinedAtom::typeZeroFillFast:
@@ -55,7 +55,7 @@ public:
     default:
       break;
     }
-    return DefaultLayout<HexagonELFType>::getSectionName(da);
+    return DefaultLayout<HexagonELFType>::getInputSectionName(da);
   }
 
   /// \brief Gets or creates a section.

Modified: lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h?rev=222311&r1=222310&r2=222311&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.h Tue Nov 18 21:51:45 2014
@@ -45,11 +45,11 @@ public:
                                                  order);
   }
 
-  StringRef getSectionName(const DefinedAtom *da) const override {
+  StringRef getInputSectionName(const DefinedAtom *da) const override {
     return llvm::StringSwitch<StringRef>(da->customSectionName())
         .StartsWith(".ctors", ".ctors")
         .StartsWith(".dtors", ".dtors")
-        .Default(TargetLayout<ELFType>::getSectionName(da));
+        .Default(TargetLayout<ELFType>::getInputSectionName(da));
   }
 
   Layout::SegmentType getSegmentType(Section<ELFType> *section) const override {

Modified: lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h?rev=222311&r1=222310&r2=222311&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/OutputELFWriter.h Tue Nov 18 21:51:45 2014
@@ -302,7 +302,9 @@ template <class ELFT> void OutputELFWrit
   _layout.addSection(_shdrtab.get());
 
   for (auto sec : _layout.sections()) {
-    if (sec->name() != ".eh_frame")
+    // TODO: use findOutputSection
+    auto section = dyn_cast<Section<ELFT>>(sec);
+    if (!section || section->outputSectionName() != ".eh_frame")
       continue;
     _ehFrameHeader.reset(new (_alloc) EHFrameHeader<ELFT>(
         _context, ".eh_frame_hdr", _layout,

Modified: lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h?rev=222311&r1=222310&r2=222311&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/SectionChunks.h Tue Nov 18 21:51:45 2014
@@ -38,11 +38,13 @@ template <class ELFT> class Segment;
 /// \brief An ELF section.
 template <class ELFT> class Section : public Chunk<ELFT> {
 public:
-  Section(const ELFLinkingContext &context, StringRef name,
+  Section(const ELFLinkingContext &context, StringRef sectionName,
+          StringRef chunkName,
           typename Chunk<ELFT>::Kind k = Chunk<ELFT>::Kind::ELFSection)
-      : Chunk<ELFT>(name, k, context), _outputSection(nullptr), _flags(0),
+      : Chunk<ELFT>(chunkName, k, context), _outputSection(nullptr), _flags(0),
         _entSize(0), _type(0), _link(0), _info(0),
-        _isFirstSectionInOutputSection(false), _segmentType(SHT_NULL) {}
+        _isFirstSectionInOutputSection(false), _segmentType(SHT_NULL),
+        _inputSectionName(sectionName), _outputSectionName(sectionName) {}
 
   /// \brief Modify the section contents before assigning virtual addresses
   //  or assigning file offsets
@@ -110,6 +112,14 @@ public:
                                           : this->_align2;
   }
 
+  virtual StringRef inputSectionName() const { return _inputSectionName; }
+
+  virtual StringRef outputSectionName() const { return _outputSectionName; }
+
+  virtual void setOutputSectionName(StringRef outputSectionName) {
+    _outputSectionName = outputSectionName;
+  }
+
 protected:
   /// \brief OutputSection this Section is a member of, or nullptr.
   OutputSection<ELFT> *_outputSection;
@@ -127,14 +137,19 @@ protected:
   bool _isFirstSectionInOutputSection;
   /// \brief the output ELF segment type of this section.
   Layout::SegmentType _segmentType;
+  /// \brief Input section name.
+  StringRef _inputSectionName;
+  /// \brief Output section name.
+  StringRef _outputSectionName;
 };
 
 /// \brief A section containing atoms.
 template <class ELFT> class AtomSection : public Section<ELFT> {
 public:
-  AtomSection(const ELFLinkingContext &context, StringRef name,
+  AtomSection(const ELFLinkingContext &context, StringRef sectionName,
               int32_t contentType, int32_t permissions, int32_t order)
-      : Section<ELFT>(context, name, Chunk<ELFT>::Kind::AtomSection),
+      : Section<ELFT>(context, sectionName, "AtomSection",
+                      Chunk<ELFT>::Kind::AtomSection),
         _contentType(contentType), _contentPermissions(permissions),
         _isLoadedInMemory(true) {
     this->setOrder(order);
@@ -555,7 +570,7 @@ private:
 template <class ELFT>
 StringTable<ELFT>::StringTable(const ELFLinkingContext &context,
                                const char *str, int32_t order, bool dynamic)
-    : Section<ELFT>(context, str) {
+    : Section<ELFT>(context, str, "StringTable") {
   // the string table has a NULL entry for which
   // add an empty string
   _strings.push_back("");
@@ -679,7 +694,7 @@ protected:
 template <class ELFT>
 SymbolTable<ELFT>::SymbolTable(const ELFLinkingContext &context,
                                const char *str, int32_t order)
-    : Section<ELFT>(context, str) {
+    : Section<ELFT>(context, str, "SymbolTable") {
   this->setOrder(order);
   Elf_Sym symbol;
   std::memset(&symbol, 0, sizeof(Elf_Sym));
@@ -911,7 +926,7 @@ public:
 
   RelocationTable(const ELFLinkingContext &context, StringRef str,
                   int32_t order)
-      : Section<ELFT>(context, str), _symbolTable(nullptr) {
+      : Section<ELFT>(context, str, "RelocationTable"), _symbolTable(nullptr) {
     this->setOrder(order);
     this->_flags = SHF_ALLOC;
     // Set the alignment properly depending on the target architecture
@@ -1027,7 +1042,7 @@ public:
 
   DynamicTable(const ELFLinkingContext &context, TargetLayout<ELFT> &layout,
                StringRef str, int32_t order)
-      : Section<ELFT>(context, str), _layout(layout) {
+      : Section<ELFT>(context, str, "DynamicSection"), _layout(layout) {
     this->setOrder(order);
     this->_entSize = sizeof(Elf_Dyn);
     this->_align2 = ELFT::Is64Bits ? 8 : 4;
@@ -1210,7 +1225,7 @@ template <class ELFT> class InterpSectio
 public:
   InterpSection(const ELFLinkingContext &context, StringRef str, int32_t order,
                 StringRef interp)
-      : Section<ELFT>(context, str), _interp(interp) {
+      : Section<ELFT>(context, str, "Dynamic:Interp"), _interp(interp) {
     this->setOrder(order);
     this->_align2 = 1;
     // + 1 for null term.
@@ -1259,7 +1274,7 @@ template <class ELFT> class HashSection
 
 public:
   HashSection(const ELFLinkingContext &context, StringRef name, int32_t order)
-      : Section<ELFT>(context, name), _symbolTable(nullptr) {
+      : Section<ELFT>(context, name, "Dynamic:Hash"), _symbolTable(nullptr) {
     this->setOrder(order);
     this->_entSize = 4;
     this->_type = SHT_HASH;
@@ -1363,7 +1378,7 @@ template <class ELFT> class EHFrameHeade
 public:
   EHFrameHeader(const ELFLinkingContext &context, StringRef name,
                 TargetLayout<ELFT> &layout, int32_t order)
-      : Section<ELFT>(context, name), _layout(layout) {
+      : Section<ELFT>(context, name, "EHFrameHeader"), _layout(layout) {
     this->setOrder(order);
     this->_entSize = 0;
     this->_type = SHT_PROGBITS;

Modified: lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h?rev=222311&r1=222310&r2=222311&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h (original)
+++ lld/trunk/lib/ReaderWriter/ELF/SegmentChunks.h Tue Nov 18 21:51:45 2014
@@ -432,7 +432,6 @@ void Segment<ELFT>::assignFileOffsets(ui
         isDataPageAlignedForNMagic = true;
       } else
         fileOffset = llvm::RoundUpToAlignment(fileOffset, section->align2());
-
       if (isFirstSection) {
         slice->setFileOffset(fileOffset);
         isFirstSection = false;
@@ -524,7 +523,7 @@ template <class ELFT> void Segment<ELFT>
       }
       uint64_t newAddr = llvm::RoundUpToAlignment(curAddr, (*si)->align2());
       // If the newAddress computed is more than a page away, let's create
-      // a separate segment, so that memory is not used up while running
+      // a separate segment, so that memory is not used up while running.
       if (((newAddr - curAddr) > this->_context.getPageSize()) &&
           (_outputMagic != ELFLinkingContext::OutputMagic::NMAGIC &&
            _outputMagic != ELFLinkingContext::OutputMagic::OMAGIC)) {





More information about the llvm-commits mailing list