1 //===---------------- EPCDynamicLibrarySearchGenerator.cpp ----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
11 #include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"
12 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
13 #include "llvm/Support/Error.h"
15 #define DEBUG_TYPE "orc"
20 Expected
<std::unique_ptr
<EPCDynamicLibrarySearchGenerator
>>
21 EPCDynamicLibrarySearchGenerator::Load(
22 ExecutionSession
&ES
, const char *LibraryPath
, SymbolPredicate Allow
,
23 AddAbsoluteSymbolsFn AddAbsoluteSymbols
) {
25 ES
.getExecutorProcessControl().getDylibMgr().loadDylib(LibraryPath
);
27 return Handle
.takeError();
29 return std::make_unique
<EPCDynamicLibrarySearchGenerator
>(
30 ES
, *Handle
, std::move(Allow
), std::move(AddAbsoluteSymbols
));
33 Error
EPCDynamicLibrarySearchGenerator::tryToGenerate(
34 LookupState
&LS
, LookupKind K
, JITDylib
&JD
,
35 JITDylibLookupFlags JDLookupFlags
, const SymbolLookupSet
&Symbols
) {
38 return Error::success();
41 dbgs() << "EPCDynamicLibrarySearchGenerator trying to generate "
45 SymbolLookupSet LookupSymbols
;
47 for (auto &KV
: Symbols
) {
48 // Skip symbols that don't match the filter.
49 if (Allow
&& !Allow(KV
.first
))
51 LookupSymbols
.add(KV
.first
, SymbolLookupFlags::WeaklyReferencedSymbol
);
54 DylibManager::LookupRequest
Request(H
, LookupSymbols
);
55 // Copy-capture LookupSymbols, since LookupRequest keeps a reference.
56 EPC
.getDylibMgr().lookupSymbolsAsync(Request
, [this, &JD
, LS
= std::move(LS
),
58 auto Result
) mutable {
61 dbgs() << "EPCDynamicLibrarySearchGenerator lookup failed due to error";
63 return LS
.continueLookup(Result
.takeError());
66 assert(Result
->size() == 1 && "Results for more than one library returned");
67 assert(Result
->front().size() == LookupSymbols
.size() &&
68 "Result has incorrect number of elements");
71 auto ResultI
= Result
->front().begin();
72 for (auto &KV
: LookupSymbols
) {
73 if (ResultI
->getAddress())
74 NewSymbols
[KV
.first
] = *ResultI
;
79 dbgs() << "EPCDynamicLibrarySearchGenerator lookup returned "
80 << NewSymbols
<< "\n";
83 // If there were no resolved symbols bail out.
84 if (NewSymbols
.empty())
85 return LS
.continueLookup(Error::success());
87 // Define resolved symbols.
88 Error Err
= AddAbsoluteSymbols
89 ? AddAbsoluteSymbols(JD
, std::move(NewSymbols
))
90 : JD
.define(absoluteSymbols(std::move(NewSymbols
)));
92 LS
.continueLookup(std::move(Err
));
95 return Error::success();
98 } // end namespace orc
99 } // end namespace llvm