-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathChunks.h
259 lines (214 loc) · 8.22 KB
/
Chunks.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
//===- Chunks.h -----------------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLD_COFF_CHUNKS_H
#define LLD_COFF_CHUNKS_H
#include "lld/Core/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Object/COFF.h"
#include <map>
#include <vector>
namespace lld {
namespace coff {
using llvm::COFF::ImportDirectoryTableEntry;
using llvm::object::COFFSymbolRef;
using llvm::object::SectionRef;
using llvm::object::coff_relocation;
using llvm::object::coff_section;
using llvm::sys::fs::file_magic;
class Defined;
class DefinedImportData;
class ObjectFile;
class OutputSection;
// A Chunk represents a chunk of data that will occupy space in the
// output (if the resolver chose that). It may or may not be backed by
// a section of an input file. It could be linker-created data, or
// doesn't even have actual data (if common or bss).
class Chunk {
public:
virtual ~Chunk() {}
// Returns the pointer to data. It is illegal to call this function if
// this is a common or BSS chunk.
virtual const uint8_t *getData() const {
llvm_unreachable("unimplemented getData");
}
// Returns the size of this chunk (even if this is a common or BSS.)
virtual size_t getSize() const = 0;
// The writer sets and uses the addresses.
uint64_t getRVA() { return RVA; }
uint64_t getFileOff() { return FileOff; }
uint32_t getAlign() { return Align; }
void setRVA(uint64_t V) { RVA = V; }
void setFileOff(uint64_t V) { FileOff = V; }
// Applies relocations, assuming Buffer points to beginning of an
// mmap'ed output file. Because this function uses file offsets and
// RVA values of other chunks, you need to set them properly before
// calling this function.
virtual void applyRelocations(uint8_t *Buf) {}
// Returns true if getData() returns a valid pointer to data.
// BSS chunks return false. If false is returned, the space occupied
// by this chunk is filled with zeros.
virtual bool hasData() const { return true; }
// Returns readable/writable/executable bits.
virtual uint32_t getPermissions() const { return 0; }
// Returns the section name if this is a section chunk.
// It is illegal to call this function on non-section chunks.
virtual StringRef getSectionName() const {
llvm_unreachable("unimplemented getSectionName");
}
// Called if the garbage collector decides to not include this chunk
// in a final output. It's supposed to print out a log message to stderr.
// It is illegal to call this function on non-section chunks because
// only section chunks are subject of garbage collection.
virtual void printDiscardedMessage() {
llvm_unreachable("unimplemented printDiscardedMessage");
}
// Returns true if this is a COMDAT section. Usually, it is an error
// if there are more than one defined symbols having the same name,
// but symbols at begining of COMDAT sections allowed to duplicate.
virtual bool isCOMDAT() const { return false; }
// Used by the garbage collector.
virtual bool isRoot() { return false; }
virtual bool isLive() { return true; }
virtual void markLive() {}
// An output section has pointers to chunks in the section, and each
// chunk has a back pointer to an output section.
void setOutputSection(OutputSection *O) { Out = O; }
OutputSection *getOutputSection() { return Out; }
protected:
// The RVA of this chunk in the output. The writer sets a value.
uint64_t RVA = 0;
// The offset from beginning of the output file. The writer sets a value.
uint64_t FileOff = 0;
// The alignment of this chunk. The writer uses the value.
uint32_t Align = 1;
// The output section for this chunk.
OutputSection *Out = nullptr;
};
// A chunk corresponding a section of an input file.
class SectionChunk : public Chunk {
public:
SectionChunk(ObjectFile *File, const coff_section *Header,
uint32_t SectionIndex);
const uint8_t *getData() const override;
size_t getSize() const override { return Header->SizeOfRawData; }
void applyRelocations(uint8_t *Buf) override;
bool hasData() const override;
uint32_t getPermissions() const override;
StringRef getSectionName() const override { return SectionName; }
void printDiscardedMessage() override;
bool isCOMDAT() const override;
bool isRoot() override;
void markLive() override;
bool isLive() override { return Live; }
// Adds COMDAT associative sections to this COMDAT section. A chunk
// and its children are treated as a group by the garbage collector.
void addAssociative(SectionChunk *Child);
private:
SectionRef getSectionRef();
void applyReloc(uint8_t *Buf, const coff_relocation *Rel);
// A file this chunk was created from.
ObjectFile *File;
const coff_section *Header;
uint32_t SectionIndex;
StringRef SectionName;
bool Live = false;
std::vector<Chunk *> AssocChildren;
bool IsAssocChild = false;
};
// A chunk for common symbols. Common chunks don't have actual data.
class CommonChunk : public Chunk {
public:
CommonChunk(const COFFSymbolRef S) : Sym(S) {}
size_t getSize() const override { return Sym.getValue(); }
bool hasData() const override { return false; }
uint32_t getPermissions() const override;
StringRef getSectionName() const override { return ".bss"; }
private:
const COFFSymbolRef Sym;
};
// A chunk for linker-created strings.
class StringChunk : public Chunk {
public:
explicit StringChunk(StringRef S);
const uint8_t *getData() const override { return &Data[0]; }
size_t getSize() const override { return Data.size(); }
private:
std::vector<uint8_t> Data;
};
// All chunks below are for the DLL import descriptor table and
// Windows-specific. You may need to read the Microsoft PE/COFF spec
// to understand details about the data structures.
static const uint8_t ImportThunkData[] = {
0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
};
// A chunk for DLL import jump table entry. In a final output, it's
// contents will be a JMP instruction to some __imp_ symbol.
class ImportThunkChunk : public Chunk {
public:
explicit ImportThunkChunk(Defined *S) : ImpSymbol(S) {}
const uint8_t *getData() const override { return ImportThunkData; }
size_t getSize() const override { return sizeof(ImportThunkData); }
void applyRelocations(uint8_t *Buf) override;
private:
Defined *ImpSymbol;
};
// A chunk for the import descriptor table.
class HintNameChunk : public Chunk {
public:
explicit HintNameChunk(StringRef Name);
const uint8_t *getData() const override { return Data.data(); }
size_t getSize() const override { return Data.size(); }
private:
std::vector<uint8_t> Data;
};
// A chunk for the import descriptor table.
class LookupChunk : public Chunk {
public:
explicit LookupChunk(HintNameChunk *H) : HintName(H) {}
bool hasData() const override { return false; }
size_t getSize() const override { return sizeof(uint64_t); }
void applyRelocations(uint8_t *Buf) override;
HintNameChunk *HintName;
};
// A chunk for the import descriptor table.
class DirectoryChunk : public Chunk {
public:
explicit DirectoryChunk(StringChunk *N) : DLLName(N) {}
bool hasData() const override { return false; }
size_t getSize() const override { return sizeof(ImportDirectoryTableEntry); }
void applyRelocations(uint8_t *Buf) override;
StringChunk *DLLName;
LookupChunk *LookupTab;
LookupChunk *AddressTab;
};
// A chunk for the import descriptor table representing.
// Contents of this chunk is always null bytes.
// This is used for terminating import tables.
class NullChunk : public Chunk {
public:
explicit NullChunk(size_t N) : Size(N) {}
bool hasData() const override { return false; }
size_t getSize() const override { return Size; }
private:
size_t Size;
};
// ImportTable creates a set of import table chunks for a given
// DLL-imported symbols.
class ImportTable {
public:
ImportTable(StringRef DLLName, std::vector<DefinedImportData *> &Symbols);
StringChunk *DLLName;
DirectoryChunk *DirTab;
std::vector<LookupChunk *> LookupTables;
std::vector<LookupChunk *> AddressTables;
std::vector<HintNameChunk *> HintNameTables;
};
} // namespace coff
} // namespace lld
#endif