]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lld/ELF/InputSection.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lld / ELF / InputSection.h
1 //===- InputSection.h -------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLD_ELF_INPUT_SECTION_H
10 #define LLD_ELF_INPUT_SECTION_H
11
12 #include "Config.h"
13 #include "Relocations.h"
14 #include "Thunks.h"
15 #include "lld/Common/LLVM.h"
16 #include "llvm/ADT/CachedHashString.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/TinyPtrVector.h"
19 #include "llvm/Object/ELF.h"
20
21 namespace lld {
22 namespace elf {
23
24 class Symbol;
25 struct SectionPiece;
26
27 class Defined;
28 struct Partition;
29 class SyntheticSection;
30 class MergeSyntheticSection;
31 template <class ELFT> class ObjFile;
32 class OutputSection;
33
34 extern std::vector<Partition> partitions;
35
36 // This is the base class of all sections that lld handles. Some are sections in
37 // input files, some are sections in the produced output file and some exist
38 // just as a convenience for implementing special ways of combining some
39 // sections.
40 class SectionBase {
41 public:
42   enum Kind { Regular, EHFrame, Merge, Synthetic, Output };
43
44   Kind kind() const { return (Kind)sectionKind; }
45
46   StringRef name;
47
48   // This pointer points to the "real" instance of this instance.
49   // Usually Repl == this. However, if ICF merges two sections,
50   // Repl pointer of one section points to another section. So,
51   // if you need to get a pointer to this instance, do not use
52   // this but instead this->Repl.
53   SectionBase *repl;
54
55   unsigned sectionKind : 3;
56
57   // The next two bit fields are only used by InputSectionBase, but we
58   // put them here so the struct packs better.
59
60   unsigned bss : 1;
61
62   // Set for sections that should not be folded by ICF.
63   unsigned keepUnique : 1;
64
65   // The 1-indexed partition that this section is assigned to by the garbage
66   // collector, or 0 if this section is dead. Normally there is only one
67   // partition, so this will either be 0 or 1.
68   uint8_t partition;
69   elf::Partition &getPartition() const;
70
71   // These corresponds to the fields in Elf_Shdr.
72   uint32_t alignment;
73   uint64_t flags;
74   uint64_t entsize;
75   uint32_t type;
76   uint32_t link;
77   uint32_t info;
78
79   OutputSection *getOutputSection();
80   const OutputSection *getOutputSection() const {
81     return const_cast<SectionBase *>(this)->getOutputSection();
82   }
83
84   // Translate an offset in the input section to an offset in the output
85   // section.
86   uint64_t getOffset(uint64_t offset) const;
87
88   uint64_t getVA(uint64_t offset = 0) const;
89
90   bool isLive() const { return partition != 0; }
91   void markLive() { partition = 1; }
92   void markDead() { partition = 0; }
93
94 protected:
95   SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
96               uint64_t entsize, uint64_t alignment, uint32_t type,
97               uint32_t info, uint32_t link)
98       : name(name), repl(this), sectionKind(sectionKind), bss(false),
99         keepUnique(false), partition(0), alignment(alignment), flags(flags),
100         entsize(entsize), type(type), link(link), info(info) {}
101 };
102
103 // This corresponds to a section of an input file.
104 class InputSectionBase : public SectionBase {
105 public:
106   template <class ELFT>
107   InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
108                    StringRef name, Kind sectionKind);
109
110   InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
111                    uint64_t entsize, uint32_t link, uint32_t info,
112                    uint32_t alignment, ArrayRef<uint8_t> data, StringRef name,
113                    Kind sectionKind);
114
115   static bool classof(const SectionBase *s) { return s->kind() != Output; }
116
117   // Relocations that refer to this section.
118   unsigned numRelocations : 31;
119   unsigned areRelocsRela : 1;
120   const void *firstRelocation = nullptr;
121
122   // The file which contains this section. Its dynamic type is always
123   // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
124   // its static type.
125   InputFile *file;
126
127   template <class ELFT> ObjFile<ELFT> *getFile() const {
128     return cast_or_null<ObjFile<ELFT>>(file);
129   }
130
131   ArrayRef<uint8_t> data() const {
132     if (uncompressedSize >= 0)
133       uncompress();
134     return rawData;
135   }
136
137   uint64_t getOffsetInFile() const;
138
139   // Input sections are part of an output section. Special sections
140   // like .eh_frame and merge sections are first combined into a
141   // synthetic section that is then added to an output section. In all
142   // cases this points one level up.
143   SectionBase *parent = nullptr;
144
145   // The next member in the section group if this section is in a group. This is
146   // used by --gc-sections.
147   InputSectionBase *nextInSectionGroup = nullptr;
148
149   template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
150     assert(!areRelocsRela);
151     return llvm::makeArrayRef(
152         static_cast<const typename ELFT::Rel *>(firstRelocation),
153         numRelocations);
154   }
155
156   template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
157     assert(areRelocsRela);
158     return llvm::makeArrayRef(
159         static_cast<const typename ELFT::Rela *>(firstRelocation),
160         numRelocations);
161   }
162
163   // InputSections that are dependent on us (reverse dependency for GC)
164   llvm::TinyPtrVector<InputSection *> dependentSections;
165
166   // Returns the size of this section (even if this is a common or BSS.)
167   size_t getSize() const;
168
169   InputSection *getLinkOrderDep() const;
170
171   // Get the function symbol that encloses this offset from within the
172   // section.
173   template <class ELFT>
174   Defined *getEnclosingFunction(uint64_t offset);
175
176   // Returns a source location string. Used to construct an error message.
177   template <class ELFT> std::string getLocation(uint64_t offset);
178   std::string getSrcMsg(const Symbol &sym, uint64_t offset);
179   std::string getObjMsg(uint64_t offset);
180
181   // Each section knows how to relocate itself. These functions apply
182   // relocations, assuming that Buf points to this section's copy in
183   // the mmap'ed output buffer.
184   template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
185   void relocateAlloc(uint8_t *buf, uint8_t *bufEnd);
186
187   // The native ELF reloc data type is not very convenient to handle.
188   // So we convert ELF reloc records to our own records in Relocations.cpp.
189   // This vector contains such "cooked" relocations.
190   std::vector<Relocation> relocations;
191
192   // A function compiled with -fsplit-stack calling a function
193   // compiled without -fsplit-stack needs its prologue adjusted. Find
194   // such functions and adjust their prologues.  This is very similar
195   // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
196   // information.
197   template <typename ELFT>
198   void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
199
200
201   template <typename T> llvm::ArrayRef<T> getDataAs() const {
202     size_t s = data().size();
203     assert(s % sizeof(T) == 0);
204     return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
205   }
206
207 protected:
208   void parseCompressedHeader();
209   void uncompress() const;
210
211   mutable ArrayRef<uint8_t> rawData;
212
213   // This field stores the uncompressed size of the compressed data in rawData,
214   // or -1 if rawData is not compressed (either because the section wasn't
215   // compressed in the first place, or because we ended up uncompressing it).
216   // Since the feature is not used often, this is usually -1.
217   mutable int64_t uncompressedSize = -1;
218 };
219
220 // SectionPiece represents a piece of splittable section contents.
221 // We allocate a lot of these and binary search on them. This means that they
222 // have to be as compact as possible, which is why we don't store the size (can
223 // be found by looking at the next one).
224 struct SectionPiece {
225   SectionPiece(size_t off, uint32_t hash, bool live)
226       : inputOff(off), live(live || !config->gcSections), hash(hash >> 1) {}
227
228   uint32_t inputOff;
229   uint32_t live : 1;
230   uint32_t hash : 31;
231   uint64_t outputOff = 0;
232 };
233
234 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
235
236 // This corresponds to a SHF_MERGE section of an input file.
237 class MergeInputSection : public InputSectionBase {
238 public:
239   template <class ELFT>
240   MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
241                     StringRef name);
242   MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize,
243                     ArrayRef<uint8_t> data, StringRef name);
244
245   static bool classof(const SectionBase *s) { return s->kind() == Merge; }
246   void splitIntoPieces();
247
248   // Translate an offset in the input section to an offset in the parent
249   // MergeSyntheticSection.
250   uint64_t getParentOffset(uint64_t offset) const;
251
252   // Splittable sections are handled as a sequence of data
253   // rather than a single large blob of data.
254   std::vector<SectionPiece> pieces;
255
256   // Returns I'th piece's data. This function is very hot when
257   // string merging is enabled, so we want to inline.
258   LLVM_ATTRIBUTE_ALWAYS_INLINE
259   llvm::CachedHashStringRef getData(size_t i) const {
260     size_t begin = pieces[i].inputOff;
261     size_t end =
262         (pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff;
263     return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash};
264   }
265
266   // Returns the SectionPiece at a given input section offset.
267   SectionPiece *getSectionPiece(uint64_t offset);
268   const SectionPiece *getSectionPiece(uint64_t offset) const {
269     return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
270   }
271
272   SyntheticSection *getParent() const;
273
274 private:
275   void splitStrings(ArrayRef<uint8_t> a, size_t size);
276   void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
277 };
278
279 struct EhSectionPiece {
280   EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
281                  unsigned firstRelocation)
282       : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
283
284   ArrayRef<uint8_t> data() {
285     return {sec->data().data() + this->inputOff, size};
286   }
287
288   size_t inputOff;
289   ssize_t outputOff = -1;
290   InputSectionBase *sec;
291   uint32_t size;
292   unsigned firstRelocation;
293 };
294
295 // This corresponds to a .eh_frame section of an input file.
296 class EhInputSection : public InputSectionBase {
297 public:
298   template <class ELFT>
299   EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
300                  StringRef name);
301   static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
302   template <class ELFT> void split();
303   template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
304
305   // Splittable sections are handled as a sequence of data
306   // rather than a single large blob of data.
307   std::vector<EhSectionPiece> pieces;
308
309   SyntheticSection *getParent() const;
310 };
311
312 // This is a section that is added directly to an output section
313 // instead of needing special combination via a synthetic section. This
314 // includes all input sections with the exceptions of SHF_MERGE and
315 // .eh_frame. It also includes the synthetic sections themselves.
316 class InputSection : public InputSectionBase {
317 public:
318   InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t alignment,
319                ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
320   template <class ELFT>
321   InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
322                StringRef name);
323
324   // Write this section to a mmap'ed file, assuming Buf is pointing to
325   // beginning of the output section.
326   template <class ELFT> void writeTo(uint8_t *buf);
327
328   uint64_t getOffset(uint64_t offset) const { return outSecOff + offset; }
329
330   OutputSection *getParent() const;
331
332   // This variable has two usages. Initially, it represents an index in the
333   // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
334   // sections. After assignAddresses is called, it represents the offset from
335   // the beginning of the output section this section was assigned to.
336   uint64_t outSecOff = 0;
337
338   static bool classof(const SectionBase *s);
339
340   InputSectionBase *getRelocatedSection() const;
341
342   template <class ELFT, class RelTy>
343   void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
344
345   // Used by ICF.
346   uint32_t eqClass[2] = {0, 0};
347
348   // Called by ICF to merge two input sections.
349   void replace(InputSection *other);
350
351   static InputSection discarded;
352
353 private:
354   template <class ELFT, class RelTy>
355   void copyRelocations(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
356
357   template <class ELFT> void copyShtGroup(uint8_t *buf);
358 };
359
360 inline bool isDebugSection(const InputSectionBase &sec) {
361   return sec.name.startswith(".debug") || sec.name.startswith(".zdebug");
362 }
363
364 // The list of all input sections.
365 extern std::vector<InputSectionBase *> inputSections;
366
367 } // namespace elf
368
369 std::string toString(const elf::InputSectionBase *);
370 } // namespace lld
371
372 #endif