xref: /freebsd/contrib/llvm-project/llvm/tools/lli/ChildTarget/ChildTarget.cpp (revision ae7e8a02e6e93455e026036132c4d053b2c12ad9)
1 #include "llvm/ExecutionEngine/Orc/OrcABISupport.h"
2 #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h"
3 #include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
4 #include "llvm/Support/Debug.h"
5 #include "llvm/Support/DynamicLibrary.h"
6 #include "llvm/Support/Process.h"
7 #include <sstream>
8 
9 #include "../RemoteJITUtils.h"
10 
11 using namespace llvm;
12 using namespace llvm::orc;
13 using namespace llvm::sys;
14 
15 #ifdef __x86_64__
16 typedef OrcX86_64_SysV HostOrcArch;
17 #else
18 typedef OrcGenericABI HostOrcArch;
19 #endif
20 
21 ExitOnError ExitOnErr;
22 
23 int main(int argc, char *argv[]) {
24 
25   if (argc != 3) {
26     errs() << "Usage: " << argv[0] << " <input fd> <output fd>\n";
27     return 1;
28   }
29 
30   ExitOnErr.setBanner(std::string(argv[0]) + ":");
31 
32   int InFD;
33   int OutFD;
34   {
35     std::istringstream InFDStream(argv[1]), OutFDStream(argv[2]);
36     InFDStream >> InFD;
37     OutFDStream >> OutFD;
38   }
39 
40   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
41     errs() << "Error loading program symbols.\n";
42     return 1;
43   }
44 
45   auto SymbolLookup = [](const std::string &Name) {
46     return RTDyldMemoryManager::getSymbolAddressInProcess(Name);
47   };
48 
49   auto RegisterEHFrames = [](uint8_t *Addr, uint32_t Size) {
50     RTDyldMemoryManager::registerEHFramesInProcess(Addr, Size);
51   };
52 
53   auto DeregisterEHFrames = [](uint8_t *Addr, uint32_t Size) {
54     RTDyldMemoryManager::deregisterEHFramesInProcess(Addr, Size);
55   };
56 
57   shared::FDRawByteChannel Channel(InFD, OutFD);
58   typedef remote::OrcRemoteTargetServer<shared::FDRawByteChannel, HostOrcArch>
59       JITServer;
60   JITServer Server(Channel, SymbolLookup, RegisterEHFrames, DeregisterEHFrames);
61 
62   while (!Server.receivedTerminate())
63     ExitOnErr(Server.handleOne());
64 
65   close(InFD);
66   close(OutFD);
67 
68   return 0;
69 }
70