1 | //===------------ EPCDynamicLibrarySearchGenerator.h ------------*- 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 | // Support loading and searching of dynamic libraries in an executor process |
10 | // via the ExecutorProcessControl class. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_EXECUTIONENGINE_ORC_EPCDYNAMICLIBRARYSEARCHGENERATOR_H |
15 | #define LLVM_EXECUTIONENGINE_ORC_EPCDYNAMICLIBRARYSEARCHGENERATOR_H |
16 | |
17 | #include "llvm/ADT/FunctionExtras.h" |
18 | #include "llvm/ExecutionEngine/Orc/Core.h" |
19 | |
20 | namespace llvm { |
21 | namespace orc { |
22 | |
23 | class ExecutorProcessControl; |
24 | |
25 | class EPCDynamicLibrarySearchGenerator : public DefinitionGenerator { |
26 | public: |
27 | using SymbolPredicate = unique_function<bool(const SymbolStringPtr &)>; |
28 | using AddAbsoluteSymbolsFn = unique_function<Error(JITDylib &, SymbolMap)>; |
29 | |
30 | /// Create a DynamicLibrarySearchGenerator that searches for symbols in the |
31 | /// library with the given handle. |
32 | /// |
33 | /// If the Allow predicate is given then only symbols matching the predicate |
34 | /// will be searched for. If the predicate is not given then all symbols will |
35 | /// be searched for. |
36 | /// |
37 | /// If \p AddAbsoluteSymbols is provided, it is used to add the symbols to the |
38 | /// \c JITDylib; otherwise it uses JD.define(absoluteSymbols(...)). |
39 | EPCDynamicLibrarySearchGenerator( |
40 | ExecutionSession &ES, tpctypes::DylibHandle H, |
41 | SymbolPredicate Allow = SymbolPredicate(), |
42 | AddAbsoluteSymbolsFn AddAbsoluteSymbols = nullptr) |
43 | : EPC(ES.getExecutorProcessControl()), H(H), Allow(std::move(Allow)), |
44 | AddAbsoluteSymbols(std::move(AddAbsoluteSymbols)) {} |
45 | |
46 | /// Permanently loads the library at the given path and, on success, returns |
47 | /// a DynamicLibrarySearchGenerator that will search it for symbol definitions |
48 | /// in the library. On failure returns the reason the library failed to load. |
49 | static Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>> |
50 | Load(ExecutionSession &ES, const char *LibraryPath, |
51 | SymbolPredicate Allow = SymbolPredicate(), |
52 | AddAbsoluteSymbolsFn AddAbsoluteSymbols = nullptr); |
53 | |
54 | /// Creates a EPCDynamicLibrarySearchGenerator that searches for symbols in |
55 | /// the target process. |
56 | static Expected<std::unique_ptr<EPCDynamicLibrarySearchGenerator>> |
57 | GetForTargetProcess(ExecutionSession &ES, |
58 | SymbolPredicate Allow = SymbolPredicate(), |
59 | AddAbsoluteSymbolsFn AddAbsoluteSymbols = nullptr) { |
60 | return Load(ES, LibraryPath: nullptr, Allow: std::move(Allow), AddAbsoluteSymbols: std::move(AddAbsoluteSymbols)); |
61 | } |
62 | |
63 | Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, |
64 | JITDylibLookupFlags JDLookupFlags, |
65 | const SymbolLookupSet &Symbols) override; |
66 | |
67 | private: |
68 | ExecutorProcessControl &EPC; |
69 | tpctypes::DylibHandle H; |
70 | SymbolPredicate Allow; |
71 | AddAbsoluteSymbolsFn AddAbsoluteSymbols; |
72 | }; |
73 | |
74 | } // end namespace orc |
75 | } // end namespace llvm |
76 | |
77 | #endif // LLVM_EXECUTIONENGINE_ORC_EPCDYNAMICLIBRARYSEARCHGENERATOR_H |
78 | |