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