2015-03-07 20:21:27 +00:00
|
|
|
//===-- RuntimeDyldCOFFX86_64.h --- COFF/X86_64 specific code ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// COFF x86_x64 support for MC-JIT runtime dynamic linker.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
|
|
|
|
#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDCOFF86_64_H
|
|
|
|
|
2017-06-06 11:49:48 +00:00
|
|
|
#include "../RuntimeDyldCOFF.h"
|
2017-06-07 03:48:56 +00:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
2015-03-07 20:21:27 +00:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
|
2015-03-07 21:47:46 +00:00
|
|
|
#define DEBUG_TYPE "dyld"
|
2015-03-07 20:21:27 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class RuntimeDyldCOFFX86_64 : public RuntimeDyldCOFF {
|
|
|
|
|
|
|
|
private:
|
|
|
|
// When a module is loaded we save the SectionID of the unwind
|
|
|
|
// sections in a table until we receive a request to register all
|
|
|
|
// unregisteredEH frame sections with the memory manager.
|
|
|
|
SmallVector<SID, 2> UnregisteredEHFrameSections;
|
|
|
|
SmallVector<SID, 2> RegisteredEHFrameSections;
|
2018-02-21 17:18:20 +00:00
|
|
|
uint64_t ImageBase;
|
|
|
|
|
|
|
|
// Fake an __ImageBase pointer by returning the section with the lowest adress
|
|
|
|
uint64_t getImageBase() {
|
|
|
|
if (!ImageBase) {
|
|
|
|
ImageBase = std::numeric_limits<uint64_t>::max();
|
|
|
|
for (const SectionEntry &Section : Sections)
|
|
|
|
ImageBase = std::min(ImageBase, Section.getLoadAddress());
|
|
|
|
}
|
|
|
|
return ImageBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write32BitOffset(uint8_t *Target, int64_t Addend, uint64_t Delta) {
|
|
|
|
uint64_t Result = Addend + Delta;
|
|
|
|
assert(Result <= UINT32_MAX && "Relocation overflow");
|
|
|
|
writeBytesUnaligned(Result, Target, 4);
|
|
|
|
}
|
2015-03-07 20:21:27 +00:00
|
|
|
|
|
|
|
public:
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 03:37:06 +00:00
|
|
|
RuntimeDyldCOFFX86_64(RuntimeDyld::MemoryManager &MM,
|
2016-08-01 20:49:11 +00:00
|
|
|
JITSymbolResolver &Resolver)
|
2018-02-21 17:18:20 +00:00
|
|
|
: RuntimeDyldCOFF(MM, Resolver), ImageBase(0) {}
|
2015-03-07 20:21:27 +00:00
|
|
|
|
2018-02-21 17:18:20 +00:00
|
|
|
unsigned getStubAlignment() override { return 1; }
|
|
|
|
|
|
|
|
// 2-byte jmp instruction + 32-bit relative address + 64-bit absolute jump
|
|
|
|
unsigned getMaxStubSize() override { return 14; }
|
2015-03-07 20:21:27 +00:00
|
|
|
|
2015-03-07 21:47:46 +00:00
|
|
|
// The target location for the relocation is described by RE.SectionID and
|
|
|
|
// RE.Offset. RE.SectionID can be used to find the SectionEntry. Each
|
|
|
|
// SectionEntry has three members describing its location.
|
|
|
|
// SectionEntry::Address is the address at which the section has been loaded
|
|
|
|
// into memory in the current (host) process. SectionEntry::LoadAddress is
|
|
|
|
// the address that the section will have in the target process.
|
|
|
|
// SectionEntry::ObjAddress is the address of the bits for this section in the
|
|
|
|
// original emitted object image (also in the current address space).
|
|
|
|
//
|
|
|
|
// Relocations will be applied as if the section were loaded at
|
|
|
|
// SectionEntry::LoadAddress, but they will be applied at an address based
|
|
|
|
// on SectionEntry::Address. SectionEntry::ObjAddress will be used to refer
|
|
|
|
// to Target memory contents if they are required for value calculations.
|
|
|
|
//
|
|
|
|
// The Value parameter here is the load address of the symbol for the
|
|
|
|
// relocation to be applied. For relocations which refer to symbols in the
|
|
|
|
// current object Value will be the LoadAddress of the section in which
|
|
|
|
// the symbol resides (RE.Addend provides additional information about the
|
|
|
|
// symbol location). For external symbols, Value will be the address of the
|
|
|
|
// symbol in the target address space.
|
|
|
|
void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
|
|
|
|
const SectionEntry &Section = Sections[RE.SectionID];
|
2015-11-23 21:47:41 +00:00
|
|
|
uint8_t *Target = Section.getAddressWithOffset(RE.Offset);
|
2015-03-07 21:47:46 +00:00
|
|
|
|
|
|
|
switch (RE.RelType) {
|
|
|
|
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_1:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_2:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_3:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_4:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_5: {
|
2015-11-23 21:47:41 +00:00
|
|
|
uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
|
2015-03-07 21:47:46 +00:00
|
|
|
// Delta is the distance from the start of the reloc to the end of the
|
|
|
|
// instruction with the reloc.
|
|
|
|
uint64_t Delta = 4 + (RE.RelType - COFF::IMAGE_REL_AMD64_REL32);
|
|
|
|
Value -= FinalAddress + Delta;
|
|
|
|
uint64_t Result = Value + RE.Addend;
|
|
|
|
assert(((int64_t)Result <= INT32_MAX) && "Relocation overflow");
|
|
|
|
assert(((int64_t)Result >= INT32_MIN) && "Relocation underflow");
|
2015-10-19 20:37:52 +00:00
|
|
|
writeBytesUnaligned(Result, Target, 4);
|
2015-03-07 21:47:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case COFF::IMAGE_REL_AMD64_ADDR32NB: {
|
2018-02-21 17:18:20 +00:00
|
|
|
// ADDR32NB requires an offset less than 2GB from 'ImageBase'.
|
|
|
|
// The MemoryManager can make sure this is always true by forcing the
|
|
|
|
// memory layout to be: CodeSection < ReadOnlySection < ReadWriteSection.
|
|
|
|
const uint64_t ImageBase = getImageBase();
|
|
|
|
if (Value < ImageBase || ((Value - ImageBase) > UINT32_MAX)) {
|
|
|
|
llvm::errs() << "IMAGE_REL_AMD64_ADDR32NB relocation requires an"
|
|
|
|
<< "ordered section layout.\n";
|
|
|
|
write32BitOffset(Target, 0, 0);
|
|
|
|
} else {
|
|
|
|
write32BitOffset(Target, RE.Addend, Value - ImageBase);
|
|
|
|
}
|
2015-03-07 21:47:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case COFF::IMAGE_REL_AMD64_ADDR64: {
|
2015-10-23 18:46:43 +00:00
|
|
|
writeBytesUnaligned(Value + RE.Addend, Target, 8);
|
2015-03-07 21:47:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Relocation type not implemented yet!");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-03-07 20:21:27 +00:00
|
|
|
|
2018-02-21 17:18:20 +00:00
|
|
|
std::tuple<uint64_t, uint64_t, uint64_t>
|
|
|
|
generateRelocationStub(unsigned SectionID, StringRef TargetName,
|
|
|
|
uint64_t Offset, uint64_t RelType, uint64_t Addend,
|
|
|
|
StubMap &Stubs) {
|
|
|
|
uintptr_t StubOffset;
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
|
|
|
|
|
|
|
RelocationValueRef OriginalRelValueRef;
|
|
|
|
OriginalRelValueRef.SectionID = SectionID;
|
|
|
|
OriginalRelValueRef.Offset = Offset;
|
|
|
|
OriginalRelValueRef.Addend = Addend;
|
|
|
|
OriginalRelValueRef.SymbolName = TargetName.data();
|
|
|
|
|
|
|
|
auto Stub = Stubs.find(OriginalRelValueRef);
|
|
|
|
if (Stub == Stubs.end()) {
|
|
|
|
DEBUG(dbgs() << " Create a new stub function for " << TargetName.data()
|
|
|
|
<< "\n");
|
|
|
|
|
|
|
|
StubOffset = Section.getStubOffset();
|
|
|
|
Stubs[OriginalRelValueRef] = StubOffset;
|
|
|
|
createStubFunction(Section.getAddressWithOffset(StubOffset));
|
|
|
|
Section.advanceStubOffset(getMaxStubSize());
|
|
|
|
} else {
|
|
|
|
DEBUG(dbgs() << " Stub function found for " << TargetName.data() << "\n");
|
|
|
|
StubOffset = Stub->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: If RelType == COFF::IMAGE_REL_AMD64_ADDR32NB we should be able
|
|
|
|
// to ignore the __ImageBase requirement and just forward to the stub
|
|
|
|
// directly as an offset of this section:
|
|
|
|
// write32BitOffset(Section.getAddressWithOffset(Offset), 0, StubOffset);
|
|
|
|
// .xdata exception handler's aren't having this though.
|
|
|
|
|
|
|
|
// Resolve original relocation to stub function.
|
|
|
|
const RelocationEntry RE(SectionID, Offset, RelType, Addend);
|
|
|
|
resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset));
|
|
|
|
|
|
|
|
// adjust relocation info so resolution writes to the stub function
|
|
|
|
Addend = 0;
|
|
|
|
Offset = StubOffset + 6;
|
|
|
|
RelType = COFF::IMAGE_REL_AMD64_ADDR64;
|
|
|
|
|
|
|
|
return std::make_tuple(Offset, RelType, Addend);
|
|
|
|
}
|
|
|
|
|
2016-04-27 20:24:48 +00:00
|
|
|
Expected<relocation_iterator>
|
|
|
|
processRelocationRef(unsigned SectionID,
|
|
|
|
relocation_iterator RelI,
|
|
|
|
const ObjectFile &Obj,
|
|
|
|
ObjSectionToIDMap &ObjSectionToID,
|
|
|
|
StubMap &Stubs) override {
|
2015-04-22 21:38:37 +00:00
|
|
|
// If possible, find the symbol referred to in the relocation,
|
|
|
|
// and the section that contains it.
|
2015-03-07 21:47:46 +00:00
|
|
|
symbol_iterator Symbol = RelI->getSymbol();
|
|
|
|
if (Symbol == Obj.symbol_end())
|
|
|
|
report_fatal_error("Unknown symbol in relocation");
|
2016-05-02 20:28:12 +00:00
|
|
|
auto SectionOrError = Symbol->getSection();
|
2016-05-18 05:31:24 +00:00
|
|
|
if (!SectionOrError)
|
|
|
|
return SectionOrError.takeError();
|
2016-05-02 20:28:12 +00:00
|
|
|
section_iterator SecI = *SectionOrError;
|
2015-04-22 21:38:37 +00:00
|
|
|
// If there is no section, this must be an external reference.
|
|
|
|
const bool IsExtern = SecI == Obj.section_end();
|
2015-03-07 21:47:46 +00:00
|
|
|
|
|
|
|
// Determine the Addend used to adjust the relocation value.
|
2015-06-30 01:53:01 +00:00
|
|
|
uint64_t RelType = RelI->getType();
|
2015-06-29 23:29:12 +00:00
|
|
|
uint64_t Offset = RelI->getOffset();
|
2015-03-07 21:47:46 +00:00
|
|
|
uint64_t Addend = 0;
|
|
|
|
SectionEntry &Section = Sections[SectionID];
|
2015-11-23 21:47:41 +00:00
|
|
|
uintptr_t ObjTarget = Section.getObjAddress() + Offset;
|
2015-03-07 21:47:46 +00:00
|
|
|
|
2018-02-21 17:18:20 +00:00
|
|
|
Expected<StringRef> TargetNameOrErr = Symbol->getName();
|
|
|
|
if (!TargetNameOrErr)
|
|
|
|
return TargetNameOrErr.takeError();
|
|
|
|
StringRef TargetName = *TargetNameOrErr;
|
|
|
|
|
2015-03-07 21:47:46 +00:00
|
|
|
switch (RelType) {
|
|
|
|
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_1:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_2:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_3:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_4:
|
|
|
|
case COFF::IMAGE_REL_AMD64_REL32_5:
|
|
|
|
case COFF::IMAGE_REL_AMD64_ADDR32NB: {
|
2015-10-19 20:37:52 +00:00
|
|
|
uint8_t *Displacement = (uint8_t *)ObjTarget;
|
|
|
|
Addend = readBytesUnaligned(Displacement, 4);
|
2018-02-21 17:18:20 +00:00
|
|
|
|
|
|
|
if (IsExtern)
|
|
|
|
std::tie(Offset, RelType, Addend) = generateRelocationStub(
|
|
|
|
SectionID, TargetName, Offset, RelType, Addend, Stubs);
|
|
|
|
|
2015-03-07 21:47:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case COFF::IMAGE_REL_AMD64_ADDR64: {
|
2015-10-19 20:37:52 +00:00
|
|
|
uint8_t *Displacement = (uint8_t *)ObjTarget;
|
|
|
|
Addend = readBytesUnaligned(Displacement, 8);
|
2015-03-07 21:47:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
|
|
|
|
<< " RelType: " << RelType << " TargetName: " << TargetName
|
|
|
|
<< " Addend " << Addend << "\n");
|
|
|
|
|
2015-04-22 21:38:37 +00:00
|
|
|
if (IsExtern) {
|
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, Addend);
|
|
|
|
addRelocationForSymbol(RE, TargetName);
|
|
|
|
} else {
|
|
|
|
bool IsCode = SecI->isText();
|
2016-04-27 20:24:48 +00:00
|
|
|
unsigned TargetSectionID;
|
|
|
|
if (auto TargetSectionIDOrErr =
|
|
|
|
findOrEmitSection(Obj, *SecI, IsCode, ObjSectionToID))
|
|
|
|
TargetSectionID = *TargetSectionIDOrErr;
|
|
|
|
else
|
|
|
|
return TargetSectionIDOrErr.takeError();
|
2015-04-22 21:38:37 +00:00
|
|
|
uint64_t TargetOffset = getSymbolOffset(*Symbol);
|
|
|
|
RelocationEntry RE(SectionID, Offset, RelType, TargetOffset + Addend);
|
|
|
|
addRelocationForSection(RE, TargetSectionID);
|
|
|
|
}
|
2015-03-07 21:47:46 +00:00
|
|
|
|
|
|
|
return ++RelI;
|
|
|
|
}
|
2015-03-07 20:21:27 +00:00
|
|
|
|
2015-03-07 21:47:46 +00:00
|
|
|
void registerEHFrames() override {
|
|
|
|
for (auto const &EHFrameSID : UnregisteredEHFrameSections) {
|
2015-11-23 21:47:41 +00:00
|
|
|
uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress();
|
|
|
|
uint64_t EHFrameLoadAddr = Sections[EHFrameSID].getLoadAddress();
|
|
|
|
size_t EHFrameSize = Sections[EHFrameSID].getSize();
|
[MCJIT][Orc] Refactor RTDyldMemoryManager, weave RuntimeDyld::SymbolInfo through
MCJIT.
This patch decouples the two responsibilities of the RTDyldMemoryManager class,
memory management and symbol resolution, into two new classes:
RuntimeDyld::MemoryManager and RuntimeDyld::SymbolResolver.
The symbol resolution interface is modified slightly, from:
uint64_t getSymbolAddress(const std::string &Name);
to:
RuntimeDyld::SymbolInfo findSymbol(const std::string &Name);
The latter passes symbol flags along with symbol addresses, allowing RuntimeDyld
and others to reason about non-strong/non-exported symbols.
The memory management interface removes the following method:
void notifyObjectLoaded(ExecutionEngine *EE,
const object::ObjectFile &) {}
as it is not related to memory management. (Note: Backwards compatibility *is*
maintained for this method in MCJIT and OrcMCJITReplacement, see below).
The RTDyldMemoryManager class remains in-tree for backwards compatibility.
It inherits directly from RuntimeDyld::SymbolResolver, and indirectly from
RuntimeDyld::MemoryManager via the new MCJITMemoryManager class, which
just subclasses RuntimeDyld::MemoryManager and reintroduces the
notifyObjectLoaded method for backwards compatibility).
The EngineBuilder class retains the existing method:
EngineBuilder&
setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager> mcjmm);
and includes two new methods:
EngineBuilder&
setMemoryManager(std::unique_ptr<MCJITMemoryManager> MM);
EngineBuilder&
setSymbolResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> SR);
Clients should use EITHER:
A single call to setMCJITMemoryManager with an RTDyldMemoryManager.
OR (exclusive)
One call each to each of setMemoryManager and setSymbolResolver.
This patch should be fully compatible with existing uses of RTDyldMemoryManager.
If it is not it should be considered a bug, and the patch either fixed or
reverted.
If clients find the new API to be an improvement the goal will be to deprecate
and eventually remove the RTDyldMemoryManager class in favor of the new classes.
llvm-svn: 233509
2015-03-30 03:37:06 +00:00
|
|
|
MemMgr.registerEHFrames(EHFrameAddr, EHFrameLoadAddr, EHFrameSize);
|
2015-03-07 21:47:46 +00:00
|
|
|
RegisteredEHFrameSections.push_back(EHFrameSID);
|
|
|
|
}
|
|
|
|
UnregisteredEHFrameSections.clear();
|
|
|
|
}
|
2018-02-21 17:18:20 +00:00
|
|
|
|
2016-04-27 20:24:48 +00:00
|
|
|
Error finalizeLoad(const ObjectFile &Obj,
|
|
|
|
ObjSectionToIDMap &SectionMap) override {
|
2015-03-07 21:47:46 +00:00
|
|
|
// Look for and record the EH frame section IDs.
|
|
|
|
for (const auto &SectionPair : SectionMap) {
|
|
|
|
const SectionRef &Section = SectionPair.first;
|
|
|
|
StringRef Name;
|
2016-04-27 20:24:48 +00:00
|
|
|
if (auto EC = Section.getName(Name))
|
|
|
|
return errorCodeToError(EC);
|
2018-02-21 17:18:20 +00:00
|
|
|
|
|
|
|
// Note unwind info is stored in .pdata but often points to .xdata
|
|
|
|
// with an IMAGE_REL_AMD64_ADDR32NB relocation. Using a memory manager
|
|
|
|
// that keeps sections ordered in relation to __ImageBase is necessary.
|
|
|
|
if (Name == ".pdata")
|
2015-03-07 21:47:46 +00:00
|
|
|
UnregisteredEHFrameSections.push_back(SectionPair.second);
|
|
|
|
}
|
2016-04-27 20:24:48 +00:00
|
|
|
return Error::success();
|
2015-03-07 21:47:46 +00:00
|
|
|
}
|
2015-03-07 20:21:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#undef DEBUG_TYPE
|
|
|
|
|
|
|
|
#endif
|