1 //===----- x86_64.cpp - Generic JITLink x86-64 edge kinds, utilities ------===//
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
7 //===----------------------------------------------------------------------===//
9 // Generic utilities for graphs representing x86-64 objects.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ExecutionEngine/JITLink/x86_64.h"
15 #define DEBUG_TYPE "jitlink"
21 const char *getEdgeKindName(Edge::Kind K
) {
28 return "Pointer32Signed";
44 return "Delta64FromGOT";
48 return "BranchPCRel32";
49 case BranchPCRel32ToPtrJumpStub
:
50 return "BranchPCRel32ToPtrJumpStub";
51 case BranchPCRel32ToPtrJumpStubBypassable
:
52 return "BranchPCRel32ToPtrJumpStubBypassable";
53 case RequestGOTAndTransformToDelta32
:
54 return "RequestGOTAndTransformToDelta32";
55 case RequestGOTAndTransformToDelta64
:
56 return "RequestGOTAndTransformToDelta64";
57 case RequestGOTAndTransformToDelta64FromGOT
:
58 return "RequestGOTAndTransformToDelta64FromGOT";
59 case PCRel32GOTLoadREXRelaxable
:
60 return "PCRel32GOTLoadREXRelaxable";
61 case RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable
:
62 return "RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable";
63 case PCRel32GOTLoadRelaxable
:
64 return "PCRel32GOTLoadRelaxable";
65 case RequestGOTAndTransformToPCRel32GOTLoadRelaxable
:
66 return "RequestGOTAndTransformToPCRel32GOTLoadRelaxable";
67 case PCRel32TLVPLoadREXRelaxable
:
68 return "PCRel32TLVPLoadREXRelaxable";
69 case RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable
:
70 return "RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable";
72 return getGenericEdgeKindName(static_cast<Edge::Kind
>(K
));
76 const char NullPointerContent
[PointerSize
] = {0x00, 0x00, 0x00, 0x00,
77 0x00, 0x00, 0x00, 0x00};
79 const char PointerJumpStubContent
[6] = {
80 static_cast<char>(0xFFu
), 0x25, 0x00, 0x00, 0x00, 0x00};
82 Error
optimizeGOTAndStubAccesses(LinkGraph
&G
) {
83 LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
85 for (auto *B
: G
.blocks())
86 for (auto &E
: B
->edges()) {
87 if (E
.getKind() == x86_64::PCRel32GOTLoadRelaxable
||
88 E
.getKind() == x86_64::PCRel32GOTLoadREXRelaxable
) {
90 bool REXPrefix
= E
.getKind() == x86_64::PCRel32GOTLoadREXRelaxable
;
91 assert(E
.getOffset() >= (REXPrefix
? 3u : 2u) &&
92 "GOT edge occurs too early in block");
94 auto *FixupData
= reinterpret_cast<uint8_t *>(
95 const_cast<char *>(B
->getContent().data())) +
97 const uint8_t Op
= FixupData
[-2];
98 const uint8_t ModRM
= FixupData
[-1];
100 auto &GOTEntryBlock
= E
.getTarget().getBlock();
101 assert(GOTEntryBlock
.getSize() == G
.getPointerSize() &&
102 "GOT entry block should be pointer sized");
103 assert(GOTEntryBlock
.edges_size() == 1 &&
104 "GOT entry should only have one outgoing edge");
105 auto &GOTTarget
= GOTEntryBlock
.edges().begin()->getTarget();
106 orc::ExecutorAddr TargetAddr
= GOTTarget
.getAddress();
107 orc::ExecutorAddr EdgeAddr
= B
->getFixupAddress(E
);
108 int64_t Displacement
= TargetAddr
- EdgeAddr
+ 4;
109 bool TargetInRangeForImmU32
= isUInt
<32>(TargetAddr
.getValue());
110 bool DisplacementInRangeForImmS32
= isInt
<32>(Displacement
);
112 // If both of the Target and displacement is out of range, then
113 // there isn't optimization chance.
114 if (!(TargetInRangeForImmU32
|| DisplacementInRangeForImmS32
))
117 // Transform "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
118 if (Op
== 0x8b && DisplacementInRangeForImmS32
) {
119 FixupData
[-2] = 0x8d;
120 E
.setKind(x86_64::Delta32
);
121 E
.setTarget(GOTTarget
);
122 E
.setAddend(E
.getAddend() - 4);
124 dbgs() << " Replaced GOT load wih LEA:\n ";
125 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
131 // Transform call/jmp instructions
132 if (Op
== 0xff && TargetInRangeForImmU32
) {
134 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call
135 // foo" But lld convert it to "addr32 call foo, because that makes
136 // result expression to be a single instruction.
137 FixupData
[-2] = 0x67;
138 FixupData
[-1] = 0xe8;
140 dbgs() << " replaced call instruction's memory operand wih imm "
142 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
146 // Transform "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop"
147 assert(ModRM
== 0x25 && "Invalid ModRm for call/jmp instructions");
148 FixupData
[-2] = 0xe9;
150 E
.setOffset(E
.getOffset() - 1);
152 dbgs() << " replaced jmp instruction's memory operand wih imm "
154 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
158 E
.setKind(x86_64::Pointer32
);
159 E
.setTarget(GOTTarget
);
162 } else if (E
.getKind() == x86_64::BranchPCRel32ToPtrJumpStubBypassable
) {
163 auto &StubBlock
= E
.getTarget().getBlock();
164 assert(StubBlock
.getSize() == sizeof(PointerJumpStubContent
) &&
165 "Stub block should be stub sized");
166 assert(StubBlock
.edges_size() == 1 &&
167 "Stub block should only have one outgoing edge");
169 auto &GOTBlock
= StubBlock
.edges().begin()->getTarget().getBlock();
170 assert(GOTBlock
.getSize() == G
.getPointerSize() &&
171 "GOT block should be pointer sized");
172 assert(GOTBlock
.edges_size() == 1 &&
173 "GOT block should only have one outgoing edge");
175 auto &GOTTarget
= GOTBlock
.edges().begin()->getTarget();
176 orc::ExecutorAddr EdgeAddr
= B
->getAddress() + E
.getOffset();
177 orc::ExecutorAddr TargetAddr
= GOTTarget
.getAddress();
179 int64_t Displacement
= TargetAddr
- EdgeAddr
+ 4;
180 if (isInt
<32>(Displacement
)) {
181 E
.setKind(x86_64::BranchPCRel32
);
182 E
.setTarget(GOTTarget
);
184 dbgs() << " Replaced stub branch with direct branch:\n ";
185 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
192 return Error::success();
195 } // end namespace x86_64
196 } // end namespace jitlink
197 } // end namespace llvm