• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
2 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h"
3 #include "llvm/Support/Debug.h"
4 #include "llvm/Support/DynamicLibrary.h"
5 #include "llvm/Support/Process.h"
6 #include <sstream>
7 
8 #include "../RemoteJITUtils.h"
9 
10 using namespace llvm;
11 using namespace llvm::orc;
12 using namespace llvm::sys;
13 
14 #ifdef __x86_64__
15 typedef OrcX86_64_SysV HostOrcArch;
16 #else
17 typedef OrcGenericABI HostOrcArch;
18 #endif
19 
20 ExitOnError ExitOnErr;
21 
main(int argc,char * argv[])22 int main(int argc, char *argv[]) {
23 
24   if (argc != 3) {
25     errs() << "Usage: " << argv[0] << " <input fd> <output fd>\n";
26     return 1;
27   }
28 
29   ExitOnErr.setBanner(std::string(argv[0]) + ":");
30 
31   int InFD;
32   int OutFD;
33   {
34     std::istringstream InFDStream(argv[1]), OutFDStream(argv[2]);
35     InFDStream >> InFD;
36     OutFDStream >> OutFD;
37   }
38 
39   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
40     errs() << "Error loading program symbols.\n";
41     return 1;
42   }
43 
44   auto SymbolLookup = [](const std::string &Name) {
45     return RTDyldMemoryManager::getSymbolAddressInProcess(Name);
46   };
47 
48   auto RegisterEHFrames = [](uint8_t *Addr, uint32_t Size) {
49     RTDyldMemoryManager::registerEHFramesInProcess(Addr, Size);
50   };
51 
52   auto DeregisterEHFrames = [](uint8_t *Addr, uint32_t Size) {
53     RTDyldMemoryManager::deregisterEHFramesInProcess(Addr, Size);
54   };
55 
56   FDRPCChannel Channel(InFD, OutFD);
57   typedef remote::OrcRemoteTargetServer<FDRPCChannel, HostOrcArch> JITServer;
58   JITServer Server(Channel, SymbolLookup, RegisterEHFrames, DeregisterEHFrames);
59 
60   while (1) {
61     uint32_t RawId;
62     ExitOnErr(Server.startReceivingFunction(Channel, RawId));
63     auto Id = static_cast<JITServer::JITFuncId>(RawId);
64     switch (Id) {
65     case JITServer::TerminateSessionId:
66       ExitOnErr(Server.handleTerminateSession());
67       return 0;
68     default:
69       ExitOnErr(Server.handleKnownFunction(Id));
70       break;
71     }
72   }
73 
74   close(InFD);
75   close(OutFD);
76 
77   return 0;
78 }
79