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