]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/Chunks.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r308421, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / Chunks.h
1 //===- Chunks.h -------------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
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 LLD_COFF_CHUNKS_H
11 #define LLD_COFF_CHUNKS_H
12
13 #include "Config.h"
14 #include "InputFiles.h"
15 #include "lld/Core/LLVM.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/iterator.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Object/COFF.h"
20 #include <utility>
21 #include <vector>
22
23 namespace lld {
24 namespace coff {
25
26 using llvm::COFF::ImportDirectoryTableEntry;
27 using llvm::object::COFFSymbolRef;
28 using llvm::object::SectionRef;
29 using llvm::object::coff_relocation;
30 using llvm::object::coff_section;
31
32 class Baserel;
33 class Defined;
34 class DefinedImportData;
35 class DefinedRegular;
36 class ObjectFile;
37 class OutputSection;
38 class SymbolBody;
39
40 // Mask for section types (code, data, bss, disacardable, etc.)
41 // and permissions (writable, readable or executable).
42 const uint32_t PermMask = 0xFF0000F0;
43
44 // A Chunk represents a chunk of data that will occupy space in the
45 // output (if the resolver chose that). It may or may not be backed by
46 // a section of an input file. It could be linker-created data, or
47 // doesn't even have actual data (if common or bss).
48 class Chunk {
49 public:
50   enum Kind { SectionKind, OtherKind };
51   Kind kind() const { return ChunkKind; }
52   virtual ~Chunk() = default;
53
54   // Returns the size of this chunk (even if this is a common or BSS.)
55   virtual size_t getSize() const = 0;
56
57   // Write this chunk to a mmap'ed file, assuming Buf is pointing to
58   // beginning of the file. Because this function may use RVA values
59   // of other chunks for relocations, you need to set them properly
60   // before calling this function.
61   virtual void writeTo(uint8_t *Buf) const {}
62
63   // The writer sets and uses the addresses.
64   uint64_t getRVA() const { return RVA; }
65   uint32_t getAlign() const { return Align; }
66   void setRVA(uint64_t V) { RVA = V; }
67
68   // Returns true if this has non-zero data. BSS chunks return
69   // false. If false is returned, the space occupied by this chunk
70   // will be filled with zeros.
71   virtual bool hasData() const { return true; }
72
73   // Returns readable/writable/executable bits.
74   virtual uint32_t getPermissions() const { return 0; }
75
76   // Returns the section name if this is a section chunk.
77   // It is illegal to call this function on non-section chunks.
78   virtual StringRef getSectionName() const {
79     llvm_unreachable("unimplemented getSectionName");
80   }
81
82   // An output section has pointers to chunks in the section, and each
83   // chunk has a back pointer to an output section.
84   void setOutputSection(OutputSection *O) { Out = O; }
85   OutputSection *getOutputSection() { return Out; }
86
87   // Windows-specific.
88   // Collect all locations that contain absolute addresses for base relocations.
89   virtual void getBaserels(std::vector<Baserel> *Res) {}
90
91   // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
92   // bytes, so this is used only for logging or debugging.
93   virtual StringRef getDebugName() { return ""; }
94
95 protected:
96   Chunk(Kind K = OtherKind) : ChunkKind(K) {}
97   const Kind ChunkKind;
98
99   // The alignment of this chunk. The writer uses the value.
100   uint32_t Align = 1;
101
102   // The RVA of this chunk in the output. The writer sets a value.
103   uint64_t RVA = 0;
104
105 public:
106   // The offset from beginning of the output section. The writer sets a value.
107   uint64_t OutputSectionOff = 0;
108
109 protected:
110   // The output section for this chunk.
111   OutputSection *Out = nullptr;
112 };
113
114 // A chunk corresponding a section of an input file.
115 class SectionChunk final : public Chunk {
116   // Identical COMDAT Folding feature accesses section internal data.
117   friend class ICF;
118
119 public:
120   class symbol_iterator : public llvm::iterator_adaptor_base<
121                               symbol_iterator, const coff_relocation *,
122                               std::random_access_iterator_tag, SymbolBody *> {
123     friend SectionChunk;
124
125     ObjectFile *File;
126
127     symbol_iterator(ObjectFile *File, const coff_relocation *I)
128         : symbol_iterator::iterator_adaptor_base(I), File(File) {}
129
130   public:
131     symbol_iterator() = default;
132
133     SymbolBody *operator*() const {
134       return File->getSymbolBody(I->SymbolTableIndex);
135     }
136   };
137
138   SectionChunk(ObjectFile *File, const coff_section *Header);
139   static bool classof(const Chunk *C) { return C->kind() == SectionKind; }
140   size_t getSize() const override { return Header->SizeOfRawData; }
141   ArrayRef<uint8_t> getContents() const;
142   void writeTo(uint8_t *Buf) const override;
143   bool hasData() const override;
144   uint32_t getPermissions() const override;
145   StringRef getSectionName() const override { return SectionName; }
146   void getBaserels(std::vector<Baserel> *Res) override;
147   bool isCOMDAT() const;
148   void applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
149                    uint64_t P) const;
150   void applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
151                    uint64_t P) const;
152   void applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
153                    uint64_t P) const;
154   void applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
155                      uint64_t P) const;
156
157   // Called if the garbage collector decides to not include this chunk
158   // in a final output. It's supposed to print out a log message to stdout.
159   void printDiscardedMessage() const;
160
161   // Adds COMDAT associative sections to this COMDAT section. A chunk
162   // and its children are treated as a group by the garbage collector.
163   void addAssociative(SectionChunk *Child);
164
165   StringRef getDebugName() override;
166   void setSymbol(DefinedRegular *S) { if (!Sym) Sym = S; }
167
168   // Returns true if the chunk was not dropped by GC or COMDAT deduplication.
169   bool isLive() { return Live && !Discarded; }
170
171   // Used by the garbage collector.
172   void markLive() {
173     assert(Config->DoGC && "should only mark things live from GC");
174     assert(!isLive() && "Cannot mark an already live section!");
175     Live = true;
176   }
177
178   // Returns true if this chunk was dropped by COMDAT deduplication.
179   bool isDiscarded() const { return Discarded; }
180
181   // Used by the SymbolTable when discarding unused comdat sections. This is
182   // redundant when GC is enabled, as all comdat sections will start out dead.
183   void markDiscarded() { Discarded = true; }
184
185   // True if this is a codeview debug info chunk. These will not be laid out in
186   // the image. Instead they will end up in the PDB, if one is requested.
187   bool isCodeView() const {
188     return SectionName == ".debug" || SectionName.startswith(".debug$");
189   }
190
191   // True if this is a DWARF debug info chunk.
192   bool isDWARF() const { return SectionName.startswith(".debug_"); }
193
194   // Allow iteration over the bodies of this chunk's relocated symbols.
195   llvm::iterator_range<symbol_iterator> symbols() const {
196     return llvm::make_range(symbol_iterator(File, Relocs.begin()),
197                             symbol_iterator(File, Relocs.end()));
198   }
199
200   // Allow iteration over the associated child chunks for this section.
201   ArrayRef<SectionChunk *> children() const { return AssocChildren; }
202
203   // A pointer pointing to a replacement for this chunk.
204   // Initially it points to "this" object. If this chunk is merged
205   // with other chunk by ICF, it points to another chunk,
206   // and this chunk is considrered as dead.
207   SectionChunk *Repl;
208
209   // The CRC of the contents as described in the COFF spec 4.5.5.
210   // Auxiliary Format 5: Section Definitions. Used for ICF.
211   uint32_t Checksum = 0;
212
213   const coff_section *Header;
214
215   // The file that this chunk was created from.
216   ObjectFile *File;
217
218 private:
219   StringRef SectionName;
220   std::vector<SectionChunk *> AssocChildren;
221   llvm::iterator_range<const coff_relocation *> Relocs;
222   size_t NumRelocs;
223
224   // True if this chunk was discarded because it was a duplicate comdat section.
225   bool Discarded;
226
227   // Used by the garbage collector.
228   bool Live;
229
230   // Used for ICF (Identical COMDAT Folding)
231   void replace(SectionChunk *Other);
232   uint32_t Class[2] = {0, 0};
233
234   // Sym points to a section symbol if this is a COMDAT chunk.
235   DefinedRegular *Sym = nullptr;
236 };
237
238 // A chunk for common symbols. Common chunks don't have actual data.
239 class CommonChunk : public Chunk {
240 public:
241   CommonChunk(const COFFSymbolRef Sym);
242   size_t getSize() const override { return Sym.getValue(); }
243   bool hasData() const override { return false; }
244   uint32_t getPermissions() const override;
245   StringRef getSectionName() const override { return ".bss"; }
246
247 private:
248   const COFFSymbolRef Sym;
249 };
250
251 // A chunk for linker-created strings.
252 class StringChunk : public Chunk {
253 public:
254   explicit StringChunk(StringRef S) : Str(S) {}
255   size_t getSize() const override { return Str.size() + 1; }
256   void writeTo(uint8_t *Buf) const override;
257
258 private:
259   StringRef Str;
260 };
261
262 static const uint8_t ImportThunkX86[] = {
263     0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
264 };
265
266 static const uint8_t ImportThunkARM[] = {
267     0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
268     0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
269     0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
270 };
271
272 static const uint8_t ImportThunkARM64[] = {
273     0x10, 0x00, 0x00, 0x90, // adrp x16, #0
274     0x10, 0x02, 0x40, 0xf9, // ldr  x16, [x16]
275     0x00, 0x02, 0x1f, 0xd6, // br   x16
276 };
277
278 // Windows-specific.
279 // A chunk for DLL import jump table entry. In a final output, it's
280 // contents will be a JMP instruction to some __imp_ symbol.
281 class ImportThunkChunkX64 : public Chunk {
282 public:
283   explicit ImportThunkChunkX64(Defined *S);
284   size_t getSize() const override { return sizeof(ImportThunkX86); }
285   void writeTo(uint8_t *Buf) const override;
286
287 private:
288   Defined *ImpSymbol;
289 };
290
291 class ImportThunkChunkX86 : public Chunk {
292 public:
293   explicit ImportThunkChunkX86(Defined *S) : ImpSymbol(S) {}
294   size_t getSize() const override { return sizeof(ImportThunkX86); }
295   void getBaserels(std::vector<Baserel> *Res) override;
296   void writeTo(uint8_t *Buf) const override;
297
298 private:
299   Defined *ImpSymbol;
300 };
301
302 class ImportThunkChunkARM : public Chunk {
303 public:
304   explicit ImportThunkChunkARM(Defined *S) : ImpSymbol(S) {}
305   size_t getSize() const override { return sizeof(ImportThunkARM); }
306   void getBaserels(std::vector<Baserel> *Res) override;
307   void writeTo(uint8_t *Buf) const override;
308
309 private:
310   Defined *ImpSymbol;
311 };
312
313 class ImportThunkChunkARM64 : public Chunk {
314 public:
315   explicit ImportThunkChunkARM64(Defined *S) : ImpSymbol(S) {}
316   size_t getSize() const override { return sizeof(ImportThunkARM64); }
317   void writeTo(uint8_t *Buf) const override;
318
319 private:
320   Defined *ImpSymbol;
321 };
322
323 // Windows-specific.
324 // See comments for DefinedLocalImport class.
325 class LocalImportChunk : public Chunk {
326 public:
327   explicit LocalImportChunk(Defined *S) : Sym(S) {}
328   size_t getSize() const override;
329   void getBaserels(std::vector<Baserel> *Res) override;
330   void writeTo(uint8_t *Buf) const override;
331
332 private:
333   Defined *Sym;
334 };
335
336 // Windows-specific.
337 // A chunk for SEH table which contains RVAs of safe exception handler
338 // functions. x86-only.
339 class SEHTableChunk : public Chunk {
340 public:
341   explicit SEHTableChunk(std::set<Defined *> S) : Syms(std::move(S)) {}
342   size_t getSize() const override { return Syms.size() * 4; }
343   void writeTo(uint8_t *Buf) const override;
344
345 private:
346   std::set<Defined *> Syms;
347 };
348
349 // Windows-specific.
350 // This class represents a block in .reloc section.
351 // See the PE/COFF spec 5.6 for details.
352 class BaserelChunk : public Chunk {
353 public:
354   BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End);
355   size_t getSize() const override { return Data.size(); }
356   void writeTo(uint8_t *Buf) const override;
357
358 private:
359   std::vector<uint8_t> Data;
360 };
361
362 class Baserel {
363 public:
364   Baserel(uint32_t V, uint8_t Ty) : RVA(V), Type(Ty) {}
365   explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
366   uint8_t getDefaultType();
367
368   uint32_t RVA;
369   uint8_t Type;
370 };
371
372 } // namespace coff
373 } // namespace lld
374
375 #endif