[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / llvm / tools / llvm-jitlink / llvm-jitlink.cpp
blobd73d247599b9efc589319fecd9771c65bff0fdaf
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-jitlink.h"
17 #include "llvm/BinaryFormat/Magic.h"
18 #include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
19 #include "llvm/ExecutionEngine/Orc/COFFVCRuntimeSupport.h"
20 #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
21 #include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h"
22 #include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
23 #include "llvm/ExecutionEngine/Orc/Debugging/PerfSupportPlugin.h"
24 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
25 #include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"
26 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
27 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
28 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
29 #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
30 #include "llvm/ExecutionEngine/Orc/MachOPlatform.h"
31 #include "llvm/ExecutionEngine/Orc/MapperJITLinkMemoryManager.h"
32 #include "llvm/ExecutionEngine/Orc/ObjectFileInterface.h"
33 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
34 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
35 #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderPerf.h"
36 #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
37 #include "llvm/MC/MCAsmInfo.h"
38 #include "llvm/MC/MCContext.h"
39 #include "llvm/MC/MCDisassembler/MCDisassembler.h"
40 #include "llvm/MC/MCInstPrinter.h"
41 #include "llvm/MC/MCInstrAnalysis.h"
42 #include "llvm/MC/MCInstrInfo.h"
43 #include "llvm/MC/MCRegisterInfo.h"
44 #include "llvm/MC/MCSubtargetInfo.h"
45 #include "llvm/MC/MCTargetOptions.h"
46 #include "llvm/MC/TargetRegistry.h"
47 #include "llvm/Object/COFF.h"
48 #include "llvm/Object/MachO.h"
49 #include "llvm/Object/ObjectFile.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/InitLLVM.h"
53 #include "llvm/Support/MemoryBuffer.h"
54 #include "llvm/Support/Path.h"
55 #include "llvm/Support/Process.h"
56 #include "llvm/Support/TargetSelect.h"
57 #include "llvm/Support/Timer.h"
59 #include <cstring>
60 #include <deque>
61 #include <list>
62 #include <string>
64 #ifdef LLVM_ON_UNIX
65 #include <netdb.h>
66 #include <netinet/in.h>
67 #include <sys/socket.h>
68 #include <unistd.h>
69 #endif // LLVM_ON_UNIX
71 #define DEBUG_TYPE "llvm_jitlink"
73 using namespace llvm;
74 using namespace llvm::jitlink;
75 using namespace llvm::orc;
77 static cl::OptionCategory JITLinkCategory("JITLink Options");
79 static cl::list<std::string> InputFiles(cl::Positional, cl::OneOrMore,
80 cl::desc("input files"),
81 cl::cat(JITLinkCategory));
83 static cl::list<std::string>
84 LibrarySearchPaths("L",
85 cl::desc("Add dir to the list of library search paths"),
86 cl::Prefix, cl::cat(JITLinkCategory));
88 static cl::list<std::string>
89 Libraries("l",
90 cl::desc("Link against library X in the library search paths"),
91 cl::Prefix, cl::cat(JITLinkCategory));
93 static cl::list<std::string>
94 LibrariesHidden("hidden-l",
95 cl::desc("Link against library X in the library search "
96 "paths with hidden visibility"),
97 cl::Prefix, cl::cat(JITLinkCategory));
99 static cl::list<std::string>
100 LoadHidden("load_hidden",
101 cl::desc("Link against library X with hidden visibility"),
102 cl::cat(JITLinkCategory));
104 static cl::opt<bool> SearchSystemLibrary(
105 "search-sys-lib",
106 cl::desc("Add system library paths to library search paths"),
107 cl::init(false), cl::cat(JITLinkCategory));
109 static cl::opt<bool> NoExec("noexec", cl::desc("Do not execute loaded code"),
110 cl::init(false), cl::cat(JITLinkCategory));
112 static cl::list<std::string>
113 CheckFiles("check", cl::desc("File containing verifier checks"),
114 cl::cat(JITLinkCategory));
116 static cl::opt<std::string>
117 CheckName("check-name", cl::desc("Name of checks to match against"),
118 cl::init("jitlink-check"), cl::cat(JITLinkCategory));
120 static cl::opt<std::string>
121 EntryPointName("entry", cl::desc("Symbol to call as main entry point"),
122 cl::init(""), cl::cat(JITLinkCategory));
124 static cl::list<std::string> JITDylibs(
125 "jd",
126 cl::desc("Specifies the JITDylib to be used for any subsequent "
127 "input file, -L<seacrh-path>, and -l<library> arguments"),
128 cl::cat(JITLinkCategory));
130 static cl::list<std::string>
131 Dylibs("preload",
132 cl::desc("Pre-load dynamic libraries (e.g. language runtimes "
133 "required by the ORC runtime)"),
134 cl::cat(JITLinkCategory));
136 static cl::list<std::string> InputArgv("args", cl::Positional,
137 cl::desc("<program arguments>..."),
138 cl::PositionalEatsArgs,
139 cl::cat(JITLinkCategory));
141 static cl::opt<bool>
142 DebuggerSupport("debugger-support",
143 cl::desc("Enable debugger suppport (default = !-noexec)"),
144 cl::init(true), cl::Hidden, cl::cat(JITLinkCategory));
146 static cl::opt<bool> PerfSupport("perf-support",
147 cl::desc("Enable perf profiling support"),
148 cl::init(false), cl::Hidden,
149 cl::cat(JITLinkCategory));
151 static cl::opt<bool>
152 NoProcessSymbols("no-process-syms",
153 cl::desc("Do not resolve to llvm-jitlink process symbols"),
154 cl::init(false), cl::cat(JITLinkCategory));
156 static cl::list<std::string> AbsoluteDefs(
157 "abs",
158 cl::desc("Inject absolute symbol definitions (syntax: <name>=<addr>)"),
159 cl::cat(JITLinkCategory));
161 static cl::list<std::string>
162 Aliases("alias",
163 cl::desc("Inject symbol aliases (syntax: <alias-name>=<aliasee>)"),
164 cl::cat(JITLinkCategory));
166 static cl::list<std::string> TestHarnesses("harness", cl::Positional,
167 cl::desc("Test harness files"),
168 cl::PositionalEatsArgs,
169 cl::cat(JITLinkCategory));
171 static cl::opt<bool> ShowInitialExecutionSessionState(
172 "show-init-es",
173 cl::desc("Print ExecutionSession state before resolving entry point"),
174 cl::init(false), cl::cat(JITLinkCategory));
176 static cl::opt<bool> ShowEntryExecutionSessionState(
177 "show-entry-es",
178 cl::desc("Print ExecutionSession state after resolving entry point"),
179 cl::init(false), cl::cat(JITLinkCategory));
181 static cl::opt<bool> ShowAddrs(
182 "show-addrs",
183 cl::desc("Print registered symbol, section, got and stub addresses"),
184 cl::init(false), cl::cat(JITLinkCategory));
186 static cl::opt<std::string> ShowLinkGraphs(
187 "show-graphs",
188 cl::desc("Takes a posix regex and prints the link graphs of all files "
189 "matching that regex after fixups have been applied"),
190 cl::Optional, cl::cat(JITLinkCategory));
192 static cl::opt<bool> ShowTimes("show-times",
193 cl::desc("Show times for llvm-jitlink phases"),
194 cl::init(false), cl::cat(JITLinkCategory));
196 static cl::opt<std::string> SlabAllocateSizeString(
197 "slab-allocate",
198 cl::desc("Allocate from a slab of the given size "
199 "(allowable suffixes: Kb, Mb, Gb. default = "
200 "Kb)"),
201 cl::init(""), cl::cat(JITLinkCategory));
203 static cl::opt<uint64_t> SlabAddress(
204 "slab-address",
205 cl::desc("Set slab target address (requires -slab-allocate and -noexec)"),
206 cl::init(~0ULL), cl::cat(JITLinkCategory));
208 static cl::opt<uint64_t> SlabPageSize(
209 "slab-page-size",
210 cl::desc("Set page size for slab (requires -slab-allocate and -noexec)"),
211 cl::init(0), cl::cat(JITLinkCategory));
213 static cl::opt<bool> ShowRelocatedSectionContents(
214 "show-relocated-section-contents",
215 cl::desc("show section contents after fixups have been applied"),
216 cl::init(false), cl::cat(JITLinkCategory));
218 static cl::opt<bool> PhonyExternals(
219 "phony-externals",
220 cl::desc("resolve all otherwise unresolved externals to null"),
221 cl::init(false), cl::cat(JITLinkCategory));
223 static cl::opt<std::string> OutOfProcessExecutor(
224 "oop-executor", cl::desc("Launch an out-of-process executor to run code"),
225 cl::ValueOptional, cl::cat(JITLinkCategory));
227 static cl::opt<std::string> OutOfProcessExecutorConnect(
228 "oop-executor-connect",
229 cl::desc("Connect to an out-of-process executor via TCP"),
230 cl::cat(JITLinkCategory));
232 static cl::opt<std::string>
233 OrcRuntime("orc-runtime", cl::desc("Use ORC runtime from given path"),
234 cl::init(""), cl::cat(JITLinkCategory));
236 static cl::opt<bool> AddSelfRelocations(
237 "add-self-relocations",
238 cl::desc("Add relocations to function pointers to the current function"),
239 cl::init(false), cl::cat(JITLinkCategory));
241 static cl::opt<bool>
242 ShowErrFailedToMaterialize("show-err-failed-to-materialize",
243 cl::desc("Show FailedToMaterialize errors"),
244 cl::init(false), cl::cat(JITLinkCategory));
246 static cl::opt<bool> UseSharedMemory(
247 "use-shared-memory",
248 cl::desc("Use shared memory to transfer generated code and data"),
249 cl::init(false), cl::cat(JITLinkCategory));
251 static ExitOnError ExitOnErr;
253 static LLVM_ATTRIBUTE_USED void linkComponents() {
254 errs() << "Linking in runtime functions\n"
255 << (void *)&llvm_orc_registerEHFrameSectionWrapper << '\n'
256 << (void *)&llvm_orc_deregisterEHFrameSectionWrapper << '\n'
257 << (void *)&llvm_orc_registerJITLoaderGDBWrapper << '\n'
258 << (void *)&llvm_orc_registerJITLoaderGDBAllocAction << '\n'
259 << (void *)&llvm_orc_registerJITLoaderPerfStart << '\n'
260 << (void *)&llvm_orc_registerJITLoaderPerfEnd << '\n'
261 << (void *)&llvm_orc_registerJITLoaderPerfImpl << '\n';
264 static bool UseTestResultOverride = false;
265 static int64_t TestResultOverride = 0;
267 extern "C" LLVM_ATTRIBUTE_USED void
268 llvm_jitlink_setTestResultOverride(int64_t Value) {
269 TestResultOverride = Value;
270 UseTestResultOverride = true;
273 static Error addSelfRelocations(LinkGraph &G);
275 namespace {
277 template <typename ErrT>
279 class ConditionalPrintErr {
280 public:
281 ConditionalPrintErr(bool C) : C(C) {}
282 void operator()(ErrT &EI) {
283 if (C) {
284 errs() << "llvm-jitlink error: ";
285 EI.log(errs());
286 errs() << "\n";
290 private:
291 bool C;
294 Expected<std::unique_ptr<MemoryBuffer>> getFile(const Twine &FileName) {
295 if (auto F = MemoryBuffer::getFile(FileName))
296 return std::move(*F);
297 else
298 return createFileError(FileName, F.getError());
301 void reportLLVMJITLinkError(Error Err) {
302 handleAllErrors(
303 std::move(Err),
304 ConditionalPrintErr<orc::FailedToMaterialize>(ShowErrFailedToMaterialize),
305 ConditionalPrintErr<ErrorInfoBase>(true));
308 } // end anonymous namespace
310 namespace llvm {
312 static raw_ostream &
313 operator<<(raw_ostream &OS, const Session::MemoryRegionInfo &MRI) {
314 return OS << "target addr = "
315 << format("0x%016" PRIx64, MRI.getTargetAddress())
316 << ", content: " << (const void *)MRI.getContent().data() << " -- "
317 << (const void *)(MRI.getContent().data() + MRI.getContent().size())
318 << " (" << MRI.getContent().size() << " bytes)";
321 static raw_ostream &
322 operator<<(raw_ostream &OS, const Session::SymbolInfoMap &SIM) {
323 OS << "Symbols:\n";
324 for (auto &SKV : SIM)
325 OS << " \"" << SKV.first() << "\" " << SKV.second << "\n";
326 return OS;
329 static raw_ostream &
330 operator<<(raw_ostream &OS, const Session::FileInfo &FI) {
331 for (auto &SIKV : FI.SectionInfos)
332 OS << " Section \"" << SIKV.first() << "\": " << SIKV.second << "\n";
333 for (auto &GOTKV : FI.GOTEntryInfos)
334 OS << " GOT \"" << GOTKV.first() << "\": " << GOTKV.second << "\n";
335 for (auto &StubKV : FI.StubInfos)
336 OS << " Stub \"" << StubKV.first() << "\": " << StubKV.second << "\n";
337 return OS;
340 static raw_ostream &
341 operator<<(raw_ostream &OS, const Session::FileInfoMap &FIM) {
342 for (auto &FIKV : FIM)
343 OS << "File \"" << FIKV.first() << "\":\n" << FIKV.second;
344 return OS;
347 static Error applyHarnessPromotions(Session &S, LinkGraph &G) {
349 // If this graph is part of the test harness there's nothing to do.
350 if (S.HarnessFiles.empty() || S.HarnessFiles.count(G.getName()))
351 return Error::success();
353 LLVM_DEBUG(dbgs() << "Applying promotions to graph " << G.getName() << "\n");
355 // If this graph is part of the test then promote any symbols referenced by
356 // the harness to default scope, remove all symbols that clash with harness
357 // definitions.
358 std::vector<Symbol *> DefinitionsToRemove;
359 for (auto *Sym : G.defined_symbols()) {
361 if (!Sym->hasName())
362 continue;
364 if (Sym->getLinkage() == Linkage::Weak) {
365 if (!S.CanonicalWeakDefs.count(Sym->getName()) ||
366 S.CanonicalWeakDefs[Sym->getName()] != G.getName()) {
367 LLVM_DEBUG({
368 dbgs() << " Externalizing weak symbol " << Sym->getName() << "\n";
370 DefinitionsToRemove.push_back(Sym);
371 } else {
372 LLVM_DEBUG({
373 dbgs() << " Making weak symbol " << Sym->getName() << " strong\n";
375 if (S.HarnessExternals.count(Sym->getName()))
376 Sym->setScope(Scope::Default);
377 else
378 Sym->setScope(Scope::Hidden);
379 Sym->setLinkage(Linkage::Strong);
381 } else if (S.HarnessExternals.count(Sym->getName())) {
382 LLVM_DEBUG(dbgs() << " Promoting " << Sym->getName() << "\n");
383 Sym->setScope(Scope::Default);
384 Sym->setLive(true);
385 continue;
386 } else if (S.HarnessDefinitions.count(Sym->getName())) {
387 LLVM_DEBUG(dbgs() << " Externalizing " << Sym->getName() << "\n");
388 DefinitionsToRemove.push_back(Sym);
392 for (auto *Sym : DefinitionsToRemove)
393 G.makeExternal(*Sym);
395 return Error::success();
398 static void dumpSectionContents(raw_ostream &OS, LinkGraph &G) {
399 constexpr orc::ExecutorAddrDiff DumpWidth = 16;
400 static_assert(isPowerOf2_64(DumpWidth), "DumpWidth must be a power of two");
402 // Put sections in address order.
403 std::vector<Section *> Sections;
404 for (auto &S : G.sections())
405 Sections.push_back(&S);
407 llvm::sort(Sections, [](const Section *LHS, const Section *RHS) {
408 if (LHS->symbols().empty() && RHS->symbols().empty())
409 return false;
410 if (LHS->symbols().empty())
411 return false;
412 if (RHS->symbols().empty())
413 return true;
414 SectionRange LHSRange(*LHS);
415 SectionRange RHSRange(*RHS);
416 return LHSRange.getStart() < RHSRange.getStart();
419 for (auto *S : Sections) {
420 OS << S->getName() << " content:";
421 if (S->symbols().empty()) {
422 OS << "\n section empty\n";
423 continue;
426 // Sort symbols into order, then render.
427 std::vector<Symbol *> Syms(S->symbols().begin(), S->symbols().end());
428 llvm::sort(Syms, [](const Symbol *LHS, const Symbol *RHS) {
429 return LHS->getAddress() < RHS->getAddress();
432 orc::ExecutorAddr NextAddr(Syms.front()->getAddress().getValue() &
433 ~(DumpWidth - 1));
434 for (auto *Sym : Syms) {
435 bool IsZeroFill = Sym->getBlock().isZeroFill();
436 auto SymStart = Sym->getAddress();
437 auto SymSize = Sym->getSize();
438 auto SymEnd = SymStart + SymSize;
439 const uint8_t *SymData = IsZeroFill ? nullptr
440 : reinterpret_cast<const uint8_t *>(
441 Sym->getSymbolContent().data());
443 // Pad any space before the symbol starts.
444 while (NextAddr != SymStart) {
445 if (NextAddr % DumpWidth == 0)
446 OS << formatv("\n{0:x16}:", NextAddr);
447 OS << " ";
448 ++NextAddr;
451 // Render the symbol content.
452 while (NextAddr != SymEnd) {
453 if (NextAddr % DumpWidth == 0)
454 OS << formatv("\n{0:x16}:", NextAddr);
455 if (IsZeroFill)
456 OS << " 00";
457 else
458 OS << formatv(" {0:x-2}", SymData[NextAddr - SymStart]);
459 ++NextAddr;
462 OS << "\n";
466 // A memory mapper with a fake offset applied only used for -noexec testing
467 class InProcessDeltaMapper final : public InProcessMemoryMapper {
468 public:
469 InProcessDeltaMapper(size_t PageSize, uint64_t TargetAddr)
470 : InProcessMemoryMapper(PageSize), TargetMapAddr(TargetAddr),
471 DeltaAddr(0) {}
473 static Expected<std::unique_ptr<InProcessDeltaMapper>> Create() {
474 size_t PageSize = SlabPageSize;
475 if (!PageSize) {
476 if (auto PageSizeOrErr = sys::Process::getPageSize())
477 PageSize = *PageSizeOrErr;
478 else
479 return PageSizeOrErr.takeError();
482 if (PageSize == 0)
483 return make_error<StringError>("Page size is zero",
484 inconvertibleErrorCode());
486 return std::make_unique<InProcessDeltaMapper>(PageSize, SlabAddress);
489 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override {
490 InProcessMemoryMapper::reserve(
491 NumBytes, [this, OnReserved = std::move(OnReserved)](
492 Expected<ExecutorAddrRange> Result) mutable {
493 if (!Result)
494 return OnReserved(Result.takeError());
496 assert(DeltaAddr == 0 && "Overwriting previous offset");
497 if (TargetMapAddr != ~0ULL)
498 DeltaAddr = TargetMapAddr - Result->Start.getValue();
499 auto OffsetRange = ExecutorAddrRange(Result->Start + DeltaAddr,
500 Result->End + DeltaAddr);
502 OnReserved(OffsetRange);
506 char *prepare(ExecutorAddr Addr, size_t ContentSize) override {
507 return InProcessMemoryMapper::prepare(Addr - DeltaAddr, ContentSize);
510 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override {
511 // Slide mapping based on delta, make all segments read-writable, and
512 // discard allocation actions.
513 auto FixedAI = std::move(AI);
514 FixedAI.MappingBase -= DeltaAddr;
515 for (auto &Seg : FixedAI.Segments)
516 Seg.AG = {MemProt::Read | MemProt::Write, Seg.AG.getMemLifetime()};
517 FixedAI.Actions.clear();
518 InProcessMemoryMapper::initialize(
519 FixedAI, [this, OnInitialized = std::move(OnInitialized)](
520 Expected<ExecutorAddr> Result) mutable {
521 if (!Result)
522 return OnInitialized(Result.takeError());
524 OnInitialized(ExecutorAddr(Result->getValue() + DeltaAddr));
528 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
529 OnDeinitializedFunction OnDeInitialized) override {
530 std::vector<ExecutorAddr> Addrs(Allocations.size());
531 for (const auto Base : Allocations) {
532 Addrs.push_back(Base - DeltaAddr);
535 InProcessMemoryMapper::deinitialize(Addrs, std::move(OnDeInitialized));
538 void release(ArrayRef<ExecutorAddr> Reservations,
539 OnReleasedFunction OnRelease) override {
540 std::vector<ExecutorAddr> Addrs(Reservations.size());
541 for (const auto Base : Reservations) {
542 Addrs.push_back(Base - DeltaAddr);
544 InProcessMemoryMapper::release(Addrs, std::move(OnRelease));
547 private:
548 uint64_t TargetMapAddr;
549 uint64_t DeltaAddr;
552 Expected<uint64_t> getSlabAllocSize(StringRef SizeString) {
553 SizeString = SizeString.trim();
555 uint64_t Units = 1024;
557 if (SizeString.ends_with_insensitive("kb"))
558 SizeString = SizeString.drop_back(2).rtrim();
559 else if (SizeString.ends_with_insensitive("mb")) {
560 Units = 1024 * 1024;
561 SizeString = SizeString.drop_back(2).rtrim();
562 } else if (SizeString.ends_with_insensitive("gb")) {
563 Units = 1024 * 1024 * 1024;
564 SizeString = SizeString.drop_back(2).rtrim();
567 uint64_t SlabSize = 0;
568 if (SizeString.getAsInteger(10, SlabSize))
569 return make_error<StringError>("Invalid numeric format for slab size",
570 inconvertibleErrorCode());
572 return SlabSize * Units;
575 static std::unique_ptr<JITLinkMemoryManager> createInProcessMemoryManager() {
576 uint64_t SlabSize;
577 #ifdef _WIN32
578 SlabSize = 1024 * 1024;
579 #else
580 SlabSize = 1024 * 1024 * 1024;
581 #endif
583 if (!SlabAllocateSizeString.empty())
584 SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString));
586 // If this is a -no-exec case and we're tweaking the slab address or size then
587 // use the delta mapper.
588 if (NoExec && (SlabAddress || SlabPageSize))
589 return ExitOnErr(
590 MapperJITLinkMemoryManager::CreateWithMapper<InProcessDeltaMapper>(
591 SlabSize));
593 // Otherwise use the standard in-process mapper.
594 return ExitOnErr(
595 MapperJITLinkMemoryManager::CreateWithMapper<InProcessMemoryMapper>(
596 SlabSize));
599 Expected<std::unique_ptr<jitlink::JITLinkMemoryManager>>
600 createSharedMemoryManager(SimpleRemoteEPC &SREPC) {
601 SharedMemoryMapper::SymbolAddrs SAs;
602 if (auto Err = SREPC.getBootstrapSymbols(
603 {{SAs.Instance, rt::ExecutorSharedMemoryMapperServiceInstanceName},
604 {SAs.Reserve,
605 rt::ExecutorSharedMemoryMapperServiceReserveWrapperName},
606 {SAs.Initialize,
607 rt::ExecutorSharedMemoryMapperServiceInitializeWrapperName},
608 {SAs.Deinitialize,
609 rt::ExecutorSharedMemoryMapperServiceDeinitializeWrapperName},
610 {SAs.Release,
611 rt::ExecutorSharedMemoryMapperServiceReleaseWrapperName}}))
612 return std::move(Err);
614 #ifdef _WIN32
615 size_t SlabSize = 1024 * 1024;
616 #else
617 size_t SlabSize = 1024 * 1024 * 1024;
618 #endif
620 if (!SlabAllocateSizeString.empty())
621 SlabSize = ExitOnErr(getSlabAllocSize(SlabAllocateSizeString));
623 return MapperJITLinkMemoryManager::CreateWithMapper<SharedMemoryMapper>(
624 SlabSize, SREPC, SAs);
628 static Expected<MaterializationUnit::Interface>
629 getTestObjectFileInterface(Session &S, MemoryBufferRef O) {
631 // Get the standard interface for this object, but ignore the symbols field.
632 // We'll handle that manually to include promotion.
633 auto I = getObjectFileInterface(S.ES, O);
634 if (!I)
635 return I.takeError();
636 I->SymbolFlags.clear();
638 // If creating an object file was going to fail it would have happened above,
639 // so we can 'cantFail' this.
640 auto Obj = cantFail(object::ObjectFile::createObjectFile(O));
642 // The init symbol must be included in the SymbolFlags map if present.
643 if (I->InitSymbol)
644 I->SymbolFlags[I->InitSymbol] =
645 JITSymbolFlags::MaterializationSideEffectsOnly;
647 for (auto &Sym : Obj->symbols()) {
648 Expected<uint32_t> SymFlagsOrErr = Sym.getFlags();
649 if (!SymFlagsOrErr)
650 // TODO: Test this error.
651 return SymFlagsOrErr.takeError();
653 // Skip symbols not defined in this object file.
654 if ((*SymFlagsOrErr & object::BasicSymbolRef::SF_Undefined))
655 continue;
657 auto Name = Sym.getName();
658 if (!Name)
659 return Name.takeError();
661 // Skip symbols that have type SF_File.
662 if (auto SymType = Sym.getType()) {
663 if (*SymType == object::SymbolRef::ST_File)
664 continue;
665 } else
666 return SymType.takeError();
668 auto SymFlags = JITSymbolFlags::fromObjectSymbol(Sym);
669 if (!SymFlags)
670 return SymFlags.takeError();
672 if (SymFlags->isWeak()) {
673 // If this is a weak symbol that's not defined in the harness then we
674 // need to either mark it as strong (if this is the first definition
675 // that we've seen) or discard it.
676 if (S.HarnessDefinitions.count(*Name) || S.CanonicalWeakDefs.count(*Name))
677 continue;
678 S.CanonicalWeakDefs[*Name] = O.getBufferIdentifier();
679 *SymFlags &= ~JITSymbolFlags::Weak;
680 if (!S.HarnessExternals.count(*Name))
681 *SymFlags &= ~JITSymbolFlags::Exported;
682 } else if (S.HarnessExternals.count(*Name)) {
683 *SymFlags |= JITSymbolFlags::Exported;
684 } else if (S.HarnessDefinitions.count(*Name) ||
685 !(*SymFlagsOrErr & object::BasicSymbolRef::SF_Global))
686 continue;
688 auto InternedName = S.ES.intern(*Name);
689 I->SymbolFlags[InternedName] = std::move(*SymFlags);
692 return I;
695 static Error loadProcessSymbols(Session &S) {
696 auto FilterMainEntryPoint =
697 [EPName = S.ES.intern(EntryPointName)](SymbolStringPtr Name) {
698 return Name != EPName;
700 S.MainJD->addGenerator(
701 ExitOnErr(orc::EPCDynamicLibrarySearchGenerator::GetForTargetProcess(
702 S.ES, std::move(FilterMainEntryPoint))));
704 return Error::success();
707 static Error loadDylibs(Session &S) {
708 LLVM_DEBUG(dbgs() << "Loading dylibs...\n");
709 for (const auto &Dylib : Dylibs) {
710 LLVM_DEBUG(dbgs() << " " << Dylib << "\n");
711 if (auto Err = S.loadAndLinkDynamicLibrary(*S.MainJD, Dylib))
712 return Err;
715 return Error::success();
718 static Expected<std::unique_ptr<ExecutorProcessControl>> launchExecutor() {
719 #ifndef LLVM_ON_UNIX
720 // FIXME: Add support for Windows.
721 return make_error<StringError>("-" + OutOfProcessExecutor.ArgStr +
722 " not supported on non-unix platforms",
723 inconvertibleErrorCode());
724 #elif !LLVM_ENABLE_THREADS
725 // Out of process mode using SimpleRemoteEPC depends on threads.
726 return make_error<StringError>(
727 "-" + OutOfProcessExecutor.ArgStr +
728 " requires threads, but LLVM was built with "
729 "LLVM_ENABLE_THREADS=Off",
730 inconvertibleErrorCode());
731 #else
733 constexpr int ReadEnd = 0;
734 constexpr int WriteEnd = 1;
736 // Pipe FDs.
737 int ToExecutor[2];
738 int FromExecutor[2];
740 pid_t ChildPID;
742 // Create pipes to/from the executor..
743 if (pipe(ToExecutor) != 0 || pipe(FromExecutor) != 0)
744 return make_error<StringError>("Unable to create pipe for executor",
745 inconvertibleErrorCode());
747 ChildPID = fork();
749 if (ChildPID == 0) {
750 // In the child...
752 // Close the parent ends of the pipes
753 close(ToExecutor[WriteEnd]);
754 close(FromExecutor[ReadEnd]);
756 // Execute the child process.
757 std::unique_ptr<char[]> ExecutorPath, FDSpecifier;
759 ExecutorPath = std::make_unique<char[]>(OutOfProcessExecutor.size() + 1);
760 strcpy(ExecutorPath.get(), OutOfProcessExecutor.data());
762 std::string FDSpecifierStr("filedescs=");
763 FDSpecifierStr += utostr(ToExecutor[ReadEnd]);
764 FDSpecifierStr += ',';
765 FDSpecifierStr += utostr(FromExecutor[WriteEnd]);
766 FDSpecifier = std::make_unique<char[]>(FDSpecifierStr.size() + 1);
767 strcpy(FDSpecifier.get(), FDSpecifierStr.c_str());
770 char *const Args[] = {ExecutorPath.get(), FDSpecifier.get(), nullptr};
771 int RC = execvp(ExecutorPath.get(), Args);
772 if (RC != 0) {
773 errs() << "unable to launch out-of-process executor \""
774 << ExecutorPath.get() << "\"\n";
775 exit(1);
778 // else we're the parent...
780 // Close the child ends of the pipes
781 close(ToExecutor[ReadEnd]);
782 close(FromExecutor[WriteEnd]);
784 auto S = SimpleRemoteEPC::Setup();
785 if (UseSharedMemory)
786 S.CreateMemoryManager = createSharedMemoryManager;
788 return SimpleRemoteEPC::Create<FDSimpleRemoteEPCTransport>(
789 std::make_unique<DynamicThreadPoolTaskDispatcher>(), std::move(S),
790 FromExecutor[ReadEnd], ToExecutor[WriteEnd]);
791 #endif
794 #if LLVM_ON_UNIX && LLVM_ENABLE_THREADS
795 static Error createTCPSocketError(Twine Details) {
796 return make_error<StringError>(
797 formatv("Failed to connect TCP socket '{0}': {1}",
798 OutOfProcessExecutorConnect, Details),
799 inconvertibleErrorCode());
802 static Expected<int> connectTCPSocket(std::string Host, std::string PortStr) {
803 addrinfo *AI;
804 addrinfo Hints{};
805 Hints.ai_family = AF_INET;
806 Hints.ai_socktype = SOCK_STREAM;
807 Hints.ai_flags = AI_NUMERICSERV;
809 if (int EC = getaddrinfo(Host.c_str(), PortStr.c_str(), &Hints, &AI))
810 return createTCPSocketError("Address resolution failed (" +
811 StringRef(gai_strerror(EC)) + ")");
813 // Cycle through the returned addrinfo structures and connect to the first
814 // reachable endpoint.
815 int SockFD;
816 addrinfo *Server;
817 for (Server = AI; Server != nullptr; Server = Server->ai_next) {
818 // socket might fail, e.g. if the address family is not supported. Skip to
819 // the next addrinfo structure in such a case.
820 if ((SockFD = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol)) < 0)
821 continue;
823 // If connect returns null, we exit the loop with a working socket.
824 if (connect(SockFD, Server->ai_addr, Server->ai_addrlen) == 0)
825 break;
827 close(SockFD);
829 freeaddrinfo(AI);
831 // If we reached the end of the loop without connecting to a valid endpoint,
832 // dump the last error that was logged in socket() or connect().
833 if (Server == nullptr)
834 return createTCPSocketError(std::strerror(errno));
836 return SockFD;
838 #endif
840 static Expected<std::unique_ptr<ExecutorProcessControl>> connectToExecutor() {
841 #ifndef LLVM_ON_UNIX
842 // FIXME: Add TCP support for Windows.
843 return make_error<StringError>("-" + OutOfProcessExecutorConnect.ArgStr +
844 " not supported on non-unix platforms",
845 inconvertibleErrorCode());
846 #elif !LLVM_ENABLE_THREADS
847 // Out of process mode using SimpleRemoteEPC depends on threads.
848 return make_error<StringError>(
849 "-" + OutOfProcessExecutorConnect.ArgStr +
850 " requires threads, but LLVM was built with "
851 "LLVM_ENABLE_THREADS=Off",
852 inconvertibleErrorCode());
853 #else
855 StringRef Host, PortStr;
856 std::tie(Host, PortStr) = StringRef(OutOfProcessExecutorConnect).split(':');
857 if (Host.empty())
858 return createTCPSocketError("Host name for -" +
859 OutOfProcessExecutorConnect.ArgStr +
860 " can not be empty");
861 if (PortStr.empty())
862 return createTCPSocketError("Port number in -" +
863 OutOfProcessExecutorConnect.ArgStr +
864 " can not be empty");
865 int Port = 0;
866 if (PortStr.getAsInteger(10, Port))
867 return createTCPSocketError("Port number '" + PortStr +
868 "' is not a valid integer");
870 Expected<int> SockFD = connectTCPSocket(Host.str(), PortStr.str());
871 if (!SockFD)
872 return SockFD.takeError();
874 auto S = SimpleRemoteEPC::Setup();
875 if (UseSharedMemory)
876 S.CreateMemoryManager = createSharedMemoryManager;
878 return SimpleRemoteEPC::Create<FDSimpleRemoteEPCTransport>(
879 std::make_unique<DynamicThreadPoolTaskDispatcher>(),
880 std::move(S), *SockFD, *SockFD);
881 #endif
884 class PhonyExternalsGenerator : public DefinitionGenerator {
885 public:
886 Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,
887 JITDylibLookupFlags JDLookupFlags,
888 const SymbolLookupSet &LookupSet) override {
889 SymbolMap PhonySymbols;
890 for (auto &KV : LookupSet)
891 PhonySymbols[KV.first] = {ExecutorAddr(), JITSymbolFlags::Exported};
892 return JD.define(absoluteSymbols(std::move(PhonySymbols)));
896 Expected<std::unique_ptr<Session>> Session::Create(Triple TT,
897 SubtargetFeatures Features) {
899 std::unique_ptr<ExecutorProcessControl> EPC;
900 if (OutOfProcessExecutor.getNumOccurrences()) {
901 /// If -oop-executor is passed then launch the executor.
902 if (auto REPC = launchExecutor())
903 EPC = std::move(*REPC);
904 else
905 return REPC.takeError();
906 } else if (OutOfProcessExecutorConnect.getNumOccurrences()) {
907 /// If -oop-executor-connect is passed then connect to the executor.
908 if (auto REPC = connectToExecutor())
909 EPC = std::move(*REPC);
910 else
911 return REPC.takeError();
912 } else {
913 /// Otherwise use SelfExecutorProcessControl to target the current process.
914 auto PageSize = sys::Process::getPageSize();
915 if (!PageSize)
916 return PageSize.takeError();
917 EPC = std::make_unique<SelfExecutorProcessControl>(
918 std::make_shared<SymbolStringPool>(),
919 std::make_unique<InPlaceTaskDispatcher>(), std::move(TT), *PageSize,
920 createInProcessMemoryManager());
923 Error Err = Error::success();
924 std::unique_ptr<Session> S(new Session(std::move(EPC), Err));
925 if (Err)
926 return std::move(Err);
927 S->Features = std::move(Features);
928 return std::move(S);
931 Session::~Session() {
932 if (auto Err = ES.endSession())
933 ES.reportError(std::move(Err));
936 Session::Session(std::unique_ptr<ExecutorProcessControl> EPC, Error &Err)
937 : ES(std::move(EPC)),
938 ObjLayer(ES, ES.getExecutorProcessControl().getMemMgr()) {
940 /// Local ObjectLinkingLayer::Plugin class to forward modifyPassConfig to the
941 /// Session.
942 class JITLinkSessionPlugin : public ObjectLinkingLayer::Plugin {
943 public:
944 JITLinkSessionPlugin(Session &S) : S(S) {}
945 void modifyPassConfig(MaterializationResponsibility &MR, LinkGraph &G,
946 PassConfiguration &PassConfig) override {
947 S.modifyPassConfig(G.getTargetTriple(), PassConfig);
950 Error notifyFailed(MaterializationResponsibility &MR) override {
951 return Error::success();
953 Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override {
954 return Error::success();
956 void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey,
957 ResourceKey SrcKey) override {}
959 private:
960 Session &S;
963 ErrorAsOutParameter _(&Err);
965 ES.setErrorReporter(reportLLVMJITLinkError);
967 if (auto MainJDOrErr = ES.createJITDylib("main"))
968 MainJD = &*MainJDOrErr;
969 else {
970 Err = MainJDOrErr.takeError();
971 return;
974 if (!NoProcessSymbols)
975 ExitOnErr(loadProcessSymbols(*this));
976 else {
977 // This symbol is used in testcases.
978 auto &TestResultJD = ES.createBareJITDylib("<TestResultJD>");
979 ExitOnErr(TestResultJD.define(absoluteSymbols(
980 {{ES.intern("llvm_jitlink_setTestResultOverride"),
981 {ExecutorAddr::fromPtr(llvm_jitlink_setTestResultOverride),
982 JITSymbolFlags::Exported}}})));
983 MainJD->addToLinkOrder(TestResultJD);
986 ExitOnErr(loadDylibs(*this));
988 auto &TT = ES.getTargetTriple();
990 if (DebuggerSupport && TT.isOSBinFormatMachO())
991 ObjLayer.addPlugin(ExitOnErr(
992 GDBJITDebugInfoRegistrationPlugin::Create(this->ES, *MainJD, TT)));
994 if (PerfSupport && TT.isOSBinFormatELF()) {
995 ObjLayer.addPlugin(ExitOnErr(DebugInfoPreservationPlugin::Create()));
996 ObjLayer.addPlugin(ExitOnErr(PerfSupportPlugin::Create(
997 this->ES.getExecutorProcessControl(), *MainJD, true, true)));
1000 // Set up the platform.
1001 if (TT.isOSBinFormatMachO() && !OrcRuntime.empty()) {
1002 if (auto P =
1003 MachOPlatform::Create(ES, ObjLayer, *MainJD, OrcRuntime.c_str()))
1004 ES.setPlatform(std::move(*P));
1005 else {
1006 Err = P.takeError();
1007 return;
1009 } else if (TT.isOSBinFormatELF() && !OrcRuntime.empty()) {
1010 if (auto P =
1011 ELFNixPlatform::Create(ES, ObjLayer, *MainJD, OrcRuntime.c_str()))
1012 ES.setPlatform(std::move(*P));
1013 else {
1014 Err = P.takeError();
1015 return;
1017 } else if (TT.isOSBinFormatCOFF() && !OrcRuntime.empty()) {
1018 auto LoadDynLibrary = [&, this](JITDylib &JD, StringRef DLLName) -> Error {
1019 if (!DLLName.ends_with_insensitive(".dll"))
1020 return make_error<StringError>("DLLName not ending with .dll",
1021 inconvertibleErrorCode());
1022 return loadAndLinkDynamicLibrary(JD, DLLName);
1025 if (auto P = COFFPlatform::Create(ES, ObjLayer, *MainJD, OrcRuntime.c_str(),
1026 std::move(LoadDynLibrary)))
1027 ES.setPlatform(std::move(*P));
1028 else {
1029 Err = P.takeError();
1030 return;
1032 } else if (TT.isOSBinFormatELF()) {
1033 if (!NoExec)
1034 ObjLayer.addPlugin(std::make_unique<EHFrameRegistrationPlugin>(
1035 ES, ExitOnErr(EPCEHFrameRegistrar::Create(this->ES))));
1036 if (DebuggerSupport)
1037 ObjLayer.addPlugin(std::make_unique<DebugObjectManagerPlugin>(
1038 ES, ExitOnErr(createJITLoaderGDBRegistrar(this->ES)), true, true));
1041 ObjLayer.addPlugin(std::make_unique<JITLinkSessionPlugin>(*this));
1043 // Process any harness files.
1044 for (auto &HarnessFile : TestHarnesses) {
1045 HarnessFiles.insert(HarnessFile);
1047 auto ObjBuffer = ExitOnErr(getFile(HarnessFile));
1049 auto ObjInterface =
1050 ExitOnErr(getObjectFileInterface(ES, ObjBuffer->getMemBufferRef()));
1052 for (auto &KV : ObjInterface.SymbolFlags)
1053 HarnessDefinitions.insert(*KV.first);
1055 auto Obj = ExitOnErr(
1056 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
1058 for (auto &Sym : Obj->symbols()) {
1059 uint32_t SymFlags = ExitOnErr(Sym.getFlags());
1060 auto Name = ExitOnErr(Sym.getName());
1062 if (Name.empty())
1063 continue;
1065 if (SymFlags & object::BasicSymbolRef::SF_Undefined)
1066 HarnessExternals.insert(Name);
1070 // If a name is defined by some harness file then it's a definition, not an
1071 // external.
1072 for (auto &DefName : HarnessDefinitions)
1073 HarnessExternals.erase(DefName.getKey());
1075 if (!ShowLinkGraphs.empty())
1076 ShowGraphsRegex = Regex(ShowLinkGraphs);
1079 void Session::dumpSessionInfo(raw_ostream &OS) {
1080 OS << "Registered addresses:\n" << SymbolInfos << FileInfos;
1083 void Session::modifyPassConfig(const Triple &TT,
1084 PassConfiguration &PassConfig) {
1085 if (!CheckFiles.empty())
1086 PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) {
1087 if (ES.getTargetTriple().getObjectFormat() == Triple::ELF)
1088 return registerELFGraphInfo(*this, G);
1090 if (ES.getTargetTriple().getObjectFormat() == Triple::MachO)
1091 return registerMachOGraphInfo(*this, G);
1093 if (ES.getTargetTriple().getObjectFormat() == Triple::COFF)
1094 return registerCOFFGraphInfo(*this, G);
1096 return make_error<StringError>("Unsupported object format for GOT/stub "
1097 "registration",
1098 inconvertibleErrorCode());
1101 if (ShowGraphsRegex)
1102 PassConfig.PostFixupPasses.push_back([this](LinkGraph &G) -> Error {
1103 // Print graph if ShowLinkGraphs is specified-but-empty, or if
1104 // it contains the given graph.
1105 if (ShowGraphsRegex->match(G.getName())) {
1106 outs() << "Link graph \"" << G.getName() << "\" post-fixup:\n";
1107 G.dump(outs());
1109 return Error::success();
1112 PassConfig.PrePrunePasses.push_back(
1113 [this](LinkGraph &G) { return applyHarnessPromotions(*this, G); });
1115 if (ShowRelocatedSectionContents)
1116 PassConfig.PostFixupPasses.push_back([](LinkGraph &G) -> Error {
1117 outs() << "Relocated section contents for " << G.getName() << ":\n";
1118 dumpSectionContents(outs(), G);
1119 return Error::success();
1122 if (AddSelfRelocations)
1123 PassConfig.PostPrunePasses.push_back(addSelfRelocations);
1126 Expected<JITDylib *> Session::getOrLoadDynamicLibrary(StringRef LibPath) {
1127 auto It = DynLibJDs.find(LibPath.str());
1128 if (It != DynLibJDs.end()) {
1129 return It->second;
1131 auto G = EPCDynamicLibrarySearchGenerator::Load(ES, LibPath.data());
1132 if (!G)
1133 return G.takeError();
1134 auto JD = &ES.createBareJITDylib(LibPath.str());
1136 JD->addGenerator(std::move(*G));
1137 DynLibJDs.emplace(LibPath.str(), JD);
1138 LLVM_DEBUG({
1139 dbgs() << "Loaded dynamic library " << LibPath.data() << " for " << LibPath
1140 << "\n";
1142 return JD;
1145 Error Session::loadAndLinkDynamicLibrary(JITDylib &JD, StringRef LibPath) {
1146 auto DL = getOrLoadDynamicLibrary(LibPath);
1147 if (!DL)
1148 return DL.takeError();
1149 JD.addToLinkOrder(**DL);
1150 LLVM_DEBUG({
1151 dbgs() << "Linking dynamic library " << LibPath << " to " << JD.getName()
1152 << "\n";
1154 return Error::success();
1157 Expected<Session::FileInfo &> Session::findFileInfo(StringRef FileName) {
1158 auto FileInfoItr = FileInfos.find(FileName);
1159 if (FileInfoItr == FileInfos.end())
1160 return make_error<StringError>("file \"" + FileName + "\" not recognized",
1161 inconvertibleErrorCode());
1162 return FileInfoItr->second;
1165 Expected<Session::MemoryRegionInfo &>
1166 Session::findSectionInfo(StringRef FileName, StringRef SectionName) {
1167 auto FI = findFileInfo(FileName);
1168 if (!FI)
1169 return FI.takeError();
1170 auto SecInfoItr = FI->SectionInfos.find(SectionName);
1171 if (SecInfoItr == FI->SectionInfos.end())
1172 return make_error<StringError>("no section \"" + SectionName +
1173 "\" registered for file \"" + FileName +
1174 "\"",
1175 inconvertibleErrorCode());
1176 return SecInfoItr->second;
1179 Expected<Session::MemoryRegionInfo &>
1180 Session::findStubInfo(StringRef FileName, StringRef TargetName) {
1181 auto FI = findFileInfo(FileName);
1182 if (!FI)
1183 return FI.takeError();
1184 auto StubInfoItr = FI->StubInfos.find(TargetName);
1185 if (StubInfoItr == FI->StubInfos.end())
1186 return make_error<StringError>("no stub for \"" + TargetName +
1187 "\" registered for file \"" + FileName +
1188 "\"",
1189 inconvertibleErrorCode());
1190 return StubInfoItr->second;
1193 Expected<Session::MemoryRegionInfo &>
1194 Session::findGOTEntryInfo(StringRef FileName, StringRef TargetName) {
1195 auto FI = findFileInfo(FileName);
1196 if (!FI)
1197 return FI.takeError();
1198 auto GOTInfoItr = FI->GOTEntryInfos.find(TargetName);
1199 if (GOTInfoItr == FI->GOTEntryInfos.end())
1200 return make_error<StringError>("no GOT entry for \"" + TargetName +
1201 "\" registered for file \"" + FileName +
1202 "\"",
1203 inconvertibleErrorCode());
1204 return GOTInfoItr->second;
1207 bool Session::isSymbolRegistered(StringRef SymbolName) {
1208 return SymbolInfos.count(SymbolName);
1211 Expected<Session::MemoryRegionInfo &>
1212 Session::findSymbolInfo(StringRef SymbolName, Twine ErrorMsgStem) {
1213 auto SymInfoItr = SymbolInfos.find(SymbolName);
1214 if (SymInfoItr == SymbolInfos.end())
1215 return make_error<StringError>(ErrorMsgStem + ": symbol " + SymbolName +
1216 " not found",
1217 inconvertibleErrorCode());
1218 return SymInfoItr->second;
1221 } // end namespace llvm
1223 static std::pair<Triple, SubtargetFeatures> getFirstFileTripleAndFeatures() {
1224 static std::pair<Triple, SubtargetFeatures> FirstTTAndFeatures = []() {
1225 assert(!InputFiles.empty() && "InputFiles can not be empty");
1226 for (auto InputFile : InputFiles) {
1227 auto ObjBuffer = ExitOnErr(getFile(InputFile));
1228 file_magic Magic = identify_magic(ObjBuffer->getBuffer());
1229 switch (Magic) {
1230 case file_magic::coff_object:
1231 case file_magic::elf_relocatable:
1232 case file_magic::macho_object: {
1233 auto Obj = ExitOnErr(
1234 object::ObjectFile::createObjectFile(ObjBuffer->getMemBufferRef()));
1235 Triple TT = Obj->makeTriple();
1236 if (Magic == file_magic::coff_object) {
1237 // TODO: Move this to makeTriple() if possible.
1238 TT.setObjectFormat(Triple::COFF);
1239 TT.setOS(Triple::OSType::Win32);
1241 SubtargetFeatures Features;
1242 if (auto ObjFeatures = Obj->getFeatures())
1243 Features = std::move(*ObjFeatures);
1244 return std::make_pair(TT, Features);
1246 default:
1247 break;
1250 return std::make_pair(Triple(), SubtargetFeatures());
1251 }();
1253 return FirstTTAndFeatures;
1256 static Error sanitizeArguments(const Triple &TT, const char *ArgV0) {
1258 // -noexec and --args should not be used together.
1259 if (NoExec && !InputArgv.empty())
1260 errs() << "Warning: --args passed to -noexec run will be ignored.\n";
1262 // Set the entry point name if not specified.
1263 if (EntryPointName.empty())
1264 EntryPointName = TT.getObjectFormat() == Triple::MachO ? "_main" : "main";
1266 // Disable debugger support by default in noexec tests.
1267 if (DebuggerSupport.getNumOccurrences() == 0 && NoExec)
1268 DebuggerSupport = false;
1270 // If -slab-allocate is passed, check that we're not trying to use it in
1271 // -oop-executor or -oop-executor-connect mode.
1273 // FIXME: Remove once we enable remote slab allocation.
1274 if (SlabAllocateSizeString != "") {
1275 if (OutOfProcessExecutor.getNumOccurrences() ||
1276 OutOfProcessExecutorConnect.getNumOccurrences())
1277 return make_error<StringError>(
1278 "-slab-allocate cannot be used with -oop-executor or "
1279 "-oop-executor-connect",
1280 inconvertibleErrorCode());
1283 // If -slab-address is passed, require -slab-allocate and -noexec
1284 if (SlabAddress != ~0ULL) {
1285 if (SlabAllocateSizeString == "" || !NoExec)
1286 return make_error<StringError>(
1287 "-slab-address requires -slab-allocate and -noexec",
1288 inconvertibleErrorCode());
1290 if (SlabPageSize == 0)
1291 errs() << "Warning: -slab-address used without -slab-page-size.\n";
1294 if (SlabPageSize != 0) {
1295 // -slab-page-size requires slab alloc.
1296 if (SlabAllocateSizeString == "")
1297 return make_error<StringError>("-slab-page-size requires -slab-allocate",
1298 inconvertibleErrorCode());
1300 // Check -slab-page-size / -noexec interactions.
1301 if (!NoExec) {
1302 if (auto RealPageSize = sys::Process::getPageSize()) {
1303 if (SlabPageSize % *RealPageSize)
1304 return make_error<StringError>(
1305 "-slab-page-size must be a multiple of real page size for exec "
1306 "tests (did you mean to use -noexec ?)\n",
1307 inconvertibleErrorCode());
1308 } else {
1309 errs() << "Could not retrieve process page size:\n";
1310 logAllUnhandledErrors(RealPageSize.takeError(), errs(), "");
1311 errs() << "Executing with slab page size = "
1312 << formatv("{0:x}", SlabPageSize) << ".\n"
1313 << "Tool may crash if " << formatv("{0:x}", SlabPageSize)
1314 << " is not a multiple of the real process page size.\n"
1315 << "(did you mean to use -noexec ?)";
1320 // Only one of -oop-executor and -oop-executor-connect can be used.
1321 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1322 !!OutOfProcessExecutorConnect.getNumOccurrences())
1323 return make_error<StringError>(
1324 "Only one of -" + OutOfProcessExecutor.ArgStr + " and -" +
1325 OutOfProcessExecutorConnect.ArgStr + " can be specified",
1326 inconvertibleErrorCode());
1328 // If -oop-executor was used but no value was specified then use a sensible
1329 // default.
1330 if (!!OutOfProcessExecutor.getNumOccurrences() &&
1331 OutOfProcessExecutor.empty()) {
1332 SmallString<256> OOPExecutorPath(sys::fs::getMainExecutable(
1333 ArgV0, reinterpret_cast<void *>(&sanitizeArguments)));
1334 sys::path::remove_filename(OOPExecutorPath);
1335 sys::path::append(OOPExecutorPath, "llvm-jitlink-executor");
1336 OutOfProcessExecutor = OOPExecutorPath.str().str();
1339 return Error::success();
1342 static void addPhonyExternalsGenerator(Session &S) {
1343 S.MainJD->addGenerator(std::make_unique<PhonyExternalsGenerator>());
1346 static Error createJITDylibs(Session &S,
1347 std::map<unsigned, JITDylib *> &IdxToJD) {
1348 // First, set up JITDylibs.
1349 LLVM_DEBUG(dbgs() << "Creating JITDylibs...\n");
1351 // Create a "main" JITLinkDylib.
1352 IdxToJD[0] = S.MainJD;
1353 S.JDSearchOrder.push_back({S.MainJD, JITDylibLookupFlags::MatchAllSymbols});
1354 LLVM_DEBUG(dbgs() << " 0: " << S.MainJD->getName() << "\n");
1356 // Add any extra JITDylibs from the command line.
1357 for (auto JDItr = JITDylibs.begin(), JDEnd = JITDylibs.end();
1358 JDItr != JDEnd; ++JDItr) {
1359 auto JD = S.ES.createJITDylib(*JDItr);
1360 if (!JD)
1361 return JD.takeError();
1362 unsigned JDIdx = JITDylibs.getPosition(JDItr - JITDylibs.begin());
1363 IdxToJD[JDIdx] = &*JD;
1364 S.JDSearchOrder.push_back({&*JD, JITDylibLookupFlags::MatchAllSymbols});
1365 LLVM_DEBUG(dbgs() << " " << JDIdx << ": " << JD->getName() << "\n");
1369 LLVM_DEBUG({
1370 dbgs() << "Dylib search order is [ ";
1371 for (auto &KV : S.JDSearchOrder)
1372 dbgs() << KV.first->getName() << " ";
1373 dbgs() << "]\n";
1376 return Error::success();
1379 static Error addAbsoluteSymbols(Session &S,
1380 const std::map<unsigned, JITDylib *> &IdxToJD) {
1381 // Define absolute symbols.
1382 LLVM_DEBUG(dbgs() << "Defining absolute symbols...\n");
1383 for (auto AbsDefItr = AbsoluteDefs.begin(), AbsDefEnd = AbsoluteDefs.end();
1384 AbsDefItr != AbsDefEnd; ++AbsDefItr) {
1385 unsigned AbsDefArgIdx =
1386 AbsoluteDefs.getPosition(AbsDefItr - AbsoluteDefs.begin());
1387 auto &JD = *std::prev(IdxToJD.lower_bound(AbsDefArgIdx))->second;
1389 StringRef AbsDefStmt = *AbsDefItr;
1390 size_t EqIdx = AbsDefStmt.find_first_of('=');
1391 if (EqIdx == StringRef::npos)
1392 return make_error<StringError>("Invalid absolute define \"" + AbsDefStmt +
1393 "\". Syntax: <name>=<addr>",
1394 inconvertibleErrorCode());
1395 StringRef Name = AbsDefStmt.substr(0, EqIdx).trim();
1396 StringRef AddrStr = AbsDefStmt.substr(EqIdx + 1).trim();
1398 uint64_t Addr;
1399 if (AddrStr.getAsInteger(0, Addr))
1400 return make_error<StringError>("Invalid address expression \"" + AddrStr +
1401 "\" in absolute symbol definition \"" +
1402 AbsDefStmt + "\"",
1403 inconvertibleErrorCode());
1404 ExecutorSymbolDef AbsDef(ExecutorAddr(Addr), JITSymbolFlags::Exported);
1405 if (auto Err = JD.define(absoluteSymbols({{S.ES.intern(Name), AbsDef}})))
1406 return Err;
1408 // Register the absolute symbol with the session symbol infos.
1409 S.SymbolInfos[Name] = {ArrayRef<char>(), Addr,
1410 AbsDef.getFlags().getTargetFlags()};
1413 return Error::success();
1416 static Error addAliases(Session &S,
1417 const std::map<unsigned, JITDylib *> &IdxToJD) {
1418 // Define absolute symbols.
1419 LLVM_DEBUG(dbgs() << "Defining aliases...\n");
1420 for (auto AliasItr = Aliases.begin(), AliasEnd = Aliases.end();
1421 AliasItr != AliasEnd; ++AliasItr) {
1422 unsigned AliasArgIdx = Aliases.getPosition(AliasItr - Aliases.begin());
1423 auto &JD = *std::prev(IdxToJD.lower_bound(AliasArgIdx))->second;
1425 StringRef AliasStmt = *AliasItr;
1426 size_t EqIdx = AliasStmt.find_first_of('=');
1427 if (EqIdx == StringRef::npos)
1428 return make_error<StringError>("Invalid alias definition \"" + AliasStmt +
1429 "\". Syntax: <name>=<addr>",
1430 inconvertibleErrorCode());
1431 StringRef Alias = AliasStmt.substr(0, EqIdx).trim();
1432 StringRef Aliasee = AliasStmt.substr(EqIdx + 1).trim();
1434 SymbolAliasMap SAM;
1435 SAM[S.ES.intern(Alias)] = {S.ES.intern(Aliasee), JITSymbolFlags::Exported};
1436 if (auto Err = JD.define(symbolAliases(std::move(SAM))))
1437 return Err;
1440 return Error::success();
1443 static Error addTestHarnesses(Session &S) {
1444 LLVM_DEBUG(dbgs() << "Adding test harness objects...\n");
1445 for (auto HarnessFile : TestHarnesses) {
1446 LLVM_DEBUG(dbgs() << " " << HarnessFile << "\n");
1447 auto ObjBuffer = getFile(HarnessFile);
1448 if (!ObjBuffer)
1449 return ObjBuffer.takeError();
1450 if (auto Err = S.ObjLayer.add(*S.MainJD, std::move(*ObjBuffer)))
1451 return Err;
1453 return Error::success();
1456 static Error addObjects(Session &S,
1457 const std::map<unsigned, JITDylib *> &IdxToJD) {
1459 // Load each object into the corresponding JITDylib..
1460 LLVM_DEBUG(dbgs() << "Adding objects...\n");
1461 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
1462 InputFileItr != InputFileEnd; ++InputFileItr) {
1463 unsigned InputFileArgIdx =
1464 InputFiles.getPosition(InputFileItr - InputFiles.begin());
1465 const std::string &InputFile = *InputFileItr;
1466 if (StringRef(InputFile).endswith(".a") ||
1467 StringRef(InputFile).endswith(".lib"))
1468 continue;
1469 auto &JD = *std::prev(IdxToJD.lower_bound(InputFileArgIdx))->second;
1470 LLVM_DEBUG(dbgs() << " " << InputFileArgIdx << ": \"" << InputFile
1471 << "\" to " << JD.getName() << "\n";);
1472 auto ObjBuffer = getFile(InputFile);
1473 if (!ObjBuffer)
1474 return ObjBuffer.takeError();
1476 if (S.HarnessFiles.empty()) {
1477 if (auto Err = S.ObjLayer.add(JD, std::move(*ObjBuffer)))
1478 return Err;
1479 } else {
1480 // We're in -harness mode. Use a custom interface for this
1481 // test object.
1482 auto ObjInterface =
1483 getTestObjectFileInterface(S, (*ObjBuffer)->getMemBufferRef());
1484 if (!ObjInterface)
1485 return ObjInterface.takeError();
1486 if (auto Err = S.ObjLayer.add(JD, std::move(*ObjBuffer),
1487 std::move(*ObjInterface)))
1488 return Err;
1492 return Error::success();
1495 static Expected<MaterializationUnit::Interface>
1496 getObjectFileInterfaceHidden(ExecutionSession &ES, MemoryBufferRef ObjBuffer) {
1497 auto I = getObjectFileInterface(ES, ObjBuffer);
1498 if (I) {
1499 for (auto &KV : I->SymbolFlags)
1500 KV.second &= ~JITSymbolFlags::Exported;
1502 return I;
1505 static SmallVector<StringRef, 5> getSearchPathsFromEnvVar(Session &S) {
1506 // FIXME: Handle EPC environment.
1507 SmallVector<StringRef, 5> PathVec;
1508 auto TT = S.ES.getTargetTriple();
1509 if (TT.isOSBinFormatCOFF())
1510 StringRef(getenv("PATH")).split(PathVec, ";");
1511 else if (TT.isOSBinFormatELF())
1512 StringRef(getenv("LD_LIBRARY_PATH")).split(PathVec, ":");
1514 return PathVec;
1517 static Error addLibraries(Session &S,
1518 const std::map<unsigned, JITDylib *> &IdxToJD) {
1520 // 1. Collect search paths for each JITDylib.
1521 DenseMap<const JITDylib *, SmallVector<StringRef, 2>> JDSearchPaths;
1523 for (auto LSPItr = LibrarySearchPaths.begin(),
1524 LSPEnd = LibrarySearchPaths.end();
1525 LSPItr != LSPEnd; ++LSPItr) {
1526 unsigned LibrarySearchPathIdx =
1527 LibrarySearchPaths.getPosition(LSPItr - LibrarySearchPaths.begin());
1528 auto &JD = *std::prev(IdxToJD.lower_bound(LibrarySearchPathIdx))->second;
1530 StringRef LibrarySearchPath = *LSPItr;
1531 if (sys::fs::get_file_type(LibrarySearchPath) !=
1532 sys::fs::file_type::directory_file)
1533 return make_error<StringError>("While linking " + JD.getName() + ", -L" +
1534 LibrarySearchPath +
1535 " does not point to a directory",
1536 inconvertibleErrorCode());
1538 JDSearchPaths[&JD].push_back(*LSPItr);
1541 LLVM_DEBUG({
1542 if (!JDSearchPaths.empty())
1543 dbgs() << "Search paths:\n";
1544 for (auto &KV : JDSearchPaths) {
1545 dbgs() << " " << KV.first->getName() << ": [";
1546 for (auto &LibSearchPath : KV.second)
1547 dbgs() << " \"" << LibSearchPath << "\"";
1548 dbgs() << " ]\n";
1552 // 2. Collect library loads
1553 struct LibraryLoad {
1554 std::string LibName;
1555 bool IsPath = false;
1556 unsigned Position;
1557 StringRef *CandidateExtensions;
1558 enum { Standard, Hidden } Modifier;
1561 // Queue to load library as in the order as it appears in the argument list.
1562 std::deque<LibraryLoad> LibraryLoadQueue;
1563 // Add archive files from the inputs to LibraryLoads.
1564 for (auto InputFileItr = InputFiles.begin(), InputFileEnd = InputFiles.end();
1565 InputFileItr != InputFileEnd; ++InputFileItr) {
1566 StringRef InputFile = *InputFileItr;
1567 if (!InputFile.endswith(".a") && !InputFile.endswith(".lib"))
1568 continue;
1569 LibraryLoad LL;
1570 LL.LibName = InputFile.str();
1571 LL.IsPath = true;
1572 LL.Position = InputFiles.getPosition(InputFileItr - InputFiles.begin());
1573 LL.CandidateExtensions = nullptr;
1574 LL.Modifier = LibraryLoad::Standard;
1575 LibraryLoadQueue.push_back(std::move(LL));
1578 // Add -load_hidden arguments to LibraryLoads.
1579 for (auto LibItr = LoadHidden.begin(), LibEnd = LoadHidden.end();
1580 LibItr != LibEnd; ++LibItr) {
1581 LibraryLoad LL;
1582 LL.LibName = *LibItr;
1583 LL.IsPath = true;
1584 LL.Position = LoadHidden.getPosition(LibItr - LoadHidden.begin());
1585 LL.CandidateExtensions = nullptr;
1586 LL.Modifier = LibraryLoad::Hidden;
1587 LibraryLoadQueue.push_back(std::move(LL));
1589 StringRef StandardExtensions[] = {".so", ".dylib", ".dll", ".a", ".lib"};
1590 StringRef DynLibExtensionsOnly[] = {".so", ".dylib", ".dll"};
1591 StringRef ArchiveExtensionsOnly[] = {".a", ".lib"};
1593 // Add -lx arguments to LibraryLoads.
1594 for (auto LibItr = Libraries.begin(), LibEnd = Libraries.end();
1595 LibItr != LibEnd; ++LibItr) {
1596 LibraryLoad LL;
1597 LL.LibName = *LibItr;
1598 LL.Position = Libraries.getPosition(LibItr - Libraries.begin());
1599 LL.CandidateExtensions = StandardExtensions;
1600 LL.Modifier = LibraryLoad::Standard;
1601 LibraryLoadQueue.push_back(std::move(LL));
1604 // Add -hidden-lx arguments to LibraryLoads.
1605 for (auto LibHiddenItr = LibrariesHidden.begin(),
1606 LibHiddenEnd = LibrariesHidden.end();
1607 LibHiddenItr != LibHiddenEnd; ++LibHiddenItr) {
1608 LibraryLoad LL;
1609 LL.LibName = *LibHiddenItr;
1610 LL.Position =
1611 LibrariesHidden.getPosition(LibHiddenItr - LibrariesHidden.begin());
1612 LL.CandidateExtensions = ArchiveExtensionsOnly;
1613 LL.Modifier = LibraryLoad::Hidden;
1614 LibraryLoadQueue.push_back(std::move(LL));
1617 // If there are any load-<modified> options then turn on flag overrides
1618 // to avoid flag mismatch errors.
1619 if (!LibrariesHidden.empty() || !LoadHidden.empty())
1620 S.ObjLayer.setOverrideObjectFlagsWithResponsibilityFlags(true);
1622 // Sort library loads by position in the argument list.
1623 llvm::sort(LibraryLoadQueue,
1624 [](const LibraryLoad &LHS, const LibraryLoad &RHS) {
1625 return LHS.Position < RHS.Position;
1628 // 3. Process library loads.
1629 auto AddArchive = [&](const char *Path, const LibraryLoad &LL)
1630 -> Expected<std::unique_ptr<StaticLibraryDefinitionGenerator>> {
1631 unique_function<Expected<MaterializationUnit::Interface>(
1632 ExecutionSession & ES, MemoryBufferRef ObjBuffer)>
1633 GetObjFileInterface;
1634 switch (LL.Modifier) {
1635 case LibraryLoad::Standard:
1636 GetObjFileInterface = getObjectFileInterface;
1637 break;
1638 case LibraryLoad::Hidden:
1639 GetObjFileInterface = getObjectFileInterfaceHidden;
1640 break;
1642 auto G = StaticLibraryDefinitionGenerator::Load(
1643 S.ObjLayer, Path, std::move(GetObjFileInterface));
1644 if (!G)
1645 return G.takeError();
1647 // Push additional dynamic libraries to search.
1648 // Note that this mechanism only happens in COFF.
1649 for (auto FileName : (*G)->getImportedDynamicLibraries()) {
1650 LibraryLoad NewLL;
1651 auto FileNameRef = StringRef(FileName);
1652 if (!FileNameRef.ends_with_insensitive(".dll"))
1653 return make_error<StringError>(
1654 "COFF Imported library not ending with dll extension?",
1655 inconvertibleErrorCode());
1656 NewLL.LibName = FileNameRef.drop_back(strlen(".dll")).str();
1657 NewLL.Position = LL.Position;
1658 NewLL.CandidateExtensions = DynLibExtensionsOnly;
1659 NewLL.Modifier = LibraryLoad::Standard;
1660 LibraryLoadQueue.push_front(std::move(NewLL));
1662 return G;
1665 SmallVector<StringRef, 5> SystemSearchPaths;
1666 if (SearchSystemLibrary.getValue())
1667 SystemSearchPaths = getSearchPathsFromEnvVar(S);
1668 while (!LibraryLoadQueue.empty()) {
1669 bool LibFound = false;
1670 auto LL = LibraryLoadQueue.front();
1671 LibraryLoadQueue.pop_front();
1672 auto &JD = *std::prev(IdxToJD.lower_bound(LL.Position))->second;
1674 // If this is the name of a JITDylib then link against that.
1675 if (auto *LJD = S.ES.getJITDylibByName(LL.LibName)) {
1676 JD.addToLinkOrder(*LJD);
1677 continue;
1680 if (LL.IsPath) {
1681 auto G = AddArchive(LL.LibName.c_str(), LL);
1682 if (!G)
1683 return createFileError(LL.LibName, G.takeError());
1684 JD.addGenerator(std::move(*G));
1685 LLVM_DEBUG({
1686 dbgs() << "Adding generator for static library " << LL.LibName << " to "
1687 << JD.getName() << "\n";
1689 continue;
1692 // Otherwise look through the search paths.
1693 auto CurJDSearchPaths = JDSearchPaths[&JD];
1694 for (StringRef SearchPath :
1695 concat<StringRef>(CurJDSearchPaths, SystemSearchPaths)) {
1696 for (const char *LibExt : {".dylib", ".so", ".dll", ".a", ".lib"}) {
1697 SmallVector<char, 256> LibPath;
1698 LibPath.reserve(SearchPath.size() + strlen("lib") + LL.LibName.size() +
1699 strlen(LibExt) + 2); // +2 for pathsep, null term.
1700 llvm::copy(SearchPath, std::back_inserter(LibPath));
1701 if (StringRef(LibExt) != ".lib" && StringRef(LibExt) != ".dll")
1702 sys::path::append(LibPath, "lib" + LL.LibName + LibExt);
1703 else
1704 sys::path::append(LibPath, LL.LibName + LibExt);
1705 LibPath.push_back('\0');
1707 // Skip missing or non-regular paths.
1708 if (sys::fs::get_file_type(LibPath.data()) !=
1709 sys::fs::file_type::regular_file) {
1710 continue;
1713 file_magic Magic;
1714 if (auto EC = identify_magic(LibPath, Magic)) {
1715 // If there was an error loading the file then skip it.
1716 LLVM_DEBUG({
1717 dbgs() << "Library search found \"" << LibPath
1718 << "\", but could not identify file type (" << EC.message()
1719 << "). Skipping.\n";
1721 continue;
1724 // We identified the magic. Assume that we can load it -- we'll reset
1725 // in the default case.
1726 LibFound = true;
1727 switch (Magic) {
1728 case file_magic::pecoff_executable:
1729 case file_magic::elf_shared_object:
1730 case file_magic::macho_dynamically_linked_shared_lib: {
1731 if (auto Err = S.loadAndLinkDynamicLibrary(JD, LibPath.data()))
1732 return Err;
1733 break;
1735 case file_magic::archive:
1736 case file_magic::macho_universal_binary: {
1737 auto G = AddArchive(LibPath.data(), LL);
1738 if (!G)
1739 return G.takeError();
1740 JD.addGenerator(std::move(*G));
1741 LLVM_DEBUG({
1742 dbgs() << "Adding generator for static library " << LibPath.data()
1743 << " to " << JD.getName() << "\n";
1745 break;
1747 default:
1748 // This file isn't a recognized library kind.
1749 LLVM_DEBUG({
1750 dbgs() << "Library search found \"" << LibPath
1751 << "\", but file type is not supported. Skipping.\n";
1753 LibFound = false;
1754 break;
1756 if (LibFound)
1757 break;
1759 if (LibFound)
1760 break;
1763 if (!LibFound)
1764 return make_error<StringError>("While linking " + JD.getName() +
1765 ", could not find library for -l" +
1766 LL.LibName,
1767 inconvertibleErrorCode());
1770 return Error::success();
1773 static Error addSessionInputs(Session &S) {
1774 std::map<unsigned, JITDylib *> IdxToJD;
1776 if (auto Err = createJITDylibs(S, IdxToJD))
1777 return Err;
1779 if (auto Err = addAbsoluteSymbols(S, IdxToJD))
1780 return Err;
1782 if (auto Err = addAliases(S, IdxToJD))
1783 return Err;
1785 if (!TestHarnesses.empty())
1786 if (auto Err = addTestHarnesses(S))
1787 return Err;
1789 if (auto Err = addObjects(S, IdxToJD))
1790 return Err;
1792 if (auto Err = addLibraries(S, IdxToJD))
1793 return Err;
1795 return Error::success();
1798 namespace {
1799 struct TargetInfo {
1800 const Target *TheTarget;
1801 std::unique_ptr<MCSubtargetInfo> STI;
1802 std::unique_ptr<MCRegisterInfo> MRI;
1803 std::unique_ptr<MCAsmInfo> MAI;
1804 std::unique_ptr<MCContext> Ctx;
1805 std::unique_ptr<MCDisassembler> Disassembler;
1806 std::unique_ptr<MCInstrInfo> MII;
1807 std::unique_ptr<MCInstrAnalysis> MIA;
1808 std::unique_ptr<MCInstPrinter> InstPrinter;
1810 } // anonymous namespace
1812 static TargetInfo
1813 getTargetInfo(const Triple &TT,
1814 const SubtargetFeatures &TF = SubtargetFeatures()) {
1815 auto TripleName = TT.str();
1816 std::string ErrorStr;
1817 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, ErrorStr);
1818 if (!TheTarget)
1819 ExitOnErr(make_error<StringError>("Error accessing target '" + TripleName +
1820 "': " + ErrorStr,
1821 inconvertibleErrorCode()));
1823 std::unique_ptr<MCSubtargetInfo> STI(
1824 TheTarget->createMCSubtargetInfo(TripleName, "", TF.getString()));
1825 if (!STI)
1826 ExitOnErr(
1827 make_error<StringError>("Unable to create subtarget for " + TripleName,
1828 inconvertibleErrorCode()));
1830 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
1831 if (!MRI)
1832 ExitOnErr(make_error<StringError>("Unable to create target register info "
1833 "for " +
1834 TripleName,
1835 inconvertibleErrorCode()));
1837 MCTargetOptions MCOptions;
1838 std::unique_ptr<MCAsmInfo> MAI(
1839 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
1840 if (!MAI)
1841 ExitOnErr(make_error<StringError>("Unable to create target asm info " +
1842 TripleName,
1843 inconvertibleErrorCode()));
1845 auto Ctx = std::make_unique<MCContext>(Triple(TripleName), MAI.get(),
1846 MRI.get(), STI.get());
1848 std::unique_ptr<MCDisassembler> Disassembler(
1849 TheTarget->createMCDisassembler(*STI, *Ctx));
1850 if (!Disassembler)
1851 ExitOnErr(make_error<StringError>("Unable to create disassembler for " +
1852 TripleName,
1853 inconvertibleErrorCode()));
1855 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
1856 if (!MII)
1857 ExitOnErr(make_error<StringError>("Unable to create instruction info for" +
1858 TripleName,
1859 inconvertibleErrorCode()));
1861 std::unique_ptr<MCInstrAnalysis> MIA(
1862 TheTarget->createMCInstrAnalysis(MII.get()));
1863 if (!MIA)
1864 ExitOnErr(make_error<StringError>(
1865 "Unable to create instruction analysis for" + TripleName,
1866 inconvertibleErrorCode()));
1868 std::unique_ptr<MCInstPrinter> InstPrinter(
1869 TheTarget->createMCInstPrinter(Triple(TripleName), 0, *MAI, *MII, *MRI));
1870 if (!InstPrinter)
1871 ExitOnErr(make_error<StringError>(
1872 "Unable to create instruction printer for" + TripleName,
1873 inconvertibleErrorCode()));
1874 return {TheTarget, std::move(STI), std::move(MRI),
1875 std::move(MAI), std::move(Ctx), std::move(Disassembler),
1876 std::move(MII), std::move(MIA), std::move(InstPrinter)};
1878 static Error runChecks(Session &S, Triple TT, SubtargetFeatures Features) {
1879 if (CheckFiles.empty())
1880 return Error::success();
1882 LLVM_DEBUG(dbgs() << "Running checks...\n");
1884 auto IsSymbolValid = [&S](StringRef Symbol) {
1885 return S.isSymbolRegistered(Symbol);
1888 auto GetSymbolInfo = [&S](StringRef Symbol) {
1889 return S.findSymbolInfo(Symbol, "Can not get symbol info");
1892 auto GetSectionInfo = [&S](StringRef FileName, StringRef SectionName) {
1893 return S.findSectionInfo(FileName, SectionName);
1896 auto GetStubInfo = [&S](StringRef FileName, StringRef SectionName) {
1897 return S.findStubInfo(FileName, SectionName);
1900 auto GetGOTInfo = [&S](StringRef FileName, StringRef SectionName) {
1901 return S.findGOTEntryInfo(FileName, SectionName);
1904 RuntimeDyldChecker Checker(
1905 IsSymbolValid, GetSymbolInfo, GetSectionInfo, GetStubInfo, GetGOTInfo,
1906 S.ES.getTargetTriple().isLittleEndian() ? llvm::endianness::little
1907 : llvm::endianness::big,
1908 TT, StringRef(), Features, dbgs());
1910 std::string CheckLineStart = "# " + CheckName + ":";
1911 for (auto &CheckFile : CheckFiles) {
1912 auto CheckerFileBuf = ExitOnErr(getFile(CheckFile));
1913 if (!Checker.checkAllRulesInBuffer(CheckLineStart, &*CheckerFileBuf))
1914 ExitOnErr(make_error<StringError>(
1915 "Some checks in " + CheckFile + " failed", inconvertibleErrorCode()));
1918 return Error::success();
1921 static Error addSelfRelocations(LinkGraph &G) {
1922 auto TI = getTargetInfo(G.getTargetTriple());
1923 for (auto *Sym : G.defined_symbols())
1924 if (Sym->isCallable())
1925 if (auto Err = addFunctionPointerRelocationsToCurrentSymbol(
1926 *Sym, G, *TI.Disassembler, *TI.MIA))
1927 return Err;
1928 return Error::success();
1931 static Expected<ExecutorSymbolDef> getMainEntryPoint(Session &S) {
1932 return S.ES.lookup(S.JDSearchOrder, S.ES.intern(EntryPointName));
1935 static Expected<ExecutorSymbolDef> getOrcRuntimeEntryPoint(Session &S) {
1936 std::string RuntimeEntryPoint = "__orc_rt_run_program_wrapper";
1937 if (S.ES.getTargetTriple().getObjectFormat() == Triple::MachO)
1938 RuntimeEntryPoint = '_' + RuntimeEntryPoint;
1939 return S.ES.lookup(S.JDSearchOrder, S.ES.intern(RuntimeEntryPoint));
1942 static Expected<ExecutorSymbolDef> getEntryPoint(Session &S) {
1943 ExecutorSymbolDef EntryPoint;
1945 // Find the entry-point function unconditionally, since we want to force
1946 // it to be materialized to collect stats.
1947 if (auto EP = getMainEntryPoint(S))
1948 EntryPoint = *EP;
1949 else
1950 return EP.takeError();
1951 LLVM_DEBUG({
1952 dbgs() << "Using entry point \"" << EntryPointName
1953 << "\": " << formatv("{0:x16}", EntryPoint.getAddress()) << "\n";
1956 // If we're running with the ORC runtime then replace the entry-point
1957 // with the __orc_rt_run_program symbol.
1958 if (!OrcRuntime.empty()) {
1959 if (auto EP = getOrcRuntimeEntryPoint(S))
1960 EntryPoint = *EP;
1961 else
1962 return EP.takeError();
1963 LLVM_DEBUG({
1964 dbgs() << "(called via __orc_rt_run_program_wrapper at "
1965 << formatv("{0:x16}", EntryPoint.getAddress()) << ")\n";
1969 return EntryPoint;
1972 static Expected<int> runWithRuntime(Session &S, ExecutorAddr EntryPointAddr) {
1973 StringRef DemangledEntryPoint = EntryPointName;
1974 if (S.ES.getTargetTriple().getObjectFormat() == Triple::MachO &&
1975 DemangledEntryPoint.front() == '_')
1976 DemangledEntryPoint = DemangledEntryPoint.drop_front();
1977 using llvm::orc::shared::SPSString;
1978 using SPSRunProgramSig =
1979 int64_t(SPSString, SPSString, shared::SPSSequence<SPSString>);
1980 int64_t Result;
1981 if (auto Err = S.ES.callSPSWrapper<SPSRunProgramSig>(
1982 EntryPointAddr, Result, S.MainJD->getName(), DemangledEntryPoint,
1983 static_cast<std::vector<std::string> &>(InputArgv)))
1984 return std::move(Err);
1985 return Result;
1988 static Expected<int> runWithoutRuntime(Session &S,
1989 ExecutorAddr EntryPointAddr) {
1990 return S.ES.getExecutorProcessControl().runAsMain(EntryPointAddr, InputArgv);
1993 namespace {
1994 struct JITLinkTimers {
1995 TimerGroup JITLinkTG{"llvm-jitlink timers", "timers for llvm-jitlink phases"};
1996 Timer LoadObjectsTimer{"load", "time to load/add object files", JITLinkTG};
1997 Timer LinkTimer{"link", "time to link object files", JITLinkTG};
1998 Timer RunTimer{"run", "time to execute jitlink'd code", JITLinkTG};
2000 } // namespace
2002 int main(int argc, char *argv[]) {
2003 InitLLVM X(argc, argv);
2005 InitializeAllTargetInfos();
2006 InitializeAllTargetMCs();
2007 InitializeAllDisassemblers();
2009 cl::HideUnrelatedOptions({&JITLinkCategory, &getColorCategory()});
2010 cl::ParseCommandLineOptions(argc, argv, "llvm jitlink tool");
2011 ExitOnErr.setBanner(std::string(argv[0]) + ": ");
2013 /// If timers are enabled, create a JITLinkTimers instance.
2014 std::unique_ptr<JITLinkTimers> Timers =
2015 ShowTimes ? std::make_unique<JITLinkTimers>() : nullptr;
2017 auto [TT, Features] = getFirstFileTripleAndFeatures();
2018 ExitOnErr(sanitizeArguments(TT, argv[0]));
2020 auto S = ExitOnErr(Session::Create(TT, Features));
2022 enableStatistics(*S, !OrcRuntime.empty());
2025 TimeRegion TR(Timers ? &Timers->LoadObjectsTimer : nullptr);
2026 ExitOnErr(addSessionInputs(*S));
2029 if (PhonyExternals)
2030 addPhonyExternalsGenerator(*S);
2032 if (ShowInitialExecutionSessionState)
2033 S->ES.dump(outs());
2035 Expected<ExecutorSymbolDef> EntryPoint((ExecutorSymbolDef()));
2037 ExpectedAsOutParameter<ExecutorSymbolDef> _(&EntryPoint);
2038 TimeRegion TR(Timers ? &Timers->LinkTimer : nullptr);
2039 EntryPoint = getEntryPoint(*S);
2042 // Print any reports regardless of whether we succeeded or failed.
2043 if (ShowEntryExecutionSessionState)
2044 S->ES.dump(outs());
2046 if (ShowAddrs)
2047 S->dumpSessionInfo(outs());
2049 if (!EntryPoint) {
2050 if (Timers)
2051 Timers->JITLinkTG.printAll(errs());
2052 reportLLVMJITLinkError(EntryPoint.takeError());
2053 exit(1);
2056 ExitOnErr(runChecks(*S, std::move(TT), std::move(Features)));
2058 int Result = 0;
2059 if (!NoExec) {
2060 LLVM_DEBUG(dbgs() << "Running \"" << EntryPointName << "\"...\n");
2061 TimeRegion TR(Timers ? &Timers->RunTimer : nullptr);
2062 if (!OrcRuntime.empty())
2063 Result =
2064 ExitOnErr(runWithRuntime(*S, ExecutorAddr(EntryPoint->getAddress())));
2065 else
2066 Result = ExitOnErr(
2067 runWithoutRuntime(*S, ExecutorAddr(EntryPoint->getAddress())));
2070 // Destroy the session.
2071 ExitOnErr(S->ES.endSession());
2072 S.reset();
2074 if (Timers)
2075 Timers->JITLinkTG.printAll(errs());
2077 // If the executing code set a test result override then use that.
2078 if (UseTestResultOverride)
2079 Result = TestResultOverride;
2081 return Result;