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.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ExecutionEngine/JITLink/x86_64.h"
14 
15 #define DEBUG_TYPE "jitlink"
16 
17 namespace llvm {
18 namespace jitlink {
19 namespace x86_64 {
20 
21 const char *getEdgeKindName(Edge::Kind K) {
22   switch (K) {
23   case Pointer64:
24     return "Pointer64";
25   case Pointer32:
26     return "Pointer32";
27   case Delta64:
28     return "Delta64";
29   case Delta32:
30     return "Delta32";
31   case NegDelta64:
32     return "NegDelta64";
33   case NegDelta32:
34     return "NegDelta32";
35   case Delta64FromGOT:
36     return "Delta64FromGOT";
37   case BranchPCRel32:
38     return "BranchPCRel32";
39   case BranchPCRel32ToPtrJumpStub:
40     return "BranchPCRel32ToPtrJumpStub";
41   case BranchPCRel32ToPtrJumpStubBypassable:
42     return "BranchPCRel32ToPtrJumpStubBypassable";
43   case RequestGOTAndTransformToDelta32:
44     return "RequestGOTAndTransformToDelta32";
45   case RequestGOTAndTransformToDelta64:
46     return "RequestGOTAndTransformToDelta64";
47   case RequestGOTAndTransformToDelta64FromGOT:
48     return "RequestGOTAndTransformToDelta64FromGOT";
49   case PCRel32GOTLoadREXRelaxable:
50     return "PCRel32GOTLoadREXRelaxable";
51   case RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable:
52     return "RequestGOTAndTransformToPCRel32GOTLoadREXRelaxable";
53   case PCRel32GOTLoadRelaxable:
54     return "PCRel32GOTLoadRelaxable";
55   case RequestGOTAndTransformToPCRel32GOTLoadRelaxable:
56     return "RequestGOTAndTransformToPCRel32GOTLoadRelaxable";
57   case PCRel32TLVPLoadREXRelaxable:
58     return "PCRel32TLVPLoadREXRelaxable";
59   case RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable:
60     return "RequestTLVPAndTransformToPCRel32TLVPLoadREXRelaxable";
61   default:
62     return getGenericEdgeKindName(static_cast<Edge::Kind>(K));
63   }
64 }
65 
66 const char NullPointerContent[PointerSize] = {0x00, 0x00, 0x00, 0x00,
67                                               0x00, 0x00, 0x00, 0x00};
68 
69 const char PointerJumpStubContent[6] = {
70     static_cast<char>(0xFFu), 0x25, 0x00, 0x00, 0x00, 0x00};
71 
72 Error optimize_x86_64_GOTAndStubs(LinkGraph &G) {
73   LLVM_DEBUG(dbgs() << "Optimizing GOT entries and stubs:\n");
74 
75   for (auto *B : G.blocks())
76     for (auto &E : B->edges()) {
77       if (E.getKind() == x86_64::PCRel32GOTLoadRelaxable ||
78           E.getKind() == x86_64::PCRel32GOTLoadREXRelaxable) {
79 #ifndef NDEBUG
80         bool REXPrefix = E.getKind() == x86_64::PCRel32GOTLoadREXRelaxable;
81         assert(E.getOffset() >= (REXPrefix ? 3 : 2) &&
82                "GOT edge occurs too early in block");
83 #endif
84         auto *FixupData = reinterpret_cast<uint8_t *>(
85                               const_cast<char *>(B->getContent().data())) +
86                           E.getOffset();
87         const uint8_t Op = FixupData[-2];
88         const uint8_t ModRM = FixupData[-1];
89 
90         auto &GOTEntryBlock = E.getTarget().getBlock();
91         assert(GOTEntryBlock.getSize() == G.getPointerSize() &&
92                "GOT entry block should be pointer sized");
93         assert(GOTEntryBlock.edges_size() == 1 &&
94                "GOT entry should only have one outgoing edge");
95         auto &GOTTarget = GOTEntryBlock.edges().begin()->getTarget();
96         JITTargetAddress TargetAddr = GOTTarget.getAddress();
97         JITTargetAddress EdgeAddr = B->getFixupAddress(E);
98         int64_t Displacement = TargetAddr - EdgeAddr + 4;
99         bool TargetInRangeForImmU32 = isInRangeForImmU32(TargetAddr);
100         bool DisplacementInRangeForImmS32 = isInRangeForImmS32(Displacement);
101 
102         // If both of the Target and displacement is out of range, then
103         // there isn't optimization chance.
104         if (!(TargetInRangeForImmU32 || DisplacementInRangeForImmS32))
105           continue;
106 
107         // Transform "mov foo@GOTPCREL(%rip),%reg" to "lea foo(%rip),%reg".
108         if (Op == 0x8b && DisplacementInRangeForImmS32) {
109           FixupData[-2] = 0x8d;
110           E.setKind(x86_64::Delta32);
111           E.setTarget(GOTTarget);
112           E.setAddend(E.getAddend() - 4);
113           LLVM_DEBUG({
114             dbgs() << "  Replaced GOT load wih LEA:\n    ";
115             printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
116             dbgs() << "\n";
117           });
118           continue;
119         }
120 
121         // Transform call/jmp instructions
122         if (Op == 0xff && TargetInRangeForImmU32) {
123           if (ModRM == 0x15) {
124             // ABI says we can convert "call *foo@GOTPCREL(%rip)" to "nop; call
125             // foo" But lld convert it to "addr32 call foo, because that makes
126             // result expression to be a single instruction.
127             FixupData[-2] = 0x67;
128             FixupData[-1] = 0xe8;
129             LLVM_DEBUG({
130               dbgs() << "  replaced call instruction's memory operand wih imm "
131                         "operand:\n    ";
132               printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
133               dbgs() << "\n";
134             });
135           } else {
136             // Transform "jmp *foo@GOTPCREL(%rip)" to "jmp foo; nop"
137             assert(ModRM == 0x25 && "Invalid ModRm for call/jmp instructions");
138             FixupData[-2] = 0xe9;
139             FixupData[3] = 0x90;
140             E.setOffset(E.getOffset() - 1);
141             LLVM_DEBUG({
142               dbgs() << "  replaced jmp instruction's memory operand wih imm "
143                         "operand:\n    ";
144               printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
145               dbgs() << "\n";
146             });
147           }
148           E.setKind(x86_64::Pointer32);
149           E.setTarget(GOTTarget);
150           continue;
151         }
152       } else if (E.getKind() == x86_64::BranchPCRel32ToPtrJumpStubBypassable) {
153         auto &StubBlock = E.getTarget().getBlock();
154         assert(StubBlock.getSize() == sizeof(PointerJumpStubContent) &&
155                "Stub block should be stub sized");
156         assert(StubBlock.edges_size() == 1 &&
157                "Stub block should only have one outgoing edge");
158 
159         auto &GOTBlock = StubBlock.edges().begin()->getTarget().getBlock();
160         assert(GOTBlock.getSize() == G.getPointerSize() &&
161                "GOT block should be pointer sized");
162         assert(GOTBlock.edges_size() == 1 &&
163                "GOT block should only have one outgoing edge");
164 
165         auto &GOTTarget = GOTBlock.edges().begin()->getTarget();
166         JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset();
167         JITTargetAddress TargetAddr = GOTTarget.getAddress();
168 
169         int64_t Displacement = TargetAddr - EdgeAddr + 4;
170         if (isInRangeForImmS32(Displacement)) {
171           E.setKind(x86_64::BranchPCRel32);
172           E.setTarget(GOTTarget);
173           LLVM_DEBUG({
174             dbgs() << "  Replaced stub branch with direct branch:\n    ";
175             printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind()));
176             dbgs() << "\n";
177           });
178         }
179       }
180     }
181 
182   return Error::success();
183 }
184 
185 } // end namespace x86_64
186 } // end namespace jitlink
187 } // end namespace llvm
188