1 //===- LookasideRTDyldMM - Redirect symbol lookup via a functor -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Defines an adapter for RuntimeDyldMM that allows lookups for external
11 // symbols to go via a functor.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H
16 #define LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H
23 /// @brief Defines an adapter for RuntimeDyldMM that allows lookups for external
24 /// symbols to go via a functor, before falling back to the lookup logic
25 /// provided by the underlying RuntimeDyldMM instance.
27 /// This class is useful for redirecting symbol lookup back to various layers
28 /// of a JIT component stack, e.g. to enable lazy module emission.
30 template <typename BaseRTDyldMM, typename ExternalLookupFtor,
31 typename DylibLookupFtor>
32 class LookasideRTDyldMM : public BaseRTDyldMM {
34 /// @brief Create a LookasideRTDyldMM intance.
35 LookasideRTDyldMM(ExternalLookupFtor ExternalLookup,
36 DylibLookupFtor DylibLookup)
37 : ExternalLookup(std::move(ExternalLookup)),
38 DylibLookup(std::move(DylibLookup)) {}
40 /// @brief Look up the given symbol address, first via the functor this
41 /// instance was created with, then (if the symbol isn't found)
42 /// via the underlying RuntimeDyldMM.
43 uint64_t getSymbolAddress(const std::string &Name) override {
44 if (uint64_t Addr = ExternalLookup(Name))
46 return BaseRTDyldMM::getSymbolAddress(Name);
49 uint64_t getSymbolAddressInLogicalDylib(const std::string &Name) override {
50 if (uint64_t Addr = DylibLookup(Name))
52 return BaseRTDyldMM::getSymbolAddressInLogicalDylib(Name);
55 /// @brief Get a reference to the ExternalLookup functor.
56 ExternalLookupFtor &getExternalLookup() { return ExternalLookup; }
58 /// @brief Get a const-reference to the ExternalLookup functor.
59 const ExternalLookupFtor &getExternalLookup() const { return ExternalLookup; }
61 /// @brief Get a reference to the DylibLookup functor.
62 DylibLookupFtor &getDylibLookup() { return DylibLookup; }
64 /// @brief Get a const-reference to the DylibLookup functor.
65 const DylibLookupFtor &getDylibLookup() const { return DylibLookup; }
68 ExternalLookupFtor ExternalLookup;
69 DylibLookupFtor DylibLookup;
72 /// @brief Create a LookasideRTDyldMM from a base memory manager type, an
73 /// external lookup functor, and a dylib lookup functor.
74 template <typename BaseRTDyldMM, typename ExternalLookupFtor,
75 typename DylibLookupFtor>
77 LookasideRTDyldMM<BaseRTDyldMM, ExternalLookupFtor, DylibLookupFtor>>
78 createLookasideRTDyldMM(ExternalLookupFtor &&ExternalLookup,
79 DylibLookupFtor &&DylibLookup) {
80 typedef LookasideRTDyldMM<BaseRTDyldMM, ExternalLookupFtor, DylibLookupFtor>
82 return llvm::make_unique<ThisLookasideMM>(
83 std::forward<ExternalLookupFtor>(ExternalLookup),
84 std::forward<DylibLookupFtor>(DylibLookup));
88 #endif // LLVM_EXECUTIONENGINE_ORC_LOOKASIDERTDYLDMM_H