1//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
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// This utility provides a simple wrapper around the LLVM Execution Engines,
10// which allow the direct execution of LLVM programs through a Just-In-Time
11// compiler, or through an interpreter if no JIT is available for this platform.
12//
13//===----------------------------------------------------------------------===//
14
15#include "ForwardingMemoryManager.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Bitcode/BitcodeReader.h"
18#include "llvm/CodeGen/CommandFlags.h"
19#include "llvm/CodeGen/LinkAllCodegenComponents.h"
20#include "llvm/Config/llvm-config.h"
21#include "llvm/ExecutionEngine/GenericValue.h"
22#include "llvm/ExecutionEngine/Interpreter.h"
23#include "llvm/ExecutionEngine/JITEventListener.h"
24#include "llvm/ExecutionEngine/JITSymbol.h"
25#include "llvm/ExecutionEngine/MCJIT.h"
26#include "llvm/ExecutionEngine/ObjectCache.h"
27#include "llvm/ExecutionEngine/Orc/DebugUtils.h"
28#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
29#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
30#include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
31#include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"
32#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
33#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
34#include "llvm/ExecutionEngine/Orc/LLJIT.h"
35#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
36#include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
37#include "llvm/ExecutionEngine/Orc/SimpleRemoteEPC.h"
38#include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
39#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
40#include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
41#include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
42#include "llvm/ExecutionEngine/SectionMemoryManager.h"
43#include "llvm/IR/IRBuilder.h"
44#include "llvm/IR/LLVMContext.h"
45#include "llvm/IR/Module.h"
46#include "llvm/IR/Type.h"
47#include "llvm/IR/Verifier.h"
48#include "llvm/IRReader/IRReader.h"
49#include "llvm/Object/Archive.h"
50#include "llvm/Object/ObjectFile.h"
51#include "llvm/Support/CommandLine.h"
52#include "llvm/Support/Debug.h"
53#include "llvm/Support/DynamicLibrary.h"
54#include "llvm/Support/Format.h"
55#include "llvm/Support/InitLLVM.h"
56#include "llvm/Support/MathExtras.h"
57#include "llvm/Support/Memory.h"
58#include "llvm/Support/MemoryBuffer.h"
59#include "llvm/Support/Path.h"
60#include "llvm/Support/PluginLoader.h"
61#include "llvm/Support/Process.h"
62#include "llvm/Support/Program.h"
63#include "llvm/Support/SourceMgr.h"
64#include "llvm/Support/TargetSelect.h"
65#include "llvm/Support/ToolOutputFile.h"
66#include "llvm/Support/WithColor.h"
67#include "llvm/Support/raw_ostream.h"
68#include "llvm/TargetParser/Triple.h"
69#include "llvm/Transforms/Instrumentation.h"
70#include <cerrno>
71#include <optional>
72
73#if !defined(_MSC_VER) && !defined(__MINGW32__)
74#include <unistd.h>
75#else
76#include <io.h>
77#endif
78
79#ifdef __CYGWIN__
80#include <cygwin/version.h>
81#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
82#define DO_NOTHING_ATEXIT 1
83#endif
84#endif
85
86using namespace llvm;
87
88static codegen::RegisterCodeGenFlags CGF;
89
90#define DEBUG_TYPE "lli"
91
92namespace {
93
94  enum class JITKind { MCJIT, Orc, OrcLazy };
95  enum class JITLinkerKind { Default, RuntimeDyld, JITLink };
96
97  cl::opt<std::string>
98  InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
99
100  cl::list<std::string>
101  InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
102
103  cl::opt<bool> ForceInterpreter("force-interpreter",
104                                 cl::desc("Force interpretation: disable JIT"),
105                                 cl::init(false));
106
107  cl::opt<JITKind> UseJITKind(
108      "jit-kind", cl::desc("Choose underlying JIT kind."),
109      cl::init(JITKind::Orc),
110      cl::values(clEnumValN(JITKind::MCJIT, "mcjit", "MCJIT"),
111                 clEnumValN(JITKind::Orc, "orc", "Orc JIT"),
112                 clEnumValN(JITKind::OrcLazy, "orc-lazy",
113                            "Orc-based lazy JIT.")));
114
115  cl::opt<JITLinkerKind>
116      JITLinker("jit-linker", cl::desc("Choose the dynamic linker/loader."),
117                cl::init(JITLinkerKind::Default),
118                cl::values(clEnumValN(JITLinkerKind::Default, "default",
119                                      "Default for platform and JIT-kind"),
120                           clEnumValN(JITLinkerKind::RuntimeDyld, "rtdyld",
121                                      "RuntimeDyld"),
122                           clEnumValN(JITLinkerKind::JITLink, "jitlink",
123                                      "Orc-specific linker")));
124  cl::opt<std::string> OrcRuntime("orc-runtime",
125                                  cl::desc("Use ORC runtime from given path"),
126                                  cl::init(""));
127
128  cl::opt<unsigned>
129  LazyJITCompileThreads("compile-threads",
130                        cl::desc("Choose the number of compile threads "
131                                 "(jit-kind=orc-lazy only)"),
132                        cl::init(0));
133
134  cl::list<std::string>
135  ThreadEntryPoints("thread-entry",
136                    cl::desc("calls the given entry-point on a new thread "
137                             "(jit-kind=orc-lazy only)"));
138
139  cl::opt<bool> PerModuleLazy(
140      "per-module-lazy",
141      cl::desc("Performs lazy compilation on whole module boundaries "
142               "rather than individual functions"),
143      cl::init(false));
144
145  cl::list<std::string>
146      JITDylibs("jd",
147                cl::desc("Specifies the JITDylib to be used for any subsequent "
148                         "-extra-module arguments."));
149
150  cl::list<std::string>
151      Dylibs("dlopen", cl::desc("Dynamic libraries to load before linking"));
152
153  // The MCJIT supports building for a target address space separate from
154  // the JIT compilation process. Use a forked process and a copying
155  // memory manager with IPC to execute using this functionality.
156  cl::opt<bool> RemoteMCJIT("remote-mcjit",
157    cl::desc("Execute MCJIT'ed code in a separate process."),
158    cl::init(false));
159
160  // Manually specify the child process for remote execution. This overrides
161  // the simulated remote execution that allocates address space for child
162  // execution. The child process will be executed and will communicate with
163  // lli via stdin/stdout pipes.
164  cl::opt<std::string>
165  ChildExecPath("mcjit-remote-process",
166                cl::desc("Specify the filename of the process to launch "
167                         "for remote MCJIT execution.  If none is specified,"
168                         "\n\tremote execution will be simulated in-process."),
169                cl::value_desc("filename"), cl::init(""));
170
171  // Determine optimization level.
172  cl::opt<char> OptLevel("O",
173                         cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
174                                  "(default = '-O2')"),
175                         cl::Prefix, cl::init('2'));
176
177  cl::opt<std::string>
178  TargetTriple("mtriple", cl::desc("Override target triple for module"));
179
180  cl::opt<std::string>
181  EntryFunc("entry-function",
182            cl::desc("Specify the entry function (default = 'main') "
183                     "of the executable"),
184            cl::value_desc("function"),
185            cl::init("main"));
186
187  cl::list<std::string>
188  ExtraModules("extra-module",
189         cl::desc("Extra modules to be loaded"),
190         cl::value_desc("input bitcode"));
191
192  cl::list<std::string>
193  ExtraObjects("extra-object",
194         cl::desc("Extra object files to be loaded"),
195         cl::value_desc("input object"));
196
197  cl::list<std::string>
198  ExtraArchives("extra-archive",
199         cl::desc("Extra archive files to be loaded"),
200         cl::value_desc("input archive"));
201
202  cl::opt<bool>
203  EnableCacheManager("enable-cache-manager",
204        cl::desc("Use cache manager to save/load modules"),
205        cl::init(false));
206
207  cl::opt<std::string>
208  ObjectCacheDir("object-cache-dir",
209                  cl::desc("Directory to store cached object files "
210                           "(must be user writable)"),
211                  cl::init(""));
212
213  cl::opt<std::string>
214  FakeArgv0("fake-argv0",
215            cl::desc("Override the 'argv[0]' value passed into the executing"
216                     " program"), cl::value_desc("executable"));
217
218  cl::opt<bool>
219  DisableCoreFiles("disable-core-files", cl::Hidden,
220                   cl::desc("Disable emission of core files if possible"));
221
222  cl::opt<bool>
223  NoLazyCompilation("disable-lazy-compilation",
224                  cl::desc("Disable JIT lazy compilation"),
225                  cl::init(false));
226
227  cl::opt<bool>
228  GenerateSoftFloatCalls("soft-float",
229    cl::desc("Generate software floating point library calls"),
230    cl::init(false));
231
232  cl::opt<bool> NoProcessSymbols(
233      "no-process-syms",
234      cl::desc("Do not resolve lli process symbols in JIT'd code"),
235      cl::init(false));
236
237  enum class LLJITPlatform { Inactive, Auto, ExecutorNative, GenericIR };
238
239  cl::opt<LLJITPlatform> Platform(
240      "lljit-platform", cl::desc("Platform to use with LLJIT"),
241      cl::init(LLJITPlatform::Auto),
242      cl::values(clEnumValN(LLJITPlatform::Auto, "Auto",
243                            "Like 'ExecutorNative' if ORC runtime "
244                            "provided, otherwise like 'GenericIR'"),
245                 clEnumValN(LLJITPlatform::ExecutorNative, "ExecutorNative",
246                            "Use the native platform for the executor."
247                            "Requires -orc-runtime"),
248                 clEnumValN(LLJITPlatform::GenericIR, "GenericIR",
249                            "Use LLJITGenericIRPlatform"),
250                 clEnumValN(LLJITPlatform::Inactive, "Inactive",
251                            "Disable platform support explicitly")),
252      cl::Hidden);
253
254  enum class DumpKind {
255    NoDump,
256    DumpFuncsToStdOut,
257    DumpModsToStdOut,
258    DumpModsToDisk,
259    DumpDebugDescriptor,
260    DumpDebugObjects,
261  };
262
263  cl::opt<DumpKind> OrcDumpKind(
264      "orc-lazy-debug", cl::desc("Debug dumping for the orc-lazy JIT."),
265      cl::init(DumpKind::NoDump),
266      cl::values(
267          clEnumValN(DumpKind::NoDump, "no-dump", "Don't dump anything."),
268          clEnumValN(DumpKind::DumpFuncsToStdOut, "funcs-to-stdout",
269                     "Dump function names to stdout."),
270          clEnumValN(DumpKind::DumpModsToStdOut, "mods-to-stdout",
271                     "Dump modules to stdout."),
272          clEnumValN(DumpKind::DumpModsToDisk, "mods-to-disk",
273                     "Dump modules to the current "
274                     "working directory. (WARNING: "
275                     "will overwrite existing files)."),
276          clEnumValN(DumpKind::DumpDebugDescriptor, "jit-debug-descriptor",
277                     "Dump __jit_debug_descriptor contents to stdout"),
278          clEnumValN(DumpKind::DumpDebugObjects, "jit-debug-objects",
279                     "Dump __jit_debug_descriptor in-memory debug "
280                     "objects as tool output")),
281      cl::Hidden);
282
283  ExitOnError ExitOnErr;
284}
285
286LLVM_ATTRIBUTE_USED void linkComponents() {
287  errs() << (void *)&llvm_orc_registerEHFrameSectionWrapper
288         << (void *)&llvm_orc_deregisterEHFrameSectionWrapper
289         << (void *)&llvm_orc_registerJITLoaderGDBWrapper
290         << (void *)&llvm_orc_registerJITLoaderGDBAllocAction;
291}
292
293//===----------------------------------------------------------------------===//
294// Object cache
295//
296// This object cache implementation writes cached objects to disk to the
297// directory specified by CacheDir, using a filename provided in the module
298// descriptor. The cache tries to load a saved object using that path if the
299// file exists. CacheDir defaults to "", in which case objects are cached
300// alongside their originating bitcodes.
301//
302class LLIObjectCache : public ObjectCache {
303public:
304  LLIObjectCache(const std::string& CacheDir) : CacheDir(CacheDir) {
305    // Add trailing '/' to cache dir if necessary.
306    if (!this->CacheDir.empty() &&
307        this->CacheDir[this->CacheDir.size() - 1] != '/')
308      this->CacheDir += '/';
309  }
310  ~LLIObjectCache() override {}
311
312  void notifyObjectCompiled(const Module *M, MemoryBufferRef Obj) override {
313    const std::string &ModuleID = M->getModuleIdentifier();
314    std::string CacheName;
315    if (!getCacheFilename(ModuleID, CacheName))
316      return;
317    if (!CacheDir.empty()) { // Create user-defined cache dir.
318      SmallString<128> dir(sys::path::parent_path(CacheName));
319      sys::fs::create_directories(Twine(dir));
320    }
321
322    std::error_code EC;
323    raw_fd_ostream outfile(CacheName, EC, sys::fs::OF_None);
324    outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
325    outfile.close();
326  }
327
328  std::unique_ptr<MemoryBuffer> getObject(const Module* M) override {
329    const std::string &ModuleID = M->getModuleIdentifier();
330    std::string CacheName;
331    if (!getCacheFilename(ModuleID, CacheName))
332      return nullptr;
333    // Load the object from the cache filename
334    ErrorOr<std::unique_ptr<MemoryBuffer>> IRObjectBuffer =
335        MemoryBuffer::getFile(CacheName, /*IsText=*/false,
336                              /*RequiresNullTerminator=*/false);
337    // If the file isn't there, that's OK.
338    if (!IRObjectBuffer)
339      return nullptr;
340    // MCJIT will want to write into this buffer, and we don't want that
341    // because the file has probably just been mmapped.  Instead we make
342    // a copy.  The filed-based buffer will be released when it goes
343    // out of scope.
344    return MemoryBuffer::getMemBufferCopy(IRObjectBuffer.get()->getBuffer());
345  }
346
347private:
348  std::string CacheDir;
349
350  bool getCacheFilename(const std::string &ModID, std::string &CacheName) {
351    std::string Prefix("file:");
352    size_t PrefixLength = Prefix.length();
353    if (ModID.substr(0, PrefixLength) != Prefix)
354      return false;
355
356    std::string CacheSubdir = ModID.substr(PrefixLength);
357    // Transform "X:\foo" => "/X\foo" for convenience on Windows.
358    if (is_style_windows(llvm::sys::path::Style::native) &&
359        isalpha(CacheSubdir[0]) && CacheSubdir[1] == ':') {
360      CacheSubdir[1] = CacheSubdir[0];
361      CacheSubdir[0] = '/';
362    }
363
364    CacheName = CacheDir + CacheSubdir;
365    size_t pos = CacheName.rfind('.');
366    CacheName.replace(pos, CacheName.length() - pos, ".o");
367    return true;
368  }
369};
370
371// On Mingw and Cygwin, an external symbol named '__main' is called from the
372// generated 'main' function to allow static initialization.  To avoid linking
373// problems with remote targets (because lli's remote target support does not
374// currently handle external linking) we add a secondary module which defines
375// an empty '__main' function.
376static void addCygMingExtraModule(ExecutionEngine &EE, LLVMContext &Context,
377                                  StringRef TargetTripleStr) {
378  IRBuilder<> Builder(Context);
379  Triple TargetTriple(TargetTripleStr);
380
381  // Create a new module.
382  std::unique_ptr<Module> M = std::make_unique<Module>("CygMingHelper", Context);
383  M->setTargetTriple(TargetTripleStr);
384
385  // Create an empty function named "__main".
386  Type *ReturnTy;
387  if (TargetTriple.isArch64Bit())
388    ReturnTy = Type::getInt64Ty(Context);
389  else
390    ReturnTy = Type::getInt32Ty(Context);
391  Function *Result =
392      Function::Create(FunctionType::get(ReturnTy, {}, false),
393                       GlobalValue::ExternalLinkage, "__main", M.get());
394
395  BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
396  Builder.SetInsertPoint(BB);
397  Value *ReturnVal = ConstantInt::get(ReturnTy, 0);
398  Builder.CreateRet(ReturnVal);
399
400  // Add this new module to the ExecutionEngine.
401  EE.addModule(std::move(M));
402}
403
404CodeGenOptLevel getOptLevel() {
405  if (auto Level = CodeGenOpt::parseLevel(OptLevel))
406    return *Level;
407  WithColor::error(errs(), "lli") << "invalid optimization level.\n";
408  exit(1);
409}
410
411[[noreturn]] static void reportError(SMDiagnostic Err, const char *ProgName) {
412  Err.print(ProgName, errs());
413  exit(1);
414}
415
416Error loadDylibs();
417int runOrcJIT(const char *ProgName);
418void disallowOrcOptions();
419Expected<std::unique_ptr<orc::ExecutorProcessControl>> launchRemote();
420
421//===----------------------------------------------------------------------===//
422// main Driver function
423//
424int main(int argc, char **argv, char * const *envp) {
425  InitLLVM X(argc, argv);
426
427  if (argc > 1)
428    ExitOnErr.setBanner(std::string(argv[0]) + ": ");
429
430  // If we have a native target, initialize it to ensure it is linked in and
431  // usable by the JIT.
432  InitializeNativeTarget();
433  InitializeNativeTargetAsmPrinter();
434  InitializeNativeTargetAsmParser();
435
436  cl::ParseCommandLineOptions(argc, argv,
437                              "llvm interpreter & dynamic compiler\n");
438
439  // If the user doesn't want core files, disable them.
440  if (DisableCoreFiles)
441    sys::Process::PreventCoreFiles();
442
443  ExitOnErr(loadDylibs());
444
445  if (EntryFunc.empty()) {
446    WithColor::error(errs(), argv[0])
447        << "--entry-function name cannot be empty\n";
448    exit(1);
449  }
450
451  if (UseJITKind == JITKind::MCJIT || ForceInterpreter)
452    disallowOrcOptions();
453  else
454    return runOrcJIT(argv[0]);
455
456  // Old lli implementation based on ExecutionEngine and MCJIT.
457  LLVMContext Context;
458
459  // Load the bitcode...
460  SMDiagnostic Err;
461  std::unique_ptr<Module> Owner = parseIRFile(InputFile, Err, Context);
462  Module *Mod = Owner.get();
463  if (!Mod)
464    reportError(Err, argv[0]);
465
466  if (EnableCacheManager) {
467    std::string CacheName("file:");
468    CacheName.append(InputFile);
469    Mod->setModuleIdentifier(CacheName);
470  }
471
472  // If not jitting lazily, load the whole bitcode file eagerly too.
473  if (NoLazyCompilation) {
474    // Use *argv instead of argv[0] to work around a wrong GCC warning.
475    ExitOnError ExitOnErr(std::string(*argv) +
476                          ": bitcode didn't read correctly: ");
477    ExitOnErr(Mod->materializeAll());
478  }
479
480  std::string ErrorMsg;
481  EngineBuilder builder(std::move(Owner));
482  builder.setMArch(codegen::getMArch());
483  builder.setMCPU(codegen::getCPUStr());
484  builder.setMAttrs(codegen::getFeatureList());
485  if (auto RM = codegen::getExplicitRelocModel())
486    builder.setRelocationModel(*RM);
487  if (auto CM = codegen::getExplicitCodeModel())
488    builder.setCodeModel(*CM);
489  builder.setErrorStr(&ErrorMsg);
490  builder.setEngineKind(ForceInterpreter
491                        ? EngineKind::Interpreter
492                        : EngineKind::JIT);
493
494  // If we are supposed to override the target triple, do so now.
495  if (!TargetTriple.empty())
496    Mod->setTargetTriple(Triple::normalize(TargetTriple));
497
498  // Enable MCJIT if desired.
499  RTDyldMemoryManager *RTDyldMM = nullptr;
500  if (!ForceInterpreter) {
501    if (RemoteMCJIT)
502      RTDyldMM = new ForwardingMemoryManager();
503    else
504      RTDyldMM = new SectionMemoryManager();
505
506    // Deliberately construct a temp std::unique_ptr to pass in. Do not null out
507    // RTDyldMM: We still use it below, even though we don't own it.
508    builder.setMCJITMemoryManager(
509      std::unique_ptr<RTDyldMemoryManager>(RTDyldMM));
510  } else if (RemoteMCJIT) {
511    WithColor::error(errs(), argv[0])
512        << "remote process execution does not work with the interpreter.\n";
513    exit(1);
514  }
515
516  builder.setOptLevel(getOptLevel());
517
518  TargetOptions Options =
519      codegen::InitTargetOptionsFromCodeGenFlags(Triple(TargetTriple));
520  if (codegen::getFloatABIForCalls() != FloatABI::Default)
521    Options.FloatABIType = codegen::getFloatABIForCalls();
522
523  builder.setTargetOptions(Options);
524
525  std::unique_ptr<ExecutionEngine> EE(builder.create());
526  if (!EE) {
527    if (!ErrorMsg.empty())
528      WithColor::error(errs(), argv[0])
529          << "error creating EE: " << ErrorMsg << "\n";
530    else
531      WithColor::error(errs(), argv[0]) << "unknown error creating EE!\n";
532    exit(1);
533  }
534
535  std::unique_ptr<LLIObjectCache> CacheManager;
536  if (EnableCacheManager) {
537    CacheManager.reset(new LLIObjectCache(ObjectCacheDir));
538    EE->setObjectCache(CacheManager.get());
539  }
540
541  // Load any additional modules specified on the command line.
542  for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
543    std::unique_ptr<Module> XMod = parseIRFile(ExtraModules[i], Err, Context);
544    if (!XMod)
545      reportError(Err, argv[0]);
546    if (EnableCacheManager) {
547      std::string CacheName("file:");
548      CacheName.append(ExtraModules[i]);
549      XMod->setModuleIdentifier(CacheName);
550    }
551    EE->addModule(std::move(XMod));
552  }
553
554  for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
555    Expected<object::OwningBinary<object::ObjectFile>> Obj =
556        object::ObjectFile::createObjectFile(ExtraObjects[i]);
557    if (!Obj) {
558      // TODO: Actually report errors helpfully.
559      consumeError(Obj.takeError());
560      reportError(Err, argv[0]);
561    }
562    object::OwningBinary<object::ObjectFile> &O = Obj.get();
563    EE->addObjectFile(std::move(O));
564  }
565
566  for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
567    ErrorOr<std::unique_ptr<MemoryBuffer>> ArBufOrErr =
568        MemoryBuffer::getFileOrSTDIN(ExtraArchives[i]);
569    if (!ArBufOrErr)
570      reportError(Err, argv[0]);
571    std::unique_ptr<MemoryBuffer> &ArBuf = ArBufOrErr.get();
572
573    Expected<std::unique_ptr<object::Archive>> ArOrErr =
574        object::Archive::create(ArBuf->getMemBufferRef());
575    if (!ArOrErr) {
576      std::string Buf;
577      raw_string_ostream OS(Buf);
578      logAllUnhandledErrors(ArOrErr.takeError(), OS);
579      OS.flush();
580      errs() << Buf;
581      exit(1);
582    }
583    std::unique_ptr<object::Archive> &Ar = ArOrErr.get();
584
585    object::OwningBinary<object::Archive> OB(std::move(Ar), std::move(ArBuf));
586
587    EE->addArchive(std::move(OB));
588  }
589
590  // If the target is Cygwin/MingW and we are generating remote code, we
591  // need an extra module to help out with linking.
592  if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
593    addCygMingExtraModule(*EE, Context, Mod->getTargetTriple());
594  }
595
596  // The following functions have no effect if their respective profiling
597  // support wasn't enabled in the build configuration.
598  EE->RegisterJITEventListener(
599                JITEventListener::createOProfileJITEventListener());
600  EE->RegisterJITEventListener(
601                JITEventListener::createIntelJITEventListener());
602  if (!RemoteMCJIT)
603    EE->RegisterJITEventListener(
604                JITEventListener::createPerfJITEventListener());
605
606  if (!NoLazyCompilation && RemoteMCJIT) {
607    WithColor::warning(errs(), argv[0])
608        << "remote mcjit does not support lazy compilation\n";
609    NoLazyCompilation = true;
610  }
611  EE->DisableLazyCompilation(NoLazyCompilation);
612
613  // If the user specifically requested an argv[0] to pass into the program,
614  // do it now.
615  if (!FakeArgv0.empty()) {
616    InputFile = static_cast<std::string>(FakeArgv0);
617  } else {
618    // Otherwise, if there is a .bc suffix on the executable strip it off, it
619    // might confuse the program.
620    if (StringRef(InputFile).ends_with(".bc"))
621      InputFile.erase(InputFile.length() - 3);
622  }
623
624  // Add the module's name to the start of the vector of arguments to main().
625  InputArgv.insert(InputArgv.begin(), InputFile);
626
627  // Call the main function from M as if its signature were:
628  //   int main (int argc, char **argv, const char **envp)
629  // using the contents of Args to determine argc & argv, and the contents of
630  // EnvVars to determine envp.
631  //
632  Function *EntryFn = Mod->getFunction(EntryFunc);
633  if (!EntryFn) {
634    WithColor::error(errs(), argv[0])
635        << '\'' << EntryFunc << "\' function not found in module.\n";
636    return -1;
637  }
638
639  // Reset errno to zero on entry to main.
640  errno = 0;
641
642  int Result = -1;
643
644  // Sanity check use of remote-jit: LLI currently only supports use of the
645  // remote JIT on Unix platforms.
646  if (RemoteMCJIT) {
647#ifndef LLVM_ON_UNIX
648    WithColor::warning(errs(), argv[0])
649        << "host does not support external remote targets.\n";
650    WithColor::note() << "defaulting to local execution\n";
651    return -1;
652#else
653    if (ChildExecPath.empty()) {
654      WithColor::error(errs(), argv[0])
655          << "-remote-mcjit requires -mcjit-remote-process.\n";
656      exit(1);
657    } else if (!sys::fs::can_execute(ChildExecPath)) {
658      WithColor::error(errs(), argv[0])
659          << "unable to find usable child executable: '" << ChildExecPath
660          << "'\n";
661      return -1;
662    }
663#endif
664  }
665
666  if (!RemoteMCJIT) {
667    // If the program doesn't explicitly call exit, we will need the Exit
668    // function later on to make an explicit call, so get the function now.
669    FunctionCallee Exit = Mod->getOrInsertFunction(
670        "exit", Type::getVoidTy(Context), Type::getInt32Ty(Context));
671
672    // Run static constructors.
673    if (!ForceInterpreter) {
674      // Give MCJIT a chance to apply relocations and set page permissions.
675      EE->finalizeObject();
676    }
677    EE->runStaticConstructorsDestructors(false);
678
679    // Trigger compilation separately so code regions that need to be
680    // invalidated will be known.
681    (void)EE->getPointerToFunction(EntryFn);
682    // Clear instruction cache before code will be executed.
683    if (RTDyldMM)
684      static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
685
686    // Run main.
687    Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
688
689    // Run static destructors.
690    EE->runStaticConstructorsDestructors(true);
691
692    // If the program didn't call exit explicitly, we should call it now.
693    // This ensures that any atexit handlers get called correctly.
694    if (Function *ExitF =
695            dyn_cast<Function>(Exit.getCallee()->stripPointerCasts())) {
696      if (ExitF->getFunctionType() == Exit.getFunctionType()) {
697        std::vector<GenericValue> Args;
698        GenericValue ResultGV;
699        ResultGV.IntVal = APInt(32, Result);
700        Args.push_back(ResultGV);
701        EE->runFunction(ExitF, Args);
702        WithColor::error(errs(), argv[0])
703            << "exit(" << Result << ") returned!\n";
704        abort();
705      }
706    }
707    WithColor::error(errs(), argv[0]) << "exit defined with wrong prototype!\n";
708    abort();
709  } else {
710    // else == "if (RemoteMCJIT)"
711    std::unique_ptr<orc::ExecutorProcessControl> EPC = ExitOnErr(launchRemote());
712
713    // Remote target MCJIT doesn't (yet) support static constructors. No reason
714    // it couldn't. This is a limitation of the LLI implementation, not the
715    // MCJIT itself. FIXME.
716
717    // Create a remote memory manager.
718    auto RemoteMM = ExitOnErr(
719        orc::EPCGenericRTDyldMemoryManager::CreateWithDefaultBootstrapSymbols(
720            *EPC));
721
722    // Forward MCJIT's memory manager calls to the remote memory manager.
723    static_cast<ForwardingMemoryManager*>(RTDyldMM)->setMemMgr(
724      std::move(RemoteMM));
725
726    // Forward MCJIT's symbol resolution calls to the remote.
727    static_cast<ForwardingMemoryManager *>(RTDyldMM)->setResolver(
728        ExitOnErr(RemoteResolver::Create(*EPC)));
729    // Grab the target address of the JIT'd main function on the remote and call
730    // it.
731    // FIXME: argv and envp handling.
732    auto Entry =
733        orc::ExecutorAddr(EE->getFunctionAddress(EntryFn->getName().str()));
734    EE->finalizeObject();
735    LLVM_DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
736                      << format("%llx", Entry.getValue()) << "\n");
737    Result = ExitOnErr(EPC->runAsMain(Entry, {}));
738
739    // Like static constructors, the remote target MCJIT support doesn't handle
740    // this yet. It could. FIXME.
741
742    // Delete the EE - we need to tear it down *before* we terminate the session
743    // with the remote, otherwise it'll crash when it tries to release resources
744    // on a remote that has already been disconnected.
745    EE.reset();
746
747    // Signal the remote target that we're done JITing.
748    ExitOnErr(EPC->disconnect());
749  }
750
751  return Result;
752}
753
754// JITLink debug support plugins put information about JITed code in this GDB
755// JIT Interface global from OrcTargetProcess.
756extern "C" struct jit_descriptor __jit_debug_descriptor;
757
758static struct jit_code_entry *
759findNextDebugDescriptorEntry(struct jit_code_entry *Latest) {
760  if (Latest == nullptr)
761    return __jit_debug_descriptor.first_entry;
762  if (Latest->next_entry)
763    return Latest->next_entry;
764  return nullptr;
765}
766
767static ToolOutputFile &claimToolOutput() {
768  static std::unique_ptr<ToolOutputFile> ToolOutput = nullptr;
769  if (ToolOutput) {
770    WithColor::error(errs(), "lli")
771        << "Can not claim stdout for tool output twice\n";
772    exit(1);
773  }
774  std::error_code EC;
775  ToolOutput = std::make_unique<ToolOutputFile>("-", EC, sys::fs::OF_None);
776  if (EC) {
777    WithColor::error(errs(), "lli")
778        << "Failed to create tool output file: " << EC.message() << "\n";
779    exit(1);
780  }
781  return *ToolOutput;
782}
783
784static std::function<void(Module &)> createIRDebugDumper() {
785  switch (OrcDumpKind) {
786  case DumpKind::NoDump:
787  case DumpKind::DumpDebugDescriptor:
788  case DumpKind::DumpDebugObjects:
789    return [](Module &M) {};
790
791  case DumpKind::DumpFuncsToStdOut:
792    return [](Module &M) {
793      printf("[ ");
794
795      for (const auto &F : M) {
796        if (F.isDeclaration())
797          continue;
798
799        if (F.hasName()) {
800          std::string Name(std::string(F.getName()));
801          printf("%s ", Name.c_str());
802        } else
803          printf("<anon> ");
804      }
805
806      printf("]\n");
807    };
808
809  case DumpKind::DumpModsToStdOut:
810    return [](Module &M) {
811      outs() << "----- Module Start -----\n" << M << "----- Module End -----\n";
812    };
813
814  case DumpKind::DumpModsToDisk:
815    return [](Module &M) {
816      std::error_code EC;
817      raw_fd_ostream Out(M.getModuleIdentifier() + ".ll", EC,
818                         sys::fs::OF_TextWithCRLF);
819      if (EC) {
820        errs() << "Couldn't open " << M.getModuleIdentifier()
821               << " for dumping.\nError:" << EC.message() << "\n";
822        exit(1);
823      }
824      Out << M;
825    };
826  }
827  llvm_unreachable("Unknown DumpKind");
828}
829
830static std::function<void(MemoryBuffer &)> createObjDebugDumper() {
831  switch (OrcDumpKind) {
832  case DumpKind::NoDump:
833  case DumpKind::DumpFuncsToStdOut:
834  case DumpKind::DumpModsToStdOut:
835  case DumpKind::DumpModsToDisk:
836    return [](MemoryBuffer &) {};
837
838  case DumpKind::DumpDebugDescriptor: {
839    // Dump the empty descriptor at startup once
840    fprintf(stderr, "jit_debug_descriptor 0x%016" PRIx64 "\n",
841            pointerToJITTargetAddress(__jit_debug_descriptor.first_entry));
842    return [](MemoryBuffer &) {
843      // Dump new entries as they appear
844      static struct jit_code_entry *Latest = nullptr;
845      while (auto *NewEntry = findNextDebugDescriptorEntry(Latest)) {
846        fprintf(stderr, "jit_debug_descriptor 0x%016" PRIx64 "\n",
847                pointerToJITTargetAddress(NewEntry));
848        Latest = NewEntry;
849      }
850    };
851  }
852
853  case DumpKind::DumpDebugObjects: {
854    return [](MemoryBuffer &Obj) {
855      static struct jit_code_entry *Latest = nullptr;
856      static ToolOutputFile &ToolOutput = claimToolOutput();
857      while (auto *NewEntry = findNextDebugDescriptorEntry(Latest)) {
858        ToolOutput.os().write(NewEntry->symfile_addr, NewEntry->symfile_size);
859        Latest = NewEntry;
860      }
861    };
862  }
863  }
864  llvm_unreachable("Unknown DumpKind");
865}
866
867Error loadDylibs() {
868  for (const auto &Dylib : Dylibs) {
869    std::string ErrMsg;
870    if (sys::DynamicLibrary::LoadLibraryPermanently(Dylib.c_str(), &ErrMsg))
871      return make_error<StringError>(ErrMsg, inconvertibleErrorCode());
872  }
873
874  return Error::success();
875}
876
877static void exitOnLazyCallThroughFailure() { exit(1); }
878
879Expected<orc::ThreadSafeModule>
880loadModule(StringRef Path, orc::ThreadSafeContext TSCtx) {
881  SMDiagnostic Err;
882  auto M = parseIRFile(Path, Err, *TSCtx.getContext());
883  if (!M) {
884    std::string ErrMsg;
885    {
886      raw_string_ostream ErrMsgStream(ErrMsg);
887      Err.print("lli", ErrMsgStream);
888    }
889    return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
890  }
891
892  if (EnableCacheManager)
893    M->setModuleIdentifier("file:" + M->getModuleIdentifier());
894
895  return orc::ThreadSafeModule(std::move(M), std::move(TSCtx));
896}
897
898int mingw_noop_main(void) {
899  // Cygwin and MinGW insert calls from the main function to the runtime
900  // function __main. The __main function is responsible for setting up main's
901  // environment (e.g. running static constructors), however this is not needed
902  // when running under lli: the executor process will have run non-JIT ctors,
903  // and ORC will take care of running JIT'd ctors. To avoid a missing symbol
904  // error we just implement __main as a no-op.
905  //
906  // FIXME: Move this to ORC-RT (and the ORC-RT substitution library once it
907  //        exists). That will allow it to work out-of-process, and for all
908  //        ORC tools (the problem isn't lli specific).
909  return 0;
910}
911
912// Try to enable debugger support for the given instance.
913// This alway returns success, but prints a warning if it's not able to enable
914// debugger support.
915Error tryEnableDebugSupport(orc::LLJIT &J) {
916  if (auto Err = enableDebuggerSupport(J)) {
917    [[maybe_unused]] std::string ErrMsg = toString(std::move(Err));
918    LLVM_DEBUG(dbgs() << "lli: " << ErrMsg << "\n");
919  }
920  return Error::success();
921}
922
923int runOrcJIT(const char *ProgName) {
924  // Start setting up the JIT environment.
925
926  // Parse the main module.
927  orc::ThreadSafeContext TSCtx(std::make_unique<LLVMContext>());
928  auto MainModule = ExitOnErr(loadModule(InputFile, TSCtx));
929
930  // Get TargetTriple and DataLayout from the main module if they're explicitly
931  // set.
932  std::optional<Triple> TT;
933  std::optional<DataLayout> DL;
934  MainModule.withModuleDo([&](Module &M) {
935      if (!M.getTargetTriple().empty())
936        TT = Triple(M.getTargetTriple());
937      if (!M.getDataLayout().isDefault())
938        DL = M.getDataLayout();
939    });
940
941  orc::LLLazyJITBuilder Builder;
942
943  Builder.setJITTargetMachineBuilder(
944      TT ? orc::JITTargetMachineBuilder(*TT)
945         : ExitOnErr(orc::JITTargetMachineBuilder::detectHost()));
946
947  TT = Builder.getJITTargetMachineBuilder()->getTargetTriple();
948  if (DL)
949    Builder.setDataLayout(DL);
950
951  if (!codegen::getMArch().empty())
952    Builder.getJITTargetMachineBuilder()->getTargetTriple().setArchName(
953        codegen::getMArch());
954
955  Builder.getJITTargetMachineBuilder()
956      ->setCPU(codegen::getCPUStr())
957      .addFeatures(codegen::getFeatureList())
958      .setRelocationModel(codegen::getExplicitRelocModel())
959      .setCodeModel(codegen::getExplicitCodeModel());
960
961  // Link process symbols unless NoProcessSymbols is set.
962  Builder.setLinkProcessSymbolsByDefault(!NoProcessSymbols);
963
964  // FIXME: Setting a dummy call-through manager in non-lazy mode prevents the
965  // JIT builder to instantiate a default (which would fail with an error for
966  // unsupported architectures).
967  if (UseJITKind != JITKind::OrcLazy) {
968    auto ES = std::make_unique<orc::ExecutionSession>(
969        ExitOnErr(orc::SelfExecutorProcessControl::Create()));
970    Builder.setLazyCallthroughManager(
971        std::make_unique<orc::LazyCallThroughManager>(*ES, orc::ExecutorAddr(),
972                                                      nullptr));
973    Builder.setExecutionSession(std::move(ES));
974  }
975
976  Builder.setLazyCompileFailureAddr(
977      orc::ExecutorAddr::fromPtr(exitOnLazyCallThroughFailure));
978  Builder.setNumCompileThreads(LazyJITCompileThreads);
979
980  // If the object cache is enabled then set a custom compile function
981  // creator to use the cache.
982  std::unique_ptr<LLIObjectCache> CacheManager;
983  if (EnableCacheManager) {
984
985    CacheManager = std::make_unique<LLIObjectCache>(ObjectCacheDir);
986
987    Builder.setCompileFunctionCreator(
988      [&](orc::JITTargetMachineBuilder JTMB)
989            -> Expected<std::unique_ptr<orc::IRCompileLayer::IRCompiler>> {
990        if (LazyJITCompileThreads > 0)
991          return std::make_unique<orc::ConcurrentIRCompiler>(std::move(JTMB),
992                                                        CacheManager.get());
993
994        auto TM = JTMB.createTargetMachine();
995        if (!TM)
996          return TM.takeError();
997
998        return std::make_unique<orc::TMOwningSimpleCompiler>(std::move(*TM),
999                                                        CacheManager.get());
1000      });
1001  }
1002
1003  // Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
1004  Builder.setPrePlatformSetup(tryEnableDebugSupport);
1005
1006  // Set up LLJIT platform.
1007  LLJITPlatform P = Platform;
1008  if (P == LLJITPlatform::Auto)
1009    P = OrcRuntime.empty() ? LLJITPlatform::GenericIR
1010                           : LLJITPlatform::ExecutorNative;
1011
1012  switch (P) {
1013  case LLJITPlatform::ExecutorNative: {
1014    Builder.setPlatformSetUp(orc::ExecutorNativePlatform(OrcRuntime));
1015    break;
1016  }
1017  case LLJITPlatform::GenericIR:
1018    // Nothing to do: LLJITBuilder will use this by default.
1019    break;
1020  case LLJITPlatform::Inactive:
1021    Builder.setPlatformSetUp(orc::setUpInactivePlatform);
1022    break;
1023  default:
1024    llvm_unreachable("Unrecognized platform value");
1025  }
1026
1027  std::unique_ptr<orc::ExecutorProcessControl> EPC = nullptr;
1028  if (JITLinker == JITLinkerKind::JITLink) {
1029    EPC = ExitOnErr(orc::SelfExecutorProcessControl::Create(
1030        std::make_shared<orc::SymbolStringPool>()));
1031
1032    Builder.getJITTargetMachineBuilder()
1033        ->setRelocationModel(Reloc::PIC_)
1034        .setCodeModel(CodeModel::Small);
1035    Builder.setObjectLinkingLayerCreator([&P](orc::ExecutionSession &ES,
1036                                              const Triple &TT) {
1037      auto L = std::make_unique<orc::ObjectLinkingLayer>(ES);
1038      if (P != LLJITPlatform::ExecutorNative)
1039        L->addPlugin(std::make_unique<orc::EHFrameRegistrationPlugin>(
1040            ES, ExitOnErr(orc::EPCEHFrameRegistrar::Create(ES))));
1041      return L;
1042    });
1043  }
1044
1045  auto J = ExitOnErr(Builder.create());
1046
1047  auto *ObjLayer = &J->getObjLinkingLayer();
1048  if (auto *RTDyldObjLayer = dyn_cast<orc::RTDyldObjectLinkingLayer>(ObjLayer)) {
1049    RTDyldObjLayer->registerJITEventListener(
1050        *JITEventListener::createGDBRegistrationListener());
1051#if LLVM_USE_OPROFILE
1052    RTDyldObjLayer->registerJITEventListener(
1053        *JITEventListener::createOProfileJITEventListener());
1054#endif
1055#if LLVM_USE_INTEL_JITEVENTS
1056    RTDyldObjLayer->registerJITEventListener(
1057        *JITEventListener::createIntelJITEventListener());
1058#endif
1059#if LLVM_USE_PERF
1060    RTDyldObjLayer->registerJITEventListener(
1061        *JITEventListener::createPerfJITEventListener());
1062#endif
1063  }
1064
1065  if (PerModuleLazy)
1066    J->setPartitionFunction(orc::CompileOnDemandLayer::compileWholeModule);
1067
1068  auto IRDump = createIRDebugDumper();
1069  J->getIRTransformLayer().setTransform(
1070      [&](orc::ThreadSafeModule TSM,
1071          const orc::MaterializationResponsibility &R) {
1072        TSM.withModuleDo([&](Module &M) {
1073          if (verifyModule(M, &dbgs())) {
1074            dbgs() << "Bad module: " << &M << "\n";
1075            exit(1);
1076          }
1077          IRDump(M);
1078        });
1079        return TSM;
1080      });
1081
1082  auto ObjDump = createObjDebugDumper();
1083  J->getObjTransformLayer().setTransform(
1084      [&](std::unique_ptr<MemoryBuffer> Obj)
1085          -> Expected<std::unique_ptr<MemoryBuffer>> {
1086        ObjDump(*Obj);
1087        return std::move(Obj);
1088      });
1089
1090  // If this is a Mingw or Cygwin executor then we need to alias __main to
1091  // orc_rt_int_void_return_0.
1092  if (J->getTargetTriple().isOSCygMing())
1093    ExitOnErr(J->getProcessSymbolsJITDylib()->define(
1094        orc::absoluteSymbols({{J->mangleAndIntern("__main"),
1095                               {orc::ExecutorAddr::fromPtr(mingw_noop_main),
1096                                JITSymbolFlags::Exported}}})));
1097
1098  // Regular modules are greedy: They materialize as a whole and trigger
1099  // materialization for all required symbols recursively. Lazy modules go
1100  // through partitioning and they replace outgoing calls with reexport stubs
1101  // that resolve on call-through.
1102  auto AddModule = [&](orc::JITDylib &JD, orc::ThreadSafeModule M) {
1103    return UseJITKind == JITKind::OrcLazy ? J->addLazyIRModule(JD, std::move(M))
1104                                          : J->addIRModule(JD, std::move(M));
1105  };
1106
1107  // Add the main module.
1108  ExitOnErr(AddModule(J->getMainJITDylib(), std::move(MainModule)));
1109
1110  // Create JITDylibs and add any extra modules.
1111  {
1112    // Create JITDylibs, keep a map from argument index to dylib. We will use
1113    // -extra-module argument indexes to determine what dylib to use for each
1114    // -extra-module.
1115    std::map<unsigned, orc::JITDylib *> IdxToDylib;
1116    IdxToDylib[0] = &J->getMainJITDylib();
1117    for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
1118         JDItr != JDEnd; ++JDItr) {
1119      orc::JITDylib *JD = J->getJITDylibByName(*JDItr);
1120      if (!JD) {
1121        JD = &ExitOnErr(J->createJITDylib(*JDItr));
1122        J->getMainJITDylib().addToLinkOrder(*JD);
1123        JD->addToLinkOrder(J->getMainJITDylib());
1124      }
1125      IdxToDylib[JITDylibs.getPosition(JDItr - JITDylibs.begin())] = JD;
1126    }
1127
1128    for (auto EMItr = ExtraModules.begin(), EMEnd = ExtraModules.end();
1129         EMItr != EMEnd; ++EMItr) {
1130      auto M = ExitOnErr(loadModule(*EMItr, TSCtx));
1131
1132      auto EMIdx = ExtraModules.getPosition(EMItr - ExtraModules.begin());
1133      assert(EMIdx != 0 && "ExtraModule should have index > 0");
1134      auto JDItr = std::prev(IdxToDylib.lower_bound(EMIdx));
1135      auto &JD = *JDItr->second;
1136      ExitOnErr(AddModule(JD, std::move(M)));
1137    }
1138
1139    for (auto EAItr = ExtraArchives.begin(), EAEnd = ExtraArchives.end();
1140         EAItr != EAEnd; ++EAItr) {
1141      auto EAIdx = ExtraArchives.getPosition(EAItr - ExtraArchives.begin());
1142      assert(EAIdx != 0 && "ExtraArchive should have index > 0");
1143      auto JDItr = std::prev(IdxToDylib.lower_bound(EAIdx));
1144      auto &JD = *JDItr->second;
1145      ExitOnErr(J->linkStaticLibraryInto(JD, EAItr->c_str()));
1146    }
1147  }
1148
1149  // Add the objects.
1150  for (auto &ObjPath : ExtraObjects) {
1151    auto Obj = ExitOnErr(errorOrToExpected(MemoryBuffer::getFile(ObjPath)));
1152    ExitOnErr(J->addObjectFile(std::move(Obj)));
1153  }
1154
1155  // Run any static constructors.
1156  ExitOnErr(J->initialize(J->getMainJITDylib()));
1157
1158  // Run any -thread-entry points.
1159  std::vector<std::thread> AltEntryThreads;
1160  for (auto &ThreadEntryPoint : ThreadEntryPoints) {
1161    auto EntryPointSym = ExitOnErr(J->lookup(ThreadEntryPoint));
1162    typedef void (*EntryPointPtr)();
1163    auto EntryPoint = EntryPointSym.toPtr<EntryPointPtr>();
1164    AltEntryThreads.push_back(std::thread([EntryPoint]() { EntryPoint(); }));
1165  }
1166
1167  // Resolve and run the main function.
1168  auto MainAddr = ExitOnErr(J->lookup(EntryFunc));
1169  int Result;
1170
1171  if (EPC) {
1172    // ExecutorProcessControl-based execution with JITLink.
1173    Result = ExitOnErr(EPC->runAsMain(MainAddr, InputArgv));
1174  } else {
1175    // Manual in-process execution with RuntimeDyld.
1176    using MainFnTy = int(int, char *[]);
1177    auto MainFn = MainAddr.toPtr<MainFnTy *>();
1178    Result = orc::runAsMain(MainFn, InputArgv, StringRef(InputFile));
1179  }
1180
1181  // Wait for -entry-point threads.
1182  for (auto &AltEntryThread : AltEntryThreads)
1183    AltEntryThread.join();
1184
1185  // Run destructors.
1186  ExitOnErr(J->deinitialize(J->getMainJITDylib()));
1187
1188  return Result;
1189}
1190
1191void disallowOrcOptions() {
1192  // Make sure nobody used an orc-lazy specific option accidentally.
1193
1194  if (LazyJITCompileThreads != 0) {
1195    errs() << "-compile-threads requires -jit-kind=orc-lazy\n";
1196    exit(1);
1197  }
1198
1199  if (!ThreadEntryPoints.empty()) {
1200    errs() << "-thread-entry requires -jit-kind=orc-lazy\n";
1201    exit(1);
1202  }
1203
1204  if (PerModuleLazy) {
1205    errs() << "-per-module-lazy requires -jit-kind=orc-lazy\n";
1206    exit(1);
1207  }
1208}
1209
1210Expected<std::unique_ptr<orc::ExecutorProcessControl>> launchRemote() {
1211#ifndef LLVM_ON_UNIX
1212  llvm_unreachable("launchRemote not supported on non-Unix platforms");
1213#else
1214  int PipeFD[2][2];
1215  pid_t ChildPID;
1216
1217  // Create two pipes.
1218  if (pipe(PipeFD[0]) != 0 || pipe(PipeFD[1]) != 0)
1219    perror("Error creating pipe: ");
1220
1221  ChildPID = fork();
1222
1223  if (ChildPID == 0) {
1224    // In the child...
1225
1226    // Close the parent ends of the pipes
1227    close(PipeFD[0][1]);
1228    close(PipeFD[1][0]);
1229
1230
1231    // Execute the child process.
1232    std::unique_ptr<char[]> ChildPath, ChildIn, ChildOut;
1233    {
1234      ChildPath.reset(new char[ChildExecPath.size() + 1]);
1235      std::copy(ChildExecPath.begin(), ChildExecPath.end(), &ChildPath[0]);
1236      ChildPath[ChildExecPath.size()] = '\0';
1237      std::string ChildInStr = utostr(PipeFD[0][0]);
1238      ChildIn.reset(new char[ChildInStr.size() + 1]);
1239      std::copy(ChildInStr.begin(), ChildInStr.end(), &ChildIn[0]);
1240      ChildIn[ChildInStr.size()] = '\0';
1241      std::string ChildOutStr = utostr(PipeFD[1][1]);
1242      ChildOut.reset(new char[ChildOutStr.size() + 1]);
1243      std::copy(ChildOutStr.begin(), ChildOutStr.end(), &ChildOut[0]);
1244      ChildOut[ChildOutStr.size()] = '\0';
1245    }
1246
1247    char * const args[] = { &ChildPath[0], &ChildIn[0], &ChildOut[0], nullptr };
1248    int rc = execv(ChildExecPath.c_str(), args);
1249    if (rc != 0)
1250      perror("Error executing child process: ");
1251    llvm_unreachable("Error executing child process");
1252  }
1253  // else we're the parent...
1254
1255  // Close the child ends of the pipes
1256  close(PipeFD[0][0]);
1257  close(PipeFD[1][1]);
1258
1259  // Return a SimpleRemoteEPC instance connected to our end of the pipes.
1260  return orc::SimpleRemoteEPC::Create<orc::FDSimpleRemoteEPCTransport>(
1261      std::make_unique<llvm::orc::InPlaceTaskDispatcher>(),
1262      llvm::orc::SimpleRemoteEPC::Setup(), PipeFD[1][0], PipeFD[0][1]);
1263#endif
1264}
1265
1266// For MinGW environments, manually export the __chkstk function from the lli
1267// executable.
1268//
1269// Normally, this function is provided by compiler-rt builtins or libgcc.
1270// It is named "_alloca" on i386, "___chkstk_ms" on x86_64, and "__chkstk" on
1271// arm/aarch64. In MSVC configurations, it's named "__chkstk" in all
1272// configurations.
1273//
1274// When Orc tries to resolve symbols at runtime, this succeeds in MSVC
1275// configurations, somewhat by accident/luck; kernelbase.dll does export a
1276// symbol named "__chkstk" which gets found by Orc, even if regular applications
1277// never link against that function from that DLL (it's linked in statically
1278// from a compiler support library).
1279//
1280// The MinGW specific symbol names aren't available in that DLL though.
1281// Therefore, manually export the relevant symbol from lli, to let it be
1282// found at runtime during tests.
1283//
1284// For real JIT uses, the real compiler support libraries should be linked
1285// in, somehow; this is a workaround to let tests pass.
1286//
1287// We need to make sure that this symbol actually is linked in when we
1288// try to export it; if no functions allocate a large enough stack area,
1289// nothing would reference it. Therefore, manually declare it and add a
1290// reference to it. (Note, the declarations of _alloca/___chkstk_ms/__chkstk
1291// are somewhat bogus, these functions use a different custom calling
1292// convention.)
1293//
1294// TODO: Move this into libORC at some point, see
1295// https://github.com/llvm/llvm-project/issues/56603.
1296#ifdef __MINGW32__
1297// This is a MinGW version of #pragma comment(linker, "...") that doesn't
1298// require compiling with -fms-extensions.
1299#if defined(__i386__)
1300#undef _alloca
1301extern "C" void _alloca(void);
1302static __attribute__((used)) void (*const ref_func)(void) = _alloca;
1303static __attribute__((section(".drectve"), used)) const char export_chkstk[] =
1304    "-export:_alloca";
1305#elif defined(__x86_64__)
1306extern "C" void ___chkstk_ms(void);
1307static __attribute__((used)) void (*const ref_func)(void) = ___chkstk_ms;
1308static __attribute__((section(".drectve"), used)) const char export_chkstk[] =
1309    "-export:___chkstk_ms";
1310#else
1311extern "C" void __chkstk(void);
1312static __attribute__((used)) void (*const ref_func)(void) = __chkstk;
1313static __attribute__((section(".drectve"), used)) const char export_chkstk[] =
1314    "-export:__chkstk";
1315#endif
1316#endif
1317