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