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