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"
17 #include "lld/Common/LLVM.h"
18 #include "llvm/MC/StringTableBuilder.h"
19 #include "llvm/Object/ELF.h"
26 struct EhSectionPiece;
29 class InputSectionBase;
30 class MergeInputSection;
32 template <class ELFT> class ObjFile;
33 template <class ELFT> class SharedFile;
37 // This represents a section in an output file.
38 // It is composed of multiple InputSections.
39 // The writer creates multiple OutputSections and assign them unique,
40 // non-overlapping file offsets and VAs.
41 class OutputSection final : public BaseCommand, public SectionBase {
43 OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
45 static bool classof(const SectionBase *S) {
46 return S->kind() == SectionBase::Output;
49 static bool classof(const BaseCommand *C);
51 uint64_t getLMA() const { return PtLoad ? Addr + PtLoad->LMAOffset : Addr; }
52 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
54 uint32_t SectionIndex = UINT32_MAX;
57 uint32_t getPhdrFlags() const;
59 // Pointer to the PT_LOAD segment, which this section resides in. This field
60 // is used to correctly compute file offset of a section. When two sections
61 // share the same load segment, difference between their file offsets should
62 // be equal to difference between their virtual addresses. To compute some
63 // section offset we use the following formula: Off = Off_first + VA -
64 // VA_first, where Off_first and VA_first is file offset and VA of first
65 // section in PT_LOAD.
66 PhdrEntry *PtLoad = nullptr;
68 // Pointer to a relocation section for this section. Usually nullptr because
69 // we consume relocations, but if --emit-relocs is specified (which is rare),
70 // it may have a non-null value.
71 OutputSection *RelocationSection = nullptr;
73 // Initially this field is the number of InputSections that have been added to
74 // the OutputSection so far. Later on, after a call to assignAddresses, it
75 // corresponds to the Elf_Shdr member.
78 // The following fields correspond to Elf_Shdr members.
83 void addSection(InputSection *IS);
85 // Location in the output buffer.
86 uint8_t *Loc = nullptr;
88 // The following members are normally only used in linker scripts.
89 MemoryRegion *MemRegion = nullptr;
90 MemoryRegion *LMARegion = nullptr;
95 std::vector<BaseCommand *> SectionCommands;
96 std::vector<StringRef> Phdrs;
97 llvm::Optional<uint32_t> Filler;
98 ConstraintKind Constraint = ConstraintKind::NoConstraint;
100 std::string MemoryRegionName;
101 std::string LMARegionName;
102 bool NonAlloc = false;
104 bool ExpressionsUseSymbols = false;
105 bool InOverlay = false;
107 template <class ELFT> void finalize();
108 template <class ELFT> void writeTo(uint8_t *Buf);
109 template <class ELFT> void maybeCompress();
111 void sort(llvm::function_ref<int(InputSectionBase *S)> Order);
113 void sortCtorsDtors();
116 // Used for implementation of --compress-debug-sections option.
117 std::vector<uint8_t> ZDebugHeader;
118 llvm::SmallVector<char, 1> CompressedData;
120 uint32_t getFiller();
123 int getPriority(StringRef S);
125 std::vector<InputSection *> getInputSections(OutputSection* OS);
127 // All output sections that are handled by the linker specially are
128 // globally accessible. Writer initializes them, so don't use them
129 // until Writer is initialized.
131 static uint8_t First;
132 static PhdrEntry *TlsPhdr;
133 static OutputSection *DebugInfo;
134 static OutputSection *ElfHeader;
135 static OutputSection *ProgramHeaders;
136 static OutputSection *PreinitArray;
137 static OutputSection *InitArray;
138 static OutputSection *FiniArray;
147 uint64_t getHeaderSize();
149 extern std::vector<OutputSection *> OutputSections;