annotate lld/ELF/Relocations.h @ 213:25ca0248ac32

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