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