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. 149*fe6060f1SDimitry Andric unsigned Idx = it->first; 150*fe6060f1SDimitry Andric uint64_t Addr = getSectionLoadAddress(Idx); 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()) { 2175ffd83dbSDimitry Andric Expected<uint32_t> FlagsOrErr = Sym.getFlags(); 2185ffd83dbSDimitry Andric if (!FlagsOrErr) 2195ffd83dbSDimitry Andric // TODO: Test this error. 2205ffd83dbSDimitry Andric return FlagsOrErr.takeError(); 2215ffd83dbSDimitry Andric if ((*FlagsOrErr & SymbolRef::SF_Common) || 2225ffd83dbSDimitry 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) { 2415ffd83dbSDimitry Andric Expected<uint32_t> FlagsOrErr = I->getFlags(); 2425ffd83dbSDimitry Andric if (!FlagsOrErr) 2435ffd83dbSDimitry Andric // TODO: Test this error. 2445ffd83dbSDimitry Andric return FlagsOrErr.takeError(); 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric // Skip undefined symbols. 2475ffd83dbSDimitry 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 2975ffd83dbSDimitry 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) 3105ffd83dbSDimitry Andric << " flags: " << *FlagsOrErr << "\n"); 311eaeb601bSDimitry Andric if (!Name.empty()) // Skip absolute symbol relocations. 312eaeb601bSDimitry Andric GlobalSymbolTable[Name] = 313eaeb601bSDimitry Andric SymbolTableEntry(SectionID, Addr, *JITSymFlags); 3140b57cec5SDimitry Andric } else if (SymType == object::SymbolRef::ST_Function || 3150b57cec5SDimitry Andric SymType == object::SymbolRef::ST_Data || 3160b57cec5SDimitry Andric SymType == object::SymbolRef::ST_Unknown || 3170b57cec5SDimitry Andric SymType == object::SymbolRef::ST_Other) { 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric section_iterator SI = Obj.section_end(); 3200b57cec5SDimitry Andric if (auto SIOrErr = I->getSection()) 3210b57cec5SDimitry Andric SI = *SIOrErr; 3220b57cec5SDimitry Andric else 3230b57cec5SDimitry Andric return SIOrErr.takeError(); 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric if (SI == Obj.section_end()) 3260b57cec5SDimitry Andric continue; 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric // Get symbol offset. 3290b57cec5SDimitry Andric uint64_t SectOffset; 3300b57cec5SDimitry Andric if (auto Err = getOffset(*I, *SI, SectOffset)) 3310b57cec5SDimitry Andric return std::move(Err); 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric bool IsCode = SI->isText(); 3340b57cec5SDimitry Andric unsigned SectionID; 3350b57cec5SDimitry Andric if (auto SectionIDOrErr = 3360b57cec5SDimitry Andric findOrEmitSection(Obj, *SI, IsCode, LocalSections)) 3370b57cec5SDimitry Andric SectionID = *SectionIDOrErr; 3380b57cec5SDimitry Andric else 3390b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tType: " << SymType << " Name: " << Name 3420b57cec5SDimitry Andric << " SID: " << SectionID 3430b57cec5SDimitry Andric << " Offset: " << format("%p", (uintptr_t)SectOffset) 3445ffd83dbSDimitry Andric << " flags: " << *FlagsOrErr << "\n"); 345eaeb601bSDimitry Andric if (!Name.empty()) // Skip absolute symbol relocations 3460b57cec5SDimitry Andric GlobalSymbolTable[Name] = 3470b57cec5SDimitry Andric SymbolTableEntry(SectionID, SectOffset, *JITSymFlags); 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric // Allocate common symbols 3520b57cec5SDimitry Andric if (auto Err = emitCommonSymbols(Obj, CommonSymbolsToAllocate, CommonSize, 3530b57cec5SDimitry Andric CommonAlign)) 3540b57cec5SDimitry Andric return std::move(Err); 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric // Parse and process relocations 3570b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Parse relocations:\n"); 3580b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 3590b57cec5SDimitry Andric SI != SE; ++SI) { 3600b57cec5SDimitry Andric StubMap Stubs; 3610b57cec5SDimitry Andric 3628bcb0991SDimitry Andric Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection(); 3638bcb0991SDimitry Andric if (!RelSecOrErr) 3648bcb0991SDimitry Andric return RelSecOrErr.takeError(); 3658bcb0991SDimitry Andric 3668bcb0991SDimitry Andric section_iterator RelocatedSection = *RelSecOrErr; 3670b57cec5SDimitry Andric if (RelocatedSection == SE) 3680b57cec5SDimitry Andric continue; 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric relocation_iterator I = SI->relocation_begin(); 3710b57cec5SDimitry Andric relocation_iterator E = SI->relocation_end(); 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric if (I == E && !ProcessAllSections) 3740b57cec5SDimitry Andric continue; 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric bool IsCode = RelocatedSection->isText(); 3770b57cec5SDimitry Andric unsigned SectionID = 0; 3780b57cec5SDimitry Andric if (auto SectionIDOrErr = findOrEmitSection(Obj, *RelocatedSection, IsCode, 3790b57cec5SDimitry Andric LocalSections)) 3800b57cec5SDimitry Andric SectionID = *SectionIDOrErr; 3810b57cec5SDimitry Andric else 3820b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tSectionID: " << SectionID << "\n"); 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric for (; I != E;) 3870b57cec5SDimitry Andric if (auto IOrErr = processRelocationRef(SectionID, I, Obj, LocalSections, Stubs)) 3880b57cec5SDimitry Andric I = *IOrErr; 3890b57cec5SDimitry Andric else 3900b57cec5SDimitry Andric return IOrErr.takeError(); 3910b57cec5SDimitry Andric 3920b57cec5SDimitry Andric // If there is a NotifyStubEmitted callback set, call it to register any 3930b57cec5SDimitry Andric // stubs created for this section. 3940b57cec5SDimitry Andric if (NotifyStubEmitted) { 3950b57cec5SDimitry Andric StringRef FileName = Obj.getFileName(); 3960b57cec5SDimitry Andric StringRef SectionName = Sections[SectionID].getName(); 3970b57cec5SDimitry Andric for (auto &KV : Stubs) { 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric auto &VR = KV.first; 4000b57cec5SDimitry Andric uint64_t StubAddr = KV.second; 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric // If this is a named stub, just call NotifyStubEmitted. 4030b57cec5SDimitry Andric if (VR.SymbolName) { 4040b57cec5SDimitry Andric NotifyStubEmitted(FileName, SectionName, VR.SymbolName, SectionID, 4050b57cec5SDimitry Andric StubAddr); 4060b57cec5SDimitry Andric continue; 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric // Otherwise we will have to try a reverse lookup on the globla symbol table. 4100b57cec5SDimitry Andric for (auto &GSTMapEntry : GlobalSymbolTable) { 4110b57cec5SDimitry Andric StringRef SymbolName = GSTMapEntry.first(); 4120b57cec5SDimitry Andric auto &GSTEntry = GSTMapEntry.second; 4130b57cec5SDimitry Andric if (GSTEntry.getSectionID() == VR.SectionID && 4140b57cec5SDimitry Andric GSTEntry.getOffset() == VR.Offset) { 4150b57cec5SDimitry Andric NotifyStubEmitted(FileName, SectionName, SymbolName, SectionID, 4160b57cec5SDimitry Andric StubAddr); 4170b57cec5SDimitry Andric break; 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric } 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric // Process remaining sections 4250b57cec5SDimitry Andric if (ProcessAllSections) { 4260b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Process remaining sections:\n"); 4270b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 4280b57cec5SDimitry Andric SI != SE; ++SI) { 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric /* Ignore already loaded sections */ 4310b57cec5SDimitry Andric if (LocalSections.find(*SI) != LocalSections.end()) 4320b57cec5SDimitry Andric continue; 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric bool IsCode = SI->isText(); 4350b57cec5SDimitry Andric if (auto SectionIDOrErr = 4360b57cec5SDimitry Andric findOrEmitSection(Obj, *SI, IsCode, LocalSections)) 4370b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\tSectionID: " << (*SectionIDOrErr) << "\n"); 4380b57cec5SDimitry Andric else 4390b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 4400b57cec5SDimitry Andric } 4410b57cec5SDimitry Andric } 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric // Give the subclasses a chance to tie-up any loose ends. 4440b57cec5SDimitry Andric if (auto Err = finalizeLoad(Obj, LocalSections)) 4450b57cec5SDimitry Andric return std::move(Err); 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric // for (auto E : LocalSections) 4480b57cec5SDimitry Andric // llvm::dbgs() << "Added: " << E.first.getRawDataRefImpl() << " -> " << E.second << "\n"; 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric return LocalSections; 4510b57cec5SDimitry Andric } 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric // A helper method for computeTotalAllocSize. 4540b57cec5SDimitry Andric // Computes the memory size required to allocate sections with the given sizes, 4550b57cec5SDimitry Andric // assuming that all sections are allocated with the given alignment 4560b57cec5SDimitry Andric static uint64_t 4570b57cec5SDimitry Andric computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes, 4580b57cec5SDimitry Andric uint64_t Alignment) { 4590b57cec5SDimitry Andric uint64_t TotalSize = 0; 4600b57cec5SDimitry Andric for (size_t Idx = 0, Cnt = SectionSizes.size(); Idx < Cnt; Idx++) { 4610b57cec5SDimitry Andric uint64_t AlignedSize = 4620b57cec5SDimitry Andric (SectionSizes[Idx] + Alignment - 1) / Alignment * Alignment; 4630b57cec5SDimitry Andric TotalSize += AlignedSize; 4640b57cec5SDimitry Andric } 4650b57cec5SDimitry Andric return TotalSize; 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric static bool isRequiredForExecution(const SectionRef Section) { 4690b57cec5SDimitry Andric const ObjectFile *Obj = Section.getObject(); 4700b57cec5SDimitry Andric if (isa<object::ELFObjectFileBase>(Obj)) 4710b57cec5SDimitry Andric return ELFSectionRef(Section).getFlags() & ELF::SHF_ALLOC; 4720b57cec5SDimitry Andric if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) { 4730b57cec5SDimitry Andric const coff_section *CoffSection = COFFObj->getCOFFSection(Section); 4740b57cec5SDimitry Andric // Avoid loading zero-sized COFF sections. 4750b57cec5SDimitry Andric // In PE files, VirtualSize gives the section size, and SizeOfRawData 4760b57cec5SDimitry Andric // may be zero for sections with content. In Obj files, SizeOfRawData 4770b57cec5SDimitry Andric // gives the section size, and VirtualSize is always zero. Hence 4780b57cec5SDimitry Andric // the need to check for both cases below. 4790b57cec5SDimitry Andric bool HasContent = 4800b57cec5SDimitry Andric (CoffSection->VirtualSize > 0) || (CoffSection->SizeOfRawData > 0); 4810b57cec5SDimitry Andric bool IsDiscardable = 4820b57cec5SDimitry Andric CoffSection->Characteristics & 4830b57cec5SDimitry Andric (COFF::IMAGE_SCN_MEM_DISCARDABLE | COFF::IMAGE_SCN_LNK_INFO); 4840b57cec5SDimitry Andric return HasContent && !IsDiscardable; 4850b57cec5SDimitry Andric } 4860b57cec5SDimitry Andric 4870b57cec5SDimitry Andric assert(isa<MachOObjectFile>(Obj)); 4880b57cec5SDimitry Andric return true; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric static bool isReadOnlyData(const SectionRef Section) { 4920b57cec5SDimitry Andric const ObjectFile *Obj = Section.getObject(); 4930b57cec5SDimitry Andric if (isa<object::ELFObjectFileBase>(Obj)) 4940b57cec5SDimitry Andric return !(ELFSectionRef(Section).getFlags() & 4950b57cec5SDimitry Andric (ELF::SHF_WRITE | ELF::SHF_EXECINSTR)); 4960b57cec5SDimitry Andric if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) 4970b57cec5SDimitry Andric return ((COFFObj->getCOFFSection(Section)->Characteristics & 4980b57cec5SDimitry Andric (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA 4990b57cec5SDimitry Andric | COFF::IMAGE_SCN_MEM_READ 5000b57cec5SDimitry Andric | COFF::IMAGE_SCN_MEM_WRITE)) 5010b57cec5SDimitry Andric == 5020b57cec5SDimitry Andric (COFF::IMAGE_SCN_CNT_INITIALIZED_DATA 5030b57cec5SDimitry Andric | COFF::IMAGE_SCN_MEM_READ)); 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric assert(isa<MachOObjectFile>(Obj)); 5060b57cec5SDimitry Andric return false; 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric static bool isZeroInit(const SectionRef Section) { 5100b57cec5SDimitry Andric const ObjectFile *Obj = Section.getObject(); 5110b57cec5SDimitry Andric if (isa<object::ELFObjectFileBase>(Obj)) 5120b57cec5SDimitry Andric return ELFSectionRef(Section).getType() == ELF::SHT_NOBITS; 5130b57cec5SDimitry Andric if (auto *COFFObj = dyn_cast<object::COFFObjectFile>(Obj)) 5140b57cec5SDimitry Andric return COFFObj->getCOFFSection(Section)->Characteristics & 5150b57cec5SDimitry Andric COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric auto *MachO = cast<MachOObjectFile>(Obj); 5180b57cec5SDimitry Andric unsigned SectionType = MachO->getSectionType(Section); 5190b57cec5SDimitry Andric return SectionType == MachO::S_ZEROFILL || 5200b57cec5SDimitry Andric SectionType == MachO::S_GB_ZEROFILL; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric // Compute an upper bound of the memory size that is required to load all 5240b57cec5SDimitry Andric // sections 5250b57cec5SDimitry Andric Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj, 5260b57cec5SDimitry Andric uint64_t &CodeSize, 5270b57cec5SDimitry Andric uint32_t &CodeAlign, 5280b57cec5SDimitry Andric uint64_t &RODataSize, 5290b57cec5SDimitry Andric uint32_t &RODataAlign, 5300b57cec5SDimitry Andric uint64_t &RWDataSize, 5310b57cec5SDimitry Andric uint32_t &RWDataAlign) { 5320b57cec5SDimitry Andric // Compute the size of all sections required for execution 5330b57cec5SDimitry Andric std::vector<uint64_t> CodeSectionSizes; 5340b57cec5SDimitry Andric std::vector<uint64_t> ROSectionSizes; 5350b57cec5SDimitry Andric std::vector<uint64_t> RWSectionSizes; 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric // Collect sizes of all sections to be loaded; 5380b57cec5SDimitry Andric // also determine the max alignment of all sections 5390b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 5400b57cec5SDimitry Andric SI != SE; ++SI) { 5410b57cec5SDimitry Andric const SectionRef &Section = *SI; 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric bool IsRequired = isRequiredForExecution(Section) || ProcessAllSections; 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric // Consider only the sections that are required to be loaded for execution 5460b57cec5SDimitry Andric if (IsRequired) { 5470b57cec5SDimitry Andric uint64_t DataSize = Section.getSize(); 5480b57cec5SDimitry Andric uint64_t Alignment64 = Section.getAlignment(); 5490b57cec5SDimitry Andric unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 5500b57cec5SDimitry Andric bool IsCode = Section.isText(); 5510b57cec5SDimitry Andric bool IsReadOnly = isReadOnlyData(Section); 5520b57cec5SDimitry Andric 5538bcb0991SDimitry Andric Expected<StringRef> NameOrErr = Section.getName(); 5548bcb0991SDimitry Andric if (!NameOrErr) 5558bcb0991SDimitry Andric return NameOrErr.takeError(); 5568bcb0991SDimitry Andric StringRef Name = *NameOrErr; 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric uint64_t StubBufSize = computeSectionStubBufSize(Obj, Section); 5590b57cec5SDimitry Andric 5600b57cec5SDimitry Andric uint64_t PaddingSize = 0; 5610b57cec5SDimitry Andric if (Name == ".eh_frame") 5620b57cec5SDimitry Andric PaddingSize += 4; 5630b57cec5SDimitry Andric if (StubBufSize != 0) 5640b57cec5SDimitry Andric PaddingSize += getStubAlignment() - 1; 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric uint64_t SectionSize = DataSize + PaddingSize + StubBufSize; 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric // The .eh_frame section (at least on Linux) needs an extra four bytes 5690b57cec5SDimitry Andric // padded 5700b57cec5SDimitry Andric // with zeroes added at the end. For MachO objects, this section has a 5710b57cec5SDimitry Andric // slightly different name, so this won't have any effect for MachO 5720b57cec5SDimitry Andric // objects. 5730b57cec5SDimitry Andric if (Name == ".eh_frame") 5740b57cec5SDimitry Andric SectionSize += 4; 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric if (!SectionSize) 5770b57cec5SDimitry Andric SectionSize = 1; 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric if (IsCode) { 5800b57cec5SDimitry Andric CodeAlign = std::max(CodeAlign, Alignment); 5810b57cec5SDimitry Andric CodeSectionSizes.push_back(SectionSize); 5820b57cec5SDimitry Andric } else if (IsReadOnly) { 5830b57cec5SDimitry Andric RODataAlign = std::max(RODataAlign, Alignment); 5840b57cec5SDimitry Andric ROSectionSizes.push_back(SectionSize); 5850b57cec5SDimitry Andric } else { 5860b57cec5SDimitry Andric RWDataAlign = std::max(RWDataAlign, Alignment); 5870b57cec5SDimitry Andric RWSectionSizes.push_back(SectionSize); 5880b57cec5SDimitry Andric } 5890b57cec5SDimitry Andric } 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric // Compute Global Offset Table size. If it is not zero we 5930b57cec5SDimitry Andric // also update alignment, which is equal to a size of a 5940b57cec5SDimitry Andric // single GOT entry. 5950b57cec5SDimitry Andric if (unsigned GotSize = computeGOTSize(Obj)) { 5960b57cec5SDimitry Andric RWSectionSizes.push_back(GotSize); 5970b57cec5SDimitry Andric RWDataAlign = std::max<uint32_t>(RWDataAlign, getGOTEntrySize()); 5980b57cec5SDimitry Andric } 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric // Compute the size of all common symbols 6010b57cec5SDimitry Andric uint64_t CommonSize = 0; 6020b57cec5SDimitry Andric uint32_t CommonAlign = 1; 6030b57cec5SDimitry Andric for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E; 6040b57cec5SDimitry Andric ++I) { 6055ffd83dbSDimitry Andric Expected<uint32_t> FlagsOrErr = I->getFlags(); 6065ffd83dbSDimitry Andric if (!FlagsOrErr) 6075ffd83dbSDimitry Andric // TODO: Test this error. 6085ffd83dbSDimitry Andric return FlagsOrErr.takeError(); 6095ffd83dbSDimitry Andric if (*FlagsOrErr & SymbolRef::SF_Common) { 6100b57cec5SDimitry Andric // Add the common symbols to a list. We'll allocate them all below. 6110b57cec5SDimitry Andric uint64_t Size = I->getCommonSize(); 6120b57cec5SDimitry Andric uint32_t Align = I->getAlignment(); 6130b57cec5SDimitry Andric // If this is the first common symbol, use its alignment as the alignment 6140b57cec5SDimitry Andric // for the common symbols section. 6150b57cec5SDimitry Andric if (CommonSize == 0) 6160b57cec5SDimitry Andric CommonAlign = Align; 6170b57cec5SDimitry Andric CommonSize = alignTo(CommonSize, Align) + Size; 6180b57cec5SDimitry Andric } 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric if (CommonSize != 0) { 6210b57cec5SDimitry Andric RWSectionSizes.push_back(CommonSize); 6220b57cec5SDimitry Andric RWDataAlign = std::max(RWDataAlign, CommonAlign); 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric // Compute the required allocation space for each different type of sections 6260b57cec5SDimitry Andric // (code, read-only data, read-write data) assuming that all sections are 6270b57cec5SDimitry Andric // allocated with the max alignment. Note that we cannot compute with the 6280b57cec5SDimitry Andric // individual alignments of the sections, because then the required size 6290b57cec5SDimitry Andric // depends on the order, in which the sections are allocated. 6300b57cec5SDimitry Andric CodeSize = computeAllocationSizeForSections(CodeSectionSizes, CodeAlign); 6310b57cec5SDimitry Andric RODataSize = computeAllocationSizeForSections(ROSectionSizes, RODataAlign); 6320b57cec5SDimitry Andric RWDataSize = computeAllocationSizeForSections(RWSectionSizes, RWDataAlign); 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric return Error::success(); 6350b57cec5SDimitry Andric } 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric // compute GOT size 6380b57cec5SDimitry Andric unsigned RuntimeDyldImpl::computeGOTSize(const ObjectFile &Obj) { 6390b57cec5SDimitry Andric size_t GotEntrySize = getGOTEntrySize(); 6400b57cec5SDimitry Andric if (!GotEntrySize) 6410b57cec5SDimitry Andric return 0; 6420b57cec5SDimitry Andric 6430b57cec5SDimitry Andric size_t GotSize = 0; 6440b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 6450b57cec5SDimitry Andric SI != SE; ++SI) { 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric for (const RelocationRef &Reloc : SI->relocations()) 6480b57cec5SDimitry Andric if (relocationNeedsGot(Reloc)) 6490b57cec5SDimitry Andric GotSize += GotEntrySize; 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric return GotSize; 6530b57cec5SDimitry Andric } 6540b57cec5SDimitry Andric 6550b57cec5SDimitry Andric // compute stub buffer size for the given section 6560b57cec5SDimitry Andric unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj, 6570b57cec5SDimitry Andric const SectionRef &Section) { 658*fe6060f1SDimitry Andric if (!MemMgr.allowStubAllocation()) { 659*fe6060f1SDimitry Andric return 0; 660*fe6060f1SDimitry Andric } 661*fe6060f1SDimitry Andric 6620b57cec5SDimitry Andric unsigned StubSize = getMaxStubSize(); 6630b57cec5SDimitry Andric if (StubSize == 0) { 6640b57cec5SDimitry Andric return 0; 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric // FIXME: this is an inefficient way to handle this. We should computed the 6670b57cec5SDimitry Andric // necessary section allocation size in loadObject by walking all the sections 6680b57cec5SDimitry Andric // once. 6690b57cec5SDimitry Andric unsigned StubBufSize = 0; 6700b57cec5SDimitry Andric for (section_iterator SI = Obj.section_begin(), SE = Obj.section_end(); 6710b57cec5SDimitry Andric SI != SE; ++SI) { 6728bcb0991SDimitry Andric 6738bcb0991SDimitry Andric Expected<section_iterator> RelSecOrErr = SI->getRelocatedSection(); 6748bcb0991SDimitry Andric if (!RelSecOrErr) 6758bcb0991SDimitry Andric report_fatal_error(toString(RelSecOrErr.takeError())); 6768bcb0991SDimitry Andric 6778bcb0991SDimitry Andric section_iterator RelSecI = *RelSecOrErr; 6780b57cec5SDimitry Andric if (!(RelSecI == Section)) 6790b57cec5SDimitry Andric continue; 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric for (const RelocationRef &Reloc : SI->relocations()) 6820b57cec5SDimitry Andric if (relocationNeedsStub(Reloc)) 6830b57cec5SDimitry Andric StubBufSize += StubSize; 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric // Get section data size and alignment 6870b57cec5SDimitry Andric uint64_t DataSize = Section.getSize(); 6880b57cec5SDimitry Andric uint64_t Alignment64 = Section.getAlignment(); 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric // Add stubbuf size alignment 6910b57cec5SDimitry Andric unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 6920b57cec5SDimitry Andric unsigned StubAlignment = getStubAlignment(); 6930b57cec5SDimitry Andric unsigned EndAlignment = (DataSize | Alignment) & -(DataSize | Alignment); 6940b57cec5SDimitry Andric if (StubAlignment > EndAlignment) 6950b57cec5SDimitry Andric StubBufSize += StubAlignment - EndAlignment; 6960b57cec5SDimitry Andric return StubBufSize; 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src, 7000b57cec5SDimitry Andric unsigned Size) const { 7010b57cec5SDimitry Andric uint64_t Result = 0; 7020b57cec5SDimitry Andric if (IsTargetLittleEndian) { 7030b57cec5SDimitry Andric Src += Size - 1; 7040b57cec5SDimitry Andric while (Size--) 7050b57cec5SDimitry Andric Result = (Result << 8) | *Src--; 7060b57cec5SDimitry Andric } else 7070b57cec5SDimitry Andric while (Size--) 7080b57cec5SDimitry Andric Result = (Result << 8) | *Src++; 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric return Result; 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst, 7140b57cec5SDimitry Andric unsigned Size) const { 7150b57cec5SDimitry Andric if (IsTargetLittleEndian) { 7160b57cec5SDimitry Andric while (Size--) { 7170b57cec5SDimitry Andric *Dst++ = Value & 0xFF; 7180b57cec5SDimitry Andric Value >>= 8; 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric } else { 7210b57cec5SDimitry Andric Dst += Size - 1; 7220b57cec5SDimitry Andric while (Size--) { 7230b57cec5SDimitry Andric *Dst-- = Value & 0xFF; 7240b57cec5SDimitry Andric Value >>= 8; 7250b57cec5SDimitry Andric } 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric Expected<JITSymbolFlags> 7300b57cec5SDimitry Andric RuntimeDyldImpl::getJITSymbolFlags(const SymbolRef &SR) { 7310b57cec5SDimitry Andric return JITSymbolFlags::fromObjectSymbol(SR); 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj, 7350b57cec5SDimitry Andric CommonSymbolList &SymbolsToAllocate, 7360b57cec5SDimitry Andric uint64_t CommonSize, 7370b57cec5SDimitry Andric uint32_t CommonAlign) { 7380b57cec5SDimitry Andric if (SymbolsToAllocate.empty()) 7390b57cec5SDimitry Andric return Error::success(); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric // Allocate memory for the section 7420b57cec5SDimitry Andric unsigned SectionID = Sections.size(); 7430b57cec5SDimitry Andric uint8_t *Addr = MemMgr.allocateDataSection(CommonSize, CommonAlign, SectionID, 7440b57cec5SDimitry Andric "<common symbols>", false); 7450b57cec5SDimitry Andric if (!Addr) 7460b57cec5SDimitry Andric report_fatal_error("Unable to allocate memory for common symbols!"); 7470b57cec5SDimitry Andric uint64_t Offset = 0; 7480b57cec5SDimitry Andric Sections.push_back( 7490b57cec5SDimitry Andric SectionEntry("<common symbols>", Addr, CommonSize, CommonSize, 0)); 7500b57cec5SDimitry Andric memset(Addr, 0, CommonSize); 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "emitCommonSection SectionID: " << SectionID 7530b57cec5SDimitry Andric << " new addr: " << format("%p", Addr) 7540b57cec5SDimitry Andric << " DataSize: " << CommonSize << "\n"); 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric // Assign the address of each symbol 7570b57cec5SDimitry Andric for (auto &Sym : SymbolsToAllocate) { 7588bcb0991SDimitry Andric uint32_t Alignment = Sym.getAlignment(); 7590b57cec5SDimitry Andric uint64_t Size = Sym.getCommonSize(); 7600b57cec5SDimitry Andric StringRef Name; 7610b57cec5SDimitry Andric if (auto NameOrErr = Sym.getName()) 7620b57cec5SDimitry Andric Name = *NameOrErr; 7630b57cec5SDimitry Andric else 7640b57cec5SDimitry Andric return NameOrErr.takeError(); 7658bcb0991SDimitry Andric if (Alignment) { 7660b57cec5SDimitry Andric // This symbol has an alignment requirement. 7678bcb0991SDimitry Andric uint64_t AlignOffset = 7688bcb0991SDimitry Andric offsetToAlignment((uint64_t)Addr, Align(Alignment)); 7690b57cec5SDimitry Andric Addr += AlignOffset; 7700b57cec5SDimitry Andric Offset += AlignOffset; 7710b57cec5SDimitry Andric } 7720b57cec5SDimitry Andric auto JITSymFlags = getJITSymbolFlags(Sym); 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric if (!JITSymFlags) 7750b57cec5SDimitry Andric return JITSymFlags.takeError(); 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Allocating common symbol " << Name << " address " 7780b57cec5SDimitry Andric << format("%p", Addr) << "\n"); 779eaeb601bSDimitry Andric if (!Name.empty()) // Skip absolute symbol relocations. 7800b57cec5SDimitry Andric GlobalSymbolTable[Name] = 7810b57cec5SDimitry Andric SymbolTableEntry(SectionID, Offset, std::move(*JITSymFlags)); 7820b57cec5SDimitry Andric Offset += Size; 7830b57cec5SDimitry Andric Addr += Size; 7840b57cec5SDimitry Andric } 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric return Error::success(); 7870b57cec5SDimitry Andric } 7880b57cec5SDimitry Andric 7890b57cec5SDimitry Andric Expected<unsigned> 7900b57cec5SDimitry Andric RuntimeDyldImpl::emitSection(const ObjectFile &Obj, 7910b57cec5SDimitry Andric const SectionRef &Section, 7920b57cec5SDimitry Andric bool IsCode) { 7930b57cec5SDimitry Andric StringRef data; 7940b57cec5SDimitry Andric uint64_t Alignment64 = Section.getAlignment(); 7950b57cec5SDimitry Andric 7960b57cec5SDimitry Andric unsigned Alignment = (unsigned)Alignment64 & 0xffffffffL; 7970b57cec5SDimitry Andric unsigned PaddingSize = 0; 7980b57cec5SDimitry Andric unsigned StubBufSize = 0; 7990b57cec5SDimitry Andric bool IsRequired = isRequiredForExecution(Section); 8000b57cec5SDimitry Andric bool IsVirtual = Section.isVirtual(); 8010b57cec5SDimitry Andric bool IsZeroInit = isZeroInit(Section); 8020b57cec5SDimitry Andric bool IsReadOnly = isReadOnlyData(Section); 8030b57cec5SDimitry Andric uint64_t DataSize = Section.getSize(); 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric // An alignment of 0 (at least with ELF) is identical to an alignment of 1, 8060b57cec5SDimitry Andric // while being more "polite". Other formats do not support 0-aligned sections 8070b57cec5SDimitry Andric // anyway, so we should guarantee that the alignment is always at least 1. 8080b57cec5SDimitry Andric Alignment = std::max(1u, Alignment); 8090b57cec5SDimitry Andric 8108bcb0991SDimitry Andric Expected<StringRef> NameOrErr = Section.getName(); 8118bcb0991SDimitry Andric if (!NameOrErr) 8128bcb0991SDimitry Andric return NameOrErr.takeError(); 8138bcb0991SDimitry Andric StringRef Name = *NameOrErr; 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric StubBufSize = computeSectionStubBufSize(Obj, Section); 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric // The .eh_frame section (at least on Linux) needs an extra four bytes padded 8180b57cec5SDimitry Andric // with zeroes added at the end. For MachO objects, this section has a 8190b57cec5SDimitry Andric // slightly different name, so this won't have any effect for MachO objects. 8200b57cec5SDimitry Andric if (Name == ".eh_frame") 8210b57cec5SDimitry Andric PaddingSize = 4; 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric uintptr_t Allocate; 8240b57cec5SDimitry Andric unsigned SectionID = Sections.size(); 8250b57cec5SDimitry Andric uint8_t *Addr; 8260b57cec5SDimitry Andric const char *pData = nullptr; 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric // If this section contains any bits (i.e. isn't a virtual or bss section), 8290b57cec5SDimitry Andric // grab a reference to them. 8300b57cec5SDimitry Andric if (!IsVirtual && !IsZeroInit) { 8310b57cec5SDimitry Andric // In either case, set the location of the unrelocated section in memory, 8320b57cec5SDimitry Andric // since we still process relocations for it even if we're not applying them. 8330b57cec5SDimitry Andric if (Expected<StringRef> E = Section.getContents()) 8340b57cec5SDimitry Andric data = *E; 8350b57cec5SDimitry Andric else 8360b57cec5SDimitry Andric return E.takeError(); 8370b57cec5SDimitry Andric pData = data.data(); 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric // If there are any stubs then the section alignment needs to be at least as 8410b57cec5SDimitry Andric // high as stub alignment or padding calculations may by incorrect when the 8420b57cec5SDimitry Andric // section is remapped. 8430b57cec5SDimitry Andric if (StubBufSize != 0) { 8440b57cec5SDimitry Andric Alignment = std::max(Alignment, getStubAlignment()); 8450b57cec5SDimitry Andric PaddingSize += getStubAlignment() - 1; 8460b57cec5SDimitry Andric } 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric // Some sections, such as debug info, don't need to be loaded for execution. 8490b57cec5SDimitry Andric // Process those only if explicitly requested. 8500b57cec5SDimitry Andric if (IsRequired || ProcessAllSections) { 8510b57cec5SDimitry Andric Allocate = DataSize + PaddingSize + StubBufSize; 8520b57cec5SDimitry Andric if (!Allocate) 8530b57cec5SDimitry Andric Allocate = 1; 8540b57cec5SDimitry Andric Addr = IsCode ? MemMgr.allocateCodeSection(Allocate, Alignment, SectionID, 8550b57cec5SDimitry Andric Name) 8560b57cec5SDimitry Andric : MemMgr.allocateDataSection(Allocate, Alignment, SectionID, 8570b57cec5SDimitry Andric Name, IsReadOnly); 8580b57cec5SDimitry Andric if (!Addr) 8590b57cec5SDimitry Andric report_fatal_error("Unable to allocate section memory!"); 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric // Zero-initialize or copy the data from the image 8620b57cec5SDimitry Andric if (IsZeroInit || IsVirtual) 8630b57cec5SDimitry Andric memset(Addr, 0, DataSize); 8640b57cec5SDimitry Andric else 8650b57cec5SDimitry Andric memcpy(Addr, pData, DataSize); 8660b57cec5SDimitry Andric 8670b57cec5SDimitry Andric // Fill in any extra bytes we allocated for padding 8680b57cec5SDimitry Andric if (PaddingSize != 0) { 8690b57cec5SDimitry Andric memset(Addr + DataSize, 0, PaddingSize); 8700b57cec5SDimitry Andric // Update the DataSize variable to include padding. 8710b57cec5SDimitry Andric DataSize += PaddingSize; 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric // Align DataSize to stub alignment if we have any stubs (PaddingSize will 8740b57cec5SDimitry Andric // have been increased above to account for this). 8750b57cec5SDimitry Andric if (StubBufSize > 0) 8760b57cec5SDimitry Andric DataSize &= -(uint64_t)getStubAlignment(); 8770b57cec5SDimitry Andric } 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "emitSection SectionID: " << SectionID << " Name: " 8800b57cec5SDimitry Andric << Name << " obj addr: " << format("%p", pData) 8810b57cec5SDimitry Andric << " new addr: " << format("%p", Addr) << " DataSize: " 8820b57cec5SDimitry Andric << DataSize << " StubBufSize: " << StubBufSize 8830b57cec5SDimitry Andric << " Allocate: " << Allocate << "\n"); 8840b57cec5SDimitry Andric } else { 8850b57cec5SDimitry Andric // Even if we didn't load the section, we need to record an entry for it 8860b57cec5SDimitry Andric // to handle later processing (and by 'handle' I mean don't do anything 8870b57cec5SDimitry Andric // with these sections). 8880b57cec5SDimitry Andric Allocate = 0; 8890b57cec5SDimitry Andric Addr = nullptr; 8900b57cec5SDimitry Andric LLVM_DEBUG( 8910b57cec5SDimitry Andric dbgs() << "emitSection SectionID: " << SectionID << " Name: " << Name 8920b57cec5SDimitry Andric << " obj addr: " << format("%p", data.data()) << " new addr: 0" 8930b57cec5SDimitry Andric << " DataSize: " << DataSize << " StubBufSize: " << StubBufSize 8940b57cec5SDimitry Andric << " Allocate: " << Allocate << "\n"); 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric 8970b57cec5SDimitry Andric Sections.push_back( 8980b57cec5SDimitry Andric SectionEntry(Name, Addr, DataSize, Allocate, (uintptr_t)pData)); 8990b57cec5SDimitry Andric 9000b57cec5SDimitry Andric // Debug info sections are linked as if their load address was zero 9010b57cec5SDimitry Andric if (!IsRequired) 9020b57cec5SDimitry Andric Sections.back().setLoadAddress(0); 9030b57cec5SDimitry Andric 9040b57cec5SDimitry Andric return SectionID; 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric 9070b57cec5SDimitry Andric Expected<unsigned> 9080b57cec5SDimitry Andric RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj, 9090b57cec5SDimitry Andric const SectionRef &Section, 9100b57cec5SDimitry Andric bool IsCode, 9110b57cec5SDimitry Andric ObjSectionToIDMap &LocalSections) { 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric unsigned SectionID = 0; 9140b57cec5SDimitry Andric ObjSectionToIDMap::iterator i = LocalSections.find(Section); 9150b57cec5SDimitry Andric if (i != LocalSections.end()) 9160b57cec5SDimitry Andric SectionID = i->second; 9170b57cec5SDimitry Andric else { 9180b57cec5SDimitry Andric if (auto SectionIDOrErr = emitSection(Obj, Section, IsCode)) 9190b57cec5SDimitry Andric SectionID = *SectionIDOrErr; 9200b57cec5SDimitry Andric else 9210b57cec5SDimitry Andric return SectionIDOrErr.takeError(); 9220b57cec5SDimitry Andric LocalSections[Section] = SectionID; 9230b57cec5SDimitry Andric } 9240b57cec5SDimitry Andric return SectionID; 9250b57cec5SDimitry Andric } 9260b57cec5SDimitry Andric 9270b57cec5SDimitry Andric void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE, 9280b57cec5SDimitry Andric unsigned SectionID) { 9290b57cec5SDimitry Andric Relocations[SectionID].push_back(RE); 9300b57cec5SDimitry Andric } 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE, 9330b57cec5SDimitry Andric StringRef SymbolName) { 9340b57cec5SDimitry Andric // Relocation by symbol. If the symbol is found in the global symbol table, 9350b57cec5SDimitry Andric // create an appropriate section relocation. Otherwise, add it to 9360b57cec5SDimitry Andric // ExternalSymbolRelocations. 9370b57cec5SDimitry Andric RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(SymbolName); 9380b57cec5SDimitry Andric if (Loc == GlobalSymbolTable.end()) { 9390b57cec5SDimitry Andric ExternalSymbolRelocations[SymbolName].push_back(RE); 9400b57cec5SDimitry Andric } else { 941eaeb601bSDimitry Andric assert(!SymbolName.empty() && 942eaeb601bSDimitry Andric "Empty symbol should not be in GlobalSymbolTable"); 9430b57cec5SDimitry Andric // Copy the RE since we want to modify its addend. 9440b57cec5SDimitry Andric RelocationEntry RECopy = RE; 9450b57cec5SDimitry Andric const auto &SymInfo = Loc->second; 9460b57cec5SDimitry Andric RECopy.Addend += SymInfo.getOffset(); 9470b57cec5SDimitry Andric Relocations[SymInfo.getSectionID()].push_back(RECopy); 9480b57cec5SDimitry Andric } 9490b57cec5SDimitry Andric } 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr, 9520b57cec5SDimitry Andric unsigned AbiVariant) { 9538bcb0991SDimitry Andric if (Arch == Triple::aarch64 || Arch == Triple::aarch64_be || 9548bcb0991SDimitry Andric Arch == Triple::aarch64_32) { 9550b57cec5SDimitry Andric // This stub has to be able to access the full address space, 9560b57cec5SDimitry Andric // since symbol lookup won't necessarily find a handy, in-range, 9570b57cec5SDimitry Andric // PLT stub for functions which could be anywhere. 9580b57cec5SDimitry Andric // Stub can use ip0 (== x16) to calculate address 9590b57cec5SDimitry Andric writeBytesUnaligned(0xd2e00010, Addr, 4); // movz ip0, #:abs_g3:<addr> 9600b57cec5SDimitry Andric writeBytesUnaligned(0xf2c00010, Addr+4, 4); // movk ip0, #:abs_g2_nc:<addr> 9610b57cec5SDimitry Andric writeBytesUnaligned(0xf2a00010, Addr+8, 4); // movk ip0, #:abs_g1_nc:<addr> 9620b57cec5SDimitry Andric writeBytesUnaligned(0xf2800010, Addr+12, 4); // movk ip0, #:abs_g0_nc:<addr> 9630b57cec5SDimitry Andric writeBytesUnaligned(0xd61f0200, Addr+16, 4); // br ip0 9640b57cec5SDimitry Andric 9650b57cec5SDimitry Andric return Addr; 9660b57cec5SDimitry Andric } else if (Arch == Triple::arm || Arch == Triple::armeb) { 9670b57cec5SDimitry Andric // TODO: There is only ARM far stub now. We should add the Thumb stub, 9680b57cec5SDimitry Andric // and stubs for branches Thumb - ARM and ARM - Thumb. 9690b57cec5SDimitry Andric writeBytesUnaligned(0xe51ff004, Addr, 4); // ldr pc, [pc, #-4] 9700b57cec5SDimitry Andric return Addr + 4; 9710b57cec5SDimitry Andric } else if (IsMipsO32ABI || IsMipsN32ABI) { 9720b57cec5SDimitry Andric // 0: 3c190000 lui t9,%hi(addr). 9730b57cec5SDimitry Andric // 4: 27390000 addiu t9,t9,%lo(addr). 9740b57cec5SDimitry Andric // 8: 03200008 jr t9. 9750b57cec5SDimitry Andric // c: 00000000 nop. 9760b57cec5SDimitry Andric const unsigned LuiT9Instr = 0x3c190000, AdduiT9Instr = 0x27390000; 9770b57cec5SDimitry Andric const unsigned NopInstr = 0x0; 9780b57cec5SDimitry Andric unsigned JrT9Instr = 0x03200008; 9790b57cec5SDimitry Andric if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_32R6 || 9800b57cec5SDimitry Andric (AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6) 9810b57cec5SDimitry Andric JrT9Instr = 0x03200009; 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric writeBytesUnaligned(LuiT9Instr, Addr, 4); 9840b57cec5SDimitry Andric writeBytesUnaligned(AdduiT9Instr, Addr + 4, 4); 9850b57cec5SDimitry Andric writeBytesUnaligned(JrT9Instr, Addr + 8, 4); 9860b57cec5SDimitry Andric writeBytesUnaligned(NopInstr, Addr + 12, 4); 9870b57cec5SDimitry Andric return Addr; 9880b57cec5SDimitry Andric } else if (IsMipsN64ABI) { 9890b57cec5SDimitry Andric // 0: 3c190000 lui t9,%highest(addr). 9900b57cec5SDimitry Andric // 4: 67390000 daddiu t9,t9,%higher(addr). 9910b57cec5SDimitry Andric // 8: 0019CC38 dsll t9,t9,16. 9920b57cec5SDimitry Andric // c: 67390000 daddiu t9,t9,%hi(addr). 9930b57cec5SDimitry Andric // 10: 0019CC38 dsll t9,t9,16. 9940b57cec5SDimitry Andric // 14: 67390000 daddiu t9,t9,%lo(addr). 9950b57cec5SDimitry Andric // 18: 03200008 jr t9. 9960b57cec5SDimitry Andric // 1c: 00000000 nop. 9970b57cec5SDimitry Andric const unsigned LuiT9Instr = 0x3c190000, DaddiuT9Instr = 0x67390000, 9980b57cec5SDimitry Andric DsllT9Instr = 0x19CC38; 9990b57cec5SDimitry Andric const unsigned NopInstr = 0x0; 10000b57cec5SDimitry Andric unsigned JrT9Instr = 0x03200008; 10010b57cec5SDimitry Andric if ((AbiVariant & ELF::EF_MIPS_ARCH) == ELF::EF_MIPS_ARCH_64R6) 10020b57cec5SDimitry Andric JrT9Instr = 0x03200009; 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric writeBytesUnaligned(LuiT9Instr, Addr, 4); 10050b57cec5SDimitry Andric writeBytesUnaligned(DaddiuT9Instr, Addr + 4, 4); 10060b57cec5SDimitry Andric writeBytesUnaligned(DsllT9Instr, Addr + 8, 4); 10070b57cec5SDimitry Andric writeBytesUnaligned(DaddiuT9Instr, Addr + 12, 4); 10080b57cec5SDimitry Andric writeBytesUnaligned(DsllT9Instr, Addr + 16, 4); 10090b57cec5SDimitry Andric writeBytesUnaligned(DaddiuT9Instr, Addr + 20, 4); 10100b57cec5SDimitry Andric writeBytesUnaligned(JrT9Instr, Addr + 24, 4); 10110b57cec5SDimitry Andric writeBytesUnaligned(NopInstr, Addr + 28, 4); 10120b57cec5SDimitry Andric return Addr; 10130b57cec5SDimitry Andric } else if (Arch == Triple::ppc64 || Arch == Triple::ppc64le) { 10140b57cec5SDimitry Andric // Depending on which version of the ELF ABI is in use, we need to 10150b57cec5SDimitry Andric // generate one of two variants of the stub. They both start with 10160b57cec5SDimitry Andric // the same sequence to load the target address into r12. 10170b57cec5SDimitry Andric writeInt32BE(Addr, 0x3D800000); // lis r12, highest(addr) 10180b57cec5SDimitry Andric writeInt32BE(Addr+4, 0x618C0000); // ori r12, higher(addr) 10190b57cec5SDimitry Andric writeInt32BE(Addr+8, 0x798C07C6); // sldi r12, r12, 32 10200b57cec5SDimitry Andric writeInt32BE(Addr+12, 0x658C0000); // oris r12, r12, h(addr) 10210b57cec5SDimitry Andric writeInt32BE(Addr+16, 0x618C0000); // ori r12, r12, l(addr) 10220b57cec5SDimitry Andric if (AbiVariant == 2) { 10230b57cec5SDimitry Andric // PowerPC64 stub ELFv2 ABI: The address points to the function itself. 10240b57cec5SDimitry Andric // The address is already in r12 as required by the ABI. Branch to it. 10250b57cec5SDimitry Andric writeInt32BE(Addr+20, 0xF8410018); // std r2, 24(r1) 10260b57cec5SDimitry Andric writeInt32BE(Addr+24, 0x7D8903A6); // mtctr r12 10270b57cec5SDimitry Andric writeInt32BE(Addr+28, 0x4E800420); // bctr 10280b57cec5SDimitry Andric } else { 10290b57cec5SDimitry Andric // PowerPC64 stub ELFv1 ABI: The address points to a function descriptor. 10300b57cec5SDimitry Andric // Load the function address on r11 and sets it to control register. Also 10310b57cec5SDimitry Andric // loads the function TOC in r2 and environment pointer to r11. 10320b57cec5SDimitry Andric writeInt32BE(Addr+20, 0xF8410028); // std r2, 40(r1) 10330b57cec5SDimitry Andric writeInt32BE(Addr+24, 0xE96C0000); // ld r11, 0(r12) 10340b57cec5SDimitry Andric writeInt32BE(Addr+28, 0xE84C0008); // ld r2, 0(r12) 10350b57cec5SDimitry Andric writeInt32BE(Addr+32, 0x7D6903A6); // mtctr r11 10360b57cec5SDimitry Andric writeInt32BE(Addr+36, 0xE96C0010); // ld r11, 16(r2) 10370b57cec5SDimitry Andric writeInt32BE(Addr+40, 0x4E800420); // bctr 10380b57cec5SDimitry Andric } 10390b57cec5SDimitry Andric return Addr; 10400b57cec5SDimitry Andric } else if (Arch == Triple::systemz) { 10410b57cec5SDimitry Andric writeInt16BE(Addr, 0xC418); // lgrl %r1,.+8 10420b57cec5SDimitry Andric writeInt16BE(Addr+2, 0x0000); 10430b57cec5SDimitry Andric writeInt16BE(Addr+4, 0x0004); 10440b57cec5SDimitry Andric writeInt16BE(Addr+6, 0x07F1); // brc 15,%r1 10450b57cec5SDimitry Andric // 8-byte address stored at Addr + 8 10460b57cec5SDimitry Andric return Addr; 10470b57cec5SDimitry Andric } else if (Arch == Triple::x86_64) { 10480b57cec5SDimitry Andric *Addr = 0xFF; // jmp 10490b57cec5SDimitry Andric *(Addr+1) = 0x25; // rip 10500b57cec5SDimitry Andric // 32-bit PC-relative address of the GOT entry will be stored at Addr+2 10510b57cec5SDimitry Andric } else if (Arch == Triple::x86) { 10520b57cec5SDimitry Andric *Addr = 0xE9; // 32-bit pc-relative jump. 10530b57cec5SDimitry Andric } 10540b57cec5SDimitry Andric return Addr; 10550b57cec5SDimitry Andric } 10560b57cec5SDimitry Andric 10570b57cec5SDimitry Andric // Assign an address to a symbol name and resolve all the relocations 10580b57cec5SDimitry Andric // associated with it. 10590b57cec5SDimitry Andric void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID, 10600b57cec5SDimitry Andric uint64_t Addr) { 10610b57cec5SDimitry Andric // The address to use for relocation resolution is not 10620b57cec5SDimitry Andric // the address of the local section buffer. We must be doing 10630b57cec5SDimitry Andric // a remote execution environment of some sort. Relocations can't 10640b57cec5SDimitry Andric // be applied until all the sections have been moved. The client must 10650b57cec5SDimitry Andric // trigger this with a call to MCJIT::finalize() or 10660b57cec5SDimitry Andric // RuntimeDyld::resolveRelocations(). 10670b57cec5SDimitry Andric // 10680b57cec5SDimitry Andric // Addr is a uint64_t because we can't assume the pointer width 10690b57cec5SDimitry Andric // of the target is the same as that of the host. Just use a generic 10700b57cec5SDimitry Andric // "big enough" type. 10710b57cec5SDimitry Andric LLVM_DEBUG( 10720b57cec5SDimitry Andric dbgs() << "Reassigning address for section " << SectionID << " (" 10730b57cec5SDimitry Andric << Sections[SectionID].getName() << "): " 10740b57cec5SDimitry Andric << format("0x%016" PRIx64, Sections[SectionID].getLoadAddress()) 10750b57cec5SDimitry Andric << " -> " << format("0x%016" PRIx64, Addr) << "\n"); 10760b57cec5SDimitry Andric Sections[SectionID].setLoadAddress(Addr); 10770b57cec5SDimitry Andric } 10780b57cec5SDimitry Andric 10790b57cec5SDimitry Andric void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs, 10800b57cec5SDimitry Andric uint64_t Value) { 10810b57cec5SDimitry Andric for (unsigned i = 0, e = Relocs.size(); i != e; ++i) { 10820b57cec5SDimitry Andric const RelocationEntry &RE = Relocs[i]; 10830b57cec5SDimitry Andric // Ignore relocations for sections that were not loaded 1084*fe6060f1SDimitry Andric if (RE.SectionID != AbsoluteSymbolSection && 1085*fe6060f1SDimitry Andric Sections[RE.SectionID].getAddress() == nullptr) 10860b57cec5SDimitry Andric continue; 10870b57cec5SDimitry Andric resolveRelocation(RE, Value); 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric } 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric void RuntimeDyldImpl::applyExternalSymbolRelocations( 10920b57cec5SDimitry Andric const StringMap<JITEvaluatedSymbol> ExternalSymbolMap) { 1093*fe6060f1SDimitry Andric for (auto &RelocKV : ExternalSymbolRelocations) { 1094*fe6060f1SDimitry Andric StringRef Name = RelocKV.first(); 1095*fe6060f1SDimitry Andric RelocationList &Relocs = RelocKV.second; 10960b57cec5SDimitry Andric if (Name.size() == 0) { 10970b57cec5SDimitry Andric // This is an absolute symbol, use an address of zero. 10980b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Resolving absolute relocations." 10990b57cec5SDimitry Andric << "\n"); 11000b57cec5SDimitry Andric resolveRelocationList(Relocs, 0); 11010b57cec5SDimitry Andric } else { 11020b57cec5SDimitry Andric uint64_t Addr = 0; 11030b57cec5SDimitry Andric JITSymbolFlags Flags; 11040b57cec5SDimitry Andric RTDyldSymbolTable::const_iterator Loc = GlobalSymbolTable.find(Name); 11050b57cec5SDimitry Andric if (Loc == GlobalSymbolTable.end()) { 11060b57cec5SDimitry Andric auto RRI = ExternalSymbolMap.find(Name); 11070b57cec5SDimitry Andric assert(RRI != ExternalSymbolMap.end() && "No result for symbol"); 11080b57cec5SDimitry Andric Addr = RRI->second.getAddress(); 11090b57cec5SDimitry Andric Flags = RRI->second.getFlags(); 11100b57cec5SDimitry Andric } else { 11110b57cec5SDimitry Andric // We found the symbol in our global table. It was probably in a 11120b57cec5SDimitry Andric // Module that we loaded previously. 11130b57cec5SDimitry Andric const auto &SymInfo = Loc->second; 11140b57cec5SDimitry Andric Addr = getSectionLoadAddress(SymInfo.getSectionID()) + 11150b57cec5SDimitry Andric SymInfo.getOffset(); 11160b57cec5SDimitry Andric Flags = SymInfo.getFlags(); 11170b57cec5SDimitry Andric } 11180b57cec5SDimitry Andric 11190b57cec5SDimitry Andric // FIXME: Implement error handling that doesn't kill the host program! 1120*fe6060f1SDimitry Andric if (!Addr && !Resolver.allowsZeroSymbols()) 11210b57cec5SDimitry Andric report_fatal_error("Program used external function '" + Name + 11220b57cec5SDimitry Andric "' which could not be resolved!"); 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric // If Resolver returned UINT64_MAX, the client wants to handle this symbol 11250b57cec5SDimitry Andric // manually and we shouldn't resolve its relocations. 11260b57cec5SDimitry Andric if (Addr != UINT64_MAX) { 11270b57cec5SDimitry Andric 11280b57cec5SDimitry Andric // Tweak the address based on the symbol flags if necessary. 11290b57cec5SDimitry Andric // For example, this is used by RuntimeDyldMachOARM to toggle the low bit 11300b57cec5SDimitry Andric // if the target symbol is Thumb. 11310b57cec5SDimitry Andric Addr = modifyAddressBasedOnFlags(Addr, Flags); 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Resolving relocations Name: " << Name << "\t" 11340b57cec5SDimitry Andric << format("0x%lx", Addr) << "\n"); 11350b57cec5SDimitry Andric resolveRelocationList(Relocs, Addr); 11360b57cec5SDimitry Andric } 11370b57cec5SDimitry Andric } 11380b57cec5SDimitry Andric } 1139*fe6060f1SDimitry Andric ExternalSymbolRelocations.clear(); 11400b57cec5SDimitry Andric } 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric Error RuntimeDyldImpl::resolveExternalSymbols() { 11430b57cec5SDimitry Andric StringMap<JITEvaluatedSymbol> ExternalSymbolMap; 11440b57cec5SDimitry Andric 11450b57cec5SDimitry Andric // Resolution can trigger emission of more symbols, so iterate until 11460b57cec5SDimitry Andric // we've resolved *everything*. 11470b57cec5SDimitry Andric { 11480b57cec5SDimitry Andric JITSymbolResolver::LookupSet ResolvedSymbols; 11490b57cec5SDimitry Andric 11500b57cec5SDimitry Andric while (true) { 11510b57cec5SDimitry Andric JITSymbolResolver::LookupSet NewSymbols; 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric for (auto &RelocKV : ExternalSymbolRelocations) { 11540b57cec5SDimitry Andric StringRef Name = RelocKV.first(); 11550b57cec5SDimitry Andric if (!Name.empty() && !GlobalSymbolTable.count(Name) && 11560b57cec5SDimitry Andric !ResolvedSymbols.count(Name)) 11570b57cec5SDimitry Andric NewSymbols.insert(Name); 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric if (NewSymbols.empty()) 11610b57cec5SDimitry Andric break; 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric #ifdef _MSC_VER 11640b57cec5SDimitry Andric using ExpectedLookupResult = 11650b57cec5SDimitry Andric MSVCPExpected<JITSymbolResolver::LookupResult>; 11660b57cec5SDimitry Andric #else 11670b57cec5SDimitry Andric using ExpectedLookupResult = Expected<JITSymbolResolver::LookupResult>; 11680b57cec5SDimitry Andric #endif 11690b57cec5SDimitry Andric 11700b57cec5SDimitry Andric auto NewSymbolsP = std::make_shared<std::promise<ExpectedLookupResult>>(); 11710b57cec5SDimitry Andric auto NewSymbolsF = NewSymbolsP->get_future(); 11720b57cec5SDimitry Andric Resolver.lookup(NewSymbols, 11730b57cec5SDimitry Andric [=](Expected<JITSymbolResolver::LookupResult> Result) { 11740b57cec5SDimitry Andric NewSymbolsP->set_value(std::move(Result)); 11750b57cec5SDimitry Andric }); 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric auto NewResolverResults = NewSymbolsF.get(); 11780b57cec5SDimitry Andric 11790b57cec5SDimitry Andric if (!NewResolverResults) 11800b57cec5SDimitry Andric return NewResolverResults.takeError(); 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric assert(NewResolverResults->size() == NewSymbols.size() && 11830b57cec5SDimitry Andric "Should have errored on unresolved symbols"); 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric for (auto &RRKV : *NewResolverResults) { 11860b57cec5SDimitry Andric assert(!ResolvedSymbols.count(RRKV.first) && "Redundant resolution?"); 11870b57cec5SDimitry Andric ExternalSymbolMap.insert(RRKV); 11880b57cec5SDimitry Andric ResolvedSymbols.insert(RRKV.first); 11890b57cec5SDimitry Andric } 11900b57cec5SDimitry Andric } 11910b57cec5SDimitry Andric } 11920b57cec5SDimitry Andric 11930b57cec5SDimitry Andric applyExternalSymbolRelocations(ExternalSymbolMap); 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric return Error::success(); 11960b57cec5SDimitry Andric } 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric void RuntimeDyldImpl::finalizeAsync( 11998bcb0991SDimitry Andric std::unique_ptr<RuntimeDyldImpl> This, 1200e8d8bef9SDimitry Andric unique_function<void(object::OwningBinary<object::ObjectFile>, 1201e8d8bef9SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)> 12025ffd83dbSDimitry Andric OnEmitted, 1203e8d8bef9SDimitry Andric object::OwningBinary<object::ObjectFile> O, 1204e8d8bef9SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info) { 12050b57cec5SDimitry Andric 12060b57cec5SDimitry Andric auto SharedThis = std::shared_ptr<RuntimeDyldImpl>(std::move(This)); 12070b57cec5SDimitry Andric auto PostResolveContinuation = 1208e8d8bef9SDimitry Andric [SharedThis, OnEmitted = std::move(OnEmitted), O = std::move(O), 1209e8d8bef9SDimitry Andric Info = std::move(Info)]( 12108bcb0991SDimitry Andric Expected<JITSymbolResolver::LookupResult> Result) mutable { 12110b57cec5SDimitry Andric if (!Result) { 1212e8d8bef9SDimitry Andric OnEmitted(std::move(O), std::move(Info), 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)) 1226e8d8bef9SDimitry Andric OnEmitted(std::move(O), std::move(Info), 12275ffd83dbSDimitry Andric make_error<StringError>(std::move(ErrMsg), 12280b57cec5SDimitry Andric inconvertibleErrorCode())); 12290b57cec5SDimitry Andric else 1230e8d8bef9SDimitry Andric OnEmitted(std::move(O), std::move(Info), 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(); 1237eaeb601bSDimitry Andric if (Name.empty()) // Skip absolute symbol relocations. 1238eaeb601bSDimitry Andric continue; 12390b57cec5SDimitry Andric assert(!SharedThis->GlobalSymbolTable.count(Name) && 12400b57cec5SDimitry Andric "Name already processed. RuntimeDyld instances can not be re-used " 12410b57cec5SDimitry Andric "when finalizing with finalizeAsync."); 12420b57cec5SDimitry Andric Symbols.insert(Name); 12430b57cec5SDimitry Andric } 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric if (!Symbols.empty()) { 12468bcb0991SDimitry Andric SharedThis->Resolver.lookup(Symbols, std::move(PostResolveContinuation)); 12470b57cec5SDimitry Andric } else 12480b57cec5SDimitry Andric PostResolveContinuation(std::map<StringRef, JITEvaluatedSymbol>()); 12490b57cec5SDimitry Andric } 12500b57cec5SDimitry Andric 12510b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12520b57cec5SDimitry Andric // RuntimeDyld class implementation 12530b57cec5SDimitry Andric 12540b57cec5SDimitry Andric uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress( 12550b57cec5SDimitry Andric const object::SectionRef &Sec) const { 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric auto I = ObjSecToIDMap.find(Sec); 12580b57cec5SDimitry Andric if (I != ObjSecToIDMap.end()) 12590b57cec5SDimitry Andric return RTDyld.Sections[I->second].getLoadAddress(); 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric return 0; 12620b57cec5SDimitry Andric } 12630b57cec5SDimitry Andric 12640b57cec5SDimitry Andric void RuntimeDyld::MemoryManager::anchor() {} 12650b57cec5SDimitry Andric void JITSymbolResolver::anchor() {} 12660b57cec5SDimitry Andric void LegacyJITSymbolResolver::anchor() {} 12670b57cec5SDimitry Andric 12680b57cec5SDimitry Andric RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr, 12690b57cec5SDimitry Andric JITSymbolResolver &Resolver) 12700b57cec5SDimitry Andric : MemMgr(MemMgr), Resolver(Resolver) { 12710b57cec5SDimitry Andric // FIXME: There's a potential issue lurking here if a single instance of 12720b57cec5SDimitry Andric // RuntimeDyld is used to load multiple objects. The current implementation 12730b57cec5SDimitry Andric // associates a single memory manager with a RuntimeDyld instance. Even 12740b57cec5SDimitry Andric // though the public class spawns a new 'impl' instance for each load, 12750b57cec5SDimitry Andric // they share a single memory manager. This can become a problem when page 12760b57cec5SDimitry Andric // permissions are applied. 12770b57cec5SDimitry Andric Dyld = nullptr; 12780b57cec5SDimitry Andric ProcessAllSections = false; 12790b57cec5SDimitry Andric } 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric RuntimeDyld::~RuntimeDyld() {} 12820b57cec5SDimitry Andric 12830b57cec5SDimitry Andric static std::unique_ptr<RuntimeDyldCOFF> 12840b57cec5SDimitry Andric createRuntimeDyldCOFF( 12850b57cec5SDimitry Andric Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 12860b57cec5SDimitry Andric JITSymbolResolver &Resolver, bool ProcessAllSections, 12870b57cec5SDimitry Andric RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { 12880b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldCOFF> Dyld = 12890b57cec5SDimitry Andric RuntimeDyldCOFF::create(Arch, MM, Resolver); 12900b57cec5SDimitry Andric Dyld->setProcessAllSections(ProcessAllSections); 12910b57cec5SDimitry Andric Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted)); 12920b57cec5SDimitry Andric return Dyld; 12930b57cec5SDimitry Andric } 12940b57cec5SDimitry Andric 12950b57cec5SDimitry Andric static std::unique_ptr<RuntimeDyldELF> 12960b57cec5SDimitry Andric createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 12970b57cec5SDimitry Andric JITSymbolResolver &Resolver, bool ProcessAllSections, 12980b57cec5SDimitry Andric RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { 12990b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldELF> Dyld = 13000b57cec5SDimitry Andric RuntimeDyldELF::create(Arch, MM, Resolver); 13010b57cec5SDimitry Andric Dyld->setProcessAllSections(ProcessAllSections); 13020b57cec5SDimitry Andric Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted)); 13030b57cec5SDimitry Andric return Dyld; 13040b57cec5SDimitry Andric } 13050b57cec5SDimitry Andric 13060b57cec5SDimitry Andric static std::unique_ptr<RuntimeDyldMachO> 13070b57cec5SDimitry Andric createRuntimeDyldMachO( 13080b57cec5SDimitry Andric Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM, 13090b57cec5SDimitry Andric JITSymbolResolver &Resolver, 13100b57cec5SDimitry Andric bool ProcessAllSections, 13110b57cec5SDimitry Andric RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { 13120b57cec5SDimitry Andric std::unique_ptr<RuntimeDyldMachO> Dyld = 13130b57cec5SDimitry Andric RuntimeDyldMachO::create(Arch, MM, Resolver); 13140b57cec5SDimitry Andric Dyld->setProcessAllSections(ProcessAllSections); 13150b57cec5SDimitry Andric Dyld->setNotifyStubEmitted(std::move(NotifyStubEmitted)); 13160b57cec5SDimitry Andric return Dyld; 13170b57cec5SDimitry Andric } 13180b57cec5SDimitry Andric 13190b57cec5SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo> 13200b57cec5SDimitry Andric RuntimeDyld::loadObject(const ObjectFile &Obj) { 13210b57cec5SDimitry Andric if (!Dyld) { 13220b57cec5SDimitry Andric if (Obj.isELF()) 13230b57cec5SDimitry Andric Dyld = 13240b57cec5SDimitry Andric createRuntimeDyldELF(static_cast<Triple::ArchType>(Obj.getArch()), 13250b57cec5SDimitry Andric MemMgr, Resolver, ProcessAllSections, 13260b57cec5SDimitry Andric std::move(NotifyStubEmitted)); 13270b57cec5SDimitry Andric else if (Obj.isMachO()) 13280b57cec5SDimitry Andric Dyld = createRuntimeDyldMachO( 13290b57cec5SDimitry Andric static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, 13300b57cec5SDimitry Andric ProcessAllSections, std::move(NotifyStubEmitted)); 13310b57cec5SDimitry Andric else if (Obj.isCOFF()) 13320b57cec5SDimitry Andric Dyld = createRuntimeDyldCOFF( 13330b57cec5SDimitry Andric static_cast<Triple::ArchType>(Obj.getArch()), MemMgr, Resolver, 13340b57cec5SDimitry Andric ProcessAllSections, std::move(NotifyStubEmitted)); 13350b57cec5SDimitry Andric else 13360b57cec5SDimitry Andric report_fatal_error("Incompatible object format!"); 13370b57cec5SDimitry Andric } 13380b57cec5SDimitry Andric 13390b57cec5SDimitry Andric if (!Dyld->isCompatibleFile(Obj)) 13400b57cec5SDimitry Andric report_fatal_error("Incompatible object format!"); 13410b57cec5SDimitry Andric 13420b57cec5SDimitry Andric auto LoadedObjInfo = Dyld->loadObject(Obj); 13430b57cec5SDimitry Andric MemMgr.notifyObjectLoaded(*this, Obj); 13440b57cec5SDimitry Andric return LoadedObjInfo; 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric 13470b57cec5SDimitry Andric void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const { 13480b57cec5SDimitry Andric if (!Dyld) 13490b57cec5SDimitry Andric return nullptr; 13500b57cec5SDimitry Andric return Dyld->getSymbolLocalAddress(Name); 13510b57cec5SDimitry Andric } 13520b57cec5SDimitry Andric 13530b57cec5SDimitry Andric unsigned RuntimeDyld::getSymbolSectionID(StringRef Name) const { 13540b57cec5SDimitry Andric assert(Dyld && "No RuntimeDyld instance attached"); 13550b57cec5SDimitry Andric return Dyld->getSymbolSectionID(Name); 13560b57cec5SDimitry Andric } 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const { 13590b57cec5SDimitry Andric if (!Dyld) 13600b57cec5SDimitry Andric return nullptr; 13610b57cec5SDimitry Andric return Dyld->getSymbol(Name); 13620b57cec5SDimitry Andric } 13630b57cec5SDimitry Andric 13640b57cec5SDimitry Andric std::map<StringRef, JITEvaluatedSymbol> RuntimeDyld::getSymbolTable() const { 13650b57cec5SDimitry Andric if (!Dyld) 13660b57cec5SDimitry Andric return std::map<StringRef, JITEvaluatedSymbol>(); 13670b57cec5SDimitry Andric return Dyld->getSymbolTable(); 13680b57cec5SDimitry Andric } 13690b57cec5SDimitry Andric 13700b57cec5SDimitry Andric void RuntimeDyld::resolveRelocations() { Dyld->resolveRelocations(); } 13710b57cec5SDimitry Andric 13720b57cec5SDimitry Andric void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) { 13730b57cec5SDimitry Andric Dyld->reassignSectionAddress(SectionID, Addr); 13740b57cec5SDimitry Andric } 13750b57cec5SDimitry Andric 13760b57cec5SDimitry Andric void RuntimeDyld::mapSectionAddress(const void *LocalAddress, 13770b57cec5SDimitry Andric uint64_t TargetAddress) { 13780b57cec5SDimitry Andric Dyld->mapSectionAddress(LocalAddress, TargetAddress); 13790b57cec5SDimitry Andric } 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric bool RuntimeDyld::hasError() { return Dyld->hasError(); } 13820b57cec5SDimitry Andric 13830b57cec5SDimitry Andric StringRef RuntimeDyld::getErrorString() { return Dyld->getErrorString(); } 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric void RuntimeDyld::finalizeWithMemoryManagerLocking() { 13860b57cec5SDimitry Andric bool MemoryFinalizationLocked = MemMgr.FinalizationLocked; 13870b57cec5SDimitry Andric MemMgr.FinalizationLocked = true; 13880b57cec5SDimitry Andric resolveRelocations(); 13890b57cec5SDimitry Andric registerEHFrames(); 13900b57cec5SDimitry Andric if (!MemoryFinalizationLocked) { 13910b57cec5SDimitry Andric MemMgr.finalizeMemory(); 13920b57cec5SDimitry Andric MemMgr.FinalizationLocked = false; 13930b57cec5SDimitry Andric } 13940b57cec5SDimitry Andric } 13950b57cec5SDimitry Andric 13960b57cec5SDimitry Andric StringRef RuntimeDyld::getSectionContent(unsigned SectionID) const { 13970b57cec5SDimitry Andric assert(Dyld && "No Dyld instance attached"); 13980b57cec5SDimitry Andric return Dyld->getSectionContent(SectionID); 13990b57cec5SDimitry Andric } 14000b57cec5SDimitry Andric 14010b57cec5SDimitry Andric uint64_t RuntimeDyld::getSectionLoadAddress(unsigned SectionID) const { 14020b57cec5SDimitry Andric assert(Dyld && "No Dyld instance attached"); 14030b57cec5SDimitry Andric return Dyld->getSectionLoadAddress(SectionID); 14040b57cec5SDimitry Andric } 14050b57cec5SDimitry Andric 14060b57cec5SDimitry Andric void RuntimeDyld::registerEHFrames() { 14070b57cec5SDimitry Andric if (Dyld) 14080b57cec5SDimitry Andric Dyld->registerEHFrames(); 14090b57cec5SDimitry Andric } 14100b57cec5SDimitry Andric 14110b57cec5SDimitry Andric void RuntimeDyld::deregisterEHFrames() { 14120b57cec5SDimitry Andric if (Dyld) 14130b57cec5SDimitry Andric Dyld->deregisterEHFrames(); 14140b57cec5SDimitry Andric } 14150b57cec5SDimitry Andric // FIXME: Kill this with fire once we have a new JIT linker: this is only here 14160b57cec5SDimitry Andric // so that we can re-use RuntimeDyld's implementation without twisting the 14170b57cec5SDimitry Andric // interface any further for ORC's purposes. 14185ffd83dbSDimitry Andric void jitLinkForORC( 14195ffd83dbSDimitry Andric object::OwningBinary<object::ObjectFile> O, 14205ffd83dbSDimitry Andric RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver, 14215ffd83dbSDimitry Andric bool ProcessAllSections, 1422e8d8bef9SDimitry Andric unique_function<Error(const object::ObjectFile &Obj, 1423e8d8bef9SDimitry Andric RuntimeDyld::LoadedObjectInfo &LoadedObj, 14240b57cec5SDimitry Andric std::map<StringRef, JITEvaluatedSymbol>)> 14250b57cec5SDimitry Andric OnLoaded, 1426e8d8bef9SDimitry Andric unique_function<void(object::OwningBinary<object::ObjectFile>, 1427e8d8bef9SDimitry Andric std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)> 14285ffd83dbSDimitry Andric OnEmitted) { 14290b57cec5SDimitry Andric 14300b57cec5SDimitry Andric RuntimeDyld RTDyld(MemMgr, Resolver); 14310b57cec5SDimitry Andric RTDyld.setProcessAllSections(ProcessAllSections); 14320b57cec5SDimitry Andric 14335ffd83dbSDimitry Andric auto Info = RTDyld.loadObject(*O.getBinary()); 14340b57cec5SDimitry Andric 14350b57cec5SDimitry Andric if (RTDyld.hasError()) { 1436e8d8bef9SDimitry Andric OnEmitted(std::move(O), std::move(Info), 1437e8d8bef9SDimitry Andric make_error<StringError>(RTDyld.getErrorString(), 14380b57cec5SDimitry Andric inconvertibleErrorCode())); 14390b57cec5SDimitry Andric return; 14400b57cec5SDimitry Andric } 14410b57cec5SDimitry Andric 1442e8d8bef9SDimitry Andric if (auto Err = OnLoaded(*O.getBinary(), *Info, RTDyld.getSymbolTable())) 1443e8d8bef9SDimitry Andric OnEmitted(std::move(O), std::move(Info), std::move(Err)); 14440b57cec5SDimitry Andric 14450b57cec5SDimitry Andric RuntimeDyldImpl::finalizeAsync(std::move(RTDyld.Dyld), std::move(OnEmitted), 1446e8d8bef9SDimitry Andric std::move(O), std::move(Info)); 14470b57cec5SDimitry Andric } 14480b57cec5SDimitry Andric 14490b57cec5SDimitry Andric } // end namespace llvm 1450