Make it easier to use DwarfContext with MCJIT
[oota-llvm.git] / lib / ExecutionEngine / RuntimeDyld / RuntimeDyldCOFF.cpp
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/RuntimeDyldCOFFX86_64.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Object/ObjectFile.h"
19
20 using namespace llvm;
21 using namespace llvm::object;
22
23 #define DEBUG_TYPE "dyld"
24
25 namespace {
26
27 class LoadedCOFFObjectInfo : public RuntimeDyld::LoadedObjectInfo {
28 public:
29   LoadedCOFFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
30                        unsigned EndIdx)
31       : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
32
33   OwningBinary<ObjectFile>
34   getObjectForDebug(const ObjectFile &Obj) const override {
35     return OwningBinary<ObjectFile>();
36   }
37
38   RuntimeDyld::LoadedObjectInfo *clone() const { return new LoadedCOFFObjectInfo(*this); }
39 };
40 }
41
42 namespace llvm {
43
44 std::unique_ptr<RuntimeDyldCOFF>
45 llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch,
46                               RuntimeDyld::MemoryManager &MemMgr,
47                               RuntimeDyld::SymbolResolver &Resolver) {
48   switch (Arch) {
49   default:
50     llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
51     break;
52   case Triple::x86_64:
53     return make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver);
54   }
55 }
56
57 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
58 RuntimeDyldCOFF::loadObject(const object::ObjectFile &O) {
59   unsigned SectionStartIdx, SectionEndIdx;
60   std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
61   return llvm::make_unique<LoadedCOFFObjectInfo>(*this, SectionStartIdx,
62                                                  SectionEndIdx);
63 }
64
65 uint64_t RuntimeDyldCOFF::getSymbolOffset(const SymbolRef &Sym) {
66   uint64_t Address;
67   if (Sym.getAddress(Address))
68     return UnknownAddressOrSize;
69
70   if (Address == UnknownAddressOrSize)
71     return UnknownAddressOrSize;
72
73   const ObjectFile *Obj = Sym.getObject();
74   section_iterator SecI(Obj->section_end());
75   if (Sym.getSection(SecI))
76     return UnknownAddressOrSize;
77
78   if (SecI == Obj->section_end())
79     return UnknownAddressOrSize;
80
81   uint64_t SectionAddress = SecI->getAddress();
82   return Address - SectionAddress;
83 }
84
85 bool RuntimeDyldCOFF::isCompatibleFile(const object::ObjectFile &Obj) const {
86   return Obj.isCOFF();
87 }
88
89 } // namespace llvm