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/TinyPtrVector.h"
19 #include "llvm/Object/ELF.h"
20 #include "llvm/Support/Compiler.h"
30 class SyntheticSection
;
31 template <class ELFT
> class ObjFile
;
34 LLVM_LIBRARY_VISIBILITY
extern std::vector
<Partition
> partitions
;
36 // Returned by InputSectionBase::relsOrRelas. At least one member is empty.
37 template <class ELFT
> struct RelsOrRelas
{
38 ArrayRef
<typename
ELFT::Rel
> rels
;
39 ArrayRef
<typename
ELFT::Rela
> relas
;
40 bool areRelocsRel() const { return rels
.size(); }
43 // This is the base class of all sections that lld handles. Some are sections in
44 // input files, some are sections in the produced output file and some exist
45 // just as a convenience for implementing special ways of combining some
49 enum Kind
{ Regular
, Synthetic
, EHFrame
, Merge
, Output
};
51 Kind
kind() const { return (Kind
)sectionKind
; }
53 uint8_t sectionKind
: 3;
55 // The next two bit fields are only used by InputSectionBase, but we
56 // put them here so the struct packs better.
60 // Set for sections that should not be folded by ICF.
61 uint8_t keepUnique
: 1;
63 uint8_t partition
= 1;
67 // The 1-indexed partition that this section is assigned to by the garbage
68 // collector, or 0 if this section is dead. Normally there is only one
69 // partition, so this will either be 0 or 1.
70 elf::Partition
&getPartition() const;
72 // 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 constexpr SectionBase(Kind sectionKind
, StringRef name
, uint64_t flags
,
96 uint32_t entsize
, uint32_t addralign
, uint32_t type
,
97 uint32_t info
, uint32_t link
)
98 : sectionKind(sectionKind
), bss(false), keepUnique(false), type(type
),
99 name(name
), flags(flags
), addralign(addralign
), entsize(entsize
),
100 link(link
), info(info
) {}
103 struct RISCVRelaxAux
;
105 // This corresponds to a section of an input file.
106 class InputSectionBase
: public SectionBase
{
108 template <class ELFT
>
109 InputSectionBase(ObjFile
<ELFT
> &file
, const typename
ELFT::Shdr
&header
,
110 StringRef name
, Kind sectionKind
);
112 InputSectionBase(InputFile
*file
, uint64_t flags
, uint32_t type
,
113 uint64_t entsize
, uint32_t link
, uint32_t info
,
114 uint32_t addralign
, ArrayRef
<uint8_t> data
, StringRef name
,
117 static bool classof(const SectionBase
*s
) { return s
->kind() != Output
; }
119 // The file which contains this section. Its dynamic type is always
120 // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
124 // Input sections are part of an output section. Special sections
125 // like .eh_frame and merge sections are first combined into a
126 // synthetic section that is then added to an output section. In all
127 // cases this points one level up.
128 SectionBase
*parent
= nullptr;
130 // Section index of the relocation section if exists.
131 uint32_t relSecIdx
= 0;
133 template <class ELFT
> ObjFile
<ELFT
> *getFile() const {
134 return cast_or_null
<ObjFile
<ELFT
>>(file
);
137 // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
138 // indicate the number of bytes which is not counted in the size. This should
139 // be reset to zero after uses.
140 uint16_t bytesDropped
= 0;
142 mutable bool compressed
= false;
144 // Whether the section needs to be padded with a NOP filler due to
145 // deleteFallThruJmpInsn.
146 bool nopFiller
= false;
148 void drop_back(unsigned num
) {
149 assert(bytesDropped
+ num
< 256);
153 void push_back(uint64_t num
) {
154 assert(bytesDropped
>= num
);
158 mutable const uint8_t *content_
;
163 size
-= bytesDropped
;
168 ArrayRef
<uint8_t> content() const {
169 return ArrayRef
<uint8_t>(content_
, size
);
171 ArrayRef
<uint8_t> contentMaybeDecompress() const {
177 // The next member in the section group if this section is in a group. This is
178 // used by --gc-sections.
179 InputSectionBase
*nextInSectionGroup
= nullptr;
181 template <class ELFT
> RelsOrRelas
<ELFT
> relsOrRelas() const;
183 // InputSections that are dependent on us (reverse dependency for GC)
184 llvm::TinyPtrVector
<InputSection
*> dependentSections
;
186 // Returns the size of this section (even if this is a common or BSS.)
187 size_t getSize() const;
189 InputSection
*getLinkOrderDep() const;
191 // Get the function symbol that encloses this offset from within the
193 Defined
*getEnclosingFunction(uint64_t offset
);
195 // Returns a source location string. Used to construct an error message.
196 std::string
getLocation(uint64_t offset
);
197 std::string
getSrcMsg(const Symbol
&sym
, uint64_t offset
);
198 std::string
getObjMsg(uint64_t offset
);
200 // Each section knows how to relocate itself. These functions apply
201 // relocations, assuming that Buf points to this section's copy in
202 // the mmap'ed output buffer.
203 template <class ELFT
> void relocate(uint8_t *buf
, uint8_t *bufEnd
);
204 static uint64_t getRelocTargetVA(const InputFile
*File
, RelType Type
,
205 int64_t A
, uint64_t P
, const Symbol
&Sym
,
208 // The native ELF reloc data type is not very convenient to handle.
209 // So we convert ELF reloc records to our own records in Relocations.cpp.
210 // This vector contains such "cooked" relocations.
211 SmallVector
<Relocation
, 0> relocations
;
213 void addReloc(const Relocation
&r
) { relocations
.push_back(r
); }
214 MutableArrayRef
<Relocation
> relocs() { return relocations
; }
215 ArrayRef
<Relocation
> relocs() const { return relocations
; }
218 // These are modifiers to jump instructions that are necessary when basic
219 // block sections are enabled. Basic block sections creates opportunities
220 // to relax jump instructions at basic block boundaries after reordering the
222 JumpInstrMod
*jumpInstrMod
= nullptr;
224 // Auxiliary information for RISC-V linker relaxation. RISC-V does not use
226 RISCVRelaxAux
*relaxAux
;
228 // The compressed content size when `compressed` is true.
229 size_t compressedSize
;
232 // A function compiled with -fsplit-stack calling a function
233 // compiled without -fsplit-stack needs its prologue adjusted. Find
234 // such functions and adjust their prologues. This is very similar
235 // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
237 template <typename ELFT
>
238 void adjustSplitStackFunctionPrologues(uint8_t *buf
, uint8_t *end
);
241 template <typename T
> llvm::ArrayRef
<T
> getDataAs() const {
242 size_t s
= content().size();
243 assert(s
% sizeof(T
) == 0);
244 return llvm::ArrayRef
<T
>((const T
*)content().data(), s
/ sizeof(T
));
248 template <typename ELFT
>
249 void parseCompressedHeader();
250 void decompress() const;
253 // SectionPiece represents a piece of splittable section contents.
254 // We allocate a lot of these and binary search on them. This means that they
255 // have to be as compact as possible, which is why we don't store the size (can
256 // be found by looking at the next one).
257 struct SectionPiece
{
258 SectionPiece() = default;
259 SectionPiece(size_t off
, uint32_t hash
, bool live
)
260 : inputOff(off
), live(live
), hash(hash
>> 1) {}
265 uint64_t outputOff
= 0;
268 static_assert(sizeof(SectionPiece
) == 16, "SectionPiece is too big");
270 // This corresponds to a SHF_MERGE section of an input file.
271 class MergeInputSection
: public InputSectionBase
{
273 template <class ELFT
>
274 MergeInputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
276 MergeInputSection(uint64_t flags
, uint32_t type
, uint64_t entsize
,
277 ArrayRef
<uint8_t> data
, StringRef name
);
279 static bool classof(const SectionBase
*s
) { return s
->kind() == Merge
; }
280 void splitIntoPieces();
282 // Translate an offset in the input section to an offset in the parent
283 // MergeSyntheticSection.
284 uint64_t getParentOffset(uint64_t offset
) const;
286 // Splittable sections are handled as a sequence of data
287 // rather than a single large blob of data.
288 SmallVector
<SectionPiece
, 0> pieces
;
290 // Returns I'th piece's data. This function is very hot when
291 // string merging is enabled, so we want to inline.
292 LLVM_ATTRIBUTE_ALWAYS_INLINE
293 llvm::CachedHashStringRef
getData(size_t i
) const {
294 size_t begin
= pieces
[i
].inputOff
;
296 (pieces
.size() - 1 == i
) ? content().size() : pieces
[i
+ 1].inputOff
;
297 return {toStringRef(content().slice(begin
, end
- begin
)), pieces
[i
].hash
};
300 // Returns the SectionPiece at a given input section offset.
301 SectionPiece
&getSectionPiece(uint64_t offset
);
302 const SectionPiece
&getSectionPiece(uint64_t offset
) const {
303 return const_cast<MergeInputSection
*>(this)->getSectionPiece(offset
);
306 SyntheticSection
*getParent() const {
307 return cast_or_null
<SyntheticSection
>(parent
);
311 void splitStrings(StringRef s
, size_t size
);
312 void splitNonStrings(ArrayRef
<uint8_t> a
, size_t size
);
315 struct EhSectionPiece
{
316 EhSectionPiece(size_t off
, InputSectionBase
*sec
, uint32_t size
,
317 unsigned firstRelocation
)
318 : inputOff(off
), sec(sec
), size(size
), firstRelocation(firstRelocation
) {}
320 ArrayRef
<uint8_t> data() const {
321 return {sec
->content().data() + this->inputOff
, size
};
325 ssize_t outputOff
= -1;
326 InputSectionBase
*sec
;
328 unsigned firstRelocation
;
331 // This corresponds to a .eh_frame section of an input file.
332 class EhInputSection
: public InputSectionBase
{
334 template <class ELFT
>
335 EhInputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
337 static bool classof(const SectionBase
*s
) { return s
->kind() == EHFrame
; }
338 template <class ELFT
> void split();
339 template <class ELFT
, class RelTy
> void split(ArrayRef
<RelTy
> rels
);
341 // Splittable sections are handled as a sequence of data
342 // rather than a single large blob of data.
343 SmallVector
<EhSectionPiece
, 0> cies
, fdes
;
345 SyntheticSection
*getParent() const;
346 uint64_t getParentOffset(uint64_t offset
) const;
349 // This is a section that is added directly to an output section
350 // instead of needing special combination via a synthetic section. This
351 // includes all input sections with the exceptions of SHF_MERGE and
352 // .eh_frame. It also includes the synthetic sections themselves.
353 class InputSection
: public InputSectionBase
{
355 InputSection(InputFile
*f
, uint64_t flags
, uint32_t type
, uint32_t addralign
,
356 ArrayRef
<uint8_t> data
, StringRef name
, Kind k
= Regular
);
357 template <class ELFT
>
358 InputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
361 static bool classof(const SectionBase
*s
) {
362 return s
->kind() == SectionBase::Regular
||
363 s
->kind() == SectionBase::Synthetic
;
366 // Write this section to a mmap'ed file, assuming Buf is pointing to
367 // beginning of the output section.
368 template <class ELFT
> void writeTo(uint8_t *buf
);
370 OutputSection
*getParent() const {
371 return reinterpret_cast<OutputSection
*>(parent
);
374 // This variable has two usages. Initially, it represents an index in the
375 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
376 // sections. After assignAddresses is called, it represents the offset from
377 // the beginning of the output section this section was assigned to.
378 uint64_t outSecOff
= 0;
380 InputSectionBase
*getRelocatedSection() const;
382 template <class ELFT
, class RelTy
>
383 void relocateNonAlloc(uint8_t *buf
, llvm::ArrayRef
<RelTy
> rels
);
385 // Points to the canonical section. If ICF folds two sections, repl pointer of
386 // one section points to the other.
387 InputSection
*repl
= this;
390 uint32_t eqClass
[2] = {0, 0};
392 // Called by ICF to merge two input sections.
393 void replace(InputSection
*other
);
395 static InputSection discarded
;
398 template <class ELFT
, class RelTy
>
399 void copyRelocations(uint8_t *buf
, llvm::ArrayRef
<RelTy
> rels
);
401 template <class ELFT
> void copyShtGroup(uint8_t *buf
);
404 static_assert(sizeof(InputSection
) <= 152, "InputSection is too big");
406 class SyntheticSection
: public InputSection
{
408 SyntheticSection(uint64_t flags
, uint32_t type
, uint32_t addralign
,
410 : InputSection(nullptr, flags
, type
, addralign
, {}, name
,
411 InputSectionBase::Synthetic
) {}
413 virtual ~SyntheticSection() = default;
414 virtual size_t getSize() const = 0;
415 virtual bool updateAllocSize() { return false; }
416 // If the section has the SHF_ALLOC flag and the size may be changed if
417 // thunks are added, update the section size.
418 virtual bool isNeeded() const { return true; }
419 virtual void finalizeContents() {}
420 virtual void writeTo(uint8_t *buf
) = 0;
422 static bool classof(const SectionBase
*sec
) {
423 return sec
->kind() == InputSectionBase::Synthetic
;
427 inline bool isDebugSection(const InputSectionBase
&sec
) {
428 return (sec
.flags
& llvm::ELF::SHF_ALLOC
) == 0 &&
429 sec
.name
.startswith(".debug");
432 // The set of TOC entries (.toc + addend) for which we should not apply
433 // toc-indirect to toc-relative relaxation. const Symbol * refers to the
434 // STT_SECTION symbol associated to the .toc input section.
435 extern llvm::DenseSet
<std::pair
<const Symbol
*, uint64_t>> ppc64noTocRelax
;
439 std::string
toString(const elf::InputSectionBase
*);