[RuntimeDyld] Handle endiannes differences between the host and target while
[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 #include "ObjectImageCommon.h"
18 #include "JITRegistrar.h"
19 using namespace llvm;
20 using namespace llvm::object;
21
22 #define DEBUG_TYPE "dyld"
23
24 namespace llvm {
25
26 class MachOObjectImage : public ObjectImageCommon {
27 private:
28   typedef SmallVector<uint64_t, 1> SectionAddrList;
29   SectionAddrList OldSectionAddrList;
30
31 protected:
32   bool is64;
33   bool Registered;
34
35 private:
36   void initOldAddress() {
37     MachOObjectFile *objf = static_cast<MachOObjectFile *>(ObjFile.get());
38     // Unfortunately we need to do this, since there's information encoded
39     // in the original addr of the section that we could not otherwise
40     // recover. The reason for this is that symbols do not actually store
41     // their file offset, but only their vmaddr. This means that in order
42     // to locate the symbol correctly in the object file, we need to know
43     // where the original start of the section was (including any padding,
44     // etc).
45     for (section_iterator i = objf->section_begin(), e = objf->section_end();
46          i != e; ++i) {
47       uint64_t Addr;
48       i->getAddress(Addr);
49       OldSectionAddrList[i->getRawDataRefImpl().d.a] = Addr;
50     }
51   }
52
53 public:
54   MachOObjectImage(ObjectBuffer *Input, bool is64)
55       : ObjectImageCommon(Input),
56         OldSectionAddrList(ObjFile->section_end()->getRawDataRefImpl().d.a, 0),
57         is64(is64), Registered(false) {
58     initOldAddress();
59   }
60
61   MachOObjectImage(std::unique_ptr<object::ObjectFile> Input, bool is64)
62       : ObjectImageCommon(std::move(Input)),
63         OldSectionAddrList(ObjFile->section_end()->getRawDataRefImpl().d.a, 0),
64         is64(is64), Registered(false) {
65     initOldAddress();
66   }
67
68   virtual ~MachOObjectImage() {
69     if (Registered)
70       deregisterWithDebugger();
71   }
72
73   // Subclasses can override these methods to update the image with loaded
74   // addresses for sections and common symbols
75   virtual void updateSectionAddress(const SectionRef &Sec, uint64_t Addr) {
76     MachOObjectFile *objf = static_cast<MachOObjectFile *>(ObjFile.get());
77     char *data =
78         const_cast<char *>(objf->getSectionPointer(Sec.getRawDataRefImpl()));
79
80     uint64_t oldAddr = OldSectionAddrList[Sec.getRawDataRefImpl().d.a];
81
82     if (is64) {
83       ((MachO::section_64 *)data)->addr = Addr;
84     } else {
85       ((MachO::section *)data)->addr = Addr;
86     }
87
88     for (symbol_iterator i = objf->symbol_begin(), e = objf->symbol_end();
89          i != e; ++i) {
90       section_iterator symSec(objf->section_end());
91       (*i).getSection(symSec);
92       if (*symSec == Sec) {
93         uint64_t symAddr;
94         (*i).getAddress(symAddr);
95         updateSymbolAddress(*i, symAddr + Addr - oldAddr);
96       }
97     }
98   }
99
100   uint64_t getOldSectionAddr(const SectionRef &Sec) const {
101     return OldSectionAddrList[Sec.getRawDataRefImpl().d.a];
102   }
103
104   virtual void updateSymbolAddress(const SymbolRef &Sym, uint64_t Addr) {
105     char *data = const_cast<char *>(
106         reinterpret_cast<const char *>(Sym.getRawDataRefImpl().p));
107     if (is64)
108       ((MachO::nlist_64 *)data)->n_value = Addr;
109     else
110       ((MachO::nlist *)data)->n_value = Addr;
111   }
112
113   virtual void registerWithDebugger() {
114     JITRegistrar::getGDBRegistrar().registerObject(*Buffer);
115     Registered = true;
116   }
117
118   virtual void deregisterWithDebugger() {
119     JITRegistrar::getGDBRegistrar().deregisterObject(*Buffer);
120   }
121 };
122
123 static uint32_t readMachOMagic(const char *InputBuffer, unsigned BufferSize) {
124   if (BufferSize < 4)
125     return 0;
126   StringRef Magic(InputBuffer, 4);
127   if (Magic == "\xFE\xED\xFA\xCE" || Magic == "\xCE\xFA\xED\xFE")
128     return 0xFEEDFACE;
129   else if (Magic == "\xFE\xED\xFA\xCF" || Magic == "\xCF\xFA\xED\xFE")
130     return 0xFEEDFACF;
131   // else
132   return 0;
133 }
134
135 ObjectImage *RuntimeDyldMachO::createObjectImage(ObjectBuffer *Buffer) {
136   uint32_t magic = readMachOMagic(Buffer->getBufferStart(),
137                                   Buffer->getBufferSize());
138   bool is64 = (magic == MachO::MH_MAGIC_64);
139   assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
140          "Unrecognized Macho Magic");
141   return new MachOObjectImage(Buffer, is64);
142 }
143
144 ObjectImage *RuntimeDyldMachO::createObjectImageFromFile(
145     std::unique_ptr<object::ObjectFile> ObjFile) {
146   if (!ObjFile)
147     return nullptr;
148
149   MemoryBuffer *Buffer =
150       MemoryBuffer::getMemBuffer(ObjFile->getData(), "", false);
151
152   uint32_t magic = readMachOMagic(Buffer->getBufferStart(),
153                                   Buffer->getBufferSize());
154   bool is64 = (magic == MachO::MH_MAGIC_64);
155   assert((magic == MachO::MH_MAGIC_64 || magic == MachO::MH_MAGIC) &&
156          "Unrecognized Macho Magic");
157   return new MachOObjectImage(std::move(ObjFile), is64);
158 }
159
160 static unsigned char *processFDE(unsigned char *P, intptr_t DeltaForText,
161                                  intptr_t DeltaForEH) {
162   DEBUG(dbgs() << "Processing FDE: Delta for text: " << DeltaForText
163                << ", Delta for EH: " << DeltaForEH << "\n");
164   uint32_t Length = *((uint32_t *)P);
165   P += 4;
166   unsigned char *Ret = P + Length;
167   uint32_t Offset = *((uint32_t *)P);
168   if (Offset == 0) // is a CIE
169     return Ret;
170
171   P += 4;
172   intptr_t FDELocation = *((intptr_t *)P);
173   intptr_t NewLocation = FDELocation - DeltaForText;
174   *((intptr_t *)P) = NewLocation;
175   P += sizeof(intptr_t);
176
177   // Skip the FDE address range
178   P += sizeof(intptr_t);
179
180   uint8_t Augmentationsize = *P;
181   P += 1;
182   if (Augmentationsize != 0) {
183     intptr_t LSDA = *((intptr_t *)P);
184     intptr_t NewLSDA = LSDA - DeltaForEH;
185     *((intptr_t *)P) = NewLSDA;
186   }
187
188   return Ret;
189 }
190
191 static intptr_t computeDelta(SectionEntry *A, SectionEntry *B) {
192   intptr_t ObjDistance = A->ObjAddress - B->ObjAddress;
193   intptr_t MemDistance = A->LoadAddress - B->LoadAddress;
194   return ObjDistance - MemDistance;
195 }
196
197 void RuntimeDyldMachO::registerEHFrames() {
198
199   if (!MemMgr)
200     return;
201   for (int i = 0, e = UnregisteredEHFrameSections.size(); i != e; ++i) {
202     EHFrameRelatedSections &SectionInfo = UnregisteredEHFrameSections[i];
203     if (SectionInfo.EHFrameSID == RTDYLD_INVALID_SECTION_ID ||
204         SectionInfo.TextSID == RTDYLD_INVALID_SECTION_ID)
205       continue;
206     SectionEntry *Text = &Sections[SectionInfo.TextSID];
207     SectionEntry *EHFrame = &Sections[SectionInfo.EHFrameSID];
208     SectionEntry *ExceptTab = nullptr;
209     if (SectionInfo.ExceptTabSID != RTDYLD_INVALID_SECTION_ID)
210       ExceptTab = &Sections[SectionInfo.ExceptTabSID];
211
212     intptr_t DeltaForText = computeDelta(Text, EHFrame);
213     intptr_t DeltaForEH = 0;
214     if (ExceptTab)
215       DeltaForEH = computeDelta(ExceptTab, EHFrame);
216
217     unsigned char *P = EHFrame->Address;
218     unsigned char *End = P + EHFrame->Size;
219     do {
220       P = processFDE(P, DeltaForText, DeltaForEH);
221     } while (P != End);
222
223     MemMgr->registerEHFrames(EHFrame->Address, EHFrame->LoadAddress,
224                              EHFrame->Size);
225   }
226   UnregisteredEHFrameSections.clear();
227 }
228
229 void RuntimeDyldMachO::finalizeLoad(ObjectImage &ObjImg,
230                                     ObjSectionToIDMap &SectionMap) {
231   unsigned EHFrameSID = RTDYLD_INVALID_SECTION_ID;
232   unsigned TextSID = RTDYLD_INVALID_SECTION_ID;
233   unsigned ExceptTabSID = RTDYLD_INVALID_SECTION_ID;
234   ObjSectionToIDMap::iterator i, e;
235   for (i = SectionMap.begin(), e = SectionMap.end(); i != e; ++i) {
236     const SectionRef &Section = i->first;
237     StringRef Name;
238     Section.getName(Name);
239     if (Name == "__eh_frame")
240       EHFrameSID = i->second;
241     else if (Name == "__text")
242       TextSID = i->second;
243     else if (Name == "__gcc_except_tab")
244       ExceptTabSID = i->second;
245     else if (Name == "__jump_table")
246       populateJumpTable(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
247                         Section, i->second);
248     else if (Name == "__pointers")
249       populatePointersSection(cast<MachOObjectFile>(*ObjImg.getObjectFile()),
250                               Section, i->second);
251   }
252   UnregisteredEHFrameSections.push_back(
253       EHFrameRelatedSections(EHFrameSID, TextSID, ExceptTabSID));
254 }
255
256 // The target location for the relocation is described by RE.SectionID and
257 // RE.Offset.  RE.SectionID can be used to find the SectionEntry.  Each
258 // SectionEntry has three members describing its location.
259 // SectionEntry::Address is the address at which the section has been loaded
260 // into memory in the current (host) process.  SectionEntry::LoadAddress is the
261 // address that the section will have in the target process.
262 // SectionEntry::ObjAddress is the address of the bits for this section in the
263 // original emitted object image (also in the current address space).
264 //
265 // Relocations will be applied as if the section were loaded at
266 // SectionEntry::LoadAddress, but they will be applied at an address based
267 // on SectionEntry::Address.  SectionEntry::ObjAddress will be used to refer to
268 // Target memory contents if they are required for value calculations.
269 //
270 // The Value parameter here is the load address of the symbol for the
271 // relocation to be applied.  For relocations which refer to symbols in the
272 // current object Value will be the LoadAddress of the section in which
273 // the symbol resides (RE.Addend provides additional information about the
274 // symbol location).  For external symbols, Value will be the address of the
275 // symbol in the target address space.
276 void RuntimeDyldMachO::resolveRelocation(const RelocationEntry &RE,
277                                          uint64_t Value) {
278   DEBUG (
279     const SectionEntry &Section = Sections[RE.SectionID];
280     uint8_t* LocalAddress = Section.Address + RE.Offset;
281     uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
282
283     dbgs() << "resolveRelocation Section: " << RE.SectionID
284            << " LocalAddress: " << format("%p", LocalAddress)
285            << " FinalAddress: " << format("%p", FinalAddress)
286            << " Value: " << format("%p", Value)
287            << " Addend: " << RE.Addend
288            << " isPCRel: " << RE.IsPCRel
289            << " MachoType: " << RE.RelType
290            << " Size: " << (1 << RE.Size) << "\n";
291   );
292
293   // This just dispatches to the proper target specific routine.
294   switch (Arch) {
295   default:
296     llvm_unreachable("Unsupported CPU type!");
297   case Triple::x86_64:
298     resolveX86_64Relocation(RE, Value);
299     break;
300   case Triple::x86:
301     resolveI386Relocation(RE, Value);
302     break;
303   case Triple::arm: // Fall through.
304   case Triple::thumb:
305     resolveARMRelocation(RE, Value);
306     break;
307   case Triple::aarch64:
308   case Triple::arm64:
309     resolveAArch64Relocation(RE, Value);
310     break;
311   }
312 }
313
314 bool RuntimeDyldMachO::resolveI386Relocation(const RelocationEntry &RE,
315                                              uint64_t Value) {
316   const SectionEntry &Section = Sections[RE.SectionID];
317   uint8_t* LocalAddress = Section.Address + RE.Offset;
318
319   if (RE.IsPCRel) {
320     uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
321     Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
322   }
323
324   switch (RE.RelType) {
325     default:
326       llvm_unreachable("Invalid relocation type!");
327     case MachO::GENERIC_RELOC_VANILLA:
328       return applyRelocationValue(LocalAddress, Value + RE.Addend,
329                                   1 << RE.Size);
330     case MachO::GENERIC_RELOC_SECTDIFF:
331     case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
332       uint64_t SectionABase = Sections[RE.Sections.SectionA].LoadAddress;
333       uint64_t SectionBBase = Sections[RE.Sections.SectionB].LoadAddress;
334       assert((Value == SectionABase || Value == SectionBBase) &&
335              "Unexpected SECTDIFF relocation value.");
336       Value = SectionABase - SectionBBase + RE.Addend;
337       return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
338     }
339     case MachO::GENERIC_RELOC_PB_LA_PTR:
340       return Error("Relocation type not implemented yet!");
341   }
342 }
343
344 bool RuntimeDyldMachO::resolveX86_64Relocation(const RelocationEntry &RE,
345                                                uint64_t Value) {
346   const SectionEntry &Section = Sections[RE.SectionID];
347   uint8_t* LocalAddress = Section.Address + RE.Offset;
348
349   // If the relocation is PC-relative, the value to be encoded is the
350   // pointer difference.
351   if (RE.IsPCRel) {
352     // FIXME: It seems this value needs to be adjusted by 4 for an effective PC
353     // address. Is that expected? Only for branches, perhaps?
354     uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
355     Value -= FinalAddress + 4; // see MachOX86_64::resolveRelocation.
356   }
357
358   switch (RE.RelType) {
359   default:
360     llvm_unreachable("Invalid relocation type!");
361   case MachO::X86_64_RELOC_SIGNED_1:
362   case MachO::X86_64_RELOC_SIGNED_2:
363   case MachO::X86_64_RELOC_SIGNED_4:
364   case MachO::X86_64_RELOC_SIGNED:
365   case MachO::X86_64_RELOC_UNSIGNED:
366   case MachO::X86_64_RELOC_BRANCH:
367     return applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
368   case MachO::X86_64_RELOC_GOT_LOAD:
369   case MachO::X86_64_RELOC_GOT:
370   case MachO::X86_64_RELOC_SUBTRACTOR:
371   case MachO::X86_64_RELOC_TLV:
372     return Error("Relocation type not implemented yet!");
373   }
374 }
375
376 bool RuntimeDyldMachO::resolveARMRelocation(const RelocationEntry &RE,
377                                             uint64_t Value) {
378   const SectionEntry &Section = Sections[RE.SectionID];
379   uint8_t* LocalAddress = Section.Address + RE.Offset;
380
381   // If the relocation is PC-relative, the value to be encoded is the
382   // pointer difference.
383   if (RE.IsPCRel) {
384     uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
385     Value -= FinalAddress;
386     // ARM PCRel relocations have an effective-PC offset of two instructions
387     // (four bytes in Thumb mode, 8 bytes in ARM mode).
388     // FIXME: For now, assume ARM mode.
389     Value -= 8;
390   }
391
392   switch (RE.RelType) {
393   default:
394     llvm_unreachable("Invalid relocation type!");
395   case MachO::ARM_RELOC_VANILLA:
396     return applyRelocationValue(LocalAddress, Value, 1 << RE.Size);
397   case MachO::ARM_RELOC_BR24: {
398     // Mask the value into the target address. We know instructions are
399     // 32-bit aligned, so we can do it all at once.
400     uint32_t *p = (uint32_t *)LocalAddress;
401     // The low two bits of the value are not encoded.
402     Value >>= 2;
403     // Mask the value to 24 bits.
404     uint64_t FinalValue = Value & 0xffffff;
405     // Check for overflow.
406     if (Value != FinalValue)
407       return Error("ARM BR24 relocation out of range.");
408     // FIXME: If the destination is a Thumb function (and the instruction
409     // is a non-predicated BL instruction), we need to change it to a BLX
410     // instruction instead.
411
412     // Insert the value into the instruction.
413     *p = (*p & ~0xffffff) | FinalValue;
414     break;
415   }
416   case MachO::ARM_THUMB_RELOC_BR22:
417   case MachO::ARM_THUMB_32BIT_BRANCH:
418   case MachO::ARM_RELOC_HALF:
419   case MachO::ARM_RELOC_HALF_SECTDIFF:
420   case MachO::ARM_RELOC_PAIR:
421   case MachO::ARM_RELOC_SECTDIFF:
422   case MachO::ARM_RELOC_LOCAL_SECTDIFF:
423   case MachO::ARM_RELOC_PB_LA_PTR:
424     return Error("Relocation type not implemented yet!");
425   }
426   return false;
427 }
428
429 bool RuntimeDyldMachO::resolveAArch64Relocation(const RelocationEntry &RE,
430                                                 uint64_t Value) {
431   const SectionEntry &Section = Sections[RE.SectionID];
432   uint8_t* LocalAddress = Section.Address + RE.Offset;
433
434   switch (RE.RelType) {
435   default:
436     llvm_unreachable("Invalid relocation type!");
437   case MachO::ARM64_RELOC_UNSIGNED: {
438     assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_UNSIGNED not supported");
439     // Mask in the target value a byte at a time (we don't have an alignment
440     // guarantee for the target address, so this is safest).
441     if (RE.Size < 2)
442       llvm_unreachable("Invalid size for ARM64_RELOC_UNSIGNED");
443
444     applyRelocationValue(LocalAddress, Value + RE.Addend, 1 << RE.Size);
445     break;
446   }
447   case MachO::ARM64_RELOC_BRANCH26: {
448     assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported");
449     // Mask the value into the target address. We know instructions are
450     // 32-bit aligned, so we can do it all at once.
451     uint32_t *p = (uint32_t*)LocalAddress;
452     // Check if the addend is encoded in the instruction.
453     uint32_t EncodedAddend = *p & 0x03FFFFFF;
454     if (EncodedAddend != 0 ) {
455       if (RE.Addend == 0)
456         llvm_unreachable("branch26 instruction has embedded addend.");
457       else
458         llvm_unreachable("branch26 instruction has embedded addend and" \
459                          "ARM64_RELOC_ADDEND.");
460     }
461     // Check if branch is in range.
462     uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
463     uint64_t PCRelVal = Value - FinalAddress + RE.Addend;
464     assert(isInt<26>(PCRelVal) && "Branch target out of range!");
465     // Insert the value into the instruction.
466     *p = (*p & 0xFC000000) | ((uint32_t)(PCRelVal >> 2) & 0x03FFFFFF);
467     break;
468   }
469   case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
470   case MachO::ARM64_RELOC_PAGE21: {
471     assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported");
472     // Mask the value into the target address. We know instructions are
473     // 32-bit aligned, so we can do it all at once.
474     uint32_t *p = (uint32_t*)LocalAddress;
475     // Check if the addend is encoded in the instruction.
476     uint32_t EncodedAddend = ((*p & 0x60000000) >> 29) |
477                              ((*p & 0x01FFFFE0) >> 3);
478     if (EncodedAddend != 0) {
479       if (RE.Addend == 0)
480         llvm_unreachable("adrp instruction has embedded addend.");
481       else
482         llvm_unreachable("adrp instruction has embedded addend and" \
483                          "ARM64_RELOC_ADDEND.");
484     }
485     // Adjust for PC-relative relocation and offset.
486     uint64_t FinalAddress = Section.LoadAddress + RE.Offset;
487     uint64_t PCRelVal = ((Value + RE.Addend) & (-4096)) -
488                          (FinalAddress & (-4096));
489     // Check that the value fits into 21 bits (+ 12 lower bits).
490     assert(isInt<33>(PCRelVal) && "Invalid page reloc value!");
491     // Insert the value into the instruction.
492     uint32_t ImmLoValue = (uint32_t)(PCRelVal << 17) & 0x60000000;
493     uint32_t ImmHiValue = (uint32_t)(PCRelVal >>  9) & 0x00FFFFE0;
494     *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue;
495     break;
496   }
497   case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
498   case MachO::ARM64_RELOC_PAGEOFF12: {
499     assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_PAGEOFF21 not supported");
500     // Mask the value into the target address. We know instructions are
501     // 32-bit aligned, so we can do it all at once.
502     uint32_t *p = (uint32_t*)LocalAddress;
503     // Check if the addend is encoded in the instruction.
504     uint32_t EncodedAddend = *p & 0x003FFC00;
505     if (EncodedAddend != 0) {
506       if (RE.Addend == 0)
507         llvm_unreachable("adrp instruction has embedded addend.");
508       else
509         llvm_unreachable("adrp instruction has embedded addend and" \
510                          "ARM64_RELOC_ADDEND.");
511     }
512     // Add the offset from the symbol.
513     Value += RE.Addend;
514     // Mask out the page address and only use the lower 12 bits.
515     Value &= 0xFFF;
516     // Check which instruction we are updating to obtain the implicit shift
517     // factor from LDR/STR instructions.
518     if (*p & 0x08000000) {
519       uint32_t ImplicitShift = ((*p >> 30) & 0x3);
520       switch (ImplicitShift) {
521       case 0:
522         // Check if this a vector op.
523         if ((*p & 0x04800000) == 0x04800000) {
524           ImplicitShift = 4;
525           assert(((Value & 0xF) == 0) &&
526                  "128-bit LDR/STR not 16-byte aligned.");
527         }
528         break;
529       case 1:
530         assert(((Value & 0x1) == 0) && "16-bit LDR/STR not 2-byte aligned.");
531       case 2:
532         assert(((Value & 0x3) == 0) && "32-bit LDR/STR not 4-byte aligned.");
533       case 3:
534         assert(((Value & 0x7) == 0) && "64-bit LDR/STR not 8-byte aligned.");
535       }
536       // Compensate for implicit shift.
537       Value >>= ImplicitShift;
538     }
539     // Insert the value into the instruction.
540     *p = (*p & 0xFFC003FF) | ((uint32_t)(Value << 10) & 0x003FFC00);
541     break;
542   }
543   case MachO::ARM64_RELOC_SUBTRACTOR:
544   case MachO::ARM64_RELOC_POINTER_TO_GOT:
545   case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
546   case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
547     llvm_unreachable("Relocation type not implemented yet!");
548     return Error("Relocation type not implemented yet!");
549   case MachO::ARM64_RELOC_ADDEND:
550     llvm_unreachable("ARM64_RELOC_ADDEND should have been handeled by " \
551                      "processRelocationRef!");
552   }
553   return false;
554 }
555
556 void RuntimeDyldMachO::populateJumpTable(MachOObjectFile &Obj,
557                                          const SectionRef &JTSection,
558                                          unsigned JTSectionID) {
559   assert(!Obj.is64Bit() &&
560          "__jump_table section not supported in 64-bit MachO.");
561
562   MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
563   MachO::section Sec32 = Obj.getSection(JTSection.getRawDataRefImpl());
564   uint32_t JTSectionSize = Sec32.size;
565   unsigned FirstIndirectSymbol = Sec32.reserved1;
566   unsigned JTEntrySize = Sec32.reserved2;
567   unsigned NumJTEntries = JTSectionSize / JTEntrySize;
568   uint8_t* JTSectionAddr = getSectionAddress(JTSectionID);
569   unsigned JTEntryOffset = 0;
570
571   assert((JTSectionSize % JTEntrySize) == 0 &&
572          "Jump-table section does not contain a whole number of stubs?");
573
574   for (unsigned i = 0; i < NumJTEntries; ++i) {
575     unsigned SymbolIndex =
576       Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
577     symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
578     StringRef IndirectSymbolName;
579     SI->getName(IndirectSymbolName);
580     uint8_t* JTEntryAddr = JTSectionAddr + JTEntryOffset;
581     createStubFunction(JTEntryAddr);
582     RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
583                        MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
584     addRelocationForSymbol(RE, IndirectSymbolName);
585     JTEntryOffset += JTEntrySize;
586   }
587 }
588
589 void RuntimeDyldMachO::populatePointersSection(MachOObjectFile &Obj,
590                                                const SectionRef &PTSection,
591                                                unsigned PTSectionID) {
592   assert(!Obj.is64Bit() &&
593          "__pointers section not supported in 64-bit MachO.");
594
595   MachO::dysymtab_command DySymTabCmd = Obj.getDysymtabLoadCommand();
596   MachO::section Sec32 = Obj.getSection(PTSection.getRawDataRefImpl());
597   uint32_t PTSectionSize = Sec32.size;
598   unsigned FirstIndirectSymbol = Sec32.reserved1;
599   const unsigned PTEntrySize = 4;
600   unsigned NumPTEntries = PTSectionSize / PTEntrySize;
601   unsigned PTEntryOffset = 0;
602
603   assert((PTSectionSize % PTEntrySize) == 0 &&
604          "Pointers section does not contain a whole number of stubs?");
605
606   DEBUG(dbgs() << "Populating __pointers, Section ID " << PTSectionID
607                << ", " << NumPTEntries << " entries, "
608                << PTEntrySize << " bytes each:\n");
609
610   for (unsigned i = 0; i < NumPTEntries; ++i) {
611     unsigned SymbolIndex =
612       Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
613     symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
614     StringRef IndirectSymbolName;
615     SI->getName(IndirectSymbolName);
616     DEBUG(dbgs() << "  " << IndirectSymbolName << ": index " << SymbolIndex
617           << ", PT offset: " << PTEntryOffset << "\n");
618     RelocationEntry RE(PTSectionID, PTEntryOffset,
619                        MachO::GENERIC_RELOC_VANILLA, 0, false, 2);
620     addRelocationForSymbol(RE, IndirectSymbolName);
621     PTEntryOffset += PTEntrySize;
622   }
623 }
624
625
626 section_iterator getSectionByAddress(const MachOObjectFile &Obj,
627                                      uint64_t Addr) {
628   section_iterator SI = Obj.section_begin();
629   section_iterator SE = Obj.section_end();
630
631   for (; SI != SE; ++SI) {
632     uint64_t SAddr, SSize;
633     SI->getAddress(SAddr);
634     SI->getSize(SSize);
635     if ((Addr >= SAddr) && (Addr < SAddr + SSize))
636       return SI;
637   }
638
639   return SE;
640 }
641
642 relocation_iterator RuntimeDyldMachO::processSECTDIFFRelocation(
643                                             unsigned SectionID,
644                                             relocation_iterator RelI,
645                                             ObjectImage &Obj,
646                                             ObjSectionToIDMap &ObjSectionToID) {
647   const MachOObjectFile *MachO =
648     static_cast<const MachOObjectFile*>(Obj.getObjectFile());
649   MachO::any_relocation_info RE =
650     MachO->getRelocation(RelI->getRawDataRefImpl());
651
652   SectionEntry &Section = Sections[SectionID];
653   uint32_t RelocType = MachO->getAnyRelocationType(RE);
654   bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
655   unsigned Size = MachO->getAnyRelocationLength(RE);
656   uint64_t Offset;
657   RelI->getOffset(Offset);
658   uint8_t *LocalAddress = Section.Address + Offset;
659   unsigned NumBytes = 1 << Size;
660   int64_t Addend = 0;
661   memcpy(&Addend, LocalAddress, NumBytes);
662
663   ++RelI;
664   MachO::any_relocation_info RE2 =
665     MachO->getRelocation(RelI->getRawDataRefImpl());
666
667   uint32_t AddrA = MachO->getScatteredRelocationValue(RE);
668   section_iterator SAI = getSectionByAddress(*MachO, AddrA);
669   assert(SAI != MachO->section_end() && "Can't find section for address A");
670   uint64_t SectionABase;
671   SAI->getAddress(SectionABase);
672   uint64_t SectionAOffset = AddrA - SectionABase;
673   SectionRef SectionA = *SAI;
674   bool IsCode;
675   SectionA.isText(IsCode);
676   uint32_t SectionAID = findOrEmitSection(Obj, SectionA, IsCode,
677                                           ObjSectionToID);
678
679   uint32_t AddrB = MachO->getScatteredRelocationValue(RE2);
680   section_iterator SBI = getSectionByAddress(*MachO, AddrB);
681   assert(SBI != MachO->section_end() && "Can't find section for address B");
682   uint64_t SectionBBase;
683   SBI->getAddress(SectionBBase);
684   uint64_t SectionBOffset = AddrB - SectionBBase;
685   SectionRef SectionB = *SBI;
686   uint32_t SectionBID = findOrEmitSection(Obj, SectionB, IsCode,
687                                           ObjSectionToID);
688
689   if (Addend != AddrA - AddrB)
690     Error("Unexpected SECTDIFF relocation addend.");
691
692   DEBUG(dbgs() << "Found SECTDIFF: AddrA: " << AddrA << ", AddrB: " << AddrB
693                << ", Addend: " << Addend << ", SectionA ID: "
694                << SectionAID << ", SectionAOffset: " << SectionAOffset
695                << ", SectionB ID: " << SectionBID << ", SectionBOffset: "
696                << SectionBOffset << "\n");
697   RelocationEntry R(SectionID, Offset, RelocType, 0,
698                     SectionAID, SectionAOffset, SectionBID, SectionBOffset,
699                     IsPCRel, Size);
700
701   addRelocationForSection(R, SectionAID);
702   addRelocationForSection(R, SectionBID);
703
704   return ++RelI;
705 }
706
707 relocation_iterator RuntimeDyldMachO::processI386ScatteredVANILLA(
708                                             unsigned SectionID,
709                                             relocation_iterator RelI,
710                                             ObjectImage &Obj,
711                                             ObjSectionToIDMap &ObjSectionToID) {
712   const MachOObjectFile *MachO =
713     static_cast<const MachOObjectFile*>(Obj.getObjectFile());
714   MachO::any_relocation_info RE =
715     MachO->getRelocation(RelI->getRawDataRefImpl());
716
717   SectionEntry &Section = Sections[SectionID];
718   uint32_t RelocType = MachO->getAnyRelocationType(RE);
719   bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
720   unsigned Size = MachO->getAnyRelocationLength(RE);
721   uint64_t Offset;
722   RelI->getOffset(Offset);
723   uint8_t *LocalAddress = Section.Address + Offset;
724   unsigned NumBytes = 1 << Size;
725   int64_t Addend = 0;
726   memcpy(&Addend, LocalAddress, NumBytes);
727
728   unsigned SymbolBaseAddr = MachO->getScatteredRelocationValue(RE);
729   section_iterator TargetSI = getSectionByAddress(*MachO, SymbolBaseAddr);
730   assert(TargetSI != MachO->section_end() && "Can't find section for symbol");
731   uint64_t SectionBaseAddr;
732   TargetSI->getAddress(SectionBaseAddr);
733   SectionRef TargetSection = *TargetSI;
734   bool IsCode;
735   TargetSection.isText(IsCode);
736   uint32_t TargetSectionID = findOrEmitSection(Obj, TargetSection, IsCode,
737                                                ObjSectionToID);
738
739   Addend -= SectionBaseAddr;
740   RelocationEntry R(SectionID, Offset, RelocType, Addend,
741                     IsPCRel, Size);
742
743   addRelocationForSection(R, TargetSectionID);
744
745   return ++RelI;
746 }
747
748 relocation_iterator RuntimeDyldMachO::processRelocationRef(
749     unsigned SectionID, relocation_iterator RelI, ObjectImage &Obj,
750     ObjSectionToIDMap &ObjSectionToID, const SymbolTableMap &Symbols,
751     StubMap &Stubs) {
752   const ObjectFile *OF = Obj.getObjectFile();
753   const MachOObjectImage &MachOObj = *static_cast<MachOObjectImage *>(&Obj);
754   const MachOObjectFile *MachO = static_cast<const MachOObjectFile *>(OF);
755   MachO::any_relocation_info RE =
756       MachO->getRelocation(RelI->getRawDataRefImpl());
757   int64_t RelocAddendValue = 0;
758   bool HasRelocAddendValue = false;
759
760   uint32_t RelType = MachO->getAnyRelocationType(RE);
761   if (Arch == Triple::arm64) {
762     // ARM64_RELOC_ADDEND provides the offset (addend) that will be used by the
763     // next relocation entry. Save the value and advance to the next relocation
764     // entry.
765     if (RelType == MachO::ARM64_RELOC_ADDEND) {
766       assert(!MachO->getPlainRelocationExternal(RE));
767       assert(!MachO->getAnyRelocationPCRel(RE));
768       assert(MachO->getAnyRelocationLength(RE) == 2);
769       uint64_t RawAddend = MachO->getPlainRelocationSymbolNum(RE);
770       // Sign-extend the 24-bit to 64-bit.
771       RelocAddendValue = RawAddend << 40;
772       RelocAddendValue >>= 40;
773       HasRelocAddendValue = true;
774
775       // Get the next entry.
776       RE = MachO->getRelocation((++RelI)->getRawDataRefImpl());
777       RelType = MachO->getAnyRelocationType(RE);
778       assert(RelType == MachO::ARM64_RELOC_BRANCH26 ||
779              RelType == MachO::ARM64_RELOC_PAGE21 ||
780              RelType == MachO::ARM64_RELOC_PAGEOFF12);
781
782     } else if (RelType == MachO::ARM64_RELOC_BRANCH26 ||
783                RelType == MachO::ARM64_RELOC_PAGE21 ||
784                RelType == MachO::ARM64_RELOC_PAGEOFF12 ||
785                RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 ||
786                RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12) {
787       RelocAddendValue = 0;
788       HasRelocAddendValue = true;
789     }
790   }
791
792   // FIXME: Properly handle scattered relocations.
793   //        Special case the couple of scattered relocations that we know how
794   //        to handle: SECTDIFF relocations, and scattered VANILLA relocations
795   //        on I386.
796   //        For all other scattered relocations, just bail out and hope for the
797   //        best, since the offsets computed by scattered relocations have often
798   //        been optimisticaly filled in by the compiler. This will fail
799   //        horribly where the relocations *do* need to be applied, but that was
800   //        already the case.
801   if (MachO->isRelocationScattered(RE)) {
802     if (RelType == MachO::GENERIC_RELOC_SECTDIFF ||
803         RelType == MachO::GENERIC_RELOC_LOCAL_SECTDIFF)
804       return processSECTDIFFRelocation(SectionID, RelI, Obj, ObjSectionToID);
805     else if (Arch == Triple::x86 && RelType == MachO::GENERIC_RELOC_VANILLA)
806       return processI386ScatteredVANILLA(SectionID, RelI, Obj, ObjSectionToID);
807     else
808       return ++RelI;
809   }
810
811   RelocationValueRef Value;
812   SectionEntry &Section = Sections[SectionID];
813
814   bool IsExtern = MachO->getPlainRelocationExternal(RE);
815   bool IsPCRel = MachO->getAnyRelocationPCRel(RE);
816   unsigned Size = MachO->getAnyRelocationLength(RE);
817   uint64_t Offset;
818   RelI->getOffset(Offset);
819   uint8_t *LocalAddress = Section.Address + Offset;
820   unsigned NumBytes = 1 << Size;
821   int64_t Addend = 0;
822   if (HasRelocAddendValue)
823     Addend = RelocAddendValue;
824   else
825     memcpy(&Addend, LocalAddress, NumBytes);
826
827   if (IsExtern) {
828     // Obtain the symbol name which is referenced in the relocation
829     symbol_iterator Symbol = RelI->getSymbol();
830     StringRef TargetName;
831     Symbol->getName(TargetName);
832     // First search for the symbol in the local symbol table
833     SymbolTableMap::const_iterator lsi = Symbols.find(TargetName.data());
834     if (lsi != Symbols.end()) {
835       Value.SectionID = lsi->second.first;
836       Value.Addend = lsi->second.second + Addend;
837     } else {
838       // Search for the symbol in the global symbol table
839       SymbolTableMap::const_iterator gsi =
840           GlobalSymbolTable.find(TargetName.data());
841       if (gsi != GlobalSymbolTable.end()) {
842         Value.SectionID = gsi->second.first;
843         Value.Addend = gsi->second.second + Addend;
844       } else {
845         Value.SymbolName = TargetName.data();
846         Value.Addend = Addend;
847       }
848     }
849
850     // Addends for external, PC-rel relocations on i386 point back to the zero
851     // offset. Calculate the final offset from the relocation target instead.
852     // This allows us to use the same logic for both external and internal
853     // relocations in resolveI386RelocationRef.
854     if (Arch == Triple::x86 && IsPCRel) {
855       uint64_t RelocAddr = 0;
856       RelI->getAddress(RelocAddr);
857       Value.Addend += RelocAddr + 4;
858     }
859
860   } else {
861     SectionRef Sec = MachO->getRelocationSection(RE);
862     bool IsCode = false;
863     Sec.isText(IsCode);
864     Value.SectionID = findOrEmitSection(Obj, Sec, IsCode, ObjSectionToID);
865     uint64_t Addr = MachOObj.getOldSectionAddr(Sec);
866     DEBUG(dbgs() << "\nAddr: " << Addr << "\nAddend: " << Addend);
867     Value.Addend = Addend - Addr;
868     if (IsPCRel)
869       Value.Addend += Offset + NumBytes;
870   }
871
872   if (Arch == Triple::x86_64 && (RelType == MachO::X86_64_RELOC_GOT ||
873                                  RelType == MachO::X86_64_RELOC_GOT_LOAD)) {
874     assert(IsPCRel);
875     assert(Size == 2);
876
877     // FIXME: Teach the generic code above not to prematurely conflate
878     //        relocation addends and symbol offsets.
879     Value.Addend -= Addend;
880     StubMap::const_iterator i = Stubs.find(Value);
881     uint8_t *Addr;
882     if (i != Stubs.end()) {
883       Addr = Section.Address + i->second;
884     } else {
885       Stubs[Value] = Section.StubOffset;
886       uint8_t *GOTEntry = Section.Address + Section.StubOffset;
887       RelocationEntry GOTRE(SectionID, Section.StubOffset,
888                             MachO::X86_64_RELOC_UNSIGNED, Value.Addend, false,
889                             3);
890       if (Value.SymbolName)
891         addRelocationForSymbol(GOTRE, Value.SymbolName);
892       else
893         addRelocationForSection(GOTRE, Value.SectionID);
894       Section.StubOffset += 8;
895       Addr = GOTEntry;
896     }
897     RelocationEntry TargetRE(SectionID, Offset,
898                              MachO::X86_64_RELOC_UNSIGNED, Addend, true,
899                              2);
900     resolveRelocation(TargetRE, (uint64_t)Addr);
901   } else if (Arch == Triple::arm && (RelType & 0xf) == MachO::ARM_RELOC_BR24) {
902     // This is an ARM branch relocation, need to use a stub function.
903
904     //  Look up for existing stub.
905     StubMap::const_iterator i = Stubs.find(Value);
906     uint8_t *Addr;
907     if (i != Stubs.end()) {
908       Addr = Section.Address + i->second;
909     } else {
910       // Create a new stub function.
911       Stubs[Value] = Section.StubOffset;
912       uint8_t *StubTargetAddr =
913           createStubFunction(Section.Address + Section.StubOffset);
914       RelocationEntry StubRE(SectionID, StubTargetAddr - Section.Address,
915                              MachO::GENERIC_RELOC_VANILLA, Value.Addend);
916       if (Value.SymbolName)
917         addRelocationForSymbol(StubRE, Value.SymbolName);
918       else
919         addRelocationForSection(StubRE, Value.SectionID);
920       Addr = Section.Address + Section.StubOffset;
921       Section.StubOffset += getMaxStubSize();
922     }
923     RelocationEntry TargetRE(Value.SectionID, Offset, RelType, 0, IsPCRel,
924                              Size);
925     resolveRelocation(TargetRE, (uint64_t)Addr);
926   } else if (Arch == Triple::arm64 &&
927              (RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 ||
928               RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12)) {
929     assert(Size == 2);
930     StubMap::const_iterator i = Stubs.find(Value);
931     uint8_t *Addr;
932     if (i != Stubs.end())
933       Addr = Section.Address + i->second;
934     else {
935       // FIXME: There must be a better way to do this then to check and fix the
936       // alignment every time!!!
937       uintptr_t BaseAddress = uintptr_t(Section.Address);
938       uintptr_t StubAlignment = getStubAlignment();
939       uintptr_t StubAddress
940         = (BaseAddress + Section.StubOffset + StubAlignment - 1) &
941           -StubAlignment;
942       unsigned StubOffset = StubAddress - BaseAddress;
943       Stubs[Value] = StubOffset;
944       assert(((StubAddress % getStubAlignment()) == 0) &&
945              "GOT entry not aligned");
946       RelocationEntry GOTRE(SectionID, StubOffset, MachO::ARM64_RELOC_UNSIGNED,
947                             Value.Addend, /*IsPCRel=*/false, /*Size=*/3);
948       if (Value.SymbolName)
949         addRelocationForSymbol(GOTRE, Value.SymbolName);
950       else
951         addRelocationForSection(GOTRE, Value.SectionID);
952       Section.StubOffset = StubOffset + getMaxStubSize();
953
954       Addr = (uint8_t *)StubAddress;
955     }
956     RelocationEntry TargetRE(SectionID, Offset, RelType, /*Addend=*/0, IsPCRel,
957                              Size);
958     resolveRelocation(TargetRE, (uint64_t)Addr);
959   } else {
960
961     RelocationEntry RE(SectionID, Offset, RelType, Value.Addend, IsPCRel, Size);
962     if (Value.SymbolName)
963       addRelocationForSymbol(RE, Value.SymbolName);
964     else
965       addRelocationForSection(RE, Value.SectionID);
966   }
967   return ++RelI;
968 }
969
970 bool
971 RuntimeDyldMachO::isCompatibleFormat(const ObjectBuffer *InputBuffer) const {
972   uint32_t Magic = readMachOMagic(InputBuffer->getBufferStart(),
973                                   InputBuffer->getBufferSize());
974   return (Magic == 0xFEEDFACE || Magic == 0xFEEDFACF);
975 }
976
977 bool RuntimeDyldMachO::isCompatibleFile(const object::ObjectFile *Obj) const {
978   return Obj->isMachO();
979 }
980
981 } // end namespace llvm