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::PCRel32GOTLoadREXRelaxable) { 78 // Replace GOT load with LEA only for MOVQ instructions. 79 assert(E.getOffset() >= 3 && "GOT edge occurs too early in block"); 80 81 constexpr uint8_t MOVQRIPRel[] = {0x48, 0x8b}; 82 if (strncmp(B->getContent().data() + E.getOffset() - 3, 83 reinterpret_cast<const char *>(MOVQRIPRel), 2) != 0) 84 continue; 85 86 auto &GOTBlock = E.getTarget().getBlock(); 87 assert(GOTBlock.getSize() == G.getPointerSize() && 88 "GOT entry block should be pointer sized"); 89 assert(GOTBlock.edges_size() == 1 && 90 "GOT entry should only have one outgoing edge"); 91 92 auto &GOTTarget = GOTBlock.edges().begin()->getTarget(); 93 JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset(); 94 JITTargetAddress TargetAddr = GOTTarget.getAddress(); 95 96 int64_t Displacement = TargetAddr - EdgeAddr + 4; 97 if (isInRangeForImmS32(Displacement)) { 98 // Change the edge kind as we don't go through GOT anymore. This is 99 // for formal correctness only. Technically, the two relocation kinds 100 // are resolved the same way. 101 E.setKind(x86_64::Delta32); 102 E.setTarget(GOTTarget); 103 E.setAddend(E.getAddend() - 4); 104 auto *BlockData = reinterpret_cast<uint8_t *>( 105 const_cast<char *>(B->getContent().data())); 106 BlockData[E.getOffset() - 2] = 0x8d; 107 LLVM_DEBUG({ 108 dbgs() << " Replaced GOT load wih LEA:\n "; 109 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind())); 110 dbgs() << "\n"; 111 }); 112 } 113 } else if (E.getKind() == x86_64::BranchPCRel32ToPtrJumpStubBypassable) { 114 auto &StubBlock = E.getTarget().getBlock(); 115 assert(StubBlock.getSize() == sizeof(PointerJumpStubContent) && 116 "Stub block should be stub sized"); 117 assert(StubBlock.edges_size() == 1 && 118 "Stub block should only have one outgoing edge"); 119 120 auto &GOTBlock = StubBlock.edges().begin()->getTarget().getBlock(); 121 assert(GOTBlock.getSize() == G.getPointerSize() && 122 "GOT block should be pointer sized"); 123 assert(GOTBlock.edges_size() == 1 && 124 "GOT block should only have one outgoing edge"); 125 126 auto &GOTTarget = GOTBlock.edges().begin()->getTarget(); 127 JITTargetAddress EdgeAddr = B->getAddress() + E.getOffset(); 128 JITTargetAddress TargetAddr = GOTTarget.getAddress(); 129 130 int64_t Displacement = TargetAddr - EdgeAddr + 4; 131 if (isInRangeForImmS32(Displacement)) { 132 E.setKind(x86_64::BranchPCRel32); 133 E.setTarget(GOTTarget); 134 LLVM_DEBUG({ 135 dbgs() << " Replaced stub branch with direct branch:\n "; 136 printEdge(dbgs(), *B, E, getEdgeKindName(E.getKind())); 137 dbgs() << "\n"; 138 }); 139 } 140 } 141 142 return Error::success(); 143 } 144 145 } // end namespace x86_64 146 } // end namespace jitlink 147 } // end namespace llvm 148