[JITLink] Add support of R_X86_64_32S relocation
[llvm-project.git] / llvm / lib / ExecutionEngine / JITLink / x86_64.cpp
blob4c107cbf41924c84379bbebe1bdd1c33776c828a
1 //===----- x86_64.cpp - Generic JITLink x86-64 edge kinds, utilities ------===//
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 x86-64 objects.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ExecutionEngine/JITLink/x86_64.h"
15 #define DEBUG_TYPE "jitlink"
17 namespace llvm {
18 namespace jitlink {
19 namespace x86_64 {
21 const char *getEdgeKindName(Edge::Kind K) {
22 switch (K) {
23 case Pointer64:
24 return "Pointer64";
25 case Pointer32:
26 return "Pointer32";
27 case Pointer32Signed:
28 return "Pointer32Signed";
29 case Delta64:
30 return "Delta64";
31 case Delta32:
32 return "Delta32";
33 case NegDelta64:
34 return "NegDelta64";
35 case NegDelta32:
36 return "NegDelta32";
37 case Delta64FromGOT:
38 return "Delta64FromGOT";
39 case BranchPCRel32:
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";
63 default:
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) {
81 #ifndef NDEBUG
82 bool REXPrefix = E.getKind() == x86_64::PCRel32GOTLoadREXRelaxable;
83 assert(E.getOffset() >= (REXPrefix ? 3 : 2) &&
84 "GOT edge occurs too early in block");
85 #endif
86 auto *FixupData = reinterpret_cast<uint8_t *>(
87 const_cast<char *>(B->getContent().data())) +
88 E.getOffset();
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))
107 continue;
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);
115 LLVM_DEBUG({
116 dbgs() << " Replaced GOT load wih LEA:\n ";
117 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
118 dbgs() << "\n";
120 continue;
123 // Transform call/jmp instructions
124 if (Op == 0xff && TargetInRangeForImmU32) {
125 if (ModRM == 0x15) {
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;
131 LLVM_DEBUG({
132 dbgs() << " replaced call instruction's memory operand wih imm "
133 "operand:\n ";
134 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
135 dbgs() << "\n";
137 } else {
138 // Transform "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop"
139 assert(ModRM == 0x25 && "Invalid ModRm for call/jmp instructions");
140 FixupData[-2] = 0xe9;
141 FixupData[3] = 0x90;
142 E.setOffset(E.getOffset() - 1);
143 LLVM_DEBUG({
144 dbgs() << " replaced jmp instruction's memory operand wih imm "
145 "operand:\n ";
146 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
147 dbgs() << "\n";
150 E.setKind(x86_64::Pointer32);
151 E.setTarget(GOTTarget);
152 continue;
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);
175 LLVM_DEBUG({
176 dbgs() << " Replaced stub branch with direct branch:\n ";
177 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
178 dbgs() << "\n";
184 return Error::success();
187 } // end namespace x86_64
188 } // end namespace jitlink
189 } // end namespace llvm