summaryrefslogtreecommitdiff
path: root/include/mcld/LD/LDSection.h
blob: 460c4792426581b4193a346aed89f4cf586fdad3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//===- LDSection.h --------------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef MCLD_LD_LDSECTION_H
#define MCLD_LD_LDSECTION_H
#ifdef ENABLE_UNITTEST
#include <gtest.h>
#endif

#include <llvm/MC/MCSection.h>
#include <llvm/MC/MCAssembler.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/DataTypes.h>
#include <mcld/LD/LDFileFormat.h>
#include <string>

namespace llvm {

class MCAsmInfo;
class raw_ostream;

} // namespace of llvm

namespace mcld {
/** \class LDSection
 *  \brief LDSection represents a section header entry. It is a unified
 *  abstraction for various file formats.
 *
 *  LDSection contains both the format-dependent data and LLVM specific data.
 *
 */
class LDSection : public llvm::MCSection
{
public:
  LDSection(const std::string& pName,
            LDFileFormat::Kind pKind,
            uint32_t pType,
            uint32_t pFlag,
            uint64_t pSize = 0,
            uint64_t pOffset = 0,
            uint64_t pAddr = 0);

  /// name - the name of this section.
  const std::string& name() const
  { return m_Name; }

  /// kind - the kind of this section, such as Text, BSS, GOT, and so on.
  /// from LDFileFormat::Kind
  LDFileFormat::Kind kind() const
  { return m_Kind; }

  /// type - The categorizes the section's contents and semantics. It's
  /// different from llvm::SectionKind. Type is format-dependent, but
  /// llvm::SectionKind is format independent and is used for bit-code.
  ///   In ELF, it is sh_type
  ///   In MachO, it's type field of struct section::flags
  uint32_t type() const
  { return m_Type; }

  /// flag - An integer describes miscellaneous attributes.
  ///   In ELF, it is sh_flags.
  ///   In MachO, it's attribute field of struct section::flags
  uint32_t flag() const
  { return m_Flag; }

  /// size - An integer specifying the size in bytes of the virtual memory
  /// occupied by this section.
  ///   In ELF, if the type() is SHT_NOBITS, this function return zero.
  ///   Before layouting, output's LDSection::size() should return zero.
  uint64_t size() const
  { return m_Size; }

  /// offset - An integer specifying the offset of this section in the file.
  ///   Before layouting, output's LDSection::offset() should return zero.
  uint64_t offset() const
  { return m_Offset; }

  /// addr - An integer specifying the virtual address of this section in the
  /// virtual image.
  ///   Before layouting, output's LDSection::offset() should return zero.
  ///   ELF uses sh_addralign to set alignment constraints. In LLVM, alignment
  ///   constraint is set in MCSectionData::setAlignment. addr() contains the
  ///   original ELF::sh_addr. Modulo sh_addr by sh_addralign is not necessary.
  ///   MachO uses the same scenario.
  ///
  ///   Because addr() in output is changing during linking, MCLinker does not
  ///   store the address of the output here. The address is in Layout
  uint64_t addr() const
  { return m_Addr; }

  /// align - An integer specifying the align of this section in the file.
  ///   Before layouting, output's LDSection::align() should return zero.
  uint32_t align() const
  { return m_Align; }

  size_t index() const
  { return m_Index; }

  /// getLink - return the Link. When a section A needs the other section B
  /// during linking or loading, we say B is A's Link section.
  /// In ELF, InfoLink section control the ElfNN_Shdr::sh_link and sh_info.
  ///
  /// @return if the section needs no other sections, return NULL
  LDSection* getLink()
  { return m_pLink; }

  const LDSection* getLink() const
  { return m_pLink; }

  size_t getInfo() const
  { return m_Info; }

  void setKind(LDFileFormat::Kind pKind)
  { m_Kind = pKind; }

  void setSize(uint64_t size)
  { m_Size = size; }

  void setOffset(uint64_t Offset)
  { m_Offset = Offset; }

  void setAddr(uint64_t addr)
  { m_Addr = addr; }

  void setAlign(uint32_t align)
  { m_Align = align; }

  void setFlag(uint32_t flag)
  { m_Flag = flag; }

  void setType(uint32_t type)
  { m_Type = type; }

  static bool classof(const MCSection *S)
  { return S->getVariant() == SV_LDContext; }

  static bool classof(const LDSection *)
  { return true; }

  // -----  methods for adapt to llvm::MCSection  ----- //
  void PrintSwitchToSection(const llvm::MCAsmInfo &MAI,
                            llvm::raw_ostream &OS) const
  { }

  bool UseCodeAlign() const
  { return true; }

  bool isVirtualSection() const
  { return false; }

  llvm::MCSectionData* getSectionData()
  { return m_pSectionData; }

  const llvm::MCSectionData* getSectionData() const
  { return m_pSectionData; }

  void setSectionData(llvm::MCSectionData* pSD)
  { m_pSectionData = pSD; }

  bool hasSectionData() const
  { return (NULL != m_pSectionData); }

  /// setLink - set the sections should link with.
  /// if pLink is NULL, no Link section is set.
  void setLink(LDSection* pLink)
  { m_pLink = pLink; }

  void setInfo(size_t pInfo)
  { m_Info = pInfo; }

  void setIndex(size_t pIndex)
  { m_Index = pIndex; }

private:
  std::string m_Name;
  LDFileFormat::Kind m_Kind;
  uint32_t m_Type;
  uint32_t m_Flag;

  uint64_t m_Size;
  uint64_t m_Offset;
  uint64_t m_Addr;
  uint32_t m_Align;

  size_t m_Info;
  LDSection* m_pLink;

  // pointer to MCSectionData.
  llvm::MCSectionData* m_pSectionData;

  // the index of the file
  size_t m_Index;

}; // end of LDSection

} // end namespace mcld

#endif