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, 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 ES.setErrorReporter(reportLLVMJITLinkError);
1226
1227 // Attach WaitingOnGraph recorder if requested.
1228 if (!WaitingOnGraphCapture.empty()) {
1229 if (auto GRecorderOrErr =
1230 WaitingOnGraphOpRecorder::Create(Path: WaitingOnGraphCapture)) {
1231 GOpRecorder = std::move(*GRecorderOrErr);
1232 ES.setWaitingOnGraphOpRecorder(*GOpRecorder);
1233 } else {
1234 Err = GRecorderOrErr.takeError();
1235 return;
1236 }
1237 }
1238
1239 if (!NoProcessSymbols)
1240 ExitOnErr(loadProcessSymbols(S&: *this));
1241
1242 ExitOnErr(loadDylibs(S&: *this));
1243
1244 auto &TT = ES.getTargetTriple();
1245
1246 if (!WriteSymbolTableTo.empty()) {
1247 if (auto STDump = SymbolTableDumpPlugin::Create(Path: WriteSymbolTableTo))
1248 ObjLayer.addPlugin(P: std::move(*STDump));
1249 else {
1250 Err = STDump.takeError();
1251 return;
1252 }
1253 }
1254
1255 if (DebuggerSupport && TT.isOSBinFormatMachO()) {
1256 if (!ProcessSymsJD) {
1257 Err = make_error<StringError>(Args: "MachO debugging requires process symbols",
1258 Args: inconvertibleErrorCode());
1259 return;
1260 }
1261 ObjLayer.addPlugin(P: ExitOnErr(GDBJITDebugInfoRegistrationPlugin::Create(
1262 ES&: this->ES, ProcessJD&: *ProcessSymsJD, TT)));
1263 }
1264
1265 if (PerfSupport && TT.isOSBinFormatELF()) {
1266 if (!ProcessSymsJD) {
1267 Err = make_error<StringError>(Args: "MachO debugging requires process symbols",
1268 Args: inconvertibleErrorCode());
1269 return;
1270 }
1271 ObjLayer.addPlugin(P: ExitOnErr(DebugInfoPreservationPlugin::Create()));
1272 ObjLayer.addPlugin(P: ExitOnErr(PerfSupportPlugin::Create(
1273 EPC&: this->ES.getExecutorProcessControl(), JD&: *ProcessSymsJD, EmitDebugInfo: true, EmitUnwindInfo: true)));
1274 }
1275
1276 if (VTuneSupport && TT.isOSBinFormatELF()) {
1277 ObjLayer.addPlugin(P: ExitOnErr(DebugInfoPreservationPlugin::Create()));
1278 ObjLayer.addPlugin(P: ExitOnErr(
1279 VTuneSupportPlugin::Create(EPC&: this->ES.getExecutorProcessControl(),
1280 JD&: *ProcessSymsJD, /*EmitDebugInfo=*/true,
1281 /*TestMode=*/true)));
1282 }
1283
1284 // Set up the platform.
1285 if (!OrcRuntime.empty()) {
1286 assert(ProcessSymsJD && "ProcessSymsJD should have been set");
1287 PlatformJD = &ES.createBareJITDylib(Name: "Platform");
1288 PlatformJD->addToLinkOrder(JD&: *ProcessSymsJD);
1289
1290 if (TT.isOSBinFormatMachO()) {
1291 if (auto P =
1292 MachOPlatform::Create(ObjLinkingLayer&: ObjLayer, PlatformJD&: *PlatformJD, OrcRuntimePath: OrcRuntime.c_str()))
1293 ES.setPlatform(std::move(*P));
1294 else {
1295 Err = P.takeError();
1296 return;
1297 }
1298 } else if (TT.isOSBinFormatELF()) {
1299 if (auto P =
1300 ELFNixPlatform::Create(ObjLinkingLayer&: ObjLayer, PlatformJD&: *PlatformJD, OrcRuntimePath: OrcRuntime.c_str()))
1301 ES.setPlatform(std::move(*P));
1302 else {
1303 Err = P.takeError();
1304 return;
1305 }
1306 } else if (TT.isOSBinFormatCOFF()) {
1307 auto LoadDynLibrary = [&, this](JITDylib &JD,
1308 StringRef DLLName) -> Error {
1309 if (!DLLName.ends_with_insensitive(Suffix: ".dll"))
1310 return make_error<StringError>(Args: "DLLName not ending with .dll",
1311 Args: inconvertibleErrorCode());
1312 return loadAndLinkDynamicLibrary(JD, LibPath: DLLName);
1313 };
1314
1315 if (auto P =
1316 COFFPlatform::Create(ObjLinkingLayer&: ObjLayer, PlatformJD&: *PlatformJD, OrcRuntimePath: OrcRuntime.c_str(),
1317 LoadDynLibrary: std::move(LoadDynLibrary)))
1318 ES.setPlatform(std::move(*P));
1319 else {
1320 Err = P.takeError();
1321 return;
1322 }
1323 } else {
1324 Err = make_error<StringError>(
1325 Args: "-" + OrcRuntime.ArgStr + " specified, but format " +
1326 Triple::getObjectFormatTypeName(ObjectFormat: TT.getObjectFormat()) +
1327 " not supported",
1328 Args: inconvertibleErrorCode());
1329 return;
1330 }
1331 } else if (TT.isOSBinFormatMachO()) {
1332 if (!NoExec) {
1333 std::optional<bool> ForceEHFrames;
1334 if ((Err = ES.getBootstrapMapValue<bool, bool>(Key: "darwin-use-ehframes-only",
1335 Val&: ForceEHFrames)))
1336 return;
1337 bool UseEHFrames = ForceEHFrames.value_or(u: false);
1338 if (!UseEHFrames)
1339 ObjLayer.addPlugin(P: ExitOnErr(UnwindInfoRegistrationPlugin::Create(ES)));
1340 else
1341 ObjLayer.addPlugin(P: ExitOnErr(EHFrameRegistrationPlugin::Create(ES)));
1342 }
1343 } else if (TT.isOSBinFormatELF()) {
1344 if (!NoExec)
1345 ObjLayer.addPlugin(P: ExitOnErr(EHFrameRegistrationPlugin::Create(ES)));
1346 if (DebuggerSupport) {
1347 Error TargetSymErr = Error::success();
1348 auto Plugin =
1349 std::make_unique<ELFDebugObjectPlugin>(args&: ES, args: true, args: true, args&: TargetSymErr);
1350 if (!TargetSymErr)
1351 ObjLayer.addPlugin(P: std::move(Plugin));
1352 else
1353 logAllUnhandledErrors(E: std::move(TargetSymErr), OS&: errs(),
1354 ErrorBanner: "Debugger support not available: ");
1355 }
1356 }
1357
1358 if (auto MainJDOrErr = ES.createJITDylib(Name: "main"))
1359 MainJD = &*MainJDOrErr;
1360 else {
1361 Err = MainJDOrErr.takeError();
1362 return;
1363 }
1364
1365 if (NoProcessSymbols) {
1366 // This symbol is used in testcases, but we're not reflecting process
1367 // symbols so we'll need to make it available some other way.
1368 auto &TestResultJD = ES.createBareJITDylib(Name: "<TestResultJD>");
1369 ExitOnErr(TestResultJD.define(MU: absoluteSymbols(
1370 Symbols: {{ES.intern(SymName: "llvm_jitlink_setTestResultOverride"),
1371 {ExecutorAddr::fromPtr(Ptr: llvm_jitlink_setTestResultOverride),
1372 JITSymbolFlags::Exported}}})));
1373 MainJD->addToLinkOrder(JD&: TestResultJD);
1374 }
1375
1376 ObjLayer.addPlugin(P: std::make_unique<JITLinkSessionPlugin>(args&: *this));
1377
1378 // Process any harness files.
1379 for (auto &HarnessFile : TestHarnesses) {
1380 HarnessFiles.insert(key: HarnessFile);
1381
1382 auto ObjBuffer =
1383 ExitOnErr(loadLinkableFile(Path: HarnessFile, TT: ES.getTargetTriple(),
1384 LA: LoadArchives::Never))
1385 .first;
1386
1387 auto ObjInterface =
1388 ExitOnErr(getObjectFileInterface(ES, ObjBuffer: ObjBuffer->getMemBufferRef()));
1389
1390 for (auto &KV : ObjInterface.SymbolFlags)
1391 HarnessDefinitions.insert(key: *KV.first);
1392
1393 auto Obj = ExitOnErr(
1394 object::ObjectFile::createObjectFile(Object: ObjBuffer->getMemBufferRef()));
1395
1396 for (auto &Sym : Obj->symbols()) {
1397 uint32_t SymFlags = ExitOnErr(Sym.getFlags());
1398 auto Name = ExitOnErr(Sym.getName());
1399
1400 if (Name.empty())
1401 continue;
1402
1403 if (SymFlags & object::BasicSymbolRef::SF_Undefined)
1404 HarnessExternals.insert(key: Name);
1405 }
1406 }
1407
1408 // If a name is defined by some harness file then it's a definition, not an
1409 // external.
1410 for (auto &DefName : HarnessDefinitions)
1411 HarnessExternals.erase(Key: DefName.getKey());
1412
1413 if (!ShowLinkGraphs.empty())
1414 ShowGraphsRegex = Regex(ShowLinkGraphs);
1415}
1416
1417void Session::dumpSessionInfo(raw_ostream &OS) {
1418 OS << "Registered addresses:\n" << SymbolInfos << FileInfos;
1419}
1420
1421void Session::modifyPassConfig(LinkGraph &G, PassConfiguration &PassConfig) {
1422
1423 if (ShowLinkedFiles)
1424 outs() << "Linking " << G.getName() << "\n";
1425
1426 if (!CheckFiles.empty() || ShowAddrs)
1427 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) {
1428 if (ES.getTargetTriple().getObjectFormat() == Triple::ELF)
1429 return registerELFGraphInfo(S&: *this, G);
1430
1431 if (ES.getTargetTriple().getObjectFormat() == Triple::MachO)
1432 return registerMachOGraphInfo(S&: *this, G);
1433
1434 if (ES.getTargetTriple().getObjectFormat() == Triple::COFF)
1435 return registerCOFFGraphInfo(S&: *this, G);
1436
1437 return make_error<StringError>(Args: "Unsupported object format for GOT/stub "
1438 "registration",
1439 Args: inconvertibleErrorCode());
1440 });
1441
1442 if (ShowGraphsRegex)
1443 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) -> Error {
1444 std::lock_guard<std::mutex> Lock(M);
1445 // Print graph if ShowLinkGraphs is specified-but-empty, or if
1446 // it contains the given graph.
1447 if (ShowGraphsRegex->match(String: G.getName())) {
1448 outs() << "Link graph \"" << G.getName() << "\" post-fixup:\n";
1449 G.dump(OS&: outs());
1450 }
1451 return Error::success();
1452 });
1453
1454 PassConfig.PrePrunePasses.push_back(x: [this](LinkGraph &G) {
1455 std::lock_guard<std::mutex> Lock(M);
1456 ++ActiveLinks;
1457 return Error::success();
1458 });
1459 PassConfig.PrePrunePasses.push_back(
1460 x: [this](LinkGraph &G) { return applyLibraryLinkModifiers(S&: *this, G); });
1461 PassConfig.PrePrunePasses.push_back(
1462 x: [this](LinkGraph &G) { return applyHarnessPromotions(S&: *this, G); });
1463
1464 if (ShowRelocatedSectionContents)
1465 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) -> Error {
1466 dumpSectionContents(OS&: outs(), S&: *this, G);
1467 return Error::success();
1468 });
1469
1470 if (AddSelfRelocations)
1471 PassConfig.PostPrunePasses.push_back(x: addSelfRelocations);
1472
1473 PassConfig.PostFixupPasses.push_back(x: [this](LinkGraph &G) {
1474 std::lock_guard<std::mutex> Lock(M);
1475 if (--ActiveLinks == 0)
1476 ActiveLinksCV.notify_all();
1477 return Error::success();
1478 });
1479}
1480
1481Expected<JITDylib *> Session::getOrLoadDynamicLibrary(StringRef LibPath) {
1482 auto It = DynLibJDs.find(x: LibPath);
1483 if (It != DynLibJDs.end()) {
1484 return It->second;
1485 }
1486 auto G = EPCDynamicLibrarySearchGenerator::Load(ES, LibraryPath: LibPath.data());
1487 if (!G)
1488 return G.takeError();
1489 auto JD = &ES.createBareJITDylib(Name: LibPath.str());
1490
1491 JD->addGenerator(DefGenerator: std::move(*G));
1492 DynLibJDs.emplace(args: LibPath.str(), args&: JD);
1493 LLVM_DEBUG({
1494 dbgs() << "Loaded dynamic library " << LibPath.data() << " for " << LibPath
1495 << "\n";
1496 });
1497 return JD;
1498}
1499
1500Error Session::loadAndLinkDynamicLibrary(JITDylib &JD, StringRef LibPath) {
1501 auto DL = getOrLoadDynamicLibrary(LibPath);
1502 if (!DL)
1503 return DL.takeError();
1504 JD.addToLinkOrder(JD&: **DL);
1505 LLVM_DEBUG({
1506 dbgs() << "Linking dynamic library " << LibPath << " to " << JD.getName()
1507 << "\n";
1508 });
1509 return Error::success();
1510}
1511
1512Error Session::FileInfo::registerGOTEntry(
1513 LinkGraph &G, Symbol &Sym, GetSymbolTargetFunction GetSymbolTarget) {
1514 if (Sym.isSymbolZeroFill())
1515 return make_error<StringError>(Args: "Unexpected zero-fill symbol in section " +
1516 Sym.getBlock().getSection().getName(),
1517 Args: inconvertibleErrorCode());
1518 auto TS = GetSymbolTarget(G, Sym.getBlock());
1519 if (!TS)
1520 return TS.takeError();
1521 GOTEntryInfos[*TS->getName()] = {Sym.getSymbolContent(),
1522 Sym.getAddress().getValue(),
1523 Sym.getTargetFlags()};
1524 return Error::success();
1525}
1526
1527Error Session::FileInfo::registerStubEntry(
1528 LinkGraph &G, Symbol &Sym, GetSymbolTargetFunction GetSymbolTarget) {
1529 if (Sym.isSymbolZeroFill())
1530 return make_error<StringError>(Args: "Unexpected zero-fill symbol in section " +
1531 Sym.getBlock().getSection().getName(),
1532 Args: inconvertibleErrorCode());
1533 auto TS = GetSymbolTarget(G, Sym.getBlock());
1534 if (!TS)
1535 return TS.takeError();
1536
1537 SmallVectorImpl<MemoryRegionInfo> &Entry = StubInfos[*TS->getName()];
1538 Entry.insert(I: Entry.begin(),
1539 Elt: {Sym.getSymbolContent(), Sym.getAddress().getValue(),
1540 Sym.getTargetFlags()});
1541 return Error::success();
1542}
1543
1544Error Session::FileInfo::registerMultiStubEntry(
1545 LinkGraph &G, Symbol &Sym, GetSymbolTargetFunction GetSymbolTarget) {
1546 if (Sym.isSymbolZeroFill())
1547 return make_error<StringError>(Args: "Unexpected zero-fill symbol in section " +
1548 Sym.getBlock().getSection().getName(),
1549 Args: inconvertibleErrorCode());
1550
1551 auto Target = GetSymbolTarget(G, Sym.getBlock());
1552 if (!Target)
1553 return Target.takeError();
1554
1555 SmallVectorImpl<MemoryRegionInfo> &Entry = StubInfos[*Target->getName()];
1556 Entry.emplace_back(Args: Sym.getSymbolContent(), Args: Sym.getAddress().getValue(),
1557 Args: Sym.getTargetFlags());
1558
1559 // Let's keep stubs ordered by ascending address.
1560 std::sort(first: Entry.begin(), last: Entry.end(),
1561 comp: [](const MemoryRegionInfo &L, const MemoryRegionInfo &R) {
1562 return L.getTargetAddress() < R.getTargetAddress();
1563 });
1564
1565 return Error::success();
1566}
1567
1568Expected<Session::FileInfo &> Session::findFileInfo(StringRef FileName) {
1569 auto FileInfoItr = FileInfos.find(Key: FileName);
1570 if (FileInfoItr == FileInfos.end())
1571 return make_error<StringError>(Args: "file \"" + FileName + "\" not recognized",
1572 Args: inconvertibleErrorCode());
1573 return FileInfoItr->second;
1574}
1575
1576Expected<Session::MemoryRegionInfo &>
1577Session::findSectionInfo(StringRef FileName, StringRef SectionName) {
1578 auto FI = findFileInfo(FileName);
1579 if (!FI)
1580 return FI.takeError();
1581 auto SecInfoItr = FI->SectionInfos.find(Key: SectionName);
1582 if (SecInfoItr == FI->SectionInfos.end())
1583 return make_error<StringError>(Args: "no section \"" + SectionName +
1584 "\" registered for file \"" + FileName +
1585 "\"",
1586 Args: inconvertibleErrorCode());
1587 return SecInfoItr->second;
1588}
1589
1590class MemoryMatcher {
1591public:
1592 MemoryMatcher(ArrayRef<char> Content)
1593 : Pos(Content.data()), End(Pos + Content.size()) {}
1594
1595 template <typename MaskType> bool matchMask(MaskType Mask) {
1596 if (Mask == (Mask & *reinterpret_cast<const MaskType *>(Pos))) {
1597 Pos += sizeof(MaskType);
1598 return true;
1599 }
1600 return false;
1601 }
1602
1603 template <typename ValueType> bool matchEqual(ValueType Value) {
1604 if (Value == *reinterpret_cast<const ValueType *>(Pos)) {
1605 Pos += sizeof(ValueType);
1606 return true;
1607 }
1608 return false;
1609 }
1610
1611 bool done() const { return Pos == End; }
1612
1613private:
1614 const char *Pos;
1615 const char *End;
1616};
1617
1618static StringRef detectStubKind(const Session::MemoryRegionInfo &Stub) {
1619 using namespace support::endian;
1620 auto Armv7MovWTle = byte_swap<uint32_t>(value: 0xe300c000, endian: endianness::little);
1621 auto Armv7BxR12le = byte_swap<uint32_t>(value: 0xe12fff1c, endian: endianness::little);
1622 auto Thumbv7MovWTle = byte_swap<uint32_t>(value: 0x0c00f240, endian: endianness::little);
1623 auto Thumbv7BxR12le = byte_swap<uint16_t>(value: 0x4760, endian: endianness::little);
1624
1625 MemoryMatcher M(Stub.getContent());
1626 if (M.matchMask(Mask: Thumbv7MovWTle)) {
1627 if (M.matchMask(Mask: Thumbv7MovWTle))
1628 if (M.matchEqual(Value: Thumbv7BxR12le))
1629 if (M.done())
1630 return "thumbv7_abs_le";
1631 } else if (M.matchMask(Mask: Armv7MovWTle)) {
1632 if (M.matchMask(Mask: Armv7MovWTle))
1633 if (M.matchEqual(Value: Armv7BxR12le))
1634 if (M.done())
1635 return "armv7_abs_le";
1636 }
1637 return "";
1638}
1639
1640Expected<Session::MemoryRegionInfo &>
1641Session::findStubInfo(StringRef FileName, StringRef TargetName,
1642 StringRef KindNameFilter) {
1643 auto FI = findFileInfo(FileName);
1644 if (!FI)
1645 return FI.takeError();
1646 auto StubInfoItr = FI->StubInfos.find(Key: TargetName);
1647 if (StubInfoItr == FI->StubInfos.end())
1648 return make_error<StringError>(Args: "no stub for \"" + TargetName +
1649 "\" registered for file \"" + FileName +
1650 "\"",
1651 Args: inconvertibleErrorCode());
1652 auto &StubsForTarget = StubInfoItr->second;
1653 assert(!StubsForTarget.empty() && "At least 1 stub in each entry");
1654 if (KindNameFilter.empty() && StubsForTarget.size() == 1)
1655 return StubsForTarget[0]; // Regular single-stub match
1656
1657 std::string KindsStr;
1658 SmallVector<MemoryRegionInfo *, 1> Matches;
1659 Regex KindNameMatcher(KindNameFilter.empty() ? ".*" : KindNameFilter);
1660 for (MemoryRegionInfo &Stub : StubsForTarget) {
1661 StringRef Kind = detectStubKind(Stub);
1662 if (KindNameMatcher.match(String: Kind))
1663 Matches.push_back(Elt: &Stub);
1664 KindsStr += "\"" + (Kind.empty() ? "<unknown>" : Kind.str()) + "\", ";
1665 }
1666 if (Matches.empty())
1667 return make_error<StringError>(
1668 Args: "\"" + TargetName + "\" has " + Twine(StubsForTarget.size()) +
1669 " stubs in file \"" + FileName +
1670 "\", but none of them matches the stub-kind filter \"" +
1671 KindNameFilter + "\" (all encountered kinds are " +
1672 StringRef(KindsStr.data(), KindsStr.size() - 2) + ").",
1673 Args: inconvertibleErrorCode());
1674 if (Matches.size() > 1)
1675 return make_error<StringError>(
1676 Args: "\"" + TargetName + "\" has " + Twine(Matches.size()) +
1677 " candidate stubs in file \"" + FileName +
1678 "\". Please refine stub-kind filter \"" + KindNameFilter +
1679 "\" for disambiguation (encountered kinds are " +
1680 StringRef(KindsStr.data(), KindsStr.size() - 2) + ").",
1681 Args: inconvertibleErrorCode());
1682
1683 return *Matches[0];
1684}
1685
1686Expected<Session::MemoryRegionInfo &>
1687Session::findGOTEntryInfo(StringRef FileName, StringRef TargetName) {
1688 auto FI = findFileInfo(FileName);
1689 if (!FI)
1690 return FI.takeError();
1691 auto GOTInfoItr = FI->GOTEntryInfos.find(Key: TargetName);
1692 if (GOTInfoItr == FI->GOTEntryInfos.end())
1693 return make_error<StringError>(Args: "no GOT entry for \"" + TargetName +
1694 "\" registered for file \"" + FileName +
1695 "\"",
1696 Args: inconvertibleErrorCode());
1697 return GOTInfoItr->second;
1698}
1699
1700bool Session::isSymbolRegistered(const orc::SymbolStringPtr &SymbolName) {
1701 return SymbolInfos.count(Val: SymbolName);
1702}
1703
1704Expected<Session::MemoryRegionInfo &>
1705Session::findSymbolInfo(const orc::SymbolStringPtr &SymbolName,
1706 Twine ErrorMsgStem) {
1707 auto SymInfoItr = SymbolInfos.find(Val: SymbolName);
1708 if (SymInfoItr == SymbolInfos.end())
1709 return make_error<StringError>(Args: ErrorMsgStem + ": symbol " + *SymbolName +
1710 " not found",
1711 Args: inconvertibleErrorCode());
1712 return SymInfoItr->second;
1713}
1714
1715} // end namespace llvm
1716
1717static std::pair<Triple, SubtargetFeatures> getFirstFileTripleAndFeatures() {
1718
1719 // If we're running in symbolicate mode then just use the process triple.
1720 if (!SymbolicateWith.empty())
1721 return std::make_pair(x: Triple(sys::getProcessTriple()), y: SubtargetFeatures());
1722
1723 // Otherwise we need to inspect the input files.
1724 static std::pair<Triple, SubtargetFeatures> FirstTTAndFeatures = []() {
1725 assert(!InputFiles.empty() && "InputFiles can not be empty");
1726
1727 if (!OverrideTriple.empty()) {
1728 LLVM_DEBUG({
1729 dbgs() << "Triple from -triple override: " << OverrideTriple << "\n";
1730 });
1731 return std::make_pair(x: Triple(OverrideTriple), y: SubtargetFeatures());
1732 }
1733
1734 for (auto InputFile : InputFiles) {
1735 auto ObjBuffer = ExitOnErr(getFile(FileName: InputFile));
1736 file_magic Magic = identify_magic(magic: ObjBuffer->getBuffer());
1737 switch (Magic) {
1738 case file_magic::coff_object:
1739 case file_magic::elf_relocatable:
1740 case file_magic::macho_object: {
1741 auto Obj = ExitOnErr(
1742 object::ObjectFile::createObjectFile(Object: ObjBuffer->getMemBufferRef()));
1743 Triple TT;
1744 if (auto *MachOObj = dyn_cast<object::MachOObjectFile>(Val: Obj.get()))
1745 TT = MachOObj->getArchTriple();
1746 else
1747 TT = Obj->makeTriple();
1748 if (Magic == file_magic::coff_object) {
1749 // TODO: Move this to makeTriple() if possible.
1750 TT.setObjectFormat(Triple::COFF);
1751 TT.setOS(Triple::OSType::Win32);
1752 }
1753 SubtargetFeatures Features;
1754 if (auto ObjFeatures = Obj->getFeatures())
1755 Features = std::move(*ObjFeatures);
1756
1757 LLVM_DEBUG({
1758 dbgs() << "Triple from " << InputFile << ": " << TT.str() << "\n";
1759 });
1760 return std::make_pair(x&: TT, y&: Features);
1761 }
1762 default:
1763 break;
1764 }
1765 }
1766
1767 // If no plain object file inputs exist to pin down the triple then detect
1768 // the host triple and default to that.
1769 auto JTMB = ExitOnErr(JITTargetMachineBuilder::detectHost());
1770 LLVM_DEBUG({
1771 dbgs() << "Triple from host-detection: " << JTMB.getTargetTriple().str()
1772 << "\n";
1773 });
1774 return std::make_pair(x&: JTMB.getTargetTriple(), y&: JTMB.getFeatures());
1775 }();
1776
1777 return FirstTTAndFeatures;
1778}
1779
1780static Error sanitizeArguments(const Triple &TT, const char *ArgV0) {
1781
1782 if (InputFiles.empty())
1783 return make_error<StringError>(
1784 Args: "Not enough positional command line arguments specified! (see "
1785 "llvm-jitlink --help)",
1786 Args: inconvertibleErrorCode());
1787
1788 // If we're in replay mode we should never get here.
1789 assert(WaitingOnGraphReplay.empty());
1790
1791 // -noexec and --args should not be used together.
1792 if (NoExec && !InputArgv.empty())
1793 errs() << "Warning: --args passed to -noexec run will be ignored.\n";
1794
1795 // Set the entry point name if not specified.
1796 if (EntryPointName.empty())
1797 EntryPointName = TT.getObjectFormat() == Triple::MachO ? "_main" : "main";
1798
1799 // Disable debugger support by default in noexec tests.
1800 if (DebuggerSupport.getNumOccurrences() == 0 && NoExec)
1801 DebuggerSupport = false;
1802
1803 if (!OrcRuntime.empty() && NoProcessSymbols)
1804 return make_error<StringError>(Args: "-orc-runtime requires process symbols",
1805 Args: inconvertibleErrorCode());
1806
1807 // If -slab-allocate is passed, check that we're not trying to use it in
1808 // -oop-executor or -oop-executor-connect mode.
1809 //
1810 // FIXME: Remove once we enable remote slab allocation.
1811 if (SlabAllocateSizeString != "") {
1812 if (OutOfProcessExecutor.getNumOccurrences() ||
1813 OutOfProcessExecutorConnect.getNumOccurrences())
1814 return make_error<StringError>(
1815 Args: "-slab-allocate cannot be used with -oop-executor or "
1816 "-oop-executor-connect",
1817 Args: inconvertibleErrorCode());
1818 }
1819
1820 // If -slab-address is passed, require -slab-allocate and -noexec
1821 if (SlabAddress != ~0ULL) {
1822 if (SlabAllocateSizeString == "" || !NoExec)
1823 return make_error<StringError>(
1824 Args: "-slab-address requires -slab-allocate and -noexec",
1825 Args: inconvertibleErrorCode());
1826
1827 if (SlabPageSize == 0)
1828 errs() << "Warning: -slab-address used without -slab-page-size.\n";
1829 }
1830
1831 if (SlabPageSize != 0) {
1832 // -slab-page-size requires slab alloc.
1833 if (SlabAllocateSizeString == "")
1834 return make_error<StringError>(Args: "-slab-page-size requires -slab-allocate",
1835 Args: inconvertibleErrorCode());
1836
1837 // Check -slab-page-size / -noexec interactions.
1838 if (!NoExec) {
1839 if (auto RealPageSize = sys::Process::getPageSize()) {
1840 if (SlabPageSize % *RealPageSize)
1841 return make_error<StringError>(
1842 Args: "-slab-page-size must be a multiple of real page size for exec "
1843 "tests (did you mean to use -noexec ?)\n",
1844 Args: inconvertibleErrorCode());
1845 } else {
1846 errs() << "Could not retrieve process page size:\n";
1847 logAllUnhandledErrors(E: RealPageSize.takeError(), OS&: errs(), ErrorBanner: "");
1848 errs() << "Executing with slab page size = "
1849 << formatv(Fmt: "{0:x}", Vals&: SlabPageSize) << ".\n"
1850 << "Tool may crash if " << formatv(Fmt: "{0:x}", Vals&: SlabPageSize)
1851 << " is not a multiple of the real process page size.\n"
1852 << "(did you mean to use -noexec ?)";
1853 }
1854 }
1855 }
1856
1857#if LLVM_ENABLE_THREADS
1858 if (MaterializationThreads == std::numeric_limits<size_t>::max()) {
1859 if (auto HC = std::thread::hardware_concurrency())
1860 MaterializationThreads = HC;
1861 else {
1862 errs() << "Warning: std::thread::hardware_concurrency() returned 0, "
1863 "defaulting to -num-threads=1.\n";
1864 MaterializationThreads = 1;
1865 }
1866 }
1867#else
1868 if (MaterializationThreads.getNumOccurrences() &&
1869 MaterializationThreads != 0) {
1870 errs() << "Warning: -num-threads was set, but LLVM was built with threads "
1871 "disabled. Resetting to -num-threads=0\n";
1872 }
1873 MaterializationThreads = 0;
1874#endif
1875
1876 if (!!OutOfProcessExecutor.getNumOccurrences() ||
1877 !!OutOfProcessExecutorConnect.getNumOccurrences()) {
1878 if (NoExec)
1879 return make_error<StringError>(Args: "-noexec cannot be used with " +
1880 OutOfProcessExecutor.ArgStr + " or " +
1881 OutOfProcessExecutorConnect.ArgStr,
1882 Args: inconvertibleErrorCode());
1883
1884 if (MaterializationThreads == 0)
1885 return make_error<StringError>(Args: "-threads=0 cannot be used with " +
1886 OutOfProcessExecutor.ArgStr + " or " +
1887 OutOfProcessExecutorConnect.ArgStr,
1888 Args: inconvertibleErrorCode());
1889 }
1890
1891#ifndef NDEBUG
1892 if (DebugFlag && MaterializationThreads != 0)
1893 errs() << "Warning: debugging output is not thread safe. "
1894 "Use -num-threads=0 to stabilize output.\n";
1895#endif // NDEBUG
1896
1897 // Only one of -oop-executor and -oop-executor-connect can be used.
1898 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1899 !!OutOfProcessExecutorConnect.getNumOccurrences())
1900 return make_error<StringError>(
1901 Args: "Only one of -" + OutOfProcessExecutor.ArgStr + " and -" +
1902 OutOfProcessExecutorConnect.ArgStr + " can be specified",
1903 Args: inconvertibleErrorCode());
1904
1905 // If -oop-executor was used but no value was specified then use a sensible
1906 // default.
1907 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1908 OutOfProcessExecutor.empty()) {
1909 SmallString<256> OOPExecutorPath(sys::fs::getMainExecutable(
1910 argv0: ArgV0, MainExecAddr: reinterpret_cast<void *>(&sanitizeArguments)));
1911 sys::path::remove_filename(path&: OOPExecutorPath);
1912 sys::path::append(path&: OOPExecutorPath, a: "llvm-jitlink-executor");
1913 OutOfProcessExecutor = OOPExecutorPath.str().str();
1914 }
1915
1916 // If lazy linking is requested then check compatibility with other options.
1917 if (lazyLinkingRequested()) {
1918 if (OrcRuntime.empty())
1919 return make_error<StringError>(Args: "Lazy linking requries the ORC runtime",
1920 Args: inconvertibleErrorCode());
1921
1922 if (!TestHarnesses.empty())
1923 return make_error<StringError>(
1924 Args: "Lazy linking cannot be used with -harness mode",
1925 Args: inconvertibleErrorCode());
1926 } else if (Speculate != SpeculateKind::None) {
1927 errs() << "Warning: -speculate ignored as there are no -lazy inputs\n";
1928 Speculate = SpeculateKind::None;
1929 }
1930
1931 if (Speculate == SpeculateKind::None) {
1932 if (!SpeculateOrder.empty()) {
1933 errs() << "Warning: -speculate-order ignored because speculation is "
1934 "disabled\n";
1935 SpeculateOrder = "";
1936 }
1937
1938 if (!RecordLazyExecs.empty()) {
1939 errs() << "Warning: -record-lazy-execs ignored because speculation is "
1940 "disabled\n";
1941 RecordLazyExecs = "";
1942 }
1943 }
1944
1945 if (!SymbolicateWith.empty()) {
1946 if (!WriteSymbolTableTo.empty())
1947 errs() << WriteSymbolTableTo.ArgStr << " specified with "
1948 << SymbolicateWith.ArgStr << ", ignoring.";
1949 if (InputFiles.empty())
1950 InputFiles.push_back(value: "-");
1951 }
1952
1953 return Error::success();
1954}
1955
1956static void addPhonyExternalsGenerator(Session &S) {
1957 S.MainJD->addGenerator(DefGenerator: std::make_unique<PhonyExternalsGenerator>());
1958}
1959
1960static Error createJITDylibs(Session &S,
1961 std::map<unsigned, JITDylib *> &IdxToJD) {
1962 // First, set up JITDylibs.
1963 LLVM_DEBUG(dbgs() << "Creating JITDylibs...\n");
1964 {
1965 // Create a "main" JITLinkDylib.
1966 IdxToJD[0] = S.MainJD;
1967 S.JDSearchOrder.push_back(x: {S.MainJD, JITDylibLookupFlags::MatchAllSymbols});
1968 LLVM_DEBUG(dbgs() << " 0: " << S.MainJD->getName() << "\n");
1969
1970 // Add any extra JITDylibs from the command line.
1971 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
1972 JDItr != JDEnd; ++JDItr) {
1973 auto JD = S.ES.createJITDylib(Name: *JDItr);
1974 if (!JD)
1975 return JD.takeError();
1976 unsigned JDIdx = JITDylibs.getPosition(optnum: JDItr - JITDylibs.begin());
1977 IdxToJD[JDIdx] = &*JD;
1978 S.JDSearchOrder.push_back(x: {&*JD, JITDylibLookupFlags::MatchAllSymbols});
1979 LLVM_DEBUG(dbgs() << " " << JDIdx << ": " << JD->getName() << "\n");
1980 }
1981 }
1982
1983 if (S.PlatformJD)
1984 S.JDSearchOrder.push_back(
1985 x: {S.PlatformJD, JITDylibLookupFlags::MatchExportedSymbolsOnly});
1986 if (S.ProcessSymsJD)
1987 S.JDSearchOrder.push_back(
1988 x: {S.ProcessSymsJD, JITDylibLookupFlags::MatchExportedSymbolsOnly});
1989
1990 LLVM_DEBUG({
1991 dbgs() << "Dylib search order is [ ";
1992 for (auto &KV : S.JDSearchOrder)
1993 dbgs() << KV.first->getName() << " ";
1994 dbgs() << "]\n";
1995 });
1996
1997 return Error::success();
1998}
1999
2000static Error addAbsoluteSymbols(Session &S,
2001 const std::map<unsigned, JITDylib *> &IdxToJD) {
2002 // Define absolute symbols.
2003 LLVM_DEBUG(dbgs() << "Defining absolute symbols...\n");
2004 for (auto AbsDefItr = AbsoluteDefs.begin(), AbsDefEnd = AbsoluteDefs.end();
2005 AbsDefItr != AbsDefEnd; ++AbsDefItr) {
2006 unsigned AbsDefArgIdx =
2007 AbsoluteDefs.getPosition(optnum: AbsDefItr - AbsoluteDefs.begin());
2008 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: AbsDefArgIdx))->second;
2009
2010 StringRef AbsDefStmt = *AbsDefItr;
2011 size_t EqIdx = AbsDefStmt.find_first_of(C: '=');
2012 if (EqIdx == StringRef::npos)
2013 return make_error<StringError>(Args: "Invalid absolute define \"" + AbsDefStmt +
2014 "\". Syntax: <name>=<addr>",
2015 Args: inconvertibleErrorCode());
2016 StringRef Name = AbsDefStmt.substr(Start: 0, N: EqIdx).trim();
2017 StringRef AddrStr = AbsDefStmt.substr(Start: EqIdx + 1).trim();
2018
2019 uint64_t Addr;
2020 if (AddrStr.getAsInteger(Radix: 0, Result&: Addr))
2021 return make_error<StringError>(Args: "Invalid address expression \"" + AddrStr +
2022 "\" in absolute symbol definition \"" +
2023 AbsDefStmt + "\"",
2024 Args: inconvertibleErrorCode());
2025 ExecutorSymbolDef AbsDef(ExecutorAddr(Addr), JITSymbolFlags::Exported);
2026 auto InternedName = S.ES.intern(SymName: Name);
2027 if (auto Err = JD.define(MU: absoluteSymbols(Symbols: {{InternedName, AbsDef}})))
2028 return Err;
2029
2030 // Register the absolute symbol with the session symbol infos.
2031 S.SymbolInfos[std::move(InternedName)] =
2032 {ArrayRef<char>(), Addr, AbsDef.getFlags().getTargetFlags()};
2033 }
2034
2035 return Error::success();
2036}
2037
2038static Error addAliases(Session &S,
2039 const std::map<unsigned, JITDylib *> &IdxToJD) {
2040 // Define absolute symbols.
2041 LLVM_DEBUG(dbgs() << "Defining aliases...\n");
2042
2043 DenseMap<std::pair<JITDylib *, JITDylib *>, SymbolAliasMap> Reexports;
2044 for (auto AliasItr = Aliases.begin(), AliasEnd = Aliases.end();
2045 AliasItr != AliasEnd; ++AliasItr) {
2046
2047 auto BadExpr = [&]() {
2048 return make_error<StringError>(
2049 Args: "Invalid alias definition \"" + *AliasItr +
2050 "\". Syntax: [<dst-jd>:]<alias>=[<src-jd>:]<aliasee>",
2051 Args: inconvertibleErrorCode());
2052 };
2053
2054 auto GetJD = [&](StringRef JDName) -> Expected<JITDylib *> {
2055 if (JDName.empty()) {
2056 unsigned AliasArgIdx = Aliases.getPosition(optnum: AliasItr - Aliases.begin());
2057 return std::prev(x: IdxToJD.lower_bound(x: AliasArgIdx))->second;
2058 }
2059
2060 auto *JD = S.ES.getJITDylibByName(Name: JDName);
2061 if (!JD)
2062 return make_error<StringError>(Args: StringRef("In alias definition \"") +
2063 *AliasItr + "\" no dylib named " +
2064 JDName,
2065 Args: inconvertibleErrorCode());
2066
2067 return JD;
2068 };
2069
2070 {
2071 // First split on '=' to get alias and aliasee.
2072 StringRef AliasStmt = *AliasItr;
2073 auto [AliasExpr, AliaseeExpr] = AliasStmt.split(Separator: '=');
2074 if (AliaseeExpr.empty())
2075 return BadExpr();
2076
2077 auto [AliasJDName, Alias] = AliasExpr.split(Separator: ':');
2078 if (Alias.empty())
2079 std::swap(a&: AliasJDName, b&: Alias);
2080
2081 auto AliasJD = GetJD(AliasJDName);
2082 if (!AliasJD)
2083 return AliasJD.takeError();
2084
2085 auto [AliaseeJDName, Aliasee] = AliaseeExpr.split(Separator: ':');
2086 if (Aliasee.empty())
2087 std::swap(a&: AliaseeJDName, b&: Aliasee);
2088
2089 if (AliaseeJDName.empty() && !AliasJDName.empty())
2090 AliaseeJDName = AliasJDName;
2091 auto AliaseeJD = GetJD(AliaseeJDName);
2092 if (!AliaseeJD)
2093 return AliaseeJD.takeError();
2094
2095 Reexports[{*AliasJD, *AliaseeJD}][S.ES.intern(SymName: Alias)] = {
2096 S.ES.intern(SymName: Aliasee), JITSymbolFlags::Exported};
2097 }
2098 }
2099
2100 for (auto &[JDs, AliasMap] : Reexports) {
2101 auto [DstJD, SrcJD] = JDs;
2102 if (auto Err = DstJD->define(MU: reexports(SourceJD&: *SrcJD, Aliases: std::move(AliasMap))))
2103 return Err;
2104 }
2105
2106 return Error::success();
2107}
2108
2109static Error addSectCreates(Session &S,
2110 const std::map<unsigned, JITDylib *> &IdxToJD) {
2111 for (auto SCItr = SectCreate.begin(), SCEnd = SectCreate.end();
2112 SCItr != SCEnd; ++SCItr) {
2113
2114 unsigned SCArgIdx = SectCreate.getPosition(optnum: SCItr - SectCreate.begin());
2115 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: SCArgIdx))->second;
2116
2117 StringRef SCArg(*SCItr);
2118
2119 auto [SectAndFileName, ExtraSymbolsString] = SCArg.rsplit(Separator: '@');
2120 auto [SectName, FileName] = SectAndFileName.rsplit(Separator: ',');
2121 if (SectName.empty())
2122 return make_error<StringError>(Args: "In -sectcreate=" + SCArg +
2123 ", filename component cannot be empty",
2124 Args: inconvertibleErrorCode());
2125 if (FileName.empty())
2126 return make_error<StringError>(Args: "In -sectcreate=" + SCArg +
2127 ", filename component cannot be empty",
2128 Args: inconvertibleErrorCode());
2129
2130 auto Content = getFile(FileName);
2131 if (!Content)
2132 return Content.takeError();
2133
2134 SectCreateMaterializationUnit::ExtraSymbolsMap ExtraSymbols;
2135 while (!ExtraSymbolsString.empty()) {
2136 StringRef NextSymPair;
2137 std::tie(args&: NextSymPair, args&: ExtraSymbolsString) = ExtraSymbolsString.split(Separator: ',');
2138
2139 auto [Sym, OffsetString] = NextSymPair.split(Separator: '=');
2140 size_t Offset;
2141
2142 if (OffsetString.getAsInteger(Radix: 0, Result&: Offset))
2143 return make_error<StringError>(Args: "In -sectcreate=" + SCArg + ", " +
2144 OffsetString +
2145 " is not a valid integer",
2146 Args: inconvertibleErrorCode());
2147
2148 ExtraSymbols[S.ES.intern(SymName: Sym)] = {.Flags: JITSymbolFlags::Exported, .Offset: Offset};
2149 }
2150
2151 if (auto Err = JD.define(MU: std::make_unique<SectCreateMaterializationUnit>(
2152 args&: S.ObjLayer, args: SectName.str(), args: MemProt::Read, args: 16, args: std::move(*Content),
2153 args: std::move(ExtraSymbols))))
2154 return Err;
2155 }
2156
2157 return Error::success();
2158}
2159
2160static Error addTestHarnesses(Session &S) {
2161 LLVM_DEBUG(dbgs() << "Adding test harness objects...\n");
2162 for (auto HarnessFile : TestHarnesses) {
2163 LLVM_DEBUG(dbgs() << " " << HarnessFile << "\n");
2164 auto Linkable = loadLinkableFile(Path: HarnessFile, TT: S.ES.getTargetTriple(),
2165 LA: LoadArchives::Never);
2166 if (!Linkable)
2167 return Linkable.takeError();
2168 if (auto Err = S.ObjLayer.add(JD&: *S.MainJD, O: std::move(Linkable->first)))
2169 return Err;
2170 }
2171 return Error::success();
2172}
2173
2174static Error addObjects(Session &S,
2175 const std::map<unsigned, JITDylib *> &IdxToJD,
2176 const DenseSet<unsigned> &LazyLinkIdxs) {
2177
2178 // Load each object into the corresponding JITDylib..
2179 LLVM_DEBUG(dbgs() << "Adding objects...\n");
2180 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
2181 InputFileItr != InputFileEnd; ++InputFileItr) {
2182 unsigned InputFileArgIdx =
2183 InputFiles.getPosition(optnum: InputFileItr - InputFiles.begin());
2184 const std::string &InputFile = *InputFileItr;
2185 if (StringRef(InputFile).ends_with(Suffix: ".a") ||
2186 StringRef(InputFile).ends_with(Suffix: ".lib"))
2187 continue;
2188 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: InputFileArgIdx))->second;
2189 bool AddLazy = LazyLinkIdxs.count(V: InputFileArgIdx);
2190 LLVM_DEBUG(dbgs() << " " << InputFileArgIdx << ": \"" << InputFile << "\" "
2191 << (AddLazy ? " (lazy-linked)" : "") << " to "
2192 << JD.getName() << "\n";);
2193 auto ObjBuffer = loadLinkableFile(Path: InputFile, TT: S.ES.getTargetTriple(),
2194 LA: LoadArchives::Never);
2195 if (!ObjBuffer)
2196 return ObjBuffer.takeError();
2197
2198 if (S.HarnessFiles.empty()) {
2199 if (auto Err =
2200 S.getLinkLayer(Lazy: AddLazy).add(JD, O: std::move(ObjBuffer->first)))
2201 return Err;
2202 } else {
2203 // We're in -harness mode. Use a custom interface for this
2204 // test object.
2205 auto ObjInterface =
2206 getTestObjectFileInterface(S, O: ObjBuffer->first->getMemBufferRef());
2207 if (!ObjInterface)
2208 return ObjInterface.takeError();
2209
2210 if (auto Err = S.ObjLayer.add(JD, O: std::move(ObjBuffer->first),
2211 I: std::move(*ObjInterface)))
2212 return Err;
2213 }
2214 }
2215
2216 return Error::success();
2217}
2218
2219static Expected<MaterializationUnit::Interface>
2220getObjectFileInterfaceHidden(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
2221 auto I = getObjectFileInterface(ES, ObjBuffer);
2222 if (I) {
2223 for (auto &KV : I->SymbolFlags)
2224 KV.second &= ~JITSymbolFlags::Exported;
2225 }
2226 return I;
2227}
2228
2229static SmallVector<StringRef, 5> getSearchPathsFromEnvVar(Session &S) {
2230 // FIXME: Handle EPC environment.
2231 SmallVector<StringRef, 5> PathVec;
2232 auto TT = S.ES.getTargetTriple();
2233 if (TT.isOSBinFormatCOFF())
2234 StringRef(getenv(name: "PATH")).split(A&: PathVec, Separator: ";");
2235 else if (TT.isOSBinFormatELF())
2236 StringRef(getenv(name: "LD_LIBRARY_PATH")).split(A&: PathVec, Separator: ":");
2237
2238 return PathVec;
2239}
2240
2241static Expected<std::unique_ptr<DefinitionGenerator>>
2242LoadLibraryWeak(Session &S, StringRef Path) {
2243 auto Symbols = getDylibInterface(ES&: S.ES, Path);
2244 if (!Symbols)
2245 return Symbols.takeError();
2246
2247 return std::make_unique<EPCDynamicLibrarySearchGenerator>(
2248 args&: S.ES, args: [Symbols = std::move(*Symbols)](const SymbolStringPtr &Sym) {
2249 return Symbols.count(V: Sym);
2250 });
2251}
2252
2253static Error addLibraries(Session &S,
2254 const std::map<unsigned, JITDylib *> &IdxToJD,
2255 const DenseSet<unsigned> &LazyLinkIdxs) {
2256
2257 // 1. Collect search paths for each JITDylib.
2258 DenseMap<const JITDylib *, SmallVector<StringRef, 2>> JDSearchPaths;
2259
2260 for (auto LSPItr = LibrarySearchPaths.begin(),
2261 LSPEnd = LibrarySearchPaths.end();
2262 LSPItr != LSPEnd; ++LSPItr) {
2263 unsigned LibrarySearchPathIdx =
2264 LibrarySearchPaths.getPosition(optnum: LSPItr - LibrarySearchPaths.begin());
2265 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: LibrarySearchPathIdx))->second;
2266
2267 StringRef LibrarySearchPath = *LSPItr;
2268 if (sys::fs::get_file_type(Path: LibrarySearchPath) !=
2269 sys::fs::file_type::directory_file)
2270 return make_error<StringError>(Args: "While linking " + JD.getName() + ", -L" +
2271 LibrarySearchPath +
2272 " does not point to a directory",
2273 Args: inconvertibleErrorCode());
2274
2275 JDSearchPaths[&JD].push_back(Elt: *LSPItr);
2276 }
2277
2278 LLVM_DEBUG({
2279 if (!JDSearchPaths.empty())
2280 dbgs() << "Search paths:\n";
2281 for (auto &KV : JDSearchPaths) {
2282 dbgs() << " " << KV.first->getName() << ": [";
2283 for (auto &LibSearchPath : KV.second)
2284 dbgs() << " \"" << LibSearchPath << "\"";
2285 dbgs() << " ]\n";
2286 }
2287 });
2288
2289 // 2. Collect library loads
2290 struct LibraryLoad {
2291 std::string LibName;
2292 bool IsPath = false;
2293 unsigned Position;
2294 ArrayRef<StringRef> CandidateExtensions;
2295 enum { Standard, Hidden, Weak } Modifier;
2296 };
2297
2298 // Queue to load library as in the order as it appears in the argument list.
2299 std::deque<LibraryLoad> LibraryLoadQueue;
2300
2301 // Add archive files from the inputs to LibraryLoads.
2302 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
2303 InputFileItr != InputFileEnd; ++InputFileItr) {
2304 StringRef InputFile = *InputFileItr;
2305 if (!InputFile.ends_with(Suffix: ".a") && !InputFile.ends_with(Suffix: ".lib"))
2306 continue;
2307 LibraryLoad LL;
2308 LL.LibName = InputFile.str();
2309 LL.IsPath = true;
2310 LL.Position = InputFiles.getPosition(optnum: InputFileItr - InputFiles.begin());
2311 LL.CandidateExtensions = {};
2312 LL.Modifier = LibraryLoad::Standard;
2313 LibraryLoadQueue.push_back(x: std::move(LL));
2314 }
2315
2316 // Add -load_hidden arguments to LibraryLoads.
2317 for (auto LibItr = LoadHidden.begin(), LibEnd = LoadHidden.end();
2318 LibItr != LibEnd; ++LibItr) {
2319 LibraryLoad LL;
2320 LL.LibName = *LibItr;
2321 LL.IsPath = true;
2322 LL.Position = LoadHidden.getPosition(optnum: LibItr - LoadHidden.begin());
2323 LL.CandidateExtensions = {};
2324 LL.Modifier = LibraryLoad::Hidden;
2325 LibraryLoadQueue.push_back(x: std::move(LL));
2326 }
2327
2328 // Add -weak_library arguments to LibraryLoads.
2329 for (auto LibItr = WeakLibraries.begin(), LibEnd = WeakLibraries.end();
2330 LibItr != LibEnd; ++LibItr) {
2331 LibraryLoad LL;
2332 LL.LibName = *LibItr;
2333 LL.IsPath = true;
2334 LL.Position = WeakLibraries.getPosition(optnum: LibItr - WeakLibraries.begin());
2335 LL.CandidateExtensions = {};
2336 LL.Modifier = LibraryLoad::Weak;
2337 LibraryLoadQueue.push_back(x: std::move(LL));
2338 }
2339
2340 StringRef StandardExtensions[] = {".so", ".dylib", ".dll", ".a", ".lib"};
2341 StringRef DynLibExtensionsOnly[] = {".so", ".dylib", ".dll"};
2342 StringRef ArchiveExtensionsOnly[] = {".a", ".lib"};
2343 StringRef WeakLinkExtensionsOnly[] = {".dylib", ".tbd"};
2344
2345 // Add -lx arguments to LibraryLoads.
2346 for (auto LibItr = Libraries.begin(), LibEnd = Libraries.end();
2347 LibItr != LibEnd; ++LibItr) {
2348 LibraryLoad LL;
2349 LL.LibName = *LibItr;
2350 LL.Position = Libraries.getPosition(optnum: LibItr - Libraries.begin());
2351 LL.CandidateExtensions = StandardExtensions;
2352 LL.Modifier = LibraryLoad::Standard;
2353 LibraryLoadQueue.push_back(x: std::move(LL));
2354 }
2355
2356 // Add -hidden-lx arguments to LibraryLoads.
2357 for (auto LibHiddenItr = LibrariesHidden.begin(),
2358 LibHiddenEnd = LibrariesHidden.end();
2359 LibHiddenItr != LibHiddenEnd; ++LibHiddenItr) {
2360 LibraryLoad LL;
2361 LL.LibName = *LibHiddenItr;
2362 LL.Position =
2363 LibrariesHidden.getPosition(optnum: LibHiddenItr - LibrariesHidden.begin());
2364 LL.CandidateExtensions = ArchiveExtensionsOnly;
2365 LL.Modifier = LibraryLoad::Hidden;
2366 LibraryLoadQueue.push_back(x: std::move(LL));
2367 }
2368
2369 // Add -weak-lx arguments to LibraryLoads.
2370 for (auto LibWeakItr = LibrariesWeak.begin(),
2371 LibWeakEnd = LibrariesWeak.end();
2372 LibWeakItr != LibWeakEnd; ++LibWeakItr) {
2373 LibraryLoad LL;
2374 LL.LibName = *LibWeakItr;
2375 LL.Position = LibrariesWeak.getPosition(optnum: LibWeakItr - LibrariesWeak.begin());
2376 LL.CandidateExtensions = WeakLinkExtensionsOnly;
2377 LL.Modifier = LibraryLoad::Weak;
2378 LibraryLoadQueue.push_back(x: std::move(LL));
2379 }
2380
2381 // Sort library loads by position in the argument list.
2382 llvm::sort(C&: LibraryLoadQueue,
2383 Comp: [](const LibraryLoad &LHS, const LibraryLoad &RHS) {
2384 return LHS.Position < RHS.Position;
2385 });
2386
2387 // 3. Process library loads.
2388 auto AddArchive = [&](JITDylib &JD, const char *Path, const LibraryLoad &LL)
2389 -> Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> {
2390 StaticLibraryDefinitionGenerator::GetObjectFileInterface
2391 GetObjFileInterface;
2392 switch (LL.Modifier) {
2393 case LibraryLoad::Standard:
2394 GetObjFileInterface = getObjectFileInterface;
2395 break;
2396 case LibraryLoad::Hidden:
2397 GetObjFileInterface = getObjectFileInterfaceHidden;
2398 S.HiddenArchives.insert(key: Path);
2399 break;
2400 case LibraryLoad::Weak:
2401 llvm_unreachable("Unsupported");
2402 break;
2403 }
2404
2405 auto &LinkLayer = S.getLinkLayer(Lazy: LazyLinkIdxs.count(V: LL.Position));
2406
2407 std::set<std::string> ImportedDynamicLibraries;
2408 StaticLibraryDefinitionGenerator::VisitMembersFunction VisitMembers;
2409
2410 // COFF gets special handling due to import libraries.
2411 if (S.ES.getTargetTriple().isOSBinFormatCOFF()) {
2412 if (AllLoad) {
2413 VisitMembers =
2414 [ImportScanner = COFFImportFileScanner(ImportedDynamicLibraries),
2415 LoadAll =
2416 StaticLibraryDefinitionGenerator::loadAllObjectFileMembers(
2417 L&: LinkLayer, JD)](object::Archive &A,
2418 MemoryBufferRef MemberBuf,
2419 size_t Index) mutable -> Expected<bool> {
2420 if (!ImportScanner(A, MemberBuf, Index))
2421 return false;
2422 return LoadAll(A, MemberBuf, Index);
2423 };
2424 } else
2425 VisitMembers = COFFImportFileScanner(ImportedDynamicLibraries);
2426 } else if (AllLoad)
2427 VisitMembers = StaticLibraryDefinitionGenerator::loadAllObjectFileMembers(
2428 L&: LinkLayer, JD);
2429 else if (S.ES.getTargetTriple().isOSBinFormatMachO() && ForceLoadObjC)
2430 VisitMembers = ForceLoadMachOArchiveMembers(LinkLayer, JD, true);
2431
2432 auto G = StaticLibraryDefinitionGenerator::Load(
2433 L&: LinkLayer, FileName: Path, VisitMembers: std::move(VisitMembers),
2434 GetObjFileInterface: std::move(GetObjFileInterface));
2435 if (!G)
2436 return G.takeError();
2437
2438 // Push additional dynamic libraries to search.
2439 // Note that this mechanism only happens in COFF.
2440 for (auto FileName : ImportedDynamicLibraries) {
2441 LibraryLoad NewLL;
2442 auto FileNameRef = StringRef(FileName);
2443 if (!FileNameRef.ends_with_insensitive(Suffix: ".dll"))
2444 return make_error<StringError>(
2445 Args: "COFF Imported library not ending with dll extension?",
2446 Args: inconvertibleErrorCode());
2447 NewLL.LibName = FileNameRef.drop_back(N: strlen(s: ".dll")).str();
2448 NewLL.Position = LL.Position;
2449 NewLL.CandidateExtensions = DynLibExtensionsOnly;
2450 NewLL.Modifier = LibraryLoad::Standard;
2451 LibraryLoadQueue.push_front(x: std::move(NewLL));
2452 }
2453 return G;
2454 };
2455
2456 SmallVector<StringRef, 5> SystemSearchPaths;
2457 if (SearchSystemLibrary.getValue())
2458 SystemSearchPaths = getSearchPathsFromEnvVar(S);
2459 while (!LibraryLoadQueue.empty()) {
2460 bool LibFound = false;
2461 auto LL = LibraryLoadQueue.front();
2462 LibraryLoadQueue.pop_front();
2463 auto &JD = *std::prev(x: IdxToJD.lower_bound(x: LL.Position))->second;
2464
2465 // If this is the name of a JITDylib then link against that.
2466 if (auto *LJD = S.ES.getJITDylibByName(Name: LL.LibName)) {
2467 if (LL.Modifier == LibraryLoad::Weak)
2468 return make_error<StringError>(
2469 Args: "Can't use -weak-lx or -weak_library to load JITDylib " +
2470 LL.LibName,
2471 Args: inconvertibleErrorCode());
2472 JD.addToLinkOrder(JD&: *LJD);
2473 continue;
2474 }
2475
2476 if (LL.IsPath) {
2477 // Must be -weak_library.
2478 if (LL.Modifier == LibraryLoad::Weak) {
2479 if (auto G = LoadLibraryWeak(S, Path: LL.LibName)) {
2480 JD.addGenerator(DefGenerator: std::move(*G));
2481 continue;
2482 } else
2483 return G.takeError();
2484 }
2485
2486 // Otherwise handle archive.
2487 auto G = AddArchive(JD, LL.LibName.c_str(), LL);
2488 if (!G)
2489 return createFileError(F: LL.LibName, E: G.takeError());
2490 JD.addGenerator(DefGenerator: std::move(*G));
2491 LLVM_DEBUG({
2492 dbgs() << "Adding generator for static library " << LL.LibName << " to "
2493 << JD.getName() << "\n";
2494 });
2495 continue;
2496 }
2497
2498 // Otherwise look through the search paths.
2499 auto CurJDSearchPaths = JDSearchPaths[&JD];
2500 for (StringRef SearchPath :
2501 concat<StringRef>(Ranges&: CurJDSearchPaths, Ranges&: SystemSearchPaths)) {
2502 for (auto LibExt : LL.CandidateExtensions) {
2503 SmallVector<char, 256> LibPath;
2504 LibPath.reserve(N: SearchPath.size() + strlen(s: "lib") + LL.LibName.size() +
2505 LibExt.size() + 2); // +2 for pathsep, null term.
2506 llvm::append_range(C&: LibPath, R&: SearchPath);
2507 if (LibExt != ".lib" && LibExt != ".dll")
2508 sys::path::append(path&: LibPath, a: "lib" + LL.LibName + LibExt);
2509 else
2510 sys::path::append(path&: LibPath, a: LL.LibName + LibExt);
2511 LibPath.push_back(Elt: '\0');
2512
2513 // Skip missing or non-regular paths.
2514 if (sys::fs::get_file_type(Path: LibPath.data()) !=
2515 sys::fs::file_type::regular_file) {
2516 continue;
2517 }
2518
2519 file_magic Magic;
2520 if (auto EC = identify_magic(path: LibPath, result&: Magic)) {
2521 // If there was an error loading the file then skip it.
2522 LLVM_DEBUG({
2523 dbgs() << "Library search found \"" << LibPath
2524 << "\", but could not identify file type (" << EC.message()
2525 << "). Skipping.\n";
2526 });
2527 continue;
2528 }
2529
2530 // We identified the magic. Assume that we can load it -- we'll reset
2531 // in the default case.
2532 LibFound = true;
2533 switch (Magic) {
2534 case file_magic::pecoff_executable:
2535 case file_magic::elf_shared_object:
2536 case file_magic::macho_dynamically_linked_shared_lib: {
2537 if (LL.Modifier == LibraryLoad::Weak) {
2538 if (auto G = LoadLibraryWeak(S, Path: LibPath.data()))
2539 JD.addGenerator(DefGenerator: std::move(*G));
2540 else
2541 return G.takeError();
2542 } else {
2543 if (auto Err = S.loadAndLinkDynamicLibrary(JD, LibPath: LibPath.data()))
2544 return Err;
2545 }
2546 break;
2547 }
2548 case file_magic::archive:
2549 case file_magic::macho_universal_binary: {
2550 auto G = AddArchive(JD, LibPath.data(), LL);
2551 if (!G)
2552 return G.takeError();
2553 JD.addGenerator(DefGenerator: std::move(*G));
2554 LLVM_DEBUG({
2555 dbgs() << "Adding generator for static library " << LibPath.data()
2556 << " to " << JD.getName() << "\n";
2557 });
2558 break;
2559 }
2560 case file_magic::tapi_file:
2561 assert(LL.Modifier == LibraryLoad::Weak &&
2562 "TextAPI file not being loaded as weak?");
2563 if (auto G = LoadLibraryWeak(S, Path: LibPath.data()))
2564 JD.addGenerator(DefGenerator: std::move(*G));
2565 else
2566 return G.takeError();
2567 break;
2568 default:
2569 // This file isn't a recognized library kind.
2570 LLVM_DEBUG({
2571 dbgs() << "Library search found \"" << LibPath
2572 << "\", but file type is not supported. Skipping.\n";
2573 });
2574 LibFound = false;
2575 break;
2576 }
2577 if (LibFound)
2578 break;
2579 }
2580 if (LibFound)
2581 break;
2582 }
2583
2584 if (!LibFound)
2585 return make_error<StringError>(Args: "While linking " + JD.getName() +
2586 ", could not find library for -l" +
2587 LL.LibName,
2588 Args: inconvertibleErrorCode());
2589 }
2590
2591 // Add platform and process symbols if available.
2592 for (auto &[Idx, JD] : IdxToJD) {
2593 if (S.PlatformJD)
2594 JD->addToLinkOrder(JD&: *S.PlatformJD);
2595 if (S.ProcessSymsJD)
2596 JD->addToLinkOrder(JD&: *S.ProcessSymsJD);
2597 }
2598
2599 return Error::success();
2600}
2601
2602static Error addSpeculationOrder(Session &S) {
2603
2604 if (SpeculateOrder.empty())
2605 return Error::success();
2606
2607 assert(S.LazyLinking && "SpeculateOrder set, but lazy linking not enabled");
2608 assert(S.LazyLinking->Speculator && "SpeculatoOrder set, but no speculator");
2609
2610 auto SpecOrderBuffer = getFile(FileName: SpeculateOrder);
2611 if (!SpecOrderBuffer)
2612 return SpecOrderBuffer.takeError();
2613
2614 StringRef LineStream((*SpecOrderBuffer)->getBuffer());
2615 std::vector<std::pair<std::string, SymbolStringPtr>> SpecOrder;
2616
2617 size_t LineNumber = 0;
2618 while (!LineStream.empty()) {
2619 ++LineNumber;
2620
2621 auto MakeSpecOrderErr = [&](StringRef Reason) {
2622 return make_error<StringError>(Args: "Error in speculation order file \"" +
2623 SpeculateOrder + "\" on line " +
2624 Twine(LineNumber) + ": " + Reason,
2625 Args: inconvertibleErrorCode());
2626 };
2627
2628 StringRef CurLine;
2629 std::tie(args&: CurLine, args&: LineStream) = LineStream.split(Separator: '\n');
2630 CurLine = CurLine.trim();
2631 if (CurLine.empty())
2632 continue;
2633
2634 auto [JDName, FuncName] = CurLine.split(Separator: ',');
2635
2636 if (FuncName.empty())
2637 return MakeSpecOrderErr("missing ',' separator");
2638
2639 JDName = JDName.trim();
2640 if (JDName.empty())
2641 return MakeSpecOrderErr("no value for column 1 (JIT Dylib name)");
2642
2643 FuncName = FuncName.trim();
2644 if (FuncName.empty())
2645 return MakeSpecOrderErr("no value for column 2 (function name)");
2646
2647 SpecOrder.push_back(x: {JDName.str(), S.ES.intern(SymName: FuncName)});
2648 }
2649
2650 S.LazyLinking->Speculator->addSpeculationSuggestions(NewSuggestions: std::move(SpecOrder));
2651
2652 return Error::success();
2653}
2654
2655static Error addSessionInputs(Session &S) {
2656 std::map<unsigned, JITDylib *> IdxToJD;
2657 DenseSet<unsigned> LazyLinkIdxs;
2658
2659 for (auto LLItr = LazyLink.begin(), LLEnd = LazyLink.end(); LLItr != LLEnd;
2660 ++LLItr) {
2661 if (*LLItr)
2662 LazyLinkIdxs.insert(V: LazyLink.getPosition(optnum: LLItr - LazyLink.begin()) + 1);
2663 }
2664
2665 if (auto Err = createJITDylibs(S, IdxToJD))
2666 return Err;
2667
2668 if (auto Err = addAbsoluteSymbols(S, IdxToJD))
2669 return Err;
2670
2671 if (auto Err = addAliases(S, IdxToJD))
2672 return Err;
2673
2674 if (auto Err = addSectCreates(S, IdxToJD))
2675 return Err;
2676
2677 if (!TestHarnesses.empty())
2678 if (auto Err = addTestHarnesses(S))
2679 return Err;
2680
2681 if (auto Err = addObjects(S, IdxToJD, LazyLinkIdxs))
2682 return Err;
2683
2684 if (auto Err = addLibraries(S, IdxToJD, LazyLinkIdxs))
2685 return Err;
2686
2687 if (auto Err = addSpeculationOrder(S))
2688 return Err;
2689
2690 return Error::success();
2691}
2692
2693namespace {
2694struct TargetInfo {
2695 const Target *TheTarget;
2696 std::unique_ptr<MCSubtargetInfo> STI;
2697 std::unique_ptr<MCRegisterInfo> MRI;
2698 std::unique_ptr<MCAsmInfo> MAI;
2699 std::unique_ptr<MCContext> Ctx;
2700 std::unique_ptr<MCDisassembler> Disassembler;
2701 std::unique_ptr<MCInstrInfo> MII;
2702 std::unique_ptr<MCInstrAnalysis> MIA;
2703 std::unique_ptr<MCInstPrinter> InstPrinter;
2704};
2705} // anonymous namespace
2706
2707static TargetInfo
2708getTargetInfo(const Triple &TT,
2709 const SubtargetFeatures &TF = SubtargetFeatures()) {
2710 std::string ErrorStr;
2711 const Target *TheTarget = TargetRegistry::lookupTarget(TheTriple: TT, Error&: ErrorStr);
2712 if (!TheTarget)
2713 ExitOnErr(make_error<StringError>(Args: "Error accessing target '" + TT.str() +
2714 "': " + ErrorStr,
2715 Args: inconvertibleErrorCode()));
2716
2717 std::unique_ptr<MCSubtargetInfo> STI(
2718 TheTarget->createMCSubtargetInfo(TheTriple: TT, CPU: "", Features: TF.getString()));
2719 if (!STI)
2720 ExitOnErr(
2721 make_error<StringError>(Args: "Unable to create subtarget for " + TT.str(),
2722 Args: inconvertibleErrorCode()));
2723
2724 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
2725 if (!MRI)
2726 ExitOnErr(make_error<StringError>(Args: "Unable to create target register info "
2727 "for " +
2728 TT.str(),
2729 Args: inconvertibleErrorCode()));
2730
2731 MCTargetOptions MCOptions;
2732 std::unique_ptr<MCAsmInfo> MAI(
2733 TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple: TT, Options: MCOptions));
2734 if (!MAI)
2735 ExitOnErr(
2736 make_error<StringError>(Args: "Unable to create target asm info " + TT.str(),
2737 Args: inconvertibleErrorCode()));
2738
2739 auto Ctx = std::make_unique<MCContext>(args: Triple(TT.str()), args: MAI.get(), args: MRI.get(),
2740 args: STI.get());
2741
2742 std::unique_ptr<MCDisassembler> Disassembler(
2743 TheTarget->createMCDisassembler(STI: *STI, Ctx&: *Ctx));
2744 if (!Disassembler)
2745 ExitOnErr(
2746 make_error<StringError>(Args: "Unable to create disassembler for " + TT.str(),
2747 Args: inconvertibleErrorCode()));
2748
2749 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
2750 if (!MII)
2751 ExitOnErr(make_error<StringError>(Args: "Unable to create instruction info for" +
2752 TT.str(),
2753 Args: inconvertibleErrorCode()));
2754
2755 std::unique_ptr<MCInstrAnalysis> MIA(
2756 TheTarget->createMCInstrAnalysis(Info: MII.get()));
2757 if (!MIA)
2758 ExitOnErr(make_error<StringError>(
2759 Args: "Unable to create instruction analysis for" + TT.str(),
2760 Args: inconvertibleErrorCode()));
2761
2762 std::unique_ptr<MCInstPrinter> InstPrinter(
2763 TheTarget->createMCInstPrinter(T: Triple(TT.str()), SyntaxVariant: 0, MAI: *MAI, MII: *MII, MRI: *MRI));
2764 if (!InstPrinter)
2765 ExitOnErr(make_error<StringError>(
2766 Args: "Unable to create instruction printer for" + TT.str(),
2767 Args: inconvertibleErrorCode()));
2768 return {.TheTarget: TheTarget, .STI: std::move(STI), .MRI: std::move(MRI),
2769 .MAI: std::move(MAI), .Ctx: std::move(Ctx), .Disassembler: std::move(Disassembler),
2770 .MII: std::move(MII), .MIA: std::move(MIA), .InstPrinter: std::move(InstPrinter)};
2771}
2772static Error runChecks(Session &S, Triple TT, SubtargetFeatures Features) {
2773 if (CheckFiles.empty())
2774 return Error::success();
2775
2776 S.waitForFilesLinkedFromEntryPointFile();
2777
2778 LLVM_DEBUG(dbgs() << "Running checks...\n");
2779
2780 auto IsSymbolValid = [&S](StringRef Symbol) {
2781 auto InternedSymbol = S.ES.getSymbolStringPool()->intern(S: Symbol);
2782 return S.isSymbolRegistered(SymbolName: InternedSymbol);
2783 };
2784
2785 auto GetSymbolInfo = [&S](StringRef Symbol) {
2786 auto InternedSymbol = S.ES.getSymbolStringPool()->intern(S: Symbol);
2787 return S.findSymbolInfo(SymbolName: InternedSymbol, ErrorMsgStem: "Can not get symbol info");
2788 };
2789
2790 auto GetSectionInfo = [&S](StringRef FileName, StringRef SectionName) {
2791 return S.findSectionInfo(FileName, SectionName);
2792 };
2793
2794 auto GetStubInfo = [&S](StringRef FileName, StringRef SectionName,
2795 StringRef KindNameFilter) {
2796 return S.findStubInfo(FileName, TargetName: SectionName, KindNameFilter);
2797 };
2798
2799 auto GetGOTInfo = [&S](StringRef FileName, StringRef SectionName) {
2800 return S.findGOTEntryInfo(FileName, TargetName: SectionName);
2801 };
2802
2803 RuntimeDyldChecker Checker(
2804 IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo, GetGOTInfo,
2805 S.ES.getTargetTriple().isLittleEndian() ? llvm::endianness::little
2806 : llvm::endianness::big,
2807 TT, StringRef(), Features, dbgs());
2808
2809 std::string CheckLineStart = "# " + CheckName + ":";
2810 for (auto &CheckFile : CheckFiles) {
2811 auto CheckerFileBuf = ExitOnErr(getFile(FileName: CheckFile));
2812 if (!Checker.checkAllRulesInBuffer(RulePrefix: CheckLineStart, MemBuf: &*CheckerFileBuf))
2813 ExitOnErr(make_error<StringError>(
2814 Args: "Some checks in " + CheckFile + " failed", Args: inconvertibleErrorCode()));
2815 }
2816
2817 return Error::success();
2818}
2819
2820static Error addSelfRelocations(LinkGraph &G) {
2821 auto TI = getTargetInfo(TT: G.getTargetTriple());
2822 for (auto *Sym : G.defined_symbols())
2823 if (Sym->isCallable())
2824 if (auto Err = addFunctionPointerRelocationsToCurrentSymbol(
2825 Sym&: *Sym, G, Disassembler&: *TI.Disassembler, MIA&: *TI.MIA))
2826 return Err;
2827 return Error::success();
2828}
2829
2830static Expected<ExecutorSymbolDef> getMainEntryPoint(Session &S) {
2831 return S.ES.lookup(SearchOrder: S.JDSearchOrder, Symbol: S.ES.intern(SymName: EntryPointName));
2832}
2833
2834static Expected<ExecutorSymbolDef> getOrcRuntimeEntryPoint(Session &S) {
2835 std::string RuntimeEntryPoint = "__orc_rt_run_program_wrapper";
2836 if (S.ES.getTargetTriple().getObjectFormat() == Triple::MachO)
2837 RuntimeEntryPoint = '_' + RuntimeEntryPoint;
2838 return S.ES.lookup(SearchOrder: S.JDSearchOrder, Symbol: S.ES.intern(SymName: RuntimeEntryPoint));
2839}
2840
2841static Expected<ExecutorSymbolDef> getEntryPoint(Session &S) {
2842 ExecutorSymbolDef EntryPoint;
2843
2844 // Find the entry-point function unconditionally, since we want to force
2845 // it to be materialized to collect stats.
2846 if (auto EP = getMainEntryPoint(S))
2847 EntryPoint = *EP;
2848 else
2849 return EP.takeError();
2850 LLVM_DEBUG({
2851 dbgs() << "Using entry point \"" << EntryPointName
2852 << "\": " << formatv("{0:x16}", EntryPoint.getAddress()) << "\n";
2853 });
2854
2855 // If we're running with the ORC runtime then replace the entry-point
2856 // with the __orc_rt_run_program symbol.
2857 if (!OrcRuntime.empty()) {
2858 if (auto EP = getOrcRuntimeEntryPoint(S))
2859 EntryPoint = *EP;
2860 else
2861 return EP.takeError();
2862 LLVM_DEBUG({
2863 dbgs() << "(called via __orc_rt_run_program_wrapper at "
2864 << formatv("{0:x16}", EntryPoint.getAddress()) << ")\n";
2865 });
2866 }
2867
2868 return EntryPoint;
2869}
2870
2871static Expected<int> runWithRuntime(Session &S, ExecutorAddr EntryPointAddr) {
2872 StringRef DemangledEntryPoint = EntryPointName;
2873 if (S.ES.getTargetTriple().getObjectFormat() == Triple::MachO &&
2874 DemangledEntryPoint.front() == '_')
2875 DemangledEntryPoint = DemangledEntryPoint.drop_front();
2876 using llvm::orc::shared::SPSString;
2877 using SPSRunProgramSig =
2878 int64_t(SPSString, SPSString, shared::SPSSequence<SPSString>);
2879 int64_t Result;
2880 if (auto Err = S.ES.callSPSWrapper<SPSRunProgramSig>(
2881 WrapperFnAddr: EntryPointAddr, WrapperCallArgs&: Result, WrapperCallArgs: S.MainJD->getName(), WrapperCallArgs&: DemangledEntryPoint,
2882 WrapperCallArgs&: static_cast<std::vector<std::string> &>(InputArgv)))
2883 return std::move(Err);
2884 return Result;
2885}
2886
2887static Expected<int> runWithoutRuntime(Session &S,
2888 ExecutorAddr EntryPointAddr) {
2889 return S.ES.getExecutorProcessControl().runAsMain(MainFnAddr: EntryPointAddr, Args: InputArgv);
2890}
2891
2892static Error symbolicateBacktraces() {
2893 auto Symtab = DumpedSymbolTable::Create(Path: SymbolicateWith);
2894 if (!Symtab)
2895 return Symtab.takeError();
2896
2897 for (auto InputFile : InputFiles) {
2898 auto BacktraceBuffer = MemoryBuffer::getFileOrSTDIN(Filename: InputFile);
2899 if (!BacktraceBuffer)
2900 return createFileError(F: InputFile, EC: BacktraceBuffer.getError());
2901
2902 outs() << Symtab->symbolicate(Backtrace: (*BacktraceBuffer)->getBuffer());
2903 }
2904
2905 return Error::success();
2906}
2907
2908static Error waitingOnGraphReplay() {
2909 // Warn about ignored options.
2910 {
2911 bool PrintedHeader = false;
2912 for (auto &[OptName, Opt] : cl::getRegisteredOptions()) {
2913 if (Opt == &WaitingOnGraphReplay)
2914 continue;
2915 if (Opt->getNumOccurrences()) {
2916 if (!PrintedHeader) {
2917 errs() << "Warning: Running in -waiting-on-graph-replay mode. "
2918 "The following options will be ignored:\n";
2919 PrintedHeader = true;
2920 }
2921 errs() << " " << OptName << "\n";
2922 }
2923 }
2924 }
2925
2926 // Read the replay buffer file.
2927 auto GraphOpsBuffer = getFile(FileName: WaitingOnGraphReplay);
2928 if (!GraphOpsBuffer)
2929 return GraphOpsBuffer.takeError();
2930
2931 using Replay = orc::detail::WaitingOnGraphOpReplay<uintptr_t, uintptr_t>;
2932 using Graph = typename Replay::Graph;
2933 using Replayer = typename Replay::Replayer;
2934
2935 std::vector<typename Replay::Op> RecordedOps;
2936
2937 // First read the buffer to build the Ops vector. Doing this up-front allows
2938 // us to avoid polluting the timings below with the cost of parsing.
2939 Error Err = Error::success();
2940 for (auto &Op :
2941 orc::detail::readWaitingOnGraphOpsFromBuffer<uintptr_t, uintptr_t>(
2942 InputBuffer: (*GraphOpsBuffer)->getBuffer(), Err))
2943 RecordedOps.push_back(x: std::move(Op));
2944 if (Err)
2945 return Err;
2946
2947 // Now replay the Ops:
2948 Graph G;
2949 Replayer R(G);
2950
2951 outs() << "Replaying WaitingOnGraph operations from " << WaitingOnGraphReplay
2952 << "...\n";
2953 auto ReplayStart = std::chrono::high_resolution_clock::now();
2954 for (auto &Op : RecordedOps)
2955 R.replay(O: std::move(Op));
2956 auto ReplayEnd = std::chrono::high_resolution_clock::now();
2957 std::chrono::duration<double> ReplayDiff = ReplayEnd - ReplayStart;
2958 outs() << ReplayDiff.count() << "s to replay " << RecordedOps.size()
2959 << " ops (wall-clock time)\n";
2960 return Error::success();
2961}
2962
2963namespace {
2964struct JITLinkTimers {
2965 TimerGroup JITLinkTG{"llvm-jitlink timers", "timers for llvm-jitlink phases"};
2966 Timer LoadObjectsTimer{"load", "time to load/add object files", JITLinkTG};
2967 Timer LinkTimer{"link", "time to link object files", JITLinkTG};
2968 Timer RunTimer{"run", "time to execute jitlink'd code", JITLinkTG};
2969};
2970} // namespace
2971
2972int main(int argc, char *argv[]) {
2973 InitLLVM X(argc, argv);
2974
2975 InitializeAllTargetInfos();
2976 InitializeAllTargetMCs();
2977 InitializeAllDisassemblers();
2978
2979 cl::HideUnrelatedOptions(Categories: {&JITLinkCategory, &getColorCategory()});
2980 cl::ParseCommandLineOptions(argc, argv, Overview: "llvm jitlink tool");
2981 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
2982
2983 // Check for WaitingOnGraph replay mode.
2984 if (!WaitingOnGraphReplay.empty()) {
2985 ExitOnErr(waitingOnGraphReplay());
2986 return 0;
2987 }
2988
2989 /// If timers are enabled, create a JITLinkTimers instance.
2990 std::unique_ptr<JITLinkTimers> Timers =
2991 ShowTimes ? std::make_unique<JITLinkTimers>() : nullptr;
2992
2993 auto [TT, Features] = getFirstFileTripleAndFeatures();
2994 ExitOnErr(sanitizeArguments(TT, ArgV0: argv[0]));
2995
2996 if (!SymbolicateWith.empty()) {
2997 ExitOnErr(symbolicateBacktraces());
2998 return 0;
2999 }
3000
3001 auto S = ExitOnErr(Session::Create(TT, Features));
3002
3003 enableStatistics(S&: *S, UsingOrcRuntime: !OrcRuntime.empty());
3004
3005 {
3006 TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
3007 ExitOnErr(addSessionInputs(S&: *S));
3008 }
3009
3010 if (PhonyExternals)
3011 addPhonyExternalsGenerator(S&: *S);
3012
3013 if (ShowInitialExecutionSessionState)
3014 S->ES.dump(OS&: outs());
3015
3016 Expected<ExecutorSymbolDef> EntryPoint((ExecutorSymbolDef()));
3017 {
3018 ExpectedAsOutParameter<ExecutorSymbolDef> _(&EntryPoint);
3019 TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
3020 EntryPoint = getEntryPoint(S&: *S);
3021 }
3022
3023 // Print any reports regardless of whether we succeeded or failed.
3024 if (ShowEntryExecutionSessionState)
3025 S->ES.dump(OS&: outs());
3026
3027 if (ShowAddrs)
3028 S->dumpSessionInfo(OS&: outs());
3029
3030 if (!EntryPoint) {
3031 if (Timers)
3032 Timers->JITLinkTG.printAll(OS&: errs());
3033 reportLLVMJITLinkError(Err: EntryPoint.takeError());
3034 ExitOnErr(S->ES.endSession());
3035 exit(status: 1);
3036 }
3037
3038 ExitOnErr(runChecks(S&: *S, TT: std::move(TT), Features: std::move(Features)));
3039
3040 int Result = 0;
3041 if (!NoExec) {
3042 LLVM_DEBUG(dbgs() << "Running \"" << EntryPointName << "\"...\n");
3043 TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
3044 if (!OrcRuntime.empty())
3045 Result = ExitOnErr(runWithRuntime(S&: *S, EntryPointAddr: EntryPoint->getAddress()));
3046 else
3047 Result = ExitOnErr(runWithoutRuntime(S&: *S, EntryPointAddr: EntryPoint->getAddress()));
3048 }
3049
3050 // Destroy the session.
3051 ExitOnErr(S->ES.endSession());
3052 S.reset();
3053
3054 if (Timers)
3055 Timers->JITLinkTG.printAll(OS&: errs());
3056
3057 // If the executing code set a test result override then use that.
3058 if (UseTestResultOverride)
3059 Result = TestResultOverride;
3060
3061 return Result;
3062}
3063