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.
52 R_RELAX_TLS_GD_TO_IE_ABS
,
53 R_RELAX_TLS_GD_TO_IE_GOT_OFF
,
54 R_RELAX_TLS_GD_TO_IE_GOTPLT
,
56 R_RELAX_TLS_GD_TO_LE_NEG
,
59 R_RELAX_TLS_LD_TO_LE_ABS
,
77 // The following is abstract relocation types used for only one target.
79 // Even though RelExpr is intended to be a target-neutral representation
80 // of a relocation type, there are some relocations whose semantics are
81 // unique to a target. Such relocation are marked with R_<TARGET_NAME>.
82 R_AARCH64_GOT_PAGE_PC
,
85 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC
,
86 R_AARCH64_TLSDESC_PAGE
,
92 R_MIPS_GOT_LOCAL_PAGE
,
102 R_PPC64_RELAX_GOT_PC
,
107 // Architecture-neutral representation of relocation.
116 // Manipulate jump instructions with these modifiers. These are used to relax
117 // jump instruction opcodes at basic block boundaries and are particularly
118 // useful when basic block sections are enabled.
119 struct JumpInstrMod
{
121 JumpModType original
;
125 // This function writes undefined symbol diagnostics to an internal buffer.
126 // Call reportUndefinedSymbols() after calling scanRelocations() to emit
128 template <class ELFT
> void scanRelocations();
129 void reportUndefinedSymbols();
130 void postScanRelocations();
132 void hexagonTLSSymbolUpdate(ArrayRef
<OutputSection
*> outputSections
);
133 bool hexagonNeedsTLSSymbol(ArrayRef
<OutputSection
*> outputSections
);
137 class InputSectionDescription
;
141 // Return true if Thunks have been added to OutputSections
142 bool createThunks(uint32_t pass
, ArrayRef
<OutputSection
*> outputSections
);
145 void mergeThunks(ArrayRef
<OutputSection
*> outputSections
);
147 ThunkSection
*getISDThunkSec(OutputSection
*os
, InputSection
*isec
,
148 InputSectionDescription
*isd
,
149 const Relocation
&rel
, uint64_t src
);
151 ThunkSection
*getISThunkSec(InputSection
*isec
);
153 void createInitialThunkSections(ArrayRef
<OutputSection
*> outputSections
);
155 std::pair
<Thunk
*, bool> getThunk(InputSection
*isec
, Relocation
&rel
,
158 ThunkSection
*addThunkSection(OutputSection
*os
, InputSectionDescription
*,
161 bool normalizeExistingThunk(Relocation
&rel
, uint64_t src
);
163 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
164 // is represented as a (section, offset) pair. There may be multiple
165 // relocations sharing the same (section, offset + addend) pair. We may revert
166 // a relocation back to its original non-Thunk target, and restore the
167 // original addend, so we cannot fold offset + addend. A nested pair is used
168 // because DenseMapInfo is not specialized for std::tuple.
169 llvm::DenseMap
<std::pair
<std::pair
<SectionBase
*, uint64_t>, int64_t>,
170 std::vector
<Thunk
*>>
171 thunkedSymbolsBySectionAndAddend
;
172 llvm::DenseMap
<std::pair
<Symbol
*, int64_t>, std::vector
<Thunk
*>>
175 // Find a Thunk from the Thunks symbol definition, we can use this to find
176 // the Thunk from a relocation to the Thunks symbol definition.
177 llvm::DenseMap
<Symbol
*, Thunk
*> thunks
;
179 // Track InputSections that have an inline ThunkSection placed in front
180 // an inline ThunkSection may have control fall through to the section below
181 // so we need to make sure that there is only one of them.
182 // The Mips LA25 Thunk is an example of an inline ThunkSection.
183 llvm::DenseMap
<InputSection
*, ThunkSection
*> thunkedSections
;
185 // The number of completed passes of createThunks this permits us
186 // to do one time initialization on Pass 0 and put a limit on the
187 // number of times it can be called to prevent infinite loops.
191 // Return a int64_t to make sure we get the sign extension out of the way as
192 // early as possible.
193 template <class ELFT
>
194 static inline int64_t getAddend(const typename
ELFT::Rel
&rel
) {
197 template <class ELFT
>
198 static inline int64_t getAddend(const typename
ELFT::Rela
&rel
) {
202 template <typename RelTy
>
203 ArrayRef
<RelTy
> sortRels(ArrayRef
<RelTy
> rels
, SmallVector
<RelTy
, 0> &storage
) {
204 auto cmp
= [](const RelTy
&a
, const RelTy
&b
) {
205 return a
.r_offset
< b
.r_offset
;
207 if (!llvm::is_sorted(rels
, cmp
)) {
208 storage
.assign(rels
.begin(), rels
.end());
209 llvm::stable_sort(storage
, cmp
);
214 } // namespace lld::elf