1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation of the MC-JIT runtime dynamic linker.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ExecutionEngine/RuntimeDyld.h"
14 #include "RuntimeDyldCOFF.h"
15 #include "RuntimeDyldELF.h"
16 #include "RuntimeDyldImpl.h"
17 #include "RuntimeDyldMachO.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Object/ELFObjectFile.h"
20 #include "llvm/Support/Alignment.h"
21 #include "llvm/Support/MSVCErrorWorkarounds.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/MathExtras.h"
24 #include <mutex>
25 
26 #include <future>
27 
28 using namespace llvm;
29 using namespace llvm::object;
30 
31 #define DEBUG_TYPE "dyld"
32 
33 namespace {
34 
35 enum RuntimeDyldErrorCode {
36   GenericRTDyldError = 1
37 };
38 
39 // FIXME: This class is only here to support the transition to llvm::Error. It
40 // will be removed once this transition is complete. Clients should prefer to
41 // deal with the Error value directly, rather than converting to error_code.
42 class RuntimeDyldErrorCategory : public std::error_category {
43 public:
44   const char *name() const noexcept override { return "runtimedyld"; }
45 
46   std::string message(int Condition) const override {
47     switch (static_cast<RuntimeDyldErrorCode>(Condition)) {
48       case GenericRTDyldError: return "Generic RuntimeDyld error";
49     }
50     llvm_unreachable("Unrecognized RuntimeDyldErrorCode");
51   }
52 };
53 
54 static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory;
55 
56 }
57 
58 char RuntimeDyldError::ID = 0;
59 
60 void RuntimeDyldError::log(raw_ostream &OS) const {
61   OS << ErrMsg << "\n";
62 }
63 
64 std::error_code RuntimeDyldError::convertToErrorCode() const {
65   return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
66 }
67 
68 // Empty out-of-line virtual destructor as the key function.
69 RuntimeDyldImpl::~RuntimeDyldImpl() {}
70 
71 // Pin LoadedObjectInfo's vtables to this file.
72 void RuntimeDyld::LoadedObjectInfo::anchor() {}
73 
74 namespace llvm {
75 
76 void RuntimeDyldImpl::registerEHFrames() {}
77 
78 void RuntimeDyldImpl::deregisterEHFrames() {
79   MemMgr.deregisterEHFrames();
80 }
81 
82 #ifndef NDEBUG
83 static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
84   dbgs() << "----- Contents of section " << S.getName() << " " << State
85          << " -----";
86 
87   if (S.getAddress() == nullptr) {
88     dbgs() << "\n          <section not emitted>\n";
89     return;
90   }
91 
92   const unsigned ColsPerRow = 16;
93 
94   uint8_t *DataAddr = S.getAddress();
95   uint64_t LoadAddr = S.getLoadAddress();
96 
97   unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
98   unsigned BytesRemaining = S.getSize();
99 
100   if (StartPadding) {
101     dbgs() << "\n" << format("0x%016" PRIx64,
102                              LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
103     while (StartPadding--)
104       dbgs() << "   ";
105   }
106 
107   while (BytesRemaining > 0) {
108     if ((LoadAddr & (ColsPerRow - 1)) == 0)
109       dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
110 
111     dbgs() << " " << format("%02x", *DataAddr);
112 
113     ++DataAddr;
114     ++LoadAddr;
115     --BytesRemaining;
116   }
117 
118   dbgs() << "\n";
119 }
120 #endif
121 
122 // Resolve the relocations for all symbols we currently know about.
123 void RuntimeDyldImpl::resolveRelocations() {
124   std::lock_guard<sys::Mutex> locked(lock);
125 
126   // Print out the sections prior to relocation.
127   LLVM_DEBUG(for (int i = 0, e = Sections.size(); i != e; ++i)
128                  dumpSectionMemory(Sections[i], "before relocations"););
129 
130   // First, resolve relocations associated with external symbols.
131   if (auto Err = resolveExternalSymbols()) {
132     HasError = true;
133     ErrorStr = toString(std::move(Err));
134   }
135 
136   resolveLocalRelocations();
137 
138   // Print out sections after relocation.
139   LLVM_DEBUG(for (int i = 0, e = Sections.size(); i != e; ++i)
140                  dumpSectionMemory(Sections[i], "after relocations"););
141 }
142 
143 void RuntimeDyldImpl::resolveLocalRelocations() {
144   // Iterate over all outstanding relocations
145   for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
146     // The Section here (Sections[i]) refers to the section in which the
147     // symbol for the relocation is located.  The SectionID in the relocation
148     // entry provides the section to which the relocation will be applied.
149     int Idx = it->first;
150     uint64_t Addr = Sections[Idx].getLoadAddress();
151     LLVM_DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
152                       << format("%p", (uintptr_t)Addr) << "\n");
153     resolveRelocationList(it->second, Addr);
154   }
155   Relocations.clear();
156 }
157 
158 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
159                                         uint64_t TargetAddress) {
160   std::lock_guard<sys::Mutex> locked(lock);
161   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
162     if (Sections[i].getAddress() == LocalAddress) {
163       reassignSectionAddress(i, TargetAddress);
164       return;
165     }
166   }
167   llvm_unreachable("Attempting to remap address of unknown section!");
168 }
169 
170 static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
171                        uint64_t &Result) {
172   Expected<uint64_t> AddressOrErr = Sym.getAddress();
173   if (!AddressOrErr)
174     return AddressOrErr.takeError();
175   Result = *AddressOrErr - Sec.getAddress();
176   return Error::success();
177 }
178 
179 Expected<RuntimeDyldImpl::ObjSectionToIDMap>
180 RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
181   std::lock_guard<sys::Mutex> locked(lock);
182 
183   // Save information about our target
184   Arch = (Triple::ArchType)Obj.getArch();
185   IsTargetLittleEndian = Obj.isLittleEndian();
186   setMipsABI(Obj);
187 
188   // Compute the memory size required to load all sections to be loaded
189   // and pass this information to the memory manager
190   if (MemMgr.needsToReserveAllocationSpace()) {
191     uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0;
192     uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1;
193     if (auto Err = computeTotalAllocSize(Obj,
194                                          CodeSize, CodeAlign,
195                                          RODataSize, RODataAlign,
196                                          RWDataSize, RWDataAlign))
197       return std::move(Err);
198     MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
199                                   RWDataSize, RWDataAlign);
200   }
201 
202   // Used sections from the object file
203   ObjSectionToIDMap LocalSections;
204 
205   // Common symbols requiring allocation, with their sizes and alignments
206   CommonSymbolList CommonSymbolsToAllocate;
207 
208   uint64_t CommonSize = 0;
209   uint32_t CommonAlign = 0;
210 
211   // First, collect all weak and common symbols. We need to know if stronger
212   // definitions occur elsewhere.
213   JITSymbolResolver::LookupSet ResponsibilitySet;
214   {
215     JITSymbolResolver::LookupSet Symbols;
216     for (auto &Sym : Obj.symbols()) {
217       Expected<uint32_t> FlagsOrErr = Sym.getFlags();
218       if (!FlagsOrErr)
219         // TODO: Test this error.
220         return FlagsOrErr.takeError();
221       if ((*FlagsOrErr & SymbolRef::SF_Common) ||
222           (*FlagsOrErr & SymbolRef::SF_Weak)) {
223         // Get symbol name.
224         if (auto NameOrErr = Sym.getName())
225           Symbols.insert(*NameOrErr);
226         else
227           return NameOrErr.takeError();
228       }
229     }
230 
231     if (auto ResultOrErr = Resolver.getResponsibilitySet(Symbols))
232       ResponsibilitySet = std::move(*ResultOrErr);
233     else
234       return ResultOrErr.takeError();
235   }
236 
237   // Parse symbols
238   LLVM_DEBUG(dbgs() << "Parse symbols:\n");
239   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
240        ++I) {
241     Expected<uint32_t> FlagsOrErr = I->getFlags();
242     if (!FlagsOrErr)
243       // TODO: Test this error.
244       return FlagsOrErr.takeError();
245 
246     // Skip undefined symbols.
247     if (*FlagsOrErr & SymbolRef::SF_Undefined)
248       continue;
249 
250     // Get the symbol type.
251     object::SymbolRef::Type SymType;
252     if (auto SymTypeOrErr = I->getType())
253       SymType = *SymTypeOrErr;
254     else
255       return SymTypeOrErr.takeError();
256 
257     // Get symbol name.
258     StringRef Name;
259     if (auto NameOrErr = I->getName())
260       Name = *NameOrErr;
261     else
262       return NameOrErr.takeError();
263 
264     // Compute JIT symbol flags.
265     auto JITSymFlags = getJITSymbolFlags(*I);
266     if (!JITSymFlags)
267       return JITSymFlags.takeError();
268 
269     // If this is a weak definition, check to see if there's a strong one.
270     // If there is, skip this symbol (we won't be providing it: the strong
271     // definition will). If there's no strong definition, make this definition
272     // strong.
273     if (JITSymFlags->isWeak() || JITSymFlags->isCommon()) {
274       // First check whether there's already a definition in this instance.
275       if (GlobalSymbolTable.count(Name))
276         continue;
277 
278       // If we're not responsible for this symbol, skip it.
279       if (!ResponsibilitySet.count(Name))
280         continue;
281 
282       // Otherwise update the flags on the symbol to make this definition
283       // strong.
284       if (JITSymFlags->isWeak())
285         *JITSymFlags &= ~JITSymbolFlags::Weak;
286       if (JITSymFlags->isCommon()) {
287         *JITSymFlags &= ~JITSymbolFlags::Common;
288         uint32_t Align = I->getAlignment();
289         uint64_t Size = I->getCommonSize();
290         if (!CommonAlign)
291           CommonAlign = Align;
292         CommonSize = alignTo(CommonSize, Align) + Size;
293         CommonSymbolsToAllocate.push_back(*I);
294       }
295     }
296 
297     if (*FlagsOrErr & SymbolRef::SF_Absolute &&
298         SymType != object::SymbolRef::ST_File) {
299       uint64_t Addr = 0;
300       if (auto AddrOrErr = I->getAddress())
301         Addr = *AddrOrErr;
302       else
303         return AddrOrErr.takeError();
304 
305       unsigned SectionID = AbsoluteSymbolSection;
306 
307       LLVM_DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name
308                         << " SID: " << SectionID
309                         << " Offset: " << format("%p", (uintptr_t)Addr)
310                         << " flags: " << *FlagsOrErr << "\n");
311       if (!Name.empty()) // Skip absolute symbol relocations.
312         GlobalSymbolTable[Name] =
313             SymbolTableEntry(SectionID, Addr, *JITSymFlags);
314     } else if (SymType == object::SymbolRef::ST_Function ||
315                SymType == object::SymbolRef::ST_Data ||
316                SymType == object::SymbolRef::ST_Unknown ||
317                SymType == object::SymbolRef::ST_Other) {
318 
319       section_iterator SI = Obj.section_end();
320       if (auto SIOrErr = I->getSection())
321         SI = *SIOrErr;
322       else
323         return SIOrErr.takeError();
324 
325       if (SI == Obj.section_end())
326         continue;
327 
328       // Get symbol offset.
329       uint64_t SectOffset;
330       if (auto Err = getOffset(*I, *SI, SectOffset))
331         return std::move(Err);
332 
333       bool IsCode = SI->isText();
334       unsigned SectionID;
335       if (auto SectionIDOrErr =
336               findOrEmitSection(Obj, *SI, IsCode, LocalSections))
337         SectionID = *SectionIDOrErr;
338       else
339         return SectionIDOrErr.takeError();
340 
341       LLVM_DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
342                         << " SID: " << SectionID
343                         << " Offset: " << format("%p", (uintptr_t)SectOffset)
344                         << " flags: " << *FlagsOrErr << "\n");
345       if (!Name.empty()) // Skip absolute symbol relocations
346         GlobalSymbolTable[Name] =
347             SymbolTableEntry(SectionID, SectOffset, *JITSymFlags);
348     }
349   }
350 
351   // Allocate common symbols
352   if (auto Err = emitCommonSymbols(Obj, CommonSymbolsToAllocate, CommonSize,
353                                    CommonAlign))
354     return std::move(Err);
355 
356   // Parse and process relocations
357   LLVM_DEBUG(dbgs() << "Parse relocations:\n");
358   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
359        SI != SE; ++SI) {
360     StubMap Stubs;
361 
362     Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection();
363     if (!RelSecOrErr)
364       return RelSecOrErr.takeError();
365 
366     section_iterator RelocatedSection = *RelSecOrErr;
367     if (RelocatedSection == SE)
368       continue;
369 
370     relocation_iterator I = SI->relocation_begin();
371     relocation_iterator E = SI->relocation_end();
372 
373     if (I == E && !ProcessAllSections)
374       continue;
375 
376     bool IsCode = RelocatedSection->isText();
377     unsigned SectionID = 0;
378     if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
379                                                 LocalSections))
380       SectionID = *SectionIDOrErr;
381     else
382       return SectionIDOrErr.takeError();
383 
384     LLVM_DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
385 
386     for (; I != E;)
387       if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs))
388         I = *IOrErr;
389       else
390         return IOrErr.takeError();
391 
392     // If there is a NotifyStubEmitted callback set, call it to register any
393     // stubs created for this section.
394     if (NotifyStubEmitted) {
395       StringRef FileName = Obj.getFileName();
396       StringRef SectionName = Sections[SectionID].getName();
397       for (auto &KV : Stubs) {
398 
399         auto &VR = KV.first;
400         uint64_t StubAddr = KV.second;
401 
402         // If this is a named stub, just call NotifyStubEmitted.
403         if (VR.SymbolName) {
404           NotifyStubEmitted(FileName, SectionName, VR.SymbolName, SectionID,
405                             StubAddr);
406           continue;
407         }
408 
409         // Otherwise we will have to try a reverse lookup on the globla symbol table.
410         for (auto &GSTMapEntry : GlobalSymbolTable) {
411           StringRef SymbolName = GSTMapEntry.first();
412           auto &GSTEntry = GSTMapEntry.second;
413           if (GSTEntry.getSectionID() == VR.SectionID &&
414               GSTEntry.getOffset() == VR.Offset) {
415             NotifyStubEmitted(FileName, SectionName, SymbolName, SectionID,
416                               StubAddr);
417             break;
418           }
419         }
420       }
421     }
422   }
423 
424   // Process remaining sections
425   if (ProcessAllSections) {
426     LLVM_DEBUG(dbgs() << "Process remaining sections:\n");
427     for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
428          SI != SE; ++SI) {
429 
430       /* Ignore already loaded sections */
431       if (LocalSections.find(*SI) != LocalSections.end())
432         continue;
433 
434       bool IsCode = SI->isText();
435       if (auto SectionIDOrErr =
436               findOrEmitSection(Obj, *SI, IsCode, LocalSections))
437         LLVM_DEBUG(dbgs() << "\tSectionID: " << (*SectionIDOrErr) << "\n");
438       else
439         return SectionIDOrErr.takeError();
440     }
441   }
442 
443   // Give the subclasses a chance to tie-up any loose ends.
444   if (auto Err = finalizeLoad(Obj, LocalSections))
445     return std::move(Err);
446 
447 //   for (auto E : LocalSections)
448 //     llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
449 
450   return LocalSections;
451 }
452 
453 // A helper method for computeTotalAllocSize.
454 // Computes the memory size required to allocate sections with the given sizes,
455 // assuming that all sections are allocated with the given alignment
456 static uint64_t
457 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
458                                  uint64_t Alignment) {
459   uint64_t TotalSize = 0;
460   for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
461     uint64_t AlignedSize =
462         (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
463     TotalSize += AlignedSize;
464   }
465   return TotalSize;
466 }
467 
468 static bool isRequiredForExecution(const SectionRef Section) {
469   const ObjectFile *Obj = Section.getObject();
470   if (isa<object::ELFObjectFileBase>(Obj))
471     return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
472   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
473     const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
474     // Avoid loading zero-sized COFF sections.
475     // In PE files, VirtualSize gives the section size, and SizeOfRawData
476     // may be zero for sections with content. In Obj files, SizeOfRawData
477     // gives the section size, and VirtualSize is always zero. Hence
478     // the need to check for both cases below.
479     bool HasContent =
480         (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0);
481     bool IsDiscardable =
482         CoffSection->Characteristics &
483         (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO);
484     return HasContent && !IsDiscardable;
485   }
486 
487   assert(isa<MachOObjectFile>(Obj));
488   return true;
489 }
490 
491 static bool isReadOnlyData(const SectionRef Section) {
492   const ObjectFile *Obj = Section.getObject();
493   if (isa<object::ELFObjectFileBase>(Obj))
494     return !(ELFSectionRef(Section).getFlags() &
495              (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
496   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
497     return ((COFFObj->getCOFFSection(Section)->Characteristics &
498              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
499              | COFF::IMAGE_SCN_MEM_READ
500              | COFF::IMAGE_SCN_MEM_WRITE))
501              ==
502              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
503              | COFF::IMAGE_SCN_MEM_READ));
504 
505   assert(isa<MachOObjectFile>(Obj));
506   return false;
507 }
508 
509 static bool isZeroInit(const SectionRef Section) {
510   const ObjectFile *Obj = Section.getObject();
511   if (isa<object::ELFObjectFileBase>(Obj))
512     return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS;
513   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
514     return COFFObj->getCOFFSection(Section)->Characteristics &
515             COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
516 
517   auto *MachO = cast<MachOObjectFile>(Obj);
518   unsigned SectionType = MachO->getSectionType(Section);
519   return SectionType == MachO::S_ZEROFILL ||
520          SectionType == MachO::S_GB_ZEROFILL;
521 }
522 
523 // Compute an upper bound of the memory size that is required to load all
524 // sections
525 Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
526                                              uint64_t &CodeSize,
527                                              uint32_t &CodeAlign,
528                                              uint64_t &RODataSize,
529                                              uint32_t &RODataAlign,
530                                              uint64_t &RWDataSize,
531                                              uint32_t &RWDataAlign) {
532   // Compute the size of all sections required for execution
533   std::vector<uint64_t> CodeSectionSizes;
534   std::vector<uint64_t> ROSectionSizes;
535   std::vector<uint64_t> RWSectionSizes;
536 
537   // Collect sizes of all sections to be loaded;
538   // also determine the max alignment of all sections
539   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
540        SI != SE; ++SI) {
541     const SectionRef &Section = *SI;
542 
543     bool IsRequired = isRequiredForExecution(Section) || ProcessAllSections;
544 
545     // Consider only the sections that are required to be loaded for execution
546     if (IsRequired) {
547       uint64_t DataSize = Section.getSize();
548       uint64_t Alignment64 = Section.getAlignment();
549       unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
550       bool IsCode = Section.isText();
551       bool IsReadOnly = isReadOnlyData(Section);
552 
553       Expected<StringRef> NameOrErr = Section.getName();
554       if (!NameOrErr)
555         return NameOrErr.takeError();
556       StringRef Name = *NameOrErr;
557 
558       uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
559 
560       uint64_t PaddingSize = 0;
561       if (Name == ".eh_frame")
562         PaddingSize += 4;
563       if (StubBufSize != 0)
564         PaddingSize += getStubAlignment() - 1;
565 
566       uint64_t SectionSize = DataSize + PaddingSize + StubBufSize;
567 
568       // The .eh_frame section (at least on Linux) needs an extra four bytes
569       // padded
570       // with zeroes added at the end.  For MachO objects, this section has a
571       // slightly different name, so this won't have any effect for MachO
572       // objects.
573       if (Name == ".eh_frame")
574         SectionSize += 4;
575 
576       if (!SectionSize)
577         SectionSize = 1;
578 
579       if (IsCode) {
580         CodeAlign = std::max(CodeAlign, Alignment);
581         CodeSectionSizes.push_back(SectionSize);
582       } else if (IsReadOnly) {
583         RODataAlign = std::max(RODataAlign, Alignment);
584         ROSectionSizes.push_back(SectionSize);
585       } else {
586         RWDataAlign = std::max(RWDataAlign, Alignment);
587         RWSectionSizes.push_back(SectionSize);
588       }
589     }
590   }
591 
592   // Compute Global Offset Table size. If it is not zero we
593   // also update alignment, which is equal to a size of a
594   // single GOT entry.
595   if (unsigned GotSize = computeGOTSize(Obj)) {
596     RWSectionSizes.push_back(GotSize);
597     RWDataAlign = std::max<uint32_t>(RWDataAlign, getGOTEntrySize());
598   }
599 
600   // Compute the size of all common symbols
601   uint64_t CommonSize = 0;
602   uint32_t CommonAlign = 1;
603   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
604        ++I) {
605     Expected<uint32_t> FlagsOrErr = I->getFlags();
606     if (!FlagsOrErr)
607       // TODO: Test this error.
608       return FlagsOrErr.takeError();
609     if (*FlagsOrErr & SymbolRef::SF_Common) {
610       // Add the common symbols to a list.  We'll allocate them all below.
611       uint64_t Size = I->getCommonSize();
612       uint32_t Align = I->getAlignment();
613       // If this is the first common symbol, use its alignment as the alignment
614       // for the common symbols section.
615       if (CommonSize == 0)
616         CommonAlign = Align;
617       CommonSize = alignTo(CommonSize, Align) + Size;
618     }
619   }
620   if (CommonSize != 0) {
621     RWSectionSizes.push_back(CommonSize);
622     RWDataAlign = std::max(RWDataAlign, CommonAlign);
623   }
624 
625   // Compute the required allocation space for each different type of sections
626   // (code, read-only data, read-write data) assuming that all sections are
627   // allocated with the max alignment. Note that we cannot compute with the
628   // individual alignments of the sections, because then the required size
629   // depends on the order, in which the sections are allocated.
630   CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign);
631   RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign);
632   RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign);
633 
634   return Error::success();
635 }
636 
637 // compute GOT size
638 unsigned RuntimeDyldImpl::computeGOTSize(const ObjectFile &Obj) {
639   size_t GotEntrySize = getGOTEntrySize();
640   if (!GotEntrySize)
641     return 0;
642 
643   size_t GotSize = 0;
644   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
645        SI != SE; ++SI) {
646 
647     for (const RelocationRef &Reloc : SI->relocations())
648       if (relocationNeedsGot(Reloc))
649         GotSize += GotEntrySize;
650   }
651 
652   return GotSize;
653 }
654 
655 // compute stub buffer size for the given section
656 unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
657                                                     const SectionRef &Section) {
658   unsigned StubSize = getMaxStubSize();
659   if (StubSize == 0) {
660     return 0;
661   }
662   // FIXME: this is an inefficient way to handle this. We should computed the
663   // necessary section allocation size in loadObject by walking all the sections
664   // once.
665   unsigned StubBufSize = 0;
666   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
667        SI != SE; ++SI) {
668 
669     Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection();
670     if (!RelSecOrErr)
671       report_fatal_error(toString(RelSecOrErr.takeError()));
672 
673     section_iterator RelSecI = *RelSecOrErr;
674     if (!(RelSecI == Section))
675       continue;
676 
677     for (const RelocationRef &Reloc : SI->relocations())
678       if (relocationNeedsStub(Reloc))
679         StubBufSize += StubSize;
680   }
681 
682   // Get section data size and alignment
683   uint64_t DataSize = Section.getSize();
684   uint64_t Alignment64 = Section.getAlignment();
685 
686   // Add stubbuf size alignment
687   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
688   unsigned StubAlignment = getStubAlignment();
689   unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
690   if (StubAlignment > EndAlignment)
691     StubBufSize += StubAlignment - EndAlignment;
692   return StubBufSize;
693 }
694 
695 uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
696                                              unsigned Size) const {
697   uint64_t Result = 0;
698   if (IsTargetLittleEndian) {
699     Src += Size - 1;
700     while (Size--)
701       Result = (Result << 8) | *Src--;
702   } else
703     while (Size--)
704       Result = (Result << 8) | *Src++;
705 
706   return Result;
707 }
708 
709 void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
710                                           unsigned Size) const {
711   if (IsTargetLittleEndian) {
712     while (Size--) {
713       *Dst++ = Value & 0xFF;
714       Value >>= 8;
715     }
716   } else {
717     Dst += Size - 1;
718     while (Size--) {
719       *Dst-- = Value & 0xFF;
720       Value >>= 8;
721     }
722   }
723 }
724 
725 Expected<JITSymbolFlags>
726 RuntimeDyldImpl::getJITSymbolFlags(const SymbolRef &SR) {
727   return JITSymbolFlags::fromObjectSymbol(SR);
728 }
729 
730 Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
731                                          CommonSymbolList &SymbolsToAllocate,
732                                          uint64_t CommonSize,
733                                          uint32_t CommonAlign) {
734   if (SymbolsToAllocate.empty())
735     return Error::success();
736 
737   // Allocate memory for the section
738   unsigned SectionID = Sections.size();
739   uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
740                                              "<common symbols>", false);
741   if (!Addr)
742     report_fatal_error("Unable to allocate memory for common symbols!");
743   uint64_t Offset = 0;
744   Sections.push_back(
745       SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0));
746   memset(Addr, 0, CommonSize);
747 
748   LLVM_DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
749                     << " new addr: " << format("%p", Addr)
750                     << " DataSize: " << CommonSize << "\n");
751 
752   // Assign the address of each symbol
753   for (auto &Sym : SymbolsToAllocate) {
754     uint32_t Alignment = Sym.getAlignment();
755     uint64_t Size = Sym.getCommonSize();
756     StringRef Name;
757     if (auto NameOrErr = Sym.getName())
758       Name = *NameOrErr;
759     else
760       return NameOrErr.takeError();
761     if (Alignment) {
762       // This symbol has an alignment requirement.
763       uint64_t AlignOffset =
764           offsetToAlignment((uint64_t)Addr, Align(Alignment));
765       Addr += AlignOffset;
766       Offset += AlignOffset;
767     }
768     auto JITSymFlags = getJITSymbolFlags(Sym);
769 
770     if (!JITSymFlags)
771       return JITSymFlags.takeError();
772 
773     LLVM_DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
774                       << format("%p", Addr) << "\n");
775     if (!Name.empty()) // Skip absolute symbol relocations.
776       GlobalSymbolTable[Name] =
777           SymbolTableEntry(SectionID, Offset, std::move(*JITSymFlags));
778     Offset += Size;
779     Addr += Size;
780   }
781 
782   return Error::success();
783 }
784 
785 Expected<unsigned>
786 RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
787                              const SectionRef &Section,
788                              bool IsCode) {
789   StringRef data;
790   uint64_t Alignment64 = Section.getAlignment();
791 
792   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
793   unsigned PaddingSize = 0;
794   unsigned StubBufSize = 0;
795   bool IsRequired = isRequiredForExecution(Section);
796   bool IsVirtual = Section.isVirtual();
797   bool IsZeroInit = isZeroInit(Section);
798   bool IsReadOnly = isReadOnlyData(Section);
799   uint64_t DataSize = Section.getSize();
800 
801   // An alignment of 0 (at least with ELF) is identical to an alignment of 1,
802   // while being more "polite".  Other formats do not support 0-aligned sections
803   // anyway, so we should guarantee that the alignment is always at least 1.
804   Alignment = std::max(1u, Alignment);
805 
806   Expected<StringRef> NameOrErr = Section.getName();
807   if (!NameOrErr)
808     return NameOrErr.takeError();
809   StringRef Name = *NameOrErr;
810 
811   StubBufSize = computeSectionStubBufSize(Obj, Section);
812 
813   // The .eh_frame section (at least on Linux) needs an extra four bytes padded
814   // with zeroes added at the end.  For MachO objects, this section has a
815   // slightly different name, so this won't have any effect for MachO objects.
816   if (Name == ".eh_frame")
817     PaddingSize = 4;
818 
819   uintptr_t Allocate;
820   unsigned SectionID = Sections.size();
821   uint8_t *Addr;
822   const char *pData = nullptr;
823 
824   // If this section contains any bits (i.e. isn't a virtual or bss section),
825   // grab a reference to them.
826   if (!IsVirtual && !IsZeroInit) {
827     // In either case, set the location of the unrelocated section in memory,
828     // since we still process relocations for it even if we're not applying them.
829     if (Expected<StringRef> E = Section.getContents())
830       data = *E;
831     else
832       return E.takeError();
833     pData = data.data();
834   }
835 
836   // If there are any stubs then the section alignment needs to be at least as
837   // high as stub alignment or padding calculations may by incorrect when the
838   // section is remapped.
839   if (StubBufSize != 0) {
840     Alignment = std::max(Alignment, getStubAlignment());
841     PaddingSize += getStubAlignment() - 1;
842   }
843 
844   // Some sections, such as debug info, don't need to be loaded for execution.
845   // Process those only if explicitly requested.
846   if (IsRequired || ProcessAllSections) {
847     Allocate = DataSize + PaddingSize + StubBufSize;
848     if (!Allocate)
849       Allocate = 1;
850     Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
851                                                Name)
852                   : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
853                                                Name, IsReadOnly);
854     if (!Addr)
855       report_fatal_error("Unable to allocate section memory!");
856 
857     // Zero-initialize or copy the data from the image
858     if (IsZeroInit || IsVirtual)
859       memset(Addr, 0, DataSize);
860     else
861       memcpy(Addr, pData, DataSize);
862 
863     // Fill in any extra bytes we allocated for padding
864     if (PaddingSize != 0) {
865       memset(Addr + DataSize, 0, PaddingSize);
866       // Update the DataSize variable to include padding.
867       DataSize += PaddingSize;
868 
869       // Align DataSize to stub alignment if we have any stubs (PaddingSize will
870       // have been increased above to account for this).
871       if (StubBufSize > 0)
872         DataSize &= -(uint64_t)getStubAlignment();
873     }
874 
875     LLVM_DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: "
876                       << Name << " obj addr: " << format("%p", pData)
877                       << " new addr: " << format("%p", Addr) << " DataSize: "
878                       << DataSize << " StubBufSize: " << StubBufSize
879                       << " Allocate: " << Allocate << "\n");
880   } else {
881     // Even if we didn't load the section, we need to record an entry for it
882     // to handle later processing (and by 'handle' I mean don't do anything
883     // with these sections).
884     Allocate = 0;
885     Addr = nullptr;
886     LLVM_DEBUG(
887         dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
888                << " obj addr: " << format("%p", data.data()) << " new addr: 0"
889                << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
890                << " Allocate: " << Allocate << "\n");
891   }
892 
893   Sections.push_back(
894       SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData));
895 
896   // Debug info sections are linked as if their load address was zero
897   if (!IsRequired)
898     Sections.back().setLoadAddress(0);
899 
900   return SectionID;
901 }
902 
903 Expected<unsigned>
904 RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
905                                    const SectionRef &Section,
906                                    bool IsCode,
907                                    ObjSectionToIDMap &LocalSections) {
908 
909   unsigned SectionID = 0;
910   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
911   if (i != LocalSections.end())
912     SectionID = i->second;
913   else {
914     if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
915       SectionID = *SectionIDOrErr;
916     else
917       return SectionIDOrErr.takeError();
918     LocalSections[Section] = SectionID;
919   }
920   return SectionID;
921 }
922 
923 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
924                                               unsigned SectionID) {
925   Relocations[SectionID].push_back(RE);
926 }
927 
928 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
929                                              StringRef SymbolName) {
930   // Relocation by symbol.  If the symbol is found in the global symbol table,
931   // create an appropriate section relocation.  Otherwise, add it to
932   // ExternalSymbolRelocations.
933   RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
934   if (Loc == GlobalSymbolTable.end()) {
935     ExternalSymbolRelocations[SymbolName].push_back(RE);
936   } else {
937     assert(!SymbolName.empty() &&
938            "Empty symbol should not be in GlobalSymbolTable");
939     // Copy the RE since we want to modify its addend.
940     RelocationEntry RECopy = RE;
941     const auto &SymInfo = Loc->second;
942     RECopy.Addend += SymInfo.getOffset();
943     Relocations[SymInfo.getSectionID()].push_back(RECopy);
944   }
945 }
946 
947 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
948                                              unsigned AbiVariant) {
949   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be ||
950       Arch == Triple::aarch64_32) {
951     // This stub has to be able to access the full address space,
952     // since symbol lookup won't necessarily find a handy, in-range,
953     // PLT stub for functions which could be anywhere.
954     // Stub can use ip0 (== x16) to calculate address
955     writeBytesUnaligned(0xd2e00010, Addr,    4); // movz ip0, #:abs_g3:<addr>
956     writeBytesUnaligned(0xf2c00010, Addr+4,  4); // movk ip0, #:abs_g2_nc:<addr>
957     writeBytesUnaligned(0xf2a00010, Addr+8,  4); // movk ip0, #:abs_g1_nc:<addr>
958     writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
959     writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
960 
961     return Addr;
962   } else if (Arch == Triple::arm || Arch == Triple::armeb) {
963     // TODO: There is only ARM far stub now. We should add the Thumb stub,
964     // and stubs for branches Thumb - ARM and ARM - Thumb.
965     writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc, [pc, #-4]
966     return Addr + 4;
967   } else if (IsMipsO32ABI || IsMipsN32ABI) {
968     // 0:   3c190000        lui     t9,%hi(addr).
969     // 4:   27390000        addiu   t9,t9,%lo(addr).
970     // 8:   03200008        jr      t9.
971     // c:   00000000        nop.
972     const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
973     const unsigned NopInstr = 0x0;
974     unsigned JrT9Instr = 0x03200008;
975     if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6 ||
976         (AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6)
977       JrT9Instr = 0x03200009;
978 
979     writeBytesUnaligned(LuiT9Instr, Addr, 4);
980     writeBytesUnaligned(AdduiT9Instr, Addr + 4, 4);
981     writeBytesUnaligned(JrT9Instr, Addr + 8, 4);
982     writeBytesUnaligned(NopInstr, Addr + 12, 4);
983     return Addr;
984   } else if (IsMipsN64ABI) {
985     // 0:   3c190000        lui     t9,%highest(addr).
986     // 4:   67390000        daddiu  t9,t9,%higher(addr).
987     // 8:   0019CC38        dsll    t9,t9,16.
988     // c:   67390000        daddiu  t9,t9,%hi(addr).
989     // 10:  0019CC38        dsll    t9,t9,16.
990     // 14:  67390000        daddiu  t9,t9,%lo(addr).
991     // 18:  03200008        jr      t9.
992     // 1c:  00000000        nop.
993     const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000,
994                    DsllT9Instr = 0x19CC38;
995     const unsigned NopInstr = 0x0;
996     unsigned JrT9Instr = 0x03200008;
997     if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6)
998       JrT9Instr = 0x03200009;
999 
1000     writeBytesUnaligned(LuiT9Instr, Addr, 4);
1001     writeBytesUnaligned(DaddiuT9Instr, Addr + 4, 4);
1002     writeBytesUnaligned(DsllT9Instr, Addr + 8, 4);
1003     writeBytesUnaligned(DaddiuT9Instr, Addr + 12, 4);
1004     writeBytesUnaligned(DsllT9Instr, Addr + 16, 4);
1005     writeBytesUnaligned(DaddiuT9Instr, Addr + 20, 4);
1006     writeBytesUnaligned(JrT9Instr, Addr + 24, 4);
1007     writeBytesUnaligned(NopInstr, Addr + 28, 4);
1008     return Addr;
1009   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
1010     // Depending on which version of the ELF ABI is in use, we need to
1011     // generate one of two variants of the stub.  They both start with
1012     // the same sequence to load the target address into r12.
1013     writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
1014     writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
1015     writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
1016     writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
1017     writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
1018     if (AbiVariant == 2) {
1019       // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
1020       // The address is already in r12 as required by the ABI.  Branch to it.
1021       writeInt32BE(Addr+20, 0xF8410018); // std   r2,  24(r1)
1022       writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
1023       writeInt32BE(Addr+28, 0x4E800420); // bctr
1024     } else {
1025       // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
1026       // Load the function address on r11 and sets it to control register. Also
1027       // loads the function TOC in r2 and environment pointer to r11.
1028       writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
1029       writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
1030       writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
1031       writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
1032       writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
1033       writeInt32BE(Addr+40, 0x4E800420); // bctr
1034     }
1035     return Addr;
1036   } else if (Arch == Triple::systemz) {
1037     writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
1038     writeInt16BE(Addr+2,  0x0000);
1039     writeInt16BE(Addr+4,  0x0004);
1040     writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
1041     // 8-byte address stored at Addr + 8
1042     return Addr;
1043   } else if (Arch == Triple::x86_64) {
1044     *Addr      = 0xFF; // jmp
1045     *(Addr+1)  = 0x25; // rip
1046     // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
1047   } else if (Arch == Triple::x86) {
1048     *Addr      = 0xE9; // 32-bit pc-relative jump.
1049   }
1050   return Addr;
1051 }
1052 
1053 // Assign an address to a symbol name and resolve all the relocations
1054 // associated with it.
1055 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
1056                                              uint64_t Addr) {
1057   // The address to use for relocation resolution is not
1058   // the address of the local section buffer. We must be doing
1059   // a remote execution environment of some sort. Relocations can't
1060   // be applied until all the sections have been moved.  The client must
1061   // trigger this with a call to MCJIT::finalize() or
1062   // RuntimeDyld::resolveRelocations().
1063   //
1064   // Addr is a uint64_t because we can't assume the pointer width
1065   // of the target is the same as that of the host. Just use a generic
1066   // "big enough" type.
1067   LLVM_DEBUG(
1068       dbgs() << "Reassigning address for section " << SectionID << " ("
1069              << Sections[SectionID].getName() << "): "
1070              << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress())
1071              << " -> " << format("0x%016" PRIx64, Addr) << "\n");
1072   Sections[SectionID].setLoadAddress(Addr);
1073 }
1074 
1075 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
1076                                             uint64_t Value) {
1077   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
1078     const RelocationEntry &RE = Relocs[i];
1079     // Ignore relocations for sections that were not loaded
1080     if (Sections[RE.SectionID].getAddress() == nullptr)
1081       continue;
1082     resolveRelocation(RE, Value);
1083   }
1084 }
1085 
1086 void RuntimeDyldImpl::applyExternalSymbolRelocations(
1087     const StringMap<JITEvaluatedSymbol> ExternalSymbolMap) {
1088   while (!ExternalSymbolRelocations.empty()) {
1089 
1090     StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
1091 
1092     StringRef Name = i->first();
1093     if (Name.size() == 0) {
1094       // This is an absolute symbol, use an address of zero.
1095       LLVM_DEBUG(dbgs() << "Resolving absolute relocations."
1096                         << "\n");
1097       RelocationList &Relocs = i->second;
1098       resolveRelocationList(Relocs, 0);
1099     } else {
1100       uint64_t Addr = 0;
1101       JITSymbolFlags Flags;
1102       RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
1103       if (Loc == GlobalSymbolTable.end()) {
1104         auto RRI = ExternalSymbolMap.find(Name);
1105         assert(RRI != ExternalSymbolMap.end() && "No result for symbol");
1106         Addr = RRI->second.getAddress();
1107         Flags = RRI->second.getFlags();
1108         // The call to getSymbolAddress may have caused additional modules to
1109         // be loaded, which may have added new entries to the
1110         // ExternalSymbolRelocations map.  Consquently, we need to update our
1111         // iterator.  This is also why retrieval of the relocation list
1112         // associated with this symbol is deferred until below this point.
1113         // New entries may have been added to the relocation list.
1114         i = ExternalSymbolRelocations.find(Name);
1115       } else {
1116         // We found the symbol in our global table.  It was probably in a
1117         // Module that we loaded previously.
1118         const auto &SymInfo = Loc->second;
1119         Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
1120                SymInfo.getOffset();
1121         Flags = SymInfo.getFlags();
1122       }
1123 
1124       // FIXME: Implement error handling that doesn't kill the host program!
1125       if (!Addr)
1126         report_fatal_error("Program used external function '" + Name +
1127                            "' which could not be resolved!");
1128 
1129       // If Resolver returned UINT64_MAX, the client wants to handle this symbol
1130       // manually and we shouldn't resolve its relocations.
1131       if (Addr != UINT64_MAX) {
1132 
1133         // Tweak the address based on the symbol flags if necessary.
1134         // For example, this is used by RuntimeDyldMachOARM to toggle the low bit
1135         // if the target symbol is Thumb.
1136         Addr = modifyAddressBasedOnFlags(Addr, Flags);
1137 
1138         LLVM_DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
1139                           << format("0x%lx", Addr) << "\n");
1140         // This list may have been updated when we called getSymbolAddress, so
1141         // don't change this code to get the list earlier.
1142         RelocationList &Relocs = i->second;
1143         resolveRelocationList(Relocs, Addr);
1144       }
1145     }
1146 
1147     ExternalSymbolRelocations.erase(i);
1148   }
1149 }
1150 
1151 Error RuntimeDyldImpl::resolveExternalSymbols() {
1152   StringMap<JITEvaluatedSymbol> ExternalSymbolMap;
1153 
1154   // Resolution can trigger emission of more symbols, so iterate until
1155   // we've resolved *everything*.
1156   {
1157     JITSymbolResolver::LookupSet ResolvedSymbols;
1158 
1159     while (true) {
1160       JITSymbolResolver::LookupSet NewSymbols;
1161 
1162       for (auto &RelocKV : ExternalSymbolRelocations) {
1163         StringRef Name = RelocKV.first();
1164         if (!Name.empty() && !GlobalSymbolTable.count(Name) &&
1165             !ResolvedSymbols.count(Name))
1166           NewSymbols.insert(Name);
1167       }
1168 
1169       if (NewSymbols.empty())
1170         break;
1171 
1172 #ifdef _MSC_VER
1173       using ExpectedLookupResult =
1174           MSVCPExpected<JITSymbolResolver::LookupResult>;
1175 #else
1176       using ExpectedLookupResult = Expected<JITSymbolResolver::LookupResult>;
1177 #endif
1178 
1179       auto NewSymbolsP = std::make_shared<std::promise<ExpectedLookupResult>>();
1180       auto NewSymbolsF = NewSymbolsP->get_future();
1181       Resolver.lookup(NewSymbols,
1182                       [=](Expected<JITSymbolResolver::LookupResult> Result) {
1183                         NewSymbolsP->set_value(std::move(Result));
1184                       });
1185 
1186       auto NewResolverResults = NewSymbolsF.get();
1187 
1188       if (!NewResolverResults)
1189         return NewResolverResults.takeError();
1190 
1191       assert(NewResolverResults->size() == NewSymbols.size() &&
1192              "Should have errored on unresolved symbols");
1193 
1194       for (auto &RRKV : *NewResolverResults) {
1195         assert(!ResolvedSymbols.count(RRKV.first) && "Redundant resolution?");
1196         ExternalSymbolMap.insert(RRKV);
1197         ResolvedSymbols.insert(RRKV.first);
1198       }
1199     }
1200   }
1201 
1202   applyExternalSymbolRelocations(ExternalSymbolMap);
1203 
1204   return Error::success();
1205 }
1206 
1207 void RuntimeDyldImpl::finalizeAsync(
1208     std::unique_ptr<RuntimeDyldImpl> This,
1209     unique_function<void(object::OwningBinary<object::ObjectFile>,
1210                          std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)>
1211         OnEmitted,
1212     object::OwningBinary<object::ObjectFile> O,
1213     std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info) {
1214 
1215   auto SharedThis = std::shared_ptr<RuntimeDyldImpl>(std::move(This));
1216   auto PostResolveContinuation =
1217       [SharedThis, OnEmitted = std::move(OnEmitted), O = std::move(O),
1218        Info = std::move(Info)](
1219           Expected<JITSymbolResolver::LookupResult> Result) mutable {
1220         if (!Result) {
1221           OnEmitted(std::move(O), std::move(Info), Result.takeError());
1222           return;
1223         }
1224 
1225         /// Copy the result into a StringMap, where the keys are held by value.
1226         StringMap<JITEvaluatedSymbol> Resolved;
1227         for (auto &KV : *Result)
1228           Resolved[KV.first] = KV.second;
1229 
1230         SharedThis->applyExternalSymbolRelocations(Resolved);
1231         SharedThis->resolveLocalRelocations();
1232         SharedThis->registerEHFrames();
1233         std::string ErrMsg;
1234         if (SharedThis->MemMgr.finalizeMemory(&ErrMsg))
1235           OnEmitted(std::move(O), std::move(Info),
1236                     make_error<StringError>(std::move(ErrMsg),
1237                                             inconvertibleErrorCode()));
1238         else
1239           OnEmitted(std::move(O), std::move(Info), Error::success());
1240       };
1241 
1242   JITSymbolResolver::LookupSet Symbols;
1243 
1244   for (auto &RelocKV : SharedThis->ExternalSymbolRelocations) {
1245     StringRef Name = RelocKV.first();
1246     if (Name.empty()) // Skip absolute symbol relocations.
1247       continue;
1248     assert(!SharedThis->GlobalSymbolTable.count(Name) &&
1249            "Name already processed. RuntimeDyld instances can not be re-used "
1250            "when finalizing with finalizeAsync.");
1251     Symbols.insert(Name);
1252   }
1253 
1254   if (!Symbols.empty()) {
1255     SharedThis->Resolver.lookup(Symbols, std::move(PostResolveContinuation));
1256   } else
1257     PostResolveContinuation(std::map<StringRef, JITEvaluatedSymbol>());
1258 }
1259 
1260 //===----------------------------------------------------------------------===//
1261 // RuntimeDyld class implementation
1262 
1263 uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
1264                                           const object::SectionRef &Sec) const {
1265 
1266   auto I = ObjSecToIDMap.find(Sec);
1267   if (I != ObjSecToIDMap.end())
1268     return RTDyld.Sections[I->second].getLoadAddress();
1269 
1270   return 0;
1271 }
1272 
1273 void RuntimeDyld::MemoryManager::anchor() {}
1274 void JITSymbolResolver::anchor() {}
1275 void LegacyJITSymbolResolver::anchor() {}
1276 
1277 RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
1278                          JITSymbolResolver &Resolver)
1279     : MemMgr(MemMgr), Resolver(Resolver) {
1280   // FIXME: There's a potential issue lurking here if a single instance of
1281   // RuntimeDyld is used to load multiple objects.  The current implementation
1282   // associates a single memory manager with a RuntimeDyld instance.  Even
1283   // though the public class spawns a new 'impl' instance for each load,
1284   // they share a single memory manager.  This can become a problem when page
1285   // permissions are applied.
1286   Dyld = nullptr;
1287   ProcessAllSections = false;
1288 }
1289 
1290 RuntimeDyld::~RuntimeDyld() {}
1291 
1292 static std::unique_ptr<RuntimeDyldCOFF>
1293 createRuntimeDyldCOFF(
1294                      Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1295                      JITSymbolResolver &Resolver, bool ProcessAllSections,
1296                      RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) {
1297   std::unique_ptr<RuntimeDyldCOFF> Dyld =
1298     RuntimeDyldCOFF::create(Arch, MM, Resolver);
1299   Dyld->setProcessAllSections(ProcessAllSections);
1300   Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1301   return Dyld;
1302 }
1303 
1304 static std::unique_ptr<RuntimeDyldELF>
1305 createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1306                      JITSymbolResolver &Resolver, bool ProcessAllSections,
1307                      RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) {
1308   std::unique_ptr<RuntimeDyldELF> Dyld =
1309       RuntimeDyldELF::create(Arch, MM, Resolver);
1310   Dyld->setProcessAllSections(ProcessAllSections);
1311   Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1312   return Dyld;
1313 }
1314 
1315 static std::unique_ptr<RuntimeDyldMachO>
1316 createRuntimeDyldMachO(
1317                      Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1318                      JITSymbolResolver &Resolver,
1319                      bool ProcessAllSections,
1320                      RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) {
1321   std::unique_ptr<RuntimeDyldMachO> Dyld =
1322     RuntimeDyldMachO::create(Arch, MM, Resolver);
1323   Dyld->setProcessAllSections(ProcessAllSections);
1324   Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted));
1325   return Dyld;
1326 }
1327 
1328 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
1329 RuntimeDyld::loadObject(const ObjectFile &Obj) {
1330   if (!Dyld) {
1331     if (Obj.isELF())
1332       Dyld =
1333           createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()),
1334                                MemMgr, Resolver, ProcessAllSections,
1335                                std::move(NotifyStubEmitted));
1336     else if (Obj.isMachO())
1337       Dyld = createRuntimeDyldMachO(
1338                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1339                ProcessAllSections, std::move(NotifyStubEmitted));
1340     else if (Obj.isCOFF())
1341       Dyld = createRuntimeDyldCOFF(
1342                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1343                ProcessAllSections, std::move(NotifyStubEmitted));
1344     else
1345       report_fatal_error("Incompatible object format!");
1346   }
1347 
1348   if (!Dyld->isCompatibleFile(Obj))
1349     report_fatal_error("Incompatible object format!");
1350 
1351   auto LoadedObjInfo = Dyld->loadObject(Obj);
1352   MemMgr.notifyObjectLoaded(*this, Obj);
1353   return LoadedObjInfo;
1354 }
1355 
1356 void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const {
1357   if (!Dyld)
1358     return nullptr;
1359   return Dyld->getSymbolLocalAddress(Name);
1360 }
1361 
1362 unsigned RuntimeDyld::getSymbolSectionID(StringRef Name) const {
1363   assert(Dyld && "No RuntimeDyld instance attached");
1364   return Dyld->getSymbolSectionID(Name);
1365 }
1366 
1367 JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const {
1368   if (!Dyld)
1369     return nullptr;
1370   return Dyld->getSymbol(Name);
1371 }
1372 
1373 std::map<StringRef, JITEvaluatedSymbol> RuntimeDyld::getSymbolTable() const {
1374   if (!Dyld)
1375     return std::map<StringRef, JITEvaluatedSymbol>();
1376   return Dyld->getSymbolTable();
1377 }
1378 
1379 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
1380 
1381 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
1382   Dyld->reassignSectionAddress(SectionID, Addr);
1383 }
1384 
1385 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
1386                                     uint64_t TargetAddress) {
1387   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1388 }
1389 
1390 bool RuntimeDyld::hasError() { return Dyld->hasError(); }
1391 
1392 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
1393 
1394 void RuntimeDyld::finalizeWithMemoryManagerLocking() {
1395   bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1396   MemMgr.FinalizationLocked = true;
1397   resolveRelocations();
1398   registerEHFrames();
1399   if (!MemoryFinalizationLocked) {
1400     MemMgr.finalizeMemory();
1401     MemMgr.FinalizationLocked = false;
1402   }
1403 }
1404 
1405 StringRef RuntimeDyld::getSectionContent(unsigned SectionID) const {
1406   assert(Dyld && "No Dyld instance attached");
1407   return Dyld->getSectionContent(SectionID);
1408 }
1409 
1410 uint64_t RuntimeDyld::getSectionLoadAddress(unsigned SectionID) const {
1411   assert(Dyld && "No Dyld instance attached");
1412   return Dyld->getSectionLoadAddress(SectionID);
1413 }
1414 
1415 void RuntimeDyld::registerEHFrames() {
1416   if (Dyld)
1417     Dyld->registerEHFrames();
1418 }
1419 
1420 void RuntimeDyld::deregisterEHFrames() {
1421   if (Dyld)
1422     Dyld->deregisterEHFrames();
1423 }
1424 // FIXME: Kill this with fire once we have a new JIT linker: this is only here
1425 // so that we can re-use RuntimeDyld's implementation without twisting the
1426 // interface any further for ORC's purposes.
1427 void jitLinkForORC(
1428     object::OwningBinary<object::ObjectFile> O,
1429     RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver,
1430     bool ProcessAllSections,
1431     unique_function<Error(const object::ObjectFile &Obj,
1432                           RuntimeDyld::LoadedObjectInfo &LoadedObj,
1433                           std::map<StringRef, JITEvaluatedSymbol>)>
1434         OnLoaded,
1435     unique_function<void(object::OwningBinary<object::ObjectFile>,
1436                          std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)>
1437         OnEmitted) {
1438 
1439   RuntimeDyld RTDyld(MemMgr, Resolver);
1440   RTDyld.setProcessAllSections(ProcessAllSections);
1441 
1442   auto Info = RTDyld.loadObject(*O.getBinary());
1443 
1444   if (RTDyld.hasError()) {
1445     OnEmitted(std::move(O), std::move(Info),
1446               make_error<StringError>(RTDyld.getErrorString(),
1447                                       inconvertibleErrorCode()));
1448     return;
1449   }
1450 
1451   if (auto Err = OnLoaded(*O.getBinary(), *Info, RTDyld.getSymbolTable()))
1452     OnEmitted(std::move(O), std::move(Info), std::move(Err));
1453 
1454   RuntimeDyldImpl::finalizeAsync(std::move(RTDyld.Dyld), std::move(OnEmitted),
1455                                  std::move(O), std::move(Info));
1456 }
1457 
1458 } // end namespace llvm
1459