]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/InputSection.h
Merge llvm, clang, lld and lldb trunk r291274, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / InputSection.h
1 //===- InputSection.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_INPUT_SECTION_H
11 #define LLD_ELF_INPUT_SECTION_H
12
13 #include "Config.h"
14 #include "Relocations.h"
15 #include "Thunks.h"
16 #include "lld/Core/LLVM.h"
17 #include "llvm/ADT/CachedHashString.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/TinyPtrVector.h"
20 #include "llvm/Object/ELF.h"
21 #include <mutex>
22
23 namespace lld {
24 namespace elf {
25
26 class DefinedCommon;
27 class SymbolBody;
28 struct SectionPiece;
29
30 template <class ELFT> class DefinedRegular;
31 template <class ELFT> class ObjectFile;
32 template <class ELFT> class OutputSection;
33 class OutputSectionBase;
34
35 // We need non-template input section class to store symbol layout
36 // in linker script parser structures, where we do not have ELFT
37 // template parameter. For each scripted output section symbol we
38 // store pointer to preceding InputSectionData object or nullptr,
39 // if symbol should be placed at the very beginning of the output
40 // section
41 class InputSectionData {
42 public:
43   enum Kind { Regular, EHFrame, Merge, Synthetic, };
44
45   // The garbage collector sets sections' Live bits.
46   // If GC is disabled, all sections are considered live by default.
47   InputSectionData(Kind SectionKind, StringRef Name, ArrayRef<uint8_t> Data,
48                    bool Live)
49       : SectionKind(SectionKind), Live(Live), Assigned(false), Name(Name),
50         Data(Data) {}
51
52 private:
53   unsigned SectionKind : 3;
54
55 public:
56   Kind kind() const { return (Kind)SectionKind; }
57
58   unsigned Live : 1;       // for garbage collection
59   unsigned Assigned : 1;   // for linker script
60   uint32_t Alignment;
61   StringRef Name;
62   ArrayRef<uint8_t> Data;
63
64   template <typename T> llvm::ArrayRef<T> getDataAs() const {
65     size_t S = Data.size();
66     assert(S % sizeof(T) == 0);
67     return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
68   }
69
70   std::vector<Relocation> Relocations;
71 };
72
73 // This corresponds to a section of an input file.
74 template <class ELFT> class InputSectionBase : public InputSectionData {
75 protected:
76   typedef typename ELFT::Chdr Elf_Chdr;
77   typedef typename ELFT::Rel Elf_Rel;
78   typedef typename ELFT::Rela Elf_Rela;
79   typedef typename ELFT::Shdr Elf_Shdr;
80   typedef typename ELFT::Sym Elf_Sym;
81   typedef typename ELFT::uint uintX_t;
82
83   // The file this section is from.
84   ObjectFile<ELFT> *File;
85
86 public:
87   // These corresponds to the fields in Elf_Shdr.
88   uintX_t Flags;
89   uintX_t Offset = 0;
90   uintX_t Entsize;
91   uint32_t Type;
92   uint32_t Link;
93   uint32_t Info;
94
95   InputSectionBase()
96       : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false), Repl(this) {
97     NumRelocations = 0;
98     AreRelocsRela = false;
99   }
100
101   InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
102                    StringRef Name, Kind SectionKind);
103   InputSectionBase(ObjectFile<ELFT> *File, uintX_t Flags, uint32_t Type,
104                    uintX_t Entsize, uint32_t Link, uint32_t Info,
105                    uintX_t Addralign, ArrayRef<uint8_t> Data, StringRef Name,
106                    Kind SectionKind);
107   OutputSectionBase *OutSec = nullptr;
108
109   // Relocations that refer to this section.
110   const Elf_Rel *FirstRelocation = nullptr;
111   unsigned NumRelocations : 31;
112   unsigned AreRelocsRela : 1;
113   ArrayRef<Elf_Rel> rels() const {
114     assert(!AreRelocsRela);
115     return llvm::makeArrayRef(FirstRelocation, NumRelocations);
116   }
117   ArrayRef<Elf_Rela> relas() const {
118     assert(AreRelocsRela);
119     return llvm::makeArrayRef(static_cast<const Elf_Rela *>(FirstRelocation),
120                               NumRelocations);
121   }
122
123   // This pointer points to the "real" instance of this instance.
124   // Usually Repl == this. However, if ICF merges two sections,
125   // Repl pointer of one section points to another section. So,
126   // if you need to get a pointer to this instance, do not use
127   // this but instead this->Repl.
128   InputSectionBase<ELFT> *Repl;
129
130   // Returns the size of this section (even if this is a common or BSS.)
131   size_t getSize() const;
132
133   ObjectFile<ELFT> *getFile() const { return File; }
134   llvm::object::ELFFile<ELFT> getObj() const { return File->getObj(); }
135   uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
136   InputSectionBase *getLinkOrderDep() const;
137   // Translate an offset in the input section to an offset in the output
138   // section.
139   uintX_t getOffset(uintX_t Offset) const;
140
141   // ELF supports ZLIB-compressed section.
142   // Returns true if the section is compressed.
143   bool isCompressed() const;
144   void uncompress();
145
146   // Returns a source location string. Used to construct an error message.
147   std::string getLocation(uintX_t Offset);
148
149   void relocate(uint8_t *Buf, uint8_t *BufEnd);
150
151 private:
152   std::pair<ArrayRef<uint8_t>, uint64_t>
153   getElfCompressedData(ArrayRef<uint8_t> Data);
154
155   std::pair<ArrayRef<uint8_t>, uint64_t>
156   getRawCompressedData(ArrayRef<uint8_t> Data);
157 };
158
159 // SectionPiece represents a piece of splittable section contents.
160 // We allocate a lot of these and binary search on them. This means that they
161 // have to be as compact as possible, which is why we don't store the size (can
162 // be found by looking at the next one) and put the hash in a side table.
163 struct SectionPiece {
164   SectionPiece(size_t Off, bool Live = false)
165       : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
166
167   size_t InputOff;
168   ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
169   size_t Live : 1;
170 };
171 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
172               "SectionPiece is too big");
173
174 // This corresponds to a SHF_MERGE section of an input file.
175 template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
176   typedef typename ELFT::uint uintX_t;
177   typedef typename ELFT::Sym Elf_Sym;
178   typedef typename ELFT::Shdr Elf_Shdr;
179
180 public:
181   MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
182                     StringRef Name);
183   static bool classof(const InputSectionData *S);
184   void splitIntoPieces();
185
186   // Mark the piece at a given offset live. Used by GC.
187   void markLiveAt(uintX_t Offset) {
188     assert(this->Flags & llvm::ELF::SHF_ALLOC);
189     LiveOffsets.insert(Offset);
190   }
191
192   // Translate an offset in the input section to an offset
193   // in the output section.
194   uintX_t getOffset(uintX_t Offset) const;
195
196   // Splittable sections are handled as a sequence of data
197   // rather than a single large blob of data.
198   std::vector<SectionPiece> Pieces;
199
200   // Returns I'th piece's data. This function is very hot when
201   // string merging is enabled, so we want to inline.
202   LLVM_ATTRIBUTE_ALWAYS_INLINE
203   llvm::CachedHashStringRef getData(size_t I) const {
204     size_t Begin = Pieces[I].InputOff;
205     size_t End;
206     if (Pieces.size() - 1 == I)
207       End = this->Data.size();
208     else
209       End = Pieces[I + 1].InputOff;
210
211     StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
212     return {S, Hashes[I]};
213   }
214
215   // Returns the SectionPiece at a given input section offset.
216   SectionPiece *getSectionPiece(uintX_t Offset);
217   const SectionPiece *getSectionPiece(uintX_t Offset) const;
218
219 private:
220   void splitStrings(ArrayRef<uint8_t> A, size_t Size);
221   void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
222
223   std::vector<uint32_t> Hashes;
224
225   mutable llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
226   mutable std::once_flag InitOffsetMap;
227
228   llvm::DenseSet<uintX_t> LiveOffsets;
229 };
230
231 struct EhSectionPiece : public SectionPiece {
232   EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size,
233                  unsigned FirstRelocation)
234       : SectionPiece(Off, false), ID(ID), Size(Size),
235         FirstRelocation(FirstRelocation) {}
236   InputSectionData *ID;
237   uint32_t Size;
238   uint32_t size() const { return Size; }
239
240   ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
241   unsigned FirstRelocation;
242 };
243
244 // This corresponds to a .eh_frame section of an input file.
245 template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
246 public:
247   typedef typename ELFT::Shdr Elf_Shdr;
248   typedef typename ELFT::uint uintX_t;
249   EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
250   static bool classof(const InputSectionData *S);
251   void split();
252   template <class RelTy> void split(ArrayRef<RelTy> Rels);
253
254   // Splittable sections are handled as a sequence of data
255   // rather than a single large blob of data.
256   std::vector<EhSectionPiece> Pieces;
257 };
258
259 // This corresponds to a non SHF_MERGE section of an input file.
260 template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
261   typedef InputSectionBase<ELFT> Base;
262   typedef typename ELFT::Shdr Elf_Shdr;
263   typedef typename ELFT::Rela Elf_Rela;
264   typedef typename ELFT::Rel Elf_Rel;
265   typedef typename ELFT::Sym Elf_Sym;
266   typedef typename ELFT::uint uintX_t;
267   typedef InputSectionData::Kind Kind;
268
269 public:
270   InputSection();
271   InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign,
272                ArrayRef<uint8_t> Data, StringRef Name,
273                Kind K = InputSectionData::Regular);
274   InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
275
276   static InputSection<ELFT> Discarded;
277
278   // Write this section to a mmap'ed file, assuming Buf is pointing to
279   // beginning of the output section.
280   void writeTo(uint8_t *Buf);
281
282   // The offset from beginning of the output sections this section was assigned
283   // to. The writer sets a value.
284   uint64_t OutSecOff = 0;
285
286   // InputSection that is dependent on us (reverse dependency for GC)
287   InputSectionBase<ELFT> *DependentSection = nullptr;
288
289   static bool classof(const InputSectionData *S);
290
291   InputSectionBase<ELFT> *getRelocatedSection();
292
293   // Register thunk related to the symbol. When the section is written
294   // to a mmap'ed file, target is requested to write an actual thunk code.
295   // Now thunks is supported for MIPS and ARM target only.
296   void addThunk(const Thunk<ELFT> *T);
297
298   // The offset of synthetic thunk code from beginning of this section.
299   uint64_t getThunkOff() const;
300
301   // Size of chunk with thunks code.
302   uint64_t getThunksSize() const;
303
304   template <class RelTy>
305   void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
306
307   // Used by ICF.
308   uint32_t Class[2] = {0, 0};
309
310   // Called by ICF to merge two input sections.
311   void replace(InputSection<ELFT> *Other);
312
313 private:
314   template <class RelTy>
315   void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
316
317   llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
318 };
319
320 template <class ELFT> InputSection<ELFT> InputSection<ELFT>::Discarded;
321 } // namespace elf
322
323 template <class ELFT> std::string toString(const elf::InputSectionBase<ELFT> *);
324 } // namespace lld
325
326 #endif