1 //===----------- ChildTarget.cpp - Out-of-proc executor for lli -----------===//
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 // Simple out-of-process executor for lli.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ExecutionEngine/Orc/Shared/FDRawByteChannel.h"
15 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
16 #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
17 #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.h"
18 #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.h"
19 #include "llvm/Support/DynamicLibrary.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/MathExtras.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <cstring>
24 #include <sstream>
25 
26 using namespace llvm;
27 using namespace llvm::orc;
28 
29 ExitOnError ExitOnErr;
30 
31 int main(int argc, char *argv[]) {
32 #if LLVM_ENABLE_THREADS
33 
34   if (argc != 3) {
35     errs() << "Usage: " << argv[0] << " <input fd> <output fd>\n";
36     return 1;
37   }
38 
39   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
40     errs() << "Error loading program symbols.\n";
41     return 1;
42   }
43 
44   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
45 
46   int InFD = 0;
47   int OutFD = 0;
48   {
49     std::istringstream InFDStream(argv[1]), OutFDStream(argv[2]);
50     InFDStream >> InFD;
51     OutFDStream >> OutFD;
52   }
53 
54   auto Server =
55       ExitOnErr(SimpleRemoteEPCServer::Create<FDSimpleRemoteEPCTransport>(
56           [](SimpleRemoteEPCServer::Setup &S) -> Error {
57             S.setDispatcher(
58                 std::make_unique<SimpleRemoteEPCServer::ThreadDispatcher>());
59             S.bootstrapSymbols() =
60                 SimpleRemoteEPCServer::defaultBootstrapSymbols();
61             S.services().push_back(
62                 std::make_unique<rt_bootstrap::SimpleExecutorMemoryManager>());
63             return Error::success();
64           },
65           InFD, OutFD));
66 
67   ExitOnErr(Server->waitForDisconnect());
68 
69   close(InFD);
70   close(OutFD);
71 
72   return 0;
73 
74 #else
75   errs() << argv[0]
76          << " error: this tool requires threads, but LLVM was "
77             "built with LLVM_ENABLE_THREADS=Off\n";
78   return 1;
79 #endif
80 }
81