• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- ELFSegment.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 #ifndef MCLD_ELF_SEGMENT_H
10 #define MCLD_ELF_SEGMENT_H
11 #ifdef ENABLE_UNITTEST
12 #include <gtest.h>
13 #endif
14 #include <llvm/Support/ELF.h>
15 #include <llvm/Support/DataTypes.h>
16 #include <mcld/LD/LDSection.h>
17 #include <cassert>
18 #include <vector>
19 
20 namespace mcld
21 {
22 
23 /** \class ELFSegment
24  *  \brief decribe the program header for ELF executable or shared object
25  */
26 class ELFSegment
27 {
28 public:
29   typedef std::vector<LDSection*>::iterator sect_iterator;
30   typedef std::vector<LDSection*>::const_iterator const_sect_iterator;
31 public:
32   ELFSegment(uint32_t pType,
33              uint32_t pFlag = llvm::ELF::PF_R,
34              uint64_t pOffset = 0,
35              uint64_t pVaddr = 0,
36              uint64_t pPaddr = 0,
37              uint64_t pFilesz = 0,
38              uint64_t pMemsz = 0,
39              uint64_t pAlign = 0,
40              uint64_t pMaxSectAlign = 0);
41   ~ELFSegment();
42 
43   ///  -----  iterators  -----  ///
begin()44   sect_iterator       begin()       { return m_SectionList.begin(); }
begin()45   const_sect_iterator begin() const { return m_SectionList.begin(); }
end()46   sect_iterator       end()         { return m_SectionList.end(); }
end()47   const_sect_iterator end()   const { return m_SectionList.end(); }
48 
front()49   LDSection*       front()       { return m_SectionList.front(); }
front()50   const LDSection* front() const { return m_SectionList.front(); }
back()51   LDSection*       back()        { return m_SectionList.back(); }
back()52   const LDSection* back()  const { return m_SectionList.back(); }
53 
54   ///  -----  observers  -----  ///
type()55   uint32_t type() const
56   { return m_Type; }
57 
offset()58   uint64_t offset() const
59   { return m_Offset; }
60 
vaddr()61   uint64_t vaddr() const
62   { return m_Vaddr; }
63 
paddr()64   uint64_t paddr() const
65   { return m_Paddr; }
66 
filesz()67   uint64_t filesz() const
68   { return m_Filesz; }
69 
memsz()70   uint64_t memsz() const
71   { return m_Memsz; }
72 
flag()73   uint32_t flag() const
74   { return m_Flag; }
75 
align()76   uint64_t align() const
77   { return std::max(m_Align, m_MaxSectionAlign); }
78 
numOfSections()79   size_t numOfSections() const
80   { return m_SectionList.size(); }
81 
82   bool isDataSegment() const;
83 
84   bool isBssSegment() const;
85 
86   ///  -----  modifiers  -----  ///
setOffset(uint64_t pOffset)87   void setOffset(uint64_t pOffset)
88   { m_Offset = pOffset; }
89 
setVaddr(uint64_t pVaddr)90   void setVaddr(uint64_t pVaddr)
91   { m_Vaddr = pVaddr; }
92 
setPaddr(uint64_t pPaddr)93   void setPaddr(uint64_t pPaddr)
94   { m_Paddr = pPaddr; }
95 
setFilesz(uint64_t pFilesz)96   void setFilesz(uint64_t pFilesz)
97   { m_Filesz = pFilesz; }
98 
setMemsz(uint64_t pMemsz)99   void setMemsz(uint64_t pMemsz)
100   { m_Memsz = pMemsz; }
101 
setFlag(uint32_t pFlag)102   void setFlag(uint32_t pFlag)
103   { m_Flag = pFlag; }
104 
updateFlag(uint32_t pFlag)105   void updateFlag(uint32_t pFlag)
106   {
107     // PT_TLS segment should be PF_R
108     if (llvm::ELF::PT_TLS != m_Type)
109       m_Flag |= pFlag;
110   }
111 
setAlign(uint64_t pAlign)112   void setAlign(uint64_t pAlign)
113   { m_Align = pAlign; }
114 
addSection(LDSection * pSection)115   void addSection(LDSection* pSection)
116   {
117     assert(NULL != pSection);
118     if (pSection->align() > m_MaxSectionAlign)
119       m_MaxSectionAlign = pSection->align();
120     m_SectionList.push_back(pSection);
121   }
122 
123 private:
124   uint32_t m_Type;            // Type of segment
125   uint32_t m_Flag;            // Segment flags
126   uint64_t m_Offset;          // File offset where segment is located, in bytes
127   uint64_t m_Vaddr;           // Virtual address of the segment
128   uint64_t m_Paddr;           // Physical address of the segment (OS-specific)
129   uint64_t m_Filesz;          // # of bytes in file image of segment (may be 0)
130   uint64_t m_Memsz;           // # of bytes in mem image of segment (may be 0)
131   uint64_t m_Align;           // alignment constraint
132   uint64_t m_MaxSectionAlign; // max alignment of the sections in this segment
133   std::vector<LDSection*> m_SectionList;
134 };
135 
136 } // namespace of mcld
137 
138 #endif
139 
140