1 //===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- C++ -*-=// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H 11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H 12 13 #include "../RuntimeDyldMachO.h" 14 15 #define DEBUG_TYPE "dyld" 16 17 namespace llvm { 18 19 class RuntimeDyldMachOX86_64 20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> { 21 public: 22 23 typedef uint64_t TargetPtrT; 24 RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager & MM,RuntimeDyld::SymbolResolver & Resolver)25 RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM, 26 RuntimeDyld::SymbolResolver &Resolver) 27 : RuntimeDyldMachOCRTPBase(MM, Resolver) {} 28 getMaxStubSize()29 unsigned getMaxStubSize() override { return 8; } 30 getStubAlignment()31 unsigned getStubAlignment() override { return 1; } 32 33 relocation_iterator processRelocationRef(unsigned SectionID,relocation_iterator RelI,const ObjectFile & BaseObjT,ObjSectionToIDMap & ObjSectionToID,StubMap & Stubs)34 processRelocationRef(unsigned SectionID, relocation_iterator RelI, 35 const ObjectFile &BaseObjT, 36 ObjSectionToIDMap &ObjSectionToID, 37 StubMap &Stubs) override { 38 const MachOObjectFile &Obj = 39 static_cast<const MachOObjectFile &>(BaseObjT); 40 MachO::any_relocation_info RelInfo = 41 Obj.getRelocation(RelI->getRawDataRefImpl()); 42 43 assert(!Obj.isRelocationScattered(RelInfo) && 44 "Scattered relocations not supported on X86_64"); 45 46 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI)); 47 RE.Addend = memcpyAddend(RE); 48 RelocationValueRef Value( 49 getRelocationValueRef(Obj, RelI, RE, ObjSectionToID)); 50 51 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo); 52 if (!IsExtern && RE.IsPCRel) 53 makeValueAddendPCRel(Value, Obj, RelI, 1 << RE.Size); 54 55 if (RE.RelType == MachO::X86_64_RELOC_GOT || 56 RE.RelType == MachO::X86_64_RELOC_GOT_LOAD) 57 processGOTRelocation(RE, Value, Stubs); 58 else { 59 RE.Addend = Value.Offset; 60 if (Value.SymbolName) 61 addRelocationForSymbol(RE, Value.SymbolName); 62 else 63 addRelocationForSection(RE, Value.SectionID); 64 } 65 66 return ++RelI; 67 } 68 resolveRelocation(const RelocationEntry & RE,uint64_t Value)69 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { 70 DEBUG(dumpRelocationToResolve(RE, Value)); 71 const SectionEntry &Section = Sections[RE.SectionID]; 72 uint8_t *LocalAddress = Section.Address + RE.Offset; 73 74 // If the relocation is PC-relative, the value to be encoded is the 75 // pointer difference. 76 if (RE.IsPCRel) { 77 // FIXME: It seems this value needs to be adjusted by 4 for an effective 78 // PC address. Is that expected? Only for branches, perhaps? 79 uint64_t FinalAddress = Section.LoadAddress + RE.Offset; 80 Value -= FinalAddress + 4; 81 } 82 83 switch (RE.RelType) { 84 default: 85 llvm_unreachable("Invalid relocation type!"); 86 case MachO::X86_64_RELOC_SIGNED_1: 87 case MachO::X86_64_RELOC_SIGNED_2: 88 case MachO::X86_64_RELOC_SIGNED_4: 89 case MachO::X86_64_RELOC_SIGNED: 90 case MachO::X86_64_RELOC_UNSIGNED: 91 case MachO::X86_64_RELOC_BRANCH: 92 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size); 93 break; 94 case MachO::X86_64_RELOC_GOT_LOAD: 95 case MachO::X86_64_RELOC_GOT: 96 case MachO::X86_64_RELOC_SUBTRACTOR: 97 case MachO::X86_64_RELOC_TLV: 98 Error("Relocation type not implemented yet!"); 99 } 100 } 101 finalizeSection(const ObjectFile & Obj,unsigned SectionID,const SectionRef & Section)102 void finalizeSection(const ObjectFile &Obj, unsigned SectionID, 103 const SectionRef &Section) {} 104 105 private: processGOTRelocation(const RelocationEntry & RE,RelocationValueRef & Value,StubMap & Stubs)106 void processGOTRelocation(const RelocationEntry &RE, 107 RelocationValueRef &Value, StubMap &Stubs) { 108 SectionEntry &Section = Sections[RE.SectionID]; 109 assert(RE.IsPCRel); 110 assert(RE.Size == 2); 111 Value.Offset -= RE.Addend; 112 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); 113 uint8_t *Addr; 114 if (i != Stubs.end()) { 115 Addr = Section.Address + i->second; 116 } else { 117 Stubs[Value] = Section.StubOffset; 118 uint8_t *GOTEntry = Section.Address + Section.StubOffset; 119 RelocationEntry GOTRE(RE.SectionID, Section.StubOffset, 120 MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false, 121 3); 122 if (Value.SymbolName) 123 addRelocationForSymbol(GOTRE, Value.SymbolName); 124 else 125 addRelocationForSection(GOTRE, Value.SectionID); 126 Section.StubOffset += 8; 127 Addr = GOTEntry; 128 } 129 RelocationEntry TargetRE(RE.SectionID, RE.Offset, 130 MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2); 131 resolveRelocation(TargetRE, (uint64_t)Addr); 132 } 133 }; 134 } 135 136 #undef DEBUG_TYPE 137 138 #endif 139