1 //===- Relocations.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_RELOCATIONS_H
10 #define LLD_ELF_RELOCATIONS_H
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/Object/ELFTypes.h"
23 class InputSectionBase
;
25 class RelocationBaseSection
;
28 // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
31 /*implicit*/ constexpr RelType(uint32_t v
= 0) : v(v
) {}
32 /*implicit*/ operator uint32_t() const { return v
; }
35 using JumpModType
= uint32_t;
37 // List of target-independent relocation types. Relocations read
38 // from files are converted to these types so that the main code
39 // doesn't have to know about architecture-specific details.
64 R_RELAX_TLS_GD_TO_IE_ABS
,
65 R_RELAX_TLS_GD_TO_IE_GOT_OFF
,
66 R_RELAX_TLS_GD_TO_IE_GOTPLT
,
68 R_RELAX_TLS_GD_TO_LE_NEG
,
71 R_RELAX_TLS_LD_TO_LE_ABS
,
89 // The following is abstract relocation types used for only one target.
91 // Even though RelExpr is intended to be a target-neutral representation
92 // of a relocation type, there are some relocations whose semantics are
93 // unique to a target. Such relocation are marked with RE_<TARGET_NAME>.
94 RE_AARCH64_GOT_PAGE_PC
,
95 RE_AARCH64_AUTH_GOT_PAGE_PC
,
98 RE_AARCH64_AUTH_GOT_PC
,
100 RE_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC
,
101 RE_AARCH64_TLSDESC_PAGE
,
102 RE_AARCH64_AUTH_TLSDESC_PAGE
,
103 RE_AARCH64_AUTH_TLSDESC
,
110 RE_MIPS_GOT_LOCAL_PAGE
,
120 RE_PPC64_RELAX_GOT_PC
,
123 RE_RISCV_PC_INDIRECT
,
124 // Same as R_PC but with page-aligned semantics.
125 RE_LOONGARCH_PAGE_PC
,
126 // Same as R_PLT_PC but with page-aligned semantics.
127 RE_LOONGARCH_PLT_PAGE_PC
,
128 // In addition to having page-aligned semantics, LoongArch GOT relocs are
129 // also reused for TLS, making the semantics differ from other architectures.
131 RE_LOONGARCH_GOT_PAGE_PC
,
132 RE_LOONGARCH_TLSGD_PAGE_PC
,
133 RE_LOONGARCH_TLSDESC_PAGE_PC
,
136 // Architecture-neutral representation of relocation.
145 // Manipulate jump instructions with these modifiers. These are used to relax
146 // jump instruction opcodes at basic block boundaries and are particularly
147 // useful when basic block sections are enabled.
148 struct JumpInstrMod
{
150 JumpModType original
;
154 // This function writes undefined symbol diagnostics to an internal buffer.
155 // Call reportUndefinedSymbols() after calling scanRelocations() to emit
157 template <class ELFT
> void scanRelocations(Ctx
&ctx
);
158 template <class ELFT
> void checkNoCrossRefs(Ctx
&ctx
);
159 void reportUndefinedSymbols(Ctx
&);
160 void postScanRelocations(Ctx
&ctx
);
161 void addGotEntry(Ctx
&ctx
, Symbol
&sym
);
163 void hexagonTLSSymbolUpdate(Ctx
&ctx
);
164 bool hexagonNeedsTLSSymbol(ArrayRef
<OutputSection
*> outputSections
);
168 class InputSectionDescription
;
172 // Thunk may be incomplete. Avoid inline ctor/dtor.
173 ThunkCreator(Ctx
&ctx
);
175 // Return true if Thunks have been added to OutputSections
176 bool createThunks(uint32_t pass
, ArrayRef
<OutputSection
*> outputSections
);
179 void mergeThunks(ArrayRef
<OutputSection
*> outputSections
);
181 ThunkSection
*getISDThunkSec(OutputSection
*os
, InputSection
*isec
,
182 InputSectionDescription
*isd
,
183 const Relocation
&rel
, uint64_t src
);
185 ThunkSection
*getISThunkSec(InputSection
*isec
);
187 void createInitialThunkSections(ArrayRef
<OutputSection
*> outputSections
);
189 std::pair
<Thunk
*, bool> getThunk(InputSection
*isec
, Relocation
&rel
,
192 std::pair
<Thunk
*, bool> getSyntheticLandingPad(Defined
&d
, int64_t a
);
194 ThunkSection
*addThunkSection(OutputSection
*os
, InputSectionDescription
*,
197 bool normalizeExistingThunk(Relocation
&rel
, uint64_t src
);
199 bool addSyntheticLandingPads();
203 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
204 // is represented as a (section, offset) pair. There may be multiple
205 // relocations sharing the same (section, offset + addend) pair. We may revert
206 // a relocation back to its original non-Thunk target, and restore the
207 // original addend, so we cannot fold offset + addend. A nested pair is used
208 // because DenseMapInfo is not specialized for std::tuple.
209 llvm::DenseMap
<std::pair
<std::pair
<SectionBase
*, uint64_t>, int64_t>,
210 SmallVector
<std::unique_ptr
<Thunk
>, 0>>
211 thunkedSymbolsBySectionAndAddend
;
212 llvm::DenseMap
<std::pair
<Symbol
*, int64_t>,
213 SmallVector
<std::unique_ptr
<Thunk
>, 0>>
216 // Find a Thunk from the Thunks symbol definition, we can use this to find
217 // the Thunk from a relocation to the Thunks symbol definition.
218 llvm::DenseMap
<Symbol
*, Thunk
*> thunks
;
220 // Track InputSections that have an inline ThunkSection placed in front
221 // an inline ThunkSection may have control fall through to the section below
222 // so we need to make sure that there is only one of them.
223 // The Mips LA25 Thunk is an example of an inline ThunkSection, as is
224 // the AArch64BTLandingPadThunk.
225 llvm::DenseMap
<InputSection
*, ThunkSection
*> thunkedSections
;
227 // Record landing pads, generated for a section + offset destination.
228 // Landling pads are alternative entry points for destinations that need
229 // to be reached via thunks that use indirect branches. A destination
230 // needs at most one landing pad as that can be reused by all callers.
231 llvm::DenseMap
<std::pair
<std::pair
<SectionBase
*, uint64_t>, int64_t>,
232 std::unique_ptr
<Thunk
>>
233 landingPadsBySectionAndAddend
;
235 // All the nonLandingPad thunks that have been created, in order of creation.
236 std::vector
<Thunk
*> allThunks
;
238 // The number of completed passes of createThunks this permits us
239 // to do one time initialization on Pass 0 and put a limit on the
240 // number of times it can be called to prevent infinite loops.
244 // Decode LEB128 without error checking. Only used by performance critical code
246 inline uint64_t readLEB128(const uint8_t *&p
, uint64_t leb
) {
247 uint64_t acc
= 0, shift
= 0, byte
;
250 acc
|= (byte
- 128 * (byte
>= leb
)) << shift
;
252 } while (byte
>= 128);
255 inline uint64_t readULEB128(const uint8_t *&p
) { return readLEB128(p
, 128); }
256 inline int64_t readSLEB128(const uint8_t *&p
) { return readLEB128(p
, 64); }
258 // This class implements a CREL iterator that does not allocate extra memory.
259 template <bool is64
> struct RelocsCrel
{
260 using uint
= std::conditional_t
<is64
, uint64_t, uint32_t>;
261 struct const_iterator
{
262 using iterator_category
= std::forward_iterator_tag
;
263 using value_type
= llvm::object::Elf_Crel_Impl
<is64
>;
264 using difference_type
= ptrdiff_t;
265 using pointer
= value_type
*;
266 using reference
= const value_type
&;
268 uint8_t flagBits
, shift
;
270 llvm::object::Elf_Crel_Impl
<is64
> crel
{};
271 const_iterator(size_t hdr
, const uint8_t *p
)
272 : count(hdr
/ 8), flagBits(hdr
& 4 ? 3 : 2), shift(hdr
% 4), p(p
) {
277 // See object::decodeCrel.
278 const uint8_t b
= *p
++;
279 crel
.r_offset
+= b
>> flagBits
<< shift
;
282 ((readULEB128(p
) << (7 - flagBits
)) - (0x80 >> flagBits
)) << shift
;
284 crel
.r_symidx
+= readSLEB128(p
);
286 crel
.r_type
+= readSLEB128(p
);
287 if (b
& 4 && flagBits
== 3)
288 crel
.r_addend
+= static_cast<uint
>(readSLEB128(p
));
290 llvm::object::Elf_Crel_Impl
<is64
> operator*() const { return crel
; };
291 const llvm::object::Elf_Crel_Impl
<is64
> *operator->() const {
294 // For llvm::enumerate.
295 bool operator==(const const_iterator
&r
) const { return count
== r
.count
; }
296 bool operator!=(const const_iterator
&r
) const { return count
!= r
.count
; }
297 const_iterator
&operator++() {
302 // For RelocationScanner::scanOne.
303 void operator+=(size_t n
) {
310 const uint8_t *p
= nullptr;
312 constexpr RelocsCrel() = default;
313 RelocsCrel(const uint8_t *p
) : hdr(readULEB128(p
)) { this->p
= p
; }
314 size_t size() const { return hdr
/ 8; }
315 const_iterator
begin() const { return {hdr
, p
}; }
316 const_iterator
end() const { return {0, nullptr}; }
319 template <class RelTy
> struct Relocs
: ArrayRef
<RelTy
> {
321 Relocs(ArrayRef
<RelTy
> a
) : ArrayRef
<RelTy
>(a
) {}
325 struct Relocs
<llvm::object::Elf_Crel_Impl
<is64
>> : RelocsCrel
<is64
> {
326 using RelocsCrel
<is64
>::RelocsCrel
;
329 // Return a int64_t to make sure we get the sign extension out of the way as
330 // early as possible.
331 template <class ELFT
>
332 static inline int64_t getAddend(const typename
ELFT::Rel
&rel
) {
335 template <class ELFT
>
336 static inline int64_t getAddend(const typename
ELFT::Rela
&rel
) {
339 template <class ELFT
>
340 static inline int64_t getAddend(const typename
ELFT::Crel
&rel
) {
344 template <typename RelTy
>
345 inline Relocs
<RelTy
> sortRels(Relocs
<RelTy
> rels
,
346 SmallVector
<RelTy
, 0> &storage
) {
347 auto cmp
= [](const RelTy
&a
, const RelTy
&b
) {
348 return a
.r_offset
< b
.r_offset
;
350 if (!llvm::is_sorted(rels
, cmp
)) {
351 storage
.assign(rels
.begin(), rels
.end());
352 llvm::stable_sort(storage
, cmp
);
353 rels
= Relocs
<RelTy
>(storage
);
359 inline Relocs
<llvm::object::Elf_Crel_Impl
<is64
>>
360 sortRels(Relocs
<llvm::object::Elf_Crel_Impl
<is64
>> rels
,
361 SmallVector
<llvm::object::Elf_Crel_Impl
<is64
>, 0> &storage
) {
365 RelocationBaseSection
&getIRelativeSection(Ctx
&ctx
);
367 // Returns true if Expr refers a GOT entry. Note that this function returns
368 // false for TLS variables even though they need GOT, because TLS variables uses
369 // GOT differently than the regular variables.
370 bool needsGot(RelExpr expr
);
371 } // namespace lld::elf