1 //===-- RuntimeDyld.cpp - Run-time dynamic linker for MC-JIT ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implementation of the MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/ExecutionEngine/RuntimeDyld.h"
15 #include "RuntimeDyldCheckerImpl.h"
16 #include "RuntimeDyldCOFF.h"
17 #include "RuntimeDyldELF.h"
18 #include "RuntimeDyldImpl.h"
19 #include "RuntimeDyldMachO.h"
20 #include "llvm/Object/ELFObjectFile.h"
21 #include "llvm/Object/COFF.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/MutexGuard.h"
25 
26 using namespace llvm;
27 using namespace llvm::object;
28 
29 #define DEBUG_TYPE "dyld"
30 
31 namespace {
32 
33 enum RuntimeDyldErrorCode {
34   GenericRTDyldError = 1
35 };
36 
37 // FIXME: This class is only here to support the transition to llvm::Error. It
38 // will be removed once this transition is complete. Clients should prefer to
39 // deal with the Error value directly, rather than converting to error_code.
40 class RuntimeDyldErrorCategory : public std::error_category {
41 public:
42   const char *name() const LLVM_NOEXCEPT override { return "runtimedyld"; }
43 
44   std::string message(int Condition) const override {
45     switch (static_cast<RuntimeDyldErrorCode>(Condition)) {
46       case GenericRTDyldError: return "Generic RuntimeDyld error";
47     }
48     llvm_unreachable("Unrecognized RuntimeDyldErrorCode");
49   }
50 };
51 
52 static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory;
53 
54 }
55 
56 char RuntimeDyldError::ID = 0;
57 
58 void RuntimeDyldError::log(raw_ostream &OS) const {
59   OS << ErrMsg << "\n";
60 }
61 
62 std::error_code RuntimeDyldError::convertToErrorCode() const {
63   return std::error_code(GenericRTDyldError, *RTDyldErrorCategory);
64 }
65 
66 // Empty out-of-line virtual destructor as the key function.
67 RuntimeDyldImpl::~RuntimeDyldImpl() {}
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 
78 #ifndef NDEBUG
79 static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
80   dbgs() << "----- Contents of section " << S.getName() << " " << State
81          << " -----";
82 
83   if (S.getAddress() == nullptr) {
84     dbgs() << "\n          <section not emitted>\n";
85     return;
86   }
87 
88   const unsigned ColsPerRow = 16;
89 
90   uint8_t *DataAddr = S.getAddress();
91   uint64_t LoadAddr = S.getLoadAddress();
92 
93   unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
94   unsigned BytesRemaining = S.getSize();
95 
96   if (StartPadding) {
97     dbgs() << "\n" << format("0x%016" PRIx64,
98                              LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
99     while (StartPadding--)
100       dbgs() << "   ";
101   }
102 
103   while (BytesRemaining > 0) {
104     if ((LoadAddr & (ColsPerRow - 1)) == 0)
105       dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
106 
107     dbgs() << " " << format("%02x", *DataAddr);
108 
109     ++DataAddr;
110     ++LoadAddr;
111     --BytesRemaining;
112   }
113 
114   dbgs() << "\n";
115 }
116 #endif
117 
118 // Resolve the relocations for all symbols we currently know about.
119 void RuntimeDyldImpl::resolveRelocations() {
120   MutexGuard locked(lock);
121 
122   // Print out the sections prior to relocation.
123   DEBUG(
124     for (int i = 0, e = Sections.size(); i != e; ++i)
125       dumpSectionMemory(Sections[i], "before relocations");
126   );
127 
128   // First, resolve relocations associated with external symbols.
129   resolveExternalSymbols();
130 
131   // Iterate over all outstanding relocations
132   for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
133     // The Section here (Sections[i]) refers to the section in which the
134     // symbol for the relocation is located.  The SectionID in the relocation
135     // entry provides the section to which the relocation will be applied.
136     int Idx = it->first;
137     uint64_t Addr = Sections[Idx].getLoadAddress();
138     DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
139                  << format("%p", (uintptr_t)Addr) << "\n");
140     resolveRelocationList(it->second, Addr);
141   }
142   Relocations.clear();
143 
144   // Print out sections after relocation.
145   DEBUG(
146     for (int i = 0, e = Sections.size(); i != e; ++i)
147       dumpSectionMemory(Sections[i], "after relocations");
148   );
149 
150 }
151 
152 void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
153                                         uint64_t TargetAddress) {
154   MutexGuard locked(lock);
155   for (unsigned i = 0, e = Sections.size(); i != e; ++i) {
156     if (Sections[i].getAddress() == LocalAddress) {
157       reassignSectionAddress(i, TargetAddress);
158       return;
159     }
160   }
161   llvm_unreachable("Attempting to remap address of unknown section!");
162 }
163 
164 static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
165                        uint64_t &Result) {
166   Expected<uint64_t> AddressOrErr = Sym.getAddress();
167   if (!AddressOrErr)
168     return AddressOrErr.takeError();
169   Result = *AddressOrErr - Sec.getAddress();
170   return Error::success();
171 }
172 
173 Expected<RuntimeDyldImpl::ObjSectionToIDMap>
174 RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) {
175   MutexGuard locked(lock);
176 
177   // Save information about our target
178   Arch = (Triple::ArchType)Obj.getArch();
179   IsTargetLittleEndian = Obj.isLittleEndian();
180   setMipsABI(Obj);
181 
182   // Compute the memory size required to load all sections to be loaded
183   // and pass this information to the memory manager
184   if (MemMgr.needsToReserveAllocationSpace()) {
185     uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0;
186     uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1;
187     if (auto Err = computeTotalAllocSize(Obj,
188                                          CodeSize, CodeAlign,
189                                          RODataSize, RODataAlign,
190                                          RWDataSize, RWDataAlign))
191       return std::move(Err);
192     MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
193                                   RWDataSize, RWDataAlign);
194   }
195 
196   // Used sections from the object file
197   ObjSectionToIDMap LocalSections;
198 
199   // Common symbols requiring allocation, with their sizes and alignments
200   CommonSymbolList CommonSymbols;
201 
202   // Parse symbols
203   DEBUG(dbgs() << "Parse symbols:\n");
204   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
205        ++I) {
206     uint32_t Flags = I->getFlags();
207 
208     if (Flags & SymbolRef::SF_Common)
209       CommonSymbols.push_back(*I);
210     else {
211 
212       // Get the symbol type.
213       object::SymbolRef::Type SymType;
214       if (auto SymTypeOrErr = I->getType())
215         SymType =  *SymTypeOrErr;
216       else
217         return SymTypeOrErr.takeError();
218 
219       // Get symbol name.
220       StringRef Name;
221       if (auto NameOrErr = I->getName())
222         Name = *NameOrErr;
223       else
224         return NameOrErr.takeError();
225 
226       // Compute JIT symbol flags.
227       JITSymbolFlags JITSymFlags = JITSymbolFlags::fromObjectSymbol(*I);
228 
229       // If this is a weak definition, check to see if there's a strong one.
230       // If there is, skip this symbol (we won't be providing it: the strong
231       // definition will). If there's no strong definition, make this definition
232       // strong.
233       if (JITSymFlags.isWeak()) {
234         // First check whether there's already a definition in this instance.
235         // FIXME: Override existing weak definitions with strong ones.
236         if (GlobalSymbolTable.count(Name))
237           continue;
238         // Then check the symbol resolver to see if there's a definition
239         // elsewhere in this logical dylib.
240         if (auto Sym = Resolver.findSymbolInLogicalDylib(Name))
241           if (Sym.getFlags().isStrongDefinition())
242             continue;
243         // else
244         JITSymFlags &= ~JITSymbolFlags::Weak;
245       }
246 
247       if (Flags & SymbolRef::SF_Absolute &&
248           SymType != object::SymbolRef::ST_File) {
249         uint64_t Addr = 0;
250         if (auto AddrOrErr = I->getAddress())
251           Addr = *AddrOrErr;
252         else
253           return AddrOrErr.takeError();
254 
255         unsigned SectionID = AbsoluteSymbolSection;
256 
257         DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name
258                      << " SID: " << SectionID << " Offset: "
259                      << format("%p", (uintptr_t)Addr)
260                      << " flags: " << Flags << "\n");
261         GlobalSymbolTable[Name] =
262           SymbolTableEntry(SectionID, Addr, JITSymFlags);
263       } else if (SymType == object::SymbolRef::ST_Function ||
264                  SymType == object::SymbolRef::ST_Data ||
265                  SymType == object::SymbolRef::ST_Unknown ||
266                  SymType == object::SymbolRef::ST_Other) {
267 
268         section_iterator SI = Obj.section_end();
269         if (auto SIOrErr = I->getSection())
270           SI = *SIOrErr;
271         else
272           return SIOrErr.takeError();
273 
274         if (SI == Obj.section_end())
275           continue;
276 
277         // Get symbol offset.
278         uint64_t SectOffset;
279         if (auto Err = getOffset(*I, *SI, SectOffset))
280           return std::move(Err);
281 
282         bool IsCode = SI->isText();
283         unsigned SectionID;
284         if (auto SectionIDOrErr = findOrEmitSection(Obj, *SI, IsCode,
285                                                     LocalSections))
286           SectionID = *SectionIDOrErr;
287         else
288           return SectionIDOrErr.takeError();
289 
290         DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
291                      << " SID: " << SectionID << " Offset: "
292                      << format("%p", (uintptr_t)SectOffset)
293                      << " flags: " << Flags << "\n");
294         GlobalSymbolTable[Name] =
295           SymbolTableEntry(SectionID, SectOffset, JITSymFlags);
296       }
297     }
298   }
299 
300   // Allocate common symbols
301   if (auto Err = emitCommonSymbols(Obj, CommonSymbols))
302     return std::move(Err);
303 
304   // Parse and process relocations
305   DEBUG(dbgs() << "Parse relocations:\n");
306   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
307        SI != SE; ++SI) {
308     StubMap Stubs;
309     section_iterator RelocatedSection = SI->getRelocatedSection();
310 
311     if (RelocatedSection == SE)
312       continue;
313 
314     relocation_iterator I = SI->relocation_begin();
315     relocation_iterator E = SI->relocation_end();
316 
317     if (I == E && !ProcessAllSections)
318       continue;
319 
320     bool IsCode = RelocatedSection->isText();
321     unsigned SectionID = 0;
322     if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
323                                                 LocalSections))
324       SectionID = *SectionIDOrErr;
325     else
326       return SectionIDOrErr.takeError();
327 
328     DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
329 
330     for (; I != E;)
331       if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs))
332         I = *IOrErr;
333       else
334         return IOrErr.takeError();
335 
336     // If there is an attached checker, notify it about the stubs for this
337     // section so that they can be verified.
338     if (Checker)
339       Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs);
340   }
341 
342   // Give the subclasses a chance to tie-up any loose ends.
343   if (auto Err = finalizeLoad(Obj, LocalSections))
344     return std::move(Err);
345 
346 //   for (auto E : LocalSections)
347 //     llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
348 
349   return LocalSections;
350 }
351 
352 // A helper method for computeTotalAllocSize.
353 // Computes the memory size required to allocate sections with the given sizes,
354 // assuming that all sections are allocated with the given alignment
355 static uint64_t
356 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
357                                  uint64_t Alignment) {
358   uint64_t TotalSize = 0;
359   for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
360     uint64_t AlignedSize =
361         (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
362     TotalSize += AlignedSize;
363   }
364   return TotalSize;
365 }
366 
367 static bool isRequiredForExecution(const SectionRef Section) {
368   const ObjectFile *Obj = Section.getObject();
369   if (isa<object::ELFObjectFileBase>(Obj))
370     return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
371   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
372     const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
373     // Avoid loading zero-sized COFF sections.
374     // In PE files, VirtualSize gives the section size, and SizeOfRawData
375     // may be zero for sections with content. In Obj files, SizeOfRawData
376     // gives the section size, and VirtualSize is always zero. Hence
377     // the need to check for both cases below.
378     bool HasContent =
379         (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0);
380     bool IsDiscardable =
381         CoffSection->Characteristics &
382         (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO);
383     return HasContent && !IsDiscardable;
384   }
385 
386   assert(isa<MachOObjectFile>(Obj));
387   return true;
388 }
389 
390 static bool isReadOnlyData(const SectionRef Section) {
391   const ObjectFile *Obj = Section.getObject();
392   if (isa<object::ELFObjectFileBase>(Obj))
393     return !(ELFSectionRef(Section).getFlags() &
394              (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
395   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
396     return ((COFFObj->getCOFFSection(Section)->Characteristics &
397              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
398              | COFF::IMAGE_SCN_MEM_READ
399              | COFF::IMAGE_SCN_MEM_WRITE))
400              ==
401              (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA
402              | COFF::IMAGE_SCN_MEM_READ));
403 
404   assert(isa<MachOObjectFile>(Obj));
405   return false;
406 }
407 
408 static bool isZeroInit(const SectionRef Section) {
409   const ObjectFile *Obj = Section.getObject();
410   if (isa<object::ELFObjectFileBase>(Obj))
411     return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS;
412   if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
413     return COFFObj->getCOFFSection(Section)->Characteristics &
414             COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA;
415 
416   auto *MachO = cast<MachOObjectFile>(Obj);
417   unsigned SectionType = MachO->getSectionType(Section);
418   return SectionType == MachO::S_ZEROFILL ||
419          SectionType == MachO::S_GB_ZEROFILL;
420 }
421 
422 // Compute an upper bound of the memory size that is required to load all
423 // sections
424 Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
425                                              uint64_t &CodeSize,
426                                              uint32_t &CodeAlign,
427                                              uint64_t &RODataSize,
428                                              uint32_t &RODataAlign,
429                                              uint64_t &RWDataSize,
430                                              uint32_t &RWDataAlign) {
431   // Compute the size of all sections required for execution
432   std::vector<uint64_t> CodeSectionSizes;
433   std::vector<uint64_t> ROSectionSizes;
434   std::vector<uint64_t> RWSectionSizes;
435 
436   // Collect sizes of all sections to be loaded;
437   // also determine the max alignment of all sections
438   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
439        SI != SE; ++SI) {
440     const SectionRef &Section = *SI;
441 
442     bool IsRequired = isRequiredForExecution(Section);
443 
444     // Consider only the sections that are required to be loaded for execution
445     if (IsRequired) {
446       uint64_t DataSize = Section.getSize();
447       uint64_t Alignment64 = Section.getAlignment();
448       unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
449       bool IsCode = Section.isText();
450       bool IsReadOnly = isReadOnlyData(Section);
451 
452       StringRef Name;
453       if (auto EC = Section.getName(Name))
454         return errorCodeToError(EC);
455 
456       uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
457       uint64_t SectionSize = DataSize + StubBufSize;
458 
459       // The .eh_frame section (at least on Linux) needs an extra four bytes
460       // padded
461       // with zeroes added at the end.  For MachO objects, this section has a
462       // slightly different name, so this won't have any effect for MachO
463       // objects.
464       if (Name == ".eh_frame")
465         SectionSize += 4;
466 
467       if (!SectionSize)
468         SectionSize = 1;
469 
470       if (IsCode) {
471         CodeAlign = std::max(CodeAlign, Alignment);
472         CodeSectionSizes.push_back(SectionSize);
473       } else if (IsReadOnly) {
474         RODataAlign = std::max(RODataAlign, Alignment);
475         ROSectionSizes.push_back(SectionSize);
476       } else {
477         RWDataAlign = std::max(RWDataAlign, Alignment);
478         RWSectionSizes.push_back(SectionSize);
479       }
480     }
481   }
482 
483   // Compute the size of all common symbols
484   uint64_t CommonSize = 0;
485   uint32_t CommonAlign = 1;
486   for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
487        ++I) {
488     uint32_t Flags = I->getFlags();
489     if (Flags & SymbolRef::SF_Common) {
490       // Add the common symbols to a list.  We'll allocate them all below.
491       uint64_t Size = I->getCommonSize();
492       uint32_t Align = I->getAlignment();
493       // If this is the first common symbol, use its alignment as the alignment
494       // for the common symbols section.
495       if (CommonSize == 0)
496         CommonAlign = Align;
497       CommonSize = alignTo(CommonSize, Align) + Size;
498     }
499   }
500   if (CommonSize != 0) {
501     RWSectionSizes.push_back(CommonSize);
502     RWDataAlign = std::max(RWDataAlign, CommonAlign);
503   }
504 
505   // Compute the required allocation space for each different type of sections
506   // (code, read-only data, read-write data) assuming that all sections are
507   // allocated with the max alignment. Note that we cannot compute with the
508   // individual alignments of the sections, because then the required size
509   // depends on the order, in which the sections are allocated.
510   CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign);
511   RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign);
512   RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign);
513 
514   return Error::success();
515 }
516 
517 // compute stub buffer size for the given section
518 unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
519                                                     const SectionRef &Section) {
520   unsigned StubSize = getMaxStubSize();
521   if (StubSize == 0) {
522     return 0;
523   }
524   // FIXME: this is an inefficient way to handle this. We should computed the
525   // necessary section allocation size in loadObject by walking all the sections
526   // once.
527   unsigned StubBufSize = 0;
528   for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
529        SI != SE; ++SI) {
530     section_iterator RelSecI = SI->getRelocatedSection();
531     if (!(RelSecI == Section))
532       continue;
533 
534     for (const RelocationRef &Reloc : SI->relocations())
535       if (relocationNeedsStub(Reloc))
536         StubBufSize += StubSize;
537   }
538 
539   // Get section data size and alignment
540   uint64_t DataSize = Section.getSize();
541   uint64_t Alignment64 = Section.getAlignment();
542 
543   // Add stubbuf size alignment
544   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
545   unsigned StubAlignment = getStubAlignment();
546   unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
547   if (StubAlignment > EndAlignment)
548     StubBufSize += StubAlignment - EndAlignment;
549   return StubBufSize;
550 }
551 
552 uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
553                                              unsigned Size) const {
554   uint64_t Result = 0;
555   if (IsTargetLittleEndian) {
556     Src += Size - 1;
557     while (Size--)
558       Result = (Result << 8) | *Src--;
559   } else
560     while (Size--)
561       Result = (Result << 8) | *Src++;
562 
563   return Result;
564 }
565 
566 void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
567                                           unsigned Size) const {
568   if (IsTargetLittleEndian) {
569     while (Size--) {
570       *Dst++ = Value & 0xFF;
571       Value >>= 8;
572     }
573   } else {
574     Dst += Size - 1;
575     while (Size--) {
576       *Dst-- = Value & 0xFF;
577       Value >>= 8;
578     }
579   }
580 }
581 
582 Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
583                                          CommonSymbolList &CommonSymbols) {
584   if (CommonSymbols.empty())
585     return Error::success();
586 
587   uint64_t CommonSize = 0;
588   uint32_t CommonAlign = CommonSymbols.begin()->getAlignment();
589   CommonSymbolList SymbolsToAllocate;
590 
591   DEBUG(dbgs() << "Processing common symbols...\n");
592 
593   for (const auto &Sym : CommonSymbols) {
594     StringRef Name;
595     if (auto NameOrErr = Sym.getName())
596       Name = *NameOrErr;
597     else
598       return NameOrErr.takeError();
599 
600     // Skip common symbols already elsewhere.
601     if (GlobalSymbolTable.count(Name)) {
602       DEBUG(dbgs() << "\tSkipping already emitted common symbol '" << Name
603                    << "'\n");
604       continue;
605     }
606 
607     if (auto Sym = Resolver.findSymbolInLogicalDylib(Name)) {
608       if (!Sym.getFlags().isCommon()) {
609         DEBUG(dbgs() << "\tSkipping common symbol '" << Name
610                      << "' in favor of stronger definition.\n");
611         continue;
612       }
613     }
614     uint32_t Align = Sym.getAlignment();
615     uint64_t Size = Sym.getCommonSize();
616 
617     CommonSize = alignTo(CommonSize, Align) + Size;
618 
619     SymbolsToAllocate.push_back(Sym);
620   }
621 
622   // Allocate memory for the section
623   unsigned SectionID = Sections.size();
624   uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
625                                              "<common symbols>", false);
626   if (!Addr)
627     report_fatal_error("Unable to allocate memory for common symbols!");
628   uint64_t Offset = 0;
629   Sections.push_back(
630       SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0));
631   memset(Addr, 0, CommonSize);
632 
633   DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID << " new addr: "
634                << format("%p", Addr) << " DataSize: " << CommonSize << "\n");
635 
636   // Assign the address of each symbol
637   for (auto &Sym : SymbolsToAllocate) {
638     uint32_t Align = Sym.getAlignment();
639     uint64_t Size = Sym.getCommonSize();
640     StringRef Name;
641     if (auto NameOrErr = Sym.getName())
642       Name = *NameOrErr;
643     else
644       return NameOrErr.takeError();
645     if (Align) {
646       // This symbol has an alignment requirement.
647       uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
648       Addr += AlignOffset;
649       Offset += AlignOffset;
650     }
651     JITSymbolFlags JITSymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
652     DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
653                  << format("%p", Addr) << "\n");
654     GlobalSymbolTable[Name] =
655       SymbolTableEntry(SectionID, Offset, JITSymFlags);
656     Offset += Size;
657     Addr += Size;
658   }
659 
660   if (Checker)
661     Checker->registerSection(Obj.getFileName(), SectionID);
662 
663   return Error::success();
664 }
665 
666 Expected<unsigned>
667 RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
668                              const SectionRef &Section,
669                              bool IsCode) {
670   StringRef data;
671   uint64_t Alignment64 = Section.getAlignment();
672 
673   unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
674   unsigned PaddingSize = 0;
675   unsigned StubBufSize = 0;
676   bool IsRequired = isRequiredForExecution(Section);
677   bool IsVirtual = Section.isVirtual();
678   bool IsZeroInit = isZeroInit(Section);
679   bool IsReadOnly = isReadOnlyData(Section);
680   uint64_t DataSize = Section.getSize();
681 
682   StringRef Name;
683   if (auto EC = Section.getName(Name))
684     return errorCodeToError(EC);
685 
686   StubBufSize = computeSectionStubBufSize(Obj, Section);
687 
688   // The .eh_frame section (at least on Linux) needs an extra four bytes padded
689   // with zeroes added at the end.  For MachO objects, this section has a
690   // slightly different name, so this won't have any effect for MachO objects.
691   if (Name == ".eh_frame")
692     PaddingSize = 4;
693 
694   uintptr_t Allocate;
695   unsigned SectionID = Sections.size();
696   uint8_t *Addr;
697   const char *pData = nullptr;
698 
699   // If this section contains any bits (i.e. isn't a virtual or bss section),
700   // grab a reference to them.
701   if (!IsVirtual && !IsZeroInit) {
702     // In either case, set the location of the unrelocated section in memory,
703     // since we still process relocations for it even if we're not applying them.
704     if (auto EC = Section.getContents(data))
705       return errorCodeToError(EC);
706     pData = data.data();
707   }
708 
709   // Code section alignment needs to be at least as high as stub alignment or
710   // padding calculations may by incorrect when the section is remapped to a
711   // higher alignment.
712   if (IsCode)
713     Alignment = std::max(Alignment, getStubAlignment());
714 
715   // Some sections, such as debug info, don't need to be loaded for execution.
716   // Leave those where they are.
717   if (IsRequired) {
718     Allocate = DataSize + PaddingSize + StubBufSize;
719     if (!Allocate)
720       Allocate = 1;
721     Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
722                                                Name)
723                   : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
724                                                Name, IsReadOnly);
725     if (!Addr)
726       report_fatal_error("Unable to allocate section memory!");
727 
728     // Zero-initialize or copy the data from the image
729     if (IsZeroInit || IsVirtual)
730       memset(Addr, 0, DataSize);
731     else
732       memcpy(Addr, pData, DataSize);
733 
734     // Fill in any extra bytes we allocated for padding
735     if (PaddingSize != 0) {
736       memset(Addr + DataSize, 0, PaddingSize);
737       // Update the DataSize variable so that the stub offset is set correctly.
738       DataSize += PaddingSize;
739     }
740 
741     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
742                  << " obj addr: " << format("%p", pData)
743                  << " new addr: " << format("%p", Addr)
744                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
745                  << " Allocate: " << Allocate << "\n");
746   } else {
747     // Even if we didn't load the section, we need to record an entry for it
748     // to handle later processing (and by 'handle' I mean don't do anything
749     // with these sections).
750     Allocate = 0;
751     Addr = nullptr;
752     DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
753                  << " obj addr: " << format("%p", data.data()) << " new addr: 0"
754                  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
755                  << " Allocate: " << Allocate << "\n");
756   }
757 
758   Sections.push_back(
759       SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData));
760 
761   if (Checker)
762     Checker->registerSection(Obj.getFileName(), SectionID);
763 
764   return SectionID;
765 }
766 
767 Expected<unsigned>
768 RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
769                                    const SectionRef &Section,
770                                    bool IsCode,
771                                    ObjSectionToIDMap &LocalSections) {
772 
773   unsigned SectionID = 0;
774   ObjSectionToIDMap::iterator i = LocalSections.find(Section);
775   if (i != LocalSections.end())
776     SectionID = i->second;
777   else {
778     if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
779       SectionID = *SectionIDOrErr;
780     else
781       return SectionIDOrErr.takeError();
782     LocalSections[Section] = SectionID;
783   }
784   return SectionID;
785 }
786 
787 void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
788                                               unsigned SectionID) {
789   Relocations[SectionID].push_back(RE);
790 }
791 
792 void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
793                                              StringRef SymbolName) {
794   // Relocation by symbol.  If the symbol is found in the global symbol table,
795   // create an appropriate section relocation.  Otherwise, add it to
796   // ExternalSymbolRelocations.
797   RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
798   if (Loc == GlobalSymbolTable.end()) {
799     ExternalSymbolRelocations[SymbolName].push_back(RE);
800   } else {
801     // Copy the RE since we want to modify its addend.
802     RelocationEntry RECopy = RE;
803     const auto &SymInfo = Loc->second;
804     RECopy.Addend += SymInfo.getOffset();
805     Relocations[SymInfo.getSectionID()].push_back(RECopy);
806   }
807 }
808 
809 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
810                                              unsigned AbiVariant) {
811   if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
812     // This stub has to be able to access the full address space,
813     // since symbol lookup won't necessarily find a handy, in-range,
814     // PLT stub for functions which could be anywhere.
815     // Stub can use ip0 (== x16) to calculate address
816     writeBytesUnaligned(0xd2e00010, Addr,    4); // movz ip0, #:abs_g3:<addr>
817     writeBytesUnaligned(0xf2c00010, Addr+4,  4); // movk ip0, #:abs_g2_nc:<addr>
818     writeBytesUnaligned(0xf2a00010, Addr+8,  4); // movk ip0, #:abs_g1_nc:<addr>
819     writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
820     writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
821 
822     return Addr;
823   } else if (Arch == Triple::arm || Arch == Triple::armeb) {
824     // TODO: There is only ARM far stub now. We should add the Thumb stub,
825     // and stubs for branches Thumb - ARM and ARM - Thumb.
826     writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc,<label>
827     return Addr + 4;
828   } else if (IsMipsO32ABI) {
829     // 0:   3c190000        lui     t9,%hi(addr).
830     // 4:   27390000        addiu   t9,t9,%lo(addr).
831     // 8:   03200008        jr      t9.
832     // c:   00000000        nop.
833     const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
834     const unsigned NopInstr = 0x0;
835     unsigned JrT9Instr = 0x03200008;
836     if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6)
837         JrT9Instr = 0x03200009;
838 
839     writeBytesUnaligned(LuiT9Instr, Addr, 4);
840     writeBytesUnaligned(AdduiT9Instr, Addr+4, 4);
841     writeBytesUnaligned(JrT9Instr, Addr+8, 4);
842     writeBytesUnaligned(NopInstr, Addr+12, 4);
843     return Addr;
844   } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
845     // Depending on which version of the ELF ABI is in use, we need to
846     // generate one of two variants of the stub.  They both start with
847     // the same sequence to load the target address into r12.
848     writeInt32BE(Addr,    0x3D800000); // lis   r12, highest(addr)
849     writeInt32BE(Addr+4,  0x618C0000); // ori   r12, higher(addr)
850     writeInt32BE(Addr+8,  0x798C07C6); // sldi  r12, r12, 32
851     writeInt32BE(Addr+12, 0x658C0000); // oris  r12, r12, h(addr)
852     writeInt32BE(Addr+16, 0x618C0000); // ori   r12, r12, l(addr)
853     if (AbiVariant == 2) {
854       // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
855       // The address is already in r12 as required by the ABI.  Branch to it.
856       writeInt32BE(Addr+20, 0xF8410018); // std   r2,  24(r1)
857       writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
858       writeInt32BE(Addr+28, 0x4E800420); // bctr
859     } else {
860       // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
861       // Load the function address on r11 and sets it to control register. Also
862       // loads the function TOC in r2 and environment pointer to r11.
863       writeInt32BE(Addr+20, 0xF8410028); // std   r2,  40(r1)
864       writeInt32BE(Addr+24, 0xE96C0000); // ld    r11, 0(r12)
865       writeInt32BE(Addr+28, 0xE84C0008); // ld    r2,  0(r12)
866       writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
867       writeInt32BE(Addr+36, 0xE96C0010); // ld    r11, 16(r2)
868       writeInt32BE(Addr+40, 0x4E800420); // bctr
869     }
870     return Addr;
871   } else if (Arch == Triple::systemz) {
872     writeInt16BE(Addr,    0xC418);     // lgrl %r1,.+8
873     writeInt16BE(Addr+2,  0x0000);
874     writeInt16BE(Addr+4,  0x0004);
875     writeInt16BE(Addr+6,  0x07F1);     // brc 15,%r1
876     // 8-byte address stored at Addr + 8
877     return Addr;
878   } else if (Arch == Triple::x86_64) {
879     *Addr      = 0xFF; // jmp
880     *(Addr+1)  = 0x25; // rip
881     // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
882   } else if (Arch == Triple::x86) {
883     *Addr      = 0xE9; // 32-bit pc-relative jump.
884   }
885   return Addr;
886 }
887 
888 // Assign an address to a symbol name and resolve all the relocations
889 // associated with it.
890 void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
891                                              uint64_t Addr) {
892   // The address to use for relocation resolution is not
893   // the address of the local section buffer. We must be doing
894   // a remote execution environment of some sort. Relocations can't
895   // be applied until all the sections have been moved.  The client must
896   // trigger this with a call to MCJIT::finalize() or
897   // RuntimeDyld::resolveRelocations().
898   //
899   // Addr is a uint64_t because we can't assume the pointer width
900   // of the target is the same as that of the host. Just use a generic
901   // "big enough" type.
902   DEBUG(dbgs() << "Reassigning address for section " << SectionID << " ("
903                << Sections[SectionID].getName() << "): "
904                << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress())
905                << " -> " << format("0x%016" PRIx64, Addr) << "\n");
906   Sections[SectionID].setLoadAddress(Addr);
907 }
908 
909 void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
910                                             uint64_t Value) {
911   for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
912     const RelocationEntry &RE = Relocs[i];
913     // Ignore relocations for sections that were not loaded
914     if (Sections[RE.SectionID].getAddress() == nullptr)
915       continue;
916     resolveRelocation(RE, Value);
917   }
918 }
919 
920 void RuntimeDyldImpl::resolveExternalSymbols() {
921   while (!ExternalSymbolRelocations.empty()) {
922     StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
923 
924     StringRef Name = i->first();
925     if (Name.size() == 0) {
926       // This is an absolute symbol, use an address of zero.
927       DEBUG(dbgs() << "Resolving absolute relocations."
928                    << "\n");
929       RelocationList &Relocs = i->second;
930       resolveRelocationList(Relocs, 0);
931     } else {
932       uint64_t Addr = 0;
933       RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
934       if (Loc == GlobalSymbolTable.end()) {
935         // This is an external symbol, try to get its address from the symbol
936         // resolver.
937         // First search for the symbol in this logical dylib.
938         Addr = Resolver.findSymbolInLogicalDylib(Name.data()).getAddress();
939         // If that fails, try searching for an external symbol.
940         if (!Addr)
941           Addr = Resolver.findSymbol(Name.data()).getAddress();
942         // The call to getSymbolAddress may have caused additional modules to
943         // be loaded, which may have added new entries to the
944         // ExternalSymbolRelocations map.  Consquently, we need to update our
945         // iterator.  This is also why retrieval of the relocation list
946         // associated with this symbol is deferred until below this point.
947         // New entries may have been added to the relocation list.
948         i = ExternalSymbolRelocations.find(Name);
949       } else {
950         // We found the symbol in our global table.  It was probably in a
951         // Module that we loaded previously.
952         const auto &SymInfo = Loc->second;
953         Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
954                SymInfo.getOffset();
955       }
956 
957       // FIXME: Implement error handling that doesn't kill the host program!
958       if (!Addr)
959         report_fatal_error("Program used external function '" + Name +
960                            "' which could not be resolved!");
961 
962       // If Resolver returned UINT64_MAX, the client wants to handle this symbol
963       // manually and we shouldn't resolve its relocations.
964       if (Addr != UINT64_MAX) {
965         DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
966                      << format("0x%lx", Addr) << "\n");
967         // This list may have been updated when we called getSymbolAddress, so
968         // don't change this code to get the list earlier.
969         RelocationList &Relocs = i->second;
970         resolveRelocationList(Relocs, Addr);
971       }
972     }
973 
974     ExternalSymbolRelocations.erase(i);
975   }
976 }
977 
978 //===----------------------------------------------------------------------===//
979 // RuntimeDyld class implementation
980 
981 uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
982                                           const object::SectionRef &Sec) const {
983 
984   auto I = ObjSecToIDMap.find(Sec);
985   if (I != ObjSecToIDMap.end())
986     return RTDyld.Sections[I->second].getLoadAddress();
987 
988   return 0;
989 }
990 
991 void RuntimeDyld::MemoryManager::anchor() {}
992 void JITSymbolResolver::anchor() {}
993 
994 RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
995                          JITSymbolResolver &Resolver)
996     : MemMgr(MemMgr), Resolver(Resolver) {
997   // FIXME: There's a potential issue lurking here if a single instance of
998   // RuntimeDyld is used to load multiple objects.  The current implementation
999   // associates a single memory manager with a RuntimeDyld instance.  Even
1000   // though the public class spawns a new 'impl' instance for each load,
1001   // they share a single memory manager.  This can become a problem when page
1002   // permissions are applied.
1003   Dyld = nullptr;
1004   ProcessAllSections = false;
1005   Checker = nullptr;
1006 }
1007 
1008 RuntimeDyld::~RuntimeDyld() {}
1009 
1010 static std::unique_ptr<RuntimeDyldCOFF>
1011 createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1012                       JITSymbolResolver &Resolver, bool ProcessAllSections,
1013                       RuntimeDyldCheckerImpl *Checker) {
1014   std::unique_ptr<RuntimeDyldCOFF> Dyld =
1015     RuntimeDyldCOFF::create(Arch, MM, Resolver);
1016   Dyld->setProcessAllSections(ProcessAllSections);
1017   Dyld->setRuntimeDyldChecker(Checker);
1018   return Dyld;
1019 }
1020 
1021 static std::unique_ptr<RuntimeDyldELF>
1022 createRuntimeDyldELF(RuntimeDyld::MemoryManager &MM,
1023                      JITSymbolResolver &Resolver, bool ProcessAllSections,
1024                      RuntimeDyldCheckerImpl *Checker) {
1025   std::unique_ptr<RuntimeDyldELF> Dyld(new RuntimeDyldELF(MM, Resolver));
1026   Dyld->setProcessAllSections(ProcessAllSections);
1027   Dyld->setRuntimeDyldChecker(Checker);
1028   return Dyld;
1029 }
1030 
1031 static std::unique_ptr<RuntimeDyldMachO>
1032 createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
1033                        JITSymbolResolver &Resolver,
1034                        bool ProcessAllSections,
1035                        RuntimeDyldCheckerImpl *Checker) {
1036   std::unique_ptr<RuntimeDyldMachO> Dyld =
1037     RuntimeDyldMachO::create(Arch, MM, Resolver);
1038   Dyld->setProcessAllSections(ProcessAllSections);
1039   Dyld->setRuntimeDyldChecker(Checker);
1040   return Dyld;
1041 }
1042 
1043 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
1044 RuntimeDyld::loadObject(const ObjectFile &Obj) {
1045   if (!Dyld) {
1046     if (Obj.isELF())
1047       Dyld = createRuntimeDyldELF(MemMgr, Resolver, ProcessAllSections, Checker);
1048     else if (Obj.isMachO())
1049       Dyld = createRuntimeDyldMachO(
1050                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1051                ProcessAllSections, Checker);
1052     else if (Obj.isCOFF())
1053       Dyld = createRuntimeDyldCOFF(
1054                static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1055                ProcessAllSections, Checker);
1056     else
1057       report_fatal_error("Incompatible object format!");
1058   }
1059 
1060   if (!Dyld->isCompatibleFile(Obj))
1061     report_fatal_error("Incompatible object format!");
1062 
1063   auto LoadedObjInfo = Dyld->loadObject(Obj);
1064   MemMgr.notifyObjectLoaded(*this, Obj);
1065   return LoadedObjInfo;
1066 }
1067 
1068 void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const {
1069   if (!Dyld)
1070     return nullptr;
1071   return Dyld->getSymbolLocalAddress(Name);
1072 }
1073 
1074 JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const {
1075   if (!Dyld)
1076     return nullptr;
1077   return Dyld->getSymbol(Name);
1078 }
1079 
1080 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
1081 
1082 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
1083   Dyld->reassignSectionAddress(SectionID, Addr);
1084 }
1085 
1086 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
1087                                     uint64_t TargetAddress) {
1088   Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1089 }
1090 
1091 bool RuntimeDyld::hasError() { return Dyld->hasError(); }
1092 
1093 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
1094 
1095 void RuntimeDyld::finalizeWithMemoryManagerLocking() {
1096   bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1097   MemMgr.FinalizationLocked = true;
1098   resolveRelocations();
1099   registerEHFrames();
1100   if (!MemoryFinalizationLocked) {
1101     MemMgr.finalizeMemory();
1102     MemMgr.FinalizationLocked = false;
1103   }
1104 }
1105 
1106 void RuntimeDyld::registerEHFrames() {
1107   if (Dyld)
1108     Dyld->registerEHFrames();
1109 }
1110 
1111 void RuntimeDyld::deregisterEHFrames() {
1112   if (Dyld)
1113     Dyld->deregisterEHFrames();
1114 }
1115 
1116 } // end namespace llvm
1117