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:
LoadedCOFFObjectInfo(RuntimeDyldImpl & RTDyld,unsigned BeginIdx,unsigned EndIdx)29 LoadedCOFFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx,
30 unsigned EndIdx)
31 : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {}
32
33 OwningBinary<ObjectFile>
getObjectForDebug(const ObjectFile & Obj) const34 getObjectForDebug(const ObjectFile &Obj) const override {
35 return OwningBinary<ObjectFile>();
36 }
37 };
38 }
39
40 namespace llvm {
41
42 std::unique_ptr<RuntimeDyldCOFF>
create(Triple::ArchType Arch,RuntimeDyld::MemoryManager & MemMgr,RuntimeDyld::SymbolResolver & Resolver)43 llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch,
44 RuntimeDyld::MemoryManager &MemMgr,
45 RuntimeDyld::SymbolResolver &Resolver) {
46 switch (Arch) {
47 default:
48 llvm_unreachable("Unsupported target for RuntimeDyldCOFF.");
49 break;
50 case Triple::x86_64:
51 return make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver);
52 }
53 }
54
55 std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
loadObject(const object::ObjectFile & O)56 RuntimeDyldCOFF::loadObject(const object::ObjectFile &O) {
57 unsigned SectionStartIdx, SectionEndIdx;
58 std::tie(SectionStartIdx, SectionEndIdx) = loadObjectImpl(O);
59 return llvm::make_unique<LoadedCOFFObjectInfo>(*this, SectionStartIdx,
60 SectionEndIdx);
61 }
62
getSymbolOffset(const SymbolRef & Sym)63 uint64_t RuntimeDyldCOFF::getSymbolOffset(const SymbolRef &Sym) {
64 uint64_t Address;
65 if (Sym.getAddress(Address))
66 return UnknownAddressOrSize;
67
68 if (Address == UnknownAddressOrSize)
69 return UnknownAddressOrSize;
70
71 const ObjectFile *Obj = Sym.getObject();
72 section_iterator SecI(Obj->section_end());
73 if (Sym.getSection(SecI))
74 return UnknownAddressOrSize;
75
76 if (SecI == Obj->section_end())
77 return UnknownAddressOrSize;
78
79 uint64_t SectionAddress = SecI->getAddress();
80 return Address - SectionAddress;
81 }
82
isCompatibleFile(const object::ObjectFile & Obj) const83 bool RuntimeDyldCOFF::isCompatibleFile(const object::ObjectFile &Obj) const {
84 return Obj.isCOFF();
85 }
86
87 } // namespace llvm
88