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