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