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"
20 class InputSectionBase
;
24 // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
25 using RelType
= uint32_t;
26 using JumpModType
= uint32_t;
28 // List of target-independent relocation types. Relocations read
29 // from files are converted to these types so that the main code
30 // doesn't have to know about architecture-specific details.
55 R_RELAX_TLS_GD_TO_IE_ABS
,
56 R_RELAX_TLS_GD_TO_IE_GOT_OFF
,
57 R_RELAX_TLS_GD_TO_IE_GOTPLT
,
59 R_RELAX_TLS_GD_TO_LE_NEG
,
62 R_RELAX_TLS_LD_TO_LE_ABS
,
80 // The following is abstract relocation types used for only one target.
82 // Even though RelExpr is intended to be a target-neutral representation
83 // of a relocation type, there are some relocations whose semantics are
84 // unique to a target. Such relocation are marked with R_<TARGET_NAME>.
85 R_AARCH64_GOT_PAGE_PC
,
88 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC
,
89 R_AARCH64_TLSDESC_PAGE
,
96 R_MIPS_GOT_LOCAL_PAGE
,
106 R_PPC64_RELAX_GOT_PC
,
110 // Same as R_PC but with page-aligned semantics.
112 // Same as R_PLT_PC but with page-aligned semantics.
113 R_LOONGARCH_PLT_PAGE_PC
,
114 // In addition to having page-aligned semantics, LoongArch GOT relocs are
115 // also reused for TLS, making the semantics differ from other architectures.
117 R_LOONGARCH_GOT_PAGE_PC
,
118 R_LOONGARCH_TLSGD_PAGE_PC
,
119 R_LOONGARCH_TLSDESC_PAGE_PC
,
122 // Architecture-neutral representation of relocation.
131 // Manipulate jump instructions with these modifiers. These are used to relax
132 // jump instruction opcodes at basic block boundaries and are particularly
133 // useful when basic block sections are enabled.
134 struct JumpInstrMod
{
136 JumpModType original
;
140 // This function writes undefined symbol diagnostics to an internal buffer.
141 // Call reportUndefinedSymbols() after calling scanRelocations() to emit
143 template <class ELFT
> void scanRelocations();
144 void reportUndefinedSymbols();
145 void postScanRelocations();
146 void addGotEntry(Symbol
&sym
);
148 void hexagonTLSSymbolUpdate(ArrayRef
<OutputSection
*> outputSections
);
149 bool hexagonNeedsTLSSymbol(ArrayRef
<OutputSection
*> outputSections
);
153 class InputSectionDescription
;
157 // Return true if Thunks have been added to OutputSections
158 bool createThunks(uint32_t pass
, ArrayRef
<OutputSection
*> outputSections
);
161 void mergeThunks(ArrayRef
<OutputSection
*> outputSections
);
163 ThunkSection
*getISDThunkSec(OutputSection
*os
, InputSection
*isec
,
164 InputSectionDescription
*isd
,
165 const Relocation
&rel
, uint64_t src
);
167 ThunkSection
*getISThunkSec(InputSection
*isec
);
169 void createInitialThunkSections(ArrayRef
<OutputSection
*> outputSections
);
171 std::pair
<Thunk
*, bool> getThunk(InputSection
*isec
, Relocation
&rel
,
174 ThunkSection
*addThunkSection(OutputSection
*os
, InputSectionDescription
*,
177 bool normalizeExistingThunk(Relocation
&rel
, uint64_t src
);
179 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
180 // is represented as a (section, offset) pair. There may be multiple
181 // relocations sharing the same (section, offset + addend) pair. We may revert
182 // a relocation back to its original non-Thunk target, and restore the
183 // original addend, so we cannot fold offset + addend. A nested pair is used
184 // because DenseMapInfo is not specialized for std::tuple.
185 llvm::DenseMap
<std::pair
<std::pair
<SectionBase
*, uint64_t>, int64_t>,
186 std::vector
<Thunk
*>>
187 thunkedSymbolsBySectionAndAddend
;
188 llvm::DenseMap
<std::pair
<Symbol
*, int64_t>, std::vector
<Thunk
*>>
191 // Find a Thunk from the Thunks symbol definition, we can use this to find
192 // the Thunk from a relocation to the Thunks symbol definition.
193 llvm::DenseMap
<Symbol
*, Thunk
*> thunks
;
195 // Track InputSections that have an inline ThunkSection placed in front
196 // an inline ThunkSection may have control fall through to the section below
197 // so we need to make sure that there is only one of them.
198 // The Mips LA25 Thunk is an example of an inline ThunkSection.
199 llvm::DenseMap
<InputSection
*, ThunkSection
*> thunkedSections
;
201 // The number of completed passes of createThunks this permits us
202 // to do one time initialization on Pass 0 and put a limit on the
203 // number of times it can be called to prevent infinite loops.
207 // Return a int64_t to make sure we get the sign extension out of the way as
208 // early as possible.
209 template <class ELFT
>
210 static inline int64_t getAddend(const typename
ELFT::Rel
&rel
) {
213 template <class ELFT
>
214 static inline int64_t getAddend(const typename
ELFT::Rela
&rel
) {
218 template <typename RelTy
>
219 ArrayRef
<RelTy
> sortRels(ArrayRef
<RelTy
> rels
, SmallVector
<RelTy
, 0> &storage
) {
220 auto cmp
= [](const RelTy
&a
, const RelTy
&b
) {
221 return a
.r_offset
< b
.r_offset
;
223 if (!llvm::is_sorted(rels
, cmp
)) {
224 storage
.assign(rels
.begin(), rels
.end());
225 llvm::stable_sort(storage
, cmp
);
231 // Returns true if Expr refers a GOT entry. Note that this function returns
232 // false for TLS variables even though they need GOT, because TLS variables uses
233 // GOT differently than the regular variables.
234 bool needsGot(RelExpr expr
);
235 } // namespace lld::elf