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_RUNTIMEDYLDMACHOX86_64_H
11 #define LLVM_RUNTIMEDYLDMACHOX86_64_H
13 #include "../RuntimeDyldMachO.h"
15 #define DEBUG_TYPE "dyld"
19 class RuntimeDyldMachOX86_64
20 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
22 RuntimeDyldMachOX86_64(RTDyldMemoryManager *MM)
23 : RuntimeDyldMachOCRTPBase(MM) {}
25 unsigned getMaxStubSize() override { return 8; }
27 unsigned getStubAlignment() override { return 1; }
30 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
31 ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
32 const SymbolTableMap &Symbols, StubMap &Stubs) override {
33 const MachOObjectFile &Obj =
34 static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
35 MachO::any_relocation_info RelInfo =
36 Obj.getRelocation(RelI->getRawDataRefImpl());
38 assert(!Obj.isRelocationScattered(RelInfo) &&
39 "Scattered relocations not supported on X86_64");
41 RelocationEntry RE(getBasicRelocationEntry(SectionID, ObjImg, RelI));
42 RelocationValueRef Value(
43 getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
45 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
46 if (!IsExtern && RE.IsPCRel)
47 makeValueAddendPCRel(Value, ObjImg, RelI);
49 if (RE.RelType == MachO::X86_64_RELOC_GOT ||
50 RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
51 processGOTRelocation(RE, Value, Stubs);
53 RE.Addend = Value.Addend;
55 addRelocationForSymbol(RE, Value.SymbolName);
57 addRelocationForSection(RE, Value.SectionID);
63 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) {
64 DEBUG(dumpRelocationToResolve(RE, Value));
65 const SectionEntry &Section = Sections[RE.SectionID];
66 uint8_t *LocalAddress = Section.Address + RE.Offset;
68 // If the relocation is PC-relative, the value to be encoded is the
69 // pointer difference.
71 // FIXME: It seems this value needs to be adjusted by 4 for an effective
72 // PC address. Is that expected? Only for branches, perhaps?
73 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
74 Value -= FinalAddress + 4;
79 llvm_unreachable("Invalid relocation type!");
80 case MachO::X86_64_RELOC_SIGNED_1:
81 case MachO::X86_64_RELOC_SIGNED_2:
82 case MachO::X86_64_RELOC_SIGNED_4:
83 case MachO::X86_64_RELOC_SIGNED:
84 case MachO::X86_64_RELOC_UNSIGNED:
85 case MachO::X86_64_RELOC_BRANCH:
86 writeBytesUnaligned(LocalAddress, Value + RE.Addend, 1 << RE.Size);
88 case MachO::X86_64_RELOC_GOT_LOAD:
89 case MachO::X86_64_RELOC_GOT:
90 case MachO::X86_64_RELOC_SUBTRACTOR:
91 case MachO::X86_64_RELOC_TLV:
92 Error("Relocation type not implemented yet!");
96 void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
97 const SectionRef &Section) {}
100 void processGOTRelocation(const RelocationEntry &RE,
101 RelocationValueRef &Value, StubMap &Stubs) {
102 SectionEntry &Section = Sections[RE.SectionID];
104 assert(RE.Size == 2);
105 Value.Addend -= RE.Addend;
106 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
108 if (i != Stubs.end()) {
109 Addr = Section.Address + i->second;
111 Stubs[Value] = Section.StubOffset;
112 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
113 RelocationEntry GOTRE(RE.SectionID, Section.StubOffset,
114 MachO::X86_64_RELOC_UNSIGNED, Value.Addend, false,
116 if (Value.SymbolName)
117 addRelocationForSymbol(GOTRE, Value.SymbolName);
119 addRelocationForSection(GOTRE, Value.SectionID);
120 Section.StubOffset += 8;
123 RelocationEntry TargetRE(RE.SectionID, RE.Offset,
124 MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
125 resolveRelocation(TargetRE, (uint64_t)Addr);
132 #endif // LLVM_RUNTIMEDYLDMACHOX86_64_H