annotate lld/ELF/InputSection.h @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- InputSection.h -------------------------------------------*- C++ -*-===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #ifndef LLD_ELF_INPUT_SECTION_H
anatofuz
parents:
diff changeset
10 #define LLD_ELF_INPUT_SECTION_H
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 #include "Relocations.h"
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
13 #include "lld/Common/CommonLinkerContext.h"
150
anatofuz
parents:
diff changeset
14 #include "lld/Common/LLVM.h"
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
15 #include "lld/Common/Memory.h"
150
anatofuz
parents:
diff changeset
16 #include "llvm/ADT/CachedHashString.h"
anatofuz
parents:
diff changeset
17 #include "llvm/ADT/DenseSet.h"
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
18 #include "llvm/ADT/StringExtras.h"
150
anatofuz
parents:
diff changeset
19 #include "llvm/ADT/TinyPtrVector.h"
anatofuz
parents:
diff changeset
20 #include "llvm/Object/ELF.h"
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
21 #include "llvm/Support/Compiler.h"
150
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 namespace lld {
anatofuz
parents:
diff changeset
24 namespace elf {
anatofuz
parents:
diff changeset
25
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
26 class InputFile;
150
anatofuz
parents:
diff changeset
27 class Symbol;
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 class Defined;
anatofuz
parents:
diff changeset
30 struct Partition;
anatofuz
parents:
diff changeset
31 class SyntheticSection;
anatofuz
parents:
diff changeset
32 template <class ELFT> class ObjFile;
anatofuz
parents:
diff changeset
33 class OutputSection;
anatofuz
parents:
diff changeset
34
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
35 LLVM_LIBRARY_VISIBILITY extern std::vector<Partition> partitions;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
36
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
37 // Returned by InputSectionBase::relsOrRelas. At least one member is empty.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
38 template <class ELFT> struct RelsOrRelas {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
39 ArrayRef<typename ELFT::Rel> rels;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
40 ArrayRef<typename ELFT::Rela> relas;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
41 bool areRelocsRel() const { return rels.size(); }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
42 };
150
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 // This is the base class of all sections that lld handles. Some are sections in
anatofuz
parents:
diff changeset
45 // input files, some are sections in the produced output file and some exist
anatofuz
parents:
diff changeset
46 // just as a convenience for implementing special ways of combining some
anatofuz
parents:
diff changeset
47 // sections.
anatofuz
parents:
diff changeset
48 class SectionBase {
anatofuz
parents:
diff changeset
49 public:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
50 enum Kind { Regular, Synthetic, EHFrame, Merge, Output };
150
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 Kind kind() const { return (Kind)sectionKind; }
anatofuz
parents:
diff changeset
53
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
54 uint8_t sectionKind : 3;
150
anatofuz
parents:
diff changeset
55
anatofuz
parents:
diff changeset
56 // The next two bit fields are only used by InputSectionBase, but we
anatofuz
parents:
diff changeset
57 // put them here so the struct packs better.
anatofuz
parents:
diff changeset
58
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
59 uint8_t bss : 1;
150
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 // Set for sections that should not be folded by ICF.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
62 uint8_t keepUnique : 1;
150
anatofuz
parents:
diff changeset
63
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
64 uint8_t partition = 1;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
65 uint32_t type;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
66 StringRef name;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
67
150
anatofuz
parents:
diff changeset
68 // The 1-indexed partition that this section is assigned to by the garbage
anatofuz
parents:
diff changeset
69 // collector, or 0 if this section is dead. Normally there is only one
anatofuz
parents:
diff changeset
70 // partition, so this will either be 0 or 1.
anatofuz
parents:
diff changeset
71 elf::Partition &getPartition() const;
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 // These corresponds to the fields in Elf_Shdr.
anatofuz
parents:
diff changeset
74 uint64_t flags;
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
75 uint32_t addralign;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
76 uint32_t entsize;
150
anatofuz
parents:
diff changeset
77 uint32_t link;
anatofuz
parents:
diff changeset
78 uint32_t info;
anatofuz
parents:
diff changeset
79
anatofuz
parents:
diff changeset
80 OutputSection *getOutputSection();
anatofuz
parents:
diff changeset
81 const OutputSection *getOutputSection() const {
anatofuz
parents:
diff changeset
82 return const_cast<SectionBase *>(this)->getOutputSection();
anatofuz
parents:
diff changeset
83 }
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 // Translate an offset in the input section to an offset in the output
anatofuz
parents:
diff changeset
86 // section.
anatofuz
parents:
diff changeset
87 uint64_t getOffset(uint64_t offset) const;
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 uint64_t getVA(uint64_t offset = 0) const;
anatofuz
parents:
diff changeset
90
anatofuz
parents:
diff changeset
91 bool isLive() const { return partition != 0; }
anatofuz
parents:
diff changeset
92 void markLive() { partition = 1; }
anatofuz
parents:
diff changeset
93 void markDead() { partition = 0; }
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 protected:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
96 constexpr SectionBase(Kind sectionKind, StringRef name, uint64_t flags,
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
97 uint32_t entsize, uint32_t addralign, uint32_t type,
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
98 uint32_t info, uint32_t link)
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
99 : sectionKind(sectionKind), bss(false), keepUnique(false), type(type),
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
100 name(name), flags(flags), addralign(addralign), entsize(entsize),
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
101 link(link), info(info) {}
150
anatofuz
parents:
diff changeset
102 };
anatofuz
parents:
diff changeset
103
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
104 struct RISCVRelaxAux;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
105
150
anatofuz
parents:
diff changeset
106 // This corresponds to a section of an input file.
anatofuz
parents:
diff changeset
107 class InputSectionBase : public SectionBase {
anatofuz
parents:
diff changeset
108 public:
anatofuz
parents:
diff changeset
109 template <class ELFT>
anatofuz
parents:
diff changeset
110 InputSectionBase(ObjFile<ELFT> &file, const typename ELFT::Shdr &header,
anatofuz
parents:
diff changeset
111 StringRef name, Kind sectionKind);
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 InputSectionBase(InputFile *file, uint64_t flags, uint32_t type,
anatofuz
parents:
diff changeset
114 uint64_t entsize, uint32_t link, uint32_t info,
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
115 uint32_t addralign, ArrayRef<uint8_t> data, StringRef name,
150
anatofuz
parents:
diff changeset
116 Kind sectionKind);
anatofuz
parents:
diff changeset
117
anatofuz
parents:
diff changeset
118 static bool classof(const SectionBase *s) { return s->kind() != Output; }
anatofuz
parents:
diff changeset
119
anatofuz
parents:
diff changeset
120 // The file which contains this section. Its dynamic type is always
anatofuz
parents:
diff changeset
121 // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
anatofuz
parents:
diff changeset
122 // its static type.
anatofuz
parents:
diff changeset
123 InputFile *file;
anatofuz
parents:
diff changeset
124
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
125 // Input sections are part of an output section. Special sections
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
126 // like .eh_frame and merge sections are first combined into a
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
127 // synthetic section that is then added to an output section. In all
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
128 // cases this points one level up.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
129 SectionBase *parent = nullptr;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
130
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
131 // Section index of the relocation section if exists.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
132 uint32_t relSecIdx = 0;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
133
150
anatofuz
parents:
diff changeset
134 template <class ELFT> ObjFile<ELFT> *getFile() const {
anatofuz
parents:
diff changeset
135 return cast_or_null<ObjFile<ELFT>>(file);
anatofuz
parents:
diff changeset
136 }
anatofuz
parents:
diff changeset
137
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
138 // Used by --optimize-bb-jumps and RISC-V linker relaxation temporarily to
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
139 // indicate the number of bytes which is not counted in the size. This should
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
140 // be reset to zero after uses.
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
141 uint32_t bytesDropped = 0;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
142
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
143 mutable bool compressed = false;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
144
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
145 // Whether the section needs to be padded with a NOP filler due to
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
146 // deleteFallThruJmpInsn.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
147 bool nopFiller = false;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
148
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
149 void drop_back(unsigned num) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
150 assert(bytesDropped + num < 256);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
151 bytesDropped += num;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
152 }
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
153
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
154 void push_back(uint64_t num) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
155 assert(bytesDropped >= num);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
156 bytesDropped -= num;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
157 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
158
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
159 mutable const uint8_t *content_;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
160 uint64_t size;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
161
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
162 void trim() {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
163 if (bytesDropped) {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
164 size -= bytesDropped;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
165 bytesDropped = 0;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
166 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
167 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
168
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
169 ArrayRef<uint8_t> content() const {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
170 return ArrayRef<uint8_t>(content_, size);
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
171 }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
172 ArrayRef<uint8_t> contentMaybeDecompress() const {
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
173 if (compressed)
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
174 decompress();
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
175 return content();
150
anatofuz
parents:
diff changeset
176 }
anatofuz
parents:
diff changeset
177
anatofuz
parents:
diff changeset
178 // The next member in the section group if this section is in a group. This is
anatofuz
parents:
diff changeset
179 // used by --gc-sections.
anatofuz
parents:
diff changeset
180 InputSectionBase *nextInSectionGroup = nullptr;
anatofuz
parents:
diff changeset
181
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
182 template <class ELFT> RelsOrRelas<ELFT> relsOrRelas() const;
150
anatofuz
parents:
diff changeset
183
anatofuz
parents:
diff changeset
184 // InputSections that are dependent on us (reverse dependency for GC)
anatofuz
parents:
diff changeset
185 llvm::TinyPtrVector<InputSection *> dependentSections;
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 // Returns the size of this section (even if this is a common or BSS.)
anatofuz
parents:
diff changeset
188 size_t getSize() const;
anatofuz
parents:
diff changeset
189
anatofuz
parents:
diff changeset
190 InputSection *getLinkOrderDep() const;
anatofuz
parents:
diff changeset
191
anatofuz
parents:
diff changeset
192 // Get the function symbol that encloses this offset from within the
anatofuz
parents:
diff changeset
193 // section.
anatofuz
parents:
diff changeset
194 Defined *getEnclosingFunction(uint64_t offset);
anatofuz
parents:
diff changeset
195
anatofuz
parents:
diff changeset
196 // Returns a source location string. Used to construct an error message.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
197 std::string getLocation(uint64_t offset);
150
anatofuz
parents:
diff changeset
198 std::string getSrcMsg(const Symbol &sym, uint64_t offset);
anatofuz
parents:
diff changeset
199 std::string getObjMsg(uint64_t offset);
anatofuz
parents:
diff changeset
200
anatofuz
parents:
diff changeset
201 // Each section knows how to relocate itself. These functions apply
anatofuz
parents:
diff changeset
202 // relocations, assuming that Buf points to this section's copy in
anatofuz
parents:
diff changeset
203 // the mmap'ed output buffer.
anatofuz
parents:
diff changeset
204 template <class ELFT> void relocate(uint8_t *buf, uint8_t *bufEnd);
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
205 static uint64_t getRelocTargetVA(const InputFile *File, RelType Type,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
206 int64_t A, uint64_t P, const Symbol &Sym,
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
207 RelExpr Expr);
150
anatofuz
parents:
diff changeset
208
anatofuz
parents:
diff changeset
209 // The native ELF reloc data type is not very convenient to handle.
anatofuz
parents:
diff changeset
210 // So we convert ELF reloc records to our own records in Relocations.cpp.
anatofuz
parents:
diff changeset
211 // This vector contains such "cooked" relocations.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
212 SmallVector<Relocation, 0> relocations;
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
213
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
214 void addReloc(const Relocation &r) { relocations.push_back(r); }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
215 MutableArrayRef<Relocation> relocs() { return relocations; }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
216 ArrayRef<Relocation> relocs() const { return relocations; }
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
217
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
218 union {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
219 // These are modifiers to jump instructions that are necessary when basic
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
220 // block sections are enabled. Basic block sections creates opportunities
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
221 // to relax jump instructions at basic block boundaries after reordering the
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
222 // basic blocks.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
223 JumpInstrMod *jumpInstrMod = nullptr;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
224
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
225 // Auxiliary information for RISC-V linker relaxation. RISC-V does not use
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
226 // jumpInstrMod.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
227 RISCVRelaxAux *relaxAux;
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
228
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
229 // The compressed content size when `compressed` is true.
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
230 size_t compressedSize;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
231 };
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
232
150
anatofuz
parents:
diff changeset
233 // A function compiled with -fsplit-stack calling a function
anatofuz
parents:
diff changeset
234 // compiled without -fsplit-stack needs its prologue adjusted. Find
anatofuz
parents:
diff changeset
235 // such functions and adjust their prologues. This is very similar
anatofuz
parents:
diff changeset
236 // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
anatofuz
parents:
diff changeset
237 // information.
anatofuz
parents:
diff changeset
238 template <typename ELFT>
anatofuz
parents:
diff changeset
239 void adjustSplitStackFunctionPrologues(uint8_t *buf, uint8_t *end);
anatofuz
parents:
diff changeset
240
anatofuz
parents:
diff changeset
241
anatofuz
parents:
diff changeset
242 template <typename T> llvm::ArrayRef<T> getDataAs() const {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
243 size_t s = content().size();
150
anatofuz
parents:
diff changeset
244 assert(s % sizeof(T) == 0);
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
245 return llvm::ArrayRef<T>((const T *)content().data(), s / sizeof(T));
150
anatofuz
parents:
diff changeset
246 }
anatofuz
parents:
diff changeset
247
anatofuz
parents:
diff changeset
248 protected:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
249 template <typename ELFT>
150
anatofuz
parents:
diff changeset
250 void parseCompressedHeader();
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
251 void decompress() const;
150
anatofuz
parents:
diff changeset
252 };
anatofuz
parents:
diff changeset
253
anatofuz
parents:
diff changeset
254 // SectionPiece represents a piece of splittable section contents.
anatofuz
parents:
diff changeset
255 // We allocate a lot of these and binary search on them. This means that they
anatofuz
parents:
diff changeset
256 // have to be as compact as possible, which is why we don't store the size (can
anatofuz
parents:
diff changeset
257 // be found by looking at the next one).
anatofuz
parents:
diff changeset
258 struct SectionPiece {
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
259 SectionPiece() = default;
150
anatofuz
parents:
diff changeset
260 SectionPiece(size_t off, uint32_t hash, bool live)
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
261 : inputOff(off), live(live), hash(hash >> 1) {}
150
anatofuz
parents:
diff changeset
262
anatofuz
parents:
diff changeset
263 uint32_t inputOff;
anatofuz
parents:
diff changeset
264 uint32_t live : 1;
anatofuz
parents:
diff changeset
265 uint32_t hash : 31;
anatofuz
parents:
diff changeset
266 uint64_t outputOff = 0;
anatofuz
parents:
diff changeset
267 };
anatofuz
parents:
diff changeset
268
anatofuz
parents:
diff changeset
269 static_assert(sizeof(SectionPiece) == 16, "SectionPiece is too big");
anatofuz
parents:
diff changeset
270
anatofuz
parents:
diff changeset
271 // This corresponds to a SHF_MERGE section of an input file.
anatofuz
parents:
diff changeset
272 class MergeInputSection : public InputSectionBase {
anatofuz
parents:
diff changeset
273 public:
anatofuz
parents:
diff changeset
274 template <class ELFT>
anatofuz
parents:
diff changeset
275 MergeInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
anatofuz
parents:
diff changeset
276 StringRef name);
anatofuz
parents:
diff changeset
277 MergeInputSection(uint64_t flags, uint32_t type, uint64_t entsize,
anatofuz
parents:
diff changeset
278 ArrayRef<uint8_t> data, StringRef name);
anatofuz
parents:
diff changeset
279
anatofuz
parents:
diff changeset
280 static bool classof(const SectionBase *s) { return s->kind() == Merge; }
anatofuz
parents:
diff changeset
281 void splitIntoPieces();
anatofuz
parents:
diff changeset
282
anatofuz
parents:
diff changeset
283 // Translate an offset in the input section to an offset in the parent
anatofuz
parents:
diff changeset
284 // MergeSyntheticSection.
anatofuz
parents:
diff changeset
285 uint64_t getParentOffset(uint64_t offset) const;
anatofuz
parents:
diff changeset
286
anatofuz
parents:
diff changeset
287 // Splittable sections are handled as a sequence of data
anatofuz
parents:
diff changeset
288 // rather than a single large blob of data.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
289 SmallVector<SectionPiece, 0> pieces;
150
anatofuz
parents:
diff changeset
290
anatofuz
parents:
diff changeset
291 // Returns I'th piece's data. This function is very hot when
anatofuz
parents:
diff changeset
292 // string merging is enabled, so we want to inline.
anatofuz
parents:
diff changeset
293 LLVM_ATTRIBUTE_ALWAYS_INLINE
anatofuz
parents:
diff changeset
294 llvm::CachedHashStringRef getData(size_t i) const {
anatofuz
parents:
diff changeset
295 size_t begin = pieces[i].inputOff;
anatofuz
parents:
diff changeset
296 size_t end =
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
297 (pieces.size() - 1 == i) ? content().size() : pieces[i + 1].inputOff;
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
298 return {toStringRef(content().slice(begin, end - begin)), pieces[i].hash};
150
anatofuz
parents:
diff changeset
299 }
anatofuz
parents:
diff changeset
300
anatofuz
parents:
diff changeset
301 // Returns the SectionPiece at a given input section offset.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
302 SectionPiece &getSectionPiece(uint64_t offset);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
303 const SectionPiece &getSectionPiece(uint64_t offset) const {
150
anatofuz
parents:
diff changeset
304 return const_cast<MergeInputSection *>(this)->getSectionPiece(offset);
anatofuz
parents:
diff changeset
305 }
anatofuz
parents:
diff changeset
306
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
307 SyntheticSection *getParent() const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
308 return cast_or_null<SyntheticSection>(parent);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
309 }
150
anatofuz
parents:
diff changeset
310
anatofuz
parents:
diff changeset
311 private:
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
312 void splitStrings(StringRef s, size_t size);
150
anatofuz
parents:
diff changeset
313 void splitNonStrings(ArrayRef<uint8_t> a, size_t size);
anatofuz
parents:
diff changeset
314 };
anatofuz
parents:
diff changeset
315
anatofuz
parents:
diff changeset
316 struct EhSectionPiece {
anatofuz
parents:
diff changeset
317 EhSectionPiece(size_t off, InputSectionBase *sec, uint32_t size,
anatofuz
parents:
diff changeset
318 unsigned firstRelocation)
anatofuz
parents:
diff changeset
319 : inputOff(off), sec(sec), size(size), firstRelocation(firstRelocation) {}
anatofuz
parents:
diff changeset
320
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
321 ArrayRef<uint8_t> data() const {
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
322 return {sec->content().data() + this->inputOff, size};
150
anatofuz
parents:
diff changeset
323 }
anatofuz
parents:
diff changeset
324
anatofuz
parents:
diff changeset
325 size_t inputOff;
anatofuz
parents:
diff changeset
326 ssize_t outputOff = -1;
anatofuz
parents:
diff changeset
327 InputSectionBase *sec;
anatofuz
parents:
diff changeset
328 uint32_t size;
anatofuz
parents:
diff changeset
329 unsigned firstRelocation;
anatofuz
parents:
diff changeset
330 };
anatofuz
parents:
diff changeset
331
anatofuz
parents:
diff changeset
332 // This corresponds to a .eh_frame section of an input file.
anatofuz
parents:
diff changeset
333 class EhInputSection : public InputSectionBase {
anatofuz
parents:
diff changeset
334 public:
anatofuz
parents:
diff changeset
335 template <class ELFT>
anatofuz
parents:
diff changeset
336 EhInputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
anatofuz
parents:
diff changeset
337 StringRef name);
anatofuz
parents:
diff changeset
338 static bool classof(const SectionBase *s) { return s->kind() == EHFrame; }
anatofuz
parents:
diff changeset
339 template <class ELFT> void split();
anatofuz
parents:
diff changeset
340 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> rels);
anatofuz
parents:
diff changeset
341
anatofuz
parents:
diff changeset
342 // Splittable sections are handled as a sequence of data
anatofuz
parents:
diff changeset
343 // rather than a single large blob of data.
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
344 SmallVector<EhSectionPiece, 0> cies, fdes;
150
anatofuz
parents:
diff changeset
345
anatofuz
parents:
diff changeset
346 SyntheticSection *getParent() const;
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
347 uint64_t getParentOffset(uint64_t offset) const;
150
anatofuz
parents:
diff changeset
348 };
anatofuz
parents:
diff changeset
349
anatofuz
parents:
diff changeset
350 // This is a section that is added directly to an output section
anatofuz
parents:
diff changeset
351 // instead of needing special combination via a synthetic section. This
anatofuz
parents:
diff changeset
352 // includes all input sections with the exceptions of SHF_MERGE and
anatofuz
parents:
diff changeset
353 // .eh_frame. It also includes the synthetic sections themselves.
anatofuz
parents:
diff changeset
354 class InputSection : public InputSectionBase {
anatofuz
parents:
diff changeset
355 public:
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
356 InputSection(InputFile *f, uint64_t flags, uint32_t type, uint32_t addralign,
150
anatofuz
parents:
diff changeset
357 ArrayRef<uint8_t> data, StringRef name, Kind k = Regular);
anatofuz
parents:
diff changeset
358 template <class ELFT>
anatofuz
parents:
diff changeset
359 InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header,
anatofuz
parents:
diff changeset
360 StringRef name);
anatofuz
parents:
diff changeset
361
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
362 static bool classof(const SectionBase *s) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
363 return s->kind() == SectionBase::Regular ||
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
364 s->kind() == SectionBase::Synthetic;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
365 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
366
150
anatofuz
parents:
diff changeset
367 // Write this section to a mmap'ed file, assuming Buf is pointing to
anatofuz
parents:
diff changeset
368 // beginning of the output section.
anatofuz
parents:
diff changeset
369 template <class ELFT> void writeTo(uint8_t *buf);
anatofuz
parents:
diff changeset
370
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
371 OutputSection *getParent() const {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
372 return reinterpret_cast<OutputSection *>(parent);
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
373 }
150
anatofuz
parents:
diff changeset
374
anatofuz
parents:
diff changeset
375 // This variable has two usages. Initially, it represents an index in the
anatofuz
parents:
diff changeset
376 // OutputSection's InputSection list, and is used when ordering SHF_LINK_ORDER
anatofuz
parents:
diff changeset
377 // sections. After assignAddresses is called, it represents the offset from
anatofuz
parents:
diff changeset
378 // the beginning of the output section this section was assigned to.
anatofuz
parents:
diff changeset
379 uint64_t outSecOff = 0;
anatofuz
parents:
diff changeset
380
anatofuz
parents:
diff changeset
381 InputSectionBase *getRelocatedSection() const;
anatofuz
parents:
diff changeset
382
anatofuz
parents:
diff changeset
383 template <class ELFT, class RelTy>
anatofuz
parents:
diff changeset
384 void relocateNonAlloc(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
anatofuz
parents:
diff changeset
385
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
386 // Points to the canonical section. If ICF folds two sections, repl pointer of
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
387 // one section points to the other.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
388 InputSection *repl = this;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
389
150
anatofuz
parents:
diff changeset
390 // Used by ICF.
anatofuz
parents:
diff changeset
391 uint32_t eqClass[2] = {0, 0};
anatofuz
parents:
diff changeset
392
anatofuz
parents:
diff changeset
393 // Called by ICF to merge two input sections.
anatofuz
parents:
diff changeset
394 void replace(InputSection *other);
anatofuz
parents:
diff changeset
395
anatofuz
parents:
diff changeset
396 static InputSection discarded;
anatofuz
parents:
diff changeset
397
anatofuz
parents:
diff changeset
398 private:
anatofuz
parents:
diff changeset
399 template <class ELFT, class RelTy>
anatofuz
parents:
diff changeset
400 void copyRelocations(uint8_t *buf, llvm::ArrayRef<RelTy> rels);
anatofuz
parents:
diff changeset
401
anatofuz
parents:
diff changeset
402 template <class ELFT> void copyShtGroup(uint8_t *buf);
anatofuz
parents:
diff changeset
403 };
anatofuz
parents:
diff changeset
404
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
405 static_assert(sizeof(InputSection) <= 160, "InputSection is too big");
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
406
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
407 class SyntheticSection : public InputSection {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
408 public:
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
409 SyntheticSection(uint64_t flags, uint32_t type, uint32_t addralign,
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
410 StringRef name)
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
411 : InputSection(nullptr, flags, type, addralign, {}, name,
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
412 InputSectionBase::Synthetic) {}
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
413
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
414 virtual ~SyntheticSection() = default;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
415 virtual size_t getSize() const = 0;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
416 virtual bool updateAllocSize() { return false; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
417 // If the section has the SHF_ALLOC flag and the size may be changed if
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
418 // thunks are added, update the section size.
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
419 virtual bool isNeeded() const { return true; }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
420 virtual void finalizeContents() {}
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
421 virtual void writeTo(uint8_t *buf) = 0;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
422
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
423 static bool classof(const SectionBase *sec) {
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
424 return sec->kind() == InputSectionBase::Synthetic;
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
425 }
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
426 };
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
427
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
428 inline bool isDebugSection(const InputSectionBase &sec) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
429 return (sec.flags & llvm::ELF::SHF_ALLOC) == 0 &&
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
430 sec.name.starts_with(".debug");
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
431 }
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
432
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
433 // The set of TOC entries (.toc + addend) for which we should not apply
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
434 // toc-indirect to toc-relative relaxation. const Symbol * refers to the
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
435 // STT_SECTION symbol associated to the .toc input section.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
436 extern llvm::DenseSet<std::pair<const Symbol *, uint64_t>> ppc64noTocRelax;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
437
150
anatofuz
parents:
diff changeset
438 } // namespace elf
anatofuz
parents:
diff changeset
439
anatofuz
parents:
diff changeset
440 std::string toString(const elf::InputSectionBase *);
anatofuz
parents:
diff changeset
441 } // namespace lld
anatofuz
parents:
diff changeset
442
anatofuz
parents:
diff changeset
443 #endif