/
SyntheticSections.h
282 lines (231 loc) · 9.06 KB
/
SyntheticSections.h
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//===- SyntheticSection.h ---------------------------------------*- C++ -*-===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_ELF_SYNTHETIC_SECTION_H
#define LLD_ELF_SYNTHETIC_SECTION_H
#include "InputSection.h"
#include "llvm/ADT/SmallPtrSet.h"
namespace lld {
namespace elf {
// .MIPS.abiflags section.
template <class ELFT>
class MipsAbiFlagsSection final : public InputSection<ELFT> {
typedef llvm::object::Elf_Mips_ABIFlags<ELFT> Elf_Mips_ABIFlags;
public:
MipsAbiFlagsSection();
private:
Elf_Mips_ABIFlags Flags = {};
};
// .MIPS.options section.
template <class ELFT>
class MipsOptionsSection final : public InputSection<ELFT> {
typedef llvm::object::Elf_Mips_Options<ELFT> Elf_Mips_Options;
typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
public:
MipsOptionsSection();
void finalize();
private:
std::vector<uint8_t> Buf;
Elf_Mips_Options *getOptions() {
return reinterpret_cast<Elf_Mips_Options *>(Buf.data());
}
};
// MIPS .reginfo section.
template <class ELFT>
class MipsReginfoSection final : public InputSection<ELFT> {
typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
public:
MipsReginfoSection();
void finalize();
private:
Elf_Mips_RegInfo Reginfo = {};
};
template <class ELFT> class SyntheticSection : public InputSection<ELFT> {
typedef typename ELFT::uint uintX_t;
public:
SyntheticSection(uintX_t Flags, uint32_t Type, uintX_t Addralign,
StringRef Name)
: InputSection<ELFT>(Flags, Type, Addralign, ArrayRef<uint8_t>(), Name,
InputSectionData::Synthetic) {
this->Live = true;
}
virtual void writeTo(uint8_t *Buf) = 0;
virtual size_t getSize() const { return this->Data.size(); }
uintX_t getVA() const {
return this->OutSec ? this->OutSec->Addr + this->OutSecOff : 0;
}
static bool classof(const InputSectionData *D) {
return D->kind() == InputSectionData::Synthetic;
}
protected:
~SyntheticSection() = default;
};
// .note.gnu.build-id section.
template <class ELFT> class BuildIdSection : public InputSection<ELFT> {
public:
virtual void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) = 0;
virtual ~BuildIdSection() = default;
uint8_t *getOutputLoc(uint8_t *Start) const;
protected:
BuildIdSection(size_t HashSize);
std::vector<uint8_t> Buf;
void
computeHash(llvm::MutableArrayRef<uint8_t> Buf,
std::function<void(ArrayRef<uint8_t> Arr, uint8_t *Hash)> Hash);
size_t HashSize;
// First 16 bytes are a header.
static const unsigned HeaderSize = 16;
};
template <class ELFT>
class BuildIdFastHash final : public BuildIdSection<ELFT> {
public:
BuildIdFastHash() : BuildIdSection<ELFT>(8) {}
void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
};
template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
public:
BuildIdMd5() : BuildIdSection<ELFT>(16) {}
void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
};
template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
public:
BuildIdSha1() : BuildIdSection<ELFT>(20) {}
void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
};
template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
public:
BuildIdUuid() : BuildIdSection<ELFT>(16) {}
void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
};
template <class ELFT>
class BuildIdHexstring final : public BuildIdSection<ELFT> {
public:
BuildIdHexstring();
void writeBuildId(llvm::MutableArrayRef<uint8_t>) override;
};
template <class ELFT> class GotSection final : public SyntheticSection<ELFT> {
typedef typename ELFT::uint uintX_t;
public:
GotSection();
void writeTo(uint8_t *Buf) override;
size_t getSize() const override { return Size; }
void finalize();
void addEntry(SymbolBody &Sym);
void addMipsEntry(SymbolBody &Sym, uintX_t Addend, RelExpr Expr);
bool addDynTlsEntry(SymbolBody &Sym);
bool addTlsIndex();
bool empty() const { return MipsPageEntries == 0 && Entries.empty(); }
uintX_t getMipsLocalPageOffset(uintX_t Addr);
uintX_t getMipsGotOffset(const SymbolBody &B, uintX_t Addend) const;
uintX_t getGlobalDynAddr(const SymbolBody &B) const;
uintX_t getGlobalDynOffset(const SymbolBody &B) const;
// Returns the symbol which corresponds to the first entry of the global part
// of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
// table properties.
// Returns nullptr if the global part is empty.
const SymbolBody *getMipsFirstGlobalEntry() const;
// Returns the number of entries in the local part of GOT including
// the number of reserved entries. This method is MIPS-specific.
unsigned getMipsLocalEntriesNum() const;
// Returns offset of TLS part of the MIPS GOT table. This part goes
// after 'local' and 'global' entries.
uintX_t getMipsTlsOffset() const;
uintX_t getTlsIndexVA() { return this->getVA() + TlsIndexOff; }
uint32_t getTlsIndexOff() const { return TlsIndexOff; }
// Flag to force GOT to be in output if we have relocations
// that relies on its address.
bool HasGotOffRel = false;
private:
std::vector<const SymbolBody *> Entries;
uint32_t TlsIndexOff = -1;
uint32_t MipsPageEntries = 0;
uintX_t Size = 0;
// Output sections referenced by MIPS GOT relocations.
llvm::SmallPtrSet<const OutputSectionBase *, 10> MipsOutSections;
llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
// MIPS ABI requires to create unique GOT entry for each Symbol/Addend
// pairs. The `MipsGotMap` maps (S,A) pair to the GOT index in the `MipsLocal`
// or `MipsGlobal` vectors. In general it does not have a sence to take in
// account addend for preemptible symbols because the corresponding
// GOT entries should have one-to-one mapping with dynamic symbols table.
// But we use the same container's types for both kind of GOT entries
// to handle them uniformly.
typedef std::pair<const SymbolBody *, uintX_t> MipsGotEntry;
typedef std::vector<MipsGotEntry> MipsGotEntries;
llvm::DenseMap<MipsGotEntry, size_t> MipsGotMap;
MipsGotEntries MipsLocal;
MipsGotEntries MipsLocal32;
MipsGotEntries MipsGlobal;
// Write MIPS-specific parts of the GOT.
void writeMipsGot(uint8_t *Buf);
};
template <class ELFT>
class GotPltSection final : public SyntheticSection<ELFT> {
typedef typename ELFT::uint uintX_t;
public:
GotPltSection();
void addEntry(SymbolBody &Sym);
bool empty() const;
size_t getSize() const override;
void writeTo(uint8_t *Buf) override;
private:
std::vector<const SymbolBody *> Entries;
};
template <class ELFT>
class StringTableSection final : public SyntheticSection<ELFT> {
public:
typedef typename ELFT::uint uintX_t;
StringTableSection(StringRef Name, bool Dynamic);
unsigned addString(StringRef S, bool HashIt = true);
void writeTo(uint8_t *Buf) override;
size_t getSize() const override { return Size; }
bool isDynamic() const { return Dynamic; }
private:
const bool Dynamic;
// ELF string tables start with a NUL byte, so 1.
uintX_t Size = 1;
llvm::DenseMap<StringRef, unsigned> StringMap;
std::vector<StringRef> Strings;
};
template <class ELFT> InputSection<ELFT> *createCommonSection();
template <class ELFT> InputSection<ELFT> *createInterpSection();
template <class ELFT> MergeInputSection<ELFT> *createCommentSection();
// Linker generated sections which can be used as inputs.
template <class ELFT> struct In {
static BuildIdSection<ELFT> *BuildId;
static InputSection<ELFT> *Common;
static StringTableSection<ELFT> *DynStrTab;
static GotSection<ELFT> *Got;
static GotPltSection<ELFT> *GotPlt;
static InputSection<ELFT> *Interp;
static MipsAbiFlagsSection<ELFT> *MipsAbiFlags;
static MipsOptionsSection<ELFT> *MipsOptions;
static MipsReginfoSection<ELFT> *MipsReginfo;
static StringTableSection<ELFT> *ShStrTab;
static StringTableSection<ELFT> *StrTab;
// Contains list of sections, which size is not known when
// createSections() is called. This list is used when output
// sections are being finalized to calculate their size correctly.
static std::vector<SyntheticSection<ELFT> *> SyntheticSections;
};
template <class ELFT> BuildIdSection<ELFT> *In<ELFT>::BuildId;
template <class ELFT> InputSection<ELFT> *In<ELFT>::Common;
template <class ELFT> StringTableSection<ELFT> *In<ELFT>::DynStrTab;
template <class ELFT> GotSection<ELFT> *In<ELFT>::Got;
template <class ELFT> GotPltSection<ELFT> *In<ELFT>::GotPlt;
template <class ELFT> InputSection<ELFT> *In<ELFT>::Interp;
template <class ELFT> MipsAbiFlagsSection<ELFT> *In<ELFT>::MipsAbiFlags;
template <class ELFT> MipsOptionsSection<ELFT> *In<ELFT>::MipsOptions;
template <class ELFT> MipsReginfoSection<ELFT> *In<ELFT>::MipsReginfo;
template <class ELFT> StringTableSection<ELFT> *In<ELFT>::ShStrTab;
template <class ELFT> StringTableSection<ELFT> *In<ELFT>::StrTab;
template <class ELFT>
std::vector<SyntheticSection<ELFT> *> In<ELFT>::SyntheticSections;
} // namespace elf
} // namespace lld
#endif