1//===- InputSection.h -------------------------------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLD_ELF_INPUT_SECTION_H
10#define LLD_ELF_INPUT_SECTION_H
11
12#include "Config.h"
13#include "Relocations.h"
14#include "lld/Common/CommonLinkerContext.h"
15#include "lld/Common/LLVM.h"
16#include "lld/Common/Memory.h"
17#include "llvm/ADT/CachedHashString.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/ADT/TinyPtrVector.h"
21#include "llvm/Object/ELF.h"
22#include "llvm/Support/Compiler.h"
23
24namespace lld {
25namespace elf {
26
27class InputFile;
28class Symbol;
29
30class Defined;
31struct Partition;
32class SyntheticSection;
33template <class ELFT> class ObjFile;
34class OutputSection;
35
36// Returned by InputSectionBase::relsOrRelas. At least two members are empty.
37template <class ELFT> struct RelsOrRelas {
38 Relocs<typename ELFT::Rel> rels;
39 Relocs<typename ELFT::Rela> relas;
40 Relocs<typename ELFT::Crel> crels;
41 bool areRelocsRel() const { return rels.size(); }
42 bool areRelocsCrel() const { return crels.size(); }
43};
44
45#define invokeOnRelocs(sec, f, ...) \
46 { \
47 const RelsOrRelas<ELFT> rs = (sec).template relsOrRelas<ELFT>(); \
48 if (rs.areRelocsCrel()) \
49 f(__VA_ARGS__, rs.crels); \
50 else if (rs.areRelocsRel()) \
51 f(__VA_ARGS__, rs.rels); \
52 else \
53 f(__VA_ARGS__, rs.relas); \
54 }
55
56// This is the base class of all sections that lld handles. Some are sections in
57// input files, some are sections in the produced output file and some exist
58// just as a convenience for implementing special ways of combining some
59// sections.
60class SectionBase {
61public:
62 enum Kind : uint8_t {
63 Regular,
64 Synthetic,
65 Spill,
66 EHFrame,
67 Merge,
68 Output,
69 Class,
70 };
71
72 Kind kind() const { return sectionKind; }
73
74 // The file which contains this section. For InputSectionBase, its dynamic
75 // type is usually ObjFile<ELFT>, but may be an InputFile of InternalKind
76 // (for a synthetic section).
77 InputFile *file;
78
79 StringRef name;
80
81 // The 1-indexed partition that this section is assigned to by the garbage
82 // collector, or 0 if this section is dead. Normally there is only one
83 // partition, so this will either be 0 or 1.
84 elf::Partition &getPartition(Ctx &) const;
85
86 // These corresponds to the fields in Elf_Shdr.
87 uint64_t flags;
88 uint32_t type;
89 uint32_t link;
90 uint32_t info;
91 uint32_t addralign;
92 uint32_t entsize;
93
94 Kind sectionKind;
95 uint8_t partition = 1;
96
97 // The next two bit fields are only used by InputSectionBase, but we
98 // put them here so the struct packs better.
99
100 Ctx &getCtx() const;
101 OutputSection *getOutputSection();
102 const OutputSection *getOutputSection() const {
103 return const_cast<SectionBase *>(this)->getOutputSection();
104 }
105
106 // Translate an offset in the input section to an offset in the output
107 // section.
108 uint64_t getOffset(uint64_t offset) const;
109
110 uint64_t getVA(uint64_t offset = 0) const;
111
112 bool isLive() const { return partition != 0; }
113 void markLive() { partition = 1; }
114 void markDead() { partition = 0; }
115
116protected:
117 constexpr SectionBase(Kind sectionKind, InputFile *file, StringRef name,
118 uint32_t type, uint64_t flags, uint32_t link,
119 uint32_t info, uint32_t addralign, uint32_t entsize)
120 : file(file), name(name), flags(flags), type(type), link(link),
121 info(info), addralign(addralign), entsize(entsize),
122 sectionKind(sectionKind) {}
123};
124
125struct SymbolAnchor {
126 uint64_t offset;
127 Defined *d;
128 bool end; // true for the anchor of st_value+st_size
129};
130
131struct RelaxAux {
132 // This records symbol start and end offsets which will be adjusted according
133 // to the nearest relocDeltas element.
134 SmallVector<SymbolAnchor, 0> anchors;
135 // For relocations[i], the actual offset is
136 // r_offset - (i ? relocDeltas[i-1] : 0).
137 std::unique_ptr<uint32_t[]> relocDeltas;
138 // For relocations[i], the actual type is relocTypes[i].
139 std::unique_ptr<RelType[]> relocTypes;
140 SmallVector<uint32_t, 0> writes;
141};
142
143// This corresponds to a section of an input file.
144class InputSectionBase : public SectionBase {
145public:
146 struct ObjMsg {
147 const InputSectionBase *sec;
148 uint64_t offset;
149 };
150 struct SrcMsg {
151 const InputSectionBase &sec;
152 const Symbol &sym;
153 uint64_t offset;
154 };
155
156 template <class ELFT>
157 InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
158 StringRef name, Kind sectionKind);
159
160 InputSectionBase(InputFile *file, StringRef name, uint32_t type,
161 uint64_t flags, uint32_t link, uint32_t info,
162 uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
163 Kind sectionKind);
164
165 static bool classof(const SectionBase *s) {
166 return s->kind() != Output && s->kind() != Class;
167 }
168
169 LLVM_PREFERRED_TYPE(bool)
170 uint8_t bss : 1;
171
172 // Whether this section is SHT_CREL and has been decoded to RELA by
173 // relsOrRelas.
174 LLVM_PREFERRED_TYPE(bool)
175 uint8_t decodedCrel : 1;
176
177 // Set for sections that should not be folded by ICF.
178 LLVM_PREFERRED_TYPE(bool)
179 uint8_t keepUnique : 1;
180
181 // Whether the section needs to be padded with a NOP filler due to
182 // deleteFallThruJmpInsn.
183 LLVM_PREFERRED_TYPE(bool)
184 uint8_t nopFiller : 1;
185
186 mutable bool compressed = false;
187
188 // Input sections are part of an output section. Special sections
189 // like .eh_frame and merge sections are first combined into a
190 // synthetic section that is then added to an output section. In all
191 // cases this points one level up.
192 SectionBase *parent = nullptr;
193
194 // Section index of the relocation section if exists.
195 uint32_t relSecIdx = 0;
196
197 // Getter when the dynamic type is ObjFile<ELFT>.
198 template <class ELFT> ObjFile<ELFT> *getFile() const {
199 return cast<ObjFile<ELFT>>(file);
200 }
201
202 // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
203 // indicate the number of bytes which is not counted in the size. This should
204 // be reset to zero after uses.
205 uint32_t bytesDropped = 0;
206
207 void drop_back(unsigned num) {
208 assert(bytesDropped + num < 256);
209 bytesDropped += num;
210 }
211
212 void push_back(uint64_t num) {
213 assert(bytesDropped >= num);
214 bytesDropped -= num;
215 }
216
217 mutable const uint8_t *content_;
218 uint64_t size;
219
220 void trim() {
221 if (bytesDropped) {
222 size -= bytesDropped;
223 bytesDropped = 0;
224 }
225 }
226
227 ArrayRef<uint8_t> content() const {
228 return ArrayRef<uint8_t>(content_, size);
229 }
230 ArrayRef<uint8_t> contentMaybeDecompress() const {
231 if (compressed)
232 decompress();
233 return content();
234 }
235
236 // The next member in the section group if this section is in a group. This is
237 // used by --gc-sections.
238 InputSectionBase *nextInSectionGroup = nullptr;
239
240 template <class ELFT>
241 RelsOrRelas<ELFT> relsOrRelas(bool supportsCrel = true) const;
242
243 // InputSections that are dependent on us (reverse dependency for GC)
244 llvm::TinyPtrVector<InputSection *> dependentSections;
245
246 // Returns the size of this section (even if this is a common or BSS.)
247 size_t getSize() const;
248
249 InputSection *getLinkOrderDep() const;
250
251 // Get a symbol that encloses this offset from within the section. If type is
252 // not zero, return a symbol with the specified type.
253 Defined *getEnclosingSymbol(uint64_t offset, uint8_t type = 0) const;
254 Defined *getEnclosingFunction(uint64_t offset) const {
255 return getEnclosingSymbol(offset, type: llvm::ELF::STT_FUNC);
256 }
257
258 // Returns a source location string. Used to construct an error message.
259 std::string getLocation(uint64_t offset) const;
260 ObjMsg getObjMsg(uint64_t offset) const { return {.sec: this, .offset: offset}; }
261 SrcMsg getSrcMsg(const Symbol &sym, uint64_t offset) const {
262 return {.sec: *this, .sym: sym, .offset: offset};
263 }
264
265 uint64_t getRelocTargetVA(Ctx &, const Relocation &r, uint64_t p) const;
266
267 // The native ELF reloc data type is not very convenient to handle.
268 // So we convert ELF reloc records to our own records in Relocations.cpp.
269 // This vector contains such "cooked" relocations.
270 SmallVector<Relocation, 0> relocations;
271
272 void addReloc(const Relocation &r) { relocations.push_back(Elt: r); }
273 MutableArrayRef<Relocation> relocs() { return relocations; }
274 ArrayRef<Relocation> relocs() const { return relocations; }
275
276 union {
277 // These are modifiers to jump instructions that are necessary when basic
278 // block sections are enabled. Basic block sections creates opportunities
279 // to relax jump instructions at basic block boundaries after reordering the
280 // basic blocks.
281 JumpInstrMod *jumpInstrMod = nullptr;
282
283 // Auxiliary information for RISC-V and LoongArch linker relaxation.
284 // They do not use jumpInstrMod.
285 RelaxAux *relaxAux;
286
287 // The compressed content size when `compressed` is true.
288 size_t compressedSize;
289 };
290
291 // A function compiled with -fsplit-stack calling a function
292 // compiled without -fsplit-stack needs its prologue adjusted. Find
293 // such functions and adjust their prologues. This is very similar
294 // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
295 // information.
296 template <typename ELFT>
297 void adjustSplitStackFunctionPrologues(Ctx &, uint8_t *buf, uint8_t *end);
298
299 template <typename T> llvm::ArrayRef<T> getDataAs() const {
300 size_t s = content().size();
301 assert(s % sizeof(T) == 0);
302 return llvm::ArrayRef<T>((const T *)content().data(), s / sizeof(T));
303 }
304
305protected:
306 template <typename ELFT> void parseCompressedHeader(Ctx &);
307 void decompress() const;
308};
309
310// SectionPiece represents a piece of splittable section contents.
311// We allocate a lot of these and binary search on them. This means that they
312// have to be as compact as possible, which is why we don't store the size (can
313// be found by looking at the next one).
314struct SectionPiece {
315 SectionPiece() = default;
316 SectionPiece(size_t off, uint32_t hash, bool live)
317 : inputOff(off), live(live), hash(hash >> 1) {}
318
319 uint32_t inputOff;
320 LLVM_PREFERRED_TYPE(bool)
321 uint32_t live : 1;
322 uint32_t hash : 31;
323 uint64_t outputOff = 0;
324};
325
326static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
327
328// This corresponds to a SHF_MERGE section of an input file.
329class MergeInputSection : public InputSectionBase {
330public:
331 template <class ELFT>
332 MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
333 StringRef name);
334 MergeInputSection(Ctx &, StringRef name, uint32_t type, uint64_t flags,
335 uint64_t entsize, ArrayRef<uint8_t> data);
336
337 static bool classof(const SectionBase *s) { return s->kind() == Merge; }
338 void splitIntoPieces();
339
340 // Translate an offset in the input section to an offset in the parent
341 // MergeSyntheticSection.
342 uint64_t getParentOffset(uint64_t offset) const;
343
344 // Splittable sections are handled as a sequence of data
345 // rather than a single large blob of data.
346 SmallVector<SectionPiece, 0> pieces;
347
348 // Returns I'th piece's data. This function is very hot when
349 // string merging is enabled, so we want to inline.
350 LLVM_ATTRIBUTE_ALWAYS_INLINE
351 llvm::CachedHashStringRef getData(size_t i) const {
352 size_t begin = pieces[i].inputOff;
353 size_t end =
354 (pieces.size() - 1 == i) ? content().size() : pieces[i + 1].inputOff;
355 return {toStringRef(Input: content().slice(N: begin, M: end - begin)), pieces[i].hash};
356 }
357
358 // Returns the SectionPiece at a given input section offset.
359 SectionPiece &getSectionPiece(uint64_t offset);
360 const SectionPiece &getSectionPiece(uint64_t offset) const {
361 return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
362 }
363
364 SyntheticSection *getParent() const {
365 return cast_or_null<SyntheticSection>(Val: parent);
366 }
367
368private:
369 void splitStrings(StringRef s, size_t size);
370 void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
371};
372
373struct EhSectionPiece {
374 EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
375 unsigned firstRelocation)
376 : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
377
378 ArrayRef<uint8_t> data() const {
379 return {sec->content().data() + this->inputOff, size};
380 }
381
382 size_t inputOff;
383 ssize_t outputOff = -1;
384 InputSectionBase *sec;
385 uint32_t size;
386 unsigned firstRelocation;
387};
388
389// This corresponds to a .eh_frame section of an input file.
390class EhInputSection : public InputSectionBase {
391public:
392 template <class ELFT>
393 EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
394 StringRef name);
395 static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
396 template <class ELFT> void split();
397 template <class ELFT, class RelTy> void preprocessRelocs(Relocs<RelTy> rels);
398
399 // Splittable sections are handled as a sequence of data
400 // rather than a single large blob of data.
401 SmallVector<EhSectionPiece, 0> cies, fdes;
402
403 SyntheticSection *getParent() const;
404 uint64_t getParentOffset(uint64_t offset) const;
405
406 // Preprocessed relocations in uniform format to avoid REL/RELA/CREL
407 // relocation format handling throughout the codebase.
408 SmallVector<Relocation, 0> rels;
409};
410
411// This is a section that is added directly to an output section
412// instead of needing special combination via a synthetic section. This
413// includes all input sections with the exceptions of SHF_MERGE and
414// .eh_frame. It also includes the synthetic sections themselves.
415class InputSection : public InputSectionBase {
416public:
417 InputSection(InputFile *f, StringRef name, uint32_t type, uint64_t flags,
418 uint32_t addralign, uint32_t entsize, ArrayRef<uint8_t> data,
419 Kind k = Regular);
420 template <class ELFT>
421 InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
422 StringRef name);
423
424 static bool classof(const SectionBase *s) {
425 return s->kind() == SectionBase::Regular ||
426 s->kind() == SectionBase::Synthetic ||
427 s->kind() == SectionBase::Spill;
428 }
429
430 // Write this section to a mmap'ed file, assuming Buf is pointing to
431 // beginning of the output section.
432 template <class ELFT> void writeTo(Ctx &, uint8_t *buf);
433
434 OutputSection *getParent() const {
435 return reinterpret_cast<OutputSection *>(parent);
436 }
437
438 // This variable has two usages. Initially, it represents an index in the
439 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
440 // sections. After assignAddresses is called, it represents the offset from
441 // the beginning of the output section this section was assigned to.
442 uint64_t outSecOff = 0;
443
444 InputSectionBase *getRelocatedSection() const;
445
446 // Each section knows how to relocate itself. These functions apply
447 // relocations, assuming that `buf` points to this section's copy in
448 // the mmap'ed output buffer.
449 template <class ELFT, class RelTy>
450 void relocateNonAlloc(Ctx &, uint8_t *buf, Relocs<RelTy> rels);
451 template <class ELFT> void relocate(Ctx &, uint8_t *buf, uint8_t *bufEnd);
452
453 // Points to the canonical section. If ICF folds two sections, repl pointer of
454 // one section points to the other.
455 InputSection *repl = this;
456
457 // Used by ICF.
458 uint32_t eqClass[2] = {0, 0};
459
460 // Called by ICF to merge two input sections.
461 void replace(InputSection *other);
462
463 static InputSection discarded;
464
465private:
466 template <class ELFT, class RelTy> void copyRelocations(Ctx &, uint8_t *buf);
467
468 template <class ELFT, class RelTy, class RelIt>
469 void copyRelocations(Ctx &, uint8_t *buf, llvm::iterator_range<RelIt> rels);
470
471 template <class ELFT> void copyShtGroup(uint8_t *buf);
472};
473
474// A marker for a potential spill location for another input section. This
475// broadly acts as if it were the original section until address assignment.
476// Then it is either replaced with the real input section or removed.
477class PotentialSpillSection : public InputSection {
478public:
479 // The containing input section description; used to quickly replace this stub
480 // with the actual section.
481 InputSectionDescription *isd;
482
483 // Next potential spill location for the same source input section.
484 PotentialSpillSection *next = nullptr;
485
486 PotentialSpillSection(const InputSectionBase &source,
487 InputSectionDescription &isd);
488
489 static bool classof(const SectionBase *sec) {
490 return sec->kind() == InputSectionBase::Spill;
491 }
492};
493
494#ifndef _WIN32
495static_assert(sizeof(InputSection) <= 152, "InputSection is too big");
496#endif
497
498class SyntheticSection : public InputSection {
499public:
500 Ctx &ctx;
501 SyntheticSection(Ctx &ctx, StringRef name, uint32_t type, uint64_t flags,
502 uint32_t addralign)
503 : InputSection(ctx.internalFile, name, type, flags, addralign,
504 /*entsize=*/0, {}, InputSectionBase::Synthetic),
505 ctx(ctx) {}
506
507 virtual ~SyntheticSection() = default;
508 virtual size_t getSize() const = 0;
509 virtual bool updateAllocSize(Ctx &) { return false; }
510 // If the section has the SHF_ALLOC flag and the size may be changed if
511 // thunks are added, update the section size.
512 virtual bool isNeeded() const { return true; }
513 virtual void finalizeContents() {}
514 virtual void writeTo(uint8_t *buf) = 0;
515
516 static bool classof(const SectionBase *sec) {
517 return sec->kind() == InputSectionBase::Synthetic;
518 }
519};
520
521inline bool isStaticRelSecType(uint32_t type) {
522 return type == llvm::ELF::SHT_RELA || type == llvm::ELF::SHT_CREL ||
523 type == llvm::ELF::SHT_REL;
524}
525
526inline bool isDebugSection(const InputSectionBase &sec) {
527 return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
528 sec.name.starts_with(Prefix: ".debug");
529}
530
531std::string toStr(elf::Ctx &, const elf::InputSectionBase *);
532const ELFSyncStream &operator<<(const ELFSyncStream &,
533 const InputSectionBase *);
534const ELFSyncStream &operator<<(const ELFSyncStream &,
535 InputSectionBase::ObjMsg &&);
536const ELFSyncStream &operator<<(const ELFSyncStream &,
537 InputSectionBase::SrcMsg &&);
538} // namespace elf
539} // namespace lld
540
541#endif
542