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());
42 uint32_t RelType = Obj.getAnyRelocationType(RelInfo);
44 if (RelType == MachO::X86_64_RELOC_SUBTRACTOR)
45 return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
47 assert(!Obj.isRelocationScattered(RelInfo) &&
48 "Scattered relocations not supported on X86_64");
50 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
51 RE.Addend = memcpyAddend(RE);
52 RelocationValueRef Value(
53 getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
55 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
56 if (!IsExtern && RE.IsPCRel)
57 makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
59 if (RE.RelType == MachO::X86_64_RELOC_GOT ||
60 RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
61 processGOTRelocation(RE, Value, Stubs);
63 RE.Addend = Value.Offset;
65 addRelocationForSymbol(RE, Value.SymbolName);
67 addRelocationForSection(RE, Value.SectionID);
73 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
74 DEBUG(dumpRelocationToResolve(RE, Value));
75 const SectionEntry &Section = Sections[RE.SectionID];
76 uint8_t *LocalAddress = Section.Address + RE.Offset;
78 // If the relocation is PC-relative, the value to be encoded is the
79 // pointer difference.
81 // FIXME: It seems this value needs to be adjusted by 4 for an effective
82 // PC address. Is that expected? Only for branches, perhaps?
83 uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
84 Value -= FinalAddress + 4;
89 llvm_unreachable("Invalid relocation type!");
90 case MachO::X86_64_RELOC_SIGNED_1:
91 case MachO::X86_64_RELOC_SIGNED_2:
92 case MachO::X86_64_RELOC_SIGNED_4:
93 case MachO::X86_64_RELOC_SIGNED:
94 case MachO::X86_64_RELOC_UNSIGNED:
95 case MachO::X86_64_RELOC_BRANCH:
96 writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
98 case MachO::X86_64_RELOC_SUBTRACTOR: {
99 uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
100 uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
101 assert((Value == SectionABase || Value == SectionBBase) &&
102 "Unexpected SUBTRACTOR relocation value.");
103 Value = SectionABase - SectionBBase + RE.Addend;
104 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
107 case MachO::X86_64_RELOC_GOT_LOAD:
108 case MachO::X86_64_RELOC_GOT:
109 case MachO::X86_64_RELOC_TLV:
110 Error("Relocation type not implemented yet!");
114 void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
115 const SectionRef &Section) {}
118 void processGOTRelocation(const RelocationEntry &RE,
119 RelocationValueRef &Value, StubMap &Stubs) {
120 SectionEntry &Section = Sections[RE.SectionID];
122 assert(RE.Size == 2);
123 Value.Offset -= RE.Addend;
124 RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
126 if (i != Stubs.end()) {
127 Addr = Section.Address + i->second;
129 Stubs[Value] = Section.StubOffset;
130 uint8_t *GOTEntry = Section.Address + Section.StubOffset;
131 RelocationEntry GOTRE(RE.SectionID, Section.StubOffset,
132 MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
134 if (Value.SymbolName)
135 addRelocationForSymbol(GOTRE, Value.SymbolName);
137 addRelocationForSection(GOTRE, Value.SectionID);
138 Section.StubOffset += 8;
141 RelocationEntry TargetRE(RE.SectionID, RE.Offset,
142 MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
143 resolveRelocation(TargetRE, (uint64_t)Addr);
147 processSubtractRelocation(unsigned SectionID, relocation_iterator RelI,
148 const ObjectFile &BaseObjT,
149 ObjSectionToIDMap &ObjSectionToID) {
150 const MachOObjectFile &Obj =
151 static_cast<const MachOObjectFile&>(BaseObjT);
152 MachO::any_relocation_info RE =
153 Obj.getRelocation(RelI->getRawDataRefImpl());
155 unsigned Size = Obj.getAnyRelocationLength(RE);
156 uint64_t Offset = RelI->getOffset();
157 ErrorOr<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();
158 if (auto EC = SubtrahendNameOrErr.getError())
159 report_fatal_error(EC.message());
160 auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);
161 unsigned SectionBID = SubtrahendI->second.getSectionID();
162 uint64_t SectionBOffset = SubtrahendI->second.getOffset();
165 ErrorOr<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();
166 if (auto EC = MinuendNameOrErr.getError())
167 report_fatal_error(EC.message());
168 auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);
169 unsigned SectionAID = MinuendI->second.getSectionID();
170 uint64_t SectionAOffset = MinuendI->second.getOffset();
172 uint64_t Addend = SectionAOffset - SectionBOffset;
173 RelocationEntry R(SectionID, Offset, MachO::X86_64_RELOC_SUBTRACTOR, Addend,
174 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
177 addRelocationForSection(R, SectionAID);