1193323Sed//===- lli.cpp - LLVM Interpreter / Dynamic compiler ----------------------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This utility provides a simple wrapper around the LLVM Execution Engines, 11193323Sed// which allow the direct execution of LLVM programs through a Just-In-Time 12195340Sed// compiler, or through an interpreter if no JIT is available for this platform. 13193323Sed// 14193323Sed//===----------------------------------------------------------------------===// 15193323Sed 16245431Sdim#define DEBUG_TYPE "lli" 17252723Sdim#include "llvm/IR/LLVMContext.h" 18263509Sdim#include "RemoteMemoryManager.h" 19245431Sdim#include "RemoteTarget.h" 20212793Sdim#include "llvm/ADT/Triple.h" 21193323Sed#include "llvm/Bitcode/ReaderWriter.h" 22193323Sed#include "llvm/CodeGen/LinkAllCodegenComponents.h" 23195098Sed#include "llvm/ExecutionEngine/GenericValue.h" 24195098Sed#include "llvm/ExecutionEngine/Interpreter.h" 25193323Sed#include "llvm/ExecutionEngine/JIT.h" 26195098Sed#include "llvm/ExecutionEngine/JITEventListener.h" 27235633Sdim#include "llvm/ExecutionEngine/JITMemoryManager.h" 28218885Sdim#include "llvm/ExecutionEngine/MCJIT.h" 29252723Sdim#include "llvm/ExecutionEngine/SectionMemoryManager.h" 30263509Sdim#include "llvm/IR/IRBuilder.h" 31252723Sdim#include "llvm/IR/Module.h" 32252723Sdim#include "llvm/IR/Type.h" 33263509Sdim#include "llvm/IR/TypeBuilder.h" 34252723Sdim#include "llvm/IRReader/IRReader.h" 35193323Sed#include "llvm/Support/CommandLine.h" 36252723Sdim#include "llvm/Support/Debug.h" 37252723Sdim#include "llvm/Support/DynamicLibrary.h" 38252723Sdim#include "llvm/Support/Format.h" 39193323Sed#include "llvm/Support/ManagedStatic.h" 40252723Sdim#include "llvm/Support/MathExtras.h" 41252723Sdim#include "llvm/Support/Memory.h" 42193323Sed#include "llvm/Support/MemoryBuffer.h" 43193323Sed#include "llvm/Support/PluginLoader.h" 44193323Sed#include "llvm/Support/PrettyStackTrace.h" 45218885Sdim#include "llvm/Support/Process.h" 46263509Sdim#include "llvm/Support/Program.h" 47218885Sdim#include "llvm/Support/Signals.h" 48252723Sdim#include "llvm/Support/SourceMgr.h" 49226584Sdim#include "llvm/Support/TargetSelect.h" 50252723Sdim#include "llvm/Support/raw_ostream.h" 51263509Sdim#include "llvm/Transforms/Instrumentation.h" 52193323Sed#include <cerrno> 53218885Sdim 54218885Sdim#ifdef __CYGWIN__ 55218885Sdim#include <cygwin/version.h> 56218885Sdim#if defined(CYGWIN_VERSION_DLL_MAJOR) && CYGWIN_VERSION_DLL_MAJOR<1007 57218885Sdim#define DO_NOTHING_ATEXIT 1 58218885Sdim#endif 59218885Sdim#endif 60218885Sdim 61193323Sedusing namespace llvm; 62193323Sed 63193323Sednamespace { 64193323Sed cl::opt<std::string> 65193323Sed InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-")); 66193323Sed 67193323Sed cl::list<std::string> 68193323Sed InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>...")); 69193323Sed 70193323Sed cl::opt<bool> ForceInterpreter("force-interpreter", 71193323Sed cl::desc("Force interpretation: disable JIT"), 72193323Sed cl::init(false)); 73193323Sed 74218885Sdim cl::opt<bool> UseMCJIT( 75218885Sdim "use-mcjit", cl::desc("Enable use of the MC-based JIT (if available)"), 76218885Sdim cl::init(false)); 77218885Sdim 78263509Sdim cl::opt<bool> DebugIR( 79263509Sdim "debug-ir", cl::desc("Generate debug information to allow debugging IR."), 80263509Sdim cl::init(false)); 81263509Sdim 82245431Sdim // The MCJIT supports building for a target address space separate from 83245431Sdim // the JIT compilation process. Use a forked process and a copying 84245431Sdim // memory manager with IPC to execute using this functionality. 85245431Sdim cl::opt<bool> RemoteMCJIT("remote-mcjit", 86245431Sdim cl::desc("Execute MCJIT'ed code in a separate process."), 87245431Sdim cl::init(false)); 88245431Sdim 89263509Sdim // Manually specify the child process for remote execution. This overrides 90263509Sdim // the simulated remote execution that allocates address space for child 91263509Sdim // execution. The child process will be executed and will communicate with 92263509Sdim // lli via stdin/stdout pipes. 93263509Sdim cl::opt<std::string> 94263509Sdim MCJITRemoteProcess("mcjit-remote-process", 95263509Sdim cl::desc("Specify the filename of the process to launch " 96263509Sdim "for remote MCJIT execution. If none is specified," 97263509Sdim "\n\tremote execution will be simulated in-process."), 98263509Sdim cl::value_desc("filename"), 99263509Sdim cl::init("")); 100263509Sdim 101193323Sed // Determine optimization level. 102193323Sed cl::opt<char> 103193323Sed OptLevel("O", 104193323Sed cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " 105193323Sed "(default = '-O2')"), 106193323Sed cl::Prefix, 107193323Sed cl::ZeroOrMore, 108193323Sed cl::init(' ')); 109193323Sed 110193323Sed cl::opt<std::string> 111193323Sed TargetTriple("mtriple", cl::desc("Override target triple for module")); 112193323Sed 113193323Sed cl::opt<std::string> 114203954Srdivacky MArch("march", 115203954Srdivacky cl::desc("Architecture to generate assembly for (see --version)")); 116203954Srdivacky 117203954Srdivacky cl::opt<std::string> 118203954Srdivacky MCPU("mcpu", 119203954Srdivacky cl::desc("Target a specific cpu type (-mcpu=help for details)"), 120203954Srdivacky cl::value_desc("cpu-name"), 121203954Srdivacky cl::init("")); 122203954Srdivacky 123203954Srdivacky cl::list<std::string> 124203954Srdivacky MAttrs("mattr", 125203954Srdivacky cl::CommaSeparated, 126203954Srdivacky cl::desc("Target specific attributes (-mattr=help for details)"), 127203954Srdivacky cl::value_desc("a1,+a2,-a3,...")); 128203954Srdivacky 129203954Srdivacky cl::opt<std::string> 130193323Sed EntryFunc("entry-function", 131193323Sed cl::desc("Specify the entry function (default = 'main') " 132193323Sed "of the executable"), 133193323Sed cl::value_desc("function"), 134193323Sed cl::init("main")); 135235633Sdim 136263509Sdim cl::list<std::string> 137263509Sdim ExtraModules("extra-module", 138263509Sdim cl::desc("Extra modules to be loaded"), 139263509Sdim cl::value_desc("input bitcode")); 140263509Sdim 141193323Sed cl::opt<std::string> 142193323Sed FakeArgv0("fake-argv0", 143193323Sed cl::desc("Override the 'argv[0]' value passed into the executing" 144193323Sed " program"), cl::value_desc("executable")); 145235633Sdim 146193323Sed cl::opt<bool> 147193323Sed DisableCoreFiles("disable-core-files", cl::Hidden, 148193323Sed cl::desc("Disable emission of core files if possible")); 149193323Sed 150193323Sed cl::opt<bool> 151193323Sed NoLazyCompilation("disable-lazy-compilation", 152193323Sed cl::desc("Disable JIT lazy compilation"), 153193323Sed cl::init(false)); 154226584Sdim 155226584Sdim cl::opt<Reloc::Model> 156226584Sdim RelocModel("relocation-model", 157226584Sdim cl::desc("Choose relocation model"), 158226584Sdim cl::init(Reloc::Default), 159226584Sdim cl::values( 160226584Sdim clEnumValN(Reloc::Default, "default", 161226584Sdim "Target default relocation model"), 162226584Sdim clEnumValN(Reloc::Static, "static", 163226584Sdim "Non-relocatable code"), 164226584Sdim clEnumValN(Reloc::PIC_, "pic", 165226584Sdim "Fully relocatable, position independent code"), 166226584Sdim clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", 167226584Sdim "Relocatable external references, non-relocatable code"), 168226584Sdim clEnumValEnd)); 169226584Sdim 170226584Sdim cl::opt<llvm::CodeModel::Model> 171226584Sdim CMModel("code-model", 172226584Sdim cl::desc("Choose code model"), 173226584Sdim cl::init(CodeModel::JITDefault), 174226584Sdim cl::values(clEnumValN(CodeModel::JITDefault, "default", 175226584Sdim "Target default JIT code model"), 176226584Sdim clEnumValN(CodeModel::Small, "small", 177226584Sdim "Small code model"), 178226584Sdim clEnumValN(CodeModel::Kernel, "kernel", 179226584Sdim "Kernel code model"), 180226584Sdim clEnumValN(CodeModel::Medium, "medium", 181226584Sdim "Medium code model"), 182226584Sdim clEnumValN(CodeModel::Large, "large", 183226584Sdim "Large code model"), 184226584Sdim clEnumValEnd)); 185226584Sdim 186235633Sdim cl::opt<bool> 187245431Sdim GenerateSoftFloatCalls("soft-float", 188245431Sdim cl::desc("Generate software floating point library calls"), 189245431Sdim cl::init(false)); 190245431Sdim 191245431Sdim cl::opt<llvm::FloatABI::ABIType> 192245431Sdim FloatABIForCalls("float-abi", 193245431Sdim cl::desc("Choose float ABI type"), 194245431Sdim cl::init(FloatABI::Default), 195245431Sdim cl::values( 196245431Sdim clEnumValN(FloatABI::Default, "default", 197245431Sdim "Target default float ABI type"), 198245431Sdim clEnumValN(FloatABI::Soft, "soft", 199245431Sdim "Soft float ABI (implied by -soft-float)"), 200245431Sdim clEnumValN(FloatABI::Hard, "hard", 201245431Sdim "Hard float ABI (uses FP registers)"), 202245431Sdim clEnumValEnd)); 203245431Sdim cl::opt<bool> 204235633Sdim// In debug builds, make this default to true. 205235633Sdim#ifdef NDEBUG 206235633Sdim#define EMIT_DEBUG false 207235633Sdim#else 208235633Sdim#define EMIT_DEBUG true 209235633Sdim#endif 210235633Sdim EmitJitDebugInfo("jit-emit-debug", 211235633Sdim cl::desc("Emit debug information to debugger"), 212235633Sdim cl::init(EMIT_DEBUG)); 213235633Sdim#undef EMIT_DEBUG 214235633Sdim 215235633Sdim static cl::opt<bool> 216235633Sdim EmitJitDebugInfoToDisk("jit-emit-debug-to-disk", 217235633Sdim cl::Hidden, 218235633Sdim cl::desc("Emit debug info objfiles to disk"), 219235633Sdim cl::init(false)); 220193323Sed} 221193323Sed 222193323Sedstatic ExecutionEngine *EE = 0; 223193323Sed 224193323Sedstatic void do_shutdown() { 225218885Sdim // Cygwin-1.5 invokes DLL's dtors before atexit handler. 226218885Sdim#ifndef DO_NOTHING_ATEXIT 227193323Sed delete EE; 228193323Sed llvm_shutdown(); 229218885Sdim#endif 230193323Sed} 231193323Sed 232263509Sdim// On Mingw and Cygwin, an external symbol named '__main' is called from the 233263509Sdim// generated 'main' function to allow static intialization. To avoid linking 234263509Sdim// problems with remote targets (because lli's remote target support does not 235263509Sdim// currently handle external linking) we add a secondary module which defines 236263509Sdim// an empty '__main' function. 237263509Sdimstatic void addCygMingExtraModule(ExecutionEngine *EE, 238263509Sdim LLVMContext &Context, 239263509Sdim StringRef TargetTripleStr) { 240263509Sdim IRBuilder<> Builder(Context); 241263509Sdim Triple TargetTriple(TargetTripleStr); 242245431Sdim 243263509Sdim // Create a new module. 244263509Sdim Module *M = new Module("CygMingHelper", Context); 245263509Sdim M->setTargetTriple(TargetTripleStr); 246245431Sdim 247263509Sdim // Create an empty function named "__main". 248263509Sdim Function *Result; 249263509Sdim if (TargetTriple.isArch64Bit()) { 250263509Sdim Result = Function::Create( 251263509Sdim TypeBuilder<int64_t(void), false>::get(Context), 252263509Sdim GlobalValue::ExternalLinkage, "__main", M); 253263509Sdim } else { 254263509Sdim Result = Function::Create( 255263509Sdim TypeBuilder<int32_t(void), false>::get(Context), 256263509Sdim GlobalValue::ExternalLinkage, "__main", M); 257245431Sdim } 258263509Sdim BasicBlock *BB = BasicBlock::Create(Context, "__main", Result); 259263509Sdim Builder.SetInsertPoint(BB); 260263509Sdim Value *ReturnVal; 261263509Sdim if (TargetTriple.isArch64Bit()) 262263509Sdim ReturnVal = ConstantInt::get(Context, APInt(64, 0)); 263263509Sdim else 264263509Sdim ReturnVal = ConstantInt::get(Context, APInt(32, 0)); 265263509Sdim Builder.CreateRet(ReturnVal); 266245431Sdim 267263509Sdim // Add this new module to the ExecutionEngine. 268263509Sdim EE->addModule(M); 269263509Sdim} 270245431Sdim 271245431Sdim 272193323Sed//===----------------------------------------------------------------------===// 273193323Sed// main Driver function 274193323Sed// 275193323Sedint main(int argc, char **argv, char * const *envp) { 276193323Sed sys::PrintStackTraceOnErrorSignal(); 277193323Sed PrettyStackTraceProgram X(argc, argv); 278235633Sdim 279198090Srdivacky LLVMContext &Context = getGlobalContext(); 280193323Sed atexit(do_shutdown); // Call llvm_shutdown() on exit. 281198090Srdivacky 282198090Srdivacky // If we have a native target, initialize it to ensure it is linked in and 283198090Srdivacky // usable by the JIT. 284198090Srdivacky InitializeNativeTarget(); 285221337Sdim InitializeNativeTargetAsmPrinter(); 286245431Sdim InitializeNativeTargetAsmParser(); 287198090Srdivacky 288193323Sed cl::ParseCommandLineOptions(argc, argv, 289193323Sed "llvm interpreter & dynamic compiler\n"); 290193323Sed 291193323Sed // If the user doesn't want core files, disable them. 292193323Sed if (DisableCoreFiles) 293193323Sed sys::Process::PreventCoreFiles(); 294235633Sdim 295193323Sed // Load the bitcode... 296218885Sdim SMDiagnostic Err; 297218885Sdim Module *Mod = ParseIRFile(InputFile, Err, Context); 298203954Srdivacky if (!Mod) { 299235633Sdim Err.print(argv[0], errs()); 300218885Sdim return 1; 301193323Sed } 302193323Sed 303203954Srdivacky // If not jitting lazily, load the whole bitcode file eagerly too. 304218885Sdim std::string ErrorMsg; 305203954Srdivacky if (NoLazyCompilation) { 306203954Srdivacky if (Mod->MaterializeAllPermanently(&ErrorMsg)) { 307203954Srdivacky errs() << argv[0] << ": bitcode didn't read correctly.\n"; 308203954Srdivacky errs() << "Reason: " << ErrorMsg << "\n"; 309203954Srdivacky exit(1); 310203954Srdivacky } 311193323Sed } 312193323Sed 313263509Sdim if (DebugIR) { 314263509Sdim if (!UseMCJIT) { 315263509Sdim errs() << "warning: -debug-ir used without -use-mcjit. Only partial debug" 316263509Sdim << " information will be emitted by the non-MC JIT engine. To see full" 317263509Sdim << " source debug information, enable the flag '-use-mcjit'.\n"; 318263509Sdim 319263509Sdim } 320263509Sdim ModulePass *DebugIRPass = createDebugIRPass(); 321263509Sdim DebugIRPass->runOnModule(*Mod); 322263509Sdim } 323263509Sdim 324203954Srdivacky EngineBuilder builder(Mod); 325203954Srdivacky builder.setMArch(MArch); 326203954Srdivacky builder.setMCPU(MCPU); 327203954Srdivacky builder.setMAttrs(MAttrs); 328226584Sdim builder.setRelocationModel(RelocModel); 329226584Sdim builder.setCodeModel(CMModel); 330198090Srdivacky builder.setErrorStr(&ErrorMsg); 331198090Srdivacky builder.setEngineKind(ForceInterpreter 332198090Srdivacky ? EngineKind::Interpreter 333198090Srdivacky : EngineKind::JIT); 334198090Srdivacky 335193323Sed // If we are supposed to override the target triple, do so now. 336193323Sed if (!TargetTriple.empty()) 337212793Sdim Mod->setTargetTriple(Triple::normalize(TargetTriple)); 338193323Sed 339235633Sdim // Enable MCJIT if desired. 340263509Sdim RTDyldMemoryManager *RTDyldMM = 0; 341235633Sdim if (UseMCJIT && !ForceInterpreter) { 342218885Sdim builder.setUseMCJIT(true); 343245431Sdim if (RemoteMCJIT) 344263509Sdim RTDyldMM = new RemoteMemoryManager(); 345245431Sdim else 346263509Sdim RTDyldMM = new SectionMemoryManager(); 347263509Sdim builder.setMCJITMemoryManager(RTDyldMM); 348245431Sdim } else { 349245431Sdim if (RemoteMCJIT) { 350245431Sdim errs() << "error: Remote process execution requires -use-mcjit\n"; 351245431Sdim exit(1); 352245431Sdim } 353245431Sdim builder.setJITMemoryManager(ForceInterpreter ? 0 : 354245431Sdim JITMemoryManager::CreateDefaultMemManager()); 355235633Sdim } 356218885Sdim 357193323Sed CodeGenOpt::Level OLvl = CodeGenOpt::Default; 358193323Sed switch (OptLevel) { 359193323Sed default: 360198090Srdivacky errs() << argv[0] << ": invalid optimization level.\n"; 361193323Sed return 1; 362193323Sed case ' ': break; 363193323Sed case '0': OLvl = CodeGenOpt::None; break; 364198396Srdivacky case '1': OLvl = CodeGenOpt::Less; break; 365193323Sed case '2': OLvl = CodeGenOpt::Default; break; 366193323Sed case '3': OLvl = CodeGenOpt::Aggressive; break; 367193323Sed } 368198090Srdivacky builder.setOptLevel(OLvl); 369193323Sed 370235633Sdim TargetOptions Options; 371245431Sdim Options.UseSoftFloat = GenerateSoftFloatCalls; 372245431Sdim if (FloatABIForCalls != FloatABI::Default) 373245431Sdim Options.FloatABIType = FloatABIForCalls; 374245431Sdim if (GenerateSoftFloatCalls) 375245431Sdim FloatABIForCalls = FloatABI::Soft; 376245431Sdim 377245431Sdim // Remote target execution doesn't handle EH or debug registration. 378245431Sdim if (!RemoteMCJIT) { 379245431Sdim Options.JITEmitDebugInfo = EmitJitDebugInfo; 380245431Sdim Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; 381245431Sdim } 382245431Sdim 383235633Sdim builder.setTargetOptions(Options); 384235633Sdim 385198090Srdivacky EE = builder.create(); 386198090Srdivacky if (!EE) { 387198090Srdivacky if (!ErrorMsg.empty()) 388198090Srdivacky errs() << argv[0] << ": error creating EE: " << ErrorMsg << "\n"; 389198090Srdivacky else 390198090Srdivacky errs() << argv[0] << ": unknown error creating EE!\n"; 391193323Sed exit(1); 392193323Sed } 393193323Sed 394263509Sdim // Load any additional modules specified on the command line. 395263509Sdim for (unsigned i = 0, e = ExtraModules.size(); i != e; ++i) { 396263509Sdim Module *XMod = ParseIRFile(ExtraModules[i], Err, Context); 397263509Sdim if (!XMod) { 398263509Sdim Err.print(argv[0], errs()); 399263509Sdim return 1; 400263509Sdim } 401263509Sdim EE->addModule(XMod); 402263509Sdim } 403263509Sdim 404263509Sdim // If the target is Cygwin/MingW and we are generating remote code, we 405263509Sdim // need an extra module to help out with linking. 406263509Sdim if (RemoteMCJIT && Triple(Mod->getTargetTriple()).isOSCygMing()) { 407263509Sdim addCygMingExtraModule(EE, Context, Mod->getTargetTriple()); 408263509Sdim } 409263509Sdim 410235633Sdim // The following functions have no effect if their respective profiling 411235633Sdim // support wasn't enabled in the build configuration. 412235633Sdim EE->RegisterJITEventListener( 413235633Sdim JITEventListener::createOProfileJITEventListener()); 414235633Sdim EE->RegisterJITEventListener( 415235633Sdim JITEventListener::createIntelJITEventListener()); 416195098Sed 417245431Sdim if (!NoLazyCompilation && RemoteMCJIT) { 418245431Sdim errs() << "warning: remote mcjit does not support lazy compilation\n"; 419245431Sdim NoLazyCompilation = true; 420245431Sdim } 421198892Srdivacky EE->DisableLazyCompilation(NoLazyCompilation); 422193323Sed 423193323Sed // If the user specifically requested an argv[0] to pass into the program, 424193323Sed // do it now. 425193323Sed if (!FakeArgv0.empty()) { 426193323Sed InputFile = FakeArgv0; 427193323Sed } else { 428193323Sed // Otherwise, if there is a .bc suffix on the executable strip it off, it 429193323Sed // might confuse the program. 430207618Srdivacky if (StringRef(InputFile).endswith(".bc")) 431193323Sed InputFile.erase(InputFile.length() - 3); 432193323Sed } 433193323Sed 434193323Sed // Add the module's name to the start of the vector of arguments to main(). 435193323Sed InputArgv.insert(InputArgv.begin(), InputFile); 436193323Sed 437193323Sed // Call the main function from M as if its signature were: 438193323Sed // int main (int argc, char **argv, const char **envp) 439193323Sed // using the contents of Args to determine argc & argv, and the contents of 440193323Sed // EnvVars to determine envp. 441193323Sed // 442193323Sed Function *EntryFn = Mod->getFunction(EntryFunc); 443193323Sed if (!EntryFn) { 444198090Srdivacky errs() << '\'' << EntryFunc << "\' function not found in module.\n"; 445193323Sed return -1; 446193323Sed } 447193323Sed 448193323Sed // Reset errno to zero on entry to main. 449193323Sed errno = 0; 450235633Sdim 451263509Sdim int Result; 452263509Sdim 453252723Sdim if (!RemoteMCJIT) { 454263509Sdim // If the program doesn't explicitly call exit, we will need the Exit 455263509Sdim // function later on to make an explicit call, so get the function now. 456263509Sdim Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context), 457263509Sdim Type::getInt32Ty(Context), 458263509Sdim NULL); 459193323Sed 460263509Sdim // Run static constructors. 461263509Sdim if (UseMCJIT && !ForceInterpreter) { 462263509Sdim // Give MCJIT a chance to apply relocations and set page permissions. 463263509Sdim EE->finalizeObject(); 464193323Sed } 465263509Sdim EE->runStaticConstructorsDestructors(false); 466193323Sed 467263509Sdim if (!UseMCJIT && NoLazyCompilation) { 468263509Sdim for (Module::iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) { 469263509Sdim Function *Fn = &*I; 470263509Sdim if (Fn != EntryFn && !Fn->isDeclaration()) 471263509Sdim EE->getPointerToFunction(Fn); 472263509Sdim } 473263509Sdim } 474193323Sed 475245431Sdim // Trigger compilation separately so code regions that need to be 476245431Sdim // invalidated will be known. 477245431Sdim (void)EE->getPointerToFunction(EntryFn); 478245431Sdim // Clear instruction cache before code will be executed. 479263509Sdim if (RTDyldMM) 480263509Sdim static_cast<SectionMemoryManager*>(RTDyldMM)->invalidateInstructionCache(); 481245431Sdim 482245431Sdim // Run main. 483245431Sdim Result = EE->runFunctionAsMain(EntryFn, InputArgv, envp); 484245431Sdim 485245431Sdim // Run static destructors. 486245431Sdim EE->runStaticConstructorsDestructors(true); 487245431Sdim 488245431Sdim // If the program didn't call exit explicitly, we should call it now. 489245431Sdim // This ensures that any atexit handlers get called correctly. 490245431Sdim if (Function *ExitF = dyn_cast<Function>(Exit)) { 491245431Sdim std::vector<GenericValue> Args; 492245431Sdim GenericValue ResultGV; 493245431Sdim ResultGV.IntVal = APInt(32, Result); 494245431Sdim Args.push_back(ResultGV); 495245431Sdim EE->runFunction(ExitF, Args); 496245431Sdim errs() << "ERROR: exit(" << Result << ") returned!\n"; 497245431Sdim abort(); 498245431Sdim } else { 499245431Sdim errs() << "ERROR: exit defined with wrong prototype!\n"; 500245431Sdim abort(); 501245431Sdim } 502263509Sdim } else { 503263509Sdim // else == "if (RemoteMCJIT)" 504263509Sdim 505263509Sdim // Remote target MCJIT doesn't (yet) support static constructors. No reason 506263509Sdim // it couldn't. This is a limitation of the LLI implemantation, not the 507263509Sdim // MCJIT itself. FIXME. 508263509Sdim // 509263509Sdim RemoteMemoryManager *MM = static_cast<RemoteMemoryManager*>(RTDyldMM); 510263509Sdim // Everything is prepared now, so lay out our program for the target 511263509Sdim // address space, assign the section addresses to resolve any relocations, 512263509Sdim // and send it to the target. 513263509Sdim 514263509Sdim OwningPtr<RemoteTarget> Target; 515263509Sdim if (!MCJITRemoteProcess.empty()) { // Remote execution on a child process 516263509Sdim if (!RemoteTarget::hostSupportsExternalRemoteTarget()) { 517263509Sdim errs() << "Warning: host does not support external remote targets.\n" 518263509Sdim << " Defaulting to simulated remote execution\n"; 519263509Sdim Target.reset(RemoteTarget::createRemoteTarget()); 520263509Sdim } else { 521263509Sdim std::string ChildEXE = sys::FindProgramByName(MCJITRemoteProcess); 522263509Sdim if (ChildEXE == "") { 523263509Sdim errs() << "Unable to find child target: '\''" << MCJITRemoteProcess << "\'\n"; 524263509Sdim return -1; 525263509Sdim } 526263509Sdim Target.reset(RemoteTarget::createExternalRemoteTarget(ChildEXE)); 527263509Sdim } 528263509Sdim } else { 529263509Sdim // No child process name provided, use simulated remote execution. 530263509Sdim Target.reset(RemoteTarget::createRemoteTarget()); 531263509Sdim } 532263509Sdim 533263509Sdim // Give the memory manager a pointer to our remote target interface object. 534263509Sdim MM->setRemoteTarget(Target.get()); 535263509Sdim 536263509Sdim // Create the remote target. 537263509Sdim Target->create(); 538263509Sdim 539263509Sdim // Since we're executing in a (at least simulated) remote address space, 540263509Sdim // we can't use the ExecutionEngine::runFunctionAsMain(). We have to 541263509Sdim // grab the function address directly here and tell the remote target 542263509Sdim // to execute the function. 543263509Sdim // 544263509Sdim // Our memory manager will map generated code into the remote address 545263509Sdim // space as it is loaded and copy the bits over during the finalizeMemory 546263509Sdim // operation. 547263509Sdim // 548263509Sdim // FIXME: argv and envp handling. 549263509Sdim uint64_t Entry = EE->getFunctionAddress(EntryFn->getName().str()); 550263509Sdim 551263509Sdim DEBUG(dbgs() << "Executing '" << EntryFn->getName() << "' at 0x" 552263509Sdim << format("%llx", Entry) << "\n"); 553263509Sdim 554263509Sdim if (Target->executeCode(Entry, Result)) 555263509Sdim errs() << "ERROR: " << Target->getErrorMsg() << "\n"; 556263509Sdim 557263509Sdim // Like static constructors, the remote target MCJIT support doesn't handle 558263509Sdim // this yet. It could. FIXME. 559263509Sdim 560263509Sdim // Stop the remote target 561263509Sdim Target->stop(); 562245431Sdim } 563263509Sdim 564245431Sdim return Result; 565193323Sed} 566