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