Reverting r222828 and r222810-r222812 as they broke the build on Windows.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / Targets / RuntimeDyldMachOX86_64.h
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
25   RuntimeDyldMachOX86_64(RTDyldMemoryManager *MM)
26       : RuntimeDyldMachOCRTPBase(MM) {}
27
28   unsigned getMaxStubSize() override { return 8; }
29
30   unsigned getStubAlignment() override { return 1; }
31
32   relocation_iterator
33   processRelocationRef(unsigned SectionID, relocation_iterator RelI,
34                        ObjectImage &ObjImg, ObjSectionToIDMap &ObjSectionToID,
35                        const SymbolTableMap &Symbols, StubMap &Stubs) override {
36     const MachOObjectFile &Obj =
37         static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
38     MachO::any_relocation_info RelInfo =
39         Obj.getRelocation(RelI->getRawDataRefImpl());
40
41     assert(!Obj.isRelocationScattered(RelInfo) &&
42            "Scattered relocations not supported on X86_64");
43
44     RelocationEntry RE(getRelocationEntry(SectionID, ObjImg, RelI));
45     RE.Addend = memcpyAddend(RE);
46     RelocationValueRef Value(
47         getRelocationValueRef(ObjImg, RelI, RE, ObjSectionToID, Symbols));
48
49     bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
50     if (!IsExtern && RE.IsPCRel)
51       makeValueAddendPCRel(Value, ObjImg, RelI, 1 << RE.Size);
52
53     if (RE.RelType == MachO::X86_64_RELOC_GOT ||
54         RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
55       processGOTRelocation(RE, Value, Stubs);
56     else {
57       RE.Addend = Value.Offset;
58       if (Value.SymbolName)
59         addRelocationForSymbol(RE, Value.SymbolName);
60       else
61         addRelocationForSection(RE, Value.SectionID);
62     }
63
64     return ++RelI;
65   }
66
67   void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
68     DEBUG(dumpRelocationToResolve(RE, Value));
69     const SectionEntry &Section = Sections[RE.SectionID];
70     uint8_t *LocalAddress = Section.Address + RE.Offset;
71
72     // If the relocation is PC-relative, the value to be encoded is the
73     // pointer difference.
74     if (RE.IsPCRel) {
75       // FIXME: It seems this value needs to be adjusted by 4 for an effective
76       // PC address. Is that expected? Only for branches, perhaps?
77       uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
78       Value -= FinalAddress + 4;
79     }
80
81     switch (RE.RelType) {
82     default:
83       llvm_unreachable("Invalid relocation type!");
84     case MachO::X86_64_RELOC_SIGNED_1:
85     case MachO::X86_64_RELOC_SIGNED_2:
86     case MachO::X86_64_RELOC_SIGNED_4:
87     case MachO::X86_64_RELOC_SIGNED:
88     case MachO::X86_64_RELOC_UNSIGNED:
89     case MachO::X86_64_RELOC_BRANCH:
90       writeBytesUnaligned(Value + RE.Addend, LocalAddress, 1 << RE.Size);
91       break;
92     case MachO::X86_64_RELOC_GOT_LOAD:
93     case MachO::X86_64_RELOC_GOT:
94     case MachO::X86_64_RELOC_SUBTRACTOR:
95     case MachO::X86_64_RELOC_TLV:
96       Error("Relocation type not implemented yet!");
97     }
98   }
99
100   void finalizeSection(ObjectImage &ObjImg, unsigned SectionID,
101                        const SectionRef &Section) {}
102
103 private:
104   void processGOTRelocation(const RelocationEntry &RE,
105                             RelocationValueRef &Value, StubMap &Stubs) {
106     SectionEntry &Section = Sections[RE.SectionID];
107     assert(RE.IsPCRel);
108     assert(RE.Size == 2);
109     Value.Offset -= RE.Addend;
110     RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
111     uint8_t *Addr;
112     if (i != Stubs.end()) {
113       Addr = Section.Address + i->second;
114     } else {
115       Stubs[Value] = Section.StubOffset;
116       uint8_t *GOTEntry = Section.Address + Section.StubOffset;
117       RelocationEntry GOTRE(RE.SectionID, Section.StubOffset,
118                             MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,
119                             3);
120       if (Value.SymbolName)
121         addRelocationForSymbol(GOTRE, Value.SymbolName);
122       else
123         addRelocationForSection(GOTRE, Value.SectionID);
124       Section.StubOffset += 8;
125       Addr = GOTEntry;
126     }
127     RelocationEntry TargetRE(RE.SectionID, RE.Offset,
128                              MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
129     resolveRelocation(TargetRE, (uint64_t)Addr);
130   }
131 };
132 }
133
134 #undef DEBUG_TYPE
135
136 #endif