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"
21*0fca6ea1SDimitry Andric #include "llvm/Object/WindowsMachineFlag.h"
220b57cec5SDimitry Andric #include <utility>
230b57cec5SDimitry Andric #include <vector>
240b57cec5SDimitry Andric
25bdd1243dSDimitry Andric namespace lld::coff {
260b57cec5SDimitry Andric
270b57cec5SDimitry Andric using llvm::COFF::ImportDirectoryTableEntry;
285f757f3fSDimitry Andric using llvm::object::chpe_range_type;
290b57cec5SDimitry Andric using llvm::object::coff_relocation;
300b57cec5SDimitry Andric using llvm::object::coff_section;
315f757f3fSDimitry Andric using llvm::object::COFFSymbolRef;
325f757f3fSDimitry Andric using llvm::object::SectionRef;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric class Baserel;
350b57cec5SDimitry Andric class Defined;
360b57cec5SDimitry Andric class DefinedImportData;
370b57cec5SDimitry Andric class DefinedRegular;
380b57cec5SDimitry Andric class ObjFile;
390b57cec5SDimitry Andric class OutputSection;
400b57cec5SDimitry Andric class RuntimePseudoReloc;
410b57cec5SDimitry Andric class Symbol;
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric // Mask for permissions (discardable, writable, readable, executable, etc).
440b57cec5SDimitry Andric const uint32_t permMask = 0xFE000000;
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric // Mask for section types (code, data, bss).
470b57cec5SDimitry Andric const uint32_t typeMask = 0x000000E0;
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric // The log base 2 of the largest section alignment, which is log2(8192), or 13.
500b57cec5SDimitry Andric enum : unsigned { Log2MaxSectionAlignment = 13 };
510b57cec5SDimitry Andric
520b57cec5SDimitry Andric // A Chunk represents a chunk of data that will occupy space in the
530b57cec5SDimitry Andric // output (if the resolver chose that). It may or may not be backed by
540b57cec5SDimitry Andric // a section of an input file. It could be linker-created data, or
550b57cec5SDimitry Andric // doesn't even have actual data (if common or bss).
560b57cec5SDimitry Andric class Chunk {
570b57cec5SDimitry Andric public:
58*0fca6ea1SDimitry Andric enum Kind : uint8_t {
59*0fca6ea1SDimitry Andric SectionKind,
60*0fca6ea1SDimitry Andric SectionECKind,
61*0fca6ea1SDimitry Andric OtherKind,
62*0fca6ea1SDimitry Andric ImportThunkKind
63*0fca6ea1SDimitry Andric };
kind()640b57cec5SDimitry Andric Kind kind() const { return chunkKind; }
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric // Returns the size of this chunk (even if this is a common or BSS.)
670b57cec5SDimitry Andric size_t getSize() const;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // Returns chunk alignment in power of two form. Value values are powers of
700b57cec5SDimitry Andric // two from 1 to 8192.
getAlignment()710b57cec5SDimitry Andric uint32_t getAlignment() const { return 1U << p2Align; }
720b57cec5SDimitry Andric
730b57cec5SDimitry Andric // Update the chunk section alignment measured in bytes. Internally alignment
740b57cec5SDimitry Andric // is stored in log2.
setAlignment(uint32_t align)750b57cec5SDimitry Andric void setAlignment(uint32_t align) {
760b57cec5SDimitry Andric // Treat zero byte alignment as 1 byte alignment.
770b57cec5SDimitry Andric align = align ? align : 1;
780b57cec5SDimitry Andric assert(llvm::isPowerOf2_32(align) && "alignment is not a power of 2");
790b57cec5SDimitry Andric p2Align = llvm::Log2_32(align);
800b57cec5SDimitry Andric assert(p2Align <= Log2MaxSectionAlignment &&
810b57cec5SDimitry Andric "impossible requested alignment");
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric // Write this chunk to a mmap'ed file, assuming Buf is pointing to
850b57cec5SDimitry Andric // beginning of the file. Because this function may use RVA values
860b57cec5SDimitry Andric // of other chunks for relocations, you need to set them properly
870b57cec5SDimitry Andric // before calling this function.
880b57cec5SDimitry Andric void writeTo(uint8_t *buf) const;
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric // The writer sets and uses the addresses. In practice, PE images cannot be
910b57cec5SDimitry Andric // larger than 2GB. Chunks are always laid as part of the image, so Chunk RVAs
920b57cec5SDimitry Andric // can be stored with 32 bits.
getRVA()930b57cec5SDimitry Andric uint32_t getRVA() const { return rva; }
setRVA(uint64_t v)940b57cec5SDimitry Andric void setRVA(uint64_t v) {
95fe6060f1SDimitry Andric // This may truncate. The writer checks for overflow later.
960b57cec5SDimitry Andric rva = (uint32_t)v;
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric // Returns readable/writable/executable bits.
1000b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const;
1010b57cec5SDimitry Andric
1020b57cec5SDimitry Andric // Returns the section name if this is a section chunk.
1030b57cec5SDimitry Andric // It is illegal to call this function on non-section chunks.
1040b57cec5SDimitry Andric StringRef getSectionName() const;
1050b57cec5SDimitry Andric
1060b57cec5SDimitry Andric // An output section has pointers to chunks in the section, and each
1070b57cec5SDimitry Andric // chunk has a back pointer to an output section.
setOutputSectionIdx(uint16_t o)1080b57cec5SDimitry Andric void setOutputSectionIdx(uint16_t o) { osidx = o; }
getOutputSectionIdx()1090b57cec5SDimitry Andric uint16_t getOutputSectionIdx() const { return osidx; }
1100b57cec5SDimitry Andric
1110b57cec5SDimitry Andric // Windows-specific.
1120b57cec5SDimitry Andric // Collect all locations that contain absolute addresses for base relocations.
1130b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res);
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
1160b57cec5SDimitry Andric // bytes, so this is used only for logging or debugging.
1170b57cec5SDimitry Andric StringRef getDebugName() const;
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric // Return true if this file has the hotpatch flag set to true in the
1200b57cec5SDimitry Andric // S_COMPILE3 record in codeview debug info. Also returns true for some thunks
1210b57cec5SDimitry Andric // synthesized by the linker.
1220b57cec5SDimitry Andric bool isHotPatchable() const;
1230b57cec5SDimitry Andric
1245f757f3fSDimitry Andric MachineTypes getMachine() const;
125*0fca6ea1SDimitry Andric llvm::Triple::ArchType getArch() const;
1265f757f3fSDimitry Andric std::optional<chpe_range_type> getArm64ECRangeType() const;
1275f757f3fSDimitry Andric
128*0fca6ea1SDimitry Andric // ARM64EC entry thunk associated with the chunk.
129*0fca6ea1SDimitry Andric Defined *getEntryThunk() const;
130*0fca6ea1SDimitry Andric void setEntryThunk(Defined *entryThunk);
131*0fca6ea1SDimitry Andric
1320b57cec5SDimitry Andric protected:
chunkKind(k)1330b57cec5SDimitry Andric Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {}
1340b57cec5SDimitry Andric
1350b57cec5SDimitry Andric const Kind chunkKind;
1360b57cec5SDimitry Andric
1370b57cec5SDimitry Andric public:
1380b57cec5SDimitry Andric // Returns true if this has non-zero data. BSS chunks return
1390b57cec5SDimitry Andric // false. If false is returned, the space occupied by this chunk
1400b57cec5SDimitry Andric // will be filled with zeros. Corresponds to the
1410b57cec5SDimitry Andric // IMAGE_SCN_CNT_UNINITIALIZED_DATA section characteristic bit.
1420b57cec5SDimitry Andric uint8_t hasData : 1;
1430b57cec5SDimitry Andric
1440b57cec5SDimitry Andric public:
1450b57cec5SDimitry Andric // The alignment of this chunk, stored in log2 form. The writer uses the
1460b57cec5SDimitry Andric // value.
1470b57cec5SDimitry Andric uint8_t p2Align : 7;
1480b57cec5SDimitry Andric
1490b57cec5SDimitry Andric // The output section index for this chunk. The first valid section number is
1500b57cec5SDimitry Andric // one.
1510b57cec5SDimitry Andric uint16_t osidx = 0;
1520b57cec5SDimitry Andric
1530b57cec5SDimitry Andric // The RVA of this chunk in the output. The writer sets a value.
1540b57cec5SDimitry Andric uint32_t rva = 0;
1550b57cec5SDimitry Andric };
1560b57cec5SDimitry Andric
1570b57cec5SDimitry Andric class NonSectionChunk : public Chunk {
1580b57cec5SDimitry Andric public:
1590b57cec5SDimitry Andric virtual ~NonSectionChunk() = default;
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric // Returns the size of this chunk (even if this is a common or BSS.)
1620b57cec5SDimitry Andric virtual size_t getSize() const = 0;
1630b57cec5SDimitry Andric
getOutputCharacteristics()1640b57cec5SDimitry Andric virtual uint32_t getOutputCharacteristics() const { return 0; }
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric // Write this chunk to a mmap'ed file, assuming Buf is pointing to
1670b57cec5SDimitry Andric // beginning of the file. Because this function may use RVA values
1680b57cec5SDimitry Andric // of other chunks for relocations, you need to set them properly
1690b57cec5SDimitry Andric // before calling this function.
writeTo(uint8_t * buf)1700b57cec5SDimitry Andric virtual void writeTo(uint8_t *buf) const {}
1710b57cec5SDimitry Andric
1720b57cec5SDimitry Andric // Returns the section name if this is a section chunk.
1730b57cec5SDimitry Andric // It is illegal to call this function on non-section chunks.
getSectionName()1740b57cec5SDimitry Andric virtual StringRef getSectionName() const {
1750b57cec5SDimitry Andric llvm_unreachable("unimplemented getSectionName");
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric
1780b57cec5SDimitry Andric // Windows-specific.
1790b57cec5SDimitry Andric // Collect all locations that contain absolute addresses for base relocations.
getBaserels(std::vector<Baserel> * res)1800b57cec5SDimitry Andric virtual void getBaserels(std::vector<Baserel> *res) {}
1810b57cec5SDimitry Andric
getMachine()1825f757f3fSDimitry Andric virtual MachineTypes getMachine() const { return IMAGE_FILE_MACHINE_UNKNOWN; }
1835f757f3fSDimitry Andric
1840b57cec5SDimitry Andric // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
1850b57cec5SDimitry Andric // bytes, so this is used only for logging or debugging.
getDebugName()1860b57cec5SDimitry Andric virtual StringRef getDebugName() const { return ""; }
1870b57cec5SDimitry Andric
classof(const Chunk * c)188*0fca6ea1SDimitry Andric static bool classof(const Chunk *c) { return c->kind() >= OtherKind; }
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric protected:
Chunk(k)1910b57cec5SDimitry Andric NonSectionChunk(Kind k = OtherKind) : Chunk(k) {}
1920b57cec5SDimitry Andric };
1930b57cec5SDimitry Andric
1945f757f3fSDimitry Andric class NonSectionCodeChunk : public NonSectionChunk {
1955f757f3fSDimitry Andric public:
getOutputCharacteristics()1965f757f3fSDimitry Andric virtual uint32_t getOutputCharacteristics() const override {
1975f757f3fSDimitry Andric return llvm::COFF::IMAGE_SCN_MEM_READ | llvm::COFF::IMAGE_SCN_MEM_EXECUTE;
1985f757f3fSDimitry Andric }
1995f757f3fSDimitry Andric
2005f757f3fSDimitry Andric protected:
NonSectionChunk(k)2015f757f3fSDimitry Andric NonSectionCodeChunk(Kind k = OtherKind) : NonSectionChunk(k) {}
2025f757f3fSDimitry Andric };
2035f757f3fSDimitry Andric
204bdd1243dSDimitry Andric // MinGW specific; information about one individual location in the image
205bdd1243dSDimitry Andric // that needs to be fixed up at runtime after loading. This represents
206bdd1243dSDimitry Andric // one individual element in the PseudoRelocTableChunk table.
207bdd1243dSDimitry Andric class RuntimePseudoReloc {
208bdd1243dSDimitry Andric public:
RuntimePseudoReloc(Defined * sym,SectionChunk * target,uint32_t targetOffset,int flags)209bdd1243dSDimitry Andric RuntimePseudoReloc(Defined *sym, SectionChunk *target, uint32_t targetOffset,
210bdd1243dSDimitry Andric int flags)
211bdd1243dSDimitry Andric : sym(sym), target(target), targetOffset(targetOffset), flags(flags) {}
212bdd1243dSDimitry Andric
213bdd1243dSDimitry Andric Defined *sym;
214bdd1243dSDimitry Andric SectionChunk *target;
215bdd1243dSDimitry Andric uint32_t targetOffset;
216bdd1243dSDimitry Andric // The Flags field contains the size of the relocation, in bits. No other
217bdd1243dSDimitry Andric // flags are currently defined.
218bdd1243dSDimitry Andric int flags;
219bdd1243dSDimitry Andric };
220bdd1243dSDimitry Andric
2210b57cec5SDimitry Andric // A chunk corresponding a section of an input file.
222*0fca6ea1SDimitry Andric class SectionChunk : public Chunk {
2230b57cec5SDimitry Andric // Identical COMDAT Folding feature accesses section internal data.
2240b57cec5SDimitry Andric friend class ICF;
2250b57cec5SDimitry Andric
2260b57cec5SDimitry Andric public:
2270b57cec5SDimitry Andric class symbol_iterator : public llvm::iterator_adaptor_base<
2280b57cec5SDimitry Andric symbol_iterator, const coff_relocation *,
2290b57cec5SDimitry Andric std::random_access_iterator_tag, Symbol *> {
2300b57cec5SDimitry Andric friend SectionChunk;
2310b57cec5SDimitry Andric
2320b57cec5SDimitry Andric ObjFile *file;
2330b57cec5SDimitry Andric
symbol_iterator(ObjFile * file,const coff_relocation * i)2340b57cec5SDimitry Andric symbol_iterator(ObjFile *file, const coff_relocation *i)
2350b57cec5SDimitry Andric : symbol_iterator::iterator_adaptor_base(i), file(file) {}
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric public:
2380b57cec5SDimitry Andric symbol_iterator() = default;
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric Symbol *operator*() const { return file->getSymbol(I->SymbolTableIndex); }
2410b57cec5SDimitry Andric };
2420b57cec5SDimitry Andric
243*0fca6ea1SDimitry Andric SectionChunk(ObjFile *file, const coff_section *header, Kind k = SectionKind);
classof(const Chunk * c)244*0fca6ea1SDimitry Andric static bool classof(const Chunk *c) { return c->kind() <= SectionECKind; }
getSize()2450b57cec5SDimitry Andric size_t getSize() const { return header->SizeOfRawData; }
2460b57cec5SDimitry Andric ArrayRef<uint8_t> getContents() const;
2470b57cec5SDimitry Andric void writeTo(uint8_t *buf) const;
2480b57cec5SDimitry Andric
getMachine()2495f757f3fSDimitry Andric MachineTypes getMachine() const { return file->getMachineType(); }
2505f757f3fSDimitry Andric
251e8d8bef9SDimitry Andric // Defend against unsorted relocations. This may be overly conservative.
252e8d8bef9SDimitry Andric void sortRelocations();
253e8d8bef9SDimitry Andric
254e8d8bef9SDimitry Andric // Write and relocate a portion of the section. This is intended to be called
255e8d8bef9SDimitry Andric // in a loop. Relocations must be sorted first.
256e8d8bef9SDimitry Andric void writeAndRelocateSubsection(ArrayRef<uint8_t> sec,
257e8d8bef9SDimitry Andric ArrayRef<uint8_t> subsec,
258e8d8bef9SDimitry Andric uint32_t &nextRelocIndex, uint8_t *buf) const;
259e8d8bef9SDimitry Andric
getOutputCharacteristics()2600b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const {
2610b57cec5SDimitry Andric return header->Characteristics & (permMask | typeMask);
2620b57cec5SDimitry Andric }
getSectionName()2630b57cec5SDimitry Andric StringRef getSectionName() const {
2640b57cec5SDimitry Andric return StringRef(sectionNameData, sectionNameSize);
2650b57cec5SDimitry Andric }
2660b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res);
2670b57cec5SDimitry Andric bool isCOMDAT() const;
268e8d8bef9SDimitry Andric void applyRelocation(uint8_t *off, const coff_relocation &rel) const;
2690b57cec5SDimitry Andric void applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
270bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const;
2710b57cec5SDimitry Andric void applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
272bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const;
2730b57cec5SDimitry Andric void applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
274bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const;
2750b57cec5SDimitry Andric void applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, uint64_t s,
276bdd1243dSDimitry Andric uint64_t p, uint64_t imageBase) const;
2770b57cec5SDimitry Andric
2780b57cec5SDimitry Andric void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &res);
2790b57cec5SDimitry Andric
2800b57cec5SDimitry Andric // Called if the garbage collector decides to not include this chunk
2810b57cec5SDimitry Andric // in a final output. It's supposed to print out a log message to stdout.
2820b57cec5SDimitry Andric void printDiscardedMessage() const;
2830b57cec5SDimitry Andric
2840b57cec5SDimitry Andric // Adds COMDAT associative sections to this COMDAT section. A chunk
2850b57cec5SDimitry Andric // and its children are treated as a group by the garbage collector.
2860b57cec5SDimitry Andric void addAssociative(SectionChunk *child);
2870b57cec5SDimitry Andric
2880b57cec5SDimitry Andric StringRef getDebugName() const;
2890b57cec5SDimitry Andric
2900b57cec5SDimitry Andric // True if this is a codeview debug info chunk. These will not be laid out in
2910b57cec5SDimitry Andric // the image. Instead they will end up in the PDB, if one is requested.
isCodeView()2920b57cec5SDimitry Andric bool isCodeView() const {
29306c3fb27SDimitry Andric return getSectionName() == ".debug" || getSectionName().starts_with(".debug$");
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric
2960b57cec5SDimitry Andric // True if this is a DWARF debug info or exception handling chunk.
isDWARF()2970b57cec5SDimitry Andric bool isDWARF() const {
29806c3fb27SDimitry Andric return getSectionName().starts_with(".debug_") || getSectionName() == ".eh_frame";
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric
3010b57cec5SDimitry Andric // Allow iteration over the bodies of this chunk's relocated symbols.
symbols()3020b57cec5SDimitry Andric llvm::iterator_range<symbol_iterator> symbols() const {
3030b57cec5SDimitry Andric return llvm::make_range(symbol_iterator(file, relocsData),
3040b57cec5SDimitry Andric symbol_iterator(file, relocsData + relocsSize));
3050b57cec5SDimitry Andric }
3060b57cec5SDimitry Andric
getRelocs()3070b57cec5SDimitry Andric ArrayRef<coff_relocation> getRelocs() const {
308bdd1243dSDimitry Andric return llvm::ArrayRef(relocsData, relocsSize);
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric
3110b57cec5SDimitry Andric // Reloc setter used by ARM range extension thunk insertion.
setRelocs(ArrayRef<coff_relocation> newRelocs)3120b57cec5SDimitry Andric void setRelocs(ArrayRef<coff_relocation> newRelocs) {
3130b57cec5SDimitry Andric relocsData = newRelocs.data();
3140b57cec5SDimitry Andric relocsSize = newRelocs.size();
3150b57cec5SDimitry Andric assert(relocsSize == newRelocs.size() && "reloc size truncation");
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric
3180b57cec5SDimitry Andric // Single linked list iterator for associated comdat children.
3190b57cec5SDimitry Andric class AssociatedIterator
3200b57cec5SDimitry Andric : public llvm::iterator_facade_base<
3210b57cec5SDimitry Andric AssociatedIterator, std::forward_iterator_tag, SectionChunk> {
3220b57cec5SDimitry Andric public:
3230b57cec5SDimitry Andric AssociatedIterator() = default;
AssociatedIterator(SectionChunk * head)3240b57cec5SDimitry Andric AssociatedIterator(SectionChunk *head) : cur(head) {}
3250b57cec5SDimitry Andric bool operator==(const AssociatedIterator &r) const { return cur == r.cur; }
3265ffd83dbSDimitry Andric // FIXME: Wrong const-ness, but it makes filter ranges work.
3275ffd83dbSDimitry Andric SectionChunk &operator*() const { return *cur; }
3280b57cec5SDimitry Andric SectionChunk &operator*() { return *cur; }
3290b57cec5SDimitry Andric AssociatedIterator &operator++() {
3300b57cec5SDimitry Andric cur = cur->assocChildren;
3310b57cec5SDimitry Andric return *this;
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric private:
3350b57cec5SDimitry Andric SectionChunk *cur = nullptr;
3360b57cec5SDimitry Andric };
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric // Allow iteration over the associated child chunks for this section.
children()3390b57cec5SDimitry Andric llvm::iterator_range<AssociatedIterator> children() const {
340fe6060f1SDimitry Andric // Associated sections do not have children. The assocChildren field is
341fe6060f1SDimitry Andric // part of the parent's list of children.
342fe6060f1SDimitry Andric bool isAssoc = selection == llvm::COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
343fe6060f1SDimitry Andric return llvm::make_range(
344fe6060f1SDimitry Andric AssociatedIterator(isAssoc ? nullptr : assocChildren),
3450b57cec5SDimitry Andric AssociatedIterator(nullptr));
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric
3480b57cec5SDimitry Andric // The section ID this chunk belongs to in its Obj.
3490b57cec5SDimitry Andric uint32_t getSectionNumber() const;
3500b57cec5SDimitry Andric
3510b57cec5SDimitry Andric ArrayRef<uint8_t> consumeDebugMagic();
3520b57cec5SDimitry Andric
3530b57cec5SDimitry Andric static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> data,
3540b57cec5SDimitry Andric StringRef sectionName);
3550b57cec5SDimitry Andric
3560b57cec5SDimitry Andric static SectionChunk *findByName(ArrayRef<SectionChunk *> sections,
3570b57cec5SDimitry Andric StringRef name);
3580b57cec5SDimitry Andric
3590b57cec5SDimitry Andric // The file that this chunk was created from.
3600b57cec5SDimitry Andric ObjFile *file;
3610b57cec5SDimitry Andric
3620b57cec5SDimitry Andric // Pointer to the COFF section header in the input file.
3630b57cec5SDimitry Andric const coff_section *header;
3640b57cec5SDimitry Andric
3650b57cec5SDimitry Andric // The COMDAT leader symbol if this is a COMDAT chunk.
3660b57cec5SDimitry Andric DefinedRegular *sym = nullptr;
3670b57cec5SDimitry Andric
3680b57cec5SDimitry Andric // The CRC of the contents as described in the COFF spec 4.5.5.
3690b57cec5SDimitry Andric // Auxiliary Format 5: Section Definitions. Used for ICF.
3700b57cec5SDimitry Andric uint32_t checksum = 0;
3710b57cec5SDimitry Andric
3720b57cec5SDimitry Andric // Used by the garbage collector.
3730b57cec5SDimitry Andric bool live;
3740b57cec5SDimitry Andric
3750b57cec5SDimitry Andric // Whether this section needs to be kept distinct from other sections during
3760b57cec5SDimitry Andric // ICF. This is set by the driver using address-significance tables.
3770b57cec5SDimitry Andric bool keepUnique = false;
3780b57cec5SDimitry Andric
3790b57cec5SDimitry Andric // The COMDAT selection if this is a COMDAT chunk.
3800b57cec5SDimitry Andric llvm::COFF::COMDATType selection = (llvm::COFF::COMDATType)0;
3810b57cec5SDimitry Andric
3820b57cec5SDimitry Andric // A pointer pointing to a replacement for this chunk.
3830b57cec5SDimitry Andric // Initially it points to "this" object. If this chunk is merged
3840b57cec5SDimitry Andric // with other chunk by ICF, it points to another chunk,
3850b57cec5SDimitry Andric // and this chunk is considered as dead.
3860b57cec5SDimitry Andric SectionChunk *repl;
3870b57cec5SDimitry Andric
3880b57cec5SDimitry Andric private:
3890b57cec5SDimitry Andric SectionChunk *assocChildren = nullptr;
3900b57cec5SDimitry Andric
3910b57cec5SDimitry Andric // Used for ICF (Identical COMDAT Folding)
3920b57cec5SDimitry Andric void replace(SectionChunk *other);
3930b57cec5SDimitry Andric uint32_t eqClass[2] = {0, 0};
3940b57cec5SDimitry Andric
3950b57cec5SDimitry Andric // Relocations for this section. Size is stored below.
3960b57cec5SDimitry Andric const coff_relocation *relocsData;
3970b57cec5SDimitry Andric
3980b57cec5SDimitry Andric // Section name string. Size is stored below.
3990b57cec5SDimitry Andric const char *sectionNameData;
4000b57cec5SDimitry Andric
4010b57cec5SDimitry Andric uint32_t relocsSize = 0;
4020b57cec5SDimitry Andric uint32_t sectionNameSize = 0;
4030b57cec5SDimitry Andric };
4040b57cec5SDimitry Andric
405*0fca6ea1SDimitry Andric // A section chunk corresponding a section of an EC input file.
406*0fca6ea1SDimitry Andric class SectionChunkEC final : public SectionChunk {
407*0fca6ea1SDimitry Andric public:
classof(const Chunk * c)408*0fca6ea1SDimitry Andric static bool classof(const Chunk *c) { return c->kind() == SectionECKind; }
409*0fca6ea1SDimitry Andric
SectionChunkEC(ObjFile * file,const coff_section * header)410*0fca6ea1SDimitry Andric SectionChunkEC(ObjFile *file, const coff_section *header)
411*0fca6ea1SDimitry Andric : SectionChunk(file, header, SectionECKind) {}
412*0fca6ea1SDimitry Andric Defined *entryThunk = nullptr;
413*0fca6ea1SDimitry Andric };
414*0fca6ea1SDimitry Andric
4150b57cec5SDimitry Andric // Inline methods to implement faux-virtual dispatch for SectionChunk.
4160b57cec5SDimitry Andric
getSize()4170b57cec5SDimitry Andric inline size_t Chunk::getSize() const {
4180b57cec5SDimitry Andric if (isa<SectionChunk>(this))
4190b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getSize();
4200b57cec5SDimitry Andric return static_cast<const NonSectionChunk *>(this)->getSize();
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
getOutputCharacteristics()4230b57cec5SDimitry Andric inline uint32_t Chunk::getOutputCharacteristics() const {
4240b57cec5SDimitry Andric if (isa<SectionChunk>(this))
4250b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getOutputCharacteristics();
4265f757f3fSDimitry Andric return static_cast<const NonSectionChunk *>(this)->getOutputCharacteristics();
4270b57cec5SDimitry Andric }
4280b57cec5SDimitry Andric
writeTo(uint8_t * buf)4290b57cec5SDimitry Andric inline void Chunk::writeTo(uint8_t *buf) const {
4300b57cec5SDimitry Andric if (isa<SectionChunk>(this))
4310b57cec5SDimitry Andric static_cast<const SectionChunk *>(this)->writeTo(buf);
4320b57cec5SDimitry Andric else
4330b57cec5SDimitry Andric static_cast<const NonSectionChunk *>(this)->writeTo(buf);
4340b57cec5SDimitry Andric }
4350b57cec5SDimitry Andric
getSectionName()4360b57cec5SDimitry Andric inline StringRef Chunk::getSectionName() const {
4370b57cec5SDimitry Andric if (isa<SectionChunk>(this))
4380b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getSectionName();
4390b57cec5SDimitry Andric return static_cast<const NonSectionChunk *>(this)->getSectionName();
4400b57cec5SDimitry Andric }
4410b57cec5SDimitry Andric
getBaserels(std::vector<Baserel> * res)4420b57cec5SDimitry Andric inline void Chunk::getBaserels(std::vector<Baserel> *res) {
4430b57cec5SDimitry Andric if (isa<SectionChunk>(this))
4440b57cec5SDimitry Andric static_cast<SectionChunk *>(this)->getBaserels(res);
4450b57cec5SDimitry Andric else
4460b57cec5SDimitry Andric static_cast<NonSectionChunk *>(this)->getBaserels(res);
4470b57cec5SDimitry Andric }
4480b57cec5SDimitry Andric
getDebugName()4490b57cec5SDimitry Andric inline StringRef Chunk::getDebugName() const {
4500b57cec5SDimitry Andric if (isa<SectionChunk>(this))
4510b57cec5SDimitry Andric return static_cast<const SectionChunk *>(this)->getDebugName();
4520b57cec5SDimitry Andric return static_cast<const NonSectionChunk *>(this)->getDebugName();
4530b57cec5SDimitry Andric }
4540b57cec5SDimitry Andric
getMachine()4555f757f3fSDimitry Andric inline MachineTypes Chunk::getMachine() const {
4565f757f3fSDimitry Andric if (isa<SectionChunk>(this))
4575f757f3fSDimitry Andric return static_cast<const SectionChunk *>(this)->getMachine();
4585f757f3fSDimitry Andric return static_cast<const NonSectionChunk *>(this)->getMachine();
4595f757f3fSDimitry Andric }
4605f757f3fSDimitry Andric
getArch()461*0fca6ea1SDimitry Andric inline llvm::Triple::ArchType Chunk::getArch() const {
462*0fca6ea1SDimitry Andric return llvm::getMachineArchType(getMachine());
463*0fca6ea1SDimitry Andric }
464*0fca6ea1SDimitry Andric
getArm64ECRangeType()4655f757f3fSDimitry Andric inline std::optional<chpe_range_type> Chunk::getArm64ECRangeType() const {
4665f757f3fSDimitry Andric // Data sections don't need codemap entries.
4675f757f3fSDimitry Andric if (!(getOutputCharacteristics() & llvm::COFF::IMAGE_SCN_MEM_EXECUTE))
4685f757f3fSDimitry Andric return std::nullopt;
4695f757f3fSDimitry Andric
4705f757f3fSDimitry Andric switch (getMachine()) {
4715f757f3fSDimitry Andric case AMD64:
4725f757f3fSDimitry Andric return chpe_range_type::Amd64;
4735f757f3fSDimitry Andric case ARM64EC:
4745f757f3fSDimitry Andric return chpe_range_type::Arm64EC;
4755f757f3fSDimitry Andric default:
4765f757f3fSDimitry Andric return chpe_range_type::Arm64;
4775f757f3fSDimitry Andric }
4785f757f3fSDimitry Andric }
4795f757f3fSDimitry Andric
4800b57cec5SDimitry Andric // This class is used to implement an lld-specific feature (not implemented in
4810b57cec5SDimitry Andric // MSVC) that minimizes the output size by finding string literals sharing tail
4820b57cec5SDimitry Andric // parts and merging them.
4830b57cec5SDimitry Andric //
4840b57cec5SDimitry Andric // If string tail merging is enabled and a section is identified as containing a
4850b57cec5SDimitry Andric // string literal, it is added to a MergeChunk with an appropriate alignment.
4860b57cec5SDimitry Andric // The MergeChunk then tail merges the strings using the StringTableBuilder
4870b57cec5SDimitry Andric // class and assigns RVAs and section offsets to each of the member chunks based
4880b57cec5SDimitry Andric // on the offsets assigned by the StringTableBuilder.
4890b57cec5SDimitry Andric class MergeChunk : public NonSectionChunk {
4900b57cec5SDimitry Andric public:
4910b57cec5SDimitry Andric MergeChunk(uint32_t alignment);
492349cc55cSDimitry Andric static void addSection(COFFLinkerContext &ctx, SectionChunk *c);
4930b57cec5SDimitry Andric void finalizeContents();
4940b57cec5SDimitry Andric void assignSubsectionRVAs();
4950b57cec5SDimitry Andric
4960b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const override;
getSectionName()4970b57cec5SDimitry Andric StringRef getSectionName() const override { return ".rdata"; }
4980b57cec5SDimitry Andric size_t getSize() const override;
4990b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
5000b57cec5SDimitry Andric
5010b57cec5SDimitry Andric std::vector<SectionChunk *> sections;
5020b57cec5SDimitry Andric
5030b57cec5SDimitry Andric private:
5040b57cec5SDimitry Andric llvm::StringTableBuilder builder;
5050b57cec5SDimitry Andric bool finalized = false;
5060b57cec5SDimitry Andric };
5070b57cec5SDimitry Andric
5080b57cec5SDimitry Andric // A chunk for common symbols. Common chunks don't have actual data.
5090b57cec5SDimitry Andric class CommonChunk : public NonSectionChunk {
5100b57cec5SDimitry Andric public:
5110b57cec5SDimitry Andric CommonChunk(const COFFSymbolRef sym);
getSize()5120b57cec5SDimitry Andric size_t getSize() const override { return sym.getValue(); }
5130b57cec5SDimitry Andric uint32_t getOutputCharacteristics() const override;
getSectionName()5140b57cec5SDimitry Andric StringRef getSectionName() const override { return ".bss"; }
5150b57cec5SDimitry Andric
5160b57cec5SDimitry Andric private:
5170b57cec5SDimitry Andric const COFFSymbolRef sym;
5180b57cec5SDimitry Andric };
5190b57cec5SDimitry Andric
5200b57cec5SDimitry Andric // A chunk for linker-created strings.
5210b57cec5SDimitry Andric class StringChunk : public NonSectionChunk {
5220b57cec5SDimitry Andric public:
StringChunk(StringRef s)5230b57cec5SDimitry Andric explicit StringChunk(StringRef s) : str(s) {}
getSize()5240b57cec5SDimitry Andric size_t getSize() const override { return str.size() + 1; }
5250b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
5260b57cec5SDimitry Andric
5270b57cec5SDimitry Andric private:
5280b57cec5SDimitry Andric StringRef str;
5290b57cec5SDimitry Andric };
5300b57cec5SDimitry Andric
5310b57cec5SDimitry Andric static const uint8_t importThunkX86[] = {
5320b57cec5SDimitry Andric 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
5330b57cec5SDimitry Andric };
5340b57cec5SDimitry Andric
5350b57cec5SDimitry Andric static const uint8_t importThunkARM[] = {
5360b57cec5SDimitry Andric 0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
5370b57cec5SDimitry Andric 0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
5380b57cec5SDimitry Andric 0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
5390b57cec5SDimitry Andric };
5400b57cec5SDimitry Andric
5410b57cec5SDimitry Andric static const uint8_t importThunkARM64[] = {
5420b57cec5SDimitry Andric 0x10, 0x00, 0x00, 0x90, // adrp x16, #0
5430b57cec5SDimitry Andric 0x10, 0x02, 0x40, 0xf9, // ldr x16, [x16]
5440b57cec5SDimitry Andric 0x00, 0x02, 0x1f, 0xd6, // br x16
5450b57cec5SDimitry Andric };
5460b57cec5SDimitry Andric
5470b57cec5SDimitry Andric // Windows-specific.
5480b57cec5SDimitry Andric // A chunk for DLL import jump table entry. In a final output, its
5490b57cec5SDimitry Andric // contents will be a JMP instruction to some __imp_ symbol.
5505f757f3fSDimitry Andric class ImportThunkChunk : public NonSectionCodeChunk {
5510b57cec5SDimitry Andric public:
ImportThunkChunk(COFFLinkerContext & ctx,Defined * s)552bdd1243dSDimitry Andric ImportThunkChunk(COFFLinkerContext &ctx, Defined *s)
5535f757f3fSDimitry Andric : NonSectionCodeChunk(ImportThunkKind), impSymbol(s), ctx(ctx) {}
classof(const Chunk * c)5540b57cec5SDimitry Andric static bool classof(const Chunk *c) { return c->kind() == ImportThunkKind; }
5550b57cec5SDimitry Andric
5560b57cec5SDimitry Andric protected:
5570b57cec5SDimitry Andric Defined *impSymbol;
558bdd1243dSDimitry Andric COFFLinkerContext &ctx;
5590b57cec5SDimitry Andric };
5600b57cec5SDimitry Andric
5610b57cec5SDimitry Andric class ImportThunkChunkX64 : public ImportThunkChunk {
5620b57cec5SDimitry Andric public:
563bdd1243dSDimitry Andric explicit ImportThunkChunkX64(COFFLinkerContext &ctx, Defined *s);
getSize()5640b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkX86); }
5650b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
getMachine()5665f757f3fSDimitry Andric MachineTypes getMachine() const override { return AMD64; }
5670b57cec5SDimitry Andric };
5680b57cec5SDimitry Andric
5690b57cec5SDimitry Andric class ImportThunkChunkX86 : public ImportThunkChunk {
5700b57cec5SDimitry Andric public:
ImportThunkChunkX86(COFFLinkerContext & ctx,Defined * s)571bdd1243dSDimitry Andric explicit ImportThunkChunkX86(COFFLinkerContext &ctx, Defined *s)
572bdd1243dSDimitry Andric : ImportThunkChunk(ctx, s) {}
getSize()5730b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkX86); }
5740b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res) override;
5750b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
getMachine()5765f757f3fSDimitry Andric MachineTypes getMachine() const override { return I386; }
5770b57cec5SDimitry Andric };
5780b57cec5SDimitry Andric
5790b57cec5SDimitry Andric class ImportThunkChunkARM : public ImportThunkChunk {
5800b57cec5SDimitry Andric public:
ImportThunkChunkARM(COFFLinkerContext & ctx,Defined * s)581bdd1243dSDimitry Andric explicit ImportThunkChunkARM(COFFLinkerContext &ctx, Defined *s)
582bdd1243dSDimitry Andric : ImportThunkChunk(ctx, s) {
583e837bb5cSDimitry Andric setAlignment(2);
584e837bb5cSDimitry Andric }
getSize()5850b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkARM); }
5860b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res) override;
5870b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
getMachine()5885f757f3fSDimitry Andric MachineTypes getMachine() const override { return ARMNT; }
5890b57cec5SDimitry Andric };
5900b57cec5SDimitry Andric
5910b57cec5SDimitry Andric class ImportThunkChunkARM64 : public ImportThunkChunk {
5920b57cec5SDimitry Andric public:
ImportThunkChunkARM64(COFFLinkerContext & ctx,Defined * s)593bdd1243dSDimitry Andric explicit ImportThunkChunkARM64(COFFLinkerContext &ctx, Defined *s)
594bdd1243dSDimitry Andric : ImportThunkChunk(ctx, s) {
595e837bb5cSDimitry Andric setAlignment(4);
596e837bb5cSDimitry Andric }
getSize()5970b57cec5SDimitry Andric size_t getSize() const override { return sizeof(importThunkARM64); }
5980b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
getMachine()5995f757f3fSDimitry Andric MachineTypes getMachine() const override { return ARM64; }
6000b57cec5SDimitry Andric };
6010b57cec5SDimitry Andric
6025f757f3fSDimitry Andric class RangeExtensionThunkARM : public NonSectionCodeChunk {
6030b57cec5SDimitry Andric public:
RangeExtensionThunkARM(COFFLinkerContext & ctx,Defined * t)604bdd1243dSDimitry Andric explicit RangeExtensionThunkARM(COFFLinkerContext &ctx, Defined *t)
605bdd1243dSDimitry Andric : target(t), ctx(ctx) {
606bdd1243dSDimitry Andric setAlignment(2);
607bdd1243dSDimitry Andric }
6080b57cec5SDimitry Andric size_t getSize() const override;
6090b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
getMachine()6105f757f3fSDimitry Andric MachineTypes getMachine() const override { return ARMNT; }
6110b57cec5SDimitry Andric
6120b57cec5SDimitry Andric Defined *target;
613bdd1243dSDimitry Andric
614bdd1243dSDimitry Andric private:
615bdd1243dSDimitry Andric COFFLinkerContext &ctx;
6160b57cec5SDimitry Andric };
6170b57cec5SDimitry Andric
6185f757f3fSDimitry Andric class RangeExtensionThunkARM64 : public NonSectionCodeChunk {
6190b57cec5SDimitry Andric public:
RangeExtensionThunkARM64(COFFLinkerContext & ctx,Defined * t)620bdd1243dSDimitry Andric explicit RangeExtensionThunkARM64(COFFLinkerContext &ctx, Defined *t)
621bdd1243dSDimitry Andric : target(t), ctx(ctx) {
622bdd1243dSDimitry Andric setAlignment(4);
623bdd1243dSDimitry Andric }
6240b57cec5SDimitry Andric size_t getSize() const override;
6250b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
getMachine()6265f757f3fSDimitry Andric MachineTypes getMachine() const override { return ARM64; }
6270b57cec5SDimitry Andric
6280b57cec5SDimitry Andric Defined *target;
629bdd1243dSDimitry Andric
630bdd1243dSDimitry Andric private:
631bdd1243dSDimitry Andric COFFLinkerContext &ctx;
6320b57cec5SDimitry Andric };
6330b57cec5SDimitry Andric
6340b57cec5SDimitry Andric // Windows-specific.
6350b57cec5SDimitry Andric // See comments for DefinedLocalImport class.
6360b57cec5SDimitry Andric class LocalImportChunk : public NonSectionChunk {
6370b57cec5SDimitry Andric public:
638bdd1243dSDimitry Andric explicit LocalImportChunk(COFFLinkerContext &ctx, Defined *s);
6390b57cec5SDimitry Andric size_t getSize() const override;
6400b57cec5SDimitry Andric void getBaserels(std::vector<Baserel> *res) override;
6410b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
6420b57cec5SDimitry Andric
6430b57cec5SDimitry Andric private:
6440b57cec5SDimitry Andric Defined *sym;
645bdd1243dSDimitry Andric COFFLinkerContext &ctx;
6460b57cec5SDimitry Andric };
6470b57cec5SDimitry Andric
6480b57cec5SDimitry Andric // Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and
6490b57cec5SDimitry Andric // offset into the chunk. Order does not matter as the RVA table will be sorted
6500b57cec5SDimitry Andric // later.
6510b57cec5SDimitry Andric struct ChunkAndOffset {
6520b57cec5SDimitry Andric Chunk *inputChunk;
6530b57cec5SDimitry Andric uint32_t offset;
6540b57cec5SDimitry Andric
6550b57cec5SDimitry Andric struct DenseMapInfo {
getEmptyKeyChunkAndOffset::DenseMapInfo6560b57cec5SDimitry Andric static ChunkAndOffset getEmptyKey() {
6570b57cec5SDimitry Andric return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0};
6580b57cec5SDimitry Andric }
getTombstoneKeyChunkAndOffset::DenseMapInfo6590b57cec5SDimitry Andric static ChunkAndOffset getTombstoneKey() {
6600b57cec5SDimitry Andric return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0};
6610b57cec5SDimitry Andric }
getHashValueChunkAndOffset::DenseMapInfo6620b57cec5SDimitry Andric static unsigned getHashValue(const ChunkAndOffset &co) {
6630b57cec5SDimitry Andric return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue(
6640b57cec5SDimitry Andric {co.inputChunk, co.offset});
6650b57cec5SDimitry Andric }
isEqualChunkAndOffset::DenseMapInfo6660b57cec5SDimitry Andric static bool isEqual(const ChunkAndOffset &lhs, const ChunkAndOffset &rhs) {
6670b57cec5SDimitry Andric return lhs.inputChunk == rhs.inputChunk && lhs.offset == rhs.offset;
6680b57cec5SDimitry Andric }
6690b57cec5SDimitry Andric };
6700b57cec5SDimitry Andric };
6710b57cec5SDimitry Andric
6720b57cec5SDimitry Andric using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>;
6730b57cec5SDimitry Andric
6740b57cec5SDimitry Andric // Table which contains symbol RVAs. Used for /safeseh and /guard:cf.
6750b57cec5SDimitry Andric class RVATableChunk : public NonSectionChunk {
6760b57cec5SDimitry Andric public:
RVATableChunk(SymbolRVASet s)6770b57cec5SDimitry Andric explicit RVATableChunk(SymbolRVASet s) : syms(std::move(s)) {}
getSize()6780b57cec5SDimitry Andric size_t getSize() const override { return syms.size() * 4; }
6790b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
6800b57cec5SDimitry Andric
6810b57cec5SDimitry Andric private:
6820b57cec5SDimitry Andric SymbolRVASet syms;
6830b57cec5SDimitry Andric };
6840b57cec5SDimitry Andric
685fe6060f1SDimitry Andric // Table which contains symbol RVAs with flags. Used for /guard:ehcont.
686fe6060f1SDimitry Andric class RVAFlagTableChunk : public NonSectionChunk {
687fe6060f1SDimitry Andric public:
RVAFlagTableChunk(SymbolRVASet s)688fe6060f1SDimitry Andric explicit RVAFlagTableChunk(SymbolRVASet s) : syms(std::move(s)) {}
getSize()689fe6060f1SDimitry Andric size_t getSize() const override { return syms.size() * 5; }
690fe6060f1SDimitry Andric void writeTo(uint8_t *buf) const override;
691fe6060f1SDimitry Andric
692fe6060f1SDimitry Andric private:
693fe6060f1SDimitry Andric SymbolRVASet syms;
694fe6060f1SDimitry Andric };
695fe6060f1SDimitry Andric
6960b57cec5SDimitry Andric // Windows-specific.
6970b57cec5SDimitry Andric // This class represents a block in .reloc section.
6980b57cec5SDimitry Andric // See the PE/COFF spec 5.6 for details.
6990b57cec5SDimitry Andric class BaserelChunk : public NonSectionChunk {
7000b57cec5SDimitry Andric public:
7010b57cec5SDimitry Andric BaserelChunk(uint32_t page, Baserel *begin, Baserel *end);
getSize()7020b57cec5SDimitry Andric size_t getSize() const override { return data.size(); }
7030b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
7040b57cec5SDimitry Andric
7050b57cec5SDimitry Andric private:
7060b57cec5SDimitry Andric std::vector<uint8_t> data;
7070b57cec5SDimitry Andric };
7080b57cec5SDimitry Andric
7090b57cec5SDimitry Andric class Baserel {
7100b57cec5SDimitry Andric public:
Baserel(uint32_t v,uint8_t ty)7110b57cec5SDimitry Andric Baserel(uint32_t v, uint8_t ty) : rva(v), type(ty) {}
Baserel(uint32_t v,llvm::COFF::MachineTypes machine)712bdd1243dSDimitry Andric explicit Baserel(uint32_t v, llvm::COFF::MachineTypes machine)
713bdd1243dSDimitry Andric : Baserel(v, getDefaultType(machine)) {}
714bdd1243dSDimitry Andric uint8_t getDefaultType(llvm::COFF::MachineTypes machine);
7150b57cec5SDimitry Andric
7160b57cec5SDimitry Andric uint32_t rva;
7170b57cec5SDimitry Andric uint8_t type;
7180b57cec5SDimitry Andric };
7190b57cec5SDimitry Andric
7200b57cec5SDimitry Andric // This is a placeholder Chunk, to allow attaching a DefinedSynthetic to a
7210b57cec5SDimitry Andric // specific place in a section, without any data. This is used for the MinGW
7220b57cec5SDimitry Andric // specific symbol __RUNTIME_PSEUDO_RELOC_LIST_END__, even though the concept
7230b57cec5SDimitry Andric // of an empty chunk isn't MinGW specific.
7240b57cec5SDimitry Andric class EmptyChunk : public NonSectionChunk {
7250b57cec5SDimitry Andric public:
EmptyChunk()7260b57cec5SDimitry Andric EmptyChunk() {}
getSize()7270b57cec5SDimitry Andric size_t getSize() const override { return 0; }
writeTo(uint8_t * buf)7280b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override {}
7290b57cec5SDimitry Andric };
7300b57cec5SDimitry Andric
7315f757f3fSDimitry Andric class ECCodeMapEntry {
7325f757f3fSDimitry Andric public:
ECCodeMapEntry(Chunk * first,Chunk * last,chpe_range_type type)7335f757f3fSDimitry Andric ECCodeMapEntry(Chunk *first, Chunk *last, chpe_range_type type)
7345f757f3fSDimitry Andric : first(first), last(last), type(type) {}
7355f757f3fSDimitry Andric Chunk *first;
7365f757f3fSDimitry Andric Chunk *last;
7375f757f3fSDimitry Andric chpe_range_type type;
7385f757f3fSDimitry Andric };
7395f757f3fSDimitry Andric
7405f757f3fSDimitry Andric // This is a chunk containing CHPE code map on EC targets. It's a table
7415f757f3fSDimitry Andric // of address ranges and their types.
7425f757f3fSDimitry Andric class ECCodeMapChunk : public NonSectionChunk {
7435f757f3fSDimitry Andric public:
ECCodeMapChunk(std::vector<ECCodeMapEntry> & map)7445f757f3fSDimitry Andric ECCodeMapChunk(std::vector<ECCodeMapEntry> &map) : map(map) {}
7455f757f3fSDimitry Andric size_t getSize() const override;
7465f757f3fSDimitry Andric void writeTo(uint8_t *buf) const override;
7475f757f3fSDimitry Andric
7485f757f3fSDimitry Andric private:
7495f757f3fSDimitry Andric std::vector<ECCodeMapEntry> ↦
7505f757f3fSDimitry Andric };
7515f757f3fSDimitry Andric
7520b57cec5SDimitry Andric // MinGW specific, for the "automatic import of variables from DLLs" feature.
7530b57cec5SDimitry Andric // This provides the table of runtime pseudo relocations, for variable
7540b57cec5SDimitry Andric // references that turned out to need to be imported from a DLL even though
7550b57cec5SDimitry Andric // the reference didn't use the dllimport attribute. The MinGW runtime will
7560b57cec5SDimitry Andric // process this table after loading, before handling control over to user
7570b57cec5SDimitry Andric // code.
7580b57cec5SDimitry Andric class PseudoRelocTableChunk : public NonSectionChunk {
7590b57cec5SDimitry Andric public:
PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> & relocs)7600b57cec5SDimitry Andric PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &relocs)
7610b57cec5SDimitry Andric : relocs(std::move(relocs)) {
7620b57cec5SDimitry Andric setAlignment(4);
7630b57cec5SDimitry Andric }
7640b57cec5SDimitry Andric size_t getSize() const override;
7650b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
7660b57cec5SDimitry Andric
7670b57cec5SDimitry Andric private:
7680b57cec5SDimitry Andric std::vector<RuntimePseudoReloc> relocs;
7690b57cec5SDimitry Andric };
7700b57cec5SDimitry Andric
7710b57cec5SDimitry Andric // MinGW specific. A Chunk that contains one pointer-sized absolute value.
7720b57cec5SDimitry Andric class AbsolutePointerChunk : public NonSectionChunk {
7730b57cec5SDimitry Andric public:
AbsolutePointerChunk(COFFLinkerContext & ctx,uint64_t value)774bdd1243dSDimitry Andric AbsolutePointerChunk(COFFLinkerContext &ctx, uint64_t value)
775bdd1243dSDimitry Andric : value(value), ctx(ctx) {
7760b57cec5SDimitry Andric setAlignment(getSize());
7770b57cec5SDimitry Andric }
7780b57cec5SDimitry Andric size_t getSize() const override;
7790b57cec5SDimitry Andric void writeTo(uint8_t *buf) const override;
7800b57cec5SDimitry Andric
7810b57cec5SDimitry Andric private:
7820b57cec5SDimitry Andric uint64_t value;
783bdd1243dSDimitry Andric COFFLinkerContext &ctx;
7840b57cec5SDimitry Andric };
7850b57cec5SDimitry Andric
7860b57cec5SDimitry Andric // Return true if this file has the hotpatch flag set to true in the S_COMPILE3
7870b57cec5SDimitry Andric // record in codeview debug info. Also returns true for some thunks synthesized
7880b57cec5SDimitry Andric // by the linker.
isHotPatchable()7890b57cec5SDimitry Andric inline bool Chunk::isHotPatchable() const {
7900b57cec5SDimitry Andric if (auto *sc = dyn_cast<SectionChunk>(this))
7910b57cec5SDimitry Andric return sc->file->hotPatchable;
7920b57cec5SDimitry Andric else if (isa<ImportThunkChunk>(this))
7930b57cec5SDimitry Andric return true;
7940b57cec5SDimitry Andric return false;
7950b57cec5SDimitry Andric }
7960b57cec5SDimitry Andric
getEntryThunk()797*0fca6ea1SDimitry Andric inline Defined *Chunk::getEntryThunk() const {
798*0fca6ea1SDimitry Andric if (auto *c = dyn_cast<const SectionChunkEC>(this))
799*0fca6ea1SDimitry Andric return c->entryThunk;
800*0fca6ea1SDimitry Andric return nullptr;
801*0fca6ea1SDimitry Andric }
802*0fca6ea1SDimitry Andric
setEntryThunk(Defined * entryThunk)803*0fca6ea1SDimitry Andric inline void Chunk::setEntryThunk(Defined *entryThunk) {
804*0fca6ea1SDimitry Andric if (auto c = dyn_cast<SectionChunkEC>(this))
805*0fca6ea1SDimitry Andric c->entryThunk = entryThunk;
806*0fca6ea1SDimitry Andric }
807*0fca6ea1SDimitry Andric
8080b57cec5SDimitry Andric void applyMOV32T(uint8_t *off, uint32_t v);
8090b57cec5SDimitry Andric void applyBranch24T(uint8_t *off, int32_t v);
8100b57cec5SDimitry Andric
8110b57cec5SDimitry Andric void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift);
8120b57cec5SDimitry Andric void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit);
8130b57cec5SDimitry Andric void applyArm64Branch26(uint8_t *off, int64_t v);
8140b57cec5SDimitry Andric
815bdd1243dSDimitry Andric // Convenience class for initializing a coff_section with specific flags.
816bdd1243dSDimitry Andric class FakeSection {
817bdd1243dSDimitry Andric public:
FakeSection(int c)818bdd1243dSDimitry Andric FakeSection(int c) { section.Characteristics = c; }
819bdd1243dSDimitry Andric
820bdd1243dSDimitry Andric coff_section section;
821bdd1243dSDimitry Andric };
822bdd1243dSDimitry Andric
823bdd1243dSDimitry Andric // Convenience class for initializing a SectionChunk with specific flags.
824bdd1243dSDimitry Andric class FakeSectionChunk {
825bdd1243dSDimitry Andric public:
FakeSectionChunk(const coff_section * section)826bdd1243dSDimitry Andric FakeSectionChunk(const coff_section *section) : chunk(nullptr, section) {
827bdd1243dSDimitry Andric // Comdats from LTO files can't be fully treated as regular comdats
828bdd1243dSDimitry Andric // at this point; we don't know what size or contents they are going to
829bdd1243dSDimitry Andric // have, so we can't do proper checking of such aspects of them.
830bdd1243dSDimitry Andric chunk.selection = llvm::COFF::IMAGE_COMDAT_SELECT_ANY;
831bdd1243dSDimitry Andric }
832bdd1243dSDimitry Andric
833bdd1243dSDimitry Andric SectionChunk chunk;
834bdd1243dSDimitry Andric };
835bdd1243dSDimitry Andric
836bdd1243dSDimitry Andric } // namespace lld::coff
8370b57cec5SDimitry Andric
8380b57cec5SDimitry Andric namespace llvm {
8390b57cec5SDimitry Andric template <>
8400b57cec5SDimitry Andric struct DenseMapInfo<lld::coff::ChunkAndOffset>
8410b57cec5SDimitry Andric : lld::coff::ChunkAndOffset::DenseMapInfo {};
8420b57cec5SDimitry Andric }
8430b57cec5SDimitry Andric
8440b57cec5SDimitry Andric #endif
845