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