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