diff options
Diffstat (limited to 'llvm/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp')
| -rw-r--r-- | llvm/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/llvm/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp b/llvm/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp index 258868aa64f..ef34c4a2b45 100644 --- a/llvm/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp +++ b/llvm/lib/ExecutionEngine/Orc/OrcTargetSupport.cpp @@ -1,7 +1,9 @@ #include "llvm/ADT/Triple.h" #include "llvm/ExecutionEngine/Orc/OrcTargetSupport.h" +#include "llvm/Support/Process.h" #include <array> + using namespace llvm::orc; namespace { @@ -134,5 +136,81 @@ OrcX86_64::insertCompileCallbackTrampolines(Module &M, return GetLabelName; } +OrcX86_64::IndirectStubsInfo::~IndirectStubsInfo() { + sys::Memory::releaseMappedMemory(StubsBlock); + sys::Memory::releaseMappedMemory(PtrsBlock); +} + +std::error_code OrcX86_64::emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo, + unsigned MinStubs, + void *InitialPtrVal) { + // Stub format is: + // + // .section __orc_stubs + // stub1: + // jmpq *ptr1(%rip) + // .byte 0xC4 ; <- Invalid opcode padding. + // .byte 0xF1 + // stub2: + // jmpq *ptr2(%rip) + // + // ... + // + // .section __orc_ptrs + // ptr1: + // .quad 0x0 + // ptr2: + // .quad 0x0 + // + // ... + + const unsigned StubSize = IndirectStubsInfo::StubSize; + + // Emit at least MinStubs, rounded up to fill the pages allocated. + unsigned PageSize = sys::Process::getPageSize(); + unsigned NumPages = ((MinStubs * StubSize) + (PageSize - 1)) / PageSize; + unsigned NumStubs = (NumPages * PageSize) / StubSize; + + // Allocate memory for stubs and pointers in one call. + std::error_code EC; + auto InitialBlock = sys::Memory::allocateMappedMemory(2 * NumPages * PageSize, + nullptr, + sys::Memory::MF_READ | + sys::Memory::MF_WRITE, + EC); + + if (EC) + return EC; + + // Create separate MemoryBlocks representing the stubs and pointers. + sys::MemoryBlock StubsBlock(InitialBlock.base(), NumPages * PageSize); + sys::MemoryBlock PtrsBlock(static_cast<char*>(InitialBlock.base()) + + NumPages * PageSize, + NumPages * PageSize); + + // Populate the stubs page stubs and mark it executable. + uint64_t *Stub = reinterpret_cast<uint64_t*>(StubsBlock.base()); + uint64_t PtrOffsetField = + static_cast<uint64_t>(NumPages * PageSize - 6) << 16; + for (unsigned I = 0; I < NumStubs; ++I) + Stub[I] = 0xF1C40000000025ff | PtrOffsetField; + + if (auto EC = sys::Memory::protectMappedMemory(StubsBlock, + sys::Memory::MF_READ | + sys::Memory::MF_EXEC)) + return EC; + + // Initialize all pointers to point at FailureAddress. + void **Ptr = reinterpret_cast<void**>(PtrsBlock.base()); + for (unsigned I = 0; I < NumStubs; ++I) + Ptr[I] = InitialPtrVal; + + StubsInfo.NumStubs = NumStubs; + StubsInfo.StubsBlock = std::move(StubsBlock); + StubsInfo.PtrsBlock = std::move(PtrsBlock); + + return std::error_code(); +} + } // End namespace orc. } // End namespace llvm. |

