1 //===- InputSection.h -------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_INPUT_SECTION_H
11 #define LLD_ELF_INPUT_SECTION_H
14 #include "Relocations.h"
16 #include "lld/Core/LLVM.h"
17 #include "llvm/ADT/CachedHashString.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/TinyPtrVector.h"
20 #include "llvm/Object/ELF.h"
30 template <class ELFT> class DefinedRegular;
31 template <class ELFT> class ObjectFile;
32 template <class ELFT> class OutputSection;
33 class OutputSectionBase;
35 // We need non-template input section class to store symbol layout
36 // in linker script parser structures, where we do not have ELFT
37 // template parameter. For each scripted output section symbol we
38 // store pointer to preceding InputSectionData object or nullptr,
39 // if symbol should be placed at the very beginning of the output
41 class InputSectionData {
43 enum Kind { Regular, EHFrame, Merge, Synthetic, };
45 // The garbage collector sets sections' Live bits.
46 // If GC is disabled, all sections are considered live by default.
47 InputSectionData(Kind SectionKind, StringRef Name, ArrayRef<uint8_t> Data,
49 : SectionKind(SectionKind), Live(Live), Assigned(false), Name(Name),
53 unsigned SectionKind : 3;
56 Kind kind() const { return (Kind)SectionKind; }
58 unsigned Live : 1; // for garbage collection
59 unsigned Assigned : 1; // for linker script
62 ArrayRef<uint8_t> Data;
64 template <typename T> llvm::ArrayRef<T> getDataAs() const {
65 size_t S = Data.size();
66 assert(S % sizeof(T) == 0);
67 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
70 std::vector<Relocation> Relocations;
73 // This corresponds to a section of an input file.
74 template <class ELFT> class InputSectionBase : public InputSectionData {
76 typedef typename ELFT::Chdr Elf_Chdr;
77 typedef typename ELFT::Rel Elf_Rel;
78 typedef typename ELFT::Rela Elf_Rela;
79 typedef typename ELFT::Shdr Elf_Shdr;
80 typedef typename ELFT::Sym Elf_Sym;
81 typedef typename ELFT::uint uintX_t;
83 // The file this section is from.
84 ObjectFile<ELFT> *File;
87 // These corresponds to the fields in Elf_Shdr.
96 : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false), Repl(this) {
98 AreRelocsRela = false;
101 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
102 StringRef Name, Kind SectionKind);
103 InputSectionBase(ObjectFile<ELFT> *File, uintX_t Flags, uint32_t Type,
104 uintX_t Entsize, uint32_t Link, uint32_t Info,
105 uintX_t Addralign, ArrayRef<uint8_t> Data, StringRef Name,
107 OutputSectionBase *OutSec = nullptr;
109 // Relocations that refer to this section.
110 const Elf_Rel *FirstRelocation = nullptr;
111 unsigned NumRelocations : 31;
112 unsigned AreRelocsRela : 1;
113 ArrayRef<Elf_Rel> rels() const {
114 assert(!AreRelocsRela);
115 return llvm::makeArrayRef(FirstRelocation, NumRelocations);
117 ArrayRef<Elf_Rela> relas() const {
118 assert(AreRelocsRela);
119 return llvm::makeArrayRef(static_cast<const Elf_Rela *>(FirstRelocation),
123 // This pointer points to the "real" instance of this instance.
124 // Usually Repl == this. However, if ICF merges two sections,
125 // Repl pointer of one section points to another section. So,
126 // if you need to get a pointer to this instance, do not use
127 // this but instead this->Repl.
128 InputSectionBase<ELFT> *Repl;
130 // Returns the size of this section (even if this is a common or BSS.)
131 size_t getSize() const;
133 ObjectFile<ELFT> *getFile() const { return File; }
134 llvm::object::ELFFile<ELFT> getObj() const { return File->getObj(); }
135 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
136 InputSectionBase *getLinkOrderDep() const;
137 // Translate an offset in the input section to an offset in the output
139 uintX_t getOffset(uintX_t Offset) const;
141 // ELF supports ZLIB-compressed section.
142 // Returns true if the section is compressed.
143 bool isCompressed() const;
146 // Returns a source location string. Used to construct an error message.
147 std::string getLocation(uintX_t Offset);
149 void relocate(uint8_t *Buf, uint8_t *BufEnd);
152 std::pair<ArrayRef<uint8_t>, uint64_t>
153 getElfCompressedData(ArrayRef<uint8_t> Data);
155 std::pair<ArrayRef<uint8_t>, uint64_t>
156 getRawCompressedData(ArrayRef<uint8_t> Data);
159 // SectionPiece represents a piece of splittable section contents.
160 // We allocate a lot of these and binary search on them. This means that they
161 // have to be as compact as possible, which is why we don't store the size (can
162 // be found by looking at the next one) and put the hash in a side table.
163 struct SectionPiece {
164 SectionPiece(size_t Off, bool Live = false)
165 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
168 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
171 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
172 "SectionPiece is too big");
174 // This corresponds to a SHF_MERGE section of an input file.
175 template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
176 typedef typename ELFT::uint uintX_t;
177 typedef typename ELFT::Sym Elf_Sym;
178 typedef typename ELFT::Shdr Elf_Shdr;
181 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
183 static bool classof(const InputSectionData *S);
184 void splitIntoPieces();
186 // Mark the piece at a given offset live. Used by GC.
187 void markLiveAt(uintX_t Offset) {
188 assert(this->Flags & llvm::ELF::SHF_ALLOC);
189 LiveOffsets.insert(Offset);
192 // Translate an offset in the input section to an offset
193 // in the output section.
194 uintX_t getOffset(uintX_t Offset) const;
196 // Splittable sections are handled as a sequence of data
197 // rather than a single large blob of data.
198 std::vector<SectionPiece> Pieces;
200 // Returns I'th piece's data. This function is very hot when
201 // string merging is enabled, so we want to inline.
202 LLVM_ATTRIBUTE_ALWAYS_INLINE
203 llvm::CachedHashStringRef getData(size_t I) const {
204 size_t Begin = Pieces[I].InputOff;
206 if (Pieces.size() - 1 == I)
207 End = this->Data.size();
209 End = Pieces[I + 1].InputOff;
211 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
212 return {S, Hashes[I]};
215 // Returns the SectionPiece at a given input section offset.
216 SectionPiece *getSectionPiece(uintX_t Offset);
217 const SectionPiece *getSectionPiece(uintX_t Offset) const;
220 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
221 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
223 std::vector<uint32_t> Hashes;
225 mutable llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
226 mutable std::once_flag InitOffsetMap;
228 llvm::DenseSet<uintX_t> LiveOffsets;
231 struct EhSectionPiece : public SectionPiece {
232 EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size,
233 unsigned FirstRelocation)
234 : SectionPiece(Off, false), ID(ID), Size(Size),
235 FirstRelocation(FirstRelocation) {}
236 InputSectionData *ID;
238 uint32_t size() const { return Size; }
240 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
241 unsigned FirstRelocation;
244 // This corresponds to a .eh_frame section of an input file.
245 template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
247 typedef typename ELFT::Shdr Elf_Shdr;
248 typedef typename ELFT::uint uintX_t;
249 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
250 static bool classof(const InputSectionData *S);
252 template <class RelTy> void split(ArrayRef<RelTy> Rels);
254 // Splittable sections are handled as a sequence of data
255 // rather than a single large blob of data.
256 std::vector<EhSectionPiece> Pieces;
259 // This corresponds to a non SHF_MERGE section of an input file.
260 template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
261 typedef InputSectionBase<ELFT> Base;
262 typedef typename ELFT::Shdr Elf_Shdr;
263 typedef typename ELFT::Rela Elf_Rela;
264 typedef typename ELFT::Rel Elf_Rel;
265 typedef typename ELFT::Sym Elf_Sym;
266 typedef typename ELFT::uint uintX_t;
267 typedef InputSectionData::Kind Kind;
271 InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign,
272 ArrayRef<uint8_t> Data, StringRef Name,
273 Kind K = InputSectionData::Regular);
274 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
276 static InputSection<ELFT> Discarded;
278 // Write this section to a mmap'ed file, assuming Buf is pointing to
279 // beginning of the output section.
280 void writeTo(uint8_t *Buf);
282 // The offset from beginning of the output sections this section was assigned
283 // to. The writer sets a value.
284 uint64_t OutSecOff = 0;
286 // InputSection that is dependent on us (reverse dependency for GC)
287 InputSectionBase<ELFT> *DependentSection = nullptr;
289 static bool classof(const InputSectionData *S);
291 InputSectionBase<ELFT> *getRelocatedSection();
293 // Register thunk related to the symbol. When the section is written
294 // to a mmap'ed file, target is requested to write an actual thunk code.
295 // Now thunks is supported for MIPS and ARM target only.
296 void addThunk(const Thunk<ELFT> *T);
298 // The offset of synthetic thunk code from beginning of this section.
299 uint64_t getThunkOff() const;
301 // Size of chunk with thunks code.
302 uint64_t getThunksSize() const;
304 template <class RelTy>
305 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
308 uint32_t Class[2] = {0, 0};
310 // Called by ICF to merge two input sections.
311 void replace(InputSection<ELFT> *Other);
314 template <class RelTy>
315 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
317 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
320 template <class ELFT> InputSection<ELFT> InputSection<ELFT>::Discarded;
322 template <class ELFT> std::string toString(const InputSectionBase<ELFT> *);