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