1 //===-- RuntimeDyldMachOX86_64.h ---- MachO/X86_64 specific code. -*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
11 #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOX86_64_H
13 #include "../RuntimeDyldMachO.h"
15 #define DEBUG_TYPE "dyld"
19 class RuntimeDyldMachOX86_64
20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
23 typedef uint64_t TargetPtrT;
25 RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM,
26 RuntimeDyld::SymbolResolver &Resolver)
27 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
29 unsigned getMaxStubSize() override { return 8; }
31 unsigned getStubAlignment() override { return 1; }
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());
43 assert(!Obj.isRelocationScattered(RelInfo) &&
44 "Scattered relocations not supported on X86_64");
46 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
47 RE.Addend = memcpyAddend(RE);
48 RelocationValueRef Value(
49 getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
51 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
52 if (!IsExtern && RE.IsPCRel)
53 makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
55 if (RE.RelType == MachO::X86_64_RELOC_GOT ||
56 RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
57 processGOTRelocation(RE, Value, Stubs);
59 RE.Addend = Value.Offset;
61 addRelocationForSymbol(RE, Value.SymbolName);
63 addRelocationForSection(RE, Value.SectionID);
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;
74 // If the relocation is PC-relative, the value to be encoded is the
75 // pointer difference.
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;
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);
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!");
102 void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
103 const SectionRef &Section) {}
106 void processGOTRelocation(const RelocationEntry &RE,
107 RelocationValueRef &Value, StubMap &Stubs) {
108 SectionEntry &Section = Sections[RE.SectionID];
110 assert(RE.Size == 2);
111 Value.Offset -= RE.Addend;
112 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
114 if (i != Stubs.end()) {
115 Addr = Section.Address + i->second;
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,
122 if (Value.SymbolName)
123 addRelocationForSymbol(GOTRE, Value.SymbolName);
125 addRelocationForSection(GOTRE, Value.SectionID);
126 Section.StubOffset += 8;
129 RelocationEntry TargetRE(RE.SectionID, RE.Offset,
130 MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
131 resolveRelocation(TargetRE, (uint64_t)Addr);