1//===- llvm-jitlink.cpp -- Command line interface/tester for llvm-jitlink -===//
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 command line interface to the llvm jitlink
10// library, which makes relocatable object files executable in memory. Its
11// primary function is as a testing utility for the jitlink library.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-jitlink.h"
16#include "llvm/BinaryFormat/Magic.h"
17#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX, LLVM_ENABLE_THREADS
18#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
19#include "llvm/ExecutionEngine/Orc/BacktraceTools.h"
20#include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
21#include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h"
22#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
23#include "llvm/ExecutionEngine/Orc/Debugging/ELFDebugObjectPlugin.h"
24#include "llvm/ExecutionEngine/Orc/Debugging/PerfSupportPlugin.h"
25#include "llvm/ExecutionEngine/Orc/Debugging/VTuneSupportPlugin.h"
26#include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
27#include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
28#include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
29#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
30#include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
31#include "llvm/ExecutionEngine/Orc/JITLinkRedirectableSymbolManager.h"
32#include "llvm/ExecutionEngine/Orc/JITLinkReentryTrampolines.h"
33#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
34#include "llvm/ExecutionEngine/Orc/LoadLinkableFile.h"
35#include "llvm/ExecutionEngine/Orc/MachO.h"
36#include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
37#include "llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h"
38#include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
39#include "llvm/ExecutionEngine/Orc/SectCreate.h"
40#include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h"
41#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
42#include "llvm/ExecutionEngine/Orc/SimpleRemoteMemoryMapper.h"
43#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
44#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderPerf.h"
45#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderVTune.h"
46#include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
47#include "llvm/ExecutionEngine/Orc/UnwindInfoRegistrationPlugin.h"
48#include "llvm/MC/MCAsmInfo.h"
49#include "llvm/MC/MCContext.h"
50#include "llvm/MC/MCDisassembler/MCDisassembler.h"
51#include "llvm/MC/MCInstPrinter.h"
52#include "llvm/MC/MCInstrAnalysis.h"
53#include "llvm/MC/MCInstrInfo.h"
54#include "llvm/MC/MCRegisterInfo.h"
55#include "llvm/MC/MCSubtargetInfo.h"
56#include "llvm/MC/MCTargetOptions.h"
57#include "llvm/MC/TargetRegistry.h"
58#include "llvm/Object/COFF.h"
59#include "llvm/Object/MachO.h"
60#include "llvm/Object/ObjectFile.h"
61#include "llvm/Object/TapiUniversal.h"
62#include "llvm/Support/CommandLine.h"
63#include "llvm/Support/Debug.h"
64#include "llvm/Support/InitLLVM.h"
65#include "llvm/Support/MemoryBuffer.h"
66#include "llvm/Support/Path.h"
67#include "llvm/Support/Process.h"
68#include "llvm/Support/TargetSelect.h"
69#include "llvm/Support/Timer.h"
70#include <chrono>
71#include <cstring>
72#include <deque>
73#include <string>
74
75#ifdef LLVM_ON_UNIX
76#include <netdb.h>
77#include <netinet/in.h>
78#include <sys/socket.h>
79#include <unistd.h>
80#endif // LLVM_ON_UNIX
81
82#define DEBUG_TYPE "llvm_jitlink"
83
84using namespace llvm;
85using namespace llvm::jitlink;
86using namespace llvm::orc;
87
88static cl::OptionCategory JITLinkCategory("JITLink Options");
89
90static cl::list<std::string> InputFiles(cl::Positional, cl::desc("input files"),
91 cl::cat(JITLinkCategory));
92
93static cl::list<bool> LazyLink("lazy",
94 cl::desc("Link the following file lazily"),
95 cl::cat(JITLinkCategory));
96
97enum class SpeculateKind { None, Simple };
98
99static cl::opt<SpeculateKind> Speculate(
100 "speculate", cl::desc("Choose speculation scheme"),
101 cl::init(Val: SpeculateKind::None),
102 cl::values(clEnumValN(SpeculateKind::None, "none", "No speculation"),
103 clEnumValN(SpeculateKind::Simple, "simple",
104 "Simple speculation")),
105 cl::cat(JITLinkCategory));
106
107static cl::opt<std::string> SpeculateOrder(
108 "speculate-order",
109 cl::desc("A CSV file containing (JITDylib, Function) pairs to"
110 "speculatively look up"),
111 cl::cat(JITLinkCategory));
112
113static cl::opt<std::string> RecordLazyExecs(
114 "record-lazy-execs",
115 cl::desc("Write lazy-function executions to a CSV file as (JITDylib, "
116 "function) pairs"),
117 cl::cat(JITLinkCategory));
118
119static cl::opt<size_t> MaterializationThreads(
120 "num-threads", cl::desc("Number of materialization threads to use"),
121 cl::init(Val: std::numeric_limits<size_t>::max()), cl::cat(JITLinkCategory));
122
123static cl::list<std::string>
124 LibrarySearchPaths("L",
125 cl::desc("Add dir to the list of library search paths"),
126 cl::Prefix, cl::cat(JITLinkCategory));
127
128static cl::list<std::string>
129 Libraries("l",
130 cl::desc("Link against library X in the library search paths"),
131 cl::Prefix, cl::cat(JITLinkCategory));
132
133static cl::list<std::string>
134 LibrariesHidden("hidden-l",
135 cl::desc("Link against library X in the library search "
136 "paths with hidden visibility"),
137 cl::Prefix, cl::cat(JITLinkCategory));
138
139static cl::list<std::string>
140 LoadHidden("load_hidden",
141 cl::desc("Link against library X with hidden visibility"),
142 cl::cat(JITLinkCategory));
143
144static cl::opt<std::string>
145 WriteSymbolTableTo("write-symtab",
146 cl::desc("Write the symbol table for the JIT'd program "
147 "to the specified file"),
148 cl::cat(JITLinkCategory));
149
150static cl::opt<std::string> SymbolicateWith(
151 "symbolicate-with",
152 cl::desc("Given a path to a symbol table file, symbolicate the given "
153 "backtrace(s)"),
154 cl::cat(JITLinkCategory));
155
156static cl::list<std::string>
157 LibrariesWeak("weak-l",
158 cl::desc("Emulate weak link against library X. Must resolve "
159 "to a TextAPI file, and all symbols in the "
160 "interface will resolve to null."),
161 cl::Prefix, cl::cat(JITLinkCategory));
162
163static cl::list<std::string> WeakLibraries(
164 "weak_library",
165 cl::desc("Emulate weak link against library X. X must point to a "
166 "TextAPI file, and all symbols in the interface will "
167 "resolve to null"),
168 cl::cat(JITLinkCategory));
169
170static cl::opt<bool> SearchSystemLibrary(
171 "search-sys-lib",
172 cl::desc("Add system library paths to library search paths"),
173 cl::init(Val: false), cl::cat(JITLinkCategory));
174
175static cl::opt<bool> NoExec("noexec", cl::desc("Do not execute loaded code"),
176 cl::init(Val: false), cl::cat(JITLinkCategory));
177
178static cl::list<std::string>
179 CheckFiles("check", cl::desc("File containing verifier checks"),
180 cl::cat(JITLinkCategory));
181
182static cl::opt<std::string>
183 CheckName("check-name", cl::desc("Name of checks to match against"),
184 cl::init(Val: "jitlink-check"), cl::cat(JITLinkCategory));
185
186static cl::opt<std::string>
187 EntryPointName("entry", cl::desc("Symbol to call as main entry point"),
188 cl::init(Val: ""), cl::cat(JITLinkCategory));
189
190static cl::list<std::string> JITDylibs(
191 "jd",
192 cl::desc("Specifies the JITDylib to be used for any subsequent "
193 "input file, -L<seacrh-path>, and -l<library> arguments"),
194 cl::cat(JITLinkCategory));
195
196static cl::list<std::string>
197 Dylibs("preload",
198 cl::desc("Pre-load dynamic libraries (e.g. language runtimes "
199 "required by the ORC runtime)"),
200 cl::cat(JITLinkCategory));
201
202static cl::list<std::string> InputArgv("args", cl::Positional,
203 cl::desc("<program arguments>..."),
204 cl::PositionalEatsArgs,
205 cl::cat(JITLinkCategory));
206
207static cl::opt<bool>
208 DebuggerSupport("debugger-support",
209 cl::desc("Enable debugger suppport (default = !-noexec)"),
210 cl::init(Val: true), cl::Hidden, cl::cat(JITLinkCategory));
211
212static cl::opt<bool> PerfSupport("perf-support",
213 cl::desc("Enable perf profiling support"),
214 cl::init(Val: false), cl::Hidden,
215 cl::cat(JITLinkCategory));
216
217static cl::opt<bool> VTuneSupport("vtune-support",
218 cl::desc("Enable vtune profiling support"),
219 cl::init(Val: false), cl::Hidden,
220 cl::cat(JITLinkCategory));
221static cl::opt<bool>
222 NoProcessSymbols("no-process-syms",
223 cl::desc("Do not resolve to llvm-jitlink process symbols"),
224 cl::init(Val: false), cl::cat(JITLinkCategory));
225
226static cl::list<std::string> AbsoluteDefs(
227 "abs",
228 cl::desc("Inject absolute symbol definitions (syntax: <name>=<addr>)"),
229 cl::cat(JITLinkCategory));
230
231static cl::list<std::string>
232 Aliases("alias",
233 cl::desc("Inject symbol aliases (syntax: <alias-name>=<aliasee>)"),
234 cl::cat(JITLinkCategory));
235
236static cl::list<std::string>
237 SectCreate("sectcreate",
238 cl::desc("given <sectname>,<filename>[@<sym>=<offset>,...] "
239 "add the content of <filename> to <sectname>"),
240 cl::cat(JITLinkCategory));
241
242static cl::list<std::string> TestHarnesses("harness", cl::Positional,
243 cl::desc("Test harness files"),
244 cl::PositionalEatsArgs,
245 cl::cat(JITLinkCategory));
246
247static cl::opt<bool>
248 ShowLinkedFiles("show-linked-files",
249 cl::desc("List each file/graph name if/when it is linked"),
250 cl::init(Val: false), cl::cat(JITLinkCategory));
251
252static cl::opt<bool> ShowInitialExecutionSessionState(
253 "show-init-es",
254 cl::desc("Print ExecutionSession state before resolving entry point"),
255 cl::init(Val: false), cl::cat(JITLinkCategory));
256
257static cl::opt<bool> ShowEntryExecutionSessionState(
258 "show-entry-es",
259 cl::desc("Print ExecutionSession state after resolving entry point"),
260 cl::init(Val: false), cl::cat(JITLinkCategory));
261
262static cl::opt<bool> ShowAddrs(
263 "show-addrs",
264 cl::desc("Print registered symbol, section, got and stub addresses"),
265 cl::init(Val: false), cl::cat(JITLinkCategory));
266
267static cl::opt<std::string> ShowLinkGraphs(
268 "show-graphs",
269 cl::desc("Takes a posix regex and prints the link graphs of all files "
270 "matching that regex after fixups have been applied"),
271 cl::Optional, cl::cat(JITLinkCategory));
272
273static cl::opt<bool> ShowTimes("show-times",
274 cl::desc("Show times for llvm-jitlink phases"),
275 cl::init(Val: false), cl::cat(JITLinkCategory));
276
277static cl::opt<std::string> SlabAllocateSizeString(
278 "slab-allocate",
279 cl::desc("Allocate from a slab of the given size "
280 "(allowable suffixes: Kb, Mb, Gb. default = "
281 "Kb)"),
282 cl::init(Val: ""), cl::cat(JITLinkCategory));
283
284static cl::opt<uint64_t> SlabAddress(
285 "slab-address",
286 cl::desc("Set slab target address (requires -slab-allocate and -noexec)"),
287 cl::init(Val: ~0ULL), cl::cat(JITLinkCategory));
288
289static cl::opt<uint64_t> SlabPageSize(
290 "slab-page-size",
291 cl::desc("Set page size for slab (requires -slab-allocate and -noexec)"),
292 cl::init(Val: 0), cl::cat(JITLinkCategory));
293
294static cl::opt<bool> ShowRelocatedSectionContents(
295 "show-relocated-section-contents",
296 cl::desc("show section contents after fixups have been applied"),
297 cl::init(Val: false), cl::cat(JITLinkCategory));
298
299static cl::opt<bool> PhonyExternals(
300 "phony-externals",
301 cl::desc("resolve all otherwise unresolved externals to null"),
302 cl::init(Val: false), cl::cat(JITLinkCategory));
303
304static cl::opt<std::string> OutOfProcessExecutor(
305 "oop-executor", cl::desc("Launch an out-of-process executor to run code"),
306 cl::ValueOptional, cl::cat(JITLinkCategory));
307
308static cl::opt<std::string> OutOfProcessExecutorConnect(
309 "oop-executor-connect",
310 cl::desc("Connect to an out-of-process executor via TCP"),
311 cl::cat(JITLinkCategory));
312
313static cl::opt<std::string>
314 OrcRuntime("orc-runtime", cl::desc("Use ORC runtime from given path"),
315 cl::init(Val: ""), cl::cat(JITLinkCategory));
316
317static cl::opt<bool> AddSelfRelocations(
318 "add-self-relocations",
319 cl::desc("Add relocations to function pointers to the current function"),
320 cl::init(Val: false), cl::cat(JITLinkCategory));
321
322static cl::opt<bool>
323 ShowErrFailedToMaterialize("show-err-failed-to-materialize",
324 cl::desc("Show FailedToMaterialize errors"),
325 cl::init(Val: false), cl::cat(JITLinkCategory));
326
327enum class MemMgr { Default, Generic, SimpleRemote, Shared };
328
329static cl::opt<MemMgr> UseMemMgr(
330 "use-memmgr", cl::desc("Choose memory manager"), cl::init(Val: MemMgr::Generic),
331 cl::values(clEnumValN(MemMgr::Default, "default",
332 "Use setup default (InProcess or EPCGeneric)"),
333 clEnumValN(MemMgr::Generic, "generic",
334 "Generic remote memory manager"),
335 clEnumValN(MemMgr::SimpleRemote, "simple-remote",
336 "Mapper memory manager with simple-remote backend"),
337 clEnumValN(MemMgr::Shared, "shared",
338 "Mapper memory manager with shared-memory manager")),
339 cl::cat(JITLinkCategory));
340
341static cl::opt<std::string>
342 OverrideTriple("triple", cl::desc("Override target triple detection"),
343 cl::init(Val: ""), cl::cat(JITLinkCategory));
344
345static cl::opt<bool> AllLoad("all_load",
346 cl::desc("Load all members of static archives"),
347 cl::init(Val: false), cl::cat(JITLinkCategory));
348
349static cl::opt<bool> ForceLoadObjC(
350 "ObjC",
351 cl::desc("Load all members of static archives that implement "
352 "Objective-C classes or categories, or Swift structs, "
353 "classes or extensions"),
354 cl::init(Val: false), cl::cat(JITLinkCategory));
355
356static cl::opt<std::string> WaitingOnGraphCapture(
357 "waiting-on-graph-capture",
358 cl::desc("Record WaitingOnGraph operations to the given file"),
359 cl::init(Val: ""), cl::cat(JITLinkCategory));
360
361static cl::opt<std::string> WaitingOnGraphReplay(
362 "waiting-on-graph-replay",
363 cl::desc("Replay WaitingOnGraph operations from the given file"),
364 cl::init(Val: ""), cl::cat(JITLinkCategory));
365
366static ExitOnError ExitOnErr;
367
368static LLVM_ATTRIBUTE_USED void linkComponents() {
369 errs() << "Linking in runtime functions\n"
370 << (void *)&llvm_orc_registerEHFrameSectionAllocAction << '\n'
371 << (void *)&llvm_orc_deregisterEHFrameSectionAllocAction << '\n'
372 << (void *)&llvm_orc_registerJITLoaderGDBAllocAction << '\n'
373 << (void *)&llvm_orc_registerJITLoaderPerfStart << '\n'
374 << (void *)&llvm_orc_registerJITLoaderPerfEnd << '\n'
375 << (void *)&llvm_orc_registerJITLoaderPerfImpl << '\n'
376 << (void *)&llvm_orc_registerVTuneImpl << '\n'
377 << (void *)&llvm_orc_unregisterVTuneImpl << '\n'
378 << (void *)&llvm_orc_test_registerVTuneImpl << '\n';
379}
380
381static bool UseTestResultOverride = false;
382static int64_t TestResultOverride = 0;
383
384extern "C" LLVM_ATTRIBUTE_USED void
385llvm_jitlink_setTestResultOverride(int64_t Value) {
386 TestResultOverride = Value;
387 UseTestResultOverride = true;
388}
389
390static Error addSelfRelocations(LinkGraph &G);
391
392namespace {
393
394template <typename ErrT>
395
396class ConditionalPrintErr {
397public:
398 ConditionalPrintErr(bool C) : C(C) {}
399 void operator()(ErrT &EI) {
400 if (C) {
401 errs() << "llvm-jitlink error: ";
402 EI.log(errs());
403 errs() << "\n";
404 }
405 }
406
407private:
408 bool C;
409};
410
411Expected<std::unique_ptr<MemoryBuffer>> getFile(const Twine &FileName) {
412 if (auto F = MemoryBuffer::getFile(Filename: FileName))
413 return std::move(*F);
414 else
415 return createFileError(F: FileName, EC: F.getError());
416}
417
418void reportLLVMJITLinkError(Error Err) {
419 handleAllErrors(
420 E: std::move(Err),
421 Handlers: ConditionalPrintErr<orc::FailedToMaterialize>(ShowErrFailedToMaterialize),
422 Handlers: ConditionalPrintErr<ErrorInfoBase>(true));
423}
424
425} // end anonymous namespace
426
427namespace llvm {
428
429static raw_ostream &
430operator<<(raw_ostream &OS, const Session::MemoryRegionInfo &MRI) {
431 OS << "target addr = " << format(Fmt: "0x%016" PRIx64, Vals: MRI.getTargetAddress());
432
433 if (MRI.isZeroFill())
434 OS << ", zero-fill: " << MRI.getZeroFillLength() << " bytes";
435 else
436 OS << ", content: " << (const void *)MRI.getContent().data() << " -- "
437 << (const void *)(MRI.getContent().data() + MRI.getContent().size())
438 << " (" << MRI.getContent().size() << " bytes)";
439
440 return OS;
441}
442
443static raw_ostream &
444operator<<(raw_ostream &OS, const Session::SymbolInfoMap &SIM) {
445 OS << "Symbols:\n";
446 for (auto &SKV : SIM)
447 OS << " \"" << SKV.first << "\" " << SKV.second << "\n";
448 return OS;
449}
450
451static raw_ostream &
452operator<<(raw_ostream &OS, const Session::FileInfo &FI) {
453 for (auto &SIKV : FI.SectionInfos)
454 OS << " Section \"" << SIKV.first() << "\": " << SIKV.second << "\n";
455 for (auto &GOTKV : FI.GOTEntryInfos)
456 OS << " GOT \"" << GOTKV.first() << "\": " << GOTKV.second << "\n";
457 for (auto &StubKVs : FI.StubInfos) {
458 OS << " Stubs \"" << StubKVs.first() << "\":";
459 for (auto MemRegion : StubKVs.second)
460 OS << " " << MemRegion;
461 OS << "\n";
462 }
463 return OS;
464}
465
466static raw_ostream &
467operator<<(raw_ostream &OS, const Session::FileInfoMap &FIM) {
468 for (auto &FIKV : FIM)
469 OS << "File \"" << FIKV.first() << "\":\n" << FIKV.second;
470 return OS;
471}
472
473bool lazyLinkingRequested() {
474 for (auto LL : LazyLink)
475 if (LL)
476 return true;
477 return false;
478}
479
480static Error applyLibraryLinkModifiers(Session &S, LinkGraph &G) {
481 // If there are hidden archives and this graph is an archive
482 // member then apply hidden modifier.
483 if (!S.HiddenArchives.empty()) {
484 StringRef ObjName(G.getName());
485 if (ObjName.ends_with(Suffix: ')')) {
486 auto LibName = ObjName.split(Separator: '[').first;
487 if (S.HiddenArchives.count(Key: LibName)) {
488 for (auto *Sym : G.defined_symbols())
489 Sym->setScope(std::max(a: Sym->getScope(), b: Scope::Hidden));
490 }
491 }
492 }
493
494 return Error::success();
495}
496
497static Error applyHarnessPromotions(Session &S, LinkGraph &G) {
498 std::lock_guard<std::mutex> Lock(S.M);
499
500 // If this graph is part of the test harness there's nothing to do.
501 if (S.HarnessFiles.empty() || S.HarnessFiles.count(Key: G.getName()))
502 return Error::success();
503
504 LLVM_DEBUG(dbgs() << "Applying promotions to graph " << G.getName() << "\n");
505
506 // If this graph is part of the test then promote any symbols referenced by
507 // the harness to default scope, remove all symbols that clash with harness
508 // definitions.
509 std::vector<Symbol *> DefinitionsToRemove;
510 for (auto *Sym : G.defined_symbols()) {
511
512 if (!Sym->hasName())
513 continue;
514
515 if (Sym->getLinkage() == Linkage::Weak) {
516 auto It = S.CanonicalWeakDefs.find(Val: *Sym->getName());
517 if (It == S.CanonicalWeakDefs.end() || It->second != G.getName()) {
518 LLVM_DEBUG({
519 dbgs() << " Externalizing weak symbol " << Sym->getName() << "\n";
520 });
521 DefinitionsToRemove.push_back(x: Sym);
522 } else {
523 LLVM_DEBUG({
524 dbgs() << " Making weak symbol " << Sym->getName() << " strong\n";
525 });
526 if (S.HarnessExternals.count(Key: *Sym->getName()))
527 Sym->setScope(Scope::Default);
528 else
529 Sym->setScope(Scope::Hidden);
530 Sym->setLinkage(Linkage::Strong);
531 }
532 } else if (S.HarnessExternals.count(Key: *Sym->getName())) {
533 LLVM_DEBUG(dbgs() << " Promoting " << Sym->getName() << "\n");
534 Sym->setScope(Scope::Default);
535 Sym->setLive(true);
536 continue;
537 } else if (S.HarnessDefinitions.count(Key: *Sym->getName())) {
538 LLVM_DEBUG(dbgs() << " Externalizing " << Sym->getName() << "\n");
539 DefinitionsToRemove.push_back(x: Sym);
540 }
541 }
542
543 for (auto *Sym : DefinitionsToRemove)
544 G.makeExternal(Sym&: *Sym);
545
546 return Error::success();
547}
548
549static void dumpSectionContents(raw_ostream &OS, Session &S, LinkGraph &G) {
550 std::lock_guard<std::mutex> Lock(S.M);
551
552 outs() << "Relocated section contents for " << G.getName() << ":\n";
553
554 constexpr orc::ExecutorAddrDiff DumpWidth = 16;
555 static_assert(isPowerOf2_64(Value: DumpWidth), "DumpWidth must be a power of two");
556
557 // Put sections in address order.
558 std::vector<Section *> Sections;
559 for (auto &S : G.sections())
560 Sections.push_back(x: &S);
561
562 llvm::sort(C&: Sections, Comp: [](const Section *LHS, const Section *RHS) {
563 if (LHS->symbols().empty() && RHS->symbols().empty())
564 return false;
565 if (LHS->symbols().empty())
566 return false;
567 if (RHS->symbols().empty())
568 return true;
569 SectionRange LHSRange(*LHS);
570 SectionRange RHSRange(*RHS);
571 return LHSRange.getStart() < RHSRange.getStart();
572 });
573
574 for (auto *S : Sections) {
575 OS << S->getName() << " content:";
576 if (S->symbols().empty()) {
577 OS << "\n section empty\n";
578 continue;
579 }
580
581 // Sort symbols into order, then render.
582 std::vector<Symbol *> Syms(S->symbols().begin(), S->symbols().end());
583 llvm::sort(C&: Syms, Comp: [](const Symbol *LHS, const Symbol *RHS) {
584 return LHS->getAddress() < RHS->getAddress();
585 });
586
587 orc::ExecutorAddr NextAddr(Syms.front()->getAddress().getValue() &
588 ~(DumpWidth - 1));
589 for (auto *Sym : Syms) {
590 bool IsZeroFill = Sym->getBlock().isZeroFill();
591 auto SymStart = Sym->getAddress();
592 auto SymSize = Sym->getSize();
593 auto SymEnd = SymStart + SymSize;
594 const uint8_t *SymData = IsZeroFill ? nullptr
595 : reinterpret_cast<const uint8_t *>(
596 Sym->getSymbolContent().data());
597
598 // Pad any space before the symbol starts.
599 while (NextAddr != SymStart) {
600 if (NextAddr % DumpWidth == 0)
601 OS << formatv(Fmt: "\n{0:x16}:", Vals&: NextAddr);
602 OS << " ";
603 ++NextAddr;
604 }
605
606 // Render the symbol content.
607 while (NextAddr != SymEnd) {
608 if (NextAddr % DumpWidth == 0)
609 OS << formatv(Fmt: "\n{0:x16}:", Vals&: NextAddr);
610 if (IsZeroFill)
611 OS << " 00";
612 else
613 OS << formatv(Fmt: " {0:x-2}", Vals: SymData[NextAddr - SymStart]);
614 ++NextAddr;
615 }
616 }
617 OS << "\n";
618 }
619}
620
621// A memory mapper with a fake offset applied only used for -noexec testing
622class InProcessDeltaMapper final : public InProcessMemoryMapper {
623public:
624 InProcessDeltaMapper(size_t PageSize, uint64_t TargetAddr)
625 : InProcessMemoryMapper(PageSize), TargetMapAddr(TargetAddr),
626 DeltaAddr(0) {}
627
628 static Expected<std::unique_ptr<InProcessDeltaMapper>> Create() {
629 size_t PageSize = SlabPageSize;
630 if (!PageSize) {
631 if (auto PageSizeOrErr = sys::Process::getPageSize())
632 PageSize = *PageSizeOrErr;
633 else
634 return PageSizeOrErr.takeError();
635 }
636
637 if (PageSize == 0)
638 return make_error<StringError>(Args: "Page size is zero",
639 Args: inconvertibleErrorCode());
640
641 return std::make_unique<InProcessDeltaMapper>(args&: PageSize, args&: SlabAddress);
642 }
643
644 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override {
645 InProcessMemoryMapper::reserve(
646 NumBytes, OnReserved: [this, OnReserved = std::move(OnReserved)](
647 Expected<ExecutorAddrRange> Result) mutable {
648 if (!Result)
649 return OnReserved(Result.takeError());
650
651 assert(DeltaAddr == 0 && "Overwriting previous offset");
652 if (TargetMapAddr != ~0ULL)
653 DeltaAddr = TargetMapAddr - Result->Start.getValue();
654 auto OffsetRange = ExecutorAddrRange(Result->Start + DeltaAddr,
655 Result->End + DeltaAddr);
656
657 OnReserved(OffsetRange);
658 });
659 }
660
661 char *prepare(jitlink::LinkGraph &G, ExecutorAddr Addr,
662 size_t ContentSize) override {
663 return InProcessMemoryMapper::prepare(G, Addr: Addr - DeltaAddr, ContentSize);
664 }
665
666 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override {
667 // Slide mapping based on delta, make all segments read-writable, and
668 // discard allocation actions.
669 auto FixedAI = std::move(AI);
670 FixedAI.MappingBase -= DeltaAddr;
671 for (auto &Seg : FixedAI.Segments)
672 Seg.AG = {MemProt::Read | MemProt::Write, Seg.AG.getMemLifetime()};
673 FixedAI.Actions.clear();
674 InProcessMemoryMapper::initialize(
675 AI&: FixedAI, OnInitialized: [this, OnInitialized = std::move(OnInitialized)](
676 Expected<ExecutorAddr> Result) mutable {
677 if (!Result)
678 return OnInitialized(Result.takeError());
679
680 OnInitialized(ExecutorAddr(Result->getValue() + DeltaAddr));
681 });
682 }
683
684 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
685 OnDeinitializedFunction OnDeInitialized) override {
686 std::vector<ExecutorAddr> Addrs(Allocations.size());
687 for (const auto Base : Allocations) {
688 Addrs.push_back(x: Base - DeltaAddr);
689 }
690
691 InProcessMemoryMapper::deinitialize(Allocations: Addrs, OnDeInitialized: std::move(OnDeInitialized));
692 }
693
694 void release(ArrayRef<ExecutorAddr> Reservations,
695 OnReleasedFunction OnRelease) override {
696 std::vector<ExecutorAddr> Addrs(Reservations.size());
697 for (const auto Base : Reservations) {
698 Addrs.push_back(x: Base - DeltaAddr);
699 }
700 InProcessMemoryMapper::release(Reservations: Addrs, OnRelease: std::move(OnRelease));
701 }
702
703private:
704 uint64_t TargetMapAddr;
705 uint64_t DeltaAddr;
706};
707
708Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
709 SizeString = SizeString.trim();
710
711 uint64_t Units = 1024;
712
713 if (SizeString.ends_with_insensitive(Suffix: "kb"))
714 SizeString = SizeString.drop_back(N: 2).rtrim();
715 else if (SizeString.ends_with_insensitive(Suffix: "mb")) {
716 Units = 1024 * 1024;
717 SizeString = SizeString.drop_back(N: 2).rtrim();
718 } else if (SizeString.ends_with_insensitive(Suffix: "gb")) {
719 Units = 1024 * 1024 * 1024;
720 SizeString = SizeString.drop_back(N: 2).rtrim();
721 }
722
723 uint64_t SlabSize = 0;
724 if (SizeString.getAsInteger(Radix: 10, Result&: SlabSize))
725 return make_error<StringError>(Args: "Invalid numeric format for slab size",
726 Args: inconvertibleErrorCode());
727
728 return SlabSize * Units;
729}
730
731static std::unique_ptr<JITLinkMemoryManager> createInProcessMemoryManager() {
732 uint64_t SlabSize;
733#ifdef _WIN32
734 SlabSize = 1024 * 1024;
735#else
736 SlabSize = 1024 * 1024 * 1024;
737#endif
738
739 if (!SlabAllocateSizeString.empty())
740 SlabSize = ExitOnErr(getSlabAllocSize(SizeString: SlabAllocateSizeString));
741
742 // If this is a -no-exec case and we're tweaking the slab address or size then
743 // use the delta mapper.
744 if (NoExec && (SlabAddress || SlabPageSize))
745 return ExitOnErr(
746 MapperJITLinkMemoryManager::CreateWithMapper<InProcessDeltaMapper>(
747 ReservationGranularity: SlabSize));
748
749 // Otherwise use the standard in-process mapper.
750 return ExitOnErr(
751 MapperJITLinkMemoryManager::CreateWithMapper<InProcessMemoryMapper>(
752 ReservationGranularity: SlabSize));
753}
754
755Expected<std::unique_ptr<jitlink::JITLinkMemoryManager>>
756createSimpleRemoteMemoryManager(SimpleRemoteEPC &SREPC) {
757 SimpleRemoteMemoryMapper::SymbolAddrs SAs;
758 if (auto Err = SREPC.getBootstrapSymbols(
759 Pairs: {{SAs.Instance, rt::SimpleExecutorMemoryManagerInstanceName},
760 {SAs.Reserve, rt::SimpleExecutorMemoryManagerReserveWrapperName},
761 {SAs.Initialize,
762 rt::SimpleExecutorMemoryManagerInitializeWrapperName},
763 {SAs.Deinitialize,
764 rt::SimpleExecutorMemoryManagerDeinitializeWrapperName},
765 {SAs.Release, rt::SimpleExecutorMemoryManagerReleaseWrapperName}}))
766 return std::move(Err);
767#ifdef _WIN32
768 size_t SlabSize = 1024 * 1024;
769#else
770 size_t SlabSize = 1024 * 1024 * 1024;
771#endif
772 return MapperJITLinkMemoryManager::CreateWithMapper<SimpleRemoteMemoryMapper>(
773 ReservationGranularity: SlabSize, A&: SREPC, A&: SAs);
774}
775
776Expected<std::unique_ptr<jitlink::JITLinkMemoryManager>>
777createSharedMemoryManager(SimpleRemoteEPC &SREPC) {
778 SharedMemoryMapper::SymbolAddrs SAs;
779 if (auto Err = SREPC.getBootstrapSymbols(
780 Pairs: {{SAs.Instance, rt::ExecutorSharedMemoryMapperServiceInstanceName},
781 {SAs.Reserve,
782 rt::ExecutorSharedMemoryMapperServiceReserveWrapperName},
783 {SAs.Initialize,
784 rt::ExecutorSharedMemoryMapperServiceInitializeWrapperName},
785 {SAs.Deinitialize,
786 rt::ExecutorSharedMemoryMapperServiceDeinitializeWrapperName},
787 {SAs.Release,
788 rt::ExecutorSharedMemoryMapperServiceReleaseWrapperName}}))
789 return std::move(Err);
790
791#ifdef _WIN32
792 size_t SlabSize = 1024 * 1024;
793#else
794 size_t SlabSize = 1024 * 1024 * 1024;
795#endif
796
797 if (!SlabAllocateSizeString.empty())
798 SlabSize = ExitOnErr(getSlabAllocSize(SizeString: SlabAllocateSizeString));
799
800 return MapperJITLinkMemoryManager::CreateWithMapper<SharedMemoryMapper>(
801 ReservationGranularity: SlabSize, A&: SREPC, A&: SAs);
802}
803
804#if LLVM_ON_UNIX && LLVM_ENABLE_THREADS
805static void setupEPCRemoteMemoryManager(SimpleRemoteEPC::Setup &S) {
806 switch (UseMemMgr) {
807 case MemMgr::Default:
808 case MemMgr::Generic:
809 break;
810 case MemMgr::SimpleRemote:
811 S.CreateMemoryManager = createSimpleRemoteMemoryManager;
812 break;
813 case MemMgr::Shared:
814 S.CreateMemoryManager = createSharedMemoryManager;
815 break;
816 }
817}
818#endif
819
820static Expected<MaterializationUnit::Interface>
821getTestObjectFileInterface(Session &S, MemoryBufferRef O) {
822
823 // Get the standard interface for this object, but ignore the symbols field.
824 // We'll handle that manually to include promotion.
825 auto I = getObjectFileInterface(ES&: S.ES, ObjBuffer: O);
826 if (!I)
827 return I.takeError();
828 I->SymbolFlags.clear();
829
830 // If creating an object file was going to fail it would have happened above,
831 // so we can 'cantFail' this.
832 auto Obj = cantFail(ValOrErr: object::ObjectFile::createObjectFile(Object: O));
833
834 // The init symbol must be included in the SymbolFlags map if present.
835 if (I->InitSymbol)
836 I->SymbolFlags[I->InitSymbol] =
837 JITSymbolFlags::MaterializationSideEffectsOnly;
838
839 for (auto &Sym : Obj->symbols()) {
840 Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
841 if (!SymFlagsOrErr)
842 // TODO: Test this error.
843 return SymFlagsOrErr.takeError();
844
845 // Skip symbols not defined in this object file.
846 if ((*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined))
847 continue;
848
849 auto Name = Sym.getName();
850 if (!Name)
851 return Name.takeError();
852
853 // Skip symbols that have type SF_File.
854 if (auto SymType = Sym.getType()) {
855 if (*SymType == object::SymbolRef::ST_File)
856 continue;
857 } else
858 return SymType.takeError();
859
860 auto SymFlags = JITSymbolFlags::fromObjectSymbol(Symbol: Sym);
861 if (!SymFlags)
862 return SymFlags.takeError();
863
864 if (SymFlags->isWeak()) {
865 // If this is a weak symbol that's not defined in the harness then we
866 // need to either mark it as strong (if this is the first definition
867 // that we've seen) or discard it.
868 if (S.HarnessDefinitions.count(Key: *Name) || S.CanonicalWeakDefs.count(Val: *Name))
869 continue;
870 S.CanonicalWeakDefs[*Name] = O.getBufferIdentifier();
871 *SymFlags &= ~JITSymbolFlags::Weak;
872 if (!S.HarnessExternals.count(Key: *Name))
873 *SymFlags &= ~JITSymbolFlags::Exported;
874 } else if (S.HarnessExternals.count(Key: *Name)) {
875 *SymFlags |= JITSymbolFlags::Exported;
876 } else if (S.HarnessDefinitions.count(Key: *Name) ||
877 !(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
878 continue;
879
880 I->SymbolFlags[S.ES.intern(SymName: *Name)] = std::move(*SymFlags);
881 }
882
883 return I;
884}
885
886static Error loadProcessSymbols(Session &S) {
887 S.ProcessSymsJD = &S.ES.createBareJITDylib(Name: "Process");
888 auto FilterMainEntryPoint =
889 [EPName = S.ES.intern(SymName: EntryPointName)](SymbolStringPtr Name) {
890 return Name != EPName;
891 };
892 S.ProcessSymsJD->addGenerator(
893 DefGenerator: ExitOnErr(orc::EPCDynamicLibrarySearchGenerator::GetForTargetProcess(
894 ES&: S.ES, DylibMgr&: *S.DylibMgr, Allow: std::move(FilterMainEntryPoint))));
895
896 return Error::success();
897}
898
899static Error loadDylibs(Session &S) {
900 LLVM_DEBUG(dbgs() << "Loading dylibs...\n");
901 for (const auto &Dylib : Dylibs) {
902 LLVM_DEBUG(dbgs() << " " << Dylib << "\n");
903 auto DL = S.getOrLoadDynamicLibrary(LibPath: Dylib);
904 if (!DL)
905 return DL.takeError();
906 }
907
908 return Error::success();
909}
910
911static Expected<std::unique_ptr<ExecutorProcessControl>> launchExecutor() {
912#ifndef LLVM_ON_UNIX
913 // FIXME: Add support for Windows.
914 return make_error<StringError>("-" + OutOfProcessExecutor.ArgStr +
915 " not supported on non-unix platforms",
916 inconvertibleErrorCode());
917#elif !LLVM_ENABLE_THREADS
918 // Out of process mode using SimpleRemoteEPC depends on threads.
919 return make_error<StringError>(
920 "-" + OutOfProcessExecutor.ArgStr +
921 " requires threads, but LLVM was built with "
922 "LLVM_ENABLE_THREADS=Off",
923 inconvertibleErrorCode());
924#else
925
926 constexpr int ReadEnd = 0;
927 constexpr int WriteEnd = 1;
928
929 // Pipe FDs.
930 int ToExecutor[2];
931 int FromExecutor[2];
932
933 pid_t ChildPID;
934
935 // Create pipes to/from the executor..
936 if (pipe(pipedes: ToExecutor) != 0 || pipe(pipedes: FromExecutor) != 0)
937 return make_error<StringError>(Args: "Unable to create pipe for executor",
938 Args: inconvertibleErrorCode());
939
940 ChildPID = fork();
941
942 if (ChildPID == 0) {
943 // In the child...
944
945 // Close the parent ends of the pipes
946 close(fd: ToExecutor[WriteEnd]);
947 close(fd: FromExecutor[ReadEnd]);
948
949 // Execute the child process.
950 std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
951 {
952 ExecutorPath = std::make_unique<char[]>(num: OutOfProcessExecutor.size() + 1);
953 strcpy(dest: ExecutorPath.get(), src: OutOfProcessExecutor.data());
954
955 std::string FDSpecifierStr("filedescs=");
956 FDSpecifierStr += utostr(X: ToExecutor[ReadEnd]);
957 FDSpecifierStr += ',';
958 FDSpecifierStr += utostr(X: FromExecutor[WriteEnd]);
959 FDSpecifier = std::make_unique<char[]>(num: FDSpecifierStr.size() + 1);
960 strcpy(dest: FDSpecifier.get(), src: FDSpecifierStr.c_str());
961 }
962
963 char *const Args[] = {ExecutorPath.get(), FDSpecifier.get(), nullptr};
964 int RC = execvp(file: ExecutorPath.get(), argv: Args);
965 if (RC != 0) {
966 errs() << "unable to launch out-of-process executor \""
967 << ExecutorPath.get() << "\"\n";
968 exit(status: 1);
969 }
970 }
971 // else we're the parent...
972
973 // Close the child ends of the pipes
974 close(fd: ToExecutor[ReadEnd]);
975 close(fd: FromExecutor[WriteEnd]);
976
977 auto S = SimpleRemoteEPC::Setup();
978 setupEPCRemoteMemoryManager(S);
979
980 return SimpleRemoteEPC::Create<FDSimpleRemoteEPCTransport>(
981 D: std::make_unique<DynamicThreadPoolTaskDispatcher>(args&: MaterializationThreads),
982 S: std::move(S), TransportTCtorArgs&: FromExecutor[ReadEnd], TransportTCtorArgs&: ToExecutor[WriteEnd]);
983#endif
984}
985
986#if LLVM_ON_UNIX && LLVM_ENABLE_THREADS
987static Error createTCPSocketError(Twine Details) {
988 return make_error<StringError>(
989 Args: formatv(Fmt: "Failed to connect TCP socket '{0}': {1}",
990 Vals&: OutOfProcessExecutorConnect, Vals&: Details),
991 Args: inconvertibleErrorCode());
992}
993
994static Expected<int> connectTCPSocket(std::string Host, std::string PortStr) {
995 addrinfo *AI;
996 addrinfo Hints{};
997 Hints.ai_family = AF_INET;
998 Hints.ai_socktype = SOCK_STREAM;
999 Hints.ai_flags = AI_NUMERICSERV;
1000
1001 if (int EC = getaddrinfo(name: Host.c_str(), service: PortStr.c_str(), req: &Hints, pai: &AI))
1002 return createTCPSocketError(Details: "Address resolution failed (" +
1003 StringRef(gai_strerror(ecode: EC)) + ")");
1004
1005 // Cycle through the returned addrinfo structures and connect to the first
1006 // reachable endpoint.
1007 int SockFD;
1008 addrinfo *Server;
1009 for (Server = AI; Server != nullptr; Server = Server->ai_next) {
1010 // socket might fail, e.g. if the address family is not supported. Skip to
1011 // the next addrinfo structure in such a case.
1012 if ((SockFD = socket(domain: AI->ai_family, type: AI->ai_socktype, protocol: AI->ai_protocol)) < 0)
1013 continue;
1014
1015 // If connect returns null, we exit the loop with a working socket.
1016 if (connect(fd: SockFD, addr: Server->ai_addr, len: Server->ai_addrlen) == 0)
1017 break;
1018
1019 close(fd: SockFD);
1020 }
1021 freeaddrinfo(ai: AI);
1022
1023 // If we reached the end of the loop without connecting to a valid endpoint,
1024 // dump the last error that was logged in socket() or connect().
1025 if (Server == nullptr)
1026 return createTCPSocketError(Details: std::strerror(errno));
1027
1028 return SockFD;
1029}
1030#endif
1031
1032static Expected<std::unique_ptr<ExecutorProcessControl>> connectToExecutor() {
1033#ifndef LLVM_ON_UNIX
1034 // FIXME: Add TCP support for Windows.
1035 return make_error<StringError>("-" + OutOfProcessExecutorConnect.ArgStr +
1036 " not supported on non-unix platforms",
1037 inconvertibleErrorCode());
1038#elif !LLVM_ENABLE_THREADS
1039 // Out of process mode using SimpleRemoteEPC depends on threads.
1040 return make_error<StringError>(
1041 "-" + OutOfProcessExecutorConnect.ArgStr +
1042 " requires threads, but LLVM was built with "
1043 "LLVM_ENABLE_THREADS=Off",
1044 inconvertibleErrorCode());
1045#else
1046
1047 StringRef Host, PortStr;
1048 std::tie(args&: Host, args&: PortStr) = StringRef(OutOfProcessExecutorConnect).split(Separator: ':');
1049 if (Host.empty())
1050 return createTCPSocketError(Details: "Host name for -" +
1051 OutOfProcessExecutorConnect.ArgStr +
1052 " can not be empty");
1053 if (PortStr.empty())
1054 return createTCPSocketError(Details: "Port number in -" +
1055 OutOfProcessExecutorConnect.ArgStr +
1056 " can not be empty");
1057 int Port = 0;
1058 if (PortStr.getAsInteger(Radix: 10, Result&: Port))
1059 return createTCPSocketError(Details: "Port number '" + PortStr +
1060 "' is not a valid integer");
1061
1062 Expected<int> SockFD = connectTCPSocket(Host: Host.str(), PortStr: PortStr.str());
1063 if (!SockFD)
1064 return SockFD.takeError();
1065
1066 auto S = SimpleRemoteEPC::Setup();
1067 setupEPCRemoteMemoryManager(S);
1068
1069 return SimpleRemoteEPC::Create<FDSimpleRemoteEPCTransport>(
1070 D: std::make_unique<DynamicThreadPoolTaskDispatcher>(args: std::nullopt),
1071 S: std::move(S), TransportTCtorArgs&: *SockFD, TransportTCtorArgs&: *SockFD);
1072#endif
1073}
1074
1075class PhonyExternalsGenerator : public DefinitionGenerator {
1076public:
1077 Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
1078 JITDylibLookupFlags JDLookupFlags,
1079 const SymbolLookupSet &LookupSet) override {
1080 SymbolMap PhonySymbols;
1081 for (auto &KV : LookupSet)
1082 PhonySymbols[KV.first] = {ExecutorAddr(), JITSymbolFlags::Exported};
1083 return JD.define(MU: absoluteSymbols(Symbols: std::move(PhonySymbols)));
1084 }
1085};
1086
1087Expected<std::unique_ptr<Session::LazyLinkingSupport>>
1088createLazyLinkingSupport(Session &S) {
1089 auto RSMgr = JITLinkRedirectableSymbolManager::Create(ObjLinkingLayer&: S.ObjLayer);
1090 if (!RSMgr)
1091 return RSMgr.takeError();
1092
1093 std::shared_ptr<SimpleLazyReexportsSpeculator> Speculator;
1094 switch (Speculate) {
1095 case SpeculateKind::None:
1096 break;
1097 case SpeculateKind::Simple:
1098 SimpleLazyReexportsSpeculator::RecordExecutionFunction RecordExecs;
1099
1100 if (!RecordLazyExecs.empty())
1101 RecordExecs = [&S](const LazyReexportsManager::CallThroughInfo &CTI) {
1102 S.LazyFnExecOrder.push_back(x: {CTI.JD->getName(), CTI.BodyName});
1103 };
1104
1105 Speculator =
1106 SimpleLazyReexportsSpeculator::Create(ES&: S.ES, RecordExec: std::move(RecordExecs));
1107 break;
1108 }
1109
1110 auto LRMgr = createJITLinkLazyReexportsManager(
1111 ObjLinkingLayer&: S.ObjLayer, RSMgr&: **RSMgr, PlatformJD&: *S.PlatformJD, L: Speculator.get());
1112 if (!LRMgr)
1113 return LRMgr.takeError();
1114
1115 return std::make_unique<Session::LazyLinkingSupport>(
1116 args: std::move(*RSMgr), args: std::move(Speculator), args: std::move(*LRMgr), args&: S.ObjLayer);
1117}
1118
1119static Error writeLazyExecOrder(Session &S) {
1120 if (RecordLazyExecs.empty())
1121 return Error::success();
1122
1123 std::error_code EC;
1124 raw_fd_ostream ExecOrderOut(RecordLazyExecs, EC);
1125 if (EC)
1126 return createFileError(F: RecordLazyExecs, EC);
1127
1128 for (auto &[JDName, FunctionName] : S.LazyFnExecOrder)
1129 ExecOrderOut << JDName << ", " << FunctionName << "\n";
1130
1131 return Error::success();
1132}
1133
1134Expected<std::unique_ptr<Session>> Session::Create(Triple TT,
1135 SubtargetFeatures Features) {
1136
1137 std::unique_ptr<ExecutorProcessControl> EPC;
1138 if (OutOfProcessExecutor.getNumOccurrences()) {
1139 /// If -oop-executor is passed then launch the executor.
1140 if (auto REPC = launchExecutor())
1141 EPC = std::move(*REPC);
1142 else
1143 return REPC.takeError();
1144 } else if (OutOfProcessExecutorConnect.getNumOccurrences()) {
1145 /// If -oop-executor-connect is passed then connect to the executor.
1146 if (auto REPC = connectToExecutor())
1147 EPC = std::move(*REPC);
1148 else
1149 return REPC.takeError();
1150 } else {
1151 /// Otherwise use SelfExecutorProcessControl to target the current process.
1152 auto PageSize = sys::Process::getPageSize();
1153 if (!PageSize)
1154 return PageSize.takeError();
1155 std::unique_ptr<TaskDispatcher> Dispatcher;
1156 if (MaterializationThreads == 0)
1157 Dispatcher = std::make_unique<InPlaceTaskDispatcher>();
1158 else {
1159#if LLVM_ENABLE_THREADS
1160 Dispatcher = std::make_unique<DynamicThreadPoolTaskDispatcher>(
1161 args&: MaterializationThreads);
1162#else
1163 llvm_unreachable("MaterializationThreads should be 0");
1164#endif
1165 }
1166
1167 EPC = std::make_unique<SelfExecutorProcessControl>(
1168 args: std::make_shared<SymbolStringPool>(), args: std::move(Dispatcher),
1169 args: std::move(TT), args&: *PageSize, args: createInProcessMemoryManager());
1170 }
1171
1172 Error Err = Error::success();
1173 std::unique_ptr<Session> S(new Session(std::move(EPC), Err));
1174 if (Err)
1175 return std::move(Err);
1176 S->Features = std::move(Features);
1177
1178 if (lazyLinkingRequested()) {
1179 if (auto LazyLinking = createLazyLinkingSupport(S&: *S))
1180 S->LazyLinking = std::move(*LazyLinking);
1181 else
1182 return LazyLinking.takeError();
1183 }
1184
1185 return std::move(S);
1186}
1187
1188Session::~Session() {
1189 if (auto Err = writeLazyExecOrder(S&: *this))
1190 ES.reportError(Err: std::move(Err));
1191
1192 if (auto Err = ES.endSession())
1193 ES.reportError(Err: std::move(Err));
1194}
1195
1196Session::Session(std::unique_ptr<ExecutorProcessControl> EPC, Error &Err)
1197 : ES(std::move(EPC)),
1198 ObjLayer(ES, ES.getExecutorProcessControl().getMemMgr()) {
1199
1200 /// Local ObjectLinkingLayer::Plugin class to forward modifyPassConfig to the
1201 /// Session.
1202 class JITLinkSessionPlugin : public ObjectLinkingLayer::Plugin {
1203 public:
1204 JITLinkSessionPlugin(Session &S) : S(S) {}
1205 void modifyPassConfig(MaterializationResponsibility &MR, LinkGraph &G,
1206 PassConfiguration &PassConfig) override {
1207 S.modifyPassConfig(G, PassConfig);
1208 }
1209
1210 Error notifyFailed(MaterializationResponsibility &MR) override {
1211 return Error::success();
1212 }
1213 Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override {
1214 return Error::success();
1215 }
1216 void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
1217 ResourceKey SrcKey) override {}
1218
1219 private:
1220 Session &S;
1221 };
1222
1223 ErrorAsOutParameter _(&Err);
1224
1225 if (auto DM = ES.getExecutorProcessControl().createDefaultDylibMgr())
1226 DylibMgr = std::move(*DM);
1227 else {
1228 Err = DM.takeError();
1229 return;
1230 }
1231
1232 ES.setErrorReporter(reportLLVMJITLinkError);
1233
1234 // Attach WaitingOnGraph recorder if requested.
1235 if (!WaitingOnGraphCapture.empty()) {
1236 if (auto GRecorderOrErr =
1237 WaitingOnGraphOpRecorder::Create(Path: WaitingOnGraphCapture)) {
1238 GOpRecorder = std::move(*GRecorderOrErr);
1239 ES.setWaitingOnGraphOpRecorder(*GOpRecorder);
1240 } else {
1241 Err = GRecorderOrErr.takeError();
1242 return;
1243 }
1244 }
1245
1246 if (!NoProcessSymbols)
1247 ExitOnErr(loadProcessSymbols(S&: *this));
1248
1249 ExitOnErr(loadDylibs(S&: *this));
1250
1251 auto &TT = ES.getTargetTriple();
1252
1253 if (!WriteSymbolTableTo.empty()) {
1254 if (auto STDump = SymbolTableDumpPlugin::Create(Path: WriteSymbolTableTo))
1255 ObjLayer.addPlugin(P: std::move(*STDump));
1256 else {
1257 Err = STDump.takeError();
1258 return;
1259 }
1260 }
1261
1262 if (DebuggerSupport && TT.isOSBinFormatMachO()) {
1263 if (!ProcessSymsJD) {
1264 Err = make_error<StringError>(Args: "MachO debugging requires process symbols",
1265 Args: inconvertibleErrorCode());
1266 return;
1267 }
1268 ObjLayer.addPlugin(P: ExitOnErr(GDBJITDebugInfoRegistrationPlugin::Create(
1269 ES&: this->ES, ProcessJD&: *ProcessSymsJD, TT)));
1270 }
1271
1272 if (PerfSupport && TT.isOSBinFormatELF()) {
1273 if (!ProcessSymsJD) {
1274 Err = make_error<StringError>(Args: "MachO debugging requires process symbols",
1275 Args: inconvertibleErrorCode());
1276 return;
1277 }
1278 ObjLayer.addPlugin(P: ExitOnErr(DebugInfoPreservationPlugin::Create()));
1279 ObjLayer.addPlugin(P: ExitOnErr(PerfSupportPlugin::Create(
1280 EPC&: this->ES.getExecutorProcessControl(), JD&: *ProcessSymsJD, EmitDebugInfo: true, EmitUnwindInfo: true)));
1281 }
1282
1283 if (VTuneSupport && TT.isOSBinFormatELF()) {
1284 ObjLayer.addPlugin(P: ExitOnErr(DebugInfoPreservationPlugin::Create()));
1285 ObjLayer.addPlugin(P: ExitOnErr(
1286 VTuneSupportPlugin::Create(EPC&: this->ES.getExecutorProcessControl(),
1287 JD&: *ProcessSymsJD, /*EmitDebugInfo=*/true,
1288 /*TestMode=*/true)));
1289 }
1290
1291 // Set up the platform.
1292 if (!OrcRuntime.empty()) {
1293 assert(ProcessSymsJD && "ProcessSymsJD should have been set");
1294 PlatformJD = &ES.createBareJITDylib(Name: "Platform");
1295 PlatformJD->addToLinkOrder(JD&: *ProcessSymsJD);
1296
1297 if (TT.isOSBinFormatMachO()) {
1298 if (auto P =
1299 MachOPlatform::Create(ObjLinkingLayer&: ObjLayer, PlatformJD&: *PlatformJD, OrcRuntimePath: OrcRuntime.c_str()))
1300 ES.setPlatform(std::move(*P));
1301 else {
1302 Err = P.takeError();
1303 return;
1304 }
1305 } else if (TT.isOSBinFormatELF()) {
1306 if (auto P =
1307 ELFNixPlatform::Create(ObjLinkingLayer&: ObjLayer, PlatformJD&: *PlatformJD, OrcRuntimePath: OrcRuntime.c_str()))
1308 ES.setPlatform(std::move(*P));
1309 else {
1310 Err = P.takeError();
1311 return;
1312 }
1313 } else if (TT.isOSBinFormatCOFF()) {
1314 auto LoadDynLibrary = [&, this](JITDylib &JD,
1315 StringRef DLLName) -> Error {
1316 if (!DLLName.ends_with_insensitive(Suffix: ".dll"))
1317 return make_error<StringError>(Args: "DLLName not ending with .dll",
1318 Args: inconvertibleErrorCode());
1319 return loadAndLinkDynamicLibrary(JD, LibPath: DLLName);
1320 };
1321
1322 if (auto P =
1323 COFFPlatform::Create(ObjLinkingLayer&: ObjLayer, PlatformJD&: *PlatformJD, OrcRuntimePath: OrcRuntime.c_str(),
1324 LoadDynLibrary: std::move(LoadDynLibrary)))
1325 ES.setPlatform(std::move(*P));
1326 else {
1327 Err = P.takeError();
1328 return;
1329 }
1330 } else {
1331 Err = make_error<StringError>(
1332 Args: "-" + OrcRuntime.ArgStr + " specified, but format " +
1333 Triple::getObjectFormatTypeName(ObjectFormat: TT.getObjectFormat()) +
1334 " not supported",
1335 Args: inconvertibleErrorCode());
1336 return;
1337 }
1338 } else if (TT.isOSBinFormatMachO()) {
1339 if (!NoExec) {
1340 std::optional<bool> ForceEHFrames;
1341 if ((Err = ES.getBootstrapMapValue<bool, bool>(Key: "darwin-use-ehframes-only",
1342 Val&: ForceEHFrames)))
1343 return;
1344 bool UseEHFrames = ForceEHFrames.value_or(u: false);
1345 if (!UseEHFrames)
1346 ObjLayer.addPlugin(P: ExitOnErr(UnwindInfoRegistrationPlugin::Create(ES)));
1347 else
1348 ObjLayer.addPlugin(P: ExitOnErr(EHFrameRegistrationPlugin::Create(ES)));
1349 }
1350 } else if (TT.isOSBinFormatELF()) {
1351 if (!NoExec)
1352 ObjLayer.addPlugin(P: ExitOnErr(EHFrameRegistrationPlugin::Create(ES)));
1353 if (DebuggerSupport) {
1354 Error TargetSymErr = Error::success();
1355 auto Plugin =
1356 std::make_unique<ELFDebugObjectPlugin>(args&: ES, args: true, args: true, args&: TargetSymErr);
1357 if (!TargetSymErr)
1358 ObjLayer.addPlugin(P: std::move(Plugin));
1359 else
1360 logAllUnhandledErrors(E: std::move(TargetSymErr), OS&: errs(),
1361 ErrorBanner: "Debugger support not available: ");
1362 }
1363 }
1364
1365 if (auto MainJDOrErr = ES.createJITDylib(Name: "main"))
1366 MainJD = &*MainJDOrErr;
1367 else {
1368 Err = MainJDOrErr.takeError();
1369 return;
1370 }
1371
1372 if (NoProcessSymbols) {
1373 // This symbol is used in testcases, but we're not reflecting process
1374 // symbols so we'll need to make it available some other way.
1375 auto &TestResultJD = ES.createBareJITDylib(Name: "<TestResultJD>");
1376 ExitOnErr(TestResultJD.define(MU: absoluteSymbols(
1377 Symbols: {{ES.intern(SymName: "llvm_jitlink_setTestResultOverride"),
1378 {ExecutorAddr::fromPtr(Ptr: llvm_jitlink_setTestResultOverride),
1379 JITSymbolFlags::Exported}}})));
1380 MainJD->addToLinkOrder(JD&: TestResultJD);
1381 }
1382
1383 ObjLayer.addPlugin(P: std::make_unique<JITLinkSessionPlugin>(args&: *this));
1384
1385 // Process any harness files.
1386 for (auto &HarnessFile : TestHarnesses) {
1387 HarnessFiles.insert(key: HarnessFile);
1388
1389 auto ObjBuffer =
1390 ExitOnErr(loadLinkableFile(Path: HarnessFile, TT: ES.getTargetTriple(),
1391 LA: LoadArchives::Never))
1392 .first;
1393
1394 auto ObjInterface =
1395 ExitOnErr(getObjectFileInterface(ES, ObjBuffer: ObjBuffer->getMemBufferRef()));
1396
1397 for (auto &KV : ObjInterface.SymbolFlags)
1398 HarnessDefinitions.insert(key: *KV.first);
1399
1400 auto Obj = ExitOnErr(
1401 object::ObjectFile::createObjectFile(Object: ObjBuffer->getMemBufferRef()));
1402
1403 for (auto &Sym : Obj->symbols()) {
1404 uint32_t SymFlags = ExitOnErr(Sym.getFlags());
1405 auto Name = ExitOnErr(Sym.getName());
1406
1407 if (Name.empty())
1408 continue;
1409
1410 if (SymFlags & object::BasicSymbolRef::SF_Undefined)
1411 HarnessExternals.insert(key: Name);
1412 }
1413 }
1414
1415 // If a name is defined by some harness file then it's a definition, not an
1416 // external.
1417 for (auto &DefName : HarnessDefinitions)
1418 HarnessExternals.erase(Key: DefName.getKey());
1419
1420 if (!ShowLinkGraphs.empty())
1421 ShowGraphsRegex = Regex(ShowLinkGraphs);
1422}
1423
1424void Session::dumpSessionInfo(raw_ostream &OS) {
1425 OS << "Registered addresses:\n" << SymbolInfos << FileInfos;
1426}
1427
1428void Session::modifyPassConfig(LinkGraph &G, PassConfiguration &PassConfig) {
1429
1430 if (ShowLinkedFiles)
1431 outs() << "Linking " << G.getName() << "\n";
1432
1433 if (!CheckFiles.empty() || ShowAddrs)
1434 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) {
1435 if (ES.getTargetTriple().getObjectFormat() == Triple::ELF)
1436 return registerELFGraphInfo(S&: *this, G);
1437
1438 if (ES.getTargetTriple().getObjectFormat() == Triple::MachO)
1439 return registerMachOGraphInfo(S&: *this, G);
1440
1441 if (ES.getTargetTriple().getObjectFormat() == Triple::COFF)
1442 return registerCOFFGraphInfo(S&: *this, G);
1443
1444 return make_error<StringError>(Args: "Unsupported object format for GOT/stub "
1445 "registration",
1446 Args: inconvertibleErrorCode());
1447 });
1448
1449 if (ShowGraphsRegex)
1450 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) -> Error {
1451 std::lock_guard<std::mutex> Lock(M);
1452 // Print graph if ShowLinkGraphs is specified-but-empty, or if
1453 // it contains the given graph.
1454 if (ShowGraphsRegex->match(String: G.getName())) {
1455 outs() << "Link graph \"" << G.getName() << "\" post-fixup:\n";
1456 G.dump(OS&: outs());
1457 }
1458 return Error::success();
1459 });
1460
1461 PassConfig.PrePrunePasses.push_back(x: [this](LinkGraph &G) {
1462 std::lock_guard<std::mutex> Lock(M);
1463 ++ActiveLinks;
1464 return Error::success();
1465 });
1466 PassConfig.PrePrunePasses.push_back(
1467 x: [this](LinkGraph &G) { return applyLibraryLinkModifiers(S&: *this, G); });
1468 PassConfig.PrePrunePasses.push_back(
1469 x: [this](LinkGraph &G) { return applyHarnessPromotions(S&: *this, G); });
1470
1471 if (ShowRelocatedSectionContents)
1472 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) -> Error {
1473 dumpSectionContents(OS&: outs(), S&: *this, G);
1474 return Error::success();
1475 });
1476
1477 if (AddSelfRelocations)
1478 PassConfig.PostPrunePasses.push_back(x: addSelfRelocations);
1479
1480 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) {
1481 std::lock_guard<std::mutex> Lock(M);
1482 if (--ActiveLinks == 0)
1483 ActiveLinksCV.notify_all();
1484 return Error::success();
1485 });
1486}
1487
1488Expected<JITDylib *> Session::getOrLoadDynamicLibrary(StringRef LibPath) {
1489 auto It = DynLibJDs.find(x: LibPath);
1490 if (It != DynLibJDs.end()) {
1491 return It->second;
1492 }
1493 auto G =
1494 EPCDynamicLibrarySearchGenerator::Load(ES, DylibMgr&: *DylibMgr, LibraryPath: LibPath.data());
1495 if (!G)
1496 return G.takeError();
1497 auto JD = &ES.createBareJITDylib(Name: LibPath.str());
1498
1499 JD->addGenerator(DefGenerator: std::move(*G));
1500 DynLibJDs.emplace(args: LibPath.str(), args&: JD);
1501 LLVM_DEBUG({
1502 dbgs() << "Loaded dynamic library " << LibPath.data() << " for " << LibPath
1503 << "\n";
1504 });
1505 return JD;
1506}
1507
1508Error Session::loadAndLinkDynamicLibrary(JITDylib &JD, StringRef LibPath) {
1509 auto DL = getOrLoadDynamicLibrary(LibPath);
1510 if (!DL)
1511 return DL.takeError();
1512 JD.addToLinkOrder(JD&: **DL);
1513 LLVM_DEBUG({
1514 dbgs() << "Linking dynamic library " << LibPath << " to " << JD.getName()
1515 << "\n";
1516 });
1517 return Error::success();
1518}
1519
1520Error Session::FileInfo::registerGOTEntry(
1521 LinkGraph &G, Symbol &Sym, GetSymbolTargetFunction GetSymbolTarget) {
1522 if (Sym.isSymbolZeroFill())
1523 return make_error<StringError>(Args: "Unexpected zero-fill symbol in section " +
1524 Sym.getBlock().getSection().getName(),
1525 Args: inconvertibleErrorCode());
1526 auto TS = GetSymbolTarget(G, Sym.getBlock());
1527 if (!TS)
1528 return TS.takeError();
1529 GOTEntryInfos[*TS->getName()] = {Sym.getSymbolContent(),
1530 Sym.getAddress().getValue(),
1531 Sym.getTargetFlags()};
1532 return Error::success();
1533}
1534
1535Error Session::FileInfo::registerStubEntry(
1536 LinkGraph &G, Symbol &Sym, GetSymbolTargetFunction GetSymbolTarget) {
1537 if (Sym.isSymbolZeroFill())
1538 return make_error<StringError>(Args: "Unexpected zero-fill symbol in section " +
1539 Sym.getBlock().getSection().getName(),
1540 Args: inconvertibleErrorCode());
1541 auto TS = GetSymbolTarget(G, Sym.getBlock());
1542 if (!TS)
1543 return TS.takeError();
1544
1545 SmallVectorImpl<MemoryRegionInfo> &Entry = StubInfos[*TS->getName()];
1546 Entry.insert(I: Entry.begin(),
1547 Elt: {Sym.getSymbolContent(), Sym.getAddress().getValue(),
1548 Sym.getTargetFlags()});
1549 return Error::success();
1550}
1551
1552Error Session::FileInfo::registerMultiStubEntry(
1553 LinkGraph &G, Symbol &Sym, GetSymbolTargetFunction GetSymbolTarget) {
1554 if (Sym.isSymbolZeroFill())
1555 return make_error<StringError>(Args: "Unexpected zero-fill symbol in section " +
1556 Sym.getBlock().getSection().getName(),
1557 Args: inconvertibleErrorCode());
1558
1559 auto Target = GetSymbolTarget(G, Sym.getBlock());
1560 if (!Target)
1561 return Target.takeError();
1562
1563 SmallVectorImpl<MemoryRegionInfo> &Entry = StubInfos[*Target->getName()];
1564 Entry.emplace_back(Args: Sym.getSymbolContent(), Args: Sym.getAddress().getValue(),
1565 Args: Sym.getTargetFlags());
1566
1567 // Let's keep stubs ordered by ascending address.
1568 std::sort(first: Entry.begin(), last: Entry.end(),
1569 comp: [](const MemoryRegionInfo &L, const MemoryRegionInfo &R) {
1570 return L.getTargetAddress() < R.getTargetAddress();
1571 });
1572
1573 return Error::success();
1574}
1575
1576Expected<Session::FileInfo &> Session::findFileInfo(StringRef FileName) {
1577 auto FileInfoItr = FileInfos.find(Key: FileName);
1578 if (FileInfoItr == FileInfos.end())
1579 return make_error<StringError>(Args: "file \"" + FileName + "\" not recognized",
1580 Args: inconvertibleErrorCode());
1581 return FileInfoItr->second;
1582}
1583
1584Expected<Session::MemoryRegionInfo &>
1585Session::findSectionInfo(StringRef FileName, StringRef SectionName) {
1586 auto FI = findFileInfo(FileName);
1587 if (!FI)
1588 return FI.takeError();
1589 auto SecInfoItr = FI->SectionInfos.find(Key: SectionName);
1590 if (SecInfoItr == FI->SectionInfos.end())
1591 return make_error<StringError>(Args: "no section \"" + SectionName +
1592 "\" registered for file \"" + FileName +
1593 "\"",
1594 Args: inconvertibleErrorCode());
1595 return SecInfoItr->second;
1596}
1597
1598class MemoryMatcher {
1599public:
1600 MemoryMatcher(ArrayRef<char> Content)
1601 : Pos(Content.data()), End(Pos + Content.size()) {}
1602
1603 template <typename MaskType> bool matchMask(MaskType Mask) {
1604 if (Mask == (Mask & *reinterpret_cast<const MaskType *>(Pos))) {
1605 Pos += sizeof(MaskType);
1606 return true;
1607 }
1608 return false;
1609 }
1610
1611 template <typename ValueType> bool matchEqual(ValueType Value) {
1612 if (Value == *reinterpret_cast<const ValueType *>(Pos)) {
1613 Pos += sizeof(ValueType);
1614 return true;
1615 }
1616 return false;
1617 }
1618
1619 bool done() const { return Pos == End; }
1620
1621private:
1622 const char *Pos;
1623 const char *End;
1624};
1625
1626static StringRef detectStubKind(const Session::MemoryRegionInfo &Stub) {
1627 using namespace support::endian;
1628 auto Armv7MovWTle = byte_swap<uint32_t>(value: 0xe300c000, endian: endianness::little);
1629 auto Armv7BxR12le = byte_swap<uint32_t>(value: 0xe12fff1c, endian: endianness::little);
1630 auto Thumbv7MovWTle = byte_swap<uint32_t>(value: 0x0c00f240, endian: endianness::little);
1631 auto Thumbv7BxR12le = byte_swap<uint16_t>(value: 0x4760, endian: endianness::little);
1632
1633 MemoryMatcher M(Stub.getContent());
1634 if (M.matchMask(Mask: Thumbv7MovWTle)) {
1635 if (M.matchMask(Mask: Thumbv7MovWTle))
1636 if (M.matchEqual(Value: Thumbv7BxR12le))
1637 if (M.done())
1638 return "thumbv7_abs_le";
1639 } else if (M.matchMask(Mask: Armv7MovWTle)) {
1640 if (M.matchMask(Mask: Armv7MovWTle))
1641 if (M.matchEqual(Value: Armv7BxR12le))
1642 if (M.done())
1643 return "armv7_abs_le";
1644 }
1645 return "";
1646}
1647
1648Expected<Session::MemoryRegionInfo &>
1649Session::findStubInfo(StringRef FileName, StringRef TargetName,
1650 StringRef KindNameFilter) {
1651 auto FI = findFileInfo(FileName);
1652 if (!FI)
1653 return FI.takeError();
1654 auto StubInfoItr = FI->StubInfos.find(Key: TargetName);
1655 if (StubInfoItr == FI->StubInfos.end())
1656 return make_error<StringError>(Args: "no stub for \"" + TargetName +
1657 "\" registered for file \"" + FileName +
1658 "\"",
1659 Args: inconvertibleErrorCode());
1660 auto &StubsForTarget = StubInfoItr->second;
1661 assert(!StubsForTarget.empty() && "At least 1 stub in each entry");
1662 if (KindNameFilter.empty() && StubsForTarget.size() == 1)
1663 return StubsForTarget[0]; // Regular single-stub match
1664
1665 std::string KindsStr;
1666 SmallVector<MemoryRegionInfo *, 1> Matches;
1667 Regex KindNameMatcher(KindNameFilter.empty() ? ".*" : KindNameFilter);
1668 for (MemoryRegionInfo &Stub : StubsForTarget) {
1669 StringRef Kind = detectStubKind(Stub);
1670 if (KindNameMatcher.match(String: Kind))
1671 Matches.push_back(Elt: &Stub);
1672 KindsStr += "\"" + (Kind.empty() ? "<unknown>" : Kind.str()) + "\", ";
1673 }
1674 if (Matches.empty())
1675 return make_error<StringError>(
1676 Args: "\"" + TargetName + "\" has " + Twine(StubsForTarget.size()) +
1677 " stubs in file \"" + FileName +
1678 "\", but none of them matches the stub-kind filter \"" +
1679 KindNameFilter + "\" (all encountered kinds are " +
1680 StringRef(KindsStr.data(), KindsStr.size() - 2) + ").",
1681 Args: inconvertibleErrorCode());
1682 if (Matches.size() > 1)
1683 return make_error<StringError>(
1684 Args: "\"" + TargetName + "\" has " + Twine(Matches.size()) +
1685 " candidate stubs in file \"" + FileName +
1686 "\". Please refine stub-kind filter \"" + KindNameFilter +
1687 "\" for disambiguation (encountered kinds are " +
1688 StringRef(KindsStr.data(), KindsStr.size() - 2) + ").",
1689 Args: inconvertibleErrorCode());
1690
1691 return *Matches[0];
1692}
1693
1694Expected<Session::MemoryRegionInfo &>
1695Session::findGOTEntryInfo(StringRef FileName, StringRef TargetName) {
1696 auto FI = findFileInfo(FileName);
1697 if (!FI)
1698 return FI.takeError();
1699 auto GOTInfoItr = FI->GOTEntryInfos.find(Key: TargetName);
1700 if (GOTInfoItr == FI->GOTEntryInfos.end())
1701 return make_error<StringError>(Args: "no GOT entry for \"" + TargetName +
1702 "\" registered for file \"" + FileName +
1703 "\"",
1704 Args: inconvertibleErrorCode());
1705 return GOTInfoItr->second;
1706}
1707
1708bool Session::isSymbolRegistered(const orc::SymbolStringPtr &SymbolName) {
1709 return SymbolInfos.count(Val: SymbolName);
1710}
1711
1712Expected<Session::MemoryRegionInfo &>
1713Session::findSymbolInfo(const orc::SymbolStringPtr &SymbolName,
1714 Twine ErrorMsgStem) {
1715 auto SymInfoItr = SymbolInfos.find(Val: SymbolName);
1716 if (SymInfoItr == SymbolInfos.end())
1717 return make_error<StringError>(Args: ErrorMsgStem + ": symbol " + *SymbolName +
1718 " not found",
1719 Args: inconvertibleErrorCode());
1720 return SymInfoItr->second;
1721}
1722
1723} // end namespace llvm
1724
1725static std::pair<Triple, SubtargetFeatures> getFirstFileTripleAndFeatures() {
1726
1727 // If we're running in symbolicate mode then just use the process triple.
1728 if (!SymbolicateWith.empty())
1729 return std::make_pair(x: Triple(sys::getProcessTriple()), y: SubtargetFeatures());
1730
1731 // Otherwise we need to inspect the input files.
1732 static std::pair<Triple, SubtargetFeatures> FirstTTAndFeatures = []() {
1733 assert(!InputFiles.empty() && "InputFiles can not be empty");
1734
1735 if (!OverrideTriple.empty()) {
1736 LLVM_DEBUG({
1737 dbgs() << "Triple from -triple override: " << OverrideTriple << "\n";
1738 });
1739 return std::make_pair(x: Triple(OverrideTriple), y: SubtargetFeatures());
1740 }
1741
1742 for (auto InputFile : InputFiles) {
1743 auto ObjBuffer = ExitOnErr(getFile(FileName: InputFile));
1744 file_magic Magic = identify_magic(magic: ObjBuffer->getBuffer());
1745 switch (Magic) {
1746 case file_magic::coff_object:
1747 case file_magic::elf_relocatable:
1748 case file_magic::macho_object: {
1749 auto Obj = ExitOnErr(
1750 object::ObjectFile::createObjectFile(Object: ObjBuffer->getMemBufferRef()));
1751 Triple TT;
1752 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Val: Obj.get()))
1753 TT = MachOObj->getArchTriple();
1754 else
1755 TT = Obj->makeTriple();
1756 if (Magic == file_magic::coff_object) {
1757 // TODO: Move this to makeTriple() if possible.
1758 TT.setObjectFormat(Triple::COFF);
1759 TT.setOS(Triple::OSType::Win32);
1760 }
1761 SubtargetFeatures Features;
1762 if (auto ObjFeatures = Obj->getFeatures())
1763 Features = std::move(*ObjFeatures);
1764
1765 LLVM_DEBUG({
1766 dbgs() << "Triple from " << InputFile << ": " << TT.str() << "\n";
1767 });
1768 return std::make_pair(x&: TT, y&: Features);
1769 }
1770 default:
1771 break;
1772 }
1773 }
1774
1775 // If no plain object file inputs exist to pin down the triple then detect
1776 // the host triple and default to that.
1777 auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
1778 LLVM_DEBUG({
1779 dbgs() << "Triple from host-detection: " << JTMB.getTargetTriple().str()
1780 << "\n";
1781 });
1782 return std::make_pair(x&: JTMB.getTargetTriple(), y&: JTMB.getFeatures());
1783 }();
1784
1785 return FirstTTAndFeatures;
1786}
1787
1788static Error sanitizeArguments(const Triple &TT, const char *ArgV0) {
1789
1790 if (InputFiles.empty())
1791 return make_error<StringError>(
1792 Args: "Not enough positional command line arguments specified! (see "
1793 "llvm-jitlink --help)",
1794 Args: inconvertibleErrorCode());
1795
1796 // If we're in replay mode we should never get here.
1797 assert(WaitingOnGraphReplay.empty());
1798
1799 // -noexec and --args should not be used together.
1800 if (NoExec && !InputArgv.empty())
1801 errs() << "Warning: --args passed to -noexec run will be ignored.\n";
1802
1803 // Set the entry point name if not specified.
1804 if (EntryPointName.empty())
1805 EntryPointName = TT.getObjectFormat() == Triple::MachO ? "_main" : "main";
1806
1807 // Disable debugger support by default in noexec tests.
1808 if (DebuggerSupport.getNumOccurrences() == 0 && NoExec)
1809 DebuggerSupport = false;
1810
1811 if (!OrcRuntime.empty() && NoProcessSymbols)
1812 return make_error<StringError>(Args: "-orc-runtime requires process symbols",
1813 Args: inconvertibleErrorCode());
1814
1815 // If -slab-allocate is passed, check that we're not trying to use it in
1816 // -oop-executor or -oop-executor-connect mode.
1817 //
1818 // FIXME: Remove once we enable remote slab allocation.
1819 if (SlabAllocateSizeString != "") {
1820 if (OutOfProcessExecutor.getNumOccurrences() ||
1821 OutOfProcessExecutorConnect.getNumOccurrences())
1822 return make_error<StringError>(
1823 Args: "-slab-allocate cannot be used with -oop-executor or "
1824 "-oop-executor-connect",
1825 Args: inconvertibleErrorCode());
1826 }
1827
1828 // If -slab-address is passed, require -slab-allocate and -noexec
1829 if (SlabAddress != ~0ULL) {
1830 if (SlabAllocateSizeString == "" || !NoExec)
1831 return make_error<StringError>(
1832 Args: "-slab-address requires -slab-allocate and -noexec",
1833 Args: inconvertibleErrorCode());
1834
1835 if (SlabPageSize == 0)
1836 errs() << "Warning: -slab-address used without -slab-page-size.\n";
1837 }
1838
1839 if (SlabPageSize != 0) {
1840 // -slab-page-size requires slab alloc.
1841 if (SlabAllocateSizeString == "")
1842 return make_error<StringError>(Args: "-slab-page-size requires -slab-allocate",
1843 Args: inconvertibleErrorCode());
1844
1845 // Check -slab-page-size / -noexec interactions.
1846 if (!NoExec) {
1847 if (auto RealPageSize = sys::Process::getPageSize()) {
1848 if (SlabPageSize % *RealPageSize)
1849 return make_error<StringError>(
1850 Args: "-slab-page-size must be a multiple of real page size for exec "
1851 "tests (did you mean to use -noexec ?)\n",
1852 Args: inconvertibleErrorCode());
1853 } else {
1854 errs() << "Could not retrieve process page size:\n";
1855 logAllUnhandledErrors(E: RealPageSize.takeError(), OS&: errs(), ErrorBanner: "");
1856 errs() << "Executing with slab page size = "
1857 << formatv(Fmt: "{0:x}", Vals&: SlabPageSize) << ".\n"
1858 << "Tool may crash if " << formatv(Fmt: "{0:x}", Vals&: SlabPageSize)
1859 << " is not a multiple of the real process page size.\n"
1860 << "(did you mean to use -noexec ?)";
1861 }
1862 }
1863 }
1864
1865#if LLVM_ENABLE_THREADS
1866 if (MaterializationThreads == std::numeric_limits<size_t>::max()) {
1867 if (auto HC = std::thread::hardware_concurrency())
1868 MaterializationThreads = HC;
1869 else {
1870 errs() << "Warning: std::thread::hardware_concurrency() returned 0, "
1871 "defaulting to -num-threads=1.\n";
1872 MaterializationThreads = 1;
1873 }
1874 }
1875#else
1876 if (MaterializationThreads.getNumOccurrences() &&
1877 MaterializationThreads != 0) {
1878 errs() << "Warning: -num-threads was set, but LLVM was built with threads "
1879 "disabled. Resetting to -num-threads=0\n";
1880 }
1881 MaterializationThreads = 0;
1882#endif
1883
1884 if (!!OutOfProcessExecutor.getNumOccurrences() ||
1885 !!OutOfProcessExecutorConnect.getNumOccurrences()) {
1886 if (NoExec)
1887 return make_error<StringError>(Args: "-noexec cannot be used with " +
1888 OutOfProcessExecutor.ArgStr + " or " +
1889 OutOfProcessExecutorConnect.ArgStr,
1890 Args: inconvertibleErrorCode());
1891
1892 if (MaterializationThreads == 0)
1893 return make_error<StringError>(Args: "-threads=0 cannot be used with " +
1894 OutOfProcessExecutor.ArgStr + " or " +
1895 OutOfProcessExecutorConnect.ArgStr,
1896 Args: inconvertibleErrorCode());
1897 }
1898
1899#ifndef NDEBUG
1900 if (DebugFlag && MaterializationThreads != 0)
1901 errs() << "Warning: debugging output is not thread safe. "
1902 "Use -num-threads=0 to stabilize output.\n";
1903#endif // NDEBUG
1904
1905 // Only one of -oop-executor and -oop-executor-connect can be used.
1906 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1907 !!OutOfProcessExecutorConnect.getNumOccurrences())
1908 return make_error<StringError>(
1909 Args: "Only one of -" + OutOfProcessExecutor.ArgStr + " and -" +
1910 OutOfProcessExecutorConnect.ArgStr + " can be specified",
1911 Args: inconvertibleErrorCode());
1912
1913 // If -oop-executor was used but no value was specified then use a sensible
1914 // default.
1915 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1916 OutOfProcessExecutor.empty()) {
1917 SmallString<256> OOPExecutorPath(sys::fs::getMainExecutable(
1918 argv0: ArgV0, MainExecAddr: reinterpret_cast<void *>(&sanitizeArguments)));
1919 sys::path::remove_filename(path&: OOPExecutorPath);
1920 sys::path::append(path&: OOPExecutorPath, a: "llvm-jitlink-executor");
1921 OutOfProcessExecutor = OOPExecutorPath.str().str();
1922 }
1923
1924 // If lazy linking is requested then check compatibility with other options.
1925 if (lazyLinkingRequested()) {
1926 if (OrcRuntime.empty())
1927 return make_error<StringError>(Args: "Lazy linking requries the ORC runtime",
1928 Args: inconvertibleErrorCode());
1929
1930 if (!TestHarnesses.empty())
1931 return make_error<StringError>(
1932 Args: "Lazy linking cannot be used with -harness mode",
1933 Args: inconvertibleErrorCode());
1934 } else if (Speculate != SpeculateKind::None) {
1935 errs() << "Warning: -speculate ignored as there are no -lazy inputs\n";
1936 Speculate = SpeculateKind::None;
1937 }
1938
1939 if (Speculate == SpeculateKind::None) {
1940 if (!SpeculateOrder.empty()) {
1941 errs() << "Warning: -speculate-order ignored because speculation is "
1942 "disabled\n";
1943 SpeculateOrder = "";
1944 }
1945
1946 if (!RecordLazyExecs.empty()) {
1947 errs() << "Warning: -record-lazy-execs ignored because speculation is "
1948 "disabled\n";
1949 RecordLazyExecs = "";
1950 }
1951 }
1952
1953 if (!SymbolicateWith.empty()) {
1954 if (!WriteSymbolTableTo.empty())
1955 errs() << WriteSymbolTableTo.ArgStr << " specified with "
1956 << SymbolicateWith.ArgStr << ", ignoring.";
1957 if (InputFiles.empty())
1958 InputFiles.push_back(value: "-");
1959 }
1960
1961 return Error::success();
1962}
1963
1964static void addPhonyExternalsGenerator(Session &S) {
1965 S.MainJD->addGenerator(DefGenerator: std::make_unique<PhonyExternalsGenerator>());
1966}
1967
1968static Error createJITDylibs(Session &S,
1969 std::map<unsigned, JITDylib *> &IdxToJD) {
1970 // First, set up JITDylibs.
1971 LLVM_DEBUG(dbgs() << "Creating JITDylibs...\n");
1972 {
1973 // Create a "main" JITLinkDylib.
1974 IdxToJD[0] = S.MainJD;
1975 S.JDSearchOrder.push_back(x: {S.MainJD, JITDylibLookupFlags::MatchAllSymbols});
1976 LLVM_DEBUG(dbgs() << " 0: " << S.MainJD->getName() << "\n");
1977
1978 // Add any extra JITDylibs from the command line.
1979 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
1980 JDItr != JDEnd; ++JDItr) {
1981 auto JD = S.ES.createJITDylib(Name: *JDItr);
1982 if (!JD)
1983 return JD.takeError();
1984 unsigned JDIdx = JITDylibs.getPosition(optnum: JDItr - JITDylibs.begin());
1985 IdxToJD[JDIdx] = &*JD;
1986 S.JDSearchOrder.push_back(x: {&*JD, JITDylibLookupFlags::MatchAllSymbols});
1987 LLVM_DEBUG(dbgs() << " " << JDIdx << ": " << JD->getName() << "\n");
1988 }
1989 }
1990
1991 if (S.PlatformJD)
1992 S.JDSearchOrder.push_back(
1993 x: {S.PlatformJD, JITDylibLookupFlags::MatchExportedSymbolsOnly});
1994 if (S.ProcessSymsJD)
1995 S.JDSearchOrder.push_back(
1996 x: {S.ProcessSymsJD, JITDylibLookupFlags::MatchExportedSymbolsOnly});
1997
1998 LLVM_DEBUG({
1999 dbgs() << "Dylib search order is [ ";
2000 for (auto &KV : S.JDSearchOrder)
2001 dbgs() << KV.first->getName() << " ";
2002 dbgs() << "]\n";
2003 });
2004
2005 return Error::success();
2006}
2007
2008static Error addAbsoluteSymbols(Session &S,
2009 const std::map<unsigned, JITDylib *> &IdxToJD) {
2010 // Define absolute symbols.
2011 LLVM_DEBUG(dbgs() << "Defining absolute symbols...\n");
2012 for (auto AbsDefItr = AbsoluteDefs.begin(), AbsDefEnd = AbsoluteDefs.end();
2013 AbsDefItr != AbsDefEnd; ++AbsDefItr) {
2014 unsigned AbsDefArgIdx =
2015 AbsoluteDefs.getPosition(optnum: AbsDefItr - AbsoluteDefs.begin());
2016 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: AbsDefArgIdx))->second;
2017
2018 StringRef AbsDefStmt = *AbsDefItr;
2019 size_t EqIdx = AbsDefStmt.find_first_of(C: '=');
2020 if (EqIdx == StringRef::npos)
2021 return make_error<StringError>(Args: "Invalid absolute define \"" + AbsDefStmt +
2022 "\". Syntax: <name>=<addr>",
2023 Args: inconvertibleErrorCode());
2024 StringRef Name = AbsDefStmt.substr(Start: 0, N: EqIdx).trim();
2025 StringRef AddrStr = AbsDefStmt.substr(Start: EqIdx + 1).trim();
2026
2027 uint64_t Addr;
2028 if (AddrStr.getAsInteger(Radix: 0, Result&: Addr))
2029 return make_error<StringError>(Args: "Invalid address expression \"" + AddrStr +
2030 "\" in absolute symbol definition \"" +
2031 AbsDefStmt + "\"",
2032 Args: inconvertibleErrorCode());
2033 ExecutorSymbolDef AbsDef(ExecutorAddr(Addr), JITSymbolFlags::Exported);
2034 auto InternedName = S.ES.intern(SymName: Name);
2035 if (auto Err = JD.define(MU: absoluteSymbols(Symbols: {{InternedName, AbsDef}})))
2036 return Err;
2037
2038 // Register the absolute symbol with the session symbol infos.
2039 S.SymbolInfos[std::move(InternedName)] =
2040 {ArrayRef<char>(), Addr, AbsDef.getFlags().getTargetFlags()};
2041 }
2042
2043 return Error::success();
2044}
2045
2046static Error addAliases(Session &S,
2047 const std::map<unsigned, JITDylib *> &IdxToJD) {
2048 // Define absolute symbols.
2049 LLVM_DEBUG(dbgs() << "Defining aliases...\n");
2050
2051 DenseMap<std::pair<JITDylib *, JITDylib *>, SymbolAliasMap> Reexports;
2052 for (auto AliasItr = Aliases.begin(), AliasEnd = Aliases.end();
2053 AliasItr != AliasEnd; ++AliasItr) {
2054
2055 auto BadExpr = [&]() {
2056 return make_error<StringError>(
2057 Args: "Invalid alias definition \"" + *AliasItr +
2058 "\". Syntax: [<dst-jd>:]<alias>=[<src-jd>:]<aliasee>",
2059 Args: inconvertibleErrorCode());
2060 };
2061
2062 auto GetJD = [&](StringRef JDName) -> Expected<JITDylib *> {
2063 if (JDName.empty()) {
2064 unsigned AliasArgIdx = Aliases.getPosition(optnum: AliasItr - Aliases.begin());
2065 return std::prev(x: IdxToJD.lower_bound(x: AliasArgIdx))->second;
2066 }
2067
2068 auto *JD = S.ES.getJITDylibByName(Name: JDName);
2069 if (!JD)
2070 return make_error<StringError>(Args: StringRef("In alias definition \"") +
2071 *AliasItr + "\" no dylib named " +
2072 JDName,
2073 Args: inconvertibleErrorCode());
2074
2075 return JD;
2076 };
2077
2078 {
2079 // First split on '=' to get alias and aliasee.
2080 StringRef AliasStmt = *AliasItr;
2081 auto [AliasExpr, AliaseeExpr] = AliasStmt.split(Separator: '=');
2082 if (AliaseeExpr.empty())
2083 return BadExpr();
2084
2085 auto [AliasJDName, Alias] = AliasExpr.split(Separator: ':');
2086 if (Alias.empty())
2087 std::swap(a&: AliasJDName, b&: Alias);
2088
2089 auto AliasJD = GetJD(AliasJDName);
2090 if (!AliasJD)
2091 return AliasJD.takeError();
2092
2093 auto [AliaseeJDName, Aliasee] = AliaseeExpr.split(Separator: ':');
2094 if (Aliasee.empty())
2095 std::swap(a&: AliaseeJDName, b&: Aliasee);
2096
2097 if (AliaseeJDName.empty() && !AliasJDName.empty())
2098 AliaseeJDName = AliasJDName;
2099 auto AliaseeJD = GetJD(AliaseeJDName);
2100 if (!AliaseeJD)
2101 return AliaseeJD.takeError();
2102
2103 Reexports[{*AliasJD, *AliaseeJD}][S.ES.intern(SymName: Alias)] = {
2104 S.ES.intern(SymName: Aliasee), JITSymbolFlags::Exported};
2105 }
2106 }
2107
2108 for (auto &[JDs, AliasMap] : Reexports) {
2109 auto [DstJD, SrcJD] = JDs;
2110 if (auto Err = DstJD->define(MU: reexports(SourceJD&: *SrcJD, Aliases: std::move(AliasMap))))
2111 return Err;
2112 }
2113
2114 return Error::success();
2115}
2116
2117static Error addSectCreates(Session &S,
2118 const std::map<unsigned, JITDylib *> &IdxToJD) {
2119 for (auto SCItr = SectCreate.begin(), SCEnd = SectCreate.end();
2120 SCItr != SCEnd; ++SCItr) {
2121
2122 unsigned SCArgIdx = SectCreate.getPosition(optnum: SCItr - SectCreate.begin());
2123 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: SCArgIdx))->second;
2124
2125 StringRef SCArg(*SCItr);
2126
2127 auto [SectAndFileName, ExtraSymbolsString] = SCArg.rsplit(Separator: '@');
2128 auto [SectName, FileName] = SectAndFileName.rsplit(Separator: ',');
2129 if (SectName.empty())
2130 return make_error<StringError>(Args: "In -sectcreate=" + SCArg +
2131 ", filename component cannot be empty",
2132 Args: inconvertibleErrorCode());
2133 if (FileName.empty())
2134 return make_error<StringError>(Args: "In -sectcreate=" + SCArg +
2135 ", filename component cannot be empty",
2136 Args: inconvertibleErrorCode());
2137
2138 auto Content = getFile(FileName);
2139 if (!Content)
2140 return Content.takeError();
2141
2142 SectCreateMaterializationUnit::ExtraSymbolsMap ExtraSymbols;
2143 while (!ExtraSymbolsString.empty()) {
2144 StringRef NextSymPair;
2145 std::tie(args&: NextSymPair, args&: ExtraSymbolsString) = ExtraSymbolsString.split(Separator: ',');
2146
2147 auto [Sym, OffsetString] = NextSymPair.split(Separator: '=');
2148 size_t Offset;
2149
2150 if (OffsetString.getAsInteger(Radix: 0, Result&: Offset))
2151 return make_error<StringError>(Args: "In -sectcreate=" + SCArg + ", " +
2152 OffsetString +
2153 " is not a valid integer",
2154 Args: inconvertibleErrorCode());
2155
2156 ExtraSymbols[S.ES.intern(SymName: Sym)] = {.Flags: JITSymbolFlags::Exported, .Offset: Offset};
2157 }
2158
2159 if (auto Err = JD.define(MU: std::make_unique<SectCreateMaterializationUnit>(
2160 args&: S.ObjLayer, args: SectName.str(), args: MemProt::Read, args: 16, args: std::move(*Content),
2161 args: std::move(ExtraSymbols))))
2162 return Err;
2163 }
2164
2165 return Error::success();
2166}
2167
2168static Error addTestHarnesses(Session &S) {
2169 LLVM_DEBUG(dbgs() << "Adding test harness objects...\n");
2170 for (auto HarnessFile : TestHarnesses) {
2171 LLVM_DEBUG(dbgs() << " " << HarnessFile << "\n");
2172 auto Linkable = loadLinkableFile(Path: HarnessFile, TT: S.ES.getTargetTriple(),
2173 LA: LoadArchives::Never);
2174 if (!Linkable)
2175 return Linkable.takeError();
2176 if (auto Err = S.ObjLayer.add(JD&: *S.MainJD, O: std::move(Linkable->first)))
2177 return Err;
2178 }
2179 return Error::success();
2180}
2181
2182static Error addObjects(Session &S,
2183 const std::map<unsigned, JITDylib *> &IdxToJD,
2184 const DenseSet<unsigned> &LazyLinkIdxs) {
2185
2186 // Load each object into the corresponding JITDylib..
2187 LLVM_DEBUG(dbgs() << "Adding objects...\n");
2188 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
2189 InputFileItr != InputFileEnd; ++InputFileItr) {
2190 unsigned InputFileArgIdx =
2191 InputFiles.getPosition(optnum: InputFileItr - InputFiles.begin());
2192 const std::string &InputFile = *InputFileItr;
2193 if (StringRef(InputFile).ends_with(Suffix: ".a") ||
2194 StringRef(InputFile).ends_with(Suffix: ".lib"))
2195 continue;
2196 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: InputFileArgIdx))->second;
2197 bool AddLazy = LazyLinkIdxs.count(V: InputFileArgIdx);
2198 LLVM_DEBUG(dbgs() << " " << InputFileArgIdx << ": \"" << InputFile << "\" "
2199 << (AddLazy ? " (lazy-linked)" : "") << " to "
2200 << JD.getName() << "\n";);
2201 auto ObjBuffer = loadLinkableFile(Path: InputFile, TT: S.ES.getTargetTriple(),
2202 LA: LoadArchives::Never);
2203 if (!ObjBuffer)
2204 return ObjBuffer.takeError();
2205
2206 if (S.HarnessFiles.empty()) {
2207 if (auto Err =
2208 S.getLinkLayer(Lazy: AddLazy).add(JD, O: std::move(ObjBuffer->first)))
2209 return Err;
2210 } else {
2211 // We're in -harness mode. Use a custom interface for this
2212 // test object.
2213 auto ObjInterface =
2214 getTestObjectFileInterface(S, O: ObjBuffer->first->getMemBufferRef());
2215 if (!ObjInterface)
2216 return ObjInterface.takeError();
2217
2218 if (auto Err = S.ObjLayer.add(JD, O: std::move(ObjBuffer->first),
2219 I: std::move(*ObjInterface)))
2220 return Err;
2221 }
2222 }
2223
2224 return Error::success();
2225}
2226
2227static Expected<MaterializationUnit::Interface>
2228getObjectFileInterfaceHidden(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
2229 auto I = getObjectFileInterface(ES, ObjBuffer);
2230 if (I) {
2231 for (auto &KV : I->SymbolFlags)
2232 KV.second &= ~JITSymbolFlags::Exported;
2233 }
2234 return I;
2235}
2236
2237static SmallVector<StringRef, 5> getSearchPathsFromEnvVar(Session &S) {
2238 // FIXME: Handle EPC environment.
2239 SmallVector<StringRef, 5> PathVec;
2240 auto TT = S.ES.getTargetTriple();
2241 if (TT.isOSBinFormatCOFF())
2242 StringRef(getenv(name: "PATH")).split(A&: PathVec, Separator: ";");
2243 else if (TT.isOSBinFormatELF())
2244 StringRef(getenv(name: "LD_LIBRARY_PATH")).split(A&: PathVec, Separator: ":");
2245
2246 return PathVec;
2247}
2248
2249static Expected<std::unique_ptr<DefinitionGenerator>>
2250LoadLibraryWeak(Session &S, StringRef Path) {
2251 auto Symbols = getDylibInterface(ES&: S.ES, Path);
2252 if (!Symbols)
2253 return Symbols.takeError();
2254
2255 return std::make_unique<EPCDynamicLibrarySearchGenerator>(
2256 args&: S.ES, args&: *S.DylibMgr,
2257 args: [Symbols = std::move(*Symbols)](const SymbolStringPtr &Sym) {
2258 return Symbols.count(V: Sym);
2259 });
2260}
2261
2262static Error addLibraries(Session &S,
2263 const std::map<unsigned, JITDylib *> &IdxToJD,
2264 const DenseSet<unsigned> &LazyLinkIdxs) {
2265
2266 // 1. Collect search paths for each JITDylib.
2267 DenseMap<const JITDylib *, SmallVector<StringRef, 2>> JDSearchPaths;
2268
2269 for (auto LSPItr = LibrarySearchPaths.begin(),
2270 LSPEnd = LibrarySearchPaths.end();
2271 LSPItr != LSPEnd; ++LSPItr) {
2272 unsigned LibrarySearchPathIdx =
2273 LibrarySearchPaths.getPosition(optnum: LSPItr - LibrarySearchPaths.begin());
2274 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: LibrarySearchPathIdx))->second;
2275
2276 StringRef LibrarySearchPath = *LSPItr;
2277 if (sys::fs::get_file_type(Path: LibrarySearchPath) !=
2278 sys::fs::file_type::directory_file)
2279 return make_error<StringError>(Args: "While linking " + JD.getName() + ", -L" +
2280 LibrarySearchPath +
2281 " does not point to a directory",
2282 Args: inconvertibleErrorCode());
2283
2284 JDSearchPaths[&JD].push_back(Elt: *LSPItr);
2285 }
2286
2287 LLVM_DEBUG({
2288 if (!JDSearchPaths.empty())
2289 dbgs() << "Search paths:\n";
2290 for (auto &KV : JDSearchPaths) {
2291 dbgs() << " " << KV.first->getName() << ": [";
2292 for (auto &LibSearchPath : KV.second)
2293 dbgs() << " \"" << LibSearchPath << "\"";
2294 dbgs() << " ]\n";
2295 }
2296 });
2297
2298 // 2. Collect library loads
2299 struct LibraryLoad {
2300 std::string LibName;
2301 bool IsPath = false;
2302 unsigned Position;
2303 ArrayRef<StringRef> CandidateExtensions;
2304 enum { Standard, Hidden, Weak } Modifier;
2305 };
2306
2307 // Queue to load library as in the order as it appears in the argument list.
2308 std::deque<LibraryLoad> LibraryLoadQueue;
2309
2310 // Add archive files from the inputs to LibraryLoads.
2311 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
2312 InputFileItr != InputFileEnd; ++InputFileItr) {
2313 StringRef InputFile = *InputFileItr;
2314 if (!InputFile.ends_with(Suffix: ".a") && !InputFile.ends_with(Suffix: ".lib"))
2315 continue;
2316 LibraryLoad LL;
2317 LL.LibName = InputFile.str();
2318 LL.IsPath = true;
2319 LL.Position = InputFiles.getPosition(optnum: InputFileItr - InputFiles.begin());
2320 LL.CandidateExtensions = {};
2321 LL.Modifier = LibraryLoad::Standard;
2322 LibraryLoadQueue.push_back(x: std::move(LL));
2323 }
2324
2325 // Add -load_hidden arguments to LibraryLoads.
2326 for (auto LibItr = LoadHidden.begin(), LibEnd = LoadHidden.end();
2327 LibItr != LibEnd; ++LibItr) {
2328 LibraryLoad LL;
2329 LL.LibName = *LibItr;
2330 LL.IsPath = true;
2331 LL.Position = LoadHidden.getPosition(optnum: LibItr - LoadHidden.begin());
2332 LL.CandidateExtensions = {};
2333 LL.Modifier = LibraryLoad::Hidden;
2334 LibraryLoadQueue.push_back(x: std::move(LL));
2335 }
2336
2337 // Add -weak_library arguments to LibraryLoads.
2338 for (auto LibItr = WeakLibraries.begin(), LibEnd = WeakLibraries.end();
2339 LibItr != LibEnd; ++LibItr) {
2340 LibraryLoad LL;
2341 LL.LibName = *LibItr;
2342 LL.IsPath = true;
2343 LL.Position = WeakLibraries.getPosition(optnum: LibItr - WeakLibraries.begin());
2344 LL.CandidateExtensions = {};
2345 LL.Modifier = LibraryLoad::Weak;
2346 LibraryLoadQueue.push_back(x: std::move(LL));
2347 }
2348
2349 StringRef StandardExtensions[] = {".so", ".dylib", ".dll", ".a", ".lib"};
2350 StringRef DynLibExtensionsOnly[] = {".so", ".dylib", ".dll"};
2351 StringRef ArchiveExtensionsOnly[] = {".a", ".lib"};
2352 StringRef WeakLinkExtensionsOnly[] = {".dylib", ".tbd"};
2353
2354 // Add -lx arguments to LibraryLoads.
2355 for (auto LibItr = Libraries.begin(), LibEnd = Libraries.end();
2356 LibItr != LibEnd; ++LibItr) {
2357 LibraryLoad LL;
2358 LL.LibName = *LibItr;
2359 LL.Position = Libraries.getPosition(optnum: LibItr - Libraries.begin());
2360 LL.CandidateExtensions = StandardExtensions;
2361 LL.Modifier = LibraryLoad::Standard;
2362 LibraryLoadQueue.push_back(x: std::move(LL));
2363 }
2364
2365 // Add -hidden-lx arguments to LibraryLoads.
2366 for (auto LibHiddenItr = LibrariesHidden.begin(),
2367 LibHiddenEnd = LibrariesHidden.end();
2368 LibHiddenItr != LibHiddenEnd; ++LibHiddenItr) {
2369 LibraryLoad LL;
2370 LL.LibName = *LibHiddenItr;
2371 LL.Position =
2372 LibrariesHidden.getPosition(optnum: LibHiddenItr - LibrariesHidden.begin());
2373 LL.CandidateExtensions = ArchiveExtensionsOnly;
2374 LL.Modifier = LibraryLoad::Hidden;
2375 LibraryLoadQueue.push_back(x: std::move(LL));
2376 }
2377
2378 // Add -weak-lx arguments to LibraryLoads.
2379 for (auto LibWeakItr = LibrariesWeak.begin(),
2380 LibWeakEnd = LibrariesWeak.end();
2381 LibWeakItr != LibWeakEnd; ++LibWeakItr) {
2382 LibraryLoad LL;
2383 LL.LibName = *LibWeakItr;
2384 LL.Position = LibrariesWeak.getPosition(optnum: LibWeakItr - LibrariesWeak.begin());
2385 LL.CandidateExtensions = WeakLinkExtensionsOnly;
2386 LL.Modifier = LibraryLoad::Weak;
2387 LibraryLoadQueue.push_back(x: std::move(LL));
2388 }
2389
2390 // Sort library loads by position in the argument list.
2391 llvm::sort(C&: LibraryLoadQueue,
2392 Comp: [](const LibraryLoad &LHS, const LibraryLoad &RHS) {
2393 return LHS.Position < RHS.Position;
2394 });
2395
2396 // 3. Process library loads.
2397 auto AddArchive = [&](JITDylib &JD, const char *Path, const LibraryLoad &LL)
2398 -> Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> {
2399 StaticLibraryDefinitionGenerator::GetObjectFileInterface
2400 GetObjFileInterface;
2401 switch (LL.Modifier) {
2402 case LibraryLoad::Standard:
2403 GetObjFileInterface = getObjectFileInterface;
2404 break;
2405 case LibraryLoad::Hidden:
2406 GetObjFileInterface = getObjectFileInterfaceHidden;
2407 S.HiddenArchives.insert(key: Path);
2408 break;
2409 case LibraryLoad::Weak:
2410 llvm_unreachable("Unsupported");
2411 break;
2412 }
2413
2414 auto &LinkLayer = S.getLinkLayer(Lazy: LazyLinkIdxs.count(V: LL.Position));
2415
2416 std::set<std::string> ImportedDynamicLibraries;
2417 StaticLibraryDefinitionGenerator::VisitMembersFunction VisitMembers;
2418
2419 // COFF gets special handling due to import libraries.
2420 if (S.ES.getTargetTriple().isOSBinFormatCOFF()) {
2421 if (AllLoad) {
2422 VisitMembers =
2423 [ImportScanner = COFFImportFileScanner(ImportedDynamicLibraries),
2424 LoadAll =
2425 StaticLibraryDefinitionGenerator::loadAllObjectFileMembers(
2426 L&: LinkLayer, JD)](object::Archive &A,
2427 MemoryBufferRef MemberBuf,
2428 size_t Index) mutable -> Expected<bool> {
2429 if (!ImportScanner(A, MemberBuf, Index))
2430 return false;
2431 return LoadAll(A, MemberBuf, Index);
2432 };
2433 } else
2434 VisitMembers = COFFImportFileScanner(ImportedDynamicLibraries);
2435 } else if (AllLoad)
2436 VisitMembers = StaticLibraryDefinitionGenerator::loadAllObjectFileMembers(
2437 L&: LinkLayer, JD);
2438 else if (S.ES.getTargetTriple().isOSBinFormatMachO() && ForceLoadObjC)
2439 VisitMembers = ForceLoadMachOArchiveMembers(LinkLayer, JD, true);
2440
2441 auto G = StaticLibraryDefinitionGenerator::Load(
2442 L&: LinkLayer, FileName: Path, VisitMembers: std::move(VisitMembers),
2443 GetObjFileInterface: std::move(GetObjFileInterface));
2444 if (!G)
2445 return G.takeError();
2446
2447 // Push additional dynamic libraries to search.
2448 // Note that this mechanism only happens in COFF.
2449 for (auto FileName : ImportedDynamicLibraries) {
2450 LibraryLoad NewLL;
2451 auto FileNameRef = StringRef(FileName);
2452 if (!FileNameRef.ends_with_insensitive(Suffix: ".dll"))
2453 return make_error<StringError>(
2454 Args: "COFF Imported library not ending with dll extension?",
2455 Args: inconvertibleErrorCode());
2456 NewLL.LibName = FileNameRef.drop_back(N: strlen(s: ".dll")).str();
2457 NewLL.Position = LL.Position;
2458 NewLL.CandidateExtensions = DynLibExtensionsOnly;
2459 NewLL.Modifier = LibraryLoad::Standard;
2460 LibraryLoadQueue.push_front(x: std::move(NewLL));
2461 }
2462 return G;
2463 };
2464
2465 SmallVector<StringRef, 5> SystemSearchPaths;
2466 if (SearchSystemLibrary.getValue())
2467 SystemSearchPaths = getSearchPathsFromEnvVar(S);
2468 while (!LibraryLoadQueue.empty()) {
2469 bool LibFound = false;
2470 auto LL = LibraryLoadQueue.front();
2471 LibraryLoadQueue.pop_front();
2472 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: LL.Position))->second;
2473
2474 // If this is the name of a JITDylib then link against that.
2475 if (auto *LJD = S.ES.getJITDylibByName(Name: LL.LibName)) {
2476 if (LL.Modifier == LibraryLoad::Weak)
2477 return make_error<StringError>(
2478 Args: "Can't use -weak-lx or -weak_library to load JITDylib " +
2479 LL.LibName,
2480 Args: inconvertibleErrorCode());
2481 JD.addToLinkOrder(JD&: *LJD);
2482 continue;
2483 }
2484
2485 if (LL.IsPath) {
2486 // Must be -weak_library.
2487 if (LL.Modifier == LibraryLoad::Weak) {
2488 if (auto G = LoadLibraryWeak(S, Path: LL.LibName)) {
2489 JD.addGenerator(DefGenerator: std::move(*G));
2490 continue;
2491 } else
2492 return G.takeError();
2493 }
2494
2495 // Otherwise handle archive.
2496 auto G = AddArchive(JD, LL.LibName.c_str(), LL);
2497 if (!G)
2498 return createFileError(F: LL.LibName, E: G.takeError());
2499 JD.addGenerator(DefGenerator: std::move(*G));
2500 LLVM_DEBUG({
2501 dbgs() << "Adding generator for static library " << LL.LibName << " to "
2502 << JD.getName() << "\n";
2503 });
2504 continue;
2505 }
2506
2507 // Otherwise look through the search paths.
2508 auto CurJDSearchPaths = JDSearchPaths[&JD];
2509 for (StringRef SearchPath :
2510 concat<StringRef>(Ranges&: CurJDSearchPaths, Ranges&: SystemSearchPaths)) {
2511 for (auto LibExt : LL.CandidateExtensions) {
2512 SmallVector<char, 256> LibPath;
2513 LibPath.reserve(N: SearchPath.size() + strlen(s: "lib") + LL.LibName.size() +
2514 LibExt.size() + 2); // +2 for pathsep, null term.
2515 llvm::append_range(C&: LibPath, R&: SearchPath);
2516 if (LibExt != ".lib" && LibExt != ".dll")
2517 sys::path::append(path&: LibPath, a: "lib" + LL.LibName + LibExt);
2518 else
2519 sys::path::append(path&: LibPath, a: LL.LibName + LibExt);
2520 LibPath.push_back(Elt: '\0');
2521
2522 // Skip missing or non-regular paths.
2523 if (sys::fs::get_file_type(Path: LibPath.data()) !=
2524 sys::fs::file_type::regular_file) {
2525 continue;
2526 }
2527
2528 file_magic Magic;
2529 if (auto EC = identify_magic(path: LibPath, result&: Magic)) {
2530 // If there was an error loading the file then skip it.
2531 LLVM_DEBUG({
2532 dbgs() << "Library search found \"" << LibPath
2533 << "\", but could not identify file type (" << EC.message()
2534 << "). Skipping.\n";
2535 });
2536 continue;
2537 }
2538
2539 // We identified the magic. Assume that we can load it -- we'll reset
2540 // in the default case.
2541 LibFound = true;
2542 switch (Magic) {
2543 case file_magic::pecoff_executable:
2544 case file_magic::elf_shared_object:
2545 case file_magic::macho_dynamically_linked_shared_lib: {
2546 if (LL.Modifier == LibraryLoad::Weak) {
2547 if (auto G = LoadLibraryWeak(S, Path: LibPath.data()))
2548 JD.addGenerator(DefGenerator: std::move(*G));
2549 else
2550 return G.takeError();
2551 } else {
2552 if (auto Err = S.loadAndLinkDynamicLibrary(JD, LibPath: LibPath.data()))
2553 return Err;
2554 }
2555 break;
2556 }
2557 case file_magic::archive:
2558 case file_magic::macho_universal_binary: {
2559 auto G = AddArchive(JD, LibPath.data(), LL);
2560 if (!G)
2561 return G.takeError();
2562 JD.addGenerator(DefGenerator: std::move(*G));
2563 LLVM_DEBUG({
2564 dbgs() << "Adding generator for static library " << LibPath.data()
2565 << " to " << JD.getName() << "\n";
2566 });
2567 break;
2568 }
2569 case file_magic::tapi_file:
2570 assert(LL.Modifier == LibraryLoad::Weak &&
2571 "TextAPI file not being loaded as weak?");
2572 if (auto G = LoadLibraryWeak(S, Path: LibPath.data()))
2573 JD.addGenerator(DefGenerator: std::move(*G));
2574 else
2575 return G.takeError();
2576 break;
2577 default:
2578 // This file isn't a recognized library kind.
2579 LLVM_DEBUG({
2580 dbgs() << "Library search found \"" << LibPath
2581 << "\", but file type is not supported. Skipping.\n";
2582 });
2583 LibFound = false;
2584 break;
2585 }
2586 if (LibFound)
2587 break;
2588 }
2589 if (LibFound)
2590 break;
2591 }
2592
2593 if (!LibFound)
2594 return make_error<StringError>(Args: "While linking " + JD.getName() +
2595 ", could not find library for -l" +
2596 LL.LibName,
2597 Args: inconvertibleErrorCode());
2598 }
2599
2600 // Add platform and process symbols if available.
2601 for (auto &[Idx, JD] : IdxToJD) {
2602 if (S.PlatformJD)
2603 JD->addToLinkOrder(JD&: *S.PlatformJD);
2604 if (S.ProcessSymsJD)
2605 JD->addToLinkOrder(JD&: *S.ProcessSymsJD);
2606 }
2607
2608 return Error::success();
2609}
2610
2611static Error addSpeculationOrder(Session &S) {
2612
2613 if (SpeculateOrder.empty())
2614 return Error::success();
2615
2616 assert(S.LazyLinking && "SpeculateOrder set, but lazy linking not enabled");
2617 assert(S.LazyLinking->Speculator && "SpeculatoOrder set, but no speculator");
2618
2619 auto SpecOrderBuffer = getFile(FileName: SpeculateOrder);
2620 if (!SpecOrderBuffer)
2621 return SpecOrderBuffer.takeError();
2622
2623 StringRef LineStream((*SpecOrderBuffer)->getBuffer());
2624 std::vector<std::pair<std::string, SymbolStringPtr>> SpecOrder;
2625
2626 size_t LineNumber = 0;
2627 while (!LineStream.empty()) {
2628 ++LineNumber;
2629
2630 auto MakeSpecOrderErr = [&](StringRef Reason) {
2631 return make_error<StringError>(Args: "Error in speculation order file \"" +
2632 SpeculateOrder + "\" on line " +
2633 Twine(LineNumber) + ": " + Reason,
2634 Args: inconvertibleErrorCode());
2635 };
2636
2637 StringRef CurLine;
2638 std::tie(args&: CurLine, args&: LineStream) = LineStream.split(Separator: '\n');
2639 CurLine = CurLine.trim();
2640 if (CurLine.empty())
2641 continue;
2642
2643 auto [JDName, FuncName] = CurLine.split(Separator: ',');
2644
2645 if (FuncName.empty())
2646 return MakeSpecOrderErr("missing ',' separator");
2647
2648 JDName = JDName.trim();
2649 if (JDName.empty())
2650 return MakeSpecOrderErr("no value for column 1 (JIT Dylib name)");
2651
2652 FuncName = FuncName.trim();
2653 if (FuncName.empty())
2654 return MakeSpecOrderErr("no value for column 2 (function name)");
2655
2656 SpecOrder.push_back(x: {JDName.str(), S.ES.intern(SymName: FuncName)});
2657 }
2658
2659 S.LazyLinking->Speculator->addSpeculationSuggestions(NewSuggestions: std::move(SpecOrder));
2660
2661 return Error::success();
2662}
2663
2664static Error addSessionInputs(Session &S) {
2665 std::map<unsigned, JITDylib *> IdxToJD;
2666 DenseSet<unsigned> LazyLinkIdxs;
2667
2668 for (auto LLItr = LazyLink.begin(), LLEnd = LazyLink.end(); LLItr != LLEnd;
2669 ++LLItr) {
2670 if (*LLItr)
2671 LazyLinkIdxs.insert(V: LazyLink.getPosition(optnum: LLItr - LazyLink.begin()) + 1);
2672 }
2673
2674 if (auto Err = createJITDylibs(S, IdxToJD))
2675 return Err;
2676
2677 if (auto Err = addAbsoluteSymbols(S, IdxToJD))
2678 return Err;
2679
2680 if (auto Err = addAliases(S, IdxToJD))
2681 return Err;
2682
2683 if (auto Err = addSectCreates(S, IdxToJD))
2684 return Err;
2685
2686 if (!TestHarnesses.empty())
2687 if (auto Err = addTestHarnesses(S))
2688 return Err;
2689
2690 if (auto Err = addObjects(S, IdxToJD, LazyLinkIdxs))
2691 return Err;
2692
2693 if (auto Err = addLibraries(S, IdxToJD, LazyLinkIdxs))
2694 return Err;
2695
2696 if (auto Err = addSpeculationOrder(S))
2697 return Err;
2698
2699 return Error::success();
2700}
2701
2702namespace {
2703struct TargetInfo {
2704 const Target *TheTarget;
2705 std::unique_ptr<MCSubtargetInfo> STI;
2706 std::unique_ptr<MCRegisterInfo> MRI;
2707 std::unique_ptr<MCAsmInfo> MAI;
2708 std::unique_ptr<MCContext> Ctx;
2709 std::unique_ptr<MCDisassembler> Disassembler;
2710 std::unique_ptr<MCInstrInfo> MII;
2711 std::unique_ptr<MCInstrAnalysis> MIA;
2712 std::unique_ptr<MCInstPrinter> InstPrinter;
2713};
2714} // anonymous namespace
2715
2716static TargetInfo
2717getTargetInfo(const Triple &TT,
2718 const SubtargetFeatures &TF = SubtargetFeatures()) {
2719 std::string ErrorStr;
2720 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple: TT, Error&: ErrorStr);
2721 if (!TheTarget)
2722 ExitOnErr(make_error<StringError>(Args: "Error accessing target '" + TT.str() +
2723 "': " + ErrorStr,
2724 Args: inconvertibleErrorCode()));
2725
2726 std::unique_ptr<MCSubtargetInfo> STI(
2727 TheTarget->createMCSubtargetInfo(TheTriple: TT, CPU: "", Features: TF.getString()));
2728 if (!STI)
2729 ExitOnErr(
2730 make_error<StringError>(Args: "Unable to create subtarget for " + TT.str(),
2731 Args: inconvertibleErrorCode()));
2732
2733 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
2734 if (!MRI)
2735 ExitOnErr(make_error<StringError>(Args: "Unable to create target register info "
2736 "for " +
2737 TT.str(),
2738 Args: inconvertibleErrorCode()));
2739
2740 MCTargetOptions MCOptions;
2741 std::unique_ptr<MCAsmInfo> MAI(
2742 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple: TT, Options: MCOptions));
2743 if (!MAI)
2744 ExitOnErr(
2745 make_error<StringError>(Args: "Unable to create target asm info " + TT.str(),
2746 Args: inconvertibleErrorCode()));
2747
2748 auto Ctx = std::make_unique<MCContext>(args: Triple(TT.str()), args: MAI.get(), args: MRI.get(),
2749 args: STI.get());
2750
2751 std::unique_ptr<MCDisassembler> Disassembler(
2752 TheTarget->createMCDisassembler(STI: *STI, Ctx&: *Ctx));
2753 if (!Disassembler)
2754 ExitOnErr(
2755 make_error<StringError>(Args: "Unable to create disassembler for " + TT.str(),
2756 Args: inconvertibleErrorCode()));
2757
2758 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
2759 if (!MII)
2760 ExitOnErr(make_error<StringError>(Args: "Unable to create instruction info for" +
2761 TT.str(),
2762 Args: inconvertibleErrorCode()));
2763
2764 std::unique_ptr<MCInstrAnalysis> MIA(
2765 TheTarget->createMCInstrAnalysis(Info: MII.get()));
2766 if (!MIA)
2767 ExitOnErr(make_error<StringError>(
2768 Args: "Unable to create instruction analysis for" + TT.str(),
2769 Args: inconvertibleErrorCode()));
2770
2771 std::unique_ptr<MCInstPrinter> InstPrinter(
2772 TheTarget->createMCInstPrinter(T: Triple(TT.str()), SyntaxVariant: 0, MAI: *MAI, MII: *MII, MRI: *MRI));
2773 if (!InstPrinter)
2774 ExitOnErr(make_error<StringError>(
2775 Args: "Unable to create instruction printer for" + TT.str(),
2776 Args: inconvertibleErrorCode()));
2777 return {.TheTarget: TheTarget, .STI: std::move(STI), .MRI: std::move(MRI),
2778 .MAI: std::move(MAI), .Ctx: std::move(Ctx), .Disassembler: std::move(Disassembler),
2779 .MII: std::move(MII), .MIA: std::move(MIA), .InstPrinter: std::move(InstPrinter)};
2780}
2781static Error runChecks(Session &S, Triple TT, SubtargetFeatures Features) {
2782 if (CheckFiles.empty())
2783 return Error::success();
2784
2785 S.waitForFilesLinkedFromEntryPointFile();
2786
2787 LLVM_DEBUG(dbgs() << "Running checks...\n");
2788
2789 auto IsSymbolValid = [&S](StringRef Symbol) {
2790 auto InternedSymbol = S.ES.intern(SymName: Symbol);
2791 return S.isSymbolRegistered(SymbolName: InternedSymbol);
2792 };
2793
2794 auto GetSymbolInfo = [&S](StringRef Symbol) {
2795 auto InternedSymbol = S.ES.intern(SymName: Symbol);
2796 return S.findSymbolInfo(SymbolName: InternedSymbol, ErrorMsgStem: "Can not get symbol info");
2797 };
2798
2799 auto GetSectionInfo = [&S](StringRef FileName, StringRef SectionName) {
2800 return S.findSectionInfo(FileName, SectionName);
2801 };
2802
2803 auto GetStubInfo = [&S](StringRef FileName, StringRef SectionName,
2804 StringRef KindNameFilter) {
2805 return S.findStubInfo(FileName, TargetName: SectionName, KindNameFilter);
2806 };
2807
2808 auto GetGOTInfo = [&S](StringRef FileName, StringRef SectionName) {
2809 return S.findGOTEntryInfo(FileName, TargetName: SectionName);
2810 };
2811
2812 RuntimeDyldChecker Checker(
2813 IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo, GetGOTInfo,
2814 S.ES.getTargetTriple().isLittleEndian() ? llvm::endianness::little
2815 : llvm::endianness::big,
2816 TT, StringRef(), Features, dbgs());
2817
2818 std::string CheckLineStart = "# " + CheckName + ":";
2819 for (auto &CheckFile : CheckFiles) {
2820 auto CheckerFileBuf = ExitOnErr(getFile(FileName: CheckFile));
2821 if (!Checker.checkAllRulesInBuffer(RulePrefix: CheckLineStart, MemBuf: &*CheckerFileBuf))
2822 ExitOnErr(make_error<StringError>(
2823 Args: "Some checks in " + CheckFile + " failed", Args: inconvertibleErrorCode()));
2824 }
2825
2826 return Error::success();
2827}
2828
2829static Error addSelfRelocations(LinkGraph &G) {
2830 auto TI = getTargetInfo(TT: G.getTargetTriple());
2831 for (auto *Sym : G.defined_symbols())
2832 if (Sym->isCallable())
2833 if (auto Err = addFunctionPointerRelocationsToCurrentSymbol(
2834 Sym&: *Sym, G, Disassembler&: *TI.Disassembler, MIA&: *TI.MIA))
2835 return Err;
2836 return Error::success();
2837}
2838
2839static Expected<ExecutorSymbolDef> getMainEntryPoint(Session &S) {
2840 return S.ES.lookup(SearchOrder: S.JDSearchOrder, Symbol: S.ES.intern(SymName: EntryPointName));
2841}
2842
2843static Expected<ExecutorSymbolDef> getOrcRuntimeEntryPoint(Session &S) {
2844 std::string RuntimeEntryPoint = "__orc_rt_run_program_wrapper";
2845 if (S.ES.getTargetTriple().getObjectFormat() == Triple::MachO)
2846 RuntimeEntryPoint = '_' + RuntimeEntryPoint;
2847 return S.ES.lookup(SearchOrder: S.JDSearchOrder, Symbol: S.ES.intern(SymName: RuntimeEntryPoint));
2848}
2849
2850static Expected<ExecutorSymbolDef> getEntryPoint(Session &S) {
2851 ExecutorSymbolDef EntryPoint;
2852
2853 // Find the entry-point function unconditionally, since we want to force
2854 // it to be materialized to collect stats.
2855 if (auto EP = getMainEntryPoint(S))
2856 EntryPoint = *EP;
2857 else
2858 return EP.takeError();
2859 LLVM_DEBUG({
2860 dbgs() << "Using entry point \"" << EntryPointName
2861 << "\": " << formatv("{0:x16}", EntryPoint.getAddress()) << "\n";
2862 });
2863
2864 // If we're running with the ORC runtime then replace the entry-point
2865 // with the __orc_rt_run_program symbol.
2866 if (!OrcRuntime.empty()) {
2867 if (auto EP = getOrcRuntimeEntryPoint(S))
2868 EntryPoint = *EP;
2869 else
2870 return EP.takeError();
2871 LLVM_DEBUG({
2872 dbgs() << "(called via __orc_rt_run_program_wrapper at "
2873 << formatv("{0:x16}", EntryPoint.getAddress()) << ")\n";
2874 });
2875 }
2876
2877 return EntryPoint;
2878}
2879
2880static Expected<int> runWithRuntime(Session &S, ExecutorAddr EntryPointAddr) {
2881 StringRef DemangledEntryPoint = EntryPointName;
2882 if (S.ES.getTargetTriple().getObjectFormat() == Triple::MachO &&
2883 DemangledEntryPoint.front() == '_')
2884 DemangledEntryPoint = DemangledEntryPoint.drop_front();
2885 using llvm::orc::shared::SPSString;
2886 using SPSRunProgramSig =
2887 int64_t(SPSString, SPSString, shared::SPSSequence<SPSString>);
2888 int64_t Result;
2889 if (auto Err = S.ES.callSPSWrapper<SPSRunProgramSig>(
2890 WrapperFnAddr: EntryPointAddr, WrapperCallArgs&: Result, WrapperCallArgs: S.MainJD->getName(), WrapperCallArgs&: DemangledEntryPoint,
2891 WrapperCallArgs&: static_cast<std::vector<std::string> &>(InputArgv)))
2892 return std::move(Err);
2893 return Result;
2894}
2895
2896static Expected<int> runWithoutRuntime(Session &S,
2897 ExecutorAddr EntryPointAddr) {
2898 return S.ES.getExecutorProcessControl().runAsMain(MainFnAddr: EntryPointAddr, Args: InputArgv);
2899}
2900
2901static Error symbolicateBacktraces() {
2902 auto Symtab = DumpedSymbolTable::Create(Path: SymbolicateWith);
2903 if (!Symtab)
2904 return Symtab.takeError();
2905
2906 for (auto InputFile : InputFiles) {
2907 auto BacktraceBuffer = MemoryBuffer::getFileOrSTDIN(Filename: InputFile);
2908 if (!BacktraceBuffer)
2909 return createFileError(F: InputFile, EC: BacktraceBuffer.getError());
2910
2911 outs() << Symtab->symbolicate(Backtrace: (*BacktraceBuffer)->getBuffer());
2912 }
2913
2914 return Error::success();
2915}
2916
2917static Error waitingOnGraphReplay() {
2918 // Warn about ignored options.
2919 {
2920 bool PrintedHeader = false;
2921 for (auto &[OptName, Opt] : cl::getRegisteredOptions()) {
2922 if (Opt == &WaitingOnGraphReplay)
2923 continue;
2924 if (Opt->getNumOccurrences()) {
2925 if (!PrintedHeader) {
2926 errs() << "Warning: Running in -waiting-on-graph-replay mode. "
2927 "The following options will be ignored:\n";
2928 PrintedHeader = true;
2929 }
2930 errs() << " " << OptName << "\n";
2931 }
2932 }
2933 }
2934
2935 // Read the replay buffer file.
2936 auto GraphOpsBuffer = getFile(FileName: WaitingOnGraphReplay);
2937 if (!GraphOpsBuffer)
2938 return GraphOpsBuffer.takeError();
2939
2940 using Replay = orc::detail::WaitingOnGraphOpReplay<uintptr_t, uintptr_t>;
2941 using Graph = typename Replay::Graph;
2942 using Replayer = typename Replay::Replayer;
2943
2944 std::vector<typename Replay::Op> RecordedOps;
2945
2946 // First read the buffer to build the Ops vector. Doing this up-front allows
2947 // us to avoid polluting the timings below with the cost of parsing.
2948 Error Err = Error::success();
2949 for (auto &Op :
2950 orc::detail::readWaitingOnGraphOpsFromBuffer<uintptr_t, uintptr_t>(
2951 InputBuffer: (*GraphOpsBuffer)->getBuffer(), Err))
2952 RecordedOps.push_back(x: std::move(Op));
2953 if (Err)
2954 return Err;
2955
2956 // Now replay the Ops:
2957 Graph G;
2958 Replayer R(G);
2959
2960 outs() << "Replaying WaitingOnGraph operations from " << WaitingOnGraphReplay
2961 << "...\n";
2962 auto ReplayStart = std::chrono::high_resolution_clock::now();
2963 for (auto &Op : RecordedOps)
2964 R.replay(O: std::move(Op));
2965 auto ReplayEnd = std::chrono::high_resolution_clock::now();
2966 std::chrono::duration<double> ReplayDiff = ReplayEnd - ReplayStart;
2967 outs() << ReplayDiff.count() << "s to replay " << RecordedOps.size()
2968 << " ops (wall-clock time)\n";
2969 return Error::success();
2970}
2971
2972namespace {
2973struct JITLinkTimers {
2974 TimerGroup JITLinkTG{"llvm-jitlink timers", "timers for llvm-jitlink phases"};
2975 Timer LoadObjectsTimer{"load", "time to load/add object files", JITLinkTG};
2976 Timer LinkTimer{"link", "time to link object files", JITLinkTG};
2977 Timer RunTimer{"run", "time to execute jitlink'd code", JITLinkTG};
2978};
2979} // namespace
2980
2981int main(int argc, char *argv[]) {
2982 InitLLVM X(argc, argv);
2983
2984 InitializeAllTargetInfos();
2985 InitializeAllTargetMCs();
2986 InitializeAllDisassemblers();
2987
2988 cl::HideUnrelatedOptions(Categories: {&JITLinkCategory, &getColorCategory()});
2989 cl::ParseCommandLineOptions(argc, argv, Overview: "llvm jitlink tool");
2990 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
2991
2992 // Check for WaitingOnGraph replay mode.
2993 if (!WaitingOnGraphReplay.empty()) {
2994 ExitOnErr(waitingOnGraphReplay());
2995 return 0;
2996 }
2997
2998 /// If timers are enabled, create a JITLinkTimers instance.
2999 std::unique_ptr<JITLinkTimers> Timers =
3000 ShowTimes ? std::make_unique<JITLinkTimers>() : nullptr;
3001
3002 auto [TT, Features] = getFirstFileTripleAndFeatures();
3003 ExitOnErr(sanitizeArguments(TT, ArgV0: argv[0]));
3004
3005 if (!SymbolicateWith.empty()) {
3006 ExitOnErr(symbolicateBacktraces());
3007 return 0;
3008 }
3009
3010 auto S = ExitOnErr(Session::Create(TT, Features));
3011
3012 enableStatistics(S&: *S, UsingOrcRuntime: !OrcRuntime.empty());
3013
3014 {
3015 TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
3016 ExitOnErr(addSessionInputs(S&: *S));
3017 }
3018
3019 if (PhonyExternals)
3020 addPhonyExternalsGenerator(S&: *S);
3021
3022 if (ShowInitialExecutionSessionState)
3023 S->ES.dump(OS&: outs());
3024
3025 Expected<ExecutorSymbolDef> EntryPoint((ExecutorSymbolDef()));
3026 {
3027 ExpectedAsOutParameter<ExecutorSymbolDef> _(&EntryPoint);
3028 TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
3029 EntryPoint = getEntryPoint(S&: *S);
3030 }
3031
3032 // Print any reports regardless of whether we succeeded or failed.
3033 if (ShowEntryExecutionSessionState)
3034 S->ES.dump(OS&: outs());
3035
3036 if (ShowAddrs)
3037 S->dumpSessionInfo(OS&: outs());
3038
3039 if (!EntryPoint) {
3040 if (Timers)
3041 Timers->JITLinkTG.printAll(OS&: errs());
3042 reportLLVMJITLinkError(Err: EntryPoint.takeError());
3043 ExitOnErr(S->ES.endSession());
3044 exit(status: 1);
3045 }
3046
3047 ExitOnErr(runChecks(S&: *S, TT: std::move(TT), Features: std::move(Features)));
3048
3049 int Result = 0;
3050 if (!NoExec) {
3051 LLVM_DEBUG(dbgs() << "Running \"" << EntryPointName << "\"...\n");
3052 TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
3053 if (!OrcRuntime.empty())
3054 Result = ExitOnErr(runWithRuntime(S&: *S, EntryPointAddr: EntryPoint->getAddress()));
3055 else
3056 Result = ExitOnErr(runWithoutRuntime(S&: *S, EntryPointAddr: EntryPoint->getAddress()));
3057 }
3058
3059 // Destroy the session.
3060 ExitOnErr(S->ES.endSession());
3061 S.reset();
3062
3063 if (Timers)
3064 Timers->JITLinkTG.printAll(OS&: errs());
3065
3066 // If the executing code set a test result override then use that.
3067 if (UseTestResultOverride)
3068 Result = TestResultOverride;
3069
3070 return Result;
3071}
3072