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