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