]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/OutputSections.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r302069, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / OutputSections.h
1 //===- OutputSections.h -----------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_ELF_OUTPUT_SECTIONS_H
11 #define LLD_ELF_OUTPUT_SECTIONS_H
12
13 #include "Config.h"
14 #include "InputSection.h"
15 #include "Relocations.h"
16
17 #include "lld/Core/LLVM.h"
18 #include "llvm/MC/StringTableBuilder.h"
19 #include "llvm/Object/ELF.h"
20
21 namespace lld {
22 namespace elf {
23
24 struct PhdrEntry;
25 class SymbolBody;
26 struct EhSectionPiece;
27 class EhInputSection;
28 class InputSection;
29 class InputSectionBase;
30 class MergeInputSection;
31 class OutputSection;
32 template <class ELFT> class ObjectFile;
33 template <class ELFT> class SharedFile;
34 class SharedSymbol;
35 class DefinedRegular;
36
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 {
42 public:
43   OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
44
45   static bool classof(const SectionBase *S) {
46     return S->kind() == SectionBase::Output;
47   }
48
49   uint64_t getLMA() const { return Addr + LMAOffset; }
50   template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
51
52   unsigned SectionIndex;
53
54   uint32_t getPhdrFlags() const;
55
56   void updateAlignment(uint32_t Val) {
57     if (Val > Alignment)
58       Alignment = Val;
59   }
60
61   // If true, this section will be page aligned on disk.
62   // Typically the first section of each PT_LOAD segment has this flag.
63   bool PageAlign = false;
64
65   // Pointer to the first section in PT_LOAD segment, which this section
66   // also resides in. This field is used to correctly compute file offset
67   // of a section. When two sections share the same load segment, difference
68   // between their file offsets should be equal to difference between their
69   // virtual addresses. To compute some section offset we use the following
70   // formula: Off = Off_first + VA - VA_first.
71   OutputSection *FirstInPtLoad = nullptr;
72
73   // The following fields correspond to Elf_Shdr members.
74   uint64_t Size = 0;
75   uint64_t Offset = 0;
76   uint64_t LMAOffset = 0;
77   uint64_t Addr = 0;
78   uint32_t ShName = 0;
79
80   void addSection(InputSection *S);
81   void sort(std::function<int(InputSectionBase *S)> Order);
82   void sortInitFini();
83   void sortCtorsDtors();
84   uint32_t getFiller();
85   template <class ELFT> void writeTo(uint8_t *Buf);
86   template <class ELFT> void finalize();
87   template <class ELFT> void maybeCompress();
88   void assignOffsets();
89   std::vector<InputSection *> Sections;
90
91   // Used for implementation of --compress-debug-sections option.
92   std::vector<uint8_t> ZDebugHeader;
93   llvm::SmallVector<char, 1> CompressedData;
94
95   // Location in the output buffer.
96   uint8_t *Loc = nullptr;
97 };
98
99 // All output sections that are handled by the linker specially are
100 // globally accessible. Writer initializes them, so don't use them
101 // until Writer is initialized.
102 struct Out {
103   static uint8_t First;
104   static OutputSection *Opd;
105   static uint8_t *OpdBuf;
106   static PhdrEntry *TlsPhdr;
107   static OutputSection *DebugInfo;
108   static OutputSection *ElfHeader;
109   static OutputSection *ProgramHeaders;
110   static OutputSection *PreinitArray;
111   static OutputSection *InitArray;
112   static OutputSection *FiniArray;
113 };
114
115 struct SectionKey {
116   StringRef Name;
117   uint64_t Flags;
118   uint32_t Alignment;
119 };
120 }
121 }
122 namespace llvm {
123 template <> struct DenseMapInfo<lld::elf::SectionKey> {
124   static lld::elf::SectionKey getEmptyKey();
125   static lld::elf::SectionKey getTombstoneKey();
126   static unsigned getHashValue(const lld::elf::SectionKey &Val);
127   static bool isEqual(const lld::elf::SectionKey &LHS,
128                       const lld::elf::SectionKey &RHS);
129 };
130 }
131 namespace lld {
132 namespace elf {
133
134 // This class knows how to create an output section for a given
135 // input section. Output section type is determined by various
136 // factors, including input section's sh_flags, sh_type and
137 // linker scripts.
138 class OutputSectionFactory {
139 public:
140   OutputSectionFactory(std::vector<OutputSection *> &OutputSections);
141   ~OutputSectionFactory();
142
143   void addInputSec(InputSectionBase *IS, StringRef OutsecName);
144   void addInputSec(InputSectionBase *IS, StringRef OutsecName,
145                    OutputSection *&Sec);
146
147 private:
148   llvm::SmallDenseMap<SectionKey, OutputSection *> Map;
149   std::vector<OutputSection *> &OutputSections;
150 };
151
152 uint64_t getHeaderSize();
153
154 } // namespace elf
155 } // namespace lld
156
157
158 #endif