0f9fc9169dbe0638da84c1fc3d0cad275e05c045
[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 #define DEBUG_TYPE "dyld"
15 #include "RuntimeDyldMachO.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 using namespace llvm;
19 using namespace llvm::object;
20
21 namespace llvm {
22
23 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText, intptr_t DeltaForEH) {
24   uint32_t Length = *((uint32_t*)P);
25   P += 4;
26   unsigned char *Ret = P + Length;
27   uint32_t Offset = *((uint32_t*)P);
28   if (Offset == 0) // is a CIE
29     return Ret;
30
31   P += 4;
32   intptr_t FDELocation = *((intptr_t*)P);
33   intptr_t NewLocation = FDELocation - DeltaForText;
34   *((intptr_t*)P) = NewLocation;
35   P += sizeof(intptr_t);
36
37   // Skip the FDE address range
38   P += sizeof(intptr_t);
39
40   uint8_t Augmentationsize = *P;
41   P += 1;
42   if (Augmentationsize != 0) {
43     intptr_t LSDA = *((intptr_t*)P);
44     intptr_t NewLSDA = LSDA - DeltaForEH;
45     *((intptr_t*)P) = NewLSDA;
46   }
47
48   return Ret;
49 }
50
51 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
52   intptr_t ObjDistance = A->ObjAddress  - B->ObjAddress;
53   intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
54   return ObjDistance - MemDistance;
55 }
56
57 void RuntimeDyldMachO::registerEHFrames() {
58
59   if (!MemMgr)
60     return;
61   for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
62     EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
63     if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
64         SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
65       continue;
66     SectionEntry *Text = &Sections[SectionInfo.TextSID];
67     SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
68     SectionEntry *ExceptTab = NULL;
69     if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
70       ExceptTab = &Sections[SectionInfo.ExceptTabSID];
71
72     intptr_t DeltaForText = computeDelta(Text, EHFrame);
73     intptr_t DeltaForEH = 0;
74     if (ExceptTab)
75       DeltaForEH = computeDelta(ExceptTab, EHFrame);
76
77     unsigned char *P = EHFrame->Address;
78     unsigned char *End = P + EHFrame->Size;
79     do  {
80       P = processFDE(P, DeltaForText, DeltaForEH);
81     } while(P != End);
82
83     MemMgr->registerEHFrames(EHFrame->Address,
84                              EHFrame->LoadAddress,
85                              EHFrame->Size);
86   }
87   UnregisteredEHFrameSections.clear();
88 }
89
90 void RuntimeDyldMachO::finalizeLoad(ObjSectionToIDMap &SectionMap) {
91   unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
92   unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
93   unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
94   ObjSectionToIDMap::iterator i, e;
95   for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
96     const SectionRef &Section = i->first;
97     StringRef Name;
98     Section.getName(Name);
99     if (Name == "__eh_frame")
100       EHFrameSID = i->second;
101     else if (Name == "__text")
102       TextSID = i->second;
103     else if (Name == "__gcc_except_tab")
104       ExceptTabSID = i->second;
105   }
106   UnregisteredEHFrameSections.push_back(EHFrameRelatedSections(EHFrameSID,
107                                                                TextSID,
108                                                                ExceptTabSID));
109 }
110
111 // The target location for the relocation is described by RE.SectionID and
112 // RE.Offset.  RE.SectionID can be used to find the SectionEntry.  Each
113 // SectionEntry has three members describing its location.
114 // SectionEntry::Address is the address at which the section has been loaded
115 // into memory in the current (host) process.  SectionEntry::LoadAddress is the
116 // address that the section will have in the target process.
117 // SectionEntry::ObjAddress is the address of the bits for this section in the
118 // original emitted object image (also in the current address space).
119 //
120 // Relocations will be applied as if the section were loaded at
121 // SectionEntry::LoadAddress, but they will be applied at an address based
122 // on SectionEntry::Address.  SectionEntry::ObjAddress will be used to refer to
123 // Target memory contents if they are required for value calculations.
124 //
125 // The Value parameter here is the load address of the symbol for the
126 // relocation to be applied.  For relocations which refer to symbols in the
127 // current object Value will be the LoadAddress of the section in which
128 // the symbol resides (RE.Addend provides additional information about the
129 // symbol location).  For external symbols, Value will be the address of the
130 // symbol in the target address space.
131 void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
132                                          uint64_t Value) {
133   const SectionEntry &Section = Sections[RE.SectionID];
134   return resolveRelocation(Section, RE.Offset, Value, RE.RelType, RE.Addend,
135                            RE.IsPCRel, RE.Size);
136 }
137
138 void RuntimeDyldMachO::resolveRelocation(const SectionEntry &Section,
139                                          uint64_t Offset,
140                                          uint64_t Value,
141                                          uint32_t Type,
142                                          int64_t Addend,
143                                          bool isPCRel,
144                                          unsigned LogSize) {
145   uint8_t *LocalAddress = Section.Address + Offset;
146   uint64_t FinalAddress = Section.LoadAddress + Offset;
147   unsigned MachoType = Type;
148   unsigned Size = 1 << LogSize;
149
150   DEBUG(dbgs() << "resolveRelocation LocalAddress: "
151         << format("%p", LocalAddress)
152         << " FinalAddress: " << format("%p", FinalAddress)
153         << " Value: " << format("%p", Value)
154         << " Addend: " << Addend
155         << " isPCRel: " << isPCRel
156         << " MachoType: " << MachoType
157         << " Size: " << Size
158         << "\n");
159
160   // This just dispatches to the proper target specific routine.
161   switch (Arch) {
162   default: llvm_unreachable("Unsupported CPU type!");
163   case Triple::x86_64:
164     resolveX86_64Relocation(LocalAddress,
165                             FinalAddress,
166                             (uintptr_t)Value,
167                             isPCRel,
168                             MachoType,
169                             Size,
170                             Addend);
171     break;
172   case Triple::x86:
173     resolveI386Relocation(LocalAddress,
174                           FinalAddress,
175                           (uintptr_t)Value,
176                           isPCRel,
177                           MachoType,
178                           Size,
179                           Addend);
180     break;
181   case Triple::arm:    // Fall through.
182   case Triple::thumb:
183     resolveARMRelocation(LocalAddress,
184                          FinalAddress,
185                          (uintptr_t)Value,
186                          isPCRel,
187                          MachoType,
188                          Size,
189                          Addend);
190     break;
191   }
192 }
193
194 bool RuntimeDyldMachO::resolveI386Relocation(uint8_t *LocalAddress,
195                                              uint64_t FinalAddress,
196                                              uint64_t Value,
197                                              bool isPCRel,
198                                              unsigned Type,
199                                              unsigned Size,
200                                              int64_t Addend) {
201   if (isPCRel)
202     Value -= FinalAddress + 4; // see resolveX86_64Relocation
203
204   switch (Type) {
205   default:
206     llvm_unreachable("Invalid relocation type!");
207   case MachO::GENERIC_RELOC_VANILLA: {
208     uint8_t *p = LocalAddress;
209     uint64_t ValueToWrite = Value + Addend;
210     for (unsigned i = 0; i < Size; ++i) {
211       *p++ = (uint8_t)(ValueToWrite & 0xff);
212       ValueToWrite >>= 8;
213     }
214     return false;
215   }
216   case MachO::GENERIC_RELOC_SECTDIFF:
217   case MachO::GENERIC_RELOC_LOCAL_SECTDIFF:
218   case MachO::GENERIC_RELOC_PB_LA_PTR:
219     return Error("Relocation type not implemented yet!");
220   }
221 }
222
223 bool RuntimeDyldMachO::resolveX86_64Relocation(uint8_t *LocalAddress,
224                                                uint64_t FinalAddress,
225                                                uint64_t Value,
226                                                bool isPCRel,
227                                                unsigned Type,
228                                                unsigned Size,
229                                                int64_t Addend) {
230   // If the relocation is PC-relative, the value to be encoded is the
231   // pointer difference.
232   if (isPCRel)
233     // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
234     // address. Is that expected? Only for branches, perhaps?
235     Value -= FinalAddress + 4;
236
237   switch(Type) {
238   default:
239     llvm_unreachable("Invalid relocation type!");
240   case MachO::X86_64_RELOC_SIGNED_1:
241   case MachO::X86_64_RELOC_SIGNED_2:
242   case MachO::X86_64_RELOC_SIGNED_4:
243   case MachO::X86_64_RELOC_SIGNED:
244   case MachO::X86_64_RELOC_UNSIGNED:
245   case MachO::X86_64_RELOC_BRANCH: {
246     Value += Addend;
247     // Mask in the target value a byte at a time (we don't have an alignment
248     // guarantee for the target address, so this is safest).
249     uint8_t *p = (uint8_t*)LocalAddress;
250     for (unsigned i = 0; i < Size; ++i) {
251       *p++ = (uint8_t)Value;
252       Value >>= 8;
253     }
254     return false;
255   }
256   case MachO::X86_64_RELOC_GOT_LOAD:
257   case MachO::X86_64_RELOC_GOT:
258   case MachO::X86_64_RELOC_SUBTRACTOR:
259   case MachO::X86_64_RELOC_TLV:
260     return Error("Relocation type not implemented yet!");
261   }
262 }
263
264 bool RuntimeDyldMachO::resolveARMRelocation(uint8_t *LocalAddress,
265                                             uint64_t FinalAddress,
266                                             uint64_t Value,
267                                             bool isPCRel,
268                                             unsigned Type,
269                                             unsigned Size,
270                                             int64_t Addend) {
271   // If the relocation is PC-relative, the value to be encoded is the
272   // pointer difference.
273   if (isPCRel) {
274     Value -= FinalAddress;
275     // ARM PCRel relocations have an effective-PC offset of two instructions
276     // (four bytes in Thumb mode, 8 bytes in ARM mode).
277     // FIXME: For now, assume ARM mode.
278     Value -= 8;
279   }
280
281   switch(Type) {
282   default:
283     llvm_unreachable("Invalid relocation type!");
284   case MachO::ARM_RELOC_VANILLA: {
285     // Mask in the target value a byte at a time (we don't have an alignment
286     // guarantee for the target address, so this is safest).
287     uint8_t *p = (uint8_t*)LocalAddress;
288     for (unsigned i = 0; i < Size; ++i) {
289       *p++ = (uint8_t)Value;
290       Value >>= 8;
291     }
292     break;
293   }
294   case MachO::ARM_RELOC_BR24: {
295     // Mask the value into the target address. We know instructions are
296     // 32-bit aligned, so we can do it all at once.
297     uint32_t *p = (uint32_t*)LocalAddress;
298     // The low two bits of the value are not encoded.
299     Value >>= 2;
300     // Mask the value to 24 bits.
301     Value &= 0xffffff;
302     // FIXME: If the destination is a Thumb function (and the instruction
303     // is a non-predicated BL instruction), we need to change it to a BLX
304     // instruction instead.
305
306     // Insert the value into the instruction.
307     *p = (*p & ~0xffffff) | Value;
308     break;
309   }
310   case MachO::ARM_THUMB_RELOC_BR22:
311   case MachO::ARM_THUMB_32BIT_BRANCH:
312   case MachO::ARM_RELOC_HALF:
313   case MachO::ARM_RELOC_HALF_SECTDIFF:
314   case MachO::ARM_RELOC_PAIR:
315   case MachO::ARM_RELOC_SECTDIFF:
316   case MachO::ARM_RELOC_LOCAL_SECTDIFF:
317   case MachO::ARM_RELOC_PB_LA_PTR:
318     return Error("Relocation type not implemented yet!");
319   }
320   return false;
321 }
322
323 relocation_iterator
324 RuntimeDyldMachO::processRelocationRef(unsigned SectionID,
325                                        relocation_iterator RelI,
326                                        ObjectImage &Obj,
327                                        ObjSectionToIDMap &ObjSectionToID,
328                                        const SymbolTableMap &Symbols,
329                                        StubMap &Stubs) {
330   const ObjectFile *OF = Obj.getObjectFile();
331   const MachOObjectFile *MachO = static_cast<const MachOObjectFile*>(OF);
332   MachO::any_relocation_info RE =
333     MachO->getRelocation(RelI->getRawDataRefImpl());
334
335   uint32_t RelType = MachO->getAnyRelocationType(RE);
336
337   // FIXME: Properly handle scattered relocations.
338   //        For now, optimistically skip these: they can often be ignored, as
339   //        the static linker will already have applied the relocation, and it
340   //        only needs to be reapplied if symbols move relative to one another.
341   //        Note: This will fail horribly where the relocations *do* need to be
342   //        applied, but that was already the case.
343   if (MachO->isRelocationScattered(RE))
344     return ++RelI;
345
346   RelocationValueRef Value;
347   SectionEntry &Section = Sections[SectionID];
348
349   bool isExtern = MachO->getPlainRelocationExternal(RE);
350   bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
351   unsigned Size = MachO->getAnyRelocationLength(RE);
352   uint64_t Offset;
353   RelI->getOffset(Offset);
354   uint8_t *LocalAddress = Section.Address + Offset;
355   unsigned NumBytes = 1 << Size;
356   uint64_t Addend = 0;
357   memcpy(&Addend, LocalAddress, NumBytes);
358
359   if (isExtern) {
360     // Obtain the symbol name which is referenced in the relocation
361     symbol_iterator Symbol = RelI->getSymbol();
362     StringRef TargetName;
363     Symbol->getName(TargetName);
364     // First search for the symbol in the local symbol table
365     SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
366     if (lsi != Symbols.end()) {
367       Value.SectionID = lsi->second.first;
368       Value.Addend = lsi->second.second + Addend;
369     } else {
370       // Search for the symbol in the global symbol table
371       SymbolTableMap::const_iterator gsi = GlobalSymbolTable.find(TargetName.data());
372       if (gsi != GlobalSymbolTable.end()) {
373         Value.SectionID = gsi->second.first;
374         Value.Addend = gsi->second.second + Addend;
375       } else {
376         Value.SymbolName = TargetName.data();
377         Value.Addend = Addend;
378       }
379     }
380   } else {
381     SectionRef Sec = MachO->getRelocationSection(RE);
382     bool IsCode = false;
383     Sec.isText(IsCode);
384     Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
385     uint64_t Addr;
386     Sec.getAddress(Addr);
387     Value.Addend = Addend - Addr;
388     if (IsPCRel)
389       Value.Addend += Offset + NumBytes;
390   }
391
392   if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
393                                  RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
394     assert(IsPCRel);
395     assert(Size == 2);
396     StubMap::const_iterator i = Stubs.find(Value);
397     uint8_t *Addr;
398     if (i != Stubs.end()) {
399       Addr = Section.Address + i->second;
400     } else {
401       Stubs[Value] = Section.StubOffset;
402       uint8_t *GOTEntry = Section.Address + Section.StubOffset;
403       RelocationEntry RE(SectionID, Section.StubOffset,
404                          MachO::X86_64_RELOC_UNSIGNED, 0, false, 3);
405       if (Value.SymbolName)
406         addRelocationForSymbol(RE, Value.SymbolName);
407       else
408         addRelocationForSection(RE, Value.SectionID);
409       Section.StubOffset += 8;
410       Addr = GOTEntry;
411     }
412     resolveRelocation(Section, Offset, (uint64_t)Addr,
413                       MachO::X86_64_RELOC_UNSIGNED, Value.Addend, true, 2);
414   } else if (Arch == Triple::arm &&
415              (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
416     // This is an ARM branch relocation, need to use a stub function.
417
418     //  Look up for existing stub.
419     StubMap::const_iterator i = Stubs.find(Value);
420     if (i != Stubs.end())
421       resolveRelocation(Section, Offset,
422                         (uint64_t)Section.Address + i->second,
423                         RelType, 0, IsPCRel, Size);
424     else {
425       // Create a new stub function.
426       Stubs[Value] = Section.StubOffset;
427       uint8_t *StubTargetAddr = createStubFunction(Section.Address +
428                                                    Section.StubOffset);
429       RelocationEntry RE(SectionID, StubTargetAddr - Section.Address,
430                          MachO::GENERIC_RELOC_VANILLA, Value.Addend);
431       if (Value.SymbolName)
432         addRelocationForSymbol(RE, Value.SymbolName);
433       else
434         addRelocationForSection(RE, Value.SectionID);
435       resolveRelocation(Section, Offset,
436                         (uint64_t)Section.Address + Section.StubOffset,
437                         RelType, 0, IsPCRel, Size);
438       Section.StubOffset += getMaxStubSize();
439     }
440   } else {
441     RelocationEntry RE(SectionID, Offset, RelType, Value.Addend,
442                        IsPCRel, Size);
443     if (Value.SymbolName)
444       addRelocationForSymbol(RE, Value.SymbolName);
445     else
446       addRelocationForSection(RE, Value.SectionID);
447   }
448   return ++RelI;
449 }
450
451
452 bool RuntimeDyldMachO::isCompatibleFormat(
453         const ObjectBuffer *InputBuffer) const {
454   if (InputBuffer->getBufferSize() < 4)
455     return false;
456   StringRef Magic(InputBuffer->getBufferStart(), 4);
457   if (Magic == "\xFE\xED\xFA\xCE") return true;
458   if (Magic == "\xCE\xFA\xED\xFE") return true;
459   if (Magic == "\xFE\xED\xFA\xCF") return true;
460   if (Magic == "\xCF\xFA\xED\xFE") return true;
461   return false;
462 }
463
464 bool RuntimeDyldMachO::isCompatibleFile(
465         const object::ObjectFile *Obj) const {
466   return Obj->isMachO();
467 }
468
469 } // end namespace llvm