LLVM  7.0.0svn
RuntimeDyld.cpp
Go to the documentation of this file.
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 
15 #include "RuntimeDyldCOFF.h"
16 #include "RuntimeDyldCheckerImpl.h"
17 #include "RuntimeDyldELF.h"
18 #include "RuntimeDyldImpl.h"
19 #include "RuntimeDyldMachO.h"
20 #include "llvm/Object/COFF.h"
25 
26 using namespace llvm;
27 using namespace llvm::object;
28 
29 #define DEBUG_TYPE "dyld"
30 
31 namespace {
32 
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 
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.
68 
69 // Pin LoadedObjectInfo's vtables to this file.
71 
72 namespace llvm {
73 
75 
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.
122  MutexGuard locked(lock);
123 
124  // Print out the sections prior to relocation.
125  LLVM_DEBUG(for (int i = 0, e = Sections.size(); i != e; ++i)
126  dumpSectionMemory(Sections[i], "before relocations"););
127 
128  // First, resolve relocations associated with external symbols.
129  if (auto Err = resolveExternalSymbols()) {
130  HasError = true;
131  ErrorStr = toString(std::move(Err));
132  }
133 
134  // Iterate over all outstanding relocations
135  for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) {
136  // The Section here (Sections[i]) refers to the section in which the
137  // symbol for the relocation is located. The SectionID in the relocation
138  // entry provides the section to which the relocation will be applied.
139  int Idx = it->first;
140  uint64_t Addr = Sections[Idx].getLoadAddress();
141  LLVM_DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t"
142  << format("%p", (uintptr_t)Addr) << "\n");
143  resolveRelocationList(it->second, Addr);
144  }
145  Relocations.clear();
146 
147  // Print out sections after relocation.
148  LLVM_DEBUG(for (int i = 0, e = Sections.size(); i != e; ++i)
149  dumpSectionMemory(Sections[i], "after relocations"););
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 
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 CommonSymbolsToAllocate;
201 
202  uint64_t CommonSize = 0;
203  uint32_t CommonAlign = 0;
204 
205  // First, collect all weak and common symbols. We need to know if stronger
206  // definitions occur elsewhere.
208  {
210  for (auto &Sym : Obj.symbols()) {
211  uint32_t Flags = Sym.getFlags();
212  if ((Flags & SymbolRef::SF_Common) || (Flags & SymbolRef::SF_Weak)) {
213  // Get symbol name.
214  if (auto NameOrErr = Sym.getName())
215  Symbols.insert(*NameOrErr);
216  else
217  return NameOrErr.takeError();
218  }
219  }
220 
221  if (auto FlagsResultOrErr = Resolver.lookupFlags(Symbols))
222  SymbolFlags = std::move(*FlagsResultOrErr);
223  else
224  return FlagsResultOrErr.takeError();
225  }
226 
227  // Parse symbols
228  LLVM_DEBUG(dbgs() << "Parse symbols:\n");
229  for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
230  ++I) {
231  uint32_t Flags = I->getFlags();
232 
233  // Skip undefined symbols.
234  if (Flags & SymbolRef::SF_Undefined)
235  continue;
236 
237  // Get the symbol type.
238  object::SymbolRef::Type SymType;
239  if (auto SymTypeOrErr = I->getType())
240  SymType = *SymTypeOrErr;
241  else
242  return SymTypeOrErr.takeError();
243 
244  // Get symbol name.
245  StringRef Name;
246  if (auto NameOrErr = I->getName())
247  Name = *NameOrErr;
248  else
249  return NameOrErr.takeError();
250 
251  // Compute JIT symbol flags.
252  JITSymbolFlags JITSymFlags = getJITSymbolFlags(*I);
253 
254  // If this is a weak definition, check to see if there's a strong one.
255  // If there is, skip this symbol (we won't be providing it: the strong
256  // definition will). If there's no strong definition, make this definition
257  // strong.
258  if (JITSymFlags.isWeak() || JITSymFlags.isCommon()) {
259  // First check whether there's already a definition in this instance.
260  // FIXME: Override existing weak definitions with strong ones.
261  if (GlobalSymbolTable.count(Name))
262  continue;
263 
264  // Then check whether we found flags for an existing symbol during the
265  // flags lookup earlier.
266  auto FlagsI = SymbolFlags.find(Name);
267  if (FlagsI == SymbolFlags.end() ||
268  (JITSymFlags.isWeak() && !FlagsI->second.isStrong()) ||
269  (JITSymFlags.isCommon() && FlagsI->second.isCommon())) {
270  if (JITSymFlags.isWeak())
271  JITSymFlags &= ~JITSymbolFlags::Weak;
272  if (JITSymFlags.isCommon()) {
273  JITSymFlags &= ~JITSymbolFlags::Common;
274  uint32_t Align = I->getAlignment();
275  uint64_t Size = I->getCommonSize();
276  if (!CommonAlign)
277  CommonAlign = Align;
278  CommonSize += alignTo(CommonSize, Align) + Size;
279  CommonSymbolsToAllocate.push_back(*I);
280  }
281  } else
282  continue;
283  }
284 
285  if (Flags & SymbolRef::SF_Absolute &&
286  SymType != object::SymbolRef::ST_File) {
287  uint64_t Addr = 0;
288  if (auto AddrOrErr = I->getAddress())
289  Addr = *AddrOrErr;
290  else
291  return AddrOrErr.takeError();
292 
293  unsigned SectionID = AbsoluteSymbolSection;
294 
295  LLVM_DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name
296  << " SID: " << SectionID
297  << " Offset: " << format("%p", (uintptr_t)Addr)
298  << " flags: " << Flags << "\n");
299  GlobalSymbolTable[Name] = SymbolTableEntry(SectionID, Addr, JITSymFlags);
300  } else if (SymType == object::SymbolRef::ST_Function ||
301  SymType == object::SymbolRef::ST_Data ||
302  SymType == object::SymbolRef::ST_Unknown ||
303  SymType == object::SymbolRef::ST_Other) {
304 
306  if (auto SIOrErr = I->getSection())
307  SI = *SIOrErr;
308  else
309  return SIOrErr.takeError();
310 
311  if (SI == Obj.section_end())
312  continue;
313 
314  // Get symbol offset.
315  uint64_t SectOffset;
316  if (auto Err = getOffset(*I, *SI, SectOffset))
317  return std::move(Err);
318 
319  bool IsCode = SI->isText();
320  unsigned SectionID;
321  if (auto SectionIDOrErr =
322  findOrEmitSection(Obj, *SI, IsCode, LocalSections))
323  SectionID = *SectionIDOrErr;
324  else
325  return SectionIDOrErr.takeError();
326 
327  LLVM_DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name
328  << " SID: " << SectionID
329  << " Offset: " << format("%p", (uintptr_t)SectOffset)
330  << " flags: " << Flags << "\n");
331  GlobalSymbolTable[Name] =
332  SymbolTableEntry(SectionID, SectOffset, JITSymFlags);
333  }
334  }
335 
336  // Allocate common symbols
337  if (auto Err = emitCommonSymbols(Obj, CommonSymbolsToAllocate, CommonSize,
338  CommonAlign))
339  return std::move(Err);
340 
341  // Parse and process relocations
342  LLVM_DEBUG(dbgs() << "Parse relocations:\n");
343  for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
344  SI != SE; ++SI) {
345  StubMap Stubs;
346  section_iterator RelocatedSection = SI->getRelocatedSection();
347 
348  if (RelocatedSection == SE)
349  continue;
350 
351  relocation_iterator I = SI->relocation_begin();
352  relocation_iterator E = SI->relocation_end();
353 
354  if (I == E && !ProcessAllSections)
355  continue;
356 
357  bool IsCode = RelocatedSection->isText();
358  unsigned SectionID = 0;
359  if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode,
360  LocalSections))
361  SectionID = *SectionIDOrErr;
362  else
363  return SectionIDOrErr.takeError();
364 
365  LLVM_DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n");
366 
367  for (; I != E;)
368  if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs))
369  I = *IOrErr;
370  else
371  return IOrErr.takeError();
372 
373  // If there is an attached checker, notify it about the stubs for this
374  // section so that they can be verified.
375  if (Checker)
376  Checker->registerStubMap(Obj.getFileName(), SectionID, Stubs);
377  }
378 
379  // Give the subclasses a chance to tie-up any loose ends.
380  if (auto Err = finalizeLoad(Obj, LocalSections))
381  return std::move(Err);
382 
383 // for (auto E : LocalSections)
384 // llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n";
385 
386  return LocalSections;
387 }
388 
389 // A helper method for computeTotalAllocSize.
390 // Computes the memory size required to allocate sections with the given sizes,
391 // assuming that all sections are allocated with the given alignment
392 static uint64_t
393 computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
394  uint64_t Alignment) {
395  uint64_t TotalSize = 0;
396  for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) {
397  uint64_t AlignedSize =
398  (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment;
399  TotalSize += AlignedSize;
400  }
401  return TotalSize;
402 }
403 
405  const ObjectFile *Obj = Section.getObject();
406  if (isa<object::ELFObjectFileBase>(Obj))
407  return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC;
408  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) {
409  const coff_section *CoffSection = COFFObj->getCOFFSection(Section);
410  // Avoid loading zero-sized COFF sections.
411  // In PE files, VirtualSize gives the section size, and SizeOfRawData
412  // may be zero for sections with content. In Obj files, SizeOfRawData
413  // gives the section size, and VirtualSize is always zero. Hence
414  // the need to check for both cases below.
415  bool HasContent =
416  (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0);
417  bool IsDiscardable =
418  CoffSection->Characteristics &
420  return HasContent && !IsDiscardable;
421  }
422 
423  assert(isa<MachOObjectFile>(Obj));
424  return true;
425 }
426 
427 static bool isReadOnlyData(const SectionRef Section) {
428  const ObjectFile *Obj = Section.getObject();
429  if (isa<object::ELFObjectFileBase>(Obj))
430  return !(ELFSectionRef(Section).getFlags() &
432  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
433  return ((COFFObj->getCOFFSection(Section)->Characteristics &
437  ==
440 
441  assert(isa<MachOObjectFile>(Obj));
442  return false;
443 }
444 
445 static bool isZeroInit(const SectionRef Section) {
446  const ObjectFile *Obj = Section.getObject();
447  if (isa<object::ELFObjectFileBase>(Obj))
448  return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS;
449  if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj))
450  return COFFObj->getCOFFSection(Section)->Characteristics &
452 
453  auto *MachO = cast<MachOObjectFile>(Obj);
454  unsigned SectionType = MachO->getSectionType(Section);
455  return SectionType == MachO::S_ZEROFILL ||
456  SectionType == MachO::S_GB_ZEROFILL;
457 }
458 
459 // Compute an upper bound of the memory size that is required to load all
460 // sections
462  uint64_t &CodeSize,
463  uint32_t &CodeAlign,
464  uint64_t &RODataSize,
465  uint32_t &RODataAlign,
466  uint64_t &RWDataSize,
467  uint32_t &RWDataAlign) {
468  // Compute the size of all sections required for execution
469  std::vector<uint64_t> CodeSectionSizes;
470  std::vector<uint64_t> ROSectionSizes;
471  std::vector<uint64_t> RWSectionSizes;
472 
473  // Collect sizes of all sections to be loaded;
474  // also determine the max alignment of all sections
475  for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
476  SI != SE; ++SI) {
477  const SectionRef &Section = *SI;
478 
479  bool IsRequired = isRequiredForExecution(Section) || ProcessAllSections;
480 
481  // Consider only the sections that are required to be loaded for execution
482  if (IsRequired) {
483  uint64_t DataSize = Section.getSize();
484  uint64_t Alignment64 = Section.getAlignment();
485  unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
486  bool IsCode = Section.isText();
487  bool IsReadOnly = isReadOnlyData(Section);
488 
489  StringRef Name;
490  if (auto EC = Section.getName(Name))
491  return errorCodeToError(EC);
492 
493  uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section);
494  uint64_t SectionSize = DataSize + StubBufSize;
495 
496  // The .eh_frame section (at least on Linux) needs an extra four bytes
497  // padded
498  // with zeroes added at the end. For MachO objects, this section has a
499  // slightly different name, so this won't have any effect for MachO
500  // objects.
501  if (Name == ".eh_frame")
502  SectionSize += 4;
503 
504  if (!SectionSize)
505  SectionSize = 1;
506 
507  if (IsCode) {
508  CodeAlign = std::max(CodeAlign, Alignment);
509  CodeSectionSizes.push_back(SectionSize);
510  } else if (IsReadOnly) {
511  RODataAlign = std::max(RODataAlign, Alignment);
512  ROSectionSizes.push_back(SectionSize);
513  } else {
514  RWDataAlign = std::max(RWDataAlign, Alignment);
515  RWSectionSizes.push_back(SectionSize);
516  }
517  }
518  }
519 
520  // Compute Global Offset Table size. If it is not zero we
521  // also update alignment, which is equal to a size of a
522  // single GOT entry.
523  if (unsigned GotSize = computeGOTSize(Obj)) {
524  RWSectionSizes.push_back(GotSize);
525  RWDataAlign = std::max<uint32_t>(RWDataAlign, getGOTEntrySize());
526  }
527 
528  // Compute the size of all common symbols
529  uint64_t CommonSize = 0;
530  uint32_t CommonAlign = 1;
531  for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
532  ++I) {
533  uint32_t Flags = I->getFlags();
534  if (Flags & SymbolRef::SF_Common) {
535  // Add the common symbols to a list. We'll allocate them all below.
536  uint64_t Size = I->getCommonSize();
537  uint32_t Align = I->getAlignment();
538  // If this is the first common symbol, use its alignment as the alignment
539  // for the common symbols section.
540  if (CommonSize == 0)
541  CommonAlign = Align;
542  CommonSize = alignTo(CommonSize, Align) + Size;
543  }
544  }
545  if (CommonSize != 0) {
546  RWSectionSizes.push_back(CommonSize);
547  RWDataAlign = std::max(RWDataAlign, CommonAlign);
548  }
549 
550  // Compute the required allocation space for each different type of sections
551  // (code, read-only data, read-write data) assuming that all sections are
552  // allocated with the max alignment. Note that we cannot compute with the
553  // individual alignments of the sections, because then the required size
554  // depends on the order, in which the sections are allocated.
555  CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign);
556  RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign);
557  RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign);
558 
559  return Error::success();
560 }
561 
562 // compute GOT size
564  size_t GotEntrySize = getGOTEntrySize();
565  if (!GotEntrySize)
566  return 0;
567 
568  size_t GotSize = 0;
569  for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
570  SI != SE; ++SI) {
571 
572  for (const RelocationRef &Reloc : SI->relocations())
573  if (relocationNeedsGot(Reloc))
574  GotSize += GotEntrySize;
575  }
576 
577  return GotSize;
578 }
579 
580 // compute stub buffer size for the given section
582  const SectionRef &Section) {
583  unsigned StubSize = getMaxStubSize();
584  if (StubSize == 0) {
585  return 0;
586  }
587  // FIXME: this is an inefficient way to handle this. We should computed the
588  // necessary section allocation size in loadObject by walking all the sections
589  // once.
590  unsigned StubBufSize = 0;
591  for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end();
592  SI != SE; ++SI) {
593  section_iterator RelSecI = SI->getRelocatedSection();
594  if (!(RelSecI == Section))
595  continue;
596 
597  for (const RelocationRef &Reloc : SI->relocations())
598  if (relocationNeedsStub(Reloc))
599  StubBufSize += StubSize;
600  }
601 
602  // Get section data size and alignment
603  uint64_t DataSize = Section.getSize();
604  uint64_t Alignment64 = Section.getAlignment();
605 
606  // Add stubbuf size alignment
607  unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
608  unsigned StubAlignment = getStubAlignment();
609  unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment);
610  if (StubAlignment > EndAlignment)
611  StubBufSize += StubAlignment - EndAlignment;
612  return StubBufSize;
613 }
614 
616  unsigned Size) const {
617  uint64_t Result = 0;
618  if (IsTargetLittleEndian) {
619  Src += Size - 1;
620  while (Size--)
621  Result = (Result << 8) | *Src--;
622  } else
623  while (Size--)
624  Result = (Result << 8) | *Src++;
625 
626  return Result;
627 }
628 
629 void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
630  unsigned Size) const {
631  if (IsTargetLittleEndian) {
632  while (Size--) {
633  *Dst++ = Value & 0xFF;
634  Value >>= 8;
635  }
636  } else {
637  Dst += Size - 1;
638  while (Size--) {
639  *Dst-- = Value & 0xFF;
640  Value >>= 8;
641  }
642  }
643 }
644 
647 }
648 
650  CommonSymbolList &SymbolsToAllocate,
651  uint64_t CommonSize,
652  uint32_t CommonAlign) {
653  if (SymbolsToAllocate.empty())
654  return Error::success();
655 
656  // Allocate memory for the section
657  unsigned SectionID = Sections.size();
658  uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID,
659  "<common symbols>", false);
660  if (!Addr)
661  report_fatal_error("Unable to allocate memory for common symbols!");
662  uint64_t Offset = 0;
663  Sections.push_back(
664  SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0));
665  memset(Addr, 0, CommonSize);
666 
667  LLVM_DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID
668  << " new addr: " << format("%p", Addr)
669  << " DataSize: " << CommonSize << "\n");
670 
671  // Assign the address of each symbol
672  for (auto &Sym : SymbolsToAllocate) {
673  uint32_t Align = Sym.getAlignment();
674  uint64_t Size = Sym.getCommonSize();
675  StringRef Name;
676  if (auto NameOrErr = Sym.getName())
677  Name = *NameOrErr;
678  else
679  return NameOrErr.takeError();
680  if (Align) {
681  // This symbol has an alignment requirement.
682  uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
683  Addr += AlignOffset;
684  Offset += AlignOffset;
685  }
686  JITSymbolFlags JITSymFlags = getJITSymbolFlags(Sym);
687  LLVM_DEBUG(dbgs() << "Allocating common symbol " << Name << " address "
688  << format("%p", Addr) << "\n");
689  GlobalSymbolTable[Name] =
690  SymbolTableEntry(SectionID, Offset, JITSymFlags);
691  Offset += Size;
692  Addr += Size;
693  }
694 
695  if (Checker)
696  Checker->registerSection(Obj.getFileName(), SectionID);
697 
698  return Error::success();
699 }
700 
703  const SectionRef &Section,
704  bool IsCode) {
705  StringRef data;
706  uint64_t Alignment64 = Section.getAlignment();
707 
708  unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL;
709  unsigned PaddingSize = 0;
710  unsigned StubBufSize = 0;
711  bool IsRequired = isRequiredForExecution(Section);
712  bool IsVirtual = Section.isVirtual();
713  bool IsZeroInit = isZeroInit(Section);
714  bool IsReadOnly = isReadOnlyData(Section);
715  uint64_t DataSize = Section.getSize();
716 
717  StringRef Name;
718  if (auto EC = Section.getName(Name))
719  return errorCodeToError(EC);
720 
721  StubBufSize = computeSectionStubBufSize(Obj, Section);
722 
723  // The .eh_frame section (at least on Linux) needs an extra four bytes padded
724  // with zeroes added at the end. For MachO objects, this section has a
725  // slightly different name, so this won't have any effect for MachO objects.
726  if (Name == ".eh_frame")
727  PaddingSize = 4;
728 
729  uintptr_t Allocate;
730  unsigned SectionID = Sections.size();
731  uint8_t *Addr;
732  const char *pData = nullptr;
733 
734  // If this section contains any bits (i.e. isn't a virtual or bss section),
735  // grab a reference to them.
736  if (!IsVirtual && !IsZeroInit) {
737  // In either case, set the location of the unrelocated section in memory,
738  // since we still process relocations for it even if we're not applying them.
739  if (auto EC = Section.getContents(data))
740  return errorCodeToError(EC);
741  pData = data.data();
742  }
743 
744  // Code section alignment needs to be at least as high as stub alignment or
745  // padding calculations may by incorrect when the section is remapped to a
746  // higher alignment.
747  if (IsCode) {
748  Alignment = std::max(Alignment, getStubAlignment());
749  if (StubBufSize > 0)
750  PaddingSize += getStubAlignment() - 1;
751  }
752 
753  // Some sections, such as debug info, don't need to be loaded for execution.
754  // Process those only if explicitly requested.
755  if (IsRequired || ProcessAllSections) {
756  Allocate = DataSize + PaddingSize + StubBufSize;
757  if (!Allocate)
758  Allocate = 1;
759  Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID,
760  Name)
761  : MemMgr.allocateDataSection(Allocate, Alignment, SectionID,
762  Name, IsReadOnly);
763  if (!Addr)
764  report_fatal_error("Unable to allocate section memory!");
765 
766  // Zero-initialize or copy the data from the image
767  if (IsZeroInit || IsVirtual)
768  memset(Addr, 0, DataSize);
769  else
770  memcpy(Addr, pData, DataSize);
771 
772  // Fill in any extra bytes we allocated for padding
773  if (PaddingSize != 0) {
774  memset(Addr + DataSize, 0, PaddingSize);
775  // Update the DataSize variable to include padding.
776  DataSize += PaddingSize;
777 
778  // Align DataSize to stub alignment if we have any stubs (PaddingSize will
779  // have been increased above to account for this).
780  if (StubBufSize > 0)
781  DataSize &= ~(getStubAlignment() - 1);
782  }
783 
784  LLVM_DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: "
785  << Name << " obj addr: " << format("%p", pData)
786  << " new addr: " << format("%p", Addr) << " DataSize: "
787  << DataSize << " StubBufSize: " << StubBufSize
788  << " Allocate: " << Allocate << "\n");
789  } else {
790  // Even if we didn't load the section, we need to record an entry for it
791  // to handle later processing (and by 'handle' I mean don't do anything
792  // with these sections).
793  Allocate = 0;
794  Addr = nullptr;
795  LLVM_DEBUG(
796  dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name
797  << " obj addr: " << format("%p", data.data()) << " new addr: 0"
798  << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize
799  << " Allocate: " << Allocate << "\n");
800  }
801 
802  Sections.push_back(
803  SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData));
804 
805  // Debug info sections are linked as if their load address was zero
806  if (!IsRequired)
807  Sections.back().setLoadAddress(0);
808 
809  if (Checker)
810  Checker->registerSection(Obj.getFileName(), SectionID);
811 
812  return SectionID;
813 }
814 
817  const SectionRef &Section,
818  bool IsCode,
819  ObjSectionToIDMap &LocalSections) {
820 
821  unsigned SectionID = 0;
822  ObjSectionToIDMap::iterator i = LocalSections.find(Section);
823  if (i != LocalSections.end())
824  SectionID = i->second;
825  else {
826  if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode))
827  SectionID = *SectionIDOrErr;
828  else
829  return SectionIDOrErr.takeError();
830  LocalSections[Section] = SectionID;
831  }
832  return SectionID;
833 }
834 
836  unsigned SectionID) {
837  Relocations[SectionID].push_back(RE);
838 }
839 
842  // Relocation by symbol. If the symbol is found in the global symbol table,
843  // create an appropriate section relocation. Otherwise, add it to
844  // ExternalSymbolRelocations.
845  RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName);
846  if (Loc == GlobalSymbolTable.end()) {
847  ExternalSymbolRelocations[SymbolName].push_back(RE);
848  } else {
849  // Copy the RE since we want to modify its addend.
850  RelocationEntry RECopy = RE;
851  const auto &SymInfo = Loc->second;
852  RECopy.Addend += SymInfo.getOffset();
853  Relocations[SymInfo.getSectionID()].push_back(RECopy);
854  }
855 }
856 
857 uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
858  unsigned AbiVariant) {
859  if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be) {
860  // This stub has to be able to access the full address space,
861  // since symbol lookup won't necessarily find a handy, in-range,
862  // PLT stub for functions which could be anywhere.
863  // Stub can use ip0 (== x16) to calculate address
864  writeBytesUnaligned(0xd2e00010, Addr, 4); // movz ip0, #:abs_g3:<addr>
865  writeBytesUnaligned(0xf2c00010, Addr+4, 4); // movk ip0, #:abs_g2_nc:<addr>
866  writeBytesUnaligned(0xf2a00010, Addr+8, 4); // movk ip0, #:abs_g1_nc:<addr>
867  writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr>
868  writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0
869 
870  return Addr;
871  } else if (Arch == Triple::arm || Arch == Triple::armeb) {
872  // TODO: There is only ARM far stub now. We should add the Thumb stub,
873  // and stubs for branches Thumb - ARM and ARM - Thumb.
874  writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc, [pc, #-4]
875  return Addr + 4;
876  } else if (IsMipsO32ABI || IsMipsN32ABI) {
877  // 0: 3c190000 lui t9,%hi(addr).
878  // 4: 27390000 addiu t9,t9,%lo(addr).
879  // 8: 03200008 jr t9.
880  // c: 00000000 nop.
881  const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000;
882  const unsigned NopInstr = 0x0;
883  unsigned JrT9Instr = 0x03200008;
884  if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6 ||
885  (AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6)
886  JrT9Instr = 0x03200009;
887 
888  writeBytesUnaligned(LuiT9Instr, Addr, 4);
889  writeBytesUnaligned(AdduiT9Instr, Addr + 4, 4);
890  writeBytesUnaligned(JrT9Instr, Addr + 8, 4);
891  writeBytesUnaligned(NopInstr, Addr + 12, 4);
892  return Addr;
893  } else if (IsMipsN64ABI) {
894  // 0: 3c190000 lui t9,%highest(addr).
895  // 4: 67390000 daddiu t9,t9,%higher(addr).
896  // 8: 0019CC38 dsll t9,t9,16.
897  // c: 67390000 daddiu t9,t9,%hi(addr).
898  // 10: 0019CC38 dsll t9,t9,16.
899  // 14: 67390000 daddiu t9,t9,%lo(addr).
900  // 18: 03200008 jr t9.
901  // 1c: 00000000 nop.
902  const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000,
903  DsllT9Instr = 0x19CC38;
904  const unsigned NopInstr = 0x0;
905  unsigned JrT9Instr = 0x03200008;
906  if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6)
907  JrT9Instr = 0x03200009;
908 
909  writeBytesUnaligned(LuiT9Instr, Addr, 4);
910  writeBytesUnaligned(DaddiuT9Instr, Addr + 4, 4);
911  writeBytesUnaligned(DsllT9Instr, Addr + 8, 4);
912  writeBytesUnaligned(DaddiuT9Instr, Addr + 12, 4);
913  writeBytesUnaligned(DsllT9Instr, Addr + 16, 4);
914  writeBytesUnaligned(DaddiuT9Instr, Addr + 20, 4);
915  writeBytesUnaligned(JrT9Instr, Addr + 24, 4);
916  writeBytesUnaligned(NopInstr, Addr + 28, 4);
917  return Addr;
918  } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) {
919  // Depending on which version of the ELF ABI is in use, we need to
920  // generate one of two variants of the stub. They both start with
921  // the same sequence to load the target address into r12.
922  writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr)
923  writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr)
924  writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32
925  writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr)
926  writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr)
927  if (AbiVariant == 2) {
928  // PowerPC64 stub ELFv2 ABI: The address points to the function itself.
929  // The address is already in r12 as required by the ABI. Branch to it.
930  writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1)
931  writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12
932  writeInt32BE(Addr+28, 0x4E800420); // bctr
933  } else {
934  // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor.
935  // Load the function address on r11 and sets it to control register. Also
936  // loads the function TOC in r2 and environment pointer to r11.
937  writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1)
938  writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12)
939  writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12)
940  writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11
941  writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2)
942  writeInt32BE(Addr+40, 0x4E800420); // bctr
943  }
944  return Addr;
945  } else if (Arch == Triple::systemz) {
946  writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8
947  writeInt16BE(Addr+2, 0x0000);
948  writeInt16BE(Addr+4, 0x0004);
949  writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1
950  // 8-byte address stored at Addr + 8
951  return Addr;
952  } else if (Arch == Triple::x86_64) {
953  *Addr = 0xFF; // jmp
954  *(Addr+1) = 0x25; // rip
955  // 32-bit PC-relative address of the GOT entry will be stored at Addr+2
956  } else if (Arch == Triple::x86) {
957  *Addr = 0xE9; // 32-bit pc-relative jump.
958  }
959  return Addr;
960 }
961 
962 // Assign an address to a symbol name and resolve all the relocations
963 // associated with it.
965  uint64_t Addr) {
966  // The address to use for relocation resolution is not
967  // the address of the local section buffer. We must be doing
968  // a remote execution environment of some sort. Relocations can't
969  // be applied until all the sections have been moved. The client must
970  // trigger this with a call to MCJIT::finalize() or
971  // RuntimeDyld::resolveRelocations().
972  //
973  // Addr is a uint64_t because we can't assume the pointer width
974  // of the target is the same as that of the host. Just use a generic
975  // "big enough" type.
976  LLVM_DEBUG(
977  dbgs() << "Reassigning address for section " << SectionID << " ("
978  << Sections[SectionID].getName() << "): "
979  << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress())
980  << " -> " << format("0x%016" PRIx64, Addr) << "\n");
981  Sections[SectionID].setLoadAddress(Addr);
982 }
983 
985  uint64_t Value) {
986  for (unsigned i = 0, e = Relocs.size(); i != e; ++i) {
987  const RelocationEntry &RE = Relocs[i];
988  // Ignore relocations for sections that were not loaded
989  if (Sections[RE.SectionID].getAddress() == nullptr)
990  continue;
991  resolveRelocation(RE, Value);
992  }
993 }
994 
996  StringMap<JITEvaluatedSymbol> ExternalSymbolMap;
997 
998  // Resolution can trigger emission of more symbols, so iterate until
999  // we've resolved *everything*.
1000  {
1001  JITSymbolResolver::LookupSet ResolvedSymbols;
1002 
1003  while (true) {
1004  JITSymbolResolver::LookupSet NewSymbols;
1005 
1006  for (auto &RelocKV : ExternalSymbolRelocations) {
1007  StringRef Name = RelocKV.first();
1008  if (!Name.empty() && !GlobalSymbolTable.count(Name) &&
1009  !ResolvedSymbols.count(Name))
1010  NewSymbols.insert(Name);
1011  }
1012 
1013  if (NewSymbols.empty())
1014  break;
1015 
1016  auto NewResolverResults = Resolver.lookup(NewSymbols);
1017  if (!NewResolverResults)
1018  return NewResolverResults.takeError();
1019 
1020  assert(NewResolverResults->size() == NewSymbols.size() &&
1021  "Should have errored on unresolved symbols");
1022 
1023  for (auto &RRKV : *NewResolverResults) {
1024  assert(!ResolvedSymbols.count(RRKV.first) && "Redundant resolution?");
1025  ExternalSymbolMap.insert(RRKV);
1026  ResolvedSymbols.insert(RRKV.first);
1027  }
1028  }
1029  }
1030 
1031  while (!ExternalSymbolRelocations.empty()) {
1032 
1033  StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin();
1034 
1035  StringRef Name = i->first();
1036  if (Name.size() == 0) {
1037  // This is an absolute symbol, use an address of zero.
1038  LLVM_DEBUG(dbgs() << "Resolving absolute relocations."
1039  << "\n");
1040  RelocationList &Relocs = i->second;
1041  resolveRelocationList(Relocs, 0);
1042  } else {
1043  uint64_t Addr = 0;
1044  JITSymbolFlags Flags;
1045  RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name);
1046  if (Loc == GlobalSymbolTable.end()) {
1047  auto RRI = ExternalSymbolMap.find(Name);
1048  assert(RRI != ExternalSymbolMap.end() && "No result for symbol");
1049  Addr = RRI->second.getAddress();
1050  Flags = RRI->second.getFlags();
1051  // The call to getSymbolAddress may have caused additional modules to
1052  // be loaded, which may have added new entries to the
1053  // ExternalSymbolRelocations map. Consquently, we need to update our
1054  // iterator. This is also why retrieval of the relocation list
1055  // associated with this symbol is deferred until below this point.
1056  // New entries may have been added to the relocation list.
1057  i = ExternalSymbolRelocations.find(Name);
1058  } else {
1059  // We found the symbol in our global table. It was probably in a
1060  // Module that we loaded previously.
1061  const auto &SymInfo = Loc->second;
1062  Addr = getSectionLoadAddress(SymInfo.getSectionID()) +
1063  SymInfo.getOffset();
1064  Flags = SymInfo.getFlags();
1065  }
1066 
1067  // FIXME: Implement error handling that doesn't kill the host program!
1068  if (!Addr)
1069  report_fatal_error("Program used external function '" + Name +
1070  "' which could not be resolved!");
1071 
1072  // If Resolver returned UINT64_MAX, the client wants to handle this symbol
1073  // manually and we shouldn't resolve its relocations.
1074  if (Addr != UINT64_MAX) {
1075 
1076  // Tweak the address based on the symbol flags if necessary.
1077  // For example, this is used by RuntimeDyldMachOARM to toggle the low bit
1078  // if the target symbol is Thumb.
1079  Addr = modifyAddressBasedOnFlags(Addr, Flags);
1080 
1081  LLVM_DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t"
1082  << format("0x%lx", Addr) << "\n");
1083  // This list may have been updated when we called getSymbolAddress, so
1084  // don't change this code to get the list earlier.
1085  RelocationList &Relocs = i->second;
1086  resolveRelocationList(Relocs, Addr);
1087  }
1088  }
1089 
1090  ExternalSymbolRelocations.erase(i);
1091  }
1092 
1093  return Error::success();
1094 }
1095 
1096 //===----------------------------------------------------------------------===//
1097 // RuntimeDyld class implementation
1098 
1100  const object::SectionRef &Sec) const {
1101 
1102  auto I = ObjSecToIDMap.find(Sec);
1103  if (I != ObjSecToIDMap.end())
1104  return RTDyld.Sections[I->second].getLoadAddress();
1105 
1106  return 0;
1107 }
1108 
1109 void RuntimeDyld::MemoryManager::anchor() {}
1110 void JITSymbolResolver::anchor() {}
1111 void LegacyJITSymbolResolver::anchor() {}
1112 
1115  : MemMgr(MemMgr), Resolver(Resolver) {
1116  // FIXME: There's a potential issue lurking here if a single instance of
1117  // RuntimeDyld is used to load multiple objects. The current implementation
1118  // associates a single memory manager with a RuntimeDyld instance. Even
1119  // though the public class spawns a new 'impl' instance for each load,
1120  // they share a single memory manager. This can become a problem when page
1121  // permissions are applied.
1122  Dyld = nullptr;
1123  ProcessAllSections = false;
1124  Checker = nullptr;
1125 }
1126 
1128 
1129 static std::unique_ptr<RuntimeDyldCOFF>
1131  JITSymbolResolver &Resolver, bool ProcessAllSections,
1132  RuntimeDyldCheckerImpl *Checker) {
1133  std::unique_ptr<RuntimeDyldCOFF> Dyld =
1134  RuntimeDyldCOFF::create(Arch, MM, Resolver);
1135  Dyld->setProcessAllSections(ProcessAllSections);
1136  Dyld->setRuntimeDyldChecker(Checker);
1137  return Dyld;
1138 }
1139 
1140 static std::unique_ptr<RuntimeDyldELF>
1142  JITSymbolResolver &Resolver, bool ProcessAllSections,
1143  RuntimeDyldCheckerImpl *Checker) {
1144  std::unique_ptr<RuntimeDyldELF> Dyld =
1145  RuntimeDyldELF::create(Arch, MM, Resolver);
1146  Dyld->setProcessAllSections(ProcessAllSections);
1147  Dyld->setRuntimeDyldChecker(Checker);
1148  return Dyld;
1149 }
1150 
1151 static std::unique_ptr<RuntimeDyldMachO>
1154  bool ProcessAllSections,
1155  RuntimeDyldCheckerImpl *Checker) {
1156  std::unique_ptr<RuntimeDyldMachO> Dyld =
1157  RuntimeDyldMachO::create(Arch, MM, Resolver);
1158  Dyld->setProcessAllSections(ProcessAllSections);
1159  Dyld->setRuntimeDyldChecker(Checker);
1160  return Dyld;
1161 }
1162 
1163 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
1165  if (!Dyld) {
1166  if (Obj.isELF())
1167  Dyld =
1168  createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()),
1169  MemMgr, Resolver, ProcessAllSections, Checker);
1170  else if (Obj.isMachO())
1171  Dyld = createRuntimeDyldMachO(
1172  static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1173  ProcessAllSections, Checker);
1174  else if (Obj.isCOFF())
1175  Dyld = createRuntimeDyldCOFF(
1176  static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver,
1177  ProcessAllSections, Checker);
1178  else
1179  report_fatal_error("Incompatible object format!");
1180  }
1181 
1182  if (!Dyld->isCompatibleFile(Obj))
1183  report_fatal_error("Incompatible object format!");
1184 
1185  auto LoadedObjInfo = Dyld->loadObject(Obj);
1186  MemMgr.notifyObjectLoaded(*this, Obj);
1187  return LoadedObjInfo;
1188 }
1189 
1191  if (!Dyld)
1192  return nullptr;
1193  return Dyld->getSymbolLocalAddress(Name);
1194 }
1195 
1197  if (!Dyld)
1198  return nullptr;
1199  return Dyld->getSymbol(Name);
1200 }
1201 
1202 std::map<StringRef, JITEvaluatedSymbol> RuntimeDyld::getSymbolTable() const {
1203  if (!Dyld)
1204  return std::map<StringRef, JITEvaluatedSymbol>();
1205  return Dyld->getSymbolTable();
1206 }
1207 
1208 void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); }
1209 
1210 void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) {
1211  Dyld->reassignSectionAddress(SectionID, Addr);
1212 }
1213 
1214 void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
1215  uint64_t TargetAddress) {
1216  Dyld->mapSectionAddress(LocalAddress, TargetAddress);
1217 }
1218 
1219 bool RuntimeDyld::hasError() { return Dyld->hasError(); }
1220 
1221 StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); }
1222 
1224  bool MemoryFinalizationLocked = MemMgr.FinalizationLocked;
1225  MemMgr.FinalizationLocked = true;
1227  registerEHFrames();
1228  if (!MemoryFinalizationLocked) {
1229  MemMgr.finalizeMemory();
1230  MemMgr.FinalizationLocked = false;
1231  }
1232 }
1233 
1235  if (Dyld)
1236  Dyld->registerEHFrames();
1237 }
1238 
1240  if (Dyld)
1241  Dyld->deregisterEHFrames();
1242 }
1243 
1244 } // end namespace llvm
std::map< StringRef, JITSymbolFlags > LookupFlagsResult
Definition: JITSymbol.h:296
RelocationEntry - used to represent relocations internally in the dynamic linker. ...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
std::unique_ptr< LoadedObjectInfo > loadObject(const object::ObjectFile &O)
Add the referenced object file to the list of objects to be loaded and relocated. ...
void registerEHFrames()
Register any EH frame sections that have been loaded but not previously registered with the memory ma...
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:115
bool isCommon() const
Returns true if the Common flag is set.
Definition: JITSymbol.h:98
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
StringRef getFileName() const
Definition: Binary.cpp:41
uint64_t readBytesUnaligned(uint8_t *Src, unsigned Size) const
Endian-aware read Read the least significant Size bytes from Src.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
StringRef getErrorString()
unsigned computeGOTSize(const ObjectFile &Obj)
static bool isReadOnlyData(const SectionRef Section)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition: RuntimeDyld.cpp:62
iterator find(StringRef Key)
Definition: StringMap.h:333
This class is the base class for all object file types.
Definition: ObjectFile.h:190
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:685
uint8_t * getAddress() const
Error takeError()
Take ownership of the stored error.
Definition: Error.h:545
std::set< StringRef > LookupSet
Definition: JITSymbol.h:294
JITEvaluatedSymbol getSymbol(StringRef Name) const
Get the target address and flags for the named symbol.
uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const override
Obtain the Load Address of a section by SectionRef.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
static std::unique_ptr< RuntimeDyldELF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
virtual basic_symbol_iterator symbol_begin() const =0
bool isWeak() const
Returns true if the Weak flag is set.
Definition: JITSymbol.h:93
RuntimeDyldErrorCode
Definition: RuntimeDyld.cpp:33
void writeBytesUnaligned(uint64_t Value, uint8_t *Dst, unsigned Size) const
Endian-aware write.
bool isCOFF() const
Definition: Binary.h:117
unsigned SectionID
SectionID - the section this relocation points to.
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:955
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
uint64_t getAddress() const
Definition: ObjectFile.h:404
void resolveRelocationList(const RelocationList &Relocs, uint64_t Value)
Resolves relocations from Relocs list with address from Value.
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
static StringRef getName(Value *V)
static bool isRequiredForExecution(const SectionRef Section)
std::map< RelocationValueRef, uintptr_t > StubMap
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
virtual void registerEHFrames()
Definition: RuntimeDyld.cpp:74
static std::unique_ptr< RuntimeDyldMachO > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Create a RuntimeDyldMachO instance for the given target architecture.
support::ulittle32_t VirtualSize
Definition: COFF.h:440
static uint64_t computeAllocationSizeForSections(std::vector< uint64_t > &SectionSizes, uint64_t Alignment)
#define UINT64_MAX
Definition: DataTypes.h:83
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
virtual ~RuntimeDyldImpl()
Definition: RuntimeDyld.cpp:67
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
const ObjectFile * getObject() const
Definition: ObjectFile.h:468
RuntimeDyld(MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Construct a RuntimeDyld instance.
Instances of this class acquire a given Mutex Lock when constructed and hold that lock until destruct...
Definition: MutexGuard.h:27
support::ulittle32_t Characteristics
Definition: COFF.h:448
void addRelocationForSymbol(const RelocationEntry &RE, StringRef SymbolName)
virtual bool finalizeMemory(std::string *ErrMsg=nullptr)=0
This method is called when object loading is complete and section page permissions can be applied...
void * getSymbolLocalAddress(StringRef Name) const
Get the address of our local copy of the symbol.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 gigabytes).
Definition: MachO.h:148
virtual void notifyObjectLoaded(RuntimeDyld &RTDyld, const object::ObjectFile &Obj)
This method is called after an object has been loaded into memory but before relocations are applied ...
Definition: RuntimeDyld.h:164
bool isVirtual() const
Definition: ObjectFile.h:440
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
void addRelocationForSection(const RelocationEntry &RE, unsigned SectionID)
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:1774
static ManagedStatic< _object_error_category > error_category
Definition: Error.cpp:74
Error computeTotalAllocSize(const ObjectFile &Obj, uint64_t &CodeSize, uint32_t &CodeAlign, uint64_t &RODataSize, uint32_t &RODataAlign, uint64_t &RWDataSize, uint32_t &RWDataAlign)
uint64_t getSize() const
Definition: ObjectFile.h:412
Flags for symbols in the JIT.
Definition: JITSymbol.h:43
Symbol resolution interface.
Definition: JITSymbol.h:292
static JITSymbolFlags fromObjectSymbol(const object::BasicSymbolRef &Symbol)
Construct a JITSymbolFlags value based on the flags of the given libobject symbol.
Definition: JITSymbol.cpp:32
std::vector< SymbolRef > CommonSymbolList
virtual basic_symbol_iterator symbol_end() const =0
virtual JITSymbolFlags getJITSymbolFlags(const BasicSymbolRef &Sym)
Generate JITSymbolFlags from a libObject symbol.
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition: RuntimeDyld.cpp:58
Expected< unsigned > findOrEmitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode, ObjSectionToIDMap &LocalSections)
Find Section in LocalSections.
support::ulittle32_t SizeOfRawData
Definition: COFF.h:442
Expected< uint64_t > getAddress() const
Returns the symbol virtual address (i.e.
Definition: ObjectFile.h:349
Error emitCommonSymbols(const ObjectFile &Obj, CommonSymbolList &CommonSymbols, uint64_t CommonSize, uint32_t CommonAlign)
Given the common symbols discovered in the object file, emit a new section for them and update the sy...
bool isELF() const
Definition: Binary.h:109
size_t size() const
Definition: SmallVector.h:53
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isLittleEndian() const
Definition: Binary.h:131
static void dumpSectionMemory(const SectionEntry &S, StringRef State)
Definition: RuntimeDyld.cpp:81
static ErrorSuccess success()
Create a success value.
Definition: Error.h:321
unsigned computeSectionStubBufSize(const ObjectFile &Obj, const SectionRef &Section)
virtual section_iterator section_begin() const =0
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
int64_t Addend
Addend - the relocation addend encoded in the instruction itself.
std::error_code getName(StringRef &Result) const
Definition: ObjectFile.h:400
virtual Triple::ArchType getArch() const =0
std::error_code getContents(StringRef &Result) const
Definition: ObjectFile.h:416
uint8_t * createStubFunction(uint8_t *Addr, unsigned AbiVariant=0)
Emits long jump instruction to Addr.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:366
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
Symbol info for RuntimeDyld.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
static std::unique_ptr< RuntimeDyldCOFF > createRuntimeDyldCOFF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker)
void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
Represents a symbol that has been evaluated to an address already.
Definition: JITSymbol.h:157
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:129
size_t getSize() const
static std::unique_ptr< RuntimeDyldELF > createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker)
static bool isZeroInit(const SectionRef Section)
std::map< StringRef, JITEvaluatedSymbol > getSymbolTable() const
Returns a copy of the symbol table.
symbol_iterator_range symbols() const
Definition: ObjectFile.h:270
virtual section_iterator section_end() const =0
std::map< SectionRef, unsigned > ObjSectionToIDMap
void finalizeWithMemoryManagerLocking()
Perform all actions needed to make the code owned by this RuntimeDyld instance executable: ...
static std::unique_ptr< RuntimeDyldCOFF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
#define I(x, y, z)
Definition: MD5.cpp:58
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:99
uint64_t getLoadAddress() const
Expected< unsigned > emitSection(const ObjectFile &Obj, const SectionRef &Section, bool IsCode)
Emits section data from the object file to the MemoryManager.
uint64_t getAlignment() const
Get the alignment of this section as the actual value (not log 2).
Definition: ObjectFile.h:420
static std::unique_ptr< RuntimeDyldMachO > createRuntimeDyldMachO(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver, bool ProcessAllSections, RuntimeDyldCheckerImpl *Checker)
void resolveRelocations()
Resolve the relocations for all symbols we currently know about.
StringRef getName() const
SymInfo contains information about symbol: it&#39;s address and section index which is -1LL for absolute ...
SectionEntry - represents a section emitted into memory by the dynamic linker.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
static const char * name
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: MathExtras.h:727
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
void reassignSectionAddress(unsigned SectionID, uint64_t Addr)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Expected< ObjSectionToIDMap > loadObjectImpl(const object::ObjectFile &Obj)
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
bool isMachO() const
Definition: Binary.h:113
S_ZEROFILL - Zero fill on demand section.
Definition: MachO.h:122
Error resolveExternalSymbols()
Resolve relocations to external symbols.
void mapSectionAddress(const void *LocalAddress, uint64_t TargetAddress)
Map a section to its target address space value.
#define LLVM_DEBUG(X)
Definition: Debug.h:119
SectionType
These are the section type and attributes fields.
Definition: MachO.h:115
iterator end()
Definition: StringMap.h:318
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81