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";
50 return "Delta64FromGOT";
54 return "BranchPCRel32";
55 case BranchPCRel32ToPtrJumpStub
:
56 return "BranchPCRel32ToPtrJumpStub";
57 case BranchPCRel32ToPtrJumpStubBypassable
:
58 return "BranchPCRel32ToPtrJumpStubBypassable";
59 case RequestGOTAndTransformToDelta32
:
60 return "RequestGOTAndTransformToDelta32";
61 case RequestGOTAndTransformToDelta64
:
62 return "RequestGOTAndTransformToDelta64";
63 case RequestGOTAndTransformToDelta64FromGOT
:
64 return "RequestGOTAndTransformToDelta64FromGOT";
65 case PCRel32GOTLoadREXRelaxable
:
66 return "PCRel32GOTLoadREXRelaxable";
67 case RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable
:
68 return "RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable";
69 case PCRel32GOTLoadRelaxable
:
70 return "PCRel32GOTLoadRelaxable";
71 case RequestGOTAndTransformToPCRel32GOTLoadRelaxable
:
72 return "RequestGOTAndTransformToPCRel32GOTLoadRelaxable";
73 case PCRel32TLVPLoadREXRelaxable
:
74 return "PCRel32TLVPLoadREXRelaxable";
75 case RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable
:
76 return "RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable";
78 return getGenericEdgeKindName(static_cast<Edge::Kind
>(K
));
82 const char NullPointerContent
[PointerSize
] = {0x00, 0x00, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00};
85 const char PointerJumpStubContent
[6] = {
86 static_cast<char>(0xFFu
), 0x25, 0x00, 0x00, 0x00, 0x00};
88 Error
optimizeGOTAndStubAccesses(LinkGraph
&G
) {
89 LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
91 for (auto *B
: G
.blocks())
92 for (auto &E
: B
->edges()) {
93 if (E
.getKind() == x86_64::PCRel32GOTLoadRelaxable
||
94 E
.getKind() == x86_64::PCRel32GOTLoadREXRelaxable
) {
96 bool REXPrefix
= E
.getKind() == x86_64::PCRel32GOTLoadREXRelaxable
;
97 assert(E
.getOffset() >= (REXPrefix
? 3u : 2u) &&
98 "GOT edge occurs too early in block");
100 auto *FixupData
= reinterpret_cast<uint8_t *>(
101 const_cast<char *>(B
->getContent().data())) +
103 const uint8_t Op
= FixupData
[-2];
104 const uint8_t ModRM
= FixupData
[-1];
106 auto &GOTEntryBlock
= E
.getTarget().getBlock();
107 assert(GOTEntryBlock
.getSize() == G
.getPointerSize() &&
108 "GOT entry block should be pointer sized");
109 assert(GOTEntryBlock
.edges_size() == 1 &&
110 "GOT entry should only have one outgoing edge");
111 auto &GOTTarget
= GOTEntryBlock
.edges().begin()->getTarget();
112 orc::ExecutorAddr TargetAddr
= GOTTarget
.getAddress();
113 orc::ExecutorAddr EdgeAddr
= B
->getFixupAddress(E
);
114 int64_t Displacement
= TargetAddr
- EdgeAddr
+ 4;
115 bool TargetInRangeForImmU32
= isUInt
<32>(TargetAddr
.getValue());
116 bool DisplacementInRangeForImmS32
= isInt
<32>(Displacement
);
118 // If both of the Target and displacement is out of range, then
119 // there isn't optimization chance.
120 if (!(TargetInRangeForImmU32
|| DisplacementInRangeForImmS32
))
123 // Transform "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
124 if (Op
== 0x8b && DisplacementInRangeForImmS32
) {
125 FixupData
[-2] = 0x8d;
126 E
.setKind(x86_64::Delta32
);
127 E
.setTarget(GOTTarget
);
128 E
.setAddend(E
.getAddend() - 4);
130 dbgs() << " Replaced GOT load wih LEA:\n ";
131 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
137 // Transform call/jmp instructions
138 if (Op
== 0xff && TargetInRangeForImmU32
) {
140 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call
141 // foo" But lld convert it to "addr32 call foo, because that makes
142 // result expression to be a single instruction.
143 FixupData
[-2] = 0x67;
144 FixupData
[-1] = 0xe8;
146 dbgs() << " replaced call instruction's memory operand wih imm "
148 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
152 // Transform "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop"
153 assert(ModRM
== 0x25 && "Invalid ModRm for call/jmp instructions");
154 FixupData
[-2] = 0xe9;
156 E
.setOffset(E
.getOffset() - 1);
158 dbgs() << " replaced jmp instruction's memory operand wih imm "
160 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
164 E
.setKind(x86_64::Pointer32
);
165 E
.setTarget(GOTTarget
);
168 } else if (E
.getKind() == x86_64::BranchPCRel32ToPtrJumpStubBypassable
) {
169 auto &StubBlock
= E
.getTarget().getBlock();
170 assert(StubBlock
.getSize() == sizeof(PointerJumpStubContent
) &&
171 "Stub block should be stub sized");
172 assert(StubBlock
.edges_size() == 1 &&
173 "Stub block should only have one outgoing edge");
175 auto &GOTBlock
= StubBlock
.edges().begin()->getTarget().getBlock();
176 assert(GOTBlock
.getSize() == G
.getPointerSize() &&
177 "GOT block should be pointer sized");
178 assert(GOTBlock
.edges_size() == 1 &&
179 "GOT block should only have one outgoing edge");
181 auto &GOTTarget
= GOTBlock
.edges().begin()->getTarget();
182 orc::ExecutorAddr EdgeAddr
= B
->getAddress() + E
.getOffset();
183 orc::ExecutorAddr TargetAddr
= GOTTarget
.getAddress();
185 int64_t Displacement
= TargetAddr
- EdgeAddr
+ 4;
186 if (isInt
<32>(Displacement
)) {
187 E
.setKind(x86_64::BranchPCRel32
);
188 E
.setTarget(GOTTarget
);
190 dbgs() << " Replaced stub branch with direct branch:\n ";
191 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
198 return Error::success();
201 } // end namespace x86_64
202 } // end namespace jitlink
203 } // end namespace llvm