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