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