xref: /freebsd/contrib/llvm-project/llvm/tools/lli/ChildTarget/ChildTarget.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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/ADT/StringRef.h"
14*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
15*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
16*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleExecutorMemoryManager.h"
17*349cc55cSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/SimpleRemoteEPCServer.h"
18*349cc55cSDimitry Andric #include "llvm/Support/DynamicLibrary.h"
19*349cc55cSDimitry Andric #include "llvm/Support/Error.h"
20*349cc55cSDimitry Andric #include "llvm/Support/MathExtras.h"
21*349cc55cSDimitry Andric #include "llvm/Support/raw_ostream.h"
22*349cc55cSDimitry Andric #include <cstring>
23*349cc55cSDimitry Andric #include <sstream>
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric using namespace llvm::orc;
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric ExitOnError ExitOnErr;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric int main(int argc, char *argv[]) {
31*349cc55cSDimitry Andric #if LLVM_ENABLE_THREADS
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric   if (argc != 3) {
340b57cec5SDimitry Andric     errs() << "Usage: " << argv[0] << " <input fd> <output fd>\n";
350b57cec5SDimitry Andric     return 1;
360b57cec5SDimitry Andric   }
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
390b57cec5SDimitry Andric     errs() << "Error loading program symbols.\n";
400b57cec5SDimitry Andric     return 1;
410b57cec5SDimitry Andric   }
420b57cec5SDimitry Andric 
43*349cc55cSDimitry Andric   ExitOnErr.setBanner(std::string(argv[0]) + ": ");
440b57cec5SDimitry Andric 
45*349cc55cSDimitry Andric   int InFD = 0;
46*349cc55cSDimitry Andric   int OutFD = 0;
47*349cc55cSDimitry Andric   {
48*349cc55cSDimitry Andric     std::istringstream InFDStream(argv[1]), OutFDStream(argv[2]);
49*349cc55cSDimitry Andric     InFDStream >> InFD;
50*349cc55cSDimitry Andric     OutFDStream >> OutFD;
51*349cc55cSDimitry Andric   }
520b57cec5SDimitry Andric 
53*349cc55cSDimitry Andric   auto Server =
54*349cc55cSDimitry Andric       ExitOnErr(SimpleRemoteEPCServer::Create<FDSimpleRemoteEPCTransport>(
55*349cc55cSDimitry Andric           [](SimpleRemoteEPCServer::Setup &S) -> Error {
56*349cc55cSDimitry Andric             S.setDispatcher(
57*349cc55cSDimitry Andric                 std::make_unique<SimpleRemoteEPCServer::ThreadDispatcher>());
58*349cc55cSDimitry Andric             S.bootstrapSymbols() =
59*349cc55cSDimitry Andric                 SimpleRemoteEPCServer::defaultBootstrapSymbols();
60*349cc55cSDimitry Andric             S.services().push_back(
61*349cc55cSDimitry Andric                 std::make_unique<rt_bootstrap::SimpleExecutorMemoryManager>());
62*349cc55cSDimitry Andric             return Error::success();
63*349cc55cSDimitry Andric           },
64*349cc55cSDimitry Andric           InFD, OutFD));
650b57cec5SDimitry Andric 
66*349cc55cSDimitry Andric   ExitOnErr(Server->waitForDisconnect());
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   return 0;
69*349cc55cSDimitry Andric 
70*349cc55cSDimitry Andric #else
71*349cc55cSDimitry Andric   errs() << argv[0]
72*349cc55cSDimitry Andric          << " error: this tool requires threads, but LLVM was "
73*349cc55cSDimitry Andric             "built with LLVM_ENABLE_THREADS=Off\n";
74*349cc55cSDimitry Andric   return 1;
75*349cc55cSDimitry Andric #endif
760b57cec5SDimitry Andric }
77