LLVM  13.0.0git
RuntimeDyldCOFF.cpp
Go to the documentation of this file.
1 //===-- RuntimeDyldCOFF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implementation of COFF support for the MC-JIT runtime dynamic linker.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "RuntimeDyldCOFF.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/Object/ObjectFile.h"
22 
23 using namespace llvm;
24 using namespace llvm::object;
25 
26 #define DEBUG_TYPE "dyld"
27 
28 namespace {
29 
30 class LoadedCOFFObjectInfo final
31  : public LoadedObjectInfoHelper<LoadedCOFFObjectInfo,
32  RuntimeDyld::LoadedObjectInfo> {
33 public:
34  LoadedCOFFObjectInfo(
35  RuntimeDyldImpl &RTDyld,
37  : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
38 
40  getObjectForDebug(const ObjectFile &Obj) const override {
41  return OwningBinary<ObjectFile>();
42  }
43 };
44 }
45 
46 namespace llvm {
47 
48 std::unique_ptr<RuntimeDyldCOFF>
52  switch (Arch) {
53  default: llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
54  case Triple::x86:
55  return std::make_unique<RuntimeDyldCOFFI386>(MemMgr, Resolver);
56  case Triple::thumb:
57  return std::make_unique<RuntimeDyldCOFFThumb>(MemMgr, Resolver);
58  case Triple::x86_64:
59  return std::make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver);
60  case Triple::aarch64:
61  return std::make_unique<RuntimeDyldCOFFAArch64>(MemMgr, Resolver);
62  }
63 }
64 
65 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
67  if (auto ObjSectionToIDOrErr = loadObjectImpl(O)) {
68  return std::make_unique<LoadedCOFFObjectInfo>(*this, *ObjSectionToIDOrErr);
69  } else {
70  HasError = true;
71  raw_string_ostream ErrStream(ErrorStr);
72  logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
73  return nullptr;
74  }
75 }
76 
78  // The value in a relocatable COFF object is the offset.
79  return cantFail(Sym.getValue());
80 }
81 
82 uint64_t RuntimeDyldCOFF::getDLLImportOffset(unsigned SectionID, StubMap &Stubs,
84  bool SetSectionIDMinus1) {
85  LLVM_DEBUG(dbgs() << "Getting DLLImport entry for " << Name << "... ");
86  assert(Name.startswith(getImportSymbolPrefix()) && "Not a DLLImport symbol?");
87  RelocationValueRef Reloc;
88  Reloc.SymbolName = Name.data();
89  auto I = Stubs.find(Reloc);
90  if (I != Stubs.end()) {
91  LLVM_DEBUG(dbgs() << format("{0:x8}", I->second) << "\n");
92  return I->second;
93  }
94 
95  assert(SectionID < Sections.size() && "SectionID out of range");
96  auto &Sec = Sections[SectionID];
97  auto EntryOffset = alignTo(Sec.getStubOffset(), PointerSize);
98  Sec.advanceStubOffset(EntryOffset + PointerSize - Sec.getStubOffset());
99  Stubs[Reloc] = EntryOffset;
100 
101  RelocationEntry RE(SectionID, EntryOffset, PointerReloc, 0, false,
103  // Hack to tell I386/Thumb resolveRelocation that this isn't section relative.
104  if (SetSectionIDMinus1)
105  RE.Sections.SectionA = -1;
106  addRelocationForSymbol(RE, Name.drop_front(getImportSymbolPrefix().size()));
107 
108  LLVM_DEBUG({
109  dbgs() << "Creating entry at "
110  << formatv("{0:x16} + {1:x8} ( {2:x16} )", Sec.getLoadAddress(),
111  EntryOffset, Sec.getLoadAddress() + EntryOffset)
112  << "\n";
113  });
114  return EntryOffset;
115 }
116 
118  return Obj.isCOFF();
119 }
120 
121 } // namespace llvm
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm::RuntimeDyldCOFF::loadObject
std::unique_ptr< RuntimeDyld::LoadedObjectInfo > loadObject(const object::ObjectFile &Obj) override
Definition: RuntimeDyldCOFF.cpp:66
llvm
Definition: AllocatorList.h:23
llvm::object::SymbolRef::getValue
Expected< uint64_t > getValue() const
Return the value of the symbol depending on the object this can be an offset or a virtual address.
Definition: ObjectFile.h:397
llvm::RelocationEntry::SectionPair::SectionA
uint32_t SectionA
Definition: RuntimeDyldImpl.h:133
llvm::Triple::x86
@ x86
Definition: Triple.h:83
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:614
llvm::LoadedObjectInfoHelper
Definition: DIContext.h:307
RuntimeDyldCOFFThumb.h
llvm::Triple::x86_64
@ x86_64
Definition: Triple.h:84
llvm::RuntimeDyldImpl
Definition: RuntimeDyldImpl.h:238
STLExtras.h
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:122
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
llvm::Resolver
Interface for looking up the initializer for a variable name, used by Init::resolveReferences.
Definition: Record.h:2000
llvm::formatv
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
Definition: FormatVariadic.h:250
llvm::object
Definition: ObjectFileTransformer.h:18
llvm::Log2_64
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:602
llvm::Triple::ArchType
ArchType
Definition: Triple.h:47
FormatVariadic.h
llvm::RelocationEntry
RelocationEntry - used to represent relocations internally in the dynamic linker.
Definition: RuntimeDyldImpl.h:117
llvm::RuntimeDyldCOFF::isCompatibleFile
bool isCompatibleFile(const object::ObjectFile &Obj) const override
Definition: RuntimeDyldCOFF.cpp:117
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:179
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
RuntimeDyldCOFFX86_64.h
I
#define I(x, y, z)
Definition: MD5.cpp:59
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFile.h
RuntimeDyldCOFFI386.h
Triple.h
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1463
llvm::RuntimeDyldCOFF::getDLLImportOffset
uint64_t getDLLImportOffset(unsigned SectionID, StubMap &Stubs, StringRef Name, bool SetSectionIDMinus1=false)
Definition: RuntimeDyldCOFF.cpp:82
llvm::object::Binary::isCOFF
bool isCOFF() const
Definition: Binary.h:128
llvm::RuntimeDyldCOFF::getSymbolOffset
uint64_t getSymbolOffset(const SymbolRef &Sym)
Definition: RuntimeDyldCOFF.cpp:77
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
llvm::cantFail
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition: Error.h:708
llvm::Triple::thumb
@ thumb
Definition: Triple.h:81
llvm::format
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
llvm::logAllUnhandledErrors
void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
Definition: Error.cpp:61
llvm::RuntimeDyld::LoadedObjectInfo::ObjSectionToIDMap
std::map< object::SectionRef, unsigned > ObjSectionToIDMap
Definition: RuntimeDyld.h:73
llvm::JITSymbolResolver
Symbol resolution interface.
Definition: JITSymbol.h:371
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
RuntimeDyldCOFF.h
llvm::RelocationValueRef::SymbolName
const char * SymbolName
Definition: RuntimeDyldImpl.h:195
llvm::RelocationEntry::Sections
SectionPair Sections
Definition: RuntimeDyldImpl.h:141
llvm::object::SymbolRef
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: ObjectFile.h:167
llvm::RuntimeDyldImpl::StubMap
std::map< RelocationValueRef, uintptr_t > StubMap
Definition: RuntimeDyldImpl.h:286
llvm::RelocationValueRef
Definition: RuntimeDyldImpl.h:190
RuntimeDyldCOFFAArch64.h
llvm::Triple::aarch64
@ aarch64
Definition: Triple.h:52
llvm::RuntimeDyldCOFF::create
static std::unique_ptr< RuntimeDyldCOFF > create(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver)
Definition: RuntimeDyldCOFF.cpp:49
llvm::RuntimeDyld::MemoryManager
Memory Management.
Definition: RuntimeDyld.h:92