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