1 | //===- Chunks.h -------------------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLD_COFF_CHUNKS_H |
10 | #define LLD_COFF_CHUNKS_H |
11 | |
12 | #include "Config.h" |
13 | #include "InputFiles.h" |
14 | #include "lld/Common/LLVM.h" |
15 | #include "llvm/ADT/ArrayRef.h" |
16 | #include "llvm/ADT/PointerIntPair.h" |
17 | #include "llvm/ADT/iterator.h" |
18 | #include "llvm/ADT/iterator_range.h" |
19 | #include "llvm/MC/StringTableBuilder.h" |
20 | #include "llvm/Object/COFF.h" |
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 | |
33 | class Baserel; |
34 | class Defined; |
35 | class DefinedImportData; |
36 | class DefinedRegular; |
37 | class ObjFile; |
38 | class OutputSection; |
39 | class RuntimePseudoReloc; |
40 | class Symbol; |
41 | |
42 | // Mask for permissions (discardable, writable, readable, executable, etc). |
43 | const uint32_t permMask = 0xFE000000; |
44 | |
45 | // Mask for section types (code, data, bss). |
46 | const uint32_t typeMask = 0x000000E0; |
47 | |
48 | // The log base 2 of the largest section alignment, which is log2(8192), or 13. |
49 | enum : unsigned { Log2MaxSectionAlignment = 13 }; |
50 | |
51 | // A Chunk represents a chunk of data that will occupy space in the |
52 | // output (if the resolver chose that). It may or may not be backed by |
53 | // a section of an input file. It could be linker-created data, or |
54 | // doesn't even have actual data (if common or bss). |
55 | class Chunk { |
56 | public: |
57 | enum Kind : uint8_t { SectionKind, OtherKind, ImportThunkKind }; |
58 | Kind kind() const { return chunkKind; } |
59 | |
60 | // Returns the size of this chunk (even if this is a common or BSS.) |
61 | size_t getSize() const; |
62 | |
63 | // Returns chunk alignment in power of two form. Value values are powers of |
64 | // two from 1 to 8192. |
65 | uint32_t getAlignment() const { return 1U << p2Align; } |
66 | |
67 | // Update the chunk section alignment measured in bytes. Internally alignment |
68 | // is stored in log2. |
69 | void setAlignment(uint32_t align) { |
70 | // Treat zero byte alignment as 1 byte alignment. |
71 | align = align ? align : 1; |
72 | assert(llvm::isPowerOf2_32(align) && "alignment is not a power of 2" ); |
73 | p2Align = llvm::Log2_32(align); |
74 | assert(p2Align <= Log2MaxSectionAlignment && |
75 | "impossible requested alignment" ); |
76 | } |
77 | |
78 | // Write this chunk to a mmap'ed file, assuming Buf is pointing to |
79 | // beginning of the file. Because this function may use RVA values |
80 | // of other chunks for relocations, you need to set them properly |
81 | // before calling this function. |
82 | void writeTo(uint8_t *buf) const; |
83 | |
84 | // The writer sets and uses the addresses. In practice, PE images cannot be |
85 | // larger than 2GB. Chunks are always laid as part of the image, so Chunk RVAs |
86 | // can be stored with 32 bits. |
87 | uint32_t getRVA() const { return rva; } |
88 | void setRVA(uint64_t v) { |
89 | rva = (uint32_t)v; |
90 | assert(rva == v && "RVA truncated" ); |
91 | } |
92 | |
93 | // Returns readable/writable/executable bits. |
94 | uint32_t getOutputCharacteristics() const; |
95 | |
96 | // Returns the section name if this is a section chunk. |
97 | // It is illegal to call this function on non-section chunks. |
98 | StringRef getSectionName() const; |
99 | |
100 | // An output section has pointers to chunks in the section, and each |
101 | // chunk has a back pointer to an output section. |
102 | void setOutputSectionIdx(uint16_t o) { osidx = o; } |
103 | uint16_t getOutputSectionIdx() const { return osidx; } |
104 | OutputSection *getOutputSection() const; |
105 | |
106 | // Windows-specific. |
107 | // Collect all locations that contain absolute addresses for base relocations. |
108 | void getBaserels(std::vector<Baserel> *res); |
109 | |
110 | // Returns a human-readable name of this chunk. Chunks are unnamed chunks of |
111 | // bytes, so this is used only for logging or debugging. |
112 | StringRef getDebugName() const; |
113 | |
114 | // Return true if this file has the hotpatch flag set to true in the |
115 | // S_COMPILE3 record in codeview debug info. Also returns true for some thunks |
116 | // synthesized by the linker. |
117 | bool isHotPatchable() const; |
118 | |
119 | protected: |
120 | Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {} |
121 | |
122 | const Kind chunkKind; |
123 | |
124 | public: |
125 | // Returns true if this has non-zero data. BSS chunks return |
126 | // false. If false is returned, the space occupied by this chunk |
127 | // will be filled with zeros. Corresponds to the |
128 | // IMAGE_SCN_CNT_UNINITIALIZED_DATA section characteristic bit. |
129 | uint8_t hasData : 1; |
130 | |
131 | public: |
132 | // The alignment of this chunk, stored in log2 form. The writer uses the |
133 | // value. |
134 | uint8_t p2Align : 7; |
135 | |
136 | // The output section index for this chunk. The first valid section number is |
137 | // one. |
138 | uint16_t osidx = 0; |
139 | |
140 | // The RVA of this chunk in the output. The writer sets a value. |
141 | uint32_t rva = 0; |
142 | }; |
143 | |
144 | class NonSectionChunk : public Chunk { |
145 | public: |
146 | virtual ~NonSectionChunk() = default; |
147 | |
148 | // Returns the size of this chunk (even if this is a common or BSS.) |
149 | virtual size_t getSize() const = 0; |
150 | |
151 | virtual uint32_t getOutputCharacteristics() const { return 0; } |
152 | |
153 | // Write this chunk to a mmap'ed file, assuming Buf is pointing to |
154 | // beginning of the file. Because this function may use RVA values |
155 | // of other chunks for relocations, you need to set them properly |
156 | // before calling this function. |
157 | virtual void writeTo(uint8_t *buf) const {} |
158 | |
159 | // Returns the section name if this is a section chunk. |
160 | // It is illegal to call this function on non-section chunks. |
161 | virtual StringRef getSectionName() const { |
162 | llvm_unreachable("unimplemented getSectionName" ); |
163 | } |
164 | |
165 | // Windows-specific. |
166 | // Collect all locations that contain absolute addresses for base relocations. |
167 | virtual void getBaserels(std::vector<Baserel> *res) {} |
168 | |
169 | // Returns a human-readable name of this chunk. Chunks are unnamed chunks of |
170 | // bytes, so this is used only for logging or debugging. |
171 | virtual StringRef getDebugName() const { return "" ; } |
172 | |
173 | static bool classof(const Chunk *c) { return c->kind() != SectionKind; } |
174 | |
175 | protected: |
176 | NonSectionChunk(Kind k = OtherKind) : Chunk(k) {} |
177 | }; |
178 | |
179 | // A chunk corresponding a section of an input file. |
180 | class SectionChunk final : public Chunk { |
181 | // Identical COMDAT Folding feature accesses section internal data. |
182 | friend class ICF; |
183 | |
184 | public: |
185 | class symbol_iterator : public llvm::iterator_adaptor_base< |
186 | symbol_iterator, const coff_relocation *, |
187 | std::random_access_iterator_tag, Symbol *> { |
188 | friend SectionChunk; |
189 | |
190 | ObjFile *file; |
191 | |
192 | symbol_iterator(ObjFile *file, const coff_relocation *i) |
193 | : symbol_iterator::iterator_adaptor_base(i), file(file) {} |
194 | |
195 | public: |
196 | symbol_iterator() = default; |
197 | |
198 | Symbol *operator*() const { return file->getSymbol(I->SymbolTableIndex); } |
199 | }; |
200 | |
201 | SectionChunk(ObjFile *file, const coff_section *); |
202 | static bool classof(const Chunk *c) { return c->kind() == SectionKind; } |
203 | size_t getSize() const { return header->SizeOfRawData; } |
204 | ArrayRef<uint8_t> getContents() const; |
205 | void writeTo(uint8_t *buf) const; |
206 | |
207 | // Defend against unsorted relocations. This may be overly conservative. |
208 | void sortRelocations(); |
209 | |
210 | // Write and relocate a portion of the section. This is intended to be called |
211 | // in a loop. Relocations must be sorted first. |
212 | void writeAndRelocateSubsection(ArrayRef<uint8_t> sec, |
213 | ArrayRef<uint8_t> subsec, |
214 | uint32_t &nextRelocIndex, uint8_t *buf) const; |
215 | |
216 | uint32_t getOutputCharacteristics() const { |
217 | return header->Characteristics & (permMask | typeMask); |
218 | } |
219 | StringRef getSectionName() const { |
220 | return StringRef(sectionNameData, sectionNameSize); |
221 | } |
222 | void getBaserels(std::vector<Baserel> *res); |
223 | bool isCOMDAT() const; |
224 | void applyRelocation(uint8_t *off, const coff_relocation &rel) const; |
225 | void applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
226 | uint64_t p) const; |
227 | void applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
228 | uint64_t p) const; |
229 | void applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
230 | uint64_t p) const; |
231 | void applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, |
232 | uint64_t p) const; |
233 | |
234 | void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &res); |
235 | |
236 | // Called if the garbage collector decides to not include this chunk |
237 | // in a final output. It's supposed to print out a log message to stdout. |
238 | void printDiscardedMessage() const; |
239 | |
240 | // Adds COMDAT associative sections to this COMDAT section. A chunk |
241 | // and its children are treated as a group by the garbage collector. |
242 | void addAssociative(SectionChunk *child); |
243 | |
244 | StringRef getDebugName() const; |
245 | |
246 | // True if this is a codeview debug info chunk. These will not be laid out in |
247 | // the image. Instead they will end up in the PDB, if one is requested. |
248 | bool isCodeView() const { |
249 | return getSectionName() == ".debug" || getSectionName().startswith(".debug$" ); |
250 | } |
251 | |
252 | // True if this is a DWARF debug info or exception handling chunk. |
253 | bool isDWARF() const { |
254 | return getSectionName().startswith(".debug_" ) || getSectionName() == ".eh_frame" ; |
255 | } |
256 | |
257 | // Allow iteration over the bodies of this chunk's relocated symbols. |
258 | llvm::iterator_range<symbol_iterator> symbols() const { |
259 | return llvm::make_range(symbol_iterator(file, relocsData), |
260 | symbol_iterator(file, relocsData + relocsSize)); |
261 | } |
262 | |
263 | ArrayRef<coff_relocation> getRelocs() const { |
264 | return llvm::makeArrayRef(relocsData, relocsSize); |
265 | } |
266 | |
267 | // Reloc setter used by ARM range extension thunk insertion. |
268 | void setRelocs(ArrayRef<coff_relocation> newRelocs) { |
269 | relocsData = newRelocs.data(); |
270 | relocsSize = newRelocs.size(); |
271 | assert(relocsSize == newRelocs.size() && "reloc size truncation" ); |
272 | } |
273 | |
274 | // Single linked list iterator for associated comdat children. |
275 | class AssociatedIterator |
276 | : public llvm::iterator_facade_base< |
277 | AssociatedIterator, std::forward_iterator_tag, SectionChunk> { |
278 | public: |
279 | AssociatedIterator() = default; |
280 | AssociatedIterator(SectionChunk *head) : cur(head) {} |
281 | bool operator==(const AssociatedIterator &r) const { return cur == r.cur; } |
282 | // FIXME: Wrong const-ness, but it makes filter ranges work. |
283 | SectionChunk &operator*() const { return *cur; } |
284 | SectionChunk &operator*() { return *cur; } |
285 | AssociatedIterator &operator++() { |
286 | cur = cur->assocChildren; |
287 | return *this; |
288 | } |
289 | |
290 | private: |
291 | SectionChunk *cur = nullptr; |
292 | }; |
293 | |
294 | // Allow iteration over the associated child chunks for this section. |
295 | llvm::iterator_range<AssociatedIterator> children() const { |
296 | // Associated sections do not have children. The assocChildren field is |
297 | // part of the parent's list of children. |
298 | bool isAssoc = selection == llvm::COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; |
299 | return llvm::make_range( |
300 | AssociatedIterator(isAssoc ? nullptr : assocChildren), |
301 | AssociatedIterator(nullptr)); |
302 | } |
303 | |
304 | // The section ID this chunk belongs to in its Obj. |
305 | uint32_t getSectionNumber() const; |
306 | |
307 | ArrayRef<uint8_t> consumeDebugMagic(); |
308 | |
309 | static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> data, |
310 | StringRef sectionName); |
311 | |
312 | static SectionChunk *findByName(ArrayRef<SectionChunk *> sections, |
313 | StringRef name); |
314 | |
315 | // The file that this chunk was created from. |
316 | ObjFile *file; |
317 | |
318 | // Pointer to the COFF section header in the input file. |
319 | const coff_section *; |
320 | |
321 | // The COMDAT leader symbol if this is a COMDAT chunk. |
322 | DefinedRegular *sym = nullptr; |
323 | |
324 | // The CRC of the contents as described in the COFF spec 4.5.5. |
325 | // Auxiliary Format 5: Section Definitions. Used for ICF. |
326 | uint32_t checksum = 0; |
327 | |
328 | // Used by the garbage collector. |
329 | bool live; |
330 | |
331 | // Whether this section needs to be kept distinct from other sections during |
332 | // ICF. This is set by the driver using address-significance tables. |
333 | bool keepUnique = false; |
334 | |
335 | // The COMDAT selection if this is a COMDAT chunk. |
336 | llvm::COFF::COMDATType selection = (llvm::COFF::COMDATType)0; |
337 | |
338 | // A pointer pointing to a replacement for this chunk. |
339 | // Initially it points to "this" object. If this chunk is merged |
340 | // with other chunk by ICF, it points to another chunk, |
341 | // and this chunk is considered as dead. |
342 | SectionChunk *repl; |
343 | |
344 | private: |
345 | SectionChunk *assocChildren = nullptr; |
346 | |
347 | // Used for ICF (Identical COMDAT Folding) |
348 | void replace(SectionChunk *other); |
349 | uint32_t eqClass[2] = {0, 0}; |
350 | |
351 | // Relocations for this section. Size is stored below. |
352 | const coff_relocation *relocsData; |
353 | |
354 | // Section name string. Size is stored below. |
355 | const char *sectionNameData; |
356 | |
357 | uint32_t relocsSize = 0; |
358 | uint32_t sectionNameSize = 0; |
359 | }; |
360 | |
361 | // Inline methods to implement faux-virtual dispatch for SectionChunk. |
362 | |
363 | inline size_t Chunk::getSize() const { |
364 | if (isa<SectionChunk>(this)) |
365 | return static_cast<const SectionChunk *>(this)->getSize(); |
366 | else |
367 | return static_cast<const NonSectionChunk *>(this)->getSize(); |
368 | } |
369 | |
370 | inline uint32_t Chunk::getOutputCharacteristics() const { |
371 | if (isa<SectionChunk>(this)) |
372 | return static_cast<const SectionChunk *>(this)->getOutputCharacteristics(); |
373 | else |
374 | return static_cast<const NonSectionChunk *>(this) |
375 | ->getOutputCharacteristics(); |
376 | } |
377 | |
378 | inline void Chunk::writeTo(uint8_t *buf) const { |
379 | if (isa<SectionChunk>(this)) |
380 | static_cast<const SectionChunk *>(this)->writeTo(buf); |
381 | else |
382 | static_cast<const NonSectionChunk *>(this)->writeTo(buf); |
383 | } |
384 | |
385 | inline StringRef Chunk::getSectionName() const { |
386 | if (isa<SectionChunk>(this)) |
387 | return static_cast<const SectionChunk *>(this)->getSectionName(); |
388 | else |
389 | return static_cast<const NonSectionChunk *>(this)->getSectionName(); |
390 | } |
391 | |
392 | inline void Chunk::getBaserels(std::vector<Baserel> *res) { |
393 | if (isa<SectionChunk>(this)) |
394 | static_cast<SectionChunk *>(this)->getBaserels(res); |
395 | else |
396 | static_cast<NonSectionChunk *>(this)->getBaserels(res); |
397 | } |
398 | |
399 | inline StringRef Chunk::getDebugName() const { |
400 | if (isa<SectionChunk>(this)) |
401 | return static_cast<const SectionChunk *>(this)->getDebugName(); |
402 | else |
403 | return static_cast<const NonSectionChunk *>(this)->getDebugName(); |
404 | } |
405 | |
406 | // This class is used to implement an lld-specific feature (not implemented in |
407 | // MSVC) that minimizes the output size by finding string literals sharing tail |
408 | // parts and merging them. |
409 | // |
410 | // If string tail merging is enabled and a section is identified as containing a |
411 | // string literal, it is added to a MergeChunk with an appropriate alignment. |
412 | // The MergeChunk then tail merges the strings using the StringTableBuilder |
413 | // class and assigns RVAs and section offsets to each of the member chunks based |
414 | // on the offsets assigned by the StringTableBuilder. |
415 | class MergeChunk : public NonSectionChunk { |
416 | public: |
417 | MergeChunk(uint32_t alignment); |
418 | static void addSection(SectionChunk *c); |
419 | void finalizeContents(); |
420 | void assignSubsectionRVAs(); |
421 | |
422 | uint32_t getOutputCharacteristics() const override; |
423 | StringRef getSectionName() const override { return ".rdata" ; } |
424 | size_t getSize() const override; |
425 | void writeTo(uint8_t *buf) const override; |
426 | |
427 | static MergeChunk *instances[Log2MaxSectionAlignment + 1]; |
428 | std::vector<SectionChunk *> sections; |
429 | |
430 | private: |
431 | llvm::StringTableBuilder builder; |
432 | bool finalized = false; |
433 | }; |
434 | |
435 | // A chunk for common symbols. Common chunks don't have actual data. |
436 | class CommonChunk : public NonSectionChunk { |
437 | public: |
438 | CommonChunk(const COFFSymbolRef sym); |
439 | size_t getSize() const override { return sym.getValue(); } |
440 | uint32_t getOutputCharacteristics() const override; |
441 | StringRef getSectionName() const override { return ".bss" ; } |
442 | |
443 | private: |
444 | const COFFSymbolRef sym; |
445 | }; |
446 | |
447 | // A chunk for linker-created strings. |
448 | class StringChunk : public NonSectionChunk { |
449 | public: |
450 | explicit StringChunk(StringRef s) : str(s) {} |
451 | size_t getSize() const override { return str.size() + 1; } |
452 | void writeTo(uint8_t *buf) const override; |
453 | |
454 | private: |
455 | StringRef str; |
456 | }; |
457 | |
458 | static const uint8_t importThunkX86[] = { |
459 | 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0 |
460 | }; |
461 | |
462 | static const uint8_t importThunkARM[] = { |
463 | 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 |
464 | 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 |
465 | 0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip] |
466 | }; |
467 | |
468 | static const uint8_t importThunkARM64[] = { |
469 | 0x10, 0x00, 0x00, 0x90, // adrp x16, #0 |
470 | 0x10, 0x02, 0x40, 0xf9, // ldr x16, [x16] |
471 | 0x00, 0x02, 0x1f, 0xd6, // br x16 |
472 | }; |
473 | |
474 | // Windows-specific. |
475 | // A chunk for DLL import jump table entry. In a final output, its |
476 | // contents will be a JMP instruction to some __imp_ symbol. |
477 | class ImportThunkChunk : public NonSectionChunk { |
478 | public: |
479 | ImportThunkChunk(Defined *s) |
480 | : NonSectionChunk(ImportThunkKind), impSymbol(s) {} |
481 | static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; } |
482 | |
483 | protected: |
484 | Defined *impSymbol; |
485 | }; |
486 | |
487 | class ImportThunkChunkX64 : public ImportThunkChunk { |
488 | public: |
489 | explicit ImportThunkChunkX64(Defined *s); |
490 | size_t getSize() const override { return sizeof(importThunkX86); } |
491 | void writeTo(uint8_t *buf) const override; |
492 | }; |
493 | |
494 | class ImportThunkChunkX86 : public ImportThunkChunk { |
495 | public: |
496 | explicit ImportThunkChunkX86(Defined *s) : ImportThunkChunk(s) {} |
497 | size_t getSize() const override { return sizeof(importThunkX86); } |
498 | void getBaserels(std::vector<Baserel> *res) override; |
499 | void writeTo(uint8_t *buf) const override; |
500 | }; |
501 | |
502 | class ImportThunkChunkARM : public ImportThunkChunk { |
503 | public: |
504 | explicit ImportThunkChunkARM(Defined *s) : ImportThunkChunk(s) { |
505 | setAlignment(2); |
506 | } |
507 | size_t getSize() const override { return sizeof(importThunkARM); } |
508 | void getBaserels(std::vector<Baserel> *res) override; |
509 | void writeTo(uint8_t *buf) const override; |
510 | }; |
511 | |
512 | class ImportThunkChunkARM64 : public ImportThunkChunk { |
513 | public: |
514 | explicit ImportThunkChunkARM64(Defined *s) : ImportThunkChunk(s) { |
515 | setAlignment(4); |
516 | } |
517 | size_t getSize() const override { return sizeof(importThunkARM64); } |
518 | void writeTo(uint8_t *buf) const override; |
519 | }; |
520 | |
521 | class RangeExtensionThunkARM : public NonSectionChunk { |
522 | public: |
523 | explicit RangeExtensionThunkARM(Defined *t) : target(t) { setAlignment(2); } |
524 | size_t getSize() const override; |
525 | void writeTo(uint8_t *buf) const override; |
526 | |
527 | Defined *target; |
528 | }; |
529 | |
530 | class RangeExtensionThunkARM64 : public NonSectionChunk { |
531 | public: |
532 | explicit RangeExtensionThunkARM64(Defined *t) : target(t) { setAlignment(4); } |
533 | size_t getSize() const override; |
534 | void writeTo(uint8_t *buf) const override; |
535 | |
536 | Defined *target; |
537 | }; |
538 | |
539 | // Windows-specific. |
540 | // See comments for DefinedLocalImport class. |
541 | class LocalImportChunk : public NonSectionChunk { |
542 | public: |
543 | explicit LocalImportChunk(Defined *s) : sym(s) { |
544 | setAlignment(config->wordsize); |
545 | } |
546 | size_t getSize() const override; |
547 | void getBaserels(std::vector<Baserel> *res) override; |
548 | void writeTo(uint8_t *buf) const override; |
549 | |
550 | private: |
551 | Defined *sym; |
552 | }; |
553 | |
554 | // Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and |
555 | // offset into the chunk. Order does not matter as the RVA table will be sorted |
556 | // later. |
557 | struct ChunkAndOffset { |
558 | Chunk *inputChunk; |
559 | uint32_t offset; |
560 | |
561 | struct DenseMapInfo { |
562 | static ChunkAndOffset getEmptyKey() { |
563 | return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0}; |
564 | } |
565 | static ChunkAndOffset getTombstoneKey() { |
566 | return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0}; |
567 | } |
568 | static unsigned getHashValue(const ChunkAndOffset &co) { |
569 | return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue( |
570 | {co.inputChunk, co.offset}); |
571 | } |
572 | static bool isEqual(const ChunkAndOffset &lhs, const ChunkAndOffset &rhs) { |
573 | return lhs.inputChunk == rhs.inputChunk && lhs.offset == rhs.offset; |
574 | } |
575 | }; |
576 | }; |
577 | |
578 | using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>; |
579 | |
580 | // Table which contains symbol RVAs. Used for /safeseh and /guard:cf. |
581 | class RVATableChunk : public NonSectionChunk { |
582 | public: |
583 | explicit RVATableChunk(SymbolRVASet s) : syms(std::move(s)) {} |
584 | size_t getSize() const override { return syms.size() * 4; } |
585 | void writeTo(uint8_t *buf) const override; |
586 | |
587 | private: |
588 | SymbolRVASet syms; |
589 | }; |
590 | |
591 | // Table which contains symbol RVAs with flags. Used for /guard:ehcont. |
592 | class RVAFlagTableChunk : public NonSectionChunk { |
593 | public: |
594 | explicit RVAFlagTableChunk(SymbolRVASet s) : syms(std::move(s)) {} |
595 | size_t getSize() const override { return syms.size() * 5; } |
596 | void writeTo(uint8_t *buf) const override; |
597 | |
598 | private: |
599 | SymbolRVASet syms; |
600 | }; |
601 | |
602 | // Windows-specific. |
603 | // This class represents a block in .reloc section. |
604 | // See the PE/COFF spec 5.6 for details. |
605 | class BaserelChunk : public NonSectionChunk { |
606 | public: |
607 | BaserelChunk(uint32_t page, Baserel *begin, Baserel *end); |
608 | size_t getSize() const override { return data.size(); } |
609 | void writeTo(uint8_t *buf) const override; |
610 | |
611 | private: |
612 | std::vector<uint8_t> data; |
613 | }; |
614 | |
615 | class Baserel { |
616 | public: |
617 | Baserel(uint32_t v, uint8_t ty) : rva(v), type(ty) {} |
618 | explicit Baserel(uint32_t v) : Baserel(v, getDefaultType()) {} |
619 | uint8_t getDefaultType(); |
620 | |
621 | uint32_t rva; |
622 | uint8_t type; |
623 | }; |
624 | |
625 | // This is a placeholder Chunk, to allow attaching a DefinedSynthetic to a |
626 | // specific place in a section, without any data. This is used for the MinGW |
627 | // specific symbol __RUNTIME_PSEUDO_RELOC_LIST_END__, even though the concept |
628 | // of an empty chunk isn't MinGW specific. |
629 | class EmptyChunk : public NonSectionChunk { |
630 | public: |
631 | EmptyChunk() {} |
632 | size_t getSize() const override { return 0; } |
633 | void writeTo(uint8_t *buf) const override {} |
634 | }; |
635 | |
636 | // MinGW specific, for the "automatic import of variables from DLLs" feature. |
637 | // This provides the table of runtime pseudo relocations, for variable |
638 | // references that turned out to need to be imported from a DLL even though |
639 | // the reference didn't use the dllimport attribute. The MinGW runtime will |
640 | // process this table after loading, before handling control over to user |
641 | // code. |
642 | class PseudoRelocTableChunk : public NonSectionChunk { |
643 | public: |
644 | PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &relocs) |
645 | : relocs(std::move(relocs)) { |
646 | setAlignment(4); |
647 | } |
648 | size_t getSize() const override; |
649 | void writeTo(uint8_t *buf) const override; |
650 | |
651 | private: |
652 | std::vector<RuntimePseudoReloc> relocs; |
653 | }; |
654 | |
655 | // MinGW specific; information about one individual location in the image |
656 | // that needs to be fixed up at runtime after loading. This represents |
657 | // one individual element in the PseudoRelocTableChunk table. |
658 | class RuntimePseudoReloc { |
659 | public: |
660 | RuntimePseudoReloc(Defined *sym, SectionChunk *target, uint32_t targetOffset, |
661 | int flags) |
662 | : sym(sym), target(target), targetOffset(targetOffset), flags(flags) {} |
663 | |
664 | Defined *sym; |
665 | SectionChunk *target; |
666 | uint32_t targetOffset; |
667 | // The Flags field contains the size of the relocation, in bits. No other |
668 | // flags are currently defined. |
669 | int flags; |
670 | }; |
671 | |
672 | // MinGW specific. A Chunk that contains one pointer-sized absolute value. |
673 | class AbsolutePointerChunk : public NonSectionChunk { |
674 | public: |
675 | AbsolutePointerChunk(uint64_t value) : value(value) { |
676 | setAlignment(getSize()); |
677 | } |
678 | size_t getSize() const override; |
679 | void writeTo(uint8_t *buf) const override; |
680 | |
681 | private: |
682 | uint64_t value; |
683 | }; |
684 | |
685 | // Return true if this file has the hotpatch flag set to true in the S_COMPILE3 |
686 | // record in codeview debug info. Also returns true for some thunks synthesized |
687 | // by the linker. |
688 | inline bool Chunk::isHotPatchable() const { |
689 | if (auto *sc = dyn_cast<SectionChunk>(this)) |
690 | return sc->file->hotPatchable; |
691 | else if (isa<ImportThunkChunk>(this)) |
692 | return true; |
693 | return false; |
694 | } |
695 | |
696 | void applyMOV32T(uint8_t *off, uint32_t v); |
697 | void applyBranch24T(uint8_t *off, int32_t v); |
698 | |
699 | void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift); |
700 | void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit); |
701 | void applyArm64Branch26(uint8_t *off, int64_t v); |
702 | |
703 | } // namespace coff |
704 | } // namespace lld |
705 | |
706 | namespace llvm { |
707 | template <> |
708 | struct DenseMapInfo<lld::coff::ChunkAndOffset> |
709 | : lld::coff::ChunkAndOffset::DenseMapInfo {}; |
710 | } |
711 | |
712 | #endif |
713 | |