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#ifdef ENABLE_UNITTEST
13#include <gtest.h>
14#endif
15
16#include <llvm/ADT/StringRef.h>
17#include <llvm/Support/DataTypes.h>
18#include <mcld/LD/LDFileFormat.h>
19#include <string>
20
21namespace mcld {
22
23class SectionData;
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 */
29class LDSection
30{
31public:
32  LDSection(const std::string& pName,
33            LDFileFormat::Kind pKind,
34            uint32_t pType,
35            uint32_t pFlag,
36            uint64_t pSize = 0,
37            uint64_t pOffset = 0,
38            uint64_t pAddr = 0);
39
40  /// name - the name of this section.
41  const std::string& name() const
42  { return m_Name; }
43
44  /// kind - the kind of this section, such as Text, BSS, GOT, and so on.
45  /// from LDFileFormat::Kind
46  LDFileFormat::Kind kind() const
47  { return m_Kind; }
48
49  /// type - The categorizes the section's contents and semantics. It's
50  /// different from llvm::SectionKind. Type is format-dependent, but
51  /// llvm::SectionKind is format independent and is used for bit-code.
52  ///   In ELF, it is sh_type
53  ///   In MachO, it's type field of struct section::flags
54  uint32_t type() const
55  { return m_Type; }
56
57  /// flag - An integer describes miscellaneous attributes.
58  ///   In ELF, it is sh_flags.
59  ///   In MachO, it's attribute field of struct section::flags
60  uint32_t flag() const
61  { return m_Flag; }
62
63  /// size - An integer specifying the size in bytes of the virtual memory
64  /// occupied by this section.
65  ///   In ELF, if the type() is SHT_NOBITS, this function return zero.
66  ///   Before layouting, output's LDSection::size() should return zero.
67  uint64_t size() const
68  { return m_Size; }
69
70  /// offset - An integer specifying the offset of this section in the file.
71  ///   Before layouting, output's LDSection::offset() should return zero.
72  uint64_t offset() const
73  { return m_Offset; }
74
75  /// addr - An integer specifying the virtual address of this section in the
76  /// virtual image.
77  ///   Before layouting, output's LDSection::offset() should return zero.
78  ///   ELF uses sh_addralign to set alignment constraints. In LLVM, alignment
79  ///   constraint is set in SectionData::setAlignment. addr() contains the
80  ///   original ELF::sh_addr. Modulo sh_addr by sh_addralign is not necessary.
81  ///   MachO uses the same scenario.
82  ///
83  ///   Because addr() in output is changing during linking, MCLinker does not
84  ///   store the address of the output here. The address is in Layout
85  uint64_t addr() const
86  { return m_Addr; }
87
88  /// align - An integer specifying the align of this section in the file.
89  ///   Before layouting, output's LDSection::align() should return zero.
90  uint32_t align() const
91  { return m_Align; }
92
93  size_t index() const
94  { return m_Index; }
95
96  /// getLink - return the Link. When a section A needs the other section B
97  /// during linking or loading, we say B is A's Link section.
98  /// In ELF, InfoLink section control the ElfNN_Shdr::sh_link and sh_info.
99  ///
100  /// @return if the section needs no other sections, return NULL
101  LDSection* getLink()
102  { return m_pLink; }
103
104  const LDSection* getLink() const
105  { return m_pLink; }
106
107  size_t getInfo() const
108  { return m_Info; }
109
110  void setKind(LDFileFormat::Kind pKind)
111  { m_Kind = pKind; }
112
113  void setSize(uint64_t size)
114  { m_Size = size; }
115
116  void setOffset(uint64_t Offset)
117  { m_Offset = Offset; }
118
119  void setAddr(uint64_t addr)
120  { m_Addr = addr; }
121
122  void setAlign(uint32_t align)
123  { m_Align = align; }
124
125  void setFlag(uint32_t flag)
126  { m_Flag = flag; }
127
128  void setType(uint32_t type)
129  { m_Type = type; }
130
131  SectionData* getSectionData()
132  { return m_pSectionData; }
133
134  const SectionData* getSectionData() const
135  { return m_pSectionData; }
136
137  void setSectionData(SectionData* pSD)
138  { m_pSectionData = pSD; }
139
140  bool hasSectionData() const
141  { return (NULL != m_pSectionData); }
142
143  /// setLink - set the sections should link with.
144  /// if pLink is NULL, no Link section is set.
145  void setLink(LDSection* pLink)
146  { m_pLink = pLink; }
147
148  void setInfo(size_t pInfo)
149  { m_Info = pInfo; }
150
151  void setIndex(size_t pIndex)
152  { m_Index = pIndex; }
153
154private:
155  std::string m_Name;
156  LDFileFormat::Kind m_Kind;
157  uint32_t m_Type;
158  uint32_t m_Flag;
159
160  uint64_t m_Size;
161  uint64_t m_Offset;
162  uint64_t m_Addr;
163  uint32_t m_Align;
164
165  size_t m_Info;
166  LDSection* m_pLink;
167
168  SectionData* m_pSectionData;
169
170  // the index of the file
171  size_t m_Index;
172
173}; // end of LDSection
174
175} // end namespace mcld
176
177#endif
178
179