xref: /freebsd/contrib/llvm-project/llvm/tools/lli/ChildTarget/ChildTarget.cpp (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 
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   FDRawChannel Channel(InFD, OutFD);
57   typedef remote::OrcRemoteTargetServer<FDRawChannel, HostOrcArch> JITServer;
58   JITServer Server(Channel, SymbolLookup, RegisterEHFrames, DeregisterEHFrames);
59 
60   while (!Server.receivedTerminate())
61     ExitOnErr(Server.handleOne());
62 
63   close(InFD);
64   close(OutFD);
65 
66   return 0;
67 }
68