LLVM 17.0.0git
i386.h
Go to the documentation of this file.
1//=== i386.h - Generic JITLink i386 edge kinds, utilities -*- 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// Generic utilities for graphs representing i386 objects.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_JITLINK_I386_H
14#define LLVM_EXECUTIONENGINE_JITLINK_I386_H
15
18
20/// Represets i386 fixups
22
23 /// None
25
26 /// A plain 32-bit pointer value relocation.
27 ///
28 /// Fixup expression:
29 /// Fixup <- Target + Addend : uint32
30 ///
31 /// Errors:
32 /// - The target must reside in the low 32-bits of the address space,
33 /// otherwise an out-of-range error will be returned.
34 ///
36
37 /// A 32-bit PC-relative relocation.
38 ///
39 /// Represents a data/control flow instruction using PC-relative addressing
40 /// to a target.
41 ///
42 /// The fixup expression for this kind includes an implicit offset to account
43 /// for the PC (unlike the Delta edges) so that a PCRel32 with a target
44 /// T and addend zero is a call/branch to the start (offset zero) of T.
45 ///
46 /// Fixup expression:
47 /// Fixup <- Target - (Fixup + 4) + Addend : int32
48 ///
49 /// Errors:
50 /// - The result of the fixup expression must fit into an int32, otherwise
51 /// an out-of-range error will be returned.
52 ///
54
55 /// A plain 16-bit pointer value relocation.
56 ///
57 /// Fixup expression:
58 /// Fixup <- Target + Addend : uint16
59 ///
60 /// Errors:
61 /// - The target must reside in the low 16-bits of the address space,
62 /// otherwise an out-of-range error will be returned.
63 ///
65
66 /// A 16-bit PC-relative relocation.
67 ///
68 /// Represents a data/control flow instruction using PC-relative addressing
69 /// to a target.
70 ///
71 /// The fixup expression for this kind includes an implicit offset to account
72 /// for the PC (unlike the Delta edges) so that a PCRel16 with a target
73 /// T and addend zero is a call/branch to the start (offset zero) of T.
74 ///
75 /// Fixup expression:
76 /// Fixup <- Target - (Fixup + 4) + Addend : int16
77 ///
78 /// Errors:
79 /// - The result of the fixup expression must fit into an int16, otherwise
80 /// an out-of-range error will be returned.
81 ///
83
84 /// A 32-bit delta.
85 ///
86 /// Delta from the fixup to the target.
87 ///
88 /// Fixup expression:
89 /// Fixup <- Target - Fixup + Addend : int64
90 ///
91 /// Errors:
92 /// - The result of the fixup expression must fit into an int32, otherwise
93 /// an out-of-range error will be returned.
95
96 /// A 32-bit GOT delta.
97 ///
98 /// Delta from the global offset table to the target.
99 ///
100 /// Fixup expression:
101 /// Fixup <- Target - GOTSymbol + Addend : int32
102 ///
103 /// Errors:
104 /// - *ASSERTION* Failure to a null pointer GOTSymbol, which the GOT section
105 /// symbol was not been defined.
107
108 /// A GOT entry offset within GOT getter/constructor, transformed to
109 /// Delta32FromGOT pointing at the GOT entry for the original target.
110 ///
111 /// Indicates that this edge should be transformed into a Delta32FromGOT
112 /// targeting the GOT entry for the edge's current target, maintaining the
113 /// same addend.
114 /// A GOT entry for the target should be created if one does not already
115 /// exist.
116 ///
117 /// Edges of this kind are usually handled by a GOT builder pass inserted by
118 /// default
119 ///
120 /// Fixup expression:
121 /// NONE
122 ///
123 /// Errors:
124 /// - *ASSERTION* Failure to handle edges of this kind prior to the fixup
125 /// phase will result in an assert/unreachable during the fixup phase
127
128 /// A 32-bit PC-relative branch.
129 ///
130 /// Represents a PC-relative call or branch to a target. This can be used to
131 /// identify, record, and/or patch call sites.
132 ///
133 /// The fixup expression for this kind includes an implicit offset to account
134 /// for the PC (unlike the Delta edges) so that a Branch32PCRel with a target
135 /// T and addend zero is a call/branch to the start (offset zero) of T.
136 ///
137 /// Fixup expression:
138 /// Fixup <- Target - (Fixup + 4) + Addend : int32
139 ///
140 /// Errors:
141 /// - The result of the fixup expression must fit into an int32, otherwise
142 /// an out-of-range error will be returned.
143 ///
145
146 /// A 32-bit PC-relative branch to a pointer jump stub.
147 ///
148 /// The target of this relocation should be a pointer jump stub of the form:
149 ///
150 /// \code{.s}
151 /// .text
152 /// jmp *tgtptr
153 /// ; ...
154 ///
155 /// .data
156 /// tgtptr:
157 /// .quad 0
158 /// \endcode
159 ///
160 /// This edge kind has the same fixup expression as BranchPCRel32, but further
161 /// identifies the call/branch as being to a pointer jump stub. For edges of
162 /// this kind the jump stub should not be bypassed (use
163 /// BranchPCRel32ToPtrJumpStubBypassable for that), but the pointer location
164 /// target may be recorded to allow manipulation at runtime.
165 ///
166 /// Fixup expression:
167 /// Fixup <- Target - Fixup + Addend - 4 : int32
168 ///
169 /// Errors:
170 /// - The result of the fixup expression must fit into an int32, otherwise
171 /// an out-of-range error will be returned.
172 ///
174
175 /// A relaxable version of BranchPCRel32ToPtrJumpStub.
176 ///
177 /// The edge kind has the same fixup expression as BranchPCRel32ToPtrJumpStub,
178 /// but identifies the call/branch as being to a pointer jump stub that may be
179 /// bypassed with a direct jump to the ultimate target if the ultimate target
180 /// is within range of the fixup location.
181 ///
182 /// Fixup expression:
183 /// Fixup <- Target - Fixup + Addend - 4: int32
184 ///
185 /// Errors:
186 /// - The result of the fixup expression must fit into an int32, otherwise
187 /// an out-of-range error will be returned.
188 ///
190};
191
192/// Returns a string name for the given i386 edge. For debugging purposes
193/// only
194const char *getEdgeKindName(Edge::Kind K);
195
196/// Returns true if the given uint32_t value is in range for a uint16_t.
198 return Value <= std::numeric_limits<uint16_t>::max();
199}
200
201/// Returns true if the given int32_t value is in range for an int16_t.
202inline bool isInRangeForImmS16(int32_t Value) {
203 return (Value >= std::numeric_limits<int16_t>::min() &&
204 Value <= std::numeric_limits<int16_t>::max());
205}
206
207/// Returns true if the given int64_t value is in range for an int32_t.
208inline bool isInRangeForImmS32(int64_t Value) {
209 return (Value >= std::numeric_limits<int32_t>::min() &&
210 Value <= std::numeric_limits<int32_t>::max());
211}
212
213/// Apply fixup expression for edge to block content.
215 const Symbol *GOTSymbol) {
216 using namespace i386;
217 using namespace llvm::support;
218
219 char *BlockWorkingMem = B.getAlreadyMutableContent().data();
220 char *FixupPtr = BlockWorkingMem + E.getOffset();
221 auto FixupAddress = B.getAddress() + E.getOffset();
222
223 switch (E.getKind()) {
224 case i386::None: {
225 break;
226 }
227
228 case i386::Pointer32: {
229 uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
230 *(ulittle32_t *)FixupPtr = Value;
231 break;
232 }
233
234 case i386::PCRel32: {
235 int32_t Value =
236 E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
237 *(little32_t *)FixupPtr = Value;
238 break;
239 }
240
241 case i386::Pointer16: {
242 uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
244 *(ulittle16_t *)FixupPtr = Value;
245 else
246 return makeTargetOutOfRangeError(G, B, E);
247 break;
248 }
249
250 case i386::PCRel16: {
251 int32_t Value =
252 E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
254 *(little16_t *)FixupPtr = Value;
255 else
256 return makeTargetOutOfRangeError(G, B, E);
257 break;
258 }
259
260 case i386::Delta32: {
261 int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
262 *(little32_t *)FixupPtr = Value;
263 break;
264 }
265
267 assert(GOTSymbol && "No GOT section symbol");
268 int32_t Value =
269 E.getTarget().getAddress() - GOTSymbol->getAddress() + E.getAddend();
270 *(little32_t *)FixupPtr = Value;
271 break;
272 }
273
277 int32_t Value =
278 E.getTarget().getAddress() - (FixupAddress + 4) + E.getAddend();
279 *(little32_t *)FixupPtr = Value;
280 break;
281 }
282
283 default:
284 return make_error<JITLinkError>(
285 "In graph " + G.getName() + ", section " + B.getSection().getName() +
286 " unsupported edge kind " + getEdgeKindName(E.getKind()));
287 }
288
289 return Error::success();
290}
291
292/// i386 pointer size.
293constexpr uint32_t PointerSize = 4;
294
295/// i386 null pointer content.
296extern const char NullPointerContent[PointerSize];
297
298/// i386 pointer jump stub content.
299///
300/// Contains the instruction sequence for an indirect jump via an in-memory
301/// pointer:
302/// jmpq *ptr
303extern const char PointerJumpStubContent[6];
304
305/// Creates a new pointer block in the given section and returns an anonymous
306/// symbol pointing to it.
307///
308/// If InitialTarget is given then an Pointer32 relocation will be added to the
309/// block pointing at InitialTarget.
310///
311/// The pointer block will have the following default values:
312/// alignment: 32-bit
313/// alignment-offset: 0
314/// address: highest allowable (~7U)
316 Symbol *InitialTarget = nullptr,
317 uint64_t InitialAddend = 0) {
318 auto &B = G.createContentBlock(PointerSection, NullPointerContent,
319 orc::ExecutorAddr(), 8, 0);
320 if (InitialTarget)
321 B.addEdge(Pointer32, 0, *InitialTarget, InitialAddend);
322 return G.addAnonymousSymbol(B, 0, PointerSize, false, false);
323}
324
325/// Create a jump stub block that jumps via the pointer at the given symbol.
326///
327/// The stub block will have the following default values:
328/// alignment: 8-bit
329/// alignment-offset: 0
330/// address: highest allowable: (~5U)
332 Symbol &PointerSymbol) {
333 auto &B = G.createContentBlock(StubSection, PointerJumpStubContent,
334 orc::ExecutorAddr(), 8, 0);
335 B.addEdge(Pointer32,
336 // Offset is 2 because the the first 2 bytes of the
337 // jump stub block are {0xff, 0x25} -- an indirect absolute
338 // jump.
339 2, PointerSymbol, 0);
340 return B;
341}
342
343/// Create a jump stub that jumps via the pointer at the given symbol and
344/// an anonymous symbol pointing to it. Return the anonymous symbol.
345///
346/// The stub block will be created by createPointerJumpStubBlock.
348 Section &StubSection,
349 Symbol &PointerSymbol) {
350 return G.addAnonymousSymbol(
351 createPointerJumpStubBlock(G, StubSection, PointerSymbol), 0, 6, true,
352 false);
353}
354
355/// Global Offset Table Builder.
356class GOTTableManager : public TableManager<GOTTableManager> {
357public:
358 static StringRef getSectionName() { return "$__GOT"; }
359
361 Edge::Kind KindToSet = Edge::Invalid;
362 switch (E.getKind()) {
364 // we need to make sure that the GOT section exists, but don't otherwise
365 // need to fix up this edge
366 getGOTSection(G);
367 return false;
368 }
370 KindToSet = i386::Delta32FromGOT;
371 break;
372 default:
373 return false;
374 }
375 assert(KindToSet != Edge::Invalid &&
376 "Fell through switch, but no new kind to set");
377 DEBUG_WITH_TYPE("jitlink", {
378 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
379 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
380 << formatv("{0:x}", E.getOffset()) << ")\n";
381 });
382 E.setKind(KindToSet);
383 E.setTarget(getEntryForTarget(G, E.getTarget()));
384 return true;
385 }
386
388 return createAnonymousPointer(G, getGOTSection(G), &Target);
389 }
390
391private:
392 Section &getGOTSection(LinkGraph &G) {
393 if (!GOTSection)
394 GOTSection = &G.createSection(getSectionName(), orc::MemProt::Read);
395 return *GOTSection;
396 }
397
398 Section *GOTSection = nullptr;
399};
400
401/// Procedure Linkage Table Builder.
402class PLTTableManager : public TableManager<PLTTableManager> {
403public:
405
406 static StringRef getSectionName() { return "$__STUBS"; }
407
409 if (E.getKind() == i386::BranchPCRel32 && !E.getTarget().isDefined()) {
410 DEBUG_WITH_TYPE("jitlink", {
411 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
412 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
413 << formatv("{0:x}", E.getOffset()) << ")\n";
414 });
415 // Set the edge kind to Branch32ToPtrJumpStubBypassable to enable it to
416 // be optimized when the target is in-range.
418 E.setTarget(getEntryForTarget(G, E.getTarget()));
419 return true;
420 }
421 return false;
422 }
423
427 }
428
429public:
431 if (!PLTSection)
432 PLTSection = &G.createSection(getSectionName(),
434 return *PLTSection;
435 }
436
438 Section *PLTSection = nullptr;
439};
440
441/// Optimize the GOT and Stub relocations if the edge target address is in range
442/// 1. PCRel32GOTLoadRelaxable. For this edge kind, if the target is in range,
443/// then replace GOT load with lea. (THIS IS UNIMPLEMENTED RIGHT NOW!)
444/// 2. BranchPCRel32ToPtrJumpStubRelaxable. For this edge kind, if the target is
445/// in range, replace a indirect jump by plt stub with a direct jump to the
446/// target
448
449} // namespace llvm::jitlink::i386
450
451#endif // LLVM_EXECUTIONENGINE_JITLINK_I386_H
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_LIKELY(EXPR)
Definition: Compiler.h:209
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
#define G(x, y, z)
Definition: MD5.cpp:56
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
Represents an address in the executor process.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163