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
17namespace llvm {
18
19class RuntimeDyldMachOX86_64
20    : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOX86_64> {
21public:
22
23  typedef uint64_t TargetPtrT;
24
25  RuntimeDyldMachOX86_64(RuntimeDyld::MemoryManager &MM,
26                         RuntimeDyld::SymbolResolver &Resolver)
27      : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
28
29  unsigned getMaxStubSize() override { return 8; }
30
31  unsigned getStubAlignment() override { return 1; }
32
33  relocation_iterator
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);
43
44    if (RelType == MachO::X86_64_RELOC_SUBTRACTOR)
45      return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
46
47    assert(!Obj.isRelocationScattered(RelInfo) &&
48           "Scattered relocations not supported on X86_64");
49
50    RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
51    RE.Addend = memcpyAddend(RE);
52    RelocationValueRef Value(
53        getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
54
55    bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
56    if (!IsExtern && RE.IsPCRel)
57      makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
58
59    if (RE.RelType == MachO::X86_64_RELOC_GOT ||
60        RE.RelType == MachO::X86_64_RELOC_GOT_LOAD)
61      processGOTRelocation(RE, Value, Stubs);
62    else {
63      RE.Addend = Value.Offset;
64      if (Value.SymbolName)
65        addRelocationForSymbol(RE, Value.SymbolName);
66      else
67        addRelocationForSection(RE, Value.SectionID);
68    }
69
70    return ++RelI;
71  }
72
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);
77
78    // If the relocation is PC-relative, the value to be encoded is the
79    // pointer difference.
80    if (RE.IsPCRel) {
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;
85    }
86
87    switch (RE.RelType) {
88    default:
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);
97      break;
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);
105      break;
106    }
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!");
111    }
112  }
113
114  void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
115                       const SectionRef &Section) {}
116
117private:
118  void processGOTRelocation(const RelocationEntry &RE,
119                            RelocationValueRef &Value, StubMap &Stubs) {
120    SectionEntry &Section = Sections[RE.SectionID];
121    assert(RE.IsPCRel);
122    assert(RE.Size == 2);
123    Value.Offset -= RE.Addend;
124    RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value);
125    uint8_t *Addr;
126    if (i != Stubs.end()) {
127      Addr = Section.getAddressWithOffset(i->second);
128    } else {
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,
133                            3);
134      if (Value.SymbolName)
135        addRelocationForSymbol(GOTRE, Value.SymbolName);
136      else
137        addRelocationForSection(GOTRE, Value.SectionID);
138      Section.advanceStubOffset(8);
139      Addr = GOTEntry;
140    }
141    RelocationEntry TargetRE(RE.SectionID, RE.Offset,
142                             MachO::X86_64_RELOC_UNSIGNED, RE.Addend, true, 2);
143    resolveRelocation(TargetRE, (uint64_t)Addr);
144  }
145
146  relocation_iterator
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());
154
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;
159
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();
166    int64_t Addend =
167      SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);
168
169    ++RelI;
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();
176
177    RelocationEntry R(SectionID, Offset, MachO::X86_64_RELOC_SUBTRACTOR, (uint64_t)Addend,
178                      SectionAID, SectionAOffset, SectionBID, SectionBOffset,
179                      false, Size);
180
181    addRelocationForSection(R, SectionAID);
182
183    return ++RelI;
184  }
185
186};
187}
188
189#undef DEBUG_TYPE
190
191#endif
192