1//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This utility provides a simple wrapper around the LLVM Execution Engines,
11// which allow the direct execution of LLVM programs through a Just-In-Time
12// compiler, or through an interpreter if no JIT is available for this platform.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "lli"
17#include "llvm/IR/LLVMContext.h"
18#include "RemoteMemoryManager.h"
19#include "RemoteTarget.h"
20#include "llvm/ADT/Triple.h"
21#include "llvm/Bitcode/ReaderWriter.h"
22#include "llvm/CodeGen/LinkAllCodegenComponents.h"
23#include "llvm/ExecutionEngine/GenericValue.h"
24#include "llvm/ExecutionEngine/Interpreter.h"
25#include "llvm/ExecutionEngine/JIT.h"
26#include "llvm/ExecutionEngine/JITEventListener.h"
27#include "llvm/ExecutionEngine/JITMemoryManager.h"
28#include "llvm/ExecutionEngine/MCJIT.h"
29#include "llvm/ExecutionEngine/SectionMemoryManager.h"
30#include "llvm/IR/IRBuilder.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/Type.h"
33#include "llvm/IR/TypeBuilder.h"
34#include "llvm/IRReader/IRReader.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/DynamicLibrary.h"
38#include "llvm/Support/Format.h"
39#include "llvm/Support/ManagedStatic.h"
40#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/Memory.h"
42#include "llvm/Support/MemoryBuffer.h"
43#include "llvm/Support/PluginLoader.h"
44#include "llvm/Support/PrettyStackTrace.h"
45#include "llvm/Support/Process.h"
46#include "llvm/Support/Program.h"
47#include "llvm/Support/Signals.h"
48#include "llvm/Support/SourceMgr.h"
49#include "llvm/Support/TargetSelect.h"
50#include "llvm/Support/raw_ostream.h"
51#include "llvm/Transforms/Instrumentation.h"
52#include <cerrno>
53
54#ifdef __CYGWIN__
55#include <cygwin/version.h>
56#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007
57#define DO_NOTHING_ATEXIT 1
58#endif
59#endif
60
61using namespace llvm;
62
63namespace {
64  cl::opt<std::string>
65  InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
66
67  cl::list<std::string>
68  InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
69
70  cl::opt<bool> ForceInterpreter("force-interpreter",
71                                 cl::desc("Force interpretation: disable JIT"),
72                                 cl::init(false));
73
74  cl::opt<bool> UseMCJIT(
75    "use-mcjit", cl::desc("Enable use of the MC-based JIT (if available)"),
76    cl::init(false));
77
78  cl::opt<bool> DebugIR(
79    "debug-ir", cl::desc("Generate debug information to allow debugging IR."),
80    cl::init(false));
81
82  // The MCJIT supports building for a target address space separate from
83  // the JIT compilation process. Use a forked process and a copying
84  // memory manager with IPC to execute using this functionality.
85  cl::opt<bool> RemoteMCJIT("remote-mcjit",
86    cl::desc("Execute MCJIT'ed code in a separate process."),
87    cl::init(false));
88
89  // Manually specify the child process for remote execution. This overrides
90  // the simulated remote execution that allocates address space for child
91  // execution. The child process will be executed and will communicate with
92  // lli via stdin/stdout pipes.
93  cl::opt<std::string>
94  MCJITRemoteProcess("mcjit-remote-process",
95            cl::desc("Specify the filename of the process to launch "
96                     "for remote MCJIT execution.  If none is specified,"
97                     "\n\tremote execution will be simulated in-process."),
98            cl::value_desc("filename"),
99            cl::init(""));
100
101  // Determine optimization level.
102  cl::opt<char>
103  OptLevel("O",
104           cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
105                    "(default = '-O2')"),
106           cl::Prefix,
107           cl::ZeroOrMore,
108           cl::init(' '));
109
110  cl::opt<std::string>
111  TargetTriple("mtriple", cl::desc("Override target triple for module"));
112
113  cl::opt<std::string>
114  MArch("march",
115        cl::desc("Architecture to generate assembly for (see --version)"));
116
117  cl::opt<std::string>
118  MCPU("mcpu",
119       cl::desc("Target a specific cpu type (-mcpu=help for details)"),
120       cl::value_desc("cpu-name"),
121       cl::init(""));
122
123  cl::list<std::string>
124  MAttrs("mattr",
125         cl::CommaSeparated,
126         cl::desc("Target specific attributes (-mattr=help for details)"),
127         cl::value_desc("a1,+a2,-a3,..."));
128
129  cl::opt<std::string>
130  EntryFunc("entry-function",
131            cl::desc("Specify the entry function (default = 'main') "
132                     "of the executable"),
133            cl::value_desc("function"),
134            cl::init("main"));
135
136  cl::list<std::string>
137  ExtraModules("extra-module",
138         cl::desc("Extra modules to be loaded"),
139         cl::value_desc("input bitcode"));
140
141  cl::opt<std::string>
142  FakeArgv0("fake-argv0",
143            cl::desc("Override the 'argv[0]' value passed into the executing"
144                     " program"), cl::value_desc("executable"));
145
146  cl::opt<bool>
147  DisableCoreFiles("disable-core-files", cl::Hidden,
148                   cl::desc("Disable emission of core files if possible"));
149
150  cl::opt<bool>
151  NoLazyCompilation("disable-lazy-compilation",
152                  cl::desc("Disable JIT lazy compilation"),
153                  cl::init(false));
154
155  cl::opt<Reloc::Model>
156  RelocModel("relocation-model",
157             cl::desc("Choose relocation model"),
158             cl::init(Reloc::Default),
159             cl::values(
160            clEnumValN(Reloc::Default, "default",
161                       "Target default relocation model"),
162            clEnumValN(Reloc::Static, "static",
163                       "Non-relocatable code"),
164            clEnumValN(Reloc::PIC_, "pic",
165                       "Fully relocatable, position independent code"),
166            clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic",
167                       "Relocatable external references, non-relocatable code"),
168            clEnumValEnd));
169
170  cl::opt<llvm::CodeModel::Model>
171  CMModel("code-model",
172          cl::desc("Choose code model"),
173          cl::init(CodeModel::JITDefault),
174          cl::values(clEnumValN(CodeModel::JITDefault, "default",
175                                "Target default JIT code model"),
176                     clEnumValN(CodeModel::Small, "small",
177                                "Small code model"),
178                     clEnumValN(CodeModel::Kernel, "kernel",
179                                "Kernel code model"),
180                     clEnumValN(CodeModel::Medium, "medium",
181                                "Medium code model"),
182                     clEnumValN(CodeModel::Large, "large",
183                                "Large code model"),
184                     clEnumValEnd));
185
186  cl::opt<bool>
187  GenerateSoftFloatCalls("soft-float",
188    cl::desc("Generate software floating point library calls"),
189    cl::init(false));
190
191  cl::opt<llvm::FloatABI::ABIType>
192  FloatABIForCalls("float-abi",
193                   cl::desc("Choose float ABI type"),
194                   cl::init(FloatABI::Default),
195                   cl::values(
196                     clEnumValN(FloatABI::Default, "default",
197                                "Target default float ABI type"),
198                     clEnumValN(FloatABI::Soft, "soft",
199                                "Soft float ABI (implied by -soft-float)"),
200                     clEnumValN(FloatABI::Hard, "hard",
201                                "Hard float ABI (uses FP registers)"),
202                     clEnumValEnd));
203  cl::opt<bool>
204// In debug builds, make this default to true.
205#ifdef NDEBUG
206#define EMIT_DEBUG false
207#else
208#define EMIT_DEBUG true
209#endif
210  EmitJitDebugInfo("jit-emit-debug",
211    cl::desc("Emit debug information to debugger"),
212    cl::init(EMIT_DEBUG));
213#undef EMIT_DEBUG
214
215  static cl::opt<bool>
216  EmitJitDebugInfoToDisk("jit-emit-debug-to-disk",
217    cl::Hidden,
218    cl::desc("Emit debug info objfiles to disk"),
219    cl::init(false));
220}
221
222static ExecutionEngine *EE = 0;
223
224static void do_shutdown() {
225  // Cygwin-1.5 invokes DLL's dtors before atexit handler.
226#ifndef DO_NOTHING_ATEXIT
227  delete EE;
228  llvm_shutdown();
229#endif
230}
231
232// On Mingw and Cygwin, an external symbol named '__main' is called from the
233// generated 'main' function to allow static intialization.  To avoid linking
234// problems with remote targets (because lli's remote target support does not
235// currently handle external linking) we add a secondary module which defines
236// an empty '__main' function.
237static void addCygMingExtraModule(ExecutionEngine *EE,
238                                  LLVMContext &Context,
239                                  StringRef TargetTripleStr) {
240  IRBuilder<> Builder(Context);
241  Triple TargetTriple(TargetTripleStr);
242
243  // Create a new module.
244  Module *M = new Module("CygMingHelper", Context);
245  M->setTargetTriple(TargetTripleStr);
246
247  // Create an empty function named "__main".
248  Function *Result;
249  if (TargetTriple.isArch64Bit()) {
250    Result = Function::Create(
251      TypeBuilder<int64_t(void), false>::get(Context),
252      GlobalValue::ExternalLinkage, "__main", M);
253  } else {
254    Result = Function::Create(
255      TypeBuilder<int32_t(void), false>::get(Context),
256      GlobalValue::ExternalLinkage, "__main", M);
257  }
258  BasicBlock *BB = BasicBlock::Create(Context, "__main", Result);
259  Builder.SetInsertPoint(BB);
260  Value *ReturnVal;
261  if (TargetTriple.isArch64Bit())
262    ReturnVal = ConstantInt::get(Context, APInt(64, 0));
263  else
264    ReturnVal = ConstantInt::get(Context, APInt(32, 0));
265  Builder.CreateRet(ReturnVal);
266
267  // Add this new module to the ExecutionEngine.
268  EE->addModule(M);
269}
270
271
272//===----------------------------------------------------------------------===//
273// main Driver function
274//
275int main(int argc, char **argv, char * const *envp) {
276  sys::PrintStackTraceOnErrorSignal();
277  PrettyStackTraceProgram X(argc, argv);
278
279  LLVMContext &Context = getGlobalContext();
280  atexit(do_shutdown);  // Call llvm_shutdown() on exit.
281
282  // If we have a native target, initialize it to ensure it is linked in and
283  // usable by the JIT.
284  InitializeNativeTarget();
285  InitializeNativeTargetAsmPrinter();
286  InitializeNativeTargetAsmParser();
287
288  cl::ParseCommandLineOptions(argc, argv,
289                              "llvm interpreter & dynamic compiler\n");
290
291  // If the user doesn't want core files, disable them.
292  if (DisableCoreFiles)
293    sys::Process::PreventCoreFiles();
294
295  // Load the bitcode...
296  SMDiagnostic Err;
297  Module *Mod = ParseIRFile(InputFile, Err, Context);
298  if (!Mod) {
299    Err.print(argv[0], errs());
300    return 1;
301  }
302
303  // If not jitting lazily, load the whole bitcode file eagerly too.
304  std::string ErrorMsg;
305  if (NoLazyCompilation) {
306    if (Mod->MaterializeAllPermanently(&ErrorMsg)) {
307      errs() << argv[0] << ": bitcode didn't read correctly.\n";
308      errs() << "Reason: " << ErrorMsg << "\n";
309      exit(1);
310    }
311  }
312
313  if (DebugIR) {
314    if (!UseMCJIT) {
315      errs() << "warning: -debug-ir used without -use-mcjit. Only partial debug"
316        << " information will be emitted by the non-MC JIT engine. To see full"
317        << " source debug information, enable the flag '-use-mcjit'.\n";
318
319    }
320    ModulePass *DebugIRPass = createDebugIRPass();
321    DebugIRPass->runOnModule(*Mod);
322  }
323
324  EngineBuilder builder(Mod);
325  builder.setMArch(MArch);
326  builder.setMCPU(MCPU);
327  builder.setMAttrs(MAttrs);
328  builder.setRelocationModel(RelocModel);
329  builder.setCodeModel(CMModel);
330  builder.setErrorStr(&ErrorMsg);
331  builder.setEngineKind(ForceInterpreter
332                        ? EngineKind::Interpreter
333                        : EngineKind::JIT);
334
335  // If we are supposed to override the target triple, do so now.
336  if (!TargetTriple.empty())
337    Mod->setTargetTriple(Triple::normalize(TargetTriple));
338
339  // Enable MCJIT if desired.
340  RTDyldMemoryManager *RTDyldMM = 0;
341  if (UseMCJIT && !ForceInterpreter) {
342    builder.setUseMCJIT(true);
343    if (RemoteMCJIT)
344      RTDyldMM = new RemoteMemoryManager();
345    else
346      RTDyldMM = new SectionMemoryManager();
347    builder.setMCJITMemoryManager(RTDyldMM);
348  } else {
349    if (RemoteMCJIT) {
350      errs() << "error: Remote process execution requires -use-mcjit\n";
351      exit(1);
352    }
353    builder.setJITMemoryManager(ForceInterpreter ? 0 :
354                                JITMemoryManager::CreateDefaultMemManager());
355  }
356
357  CodeGenOpt::Level OLvl = CodeGenOpt::Default;
358  switch (OptLevel) {
359  default:
360    errs() << argv[0] << ": invalid optimization level.\n";
361    return 1;
362  case ' ': break;
363  case '0': OLvl = CodeGenOpt::None; break;
364  case '1': OLvl = CodeGenOpt::Less; break;
365  case '2': OLvl = CodeGenOpt::Default; break;
366  case '3': OLvl = CodeGenOpt::Aggressive; break;
367  }
368  builder.setOptLevel(OLvl);
369
370  TargetOptions Options;
371  Options.UseSoftFloat = GenerateSoftFloatCalls;
372  if (FloatABIForCalls != FloatABI::Default)
373    Options.FloatABIType = FloatABIForCalls;
374  if (GenerateSoftFloatCalls)
375    FloatABIForCalls = FloatABI::Soft;
376
377  // Remote target execution doesn't handle EH or debug registration.
378  if (!RemoteMCJIT) {
379    Options.JITEmitDebugInfo = EmitJitDebugInfo;
380    Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk;
381  }
382
383  builder.setTargetOptions(Options);
384
385  EE = builder.create();
386  if (!EE) {
387    if (!ErrorMsg.empty())
388      errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n";
389    else
390      errs() << argv[0] << ": unknown error creating EE!\n";
391    exit(1);
392  }
393
394  // Load any additional modules specified on the command line.
395  for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) {
396    Module *XMod = ParseIRFile(ExtraModules[i], Err, Context);
397    if (!XMod) {
398      Err.print(argv[0], errs());
399      return 1;
400    }
401    EE->addModule(XMod);
402  }
403
404  // If the target is Cygwin/MingW and we are generating remote code, we
405  // need an extra module to help out with linking.
406  if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) {
407    addCygMingExtraModule(EE, Context, Mod->getTargetTriple());
408  }
409
410  // The following functions have no effect if their respective profiling
411  // support wasn't enabled in the build configuration.
412  EE->RegisterJITEventListener(
413                JITEventListener::createOProfileJITEventListener());
414  EE->RegisterJITEventListener(
415                JITEventListener::createIntelJITEventListener());
416
417  if (!NoLazyCompilation && RemoteMCJIT) {
418    errs() << "warning: remote mcjit does not support lazy compilation\n";
419    NoLazyCompilation = true;
420  }
421  EE->DisableLazyCompilation(NoLazyCompilation);
422
423  // If the user specifically requested an argv[0] to pass into the program,
424  // do it now.
425  if (!FakeArgv0.empty()) {
426    InputFile = FakeArgv0;
427  } else {
428    // Otherwise, if there is a .bc suffix on the executable strip it off, it
429    // might confuse the program.
430    if (StringRef(InputFile).endswith(".bc"))
431      InputFile.erase(InputFile.length() - 3);
432  }
433
434  // Add the module's name to the start of the vector of arguments to main().
435  InputArgv.insert(InputArgv.begin(), InputFile);
436
437  // Call the main function from M as if its signature were:
438  //   int main (int argc, char **argv, const char **envp)
439  // using the contents of Args to determine argc & argv, and the contents of
440  // EnvVars to determine envp.
441  //
442  Function *EntryFn = Mod->getFunction(EntryFunc);
443  if (!EntryFn) {
444    errs() << '\'' << EntryFunc << "\' function not found in module.\n";
445    return -1;
446  }
447
448  // Reset errno to zero on entry to main.
449  errno = 0;
450
451  int Result;
452
453  if (!RemoteMCJIT) {
454    // If the program doesn't explicitly call exit, we will need the Exit
455    // function later on to make an explicit call, so get the function now.
456    Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context),
457                                                      Type::getInt32Ty(Context),
458                                                      NULL);
459
460    // Run static constructors.
461    if (UseMCJIT && !ForceInterpreter) {
462      // Give MCJIT a chance to apply relocations and set page permissions.
463      EE->finalizeObject();
464    }
465    EE->runStaticConstructorsDestructors(false);
466
467    if (!UseMCJIT && NoLazyCompilation) {
468      for (Module::iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) {
469        Function *Fn = &*I;
470        if (Fn != EntryFn && !Fn->isDeclaration())
471          EE->getPointerToFunction(Fn);
472      }
473    }
474
475    // Trigger compilation separately so code regions that need to be
476    // invalidated will be known.
477    (void)EE->getPointerToFunction(EntryFn);
478    // Clear instruction cache before code will be executed.
479    if (RTDyldMM)
480      static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache();
481
482    // Run main.
483    Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp);
484
485    // Run static destructors.
486    EE->runStaticConstructorsDestructors(true);
487
488    // If the program didn't call exit explicitly, we should call it now.
489    // This ensures that any atexit handlers get called correctly.
490    if (Function *ExitF = dyn_cast<Function>(Exit)) {
491      std::vector<GenericValue> Args;
492      GenericValue ResultGV;
493      ResultGV.IntVal = APInt(32, Result);
494      Args.push_back(ResultGV);
495      EE->runFunction(ExitF, Args);
496      errs() << "ERROR: exit(" << Result << ") returned!\n";
497      abort();
498    } else {
499      errs() << "ERROR: exit defined with wrong prototype!\n";
500      abort();
501    }
502  } else {
503    // else == "if (RemoteMCJIT)"
504
505    // Remote target MCJIT doesn't (yet) support static constructors. No reason
506    // it couldn't. This is a limitation of the LLI implemantation, not the
507    // MCJIT itself. FIXME.
508    //
509    RemoteMemoryManager *MM = static_cast<RemoteMemoryManager*>(RTDyldMM);
510    // Everything is prepared now, so lay out our program for the target
511    // address space, assign the section addresses to resolve any relocations,
512    // and send it to the target.
513
514    OwningPtr<RemoteTarget> Target;
515    if (!MCJITRemoteProcess.empty()) { // Remote execution on a child process
516      if (!RemoteTarget::hostSupportsExternalRemoteTarget()) {
517        errs() << "Warning: host does not support external remote targets.\n"
518               << "  Defaulting to simulated remote execution\n";
519        Target.reset(RemoteTarget::createRemoteTarget());
520      } else {
521        std::string ChildEXE = sys::FindProgramByName(MCJITRemoteProcess);
522        if (ChildEXE == "") {
523          errs() << "Unable to find child target: '\''" << MCJITRemoteProcess << "\'\n";
524          return -1;
525        }
526        Target.reset(RemoteTarget::createExternalRemoteTarget(ChildEXE));
527      }
528    } else {
529      // No child process name provided, use simulated remote execution.
530      Target.reset(RemoteTarget::createRemoteTarget());
531    }
532
533    // Give the memory manager a pointer to our remote target interface object.
534    MM->setRemoteTarget(Target.get());
535
536    // Create the remote target.
537    Target->create();
538
539    // Since we're executing in a (at least simulated) remote address space,
540    // we can't use the ExecutionEngine::runFunctionAsMain(). We have to
541    // grab the function address directly here and tell the remote target
542    // to execute the function.
543    //
544    // Our memory manager will map generated code into the remote address
545    // space as it is loaded and copy the bits over during the finalizeMemory
546    // operation.
547    //
548    // FIXME: argv and envp handling.
549    uint64_t Entry = EE->getFunctionAddress(EntryFn->getName().str());
550
551    DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x"
552                 << format("%llx", Entry) << "\n");
553
554    if (Target->executeCode(Entry, Result))
555      errs() << "ERROR: " << Target->getErrorMsg() << "\n";
556
557    // Like static constructors, the remote target MCJIT support doesn't handle
558    // this yet. It could. FIXME.
559
560    // Stop the remote target
561    Target->stop();
562  }
563
564  return Result;
565}
566