xref: /freebsd/contrib/llvm-project/lld/ELF/InputSection.h (revision 753f127f3ace09432b2baeffd71a308760641a62)
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 
120b57cec5SDimitry Andric #include "Relocations.h"
13*753f127fSDimitry Andric #include "lld/Common/CommonLinkerContext.h"
140b57cec5SDimitry Andric #include "lld/Common/LLVM.h"
15*753f127fSDimitry Andric #include "lld/Common/Memory.h"
160b57cec5SDimitry Andric #include "llvm/ADT/CachedHashString.h"
170b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h"
180b57cec5SDimitry Andric #include "llvm/ADT/TinyPtrVector.h"
190b57cec5SDimitry Andric #include "llvm/Object/ELF.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace lld {
220b57cec5SDimitry Andric namespace elf {
230b57cec5SDimitry Andric 
2481ad6265SDimitry Andric class InputFile;
250b57cec5SDimitry Andric class Symbol;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric class Defined;
280b57cec5SDimitry Andric struct Partition;
290b57cec5SDimitry Andric class SyntheticSection;
300b57cec5SDimitry Andric template <class ELFT> class ObjFile;
310b57cec5SDimitry Andric class OutputSection;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric extern std::vector<Partition> partitions;
340b57cec5SDimitry Andric 
35349cc55cSDimitry Andric // Returned by InputSectionBase::relsOrRelas. At least one member is empty.
36349cc55cSDimitry Andric template <class ELFT> struct RelsOrRelas {
37349cc55cSDimitry Andric   ArrayRef<typename ELFT::Rel> rels;
38349cc55cSDimitry Andric   ArrayRef<typename ELFT::Rela> relas;
39349cc55cSDimitry Andric   bool areRelocsRel() const { return rels.size(); }
40349cc55cSDimitry Andric };
41349cc55cSDimitry Andric 
420b57cec5SDimitry Andric // This is the base class of all sections that lld handles. Some are sections in
430b57cec5SDimitry Andric // input files, some are sections in the produced output file and some exist
440b57cec5SDimitry Andric // just as a convenience for implementing special ways of combining some
450b57cec5SDimitry Andric // sections.
460b57cec5SDimitry Andric class SectionBase {
470b57cec5SDimitry Andric public:
4881ad6265SDimitry Andric   enum Kind { Regular, Synthetic, EHFrame, Merge, Output };
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   Kind kind() const { return (Kind)sectionKind; }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   StringRef name;
530b57cec5SDimitry Andric 
54e8d8bef9SDimitry Andric   uint8_t sectionKind : 3;
550b57cec5SDimitry Andric 
5685868e8aSDimitry Andric   // The next two bit fields are only used by InputSectionBase, but we
570b57cec5SDimitry Andric   // put them here so the struct packs better.
580b57cec5SDimitry Andric 
59e8d8bef9SDimitry Andric   uint8_t bss : 1;
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   // Set for sections that should not be folded by ICF.
62e8d8bef9SDimitry Andric   uint8_t keepUnique : 1;
630b57cec5SDimitry Andric 
640b57cec5SDimitry Andric   // The 1-indexed partition that this section is assigned to by the garbage
650b57cec5SDimitry Andric   // collector, or 0 if this section is dead. Normally there is only one
660b57cec5SDimitry Andric   // partition, so this will either be 0 or 1.
671fd87a68SDimitry Andric   uint8_t partition = 1;
680b57cec5SDimitry Andric   elf::Partition &getPartition() const;
690b57cec5SDimitry Andric 
700b57cec5SDimitry Andric   // These corresponds to the fields in Elf_Shdr.
710b57cec5SDimitry Andric   uint32_t alignment;
720b57cec5SDimitry Andric   uint64_t flags;
734824e7fdSDimitry Andric   uint32_t entsize;
740b57cec5SDimitry Andric   uint32_t type;
750b57cec5SDimitry Andric   uint32_t link;
760b57cec5SDimitry Andric   uint32_t info;
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   OutputSection *getOutputSection();
790b57cec5SDimitry Andric   const OutputSection *getOutputSection() const {
800b57cec5SDimitry Andric     return const_cast<SectionBase *>(this)->getOutputSection();
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   // Translate an offset in the input section to an offset in the output
840b57cec5SDimitry Andric   // section.
850b57cec5SDimitry Andric   uint64_t getOffset(uint64_t offset) const;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   uint64_t getVA(uint64_t offset = 0) const;
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric   bool isLive() const { return partition != 0; }
900b57cec5SDimitry Andric   void markLive() { partition = 1; }
910b57cec5SDimitry Andric   void markDead() { partition = 0; }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric protected:
944824e7fdSDimitry Andric   constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
954824e7fdSDimitry Andric                         uint32_t entsize, uint32_t alignment, uint32_t type,
960b57cec5SDimitry Andric                         uint32_t info, uint32_t link)
970eae32dcSDimitry Andric       : name(name), sectionKind(sectionKind), bss(false), keepUnique(false),
981fd87a68SDimitry Andric         alignment(alignment), flags(flags), entsize(entsize), type(type),
991fd87a68SDimitry Andric         link(link), info(info) {}
1000b57cec5SDimitry Andric };
1010b57cec5SDimitry Andric 
102*753f127fSDimitry Andric struct RISCVRelaxAux;
103*753f127fSDimitry Andric 
1040b57cec5SDimitry Andric // This corresponds to a section of an input file.
1050b57cec5SDimitry Andric class InputSectionBase : public SectionBase {
1060b57cec5SDimitry Andric public:
1070b57cec5SDimitry Andric   template <class ELFT>
1080b57cec5SDimitry Andric   InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
1090b57cec5SDimitry Andric                    StringRef name, Kind sectionKind);
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
1120b57cec5SDimitry Andric                    uint64_t entsize, uint32_t link, uint32_t info,
1130b57cec5SDimitry Andric                    uint32_t alignment, ArrayRef<uint8_t> data, StringRef name,
1140b57cec5SDimitry Andric                    Kind sectionKind);
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   static bool classof(const SectionBase *s) { return s->kind() != Output; }
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   // The file which contains this section. Its dynamic type is always
1190b57cec5SDimitry Andric   // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
1200b57cec5SDimitry Andric   // its static type.
1210b57cec5SDimitry Andric   InputFile *file;
1220b57cec5SDimitry Andric 
1231fd87a68SDimitry Andric   // Input sections are part of an output section. Special sections
1241fd87a68SDimitry Andric   // like .eh_frame and merge sections are first combined into a
1251fd87a68SDimitry Andric   // synthetic section that is then added to an output section. In all
1261fd87a68SDimitry Andric   // cases this points one level up.
1271fd87a68SDimitry Andric   SectionBase *parent = nullptr;
1281fd87a68SDimitry Andric 
1294824e7fdSDimitry Andric   // Section index of the relocation section if exists.
1304824e7fdSDimitry Andric   uint32_t relSecIdx = 0;
1314824e7fdSDimitry Andric 
1320b57cec5SDimitry Andric   template <class ELFT> ObjFile<ELFT> *getFile() const {
1330b57cec5SDimitry Andric     return cast_or_null<ObjFile<ELFT>>(file);
1340b57cec5SDimitry Andric   }
1350b57cec5SDimitry Andric 
136*753f127fSDimitry Andric   // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
137*753f127fSDimitry Andric   // indicate the number of bytes which is not counted in the size. This should
138*753f127fSDimitry Andric   // be reset to zero after uses.
139*753f127fSDimitry Andric   uint16_t bytesDropped = 0;
1405ffd83dbSDimitry Andric 
141e8d8bef9SDimitry Andric   // Whether the section needs to be padded with a NOP filler due to
142e8d8bef9SDimitry Andric   // deleteFallThruJmpInsn.
143e8d8bef9SDimitry Andric   bool nopFiller = false;
144e8d8bef9SDimitry Andric 
14504eeddc0SDimitry Andric   void drop_back(unsigned num) {
14604eeddc0SDimitry Andric     assert(bytesDropped + num < 256);
14704eeddc0SDimitry Andric     bytesDropped += num;
14804eeddc0SDimitry Andric   }
1495ffd83dbSDimitry Andric 
1505ffd83dbSDimitry Andric   void push_back(uint64_t num) {
1515ffd83dbSDimitry Andric     assert(bytesDropped >= num);
1525ffd83dbSDimitry Andric     bytesDropped -= num;
1535ffd83dbSDimitry Andric   }
1545ffd83dbSDimitry Andric 
15581ad6265SDimitry Andric   mutable ArrayRef<uint8_t> rawData;
15681ad6265SDimitry Andric 
1575ffd83dbSDimitry Andric   void trim() {
1585ffd83dbSDimitry Andric     if (bytesDropped) {
1595ffd83dbSDimitry Andric       rawData = rawData.drop_back(bytesDropped);
1605ffd83dbSDimitry Andric       bytesDropped = 0;
1615ffd83dbSDimitry Andric     }
1625ffd83dbSDimitry Andric   }
1635ffd83dbSDimitry Andric 
1640b57cec5SDimitry Andric   ArrayRef<uint8_t> data() const {
1650b57cec5SDimitry Andric     if (uncompressedSize >= 0)
1660b57cec5SDimitry Andric       uncompress();
1670b57cec5SDimitry Andric     return rawData;
1680b57cec5SDimitry Andric   }
1690b57cec5SDimitry Andric 
170480093f4SDimitry Andric   // The next member in the section group if this section is in a group. This is
171480093f4SDimitry Andric   // used by --gc-sections.
172480093f4SDimitry Andric   InputSectionBase *nextInSectionGroup = nullptr;
173480093f4SDimitry Andric 
174349cc55cSDimitry Andric   template <class ELFT> RelsOrRelas<ELFT> relsOrRelas() const;
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric   // InputSections that are dependent on us (reverse dependency for GC)
1770b57cec5SDimitry Andric   llvm::TinyPtrVector<InputSection *> dependentSections;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric   // Returns the size of this section (even if this is a common or BSS.)
1800b57cec5SDimitry Andric   size_t getSize() const;
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   InputSection *getLinkOrderDep() const;
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric   // Get the function symbol that encloses this offset from within the
1850b57cec5SDimitry Andric   // section.
1860b57cec5SDimitry Andric   Defined *getEnclosingFunction(uint64_t offset);
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   // Returns a source location string. Used to construct an error message.
18904eeddc0SDimitry Andric   std::string getLocation(uint64_t offset);
1900b57cec5SDimitry Andric   std::string getSrcMsg(const Symbol &sym, uint64_t offset);
1910b57cec5SDimitry Andric   std::string getObjMsg(uint64_t offset);
1920b57cec5SDimitry Andric 
1930b57cec5SDimitry Andric   // Each section knows how to relocate itself. These functions apply
1940b57cec5SDimitry Andric   // relocations, assuming that Buf points to this section's copy in
1950b57cec5SDimitry Andric   // the mmap'ed output buffer.
1960b57cec5SDimitry Andric   template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
1970b57cec5SDimitry Andric   void relocateAlloc(uint8_t *buf, uint8_t *bufEnd);
1985ffd83dbSDimitry Andric   static uint64_t getRelocTargetVA(const InputFile *File, RelType Type,
1995ffd83dbSDimitry Andric                                    int64_t A, uint64_t P, const Symbol &Sym,
2005ffd83dbSDimitry Andric                                    RelExpr Expr);
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric   // The native ELF reloc data type is not very convenient to handle.
2030b57cec5SDimitry Andric   // So we convert ELF reloc records to our own records in Relocations.cpp.
2040b57cec5SDimitry Andric   // This vector contains such "cooked" relocations.
205e8d8bef9SDimitry Andric   SmallVector<Relocation, 0> relocations;
2065ffd83dbSDimitry Andric 
207*753f127fSDimitry Andric   union {
2085ffd83dbSDimitry Andric     // These are modifiers to jump instructions that are necessary when basic
209*753f127fSDimitry Andric     // block sections are enabled.  Basic block sections creates opportunities
210*753f127fSDimitry Andric     // to relax jump instructions at basic block boundaries after reordering the
2115ffd83dbSDimitry Andric     // basic blocks.
21204eeddc0SDimitry Andric     JumpInstrMod *jumpInstrMod = nullptr;
2135ffd83dbSDimitry Andric 
214*753f127fSDimitry Andric     // Auxiliary information for RISC-V linker relaxation. RISC-V does not use
215*753f127fSDimitry Andric     // jumpInstrMod.
216*753f127fSDimitry Andric     RISCVRelaxAux *relaxAux;
217*753f127fSDimitry Andric   };
218*753f127fSDimitry Andric 
2190b57cec5SDimitry Andric   // A function compiled with -fsplit-stack calling a function
2200b57cec5SDimitry Andric   // compiled without -fsplit-stack needs its prologue adjusted. Find
2210b57cec5SDimitry Andric   // such functions and adjust their prologues.  This is very similar
2220b57cec5SDimitry Andric   // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
2230b57cec5SDimitry Andric   // information.
2240b57cec5SDimitry Andric   template <typename ELFT>
2250b57cec5SDimitry Andric   void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
2260b57cec5SDimitry Andric 
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric   template <typename T> llvm::ArrayRef<T> getDataAs() const {
22981ad6265SDimitry Andric     size_t s = rawData.size();
2300b57cec5SDimitry Andric     assert(s % sizeof(T) == 0);
23181ad6265SDimitry Andric     return llvm::makeArrayRef<T>((const T *)rawData.data(), s / sizeof(T));
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric protected:
235d69d0756SDimitry Andric   template <typename ELFT>
2360b57cec5SDimitry Andric   void parseCompressedHeader();
2370b57cec5SDimitry Andric   void uncompress() const;
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   // This field stores the uncompressed size of the compressed data in rawData,
2400b57cec5SDimitry Andric   // or -1 if rawData is not compressed (either because the section wasn't
2410b57cec5SDimitry Andric   // compressed in the first place, or because we ended up uncompressing it).
2420b57cec5SDimitry Andric   // Since the feature is not used often, this is usually -1.
2430b57cec5SDimitry Andric   mutable int64_t uncompressedSize = -1;
2440b57cec5SDimitry Andric };
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric // SectionPiece represents a piece of splittable section contents.
2470b57cec5SDimitry Andric // We allocate a lot of these and binary search on them. This means that they
2480b57cec5SDimitry Andric // have to be as compact as possible, which is why we don't store the size (can
2490b57cec5SDimitry Andric // be found by looking at the next one).
2500b57cec5SDimitry Andric struct SectionPiece {
2511fd87a68SDimitry Andric   SectionPiece() = default;
2520b57cec5SDimitry Andric   SectionPiece(size_t off, uint32_t hash, bool live)
2530eae32dcSDimitry Andric       : inputOff(off), live(live), hash(hash >> 1) {}
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric   uint32_t inputOff;
2560b57cec5SDimitry Andric   uint32_t live : 1;
2570b57cec5SDimitry Andric   uint32_t hash : 31;
2580b57cec5SDimitry Andric   uint64_t outputOff = 0;
2590b57cec5SDimitry Andric };
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric // This corresponds to a SHF_MERGE section of an input file.
2640b57cec5SDimitry Andric class MergeInputSection : public InputSectionBase {
2650b57cec5SDimitry Andric public:
2660b57cec5SDimitry Andric   template <class ELFT>
2670b57cec5SDimitry Andric   MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
2680b57cec5SDimitry Andric                     StringRef name);
2690b57cec5SDimitry Andric   MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize,
2700b57cec5SDimitry Andric                     ArrayRef<uint8_t> data, StringRef name);
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   static bool classof(const SectionBase *s) { return s->kind() == Merge; }
2730b57cec5SDimitry Andric   void splitIntoPieces();
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   // Translate an offset in the input section to an offset in the parent
2760b57cec5SDimitry Andric   // MergeSyntheticSection.
2770b57cec5SDimitry Andric   uint64_t getParentOffset(uint64_t offset) const;
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   // Splittable sections are handled as a sequence of data
2800b57cec5SDimitry Andric   // rather than a single large blob of data.
2810eae32dcSDimitry Andric   SmallVector<SectionPiece, 0> pieces;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   // Returns I'th piece's data. This function is very hot when
2840b57cec5SDimitry Andric   // string merging is enabled, so we want to inline.
2850b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
2860b57cec5SDimitry Andric   llvm::CachedHashStringRef getData(size_t i) const {
2870b57cec5SDimitry Andric     size_t begin = pieces[i].inputOff;
2880b57cec5SDimitry Andric     size_t end =
28981ad6265SDimitry Andric         (pieces.size() - 1 == i) ? rawData.size() : pieces[i + 1].inputOff;
29081ad6265SDimitry Andric     return {toStringRef(rawData.slice(begin, end - begin)), pieces[i].hash};
2910b57cec5SDimitry Andric   }
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   // Returns the SectionPiece at a given input section offset.
29481ad6265SDimitry Andric   SectionPiece &getSectionPiece(uint64_t offset);
29581ad6265SDimitry Andric   const SectionPiece &getSectionPiece(uint64_t offset) const {
2960b57cec5SDimitry Andric     return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
2970b57cec5SDimitry Andric   }
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   SyntheticSection *getParent() const;
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric private:
3021fd87a68SDimitry Andric   void splitStrings(StringRef s, size_t size);
3030b57cec5SDimitry Andric   void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
3040b57cec5SDimitry Andric };
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric struct EhSectionPiece {
3070b57cec5SDimitry Andric   EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
3080b57cec5SDimitry Andric                  unsigned firstRelocation)
3090b57cec5SDimitry Andric       : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
3100b57cec5SDimitry Andric 
311e8d8bef9SDimitry Andric   ArrayRef<uint8_t> data() const {
31281ad6265SDimitry Andric     return {sec->rawData.data() + this->inputOff, size};
3130b57cec5SDimitry Andric   }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   size_t inputOff;
3160b57cec5SDimitry Andric   ssize_t outputOff = -1;
3170b57cec5SDimitry Andric   InputSectionBase *sec;
3180b57cec5SDimitry Andric   uint32_t size;
3190b57cec5SDimitry Andric   unsigned firstRelocation;
3200b57cec5SDimitry Andric };
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric // This corresponds to a .eh_frame section of an input file.
3230b57cec5SDimitry Andric class EhInputSection : public InputSectionBase {
3240b57cec5SDimitry Andric public:
3250b57cec5SDimitry Andric   template <class ELFT>
3260b57cec5SDimitry Andric   EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
3270b57cec5SDimitry Andric                  StringRef name);
3280b57cec5SDimitry Andric   static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
3290b57cec5SDimitry Andric   template <class ELFT> void split();
3300b57cec5SDimitry Andric   template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric   // Splittable sections are handled as a sequence of data
3330b57cec5SDimitry Andric   // rather than a single large blob of data.
33404eeddc0SDimitry Andric   SmallVector<EhSectionPiece, 0> pieces;
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   SyntheticSection *getParent() const;
33781ad6265SDimitry Andric   uint64_t getParentOffset(uint64_t offset) const;
3380b57cec5SDimitry Andric };
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric // This is a section that is added directly to an output section
3410b57cec5SDimitry Andric // instead of needing special combination via a synthetic section. This
3420b57cec5SDimitry Andric // includes all input sections with the exceptions of SHF_MERGE and
3430b57cec5SDimitry Andric // .eh_frame. It also includes the synthetic sections themselves.
3440b57cec5SDimitry Andric class InputSection : public InputSectionBase {
3450b57cec5SDimitry Andric public:
3460b57cec5SDimitry Andric   InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t alignment,
3470b57cec5SDimitry Andric                ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
3480b57cec5SDimitry Andric   template <class ELFT>
3490b57cec5SDimitry Andric   InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
3500b57cec5SDimitry Andric                StringRef name);
3510b57cec5SDimitry Andric 
35281ad6265SDimitry Andric   static bool classof(const SectionBase *s) {
35381ad6265SDimitry Andric     return s->kind() == SectionBase::Regular ||
35481ad6265SDimitry Andric            s->kind() == SectionBase::Synthetic;
35581ad6265SDimitry Andric   }
35681ad6265SDimitry Andric 
3570b57cec5SDimitry Andric   // Write this section to a mmap'ed file, assuming Buf is pointing to
3580b57cec5SDimitry Andric   // beginning of the output section.
3590b57cec5SDimitry Andric   template <class ELFT> void writeTo(uint8_t *buf);
3600b57cec5SDimitry Andric 
36181ad6265SDimitry Andric   OutputSection *getParent() const {
36281ad6265SDimitry Andric     return reinterpret_cast<OutputSection *>(parent);
36381ad6265SDimitry Andric   }
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   // This variable has two usages. Initially, it represents an index in the
3660b57cec5SDimitry Andric   // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
3670b57cec5SDimitry Andric   // sections. After assignAddresses is called, it represents the offset from
3680b57cec5SDimitry Andric   // the beginning of the output section this section was assigned to.
3690b57cec5SDimitry Andric   uint64_t outSecOff = 0;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   InputSectionBase *getRelocatedSection() const;
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   template <class ELFT, class RelTy>
3740b57cec5SDimitry Andric   void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
3750b57cec5SDimitry Andric 
3760eae32dcSDimitry Andric   // Points to the canonical section. If ICF folds two sections, repl pointer of
3770eae32dcSDimitry Andric   // one section points to the other.
3780eae32dcSDimitry Andric   InputSection *repl = this;
3790eae32dcSDimitry Andric 
3800b57cec5SDimitry Andric   // Used by ICF.
3810b57cec5SDimitry Andric   uint32_t eqClass[2] = {0, 0};
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   // Called by ICF to merge two input sections.
3840b57cec5SDimitry Andric   void replace(InputSection *other);
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   static InputSection discarded;
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric private:
3890b57cec5SDimitry Andric   template <class ELFT, class RelTy>
3900b57cec5SDimitry Andric   void copyRelocations(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   template <class ELFT> void copyShtGroup(uint8_t *buf);
3930b57cec5SDimitry Andric };
3940b57cec5SDimitry Andric 
39504eeddc0SDimitry Andric static_assert(sizeof(InputSection) <= 160, "InputSection is too big");
396e8d8bef9SDimitry Andric 
397d65cd7a5SDimitry Andric inline bool isDebugSection(const InputSectionBase &sec) {
3982418469bSDimitry Andric   return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
39981ad6265SDimitry Andric          sec.name.startswith(".debug");
400d65cd7a5SDimitry Andric }
401d65cd7a5SDimitry Andric 
4020b57cec5SDimitry Andric // The list of all input sections.
4030eae32dcSDimitry Andric extern SmallVector<InputSectionBase *, 0> inputSections;
4040b57cec5SDimitry Andric 
4055ffd83dbSDimitry Andric // The set of TOC entries (.toc + addend) for which we should not apply
4065ffd83dbSDimitry Andric // toc-indirect to toc-relative relaxation. const Symbol * refers to the
4075ffd83dbSDimitry Andric // STT_SECTION symbol associated to the .toc input section.
4085ffd83dbSDimitry Andric extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax;
4095ffd83dbSDimitry Andric 
4100b57cec5SDimitry Andric } // namespace elf
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric std::string toString(const elf::InputSectionBase *);
4130b57cec5SDimitry Andric } // namespace lld
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric #endif
416