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