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.getAddressWithOffset(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.getLoadAddressWithOffset(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].getLoadAddress();
100 uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
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.getAddressWithOffset(i->second);
129 Stubs[Value] = Section.getStubOffset();
130 uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset());
131 RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(),
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.advanceStubOffset(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 uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);
158 unsigned NumBytes = 1 << Size;
160 ErrorOr<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();
161 if (auto EC = SubtrahendNameOrErr.getError())
162 report_fatal_error(EC.message());
163 auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);
164 unsigned SectionBID = SubtrahendI->second.getSectionID();
165 uint64_t SectionBOffset = SubtrahendI->second.getOffset();
167 SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);
170 ErrorOr<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();
171 if (auto EC = MinuendNameOrErr.getError())
172 report_fatal_error(EC.message());
173 auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);
174 unsigned SectionAID = MinuendI->second.getSectionID();
175 uint64_t SectionAOffset = MinuendI->second.getOffset();
177 RelocationEntry R(SectionID, Offset, MachO::X86_64_RELOC_SUBTRACTOR, (uint64_t)Addend,
178 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
181 addRelocationForSection(R, SectionAID);