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"
29 class SyntheticSection
;
30 template <class ELFT
> class ObjFile
;
33 extern std::vector
<Partition
> partitions
;
35 // Returned by InputSectionBase::relsOrRelas. At least one member is empty.
36 template <class ELFT
> struct RelsOrRelas
{
37 ArrayRef
<typename
ELFT::Rel
> rels
;
38 ArrayRef
<typename
ELFT::Rela
> relas
;
39 bool areRelocsRel() const { return rels
.size(); }
42 // This is the base class of all sections that lld handles. Some are sections in
43 // input files, some are sections in the produced output file and some exist
44 // just as a convenience for implementing special ways of combining some
48 enum Kind
{ Regular
, Synthetic
, EHFrame
, Merge
, Output
};
50 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 // The 1-indexed partition that this section is assigned to by the garbage
65 // collector, or 0 if this section is dead. Normally there is only one
66 // partition, so this will either be 0 or 1.
67 uint8_t partition
= 1;
68 elf::Partition
&getPartition() const;
70 // These corresponds to the fields in Elf_Shdr.
78 OutputSection
*getOutputSection();
79 const OutputSection
*getOutputSection() const {
80 return const_cast<SectionBase
*>(this)->getOutputSection();
83 // Translate an offset in the input section to an offset in the output
85 uint64_t getOffset(uint64_t offset
) const;
87 uint64_t getVA(uint64_t offset
= 0) const;
89 bool isLive() const { return partition
!= 0; }
90 void markLive() { partition
= 1; }
91 void markDead() { partition
= 0; }
94 constexpr SectionBase(Kind sectionKind
, StringRef name
, uint64_t flags
,
95 uint32_t entsize
, uint32_t alignment
, uint32_t type
,
96 uint32_t info
, uint32_t link
)
97 : name(name
), sectionKind(sectionKind
), bss(false), keepUnique(false),
98 alignment(alignment
), flags(flags
), entsize(entsize
), type(type
),
99 link(link
), info(info
) {}
102 struct RISCVRelaxAux
;
104 // This corresponds to a section of an input file.
105 class InputSectionBase
: public SectionBase
{
107 template <class ELFT
>
108 InputSectionBase(ObjFile
<ELFT
> &file
, const typename
ELFT::Shdr
&header
,
109 StringRef name
, Kind sectionKind
);
111 InputSectionBase(InputFile
*file
, uint64_t flags
, uint32_t type
,
112 uint64_t entsize
, uint32_t link
, uint32_t info
,
113 uint32_t alignment
, ArrayRef
<uint8_t> data
, StringRef name
,
116 static bool classof(const SectionBase
*s
) { return s
->kind() != Output
; }
118 // The file which contains this section. Its dynamic type is always
119 // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
123 // Input sections are part of an output section. Special sections
124 // like .eh_frame and merge sections are first combined into a
125 // synthetic section that is then added to an output section. In all
126 // cases this points one level up.
127 SectionBase
*parent
= nullptr;
129 // Section index of the relocation section if exists.
130 uint32_t relSecIdx
= 0;
132 template <class ELFT
> ObjFile
<ELFT
> *getFile() const {
133 return cast_or_null
<ObjFile
<ELFT
>>(file
);
136 // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
137 // indicate the number of bytes which is not counted in the size. This should
138 // be reset to zero after uses.
139 uint16_t bytesDropped
= 0;
141 // Whether the section needs to be padded with a NOP filler due to
142 // deleteFallThruJmpInsn.
143 bool nopFiller
= false;
145 void drop_back(unsigned num
) {
146 assert(bytesDropped
+ num
< 256);
150 void push_back(uint64_t num
) {
151 assert(bytesDropped
>= num
);
155 mutable ArrayRef
<uint8_t> rawData
;
159 rawData
= rawData
.drop_back(bytesDropped
);
164 ArrayRef
<uint8_t> data() const {
165 if (uncompressedSize
>= 0)
170 // The next member in the section group if this section is in a group. This is
171 // used by --gc-sections.
172 InputSectionBase
*nextInSectionGroup
= nullptr;
174 template <class ELFT
> RelsOrRelas
<ELFT
> relsOrRelas() const;
176 // InputSections that are dependent on us (reverse dependency for GC)
177 llvm::TinyPtrVector
<InputSection
*> dependentSections
;
179 // Returns the size of this section (even if this is a common or BSS.)
180 size_t getSize() const;
182 InputSection
*getLinkOrderDep() const;
184 // Get the function symbol that encloses this offset from within the
186 Defined
*getEnclosingFunction(uint64_t offset
);
188 // Returns a source location string. Used to construct an error message.
189 std::string
getLocation(uint64_t offset
);
190 std::string
getSrcMsg(const Symbol
&sym
, uint64_t offset
);
191 std::string
getObjMsg(uint64_t offset
);
193 // Each section knows how to relocate itself. These functions apply
194 // relocations, assuming that Buf points to this section's copy in
195 // the mmap'ed output buffer.
196 template <class ELFT
> void relocate(uint8_t *buf
, uint8_t *bufEnd
);
197 void relocateAlloc(uint8_t *buf
, uint8_t *bufEnd
);
198 static uint64_t getRelocTargetVA(const InputFile
*File
, RelType Type
,
199 int64_t A
, uint64_t P
, const Symbol
&Sym
,
202 // The native ELF reloc data type is not very convenient to handle.
203 // So we convert ELF reloc records to our own records in Relocations.cpp.
204 // This vector contains such "cooked" relocations.
205 SmallVector
<Relocation
, 0> relocations
;
208 // These are modifiers to jump instructions that are necessary when basic
209 // block sections are enabled. Basic block sections creates opportunities
210 // to relax jump instructions at basic block boundaries after reordering the
212 JumpInstrMod
*jumpInstrMod
= nullptr;
214 // Auxiliary information for RISC-V linker relaxation. RISC-V does not use
216 RISCVRelaxAux
*relaxAux
;
219 // A function compiled with -fsplit-stack calling a function
220 // compiled without -fsplit-stack needs its prologue adjusted. Find
221 // such functions and adjust their prologues. This is very similar
222 // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
224 template <typename ELFT
>
225 void adjustSplitStackFunctionPrologues(uint8_t *buf
, uint8_t *end
);
228 template <typename T
> llvm::ArrayRef
<T
> getDataAs() const {
229 size_t s
= rawData
.size();
230 assert(s
% sizeof(T
) == 0);
231 return llvm::makeArrayRef
<T
>((const T
*)rawData
.data(), s
/ sizeof(T
));
235 template <typename ELFT
>
236 void parseCompressedHeader();
237 void uncompress() const;
239 // This field stores the uncompressed size of the compressed data in rawData,
240 // or -1 if rawData is not compressed (either because the section wasn't
241 // compressed in the first place, or because we ended up uncompressing it).
242 // Since the feature is not used often, this is usually -1.
243 mutable int64_t uncompressedSize
= -1;
246 // SectionPiece represents a piece of splittable section contents.
247 // We allocate a lot of these and binary search on them. This means that they
248 // have to be as compact as possible, which is why we don't store the size (can
249 // be found by looking at the next one).
250 struct SectionPiece
{
251 SectionPiece() = default;
252 SectionPiece(size_t off
, uint32_t hash
, bool live
)
253 : inputOff(off
), live(live
), hash(hash
>> 1) {}
258 uint64_t outputOff
= 0;
261 static_assert(sizeof(SectionPiece
) == 16, "SectionPiece is too big");
263 // This corresponds to a SHF_MERGE section of an input file.
264 class MergeInputSection
: public InputSectionBase
{
266 template <class ELFT
>
267 MergeInputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
269 MergeInputSection(uint64_t flags
, uint32_t type
, uint64_t entsize
,
270 ArrayRef
<uint8_t> data
, StringRef name
);
272 static bool classof(const SectionBase
*s
) { return s
->kind() == Merge
; }
273 void splitIntoPieces();
275 // Translate an offset in the input section to an offset in the parent
276 // MergeSyntheticSection.
277 uint64_t getParentOffset(uint64_t offset
) const;
279 // Splittable sections are handled as a sequence of data
280 // rather than a single large blob of data.
281 SmallVector
<SectionPiece
, 0> pieces
;
283 // Returns I'th piece's data. This function is very hot when
284 // string merging is enabled, so we want to inline.
285 LLVM_ATTRIBUTE_ALWAYS_INLINE
286 llvm::CachedHashStringRef
getData(size_t i
) const {
287 size_t begin
= pieces
[i
].inputOff
;
289 (pieces
.size() - 1 == i
) ? rawData
.size() : pieces
[i
+ 1].inputOff
;
290 return {toStringRef(rawData
.slice(begin
, end
- begin
)), pieces
[i
].hash
};
293 // Returns the SectionPiece at a given input section offset.
294 SectionPiece
&getSectionPiece(uint64_t offset
);
295 const SectionPiece
&getSectionPiece(uint64_t offset
) const {
296 return const_cast<MergeInputSection
*>(this)->getSectionPiece(offset
);
299 SyntheticSection
*getParent() const {
300 return cast_or_null
<SyntheticSection
>(parent
);
304 void splitStrings(StringRef s
, size_t size
);
305 void splitNonStrings(ArrayRef
<uint8_t> a
, size_t size
);
308 struct EhSectionPiece
{
309 EhSectionPiece(size_t off
, InputSectionBase
*sec
, uint32_t size
,
310 unsigned firstRelocation
)
311 : inputOff(off
), sec(sec
), size(size
), firstRelocation(firstRelocation
) {}
313 ArrayRef
<uint8_t> data() const {
314 return {sec
->rawData
.data() + this->inputOff
, size
};
318 ssize_t outputOff
= -1;
319 InputSectionBase
*sec
;
321 unsigned firstRelocation
;
324 // This corresponds to a .eh_frame section of an input file.
325 class EhInputSection
: public InputSectionBase
{
327 template <class ELFT
>
328 EhInputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
330 static bool classof(const SectionBase
*s
) { return s
->kind() == EHFrame
; }
331 template <class ELFT
> void split();
332 template <class ELFT
, class RelTy
> void split(ArrayRef
<RelTy
> rels
);
334 // Splittable sections are handled as a sequence of data
335 // rather than a single large blob of data.
336 SmallVector
<EhSectionPiece
, 0> cies
, fdes
;
338 SyntheticSection
*getParent() const;
339 uint64_t getParentOffset(uint64_t offset
) const;
342 // This is a section that is added directly to an output section
343 // instead of needing special combination via a synthetic section. This
344 // includes all input sections with the exceptions of SHF_MERGE and
345 // .eh_frame. It also includes the synthetic sections themselves.
346 class InputSection
: public InputSectionBase
{
348 InputSection(InputFile
*f
, uint64_t flags
, uint32_t type
, uint32_t alignment
,
349 ArrayRef
<uint8_t> data
, StringRef name
, Kind k
= Regular
);
350 template <class ELFT
>
351 InputSection(ObjFile
<ELFT
> &f
, const typename
ELFT::Shdr
&header
,
354 static bool classof(const SectionBase
*s
) {
355 return s
->kind() == SectionBase::Regular
||
356 s
->kind() == SectionBase::Synthetic
;
359 // Write this section to a mmap'ed file, assuming Buf is pointing to
360 // beginning of the output section.
361 template <class ELFT
> void writeTo(uint8_t *buf
);
363 OutputSection
*getParent() const {
364 return reinterpret_cast<OutputSection
*>(parent
);
367 // This variable has two usages. Initially, it represents an index in the
368 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
369 // sections. After assignAddresses is called, it represents the offset from
370 // the beginning of the output section this section was assigned to.
371 uint64_t outSecOff
= 0;
373 InputSectionBase
*getRelocatedSection() const;
375 template <class ELFT
, class RelTy
>
376 void relocateNonAlloc(uint8_t *buf
, llvm::ArrayRef
<RelTy
> rels
);
378 // Points to the canonical section. If ICF folds two sections, repl pointer of
379 // one section points to the other.
380 InputSection
*repl
= this;
383 uint32_t eqClass
[2] = {0, 0};
385 // Called by ICF to merge two input sections.
386 void replace(InputSection
*other
);
388 static InputSection discarded
;
391 template <class ELFT
, class RelTy
>
392 void copyRelocations(uint8_t *buf
, llvm::ArrayRef
<RelTy
> rels
);
394 template <class ELFT
> void copyShtGroup(uint8_t *buf
);
397 static_assert(sizeof(InputSection
) <= 160, "InputSection is too big");
399 class SyntheticSection
: public InputSection
{
401 SyntheticSection(uint64_t flags
, uint32_t type
, uint32_t alignment
,
403 : InputSection(nullptr, flags
, type
, alignment
, {}, name
,
404 InputSectionBase::Synthetic
) {}
406 virtual ~SyntheticSection() = default;
407 virtual size_t getSize() const = 0;
408 virtual bool updateAllocSize() { return false; }
409 // If the section has the SHF_ALLOC flag and the size may be changed if
410 // thunks are added, update the section size.
411 virtual bool isNeeded() const { return true; }
412 virtual void finalizeContents() {}
413 virtual void writeTo(uint8_t *buf
) = 0;
415 static bool classof(const SectionBase
*sec
) {
416 return sec
->kind() == InputSectionBase::Synthetic
;
420 inline bool isDebugSection(const InputSectionBase
&sec
) {
421 return (sec
.flags
& llvm::ELF::SHF_ALLOC
) == 0 &&
422 sec
.name
.startswith(".debug");
425 // The list of all input sections.
426 extern SmallVector
<InputSectionBase
*, 0> inputSections
;
427 extern SmallVector
<EhInputSection
*, 0> ehInputSections
;
429 // The set of TOC entries (.toc + addend) for which we should not apply
430 // toc-indirect to toc-relative relaxation. const Symbol * refers to the
431 // STT_SECTION symbol associated to the .toc input section.
432 extern llvm::DenseSet
<std::pair
<const Symbol
*, uint64_t>> ppc64noTocRelax
;
436 std::string
toString(const elf::InputSectionBase
*);