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"
31 class SyntheticSection;
32 template <class ELFT> class EhFrameSection;
33 class MergeSyntheticSection;
34 template <class ELFT> class ObjectFile;
37 // This is the base class of all sections that lld handles. Some are sections in
38 // input files, some are sections in the produced output file and some exist
39 // just as a convenience for implementing special ways of combining some
43 enum Kind { Regular, EHFrame, Merge, Synthetic, Output };
45 Kind kind() const { return (Kind)SectionKind; }
49 unsigned SectionKind : 3;
51 // The next two bit fields are only used by InputSectionBase, but we
52 // put them here so the struct packs better.
54 // The garbage collector sets sections' Live bits.
55 // If GC is disabled, all sections are considered live by default.
56 unsigned Live : 1; // for garbage collection
57 unsigned Assigned : 1; // for linker script
61 // These corresponds to the fields in Elf_Shdr.
68 OutputSection *getOutputSection();
69 const OutputSection *getOutputSection() const {
70 return const_cast<SectionBase *>(this)->getOutputSection();
73 // Translate an offset in the input section to an offset in the output
75 uint64_t getOffset(uint64_t Offset) const;
77 uint64_t getOffset(const DefinedRegular &Sym) const;
80 SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags,
81 uint64_t Entsize, uint64_t Alignment, uint32_t Type,
82 uint32_t Info, uint32_t Link)
83 : Name(Name), SectionKind(SectionKind), Alignment(Alignment),
84 Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info) {
90 // This corresponds to a section of an input file.
91 class InputSectionBase : public SectionBase {
93 static bool classof(const SectionBase *S);
95 // The file this section is from.
98 ArrayRef<uint8_t> Data;
99 uint64_t getOffsetInFile() const;
101 static InputSectionBase Discarded;
104 : SectionBase(Regular, "", /*Flags*/ 0, /*Entsize*/ 0, /*Alignment*/ 0,
106 /*Info*/ 0, /*Link*/ 0),
111 AreRelocsRela = false;
114 template <class ELFT>
115 InputSectionBase(ObjectFile<ELFT> *File, const typename ELFT::Shdr *Header,
116 StringRef Name, Kind SectionKind);
118 InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type,
119 uint64_t Entsize, uint32_t Link, uint32_t Info,
120 uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name,
122 OutputSection *OutSec = nullptr;
124 // Relocations that refer to this section.
125 const void *FirstRelocation = nullptr;
126 unsigned NumRelocations : 31;
127 unsigned AreRelocsRela : 1;
128 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
129 assert(!AreRelocsRela);
130 return llvm::makeArrayRef(
131 static_cast<const typename ELFT::Rel *>(FirstRelocation),
134 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
135 assert(AreRelocsRela);
136 return llvm::makeArrayRef(
137 static_cast<const typename ELFT::Rela *>(FirstRelocation),
141 // This pointer points to the "real" instance of this instance.
142 // Usually Repl == this. However, if ICF merges two sections,
143 // Repl pointer of one section points to another section. So,
144 // if you need to get a pointer to this instance, do not use
145 // this but instead this->Repl.
146 InputSectionBase *Repl;
148 // InputSections that are dependent on us (reverse dependency for GC)
149 llvm::TinyPtrVector<InputSectionBase *> DependentSections;
151 // Returns the size of this section (even if this is a common or BSS.)
152 size_t getSize() const;
154 template <class ELFT> ObjectFile<ELFT> *getFile() const;
156 template <class ELFT> llvm::object::ELFFile<ELFT> getObj() const {
157 return getFile<ELFT>()->getObj();
160 InputSectionBase *getLinkOrderDep() const;
164 // Returns a source location string. Used to construct an error message.
165 template <class ELFT> std::string getLocation(uint64_t Offset);
166 template <class ELFT> std::string getSrcMsg(uint64_t Offset);
167 template <class ELFT> std::string getObjMsg(uint64_t Offset);
169 template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd);
170 void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd);
171 template <class ELFT> void relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd);
173 std::vector<Relocation> Relocations;
175 template <typename T> llvm::ArrayRef<T> getDataAs() const {
176 size_t S = Data.size();
177 assert(S % sizeof(T) == 0);
178 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
182 // SectionPiece represents a piece of splittable section contents.
183 // We allocate a lot of these and binary search on them. This means that they
184 // have to be as compact as possible, which is why we don't store the size (can
185 // be found by looking at the next one) and put the hash in a side table.
186 struct SectionPiece {
187 SectionPiece(size_t Off, bool Live = false)
188 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
191 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
194 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
195 "SectionPiece is too big");
197 // This corresponds to a SHF_MERGE section of an input file.
198 class MergeInputSection : public InputSectionBase {
200 template <class ELFT>
201 MergeInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
203 static bool classof(const SectionBase *S);
204 void splitIntoPieces();
206 // Mark the piece at a given offset live. Used by GC.
207 void markLiveAt(uint64_t Offset) {
208 assert(this->Flags & llvm::ELF::SHF_ALLOC);
209 LiveOffsets.insert(Offset);
212 // Translate an offset in the input section to an offset
213 // in the output section.
214 uint64_t getOffset(uint64_t Offset) const;
216 // Splittable sections are handled as a sequence of data
217 // rather than a single large blob of data.
218 std::vector<SectionPiece> Pieces;
220 // Returns I'th piece's data. This function is very hot when
221 // string merging is enabled, so we want to inline.
222 LLVM_ATTRIBUTE_ALWAYS_INLINE
223 llvm::CachedHashStringRef getData(size_t I) const {
224 size_t Begin = Pieces[I].InputOff;
226 if (Pieces.size() - 1 == I)
227 End = this->Data.size();
229 End = Pieces[I + 1].InputOff;
231 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
232 return {S, Hashes[I]};
235 // Returns the SectionPiece at a given input section offset.
236 SectionPiece *getSectionPiece(uint64_t Offset);
237 const SectionPiece *getSectionPiece(uint64_t Offset) const;
239 // MergeInputSections are aggregated to a synthetic input sections,
240 // and then added to an OutputSection. This pointer points to a
241 // synthetic MergeSyntheticSection which this section belongs to.
242 MergeSyntheticSection *MergeSec = nullptr;
245 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
246 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
248 std::vector<uint32_t> Hashes;
250 mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap;
251 mutable std::once_flag InitOffsetMap;
253 llvm::DenseSet<uint64_t> LiveOffsets;
256 struct EhSectionPiece : public SectionPiece {
257 EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size,
258 unsigned FirstRelocation)
259 : SectionPiece(Off, false), ID(ID), Size(Size),
260 FirstRelocation(FirstRelocation) {}
261 InputSectionBase *ID;
263 uint32_t size() const { return Size; }
265 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
266 unsigned FirstRelocation;
269 // This corresponds to a .eh_frame section of an input file.
270 class EhInputSection : public InputSectionBase {
272 template <class ELFT>
273 EhInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
275 static bool classof(const SectionBase *S);
276 template <class ELFT> void split();
277 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels);
279 // Splittable sections are handled as a sequence of data
280 // rather than a single large blob of data.
281 std::vector<EhSectionPiece> Pieces;
282 SyntheticSection *EHSec = nullptr;
285 // This is a section that is added directly to an output section
286 // instead of needing special combination via a synthetic section. This
287 // includes all input sections with the exceptions of SHF_MERGE and
288 // .eh_frame. It also includes the synthetic sections themselves.
289 class InputSection : public InputSectionBase {
291 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
292 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular);
293 template <class ELFT>
294 InputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
297 // Write this section to a mmap'ed file, assuming Buf is pointing to
298 // beginning of the output section.
299 template <class ELFT> void writeTo(uint8_t *Buf);
301 // The offset from beginning of the output sections this section was assigned
302 // to. The writer sets a value.
303 uint64_t OutSecOff = 0;
305 static bool classof(const SectionBase *S);
307 InputSectionBase *getRelocatedSection();
309 template <class ELFT, class RelTy>
310 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
313 uint32_t Class[2] = {0, 0};
315 // Called by ICF to merge two input sections.
316 void replace(InputSection *Other);
319 template <class ELFT, class RelTy>
320 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
323 // The list of all input sections.
324 extern std::vector<InputSectionBase *> InputSections;
328 std::string toString(const elf::InputSectionBase *);