xref: /freebsd/contrib/llvm-project/llvm/tools/lli/ChildTarget/ChildTarget.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1*349cc55cSDimitry Andric //===----------- ChildTarget.cpp - Out-of-proc executor for lli -----------===//
2*349cc55cSDimitry Andric //
3*349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*349cc55cSDimitry Andric //
7*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8*349cc55cSDimitry Andric //
9*349cc55cSDimitry Andric // Simple out-of-process executor for lli.
10*349cc55cSDimitry Andric //
11*349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
14*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
15*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.h"
16*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.h"
17*349cc55cSDimitry Andric #include "llvm/Support/DynamicLibrary.h"
18*349cc55cSDimitry Andric #include "llvm/Support/Error.h"
19*349cc55cSDimitry Andric #include "llvm/Support/MathExtras.h"
20*349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h"
21*349cc55cSDimitry Andric #include <cstring>
22*349cc55cSDimitry Andric #include <sstream>
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric using namespace llvm;
250b57cec5SDimitry Andric using namespace llvm::orc;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric ExitOnError ExitOnErr;
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric int main(int argc, char *argv[]) {
30*349cc55cSDimitry Andric #if LLVM_ENABLE_THREADS
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric   if (argc != 3) {
330b57cec5SDimitry Andric     errs() << "Usage: " << argv[0] << " <input fd> <output fd>\n";
340b57cec5SDimitry Andric     return 1;
350b57cec5SDimitry Andric   }
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
380b57cec5SDimitry Andric     errs() << "Error loading program symbols.\n";
390b57cec5SDimitry Andric     return 1;
400b57cec5SDimitry Andric   }
410b57cec5SDimitry Andric 
42*349cc55cSDimitry Andric   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
430b57cec5SDimitry Andric 
44*349cc55cSDimitry Andric   int InFD = 0;
45*349cc55cSDimitry Andric   int OutFD = 0;
46*349cc55cSDimitry Andric   {
47*349cc55cSDimitry Andric     std::istringstream InFDStream(argv[1]), OutFDStream(argv[2]);
48*349cc55cSDimitry Andric     InFDStream >> InFD;
49*349cc55cSDimitry Andric     OutFDStream >> OutFD;
50*349cc55cSDimitry Andric   }
510b57cec5SDimitry Andric 
52*349cc55cSDimitry Andric   auto Server =
53*349cc55cSDimitry Andric       ExitOnErr(SimpleRemoteEPCServer::Create<FDSimpleRemoteEPCTransport>(
54*349cc55cSDimitry Andric           [](SimpleRemoteEPCServer::Setup &S) -> Error {
55*349cc55cSDimitry Andric             S.setDispatcher(
56*349cc55cSDimitry Andric                 std::make_unique<SimpleRemoteEPCServer::ThreadDispatcher>());
57*349cc55cSDimitry Andric             S.bootstrapSymbols() =
58*349cc55cSDimitry Andric                 SimpleRemoteEPCServer::defaultBootstrapSymbols();
59*349cc55cSDimitry Andric             S.services().push_back(
60*349cc55cSDimitry Andric                 std::make_unique<rt_bootstrap::SimpleExecutorMemoryManager>());
61*349cc55cSDimitry Andric             return Error::success();
62*349cc55cSDimitry Andric           },
63*349cc55cSDimitry Andric           InFD, OutFD));
640b57cec5SDimitry Andric 
65*349cc55cSDimitry Andric   ExitOnErr(Server->waitForDisconnect());
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric   return 0;
68*349cc55cSDimitry Andric 
69*349cc55cSDimitry Andric #else
70*349cc55cSDimitry Andric   errs() << argv[0]
71*349cc55cSDimitry Andric          << " error: this tool requires threads, but LLVM was "
72*349cc55cSDimitry Andric             "built with LLVM_ENABLE_THREADS=Off\n";
73*349cc55cSDimitry Andric   return 1;
74*349cc55cSDimitry Andric #endif
750b57cec5SDimitry Andric }
76