OSDN Git Service

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