xref: /freebsd/contrib/llvm-project/lld/ELF/InputSection.h (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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 "Relocations.h"
13 #include "lld/Common/CommonLinkerContext.h"
14 #include "lld/Common/LLVM.h"
15 #include "lld/Common/Memory.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 InputFile;
25 class Symbol;
26 
27 class Defined;
28 struct Partition;
29 class SyntheticSection;
30 template <class ELFT> class ObjFile;
31 class OutputSection;
32 
33 extern std::vector<Partition> partitions;
34 
35 // Returned by InputSectionBase::relsOrRelas. At least one member is empty.
36 template <class ELFT> struct RelsOrRelas {
37   ArrayRef<typename ELFT::Rel> rels;
38   ArrayRef<typename ELFT::Rela> relas;
39   bool areRelocsRel() const { return rels.size(); }
40 };
41 
42 // This is the base class of all sections that lld handles. Some are sections in
43 // input files, some are sections in the produced output file and some exist
44 // just as a convenience for implementing special ways of combining some
45 // sections.
46 class SectionBase {
47 public:
48   enum Kind { Regular, Synthetic, EHFrame, Merge, Output };
49 
50   Kind kind() const { return (Kind)sectionKind; }
51 
52   StringRef name;
53 
54   uint8_t sectionKind : 3;
55 
56   // The next two bit fields are only used by InputSectionBase, but we
57   // put them here so the struct packs better.
58 
59   uint8_t bss : 1;
60 
61   // Set for sections that should not be folded by ICF.
62   uint8_t keepUnique : 1;
63 
64   // The 1-indexed partition that this section is assigned to by the garbage
65   // collector, or 0 if this section is dead. Normally there is only one
66   // partition, so this will either be 0 or 1.
67   uint8_t partition = 1;
68   elf::Partition &getPartition() const;
69 
70   // These corresponds to the fields in Elf_Shdr.
71   uint32_t alignment;
72   uint64_t flags;
73   uint32_t entsize;
74   uint32_t type;
75   uint32_t link;
76   uint32_t info;
77 
78   OutputSection *getOutputSection();
79   const OutputSection *getOutputSection() const {
80     return const_cast<SectionBase *>(this)->getOutputSection();
81   }
82 
83   // Translate an offset in the input section to an offset in the output
84   // section.
85   uint64_t getOffset(uint64_t offset) const;
86 
87   uint64_t getVA(uint64_t offset = 0) const;
88 
89   bool isLive() const { return partition != 0; }
90   void markLive() { partition = 1; }
91   void markDead() { partition = 0; }
92 
93 protected:
94   constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
95                         uint32_t entsize, uint32_t alignment, uint32_t type,
96                         uint32_t info, uint32_t link)
97       : name(name), sectionKind(sectionKind), bss(false), keepUnique(false),
98         alignment(alignment), flags(flags), entsize(entsize), type(type),
99         link(link), info(info) {}
100 };
101 
102 struct RISCVRelaxAux;
103 
104 // This corresponds to a section of an input file.
105 class InputSectionBase : public SectionBase {
106 public:
107   template <class ELFT>
108   InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
109                    StringRef name, Kind sectionKind);
110 
111   InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
112                    uint64_t entsize, uint32_t link, uint32_t info,
113                    uint32_t alignment, ArrayRef<uint8_t> data, StringRef name,
114                    Kind sectionKind);
115 
116   static bool classof(const SectionBase *s) { return s->kind() != Output; }
117 
118   // The file which contains this section. Its dynamic type is always
119   // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
120   // its static type.
121   InputFile *file;
122 
123   // Input sections are part of an output section. Special sections
124   // like .eh_frame and merge sections are first combined into a
125   // synthetic section that is then added to an output section. In all
126   // cases this points one level up.
127   SectionBase *parent = nullptr;
128 
129   // Section index of the relocation section if exists.
130   uint32_t relSecIdx = 0;
131 
132   template <class ELFT> ObjFile<ELFT> *getFile() const {
133     return cast_or_null<ObjFile<ELFT>>(file);
134   }
135 
136   // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
137   // indicate the number of bytes which is not counted in the size. This should
138   // be reset to zero after uses.
139   uint16_t bytesDropped = 0;
140 
141   // Whether the section needs to be padded with a NOP filler due to
142   // deleteFallThruJmpInsn.
143   bool nopFiller = false;
144 
145   void drop_back(unsigned num) {
146     assert(bytesDropped + num < 256);
147     bytesDropped += num;
148   }
149 
150   void push_back(uint64_t num) {
151     assert(bytesDropped >= num);
152     bytesDropped -= num;
153   }
154 
155   mutable ArrayRef<uint8_t> rawData;
156 
157   void trim() {
158     if (bytesDropped) {
159       rawData = rawData.drop_back(bytesDropped);
160       bytesDropped = 0;
161     }
162   }
163 
164   ArrayRef<uint8_t> data() const {
165     if (uncompressedSize >= 0)
166       uncompress();
167     return rawData;
168   }
169 
170   // The next member in the section group if this section is in a group. This is
171   // used by --gc-sections.
172   InputSectionBase *nextInSectionGroup = nullptr;
173 
174   template <class ELFT> RelsOrRelas<ELFT> relsOrRelas() const;
175 
176   // InputSections that are dependent on us (reverse dependency for GC)
177   llvm::TinyPtrVector<InputSection *> dependentSections;
178 
179   // Returns the size of this section (even if this is a common or BSS.)
180   size_t getSize() const;
181 
182   InputSection *getLinkOrderDep() const;
183 
184   // Get the function symbol that encloses this offset from within the
185   // section.
186   Defined *getEnclosingFunction(uint64_t offset);
187 
188   // Returns a source location string. Used to construct an error message.
189   std::string getLocation(uint64_t offset);
190   std::string getSrcMsg(const Symbol &sym, uint64_t offset);
191   std::string getObjMsg(uint64_t offset);
192 
193   // Each section knows how to relocate itself. These functions apply
194   // relocations, assuming that Buf points to this section's copy in
195   // the mmap'ed output buffer.
196   template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
197   void relocateAlloc(uint8_t *buf, uint8_t *bufEnd);
198   static uint64_t getRelocTargetVA(const InputFile *File, RelType Type,
199                                    int64_t A, uint64_t P, const Symbol &Sym,
200                                    RelExpr Expr);
201 
202   // The native ELF reloc data type is not very convenient to handle.
203   // So we convert ELF reloc records to our own records in Relocations.cpp.
204   // This vector contains such "cooked" relocations.
205   SmallVector<Relocation, 0> relocations;
206 
207   union {
208     // These are modifiers to jump instructions that are necessary when basic
209     // block sections are enabled.  Basic block sections creates opportunities
210     // to relax jump instructions at basic block boundaries after reordering the
211     // basic blocks.
212     JumpInstrMod *jumpInstrMod = nullptr;
213 
214     // Auxiliary information for RISC-V linker relaxation. RISC-V does not use
215     // jumpInstrMod.
216     RISCVRelaxAux *relaxAux;
217   };
218 
219   // A function compiled with -fsplit-stack calling a function
220   // compiled without -fsplit-stack needs its prologue adjusted. Find
221   // such functions and adjust their prologues.  This is very similar
222   // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
223   // information.
224   template <typename ELFT>
225   void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
226 
227 
228   template <typename T> llvm::ArrayRef<T> getDataAs() const {
229     size_t s = rawData.size();
230     assert(s % sizeof(T) == 0);
231     return llvm::makeArrayRef<T>((const T *)rawData.data(), s / sizeof(T));
232   }
233 
234 protected:
235   template <typename ELFT>
236   void parseCompressedHeader();
237   void uncompress() const;
238 
239   // This field stores the uncompressed size of the compressed data in rawData,
240   // or -1 if rawData is not compressed (either because the section wasn't
241   // compressed in the first place, or because we ended up uncompressing it).
242   // Since the feature is not used often, this is usually -1.
243   mutable int64_t uncompressedSize = -1;
244 };
245 
246 // SectionPiece represents a piece of splittable section contents.
247 // We allocate a lot of these and binary search on them. This means that they
248 // have to be as compact as possible, which is why we don't store the size (can
249 // be found by looking at the next one).
250 struct SectionPiece {
251   SectionPiece() = default;
252   SectionPiece(size_t off, uint32_t hash, bool live)
253       : inputOff(off), live(live), hash(hash >> 1) {}
254 
255   uint32_t inputOff;
256   uint32_t live : 1;
257   uint32_t hash : 31;
258   uint64_t outputOff = 0;
259 };
260 
261 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
262 
263 // This corresponds to a SHF_MERGE section of an input file.
264 class MergeInputSection : public InputSectionBase {
265 public:
266   template <class ELFT>
267   MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
268                     StringRef name);
269   MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize,
270                     ArrayRef<uint8_t> data, StringRef name);
271 
272   static bool classof(const SectionBase *s) { return s->kind() == Merge; }
273   void splitIntoPieces();
274 
275   // Translate an offset in the input section to an offset in the parent
276   // MergeSyntheticSection.
277   uint64_t getParentOffset(uint64_t offset) const;
278 
279   // Splittable sections are handled as a sequence of data
280   // rather than a single large blob of data.
281   SmallVector<SectionPiece, 0> pieces;
282 
283   // Returns I'th piece's data. This function is very hot when
284   // string merging is enabled, so we want to inline.
285   LLVM_ATTRIBUTE_ALWAYS_INLINE
286   llvm::CachedHashStringRef getData(size_t i) const {
287     size_t begin = pieces[i].inputOff;
288     size_t end =
289         (pieces.size() - 1 == i) ? rawData.size() : pieces[i + 1].inputOff;
290     return {toStringRef(rawData.slice(begin, end - begin)), pieces[i].hash};
291   }
292 
293   // Returns the SectionPiece at a given input section offset.
294   SectionPiece &getSectionPiece(uint64_t offset);
295   const SectionPiece &getSectionPiece(uint64_t offset) const {
296     return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
297   }
298 
299   SyntheticSection *getParent() const;
300 
301 private:
302   void splitStrings(StringRef s, size_t size);
303   void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
304 };
305 
306 struct EhSectionPiece {
307   EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
308                  unsigned firstRelocation)
309       : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
310 
311   ArrayRef<uint8_t> data() const {
312     return {sec->rawData.data() + this->inputOff, size};
313   }
314 
315   size_t inputOff;
316   ssize_t outputOff = -1;
317   InputSectionBase *sec;
318   uint32_t size;
319   unsigned firstRelocation;
320 };
321 
322 // This corresponds to a .eh_frame section of an input file.
323 class EhInputSection : public InputSectionBase {
324 public:
325   template <class ELFT>
326   EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
327                  StringRef name);
328   static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
329   template <class ELFT> void split();
330   template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
331 
332   // Splittable sections are handled as a sequence of data
333   // rather than a single large blob of data.
334   SmallVector<EhSectionPiece, 0> pieces;
335 
336   SyntheticSection *getParent() const;
337   uint64_t getParentOffset(uint64_t offset) const;
338 };
339 
340 // This is a section that is added directly to an output section
341 // instead of needing special combination via a synthetic section. This
342 // includes all input sections with the exceptions of SHF_MERGE and
343 // .eh_frame. It also includes the synthetic sections themselves.
344 class InputSection : public InputSectionBase {
345 public:
346   InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t alignment,
347                ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
348   template <class ELFT>
349   InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
350                StringRef name);
351 
352   static bool classof(const SectionBase *s) {
353     return s->kind() == SectionBase::Regular ||
354            s->kind() == SectionBase::Synthetic;
355   }
356 
357   // Write this section to a mmap'ed file, assuming Buf is pointing to
358   // beginning of the output section.
359   template <class ELFT> void writeTo(uint8_t *buf);
360 
361   OutputSection *getParent() const {
362     return reinterpret_cast<OutputSection *>(parent);
363   }
364 
365   // This variable has two usages. Initially, it represents an index in the
366   // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
367   // sections. After assignAddresses is called, it represents the offset from
368   // the beginning of the output section this section was assigned to.
369   uint64_t outSecOff = 0;
370 
371   InputSectionBase *getRelocatedSection() const;
372 
373   template <class ELFT, class RelTy>
374   void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
375 
376   // Points to the canonical section. If ICF folds two sections, repl pointer of
377   // one section points to the other.
378   InputSection *repl = this;
379 
380   // Used by ICF.
381   uint32_t eqClass[2] = {0, 0};
382 
383   // Called by ICF to merge two input sections.
384   void replace(InputSection *other);
385 
386   static InputSection discarded;
387 
388 private:
389   template <class ELFT, class RelTy>
390   void copyRelocations(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
391 
392   template <class ELFT> void copyShtGroup(uint8_t *buf);
393 };
394 
395 static_assert(sizeof(InputSection) <= 160, "InputSection is too big");
396 
397 inline bool isDebugSection(const InputSectionBase &sec) {
398   return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
399          sec.name.startswith(".debug");
400 }
401 
402 // The list of all input sections.
403 extern SmallVector<InputSectionBase *, 0> inputSections;
404 
405 // The set of TOC entries (.toc + addend) for which we should not apply
406 // toc-indirect to toc-relative relaxation. const Symbol * refers to the
407 // STT_SECTION symbol associated to the .toc input section.
408 extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax;
409 
410 } // namespace elf
411 
412 std::string toString(const elf::InputSectionBase *);
413 } // namespace lld
414 
415 #endif
416