xref: /freebsd/contrib/llvm-project/lld/COFF/Chunks.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 
240b57cec5SDimitry Andric namespace lld {
250b57cec5SDimitry Andric namespace coff {
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric using llvm::COFF::ImportDirectoryTableEntry;
280b57cec5SDimitry Andric using llvm::object::COFFSymbolRef;
290b57cec5SDimitry Andric using llvm::object::SectionRef;
300b57cec5SDimitry Andric using llvm::object::coff_relocation;
310b57cec5SDimitry Andric using llvm::object::coff_section;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric class Baserel;
340b57cec5SDimitry Andric class Defined;
350b57cec5SDimitry Andric class DefinedImportData;
360b57cec5SDimitry Andric class DefinedRegular;
370b57cec5SDimitry Andric class ObjFile;
380b57cec5SDimitry Andric class OutputSection;
390b57cec5SDimitry Andric class RuntimePseudoReloc;
400b57cec5SDimitry Andric class Symbol;
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric // Mask for permissions (discardable, writable, readable, executable, etc).
430b57cec5SDimitry Andric const uint32_t permMask = 0xFE000000;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric // Mask for section types (code, data, bss).
460b57cec5SDimitry Andric const uint32_t typeMask = 0x000000E0;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric // The log base 2 of the largest section alignment, which is log2(8192), or 13.
490b57cec5SDimitry Andric enum : unsigned { Log2MaxSectionAlignment = 13 };
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric // A Chunk represents a chunk of data that will occupy space in the
520b57cec5SDimitry Andric // output (if the resolver chose that). It may or may not be backed by
530b57cec5SDimitry Andric // a section of an input file. It could be linker-created data, or
540b57cec5SDimitry Andric // doesn't even have actual data (if common or bss).
550b57cec5SDimitry Andric class Chunk {
560b57cec5SDimitry Andric public:
570b57cec5SDimitry Andric   enum Kind : uint8_t { SectionKind, OtherKind, ImportThunkKind };
580b57cec5SDimitry Andric   Kind kind() const { return chunkKind; }
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   // Returns the size of this chunk (even if this is a common or BSS.)
610b57cec5SDimitry Andric   size_t getSize() const;
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   // Returns chunk alignment in power of two form. Value values are powers of
640b57cec5SDimitry Andric   // two from 1 to 8192.
650b57cec5SDimitry Andric   uint32_t getAlignment() const { return 1U << p2Align; }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   // Update the chunk section alignment measured in bytes. Internally alignment
680b57cec5SDimitry Andric   // is stored in log2.
690b57cec5SDimitry Andric   void setAlignment(uint32_t align) {
700b57cec5SDimitry Andric     // Treat zero byte alignment as 1 byte alignment.
710b57cec5SDimitry Andric     align = align ? align : 1;
720b57cec5SDimitry Andric     assert(llvm::isPowerOf2_32(align) && "alignment is not a power of 2");
730b57cec5SDimitry Andric     p2Align = llvm::Log2_32(align);
740b57cec5SDimitry Andric     assert(p2Align <= Log2MaxSectionAlignment &&
750b57cec5SDimitry Andric            "impossible requested alignment");
760b57cec5SDimitry Andric   }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   // Write this chunk to a mmap'ed file, assuming Buf is pointing to
790b57cec5SDimitry Andric   // beginning of the file. Because this function may use RVA values
800b57cec5SDimitry Andric   // of other chunks for relocations, you need to set them properly
810b57cec5SDimitry Andric   // before calling this function.
820b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric   // The writer sets and uses the addresses. In practice, PE images cannot be
850b57cec5SDimitry Andric   // larger than 2GB. Chunks are always laid as part of the image, so Chunk RVAs
860b57cec5SDimitry Andric   // can be stored with 32 bits.
870b57cec5SDimitry Andric   uint32_t getRVA() const { return rva; }
880b57cec5SDimitry Andric   void setRVA(uint64_t v) {
890b57cec5SDimitry Andric     rva = (uint32_t)v;
900b57cec5SDimitry Andric     assert(rva == v && "RVA truncated");
910b57cec5SDimitry Andric   }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric   // Returns readable/writable/executable bits.
940b57cec5SDimitry Andric   uint32_t getOutputCharacteristics() const;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   // Returns the section name if this is a section chunk.
970b57cec5SDimitry Andric   // It is illegal to call this function on non-section chunks.
980b57cec5SDimitry Andric   StringRef getSectionName() const;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric   // An output section has pointers to chunks in the section, and each
1010b57cec5SDimitry Andric   // chunk has a back pointer to an output section.
1020b57cec5SDimitry Andric   void setOutputSectionIdx(uint16_t o) { osidx = o; }
1030b57cec5SDimitry Andric   uint16_t getOutputSectionIdx() const { return osidx; }
1040b57cec5SDimitry Andric   OutputSection *getOutputSection() const;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   // Windows-specific.
1070b57cec5SDimitry Andric   // Collect all locations that contain absolute addresses for base relocations.
1080b57cec5SDimitry Andric   void getBaserels(std::vector<Baserel> *res);
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
1110b57cec5SDimitry Andric   // bytes, so this is used only for logging or debugging.
1120b57cec5SDimitry Andric   StringRef getDebugName() const;
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric   // Return true if this file has the hotpatch flag set to true in the
1150b57cec5SDimitry Andric   // S_COMPILE3 record in codeview debug info. Also returns true for some thunks
1160b57cec5SDimitry Andric   // synthesized by the linker.
1170b57cec5SDimitry Andric   bool isHotPatchable() const;
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric protected:
1200b57cec5SDimitry Andric   Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {}
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   const Kind chunkKind;
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric public:
1250b57cec5SDimitry Andric   // Returns true if this has non-zero data. BSS chunks return
1260b57cec5SDimitry Andric   // false. If false is returned, the space occupied by this chunk
1270b57cec5SDimitry Andric   // will be filled with zeros. Corresponds to the
1280b57cec5SDimitry Andric   // IMAGE_SCN_CNT_UNINITIALIZED_DATA section characteristic bit.
1290b57cec5SDimitry Andric   uint8_t hasData : 1;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric public:
1320b57cec5SDimitry Andric   // The alignment of this chunk, stored in log2 form. The writer uses the
1330b57cec5SDimitry Andric   // value.
1340b57cec5SDimitry Andric   uint8_t p2Align : 7;
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric   // The output section index for this chunk. The first valid section number is
1370b57cec5SDimitry Andric   // one.
1380b57cec5SDimitry Andric   uint16_t osidx = 0;
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   // The RVA of this chunk in the output. The writer sets a value.
1410b57cec5SDimitry Andric   uint32_t rva = 0;
1420b57cec5SDimitry Andric };
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric class NonSectionChunk : public Chunk {
1450b57cec5SDimitry Andric public:
1460b57cec5SDimitry Andric   virtual ~NonSectionChunk() = default;
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric   // Returns the size of this chunk (even if this is a common or BSS.)
1490b57cec5SDimitry Andric   virtual size_t getSize() const = 0;
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   virtual uint32_t getOutputCharacteristics() const { return 0; }
1520b57cec5SDimitry Andric 
1530b57cec5SDimitry Andric   // Write this chunk to a mmap'ed file, assuming Buf is pointing to
1540b57cec5SDimitry Andric   // beginning of the file. Because this function may use RVA values
1550b57cec5SDimitry Andric   // of other chunks for relocations, you need to set them properly
1560b57cec5SDimitry Andric   // before calling this function.
1570b57cec5SDimitry Andric   virtual void writeTo(uint8_t *buf) const {}
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   // Returns the section name if this is a section chunk.
1600b57cec5SDimitry Andric   // It is illegal to call this function on non-section chunks.
1610b57cec5SDimitry Andric   virtual StringRef getSectionName() const {
1620b57cec5SDimitry Andric     llvm_unreachable("unimplemented getSectionName");
1630b57cec5SDimitry Andric   }
1640b57cec5SDimitry Andric 
1650b57cec5SDimitry Andric   // Windows-specific.
1660b57cec5SDimitry Andric   // Collect all locations that contain absolute addresses for base relocations.
1670b57cec5SDimitry Andric   virtual void getBaserels(std::vector<Baserel> *res) {}
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
1700b57cec5SDimitry Andric   // bytes, so this is used only for logging or debugging.
1710b57cec5SDimitry Andric   virtual StringRef getDebugName() const { return ""; }
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric   static bool classof(const Chunk *c) { return c->kind() != SectionKind; }
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric protected:
1760b57cec5SDimitry Andric   NonSectionChunk(Kind k = OtherKind) : Chunk(k) {}
1770b57cec5SDimitry Andric };
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric // A chunk corresponding a section of an input file.
1800b57cec5SDimitry Andric class SectionChunk final : public Chunk {
1810b57cec5SDimitry Andric   // Identical COMDAT Folding feature accesses section internal data.
1820b57cec5SDimitry Andric   friend class ICF;
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric public:
1850b57cec5SDimitry Andric   class symbol_iterator : public llvm::iterator_adaptor_base<
1860b57cec5SDimitry Andric                               symbol_iterator, const coff_relocation *,
1870b57cec5SDimitry Andric                               std::random_access_iterator_tag, Symbol *> {
1880b57cec5SDimitry Andric     friend SectionChunk;
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric     ObjFile *file;
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric     symbol_iterator(ObjFile *file, const coff_relocation *i)
1930b57cec5SDimitry Andric         : symbol_iterator::iterator_adaptor_base(i), file(file) {}
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   public:
1960b57cec5SDimitry Andric     symbol_iterator() = default;
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric     Symbol *operator*() const { return file->getSymbol(I->SymbolTableIndex); }
1990b57cec5SDimitry Andric   };
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   SectionChunk(ObjFile *file, const coff_section *header);
2020b57cec5SDimitry Andric   static bool classof(const Chunk *c) { return c->kind() == SectionKind; }
2030b57cec5SDimitry Andric   size_t getSize() const { return header->SizeOfRawData; }
2040b57cec5SDimitry Andric   ArrayRef<uint8_t> getContents() const;
2050b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const;
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   uint32_t getOutputCharacteristics() const {
2080b57cec5SDimitry Andric     return header->Characteristics & (permMask | typeMask);
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric   StringRef getSectionName() const {
2110b57cec5SDimitry Andric     return StringRef(sectionNameData, sectionNameSize);
2120b57cec5SDimitry Andric   }
2130b57cec5SDimitry Andric   void getBaserels(std::vector<Baserel> *res);
2140b57cec5SDimitry Andric   bool isCOMDAT() const;
2150b57cec5SDimitry Andric   void applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
2160b57cec5SDimitry Andric                    uint64_t p) const;
2170b57cec5SDimitry Andric   void applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
2180b57cec5SDimitry Andric                    uint64_t p) const;
2190b57cec5SDimitry Andric   void applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
2200b57cec5SDimitry Andric                    uint64_t p) const;
2210b57cec5SDimitry Andric   void applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
2220b57cec5SDimitry Andric                      uint64_t p) const;
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &res);
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   // Called if the garbage collector decides to not include this chunk
2270b57cec5SDimitry Andric   // in a final output. It's supposed to print out a log message to stdout.
2280b57cec5SDimitry Andric   void printDiscardedMessage() const;
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric   // Adds COMDAT associative sections to this COMDAT section. A chunk
2310b57cec5SDimitry Andric   // and its children are treated as a group by the garbage collector.
2320b57cec5SDimitry Andric   void addAssociative(SectionChunk *child);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   StringRef getDebugName() const;
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric   // True if this is a codeview debug info chunk. These will not be laid out in
2370b57cec5SDimitry Andric   // the image. Instead they will end up in the PDB, if one is requested.
2380b57cec5SDimitry Andric   bool isCodeView() const {
2390b57cec5SDimitry Andric     return getSectionName() == ".debug" || getSectionName().startswith(".debug$");
2400b57cec5SDimitry Andric   }
2410b57cec5SDimitry Andric 
2420b57cec5SDimitry Andric   // True if this is a DWARF debug info or exception handling chunk.
2430b57cec5SDimitry Andric   bool isDWARF() const {
2440b57cec5SDimitry Andric     return getSectionName().startswith(".debug_") || getSectionName() == ".eh_frame";
2450b57cec5SDimitry Andric   }
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   // Allow iteration over the bodies of this chunk's relocated symbols.
2480b57cec5SDimitry Andric   llvm::iterator_range<symbol_iterator> symbols() const {
2490b57cec5SDimitry Andric     return llvm::make_range(symbol_iterator(file, relocsData),
2500b57cec5SDimitry Andric                             symbol_iterator(file, relocsData + relocsSize));
2510b57cec5SDimitry Andric   }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   ArrayRef<coff_relocation> getRelocs() const {
2540b57cec5SDimitry Andric     return llvm::makeArrayRef(relocsData, relocsSize);
2550b57cec5SDimitry Andric   }
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric   // Reloc setter used by ARM range extension thunk insertion.
2580b57cec5SDimitry Andric   void setRelocs(ArrayRef<coff_relocation> newRelocs) {
2590b57cec5SDimitry Andric     relocsData = newRelocs.data();
2600b57cec5SDimitry Andric     relocsSize = newRelocs.size();
2610b57cec5SDimitry Andric     assert(relocsSize == newRelocs.size() && "reloc size truncation");
2620b57cec5SDimitry Andric   }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric   // Single linked list iterator for associated comdat children.
2650b57cec5SDimitry Andric   class AssociatedIterator
2660b57cec5SDimitry Andric       : public llvm::iterator_facade_base<
2670b57cec5SDimitry Andric             AssociatedIterator, std::forward_iterator_tag, SectionChunk> {
2680b57cec5SDimitry Andric   public:
2690b57cec5SDimitry Andric     AssociatedIterator() = default;
2700b57cec5SDimitry Andric     AssociatedIterator(SectionChunk *head) : cur(head) {}
2710b57cec5SDimitry Andric     bool operator==(const AssociatedIterator &r) const { return cur == r.cur; }
272*5ffd83dbSDimitry Andric     // FIXME: Wrong const-ness, but it makes filter ranges work.
273*5ffd83dbSDimitry Andric     SectionChunk &operator*() const { return *cur; }
2740b57cec5SDimitry Andric     SectionChunk &operator*() { return *cur; }
2750b57cec5SDimitry Andric     AssociatedIterator &operator++() {
2760b57cec5SDimitry Andric       cur = cur->assocChildren;
2770b57cec5SDimitry Andric       return *this;
2780b57cec5SDimitry Andric     }
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   private:
2810b57cec5SDimitry Andric     SectionChunk *cur = nullptr;
2820b57cec5SDimitry Andric   };
2830b57cec5SDimitry Andric 
2840b57cec5SDimitry Andric   // Allow iteration over the associated child chunks for this section.
2850b57cec5SDimitry Andric   llvm::iterator_range<AssociatedIterator> children() const {
2860b57cec5SDimitry Andric     return llvm::make_range(AssociatedIterator(assocChildren),
2870b57cec5SDimitry Andric                             AssociatedIterator(nullptr));
2880b57cec5SDimitry Andric   }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   // The section ID this chunk belongs to in its Obj.
2910b57cec5SDimitry Andric   uint32_t getSectionNumber() const;
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   ArrayRef<uint8_t> consumeDebugMagic();
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric   static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> data,
2960b57cec5SDimitry Andric                                              StringRef sectionName);
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   static SectionChunk *findByName(ArrayRef<SectionChunk *> sections,
2990b57cec5SDimitry Andric                                   StringRef name);
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   // The file that this chunk was created from.
3020b57cec5SDimitry Andric   ObjFile *file;
3030b57cec5SDimitry Andric 
3040b57cec5SDimitry Andric   // Pointer to the COFF section header in the input file.
3050b57cec5SDimitry Andric   const coff_section *header;
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   // The COMDAT leader symbol if this is a COMDAT chunk.
3080b57cec5SDimitry Andric   DefinedRegular *sym = nullptr;
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   // The CRC of the contents as described in the COFF spec 4.5.5.
3110b57cec5SDimitry Andric   // Auxiliary Format 5: Section Definitions. Used for ICF.
3120b57cec5SDimitry Andric   uint32_t checksum = 0;
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric   // Used by the garbage collector.
3150b57cec5SDimitry Andric   bool live;
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   // Whether this section needs to be kept distinct from other sections during
3180b57cec5SDimitry Andric   // ICF. This is set by the driver using address-significance tables.
3190b57cec5SDimitry Andric   bool keepUnique = false;
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   // The COMDAT selection if this is a COMDAT chunk.
3220b57cec5SDimitry Andric   llvm::COFF::COMDATType selection = (llvm::COFF::COMDATType)0;
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric   // A pointer pointing to a replacement for this chunk.
3250b57cec5SDimitry Andric   // Initially it points to "this" object. If this chunk is merged
3260b57cec5SDimitry Andric   // with other chunk by ICF, it points to another chunk,
3270b57cec5SDimitry Andric   // and this chunk is considered as dead.
3280b57cec5SDimitry Andric   SectionChunk *repl;
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric private:
3310b57cec5SDimitry Andric   SectionChunk *assocChildren = nullptr;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   // Used for ICF (Identical COMDAT Folding)
3340b57cec5SDimitry Andric   void replace(SectionChunk *other);
3350b57cec5SDimitry Andric   uint32_t eqClass[2] = {0, 0};
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   // Relocations for this section. Size is stored below.
3380b57cec5SDimitry Andric   const coff_relocation *relocsData;
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   // Section name string. Size is stored below.
3410b57cec5SDimitry Andric   const char *sectionNameData;
3420b57cec5SDimitry Andric 
3430b57cec5SDimitry Andric   uint32_t relocsSize = 0;
3440b57cec5SDimitry Andric   uint32_t sectionNameSize = 0;
3450b57cec5SDimitry Andric };
3460b57cec5SDimitry Andric 
3470b57cec5SDimitry Andric // Inline methods to implement faux-virtual dispatch for SectionChunk.
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric inline size_t Chunk::getSize() const {
3500b57cec5SDimitry Andric   if (isa<SectionChunk>(this))
3510b57cec5SDimitry Andric     return static_cast<const SectionChunk *>(this)->getSize();
3520b57cec5SDimitry Andric   else
3530b57cec5SDimitry Andric     return static_cast<const NonSectionChunk *>(this)->getSize();
3540b57cec5SDimitry Andric }
3550b57cec5SDimitry Andric 
3560b57cec5SDimitry Andric inline uint32_t Chunk::getOutputCharacteristics() const {
3570b57cec5SDimitry Andric   if (isa<SectionChunk>(this))
3580b57cec5SDimitry Andric     return static_cast<const SectionChunk *>(this)->getOutputCharacteristics();
3590b57cec5SDimitry Andric   else
3600b57cec5SDimitry Andric     return static_cast<const NonSectionChunk *>(this)
3610b57cec5SDimitry Andric         ->getOutputCharacteristics();
3620b57cec5SDimitry Andric }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric inline void Chunk::writeTo(uint8_t *buf) const {
3650b57cec5SDimitry Andric   if (isa<SectionChunk>(this))
3660b57cec5SDimitry Andric     static_cast<const SectionChunk *>(this)->writeTo(buf);
3670b57cec5SDimitry Andric   else
3680b57cec5SDimitry Andric     static_cast<const NonSectionChunk *>(this)->writeTo(buf);
3690b57cec5SDimitry Andric }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric inline StringRef Chunk::getSectionName() const {
3720b57cec5SDimitry Andric   if (isa<SectionChunk>(this))
3730b57cec5SDimitry Andric     return static_cast<const SectionChunk *>(this)->getSectionName();
3740b57cec5SDimitry Andric   else
3750b57cec5SDimitry Andric     return static_cast<const NonSectionChunk *>(this)->getSectionName();
3760b57cec5SDimitry Andric }
3770b57cec5SDimitry Andric 
3780b57cec5SDimitry Andric inline void Chunk::getBaserels(std::vector<Baserel> *res) {
3790b57cec5SDimitry Andric   if (isa<SectionChunk>(this))
3800b57cec5SDimitry Andric     static_cast<SectionChunk *>(this)->getBaserels(res);
3810b57cec5SDimitry Andric   else
3820b57cec5SDimitry Andric     static_cast<NonSectionChunk *>(this)->getBaserels(res);
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric inline StringRef Chunk::getDebugName() const {
3860b57cec5SDimitry Andric   if (isa<SectionChunk>(this))
3870b57cec5SDimitry Andric     return static_cast<const SectionChunk *>(this)->getDebugName();
3880b57cec5SDimitry Andric   else
3890b57cec5SDimitry Andric     return static_cast<const NonSectionChunk *>(this)->getDebugName();
3900b57cec5SDimitry Andric }
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric // This class is used to implement an lld-specific feature (not implemented in
3930b57cec5SDimitry Andric // MSVC) that minimizes the output size by finding string literals sharing tail
3940b57cec5SDimitry Andric // parts and merging them.
3950b57cec5SDimitry Andric //
3960b57cec5SDimitry Andric // If string tail merging is enabled and a section is identified as containing a
3970b57cec5SDimitry Andric // string literal, it is added to a MergeChunk with an appropriate alignment.
3980b57cec5SDimitry Andric // The MergeChunk then tail merges the strings using the StringTableBuilder
3990b57cec5SDimitry Andric // class and assigns RVAs and section offsets to each of the member chunks based
4000b57cec5SDimitry Andric // on the offsets assigned by the StringTableBuilder.
4010b57cec5SDimitry Andric class MergeChunk : public NonSectionChunk {
4020b57cec5SDimitry Andric public:
4030b57cec5SDimitry Andric   MergeChunk(uint32_t alignment);
4040b57cec5SDimitry Andric   static void addSection(SectionChunk *c);
4050b57cec5SDimitry Andric   void finalizeContents();
4060b57cec5SDimitry Andric   void assignSubsectionRVAs();
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   uint32_t getOutputCharacteristics() const override;
4090b57cec5SDimitry Andric   StringRef getSectionName() const override { return ".rdata"; }
4100b57cec5SDimitry Andric   size_t getSize() const override;
4110b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
4120b57cec5SDimitry Andric 
4130b57cec5SDimitry Andric   static MergeChunk *instances[Log2MaxSectionAlignment + 1];
4140b57cec5SDimitry Andric   std::vector<SectionChunk *> sections;
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric private:
4170b57cec5SDimitry Andric   llvm::StringTableBuilder builder;
4180b57cec5SDimitry Andric   bool finalized = false;
4190b57cec5SDimitry Andric };
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric // A chunk for common symbols. Common chunks don't have actual data.
4220b57cec5SDimitry Andric class CommonChunk : public NonSectionChunk {
4230b57cec5SDimitry Andric public:
4240b57cec5SDimitry Andric   CommonChunk(const COFFSymbolRef sym);
4250b57cec5SDimitry Andric   size_t getSize() const override { return sym.getValue(); }
4260b57cec5SDimitry Andric   uint32_t getOutputCharacteristics() const override;
4270b57cec5SDimitry Andric   StringRef getSectionName() const override { return ".bss"; }
4280b57cec5SDimitry Andric 
4290b57cec5SDimitry Andric private:
4300b57cec5SDimitry Andric   const COFFSymbolRef sym;
4310b57cec5SDimitry Andric };
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric // A chunk for linker-created strings.
4340b57cec5SDimitry Andric class StringChunk : public NonSectionChunk {
4350b57cec5SDimitry Andric public:
4360b57cec5SDimitry Andric   explicit StringChunk(StringRef s) : str(s) {}
4370b57cec5SDimitry Andric   size_t getSize() const override { return str.size() + 1; }
4380b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric private:
4410b57cec5SDimitry Andric   StringRef str;
4420b57cec5SDimitry Andric };
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric static const uint8_t importThunkX86[] = {
4450b57cec5SDimitry Andric     0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
4460b57cec5SDimitry Andric };
4470b57cec5SDimitry Andric 
4480b57cec5SDimitry Andric static const uint8_t importThunkARM[] = {
4490b57cec5SDimitry Andric     0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
4500b57cec5SDimitry Andric     0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
4510b57cec5SDimitry Andric     0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
4520b57cec5SDimitry Andric };
4530b57cec5SDimitry Andric 
4540b57cec5SDimitry Andric static const uint8_t importThunkARM64[] = {
4550b57cec5SDimitry Andric     0x10, 0x00, 0x00, 0x90, // adrp x16, #0
4560b57cec5SDimitry Andric     0x10, 0x02, 0x40, 0xf9, // ldr  x16, [x16]
4570b57cec5SDimitry Andric     0x00, 0x02, 0x1f, 0xd6, // br   x16
4580b57cec5SDimitry Andric };
4590b57cec5SDimitry Andric 
4600b57cec5SDimitry Andric // Windows-specific.
4610b57cec5SDimitry Andric // A chunk for DLL import jump table entry. In a final output, its
4620b57cec5SDimitry Andric // contents will be a JMP instruction to some __imp_ symbol.
4630b57cec5SDimitry Andric class ImportThunkChunk : public NonSectionChunk {
4640b57cec5SDimitry Andric public:
4650b57cec5SDimitry Andric   ImportThunkChunk(Defined *s)
4660b57cec5SDimitry Andric       : NonSectionChunk(ImportThunkKind), impSymbol(s) {}
4670b57cec5SDimitry Andric   static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; }
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric protected:
4700b57cec5SDimitry Andric   Defined *impSymbol;
4710b57cec5SDimitry Andric };
4720b57cec5SDimitry Andric 
4730b57cec5SDimitry Andric class ImportThunkChunkX64 : public ImportThunkChunk {
4740b57cec5SDimitry Andric public:
4750b57cec5SDimitry Andric   explicit ImportThunkChunkX64(Defined *s);
4760b57cec5SDimitry Andric   size_t getSize() const override { return sizeof(importThunkX86); }
4770b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
4780b57cec5SDimitry Andric };
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric class ImportThunkChunkX86 : public ImportThunkChunk {
4810b57cec5SDimitry Andric public:
4820b57cec5SDimitry Andric   explicit ImportThunkChunkX86(Defined *s) : ImportThunkChunk(s) {}
4830b57cec5SDimitry Andric   size_t getSize() const override { return sizeof(importThunkX86); }
4840b57cec5SDimitry Andric   void getBaserels(std::vector<Baserel> *res) override;
4850b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
4860b57cec5SDimitry Andric };
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric class ImportThunkChunkARM : public ImportThunkChunk {
4890b57cec5SDimitry Andric public:
490e837bb5cSDimitry Andric   explicit ImportThunkChunkARM(Defined *s) : ImportThunkChunk(s) {
491e837bb5cSDimitry Andric     setAlignment(2);
492e837bb5cSDimitry Andric   }
4930b57cec5SDimitry Andric   size_t getSize() const override { return sizeof(importThunkARM); }
4940b57cec5SDimitry Andric   void getBaserels(std::vector<Baserel> *res) override;
4950b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
4960b57cec5SDimitry Andric };
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric class ImportThunkChunkARM64 : public ImportThunkChunk {
4990b57cec5SDimitry Andric public:
500e837bb5cSDimitry Andric   explicit ImportThunkChunkARM64(Defined *s) : ImportThunkChunk(s) {
501e837bb5cSDimitry Andric     setAlignment(4);
502e837bb5cSDimitry Andric   }
5030b57cec5SDimitry Andric   size_t getSize() const override { return sizeof(importThunkARM64); }
5040b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
5050b57cec5SDimitry Andric };
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric class RangeExtensionThunkARM : public NonSectionChunk {
5080b57cec5SDimitry Andric public:
509e837bb5cSDimitry Andric   explicit RangeExtensionThunkARM(Defined *t) : target(t) { setAlignment(2); }
5100b57cec5SDimitry Andric   size_t getSize() const override;
5110b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
5120b57cec5SDimitry Andric 
5130b57cec5SDimitry Andric   Defined *target;
5140b57cec5SDimitry Andric };
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric class RangeExtensionThunkARM64 : public NonSectionChunk {
5170b57cec5SDimitry Andric public:
518480093f4SDimitry Andric   explicit RangeExtensionThunkARM64(Defined *t) : target(t) { setAlignment(4); }
5190b57cec5SDimitry Andric   size_t getSize() const override;
5200b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   Defined *target;
5230b57cec5SDimitry Andric };
5240b57cec5SDimitry Andric 
5250b57cec5SDimitry Andric // Windows-specific.
5260b57cec5SDimitry Andric // See comments for DefinedLocalImport class.
5270b57cec5SDimitry Andric class LocalImportChunk : public NonSectionChunk {
5280b57cec5SDimitry Andric public:
5290b57cec5SDimitry Andric   explicit LocalImportChunk(Defined *s) : sym(s) {
5300b57cec5SDimitry Andric     setAlignment(config->wordsize);
5310b57cec5SDimitry Andric   }
5320b57cec5SDimitry Andric   size_t getSize() const override;
5330b57cec5SDimitry Andric   void getBaserels(std::vector<Baserel> *res) override;
5340b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
5350b57cec5SDimitry Andric 
5360b57cec5SDimitry Andric private:
5370b57cec5SDimitry Andric   Defined *sym;
5380b57cec5SDimitry Andric };
5390b57cec5SDimitry Andric 
5400b57cec5SDimitry Andric // Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and
5410b57cec5SDimitry Andric // offset into the chunk. Order does not matter as the RVA table will be sorted
5420b57cec5SDimitry Andric // later.
5430b57cec5SDimitry Andric struct ChunkAndOffset {
5440b57cec5SDimitry Andric   Chunk *inputChunk;
5450b57cec5SDimitry Andric   uint32_t offset;
5460b57cec5SDimitry Andric 
5470b57cec5SDimitry Andric   struct DenseMapInfo {
5480b57cec5SDimitry Andric     static ChunkAndOffset getEmptyKey() {
5490b57cec5SDimitry Andric       return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0};
5500b57cec5SDimitry Andric     }
5510b57cec5SDimitry Andric     static ChunkAndOffset getTombstoneKey() {
5520b57cec5SDimitry Andric       return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0};
5530b57cec5SDimitry Andric     }
5540b57cec5SDimitry Andric     static unsigned getHashValue(const ChunkAndOffset &co) {
5550b57cec5SDimitry Andric       return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue(
5560b57cec5SDimitry Andric           {co.inputChunk, co.offset});
5570b57cec5SDimitry Andric     }
5580b57cec5SDimitry Andric     static bool isEqual(const ChunkAndOffset &lhs, const ChunkAndOffset &rhs) {
5590b57cec5SDimitry Andric       return lhs.inputChunk == rhs.inputChunk && lhs.offset == rhs.offset;
5600b57cec5SDimitry Andric     }
5610b57cec5SDimitry Andric   };
5620b57cec5SDimitry Andric };
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>;
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric // Table which contains symbol RVAs. Used for /safeseh and /guard:cf.
5670b57cec5SDimitry Andric class RVATableChunk : public NonSectionChunk {
5680b57cec5SDimitry Andric public:
5690b57cec5SDimitry Andric   explicit RVATableChunk(SymbolRVASet s) : syms(std::move(s)) {}
5700b57cec5SDimitry Andric   size_t getSize() const override { return syms.size() * 4; }
5710b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric private:
5740b57cec5SDimitry Andric   SymbolRVASet syms;
5750b57cec5SDimitry Andric };
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric // Windows-specific.
5780b57cec5SDimitry Andric // This class represents a block in .reloc section.
5790b57cec5SDimitry Andric // See the PE/COFF spec 5.6 for details.
5800b57cec5SDimitry Andric class BaserelChunk : public NonSectionChunk {
5810b57cec5SDimitry Andric public:
5820b57cec5SDimitry Andric   BaserelChunk(uint32_t page, Baserel *begin, Baserel *end);
5830b57cec5SDimitry Andric   size_t getSize() const override { return data.size(); }
5840b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric private:
5870b57cec5SDimitry Andric   std::vector<uint8_t> data;
5880b57cec5SDimitry Andric };
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric class Baserel {
5910b57cec5SDimitry Andric public:
5920b57cec5SDimitry Andric   Baserel(uint32_t v, uint8_t ty) : rva(v), type(ty) {}
5930b57cec5SDimitry Andric   explicit Baserel(uint32_t v) : Baserel(v, getDefaultType()) {}
5940b57cec5SDimitry Andric   uint8_t getDefaultType();
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric   uint32_t rva;
5970b57cec5SDimitry Andric   uint8_t type;
5980b57cec5SDimitry Andric };
5990b57cec5SDimitry Andric 
6000b57cec5SDimitry Andric // This is a placeholder Chunk, to allow attaching a DefinedSynthetic to a
6010b57cec5SDimitry Andric // specific place in a section, without any data. This is used for the MinGW
6020b57cec5SDimitry Andric // specific symbol __RUNTIME_PSEUDO_RELOC_LIST_END__, even though the concept
6030b57cec5SDimitry Andric // of an empty chunk isn't MinGW specific.
6040b57cec5SDimitry Andric class EmptyChunk : public NonSectionChunk {
6050b57cec5SDimitry Andric public:
6060b57cec5SDimitry Andric   EmptyChunk() {}
6070b57cec5SDimitry Andric   size_t getSize() const override { return 0; }
6080b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override {}
6090b57cec5SDimitry Andric };
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric // MinGW specific, for the "automatic import of variables from DLLs" feature.
6120b57cec5SDimitry Andric // This provides the table of runtime pseudo relocations, for variable
6130b57cec5SDimitry Andric // references that turned out to need to be imported from a DLL even though
6140b57cec5SDimitry Andric // the reference didn't use the dllimport attribute. The MinGW runtime will
6150b57cec5SDimitry Andric // process this table after loading, before handling control over to user
6160b57cec5SDimitry Andric // code.
6170b57cec5SDimitry Andric class PseudoRelocTableChunk : public NonSectionChunk {
6180b57cec5SDimitry Andric public:
6190b57cec5SDimitry Andric   PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &relocs)
6200b57cec5SDimitry Andric       : relocs(std::move(relocs)) {
6210b57cec5SDimitry Andric     setAlignment(4);
6220b57cec5SDimitry Andric   }
6230b57cec5SDimitry Andric   size_t getSize() const override;
6240b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric private:
6270b57cec5SDimitry Andric   std::vector<RuntimePseudoReloc> relocs;
6280b57cec5SDimitry Andric };
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric // MinGW specific; information about one individual location in the image
6310b57cec5SDimitry Andric // that needs to be fixed up at runtime after loading. This represents
6320b57cec5SDimitry Andric // one individual element in the PseudoRelocTableChunk table.
6330b57cec5SDimitry Andric class RuntimePseudoReloc {
6340b57cec5SDimitry Andric public:
6350b57cec5SDimitry Andric   RuntimePseudoReloc(Defined *sym, SectionChunk *target, uint32_t targetOffset,
6360b57cec5SDimitry Andric                      int flags)
6370b57cec5SDimitry Andric       : sym(sym), target(target), targetOffset(targetOffset), flags(flags) {}
6380b57cec5SDimitry Andric 
6390b57cec5SDimitry Andric   Defined *sym;
6400b57cec5SDimitry Andric   SectionChunk *target;
6410b57cec5SDimitry Andric   uint32_t targetOffset;
6420b57cec5SDimitry Andric   // The Flags field contains the size of the relocation, in bits. No other
6430b57cec5SDimitry Andric   // flags are currently defined.
6440b57cec5SDimitry Andric   int flags;
6450b57cec5SDimitry Andric };
6460b57cec5SDimitry Andric 
6470b57cec5SDimitry Andric // MinGW specific. A Chunk that contains one pointer-sized absolute value.
6480b57cec5SDimitry Andric class AbsolutePointerChunk : public NonSectionChunk {
6490b57cec5SDimitry Andric public:
6500b57cec5SDimitry Andric   AbsolutePointerChunk(uint64_t value) : value(value) {
6510b57cec5SDimitry Andric     setAlignment(getSize());
6520b57cec5SDimitry Andric   }
6530b57cec5SDimitry Andric   size_t getSize() const override;
6540b57cec5SDimitry Andric   void writeTo(uint8_t *buf) const override;
6550b57cec5SDimitry Andric 
6560b57cec5SDimitry Andric private:
6570b57cec5SDimitry Andric   uint64_t value;
6580b57cec5SDimitry Andric };
6590b57cec5SDimitry Andric 
6600b57cec5SDimitry Andric // Return true if this file has the hotpatch flag set to true in the S_COMPILE3
6610b57cec5SDimitry Andric // record in codeview debug info. Also returns true for some thunks synthesized
6620b57cec5SDimitry Andric // by the linker.
6630b57cec5SDimitry Andric inline bool Chunk::isHotPatchable() const {
6640b57cec5SDimitry Andric   if (auto *sc = dyn_cast<SectionChunk>(this))
6650b57cec5SDimitry Andric     return sc->file->hotPatchable;
6660b57cec5SDimitry Andric   else if (isa<ImportThunkChunk>(this))
6670b57cec5SDimitry Andric     return true;
6680b57cec5SDimitry Andric   return false;
6690b57cec5SDimitry Andric }
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric void applyMOV32T(uint8_t *off, uint32_t v);
6720b57cec5SDimitry Andric void applyBranch24T(uint8_t *off, int32_t v);
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift);
6750b57cec5SDimitry Andric void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit);
6760b57cec5SDimitry Andric void applyArm64Branch26(uint8_t *off, int64_t v);
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric } // namespace coff
6790b57cec5SDimitry Andric } // namespace lld
6800b57cec5SDimitry Andric 
6810b57cec5SDimitry Andric namespace llvm {
6820b57cec5SDimitry Andric template <>
6830b57cec5SDimitry Andric struct DenseMapInfo<lld::coff::ChunkAndOffset>
6840b57cec5SDimitry Andric     : lld::coff::ChunkAndOffset::DenseMapInfo {};
6850b57cec5SDimitry Andric }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric #endif
688