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"
21 #include "llvm/Support/Threading.h"
32 class SyntheticSection;
33 template <class ELFT> class EhFrameSection;
34 class MergeSyntheticSection;
35 template <class ELFT> class ObjectFile;
38 // This is the base class of all sections that lld handles. Some are sections in
39 // input files, some are sections in the produced output file and some exist
40 // just as a convenience for implementing special ways of combining some
44 enum Kind { Regular, EHFrame, Merge, Synthetic, Output };
46 Kind kind() const { return (Kind)SectionKind; }
50 unsigned SectionKind : 3;
52 // The next two bit fields are only used by InputSectionBase, but we
53 // put them here so the struct packs better.
55 // The garbage collector sets sections' Live bits.
56 // If GC is disabled, all sections are considered live by default.
57 unsigned Live : 1; // for garbage collection
58 unsigned Assigned : 1; // for linker script
62 // These corresponds to the fields in Elf_Shdr.
69 OutputSection *getOutputSection();
70 const OutputSection *getOutputSection() const {
71 return const_cast<SectionBase *>(this)->getOutputSection();
74 // Translate an offset in the input section to an offset in the output
76 uint64_t getOffset(uint64_t Offset) const;
78 uint64_t getOffset(const DefinedRegular &Sym) const;
81 SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags,
82 uint64_t Entsize, uint64_t Alignment, uint32_t Type,
83 uint32_t Info, uint32_t Link)
84 : Name(Name), SectionKind(SectionKind), Alignment(Alignment),
85 Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info) {
91 // This corresponds to a section of an input file.
92 class InputSectionBase : public SectionBase {
94 static bool classof(const SectionBase *S);
96 // The file this section is from.
99 ArrayRef<uint8_t> Data;
100 uint64_t getOffsetInFile() const;
102 static InputSectionBase Discarded;
105 : SectionBase(Regular, "", /*Flags*/ 0, /*Entsize*/ 0, /*Alignment*/ 0,
107 /*Info*/ 0, /*Link*/ 0),
112 AreRelocsRela = false;
115 template <class ELFT>
116 InputSectionBase(ObjectFile<ELFT> *File, const typename ELFT::Shdr *Header,
117 StringRef Name, Kind SectionKind);
119 InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type,
120 uint64_t Entsize, uint32_t Link, uint32_t Info,
121 uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name,
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;
130 // Relocations that refer to this section.
131 const void *FirstRelocation = nullptr;
132 unsigned NumRelocations : 31;
133 unsigned AreRelocsRela : 1;
134 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
135 assert(!AreRelocsRela);
136 return llvm::makeArrayRef(
137 static_cast<const typename ELFT::Rel *>(FirstRelocation),
140 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
141 assert(AreRelocsRela);
142 return llvm::makeArrayRef(
143 static_cast<const typename ELFT::Rela *>(FirstRelocation),
147 // This pointer points to the "real" instance of this instance.
148 // Usually Repl == this. However, if ICF merges two sections,
149 // Repl pointer of one section points to another section. So,
150 // if you need to get a pointer to this instance, do not use
151 // this but instead this->Repl.
152 InputSectionBase *Repl;
154 // InputSections that are dependent on us (reverse dependency for GC)
155 llvm::TinyPtrVector<InputSectionBase *> DependentSections;
157 // Returns the size of this section (even if this is a common or BSS.)
158 size_t getSize() const;
160 template <class ELFT> ObjectFile<ELFT> *getFile() const;
162 template <class ELFT> llvm::object::ELFFile<ELFT> getObj() const {
163 return getFile<ELFT>()->getObj();
166 InputSection *getLinkOrderDep() const;
170 // Returns a source location string. Used to construct an error message.
171 template <class ELFT> std::string getLocation(uint64_t Offset);
172 template <class ELFT> std::string getSrcMsg(uint64_t Offset);
173 template <class ELFT> std::string getObjMsg(uint64_t Offset);
175 template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd);
176 void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd);
177 template <class ELFT> void relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd);
179 std::vector<Relocation> Relocations;
181 template <typename T> llvm::ArrayRef<T> getDataAs() const {
182 size_t S = Data.size();
183 assert(S % sizeof(T) == 0);
184 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
188 // SectionPiece represents a piece of splittable section contents.
189 // We allocate a lot of these and binary search on them. This means that they
190 // have to be as compact as possible, which is why we don't store the size (can
191 // be found by looking at the next one) and put the hash in a side table.
192 struct SectionPiece {
193 SectionPiece(size_t Off, bool Live = false)
194 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
197 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
200 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
201 "SectionPiece is too big");
203 // This corresponds to a SHF_MERGE section of an input file.
204 class MergeInputSection : public InputSectionBase {
206 template <class ELFT>
207 MergeInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
209 static bool classof(const SectionBase *S);
210 void splitIntoPieces();
212 // Mark the piece at a given offset live. Used by GC.
213 void markLiveAt(uint64_t Offset) {
214 assert(this->Flags & llvm::ELF::SHF_ALLOC);
215 LiveOffsets.insert(Offset);
218 // Translate an offset in the input section to an offset
219 // in the output section.
220 uint64_t getOffset(uint64_t Offset) const;
222 // Splittable sections are handled as a sequence of data
223 // rather than a single large blob of data.
224 std::vector<SectionPiece> Pieces;
226 // Returns I'th piece's data. This function is very hot when
227 // string merging is enabled, so we want to inline.
228 LLVM_ATTRIBUTE_ALWAYS_INLINE
229 llvm::CachedHashStringRef getData(size_t I) const {
230 size_t Begin = Pieces[I].InputOff;
232 if (Pieces.size() - 1 == I)
233 End = this->Data.size();
235 End = Pieces[I + 1].InputOff;
237 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
238 return {S, Hashes[I]};
241 // Returns the SectionPiece at a given input section offset.
242 SectionPiece *getSectionPiece(uint64_t Offset);
243 const SectionPiece *getSectionPiece(uint64_t Offset) const;
245 SyntheticSection *getParent() const;
248 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
249 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
251 std::vector<uint32_t> Hashes;
253 mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap;
254 mutable llvm::once_flag InitOffsetMap;
256 llvm::DenseSet<uint64_t> LiveOffsets;
259 struct EhSectionPiece : public SectionPiece {
260 EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size,
261 unsigned FirstRelocation)
262 : SectionPiece(Off, false), ID(ID), Size(Size),
263 FirstRelocation(FirstRelocation) {}
264 InputSectionBase *ID;
266 uint32_t size() const { return Size; }
268 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
269 unsigned FirstRelocation;
272 // This corresponds to a .eh_frame section of an input file.
273 class EhInputSection : public InputSectionBase {
275 template <class ELFT>
276 EhInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
278 static bool classof(const SectionBase *S);
279 template <class ELFT> void split();
280 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels);
282 // Splittable sections are handled as a sequence of data
283 // rather than a single large blob of data.
284 std::vector<EhSectionPiece> Pieces;
286 SyntheticSection *getParent() const;
289 // This is a section that is added directly to an output section
290 // instead of needing special combination via a synthetic section. This
291 // includes all input sections with the exceptions of SHF_MERGE and
292 // .eh_frame. It also includes the synthetic sections themselves.
293 class InputSection : public InputSectionBase {
295 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
296 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular);
297 template <class ELFT>
298 InputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
301 // Write this section to a mmap'ed file, assuming Buf is pointing to
302 // beginning of the output section.
303 template <class ELFT> void writeTo(uint8_t *Buf);
305 OutputSection *getParent() const;
307 // The offset from beginning of the output sections this section was assigned
308 // to. The writer sets a value.
309 uint64_t OutSecOff = 0;
311 static bool classof(const SectionBase *S);
313 InputSectionBase *getRelocatedSection();
315 template <class ELFT, class RelTy>
316 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
319 uint32_t Class[2] = {0, 0};
321 // Called by ICF to merge two input sections.
322 void replace(InputSection *Other);
325 template <class ELFT, class RelTy>
326 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
328 template <class ELFT> void copyShtGroup(uint8_t *Buf);
331 // The list of all input sections.
332 extern std::vector<InputSectionBase *> InputSections;
336 std::string toString(const elf::InputSectionBase *);