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