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