Home | History | Annotate | Download | only in LD
      1 //===- LDSection.h --------------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 #ifndef MCLD_LD_LDSECTION_H
     11 #define MCLD_LD_LDSECTION_H
     12 
     13 #include <llvm/Support/DataTypes.h>
     14 #include <mcld/Support/Allocators.h>
     15 #include <mcld/Config/Config.h>
     16 #include <mcld/LD/LDFileFormat.h>
     17 #include <string>
     18 
     19 namespace mcld {
     20 
     21 class SectionData;
     22 class RelocData;
     23 class EhFrame;
     24 
     25 /** \class LDSection
     26  *  \brief LDSection represents a section header entry. It is a unified
     27  *  abstraction of a section header entry for various file formats.
     28  */
     29 class LDSection
     30 {
     31 private:
     32   friend class Chunk<LDSection, MCLD_SECTIONS_PER_INPUT>;
     33 
     34   LDSection();
     35 
     36   LDSection(const std::string& pName,
     37             LDFileFormat::Kind pKind,
     38             uint32_t pType,
     39             uint32_t pFlag,
     40             uint64_t pSize = 0,
     41             uint64_t pAddr = 0);
     42 
     43 public:
     44   ~LDSection();
     45 
     46   static LDSection* Create(const std::string& pName,
     47                            LDFileFormat::Kind pKind,
     48                            uint32_t pType,
     49                            uint32_t pFlag,
     50                            uint64_t pSize = 0,
     51                            uint64_t pAddr = 0);
     52 
     53   static void Destroy(LDSection*& pSection);
     54 
     55   static void Clear();
     56 
     57   bool hasOffset() const;
     58 
     59   /// name - the name of this section.
     60   const std::string& name() const
     61   { return m_Name; }
     62 
     63   /// kind - the kind of this section, such as Text, BSS, GOT, and so on.
     64   /// from LDFileFormat::Kind
     65   LDFileFormat::Kind kind() const
     66   { return m_Kind; }
     67 
     68   /// type - The categorizes the section's contents and semantics. It's
     69   /// different from llvm::SectionKind. Type is format-dependent, but
     70   /// llvm::SectionKind is format independent and is used for bit-code.
     71   ///   In ELF, it is sh_type
     72   ///   In MachO, it's type field of struct section::flags
     73   uint32_t type() const
     74   { return m_Type; }
     75 
     76   /// flag - An integer describes miscellaneous attributes.
     77   ///   In ELF, it is sh_flags.
     78   ///   In MachO, it's attribute field of struct section::flags
     79   uint32_t flag() const
     80   { return m_Flag; }
     81 
     82   /// size - An integer specifying the size in bytes of the virtual memory
     83   /// occupied by this section.
     84   ///   In ELF, if the type() is SHT_NOBITS, this function return zero.
     85   ///   Before layouting, output's LDSection::size() should return zero.
     86   uint64_t size() const
     87   { return m_Size; }
     88 
     89   /// offset - An integer specifying the offset of this section in the file.
     90   ///   Before layouting, output's LDSection::offset() should return zero.
     91   uint64_t offset() const
     92   { return m_Offset; }
     93 
     94   /// addr - An integer specifying the virtual address of this section in the
     95   /// virtual image.
     96   ///   Before layouting, output's LDSection::offset() should return zero.
     97   ///   ELF uses sh_addralign to set alignment constraints. In LLVM, alignment
     98   ///   constraint is set in SectionData::setAlignment. addr() contains the
     99   ///   original ELF::sh_addr. Modulo sh_addr by sh_addralign is not necessary.
    100   ///   MachO uses the same scenario.
    101   ///
    102   ///   Because addr() in output is changing during linking, MCLinker does not
    103   ///   store the address of the output here. The address is in Layout
    104   uint64_t addr() const
    105   { return m_Addr; }
    106 
    107   /// align - An integer specifying the align of this section in the file.
    108   ///   Before layouting, output's LDSection::align() should return zero.
    109   uint32_t align() const
    110   { return m_Align; }
    111 
    112   size_t index() const
    113   { return m_Index; }
    114 
    115   /// getLink - return the Link. When a section A needs the other section B
    116   /// during linking or loading, we say B is A's Link section.
    117   /// In ELF, InfoLink section control the ElfNN_Shdr::sh_link and sh_info.
    118   ///
    119   /// @return if the section needs no other sections, return NULL
    120   LDSection* getLink()
    121   { return m_pLink; }
    122 
    123   const LDSection* getLink() const
    124   { return m_pLink; }
    125 
    126   size_t getInfo() const
    127   { return m_Info; }
    128 
    129   void setKind(LDFileFormat::Kind pKind)
    130   { m_Kind = pKind; }
    131 
    132   void setSize(uint64_t size)
    133   { m_Size = size; }
    134 
    135   void setOffset(uint64_t Offset)
    136   { m_Offset = Offset; }
    137 
    138   void setAddr(uint64_t addr)
    139   { m_Addr = addr; }
    140 
    141   void setAlign(uint32_t align)
    142   { m_Align = align; }
    143 
    144   void setFlag(uint32_t flag)
    145   { m_Flag = flag; }
    146 
    147   void setType(uint32_t type)
    148   { m_Type = type; }
    149 
    150   // -----  SectionData  ----- //
    151   const SectionData* getSectionData() const { return m_Data.sect_data; }
    152   SectionData*       getSectionData()       { return m_Data.sect_data; }
    153 
    154   void setSectionData(SectionData* pSD) { m_Data.sect_data = pSD; }
    155 
    156   bool hasSectionData() const;
    157 
    158   // ------  RelocData  ------ //
    159   const RelocData* getRelocData() const { return m_Data.reloc_data; }
    160   RelocData*       getRelocData()       { return m_Data.reloc_data; }
    161 
    162   void setRelocData(RelocData* pRD) { m_Data.reloc_data = pRD; }
    163 
    164   bool hasRelocData() const;
    165 
    166   // ------  EhFrame  ------ //
    167   const EhFrame* getEhFrame() const { return m_Data.eh_frame; }
    168   EhFrame*       getEhFrame()       { return m_Data.eh_frame; }
    169 
    170   void setEhFrame(EhFrame* pEhFrame) { m_Data.eh_frame = pEhFrame; }
    171 
    172   bool hasEhFrame() const;
    173 
    174   /// setLink - set the sections should link with.
    175   /// if pLink is NULL, no Link section is set.
    176   void setLink(LDSection* pLink)
    177   { m_pLink = pLink; }
    178 
    179   void setInfo(size_t pInfo)
    180   { m_Info = pInfo; }
    181 
    182   void setIndex(size_t pIndex)
    183   { m_Index = pIndex; }
    184 
    185 private:
    186   union SectOrRelocData
    187   {
    188     SectionData* sect_data;
    189     RelocData*   reloc_data;
    190     EhFrame*     eh_frame;
    191   };
    192 
    193 private:
    194   std::string m_Name;
    195 
    196   LDFileFormat::Kind m_Kind;
    197   uint32_t m_Type;
    198   uint32_t m_Flag;
    199 
    200   uint64_t m_Size;
    201   uint64_t m_Offset;
    202   uint64_t m_Addr;
    203   uint32_t m_Align;
    204 
    205   size_t m_Info;
    206   LDSection* m_pLink;
    207 
    208   /// m_Data - the SectionData or RelocData of this section
    209   SectOrRelocData m_Data;
    210 
    211   /// m_Index - the index of the file
    212   size_t m_Index;
    213 
    214 }; // end of LDSection
    215 
    216 } // end namespace mcld
    217 
    218 #endif
    219 
    220