AArch64: remove arm64 triple enumerator.
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyld.cpp
1 //===-- RuntimeDyld.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 "llvm/ExecutionEngine/RuntimeDyld.h"
15 #include "JITRegistrar.h"
16 #include "ObjectImageCommon.h"
17 #include "RuntimeDyldCheckerImpl.h"
18 #include "RuntimeDyldELF.h"
19 #include "RuntimeDyldImpl.h"
20 #include "RuntimeDyldMachO.h"
21 #include "llvm/Object/ELF.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/MutexGuard.h"
24
25 using namespace llvm;
26 using namespace llvm::object;
27
28 #define DEBUG_TYPE "dyld"
29
30 // Empty out-of-line virtual destructor as the key function.
31 RuntimeDyldImpl::~RuntimeDyldImpl() {}
32
33 // Pin the JITRegistrar's and ObjectImage*'s vtables to this file.
34 void JITRegistrar::anchor() {}
35 void ObjectImage::anchor() {}
36 void ObjectImageCommon::anchor() {}
37
38 namespace llvm {
39
40 void RuntimeDyldImpl::registerEHFrames() {}
41
42 void RuntimeDyldImpl::deregisterEHFrames() {}
43
44 // Resolve the relocations for all symbols we currently know about.
45 void RuntimeDyldImpl::resolveRelocations() {
46   MutexGuard locked(lock);
47
48   // First, resolve relocations associated with external symbols.
49   resolveExternalSymbols();
50
51   // Just iterate over the sections we have and resolve all the relocations
52   // in them. Gross overkill, but it gets the job done.
53   for (int i = 0, e = Sections.size(); i != e; ++i) {
54     // The Section here (Sections[i]) refers to the section in which the
55     // symbol for the relocation is located.  The SectionID in the relocation
56     // entry provides the section to which the relocation will be applied.
57     uint64_t Addr = Sections[i].LoadAddress;
58     DEBUG(dbgs() << "Resolving relocations Section #" << i << "\t"
59                  << format("%p", (uint8_t *)Addr) << "\n");
60     resolveRelocationList(Relocations[i], Addr);
61     Relocations.erase(i);
62   }
63 }
64
65 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
66                                         uint64_t TargetAddress) {
67   MutexGuard locked(lock);
68   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
69     if (Sections[i].Address == LocalAddress) {
70       reassignSectionAddress(i, TargetAddress);
71       return;
72     }
73   }
74   llvm_unreachable("Attempting to remap address of unknown section!");
75 }
76
77 static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
78   uint64_t Address;
79   if (std::error_code EC = Sym.getAddress(Address))
80     return EC;
81
82   if (Address == UnknownAddressOrSize) {
83     Result = UnknownAddressOrSize;
84     return object_error::success;
85   }
86
87   const ObjectFile *Obj = Sym.getObject();
88   section_iterator SecI(Obj->section_begin());
89   if (std::error_code EC = Sym.getSection(SecI))
90     return EC;
91
92  if (SecI == Obj->section_end()) {
93    Result = UnknownAddressOrSize;
94    return object_error::success;
95  }
96
97   uint64_t SectionAddress;
98   if (std::error_code EC = SecI->getAddress(SectionAddress))
99     return EC;
100
101   Result = Address - SectionAddress;
102   return object_error::success;
103 }
104
105 ObjectImage *RuntimeDyldImpl::loadObject(ObjectImage *InputObject) {
106   MutexGuard locked(lock);
107
108   std::unique_ptr<ObjectImage> Obj(InputObject);
109   if (!Obj)
110     return nullptr;
111
112   // Save information about our target
113   Arch = (Triple::ArchType)Obj->getArch();
114   IsTargetLittleEndian = Obj->getObjectFile()->isLittleEndian();
115
116   // Compute the memory size required to load all sections to be loaded
117   // and pass this information to the memory manager
118   if (MemMgr->needsToReserveAllocationSpace()) {
119     uint64_t CodeSize = 0, DataSizeRO = 0, DataSizeRW = 0;
120     computeTotalAllocSize(*Obj, CodeSize, DataSizeRO, DataSizeRW);
121     MemMgr->reserveAllocationSpace(CodeSize, DataSizeRO, DataSizeRW);
122   }
123
124   // Symbols found in this object
125   StringMap<SymbolLoc> LocalSymbols;
126   // Used sections from the object file
127   ObjSectionToIDMap LocalSections;
128
129   // Common symbols requiring allocation, with their sizes and alignments
130   CommonSymbolMap CommonSymbols;
131   // Maximum required total memory to allocate all common symbols
132   uint64_t CommonSize = 0;
133
134   // Parse symbols
135   DEBUG(dbgs() << "Parse symbols:\n");
136   for (symbol_iterator I = Obj->begin_symbols(), E = Obj->end_symbols(); I != E;
137        ++I) {
138     object::SymbolRef::Type SymType;
139     StringRef Name;
140     Check(I->getType(SymType));
141     Check(I->getName(Name));
142
143     uint32_t Flags = I->getFlags();
144
145     bool IsCommon = Flags & SymbolRef::SF_Common;
146     if (IsCommon) {
147       // Add the common symbols to a list.  We'll allocate them all below.
148       if (!GlobalSymbolTable.count(Name)) {
149         uint32_t Align;
150         Check(I->getAlignment(Align));
151         uint64_t Size = 0;
152         Check(I->getSize(Size));
153         CommonSize += Size + Align;
154         CommonSymbols[*I] = CommonSymbolInfo(Size, Align);
155       }
156     } else {
157       if (SymType == object::SymbolRef::ST_Function ||
158           SymType == object::SymbolRef::ST_Data ||
159           SymType == object::SymbolRef::ST_Unknown) {
160         uint64_t SectOffset;
161         StringRef SectionData;
162         bool IsCode;
163         section_iterator SI = Obj->end_sections();
164         Check(getOffset(*I, SectOffset));
165         Check(I->getSection(SI));
166         if (SI == Obj->end_sections())
167           continue;
168         Check(SI->getContents(SectionData));
169         Check(SI->isText(IsCode));
170         unsigned SectionID =
171             findOrEmitSection(*Obj, *SI, IsCode, LocalSections);
172         LocalSymbols[Name.data()] = SymbolLoc(SectionID, SectOffset);
173         DEBUG(dbgs() << "\tOffset: " << format("%p", (uintptr_t)SectOffset)
174                      << " flags: " << Flags << " SID: " << SectionID);
175         GlobalSymbolTable[Name] = SymbolLoc(SectionID, SectOffset);
176       }
177     }
178     DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name << "\n");
179   }
180
181   // Allocate common symbols
182   if (CommonSize != 0)
183     emitCommonSymbols(*Obj, CommonSymbols, CommonSize, GlobalSymbolTable);
184
185   // Parse and process relocations
186   DEBUG(dbgs() << "Parse relocations:\n");
187   for (section_iterator SI = Obj->begin_sections(), SE = Obj->end_sections();
188        SI != SE; ++SI) {
189     unsigned SectionID = 0;
190     StubMap Stubs;
191     section_iterator RelocatedSection = SI->getRelocatedSection();
192
193     relocation_iterator I = SI->relocation_begin();
194     relocation_iterator E = SI->relocation_end();
195
196     if (I == E && !ProcessAllSections)
197       continue;
198
199     bool IsCode = false;
200     Check(RelocatedSection->isText(IsCode));
201     SectionID =
202         findOrEmitSection(*Obj, *RelocatedSection, IsCode, LocalSections);
203     DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
204
205     for (; I != E;)
206       I = processRelocationRef(SectionID, I, *Obj, LocalSections, LocalSymbols,
207                                Stubs);
208
209     // If there is an attached checker, notify it about the stubs for this
210     // section so that they can be verified.
211     if (Checker)
212       Checker->registerStubMap(Obj->getImageName(), SectionID, Stubs);
213   }
214
215   // Give the subclasses a chance to tie-up any loose ends.
216   finalizeLoad(*Obj, LocalSections);
217
218   return Obj.release();
219 }
220
221 // A helper method for computeTotalAllocSize.
222 // Computes the memory size required to allocate sections with the given sizes,
223 // assuming that all sections are allocated with the given alignment
224 static uint64_t
225 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
226                                  uint64_t Alignment) {
227   uint64_t TotalSize = 0;
228   for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
229     uint64_t AlignedSize =
230         (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
231     TotalSize += AlignedSize;
232   }
233   return TotalSize;
234 }
235
236 // Compute an upper bound of the memory size that is required to load all
237 // sections
238 void RuntimeDyldImpl::computeTotalAllocSize(ObjectImage &Obj,
239                                             uint64_t &CodeSize,
240                                             uint64_t &DataSizeRO,
241                                             uint64_t &DataSizeRW) {
242   // Compute the size of all sections required for execution
243   std::vector<uint64_t> CodeSectionSizes;
244   std::vector<uint64_t> ROSectionSizes;
245   std::vector<uint64_t> RWSectionSizes;
246   uint64_t MaxAlignment = sizeof(void *);
247
248   // Collect sizes of all sections to be loaded;
249   // also determine the max alignment of all sections
250   for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
251        SI != SE; ++SI) {
252     const SectionRef &Section = *SI;
253
254     bool IsRequired;
255     Check(Section.isRequiredForExecution(IsRequired));
256
257     // Consider only the sections that are required to be loaded for execution
258     if (IsRequired) {
259       uint64_t DataSize = 0;
260       uint64_t Alignment64 = 0;
261       bool IsCode = false;
262       bool IsReadOnly = false;
263       StringRef Name;
264       Check(Section.getSize(DataSize));
265       Check(Section.getAlignment(Alignment64));
266       Check(Section.isText(IsCode));
267       Check(Section.isReadOnlyData(IsReadOnly));
268       Check(Section.getName(Name));
269       unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
270
271       uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
272       uint64_t SectionSize = DataSize + StubBufSize;
273
274       // The .eh_frame section (at least on Linux) needs an extra four bytes
275       // padded
276       // with zeroes added at the end.  For MachO objects, this section has a
277       // slightly different name, so this won't have any effect for MachO
278       // objects.
279       if (Name == ".eh_frame")
280         SectionSize += 4;
281
282       if (SectionSize > 0) {
283         // save the total size of the section
284         if (IsCode) {
285           CodeSectionSizes.push_back(SectionSize);
286         } else if (IsReadOnly) {
287           ROSectionSizes.push_back(SectionSize);
288         } else {
289           RWSectionSizes.push_back(SectionSize);
290         }
291         // update the max alignment
292         if (Alignment > MaxAlignment) {
293           MaxAlignment = Alignment;
294         }
295       }
296     }
297   }
298
299   // Compute the size of all common symbols
300   uint64_t CommonSize = 0;
301   for (symbol_iterator I = Obj.begin_symbols(), E = Obj.end_symbols(); I != E;
302        ++I) {
303     uint32_t Flags = I->getFlags();
304     if (Flags & SymbolRef::SF_Common) {
305       // Add the common symbols to a list.  We'll allocate them all below.
306       uint64_t Size = 0;
307       Check(I->getSize(Size));
308       CommonSize += Size;
309     }
310   }
311   if (CommonSize != 0) {
312     RWSectionSizes.push_back(CommonSize);
313   }
314
315   // Compute the required allocation space for each different type of sections
316   // (code, read-only data, read-write data) assuming that all sections are
317   // allocated with the max alignment. Note that we cannot compute with the
318   // individual alignments of the sections, because then the required size
319   // depends on the order, in which the sections are allocated.
320   CodeSize = computeAllocationSizeForSections(CodeSectionSizes, MaxAlignment);
321   DataSizeRO = computeAllocationSizeForSections(ROSectionSizes, MaxAlignment);
322   DataSizeRW = computeAllocationSizeForSections(RWSectionSizes, MaxAlignment);
323 }
324
325 // compute stub buffer size for the given section
326 unsigned RuntimeDyldImpl::computeSectionStubBufSize(ObjectImage &Obj,
327                                                     const SectionRef &Section) {
328   unsigned StubSize = getMaxStubSize();
329   if (StubSize == 0) {
330     return 0;
331   }
332   // FIXME: this is an inefficient way to handle this. We should computed the
333   // necessary section allocation size in loadObject by walking all the sections
334   // once.
335   unsigned StubBufSize = 0;
336   for (section_iterator SI = Obj.begin_sections(), SE = Obj.end_sections();
337        SI != SE; ++SI) {
338     section_iterator RelSecI = SI->getRelocatedSection();
339     if (!(RelSecI == Section))
340       continue;
341
342     for (const RelocationRef &Reloc : SI->relocations()) {
343       (void)Reloc;
344       StubBufSize += StubSize;
345     }
346   }
347
348   // Get section data size and alignment
349   uint64_t Alignment64;
350   uint64_t DataSize;
351   Check(Section.getSize(DataSize));
352   Check(Section.getAlignment(Alignment64));
353
354   // Add stubbuf size alignment
355   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
356   unsigned StubAlignment = getStubAlignment();
357   unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
358   if (StubAlignment > EndAlignment)
359     StubBufSize += StubAlignment - EndAlignment;
360   return StubBufSize;
361 }
362
363 void RuntimeDyldImpl::emitCommonSymbols(ObjectImage &Obj,
364                                         const CommonSymbolMap &CommonSymbols,
365                                         uint64_t TotalSize,
366                                         SymbolTableMap &SymbolTable) {
367   // Allocate memory for the section
368   unsigned SectionID = Sections.size();
369   uint8_t *Addr = MemMgr->allocateDataSection(TotalSize, sizeof(void *),
370                                               SectionID, StringRef(), false);
371   if (!Addr)
372     report_fatal_error("Unable to allocate memory for common symbols!");
373   uint64_t Offset = 0;
374   Sections.push_back(SectionEntry(StringRef(), Addr, TotalSize, 0));
375   memset(Addr, 0, TotalSize);
376
377   DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
378                << format("%p", Addr) << " DataSize: " << TotalSize << "\n");
379
380   // Assign the address of each symbol
381   for (CommonSymbolMap::const_iterator it = CommonSymbols.begin(),
382        itEnd = CommonSymbols.end(); it != itEnd; ++it) {
383     uint64_t Size = it->second.first;
384     uint64_t Align = it->second.second;
385     StringRef Name;
386     it->first.getName(Name);
387     if (Align) {
388       // This symbol has an alignment requirement.
389       uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
390       Addr += AlignOffset;
391       Offset += AlignOffset;
392       DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
393                    << format("%p\n", Addr));
394     }
395     Obj.updateSymbolAddress(it->first, (uint64_t)Addr);
396     SymbolTable[Name.data()] = SymbolLoc(SectionID, Offset);
397     Offset += Size;
398     Addr += Size;
399   }
400 }
401
402 unsigned RuntimeDyldImpl::emitSection(ObjectImage &Obj,
403                                       const SectionRef &Section, bool IsCode) {
404
405   StringRef data;
406   uint64_t Alignment64;
407   Check(Section.getContents(data));
408   Check(Section.getAlignment(Alignment64));
409
410   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
411   bool IsRequired;
412   bool IsVirtual;
413   bool IsZeroInit;
414   bool IsReadOnly;
415   uint64_t DataSize;
416   unsigned PaddingSize = 0;
417   unsigned StubBufSize = 0;
418   StringRef Name;
419   Check(Section.isRequiredForExecution(IsRequired));
420   Check(Section.isVirtual(IsVirtual));
421   Check(Section.isZeroInit(IsZeroInit));
422   Check(Section.isReadOnlyData(IsReadOnly));
423   Check(Section.getSize(DataSize));
424   Check(Section.getName(Name));
425
426   StubBufSize = computeSectionStubBufSize(Obj, Section);
427
428   // The .eh_frame section (at least on Linux) needs an extra four bytes padded
429   // with zeroes added at the end.  For MachO objects, this section has a
430   // slightly different name, so this won't have any effect for MachO objects.
431   if (Name == ".eh_frame")
432     PaddingSize = 4;
433
434   uintptr_t Allocate;
435   unsigned SectionID = Sections.size();
436   uint8_t *Addr;
437   const char *pData = nullptr;
438
439   // Some sections, such as debug info, don't need to be loaded for execution.
440   // Leave those where they are.
441   if (IsRequired) {
442     Allocate = DataSize + PaddingSize + StubBufSize;
443     Addr = IsCode ? MemMgr->allocateCodeSection(Allocate, Alignment, SectionID,
444                                                 Name)
445                   : MemMgr->allocateDataSection(Allocate, Alignment, SectionID,
446                                                 Name, IsReadOnly);
447     if (!Addr)
448       report_fatal_error("Unable to allocate section memory!");
449
450     // Virtual sections have no data in the object image, so leave pData = 0
451     if (!IsVirtual)
452       pData = data.data();
453
454     // Zero-initialize or copy the data from the image
455     if (IsZeroInit || IsVirtual)
456       memset(Addr, 0, DataSize);
457     else
458       memcpy(Addr, pData, DataSize);
459
460     // Fill in any extra bytes we allocated for padding
461     if (PaddingSize != 0) {
462       memset(Addr + DataSize, 0, PaddingSize);
463       // Update the DataSize variable so that the stub offset is set correctly.
464       DataSize += PaddingSize;
465     }
466
467     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
468                  << " obj addr: " << format("%p", pData)
469                  << " new addr: " << format("%p", Addr)
470                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
471                  << " Allocate: " << Allocate << "\n");
472     Obj.updateSectionAddress(Section, (uint64_t)Addr);
473   } else {
474     // Even if we didn't load the section, we need to record an entry for it
475     // to handle later processing (and by 'handle' I mean don't do anything
476     // with these sections).
477     Allocate = 0;
478     Addr = nullptr;
479     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
480                  << " obj addr: " << format("%p", data.data()) << " new addr: 0"
481                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
482                  << " Allocate: " << Allocate << "\n");
483   }
484
485   Sections.push_back(SectionEntry(Name, Addr, DataSize, (uintptr_t)pData));
486   return SectionID;
487 }
488
489 unsigned RuntimeDyldImpl::findOrEmitSection(ObjectImage &Obj,
490                                             const SectionRef &Section,
491                                             bool IsCode,
492                                             ObjSectionToIDMap &LocalSections) {
493
494   unsigned SectionID = 0;
495   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
496   if (i != LocalSections.end())
497     SectionID = i->second;
498   else {
499     SectionID = emitSection(Obj, Section, IsCode);
500     LocalSections[Section] = SectionID;
501   }
502   return SectionID;
503 }
504
505 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
506                                               unsigned SectionID) {
507   Relocations[SectionID].push_back(RE);
508 }
509
510 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
511                                              StringRef SymbolName) {
512   // Relocation by symbol.  If the symbol is found in the global symbol table,
513   // create an appropriate section relocation.  Otherwise, add it to
514   // ExternalSymbolRelocations.
515   SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
516   if (Loc == GlobalSymbolTable.end()) {
517     ExternalSymbolRelocations[SymbolName].push_back(RE);
518   } else {
519     // Copy the RE since we want to modify its addend.
520     RelocationEntry RECopy = RE;
521     RECopy.Addend += Loc->second.second;
522     Relocations[Loc->second.first].push_back(RECopy);
523   }
524 }
525
526 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
527                                              unsigned AbiVariant) {
528   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
529     // This stub has to be able to access the full address space,
530     // since symbol lookup won't necessarily find a handy, in-range,
531     // PLT stub for functions which could be anywhere.
532     uint32_t *StubAddr = (uint32_t *)Addr;
533
534     // Stub can use ip0 (== x16) to calculate address
535     *StubAddr = 0xd2e00010; // movz ip0, #:abs_g3:<addr>
536     StubAddr++;
537     *StubAddr = 0xf2c00010; // movk ip0, #:abs_g2_nc:<addr>
538     StubAddr++;
539     *StubAddr = 0xf2a00010; // movk ip0, #:abs_g1_nc:<addr>
540     StubAddr++;
541     *StubAddr = 0xf2800010; // movk ip0, #:abs_g0_nc:<addr>
542     StubAddr++;
543     *StubAddr = 0xd61f0200; // br ip0
544
545     return Addr;
546   } else if (Arch == Triple::arm || Arch == Triple::armeb) {
547     // TODO: There is only ARM far stub now. We should add the Thumb stub,
548     // and stubs for branches Thumb - ARM and ARM - Thumb.
549     uint32_t *StubAddr = (uint32_t *)Addr;
550     *StubAddr = 0xe51ff004; // ldr pc,<label>
551     return (uint8_t *)++StubAddr;
552   } else if (Arch == Triple::mipsel || Arch == Triple::mips) {
553     uint32_t *StubAddr = (uint32_t *)Addr;
554     // 0:   3c190000        lui     t9,%hi(addr).
555     // 4:   27390000        addiu   t9,t9,%lo(addr).
556     // 8:   03200008        jr      t9.
557     // c:   00000000        nop.
558     const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
559     const unsigned JrT9Instr = 0x03200008, NopInstr = 0x0;
560
561     *StubAddr = LuiT9Instr;
562     StubAddr++;
563     *StubAddr = AdduiT9Instr;
564     StubAddr++;
565     *StubAddr = JrT9Instr;
566     StubAddr++;
567     *StubAddr = NopInstr;
568     return Addr;
569   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
570     // Depending on which version of the ELF ABI is in use, we need to
571     // generate one of two variants of the stub.  They both start with
572     // the same sequence to load the target address into r12.
573     writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
574     writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
575     writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
576     writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
577     writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
578     if (AbiVariant == 2) {
579       // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
580       // The address is already in r12 as required by the ABI.  Branch to it.
581       writeInt32BE(Addr+20, 0xF8410018); // std   r2,  24(r1)
582       writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
583       writeInt32BE(Addr+28, 0x4E800420); // bctr
584     } else {
585       // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
586       // Load the function address on r11 and sets it to control register. Also
587       // loads the function TOC in r2 and environment pointer to r11.
588       writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
589       writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
590       writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
591       writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
592       writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
593       writeInt32BE(Addr+40, 0x4E800420); // bctr
594     }
595     return Addr;
596   } else if (Arch == Triple::systemz) {
597     writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
598     writeInt16BE(Addr+2,  0x0000);
599     writeInt16BE(Addr+4,  0x0004);
600     writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
601     // 8-byte address stored at Addr + 8
602     return Addr;
603   } else if (Arch == Triple::x86_64) {
604     *Addr      = 0xFF; // jmp
605     *(Addr+1)  = 0x25; // rip
606     // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
607   } else if (Arch == Triple::x86) {
608     *Addr      = 0xE9; // 32-bit pc-relative jump.
609   }
610   return Addr;
611 }
612
613 // Assign an address to a symbol name and resolve all the relocations
614 // associated with it.
615 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
616                                              uint64_t Addr) {
617   // The address to use for relocation resolution is not
618   // the address of the local section buffer. We must be doing
619   // a remote execution environment of some sort. Relocations can't
620   // be applied until all the sections have been moved.  The client must
621   // trigger this with a call to MCJIT::finalize() or
622   // RuntimeDyld::resolveRelocations().
623   //
624   // Addr is a uint64_t because we can't assume the pointer width
625   // of the target is the same as that of the host. Just use a generic
626   // "big enough" type.
627   Sections[SectionID].LoadAddress = Addr;
628 }
629
630 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
631                                             uint64_t Value) {
632   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
633     const RelocationEntry &RE = Relocs[i];
634     // Ignore relocations for sections that were not loaded
635     if (Sections[RE.SectionID].Address == nullptr)
636       continue;
637     resolveRelocation(RE, Value);
638   }
639 }
640
641 void RuntimeDyldImpl::resolveExternalSymbols() {
642   while (!ExternalSymbolRelocations.empty()) {
643     StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
644
645     StringRef Name = i->first();
646     if (Name.size() == 0) {
647       // This is an absolute symbol, use an address of zero.
648       DEBUG(dbgs() << "Resolving absolute relocations."
649                    << "\n");
650       RelocationList &Relocs = i->second;
651       resolveRelocationList(Relocs, 0);
652     } else {
653       uint64_t Addr = 0;
654       SymbolTableMap::const_iterator Loc = GlobalSymbolTable.find(Name);
655       if (Loc == GlobalSymbolTable.end()) {
656         // This is an external symbol, try to get its address from
657         // MemoryManager.
658         Addr = MemMgr->getSymbolAddress(Name.data());
659         // The call to getSymbolAddress may have caused additional modules to
660         // be loaded, which may have added new entries to the
661         // ExternalSymbolRelocations map.  Consquently, we need to update our
662         // iterator.  This is also why retrieval of the relocation list
663         // associated with this symbol is deferred until below this point.
664         // New entries may have been added to the relocation list.
665         i = ExternalSymbolRelocations.find(Name);
666       } else {
667         // We found the symbol in our global table.  It was probably in a
668         // Module that we loaded previously.
669         SymbolLoc SymLoc = Loc->second;
670         Addr = getSectionLoadAddress(SymLoc.first) + SymLoc.second;
671       }
672
673       // FIXME: Implement error handling that doesn't kill the host program!
674       if (!Addr)
675         report_fatal_error("Program used external function '" + Name +
676                            "' which could not be resolved!");
677
678       updateGOTEntries(Name, Addr);
679       DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
680                    << format("0x%lx", Addr) << "\n");
681       // This list may have been updated when we called getSymbolAddress, so
682       // don't change this code to get the list earlier.
683       RelocationList &Relocs = i->second;
684       resolveRelocationList(Relocs, Addr);
685     }
686
687     ExternalSymbolRelocations.erase(i);
688   }
689 }
690
691 //===----------------------------------------------------------------------===//
692 // RuntimeDyld class implementation
693 RuntimeDyld::RuntimeDyld(RTDyldMemoryManager *mm) {
694   // FIXME: There's a potential issue lurking here if a single instance of
695   // RuntimeDyld is used to load multiple objects.  The current implementation
696   // associates a single memory manager with a RuntimeDyld instance.  Even
697   // though the public class spawns a new 'impl' instance for each load,
698   // they share a single memory manager.  This can become a problem when page
699   // permissions are applied.
700   Dyld = nullptr;
701   MM = mm;
702   ProcessAllSections = false;
703   Checker = nullptr;
704 }
705
706 RuntimeDyld::~RuntimeDyld() { delete Dyld; }
707
708 static std::unique_ptr<RuntimeDyldELF>
709 createRuntimeDyldELF(RTDyldMemoryManager *MM, bool ProcessAllSections,
710                      RuntimeDyldCheckerImpl *Checker) {
711   std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM));
712   Dyld->setProcessAllSections(ProcessAllSections);
713   Dyld->setRuntimeDyldChecker(Checker);
714   return Dyld;
715 }
716
717 static std::unique_ptr<RuntimeDyldMachO>
718 createRuntimeDyldMachO(Triple::ArchType Arch, RTDyldMemoryManager *MM,
719                        bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker) {
720   std::unique_ptr<RuntimeDyldMachO> Dyld(RuntimeDyldMachO::create(Arch, MM));
721   Dyld->setProcessAllSections(ProcessAllSections);
722   Dyld->setRuntimeDyldChecker(Checker);
723   return Dyld;
724 }
725
726 ObjectImage *RuntimeDyld::loadObject(std::unique_ptr<ObjectFile> InputObject) {
727   std::unique_ptr<ObjectImage> InputImage;
728
729   ObjectFile &Obj = *InputObject;
730
731   if (InputObject->isELF()) {
732     InputImage.reset(RuntimeDyldELF::createObjectImageFromFile(std::move(InputObject)));
733     if (!Dyld)
734       Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
735   } else if (InputObject->isMachO()) {
736     InputImage.reset(RuntimeDyldMachO::createObjectImageFromFile(std::move(InputObject)));
737     if (!Dyld)
738       Dyld = createRuntimeDyldMachO(
739                            static_cast<Triple::ArchType>(InputImage->getArch()),
740                            MM, ProcessAllSections, Checker).release();
741   } else
742     report_fatal_error("Incompatible object format!");
743
744   if (!Dyld->isCompatibleFile(&Obj))
745     report_fatal_error("Incompatible object format!");
746
747   Dyld->loadObject(InputImage.get());
748   return InputImage.release();
749 }
750
751 ObjectImage *RuntimeDyld::loadObject(ObjectBuffer *InputBuffer) {
752   std::unique_ptr<ObjectImage> InputImage;
753   sys::fs::file_magic Type = sys::fs::identify_magic(InputBuffer->getBuffer());
754
755   switch (Type) {
756   case sys::fs::file_magic::elf_relocatable:
757   case sys::fs::file_magic::elf_executable:
758   case sys::fs::file_magic::elf_shared_object:
759   case sys::fs::file_magic::elf_core:
760     InputImage.reset(RuntimeDyldELF::createObjectImage(InputBuffer));
761     if (!Dyld)
762       Dyld = createRuntimeDyldELF(MM, ProcessAllSections, Checker).release();
763     break;
764   case sys::fs::file_magic::macho_object:
765   case sys::fs::file_magic::macho_executable:
766   case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
767   case sys::fs::file_magic::macho_core:
768   case sys::fs::file_magic::macho_preload_executable:
769   case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
770   case sys::fs::file_magic::macho_dynamic_linker:
771   case sys::fs::file_magic::macho_bundle:
772   case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
773   case sys::fs::file_magic::macho_dsym_companion:
774     InputImage.reset(RuntimeDyldMachO::createObjectImage(InputBuffer));
775     if (!Dyld)
776       Dyld = createRuntimeDyldMachO(
777                            static_cast<Triple::ArchType>(InputImage->getArch()),
778                            MM, ProcessAllSections, Checker).release();
779     break;
780   case sys::fs::file_magic::unknown:
781   case sys::fs::file_magic::bitcode:
782   case sys::fs::file_magic::archive:
783   case sys::fs::file_magic::coff_object:
784   case sys::fs::file_magic::coff_import_library:
785   case sys::fs::file_magic::pecoff_executable:
786   case sys::fs::file_magic::macho_universal_binary:
787   case sys::fs::file_magic::windows_resource:
788     report_fatal_error("Incompatible object format!");
789   }
790
791   if (!Dyld->isCompatibleFormat(InputBuffer))
792     report_fatal_error("Incompatible object format!");
793
794   Dyld->loadObject(InputImage.get());
795   return InputImage.release();
796 }
797
798 void *RuntimeDyld::getSymbolAddress(StringRef Name) {
799   if (!Dyld)
800     return nullptr;
801   return Dyld->getSymbolAddress(Name);
802 }
803
804 uint64_t RuntimeDyld::getSymbolLoadAddress(StringRef Name) {
805   if (!Dyld)
806     return 0;
807   return Dyld->getSymbolLoadAddress(Name);
808 }
809
810 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
811
812 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
813   Dyld->reassignSectionAddress(SectionID, Addr);
814 }
815
816 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
817                                     uint64_t TargetAddress) {
818   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
819 }
820
821 bool RuntimeDyld::hasError() { return Dyld->hasError(); }
822
823 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
824
825 void RuntimeDyld::registerEHFrames() {
826   if (Dyld)
827     Dyld->registerEHFrames();
828 }
829
830 void RuntimeDyld::deregisterEHFrames() {
831   if (Dyld)
832     Dyld->deregisterEHFrames();
833 }
834
835 } // end namespace llvm