#include "llvm/ExecutionEngine/RuntimeDyld.h"
#include "RuntimeDyldCOFF.h"
#include "RuntimeDyldELF.h"
#include "RuntimeDyldImpl.h"
#include "RuntimeDyldMachO.h"
#include "llvm/Object/COFF.h"
#include "llvm/Object/ELFObjectFile.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/MSVCErrorWorkarounds.h"
#include "llvm/Support/MathExtras.h"
#include <mutex>
#include <future>
usingnamespacellvm;
usingnamespacellvm::object;
#define DEBUG_TYPE …
namespace {
enum RuntimeDyldErrorCode { … };
class RuntimeDyldErrorCategory : public std::error_category { … };
}
char RuntimeDyldError::ID = …;
void RuntimeDyldError::log(raw_ostream &OS) const { … }
std::error_code RuntimeDyldError::convertToErrorCode() const { … }
RuntimeDyldImpl::~RuntimeDyldImpl() = default;
void RuntimeDyld::LoadedObjectInfo::anchor() { … }
namespace llvm {
void RuntimeDyldImpl::registerEHFrames() { … }
void RuntimeDyldImpl::deregisterEHFrames() { … }
#ifndef NDEBUG
static void dumpSectionMemory(const SectionEntry &S, StringRef State) {
dbgs() << "----- Contents of section " << S.getName() << " " << State
<< " -----";
if (S.getAddress() == nullptr) {
dbgs() << "\n <section not emitted>\n";
return;
}
const unsigned ColsPerRow = 16;
uint8_t *DataAddr = S.getAddress();
uint64_t LoadAddr = S.getLoadAddress();
unsigned StartPadding = LoadAddr & (ColsPerRow - 1);
unsigned BytesRemaining = S.getSize();
if (StartPadding) {
dbgs() << "\n" << format("0x%016" PRIx64,
LoadAddr & ~(uint64_t)(ColsPerRow - 1)) << ":";
while (StartPadding--)
dbgs() << " ";
}
while (BytesRemaining > 0) {
if ((LoadAddr & (ColsPerRow - 1)) == 0)
dbgs() << "\n" << format("0x%016" PRIx64, LoadAddr) << ":";
dbgs() << " " << format("%02x", *DataAddr);
++DataAddr;
++LoadAddr;
--BytesRemaining;
}
dbgs() << "\n";
}
#endif
void RuntimeDyldImpl::resolveRelocations() { … }
void RuntimeDyldImpl::resolveLocalRelocations() { … }
void RuntimeDyldImpl::mapSectionAddress(const void *LocalAddress,
uint64_t TargetAddress) { … }
static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
uint64_t &Result) { … }
Expected<RuntimeDyldImpl::ObjSectionToIDMap>
RuntimeDyldImpl::loadObjectImpl(const object::ObjectFile &Obj) { … }
static uint64_t
computeAllocationSizeForSections(std::vector<uint64_t> &SectionSizes,
Align Alignment) { … }
static bool isRequiredForExecution(const SectionRef Section) { … }
static bool isReadOnlyData(const SectionRef Section) { … }
static bool isZeroInit(const SectionRef Section) { … }
static bool isTLS(const SectionRef Section) { … }
Error RuntimeDyldImpl::computeTotalAllocSize(
const ObjectFile &Obj, uint64_t &CodeSize, Align &CodeAlign,
uint64_t &RODataSize, Align &RODataAlign, uint64_t &RWDataSize,
Align &RWDataAlign) { … }
unsigned RuntimeDyldImpl::computeGOTSize(const ObjectFile &Obj) { … }
unsigned RuntimeDyldImpl::computeSectionStubBufSize(const ObjectFile &Obj,
const SectionRef &Section) { … }
uint64_t RuntimeDyldImpl::readBytesUnaligned(uint8_t *Src,
unsigned Size) const { … }
void RuntimeDyldImpl::writeBytesUnaligned(uint64_t Value, uint8_t *Dst,
unsigned Size) const { … }
Expected<JITSymbolFlags>
RuntimeDyldImpl::getJITSymbolFlags(const SymbolRef &SR) { … }
Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
CommonSymbolList &SymbolsToAllocate,
uint64_t CommonSize,
uint32_t CommonAlign) { … }
Expected<unsigned>
RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
const SectionRef &Section,
bool IsCode) { … }
Expected<unsigned>
RuntimeDyldImpl::findOrEmitSection(const ObjectFile &Obj,
const SectionRef &Section,
bool IsCode,
ObjSectionToIDMap &LocalSections) { … }
void RuntimeDyldImpl::addRelocationForSection(const RelocationEntry &RE,
unsigned SectionID) { … }
void RuntimeDyldImpl::addRelocationForSymbol(const RelocationEntry &RE,
StringRef SymbolName) { … }
uint8_t *RuntimeDyldImpl::createStubFunction(uint8_t *Addr,
unsigned AbiVariant) { … }
void RuntimeDyldImpl::reassignSectionAddress(unsigned SectionID,
uint64_t Addr) { … }
void RuntimeDyldImpl::resolveRelocationList(const RelocationList &Relocs,
uint64_t Value) { … }
void RuntimeDyldImpl::applyExternalSymbolRelocations(
const StringMap<JITEvaluatedSymbol> ExternalSymbolMap) { … }
Error RuntimeDyldImpl::resolveExternalSymbols() { … }
void RuntimeDyldImpl::finalizeAsync(
std::unique_ptr<RuntimeDyldImpl> This,
unique_function<void(object::OwningBinary<object::ObjectFile>,
std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)>
OnEmitted,
object::OwningBinary<object::ObjectFile> O,
std::unique_ptr<RuntimeDyld::LoadedObjectInfo> Info) { … }
uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
const object::SectionRef &Sec) const { … }
RuntimeDyld::MemoryManager::TLSSection
RuntimeDyld::MemoryManager::allocateTLSSection(uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
StringRef SectionName) { … }
void RuntimeDyld::MemoryManager::anchor() { … }
void JITSymbolResolver::anchor() { … }
void LegacyJITSymbolResolver::anchor() { … }
RuntimeDyld::RuntimeDyld(RuntimeDyld::MemoryManager &MemMgr,
JITSymbolResolver &Resolver)
: … { … }
RuntimeDyld::~RuntimeDyld() = default;
static std::unique_ptr<RuntimeDyldCOFF>
createRuntimeDyldCOFF(
Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
JITSymbolResolver &Resolver, bool ProcessAllSections,
RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { … }
static std::unique_ptr<RuntimeDyldELF>
createRuntimeDyldELF(Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
JITSymbolResolver &Resolver, bool ProcessAllSections,
RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { … }
static std::unique_ptr<RuntimeDyldMachO>
createRuntimeDyldMachO(
Triple::ArchType Arch, RuntimeDyld::MemoryManager &MM,
JITSymbolResolver &Resolver,
bool ProcessAllSections,
RuntimeDyld::NotifyStubEmittedFunction NotifyStubEmitted) { … }
std::unique_ptr<RuntimeDyld::LoadedObjectInfo>
RuntimeDyld::loadObject(const ObjectFile &Obj) { … }
void *RuntimeDyld::getSymbolLocalAddress(StringRef Name) const { … }
unsigned RuntimeDyld::getSymbolSectionID(StringRef Name) const { … }
JITEvaluatedSymbol RuntimeDyld::getSymbol(StringRef Name) const { … }
std::map<StringRef, JITEvaluatedSymbol> RuntimeDyld::getSymbolTable() const { … }
void RuntimeDyld::resolveRelocations() { … }
void RuntimeDyld::reassignSectionAddress(unsigned SectionID, uint64_t Addr) { … }
void RuntimeDyld::mapSectionAddress(const void *LocalAddress,
uint64_t TargetAddress) { … }
bool RuntimeDyld::hasError() { … }
StringRef RuntimeDyld::getErrorString() { … }
void RuntimeDyld::finalizeWithMemoryManagerLocking() { … }
StringRef RuntimeDyld::getSectionContent(unsigned SectionID) const { … }
uint64_t RuntimeDyld::getSectionLoadAddress(unsigned SectionID) const { … }
void RuntimeDyld::registerEHFrames() { … }
void RuntimeDyld::deregisterEHFrames() { … }
void jitLinkForORC(
object::OwningBinary<object::ObjectFile> O,
RuntimeDyld::MemoryManager &MemMgr, JITSymbolResolver &Resolver,
bool ProcessAllSections,
unique_function<Error(const object::ObjectFile &Obj,
RuntimeDyld::LoadedObjectInfo &LoadedObj,
std::map<StringRef, JITEvaluatedSymbol>)>
OnLoaded,
unique_function<void(object::OwningBinary<object::ObjectFile>,
std::unique_ptr<RuntimeDyld::LoadedObjectInfo>, Error)>
OnEmitted) { … }
}