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