David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 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 | |
Tobias Grosser | 87fc5f8 | 2015-05-22 06:01:04 +0000 | [diff] [blame] | 27 | class LoadedCOFFObjectInfo : public RuntimeDyld::LoadedObjectInfo { |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 28 | public: |
| 29 | LoadedCOFFObjectInfo(RuntimeDyldImpl &RTDyld, unsigned BeginIdx, |
| 30 | unsigned EndIdx) |
Tobias Grosser | 87fc5f8 | 2015-05-22 06:01:04 +0000 | [diff] [blame] | 31 | : RuntimeDyld::LoadedObjectInfo(RTDyld, BeginIdx, EndIdx) {} |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 32 | |
| 33 | OwningBinary<ObjectFile> |
| 34 | getObjectForDebug(const ObjectFile &Obj) const override { |
| 35 | return OwningBinary<ObjectFile>(); |
| 36 | } |
Tobias Grosser | 87fc5f8 | 2015-05-22 06:01:04 +0000 | [diff] [blame] | 37 | |
| 38 | RuntimeDyld::LoadedObjectInfo *clone() const { return new LoadedCOFFObjectInfo(*this); } |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 39 | }; |
| 40 | } |
| 41 | |
| 42 | namespace llvm { |
| 43 | |
| 44 | std::unique_ptr<RuntimeDyldCOFF> |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 45 | llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch, |
| 46 | RuntimeDyld::MemoryManager &MemMgr, |
| 47 | RuntimeDyld::SymbolResolver &Resolver) { |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 48 | switch (Arch) { |
| 49 | default: |
| 50 | llvm_unreachable("Unsupported target for RuntimeDyldCOFF."); |
| 51 | break; |
| 52 | case Triple::x86_64: |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 53 | return make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver); |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 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; |
David Majnemer | b654b55 | 2015-03-07 20:56:50 +0000 | [diff] [blame] | 67 | if (Sym.getAddress(Address)) |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 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()); |
David Majnemer | b654b55 | 2015-03-07 20:56:50 +0000 | [diff] [blame] | 75 | if (Sym.getSection(SecI)) |
David Majnemer | 1a666e0 | 2015-03-07 20:21:27 +0000 | [diff] [blame] | 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 |