1 //===- InputSection.h -------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLD_ELF_INPUT_SECTION_H
11 #define LLD_ELF_INPUT_SECTION_H
12 
13 #include "Config.h"
14 #include "Relocations.h"
15 #include "Thunks.h"
16 #include "lld/Core/LLVM.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 DefinedCommon;
25 class SymbolBody;
26 struct SectionPiece;
27 
28 template <class ELFT> class ICF;
29 template <class ELFT> class DefinedRegular;
30 template <class ELFT> class ObjectFile;
31 template <class ELFT> class OutputSection;
32 template <class ELFT> class OutputSectionBase;
33 
34 // We need non-template input section class to store symbol layout
35 // in linker script parser structures, where we do not have ELFT
36 // template parameter. For each scripted output section symbol we
37 // store pointer to preceding InputSectionData object or nullptr,
38 // if symbol should be placed at the very beginning of the output
39 // section
40 class InputSectionData {
41 public:
42   enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions, MipsAbiFlags };
43 
44   // The garbage collector sets sections' Live bits.
45   // If GC is disabled, all sections are considered live by default.
46   InputSectionData(Kind SectionKind, StringRef Name, ArrayRef<uint8_t> Data,
47                    bool Compressed, bool Live)
48       : SectionKind(SectionKind), Live(Live), Compressed(Compressed),
49         Name(Name), Data(Data) {}
50 
51 private:
52   unsigned SectionKind : 3;
53 
54 public:
55   Kind kind() const { return (Kind)SectionKind; }
56 
57   // Used for garbage collection.
58   unsigned Live : 1;
59 
60   unsigned Compressed : 1;
61 
62   uint32_t Alignment;
63 
64   StringRef Name;
65 
66   ArrayRef<uint8_t> Data;
67   ArrayRef<uint8_t> getData(const SectionPiece &P) const;
68 
69   // If a section is compressed, this has the uncompressed section data.
70   std::unique_ptr<uint8_t[]> UncompressedData;
71 
72   std::vector<Relocation> Relocations;
73 };
74 
75 // This corresponds to a section of an input file.
76 template <class ELFT> class InputSectionBase : public InputSectionData {
77 protected:
78   typedef typename ELFT::Chdr Elf_Chdr;
79   typedef typename ELFT::Rel Elf_Rel;
80   typedef typename ELFT::Rela Elf_Rela;
81   typedef typename ELFT::Shdr Elf_Shdr;
82   typedef typename ELFT::Sym Elf_Sym;
83   typedef typename ELFT::uint uintX_t;
84   const Elf_Shdr *Header;
85 
86   // The file this section is from.
87   ObjectFile<ELFT> *File;
88 
89 public:
90   InputSectionBase()
91       : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false, false),
92         Repl(this) {}
93 
94   InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
95                    StringRef Name, Kind SectionKind);
96   OutputSectionBase<ELFT> *OutSec = nullptr;
97 
98   // This pointer points to the "real" instance of this instance.
99   // Usually Repl == this. However, if ICF merges two sections,
100   // Repl pointer of one section points to another section. So,
101   // if you need to get a pointer to this instance, do not use
102   // this but instead this->Repl.
103   InputSectionBase<ELFT> *Repl;
104 
105   // Returns the size of this section (even if this is a common or BSS.)
106   size_t getSize() const;
107 
108   static InputSectionBase<ELFT> Discarded;
109 
110   const Elf_Shdr *getSectionHdr() const { return Header; }
111   ObjectFile<ELFT> *getFile() const { return File; }
112   uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
113   InputSectionBase *getLinkOrderDep() const;
114   // Translate an offset in the input section to an offset in the output
115   // section.
116   uintX_t getOffset(uintX_t Offset) const;
117 
118   void uncompress();
119 
120   void relocate(uint8_t *Buf, uint8_t *BufEnd);
121 
122 private:
123   std::pair<ArrayRef<uint8_t>, uint64_t>
124   getElfCompressedData(ArrayRef<uint8_t> Data);
125 
126   std::pair<ArrayRef<uint8_t>, uint64_t>
127   getRawCompressedData(ArrayRef<uint8_t> Data);
128 };
129 
130 template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
131 
132 // SectionPiece represents a piece of splittable section contents.
133 struct SectionPiece {
134   SectionPiece(size_t Off, ArrayRef<uint8_t> Data, uint32_t Hash, bool Live)
135       : InputOff(Off), Hash(Hash), Size(Data.size()),
136         Live(Live || !Config->GcSections) {}
137   SectionPiece(size_t Off, ArrayRef<uint8_t> Data, bool Live = false)
138       : SectionPiece(Off, Data, hash_value(Data), Live) {}
139 
140   size_t size() const { return Size; }
141 
142   size_t InputOff;
143   size_t OutputOff = -1;
144 
145   uint32_t Hash;
146 
147 private:
148   // We use bitfields because SplitInputSection is accessed by
149   // std::upper_bound very often.
150   // We want to save bits to make it cache friendly.
151   uint32_t Size : 31;
152 
153 public:
154   uint32_t Live : 1;
155 };
156 
157 // This corresponds to a SHF_MERGE section of an input file.
158 template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
159   typedef typename ELFT::uint uintX_t;
160   typedef typename ELFT::Sym Elf_Sym;
161   typedef typename ELFT::Shdr Elf_Shdr;
162 
163 public:
164   MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
165                     StringRef Name);
166   static bool classof(const InputSectionBase<ELFT> *S);
167   void splitIntoPieces();
168 
169   // Mark the piece at a given offset live. Used by GC.
170   void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); }
171 
172   // Translate an offset in the input section to an offset
173   // in the output section.
174   uintX_t getOffset(uintX_t Offset) const;
175 
176   void finalizePieces();
177 
178   // Splittable sections are handled as a sequence of data
179   // rather than a single large blob of data.
180   std::vector<SectionPiece> Pieces;
181 
182   // Returns the SectionPiece at a given input section offset.
183   SectionPiece *getSectionPiece(uintX_t Offset);
184   const SectionPiece *getSectionPiece(uintX_t Offset) const;
185 
186 private:
187   std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> A, size_t Size);
188   std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
189 
190   llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
191   llvm::DenseSet<uintX_t> LiveOffsets;
192 };
193 
194 struct EhSectionPiece : public SectionPiece {
195   EhSectionPiece(size_t Off, ArrayRef<uint8_t> Data, unsigned FirstRelocation)
196       : SectionPiece(Off, Data, 0, false), Data(Data.data()),
197         FirstRelocation(FirstRelocation) {}
198   const uint8_t *Data;
199   ArrayRef<uint8_t> data() { return {Data, size()}; }
200   unsigned FirstRelocation;
201 };
202 
203 // This corresponds to a .eh_frame section of an input file.
204 template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
205 public:
206   typedef typename ELFT::Shdr Elf_Shdr;
207   typedef typename ELFT::uint uintX_t;
208   EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
209   static bool classof(const InputSectionBase<ELFT> *S);
210   void split();
211   template <class RelTy> void split(ArrayRef<RelTy> Rels);
212 
213   // Splittable sections are handled as a sequence of data
214   // rather than a single large blob of data.
215   std::vector<EhSectionPiece> Pieces;
216 
217   // Relocation section that refer to this one.
218   const Elf_Shdr *RelocSection = nullptr;
219 };
220 
221 // This corresponds to a non SHF_MERGE section of an input file.
222 template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
223   friend ICF<ELFT>;
224   typedef InputSectionBase<ELFT> Base;
225   typedef typename ELFT::Shdr Elf_Shdr;
226   typedef typename ELFT::Rela Elf_Rela;
227   typedef typename ELFT::Rel Elf_Rel;
228   typedef typename ELFT::Sym Elf_Sym;
229   typedef typename ELFT::uint uintX_t;
230 
231 public:
232   InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
233 
234   // Write this section to a mmap'ed file, assuming Buf is pointing to
235   // beginning of the output section.
236   void writeTo(uint8_t *Buf);
237 
238   // Relocation sections that refer to this one.
239   llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
240 
241   // The offset from beginning of the output sections this section was assigned
242   // to. The writer sets a value.
243   uint64_t OutSecOff = 0;
244 
245   // InputSection that is dependent on us (reverse dependency for GC)
246   InputSectionBase<ELFT> *DependentSection = nullptr;
247 
248   static bool classof(const InputSectionBase<ELFT> *S);
249 
250   InputSectionBase<ELFT> *getRelocatedSection();
251 
252   // Register thunk related to the symbol. When the section is written
253   // to a mmap'ed file, target is requested to write an actual thunk code.
254   // Now thunks is supported for MIPS and ARM target only.
255   void addThunk(const Thunk<ELFT> *T);
256 
257   // The offset of synthetic thunk code from beginning of this section.
258   uint64_t getThunkOff() const;
259 
260   // Size of chunk with thunks code.
261   uint64_t getThunksSize() const;
262 
263   template <class RelTy>
264   void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
265 
266 private:
267   template <class RelTy>
268   void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
269 
270   // Called by ICF to merge two input sections.
271   void replace(InputSection<ELFT> *Other);
272 
273   // Used by ICF.
274   uint64_t GroupId = 0;
275 
276   llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
277 };
278 
279 // MIPS .reginfo section provides information on the registers used by the code
280 // in the object file. Linker should collect this information and write a single
281 // .reginfo section in the output file. The output section contains a union of
282 // used registers masks taken from input .reginfo sections and final value
283 // of the `_gp` symbol.  For details: Chapter 4 / "Register Information" at
284 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
285 template <class ELFT>
286 class MipsReginfoInputSection : public InputSectionBase<ELFT> {
287   typedef typename ELFT::Shdr Elf_Shdr;
288 
289 public:
290   MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr,
291                           StringRef Name);
292   static bool classof(const InputSectionBase<ELFT> *S);
293 
294   const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
295 };
296 
297 template <class ELFT>
298 class MipsOptionsInputSection : public InputSectionBase<ELFT> {
299   typedef typename ELFT::Shdr Elf_Shdr;
300 
301 public:
302   MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr,
303                           StringRef Name);
304   static bool classof(const InputSectionBase<ELFT> *S);
305 
306   const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
307 };
308 
309 template <class ELFT>
310 class MipsAbiFlagsInputSection : public InputSectionBase<ELFT> {
311   typedef typename ELFT::Shdr Elf_Shdr;
312 
313 public:
314   MipsAbiFlagsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr,
315                            StringRef Name);
316   static bool classof(const InputSectionBase<ELFT> *S);
317 
318   const llvm::object::Elf_Mips_ABIFlags<ELFT> *Flags = nullptr;
319 };
320 
321 // Common symbols don't belong to any section. But it is easier for us
322 // to handle them as if they belong to some input section. So we defined
323 // this class. CommonInputSection is a virtual singleton class that
324 // "contains" all common symbols.
325 template <class ELFT> class CommonInputSection : public InputSection<ELFT> {
326   typedef typename ELFT::uint uintX_t;
327 
328 public:
329   CommonInputSection(std::vector<DefinedCommon *> Syms);
330 
331   // The singleton instance of this class.
332   static CommonInputSection<ELFT> *X;
333 
334 private:
335   static typename ELFT::Shdr Hdr;
336 };
337 
338 template <class ELFT> CommonInputSection<ELFT> *CommonInputSection<ELFT>::X;
339 template <class ELFT> typename ELFT::Shdr CommonInputSection<ELFT>::Hdr;
340 
341 } // namespace elf
342 } // namespace lld
343 
344 #endif
345