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