]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/OutputSections.h
Merge compiler-rt trunk r321414 to contrib/compiler-rt.
[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 "LinkerScript.h"
16 #include "Relocations.h"
17
18 #include "lld/Common/LLVM.h"
19 #include "llvm/MC/StringTableBuilder.h"
20 #include "llvm/Object/ELF.h"
21
22 namespace lld {
23 namespace elf {
24
25 struct PhdrEntry;
26 class Symbol;
27 struct EhSectionPiece;
28 class EhInputSection;
29 class InputSection;
30 class InputSectionBase;
31 class MergeInputSection;
32 class OutputSection;
33 template <class ELFT> class ObjFile;
34 template <class ELFT> class SharedFile;
35 class SharedSymbol;
36 class Defined;
37
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 {
43 public:
44   OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
45
46   static bool classof(const SectionBase *S) {
47     return S->kind() == SectionBase::Output;
48   }
49
50   static bool classof(const BaseCommand *C);
51
52   uint64_t getLMA() const { return Addr + LMAOffset; }
53   template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
54
55   unsigned SectionIndex;
56   unsigned SortRank;
57
58   uint32_t getPhdrFlags() const;
59
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;
68
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;
73
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.
77   uint64_t Size = 0;
78
79   // The following fields correspond to Elf_Shdr members.
80   uint64_t Offset = 0;
81   uint64_t LMAOffset = 0;
82   uint64_t Addr = 0;
83   uint32_t ShName = 0;
84
85   void addSection(InputSection *IS);
86
87   // Location in the output buffer.
88   uint8_t *Loc = nullptr;
89
90   // The following members are normally only used in linker scripts.
91   MemoryRegion *MemRegion = nullptr;
92   Expr AddrExpr;
93   Expr AlignExpr;
94   Expr LMAExpr;
95   Expr SubalignExpr;
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   bool Noload = false;
103
104   template <class ELFT> void finalize();
105   template <class ELFT> void writeTo(uint8_t *Buf);
106   template <class ELFT> void maybeCompress();
107
108   void sort(std::function<int(InputSectionBase *S)> Order);
109   void sortInitFini();
110   void sortCtorsDtors();
111
112 private:
113   // Used for implementation of --compress-debug-sections option.
114   std::vector<uint8_t> ZDebugHeader;
115   llvm::SmallVector<char, 1> CompressedData;
116
117   uint32_t getFiller();
118 };
119
120 int getPriority(StringRef S);
121
122 // All output sections that are handled by the linker specially are
123 // globally accessible. Writer initializes them, so don't use them
124 // until Writer is initialized.
125 struct Out {
126   static uint8_t First;
127   static OutputSection *Opd;
128   static uint8_t *OpdBuf;
129   static PhdrEntry *TlsPhdr;
130   static OutputSection *DebugInfo;
131   static OutputSection *ElfHeader;
132   static OutputSection *ProgramHeaders;
133   static OutputSection *PreinitArray;
134   static OutputSection *InitArray;
135   static OutputSection *FiniArray;
136 };
137
138 } // namespace elf
139 } // namespace lld
140
141 namespace lld {
142 namespace elf {
143
144 uint64_t getHeaderSize();
145 void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
146                  std::function<int(InputSectionBase *S)> Order);
147
148 extern std::vector<OutputSection *> OutputSections;
149 } // namespace elf
150 } // namespace lld
151
152 #endif