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"
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 uint32_t SectionIndex = UINT32_MAX;
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<std::array<uint8_t, 4>> Filler;
99 ConstraintKind Constraint = ConstraintKind::NoConstraint;
100 std::string Location;
101 std::string MemoryRegionName;
102 std::string LMARegionName;
103 bool NonAlloc = false;
105 bool ExpressionsUseSymbols = false;
106 bool InOverlay = false;
108 template <class ELFT> void finalize();
109 template <class ELFT> void writeTo(uint8_t *Buf);
110 template <class ELFT> void maybeCompress();
112 void sort(llvm::function_ref<int(InputSectionBase *S)> Order);
114 void sortCtorsDtors();
117 // Used for implementation of --compress-debug-sections option.
118 std::vector<uint8_t> ZDebugHeader;
119 llvm::SmallVector<char, 1> CompressedData;
121 std::array<uint8_t, 4> getFiller();
124 int getPriority(StringRef S);
126 std::vector<InputSection *> getInputSections(OutputSection* OS);
128 // All output sections that are handled by the linker specially are
129 // globally accessible. Writer initializes them, so don't use them
130 // until Writer is initialized.
132 static uint8_t First;
133 static PhdrEntry *TlsPhdr;
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;