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