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 "Relocations.h"
17 #include "lld/Core/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 ObjectFile;
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 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 uint64_t getLMA() const { return Addr + LMAOffset; }
50 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
52 unsigned SectionIndex;
55 uint32_t getPhdrFlags() const;
57 void updateAlignment(uint32_t Val) {
62 // If true, this section will be page aligned on disk.
63 // Typically the first section of each PT_LOAD segment has this flag.
64 bool PageAlign = false;
66 // Pointer to the first section in PT_LOAD segment, which this section
67 // also resides in. This field is used to correctly compute file offset
68 // of a section. When two sections share the same load segment, difference
69 // between their file offsets should be equal to difference between their
70 // virtual addresses. To compute some section offset we use the following
71 // formula: Off = Off_first + VA - VA_first.
72 OutputSection *FirstInPtLoad = nullptr;
74 // The following fields correspond to Elf_Shdr members.
77 uint64_t LMAOffset = 0;
81 void addSection(InputSection *S);
82 void sort(std::function<int(InputSectionBase *S)> Order);
84 void sortCtorsDtors();
86 template <class ELFT> void writeTo(uint8_t *Buf);
87 template <class ELFT> void finalize();
88 template <class ELFT> void maybeCompress();
90 std::vector<InputSection *> Sections;
92 // Used for implementation of --compress-debug-sections option.
93 std::vector<uint8_t> ZDebugHeader;
94 llvm::SmallVector<char, 1> CompressedData;
96 // Location in the output buffer.
97 uint8_t *Loc = nullptr;
100 // All output sections that are handled by the linker specially are
101 // globally accessible. Writer initializes them, so don't use them
102 // until Writer is initialized.
104 static uint8_t First;
105 static OutputSection *Opd;
106 static uint8_t *OpdBuf;
107 static PhdrEntry *TlsPhdr;
108 static OutputSection *DebugInfo;
109 static OutputSection *ElfHeader;
110 static OutputSection *ProgramHeaders;
111 static OutputSection *PreinitArray;
112 static OutputSection *InitArray;
113 static OutputSection *FiniArray;
124 template <> struct DenseMapInfo<lld::elf::SectionKey> {
125 static lld::elf::SectionKey getEmptyKey();
126 static lld::elf::SectionKey getTombstoneKey();
127 static unsigned getHashValue(const lld::elf::SectionKey &Val);
128 static bool isEqual(const lld::elf::SectionKey &LHS,
129 const lld::elf::SectionKey &RHS);
135 // This class knows how to create an output section for a given
136 // input section. Output section type is determined by various
137 // factors, including input section's sh_flags, sh_type and
139 class OutputSectionFactory {
141 OutputSectionFactory(std::vector<OutputSection *> &OutputSections);
142 ~OutputSectionFactory();
144 void addInputSec(InputSectionBase *IS, StringRef OutsecName);
145 void addInputSec(InputSectionBase *IS, StringRef OutsecName,
146 OutputSection *&Sec);
149 llvm::SmallDenseMap<SectionKey, OutputSection *> Map;
150 std::vector<OutputSection *> &OutputSections;
153 uint64_t getHeaderSize();