[MCJIT] Simplify immediate decoding code in the RuntimeDyldMachO hierarchy.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldMachO.cpp
1 //===-- RuntimeDyldMachO.cpp - Run-time dynamic linker for MC-JIT -*- 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 // Implementation of the MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "RuntimeDyldMachO.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringRef.h"
17
18 #include "Targets/RuntimeDyldMachOARM.h"
19 #include "Targets/RuntimeDyldMachOAArch64.h"
20 #include "Targets/RuntimeDyldMachOI386.h"
21 #include "Targets/RuntimeDyldMachOX86_64.h"
22
23 using namespace llvm;
24 using namespace llvm::object;
25
26 #define DEBUG_TYPE "dyld"
27
28 namespace llvm {
29
30 int64_t RuntimeDyldMachO::memcpyAddend(const RelocationEntry &RE) const {
31   const SectionEntry &Section = Sections[RE.SectionID];
32   uint8_t *LocalAddress = Section.Address + RE.Offset;
33   unsigned NumBytes = 1 << RE.Size;
34   int64_t Addend = 0;
35   memcpy(&Addend, LocalAddress, NumBytes);
36   return Addend;
37 }
38
39 RelocationValueRef RuntimeDyldMachO::getRelocationValueRef(
40     ObjectImage &ObjImg, const relocation_iterator &RI,
41     const RelocationEntry &RE, ObjSectionToIDMap &ObjSectionToID,
42     const SymbolTableMap &Symbols) {
43
44   const MachOObjectFile &Obj =
45       static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
46   MachO::any_relocation_info RelInfo =
47       Obj.getRelocation(RI->getRawDataRefImpl());
48   RelocationValueRef Value;
49
50   bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
51   if (IsExternal) {
52     symbol_iterator Symbol = RI->getSymbol();
53     StringRef TargetName;
54     Symbol->getName(TargetName);
55     SymbolTableMap::const_iterator SI = Symbols.find(TargetName.data());
56     if (SI != Symbols.end()) {
57       Value.SectionID = SI->second.first;
58       Value.Addend = SI->second.second + RE.Addend;
59     } else {
60       SI = GlobalSymbolTable.find(TargetName.data());
61       if (SI != GlobalSymbolTable.end()) {
62         Value.SectionID = SI->second.first;
63         Value.Addend = SI->second.second + RE.Addend;
64       } else {
65         Value.SymbolName = TargetName.data();
66         Value.Addend = RE.Addend;
67       }
68     }
69   } else {
70     SectionRef Sec = Obj.getRelocationSection(RelInfo);
71     bool IsCode = false;
72     Sec.isText(IsCode);
73     Value.SectionID = findOrEmitSection(ObjImg, Sec, IsCode, ObjSectionToID);
74     uint64_t Addr;
75     Sec.getAddress(Addr);
76     Value.Addend = RE.Addend - Addr;
77   }
78
79   return Value;
80 }
81
82 void RuntimeDyldMachO::makeValueAddendPCRel(RelocationValueRef &Value,
83                                             ObjectImage &ObjImg,
84                                             const relocation_iterator &RI,
85                                             unsigned OffsetToNextPC) {
86   const MachOObjectFile &Obj =
87       static_cast<const MachOObjectFile &>(*ObjImg.getObjectFile());
88   MachO::any_relocation_info RelInfo =
89       Obj.getRelocation(RI->getRawDataRefImpl());
90
91   bool IsPCRel = Obj.getAnyRelocationPCRel(RelInfo);
92   if (IsPCRel) {
93     uint64_t RelocAddr = 0;
94     RI->getAddress(RelocAddr);
95     Value.Addend += RelocAddr + OffsetToNextPC;
96   }
97 }
98
99 void RuntimeDyldMachO::dumpRelocationToResolve(const RelocationEntry &RE,
100                                                uint64_t Value) const {
101   const SectionEntry &Section = Sections[RE.SectionID];
102   uint8_t *LocalAddress = Section.Address + RE.Offset;
103   uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
104
105   dbgs() << "resolveRelocation Section: " << RE.SectionID
106          << " LocalAddress: " << format("%p", LocalAddress)
107          << " FinalAddress: " << format("%p", FinalAddress)
108          << " Value: " << format("%p", Value) << " Addend: " << RE.Addend
109          << " isPCRel: " << RE.IsPCRel << " MachoType: " << RE.RelType
110          << " Size: " << (1 << RE.Size) << "\n";
111 }
112
113 bool RuntimeDyldMachO::writeBytesUnaligned(uint8_t *Addr, uint64_t Value,
114                                            unsigned Size) {
115   for (unsigned i = 0; i < Size; ++i) {
116     *Addr++ = (uint8_t)Value;
117     Value >>= 8;
118   }
119
120   return false;
121 }
122
123 bool
124 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
125   if (InputBuffer->getBufferSize() < 4)
126     return false;
127   StringRef Magic(InputBuffer->getBufferStart(), 4);
128   if (Magic == "\xFE\xED\xFA\xCE")
129     return true;
130   if (Magic == "\xCE\xFA\xED\xFE")
131     return true;
132   if (Magic == "\xFE\xED\xFA\xCF")
133     return true;
134   if (Magic == "\xCF\xFA\xED\xFE")
135     return true;
136   return false;
137 }
138
139 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
140   return Obj->isMachO();
141 }
142
143 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
144                                  intptr_t DeltaForEH) {
145   DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
146                << ", Delta for EH: " << DeltaForEH << "\n");
147   uint32_t Length = *((uint32_t *)P);
148   P += 4;
149   unsigned char *Ret = P + Length;
150   uint32_t Offset = *((uint32_t *)P);
151   if (Offset == 0) // is a CIE
152     return Ret;
153
154   P += 4;
155   intptr_t FDELocation = *((intptr_t *)P);
156   intptr_t NewLocation = FDELocation - DeltaForText;
157   *((intptr_t *)P) = NewLocation;
158   P += sizeof(intptr_t);
159
160   // Skip the FDE address range
161   P += sizeof(intptr_t);
162
163   uint8_t Augmentationsize = *P;
164   P += 1;
165   if (Augmentationsize != 0) {
166     intptr_t LSDA = *((intptr_t *)P);
167     intptr_t NewLSDA = LSDA - DeltaForEH;
168     *((intptr_t *)P) = NewLSDA;
169   }
170
171   return Ret;
172 }
173
174 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
175   intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
176   intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
177   return ObjDistance - MemDistance;
178 }
179
180 void RuntimeDyldMachO::registerEHFrames() {
181
182   if (!MemMgr)
183     return;
184   for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
185     EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
186     if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
187         SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
188       continue;
189     SectionEntry *Text = &Sections[SectionInfo.TextSID];
190     SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
191     SectionEntry *ExceptTab = nullptr;
192     if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
193       ExceptTab = &Sections[SectionInfo.ExceptTabSID];
194
195     intptr_t DeltaForText = computeDelta(Text, EHFrame);
196     intptr_t DeltaForEH = 0;
197     if (ExceptTab)
198       DeltaForEH = computeDelta(ExceptTab, EHFrame);
199
200     unsigned char *P = EHFrame->Address;
201     unsigned char *End = P + EHFrame->Size;
202     do {
203       P = processFDE(P, DeltaForText, DeltaForEH);
204     } while (P != End);
205
206     MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
207                              EHFrame->Size);
208   }
209   UnregisteredEHFrameSections.clear();
210 }
211
212 std::unique_ptr<RuntimeDyldMachO>
213 llvm::RuntimeDyldMachO::create(Triple::ArchType Arch, RTDyldMemoryManager *MM) {
214   switch (Arch) {
215   default:
216     llvm_unreachable("Unsupported target for RuntimeDyldMachO.");
217     break;
218   case Triple::arm: return make_unique<RuntimeDyldMachOARM>(MM);
219   case Triple::aarch64: return make_unique<RuntimeDyldMachOAArch64>(MM);
220   case Triple::x86: return make_unique<RuntimeDyldMachOI386>(MM);
221   case Triple::x86_64: return make_unique<RuntimeDyldMachOX86_64>(MM);
222   }
223 }
224
225 } // end namespace llvm