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";
38 return "Delta64FromGOT";
40 return "BranchPCRel32";
41 case BranchPCRel32ToPtrJumpStub
:
42 return "BranchPCRel32ToPtrJumpStub";
43 case BranchPCRel32ToPtrJumpStubBypassable
:
44 return "BranchPCRel32ToPtrJumpStubBypassable";
45 case RequestGOTAndTransformToDelta32
:
46 return "RequestGOTAndTransformToDelta32";
47 case RequestGOTAndTransformToDelta64
:
48 return "RequestGOTAndTransformToDelta64";
49 case RequestGOTAndTransformToDelta64FromGOT
:
50 return "RequestGOTAndTransformToDelta64FromGOT";
51 case PCRel32GOTLoadREXRelaxable
:
52 return "PCRel32GOTLoadREXRelaxable";
53 case RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable
:
54 return "RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable";
55 case PCRel32GOTLoadRelaxable
:
56 return "PCRel32GOTLoadRelaxable";
57 case RequestGOTAndTransformToPCRel32GOTLoadRelaxable
:
58 return "RequestGOTAndTransformToPCRel32GOTLoadRelaxable";
59 case PCRel32TLVPLoadREXRelaxable
:
60 return "PCRel32TLVPLoadREXRelaxable";
61 case RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable
:
62 return "RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable";
64 return getGenericEdgeKindName(static_cast<Edge::Kind
>(K
));
68 const char NullPointerContent
[PointerSize
] = {0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00};
71 const char PointerJumpStubContent
[6] = {
72 static_cast<char>(0xFFu
), 0x25, 0x00, 0x00, 0x00, 0x00};
74 Error
optimize_x86_64_GOTAndStubs(LinkGraph
&G
) {
75 LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
77 for (auto *B
: G
.blocks())
78 for (auto &E
: B
->edges()) {
79 if (E
.getKind() == x86_64::PCRel32GOTLoadRelaxable
||
80 E
.getKind() == x86_64::PCRel32GOTLoadREXRelaxable
) {
82 bool REXPrefix
= E
.getKind() == x86_64::PCRel32GOTLoadREXRelaxable
;
83 assert(E
.getOffset() >= (REXPrefix
? 3 : 2) &&
84 "GOT edge occurs too early in block");
86 auto *FixupData
= reinterpret_cast<uint8_t *>(
87 const_cast<char *>(B
->getContent().data())) +
89 const uint8_t Op
= FixupData
[-2];
90 const uint8_t ModRM
= FixupData
[-1];
92 auto &GOTEntryBlock
= E
.getTarget().getBlock();
93 assert(GOTEntryBlock
.getSize() == G
.getPointerSize() &&
94 "GOT entry block should be pointer sized");
95 assert(GOTEntryBlock
.edges_size() == 1 &&
96 "GOT entry should only have one outgoing edge");
97 auto &GOTTarget
= GOTEntryBlock
.edges().begin()->getTarget();
98 JITTargetAddress TargetAddr
= GOTTarget
.getAddress();
99 JITTargetAddress EdgeAddr
= B
->getFixupAddress(E
);
100 int64_t Displacement
= TargetAddr
- EdgeAddr
+ 4;
101 bool TargetInRangeForImmU32
= isInRangeForImmU32(TargetAddr
);
102 bool DisplacementInRangeForImmS32
= isInRangeForImmS32(Displacement
);
104 // If both of the Target and displacement is out of range, then
105 // there isn't optimization chance.
106 if (!(TargetInRangeForImmU32
|| DisplacementInRangeForImmS32
))
109 // Transform "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
110 if (Op
== 0x8b && DisplacementInRangeForImmS32
) {
111 FixupData
[-2] = 0x8d;
112 E
.setKind(x86_64::Delta32
);
113 E
.setTarget(GOTTarget
);
114 E
.setAddend(E
.getAddend() - 4);
116 dbgs() << " Replaced GOT load wih LEA:\n ";
117 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
123 // Transform call/jmp instructions
124 if (Op
== 0xff && TargetInRangeForImmU32
) {
126 // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call
127 // foo" But lld convert it to "addr32 call foo, because that makes
128 // result expression to be a single instruction.
129 FixupData
[-2] = 0x67;
130 FixupData
[-1] = 0xe8;
132 dbgs() << " replaced call instruction's memory operand wih imm "
134 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
138 // Transform "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop"
139 assert(ModRM
== 0x25 && "Invalid ModRm for call/jmp instructions");
140 FixupData
[-2] = 0xe9;
142 E
.setOffset(E
.getOffset() - 1);
144 dbgs() << " replaced jmp instruction's memory operand wih imm "
146 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
150 E
.setKind(x86_64::Pointer32
);
151 E
.setTarget(GOTTarget
);
154 } else if (E
.getKind() == x86_64::BranchPCRel32ToPtrJumpStubBypassable
) {
155 auto &StubBlock
= E
.getTarget().getBlock();
156 assert(StubBlock
.getSize() == sizeof(PointerJumpStubContent
) &&
157 "Stub block should be stub sized");
158 assert(StubBlock
.edges_size() == 1 &&
159 "Stub block should only have one outgoing edge");
161 auto &GOTBlock
= StubBlock
.edges().begin()->getTarget().getBlock();
162 assert(GOTBlock
.getSize() == G
.getPointerSize() &&
163 "GOT block should be pointer sized");
164 assert(GOTBlock
.edges_size() == 1 &&
165 "GOT block should only have one outgoing edge");
167 auto &GOTTarget
= GOTBlock
.edges().begin()->getTarget();
168 JITTargetAddress EdgeAddr
= B
->getAddress() + E
.getOffset();
169 JITTargetAddress TargetAddr
= GOTTarget
.getAddress();
171 int64_t Displacement
= TargetAddr
- EdgeAddr
+ 4;
172 if (isInRangeForImmS32(Displacement
)) {
173 E
.setKind(x86_64::BranchPCRel32
);
174 E
.setTarget(GOTTarget
);
176 dbgs() << " Replaced stub branch with direct branch:\n ";
177 printEdge(dbgs(), *B
, E
, getEdgeKindName(E
.getKind()));
184 return Error::success();
187 } // end namespace x86_64
188 } // end namespace jitlink
189 } // end namespace llvm