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);
171 std::vector<Relocation> Relocations;
173 template <typename T> llvm::ArrayRef<T> getDataAs() const {
174 size_t S = Data.size();
175 assert(S % sizeof(T) == 0);
176 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
180 // SectionPiece represents a piece of splittable section contents.
181 // We allocate a lot of these and binary search on them. This means that they
182 // have to be as compact as possible, which is why we don't store the size (can
183 // be found by looking at the next one) and put the hash in a side table.
184 struct SectionPiece {
185 SectionPiece(size_t Off, bool Live = false)
186 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
189 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
192 static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
193 "SectionPiece is too big");
195 // This corresponds to a SHF_MERGE section of an input file.
196 class MergeInputSection : public InputSectionBase {
198 template <class ELFT>
199 MergeInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
201 static bool classof(const SectionBase *S);
202 void splitIntoPieces();
204 // Mark the piece at a given offset live. Used by GC.
205 void markLiveAt(uint64_t Offset) {
206 assert(this->Flags & llvm::ELF::SHF_ALLOC);
207 LiveOffsets.insert(Offset);
210 // Translate an offset in the input section to an offset
211 // in the output section.
212 uint64_t getOffset(uint64_t Offset) const;
214 // Splittable sections are handled as a sequence of data
215 // rather than a single large blob of data.
216 std::vector<SectionPiece> Pieces;
218 // Returns I'th piece's data. This function is very hot when
219 // string merging is enabled, so we want to inline.
220 LLVM_ATTRIBUTE_ALWAYS_INLINE
221 llvm::CachedHashStringRef getData(size_t I) const {
222 size_t Begin = Pieces[I].InputOff;
224 if (Pieces.size() - 1 == I)
225 End = this->Data.size();
227 End = Pieces[I + 1].InputOff;
229 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
230 return {S, Hashes[I]};
233 // Returns the SectionPiece at a given input section offset.
234 SectionPiece *getSectionPiece(uint64_t Offset);
235 const SectionPiece *getSectionPiece(uint64_t Offset) const;
237 // MergeInputSections are aggregated to a synthetic input sections,
238 // and then added to an OutputSection. This pointer points to a
239 // synthetic MergeSyntheticSection which this section belongs to.
240 MergeSyntheticSection *MergeSec = nullptr;
243 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
244 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
246 std::vector<uint32_t> Hashes;
248 mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap;
249 mutable std::once_flag InitOffsetMap;
251 llvm::DenseSet<uint64_t> LiveOffsets;
254 struct EhSectionPiece : public SectionPiece {
255 EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size,
256 unsigned FirstRelocation)
257 : SectionPiece(Off, false), ID(ID), Size(Size),
258 FirstRelocation(FirstRelocation) {}
259 InputSectionBase *ID;
261 uint32_t size() const { return Size; }
263 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
264 unsigned FirstRelocation;
267 // This corresponds to a .eh_frame section of an input file.
268 class EhInputSection : public InputSectionBase {
270 template <class ELFT>
271 EhInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
273 static bool classof(const SectionBase *S);
274 template <class ELFT> void split();
275 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels);
277 // Splittable sections are handled as a sequence of data
278 // rather than a single large blob of data.
279 std::vector<EhSectionPiece> Pieces;
280 SyntheticSection *EHSec = nullptr;
283 // This is a section that is added directly to an output section
284 // instead of needing special combination via a synthetic section. This
285 // includes all input sections with the exceptions of SHF_MERGE and
286 // .eh_frame. It also includes the synthetic sections themselves.
287 class InputSection : public InputSectionBase {
289 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
290 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular);
291 template <class ELFT>
292 InputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
295 // Write this section to a mmap'ed file, assuming Buf is pointing to
296 // beginning of the output section.
297 template <class ELFT> void writeTo(uint8_t *Buf);
299 // The offset from beginning of the output sections this section was assigned
300 // to. The writer sets a value.
301 uint64_t OutSecOff = 0;
303 static bool classof(const SectionBase *S);
305 InputSectionBase *getRelocatedSection();
307 template <class ELFT, class RelTy>
308 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
311 uint32_t Class[2] = {0, 0};
313 // Called by ICF to merge two input sections.
314 void replace(InputSection *Other);
317 template <class ELFT, class RelTy>
318 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
321 // The list of all input sections.
322 extern std::vector<InputSectionBase *> InputSections;
326 std::string toString(const elf::InputSectionBase *);