annotate lld/ELF/Relocations.h @ 201:a96fbbdf2d0f

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 04 Jun 2021 21:07:06 +0900
parents 0572611fdcc8
children 2e18cbf3894f
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_NEG_TLS,
anatofuz
parents:
diff changeset
45 R_NONE,
anatofuz
parents:
diff changeset
46 R_PC,
anatofuz
parents:
diff changeset
47 R_PLT,
anatofuz
parents:
diff changeset
48 R_PLT_PC,
anatofuz
parents:
diff changeset
49 R_RELAX_GOT_PC,
anatofuz
parents:
diff changeset
50 R_RELAX_GOT_PC_NOPIC,
anatofuz
parents:
diff changeset
51 R_RELAX_TLS_GD_TO_IE,
anatofuz
parents:
diff changeset
52 R_RELAX_TLS_GD_TO_IE_ABS,
anatofuz
parents:
diff changeset
53 R_RELAX_TLS_GD_TO_IE_GOT_OFF,
anatofuz
parents:
diff changeset
54 R_RELAX_TLS_GD_TO_IE_GOTPLT,
anatofuz
parents:
diff changeset
55 R_RELAX_TLS_GD_TO_LE,
anatofuz
parents:
diff changeset
56 R_RELAX_TLS_GD_TO_LE_NEG,
anatofuz
parents:
diff changeset
57 R_RELAX_TLS_IE_TO_LE,
anatofuz
parents:
diff changeset
58 R_RELAX_TLS_LD_TO_LE,
anatofuz
parents:
diff changeset
59 R_RELAX_TLS_LD_TO_LE_ABS,
anatofuz
parents:
diff changeset
60 R_SIZE,
anatofuz
parents:
diff changeset
61 R_TLS,
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,
anatofuz
parents:
diff changeset
81 R_AARCH64_PAGE_PC,
anatofuz
parents:
diff changeset
82 R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC,
anatofuz
parents:
diff changeset
83 R_AARCH64_TLSDESC_PAGE,
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
84 R_ARM_PCA,
150
anatofuz
parents:
diff changeset
85 R_ARM_SBREL,
anatofuz
parents:
diff changeset
86 R_MIPS_GOTREL,
anatofuz
parents:
diff changeset
87 R_MIPS_GOT_GP,
anatofuz
parents:
diff changeset
88 R_MIPS_GOT_GP_PC,
anatofuz
parents:
diff changeset
89 R_MIPS_GOT_LOCAL_PAGE,
anatofuz
parents:
diff changeset
90 R_MIPS_GOT_OFF,
anatofuz
parents:
diff changeset
91 R_MIPS_GOT_OFF32,
anatofuz
parents:
diff changeset
92 R_MIPS_TLSGD,
anatofuz
parents:
diff changeset
93 R_MIPS_TLSLD,
anatofuz
parents:
diff changeset
94 R_PPC32_PLTREL,
anatofuz
parents:
diff changeset
95 R_PPC64_CALL,
anatofuz
parents:
diff changeset
96 R_PPC64_CALL_PLT,
anatofuz
parents:
diff changeset
97 R_PPC64_RELAX_TOC,
anatofuz
parents:
diff changeset
98 R_PPC64_TOCBASE,
anatofuz
parents:
diff changeset
99 R_RISCV_ADD,
anatofuz
parents:
diff changeset
100 R_RISCV_PC_INDIRECT,
anatofuz
parents:
diff changeset
101 };
anatofuz
parents:
diff changeset
102
anatofuz
parents:
diff changeset
103 // Architecture-neutral representation of relocation.
anatofuz
parents:
diff changeset
104 struct Relocation {
anatofuz
parents:
diff changeset
105 RelExpr expr;
anatofuz
parents:
diff changeset
106 RelType type;
anatofuz
parents:
diff changeset
107 uint64_t offset;
anatofuz
parents:
diff changeset
108 int64_t addend;
anatofuz
parents:
diff changeset
109 Symbol *sym;
anatofuz
parents:
diff changeset
110 };
anatofuz
parents:
diff changeset
111
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
112 // 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
113 // 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
114 // useful when basic block sections are enabled.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
115 struct JumpInstrMod {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
116 JumpModType original;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
117 uint64_t offset;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
118 unsigned size;
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
119 };
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
120
150
anatofuz
parents:
diff changeset
121 // This function writes undefined symbol diagnostics to an internal buffer.
anatofuz
parents:
diff changeset
122 // Call reportUndefinedSymbols() after calling scanRelocations() to emit
anatofuz
parents:
diff changeset
123 // the diagnostics.
anatofuz
parents:
diff changeset
124 template <class ELFT> void scanRelocations(InputSectionBase &);
anatofuz
parents:
diff changeset
125
anatofuz
parents:
diff changeset
126 template <class ELFT> void reportUndefinedSymbols();
anatofuz
parents:
diff changeset
127
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
128 void hexagonTLSSymbolUpdate(ArrayRef<OutputSection *> outputSections);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
129 bool hexagonNeedsTLSSymbol(ArrayRef<OutputSection *> outputSections);
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
130
150
anatofuz
parents:
diff changeset
131 class ThunkSection;
anatofuz
parents:
diff changeset
132 class Thunk;
anatofuz
parents:
diff changeset
133 struct InputSectionDescription;
anatofuz
parents:
diff changeset
134
anatofuz
parents:
diff changeset
135 class ThunkCreator {
anatofuz
parents:
diff changeset
136 public:
anatofuz
parents:
diff changeset
137 // Return true if Thunks have been added to OutputSections
anatofuz
parents:
diff changeset
138 bool createThunks(ArrayRef<OutputSection *> outputSections);
anatofuz
parents:
diff changeset
139
anatofuz
parents:
diff changeset
140 // The number of completed passes of createThunks this permits us
anatofuz
parents:
diff changeset
141 // to do one time initialization on Pass 0 and put a limit on the
anatofuz
parents:
diff changeset
142 // number of times it can be called to prevent infinite loops.
anatofuz
parents:
diff changeset
143 uint32_t pass = 0;
anatofuz
parents:
diff changeset
144
anatofuz
parents:
diff changeset
145 private:
anatofuz
parents:
diff changeset
146 void mergeThunks(ArrayRef<OutputSection *> outputSections);
anatofuz
parents:
diff changeset
147
anatofuz
parents:
diff changeset
148 ThunkSection *getISDThunkSec(OutputSection *os, InputSection *isec,
anatofuz
parents:
diff changeset
149 InputSectionDescription *isd, uint32_t type,
anatofuz
parents:
diff changeset
150 uint64_t src);
anatofuz
parents:
diff changeset
151
anatofuz
parents:
diff changeset
152 ThunkSection *getISThunkSec(InputSection *isec);
anatofuz
parents:
diff changeset
153
anatofuz
parents:
diff changeset
154 void createInitialThunkSections(ArrayRef<OutputSection *> outputSections);
anatofuz
parents:
diff changeset
155
anatofuz
parents:
diff changeset
156 std::pair<Thunk *, bool> getThunk(InputSection *isec, Relocation &rel,
anatofuz
parents:
diff changeset
157 uint64_t src);
anatofuz
parents:
diff changeset
158
anatofuz
parents:
diff changeset
159 ThunkSection *addThunkSection(OutputSection *os, InputSectionDescription *,
anatofuz
parents:
diff changeset
160 uint64_t off);
anatofuz
parents:
diff changeset
161
anatofuz
parents:
diff changeset
162 bool normalizeExistingThunk(Relocation &rel, uint64_t src);
anatofuz
parents:
diff changeset
163
anatofuz
parents:
diff changeset
164 // Record all the available Thunks for a (Symbol, addend) pair, where Symbol
anatofuz
parents:
diff changeset
165 // is represented as a (section, offset) pair. There may be multiple
anatofuz
parents:
diff changeset
166 // relocations sharing the same (section, offset + addend) pair. We may revert
anatofuz
parents:
diff changeset
167 // a relocation back to its original non-Thunk target, and restore the
anatofuz
parents:
diff changeset
168 // original addend, so we cannot fold offset + addend. A nested pair is used
anatofuz
parents:
diff changeset
169 // because DenseMapInfo is not specialized for std::tuple.
anatofuz
parents:
diff changeset
170 llvm::DenseMap<std::pair<std::pair<SectionBase *, uint64_t>, int64_t>,
anatofuz
parents:
diff changeset
171 std::vector<Thunk *>>
anatofuz
parents:
diff changeset
172 thunkedSymbolsBySectionAndAddend;
anatofuz
parents:
diff changeset
173 llvm::DenseMap<std::pair<Symbol *, int64_t>, std::vector<Thunk *>>
anatofuz
parents:
diff changeset
174 thunkedSymbols;
anatofuz
parents:
diff changeset
175
anatofuz
parents:
diff changeset
176 // Find a Thunk from the Thunks symbol definition, we can use this to find
anatofuz
parents:
diff changeset
177 // the Thunk from a relocation to the Thunks symbol definition.
anatofuz
parents:
diff changeset
178 llvm::DenseMap<Symbol *, Thunk *> thunks;
anatofuz
parents:
diff changeset
179
anatofuz
parents:
diff changeset
180 // Track InputSections that have an inline ThunkSection placed in front
anatofuz
parents:
diff changeset
181 // an inline ThunkSection may have control fall through to the section below
anatofuz
parents:
diff changeset
182 // so we need to make sure that there is only one of them.
anatofuz
parents:
diff changeset
183 // The Mips LA25 Thunk is an example of an inline ThunkSection.
anatofuz
parents:
diff changeset
184 llvm::DenseMap<InputSection *, ThunkSection *> thunkedSections;
anatofuz
parents:
diff changeset
185 };
anatofuz
parents:
diff changeset
186
anatofuz
parents:
diff changeset
187 // Return a int64_t to make sure we get the sign extension out of the way as
anatofuz
parents:
diff changeset
188 // early as possible.
anatofuz
parents:
diff changeset
189 template <class ELFT>
anatofuz
parents:
diff changeset
190 static inline int64_t getAddend(const typename ELFT::Rel &rel) {
anatofuz
parents:
diff changeset
191 return 0;
anatofuz
parents:
diff changeset
192 }
anatofuz
parents:
diff changeset
193 template <class ELFT>
anatofuz
parents:
diff changeset
194 static inline int64_t getAddend(const typename ELFT::Rela &rel) {
anatofuz
parents:
diff changeset
195 return rel.r_addend;
anatofuz
parents:
diff changeset
196 }
anatofuz
parents:
diff changeset
197 } // namespace elf
anatofuz
parents:
diff changeset
198 } // namespace lld
anatofuz
parents:
diff changeset
199
anatofuz
parents:
diff changeset
200 #endif