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