10b57cec5SDimitry Andric //===-- RuntimeDyld.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 the MC-JIT runtime dynamic linker. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/ExecutionEngine/RuntimeDyld.h" 140b57cec5SDimitry Andric #include "RuntimeDyldCOFF.h" 150b57cec5SDimitry Andric #include "RuntimeDyldELF.h" 160b57cec5SDimitry Andric #include "RuntimeDyldImpl.h" 170b57cec5SDimitry Andric #include "RuntimeDyldMachO.h" 180b57cec5SDimitry Andric #include "llvm/Object/COFF.h" 190b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h" 208bcb0991SDimitry Andric #include "llvm/Support/Alignment.h" 210b57cec5SDimitry Andric #include "llvm/Support/MSVCErrorWorkarounds.h" 220b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h" 230b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 248bcb0991SDimitry Andric #include <mutex> 250b57cec5SDimitry Andric 260b57cec5SDimitry Andric #include <future> 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric using namespace llvm; 290b57cec5SDimitry Andric using namespace llvm::object; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric #define DEBUG_TYPE "dyld" 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric namespace { 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric enum RuntimeDyldErrorCode { 360b57cec5SDimitry Andric GenericRTDyldError = 1 370b57cec5SDimitry Andric }; 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric // FIXME: This class is only here to support the transition to llvm::Error. It 400b57cec5SDimitry Andric // will be removed once this transition is complete. Clients should prefer to 410b57cec5SDimitry Andric // deal with the Error value directly, rather than converting to error_code. 420b57cec5SDimitry Andric class RuntimeDyldErrorCategory : public std::error_category { 430b57cec5SDimitry Andric public: 440b57cec5SDimitry Andric const char *name() const noexcept override { return "runtimedyld"; } 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric std::string message(int Condition) const override { 470b57cec5SDimitry Andric switch (static_cast<RuntimeDyldErrorCode>(Condition)) { 480b57cec5SDimitry Andric case GenericRTDyldError: return "Generic RuntimeDyld error"; 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric llvm_unreachable("Unrecognized RuntimeDyldErrorCode"); 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric }; 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric static ManagedStatic<RuntimeDyldErrorCategory> RTDyldErrorCategory; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric char RuntimeDyldError::ID = 0; 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric void RuntimeDyldError::log(raw_ostream &OS) const { 610b57cec5SDimitry Andric OS << ErrMsg << "\n"; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric std::error_code RuntimeDyldError::convertToErrorCode() const { 650b57cec5SDimitry Andric return std::error_code(GenericRTDyldError, *RTDyldErrorCategory); 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric 680b57cec5SDimitry Andric // Empty out-of-line virtual destructor as the key function. 690b57cec5SDimitry Andric RuntimeDyldImpl::~RuntimeDyldImpl() {} 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric // Pin LoadedObjectInfo's vtables to this file. 720b57cec5SDimitry Andric void RuntimeDyld::LoadedObjectInfo::anchor() {} 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric namespace llvm { 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric void RuntimeDyldImpl::registerEHFrames() {} 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric void RuntimeDyldImpl::deregisterEHFrames() { 790b57cec5SDimitry Andric MemMgr.deregisterEHFrames(); 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric #ifndef NDEBUG 830b57cec5SDimitry Andric static void dumpSectionMemory(const SectionEntry &S, StringRef State) { 840b57cec5SDimitry Andric dbgs() << "----- Contents of section " << S.getName() << " " << State 850b57cec5SDimitry Andric << " -----"; 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric if (S.getAddress() == nullptr) { 880b57cec5SDimitry Andric dbgs() << "\n <section not emitted>\n"; 890b57cec5SDimitry Andric return; 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric const unsigned ColsPerRow = 16; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric uint8_t *DataAddr = S.getAddress(); 950b57cec5SDimitry Andric uint64_t LoadAddr = S.getLoadAddress(); 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric unsigned StartPadding = LoadAddr & (ColsPerRow - 1); 980b57cec5SDimitry Andric unsigned BytesRemaining = S.getSize(); 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric if (StartPadding) { 1010b57cec5SDimitry Andric dbgs() << "\n" << format("0x%016" PRIx64, 1020b57cec5SDimitry Andric LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":"; 1030b57cec5SDimitry Andric while (StartPadding--) 1040b57cec5SDimitry Andric dbgs() << " "; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric while (BytesRemaining > 0) { 1080b57cec5SDimitry Andric if ((LoadAddr & (ColsPerRow - 1)) == 0) 1090b57cec5SDimitry Andric dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":"; 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric dbgs() << " " << format("%02x", *DataAddr); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric ++DataAddr; 1140b57cec5SDimitry Andric ++LoadAddr; 1150b57cec5SDimitry Andric --BytesRemaining; 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 1180b57cec5SDimitry Andric dbgs() << "\n"; 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric #endif 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric // Resolve the relocations for all symbols we currently know about. 1230b57cec5SDimitry Andric void RuntimeDyldImpl::resolveRelocations() { 1248bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock); 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric // Print out the sections prior to relocation. 1270b57cec5SDimitry Andric LLVM_DEBUG(for (int i = 0, e = Sections.size(); i != e; ++i) 1280b57cec5SDimitry Andric dumpSectionMemory(Sections[i], "before relocations");); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric // First, resolve relocations associated with external symbols. 1310b57cec5SDimitry Andric if (auto Err = resolveExternalSymbols()) { 1320b57cec5SDimitry Andric HasError = true; 1330b57cec5SDimitry Andric ErrorStr = toString(std::move(Err)); 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric resolveLocalRelocations(); 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric // Print out sections after relocation. 1390b57cec5SDimitry Andric LLVM_DEBUG(for (int i = 0, e = Sections.size(); i != e; ++i) 1400b57cec5SDimitry Andric dumpSectionMemory(Sections[i], "after relocations");); 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric void RuntimeDyldImpl::resolveLocalRelocations() { 1440b57cec5SDimitry Andric // Iterate over all outstanding relocations 1450b57cec5SDimitry Andric for (auto it = Relocations.begin(), e = Relocations.end(); it != e; ++it) { 1460b57cec5SDimitry Andric // The Section here (Sections[i]) refers to the section in which the 1470b57cec5SDimitry Andric // symbol for the relocation is located. The SectionID in the relocation 1480b57cec5SDimitry Andric // entry provides the section to which the relocation will be applied. 1490b57cec5SDimitry Andric int Idx = it->first; 1500b57cec5SDimitry Andric uint64_t Addr = Sections[Idx].getLoadAddress(); 1510b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Resolving relocations Section #" << Idx << "\t" 1520b57cec5SDimitry Andric << format("%p", (uintptr_t)Addr) << "\n"); 1530b57cec5SDimitry Andric resolveRelocationList(it->second, Addr); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric Relocations.clear(); 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress, 1590b57cec5SDimitry Andric uint64_t TargetAddress) { 1608bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock); 1610b57cec5SDimitry Andric for (unsigned i = 0, e = Sections.size(); i != e; ++i) { 1620b57cec5SDimitry Andric if (Sections[i].getAddress() == LocalAddress) { 1630b57cec5SDimitry Andric reassignSectionAddress(i, TargetAddress); 1640b57cec5SDimitry Andric return; 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric llvm_unreachable("Attempting to remap address of unknown section!"); 1680b57cec5SDimitry Andric } 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric static Error getOffset(const SymbolRef &Sym, SectionRef Sec, 1710b57cec5SDimitry Andric uint64_t &Result) { 1720b57cec5SDimitry Andric Expected<uint64_t> AddressOrErr = Sym.getAddress(); 1730b57cec5SDimitry Andric if (!AddressOrErr) 1740b57cec5SDimitry Andric return AddressOrErr.takeError(); 1750b57cec5SDimitry Andric Result = *AddressOrErr - Sec.getAddress(); 1760b57cec5SDimitry Andric return Error::success(); 1770b57cec5SDimitry Andric } 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric Expected<RuntimeDyldImpl::ObjSectionToIDMap> 1800b57cec5SDimitry Andric RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { 1818bcb0991SDimitry Andric std::lock_guard<sys::Mutex> locked(lock); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric // Save information about our target 1840b57cec5SDimitry Andric Arch = (Triple::ArchType)Obj.getArch(); 1850b57cec5SDimitry Andric IsTargetLittleEndian = Obj.isLittleEndian(); 1860b57cec5SDimitry Andric setMipsABI(Obj); 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric // Compute the memory size required to load all sections to be loaded 1890b57cec5SDimitry Andric // and pass this information to the memory manager 1900b57cec5SDimitry Andric if (MemMgr.needsToReserveAllocationSpace()) { 1910b57cec5SDimitry Andric uint64_t CodeSize = 0, RODataSize = 0, RWDataSize = 0; 1920b57cec5SDimitry Andric uint32_t CodeAlign = 1, RODataAlign = 1, RWDataAlign = 1; 1930b57cec5SDimitry Andric if (auto Err = computeTotalAllocSize(Obj, 1940b57cec5SDimitry Andric CodeSize, CodeAlign, 1950b57cec5SDimitry Andric RODataSize, RODataAlign, 1960b57cec5SDimitry Andric RWDataSize, RWDataAlign)) 1970b57cec5SDimitry Andric return std::move(Err); 1980b57cec5SDimitry Andric MemMgr.reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign, 1990b57cec5SDimitry Andric RWDataSize, RWDataAlign); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric // Used sections from the object file 2030b57cec5SDimitry Andric ObjSectionToIDMap LocalSections; 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric // Common symbols requiring allocation, with their sizes and alignments 2060b57cec5SDimitry Andric CommonSymbolList CommonSymbolsToAllocate; 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric uint64_t CommonSize = 0; 2090b57cec5SDimitry Andric uint32_t CommonAlign = 0; 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric // First, collect all weak and common symbols. We need to know if stronger 2120b57cec5SDimitry Andric // definitions occur elsewhere. 2130b57cec5SDimitry Andric JITSymbolResolver::LookupSet ResponsibilitySet; 2140b57cec5SDimitry Andric { 2150b57cec5SDimitry Andric JITSymbolResolver::LookupSet Symbols; 2160b57cec5SDimitry Andric for (auto &Sym : Obj.symbols()) { 217*5ffd83dbSDimitry Andric Expected<uint32_t> FlagsOrErr = Sym.getFlags(); 218*5ffd83dbSDimitry Andric if (!FlagsOrErr) 219*5ffd83dbSDimitry Andric // TODO: Test this error. 220*5ffd83dbSDimitry Andric return FlagsOrErr.takeError(); 221*5ffd83dbSDimitry Andric if ((*FlagsOrErr & SymbolRef::SF_Common) || 222*5ffd83dbSDimitry Andric (*FlagsOrErr & SymbolRef::SF_Weak)) { 2230b57cec5SDimitry Andric // Get symbol name. 2240b57cec5SDimitry Andric if (auto NameOrErr = Sym.getName()) 2250b57cec5SDimitry Andric Symbols.insert(*NameOrErr); 2260b57cec5SDimitry Andric else 2270b57cec5SDimitry Andric return NameOrErr.takeError(); 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric if (auto ResultOrErr = Resolver.getResponsibilitySet(Symbols)) 2320b57cec5SDimitry Andric ResponsibilitySet = std::move(*ResultOrErr); 2330b57cec5SDimitry Andric else 2340b57cec5SDimitry Andric return ResultOrErr.takeError(); 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // Parse symbols 2380b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Parse symbols:\n"); 2390b57cec5SDimitry Andric for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; 2400b57cec5SDimitry Andric ++I) { 241*5ffd83dbSDimitry Andric Expected<uint32_t> FlagsOrErr = I->getFlags(); 242*5ffd83dbSDimitry Andric if (!FlagsOrErr) 243*5ffd83dbSDimitry Andric // TODO: Test this error. 244*5ffd83dbSDimitry Andric return FlagsOrErr.takeError(); 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric // Skip undefined symbols. 247*5ffd83dbSDimitry Andric if (*FlagsOrErr & SymbolRef::SF_Undefined) 2480b57cec5SDimitry Andric continue; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric // Get the symbol type. 2510b57cec5SDimitry Andric object::SymbolRef::Type SymType; 2520b57cec5SDimitry Andric if (auto SymTypeOrErr = I->getType()) 2530b57cec5SDimitry Andric SymType = *SymTypeOrErr; 2540b57cec5SDimitry Andric else 2550b57cec5SDimitry Andric return SymTypeOrErr.takeError(); 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric // Get symbol name. 2580b57cec5SDimitry Andric StringRef Name; 2590b57cec5SDimitry Andric if (auto NameOrErr = I->getName()) 2600b57cec5SDimitry Andric Name = *NameOrErr; 2610b57cec5SDimitry Andric else 2620b57cec5SDimitry Andric return NameOrErr.takeError(); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric // Compute JIT symbol flags. 2650b57cec5SDimitry Andric auto JITSymFlags = getJITSymbolFlags(*I); 2660b57cec5SDimitry Andric if (!JITSymFlags) 2670b57cec5SDimitry Andric return JITSymFlags.takeError(); 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric // If this is a weak definition, check to see if there's a strong one. 2700b57cec5SDimitry Andric // If there is, skip this symbol (we won't be providing it: the strong 2710b57cec5SDimitry Andric // definition will). If there's no strong definition, make this definition 2720b57cec5SDimitry Andric // strong. 2730b57cec5SDimitry Andric if (JITSymFlags->isWeak() || JITSymFlags->isCommon()) { 2740b57cec5SDimitry Andric // First check whether there's already a definition in this instance. 2750b57cec5SDimitry Andric if (GlobalSymbolTable.count(Name)) 2760b57cec5SDimitry Andric continue; 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric // If we're not responsible for this symbol, skip it. 2790b57cec5SDimitry Andric if (!ResponsibilitySet.count(Name)) 2800b57cec5SDimitry Andric continue; 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric // Otherwise update the flags on the symbol to make this definition 2830b57cec5SDimitry Andric // strong. 2840b57cec5SDimitry Andric if (JITSymFlags->isWeak()) 2850b57cec5SDimitry Andric *JITSymFlags &= ~JITSymbolFlags::Weak; 2860b57cec5SDimitry Andric if (JITSymFlags->isCommon()) { 2870b57cec5SDimitry Andric *JITSymFlags &= ~JITSymbolFlags::Common; 2880b57cec5SDimitry Andric uint32_t Align = I->getAlignment(); 2890b57cec5SDimitry Andric uint64_t Size = I->getCommonSize(); 2900b57cec5SDimitry Andric if (!CommonAlign) 2910b57cec5SDimitry Andric CommonAlign = Align; 2920b57cec5SDimitry Andric CommonSize = alignTo(CommonSize, Align) + Size; 2930b57cec5SDimitry Andric CommonSymbolsToAllocate.push_back(*I); 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric 297*5ffd83dbSDimitry Andric if (*FlagsOrErr & SymbolRef::SF_Absolute && 2980b57cec5SDimitry Andric SymType != object::SymbolRef::ST_File) { 2990b57cec5SDimitry Andric uint64_t Addr = 0; 3000b57cec5SDimitry Andric if (auto AddrOrErr = I->getAddress()) 3010b57cec5SDimitry Andric Addr = *AddrOrErr; 3020b57cec5SDimitry Andric else 3030b57cec5SDimitry Andric return AddrOrErr.takeError(); 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric unsigned SectionID = AbsoluteSymbolSection; 3060b57cec5SDimitry Andric 3070b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tType: " << SymType << " (absolute) Name: " << Name 3080b57cec5SDimitry Andric << " SID: " << SectionID 3090b57cec5SDimitry Andric << " Offset: " << format("%p", (uintptr_t)Addr) 310*5ffd83dbSDimitry Andric << " flags: " << *FlagsOrErr << "\n"); 3110b57cec5SDimitry Andric GlobalSymbolTable[Name] = SymbolTableEntry(SectionID, Addr, *JITSymFlags); 3120b57cec5SDimitry Andric } else if (SymType == object::SymbolRef::ST_Function || 3130b57cec5SDimitry Andric SymType == object::SymbolRef::ST_Data || 3140b57cec5SDimitry Andric SymType == object::SymbolRef::ST_Unknown || 3150b57cec5SDimitry Andric SymType == object::SymbolRef::ST_Other) { 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric section_iterator SI = Obj.section_end(); 3180b57cec5SDimitry Andric if (auto SIOrErr = I->getSection()) 3190b57cec5SDimitry Andric SI = *SIOrErr; 3200b57cec5SDimitry Andric else 3210b57cec5SDimitry Andric return SIOrErr.takeError(); 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric if (SI == Obj.section_end()) 3240b57cec5SDimitry Andric continue; 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric // Get symbol offset. 3270b57cec5SDimitry Andric uint64_t SectOffset; 3280b57cec5SDimitry Andric if (auto Err = getOffset(*I, *SI, SectOffset)) 3290b57cec5SDimitry Andric return std::move(Err); 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric bool IsCode = SI->isText(); 3320b57cec5SDimitry Andric unsigned SectionID; 3330b57cec5SDimitry Andric if (auto SectionIDOrErr = 3340b57cec5SDimitry Andric findOrEmitSection(Obj, *SI, IsCode, LocalSections)) 3350b57cec5SDimitry Andric SectionID = *SectionIDOrErr; 3360b57cec5SDimitry Andric else 3370b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name 3400b57cec5SDimitry Andric << " SID: " << SectionID 3410b57cec5SDimitry Andric << " Offset: " << format("%p", (uintptr_t)SectOffset) 342*5ffd83dbSDimitry Andric << " flags: " << *FlagsOrErr << "\n"); 3430b57cec5SDimitry Andric GlobalSymbolTable[Name] = 3440b57cec5SDimitry Andric SymbolTableEntry(SectionID, SectOffset, *JITSymFlags); 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // Allocate common symbols 3490b57cec5SDimitry Andric if (auto Err = emitCommonSymbols(Obj, CommonSymbolsToAllocate, CommonSize, 3500b57cec5SDimitry Andric CommonAlign)) 3510b57cec5SDimitry Andric return std::move(Err); 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric // Parse and process relocations 3540b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Parse relocations:\n"); 3550b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 3560b57cec5SDimitry Andric SI != SE; ++SI) { 3570b57cec5SDimitry Andric StubMap Stubs; 3580b57cec5SDimitry Andric 3598bcb0991SDimitry Andric Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection(); 3608bcb0991SDimitry Andric if (!RelSecOrErr) 3618bcb0991SDimitry Andric return RelSecOrErr.takeError(); 3628bcb0991SDimitry Andric 3638bcb0991SDimitry Andric section_iterator RelocatedSection = *RelSecOrErr; 3640b57cec5SDimitry Andric if (RelocatedSection == SE) 3650b57cec5SDimitry Andric continue; 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric relocation_iterator I = SI->relocation_begin(); 3680b57cec5SDimitry Andric relocation_iterator E = SI->relocation_end(); 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric if (I == E && !ProcessAllSections) 3710b57cec5SDimitry Andric continue; 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric bool IsCode = RelocatedSection->isText(); 3740b57cec5SDimitry Andric unsigned SectionID = 0; 3750b57cec5SDimitry Andric if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode, 3760b57cec5SDimitry Andric LocalSections)) 3770b57cec5SDimitry Andric SectionID = *SectionIDOrErr; 3780b57cec5SDimitry Andric else 3790b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric for (; I != E;) 3840b57cec5SDimitry Andric if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs)) 3850b57cec5SDimitry Andric I = *IOrErr; 3860b57cec5SDimitry Andric else 3870b57cec5SDimitry Andric return IOrErr.takeError(); 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric // If there is a NotifyStubEmitted callback set, call it to register any 3900b57cec5SDimitry Andric // stubs created for this section. 3910b57cec5SDimitry Andric if (NotifyStubEmitted) { 3920b57cec5SDimitry Andric StringRef FileName = Obj.getFileName(); 3930b57cec5SDimitry Andric StringRef SectionName = Sections[SectionID].getName(); 3940b57cec5SDimitry Andric for (auto &KV : Stubs) { 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric auto &VR = KV.first; 3970b57cec5SDimitry Andric uint64_t StubAddr = KV.second; 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric // If this is a named stub, just call NotifyStubEmitted. 4000b57cec5SDimitry Andric if (VR.SymbolName) { 4010b57cec5SDimitry Andric NotifyStubEmitted(FileName, SectionName, VR.SymbolName, SectionID, 4020b57cec5SDimitry Andric StubAddr); 4030b57cec5SDimitry Andric continue; 4040b57cec5SDimitry Andric } 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric // Otherwise we will have to try a reverse lookup on the globla symbol table. 4070b57cec5SDimitry Andric for (auto &GSTMapEntry : GlobalSymbolTable) { 4080b57cec5SDimitry Andric StringRef SymbolName = GSTMapEntry.first(); 4090b57cec5SDimitry Andric auto &GSTEntry = GSTMapEntry.second; 4100b57cec5SDimitry Andric if (GSTEntry.getSectionID() == VR.SectionID && 4110b57cec5SDimitry Andric GSTEntry.getOffset() == VR.Offset) { 4120b57cec5SDimitry Andric NotifyStubEmitted(FileName, SectionName, SymbolName, SectionID, 4130b57cec5SDimitry Andric StubAddr); 4140b57cec5SDimitry Andric break; 4150b57cec5SDimitry Andric } 4160b57cec5SDimitry Andric } 4170b57cec5SDimitry Andric } 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric // Process remaining sections 4220b57cec5SDimitry Andric if (ProcessAllSections) { 4230b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Process remaining sections:\n"); 4240b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 4250b57cec5SDimitry Andric SI != SE; ++SI) { 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric /* Ignore already loaded sections */ 4280b57cec5SDimitry Andric if (LocalSections.find(*SI) != LocalSections.end()) 4290b57cec5SDimitry Andric continue; 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric bool IsCode = SI->isText(); 4320b57cec5SDimitry Andric if (auto SectionIDOrErr = 4330b57cec5SDimitry Andric findOrEmitSection(Obj, *SI, IsCode, LocalSections)) 4340b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tSectionID: " << (*SectionIDOrErr) << "\n"); 4350b57cec5SDimitry Andric else 4360b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric // Give the subclasses a chance to tie-up any loose ends. 4410b57cec5SDimitry Andric if (auto Err = finalizeLoad(Obj, LocalSections)) 4420b57cec5SDimitry Andric return std::move(Err); 4430b57cec5SDimitry Andric 4440b57cec5SDimitry Andric // for (auto E : LocalSections) 4450b57cec5SDimitry Andric // llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n"; 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric return LocalSections; 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric // A helper method for computeTotalAllocSize. 4510b57cec5SDimitry Andric // Computes the memory size required to allocate sections with the given sizes, 4520b57cec5SDimitry Andric // assuming that all sections are allocated with the given alignment 4530b57cec5SDimitry Andric static uint64_t 4540b57cec5SDimitry Andric computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes, 4550b57cec5SDimitry Andric uint64_t Alignment) { 4560b57cec5SDimitry Andric uint64_t TotalSize = 0; 4570b57cec5SDimitry Andric for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) { 4580b57cec5SDimitry Andric uint64_t AlignedSize = 4590b57cec5SDimitry Andric (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment; 4600b57cec5SDimitry Andric TotalSize += AlignedSize; 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric return TotalSize; 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric static bool isRequiredForExecution(const SectionRef Section) { 4660b57cec5SDimitry Andric const ObjectFile *Obj = Section.getObject(); 4670b57cec5SDimitry Andric if (isa<object::ELFObjectFileBase>(Obj)) 4680b57cec5SDimitry Andric return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC; 4690b57cec5SDimitry Andric if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) { 4700b57cec5SDimitry Andric const coff_section *CoffSection = COFFObj->getCOFFSection(Section); 4710b57cec5SDimitry Andric // Avoid loading zero-sized COFF sections. 4720b57cec5SDimitry Andric // In PE files, VirtualSize gives the section size, and SizeOfRawData 4730b57cec5SDimitry Andric // may be zero for sections with content. In Obj files, SizeOfRawData 4740b57cec5SDimitry Andric // gives the section size, and VirtualSize is always zero. Hence 4750b57cec5SDimitry Andric // the need to check for both cases below. 4760b57cec5SDimitry Andric bool HasContent = 4770b57cec5SDimitry Andric (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0); 4780b57cec5SDimitry Andric bool IsDiscardable = 4790b57cec5SDimitry Andric CoffSection->Characteristics & 4800b57cec5SDimitry Andric (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO); 4810b57cec5SDimitry Andric return HasContent && !IsDiscardable; 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric assert(isa<MachOObjectFile>(Obj)); 4850b57cec5SDimitry Andric return true; 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric static bool isReadOnlyData(const SectionRef Section) { 4890b57cec5SDimitry Andric const ObjectFile *Obj = Section.getObject(); 4900b57cec5SDimitry Andric if (isa<object::ELFObjectFileBase>(Obj)) 4910b57cec5SDimitry Andric return !(ELFSectionRef(Section).getFlags() & 4920b57cec5SDimitry Andric (ELF::SHF_WRITE | ELF::SHF_EXECINSTR)); 4930b57cec5SDimitry Andric if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) 4940b57cec5SDimitry Andric return ((COFFObj->getCOFFSection(Section)->Characteristics & 4950b57cec5SDimitry Andric (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA 4960b57cec5SDimitry Andric | COFF::IMAGE_SCN_MEM_READ 4970b57cec5SDimitry Andric | COFF::IMAGE_SCN_MEM_WRITE)) 4980b57cec5SDimitry Andric == 4990b57cec5SDimitry Andric (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA 5000b57cec5SDimitry Andric | COFF::IMAGE_SCN_MEM_READ)); 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric assert(isa<MachOObjectFile>(Obj)); 5030b57cec5SDimitry Andric return false; 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric static bool isZeroInit(const SectionRef Section) { 5070b57cec5SDimitry Andric const ObjectFile *Obj = Section.getObject(); 5080b57cec5SDimitry Andric if (isa<object::ELFObjectFileBase>(Obj)) 5090b57cec5SDimitry Andric return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS; 5100b57cec5SDimitry Andric if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) 5110b57cec5SDimitry Andric return COFFObj->getCOFFSection(Section)->Characteristics & 5120b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric auto *MachO = cast<MachOObjectFile>(Obj); 5150b57cec5SDimitry Andric unsigned SectionType = MachO->getSectionType(Section); 5160b57cec5SDimitry Andric return SectionType == MachO::S_ZEROFILL || 5170b57cec5SDimitry Andric SectionType == MachO::S_GB_ZEROFILL; 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric 5200b57cec5SDimitry Andric // Compute an upper bound of the memory size that is required to load all 5210b57cec5SDimitry Andric // sections 5220b57cec5SDimitry Andric Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj, 5230b57cec5SDimitry Andric uint64_t &CodeSize, 5240b57cec5SDimitry Andric uint32_t &CodeAlign, 5250b57cec5SDimitry Andric uint64_t &RODataSize, 5260b57cec5SDimitry Andric uint32_t &RODataAlign, 5270b57cec5SDimitry Andric uint64_t &RWDataSize, 5280b57cec5SDimitry Andric uint32_t &RWDataAlign) { 5290b57cec5SDimitry Andric // Compute the size of all sections required for execution 5300b57cec5SDimitry Andric std::vector<uint64_t> CodeSectionSizes; 5310b57cec5SDimitry Andric std::vector<uint64_t> ROSectionSizes; 5320b57cec5SDimitry Andric std::vector<uint64_t> RWSectionSizes; 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric // Collect sizes of all sections to be loaded; 5350b57cec5SDimitry Andric // also determine the max alignment of all sections 5360b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 5370b57cec5SDimitry Andric SI != SE; ++SI) { 5380b57cec5SDimitry Andric const SectionRef &Section = *SI; 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric bool IsRequired = isRequiredForExecution(Section) || ProcessAllSections; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Consider only the sections that are required to be loaded for execution 5430b57cec5SDimitry Andric if (IsRequired) { 5440b57cec5SDimitry Andric uint64_t DataSize = Section.getSize(); 5450b57cec5SDimitry Andric uint64_t Alignment64 = Section.getAlignment(); 5460b57cec5SDimitry Andric unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 5470b57cec5SDimitry Andric bool IsCode = Section.isText(); 5480b57cec5SDimitry Andric bool IsReadOnly = isReadOnlyData(Section); 5490b57cec5SDimitry Andric 5508bcb0991SDimitry Andric Expected<StringRef> NameOrErr = Section.getName(); 5518bcb0991SDimitry Andric if (!NameOrErr) 5528bcb0991SDimitry Andric return NameOrErr.takeError(); 5538bcb0991SDimitry Andric StringRef Name = *NameOrErr; 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section); 5560b57cec5SDimitry Andric 5570b57cec5SDimitry Andric uint64_t PaddingSize = 0; 5580b57cec5SDimitry Andric if (Name == ".eh_frame") 5590b57cec5SDimitry Andric PaddingSize += 4; 5600b57cec5SDimitry Andric if (StubBufSize != 0) 5610b57cec5SDimitry Andric PaddingSize += getStubAlignment() - 1; 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric uint64_t SectionSize = DataSize + PaddingSize + StubBufSize; 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric // The .eh_frame section (at least on Linux) needs an extra four bytes 5660b57cec5SDimitry Andric // padded 5670b57cec5SDimitry Andric // with zeroes added at the end. For MachO objects, this section has a 5680b57cec5SDimitry Andric // slightly different name, so this won't have any effect for MachO 5690b57cec5SDimitry Andric // objects. 5700b57cec5SDimitry Andric if (Name == ".eh_frame") 5710b57cec5SDimitry Andric SectionSize += 4; 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric if (!SectionSize) 5740b57cec5SDimitry Andric SectionSize = 1; 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric if (IsCode) { 5770b57cec5SDimitry Andric CodeAlign = std::max(CodeAlign, Alignment); 5780b57cec5SDimitry Andric CodeSectionSizes.push_back(SectionSize); 5790b57cec5SDimitry Andric } else if (IsReadOnly) { 5800b57cec5SDimitry Andric RODataAlign = std::max(RODataAlign, Alignment); 5810b57cec5SDimitry Andric ROSectionSizes.push_back(SectionSize); 5820b57cec5SDimitry Andric } else { 5830b57cec5SDimitry Andric RWDataAlign = std::max(RWDataAlign, Alignment); 5840b57cec5SDimitry Andric RWSectionSizes.push_back(SectionSize); 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric // Compute Global Offset Table size. If it is not zero we 5900b57cec5SDimitry Andric // also update alignment, which is equal to a size of a 5910b57cec5SDimitry Andric // single GOT entry. 5920b57cec5SDimitry Andric if (unsigned GotSize = computeGOTSize(Obj)) { 5930b57cec5SDimitry Andric RWSectionSizes.push_back(GotSize); 5940b57cec5SDimitry Andric RWDataAlign = std::max<uint32_t>(RWDataAlign, getGOTEntrySize()); 5950b57cec5SDimitry Andric } 5960b57cec5SDimitry Andric 5970b57cec5SDimitry Andric // Compute the size of all common symbols 5980b57cec5SDimitry Andric uint64_t CommonSize = 0; 5990b57cec5SDimitry Andric uint32_t CommonAlign = 1; 6000b57cec5SDimitry Andric for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; 6010b57cec5SDimitry Andric ++I) { 602*5ffd83dbSDimitry Andric Expected<uint32_t> FlagsOrErr = I->getFlags(); 603*5ffd83dbSDimitry Andric if (!FlagsOrErr) 604*5ffd83dbSDimitry Andric // TODO: Test this error. 605*5ffd83dbSDimitry Andric return FlagsOrErr.takeError(); 606*5ffd83dbSDimitry Andric if (*FlagsOrErr & SymbolRef::SF_Common) { 6070b57cec5SDimitry Andric // Add the common symbols to a list. We'll allocate them all below. 6080b57cec5SDimitry Andric uint64_t Size = I->getCommonSize(); 6090b57cec5SDimitry Andric uint32_t Align = I->getAlignment(); 6100b57cec5SDimitry Andric // If this is the first common symbol, use its alignment as the alignment 6110b57cec5SDimitry Andric // for the common symbols section. 6120b57cec5SDimitry Andric if (CommonSize == 0) 6130b57cec5SDimitry Andric CommonAlign = Align; 6140b57cec5SDimitry Andric CommonSize = alignTo(CommonSize, Align) + Size; 6150b57cec5SDimitry Andric } 6160b57cec5SDimitry Andric } 6170b57cec5SDimitry Andric if (CommonSize != 0) { 6180b57cec5SDimitry Andric RWSectionSizes.push_back(CommonSize); 6190b57cec5SDimitry Andric RWDataAlign = std::max(RWDataAlign, CommonAlign); 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric // Compute the required allocation space for each different type of sections 6230b57cec5SDimitry Andric // (code, read-only data, read-write data) assuming that all sections are 6240b57cec5SDimitry Andric // allocated with the max alignment. Note that we cannot compute with the 6250b57cec5SDimitry Andric // individual alignments of the sections, because then the required size 6260b57cec5SDimitry Andric // depends on the order, in which the sections are allocated. 6270b57cec5SDimitry Andric CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign); 6280b57cec5SDimitry Andric RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign); 6290b57cec5SDimitry Andric RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign); 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric return Error::success(); 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric // compute GOT size 6350b57cec5SDimitry Andric unsigned RuntimeDyldImpl::computeGOTSize(const ObjectFile &Obj) { 6360b57cec5SDimitry Andric size_t GotEntrySize = getGOTEntrySize(); 6370b57cec5SDimitry Andric if (!GotEntrySize) 6380b57cec5SDimitry Andric return 0; 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric size_t GotSize = 0; 6410b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 6420b57cec5SDimitry Andric SI != SE; ++SI) { 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric for (const RelocationRef &Reloc : SI->relocations()) 6450b57cec5SDimitry Andric if (relocationNeedsGot(Reloc)) 6460b57cec5SDimitry Andric GotSize += GotEntrySize; 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric 6490b57cec5SDimitry Andric return GotSize; 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric // compute stub buffer size for the given section 6530b57cec5SDimitry Andric unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj, 6540b57cec5SDimitry Andric const SectionRef &Section) { 6550b57cec5SDimitry Andric unsigned StubSize = getMaxStubSize(); 6560b57cec5SDimitry Andric if (StubSize == 0) { 6570b57cec5SDimitry Andric return 0; 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric // FIXME: this is an inefficient way to handle this. We should computed the 6600b57cec5SDimitry Andric // necessary section allocation size in loadObject by walking all the sections 6610b57cec5SDimitry Andric // once. 6620b57cec5SDimitry Andric unsigned StubBufSize = 0; 6630b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 6640b57cec5SDimitry Andric SI != SE; ++SI) { 6658bcb0991SDimitry Andric 6668bcb0991SDimitry Andric Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection(); 6678bcb0991SDimitry Andric if (!RelSecOrErr) 6688bcb0991SDimitry Andric report_fatal_error(toString(RelSecOrErr.takeError())); 6698bcb0991SDimitry Andric 6708bcb0991SDimitry Andric section_iterator RelSecI = *RelSecOrErr; 6710b57cec5SDimitry Andric if (!(RelSecI == Section)) 6720b57cec5SDimitry Andric continue; 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric for (const RelocationRef &Reloc : SI->relocations()) 6750b57cec5SDimitry Andric if (relocationNeedsStub(Reloc)) 6760b57cec5SDimitry Andric StubBufSize += StubSize; 6770b57cec5SDimitry Andric } 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric // Get section data size and alignment 6800b57cec5SDimitry Andric uint64_t DataSize = Section.getSize(); 6810b57cec5SDimitry Andric uint64_t Alignment64 = Section.getAlignment(); 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric // Add stubbuf size alignment 6840b57cec5SDimitry Andric unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 6850b57cec5SDimitry Andric unsigned StubAlignment = getStubAlignment(); 6860b57cec5SDimitry Andric unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment); 6870b57cec5SDimitry Andric if (StubAlignment > EndAlignment) 6880b57cec5SDimitry Andric StubBufSize += StubAlignment - EndAlignment; 6890b57cec5SDimitry Andric return StubBufSize; 6900b57cec5SDimitry Andric } 6910b57cec5SDimitry Andric 6920b57cec5SDimitry Andric uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src, 6930b57cec5SDimitry Andric unsigned Size) const { 6940b57cec5SDimitry Andric uint64_t Result = 0; 6950b57cec5SDimitry Andric if (IsTargetLittleEndian) { 6960b57cec5SDimitry Andric Src += Size - 1; 6970b57cec5SDimitry Andric while (Size--) 6980b57cec5SDimitry Andric Result = (Result << 8) | *Src--; 6990b57cec5SDimitry Andric } else 7000b57cec5SDimitry Andric while (Size--) 7010b57cec5SDimitry Andric Result = (Result << 8) | *Src++; 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric return Result; 7040b57cec5SDimitry Andric } 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst, 7070b57cec5SDimitry Andric unsigned Size) const { 7080b57cec5SDimitry Andric if (IsTargetLittleEndian) { 7090b57cec5SDimitry Andric while (Size--) { 7100b57cec5SDimitry Andric *Dst++ = Value & 0xFF; 7110b57cec5SDimitry Andric Value >>= 8; 7120b57cec5SDimitry Andric } 7130b57cec5SDimitry Andric } else { 7140b57cec5SDimitry Andric Dst += Size - 1; 7150b57cec5SDimitry Andric while (Size--) { 7160b57cec5SDimitry Andric *Dst-- = Value & 0xFF; 7170b57cec5SDimitry Andric Value >>= 8; 7180b57cec5SDimitry Andric } 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric Expected<JITSymbolFlags> 7230b57cec5SDimitry Andric RuntimeDyldImpl::getJITSymbolFlags(const SymbolRef &SR) { 7240b57cec5SDimitry Andric return JITSymbolFlags::fromObjectSymbol(SR); 7250b57cec5SDimitry Andric } 7260b57cec5SDimitry Andric 7270b57cec5SDimitry Andric Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj, 7280b57cec5SDimitry Andric CommonSymbolList &SymbolsToAllocate, 7290b57cec5SDimitry Andric uint64_t CommonSize, 7300b57cec5SDimitry Andric uint32_t CommonAlign) { 7310b57cec5SDimitry Andric if (SymbolsToAllocate.empty()) 7320b57cec5SDimitry Andric return Error::success(); 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric // Allocate memory for the section 7350b57cec5SDimitry Andric unsigned SectionID = Sections.size(); 7360b57cec5SDimitry Andric uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID, 7370b57cec5SDimitry Andric "<common symbols>", false); 7380b57cec5SDimitry Andric if (!Addr) 7390b57cec5SDimitry Andric report_fatal_error("Unable to allocate memory for common symbols!"); 7400b57cec5SDimitry Andric uint64_t Offset = 0; 7410b57cec5SDimitry Andric Sections.push_back( 7420b57cec5SDimitry Andric SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0)); 7430b57cec5SDimitry Andric memset(Addr, 0, CommonSize); 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID 7460b57cec5SDimitry Andric << " new addr: " << format("%p", Addr) 7470b57cec5SDimitry Andric << " DataSize: " << CommonSize << "\n"); 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric // Assign the address of each symbol 7500b57cec5SDimitry Andric for (auto &Sym : SymbolsToAllocate) { 7518bcb0991SDimitry Andric uint32_t Alignment = Sym.getAlignment(); 7520b57cec5SDimitry Andric uint64_t Size = Sym.getCommonSize(); 7530b57cec5SDimitry Andric StringRef Name; 7540b57cec5SDimitry Andric if (auto NameOrErr = Sym.getName()) 7550b57cec5SDimitry Andric Name = *NameOrErr; 7560b57cec5SDimitry Andric else 7570b57cec5SDimitry Andric return NameOrErr.takeError(); 7588bcb0991SDimitry Andric if (Alignment) { 7590b57cec5SDimitry Andric // This symbol has an alignment requirement. 7608bcb0991SDimitry Andric uint64_t AlignOffset = 7618bcb0991SDimitry Andric offsetToAlignment((uint64_t)Addr, Align(Alignment)); 7620b57cec5SDimitry Andric Addr += AlignOffset; 7630b57cec5SDimitry Andric Offset += AlignOffset; 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric auto JITSymFlags = getJITSymbolFlags(Sym); 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric if (!JITSymFlags) 7680b57cec5SDimitry Andric return JITSymFlags.takeError(); 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Allocating common symbol " << Name << " address " 7710b57cec5SDimitry Andric << format("%p", Addr) << "\n"); 7720b57cec5SDimitry Andric GlobalSymbolTable[Name] = 7730b57cec5SDimitry Andric SymbolTableEntry(SectionID, Offset, std::move(*JITSymFlags)); 7740b57cec5SDimitry Andric Offset += Size; 7750b57cec5SDimitry Andric Addr += Size; 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric return Error::success(); 7790b57cec5SDimitry Andric } 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric Expected<unsigned> 7820b57cec5SDimitry Andric RuntimeDyldImpl::emitSection(const ObjectFile &Obj, 7830b57cec5SDimitry Andric const SectionRef &Section, 7840b57cec5SDimitry Andric bool IsCode) { 7850b57cec5SDimitry Andric StringRef data; 7860b57cec5SDimitry Andric uint64_t Alignment64 = Section.getAlignment(); 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 7890b57cec5SDimitry Andric unsigned PaddingSize = 0; 7900b57cec5SDimitry Andric unsigned StubBufSize = 0; 7910b57cec5SDimitry Andric bool IsRequired = isRequiredForExecution(Section); 7920b57cec5SDimitry Andric bool IsVirtual = Section.isVirtual(); 7930b57cec5SDimitry Andric bool IsZeroInit = isZeroInit(Section); 7940b57cec5SDimitry Andric bool IsReadOnly = isReadOnlyData(Section); 7950b57cec5SDimitry Andric uint64_t DataSize = Section.getSize(); 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric // An alignment of 0 (at least with ELF) is identical to an alignment of 1, 7980b57cec5SDimitry Andric // while being more "polite". Other formats do not support 0-aligned sections 7990b57cec5SDimitry Andric // anyway, so we should guarantee that the alignment is always at least 1. 8000b57cec5SDimitry Andric Alignment = std::max(1u, Alignment); 8010b57cec5SDimitry Andric 8028bcb0991SDimitry Andric Expected<StringRef> NameOrErr = Section.getName(); 8038bcb0991SDimitry Andric if (!NameOrErr) 8048bcb0991SDimitry Andric return NameOrErr.takeError(); 8058bcb0991SDimitry Andric StringRef Name = *NameOrErr; 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric StubBufSize = computeSectionStubBufSize(Obj, Section); 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric // The .eh_frame section (at least on Linux) needs an extra four bytes padded 8100b57cec5SDimitry Andric // with zeroes added at the end. For MachO objects, this section has a 8110b57cec5SDimitry Andric // slightly different name, so this won't have any effect for MachO objects. 8120b57cec5SDimitry Andric if (Name == ".eh_frame") 8130b57cec5SDimitry Andric PaddingSize = 4; 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric uintptr_t Allocate; 8160b57cec5SDimitry Andric unsigned SectionID = Sections.size(); 8170b57cec5SDimitry Andric uint8_t *Addr; 8180b57cec5SDimitry Andric const char *pData = nullptr; 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric // If this section contains any bits (i.e. isn't a virtual or bss section), 8210b57cec5SDimitry Andric // grab a reference to them. 8220b57cec5SDimitry Andric if (!IsVirtual && !IsZeroInit) { 8230b57cec5SDimitry Andric // In either case, set the location of the unrelocated section in memory, 8240b57cec5SDimitry Andric // since we still process relocations for it even if we're not applying them. 8250b57cec5SDimitry Andric if (Expected<StringRef> E = Section.getContents()) 8260b57cec5SDimitry Andric data = *E; 8270b57cec5SDimitry Andric else 8280b57cec5SDimitry Andric return E.takeError(); 8290b57cec5SDimitry Andric pData = data.data(); 8300b57cec5SDimitry Andric } 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric // If there are any stubs then the section alignment needs to be at least as 8330b57cec5SDimitry Andric // high as stub alignment or padding calculations may by incorrect when the 8340b57cec5SDimitry Andric // section is remapped. 8350b57cec5SDimitry Andric if (StubBufSize != 0) { 8360b57cec5SDimitry Andric Alignment = std::max(Alignment, getStubAlignment()); 8370b57cec5SDimitry Andric PaddingSize += getStubAlignment() - 1; 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric // Some sections, such as debug info, don't need to be loaded for execution. 8410b57cec5SDimitry Andric // Process those only if explicitly requested. 8420b57cec5SDimitry Andric if (IsRequired || ProcessAllSections) { 8430b57cec5SDimitry Andric Allocate = DataSize + PaddingSize + StubBufSize; 8440b57cec5SDimitry Andric if (!Allocate) 8450b57cec5SDimitry Andric Allocate = 1; 8460b57cec5SDimitry Andric Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID, 8470b57cec5SDimitry Andric Name) 8480b57cec5SDimitry Andric : MemMgr.allocateDataSection(Allocate, Alignment, SectionID, 8490b57cec5SDimitry Andric Name, IsReadOnly); 8500b57cec5SDimitry Andric if (!Addr) 8510b57cec5SDimitry Andric report_fatal_error("Unable to allocate section memory!"); 8520b57cec5SDimitry Andric 8530b57cec5SDimitry Andric // Zero-initialize or copy the data from the image 8540b57cec5SDimitry Andric if (IsZeroInit || IsVirtual) 8550b57cec5SDimitry Andric memset(Addr, 0, DataSize); 8560b57cec5SDimitry Andric else 8570b57cec5SDimitry Andric memcpy(Addr, pData, DataSize); 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric // Fill in any extra bytes we allocated for padding 8600b57cec5SDimitry Andric if (PaddingSize != 0) { 8610b57cec5SDimitry Andric memset(Addr + DataSize, 0, PaddingSize); 8620b57cec5SDimitry Andric // Update the DataSize variable to include padding. 8630b57cec5SDimitry Andric DataSize += PaddingSize; 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric // Align DataSize to stub alignment if we have any stubs (PaddingSize will 8660b57cec5SDimitry Andric // have been increased above to account for this). 8670b57cec5SDimitry Andric if (StubBufSize > 0) 8680b57cec5SDimitry Andric DataSize &= -(uint64_t)getStubAlignment(); 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric 8710b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " 8720b57cec5SDimitry Andric << Name << " obj addr: " << format("%p", pData) 8730b57cec5SDimitry Andric << " new addr: " << format("%p", Addr) << " DataSize: " 8740b57cec5SDimitry Andric << DataSize << " StubBufSize: " << StubBufSize 8750b57cec5SDimitry Andric << " Allocate: " << Allocate << "\n"); 8760b57cec5SDimitry Andric } else { 8770b57cec5SDimitry Andric // Even if we didn't load the section, we need to record an entry for it 8780b57cec5SDimitry Andric // to handle later processing (and by 'handle' I mean don't do anything 8790b57cec5SDimitry Andric // with these sections). 8800b57cec5SDimitry Andric Allocate = 0; 8810b57cec5SDimitry Andric Addr = nullptr; 8820b57cec5SDimitry Andric LLVM_DEBUG( 8830b57cec5SDimitry Andric dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name 8840b57cec5SDimitry Andric << " obj addr: " << format("%p", data.data()) << " new addr: 0" 8850b57cec5SDimitry Andric << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize 8860b57cec5SDimitry Andric << " Allocate: " << Allocate << "\n"); 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric Sections.push_back( 8900b57cec5SDimitry Andric SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData)); 8910b57cec5SDimitry Andric 8920b57cec5SDimitry Andric // Debug info sections are linked as if their load address was zero 8930b57cec5SDimitry Andric if (!IsRequired) 8940b57cec5SDimitry Andric Sections.back().setLoadAddress(0); 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric return SectionID; 8970b57cec5SDimitry Andric } 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric Expected<unsigned> 9000b57cec5SDimitry Andric RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj, 9010b57cec5SDimitry Andric const SectionRef &Section, 9020b57cec5SDimitry Andric bool IsCode, 9030b57cec5SDimitry Andric ObjSectionToIDMap &LocalSections) { 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric unsigned SectionID = 0; 9060b57cec5SDimitry Andric ObjSectionToIDMap::iterator i = LocalSections.find(Section); 9070b57cec5SDimitry Andric if (i != LocalSections.end()) 9080b57cec5SDimitry Andric SectionID = i->second; 9090b57cec5SDimitry Andric else { 9100b57cec5SDimitry Andric if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode)) 9110b57cec5SDimitry Andric SectionID = *SectionIDOrErr; 9120b57cec5SDimitry Andric else 9130b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 9140b57cec5SDimitry Andric LocalSections[Section] = SectionID; 9150b57cec5SDimitry Andric } 9160b57cec5SDimitry Andric return SectionID; 9170b57cec5SDimitry Andric } 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, 9200b57cec5SDimitry Andric unsigned SectionID) { 9210b57cec5SDimitry Andric Relocations[SectionID].push_back(RE); 9220b57cec5SDimitry Andric } 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, 9250b57cec5SDimitry Andric StringRef SymbolName) { 9260b57cec5SDimitry Andric // Relocation by symbol. If the symbol is found in the global symbol table, 9270b57cec5SDimitry Andric // create an appropriate section relocation. Otherwise, add it to 9280b57cec5SDimitry Andric // ExternalSymbolRelocations. 9290b57cec5SDimitry Andric RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName); 9300b57cec5SDimitry Andric if (Loc == GlobalSymbolTable.end()) { 9310b57cec5SDimitry Andric ExternalSymbolRelocations[SymbolName].push_back(RE); 9320b57cec5SDimitry Andric } else { 9330b57cec5SDimitry Andric // Copy the RE since we want to modify its addend. 9340b57cec5SDimitry Andric RelocationEntry RECopy = RE; 9350b57cec5SDimitry Andric const auto &SymInfo = Loc->second; 9360b57cec5SDimitry Andric RECopy.Addend += SymInfo.getOffset(); 9370b57cec5SDimitry Andric Relocations[SymInfo.getSectionID()].push_back(RECopy); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric } 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr, 9420b57cec5SDimitry Andric unsigned AbiVariant) { 9438bcb0991SDimitry Andric if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be || 9448bcb0991SDimitry Andric Arch == Triple::aarch64_32) { 9450b57cec5SDimitry Andric // This stub has to be able to access the full address space, 9460b57cec5SDimitry Andric // since symbol lookup won't necessarily find a handy, in-range, 9470b57cec5SDimitry Andric // PLT stub for functions which could be anywhere. 9480b57cec5SDimitry Andric // Stub can use ip0 (== x16) to calculate address 9490b57cec5SDimitry Andric writeBytesUnaligned(0xd2e00010, Addr, 4); // movz ip0, #:abs_g3:<addr> 9500b57cec5SDimitry Andric writeBytesUnaligned(0xf2c00010, Addr+4, 4); // movk ip0, #:abs_g2_nc:<addr> 9510b57cec5SDimitry Andric writeBytesUnaligned(0xf2a00010, Addr+8, 4); // movk ip0, #:abs_g1_nc:<addr> 9520b57cec5SDimitry Andric writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr> 9530b57cec5SDimitry Andric writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0 9540b57cec5SDimitry Andric 9550b57cec5SDimitry Andric return Addr; 9560b57cec5SDimitry Andric } else if (Arch == Triple::arm || Arch == Triple::armeb) { 9570b57cec5SDimitry Andric // TODO: There is only ARM far stub now. We should add the Thumb stub, 9580b57cec5SDimitry Andric // and stubs for branches Thumb - ARM and ARM - Thumb. 9590b57cec5SDimitry Andric writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc, [pc, #-4] 9600b57cec5SDimitry Andric return Addr + 4; 9610b57cec5SDimitry Andric } else if (IsMipsO32ABI || IsMipsN32ABI) { 9620b57cec5SDimitry Andric // 0: 3c190000 lui t9,%hi(addr). 9630b57cec5SDimitry Andric // 4: 27390000 addiu t9,t9,%lo(addr). 9640b57cec5SDimitry Andric // 8: 03200008 jr t9. 9650b57cec5SDimitry Andric // c: 00000000 nop. 9660b57cec5SDimitry Andric const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; 9670b57cec5SDimitry Andric const unsigned NopInstr = 0x0; 9680b57cec5SDimitry Andric unsigned JrT9Instr = 0x03200008; 9690b57cec5SDimitry Andric if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6 || 9700b57cec5SDimitry Andric (AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6) 9710b57cec5SDimitry Andric JrT9Instr = 0x03200009; 9720b57cec5SDimitry Andric 9730b57cec5SDimitry Andric writeBytesUnaligned(LuiT9Instr, Addr, 4); 9740b57cec5SDimitry Andric writeBytesUnaligned(AdduiT9Instr, Addr + 4, 4); 9750b57cec5SDimitry Andric writeBytesUnaligned(JrT9Instr, Addr + 8, 4); 9760b57cec5SDimitry Andric writeBytesUnaligned(NopInstr, Addr + 12, 4); 9770b57cec5SDimitry Andric return Addr; 9780b57cec5SDimitry Andric } else if (IsMipsN64ABI) { 9790b57cec5SDimitry Andric // 0: 3c190000 lui t9,%highest(addr). 9800b57cec5SDimitry Andric // 4: 67390000 daddiu t9,t9,%higher(addr). 9810b57cec5SDimitry Andric // 8: 0019CC38 dsll t9,t9,16. 9820b57cec5SDimitry Andric // c: 67390000 daddiu t9,t9,%hi(addr). 9830b57cec5SDimitry Andric // 10: 0019CC38 dsll t9,t9,16. 9840b57cec5SDimitry Andric // 14: 67390000 daddiu t9,t9,%lo(addr). 9850b57cec5SDimitry Andric // 18: 03200008 jr t9. 9860b57cec5SDimitry Andric // 1c: 00000000 nop. 9870b57cec5SDimitry Andric const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000, 9880b57cec5SDimitry Andric DsllT9Instr = 0x19CC38; 9890b57cec5SDimitry Andric const unsigned NopInstr = 0x0; 9900b57cec5SDimitry Andric unsigned JrT9Instr = 0x03200008; 9910b57cec5SDimitry Andric if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6) 9920b57cec5SDimitry Andric JrT9Instr = 0x03200009; 9930b57cec5SDimitry Andric 9940b57cec5SDimitry Andric writeBytesUnaligned(LuiT9Instr, Addr, 4); 9950b57cec5SDimitry Andric writeBytesUnaligned(DaddiuT9Instr, Addr + 4, 4); 9960b57cec5SDimitry Andric writeBytesUnaligned(DsllT9Instr, Addr + 8, 4); 9970b57cec5SDimitry Andric writeBytesUnaligned(DaddiuT9Instr, Addr + 12, 4); 9980b57cec5SDimitry Andric writeBytesUnaligned(DsllT9Instr, Addr + 16, 4); 9990b57cec5SDimitry Andric writeBytesUnaligned(DaddiuT9Instr, Addr + 20, 4); 10000b57cec5SDimitry Andric writeBytesUnaligned(JrT9Instr, Addr + 24, 4); 10010b57cec5SDimitry Andric writeBytesUnaligned(NopInstr, Addr + 28, 4); 10020b57cec5SDimitry Andric return Addr; 10030b57cec5SDimitry Andric } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { 10040b57cec5SDimitry Andric // Depending on which version of the ELF ABI is in use, we need to 10050b57cec5SDimitry Andric // generate one of two variants of the stub. They both start with 10060b57cec5SDimitry Andric // the same sequence to load the target address into r12. 10070b57cec5SDimitry Andric writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr) 10080b57cec5SDimitry Andric writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr) 10090b57cec5SDimitry Andric writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32 10100b57cec5SDimitry Andric writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr) 10110b57cec5SDimitry Andric writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr) 10120b57cec5SDimitry Andric if (AbiVariant == 2) { 10130b57cec5SDimitry Andric // PowerPC64 stub ELFv2 ABI: The address points to the function itself. 10140b57cec5SDimitry Andric // The address is already in r12 as required by the ABI. Branch to it. 10150b57cec5SDimitry Andric writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1) 10160b57cec5SDimitry Andric writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12 10170b57cec5SDimitry Andric writeInt32BE(Addr+28, 0x4E800420); // bctr 10180b57cec5SDimitry Andric } else { 10190b57cec5SDimitry Andric // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor. 10200b57cec5SDimitry Andric // Load the function address on r11 and sets it to control register. Also 10210b57cec5SDimitry Andric // loads the function TOC in r2 and environment pointer to r11. 10220b57cec5SDimitry Andric writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1) 10230b57cec5SDimitry Andric writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12) 10240b57cec5SDimitry Andric writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12) 10250b57cec5SDimitry Andric writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11 10260b57cec5SDimitry Andric writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2) 10270b57cec5SDimitry Andric writeInt32BE(Addr+40, 0x4E800420); // bctr 10280b57cec5SDimitry Andric } 10290b57cec5SDimitry Andric return Addr; 10300b57cec5SDimitry Andric } else if (Arch == Triple::systemz) { 10310b57cec5SDimitry Andric writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8 10320b57cec5SDimitry Andric writeInt16BE(Addr+2, 0x0000); 10330b57cec5SDimitry Andric writeInt16BE(Addr+4, 0x0004); 10340b57cec5SDimitry Andric writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1 10350b57cec5SDimitry Andric // 8-byte address stored at Addr + 8 10360b57cec5SDimitry Andric return Addr; 10370b57cec5SDimitry Andric } else if (Arch == Triple::x86_64) { 10380b57cec5SDimitry Andric *Addr = 0xFF; // jmp 10390b57cec5SDimitry Andric *(Addr+1) = 0x25; // rip 10400b57cec5SDimitry Andric // 32-bit PC-relative address of the GOT entry will be stored at Addr+2 10410b57cec5SDimitry Andric } else if (Arch == Triple::x86) { 10420b57cec5SDimitry Andric *Addr = 0xE9; // 32-bit pc-relative jump. 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric return Addr; 10450b57cec5SDimitry Andric } 10460b57cec5SDimitry Andric 10470b57cec5SDimitry Andric // Assign an address to a symbol name and resolve all the relocations 10480b57cec5SDimitry Andric // associated with it. 10490b57cec5SDimitry Andric void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, 10500b57cec5SDimitry Andric uint64_t Addr) { 10510b57cec5SDimitry Andric // The address to use for relocation resolution is not 10520b57cec5SDimitry Andric // the address of the local section buffer. We must be doing 10530b57cec5SDimitry Andric // a remote execution environment of some sort. Relocations can't 10540b57cec5SDimitry Andric // be applied until all the sections have been moved. The client must 10550b57cec5SDimitry Andric // trigger this with a call to MCJIT::finalize() or 10560b57cec5SDimitry Andric // RuntimeDyld::resolveRelocations(). 10570b57cec5SDimitry Andric // 10580b57cec5SDimitry Andric // Addr is a uint64_t because we can't assume the pointer width 10590b57cec5SDimitry Andric // of the target is the same as that of the host. Just use a generic 10600b57cec5SDimitry Andric // "big enough" type. 10610b57cec5SDimitry Andric LLVM_DEBUG( 10620b57cec5SDimitry Andric dbgs() << "Reassigning address for section " << SectionID << " (" 10630b57cec5SDimitry Andric << Sections[SectionID].getName() << "): " 10640b57cec5SDimitry Andric << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress()) 10650b57cec5SDimitry Andric << " -> " << format("0x%016" PRIx64, Addr) << "\n"); 10660b57cec5SDimitry Andric Sections[SectionID].setLoadAddress(Addr); 10670b57cec5SDimitry Andric } 10680b57cec5SDimitry Andric 10690b57cec5SDimitry Andric void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, 10700b57cec5SDimitry Andric uint64_t Value) { 10710b57cec5SDimitry Andric for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 10720b57cec5SDimitry Andric const RelocationEntry &RE = Relocs[i]; 10730b57cec5SDimitry Andric // Ignore relocations for sections that were not loaded 10740b57cec5SDimitry Andric if (Sections[RE.SectionID].getAddress() == nullptr) 10750b57cec5SDimitry Andric continue; 10760b57cec5SDimitry Andric resolveRelocation(RE, Value); 10770b57cec5SDimitry Andric } 10780b57cec5SDimitry Andric } 10790b57cec5SDimitry Andric 10800b57cec5SDimitry Andric void RuntimeDyldImpl::applyExternalSymbolRelocations( 10810b57cec5SDimitry Andric const StringMap<JITEvaluatedSymbol> ExternalSymbolMap) { 10820b57cec5SDimitry Andric while (!ExternalSymbolRelocations.empty()) { 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric StringMap<RelocationList>::iterator i = ExternalSymbolRelocations.begin(); 10850b57cec5SDimitry Andric 10860b57cec5SDimitry Andric StringRef Name = i->first(); 10870b57cec5SDimitry Andric if (Name.size() == 0) { 10880b57cec5SDimitry Andric // This is an absolute symbol, use an address of zero. 10890b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Resolving absolute relocations." 10900b57cec5SDimitry Andric << "\n"); 10910b57cec5SDimitry Andric RelocationList &Relocs = i->second; 10920b57cec5SDimitry Andric resolveRelocationList(Relocs, 0); 10930b57cec5SDimitry Andric } else { 10940b57cec5SDimitry Andric uint64_t Addr = 0; 10950b57cec5SDimitry Andric JITSymbolFlags Flags; 10960b57cec5SDimitry Andric RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name); 10970b57cec5SDimitry Andric if (Loc == GlobalSymbolTable.end()) { 10980b57cec5SDimitry Andric auto RRI = ExternalSymbolMap.find(Name); 10990b57cec5SDimitry Andric assert(RRI != ExternalSymbolMap.end() && "No result for symbol"); 11000b57cec5SDimitry Andric Addr = RRI->second.getAddress(); 11010b57cec5SDimitry Andric Flags = RRI->second.getFlags(); 11020b57cec5SDimitry Andric // The call to getSymbolAddress may have caused additional modules to 11030b57cec5SDimitry Andric // be loaded, which may have added new entries to the 11040b57cec5SDimitry Andric // ExternalSymbolRelocations map. Consquently, we need to update our 11050b57cec5SDimitry Andric // iterator. This is also why retrieval of the relocation list 11060b57cec5SDimitry Andric // associated with this symbol is deferred until below this point. 11070b57cec5SDimitry Andric // New entries may have been added to the relocation list. 11080b57cec5SDimitry Andric i = ExternalSymbolRelocations.find(Name); 11090b57cec5SDimitry Andric } else { 11100b57cec5SDimitry Andric // We found the symbol in our global table. It was probably in a 11110b57cec5SDimitry Andric // Module that we loaded previously. 11120b57cec5SDimitry Andric const auto &SymInfo = Loc->second; 11130b57cec5SDimitry Andric Addr = getSectionLoadAddress(SymInfo.getSectionID()) + 11140b57cec5SDimitry Andric SymInfo.getOffset(); 11150b57cec5SDimitry Andric Flags = SymInfo.getFlags(); 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric // FIXME: Implement error handling that doesn't kill the host program! 11190b57cec5SDimitry Andric if (!Addr) 11200b57cec5SDimitry Andric report_fatal_error("Program used external function '" + Name + 11210b57cec5SDimitry Andric "' which could not be resolved!"); 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric // If Resolver returned UINT64_MAX, the client wants to handle this symbol 11240b57cec5SDimitry Andric // manually and we shouldn't resolve its relocations. 11250b57cec5SDimitry Andric if (Addr != UINT64_MAX) { 11260b57cec5SDimitry Andric 11270b57cec5SDimitry Andric // Tweak the address based on the symbol flags if necessary. 11280b57cec5SDimitry Andric // For example, this is used by RuntimeDyldMachOARM to toggle the low bit 11290b57cec5SDimitry Andric // if the target symbol is Thumb. 11300b57cec5SDimitry Andric Addr = modifyAddressBasedOnFlags(Addr, Flags); 11310b57cec5SDimitry Andric 11320b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t" 11330b57cec5SDimitry Andric << format("0x%lx", Addr) << "\n"); 11340b57cec5SDimitry Andric // This list may have been updated when we called getSymbolAddress, so 11350b57cec5SDimitry Andric // don't change this code to get the list earlier. 11360b57cec5SDimitry Andric RelocationList &Relocs = i->second; 11370b57cec5SDimitry Andric resolveRelocationList(Relocs, Addr); 11380b57cec5SDimitry Andric } 11390b57cec5SDimitry Andric } 11400b57cec5SDimitry Andric 11410b57cec5SDimitry Andric ExternalSymbolRelocations.erase(i); 11420b57cec5SDimitry Andric } 11430b57cec5SDimitry Andric } 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric Error RuntimeDyldImpl::resolveExternalSymbols() { 11460b57cec5SDimitry Andric StringMap<JITEvaluatedSymbol> ExternalSymbolMap; 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric // Resolution can trigger emission of more symbols, so iterate until 11490b57cec5SDimitry Andric // we've resolved *everything*. 11500b57cec5SDimitry Andric { 11510b57cec5SDimitry Andric JITSymbolResolver::LookupSet ResolvedSymbols; 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric while (true) { 11540b57cec5SDimitry Andric JITSymbolResolver::LookupSet NewSymbols; 11550b57cec5SDimitry Andric 11560b57cec5SDimitry Andric for (auto &RelocKV : ExternalSymbolRelocations) { 11570b57cec5SDimitry Andric StringRef Name = RelocKV.first(); 11580b57cec5SDimitry Andric if (!Name.empty() && !GlobalSymbolTable.count(Name) && 11590b57cec5SDimitry Andric !ResolvedSymbols.count(Name)) 11600b57cec5SDimitry Andric NewSymbols.insert(Name); 11610b57cec5SDimitry Andric } 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric if (NewSymbols.empty()) 11640b57cec5SDimitry Andric break; 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric #ifdef _MSC_VER 11670b57cec5SDimitry Andric using ExpectedLookupResult = 11680b57cec5SDimitry Andric MSVCPExpected<JITSymbolResolver::LookupResult>; 11690b57cec5SDimitry Andric #else 11700b57cec5SDimitry Andric using ExpectedLookupResult = Expected<JITSymbolResolver::LookupResult>; 11710b57cec5SDimitry Andric #endif 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric auto NewSymbolsP = std::make_shared<std::promise<ExpectedLookupResult>>(); 11740b57cec5SDimitry Andric auto NewSymbolsF = NewSymbolsP->get_future(); 11750b57cec5SDimitry Andric Resolver.lookup(NewSymbols, 11760b57cec5SDimitry Andric [=](Expected<JITSymbolResolver::LookupResult> Result) { 11770b57cec5SDimitry Andric NewSymbolsP->set_value(std::move(Result)); 11780b57cec5SDimitry Andric }); 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andric auto NewResolverResults = NewSymbolsF.get(); 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric if (!NewResolverResults) 11830b57cec5SDimitry Andric return NewResolverResults.takeError(); 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric assert(NewResolverResults->size() == NewSymbols.size() && 11860b57cec5SDimitry Andric "Should have errored on unresolved symbols"); 11870b57cec5SDimitry Andric 11880b57cec5SDimitry Andric for (auto &RRKV : *NewResolverResults) { 11890b57cec5SDimitry Andric assert(!ResolvedSymbols.count(RRKV.first) && "Redundant resolution?"); 11900b57cec5SDimitry Andric ExternalSymbolMap.insert(RRKV); 11910b57cec5SDimitry Andric ResolvedSymbols.insert(RRKV.first); 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric } 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric applyExternalSymbolRelocations(ExternalSymbolMap); 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric return Error::success(); 11990b57cec5SDimitry Andric } 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric void RuntimeDyldImpl::finalizeAsync( 12028bcb0991SDimitry Andric std::unique_ptr<RuntimeDyldImpl> This, 1203*5ffd83dbSDimitry Andric unique_function<void(object::OwningBinary<object::ObjectFile>, Error)> 1204*5ffd83dbSDimitry Andric OnEmitted, 1205*5ffd83dbSDimitry Andric object::OwningBinary<object::ObjectFile> O) { 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric auto SharedThis = std::shared_ptr<RuntimeDyldImpl>(std::move(This)); 12080b57cec5SDimitry Andric auto PostResolveContinuation = 1209*5ffd83dbSDimitry Andric [SharedThis, OnEmitted = std::move(OnEmitted), O = std::move(O)]( 12108bcb0991SDimitry Andric Expected<JITSymbolResolver::LookupResult> Result) mutable { 12110b57cec5SDimitry Andric if (!Result) { 1212*5ffd83dbSDimitry Andric OnEmitted(std::move(O), Result.takeError()); 12130b57cec5SDimitry Andric return; 12140b57cec5SDimitry Andric } 12150b57cec5SDimitry Andric 12160b57cec5SDimitry Andric /// Copy the result into a StringMap, where the keys are held by value. 12170b57cec5SDimitry Andric StringMap<JITEvaluatedSymbol> Resolved; 12180b57cec5SDimitry Andric for (auto &KV : *Result) 12190b57cec5SDimitry Andric Resolved[KV.first] = KV.second; 12200b57cec5SDimitry Andric 12210b57cec5SDimitry Andric SharedThis->applyExternalSymbolRelocations(Resolved); 12220b57cec5SDimitry Andric SharedThis->resolveLocalRelocations(); 12230b57cec5SDimitry Andric SharedThis->registerEHFrames(); 12240b57cec5SDimitry Andric std::string ErrMsg; 12250b57cec5SDimitry Andric if (SharedThis->MemMgr.finalizeMemory(&ErrMsg)) 1226*5ffd83dbSDimitry Andric OnEmitted(std::move(O), 1227*5ffd83dbSDimitry Andric make_error<StringError>(std::move(ErrMsg), 12280b57cec5SDimitry Andric inconvertibleErrorCode())); 12290b57cec5SDimitry Andric else 1230*5ffd83dbSDimitry Andric OnEmitted(std::move(O), Error::success()); 12310b57cec5SDimitry Andric }; 12320b57cec5SDimitry Andric 12330b57cec5SDimitry Andric JITSymbolResolver::LookupSet Symbols; 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric for (auto &RelocKV : SharedThis->ExternalSymbolRelocations) { 12360b57cec5SDimitry Andric StringRef Name = RelocKV.first(); 12370b57cec5SDimitry Andric assert(!Name.empty() && "Symbol has no name?"); 12380b57cec5SDimitry Andric assert(!SharedThis->GlobalSymbolTable.count(Name) && 12390b57cec5SDimitry Andric "Name already processed. RuntimeDyld instances can not be re-used " 12400b57cec5SDimitry Andric "when finalizing with finalizeAsync."); 12410b57cec5SDimitry Andric Symbols.insert(Name); 12420b57cec5SDimitry Andric } 12430b57cec5SDimitry Andric 12440b57cec5SDimitry Andric if (!Symbols.empty()) { 12458bcb0991SDimitry Andric SharedThis->Resolver.lookup(Symbols, std::move(PostResolveContinuation)); 12460b57cec5SDimitry Andric } else 12470b57cec5SDimitry Andric PostResolveContinuation(std::map<StringRef, JITEvaluatedSymbol>()); 12480b57cec5SDimitry Andric } 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12510b57cec5SDimitry Andric // RuntimeDyld class implementation 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress( 12540b57cec5SDimitry Andric const object::SectionRef &Sec) const { 12550b57cec5SDimitry Andric 12560b57cec5SDimitry Andric auto I = ObjSecToIDMap.find(Sec); 12570b57cec5SDimitry Andric if (I != ObjSecToIDMap.end()) 12580b57cec5SDimitry Andric return RTDyld.Sections[I->second].getLoadAddress(); 12590b57cec5SDimitry Andric 12600b57cec5SDimitry Andric return 0; 12610b57cec5SDimitry Andric } 12620b57cec5SDimitry Andric 12630b57cec5SDimitry Andric void RuntimeDyld::MemoryManager::anchor() {} 12640b57cec5SDimitry Andric void JITSymbolResolver::anchor() {} 12650b57cec5SDimitry Andric void LegacyJITSymbolResolver::anchor() {} 12660b57cec5SDimitry Andric 12670b57cec5SDimitry Andric RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr, 12680b57cec5SDimitry Andric JITSymbolResolver &Resolver) 12690b57cec5SDimitry Andric : MemMgr(MemMgr), Resolver(Resolver) { 12700b57cec5SDimitry Andric // FIXME: There's a potential issue lurking here if a single instance of 12710b57cec5SDimitry Andric // RuntimeDyld is used to load multiple objects. The current implementation 12720b57cec5SDimitry Andric // associates a single memory manager with a RuntimeDyld instance. Even 12730b57cec5SDimitry Andric // though the public class spawns a new 'impl' instance for each load, 12740b57cec5SDimitry Andric // they share a single memory manager. This can become a problem when page 12750b57cec5SDimitry Andric // permissions are applied. 12760b57cec5SDimitry Andric Dyld = nullptr; 12770b57cec5SDimitry Andric ProcessAllSections = false; 12780b57cec5SDimitry Andric } 12790b57cec5SDimitry Andric 12800b57cec5SDimitry Andric RuntimeDyld::~RuntimeDyld() {} 12810b57cec5SDimitry Andric 12820b57cec5SDimitry Andric static std::unique_ptr<RuntimeDyldCOFF> 12830b57cec5SDimitry Andric createRuntimeDyldCOFF( 12840b57cec5SDimitry Andric Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 12850b57cec5SDimitry Andric JITSymbolResolver &Resolver, bool ProcessAllSections, 12860b57cec5SDimitry Andric RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { 12870b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldCOFF> Dyld = 12880b57cec5SDimitry Andric RuntimeDyldCOFF::create(Arch, MM, Resolver); 12890b57cec5SDimitry Andric Dyld->setProcessAllSections(ProcessAllSections); 12900b57cec5SDimitry Andric Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted)); 12910b57cec5SDimitry Andric return Dyld; 12920b57cec5SDimitry Andric } 12930b57cec5SDimitry Andric 12940b57cec5SDimitry Andric static std::unique_ptr<RuntimeDyldELF> 12950b57cec5SDimitry Andric createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 12960b57cec5SDimitry Andric JITSymbolResolver &Resolver, bool ProcessAllSections, 12970b57cec5SDimitry Andric RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { 12980b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldELF> Dyld = 12990b57cec5SDimitry Andric RuntimeDyldELF::create(Arch, MM, Resolver); 13000b57cec5SDimitry Andric Dyld->setProcessAllSections(ProcessAllSections); 13010b57cec5SDimitry Andric Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted)); 13020b57cec5SDimitry Andric return Dyld; 13030b57cec5SDimitry Andric } 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric static std::unique_ptr<RuntimeDyldMachO> 13060b57cec5SDimitry Andric createRuntimeDyldMachO( 13070b57cec5SDimitry Andric Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 13080b57cec5SDimitry Andric JITSymbolResolver &Resolver, 13090b57cec5SDimitry Andric bool ProcessAllSections, 13100b57cec5SDimitry Andric RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { 13110b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldMachO> Dyld = 13120b57cec5SDimitry Andric RuntimeDyldMachO::create(Arch, MM, Resolver); 13130b57cec5SDimitry Andric Dyld->setProcessAllSections(ProcessAllSections); 13140b57cec5SDimitry Andric Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted)); 13150b57cec5SDimitry Andric return Dyld; 13160b57cec5SDimitry Andric } 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo> 13190b57cec5SDimitry Andric RuntimeDyld::loadObject(const ObjectFile &Obj) { 13200b57cec5SDimitry Andric if (!Dyld) { 13210b57cec5SDimitry Andric if (Obj.isELF()) 13220b57cec5SDimitry Andric Dyld = 13230b57cec5SDimitry Andric createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()), 13240b57cec5SDimitry Andric MemMgr, Resolver, ProcessAllSections, 13250b57cec5SDimitry Andric std::move(NotifyStubEmitted)); 13260b57cec5SDimitry Andric else if (Obj.isMachO()) 13270b57cec5SDimitry Andric Dyld = createRuntimeDyldMachO( 13280b57cec5SDimitry Andric static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, 13290b57cec5SDimitry Andric ProcessAllSections, std::move(NotifyStubEmitted)); 13300b57cec5SDimitry Andric else if (Obj.isCOFF()) 13310b57cec5SDimitry Andric Dyld = createRuntimeDyldCOFF( 13320b57cec5SDimitry Andric static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, 13330b57cec5SDimitry Andric ProcessAllSections, std::move(NotifyStubEmitted)); 13340b57cec5SDimitry Andric else 13350b57cec5SDimitry Andric report_fatal_error("Incompatible object format!"); 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric 13380b57cec5SDimitry Andric if (!Dyld->isCompatibleFile(Obj)) 13390b57cec5SDimitry Andric report_fatal_error("Incompatible object format!"); 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric auto LoadedObjInfo = Dyld->loadObject(Obj); 13420b57cec5SDimitry Andric MemMgr.notifyObjectLoaded(*this, Obj); 13430b57cec5SDimitry Andric return LoadedObjInfo; 13440b57cec5SDimitry Andric } 13450b57cec5SDimitry Andric 13460b57cec5SDimitry Andric void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const { 13470b57cec5SDimitry Andric if (!Dyld) 13480b57cec5SDimitry Andric return nullptr; 13490b57cec5SDimitry Andric return Dyld->getSymbolLocalAddress(Name); 13500b57cec5SDimitry Andric } 13510b57cec5SDimitry Andric 13520b57cec5SDimitry Andric unsigned RuntimeDyld::getSymbolSectionID(StringRef Name) const { 13530b57cec5SDimitry Andric assert(Dyld && "No RuntimeDyld instance attached"); 13540b57cec5SDimitry Andric return Dyld->getSymbolSectionID(Name); 13550b57cec5SDimitry Andric } 13560b57cec5SDimitry Andric 13570b57cec5SDimitry Andric JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const { 13580b57cec5SDimitry Andric if (!Dyld) 13590b57cec5SDimitry Andric return nullptr; 13600b57cec5SDimitry Andric return Dyld->getSymbol(Name); 13610b57cec5SDimitry Andric } 13620b57cec5SDimitry Andric 13630b57cec5SDimitry Andric std::map<StringRef, JITEvaluatedSymbol> RuntimeDyld::getSymbolTable() const { 13640b57cec5SDimitry Andric if (!Dyld) 13650b57cec5SDimitry Andric return std::map<StringRef, JITEvaluatedSymbol>(); 13660b57cec5SDimitry Andric return Dyld->getSymbolTable(); 13670b57cec5SDimitry Andric } 13680b57cec5SDimitry Andric 13690b57cec5SDimitry Andric void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); } 13700b57cec5SDimitry Andric 13710b57cec5SDimitry Andric void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) { 13720b57cec5SDimitry Andric Dyld->reassignSectionAddress(SectionID, Addr); 13730b57cec5SDimitry Andric } 13740b57cec5SDimitry Andric 13750b57cec5SDimitry Andric void RuntimeDyld::mapSectionAddress(const void *LocalAddress, 13760b57cec5SDimitry Andric uint64_t TargetAddress) { 13770b57cec5SDimitry Andric Dyld->mapSectionAddress(LocalAddress, TargetAddress); 13780b57cec5SDimitry Andric } 13790b57cec5SDimitry Andric 13800b57cec5SDimitry Andric bool RuntimeDyld::hasError() { return Dyld->hasError(); } 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); } 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric void RuntimeDyld::finalizeWithMemoryManagerLocking() { 13850b57cec5SDimitry Andric bool MemoryFinalizationLocked = MemMgr.FinalizationLocked; 13860b57cec5SDimitry Andric MemMgr.FinalizationLocked = true; 13870b57cec5SDimitry Andric resolveRelocations(); 13880b57cec5SDimitry Andric registerEHFrames(); 13890b57cec5SDimitry Andric if (!MemoryFinalizationLocked) { 13900b57cec5SDimitry Andric MemMgr.finalizeMemory(); 13910b57cec5SDimitry Andric MemMgr.FinalizationLocked = false; 13920b57cec5SDimitry Andric } 13930b57cec5SDimitry Andric } 13940b57cec5SDimitry Andric 13950b57cec5SDimitry Andric StringRef RuntimeDyld::getSectionContent(unsigned SectionID) const { 13960b57cec5SDimitry Andric assert(Dyld && "No Dyld instance attached"); 13970b57cec5SDimitry Andric return Dyld->getSectionContent(SectionID); 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric uint64_t RuntimeDyld::getSectionLoadAddress(unsigned SectionID) const { 14010b57cec5SDimitry Andric assert(Dyld && "No Dyld instance attached"); 14020b57cec5SDimitry Andric return Dyld->getSectionLoadAddress(SectionID); 14030b57cec5SDimitry Andric } 14040b57cec5SDimitry Andric 14050b57cec5SDimitry Andric void RuntimeDyld::registerEHFrames() { 14060b57cec5SDimitry Andric if (Dyld) 14070b57cec5SDimitry Andric Dyld->registerEHFrames(); 14080b57cec5SDimitry Andric } 14090b57cec5SDimitry Andric 14100b57cec5SDimitry Andric void RuntimeDyld::deregisterEHFrames() { 14110b57cec5SDimitry Andric if (Dyld) 14120b57cec5SDimitry Andric Dyld->deregisterEHFrames(); 14130b57cec5SDimitry Andric } 14140b57cec5SDimitry Andric // FIXME: Kill this with fire once we have a new JIT linker: this is only here 14150b57cec5SDimitry Andric // so that we can re-use RuntimeDyld's implementation without twisting the 14160b57cec5SDimitry Andric // interface any further for ORC's purposes. 1417*5ffd83dbSDimitry Andric void jitLinkForORC( 1418*5ffd83dbSDimitry Andric object::OwningBinary<object::ObjectFile> O, 1419*5ffd83dbSDimitry Andric RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, 1420*5ffd83dbSDimitry Andric bool ProcessAllSections, 1421*5ffd83dbSDimitry Andric unique_function< 1422*5ffd83dbSDimitry Andric Error(const object::ObjectFile &Obj, 14230b57cec5SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo> LoadedObj, 14240b57cec5SDimitry Andric std::map<StringRef, JITEvaluatedSymbol>)> 14250b57cec5SDimitry Andric OnLoaded, 1426*5ffd83dbSDimitry Andric unique_function<void(object::OwningBinary<object::ObjectFile>, Error)> 1427*5ffd83dbSDimitry Andric OnEmitted) { 14280b57cec5SDimitry Andric 14290b57cec5SDimitry Andric RuntimeDyld RTDyld(MemMgr, Resolver); 14300b57cec5SDimitry Andric RTDyld.setProcessAllSections(ProcessAllSections); 14310b57cec5SDimitry Andric 1432*5ffd83dbSDimitry Andric auto Info = RTDyld.loadObject(*O.getBinary()); 14330b57cec5SDimitry Andric 14340b57cec5SDimitry Andric if (RTDyld.hasError()) { 1435*5ffd83dbSDimitry Andric OnEmitted(std::move(O), make_error<StringError>(RTDyld.getErrorString(), 14360b57cec5SDimitry Andric inconvertibleErrorCode())); 14370b57cec5SDimitry Andric return; 14380b57cec5SDimitry Andric } 14390b57cec5SDimitry Andric 1440*5ffd83dbSDimitry Andric if (auto Err = 1441*5ffd83dbSDimitry Andric OnLoaded(*O.getBinary(), std::move(Info), RTDyld.getSymbolTable())) 1442*5ffd83dbSDimitry Andric OnEmitted(std::move(O), std::move(Err)); 14430b57cec5SDimitry Andric 14440b57cec5SDimitry Andric RuntimeDyldImpl::finalizeAsync(std::move(RTDyld.Dyld), std::move(OnEmitted), 1445*5ffd83dbSDimitry Andric std::move(O)); 14460b57cec5SDimitry Andric } 14470b57cec5SDimitry Andric 14480b57cec5SDimitry Andric } // end namespace llvm 1449