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
12 #include "Relocations.h"
13 #include "lld/Common/CommonLinkerContext.h"
14 #include "lld/Common/LLVM.h"
15 #include "lld/Common/Memory.h"
16 #include "llvm/ADT/CachedHashString.h"
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/TinyPtrVector.h"
20 #include "llvm/Object/ELF.h"
21 #include "llvm/Support/Compiler.h"
31 class SyntheticSection
;
32 template <class ELFT
> class ObjFile
;
35 LLVM_LIBRARY_VISIBILITY
extern std::vector
<Partition
> partitions
;
37 // Returned by InputSectionBase::relsOrRelas. At least one member is empty.
38 template <class ELFT
> struct RelsOrRelas
{
39 ArrayRef
<typename
ELFT::Rel
> rels
;
40 ArrayRef
<typename
ELFT::Rela
> relas
;
41 bool areRelocsRel() const { return rels
.size(); }
44 // This is the base class of all sections that lld handles. Some are sections in
45 // input files, some are sections in the produced output file and some exist
46 // just as a convenience for implementing special ways of combining some
50 enum Kind
{ Regular
, Synthetic
, EHFrame
, Merge
, Output
};
52 Kind
kind() const { return (Kind
)sectionKind
; }
54 uint8_t sectionKind
: 3;
56 // The next two bit fields are only used by InputSectionBase, but we
57 // put them here so the struct packs better.
61 // Set for sections that should not be folded by ICF.
62 uint8_t keepUnique
: 1;
64 uint8_t partition
= 1;
68 // The 1-indexed partition that this section is assigned to by the garbage
69 // collector, or 0 if this section is dead. Normally there is only one
70 // partition, so this will either be 0 or 1.
71 elf::Partition
&getPartition() const;
73 // These corresponds to the fields in Elf_Shdr.
80 OutputSection
*getOutputSection();
81 const OutputSection
*getOutputSection() const {
82 return const_cast<SectionBase
*>(this)->getOutputSection();
85 // Translate an offset in the input section to an offset in the output
87 uint64_t getOffset(uint64_t offset
) const;
89 uint64_t getVA(uint64_t offset
= 0) const;
91 bool isLive() const { return partition
!= 0; }
92 void markLive() { partition
= 1; }
93 void markDead() { partition
= 0; }
96 constexpr SectionBase(Kind sectionKind
, StringRef name
, uint64_t flags
,
97 uint32_t entsize
, uint32_t addralign
, uint32_t type
,
98 uint32_t info
, uint32_t link
)
99 : sectionKind(sectionKind
), bss(false), keepUnique(false), type(type
),
100 name(name
), flags(flags
), addralign(addralign
), entsize(entsize
),
101 link(link
), info(info
) {}
104 struct RISCVRelaxAux
;
106 // This corresponds to a section of an input file.
107 class InputSectionBase
: public SectionBase
{
109 template <class ELFT
>
110 InputSectionBase(ObjFile
<ELFT
> &file
, const typename
ELFT::Shdr
&header
,
111 StringRef name
, Kind sectionKind
);
113 InputSectionBase(InputFile
*file
, uint64_t flags
, uint32_t type
,
114 uint64_t entsize
, uint32_t link
, uint32_t info
,
115 uint32_t addralign
, ArrayRef
<uint8_t> data
, StringRef name
,
118 static bool classof(const SectionBase
*s
) { return s
->kind() != Output
; }
120 // The file which contains this section. Its dynamic type is always
121 // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
125 // Input sections are part of an output section. Special sections
126 // like .eh_frame and merge sections are first combined into a
127 // synthetic section that is then added to an output section. In all
128 // cases this points one level up.
129 SectionBase
*parent
= nullptr;
131 // Section index of the relocation section if exists.
132 uint32_t relSecIdx
= 0;
134 template <class ELFT
> ObjFile
<ELFT
> *getFile() const {
135 return cast_or_null
<ObjFile
<ELFT
>>(file
);
138 // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
139 // indicate the number of bytes which is not counted in the size. This should
140 // be reset to zero after uses.
141 uint32_t bytesDropped
= 0;
143 mutable bool compressed
= false;
145 // Whether the section needs to be padded with a NOP filler due to
146 // deleteFallThruJmpInsn.
147 bool nopFiller
= false;
149 void drop_back(unsigned num
) {
150 assert(bytesDropped
+ num
< 256);
154 void push_back(uint64_t num
) {
155 assert(bytesDropped
>= num
);
159 mutable const uint8_t *content_
;
164 size
-= bytesDropped
;
169 ArrayRef
<uint8_t> content() const {
170 return ArrayRef
<uint8_t>(content_
, size
);
172 ArrayRef
<uint8_t> contentMaybeDecompress() const {
178 // The next member in the section group if this section is in a group. This is
179 // used by --gc-sections.
180 InputSectionBase
*nextInSectionGroup
= nullptr;
182 template <class ELFT
> RelsOrRelas
<ELFT
> relsOrRelas() const;
184 // InputSections that are dependent on us (reverse dependency for GC)
185 llvm::TinyPtrVector
<InputSection
*> dependentSections
;
187 // Returns the size of this section (even if this is a common or BSS.)
188 size_t getSize() const;
190 InputSection
*getLinkOrderDep() const;
192 // Get the function symbol that encloses this offset from within the
194 Defined
*getEnclosingFunction(uint64_t offset
);
196 // Returns a source location string. Used to construct an error message.
197 std::string
getLocation(uint64_t offset
);
198 std::string
getSrcMsg(const Symbol
&sym
, uint64_t offset
);
199 std::string
getObjMsg(uint64_t offset
);
201 // Each section knows how to relocate itself. These functions apply
202 // relocations, assuming that Buf points to this section's copy in
203 // the mmap'ed output buffer.
204 template <class ELFT
> void relocate(uint8_t *buf
, uint8_t *bufEnd
);
205 static uint64_t getRelocTargetVA(const InputFile
*File
, RelType Type
,
206 int64_t A
, uint64_t P
, const Symbol
&Sym
,
209 // The native ELF reloc data type is not very convenient to handle.
210 // So we convert ELF reloc records to our own records in Relocations.cpp.
211 // This vector contains such "cooked" relocations.
212 SmallVector
<Relocation
, 0> relocations
;
214 void addReloc(const Relocation
&r
) { relocations
.push_back(r
); }
215 MutableArrayRef
<Relocation
> relocs() { return relocations
; }
216 ArrayRef
<Relocation
> relocs() const { return relocations
; }
219 // These are modifiers to jump instructions that are necessary when basic
220 // block sections are enabled. Basic block sections creates opportunities
221 // to relax jump instructions at basic block boundaries after reordering the
223 JumpInstrMod
*jumpInstrMod
= nullptr;
225 // Auxiliary information for RISC-V linker relaxation. RISC-V does not use
227 RISCVRelaxAux
*relaxAux
;
229 // The compressed content size when `compressed` is true.
230 size_t compressedSize
;
233 // A function compiled with -fsplit-stack calling a function
234 // compiled without -fsplit-stack needs its prologue adjusted. Find
235 // such functions and adjust their prologues. This is very similar
236 // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
238 template <typename ELFT
>
239 void adjustSplitStackFunctionPrologues(uint8_t *buf
, uint8_t *end
);
242 template <typename T
> llvm::ArrayRef
<T
> getDataAs() const {
243 size_t s
= content().size();
244 assert(s
% sizeof(T
) == 0);
245 return llvm::ArrayRef
<T
>((const T
*)content().data(), s
/ sizeof(T
));
249 template <typename ELFT
>
250 void parseCompressedHeader();
251 void decompress() const;
254 // SectionPiece represents a piece of splittable section contents.
255 // We allocate a lot of these and binary search on them. This means that they
256 // have to be as compact as possible, which is why we don't store the size (can
257 // be found by looking at the next one).
258 struct SectionPiece
{
259 SectionPiece() = default;
260 SectionPiece(size_t off
, uint32_t hash
, bool live
)
261 : inputOff(off
), live(live
), hash(hash
>> 1) {}
266 uint64_t outputOff
= 0;
269 static_assert(sizeof(SectionPiece
) == 16, "SectionPiece is too big");
271 // This corresponds to a SHF_MERGE section of an input file.
272 class MergeInputSection
: public InputSectionBase
{
274 template <class ELFT
>
275 MergeInputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
277 MergeInputSection(uint64_t flags
, uint32_t type
, uint64_t entsize
,
278 ArrayRef
<uint8_t> data
, StringRef name
);
280 static bool classof(const SectionBase
*s
) { return s
->kind() == Merge
; }
281 void splitIntoPieces();
283 // Translate an offset in the input section to an offset in the parent
284 // MergeSyntheticSection.
285 uint64_t getParentOffset(uint64_t offset
) const;
287 // Splittable sections are handled as a sequence of data
288 // rather than a single large blob of data.
289 SmallVector
<SectionPiece
, 0> pieces
;
291 // Returns I'th piece's data. This function is very hot when
292 // string merging is enabled, so we want to inline.
293 LLVM_ATTRIBUTE_ALWAYS_INLINE
294 llvm::CachedHashStringRef
getData(size_t i
) const {
295 size_t begin
= pieces
[i
].inputOff
;
297 (pieces
.size() - 1 == i
) ? content().size() : pieces
[i
+ 1].inputOff
;
298 return {toStringRef(content().slice(begin
, end
- begin
)), pieces
[i
].hash
};
301 // Returns the SectionPiece at a given input section offset.
302 SectionPiece
&getSectionPiece(uint64_t offset
);
303 const SectionPiece
&getSectionPiece(uint64_t offset
) const {
304 return const_cast<MergeInputSection
*>(this)->getSectionPiece(offset
);
307 SyntheticSection
*getParent() const {
308 return cast_or_null
<SyntheticSection
>(parent
);
312 void splitStrings(StringRef s
, size_t size
);
313 void splitNonStrings(ArrayRef
<uint8_t> a
, size_t size
);
316 struct EhSectionPiece
{
317 EhSectionPiece(size_t off
, InputSectionBase
*sec
, uint32_t size
,
318 unsigned firstRelocation
)
319 : inputOff(off
), sec(sec
), size(size
), firstRelocation(firstRelocation
) {}
321 ArrayRef
<uint8_t> data() const {
322 return {sec
->content().data() + this->inputOff
, size
};
326 ssize_t outputOff
= -1;
327 InputSectionBase
*sec
;
329 unsigned firstRelocation
;
332 // This corresponds to a .eh_frame section of an input file.
333 class EhInputSection
: public InputSectionBase
{
335 template <class ELFT
>
336 EhInputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
338 static bool classof(const SectionBase
*s
) { return s
->kind() == EHFrame
; }
339 template <class ELFT
> void split();
340 template <class ELFT
, class RelTy
> void split(ArrayRef
<RelTy
> rels
);
342 // Splittable sections are handled as a sequence of data
343 // rather than a single large blob of data.
344 SmallVector
<EhSectionPiece
, 0> cies
, fdes
;
346 SyntheticSection
*getParent() const;
347 uint64_t getParentOffset(uint64_t offset
) const;
350 // This is a section that is added directly to an output section
351 // instead of needing special combination via a synthetic section. This
352 // includes all input sections with the exceptions of SHF_MERGE and
353 // .eh_frame. It also includes the synthetic sections themselves.
354 class InputSection
: public InputSectionBase
{
356 InputSection(InputFile
*f
, uint64_t flags
, uint32_t type
, uint32_t addralign
,
357 ArrayRef
<uint8_t> data
, StringRef name
, Kind k
= Regular
);
358 template <class ELFT
>
359 InputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
362 static bool classof(const SectionBase
*s
) {
363 return s
->kind() == SectionBase::Regular
||
364 s
->kind() == SectionBase::Synthetic
;
367 // Write this section to a mmap'ed file, assuming Buf is pointing to
368 // beginning of the output section.
369 template <class ELFT
> void writeTo(uint8_t *buf
);
371 OutputSection
*getParent() const {
372 return reinterpret_cast<OutputSection
*>(parent
);
375 // This variable has two usages. Initially, it represents an index in the
376 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
377 // sections. After assignAddresses is called, it represents the offset from
378 // the beginning of the output section this section was assigned to.
379 uint64_t outSecOff
= 0;
381 InputSectionBase
*getRelocatedSection() const;
383 template <class ELFT
, class RelTy
>
384 void relocateNonAlloc(uint8_t *buf
, llvm::ArrayRef
<RelTy
> rels
);
386 // Points to the canonical section. If ICF folds two sections, repl pointer of
387 // one section points to the other.
388 InputSection
*repl
= this;
391 uint32_t eqClass
[2] = {0, 0};
393 // Called by ICF to merge two input sections.
394 void replace(InputSection
*other
);
396 static InputSection discarded
;
399 template <class ELFT
, class RelTy
> void copyRelocations(uint8_t *buf
);
401 template <class ELFT
, class RelTy
, class RelIt
>
402 void copyRelocations(uint8_t *buf
, llvm::iterator_range
<RelIt
> rels
);
404 template <class ELFT
> void copyShtGroup(uint8_t *buf
);
407 static_assert(sizeof(InputSection
) <= 160, "InputSection is too big");
409 class SyntheticSection
: public InputSection
{
411 SyntheticSection(uint64_t flags
, uint32_t type
, uint32_t addralign
,
413 : InputSection(nullptr, flags
, type
, addralign
, {}, name
,
414 InputSectionBase::Synthetic
) {}
416 virtual ~SyntheticSection() = default;
417 virtual size_t getSize() const = 0;
418 virtual bool updateAllocSize() { return false; }
419 // If the section has the SHF_ALLOC flag and the size may be changed if
420 // thunks are added, update the section size.
421 virtual bool isNeeded() const { return true; }
422 virtual void finalizeContents() {}
423 virtual void writeTo(uint8_t *buf
) = 0;
425 static bool classof(const SectionBase
*sec
) {
426 return sec
->kind() == InputSectionBase::Synthetic
;
430 inline bool isDebugSection(const InputSectionBase
&sec
) {
431 return (sec
.flags
& llvm::ELF::SHF_ALLOC
) == 0 &&
432 sec
.name
.starts_with(".debug");
435 // The set of TOC entries (.toc + addend) for which we should not apply
436 // toc-indirect to toc-relative relaxation. const Symbol * refers to the
437 // STT_SECTION symbol associated to the .toc input section.
438 extern llvm::DenseSet
<std::pair
<const Symbol
*, uint64_t>> ppc64noTocRelax
;
442 std::string
toString(const elf::InputSectionBase
*);