10b57cec5SDimitry Andric //===- Chunks.h -------------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef LLD_COFF_CHUNKS_H 100b57cec5SDimitry Andric #define LLD_COFF_CHUNKS_H 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "Config.h" 130b57cec5SDimitry Andric #include "InputFiles.h" 140b57cec5SDimitry Andric #include "lld/Common/LLVM.h" 150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 160b57cec5SDimitry Andric #include "llvm/ADT/PointerIntPair.h" 170b57cec5SDimitry Andric #include "llvm/ADT/iterator.h" 180b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 190b57cec5SDimitry Andric #include "llvm/MC/StringTableBuilder.h" 200b57cec5SDimitry Andric #include "llvm/Object/COFF.h" 210b57cec5SDimitry Andric #include <utility> 220b57cec5SDimitry Andric #include <vector> 230b57cec5SDimitry Andric 24*bdd1243dSDimitry Andric namespace lld::coff { 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric using llvm::COFF::ImportDirectoryTableEntry; 270b57cec5SDimitry Andric using llvm::object::COFFSymbolRef; 280b57cec5SDimitry Andric using llvm::object::SectionRef; 290b57cec5SDimitry Andric using llvm::object::coff_relocation; 300b57cec5SDimitry Andric using llvm::object::coff_section; 310b57cec5SDimitry Andric 320b57cec5SDimitry Andric class Baserel; 330b57cec5SDimitry Andric class Defined; 340b57cec5SDimitry Andric class DefinedImportData; 350b57cec5SDimitry Andric class DefinedRegular; 360b57cec5SDimitry Andric class ObjFile; 370b57cec5SDimitry Andric class OutputSection; 380b57cec5SDimitry Andric class RuntimePseudoReloc; 390b57cec5SDimitry Andric class Symbol; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric // Mask for permissions (discardable, writable, readable, executable, etc). 420b57cec5SDimitry Andric const uint32_t permMask = 0xFE000000; 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric // Mask for section types (code, data, bss). 450b57cec5SDimitry Andric const uint32_t typeMask = 0x000000E0; 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric // The log base 2 of the largest section alignment, which is log2(8192), or 13. 480b57cec5SDimitry Andric enum : unsigned { Log2MaxSectionAlignment = 13 }; 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric // A Chunk represents a chunk of data that will occupy space in the 510b57cec5SDimitry Andric // output (if the resolver chose that). It may or may not be backed by 520b57cec5SDimitry Andric // a section of an input file. It could be linker-created data, or 530b57cec5SDimitry Andric // doesn't even have actual data (if common or bss). 540b57cec5SDimitry Andric class Chunk { 550b57cec5SDimitry Andric public: 560b57cec5SDimitry Andric enum Kind : uint8_t { SectionKind, OtherKind, ImportThunkKind }; 570b57cec5SDimitry Andric Kind kind() const { return chunkKind; } 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric // Returns the size of this chunk (even if this is a common or BSS.) 600b57cec5SDimitry Andric size_t getSize() const; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric // Returns chunk alignment in power of two form. Value values are powers of 630b57cec5SDimitry Andric // two from 1 to 8192. 640b57cec5SDimitry Andric uint32_t getAlignment() const { return 1U << p2Align; } 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric // Update the chunk section alignment measured in bytes. Internally alignment 670b57cec5SDimitry Andric // is stored in log2. 680b57cec5SDimitry Andric void setAlignment(uint32_t align) { 690b57cec5SDimitry Andric // Treat zero byte alignment as 1 byte alignment. 700b57cec5SDimitry Andric align = align ? align : 1; 710b57cec5SDimitry Andric assert(llvm::isPowerOf2_32(align) && "alignment is not a power of 2"); 720b57cec5SDimitry Andric p2Align = llvm::Log2_32(align); 730b57cec5SDimitry Andric assert(p2Align <= Log2MaxSectionAlignment && 740b57cec5SDimitry Andric "impossible requested alignment"); 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric // Write this chunk to a mmap'ed file, assuming Buf is pointing to 780b57cec5SDimitry Andric // beginning of the file. Because this function may use RVA values 790b57cec5SDimitry Andric // of other chunks for relocations, you need to set them properly 800b57cec5SDimitry Andric // before calling this function. 810b57cec5SDimitry Andric void writeTo(uint8_t *buf) const; 820b57cec5SDimitry Andric 830b57cec5SDimitry Andric // The writer sets and uses the addresses. In practice, PE images cannot be 840b57cec5SDimitry Andric // larger than 2GB. Chunks are always laid as part of the image, so Chunk RVAs 850b57cec5SDimitry Andric // can be stored with 32 bits. 860b57cec5SDimitry Andric uint32_t getRVA() const { return rva; } 870b57cec5SDimitry Andric void setRVA(uint64_t v) { 88fe6060f1SDimitry Andric // This may truncate. The writer checks for overflow later. 890b57cec5SDimitry Andric rva = (uint32_t)v; 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric // Returns readable/writable/executable bits. 930b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric // Returns the section name if this is a section chunk. 960b57cec5SDimitry Andric // It is illegal to call this function on non-section chunks. 970b57cec5SDimitry Andric StringRef getSectionName() const; 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric // An output section has pointers to chunks in the section, and each 1000b57cec5SDimitry Andric // chunk has a back pointer to an output section. 1010b57cec5SDimitry Andric void setOutputSectionIdx(uint16_t o) { osidx = o; } 1020b57cec5SDimitry Andric uint16_t getOutputSectionIdx() const { return osidx; } 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric // Windows-specific. 1050b57cec5SDimitry Andric // Collect all locations that contain absolute addresses for base relocations. 1060b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res); 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric // Returns a human-readable name of this chunk. Chunks are unnamed chunks of 1090b57cec5SDimitry Andric // bytes, so this is used only for logging or debugging. 1100b57cec5SDimitry Andric StringRef getDebugName() const; 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric // Return true if this file has the hotpatch flag set to true in the 1130b57cec5SDimitry Andric // S_COMPILE3 record in codeview debug info. Also returns true for some thunks 1140b57cec5SDimitry Andric // synthesized by the linker. 1150b57cec5SDimitry Andric bool isHotPatchable() const; 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric protected: 1180b57cec5SDimitry Andric Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {} 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric const Kind chunkKind; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric public: 1230b57cec5SDimitry Andric // Returns true if this has non-zero data. BSS chunks return 1240b57cec5SDimitry Andric // false. If false is returned, the space occupied by this chunk 1250b57cec5SDimitry Andric // will be filled with zeros. Corresponds to the 1260b57cec5SDimitry Andric // IMAGE_SCN_CNT_UNINITIALIZED_DATA section characteristic bit. 1270b57cec5SDimitry Andric uint8_t hasData : 1; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric public: 1300b57cec5SDimitry Andric // The alignment of this chunk, stored in log2 form. The writer uses the 1310b57cec5SDimitry Andric // value. 1320b57cec5SDimitry Andric uint8_t p2Align : 7; 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric // The output section index for this chunk. The first valid section number is 1350b57cec5SDimitry Andric // one. 1360b57cec5SDimitry Andric uint16_t osidx = 0; 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric // The RVA of this chunk in the output. The writer sets a value. 1390b57cec5SDimitry Andric uint32_t rva = 0; 1400b57cec5SDimitry Andric }; 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric class NonSectionChunk : public Chunk { 1430b57cec5SDimitry Andric public: 1440b57cec5SDimitry Andric virtual ~NonSectionChunk() = default; 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric // Returns the size of this chunk (even if this is a common or BSS.) 1470b57cec5SDimitry Andric virtual size_t getSize() const = 0; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric virtual uint32_t getOutputCharacteristics() const { return 0; } 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric // Write this chunk to a mmap'ed file, assuming Buf is pointing to 1520b57cec5SDimitry Andric // beginning of the file. Because this function may use RVA values 1530b57cec5SDimitry Andric // of other chunks for relocations, you need to set them properly 1540b57cec5SDimitry Andric // before calling this function. 1550b57cec5SDimitry Andric virtual void writeTo(uint8_t *buf) const {} 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric // Returns the section name if this is a section chunk. 1580b57cec5SDimitry Andric // It is illegal to call this function on non-section chunks. 1590b57cec5SDimitry Andric virtual StringRef getSectionName() const { 1600b57cec5SDimitry Andric llvm_unreachable("unimplemented getSectionName"); 1610b57cec5SDimitry Andric } 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric // Windows-specific. 1640b57cec5SDimitry Andric // Collect all locations that contain absolute addresses for base relocations. 1650b57cec5SDimitry Andric virtual void getBaserels(std::vector<Baserel> *res) {} 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric // Returns a human-readable name of this chunk. Chunks are unnamed chunks of 1680b57cec5SDimitry Andric // bytes, so this is used only for logging or debugging. 1690b57cec5SDimitry Andric virtual StringRef getDebugName() const { return ""; } 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric static bool classof(const Chunk *c) { return c->kind() != SectionKind; } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric protected: 1740b57cec5SDimitry Andric NonSectionChunk(Kind k = OtherKind) : Chunk(k) {} 1750b57cec5SDimitry Andric }; 1760b57cec5SDimitry Andric 177*bdd1243dSDimitry Andric // MinGW specific; information about one individual location in the image 178*bdd1243dSDimitry Andric // that needs to be fixed up at runtime after loading. This represents 179*bdd1243dSDimitry Andric // one individual element in the PseudoRelocTableChunk table. 180*bdd1243dSDimitry Andric class RuntimePseudoReloc { 181*bdd1243dSDimitry Andric public: 182*bdd1243dSDimitry Andric RuntimePseudoReloc(Defined *sym, SectionChunk *target, uint32_t targetOffset, 183*bdd1243dSDimitry Andric int flags) 184*bdd1243dSDimitry Andric : sym(sym), target(target), targetOffset(targetOffset), flags(flags) {} 185*bdd1243dSDimitry Andric 186*bdd1243dSDimitry Andric Defined *sym; 187*bdd1243dSDimitry Andric SectionChunk *target; 188*bdd1243dSDimitry Andric uint32_t targetOffset; 189*bdd1243dSDimitry Andric // The Flags field contains the size of the relocation, in bits. No other 190*bdd1243dSDimitry Andric // flags are currently defined. 191*bdd1243dSDimitry Andric int flags; 192*bdd1243dSDimitry Andric }; 193*bdd1243dSDimitry Andric 1940b57cec5SDimitry Andric // A chunk corresponding a section of an input file. 1950b57cec5SDimitry Andric class SectionChunk final : public Chunk { 1960b57cec5SDimitry Andric // Identical COMDAT Folding feature accesses section internal data. 1970b57cec5SDimitry Andric friend class ICF; 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric public: 2000b57cec5SDimitry Andric class symbol_iterator : public llvm::iterator_adaptor_base< 2010b57cec5SDimitry Andric symbol_iterator, const coff_relocation *, 2020b57cec5SDimitry Andric std::random_access_iterator_tag, Symbol *> { 2030b57cec5SDimitry Andric friend SectionChunk; 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric ObjFile *file; 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric symbol_iterator(ObjFile *file, const coff_relocation *i) 2080b57cec5SDimitry Andric : symbol_iterator::iterator_adaptor_base(i), file(file) {} 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric public: 2110b57cec5SDimitry Andric symbol_iterator() = default; 2120b57cec5SDimitry Andric 2130b57cec5SDimitry Andric Symbol *operator*() const { return file->getSymbol(I->SymbolTableIndex); } 2140b57cec5SDimitry Andric }; 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric SectionChunk(ObjFile *file, const coff_section *header); 2170b57cec5SDimitry Andric static bool classof(const Chunk *c) { return c->kind() == SectionKind; } 2180b57cec5SDimitry Andric size_t getSize() const { return header->SizeOfRawData; } 2190b57cec5SDimitry Andric ArrayRef<uint8_t> getContents() const; 2200b57cec5SDimitry Andric void writeTo(uint8_t *buf) const; 2210b57cec5SDimitry Andric 222e8d8bef9SDimitry Andric // Defend against unsorted relocations. This may be overly conservative. 223e8d8bef9SDimitry Andric void sortRelocations(); 224e8d8bef9SDimitry Andric 225e8d8bef9SDimitry Andric // Write and relocate a portion of the section. This is intended to be called 226e8d8bef9SDimitry Andric // in a loop. Relocations must be sorted first. 227e8d8bef9SDimitry Andric void writeAndRelocateSubsection(ArrayRef<uint8_t> sec, 228e8d8bef9SDimitry Andric ArrayRef<uint8_t> subsec, 229e8d8bef9SDimitry Andric uint32_t &nextRelocIndex, uint8_t *buf) const; 230e8d8bef9SDimitry Andric 2310b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const { 2320b57cec5SDimitry Andric return header->Characteristics & (permMask | typeMask); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric StringRef getSectionName() const { 2350b57cec5SDimitry Andric return StringRef(sectionNameData, sectionNameSize); 2360b57cec5SDimitry Andric } 2370b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res); 2380b57cec5SDimitry Andric bool isCOMDAT() const; 239e8d8bef9SDimitry Andric void applyRelocation(uint8_t *off, const coff_relocation &rel) const; 2400b57cec5SDimitry Andric void applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, 241*bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const; 2420b57cec5SDimitry Andric void applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, 243*bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const; 2440b57cec5SDimitry Andric void applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, 245*bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const; 2460b57cec5SDimitry Andric void applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s, 247*bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const; 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &res); 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // Called if the garbage collector decides to not include this chunk 2520b57cec5SDimitry Andric // in a final output. It's supposed to print out a log message to stdout. 2530b57cec5SDimitry Andric void printDiscardedMessage() const; 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric // Adds COMDAT associative sections to this COMDAT section. A chunk 2560b57cec5SDimitry Andric // and its children are treated as a group by the garbage collector. 2570b57cec5SDimitry Andric void addAssociative(SectionChunk *child); 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric StringRef getDebugName() const; 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric // True if this is a codeview debug info chunk. These will not be laid out in 2620b57cec5SDimitry Andric // the image. Instead they will end up in the PDB, if one is requested. 2630b57cec5SDimitry Andric bool isCodeView() const { 2640b57cec5SDimitry Andric return getSectionName() == ".debug" || getSectionName().startswith(".debug$"); 2650b57cec5SDimitry Andric } 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric // True if this is a DWARF debug info or exception handling chunk. 2680b57cec5SDimitry Andric bool isDWARF() const { 2690b57cec5SDimitry Andric return getSectionName().startswith(".debug_") || getSectionName() == ".eh_frame"; 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric // Allow iteration over the bodies of this chunk's relocated symbols. 2730b57cec5SDimitry Andric llvm::iterator_range<symbol_iterator> symbols() const { 2740b57cec5SDimitry Andric return llvm::make_range(symbol_iterator(file, relocsData), 2750b57cec5SDimitry Andric symbol_iterator(file, relocsData + relocsSize)); 2760b57cec5SDimitry Andric } 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric ArrayRef<coff_relocation> getRelocs() const { 279*bdd1243dSDimitry Andric return llvm::ArrayRef(relocsData, relocsSize); 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric // Reloc setter used by ARM range extension thunk insertion. 2830b57cec5SDimitry Andric void setRelocs(ArrayRef<coff_relocation> newRelocs) { 2840b57cec5SDimitry Andric relocsData = newRelocs.data(); 2850b57cec5SDimitry Andric relocsSize = newRelocs.size(); 2860b57cec5SDimitry Andric assert(relocsSize == newRelocs.size() && "reloc size truncation"); 2870b57cec5SDimitry Andric } 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric // Single linked list iterator for associated comdat children. 2900b57cec5SDimitry Andric class AssociatedIterator 2910b57cec5SDimitry Andric : public llvm::iterator_facade_base< 2920b57cec5SDimitry Andric AssociatedIterator, std::forward_iterator_tag, SectionChunk> { 2930b57cec5SDimitry Andric public: 2940b57cec5SDimitry Andric AssociatedIterator() = default; 2950b57cec5SDimitry Andric AssociatedIterator(SectionChunk *head) : cur(head) {} 2960b57cec5SDimitry Andric bool operator==(const AssociatedIterator &r) const { return cur == r.cur; } 2975ffd83dbSDimitry Andric // FIXME: Wrong const-ness, but it makes filter ranges work. 2985ffd83dbSDimitry Andric SectionChunk &operator*() const { return *cur; } 2990b57cec5SDimitry Andric SectionChunk &operator*() { return *cur; } 3000b57cec5SDimitry Andric AssociatedIterator &operator++() { 3010b57cec5SDimitry Andric cur = cur->assocChildren; 3020b57cec5SDimitry Andric return *this; 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric private: 3060b57cec5SDimitry Andric SectionChunk *cur = nullptr; 3070b57cec5SDimitry Andric }; 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric // Allow iteration over the associated child chunks for this section. 3100b57cec5SDimitry Andric llvm::iterator_range<AssociatedIterator> children() const { 311fe6060f1SDimitry Andric // Associated sections do not have children. The assocChildren field is 312fe6060f1SDimitry Andric // part of the parent's list of children. 313fe6060f1SDimitry Andric bool isAssoc = selection == llvm::COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE; 314fe6060f1SDimitry Andric return llvm::make_range( 315fe6060f1SDimitry Andric AssociatedIterator(isAssoc ? nullptr : assocChildren), 3160b57cec5SDimitry Andric AssociatedIterator(nullptr)); 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric // The section ID this chunk belongs to in its Obj. 3200b57cec5SDimitry Andric uint32_t getSectionNumber() const; 3210b57cec5SDimitry Andric 3220b57cec5SDimitry Andric ArrayRef<uint8_t> consumeDebugMagic(); 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> data, 3250b57cec5SDimitry Andric StringRef sectionName); 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric static SectionChunk *findByName(ArrayRef<SectionChunk *> sections, 3280b57cec5SDimitry Andric StringRef name); 3290b57cec5SDimitry Andric 3300b57cec5SDimitry Andric // The file that this chunk was created from. 3310b57cec5SDimitry Andric ObjFile *file; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric // Pointer to the COFF section header in the input file. 3340b57cec5SDimitry Andric const coff_section *header; 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // The COMDAT leader symbol if this is a COMDAT chunk. 3370b57cec5SDimitry Andric DefinedRegular *sym = nullptr; 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric // The CRC of the contents as described in the COFF spec 4.5.5. 3400b57cec5SDimitry Andric // Auxiliary Format 5: Section Definitions. Used for ICF. 3410b57cec5SDimitry Andric uint32_t checksum = 0; 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric // Used by the garbage collector. 3440b57cec5SDimitry Andric bool live; 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric // Whether this section needs to be kept distinct from other sections during 3470b57cec5SDimitry Andric // ICF. This is set by the driver using address-significance tables. 3480b57cec5SDimitry Andric bool keepUnique = false; 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric // The COMDAT selection if this is a COMDAT chunk. 3510b57cec5SDimitry Andric llvm::COFF::COMDATType selection = (llvm::COFF::COMDATType)0; 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric // A pointer pointing to a replacement for this chunk. 3540b57cec5SDimitry Andric // Initially it points to "this" object. If this chunk is merged 3550b57cec5SDimitry Andric // with other chunk by ICF, it points to another chunk, 3560b57cec5SDimitry Andric // and this chunk is considered as dead. 3570b57cec5SDimitry Andric SectionChunk *repl; 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric private: 3600b57cec5SDimitry Andric SectionChunk *assocChildren = nullptr; 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // Used for ICF (Identical COMDAT Folding) 3630b57cec5SDimitry Andric void replace(SectionChunk *other); 3640b57cec5SDimitry Andric uint32_t eqClass[2] = {0, 0}; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric // Relocations for this section. Size is stored below. 3670b57cec5SDimitry Andric const coff_relocation *relocsData; 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric // Section name string. Size is stored below. 3700b57cec5SDimitry Andric const char *sectionNameData; 3710b57cec5SDimitry Andric 3720b57cec5SDimitry Andric uint32_t relocsSize = 0; 3730b57cec5SDimitry Andric uint32_t sectionNameSize = 0; 3740b57cec5SDimitry Andric }; 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric // Inline methods to implement faux-virtual dispatch for SectionChunk. 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric inline size_t Chunk::getSize() const { 3790b57cec5SDimitry Andric if (isa<SectionChunk>(this)) 3800b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getSize(); 3810b57cec5SDimitry Andric else 3820b57cec5SDimitry Andric return static_cast<const NonSectionChunk *>(this)->getSize(); 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric inline uint32_t Chunk::getOutputCharacteristics() const { 3860b57cec5SDimitry Andric if (isa<SectionChunk>(this)) 3870b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getOutputCharacteristics(); 3880b57cec5SDimitry Andric else 3890b57cec5SDimitry Andric return static_cast<const NonSectionChunk *>(this) 3900b57cec5SDimitry Andric ->getOutputCharacteristics(); 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric inline void Chunk::writeTo(uint8_t *buf) const { 3940b57cec5SDimitry Andric if (isa<SectionChunk>(this)) 3950b57cec5SDimitry Andric static_cast<const SectionChunk *>(this)->writeTo(buf); 3960b57cec5SDimitry Andric else 3970b57cec5SDimitry Andric static_cast<const NonSectionChunk *>(this)->writeTo(buf); 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric inline StringRef Chunk::getSectionName() const { 4010b57cec5SDimitry Andric if (isa<SectionChunk>(this)) 4020b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getSectionName(); 4030b57cec5SDimitry Andric else 4040b57cec5SDimitry Andric return static_cast<const NonSectionChunk *>(this)->getSectionName(); 4050b57cec5SDimitry Andric } 4060b57cec5SDimitry Andric 4070b57cec5SDimitry Andric inline void Chunk::getBaserels(std::vector<Baserel> *res) { 4080b57cec5SDimitry Andric if (isa<SectionChunk>(this)) 4090b57cec5SDimitry Andric static_cast<SectionChunk *>(this)->getBaserels(res); 4100b57cec5SDimitry Andric else 4110b57cec5SDimitry Andric static_cast<NonSectionChunk *>(this)->getBaserels(res); 4120b57cec5SDimitry Andric } 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric inline StringRef Chunk::getDebugName() const { 4150b57cec5SDimitry Andric if (isa<SectionChunk>(this)) 4160b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getDebugName(); 4170b57cec5SDimitry Andric else 4180b57cec5SDimitry Andric return static_cast<const NonSectionChunk *>(this)->getDebugName(); 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // This class is used to implement an lld-specific feature (not implemented in 4220b57cec5SDimitry Andric // MSVC) that minimizes the output size by finding string literals sharing tail 4230b57cec5SDimitry Andric // parts and merging them. 4240b57cec5SDimitry Andric // 4250b57cec5SDimitry Andric // If string tail merging is enabled and a section is identified as containing a 4260b57cec5SDimitry Andric // string literal, it is added to a MergeChunk with an appropriate alignment. 4270b57cec5SDimitry Andric // The MergeChunk then tail merges the strings using the StringTableBuilder 4280b57cec5SDimitry Andric // class and assigns RVAs and section offsets to each of the member chunks based 4290b57cec5SDimitry Andric // on the offsets assigned by the StringTableBuilder. 4300b57cec5SDimitry Andric class MergeChunk : public NonSectionChunk { 4310b57cec5SDimitry Andric public: 4320b57cec5SDimitry Andric MergeChunk(uint32_t alignment); 433349cc55cSDimitry Andric static void addSection(COFFLinkerContext &ctx, SectionChunk *c); 4340b57cec5SDimitry Andric void finalizeContents(); 4350b57cec5SDimitry Andric void assignSubsectionRVAs(); 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const override; 4380b57cec5SDimitry Andric StringRef getSectionName() const override { return ".rdata"; } 4390b57cec5SDimitry Andric size_t getSize() const override; 4400b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric std::vector<SectionChunk *> sections; 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric private: 4450b57cec5SDimitry Andric llvm::StringTableBuilder builder; 4460b57cec5SDimitry Andric bool finalized = false; 4470b57cec5SDimitry Andric }; 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric // A chunk for common symbols. Common chunks don't have actual data. 4500b57cec5SDimitry Andric class CommonChunk : public NonSectionChunk { 4510b57cec5SDimitry Andric public: 4520b57cec5SDimitry Andric CommonChunk(const COFFSymbolRef sym); 4530b57cec5SDimitry Andric size_t getSize() const override { return sym.getValue(); } 4540b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const override; 4550b57cec5SDimitry Andric StringRef getSectionName() const override { return ".bss"; } 4560b57cec5SDimitry Andric 4570b57cec5SDimitry Andric private: 4580b57cec5SDimitry Andric const COFFSymbolRef sym; 4590b57cec5SDimitry Andric }; 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric // A chunk for linker-created strings. 4620b57cec5SDimitry Andric class StringChunk : public NonSectionChunk { 4630b57cec5SDimitry Andric public: 4640b57cec5SDimitry Andric explicit StringChunk(StringRef s) : str(s) {} 4650b57cec5SDimitry Andric size_t getSize() const override { return str.size() + 1; } 4660b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric private: 4690b57cec5SDimitry Andric StringRef str; 4700b57cec5SDimitry Andric }; 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric static const uint8_t importThunkX86[] = { 4730b57cec5SDimitry Andric 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0 4740b57cec5SDimitry Andric }; 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric static const uint8_t importThunkARM[] = { 4770b57cec5SDimitry Andric 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0 4780b57cec5SDimitry Andric 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0 4790b57cec5SDimitry Andric 0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip] 4800b57cec5SDimitry Andric }; 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric static const uint8_t importThunkARM64[] = { 4830b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, #0 4840b57cec5SDimitry Andric 0x10, 0x02, 0x40, 0xf9, // ldr x16, [x16] 4850b57cec5SDimitry Andric 0x00, 0x02, 0x1f, 0xd6, // br x16 4860b57cec5SDimitry Andric }; 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric // Windows-specific. 4890b57cec5SDimitry Andric // A chunk for DLL import jump table entry. In a final output, its 4900b57cec5SDimitry Andric // contents will be a JMP instruction to some __imp_ symbol. 4910b57cec5SDimitry Andric class ImportThunkChunk : public NonSectionChunk { 4920b57cec5SDimitry Andric public: 493*bdd1243dSDimitry Andric ImportThunkChunk(COFFLinkerContext &ctx, Defined *s) 494*bdd1243dSDimitry Andric : NonSectionChunk(ImportThunkKind), impSymbol(s), ctx(ctx) {} 4950b57cec5SDimitry Andric static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; } 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric protected: 4980b57cec5SDimitry Andric Defined *impSymbol; 499*bdd1243dSDimitry Andric COFFLinkerContext &ctx; 5000b57cec5SDimitry Andric }; 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric class ImportThunkChunkX64 : public ImportThunkChunk { 5030b57cec5SDimitry Andric public: 504*bdd1243dSDimitry Andric explicit ImportThunkChunkX64(COFFLinkerContext &ctx, Defined *s); 5050b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkX86); } 5060b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 5070b57cec5SDimitry Andric }; 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric class ImportThunkChunkX86 : public ImportThunkChunk { 5100b57cec5SDimitry Andric public: 511*bdd1243dSDimitry Andric explicit ImportThunkChunkX86(COFFLinkerContext &ctx, Defined *s) 512*bdd1243dSDimitry Andric : ImportThunkChunk(ctx, s) {} 5130b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkX86); } 5140b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res) override; 5150b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 5160b57cec5SDimitry Andric }; 5170b57cec5SDimitry Andric 5180b57cec5SDimitry Andric class ImportThunkChunkARM : public ImportThunkChunk { 5190b57cec5SDimitry Andric public: 520*bdd1243dSDimitry Andric explicit ImportThunkChunkARM(COFFLinkerContext &ctx, Defined *s) 521*bdd1243dSDimitry Andric : ImportThunkChunk(ctx, s) { 522e837bb5cSDimitry Andric setAlignment(2); 523e837bb5cSDimitry Andric } 5240b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkARM); } 5250b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res) override; 5260b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 5270b57cec5SDimitry Andric }; 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric class ImportThunkChunkARM64 : public ImportThunkChunk { 5300b57cec5SDimitry Andric public: 531*bdd1243dSDimitry Andric explicit ImportThunkChunkARM64(COFFLinkerContext &ctx, Defined *s) 532*bdd1243dSDimitry Andric : ImportThunkChunk(ctx, s) { 533e837bb5cSDimitry Andric setAlignment(4); 534e837bb5cSDimitry Andric } 5350b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkARM64); } 5360b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 5370b57cec5SDimitry Andric }; 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric class RangeExtensionThunkARM : public NonSectionChunk { 5400b57cec5SDimitry Andric public: 541*bdd1243dSDimitry Andric explicit RangeExtensionThunkARM(COFFLinkerContext &ctx, Defined *t) 542*bdd1243dSDimitry Andric : target(t), ctx(ctx) { 543*bdd1243dSDimitry Andric setAlignment(2); 544*bdd1243dSDimitry Andric } 5450b57cec5SDimitry Andric size_t getSize() const override; 5460b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric Defined *target; 549*bdd1243dSDimitry Andric 550*bdd1243dSDimitry Andric private: 551*bdd1243dSDimitry Andric COFFLinkerContext &ctx; 5520b57cec5SDimitry Andric }; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric class RangeExtensionThunkARM64 : public NonSectionChunk { 5550b57cec5SDimitry Andric public: 556*bdd1243dSDimitry Andric explicit RangeExtensionThunkARM64(COFFLinkerContext &ctx, Defined *t) 557*bdd1243dSDimitry Andric : target(t), ctx(ctx) { 558*bdd1243dSDimitry Andric setAlignment(4); 559*bdd1243dSDimitry Andric } 5600b57cec5SDimitry Andric size_t getSize() const override; 5610b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric Defined *target; 564*bdd1243dSDimitry Andric 565*bdd1243dSDimitry Andric private: 566*bdd1243dSDimitry Andric COFFLinkerContext &ctx; 5670b57cec5SDimitry Andric }; 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric // Windows-specific. 5700b57cec5SDimitry Andric // See comments for DefinedLocalImport class. 5710b57cec5SDimitry Andric class LocalImportChunk : public NonSectionChunk { 5720b57cec5SDimitry Andric public: 573*bdd1243dSDimitry Andric explicit LocalImportChunk(COFFLinkerContext &ctx, Defined *s); 5740b57cec5SDimitry Andric size_t getSize() const override; 5750b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res) override; 5760b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric private: 5790b57cec5SDimitry Andric Defined *sym; 580*bdd1243dSDimitry Andric COFFLinkerContext &ctx; 5810b57cec5SDimitry Andric }; 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric // Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and 5840b57cec5SDimitry Andric // offset into the chunk. Order does not matter as the RVA table will be sorted 5850b57cec5SDimitry Andric // later. 5860b57cec5SDimitry Andric struct ChunkAndOffset { 5870b57cec5SDimitry Andric Chunk *inputChunk; 5880b57cec5SDimitry Andric uint32_t offset; 5890b57cec5SDimitry Andric 5900b57cec5SDimitry Andric struct DenseMapInfo { 5910b57cec5SDimitry Andric static ChunkAndOffset getEmptyKey() { 5920b57cec5SDimitry Andric return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0}; 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric static ChunkAndOffset getTombstoneKey() { 5950b57cec5SDimitry Andric return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0}; 5960b57cec5SDimitry Andric } 5970b57cec5SDimitry Andric static unsigned getHashValue(const ChunkAndOffset &co) { 5980b57cec5SDimitry Andric return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue( 5990b57cec5SDimitry Andric {co.inputChunk, co.offset}); 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric static bool isEqual(const ChunkAndOffset &lhs, const ChunkAndOffset &rhs) { 6020b57cec5SDimitry Andric return lhs.inputChunk == rhs.inputChunk && lhs.offset == rhs.offset; 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric }; 6050b57cec5SDimitry Andric }; 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>; 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric // Table which contains symbol RVAs. Used for /safeseh and /guard:cf. 6100b57cec5SDimitry Andric class RVATableChunk : public NonSectionChunk { 6110b57cec5SDimitry Andric public: 6120b57cec5SDimitry Andric explicit RVATableChunk(SymbolRVASet s) : syms(std::move(s)) {} 6130b57cec5SDimitry Andric size_t getSize() const override { return syms.size() * 4; } 6140b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric private: 6170b57cec5SDimitry Andric SymbolRVASet syms; 6180b57cec5SDimitry Andric }; 6190b57cec5SDimitry Andric 620fe6060f1SDimitry Andric // Table which contains symbol RVAs with flags. Used for /guard:ehcont. 621fe6060f1SDimitry Andric class RVAFlagTableChunk : public NonSectionChunk { 622fe6060f1SDimitry Andric public: 623fe6060f1SDimitry Andric explicit RVAFlagTableChunk(SymbolRVASet s) : syms(std::move(s)) {} 624fe6060f1SDimitry Andric size_t getSize() const override { return syms.size() * 5; } 625fe6060f1SDimitry Andric void writeTo(uint8_t *buf) const override; 626fe6060f1SDimitry Andric 627fe6060f1SDimitry Andric private: 628fe6060f1SDimitry Andric SymbolRVASet syms; 629fe6060f1SDimitry Andric }; 630fe6060f1SDimitry Andric 6310b57cec5SDimitry Andric // Windows-specific. 6320b57cec5SDimitry Andric // This class represents a block in .reloc section. 6330b57cec5SDimitry Andric // See the PE/COFF spec 5.6 for details. 6340b57cec5SDimitry Andric class BaserelChunk : public NonSectionChunk { 6350b57cec5SDimitry Andric public: 6360b57cec5SDimitry Andric BaserelChunk(uint32_t page, Baserel *begin, Baserel *end); 6370b57cec5SDimitry Andric size_t getSize() const override { return data.size(); } 6380b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric private: 6410b57cec5SDimitry Andric std::vector<uint8_t> data; 6420b57cec5SDimitry Andric }; 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric class Baserel { 6450b57cec5SDimitry Andric public: 6460b57cec5SDimitry Andric Baserel(uint32_t v, uint8_t ty) : rva(v), type(ty) {} 647*bdd1243dSDimitry Andric explicit Baserel(uint32_t v, llvm::COFF::MachineTypes machine) 648*bdd1243dSDimitry Andric : Baserel(v, getDefaultType(machine)) {} 649*bdd1243dSDimitry Andric uint8_t getDefaultType(llvm::COFF::MachineTypes machine); 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric uint32_t rva; 6520b57cec5SDimitry Andric uint8_t type; 6530b57cec5SDimitry Andric }; 6540b57cec5SDimitry Andric 6550b57cec5SDimitry Andric // This is a placeholder Chunk, to allow attaching a DefinedSynthetic to a 6560b57cec5SDimitry Andric // specific place in a section, without any data. This is used for the MinGW 6570b57cec5SDimitry Andric // specific symbol __RUNTIME_PSEUDO_RELOC_LIST_END__, even though the concept 6580b57cec5SDimitry Andric // of an empty chunk isn't MinGW specific. 6590b57cec5SDimitry Andric class EmptyChunk : public NonSectionChunk { 6600b57cec5SDimitry Andric public: 6610b57cec5SDimitry Andric EmptyChunk() {} 6620b57cec5SDimitry Andric size_t getSize() const override { return 0; } 6630b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override {} 6640b57cec5SDimitry Andric }; 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric // MinGW specific, for the "automatic import of variables from DLLs" feature. 6670b57cec5SDimitry Andric // This provides the table of runtime pseudo relocations, for variable 6680b57cec5SDimitry Andric // references that turned out to need to be imported from a DLL even though 6690b57cec5SDimitry Andric // the reference didn't use the dllimport attribute. The MinGW runtime will 6700b57cec5SDimitry Andric // process this table after loading, before handling control over to user 6710b57cec5SDimitry Andric // code. 6720b57cec5SDimitry Andric class PseudoRelocTableChunk : public NonSectionChunk { 6730b57cec5SDimitry Andric public: 6740b57cec5SDimitry Andric PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &relocs) 6750b57cec5SDimitry Andric : relocs(std::move(relocs)) { 6760b57cec5SDimitry Andric setAlignment(4); 6770b57cec5SDimitry Andric } 6780b57cec5SDimitry Andric size_t getSize() const override; 6790b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric private: 6820b57cec5SDimitry Andric std::vector<RuntimePseudoReloc> relocs; 6830b57cec5SDimitry Andric }; 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric // MinGW specific. A Chunk that contains one pointer-sized absolute value. 6860b57cec5SDimitry Andric class AbsolutePointerChunk : public NonSectionChunk { 6870b57cec5SDimitry Andric public: 688*bdd1243dSDimitry Andric AbsolutePointerChunk(COFFLinkerContext &ctx, uint64_t value) 689*bdd1243dSDimitry Andric : value(value), ctx(ctx) { 6900b57cec5SDimitry Andric setAlignment(getSize()); 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric size_t getSize() const override; 6930b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override; 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric private: 6960b57cec5SDimitry Andric uint64_t value; 697*bdd1243dSDimitry Andric COFFLinkerContext &ctx; 6980b57cec5SDimitry Andric }; 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric // Return true if this file has the hotpatch flag set to true in the S_COMPILE3 7010b57cec5SDimitry Andric // record in codeview debug info. Also returns true for some thunks synthesized 7020b57cec5SDimitry Andric // by the linker. 7030b57cec5SDimitry Andric inline bool Chunk::isHotPatchable() const { 7040b57cec5SDimitry Andric if (auto *sc = dyn_cast<SectionChunk>(this)) 7050b57cec5SDimitry Andric return sc->file->hotPatchable; 7060b57cec5SDimitry Andric else if (isa<ImportThunkChunk>(this)) 7070b57cec5SDimitry Andric return true; 7080b57cec5SDimitry Andric return false; 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric void applyMOV32T(uint8_t *off, uint32_t v); 7120b57cec5SDimitry Andric void applyBranch24T(uint8_t *off, int32_t v); 7130b57cec5SDimitry Andric 7140b57cec5SDimitry Andric void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift); 7150b57cec5SDimitry Andric void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit); 7160b57cec5SDimitry Andric void applyArm64Branch26(uint8_t *off, int64_t v); 7170b57cec5SDimitry Andric 718*bdd1243dSDimitry Andric // Convenience class for initializing a coff_section with specific flags. 719*bdd1243dSDimitry Andric class FakeSection { 720*bdd1243dSDimitry Andric public: 721*bdd1243dSDimitry Andric FakeSection(int c) { section.Characteristics = c; } 722*bdd1243dSDimitry Andric 723*bdd1243dSDimitry Andric coff_section section; 724*bdd1243dSDimitry Andric }; 725*bdd1243dSDimitry Andric 726*bdd1243dSDimitry Andric // Convenience class for initializing a SectionChunk with specific flags. 727*bdd1243dSDimitry Andric class FakeSectionChunk { 728*bdd1243dSDimitry Andric public: 729*bdd1243dSDimitry Andric FakeSectionChunk(const coff_section *section) : chunk(nullptr, section) { 730*bdd1243dSDimitry Andric // Comdats from LTO files can't be fully treated as regular comdats 731*bdd1243dSDimitry Andric // at this point; we don't know what size or contents they are going to 732*bdd1243dSDimitry Andric // have, so we can't do proper checking of such aspects of them. 733*bdd1243dSDimitry Andric chunk.selection = llvm::COFF::IMAGE_COMDAT_SELECT_ANY; 734*bdd1243dSDimitry Andric } 735*bdd1243dSDimitry Andric 736*bdd1243dSDimitry Andric SectionChunk chunk; 737*bdd1243dSDimitry Andric }; 738*bdd1243dSDimitry Andric 739*bdd1243dSDimitry Andric } // namespace lld::coff 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric namespace llvm { 7420b57cec5SDimitry Andric template <> 7430b57cec5SDimitry Andric struct DenseMapInfo<lld::coff::ChunkAndOffset> 7440b57cec5SDimitry Andric : lld::coff::ChunkAndOffset::DenseMapInfo {}; 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric #endif 748