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,
123 OutputSection *OutSec = nullptr;
125 // Relocations that refer to this section.
126 const void *FirstRelocation = nullptr;
127 unsigned NumRelocations : 31;
128 unsigned AreRelocsRela : 1;
129 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
130 assert(!AreRelocsRela);
131 return llvm::makeArrayRef(
132 static_cast<const typename ELFT::Rel *>(FirstRelocation),
135 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
136 assert(AreRelocsRela);
137 return llvm::makeArrayRef(
138 static_cast<const typename ELFT::Rela *>(FirstRelocation),
142 // This pointer points to the "real" instance of this instance.
143 // Usually Repl == this. However, if ICF merges two sections,
144 // Repl pointer of one section points to another section. So,
145 // if you need to get a pointer to this instance, do not use
146 // this but instead this->Repl.
147 InputSectionBase *Repl;
149 // InputSections that are dependent on us (reverse dependency for GC)
150 llvm::TinyPtrVector<InputSectionBase *> DependentSections;
152 // Returns the size of this section (even if this is a common or BSS.)
153 size_t getSize() const;
155 template <class ELFT> ObjectFile<ELFT> *getFile() const;
157 template <class ELFT> llvm::object::ELFFile<ELFT> getObj() const {
158 return getFile<ELFT>()->getObj();
161 InputSectionBase *getLinkOrderDep() const;
165 // Returns a source location string. Used to construct an error message.
166 template <class ELFT> std::string getLocation(uint64_t Offset);
167 template <class ELFT> std::string getSrcMsg(uint64_t Offset);
168 template <class ELFT> std::string getObjMsg(uint64_t Offset);
170 template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd);
171 void relocateAlloc(uint8_t *Buf, uint8_t *BufEnd);
172 template <class ELFT> void relocateNonAlloc(uint8_t *Buf, uint8_t *BufEnd);
174 std::vector<Relocation> Relocations;
176 template <typename T> llvm::ArrayRef<T> getDataAs() const {
177 size_t S = Data.size();
178 assert(S % sizeof(T) == 0);
179 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
183 // SectionPiece represents a piece of splittable section contents.
184 // We allocate a lot of these and binary search on them. This means that they
185 // have to be as compact as possible, which is why we don't store the size (can
186 // be found by looking at the next one) and put the hash in a side table.
187 struct SectionPiece {
188 SectionPiece(size_t Off, bool Live = false)
189 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
192 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
195 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
196 "SectionPiece is too big");
198 // This corresponds to a SHF_MERGE section of an input file.
199 class MergeInputSection : public InputSectionBase {
201 template <class ELFT>
202 MergeInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
204 static bool classof(const SectionBase *S);
205 void splitIntoPieces();
207 // Mark the piece at a given offset live. Used by GC.
208 void markLiveAt(uint64_t Offset) {
209 assert(this->Flags & llvm::ELF::SHF_ALLOC);
210 LiveOffsets.insert(Offset);
213 // Translate an offset in the input section to an offset
214 // in the output section.
215 uint64_t getOffset(uint64_t Offset) const;
217 // Splittable sections are handled as a sequence of data
218 // rather than a single large blob of data.
219 std::vector<SectionPiece> Pieces;
221 // Returns I'th piece's data. This function is very hot when
222 // string merging is enabled, so we want to inline.
223 LLVM_ATTRIBUTE_ALWAYS_INLINE
224 llvm::CachedHashStringRef getData(size_t I) const {
225 size_t Begin = Pieces[I].InputOff;
227 if (Pieces.size() - 1 == I)
228 End = this->Data.size();
230 End = Pieces[I + 1].InputOff;
232 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
233 return {S, Hashes[I]};
236 // Returns the SectionPiece at a given input section offset.
237 SectionPiece *getSectionPiece(uint64_t Offset);
238 const SectionPiece *getSectionPiece(uint64_t Offset) const;
240 // MergeInputSections are aggregated to a synthetic input sections,
241 // and then added to an OutputSection. This pointer points to a
242 // synthetic MergeSyntheticSection which this section belongs to.
243 MergeSyntheticSection *MergeSec = nullptr;
246 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
247 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
249 std::vector<uint32_t> Hashes;
251 mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap;
252 mutable llvm::once_flag InitOffsetMap;
254 llvm::DenseSet<uint64_t> LiveOffsets;
257 struct EhSectionPiece : public SectionPiece {
258 EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size,
259 unsigned FirstRelocation)
260 : SectionPiece(Off, false), ID(ID), Size(Size),
261 FirstRelocation(FirstRelocation) {}
262 InputSectionBase *ID;
264 uint32_t size() const { return Size; }
266 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
267 unsigned FirstRelocation;
270 // This corresponds to a .eh_frame section of an input file.
271 class EhInputSection : public InputSectionBase {
273 template <class ELFT>
274 EhInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
276 static bool classof(const SectionBase *S);
277 template <class ELFT> void split();
278 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels);
280 // Splittable sections are handled as a sequence of data
281 // rather than a single large blob of data.
282 std::vector<EhSectionPiece> Pieces;
283 SyntheticSection *EHSec = nullptr;
286 // This is a section that is added directly to an output section
287 // instead of needing special combination via a synthetic section. This
288 // includes all input sections with the exceptions of SHF_MERGE and
289 // .eh_frame. It also includes the synthetic sections themselves.
290 class InputSection : public InputSectionBase {
292 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
293 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular);
294 template <class ELFT>
295 InputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
298 // Write this section to a mmap'ed file, assuming Buf is pointing to
299 // beginning of the output section.
300 template <class ELFT> void writeTo(uint8_t *Buf);
302 // The offset from beginning of the output sections this section was assigned
303 // to. The writer sets a value.
304 uint64_t OutSecOff = 0;
306 static bool classof(const SectionBase *S);
308 InputSectionBase *getRelocatedSection();
310 template <class ELFT, class RelTy>
311 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
314 uint32_t Class[2] = {0, 0};
316 // Called by ICF to merge two input sections.
317 void replace(InputSection *Other);
320 template <class ELFT, class RelTy>
321 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
323 void copyShtGroup(uint8_t *Buf);
326 // The list of all input sections.
327 extern std::vector<InputSectionBase *> InputSections;
331 std::string toString(const elf::InputSectionBase *);