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