1 //===- InputSection.h -------------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_ELF_INPUT_SECTION_H
10 #define LLD_ELF_INPUT_SECTION_H
13 #include "Relocations.h"
15 #include "lld/Common/LLVM.h"
16 #include "llvm/ADT/CachedHashString.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/TinyPtrVector.h"
19 #include "llvm/Object/ELF.h"
29 class SyntheticSection;
30 class MergeSyntheticSection;
31 template <class ELFT> class ObjFile;
34 extern std::vector<Partition> partitions;
36 // This is the base class of all sections that lld handles. Some are sections in
37 // input files, some are sections in the produced output file and some exist
38 // just as a convenience for implementing special ways of combining some
42 enum Kind { Regular, EHFrame, Merge, Synthetic, Output };
44 Kind kind() const { return (Kind)sectionKind; }
48 // This pointer points to the "real" instance of this instance.
49 // Usually Repl == this. However, if ICF merges two sections,
50 // Repl pointer of one section points to another section. So,
51 // if you need to get a pointer to this instance, do not use
52 // this but instead this->Repl.
55 unsigned sectionKind : 3;
57 // The next two bit fields are only used by InputSectionBase, but we
58 // put them here so the struct packs better.
62 // Set for sections that should not be folded by ICF.
63 unsigned keepUnique : 1;
65 // The 1-indexed partition that this section is assigned to by the garbage
66 // collector, or 0 if this section is dead. Normally there is only one
67 // partition, so this will either be 0 or 1.
69 elf::Partition &getPartition() const;
71 // These corresponds to the fields in Elf_Shdr.
79 OutputSection *getOutputSection();
80 const OutputSection *getOutputSection() const {
81 return const_cast<SectionBase *>(this)->getOutputSection();
84 // Translate an offset in the input section to an offset in the output
86 uint64_t getOffset(uint64_t offset) const;
88 uint64_t getVA(uint64_t offset = 0) const;
90 bool isLive() const { return partition != 0; }
91 void markLive() { partition = 1; }
92 void markDead() { partition = 0; }
95 SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
96 uint64_t entsize, uint64_t alignment, uint32_t type,
97 uint32_t info, uint32_t link)
98 : name(name), repl(this), sectionKind(sectionKind), bss(false),
99 keepUnique(false), partition(0), alignment(alignment), flags(flags),
100 entsize(entsize), type(type), link(link), info(info) {}
103 // This corresponds to a section of an input file.
104 class InputSectionBase : public SectionBase {
106 template <class ELFT>
107 InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
108 StringRef name, Kind sectionKind);
110 InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
111 uint64_t entsize, uint32_t link, uint32_t info,
112 uint32_t alignment, ArrayRef<uint8_t> data, StringRef name,
115 static bool classof(const SectionBase *s) { return s->kind() != Output; }
117 // Relocations that refer to this section.
118 unsigned numRelocations : 31;
119 unsigned areRelocsRela : 1;
120 const void *firstRelocation = nullptr;
122 // The file which contains this section. Its dynamic type is always
123 // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
127 template <class ELFT> ObjFile<ELFT> *getFile() const {
128 return cast_or_null<ObjFile<ELFT>>(file);
131 ArrayRef<uint8_t> data() const {
132 if (uncompressedSize >= 0)
137 uint64_t getOffsetInFile() const;
139 // Input sections are part of an output section. Special sections
140 // like .eh_frame and merge sections are first combined into a
141 // synthetic section that is then added to an output section. In all
142 // cases this points one level up.
143 SectionBase *parent = nullptr;
145 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
146 assert(!areRelocsRela);
147 return llvm::makeArrayRef(
148 static_cast<const typename ELFT::Rel *>(firstRelocation),
152 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
153 assert(areRelocsRela);
154 return llvm::makeArrayRef(
155 static_cast<const typename ELFT::Rela *>(firstRelocation),
159 // InputSections that are dependent on us (reverse dependency for GC)
160 llvm::TinyPtrVector<InputSection *> dependentSections;
162 // Returns the size of this section (even if this is a common or BSS.)
163 size_t getSize() const;
165 InputSection *getLinkOrderDep() const;
167 // Get the function symbol that encloses this offset from within the
169 template <class ELFT>
170 Defined *getEnclosingFunction(uint64_t offset);
172 // Returns a source location string. Used to construct an error message.
173 template <class ELFT> std::string getLocation(uint64_t offset);
174 std::string getSrcMsg(const Symbol &sym, uint64_t offset);
175 std::string getObjMsg(uint64_t offset);
177 // Each section knows how to relocate itself. These functions apply
178 // relocations, assuming that Buf points to this section's copy in
179 // the mmap'ed output buffer.
180 template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
181 void relocateAlloc(uint8_t *buf, uint8_t *bufEnd);
183 // The native ELF reloc data type is not very convenient to handle.
184 // So we convert ELF reloc records to our own records in Relocations.cpp.
185 // This vector contains such "cooked" relocations.
186 std::vector<Relocation> relocations;
188 // A function compiled with -fsplit-stack calling a function
189 // compiled without -fsplit-stack needs its prologue adjusted. Find
190 // such functions and adjust their prologues. This is very similar
191 // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
193 template <typename ELFT>
194 void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
197 template <typename T> llvm::ArrayRef<T> getDataAs() const {
198 size_t s = data().size();
199 assert(s % sizeof(T) == 0);
200 return llvm::makeArrayRef<T>((const T *)data().data(), s / sizeof(T));
204 void parseCompressedHeader();
205 void uncompress() const;
207 mutable ArrayRef<uint8_t> rawData;
209 // This field stores the uncompressed size of the compressed data in rawData,
210 // or -1 if rawData is not compressed (either because the section wasn't
211 // compressed in the first place, or because we ended up uncompressing it).
212 // Since the feature is not used often, this is usually -1.
213 mutable int64_t uncompressedSize = -1;
216 // SectionPiece represents a piece of splittable section contents.
217 // We allocate a lot of these and binary search on them. This means that they
218 // have to be as compact as possible, which is why we don't store the size (can
219 // be found by looking at the next one).
220 struct SectionPiece {
221 SectionPiece(size_t off, uint32_t hash, bool live)
222 : inputOff(off), live(live || !config->gcSections), hash(hash >> 1) {}
227 uint64_t outputOff = 0;
230 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
232 // This corresponds to a SHF_MERGE section of an input file.
233 class MergeInputSection : public InputSectionBase {
235 template <class ELFT>
236 MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
238 MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize,
239 ArrayRef<uint8_t> data, StringRef name);
241 static bool classof(const SectionBase *s) { return s->kind() == Merge; }
242 void splitIntoPieces();
244 // Translate an offset in the input section to an offset in the parent
245 // MergeSyntheticSection.
246 uint64_t getParentOffset(uint64_t offset) const;
248 // Splittable sections are handled as a sequence of data
249 // rather than a single large blob of data.
250 std::vector<SectionPiece> pieces;
252 // Returns I'th piece's data. This function is very hot when
253 // string merging is enabled, so we want to inline.
254 LLVM_ATTRIBUTE_ALWAYS_INLINE
255 llvm::CachedHashStringRef getData(size_t i) const {
256 size_t begin = pieces[i].inputOff;
258 (pieces.size() - 1 == i) ? data().size() : pieces[i + 1].inputOff;
259 return {toStringRef(data().slice(begin, end - begin)), pieces[i].hash};
262 // Returns the SectionPiece at a given input section offset.
263 SectionPiece *getSectionPiece(uint64_t offset);
264 const SectionPiece *getSectionPiece(uint64_t offset) const {
265 return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
268 SyntheticSection *getParent() const;
271 void splitStrings(ArrayRef<uint8_t> a, size_t size);
272 void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
275 struct EhSectionPiece {
276 EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
277 unsigned firstRelocation)
278 : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
280 ArrayRef<uint8_t> data() {
281 return {sec->data().data() + this->inputOff, size};
285 ssize_t outputOff = -1;
286 InputSectionBase *sec;
288 unsigned firstRelocation;
291 // This corresponds to a .eh_frame section of an input file.
292 class EhInputSection : public InputSectionBase {
294 template <class ELFT>
295 EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
297 static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
298 template <class ELFT> void split();
299 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
301 // Splittable sections are handled as a sequence of data
302 // rather than a single large blob of data.
303 std::vector<EhSectionPiece> pieces;
305 SyntheticSection *getParent() const;
308 // This is a section that is added directly to an output section
309 // instead of needing special combination via a synthetic section. This
310 // includes all input sections with the exceptions of SHF_MERGE and
311 // .eh_frame. It also includes the synthetic sections themselves.
312 class InputSection : public InputSectionBase {
314 InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t alignment,
315 ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
316 template <class ELFT>
317 InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
320 // Write this section to a mmap'ed file, assuming Buf is pointing to
321 // beginning of the output section.
322 template <class ELFT> void writeTo(uint8_t *buf);
324 uint64_t getOffset(uint64_t offset) const { return outSecOff + offset; }
326 OutputSection *getParent() const;
328 // This variable has two usages. Initially, it represents an index in the
329 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
330 // sections. After assignAddresses is called, it represents the offset from
331 // the beginning of the output section this section was assigned to.
332 uint64_t outSecOff = 0;
334 static bool classof(const SectionBase *s);
336 InputSectionBase *getRelocatedSection() const;
338 template <class ELFT, class RelTy>
339 void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
342 uint32_t eqClass[2] = {0, 0};
344 // Called by ICF to merge two input sections.
345 void replace(InputSection *other);
347 static InputSection discarded;
350 template <class ELFT, class RelTy>
351 void copyRelocations(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
353 template <class ELFT> void copyShtGroup(uint8_t *buf);
356 // The list of all input sections.
357 extern std::vector<InputSectionBase *> inputSections;
361 std::string toString(const elf::InputSectionBase *);