10b57cec5SDimitry Andric //===- InputSection.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_ELF_INPUT_SECTION_H 100b57cec5SDimitry Andric #define LLD_ELF_INPUT_SECTION_H 110b57cec5SDimitry Andric 127a6dacacSDimitry Andric #include "Config.h" 130b57cec5SDimitry Andric #include "Relocations.h" 14753f127fSDimitry Andric #include "lld/Common/CommonLinkerContext.h" 150b57cec5SDimitry Andric #include "lld/Common/LLVM.h" 16753f127fSDimitry Andric #include "lld/Common/Memory.h" 170b57cec5SDimitry Andric #include "llvm/ADT/CachedHashString.h" 180b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 1906c3fb27SDimitry Andric #include "llvm/ADT/StringExtras.h" 200b57cec5SDimitry Andric #include "llvm/ADT/TinyPtrVector.h" 210b57cec5SDimitry Andric #include "llvm/Object/ELF.h" 22bdd1243dSDimitry Andric #include "llvm/Support/Compiler.h" 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric namespace lld { 250b57cec5SDimitry Andric namespace elf { 260b57cec5SDimitry Andric 2781ad6265SDimitry Andric class InputFile; 280b57cec5SDimitry Andric class Symbol; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric class Defined; 310b57cec5SDimitry Andric struct Partition; 320b57cec5SDimitry Andric class SyntheticSection; 330b57cec5SDimitry Andric template <class ELFT> class ObjFile; 340b57cec5SDimitry Andric class OutputSection; 350b57cec5SDimitry Andric 36bdd1243dSDimitry Andric LLVM_LIBRARY_VISIBILITY extern std::vector<Partition> partitions; 370b57cec5SDimitry Andric 38349cc55cSDimitry Andric // Returned by InputSectionBase::relsOrRelas. At least one member is empty. 39349cc55cSDimitry Andric template <class ELFT> struct RelsOrRelas { 40349cc55cSDimitry Andric ArrayRef<typename ELFT::Rel> rels; 41349cc55cSDimitry Andric ArrayRef<typename ELFT::Rela> relas; 42349cc55cSDimitry Andric bool areRelocsRel() const { return rels.size(); } 43349cc55cSDimitry Andric }; 44349cc55cSDimitry Andric 450b57cec5SDimitry Andric // This is the base class of all sections that lld handles. Some are sections in 460b57cec5SDimitry Andric // input files, some are sections in the produced output file and some exist 470b57cec5SDimitry Andric // just as a convenience for implementing special ways of combining some 480b57cec5SDimitry Andric // sections. 490b57cec5SDimitry Andric class SectionBase { 500b57cec5SDimitry Andric public: 5181ad6265SDimitry Andric enum Kind { Regular, Synthetic, EHFrame, Merge, Output }; 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric Kind kind() const { return (Kind)sectionKind; } 540b57cec5SDimitry Andric 55e8d8bef9SDimitry Andric uint8_t sectionKind : 3; 560b57cec5SDimitry Andric 5785868e8aSDimitry Andric // The next two bit fields are only used by InputSectionBase, but we 580b57cec5SDimitry Andric // put them here so the struct packs better. 590b57cec5SDimitry Andric 60e8d8bef9SDimitry Andric uint8_t bss : 1; 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric // Set for sections that should not be folded by ICF. 63e8d8bef9SDimitry Andric uint8_t keepUnique : 1; 640b57cec5SDimitry Andric 65bdd1243dSDimitry Andric uint8_t partition = 1; 66bdd1243dSDimitry Andric uint32_t type; 67bdd1243dSDimitry Andric StringRef name; 68bdd1243dSDimitry Andric 690b57cec5SDimitry Andric // The 1-indexed partition that this section is assigned to by the garbage 700b57cec5SDimitry Andric // collector, or 0 if this section is dead. Normally there is only one 710b57cec5SDimitry Andric // partition, so this will either be 0 or 1. 720b57cec5SDimitry Andric elf::Partition &getPartition() const; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // These corresponds to the fields in Elf_Shdr. 750b57cec5SDimitry Andric uint64_t flags; 76bdd1243dSDimitry Andric uint32_t addralign; 774824e7fdSDimitry Andric uint32_t entsize; 780b57cec5SDimitry Andric uint32_t link; 790b57cec5SDimitry Andric uint32_t info; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric OutputSection *getOutputSection(); 820b57cec5SDimitry Andric const OutputSection *getOutputSection() const { 830b57cec5SDimitry Andric return const_cast<SectionBase *>(this)->getOutputSection(); 840b57cec5SDimitry Andric } 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric // Translate an offset in the input section to an offset in the output 870b57cec5SDimitry Andric // section. 880b57cec5SDimitry Andric uint64_t getOffset(uint64_t offset) const; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric uint64_t getVA(uint64_t offset = 0) const; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric bool isLive() const { return partition != 0; } 930b57cec5SDimitry Andric void markLive() { partition = 1; } 940b57cec5SDimitry Andric void markDead() { partition = 0; } 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric protected: 974824e7fdSDimitry Andric constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags, 98bdd1243dSDimitry Andric uint32_t entsize, uint32_t addralign, uint32_t type, 990b57cec5SDimitry Andric uint32_t info, uint32_t link) 100bdd1243dSDimitry Andric : sectionKind(sectionKind), bss(false), keepUnique(false), type(type), 101bdd1243dSDimitry Andric name(name), flags(flags), addralign(addralign), entsize(entsize), 1021fd87a68SDimitry Andric link(link), info(info) {} 1030b57cec5SDimitry Andric }; 1040b57cec5SDimitry Andric 105*74626c16SDimitry Andric struct SymbolAnchor { 106*74626c16SDimitry Andric uint64_t offset; 107*74626c16SDimitry Andric Defined *d; 108*74626c16SDimitry Andric bool end; // true for the anchor of st_value+st_size 109*74626c16SDimitry Andric }; 110*74626c16SDimitry Andric 111*74626c16SDimitry Andric struct RelaxAux { 112*74626c16SDimitry Andric // This records symbol start and end offsets which will be adjusted according 113*74626c16SDimitry Andric // to the nearest relocDeltas element. 114*74626c16SDimitry Andric SmallVector<SymbolAnchor, 0> anchors; 115*74626c16SDimitry Andric // For relocations[i], the actual offset is 116*74626c16SDimitry Andric // r_offset - (i ? relocDeltas[i-1] : 0). 117*74626c16SDimitry Andric std::unique_ptr<uint32_t[]> relocDeltas; 118*74626c16SDimitry Andric // For relocations[i], the actual type is relocTypes[i]. 119*74626c16SDimitry Andric std::unique_ptr<RelType[]> relocTypes; 120*74626c16SDimitry Andric SmallVector<uint32_t, 0> writes; 121*74626c16SDimitry Andric }; 122753f127fSDimitry Andric 1230b57cec5SDimitry Andric // This corresponds to a section of an input file. 1240b57cec5SDimitry Andric class InputSectionBase : public SectionBase { 1250b57cec5SDimitry Andric public: 1260b57cec5SDimitry Andric template <class ELFT> 1270b57cec5SDimitry Andric InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header, 1280b57cec5SDimitry Andric StringRef name, Kind sectionKind); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric InputSectionBase(InputFile *file, uint64_t flags, uint32_t type, 1310b57cec5SDimitry Andric uint64_t entsize, uint32_t link, uint32_t info, 132bdd1243dSDimitry Andric uint32_t addralign, ArrayRef<uint8_t> data, StringRef name, 1330b57cec5SDimitry Andric Kind sectionKind); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric static bool classof(const SectionBase *s) { return s->kind() != Output; } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric // The file which contains this section. Its dynamic type is always 1380b57cec5SDimitry Andric // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as 1390b57cec5SDimitry Andric // its static type. 1400b57cec5SDimitry Andric InputFile *file; 1410b57cec5SDimitry Andric 1421fd87a68SDimitry Andric // Input sections are part of an output section. Special sections 1431fd87a68SDimitry Andric // like .eh_frame and merge sections are first combined into a 1441fd87a68SDimitry Andric // synthetic section that is then added to an output section. In all 1451fd87a68SDimitry Andric // cases this points one level up. 1461fd87a68SDimitry Andric SectionBase *parent = nullptr; 1471fd87a68SDimitry Andric 1484824e7fdSDimitry Andric // Section index of the relocation section if exists. 1494824e7fdSDimitry Andric uint32_t relSecIdx = 0; 1504824e7fdSDimitry Andric 1510b57cec5SDimitry Andric template <class ELFT> ObjFile<ELFT> *getFile() const { 1520b57cec5SDimitry Andric return cast_or_null<ObjFile<ELFT>>(file); 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric 155753f127fSDimitry Andric // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to 156753f127fSDimitry Andric // indicate the number of bytes which is not counted in the size. This should 157753f127fSDimitry Andric // be reset to zero after uses. 15806c3fb27SDimitry Andric uint32_t bytesDropped = 0; 1595ffd83dbSDimitry Andric 160bdd1243dSDimitry Andric mutable bool compressed = false; 161bdd1243dSDimitry Andric 162e8d8bef9SDimitry Andric // Whether the section needs to be padded with a NOP filler due to 163e8d8bef9SDimitry Andric // deleteFallThruJmpInsn. 164e8d8bef9SDimitry Andric bool nopFiller = false; 165e8d8bef9SDimitry Andric 16604eeddc0SDimitry Andric void drop_back(unsigned num) { 16704eeddc0SDimitry Andric assert(bytesDropped + num < 256); 16804eeddc0SDimitry Andric bytesDropped += num; 16904eeddc0SDimitry Andric } 1705ffd83dbSDimitry Andric 1715ffd83dbSDimitry Andric void push_back(uint64_t num) { 1725ffd83dbSDimitry Andric assert(bytesDropped >= num); 1735ffd83dbSDimitry Andric bytesDropped -= num; 1745ffd83dbSDimitry Andric } 1755ffd83dbSDimitry Andric 176bdd1243dSDimitry Andric mutable const uint8_t *content_; 177bdd1243dSDimitry Andric uint64_t size; 17881ad6265SDimitry Andric 1795ffd83dbSDimitry Andric void trim() { 1805ffd83dbSDimitry Andric if (bytesDropped) { 181bdd1243dSDimitry Andric size -= bytesDropped; 1825ffd83dbSDimitry Andric bytesDropped = 0; 1835ffd83dbSDimitry Andric } 1845ffd83dbSDimitry Andric } 1855ffd83dbSDimitry Andric 186bdd1243dSDimitry Andric ArrayRef<uint8_t> content() const { 187bdd1243dSDimitry Andric return ArrayRef<uint8_t>(content_, size); 188bdd1243dSDimitry Andric } 189bdd1243dSDimitry Andric ArrayRef<uint8_t> contentMaybeDecompress() const { 190bdd1243dSDimitry Andric if (compressed) 191bdd1243dSDimitry Andric decompress(); 192bdd1243dSDimitry Andric return content(); 1930b57cec5SDimitry Andric } 1940b57cec5SDimitry Andric 195480093f4SDimitry Andric // The next member in the section group if this section is in a group. This is 196480093f4SDimitry Andric // used by --gc-sections. 197480093f4SDimitry Andric InputSectionBase *nextInSectionGroup = nullptr; 198480093f4SDimitry Andric 199349cc55cSDimitry Andric template <class ELFT> RelsOrRelas<ELFT> relsOrRelas() const; 2000b57cec5SDimitry Andric 2010b57cec5SDimitry Andric // InputSections that are dependent on us (reverse dependency for GC) 2020b57cec5SDimitry Andric llvm::TinyPtrVector<InputSection *> dependentSections; 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric // Returns the size of this section (even if this is a common or BSS.) 2050b57cec5SDimitry Andric size_t getSize() const; 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric InputSection *getLinkOrderDep() const; 2080b57cec5SDimitry Andric 2095f757f3fSDimitry Andric // Get a symbol that encloses this offset from within the section. If type is 2105f757f3fSDimitry Andric // not zero, return a symbol with the specified type. 2115f757f3fSDimitry Andric Defined *getEnclosingSymbol(uint64_t offset, uint8_t type = 0) const; 2125f757f3fSDimitry Andric Defined *getEnclosingFunction(uint64_t offset) const { 2135f757f3fSDimitry Andric return getEnclosingSymbol(offset, llvm::ELF::STT_FUNC); 2145f757f3fSDimitry Andric } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric // Returns a source location string. Used to construct an error message. 2175f757f3fSDimitry Andric std::string getLocation(uint64_t offset) const; 2185f757f3fSDimitry Andric std::string getSrcMsg(const Symbol &sym, uint64_t offset) const; 2195f757f3fSDimitry Andric std::string getObjMsg(uint64_t offset) const; 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric // Each section knows how to relocate itself. These functions apply 2220b57cec5SDimitry Andric // relocations, assuming that Buf points to this section's copy in 2230b57cec5SDimitry Andric // the mmap'ed output buffer. 2240b57cec5SDimitry Andric template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd); 2255ffd83dbSDimitry Andric static uint64_t getRelocTargetVA(const InputFile *File, RelType Type, 2265ffd83dbSDimitry Andric int64_t A, uint64_t P, const Symbol &Sym, 2275ffd83dbSDimitry Andric RelExpr Expr); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric // The native ELF reloc data type is not very convenient to handle. 2300b57cec5SDimitry Andric // So we convert ELF reloc records to our own records in Relocations.cpp. 2310b57cec5SDimitry Andric // This vector contains such "cooked" relocations. 232e8d8bef9SDimitry Andric SmallVector<Relocation, 0> relocations; 2335ffd83dbSDimitry Andric 234bdd1243dSDimitry Andric void addReloc(const Relocation &r) { relocations.push_back(r); } 235bdd1243dSDimitry Andric MutableArrayRef<Relocation> relocs() { return relocations; } 236bdd1243dSDimitry Andric ArrayRef<Relocation> relocs() const { return relocations; } 237bdd1243dSDimitry Andric 238753f127fSDimitry Andric union { 2395ffd83dbSDimitry Andric // These are modifiers to jump instructions that are necessary when basic 240753f127fSDimitry Andric // block sections are enabled. Basic block sections creates opportunities 241753f127fSDimitry Andric // to relax jump instructions at basic block boundaries after reordering the 2425ffd83dbSDimitry Andric // basic blocks. 24304eeddc0SDimitry Andric JumpInstrMod *jumpInstrMod = nullptr; 2445ffd83dbSDimitry Andric 245*74626c16SDimitry Andric // Auxiliary information for RISC-V and LoongArch linker relaxation. 246*74626c16SDimitry Andric // They do not use jumpInstrMod. 247*74626c16SDimitry Andric RelaxAux *relaxAux; 248bdd1243dSDimitry Andric 249bdd1243dSDimitry Andric // The compressed content size when `compressed` is true. 250bdd1243dSDimitry Andric size_t compressedSize; 251753f127fSDimitry Andric }; 252753f127fSDimitry Andric 2530b57cec5SDimitry Andric // A function compiled with -fsplit-stack calling a function 2540b57cec5SDimitry Andric // compiled without -fsplit-stack needs its prologue adjusted. Find 2550b57cec5SDimitry Andric // such functions and adjust their prologues. This is very similar 2560b57cec5SDimitry Andric // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more 2570b57cec5SDimitry Andric // information. 2580b57cec5SDimitry Andric template <typename ELFT> 2590b57cec5SDimitry Andric void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end); 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric template <typename T> llvm::ArrayRef<T> getDataAs() const { 263bdd1243dSDimitry Andric size_t s = content().size(); 2640b57cec5SDimitry Andric assert(s % sizeof(T) == 0); 265bdd1243dSDimitry Andric return llvm::ArrayRef<T>((const T *)content().data(), s / sizeof(T)); 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric protected: 269d69d0756SDimitry Andric template <typename ELFT> 2700b57cec5SDimitry Andric void parseCompressedHeader(); 271bdd1243dSDimitry Andric void decompress() const; 2720b57cec5SDimitry Andric }; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // SectionPiece represents a piece of splittable section contents. 2750b57cec5SDimitry Andric // We allocate a lot of these and binary search on them. This means that they 2760b57cec5SDimitry Andric // have to be as compact as possible, which is why we don't store the size (can 2770b57cec5SDimitry Andric // be found by looking at the next one). 2780b57cec5SDimitry Andric struct SectionPiece { 2791fd87a68SDimitry Andric SectionPiece() = default; 2800b57cec5SDimitry Andric SectionPiece(size_t off, uint32_t hash, bool live) 2810eae32dcSDimitry Andric : inputOff(off), live(live), hash(hash >> 1) {} 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric uint32_t inputOff; 2840b57cec5SDimitry Andric uint32_t live : 1; 2850b57cec5SDimitry Andric uint32_t hash : 31; 2860b57cec5SDimitry Andric uint64_t outputOff = 0; 2870b57cec5SDimitry Andric }; 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big"); 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric // This corresponds to a SHF_MERGE section of an input file. 2920b57cec5SDimitry Andric class MergeInputSection : public InputSectionBase { 2930b57cec5SDimitry Andric public: 2940b57cec5SDimitry Andric template <class ELFT> 2950b57cec5SDimitry Andric MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 2960b57cec5SDimitry Andric StringRef name); 2970b57cec5SDimitry Andric MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize, 2980b57cec5SDimitry Andric ArrayRef<uint8_t> data, StringRef name); 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric static bool classof(const SectionBase *s) { return s->kind() == Merge; } 3010b57cec5SDimitry Andric void splitIntoPieces(); 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric // Translate an offset in the input section to an offset in the parent 3040b57cec5SDimitry Andric // MergeSyntheticSection. 3050b57cec5SDimitry Andric uint64_t getParentOffset(uint64_t offset) const; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric // Splittable sections are handled as a sequence of data 3080b57cec5SDimitry Andric // rather than a single large blob of data. 3090eae32dcSDimitry Andric SmallVector<SectionPiece, 0> pieces; 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric // Returns I'th piece's data. This function is very hot when 3120b57cec5SDimitry Andric // string merging is enabled, so we want to inline. 3130b57cec5SDimitry Andric LLVM_ATTRIBUTE_ALWAYS_INLINE 3140b57cec5SDimitry Andric llvm::CachedHashStringRef getData(size_t i) const { 3150b57cec5SDimitry Andric size_t begin = pieces[i].inputOff; 3160b57cec5SDimitry Andric size_t end = 317bdd1243dSDimitry Andric (pieces.size() - 1 == i) ? content().size() : pieces[i + 1].inputOff; 318bdd1243dSDimitry Andric return {toStringRef(content().slice(begin, end - begin)), pieces[i].hash}; 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // Returns the SectionPiece at a given input section offset. 32281ad6265SDimitry Andric SectionPiece &getSectionPiece(uint64_t offset); 32381ad6265SDimitry Andric const SectionPiece &getSectionPiece(uint64_t offset) const { 3240b57cec5SDimitry Andric return const_cast<MergeInputSection *>(this)->getSectionPiece(offset); 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 327bdd1243dSDimitry Andric SyntheticSection *getParent() const { 328bdd1243dSDimitry Andric return cast_or_null<SyntheticSection>(parent); 329bdd1243dSDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric private: 3321fd87a68SDimitry Andric void splitStrings(StringRef s, size_t size); 3330b57cec5SDimitry Andric void splitNonStrings(ArrayRef<uint8_t> a, size_t size); 3340b57cec5SDimitry Andric }; 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric struct EhSectionPiece { 3370b57cec5SDimitry Andric EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size, 3380b57cec5SDimitry Andric unsigned firstRelocation) 3390b57cec5SDimitry Andric : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {} 3400b57cec5SDimitry Andric 341e8d8bef9SDimitry Andric ArrayRef<uint8_t> data() const { 342bdd1243dSDimitry Andric return {sec->content().data() + this->inputOff, size}; 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric size_t inputOff; 3460b57cec5SDimitry Andric ssize_t outputOff = -1; 3470b57cec5SDimitry Andric InputSectionBase *sec; 3480b57cec5SDimitry Andric uint32_t size; 3490b57cec5SDimitry Andric unsigned firstRelocation; 3500b57cec5SDimitry Andric }; 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric // This corresponds to a .eh_frame section of an input file. 3530b57cec5SDimitry Andric class EhInputSection : public InputSectionBase { 3540b57cec5SDimitry Andric public: 3550b57cec5SDimitry Andric template <class ELFT> 3560b57cec5SDimitry Andric EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 3570b57cec5SDimitry Andric StringRef name); 3580b57cec5SDimitry Andric static bool classof(const SectionBase *s) { return s->kind() == EHFrame; } 3590b57cec5SDimitry Andric template <class ELFT> void split(); 3600b57cec5SDimitry Andric template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels); 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric // Splittable sections are handled as a sequence of data 3630b57cec5SDimitry Andric // rather than a single large blob of data. 364bdd1243dSDimitry Andric SmallVector<EhSectionPiece, 0> cies, fdes; 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric SyntheticSection *getParent() const; 36781ad6265SDimitry Andric uint64_t getParentOffset(uint64_t offset) const; 3680b57cec5SDimitry Andric }; 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric // This is a section that is added directly to an output section 3710b57cec5SDimitry Andric // instead of needing special combination via a synthetic section. This 3720b57cec5SDimitry Andric // includes all input sections with the exceptions of SHF_MERGE and 3730b57cec5SDimitry Andric // .eh_frame. It also includes the synthetic sections themselves. 3740b57cec5SDimitry Andric class InputSection : public InputSectionBase { 3750b57cec5SDimitry Andric public: 376bdd1243dSDimitry Andric InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t addralign, 3770b57cec5SDimitry Andric ArrayRef<uint8_t> data, StringRef name, Kind k = Regular); 3780b57cec5SDimitry Andric template <class ELFT> 3790b57cec5SDimitry Andric InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 3800b57cec5SDimitry Andric StringRef name); 3810b57cec5SDimitry Andric 38281ad6265SDimitry Andric static bool classof(const SectionBase *s) { 38381ad6265SDimitry Andric return s->kind() == SectionBase::Regular || 38481ad6265SDimitry Andric s->kind() == SectionBase::Synthetic; 38581ad6265SDimitry Andric } 38681ad6265SDimitry Andric 3870b57cec5SDimitry Andric // Write this section to a mmap'ed file, assuming Buf is pointing to 3880b57cec5SDimitry Andric // beginning of the output section. 3890b57cec5SDimitry Andric template <class ELFT> void writeTo(uint8_t *buf); 3900b57cec5SDimitry Andric 39181ad6265SDimitry Andric OutputSection *getParent() const { 39281ad6265SDimitry Andric return reinterpret_cast<OutputSection *>(parent); 39381ad6265SDimitry Andric } 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric // This variable has two usages. Initially, it represents an index in the 3960b57cec5SDimitry Andric // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER 3970b57cec5SDimitry Andric // sections. After assignAddresses is called, it represents the offset from 3980b57cec5SDimitry Andric // the beginning of the output section this section was assigned to. 3990b57cec5SDimitry Andric uint64_t outSecOff = 0; 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric InputSectionBase *getRelocatedSection() const; 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric template <class ELFT, class RelTy> 4040b57cec5SDimitry Andric void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels); 4050b57cec5SDimitry Andric 4060eae32dcSDimitry Andric // Points to the canonical section. If ICF folds two sections, repl pointer of 4070eae32dcSDimitry Andric // one section points to the other. 4080eae32dcSDimitry Andric InputSection *repl = this; 4090eae32dcSDimitry Andric 4100b57cec5SDimitry Andric // Used by ICF. 4110b57cec5SDimitry Andric uint32_t eqClass[2] = {0, 0}; 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric // Called by ICF to merge two input sections. 4140b57cec5SDimitry Andric void replace(InputSection *other); 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric static InputSection discarded; 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric private: 4195f757f3fSDimitry Andric template <class ELFT, class RelTy> void copyRelocations(uint8_t *buf); 4205f757f3fSDimitry Andric 4215f757f3fSDimitry Andric template <class ELFT, class RelTy, class RelIt> 4225f757f3fSDimitry Andric void copyRelocations(uint8_t *buf, llvm::iterator_range<RelIt> rels); 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric template <class ELFT> void copyShtGroup(uint8_t *buf); 4250b57cec5SDimitry Andric }; 4260b57cec5SDimitry Andric 42706c3fb27SDimitry Andric static_assert(sizeof(InputSection) <= 160, "InputSection is too big"); 428bdd1243dSDimitry Andric 429bdd1243dSDimitry Andric class SyntheticSection : public InputSection { 430bdd1243dSDimitry Andric public: 431bdd1243dSDimitry Andric SyntheticSection(uint64_t flags, uint32_t type, uint32_t addralign, 432bdd1243dSDimitry Andric StringRef name) 4337a6dacacSDimitry Andric : InputSection(ctx.internalFile, flags, type, addralign, {}, name, 434bdd1243dSDimitry Andric InputSectionBase::Synthetic) {} 435bdd1243dSDimitry Andric 436bdd1243dSDimitry Andric virtual ~SyntheticSection() = default; 437bdd1243dSDimitry Andric virtual size_t getSize() const = 0; 438bdd1243dSDimitry Andric virtual bool updateAllocSize() { return false; } 439bdd1243dSDimitry Andric // If the section has the SHF_ALLOC flag and the size may be changed if 440bdd1243dSDimitry Andric // thunks are added, update the section size. 441bdd1243dSDimitry Andric virtual bool isNeeded() const { return true; } 442bdd1243dSDimitry Andric virtual void finalizeContents() {} 443bdd1243dSDimitry Andric virtual void writeTo(uint8_t *buf) = 0; 444bdd1243dSDimitry Andric 445bdd1243dSDimitry Andric static bool classof(const SectionBase *sec) { 446bdd1243dSDimitry Andric return sec->kind() == InputSectionBase::Synthetic; 447bdd1243dSDimitry Andric } 448bdd1243dSDimitry Andric }; 449e8d8bef9SDimitry Andric 450d65cd7a5SDimitry Andric inline bool isDebugSection(const InputSectionBase &sec) { 4512418469bSDimitry Andric return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 && 45206c3fb27SDimitry Andric sec.name.starts_with(".debug"); 453d65cd7a5SDimitry Andric } 454d65cd7a5SDimitry Andric 4555ffd83dbSDimitry Andric // The set of TOC entries (.toc + addend) for which we should not apply 4565ffd83dbSDimitry Andric // toc-indirect to toc-relative relaxation. const Symbol * refers to the 4575ffd83dbSDimitry Andric // STT_SECTION symbol associated to the .toc input section. 4585ffd83dbSDimitry Andric extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax; 4595ffd83dbSDimitry Andric 4600b57cec5SDimitry Andric } // namespace elf 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric std::string toString(const elf::InputSectionBase *); 4630b57cec5SDimitry Andric } // namespace lld 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric #endif 466