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