1 //===- Relocations.h -------------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLD_ELF_RELOCATIONS_H
10 #define LLD_ELF_RELOCATIONS_H
11
12 #include "lld/Common/LLVM.h"
13 #include "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include <vector>
16
17 namespace lld {
18 namespace elf {
19 class Symbol;
20 class InputSection;
21 class InputSectionBase;
22 class OutputSection;
23 class SectionBase;
24
25 // Represents a relocation type, such as R_X86_64_PC32 or R_ARM_THM_CALL.
26 using RelType = uint32_t;
27 using JumpModType = uint32_t;
28
29 // List of target-independent relocation types. Relocations read
30 // from files are converted to these types so that the main code
31 // doesn't have to know about architecture-specific details.
32 enum RelExpr {
33 R_ABS,
34 R_ADDEND,
35 R_DTPREL,
36 R_GOT,
37 R_GOT_OFF,
38 R_GOT_PC,
39 R_GOTONLY_PC,
40 R_GOTPLTONLY_PC,
41 R_GOTPLT,
42 R_GOTPLTREL,
43 R_GOTREL,
44 R_NONE,
45 R_PC,
46 R_PLT,
47 R_PLT_PC,
48 R_PLT_GOTPLT,
49 R_RELAX_HINT,
50 R_RELAX_GOT_PC,
51 R_RELAX_GOT_PC_NOPIC,
52 R_RELAX_TLS_GD_TO_IE,
53 R_RELAX_TLS_GD_TO_IE_ABS,
54 R_RELAX_TLS_GD_TO_IE_GOT_OFF,
55 R_RELAX_TLS_GD_TO_IE_GOTPLT,
56 R_RELAX_TLS_GD_TO_LE,
57 R_RELAX_TLS_GD_TO_LE_NEG,
58 R_RELAX_TLS_IE_TO_LE,
59 R_RELAX_TLS_LD_TO_LE,
60 R_RELAX_TLS_LD_TO_LE_ABS,
61 R_SIZE,
62 R_TPREL,
63 R_TPREL_NEG,
64 R_TLSDESC,
65 R_TLSDESC_CALL,
66 R_TLSDESC_PC,
67 R_TLSDESC_GOTPLT,
68 R_TLSGD_GOT,
69 R_TLSGD_GOTPLT,
70 R_TLSGD_PC,
71 R_TLSIE_HINT,
72 R_TLSLD_GOT,
73 R_TLSLD_GOTPLT,
74 R_TLSLD_GOT_OFF,
75 R_TLSLD_HINT,
76 R_TLSLD_PC,
77
78 // The following is abstract relocation types used for only one target.
79 //
80 // Even though RelExpr is intended to be a target-neutral representation
81 // of a relocation type, there are some relocations whose semantics are
82 // unique to a target. Such relocation are marked with R_<TARGET_NAME>.
83 R_AARCH64_GOT_PAGE_PC,
84 R_AARCH64_GOT_PAGE,
85 R_AARCH64_PAGE_PC,
86 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
87 R_AARCH64_TLSDESC_PAGE,
88 R_ARM_PCA,
89 R_ARM_SBREL,
90 R_MIPS_GOTREL,
91 R_MIPS_GOT_GP,
92 R_MIPS_GOT_GP_PC,
93 R_MIPS_GOT_LOCAL_PAGE,
94 R_MIPS_GOT_OFF,
95 R_MIPS_GOT_OFF32,
96 R_MIPS_TLSGD,
97 R_MIPS_TLSLD,
98 R_PPC32_PLTREL,
99 R_PPC64_CALL,
100 R_PPC64_CALL_PLT,
101 R_PPC64_RELAX_TOC,
102 R_PPC64_TOCBASE,
103 R_PPC64_RELAX_GOT_PC,
104 R_RISCV_ADD,
105 R_RISCV_PC_INDIRECT,
106 };
107
108 // Architecture-neutral representation of relocation.
109 struct Relocation {
110 RelExpr expr;
111 RelType type;
112 uint64_t offset;
113 int64_t addend;
114 Symbol *sym;
115 };
116
117 // Manipulate jump instructions with these modifiers. These are used to relax
118 // jump instruction opcodes at basic block boundaries and are particularly
119 // useful when basic block sections are enabled.
120 struct JumpInstrMod {
121 uint64_t offset;
122 JumpModType original;
123 unsigned size;
124 };
125
126 // This function writes undefined symbol diagnostics to an internal buffer.
127 // Call reportUndefinedSymbols() after calling scanRelocations() to emit
128 // the diagnostics.
129 template <class ELFT> void scanRelocations(InputSectionBase &);
130 void reportUndefinedSymbols();
131 void postScanRelocations();
132
133 void hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections);
134 bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);
135
136 class ThunkSection;
137 class Thunk;
138 class InputSectionDescription;
139
140 class ThunkCreator {
141 public:
142 // Return true if Thunks have been added to OutputSections
143 bool createThunks(uint32_t pass, ArrayRef<OutputSection *> outputSections);
144
145 private:
146 void mergeThunks(ArrayRef<OutputSection *> outputSections);
147
148 ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,
149 InputSectionDescription *isd,
150 const Relocation &rel, uint64_t src);
151
152 ThunkSection *getISThunkSec(InputSection *isec);
153
154 void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);
155
156 std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,
157 uint64_t src);
158
159 ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,
160 uint64_t off);
161
162 bool normalizeExistingThunk(Relocation &rel, uint64_t src);
163
164 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
165 // is represented as a (section, offset) pair. There may be multiple
166 // relocations sharing the same (section, offset + addend) pair. We may revert
167 // a relocation back to its original non-Thunk target, and restore the
168 // original addend, so we cannot fold offset + addend. A nested pair is used
169 // because DenseMapInfo is not specialized for std::tuple.
170 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
171 std::vector<Thunk *>>
172 thunkedSymbolsBySectionAndAddend;
173 llvm::DenseMap<std::pair<Symbol *, int64_t>, std::vector<Thunk *>>
174 thunkedSymbols;
175
176 // Find a Thunk from the Thunks symbol definition, we can use this to find
177 // the Thunk from a relocation to the Thunks symbol definition.
178 llvm::DenseMap<Symbol *, Thunk *> thunks;
179
180 // Track InputSections that have an inline ThunkSection placed in front
181 // an inline ThunkSection may have control fall through to the section below
182 // so we need to make sure that there is only one of them.
183 // The Mips LA25 Thunk is an example of an inline ThunkSection.
184 llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;
185
186 // The number of completed passes of createThunks this permits us
187 // to do one time initialization on Pass 0 and put a limit on the
188 // number of times it can be called to prevent infinite loops.
189 uint32_t pass = 0;
190 };
191
192 // Return a int64_t to make sure we get the sign extension out of the way as
193 // early as possible.
194 template <class ELFT>
getAddend(const typename ELFT::Rel & rel)195 static inline int64_t getAddend(const typename ELFT::Rel &rel) {
196 return 0;
197 }
198 template <class ELFT>
getAddend(const typename ELFT::Rela & rel)199 static inline int64_t getAddend(const typename ELFT::Rela &rel) {
200 return rel.r_addend;
201 }
202
203 template <typename RelTy>
sortRels(ArrayRef<RelTy> rels,SmallVector<RelTy,0> & storage)204 ArrayRef<RelTy> sortRels(ArrayRef<RelTy> rels, SmallVector<RelTy, 0> &storage) {
205 auto cmp = [](const RelTy &a, const RelTy &b) {
206 return a.r_offset < b.r_offset;
207 };
208 if (!llvm::is_sorted(rels, cmp)) {
209 storage.assign(rels.begin(), rels.end());
210 llvm::stable_sort(storage, cmp);
211 rels = storage;
212 }
213 return rels;
214 }
215 } // namespace elf
216 } // namespace lld
217
218 #endif
219