[UpdateCCTestChecks] Detect function mangled name on separate line
[llvm-core.git] / tools / lli / RemoteJITUtils.h
blob8e80e73c808251727b57c291a543dabe8e088892
1 //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utilities for remote-JITing with LLI.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
14 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
16 #include "llvm/ExecutionEngine/Orc/RawByteChannel.h"
17 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
18 #include <mutex>
20 #if !defined(_MSC_VER) && !defined(__MINGW32__)
21 #include <unistd.h>
22 #else
23 #include <io.h>
24 #endif
26 /// RPC channel that reads from and writes from file descriptors.
27 class FDRawChannel final : public llvm::orc::rpc::RawByteChannel {
28 public:
29 FDRawChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
31 llvm::Error readBytes(char *Dst, unsigned Size) override {
32 assert(Dst && "Attempt to read into null.");
33 ssize_t Completed = 0;
34 while (Completed < static_cast<ssize_t>(Size)) {
35 ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
36 if (Read <= 0) {
37 auto ErrNo = errno;
38 if (ErrNo == EAGAIN || ErrNo == EINTR)
39 continue;
40 else
41 return llvm::errorCodeToError(
42 std::error_code(errno, std::generic_category()));
44 Completed += Read;
46 return llvm::Error::success();
49 llvm::Error appendBytes(const char *Src, unsigned Size) override {
50 assert(Src && "Attempt to append from null.");
51 ssize_t Completed = 0;
52 while (Completed < static_cast<ssize_t>(Size)) {
53 ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
54 if (Written < 0) {
55 auto ErrNo = errno;
56 if (ErrNo == EAGAIN || ErrNo == EINTR)
57 continue;
58 else
59 return llvm::errorCodeToError(
60 std::error_code(errno, std::generic_category()));
62 Completed += Written;
64 return llvm::Error::success();
67 llvm::Error send() override { return llvm::Error::success(); }
69 private:
70 int InFD, OutFD;
73 // launch the remote process (see lli.cpp) and return a channel to it.
74 std::unique_ptr<FDRawChannel> launchRemote();
76 namespace llvm {
78 // ForwardingMM - Adapter to connect MCJIT to Orc's Remote
79 // memory manager.
80 class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
81 public:
82 void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
83 this->MemMgr = std::move(MemMgr);
86 void setResolver(std::shared_ptr<LegacyJITSymbolResolver> Resolver) {
87 this->Resolver = std::move(Resolver);
90 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
91 unsigned SectionID,
92 StringRef SectionName) override {
93 return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
96 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
97 unsigned SectionID, StringRef SectionName,
98 bool IsReadOnly) override {
99 return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
100 IsReadOnly);
103 void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
104 uintptr_t RODataSize, uint32_t RODataAlign,
105 uintptr_t RWDataSize,
106 uint32_t RWDataAlign) override {
107 MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
108 RWDataSize, RWDataAlign);
111 bool needsToReserveAllocationSpace() override {
112 return MemMgr->needsToReserveAllocationSpace();
115 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
116 size_t Size) override {
117 MemMgr->registerEHFrames(Addr, LoadAddr, Size);
120 void deregisterEHFrames() override {
121 MemMgr->deregisterEHFrames();
124 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
125 return MemMgr->finalizeMemory(ErrMsg);
128 void notifyObjectLoaded(RuntimeDyld &RTDyld,
129 const object::ObjectFile &Obj) override {
130 MemMgr->notifyObjectLoaded(RTDyld, Obj);
133 // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
134 using RTDyldMemoryManager::notifyObjectLoaded;
136 JITSymbol findSymbol(const std::string &Name) override {
137 return Resolver->findSymbol(Name);
140 JITSymbol
141 findSymbolInLogicalDylib(const std::string &Name) override {
142 return Resolver->findSymbolInLogicalDylib(Name);
145 private:
146 std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
147 std::shared_ptr<LegacyJITSymbolResolver> Resolver;
151 #endif