1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//=== i386.h - Generic JITLink i386 edge kinds, utilities -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Generic utilities for graphs representing i386 objects.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_EXECUTIONENGINE_JITLINK_I386_H
#define LLVM_EXECUTIONENGINE_JITLINK_I386_H
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
#include "llvm/ExecutionEngine/JITLink/TableManager.h"
namespace llvm::jitlink::i386 {
/// Represets i386 fixups
enum EdgeKind_i386 : Edge::Kind {
/// None
None = Edge::FirstRelocation,
/// A plain 32-bit pointer value relocation.
///
/// Fixup expression:
/// Fixup <- Target + Addend : uint32
///
/// Errors:
/// - The target must reside in the low 32-bits of the address space,
/// otherwise an out-of-range error will be returned.
///
Pointer32,
/// A 32-bit PC-relative relocation.
///
/// Represents a data/control flow instruction using PC-relative addressing
/// to a target.
///
/// The fixup expression for this kind includes an implicit offset to account
/// for the PC (unlike the Delta edges) so that a PCRel32 with a target
/// T and addend zero is a call/branch to the start (offset zero) of T.
///
/// Fixup expression:
/// Fixup <- Target - (Fixup + 4) + Addend : int32
///
/// Errors:
/// - The result of the fixup expression must fit into an int32, otherwise
/// an out-of-range error will be returned.
///
PCRel32,
/// A plain 16-bit pointer value relocation.
///
/// Fixup expression:
/// Fixup <- Target + Addend : uint16
///
/// Errors:
/// - The target must reside in the low 16-bits of the address space,
/// otherwise an out-of-range error will be returned.
///
Pointer16,
/// A 16-bit PC-relative relocation.
///
/// Represents a data/control flow instruction using PC-relative addressing
/// to a target.
///
/// The fixup expression for this kind includes an implicit offset to account
/// for the PC (unlike the Delta edges) so that a PCRel16 with a target
/// T and addend zero is a call/branch to the start (offset zero) of T.
///
/// Fixup expression:
/// Fixup <- Target - (Fixup + 4) + Addend : int16
///
/// Errors:
/// - The result of the fixup expression must fit into an int16, otherwise
/// an out-of-range error will be returned.
///
PCRel16,
/// A 32-bit delta.
///
/// Delta from the fixup to the target.
///
/// Fixup expression:
/// Fixup <- Target - Fixup + Addend : int64
///
/// Errors:
/// - The result of the fixup expression must fit into an int32, otherwise
/// an out-of-range error will be returned.
Delta32,
/// A 32-bit GOT delta.
///
/// Delta from the global offset table to the target.
///
/// Fixup expression:
/// Fixup <- Target - GOTSymbol + Addend : int32
///
/// Errors:
/// - *ASSERTION* Failure to a null pointer GOTSymbol, which the GOT section
/// symbol was not been defined.
Delta32FromGOT,
/// A GOT entry offset within GOT getter/constructor, transformed to
/// Delta32FromGOT pointing at the GOT entry for the original target.
///
/// Indicates that this edge should be transformed into a Delta32FromGOT
/// targeting the GOT entry for the edge's current target, maintaining the
/// same addend.
/// A GOT entry for the target should be created if one does not already
/// exist.
///
/// Edges of this kind are usually handled by a GOT builder pass inserted by
/// default
///
/// Fixup expression:
/// NONE
///
/// Errors:
/// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
/// phase will result in an assert/unreachable during the fixup phase
RequestGOTAndTransformToDelta32FromGOT,
};
/// Returns a string name for the given i386 edge. For debugging purposes
/// only
const char *getEdgeKindName(Edge::Kind K);
/// Returns true if the given uint32_t value is in range for a uint16_t.
inline bool isInRangeForImmU16(uint32_t Value) {
return Value <= std::numeric_limits<uint16_t>::max();
}
/// Returns true if the given int32_t value is in range for an int16_t.
inline bool isInRangeForImmS16(int32_t Value) {
return (Value >= std::numeric_limits<int16_t>::min() &&
Value <= std::numeric_limits<int16_t>::max());
}
/// Apply fixup expression for edge to block content.
inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
const Symbol *GOTSymbol) {
using namespace i386;
using namespace llvm::support;
char *BlockWorkingMem = B.getAlreadyMutableContent().data();
char *FixupPtr = BlockWorkingMem + E.getOffset();
auto FixupAddress = B.getAddress() + E.getOffset();
switch (E.getKind()) {
case i386::None: {
break;
}
case i386::Pointer32: {
uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
*(ulittle32_t *)FixupPtr = Value;
break;
}
case i386::PCRel32: {
int32_t Value =
E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
*(little32_t *)FixupPtr = Value;
break;
}
case i386::Pointer16: {
uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
if (LLVM_LIKELY(isInRangeForImmU16(Value)))
*(ulittle16_t *)FixupPtr = Value;
else
return makeTargetOutOfRangeError(G, B, E);
break;
}
case i386::PCRel16: {
int32_t Value =
E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
if (LLVM_LIKELY(isInRangeForImmS16(Value)))
*(little16_t *)FixupPtr = Value;
else
return makeTargetOutOfRangeError(G, B, E);
break;
}
case i386::Delta32: {
int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
*(little32_t *)FixupPtr = Value;
break;
}
case i386::Delta32FromGOT: {
assert(GOTSymbol && "No GOT section symbol");
int32_t Value =
E.getTarget().getAddress() - GOTSymbol->getAddress() + E.getAddend();
*(little32_t *)FixupPtr = Value;
break;
}
default:
return make_error<JITLinkError>(
"In graph " + G.getName() + ", section " + B.getSection().getName() +
"unsupported edge kind" + getEdgeKindName(E.getKind()));
}
return Error::success();
}
/// i386 pointer size.
constexpr uint32_t PointerSize = 4;
/// i386 null pointer content.
extern const char NullPointerContent[PointerSize];
/// Creates a new pointer block in the given section and returns an anonymous
/// symbol pointing to it.
///
/// If InitialTarget is given then an Pointer32 relocation will be added to the
/// block pointing at InitialTarget.
///
/// The pointer block will have the following default values:
/// alignment: 32-bit
/// alignment-offset: 0
/// address: highest allowable (~7U)
inline Symbol &createAnonymousPointer(LinkGraph &G, Section &PointerSection,
Symbol *InitialTarget = nullptr,
uint64_t InitialAddend = 0) {
auto &B = G.createContentBlock(PointerSection, NullPointerContent,
orc::ExecutorAddr(), 8, 0);
if (InitialTarget)
B.addEdge(Pointer32, 0, *InitialTarget, InitialAddend);
return G.addAnonymousSymbol(B, 0, PointerSize, false, false);
}
/// Global Offset Table Builder.
class GOTTableManager : public TableManager<GOTTableManager> {
public:
static StringRef getSectionName() { return "$__GOT"; }
bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
Edge::Kind KindToSet = Edge::Invalid;
switch (E.getKind()) {
case i386::Delta32FromGOT: {
// we need to make sure that the GOT section exists, but don't otherwise
// need to fix up this edge
getGOTSection(G);
return false;
}
case i386::RequestGOTAndTransformToDelta32FromGOT:
KindToSet = i386::Delta32FromGOT;
break;
default:
return false;
}
assert(KindToSet != Edge::Invalid &&
"Fell through switch, but no new kind to set");
DEBUG_WITH_TYPE("jitlink", {
dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
<< B->getFixupAddress(E) << " (" << B->getAddress() << " + "
<< formatv("{0:x}", E.getOffset()) << ")\n";
});
E.setKind(KindToSet);
E.setTarget(getEntryForTarget(G, E.getTarget()));
return true;
}
Symbol &createEntry(LinkGraph &G, Symbol &Target) {
return createAnonymousPointer(G, getGOTSection(G), &Target);
}
private:
Section &getGOTSection(LinkGraph &G) {
if (!GOTSection)
GOTSection = &G.createSection(getSectionName(), orc::MemProt::Read);
return *GOTSection;
}
Section *GOTSection = nullptr;
};
} // namespace llvm::jitlink::i386
#endif // LLVM_EXECUTIONENGINE_JITLINK_I386_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|