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