1 //===-- RuntimeDyldCOFF.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 COFF support for the MC-JIT runtime dynamic linker.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "RuntimeDyldCOFF.h"
15 #include "Targets/RuntimeDyldCOFFAArch64.h"
16 #include "Targets/RuntimeDyldCOFFI386.h"
17 #include "Targets/RuntimeDyldCOFFThumb.h"
18 #include "Targets/RuntimeDyldCOFFX86_64.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/Triple.h"
21 #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:
LoadedCOFFObjectInfo(RuntimeDyldImpl & RTDyld,RuntimeDyld::LoadedObjectInfo::ObjSectionToIDMap ObjSecToIDMap)34 LoadedCOFFObjectInfo(
35 RuntimeDyldImpl &RTDyld,
36 RuntimeDyld::LoadedObjectInfo::ObjSectionToIDMap ObjSecToIDMap)
37 : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {}
38
39 OwningBinary<ObjectFile>
getObjectForDebug(const ObjectFile & Obj) const40 getObjectForDebug(const ObjectFile &Obj) const override {
41 return OwningBinary<ObjectFile>();
42 }
43 };
44 }
45
46 namespace llvm {
47
48 std::unique_ptr<RuntimeDyldCOFF>
create(Triple::ArchType Arch,RuntimeDyld::MemoryManager & MemMgr,JITSymbolResolver & Resolver)49 llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch,
50 RuntimeDyld::MemoryManager &MemMgr,
51 JITSymbolResolver &Resolver) {
52 switch (Arch) {
53 default: llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
54 case Triple::x86:
55 return make_unique<RuntimeDyldCOFFI386>(MemMgr, Resolver);
56 case Triple::thumb:
57 return make_unique<RuntimeDyldCOFFThumb>(MemMgr, Resolver);
58 case Triple::x86_64:
59 return make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver);
60 case Triple::aarch64:
61 return make_unique<RuntimeDyldCOFFAArch64>(MemMgr, Resolver);
62 }
63 }
64
65 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
loadObject(const object::ObjectFile & O)66 RuntimeDyldCOFF::loadObject(const object::ObjectFile &O) {
67 if (auto ObjSectionToIDOrErr = loadObjectImpl(O)) {
68 return llvm::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
getSymbolOffset(const SymbolRef & Sym)77 uint64_t RuntimeDyldCOFF::getSymbolOffset(const SymbolRef &Sym) {
78 // The value in a relocatable COFF object is the offset.
79 return Sym.getValue();
80 }
81
isCompatibleFile(const object::ObjectFile & Obj) const82 bool RuntimeDyldCOFF::isCompatibleFile(const object::ObjectFile &Obj) const {
83 return Obj.isCOFF();
84 }
85
86 } // namespace llvm
87