xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.cpp (revision a8089ea5aee578e08acab2438e82fc9a9ae50ed8)
1 //===---------------- EPCDynamicLibrarySearchGenerator.cpp ----------------===//
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 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
10 
11 namespace llvm {
12 namespace orc {
13 
14 Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>>
15 EPCDynamicLibrarySearchGenerator::Load(
16     ExecutionSession &ES, const char *LibraryPath, SymbolPredicate Allow,
17     AddAbsoluteSymbolsFn AddAbsoluteSymbols) {
18   auto Handle = ES.getExecutorProcessControl().loadDylib(LibraryPath);
19   if (!Handle)
20     return Handle.takeError();
21 
22   return std::make_unique<EPCDynamicLibrarySearchGenerator>(
23       ES, *Handle, std::move(Allow), std::move(AddAbsoluteSymbols));
24 }
25 
26 Error EPCDynamicLibrarySearchGenerator::tryToGenerate(
27     LookupState &LS, LookupKind K, JITDylib &JD,
28     JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) {
29 
30   if (Symbols.empty())
31     return Error::success();
32 
33   SymbolLookupSet LookupSymbols;
34 
35   for (auto &KV : Symbols) {
36     // Skip symbols that don't match the filter.
37     if (Allow && !Allow(KV.first))
38       continue;
39     LookupSymbols.add(KV.first, SymbolLookupFlags::WeaklyReferencedSymbol);
40   }
41 
42   SymbolMap NewSymbols;
43 
44   ExecutorProcessControl::LookupRequest Request(H, LookupSymbols);
45   auto Result = EPC.lookupSymbols(Request);
46   if (!Result)
47     return Result.takeError();
48 
49   assert(Result->size() == 1 && "Results for more than one library returned");
50   assert(Result->front().size() == LookupSymbols.size() &&
51          "Result has incorrect number of elements");
52 
53   auto ResultI = Result->front().begin();
54   for (auto &KV : LookupSymbols) {
55     if (ResultI->getAddress())
56       NewSymbols[KV.first] = *ResultI;
57     ++ResultI;
58   }
59 
60   // If there were no resolved symbols bail out.
61   if (NewSymbols.empty())
62     return Error::success();
63 
64   // Define resolved symbols.
65   if (AddAbsoluteSymbols)
66     return AddAbsoluteSymbols(JD, std::move(NewSymbols));
67   return JD.define(absoluteSymbols(std::move(NewSymbols)));
68 }
69 
70 } // end namespace orc
71 } // end namespace llvm
72