10b57cec5SDimitry Andric //===-- RuntimeDyldCOFF.cpp - Run-time dynamic linker for MC-JIT -*- C++ -*-==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // Implementation of COFF support for the MC-JIT runtime dynamic linker. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "RuntimeDyldCOFF.h" 14480093f4SDimitry Andric #include "Targets/RuntimeDyldCOFFAArch64.h" 150b57cec5SDimitry Andric #include "Targets/RuntimeDyldCOFFI386.h" 160b57cec5SDimitry Andric #include "Targets/RuntimeDyldCOFFThumb.h" 170b57cec5SDimitry Andric #include "Targets/RuntimeDyldCOFFX86_64.h" 180b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 190b57cec5SDimitry Andric #include "llvm/ADT/Triple.h" 200b57cec5SDimitry Andric #include "llvm/Object/ObjectFile.h" 21*5ffd83dbSDimitry Andric #include "llvm/Support/FormatVariadic.h" 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric using namespace llvm; 240b57cec5SDimitry Andric using namespace llvm::object; 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric #define DEBUG_TYPE "dyld" 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric namespace { 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric class LoadedCOFFObjectInfo final 310b57cec5SDimitry Andric : public LoadedObjectInfoHelper<LoadedCOFFObjectInfo, 320b57cec5SDimitry Andric RuntimeDyld::LoadedObjectInfo> { 330b57cec5SDimitry Andric public: 340b57cec5SDimitry Andric LoadedCOFFObjectInfo( 350b57cec5SDimitry Andric RuntimeDyldImpl &RTDyld, 360b57cec5SDimitry Andric RuntimeDyld::LoadedObjectInfo::ObjSectionToIDMap ObjSecToIDMap) 370b57cec5SDimitry Andric : LoadedObjectInfoHelper(RTDyld, std::move(ObjSecToIDMap)) {} 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric OwningBinary<ObjectFile> 400b57cec5SDimitry Andric getObjectForDebug(const ObjectFile &Obj) const override { 410b57cec5SDimitry Andric return OwningBinary<ObjectFile>(); 420b57cec5SDimitry Andric } 430b57cec5SDimitry Andric }; 440b57cec5SDimitry Andric } 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric namespace llvm { 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldCOFF> 490b57cec5SDimitry Andric llvm::RuntimeDyldCOFF::create(Triple::ArchType Arch, 500b57cec5SDimitry Andric RuntimeDyld::MemoryManager &MemMgr, 510b57cec5SDimitry Andric JITSymbolResolver &Resolver) { 520b57cec5SDimitry Andric switch (Arch) { 530b57cec5SDimitry Andric default: llvm_unreachable("Unsupported target for RuntimeDyldCOFF."); 540b57cec5SDimitry Andric case Triple::x86: 558bcb0991SDimitry Andric return std::make_unique<RuntimeDyldCOFFI386>(MemMgr, Resolver); 560b57cec5SDimitry Andric case Triple::thumb: 578bcb0991SDimitry Andric return std::make_unique<RuntimeDyldCOFFThumb>(MemMgr, Resolver); 580b57cec5SDimitry Andric case Triple::x86_64: 598bcb0991SDimitry Andric return std::make_unique<RuntimeDyldCOFFX86_64>(MemMgr, Resolver); 60480093f4SDimitry Andric case Triple::aarch64: 61480093f4SDimitry Andric return std::make_unique<RuntimeDyldCOFFAArch64>(MemMgr, Resolver); 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo> 660b57cec5SDimitry Andric RuntimeDyldCOFF::loadObject(const object::ObjectFile &O) { 670b57cec5SDimitry Andric if (auto ObjSectionToIDOrErr = loadObjectImpl(O)) { 688bcb0991SDimitry Andric return std::make_unique<LoadedCOFFObjectInfo>(*this, *ObjSectionToIDOrErr); 690b57cec5SDimitry Andric } else { 700b57cec5SDimitry Andric HasError = true; 710b57cec5SDimitry Andric raw_string_ostream ErrStream(ErrorStr); 720b57cec5SDimitry Andric logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream); 730b57cec5SDimitry Andric return nullptr; 740b57cec5SDimitry Andric } 750b57cec5SDimitry Andric } 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric uint64_t RuntimeDyldCOFF::getSymbolOffset(const SymbolRef &Sym) { 780b57cec5SDimitry Andric // The value in a relocatable COFF object is the offset. 79*5ffd83dbSDimitry Andric return cantFail(Sym.getValue()); 80*5ffd83dbSDimitry Andric } 81*5ffd83dbSDimitry Andric 82*5ffd83dbSDimitry Andric uint64_t RuntimeDyldCOFF::getDLLImportOffset(unsigned SectionID, StubMap &Stubs, 83*5ffd83dbSDimitry Andric StringRef Name, 84*5ffd83dbSDimitry Andric bool SetSectionIDMinus1) { 85*5ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Getting DLLImport entry for " << Name << "... "); 86*5ffd83dbSDimitry Andric assert(Name.startswith(getImportSymbolPrefix()) && "Not a DLLImport symbol?"); 87*5ffd83dbSDimitry Andric RelocationValueRef Reloc; 88*5ffd83dbSDimitry Andric Reloc.SymbolName = Name.data(); 89*5ffd83dbSDimitry Andric auto I = Stubs.find(Reloc); 90*5ffd83dbSDimitry Andric if (I != Stubs.end()) { 91*5ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << format("{0:x8}", I->second) << "\n"); 92*5ffd83dbSDimitry Andric return I->second; 93*5ffd83dbSDimitry Andric } 94*5ffd83dbSDimitry Andric 95*5ffd83dbSDimitry Andric assert(SectionID < Sections.size() && "SectionID out of range"); 96*5ffd83dbSDimitry Andric auto &Sec = Sections[SectionID]; 97*5ffd83dbSDimitry Andric auto EntryOffset = alignTo(Sec.getStubOffset(), PointerSize); 98*5ffd83dbSDimitry Andric Sec.advanceStubOffset(EntryOffset + PointerSize - Sec.getStubOffset()); 99*5ffd83dbSDimitry Andric Stubs[Reloc] = EntryOffset; 100*5ffd83dbSDimitry Andric 101*5ffd83dbSDimitry Andric RelocationEntry RE(SectionID, EntryOffset, PointerReloc, 0, false, 102*5ffd83dbSDimitry Andric Log2_64(PointerSize)); 103*5ffd83dbSDimitry Andric // Hack to tell I386/Thumb resolveRelocation that this isn't section relative. 104*5ffd83dbSDimitry Andric if (SetSectionIDMinus1) 105*5ffd83dbSDimitry Andric RE.Sections.SectionA = -1; 106*5ffd83dbSDimitry Andric addRelocationForSymbol(RE, Name.drop_front(getImportSymbolPrefix().size())); 107*5ffd83dbSDimitry Andric 108*5ffd83dbSDimitry Andric LLVM_DEBUG({ 109*5ffd83dbSDimitry Andric dbgs() << "Creating entry at " 110*5ffd83dbSDimitry Andric << formatv("{0:x16} + {1:x8} ( {2:x16} )", Sec.getLoadAddress(), 111*5ffd83dbSDimitry Andric EntryOffset, Sec.getLoadAddress() + EntryOffset) 112*5ffd83dbSDimitry Andric << "\n"; 113*5ffd83dbSDimitry Andric }); 114*5ffd83dbSDimitry Andric return EntryOffset; 1150b57cec5SDimitry Andric } 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric bool RuntimeDyldCOFF::isCompatibleFile(const object::ObjectFile &Obj) const { 1180b57cec5SDimitry Andric return Obj.isCOFF(); 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric } // namespace llvm 122