xref: /freebsd/contrib/llvm-project/lld/ELF/InputSection.h (revision 52418fc2be8efa5172b90a3a9e617017173612c4)
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 
38*52418fc2SDimitry Andric // Returned by InputSectionBase::relsOrRelas. At most one member is empty.
39349cc55cSDimitry Andric template <class ELFT> struct RelsOrRelas {
40*52418fc2SDimitry Andric   Relocs<typename ELFT::Rel> rels;
41*52418fc2SDimitry Andric   Relocs<typename ELFT::Rela> relas;
42*52418fc2SDimitry Andric   Relocs<typename ELFT::Crel> crels;
43349cc55cSDimitry Andric   bool areRelocsRel() const { return rels.size(); }
44*52418fc2SDimitry Andric   bool areRelocsCrel() const { return crels.size(); }
45349cc55cSDimitry Andric };
46349cc55cSDimitry Andric 
47*52418fc2SDimitry Andric #define invokeOnRelocs(sec, f, ...)                                            \
48*52418fc2SDimitry Andric   {                                                                            \
49*52418fc2SDimitry Andric     const RelsOrRelas<ELFT> rs = (sec).template relsOrRelas<ELFT>();           \
50*52418fc2SDimitry Andric     if (rs.areRelocsCrel())                                                    \
51*52418fc2SDimitry Andric       f(__VA_ARGS__, rs.crels);                                                \
52*52418fc2SDimitry Andric     else if (rs.areRelocsRel())                                                \
53*52418fc2SDimitry Andric       f(__VA_ARGS__, rs.rels);                                                 \
54*52418fc2SDimitry Andric     else                                                                       \
55*52418fc2SDimitry Andric       f(__VA_ARGS__, rs.relas);                                                \
56*52418fc2SDimitry Andric   }
57*52418fc2SDimitry Andric 
580b57cec5SDimitry Andric // This is the base class of all sections that lld handles. Some are sections in
590b57cec5SDimitry Andric // input files, some are sections in the produced output file and some exist
600b57cec5SDimitry Andric // just as a convenience for implementing special ways of combining some
610b57cec5SDimitry Andric // sections.
620b57cec5SDimitry Andric class SectionBase {
630b57cec5SDimitry Andric public:
640fca6ea1SDimitry Andric   enum Kind { Regular, Synthetic, Spill, EHFrame, Merge, Output };
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric   Kind kind() const { return (Kind)sectionKind; }
670b57cec5SDimitry Andric 
680fca6ea1SDimitry Andric   LLVM_PREFERRED_TYPE(Kind)
69e8d8bef9SDimitry Andric   uint8_t sectionKind : 3;
700b57cec5SDimitry Andric 
7185868e8aSDimitry Andric   // The next two bit fields are only used by InputSectionBase, but we
720b57cec5SDimitry Andric   // put them here so the struct packs better.
730b57cec5SDimitry Andric 
740fca6ea1SDimitry Andric   LLVM_PREFERRED_TYPE(bool)
75e8d8bef9SDimitry Andric   uint8_t bss : 1;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   // Set for sections that should not be folded by ICF.
780fca6ea1SDimitry Andric   LLVM_PREFERRED_TYPE(bool)
79e8d8bef9SDimitry Andric   uint8_t keepUnique : 1;
800b57cec5SDimitry Andric 
81bdd1243dSDimitry Andric   uint8_t partition = 1;
82bdd1243dSDimitry Andric   uint32_t type;
83bdd1243dSDimitry Andric   StringRef name;
84bdd1243dSDimitry Andric 
850b57cec5SDimitry Andric   // The 1-indexed partition that this section is assigned to by the garbage
860b57cec5SDimitry Andric   // collector, or 0 if this section is dead. Normally there is only one
870b57cec5SDimitry Andric   // partition, so this will either be 0 or 1.
880b57cec5SDimitry Andric   elf::Partition &getPartition() const;
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   // These corresponds to the fields in Elf_Shdr.
910b57cec5SDimitry Andric   uint64_t flags;
92bdd1243dSDimitry Andric   uint32_t addralign;
934824e7fdSDimitry Andric   uint32_t entsize;
940b57cec5SDimitry Andric   uint32_t link;
950b57cec5SDimitry Andric   uint32_t info;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   OutputSection *getOutputSection();
980b57cec5SDimitry Andric   const OutputSection *getOutputSection() const {
990b57cec5SDimitry Andric     return const_cast<SectionBase *>(this)->getOutputSection();
1000b57cec5SDimitry Andric   }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   // Translate an offset in the input section to an offset in the output
1030b57cec5SDimitry Andric   // section.
1040b57cec5SDimitry Andric   uint64_t getOffset(uint64_t offset) const;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric   uint64_t getVA(uint64_t offset = 0) const;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   bool isLive() const { return partition != 0; }
1090b57cec5SDimitry Andric   void markLive() { partition = 1; }
1100b57cec5SDimitry Andric   void markDead() { partition = 0; }
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric protected:
1134824e7fdSDimitry Andric   constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
114bdd1243dSDimitry Andric                         uint32_t entsize, uint32_t addralign, uint32_t type,
1150b57cec5SDimitry Andric                         uint32_t info, uint32_t link)
116bdd1243dSDimitry Andric       : sectionKind(sectionKind), bss(false), keepUnique(false), type(type),
117bdd1243dSDimitry Andric         name(name), flags(flags), addralign(addralign), entsize(entsize),
1181fd87a68SDimitry Andric         link(link), info(info) {}
1190b57cec5SDimitry Andric };
1200b57cec5SDimitry Andric 
12174626c16SDimitry Andric struct SymbolAnchor {
12274626c16SDimitry Andric   uint64_t offset;
12374626c16SDimitry Andric   Defined *d;
12474626c16SDimitry Andric   bool end; // true for the anchor of st_value+st_size
12574626c16SDimitry Andric };
12674626c16SDimitry Andric 
12774626c16SDimitry Andric struct RelaxAux {
12874626c16SDimitry Andric   // This records symbol start and end offsets which will be adjusted according
12974626c16SDimitry Andric   // to the nearest relocDeltas element.
13074626c16SDimitry Andric   SmallVector<SymbolAnchor, 0> anchors;
13174626c16SDimitry Andric   // For relocations[i], the actual offset is
13274626c16SDimitry Andric   //   r_offset - (i ? relocDeltas[i-1] : 0).
13374626c16SDimitry Andric   std::unique_ptr<uint32_t[]> relocDeltas;
13474626c16SDimitry Andric   // For relocations[i], the actual type is relocTypes[i].
13574626c16SDimitry Andric   std::unique_ptr<RelType[]> relocTypes;
13674626c16SDimitry Andric   SmallVector<uint32_t, 0> writes;
13774626c16SDimitry Andric };
138753f127fSDimitry Andric 
1390b57cec5SDimitry Andric // This corresponds to a section of an input file.
1400b57cec5SDimitry Andric class InputSectionBase : public SectionBase {
1410b57cec5SDimitry Andric public:
1420b57cec5SDimitry Andric   template <class ELFT>
1430b57cec5SDimitry Andric   InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
1440b57cec5SDimitry Andric                    StringRef name, Kind sectionKind);
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
1470b57cec5SDimitry Andric                    uint64_t entsize, uint32_t link, uint32_t info,
148bdd1243dSDimitry Andric                    uint32_t addralign, ArrayRef<uint8_t> data, StringRef name,
1490b57cec5SDimitry Andric                    Kind sectionKind);
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   static bool classof(const SectionBase *s) { return s->kind() != Output; }
1520b57cec5SDimitry Andric 
1530fca6ea1SDimitry Andric   // The file which contains this section. Its dynamic type is usually
1540fca6ea1SDimitry Andric   // ObjFile<ELFT>, but may be an InputFile of InternalKind (for a synthetic
1550fca6ea1SDimitry Andric   // section).
1560b57cec5SDimitry Andric   InputFile *file;
1570b57cec5SDimitry Andric 
1581fd87a68SDimitry Andric   // Input sections are part of an output section. Special sections
1591fd87a68SDimitry Andric   // like .eh_frame and merge sections are first combined into a
1601fd87a68SDimitry Andric   // synthetic section that is then added to an output section. In all
1611fd87a68SDimitry Andric   // cases this points one level up.
1621fd87a68SDimitry Andric   SectionBase *parent = nullptr;
1631fd87a68SDimitry Andric 
1644824e7fdSDimitry Andric   // Section index of the relocation section if exists.
1654824e7fdSDimitry Andric   uint32_t relSecIdx = 0;
1664824e7fdSDimitry Andric 
1670fca6ea1SDimitry Andric   // Getter when the dynamic type is ObjFile<ELFT>.
1680b57cec5SDimitry Andric   template <class ELFT> ObjFile<ELFT> *getFile() const {
1690fca6ea1SDimitry Andric     return cast<ObjFile<ELFT>>(file);
1700b57cec5SDimitry Andric   }
1710b57cec5SDimitry Andric 
172753f127fSDimitry Andric   // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
173753f127fSDimitry Andric   // indicate the number of bytes which is not counted in the size. This should
174753f127fSDimitry Andric   // be reset to zero after uses.
17506c3fb27SDimitry Andric   uint32_t bytesDropped = 0;
1765ffd83dbSDimitry Andric 
177bdd1243dSDimitry Andric   mutable bool compressed = false;
178bdd1243dSDimitry Andric 
179e8d8bef9SDimitry Andric   // Whether the section needs to be padded with a NOP filler due to
180e8d8bef9SDimitry Andric   // deleteFallThruJmpInsn.
181e8d8bef9SDimitry Andric   bool nopFiller = false;
182e8d8bef9SDimitry Andric 
18304eeddc0SDimitry Andric   void drop_back(unsigned num) {
18404eeddc0SDimitry Andric     assert(bytesDropped + num < 256);
18504eeddc0SDimitry Andric     bytesDropped += num;
18604eeddc0SDimitry Andric   }
1875ffd83dbSDimitry Andric 
1885ffd83dbSDimitry Andric   void push_back(uint64_t num) {
1895ffd83dbSDimitry Andric     assert(bytesDropped >= num);
1905ffd83dbSDimitry Andric     bytesDropped -= num;
1915ffd83dbSDimitry Andric   }
1925ffd83dbSDimitry Andric 
193bdd1243dSDimitry Andric   mutable const uint8_t *content_;
194bdd1243dSDimitry Andric   uint64_t size;
19581ad6265SDimitry Andric 
1965ffd83dbSDimitry Andric   void trim() {
1975ffd83dbSDimitry Andric     if (bytesDropped) {
198bdd1243dSDimitry Andric       size -= bytesDropped;
1995ffd83dbSDimitry Andric       bytesDropped = 0;
2005ffd83dbSDimitry Andric     }
2015ffd83dbSDimitry Andric   }
2025ffd83dbSDimitry Andric 
203bdd1243dSDimitry Andric   ArrayRef<uint8_t> content() const {
204bdd1243dSDimitry Andric     return ArrayRef<uint8_t>(content_, size);
205bdd1243dSDimitry Andric   }
206bdd1243dSDimitry Andric   ArrayRef<uint8_t> contentMaybeDecompress() const {
207bdd1243dSDimitry Andric     if (compressed)
208bdd1243dSDimitry Andric       decompress();
209bdd1243dSDimitry Andric     return content();
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric 
212480093f4SDimitry Andric   // The next member in the section group if this section is in a group. This is
213480093f4SDimitry Andric   // used by --gc-sections.
214480093f4SDimitry Andric   InputSectionBase *nextInSectionGroup = nullptr;
215480093f4SDimitry Andric 
216*52418fc2SDimitry Andric   template <class ELFT>
217*52418fc2SDimitry Andric   RelsOrRelas<ELFT> relsOrRelas(bool supportsCrel = true) const;
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric   // InputSections that are dependent on us (reverse dependency for GC)
2200b57cec5SDimitry Andric   llvm::TinyPtrVector<InputSection *> dependentSections;
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric   // Returns the size of this section (even if this is a common or BSS.)
2230b57cec5SDimitry Andric   size_t getSize() const;
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   InputSection *getLinkOrderDep() const;
2260b57cec5SDimitry Andric 
2275f757f3fSDimitry Andric   // Get a symbol that encloses this offset from within the section. If type is
2285f757f3fSDimitry Andric   // not zero, return a symbol with the specified type.
2295f757f3fSDimitry Andric   Defined *getEnclosingSymbol(uint64_t offset, uint8_t type = 0) const;
2305f757f3fSDimitry Andric   Defined *getEnclosingFunction(uint64_t offset) const {
2315f757f3fSDimitry Andric     return getEnclosingSymbol(offset, llvm::ELF::STT_FUNC);
2325f757f3fSDimitry Andric   }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   // Returns a source location string. Used to construct an error message.
2355f757f3fSDimitry Andric   std::string getLocation(uint64_t offset) const;
2365f757f3fSDimitry Andric   std::string getSrcMsg(const Symbol &sym, uint64_t offset) const;
2375f757f3fSDimitry Andric   std::string getObjMsg(uint64_t offset) const;
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric   // Each section knows how to relocate itself. These functions apply
2400b57cec5SDimitry Andric   // relocations, assuming that Buf points to this section's copy in
2410b57cec5SDimitry Andric   // the mmap'ed output buffer.
2420b57cec5SDimitry Andric   template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
2435ffd83dbSDimitry Andric   static uint64_t getRelocTargetVA(const InputFile *File, RelType Type,
2445ffd83dbSDimitry Andric                                    int64_t A, uint64_t P, const Symbol &Sym,
2455ffd83dbSDimitry Andric                                    RelExpr Expr);
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric   // The native ELF reloc data type is not very convenient to handle.
2480b57cec5SDimitry Andric   // So we convert ELF reloc records to our own records in Relocations.cpp.
2490b57cec5SDimitry Andric   // This vector contains such "cooked" relocations.
250e8d8bef9SDimitry Andric   SmallVector<Relocation, 0> relocations;
2515ffd83dbSDimitry Andric 
252bdd1243dSDimitry Andric   void addReloc(const Relocation &r) { relocations.push_back(r); }
253bdd1243dSDimitry Andric   MutableArrayRef<Relocation> relocs() { return relocations; }
254bdd1243dSDimitry Andric   ArrayRef<Relocation> relocs() const { return relocations; }
255bdd1243dSDimitry Andric 
256753f127fSDimitry Andric   union {
2575ffd83dbSDimitry Andric     // These are modifiers to jump instructions that are necessary when basic
258753f127fSDimitry Andric     // block sections are enabled.  Basic block sections creates opportunities
259753f127fSDimitry Andric     // to relax jump instructions at basic block boundaries after reordering the
2605ffd83dbSDimitry Andric     // basic blocks.
26104eeddc0SDimitry Andric     JumpInstrMod *jumpInstrMod = nullptr;
2625ffd83dbSDimitry Andric 
26374626c16SDimitry Andric     // Auxiliary information for RISC-V and LoongArch linker relaxation.
26474626c16SDimitry Andric     // They do not use jumpInstrMod.
26574626c16SDimitry Andric     RelaxAux *relaxAux;
266bdd1243dSDimitry Andric 
267bdd1243dSDimitry Andric     // The compressed content size when `compressed` is true.
268bdd1243dSDimitry Andric     size_t compressedSize;
269753f127fSDimitry Andric   };
270753f127fSDimitry Andric 
2710b57cec5SDimitry Andric   // A function compiled with -fsplit-stack calling a function
2720b57cec5SDimitry Andric   // compiled without -fsplit-stack needs its prologue adjusted. Find
2730b57cec5SDimitry Andric   // such functions and adjust their prologues.  This is very similar
2740b57cec5SDimitry Andric   // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
2750b57cec5SDimitry Andric   // information.
2760b57cec5SDimitry Andric   template <typename ELFT>
2770b57cec5SDimitry Andric   void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric 
2800b57cec5SDimitry Andric   template <typename T> llvm::ArrayRef<T> getDataAs() const {
281bdd1243dSDimitry Andric     size_t s = content().size();
2820b57cec5SDimitry Andric     assert(s % sizeof(T) == 0);
283bdd1243dSDimitry Andric     return llvm::ArrayRef<T>((const T *)content().data(), s / sizeof(T));
2840b57cec5SDimitry Andric   }
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric protected:
287d69d0756SDimitry Andric   template <typename ELFT>
2880b57cec5SDimitry Andric   void parseCompressedHeader();
289bdd1243dSDimitry Andric   void decompress() const;
2900b57cec5SDimitry Andric };
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric // SectionPiece represents a piece of splittable section contents.
2930b57cec5SDimitry Andric // We allocate a lot of these and binary search on them. This means that they
2940b57cec5SDimitry Andric // have to be as compact as possible, which is why we don't store the size (can
2950b57cec5SDimitry Andric // be found by looking at the next one).
2960b57cec5SDimitry Andric struct SectionPiece {
2971fd87a68SDimitry Andric   SectionPiece() = default;
2980b57cec5SDimitry Andric   SectionPiece(size_t off, uint32_t hash, bool live)
2990eae32dcSDimitry Andric       : inputOff(off), live(live), hash(hash >> 1) {}
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric   uint32_t inputOff;
3020fca6ea1SDimitry Andric   LLVM_PREFERRED_TYPE(bool)
3030b57cec5SDimitry Andric   uint32_t live : 1;
3040b57cec5SDimitry Andric   uint32_t hash : 31;
3050b57cec5SDimitry Andric   uint64_t outputOff = 0;
3060b57cec5SDimitry Andric };
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric // This corresponds to a SHF_MERGE section of an input file.
3110b57cec5SDimitry Andric class MergeInputSection : public InputSectionBase {
3120b57cec5SDimitry Andric public:
3130b57cec5SDimitry Andric   template <class ELFT>
3140b57cec5SDimitry Andric   MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
3150b57cec5SDimitry Andric                     StringRef name);
3160b57cec5SDimitry Andric   MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize,
3170b57cec5SDimitry Andric                     ArrayRef<uint8_t> data, StringRef name);
3180b57cec5SDimitry Andric 
3190b57cec5SDimitry Andric   static bool classof(const SectionBase *s) { return s->kind() == Merge; }
3200b57cec5SDimitry Andric   void splitIntoPieces();
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   // Translate an offset in the input section to an offset in the parent
3230b57cec5SDimitry Andric   // MergeSyntheticSection.
3240b57cec5SDimitry Andric   uint64_t getParentOffset(uint64_t offset) const;
3250b57cec5SDimitry Andric 
3260b57cec5SDimitry Andric   // Splittable sections are handled as a sequence of data
3270b57cec5SDimitry Andric   // rather than a single large blob of data.
3280eae32dcSDimitry Andric   SmallVector<SectionPiece, 0> pieces;
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric   // Returns I'th piece's data. This function is very hot when
3310b57cec5SDimitry Andric   // string merging is enabled, so we want to inline.
3320b57cec5SDimitry Andric   LLVM_ATTRIBUTE_ALWAYS_INLINE
3330b57cec5SDimitry Andric   llvm::CachedHashStringRef getData(size_t i) const {
3340b57cec5SDimitry Andric     size_t begin = pieces[i].inputOff;
3350b57cec5SDimitry Andric     size_t end =
336bdd1243dSDimitry Andric         (pieces.size() - 1 == i) ? content().size() : pieces[i + 1].inputOff;
337bdd1243dSDimitry Andric     return {toStringRef(content().slice(begin, end - begin)), pieces[i].hash};
3380b57cec5SDimitry Andric   }
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   // Returns the SectionPiece at a given input section offset.
34181ad6265SDimitry Andric   SectionPiece &getSectionPiece(uint64_t offset);
34281ad6265SDimitry Andric   const SectionPiece &getSectionPiece(uint64_t offset) const {
3430b57cec5SDimitry Andric     return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
3440b57cec5SDimitry Andric   }
3450b57cec5SDimitry Andric 
346bdd1243dSDimitry Andric   SyntheticSection *getParent() const {
347bdd1243dSDimitry Andric     return cast_or_null<SyntheticSection>(parent);
348bdd1243dSDimitry Andric   }
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric private:
3511fd87a68SDimitry Andric   void splitStrings(StringRef s, size_t size);
3520b57cec5SDimitry Andric   void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
3530b57cec5SDimitry Andric };
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric struct EhSectionPiece {
3560b57cec5SDimitry Andric   EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
3570b57cec5SDimitry Andric                  unsigned firstRelocation)
3580b57cec5SDimitry Andric       : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
3590b57cec5SDimitry Andric 
360e8d8bef9SDimitry Andric   ArrayRef<uint8_t> data() const {
361bdd1243dSDimitry Andric     return {sec->content().data() + this->inputOff, size};
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric 
3640b57cec5SDimitry Andric   size_t inputOff;
3650b57cec5SDimitry Andric   ssize_t outputOff = -1;
3660b57cec5SDimitry Andric   InputSectionBase *sec;
3670b57cec5SDimitry Andric   uint32_t size;
3680b57cec5SDimitry Andric   unsigned firstRelocation;
3690b57cec5SDimitry Andric };
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric // This corresponds to a .eh_frame section of an input file.
3720b57cec5SDimitry Andric class EhInputSection : public InputSectionBase {
3730b57cec5SDimitry Andric public:
3740b57cec5SDimitry Andric   template <class ELFT>
3750b57cec5SDimitry Andric   EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
3760b57cec5SDimitry Andric                  StringRef name);
3770b57cec5SDimitry Andric   static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
3780b57cec5SDimitry Andric   template <class ELFT> void split();
3790b57cec5SDimitry Andric   template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   // Splittable sections are handled as a sequence of data
3820b57cec5SDimitry Andric   // rather than a single large blob of data.
383bdd1243dSDimitry Andric   SmallVector<EhSectionPiece, 0> cies, fdes;
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric   SyntheticSection *getParent() const;
38681ad6265SDimitry Andric   uint64_t getParentOffset(uint64_t offset) const;
3870b57cec5SDimitry Andric };
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric // This is a section that is added directly to an output section
3900b57cec5SDimitry Andric // instead of needing special combination via a synthetic section. This
3910b57cec5SDimitry Andric // includes all input sections with the exceptions of SHF_MERGE and
3920b57cec5SDimitry Andric // .eh_frame. It also includes the synthetic sections themselves.
3930b57cec5SDimitry Andric class InputSection : public InputSectionBase {
3940b57cec5SDimitry Andric public:
395bdd1243dSDimitry Andric   InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t addralign,
3960b57cec5SDimitry Andric                ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
3970b57cec5SDimitry Andric   template <class ELFT>
3980b57cec5SDimitry Andric   InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
3990b57cec5SDimitry Andric                StringRef name);
4000b57cec5SDimitry Andric 
40181ad6265SDimitry Andric   static bool classof(const SectionBase *s) {
40281ad6265SDimitry Andric     return s->kind() == SectionBase::Regular ||
4030fca6ea1SDimitry Andric            s->kind() == SectionBase::Synthetic ||
4040fca6ea1SDimitry Andric            s->kind() == SectionBase::Spill;
40581ad6265SDimitry Andric   }
40681ad6265SDimitry Andric 
4070b57cec5SDimitry Andric   // Write this section to a mmap'ed file, assuming Buf is pointing to
4080b57cec5SDimitry Andric   // beginning of the output section.
4090b57cec5SDimitry Andric   template <class ELFT> void writeTo(uint8_t *buf);
4100b57cec5SDimitry Andric 
41181ad6265SDimitry Andric   OutputSection *getParent() const {
41281ad6265SDimitry Andric     return reinterpret_cast<OutputSection *>(parent);
41381ad6265SDimitry Andric   }
4140b57cec5SDimitry Andric 
4150b57cec5SDimitry Andric   // This variable has two usages. Initially, it represents an index in the
4160b57cec5SDimitry Andric   // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
4170b57cec5SDimitry Andric   // sections. After assignAddresses is called, it represents the offset from
4180b57cec5SDimitry Andric   // the beginning of the output section this section was assigned to.
4190b57cec5SDimitry Andric   uint64_t outSecOff = 0;
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   InputSectionBase *getRelocatedSection() const;
4220b57cec5SDimitry Andric 
4230b57cec5SDimitry Andric   template <class ELFT, class RelTy>
424*52418fc2SDimitry Andric   void relocateNonAlloc(uint8_t *buf, Relocs<RelTy> rels);
4250b57cec5SDimitry Andric 
4260eae32dcSDimitry Andric   // Points to the canonical section. If ICF folds two sections, repl pointer of
4270eae32dcSDimitry Andric   // one section points to the other.
4280eae32dcSDimitry Andric   InputSection *repl = this;
4290eae32dcSDimitry Andric 
4300b57cec5SDimitry Andric   // Used by ICF.
4310b57cec5SDimitry Andric   uint32_t eqClass[2] = {0, 0};
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric   // Called by ICF to merge two input sections.
4340b57cec5SDimitry Andric   void replace(InputSection *other);
4350b57cec5SDimitry Andric 
4360b57cec5SDimitry Andric   static InputSection discarded;
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric private:
4395f757f3fSDimitry Andric   template <class ELFT, class RelTy> void copyRelocations(uint8_t *buf);
4405f757f3fSDimitry Andric 
4415f757f3fSDimitry Andric   template <class ELFT, class RelTy, class RelIt>
4425f757f3fSDimitry Andric   void copyRelocations(uint8_t *buf, llvm::iterator_range<RelIt> rels);
4430b57cec5SDimitry Andric 
4440b57cec5SDimitry Andric   template <class ELFT> void copyShtGroup(uint8_t *buf);
4450b57cec5SDimitry Andric };
4460b57cec5SDimitry Andric 
4470fca6ea1SDimitry Andric // A marker for a potential spill location for another input section. This
4480fca6ea1SDimitry Andric // broadly acts as if it were the original section until address assignment.
4490fca6ea1SDimitry Andric // Then it is either replaced with the real input section or removed.
4500fca6ea1SDimitry Andric class PotentialSpillSection : public InputSection {
4510fca6ea1SDimitry Andric public:
4520fca6ea1SDimitry Andric   // The containing input section description; used to quickly replace this stub
4530fca6ea1SDimitry Andric   // with the actual section.
4540fca6ea1SDimitry Andric   InputSectionDescription *isd;
4550fca6ea1SDimitry Andric 
4560fca6ea1SDimitry Andric   // Next potential spill location for the same source input section.
4570fca6ea1SDimitry Andric   PotentialSpillSection *next = nullptr;
4580fca6ea1SDimitry Andric 
4590fca6ea1SDimitry Andric   PotentialSpillSection(const InputSectionBase &source,
4600fca6ea1SDimitry Andric                         InputSectionDescription &isd);
4610fca6ea1SDimitry Andric 
4620fca6ea1SDimitry Andric   static bool classof(const SectionBase *sec) {
4630fca6ea1SDimitry Andric     return sec->kind() == InputSectionBase::Spill;
4640fca6ea1SDimitry Andric   }
4650fca6ea1SDimitry Andric };
4660fca6ea1SDimitry Andric 
46706c3fb27SDimitry Andric static_assert(sizeof(InputSection) <= 160, "InputSection is too big");
468bdd1243dSDimitry Andric 
469bdd1243dSDimitry Andric class SyntheticSection : public InputSection {
470bdd1243dSDimitry Andric public:
471bdd1243dSDimitry Andric   SyntheticSection(uint64_t flags, uint32_t type, uint32_t addralign,
472bdd1243dSDimitry Andric                    StringRef name)
4737a6dacacSDimitry Andric       : InputSection(ctx.internalFile, flags, type, addralign, {}, name,
474bdd1243dSDimitry Andric                      InputSectionBase::Synthetic) {}
475bdd1243dSDimitry Andric 
476bdd1243dSDimitry Andric   virtual ~SyntheticSection() = default;
477bdd1243dSDimitry Andric   virtual size_t getSize() const = 0;
478bdd1243dSDimitry Andric   virtual bool updateAllocSize() { return false; }
479bdd1243dSDimitry Andric   // If the section has the SHF_ALLOC flag and the size may be changed if
480bdd1243dSDimitry Andric   // thunks are added, update the section size.
481bdd1243dSDimitry Andric   virtual bool isNeeded() const { return true; }
482bdd1243dSDimitry Andric   virtual void finalizeContents() {}
483bdd1243dSDimitry Andric   virtual void writeTo(uint8_t *buf) = 0;
484bdd1243dSDimitry Andric 
485bdd1243dSDimitry Andric   static bool classof(const SectionBase *sec) {
486bdd1243dSDimitry Andric     return sec->kind() == InputSectionBase::Synthetic;
487bdd1243dSDimitry Andric   }
488bdd1243dSDimitry Andric };
489e8d8bef9SDimitry Andric 
4900fca6ea1SDimitry Andric inline bool isStaticRelSecType(uint32_t type) {
491*52418fc2SDimitry Andric   return type == llvm::ELF::SHT_RELA || type == llvm::ELF::SHT_CREL ||
492*52418fc2SDimitry Andric          type == llvm::ELF::SHT_REL;
4930fca6ea1SDimitry Andric }
4940fca6ea1SDimitry Andric 
495d65cd7a5SDimitry Andric inline bool isDebugSection(const InputSectionBase &sec) {
4962418469bSDimitry Andric   return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
49706c3fb27SDimitry Andric          sec.name.starts_with(".debug");
498d65cd7a5SDimitry Andric }
499d65cd7a5SDimitry Andric 
5005ffd83dbSDimitry Andric // The set of TOC entries (.toc + addend) for which we should not apply
5015ffd83dbSDimitry Andric // toc-indirect to toc-relative relaxation. const Symbol * refers to the
5025ffd83dbSDimitry Andric // STT_SECTION symbol associated to the .toc input section.
5035ffd83dbSDimitry Andric extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax;
5045ffd83dbSDimitry Andric 
5050b57cec5SDimitry Andric } // namespace elf
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric std::string toString(const elf::InputSectionBase *);
5080b57cec5SDimitry Andric } // namespace lld
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric #endif
511