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