1 //===- OutputSections.h -----------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_OUTPUT_SECTIONS_H
11 #define LLD_ELF_OUTPUT_SECTIONS_H
14 #include "InputSection.h"
15 #include "LinkerScript.h"
16 #include "Relocations.h"
18 #include "lld/Common/LLVM.h"
19 #include "llvm/MC/StringTableBuilder.h"
20 #include "llvm/Object/ELF.h"
27 struct EhSectionPiece;
30 class InputSectionBase;
31 class MergeInputSection;
33 template <class ELFT> class ObjFile;
34 template <class ELFT> class SharedFile;
38 // This represents a section in an output file.
39 // It is composed of multiple InputSections.
40 // The writer creates multiple OutputSections and assign them unique,
41 // non-overlapping file offsets and VAs.
42 class OutputSection final : public BaseCommand, public SectionBase {
44 OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
46 static bool classof(const SectionBase *S) {
47 return S->kind() == SectionBase::Output;
50 static bool classof(const BaseCommand *C);
52 uint64_t getLMA() const { return PtLoad ? Addr + PtLoad->LMAOffset : Addr; }
53 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
55 unsigned SectionIndex;
58 uint32_t getPhdrFlags() const;
60 // Pointer to the PT_LOAD segment, which this section resides in. This field
61 // is used to correctly compute file offset of a section. When two sections
62 // share the same load segment, difference between their file offsets should
63 // be equal to difference between their virtual addresses. To compute some
64 // section offset we use the following formula: Off = Off_first + VA -
65 // VA_first, where Off_first and VA_first is file offset and VA of first
66 // section in PT_LOAD.
67 PhdrEntry *PtLoad = nullptr;
69 // Pointer to a relocation section for this section. Usually nullptr because
70 // we consume relocations, but if --emit-relocs is specified (which is rare),
71 // it may have a non-null value.
72 OutputSection *RelocationSection = nullptr;
74 // Initially this field is the number of InputSections that have been added to
75 // the OutputSection so far. Later on, after a call to assignAddresses, it
76 // corresponds to the Elf_Shdr member.
79 // The following fields correspond to Elf_Shdr members.
84 void addSection(InputSection *IS);
86 // Location in the output buffer.
87 uint8_t *Loc = nullptr;
89 // The following members are normally only used in linker scripts.
90 MemoryRegion *MemRegion = nullptr;
91 MemoryRegion *LMARegion = nullptr;
96 std::vector<BaseCommand *> SectionCommands;
97 std::vector<StringRef> Phdrs;
98 llvm::Optional<uint32_t> Filler;
99 ConstraintKind Constraint = ConstraintKind::NoConstraint;
100 std::string Location;
101 std::string MemoryRegionName;
102 std::string LMARegionName;
105 template <class ELFT> void finalize();
106 template <class ELFT> void writeTo(uint8_t *Buf);
107 template <class ELFT> void maybeCompress();
109 void sort(std::function<int(InputSectionBase *S)> Order);
111 void sortCtorsDtors();
114 // Used for implementation of --compress-debug-sections option.
115 std::vector<uint8_t> ZDebugHeader;
116 llvm::SmallVector<char, 1> CompressedData;
118 uint32_t getFiller();
121 int getPriority(StringRef S);
123 // All output sections that are handled by the linker specially are
124 // globally accessible. Writer initializes them, so don't use them
125 // until Writer is initialized.
127 static uint8_t First;
128 static OutputSection *Opd;
129 static uint8_t *OpdBuf;
130 static PhdrEntry *TlsPhdr;
131 static OutputSection *DebugInfo;
132 static OutputSection *ElfHeader;
133 static OutputSection *ProgramHeaders;
134 static OutputSection *PreinitArray;
135 static OutputSection *InitArray;
136 static OutputSection *FiniArray;
145 uint64_t getHeaderSize();
146 void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
147 std::function<int(InputSectionBase *S)> Order);
149 extern std::vector<OutputSection *> OutputSections;