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